Skip to contentSkip to conversation

block: Preallocated truncation for gluster and sheepdog

PATCH Applied Max Reitz 3 participants 16 messages Feb 13, 2018 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

cover block: Preallocated truncation for gluster and sheepdog

As far as I can see, these are the only protocols beside file-posix that support preallocated creation. In contrast to file-posix, however, they have not supported preallocated truncation so far. This series brings their truncation code to feature parity with their creation code in this regard.

Note that I do not have a test setup for either of the two drivers, so I do not actually know whether this works. Anyone with a setup is more than welcome to test this series.

Max Reitz (7):
gluster: Move glfs_close() to create's clean-up
gluster: Pull truncation from qemu_gluster_create
gluster: Query current size in do_truncate()
gluster: Add preallocated truncation
sheepdog: Make sd_prealloc() take a BDS
sheepdog: Pass old and new size to sd_prealloc()
sheepdog: Allow fully preallocated truncation

 block/gluster.c  | 116 ++++++++++++++++++++++++++++++-------------------------
 block/sheepdog.c |  56 +++++++++++++++++----------
 2 files changed, 99 insertions(+), 73 deletions(-)
Signature · 1 line
2.14.3
author
#1 lore raw

PATCH 1/7 gluster: Move glfs_close() to create's clean-up

glfs_close() is a classical clean-up operation, as can be seen by the fact that it is executed even if the truncation before it failed. Also, moving it to clean-up makes it more clear that if it fails, we do not want it to overwrite the current ret value if that signifies an error already.

---
 block/gluster.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
block/gluster.c +6 -4
index d8decc41ad..7fab2dfa12 100644
@@ -970,7 +970,7 @@ static int qemu_gluster_create(const char *filename,
 {
     BlockdevOptionsGluster *gconf;
     struct glfs *glfs;
-    struct glfs_fd *fd;
+    struct glfs_fd *fd = NULL;
     int ret = 0;
     PreallocMode prealloc;
     int64_t total_size = 0;
@@ -1054,10 +1054,12 @@ static int qemu_gluster_create(const char *filename,
         break;
     }
 
-    if (glfs_close(fd) != 0) {
-        ret = -errno;
-    }
 out:
+    if (fd) {
+        if (glfs_close(fd) != 0 && ret == 0) {
+            ret = -errno;
+        }
+    }
     qapi_free_BlockdevOptionsGluster(gconf);
     glfs_clear_preopened(glfs);
     return ret;
Signature · 1 line
2.14.3
Reviewed-by on v1 1/7
#2 lore raw

Re: [Qemu-devel] [PATCH 1/7] gluster: Move glfs_close() to create's clean-up

Quoted 11 lines · Max Reitz

glfs_close() is a classical clean-up operation, as can be seen by the fact that it is executed even if the truncation before it failed. Also, moving it to clean-up makes it more clear that if it fails, we do not want it to overwrite the current ret value if that signifies an error already.

---
  block/gluster.c | 10 ++++++----
  1 file changed, 6 insertions(+), 4 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#3 lore raw

PATCH 2/7 gluster: Pull truncation from qemu_gluster_create

In reply to Max Reitz

Pull out the truncation code from the qemu_cluster_create() function so we can later reuse it in qemu_gluster_truncate().

---
 block/gluster.c | 74 +++++++++++++++++++++++++++++++--------------------------
 1 file changed, 40 insertions(+), 34 deletions(-)
block/gluster.c +40 -34
index 7fab2dfa12..8178541416 100644
@@ -965,6 +965,45 @@ static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
 }
 #endif
 
+static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
+                                    PreallocMode prealloc, Error **errp)
+{
+    switch (prealloc) {
+#ifdef CONFIG_GLUSTERFS_FALLOCATE
+    case PREALLOC_MODE_FALLOC:
+        if (glfs_fallocate(fd, 0, 0, offset)) {
+            error_setg_errno(errp, errno, "Could not preallocate data");
+            return -errno;
+        }
+        break;
+#endif /* CONFIG_GLUSTERFS_FALLOCATE */
+#ifdef CONFIG_GLUSTERFS_ZEROFILL
+    case PREALLOC_MODE_FULL:
+        if (glfs_ftruncate(fd, offset)) {
+            error_setg_errno(errp, errno, "Could not resize file");
+            return -errno;
+        }
+        if (glfs_zerofill(fd, 0, offset)) {
+            error_setg_errno(errp, errno, "Could not zerofill the new area");
+            return -errno;
+        }
+        break;
+#endif /* CONFIG_GLUSTERFS_ZEROFILL */
+    case PREALLOC_MODE_OFF:
+        if (glfs_ftruncate(fd, offset)) {
+            error_setg_errno(errp, errno, "Could not resize file");
+            return -errno;
+        }
+        break;
+    default:
+        error_setg(errp, "Unsupported preallocation mode: %s",
+                   PreallocMode_str(prealloc));
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
 static int qemu_gluster_create(const char *filename,
                                QemuOpts *opts, Error **errp)
 {
@@ -1019,40 +1058,7 @@ static int qemu_gluster_create(const char *filename,
         goto out;
     }
 
-    switch (prealloc) {
-#ifdef CONFIG_GLUSTERFS_FALLOCATE
-    case PREALLOC_MODE_FALLOC:
-        if (glfs_fallocate(fd, 0, 0, total_size)) {
-            error_setg(errp, "Could not preallocate data for the new file");
-            ret = -errno;
-        }
-        break;
-#endif /* CONFIG_GLUSTERFS_FALLOCATE */
-#ifdef CONFIG_GLUSTERFS_ZEROFILL
-    case PREALLOC_MODE_FULL:
-        if (!glfs_ftruncate(fd, total_size)) {
-            if (glfs_zerofill(fd, 0, total_size)) {
-                error_setg(errp, "Could not zerofill the new file");
-                ret = -errno;
-            }
-        } else {
-            error_setg(errp, "Could not resize file");
-            ret = -errno;
-        }
-        break;
-#endif /* CONFIG_GLUSTERFS_ZEROFILL */
-    case PREALLOC_MODE_OFF:
-        if (glfs_ftruncate(fd, total_size) != 0) {
-            ret = -errno;
-            error_setg(errp, "Could not resize file");
-        }
-        break;
-    default:
-        ret = -EINVAL;
-        error_setg(errp, "Unsupported preallocation mode: %s",
-                   PreallocMode_str(prealloc));
-        break;
-    }
+    ret = qemu_gluster_do_truncate(fd, total_size, prealloc, errp);
 
 out:
     if (fd) {
Signature · 1 line
2.14.3
Reviewed-by on v1 2/7
#4 lore raw

Re: [Qemu-devel] [PATCH 2/7] gluster: Pull truncation from qemu_gluster_create

Quoted 8 lines · Max Reitz

Pull out the truncation code from the qemu_cluster_create() function so we can later reuse it in qemu_gluster_truncate().

---
  block/gluster.c | 74 +++++++++++++++++++++++++++++++--------------------------
  1 file changed, 40 insertions(+), 34 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#5 lore raw

PATCH 3/7 gluster: Query current size in do_truncate()

In reply to Max Reitz

Instead of expecting the current size to be 0, query it and allocate only the area [current_size, offset) if preallocation is requested.

---
 block/gluster.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
block/gluster.c +19 -2
index 8178541416..806b894bc8 100644
@@ -968,10 +968,27 @@ static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
 static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
                                     PreallocMode prealloc, Error **errp)
 {
+    int64_t current_length;
+
+    current_length = glfs_lseek(fd, 0, SEEK_END);
+    if (current_length < 0) {
+        error_setg_errno(errp, errno, "Failed to determine current size");
+        return -errno;
+    }
+
+    if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
+        error_setg(errp, "Cannot use preallocation for shrinking files");
+        return -ENOTSUP;
+    }
+
+    if (current_length == offset) {
+        return 0;
+    }
+
     switch (prealloc) {
 #ifdef CONFIG_GLUSTERFS_FALLOCATE
     case PREALLOC_MODE_FALLOC:
-        if (glfs_fallocate(fd, 0, 0, offset)) {
+        if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
             error_setg_errno(errp, errno, "Could not preallocate data");
             return -errno;
         }
@@ -983,7 +1000,7 @@ static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
             error_setg_errno(errp, errno, "Could not resize file");
             return -errno;
         }
-        if (glfs_zerofill(fd, 0, offset)) {
+        if (glfs_zerofill(fd, current_length, offset - current_length)) {
             error_setg_errno(errp, errno, "Could not zerofill the new area");
             return -errno;
         }
Signature · 1 line
2.14.3
Reviewed-by on v1 3/7
#6 lore raw

Re: [Qemu-devel] [PATCH 3/7] gluster: Query current size in do_truncate()

Quoted 8 lines · Max Reitz

Instead of expecting the current size to be 0, query it and allocate only the area [current_size, offset) if preallocation is requested.

---
  block/gluster.c | 21 +++++++++++++++++++--
  1 file changed, 19 insertions(+), 2 deletions(-)

I don't have a gluster setup handy to test this, but the concept matches file-posix and the change looks reasonable.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#7 lore raw

PATCH 4/7 gluster: Add preallocated truncation

In reply to Max Reitz

By using qemu_do_cluster_truncate() in qemu_cluster_truncate(), we now automatically have preallocated truncation.

---
 block/gluster.c | 17 +----------------
 1 file changed, 1 insertion(+), 16 deletions(-)
block/gluster.c +1 -16
index 806b894bc8..3f17b7819d 100644
@@ -1122,23 +1122,8 @@ static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
 static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset,
                                  PreallocMode prealloc, Error **errp)
 {
-    int ret;
     BDRVGlusterState *s = bs->opaque;
-
-    if (prealloc != PREALLOC_MODE_OFF) {
-        error_setg(errp, "Unsupported preallocation mode '%s'",
-                   PreallocMode_str(prealloc));
-        return -ENOTSUP;
-    }
-
-    ret = glfs_ftruncate(s->fd, offset);
-    if (ret < 0) {
-        ret = -errno;
-        error_setg_errno(errp, -ret, "Failed to truncate file");
-        return ret;
-    }
-
-    return 0;
+    return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
 }
 
 static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
Signature · 1 line
2.14.3
Reviewed-by on v1 4/7
#8 lore raw

Re: [Qemu-devel] [PATCH 4/7] gluster: Add preallocated truncation

Quoted 8 lines · Max Reitz

By using qemu_do_cluster_truncate() in qemu_cluster_truncate(), we now automatically have preallocated truncation.

---
  block/gluster.c | 17 +----------------
  1 file changed, 1 insertion(+), 16 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#9 lore raw

PATCH 5/7 sheepdog: Make sd_prealloc() take a BDS

In reply to Max Reitz

We want to use this function in sd_truncate() later on, so taking a filename is not exactly ideal.

---
 block/sheepdog.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)
block/sheepdog.c +21 -8
index af125a2c8d..cc1d37b3da 100644
@@ -1826,10 +1826,10 @@ static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot,
     return 0;
 }
 
-static int sd_prealloc(const char *filename, Error **errp)
+static int sd_prealloc(BlockDriverState *bs, Error **errp)
 {
     BlockBackend *blk = NULL;
-    BDRVSheepdogState *base = NULL;
+    BDRVSheepdogState *base = bs->opaque;
     unsigned long buf_size;
     uint32_t idx, max_idx;
     uint32_t object_size;
@@ -1837,10 +1837,11 @@ static int sd_prealloc(const char *filename, Error **errp)
     void *buf = NULL;
     int ret;
 
-    blk = blk_new_open(filename, NULL, NULL,
-                       BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
-    if (blk == NULL) {
-        ret = -EIO;
+    blk = blk_new(BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
+                  BLK_PERM_ALL);
+
+    ret = blk_insert_bs(blk, bs, errp);
+    if (ret < 0) {
         goto out_with_err_set;
     }
 
@@ -1852,7 +1853,6 @@ static int sd_prealloc(const char *filename, Error **errp)
         goto out;
     }
 
-    base = blk_bs(blk)->opaque;
     object_size = (UINT32_C(1) << base->inode.block_size_shift);
     buf_size = MIN(object_size, SD_DATA_OBJ_SIZE);
     buf = g_malloc0(buf_size);
@@ -2108,7 +2108,20 @@ static int sd_create(const char *filename, QemuOpts *opts,
     }
 
     if (prealloc) {
-        ret = sd_prealloc(filename, errp);
+        BlockDriverState *bs;
+        QDict *opts;
+
+        opts = qdict_new();
+        qdict_put_str(opts, "driver", "sheepdog");
+        bs = bdrv_open(filename, NULL, opts, BDRV_O_PROTOCOL | BDRV_O_RDWR,
+                       errp);
+        if (!bs) {
+            goto out;
+        }
+
+        ret = sd_prealloc(bs, errp);
+
+        bdrv_unref(bs);
     }
 out:
     g_free(backing_file);
Signature · 1 line
2.14.3
Reviewed-by on v1 5/7
#10 lore raw

Re: [Qemu-devel] [PATCH 5/7] sheepdog: Make sd_prealloc() take a BDS

Quoted 23 lines · Max Reitz

We want to use this function in sd_truncate() later on, so taking a filename is not exactly ideal.

---
  block/sheepdog.c | 29 +++++++++++++++++++++--------
  1 file changed, 21 insertions(+), 8 deletions(-)
block/sheepdog.c +5 -4
@@ -1837,10 +1837,11 @@ static int sd_prealloc(const char *filename, Error **errp)
      void *buf = NULL;
      int ret;
 
-    blk = blk_new_open(filename, NULL, NULL,
-                       BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
-    if (blk == NULL) {
-        ret = -EIO;
+    blk = blk_new(BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE | BLK_PERM_RESIZE,
+                  BLK_PERM_ALL);
+
+    ret = blk_insert_bs(blk, bs, errp);
+    if (ret < 0) {
 

block/sheepdog.c static int sd_prealloc(const char *filename, Error **errp)

Quoted 10 lines
block/sheepdog.c +7 -1
 
      if (prealloc) {
-        ret = sd_prealloc(filename, errp);
+        BlockDriverState *bs;
+        QDict *opts;
+
+        opts = qdict_new();
+        qdict_put_str(opts, "driver", "sheepdog");
+        bs = bdrv_open(filename, NULL, opts, BDRV_O_PROTOCOL | BDRV_O_RDWR,
+                       errp);

I'm a bit weak on ensuring the same ending flags result after the new bdrv_open()/blk_new() as what you used to get on blk_new_open(), so someone that can actually test this is appreciated.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#11 lore raw

PATCH 6/7 sheepdog: Pass old and new size to sd_prealloc()

In reply to Max Reitz

sd_prealloc() will now preallocate the area [old_size, new_size). As before, it rounds to buf_size and may thus overshoot and preallocate areas that were not requested to be preallocated. For image creation, this is no change in behavior. For truncation, this is in accordance with the documentation for preallocated truncation.

---
 block/sheepdog.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
block/sheepdog.c +5 -11
index cc1d37b3da..d300fb69c0 100644
@@ -1826,14 +1826,14 @@ static int do_sd_create(BDRVSheepdogState *s, uint32_t *vdi_id, int snapshot,
     return 0;
 }
 
-static int sd_prealloc(BlockDriverState *bs, Error **errp)
+static int sd_prealloc(BlockDriverState *bs, int64_t old_size, int64_t new_size,
+                       Error **errp)
 {
     BlockBackend *blk = NULL;
     BDRVSheepdogState *base = bs->opaque;
     unsigned long buf_size;
     uint32_t idx, max_idx;
     uint32_t object_size;
-    int64_t vdi_size;
     void *buf = NULL;
     int ret;
 
@@ -1847,19 +1847,13 @@ static int sd_prealloc(BlockDriverState *bs, Error **errp)
 
     blk_set_allow_write_beyond_eof(blk, true);
 
-    vdi_size = blk_getlength(blk);
-    if (vdi_size < 0) {
-        ret = vdi_size;
-        goto out;
-    }
-
     object_size = (UINT32_C(1) << base->inode.block_size_shift);
     buf_size = MIN(object_size, SD_DATA_OBJ_SIZE);
     buf = g_malloc0(buf_size);
 
-    max_idx = DIV_ROUND_UP(vdi_size, buf_size);
+    max_idx = DIV_ROUND_UP(new_size, buf_size);
 
-    for (idx = 0; idx < max_idx; idx++) {
+    for (idx = old_size / buf_size; idx < max_idx; idx++) {
         /*
          * The created image can be a cloned image, so we need to read
          * a data from the source image.
@@ -2119,7 +2113,7 @@ static int sd_create(const char *filename, QemuOpts *opts,
             goto out;
         }
 
-        ret = sd_prealloc(bs, errp);
+        ret = sd_prealloc(bs, 0, s->inode.vdi_size, errp);
 
         bdrv_unref(bs);
     }
Signature · 1 line
2.14.3
Reviewed-by on v1 6/7
#12 lore raw

Re: [Qemu-devel] [PATCH 6/7] sheepdog: Pass old and new size to sd_prealloc()

Quoted 24 lines · Max Reitz

sd_prealloc() will now preallocate the area [old_size, new_size). As before, it rounds to buf_size and may thus overshoot and preallocate areas that were not requested to be preallocated. For image creation, this is no change in behavior. For truncation, this is in accordance with the documentation for preallocated truncation.

---
  block/sheepdog.c | 16 +++++-----------
  1 file changed, 5 insertions(+), 11 deletions(-)
block/sheepdog.c +0 -6
@@ -1847,19 +1847,13 @@ static int sd_prealloc(BlockDriverState *bs, Error **errp)
 
      blk_set_allow_write_beyond_eof(blk, true);
 
-    vdi_size = blk_getlength(blk);
-    if (vdi_size < 0) {
-        ret = vdi_size;
-        goto out;
-    }
-
 

block/sheepdog.c static int sd_create(const char *filename, QemuOpts *opts, · line 2116

Quoted 5 lines
block/sheepdog.c +1 -1
@@ -2119,7 +2113,7 @@ static int sd_create(const char *filename, QemuOpts *opts,
              goto out;
          }
 
-        ret = sd_prealloc(bs, errp);
+        ret = sd_prealloc(bs, 0, s->inode.vdi_size, errp);

Nice - you also got rid of a potential failure in blk_getlength().

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#13 lore raw

PATCH 7/7 sheepdog: Allow fully preallocated truncation

In reply to Max Reitz
---
 block/sheepdog.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
block/sheepdog.c +12 -3
index d300fb69c0..ac02b10fe0 100644
@@ -2180,15 +2180,16 @@ static int sd_truncate(BlockDriverState *bs, int64_t offset,
     int ret, fd;
     unsigned int datalen;
     uint64_t max_vdi_size;
+    int64_t old_size = s->inode.vdi_size;
 
-    if (prealloc != PREALLOC_MODE_OFF) {
+    if (prealloc != PREALLOC_MODE_OFF && prealloc != PREALLOC_MODE_FULL) {
         error_setg(errp, "Unsupported preallocation mode '%s'",
                    PreallocMode_str(prealloc));
         return -ENOTSUP;
     }
 
     max_vdi_size = (UINT64_C(1) << s->inode.block_size_shift) * MAX_DATA_OBJS;
-    if (offset < s->inode.vdi_size) {
+    if (offset < old_size) {
         error_setg(errp, "shrinking is not supported");
         return -EINVAL;
     } else if (offset > max_vdi_size) {
@@ -2211,9 +2212,17 @@ static int sd_truncate(BlockDriverState *bs, int64_t offset,
 
     if (ret < 0) {
         error_setg_errno(errp, -ret, "failed to update an inode");
+        return ret;
     }
 
-    return ret;
+    if (prealloc == PREALLOC_MODE_FULL) {
+        ret = sd_prealloc(bs, old_size, offset, errp);
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
+    return 0;
 }
 
 /*
Signature · 1 line
2.14.3
Reviewed-by on v1 7/7
#14 lore raw

Re: [Qemu-devel] [PATCH 7/7] sheepdog: Allow fully preallocated truncation

Quoted 5 lines · Max Reitz
---
  block/sheepdog.c | 15 ++++++++++++---
  1 file changed, 12 insertions(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Applied
#15 lore raw
In reply to Max Reitz

Am 13.02.2018 um 14:03 hat Max Reitz geschrieben:

Quoted 9 lines

As far as I can see, these are the only protocols beside file-posix that support preallocated creation. In contrast to file-posix, however, they have not supported preallocated truncation so far. This series brings their truncation code to feature parity with their creation code in this regard.

Note that I do not have a test setup for either of the two drivers, so I do not actually know whether this works. Anyone with a setup is more than welcome to test this series.

Thanks, applied to the block branch.

We'll see if someone screams once this hits master.

Kevin