Skip to contentSkip to conversation

qemu-img: add preallocation=full

RFC PATCH Superseded Hu Tao 3 participants 22 messages Nov 27, 2013 – Dec 17, 2013 Block layer

v2 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Hu Tao

author
#0 lore raw

v2 cover qemu-img: add preallocation=full

This series implements full image preallocation to create a non-sparse image file at creation time, both for raw and qcow2 format. The purpose is to avoid performance deterioration of the guest cause by sparse image.

This series implements full preallocation by using fallocate()/posix_fallocate(), which have the advantage that it is fast when creating large image file. Zero-filling is not implemented, as writing zeros to image could be slow for large file. (fallocate() ensures zero-filling, but posix_fallocate()) If users want it, we can maybe add a option to let users have a choice. Suggestions?

Base on Kevin's patch at:
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg03017.html

Hu Tao (6):
block: introduce prealloc_mode
block: add BlockDriver.bdrv_preallocate.
block/raw-posix: implement bdrv_preallocate
raw-posix: Add full image preallocation option
qcow2: implement bdrv_preallocate
qcow2: Add full image preallocation option

 block.c                   | 13 +++++++++++
 block/qcow2.c             | 32 +++++++++++++++++++++------
 block/raw-posix.c         | 56 +++++++++++++++++++++++++++++++++++++++++++++++
 include/block/block.h     |  7 ++++++
 include/block/block_int.h |  3 +++
 5 files changed, 104 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.3.1
author
#1 lore raw

RFC PATCH v2 1/6 block: introduce prealloc_mode

This patch prepares for the subsequent patches.

---
 block/qcow2.c         | 6 +++---
 include/block/block.h | 6 ++++++
 2 files changed, 9 insertions(+), 3 deletions(-)
block/qcow2.c +3 -3
index 6e5d98d..b054a01 100644
@@ -1608,7 +1608,7 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
     uint64_t sectors = 0;
     int flags = 0;
     size_t cluster_size = DEFAULT_CLUSTER_SIZE;
-    int prealloc = 0;
+    int prealloc = PREALLOC_OFF;
     int version = 3;
     Error *local_err = NULL;
     int ret;
@@ -1629,9 +1629,9 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
             }
         } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
             if (!options->value.s || !strcmp(options->value.s, "off")) {
-                prealloc = 0;
+                prealloc = PREALLOC_OFF;
             } else if (!strcmp(options->value.s, "metadata")) {
-                prealloc = 1;
+                prealloc = PREALLOC_METADATA;
             } else {
                 error_setg(errp, "Invalid preallocation mode: '%s'",
                            options->value.s);
include/block/block.h +6 -0
index 3560deb..461eaa4 100644
@@ -487,4 +487,10 @@ int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
 int bdrv_debug_resume(BlockDriverState *bs, const char *tag);
 bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag);
 
+enum prealloc_mode {
+    PREALLOC_OFF = 0,
+    PREALLOC_METADATA,
+    PREALLOC_FULL,
+};
+
 #endif
Signature · 1 line
1.8.3.1
author
#2 lore raw

RFC PATCH v2 2/6 block: add BlockDriver.bdrv_preallocate.

In reply to Hu Tao

This field is used to preallocate disk space for block device.

---
 block.c                   | 13 +++++++++++++
 include/block/block.h     |  1 +
 include/block/block_int.h |  3 +++
 3 files changed, 17 insertions(+)
block.c +13 -0
index 382ea71..3985133 100644
@@ -3102,6 +3102,19 @@ int bdrv_has_zero_init(BlockDriverState *bs)
     return 0;
 }
 
+int bdrv_preallocate(BlockDriverState *bs, int64_t offset, int64_t length)
+{
+    if (bs->backing_hd) {
+        return -1;
+    }
+
+    if (bs->drv->bdrv_preallocate) {
+        return bs->drv->bdrv_preallocate(bs, offset, length);
+    }
+
+    return -1;
+}
+
 typedef struct BdrvCoGetBlockStatusData {
     BlockDriverState *bs;
     BlockDriverState *base;
include/block/block.h +1 -0
index 461eaa4..8d48c8a 100644
@@ -315,6 +315,7 @@ void bdrv_drain_all(void);
 int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
 int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
 int bdrv_has_zero_init_1(BlockDriverState *bs);
+int bdrv_preallocate(BlockDriverState *bs, int64_t offset, int64_t length);
 int bdrv_has_zero_init(BlockDriverState *bs);
 int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
                               int nb_sectors, int *pnum);
include/block/block_int.h +3 -0
index 1666066..6ac3735 100644
@@ -227,6 +227,9 @@ struct BlockDriver {
      */
     int (*bdrv_has_zero_init)(BlockDriverState *bs);
 
+    int (*bdrv_preallocate)(BlockDriverState *bs, int64_t offset,
+                            int64_t length);
+
     QLIST_ENTRY(BlockDriver) list;
 };
 
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/6] block: add BlockDriver.bdrv_preallocate.

Quoted 15 lines · Hu Tao

This field is used to preallocate disk space for block device.

---
  block.c                   | 13 +++++++++++++
  include/block/block.h     |  1 +
  include/block/block_int.h |  3 +++
  3 files changed, 17 insertions(+)
block.c +0 -0
index 382ea71..3985133 100644

block.c int bdrv_has_zero_init(BlockDriverState *bs) · line 3108

Quoted 7 lines
block.c +4 -0
@@ -3102,6 +3102,19 @@ int bdrv_has_zero_init(BlockDriverState *bs)
      return 0;
  }
 
+int bdrv_preallocate(BlockDriverState *bs, int64_t offset, int64_t length)
+{
+    if (bs->backing_hd) {
+        return -1;

Better to use -ENOTSUP.

block.c int bdrv_has_zero_init(BlockDriverState *bs) · line 3115

Quoted 7 lines
block.c +7 -0
+    }
+
+    if (bs->drv->bdrv_preallocate) {
+        return bs->drv->bdrv_preallocate(bs, offset, length);
+    }
+
+    return -1;

-ENOTSUP

Fam

Quoted 32 lines
block.c +2 -0
+}
+
  typedef struct BdrvCoGetBlockStatusData {
      BlockDriverState *bs;
      BlockDriverState *base;
include/block/block.h +1 -0
index 461eaa4..8d48c8a 100644
@@ -315,6 +315,7 @@ void bdrv_drain_all(void);
  int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
  int bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
  int bdrv_has_zero_init_1(BlockDriverState *bs);
+int bdrv_preallocate(BlockDriverState *bs, int64_t offset, int64_t length);
  int bdrv_has_zero_init(BlockDriverState *bs);
  int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
                                int nb_sectors, int *pnum);
include/block/block_int.h +3 -0
index 1666066..6ac3735 100644
@@ -227,6 +227,9 @@ struct BlockDriver {
       */
      int (*bdrv_has_zero_init)(BlockDriverState *bs);
 
+    int (*bdrv_preallocate)(BlockDriverState *bs, int64_t offset,
+                            int64_t length);
+
      QLIST_ENTRY(BlockDriver) list;
  };
 
author
#4 lore raw

RFC PATCH v2 3/6 block/raw-posix: implement bdrv_preallocate

In reply to Hu Tao
---
 block/raw-posix.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
block/raw-posix.c +34 -0
index f836c8e..c563073 100644
@@ -1050,6 +1050,39 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
     return (int64_t)st.st_blocks * 512;
 }
 
+#ifdef __linux__
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    int ret = -1;
+
+    ret = fallocate(fd, 0, offset, length);
+
+    /* fallback to posix_fallocate() if fallocate() is not supported */
+    if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) {
+        ret = posix_fallocate(fd, offset, length);
+    }
+
+    return ret;
+}
+#else
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    return posix_fallocate(fd, offset, length);
+}
+#endif
+
+static int raw_preallocate(BlockDriverState *bs, int64_t offset, int64_t length)
+{
+    BDRVRawState *s = bs->opaque;
+    int64_t len = bdrv_getlength(bs);
+
+    if (offset + length < 0 || offset + length > len) {
+        return -1;
+    }
+
+    return raw_preallocate2(s->fd, offset, length);
+}
+
 static int raw_create(const char *filename, QEMUOptionParameter *options,
                       Error **errp)
 {
@@ -1221,6 +1254,7 @@ static BlockDriver bdrv_file = {
     .bdrv_close = raw_close,
     .bdrv_create = raw_create,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
+    .bdrv_preallocate = raw_preallocate,
     .bdrv_co_get_block_status = raw_co_get_block_status,
 
     .bdrv_aio_readv = raw_aio_readv,
Signature · 1 line
1.8.3.1
#5 lore raw

Re: [Qemu-devel] [RFC PATCH v2 3/6] block/raw-posix: implement bdrv_preallocate

Quoted 31 lines · Hu Tao
---
  block/raw-posix.c | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)
block/raw-posix.c +18 -0
index f836c8e..c563073 100644
@@ -1050,6 +1050,39 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs)
      return (int64_t)st.st_blocks * 512;
  }
 
+#ifdef __linux__
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    int ret = -1;
+
+    ret = fallocate(fd, 0, offset, length);
+
+    /* fallback to posix_fallocate() if fallocate() is not supported */
+    if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) {
+        ret = posix_fallocate(fd, offset, length);
+    }
+
+    return ret;
+}
+#else
+static int raw_preallocate2(int fd, int64_t offset, int64_t length)
+{
+    return posix_fallocate(fd, offset, length);

block/raw-posix.c static int64_t raw_get_allocated_file_size(BlockDriverState *bs) · line 1080

Quoted 10 lines
block/raw-posix.c +10 -0
+}
+#endif
+
+static int raw_preallocate(BlockDriverState *bs, int64_t offset, int64_t length)
+{
+    BDRVRawState *s = bs->opaque;
+    int64_t len = bdrv_getlength(bs);
+
+    if (offset + length < 0 || offset + length > len) {
+        return -1;

-EINVAL

Fam

Quoted 17 lines
block/raw-posix.c +6 -0
+    }
+
+    return raw_preallocate2(s->fd, offset, length);
+}
+
  static int raw_create(const char *filename, QEMUOptionParameter *options,
                        Error **errp)
  {
@@ -1221,6 +1254,7 @@ static BlockDriver bdrv_file = {
      .bdrv_close = raw_close,
      .bdrv_create = raw_create,
      .bdrv_has_zero_init = bdrv_has_zero_init_1,
+    .bdrv_preallocate = raw_preallocate,
      .bdrv_co_get_block_status = raw_co_get_block_status,
 
      .bdrv_aio_readv = raw_aio_readv,
author
#6 lore raw

RFC PATCH v2 4/6 raw-posix: Add full image preallocation option

In reply to Hu Tao

This patch adds a new option preallocation for raw format, and implements full preallocation.

---
 block/raw-posix.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)
block/raw-posix.c +22 -0
index c563073..200886d 100644
@@ -1089,11 +1089,22 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
     int fd;
     int result = 0;
     int64_t total_size = 0;
+    int prealloc = PREALLOC_OFF;
 
     /* Read out options */
     while (options && options->name) {
         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
             total_size = options->value.n / BDRV_SECTOR_SIZE;
+        } else if (!strcmp(options->name, BLOCK_OPT_PREALLOC)) {
+            if (!options->value.s || !strcmp(options->value.s, "off")) {
+                prealloc = PREALLOC_OFF;
+            } else if (!strcmp(options->value.s, "full")) {
+                prealloc = PREALLOC_FULL;
+            } else {
+                error_setg(errp, "Invalid preallocation mode: '%s'",
+                           options->value.s);
+                return -EINVAL;
+            }
         }
         options++;
     }
@@ -1108,6 +1119,12 @@ static int raw_create(const char *filename, QEMUOptionParameter *options,
             result = -errno;
             error_setg_errno(errp, -result, "Could not resize file");
         }
+        if (prealloc == PREALLOC_FULL &&
+            raw_preallocate2(fd, 0, total_size * BDRV_SECTOR_SIZE) != 0) {
+            result = -errno;
+            error_setg_errno(errp, -result,
+                             "Could not preallocate data for the new file");
+        }
         if (qemu_close(fd) != 0) {
             result = -errno;
             error_setg_errno(errp, -result, "Could not close the new file");
@@ -1238,6 +1255,11 @@ static QEMUOptionParameter raw_create_options[] = {
         .type = OPT_SIZE,
         .help = "Virtual disk size"
     },
+    {
+        .name = BLOCK_OPT_PREALLOC,
+        .type = OPT_STRING,
+        .help = "Preallocation mode (allowed values: off, full)"
+    },
     { NULL }
 };
 
Signature · 1 line
1.8.3.1
author
#7 lore raw

RFC PATCH v2 5/6 qcow2: implement bdrv_preallocate

In reply to Hu Tao
---
 block/qcow2.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/qcow2.c +7 -0
index b054a01..a23fade 100644
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
     return 0;
 }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+
 static QEMUOptionParameter qcow2_create_options[] = {
     {
         .name = BLOCK_OPT_SIZE,
@@ -2234,6 +2240,7 @@ static BlockDriver bdrv_qcow2 = {
     .bdrv_reopen_prepare  = qcow2_reopen_prepare,
     .bdrv_create        = qcow2_create,
     .bdrv_has_zero_init = bdrv_has_zero_init_1,
+    .bdrv_preallocate   = qcow2_preallocate,
     .bdrv_co_get_block_status = qcow2_co_get_block_status,
     .bdrv_set_key       = qcow2_set_key,
     .bdrv_make_empty    = qcow2_make_empty,
Signature · 1 line
1.8.3.1
#8 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 10 lines · Hu Tao
---
  block/qcow2.c | 7 +++++++
  1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
      return 0;
  }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

Fam

Quoted 12 lines
block/qcow2.c +1 -0
  static QEMUOptionParameter qcow2_create_options[] = {
      {
          .name = BLOCK_OPT_SIZE,
@@ -2234,6 +2240,7 @@ static BlockDriver bdrv_qcow2 = {
      .bdrv_reopen_prepare  = qcow2_reopen_prepare,
      .bdrv_create        = qcow2_create,
      .bdrv_has_zero_init = bdrv_has_zero_init_1,
+    .bdrv_preallocate   = qcow2_preallocate,
      .bdrv_co_get_block_status = qcow2_co_get_block_status,
      .bdrv_set_key       = qcow2_set_key,
      .bdrv_make_empty    = qcow2_make_empty,
author
#9 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 25 lines · Fam Zheng
Quoted 10 lines · Hu Tao
---
 block/qcow2.c | 7 +++++++
 1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
     return 0;
 }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

#10 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 10 lines · Hu Tao
---
  block/qcow2.c | 7 +++++++
  1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
      return 0;
  }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

Fam

#11 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 31 lines · Hu Tao
Quoted 30 lines · Fam Zheng
Quoted 28 lines · Hu Tao
Quoted 10 lines
---
  block/qcow2.c | 7 +++++++
  1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
      return 0;
  }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

Quoted 5 lines
Quoted 3 lines

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Peter

#12 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 36 lines · Peter Lieven

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 35 lines · Hu Tao
Quoted 33 lines · Fam Zheng
Quoted 25 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

Quoted 6 lines
Quoted 1 line

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

Fam

#13 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 37 lines · Peter Lieven
Quoted 36 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 35 lines · Hu Tao
Quoted 33 lines · Fam Zheng
Quoted 25 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

Peter

author
#14 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 31 lines · Hu Tao
Quoted 30 lines · Fam Zheng
Quoted 28 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

Quoted 5 lines
Quoted 3 lines

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

Quoted 7 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

#15 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 42 lines · Hu Tao
Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 24 lines · Hu Tao
Quoted 10 lines
---
    block/qcow2.c | 7 +++++++
    1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
        return 0;
    }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

When you want to e.g. convert this qcow2 later the performance is lower than needed because you read all those preallocated sectors altough you could now they are empty.

Quoted 10 lines
Quoted 7 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

If you do not premap them they are allocated in the order they are written.
So if you are going to preallocate the whole file anyway, you should sequentally map all clusters into the file AND still keep the information that they are in fact not yet written.

Peter

author
#16 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 57 lines · Peter Lieven
Quoted 42 lines · Hu Tao
Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 24 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

When you want to e.g. convert this qcow2 later the performance is lower than needed because you read all those preallocated sectors altough you could now they are empty.

Quoted 5 lines
Quoted 5 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

If you do not premap them they are allocated in the order they are written.
So if you are going to preallocate the whole file anyway, you should sequentally map all clusters into the file AND still keep the information that they are in fact not yet written.

Can this be achieved by first fallocate() the disk file, then allocate metadata? This way all metadata clusters are allocated before any data clusters, leaving all data clusters at the end of file.

Signature · 2 lines
Regards,
Hu Tao
author
#17 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 63 lines · Hu Tao
Quoted 62 lines · Peter Lieven
Quoted 42 lines · Hu Tao
Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 24 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

When you want to e.g. convert this qcow2 later the performance is lower than needed because you read all those preallocated sectors altough you could now they are empty.

Quoted 10 lines
Quoted 7 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

Quoted 7 lines
Quoted 3 lines

If you do not premap them they are allocated in the order they are written.
So if you are going to preallocate the whole file anyway, you should sequentally map all clusters into the file AND still keep the information that they are in fact not yet written.

Can this be achieved by first fallocate() the disk file, then allocate metadata? This way all metadata clusters are allocated before any data clusters, leaving all data clusters at the end of file.

Any comments?

#18 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

In reply to Hu Tao
Quoted 63 lines · Hu Tao
Quoted 62 lines · Peter Lieven
Quoted 42 lines · Hu Tao
Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 24 lines · Hu Tao
Quoted 10 lines
---
    block/qcow2.c | 7 +++++++
    1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
        return 0;
    }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

When you want to e.g. convert this qcow2 later the performance is lower than needed because you read all those preallocated sectors altough you could now they are empty.

Quoted 10 lines
Quoted 7 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

Quoted 8 lines
Quoted 3 lines

If you do not premap them they are allocated in the order they are written.
So if you are going to preallocate the whole file anyway, you should sequentally map all clusters into the file AND still keep the information that they are in fact not yet written.

Can this be achieved by first fallocate() the disk file, then allocate metadata? This way all metadata clusters are allocated before any data clusters, leaving all data clusters at the end of file.

I think Peter means your need to sequentially map clusters into the file, so that sequential IO in guest is translated to sequential IO on the image file.

fallocate() or posix_fallocate() should work. You need to set zero flag on the allocated cluster when mapping it in L2, instead of actually writing zeros.

Fam

author
#19 lore raw

Re: [Qemu-devel] [RFC PATCH v2 5/6] qcow2: implement bdrv_preallocate

Quoted 68 lines · Fam Zheng
Quoted 67 lines · Hu Tao
Quoted 65 lines · Peter Lieven
Quoted 42 lines · Hu Tao
Quoted 41 lines · Peter Lieven

Am 27.11.2013 11:07, schrieb Fam Zheng:

Quoted 40 lines · Peter Lieven
Quoted 39 lines

Am 27.11.2013 07:40, schrieb Fam Zheng:

Quoted 26 lines · Hu Tao
Quoted 25 lines · Fam Zheng
Quoted 24 lines · Hu Tao
Quoted 10 lines
---
   block/qcow2.c | 7 +++++++
   1 file changed, 7 insertions(+)
block/qcow2.c +0 -0
index b054a01..a23fade 100644

block/qcow2.c static int qcow2_amend_options(BlockDriverState *bs, · line 2188

Quoted 9 lines
block/qcow2.c +6 -0
@@ -2180,6 +2180,12 @@ static int qcow2_amend_options(BlockDriverState *bs,
       return 0;
   }
 
+static int qcow2_preallocate(BlockDriverState *bs, int64_t offset,
+                             int64_t length)
+{
+    return bdrv_preallocate(bs->file, offset, length);
+}
+

What's the semantics of .bdrv_preallocate? I think you should map [offset, offset + length) to clusters in image file, and then forward to bs->file, rather than this direct wrapper.

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines

E.g. bdrv_preallocate(qcow2_bs, 0, cluster_size) should call bdrv_preallocate(qcow2_bs->file, offset_off_first_cluster, cluster_size).

You mean data clusters here, right? Is there a single function to get the offset of the first data cluster?

There is a function, qcow2_get_cluster_offset.

This should return no valid offset as long as the cluster is not allocated.

I think you actually have to "write" all clusters of a qcow2 one by one. Eventually this write could be an fallocate call instead of a zero write.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Yes, I was wrong about qcow2_get_cluster_offset. The logic here is more like cluster allocation in qcow2_alloc_cluster_offset. Maybe we can reuse that.

What I don't like about the preallocation is that we would loose the information that a cluster contains no valid data and would read it e.g. during conversion.

So the information is stored in table and you mean we shouldn't clear table when do preallocation? I'm not sure how the information could be useful on a newly-created image, but it seems ideal to keep informations in table.

When you want to e.g. convert this qcow2 later the performance is lower than needed because you read all those preallocated sectors altough you could now they are empty.

Quoted 10 lines
Quoted 7 lines

I think what we want is a preallocated image with all clusters sequentally mapped into the qcow2 file. Preallocate all the cluster extends, but still have the information in the table that the cluster in fact has no valid data. So we would need a valid cluster offset while still haveing the flag that the cluster is unallocated. I think this would require thoughtfully checking all the cluster functions if they can easily cope with this.

The quetion is Hu, what do you want to achieve? Do you want that the space on the filesystem is preallocated so you can't overcommit or do you also want a sequential mapping of all the clusters into the file?

The goal is to avoid sparse file as it can cause performance problem. So the first one. I'm not sure about the second but IIUC, one fallocate() is enough for all clusters if they are sequentially mapped.

If you do not premap them they are allocated in the order they are written.
So if you are going to preallocate the whole file anyway, you should sequentally map all clusters into the file AND still keep the information that they are in fact not yet written.

Quoted 8 lines
Quoted 4 lines

Can this be achieved by first fallocate() the disk file, then allocate metadata? This way all metadata clusters are allocated before any data clusters, leaving all data clusters at the end of file.

I think Peter means your need to sequentially map clusters into the file, so that sequential IO in guest is translated to sequential IO on the image file.

The question is how to map data clusters sequentially. If I read code correctly, qemu allocates clusters (containing data and metadata) on demand. So if we allocates all metadata clusters right after the creation of image file, we can leave all left space allocated for data laterly. Fix me.

Quoted 4 lines

fallocate() or posix_fallocate() should work. You need to set zero flag on the allocated cluster when mapping it in L2, instead of actually writing zeros.

You mean bit 0 of L2 entry by zero flag? But the doc says it is always 0 with version 2.

author
#20 lore raw

RFC PATCH v2 6/6 qcow2: Add full image preallocation option

In reply to Hu Tao

This adds a preallocation=full mode to qcow2 image creation, which creates a non-sparse image file.

---
 block/qcow2.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)
block/qcow2.c +15 -4
index a23fade..492c5e6 100644
@@ -1385,7 +1385,7 @@ static int qcow2_change_backing_file(BlockDriverState *bs,
     return qcow2_update_header(bs);
 }
 
-static int preallocate(BlockDriverState *bs)
+static int preallocate(BlockDriverState *bs, enum prealloc_mode mode)
 {
     uint64_t nb_sectors;
     uint64_t offset;
@@ -1394,6 +1394,8 @@ static int preallocate(BlockDriverState *bs)
     int ret;
     QCowL2Meta *meta;
 
+    assert(mode != PREALLOC_OFF);
+
     nb_sectors = bdrv_getlength(bs) >> 9;
     offset = 0;
 
@@ -1424,6 +1426,13 @@ static int preallocate(BlockDriverState *bs)
         offset += num << 9;
     }
 
+    if (mode == PREALLOC_FULL) {
+        ret = bdrv_preallocate(bs->file, 0, bdrv_getlength(bs));
+        if (ret < 0) {
+            return ret;
+        }
+    }
+
     /*
      * It is expected that the image file is large enough to actually contain
      * all of the allocated clusters (otherwise we get failing reads after
@@ -1572,11 +1581,11 @@ static int qcow2_create2(const char *filename, int64_t total_size,
         }
     }
 
-    /* And if we're supposed to preallocate metadata, do that now */
+    /* And if we're supposed to preallocate data, do that now */
     if (prealloc) {
         BDRVQcowState *s = bs->opaque;
         qemu_co_mutex_lock(&s->lock);
-        ret = preallocate(bs);
+        ret = preallocate(bs, prealloc);
         qemu_co_mutex_unlock(&s->lock);
         if (ret < 0) {
             error_setg_errno(errp, -ret, "Could not preallocate metadata");
@@ -1632,6 +1641,8 @@ static int qcow2_create(const char *filename, QEMUOptionParameter *options,
                 prealloc = PREALLOC_OFF;
             } else if (!strcmp(options->value.s, "metadata")) {
                 prealloc = PREALLOC_METADATA;
+            } else if (!strcmp(options->value.s, "full")) {
+                prealloc = PREALLOC_FULL;
             } else {
                 error_setg(errp, "Invalid preallocation mode: '%s'",
                            options->value.s);
@@ -2221,7 +2232,7 @@ static QEMUOptionParameter qcow2_create_options[] = {
     {
         .name = BLOCK_OPT_PREALLOC,
         .type = OPT_STRING,
-        .help = "Preallocation mode (allowed values: off, metadata)"
+        .help = "Preallocation mode (allowed values: off, metadata, full)"
     },
     {
         .name = BLOCK_OPT_LAZY_REFCOUNTS,
Signature · 1 line
1.8.3.1
#21 lore raw
In reply to Hu Tao
Quoted 10 lines · Hu Tao

This series implements full image preallocation to create a non-sparse image file at creation time, both for raw and qcow2 format. The purpose is to avoid performance deterioration of the guest cause by sparse image.

This series implements full preallocation by using fallocate()/posix_fallocate(), which have the advantage that it is fast when creating large image file. Zero-filling is not implemented, as writing zeros to image could be slow for large file. (fallocate() ensures zero-filling, but posix_fallocate()) If users want it, we can maybe add a option to let users have a choice. Suggestions?

We have zero init in raw-posix:

static BlockDriver bdrv_file = {
    ...
    .bdrv_has_zero_init = bdrv_has_zero_init_1,
}

so I think we have to zero-fill it if posix_fallocate doesn't ensure.

Fam

Quoted 18 lines

Base on Kevin's patch at:
http://lists.gnu.org/archive/html/qemu-devel/2011-01/msg03017.html

Hu Tao (6):
block: introduce prealloc_mode
block: add BlockDriver.bdrv_preallocate.
block/raw-posix: implement bdrv_preallocate
raw-posix: Add full image preallocation option
qcow2: implement bdrv_preallocate
qcow2: Add full image preallocation option

  block.c                   | 13 +++++++++++
  block/qcow2.c             | 32 +++++++++++++++++++++------
  block/raw-posix.c         | 56 +++++++++++++++++++++++++++++++++++++++++++++++
  include/block/block.h     |  7 ++++++
  include/block/block_int.h |  3 +++
  5 files changed, 104 insertions(+), 7 deletions(-)