On Tue, Feb 11, 2014 at 03:07:10PM +0800, Hu Tao wrote: > This adds a preallocation=full mode to qcow2 image creation, which > creates a non-sparse image file.
Ping. This is the major part of the patchset that needs review, especially the calculation of metadata size. > > Signed-off-by: Hu Tao <hu...@cn.fujitsu.com> > --- > block/qcow2.c | 75 > ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- > 1 file changed, 72 insertions(+), 3 deletions(-) > > diff --git a/block/qcow2.c b/block/qcow2.c > index e4bab70..4b113b7 100644 > --- a/block/qcow2.c > +++ b/block/qcow2.c > @@ -1456,6 +1456,7 @@ static int qcow2_create2(const char *filename, int64_t > total_size, > QEMUOptionParameter *options, int version, > Error **errp) > { > + QEMUOptionParameter *alloc_options = NULL; > /* Calculate cluster_bits */ > int cluster_bits; > cluster_bits = ffs(cluster_size) - 1; > @@ -1485,16 +1486,80 @@ static int qcow2_create2(const char *filename, > int64_t total_size, > Error *local_err = NULL; > int ret; > > + if (prealloc == PREALLOC_MODE_FULL) { > + int64_t meta_size = 0; > + unsigned nreftablee, nrefblocke, nl1e, nl2e; > + BlockDriver *drv; > + > + total_size = align_offset(total_size, cluster_size); > + > + drv = bdrv_find_protocol(filename, true); > + if (drv == NULL) { > + error_setg(errp, "Could not find protocol for file '%s'", > filename); > + return -ENOENT; > + } > + > + alloc_options = append_option_parameters(alloc_options, > + drv->create_options); > + alloc_options = append_option_parameters(alloc_options, options); > + > + /* header: 1 cluster */ > + meta_size += cluster_size; > + > + /* total size of L2 tables */ > + nl2e = total_size / cluster_size; > + nl2e = align_offset(nl2e, cluster_size / sizeof(uint64_t)); > + meta_size += nl2e * sizeof(uint64_t); > + > + /* total size of L1 tables */ > + nl1e = nl2e * sizeof(uint64_t) / cluster_size; > + nl1e = align_offset(nl1e, cluster_size / sizeof(uint64_t)); Although not stated in the qcow2 spec, but actually l1 tables are aligned to cluster too because of the nature of qcow2_alloc_clusters(). Same for refcount tables below. > + meta_size += nl1e * sizeof(uint64_t); > + > + /* total size of refcount blocks > + * > + * note: every host cluster is reference-counted, including metadata > + * (even refcount blocks are recursively included). > + * Let: > + * a = total_size (this is the guest disk size) > + * m = meta size not including refcount blocks and refcount tables > + * c = cluster size > + * y1 = number of refcount blocks entries > + * y2 = meta size including everything > + * then, > + * y1 = (y2 + a)/c > + * y2 = y1 * sizeof(u16) + y1 * sizeof(u16) * sizeof(u64) / c + m > + * we can get y1: > + * y1 = (a + m) / (c - sizeof(u16) - sizeof(u16) * sizeof(u64) / c) > + */ > + nrefblocke = (total_size + meta_size + cluster_size) / > + (cluster_size - sizeof(uint16_t) - > + 1.0 * sizeof(uint16_t) * sizeof(uint64_t) / cluster_size); > + nrefblocke = align_offset(nrefblocke, cluster_size / > sizeof(uint16_t)); > + meta_size += nrefblocke * sizeof(uint16_t); > + > + /* total size of refcount tables */ > + nreftablee = nrefblocke * sizeof(uint16_t) / cluster_size; > + nreftablee = align_offset(nreftablee, cluster_size / > sizeof(uint64_t)); > + meta_size += nreftablee * sizeof(uint64_t); > + > + set_option_parameter_int(alloc_options, BLOCK_OPT_SIZE, > + total_size + meta_size); > + set_option_parameter(alloc_options, BLOCK_OPT_PREALLOC, "full"); > + > + options = alloc_options; > + } > + > ret = bdrv_create_file(filename, options, &local_err); > if (ret < 0) { > error_propagate(errp, local_err); > - return ret; > + goto out_options; > } > > ret = bdrv_file_open(&bs, filename, NULL, NULL, BDRV_O_RDWR, &local_err); > if (ret < 0) { > error_propagate(errp, local_err); > - return ret; > + goto out_options; > } > > /* Write the header */ > @@ -1611,6 +1676,8 @@ static int qcow2_create2(const char *filename, int64_t > total_size, > ret = 0; > out: > bdrv_unref(bs); > +out_options: > + free_option_parameters(alloc_options); > return ret; > } > > @@ -1646,6 +1713,8 @@ static int qcow2_create(const char *filename, > QEMUOptionParameter *options, > prealloc = PREALLOC_MODE_OFF; > } else if (!strcmp(options->value.s, "metadata")) { > prealloc = PREALLOC_MODE_METADATA; > + } else if (!strcmp(options->value.s, "full")) { > + prealloc = PREALLOC_MODE_FULL; > } else { > error_setg(errp, "Invalid preallocation mode: '%s'", > options->value.s); > @@ -2211,7 +2280,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, > -- > 1.8.0 >