From patchwork Tue Sep 3 13:57:05 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: 1157023 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="Iz53mL1n"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46N7ys2h2Sz9s7T for ; Wed, 4 Sep 2019 00:05:57 +1000 (AEST) Received: from localhost ([::1]:46704 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59RT-0004L9-4q for incoming@patchwork.ozlabs.org; Tue, 03 Sep 2019 10:05:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33756) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i59JF-0003LP-9p for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:27 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i59JC-0005iW-4B for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:23 -0400 Received: from mail-qt1-x843.google.com ([2607:f8b0:4864:20::843]:42396) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i59JB-0005hd-E4 for qemu-devel@nongnu.org; Tue, 03 Sep 2019 09:57:21 -0400 Received: by mail-qt1-x843.google.com with SMTP id t12so19982133qtp.9 for ; Tue, 03 Sep 2019 06:57:19 -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=ISRFJIcsQHvny5ehyZ7g2NR+kYMFconvzKsY4Lpa5sc=; b=Iz53mL1nw0oDH9yJIX2czma0m3exIDaGGevWWE60ww58fd2dSF/ksVCYqMF0fx+SZZ sHJJPLBA/l4VU55Y9bJlIxtXxf9AZOp0ucwl3b64UvlsR58cJFhVik2qm5iahigvBGbk DTPtsPLF+uJHpJu8mtly99ezJ8NwSbAoXueaJRy49Z9XoIsOEfej7JAQXPP0H+rsIgOv rciKmnRjOrFuX7n/pO01349rqkXOBjR6+5vSa4uqlwcQQEBFP05cHd9bV3mSzBvK03+u LRZrNvo6Z3javk0XiHRmnkULoMcMMp1dGlg3IYcRSRUsTAbUpe61ZP3lUeLsmtQlTtTe G1vQ== 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=ISRFJIcsQHvny5ehyZ7g2NR+kYMFconvzKsY4Lpa5sc=; b=S9CfzeLOVkspEvU1bXO++UPBB/4p+R8Ap6Xlg5M1eSqIV8RVJ9IzJDWdjKUYFOsEW/ ZnTjovCTXIDjod91qJT7x0zEx34xYdYV+n+glqsUcecMgTvKf4owkdYzm28lUrsi38DB gONpK0hyz43AggYZ4OHDqtuOClr7p52sttp3d732e7out9n+A4imsVBy3y4TiLRvJHRM fNFFEUj6kZmzBVd7KT/w+XP06RDauJR+CFut38rPJoTawc8sP671l7HFzkmGLd4GT0OX nvSy4X5DjoF9YBmfgp5rI8Q0pc0S8i90kuZCG3SKVso2X1xjIsHYPKTUQSSwxbBEenL4 5RmA== X-Gm-Message-State: APjAAAWpTHqteHiMF8sLlVbh85vlcEkp7eLkuKUsl82UsArdfxhhaCOq ukWbj8BtjUUVYIwzxVqmqnvH8UuKfLg= X-Google-Smtp-Source: APXvYqz5Wqcfsq/racGGRHgWqMXUKS6/s6I9j4X7CICowQrOafsruN1JTs7jec3UbUnODbG7nMt49Q== X-Received: by 2002:ac8:6745:: with SMTP id n5mr30498619qtp.68.1567519038775; Tue, 03 Sep 2019 06:57:18 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c7:5c9f:9471:e230:52b:8c93]) by smtp.gmail.com with ESMTPSA id k49sm9211158qtc.9.2019.09.03.06.57.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Sep 2019 06:57:18 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Tue, 3 Sep 2019 10:57:05 -0300 Message-Id: <20190903135708.21624-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190903135708.21624-1-danielhb413@gmail.com> References: <20190903135708.21624-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::843 Subject: [Qemu-devel] [PATCH v7 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, =?utf-8?q?Daniel_P_=2E_Berrang=C3=A9?= , Daniel Henrique Barboza , jsnow@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'. 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 fbeb0068db..52756de522 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2390,6 +2390,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. @@ -2942,6 +2969,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 0422acdf1c..a959ec2d1e 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -314,6 +314,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()).