From patchwork Fri Mar 26 16:40:45 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 48698 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 8D27DB7CF1 for ; Sat, 27 Mar 2010 05:50:10 +1100 (EST) Received: from localhost ([127.0.0.1]:33372 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NvEbc-0004eb-M5 for incoming@patchwork.ozlabs.org; Fri, 26 Mar 2010 14:50:04 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1NvCbw-0002f1-3I for qemu-devel@nongnu.org; Fri, 26 Mar 2010 12:42:16 -0400 Received: from [140.186.70.92] (port=33016 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1NvCbp-0002Mt-6U for qemu-devel@nongnu.org; Fri, 26 Mar 2010 12:42:14 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1NvCb5-0004Hg-I4 for qemu-devel@nongnu.org; Fri, 26 Mar 2010 12:41:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63615) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1NvCb5-0004HX-B0 for qemu-devel@nongnu.org; Fri, 26 Mar 2010 12:41:23 -0400 Received: from int-mx05.intmail.prod.int.phx2.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.18]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o2QGfLfj003958 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Fri, 26 Mar 2010 12:41:22 -0400 Received: from localhost.localdomain (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx05.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o2QGfKZ9000593; Fri, 26 Mar 2010 12:41:20 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Fri, 26 Mar 2010 17:40:45 +0100 Message-Id: <1269621645-30954-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.18 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. Cc: kwolf@redhat.com Subject: [Qemu-devel] [PATCH] qcow2: Fix creation of large images X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org qcow_create2 assumes that the new image will only need one cluster for its refcount table initially. Obviously that's not true any more when the image is big enough (exact value depends on the cluster size). This patch calculates the refcount table size dynamically. Signed-off-by: Kevin Wolf --- block/qcow2.c | 41 +++++++++++++++++++++++++++++++---------- 1 files changed, 31 insertions(+), 10 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 5b6dad9..aca8844 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -839,7 +839,7 @@ static int qcow_create2(const char *filename, int64_t total_size, { int fd, header_size, backing_filename_len, l1_size, i, shift, l2_bits; - int ref_clusters, backing_format_len = 0; + int ref_clusters, reftable_clusters, backing_format_len = 0; int rounded_ext_bf_len = 0; QCowHeader header; uint64_t tmp, offset; @@ -901,17 +901,36 @@ static int qcow_create2(const char *filename, int64_t total_size, header.l1_size = cpu_to_be32(l1_size); offset += align_offset(l1_size * sizeof(uint64_t), s->cluster_size); - s->refcount_table = qemu_mallocz(s->cluster_size); + /* count how many refcount blocks needed */ + tmp = offset >> s->cluster_bits; + ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1; + + do { + uint64_t image_clusters; + tmp = ref_clusters; + + /* Number of clusters used for the refcount table */ + reftable_clusters = ((ref_clusters * sizeof(uint64_t)) + + s->cluster_size - 1) >> s->cluster_bits; + + /* Number of clusters that the whole image will have */ + image_clusters = (((offset + s->cluster_size - 1) >> s->cluster_bits) + + ref_clusters + reftable_clusters); + + /* Number of refcount blocks needed for the image */ + ref_clusters = + (image_clusters + (1 << (s->cluster_bits - REFCOUNT_SHIFT)) - 1) >> + (s->cluster_bits - REFCOUNT_SHIFT); + } while (ref_clusters != tmp); + + s->refcount_table = qemu_mallocz(reftable_clusters * s->cluster_size); s->refcount_table_offset = offset; header.refcount_table_offset = cpu_to_be64(offset); - header.refcount_table_clusters = cpu_to_be32(1); - offset += s->cluster_size; + header.refcount_table_clusters = cpu_to_be32(reftable_clusters); + offset += (reftable_clusters * s->cluster_size); s->refcount_block_offset = offset; - /* count how many refcount blocks needed */ - tmp = offset >> s->cluster_bits; - ref_clusters = (tmp >> (s->cluster_bits - REFCOUNT_SHIFT)) + 1; for (i=0; i < ref_clusters; i++) { s->refcount_table[i] = cpu_to_be64(offset); offset += s->cluster_size; @@ -923,7 +942,8 @@ static int qcow_create2(const char *filename, int64_t total_size, qcow2_create_refcount_update(s, 0, header_size); qcow2_create_refcount_update(s, s->l1_table_offset, l1_size * sizeof(uint64_t)); - qcow2_create_refcount_update(s, s->refcount_table_offset, s->cluster_size); + qcow2_create_refcount_update(s, s->refcount_table_offset, + reftable_clusters * s->cluster_size); qcow2_create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size); @@ -975,8 +995,9 @@ static int qcow_create2(const char *filename, int64_t total_size, } } lseek(fd, s->refcount_table_offset, SEEK_SET); - ret = qemu_write_full(fd, s->refcount_table, s->cluster_size); - if (ret != s->cluster_size) { + ret = qemu_write_full(fd, s->refcount_table, + reftable_clusters * s->cluster_size); + if (ret != reftable_clusters * s->cluster_size) { ret = -errno; goto exit; }