diff mbox

[15/21] block: add "snapshot.size" option to avoid extra bdrv_open()

Message ID 1384777531-14635-16-git-send-email-marcandre.lureau@gmail.com
State New
Headers show

Commit Message

Marc-André Lureau Nov. 18, 2013, 12:25 p.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 block.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
diff mbox

Patch

diff --git a/block.c b/block.c
index 09aada5..9e7632e 100644
--- a/block.c
+++ b/block.c
@@ -1135,27 +1135,32 @@  int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
         BlockDriverState *bs1;
         int64_t total_size;
 
+        total_size = qdict_get_try_int(options, "snapshot.size", -1);
+        qdict_del(options, "snapshot.size");
+
         if (qdict_size(options) != 0) {
             error_setg(errp, "Can't use snapshot=on with driver-specific options");
             ret = -EINVAL;
             goto fail;
         }
 
-        bs1 = bdrv_new_int("", NULL);
-        ret = bdrv_open(bs1, filename, NULL, 0, drv, &local_err);
-        if (ret < 0) {
+        if (total_size == -1) {
+            bs1 = bdrv_new_int("", NULL);
+            ret = bdrv_open(bs1, filename, NULL, 0, drv, &local_err);
+            if (ret < 0) {
+                bdrv_unref(bs1);
+                goto fail;
+            }
+            total_size = bdrv_getlength(bs1);
             bdrv_unref(bs1);
-            goto fail;
         }
 
-        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
-        bdrv_unref(bs1);
-
         ret = make_snapshot(bs, total_size, &filename, &drv, errp);
         if (ret < 0) {
             goto fail;
         }
     }
+    qdict_del(options, "snapshot.size");
 
     /* Open image file without format layer */
     if (flags & BDRV_O_RDWR) {