From patchwork Tue Jul 31 16:51:46 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Robert Wang X-Patchwork-Id: 174289 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 C87192C0094 for ; Wed, 1 Aug 2012 02:53:09 +1000 (EST) Received: from localhost ([::1]:34374 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwFgx-0002ln-Uj for incoming@patchwork.ozlabs.org; Tue, 31 Jul 2012 12:53:07 -0400 Received: from eggs.gnu.org ([208.118.235.92]:35290) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwFgK-0001Qt-IF for qemu-devel@nongnu.org; Tue, 31 Jul 2012 12:52:29 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SwFgJ-0004Ji-5L for qemu-devel@nongnu.org; Tue, 31 Jul 2012 12:52:28 -0400 Received: from mail-yw0-f45.google.com ([209.85.213.45]:39301) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SwFgJ-0004Jc-0J for qemu-devel@nongnu.org; Tue, 31 Jul 2012 12:52:27 -0400 Received: by yhpp34 with SMTP id p34so6071606yhp.4 for ; Tue, 31 Jul 2012 09:52:26 -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:x-mailer:in-reply-to :references; bh=/+0sz/JHb8SqOz1jzg0ThfdJKnNZDbrq/Abf/5d8pCo=; b=UX1BGh8E5QCTwpbTk3PgZ942TRd1lgSwUb8WrwC4OPsoARkorwYJNM3uCH1AqUOX59 90ivSER8u0RGc7a/i1aj0NaXfN6vtcN0ocfk1fBUhSv8x9koctdkzHePazmaJq3fJeMI 3ioSZMFj3zo4TPdrO0hKXCbI4ZXfJvVzG+1XbWVPJOG7gUBLlcEaw/GeUd3PtQf8Rr3L 7AEigh/gWbnVxIPnaNvd5Y0LZjwkAUOaC2BTi35dfp5hkR6l6WR+dRKWWRRUzordrCEH 2oeHAxtEfwKbhGcTS7stCoLOoz/pHqSVfy0e0N/K9sOkYVa2dDumX/WGv1sFj3kh/4Nj farw== Received: by 10.50.219.138 with SMTP id po10mr2553874igc.52.1343753546039; Tue, 31 Jul 2012 09:52:26 -0700 (PDT) Received: from localhost.localdomain ([202.108.130.138]) by mx.google.com with ESMTPS id uq6sm3025753igb.14.2012.07.31.09.52.23 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 31 Jul 2012 09:52:25 -0700 (PDT) From: Dong Xu Wang To: qemu-devel@nongnu.org Date: Wed, 1 Aug 2012 00:51:46 +0800 Message-Id: <1343753510-24661-2-git-send-email-wdongxu@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: <1343753510-24661-1-git-send-email-wdongxu@linux.vnet.ibm.com> References: <1343753510-24661-1-git-send-email-wdongxu@linux.vnet.ibm.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.213.45 Cc: kwolf@redhat.com, Dong Xu Wang Subject: [Qemu-devel] [PATCH 2/6 v11 v11] block: make some functions public 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 In add-cow file format, we will use path_has_protocol and we will read a NUL-terminated string from image , qed_read_string has done the samething, so make the two functions public, then we will reuse them directly. While creating images files, if no size is specified, will use size of backing file. If no backing file is specified, we will use the size of image file. Signed-off-by: Dong Xu Wang --- block.c | 37 +++++++++++++++++++++++++++++++++---- block.h | 3 +++ block/qed.c | 29 +---------------------------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/block.c b/block.c index b38940b..3b4d27f 100644 --- a/block.c +++ b/block.c @@ -196,7 +196,7 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs, } /* check if the path starts with ":" */ -static int path_has_protocol(const char *path) +int path_has_protocol(const char *path) { const char *p; @@ -213,6 +213,21 @@ static int path_has_protocol(const char *path) return *p == ':'; } +int bdrv_read_string(BlockDriverState *file, uint64_t offset, size_t n, + char *buf, size_t buflen) +{ + int ret; + if (n >= buflen) { + return -EINVAL; + } + ret = bdrv_pread(file, offset, buf, n); + if (ret < 0) { + return ret; + } + buf[n] = '\0'; + return 0; +} + int path_is_absolute(const char *path) { #ifdef _WIN32 @@ -3884,7 +3899,7 @@ int bdrv_img_create(const char *filename, const char *fmt, char *options, uint64_t img_size, int flags) { QEMUOptionParameter *param = NULL, *create_options = NULL; - QEMUOptionParameter *backing_fmt, *backing_file, *size; + QEMUOptionParameter *backing_fmt, *backing_file, *size, *image_file; BlockDriverState *bs = NULL; BlockDriver *drv, *proto_drv; BlockDriver *backing_drv = NULL; @@ -3965,9 +3980,11 @@ int bdrv_img_create(const char *filename, const char *fmt, } } - // The size for the image must always be specified, with one exception: - // If we are using a backing file, we can obtain the size from there + /* The size for the image must always be specified, with one exception: + If we are using a backing file, we can obtain the size from there, + but if not, and we are using an image file, we will obtain the size from it.*/ size = get_option_parameter(param, BLOCK_OPT_SIZE); + image_file = get_option_parameter(param, BLOCK_OPT_IMAGE_FILE); if (size && size->value.n == -1) { if (backing_file && backing_file->value.s) { uint64_t size; @@ -3990,6 +4007,18 @@ int bdrv_img_create(const char *filename, const char *fmt, snprintf(buf, sizeof(buf), "%" PRId64, size); set_option_parameter(param, BLOCK_OPT_SIZE, buf); + } else if (image_file && image_file->value.s) { + uint64_t size; + char buf[32]; + bs = bdrv_new(""); + ret = bdrv_file_open(&bs, image_file->value.s, BDRV_O_RDWR); + if (ret < 0) { + error_report("Could not open '%s'", image_file->value.s); + goto out; + } + size = bdrv_getlength(bs); + snprintf(buf, sizeof(buf), "%" PRId64, size); + set_option_parameter(param, BLOCK_OPT_SIZE, buf); } else { error_report("Image creation needs a size parameter"); ret = -EINVAL; diff --git a/block.h b/block.h index c89590d..b523076 100644 --- a/block.h +++ b/block.h @@ -152,6 +152,8 @@ int bdrv_pwrite(BlockDriverState *bs, int64_t offset, const void *buf, int count); int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, const void *buf, int count); +int bdrv_read_string(BlockDriverState *file, uint64_t offset, size_t n, + char *buf, size_t buflen); int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, @@ -306,6 +308,7 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn); char *get_human_readable_size(char *buf, int buf_size, int64_t size); int path_is_absolute(const char *path); +int path_has_protocol(const char *path); void path_combine(char *dest, int dest_size, const char *base_path, const char *filename); diff --git a/block/qed.c b/block/qed.c index 5f3eefa..311c589 100644 --- a/block/qed.c +++ b/block/qed.c @@ -217,33 +217,6 @@ static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size, } /** - * Read a string of known length from the image file - * - * @file: Image file - * @offset: File offset to start of string, in bytes - * @n: String length in bytes - * @buf: Destination buffer - * @buflen: Destination buffer length in bytes - * @ret: 0 on success, -errno on failure - * - * The string is NUL-terminated. - */ -static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n, - char *buf, size_t buflen) -{ - int ret; - if (n >= buflen) { - return -EINVAL; - } - ret = bdrv_pread(file, offset, buf, n); - if (ret < 0) { - return ret; - } - buf[n] = '\0'; - return 0; -} - -/** * Allocate new clusters * * @s: QED state @@ -437,7 +410,7 @@ static int bdrv_qed_open(BlockDriverState *bs, int flags) return -EINVAL; } - ret = qed_read_string(bs->file, s->header.backing_filename_offset, + ret = bdrv_read_string(bs->file, s->header.backing_filename_offset, s->header.backing_filename_size, bs->backing_file, sizeof(bs->backing_file)); if (ret < 0) {