From patchwork Wed Apr 28 09:24:15 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 51153 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 B7FCAB7D57 for ; Wed, 28 Apr 2010 19:36:49 +1000 (EST) Received: from localhost ([127.0.0.1]:35177 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O73hG-0008B9-ST for incoming@patchwork.ozlabs.org; Wed, 28 Apr 2010 05:36:46 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1O73dZ-0006pY-ER for qemu-devel@nongnu.org; Wed, 28 Apr 2010 05:32:57 -0400 Received: from [140.186.70.92] (port=37009 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1O73dV-0005Wg-6X for qemu-devel@nongnu.org; Wed, 28 Apr 2010 05:32:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1O73VF-0001WP-NZ for qemu-devel@nongnu.org; Wed, 28 Apr 2010 05:24:23 -0400 Received: from mtagate7.uk.ibm.com ([194.196.100.167]:51769) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1O73VF-0001Vw-Fm for qemu-devel@nongnu.org; Wed, 28 Apr 2010 05:24:21 -0400 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate7.uk.ibm.com (8.13.1/8.13.1) with ESMTP id o3S9OJTP011584 for ; Wed, 28 Apr 2010 09:24:19 GMT Received: from d06av03.portsmouth.uk.ibm.com (d06av03.portsmouth.uk.ibm.com [9.149.37.213]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o3S9OJ2j1581188 for ; Wed, 28 Apr 2010 10:24:19 +0100 Received: from d06av03.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.13.3) with ESMTP id o3S9OItI022205 for ; Wed, 28 Apr 2010 10:24:18 +0100 Received: from localhost.localdomain (dyn-9-174-219-79.manchester-maybrook.uk.ibm.com [9.174.219.79]) by d06av03.portsmouth.uk.ibm.com (8.12.11.20060308/8.12.11) with ESMTP id o3S9OIil022184; Wed, 28 Apr 2010 10:24:18 +0100 From: Stefan Hajnoczi To: Date: Wed, 28 Apr 2010 10:24:15 +0100 Message-Id: <1272446655-4810-1-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) Cc: qemu-devel@nongnu.org, Stefan Hajnoczi Subject: [Qemu-devel] [PATCH v2] qcow2: Implement bdrv_truncate() for growing 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 This patch adds the ability to grow qcow2 images in-place using bdrv_truncate(). This enables qemu-img resize command support for qcow2. Snapshots are not supported and bdrv_truncate() will return -ENOTSUP. The notion of resizing an image with snapshots could lead to confusion: users may expect snapshots to remain unchanged, but this is not possible with the current qcow2 on-disk format where the header.size field is global instead of per-snapshot. Others may expect snapshots to change size along with the current image data. I think it is safest to not support snapshots and perhaps add behavior later if there is a consensus. Backing images continue to work. If the image is now larger than its backing image, zeroes are read when accessing beyond the end of the backing image. Signed-off-by: Stefan Hajnoczi --- v2: * Remove superfluous qcow2_grow_l1_table()-related changes block/qcow2.c | 45 +++++++++++++++++++++++++++++++++++++++++---- block/qcow2.h | 6 ++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/block/qcow2.c b/block/qcow2.c index 4fa3ff9..a65af41 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -140,7 +140,7 @@ static int qcow_read_extensions(BlockDriverState *bs, uint64_t start_offset, static int qcow_open(BlockDriverState *bs, int flags) { BDRVQcowState *s = bs->opaque; - int len, i, shift; + int len, i; QCowHeader header; uint64_t ext_end; @@ -188,8 +188,7 @@ static int qcow_open(BlockDriverState *bs, int flags) /* read the level 1 table */ s->l1_size = header.l1_size; - shift = s->cluster_bits + s->l2_bits; - s->l1_vm_state_index = (header.size + (1LL << shift) - 1) >> shift; + s->l1_vm_state_index = size_to_l1(s, header.size); /* the L1 table must contain at least enough entries to put header.size bytes */ if (s->l1_size < s->l1_vm_state_index) @@ -851,6 +850,42 @@ static int qcow_make_empty(BlockDriverState *bs) return 0; } +static int qcow2_truncate(BlockDriverState *bs, int64_t offset) +{ + BDRVQcowState *s = bs->opaque; + int ret, new_l1_size; + + if (offset & 511) { + return -EINVAL; + } + + /* cannot proceed if image has snapshots */ + if (s->nb_snapshots) { + return -ENOTSUP; + } + + /* shrinking is currently not supported */ + if (offset < bs->total_sectors * 512) { + return -ENOTSUP; + } + + new_l1_size = size_to_l1(s, offset); + ret = qcow2_grow_l1_table(bs, new_l1_size); + if (ret < 0) { + return ret; + } + + /* write updated header.size */ + offset = cpu_to_be64(offset); + if (bdrv_pwrite(bs->file, offsetof(QCowHeader, size), &offset, + sizeof(uint64_t)) != sizeof(uint64_t)) { + return -EIO; + } + + s->l1_vm_state_index = new_l1_size; + return 0; +} + /* XXX: put compressed sectors first, then all the cluster aligned tables to avoid losing bytes in alignment */ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num, @@ -1050,7 +1085,9 @@ static BlockDriver bdrv_qcow2 = { .bdrv_aio_readv = qcow_aio_readv, .bdrv_aio_writev = qcow_aio_writev, .bdrv_aio_flush = qcow_aio_flush, - .bdrv_write_compressed = qcow_write_compressed, + + .bdrv_truncate = qcow2_truncate, + .bdrv_write_compressed = qcow_write_compressed, .bdrv_snapshot_create = qcow2_snapshot_create, .bdrv_snapshot_goto = qcow2_snapshot_goto, diff --git a/block/qcow2.h b/block/qcow2.h index 5bd08db..01053b7 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -150,6 +150,12 @@ static inline int size_to_clusters(BDRVQcowState *s, int64_t size) return (size + (s->cluster_size - 1)) >> s->cluster_bits; } +static inline int size_to_l1(BDRVQcowState *s, int64_t size) +{ + int shift = s->cluster_bits + s->l2_bits; + return (size + (1ULL << shift) - 1) >> shift; +} + static inline int64_t align_offset(int64_t offset, int n) { offset = (offset + n - 1) & ~(n - 1);