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"
38 #include "qemu/atomic.h"
40 #include "qapi/qapi-visit-sockets.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qapi/clone-visitor.h"
44 #include "block/qdict.h"
45 #include "block/nbd.h"
46 #include "block/block_int.h"
48 #include "qemu/yank.h"
50 #define EN_OPTSTR ":exportname="
51 #define MAX_NBD_REQUESTS 16
53 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
54 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
58 uint64_t offset
; /* original offset of the request */
59 bool receiving
; /* waiting for connection_co? */
62 typedef enum NBDClientState
{
63 NBD_CLIENT_CONNECTING_WAIT
,
64 NBD_CLIENT_CONNECTING_NOWAIT
,
69 typedef struct BDRVNBDState
{
70 QIOChannel
*ioc
; /* The current I/O channel */
75 Coroutine
*connection_co
;
76 Coroutine
*teardown_co
;
77 QemuCoSleep reconnect_sleep
;
79 bool wait_drained_end
;
84 QEMUTimer
*reconnect_delay_timer
;
86 NBDClientRequest requests
[MAX_NBD_REQUESTS
];
90 /* Connection parameters */
91 uint32_t reconnect_delay
;
93 char *export
, *tlscredsid
;
94 QCryptoTLSCreds
*tlscreds
;
99 NBDClientConnection
*conn
;
102 static QIOChannelSocket
*nbd_establish_connection(BlockDriverState
*bs
,
103 SocketAddress
*saddr
,
105 static int nbd_client_handshake(BlockDriverState
*bs
, QIOChannelSocket
*sioc
,
107 static void nbd_yank(void *opaque
);
109 static void nbd_clear_bdrvstate(BlockDriverState
*bs
)
111 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
113 nbd_client_connection_release(s
->conn
);
116 yank_unregister_instance(BLOCKDEV_YANK_INSTANCE(bs
->node_name
));
118 object_unref(OBJECT(s
->tlscreds
));
119 qapi_free_SocketAddress(s
->saddr
);
123 g_free(s
->tlscredsid
);
124 s
->tlscredsid
= NULL
;
125 g_free(s
->x_dirty_bitmap
);
126 s
->x_dirty_bitmap
= NULL
;
129 static void nbd_channel_error(BDRVNBDState
*s
, int ret
)
132 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTED
) {
133 s
->state
= s
->reconnect_delay
? NBD_CLIENT_CONNECTING_WAIT
:
134 NBD_CLIENT_CONNECTING_NOWAIT
;
137 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTED
) {
138 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
140 s
->state
= NBD_CLIENT_QUIT
;
144 static void nbd_recv_coroutines_wake_all(BDRVNBDState
*s
)
148 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
149 NBDClientRequest
*req
= &s
->requests
[i
];
151 if (req
->coroutine
&& req
->receiving
) {
152 aio_co_wake(req
->coroutine
);
157 static void reconnect_delay_timer_del(BDRVNBDState
*s
)
159 if (s
->reconnect_delay_timer
) {
160 timer_free(s
->reconnect_delay_timer
);
161 s
->reconnect_delay_timer
= NULL
;
165 static void reconnect_delay_timer_cb(void *opaque
)
167 BDRVNBDState
*s
= opaque
;
169 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTING_WAIT
) {
170 s
->state
= NBD_CLIENT_CONNECTING_NOWAIT
;
171 while (qemu_co_enter_next(&s
->free_sema
, NULL
)) {
172 /* Resume all queued requests */
176 reconnect_delay_timer_del(s
);
179 static void reconnect_delay_timer_init(BDRVNBDState
*s
, uint64_t expire_time_ns
)
181 if (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTING_WAIT
) {
185 assert(!s
->reconnect_delay_timer
);
186 s
->reconnect_delay_timer
= aio_timer_new(bdrv_get_aio_context(s
->bs
),
189 reconnect_delay_timer_cb
, s
);
190 timer_mod(s
->reconnect_delay_timer
, expire_time_ns
);
193 static void nbd_client_detach_aio_context(BlockDriverState
*bs
)
195 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
197 /* Timer is deleted in nbd_client_co_drain_begin() */
198 assert(!s
->reconnect_delay_timer
);
200 * If reconnect is in progress we may have no ->ioc. It will be
201 * re-instantiated in the proper aio context once the connection is
205 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
209 static void nbd_client_attach_aio_context_bh(void *opaque
)
211 BlockDriverState
*bs
= opaque
;
212 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
214 if (s
->connection_co
) {
216 * The node is still drained, so we know the coroutine has yielded in
217 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or
218 * it is entered for the first time. Both places are safe for entering
221 qemu_aio_coroutine_enter(bs
->aio_context
, s
->connection_co
);
223 bdrv_dec_in_flight(bs
);
226 static void nbd_client_attach_aio_context(BlockDriverState
*bs
,
227 AioContext
*new_context
)
229 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
232 * s->connection_co is either yielded from nbd_receive_reply or from
233 * nbd_co_reconnect_loop()
235 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTED
) {
236 qio_channel_attach_aio_context(QIO_CHANNEL(s
->ioc
), new_context
);
239 bdrv_inc_in_flight(bs
);
242 * Need to wait here for the BH to run because the BH must run while the
243 * node is still drained.
245 aio_wait_bh_oneshot(new_context
, nbd_client_attach_aio_context_bh
, bs
);
248 static void coroutine_fn
nbd_client_co_drain_begin(BlockDriverState
*bs
)
250 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
253 qemu_co_sleep_wake(&s
->reconnect_sleep
);
255 nbd_co_establish_connection_cancel(s
->conn
);
257 reconnect_delay_timer_del(s
);
259 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTING_WAIT
) {
260 s
->state
= NBD_CLIENT_CONNECTING_NOWAIT
;
261 qemu_co_queue_restart_all(&s
->free_sema
);
265 static void coroutine_fn
nbd_client_co_drain_end(BlockDriverState
*bs
)
267 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
270 if (s
->wait_drained_end
) {
271 s
->wait_drained_end
= false;
272 aio_co_wake(s
->connection_co
);
277 static void nbd_teardown_connection(BlockDriverState
*bs
)
279 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
282 /* finish any pending coroutines */
283 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
286 s
->state
= NBD_CLIENT_QUIT
;
287 if (s
->connection_co
) {
288 qemu_co_sleep_wake(&s
->reconnect_sleep
);
289 nbd_co_establish_connection_cancel(s
->conn
);
291 if (qemu_in_coroutine()) {
292 s
->teardown_co
= qemu_coroutine_self();
293 /* connection_co resumes us when it terminates */
294 qemu_coroutine_yield();
295 s
->teardown_co
= NULL
;
297 BDRV_POLL_WHILE(bs
, s
->connection_co
);
299 assert(!s
->connection_co
);
302 static bool nbd_client_connecting(BDRVNBDState
*s
)
304 NBDClientState state
= qatomic_load_acquire(&s
->state
);
305 return state
== NBD_CLIENT_CONNECTING_WAIT
||
306 state
== NBD_CLIENT_CONNECTING_NOWAIT
;
309 static bool nbd_client_connecting_wait(BDRVNBDState
*s
)
311 return qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTING_WAIT
;
315 * Update @bs with information learned during a completed negotiation process.
316 * Return failure if the server's advertised options are incompatible with the
319 static int nbd_handle_updated_info(BlockDriverState
*bs
, Error
**errp
)
321 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
324 if (s
->x_dirty_bitmap
) {
325 if (!s
->info
.base_allocation
) {
326 error_setg(errp
, "requested x-dirty-bitmap %s not found",
330 if (strcmp(s
->x_dirty_bitmap
, "qemu:allocation-depth") == 0) {
331 s
->alloc_depth
= true;
335 if (s
->info
.flags
& NBD_FLAG_READ_ONLY
) {
336 ret
= bdrv_apply_auto_read_only(bs
, "NBD export is read-only", errp
);
342 if (s
->info
.flags
& NBD_FLAG_SEND_FUA
) {
343 bs
->supported_write_flags
= BDRV_REQ_FUA
;
344 bs
->supported_zero_flags
|= BDRV_REQ_FUA
;
347 if (s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
) {
348 bs
->supported_zero_flags
|= BDRV_REQ_MAY_UNMAP
;
349 if (s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
) {
350 bs
->supported_zero_flags
|= BDRV_REQ_NO_FALLBACK
;
354 trace_nbd_client_handshake_success(s
->export
);
359 static int coroutine_fn
nbd_co_do_establish_connection(BlockDriverState
*bs
,
362 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
367 s
->ioc
= nbd_co_establish_connection(s
->conn
, &s
->info
, true, errp
);
369 return -ECONNREFUSED
;
372 ret
= nbd_handle_updated_info(s
->bs
, NULL
);
375 * We have connected, but must fail for other reasons.
376 * Send NBD_CMD_DISC as a courtesy to the server.
378 NBDRequest request
= { .type
= NBD_CMD_DISC
};
380 nbd_send_request(s
->ioc
, &request
);
382 object_unref(OBJECT(s
->ioc
));
388 qio_channel_set_blocking(s
->ioc
, false, NULL
);
389 qio_channel_attach_aio_context(s
->ioc
, bdrv_get_aio_context(bs
));
391 yank_register_function(BLOCKDEV_YANK_INSTANCE(s
->bs
->node_name
), nbd_yank
,
394 /* successfully connected */
395 s
->state
= NBD_CLIENT_CONNECTED
;
396 qemu_co_queue_restart_all(&s
->free_sema
);
401 static coroutine_fn
void nbd_reconnect_attempt(BDRVNBDState
*s
)
403 if (!nbd_client_connecting(s
)) {
407 /* Wait for completion of all in-flight requests */
409 qemu_co_mutex_lock(&s
->send_mutex
);
411 while (s
->in_flight
> 0) {
412 qemu_co_mutex_unlock(&s
->send_mutex
);
413 nbd_recv_coroutines_wake_all(s
);
414 s
->wait_in_flight
= true;
415 qemu_coroutine_yield();
416 s
->wait_in_flight
= false;
417 qemu_co_mutex_lock(&s
->send_mutex
);
420 qemu_co_mutex_unlock(&s
->send_mutex
);
422 if (!nbd_client_connecting(s
)) {
427 * Now we are sure that nobody is accessing the channel, and no one will
428 * try until we set the state to CONNECTED.
431 /* Finalize previous connection if any */
433 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
434 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s
->bs
->node_name
),
436 object_unref(OBJECT(s
->ioc
));
440 nbd_co_do_establish_connection(s
->bs
, NULL
);
443 static coroutine_fn
void nbd_co_reconnect_loop(BDRVNBDState
*s
)
445 uint64_t timeout
= 1 * NANOSECONDS_PER_SECOND
;
446 uint64_t max_timeout
= 16 * NANOSECONDS_PER_SECOND
;
448 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTING_WAIT
) {
449 reconnect_delay_timer_init(s
, qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) +
450 s
->reconnect_delay
* NANOSECONDS_PER_SECOND
);
453 nbd_reconnect_attempt(s
);
455 while (nbd_client_connecting(s
)) {
457 bdrv_dec_in_flight(s
->bs
);
458 s
->wait_drained_end
= true;
461 * We may be entered once from nbd_client_attach_aio_context_bh
462 * and then from nbd_client_co_drain_end. So here is a loop.
464 qemu_coroutine_yield();
466 bdrv_inc_in_flight(s
->bs
);
468 qemu_co_sleep_ns_wakeable(&s
->reconnect_sleep
,
469 QEMU_CLOCK_REALTIME
, timeout
);
473 if (timeout
< max_timeout
) {
478 nbd_reconnect_attempt(s
);
481 reconnect_delay_timer_del(s
);
484 static coroutine_fn
void nbd_connection_entry(void *opaque
)
486 BDRVNBDState
*s
= opaque
;
489 Error
*local_err
= NULL
;
491 while (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_QUIT
) {
493 * The NBD client can only really be considered idle when it has
494 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
495 * the point where the additional scheduled coroutine entry happens
496 * after nbd_client_attach_aio_context().
498 * Therefore we keep an additional in_flight reference all the time and
499 * only drop it temporarily here.
502 if (nbd_client_connecting(s
)) {
503 nbd_co_reconnect_loop(s
);
506 if (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTED
) {
510 assert(s
->reply
.handle
== 0);
511 ret
= nbd_receive_reply(s
->bs
, s
->ioc
, &s
->reply
, &local_err
);
514 trace_nbd_read_reply_entry_fail(ret
, error_get_pretty(local_err
));
515 error_free(local_err
);
519 nbd_channel_error(s
, ret
? ret
: -EIO
);
524 * There's no need for a mutex on the receive side, because the
525 * handler acts as a synchronization point and ensures that only
526 * one coroutine is called until the reply finishes.
528 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
529 if (i
>= MAX_NBD_REQUESTS
||
530 !s
->requests
[i
].coroutine
||
531 !s
->requests
[i
].receiving
||
532 (nbd_reply_is_structured(&s
->reply
) && !s
->info
.structured_reply
))
534 nbd_channel_error(s
, -EINVAL
);
539 * We're woken up again by the request itself. Note that there
540 * is no race between yielding and reentering connection_co. This
543 * - if the request runs on the same AioContext, it is only
544 * entered after we yield
546 * - if the request runs on a different AioContext, reentering
547 * connection_co happens through a bottom half, which can only
548 * run after we yield.
550 aio_co_wake(s
->requests
[i
].coroutine
);
551 qemu_coroutine_yield();
554 qemu_co_queue_restart_all(&s
->free_sema
);
555 nbd_recv_coroutines_wake_all(s
);
556 bdrv_dec_in_flight(s
->bs
);
558 s
->connection_co
= NULL
;
560 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
561 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(s
->bs
->node_name
),
563 object_unref(OBJECT(s
->ioc
));
567 if (s
->teardown_co
) {
568 aio_co_wake(s
->teardown_co
);
573 static int nbd_co_send_request(BlockDriverState
*bs
,
577 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
580 qemu_co_mutex_lock(&s
->send_mutex
);
581 while (s
->in_flight
== MAX_NBD_REQUESTS
|| nbd_client_connecting_wait(s
)) {
582 qemu_co_queue_wait(&s
->free_sema
, &s
->send_mutex
);
585 if (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTED
) {
592 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
593 if (s
->requests
[i
].coroutine
== NULL
) {
598 g_assert(qemu_in_coroutine());
599 assert(i
< MAX_NBD_REQUESTS
);
601 s
->requests
[i
].coroutine
= qemu_coroutine_self();
602 s
->requests
[i
].offset
= request
->from
;
603 s
->requests
[i
].receiving
= false;
605 request
->handle
= INDEX_TO_HANDLE(s
, i
);
610 qio_channel_set_cork(s
->ioc
, true);
611 rc
= nbd_send_request(s
->ioc
, request
);
612 if (qatomic_load_acquire(&s
->state
) == NBD_CLIENT_CONNECTED
&&
614 if (qio_channel_writev_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
618 } else if (rc
>= 0) {
621 qio_channel_set_cork(s
->ioc
, false);
623 rc
= nbd_send_request(s
->ioc
, request
);
628 nbd_channel_error(s
, rc
);
630 s
->requests
[i
].coroutine
= NULL
;
633 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
634 aio_co_wake(s
->connection_co
);
636 qemu_co_queue_next(&s
->free_sema
);
639 qemu_co_mutex_unlock(&s
->send_mutex
);
643 static inline uint16_t payload_advance16(uint8_t **payload
)
646 return lduw_be_p(*payload
- 2);
649 static inline uint32_t payload_advance32(uint8_t **payload
)
652 return ldl_be_p(*payload
- 4);
655 static inline uint64_t payload_advance64(uint8_t **payload
)
658 return ldq_be_p(*payload
- 8);
661 static int nbd_parse_offset_hole_payload(BDRVNBDState
*s
,
662 NBDStructuredReplyChunk
*chunk
,
663 uint8_t *payload
, uint64_t orig_offset
,
664 QEMUIOVector
*qiov
, Error
**errp
)
669 if (chunk
->length
!= sizeof(offset
) + sizeof(hole_size
)) {
670 error_setg(errp
, "Protocol error: invalid payload for "
671 "NBD_REPLY_TYPE_OFFSET_HOLE");
675 offset
= payload_advance64(&payload
);
676 hole_size
= payload_advance32(&payload
);
678 if (!hole_size
|| offset
< orig_offset
|| hole_size
> qiov
->size
||
679 offset
> orig_offset
+ qiov
->size
- hole_size
) {
680 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
684 if (s
->info
.min_block
&&
685 !QEMU_IS_ALIGNED(hole_size
, s
->info
.min_block
)) {
686 trace_nbd_structured_read_compliance("hole");
689 qemu_iovec_memset(qiov
, offset
- orig_offset
, 0, hole_size
);
695 * nbd_parse_blockstatus_payload
696 * Based on our request, we expect only one extent in reply, for the
697 * base:allocation context.
699 static int nbd_parse_blockstatus_payload(BDRVNBDState
*s
,
700 NBDStructuredReplyChunk
*chunk
,
701 uint8_t *payload
, uint64_t orig_length
,
702 NBDExtent
*extent
, Error
**errp
)
706 /* The server succeeded, so it must have sent [at least] one extent */
707 if (chunk
->length
< sizeof(context_id
) + sizeof(*extent
)) {
708 error_setg(errp
, "Protocol error: invalid payload for "
709 "NBD_REPLY_TYPE_BLOCK_STATUS");
713 context_id
= payload_advance32(&payload
);
714 if (s
->info
.context_id
!= context_id
) {
715 error_setg(errp
, "Protocol error: unexpected context id %d for "
716 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
717 "id is %d", context_id
,
722 extent
->length
= payload_advance32(&payload
);
723 extent
->flags
= payload_advance32(&payload
);
725 if (extent
->length
== 0) {
726 error_setg(errp
, "Protocol error: server sent status chunk with "
732 * A server sending unaligned block status is in violation of the
733 * protocol, but as qemu-nbd 3.1 is such a server (at least for
734 * POSIX files that are not a multiple of 512 bytes, since qemu
735 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
736 * still sees an implicit hole beyond the real EOF), it's nicer to
737 * work around the misbehaving server. If the request included
738 * more than the final unaligned block, truncate it back to an
739 * aligned result; if the request was only the final block, round
740 * up to the full block and change the status to fully-allocated
741 * (always a safe status, even if it loses information).
743 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(extent
->length
,
744 s
->info
.min_block
)) {
745 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
746 if (extent
->length
> s
->info
.min_block
) {
747 extent
->length
= QEMU_ALIGN_DOWN(extent
->length
,
750 extent
->length
= s
->info
.min_block
;
756 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
757 * sent us any more than one extent, nor should it have included
758 * status beyond our request in that extent. However, it's easy
759 * enough to ignore the server's noncompliance without killing the
760 * connection; just ignore trailing extents, and clamp things to
761 * the length of our request.
763 if (chunk
->length
> sizeof(context_id
) + sizeof(*extent
)) {
764 trace_nbd_parse_blockstatus_compliance("more than one extent");
766 if (extent
->length
> orig_length
) {
767 extent
->length
= orig_length
;
768 trace_nbd_parse_blockstatus_compliance("extent length too large");
772 * HACK: if we are using x-dirty-bitmaps to access
773 * qemu:allocation-depth, treat all depths > 2 the same as 2,
774 * since nbd_client_co_block_status is only expecting the low two
777 if (s
->alloc_depth
&& extent
->flags
> 2) {
785 * nbd_parse_error_payload
786 * on success @errp contains message describing nbd error reply
788 static int nbd_parse_error_payload(NBDStructuredReplyChunk
*chunk
,
789 uint8_t *payload
, int *request_ret
,
793 uint16_t message_size
;
795 assert(chunk
->type
& (1 << 15));
797 if (chunk
->length
< sizeof(error
) + sizeof(message_size
)) {
799 "Protocol error: invalid payload for structured error");
803 error
= nbd_errno_to_system_errno(payload_advance32(&payload
));
805 error_setg(errp
, "Protocol error: server sent structured error chunk "
810 *request_ret
= -error
;
811 message_size
= payload_advance16(&payload
);
813 if (message_size
> chunk
->length
- sizeof(error
) - sizeof(message_size
)) {
814 error_setg(errp
, "Protocol error: server sent structured error chunk "
815 "with incorrect message size");
819 /* TODO: Add a trace point to mention the server complaint */
821 /* TODO handle ERROR_OFFSET */
826 static int nbd_co_receive_offset_data_payload(BDRVNBDState
*s
,
827 uint64_t orig_offset
,
828 QEMUIOVector
*qiov
, Error
**errp
)
830 QEMUIOVector sub_qiov
;
834 NBDStructuredReplyChunk
*chunk
= &s
->reply
.structured
;
836 assert(nbd_reply_is_structured(&s
->reply
));
838 /* The NBD spec requires at least one byte of payload */
839 if (chunk
->length
<= sizeof(offset
)) {
840 error_setg(errp
, "Protocol error: invalid payload for "
841 "NBD_REPLY_TYPE_OFFSET_DATA");
845 if (nbd_read64(s
->ioc
, &offset
, "OFFSET_DATA offset", errp
) < 0) {
849 data_size
= chunk
->length
- sizeof(offset
);
851 if (offset
< orig_offset
|| data_size
> qiov
->size
||
852 offset
> orig_offset
+ qiov
->size
- data_size
) {
853 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
857 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(data_size
, s
->info
.min_block
)) {
858 trace_nbd_structured_read_compliance("data");
861 qemu_iovec_init(&sub_qiov
, qiov
->niov
);
862 qemu_iovec_concat(&sub_qiov
, qiov
, offset
- orig_offset
, data_size
);
863 ret
= qio_channel_readv_all(s
->ioc
, sub_qiov
.iov
, sub_qiov
.niov
, errp
);
864 qemu_iovec_destroy(&sub_qiov
);
866 return ret
< 0 ? -EIO
: 0;
869 #define NBD_MAX_MALLOC_PAYLOAD 1000
870 static coroutine_fn
int nbd_co_receive_structured_payload(
871 BDRVNBDState
*s
, void **payload
, Error
**errp
)
876 assert(nbd_reply_is_structured(&s
->reply
));
878 len
= s
->reply
.structured
.length
;
884 if (payload
== NULL
) {
885 error_setg(errp
, "Unexpected structured payload");
889 if (len
> NBD_MAX_MALLOC_PAYLOAD
) {
890 error_setg(errp
, "Payload too large");
894 *payload
= g_new(char, len
);
895 ret
= nbd_read(s
->ioc
, *payload
, len
, "structured payload", errp
);
906 * nbd_co_do_receive_one_chunk
908 * set request_ret to received reply error
909 * if qiov is not NULL: read payload to @qiov
910 * for structured reply chunk:
911 * if error chunk: read payload, set @request_ret, do not set @payload
912 * else if offset_data chunk: read payload data to @qiov, do not set @payload
913 * else: read payload to @payload
915 * If function fails, @errp contains corresponding error message, and the
916 * connection with the server is suspect. If it returns 0, then the
917 * transaction succeeded (although @request_ret may be a negative errno
918 * corresponding to the server's error reply), and errp is unchanged.
920 static coroutine_fn
int nbd_co_do_receive_one_chunk(
921 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
922 int *request_ret
, QEMUIOVector
*qiov
, void **payload
, Error
**errp
)
925 int i
= HANDLE_TO_INDEX(s
, handle
);
926 void *local_payload
= NULL
;
927 NBDStructuredReplyChunk
*chunk
;
934 /* Wait until we're woken up by nbd_connection_entry. */
935 s
->requests
[i
].receiving
= true;
936 qemu_coroutine_yield();
937 s
->requests
[i
].receiving
= false;
938 if (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTED
) {
939 error_setg(errp
, "Connection closed");
944 assert(s
->reply
.handle
== handle
);
946 if (nbd_reply_is_simple(&s
->reply
)) {
947 if (only_structured
) {
948 error_setg(errp
, "Protocol error: simple reply when structured "
949 "reply chunk was expected");
953 *request_ret
= -nbd_errno_to_system_errno(s
->reply
.simple
.error
);
954 if (*request_ret
< 0 || !qiov
) {
958 return qio_channel_readv_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
959 errp
) < 0 ? -EIO
: 0;
962 /* handle structured reply chunk */
963 assert(s
->info
.structured_reply
);
964 chunk
= &s
->reply
.structured
;
966 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
967 if (!(chunk
->flags
& NBD_REPLY_FLAG_DONE
)) {
968 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
969 " NBD_REPLY_FLAG_DONE flag set");
973 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
980 if (chunk
->type
== NBD_REPLY_TYPE_OFFSET_DATA
) {
982 error_setg(errp
, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
986 return nbd_co_receive_offset_data_payload(s
, s
->requests
[i
].offset
,
990 if (nbd_reply_type_is_error(chunk
->type
)) {
991 payload
= &local_payload
;
994 ret
= nbd_co_receive_structured_payload(s
, payload
, errp
);
999 if (nbd_reply_type_is_error(chunk
->type
)) {
1000 ret
= nbd_parse_error_payload(chunk
, local_payload
, request_ret
, errp
);
1001 g_free(local_payload
);
1009 * nbd_co_receive_one_chunk
1010 * Read reply, wake up connection_co and set s->quit if needed.
1011 * Return value is a fatal error code or normal nbd reply error code
1013 static coroutine_fn
int nbd_co_receive_one_chunk(
1014 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
1015 int *request_ret
, QEMUIOVector
*qiov
, NBDReply
*reply
, void **payload
,
1018 int ret
= nbd_co_do_receive_one_chunk(s
, handle
, only_structured
,
1019 request_ret
, qiov
, payload
, errp
);
1022 memset(reply
, 0, sizeof(*reply
));
1023 nbd_channel_error(s
, ret
);
1025 /* For assert at loop start in nbd_connection_entry */
1028 s
->reply
.handle
= 0;
1030 if (s
->connection_co
&& !s
->wait_in_flight
) {
1032 * We must check s->wait_in_flight, because we may entered by
1033 * nbd_recv_coroutines_wake_all(), in this case we should not
1034 * wake connection_co here, it will woken by last request.
1036 aio_co_wake(s
->connection_co
);
1042 typedef struct NBDReplyChunkIter
{
1046 bool done
, only_structured
;
1047 } NBDReplyChunkIter
;
1049 static void nbd_iter_channel_error(NBDReplyChunkIter
*iter
,
1050 int ret
, Error
**local_err
)
1052 assert(local_err
&& *local_err
);
1057 error_propagate(&iter
->err
, *local_err
);
1059 error_free(*local_err
);
1065 static void nbd_iter_request_error(NBDReplyChunkIter
*iter
, int ret
)
1069 if (!iter
->request_ret
) {
1070 iter
->request_ret
= ret
;
1075 * NBD_FOREACH_REPLY_CHUNK
1076 * The pointer stored in @payload requires g_free() to free it.
1078 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
1079 qiov, reply, payload) \
1080 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
1081 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
1084 * nbd_reply_chunk_iter_receive
1085 * The pointer stored in @payload requires g_free() to free it.
1087 static bool nbd_reply_chunk_iter_receive(BDRVNBDState
*s
,
1088 NBDReplyChunkIter
*iter
,
1090 QEMUIOVector
*qiov
, NBDReply
*reply
,
1093 int ret
, request_ret
;
1094 NBDReply local_reply
;
1095 NBDStructuredReplyChunk
*chunk
;
1096 Error
*local_err
= NULL
;
1097 if (qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTED
) {
1098 error_setg(&local_err
, "Connection closed");
1099 nbd_iter_channel_error(iter
, -EIO
, &local_err
);
1104 /* Previous iteration was last. */
1108 if (reply
== NULL
) {
1109 reply
= &local_reply
;
1112 ret
= nbd_co_receive_one_chunk(s
, handle
, iter
->only_structured
,
1113 &request_ret
, qiov
, reply
, payload
,
1116 nbd_iter_channel_error(iter
, ret
, &local_err
);
1117 } else if (request_ret
< 0) {
1118 nbd_iter_request_error(iter
, request_ret
);
1121 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
1122 if (nbd_reply_is_simple(reply
) ||
1123 qatomic_load_acquire(&s
->state
) != NBD_CLIENT_CONNECTED
) {
1127 chunk
= &reply
->structured
;
1128 iter
->only_structured
= true;
1130 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
1131 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1132 assert(chunk
->flags
& NBD_REPLY_FLAG_DONE
);
1136 if (chunk
->flags
& NBD_REPLY_FLAG_DONE
) {
1137 /* This iteration is last. */
1141 /* Execute the loop body */
1145 s
->requests
[HANDLE_TO_INDEX(s
, handle
)].coroutine
= NULL
;
1147 qemu_co_mutex_lock(&s
->send_mutex
);
1149 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
1150 aio_co_wake(s
->connection_co
);
1152 qemu_co_queue_next(&s
->free_sema
);
1154 qemu_co_mutex_unlock(&s
->send_mutex
);
1159 static int nbd_co_receive_return_code(BDRVNBDState
*s
, uint64_t handle
,
1160 int *request_ret
, Error
**errp
)
1162 NBDReplyChunkIter iter
;
1164 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, NULL
, NULL
) {
1165 /* nbd_reply_chunk_iter_receive does all the work */
1168 error_propagate(errp
, iter
.err
);
1169 *request_ret
= iter
.request_ret
;
1173 static int nbd_co_receive_cmdread_reply(BDRVNBDState
*s
, uint64_t handle
,
1174 uint64_t offset
, QEMUIOVector
*qiov
,
1175 int *request_ret
, Error
**errp
)
1177 NBDReplyChunkIter iter
;
1179 void *payload
= NULL
;
1180 Error
*local_err
= NULL
;
1182 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, s
->info
.structured_reply
,
1183 qiov
, &reply
, &payload
)
1186 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1188 assert(nbd_reply_is_structured(&reply
));
1190 switch (chunk
->type
) {
1191 case NBD_REPLY_TYPE_OFFSET_DATA
:
1193 * special cased in nbd_co_receive_one_chunk, data is already
1197 case NBD_REPLY_TYPE_OFFSET_HOLE
:
1198 ret
= nbd_parse_offset_hole_payload(s
, &reply
.structured
, payload
,
1199 offset
, qiov
, &local_err
);
1201 nbd_channel_error(s
, ret
);
1202 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1206 if (!nbd_reply_type_is_error(chunk
->type
)) {
1207 /* not allowed reply type */
1208 nbd_channel_error(s
, -EINVAL
);
1209 error_setg(&local_err
,
1210 "Unexpected reply type: %d (%s) for CMD_READ",
1211 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1212 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1220 error_propagate(errp
, iter
.err
);
1221 *request_ret
= iter
.request_ret
;
1225 static int nbd_co_receive_blockstatus_reply(BDRVNBDState
*s
,
1226 uint64_t handle
, uint64_t length
,
1228 int *request_ret
, Error
**errp
)
1230 NBDReplyChunkIter iter
;
1232 void *payload
= NULL
;
1233 Error
*local_err
= NULL
;
1234 bool received
= false;
1236 assert(!extent
->length
);
1237 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, &reply
, &payload
) {
1239 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1241 assert(nbd_reply_is_structured(&reply
));
1243 switch (chunk
->type
) {
1244 case NBD_REPLY_TYPE_BLOCK_STATUS
:
1246 nbd_channel_error(s
, -EINVAL
);
1247 error_setg(&local_err
, "Several BLOCK_STATUS chunks in reply");
1248 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1252 ret
= nbd_parse_blockstatus_payload(s
, &reply
.structured
,
1253 payload
, length
, extent
,
1256 nbd_channel_error(s
, ret
);
1257 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1261 if (!nbd_reply_type_is_error(chunk
->type
)) {
1262 nbd_channel_error(s
, -EINVAL
);
1263 error_setg(&local_err
,
1264 "Unexpected reply type: %d (%s) "
1265 "for CMD_BLOCK_STATUS",
1266 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1267 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1275 if (!extent
->length
&& !iter
.request_ret
) {
1276 error_setg(&local_err
, "Server did not reply with any status extents");
1277 nbd_iter_channel_error(&iter
, -EIO
, &local_err
);
1280 error_propagate(errp
, iter
.err
);
1281 *request_ret
= iter
.request_ret
;
1285 static int nbd_co_request(BlockDriverState
*bs
, NBDRequest
*request
,
1286 QEMUIOVector
*write_qiov
)
1288 int ret
, request_ret
;
1289 Error
*local_err
= NULL
;
1290 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1292 assert(request
->type
!= NBD_CMD_READ
);
1294 assert(request
->type
== NBD_CMD_WRITE
);
1295 assert(request
->len
== iov_size(write_qiov
->iov
, write_qiov
->niov
));
1297 assert(request
->type
!= NBD_CMD_WRITE
);
1301 ret
= nbd_co_send_request(bs
, request
, write_qiov
);
1306 ret
= nbd_co_receive_return_code(s
, request
->handle
,
1307 &request_ret
, &local_err
);
1309 trace_nbd_co_request_fail(request
->from
, request
->len
,
1310 request
->handle
, request
->flags
,
1312 nbd_cmd_lookup(request
->type
),
1313 ret
, error_get_pretty(local_err
));
1314 error_free(local_err
);
1317 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1319 return ret
? ret
: request_ret
;
1322 static int nbd_client_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
1323 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1325 int ret
, request_ret
;
1326 Error
*local_err
= NULL
;
1327 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1328 NBDRequest request
= {
1329 .type
= NBD_CMD_READ
,
1334 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1341 * Work around the fact that the block layer doesn't do
1342 * byte-accurate sizing yet - if the read exceeds the server's
1343 * advertised size because the block layer rounded size up, then
1344 * truncate the request to the server and tail-pad with zero.
1346 if (offset
>= s
->info
.size
) {
1347 assert(bytes
< BDRV_SECTOR_SIZE
);
1348 qemu_iovec_memset(qiov
, 0, 0, bytes
);
1351 if (offset
+ bytes
> s
->info
.size
) {
1352 uint64_t slop
= offset
+ bytes
- s
->info
.size
;
1354 assert(slop
< BDRV_SECTOR_SIZE
);
1355 qemu_iovec_memset(qiov
, bytes
- slop
, 0, slop
);
1356 request
.len
-= slop
;
1360 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1365 ret
= nbd_co_receive_cmdread_reply(s
, request
.handle
, offset
, qiov
,
1366 &request_ret
, &local_err
);
1368 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1369 request
.flags
, request
.type
,
1370 nbd_cmd_lookup(request
.type
),
1371 ret
, error_get_pretty(local_err
));
1372 error_free(local_err
);
1375 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1377 return ret
? ret
: request_ret
;
1380 static int nbd_client_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1381 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1383 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1384 NBDRequest request
= {
1385 .type
= NBD_CMD_WRITE
,
1390 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1391 if (flags
& BDRV_REQ_FUA
) {
1392 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1393 request
.flags
|= NBD_CMD_FLAG_FUA
;
1396 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1401 return nbd_co_request(bs
, &request
, qiov
);
1404 static int nbd_client_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1405 int bytes
, BdrvRequestFlags flags
)
1407 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1408 NBDRequest request
= {
1409 .type
= NBD_CMD_WRITE_ZEROES
,
1414 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1415 if (!(s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
)) {
1419 if (flags
& BDRV_REQ_FUA
) {
1420 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1421 request
.flags
|= NBD_CMD_FLAG_FUA
;
1423 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1424 request
.flags
|= NBD_CMD_FLAG_NO_HOLE
;
1426 if (flags
& BDRV_REQ_NO_FALLBACK
) {
1427 assert(s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
);
1428 request
.flags
|= NBD_CMD_FLAG_FAST_ZERO
;
1434 return nbd_co_request(bs
, &request
, NULL
);
1437 static int nbd_client_co_flush(BlockDriverState
*bs
)
1439 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1440 NBDRequest request
= { .type
= NBD_CMD_FLUSH
};
1442 if (!(s
->info
.flags
& NBD_FLAG_SEND_FLUSH
)) {
1449 return nbd_co_request(bs
, &request
, NULL
);
1452 static int nbd_client_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
1455 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1456 NBDRequest request
= {
1457 .type
= NBD_CMD_TRIM
,
1462 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1463 if (!(s
->info
.flags
& NBD_FLAG_SEND_TRIM
) || !bytes
) {
1467 return nbd_co_request(bs
, &request
, NULL
);
1470 static int coroutine_fn
nbd_client_co_block_status(
1471 BlockDriverState
*bs
, bool want_zero
, int64_t offset
, int64_t bytes
,
1472 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
1474 int ret
, request_ret
;
1475 NBDExtent extent
= { 0 };
1476 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1477 Error
*local_err
= NULL
;
1479 NBDRequest request
= {
1480 .type
= NBD_CMD_BLOCK_STATUS
,
1482 .len
= MIN(QEMU_ALIGN_DOWN(INT_MAX
, bs
->bl
.request_alignment
),
1483 MIN(bytes
, s
->info
.size
- offset
)),
1484 .flags
= NBD_CMD_FLAG_REQ_ONE
,
1487 if (!s
->info
.base_allocation
) {
1491 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1495 * Work around the fact that the block layer doesn't do
1496 * byte-accurate sizing yet - if the status request exceeds the
1497 * server's advertised size because the block layer rounded size
1498 * up, we truncated the request to the server (above), or are
1499 * called on just the hole.
1501 if (offset
>= s
->info
.size
) {
1503 assert(bytes
< BDRV_SECTOR_SIZE
);
1504 /* Intentionally don't report offset_valid for the hole */
1505 return BDRV_BLOCK_ZERO
;
1508 if (s
->info
.min_block
) {
1509 assert(QEMU_IS_ALIGNED(request
.len
, s
->info
.min_block
));
1512 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1517 ret
= nbd_co_receive_blockstatus_reply(s
, request
.handle
, bytes
,
1518 &extent
, &request_ret
,
1521 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1522 request
.flags
, request
.type
,
1523 nbd_cmd_lookup(request
.type
),
1524 ret
, error_get_pretty(local_err
));
1525 error_free(local_err
);
1528 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1530 if (ret
< 0 || request_ret
< 0) {
1531 return ret
? ret
: request_ret
;
1534 assert(extent
.length
);
1535 *pnum
= extent
.length
;
1538 return (extent
.flags
& NBD_STATE_HOLE
? 0 : BDRV_BLOCK_DATA
) |
1539 (extent
.flags
& NBD_STATE_ZERO
? BDRV_BLOCK_ZERO
: 0) |
1540 BDRV_BLOCK_OFFSET_VALID
;
1543 static int nbd_client_reopen_prepare(BDRVReopenState
*state
,
1544 BlockReopenQueue
*queue
, Error
**errp
)
1546 BDRVNBDState
*s
= (BDRVNBDState
*)state
->bs
->opaque
;
1548 if ((state
->flags
& BDRV_O_RDWR
) && (s
->info
.flags
& NBD_FLAG_READ_ONLY
)) {
1549 error_setg(errp
, "Can't reopen read-only NBD mount as read/write");
1555 static void nbd_yank(void *opaque
)
1557 BlockDriverState
*bs
= opaque
;
1558 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1560 qatomic_store_release(&s
->state
, NBD_CLIENT_QUIT
);
1561 qio_channel_shutdown(QIO_CHANNEL(s
->ioc
), QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
1564 static void nbd_client_close(BlockDriverState
*bs
)
1566 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1567 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1570 nbd_send_request(s
->ioc
, &request
);
1573 nbd_teardown_connection(bs
);
1576 static QIOChannelSocket
*nbd_establish_connection(BlockDriverState
*bs
,
1577 SocketAddress
*saddr
,
1581 QIOChannelSocket
*sioc
;
1583 sioc
= qio_channel_socket_new();
1584 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
1586 qio_channel_socket_connect_sync(sioc
, saddr
, errp
);
1588 object_unref(OBJECT(sioc
));
1592 yank_register_function(BLOCKDEV_YANK_INSTANCE(bs
->node_name
), nbd_yank
, bs
);
1593 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
1598 /* nbd_client_handshake takes ownership on sioc. */
1599 static int nbd_client_handshake(BlockDriverState
*bs
, QIOChannelSocket
*sioc
,
1602 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1603 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1606 trace_nbd_client_handshake(s
->export
);
1607 qio_channel_set_blocking(QIO_CHANNEL(sioc
), false, NULL
);
1608 qio_channel_attach_aio_context(QIO_CHANNEL(sioc
), aio_context
);
1610 s
->info
.request_sizes
= true;
1611 s
->info
.structured_reply
= true;
1612 s
->info
.base_allocation
= true;
1613 s
->info
.x_dirty_bitmap
= g_strdup(s
->x_dirty_bitmap
);
1614 s
->info
.name
= g_strdup(s
->export
?: "");
1615 ret
= nbd_receive_negotiate(aio_context
, QIO_CHANNEL(sioc
), s
->tlscreds
,
1616 s
->hostname
, &s
->ioc
, &s
->info
, errp
);
1617 g_free(s
->info
.x_dirty_bitmap
);
1618 g_free(s
->info
.name
);
1620 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs
->node_name
),
1622 object_unref(OBJECT(sioc
));
1627 /* sioc is referenced by s->ioc */
1628 object_unref(OBJECT(sioc
));
1630 s
->ioc
= QIO_CHANNEL(sioc
);
1634 ret
= nbd_handle_updated_info(bs
, errp
);
1637 * We have connected, but must fail for other reasons.
1638 * Send NBD_CMD_DISC as a courtesy to the server.
1640 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1642 nbd_send_request(s
->ioc
, &request
);
1644 yank_unregister_function(BLOCKDEV_YANK_INSTANCE(bs
->node_name
),
1646 object_unref(OBJECT(s
->ioc
));
1655 * Parse nbd_open options
1658 static int nbd_parse_uri(const char *filename
, QDict
*options
)
1662 QueryParams
*qp
= NULL
;
1666 uri
= uri_parse(filename
);
1672 if (!g_strcmp0(uri
->scheme
, "nbd")) {
1674 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
1676 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
1683 p
= uri
->path
? uri
->path
: "";
1688 qdict_put_str(options
, "export", p
);
1691 qp
= query_params_parse(uri
->query
);
1692 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
1698 /* nbd+unix:///export?socket=path */
1699 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
1703 qdict_put_str(options
, "server.type", "unix");
1704 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
1709 /* nbd[+tcp]://host[:port]/export */
1715 /* strip braces from literal IPv6 address */
1716 if (uri
->server
[0] == '[') {
1717 host
= qstring_from_substr(uri
->server
, 1,
1718 strlen(uri
->server
) - 1);
1720 host
= qstring_from_str(uri
->server
);
1723 qdict_put_str(options
, "server.type", "inet");
1724 qdict_put(options
, "server.host", host
);
1726 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
1727 qdict_put_str(options
, "server.port", port_str
);
1733 query_params_free(qp
);
1739 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
1741 const QDictEntry
*e
;
1743 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
1744 if (!strcmp(e
->key
, "host") ||
1745 !strcmp(e
->key
, "port") ||
1746 !strcmp(e
->key
, "path") ||
1747 !strcmp(e
->key
, "export") ||
1748 strstart(e
->key
, "server.", NULL
))
1750 error_setg(errp
, "Option '%s' cannot be used with a file name",
1759 static void nbd_parse_filename(const char *filename
, QDict
*options
,
1762 g_autofree
char *file
= NULL
;
1764 const char *host_spec
;
1765 const char *unixpath
;
1767 if (nbd_has_filename_options_conflict(options
, errp
)) {
1771 if (strstr(filename
, "://")) {
1772 int ret
= nbd_parse_uri(filename
, options
);
1774 error_setg(errp
, "No valid URL specified");
1779 file
= g_strdup(filename
);
1781 export_name
= strstr(file
, EN_OPTSTR
);
1783 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
1786 export_name
[0] = 0; /* truncate 'file' */
1787 export_name
+= strlen(EN_OPTSTR
);
1789 qdict_put_str(options
, "export", export_name
);
1792 /* extract the host_spec - fail if it's not nbd:... */
1793 if (!strstart(file
, "nbd:", &host_spec
)) {
1794 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
1802 /* are we a UNIX or TCP socket? */
1803 if (strstart(host_spec
, "unix:", &unixpath
)) {
1804 qdict_put_str(options
, "server.type", "unix");
1805 qdict_put_str(options
, "server.path", unixpath
);
1807 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
1809 if (inet_parse(addr
, host_spec
, errp
)) {
1813 qdict_put_str(options
, "server.type", "inet");
1814 qdict_put_str(options
, "server.host", addr
->host
);
1815 qdict_put_str(options
, "server.port", addr
->port
);
1817 qapi_free_InetSocketAddress(addr
);
1821 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
1822 QemuOpts
*legacy_opts
,
1825 const char *path
= qemu_opt_get(legacy_opts
, "path");
1826 const char *host
= qemu_opt_get(legacy_opts
, "host");
1827 const char *port
= qemu_opt_get(legacy_opts
, "port");
1828 const QDictEntry
*e
;
1830 if (!path
&& !host
&& !port
) {
1834 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
1836 if (strstart(e
->key
, "server.", NULL
)) {
1837 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
1844 error_setg(errp
, "path and host may not be used at the same time");
1848 error_setg(errp
, "port may not be used without host");
1852 qdict_put_str(output_options
, "server.type", "unix");
1853 qdict_put_str(output_options
, "server.path", path
);
1855 qdict_put_str(output_options
, "server.type", "inet");
1856 qdict_put_str(output_options
, "server.host", host
);
1857 qdict_put_str(output_options
, "server.port",
1858 port
?: stringify(NBD_DEFAULT_PORT
));
1864 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
1867 SocketAddress
*saddr
= NULL
;
1871 qdict_extract_subqdict(options
, &addr
, "server.");
1872 if (!qdict_size(addr
)) {
1873 error_setg(errp
, "NBD server address missing");
1877 iv
= qobject_input_visitor_new_flat_confused(addr
, errp
);
1882 if (!visit_type_SocketAddress(iv
, NULL
, &saddr
, errp
)) {
1886 if (socket_address_parse_named_fd(saddr
, errp
) < 0) {
1887 qapi_free_SocketAddress(saddr
);
1893 qobject_unref(addr
);
1898 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
1901 QCryptoTLSCreds
*creds
;
1903 obj
= object_resolve_path_component(
1904 object_get_objects_root(), id
);
1906 error_setg(errp
, "No TLS credentials with id '%s'",
1910 creds
= (QCryptoTLSCreds
*)
1911 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
1913 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1918 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
1920 "Expecting TLS credentials with a client endpoint");
1928 static QemuOptsList nbd_runtime_opts
= {
1930 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
1934 .type
= QEMU_OPT_STRING
,
1935 .help
= "TCP host to connect to",
1939 .type
= QEMU_OPT_STRING
,
1940 .help
= "TCP port to connect to",
1944 .type
= QEMU_OPT_STRING
,
1945 .help
= "Unix socket path to connect to",
1949 .type
= QEMU_OPT_STRING
,
1950 .help
= "Name of the NBD export to open",
1953 .name
= "tls-creds",
1954 .type
= QEMU_OPT_STRING
,
1955 .help
= "ID of the TLS credentials to use",
1958 .name
= "x-dirty-bitmap",
1959 .type
= QEMU_OPT_STRING
,
1960 .help
= "experimental: expose named dirty bitmap in place of "
1964 .name
= "reconnect-delay",
1965 .type
= QEMU_OPT_NUMBER
,
1966 .help
= "On an unexpected disconnect, the nbd client tries to "
1967 "connect again until succeeding or encountering a serious "
1968 "error. During the first @reconnect-delay seconds, all "
1969 "requests are paused and will be rerun on a successful "
1970 "reconnect. After that time, any delayed requests and all "
1971 "future requests before a successful reconnect will "
1972 "immediately fail. Default 0",
1974 { /* end of list */ }
1978 static int nbd_process_options(BlockDriverState
*bs
, QDict
*options
,
1981 BDRVNBDState
*s
= bs
->opaque
;
1985 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
1986 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
1990 /* Translate @host, @port, and @path to a SocketAddress */
1991 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
1995 /* Pop the config into our state object. Exit if invalid. */
1996 s
->saddr
= nbd_config(s
, options
, errp
);
2001 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
2002 if (s
->export
&& strlen(s
->export
) > NBD_MAX_STRING_SIZE
) {
2003 error_setg(errp
, "export name too long to send to server");
2007 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
2008 if (s
->tlscredsid
) {
2009 s
->tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
2014 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
2015 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
2016 error_setg(errp
, "TLS only supported over IP sockets");
2019 s
->hostname
= s
->saddr
->u
.inet
.host
;
2022 s
->x_dirty_bitmap
= g_strdup(qemu_opt_get(opts
, "x-dirty-bitmap"));
2023 if (s
->x_dirty_bitmap
&& strlen(s
->x_dirty_bitmap
) > NBD_MAX_STRING_SIZE
) {
2024 error_setg(errp
, "x-dirty-bitmap query too long to send to server");
2028 s
->reconnect_delay
= qemu_opt_get_number(opts
, "reconnect-delay", 0);
2033 qemu_opts_del(opts
);
2037 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
2041 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
2042 QIOChannelSocket
*sioc
;
2045 qemu_co_mutex_init(&s
->send_mutex
);
2046 qemu_co_queue_init(&s
->free_sema
);
2048 if (!yank_register_instance(BLOCKDEV_YANK_INSTANCE(bs
->node_name
), errp
)) {
2052 ret
= nbd_process_options(bs
, options
, errp
);
2057 s
->conn
= nbd_client_connection_new(s
->saddr
, true, s
->export
,
2058 s
->x_dirty_bitmap
, s
->tlscreds
);
2061 * establish TCP connection, return error if it fails
2062 * TODO: Configurable retry-until-timeout behaviour.
2064 sioc
= nbd_establish_connection(bs
, s
->saddr
, errp
);
2066 ret
= -ECONNREFUSED
;
2070 ret
= nbd_client_handshake(bs
, sioc
, errp
);
2074 /* successfully connected */
2075 s
->state
= NBD_CLIENT_CONNECTED
;
2077 s
->connection_co
= qemu_coroutine_create(nbd_connection_entry
, s
);
2078 bdrv_inc_in_flight(bs
);
2079 aio_co_schedule(bdrv_get_aio_context(bs
), s
->connection_co
);
2084 nbd_clear_bdrvstate(bs
);
2088 static int nbd_co_flush(BlockDriverState
*bs
)
2090 return nbd_client_co_flush(bs
);
2093 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
2095 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
2096 uint32_t min
= s
->info
.min_block
;
2097 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
2100 * If the server did not advertise an alignment:
2101 * - a size that is not sector-aligned implies that an alignment
2102 * of 1 can be used to access those tail bytes
2103 * - advertisement of block status requires an alignment of 1, so
2104 * that we don't violate block layer constraints that block
2105 * status is always aligned (as we can't control whether the
2106 * server will report sub-sector extents, such as a hole at EOF
2107 * on an unaligned POSIX file)
2108 * - otherwise, assume the server is so old that we are safer avoiding
2109 * sub-sector requests
2112 min
= (!QEMU_IS_ALIGNED(s
->info
.size
, BDRV_SECTOR_SIZE
) ||
2113 s
->info
.base_allocation
) ? 1 : BDRV_SECTOR_SIZE
;
2116 bs
->bl
.request_alignment
= min
;
2117 bs
->bl
.max_pdiscard
= QEMU_ALIGN_DOWN(INT_MAX
, min
);
2118 bs
->bl
.max_pwrite_zeroes
= max
;
2119 bs
->bl
.max_transfer
= max
;
2121 if (s
->info
.opt_block
&&
2122 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
2123 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
2127 static void nbd_close(BlockDriverState
*bs
)
2129 nbd_client_close(bs
);
2130 nbd_clear_bdrvstate(bs
);
2134 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2135 * to a smaller size with exact=false, there is no reason to fail the
2138 * Preallocation mode is ignored since it does not seems useful to fail when
2139 * we never change anything.
2141 static int coroutine_fn
nbd_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2142 bool exact
, PreallocMode prealloc
,
2143 BdrvRequestFlags flags
, Error
**errp
)
2145 BDRVNBDState
*s
= bs
->opaque
;
2147 if (offset
!= s
->info
.size
&& exact
) {
2148 error_setg(errp
, "Cannot resize NBD nodes");
2152 if (offset
> s
->info
.size
) {
2153 error_setg(errp
, "Cannot grow NBD nodes");
2160 static int64_t nbd_getlength(BlockDriverState
*bs
)
2162 BDRVNBDState
*s
= bs
->opaque
;
2164 return s
->info
.size
;
2167 static void nbd_refresh_filename(BlockDriverState
*bs
)
2169 BDRVNBDState
*s
= bs
->opaque
;
2170 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
2173 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
2174 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
2175 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
2179 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
2180 path
= s
->saddr
->u
.q_unix
.path
;
2181 } /* else can't represent as pseudo-filename */
2183 if (path
&& s
->export
) {
2184 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2185 "nbd+unix:///%s?socket=%s", s
->export
, path
);
2186 } else if (path
&& !s
->export
) {
2187 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2188 "nbd+unix://?socket=%s", path
);
2189 } else if (host
&& s
->export
) {
2190 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2191 "nbd://%s:%s/%s", host
, port
, s
->export
);
2192 } else if (host
&& !s
->export
) {
2193 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2194 "nbd://%s:%s", host
, port
);
2196 if (len
>= sizeof(bs
->exact_filename
)) {
2197 /* Name is too long to represent exactly, so leave it empty. */
2198 bs
->exact_filename
[0] = '\0';
2202 static char *nbd_dirname(BlockDriverState
*bs
, Error
**errp
)
2204 /* The generic bdrv_dirname() implementation is able to work out some
2205 * directory name for NBD nodes, but that would be wrong. So far there is no
2206 * specification for how "export paths" would work, so NBD does not have
2207 * directory names. */
2208 error_setg(errp
, "Cannot generate a base directory for NBD nodes");
2212 static const char *const nbd_strong_runtime_opts
[] = {
2223 static void nbd_cancel_in_flight(BlockDriverState
*bs
)
2225 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
2227 reconnect_delay_timer_del(s
);
2229 if (s
->state
== NBD_CLIENT_CONNECTING_WAIT
) {
2230 s
->state
= NBD_CLIENT_CONNECTING_NOWAIT
;
2231 qemu_co_queue_restart_all(&s
->free_sema
);
2235 static BlockDriver bdrv_nbd
= {
2236 .format_name
= "nbd",
2237 .protocol_name
= "nbd",
2238 .instance_size
= sizeof(BDRVNBDState
),
2239 .bdrv_parse_filename
= nbd_parse_filename
,
2240 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2241 .create_opts
= &bdrv_create_opts_simple
,
2242 .bdrv_file_open
= nbd_open
,
2243 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2244 .bdrv_co_preadv
= nbd_client_co_preadv
,
2245 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2246 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2247 .bdrv_close
= nbd_close
,
2248 .bdrv_co_flush_to_os
= nbd_co_flush
,
2249 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2250 .bdrv_refresh_limits
= nbd_refresh_limits
,
2251 .bdrv_co_truncate
= nbd_co_truncate
,
2252 .bdrv_getlength
= nbd_getlength
,
2253 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2254 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2255 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2256 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2257 .bdrv_refresh_filename
= nbd_refresh_filename
,
2258 .bdrv_co_block_status
= nbd_client_co_block_status
,
2259 .bdrv_dirname
= nbd_dirname
,
2260 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2261 .bdrv_cancel_in_flight
= nbd_cancel_in_flight
,
2264 static BlockDriver bdrv_nbd_tcp
= {
2265 .format_name
= "nbd",
2266 .protocol_name
= "nbd+tcp",
2267 .instance_size
= sizeof(BDRVNBDState
),
2268 .bdrv_parse_filename
= nbd_parse_filename
,
2269 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2270 .create_opts
= &bdrv_create_opts_simple
,
2271 .bdrv_file_open
= nbd_open
,
2272 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2273 .bdrv_co_preadv
= nbd_client_co_preadv
,
2274 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2275 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2276 .bdrv_close
= nbd_close
,
2277 .bdrv_co_flush_to_os
= nbd_co_flush
,
2278 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2279 .bdrv_refresh_limits
= nbd_refresh_limits
,
2280 .bdrv_co_truncate
= nbd_co_truncate
,
2281 .bdrv_getlength
= nbd_getlength
,
2282 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2283 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2284 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2285 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2286 .bdrv_refresh_filename
= nbd_refresh_filename
,
2287 .bdrv_co_block_status
= nbd_client_co_block_status
,
2288 .bdrv_dirname
= nbd_dirname
,
2289 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2290 .bdrv_cancel_in_flight
= nbd_cancel_in_flight
,
2293 static BlockDriver bdrv_nbd_unix
= {
2294 .format_name
= "nbd",
2295 .protocol_name
= "nbd+unix",
2296 .instance_size
= sizeof(BDRVNBDState
),
2297 .bdrv_parse_filename
= nbd_parse_filename
,
2298 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2299 .create_opts
= &bdrv_create_opts_simple
,
2300 .bdrv_file_open
= nbd_open
,
2301 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2302 .bdrv_co_preadv
= nbd_client_co_preadv
,
2303 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2304 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2305 .bdrv_close
= nbd_close
,
2306 .bdrv_co_flush_to_os
= nbd_co_flush
,
2307 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2308 .bdrv_refresh_limits
= nbd_refresh_limits
,
2309 .bdrv_co_truncate
= nbd_co_truncate
,
2310 .bdrv_getlength
= nbd_getlength
,
2311 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2312 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2313 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2314 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2315 .bdrv_refresh_filename
= nbd_refresh_filename
,
2316 .bdrv_co_block_status
= nbd_client_co_block_status
,
2317 .bdrv_dirname
= nbd_dirname
,
2318 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2319 .bdrv_cancel_in_flight
= nbd_cancel_in_flight
,
2322 static void bdrv_nbd_init(void)
2324 bdrv_register(&bdrv_nbd
);
2325 bdrv_register(&bdrv_nbd_tcp
);
2326 bdrv_register(&bdrv_nbd_unix
);
2329 block_init(bdrv_nbd_init
);