From nobody Tue May 21 08:35:12 2024 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553278088938734.6446752690103; Fri, 22 Mar 2019 11:08:08 -0700 (PDT) Received: from localhost ([127.0.0.1]:32870 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OaF-0007FY-Qy for importer@patchew.org; Fri, 22 Mar 2019 14:07:59 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OVH-0002dX-GD for qemu-devel@nongnu.org; Fri, 22 Mar 2019 14:02:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h7OLq-0000RT-6O for qemu-devel@nongnu.org; Fri, 22 Mar 2019 13:53:07 -0400 Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]:35628) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h7OLp-0000Ql-Pe; Fri, 22 Mar 2019 13:53:06 -0400 Received: by mail-ed1-x542.google.com with SMTP id d6so2394822eds.2; Fri, 22 Mar 2019 10:53:05 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:2d8:6f86:51db:ba10:5f2f]) by smtp.gmail.com with ESMTPSA id l26sm1790053eja.29.2019.03.22.10.53.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 10:53:03 -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=uYEFYsfypM6WGUa6L3XGFA6h8BKJhU8B+mIv1ZMcTEs=; b=Gc5N9fbCO5fIoQXu2OLE+EM7dli8MzpIyjDc4hFON+ZfT8gvF1O9D0z8L97fx3whru WETnejJtOJJCnzSH8b6ZnE1eLQV/Pbs9kfO7qnc1+Rn81RoeSoCnaHUIjQVBB52dg1Vt +/YlrmVnXn5WxN38KRdKQkEdCgUA9q3yb4IakZbU5ZQlSomPfmdCoTefOqOy5tWocpfP wSzI6xAu//MwD4W4tndVh+QM4GvzoDirmdu8h0l1p4ZzUvDoYWb4LWzVKTxDhibOwb5w h+W4xweKw/PzNIadhy0Cn4pORTNlPzHBj49GDRwpSSg1ltHCNHHIp4L3OHIKr4FsvVrT 57BA== 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=uYEFYsfypM6WGUa6L3XGFA6h8BKJhU8B+mIv1ZMcTEs=; b=gp3MKz1zm1W8s9oKqnRIzK9yyIlLSmq42qQXe9xEhyE0/FuLw2uZc6kZHc8namwelu SPwFsYtI8Ed0E1x/bY4CyiiV1SCAa5vjNkQO458z81Z8L8fSYBuPtMKUfq6qqB3ZUtLy ZR8KpzDVHLTvO/ukz65YvuEYf58mMyteFr1Z8TQ1DgQh28Uaa3YWcddnDATW98pBwOW/ Q5noeRzOazr1LCWYfpRt0yk/6zptvL2+gD6sLDh+rkwRHpb+RvYC3tdMl/AjuDrNbT6f s3IMDdMZ3wdIz0yike1aN4YTRLzS5VTluXcn5oKsciG94ms2+BKFcwu/A8pYRe1C1PQX cafw== X-Gm-Message-State: APjAAAVxhFaFmcZv7T51sTu2VqCksObKsPyPquHMp95fiXmxhhf7MRdt wMJC8XoxxO8hYL0wBe73vDInIT9d X-Google-Smtp-Source: APXvYqzSdIW5RPNkDAz1T28G8swL2pz/tm9ZPquF3vyU8mnAtjQ89v9i/K00hIOgwIlt3Hd3JlBgaw== X-Received: by 2002:a50:94d6:: with SMTP id t22mr7478626eda.32.1553277184390; Fri, 22 Mar 2019 10:53:04 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 22 Mar 2019 14:52:39 -0300 Message-Id: <20190322175241.5954-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322175241.5954-1-danielhb413@gmail.com> References: <20190322175241.5954-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: 2a00:1450:4864:20::542 Subject: [Qemu-devel] [PATCH v2 1/3] block: introducing 'bdrv_co_delete_file' interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Daniel Henrique Barboza , qemu-block@nongnu.org, mreitz@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 LUKS driver. The implementation is provided in a new 'bdrv_co_delete_file_generic' function inside block.c. This function is made public in case other block drivers wants to support this cleanup interface as well. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 45 +++++++++++++++++++++++++++++++++++++++ block/crypto.c | 2 ++ include/block/block.h | 3 +++ include/block/block_int.h | 6 ++++++ 4 files changed, 56 insertions(+) diff --git a/block.c b/block.c index 0a93ee9ac8..2b632baba2 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,51 @@ int bdrv_create_file(const char *filename, QemuOpts *o= pts, Error **errp) return ret; } =20 +/** + * Helper that checks if a given path represents a regular + * local file. + */ +bool bdrv_path_is_regular_file(const char *path) +{ + struct stat st; + + return (stat(path, &st) =3D=3D 0) && S_ISREG(st.st_mode); +} + +/** + * Co-routine function that erases a regular file. Its original + * intent is as a implementation of bdrv_co_delete_file for + * the "luks" driver that can leave created files behind in the + * file system when the authentication fails. + * + * The function is exposed here, and with 'generic' in its name, + * because file removal isn't usually format specific and any other + * BlockDriver might want to re-use this function. + */ +int coroutine_fn bdrv_co_delete_file_generic(const char *filename, + Error **errp) +{ + int ret; + + /* Skip file: protocol prefix */ + strstart(filename, "file:", &filename); + + if (!bdrv_path_is_regular_file(filename)) { + ret =3D -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", filename); + goto done; + } + + ret =3D unlink(filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", filena= me); + } + +done: + 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/block/crypto.c b/block/crypto.c index 3af46b805f..c604c96c93 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -639,6 +639,8 @@ static BlockDriver bdrv_crypto_luks =3D { .bdrv_co_truncate =3D block_crypto_co_truncate, .create_opts =3D &block_crypto_create_opts_luks, =20 + .bdrv_co_delete_file =3D bdrv_co_delete_file_generic, + .bdrv_reopen_prepare =3D block_crypto_reopen_prepare, .bdrv_refresh_limits =3D block_crypto_refresh_limits, .bdrv_co_preadv =3D block_crypto_co_preadv, diff --git a/include/block/block.h b/include/block/block.h index e452988b66..efb77daf9f 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -363,6 +363,9 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, Blo= ckDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); =20 +bool bdrv_path_is_regular_file(const char *path); +int coroutine_fn bdrv_co_delete_file_generic(const char *filename, + Error **errp); =20 typedef struct BdrvCheckResult { int corruptions; diff --git a/include/block/block_int.h b/include/block/block_int.h index 01e855a066..74abb78ce7 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -309,6 +309,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(const char *filename, + 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.20.1 From nobody Tue May 21 08:35:12 2024 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553277913246942.8861760993843; Fri, 22 Mar 2019 11:05:13 -0700 (PDT) Received: from localhost ([127.0.0.1]:32794 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OXW-0004Gn-3S for importer@patchew.org; Fri, 22 Mar 2019 14:05:10 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34145) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OVH-0002iB-El for qemu-devel@nongnu.org; Fri, 22 Mar 2019 14:02:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h7OLu-0000VV-2b for qemu-devel@nongnu.org; Fri, 22 Mar 2019 13:53:11 -0400 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]:41744) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h7OLt-0000Ue-HN; Fri, 22 Mar 2019 13:53:09 -0400 Received: by mail-ed1-x543.google.com with SMTP id a25so2386475edc.8; Fri, 22 Mar 2019 10:53:09 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:2d8:6f86:51db:ba10:5f2f]) by smtp.gmail.com with ESMTPSA id l26sm1790053eja.29.2019.03.22.10.53.04 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 10:53:07 -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=IGJqnu9+LGm7CbGBU9UjP+k+zCl4cVrn5PKnU248prk=; b=nNRFZnx95UHgj+HjcOw4rA0JxKhYLX0GUMREW49slcJnin/CnVSXoI/aizkvgFY4DU hHWxXUYecDn2SJyziTFoqHNNBVASOAODTOwXairiVpU1kHnCryg251yMYUif2NLZs6BM nVavnUNrTyXmekVC+BPrtuLEbdKobfm81d459M8aEqQyQzv+8soJVY4vwjWXrkb4aQh6 zJ9r5Dlp/1TKzoGJYMiORNlJGI8q75aS+7+ET+bOhJitrDPRZ+U1DhaEXyhTfIQYwak1 9MGSx9lLqcFJj+W1vQI3R5MbOVL9lhGXUHNB26nwD/8i+TxvAKkPS6L8xnac0ga1DEl9 Upjw== 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=IGJqnu9+LGm7CbGBU9UjP+k+zCl4cVrn5PKnU248prk=; b=W1km9DHBVZGW2cxdhpKFgJ1l07z5D/p1irN9AeMfceqjDIoBClTQE/ukyTjP+0qrcs KQ+Z/V8D7etaEHaC64Vv7zYohRt1F8uYKWOoolKRN6+V5fRlEOYJxy//ZZsgSEGJUAh9 Cgg9ua8bM809dPB6sPKNczdXvtgUPLguc07mD8zgacrOFCMZoC6fUvNawWuWYnx6esn4 2EsY0lgjzpa7dzIjt+ptWQorRewtH3nykQszO2wdZKX7/feHG3dtv9HmljNMP7NeXNGk sM870V6MAgwGu9hMkTaxL0wuyr/o6xC/ieoxVKbwowpbK7EhMsvBif7P/fMVsbQ7FKOy isMg== X-Gm-Message-State: APjAAAVWwhk4kVOPVDOIvJ0ljC5E1yiW3t5tds2Q3nTx+lTdEWX202L/ cXV6L/Wt0Ix+1a1q3HsOcm1Gdn5c X-Google-Smtp-Source: APXvYqwiKt92SyI4ejrOMGKRWH1bUNOgMMOIkpLedvXd2Jy7x3FEOZgjstUx1CfE27LFqBDFk6Gjrw== X-Received: by 2002:a50:f5ac:: with SMTP id u41mr7172121edm.47.1553277188333; Fri, 22 Mar 2019 10:53:08 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 22 Mar 2019 14:52:40 -0300 Message-Id: <20190322175241.5954-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322175241.5954-1-danielhb413@gmail.com> References: <20190322175241.5954-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: 2a00:1450:4864:20::543 Subject: [Qemu-devel] [PATCH v2 2/3] block.c: adding bdrv_delete_file X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Daniel Henrique Barboza , qemu-block@nongnu.org, mreitz@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 invoke a driver, given by a format @fmt, 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 --- @Daniel: I put the 'Suggested-by' tag here aware that what is being done in this patch wasn't explicitly suggested by you in that review. However, since it's a consequence of your suggestion, here it is. If you mind the tag here, let me know and we can remove it. block.c | 72 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 3 ++ 2 files changed, 75 insertions(+) diff --git a/block.c b/block.c index 2b632baba2..5c7781e471 100644 --- a/block.c +++ b/block.c @@ -592,6 +592,78 @@ done: return ret; } =20 +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err =3D NULL; + int ret; + + CreateCo *cco =3D opaque; + assert(cco->drv); + + ret =3D cco->drv->bdrv_co_delete_file(cco->filename, &local_err); + error_propagate(&cco->err, local_err); + cco->ret =3D ret; +} + +int bdrv_delete_file(const char *filename, const char *fmt, + Error **errp) +{ + + BlockDriver *drv =3D bdrv_find_format(fmt); + Coroutine *co; + CreateCo cco =3D { + .drv =3D drv, + .filename =3D g_strdup(filename), + .ret =3D NOT_DONE, + .err =3D NULL, + }; + int ret; + + if (!drv) { + error_setg(errp, "Unknown file format '%s'", fmt); + ret =3D -ENOENT; + 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 (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret =3D -ENOTSUP; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&cco); + } else { + co =3D qemu_coroutine_create(bdrv_delete_co_entry, &cco); + qemu_coroutine_enter(co); + while (cco.ret =3D=3D NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret =3D cco.ret; + if (ret < 0) { + if (cco.err) { + error_propagate(errp, cco.err); + } else { + error_setg_errno(errp, -ret, "Could not delete image"); + } + } + +out: + g_free(cco.filename); + 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 efb77daf9f..9b66cf00cb 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -366,6 +366,9 @@ void bdrv_unfreeze_backing_chain(BlockDriverState *bs, = BlockDriverState *base); bool bdrv_path_is_regular_file(const char *path); int coroutine_fn bdrv_co_delete_file_generic(const char *filename, Error **errp); +int bdrv_delete_file(const char *filename, const char *fmt, + Error **errp); + =20 typedef struct BdrvCheckResult { int corruptions; --=20 2.20.1 From nobody Tue May 21 08:35:12 2024 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553277909077683.9780631421353; Fri, 22 Mar 2019 11:05:09 -0700 (PDT) Received: from localhost ([127.0.0.1]:32790 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OXJ-0003t0-Er for importer@patchew.org; Fri, 22 Mar 2019 14:04:57 -0400 Received: from eggs.gnu.org ([209.51.188.92]:33916) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OVD-0002bl-R6 for qemu-devel@nongnu.org; Fri, 22 Mar 2019 14:02:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h7OM0-0000ZN-DK for qemu-devel@nongnu.org; Fri, 22 Mar 2019 13:53:18 -0400 Received: from mail-ed1-x543.google.com ([2a00:1450:4864:20::543]:39396) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h7OLw-0000Wz-Sa; Fri, 22 Mar 2019 13:53:14 -0400 Received: by mail-ed1-x543.google.com with SMTP id p20so1916246eds.6; Fri, 22 Mar 2019 10:53:12 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:2d8:6f86:51db:ba10:5f2f]) by smtp.gmail.com with ESMTPSA id l26sm1790053eja.29.2019.03.22.10.53.08 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 10:53:11 -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=9hdvoLW4ZTdzR9lWQHstAXQ0KRxp/0Xdkn2qHioSK8I=; b=rcqmeD+697AJVtyq8TjM9tEYBzmfimQbikGrywOW0aqBerJUw3Pc/3GEO2la2oRgEN Lh/Sx7TsaK4ftGnEHnQCYZnssz97M2eAh2/+BSVm4cVH3fezns10WzOAxtRtd5/2NdiX JdGfV+eTPmJSgKDuhv3ZkACkWDd5QwARglKK7F6WP8Fm8n1cHoKqOHaetVi0TLctdk82 v+2jX/gDNC6HNttGsTUjgqGEXqUPSZWWI1SMIe4eCnduj9whZof2oVJAQVOtHrPFmSEF IlMWWMtxIAFaBMwoIklteavV8FWONv125gpCZTdHhmG2/NQVyD0cX9EEHIsZOyk8qMM6 7+QQ== 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=9hdvoLW4ZTdzR9lWQHstAXQ0KRxp/0Xdkn2qHioSK8I=; b=Sh3QvR8j6r2mhQmek3A5wCVS57zeRbG+/FiwXhBbXrYipMK2UcoNyO4loU6cpTpioR uDhjke0oYa2yiwA9icvicNsarn1GDonZO8IX+yY0tRzurqWfU/uJ0VHZXywQvsxVy3Gs p/y6L1ia+Jvo4D/jPXW0VsYjYiBKm3AFiAjj/qR4qRvq2us4I8WyoTAx10rIKhIPFYSL WjPC1KoUmfLGync/gdW2/dIf7963BxM/ps8ddsjFmv/ywIjneqd+ypw/dDKING7CRV3G HnJ+/t35jqqgLPPHt1/qJ+rcCTSjqk1iBHs/P0HH49FzcbiWzggYdvA2HUQFH43U/EU2 O0VQ== X-Gm-Message-State: APjAAAXplrckAEq8mmK7Y+a9Xh2ZIMq0XSX8uoodQgdFFR9V19TTahQh rSWsBixCDZ6Scg9SAATXkELJWpKK X-Google-Smtp-Source: APXvYqwxFpbk9uFyKqp0n5cjC8AksjBnFbhVOu3nXuCZ9Bj4ON7WQp1IRRh9YpFPaqVmP2OHuZhz8A== X-Received: by 2002:a17:906:49d9:: with SMTP id w25mr6200066ejv.52.1553277191639; Fri, 22 Mar 2019 10:53:11 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 22 Mar 2019 14:52:41 -0300 Message-Id: <20190322175241.5954-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322175241.5954-1-danielhb413@gmail.com> References: <20190322175241.5954-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: 2a00:1450:4864:20::543 Subject: [Qemu-devel] [PATCH v2 3/3] qemu-img.c: clean up created file on img_create failure X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Srikanth Aithal , qemu-block@nongnu.org, Daniel Henrique Barboza , mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) 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 img_create to check if @filename is an existing file before bdrv_img_create is called. In case of failure, if @filename didn't exist before, check again for its existence and, if affirmative, erase it by calling bdrv_delete_file. * 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: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- qemu-img.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index 5fac840742..03b139b4ac 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -422,11 +422,12 @@ static int img_create(int argc, char **argv) uint64_t img_size =3D -1; const char *fmt =3D "raw"; const char *base_fmt =3D NULL; - const char *filename; + const char *filename, *path; const char *base_filename =3D NULL; char *options =3D NULL; Error *local_err =3D NULL; bool quiet =3D false; + bool file_already_existed =3D false; int flags =3D 0; =20 for(;;) { @@ -529,6 +530,15 @@ static int img_create(int argc, char **argv) error_exit("Unexpected argument: %s", argv[optind]); } =20 + /* + * Check if 'filename' represents a local file that already + * exists in the file system prior to bdrv_img_create. Strip + * the leading 'file:' from the filename if it exists. + */ + path =3D filename; + strstart(path, "file:", &path); + file_already_existed =3D bdrv_path_is_regular_file(path); + bdrv_img_create(filename, fmt, base_filename, base_fmt, options, img_size, flags, quiet, &local_err); if (local_err) { @@ -541,6 +551,23 @@ static int img_create(int argc, char **argv) =20 fail: g_free(options); + /* + * If an error occurred and we ended up creating a bogus + * 'filename' file, delete it + */ + if (!file_already_existed && bdrv_path_is_regular_file(path)) { + + int ret =3D bdrv_delete_file(path, fmt, &local_err); + /* + * ENOTSUP will happen if the block driver doesn't support + * 'bdrv_co_delete_file'. ENOENT will happen if the file + * doesn't exist. Both are predictable and shouldn't be + * reported back to the user. + */ + if ((ret < 0) && (ret !=3D -ENOTSUP) && (ret !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", filename); + } + } return 1; } =20 --=20 2.20.1