block/nbd: introduce nbd_client_connection_release()

This is a last step of creating bs-independent nbd connection
interface. With next commit we can finally move it to separate file.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Message-Id: <20210610100802.5888-17-vsementsov@virtuozzo.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Vladimir Sementsov-Ogievskiy 2021-06-10 13:07:46 +03:00 committed by Eric Blake
parent f68729747d
commit 248d470198
1 changed files with 27 additions and 18 deletions

View File

@ -127,7 +127,7 @@ typedef struct BDRVNBDState {
NBDClientConnection *conn; NBDClientConnection *conn;
} BDRVNBDState; } BDRVNBDState;
static void nbd_free_connect_thread(NBDClientConnection *conn); static void nbd_client_connection_release(NBDClientConnection *conn);
static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *saddr, static int nbd_establish_connection(BlockDriverState *bs, SocketAddress *saddr,
Error **errp); Error **errp);
static coroutine_fn QIOChannelSocket * static coroutine_fn QIOChannelSocket *
@ -139,22 +139,9 @@ static void nbd_yank(void *opaque);
static void nbd_clear_bdrvstate(BlockDriverState *bs) static void nbd_clear_bdrvstate(BlockDriverState *bs)
{ {
BDRVNBDState *s = (BDRVNBDState *)bs->opaque; BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
NBDClientConnection *conn = s->conn;
bool do_free = false;
qemu_mutex_lock(&conn->mutex); nbd_client_connection_release(s->conn);
assert(!conn->detached); s->conn = NULL;
if (conn->running) {
conn->detached = true;
} else {
do_free = true;
}
qemu_mutex_unlock(&conn->mutex);
/* the runaway thread will clean up itself */
if (do_free) {
nbd_free_connect_thread(conn);
}
yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name)); yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name));
@ -372,7 +359,7 @@ nbd_client_connection_new(const SocketAddress *saddr)
return conn; return conn;
} }
static void nbd_free_connect_thread(NBDClientConnection *conn) static void nbd_client_connection_do_free(NBDClientConnection *conn)
{ {
if (conn->sioc) { if (conn->sioc) {
qio_channel_close(QIO_CHANNEL(conn->sioc), NULL); qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
@ -414,12 +401,34 @@ static void *connect_thread_func(void *opaque)
qemu_mutex_unlock(&conn->mutex); qemu_mutex_unlock(&conn->mutex);
if (do_free) { if (do_free) {
nbd_free_connect_thread(conn); nbd_client_connection_do_free(conn);
} }
return NULL; return NULL;
} }
static void nbd_client_connection_release(NBDClientConnection *conn)
{
bool do_free = false;
if (!conn) {
return;
}
qemu_mutex_lock(&conn->mutex);
assert(!conn->detached);
if (conn->running) {
conn->detached = true;
} else {
do_free = true;
}
qemu_mutex_unlock(&conn->mutex);
if (do_free) {
nbd_client_connection_do_free(conn);
}
}
/* /*
* Get a new connection in context of @conn: * Get a new connection in context of @conn:
* if the thread is running, wait for completion * if the thread is running, wait for completion