qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, Dong Xu Wang <wdongxu@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH 2/6 v11 v11] block: make some functions public
Date: Wed,  1 Aug 2012 00:51:46 +0800	[thread overview]
Message-ID: <1343753510-24661-2-git-send-email-wdongxu@linux.vnet.ibm.com> (raw)
In-Reply-To: <1343753510-24661-1-git-send-email-wdongxu@linux.vnet.ibm.com>

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 <wdongxu@linux.vnet.ibm.com>
---
 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 "<protocol>:" */
-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) {
-- 
1.7.1

  reply	other threads:[~2012-07-31 16:52 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-31 16:51 [Qemu-devel] [PATCH 1/6 v11] docs: spec for add-cow file format Dong Xu Wang
2012-07-31 16:51 ` Dong Xu Wang [this message]
2012-08-01 13:53   ` [Qemu-devel] [PATCH 2/6 v11 v11] block: make some functions public Eric Blake
2012-08-02  7:10     ` Dong Xu Wang
2012-08-01 14:01   ` Stefan Hajnoczi
2012-08-02  7:11     ` Dong Xu Wang
2012-07-31 16:51 ` [Qemu-devel] [PATCH 3/6] add-cow file format Dong Xu Wang
2012-08-01 13:57   ` Eric Blake
2012-08-01 14:14     ` Stefan Hajnoczi
2012-08-01 14:21       ` Kevin Wolf
2012-08-02  7:20     ` Dong Xu Wang
2012-08-01 15:31   ` Stefan Hajnoczi
2012-08-02  7:20     ` Dong Xu Wang
2012-07-31 16:51 ` [Qemu-devel] [PATCH 4/6 v11] add-cow: support snapshot_blkde Dong Xu Wang
2012-08-01 15:37   ` Stefan Hajnoczi
2012-08-02  7:28     ` Dong Xu Wang
2012-08-02 10:37       ` Stefan Hajnoczi
2012-07-31 16:51 ` [Qemu-devel] [PATCH 5/6 v11] add-cow: hmp and qmp interface Dong Xu Wang
2012-07-31 16:51 ` [Qemu-devel] [PATCH 6/6 v11] add-cow: support qemu-iotests Dong Xu Wang
2012-08-01 13:51 ` [Qemu-devel] [PATCH 1/6 v11] docs: spec for add-cow file format Eric Blake
2012-08-02  7:03   ` Dong Xu Wang
2012-08-01 13:55 ` Stefan Hajnoczi
2012-08-02  7:09   ` Dong Xu Wang
2012-08-02 10:44     ` Stefan Hajnoczi
2012-08-03  5:56       ` Dong Xu Wang
2012-08-03  8:26         ` Stefan Hajnoczi
2012-08-06  2:05           ` Dong Xu Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1343753510-24661-2-git-send-email-wdongxu@linux.vnet.ibm.com \
    --to=wdongxu@linux.vnet.ibm.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).