From patchwork Wed Aug 7 14:21:11 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: 11082269 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 CF36C912 for ; Wed, 7 Aug 2019 14:22:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C073728429 for ; Wed, 7 Aug 2019 14:22:05 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BE79D28A37; Wed, 7 Aug 2019 14:22:05 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 2674828A1F for ; Wed, 7 Aug 2019 14:22:05 +0000 (UTC) Received: from localhost ([::1]:41764 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMpI-0007NX-I9 for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Aug 2019 10:22:04 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39003) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMom-0005xO-2V for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMol-0008Vo-19 for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:32 -0400 Received: from mail-qk1-x743.google.com ([2607:f8b0:4864:20::743]:40251) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMok-0008VS-Tc for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:30 -0400 Received: by mail-qk1-x743.google.com with SMTP id s145so65919906qke.7 for ; Wed, 07 Aug 2019 07:21:30 -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=kkvGcqaG6Hw21CZ6CMR4F3QU5tZjnOVQB5zjN4lycZY=; b=ND0QVaF0LBULJ/dWcaXXlRMEYtojW4/DZ1Q3fBgV86/PuowwVee2vxF88gl8kVp9yl s7ya5tw64Txcys4KX+0H80xtnEyJAMP+KYJ665DEJ88x2LjtNF3LGMIIvZLAD/2maxAM lC1uJ0zJrh3gr80EvIlMtj0fpiiCJJYGqduV1Fq1POL8xLn3Pmd3gCVBWuM1NDTRtnV+ MQhI8i8FkybEpBj/g4lRmtRwjtMBWKAStPa2byQIIudspdv2cdNSoqipMnT+Sz3koQaf Ti7pTyOuZSbHQwuL66euQeZDuippdleAdTuHDaop7XiV3hg8pWzboNqf1Bkh7fPGtFXp VegQ== 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=kkvGcqaG6Hw21CZ6CMR4F3QU5tZjnOVQB5zjN4lycZY=; b=bf0zXhrbSes/LawDyVlGKhtU+wslPm9AnayuhUiowvXW7+p10QmVaJxcsNnPt8Jmvt UzcojSo4mHLNB2BscXC/oz4o80Mii2zgX/UOMqmZFIJzS4ZzySh+dwGQTeQS+bYfbOCb Q+gAqNU+V3DmAtxDZAxr57zMYsGvCnoWEbc5l4s4dtZkxXUwwHrW6IF1f9ol7x3zBmQY A8iJVyjW/yxoxPQUKPxoZ4O2KrKn6PZmJw0lug7BMabptrCnZ5cZvHL2YC+k2JDvlRw3 +Wc/h+/M4I/l/JGWGVpYVBqzpD6kNyR9p8vCVnFMnziwwIi+Skexn/sxvCu1T4M3DXkc 7F0w== X-Gm-Message-State: APjAAAUR1nK/rjKPdy9n48iUPPE70SAn7jjf4+j0wVilKmImNIcjzPJB r5mWghy6D9NkryWkYUmoQj0fx8MF X-Google-Smtp-Source: APXvYqxfcw8jp43y7sZ9T3yjPDk86xADVFye5tOvbVvlO1RgZqocvxm5yuzPLzG1Tl+L9yc1Wqv5SQ== X-Received: by 2002:a37:bc03:: with SMTP id m3mr8307018qkf.199.1565187690319; Wed, 07 Aug 2019 07:21:30 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.28 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:30 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:11 -0300 Message-Id: <20190807142114.17569-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-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: 2607:f8b0:4864:20::743 Subject: [Qemu-devel] [PATCH v5 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, 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 'file' driver in file-posix.c.The implementation is given by 'raw_co_delete_file'. Suggested-by: Daniel P. Berrangé 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 4479cc7ab4..278952d5a2 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2376,6 +2376,33 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, return raw_co_create(&options, errp); } +/** + * 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) == 0) || !S_ISREG(st.st_mode)) { + ret = -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", + bs->filename); + goto done; + } + + ret = unlink(bs->filename); + if (ret < 0) { + ret = -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. @@ -2927,6 +2954,7 @@ BlockDriver bdrv_file = { .bdrv_co_block_status = raw_co_block_status, .bdrv_co_invalidate_cache = raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes, + .bdrv_co_delete_file = raw_co_delete_file, .bdrv_co_preadv = raw_co_preadv, .bdrv_co_pwritev = raw_co_pwritev, diff --git a/include/block/block_int.h b/include/block/block_int.h index 3aa1e832a8..4cb3232dc4 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)(BlockDriverState *bs, + 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 Wed Aug 7 14:21:12 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: 11082275 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 846EC912 for ; Wed, 7 Aug 2019 14:23:06 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 74EA526256 for ; Wed, 7 Aug 2019 14:23:06 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6833A28995; Wed, 7 Aug 2019 14:23:06 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 04CB926256 for ; Wed, 7 Aug 2019 14:23:05 +0000 (UTC) Received: from localhost ([::1]:41790 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMqH-0001Hl-As for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Aug 2019 10:23:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39018) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMoo-00061r-O0 for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMon-00005D-6N for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:34 -0400 Received: from mail-qk1-x741.google.com ([2607:f8b0:4864:20::741]:33998) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMon-000054-2T for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:33 -0400 Received: by mail-qk1-x741.google.com with SMTP id t8so65900057qkt.1 for ; Wed, 07 Aug 2019 07:21:33 -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=9epP7UsNCqbjxzCDIUUM8j75jBuiFJRy3qqtGTzHLVk=; b=HX4HWE6/5/Ks2Ubcp/8NLOuMnwuvPC5rrmOv37ezmUJy4SF2Ezz3HVUhhkJJ3OLOsc fFvXjZgp4qh3sKRaiqbRyHAqMn2sd5rM4LamrRjWuIUvGyEfGUkjbpa+Q8FWqvDl8pe2 fG4L/SAQoVRn7/gZ2mbogNjMd9ZqzB0Nbaa4SrYZ7TkzlTKu4F8yK5md3e+kXBRE2nLo nxhOKVWjUQrqf+trTDvMyKNip2VeA1je1GbNOXDqMZuFHue3Gjs9d6cW+w+9v14EGDww HmkE2MaYe7OsNu+NUuxJbCIUs7zYcQOxa/AAiuRAswozirzvoB5FvOr5hYypAQKPTVMZ kl0w== 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=9epP7UsNCqbjxzCDIUUM8j75jBuiFJRy3qqtGTzHLVk=; b=VkzgLWMuk8/Q1SZRtczS2VLu6r4ajRYx5yWG63kTGSqtmyLmNk+c0n9Lb4mGLbxIzG ia8Srn3Y8jTiC0yd+YpDePAFQMyEwgsmHHYrDQ9GYWWVsovtDknTY3yUR/0PWgNq2sX1 z535MS+HFrCbWDko2sVuHtwSHnFvhcIRsxy5uUIq8x8MuQ/n/tPNcJQZd224+TtgNQ58 m+jXw4My/YVFZDC/qEguZZyKtJzUzukvkxEiF5LwTyiyIOmWO3RlhPQyGlM+CVRzl9aV k5Qs76AzZ5T5hFC0Mgkf6OPvJ/LPv/F/pMj9vrkGKd2WkAMQ6Gcihbt00YullTcAD/vy seTA== X-Gm-Message-State: APjAAAXpDrRndUWzkGKilJkWdaw1l9RDKyUOpTgUyVf7/XJJrK7iYGwM vvJ9QeLpTGMIX9VsKDD36Z6S+An5 X-Google-Smtp-Source: APXvYqznVeecbHpwuNVYJxe6IIPtNIigC44qNTn0/sb1tpUMlJrsuO7MV+ctZiPp+1ffC0KdCehwJw== X-Received: by 2002:a05:620a:10bc:: with SMTP id h28mr8350840qkk.289.1565187692452; Wed, 07 Aug 2019 07:21:32 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.30 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:32 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:12 -0300 Message-Id: <20190807142114.17569-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-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: 2607:f8b0:4864:20::741 Subject: [Qemu-devel] [PATCH v5 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, 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 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 --- block.c | 77 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 1 + 2 files changed, 78 insertions(+) diff --git a/block.c b/block.c index cbd8da5f3b..1e20250627 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,83 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) return ret; } +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 = NULL; + DeleteCo *dco = opaque; + + assert(dco->bs); + + dco->ret = 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 = bdrv_find_protocol(filename, true, NULL); + BlockDriverState *bs = bdrv_open(filename, NULL, NULL, + BDRV_O_RDWR | BDRV_O_PROTOCOL, NULL); + DeleteCo dco = { + .drv = drv, + .bs = bs, + .ret = NOT_DONE, + .err = NULL, + }; + Coroutine *co; + int ret; + + if (!drv) { + error_setg(errp, "File '%s' has unknown format", filename); + 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 (!bs) { + error_setg(errp, "Could not open image '%s' for erasing", + filename); + ret = 1; + goto out; + } + + if (qemu_in_coroutine()) { + /* Fast-path if already in coroutine context */ + bdrv_delete_co_entry(&dco); + } else { + co = qemu_coroutine_create(bdrv_delete_co_entry, &dco); + qemu_coroutine_enter(co); + while (dco.ret == NOT_DONE) { + aio_poll(qemu_get_aio_context(), true); + } + } + + ret = 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 50a07c1c33..5e83532364 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -369,6 +369,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 *base); +int bdrv_delete_file(const char *filename, Error **errp); typedef struct BdrvCheckResult { From patchwork Wed Aug 7 14:21:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11082273 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 0AB711399 for ; Wed, 7 Aug 2019 14:23:05 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EFB272881B for ; Wed, 7 Aug 2019 14:23:04 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E41F8285B6; Wed, 7 Aug 2019 14:23:04 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 563B128995 for ; Wed, 7 Aug 2019 14:23:04 +0000 (UTC) Received: from localhost ([::1]:41788 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMqF-0001E1-LE for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Aug 2019 10:23:03 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39042) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMoq-00067D-FY for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMop-000076-AA for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:36 -0400 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]:34685) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMop-00006x-5q for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:35 -0400 Received: by mail-qt1-x842.google.com with SMTP id k10so19333533qtq.1 for ; Wed, 07 Aug 2019 07:21:35 -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=dGfD+OKaAJtklYlmiZJfVWpoqCv42S59cVjzwBeRJNs=; b=fLaUpvx+YTXTwSrSe/iAG0Z7uSXo7Wcgu4E24TBm/UxIfq8POpcJqdfiuTRvX9qj0T D6klQjWfclW1ugpsxa90cjxRH0UPoXToibXlZ97+bCAQx7cmIGjNmyqFFPvS8lnPtbjy b4+DGYcMBePyMOjTjskvRJHvyVldEICdbuAFToUgeejyhsnHf7V94VFD+V1ry73cODxz TAyoze6y3dn2h2aaCTjoYox6oOlUSP42XTbF09Nk+k8cxhUn+6/m69zosYdYgQaGVUsL dQsR3wilDq4pkcqENBDtFBw50dmExmY/ylQ9auzzSIRuU0DUgC0unwCOw1LCRelYWxPb JEAg== 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=dGfD+OKaAJtklYlmiZJfVWpoqCv42S59cVjzwBeRJNs=; b=Or33CUo2rDvdoQRiylAv7dCICyKOTQ6BgTri14+9sLQaABzLu8NxIW7RPr5ajdDK+E m2NksVpkwQq3rT7hOTgrC60zmeB09c3PVlwzlFOxhqzkA6NOwO9CoUVkSaX9B21v4PIQ ObyGeh4N8DscsaNTP3IWsZ0NiHIQkaoe1WVjnHoYGoUUWerzdFl9gHoM0QzBd82i8uBm 0HGeG6rP+Y1tCMR+UnxvqvLRBRiS4fZzUMY1myJ0KNqfWrk5ghQiPXU/tHiYAwf4VWJ0 MOk3uYn7MRd7UqwQWQEckX5hjS+Lbgclc3rDm5RtbrbkPOBuWS/Qy/OZwoBzj2Ek/tzb oWPw== X-Gm-Message-State: APjAAAVOWPuK6otuE2EJ4+RJYwdoRKmgoXE9Q+Xh2XFGd8bH8opdbGME 7cIKLPkAp7YSijw0tYmBsFPpULFi X-Google-Smtp-Source: APXvYqwDxuCSHvRbxW42whOvF1eLZbuBwmtBfH2LmZ7I+ICRCKOilf7wV2xsxC1VIRmeqhibqa10IA== X-Received: by 2002:ac8:c0e:: with SMTP id k14mr8248597qti.72.1565187694567; Wed, 07 Aug 2019 07:21:34 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.32 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:34 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:13 -0300 Message-Id: <20190807142114.17569-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-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: 2607:f8b0:4864:20::842 Subject: [Qemu-devel] [PATCH v5 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, berrange@redhat.com, Daniel Henrique Barboza , mreitz@redhat.com, Srikanth Aithal , jsnow@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 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 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 8237424ae6..8ffca81df6 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" typedef struct BlockCrypto BlockCrypto; @@ -575,6 +576,25 @@ 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 = bdrv_delete_file(filename, &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 ((r_del < 0) && (r_del != -ENOTSUP) && (r_del != -ENOENT)) { + error_reportf_err(local_err, "%s: ", bs->filename); + } + } + return ret; } From patchwork Wed Aug 7 14:21:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Henrique Barboza X-Patchwork-Id: 11082271 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 B12491399 for ; Wed, 7 Aug 2019 14:22:12 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9EB292875F for ; Wed, 7 Aug 2019 14:22:12 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 92C14289FC; Wed, 7 Aug 2019 14:22:12 +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=-5.0 required=2.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1D82128A1B for ; Wed, 7 Aug 2019 14:22:12 +0000 (UTC) Received: from localhost ([::1]:41768 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMpP-0007jv-Em for patchwork-qemu-devel@patchwork.kernel.org; Wed, 07 Aug 2019 10:22:11 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:39062) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hvMot-0006Ev-3S for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hvMor-00007y-Mi for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:39 -0400 Received: from mail-qt1-x82e.google.com ([2607:f8b0:4864:20::82e]:34417) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hvMor-00007o-Ip for qemu-devel@nongnu.org; Wed, 07 Aug 2019 10:21:37 -0400 Received: by mail-qt1-x82e.google.com with SMTP id k10so19333669qtq.1 for ; Wed, 07 Aug 2019 07:21:37 -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=NwJ4uWjVtM+26b3Wt3huTtV/qJaSrXNc+otrwh9eGR4=; b=eJoPLpFPnGtP6/gNG6bsKghxb7SVMFTEX8Gyfc7NOYGV56e5i4g3euyrzq863WzpEt YfYbtNyphMvv9Vr08aIl4mCkNi2cSrUCdinVMX59HgrlgwljxVxrwZ0/Y9sFqpP/fUR2 n4sCV/1HmoLJ0JK+4sFNFw6Ye/MYlmMscFGb3kcpF9BP8OKOgnPjTvtuzArsCEl9fU0R u7jCbbAn0om+CmN5WU7HWENdv7XINgGx9nisRda964IaM3FMoSjC6aimzX4iSjoPIcwM nmUdX8kUtSDokegc7rsafuxX4lFVnOXszK0rHiMOMEmjcZDOzgBFcxP2JnucasAihRl8 w34w== 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=NwJ4uWjVtM+26b3Wt3huTtV/qJaSrXNc+otrwh9eGR4=; b=IrBUp9dB8dDd0kD6h4/6Lg4UZ2tFeCeFVYui6vdM0XCKj/FVgtuoZEi86S5k09veAo Ice2J6ycgsWEL6cY7WAzLgnNbOuc+hrvNEvmznXKvm5POf5wi5K36Iy+RSWBQaM8aJCV yaB9fs3fAIu5aVVVoVgylHxkMAxZA0IJEgbtuiGgct/D9tynIOubo9qpA+iIX5ldIEXL ScV8aVgnE78Rl33Ub9X9FLMiPHdR6qpm1LWOh6/J2D6FdKEdnPDzf9Zxg77P6J+bPzN+ 4fzbwophSZrEhfX8Eg24CbdBll2XxOQKG3GiKbJKruU1T1U54MA4QZ1L00IlsCt9Qpey nUdQ== X-Gm-Message-State: APjAAAUjF+tbu7PAr7cCueWXgFbXzl+QAsla1UMGTbHMLuHohU5y9Eoi uFboof0S5yW9TY+mxL2j4UMUCMgW X-Google-Smtp-Source: APXvYqw8vkfOvWKMox19ZE3JTlAn8g1D117j1IKGGwdItmwLHR+QNT9yb7R/S98STFLaqjUtWOYckg== X-Received: by 2002:ac8:2f43:: with SMTP id k3mr8416162qta.179.1565187696773; Wed, 07 Aug 2019 07:21:36 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5ef4:7266:b7a7:9def:f8fc]) by smtp.gmail.com with ESMTPSA id e18sm31472034qkm.49.2019.08.07.07.21.34 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 07 Aug 2019 07:21:36 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Wed, 7 Aug 2019 11:21:14 -0300 Message-Id: <20190807142114.17569-5-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190807142114.17569-1-danielhb413@gmail.com> References: <20190807142114.17569-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: 2607:f8b0:4864:20::82e Subject: [Qemu-devel] [PATCH v5 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, jsnow@redhat.com, Daniel Henrique Barboza , berrange@redhat.com, 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 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/257 | 67 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/257.out | 11 +++++++ tests/qemu-iotests/group | 1 + 3 files changed, 79 insertions(+) create mode 100755 tests/qemu-iotests/257 create mode 100644 tests/qemu-iotests/257.out diff --git a/tests/qemu-iotests/257 b/tests/qemu-iotests/257 new file mode 100755 index 0000000000..cb362598b4 --- /dev/null +++ b/tests/qemu-iotests/257 @@ -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=`basename $0` +echo "QA output created by $seq" + +status=1 # failure is the default! +TEST_IMAGE_FILE='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 "== Create non-UTF8 secret ==" +echo -n -e '\x3a\x3c\x3b\xff' > non_utf8_secret +SECRET="secret,id=sec0,file=non_utf8_secret" + +echo "== Throws an error because of invalid UTF-8 secret ==" +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Image file should not exist after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +echo "== Create a stub image file and run qemu-img again ==" +touch $TEST_IMAGE_FILE +$QEMU_IMG create -f $IMGFMT --object $SECRET -o "key-secret=sec0" $TEST_IMAGE_FILE 4M + +echo "== Pre-existing image file should also be deleted after the error ==" +if test -f "$TEST_IMAGE_FILE"; then + exit 1 +fi + +# success, all done +echo "*** done" +rm -f $seq.full +status=0 diff --git a/tests/qemu-iotests/257.out b/tests/qemu-iotests/257.out new file mode 100644 index 0000000000..3803933571 --- /dev/null +++ b/tests/qemu-iotests/257.out @@ -0,0 +1,11 @@ +QA output created by 257 +== Create non-UTF8 secret == +== Throws an error because of invalid UTF-8 secret == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Image file should not exist after the error == +== Create a stub image file and run qemu-img again == +qemu-img: vol.img: Data from secret sec0 is not valid UTF-8 +Formatting 'vol.img', fmt=luks size=4194304 key-secret=sec0 +== Pre-existing image file should also be deleted after the error == + *** done diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group index f13e5f2e23..ae7bb89642 100644 --- a/tests/qemu-iotests/group +++ b/tests/qemu-iotests/group @@ -271,3 +271,4 @@ 254 rw backing quick 255 rw quick 256 rw quick +257 rw auto quick