From patchwork Mon Sep 2 20:58:39 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: 1156724 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="T8oZEohI"; 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 46MjBR700hz9sP3 for ; Tue, 3 Sep 2019 06:59:27 +1000 (AEST) Received: from localhost ([::1]:39962 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tQ5-0005sd-Sj for incoming@patchwork.ozlabs.org; Mon, 02 Sep 2019 16:59:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:33402) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i4tPb-0005pe-Qp for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i4tPa-0004Fc-PP for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:55 -0400 Received: from mail-qt1-x844.google.com ([2607:f8b0:4864:20::844]:43907) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i4tPa-0004EZ-L1 for qemu-devel@nongnu.org; Mon, 02 Sep 2019 16:58:54 -0400 Received: by mail-qt1-x844.google.com with SMTP id l22so4715029qtp.10 for ; Mon, 02 Sep 2019 13:58:54 -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=VSXNt8Kn3icnDHyUvJOBRa++l6851CYhlNFBcR1U6UQ=; b=T8oZEohIrzAdd5PFQXwnFFz/IxZ4PpOwm7nxtbmXEb3mZsuYwU8aGee4iyHY4I7z/B wk2h9wQkUzbXjEvII9oeFmcbk6XGUXqxZ7cvAzD+2Kx1jpHBxiqK22giaGFeefdS6U7X RqyuaLhXPcl7pOpysNDtxlOTTjslA8j7FVJLLpvKuEZX8ZUg7mpi2KdVrz9chpaHvee6 rjn613l6EqwoKzO+MPB4XlZeYSMnWzX3j2Ux3I26Pjb56LnZS2jOsNuM91T4LIf37G1I z7blZKuTiRWcKHVtgWjM1nhxCbqLlFBtwP7uYGz6RPFKy6Kf3QfqFfcBwGZ9ncrNfAwV XuUg== 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=VSXNt8Kn3icnDHyUvJOBRa++l6851CYhlNFBcR1U6UQ=; b=qZAd0K1WY2iBfRo27XcVQmHxt+mTL1ByQuFzPzsrN1SCQqLie7I3Krx402FEE1ukFZ 4X9l3AmdST57c5JfQWvU05OMHdZ0I4dceWKvC1xo3znbU9RRvJqi40pUxAbDMCrcqXeX uQeEoEJRorvqvwVk6VE8TOydWBpKTCqHlwbgPzsTXQD+E6fh6lsJH0W6gPGkL4jtdCrB xXwvMcCOO7nm+DMvG/MPQnY2w5mPSG2eMQyaJJ1Dls0EC5F+4wlHUt3wM42ATfMAn13I CqmnEbHKnOWDvtMjTk1Aojang46aGHDTqKEINhdrjYAe3B6k85+VOkKIw/OAfbnGPJhs C00w== X-Gm-Message-State: APjAAAWH6w6cgRvWuREuqcSwWE9YTdc5ITkuq7DxiRFuPwnn4mmQCMJr dWAhfSAcGhljXElAXECLrdHpW/r9 X-Google-Smtp-Source: APXvYqxRIDM0dRA3cFdzCsAhfcEZcFNcE/CSRHie+LUQNxhNl/MbmwDvUCvJ/fiHpSvCnRiHPdHGNQ== X-Received: by 2002:ac8:6916:: with SMTP id e22mr29288521qtr.313.1567457933847; Mon, 02 Sep 2019 13:58:53 -0700 (PDT) Received: from rekt.ibmuc.com ([2804:431:c7c6:9589:1351:c7eb:87b:c9d5]) by smtp.gmail.com with ESMTPSA id w80sm4604038qka.62.2019.09.02.13.58.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Sep 2019 13:58:53 -0700 (PDT) From: Daniel Henrique Barboza To: qemu-devel@nongnu.org Date: Mon, 2 Sep 2019 17:58:39 -0300 Message-Id: <20190902205842.1884-2-danielhb413@gmail.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190902205842.1884-1-danielhb413@gmail.com> References: <20190902205842.1884-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 v6 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 ceec8c2f56..1442c75e3c 100644 --- a/include/block/block_int.h +++ b/include/block/block_int.h @@ -308,6 +308,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()).