From patchwork Wed Apr 27 13:27:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Hajnoczi X-Patchwork-Id: 93043 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 64F411007D7 for ; Wed, 27 Apr 2011 23:28:18 +1000 (EST) Received: from localhost ([::1]:42087 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QF4mt-00063i-MB for incoming@patchwork.ozlabs.org; Wed, 27 Apr 2011 09:28:15 -0400 Received: from eggs.gnu.org ([140.186.70.92]:58612) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QF4mP-00062h-M2 for qemu-devel@nongnu.org; Wed, 27 Apr 2011 09:27:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QF4mN-0005ao-QD for qemu-devel@nongnu.org; Wed, 27 Apr 2011 09:27:45 -0400 Received: from mtagate2.uk.ibm.com ([194.196.100.162]:40396) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QF4mN-0005ZI-IY for qemu-devel@nongnu.org; Wed, 27 Apr 2011 09:27:43 -0400 Received: from d06nrmr1806.portsmouth.uk.ibm.com (d06nrmr1806.portsmouth.uk.ibm.com [9.149.39.193]) by mtagate2.uk.ibm.com (8.13.1/8.13.1) with ESMTP id p3RDRgVY008523 for ; Wed, 27 Apr 2011 13:27:42 GMT Received: from d06av04.portsmouth.uk.ibm.com (d06av04.portsmouth.uk.ibm.com [9.149.37.216]) by d06nrmr1806.portsmouth.uk.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id p3RDSkZQ2007278 for ; Wed, 27 Apr 2011 14:28:46 +0100 Received: from d06av04.portsmouth.uk.ibm.com (loopback [127.0.0.1]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id p3RDRgPH031331 for ; Wed, 27 Apr 2011 07:27:42 -0600 Received: from stefanha-thinkpad.ibm.com (sig-9-145-204-45.de.ibm.com [9.145.204.45]) by d06av04.portsmouth.uk.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id p3RDRcjY031186; Wed, 27 Apr 2011 07:27:41 -0600 From: Stefan Hajnoczi To: Date: Wed, 27 Apr 2011 14:27:30 +0100 Message-Id: <1303910855-28999-4-git-send-email-stefanha@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.4.4 In-Reply-To: <1303910855-28999-1-git-send-email-stefanha@linux.vnet.ibm.com> References: <1303910855-28999-1-git-send-email-stefanha@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6, seldom 2.4 (older, 4) X-Received-From: 194.196.100.162 Cc: Kevin Wolf , Anthony Liguori Subject: [Qemu-devel] [PATCH 3/8] qed: add support for Copy-on-Read 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 From: Anthony Liguori When creating an image using qemu-img, just pass '-o copy_on_read' and then whenever QED reads from a backing file, it will write the block to the QED file after the read completes ensuring that you only fetch from the backing device once. This is very useful for streaming images over a slow connection. Signed-off-by: Anthony Liguori --- block/qed.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- block/qed.h | 15 +++++++++++---- 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/block/qed.c b/block/qed.c index c8c5930..7487683 100644 --- a/block/qed.c +++ b/block/qed.c @@ -448,7 +448,8 @@ static int bdrv_qed_flush(BlockDriverState *bs) static int qed_create(const char *filename, uint32_t cluster_size, uint64_t image_size, uint32_t table_size, - const char *backing_file, const char *backing_fmt) + const char *backing_file, const char *backing_fmt, + bool copy_on_read) { QEDHeader header = { .magic = QED_MAGIC, @@ -490,6 +491,9 @@ static int qed_create(const char *filename, uint32_t cluster_size, if (qed_fmt_is_raw(backing_fmt)) { header.features |= QED_F_BACKING_FORMAT_NO_PROBE; } + if (copy_on_read) { + header.compat_features |= QED_CF_COPY_ON_READ; + } } qed_header_cpu_to_le(&header, &le_header); @@ -523,6 +527,7 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) uint32_t table_size = QED_DEFAULT_TABLE_SIZE; const char *backing_file = NULL; const char *backing_fmt = NULL; + bool copy_on_read = false; while (options && options->name) { if (!strcmp(options->name, BLOCK_OPT_SIZE)) { @@ -539,6 +544,10 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) if (options->value.n) { table_size = options->value.n; } + } else if (!strcmp(options->name, "copy_on_read")) { + if (options->value.n) { + copy_on_read = true; + } } options++; } @@ -559,9 +568,14 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) qed_max_image_size(cluster_size, table_size)); return -EINVAL; } + if (copy_on_read && !backing_file) { + fprintf(stderr, + "QED only supports Copy-on-Read with a backing file\n"); + return -EINVAL; + } return qed_create(filename, cluster_size, image_size, table_size, - backing_file, backing_fmt); + backing_file, backing_fmt, copy_on_read); } typedef struct { @@ -1092,6 +1106,27 @@ static void qed_aio_write_data(void *opaque, int ret, } /** + * Copy on read callback + * + * Write data from backing file to QED that's been read if CoR is enabled. + */ +static void qed_copy_on_read_cb(void *opaque, int ret) +{ + QEDAIOCB *acb = opaque; + BDRVQEDState *s = acb_to_s(acb); + BlockDriverAIOCB *cor_acb; + + cor_acb = bdrv_aio_writev(s->bs, + acb->cur_pos / BDRV_SECTOR_SIZE, + &acb->cur_qiov, + acb->cur_qiov.size / BDRV_SECTOR_SIZE, + qed_aio_next_io, acb); + if (!cor_acb) { + qed_aio_complete(acb, -EIO); + } +} + +/** * Read data cluster * * @opaque: Read request @@ -1127,8 +1162,14 @@ static void qed_aio_read_data(void *opaque, int ret, qed_aio_next_io(acb, 0); return; } else if (ret != QED_CLUSTER_FOUND) { + BlockDriverCompletionFunc *cb = qed_aio_next_io; + + if (bs->backing_hd && + (s->header.compat_features & QED_CF_COPY_ON_READ)) { + cb = qed_copy_on_read_cb; + } qed_read_backing_file(s, acb->cur_pos, &acb->cur_qiov, - qed_aio_next_io, acb); + cb, acb); return; } @@ -1349,6 +1390,10 @@ static QEMUOptionParameter qed_create_options[] = { .name = BLOCK_OPT_TABLE_SIZE, .type = OPT_SIZE, .help = "L1/L2 table size (in clusters)" + }, { + .name = "copy_on_read", + .type = OPT_FLAG, + .help = "Copy blocks from base image on read" }, { /* end of list */ } }; diff --git a/block/qed.h b/block/qed.h index 3e1ab84..845a80e 100644 --- a/block/qed.h +++ b/block/qed.h @@ -56,12 +56,19 @@ enum { /* The backing file format must not be probed, treat as raw image */ QED_F_BACKING_FORMAT_NO_PROBE = 0x04, - /* Feature bits must be used when the on-disk format changes */ - QED_FEATURE_MASK = QED_F_BACKING_FILE | /* supported feature bits */ + /* Reads to the backing file should populate the image file */ + QED_CF_COPY_ON_READ = 0x01, + + /* Supported feature bits */ + QED_FEATURE_MASK = QED_F_BACKING_FILE | QED_F_NEED_CHECK | QED_F_BACKING_FORMAT_NO_PROBE, - QED_COMPAT_FEATURE_MASK = 0, /* supported compat feature bits */ - QED_AUTOCLEAR_FEATURE_MASK = 0, /* supported autoclear feature bits */ + + /* Supported compat feature bits */ + QED_COMPAT_FEATURE_MASK = QED_CF_COPY_ON_READ, + + /* Supported autoclear feature bits */ + QED_AUTOCLEAR_FEATURE_MASK = 0, /* Data is stored in groups of sectors called clusters. Cluster size must * be large to avoid keeping too much metadata. I/O requests that have