From patchwork Tue Aug 13 04:31:53 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 266715 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (unknown [IPv6:2001:4830:134:3::12]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 34D522C00E1 for ; Tue, 13 Aug 2013 15:25:17 +1000 (EST) Received: from localhost ([::1]:54570 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96Lw-0001qg-6Y for incoming@patchwork.ozlabs.org; Tue, 13 Aug 2013 00:37:04 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55001) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96IZ-0007QK-Vv for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V96IQ-0000Si-Su for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:35 -0400 Received: from mail-pd0-x22b.google.com ([2607:f8b0:400e:c02::22b]:55334) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V96IQ-0000SD-Iu for qemu-devel@nongnu.org; Tue, 13 Aug 2013 00:33:26 -0400 Received: by mail-pd0-f171.google.com with SMTP id g10so4328539pdj.30 for ; Mon, 12 Aug 2013 21:33:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references; bh=IzRDLeZ8g7hKXGPRFWDYoxG+feGN0dJbdWtj91GVunw=; b=VJGt+3rWseKowYuxVo/RniBQ9hNRFgaoR3TbMLfrKFR/76F5OBcPKEDh1e9Sbht9t9 oX9Buv7yOCO1UltuA6WD0uK3xqR3LsI6pcRUPkfXcq8mGWN0wbjMcDAhtkyyM9lUBCok Ty5T3hPYU5cGtb09l3GmkjsgIm3tCdulntZXgUHPANV68+0fVavtIz9bb7L/uVPeAzf/ g2nv7wjbZGZeJDVK6yqPc7XHpO4MyFGP+ta5xhZ2ew7yOPkErxFyoQPtI6waOCeR6f/a Q7dykYXvNnUYDlsttlvSAo7HV/noXKAvGSq65hX6LkIj2RAPWJjuKAqG0ay579d2pnTa UlRw== X-Received: by 10.66.171.204 with SMTP id aw12mr1777344pac.7.1376368405801; Mon, 12 Aug 2013 21:33:25 -0700 (PDT) Received: from 11.wdongxu.kvm58 ([202.108.130.153]) by mx.google.com with ESMTPSA id mz5sm10285023pbc.18.2013.08.12.21.33.21 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Mon, 12 Aug 2013 21:33:25 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Tue, 13 Aug 2013 12:31:53 +0800 Message-Id: <1376368326-7433-13-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1376368326-7433-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c02::22b Cc: kwolf@redhat.com, wdongxu@cn.ibm.com, stefanha@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH V18 12/25] block: add QemuOpts support for qed.c 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 Signed-off-by: Dong Xu Wang --- block/qed.c | 153 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ block/qed.h | 3 +- 2 files changed, 155 insertions(+), 1 deletion(-) diff --git a/block/qed.c b/block/qed.c index f767b05..348695f 100644 --- a/block/qed.c +++ b/block/qed.c @@ -651,6 +651,123 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) backing_file, backing_fmt); } +static int qed_create_new(const char *filename, uint32_t cluster_size, + uint64_t image_size, uint32_t table_size, + const char *backing_file, const char *backing_fmt) +{ + QEDHeader header = { + .magic = QED_MAGIC, + .cluster_size = cluster_size, + .table_size = table_size, + .header_size = 1, + .features = 0, + .compat_features = 0, + .l1_table_offset = cluster_size, + .image_size = image_size, + }; + QEDHeader le_header; + uint8_t *l1_table = NULL; + size_t l1_size = header.cluster_size * header.table_size; + int ret = 0; + BlockDriverState *bs = NULL; + + ret = bdrv_create_file_new(filename, NULL); + if (ret < 0) { + goto finish; + } + + ret = bdrv_file_open(&bs, filename, NULL, BDRV_O_RDWR | BDRV_O_CACHE_WB); + if (ret < 0) { + goto finish; + } + + /* File must start empty and grow, check truncate is supported */ + ret = bdrv_truncate(bs, 0); + if (ret < 0) { + goto out; + } + + if (backing_file) { + header.features |= QED_F_BACKING_FILE; + header.backing_filename_offset = sizeof(le_header); + header.backing_filename_size = strlen(backing_file); + + if (qed_fmt_is_raw(backing_fmt)) { + header.features |= QED_F_BACKING_FORMAT_NO_PROBE; + } + } + + qed_header_cpu_to_le(&header, &le_header); + ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header)); + if (ret < 0) { + goto out; + } + ret = bdrv_pwrite(bs, sizeof(le_header), backing_file, + header.backing_filename_size); + if (ret < 0) { + goto out; + } + + l1_table = g_malloc0(l1_size); + ret = bdrv_pwrite(bs, header.l1_table_offset, l1_table, l1_size); + if (ret < 0) { + goto out; + } + + ret = 0; /* success */ +out: + g_free(l1_table); + bdrv_delete(bs); +finish: + return ret; +} + +static int bdrv_qed_create_new(const char *filename, QemuOpts *opts) +{ + uint64_t image_size = 0; + uint32_t cluster_size = QED_DEFAULT_CLUSTER_SIZE; + uint32_t table_size = QED_DEFAULT_TABLE_SIZE; + char *backing_file = NULL; + char *backing_fmt = NULL; + int ret = 0; + + image_size = qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0); + backing_file = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FILE); + backing_fmt = qemu_opt_get_del(opts, BLOCK_OPT_BACKING_FMT); + cluster_size = qemu_opt_get_size_del(opts, + BLOCK_OPT_CLUSTER_SIZE, + QED_DEFAULT_CLUSTER_SIZE); + table_size = qemu_opt_get_size_del(opts, BLOCK_OPT_TABLE_SIZE, + QED_DEFAULT_TABLE_SIZE); + + if (!qed_is_cluster_size_valid(cluster_size)) { + fprintf(stderr, "QED cluster size must be within range [%u, %u] and power of 2\n", + QED_MIN_CLUSTER_SIZE, QED_MAX_CLUSTER_SIZE); + ret = -EINVAL; + goto finish; + } + if (!qed_is_table_size_valid(table_size)) { + fprintf(stderr, "QED table size must be within range [%u, %u] and power of 2\n", + QED_MIN_TABLE_SIZE, QED_MAX_TABLE_SIZE); + ret = -EINVAL; + goto finish; + } + if (!qed_is_image_size_valid(image_size, cluster_size, table_size)) { + fprintf(stderr, "QED image size must be a non-zero multiple of " + "cluster size and less than %" PRIu64 " bytes\n", + qed_max_image_size(cluster_size, table_size)); + ret = -EINVAL; + goto finish; + } + + ret = qed_create_new(filename, cluster_size, image_size, table_size, + backing_file, backing_fmt); + +finish: + g_free(backing_file); + g_free(backing_fmt); + return ret; +} typedef struct { Coroutine *co; int is_allocated; @@ -1563,10 +1680,45 @@ static QEMUOptionParameter qed_create_options[] = { { /* end of list */ } }; +static QemuOptsList qed_create_opts = { + .name = "qed-create-opts", + .head = QTAILQ_HEAD_INITIALIZER(qed_create_opts.head), + .desc = { + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, + { + .name = BLOCK_OPT_BACKING_FILE, + .type = QEMU_OPT_STRING, + .help = "File name of a base image" + }, + { + .name = BLOCK_OPT_BACKING_FMT, + .type = QEMU_OPT_STRING, + .help = "Image format of the base image" + }, + { + .name = BLOCK_OPT_CLUSTER_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Cluster size (in bytes)", + .def_value_str = stringify(QED_DEFAULT_CLUSTER_SIZE), + }, + { + .name = BLOCK_OPT_TABLE_SIZE, + .type = QEMU_OPT_SIZE, + .help = "L1/L2 table size (in clusters)" + }, + { /* end of list */ } + } +}; + static BlockDriver bdrv_qed = { .format_name = "qed", .instance_size = sizeof(BDRVQEDState), .create_options = qed_create_options, + .bdrv_create_opts = &qed_create_opts, .bdrv_probe = bdrv_qed_probe, .bdrv_rebind = bdrv_qed_rebind, @@ -1574,6 +1726,7 @@ static BlockDriver bdrv_qed = { .bdrv_close = bdrv_qed_close, .bdrv_reopen_prepare = bdrv_qed_reopen_prepare, .bdrv_create = bdrv_qed_create, + .bdrv_create_new = bdrv_qed_create_new, .bdrv_has_zero_init = bdrv_has_zero_init_1, .bdrv_co_is_allocated = bdrv_qed_co_is_allocated, .bdrv_make_empty = bdrv_qed_make_empty, diff --git a/block/qed.h b/block/qed.h index 2b4dded..29e1619 100644 --- a/block/qed.h +++ b/block/qed.h @@ -44,6 +44,8 @@ * All fields are little-endian on disk. */ +#define QED_DEFAULT_CLUSTER_SIZE 65536 + enum { QED_MAGIC = 'Q' | 'E' << 8 | 'D' << 16 | '\0' << 24, @@ -69,7 +71,6 @@ enum { */ QED_MIN_CLUSTER_SIZE = 4 * 1024, /* in bytes */ QED_MAX_CLUSTER_SIZE = 64 * 1024 * 1024, - QED_DEFAULT_CLUSTER_SIZE = 64 * 1024, /* Allocated clusters are tracked using a 2-level pagetable. Table size is * a multiple of clusters so large maximum image sizes can be supported