qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, hbrock@redhat.com, rjones@redhat.com,
	imain@redhat.com, stefanha@redhat.com, pbonzini@redhat.com
Subject: [Qemu-devel] [PATCH v5 5/7] block: Parse "backing" option to reference existing BDS
Date: Tue, 26 Nov 2013 12:05:26 +0800	[thread overview]
Message-ID: <1385438728-17924-6-git-send-email-famz@redhat.com> (raw)
In-Reply-To: <1385438728-17924-1-git-send-email-famz@redhat.com>

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(-)

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);
-- 
1.8.4.2

  parent reply	other threads:[~2013-11-26  4:06 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-26  4:05 [Qemu-devel] [PATCH v5 0/7] Drop in_use from BlockDriverState and enable point-in-time snapshot exporting over NBD Fam Zheng
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 1/7] qapi: Add BlockOperationType enum Fam Zheng
2013-11-26 16:21   ` Paolo Bonzini
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 2/7] block: Introduce op_blockers to BlockDriverState Fam Zheng
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 3/7] block: Replace in_use with operation blocker Fam Zheng
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 4/7] block: Add checks of blocker in block operations Fam Zheng
2013-11-26 16:13   ` Paolo Bonzini
2013-11-28  3:39     ` Fam Zheng
2013-11-26  4:05 ` Fam Zheng [this message]
2013-11-26 16:18   ` [Qemu-devel] [PATCH v5 5/7] block: Parse "backing" option to reference existing BDS Paolo Bonzini
2013-11-27  0:56     ` Fam Zheng
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 6/7] qmp: add command 'blockdev-backup' Fam Zheng
2013-11-26  4:05 ` [Qemu-devel] [PATCH v5 7/7] block: Allow backup on referenced named BlockDriverState Fam Zheng

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=1385438728-17924-6-git-send-email-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=hbrock@redhat.com \
    --cc=imain@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=rjones@redhat.com \
    --cc=stefanha@redhat.com \
    /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).