From nobody Sat May 4 08:19:30 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 1553635496420392.9378414539634; Tue, 26 Mar 2019 14:24:56 -0700 (PDT) Received: from localhost ([127.0.0.1]:38152 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tYz-0004Wb-EI for importer@patchew.org; Tue, 26 Mar 2019 17:24:53 -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) 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) 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) 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::844 Subject: [Qemu-devel] [PATCH 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the 'file' driver in file-posix.c. The implementation is given by 'raw_co_delete_file'. 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=C3=A9 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 } =20 +/** + * 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) =3D=3D 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 cha= r *filename, QemuOpts *opts, return raw_co_create(&options, errp); } =20 +/** + * Co-routine function that erases a regular file. + */ +static int coroutine_fn raw_co_delete_file(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; +} + /* * Find allocation range in @bs around offset @start. * May change underlying file descriptor's file offset. @@ -2867,6 +2894,7 @@ BlockDriver bdrv_file =3D { .bdrv_co_block_status =3D raw_co_block_status, .bdrv_co_invalidate_cache =3D raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes =3D raw_co_pwrite_zeroes, + .bdrv_co_delete_file =3D raw_co_delete_file, =20 .bdrv_co_preadv =3D raw_co_preadv, .bdrv_co_pwritev =3D raw_co_pwritev, diff --git a/include/block/block.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, 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); =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 Sat May 4 08:19:30 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553635381471736.8048533293887; Tue, 26 Mar 2019 14:23:01 -0700 (PDT) Received: from localhost ([127.0.0.1]:38113 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tX0-0002UB-FR for importer@patchew.org; Tue, 26 Mar 2019 17:22:50 -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) 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) 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) 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 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Using the new 'bdrv_co_delete_file' interface, bdrv_delete_file can be used in a way similar of the existing bdrv_create_file to to clean up a created file. The logic is also similar to what is already done in bdrv_create_file: a qemu_coroutine is created if needed, a specialized function bdrv_delete_co_entry is used to call the bdrv_co_delete_file co-routine of the driver, if the driver implements it. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 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 *o= pts, Error **errp) 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, Error **errp) +{ + + BlockDriver *drv =3D bdrv_find_protocol(filename, true, errp); + CreateCo cco =3D { + .drv =3D drv, + .filename =3D g_strdup(filename), + .ret =3D NOT_DONE, + .err =3D NULL, + }; + Coroutine *co; + int ret; + + if (!drv) { + error_setg(errp, "File '%s' has unknown format", filename); + 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 820643f96d..76901a63de 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -364,6 +364,8 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, Blo= ckDriverState *base, void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); =20 bool bdrv_path_is_regular_file(const char *path); +int bdrv_delete_file(const char *filename, Error **errp); + =20 typedef struct BdrvCheckResult { int corruptions; --=20 2.20.1 From nobody Sat May 4 08:19:30 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 (209.51.188.17 [209.51.188.17]) by mx.zohomail.com with SMTPS id 155363537085393.71810667849968; Tue, 26 Mar 2019 14:22:50 -0700 (PDT) Received: from localhost ([127.0.0.1]:38111 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h8tWq-0002Je-Sd for importer@patchew.org; Tue, 26 Mar 2019 17:22: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) 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) 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) 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 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::742 Subject: [Qemu-devel] [PATCH 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When using a non-UTF8 secret to create a volume using qemu-img, the following error happens: $ qemu-img create -f luks --object secret,id=3Dvol_1_encrypt0,file=3Dvol_re= size_pool.vol_1.secret.qzVQrI -o key-secret=3Dvol_1_encrypt0 /var/tmp/pool_= target/vol_1 10240K Formatting '/var/tmp/pool_target/vol_1', fmt=3Dluks size=3D10485760 key-sec= ret=3Dvol_1_encrypt0 qemu-img: /var/tmp/pool_target/vol_1: Data from secret vol_1_encrypt0 is no= t valid UTF-8 However, the created file /var/tmp/pool_target/vol_1 is left behind in the file system after the failure. This behavior can be observed when creating the volume using Libvirt, via 'virsh vol-create', and then getting "volume target path already exist" errors when trying to re-create the volume. The volume file is created inside block_crypto_co_create_opts_luks, in block/crypto.c. If the bdrv_create_file() call is successful but any succeeding step fails*, the existing 'fail' label does not take into account the created file, leaving it behind. This patch changes block_crypto_co_create_opts_luks to 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" =20 typedef struct BlockCrypto BlockCrypto; @@ -533,6 +534,8 @@ static int coroutine_fn block_crypto_co_create_opts_luk= s(const char *filename, BlockDriverState *bs =3D NULL; QDict *cryptoopts; int64_t size; + const char *path; + bool file_already_existed =3D false; int ret; =20 /* Parse options */ @@ -549,6 +552,15 @@ static int coroutine_fn block_crypto_co_create_opts_lu= ks(const char *filename, goto fail; } =20 + /* + * 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 =3D filename; + strstart(path, "file:", &path); + file_already_existed =3D bdrv_path_is_regular_file(path); + /* Create protocol layer */ ret =3D 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 =3D 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 !=3D -ENOTSUP) && (r_del !=3D -ENOENT)) { + error_reportf_err(local_err, "%s: ", path); + } + } + return ret; } =20 --=20 2.20.1