From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([140.186.70.92]:60267) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RCCh1-0004TE-FQ for qemu-devel@nongnu.org; Fri, 07 Oct 2011 11:50:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RCCgy-0001Lv-3f for qemu-devel@nongnu.org; Fri, 07 Oct 2011 11:50:35 -0400 Received: from mtagate1.uk.ibm.com ([194.196.100.161]:41687) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RCCgx-0001Cr-SM for qemu-devel@nongnu.org; Fri, 07 Oct 2011 11:50:32 -0400 Received: from d06nrmr1707.portsmouth.uk.ibm.com (d06nrmr1707.portsmouth.uk.ibm.com [9.149.39.225]) by mtagate1.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p97Fnt9s022261 for ; Fri, 7 Oct 2011 15:49:55 GMT Received: from d06av09.portsmouth.uk.ibm.com (d06av09.portsmouth.uk.ibm.com [9.149.37.250]) by d06nrmr1707.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p97FntFS2449430 for ; Fri, 7 Oct 2011 16:49:55 +0100 Received: from d06av09.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av09.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p97Fns78019759 for ; Fri, 7 Oct 2011 09:49:55 -0600 From: Stefan Hajnoczi Date: Fri, 7 Oct 2011 16:49:48 +0100 Message-Id: <1318002589-11315-3-git-send-email-stefanha@linux.vnet.ibm.com> In-Reply-To: <1318002589-11315-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1318002589-11315-1-git-send-email-stefanha@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 2/3] qed: add zero write detection support List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Anthony Liguori , Marcelo Tosatti , Stefan Hajnoczi The QED image format is able to efficiently represent clusters containing zeroes with a magic offset value. This patch implements zero write detection for allocating writes so that image streaming can copy over zero clusters from a backing file without expanding the image file unnecessarily. This is based code by Anthony Liguori . Signed-off-by: Stefan Hajnoczi --- block/qed.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 files changed, 73 insertions(+), 8 deletions(-) diff --git a/block/qed.c b/block/qed.c index e87dc4d..ec3113b 100644 --- a/block/qed.c +++ b/block/qed.c @@ -947,9 +947,8 @@ static void qed_aio_write_l1_update(void *opaque, int ret) /** * Update L2 table with new cluster offsets and write them out */ -static void qed_aio_write_l2_update(void *opaque, int ret) +static void qed_aio_write_l2_update(QEDAIOCB *acb, int ret, uint64_t offset) { - QEDAIOCB *acb = opaque; BDRVQEDState *s = acb_to_s(acb); bool need_alloc = acb->find_cluster_ret == QED_CLUSTER_L1; int index; @@ -965,7 +964,7 @@ static void qed_aio_write_l2_update(void *opaque, int ret) index = qed_l2_index(s, acb->cur_pos); qed_update_l2_table(s, acb->request.l2_table->table, index, acb->cur_nclusters, - acb->cur_cluster); + offset); if (need_alloc) { /* Write out the whole new L2 table */ @@ -982,6 +981,51 @@ err: qed_aio_complete(acb, ret); } +static void qed_aio_write_l2_update_cb(void *opaque, int ret) +{ + QEDAIOCB *acb = opaque; + qed_aio_write_l2_update(acb, ret, acb->cur_cluster); +} + +/** + * Determine if we have a zero write to a block of clusters + * + * We validate that the write is aligned to a cluster boundary, and that it's + * a multiple of cluster size with all zeros. + */ +static bool qed_is_zero_write(QEDAIOCB *acb) +{ + BDRVQEDState *s = acb_to_s(acb); + int i; + + if (!qed_offset_is_cluster_aligned(s, acb->cur_pos)) { + return false; + } + + if (!qed_offset_is_cluster_aligned(s, acb->cur_qiov.size)) { + return false; + } + + for (i = 0; i < acb->cur_qiov.niov; i++) { + struct iovec *iov = &acb->cur_qiov.iov[i]; + uint64_t *v; + int j; + + if ((iov->iov_len & 0x07)) { + return false; + } + + v = iov->iov_base; + for (j = 0; j < iov->iov_len; j += sizeof(v[0])) { + if (v[j >> 3]) { + return false; + } + } + } + + return true; +} + /** * Flush new data clusters before updating the L2 table * @@ -996,7 +1040,7 @@ static void qed_aio_write_flush_before_l2_update(void *opaque, int ret) QEDAIOCB *acb = opaque; BDRVQEDState *s = acb_to_s(acb); - if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update, opaque)) { + if (!bdrv_aio_flush(s->bs->file, qed_aio_write_l2_update_cb, opaque)) { qed_aio_complete(acb, -EIO); } } @@ -1026,7 +1070,7 @@ static void qed_aio_write_main(void *opaque, int ret) if (s->bs->backing_hd) { next_fn = qed_aio_write_flush_before_l2_update; } else { - next_fn = qed_aio_write_l2_update; + next_fn = qed_aio_write_l2_update_cb; } } @@ -1092,6 +1136,18 @@ static bool qed_should_set_need_check(BDRVQEDState *s) return !(s->header.features & QED_F_NEED_CHECK); } +static void qed_aio_write_zero_cluster(void *opaque, int ret) +{ + QEDAIOCB *acb = opaque; + + if (ret) { + qed_aio_complete(acb, ret); + return; + } + + qed_aio_write_l2_update(acb, 0, 1); +} + /** * Write new data cluster * @@ -1103,6 +1159,7 @@ static bool qed_should_set_need_check(BDRVQEDState *s) static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) { BDRVQEDState *s = acb_to_s(acb); + BlockDriverCompletionFunc *cb; /* Cancel timer when the first allocating request comes in */ if (QSIMPLEQ_EMPTY(&s->allocating_write_reqs)) { @@ -1120,14 +1177,21 @@ static void qed_aio_write_alloc(QEDAIOCB *acb, size_t len) acb->cur_nclusters = qed_bytes_to_clusters(s, qed_offset_into_cluster(s, acb->cur_pos) + len); - acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); qemu_iovec_copy(&acb->cur_qiov, acb->qiov, acb->qiov_offset, len); + /* Zero write detection */ + if (s->bs->use_zero_detection && qed_is_zero_write(acb)) { + cb = qed_aio_write_zero_cluster; + } else { + cb = qed_aio_write_prefill; + acb->cur_cluster = qed_alloc_clusters(s, acb->cur_nclusters); + } + if (qed_should_set_need_check(s)) { s->header.features |= QED_F_NEED_CHECK; - qed_write_header(s, qed_aio_write_prefill, acb); + qed_write_header(s, cb, acb); } else { - qed_aio_write_prefill(acb, 0); + cb(acb, 0); } } @@ -1474,6 +1538,7 @@ static BlockDriver bdrv_qed = { .format_name = "qed", .instance_size = sizeof(BDRVQEDState), .create_options = qed_create_options, + .has_zero_detection = true, .bdrv_probe = bdrv_qed_probe, .bdrv_open = bdrv_qed_open, -- 1.7.6.3