Received: from eggs.gnu.org ([2001:4830:134:3::10]:43794) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W456X-0004gF-Ib for qemu-devel@nongnu.org; Fri, 17 Jan 2014 03:48:49 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1W456P-0004Le-5A for qemu-devel@nongnu.org; Fri, 17 Jan 2014 03:48:41 -0500 Received: from mail-ea0-x230.google.com ([2a00:1450:4013:c01::230]:42782) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1W456O-0004KO-U1 for qemu-devel@nongnu.org; Fri, 17 Jan 2014 03:48:33 -0500 Received: by mail-ea0-f176.google.com with SMTP id h14so1602902eaj.7 for ; Fri, 17 Jan 2014 00:48:31 -0800 (PST) Date: Fri, 17 Jan 2014 16:48:16 +0800 From: Stefan Hajnoczi Message-ID: <20140117084816.GC31039@stefanha-thinkpad.redhat.com> References: <25565d3a53f29829dc2d97480cb19ba34be49895.1388112645.git.hutao@cn.fujitsu.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <25565d3a53f29829dc2d97480cb19ba34be49895.1388112645.git.hutao@cn.fujitsu.com> Subject: Re: [Qemu-devel] [RFC PATCH v4 4/4] qcow2: Add full image preallocation option List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Hu Tao Cc: Kevin Wolf , Fam Zheng , Peter Lieven , qemu-devel@nongnu.org On Fri, Dec 27, 2013 at 11:05:54AM +0800, Hu Tao wrote: This approach seems okay but the calculation isn't quite right yet. On Windows an error would be raised since we don't have preallocate=full support. That's okay. > @@ -1477,16 +1478,53 @@ 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 nrefe, nl2e; > + BlockDriver *drv; > + > + 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 refblocks */ > + nrefe = (total_size / cluster_size); > + nrefe = align_offset(nrefe, cluster_size / sizeof(uint16_t)); > + meta_size += nrefe * sizeof(uint16_t); Every host cluster is reference-counted, including metadata (even refcount blocks are recursively included). This calculation is wrong because it only considers data clusters. > + /* total size of reftables */ > + meta_size += nrefe * sizeof(uint16_t) * sizeof(uint16_t) / cluster_size; I don't understand this calculation. The refcount table consists of contiguous clusters where each element is a 64-bit offset to a refcount block. > + /* 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 */ > + meta_size += nl2e * sizeof(uint64_t) * sizeof(uint64_t) / cluster_size; Another strange calculation. The L1 table consists of contiguous clusters where each element is a 64-bit offset to an L1 table.