qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Wolf <kwolf@redhat.com>
To: qemu-block@nongnu.org
Cc: kwolf@redhat.com, qemu-devel@nongnu.org, mreitz@redhat.com
Subject: [PATCH 23/29] block/export: Create BlockBackend in blk_exp_add()
Date: Mon,  7 Sep 2020 20:20:05 +0200	[thread overview]
Message-ID: <20200907182011.521007-24-kwolf@redhat.com> (raw)
In-Reply-To: <20200907182011.521007-1-kwolf@redhat.com>

Every export type will need a BlockBackend, so creating it centrally in
blk_exp_add() instead of the .create driver callback avoids duplication.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 include/block/nbd.h   |  4 ++--
 block/export/export.c | 49 +++++++++++++++++++++++++++++++++++++++----
 blockdev-nbd.c        | 33 ++++-------------------------
 nbd/server.c          | 38 +++++++++++----------------------
 4 files changed, 63 insertions(+), 61 deletions(-)

diff --git a/include/block/nbd.h b/include/block/nbd.h
index a4dc1f9e54..5270b7eadd 100644
--- a/include/block/nbd.h
+++ b/include/block/nbd.h
@@ -332,10 +332,10 @@ typedef struct NBDClient NBDClient;
 
 int nbd_export_create(BlockExport *exp, BlockExportOptions *exp_args,
                       Error **errp);
-int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
+int nbd_export_new(BlockExport *blk_exp,
                    const char *name, const char *desc,
                    const char *bitmap, bool readonly, bool shared,
-                   bool writethrough, Error **errp);
+                   Error **errp);
 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk);
 
 AioContext *nbd_export_aio_context(NBDExport *exp);
diff --git a/block/export/export.c b/block/export/export.c
index 40fc9d505f..52ec00dfcf 100644
--- a/block/export/export.c
+++ b/block/export/export.c
@@ -58,7 +58,10 @@ static const BlockExportDriver *blk_exp_find_driver(BlockExportType type)
 BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
 {
     const BlockExportDriver *drv;
-    BlockExport *exp;
+    BlockExport *exp = NULL;
+    BlockDriverState *bs;
+    BlockBackend *blk;
+    AioContext *ctx;
     int ret;
 
     if (!id_wellformed(export->id)) {
@@ -76,6 +79,33 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
         return NULL;
     }
 
+    bs = bdrv_lookup_bs(NULL, export->node_name, errp);
+    if (!bs) {
+        return NULL;
+    }
+
+    ctx = bdrv_get_aio_context(bs);
+    aio_context_acquire(ctx);
+
+    /*
+     * Block exports are used for non-shared storage migration. Make sure
+     * that BDRV_O_INACTIVE is cleared and the image is ready for write
+     * access since the export could be available before migration handover.
+     * ctx was acquired in the caller.
+     */
+    bdrv_invalidate_cache(bs, NULL);
+
+    blk = blk_new(ctx, BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
+    ret = blk_insert_bs(blk, bs, errp);
+    if (ret < 0) {
+        goto fail;
+    }
+
+    if (!export->has_writethrough) {
+        export->writethrough = false;
+    }
+    blk_set_enable_write_cache(blk, !export->writethrough);
+
     assert(drv->instance_size >= sizeof(BlockExport));
     exp = g_malloc0(drv->instance_size);
     *exp = (BlockExport) {
@@ -83,19 +113,30 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
         .refcount   = 1,
         .user_owned = true,
         .id         = g_strdup(export->id),
+        .ctx        = ctx,
+        .blk        = blk,
     };
 
     ret = drv->create(exp, export, errp);
     if (ret < 0) {
-        g_free(exp->id);
-        g_free(exp);
-        return NULL;
+        goto fail;
     }
 
     assert(exp->blk != NULL);
 
     QLIST_INSERT_HEAD(&block_exports, exp, next);
+
+    aio_context_release(ctx);
     return exp;
+
+fail:
+    blk_unref(blk);
+    aio_context_release(ctx);
+    if (exp) {
+        g_free(exp->id);
+        g_free(exp);
+    }
+    return NULL;
 }
 
 /* Callers must hold exp->ctx lock */
diff --git a/blockdev-nbd.c b/blockdev-nbd.c
index 4a9a1be571..cdbbcdb958 100644
--- a/blockdev-nbd.c
+++ b/blockdev-nbd.c
@@ -177,9 +177,6 @@ int nbd_export_create(BlockExport *exp, BlockExportOptions *exp_args,
                       Error **errp)
 {
     BlockExportOptionsNbd *arg = &exp_args->u.nbd;
-    BlockDriverState *bs = NULL;
-    AioContext *aio_context;
-    int ret;
 
     assert(exp_args->type == BLOCK_EXPORT_TYPE_NBD);
 
@@ -207,38 +204,16 @@ int nbd_export_create(BlockExport *exp, BlockExportOptions *exp_args,
         return -EEXIST;
     }
 
-    bs = bdrv_lookup_bs(NULL, exp_args->node_name, errp);
-    if (!bs) {
-        return -ENOENT;
-    }
-
-    aio_context = bdrv_get_aio_context(bs);
-    aio_context_acquire(aio_context);
-
     if (!arg->has_writable) {
         arg->writable = false;
     }
-    if (bdrv_is_read_only(bs) && arg->writable) {
-        ret = -EINVAL;
+    if (blk_is_read_only(exp->blk) && arg->writable) {
         error_setg(errp, "Cannot export read-only node as writable");
-        goto out;
-    }
-
-    if (!exp_args->has_writethrough) {
-        exp_args->writethrough = false;
-    }
-
-    ret = nbd_export_new(exp, bs, arg->name, arg->description, arg->bitmap,
-                         !arg->writable, !arg->writable,
-                         exp_args->writethrough, errp);
-    if (ret < 0) {
-        goto out;
+        return -EINVAL;
     }
 
-    ret = 0;
- out:
-    aio_context_release(aio_context);
-    return ret;
+    return nbd_export_new(exp, arg->name, arg->description, arg->bitmap,
+                          !arg->writable, !arg->writable, errp);
 }
 
 void qmp_nbd_server_add(NbdServerAddOptions *arg, Error **errp)
diff --git a/nbd/server.c b/nbd/server.c
index f9af45c480..6c8532de23 100644
--- a/nbd/server.c
+++ b/nbd/server.c
@@ -1507,56 +1507,42 @@ void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
     blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
 }
 
-int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
+int nbd_export_new(BlockExport *blk_exp,
                    const char *name, const char *desc,
                    const char *bitmap, bool readonly, bool shared,
-                   bool writethrough, Error **errp)
+                   Error **errp)
 {
     NBDExport *exp = container_of(blk_exp, NBDExport, common);
-    AioContext *ctx;
-    BlockBackend *blk;
+    BlockBackend *blk = blk_exp->blk;
     int64_t size;
-    uint64_t perm;
+    uint64_t perm, shared_perm;
     int ret;
 
-    size = bdrv_getlength(bs);
+    size = blk_getlength(blk);
     if (size < 0) {
         error_setg_errno(errp, -size,
                          "Failed to determine the NBD export's length");
         return size;
     }
 
-    ctx = bdrv_get_aio_context(bs);
-    blk_exp->ctx = ctx;
-
-    /*
-     * NBD exports are used for non-shared storage migration.  Make sure
-     * that BDRV_O_INACTIVE is cleared and the image is ready for write
-     * access since the export could be available before migration handover.
-     * ctx was acquired in the caller.
-     */
     assert(name && strlen(name) <= NBD_MAX_STRING_SIZE);
 
-    bdrv_invalidate_cache(bs, NULL);
-
     /* Don't allow resize while the NBD server is running, otherwise we don't
      * care what happens with the node. */
-    perm = BLK_PERM_CONSISTENT_READ;
+    blk_get_perm(blk, &perm, &shared_perm);
+
     if (!readonly) {
         perm |= BLK_PERM_WRITE;
     }
-    blk = blk_new(ctx, perm,
-                  BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
-                  BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
-    ret = blk_insert_bs(blk, bs, errp);
+
+    ret = blk_set_perm(blk, perm, shared_perm & ~BLK_PERM_RESIZE, errp);
     if (ret < 0) {
-        goto fail;
+        return ret;
     }
-    blk_set_enable_write_cache(blk, !writethrough);
+
     blk_set_allow_aio_context_change(blk, true);
 
     QTAILQ_INIT(&exp->clients);
-    exp->common.blk = blk;
     exp->name = g_strdup(name);
     assert(!desc || strlen(desc) <= NBD_MAX_STRING_SIZE);
     exp->description = g_strdup(desc);
@@ -1574,6 +1560,7 @@ int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
     exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
 
     if (bitmap) {
+        BlockDriverState *bs = blk_bs(blk);
         BdrvDirtyBitmap *bm = NULL;
 
         while (bs) {
@@ -1620,7 +1607,6 @@ int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
     return 0;
 
 fail:
-    blk_unref(blk);
     g_free(exp->name);
     g_free(exp->description);
     return ret;
-- 
2.25.4



  parent reply	other threads:[~2020-09-07 18:29 UTC|newest]

Thread overview: 53+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-07 18:19 [PATCH 00/29] block/export: Add infrastructure and QAPI for block exports Kevin Wolf
2020-09-07 18:19 ` [PATCH 01/29] nbd: Remove unused nbd_export_get_blockdev() Kevin Wolf
2020-09-07 18:19 ` [PATCH 02/29] qapi: Create block-export module Kevin Wolf
2020-09-07 18:19 ` [PATCH 03/29] qapi: Rename BlockExport to BlockExportOptions Kevin Wolf
2020-09-07 18:19 ` [PATCH 04/29] block/export: Add BlockExport infrastructure and block-export-add Kevin Wolf
2020-09-10 10:16   ` Max Reitz
2020-09-07 18:19 ` [PATCH 05/29] qemu-storage-daemon: Use qmp_block_export_add() Kevin Wolf
2020-09-07 18:19 ` [PATCH 06/29] qemu-nbd: Use raw block driver for --offset Kevin Wolf
2020-09-07 18:19 ` [PATCH 07/29] block/export: Remove magic from block-export-add Kevin Wolf
2020-09-10 10:53   ` Max Reitz
2020-09-07 18:19 ` [PATCH 08/29] nbd: Add max-connections to nbd-server-start Kevin Wolf
2020-09-07 18:19 ` [PATCH 09/29] nbd: Add writethrough to block-export-add Kevin Wolf
2020-09-10 11:15   ` Max Reitz
2020-09-07 18:19 ` [PATCH 10/29] nbd: Remove NBDExport.close callback Kevin Wolf
2020-09-07 18:19 ` [PATCH 11/29] qemu-nbd: Use blk_exp_add() to create the export Kevin Wolf
2020-09-07 18:19 ` [PATCH 12/29] nbd/server: Simplify export shutdown Kevin Wolf
2020-09-07 18:19 ` [PATCH 13/29] block/export: Move refcount from NBDExport to BlockExport Kevin Wolf
2020-09-07 18:19 ` [PATCH 14/29] block/export: Move AioContext " Kevin Wolf
2020-09-10 11:52   ` Max Reitz
2020-09-07 18:19 ` [PATCH 15/29] block/export: Add node-name to BlockExportOptions Kevin Wolf
2020-09-10 12:35   ` Max Reitz
2020-09-07 18:19 ` [PATCH 16/29] block/export: Allocate BlockExport in blk_exp_add() Kevin Wolf
2020-09-16 10:56   ` Max Reitz
2020-09-07 18:19 ` [PATCH 17/29] block/export: Add blk_exp_close_all(_type) Kevin Wolf
2020-09-10 13:22   ` Max Reitz
2020-09-07 18:20 ` [PATCH 18/29] block/export: Add 'id' option to block-export-add Kevin Wolf
2020-09-10 13:26   ` Max Reitz
2020-09-07 18:20 ` [PATCH 19/29] block/export: Move strong user reference to block_exports Kevin Wolf
2020-09-10 13:33   ` Max Reitz
2020-09-10 13:36     ` Max Reitz
2020-09-07 18:20 ` [PATCH 20/29] block/export: Add block-export-del Kevin Wolf
2020-09-07 18:20 ` [PATCH 21/29] block/export: Add BLOCK_EXPORT_DELETED event Kevin Wolf
2020-09-10 14:04   ` Max Reitz
2020-09-10 15:12   ` Max Reitz
2020-09-16 14:57   ` Max Reitz
2020-09-07 18:20 ` [PATCH 22/29] block/export: Move blk to BlockExport Kevin Wolf
2020-09-07 18:20 ` Kevin Wolf [this message]
2020-09-10 15:09   ` [PATCH 23/29] block/export: Create BlockBackend in blk_exp_add() Max Reitz
2020-09-07 18:20 ` [PATCH 24/29] block/export: Add query-block-exports Kevin Wolf
2020-09-10 15:10   ` Max Reitz
2020-09-07 18:20 ` [PATCH 25/29] block/export: Move writable to BlockExportOptions Kevin Wolf
2020-09-10 15:15   ` Max Reitz
2020-09-07 18:20 ` [PATCH 26/29] nbd: Merge nbd_export_new() and nbd_export_create() Kevin Wolf
2020-09-10 15:30   ` Max Reitz
2020-09-07 18:20 ` [PATCH 27/29] nbd: Deprecate nbd-server-add/remove Kevin Wolf
2020-09-10 15:34   ` Max Reitz
2020-09-23 16:19     ` Kevin Wolf
2020-09-07 18:20 ` [PATCH 28/29] iotests: Factor out qemu_tool_pipe_and_status() Kevin Wolf
2020-09-10 15:45   ` Max Reitz
2020-09-07 18:20 ` [PATCH 29/29] iotests: Test block-export-* QMP interface Kevin Wolf
2020-09-10 16:11   ` Max Reitz
2020-09-08  8:38 ` [PATCH 00/29] block/export: Add infrastructure and QAPI for block exports Markus Armbruster
2020-09-08 12:29   ` Kevin Wolf

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=20200907182011.521007-24-kwolf@redhat.com \
    --to=kwolf@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 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).