From patchwork Fri Mar 22 17:52:39 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 10866397 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 5E9C014DE for ; Fri, 22 Mar 2019 18:07:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 488D22A978 for ; Fri, 22 Mar 2019 18:07:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3C5282A97C; Fri, 22 Mar 2019 18:07:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id B3C0B2A978 for ; Fri, 22 Mar 2019 18:07:32 +0000 (UTC) Received: from localhost ([127.0.0.1]:32870 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OZo-0007FY-10 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Mar 2019 14:07:32 -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) 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) 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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é 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 *opts, Error **errp) return ret; } +/** + * 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) == 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 = -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", filename); + goto done; + } + + ret = unlink(filename); + if (ret < 0) { + ret = -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", filename); + } + +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 = { .bdrv_co_truncate = block_crypto_co_truncate, .create_opts = &block_crypto_create_opts_luks, + .bdrv_co_delete_file = bdrv_co_delete_file_generic, + .bdrv_reopen_prepare = block_crypto_reopen_prepare, .bdrv_refresh_limits = block_crypto_refresh_limits, .bdrv_co_preadv = 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, BlockDriverState *base, Error **errp); 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); 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); + /* + * 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 down to * the disk (for example file-posix.c calls fsync()). From patchwork Fri Mar 22 17:52:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 10866385 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7F95214DE for ; Fri, 22 Mar 2019 18:04:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 669862A5AE for ; Fri, 22 Mar 2019 18:04:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 59CF52A63F; Fri, 22 Mar 2019 18:04:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id E82CD2A5AE for ; Fri, 22 Mar 2019 18:04:33 +0000 (UTC) Received: from localhost ([127.0.0.1]:32794 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OWv-0004Gn-8j for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Mar 2019 14:04:33 -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) 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) 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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é 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; } +static void coroutine_fn bdrv_delete_co_entry(void *opaque) +{ + Error *local_err = NULL; + int ret; + + CreateCo *cco = opaque; + assert(cco->drv); + + ret = cco->drv->bdrv_co_delete_file(cco->filename, &local_err); + error_propagate(&cco->err, local_err); + cco->ret = ret; +} + +int bdrv_delete_file(const char *filename, const char *fmt, + Error **errp) +{ + + BlockDriver *drv = bdrv_find_format(fmt); + Coroutine *co; + CreateCo cco = { + .drv = drv, + .filename = g_strdup(filename), + .ret = NOT_DONE, + .err = NULL, + }; + int ret; + + if (!drv) { + error_setg(errp, "Unknown file format '%s'", fmt); + ret = -ENOENT; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret = -ENOTSUP; + goto out; + } + + if (!drv->bdrv_co_delete_file) { + error_setg(errp, "Driver '%s' does not support image delete", + drv->format_name); + ret = -ENOTSUP; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&cco); + } else { + co = qemu_coroutine_create(bdrv_delete_co_entry, &cco); + qemu_coroutine_enter(co); + while (cco.ret == NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret = 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); + typedef struct BdrvCheckResult { int corruptions; From patchwork Fri Mar 22 17:52:41 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 10866383 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EA8C714DE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D1D492A5AE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C56162A63F; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.7 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 49E572A5AE for ; Fri, 22 Mar 2019 18:04:21 +0000 (UTC) Received: from localhost ([127.0.0.1]:32790 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OWi-0003t0-69 for patchwork-qemu-devel@patchwork.kernel.org; Fri, 22 Mar 2019 14:04:20 -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) 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) 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) 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 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+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP 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=vol_1_encrypt0,file=vol_resize_pool.vol_1.secret.qzVQrI -o key-secret=vol_1_encrypt0 /var/tmp/pool_target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=luks size=10485760 key-secret=vol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is not 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é 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 = -1; const char *fmt = "raw"; const char *base_fmt = NULL; - const char *filename; + const char *filename, *path; const char *base_filename = NULL; char *options = NULL; Error *local_err = NULL; bool quiet = false; + bool file_already_existed = false; int flags = 0; for(;;) { @@ -529,6 +530,15 @@ static int img_create(int argc, char **argv) error_exit("Unexpected argument: %s", argv[optind]); } + /* + * 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 = filename; + strstart(path, "file:", &path); + file_already_existed = 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) 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 = 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 != -ENOTSUP) && (ret != -ENOENT)) { + error_reportf_err(local_err, "%s: ", filename); + } + } return 1; }