qapi: introduce x-query-usb QMP command
[qemu.git] / block / nbd.c
blob5ef462db1b7fd805d0cc886c395f80fe74623fe2
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"
38 #include "qemu/atomic.h"
40 #include "qapi/qapi-visit-sockets.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qapi/clone-visitor.h"
44 #include "block/qdict.h"
45 #include "block/nbd.h"
46 #include "block/block_int.h"
47 #include "block/coroutines.h"
49 #include "qemu/yank.h"
51 #define EN_OPTSTR ":exportname="
52 #define MAX_NBD_REQUESTS 16
54 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
55 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
57 typedef struct {
58 Coroutine *coroutine;
59 uint64_t offset; /* original offset of the request */
60 bool receiving; /* sleeping in the yield in nbd_receive_replies */
61 bool reply_possible; /* reply header not yet received */
62 } NBDClientRequest;
64 typedef enum NBDClientState {
65 NBD_CLIENT_CONNECTING_WAIT,
66 NBD_CLIENT_CONNECTING_NOWAIT,
67 NBD_CLIENT_CONNECTED,
68 NBD_CLIENT_QUIT
69 } NBDClientState;
71 typedef struct BDRVNBDState {
72 QIOChannel *ioc; /* The current I/O channel */
73 NBDExportInfo info;
75 CoMutex send_mutex;
76 CoQueue free_sema;
78 CoMutex receive_mutex;
79 int in_flight;
80 NBDClientState state;
82 QEMUTimer *reconnect_delay_timer;
84 NBDClientRequest requests[MAX_NBD_REQUESTS];
85 NBDReply reply;
86 BlockDriverState *bs;
88 /* Connection parameters */
89 uint32_t reconnect_delay;
90 SocketAddress *saddr;
91 char *export, *tlscredsid;
92 QCryptoTLSCreds *tlscreds;
93 const char *hostname;
94 char *x_dirty_bitmap;
95 bool alloc_depth;
97 NBDClientConnection *conn;
98 } BDRVNBDState;
100 static void nbd_yank(void *opaque);
102 static void nbd_clear_bdrvstate(BlockDriverState *bs)
104 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
106 nbd_client_connection_release(s->conn);
107 s->conn = NULL;
109 yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name));
111 object_unref(OBJECT(s->tlscreds));
112 qapi_free_SocketAddress(s->saddr);
113 s->saddr = NULL;
114 g_free(s->export);
115 s->export = NULL;
116 g_free(s->tlscredsid);
117 s->tlscredsid = NULL;
118 g_free(s->x_dirty_bitmap);
119 s->x_dirty_bitmap = NULL;
122 static bool nbd_client_connected(BDRVNBDState *s)
124 return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTED;
127 static bool nbd_recv_coroutine_wake_one(NBDClientRequest *req)
129 if (req->receiving) {
130 req->receiving = false;
131 aio_co_wake(req->coroutine);
132 return true;
135 return false;
138 static void nbd_recv_coroutines_wake(BDRVNBDState *s, bool all)
140 int i;
142 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
143 if (nbd_recv_coroutine_wake_one(&s->requests[i]) && !all) {
144 return;
149 static void nbd_channel_error(BDRVNBDState *s, int ret)
151 if (nbd_client_connected(s)) {
152 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
155 if (ret == -EIO) {
156 if (nbd_client_connected(s)) {
157 s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
158 NBD_CLIENT_CONNECTING_NOWAIT;
160 } else {
161 s->state = NBD_CLIENT_QUIT;
164 nbd_recv_coroutines_wake(s, true);
167 static void reconnect_delay_timer_del(BDRVNBDState *s)
169 if (s->reconnect_delay_timer) {
170 timer_free(s->reconnect_delay_timer);
171 s->reconnect_delay_timer = NULL;
175 static void reconnect_delay_timer_cb(void *opaque)
177 BDRVNBDState *s = opaque;
179 if (qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT) {
180 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
181 nbd_co_establish_connection_cancel(s->conn);
182 while (qemu_co_enter_next(&s->free_sema, NULL)) {
183 /* Resume all queued requests */
187 reconnect_delay_timer_del(s);
190 static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)
192 if (qatomic_load_acquire(&s->state) != NBD_CLIENT_CONNECTING_WAIT) {
193 return;
196 assert(!s->reconnect_delay_timer);
197 s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs),
198 QEMU_CLOCK_REALTIME,
199 SCALE_NS,
200 reconnect_delay_timer_cb, s);
201 timer_mod(s->reconnect_delay_timer, expire_time_ns);
204 static void nbd_teardown_connection(BlockDriverState *bs)
206 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
208 assert(!s->in_flight);
210 if (s->ioc) {
211 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
212 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
213 nbd_yank, s->bs);
214 object_unref(OBJECT(s->ioc));
215 s->ioc = NULL;
218 s->state = NBD_CLIENT_QUIT;
221 static bool nbd_client_connecting(BDRVNBDState *s)
223 NBDClientState state = qatomic_load_acquire(&s->state);
224 return state == NBD_CLIENT_CONNECTING_WAIT ||
225 state == NBD_CLIENT_CONNECTING_NOWAIT;
228 static bool nbd_client_connecting_wait(BDRVNBDState *s)
230 return qatomic_load_acquire(&s->state) == NBD_CLIENT_CONNECTING_WAIT;
234 * Update @bs with information learned during a completed negotiation process.
235 * Return failure if the server's advertised options are incompatible with the
236 * client's needs.
238 static int nbd_handle_updated_info(BlockDriverState *bs, Error **errp)
240 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
241 int ret;
243 if (s->x_dirty_bitmap) {
244 if (!s->info.base_allocation) {
245 error_setg(errp, "requested x-dirty-bitmap %s not found",
246 s->x_dirty_bitmap);
247 return -EINVAL;
249 if (strcmp(s->x_dirty_bitmap, "qemu:allocation-depth") == 0) {
250 s->alloc_depth = true;
254 if (s->info.flags & NBD_FLAG_READ_ONLY) {
255 ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
256 if (ret < 0) {
257 return ret;
261 if (s->info.flags & NBD_FLAG_SEND_FUA) {
262 bs->supported_write_flags = BDRV_REQ_FUA;
263 bs->supported_zero_flags |= BDRV_REQ_FUA;
266 if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
267 bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
268 if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
269 bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
273 trace_nbd_client_handshake_success(s->export);
275 return 0;
278 int coroutine_fn nbd_co_do_establish_connection(BlockDriverState *bs,
279 Error **errp)
281 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
282 int ret;
283 bool blocking = nbd_client_connecting_wait(s);
285 assert(!s->ioc);
287 s->ioc = nbd_co_establish_connection(s->conn, &s->info, blocking, errp);
288 if (!s->ioc) {
289 return -ECONNREFUSED;
292 yank_register_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name), nbd_yank,
293 bs);
295 ret = nbd_handle_updated_info(s->bs, NULL);
296 if (ret < 0) {
298 * We have connected, but must fail for other reasons.
299 * Send NBD_CMD_DISC as a courtesy to the server.
301 NBDRequest request = { .type = NBD_CMD_DISC };
303 nbd_send_request(s->ioc, &request);
305 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
306 nbd_yank, bs);
307 object_unref(OBJECT(s->ioc));
308 s->ioc = NULL;
310 return ret;
313 qio_channel_set_blocking(s->ioc, false, NULL);
314 qio_channel_attach_aio_context(s->ioc, bdrv_get_aio_context(bs));
316 /* successfully connected */
317 s->state = NBD_CLIENT_CONNECTED;
318 qemu_co_queue_restart_all(&s->free_sema);
320 return 0;
323 /* called under s->send_mutex */
324 static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
326 assert(nbd_client_connecting(s));
327 assert(s->in_flight == 0);
329 if (nbd_client_connecting_wait(s) && s->reconnect_delay &&
330 !s->reconnect_delay_timer)
333 * It's first reconnect attempt after switching to
334 * NBD_CLIENT_CONNECTING_WAIT
336 reconnect_delay_timer_init(s,
337 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) +
338 s->reconnect_delay * NANOSECONDS_PER_SECOND);
342 * Now we are sure that nobody is accessing the channel, and no one will
343 * try until we set the state to CONNECTED.
346 /* Finalize previous connection if any */
347 if (s->ioc) {
348 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
349 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s->bs->node_name),
350 nbd_yank, s->bs);
351 object_unref(OBJECT(s->ioc));
352 s->ioc = NULL;
355 nbd_co_do_establish_connection(s->bs, NULL);
358 static coroutine_fn int nbd_receive_replies(BDRVNBDState *s, uint64_t handle)
360 int ret;
361 uint64_t ind = HANDLE_TO_INDEX(s, handle), ind2;
362 QEMU_LOCK_GUARD(&s->receive_mutex);
364 while (true) {
365 if (s->reply.handle == handle) {
366 /* We are done */
367 return 0;
370 if (!nbd_client_connected(s)) {
371 return -EIO;
374 if (s->reply.handle != 0) {
376 * Some other request is being handled now. It should already be
377 * woken by whoever set s->reply.handle (or never wait in this
378 * yield). So, we should not wake it here.
380 ind2 = HANDLE_TO_INDEX(s, s->reply.handle);
381 assert(!s->requests[ind2].receiving);
383 s->requests[ind].receiving = true;
384 qemu_co_mutex_unlock(&s->receive_mutex);
386 qemu_coroutine_yield();
388 * We may be woken for 3 reasons:
389 * 1. From this function, executing in parallel coroutine, when our
390 * handle is received.
391 * 2. From nbd_channel_error(), when connection is lost.
392 * 3. From nbd_co_receive_one_chunk(), when previous request is
393 * finished and s->reply.handle set to 0.
394 * Anyway, it's OK to lock the mutex and go to the next iteration.
397 qemu_co_mutex_lock(&s->receive_mutex);
398 assert(!s->requests[ind].receiving);
399 continue;
402 /* We are under mutex and handle is 0. We have to do the dirty work. */
403 assert(s->reply.handle == 0);
404 ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, NULL);
405 if (ret <= 0) {
406 ret = ret ? ret : -EIO;
407 nbd_channel_error(s, ret);
408 return ret;
410 if (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply) {
411 nbd_channel_error(s, -EINVAL);
412 return -EINVAL;
414 if (s->reply.handle == handle) {
415 /* We are done */
416 return 0;
418 ind2 = HANDLE_TO_INDEX(s, s->reply.handle);
419 if (ind2 >= MAX_NBD_REQUESTS || !s->requests[ind2].reply_possible) {
420 nbd_channel_error(s, -EINVAL);
421 return -EINVAL;
423 nbd_recv_coroutine_wake_one(&s->requests[ind2]);
427 static int nbd_co_send_request(BlockDriverState *bs,
428 NBDRequest *request,
429 QEMUIOVector *qiov)
431 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
432 int rc, i = -1;
434 qemu_co_mutex_lock(&s->send_mutex);
436 while (s->in_flight == MAX_NBD_REQUESTS ||
437 (!nbd_client_connected(s) && s->in_flight > 0))
439 qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
442 if (nbd_client_connecting(s)) {
443 nbd_reconnect_attempt(s);
446 if (!nbd_client_connected(s)) {
447 rc = -EIO;
448 goto err;
451 s->in_flight++;
453 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
454 if (s->requests[i].coroutine == NULL) {
455 break;
459 g_assert(qemu_in_coroutine());
460 assert(i < MAX_NBD_REQUESTS);
462 s->requests[i].coroutine = qemu_coroutine_self();
463 s->requests[i].offset = request->from;
464 s->requests[i].receiving = false;
465 s->requests[i].reply_possible = true;
467 request->handle = INDEX_TO_HANDLE(s, i);
469 assert(s->ioc);
471 if (qiov) {
472 qio_channel_set_cork(s->ioc, true);
473 rc = nbd_send_request(s->ioc, request);
474 if (nbd_client_connected(s) && rc >= 0) {
475 if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
476 NULL) < 0) {
477 rc = -EIO;
479 } else if (rc >= 0) {
480 rc = -EIO;
482 qio_channel_set_cork(s->ioc, false);
483 } else {
484 rc = nbd_send_request(s->ioc, request);
487 err:
488 if (rc < 0) {
489 nbd_channel_error(s, rc);
490 if (i != -1) {
491 s->requests[i].coroutine = NULL;
492 s->in_flight--;
493 qemu_co_queue_next(&s->free_sema);
496 qemu_co_mutex_unlock(&s->send_mutex);
497 return rc;
500 static inline uint16_t payload_advance16(uint8_t **payload)
502 *payload += 2;
503 return lduw_be_p(*payload - 2);
506 static inline uint32_t payload_advance32(uint8_t **payload)
508 *payload += 4;
509 return ldl_be_p(*payload - 4);
512 static inline uint64_t payload_advance64(uint8_t **payload)
514 *payload += 8;
515 return ldq_be_p(*payload - 8);
518 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
519 NBDStructuredReplyChunk *chunk,
520 uint8_t *payload, uint64_t orig_offset,
521 QEMUIOVector *qiov, Error **errp)
523 uint64_t offset;
524 uint32_t hole_size;
526 if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
527 error_setg(errp, "Protocol error: invalid payload for "
528 "NBD_REPLY_TYPE_OFFSET_HOLE");
529 return -EINVAL;
532 offset = payload_advance64(&payload);
533 hole_size = payload_advance32(&payload);
535 if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
536 offset > orig_offset + qiov->size - hole_size) {
537 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
538 " region");
539 return -EINVAL;
541 if (s->info.min_block &&
542 !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
543 trace_nbd_structured_read_compliance("hole");
546 qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
548 return 0;
552 * nbd_parse_blockstatus_payload
553 * Based on our request, we expect only one extent in reply, for the
554 * base:allocation context.
556 static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
557 NBDStructuredReplyChunk *chunk,
558 uint8_t *payload, uint64_t orig_length,
559 NBDExtent *extent, Error **errp)
561 uint32_t context_id;
563 /* The server succeeded, so it must have sent [at least] one extent */
564 if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
565 error_setg(errp, "Protocol error: invalid payload for "
566 "NBD_REPLY_TYPE_BLOCK_STATUS");
567 return -EINVAL;
570 context_id = payload_advance32(&payload);
571 if (s->info.context_id != context_id) {
572 error_setg(errp, "Protocol error: unexpected context id %d for "
573 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
574 "id is %d", context_id,
575 s->info.context_id);
576 return -EINVAL;
579 extent->length = payload_advance32(&payload);
580 extent->flags = payload_advance32(&payload);
582 if (extent->length == 0) {
583 error_setg(errp, "Protocol error: server sent status chunk with "
584 "zero length");
585 return -EINVAL;
589 * A server sending unaligned block status is in violation of the
590 * protocol, but as qemu-nbd 3.1 is such a server (at least for
591 * POSIX files that are not a multiple of 512 bytes, since qemu
592 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
593 * still sees an implicit hole beyond the real EOF), it's nicer to
594 * work around the misbehaving server. If the request included
595 * more than the final unaligned block, truncate it back to an
596 * aligned result; if the request was only the final block, round
597 * up to the full block and change the status to fully-allocated
598 * (always a safe status, even if it loses information).
600 if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
601 s->info.min_block)) {
602 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
603 if (extent->length > s->info.min_block) {
604 extent->length = QEMU_ALIGN_DOWN(extent->length,
605 s->info.min_block);
606 } else {
607 extent->length = s->info.min_block;
608 extent->flags = 0;
613 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
614 * sent us any more than one extent, nor should it have included
615 * status beyond our request in that extent. However, it's easy
616 * enough to ignore the server's noncompliance without killing the
617 * connection; just ignore trailing extents, and clamp things to
618 * the length of our request.
620 if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
621 trace_nbd_parse_blockstatus_compliance("more than one extent");
623 if (extent->length > orig_length) {
624 extent->length = orig_length;
625 trace_nbd_parse_blockstatus_compliance("extent length too large");
629 * HACK: if we are using x-dirty-bitmaps to access
630 * qemu:allocation-depth, treat all depths > 2 the same as 2,
631 * since nbd_client_co_block_status is only expecting the low two
632 * bits to be set.
634 if (s->alloc_depth && extent->flags > 2) {
635 extent->flags = 2;
638 return 0;
642 * nbd_parse_error_payload
643 * on success @errp contains message describing nbd error reply
645 static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
646 uint8_t *payload, int *request_ret,
647 Error **errp)
649 uint32_t error;
650 uint16_t message_size;
652 assert(chunk->type & (1 << 15));
654 if (chunk->length < sizeof(error) + sizeof(message_size)) {
655 error_setg(errp,
656 "Protocol error: invalid payload for structured error");
657 return -EINVAL;
660 error = nbd_errno_to_system_errno(payload_advance32(&payload));
661 if (error == 0) {
662 error_setg(errp, "Protocol error: server sent structured error chunk "
663 "with error = 0");
664 return -EINVAL;
667 *request_ret = -error;
668 message_size = payload_advance16(&payload);
670 if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
671 error_setg(errp, "Protocol error: server sent structured error chunk "
672 "with incorrect message size");
673 return -EINVAL;
676 /* TODO: Add a trace point to mention the server complaint */
678 /* TODO handle ERROR_OFFSET */
680 return 0;
683 static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
684 uint64_t orig_offset,
685 QEMUIOVector *qiov, Error **errp)
687 QEMUIOVector sub_qiov;
688 uint64_t offset;
689 size_t data_size;
690 int ret;
691 NBDStructuredReplyChunk *chunk = &s->reply.structured;
693 assert(nbd_reply_is_structured(&s->reply));
695 /* The NBD spec requires at least one byte of payload */
696 if (chunk->length <= sizeof(offset)) {
697 error_setg(errp, "Protocol error: invalid payload for "
698 "NBD_REPLY_TYPE_OFFSET_DATA");
699 return -EINVAL;
702 if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
703 return -EIO;
706 data_size = chunk->length - sizeof(offset);
707 assert(data_size);
708 if (offset < orig_offset || data_size > qiov->size ||
709 offset > orig_offset + qiov->size - data_size) {
710 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
711 " region");
712 return -EINVAL;
714 if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
715 trace_nbd_structured_read_compliance("data");
718 qemu_iovec_init(&sub_qiov, qiov->niov);
719 qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
720 ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
721 qemu_iovec_destroy(&sub_qiov);
723 return ret < 0 ? -EIO : 0;
726 #define NBD_MAX_MALLOC_PAYLOAD 1000
727 static coroutine_fn int nbd_co_receive_structured_payload(
728 BDRVNBDState *s, void **payload, Error **errp)
730 int ret;
731 uint32_t len;
733 assert(nbd_reply_is_structured(&s->reply));
735 len = s->reply.structured.length;
737 if (len == 0) {
738 return 0;
741 if (payload == NULL) {
742 error_setg(errp, "Unexpected structured payload");
743 return -EINVAL;
746 if (len > NBD_MAX_MALLOC_PAYLOAD) {
747 error_setg(errp, "Payload too large");
748 return -EINVAL;
751 *payload = g_new(char, len);
752 ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
753 if (ret < 0) {
754 g_free(*payload);
755 *payload = NULL;
756 return ret;
759 return 0;
763 * nbd_co_do_receive_one_chunk
764 * for simple reply:
765 * set request_ret to received reply error
766 * if qiov is not NULL: read payload to @qiov
767 * for structured reply chunk:
768 * if error chunk: read payload, set @request_ret, do not set @payload
769 * else if offset_data chunk: read payload data to @qiov, do not set @payload
770 * else: read payload to @payload
772 * If function fails, @errp contains corresponding error message, and the
773 * connection with the server is suspect. If it returns 0, then the
774 * transaction succeeded (although @request_ret may be a negative errno
775 * corresponding to the server's error reply), and errp is unchanged.
777 static coroutine_fn int nbd_co_do_receive_one_chunk(
778 BDRVNBDState *s, uint64_t handle, bool only_structured,
779 int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
781 int ret;
782 int i = HANDLE_TO_INDEX(s, handle);
783 void *local_payload = NULL;
784 NBDStructuredReplyChunk *chunk;
786 if (payload) {
787 *payload = NULL;
789 *request_ret = 0;
791 nbd_receive_replies(s, handle);
792 if (!nbd_client_connected(s)) {
793 error_setg(errp, "Connection closed");
794 return -EIO;
796 assert(s->ioc);
798 assert(s->reply.handle == handle);
800 if (nbd_reply_is_simple(&s->reply)) {
801 if (only_structured) {
802 error_setg(errp, "Protocol error: simple reply when structured "
803 "reply chunk was expected");
804 return -EINVAL;
807 *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
808 if (*request_ret < 0 || !qiov) {
809 return 0;
812 return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
813 errp) < 0 ? -EIO : 0;
816 /* handle structured reply chunk */
817 assert(s->info.structured_reply);
818 chunk = &s->reply.structured;
820 if (chunk->type == NBD_REPLY_TYPE_NONE) {
821 if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
822 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
823 " NBD_REPLY_FLAG_DONE flag set");
824 return -EINVAL;
826 if (chunk->length) {
827 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
828 " nonzero length");
829 return -EINVAL;
831 return 0;
834 if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
835 if (!qiov) {
836 error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
837 return -EINVAL;
840 return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
841 qiov, errp);
844 if (nbd_reply_type_is_error(chunk->type)) {
845 payload = &local_payload;
848 ret = nbd_co_receive_structured_payload(s, payload, errp);
849 if (ret < 0) {
850 return ret;
853 if (nbd_reply_type_is_error(chunk->type)) {
854 ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
855 g_free(local_payload);
856 return ret;
859 return 0;
863 * nbd_co_receive_one_chunk
864 * Read reply, wake up connection_co and set s->quit if needed.
865 * Return value is a fatal error code or normal nbd reply error code
867 static coroutine_fn int nbd_co_receive_one_chunk(
868 BDRVNBDState *s, uint64_t handle, bool only_structured,
869 int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
870 Error **errp)
872 int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
873 request_ret, qiov, payload, errp);
875 if (ret < 0) {
876 memset(reply, 0, sizeof(*reply));
877 nbd_channel_error(s, ret);
878 } else {
879 /* For assert at loop start in nbd_connection_entry */
880 *reply = s->reply;
882 s->reply.handle = 0;
884 nbd_recv_coroutines_wake(s, false);
886 return ret;
889 typedef struct NBDReplyChunkIter {
890 int ret;
891 int request_ret;
892 Error *err;
893 bool done, only_structured;
894 } NBDReplyChunkIter;
896 static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
897 int ret, Error **local_err)
899 assert(local_err && *local_err);
900 assert(ret < 0);
902 if (!iter->ret) {
903 iter->ret = ret;
904 error_propagate(&iter->err, *local_err);
905 } else {
906 error_free(*local_err);
909 *local_err = NULL;
912 static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
914 assert(ret < 0);
916 if (!iter->request_ret) {
917 iter->request_ret = ret;
922 * NBD_FOREACH_REPLY_CHUNK
923 * The pointer stored in @payload requires g_free() to free it.
925 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
926 qiov, reply, payload) \
927 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
928 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
931 * nbd_reply_chunk_iter_receive
932 * The pointer stored in @payload requires g_free() to free it.
934 static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
935 NBDReplyChunkIter *iter,
936 uint64_t handle,
937 QEMUIOVector *qiov, NBDReply *reply,
938 void **payload)
940 int ret, request_ret;
941 NBDReply local_reply;
942 NBDStructuredReplyChunk *chunk;
943 Error *local_err = NULL;
944 if (!nbd_client_connected(s)) {
945 error_setg(&local_err, "Connection closed");
946 nbd_iter_channel_error(iter, -EIO, &local_err);
947 goto break_loop;
950 if (iter->done) {
951 /* Previous iteration was last. */
952 goto break_loop;
955 if (reply == NULL) {
956 reply = &local_reply;
959 ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
960 &request_ret, qiov, reply, payload,
961 &local_err);
962 if (ret < 0) {
963 nbd_iter_channel_error(iter, ret, &local_err);
964 } else if (request_ret < 0) {
965 nbd_iter_request_error(iter, request_ret);
968 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
969 if (nbd_reply_is_simple(reply) || !nbd_client_connected(s)) {
970 goto break_loop;
973 chunk = &reply->structured;
974 iter->only_structured = true;
976 if (chunk->type == NBD_REPLY_TYPE_NONE) {
977 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
978 assert(chunk->flags & NBD_REPLY_FLAG_DONE);
979 goto break_loop;
982 if (chunk->flags & NBD_REPLY_FLAG_DONE) {
983 /* This iteration is last. */
984 iter->done = true;
987 /* Execute the loop body */
988 return true;
990 break_loop:
991 s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
993 qemu_co_mutex_lock(&s->send_mutex);
994 s->in_flight--;
995 qemu_co_queue_next(&s->free_sema);
996 qemu_co_mutex_unlock(&s->send_mutex);
998 return false;
1001 static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
1002 int *request_ret, Error **errp)
1004 NBDReplyChunkIter iter;
1006 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
1007 /* nbd_reply_chunk_iter_receive does all the work */
1010 error_propagate(errp, iter.err);
1011 *request_ret = iter.request_ret;
1012 return iter.ret;
1015 static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
1016 uint64_t offset, QEMUIOVector *qiov,
1017 int *request_ret, Error **errp)
1019 NBDReplyChunkIter iter;
1020 NBDReply reply;
1021 void *payload = NULL;
1022 Error *local_err = NULL;
1024 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
1025 qiov, &reply, &payload)
1027 int ret;
1028 NBDStructuredReplyChunk *chunk = &reply.structured;
1030 assert(nbd_reply_is_structured(&reply));
1032 switch (chunk->type) {
1033 case NBD_REPLY_TYPE_OFFSET_DATA:
1035 * special cased in nbd_co_receive_one_chunk, data is already
1036 * in qiov
1038 break;
1039 case NBD_REPLY_TYPE_OFFSET_HOLE:
1040 ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
1041 offset, qiov, &local_err);
1042 if (ret < 0) {
1043 nbd_channel_error(s, ret);
1044 nbd_iter_channel_error(&iter, ret, &local_err);
1046 break;
1047 default:
1048 if (!nbd_reply_type_is_error(chunk->type)) {
1049 /* not allowed reply type */
1050 nbd_channel_error(s, -EINVAL);
1051 error_setg(&local_err,
1052 "Unexpected reply type: %d (%s) for CMD_READ",
1053 chunk->type, nbd_reply_type_lookup(chunk->type));
1054 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1058 g_free(payload);
1059 payload = NULL;
1062 error_propagate(errp, iter.err);
1063 *request_ret = iter.request_ret;
1064 return iter.ret;
1067 static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
1068 uint64_t handle, uint64_t length,
1069 NBDExtent *extent,
1070 int *request_ret, Error **errp)
1072 NBDReplyChunkIter iter;
1073 NBDReply reply;
1074 void *payload = NULL;
1075 Error *local_err = NULL;
1076 bool received = false;
1078 assert(!extent->length);
1079 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
1080 int ret;
1081 NBDStructuredReplyChunk *chunk = &reply.structured;
1083 assert(nbd_reply_is_structured(&reply));
1085 switch (chunk->type) {
1086 case NBD_REPLY_TYPE_BLOCK_STATUS:
1087 if (received) {
1088 nbd_channel_error(s, -EINVAL);
1089 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
1090 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1092 received = true;
1094 ret = nbd_parse_blockstatus_payload(s, &reply.structured,
1095 payload, length, extent,
1096 &local_err);
1097 if (ret < 0) {
1098 nbd_channel_error(s, ret);
1099 nbd_iter_channel_error(&iter, ret, &local_err);
1101 break;
1102 default:
1103 if (!nbd_reply_type_is_error(chunk->type)) {
1104 nbd_channel_error(s, -EINVAL);
1105 error_setg(&local_err,
1106 "Unexpected reply type: %d (%s) "
1107 "for CMD_BLOCK_STATUS",
1108 chunk->type, nbd_reply_type_lookup(chunk->type));
1109 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1113 g_free(payload);
1114 payload = NULL;
1117 if (!extent->length && !iter.request_ret) {
1118 error_setg(&local_err, "Server did not reply with any status extents");
1119 nbd_iter_channel_error(&iter, -EIO, &local_err);
1122 error_propagate(errp, iter.err);
1123 *request_ret = iter.request_ret;
1124 return iter.ret;
1127 static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
1128 QEMUIOVector *write_qiov)
1130 int ret, request_ret;
1131 Error *local_err = NULL;
1132 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1134 assert(request->type != NBD_CMD_READ);
1135 if (write_qiov) {
1136 assert(request->type == NBD_CMD_WRITE);
1137 assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
1138 } else {
1139 assert(request->type != NBD_CMD_WRITE);
1142 do {
1143 ret = nbd_co_send_request(bs, request, write_qiov);
1144 if (ret < 0) {
1145 continue;
1148 ret = nbd_co_receive_return_code(s, request->handle,
1149 &request_ret, &local_err);
1150 if (local_err) {
1151 trace_nbd_co_request_fail(request->from, request->len,
1152 request->handle, request->flags,
1153 request->type,
1154 nbd_cmd_lookup(request->type),
1155 ret, error_get_pretty(local_err));
1156 error_free(local_err);
1157 local_err = NULL;
1159 } while (ret < 0 && nbd_client_connecting_wait(s));
1161 return ret ? ret : request_ret;
1164 static int nbd_client_co_preadv(BlockDriverState *bs, int64_t offset,
1165 int64_t bytes, QEMUIOVector *qiov,
1166 BdrvRequestFlags flags)
1168 int ret, request_ret;
1169 Error *local_err = NULL;
1170 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1171 NBDRequest request = {
1172 .type = NBD_CMD_READ,
1173 .from = offset,
1174 .len = bytes,
1177 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1178 assert(!flags);
1180 if (!bytes) {
1181 return 0;
1184 * Work around the fact that the block layer doesn't do
1185 * byte-accurate sizing yet - if the read exceeds the server's
1186 * advertised size because the block layer rounded size up, then
1187 * truncate the request to the server and tail-pad with zero.
1189 if (offset >= s->info.size) {
1190 assert(bytes < BDRV_SECTOR_SIZE);
1191 qemu_iovec_memset(qiov, 0, 0, bytes);
1192 return 0;
1194 if (offset + bytes > s->info.size) {
1195 uint64_t slop = offset + bytes - s->info.size;
1197 assert(slop < BDRV_SECTOR_SIZE);
1198 qemu_iovec_memset(qiov, bytes - slop, 0, slop);
1199 request.len -= slop;
1202 do {
1203 ret = nbd_co_send_request(bs, &request, NULL);
1204 if (ret < 0) {
1205 continue;
1208 ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
1209 &request_ret, &local_err);
1210 if (local_err) {
1211 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1212 request.flags, request.type,
1213 nbd_cmd_lookup(request.type),
1214 ret, error_get_pretty(local_err));
1215 error_free(local_err);
1216 local_err = NULL;
1218 } while (ret < 0 && nbd_client_connecting_wait(s));
1220 return ret ? ret : request_ret;
1223 static int nbd_client_co_pwritev(BlockDriverState *bs, int64_t offset,
1224 int64_t bytes, QEMUIOVector *qiov,
1225 BdrvRequestFlags flags)
1227 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1228 NBDRequest request = {
1229 .type = NBD_CMD_WRITE,
1230 .from = offset,
1231 .len = bytes,
1234 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1235 if (flags & BDRV_REQ_FUA) {
1236 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1237 request.flags |= NBD_CMD_FLAG_FUA;
1240 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1242 if (!bytes) {
1243 return 0;
1245 return nbd_co_request(bs, &request, qiov);
1248 static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1249 int64_t bytes, BdrvRequestFlags flags)
1251 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1252 NBDRequest request = {
1253 .type = NBD_CMD_WRITE_ZEROES,
1254 .from = offset,
1255 .len = bytes, /* .len is uint32_t actually */
1258 assert(bytes <= UINT32_MAX); /* rely on max_pwrite_zeroes */
1260 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1261 if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1262 return -ENOTSUP;
1265 if (flags & BDRV_REQ_FUA) {
1266 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1267 request.flags |= NBD_CMD_FLAG_FUA;
1269 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1270 request.flags |= NBD_CMD_FLAG_NO_HOLE;
1272 if (flags & BDRV_REQ_NO_FALLBACK) {
1273 assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1274 request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1277 if (!bytes) {
1278 return 0;
1280 return nbd_co_request(bs, &request, NULL);
1283 static int nbd_client_co_flush(BlockDriverState *bs)
1285 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1286 NBDRequest request = { .type = NBD_CMD_FLUSH };
1288 if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1289 return 0;
1292 request.from = 0;
1293 request.len = 0;
1295 return nbd_co_request(bs, &request, NULL);
1298 static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
1299 int64_t bytes)
1301 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1302 NBDRequest request = {
1303 .type = NBD_CMD_TRIM,
1304 .from = offset,
1305 .len = bytes, /* len is uint32_t */
1308 assert(bytes <= UINT32_MAX); /* rely on max_pdiscard */
1310 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1311 if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1312 return 0;
1315 return nbd_co_request(bs, &request, NULL);
1318 static int coroutine_fn nbd_client_co_block_status(
1319 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
1320 int64_t *pnum, int64_t *map, BlockDriverState **file)
1322 int ret, request_ret;
1323 NBDExtent extent = { 0 };
1324 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1325 Error *local_err = NULL;
1327 NBDRequest request = {
1328 .type = NBD_CMD_BLOCK_STATUS,
1329 .from = offset,
1330 .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment),
1331 MIN(bytes, s->info.size - offset)),
1332 .flags = NBD_CMD_FLAG_REQ_ONE,
1335 if (!s->info.base_allocation) {
1336 *pnum = bytes;
1337 *map = offset;
1338 *file = bs;
1339 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1343 * Work around the fact that the block layer doesn't do
1344 * byte-accurate sizing yet - if the status request exceeds the
1345 * server's advertised size because the block layer rounded size
1346 * up, we truncated the request to the server (above), or are
1347 * called on just the hole.
1349 if (offset >= s->info.size) {
1350 *pnum = bytes;
1351 assert(bytes < BDRV_SECTOR_SIZE);
1352 /* Intentionally don't report offset_valid for the hole */
1353 return BDRV_BLOCK_ZERO;
1356 if (s->info.min_block) {
1357 assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1359 do {
1360 ret = nbd_co_send_request(bs, &request, NULL);
1361 if (ret < 0) {
1362 continue;
1365 ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1366 &extent, &request_ret,
1367 &local_err);
1368 if (local_err) {
1369 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1370 request.flags, request.type,
1371 nbd_cmd_lookup(request.type),
1372 ret, error_get_pretty(local_err));
1373 error_free(local_err);
1374 local_err = NULL;
1376 } while (ret < 0 && nbd_client_connecting_wait(s));
1378 if (ret < 0 || request_ret < 0) {
1379 return ret ? ret : request_ret;
1382 assert(extent.length);
1383 *pnum = extent.length;
1384 *map = offset;
1385 *file = bs;
1386 return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1387 (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1388 BDRV_BLOCK_OFFSET_VALID;
1391 static int nbd_client_reopen_prepare(BDRVReopenState *state,
1392 BlockReopenQueue *queue, Error **errp)
1394 BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1396 if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1397 error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1398 return -EACCES;
1400 return 0;
1403 static void nbd_yank(void *opaque)
1405 BlockDriverState *bs = opaque;
1406 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1408 qatomic_store_release(&s->state, NBD_CLIENT_QUIT);
1409 qio_channel_shutdown(QIO_CHANNEL(s->ioc), QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
1412 static void nbd_client_close(BlockDriverState *bs)
1414 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1415 NBDRequest request = { .type = NBD_CMD_DISC };
1417 if (s->ioc) {
1418 nbd_send_request(s->ioc, &request);
1421 nbd_teardown_connection(bs);
1426 * Parse nbd_open options
1429 static int nbd_parse_uri(const char *filename, QDict *options)
1431 URI *uri;
1432 const char *p;
1433 QueryParams *qp = NULL;
1434 int ret = 0;
1435 bool is_unix;
1437 uri = uri_parse(filename);
1438 if (!uri) {
1439 return -EINVAL;
1442 /* transport */
1443 if (!g_strcmp0(uri->scheme, "nbd")) {
1444 is_unix = false;
1445 } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1446 is_unix = false;
1447 } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1448 is_unix = true;
1449 } else {
1450 ret = -EINVAL;
1451 goto out;
1454 p = uri->path ? uri->path : "";
1455 if (p[0] == '/') {
1456 p++;
1458 if (p[0]) {
1459 qdict_put_str(options, "export", p);
1462 qp = query_params_parse(uri->query);
1463 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1464 ret = -EINVAL;
1465 goto out;
1468 if (is_unix) {
1469 /* nbd+unix:///export?socket=path */
1470 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
1471 ret = -EINVAL;
1472 goto out;
1474 qdict_put_str(options, "server.type", "unix");
1475 qdict_put_str(options, "server.path", qp->p[0].value);
1476 } else {
1477 QString *host;
1478 char *port_str;
1480 /* nbd[+tcp]://host[:port]/export */
1481 if (!uri->server) {
1482 ret = -EINVAL;
1483 goto out;
1486 /* strip braces from literal IPv6 address */
1487 if (uri->server[0] == '[') {
1488 host = qstring_from_substr(uri->server, 1,
1489 strlen(uri->server) - 1);
1490 } else {
1491 host = qstring_from_str(uri->server);
1494 qdict_put_str(options, "server.type", "inet");
1495 qdict_put(options, "server.host", host);
1497 port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
1498 qdict_put_str(options, "server.port", port_str);
1499 g_free(port_str);
1502 out:
1503 if (qp) {
1504 query_params_free(qp);
1506 uri_free(uri);
1507 return ret;
1510 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1512 const QDictEntry *e;
1514 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1515 if (!strcmp(e->key, "host") ||
1516 !strcmp(e->key, "port") ||
1517 !strcmp(e->key, "path") ||
1518 !strcmp(e->key, "export") ||
1519 strstart(e->key, "server.", NULL))
1521 error_setg(errp, "Option '%s' cannot be used with a file name",
1522 e->key);
1523 return true;
1527 return false;
1530 static void nbd_parse_filename(const char *filename, QDict *options,
1531 Error **errp)
1533 g_autofree char *file = NULL;
1534 char *export_name;
1535 const char *host_spec;
1536 const char *unixpath;
1538 if (nbd_has_filename_options_conflict(options, errp)) {
1539 return;
1542 if (strstr(filename, "://")) {
1543 int ret = nbd_parse_uri(filename, options);
1544 if (ret < 0) {
1545 error_setg(errp, "No valid URL specified");
1547 return;
1550 file = g_strdup(filename);
1552 export_name = strstr(file, EN_OPTSTR);
1553 if (export_name) {
1554 if (export_name[strlen(EN_OPTSTR)] == 0) {
1555 return;
1557 export_name[0] = 0; /* truncate 'file' */
1558 export_name += strlen(EN_OPTSTR);
1560 qdict_put_str(options, "export", export_name);
1563 /* extract the host_spec - fail if it's not nbd:... */
1564 if (!strstart(file, "nbd:", &host_spec)) {
1565 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1566 return;
1569 if (!*host_spec) {
1570 return;
1573 /* are we a UNIX or TCP socket? */
1574 if (strstart(host_spec, "unix:", &unixpath)) {
1575 qdict_put_str(options, "server.type", "unix");
1576 qdict_put_str(options, "server.path", unixpath);
1577 } else {
1578 InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1580 if (inet_parse(addr, host_spec, errp)) {
1581 goto out_inet;
1584 qdict_put_str(options, "server.type", "inet");
1585 qdict_put_str(options, "server.host", addr->host);
1586 qdict_put_str(options, "server.port", addr->port);
1587 out_inet:
1588 qapi_free_InetSocketAddress(addr);
1592 static bool nbd_process_legacy_socket_options(QDict *output_options,
1593 QemuOpts *legacy_opts,
1594 Error **errp)
1596 const char *path = qemu_opt_get(legacy_opts, "path");
1597 const char *host = qemu_opt_get(legacy_opts, "host");
1598 const char *port = qemu_opt_get(legacy_opts, "port");
1599 const QDictEntry *e;
1601 if (!path && !host && !port) {
1602 return true;
1605 for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1607 if (strstart(e->key, "server.", NULL)) {
1608 error_setg(errp, "Cannot use 'server' and path/host/port at the "
1609 "same time");
1610 return false;
1614 if (path && host) {
1615 error_setg(errp, "path and host may not be used at the same time");
1616 return false;
1617 } else if (path) {
1618 if (port) {
1619 error_setg(errp, "port may not be used without host");
1620 return false;
1623 qdict_put_str(output_options, "server.type", "unix");
1624 qdict_put_str(output_options, "server.path", path);
1625 } else if (host) {
1626 qdict_put_str(output_options, "server.type", "inet");
1627 qdict_put_str(output_options, "server.host", host);
1628 qdict_put_str(output_options, "server.port",
1629 port ?: stringify(NBD_DEFAULT_PORT));
1632 return true;
1635 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
1636 Error **errp)
1638 SocketAddress *saddr = NULL;
1639 QDict *addr = NULL;
1640 Visitor *iv = NULL;
1642 qdict_extract_subqdict(options, &addr, "server.");
1643 if (!qdict_size(addr)) {
1644 error_setg(errp, "NBD server address missing");
1645 goto done;
1648 iv = qobject_input_visitor_new_flat_confused(addr, errp);
1649 if (!iv) {
1650 goto done;
1653 if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) {
1654 goto done;
1657 if (socket_address_parse_named_fd(saddr, errp) < 0) {
1658 qapi_free_SocketAddress(saddr);
1659 saddr = NULL;
1660 goto done;
1663 done:
1664 qobject_unref(addr);
1665 visit_free(iv);
1666 return saddr;
1669 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
1671 Object *obj;
1672 QCryptoTLSCreds *creds;
1674 obj = object_resolve_path_component(
1675 object_get_objects_root(), id);
1676 if (!obj) {
1677 error_setg(errp, "No TLS credentials with id '%s'",
1678 id);
1679 return NULL;
1681 creds = (QCryptoTLSCreds *)
1682 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
1683 if (!creds) {
1684 error_setg(errp, "Object with id '%s' is not TLS credentials",
1685 id);
1686 return NULL;
1689 if (!qcrypto_tls_creds_check_endpoint(creds,
1690 QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT,
1691 errp)) {
1692 return NULL;
1694 object_ref(obj);
1695 return creds;
1699 static QemuOptsList nbd_runtime_opts = {
1700 .name = "nbd",
1701 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
1702 .desc = {
1704 .name = "host",
1705 .type = QEMU_OPT_STRING,
1706 .help = "TCP host to connect to",
1709 .name = "port",
1710 .type = QEMU_OPT_STRING,
1711 .help = "TCP port to connect to",
1714 .name = "path",
1715 .type = QEMU_OPT_STRING,
1716 .help = "Unix socket path to connect to",
1719 .name = "export",
1720 .type = QEMU_OPT_STRING,
1721 .help = "Name of the NBD export to open",
1724 .name = "tls-creds",
1725 .type = QEMU_OPT_STRING,
1726 .help = "ID of the TLS credentials to use",
1729 .name = "x-dirty-bitmap",
1730 .type = QEMU_OPT_STRING,
1731 .help = "experimental: expose named dirty bitmap in place of "
1732 "block status",
1735 .name = "reconnect-delay",
1736 .type = QEMU_OPT_NUMBER,
1737 .help = "On an unexpected disconnect, the nbd client tries to "
1738 "connect again until succeeding or encountering a serious "
1739 "error. During the first @reconnect-delay seconds, all "
1740 "requests are paused and will be rerun on a successful "
1741 "reconnect. After that time, any delayed requests and all "
1742 "future requests before a successful reconnect will "
1743 "immediately fail. Default 0",
1745 { /* end of list */ }
1749 static int nbd_process_options(BlockDriverState *bs, QDict *options,
1750 Error **errp)
1752 BDRVNBDState *s = bs->opaque;
1753 QemuOpts *opts;
1754 int ret = -EINVAL;
1756 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
1757 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1758 goto error;
1761 /* Translate @host, @port, and @path to a SocketAddress */
1762 if (!nbd_process_legacy_socket_options(options, opts, errp)) {
1763 goto error;
1766 /* Pop the config into our state object. Exit if invalid. */
1767 s->saddr = nbd_config(s, options, errp);
1768 if (!s->saddr) {
1769 goto error;
1772 s->export = g_strdup(qemu_opt_get(opts, "export"));
1773 if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
1774 error_setg(errp, "export name too long to send to server");
1775 goto error;
1778 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
1779 if (s->tlscredsid) {
1780 s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
1781 if (!s->tlscreds) {
1782 goto error;
1785 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1786 if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
1787 error_setg(errp, "TLS only supported over IP sockets");
1788 goto error;
1790 s->hostname = s->saddr->u.inet.host;
1793 s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
1794 if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
1795 error_setg(errp, "x-dirty-bitmap query too long to send to server");
1796 goto error;
1799 s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
1801 ret = 0;
1803 error:
1804 qemu_opts_del(opts);
1805 return ret;
1808 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
1809 Error **errp)
1811 int ret;
1812 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1814 s->bs = bs;
1815 qemu_co_mutex_init(&s->send_mutex);
1816 qemu_co_queue_init(&s->free_sema);
1817 qemu_co_mutex_init(&s->receive_mutex);
1819 if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs->node_name), errp)) {
1820 return -EEXIST;
1823 ret = nbd_process_options(bs, options, errp);
1824 if (ret < 0) {
1825 goto fail;
1828 s->conn = nbd_client_connection_new(s->saddr, true, s->export,
1829 s->x_dirty_bitmap, s->tlscreds);
1831 /* TODO: Configurable retry-until-timeout behaviour. */
1832 s->state = NBD_CLIENT_CONNECTING_WAIT;
1833 ret = nbd_do_establish_connection(bs, errp);
1834 if (ret < 0) {
1835 goto fail;
1838 nbd_client_connection_enable_retry(s->conn);
1840 return 0;
1842 fail:
1843 nbd_clear_bdrvstate(bs);
1844 return ret;
1847 static int nbd_co_flush(BlockDriverState *bs)
1849 return nbd_client_co_flush(bs);
1852 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
1854 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1855 uint32_t min = s->info.min_block;
1856 uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
1859 * If the server did not advertise an alignment:
1860 * - a size that is not sector-aligned implies that an alignment
1861 * of 1 can be used to access those tail bytes
1862 * - advertisement of block status requires an alignment of 1, so
1863 * that we don't violate block layer constraints that block
1864 * status is always aligned (as we can't control whether the
1865 * server will report sub-sector extents, such as a hole at EOF
1866 * on an unaligned POSIX file)
1867 * - otherwise, assume the server is so old that we are safer avoiding
1868 * sub-sector requests
1870 if (!min) {
1871 min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
1872 s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
1875 bs->bl.request_alignment = min;
1876 bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min);
1877 bs->bl.max_pwrite_zeroes = max;
1878 bs->bl.max_transfer = max;
1880 if (s->info.opt_block &&
1881 s->info.opt_block > bs->bl.opt_transfer) {
1882 bs->bl.opt_transfer = s->info.opt_block;
1886 static void nbd_close(BlockDriverState *bs)
1888 nbd_client_close(bs);
1889 nbd_clear_bdrvstate(bs);
1893 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
1894 * to a smaller size with exact=false, there is no reason to fail the
1895 * operation.
1897 * Preallocation mode is ignored since it does not seems useful to fail when
1898 * we never change anything.
1900 static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset,
1901 bool exact, PreallocMode prealloc,
1902 BdrvRequestFlags flags, Error **errp)
1904 BDRVNBDState *s = bs->opaque;
1906 if (offset != s->info.size && exact) {
1907 error_setg(errp, "Cannot resize NBD nodes");
1908 return -ENOTSUP;
1911 if (offset > s->info.size) {
1912 error_setg(errp, "Cannot grow NBD nodes");
1913 return -EINVAL;
1916 return 0;
1919 static int64_t nbd_getlength(BlockDriverState *bs)
1921 BDRVNBDState *s = bs->opaque;
1923 return s->info.size;
1926 static void nbd_refresh_filename(BlockDriverState *bs)
1928 BDRVNBDState *s = bs->opaque;
1929 const char *host = NULL, *port = NULL, *path = NULL;
1930 size_t len = 0;
1932 if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
1933 const InetSocketAddress *inet = &s->saddr->u.inet;
1934 if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
1935 host = inet->host;
1936 port = inet->port;
1938 } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1939 path = s->saddr->u.q_unix.path;
1940 } /* else can't represent as pseudo-filename */
1942 if (path && s->export) {
1943 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1944 "nbd+unix:///%s?socket=%s", s->export, path);
1945 } else if (path && !s->export) {
1946 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1947 "nbd+unix://?socket=%s", path);
1948 } else if (host && s->export) {
1949 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1950 "nbd://%s:%s/%s", host, port, s->export);
1951 } else if (host && !s->export) {
1952 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1953 "nbd://%s:%s", host, port);
1955 if (len >= sizeof(bs->exact_filename)) {
1956 /* Name is too long to represent exactly, so leave it empty. */
1957 bs->exact_filename[0] = '\0';
1961 static char *nbd_dirname(BlockDriverState *bs, Error **errp)
1963 /* The generic bdrv_dirname() implementation is able to work out some
1964 * directory name for NBD nodes, but that would be wrong. So far there is no
1965 * specification for how "export paths" would work, so NBD does not have
1966 * directory names. */
1967 error_setg(errp, "Cannot generate a base directory for NBD nodes");
1968 return NULL;
1971 static const char *const nbd_strong_runtime_opts[] = {
1972 "path",
1973 "host",
1974 "port",
1975 "export",
1976 "tls-creds",
1977 "server.",
1979 NULL
1982 static void nbd_cancel_in_flight(BlockDriverState *bs)
1984 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1986 reconnect_delay_timer_del(s);
1988 if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
1989 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
1990 qemu_co_queue_restart_all(&s->free_sema);
1993 nbd_co_establish_connection_cancel(s->conn);
1996 static BlockDriver bdrv_nbd = {
1997 .format_name = "nbd",
1998 .protocol_name = "nbd",
1999 .instance_size = sizeof(BDRVNBDState),
2000 .bdrv_parse_filename = nbd_parse_filename,
2001 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2002 .create_opts = &bdrv_create_opts_simple,
2003 .bdrv_file_open = nbd_open,
2004 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2005 .bdrv_co_preadv = nbd_client_co_preadv,
2006 .bdrv_co_pwritev = nbd_client_co_pwritev,
2007 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2008 .bdrv_close = nbd_close,
2009 .bdrv_co_flush_to_os = nbd_co_flush,
2010 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2011 .bdrv_refresh_limits = nbd_refresh_limits,
2012 .bdrv_co_truncate = nbd_co_truncate,
2013 .bdrv_getlength = nbd_getlength,
2014 .bdrv_refresh_filename = nbd_refresh_filename,
2015 .bdrv_co_block_status = nbd_client_co_block_status,
2016 .bdrv_dirname = nbd_dirname,
2017 .strong_runtime_opts = nbd_strong_runtime_opts,
2018 .bdrv_cancel_in_flight = nbd_cancel_in_flight,
2021 static BlockDriver bdrv_nbd_tcp = {
2022 .format_name = "nbd",
2023 .protocol_name = "nbd+tcp",
2024 .instance_size = sizeof(BDRVNBDState),
2025 .bdrv_parse_filename = nbd_parse_filename,
2026 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2027 .create_opts = &bdrv_create_opts_simple,
2028 .bdrv_file_open = nbd_open,
2029 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2030 .bdrv_co_preadv = nbd_client_co_preadv,
2031 .bdrv_co_pwritev = nbd_client_co_pwritev,
2032 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2033 .bdrv_close = nbd_close,
2034 .bdrv_co_flush_to_os = nbd_co_flush,
2035 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2036 .bdrv_refresh_limits = nbd_refresh_limits,
2037 .bdrv_co_truncate = nbd_co_truncate,
2038 .bdrv_getlength = nbd_getlength,
2039 .bdrv_refresh_filename = nbd_refresh_filename,
2040 .bdrv_co_block_status = nbd_client_co_block_status,
2041 .bdrv_dirname = nbd_dirname,
2042 .strong_runtime_opts = nbd_strong_runtime_opts,
2043 .bdrv_cancel_in_flight = nbd_cancel_in_flight,
2046 static BlockDriver bdrv_nbd_unix = {
2047 .format_name = "nbd",
2048 .protocol_name = "nbd+unix",
2049 .instance_size = sizeof(BDRVNBDState),
2050 .bdrv_parse_filename = nbd_parse_filename,
2051 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2052 .create_opts = &bdrv_create_opts_simple,
2053 .bdrv_file_open = nbd_open,
2054 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2055 .bdrv_co_preadv = nbd_client_co_preadv,
2056 .bdrv_co_pwritev = nbd_client_co_pwritev,
2057 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2058 .bdrv_close = nbd_close,
2059 .bdrv_co_flush_to_os = nbd_co_flush,
2060 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2061 .bdrv_refresh_limits = nbd_refresh_limits,
2062 .bdrv_co_truncate = nbd_co_truncate,
2063 .bdrv_getlength = nbd_getlength,
2064 .bdrv_refresh_filename = nbd_refresh_filename,
2065 .bdrv_co_block_status = nbd_client_co_block_status,
2066 .bdrv_dirname = nbd_dirname,
2067 .strong_runtime_opts = nbd_strong_runtime_opts,
2068 .bdrv_cancel_in_flight = nbd_cancel_in_flight,
2071 static void bdrv_nbd_init(void)
2073 bdrv_register(&bdrv_nbd);
2074 bdrv_register(&bdrv_nbd_tcp);
2075 bdrv_register(&bdrv_nbd_unix);
2078 block_init(bdrv_nbd_init);