Merge remote-tracking branch 'remotes/amarkovic/tags/mips-queue-jun-09-2020' into...
[qemu/ar7.git] / block / nbd.c
blob4ac23c8f62994c025467f3958a6870ea2db89efd
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (c) 2019 Virtuozzo International GmbH.
5 * Copyright (C) 2016 Red Hat, Inc.
6 * Copyright (C) 2008 Bull S.A.S.
7 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
9 * Some parts:
10 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
12 * Permission is hereby granted, free of charge, to any person obtaining a copy
13 * of this software and associated documentation files (the "Software"), to deal
14 * in the Software without restriction, including without limitation the rights
15 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 * copies of the Software, and to permit persons to whom the Software is
17 * furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 * THE SOFTWARE.
31 #include "qemu/osdep.h"
33 #include "trace.h"
34 #include "qemu/uri.h"
35 #include "qemu/option.h"
36 #include "qemu/cutils.h"
37 #include "qemu/main-loop.h"
39 #include "qapi/qapi-visit-sockets.h"
40 #include "qapi/qmp/qstring.h"
42 #include "block/qdict.h"
43 #include "block/nbd.h"
44 #include "block/block_int.h"
46 #define EN_OPTSTR ":exportname="
47 #define MAX_NBD_REQUESTS 16
49 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
50 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
52 typedef struct {
53 Coroutine *coroutine;
54 uint64_t offset; /* original offset of the request */
55 bool receiving; /* waiting for connection_co? */
56 } NBDClientRequest;
58 typedef enum NBDClientState {
59 NBD_CLIENT_CONNECTING_WAIT,
60 NBD_CLIENT_CONNECTING_NOWAIT,
61 NBD_CLIENT_CONNECTED,
62 NBD_CLIENT_QUIT
63 } NBDClientState;
65 typedef struct BDRVNBDState {
66 QIOChannelSocket *sioc; /* The master data channel */
67 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
68 NBDExportInfo info;
70 CoMutex send_mutex;
71 CoQueue free_sema;
72 Coroutine *connection_co;
73 Coroutine *teardown_co;
74 QemuCoSleepState *connection_co_sleep_ns_state;
75 bool drained;
76 bool wait_drained_end;
77 int in_flight;
78 NBDClientState state;
79 int connect_status;
80 Error *connect_err;
81 bool wait_in_flight;
83 NBDClientRequest requests[MAX_NBD_REQUESTS];
84 NBDReply reply;
85 BlockDriverState *bs;
87 /* Connection parameters */
88 uint32_t reconnect_delay;
89 SocketAddress *saddr;
90 char *export, *tlscredsid;
91 QCryptoTLSCreds *tlscreds;
92 const char *hostname;
93 char *x_dirty_bitmap;
94 } BDRVNBDState;
96 static int nbd_client_connect(BlockDriverState *bs, Error **errp);
98 static void nbd_clear_bdrvstate(BDRVNBDState *s)
100 object_unref(OBJECT(s->tlscreds));
101 qapi_free_SocketAddress(s->saddr);
102 s->saddr = NULL;
103 g_free(s->export);
104 s->export = NULL;
105 g_free(s->tlscredsid);
106 s->tlscredsid = NULL;
107 g_free(s->x_dirty_bitmap);
108 s->x_dirty_bitmap = NULL;
111 static void nbd_channel_error(BDRVNBDState *s, int ret)
113 if (ret == -EIO) {
114 if (s->state == NBD_CLIENT_CONNECTED) {
115 s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
116 NBD_CLIENT_CONNECTING_NOWAIT;
118 } else {
119 if (s->state == NBD_CLIENT_CONNECTED) {
120 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
122 s->state = NBD_CLIENT_QUIT;
126 static void nbd_recv_coroutines_wake_all(BDRVNBDState *s)
128 int i;
130 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
131 NBDClientRequest *req = &s->requests[i];
133 if (req->coroutine && req->receiving) {
134 aio_co_wake(req->coroutine);
139 static void nbd_client_detach_aio_context(BlockDriverState *bs)
141 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
143 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
146 static void nbd_client_attach_aio_context_bh(void *opaque)
148 BlockDriverState *bs = opaque;
149 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
152 * The node is still drained, so we know the coroutine has yielded in
153 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
154 * entered for the first time. Both places are safe for entering the
155 * coroutine.
157 qemu_aio_coroutine_enter(bs->aio_context, s->connection_co);
158 bdrv_dec_in_flight(bs);
161 static void nbd_client_attach_aio_context(BlockDriverState *bs,
162 AioContext *new_context)
164 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
167 * s->connection_co is either yielded from nbd_receive_reply or from
168 * nbd_co_reconnect_loop()
170 if (s->state == NBD_CLIENT_CONNECTED) {
171 qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context);
174 bdrv_inc_in_flight(bs);
177 * Need to wait here for the BH to run because the BH must run while the
178 * node is still drained.
180 aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs);
183 static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
185 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
187 s->drained = true;
188 if (s->connection_co_sleep_ns_state) {
189 qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
193 static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs)
195 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
197 s->drained = false;
198 if (s->wait_drained_end) {
199 s->wait_drained_end = false;
200 aio_co_wake(s->connection_co);
205 static void nbd_teardown_connection(BlockDriverState *bs)
207 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
209 if (s->state == NBD_CLIENT_CONNECTED) {
210 /* finish any pending coroutines */
211 assert(s->ioc);
212 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
214 s->state = NBD_CLIENT_QUIT;
215 if (s->connection_co) {
216 if (s->connection_co_sleep_ns_state) {
217 qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
220 if (qemu_in_coroutine()) {
221 s->teardown_co = qemu_coroutine_self();
222 /* connection_co resumes us when it terminates */
223 qemu_coroutine_yield();
224 s->teardown_co = NULL;
225 } else {
226 BDRV_POLL_WHILE(bs, s->connection_co);
228 assert(!s->connection_co);
231 static bool nbd_client_connecting(BDRVNBDState *s)
233 return s->state == NBD_CLIENT_CONNECTING_WAIT ||
234 s->state == NBD_CLIENT_CONNECTING_NOWAIT;
237 static bool nbd_client_connecting_wait(BDRVNBDState *s)
239 return s->state == NBD_CLIENT_CONNECTING_WAIT;
242 static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
244 Error *local_err = NULL;
246 if (!nbd_client_connecting(s)) {
247 return;
250 /* Wait for completion of all in-flight requests */
252 qemu_co_mutex_lock(&s->send_mutex);
254 while (s->in_flight > 0) {
255 qemu_co_mutex_unlock(&s->send_mutex);
256 nbd_recv_coroutines_wake_all(s);
257 s->wait_in_flight = true;
258 qemu_coroutine_yield();
259 s->wait_in_flight = false;
260 qemu_co_mutex_lock(&s->send_mutex);
263 qemu_co_mutex_unlock(&s->send_mutex);
265 if (!nbd_client_connecting(s)) {
266 return;
270 * Now we are sure that nobody is accessing the channel, and no one will
271 * try until we set the state to CONNECTED.
274 /* Finalize previous connection if any */
275 if (s->ioc) {
276 nbd_client_detach_aio_context(s->bs);
277 object_unref(OBJECT(s->sioc));
278 s->sioc = NULL;
279 object_unref(OBJECT(s->ioc));
280 s->ioc = NULL;
283 s->connect_status = nbd_client_connect(s->bs, &local_err);
284 error_free(s->connect_err);
285 s->connect_err = NULL;
286 error_propagate(&s->connect_err, local_err);
288 if (s->connect_status < 0) {
289 /* failed attempt */
290 return;
293 /* successfully connected */
294 s->state = NBD_CLIENT_CONNECTED;
295 qemu_co_queue_restart_all(&s->free_sema);
298 static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
300 uint64_t start_time_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
301 uint64_t delay_ns = s->reconnect_delay * NANOSECONDS_PER_SECOND;
302 uint64_t timeout = 1 * NANOSECONDS_PER_SECOND;
303 uint64_t max_timeout = 16 * NANOSECONDS_PER_SECOND;
305 nbd_reconnect_attempt(s);
307 while (nbd_client_connecting(s)) {
308 if (s->state == NBD_CLIENT_CONNECTING_WAIT &&
309 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - start_time_ns > delay_ns)
311 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
312 qemu_co_queue_restart_all(&s->free_sema);
315 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
316 &s->connection_co_sleep_ns_state);
317 if (s->drained) {
318 bdrv_dec_in_flight(s->bs);
319 s->wait_drained_end = true;
320 while (s->drained) {
322 * We may be entered once from nbd_client_attach_aio_context_bh
323 * and then from nbd_client_co_drain_end. So here is a loop.
325 qemu_coroutine_yield();
327 bdrv_inc_in_flight(s->bs);
329 if (timeout < max_timeout) {
330 timeout *= 2;
333 nbd_reconnect_attempt(s);
337 static coroutine_fn void nbd_connection_entry(void *opaque)
339 BDRVNBDState *s = opaque;
340 uint64_t i;
341 int ret = 0;
342 Error *local_err = NULL;
344 while (s->state != NBD_CLIENT_QUIT) {
346 * The NBD client can only really be considered idle when it has
347 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
348 * the point where the additional scheduled coroutine entry happens
349 * after nbd_client_attach_aio_context().
351 * Therefore we keep an additional in_flight reference all the time and
352 * only drop it temporarily here.
355 if (nbd_client_connecting(s)) {
356 nbd_co_reconnect_loop(s);
359 if (s->state != NBD_CLIENT_CONNECTED) {
360 continue;
363 assert(s->reply.handle == 0);
364 ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err);
366 if (local_err) {
367 trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err));
368 error_free(local_err);
369 local_err = NULL;
371 if (ret <= 0) {
372 nbd_channel_error(s, ret ? ret : -EIO);
373 continue;
377 * There's no need for a mutex on the receive side, because the
378 * handler acts as a synchronization point and ensures that only
379 * one coroutine is called until the reply finishes.
381 i = HANDLE_TO_INDEX(s, s->reply.handle);
382 if (i >= MAX_NBD_REQUESTS ||
383 !s->requests[i].coroutine ||
384 !s->requests[i].receiving ||
385 (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply))
387 nbd_channel_error(s, -EINVAL);
388 continue;
392 * We're woken up again by the request itself. Note that there
393 * is no race between yielding and reentering connection_co. This
394 * is because:
396 * - if the request runs on the same AioContext, it is only
397 * entered after we yield
399 * - if the request runs on a different AioContext, reentering
400 * connection_co happens through a bottom half, which can only
401 * run after we yield.
403 aio_co_wake(s->requests[i].coroutine);
404 qemu_coroutine_yield();
407 qemu_co_queue_restart_all(&s->free_sema);
408 nbd_recv_coroutines_wake_all(s);
409 bdrv_dec_in_flight(s->bs);
411 s->connection_co = NULL;
412 if (s->ioc) {
413 nbd_client_detach_aio_context(s->bs);
414 object_unref(OBJECT(s->sioc));
415 s->sioc = NULL;
416 object_unref(OBJECT(s->ioc));
417 s->ioc = NULL;
420 if (s->teardown_co) {
421 aio_co_wake(s->teardown_co);
423 aio_wait_kick();
426 static int nbd_co_send_request(BlockDriverState *bs,
427 NBDRequest *request,
428 QEMUIOVector *qiov)
430 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
431 int rc, i = -1;
433 qemu_co_mutex_lock(&s->send_mutex);
434 while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) {
435 qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
438 if (s->state != NBD_CLIENT_CONNECTED) {
439 rc = -EIO;
440 goto err;
443 s->in_flight++;
445 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
446 if (s->requests[i].coroutine == NULL) {
447 break;
451 g_assert(qemu_in_coroutine());
452 assert(i < MAX_NBD_REQUESTS);
454 s->requests[i].coroutine = qemu_coroutine_self();
455 s->requests[i].offset = request->from;
456 s->requests[i].receiving = false;
458 request->handle = INDEX_TO_HANDLE(s, i);
460 assert(s->ioc);
462 if (qiov) {
463 qio_channel_set_cork(s->ioc, true);
464 rc = nbd_send_request(s->ioc, request);
465 if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) {
466 if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
467 NULL) < 0) {
468 rc = -EIO;
470 } else if (rc >= 0) {
471 rc = -EIO;
473 qio_channel_set_cork(s->ioc, false);
474 } else {
475 rc = nbd_send_request(s->ioc, request);
478 err:
479 if (rc < 0) {
480 nbd_channel_error(s, rc);
481 if (i != -1) {
482 s->requests[i].coroutine = NULL;
483 s->in_flight--;
485 if (s->in_flight == 0 && s->wait_in_flight) {
486 aio_co_wake(s->connection_co);
487 } else {
488 qemu_co_queue_next(&s->free_sema);
491 qemu_co_mutex_unlock(&s->send_mutex);
492 return rc;
495 static inline uint16_t payload_advance16(uint8_t **payload)
497 *payload += 2;
498 return lduw_be_p(*payload - 2);
501 static inline uint32_t payload_advance32(uint8_t **payload)
503 *payload += 4;
504 return ldl_be_p(*payload - 4);
507 static inline uint64_t payload_advance64(uint8_t **payload)
509 *payload += 8;
510 return ldq_be_p(*payload - 8);
513 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
514 NBDStructuredReplyChunk *chunk,
515 uint8_t *payload, uint64_t orig_offset,
516 QEMUIOVector *qiov, Error **errp)
518 uint64_t offset;
519 uint32_t hole_size;
521 if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
522 error_setg(errp, "Protocol error: invalid payload for "
523 "NBD_REPLY_TYPE_OFFSET_HOLE");
524 return -EINVAL;
527 offset = payload_advance64(&payload);
528 hole_size = payload_advance32(&payload);
530 if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
531 offset > orig_offset + qiov->size - hole_size) {
532 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
533 " region");
534 return -EINVAL;
536 if (s->info.min_block &&
537 !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
538 trace_nbd_structured_read_compliance("hole");
541 qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
543 return 0;
547 * nbd_parse_blockstatus_payload
548 * Based on our request, we expect only one extent in reply, for the
549 * base:allocation context.
551 static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
552 NBDStructuredReplyChunk *chunk,
553 uint8_t *payload, uint64_t orig_length,
554 NBDExtent *extent, Error **errp)
556 uint32_t context_id;
558 /* The server succeeded, so it must have sent [at least] one extent */
559 if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
560 error_setg(errp, "Protocol error: invalid payload for "
561 "NBD_REPLY_TYPE_BLOCK_STATUS");
562 return -EINVAL;
565 context_id = payload_advance32(&payload);
566 if (s->info.context_id != context_id) {
567 error_setg(errp, "Protocol error: unexpected context id %d for "
568 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
569 "id is %d", context_id,
570 s->info.context_id);
571 return -EINVAL;
574 extent->length = payload_advance32(&payload);
575 extent->flags = payload_advance32(&payload);
577 if (extent->length == 0) {
578 error_setg(errp, "Protocol error: server sent status chunk with "
579 "zero length");
580 return -EINVAL;
584 * A server sending unaligned block status is in violation of the
585 * protocol, but as qemu-nbd 3.1 is such a server (at least for
586 * POSIX files that are not a multiple of 512 bytes, since qemu
587 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
588 * still sees an implicit hole beyond the real EOF), it's nicer to
589 * work around the misbehaving server. If the request included
590 * more than the final unaligned block, truncate it back to an
591 * aligned result; if the request was only the final block, round
592 * up to the full block and change the status to fully-allocated
593 * (always a safe status, even if it loses information).
595 if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
596 s->info.min_block)) {
597 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
598 if (extent->length > s->info.min_block) {
599 extent->length = QEMU_ALIGN_DOWN(extent->length,
600 s->info.min_block);
601 } else {
602 extent->length = s->info.min_block;
603 extent->flags = 0;
608 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
609 * sent us any more than one extent, nor should it have included
610 * status beyond our request in that extent. However, it's easy
611 * enough to ignore the server's noncompliance without killing the
612 * connection; just ignore trailing extents, and clamp things to
613 * the length of our request.
615 if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
616 trace_nbd_parse_blockstatus_compliance("more than one extent");
618 if (extent->length > orig_length) {
619 extent->length = orig_length;
620 trace_nbd_parse_blockstatus_compliance("extent length too large");
623 return 0;
627 * nbd_parse_error_payload
628 * on success @errp contains message describing nbd error reply
630 static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
631 uint8_t *payload, int *request_ret,
632 Error **errp)
634 uint32_t error;
635 uint16_t message_size;
637 assert(chunk->type & (1 << 15));
639 if (chunk->length < sizeof(error) + sizeof(message_size)) {
640 error_setg(errp,
641 "Protocol error: invalid payload for structured error");
642 return -EINVAL;
645 error = nbd_errno_to_system_errno(payload_advance32(&payload));
646 if (error == 0) {
647 error_setg(errp, "Protocol error: server sent structured error chunk "
648 "with error = 0");
649 return -EINVAL;
652 *request_ret = -error;
653 message_size = payload_advance16(&payload);
655 if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
656 error_setg(errp, "Protocol error: server sent structured error chunk "
657 "with incorrect message size");
658 return -EINVAL;
661 /* TODO: Add a trace point to mention the server complaint */
663 /* TODO handle ERROR_OFFSET */
665 return 0;
668 static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
669 uint64_t orig_offset,
670 QEMUIOVector *qiov, Error **errp)
672 QEMUIOVector sub_qiov;
673 uint64_t offset;
674 size_t data_size;
675 int ret;
676 NBDStructuredReplyChunk *chunk = &s->reply.structured;
678 assert(nbd_reply_is_structured(&s->reply));
680 /* The NBD spec requires at least one byte of payload */
681 if (chunk->length <= sizeof(offset)) {
682 error_setg(errp, "Protocol error: invalid payload for "
683 "NBD_REPLY_TYPE_OFFSET_DATA");
684 return -EINVAL;
687 if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
688 return -EIO;
691 data_size = chunk->length - sizeof(offset);
692 assert(data_size);
693 if (offset < orig_offset || data_size > qiov->size ||
694 offset > orig_offset + qiov->size - data_size) {
695 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
696 " region");
697 return -EINVAL;
699 if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
700 trace_nbd_structured_read_compliance("data");
703 qemu_iovec_init(&sub_qiov, qiov->niov);
704 qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
705 ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
706 qemu_iovec_destroy(&sub_qiov);
708 return ret < 0 ? -EIO : 0;
711 #define NBD_MAX_MALLOC_PAYLOAD 1000
712 static coroutine_fn int nbd_co_receive_structured_payload(
713 BDRVNBDState *s, void **payload, Error **errp)
715 int ret;
716 uint32_t len;
718 assert(nbd_reply_is_structured(&s->reply));
720 len = s->reply.structured.length;
722 if (len == 0) {
723 return 0;
726 if (payload == NULL) {
727 error_setg(errp, "Unexpected structured payload");
728 return -EINVAL;
731 if (len > NBD_MAX_MALLOC_PAYLOAD) {
732 error_setg(errp, "Payload too large");
733 return -EINVAL;
736 *payload = g_new(char, len);
737 ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
738 if (ret < 0) {
739 g_free(*payload);
740 *payload = NULL;
741 return ret;
744 return 0;
748 * nbd_co_do_receive_one_chunk
749 * for simple reply:
750 * set request_ret to received reply error
751 * if qiov is not NULL: read payload to @qiov
752 * for structured reply chunk:
753 * if error chunk: read payload, set @request_ret, do not set @payload
754 * else if offset_data chunk: read payload data to @qiov, do not set @payload
755 * else: read payload to @payload
757 * If function fails, @errp contains corresponding error message, and the
758 * connection with the server is suspect. If it returns 0, then the
759 * transaction succeeded (although @request_ret may be a negative errno
760 * corresponding to the server's error reply), and errp is unchanged.
762 static coroutine_fn int nbd_co_do_receive_one_chunk(
763 BDRVNBDState *s, uint64_t handle, bool only_structured,
764 int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
766 int ret;
767 int i = HANDLE_TO_INDEX(s, handle);
768 void *local_payload = NULL;
769 NBDStructuredReplyChunk *chunk;
771 if (payload) {
772 *payload = NULL;
774 *request_ret = 0;
776 /* Wait until we're woken up by nbd_connection_entry. */
777 s->requests[i].receiving = true;
778 qemu_coroutine_yield();
779 s->requests[i].receiving = false;
780 if (s->state != NBD_CLIENT_CONNECTED) {
781 error_setg(errp, "Connection closed");
782 return -EIO;
784 assert(s->ioc);
786 assert(s->reply.handle == handle);
788 if (nbd_reply_is_simple(&s->reply)) {
789 if (only_structured) {
790 error_setg(errp, "Protocol error: simple reply when structured "
791 "reply chunk was expected");
792 return -EINVAL;
795 *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
796 if (*request_ret < 0 || !qiov) {
797 return 0;
800 return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
801 errp) < 0 ? -EIO : 0;
804 /* handle structured reply chunk */
805 assert(s->info.structured_reply);
806 chunk = &s->reply.structured;
808 if (chunk->type == NBD_REPLY_TYPE_NONE) {
809 if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
810 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
811 " NBD_REPLY_FLAG_DONE flag set");
812 return -EINVAL;
814 if (chunk->length) {
815 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
816 " nonzero length");
817 return -EINVAL;
819 return 0;
822 if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
823 if (!qiov) {
824 error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
825 return -EINVAL;
828 return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
829 qiov, errp);
832 if (nbd_reply_type_is_error(chunk->type)) {
833 payload = &local_payload;
836 ret = nbd_co_receive_structured_payload(s, payload, errp);
837 if (ret < 0) {
838 return ret;
841 if (nbd_reply_type_is_error(chunk->type)) {
842 ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
843 g_free(local_payload);
844 return ret;
847 return 0;
851 * nbd_co_receive_one_chunk
852 * Read reply, wake up connection_co and set s->quit if needed.
853 * Return value is a fatal error code or normal nbd reply error code
855 static coroutine_fn int nbd_co_receive_one_chunk(
856 BDRVNBDState *s, uint64_t handle, bool only_structured,
857 int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
858 Error **errp)
860 int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
861 request_ret, qiov, payload, errp);
863 if (ret < 0) {
864 memset(reply, 0, sizeof(*reply));
865 nbd_channel_error(s, ret);
866 } else {
867 /* For assert at loop start in nbd_connection_entry */
868 *reply = s->reply;
870 s->reply.handle = 0;
872 if (s->connection_co && !s->wait_in_flight) {
874 * We must check s->wait_in_flight, because we may entered by
875 * nbd_recv_coroutines_wake_all(), in this case we should not
876 * wake connection_co here, it will woken by last request.
878 aio_co_wake(s->connection_co);
881 return ret;
884 typedef struct NBDReplyChunkIter {
885 int ret;
886 int request_ret;
887 Error *err;
888 bool done, only_structured;
889 } NBDReplyChunkIter;
891 static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
892 int ret, Error **local_err)
894 assert(local_err && *local_err);
895 assert(ret < 0);
897 if (!iter->ret) {
898 iter->ret = ret;
899 error_propagate(&iter->err, *local_err);
900 } else {
901 error_free(*local_err);
904 *local_err = NULL;
907 static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
909 assert(ret < 0);
911 if (!iter->request_ret) {
912 iter->request_ret = ret;
917 * NBD_FOREACH_REPLY_CHUNK
918 * The pointer stored in @payload requires g_free() to free it.
920 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
921 qiov, reply, payload) \
922 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
923 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
926 * nbd_reply_chunk_iter_receive
927 * The pointer stored in @payload requires g_free() to free it.
929 static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
930 NBDReplyChunkIter *iter,
931 uint64_t handle,
932 QEMUIOVector *qiov, NBDReply *reply,
933 void **payload)
935 int ret, request_ret;
936 NBDReply local_reply;
937 NBDStructuredReplyChunk *chunk;
938 Error *local_err = NULL;
939 if (s->state != NBD_CLIENT_CONNECTED) {
940 error_setg(&local_err, "Connection closed");
941 nbd_iter_channel_error(iter, -EIO, &local_err);
942 goto break_loop;
945 if (iter->done) {
946 /* Previous iteration was last. */
947 goto break_loop;
950 if (reply == NULL) {
951 reply = &local_reply;
954 ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
955 &request_ret, qiov, reply, payload,
956 &local_err);
957 if (ret < 0) {
958 nbd_iter_channel_error(iter, ret, &local_err);
959 } else if (request_ret < 0) {
960 nbd_iter_request_error(iter, request_ret);
963 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
964 if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) {
965 goto break_loop;
968 chunk = &reply->structured;
969 iter->only_structured = true;
971 if (chunk->type == NBD_REPLY_TYPE_NONE) {
972 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
973 assert(chunk->flags & NBD_REPLY_FLAG_DONE);
974 goto break_loop;
977 if (chunk->flags & NBD_REPLY_FLAG_DONE) {
978 /* This iteration is last. */
979 iter->done = true;
982 /* Execute the loop body */
983 return true;
985 break_loop:
986 s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
988 qemu_co_mutex_lock(&s->send_mutex);
989 s->in_flight--;
990 if (s->in_flight == 0 && s->wait_in_flight) {
991 aio_co_wake(s->connection_co);
992 } else {
993 qemu_co_queue_next(&s->free_sema);
995 qemu_co_mutex_unlock(&s->send_mutex);
997 return false;
1000 static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
1001 int *request_ret, Error **errp)
1003 NBDReplyChunkIter iter;
1005 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
1006 /* nbd_reply_chunk_iter_receive does all the work */
1009 error_propagate(errp, iter.err);
1010 *request_ret = iter.request_ret;
1011 return iter.ret;
1014 static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
1015 uint64_t offset, QEMUIOVector *qiov,
1016 int *request_ret, Error **errp)
1018 NBDReplyChunkIter iter;
1019 NBDReply reply;
1020 void *payload = NULL;
1021 Error *local_err = NULL;
1023 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
1024 qiov, &reply, &payload)
1026 int ret;
1027 NBDStructuredReplyChunk *chunk = &reply.structured;
1029 assert(nbd_reply_is_structured(&reply));
1031 switch (chunk->type) {
1032 case NBD_REPLY_TYPE_OFFSET_DATA:
1034 * special cased in nbd_co_receive_one_chunk, data is already
1035 * in qiov
1037 break;
1038 case NBD_REPLY_TYPE_OFFSET_HOLE:
1039 ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
1040 offset, qiov, &local_err);
1041 if (ret < 0) {
1042 nbd_channel_error(s, ret);
1043 nbd_iter_channel_error(&iter, ret, &local_err);
1045 break;
1046 default:
1047 if (!nbd_reply_type_is_error(chunk->type)) {
1048 /* not allowed reply type */
1049 nbd_channel_error(s, -EINVAL);
1050 error_setg(&local_err,
1051 "Unexpected reply type: %d (%s) for CMD_READ",
1052 chunk->type, nbd_reply_type_lookup(chunk->type));
1053 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1057 g_free(payload);
1058 payload = NULL;
1061 error_propagate(errp, iter.err);
1062 *request_ret = iter.request_ret;
1063 return iter.ret;
1066 static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
1067 uint64_t handle, uint64_t length,
1068 NBDExtent *extent,
1069 int *request_ret, Error **errp)
1071 NBDReplyChunkIter iter;
1072 NBDReply reply;
1073 void *payload = NULL;
1074 Error *local_err = NULL;
1075 bool received = false;
1077 assert(!extent->length);
1078 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
1079 int ret;
1080 NBDStructuredReplyChunk *chunk = &reply.structured;
1082 assert(nbd_reply_is_structured(&reply));
1084 switch (chunk->type) {
1085 case NBD_REPLY_TYPE_BLOCK_STATUS:
1086 if (received) {
1087 nbd_channel_error(s, -EINVAL);
1088 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
1089 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1091 received = true;
1093 ret = nbd_parse_blockstatus_payload(s, &reply.structured,
1094 payload, length, extent,
1095 &local_err);
1096 if (ret < 0) {
1097 nbd_channel_error(s, ret);
1098 nbd_iter_channel_error(&iter, ret, &local_err);
1100 break;
1101 default:
1102 if (!nbd_reply_type_is_error(chunk->type)) {
1103 nbd_channel_error(s, -EINVAL);
1104 error_setg(&local_err,
1105 "Unexpected reply type: %d (%s) "
1106 "for CMD_BLOCK_STATUS",
1107 chunk->type, nbd_reply_type_lookup(chunk->type));
1108 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1112 g_free(payload);
1113 payload = NULL;
1116 if (!extent->length && !iter.request_ret) {
1117 error_setg(&local_err, "Server did not reply with any status extents");
1118 nbd_iter_channel_error(&iter, -EIO, &local_err);
1121 error_propagate(errp, iter.err);
1122 *request_ret = iter.request_ret;
1123 return iter.ret;
1126 static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
1127 QEMUIOVector *write_qiov)
1129 int ret, request_ret;
1130 Error *local_err = NULL;
1131 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1133 assert(request->type != NBD_CMD_READ);
1134 if (write_qiov) {
1135 assert(request->type == NBD_CMD_WRITE);
1136 assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
1137 } else {
1138 assert(request->type != NBD_CMD_WRITE);
1141 do {
1142 ret = nbd_co_send_request(bs, request, write_qiov);
1143 if (ret < 0) {
1144 continue;
1147 ret = nbd_co_receive_return_code(s, request->handle,
1148 &request_ret, &local_err);
1149 if (local_err) {
1150 trace_nbd_co_request_fail(request->from, request->len,
1151 request->handle, request->flags,
1152 request->type,
1153 nbd_cmd_lookup(request->type),
1154 ret, error_get_pretty(local_err));
1155 error_free(local_err);
1156 local_err = NULL;
1158 } while (ret < 0 && nbd_client_connecting_wait(s));
1160 return ret ? ret : request_ret;
1163 static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
1164 uint64_t bytes, QEMUIOVector *qiov, int flags)
1166 int ret, request_ret;
1167 Error *local_err = NULL;
1168 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1169 NBDRequest request = {
1170 .type = NBD_CMD_READ,
1171 .from = offset,
1172 .len = bytes,
1175 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1176 assert(!flags);
1178 if (!bytes) {
1179 return 0;
1182 * Work around the fact that the block layer doesn't do
1183 * byte-accurate sizing yet - if the read exceeds the server's
1184 * advertised size because the block layer rounded size up, then
1185 * truncate the request to the server and tail-pad with zero.
1187 if (offset >= s->info.size) {
1188 assert(bytes < BDRV_SECTOR_SIZE);
1189 qemu_iovec_memset(qiov, 0, 0, bytes);
1190 return 0;
1192 if (offset + bytes > s->info.size) {
1193 uint64_t slop = offset + bytes - s->info.size;
1195 assert(slop < BDRV_SECTOR_SIZE);
1196 qemu_iovec_memset(qiov, bytes - slop, 0, slop);
1197 request.len -= slop;
1200 do {
1201 ret = nbd_co_send_request(bs, &request, NULL);
1202 if (ret < 0) {
1203 continue;
1206 ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
1207 &request_ret, &local_err);
1208 if (local_err) {
1209 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1210 request.flags, request.type,
1211 nbd_cmd_lookup(request.type),
1212 ret, error_get_pretty(local_err));
1213 error_free(local_err);
1214 local_err = NULL;
1216 } while (ret < 0 && nbd_client_connecting_wait(s));
1218 return ret ? ret : request_ret;
1221 static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
1222 uint64_t bytes, QEMUIOVector *qiov, int flags)
1224 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1225 NBDRequest request = {
1226 .type = NBD_CMD_WRITE,
1227 .from = offset,
1228 .len = bytes,
1231 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1232 if (flags & BDRV_REQ_FUA) {
1233 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1234 request.flags |= NBD_CMD_FLAG_FUA;
1237 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1239 if (!bytes) {
1240 return 0;
1242 return nbd_co_request(bs, &request, qiov);
1245 static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1246 int bytes, BdrvRequestFlags flags)
1248 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1249 NBDRequest request = {
1250 .type = NBD_CMD_WRITE_ZEROES,
1251 .from = offset,
1252 .len = bytes,
1255 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1256 if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1257 return -ENOTSUP;
1260 if (flags & BDRV_REQ_FUA) {
1261 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1262 request.flags |= NBD_CMD_FLAG_FUA;
1264 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1265 request.flags |= NBD_CMD_FLAG_NO_HOLE;
1267 if (flags & BDRV_REQ_NO_FALLBACK) {
1268 assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1269 request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1272 if (!bytes) {
1273 return 0;
1275 return nbd_co_request(bs, &request, NULL);
1278 static int nbd_client_co_flush(BlockDriverState *bs)
1280 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1281 NBDRequest request = { .type = NBD_CMD_FLUSH };
1283 if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1284 return 0;
1287 request.from = 0;
1288 request.len = 0;
1290 return nbd_co_request(bs, &request, NULL);
1293 static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
1294 int bytes)
1296 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1297 NBDRequest request = {
1298 .type = NBD_CMD_TRIM,
1299 .from = offset,
1300 .len = bytes,
1303 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1304 if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1305 return 0;
1308 return nbd_co_request(bs, &request, NULL);
1311 static int coroutine_fn nbd_client_co_block_status(
1312 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
1313 int64_t *pnum, int64_t *map, BlockDriverState **file)
1315 int ret, request_ret;
1316 NBDExtent extent = { 0 };
1317 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1318 Error *local_err = NULL;
1320 NBDRequest request = {
1321 .type = NBD_CMD_BLOCK_STATUS,
1322 .from = offset,
1323 .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment),
1324 MIN(bytes, s->info.size - offset)),
1325 .flags = NBD_CMD_FLAG_REQ_ONE,
1328 if (!s->info.base_allocation) {
1329 *pnum = bytes;
1330 *map = offset;
1331 *file = bs;
1332 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1336 * Work around the fact that the block layer doesn't do
1337 * byte-accurate sizing yet - if the status request exceeds the
1338 * server's advertised size because the block layer rounded size
1339 * up, we truncated the request to the server (above), or are
1340 * called on just the hole.
1342 if (offset >= s->info.size) {
1343 *pnum = bytes;
1344 assert(bytes < BDRV_SECTOR_SIZE);
1345 /* Intentionally don't report offset_valid for the hole */
1346 return BDRV_BLOCK_ZERO;
1349 if (s->info.min_block) {
1350 assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1352 do {
1353 ret = nbd_co_send_request(bs, &request, NULL);
1354 if (ret < 0) {
1355 continue;
1358 ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1359 &extent, &request_ret,
1360 &local_err);
1361 if (local_err) {
1362 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1363 request.flags, request.type,
1364 nbd_cmd_lookup(request.type),
1365 ret, error_get_pretty(local_err));
1366 error_free(local_err);
1367 local_err = NULL;
1369 } while (ret < 0 && nbd_client_connecting_wait(s));
1371 if (ret < 0 || request_ret < 0) {
1372 return ret ? ret : request_ret;
1375 assert(extent.length);
1376 *pnum = extent.length;
1377 *map = offset;
1378 *file = bs;
1379 return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1380 (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1381 BDRV_BLOCK_OFFSET_VALID;
1384 static int nbd_client_reopen_prepare(BDRVReopenState *state,
1385 BlockReopenQueue *queue, Error **errp)
1387 BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1389 if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1390 error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1391 return -EACCES;
1393 return 0;
1396 static void nbd_client_close(BlockDriverState *bs)
1398 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1399 NBDRequest request = { .type = NBD_CMD_DISC };
1401 if (s->ioc) {
1402 nbd_send_request(s->ioc, &request);
1405 nbd_teardown_connection(bs);
1408 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
1409 Error **errp)
1411 QIOChannelSocket *sioc;
1412 Error *local_err = NULL;
1414 sioc = qio_channel_socket_new();
1415 qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
1417 qio_channel_socket_connect_sync(sioc, saddr, &local_err);
1418 if (local_err) {
1419 object_unref(OBJECT(sioc));
1420 error_propagate(errp, local_err);
1421 return NULL;
1424 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
1426 return sioc;
1429 static int nbd_client_connect(BlockDriverState *bs, Error **errp)
1431 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1432 AioContext *aio_context = bdrv_get_aio_context(bs);
1433 int ret;
1436 * establish TCP connection, return error if it fails
1437 * TODO: Configurable retry-until-timeout behaviour.
1439 QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp);
1441 if (!sioc) {
1442 return -ECONNREFUSED;
1445 /* NBD handshake */
1446 trace_nbd_client_connect(s->export);
1447 qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
1448 qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
1450 s->info.request_sizes = true;
1451 s->info.structured_reply = true;
1452 s->info.base_allocation = true;
1453 s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap);
1454 s->info.name = g_strdup(s->export ?: "");
1455 ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds,
1456 s->hostname, &s->ioc, &s->info, errp);
1457 g_free(s->info.x_dirty_bitmap);
1458 g_free(s->info.name);
1459 if (ret < 0) {
1460 object_unref(OBJECT(sioc));
1461 return ret;
1463 if (s->x_dirty_bitmap && !s->info.base_allocation) {
1464 error_setg(errp, "requested x-dirty-bitmap %s not found",
1465 s->x_dirty_bitmap);
1466 ret = -EINVAL;
1467 goto fail;
1469 if (s->info.flags & NBD_FLAG_READ_ONLY) {
1470 ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
1471 if (ret < 0) {
1472 goto fail;
1475 if (s->info.flags & NBD_FLAG_SEND_FUA) {
1476 bs->supported_write_flags = BDRV_REQ_FUA;
1477 bs->supported_zero_flags |= BDRV_REQ_FUA;
1479 if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
1480 bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
1481 if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
1482 bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
1486 s->sioc = sioc;
1488 if (!s->ioc) {
1489 s->ioc = QIO_CHANNEL(sioc);
1490 object_ref(OBJECT(s->ioc));
1493 trace_nbd_client_connect_success(s->export);
1495 return 0;
1497 fail:
1499 * We have connected, but must fail for other reasons.
1500 * Send NBD_CMD_DISC as a courtesy to the server.
1503 NBDRequest request = { .type = NBD_CMD_DISC };
1505 nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request);
1507 object_unref(OBJECT(sioc));
1509 return ret;
1514 * Parse nbd_open options
1517 static int nbd_parse_uri(const char *filename, QDict *options)
1519 URI *uri;
1520 const char *p;
1521 QueryParams *qp = NULL;
1522 int ret = 0;
1523 bool is_unix;
1525 uri = uri_parse(filename);
1526 if (!uri) {
1527 return -EINVAL;
1530 /* transport */
1531 if (!g_strcmp0(uri->scheme, "nbd")) {
1532 is_unix = false;
1533 } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1534 is_unix = false;
1535 } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1536 is_unix = true;
1537 } else {
1538 ret = -EINVAL;
1539 goto out;
1542 p = uri->path ? uri->path : "";
1543 if (p[0] == '/') {
1544 p++;
1546 if (p[0]) {
1547 qdict_put_str(options, "export", p);
1550 qp = query_params_parse(uri->query);
1551 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1552 ret = -EINVAL;
1553 goto out;
1556 if (is_unix) {
1557 /* nbd+unix:///export?socket=path */
1558 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
1559 ret = -EINVAL;
1560 goto out;
1562 qdict_put_str(options, "server.type", "unix");
1563 qdict_put_str(options, "server.path", qp->p[0].value);
1564 } else {
1565 QString *host;
1566 char *port_str;
1568 /* nbd[+tcp]://host[:port]/export */
1569 if (!uri->server) {
1570 ret = -EINVAL;
1571 goto out;
1574 /* strip braces from literal IPv6 address */
1575 if (uri->server[0] == '[') {
1576 host = qstring_from_substr(uri->server, 1,
1577 strlen(uri->server) - 1);
1578 } else {
1579 host = qstring_from_str(uri->server);
1582 qdict_put_str(options, "server.type", "inet");
1583 qdict_put(options, "server.host", host);
1585 port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
1586 qdict_put_str(options, "server.port", port_str);
1587 g_free(port_str);
1590 out:
1591 if (qp) {
1592 query_params_free(qp);
1594 uri_free(uri);
1595 return ret;
1598 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1600 const QDictEntry *e;
1602 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1603 if (!strcmp(e->key, "host") ||
1604 !strcmp(e->key, "port") ||
1605 !strcmp(e->key, "path") ||
1606 !strcmp(e->key, "export") ||
1607 strstart(e->key, "server.", NULL))
1609 error_setg(errp, "Option '%s' cannot be used with a file name",
1610 e->key);
1611 return true;
1615 return false;
1618 static void nbd_parse_filename(const char *filename, QDict *options,
1619 Error **errp)
1621 g_autofree char *file = NULL;
1622 char *export_name;
1623 const char *host_spec;
1624 const char *unixpath;
1626 if (nbd_has_filename_options_conflict(options, errp)) {
1627 return;
1630 if (strstr(filename, "://")) {
1631 int ret = nbd_parse_uri(filename, options);
1632 if (ret < 0) {
1633 error_setg(errp, "No valid URL specified");
1635 return;
1638 file = g_strdup(filename);
1640 export_name = strstr(file, EN_OPTSTR);
1641 if (export_name) {
1642 if (export_name[strlen(EN_OPTSTR)] == 0) {
1643 return;
1645 export_name[0] = 0; /* truncate 'file' */
1646 export_name += strlen(EN_OPTSTR);
1648 qdict_put_str(options, "export", export_name);
1651 /* extract the host_spec - fail if it's not nbd:... */
1652 if (!strstart(file, "nbd:", &host_spec)) {
1653 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1654 return;
1657 if (!*host_spec) {
1658 return;
1661 /* are we a UNIX or TCP socket? */
1662 if (strstart(host_spec, "unix:", &unixpath)) {
1663 qdict_put_str(options, "server.type", "unix");
1664 qdict_put_str(options, "server.path", unixpath);
1665 } else {
1666 InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1668 if (inet_parse(addr, host_spec, errp)) {
1669 goto out_inet;
1672 qdict_put_str(options, "server.type", "inet");
1673 qdict_put_str(options, "server.host", addr->host);
1674 qdict_put_str(options, "server.port", addr->port);
1675 out_inet:
1676 qapi_free_InetSocketAddress(addr);
1680 static bool nbd_process_legacy_socket_options(QDict *output_options,
1681 QemuOpts *legacy_opts,
1682 Error **errp)
1684 const char *path = qemu_opt_get(legacy_opts, "path");
1685 const char *host = qemu_opt_get(legacy_opts, "host");
1686 const char *port = qemu_opt_get(legacy_opts, "port");
1687 const QDictEntry *e;
1689 if (!path && !host && !port) {
1690 return true;
1693 for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1695 if (strstart(e->key, "server.", NULL)) {
1696 error_setg(errp, "Cannot use 'server' and path/host/port at the "
1697 "same time");
1698 return false;
1702 if (path && host) {
1703 error_setg(errp, "path and host may not be used at the same time");
1704 return false;
1705 } else if (path) {
1706 if (port) {
1707 error_setg(errp, "port may not be used without host");
1708 return false;
1711 qdict_put_str(output_options, "server.type", "unix");
1712 qdict_put_str(output_options, "server.path", path);
1713 } else if (host) {
1714 qdict_put_str(output_options, "server.type", "inet");
1715 qdict_put_str(output_options, "server.host", host);
1716 qdict_put_str(output_options, "server.port",
1717 port ?: stringify(NBD_DEFAULT_PORT));
1720 return true;
1723 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
1724 Error **errp)
1726 SocketAddress *saddr = NULL;
1727 QDict *addr = NULL;
1728 Visitor *iv = NULL;
1729 Error *local_err = NULL;
1731 qdict_extract_subqdict(options, &addr, "server.");
1732 if (!qdict_size(addr)) {
1733 error_setg(errp, "NBD server address missing");
1734 goto done;
1737 iv = qobject_input_visitor_new_flat_confused(addr, errp);
1738 if (!iv) {
1739 goto done;
1742 visit_type_SocketAddress(iv, NULL, &saddr, &local_err);
1743 if (local_err) {
1744 error_propagate(errp, local_err);
1745 goto done;
1748 done:
1749 qobject_unref(addr);
1750 visit_free(iv);
1751 return saddr;
1754 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
1756 Object *obj;
1757 QCryptoTLSCreds *creds;
1759 obj = object_resolve_path_component(
1760 object_get_objects_root(), id);
1761 if (!obj) {
1762 error_setg(errp, "No TLS credentials with id '%s'",
1763 id);
1764 return NULL;
1766 creds = (QCryptoTLSCreds *)
1767 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
1768 if (!creds) {
1769 error_setg(errp, "Object with id '%s' is not TLS credentials",
1770 id);
1771 return NULL;
1774 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1775 error_setg(errp,
1776 "Expecting TLS credentials with a client endpoint");
1777 return NULL;
1779 object_ref(obj);
1780 return creds;
1784 static QemuOptsList nbd_runtime_opts = {
1785 .name = "nbd",
1786 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
1787 .desc = {
1789 .name = "host",
1790 .type = QEMU_OPT_STRING,
1791 .help = "TCP host to connect to",
1794 .name = "port",
1795 .type = QEMU_OPT_STRING,
1796 .help = "TCP port to connect to",
1799 .name = "path",
1800 .type = QEMU_OPT_STRING,
1801 .help = "Unix socket path to connect to",
1804 .name = "export",
1805 .type = QEMU_OPT_STRING,
1806 .help = "Name of the NBD export to open",
1809 .name = "tls-creds",
1810 .type = QEMU_OPT_STRING,
1811 .help = "ID of the TLS credentials to use",
1814 .name = "x-dirty-bitmap",
1815 .type = QEMU_OPT_STRING,
1816 .help = "experimental: expose named dirty bitmap in place of "
1817 "block status",
1820 .name = "reconnect-delay",
1821 .type = QEMU_OPT_NUMBER,
1822 .help = "On an unexpected disconnect, the nbd client tries to "
1823 "connect again until succeeding or encountering a serious "
1824 "error. During the first @reconnect-delay seconds, all "
1825 "requests are paused and will be rerun on a successful "
1826 "reconnect. After that time, any delayed requests and all "
1827 "future requests before a successful reconnect will "
1828 "immediately fail. Default 0",
1830 { /* end of list */ }
1834 static int nbd_process_options(BlockDriverState *bs, QDict *options,
1835 Error **errp)
1837 BDRVNBDState *s = bs->opaque;
1838 QemuOpts *opts;
1839 Error *local_err = NULL;
1840 int ret = -EINVAL;
1842 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
1843 qemu_opts_absorb_qdict(opts, options, &local_err);
1844 if (local_err) {
1845 error_propagate(errp, local_err);
1846 goto error;
1849 /* Translate @host, @port, and @path to a SocketAddress */
1850 if (!nbd_process_legacy_socket_options(options, opts, errp)) {
1851 goto error;
1854 /* Pop the config into our state object. Exit if invalid. */
1855 s->saddr = nbd_config(s, options, errp);
1856 if (!s->saddr) {
1857 goto error;
1860 s->export = g_strdup(qemu_opt_get(opts, "export"));
1861 if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
1862 error_setg(errp, "export name too long to send to server");
1863 goto error;
1866 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
1867 if (s->tlscredsid) {
1868 s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
1869 if (!s->tlscreds) {
1870 goto error;
1873 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1874 if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
1875 error_setg(errp, "TLS only supported over IP sockets");
1876 goto error;
1878 s->hostname = s->saddr->u.inet.host;
1881 s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
1882 if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
1883 error_setg(errp, "x-dirty-bitmap query too long to send to server");
1884 goto error;
1887 s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
1889 ret = 0;
1891 error:
1892 if (ret < 0) {
1893 nbd_clear_bdrvstate(s);
1895 qemu_opts_del(opts);
1896 return ret;
1899 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
1900 Error **errp)
1902 int ret;
1903 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1905 ret = nbd_process_options(bs, options, errp);
1906 if (ret < 0) {
1907 return ret;
1910 s->bs = bs;
1911 qemu_co_mutex_init(&s->send_mutex);
1912 qemu_co_queue_init(&s->free_sema);
1914 ret = nbd_client_connect(bs, errp);
1915 if (ret < 0) {
1916 nbd_clear_bdrvstate(s);
1917 return ret;
1919 /* successfully connected */
1920 s->state = NBD_CLIENT_CONNECTED;
1922 s->connection_co = qemu_coroutine_create(nbd_connection_entry, s);
1923 bdrv_inc_in_flight(bs);
1924 aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co);
1926 return 0;
1929 static int nbd_co_flush(BlockDriverState *bs)
1931 return nbd_client_co_flush(bs);
1934 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
1936 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1937 uint32_t min = s->info.min_block;
1938 uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
1941 * If the server did not advertise an alignment:
1942 * - a size that is not sector-aligned implies that an alignment
1943 * of 1 can be used to access those tail bytes
1944 * - advertisement of block status requires an alignment of 1, so
1945 * that we don't violate block layer constraints that block
1946 * status is always aligned (as we can't control whether the
1947 * server will report sub-sector extents, such as a hole at EOF
1948 * on an unaligned POSIX file)
1949 * - otherwise, assume the server is so old that we are safer avoiding
1950 * sub-sector requests
1952 if (!min) {
1953 min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
1954 s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
1957 bs->bl.request_alignment = min;
1958 bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min);
1959 bs->bl.max_pwrite_zeroes = max;
1960 bs->bl.max_transfer = max;
1962 if (s->info.opt_block &&
1963 s->info.opt_block > bs->bl.opt_transfer) {
1964 bs->bl.opt_transfer = s->info.opt_block;
1968 static void nbd_close(BlockDriverState *bs)
1970 BDRVNBDState *s = bs->opaque;
1972 nbd_client_close(bs);
1973 nbd_clear_bdrvstate(s);
1976 static int64_t nbd_getlength(BlockDriverState *bs)
1978 BDRVNBDState *s = bs->opaque;
1980 return s->info.size;
1983 static void nbd_refresh_filename(BlockDriverState *bs)
1985 BDRVNBDState *s = bs->opaque;
1986 const char *host = NULL, *port = NULL, *path = NULL;
1988 if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
1989 const InetSocketAddress *inet = &s->saddr->u.inet;
1990 if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
1991 host = inet->host;
1992 port = inet->port;
1994 } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1995 path = s->saddr->u.q_unix.path;
1996 } /* else can't represent as pseudo-filename */
1998 if (path && s->export) {
1999 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2000 "nbd+unix:///%s?socket=%s", s->export, path);
2001 } else if (path && !s->export) {
2002 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2003 "nbd+unix://?socket=%s", path);
2004 } else if (host && s->export) {
2005 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2006 "nbd://%s:%s/%s", host, port, s->export);
2007 } else if (host && !s->export) {
2008 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2009 "nbd://%s:%s", host, port);
2013 static char *nbd_dirname(BlockDriverState *bs, Error **errp)
2015 /* The generic bdrv_dirname() implementation is able to work out some
2016 * directory name for NBD nodes, but that would be wrong. So far there is no
2017 * specification for how "export paths" would work, so NBD does not have
2018 * directory names. */
2019 error_setg(errp, "Cannot generate a base directory for NBD nodes");
2020 return NULL;
2023 static const char *const nbd_strong_runtime_opts[] = {
2024 "path",
2025 "host",
2026 "port",
2027 "export",
2028 "tls-creds",
2029 "server.",
2031 NULL
2034 static BlockDriver bdrv_nbd = {
2035 .format_name = "nbd",
2036 .protocol_name = "nbd",
2037 .instance_size = sizeof(BDRVNBDState),
2038 .bdrv_parse_filename = nbd_parse_filename,
2039 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2040 .create_opts = &bdrv_create_opts_simple,
2041 .bdrv_file_open = nbd_open,
2042 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2043 .bdrv_co_preadv = nbd_client_co_preadv,
2044 .bdrv_co_pwritev = nbd_client_co_pwritev,
2045 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2046 .bdrv_close = nbd_close,
2047 .bdrv_co_flush_to_os = nbd_co_flush,
2048 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2049 .bdrv_refresh_limits = nbd_refresh_limits,
2050 .bdrv_getlength = nbd_getlength,
2051 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2052 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2053 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2054 .bdrv_co_drain_end = nbd_client_co_drain_end,
2055 .bdrv_refresh_filename = nbd_refresh_filename,
2056 .bdrv_co_block_status = nbd_client_co_block_status,
2057 .bdrv_dirname = nbd_dirname,
2058 .strong_runtime_opts = nbd_strong_runtime_opts,
2061 static BlockDriver bdrv_nbd_tcp = {
2062 .format_name = "nbd",
2063 .protocol_name = "nbd+tcp",
2064 .instance_size = sizeof(BDRVNBDState),
2065 .bdrv_parse_filename = nbd_parse_filename,
2066 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2067 .create_opts = &bdrv_create_opts_simple,
2068 .bdrv_file_open = nbd_open,
2069 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2070 .bdrv_co_preadv = nbd_client_co_preadv,
2071 .bdrv_co_pwritev = nbd_client_co_pwritev,
2072 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2073 .bdrv_close = nbd_close,
2074 .bdrv_co_flush_to_os = nbd_co_flush,
2075 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2076 .bdrv_refresh_limits = nbd_refresh_limits,
2077 .bdrv_getlength = nbd_getlength,
2078 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2079 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2080 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2081 .bdrv_co_drain_end = nbd_client_co_drain_end,
2082 .bdrv_refresh_filename = nbd_refresh_filename,
2083 .bdrv_co_block_status = nbd_client_co_block_status,
2084 .bdrv_dirname = nbd_dirname,
2085 .strong_runtime_opts = nbd_strong_runtime_opts,
2088 static BlockDriver bdrv_nbd_unix = {
2089 .format_name = "nbd",
2090 .protocol_name = "nbd+unix",
2091 .instance_size = sizeof(BDRVNBDState),
2092 .bdrv_parse_filename = nbd_parse_filename,
2093 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2094 .create_opts = &bdrv_create_opts_simple,
2095 .bdrv_file_open = nbd_open,
2096 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2097 .bdrv_co_preadv = nbd_client_co_preadv,
2098 .bdrv_co_pwritev = nbd_client_co_pwritev,
2099 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2100 .bdrv_close = nbd_close,
2101 .bdrv_co_flush_to_os = nbd_co_flush,
2102 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2103 .bdrv_refresh_limits = nbd_refresh_limits,
2104 .bdrv_getlength = nbd_getlength,
2105 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2106 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2107 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2108 .bdrv_co_drain_end = nbd_client_co_drain_end,
2109 .bdrv_refresh_filename = nbd_refresh_filename,
2110 .bdrv_co_block_status = nbd_client_co_block_status,
2111 .bdrv_dirname = nbd_dirname,
2112 .strong_runtime_opts = nbd_strong_runtime_opts,
2115 static void bdrv_nbd_init(void)
2117 bdrv_register(&bdrv_nbd);
2118 bdrv_register(&bdrv_nbd_tcp);
2119 bdrv_register(&bdrv_nbd_unix);
2122 block_init(bdrv_nbd_init);