nbd: Rename NbdClientSession to NBDClientSession
[qemu/kevin.git] / block / nbd-client.c
blobe6fe603ef3182e8e03f66325c695618160c558b0
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (C) 2016 Red Hat, Inc.
5 * Copyright (C) 2008 Bull S.A.S.
6 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
8 * Some parts:
9 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
30 #include "qemu/osdep.h"
31 #include "nbd-client.h"
33 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
34 #define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
36 static void nbd_recv_coroutines_enter_all(NBDClientSession *s)
38 int i;
40 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
41 if (s->recv_coroutine[i]) {
42 qemu_coroutine_enter(s->recv_coroutine[i]);
47 static void nbd_teardown_connection(BlockDriverState *bs)
49 NBDClientSession *client = nbd_get_client_session(bs);
51 if (!client->ioc) { /* Already closed */
52 return;
55 /* finish any pending coroutines */
56 qio_channel_shutdown(client->ioc,
57 QIO_CHANNEL_SHUTDOWN_BOTH,
58 NULL);
59 nbd_recv_coroutines_enter_all(client);
61 nbd_client_detach_aio_context(bs);
62 object_unref(OBJECT(client->sioc));
63 client->sioc = NULL;
64 object_unref(OBJECT(client->ioc));
65 client->ioc = NULL;
68 static void nbd_reply_ready(void *opaque)
70 BlockDriverState *bs = opaque;
71 NBDClientSession *s = nbd_get_client_session(bs);
72 uint64_t i;
73 int ret;
75 if (!s->ioc) { /* Already closed */
76 return;
79 if (s->reply.handle == 0) {
80 /* No reply already in flight. Fetch a header. It is possible
81 * that another thread has done the same thing in parallel, so
82 * the socket is not readable anymore.
84 ret = nbd_receive_reply(s->ioc, &s->reply);
85 if (ret == -EAGAIN) {
86 return;
88 if (ret < 0) {
89 s->reply.handle = 0;
90 goto fail;
94 /* There's no need for a mutex on the receive side, because the
95 * handler acts as a synchronization point and ensures that only
96 * one coroutine is called until the reply finishes. */
97 i = HANDLE_TO_INDEX(s, s->reply.handle);
98 if (i >= MAX_NBD_REQUESTS) {
99 goto fail;
102 if (s->recv_coroutine[i]) {
103 qemu_coroutine_enter(s->recv_coroutine[i]);
104 return;
107 fail:
108 nbd_teardown_connection(bs);
111 static void nbd_restart_write(void *opaque)
113 BlockDriverState *bs = opaque;
115 qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine);
118 static int nbd_co_send_request(BlockDriverState *bs,
119 struct nbd_request *request,
120 QEMUIOVector *qiov)
122 NBDClientSession *s = nbd_get_client_session(bs);
123 AioContext *aio_context;
124 int rc, ret, i;
126 qemu_co_mutex_lock(&s->send_mutex);
128 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
129 if (s->recv_coroutine[i] == NULL) {
130 s->recv_coroutine[i] = qemu_coroutine_self();
131 break;
135 g_assert(qemu_in_coroutine());
136 assert(i < MAX_NBD_REQUESTS);
137 request->handle = INDEX_TO_HANDLE(s, i);
139 if (!s->ioc) {
140 qemu_co_mutex_unlock(&s->send_mutex);
141 return -EPIPE;
144 s->send_coroutine = qemu_coroutine_self();
145 aio_context = bdrv_get_aio_context(bs);
147 aio_set_fd_handler(aio_context, s->sioc->fd, false,
148 nbd_reply_ready, nbd_restart_write, bs);
149 if (qiov) {
150 qio_channel_set_cork(s->ioc, true);
151 rc = nbd_send_request(s->ioc, request);
152 if (rc >= 0) {
153 ret = nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov, request->len,
154 false);
155 if (ret != request->len) {
156 rc = -EIO;
159 qio_channel_set_cork(s->ioc, false);
160 } else {
161 rc = nbd_send_request(s->ioc, request);
163 aio_set_fd_handler(aio_context, s->sioc->fd, false,
164 nbd_reply_ready, NULL, bs);
165 s->send_coroutine = NULL;
166 qemu_co_mutex_unlock(&s->send_mutex);
167 return rc;
170 static void nbd_co_receive_reply(NBDClientSession *s,
171 struct nbd_request *request,
172 struct nbd_reply *reply,
173 QEMUIOVector *qiov)
175 int ret;
177 /* Wait until we're woken up by the read handler. TODO: perhaps
178 * peek at the next reply and avoid yielding if it's ours? */
179 qemu_coroutine_yield();
180 *reply = s->reply;
181 if (reply->handle != request->handle ||
182 !s->ioc) {
183 reply->error = EIO;
184 } else {
185 if (qiov && reply->error == 0) {
186 ret = nbd_wr_syncv(s->ioc, qiov->iov, qiov->niov, request->len,
187 true);
188 if (ret != request->len) {
189 reply->error = EIO;
193 /* Tell the read handler to read another header. */
194 s->reply.handle = 0;
198 static void nbd_coroutine_start(NBDClientSession *s,
199 struct nbd_request *request)
201 /* Poor man semaphore. The free_sema is locked when no other request
202 * can be accepted, and unlocked after receiving one reply. */
203 if (s->in_flight == MAX_NBD_REQUESTS) {
204 qemu_co_queue_wait(&s->free_sema);
205 assert(s->in_flight < MAX_NBD_REQUESTS);
207 s->in_flight++;
209 /* s->recv_coroutine[i] is set as soon as we get the send_lock. */
212 static void nbd_coroutine_end(NBDClientSession *s,
213 struct nbd_request *request)
215 int i = HANDLE_TO_INDEX(s, request->handle);
216 s->recv_coroutine[i] = NULL;
217 if (s->in_flight-- == MAX_NBD_REQUESTS) {
218 qemu_co_queue_next(&s->free_sema);
222 int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
223 uint64_t bytes, QEMUIOVector *qiov, int flags)
225 NBDClientSession *client = nbd_get_client_session(bs);
226 struct nbd_request request = {
227 .type = NBD_CMD_READ,
228 .from = offset,
229 .len = bytes,
231 struct nbd_reply reply;
232 ssize_t ret;
234 assert(bytes <= NBD_MAX_BUFFER_SIZE);
235 assert(!flags);
237 nbd_coroutine_start(client, &request);
238 ret = nbd_co_send_request(bs, &request, NULL);
239 if (ret < 0) {
240 reply.error = -ret;
241 } else {
242 nbd_co_receive_reply(client, &request, &reply, qiov);
244 nbd_coroutine_end(client, &request);
245 return -reply.error;
248 int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
249 uint64_t bytes, QEMUIOVector *qiov, int flags)
251 NBDClientSession *client = nbd_get_client_session(bs);
252 struct nbd_request request = {
253 .type = NBD_CMD_WRITE,
254 .from = offset,
255 .len = bytes,
257 struct nbd_reply reply;
258 ssize_t ret;
260 if (flags & BDRV_REQ_FUA) {
261 assert(client->nbdflags & NBD_FLAG_SEND_FUA);
262 request.flags |= NBD_CMD_FLAG_FUA;
265 assert(bytes <= NBD_MAX_BUFFER_SIZE);
267 nbd_coroutine_start(client, &request);
268 ret = nbd_co_send_request(bs, &request, qiov);
269 if (ret < 0) {
270 reply.error = -ret;
271 } else {
272 nbd_co_receive_reply(client, &request, &reply, NULL);
274 nbd_coroutine_end(client, &request);
275 return -reply.error;
278 int nbd_client_co_flush(BlockDriverState *bs)
280 NBDClientSession *client = nbd_get_client_session(bs);
281 struct nbd_request request = { .type = NBD_CMD_FLUSH };
282 struct nbd_reply reply;
283 ssize_t ret;
285 if (!(client->nbdflags & NBD_FLAG_SEND_FLUSH)) {
286 return 0;
289 request.from = 0;
290 request.len = 0;
292 nbd_coroutine_start(client, &request);
293 ret = nbd_co_send_request(bs, &request, NULL);
294 if (ret < 0) {
295 reply.error = -ret;
296 } else {
297 nbd_co_receive_reply(client, &request, &reply, NULL);
299 nbd_coroutine_end(client, &request);
300 return -reply.error;
303 int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int count)
305 NBDClientSession *client = nbd_get_client_session(bs);
306 struct nbd_request request = {
307 .type = NBD_CMD_TRIM,
308 .from = offset,
309 .len = count,
311 struct nbd_reply reply;
312 ssize_t ret;
314 if (!(client->nbdflags & NBD_FLAG_SEND_TRIM)) {
315 return 0;
318 nbd_coroutine_start(client, &request);
319 ret = nbd_co_send_request(bs, &request, NULL);
320 if (ret < 0) {
321 reply.error = -ret;
322 } else {
323 nbd_co_receive_reply(client, &request, &reply, NULL);
325 nbd_coroutine_end(client, &request);
326 return -reply.error;
330 void nbd_client_detach_aio_context(BlockDriverState *bs)
332 aio_set_fd_handler(bdrv_get_aio_context(bs),
333 nbd_get_client_session(bs)->sioc->fd,
334 false, NULL, NULL, NULL);
337 void nbd_client_attach_aio_context(BlockDriverState *bs,
338 AioContext *new_context)
340 aio_set_fd_handler(new_context, nbd_get_client_session(bs)->sioc->fd,
341 false, nbd_reply_ready, NULL, bs);
344 void nbd_client_close(BlockDriverState *bs)
346 NBDClientSession *client = nbd_get_client_session(bs);
347 struct nbd_request request = { .type = NBD_CMD_DISC };
349 if (client->ioc == NULL) {
350 return;
353 nbd_send_request(client->ioc, &request);
355 nbd_teardown_connection(bs);
358 int nbd_client_init(BlockDriverState *bs,
359 QIOChannelSocket *sioc,
360 const char *export,
361 QCryptoTLSCreds *tlscreds,
362 const char *hostname,
363 Error **errp)
365 NBDClientSession *client = nbd_get_client_session(bs);
366 int ret;
368 /* NBD handshake */
369 logout("session init %s\n", export);
370 qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL);
372 ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), export,
373 &client->nbdflags,
374 tlscreds, hostname,
375 &client->ioc,
376 &client->size, errp);
377 if (ret < 0) {
378 logout("Failed to negotiate with the NBD server\n");
379 return ret;
381 if (client->nbdflags & NBD_FLAG_SEND_FUA) {
382 bs->supported_write_flags = BDRV_REQ_FUA;
385 qemu_co_mutex_init(&client->send_mutex);
386 qemu_co_queue_init(&client->free_sema);
387 client->sioc = sioc;
388 object_ref(OBJECT(client->sioc));
390 if (!client->ioc) {
391 client->ioc = QIO_CHANNEL(sioc);
392 object_ref(OBJECT(client->ioc));
395 /* Now that we're connected, set the socket to be non-blocking and
396 * kick the reply mechanism. */
397 qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
399 nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs));
401 logout("Established connection with NBD server\n");
402 return 0;