Skip to content

Commit

Permalink
block/export: Move strong user reference to block_exports
Browse files Browse the repository at this point in the history
The reference owned by the user/monitor that is created when adding the
export and dropped when removing it was tied to the 'exports' list in
nbd/server.c. Every block export will have a user reference, so move it
to the block export level and tie it to the 'block_exports' list in
block/export/export.c instead. This is necessary for introducing a QMP
command for removing exports.

Note that exports are present in block_exports even after the user has
requested shutdown. This is different from NBD's exports where exports
are immediately removed on a shutdown request, even if they are still in
the process of shutting down. In order to avoid that the user still
interacts with an export that is shutting down (and possibly removes it
a second time), we need to remember if the user actually still owns it.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
Reviewed-by: Max Reitz <mreitz@redhat.com>
Message-Id: <20200924152717.287415-20-kwolf@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
kevmw committed Oct 2, 2020
1 parent d53be9c commit 3859ad3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions block/export/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ BlockExport *blk_exp_add(BlockExportOptions *export, Error **errp)
*exp = (BlockExport) {
.drv = drv,
.refcount = 1,
.user_owned = true,
.id = g_strdup(export->id),
};

Expand Down Expand Up @@ -143,6 +144,11 @@ void blk_exp_request_shutdown(BlockExport *exp)

aio_context_acquire(aio_context);
exp->drv->request_shutdown(exp);

assert(exp->user_owned);
exp->user_owned = false;
blk_exp_unref(exp);

aio_context_release(aio_context);
}

Expand Down
5 changes: 0 additions & 5 deletions blockdev-nbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,6 @@ int nbd_export_create(BlockExport *exp, BlockExportOptions *exp_args,
goto out;
}

/* The list of named exports has a strong reference to this export now and
* our only way of accessing it is through nbd_export_find(), so we can drop
* the strong reference that is @exp. */
blk_exp_unref(exp);

ret = 0;
out:
aio_context_release(aio_context);
Expand Down
8 changes: 8 additions & 0 deletions include/block/export.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ struct BlockExport {
*/
int refcount;

/*
* True if one of the references in refcount belongs to the user. After the
* user has dropped their reference, they may not e.g. remove the same
* export a second time (which would decrease the refcount without having
* it incremented first).
*/
bool user_owned;

/* The AioContext whose lock protects this BlockExport object. */
AioContext *ctx;

Expand Down
2 changes: 0 additions & 2 deletions nbd/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,6 @@ int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,

blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);

blk_exp_ref(&exp->common);
QTAILQ_INSERT_TAIL(&exports, exp, next);

return 0;
Expand Down Expand Up @@ -1663,7 +1662,6 @@ static void nbd_export_request_shutdown(BlockExport *blk_exp)
client_close(client, true);
}
if (exp->name) {
blk_exp_unref(&exp->common);
g_free(exp->name);
exp->name = NULL;
QTAILQ_REMOVE(&exports, exp, next);
Expand Down

0 comments on commit 3859ad3

Please sign in to comment.