From patchwork Tue Mar 26 21:17: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: 1066117 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="VxpC1Smu"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44TPJz27m8z9sP4 for ; Wed, 27 Mar 2019 08:24:19 +1100 (AEDT) Received: from localhost ([127.0.0.1]:38126 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tYP-0003Le-8A for incoming@patchwork.ozlabs.org; Tue, 26 Mar 2019 17:24:17 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58519) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tV7-0001OX-CS for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:20:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8tSG-00016E-Jj for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:17:57 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:33831) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8tSG-000160-Ef; Tue, 26 Mar 2019 17:17:56 -0400 Received: by mail-qt1-x844.google.com with SMTP id k2so16479931qtm.1; Tue, 26 Mar 2019 14:17: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=aAMblIhkyZ3K7eFe/YT7i45tLeR3QjU4K9ZXpZYwYlo=; b=VxpC1SmuARzSnXkiL/CbZfQGuIIQVipsrz1HZKCl+o4aUxFvl+kymnEtevE4JFOGVO bxBW17aVo10pOtReFf0IX/5C82W6glzgq4yVtGR80J44TzONvQusjGOoxCNJEyM34hBA /KAizwiWcF86ym0YL7Edrrm11EIkrEyEUL2j5oaxNk2CzNmBbsrIZVmJMZ/gj8w+jwgo djpFD6acMvefgM7gzPdHm0lzXe4X3g16+OLmy9aEL/sIn9B4EYmw9v9cgOrGQXAPxvC5 9WxyB9dWlZhNu4C/IjVcV/iJRBaBSqAYujgYw8LFyH6UK0PooY3z4cXYjdF71i83zikg UI2Q== 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=aAMblIhkyZ3K7eFe/YT7i45tLeR3QjU4K9ZXpZYwYlo=; b=aHQetLMspOUT5V5hpa9UpoWijOCV7obhi3X1J5F78SnXcxwsg5z4CndcBsIfJPDAd4 7n4KerxmK9VU9OOWihmlso2ty3xiESnnHFQGljBMtoQAeGmGYyB9FtTwpVFXk7mKiDZq AMVchUzZIfs3wgnlMdnNzE43D88szbGKk8ouiWzsFwkQsnG/2LuORENggfMpUrBKbTio NOfrthlfzvrrcpg1VK1ZOjwLixPeWPIlPvK2ODxsKexNHOLVg7uMYQZXIBwWJtdKD1px DuZiZcnPGi0/MO3V9cqIu2GYYPrNyUepbK6io0ZNLYMfgQTV/j5ic2T95244zZczp+J+ aLtg== X-Gm-Message-State: APjAAAXrJmevisLtIBaiFa9FTP1u02nrzhRe892GyctRIoLTh5LWwqvQ 58AaoA+PIzFkCB7k4HttjvIMaQAwyog= X-Google-Smtp-Source: APXvYqwNoKyNGJUhli9zt+MM2ZTXVznm1gpa11k1+21hGVkPmzwLaQSEVhM3erqQhiYp567+Xhm3nw== X-Received: by 2002:a0c:9524:: with SMTP id l33mr26890289qvl.41.1553635075826; Tue, 26 Mar 2019 14:17:55 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:6d41:b488:ffd7:8d35:4aac]) by smtp.gmail.com with ESMTPSA id d21sm11735060qtc.91.2019.03.26.14.17.53 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 26 Mar 2019 14:17:55 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 26 Mar 2019 18:17:40 -0300 Message-Id: <20190326211742.26140-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190326211742.26140-1-danielhb413@gmail.com> References: <20190326211742.26140-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::844 Subject: [Qemu-devel] [PATCH v3 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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'. The helper 'bdrv_path_is_regular_file' is being used only in raw_co_delete_file at this moment, but it will be used inside LUKS in a later patch. Foreseeing this future use, let's put it in block.c and make it public. Suggested-by: Daniel P. Berrangé Signed-off-by: Daniel Henrique Barboza --- block.c | 11 +++++++++++ block/file-posix.c | 28 ++++++++++++++++++++++++++++ include/block/block.h | 1 + include/block/block_int.h | 6 ++++++ 4 files changed, 46 insertions(+) diff --git a/block.c b/block.c index 0a93ee9ac8..227362b282 100644 --- a/block.c +++ b/block.c @@ -621,6 +621,17 @@ int get_tmp_filename(char *filename, int size) #endif } +/** + * Helper that checks if a given string 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); +} + /* * Detect host devices. By convention, /dev/cdrom[N] is always * recognized as a host CDROM. diff --git a/block/file-posix.c b/block/file-posix.c index d102f3b222..09d84bab37 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2342,6 +2342,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(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; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2867,6 +2894,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.h b/include/block/block.h index e452988b66..820643f96d 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -363,6 +363,7 @@ 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); 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 Tue Mar 26 21:17: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: 1066113 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="rg8qIK9t"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44TPGB42jJz9sTv for ; Wed, 27 Mar 2019 08:21:54 +1100 (AEDT) Received: from localhost ([127.0.0.1]:38105 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tW4-0001a2-GX for incoming@patchwork.ozlabs.org; Tue, 26 Mar 2019 17:21:52 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58529) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tV6-0001Ob-Sn for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:20:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8tSJ-000177-1w for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:18:00 -0400 Received: from mail-qt1-x842.google.com ([2607:f8b0:4864:20::842]:44786) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8tSI-00016n-PG; Tue, 26 Mar 2019 17:17:58 -0400 Received: by mail-qt1-x842.google.com with SMTP id w5so16391434qtb.11; Tue, 26 Mar 2019 14:17: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=c5P7W8u4SI9IzsNzKkEt5fwljjlwnMVE0EbXSH1SCzo=; b=rg8qIK9tR4ZiQ6Rt4s5UrRLn63WprqSXP4S+uyKEzCZzwq0iloezikJCFflQhcoAeC BzGyOYlpWGYx5ne+WDIDgYHwJPDSaFxcp0ryWEWweYhPk6ztuhXSfEc7LzgGYCMk9COQ G/bMfI6cYM+nMwVY8CQdvGMj77R1vinfrsCbVP9FcFxNOYUao+wANTutVwdImmjCDoFF bSgSzlpqeMMg5ztD/EMdvOPMYfboc0mkvdD6UBQFWQgSoQpBUQlWkxW8GsmSvAQoAFLc ow/ajpXFmKm73rxF/bD+B4dqZRZLwBhAP8ZxFR8adFUDvYqx9pGz7lXgg4w62teN9lUz B1KA== 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=c5P7W8u4SI9IzsNzKkEt5fwljjlwnMVE0EbXSH1SCzo=; b=FxvdyDq/eieIAtszU5kncCvDyx9Fo2NA5SGTg6x0ghRfPbW/z5VB3sQ+Rvkowye678 3xFhrMw3RBqwQrxmb4JiJlz77wknUglFHgwT+ZGWcWukDV5bDhHmPMHVkvUcl1MlQC6a BneOQMcDfSjPI9v1GVvBEMRaWPW8z+RZcxuMtIPGTcMUQ0iaAHrNIc7Tst+N88++cJyO +EBWTXE0kTPUDa90nCFQg5gfqCHo9hDoX0zgrzn2eLMAgMbNS95xvUGJDQYoV06Zsnus Xel+z+rPAzn4IYUZrDfv3cPfy7IwqO0kcJPfuJilf4xgKsHua7WtXmv1wNE5YnVV69jp RLrQ== X-Gm-Message-State: APjAAAWe3jt0f4t5eze61sco/Kfo9MlUIXaGt4zoaZAouNa67spNcg2I pSvs3pPYkhpeElUSKANFASGvkET2TRY= X-Google-Smtp-Source: APXvYqz0VL+g9RUeJ1wJIvcFDtuN4k5l1FfAbGzqnWmBMmmpBzdFfxblvQiI3KbmVKx/HsgEP/sIbA== X-Received: by 2002:a0c:9651:: with SMTP id 17mr12874485qvy.59.1553635078146; Tue, 26 Mar 2019 14:17:58 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:6d41:b488:ffd7:8d35:4aac]) by smtp.gmail.com with ESMTPSA id d21sm11735060qtc.91.2019.03.26.14.17.56 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 26 Mar 2019 14:17:57 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 26 Mar 2019 18:17:41 -0300 Message-Id: <20190326211742.26140-3-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190326211742.26140-1-danielhb413@gmail.com> References: <20190326211742.26140-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 v3 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 | 71 +++++++++++++++++++++++++++++++++++++++++++ include/block/block.h | 2 ++ 2 files changed, 73 insertions(+) diff --git a/block.c b/block.c index 227362b282..4eb9d27863 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,77 @@ int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) 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, Error **errp) +{ + + BlockDriver *drv = bdrv_find_protocol(filename, true, errp); + CreateCo cco = { + .drv = drv, + .filename = g_strdup(filename), + .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 (!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 820643f96d..76901a63de 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -364,6 +364,8 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, BlockDriverState *base, void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *base); bool bdrv_path_is_regular_file(const char *path); +int bdrv_delete_file(const char *filename, Error **errp); + typedef struct BdrvCheckResult { int corruptions; From patchwork Tue Mar 26 21:17:42 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: 1066112 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="tFP8v0OF"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 44TPG45kNYz9sV0 for ; Wed, 27 Mar 2019 08:21:44 +1100 (AEDT) Received: from localhost ([127.0.0.1]:38098 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tVs-0001PJ-QG for incoming@patchwork.ozlabs.org; Tue, 26 Mar 2019 17:21:40 -0400 Received: from eggs.gnu.org ([209.51.188.92]:58509) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tV5-0001OW-Ne for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:20:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h8tSL-000180-Il for qemu-devel@nongnu.org; Tue, 26 Mar 2019 17:18:02 -0400 Received: from mail-qk1-x742.google.com ([2607:f8b0:4864:20::742]:39032) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h8tSL-00017q-Ck; Tue, 26 Mar 2019 17:18:01 -0400 Received: by mail-qk1-x742.google.com with SMTP id c189so8602885qke.6; Tue, 26 Mar 2019 14:18:01 -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=9MtXhX5dJ+tUvF2J8ktiidfJ2IOKL8NxvGnYVHhQzDg=; b=tFP8v0OFtoSQmXkcBxK2eScRgthaVhV2J5HgU8XQK584JqjO61AZIEoIVrEscvaeUB VTBPsl7zuPEB0R5p4NT58+j6etMFFXybOjVoZ2sXyuyQkHbEtjxjogSEtgYTeLJBjMMT 5/CabdUQLi7jR6Jyk11NgoW3Pn+jekoVFYG3S28ETZqg+aI9JXZq9/ilgFK43B2u4AJ8 Qi9TtiHIDODnApOOV/WNwUGDZLIZ1HunYAu2UW3jfHA4Rw1j+wyo7R69BcojHOruMHZp uwdhdWQrRnjXu7OxgV+vWHdakL8u970yok+xoPmubGWE9cq9FWHCJ1txktedHlA0bAH6 pPow== 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=9MtXhX5dJ+tUvF2J8ktiidfJ2IOKL8NxvGnYVHhQzDg=; b=Ii4rleJRbXdLpBrR61C1FXON14Njh3rhbsT258JyP25jXBhUA5+K4uBVhiC3rR0Idh mOE5PpEq6SGA2gqpAIIoJKI4CyvS/nF38SrmE0PpZqrJlAUkvI4c/3UsdFIR8h+9UOt9 SUH8KMdw7p4VIMGgDBE8jxoh78uOvBhy0YO4emu4AFvI/1sJRi0TA/hIrnAdfeoaod1n ymDssHK8vAMsgosCHqJ2Tj2Bz0R3dsitpNIPnUCOMydgDFCKI2HnMi8LH6AyQkGwSXit d4R0tAShIPHVyndRqabmT6fBPwg5q8kv+w+jJoyurhUMFKdrS8BGLSeNmHd3XhUFK8Sr Dwuw== X-Gm-Message-State: APjAAAXKCK6Y/6wk8h/YGFZidfB34D0vccnbYl+nuNk2BeKdW4wBvVOA RDiCOvelxuE7Q+/CTA7HVmYAFi1DlCY= X-Google-Smtp-Source: APXvYqw6HrO3K115wck7uwp3AuTaasMm2/nx2AwfmJGxyYMS4zLbIDlgQ/M/sICwrema1YGcsvbuYQ== X-Received: by 2002:a37:5d06:: with SMTP id r6mr25784797qkb.148.1553635080734; Tue, 26 Mar 2019 14:18:00 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:6d41:b488:ffd7:8d35:4aac]) by smtp.gmail.com with ESMTPSA id d21sm11735060qtc.91.2019.03.26.14.17.58 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 26 Mar 2019 14:18:00 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 26 Mar 2019 18:17:42 -0300 Message-Id: <20190326211742.26140-4-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190326211742.26140-1-danielhb413@gmail.com> References: <20190326211742.26140-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::742 Subject: [Qemu-devel] [PATCH v3 3/3] crypto.c: cleanup created file when block_crypto_co_create_opts_luks fails 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+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 check if @filename is an existing file before bdrv_create_file 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: Kevin Wolf Signed-off-by: Daniel Henrique Barboza --- block/crypto.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/block/crypto.c b/block/crypto.c index 3af46b805f..7c1b80616c 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -29,6 +29,7 @@ #include "qapi/qobject-input-visitor.h" #include "qapi/error.h" #include "qemu/option.h" +#include "qemu/cutils.h" #include "crypto.h" typedef struct BlockCrypto BlockCrypto; @@ -533,6 +534,8 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, BlockDriverState *bs = NULL; QDict *cryptoopts; int64_t size; + const char *path; + bool file_already_existed = false; int ret; /* Parse options */ @@ -549,6 +552,15 @@ static int coroutine_fn block_crypto_co_create_opts_luks(const char *filename, goto fail; } + /* + * Check if 'filename' represents a local file that already + * exists in the file system prior to bdrv_create_file. 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); + /* Create protocol layer */ ret = bdrv_create_file(filename, opts, errp); if (ret < 0) { @@ -573,6 +585,25 @@ fail: bdrv_unref(bs); qapi_free_QCryptoBlockCreateOptions(create_opts); qobject_unref(cryptoopts); + + /* + * If an error occurred and we ended up creating a bogus + * 'filename' file, delete it + */ + if (ret && !file_already_existed && bdrv_path_is_regular_file(path)) { + Error *local_err; + int r_del = bdrv_delete_file(path, &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: ", path); + } + } + return ret; }