From nobody Wed Nov 12 00:28:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1567458003; cv=none; d=zoho.com; s=zohoarc; b=HfXwVPAskdoOZhiBnP9YVyBy1fppfaL0pJj4fY3uPUpDOkGkmcVGrHzG9Ki3KkDaa6r6sZFqF7gEZacrHENQyznh2qo8W6u75OVQ+BEtk74DsYmqC40xirHFguCPTaPOmVbDOqod1jD5fIRTByXi7YqHI0iB+dhbjtXLi6rvAHA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567458003; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=VSXNt8Kn3icnDHyUvJOBRa++l6851CYhlNFBcR1U6UQ=; b=DL6Kx0kCJWmyAgWtcPLaD1sgZEr97fX87V6GQf/A01IWzctB0b4hVraeReHeTRvkQ3qalJ7TKjhYNmB6EenUhimzeJOXenIt79Q+35laXaQAi/d8DKyvS44Uc6lRNBaPtXUmx10AMkhPr+K5T92FgQxTmBdSHEnpNKvipx/mB8E= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 156745800363943.16088822844313; Mon, 2 Sep 2019 14:00:03 -0700 (PDT) Received: from localhost ([::1]:39966 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tQd-0006kz-16 for importer@patchew.org; Mon, 02 Sep 2019 16:59:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33402) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPb-0005pe-Qp for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPa-0004Fc-PP for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:55 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:43907) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPa-0004EZ-L1 for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:54 -0400 Received: by mail-qt1-x844.google.com with SMTP id l22so4715029qtp.10 for ; Mon, 02 Sep 2019 13:58:54 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VSXNt8Kn3icnDHyUvJOBRa++l6851CYhlNFBcR1U6UQ=; b=T8oZEohIrzAdd5PFQXwnFFz/IxZ4PpOwm7nxtbmXEb3mZsuYwU8aGee4iyHY4I7z/B wk2h9wQkUzbXjEvII9oeFmcbk6XGUXqxZ7cvAzD+2Kx1jpHBxiqK22giaGFeefdS6U7X RqyuaLhXPcl7pOpysNDtxlOTTjslA8j7FVJLLpvKuEZX8ZUg7mpi2KdVrz9chpaHvee6 rjn613l6EqwoKzO+MPB4XlZeYSMnWzX3j2Ux3I26Pjb56LnZS2jOsNuM91T4LIf37G1I z7blZKuTiRWcKHVtgWjM1nhxCbqLlFBtwP7uYGz6RPFKy6Kf3QfqFfcBwGZ9ncrNfAwV XuUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VSXNt8Kn3icnDHyUvJOBRa++l6851CYhlNFBcR1U6UQ=; b=qZAd0K1WY2iBfRo27XcVQmHxt+mTL1ByQuFzPzsrN1SCQqLie7I3Krx402FEE1ukFZ 4X9l3AmdST57c5JfQWvU05OMHdZ0I4dceWKvC1xo3znbU9RRvJqi40pUxAbDMCrcqXeX uQeEoEJRorvqvwVk6VE8TOydWBpKTCqHlwbgPzsTXQD+E6fh6lsJH0W6gPGkL4jtdCrB xXwvMcCOO7nm+DMvG/MPQnY2w5mPSG2eMQyaJJ1Dls0EC5F+4wlHUt3wM42ATfMAn13I CqmnEbHKnOWDvtMjTk1Aojang46aGHDTqKEINhdrjYAe3B6k85+VOkKIw/OAfbnGPJhs C00w== X-Gm-Message-State: APjAAAWH6w6cgRvWuREuqcSwWE9YTdc5ITkuq7DxiRFuPwnn4mmQCMJr dWAhfSAcGhljXElAXECLrdHpW/r9 X-Google-Smtp-Source: APXvYqxRIDM0dRA3cFdzCsAhfcEZcFNcE/CSRHie+LUQNxhNl/MbmwDvUCvJ/fiHpSvCnRiHPdHGNQ== X-Received: by 2002:ac8:6916:: with SMTP id e22mr29288521qtr.313.1567457933847; Mon, 02 Sep 2019 13:58:53 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:39 -0300 Message-Id: <20190902205842.1884-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::844 Subject: [Qemu-devel] [PATCH v6 1/4] block: introducing 'bdrv_co_delete_file' interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the 'file' driver in file-posix.c.The implementation is given by 'raw_co_delete_file'. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block_int.h | 6 ++++++ 2 files changed, 34 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index fbeb0068db..52756de522 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2390,6 +2390,33 @@ static int coroutine_fn raw_co_create_opts(const cha= r *filename, QemuOpts *opts, return raw_co_create(&options, errp); } =20 +/** + * Co-routine function that erases a regular file. + */ +static int coroutine_fn raw_co_delete_file(BlockDriverState *bs, + Error **errp) +{ + struct stat st; + int ret; + + if (!(stat(bs->filename, &st) =3D=3D 0) || !S_ISREG(st.st_mode)) { + ret =3D -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", + bs->filename); + goto done; + } + + ret =3D unlink(bs->filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", + bs->filename); + } + +done: + return ret; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2942,6 +2969,7 @@ BlockDriver bdrv_file =3D { .bdrv_co_block_status =3D raw_co_block_status, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes =3D raw_co_pwrite_zeroes, + .bdrv_co_delete_file =3D raw_co_delete_file, =20 .bdrv_co_preadv =3D raw_co_preadv, .bdrv_co_pwritev =3D raw_co_pwritev, diff --git a/include/block/block_int.h b/include/block/block_int.h index ceec8c2f56..1442c75e3c 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -308,6 +308,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(BlockDriverState *bs, + Error **errp); + /* * Flushes all data that was already written to the OS all the way dow= n to * the disk (for example file-posix.c calls fsync()). --=20 2.21.0 From nobody Wed Nov 12 00:28:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1567458185; cv=none; d=zoho.com; s=zohoarc; b=gz+9GoO6mfcaMFfa/2sMyfcUnXD2bhcIYMOP92gw4p0d0XH5WolAyMr5HejihvRX6PLFuDxjlUDOThkob7po7qfKubc/VhcWHenFTr4a70lQ6EjG2TnMA2Oy02FlDJO1VrcqZAcCmLeL1kwfYz6ilLK4kRIVMdPqyf8D70IlJDE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567458185; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=RkpF/3bkRRxXIQKIG0bVJK8VOHWsiINFvPG77fhS7YE=; b=O9oOAS4GmwQKiziisRvJpXjs8jQbUGIcR1/QL6itQhW8+khxUgx8kN14mDTwvkRdfShnpyfpLmi+GoKEyAPgQn/p3ajlhGg5C7LEgzhWKSjLsu0mwCllIeon/xEM47E7tMIx0e/SXiApq6V+RZo5ZnZzbPIvwv7Uf8hVgU6FBcg= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567458185620778.7807698295786; Mon, 2 Sep 2019 14:03:05 -0700 (PDT) Received: from localhost ([::1]:39992 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tTX-0000Kk-CG for importer@patchew.org; Mon, 02 Sep 2019 17:02:59 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33419) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPe-0005r3-EG for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPc-0004KB-6U for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:58 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:35789) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPc-0004J7-1v for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:56 -0400 Received: by mail-qk1-x742.google.com with SMTP id d26so6037372qkk.2 for ; Mon, 02 Sep 2019 13:58:56 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=RkpF/3bkRRxXIQKIG0bVJK8VOHWsiINFvPG77fhS7YE=; b=JQ2CAku9rwLhcVOoSOuGKFgND/LqavPa0Rb4px+UhJHaYPfsWNmaCyLQye03zJOVSm o8Er1lOpzGtGaM1QrQvtyrkuEY6yVSpyB5XE633FQtSHNnKsdb/PvMQ4N1anA448wTxA nstkAjbun0cmPhXjCbwJ3mJnx71Lz3R2j9NAdwiz3Y3YWTHQqI1/iA3QMFoDVVXlnbxV y/itJ+elcMODhjqZMDaqKJQVuAcSF4b5pTgMExa1OBNkodArURVNiGx8mLSpIuBouPYs +EkVFvuYdfL9kCX59sKGJOuRR/p48pfipwyOIMuKrKXywYPGD+6qtbPs0EkltbApw8ZE q7nQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=RkpF/3bkRRxXIQKIG0bVJK8VOHWsiINFvPG77fhS7YE=; b=UBZBjBTW23MgihZSAxSS1PzcCWReAxQ2Ed/AJ1LMpRVltSoc1W40DxDQucCP34eSDi GIIG857T2TNbRdYPX3hmBtlDko+V8tOeSioS50caYVpuHDOMRNbMyLtMtWQL1IWfgx6w pCYSX4Jbv5ANx06Ra4H01ghAfUpwuAmmjhuNhcDY0SB7i8TCepNWiIAetpojiinI4aAu pskJ67aWhihFn7TRibAHeFcHNFKAA/Cv6XWWjmnlfGsVzXWRhADrjoTcLlnPUCok9GdN fwMxIgHtpzO1+ofW9FR4HFvlGXlcQErmNqx0pVyl/jcvuQxXVOhKW9nnoxSseReewqbD 2xFA== X-Gm-Message-State: APjAAAW5pBZB04s7EhVT5az2PYydDnQj/N/b78i4AxOGcyzGlof7R8oN ZnvijNKTWsPCzeekHGpARqDNlD97 X-Google-Smtp-Source: APXvYqzBfD+7w24+bOMM/U4CBluY8YG9UuKj3XU9hlRW37Sxn7dnI79zQSz9QU8x+XuLwotWLCBCsw== X-Received: by 2002:ae9:e648:: with SMTP id x8mr30511276qkl.247.1567457935488; Mon, 02 Sep 2019 13:58:55 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:40 -0300 Message-Id: <20190902205842.1884-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::742 Subject: [Qemu-devel] [PATCH v6 2/4] block.c: adding bdrv_delete_file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Using the new 'bdrv_co_delete_file' interface, bdrv_delete_file can be used in a way similar of the existing bdrv_create_file to to clean up a created file. The logic is also similar to what is already done in bdrv_create_file: a qemu_coroutine is created if needed, a specialized function bdrv_delete_co_entry is used to call the bdrv_co_delete_file co-routine of the driver, if the driver implements it. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 78 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 79 insertions(+) diff --git a/block.c b/block.c index 874a29a983..75212d873a 100644 --- a/block.c +++ b/block.c @@ -548,6 +548,84 @@ int bdrv_create_file(const char *filename, QemuOpts *o= pts, Error **errp) return ret; } =20 +typedef struct DeleteCo { + BlockDriver *drv; + BlockDriverState *bs; + int ret; + Error *err; +} DeleteCo; + +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err =3D NULL; + DeleteCo *dco =3D opaque; + + assert(dco->bs); + + dco->ret =3D dco->drv->bdrv_co_delete_file(dco->bs, &local_err); + error_propagate(&dco->err, local_err); +} + +int bdrv_delete_file(const char *filename, Error **errp) +{ + BlockDriver *drv =3D bdrv_find_protocol(filename, true, NULL); + BlockDriverState *bs =3D bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_PROTOCOL, + NULL); + DeleteCo dco =3D { + .drv =3D drv, + .bs =3D bs, + .ret =3D NOT_DONE, + .err =3D NULL, + }; + Coroutine *co; + int ret; + + if (!drv) { + error_setg(errp, "File '%s' has unknown format", filename); + ret =3D -ENOMEDIUM; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret =3D -ENOTSUP; + goto out; + } + + if (!bs) { + error_setg(errp, "Could not open image '%s' for erasing", + filename); + ret =3D -1; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&dco); + } else { + co =3D qemu_coroutine_create(bdrv_delete_co_entry, &dco); + qemu_coroutine_enter(co); + while (dco.ret =3D=3D NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret =3D dco.ret; + if (ret < 0) { + if (dco.err) { + error_propagate(errp, dco.err); + } else { + error_setg_errno(errp, -ret, "Could not delete image"); + } + } + +out: + bdrv_unref(bs); + return ret; +} + /** * Try to get @bs's logical and physical block size. * On success, store them in @bsz struct and return 0. diff --git a/include/block/block.h b/include/block/block.h index 124ad40809..dbe737202b 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -374,6 +374,7 @@ bool bdrv_is_backing_chain_frozen(BlockDriverState *bs,= BlockDriverState *base, int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); +int bdrv_delete_file(const char *filename, Error **errp); =20 =20 typedef struct BdrvCheckResult { --=20 2.21.0 From nobody Wed Nov 12 00:28:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1567458181; cv=none; d=zoho.com; s=zohoarc; b=hTu4VDzMyBJARH104+DpqJz/gn5vKGshFYoljY7KvJmcEY8ZlqRpqz4+RRL8y0Ee/24WmoKZE5zaxVevOJPVpxM2TiRR9S6vDXeyQvlMXgHV2rsfqsHUeMWwPuuGukgRkz7d/4Gd9cw9bNc+mdmNQwKKYwCuFItW5Mk+FDHP6RQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567458181; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=G+EoknQSNzb4lwqKyLQnOAIJR7EMqZnQXZCcPxFT6l4/sEZQUyMflVk6bJFx47r0LiMmjJr0hDSvYEjuFFHF5dGgWBH9yzpjzDZBc06Cp6Ekby9l7kINfb5M5NaOGtcWTKvzBqJ5ucoQSfW5JsNlE6LiEQFBIavbuGpUPf1fZTM= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567458181320658.7371281943771; Mon, 2 Sep 2019 14:03:01 -0700 (PDT) Received: from localhost ([::1]:39990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tTU-0000Cf-QZ for importer@patchew.org; Mon, 02 Sep 2019 17:02:56 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33429) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPf-0005rz-Bo for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:59:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPd-0004Mu-QL for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:59 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:44465) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPd-0004MG-Mb for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:57 -0400 Received: by mail-qk1-x742.google.com with SMTP id i78so12180622qke.11 for ; Mon, 02 Sep 2019 13:58:57 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=oPaHINxnFgmuXrpWzFMhoyWEZXP+9V2pMbLQt9juEQ88tbDdeStkCgOqZIcYdW+orD CIbrakIBSQjjsBvmbwlC9KEao2dwXRt9mxLuBW2cJkfTwRstrxA9ernAs1MKo/Jx7r7d 6PH6rsYw9p4DFgbgfKyXv4qDtuPsCRgY8+Mb9kLIPjk7wu9rSakfbzjQfKnigw8lmGtL X24rt/3b9zB2K60gr/qUcyrVefExVBS0FPPmofGjoj8/+OiqizSo7fp0O4+mdwmSoY/W /qoyk/kcFwYZMosNeEtE4m29fZkQ583HrkpeC1SH2Q85vwJ+9dgpXY59crw3kI8MtdqD N85A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=cqXvzeH9JGbK13wh2PF5LuHVAWopqM5fCsamJ7eRinw=; b=jzqoXrg4/PexmC1ZJjWFjUgYMatMhn90w/5uMSiVRBhKdAJRVDNkr+Ik8zsmryLGIh oYZ5tXag2e2WwPxLQKDrOJB3TmmGWnB8+ZjNSbLWZvZTX74lgE5pFXlUIYbzGmvgrmb1 RS5qbDbbZfxdmgPpzqH8vljJKffjG6duHiibfsXuxY0/VVknzX7BLpFY899Z8wW/ggMy 31qh8YeHRCT2xyuZe1NZgaGVXXhKcycbgssDKzthcSqfTS1BGvTudLLx7QXgjwmQa81T wyvzO7tbiDigFdlQ5OrWizYupJjKpSPJ4n6u4Tgf48d7xsngOhdszyzlpOv/b/iHI09V 1rvA== X-Gm-Message-State: APjAAAWFXysf6wYj7zGdUTd+un6JhJF8yXYlV5XaRHs63tEDP6jLB2yR zx78NSSiQwaAbqzO7VFV+MFtrQm2 X-Google-Smtp-Source: APXvYqyUMmVcYv1AW3X0C4NXs+rGJKFZRlm1deAqpvJmqASPdVJt/nrfzUtC7PLBHdyNMrAAR3YBpg== X-Received: by 2002:a05:620a:1e4:: with SMTP id x4mr16266606qkn.150.1567457937206; Mon, 02 Sep 2019 13:58:57 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:41 -0300 Message-Id: <20190902205842.1884-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::742 Subject: [Qemu-devel] [PATCH v6 3/4] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Srikanth Aithal , Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When using a non-UTF8 secret to create a volume using qemu-img, the following error happens: $ qemu-img create -f luks --object secret,id=3Dvol_1_encrypt0,file=3Dvol_re= size_pool.vol_1.secret.qzVQrI -o key-secret=3Dvol_1_encrypt0 /var/tmp/pool_= target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=3Dluks size=3D10485760 key-sec= ret=3Dvol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is no= t valid UTF-8 However, the created file '/var/tmp/pool_target/vol_1' is left behind in the file system after the failure. This behavior can be observed when creating the volume using Libvirt, via 'virsh vol-create', and then getting "volume target path already exist" errors when trying to re-create the volume. The volume file is created inside block_crypto_co_create_opts_luks(), in block/crypto.c. If the bdrv_create_file() call is successful but any succeeding step fails*, the existing 'fail' label does not take into account the created file, leaving it behind. This patch changes block_crypto_co_create_opts_luks() to delete 'filename' in case of failure. A failure in this point means that the volume is now truncated/corrupted, so even if 'filename' was an existing volume before calling qemu-img, it is now unusable. Deleting the file it is not much worse than leaving it in the filesystem in this scenario, and we don't have to deal with checking the file pre-existence in the code. * in our case, block_crypto_co_create_generic calls qcrypto_block_create, which calls qcrypto_block_luks_create, and this function fails when calling qcrypto_secret_lookup_as_utf8. Reported-by: Srikanth Aithal Suggested-by: Kevin Wolf Signed-off-by: Daniel Henrique Barboza --- block/crypto.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 7eb698774e..fe2f0c42b0 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -30,6 +30,7 @@ #include "qapi/error.h" #include "qemu/module.h" #include "qemu/option.h" +#include "qemu/cutils.h" #include "crypto.h" =20 typedef struct BlockCrypto BlockCrypto; @@ -599,6 +600,27 @@ fail: bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + + /* + * If an error occurred, delete the file. Even if the file existed + * beforehand, it has been truncated and corrupted in the process. + */ + if (ret) { + Error *local_err; + int r_del =3D bdrv_delete_file(filename, &local_err); + /* + * ENOTSUP will happen if the block driver doesn't support + * 'bdrv_co_delete_file'. ENOENT will be fired by + * 'raw_co_delete_file' if the file doesn't exist. Both are + * predictable (we're not verifying if the driver supports + * file deletion or if the file was created), thus we + * shouldn't report this back to the user. + */ + if ((r_del < 0) && (r_del !=3D -ENOTSUP) && (r_del !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", bs->filename); + } + } + return ret; } =20 --=20 2.21.0 From nobody Wed Nov 12 00:28:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1567458312; cv=none; d=zoho.com; s=zohoarc; b=Ycnz8Z6xx53xPJPMEWRdeIfRECl8DOLl/A7BUrhHtpRYeMNMtosUctM/rruE9lBwoweJEju6zwFQma/n+kSRaOCxDJjzc38oNRM3ig99pmYRSOCZaCfRHvM6c/7xVH9wuhX+9o8qFP0nN7WrRLH1kzQ8oXIRLwTtUghS6fTJi+A= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1567458312; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To:ARC-Authentication-Results; bh=u9G4IYIbYDj0k9bcX3iHzHaFezRpdzekc5r3iFKBM9A=; b=E1l1RF+iWL/8YnBLWIuA6Zo/CVmFiPOflKSQeKBiY7Y4u/2zgWHU8JapJkV1DQqFrcIeZrj2xWsSqdkB59inp7KkU9xZEmPX7vPiRZP8meDU1P2HCHH572RHHE7aOwvvyiY3F5S7w05QWeM2wPot84NyARpH4na+b9wEE5XLXyE= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1567458312513740.3542163816069; Mon, 2 Sep 2019 14:05:12 -0700 (PDT) Received: from localhost ([::1]:40014 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tVf-00020n-9e for importer@patchew.org; Mon, 02 Sep 2019 17:05:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33442) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPg-0005ts-OC for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:59:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPf-0004PJ-BX for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:59:00 -0400 Received: from mail-qk1-x731.google.com ([2607:f8b0:4864:20::731]:46060) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPf-0004Om-84 for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:59 -0400 Received: by mail-qk1-x731.google.com with SMTP id z67so744942qkb.12 for ; Mon, 02 Sep 2019 13:58:59 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=u9G4IYIbYDj0k9bcX3iHzHaFezRpdzekc5r3iFKBM9A=; b=FXOea6Fzqk8z9B82LH0nnAtVHBnHVinX6vqaEqZ2ZhZ7/cOw1Qble8mDEWbI5Nkb8M 5n8zvoSgOK5AVggmbwMFBqsISZgeqGyjkLtqCAHPK71FI1XMC8ho16x27/KzGI0TJ2ca R5+gIF2JqsOxb/FXmV/u/EFTGI+LSjdDYhUdLWDYDoagqz10xJqJwsmOrYjBZ+JFrOkv qXvJ4rFZHixcMHKQTq1s79Ek3VP07J+QmworPEVaMn6JRfU+tOy1oihEf8/rHjW2dQOg vLHjRXSKqydePUxklvjU1+7v8t3Hlq4mEpMjrLc1unawAv3cOWemr5FXKVqoxNi8viLu vQCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=u9G4IYIbYDj0k9bcX3iHzHaFezRpdzekc5r3iFKBM9A=; b=rEQIHoTa0If+zpkH2Cv9KCFiYvN9Rc1jJC1hEZIVaDm5QvfBTxXccHs7xfYCz4wGL0 lGP49dvHgwuNCsNAV0aEsvr4QdZDaVJ2bvYMvjQqoSFMRgUB5Dyj5ZmxaueHSkJm986b u5WWivhqcXzY6olSboKCzxYu5x26XKKwUhVvOXyUol7dvxGIOXTNIvc91Zp94vtN619S n09xIDuQHPEtyLpRSF15S6j8qm8yWgkdEDheb/xSlxt5jsM6NaxncQD3uuKSK0P6A7/e WzI/d1sGkP1cil/4dRfs+dFfvyCRbnrZoLIzvOGukcyTwjtHypp3drmg6UWafY9RK0B4 2kcw== X-Gm-Message-State: APjAAAXxrjclQe0rpYCbfu2sLuzTfqMiy4hb8xVOSTDg0uMKOUpdAp1y KzBoY1GJkxLHeaKDw1lnPmcdqN/y X-Google-Smtp-Source: APXvYqyxXl+g/BVjWlYYEzchUJPbT8zY/ID1e/zOPzQ5T3wbW2QCRDwL5MrDDSKDdwuNLDExCOFL1g== X-Received: by 2002:ae9:f707:: with SMTP id s7mr3749476qkg.195.1567457938646; Mon, 02 Sep 2019 13:58:58 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:42 -0300 Message-Id: <20190902205842.1884-5-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::731 Subject: [Qemu-devel] [PATCH v6 4/4] qemu-iotests: adding LUKS cleanup for non-UTF8 secret error X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Daniel Henrique Barboza , jsnow@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" This patch adds a new test file, 257, to exercise the case where qemu-img fails to complete for the LUKS format when a non-UTF8 secret is used. Signed-off-by: Daniel Henrique Barboza --- tests/qemu-iotests/259 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/259.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/259 create mode 100644 tests/qemu-iotests/259.out diff --git a/tests/qemu-iotests/259 b/tests/qemu-iotests/259 new file mode 100755 index 0000000000..cb362598b4 --- /dev/null +++ b/tests/qemu-iotests/259 @@ -0,0 +1,67 @@ +#!/usr/bin/env bash +# +# Test qemu-img file cleanup for LUKS when using a non-UTF8 secret +# +# Copyright (C) 2019, IBM Corporation. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +seq=3D`basename $0` +echo "QA output created by $seq" + +status=3D1 # failure is the default! +TEST_IMAGE_FILE=3D'vol.img' + +_cleanup() +{ + _cleanup_test_img + rm non_utf8_secret + rm -f $TEST_IMAGE_FILE +} +trap "_cleanup; exit \$status" 0 1 2 3 15 + +# get standard environment, filters and checks +. ./common.rc +. ./common.filter + +_supported_fmt luks +_supported_proto generic +_unsupported_proto vxhs + +echo "=3D=3D Create non-UTF8 secret =3D=3D" +echo -n -e '\x3a\x3c\x3b\xff' > non_utf8_secret +SECRET=3D"secret,id=3Dsec0,file=3Dnon_utf8_secret" + +echo "=3D=3D Throws an error because of invalid UTF-8 secret =3D=3D" +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=3Dsec0" $TEST_= IMAGE_FILE 4M + +echo "=3D=3D Image file should not exist after the error =3D=3D" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +echo "=3D=3D Create a stub image file and run qemu-img again =3D=3D" +touch $TEST_IMAGE_FILE +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=3Dsec0" $TEST_= IMAGE_FILE 4M + +echo "=3D=3D Pre-existing image file should also be deleted after the erro= r =3D=3D" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +# success, all done +echo "*** done" +rm -f $seq.full +status=3D0 diff --git a/tests/qemu-iotests/259.out b/tests/qemu-iotests/259.out new file mode 100644 index 0000000000..6b0188111c --- /dev/null +++ b/tests/qemu-iotests/259.out @@ -0,0 +1,11 @@ +QA output created by 259 +=3D=3D Create non-UTF8 secret =3D=3D +=3D=3D Throws an error because of invalid UTF-8 secret =3D=3D +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=3Dluks size=3D4194304 key-secret=3Dsec0 +=3D=3D Image file should not exist after the error =3D=3D +=3D=3D Create a stub image file and run qemu-img again =3D=3D +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=3Dluks size=3D4194304 key-secret=3Dsec0 +=3D=3D Pre-existing image file should also be deleted after the error =3D= =3D + *** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index d95d556414..67c3faf80f 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -273,4 +273,5 @@ 256 rw quick 257 rw 258 rw quick +259 rw auto quick 262 rw quick migration --=20 2.21.0