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>
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
31 #include "qemu/osdep.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"
41 #include "qapi/clone-visitor.h"
43 #include "block/qdict.h"
44 #include "block/nbd.h"
45 #include "block/block_int.h"
47 #define EN_OPTSTR ":exportname="
48 #define MAX_NBD_REQUESTS 16
50 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
51 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
55 uint64_t offset; /* original offset of the request */
56 bool receiving; /* waiting for connection_co? */
59 typedef enum NBDClientState {
60 NBD_CLIENT_CONNECTING_WAIT,
61 NBD_CLIENT_CONNECTING_NOWAIT,
66 typedef enum NBDConnectThreadState {
67 /* No thread, no pending results */
70 /* Thread is running, no results for now */
71 CONNECT_THREAD_RUNNING,
74 * Thread is running, but requestor exited. Thread should close
75 * the new socket and free the connect state on exit.
77 CONNECT_THREAD_RUNNING_DETACHED,
79 /* Thread finished, results are stored in a state */
81 CONNECT_THREAD_SUCCESS
82 } NBDConnectThreadState;
84 typedef struct NBDConnectThread {
85 /* Initialization constants */
86 SocketAddress *saddr; /* address to connect to */
88 * Bottom half to schedule on completion. Scheduled only if bh_ctx is not
95 * Result of last attempt. Valid in FAIL and SUCCESS states.
96 * If you want to steal error, don't forget to set pointer to NULL.
98 QIOChannelSocket *sioc;
101 /* state and bh_ctx are protected by mutex */
103 NBDConnectThreadState state; /* current state of the thread */
104 AioContext *bh_ctx; /* where to schedule bh (NULL means don't schedule) */
107 typedef struct BDRVNBDState {
108 QIOChannelSocket *sioc; /* The master data channel */
109 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
114 Coroutine *connection_co;
115 Coroutine *teardown_co;
116 QemuCoSleepState *connection_co_sleep_ns_state;
118 bool wait_drained_end;
120 NBDClientState state;
125 QEMUTimer *reconnect_delay_timer;
127 NBDClientRequest requests[MAX_NBD_REQUESTS];
129 BlockDriverState *bs;
131 /* Connection parameters */
132 uint32_t reconnect_delay;
133 SocketAddress *saddr;
134 char *export, *tlscredsid;
135 QCryptoTLSCreds *tlscreds;
136 const char *hostname;
137 char *x_dirty_bitmap;
140 NBDConnectThread *connect_thread;
143 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
145 static QIOChannelSocket *nbd_co_establish_connection(BlockDriverState *bs,
147 static void nbd_co_establish_connection_cancel(BlockDriverState *bs,
149 static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
152 static void nbd_clear_bdrvstate(BDRVNBDState *s)
154 object_unref(OBJECT(s->tlscreds));
155 qapi_free_SocketAddress(s->saddr);
159 g_free(s->tlscredsid);
160 s->tlscredsid = NULL;
161 g_free(s->x_dirty_bitmap);
162 s->x_dirty_bitmap = NULL;
165 static void nbd_channel_error(BDRVNBDState *s, int ret)
168 if (s->state == NBD_CLIENT_CONNECTED) {
169 s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
170 NBD_CLIENT_CONNECTING_NOWAIT;
173 if (s->state == NBD_CLIENT_CONNECTED) {
174 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
176 s->state = NBD_CLIENT_QUIT;
180 static void nbd_recv_coroutines_wake_all(BDRVNBDState *s)
184 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
185 NBDClientRequest *req = &s->requests[i];
187 if (req->coroutine && req->receiving) {
188 aio_co_wake(req->coroutine);
193 static void reconnect_delay_timer_del(BDRVNBDState *s)
195 if (s->reconnect_delay_timer) {
196 timer_del(s->reconnect_delay_timer);
197 timer_free(s->reconnect_delay_timer);
198 s->reconnect_delay_timer = NULL;
202 static void reconnect_delay_timer_cb(void *opaque)
204 BDRVNBDState *s = opaque;
206 if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
207 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
208 while (qemu_co_enter_next(&s->free_sema, NULL)) {
209 /* Resume all queued requests */
213 reconnect_delay_timer_del(s);
216 static void reconnect_delay_timer_init(BDRVNBDState *s, uint64_t expire_time_ns)
218 if (s->state != NBD_CLIENT_CONNECTING_WAIT) {
222 assert(!s->reconnect_delay_timer);
223 s->reconnect_delay_timer = aio_timer_new(bdrv_get_aio_context(s->bs),
226 reconnect_delay_timer_cb, s);
227 timer_mod(s->reconnect_delay_timer, expire_time_ns);
230 static void nbd_client_detach_aio_context(BlockDriverState *bs)
232 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
234 /* Timer is deleted in nbd_client_co_drain_begin() */
235 assert(!s->reconnect_delay_timer);
236 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
239 static void nbd_client_attach_aio_context_bh(void *opaque)
241 BlockDriverState *bs = opaque;
242 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
245 * The node is still drained, so we know the coroutine has yielded in
246 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
247 * entered for the first time. Both places are safe for entering the
250 qemu_aio_coroutine_enter(bs->aio_context, s->connection_co);
251 bdrv_dec_in_flight(bs);
254 static void nbd_client_attach_aio_context(BlockDriverState *bs,
255 AioContext *new_context)
257 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
260 * s->connection_co is either yielded from nbd_receive_reply or from
261 * nbd_co_reconnect_loop()
263 if (s->state == NBD_CLIENT_CONNECTED) {
264 qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context);
267 bdrv_inc_in_flight(bs);
270 * Need to wait here for the BH to run because the BH must run while the
271 * node is still drained.
273 aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs);
276 static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
278 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
281 if (s->connection_co_sleep_ns_state) {
282 qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
285 nbd_co_establish_connection_cancel(bs, false);
287 reconnect_delay_timer_del(s);
289 if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
290 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
291 qemu_co_queue_restart_all(&s->free_sema);
295 static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs)
297 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
300 if (s->wait_drained_end) {
301 s->wait_drained_end = false;
302 aio_co_wake(s->connection_co);
307 static void nbd_teardown_connection(BlockDriverState *bs)
309 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
312 /* finish any pending coroutines */
313 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
314 } else if (s->sioc) {
315 /* abort negotiation */
316 qio_channel_shutdown(QIO_CHANNEL(s->sioc), QIO_CHANNEL_SHUTDOWN_BOTH,
320 s->state = NBD_CLIENT_QUIT;
321 if (s->connection_co) {
322 if (s->connection_co_sleep_ns_state) {
323 qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
325 nbd_co_establish_connection_cancel(bs, true);
327 if (qemu_in_coroutine()) {
328 s->teardown_co = qemu_coroutine_self();
329 /* connection_co resumes us when it terminates */
330 qemu_coroutine_yield();
331 s->teardown_co = NULL;
333 BDRV_POLL_WHILE(bs, s->connection_co);
335 assert(!s->connection_co);
338 static bool nbd_client_connecting(BDRVNBDState *s)
340 return s->state == NBD_CLIENT_CONNECTING_WAIT ||
341 s->state == NBD_CLIENT_CONNECTING_NOWAIT;
344 static bool nbd_client_connecting_wait(BDRVNBDState *s)
346 return s->state == NBD_CLIENT_CONNECTING_WAIT;
349 static void connect_bh(void *opaque)
351 BDRVNBDState *state = opaque;
353 assert(state->wait_connect);
354 state->wait_connect = false;
355 aio_co_wake(state->connection_co);
358 static void nbd_init_connect_thread(BDRVNBDState *s)
360 s->connect_thread = g_new(NBDConnectThread, 1);
362 *s->connect_thread = (NBDConnectThread) {
363 .saddr = QAPI_CLONE(SocketAddress, s->saddr),
364 .state = CONNECT_THREAD_NONE,
365 .bh_func = connect_bh,
369 qemu_mutex_init(&s->connect_thread->mutex);
372 static void nbd_free_connect_thread(NBDConnectThread *thr)
375 qio_channel_close(QIO_CHANNEL(thr->sioc), NULL);
377 error_free(thr->err);
378 qapi_free_SocketAddress(thr->saddr);
382 static void *connect_thread_func(void *opaque)
384 NBDConnectThread *thr = opaque;
386 bool do_free = false;
388 thr->sioc = qio_channel_socket_new();
390 error_free(thr->err);
392 ret = qio_channel_socket_connect_sync(thr->sioc, thr->saddr, &thr->err);
394 object_unref(OBJECT(thr->sioc));
398 qemu_mutex_lock(&thr->mutex);
400 switch (thr->state) {
401 case CONNECT_THREAD_RUNNING:
402 thr->state = ret < 0 ? CONNECT_THREAD_FAIL : CONNECT_THREAD_SUCCESS;
404 aio_bh_schedule_oneshot(thr->bh_ctx, thr->bh_func, thr->bh_opaque);
406 /* play safe, don't reuse bh_ctx on further connection attempts */
410 case CONNECT_THREAD_RUNNING_DETACHED:
417 qemu_mutex_unlock(&thr->mutex);
420 nbd_free_connect_thread(thr);
426 static QIOChannelSocket *coroutine_fn
427 nbd_co_establish_connection(BlockDriverState *bs, Error **errp)
430 BDRVNBDState *s = bs->opaque;
431 QIOChannelSocket *res;
432 NBDConnectThread *thr = s->connect_thread;
434 qemu_mutex_lock(&thr->mutex);
436 switch (thr->state) {
437 case CONNECT_THREAD_FAIL:
438 case CONNECT_THREAD_NONE:
439 error_free(thr->err);
441 thr->state = CONNECT_THREAD_RUNNING;
442 qemu_thread_create(&thread, "nbd-connect",
443 connect_thread_func, thr, QEMU_THREAD_DETACHED);
445 case CONNECT_THREAD_SUCCESS:
446 /* Previous attempt finally succeeded in background */
447 thr->state = CONNECT_THREAD_NONE;
450 qemu_mutex_unlock(&thr->mutex);
452 case CONNECT_THREAD_RUNNING:
453 /* Already running, will wait */
459 thr->bh_ctx = qemu_get_current_aio_context();
461 qemu_mutex_unlock(&thr->mutex);
465 * We are going to wait for connect-thread finish, but
466 * nbd_client_co_drain_begin() can interrupt.
468 * Note that wait_connect variable is not visible for connect-thread. It
469 * doesn't need mutex protection, it used only inside home aio context of
472 s->wait_connect = true;
473 qemu_coroutine_yield();
475 qemu_mutex_lock(&thr->mutex);
477 switch (thr->state) {
478 case CONNECT_THREAD_SUCCESS:
479 case CONNECT_THREAD_FAIL:
480 thr->state = CONNECT_THREAD_NONE;
481 error_propagate(errp, thr->err);
486 case CONNECT_THREAD_RUNNING:
487 case CONNECT_THREAD_RUNNING_DETACHED:
489 * Obviously, drained section wants to start. Report the attempt as
490 * failed. Still connect thread is executing in background, and its
491 * result may be used for next connection attempt.
494 error_setg(errp, "Connection attempt cancelled by other operation");
497 case CONNECT_THREAD_NONE:
499 * Impossible. We've seen this thread running. So it should be
500 * running or at least give some results.
508 qemu_mutex_unlock(&thr->mutex);
514 * nbd_co_establish_connection_cancel
515 * Cancel nbd_co_establish_connection asynchronously: it will finish soon, to
516 * allow drained section to begin.
518 * If detach is true, also cleanup the state (or if thread is running, move it
519 * to CONNECT_THREAD_RUNNING_DETACHED state). s->connect_thread becomes NULL if
522 static void nbd_co_establish_connection_cancel(BlockDriverState *bs,
525 BDRVNBDState *s = bs->opaque;
526 NBDConnectThread *thr = s->connect_thread;
528 bool do_free = false;
530 qemu_mutex_lock(&thr->mutex);
532 if (thr->state == CONNECT_THREAD_RUNNING) {
533 /* We can cancel only in running state, when bh is not yet scheduled */
535 if (s->wait_connect) {
536 s->wait_connect = false;
540 thr->state = CONNECT_THREAD_RUNNING_DETACHED;
541 s->connect_thread = NULL;
547 qemu_mutex_unlock(&thr->mutex);
550 nbd_free_connect_thread(thr);
551 s->connect_thread = NULL;
555 aio_co_wake(s->connection_co);
559 static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
562 Error *local_err = NULL;
563 QIOChannelSocket *sioc;
565 if (!nbd_client_connecting(s)) {
569 /* Wait for completion of all in-flight requests */
571 qemu_co_mutex_lock(&s->send_mutex);
573 while (s->in_flight > 0) {
574 qemu_co_mutex_unlock(&s->send_mutex);
575 nbd_recv_coroutines_wake_all(s);
576 s->wait_in_flight = true;
577 qemu_coroutine_yield();
578 s->wait_in_flight = false;
579 qemu_co_mutex_lock(&s->send_mutex);
582 qemu_co_mutex_unlock(&s->send_mutex);
584 if (!nbd_client_connecting(s)) {
589 * Now we are sure that nobody is accessing the channel, and no one will
590 * try until we set the state to CONNECTED.
593 /* Finalize previous connection if any */
595 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
596 object_unref(OBJECT(s->sioc));
598 object_unref(OBJECT(s->ioc));
602 sioc = nbd_co_establish_connection(s->bs, &local_err);
608 bdrv_dec_in_flight(s->bs);
610 ret = nbd_client_handshake(s->bs, sioc, &local_err);
613 s->wait_drained_end = true;
616 * We may be entered once from nbd_client_attach_aio_context_bh
617 * and then from nbd_client_co_drain_end. So here is a loop.
619 qemu_coroutine_yield();
622 bdrv_inc_in_flight(s->bs);
625 s->connect_status = ret;
626 error_free(s->connect_err);
627 s->connect_err = NULL;
628 error_propagate(&s->connect_err, local_err);
631 /* successfully connected */
632 s->state = NBD_CLIENT_CONNECTED;
633 qemu_co_queue_restart_all(&s->free_sema);
637 static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
639 uint64_t timeout = 1 * NANOSECONDS_PER_SECOND;
640 uint64_t max_timeout = 16 * NANOSECONDS_PER_SECOND;
642 if (s->state == NBD_CLIENT_CONNECTING_WAIT) {
643 reconnect_delay_timer_init(s, qemu_clock_get_ns(QEMU_CLOCK_REALTIME) +
644 s->reconnect_delay * NANOSECONDS_PER_SECOND);
647 nbd_reconnect_attempt(s);
649 while (nbd_client_connecting(s)) {
651 bdrv_dec_in_flight(s->bs);
652 s->wait_drained_end = true;
655 * We may be entered once from nbd_client_attach_aio_context_bh
656 * and then from nbd_client_co_drain_end. So here is a loop.
658 qemu_coroutine_yield();
660 bdrv_inc_in_flight(s->bs);
662 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
663 &s->connection_co_sleep_ns_state);
667 if (timeout < max_timeout) {
672 nbd_reconnect_attempt(s);
675 reconnect_delay_timer_del(s);
678 static coroutine_fn void nbd_connection_entry(void *opaque)
680 BDRVNBDState *s = opaque;
683 Error *local_err = NULL;
685 while (s->state != NBD_CLIENT_QUIT) {
687 * The NBD client can only really be considered idle when it has
688 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
689 * the point where the additional scheduled coroutine entry happens
690 * after nbd_client_attach_aio_context().
692 * Therefore we keep an additional in_flight reference all the time and
693 * only drop it temporarily here.
696 if (nbd_client_connecting(s)) {
697 nbd_co_reconnect_loop(s);
700 if (s->state != NBD_CLIENT_CONNECTED) {
704 assert(s->reply.handle == 0);
705 ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err);
708 trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err));
709 error_free(local_err);
713 nbd_channel_error(s, ret ? ret : -EIO);
718 * There's no need for a mutex on the receive side, because the
719 * handler acts as a synchronization point and ensures that only
720 * one coroutine is called until the reply finishes.
722 i = HANDLE_TO_INDEX(s, s->reply.handle);
723 if (i >= MAX_NBD_REQUESTS ||
724 !s->requests[i].coroutine ||
725 !s->requests[i].receiving ||
726 (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply))
728 nbd_channel_error(s, -EINVAL);
733 * We're woken up again by the request itself. Note that there
734 * is no race between yielding and reentering connection_co. This
737 * - if the request runs on the same AioContext, it is only
738 * entered after we yield
740 * - if the request runs on a different AioContext, reentering
741 * connection_co happens through a bottom half, which can only
742 * run after we yield.
744 aio_co_wake(s->requests[i].coroutine);
745 qemu_coroutine_yield();
748 qemu_co_queue_restart_all(&s->free_sema);
749 nbd_recv_coroutines_wake_all(s);
750 bdrv_dec_in_flight(s->bs);
752 s->connection_co = NULL;
754 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
755 object_unref(OBJECT(s->sioc));
757 object_unref(OBJECT(s->ioc));
761 if (s->teardown_co) {
762 aio_co_wake(s->teardown_co);
767 static int nbd_co_send_request(BlockDriverState *bs,
771 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
774 qemu_co_mutex_lock(&s->send_mutex);
775 while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) {
776 qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
779 if (s->state != NBD_CLIENT_CONNECTED) {
786 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
787 if (s->requests[i].coroutine == NULL) {
792 g_assert(qemu_in_coroutine());
793 assert(i < MAX_NBD_REQUESTS);
795 s->requests[i].coroutine = qemu_coroutine_self();
796 s->requests[i].offset = request->from;
797 s->requests[i].receiving = false;
799 request->handle = INDEX_TO_HANDLE(s, i);
804 qio_channel_set_cork(s->ioc, true);
805 rc = nbd_send_request(s->ioc, request);
806 if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) {
807 if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
811 } else if (rc >= 0) {
814 qio_channel_set_cork(s->ioc, false);
816 rc = nbd_send_request(s->ioc, request);
821 nbd_channel_error(s, rc);
823 s->requests[i].coroutine = NULL;
826 if (s->in_flight == 0 && s->wait_in_flight) {
827 aio_co_wake(s->connection_co);
829 qemu_co_queue_next(&s->free_sema);
832 qemu_co_mutex_unlock(&s->send_mutex);
836 static inline uint16_t payload_advance16(uint8_t **payload)
839 return lduw_be_p(*payload - 2);
842 static inline uint32_t payload_advance32(uint8_t **payload)
845 return ldl_be_p(*payload - 4);
848 static inline uint64_t payload_advance64(uint8_t **payload)
851 return ldq_be_p(*payload - 8);
854 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
855 NBDStructuredReplyChunk *chunk,
856 uint8_t *payload, uint64_t orig_offset,
857 QEMUIOVector *qiov, Error **errp)
862 if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
863 error_setg(errp, "Protocol error: invalid payload for "
864 "NBD_REPLY_TYPE_OFFSET_HOLE");
868 offset = payload_advance64(&payload);
869 hole_size = payload_advance32(&payload);
871 if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
872 offset > orig_offset + qiov->size - hole_size) {
873 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
877 if (s->info.min_block &&
878 !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
879 trace_nbd_structured_read_compliance("hole");
882 qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
888 * nbd_parse_blockstatus_payload
889 * Based on our request, we expect only one extent in reply, for the
890 * base:allocation context.
892 static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
893 NBDStructuredReplyChunk *chunk,
894 uint8_t *payload, uint64_t orig_length,
895 NBDExtent *extent, Error **errp)
899 /* The server succeeded, so it must have sent [at least] one extent */
900 if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
901 error_setg(errp, "Protocol error: invalid payload for "
902 "NBD_REPLY_TYPE_BLOCK_STATUS");
906 context_id = payload_advance32(&payload);
907 if (s->info.context_id != context_id) {
908 error_setg(errp, "Protocol error: unexpected context id %d for "
909 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
910 "id is %d", context_id,
915 extent->length = payload_advance32(&payload);
916 extent->flags = payload_advance32(&payload);
918 if (extent->length == 0) {
919 error_setg(errp, "Protocol error: server sent status chunk with "
925 * A server sending unaligned block status is in violation of the
926 * protocol, but as qemu-nbd 3.1 is such a server (at least for
927 * POSIX files that are not a multiple of 512 bytes, since qemu
928 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
929 * still sees an implicit hole beyond the real EOF), it's nicer to
930 * work around the misbehaving server. If the request included
931 * more than the final unaligned block, truncate it back to an
932 * aligned result; if the request was only the final block, round
933 * up to the full block and change the status to fully-allocated
934 * (always a safe status, even if it loses information).
936 if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
937 s->info.min_block)) {
938 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
939 if (extent->length > s->info.min_block) {
940 extent->length = QEMU_ALIGN_DOWN(extent->length,
943 extent->length = s->info.min_block;
949 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
950 * sent us any more than one extent, nor should it have included
951 * status beyond our request in that extent. However, it's easy
952 * enough to ignore the server's noncompliance without killing the
953 * connection; just ignore trailing extents, and clamp things to
954 * the length of our request.
956 if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
957 trace_nbd_parse_blockstatus_compliance("more than one extent");
959 if (extent->length > orig_length) {
960 extent->length = orig_length;
961 trace_nbd_parse_blockstatus_compliance("extent length too large");
968 * nbd_parse_error_payload
969 * on success @errp contains message describing nbd error reply
971 static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
972 uint8_t *payload, int *request_ret,
976 uint16_t message_size;
978 assert(chunk->type & (1 << 15));
980 if (chunk->length < sizeof(error) + sizeof(message_size)) {
982 "Protocol error: invalid payload for structured error");
986 error = nbd_errno_to_system_errno(payload_advance32(&payload));
988 error_setg(errp, "Protocol error: server sent structured error chunk "
993 *request_ret = -error;
994 message_size = payload_advance16(&payload);
996 if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
997 error_setg(errp, "Protocol error: server sent structured error chunk "
998 "with incorrect message size");
1002 /* TODO: Add a trace point to mention the server complaint */
1004 /* TODO handle ERROR_OFFSET */
1009 static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
1010 uint64_t orig_offset,
1011 QEMUIOVector *qiov, Error **errp)
1013 QEMUIOVector sub_qiov;
1017 NBDStructuredReplyChunk *chunk = &s->reply.structured;
1019 assert(nbd_reply_is_structured(&s->reply));
1021 /* The NBD spec requires at least one byte of payload */
1022 if (chunk->length <= sizeof(offset)) {
1023 error_setg(errp, "Protocol error: invalid payload for "
1024 "NBD_REPLY_TYPE_OFFSET_DATA");
1028 if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
1032 data_size = chunk->length - sizeof(offset);
1034 if (offset < orig_offset || data_size > qiov->size ||
1035 offset > orig_offset + qiov->size - data_size) {
1036 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
1040 if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
1041 trace_nbd_structured_read_compliance("data");
1044 qemu_iovec_init(&sub_qiov, qiov->niov);
1045 qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
1046 ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
1047 qemu_iovec_destroy(&sub_qiov);
1049 return ret < 0 ? -EIO : 0;
1052 #define NBD_MAX_MALLOC_PAYLOAD 1000
1053 static coroutine_fn int nbd_co_receive_structured_payload(
1054 BDRVNBDState *s, void **payload, Error **errp)
1059 assert(nbd_reply_is_structured(&s->reply));
1061 len = s->reply.structured.length;
1067 if (payload == NULL) {
1068 error_setg(errp, "Unexpected structured payload");
1072 if (len > NBD_MAX_MALLOC_PAYLOAD) {
1073 error_setg(errp, "Payload too large");
1077 *payload = g_new(char, len);
1078 ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
1089 * nbd_co_do_receive_one_chunk
1091 * set request_ret to received reply error
1092 * if qiov is not NULL: read payload to @qiov
1093 * for structured reply chunk:
1094 * if error chunk: read payload, set @request_ret, do not set @payload
1095 * else if offset_data chunk: read payload data to @qiov, do not set @payload
1096 * else: read payload to @payload
1098 * If function fails, @errp contains corresponding error message, and the
1099 * connection with the server is suspect. If it returns 0, then the
1100 * transaction succeeded (although @request_ret may be a negative errno
1101 * corresponding to the server's error reply), and errp is unchanged.
1103 static coroutine_fn int nbd_co_do_receive_one_chunk(
1104 BDRVNBDState *s, uint64_t handle, bool only_structured,
1105 int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
1108 int i = HANDLE_TO_INDEX(s, handle);
1109 void *local_payload = NULL;
1110 NBDStructuredReplyChunk *chunk;
1117 /* Wait until we're woken up by nbd_connection_entry. */
1118 s->requests[i].receiving = true;
1119 qemu_coroutine_yield();
1120 s->requests[i].receiving = false;
1121 if (s->state != NBD_CLIENT_CONNECTED) {
1122 error_setg(errp, "Connection closed");
1127 assert(s->reply.handle == handle);
1129 if (nbd_reply_is_simple(&s->reply)) {
1130 if (only_structured) {
1131 error_setg(errp, "Protocol error: simple reply when structured "
1132 "reply chunk was expected");
1136 *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
1137 if (*request_ret < 0 || !qiov) {
1141 return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
1142 errp) < 0 ? -EIO : 0;
1145 /* handle structured reply chunk */
1146 assert(s->info.structured_reply);
1147 chunk = &s->reply.structured;
1149 if (chunk->type == NBD_REPLY_TYPE_NONE) {
1150 if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
1151 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
1152 " NBD_REPLY_FLAG_DONE flag set");
1155 if (chunk->length) {
1156 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
1163 if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
1165 error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
1169 return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
1173 if (nbd_reply_type_is_error(chunk->type)) {
1174 payload = &local_payload;
1177 ret = nbd_co_receive_structured_payload(s, payload, errp);
1182 if (nbd_reply_type_is_error(chunk->type)) {
1183 ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
1184 g_free(local_payload);
1192 * nbd_co_receive_one_chunk
1193 * Read reply, wake up connection_co and set s->quit if needed.
1194 * Return value is a fatal error code or normal nbd reply error code
1196 static coroutine_fn int nbd_co_receive_one_chunk(
1197 BDRVNBDState *s, uint64_t handle, bool only_structured,
1198 int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
1201 int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
1202 request_ret, qiov, payload, errp);
1205 memset(reply, 0, sizeof(*reply));
1206 nbd_channel_error(s, ret);
1208 /* For assert at loop start in nbd_connection_entry */
1211 s->reply.handle = 0;
1213 if (s->connection_co && !s->wait_in_flight) {
1215 * We must check s->wait_in_flight, because we may entered by
1216 * nbd_recv_coroutines_wake_all(), in this case we should not
1217 * wake connection_co here, it will woken by last request.
1219 aio_co_wake(s->connection_co);
1225 typedef struct NBDReplyChunkIter {
1229 bool done, only_structured;
1230 } NBDReplyChunkIter;
1232 static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
1233 int ret, Error **local_err)
1235 assert(local_err && *local_err);
1240 error_propagate(&iter->err, *local_err);
1242 error_free(*local_err);
1248 static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
1252 if (!iter->request_ret) {
1253 iter->request_ret = ret;
1258 * NBD_FOREACH_REPLY_CHUNK
1259 * The pointer stored in @payload requires g_free() to free it.
1261 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
1262 qiov, reply, payload) \
1263 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
1264 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
1267 * nbd_reply_chunk_iter_receive
1268 * The pointer stored in @payload requires g_free() to free it.
1270 static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
1271 NBDReplyChunkIter *iter,
1273 QEMUIOVector *qiov, NBDReply *reply,
1276 int ret, request_ret;
1277 NBDReply local_reply;
1278 NBDStructuredReplyChunk *chunk;
1279 Error *local_err = NULL;
1280 if (s->state != NBD_CLIENT_CONNECTED) {
1281 error_setg(&local_err, "Connection closed");
1282 nbd_iter_channel_error(iter, -EIO, &local_err);
1287 /* Previous iteration was last. */
1291 if (reply == NULL) {
1292 reply = &local_reply;
1295 ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
1296 &request_ret, qiov, reply, payload,
1299 nbd_iter_channel_error(iter, ret, &local_err);
1300 } else if (request_ret < 0) {
1301 nbd_iter_request_error(iter, request_ret);
1304 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
1305 if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) {
1309 chunk = &reply->structured;
1310 iter->only_structured = true;
1312 if (chunk->type == NBD_REPLY_TYPE_NONE) {
1313 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1314 assert(chunk->flags & NBD_REPLY_FLAG_DONE);
1318 if (chunk->flags & NBD_REPLY_FLAG_DONE) {
1319 /* This iteration is last. */
1323 /* Execute the loop body */
1327 s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
1329 qemu_co_mutex_lock(&s->send_mutex);
1331 if (s->in_flight == 0 && s->wait_in_flight) {
1332 aio_co_wake(s->connection_co);
1334 qemu_co_queue_next(&s->free_sema);
1336 qemu_co_mutex_unlock(&s->send_mutex);
1341 static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
1342 int *request_ret, Error **errp)
1344 NBDReplyChunkIter iter;
1346 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
1347 /* nbd_reply_chunk_iter_receive does all the work */
1350 error_propagate(errp, iter.err);
1351 *request_ret = iter.request_ret;
1355 static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
1356 uint64_t offset, QEMUIOVector *qiov,
1357 int *request_ret, Error **errp)
1359 NBDReplyChunkIter iter;
1361 void *payload = NULL;
1362 Error *local_err = NULL;
1364 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
1365 qiov, &reply, &payload)
1368 NBDStructuredReplyChunk *chunk = &reply.structured;
1370 assert(nbd_reply_is_structured(&reply));
1372 switch (chunk->type) {
1373 case NBD_REPLY_TYPE_OFFSET_DATA:
1375 * special cased in nbd_co_receive_one_chunk, data is already
1379 case NBD_REPLY_TYPE_OFFSET_HOLE:
1380 ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
1381 offset, qiov, &local_err);
1383 nbd_channel_error(s, ret);
1384 nbd_iter_channel_error(&iter, ret, &local_err);
1388 if (!nbd_reply_type_is_error(chunk->type)) {
1389 /* not allowed reply type */
1390 nbd_channel_error(s, -EINVAL);
1391 error_setg(&local_err,
1392 "Unexpected reply type: %d (%s) for CMD_READ",
1393 chunk->type, nbd_reply_type_lookup(chunk->type));
1394 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1402 error_propagate(errp, iter.err);
1403 *request_ret = iter.request_ret;
1407 static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
1408 uint64_t handle, uint64_t length,
1410 int *request_ret, Error **errp)
1412 NBDReplyChunkIter iter;
1414 void *payload = NULL;
1415 Error *local_err = NULL;
1416 bool received = false;
1418 assert(!extent->length);
1419 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
1421 NBDStructuredReplyChunk *chunk = &reply.structured;
1423 assert(nbd_reply_is_structured(&reply));
1425 switch (chunk->type) {
1426 case NBD_REPLY_TYPE_BLOCK_STATUS:
1428 nbd_channel_error(s, -EINVAL);
1429 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
1430 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1434 ret = nbd_parse_blockstatus_payload(s, &reply.structured,
1435 payload, length, extent,
1438 nbd_channel_error(s, ret);
1439 nbd_iter_channel_error(&iter, ret, &local_err);
1443 if (!nbd_reply_type_is_error(chunk->type)) {
1444 nbd_channel_error(s, -EINVAL);
1445 error_setg(&local_err,
1446 "Unexpected reply type: %d (%s) "
1447 "for CMD_BLOCK_STATUS",
1448 chunk->type, nbd_reply_type_lookup(chunk->type));
1449 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1457 if (!extent->length && !iter.request_ret) {
1458 error_setg(&local_err, "Server did not reply with any status extents");
1459 nbd_iter_channel_error(&iter, -EIO, &local_err);
1462 error_propagate(errp, iter.err);
1463 *request_ret = iter.request_ret;
1467 static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
1468 QEMUIOVector *write_qiov)
1470 int ret, request_ret;
1471 Error *local_err = NULL;
1472 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1474 assert(request->type != NBD_CMD_READ);
1476 assert(request->type == NBD_CMD_WRITE);
1477 assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
1479 assert(request->type != NBD_CMD_WRITE);
1483 ret = nbd_co_send_request(bs, request, write_qiov);
1488 ret = nbd_co_receive_return_code(s, request->handle,
1489 &request_ret, &local_err);
1491 trace_nbd_co_request_fail(request->from, request->len,
1492 request->handle, request->flags,
1494 nbd_cmd_lookup(request->type),
1495 ret, error_get_pretty(local_err));
1496 error_free(local_err);
1499 } while (ret < 0 && nbd_client_connecting_wait(s));
1501 return ret ? ret : request_ret;
1504 static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
1505 uint64_t bytes, QEMUIOVector *qiov, int flags)
1507 int ret, request_ret;
1508 Error *local_err = NULL;
1509 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1510 NBDRequest request = {
1511 .type = NBD_CMD_READ,
1516 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1523 * Work around the fact that the block layer doesn't do
1524 * byte-accurate sizing yet - if the read exceeds the server's
1525 * advertised size because the block layer rounded size up, then
1526 * truncate the request to the server and tail-pad with zero.
1528 if (offset >= s->info.size) {
1529 assert(bytes < BDRV_SECTOR_SIZE);
1530 qemu_iovec_memset(qiov, 0, 0, bytes);
1533 if (offset + bytes > s->info.size) {
1534 uint64_t slop = offset + bytes - s->info.size;
1536 assert(slop < BDRV_SECTOR_SIZE);
1537 qemu_iovec_memset(qiov, bytes - slop, 0, slop);
1538 request.len -= slop;
1542 ret = nbd_co_send_request(bs, &request, NULL);
1547 ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
1548 &request_ret, &local_err);
1550 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1551 request.flags, request.type,
1552 nbd_cmd_lookup(request.type),
1553 ret, error_get_pretty(local_err));
1554 error_free(local_err);
1557 } while (ret < 0 && nbd_client_connecting_wait(s));
1559 return ret ? ret : request_ret;
1562 static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
1563 uint64_t bytes, QEMUIOVector *qiov, int flags)
1565 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1566 NBDRequest request = {
1567 .type = NBD_CMD_WRITE,
1572 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1573 if (flags & BDRV_REQ_FUA) {
1574 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1575 request.flags |= NBD_CMD_FLAG_FUA;
1578 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1583 return nbd_co_request(bs, &request, qiov);
1586 static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1587 int bytes, BdrvRequestFlags flags)
1589 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1590 NBDRequest request = {
1591 .type = NBD_CMD_WRITE_ZEROES,
1596 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1597 if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1601 if (flags & BDRV_REQ_FUA) {
1602 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1603 request.flags |= NBD_CMD_FLAG_FUA;
1605 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1606 request.flags |= NBD_CMD_FLAG_NO_HOLE;
1608 if (flags & BDRV_REQ_NO_FALLBACK) {
1609 assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1610 request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1616 return nbd_co_request(bs, &request, NULL);
1619 static int nbd_client_co_flush(BlockDriverState *bs)
1621 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1622 NBDRequest request = { .type = NBD_CMD_FLUSH };
1624 if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1631 return nbd_co_request(bs, &request, NULL);
1634 static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
1637 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1638 NBDRequest request = {
1639 .type = NBD_CMD_TRIM,
1644 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1645 if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1649 return nbd_co_request(bs, &request, NULL);
1652 static int coroutine_fn nbd_client_co_block_status(
1653 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
1654 int64_t *pnum, int64_t *map, BlockDriverState **file)
1656 int ret, request_ret;
1657 NBDExtent extent = { 0 };
1658 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1659 Error *local_err = NULL;
1661 NBDRequest request = {
1662 .type = NBD_CMD_BLOCK_STATUS,
1664 .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment),
1665 MIN(bytes, s->info.size - offset)),
1666 .flags = NBD_CMD_FLAG_REQ_ONE,
1669 if (!s->info.base_allocation) {
1673 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1677 * Work around the fact that the block layer doesn't do
1678 * byte-accurate sizing yet - if the status request exceeds the
1679 * server's advertised size because the block layer rounded size
1680 * up, we truncated the request to the server (above), or are
1681 * called on just the hole.
1683 if (offset >= s->info.size) {
1685 assert(bytes < BDRV_SECTOR_SIZE);
1686 /* Intentionally don't report offset_valid for the hole */
1687 return BDRV_BLOCK_ZERO;
1690 if (s->info.min_block) {
1691 assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1694 ret = nbd_co_send_request(bs, &request, NULL);
1699 ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1700 &extent, &request_ret,
1703 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1704 request.flags, request.type,
1705 nbd_cmd_lookup(request.type),
1706 ret, error_get_pretty(local_err));
1707 error_free(local_err);
1710 } while (ret < 0 && nbd_client_connecting_wait(s));
1712 if (ret < 0 || request_ret < 0) {
1713 return ret ? ret : request_ret;
1716 assert(extent.length);
1717 *pnum = extent.length;
1720 return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1721 (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1722 BDRV_BLOCK_OFFSET_VALID;
1725 static int nbd_client_reopen_prepare(BDRVReopenState *state,
1726 BlockReopenQueue *queue, Error **errp)
1728 BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1730 if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1731 error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1737 static void nbd_client_close(BlockDriverState *bs)
1739 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1740 NBDRequest request = { .type = NBD_CMD_DISC };
1743 nbd_send_request(s->ioc, &request);
1746 nbd_teardown_connection(bs);
1749 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
1753 QIOChannelSocket *sioc;
1755 sioc = qio_channel_socket_new();
1756 qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
1758 qio_channel_socket_connect_sync(sioc, saddr, errp);
1760 object_unref(OBJECT(sioc));
1764 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
1769 /* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
1770 static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
1773 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1774 AioContext *aio_context = bdrv_get_aio_context(bs);
1777 trace_nbd_client_handshake(s->export);
1781 qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
1782 qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
1784 s->info.request_sizes = true;
1785 s->info.structured_reply = true;
1786 s->info.base_allocation = true;
1787 s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap);
1788 s->info.name = g_strdup(s->export ?: "");
1789 ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds,
1790 s->hostname, &s->ioc, &s->info, errp);
1791 g_free(s->info.x_dirty_bitmap);
1792 g_free(s->info.name);
1794 object_unref(OBJECT(sioc));
1798 if (s->x_dirty_bitmap && !s->info.base_allocation) {
1799 error_setg(errp, "requested x-dirty-bitmap %s not found",
1804 if (s->info.flags & NBD_FLAG_READ_ONLY) {
1805 ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
1810 if (s->info.flags & NBD_FLAG_SEND_FUA) {
1811 bs->supported_write_flags = BDRV_REQ_FUA;
1812 bs->supported_zero_flags |= BDRV_REQ_FUA;
1814 if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
1815 bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
1816 if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
1817 bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
1822 s->ioc = QIO_CHANNEL(sioc);
1823 object_ref(OBJECT(s->ioc));
1826 trace_nbd_client_handshake_success(s->export);
1832 * We have connected, but must fail for other reasons.
1833 * Send NBD_CMD_DISC as a courtesy to the server.
1836 NBDRequest request = { .type = NBD_CMD_DISC };
1838 nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request);
1840 object_unref(OBJECT(sioc));
1848 * Parse nbd_open options
1851 static int nbd_parse_uri(const char *filename, QDict *options)
1855 QueryParams *qp = NULL;
1859 uri = uri_parse(filename);
1865 if (!g_strcmp0(uri->scheme, "nbd")) {
1867 } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1869 } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1876 p = uri->path ? uri->path : "";
1881 qdict_put_str(options, "export", p);
1884 qp = query_params_parse(uri->query);
1885 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1891 /* nbd+unix:///export?socket=path */
1892 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
1896 qdict_put_str(options, "server.type", "unix");
1897 qdict_put_str(options, "server.path", qp->p[0].value);
1902 /* nbd[+tcp]://host[:port]/export */
1908 /* strip braces from literal IPv6 address */
1909 if (uri->server[0] == '[') {
1910 host = qstring_from_substr(uri->server, 1,
1911 strlen(uri->server) - 1);
1913 host = qstring_from_str(uri->server);
1916 qdict_put_str(options, "server.type", "inet");
1917 qdict_put(options, "server.host", host);
1919 port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
1920 qdict_put_str(options, "server.port", port_str);
1926 query_params_free(qp);
1932 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1934 const QDictEntry *e;
1936 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1937 if (!strcmp(e->key, "host") ||
1938 !strcmp(e->key, "port") ||
1939 !strcmp(e->key, "path") ||
1940 !strcmp(e->key, "export") ||
1941 strstart(e->key, "server.", NULL))
1943 error_setg(errp, "Option '%s' cannot be used with a file name",
1952 static void nbd_parse_filename(const char *filename, QDict *options,
1955 g_autofree char *file = NULL;
1957 const char *host_spec;
1958 const char *unixpath;
1960 if (nbd_has_filename_options_conflict(options, errp)) {
1964 if (strstr(filename, "://")) {
1965 int ret = nbd_parse_uri(filename, options);
1967 error_setg(errp, "No valid URL specified");
1972 file = g_strdup(filename);
1974 export_name = strstr(file, EN_OPTSTR);
1976 if (export_name[strlen(EN_OPTSTR)] == 0) {
1979 export_name[0] = 0; /* truncate 'file' */
1980 export_name += strlen(EN_OPTSTR);
1982 qdict_put_str(options, "export", export_name);
1985 /* extract the host_spec - fail if it's not nbd:... */
1986 if (!strstart(file, "nbd:", &host_spec)) {
1987 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1995 /* are we a UNIX or TCP socket? */
1996 if (strstart(host_spec, "unix:", &unixpath)) {
1997 qdict_put_str(options, "server.type", "unix");
1998 qdict_put_str(options, "server.path", unixpath);
2000 InetSocketAddress *addr = g_new(InetSocketAddress, 1);
2002 if (inet_parse(addr, host_spec, errp)) {
2006 qdict_put_str(options, "server.type", "inet");
2007 qdict_put_str(options, "server.host", addr->host);
2008 qdict_put_str(options, "server.port", addr->port);
2010 qapi_free_InetSocketAddress(addr);
2014 static bool nbd_process_legacy_socket_options(QDict *output_options,
2015 QemuOpts *legacy_opts,
2018 const char *path = qemu_opt_get(legacy_opts, "path");
2019 const char *host = qemu_opt_get(legacy_opts, "host");
2020 const char *port = qemu_opt_get(legacy_opts, "port");
2021 const QDictEntry *e;
2023 if (!path && !host && !port) {
2027 for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
2029 if (strstart(e->key, "server.", NULL)) {
2030 error_setg(errp, "Cannot use 'server' and path/host/port at the "
2037 error_setg(errp, "path and host may not be used at the same time");
2041 error_setg(errp, "port may not be used without host");
2045 qdict_put_str(output_options, "server.type", "unix");
2046 qdict_put_str(output_options, "server.path", path);
2048 qdict_put_str(output_options, "server.type", "inet");
2049 qdict_put_str(output_options, "server.host", host);
2050 qdict_put_str(output_options, "server.port",
2051 port ?: stringify(NBD_DEFAULT_PORT));
2057 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
2060 SocketAddress *saddr = NULL;
2064 qdict_extract_subqdict(options, &addr, "server.");
2065 if (!qdict_size(addr)) {
2066 error_setg(errp, "NBD server address missing");
2070 iv = qobject_input_visitor_new_flat_confused(addr, errp);
2075 if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) {
2080 qobject_unref(addr);
2085 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
2088 QCryptoTLSCreds *creds;
2090 obj = object_resolve_path_component(
2091 object_get_objects_root(), id);
2093 error_setg(errp, "No TLS credentials with id '%s'",
2097 creds = (QCryptoTLSCreds *)
2098 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
2100 error_setg(errp, "Object with id '%s' is not TLS credentials",
2105 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
2107 "Expecting TLS credentials with a client endpoint");
2115 static QemuOptsList nbd_runtime_opts = {
2117 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
2121 .type = QEMU_OPT_STRING,
2122 .help = "TCP host to connect to",
2126 .type = QEMU_OPT_STRING,
2127 .help = "TCP port to connect to",
2131 .type = QEMU_OPT_STRING,
2132 .help = "Unix socket path to connect to",
2136 .type = QEMU_OPT_STRING,
2137 .help = "Name of the NBD export to open",
2140 .name = "tls-creds",
2141 .type = QEMU_OPT_STRING,
2142 .help = "ID of the TLS credentials to use",
2145 .name = "x-dirty-bitmap",
2146 .type = QEMU_OPT_STRING,
2147 .help = "experimental: expose named dirty bitmap in place of "
2151 .name = "reconnect-delay",
2152 .type = QEMU_OPT_NUMBER,
2153 .help = "On an unexpected disconnect, the nbd client tries to "
2154 "connect again until succeeding or encountering a serious "
2155 "error. During the first @reconnect-delay seconds, all "
2156 "requests are paused and will be rerun on a successful "
2157 "reconnect. After that time, any delayed requests and all "
2158 "future requests before a successful reconnect will "
2159 "immediately fail. Default 0",
2161 { /* end of list */ }
2165 static int nbd_process_options(BlockDriverState *bs, QDict *options,
2168 BDRVNBDState *s = bs->opaque;
2172 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
2173 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
2177 /* Translate @host, @port, and @path to a SocketAddress */
2178 if (!nbd_process_legacy_socket_options(options, opts, errp)) {
2182 /* Pop the config into our state object. Exit if invalid. */
2183 s->saddr = nbd_config(s, options, errp);
2188 s->export = g_strdup(qemu_opt_get(opts, "export"));
2189 if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
2190 error_setg(errp, "export name too long to send to server");
2194 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
2195 if (s->tlscredsid) {
2196 s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
2201 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
2202 if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
2203 error_setg(errp, "TLS only supported over IP sockets");
2206 s->hostname = s->saddr->u.inet.host;
2209 s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
2210 if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
2211 error_setg(errp, "x-dirty-bitmap query too long to send to server");
2215 s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
2221 nbd_clear_bdrvstate(s);
2223 qemu_opts_del(opts);
2227 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
2231 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
2232 QIOChannelSocket *sioc;
2234 ret = nbd_process_options(bs, options, errp);
2240 qemu_co_mutex_init(&s->send_mutex);
2241 qemu_co_queue_init(&s->free_sema);
2244 * establish TCP connection, return error if it fails
2245 * TODO: Configurable retry-until-timeout behaviour.
2247 sioc = nbd_establish_connection(s->saddr, errp);
2249 return -ECONNREFUSED;
2252 ret = nbd_client_handshake(bs, sioc, errp);
2254 nbd_clear_bdrvstate(s);
2257 /* successfully connected */
2258 s->state = NBD_CLIENT_CONNECTED;
2260 nbd_init_connect_thread(s);
2262 s->connection_co = qemu_coroutine_create(nbd_connection_entry, s);
2263 bdrv_inc_in_flight(bs);
2264 aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co);
2269 static int nbd_co_flush(BlockDriverState *bs)
2271 return nbd_client_co_flush(bs);
2274 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
2276 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
2277 uint32_t min = s->info.min_block;
2278 uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
2281 * If the server did not advertise an alignment:
2282 * - a size that is not sector-aligned implies that an alignment
2283 * of 1 can be used to access those tail bytes
2284 * - advertisement of block status requires an alignment of 1, so
2285 * that we don't violate block layer constraints that block
2286 * status is always aligned (as we can't control whether the
2287 * server will report sub-sector extents, such as a hole at EOF
2288 * on an unaligned POSIX file)
2289 * - otherwise, assume the server is so old that we are safer avoiding
2290 * sub-sector requests
2293 min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
2294 s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
2297 bs->bl.request_alignment = min;
2298 bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min);
2299 bs->bl.max_pwrite_zeroes = max;
2300 bs->bl.max_transfer = max;
2302 if (s->info.opt_block &&
2303 s->info.opt_block > bs->bl.opt_transfer) {
2304 bs->bl.opt_transfer = s->info.opt_block;
2308 static void nbd_close(BlockDriverState *bs)
2310 BDRVNBDState *s = bs->opaque;
2312 nbd_client_close(bs);
2313 nbd_clear_bdrvstate(s);
2317 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2318 * to a smaller size with exact=false, there is no reason to fail the
2321 * Preallocation mode is ignored since it does not seems useful to fail when
2322 * we never change anything.
2324 static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset,
2325 bool exact, PreallocMode prealloc,
2326 BdrvRequestFlags flags, Error **errp)
2328 BDRVNBDState *s = bs->opaque;
2330 if (offset != s->info.size && exact) {
2331 error_setg(errp, "Cannot resize NBD nodes");
2335 if (offset > s->info.size) {
2336 error_setg(errp, "Cannot grow NBD nodes");
2343 static int64_t nbd_getlength(BlockDriverState *bs)
2345 BDRVNBDState *s = bs->opaque;
2347 return s->info.size;
2350 static void nbd_refresh_filename(BlockDriverState *bs)
2352 BDRVNBDState *s = bs->opaque;
2353 const char *host = NULL, *port = NULL, *path = NULL;
2356 if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
2357 const InetSocketAddress *inet = &s->saddr->u.inet;
2358 if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
2362 } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
2363 path = s->saddr->u.q_unix.path;
2364 } /* else can't represent as pseudo-filename */
2366 if (path && s->export) {
2367 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2368 "nbd+unix:///%s?socket=%s", s->export, path);
2369 } else if (path && !s->export) {
2370 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2371 "nbd+unix://?socket=%s", path);
2372 } else if (host && s->export) {
2373 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2374 "nbd://%s:%s/%s", host, port, s->export);
2375 } else if (host && !s->export) {
2376 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2377 "nbd://%s:%s", host, port);
2379 if (len >= sizeof(bs->exact_filename)) {
2380 /* Name is too long to represent exactly, so leave it empty. */
2381 bs->exact_filename[0] = '\0';
2385 static char *nbd_dirname(BlockDriverState *bs, Error **errp)
2387 /* The generic bdrv_dirname() implementation is able to work out some
2388 * directory name for NBD nodes, but that would be wrong. So far there is no
2389 * specification for how "export paths" would work, so NBD does not have
2390 * directory names. */
2391 error_setg(errp, "Cannot generate a base directory for NBD nodes");
2395 static const char *const nbd_strong_runtime_opts[] = {
2406 static BlockDriver bdrv_nbd = {
2407 .format_name = "nbd",
2408 .protocol_name = "nbd",
2409 .instance_size = sizeof(BDRVNBDState),
2410 .bdrv_parse_filename = nbd_parse_filename,
2411 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2412 .create_opts = &bdrv_create_opts_simple,
2413 .bdrv_file_open = nbd_open,
2414 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2415 .bdrv_co_preadv = nbd_client_co_preadv,
2416 .bdrv_co_pwritev = nbd_client_co_pwritev,
2417 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2418 .bdrv_close = nbd_close,
2419 .bdrv_co_flush_to_os = nbd_co_flush,
2420 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2421 .bdrv_refresh_limits = nbd_refresh_limits,
2422 .bdrv_co_truncate = nbd_co_truncate,
2423 .bdrv_getlength = nbd_getlength,
2424 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2425 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2426 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2427 .bdrv_co_drain_end = nbd_client_co_drain_end,
2428 .bdrv_refresh_filename = nbd_refresh_filename,
2429 .bdrv_co_block_status = nbd_client_co_block_status,
2430 .bdrv_dirname = nbd_dirname,
2431 .strong_runtime_opts = nbd_strong_runtime_opts,
2434 static BlockDriver bdrv_nbd_tcp = {
2435 .format_name = "nbd",
2436 .protocol_name = "nbd+tcp",
2437 .instance_size = sizeof(BDRVNBDState),
2438 .bdrv_parse_filename = nbd_parse_filename,
2439 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2440 .create_opts = &bdrv_create_opts_simple,
2441 .bdrv_file_open = nbd_open,
2442 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2443 .bdrv_co_preadv = nbd_client_co_preadv,
2444 .bdrv_co_pwritev = nbd_client_co_pwritev,
2445 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2446 .bdrv_close = nbd_close,
2447 .bdrv_co_flush_to_os = nbd_co_flush,
2448 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2449 .bdrv_refresh_limits = nbd_refresh_limits,
2450 .bdrv_co_truncate = nbd_co_truncate,
2451 .bdrv_getlength = nbd_getlength,
2452 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2453 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2454 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2455 .bdrv_co_drain_end = nbd_client_co_drain_end,
2456 .bdrv_refresh_filename = nbd_refresh_filename,
2457 .bdrv_co_block_status = nbd_client_co_block_status,
2458 .bdrv_dirname = nbd_dirname,
2459 .strong_runtime_opts = nbd_strong_runtime_opts,
2462 static BlockDriver bdrv_nbd_unix = {
2463 .format_name = "nbd",
2464 .protocol_name = "nbd+unix",
2465 .instance_size = sizeof(BDRVNBDState),
2466 .bdrv_parse_filename = nbd_parse_filename,
2467 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2468 .create_opts = &bdrv_create_opts_simple,
2469 .bdrv_file_open = nbd_open,
2470 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2471 .bdrv_co_preadv = nbd_client_co_preadv,
2472 .bdrv_co_pwritev = nbd_client_co_pwritev,
2473 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2474 .bdrv_close = nbd_close,
2475 .bdrv_co_flush_to_os = nbd_co_flush,
2476 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2477 .bdrv_refresh_limits = nbd_refresh_limits,
2478 .bdrv_co_truncate = nbd_co_truncate,
2479 .bdrv_getlength = nbd_getlength,
2480 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2481 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2482 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2483 .bdrv_co_drain_end = nbd_client_co_drain_end,
2484 .bdrv_refresh_filename = nbd_refresh_filename,
2485 .bdrv_co_block_status = nbd_client_co_block_status,
2486 .bdrv_dirname = nbd_dirname,
2487 .strong_runtime_opts = nbd_strong_runtime_opts,
2490 static void bdrv_nbd_init(void)
2492 bdrv_register(&bdrv_nbd);
2493 bdrv_register(&bdrv_nbd_tcp);
2494 bdrv_register(&bdrv_nbd_unix);
2497 block_init(bdrv_nbd_init);