nbd/client-connection: use QEMU_LOCK_GUARD
[qemu.git] / nbd / client-connection.c
blobeb5cae2eaea43432b553554cda4c3597b83292ae
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (c) 2021 Virtuozzo International GmbH.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
27 #include "block/nbd.h"
29 #include "qapi/qapi-visit-sockets.h"
30 #include "qapi/clone-visitor.h"
32 struct NBDClientConnection {
33 /* Initialization constants */
34 SocketAddress *saddr; /* address to connect to */
36 QemuMutex mutex;
39 * @sioc and @err represent a connection attempt. While running
40 * is true, they are only used by the connection thread, and mutex
41 * locking is not needed. Once the thread finishes,
42 * nbd_co_establish_connection then steals these pointers while
43 * under the mutex.
45 QIOChannelSocket *sioc;
46 Error *err;
48 /* All further fields are accessed only under mutex */
49 bool running; /* thread is running now */
50 bool detached; /* thread is detached and should cleanup the state */
53 * wait_co: if non-NULL, which coroutine to wake in
54 * nbd_co_establish_connection() after yield()
56 Coroutine *wait_co;
59 NBDClientConnection *nbd_client_connection_new(const SocketAddress *saddr)
61 NBDClientConnection *conn = g_new(NBDClientConnection, 1);
63 *conn = (NBDClientConnection) {
64 .saddr = QAPI_CLONE(SocketAddress, saddr),
67 qemu_mutex_init(&conn->mutex);
69 return conn;
72 static void nbd_client_connection_do_free(NBDClientConnection *conn)
74 if (conn->sioc) {
75 qio_channel_close(QIO_CHANNEL(conn->sioc), NULL);
76 object_unref(OBJECT(conn->sioc));
78 error_free(conn->err);
79 qapi_free_SocketAddress(conn->saddr);
80 g_free(conn);
83 static void *connect_thread_func(void *opaque)
85 NBDClientConnection *conn = opaque;
86 int ret;
87 bool do_free;
89 conn->sioc = qio_channel_socket_new();
91 error_free(conn->err);
92 conn->err = NULL;
93 ret = qio_channel_socket_connect_sync(conn->sioc, conn->saddr, &conn->err);
94 if (ret < 0) {
95 object_unref(OBJECT(conn->sioc));
96 conn->sioc = NULL;
99 qio_channel_set_delay(QIO_CHANNEL(conn->sioc), false);
101 qemu_mutex_lock(&conn->mutex);
103 assert(conn->running);
104 conn->running = false;
105 if (conn->wait_co) {
106 aio_co_wake(conn->wait_co);
107 conn->wait_co = NULL;
109 do_free = conn->detached;
111 qemu_mutex_unlock(&conn->mutex);
113 if (do_free) {
114 nbd_client_connection_do_free(conn);
117 return NULL;
120 void nbd_client_connection_release(NBDClientConnection *conn)
122 bool do_free = false;
124 if (!conn) {
125 return;
128 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
129 assert(!conn->detached);
130 if (conn->running) {
131 conn->detached = true;
132 } else {
133 do_free = true;
137 if (do_free) {
138 nbd_client_connection_do_free(conn);
143 * Get a new connection in context of @conn:
144 * if the thread is running, wait for completion
145 * if the thread already succeeded in the background, and user didn't get the
146 * result, just return it now
147 * otherwise the thread is not running, so start a thread and wait for
148 * completion
150 QIOChannelSocket *coroutine_fn
151 nbd_co_establish_connection(NBDClientConnection *conn, Error **errp)
153 QemuThread thread;
155 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
157 * Don't call nbd_co_establish_connection() in several coroutines in
158 * parallel. Only one call at once is supported.
160 assert(!conn->wait_co);
162 if (!conn->running) {
163 if (conn->sioc) {
164 /* Previous attempt finally succeeded in background */
165 return g_steal_pointer(&conn->sioc);
168 conn->running = true;
169 error_free(conn->err);
170 conn->err = NULL;
171 qemu_thread_create(&thread, "nbd-connect",
172 connect_thread_func, conn, QEMU_THREAD_DETACHED);
175 conn->wait_co = qemu_coroutine_self();
179 * We are going to wait for connect-thread finish, but
180 * nbd_co_establish_connection_cancel() can interrupt.
182 qemu_coroutine_yield();
184 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
185 if (conn->running) {
187 * The connection attempt was canceled and the coroutine resumed
188 * before the connection thread finished its job. Report the
189 * attempt as failed, but leave the connection thread running,
190 * to reuse it for the next connection attempt.
192 error_setg(errp, "Connection attempt cancelled by other operation");
193 return NULL;
194 } else {
195 error_propagate(errp, conn->err);
196 conn->err = NULL;
197 return g_steal_pointer(&conn->sioc);
201 abort(); /* unreachable */
205 * nbd_co_establish_connection_cancel
206 * Cancel nbd_co_establish_connection() asynchronously.
208 * Note that this function neither directly stops the thread nor closes the
209 * socket, but rather safely wakes nbd_co_establish_connection() which is
210 * sleeping in yield()
212 void nbd_co_establish_connection_cancel(NBDClientConnection *conn)
214 Coroutine *wait_co;
216 WITH_QEMU_LOCK_GUARD(&conn->mutex) {
217 wait_co = g_steal_pointer(&conn->wait_co);
220 if (wait_co) {
221 aio_co_wake(wait_co);