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
);
664 if (timeout
< max_timeout
) {
669 nbd_reconnect_attempt(s
);
672 reconnect_delay_timer_del(s
);
675 static coroutine_fn
void nbd_connection_entry(void *opaque
)
677 BDRVNBDState
*s
= opaque
;
680 Error
*local_err
= NULL
;
682 while (s
->state
!= NBD_CLIENT_QUIT
) {
684 * The NBD client can only really be considered idle when it has
685 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
686 * the point where the additional scheduled coroutine entry happens
687 * after nbd_client_attach_aio_context().
689 * Therefore we keep an additional in_flight reference all the time and
690 * only drop it temporarily here.
693 if (nbd_client_connecting(s
)) {
694 nbd_co_reconnect_loop(s
);
697 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
701 assert(s
->reply
.handle
== 0);
702 ret
= nbd_receive_reply(s
->bs
, s
->ioc
, &s
->reply
, &local_err
);
705 trace_nbd_read_reply_entry_fail(ret
, error_get_pretty(local_err
));
706 error_free(local_err
);
710 nbd_channel_error(s
, ret
? ret
: -EIO
);
715 * There's no need for a mutex on the receive side, because the
716 * handler acts as a synchronization point and ensures that only
717 * one coroutine is called until the reply finishes.
719 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
720 if (i
>= MAX_NBD_REQUESTS
||
721 !s
->requests
[i
].coroutine
||
722 !s
->requests
[i
].receiving
||
723 (nbd_reply_is_structured(&s
->reply
) && !s
->info
.structured_reply
))
725 nbd_channel_error(s
, -EINVAL
);
730 * We're woken up again by the request itself. Note that there
731 * is no race between yielding and reentering connection_co. This
734 * - if the request runs on the same AioContext, it is only
735 * entered after we yield
737 * - if the request runs on a different AioContext, reentering
738 * connection_co happens through a bottom half, which can only
739 * run after we yield.
741 aio_co_wake(s
->requests
[i
].coroutine
);
742 qemu_coroutine_yield();
745 qemu_co_queue_restart_all(&s
->free_sema
);
746 nbd_recv_coroutines_wake_all(s
);
747 bdrv_dec_in_flight(s
->bs
);
749 s
->connection_co
= NULL
;
751 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
752 object_unref(OBJECT(s
->sioc
));
754 object_unref(OBJECT(s
->ioc
));
758 if (s
->teardown_co
) {
759 aio_co_wake(s
->teardown_co
);
764 static int nbd_co_send_request(BlockDriverState
*bs
,
768 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
771 qemu_co_mutex_lock(&s
->send_mutex
);
772 while (s
->in_flight
== MAX_NBD_REQUESTS
|| nbd_client_connecting_wait(s
)) {
773 qemu_co_queue_wait(&s
->free_sema
, &s
->send_mutex
);
776 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
783 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
784 if (s
->requests
[i
].coroutine
== NULL
) {
789 g_assert(qemu_in_coroutine());
790 assert(i
< MAX_NBD_REQUESTS
);
792 s
->requests
[i
].coroutine
= qemu_coroutine_self();
793 s
->requests
[i
].offset
= request
->from
;
794 s
->requests
[i
].receiving
= false;
796 request
->handle
= INDEX_TO_HANDLE(s
, i
);
801 qio_channel_set_cork(s
->ioc
, true);
802 rc
= nbd_send_request(s
->ioc
, request
);
803 if (rc
>= 0 && s
->state
== NBD_CLIENT_CONNECTED
) {
804 if (qio_channel_writev_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
808 } else if (rc
>= 0) {
811 qio_channel_set_cork(s
->ioc
, false);
813 rc
= nbd_send_request(s
->ioc
, request
);
818 nbd_channel_error(s
, rc
);
820 s
->requests
[i
].coroutine
= NULL
;
823 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
824 aio_co_wake(s
->connection_co
);
826 qemu_co_queue_next(&s
->free_sema
);
829 qemu_co_mutex_unlock(&s
->send_mutex
);
833 static inline uint16_t payload_advance16(uint8_t **payload
)
836 return lduw_be_p(*payload
- 2);
839 static inline uint32_t payload_advance32(uint8_t **payload
)
842 return ldl_be_p(*payload
- 4);
845 static inline uint64_t payload_advance64(uint8_t **payload
)
848 return ldq_be_p(*payload
- 8);
851 static int nbd_parse_offset_hole_payload(BDRVNBDState
*s
,
852 NBDStructuredReplyChunk
*chunk
,
853 uint8_t *payload
, uint64_t orig_offset
,
854 QEMUIOVector
*qiov
, Error
**errp
)
859 if (chunk
->length
!= sizeof(offset
) + sizeof(hole_size
)) {
860 error_setg(errp
, "Protocol error: invalid payload for "
861 "NBD_REPLY_TYPE_OFFSET_HOLE");
865 offset
= payload_advance64(&payload
);
866 hole_size
= payload_advance32(&payload
);
868 if (!hole_size
|| offset
< orig_offset
|| hole_size
> qiov
->size
||
869 offset
> orig_offset
+ qiov
->size
- hole_size
) {
870 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
874 if (s
->info
.min_block
&&
875 !QEMU_IS_ALIGNED(hole_size
, s
->info
.min_block
)) {
876 trace_nbd_structured_read_compliance("hole");
879 qemu_iovec_memset(qiov
, offset
- orig_offset
, 0, hole_size
);
885 * nbd_parse_blockstatus_payload
886 * Based on our request, we expect only one extent in reply, for the
887 * base:allocation context.
889 static int nbd_parse_blockstatus_payload(BDRVNBDState
*s
,
890 NBDStructuredReplyChunk
*chunk
,
891 uint8_t *payload
, uint64_t orig_length
,
892 NBDExtent
*extent
, Error
**errp
)
896 /* The server succeeded, so it must have sent [at least] one extent */
897 if (chunk
->length
< sizeof(context_id
) + sizeof(*extent
)) {
898 error_setg(errp
, "Protocol error: invalid payload for "
899 "NBD_REPLY_TYPE_BLOCK_STATUS");
903 context_id
= payload_advance32(&payload
);
904 if (s
->info
.context_id
!= context_id
) {
905 error_setg(errp
, "Protocol error: unexpected context id %d for "
906 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
907 "id is %d", context_id
,
912 extent
->length
= payload_advance32(&payload
);
913 extent
->flags
= payload_advance32(&payload
);
915 if (extent
->length
== 0) {
916 error_setg(errp
, "Protocol error: server sent status chunk with "
922 * A server sending unaligned block status is in violation of the
923 * protocol, but as qemu-nbd 3.1 is such a server (at least for
924 * POSIX files that are not a multiple of 512 bytes, since qemu
925 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
926 * still sees an implicit hole beyond the real EOF), it's nicer to
927 * work around the misbehaving server. If the request included
928 * more than the final unaligned block, truncate it back to an
929 * aligned result; if the request was only the final block, round
930 * up to the full block and change the status to fully-allocated
931 * (always a safe status, even if it loses information).
933 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(extent
->length
,
934 s
->info
.min_block
)) {
935 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
936 if (extent
->length
> s
->info
.min_block
) {
937 extent
->length
= QEMU_ALIGN_DOWN(extent
->length
,
940 extent
->length
= s
->info
.min_block
;
946 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
947 * sent us any more than one extent, nor should it have included
948 * status beyond our request in that extent. However, it's easy
949 * enough to ignore the server's noncompliance without killing the
950 * connection; just ignore trailing extents, and clamp things to
951 * the length of our request.
953 if (chunk
->length
> sizeof(context_id
) + sizeof(*extent
)) {
954 trace_nbd_parse_blockstatus_compliance("more than one extent");
956 if (extent
->length
> orig_length
) {
957 extent
->length
= orig_length
;
958 trace_nbd_parse_blockstatus_compliance("extent length too large");
965 * nbd_parse_error_payload
966 * on success @errp contains message describing nbd error reply
968 static int nbd_parse_error_payload(NBDStructuredReplyChunk
*chunk
,
969 uint8_t *payload
, int *request_ret
,
973 uint16_t message_size
;
975 assert(chunk
->type
& (1 << 15));
977 if (chunk
->length
< sizeof(error
) + sizeof(message_size
)) {
979 "Protocol error: invalid payload for structured error");
983 error
= nbd_errno_to_system_errno(payload_advance32(&payload
));
985 error_setg(errp
, "Protocol error: server sent structured error chunk "
990 *request_ret
= -error
;
991 message_size
= payload_advance16(&payload
);
993 if (message_size
> chunk
->length
- sizeof(error
) - sizeof(message_size
)) {
994 error_setg(errp
, "Protocol error: server sent structured error chunk "
995 "with incorrect message size");
999 /* TODO: Add a trace point to mention the server complaint */
1001 /* TODO handle ERROR_OFFSET */
1006 static int nbd_co_receive_offset_data_payload(BDRVNBDState
*s
,
1007 uint64_t orig_offset
,
1008 QEMUIOVector
*qiov
, Error
**errp
)
1010 QEMUIOVector sub_qiov
;
1014 NBDStructuredReplyChunk
*chunk
= &s
->reply
.structured
;
1016 assert(nbd_reply_is_structured(&s
->reply
));
1018 /* The NBD spec requires at least one byte of payload */
1019 if (chunk
->length
<= sizeof(offset
)) {
1020 error_setg(errp
, "Protocol error: invalid payload for "
1021 "NBD_REPLY_TYPE_OFFSET_DATA");
1025 if (nbd_read64(s
->ioc
, &offset
, "OFFSET_DATA offset", errp
) < 0) {
1029 data_size
= chunk
->length
- sizeof(offset
);
1031 if (offset
< orig_offset
|| data_size
> qiov
->size
||
1032 offset
> orig_offset
+ qiov
->size
- data_size
) {
1033 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
1037 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(data_size
, s
->info
.min_block
)) {
1038 trace_nbd_structured_read_compliance("data");
1041 qemu_iovec_init(&sub_qiov
, qiov
->niov
);
1042 qemu_iovec_concat(&sub_qiov
, qiov
, offset
- orig_offset
, data_size
);
1043 ret
= qio_channel_readv_all(s
->ioc
, sub_qiov
.iov
, sub_qiov
.niov
, errp
);
1044 qemu_iovec_destroy(&sub_qiov
);
1046 return ret
< 0 ? -EIO
: 0;
1049 #define NBD_MAX_MALLOC_PAYLOAD 1000
1050 static coroutine_fn
int nbd_co_receive_structured_payload(
1051 BDRVNBDState
*s
, void **payload
, Error
**errp
)
1056 assert(nbd_reply_is_structured(&s
->reply
));
1058 len
= s
->reply
.structured
.length
;
1064 if (payload
== NULL
) {
1065 error_setg(errp
, "Unexpected structured payload");
1069 if (len
> NBD_MAX_MALLOC_PAYLOAD
) {
1070 error_setg(errp
, "Payload too large");
1074 *payload
= g_new(char, len
);
1075 ret
= nbd_read(s
->ioc
, *payload
, len
, "structured payload", errp
);
1086 * nbd_co_do_receive_one_chunk
1088 * set request_ret to received reply error
1089 * if qiov is not NULL: read payload to @qiov
1090 * for structured reply chunk:
1091 * if error chunk: read payload, set @request_ret, do not set @payload
1092 * else if offset_data chunk: read payload data to @qiov, do not set @payload
1093 * else: read payload to @payload
1095 * If function fails, @errp contains corresponding error message, and the
1096 * connection with the server is suspect. If it returns 0, then the
1097 * transaction succeeded (although @request_ret may be a negative errno
1098 * corresponding to the server's error reply), and errp is unchanged.
1100 static coroutine_fn
int nbd_co_do_receive_one_chunk(
1101 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
1102 int *request_ret
, QEMUIOVector
*qiov
, void **payload
, Error
**errp
)
1105 int i
= HANDLE_TO_INDEX(s
, handle
);
1106 void *local_payload
= NULL
;
1107 NBDStructuredReplyChunk
*chunk
;
1114 /* Wait until we're woken up by nbd_connection_entry. */
1115 s
->requests
[i
].receiving
= true;
1116 qemu_coroutine_yield();
1117 s
->requests
[i
].receiving
= false;
1118 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
1119 error_setg(errp
, "Connection closed");
1124 assert(s
->reply
.handle
== handle
);
1126 if (nbd_reply_is_simple(&s
->reply
)) {
1127 if (only_structured
) {
1128 error_setg(errp
, "Protocol error: simple reply when structured "
1129 "reply chunk was expected");
1133 *request_ret
= -nbd_errno_to_system_errno(s
->reply
.simple
.error
);
1134 if (*request_ret
< 0 || !qiov
) {
1138 return qio_channel_readv_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
1139 errp
) < 0 ? -EIO
: 0;
1142 /* handle structured reply chunk */
1143 assert(s
->info
.structured_reply
);
1144 chunk
= &s
->reply
.structured
;
1146 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
1147 if (!(chunk
->flags
& NBD_REPLY_FLAG_DONE
)) {
1148 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
1149 " NBD_REPLY_FLAG_DONE flag set");
1152 if (chunk
->length
) {
1153 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
1160 if (chunk
->type
== NBD_REPLY_TYPE_OFFSET_DATA
) {
1162 error_setg(errp
, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
1166 return nbd_co_receive_offset_data_payload(s
, s
->requests
[i
].offset
,
1170 if (nbd_reply_type_is_error(chunk
->type
)) {
1171 payload
= &local_payload
;
1174 ret
= nbd_co_receive_structured_payload(s
, payload
, errp
);
1179 if (nbd_reply_type_is_error(chunk
->type
)) {
1180 ret
= nbd_parse_error_payload(chunk
, local_payload
, request_ret
, errp
);
1181 g_free(local_payload
);
1189 * nbd_co_receive_one_chunk
1190 * Read reply, wake up connection_co and set s->quit if needed.
1191 * Return value is a fatal error code or normal nbd reply error code
1193 static coroutine_fn
int nbd_co_receive_one_chunk(
1194 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
1195 int *request_ret
, QEMUIOVector
*qiov
, NBDReply
*reply
, void **payload
,
1198 int ret
= nbd_co_do_receive_one_chunk(s
, handle
, only_structured
,
1199 request_ret
, qiov
, payload
, errp
);
1202 memset(reply
, 0, sizeof(*reply
));
1203 nbd_channel_error(s
, ret
);
1205 /* For assert at loop start in nbd_connection_entry */
1208 s
->reply
.handle
= 0;
1210 if (s
->connection_co
&& !s
->wait_in_flight
) {
1212 * We must check s->wait_in_flight, because we may entered by
1213 * nbd_recv_coroutines_wake_all(), in this case we should not
1214 * wake connection_co here, it will woken by last request.
1216 aio_co_wake(s
->connection_co
);
1222 typedef struct NBDReplyChunkIter
{
1226 bool done
, only_structured
;
1227 } NBDReplyChunkIter
;
1229 static void nbd_iter_channel_error(NBDReplyChunkIter
*iter
,
1230 int ret
, Error
**local_err
)
1232 assert(local_err
&& *local_err
);
1237 error_propagate(&iter
->err
, *local_err
);
1239 error_free(*local_err
);
1245 static void nbd_iter_request_error(NBDReplyChunkIter
*iter
, int ret
)
1249 if (!iter
->request_ret
) {
1250 iter
->request_ret
= ret
;
1255 * NBD_FOREACH_REPLY_CHUNK
1256 * The pointer stored in @payload requires g_free() to free it.
1258 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
1259 qiov, reply, payload) \
1260 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
1261 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
1264 * nbd_reply_chunk_iter_receive
1265 * The pointer stored in @payload requires g_free() to free it.
1267 static bool nbd_reply_chunk_iter_receive(BDRVNBDState
*s
,
1268 NBDReplyChunkIter
*iter
,
1270 QEMUIOVector
*qiov
, NBDReply
*reply
,
1273 int ret
, request_ret
;
1274 NBDReply local_reply
;
1275 NBDStructuredReplyChunk
*chunk
;
1276 Error
*local_err
= NULL
;
1277 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
1278 error_setg(&local_err
, "Connection closed");
1279 nbd_iter_channel_error(iter
, -EIO
, &local_err
);
1284 /* Previous iteration was last. */
1288 if (reply
== NULL
) {
1289 reply
= &local_reply
;
1292 ret
= nbd_co_receive_one_chunk(s
, handle
, iter
->only_structured
,
1293 &request_ret
, qiov
, reply
, payload
,
1296 nbd_iter_channel_error(iter
, ret
, &local_err
);
1297 } else if (request_ret
< 0) {
1298 nbd_iter_request_error(iter
, request_ret
);
1301 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
1302 if (nbd_reply_is_simple(reply
) || s
->state
!= NBD_CLIENT_CONNECTED
) {
1306 chunk
= &reply
->structured
;
1307 iter
->only_structured
= true;
1309 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
1310 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1311 assert(chunk
->flags
& NBD_REPLY_FLAG_DONE
);
1315 if (chunk
->flags
& NBD_REPLY_FLAG_DONE
) {
1316 /* This iteration is last. */
1320 /* Execute the loop body */
1324 s
->requests
[HANDLE_TO_INDEX(s
, handle
)].coroutine
= NULL
;
1326 qemu_co_mutex_lock(&s
->send_mutex
);
1328 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
1329 aio_co_wake(s
->connection_co
);
1331 qemu_co_queue_next(&s
->free_sema
);
1333 qemu_co_mutex_unlock(&s
->send_mutex
);
1338 static int nbd_co_receive_return_code(BDRVNBDState
*s
, uint64_t handle
,
1339 int *request_ret
, Error
**errp
)
1341 NBDReplyChunkIter iter
;
1343 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, NULL
, NULL
) {
1344 /* nbd_reply_chunk_iter_receive does all the work */
1347 error_propagate(errp
, iter
.err
);
1348 *request_ret
= iter
.request_ret
;
1352 static int nbd_co_receive_cmdread_reply(BDRVNBDState
*s
, uint64_t handle
,
1353 uint64_t offset
, QEMUIOVector
*qiov
,
1354 int *request_ret
, Error
**errp
)
1356 NBDReplyChunkIter iter
;
1358 void *payload
= NULL
;
1359 Error
*local_err
= NULL
;
1361 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, s
->info
.structured_reply
,
1362 qiov
, &reply
, &payload
)
1365 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1367 assert(nbd_reply_is_structured(&reply
));
1369 switch (chunk
->type
) {
1370 case NBD_REPLY_TYPE_OFFSET_DATA
:
1372 * special cased in nbd_co_receive_one_chunk, data is already
1376 case NBD_REPLY_TYPE_OFFSET_HOLE
:
1377 ret
= nbd_parse_offset_hole_payload(s
, &reply
.structured
, payload
,
1378 offset
, qiov
, &local_err
);
1380 nbd_channel_error(s
, ret
);
1381 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1385 if (!nbd_reply_type_is_error(chunk
->type
)) {
1386 /* not allowed reply type */
1387 nbd_channel_error(s
, -EINVAL
);
1388 error_setg(&local_err
,
1389 "Unexpected reply type: %d (%s) for CMD_READ",
1390 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1391 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1399 error_propagate(errp
, iter
.err
);
1400 *request_ret
= iter
.request_ret
;
1404 static int nbd_co_receive_blockstatus_reply(BDRVNBDState
*s
,
1405 uint64_t handle
, uint64_t length
,
1407 int *request_ret
, Error
**errp
)
1409 NBDReplyChunkIter iter
;
1411 void *payload
= NULL
;
1412 Error
*local_err
= NULL
;
1413 bool received
= false;
1415 assert(!extent
->length
);
1416 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, &reply
, &payload
) {
1418 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1420 assert(nbd_reply_is_structured(&reply
));
1422 switch (chunk
->type
) {
1423 case NBD_REPLY_TYPE_BLOCK_STATUS
:
1425 nbd_channel_error(s
, -EINVAL
);
1426 error_setg(&local_err
, "Several BLOCK_STATUS chunks in reply");
1427 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1431 ret
= nbd_parse_blockstatus_payload(s
, &reply
.structured
,
1432 payload
, length
, extent
,
1435 nbd_channel_error(s
, ret
);
1436 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1440 if (!nbd_reply_type_is_error(chunk
->type
)) {
1441 nbd_channel_error(s
, -EINVAL
);
1442 error_setg(&local_err
,
1443 "Unexpected reply type: %d (%s) "
1444 "for CMD_BLOCK_STATUS",
1445 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1446 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1454 if (!extent
->length
&& !iter
.request_ret
) {
1455 error_setg(&local_err
, "Server did not reply with any status extents");
1456 nbd_iter_channel_error(&iter
, -EIO
, &local_err
);
1459 error_propagate(errp
, iter
.err
);
1460 *request_ret
= iter
.request_ret
;
1464 static int nbd_co_request(BlockDriverState
*bs
, NBDRequest
*request
,
1465 QEMUIOVector
*write_qiov
)
1467 int ret
, request_ret
;
1468 Error
*local_err
= NULL
;
1469 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1471 assert(request
->type
!= NBD_CMD_READ
);
1473 assert(request
->type
== NBD_CMD_WRITE
);
1474 assert(request
->len
== iov_size(write_qiov
->iov
, write_qiov
->niov
));
1476 assert(request
->type
!= NBD_CMD_WRITE
);
1480 ret
= nbd_co_send_request(bs
, request
, write_qiov
);
1485 ret
= nbd_co_receive_return_code(s
, request
->handle
,
1486 &request_ret
, &local_err
);
1488 trace_nbd_co_request_fail(request
->from
, request
->len
,
1489 request
->handle
, request
->flags
,
1491 nbd_cmd_lookup(request
->type
),
1492 ret
, error_get_pretty(local_err
));
1493 error_free(local_err
);
1496 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1498 return ret
? ret
: request_ret
;
1501 static int nbd_client_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
1502 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1504 int ret
, request_ret
;
1505 Error
*local_err
= NULL
;
1506 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1507 NBDRequest request
= {
1508 .type
= NBD_CMD_READ
,
1513 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1520 * Work around the fact that the block layer doesn't do
1521 * byte-accurate sizing yet - if the read exceeds the server's
1522 * advertised size because the block layer rounded size up, then
1523 * truncate the request to the server and tail-pad with zero.
1525 if (offset
>= s
->info
.size
) {
1526 assert(bytes
< BDRV_SECTOR_SIZE
);
1527 qemu_iovec_memset(qiov
, 0, 0, bytes
);
1530 if (offset
+ bytes
> s
->info
.size
) {
1531 uint64_t slop
= offset
+ bytes
- s
->info
.size
;
1533 assert(slop
< BDRV_SECTOR_SIZE
);
1534 qemu_iovec_memset(qiov
, bytes
- slop
, 0, slop
);
1535 request
.len
-= slop
;
1539 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1544 ret
= nbd_co_receive_cmdread_reply(s
, request
.handle
, offset
, qiov
,
1545 &request_ret
, &local_err
);
1547 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1548 request
.flags
, request
.type
,
1549 nbd_cmd_lookup(request
.type
),
1550 ret
, error_get_pretty(local_err
));
1551 error_free(local_err
);
1554 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1556 return ret
? ret
: request_ret
;
1559 static int nbd_client_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1560 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1562 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1563 NBDRequest request
= {
1564 .type
= NBD_CMD_WRITE
,
1569 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1570 if (flags
& BDRV_REQ_FUA
) {
1571 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1572 request
.flags
|= NBD_CMD_FLAG_FUA
;
1575 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1580 return nbd_co_request(bs
, &request
, qiov
);
1583 static int nbd_client_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1584 int bytes
, BdrvRequestFlags flags
)
1586 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1587 NBDRequest request
= {
1588 .type
= NBD_CMD_WRITE_ZEROES
,
1593 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1594 if (!(s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
)) {
1598 if (flags
& BDRV_REQ_FUA
) {
1599 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1600 request
.flags
|= NBD_CMD_FLAG_FUA
;
1602 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1603 request
.flags
|= NBD_CMD_FLAG_NO_HOLE
;
1605 if (flags
& BDRV_REQ_NO_FALLBACK
) {
1606 assert(s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
);
1607 request
.flags
|= NBD_CMD_FLAG_FAST_ZERO
;
1613 return nbd_co_request(bs
, &request
, NULL
);
1616 static int nbd_client_co_flush(BlockDriverState
*bs
)
1618 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1619 NBDRequest request
= { .type
= NBD_CMD_FLUSH
};
1621 if (!(s
->info
.flags
& NBD_FLAG_SEND_FLUSH
)) {
1628 return nbd_co_request(bs
, &request
, NULL
);
1631 static int nbd_client_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
1634 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1635 NBDRequest request
= {
1636 .type
= NBD_CMD_TRIM
,
1641 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1642 if (!(s
->info
.flags
& NBD_FLAG_SEND_TRIM
) || !bytes
) {
1646 return nbd_co_request(bs
, &request
, NULL
);
1649 static int coroutine_fn
nbd_client_co_block_status(
1650 BlockDriverState
*bs
, bool want_zero
, int64_t offset
, int64_t bytes
,
1651 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
1653 int ret
, request_ret
;
1654 NBDExtent extent
= { 0 };
1655 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1656 Error
*local_err
= NULL
;
1658 NBDRequest request
= {
1659 .type
= NBD_CMD_BLOCK_STATUS
,
1661 .len
= MIN(QEMU_ALIGN_DOWN(INT_MAX
, bs
->bl
.request_alignment
),
1662 MIN(bytes
, s
->info
.size
- offset
)),
1663 .flags
= NBD_CMD_FLAG_REQ_ONE
,
1666 if (!s
->info
.base_allocation
) {
1670 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1674 * Work around the fact that the block layer doesn't do
1675 * byte-accurate sizing yet - if the status request exceeds the
1676 * server's advertised size because the block layer rounded size
1677 * up, we truncated the request to the server (above), or are
1678 * called on just the hole.
1680 if (offset
>= s
->info
.size
) {
1682 assert(bytes
< BDRV_SECTOR_SIZE
);
1683 /* Intentionally don't report offset_valid for the hole */
1684 return BDRV_BLOCK_ZERO
;
1687 if (s
->info
.min_block
) {
1688 assert(QEMU_IS_ALIGNED(request
.len
, s
->info
.min_block
));
1691 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1696 ret
= nbd_co_receive_blockstatus_reply(s
, request
.handle
, bytes
,
1697 &extent
, &request_ret
,
1700 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1701 request
.flags
, request
.type
,
1702 nbd_cmd_lookup(request
.type
),
1703 ret
, error_get_pretty(local_err
));
1704 error_free(local_err
);
1707 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1709 if (ret
< 0 || request_ret
< 0) {
1710 return ret
? ret
: request_ret
;
1713 assert(extent
.length
);
1714 *pnum
= extent
.length
;
1717 return (extent
.flags
& NBD_STATE_HOLE
? 0 : BDRV_BLOCK_DATA
) |
1718 (extent
.flags
& NBD_STATE_ZERO
? BDRV_BLOCK_ZERO
: 0) |
1719 BDRV_BLOCK_OFFSET_VALID
;
1722 static int nbd_client_reopen_prepare(BDRVReopenState
*state
,
1723 BlockReopenQueue
*queue
, Error
**errp
)
1725 BDRVNBDState
*s
= (BDRVNBDState
*)state
->bs
->opaque
;
1727 if ((state
->flags
& BDRV_O_RDWR
) && (s
->info
.flags
& NBD_FLAG_READ_ONLY
)) {
1728 error_setg(errp
, "Can't reopen read-only NBD mount as read/write");
1734 static void nbd_client_close(BlockDriverState
*bs
)
1736 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1737 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1740 nbd_send_request(s
->ioc
, &request
);
1743 nbd_teardown_connection(bs
);
1746 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
1750 QIOChannelSocket
*sioc
;
1752 sioc
= qio_channel_socket_new();
1753 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
1755 qio_channel_socket_connect_sync(sioc
, saddr
, errp
);
1757 object_unref(OBJECT(sioc
));
1761 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
1766 /* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
1767 static int nbd_client_handshake(BlockDriverState
*bs
, QIOChannelSocket
*sioc
,
1770 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1771 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1774 trace_nbd_client_handshake(s
->export
);
1778 qio_channel_set_blocking(QIO_CHANNEL(sioc
), false, NULL
);
1779 qio_channel_attach_aio_context(QIO_CHANNEL(sioc
), aio_context
);
1781 s
->info
.request_sizes
= true;
1782 s
->info
.structured_reply
= true;
1783 s
->info
.base_allocation
= true;
1784 s
->info
.x_dirty_bitmap
= g_strdup(s
->x_dirty_bitmap
);
1785 s
->info
.name
= g_strdup(s
->export
?: "");
1786 ret
= nbd_receive_negotiate(aio_context
, QIO_CHANNEL(sioc
), s
->tlscreds
,
1787 s
->hostname
, &s
->ioc
, &s
->info
, errp
);
1788 g_free(s
->info
.x_dirty_bitmap
);
1789 g_free(s
->info
.name
);
1791 object_unref(OBJECT(sioc
));
1795 if (s
->x_dirty_bitmap
&& !s
->info
.base_allocation
) {
1796 error_setg(errp
, "requested x-dirty-bitmap %s not found",
1801 if (s
->info
.flags
& NBD_FLAG_READ_ONLY
) {
1802 ret
= bdrv_apply_auto_read_only(bs
, "NBD export is read-only", errp
);
1807 if (s
->info
.flags
& NBD_FLAG_SEND_FUA
) {
1808 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1809 bs
->supported_zero_flags
|= BDRV_REQ_FUA
;
1811 if (s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
) {
1812 bs
->supported_zero_flags
|= BDRV_REQ_MAY_UNMAP
;
1813 if (s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
) {
1814 bs
->supported_zero_flags
|= BDRV_REQ_NO_FALLBACK
;
1819 s
->ioc
= QIO_CHANNEL(sioc
);
1820 object_ref(OBJECT(s
->ioc
));
1823 trace_nbd_client_handshake_success(s
->export
);
1829 * We have connected, but must fail for other reasons.
1830 * Send NBD_CMD_DISC as a courtesy to the server.
1833 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1835 nbd_send_request(s
->ioc
?: QIO_CHANNEL(sioc
), &request
);
1837 object_unref(OBJECT(sioc
));
1845 * Parse nbd_open options
1848 static int nbd_parse_uri(const char *filename
, QDict
*options
)
1852 QueryParams
*qp
= NULL
;
1856 uri
= uri_parse(filename
);
1862 if (!g_strcmp0(uri
->scheme
, "nbd")) {
1864 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
1866 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
1873 p
= uri
->path
? uri
->path
: "";
1878 qdict_put_str(options
, "export", p
);
1881 qp
= query_params_parse(uri
->query
);
1882 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
1888 /* nbd+unix:///export?socket=path */
1889 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
1893 qdict_put_str(options
, "server.type", "unix");
1894 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
1899 /* nbd[+tcp]://host[:port]/export */
1905 /* strip braces from literal IPv6 address */
1906 if (uri
->server
[0] == '[') {
1907 host
= qstring_from_substr(uri
->server
, 1,
1908 strlen(uri
->server
) - 1);
1910 host
= qstring_from_str(uri
->server
);
1913 qdict_put_str(options
, "server.type", "inet");
1914 qdict_put(options
, "server.host", host
);
1916 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
1917 qdict_put_str(options
, "server.port", port_str
);
1923 query_params_free(qp
);
1929 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
1931 const QDictEntry
*e
;
1933 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
1934 if (!strcmp(e
->key
, "host") ||
1935 !strcmp(e
->key
, "port") ||
1936 !strcmp(e
->key
, "path") ||
1937 !strcmp(e
->key
, "export") ||
1938 strstart(e
->key
, "server.", NULL
))
1940 error_setg(errp
, "Option '%s' cannot be used with a file name",
1949 static void nbd_parse_filename(const char *filename
, QDict
*options
,
1952 g_autofree
char *file
= NULL
;
1954 const char *host_spec
;
1955 const char *unixpath
;
1957 if (nbd_has_filename_options_conflict(options
, errp
)) {
1961 if (strstr(filename
, "://")) {
1962 int ret
= nbd_parse_uri(filename
, options
);
1964 error_setg(errp
, "No valid URL specified");
1969 file
= g_strdup(filename
);
1971 export_name
= strstr(file
, EN_OPTSTR
);
1973 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
1976 export_name
[0] = 0; /* truncate 'file' */
1977 export_name
+= strlen(EN_OPTSTR
);
1979 qdict_put_str(options
, "export", export_name
);
1982 /* extract the host_spec - fail if it's not nbd:... */
1983 if (!strstart(file
, "nbd:", &host_spec
)) {
1984 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
1992 /* are we a UNIX or TCP socket? */
1993 if (strstart(host_spec
, "unix:", &unixpath
)) {
1994 qdict_put_str(options
, "server.type", "unix");
1995 qdict_put_str(options
, "server.path", unixpath
);
1997 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
1999 if (inet_parse(addr
, host_spec
, errp
)) {
2003 qdict_put_str(options
, "server.type", "inet");
2004 qdict_put_str(options
, "server.host", addr
->host
);
2005 qdict_put_str(options
, "server.port", addr
->port
);
2007 qapi_free_InetSocketAddress(addr
);
2011 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
2012 QemuOpts
*legacy_opts
,
2015 const char *path
= qemu_opt_get(legacy_opts
, "path");
2016 const char *host
= qemu_opt_get(legacy_opts
, "host");
2017 const char *port
= qemu_opt_get(legacy_opts
, "port");
2018 const QDictEntry
*e
;
2020 if (!path
&& !host
&& !port
) {
2024 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
2026 if (strstart(e
->key
, "server.", NULL
)) {
2027 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
2034 error_setg(errp
, "path and host may not be used at the same time");
2038 error_setg(errp
, "port may not be used without host");
2042 qdict_put_str(output_options
, "server.type", "unix");
2043 qdict_put_str(output_options
, "server.path", path
);
2045 qdict_put_str(output_options
, "server.type", "inet");
2046 qdict_put_str(output_options
, "server.host", host
);
2047 qdict_put_str(output_options
, "server.port",
2048 port
?: stringify(NBD_DEFAULT_PORT
));
2054 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
2057 SocketAddress
*saddr
= NULL
;
2061 qdict_extract_subqdict(options
, &addr
, "server.");
2062 if (!qdict_size(addr
)) {
2063 error_setg(errp
, "NBD server address missing");
2067 iv
= qobject_input_visitor_new_flat_confused(addr
, errp
);
2072 if (!visit_type_SocketAddress(iv
, NULL
, &saddr
, errp
)) {
2077 qobject_unref(addr
);
2082 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
2085 QCryptoTLSCreds
*creds
;
2087 obj
= object_resolve_path_component(
2088 object_get_objects_root(), id
);
2090 error_setg(errp
, "No TLS credentials with id '%s'",
2094 creds
= (QCryptoTLSCreds
*)
2095 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
2097 error_setg(errp
, "Object with id '%s' is not TLS credentials",
2102 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
2104 "Expecting TLS credentials with a client endpoint");
2112 static QemuOptsList nbd_runtime_opts
= {
2114 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
2118 .type
= QEMU_OPT_STRING
,
2119 .help
= "TCP host to connect to",
2123 .type
= QEMU_OPT_STRING
,
2124 .help
= "TCP port to connect to",
2128 .type
= QEMU_OPT_STRING
,
2129 .help
= "Unix socket path to connect to",
2133 .type
= QEMU_OPT_STRING
,
2134 .help
= "Name of the NBD export to open",
2137 .name
= "tls-creds",
2138 .type
= QEMU_OPT_STRING
,
2139 .help
= "ID of the TLS credentials to use",
2142 .name
= "x-dirty-bitmap",
2143 .type
= QEMU_OPT_STRING
,
2144 .help
= "experimental: expose named dirty bitmap in place of "
2148 .name
= "reconnect-delay",
2149 .type
= QEMU_OPT_NUMBER
,
2150 .help
= "On an unexpected disconnect, the nbd client tries to "
2151 "connect again until succeeding or encountering a serious "
2152 "error. During the first @reconnect-delay seconds, all "
2153 "requests are paused and will be rerun on a successful "
2154 "reconnect. After that time, any delayed requests and all "
2155 "future requests before a successful reconnect will "
2156 "immediately fail. Default 0",
2158 { /* end of list */ }
2162 static int nbd_process_options(BlockDriverState
*bs
, QDict
*options
,
2165 BDRVNBDState
*s
= bs
->opaque
;
2169 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
2170 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
2174 /* Translate @host, @port, and @path to a SocketAddress */
2175 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
2179 /* Pop the config into our state object. Exit if invalid. */
2180 s
->saddr
= nbd_config(s
, options
, errp
);
2185 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
2186 if (s
->export
&& strlen(s
->export
) > NBD_MAX_STRING_SIZE
) {
2187 error_setg(errp
, "export name too long to send to server");
2191 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
2192 if (s
->tlscredsid
) {
2193 s
->tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
2198 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
2199 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
2200 error_setg(errp
, "TLS only supported over IP sockets");
2203 s
->hostname
= s
->saddr
->u
.inet
.host
;
2206 s
->x_dirty_bitmap
= g_strdup(qemu_opt_get(opts
, "x-dirty-bitmap"));
2207 if (s
->x_dirty_bitmap
&& strlen(s
->x_dirty_bitmap
) > NBD_MAX_STRING_SIZE
) {
2208 error_setg(errp
, "x-dirty-bitmap query too long to send to server");
2212 s
->reconnect_delay
= qemu_opt_get_number(opts
, "reconnect-delay", 0);
2218 nbd_clear_bdrvstate(s
);
2220 qemu_opts_del(opts
);
2224 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2228 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
2229 QIOChannelSocket
*sioc
;
2231 ret
= nbd_process_options(bs
, options
, errp
);
2237 qemu_co_mutex_init(&s
->send_mutex
);
2238 qemu_co_queue_init(&s
->free_sema
);
2241 * establish TCP connection, return error if it fails
2242 * TODO: Configurable retry-until-timeout behaviour.
2244 sioc
= nbd_establish_connection(s
->saddr
, errp
);
2246 return -ECONNREFUSED
;
2249 ret
= nbd_client_handshake(bs
, sioc
, errp
);
2251 nbd_clear_bdrvstate(s
);
2254 /* successfully connected */
2255 s
->state
= NBD_CLIENT_CONNECTED
;
2257 nbd_init_connect_thread(s
);
2259 s
->connection_co
= qemu_coroutine_create(nbd_connection_entry
, s
);
2260 bdrv_inc_in_flight(bs
);
2261 aio_co_schedule(bdrv_get_aio_context(bs
), s
->connection_co
);
2266 static int nbd_co_flush(BlockDriverState
*bs
)
2268 return nbd_client_co_flush(bs
);
2271 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
2273 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
2274 uint32_t min
= s
->info
.min_block
;
2275 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
2278 * If the server did not advertise an alignment:
2279 * - a size that is not sector-aligned implies that an alignment
2280 * of 1 can be used to access those tail bytes
2281 * - advertisement of block status requires an alignment of 1, so
2282 * that we don't violate block layer constraints that block
2283 * status is always aligned (as we can't control whether the
2284 * server will report sub-sector extents, such as a hole at EOF
2285 * on an unaligned POSIX file)
2286 * - otherwise, assume the server is so old that we are safer avoiding
2287 * sub-sector requests
2290 min
= (!QEMU_IS_ALIGNED(s
->info
.size
, BDRV_SECTOR_SIZE
) ||
2291 s
->info
.base_allocation
) ? 1 : BDRV_SECTOR_SIZE
;
2294 bs
->bl
.request_alignment
= min
;
2295 bs
->bl
.max_pdiscard
= QEMU_ALIGN_DOWN(INT_MAX
, min
);
2296 bs
->bl
.max_pwrite_zeroes
= max
;
2297 bs
->bl
.max_transfer
= max
;
2299 if (s
->info
.opt_block
&&
2300 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
2301 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
2305 static void nbd_close(BlockDriverState
*bs
)
2307 BDRVNBDState
*s
= bs
->opaque
;
2309 nbd_client_close(bs
);
2310 nbd_clear_bdrvstate(s
);
2314 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2315 * to a smaller size with exact=false, there is no reason to fail the
2318 * Preallocation mode is ignored since it does not seems useful to fail when
2319 * we never change anything.
2321 static int coroutine_fn
nbd_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2322 bool exact
, PreallocMode prealloc
,
2323 BdrvRequestFlags flags
, Error
**errp
)
2325 BDRVNBDState
*s
= bs
->opaque
;
2327 if (offset
!= s
->info
.size
&& exact
) {
2328 error_setg(errp
, "Cannot resize NBD nodes");
2332 if (offset
> s
->info
.size
) {
2333 error_setg(errp
, "Cannot grow NBD nodes");
2340 static int64_t nbd_getlength(BlockDriverState
*bs
)
2342 BDRVNBDState
*s
= bs
->opaque
;
2344 return s
->info
.size
;
2347 static void nbd_refresh_filename(BlockDriverState
*bs
)
2349 BDRVNBDState
*s
= bs
->opaque
;
2350 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
2353 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
2354 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
2355 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
2359 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
2360 path
= s
->saddr
->u
.q_unix
.path
;
2361 } /* else can't represent as pseudo-filename */
2363 if (path
&& s
->export
) {
2364 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2365 "nbd+unix:///%s?socket=%s", s
->export
, path
);
2366 } else if (path
&& !s
->export
) {
2367 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2368 "nbd+unix://?socket=%s", path
);
2369 } else if (host
&& s
->export
) {
2370 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2371 "nbd://%s:%s/%s", host
, port
, s
->export
);
2372 } else if (host
&& !s
->export
) {
2373 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2374 "nbd://%s:%s", host
, port
);
2376 if (len
>= sizeof(bs
->exact_filename
)) {
2377 /* Name is too long to represent exactly, so leave it empty. */
2378 bs
->exact_filename
[0] = '\0';
2382 static char *nbd_dirname(BlockDriverState
*bs
, Error
**errp
)
2384 /* The generic bdrv_dirname() implementation is able to work out some
2385 * directory name for NBD nodes, but that would be wrong. So far there is no
2386 * specification for how "export paths" would work, so NBD does not have
2387 * directory names. */
2388 error_setg(errp
, "Cannot generate a base directory for NBD nodes");
2392 static const char *const nbd_strong_runtime_opts
[] = {
2403 static BlockDriver bdrv_nbd
= {
2404 .format_name
= "nbd",
2405 .protocol_name
= "nbd",
2406 .instance_size
= sizeof(BDRVNBDState
),
2407 .bdrv_parse_filename
= nbd_parse_filename
,
2408 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2409 .create_opts
= &bdrv_create_opts_simple
,
2410 .bdrv_file_open
= nbd_open
,
2411 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2412 .bdrv_co_preadv
= nbd_client_co_preadv
,
2413 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2414 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2415 .bdrv_close
= nbd_close
,
2416 .bdrv_co_flush_to_os
= nbd_co_flush
,
2417 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2418 .bdrv_refresh_limits
= nbd_refresh_limits
,
2419 .bdrv_co_truncate
= nbd_co_truncate
,
2420 .bdrv_getlength
= nbd_getlength
,
2421 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2422 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2423 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2424 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2425 .bdrv_refresh_filename
= nbd_refresh_filename
,
2426 .bdrv_co_block_status
= nbd_client_co_block_status
,
2427 .bdrv_dirname
= nbd_dirname
,
2428 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2431 static BlockDriver bdrv_nbd_tcp
= {
2432 .format_name
= "nbd",
2433 .protocol_name
= "nbd+tcp",
2434 .instance_size
= sizeof(BDRVNBDState
),
2435 .bdrv_parse_filename
= nbd_parse_filename
,
2436 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2437 .create_opts
= &bdrv_create_opts_simple
,
2438 .bdrv_file_open
= nbd_open
,
2439 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2440 .bdrv_co_preadv
= nbd_client_co_preadv
,
2441 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2442 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2443 .bdrv_close
= nbd_close
,
2444 .bdrv_co_flush_to_os
= nbd_co_flush
,
2445 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2446 .bdrv_refresh_limits
= nbd_refresh_limits
,
2447 .bdrv_co_truncate
= nbd_co_truncate
,
2448 .bdrv_getlength
= nbd_getlength
,
2449 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2450 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2451 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2452 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2453 .bdrv_refresh_filename
= nbd_refresh_filename
,
2454 .bdrv_co_block_status
= nbd_client_co_block_status
,
2455 .bdrv_dirname
= nbd_dirname
,
2456 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2459 static BlockDriver bdrv_nbd_unix
= {
2460 .format_name
= "nbd",
2461 .protocol_name
= "nbd+unix",
2462 .instance_size
= sizeof(BDRVNBDState
),
2463 .bdrv_parse_filename
= nbd_parse_filename
,
2464 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2465 .create_opts
= &bdrv_create_opts_simple
,
2466 .bdrv_file_open
= nbd_open
,
2467 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2468 .bdrv_co_preadv
= nbd_client_co_preadv
,
2469 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2470 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2471 .bdrv_close
= nbd_close
,
2472 .bdrv_co_flush_to_os
= nbd_co_flush
,
2473 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2474 .bdrv_refresh_limits
= nbd_refresh_limits
,
2475 .bdrv_co_truncate
= nbd_co_truncate
,
2476 .bdrv_getlength
= nbd_getlength
,
2477 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2478 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2479 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2480 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2481 .bdrv_refresh_filename
= nbd_refresh_filename
,
2482 .bdrv_co_block_status
= nbd_client_co_block_status
,
2483 .bdrv_dirname
= nbd_dirname
,
2484 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2487 static void bdrv_nbd_init(void)
2489 bdrv_register(&bdrv_nbd
);
2490 bdrv_register(&bdrv_nbd_tcp
);
2491 bdrv_register(&bdrv_nbd_unix
);
2494 block_init(bdrv_nbd_init
);