Skip to contentSkip to conversation

delete created files when block_crypto_co_create_opts_luks fails

PATCH Daniel Henrique Barboza 2 participants 3 messages Apr 16, 2019 – May 16, 2019 2 messages not in the archive

lore
Share & tools
lore
#1 lore raw
In reply to a message not in the archive

Ping

Quoted 22 lines · Daniel Henrique Barboza

In this new interation, the following changes were made from the previous version [1]:
- moved the 'generic' code from block.c to file-posix.c
- moved the file delete call from qemu_img to the error path of block_crypto_co_create_opts_luks

[1] https://lists.gnu.org/archive/html/qemu-devel/2019-03/msg06444.html

Daniel Henrique Barboza (3):
block: introducing 'bdrv_co_delete_file' interface block.c: adding bdrv_delete_file
crypto.c: cleanup created file when block_crypto_co_create_opts_luks
fails

  block.c                   | 82 +++++++++++++++++++++++++++++++++++++++
  block/crypto.c            | 31 +++++++++++++++
  block/file-posix.c        | 28 +++++++++++++
  include/block/block.h     |  3 ++
  include/block/block_int.h |  6 +++
  5 files changed, 150 insertions(+)
#3 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 1/3] block: introducing 'bdrv_co_delete_file' interface

In reply to a message not in the archive

It looks like this one has gone un-noticed for a little while.

Quoted 49 lines · Daniel Henrique Barboza

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.

---
 block.c                   | 11 +++++++++++
 block/file-posix.c        | 28 ++++++++++++++++++++++++++++
 include/block/block.h     |  1 +
 include/block/block_int.h |  6 ++++++
 4 files changed, 46 insertions(+)
block.c +11 -0
index 0a93ee9ac8..227362b282 100644
@@ -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.
block/file-posix.c +0 -0
index d102f3b222..09d84bab37 100644

block/file-posix.c static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, · line 2349

Quoted 8 lines
block/file-posix.c +5 -0
@@ -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)

Do we need to mark functions that make no use of coroutines as coroutine_fn? I guess this way the interface is *allowed* to be a coroutine if other drivers need to make use of that.

block/file-posix.c static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, · line 2355

Quoted 6 lines
block/file-posix.c +6 -0
+{
+    int ret;
+
+    /* Skip file: protocol prefix */
+    strstart(filename, "file:", &filename);
+

This sticks out as fragile to me, but I guess that's exactly how create works, so... OK.

Quoted 47 lines
block/file-posix.c +17 -0
+    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,
include/block/block.h +1 -0
index e452988b66..820643f96d 100644
@@ -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;
include/block/block_int.h +0 -0
index 01e855a066..74abb78ce7 100644
@@ -309,6 +309,12 @@ struct BlockDriver {
      */
     int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs);

include/block/block_int.h struct BlockDriver { · line 320

Quoted 10 lines
include/block/block_int.h +6 -0
 
+    /*
+     * 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()).

Seems alright at a glance, if we want this interface.

Kevin, do we?

--js

#4 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH v3 1/3] block: introducing 'bdrv_co_delete_file' interface

Hi John,

Quoted 55 lines · John Snow

It looks like this one has gone un-noticed for a little while.

Quoted 52 lines · Daniel Henrique Barboza

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.

---
  block.c                   | 11 +++++++++++
  block/file-posix.c        | 28 ++++++++++++++++++++++++++++
  include/block/block.h     |  1 +
  include/block/block_int.h |  6 ++++++
  4 files changed, 46 insertions(+)
block.c +11 -0
index 0a93ee9ac8..227362b282 100644
@@ -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.
block/file-posix.c +0 -0
index d102f3b222..09d84bab37 100644
@@ -2342,6 +2342,33 @@ static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts,
      return raw_co_create(&options, errp);
  }
 
Quoted 8 lines

block/file-posix.c static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, · line 2349

Quoted 5 lines
block/file-posix.c +5 -0
+/**
+ * Co-routine function that erases a regular file.
+ */
+static int coroutine_fn raw_co_delete_file(const char *filename,
+                                           Error **errp)

Do we need to mark functions that make no use of coroutines as coroutine_fn? I guess this way the interface is *allowed* to be a coroutine if other drivers need to make use of that.

This function is used in a coroutine in patch 2. But to be honest, what I did here was to emulate the existing behavior of bdrv_create. Which is kind of lame if bdrv_create happens to have design problems or inconsistencies, but at least it's based on something that's already working.

Quoted 9 lines

block/file-posix.c static int coroutine_fn raw_co_create_opts(const char *filename, QemuOpts *opts, · line 2355

Quoted 7 lines
block/file-posix.c +6 -0
+{
+    int ret;
+
+    /* Skip file: protocol prefix */
+    strstart(filename, "file:", &filename);
+

This sticks out as fragile to me, but I guess that's exactly how create works, so... OK.

Yep, create does the same thing.

Thanks,

DHB

Quoted 63 lines
Quoted 48 lines
block/file-posix.c +17 -0
+    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,
include/block/block.h +1 -0
index e452988b66..820643f96d 100644
@@ -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;
include/block/block_int.h +0 -0
index 01e855a066..74abb78ce7 100644
@@ -309,6 +309,12 @@ struct BlockDriver {
       */
      int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs);

include/block/block_int.h struct BlockDriver { · line 320

Quoted 10 lines
include/block/block_int.h +6 -0
 
+    /*
+     * 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()).

Seems alright at a glance, if we want this interface.

Kevin, do we?

--js