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"
42 #include "block/qdict.h"
43 #include "block/nbd.h"
44 #include "block/block_int.h"
46 #define EN_OPTSTR ":exportname="
47 #define MAX_NBD_REQUESTS 16
49 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
50 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
54 uint64_t offset
; /* original offset of the request */
55 bool receiving
; /* waiting for connection_co? */
58 typedef enum NBDClientState
{
59 NBD_CLIENT_CONNECTING_WAIT
,
60 NBD_CLIENT_CONNECTING_NOWAIT
,
65 typedef struct BDRVNBDState
{
66 QIOChannelSocket
*sioc
; /* The master data channel */
67 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
72 Coroutine
*connection_co
;
73 Coroutine
*teardown_co
;
74 QemuCoSleepState
*connection_co_sleep_ns_state
;
76 bool wait_drained_end
;
83 NBDClientRequest requests
[MAX_NBD_REQUESTS
];
87 /* Connection parameters */
88 uint32_t reconnect_delay
;
90 char *export
, *tlscredsid
;
91 QCryptoTLSCreds
*tlscreds
;
96 static int nbd_client_connect(BlockDriverState
*bs
, Error
**errp
);
98 static void nbd_clear_bdrvstate(BDRVNBDState
*s
)
100 object_unref(OBJECT(s
->tlscreds
));
101 qapi_free_SocketAddress(s
->saddr
);
105 g_free(s
->tlscredsid
);
106 s
->tlscredsid
= NULL
;
107 g_free(s
->x_dirty_bitmap
);
108 s
->x_dirty_bitmap
= NULL
;
111 static void nbd_channel_error(BDRVNBDState
*s
, int ret
)
114 if (s
->state
== NBD_CLIENT_CONNECTED
) {
115 s
->state
= s
->reconnect_delay
? NBD_CLIENT_CONNECTING_WAIT
:
116 NBD_CLIENT_CONNECTING_NOWAIT
;
119 if (s
->state
== NBD_CLIENT_CONNECTED
) {
120 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
122 s
->state
= NBD_CLIENT_QUIT
;
126 static void nbd_recv_coroutines_wake_all(BDRVNBDState
*s
)
130 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
131 NBDClientRequest
*req
= &s
->requests
[i
];
133 if (req
->coroutine
&& req
->receiving
) {
134 aio_co_wake(req
->coroutine
);
139 static void nbd_client_detach_aio_context(BlockDriverState
*bs
)
141 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
143 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
146 static void nbd_client_attach_aio_context_bh(void *opaque
)
148 BlockDriverState
*bs
= opaque
;
149 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
152 * The node is still drained, so we know the coroutine has yielded in
153 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
154 * entered for the first time. Both places are safe for entering the
157 qemu_aio_coroutine_enter(bs
->aio_context
, s
->connection_co
);
158 bdrv_dec_in_flight(bs
);
161 static void nbd_client_attach_aio_context(BlockDriverState
*bs
,
162 AioContext
*new_context
)
164 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
167 * s->connection_co is either yielded from nbd_receive_reply or from
168 * nbd_co_reconnect_loop()
170 if (s
->state
== NBD_CLIENT_CONNECTED
) {
171 qio_channel_attach_aio_context(QIO_CHANNEL(s
->ioc
), new_context
);
174 bdrv_inc_in_flight(bs
);
177 * Need to wait here for the BH to run because the BH must run while the
178 * node is still drained.
180 aio_wait_bh_oneshot(new_context
, nbd_client_attach_aio_context_bh
, bs
);
183 static void coroutine_fn
nbd_client_co_drain_begin(BlockDriverState
*bs
)
185 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
188 if (s
->connection_co_sleep_ns_state
) {
189 qemu_co_sleep_wake(s
->connection_co_sleep_ns_state
);
193 static void coroutine_fn
nbd_client_co_drain_end(BlockDriverState
*bs
)
195 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
198 if (s
->wait_drained_end
) {
199 s
->wait_drained_end
= false;
200 aio_co_wake(s
->connection_co
);
205 static void nbd_teardown_connection(BlockDriverState
*bs
)
207 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
209 if (s
->state
== NBD_CLIENT_CONNECTED
) {
210 /* finish any pending coroutines */
212 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
214 s
->state
= NBD_CLIENT_QUIT
;
215 if (s
->connection_co
) {
216 if (s
->connection_co_sleep_ns_state
) {
217 qemu_co_sleep_wake(s
->connection_co_sleep_ns_state
);
220 if (qemu_in_coroutine()) {
221 s
->teardown_co
= qemu_coroutine_self();
222 /* connection_co resumes us when it terminates */
223 qemu_coroutine_yield();
224 s
->teardown_co
= NULL
;
226 BDRV_POLL_WHILE(bs
, s
->connection_co
);
228 assert(!s
->connection_co
);
231 static bool nbd_client_connecting(BDRVNBDState
*s
)
233 return s
->state
== NBD_CLIENT_CONNECTING_WAIT
||
234 s
->state
== NBD_CLIENT_CONNECTING_NOWAIT
;
237 static bool nbd_client_connecting_wait(BDRVNBDState
*s
)
239 return s
->state
== NBD_CLIENT_CONNECTING_WAIT
;
242 static coroutine_fn
void nbd_reconnect_attempt(BDRVNBDState
*s
)
244 Error
*local_err
= NULL
;
246 if (!nbd_client_connecting(s
)) {
250 /* Wait for completion of all in-flight requests */
252 qemu_co_mutex_lock(&s
->send_mutex
);
254 while (s
->in_flight
> 0) {
255 qemu_co_mutex_unlock(&s
->send_mutex
);
256 nbd_recv_coroutines_wake_all(s
);
257 s
->wait_in_flight
= true;
258 qemu_coroutine_yield();
259 s
->wait_in_flight
= false;
260 qemu_co_mutex_lock(&s
->send_mutex
);
263 qemu_co_mutex_unlock(&s
->send_mutex
);
265 if (!nbd_client_connecting(s
)) {
270 * Now we are sure that nobody is accessing the channel, and no one will
271 * try until we set the state to CONNECTED.
274 /* Finalize previous connection if any */
276 nbd_client_detach_aio_context(s
->bs
);
277 object_unref(OBJECT(s
->sioc
));
279 object_unref(OBJECT(s
->ioc
));
283 s
->connect_status
= nbd_client_connect(s
->bs
, &local_err
);
284 error_free(s
->connect_err
);
285 s
->connect_err
= NULL
;
286 error_propagate(&s
->connect_err
, local_err
);
288 if (s
->connect_status
< 0) {
293 /* successfully connected */
294 s
->state
= NBD_CLIENT_CONNECTED
;
295 qemu_co_queue_restart_all(&s
->free_sema
);
298 static coroutine_fn
void nbd_co_reconnect_loop(BDRVNBDState
*s
)
300 uint64_t start_time_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
301 uint64_t delay_ns
= s
->reconnect_delay
* NANOSECONDS_PER_SECOND
;
302 uint64_t timeout
= 1 * NANOSECONDS_PER_SECOND
;
303 uint64_t max_timeout
= 16 * NANOSECONDS_PER_SECOND
;
305 nbd_reconnect_attempt(s
);
307 while (nbd_client_connecting(s
)) {
308 if (s
->state
== NBD_CLIENT_CONNECTING_WAIT
&&
309 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - start_time_ns
> delay_ns
)
311 s
->state
= NBD_CLIENT_CONNECTING_NOWAIT
;
312 qemu_co_queue_restart_all(&s
->free_sema
);
315 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME
, timeout
,
316 &s
->connection_co_sleep_ns_state
);
318 bdrv_dec_in_flight(s
->bs
);
319 s
->wait_drained_end
= true;
322 * We may be entered once from nbd_client_attach_aio_context_bh
323 * and then from nbd_client_co_drain_end. So here is a loop.
325 qemu_coroutine_yield();
327 bdrv_inc_in_flight(s
->bs
);
329 if (timeout
< max_timeout
) {
333 nbd_reconnect_attempt(s
);
337 static coroutine_fn
void nbd_connection_entry(void *opaque
)
339 BDRVNBDState
*s
= opaque
;
342 Error
*local_err
= NULL
;
344 while (s
->state
!= NBD_CLIENT_QUIT
) {
346 * The NBD client can only really be considered idle when it has
347 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
348 * the point where the additional scheduled coroutine entry happens
349 * after nbd_client_attach_aio_context().
351 * Therefore we keep an additional in_flight reference all the time and
352 * only drop it temporarily here.
355 if (nbd_client_connecting(s
)) {
356 nbd_co_reconnect_loop(s
);
359 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
363 assert(s
->reply
.handle
== 0);
364 ret
= nbd_receive_reply(s
->bs
, s
->ioc
, &s
->reply
, &local_err
);
367 trace_nbd_read_reply_entry_fail(ret
, error_get_pretty(local_err
));
368 error_free(local_err
);
372 nbd_channel_error(s
, ret
? ret
: -EIO
);
377 * There's no need for a mutex on the receive side, because the
378 * handler acts as a synchronization point and ensures that only
379 * one coroutine is called until the reply finishes.
381 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
382 if (i
>= MAX_NBD_REQUESTS
||
383 !s
->requests
[i
].coroutine
||
384 !s
->requests
[i
].receiving
||
385 (nbd_reply_is_structured(&s
->reply
) && !s
->info
.structured_reply
))
387 nbd_channel_error(s
, -EINVAL
);
392 * We're woken up again by the request itself. Note that there
393 * is no race between yielding and reentering connection_co. This
396 * - if the request runs on the same AioContext, it is only
397 * entered after we yield
399 * - if the request runs on a different AioContext, reentering
400 * connection_co happens through a bottom half, which can only
401 * run after we yield.
403 aio_co_wake(s
->requests
[i
].coroutine
);
404 qemu_coroutine_yield();
407 qemu_co_queue_restart_all(&s
->free_sema
);
408 nbd_recv_coroutines_wake_all(s
);
409 bdrv_dec_in_flight(s
->bs
);
411 s
->connection_co
= NULL
;
413 nbd_client_detach_aio_context(s
->bs
);
414 object_unref(OBJECT(s
->sioc
));
416 object_unref(OBJECT(s
->ioc
));
420 if (s
->teardown_co
) {
421 aio_co_wake(s
->teardown_co
);
426 static int nbd_co_send_request(BlockDriverState
*bs
,
430 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
433 qemu_co_mutex_lock(&s
->send_mutex
);
434 while (s
->in_flight
== MAX_NBD_REQUESTS
|| nbd_client_connecting_wait(s
)) {
435 qemu_co_queue_wait(&s
->free_sema
, &s
->send_mutex
);
438 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
445 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
446 if (s
->requests
[i
].coroutine
== NULL
) {
451 g_assert(qemu_in_coroutine());
452 assert(i
< MAX_NBD_REQUESTS
);
454 s
->requests
[i
].coroutine
= qemu_coroutine_self();
455 s
->requests
[i
].offset
= request
->from
;
456 s
->requests
[i
].receiving
= false;
458 request
->handle
= INDEX_TO_HANDLE(s
, i
);
463 qio_channel_set_cork(s
->ioc
, true);
464 rc
= nbd_send_request(s
->ioc
, request
);
465 if (rc
>= 0 && s
->state
== NBD_CLIENT_CONNECTED
) {
466 if (qio_channel_writev_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
470 } else if (rc
>= 0) {
473 qio_channel_set_cork(s
->ioc
, false);
475 rc
= nbd_send_request(s
->ioc
, request
);
480 nbd_channel_error(s
, rc
);
482 s
->requests
[i
].coroutine
= NULL
;
485 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
486 aio_co_wake(s
->connection_co
);
488 qemu_co_queue_next(&s
->free_sema
);
491 qemu_co_mutex_unlock(&s
->send_mutex
);
495 static inline uint16_t payload_advance16(uint8_t **payload
)
498 return lduw_be_p(*payload
- 2);
501 static inline uint32_t payload_advance32(uint8_t **payload
)
504 return ldl_be_p(*payload
- 4);
507 static inline uint64_t payload_advance64(uint8_t **payload
)
510 return ldq_be_p(*payload
- 8);
513 static int nbd_parse_offset_hole_payload(BDRVNBDState
*s
,
514 NBDStructuredReplyChunk
*chunk
,
515 uint8_t *payload
, uint64_t orig_offset
,
516 QEMUIOVector
*qiov
, Error
**errp
)
521 if (chunk
->length
!= sizeof(offset
) + sizeof(hole_size
)) {
522 error_setg(errp
, "Protocol error: invalid payload for "
523 "NBD_REPLY_TYPE_OFFSET_HOLE");
527 offset
= payload_advance64(&payload
);
528 hole_size
= payload_advance32(&payload
);
530 if (!hole_size
|| offset
< orig_offset
|| hole_size
> qiov
->size
||
531 offset
> orig_offset
+ qiov
->size
- hole_size
) {
532 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
536 if (s
->info
.min_block
&&
537 !QEMU_IS_ALIGNED(hole_size
, s
->info
.min_block
)) {
538 trace_nbd_structured_read_compliance("hole");
541 qemu_iovec_memset(qiov
, offset
- orig_offset
, 0, hole_size
);
547 * nbd_parse_blockstatus_payload
548 * Based on our request, we expect only one extent in reply, for the
549 * base:allocation context.
551 static int nbd_parse_blockstatus_payload(BDRVNBDState
*s
,
552 NBDStructuredReplyChunk
*chunk
,
553 uint8_t *payload
, uint64_t orig_length
,
554 NBDExtent
*extent
, Error
**errp
)
558 /* The server succeeded, so it must have sent [at least] one extent */
559 if (chunk
->length
< sizeof(context_id
) + sizeof(*extent
)) {
560 error_setg(errp
, "Protocol error: invalid payload for "
561 "NBD_REPLY_TYPE_BLOCK_STATUS");
565 context_id
= payload_advance32(&payload
);
566 if (s
->info
.context_id
!= context_id
) {
567 error_setg(errp
, "Protocol error: unexpected context id %d for "
568 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
569 "id is %d", context_id
,
574 extent
->length
= payload_advance32(&payload
);
575 extent
->flags
= payload_advance32(&payload
);
577 if (extent
->length
== 0) {
578 error_setg(errp
, "Protocol error: server sent status chunk with "
584 * A server sending unaligned block status is in violation of the
585 * protocol, but as qemu-nbd 3.1 is such a server (at least for
586 * POSIX files that are not a multiple of 512 bytes, since qemu
587 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
588 * still sees an implicit hole beyond the real EOF), it's nicer to
589 * work around the misbehaving server. If the request included
590 * more than the final unaligned block, truncate it back to an
591 * aligned result; if the request was only the final block, round
592 * up to the full block and change the status to fully-allocated
593 * (always a safe status, even if it loses information).
595 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(extent
->length
,
596 s
->info
.min_block
)) {
597 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
598 if (extent
->length
> s
->info
.min_block
) {
599 extent
->length
= QEMU_ALIGN_DOWN(extent
->length
,
602 extent
->length
= s
->info
.min_block
;
608 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
609 * sent us any more than one extent, nor should it have included
610 * status beyond our request in that extent. However, it's easy
611 * enough to ignore the server's noncompliance without killing the
612 * connection; just ignore trailing extents, and clamp things to
613 * the length of our request.
615 if (chunk
->length
> sizeof(context_id
) + sizeof(*extent
)) {
616 trace_nbd_parse_blockstatus_compliance("more than one extent");
618 if (extent
->length
> orig_length
) {
619 extent
->length
= orig_length
;
620 trace_nbd_parse_blockstatus_compliance("extent length too large");
627 * nbd_parse_error_payload
628 * on success @errp contains message describing nbd error reply
630 static int nbd_parse_error_payload(NBDStructuredReplyChunk
*chunk
,
631 uint8_t *payload
, int *request_ret
,
635 uint16_t message_size
;
637 assert(chunk
->type
& (1 << 15));
639 if (chunk
->length
< sizeof(error
) + sizeof(message_size
)) {
641 "Protocol error: invalid payload for structured error");
645 error
= nbd_errno_to_system_errno(payload_advance32(&payload
));
647 error_setg(errp
, "Protocol error: server sent structured error chunk "
652 *request_ret
= -error
;
653 message_size
= payload_advance16(&payload
);
655 if (message_size
> chunk
->length
- sizeof(error
) - sizeof(message_size
)) {
656 error_setg(errp
, "Protocol error: server sent structured error chunk "
657 "with incorrect message size");
661 /* TODO: Add a trace point to mention the server complaint */
663 /* TODO handle ERROR_OFFSET */
668 static int nbd_co_receive_offset_data_payload(BDRVNBDState
*s
,
669 uint64_t orig_offset
,
670 QEMUIOVector
*qiov
, Error
**errp
)
672 QEMUIOVector sub_qiov
;
676 NBDStructuredReplyChunk
*chunk
= &s
->reply
.structured
;
678 assert(nbd_reply_is_structured(&s
->reply
));
680 /* The NBD spec requires at least one byte of payload */
681 if (chunk
->length
<= sizeof(offset
)) {
682 error_setg(errp
, "Protocol error: invalid payload for "
683 "NBD_REPLY_TYPE_OFFSET_DATA");
687 if (nbd_read64(s
->ioc
, &offset
, "OFFSET_DATA offset", errp
) < 0) {
691 data_size
= chunk
->length
- sizeof(offset
);
693 if (offset
< orig_offset
|| data_size
> qiov
->size
||
694 offset
> orig_offset
+ qiov
->size
- data_size
) {
695 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
699 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(data_size
, s
->info
.min_block
)) {
700 trace_nbd_structured_read_compliance("data");
703 qemu_iovec_init(&sub_qiov
, qiov
->niov
);
704 qemu_iovec_concat(&sub_qiov
, qiov
, offset
- orig_offset
, data_size
);
705 ret
= qio_channel_readv_all(s
->ioc
, sub_qiov
.iov
, sub_qiov
.niov
, errp
);
706 qemu_iovec_destroy(&sub_qiov
);
708 return ret
< 0 ? -EIO
: 0;
711 #define NBD_MAX_MALLOC_PAYLOAD 1000
712 static coroutine_fn
int nbd_co_receive_structured_payload(
713 BDRVNBDState
*s
, void **payload
, Error
**errp
)
718 assert(nbd_reply_is_structured(&s
->reply
));
720 len
= s
->reply
.structured
.length
;
726 if (payload
== NULL
) {
727 error_setg(errp
, "Unexpected structured payload");
731 if (len
> NBD_MAX_MALLOC_PAYLOAD
) {
732 error_setg(errp
, "Payload too large");
736 *payload
= g_new(char, len
);
737 ret
= nbd_read(s
->ioc
, *payload
, len
, "structured payload", errp
);
748 * nbd_co_do_receive_one_chunk
750 * set request_ret to received reply error
751 * if qiov is not NULL: read payload to @qiov
752 * for structured reply chunk:
753 * if error chunk: read payload, set @request_ret, do not set @payload
754 * else if offset_data chunk: read payload data to @qiov, do not set @payload
755 * else: read payload to @payload
757 * If function fails, @errp contains corresponding error message, and the
758 * connection with the server is suspect. If it returns 0, then the
759 * transaction succeeded (although @request_ret may be a negative errno
760 * corresponding to the server's error reply), and errp is unchanged.
762 static coroutine_fn
int nbd_co_do_receive_one_chunk(
763 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
764 int *request_ret
, QEMUIOVector
*qiov
, void **payload
, Error
**errp
)
767 int i
= HANDLE_TO_INDEX(s
, handle
);
768 void *local_payload
= NULL
;
769 NBDStructuredReplyChunk
*chunk
;
776 /* Wait until we're woken up by nbd_connection_entry. */
777 s
->requests
[i
].receiving
= true;
778 qemu_coroutine_yield();
779 s
->requests
[i
].receiving
= false;
780 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
781 error_setg(errp
, "Connection closed");
786 assert(s
->reply
.handle
== handle
);
788 if (nbd_reply_is_simple(&s
->reply
)) {
789 if (only_structured
) {
790 error_setg(errp
, "Protocol error: simple reply when structured "
791 "reply chunk was expected");
795 *request_ret
= -nbd_errno_to_system_errno(s
->reply
.simple
.error
);
796 if (*request_ret
< 0 || !qiov
) {
800 return qio_channel_readv_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
801 errp
) < 0 ? -EIO
: 0;
804 /* handle structured reply chunk */
805 assert(s
->info
.structured_reply
);
806 chunk
= &s
->reply
.structured
;
808 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
809 if (!(chunk
->flags
& NBD_REPLY_FLAG_DONE
)) {
810 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
811 " NBD_REPLY_FLAG_DONE flag set");
815 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
822 if (chunk
->type
== NBD_REPLY_TYPE_OFFSET_DATA
) {
824 error_setg(errp
, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
828 return nbd_co_receive_offset_data_payload(s
, s
->requests
[i
].offset
,
832 if (nbd_reply_type_is_error(chunk
->type
)) {
833 payload
= &local_payload
;
836 ret
= nbd_co_receive_structured_payload(s
, payload
, errp
);
841 if (nbd_reply_type_is_error(chunk
->type
)) {
842 ret
= nbd_parse_error_payload(chunk
, local_payload
, request_ret
, errp
);
843 g_free(local_payload
);
851 * nbd_co_receive_one_chunk
852 * Read reply, wake up connection_co and set s->quit if needed.
853 * Return value is a fatal error code or normal nbd reply error code
855 static coroutine_fn
int nbd_co_receive_one_chunk(
856 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
857 int *request_ret
, QEMUIOVector
*qiov
, NBDReply
*reply
, void **payload
,
860 int ret
= nbd_co_do_receive_one_chunk(s
, handle
, only_structured
,
861 request_ret
, qiov
, payload
, errp
);
864 memset(reply
, 0, sizeof(*reply
));
865 nbd_channel_error(s
, ret
);
867 /* For assert at loop start in nbd_connection_entry */
872 if (s
->connection_co
&& !s
->wait_in_flight
) {
874 * We must check s->wait_in_flight, because we may entered by
875 * nbd_recv_coroutines_wake_all(), in this case we should not
876 * wake connection_co here, it will woken by last request.
878 aio_co_wake(s
->connection_co
);
884 typedef struct NBDReplyChunkIter
{
888 bool done
, only_structured
;
891 static void nbd_iter_channel_error(NBDReplyChunkIter
*iter
,
892 int ret
, Error
**local_err
)
894 assert(local_err
&& *local_err
);
899 error_propagate(&iter
->err
, *local_err
);
901 error_free(*local_err
);
907 static void nbd_iter_request_error(NBDReplyChunkIter
*iter
, int ret
)
911 if (!iter
->request_ret
) {
912 iter
->request_ret
= ret
;
917 * NBD_FOREACH_REPLY_CHUNK
918 * The pointer stored in @payload requires g_free() to free it.
920 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
921 qiov, reply, payload) \
922 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
923 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
926 * nbd_reply_chunk_iter_receive
927 * The pointer stored in @payload requires g_free() to free it.
929 static bool nbd_reply_chunk_iter_receive(BDRVNBDState
*s
,
930 NBDReplyChunkIter
*iter
,
932 QEMUIOVector
*qiov
, NBDReply
*reply
,
935 int ret
, request_ret
;
936 NBDReply local_reply
;
937 NBDStructuredReplyChunk
*chunk
;
938 Error
*local_err
= NULL
;
939 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
940 error_setg(&local_err
, "Connection closed");
941 nbd_iter_channel_error(iter
, -EIO
, &local_err
);
946 /* Previous iteration was last. */
951 reply
= &local_reply
;
954 ret
= nbd_co_receive_one_chunk(s
, handle
, iter
->only_structured
,
955 &request_ret
, qiov
, reply
, payload
,
958 nbd_iter_channel_error(iter
, ret
, &local_err
);
959 } else if (request_ret
< 0) {
960 nbd_iter_request_error(iter
, request_ret
);
963 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
964 if (nbd_reply_is_simple(reply
) || s
->state
!= NBD_CLIENT_CONNECTED
) {
968 chunk
= &reply
->structured
;
969 iter
->only_structured
= true;
971 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
972 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
973 assert(chunk
->flags
& NBD_REPLY_FLAG_DONE
);
977 if (chunk
->flags
& NBD_REPLY_FLAG_DONE
) {
978 /* This iteration is last. */
982 /* Execute the loop body */
986 s
->requests
[HANDLE_TO_INDEX(s
, handle
)].coroutine
= NULL
;
988 qemu_co_mutex_lock(&s
->send_mutex
);
990 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
991 aio_co_wake(s
->connection_co
);
993 qemu_co_queue_next(&s
->free_sema
);
995 qemu_co_mutex_unlock(&s
->send_mutex
);
1000 static int nbd_co_receive_return_code(BDRVNBDState
*s
, uint64_t handle
,
1001 int *request_ret
, Error
**errp
)
1003 NBDReplyChunkIter iter
;
1005 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, NULL
, NULL
) {
1006 /* nbd_reply_chunk_iter_receive does all the work */
1009 error_propagate(errp
, iter
.err
);
1010 *request_ret
= iter
.request_ret
;
1014 static int nbd_co_receive_cmdread_reply(BDRVNBDState
*s
, uint64_t handle
,
1015 uint64_t offset
, QEMUIOVector
*qiov
,
1016 int *request_ret
, Error
**errp
)
1018 NBDReplyChunkIter iter
;
1020 void *payload
= NULL
;
1021 Error
*local_err
= NULL
;
1023 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, s
->info
.structured_reply
,
1024 qiov
, &reply
, &payload
)
1027 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1029 assert(nbd_reply_is_structured(&reply
));
1031 switch (chunk
->type
) {
1032 case NBD_REPLY_TYPE_OFFSET_DATA
:
1034 * special cased in nbd_co_receive_one_chunk, data is already
1038 case NBD_REPLY_TYPE_OFFSET_HOLE
:
1039 ret
= nbd_parse_offset_hole_payload(s
, &reply
.structured
, payload
,
1040 offset
, qiov
, &local_err
);
1042 nbd_channel_error(s
, ret
);
1043 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1047 if (!nbd_reply_type_is_error(chunk
->type
)) {
1048 /* not allowed reply type */
1049 nbd_channel_error(s
, -EINVAL
);
1050 error_setg(&local_err
,
1051 "Unexpected reply type: %d (%s) for CMD_READ",
1052 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1053 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1061 error_propagate(errp
, iter
.err
);
1062 *request_ret
= iter
.request_ret
;
1066 static int nbd_co_receive_blockstatus_reply(BDRVNBDState
*s
,
1067 uint64_t handle
, uint64_t length
,
1069 int *request_ret
, Error
**errp
)
1071 NBDReplyChunkIter iter
;
1073 void *payload
= NULL
;
1074 Error
*local_err
= NULL
;
1075 bool received
= false;
1077 assert(!extent
->length
);
1078 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, &reply
, &payload
) {
1080 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1082 assert(nbd_reply_is_structured(&reply
));
1084 switch (chunk
->type
) {
1085 case NBD_REPLY_TYPE_BLOCK_STATUS
:
1087 nbd_channel_error(s
, -EINVAL
);
1088 error_setg(&local_err
, "Several BLOCK_STATUS chunks in reply");
1089 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1093 ret
= nbd_parse_blockstatus_payload(s
, &reply
.structured
,
1094 payload
, length
, extent
,
1097 nbd_channel_error(s
, ret
);
1098 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1102 if (!nbd_reply_type_is_error(chunk
->type
)) {
1103 nbd_channel_error(s
, -EINVAL
);
1104 error_setg(&local_err
,
1105 "Unexpected reply type: %d (%s) "
1106 "for CMD_BLOCK_STATUS",
1107 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1108 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1116 if (!extent
->length
&& !iter
.request_ret
) {
1117 error_setg(&local_err
, "Server did not reply with any status extents");
1118 nbd_iter_channel_error(&iter
, -EIO
, &local_err
);
1121 error_propagate(errp
, iter
.err
);
1122 *request_ret
= iter
.request_ret
;
1126 static int nbd_co_request(BlockDriverState
*bs
, NBDRequest
*request
,
1127 QEMUIOVector
*write_qiov
)
1129 int ret
, request_ret
;
1130 Error
*local_err
= NULL
;
1131 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1133 assert(request
->type
!= NBD_CMD_READ
);
1135 assert(request
->type
== NBD_CMD_WRITE
);
1136 assert(request
->len
== iov_size(write_qiov
->iov
, write_qiov
->niov
));
1138 assert(request
->type
!= NBD_CMD_WRITE
);
1142 ret
= nbd_co_send_request(bs
, request
, write_qiov
);
1147 ret
= nbd_co_receive_return_code(s
, request
->handle
,
1148 &request_ret
, &local_err
);
1150 trace_nbd_co_request_fail(request
->from
, request
->len
,
1151 request
->handle
, request
->flags
,
1153 nbd_cmd_lookup(request
->type
),
1154 ret
, error_get_pretty(local_err
));
1155 error_free(local_err
);
1158 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1160 return ret
? ret
: request_ret
;
1163 static int nbd_client_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
1164 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1166 int ret
, request_ret
;
1167 Error
*local_err
= NULL
;
1168 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1169 NBDRequest request
= {
1170 .type
= NBD_CMD_READ
,
1175 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1182 * Work around the fact that the block layer doesn't do
1183 * byte-accurate sizing yet - if the read exceeds the server's
1184 * advertised size because the block layer rounded size up, then
1185 * truncate the request to the server and tail-pad with zero.
1187 if (offset
>= s
->info
.size
) {
1188 assert(bytes
< BDRV_SECTOR_SIZE
);
1189 qemu_iovec_memset(qiov
, 0, 0, bytes
);
1192 if (offset
+ bytes
> s
->info
.size
) {
1193 uint64_t slop
= offset
+ bytes
- s
->info
.size
;
1195 assert(slop
< BDRV_SECTOR_SIZE
);
1196 qemu_iovec_memset(qiov
, bytes
- slop
, 0, slop
);
1197 request
.len
-= slop
;
1201 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1206 ret
= nbd_co_receive_cmdread_reply(s
, request
.handle
, offset
, qiov
,
1207 &request_ret
, &local_err
);
1209 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1210 request
.flags
, request
.type
,
1211 nbd_cmd_lookup(request
.type
),
1212 ret
, error_get_pretty(local_err
));
1213 error_free(local_err
);
1216 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1218 return ret
? ret
: request_ret
;
1221 static int nbd_client_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1222 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1224 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1225 NBDRequest request
= {
1226 .type
= NBD_CMD_WRITE
,
1231 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1232 if (flags
& BDRV_REQ_FUA
) {
1233 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1234 request
.flags
|= NBD_CMD_FLAG_FUA
;
1237 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1242 return nbd_co_request(bs
, &request
, qiov
);
1245 static int nbd_client_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1246 int bytes
, BdrvRequestFlags flags
)
1248 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1249 NBDRequest request
= {
1250 .type
= NBD_CMD_WRITE_ZEROES
,
1255 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1256 if (!(s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
)) {
1260 if (flags
& BDRV_REQ_FUA
) {
1261 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1262 request
.flags
|= NBD_CMD_FLAG_FUA
;
1264 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1265 request
.flags
|= NBD_CMD_FLAG_NO_HOLE
;
1267 if (flags
& BDRV_REQ_NO_FALLBACK
) {
1268 assert(s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
);
1269 request
.flags
|= NBD_CMD_FLAG_FAST_ZERO
;
1275 return nbd_co_request(bs
, &request
, NULL
);
1278 static int nbd_client_co_flush(BlockDriverState
*bs
)
1280 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1281 NBDRequest request
= { .type
= NBD_CMD_FLUSH
};
1283 if (!(s
->info
.flags
& NBD_FLAG_SEND_FLUSH
)) {
1290 return nbd_co_request(bs
, &request
, NULL
);
1293 static int nbd_client_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
1296 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1297 NBDRequest request
= {
1298 .type
= NBD_CMD_TRIM
,
1303 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1304 if (!(s
->info
.flags
& NBD_FLAG_SEND_TRIM
) || !bytes
) {
1308 return nbd_co_request(bs
, &request
, NULL
);
1311 static int coroutine_fn
nbd_client_co_block_status(
1312 BlockDriverState
*bs
, bool want_zero
, int64_t offset
, int64_t bytes
,
1313 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
1315 int ret
, request_ret
;
1316 NBDExtent extent
= { 0 };
1317 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1318 Error
*local_err
= NULL
;
1320 NBDRequest request
= {
1321 .type
= NBD_CMD_BLOCK_STATUS
,
1323 .len
= MIN(QEMU_ALIGN_DOWN(INT_MAX
, bs
->bl
.request_alignment
),
1324 MIN(bytes
, s
->info
.size
- offset
)),
1325 .flags
= NBD_CMD_FLAG_REQ_ONE
,
1328 if (!s
->info
.base_allocation
) {
1332 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1336 * Work around the fact that the block layer doesn't do
1337 * byte-accurate sizing yet - if the status request exceeds the
1338 * server's advertised size because the block layer rounded size
1339 * up, we truncated the request to the server (above), or are
1340 * called on just the hole.
1342 if (offset
>= s
->info
.size
) {
1344 assert(bytes
< BDRV_SECTOR_SIZE
);
1345 /* Intentionally don't report offset_valid for the hole */
1346 return BDRV_BLOCK_ZERO
;
1349 if (s
->info
.min_block
) {
1350 assert(QEMU_IS_ALIGNED(request
.len
, s
->info
.min_block
));
1353 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1358 ret
= nbd_co_receive_blockstatus_reply(s
, request
.handle
, bytes
,
1359 &extent
, &request_ret
,
1362 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1363 request
.flags
, request
.type
,
1364 nbd_cmd_lookup(request
.type
),
1365 ret
, error_get_pretty(local_err
));
1366 error_free(local_err
);
1369 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1371 if (ret
< 0 || request_ret
< 0) {
1372 return ret
? ret
: request_ret
;
1375 assert(extent
.length
);
1376 *pnum
= extent
.length
;
1379 return (extent
.flags
& NBD_STATE_HOLE
? 0 : BDRV_BLOCK_DATA
) |
1380 (extent
.flags
& NBD_STATE_ZERO
? BDRV_BLOCK_ZERO
: 0) |
1381 BDRV_BLOCK_OFFSET_VALID
;
1384 static int nbd_client_reopen_prepare(BDRVReopenState
*state
,
1385 BlockReopenQueue
*queue
, Error
**errp
)
1387 BDRVNBDState
*s
= (BDRVNBDState
*)state
->bs
->opaque
;
1389 if ((state
->flags
& BDRV_O_RDWR
) && (s
->info
.flags
& NBD_FLAG_READ_ONLY
)) {
1390 error_setg(errp
, "Can't reopen read-only NBD mount as read/write");
1396 static void nbd_client_close(BlockDriverState
*bs
)
1398 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1399 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1402 nbd_send_request(s
->ioc
, &request
);
1405 nbd_teardown_connection(bs
);
1408 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
1411 QIOChannelSocket
*sioc
;
1412 Error
*local_err
= NULL
;
1414 sioc
= qio_channel_socket_new();
1415 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
1417 qio_channel_socket_connect_sync(sioc
, saddr
, &local_err
);
1419 object_unref(OBJECT(sioc
));
1420 error_propagate(errp
, local_err
);
1424 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
1429 static int nbd_client_connect(BlockDriverState
*bs
, Error
**errp
)
1431 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1432 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1436 * establish TCP connection, return error if it fails
1437 * TODO: Configurable retry-until-timeout behaviour.
1439 QIOChannelSocket
*sioc
= nbd_establish_connection(s
->saddr
, errp
);
1442 return -ECONNREFUSED
;
1446 trace_nbd_client_connect(s
->export
);
1447 qio_channel_set_blocking(QIO_CHANNEL(sioc
), false, NULL
);
1448 qio_channel_attach_aio_context(QIO_CHANNEL(sioc
), aio_context
);
1450 s
->info
.request_sizes
= true;
1451 s
->info
.structured_reply
= true;
1452 s
->info
.base_allocation
= true;
1453 s
->info
.x_dirty_bitmap
= g_strdup(s
->x_dirty_bitmap
);
1454 s
->info
.name
= g_strdup(s
->export
?: "");
1455 ret
= nbd_receive_negotiate(aio_context
, QIO_CHANNEL(sioc
), s
->tlscreds
,
1456 s
->hostname
, &s
->ioc
, &s
->info
, errp
);
1457 g_free(s
->info
.x_dirty_bitmap
);
1458 g_free(s
->info
.name
);
1460 object_unref(OBJECT(sioc
));
1463 if (s
->x_dirty_bitmap
&& !s
->info
.base_allocation
) {
1464 error_setg(errp
, "requested x-dirty-bitmap %s not found",
1469 if (s
->info
.flags
& NBD_FLAG_READ_ONLY
) {
1470 ret
= bdrv_apply_auto_read_only(bs
, "NBD export is read-only", errp
);
1475 if (s
->info
.flags
& NBD_FLAG_SEND_FUA
) {
1476 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1477 bs
->supported_zero_flags
|= BDRV_REQ_FUA
;
1479 if (s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
) {
1480 bs
->supported_zero_flags
|= BDRV_REQ_MAY_UNMAP
;
1481 if (s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
) {
1482 bs
->supported_zero_flags
|= BDRV_REQ_NO_FALLBACK
;
1489 s
->ioc
= QIO_CHANNEL(sioc
);
1490 object_ref(OBJECT(s
->ioc
));
1493 trace_nbd_client_connect_success(s
->export
);
1499 * We have connected, but must fail for other reasons.
1500 * Send NBD_CMD_DISC as a courtesy to the server.
1503 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1505 nbd_send_request(s
->ioc
?: QIO_CHANNEL(sioc
), &request
);
1507 object_unref(OBJECT(sioc
));
1514 * Parse nbd_open options
1517 static int nbd_parse_uri(const char *filename
, QDict
*options
)
1521 QueryParams
*qp
= NULL
;
1525 uri
= uri_parse(filename
);
1531 if (!g_strcmp0(uri
->scheme
, "nbd")) {
1533 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
1535 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
1542 p
= uri
->path
? uri
->path
: "";
1547 qdict_put_str(options
, "export", p
);
1550 qp
= query_params_parse(uri
->query
);
1551 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
1557 /* nbd+unix:///export?socket=path */
1558 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
1562 qdict_put_str(options
, "server.type", "unix");
1563 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
1568 /* nbd[+tcp]://host[:port]/export */
1574 /* strip braces from literal IPv6 address */
1575 if (uri
->server
[0] == '[') {
1576 host
= qstring_from_substr(uri
->server
, 1,
1577 strlen(uri
->server
) - 1);
1579 host
= qstring_from_str(uri
->server
);
1582 qdict_put_str(options
, "server.type", "inet");
1583 qdict_put(options
, "server.host", host
);
1585 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
1586 qdict_put_str(options
, "server.port", port_str
);
1592 query_params_free(qp
);
1598 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
1600 const QDictEntry
*e
;
1602 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
1603 if (!strcmp(e
->key
, "host") ||
1604 !strcmp(e
->key
, "port") ||
1605 !strcmp(e
->key
, "path") ||
1606 !strcmp(e
->key
, "export") ||
1607 strstart(e
->key
, "server.", NULL
))
1609 error_setg(errp
, "Option '%s' cannot be used with a file name",
1618 static void nbd_parse_filename(const char *filename
, QDict
*options
,
1621 g_autofree
char *file
= NULL
;
1623 const char *host_spec
;
1624 const char *unixpath
;
1626 if (nbd_has_filename_options_conflict(options
, errp
)) {
1630 if (strstr(filename
, "://")) {
1631 int ret
= nbd_parse_uri(filename
, options
);
1633 error_setg(errp
, "No valid URL specified");
1638 file
= g_strdup(filename
);
1640 export_name
= strstr(file
, EN_OPTSTR
);
1642 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
1645 export_name
[0] = 0; /* truncate 'file' */
1646 export_name
+= strlen(EN_OPTSTR
);
1648 qdict_put_str(options
, "export", export_name
);
1651 /* extract the host_spec - fail if it's not nbd:... */
1652 if (!strstart(file
, "nbd:", &host_spec
)) {
1653 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
1661 /* are we a UNIX or TCP socket? */
1662 if (strstart(host_spec
, "unix:", &unixpath
)) {
1663 qdict_put_str(options
, "server.type", "unix");
1664 qdict_put_str(options
, "server.path", unixpath
);
1666 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
1668 if (inet_parse(addr
, host_spec
, errp
)) {
1672 qdict_put_str(options
, "server.type", "inet");
1673 qdict_put_str(options
, "server.host", addr
->host
);
1674 qdict_put_str(options
, "server.port", addr
->port
);
1676 qapi_free_InetSocketAddress(addr
);
1680 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
1681 QemuOpts
*legacy_opts
,
1684 const char *path
= qemu_opt_get(legacy_opts
, "path");
1685 const char *host
= qemu_opt_get(legacy_opts
, "host");
1686 const char *port
= qemu_opt_get(legacy_opts
, "port");
1687 const QDictEntry
*e
;
1689 if (!path
&& !host
&& !port
) {
1693 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
1695 if (strstart(e
->key
, "server.", NULL
)) {
1696 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
1703 error_setg(errp
, "path and host may not be used at the same time");
1707 error_setg(errp
, "port may not be used without host");
1711 qdict_put_str(output_options
, "server.type", "unix");
1712 qdict_put_str(output_options
, "server.path", path
);
1714 qdict_put_str(output_options
, "server.type", "inet");
1715 qdict_put_str(output_options
, "server.host", host
);
1716 qdict_put_str(output_options
, "server.port",
1717 port
?: stringify(NBD_DEFAULT_PORT
));
1723 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
1726 SocketAddress
*saddr
= NULL
;
1729 Error
*local_err
= NULL
;
1731 qdict_extract_subqdict(options
, &addr
, "server.");
1732 if (!qdict_size(addr
)) {
1733 error_setg(errp
, "NBD server address missing");
1737 iv
= qobject_input_visitor_new_flat_confused(addr
, errp
);
1742 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
1744 error_propagate(errp
, local_err
);
1749 qobject_unref(addr
);
1754 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
1757 QCryptoTLSCreds
*creds
;
1759 obj
= object_resolve_path_component(
1760 object_get_objects_root(), id
);
1762 error_setg(errp
, "No TLS credentials with id '%s'",
1766 creds
= (QCryptoTLSCreds
*)
1767 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
1769 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1774 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
1776 "Expecting TLS credentials with a client endpoint");
1784 static QemuOptsList nbd_runtime_opts
= {
1786 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
1790 .type
= QEMU_OPT_STRING
,
1791 .help
= "TCP host to connect to",
1795 .type
= QEMU_OPT_STRING
,
1796 .help
= "TCP port to connect to",
1800 .type
= QEMU_OPT_STRING
,
1801 .help
= "Unix socket path to connect to",
1805 .type
= QEMU_OPT_STRING
,
1806 .help
= "Name of the NBD export to open",
1809 .name
= "tls-creds",
1810 .type
= QEMU_OPT_STRING
,
1811 .help
= "ID of the TLS credentials to use",
1814 .name
= "x-dirty-bitmap",
1815 .type
= QEMU_OPT_STRING
,
1816 .help
= "experimental: expose named dirty bitmap in place of "
1820 .name
= "reconnect-delay",
1821 .type
= QEMU_OPT_NUMBER
,
1822 .help
= "On an unexpected disconnect, the nbd client tries to "
1823 "connect again until succeeding or encountering a serious "
1824 "error. During the first @reconnect-delay seconds, all "
1825 "requests are paused and will be rerun on a successful "
1826 "reconnect. After that time, any delayed requests and all "
1827 "future requests before a successful reconnect will "
1828 "immediately fail. Default 0",
1830 { /* end of list */ }
1834 static int nbd_process_options(BlockDriverState
*bs
, QDict
*options
,
1837 BDRVNBDState
*s
= bs
->opaque
;
1839 Error
*local_err
= NULL
;
1842 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
1843 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1845 error_propagate(errp
, local_err
);
1849 /* Translate @host, @port, and @path to a SocketAddress */
1850 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
1854 /* Pop the config into our state object. Exit if invalid. */
1855 s
->saddr
= nbd_config(s
, options
, errp
);
1860 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
1861 if (s
->export
&& strlen(s
->export
) > NBD_MAX_STRING_SIZE
) {
1862 error_setg(errp
, "export name too long to send to server");
1866 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
1867 if (s
->tlscredsid
) {
1868 s
->tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
1873 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1874 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
1875 error_setg(errp
, "TLS only supported over IP sockets");
1878 s
->hostname
= s
->saddr
->u
.inet
.host
;
1881 s
->x_dirty_bitmap
= g_strdup(qemu_opt_get(opts
, "x-dirty-bitmap"));
1882 if (s
->x_dirty_bitmap
&& strlen(s
->x_dirty_bitmap
) > NBD_MAX_STRING_SIZE
) {
1883 error_setg(errp
, "x-dirty-bitmap query too long to send to server");
1887 s
->reconnect_delay
= qemu_opt_get_number(opts
, "reconnect-delay", 0);
1893 nbd_clear_bdrvstate(s
);
1895 qemu_opts_del(opts
);
1899 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1903 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1905 ret
= nbd_process_options(bs
, options
, errp
);
1911 qemu_co_mutex_init(&s
->send_mutex
);
1912 qemu_co_queue_init(&s
->free_sema
);
1914 ret
= nbd_client_connect(bs
, errp
);
1916 nbd_clear_bdrvstate(s
);
1919 /* successfully connected */
1920 s
->state
= NBD_CLIENT_CONNECTED
;
1922 s
->connection_co
= qemu_coroutine_create(nbd_connection_entry
, s
);
1923 bdrv_inc_in_flight(bs
);
1924 aio_co_schedule(bdrv_get_aio_context(bs
), s
->connection_co
);
1929 static int nbd_co_flush(BlockDriverState
*bs
)
1931 return nbd_client_co_flush(bs
);
1934 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1936 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1937 uint32_t min
= s
->info
.min_block
;
1938 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
1941 * If the server did not advertise an alignment:
1942 * - a size that is not sector-aligned implies that an alignment
1943 * of 1 can be used to access those tail bytes
1944 * - advertisement of block status requires an alignment of 1, so
1945 * that we don't violate block layer constraints that block
1946 * status is always aligned (as we can't control whether the
1947 * server will report sub-sector extents, such as a hole at EOF
1948 * on an unaligned POSIX file)
1949 * - otherwise, assume the server is so old that we are safer avoiding
1950 * sub-sector requests
1953 min
= (!QEMU_IS_ALIGNED(s
->info
.size
, BDRV_SECTOR_SIZE
) ||
1954 s
->info
.base_allocation
) ? 1 : BDRV_SECTOR_SIZE
;
1957 bs
->bl
.request_alignment
= min
;
1958 bs
->bl
.max_pdiscard
= QEMU_ALIGN_DOWN(INT_MAX
, min
);
1959 bs
->bl
.max_pwrite_zeroes
= max
;
1960 bs
->bl
.max_transfer
= max
;
1962 if (s
->info
.opt_block
&&
1963 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
1964 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
1968 static void nbd_close(BlockDriverState
*bs
)
1970 BDRVNBDState
*s
= bs
->opaque
;
1972 nbd_client_close(bs
);
1973 nbd_clear_bdrvstate(s
);
1976 static int64_t nbd_getlength(BlockDriverState
*bs
)
1978 BDRVNBDState
*s
= bs
->opaque
;
1980 return s
->info
.size
;
1983 static void nbd_refresh_filename(BlockDriverState
*bs
)
1985 BDRVNBDState
*s
= bs
->opaque
;
1986 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
1989 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
1990 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
1991 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
1995 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1996 path
= s
->saddr
->u
.q_unix
.path
;
1997 } /* else can't represent as pseudo-filename */
1999 if (path
&& s
->export
) {
2000 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2001 "nbd+unix:///%s?socket=%s", s
->export
, path
);
2002 } else if (path
&& !s
->export
) {
2003 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2004 "nbd+unix://?socket=%s", path
);
2005 } else if (host
&& s
->export
) {
2006 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2007 "nbd://%s:%s/%s", host
, port
, s
->export
);
2008 } else if (host
&& !s
->export
) {
2009 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2010 "nbd://%s:%s", host
, port
);
2012 if (len
> sizeof(bs
->exact_filename
)) {
2013 /* Name is too long to represent exactly, so leave it empty. */
2014 bs
->exact_filename
[0] = '\0';
2018 static char *nbd_dirname(BlockDriverState
*bs
, Error
**errp
)
2020 /* The generic bdrv_dirname() implementation is able to work out some
2021 * directory name for NBD nodes, but that would be wrong. So far there is no
2022 * specification for how "export paths" would work, so NBD does not have
2023 * directory names. */
2024 error_setg(errp
, "Cannot generate a base directory for NBD nodes");
2028 static const char *const nbd_strong_runtime_opts
[] = {
2039 static BlockDriver bdrv_nbd
= {
2040 .format_name
= "nbd",
2041 .protocol_name
= "nbd",
2042 .instance_size
= sizeof(BDRVNBDState
),
2043 .bdrv_parse_filename
= nbd_parse_filename
,
2044 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2045 .create_opts
= &bdrv_create_opts_simple
,
2046 .bdrv_file_open
= nbd_open
,
2047 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2048 .bdrv_co_preadv
= nbd_client_co_preadv
,
2049 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2050 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2051 .bdrv_close
= nbd_close
,
2052 .bdrv_co_flush_to_os
= nbd_co_flush
,
2053 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2054 .bdrv_refresh_limits
= nbd_refresh_limits
,
2055 .bdrv_getlength
= nbd_getlength
,
2056 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2057 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2058 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2059 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2060 .bdrv_refresh_filename
= nbd_refresh_filename
,
2061 .bdrv_co_block_status
= nbd_client_co_block_status
,
2062 .bdrv_dirname
= nbd_dirname
,
2063 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2066 static BlockDriver bdrv_nbd_tcp
= {
2067 .format_name
= "nbd",
2068 .protocol_name
= "nbd+tcp",
2069 .instance_size
= sizeof(BDRVNBDState
),
2070 .bdrv_parse_filename
= nbd_parse_filename
,
2071 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2072 .create_opts
= &bdrv_create_opts_simple
,
2073 .bdrv_file_open
= nbd_open
,
2074 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2075 .bdrv_co_preadv
= nbd_client_co_preadv
,
2076 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2077 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2078 .bdrv_close
= nbd_close
,
2079 .bdrv_co_flush_to_os
= nbd_co_flush
,
2080 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2081 .bdrv_refresh_limits
= nbd_refresh_limits
,
2082 .bdrv_getlength
= nbd_getlength
,
2083 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2084 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2085 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2086 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2087 .bdrv_refresh_filename
= nbd_refresh_filename
,
2088 .bdrv_co_block_status
= nbd_client_co_block_status
,
2089 .bdrv_dirname
= nbd_dirname
,
2090 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2093 static BlockDriver bdrv_nbd_unix
= {
2094 .format_name
= "nbd",
2095 .protocol_name
= "nbd+unix",
2096 .instance_size
= sizeof(BDRVNBDState
),
2097 .bdrv_parse_filename
= nbd_parse_filename
,
2098 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2099 .create_opts
= &bdrv_create_opts_simple
,
2100 .bdrv_file_open
= nbd_open
,
2101 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2102 .bdrv_co_preadv
= nbd_client_co_preadv
,
2103 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2104 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2105 .bdrv_close
= nbd_close
,
2106 .bdrv_co_flush_to_os
= nbd_co_flush
,
2107 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2108 .bdrv_refresh_limits
= nbd_refresh_limits
,
2109 .bdrv_getlength
= nbd_getlength
,
2110 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2111 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2112 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2113 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2114 .bdrv_refresh_filename
= nbd_refresh_filename
,
2115 .bdrv_co_block_status
= nbd_client_co_block_status
,
2116 .bdrv_dirname
= nbd_dirname
,
2117 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2120 static void bdrv_nbd_init(void)
2122 bdrv_register(&bdrv_nbd
);
2123 bdrv_register(&bdrv_nbd_tcp
);
2124 bdrv_register(&bdrv_nbd_unix
);
2127 block_init(bdrv_nbd_init
);