From patchwork Tue Sep 18 11:40:28 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Wolf X-Patchwork-Id: 184703 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id A9E2A2C008A for ; Tue, 18 Sep 2012 22:40:29 +1000 (EST) Received: from localhost ([::1]:32865 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDwBM-00006x-S2 for incoming@patchwork.ozlabs.org; Tue, 18 Sep 2012 07:41:36 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35402) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDwAj-00077c-9X for qemu-devel@nongnu.org; Tue, 18 Sep 2012 07:41:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TDwAa-0005Gq-OF for qemu-devel@nongnu.org; Tue, 18 Sep 2012 07:40:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:42950) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TDwAa-0005GY-Fe for qemu-devel@nongnu.org; Tue, 18 Sep 2012 07:40:48 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q8IBekHS021693 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 18 Sep 2012 07:40:47 -0400 Received: from dhcp-5-188.str.redhat.com (dhcp-5-175.str.redhat.com [10.32.5.175]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q8IBegGM028821; Tue, 18 Sep 2012 07:40:45 -0400 From: Kevin Wolf To: qemu-devel@nongnu.org Date: Tue, 18 Sep 2012 13:40:28 +0200 Message-Id: <1347968442-8860-3-git-send-email-kwolf@redhat.com> In-Reply-To: <1347968442-8860-1-git-send-email-kwolf@redhat.com> References: <1347968442-8860-1-git-send-email-kwolf@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com Subject: [Qemu-devel] [RFC PATCH 02/16] qcow2: Introduce Qcow2COWRegion X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org This makes it easier to address the areas for which a COW must be performed. As a nice side effect, the COW code in qcow2_alloc_cluster_link_l2 becomes really trivial. Signed-off-by: Kevin Wolf --- block/qcow2-cluster.c | 85 +++++++++++++++++++++++++++++++------------------ block/qcow2.h | 29 +++++++++++++--- 2 files changed, 77 insertions(+), 37 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index d17a37c..94b7f13 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -615,13 +615,41 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, return cluster_offset; } +static int perform_cow(BlockDriverState *bs, QCowL2Meta *m, Qcow2COWRegion *r) +{ + BDRVQcowState *s = bs->opaque; + int ret; + + if (r->nb_sectors == 0) { + return 0; + } + + qemu_co_mutex_unlock(&s->lock); + ret = copy_sectors(bs, m->offset / BDRV_SECTOR_SIZE, m->alloc_offset, + r->offset / BDRV_SECTOR_SIZE, + r->offset / BDRV_SECTOR_SIZE + r->nb_sectors); + qemu_co_mutex_lock(&s->lock); + + if (ret < 0) { + return ret; + } + + /* + * Before we update the L2 table to actually point to the new cluster, we + * need to be sure that the refcounts have been increased and COW was + * handled. + */ + qcow2_cache_depends_on_flush(s->l2_table_cache); + + return 0; +} + int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) { BDRVQcowState *s = bs->opaque; int i, j = 0, l2_index, ret; - uint64_t *old_cluster, start_sect, *l2_table; + uint64_t *old_cluster, *l2_table; uint64_t cluster_offset = m->alloc_offset; - bool cow = false; trace_qcow2_cluster_link_l2(qemu_coroutine_self(), m->nb_clusters); @@ -631,36 +659,17 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) old_cluster = g_malloc(m->nb_clusters * sizeof(uint64_t)); /* copy content of unmodified sectors */ - start_sect = m->offset >> 9; - if (m->n_start) { - cow = true; - qemu_co_mutex_unlock(&s->lock); - ret = copy_sectors(bs, start_sect, cluster_offset, 0, m->n_start); - qemu_co_mutex_lock(&s->lock); - if (ret < 0) - goto err; + ret = perform_cow(bs, m, &m->cow_start); + if (ret < 0) { + goto err; } - if (m->nb_available & (s->cluster_sectors - 1)) { - cow = true; - qemu_co_mutex_unlock(&s->lock); - ret = copy_sectors(bs, start_sect, cluster_offset, m->nb_available, - align_offset(m->nb_available, s->cluster_sectors)); - qemu_co_mutex_lock(&s->lock); - if (ret < 0) - goto err; + ret = perform_cow(bs, m, &m->cow_end); + if (ret < 0) { + goto err; } - /* - * Update L2 table. - * - * Before we update the L2 table to actually point to the new cluster, we - * need to be sure that the refcounts have been increased and COW was - * handled. - */ - if (cow) { - qcow2_cache_depends_on_flush(s->l2_table_cache); - } + /* Update L2 table. */ if (qcow2_need_accurate_refcounts(s)) { qcow2_cache_set_dependency(bs, s->l2_table_cache, @@ -957,19 +966,33 @@ again: * * avail_sectors: Number of sectors from the start of the first * newly allocated to the end of the last newly allocated cluster. + * + * nb_sectors: The number of sectors from the start of the first + * newly allocated cluster to the end of the aread that the write + * request actually writes to (excluding COW at the end) */ int requested_sectors = n_end - keep_clusters * s->cluster_sectors; int avail_sectors = nb_clusters << (s->cluster_bits - BDRV_SECTOR_BITS); + int alloc_n_start = keep_clusters == 0 ? n_start : 0; + int nb_sectors = MIN(requested_sectors, avail_sectors); *m = (QCowL2Meta) { .cluster_offset = keep_clusters == 0 ? alloc_cluster_offset : cluster_offset, .alloc_offset = alloc_cluster_offset, .offset = alloc_offset & ~(s->cluster_size - 1), - .n_start = keep_clusters == 0 ? n_start : 0, .nb_clusters = nb_clusters, - .nb_available = MIN(requested_sectors, avail_sectors), + .nb_available = nb_sectors, + + .cow_start = { + .offset = 0, + .nb_sectors = alloc_n_start, + }, + .cow_end = { + .offset = nb_sectors * BDRV_SECTOR_SIZE, + .nb_sectors = avail_sectors - nb_sectors, + }, }; qemu_co_queue_init(&m->dependent_requests); QLIST_INSERT_HEAD(&s->cluster_allocs, m, next_in_flight); diff --git a/block/qcow2.h b/block/qcow2.h index 2a406a7..1106b33 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -196,6 +196,17 @@ typedef struct QCowCreateState { struct QCowAIOCB; +typedef struct Qcow2COWRegion { + /** + * Offset of the COW region in bytes from the start of the first cluster + * touched by the request. + */ + uint64_t offset; + + /** Number of sectors to copy */ + int nb_sectors; +} Qcow2COWRegion; + /* XXX This could be private for qcow2-cluster.c */ typedef struct QCowL2Meta { @@ -209,12 +220,6 @@ typedef struct QCowL2Meta uint64_t alloc_offset; /** - * Number of sectors between the start of the first allocated cluster and - * the area that the guest actually writes to. - */ - int n_start; - - /** * Number of sectors from the start of the first allocated cluster to * the end of the (possibly shortened) request */ @@ -229,6 +234,18 @@ typedef struct QCowL2Meta */ CoQueue dependent_requests; + /** + * The COW Region between the start of the first allocated cluster and the + * area the guest actually writes to. + */ + Qcow2COWRegion cow_start; + + /** + * The COW Region between the area the guest actually writes to and the + * end of the last allocated cluster. + */ + Qcow2COWRegion cow_end; + QLIST_ENTRY(QCowL2Meta) next_in_flight; } QCowL2Meta;