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 QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
98 static int nbd_client_handshake(BlockDriverState
*bs
, QIOChannelSocket
*sioc
,
101 static void nbd_clear_bdrvstate(BDRVNBDState
*s
)
103 object_unref(OBJECT(s
->tlscreds
));
104 qapi_free_SocketAddress(s
->saddr
);
108 g_free(s
->tlscredsid
);
109 s
->tlscredsid
= NULL
;
110 g_free(s
->x_dirty_bitmap
);
111 s
->x_dirty_bitmap
= NULL
;
114 static void nbd_channel_error(BDRVNBDState
*s
, int ret
)
117 if (s
->state
== NBD_CLIENT_CONNECTED
) {
118 s
->state
= s
->reconnect_delay
? NBD_CLIENT_CONNECTING_WAIT
:
119 NBD_CLIENT_CONNECTING_NOWAIT
;
122 if (s
->state
== NBD_CLIENT_CONNECTED
) {
123 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
125 s
->state
= NBD_CLIENT_QUIT
;
129 static void nbd_recv_coroutines_wake_all(BDRVNBDState
*s
)
133 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
134 NBDClientRequest
*req
= &s
->requests
[i
];
136 if (req
->coroutine
&& req
->receiving
) {
137 aio_co_wake(req
->coroutine
);
142 static void nbd_client_detach_aio_context(BlockDriverState
*bs
)
144 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
146 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
149 static void nbd_client_attach_aio_context_bh(void *opaque
)
151 BlockDriverState
*bs
= opaque
;
152 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
155 * The node is still drained, so we know the coroutine has yielded in
156 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
157 * entered for the first time. Both places are safe for entering the
160 qemu_aio_coroutine_enter(bs
->aio_context
, s
->connection_co
);
161 bdrv_dec_in_flight(bs
);
164 static void nbd_client_attach_aio_context(BlockDriverState
*bs
,
165 AioContext
*new_context
)
167 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
170 * s->connection_co is either yielded from nbd_receive_reply or from
171 * nbd_co_reconnect_loop()
173 if (s
->state
== NBD_CLIENT_CONNECTED
) {
174 qio_channel_attach_aio_context(QIO_CHANNEL(s
->ioc
), new_context
);
177 bdrv_inc_in_flight(bs
);
180 * Need to wait here for the BH to run because the BH must run while the
181 * node is still drained.
183 aio_wait_bh_oneshot(new_context
, nbd_client_attach_aio_context_bh
, bs
);
186 static void coroutine_fn
nbd_client_co_drain_begin(BlockDriverState
*bs
)
188 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
191 if (s
->connection_co_sleep_ns_state
) {
192 qemu_co_sleep_wake(s
->connection_co_sleep_ns_state
);
196 static void coroutine_fn
nbd_client_co_drain_end(BlockDriverState
*bs
)
198 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
201 if (s
->wait_drained_end
) {
202 s
->wait_drained_end
= false;
203 aio_co_wake(s
->connection_co
);
208 static void nbd_teardown_connection(BlockDriverState
*bs
)
210 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
213 /* finish any pending coroutines */
214 qio_channel_shutdown(s
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
, NULL
);
215 } else if (s
->sioc
) {
216 /* abort negotiation */
217 qio_channel_shutdown(QIO_CHANNEL(s
->sioc
), QIO_CHANNEL_SHUTDOWN_BOTH
,
221 s
->state
= NBD_CLIENT_QUIT
;
222 if (s
->connection_co
) {
223 if (s
->connection_co_sleep_ns_state
) {
224 qemu_co_sleep_wake(s
->connection_co_sleep_ns_state
);
227 if (qemu_in_coroutine()) {
228 s
->teardown_co
= qemu_coroutine_self();
229 /* connection_co resumes us when it terminates */
230 qemu_coroutine_yield();
231 s
->teardown_co
= NULL
;
233 BDRV_POLL_WHILE(bs
, s
->connection_co
);
235 assert(!s
->connection_co
);
238 static bool nbd_client_connecting(BDRVNBDState
*s
)
240 return s
->state
== NBD_CLIENT_CONNECTING_WAIT
||
241 s
->state
== NBD_CLIENT_CONNECTING_NOWAIT
;
244 static bool nbd_client_connecting_wait(BDRVNBDState
*s
)
246 return s
->state
== NBD_CLIENT_CONNECTING_WAIT
;
249 static coroutine_fn
void nbd_reconnect_attempt(BDRVNBDState
*s
)
252 Error
*local_err
= NULL
;
253 QIOChannelSocket
*sioc
;
255 if (!nbd_client_connecting(s
)) {
259 /* Wait for completion of all in-flight requests */
261 qemu_co_mutex_lock(&s
->send_mutex
);
263 while (s
->in_flight
> 0) {
264 qemu_co_mutex_unlock(&s
->send_mutex
);
265 nbd_recv_coroutines_wake_all(s
);
266 s
->wait_in_flight
= true;
267 qemu_coroutine_yield();
268 s
->wait_in_flight
= false;
269 qemu_co_mutex_lock(&s
->send_mutex
);
272 qemu_co_mutex_unlock(&s
->send_mutex
);
274 if (!nbd_client_connecting(s
)) {
279 * Now we are sure that nobody is accessing the channel, and no one will
280 * try until we set the state to CONNECTED.
283 /* Finalize previous connection if any */
285 nbd_client_detach_aio_context(s
->bs
);
286 object_unref(OBJECT(s
->sioc
));
288 object_unref(OBJECT(s
->ioc
));
292 sioc
= nbd_establish_connection(s
->saddr
, &local_err
);
298 bdrv_dec_in_flight(s
->bs
);
300 ret
= nbd_client_handshake(s
->bs
, sioc
, &local_err
);
303 s
->wait_drained_end
= true;
306 * We may be entered once from nbd_client_attach_aio_context_bh
307 * and then from nbd_client_co_drain_end. So here is a loop.
309 qemu_coroutine_yield();
312 bdrv_inc_in_flight(s
->bs
);
315 s
->connect_status
= ret
;
316 error_free(s
->connect_err
);
317 s
->connect_err
= NULL
;
318 error_propagate(&s
->connect_err
, local_err
);
321 /* successfully connected */
322 s
->state
= NBD_CLIENT_CONNECTED
;
323 qemu_co_queue_restart_all(&s
->free_sema
);
327 static coroutine_fn
void nbd_co_reconnect_loop(BDRVNBDState
*s
)
329 uint64_t start_time_ns
= qemu_clock_get_ns(QEMU_CLOCK_REALTIME
);
330 uint64_t delay_ns
= s
->reconnect_delay
* NANOSECONDS_PER_SECOND
;
331 uint64_t timeout
= 1 * NANOSECONDS_PER_SECOND
;
332 uint64_t max_timeout
= 16 * NANOSECONDS_PER_SECOND
;
334 nbd_reconnect_attempt(s
);
336 while (nbd_client_connecting(s
)) {
337 if (s
->state
== NBD_CLIENT_CONNECTING_WAIT
&&
338 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) - start_time_ns
> delay_ns
)
340 s
->state
= NBD_CLIENT_CONNECTING_NOWAIT
;
341 qemu_co_queue_restart_all(&s
->free_sema
);
345 bdrv_dec_in_flight(s
->bs
);
346 s
->wait_drained_end
= true;
349 * We may be entered once from nbd_client_attach_aio_context_bh
350 * and then from nbd_client_co_drain_end. So here is a loop.
352 qemu_coroutine_yield();
354 bdrv_inc_in_flight(s
->bs
);
356 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME
, timeout
,
357 &s
->connection_co_sleep_ns_state
);
358 if (timeout
< max_timeout
) {
363 nbd_reconnect_attempt(s
);
367 static coroutine_fn
void nbd_connection_entry(void *opaque
)
369 BDRVNBDState
*s
= opaque
;
372 Error
*local_err
= NULL
;
374 while (s
->state
!= NBD_CLIENT_QUIT
) {
376 * The NBD client can only really be considered idle when it has
377 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
378 * the point where the additional scheduled coroutine entry happens
379 * after nbd_client_attach_aio_context().
381 * Therefore we keep an additional in_flight reference all the time and
382 * only drop it temporarily here.
385 if (nbd_client_connecting(s
)) {
386 nbd_co_reconnect_loop(s
);
389 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
393 assert(s
->reply
.handle
== 0);
394 ret
= nbd_receive_reply(s
->bs
, s
->ioc
, &s
->reply
, &local_err
);
397 trace_nbd_read_reply_entry_fail(ret
, error_get_pretty(local_err
));
398 error_free(local_err
);
402 nbd_channel_error(s
, ret
? ret
: -EIO
);
407 * There's no need for a mutex on the receive side, because the
408 * handler acts as a synchronization point and ensures that only
409 * one coroutine is called until the reply finishes.
411 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
412 if (i
>= MAX_NBD_REQUESTS
||
413 !s
->requests
[i
].coroutine
||
414 !s
->requests
[i
].receiving
||
415 (nbd_reply_is_structured(&s
->reply
) && !s
->info
.structured_reply
))
417 nbd_channel_error(s
, -EINVAL
);
422 * We're woken up again by the request itself. Note that there
423 * is no race between yielding and reentering connection_co. This
426 * - if the request runs on the same AioContext, it is only
427 * entered after we yield
429 * - if the request runs on a different AioContext, reentering
430 * connection_co happens through a bottom half, which can only
431 * run after we yield.
433 aio_co_wake(s
->requests
[i
].coroutine
);
434 qemu_coroutine_yield();
437 qemu_co_queue_restart_all(&s
->free_sema
);
438 nbd_recv_coroutines_wake_all(s
);
439 bdrv_dec_in_flight(s
->bs
);
441 s
->connection_co
= NULL
;
443 nbd_client_detach_aio_context(s
->bs
);
444 object_unref(OBJECT(s
->sioc
));
446 object_unref(OBJECT(s
->ioc
));
450 if (s
->teardown_co
) {
451 aio_co_wake(s
->teardown_co
);
456 static int nbd_co_send_request(BlockDriverState
*bs
,
460 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
463 qemu_co_mutex_lock(&s
->send_mutex
);
464 while (s
->in_flight
== MAX_NBD_REQUESTS
|| nbd_client_connecting_wait(s
)) {
465 qemu_co_queue_wait(&s
->free_sema
, &s
->send_mutex
);
468 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
475 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
476 if (s
->requests
[i
].coroutine
== NULL
) {
481 g_assert(qemu_in_coroutine());
482 assert(i
< MAX_NBD_REQUESTS
);
484 s
->requests
[i
].coroutine
= qemu_coroutine_self();
485 s
->requests
[i
].offset
= request
->from
;
486 s
->requests
[i
].receiving
= false;
488 request
->handle
= INDEX_TO_HANDLE(s
, i
);
493 qio_channel_set_cork(s
->ioc
, true);
494 rc
= nbd_send_request(s
->ioc
, request
);
495 if (rc
>= 0 && s
->state
== NBD_CLIENT_CONNECTED
) {
496 if (qio_channel_writev_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
500 } else if (rc
>= 0) {
503 qio_channel_set_cork(s
->ioc
, false);
505 rc
= nbd_send_request(s
->ioc
, request
);
510 nbd_channel_error(s
, rc
);
512 s
->requests
[i
].coroutine
= NULL
;
515 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
516 aio_co_wake(s
->connection_co
);
518 qemu_co_queue_next(&s
->free_sema
);
521 qemu_co_mutex_unlock(&s
->send_mutex
);
525 static inline uint16_t payload_advance16(uint8_t **payload
)
528 return lduw_be_p(*payload
- 2);
531 static inline uint32_t payload_advance32(uint8_t **payload
)
534 return ldl_be_p(*payload
- 4);
537 static inline uint64_t payload_advance64(uint8_t **payload
)
540 return ldq_be_p(*payload
- 8);
543 static int nbd_parse_offset_hole_payload(BDRVNBDState
*s
,
544 NBDStructuredReplyChunk
*chunk
,
545 uint8_t *payload
, uint64_t orig_offset
,
546 QEMUIOVector
*qiov
, Error
**errp
)
551 if (chunk
->length
!= sizeof(offset
) + sizeof(hole_size
)) {
552 error_setg(errp
, "Protocol error: invalid payload for "
553 "NBD_REPLY_TYPE_OFFSET_HOLE");
557 offset
= payload_advance64(&payload
);
558 hole_size
= payload_advance32(&payload
);
560 if (!hole_size
|| offset
< orig_offset
|| hole_size
> qiov
->size
||
561 offset
> orig_offset
+ qiov
->size
- hole_size
) {
562 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
566 if (s
->info
.min_block
&&
567 !QEMU_IS_ALIGNED(hole_size
, s
->info
.min_block
)) {
568 trace_nbd_structured_read_compliance("hole");
571 qemu_iovec_memset(qiov
, offset
- orig_offset
, 0, hole_size
);
577 * nbd_parse_blockstatus_payload
578 * Based on our request, we expect only one extent in reply, for the
579 * base:allocation context.
581 static int nbd_parse_blockstatus_payload(BDRVNBDState
*s
,
582 NBDStructuredReplyChunk
*chunk
,
583 uint8_t *payload
, uint64_t orig_length
,
584 NBDExtent
*extent
, Error
**errp
)
588 /* The server succeeded, so it must have sent [at least] one extent */
589 if (chunk
->length
< sizeof(context_id
) + sizeof(*extent
)) {
590 error_setg(errp
, "Protocol error: invalid payload for "
591 "NBD_REPLY_TYPE_BLOCK_STATUS");
595 context_id
= payload_advance32(&payload
);
596 if (s
->info
.context_id
!= context_id
) {
597 error_setg(errp
, "Protocol error: unexpected context id %d for "
598 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
599 "id is %d", context_id
,
604 extent
->length
= payload_advance32(&payload
);
605 extent
->flags
= payload_advance32(&payload
);
607 if (extent
->length
== 0) {
608 error_setg(errp
, "Protocol error: server sent status chunk with "
614 * A server sending unaligned block status is in violation of the
615 * protocol, but as qemu-nbd 3.1 is such a server (at least for
616 * POSIX files that are not a multiple of 512 bytes, since qemu
617 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
618 * still sees an implicit hole beyond the real EOF), it's nicer to
619 * work around the misbehaving server. If the request included
620 * more than the final unaligned block, truncate it back to an
621 * aligned result; if the request was only the final block, round
622 * up to the full block and change the status to fully-allocated
623 * (always a safe status, even if it loses information).
625 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(extent
->length
,
626 s
->info
.min_block
)) {
627 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
628 if (extent
->length
> s
->info
.min_block
) {
629 extent
->length
= QEMU_ALIGN_DOWN(extent
->length
,
632 extent
->length
= s
->info
.min_block
;
638 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
639 * sent us any more than one extent, nor should it have included
640 * status beyond our request in that extent. However, it's easy
641 * enough to ignore the server's noncompliance without killing the
642 * connection; just ignore trailing extents, and clamp things to
643 * the length of our request.
645 if (chunk
->length
> sizeof(context_id
) + sizeof(*extent
)) {
646 trace_nbd_parse_blockstatus_compliance("more than one extent");
648 if (extent
->length
> orig_length
) {
649 extent
->length
= orig_length
;
650 trace_nbd_parse_blockstatus_compliance("extent length too large");
657 * nbd_parse_error_payload
658 * on success @errp contains message describing nbd error reply
660 static int nbd_parse_error_payload(NBDStructuredReplyChunk
*chunk
,
661 uint8_t *payload
, int *request_ret
,
665 uint16_t message_size
;
667 assert(chunk
->type
& (1 << 15));
669 if (chunk
->length
< sizeof(error
) + sizeof(message_size
)) {
671 "Protocol error: invalid payload for structured error");
675 error
= nbd_errno_to_system_errno(payload_advance32(&payload
));
677 error_setg(errp
, "Protocol error: server sent structured error chunk "
682 *request_ret
= -error
;
683 message_size
= payload_advance16(&payload
);
685 if (message_size
> chunk
->length
- sizeof(error
) - sizeof(message_size
)) {
686 error_setg(errp
, "Protocol error: server sent structured error chunk "
687 "with incorrect message size");
691 /* TODO: Add a trace point to mention the server complaint */
693 /* TODO handle ERROR_OFFSET */
698 static int nbd_co_receive_offset_data_payload(BDRVNBDState
*s
,
699 uint64_t orig_offset
,
700 QEMUIOVector
*qiov
, Error
**errp
)
702 QEMUIOVector sub_qiov
;
706 NBDStructuredReplyChunk
*chunk
= &s
->reply
.structured
;
708 assert(nbd_reply_is_structured(&s
->reply
));
710 /* The NBD spec requires at least one byte of payload */
711 if (chunk
->length
<= sizeof(offset
)) {
712 error_setg(errp
, "Protocol error: invalid payload for "
713 "NBD_REPLY_TYPE_OFFSET_DATA");
717 if (nbd_read64(s
->ioc
, &offset
, "OFFSET_DATA offset", errp
) < 0) {
721 data_size
= chunk
->length
- sizeof(offset
);
723 if (offset
< orig_offset
|| data_size
> qiov
->size
||
724 offset
> orig_offset
+ qiov
->size
- data_size
) {
725 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
729 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(data_size
, s
->info
.min_block
)) {
730 trace_nbd_structured_read_compliance("data");
733 qemu_iovec_init(&sub_qiov
, qiov
->niov
);
734 qemu_iovec_concat(&sub_qiov
, qiov
, offset
- orig_offset
, data_size
);
735 ret
= qio_channel_readv_all(s
->ioc
, sub_qiov
.iov
, sub_qiov
.niov
, errp
);
736 qemu_iovec_destroy(&sub_qiov
);
738 return ret
< 0 ? -EIO
: 0;
741 #define NBD_MAX_MALLOC_PAYLOAD 1000
742 static coroutine_fn
int nbd_co_receive_structured_payload(
743 BDRVNBDState
*s
, void **payload
, Error
**errp
)
748 assert(nbd_reply_is_structured(&s
->reply
));
750 len
= s
->reply
.structured
.length
;
756 if (payload
== NULL
) {
757 error_setg(errp
, "Unexpected structured payload");
761 if (len
> NBD_MAX_MALLOC_PAYLOAD
) {
762 error_setg(errp
, "Payload too large");
766 *payload
= g_new(char, len
);
767 ret
= nbd_read(s
->ioc
, *payload
, len
, "structured payload", errp
);
778 * nbd_co_do_receive_one_chunk
780 * set request_ret to received reply error
781 * if qiov is not NULL: read payload to @qiov
782 * for structured reply chunk:
783 * if error chunk: read payload, set @request_ret, do not set @payload
784 * else if offset_data chunk: read payload data to @qiov, do not set @payload
785 * else: read payload to @payload
787 * If function fails, @errp contains corresponding error message, and the
788 * connection with the server is suspect. If it returns 0, then the
789 * transaction succeeded (although @request_ret may be a negative errno
790 * corresponding to the server's error reply), and errp is unchanged.
792 static coroutine_fn
int nbd_co_do_receive_one_chunk(
793 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
794 int *request_ret
, QEMUIOVector
*qiov
, void **payload
, Error
**errp
)
797 int i
= HANDLE_TO_INDEX(s
, handle
);
798 void *local_payload
= NULL
;
799 NBDStructuredReplyChunk
*chunk
;
806 /* Wait until we're woken up by nbd_connection_entry. */
807 s
->requests
[i
].receiving
= true;
808 qemu_coroutine_yield();
809 s
->requests
[i
].receiving
= false;
810 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
811 error_setg(errp
, "Connection closed");
816 assert(s
->reply
.handle
== handle
);
818 if (nbd_reply_is_simple(&s
->reply
)) {
819 if (only_structured
) {
820 error_setg(errp
, "Protocol error: simple reply when structured "
821 "reply chunk was expected");
825 *request_ret
= -nbd_errno_to_system_errno(s
->reply
.simple
.error
);
826 if (*request_ret
< 0 || !qiov
) {
830 return qio_channel_readv_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
831 errp
) < 0 ? -EIO
: 0;
834 /* handle structured reply chunk */
835 assert(s
->info
.structured_reply
);
836 chunk
= &s
->reply
.structured
;
838 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
839 if (!(chunk
->flags
& NBD_REPLY_FLAG_DONE
)) {
840 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
841 " NBD_REPLY_FLAG_DONE flag set");
845 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
852 if (chunk
->type
== NBD_REPLY_TYPE_OFFSET_DATA
) {
854 error_setg(errp
, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
858 return nbd_co_receive_offset_data_payload(s
, s
->requests
[i
].offset
,
862 if (nbd_reply_type_is_error(chunk
->type
)) {
863 payload
= &local_payload
;
866 ret
= nbd_co_receive_structured_payload(s
, payload
, errp
);
871 if (nbd_reply_type_is_error(chunk
->type
)) {
872 ret
= nbd_parse_error_payload(chunk
, local_payload
, request_ret
, errp
);
873 g_free(local_payload
);
881 * nbd_co_receive_one_chunk
882 * Read reply, wake up connection_co and set s->quit if needed.
883 * Return value is a fatal error code or normal nbd reply error code
885 static coroutine_fn
int nbd_co_receive_one_chunk(
886 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
887 int *request_ret
, QEMUIOVector
*qiov
, NBDReply
*reply
, void **payload
,
890 int ret
= nbd_co_do_receive_one_chunk(s
, handle
, only_structured
,
891 request_ret
, qiov
, payload
, errp
);
894 memset(reply
, 0, sizeof(*reply
));
895 nbd_channel_error(s
, ret
);
897 /* For assert at loop start in nbd_connection_entry */
902 if (s
->connection_co
&& !s
->wait_in_flight
) {
904 * We must check s->wait_in_flight, because we may entered by
905 * nbd_recv_coroutines_wake_all(), in this case we should not
906 * wake connection_co here, it will woken by last request.
908 aio_co_wake(s
->connection_co
);
914 typedef struct NBDReplyChunkIter
{
918 bool done
, only_structured
;
921 static void nbd_iter_channel_error(NBDReplyChunkIter
*iter
,
922 int ret
, Error
**local_err
)
924 assert(local_err
&& *local_err
);
929 error_propagate(&iter
->err
, *local_err
);
931 error_free(*local_err
);
937 static void nbd_iter_request_error(NBDReplyChunkIter
*iter
, int ret
)
941 if (!iter
->request_ret
) {
942 iter
->request_ret
= ret
;
947 * NBD_FOREACH_REPLY_CHUNK
948 * The pointer stored in @payload requires g_free() to free it.
950 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
951 qiov, reply, payload) \
952 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
953 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
956 * nbd_reply_chunk_iter_receive
957 * The pointer stored in @payload requires g_free() to free it.
959 static bool nbd_reply_chunk_iter_receive(BDRVNBDState
*s
,
960 NBDReplyChunkIter
*iter
,
962 QEMUIOVector
*qiov
, NBDReply
*reply
,
965 int ret
, request_ret
;
966 NBDReply local_reply
;
967 NBDStructuredReplyChunk
*chunk
;
968 Error
*local_err
= NULL
;
969 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
970 error_setg(&local_err
, "Connection closed");
971 nbd_iter_channel_error(iter
, -EIO
, &local_err
);
976 /* Previous iteration was last. */
981 reply
= &local_reply
;
984 ret
= nbd_co_receive_one_chunk(s
, handle
, iter
->only_structured
,
985 &request_ret
, qiov
, reply
, payload
,
988 nbd_iter_channel_error(iter
, ret
, &local_err
);
989 } else if (request_ret
< 0) {
990 nbd_iter_request_error(iter
, request_ret
);
993 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
994 if (nbd_reply_is_simple(reply
) || s
->state
!= NBD_CLIENT_CONNECTED
) {
998 chunk
= &reply
->structured
;
999 iter
->only_structured
= true;
1001 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
1002 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1003 assert(chunk
->flags
& NBD_REPLY_FLAG_DONE
);
1007 if (chunk
->flags
& NBD_REPLY_FLAG_DONE
) {
1008 /* This iteration is last. */
1012 /* Execute the loop body */
1016 s
->requests
[HANDLE_TO_INDEX(s
, handle
)].coroutine
= NULL
;
1018 qemu_co_mutex_lock(&s
->send_mutex
);
1020 if (s
->in_flight
== 0 && s
->wait_in_flight
) {
1021 aio_co_wake(s
->connection_co
);
1023 qemu_co_queue_next(&s
->free_sema
);
1025 qemu_co_mutex_unlock(&s
->send_mutex
);
1030 static int nbd_co_receive_return_code(BDRVNBDState
*s
, uint64_t handle
,
1031 int *request_ret
, Error
**errp
)
1033 NBDReplyChunkIter iter
;
1035 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, NULL
, NULL
) {
1036 /* nbd_reply_chunk_iter_receive does all the work */
1039 error_propagate(errp
, iter
.err
);
1040 *request_ret
= iter
.request_ret
;
1044 static int nbd_co_receive_cmdread_reply(BDRVNBDState
*s
, uint64_t handle
,
1045 uint64_t offset
, QEMUIOVector
*qiov
,
1046 int *request_ret
, Error
**errp
)
1048 NBDReplyChunkIter iter
;
1050 void *payload
= NULL
;
1051 Error
*local_err
= NULL
;
1053 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, s
->info
.structured_reply
,
1054 qiov
, &reply
, &payload
)
1057 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1059 assert(nbd_reply_is_structured(&reply
));
1061 switch (chunk
->type
) {
1062 case NBD_REPLY_TYPE_OFFSET_DATA
:
1064 * special cased in nbd_co_receive_one_chunk, data is already
1068 case NBD_REPLY_TYPE_OFFSET_HOLE
:
1069 ret
= nbd_parse_offset_hole_payload(s
, &reply
.structured
, payload
,
1070 offset
, qiov
, &local_err
);
1072 nbd_channel_error(s
, ret
);
1073 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1077 if (!nbd_reply_type_is_error(chunk
->type
)) {
1078 /* not allowed reply type */
1079 nbd_channel_error(s
, -EINVAL
);
1080 error_setg(&local_err
,
1081 "Unexpected reply type: %d (%s) for CMD_READ",
1082 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1083 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1091 error_propagate(errp
, iter
.err
);
1092 *request_ret
= iter
.request_ret
;
1096 static int nbd_co_receive_blockstatus_reply(BDRVNBDState
*s
,
1097 uint64_t handle
, uint64_t length
,
1099 int *request_ret
, Error
**errp
)
1101 NBDReplyChunkIter iter
;
1103 void *payload
= NULL
;
1104 Error
*local_err
= NULL
;
1105 bool received
= false;
1107 assert(!extent
->length
);
1108 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, &reply
, &payload
) {
1110 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
1112 assert(nbd_reply_is_structured(&reply
));
1114 switch (chunk
->type
) {
1115 case NBD_REPLY_TYPE_BLOCK_STATUS
:
1117 nbd_channel_error(s
, -EINVAL
);
1118 error_setg(&local_err
, "Several BLOCK_STATUS chunks in reply");
1119 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1123 ret
= nbd_parse_blockstatus_payload(s
, &reply
.structured
,
1124 payload
, length
, extent
,
1127 nbd_channel_error(s
, ret
);
1128 nbd_iter_channel_error(&iter
, ret
, &local_err
);
1132 if (!nbd_reply_type_is_error(chunk
->type
)) {
1133 nbd_channel_error(s
, -EINVAL
);
1134 error_setg(&local_err
,
1135 "Unexpected reply type: %d (%s) "
1136 "for CMD_BLOCK_STATUS",
1137 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
1138 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
1146 if (!extent
->length
&& !iter
.request_ret
) {
1147 error_setg(&local_err
, "Server did not reply with any status extents");
1148 nbd_iter_channel_error(&iter
, -EIO
, &local_err
);
1151 error_propagate(errp
, iter
.err
);
1152 *request_ret
= iter
.request_ret
;
1156 static int nbd_co_request(BlockDriverState
*bs
, NBDRequest
*request
,
1157 QEMUIOVector
*write_qiov
)
1159 int ret
, request_ret
;
1160 Error
*local_err
= NULL
;
1161 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1163 assert(request
->type
!= NBD_CMD_READ
);
1165 assert(request
->type
== NBD_CMD_WRITE
);
1166 assert(request
->len
== iov_size(write_qiov
->iov
, write_qiov
->niov
));
1168 assert(request
->type
!= NBD_CMD_WRITE
);
1172 ret
= nbd_co_send_request(bs
, request
, write_qiov
);
1177 ret
= nbd_co_receive_return_code(s
, request
->handle
,
1178 &request_ret
, &local_err
);
1180 trace_nbd_co_request_fail(request
->from
, request
->len
,
1181 request
->handle
, request
->flags
,
1183 nbd_cmd_lookup(request
->type
),
1184 ret
, error_get_pretty(local_err
));
1185 error_free(local_err
);
1188 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1190 return ret
? ret
: request_ret
;
1193 static int nbd_client_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
1194 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1196 int ret
, request_ret
;
1197 Error
*local_err
= NULL
;
1198 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1199 NBDRequest request
= {
1200 .type
= NBD_CMD_READ
,
1205 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1212 * Work around the fact that the block layer doesn't do
1213 * byte-accurate sizing yet - if the read exceeds the server's
1214 * advertised size because the block layer rounded size up, then
1215 * truncate the request to the server and tail-pad with zero.
1217 if (offset
>= s
->info
.size
) {
1218 assert(bytes
< BDRV_SECTOR_SIZE
);
1219 qemu_iovec_memset(qiov
, 0, 0, bytes
);
1222 if (offset
+ bytes
> s
->info
.size
) {
1223 uint64_t slop
= offset
+ bytes
- s
->info
.size
;
1225 assert(slop
< BDRV_SECTOR_SIZE
);
1226 qemu_iovec_memset(qiov
, bytes
- slop
, 0, slop
);
1227 request
.len
-= slop
;
1231 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1236 ret
= nbd_co_receive_cmdread_reply(s
, request
.handle
, offset
, qiov
,
1237 &request_ret
, &local_err
);
1239 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1240 request
.flags
, request
.type
,
1241 nbd_cmd_lookup(request
.type
),
1242 ret
, error_get_pretty(local_err
));
1243 error_free(local_err
);
1246 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1248 return ret
? ret
: request_ret
;
1251 static int nbd_client_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1252 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1254 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1255 NBDRequest request
= {
1256 .type
= NBD_CMD_WRITE
,
1261 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1262 if (flags
& BDRV_REQ_FUA
) {
1263 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1264 request
.flags
|= NBD_CMD_FLAG_FUA
;
1267 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1272 return nbd_co_request(bs
, &request
, qiov
);
1275 static int nbd_client_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1276 int bytes
, BdrvRequestFlags flags
)
1278 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1279 NBDRequest request
= {
1280 .type
= NBD_CMD_WRITE_ZEROES
,
1285 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1286 if (!(s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
)) {
1290 if (flags
& BDRV_REQ_FUA
) {
1291 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1292 request
.flags
|= NBD_CMD_FLAG_FUA
;
1294 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1295 request
.flags
|= NBD_CMD_FLAG_NO_HOLE
;
1297 if (flags
& BDRV_REQ_NO_FALLBACK
) {
1298 assert(s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
);
1299 request
.flags
|= NBD_CMD_FLAG_FAST_ZERO
;
1305 return nbd_co_request(bs
, &request
, NULL
);
1308 static int nbd_client_co_flush(BlockDriverState
*bs
)
1310 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1311 NBDRequest request
= { .type
= NBD_CMD_FLUSH
};
1313 if (!(s
->info
.flags
& NBD_FLAG_SEND_FLUSH
)) {
1320 return nbd_co_request(bs
, &request
, NULL
);
1323 static int nbd_client_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
1326 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1327 NBDRequest request
= {
1328 .type
= NBD_CMD_TRIM
,
1333 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1334 if (!(s
->info
.flags
& NBD_FLAG_SEND_TRIM
) || !bytes
) {
1338 return nbd_co_request(bs
, &request
, NULL
);
1341 static int coroutine_fn
nbd_client_co_block_status(
1342 BlockDriverState
*bs
, bool want_zero
, int64_t offset
, int64_t bytes
,
1343 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
1345 int ret
, request_ret
;
1346 NBDExtent extent
= { 0 };
1347 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1348 Error
*local_err
= NULL
;
1350 NBDRequest request
= {
1351 .type
= NBD_CMD_BLOCK_STATUS
,
1353 .len
= MIN(QEMU_ALIGN_DOWN(INT_MAX
, bs
->bl
.request_alignment
),
1354 MIN(bytes
, s
->info
.size
- offset
)),
1355 .flags
= NBD_CMD_FLAG_REQ_ONE
,
1358 if (!s
->info
.base_allocation
) {
1362 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1366 * Work around the fact that the block layer doesn't do
1367 * byte-accurate sizing yet - if the status request exceeds the
1368 * server's advertised size because the block layer rounded size
1369 * up, we truncated the request to the server (above), or are
1370 * called on just the hole.
1372 if (offset
>= s
->info
.size
) {
1374 assert(bytes
< BDRV_SECTOR_SIZE
);
1375 /* Intentionally don't report offset_valid for the hole */
1376 return BDRV_BLOCK_ZERO
;
1379 if (s
->info
.min_block
) {
1380 assert(QEMU_IS_ALIGNED(request
.len
, s
->info
.min_block
));
1383 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1388 ret
= nbd_co_receive_blockstatus_reply(s
, request
.handle
, bytes
,
1389 &extent
, &request_ret
,
1392 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1393 request
.flags
, request
.type
,
1394 nbd_cmd_lookup(request
.type
),
1395 ret
, error_get_pretty(local_err
));
1396 error_free(local_err
);
1399 } while (ret
< 0 && nbd_client_connecting_wait(s
));
1401 if (ret
< 0 || request_ret
< 0) {
1402 return ret
? ret
: request_ret
;
1405 assert(extent
.length
);
1406 *pnum
= extent
.length
;
1409 return (extent
.flags
& NBD_STATE_HOLE
? 0 : BDRV_BLOCK_DATA
) |
1410 (extent
.flags
& NBD_STATE_ZERO
? BDRV_BLOCK_ZERO
: 0) |
1411 BDRV_BLOCK_OFFSET_VALID
;
1414 static int nbd_client_reopen_prepare(BDRVReopenState
*state
,
1415 BlockReopenQueue
*queue
, Error
**errp
)
1417 BDRVNBDState
*s
= (BDRVNBDState
*)state
->bs
->opaque
;
1419 if ((state
->flags
& BDRV_O_RDWR
) && (s
->info
.flags
& NBD_FLAG_READ_ONLY
)) {
1420 error_setg(errp
, "Can't reopen read-only NBD mount as read/write");
1426 static void nbd_client_close(BlockDriverState
*bs
)
1428 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1429 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1432 nbd_send_request(s
->ioc
, &request
);
1435 nbd_teardown_connection(bs
);
1438 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
1442 QIOChannelSocket
*sioc
;
1444 sioc
= qio_channel_socket_new();
1445 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
1447 qio_channel_socket_connect_sync(sioc
, saddr
, errp
);
1449 object_unref(OBJECT(sioc
));
1453 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
1458 /* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
1459 static int nbd_client_handshake(BlockDriverState
*bs
, QIOChannelSocket
*sioc
,
1462 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1463 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1466 trace_nbd_client_handshake(s
->export
);
1470 qio_channel_set_blocking(QIO_CHANNEL(sioc
), false, NULL
);
1471 qio_channel_attach_aio_context(QIO_CHANNEL(sioc
), aio_context
);
1473 s
->info
.request_sizes
= true;
1474 s
->info
.structured_reply
= true;
1475 s
->info
.base_allocation
= true;
1476 s
->info
.x_dirty_bitmap
= g_strdup(s
->x_dirty_bitmap
);
1477 s
->info
.name
= g_strdup(s
->export
?: "");
1478 ret
= nbd_receive_negotiate(aio_context
, QIO_CHANNEL(sioc
), s
->tlscreds
,
1479 s
->hostname
, &s
->ioc
, &s
->info
, errp
);
1480 g_free(s
->info
.x_dirty_bitmap
);
1481 g_free(s
->info
.name
);
1483 object_unref(OBJECT(sioc
));
1487 if (s
->x_dirty_bitmap
&& !s
->info
.base_allocation
) {
1488 error_setg(errp
, "requested x-dirty-bitmap %s not found",
1493 if (s
->info
.flags
& NBD_FLAG_READ_ONLY
) {
1494 ret
= bdrv_apply_auto_read_only(bs
, "NBD export is read-only", errp
);
1499 if (s
->info
.flags
& NBD_FLAG_SEND_FUA
) {
1500 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1501 bs
->supported_zero_flags
|= BDRV_REQ_FUA
;
1503 if (s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
) {
1504 bs
->supported_zero_flags
|= BDRV_REQ_MAY_UNMAP
;
1505 if (s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
) {
1506 bs
->supported_zero_flags
|= BDRV_REQ_NO_FALLBACK
;
1511 s
->ioc
= QIO_CHANNEL(sioc
);
1512 object_ref(OBJECT(s
->ioc
));
1515 trace_nbd_client_handshake_success(s
->export
);
1521 * We have connected, but must fail for other reasons.
1522 * Send NBD_CMD_DISC as a courtesy to the server.
1525 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1527 nbd_send_request(s
->ioc
?: QIO_CHANNEL(sioc
), &request
);
1529 object_unref(OBJECT(sioc
));
1537 * Parse nbd_open options
1540 static int nbd_parse_uri(const char *filename
, QDict
*options
)
1544 QueryParams
*qp
= NULL
;
1548 uri
= uri_parse(filename
);
1554 if (!g_strcmp0(uri
->scheme
, "nbd")) {
1556 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
1558 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
1565 p
= uri
->path
? uri
->path
: "";
1570 qdict_put_str(options
, "export", p
);
1573 qp
= query_params_parse(uri
->query
);
1574 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
1580 /* nbd+unix:///export?socket=path */
1581 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
1585 qdict_put_str(options
, "server.type", "unix");
1586 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
1591 /* nbd[+tcp]://host[:port]/export */
1597 /* strip braces from literal IPv6 address */
1598 if (uri
->server
[0] == '[') {
1599 host
= qstring_from_substr(uri
->server
, 1,
1600 strlen(uri
->server
) - 1);
1602 host
= qstring_from_str(uri
->server
);
1605 qdict_put_str(options
, "server.type", "inet");
1606 qdict_put(options
, "server.host", host
);
1608 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
1609 qdict_put_str(options
, "server.port", port_str
);
1615 query_params_free(qp
);
1621 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
1623 const QDictEntry
*e
;
1625 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
1626 if (!strcmp(e
->key
, "host") ||
1627 !strcmp(e
->key
, "port") ||
1628 !strcmp(e
->key
, "path") ||
1629 !strcmp(e
->key
, "export") ||
1630 strstart(e
->key
, "server.", NULL
))
1632 error_setg(errp
, "Option '%s' cannot be used with a file name",
1641 static void nbd_parse_filename(const char *filename
, QDict
*options
,
1644 g_autofree
char *file
= NULL
;
1646 const char *host_spec
;
1647 const char *unixpath
;
1649 if (nbd_has_filename_options_conflict(options
, errp
)) {
1653 if (strstr(filename
, "://")) {
1654 int ret
= nbd_parse_uri(filename
, options
);
1656 error_setg(errp
, "No valid URL specified");
1661 file
= g_strdup(filename
);
1663 export_name
= strstr(file
, EN_OPTSTR
);
1665 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
1668 export_name
[0] = 0; /* truncate 'file' */
1669 export_name
+= strlen(EN_OPTSTR
);
1671 qdict_put_str(options
, "export", export_name
);
1674 /* extract the host_spec - fail if it's not nbd:... */
1675 if (!strstart(file
, "nbd:", &host_spec
)) {
1676 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
1684 /* are we a UNIX or TCP socket? */
1685 if (strstart(host_spec
, "unix:", &unixpath
)) {
1686 qdict_put_str(options
, "server.type", "unix");
1687 qdict_put_str(options
, "server.path", unixpath
);
1689 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
1691 if (inet_parse(addr
, host_spec
, errp
)) {
1695 qdict_put_str(options
, "server.type", "inet");
1696 qdict_put_str(options
, "server.host", addr
->host
);
1697 qdict_put_str(options
, "server.port", addr
->port
);
1699 qapi_free_InetSocketAddress(addr
);
1703 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
1704 QemuOpts
*legacy_opts
,
1707 const char *path
= qemu_opt_get(legacy_opts
, "path");
1708 const char *host
= qemu_opt_get(legacy_opts
, "host");
1709 const char *port
= qemu_opt_get(legacy_opts
, "port");
1710 const QDictEntry
*e
;
1712 if (!path
&& !host
&& !port
) {
1716 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
1718 if (strstart(e
->key
, "server.", NULL
)) {
1719 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
1726 error_setg(errp
, "path and host may not be used at the same time");
1730 error_setg(errp
, "port may not be used without host");
1734 qdict_put_str(output_options
, "server.type", "unix");
1735 qdict_put_str(output_options
, "server.path", path
);
1737 qdict_put_str(output_options
, "server.type", "inet");
1738 qdict_put_str(output_options
, "server.host", host
);
1739 qdict_put_str(output_options
, "server.port",
1740 port
?: stringify(NBD_DEFAULT_PORT
));
1746 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
1749 SocketAddress
*saddr
= NULL
;
1753 qdict_extract_subqdict(options
, &addr
, "server.");
1754 if (!qdict_size(addr
)) {
1755 error_setg(errp
, "NBD server address missing");
1759 iv
= qobject_input_visitor_new_flat_confused(addr
, errp
);
1764 if (!visit_type_SocketAddress(iv
, NULL
, &saddr
, errp
)) {
1769 qobject_unref(addr
);
1774 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
1777 QCryptoTLSCreds
*creds
;
1779 obj
= object_resolve_path_component(
1780 object_get_objects_root(), id
);
1782 error_setg(errp
, "No TLS credentials with id '%s'",
1786 creds
= (QCryptoTLSCreds
*)
1787 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
1789 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1794 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
1796 "Expecting TLS credentials with a client endpoint");
1804 static QemuOptsList nbd_runtime_opts
= {
1806 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
1810 .type
= QEMU_OPT_STRING
,
1811 .help
= "TCP host to connect to",
1815 .type
= QEMU_OPT_STRING
,
1816 .help
= "TCP port to connect to",
1820 .type
= QEMU_OPT_STRING
,
1821 .help
= "Unix socket path to connect to",
1825 .type
= QEMU_OPT_STRING
,
1826 .help
= "Name of the NBD export to open",
1829 .name
= "tls-creds",
1830 .type
= QEMU_OPT_STRING
,
1831 .help
= "ID of the TLS credentials to use",
1834 .name
= "x-dirty-bitmap",
1835 .type
= QEMU_OPT_STRING
,
1836 .help
= "experimental: expose named dirty bitmap in place of "
1840 .name
= "reconnect-delay",
1841 .type
= QEMU_OPT_NUMBER
,
1842 .help
= "On an unexpected disconnect, the nbd client tries to "
1843 "connect again until succeeding or encountering a serious "
1844 "error. During the first @reconnect-delay seconds, all "
1845 "requests are paused and will be rerun on a successful "
1846 "reconnect. After that time, any delayed requests and all "
1847 "future requests before a successful reconnect will "
1848 "immediately fail. Default 0",
1850 { /* end of list */ }
1854 static int nbd_process_options(BlockDriverState
*bs
, QDict
*options
,
1857 BDRVNBDState
*s
= bs
->opaque
;
1861 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
1862 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
1866 /* Translate @host, @port, and @path to a SocketAddress */
1867 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
1871 /* Pop the config into our state object. Exit if invalid. */
1872 s
->saddr
= nbd_config(s
, options
, errp
);
1877 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
1878 if (s
->export
&& strlen(s
->export
) > NBD_MAX_STRING_SIZE
) {
1879 error_setg(errp
, "export name too long to send to server");
1883 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
1884 if (s
->tlscredsid
) {
1885 s
->tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
1890 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1891 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
1892 error_setg(errp
, "TLS only supported over IP sockets");
1895 s
->hostname
= s
->saddr
->u
.inet
.host
;
1898 s
->x_dirty_bitmap
= g_strdup(qemu_opt_get(opts
, "x-dirty-bitmap"));
1899 if (s
->x_dirty_bitmap
&& strlen(s
->x_dirty_bitmap
) > NBD_MAX_STRING_SIZE
) {
1900 error_setg(errp
, "x-dirty-bitmap query too long to send to server");
1904 s
->reconnect_delay
= qemu_opt_get_number(opts
, "reconnect-delay", 0);
1910 nbd_clear_bdrvstate(s
);
1912 qemu_opts_del(opts
);
1916 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1920 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1921 QIOChannelSocket
*sioc
;
1923 ret
= nbd_process_options(bs
, options
, errp
);
1929 qemu_co_mutex_init(&s
->send_mutex
);
1930 qemu_co_queue_init(&s
->free_sema
);
1933 * establish TCP connection, return error if it fails
1934 * TODO: Configurable retry-until-timeout behaviour.
1936 sioc
= nbd_establish_connection(s
->saddr
, errp
);
1938 return -ECONNREFUSED
;
1941 ret
= nbd_client_handshake(bs
, sioc
, errp
);
1943 nbd_clear_bdrvstate(s
);
1946 /* successfully connected */
1947 s
->state
= NBD_CLIENT_CONNECTED
;
1949 s
->connection_co
= qemu_coroutine_create(nbd_connection_entry
, s
);
1950 bdrv_inc_in_flight(bs
);
1951 aio_co_schedule(bdrv_get_aio_context(bs
), s
->connection_co
);
1956 static int nbd_co_flush(BlockDriverState
*bs
)
1958 return nbd_client_co_flush(bs
);
1961 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1963 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1964 uint32_t min
= s
->info
.min_block
;
1965 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
1968 * If the server did not advertise an alignment:
1969 * - a size that is not sector-aligned implies that an alignment
1970 * of 1 can be used to access those tail bytes
1971 * - advertisement of block status requires an alignment of 1, so
1972 * that we don't violate block layer constraints that block
1973 * status is always aligned (as we can't control whether the
1974 * server will report sub-sector extents, such as a hole at EOF
1975 * on an unaligned POSIX file)
1976 * - otherwise, assume the server is so old that we are safer avoiding
1977 * sub-sector requests
1980 min
= (!QEMU_IS_ALIGNED(s
->info
.size
, BDRV_SECTOR_SIZE
) ||
1981 s
->info
.base_allocation
) ? 1 : BDRV_SECTOR_SIZE
;
1984 bs
->bl
.request_alignment
= min
;
1985 bs
->bl
.max_pdiscard
= QEMU_ALIGN_DOWN(INT_MAX
, min
);
1986 bs
->bl
.max_pwrite_zeroes
= max
;
1987 bs
->bl
.max_transfer
= max
;
1989 if (s
->info
.opt_block
&&
1990 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
1991 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
1995 static void nbd_close(BlockDriverState
*bs
)
1997 BDRVNBDState
*s
= bs
->opaque
;
1999 nbd_client_close(bs
);
2000 nbd_clear_bdrvstate(s
);
2004 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2005 * to a smaller size with exact=false, there is no reason to fail the
2008 * Preallocation mode is ignored since it does not seems useful to fail when
2009 * we never change anything.
2011 static int coroutine_fn
nbd_co_truncate(BlockDriverState
*bs
, int64_t offset
,
2012 bool exact
, PreallocMode prealloc
,
2013 BdrvRequestFlags flags
, Error
**errp
)
2015 BDRVNBDState
*s
= bs
->opaque
;
2017 if (offset
!= s
->info
.size
&& exact
) {
2018 error_setg(errp
, "Cannot resize NBD nodes");
2022 if (offset
> s
->info
.size
) {
2023 error_setg(errp
, "Cannot grow NBD nodes");
2030 static int64_t nbd_getlength(BlockDriverState
*bs
)
2032 BDRVNBDState
*s
= bs
->opaque
;
2034 return s
->info
.size
;
2037 static void nbd_refresh_filename(BlockDriverState
*bs
)
2039 BDRVNBDState
*s
= bs
->opaque
;
2040 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
2043 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
2044 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
2045 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
2049 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
2050 path
= s
->saddr
->u
.q_unix
.path
;
2051 } /* else can't represent as pseudo-filename */
2053 if (path
&& s
->export
) {
2054 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2055 "nbd+unix:///%s?socket=%s", s
->export
, path
);
2056 } else if (path
&& !s
->export
) {
2057 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2058 "nbd+unix://?socket=%s", path
);
2059 } else if (host
&& s
->export
) {
2060 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2061 "nbd://%s:%s/%s", host
, port
, s
->export
);
2062 } else if (host
&& !s
->export
) {
2063 len
= snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
2064 "nbd://%s:%s", host
, port
);
2066 if (len
>= sizeof(bs
->exact_filename
)) {
2067 /* Name is too long to represent exactly, so leave it empty. */
2068 bs
->exact_filename
[0] = '\0';
2072 static char *nbd_dirname(BlockDriverState
*bs
, Error
**errp
)
2074 /* The generic bdrv_dirname() implementation is able to work out some
2075 * directory name for NBD nodes, but that would be wrong. So far there is no
2076 * specification for how "export paths" would work, so NBD does not have
2077 * directory names. */
2078 error_setg(errp
, "Cannot generate a base directory for NBD nodes");
2082 static const char *const nbd_strong_runtime_opts
[] = {
2093 static BlockDriver bdrv_nbd
= {
2094 .format_name
= "nbd",
2095 .protocol_name
= "nbd",
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_co_truncate
= nbd_co_truncate
,
2110 .bdrv_getlength
= nbd_getlength
,
2111 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2112 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2113 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2114 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2115 .bdrv_refresh_filename
= nbd_refresh_filename
,
2116 .bdrv_co_block_status
= nbd_client_co_block_status
,
2117 .bdrv_dirname
= nbd_dirname
,
2118 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2121 static BlockDriver bdrv_nbd_tcp
= {
2122 .format_name
= "nbd",
2123 .protocol_name
= "nbd+tcp",
2124 .instance_size
= sizeof(BDRVNBDState
),
2125 .bdrv_parse_filename
= nbd_parse_filename
,
2126 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2127 .create_opts
= &bdrv_create_opts_simple
,
2128 .bdrv_file_open
= nbd_open
,
2129 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2130 .bdrv_co_preadv
= nbd_client_co_preadv
,
2131 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2132 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2133 .bdrv_close
= nbd_close
,
2134 .bdrv_co_flush_to_os
= nbd_co_flush
,
2135 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2136 .bdrv_refresh_limits
= nbd_refresh_limits
,
2137 .bdrv_co_truncate
= nbd_co_truncate
,
2138 .bdrv_getlength
= nbd_getlength
,
2139 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2140 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2141 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2142 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2143 .bdrv_refresh_filename
= nbd_refresh_filename
,
2144 .bdrv_co_block_status
= nbd_client_co_block_status
,
2145 .bdrv_dirname
= nbd_dirname
,
2146 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2149 static BlockDriver bdrv_nbd_unix
= {
2150 .format_name
= "nbd",
2151 .protocol_name
= "nbd+unix",
2152 .instance_size
= sizeof(BDRVNBDState
),
2153 .bdrv_parse_filename
= nbd_parse_filename
,
2154 .bdrv_co_create_opts
= bdrv_co_create_opts_simple
,
2155 .create_opts
= &bdrv_create_opts_simple
,
2156 .bdrv_file_open
= nbd_open
,
2157 .bdrv_reopen_prepare
= nbd_client_reopen_prepare
,
2158 .bdrv_co_preadv
= nbd_client_co_preadv
,
2159 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
2160 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
2161 .bdrv_close
= nbd_close
,
2162 .bdrv_co_flush_to_os
= nbd_co_flush
,
2163 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
2164 .bdrv_refresh_limits
= nbd_refresh_limits
,
2165 .bdrv_co_truncate
= nbd_co_truncate
,
2166 .bdrv_getlength
= nbd_getlength
,
2167 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
2168 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
2169 .bdrv_co_drain_begin
= nbd_client_co_drain_begin
,
2170 .bdrv_co_drain_end
= nbd_client_co_drain_end
,
2171 .bdrv_refresh_filename
= nbd_refresh_filename
,
2172 .bdrv_co_block_status
= nbd_client_co_block_status
,
2173 .bdrv_dirname
= nbd_dirname
,
2174 .strong_runtime_opts
= nbd_strong_runtime_opts
,
2177 static void bdrv_nbd_init(void)
2179 bdrv_register(&bdrv_nbd
);
2180 bdrv_register(&bdrv_nbd_tcp
);
2181 bdrv_register(&bdrv_nbd_unix
);
2184 block_init(bdrv_nbd_init
);