diff mbox

[v5,5/7] block: Parse "backing" option to reference existing BDS

Message ID 1385438728-17924-6-git-send-email-famz@redhat.com
State New
Headers show

Commit Message

Fam Zheng Nov. 26, 2013, 4:05 a.m. UTC
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block.c                   | 31 ++++++++++++++++++++++++++++---
 block/mirror.c            |  2 +-
 include/block/block.h     |  3 ++-
 include/block/block_int.h |  3 +++
 4 files changed, 34 insertions(+), 5 deletions(-)

Comments

Paolo Bonzini Nov. 26, 2013, 4:18 p.m. UTC | #1
Il 26/11/2013 05:05, Fam Zheng ha scritto:
> +    if (backing_bs && *backing_bs != '\0') {
> +        bs->backing_hd = bdrv_find(backing_bs);
> +        if (!bs->backing_hd) {
> +            error_setg(errp, "Backing device not found: %s", backing_bs);
> +            return -ENOENT;
> +        }
> +        bdrv_ref(bs->backing_hd);
> +        assert(!bs->backing_blocker);
> +        error_setg(&bs->backing_blocker,
> +                   "device is used as backing hd of '%s'",
> +                   bs->device_name);
> +        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);

Why should this blocker only apply to the "named backing file" case, and
not to all backing files?

Paolo

> +        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
> +                bs->backing_hd->filename);
> +        pstrcpy(bs->backing_format, sizeof(bs->backing_format),
> +                bs->backing_hd->drv->format_name);
> +    }
Fam Zheng Nov. 27, 2013, 12:56 a.m. UTC | #2
On 2013年11月27日 00:18, Paolo Bonzini wrote:
> Il 26/11/2013 05:05, Fam Zheng ha scritto:
>> +    if (backing_bs && *backing_bs != '\0') {
>> +        bs->backing_hd = bdrv_find(backing_bs);
>> +        if (!bs->backing_hd) {
>> +            error_setg(errp, "Backing device not found: %s", backing_bs);
>> +            return -ENOENT;
>> +        }
>> +        bdrv_ref(bs->backing_hd);
>> +        assert(!bs->backing_blocker);
>> +        error_setg(&bs->backing_blocker,
>> +                   "device is used as backing hd of '%s'",
>> +                   bs->device_name);
>> +        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
>
> Why should this blocker only apply to the "named backing file" case, and
> not to all backing files?
>

Good point, thanks.

Fam
diff mbox

Patch

diff --git a/block.c b/block.c
index c2ab6d8..d633f2b 100644
--- a/block.c
+++ b/block.c
@@ -959,18 +959,39 @@  fail:
 /*
  * Opens the backing file for a BlockDriverState if not yet open
  *
+ * If backing_bs is not NULL or empty, find the BDS by name and reference it as
+ * the backing_hd, in this case options is ignored.
+ *
  * options is a QDict of options to pass to the block drivers, or NULL for an
  * empty set of options. The reference to the QDict is transferred to this
  * function (even on failure), so if the caller intends to reuse the dictionary,
  * it needs to use QINCREF() before calling bdrv_file_open.
  */
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
+int bdrv_open_backing_file(BlockDriverState *bs, const char *backing_bs,
+                           QDict *options, Error **errp)
 {
     char backing_filename[PATH_MAX];
     int back_flags, ret;
     BlockDriver *back_drv = NULL;
     Error *local_err = NULL;
 
+    if (backing_bs && *backing_bs != '\0') {
+        bs->backing_hd = bdrv_find(backing_bs);
+        if (!bs->backing_hd) {
+            error_setg(errp, "Backing device not found: %s", backing_bs);
+            return -ENOENT;
+        }
+        bdrv_ref(bs->backing_hd);
+        assert(!bs->backing_blocker);
+        error_setg(&bs->backing_blocker,
+                   "device is used as backing hd of '%s'",
+                   bs->device_name);
+        bdrv_op_block_all(bs->backing_hd, bs->backing_blocker);
+        pstrcpy(bs->backing_file, sizeof(bs->backing_file),
+                bs->backing_hd->filename);
+        pstrcpy(bs->backing_format, sizeof(bs->backing_format),
+                bs->backing_hd->drv->format_name);
+    }
     if (bs->backing_hd != NULL) {
         QDECREF(options);
         return 0;
@@ -1166,9 +1187,11 @@  int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
     /* If there is a backing file, use it */
     if ((flags & BDRV_O_NO_BACKING) == 0) {
         QDict *backing_options;
-
         qdict_extract_subqdict(options, &backing_options, "backing.");
-        ret = bdrv_open_backing_file(bs, backing_options, &local_err);
+        ret = bdrv_open_backing_file(bs, qdict_get_try_str(options, "backing"),
+                                     backing_options, &local_err);
+
+        qdict_del(options, "backing");
         if (ret < 0) {
             goto close_and_fail;
         }
@@ -1461,6 +1484,8 @@  void bdrv_close(BlockDriverState *bs)
 
     if (bs->drv) {
         if (bs->backing_hd) {
+            bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker);
+            error_free(bs->backing_blocker);
             bdrv_unref(bs->backing_hd);
             bs->backing_hd = NULL;
         }
diff --git a/block/mirror.c b/block/mirror.c
index 7b95acf..ce0103a 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -508,7 +508,7 @@  static void mirror_complete(BlockJob *job, Error **errp)
     Error *local_err = NULL;
     int ret;
 
-    ret = bdrv_open_backing_file(s->target, NULL, &local_err);
+    ret = bdrv_open_backing_file(s->target, NULL, NULL, &local_err);
     if (ret < 0) {
         char backing_filename[PATH_MAX];
         bdrv_get_full_backing_filename(s->target, backing_filename,
diff --git a/include/block/block.h b/include/block/block.h
index 173c09a..e6ec03e 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -157,7 +157,8 @@  int bdrv_parse_cache_flags(const char *mode, int *flags);
 int bdrv_parse_discard_flags(const char *mode, int *flags);
 int bdrv_file_open(BlockDriverState **pbs, const char *filename,
                    QDict *options, int flags, Error **errp);
-int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
+int bdrv_open_backing_file(BlockDriverState *bs, const char *backing_bs,
+                           QDict *options, Error **errp);
 int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
               int flags, BlockDriver *drv, Error **errp);
 BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 60edc80..6db30c9 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -316,6 +316,9 @@  struct BlockDriverState {
     BlockJob *job;
 
     QDict *options;
+
+    /* For backing reference, block the operations of named backing device */
+    Error *backing_blocker;
 };
 
 int get_tmp_filename(char *filename, int size);