All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com,
	mreitz@redhat.com
Subject: [Qemu-devel] [PATCH 07/34] block: Move flag inheritance to bdrv_open_inherited()
Date: Fri,  8 May 2015 19:21:39 +0200	[thread overview]
Message-ID: <1431105726-3682-8-git-send-email-kwolf@redhat.com> (raw)
In-Reply-To: <1431105726-3682-1-git-send-email-kwolf@redhat.com>

Instead of letting every caller of bdrv_open() determine the right flags
for its child node manually and pass them to the function, pass the
parent node and the role of the newly opened child (like backing file,
protocol layer, etc.).

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block.c                   | 74 ++++++++++++++++++++++++++++++++++++++---------
 block/blkdebug.c          |  2 +-
 block/blkverify.c         |  4 +--
 block/quorum.c            |  4 +--
 block/vmdk.c              |  5 ++--
 include/block/block.h     |  4 ++-
 include/block/block_int.h |  7 +++++
 7 files changed, 78 insertions(+), 22 deletions(-)

diff --git a/block.c b/block.c
index cea022f..c4f0fb4 100644
--- a/block.c
+++ b/block.c
@@ -79,6 +79,12 @@ static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
 static QLIST_HEAD(, BlockDriver) bdrv_drivers =
     QLIST_HEAD_INITIALIZER(bdrv_drivers);
 
+static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
+                             const char *reference, QDict *options, int flags,
+                             BlockDriverState* parent,
+                             const BdrvChildRole *child_role,
+                             BlockDriver *drv, Error **errp);
+
 static void bdrv_dirty_bitmap_truncate(BlockDriverState *bs);
 /* If non-zero, use only whitelisted block drivers */
 static int use_bdrv_whitelist;
@@ -672,8 +678,8 @@ static int bdrv_temp_snapshot_flags(int flags)
 }
 
 /*
- * Returns the flags that bs->file should get, based on the given flags for
- * the parent BDS
+ * Returns the flags that bs->file should get if a protocol driver is expected,
+ * based on the given flags for the parent BDS
  */
 static int bdrv_inherited_flags(int flags)
 {
@@ -690,6 +696,25 @@ static int bdrv_inherited_flags(int flags)
     return flags;
 }
 
+const BdrvChildRole child_file = {
+    .inherit_flags = bdrv_inherited_flags,
+};
+
+/*
+ * Returns the flags that bs->file should get if the use of formats (and not
+ * only protocols) is permitted for it, based on the given flags for the parent
+ * BDS
+ */
+static int bdrv_inherited_fmt_flags(int parent_flags)
+{
+    int flags = child_file.inherit_flags(parent_flags);
+    return flags & ~BDRV_O_PROTOCOL;
+}
+
+const BdrvChildRole child_format = {
+    .inherit_flags = bdrv_inherited_fmt_flags,
+};
+
 /*
  * Returns the flags that bs->backing_hd should get, based on the given flags
  * for the parent BDS
@@ -705,6 +730,10 @@ static int bdrv_backing_flags(int flags)
     return flags;
 }
 
+static const BdrvChildRole child_backing = {
+    .inherit_flags = bdrv_backing_flags,
+};
+
 static int bdrv_open_flags(BlockDriverState *bs, int flags)
 {
     int open_flags = flags | BDRV_O_CACHE_WB;
@@ -827,7 +856,6 @@ static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
         return 0;
     }
 
-    bs->open_flags = flags;
     bs->guest_block_size = 512;
     bs->request_alignment = 512;
     bs->zero_beyond_eof = true;
@@ -1134,9 +1162,10 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp)
     }
 
     assert(bs->backing_hd == NULL);
-    ret = bdrv_open(&backing_hd,
-                    *backing_filename ? backing_filename : NULL, NULL, options,
-                    bdrv_backing_flags(bs->open_flags), NULL, &local_err);
+    ret = bdrv_open_inherit(&backing_hd,
+                            *backing_filename ? backing_filename : NULL,
+                            NULL, options, 0, bs, &child_backing,
+                            NULL, &local_err);
     if (ret < 0) {
         bdrv_unref(backing_hd);
         backing_hd = NULL;
@@ -1170,7 +1199,8 @@ free_exit:
  * To conform with the behavior of bdrv_open(), *pbs has to be NULL.
  */
 int bdrv_open_image(BlockDriverState **pbs, const char *filename,
-                    QDict *options, const char *bdref_key, int flags,
+                    QDict *options, const char *bdref_key,
+                    BlockDriverState* parent, const BdrvChildRole *child_role,
                     bool allow_none, Error **errp)
 {
     QDict *image_options;
@@ -1198,7 +1228,8 @@ int bdrv_open_image(BlockDriverState **pbs, const char *filename,
         goto done;
     }
 
-    ret = bdrv_open(pbs, filename, reference, image_options, flags, NULL, errp);
+    ret = bdrv_open_inherit(pbs, filename, reference, image_options, 0,
+                            parent, child_role, NULL, errp);
 
 done:
     qdict_del(options, bdref_key);
@@ -1285,9 +1316,11 @@ out:
  * should be opened. If specified, neither options nor a filename may be given,
  * nor can an existing BDS be reused (that is, *pbs has to be NULL).
  */
-int bdrv_open(BlockDriverState **pbs, const char *filename,
-              const char *reference, QDict *options, int flags,
-              BlockDriver *drv, Error **errp)
+static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,
+                             const char *reference, QDict *options, int flags,
+                             BlockDriverState* parent,
+                             const BdrvChildRole *child_role,
+                             BlockDriver *drv, Error **errp)
 {
     int ret;
     BlockDriverState *file = NULL, *bs;
@@ -1296,6 +1329,8 @@ int bdrv_open(BlockDriverState **pbs, const char *filename,
     int snapshot_flags = 0;
 
     assert(pbs);
+    assert(!child_role || !flags);
+    assert(!child_role == !parent);
 
     if (reference) {
         bool options_non_empty = options ? qdict_size(options) : false;
@@ -1333,6 +1368,10 @@ int bdrv_open(BlockDriverState **pbs, const char *filename,
         options = qdict_new();
     }
 
+    if (child_role) {
+        flags = child_role->inherit_flags(parent->open_flags);
+    }
+
     ret = bdrv_fill_options(&options, &filename, flags, drv, &local_err);
     if (local_err) {
         goto fail;
@@ -1358,6 +1397,7 @@ int bdrv_open(BlockDriverState **pbs, const char *filename,
         flags &= ~BDRV_O_PROTOCOL;
     }
 
+    bs->open_flags = flags;
     bs->options = options;
     options = qdict_clone_shallow(options);
 
@@ -1372,9 +1412,9 @@ int bdrv_open(BlockDriverState **pbs, const char *filename,
         }
 
         assert(file == NULL);
+        bs->open_flags = flags;
         ret = bdrv_open_image(&file, filename, options, "file",
-                              bdrv_inherited_flags(flags),
-                              true, &local_err);
+                              bs, &child_file, true, &local_err);
         if (ret < 0) {
             goto fail;
         }
@@ -1491,6 +1531,14 @@ close_and_fail:
     return ret;
 }
 
+int bdrv_open(BlockDriverState **pbs, const char *filename,
+              const char *reference, QDict *options, int flags,
+              BlockDriver *drv, Error **errp)
+{
+    return bdrv_open_inherit(pbs, filename, reference, options, flags, NULL,
+                             NULL, drv, errp);
+}
+
 typedef struct BlockReopenQueueEntry {
      bool prepared;
      BDRVReopenState state;
diff --git a/block/blkdebug.c b/block/blkdebug.c
index 3c30edb..17b7c85 100644
--- a/block/blkdebug.c
+++ b/block/blkdebug.c
@@ -431,7 +431,7 @@ static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
     /* Open the backing file */
     assert(bs->file == NULL);
     ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
-                          flags | BDRV_O_PROTOCOL, false, &local_err);
+                          bs, &child_file, false, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         goto out;
diff --git a/block/blkverify.c b/block/blkverify.c
index 438dff8..d277e63 100644
--- a/block/blkverify.c
+++ b/block/blkverify.c
@@ -125,7 +125,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
     /* Open the raw file */
     assert(bs->file == NULL);
     ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options,
-                          "raw", flags | BDRV_O_PROTOCOL, false, &local_err);
+                          "raw", bs, &child_file, false, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         goto fail;
@@ -134,7 +134,7 @@ static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
     /* Open the test file */
     assert(s->test_file == NULL);
     ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options,
-                          "test", flags, false, &local_err);
+                          "test", bs, &child_format, false, &local_err);
     if (ret < 0) {
         error_propagate(errp, local_err);
         s->test_file = NULL;
diff --git a/block/quorum.c b/block/quorum.c
index a33881a..77e55b2 100644
--- a/block/quorum.c
+++ b/block/quorum.c
@@ -935,8 +935,8 @@ static int quorum_open(BlockDriverState *bs, QDict *options, int flags,
         ret = snprintf(indexstr, 32, "children.%d", i);
         assert(ret < 32);
 
-        ret = bdrv_open_image(&s->bs[i], NULL, options, indexstr, flags,
-                              false, &local_err);
+        ret = bdrv_open_image(&s->bs[i], NULL, options, indexstr, bs,
+                              &child_format, false, &local_err);
         if (ret < 0) {
             goto close_exit;
         }
diff --git a/block/vmdk.c b/block/vmdk.c
index 641e026..69bab3a 100644
--- a/block/vmdk.c
+++ b/block/vmdk.c
@@ -852,9 +852,8 @@ static int vmdk_parse_extents(const char *desc, BlockDriverState *bs,
         ret = snprintf(extent_opt_prefix, 32, "extents.%d", s->num_extents);
         assert(ret < 32);
 
-        ret = bdrv_open_image(&extent_file, extent_path,
-                              options, extent_opt_prefix,
-                              bs->open_flags | BDRV_O_PROTOCOL, false, errp);
+        ret = bdrv_open_image(&extent_file, extent_path, options,
+                              extent_opt_prefix, bs, &child_file, false, errp);
         g_free(extent_path);
         if (ret) {
             return ret;
diff --git a/include/block/block.h b/include/block/block.h
index 86bdac8..27ab2c8 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -12,6 +12,7 @@
 /* block.c */
 typedef struct BlockDriver BlockDriver;
 typedef struct BlockJob BlockJob;
+typedef struct BdrvChildRole BdrvChildRole;
 
 typedef struct BlockDriverInfo {
     /* in bytes, 0 if irrelevant */
@@ -203,7 +204,8 @@ void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top);
 int bdrv_parse_cache_flags(const char *mode, int *flags);
 int bdrv_parse_discard_flags(const char *mode, int *flags);
 int bdrv_open_image(BlockDriverState **pbs, const char *filename,
-                    QDict *options, const char *bdref_key, int flags,
+                    QDict *options, const char *bdref_key,
+                    BlockDriverState* parent, const BdrvChildRole *child_role,
                     bool allow_none, Error **errp);
 void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd);
 int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp);
diff --git a/include/block/block_int.h b/include/block/block_int.h
index db29b74..eb01ea2 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -327,6 +327,13 @@ typedef struct BdrvAioNotifier {
     QLIST_ENTRY(BdrvAioNotifier) list;
 } BdrvAioNotifier;
 
+struct BdrvChildRole {
+    int (*inherit_flags)(int parent_flags);
+};
+
+extern const BdrvChildRole child_file;
+extern const BdrvChildRole child_format;
+
 /*
  * Note: the function bdrv_append() copies and swaps contents of
  * BlockDriverStates, so if you add new fields to this struct, please
-- 
1.8.3.1

  parent reply	other threads:[~2015-05-08 17:22 UTC|newest]

Thread overview: 130+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-08 17:21 [Qemu-devel] [PATCH 00/34] block: Cache mode for children, reopen overhaul and more Kevin Wolf
2015-05-08 17:21 ` [Qemu-devel] [PATCH 01/34] qdict: Add qdict_array_entries() Kevin Wolf
2015-05-08 20:06   ` Eric Blake
2015-05-08 21:22     ` Eric Blake
2015-05-11 14:40     ` Kevin Wolf
2015-05-11 15:28       ` Eric Blake
2015-05-20 14:19     ` [Qemu-devel] [Qemu-block] " Alberto Garcia
2015-05-11 13:56   ` [Qemu-devel] " Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 02/34] qdict: Add qdict_{set,copy}_default() Kevin Wolf
2015-05-08 21:30   ` [Qemu-devel] [PATCH 02/34] qdict: Add qdict_{set, copy}_default() Eric Blake
2015-05-11 14:06     ` Kevin Wolf
2015-05-11 14:16   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 03/34] quorum: Use bdrv_open_image() Kevin Wolf
2015-05-08 21:33   ` Eric Blake
2015-05-11 14:27   ` Max Reitz
2015-05-12 19:07   ` [Qemu-devel] [Qemu-block] " Jeff Cody
2015-05-20 14:46   ` Alberto Garcia
2015-05-08 17:21 ` [Qemu-devel] [PATCH 04/34] vmdk: " Kevin Wolf
2015-05-08 22:00   ` Eric Blake
2015-05-11 14:35   ` Max Reitz
2015-05-12 19:12   ` [Qemu-devel] [Qemu-block] " Jeff Cody
2015-05-08 17:21 ` [Qemu-devel] [PATCH 05/34] block: Use macro for cache option names Kevin Wolf
2015-05-08 22:54   ` Eric Blake
2015-05-11 14:40   ` Max Reitz
2015-05-11 14:51     ` Kevin Wolf
2015-05-11 14:59       ` Max Reitz
2015-05-11 15:00   ` Max Reitz
2015-05-12 19:14   ` [Qemu-devel] [Qemu-block] " Jeff Cody
2015-05-20 14:49   ` Alberto Garcia
2015-05-08 17:21 ` [Qemu-devel] [PATCH 06/34] block: Use QemuOpts in bdrv_open_common() Kevin Wolf
2015-05-08 22:57   ` Eric Blake
2015-05-11 14:57   ` Max Reitz
2015-05-08 17:21 ` Kevin Wolf [this message]
2015-05-08 23:20   ` [Qemu-devel] [PATCH 07/34] block: Move flag inheritance to bdrv_open_inherited() Eric Blake
2015-05-11 15:20   ` Max Reitz
2015-05-12 13:32     ` Kevin Wolf
2015-05-28 11:10   ` Wen Congyang
2015-05-08 17:21 ` [Qemu-devel] [PATCH 08/34] block: Add list of children to BlockDriverState Kevin Wolf
2015-05-08 23:34   ` Eric Blake
2015-05-11 15:45   ` Max Reitz
2015-05-12 14:23     ` Kevin Wolf
2015-06-10 12:09     ` Kevin Wolf
2015-06-10 13:48       ` Max Reitz
2015-05-27 11:30   ` Kevin Wolf
2015-05-08 17:21 ` [Qemu-devel] [PATCH 09/34] block: Add BlockDriverState.inherits_from Kevin Wolf
2015-05-08 23:39   ` Eric Blake
2015-05-11 15:50   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 10/34] block: Fix reopen flag inheritance Kevin Wolf
2015-05-11 16:04   ` Max Reitz
2015-05-12 14:32   ` Eric Blake
2015-05-08 17:21 ` [Qemu-devel] [PATCH 11/34] block: Allow references for backing files Kevin Wolf
2015-05-11 16:19   ` Max Reitz
2015-05-12 14:46   ` Eric Blake
2015-05-21  5:47   ` Wen Congyang
2015-05-27 12:31     ` Kevin Wolf
2015-05-27 13:30       ` [Qemu-devel] [Qemu-block] " Kevin Wolf
2015-05-27 13:44         ` Paolo Bonzini
2015-05-28  0:59       ` [Qemu-devel] " Wen Congyang
2015-05-28  9:48         ` Kevin Wolf
2015-05-28  9:58           ` Wen Congyang
2015-06-01  2:01       ` Wen Congyang
2015-05-08 17:21 ` [Qemu-devel] [PATCH 12/34] block: Allow specifying driver-specific options to reopen Kevin Wolf
2015-05-11 16:35   ` Max Reitz
2015-05-12 14:59   ` Eric Blake
2015-05-08 17:21 ` [Qemu-devel] [PATCH 13/34] qemu-io: Add command 'reopen' Kevin Wolf
2015-05-11 16:50   ` Max Reitz
2015-05-12 15:05   ` Eric Blake
2015-05-13  8:37     ` Kevin Wolf
2015-05-08 17:21 ` [Qemu-devel] [PATCH 14/34] qcow2: Factor out qcow2_update_options() Kevin Wolf
2015-05-12 20:04   ` Eric Blake
2015-05-13  9:11     ` Kevin Wolf
2015-05-13 17:04       ` Eric Blake
2015-05-13 11:21   ` Max Reitz
2015-05-13 11:28   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 15/34] qcow2: Move qcow2_update_options() call up Kevin Wolf
2015-05-12 20:15   ` Eric Blake
2015-05-13 11:25   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 16/34] qcow2: Move rest of option handling to qcow2_update_options() Kevin Wolf
2015-05-12 20:47   ` Eric Blake
2015-05-13 11:38   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 17/34] qcow2: Leave s unchanged on qcow2_update_options() failure Kevin Wolf
2015-05-12 20:57   ` Eric Blake
2015-05-13 11:47   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 18/34] qcow2: Fix memory leak in qcow2_update_options() error path Kevin Wolf
2015-05-12 21:26   ` Eric Blake
2015-05-13 11:52   ` Max Reitz
2015-05-13 12:02     ` Kevin Wolf
2015-05-13 12:04       ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 19/34] qcow2: Make qcow2_update_options() suitable for transactions Kevin Wolf
2015-05-12 21:40   ` Eric Blake
2015-05-13  9:21     ` Kevin Wolf
2015-05-13 12:06   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 20/34] qcow2: Support updating driver-specific options in reopen Kevin Wolf
2015-05-12 21:47   ` Eric Blake
2015-05-13  9:26     ` Kevin Wolf
2015-05-08 17:21 ` [Qemu-devel] [PATCH 21/34] block: Consider all block layer options in append_open_options Kevin Wolf
2015-05-12 21:59   ` Eric Blake
2015-05-13 12:26   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 22/34] block: Exclude nested options only for children in append_open_options() Kevin Wolf
2015-05-13 12:49   ` Max Reitz
2015-05-13 12:50     ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 23/34] block: Pass driver-specific options to .bdrv_refresh_filename() Kevin Wolf
2015-05-13 12:57   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 24/34] block: Keep "driver" in bs->options Kevin Wolf
2015-05-13 13:22   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 25/34] block: Allow specifying child options in reopen Kevin Wolf
2015-05-13 13:41   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 26/34] block: reopen: Document option precedence and refactor accordingly Kevin Wolf
2015-05-13 14:47   ` Max Reitz
2015-05-08 17:21 ` [Qemu-devel] [PATCH 27/34] block: Add infrastructure for option inheritance Kevin Wolf
2015-05-13 15:10   ` Max Reitz
2015-05-13 15:28     ` Kevin Wolf
2015-05-08 17:22 ` [Qemu-devel] [PATCH 28/34] block: Introduce bs->explicit_options Kevin Wolf
2015-05-15 17:47   ` Max Reitz
2015-10-29 11:38     ` Kevin Wolf
2015-05-08 17:22 ` [Qemu-devel] [PATCH 29/34] qemu-iotests: Remove cache mode test without medium Kevin Wolf
2015-05-15 17:53   ` Max Reitz
2015-05-08 17:22 ` [Qemu-devel] [PATCH 30/34] block: reopen: Extract QemuOpts for generic block layer options Kevin Wolf
2015-05-15 18:07   ` Max Reitz
2015-05-08 17:22 ` [Qemu-devel] [PATCH 31/34] block: Move cache options into options QDict Kevin Wolf
2015-05-15 18:43   ` Max Reitz
2015-05-15 19:44     ` Eric Blake
2015-05-08 17:22 ` [Qemu-devel] [PATCH 32/34] qemu-iotests: Try setting cache mode for children Kevin Wolf
2015-05-15 18:52   ` Max Reitz
2015-05-08 17:22 ` [Qemu-devel] [PATCH 33/34] qemu-iotests: Test cache mode option inheritance Kevin Wolf
2015-05-15 19:16   ` Max Reitz
2015-05-18 14:39     ` Kevin Wolf
2015-05-18 15:32       ` Max Reitz
2015-05-08 17:22 ` [Qemu-devel] [PATCH 34/34] qemu-iotests: Test reopen with node-name/driver options Kevin Wolf
2015-05-15 19:19   ` Max Reitz

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=1431105726-3682-8-git-send-email-kwolf@redhat.com \
    --to=kwolf@redhat.com \
    --cc=armbru@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.