From nobody Sun Feb 8 08:27:32 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1553278088938734.6446752690103; Fri, 22 Mar 2019 11:08:08 -0700 (PDT) Received: from localhost ([127.0.0.1]:32870 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OaF-0007FY-Qy for importer@patchew.org; Fri, 22 Mar 2019 14:07:59 -0400 Received: from eggs.gnu.org ([209.51.188.92]:34020) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h7OVH-0002dX-GD for qemu-devel@nongnu.org; Fri, 22 Mar 2019 14:02:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h7OLq-0000RT-6O for qemu-devel@nongnu.org; Fri, 22 Mar 2019 13:53:07 -0400 Received: from mail-ed1-x542.google.com ([2a00:1450:4864:20::542]:35628) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h7OLp-0000Ql-Pe; Fri, 22 Mar 2019 13:53:06 -0400 Received: by mail-ed1-x542.google.com with SMTP id d6so2394822eds.2; Fri, 22 Mar 2019 10:53:05 -0700 (PDT) Received: from rekt.ibmmodules.com ([2804:431:f700:2d8:6f86:51db:ba10:5f2f]) by smtp.gmail.com with ESMTPSA id l26sm1790053eja.29.2019.03.22.10.53.01 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 22 Mar 2019 10:53:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=uYEFYsfypM6WGUa6L3XGFA6h8BKJhU8B+mIv1ZMcTEs=; b=Gc5N9fbCO5fIoQXu2OLE+EM7dli8MzpIyjDc4hFON+ZfT8gvF1O9D0z8L97fx3whru WETnejJtOJJCnzSH8b6ZnE1eLQV/Pbs9kfO7qnc1+Rn81RoeSoCnaHUIjQVBB52dg1Vt +/YlrmVnXn5WxN38KRdKQkEdCgUA9q3yb4IakZbU5ZQlSomPfmdCoTefOqOy5tWocpfP wSzI6xAu//MwD4W4tndVh+QM4GvzoDirmdu8h0l1p4ZzUvDoYWb4LWzVKTxDhibOwb5w h+W4xweKw/PzNIadhy0Cn4pORTNlPzHBj49GDRwpSSg1ltHCNHHIp4L3OHIKr4FsvVrT 57BA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uYEFYsfypM6WGUa6L3XGFA6h8BKJhU8B+mIv1ZMcTEs=; b=gp3MKz1zm1W8s9oKqnRIzK9yyIlLSmq42qQXe9xEhyE0/FuLw2uZc6kZHc8namwelu SPwFsYtI8Ed0E1x/bY4CyiiV1SCAa5vjNkQO458z81Z8L8fSYBuPtMKUfq6qqB3ZUtLy ZR8KpzDVHLTvO/ukz65YvuEYf58mMyteFr1Z8TQ1DgQh28Uaa3YWcddnDATW98pBwOW/ Q5noeRzOazr1LCWYfpRt0yk/6zptvL2+gD6sLDh+rkwRHpb+RvYC3tdMl/AjuDrNbT6f s3IMDdMZ3wdIz0yike1aN4YTRLzS5VTluXcn5oKsciG94ms2+BKFcwu/A8pYRe1C1PQX cafw== X-Gm-Message-State: APjAAAVxhFaFmcZv7T51sTu2VqCksObKsPyPquHMp95fiXmxhhf7MRdt wMJC8XoxxO8hYL0wBe73vDInIT9d X-Google-Smtp-Source: APXvYqzSdIW5RPNkDAz1T28G8swL2pz/tm9ZPquF3vyU8mnAtjQ89v9i/K00hIOgwIlt3Hd3JlBgaw== X-Received: by 2002:a50:94d6:: with SMTP id t22mr7478626eda.32.1553277184390; Fri, 22 Mar 2019 10:53:04 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Fri, 22 Mar 2019 14:52:39 -0300 Message-Id: <20190322175241.5954-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190322175241.5954-1-danielhb413@gmail.com> References: <20190322175241.5954-1-danielhb413@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::542 Subject: [Qemu-devel] [PATCH v2 1/3] block: introducing 'bdrv_co_delete_file' interface X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, Daniel Henrique Barboza , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Adding to Block Drivers the capability of being able to clean up its created files can be useful in certain situations. For the LUKS driver, for instance, a failure in one of its authentication steps can leave files in the host that weren't there before. This patch adds the 'bdrv_co_delete_file' interface to block drivers and add it to the LUKS driver. The implementation is provided in a new 'bdrv_co_delete_file_generic' function inside block.c. This function is made public in case other block drivers wants to support this cleanup interface as well. Suggested-by: Daniel P. Berrang=C3=A9 Signed-off-by: Daniel Henrique Barboza --- block.c | 45 +++++++++++++++++++++++++++++++++++++++ block/crypto.c | 2 ++ include/block/block.h | 3 +++ include/block/block_int.h | 6 ++++++ 4 files changed, 56 insertions(+) diff --git a/block.c b/block.c index 0a93ee9ac8..2b632baba2 100644 --- a/block.c +++ b/block.c @@ -547,6 +547,51 @@ int bdrv_create_file(const char *filename, QemuOpts *o= pts, Error **errp) return ret; } =20 +/** + * Helper that checks if a given path represents a regular + * local file. + */ +bool bdrv_path_is_regular_file(const char *path) +{ + struct stat st; + + return (stat(path, &st) =3D=3D 0) && S_ISREG(st.st_mode); +} + +/** + * Co-routine function that erases a regular file. Its original + * intent is as a implementation of bdrv_co_delete_file for + * the "luks" driver that can leave created files behind in the + * file system when the authentication fails. + * + * The function is exposed here, and with 'generic' in its name, + * because file removal isn't usually format specific and any other + * BlockDriver might want to re-use this function. + */ +int coroutine_fn bdrv_co_delete_file_generic(const char *filename, + Error **errp) +{ + int ret; + + /* Skip file: protocol prefix */ + strstart(filename, "file:", &filename); + + if (!bdrv_path_is_regular_file(filename)) { + ret =3D -ENOENT; + error_setg_errno(errp, -ret, "%s is not a regular file", filename); + goto done; + } + + ret =3D unlink(filename); + if (ret < 0) { + ret =3D -errno; + error_setg_errno(errp, -ret, "Error when deleting file %s", filena= me); + } + +done: + return ret; +} + /** * Try to get @bs's logical and physical block size. * On success, store them in @bsz struct and return 0. diff --git a/block/crypto.c b/block/crypto.c index 3af46b805f..c604c96c93 100644 --- a/block/crypto.c +++ b/block/crypto.c @@ -639,6 +639,8 @@ static BlockDriver bdrv_crypto_luks =3D { .bdrv_co_truncate =3D block_crypto_co_truncate, .create_opts =3D &block_crypto_create_opts_luks, =20 + .bdrv_co_delete_file =3D bdrv_co_delete_file_generic, + .bdrv_reopen_prepare =3D block_crypto_reopen_prepare, .bdrv_refresh_limits =3D block_crypto_refresh_limits, .bdrv_co_preadv =3D block_crypto_co_preadv, diff --git a/include/block/block.h b/include/block/block.h index e452988b66..efb77daf9f 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -363,6 +363,9 @@ int bdrv_freeze_backing_chain(BlockDriverState *bs, Blo= ckDriverState *base, Error **errp); void bdrv_unfreeze_backing_chain(BlockDriverState *bs, BlockDriverState *b= ase); =20 +bool bdrv_path_is_regular_file(const char *path); +int coroutine_fn bdrv_co_delete_file_generic(const char *filename, + Error **errp); =20 typedef struct BdrvCheckResult { int corruptions; diff --git a/include/block/block_int.h b/include/block/block_int.h index 01e855a066..74abb78ce7 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -309,6 +309,12 @@ struct BlockDriver { */ int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); =20 + /* + * Delete a local created file. + */ + int coroutine_fn (*bdrv_co_delete_file)(const char *filename, + Error **errp); + /* * Flushes all data that was already written to the OS all the way dow= n to * the disk (for example file-posix.c calls fsync()). --=20 2.20.1