2 * QEMU Block driver for NBD
4 * Copyright (C) 2016 Red Hat, Inc.
5 * Copyright (C) 2008 Bull S.A.S.
6 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
9 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 #include "qemu/osdep.h"
34 #include "qemu/option.h"
35 #include "qemu/cutils.h"
36 #include "qemu/main-loop.h"
38 #include "qapi/qapi-visit-sockets.h"
39 #include "qapi/qmp/qstring.h"
41 #include "block/qdict.h"
42 #include "block/nbd.h"
43 #include "block/block_int.h"
45 #define EN_OPTSTR ":exportname="
46 #define MAX_NBD_REQUESTS 16
48 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
49 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
53 uint64_t offset
; /* original offset of the request */
54 bool receiving
; /* waiting for connection_co? */
57 typedef enum NBDClientState
{
62 typedef struct BDRVNBDState
{
63 QIOChannelSocket
*sioc
; /* The master data channel */
64 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
69 Coroutine
*connection_co
;
73 NBDClientRequest requests
[MAX_NBD_REQUESTS
];
77 /* Connection parameters */
78 uint32_t reconnect_delay
;
80 char *export
, *tlscredsid
;
81 QCryptoTLSCreds
*tlscreds
;
86 /* @ret will be used for reconnect in future */
87 static void nbd_channel_error(BDRVNBDState
*s
, int ret
)
89 s
->state
= NBD_CLIENT_QUIT
;
92 static void nbd_recv_coroutines_wake_all(BDRVNBDState
*s
)
96 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
97 NBDClientRequest
*req
= &s
->requests
[i
];
99 if (req
->coroutine
&& req
->receiving
) {
100 aio_co_wake(req
->coroutine
);
105 static void nbd_client_detach_aio_context(BlockDriverState
*bs
)
107 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
109 qio_channel_detach_aio_context(QIO_CHANNEL(s
->ioc
));
112 static void nbd_client_attach_aio_context_bh(void *opaque
)
114 BlockDriverState
*bs
= opaque
;
115 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
118 * The node is still drained, so we know the coroutine has yielded in
119 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
120 * entered for the first time. Both places are safe for entering the
123 qemu_aio_coroutine_enter(bs
->aio_context
, s
->connection_co
);
124 bdrv_dec_in_flight(bs
);
127 static void nbd_client_attach_aio_context(BlockDriverState
*bs
,
128 AioContext
*new_context
)
130 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
132 qio_channel_attach_aio_context(QIO_CHANNEL(s
->ioc
), new_context
);
134 bdrv_inc_in_flight(bs
);
137 * Need to wait here for the BH to run because the BH must run while the
138 * node is still drained.
140 aio_wait_bh_oneshot(new_context
, nbd_client_attach_aio_context_bh
, bs
);
144 static void nbd_teardown_connection(BlockDriverState
*bs
)
146 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
150 /* finish any pending coroutines */
151 qio_channel_shutdown(s
->ioc
,
152 QIO_CHANNEL_SHUTDOWN_BOTH
,
154 BDRV_POLL_WHILE(bs
, s
->connection_co
);
156 nbd_client_detach_aio_context(bs
);
157 object_unref(OBJECT(s
->sioc
));
159 object_unref(OBJECT(s
->ioc
));
163 static coroutine_fn
void nbd_connection_entry(void *opaque
)
165 BDRVNBDState
*s
= opaque
;
168 Error
*local_err
= NULL
;
170 while (s
->state
!= NBD_CLIENT_QUIT
) {
172 * The NBD client can only really be considered idle when it has
173 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
174 * the point where the additional scheduled coroutine entry happens
175 * after nbd_client_attach_aio_context().
177 * Therefore we keep an additional in_flight reference all the time and
178 * only drop it temporarily here.
180 assert(s
->reply
.handle
== 0);
181 ret
= nbd_receive_reply(s
->bs
, s
->ioc
, &s
->reply
, &local_err
);
184 trace_nbd_read_reply_entry_fail(ret
, error_get_pretty(local_err
));
185 error_free(local_err
);
188 nbd_channel_error(s
, ret
? ret
: -EIO
);
193 * There's no need for a mutex on the receive side, because the
194 * handler acts as a synchronization point and ensures that only
195 * one coroutine is called until the reply finishes.
197 i
= HANDLE_TO_INDEX(s
, s
->reply
.handle
);
198 if (i
>= MAX_NBD_REQUESTS
||
199 !s
->requests
[i
].coroutine
||
200 !s
->requests
[i
].receiving
||
201 (nbd_reply_is_structured(&s
->reply
) && !s
->info
.structured_reply
))
203 nbd_channel_error(s
, -EINVAL
);
208 * We're woken up again by the request itself. Note that there
209 * is no race between yielding and reentering connection_co. This
212 * - if the request runs on the same AioContext, it is only
213 * entered after we yield
215 * - if the request runs on a different AioContext, reentering
216 * connection_co happens through a bottom half, which can only
217 * run after we yield.
219 aio_co_wake(s
->requests
[i
].coroutine
);
220 qemu_coroutine_yield();
223 nbd_recv_coroutines_wake_all(s
);
224 bdrv_dec_in_flight(s
->bs
);
226 s
->connection_co
= NULL
;
230 static int nbd_co_send_request(BlockDriverState
*bs
,
234 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
237 qemu_co_mutex_lock(&s
->send_mutex
);
238 while (s
->in_flight
== MAX_NBD_REQUESTS
) {
239 qemu_co_queue_wait(&s
->free_sema
, &s
->send_mutex
);
242 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
249 for (i
= 0; i
< MAX_NBD_REQUESTS
; i
++) {
250 if (s
->requests
[i
].coroutine
== NULL
) {
255 g_assert(qemu_in_coroutine());
256 assert(i
< MAX_NBD_REQUESTS
);
258 s
->requests
[i
].coroutine
= qemu_coroutine_self();
259 s
->requests
[i
].offset
= request
->from
;
260 s
->requests
[i
].receiving
= false;
262 request
->handle
= INDEX_TO_HANDLE(s
, i
);
267 qio_channel_set_cork(s
->ioc
, true);
268 rc
= nbd_send_request(s
->ioc
, request
);
269 if (rc
>= 0 && s
->state
== NBD_CLIENT_CONNECTED
) {
270 if (qio_channel_writev_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
274 } else if (rc
>= 0) {
277 qio_channel_set_cork(s
->ioc
, false);
279 rc
= nbd_send_request(s
->ioc
, request
);
284 nbd_channel_error(s
, rc
);
286 s
->requests
[i
].coroutine
= NULL
;
289 qemu_co_queue_next(&s
->free_sema
);
291 qemu_co_mutex_unlock(&s
->send_mutex
);
295 static inline uint16_t payload_advance16(uint8_t **payload
)
298 return lduw_be_p(*payload
- 2);
301 static inline uint32_t payload_advance32(uint8_t **payload
)
304 return ldl_be_p(*payload
- 4);
307 static inline uint64_t payload_advance64(uint8_t **payload
)
310 return ldq_be_p(*payload
- 8);
313 static int nbd_parse_offset_hole_payload(BDRVNBDState
*s
,
314 NBDStructuredReplyChunk
*chunk
,
315 uint8_t *payload
, uint64_t orig_offset
,
316 QEMUIOVector
*qiov
, Error
**errp
)
321 if (chunk
->length
!= sizeof(offset
) + sizeof(hole_size
)) {
322 error_setg(errp
, "Protocol error: invalid payload for "
323 "NBD_REPLY_TYPE_OFFSET_HOLE");
327 offset
= payload_advance64(&payload
);
328 hole_size
= payload_advance32(&payload
);
330 if (!hole_size
|| offset
< orig_offset
|| hole_size
> qiov
->size
||
331 offset
> orig_offset
+ qiov
->size
- hole_size
) {
332 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
336 if (s
->info
.min_block
&&
337 !QEMU_IS_ALIGNED(hole_size
, s
->info
.min_block
)) {
338 trace_nbd_structured_read_compliance("hole");
341 qemu_iovec_memset(qiov
, offset
- orig_offset
, 0, hole_size
);
347 * nbd_parse_blockstatus_payload
348 * Based on our request, we expect only one extent in reply, for the
349 * base:allocation context.
351 static int nbd_parse_blockstatus_payload(BDRVNBDState
*s
,
352 NBDStructuredReplyChunk
*chunk
,
353 uint8_t *payload
, uint64_t orig_length
,
354 NBDExtent
*extent
, Error
**errp
)
358 /* The server succeeded, so it must have sent [at least] one extent */
359 if (chunk
->length
< sizeof(context_id
) + sizeof(*extent
)) {
360 error_setg(errp
, "Protocol error: invalid payload for "
361 "NBD_REPLY_TYPE_BLOCK_STATUS");
365 context_id
= payload_advance32(&payload
);
366 if (s
->info
.context_id
!= context_id
) {
367 error_setg(errp
, "Protocol error: unexpected context id %d for "
368 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
369 "id is %d", context_id
,
374 extent
->length
= payload_advance32(&payload
);
375 extent
->flags
= payload_advance32(&payload
);
377 if (extent
->length
== 0) {
378 error_setg(errp
, "Protocol error: server sent status chunk with "
384 * A server sending unaligned block status is in violation of the
385 * protocol, but as qemu-nbd 3.1 is such a server (at least for
386 * POSIX files that are not a multiple of 512 bytes, since qemu
387 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
388 * still sees an implicit hole beyond the real EOF), it's nicer to
389 * work around the misbehaving server. If the request included
390 * more than the final unaligned block, truncate it back to an
391 * aligned result; if the request was only the final block, round
392 * up to the full block and change the status to fully-allocated
393 * (always a safe status, even if it loses information).
395 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(extent
->length
,
396 s
->info
.min_block
)) {
397 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
398 if (extent
->length
> s
->info
.min_block
) {
399 extent
->length
= QEMU_ALIGN_DOWN(extent
->length
,
402 extent
->length
= s
->info
.min_block
;
408 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
409 * sent us any more than one extent, nor should it have included
410 * status beyond our request in that extent. However, it's easy
411 * enough to ignore the server's noncompliance without killing the
412 * connection; just ignore trailing extents, and clamp things to
413 * the length of our request.
415 if (chunk
->length
> sizeof(context_id
) + sizeof(*extent
)) {
416 trace_nbd_parse_blockstatus_compliance("more than one extent");
418 if (extent
->length
> orig_length
) {
419 extent
->length
= orig_length
;
420 trace_nbd_parse_blockstatus_compliance("extent length too large");
427 * nbd_parse_error_payload
428 * on success @errp contains message describing nbd error reply
430 static int nbd_parse_error_payload(NBDStructuredReplyChunk
*chunk
,
431 uint8_t *payload
, int *request_ret
,
435 uint16_t message_size
;
437 assert(chunk
->type
& (1 << 15));
439 if (chunk
->length
< sizeof(error
) + sizeof(message_size
)) {
441 "Protocol error: invalid payload for structured error");
445 error
= nbd_errno_to_system_errno(payload_advance32(&payload
));
447 error_setg(errp
, "Protocol error: server sent structured error chunk "
452 *request_ret
= -error
;
453 message_size
= payload_advance16(&payload
);
455 if (message_size
> chunk
->length
- sizeof(error
) - sizeof(message_size
)) {
456 error_setg(errp
, "Protocol error: server sent structured error chunk "
457 "with incorrect message size");
461 /* TODO: Add a trace point to mention the server complaint */
463 /* TODO handle ERROR_OFFSET */
468 static int nbd_co_receive_offset_data_payload(BDRVNBDState
*s
,
469 uint64_t orig_offset
,
470 QEMUIOVector
*qiov
, Error
**errp
)
472 QEMUIOVector sub_qiov
;
476 NBDStructuredReplyChunk
*chunk
= &s
->reply
.structured
;
478 assert(nbd_reply_is_structured(&s
->reply
));
480 /* The NBD spec requires at least one byte of payload */
481 if (chunk
->length
<= sizeof(offset
)) {
482 error_setg(errp
, "Protocol error: invalid payload for "
483 "NBD_REPLY_TYPE_OFFSET_DATA");
487 if (nbd_read64(s
->ioc
, &offset
, "OFFSET_DATA offset", errp
) < 0) {
491 data_size
= chunk
->length
- sizeof(offset
);
493 if (offset
< orig_offset
|| data_size
> qiov
->size
||
494 offset
> orig_offset
+ qiov
->size
- data_size
) {
495 error_setg(errp
, "Protocol error: server sent chunk exceeding requested"
499 if (s
->info
.min_block
&& !QEMU_IS_ALIGNED(data_size
, s
->info
.min_block
)) {
500 trace_nbd_structured_read_compliance("data");
503 qemu_iovec_init(&sub_qiov
, qiov
->niov
);
504 qemu_iovec_concat(&sub_qiov
, qiov
, offset
- orig_offset
, data_size
);
505 ret
= qio_channel_readv_all(s
->ioc
, sub_qiov
.iov
, sub_qiov
.niov
, errp
);
506 qemu_iovec_destroy(&sub_qiov
);
508 return ret
< 0 ? -EIO
: 0;
511 #define NBD_MAX_MALLOC_PAYLOAD 1000
512 static coroutine_fn
int nbd_co_receive_structured_payload(
513 BDRVNBDState
*s
, void **payload
, Error
**errp
)
518 assert(nbd_reply_is_structured(&s
->reply
));
520 len
= s
->reply
.structured
.length
;
526 if (payload
== NULL
) {
527 error_setg(errp
, "Unexpected structured payload");
531 if (len
> NBD_MAX_MALLOC_PAYLOAD
) {
532 error_setg(errp
, "Payload too large");
536 *payload
= g_new(char, len
);
537 ret
= nbd_read(s
->ioc
, *payload
, len
, "structured payload", errp
);
548 * nbd_co_do_receive_one_chunk
550 * set request_ret to received reply error
551 * if qiov is not NULL: read payload to @qiov
552 * for structured reply chunk:
553 * if error chunk: read payload, set @request_ret, do not set @payload
554 * else if offset_data chunk: read payload data to @qiov, do not set @payload
555 * else: read payload to @payload
557 * If function fails, @errp contains corresponding error message, and the
558 * connection with the server is suspect. If it returns 0, then the
559 * transaction succeeded (although @request_ret may be a negative errno
560 * corresponding to the server's error reply), and errp is unchanged.
562 static coroutine_fn
int nbd_co_do_receive_one_chunk(
563 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
564 int *request_ret
, QEMUIOVector
*qiov
, void **payload
, Error
**errp
)
567 int i
= HANDLE_TO_INDEX(s
, handle
);
568 void *local_payload
= NULL
;
569 NBDStructuredReplyChunk
*chunk
;
576 /* Wait until we're woken up by nbd_connection_entry. */
577 s
->requests
[i
].receiving
= true;
578 qemu_coroutine_yield();
579 s
->requests
[i
].receiving
= false;
580 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
581 error_setg(errp
, "Connection closed");
586 assert(s
->reply
.handle
== handle
);
588 if (nbd_reply_is_simple(&s
->reply
)) {
589 if (only_structured
) {
590 error_setg(errp
, "Protocol error: simple reply when structured "
591 "reply chunk was expected");
595 *request_ret
= -nbd_errno_to_system_errno(s
->reply
.simple
.error
);
596 if (*request_ret
< 0 || !qiov
) {
600 return qio_channel_readv_all(s
->ioc
, qiov
->iov
, qiov
->niov
,
601 errp
) < 0 ? -EIO
: 0;
604 /* handle structured reply chunk */
605 assert(s
->info
.structured_reply
);
606 chunk
= &s
->reply
.structured
;
608 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
609 if (!(chunk
->flags
& NBD_REPLY_FLAG_DONE
)) {
610 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
611 " NBD_REPLY_FLAG_DONE flag set");
615 error_setg(errp
, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
622 if (chunk
->type
== NBD_REPLY_TYPE_OFFSET_DATA
) {
624 error_setg(errp
, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
628 return nbd_co_receive_offset_data_payload(s
, s
->requests
[i
].offset
,
632 if (nbd_reply_type_is_error(chunk
->type
)) {
633 payload
= &local_payload
;
636 ret
= nbd_co_receive_structured_payload(s
, payload
, errp
);
641 if (nbd_reply_type_is_error(chunk
->type
)) {
642 ret
= nbd_parse_error_payload(chunk
, local_payload
, request_ret
, errp
);
643 g_free(local_payload
);
651 * nbd_co_receive_one_chunk
652 * Read reply, wake up connection_co and set s->quit if needed.
653 * Return value is a fatal error code or normal nbd reply error code
655 static coroutine_fn
int nbd_co_receive_one_chunk(
656 BDRVNBDState
*s
, uint64_t handle
, bool only_structured
,
657 int *request_ret
, QEMUIOVector
*qiov
, NBDReply
*reply
, void **payload
,
660 int ret
= nbd_co_do_receive_one_chunk(s
, handle
, only_structured
,
661 request_ret
, qiov
, payload
, errp
);
664 memset(reply
, 0, sizeof(*reply
));
665 nbd_channel_error(s
, ret
);
667 /* For assert at loop start in nbd_connection_entry */
672 if (s
->connection_co
) {
673 aio_co_wake(s
->connection_co
);
679 typedef struct NBDReplyChunkIter
{
683 bool done
, only_structured
;
686 static void nbd_iter_channel_error(NBDReplyChunkIter
*iter
,
687 int ret
, Error
**local_err
)
693 error_propagate(&iter
->err
, *local_err
);
695 error_free(*local_err
);
701 static void nbd_iter_request_error(NBDReplyChunkIter
*iter
, int ret
)
705 if (!iter
->request_ret
) {
706 iter
->request_ret
= ret
;
711 * NBD_FOREACH_REPLY_CHUNK
712 * The pointer stored in @payload requires g_free() to free it.
714 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
715 qiov, reply, payload) \
716 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
717 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
720 * nbd_reply_chunk_iter_receive
721 * The pointer stored in @payload requires g_free() to free it.
723 static bool nbd_reply_chunk_iter_receive(BDRVNBDState
*s
,
724 NBDReplyChunkIter
*iter
,
726 QEMUIOVector
*qiov
, NBDReply
*reply
,
729 int ret
, request_ret
;
730 NBDReply local_reply
;
731 NBDStructuredReplyChunk
*chunk
;
732 Error
*local_err
= NULL
;
733 if (s
->state
!= NBD_CLIENT_CONNECTED
) {
734 error_setg(&local_err
, "Connection closed");
735 nbd_iter_channel_error(iter
, -EIO
, &local_err
);
740 /* Previous iteration was last. */
745 reply
= &local_reply
;
748 ret
= nbd_co_receive_one_chunk(s
, handle
, iter
->only_structured
,
749 &request_ret
, qiov
, reply
, payload
,
752 nbd_iter_channel_error(iter
, ret
, &local_err
);
753 } else if (request_ret
< 0) {
754 nbd_iter_request_error(iter
, request_ret
);
757 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
758 if (nbd_reply_is_simple(reply
) || s
->state
!= NBD_CLIENT_CONNECTED
) {
762 chunk
= &reply
->structured
;
763 iter
->only_structured
= true;
765 if (chunk
->type
== NBD_REPLY_TYPE_NONE
) {
766 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
767 assert(chunk
->flags
& NBD_REPLY_FLAG_DONE
);
771 if (chunk
->flags
& NBD_REPLY_FLAG_DONE
) {
772 /* This iteration is last. */
776 /* Execute the loop body */
780 s
->requests
[HANDLE_TO_INDEX(s
, handle
)].coroutine
= NULL
;
782 qemu_co_mutex_lock(&s
->send_mutex
);
784 qemu_co_queue_next(&s
->free_sema
);
785 qemu_co_mutex_unlock(&s
->send_mutex
);
790 static int nbd_co_receive_return_code(BDRVNBDState
*s
, uint64_t handle
,
791 int *request_ret
, Error
**errp
)
793 NBDReplyChunkIter iter
;
795 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, NULL
, NULL
) {
796 /* nbd_reply_chunk_iter_receive does all the work */
799 error_propagate(errp
, iter
.err
);
800 *request_ret
= iter
.request_ret
;
804 static int nbd_co_receive_cmdread_reply(BDRVNBDState
*s
, uint64_t handle
,
805 uint64_t offset
, QEMUIOVector
*qiov
,
806 int *request_ret
, Error
**errp
)
808 NBDReplyChunkIter iter
;
810 void *payload
= NULL
;
811 Error
*local_err
= NULL
;
813 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, s
->info
.structured_reply
,
814 qiov
, &reply
, &payload
)
817 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
819 assert(nbd_reply_is_structured(&reply
));
821 switch (chunk
->type
) {
822 case NBD_REPLY_TYPE_OFFSET_DATA
:
824 * special cased in nbd_co_receive_one_chunk, data is already
828 case NBD_REPLY_TYPE_OFFSET_HOLE
:
829 ret
= nbd_parse_offset_hole_payload(s
, &reply
.structured
, payload
,
830 offset
, qiov
, &local_err
);
832 nbd_channel_error(s
, ret
);
833 nbd_iter_channel_error(&iter
, ret
, &local_err
);
837 if (!nbd_reply_type_is_error(chunk
->type
)) {
838 /* not allowed reply type */
839 nbd_channel_error(s
, -EINVAL
);
840 error_setg(&local_err
,
841 "Unexpected reply type: %d (%s) for CMD_READ",
842 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
843 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
851 error_propagate(errp
, iter
.err
);
852 *request_ret
= iter
.request_ret
;
856 static int nbd_co_receive_blockstatus_reply(BDRVNBDState
*s
,
857 uint64_t handle
, uint64_t length
,
859 int *request_ret
, Error
**errp
)
861 NBDReplyChunkIter iter
;
863 void *payload
= NULL
;
864 Error
*local_err
= NULL
;
865 bool received
= false;
867 assert(!extent
->length
);
868 NBD_FOREACH_REPLY_CHUNK(s
, iter
, handle
, false, NULL
, &reply
, &payload
) {
870 NBDStructuredReplyChunk
*chunk
= &reply
.structured
;
872 assert(nbd_reply_is_structured(&reply
));
874 switch (chunk
->type
) {
875 case NBD_REPLY_TYPE_BLOCK_STATUS
:
877 nbd_channel_error(s
, -EINVAL
);
878 error_setg(&local_err
, "Several BLOCK_STATUS chunks in reply");
879 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
883 ret
= nbd_parse_blockstatus_payload(s
, &reply
.structured
,
884 payload
, length
, extent
,
887 nbd_channel_error(s
, ret
);
888 nbd_iter_channel_error(&iter
, ret
, &local_err
);
892 if (!nbd_reply_type_is_error(chunk
->type
)) {
893 nbd_channel_error(s
, -EINVAL
);
894 error_setg(&local_err
,
895 "Unexpected reply type: %d (%s) "
896 "for CMD_BLOCK_STATUS",
897 chunk
->type
, nbd_reply_type_lookup(chunk
->type
));
898 nbd_iter_channel_error(&iter
, -EINVAL
, &local_err
);
906 if (!extent
->length
&& !iter
.request_ret
) {
907 error_setg(&local_err
, "Server did not reply with any status extents");
908 nbd_iter_channel_error(&iter
, -EIO
, &local_err
);
911 error_propagate(errp
, iter
.err
);
912 *request_ret
= iter
.request_ret
;
916 static int nbd_co_request(BlockDriverState
*bs
, NBDRequest
*request
,
917 QEMUIOVector
*write_qiov
)
919 int ret
, request_ret
;
920 Error
*local_err
= NULL
;
921 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
923 assert(request
->type
!= NBD_CMD_READ
);
925 assert(request
->type
== NBD_CMD_WRITE
);
926 assert(request
->len
== iov_size(write_qiov
->iov
, write_qiov
->niov
));
928 assert(request
->type
!= NBD_CMD_WRITE
);
930 ret
= nbd_co_send_request(bs
, request
, write_qiov
);
935 ret
= nbd_co_receive_return_code(s
, request
->handle
,
936 &request_ret
, &local_err
);
938 trace_nbd_co_request_fail(request
->from
, request
->len
, request
->handle
,
939 request
->flags
, request
->type
,
940 nbd_cmd_lookup(request
->type
),
941 ret
, error_get_pretty(local_err
));
942 error_free(local_err
);
944 return ret
? ret
: request_ret
;
947 static int nbd_client_co_preadv(BlockDriverState
*bs
, uint64_t offset
,
948 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
950 int ret
, request_ret
;
951 Error
*local_err
= NULL
;
952 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
953 NBDRequest request
= {
954 .type
= NBD_CMD_READ
,
959 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
966 * Work around the fact that the block layer doesn't do
967 * byte-accurate sizing yet - if the read exceeds the server's
968 * advertised size because the block layer rounded size up, then
969 * truncate the request to the server and tail-pad with zero.
971 if (offset
>= s
->info
.size
) {
972 assert(bytes
< BDRV_SECTOR_SIZE
);
973 qemu_iovec_memset(qiov
, 0, 0, bytes
);
976 if (offset
+ bytes
> s
->info
.size
) {
977 uint64_t slop
= offset
+ bytes
- s
->info
.size
;
979 assert(slop
< BDRV_SECTOR_SIZE
);
980 qemu_iovec_memset(qiov
, bytes
- slop
, 0, slop
);
984 ret
= nbd_co_send_request(bs
, &request
, NULL
);
989 ret
= nbd_co_receive_cmdread_reply(s
, request
.handle
, offset
, qiov
,
990 &request_ret
, &local_err
);
992 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
993 request
.flags
, request
.type
,
994 nbd_cmd_lookup(request
.type
),
995 ret
, error_get_pretty(local_err
));
996 error_free(local_err
);
998 return ret
? ret
: request_ret
;
1001 static int nbd_client_co_pwritev(BlockDriverState
*bs
, uint64_t offset
,
1002 uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
1004 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1005 NBDRequest request
= {
1006 .type
= NBD_CMD_WRITE
,
1011 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1012 if (flags
& BDRV_REQ_FUA
) {
1013 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1014 request
.flags
|= NBD_CMD_FLAG_FUA
;
1017 assert(bytes
<= NBD_MAX_BUFFER_SIZE
);
1022 return nbd_co_request(bs
, &request
, qiov
);
1025 static int nbd_client_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1026 int bytes
, BdrvRequestFlags flags
)
1028 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1029 NBDRequest request
= {
1030 .type
= NBD_CMD_WRITE_ZEROES
,
1035 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1036 if (!(s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
)) {
1040 if (flags
& BDRV_REQ_FUA
) {
1041 assert(s
->info
.flags
& NBD_FLAG_SEND_FUA
);
1042 request
.flags
|= NBD_CMD_FLAG_FUA
;
1044 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1045 request
.flags
|= NBD_CMD_FLAG_NO_HOLE
;
1047 if (flags
& BDRV_REQ_NO_FALLBACK
) {
1048 assert(s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
);
1049 request
.flags
|= NBD_CMD_FLAG_FAST_ZERO
;
1055 return nbd_co_request(bs
, &request
, NULL
);
1058 static int nbd_client_co_flush(BlockDriverState
*bs
)
1060 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1061 NBDRequest request
= { .type
= NBD_CMD_FLUSH
};
1063 if (!(s
->info
.flags
& NBD_FLAG_SEND_FLUSH
)) {
1070 return nbd_co_request(bs
, &request
, NULL
);
1073 static int nbd_client_co_pdiscard(BlockDriverState
*bs
, int64_t offset
,
1076 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1077 NBDRequest request
= {
1078 .type
= NBD_CMD_TRIM
,
1083 assert(!(s
->info
.flags
& NBD_FLAG_READ_ONLY
));
1084 if (!(s
->info
.flags
& NBD_FLAG_SEND_TRIM
) || !bytes
) {
1088 return nbd_co_request(bs
, &request
, NULL
);
1091 static int coroutine_fn
nbd_client_co_block_status(
1092 BlockDriverState
*bs
, bool want_zero
, int64_t offset
, int64_t bytes
,
1093 int64_t *pnum
, int64_t *map
, BlockDriverState
**file
)
1095 int ret
, request_ret
;
1096 NBDExtent extent
= { 0 };
1097 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1098 Error
*local_err
= NULL
;
1100 NBDRequest request
= {
1101 .type
= NBD_CMD_BLOCK_STATUS
,
1103 .len
= MIN(MIN_NON_ZERO(QEMU_ALIGN_DOWN(INT_MAX
,
1104 bs
->bl
.request_alignment
),
1106 MIN(bytes
, s
->info
.size
- offset
)),
1107 .flags
= NBD_CMD_FLAG_REQ_ONE
,
1110 if (!s
->info
.base_allocation
) {
1114 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1118 * Work around the fact that the block layer doesn't do
1119 * byte-accurate sizing yet - if the status request exceeds the
1120 * server's advertised size because the block layer rounded size
1121 * up, we truncated the request to the server (above), or are
1122 * called on just the hole.
1124 if (offset
>= s
->info
.size
) {
1126 assert(bytes
< BDRV_SECTOR_SIZE
);
1127 /* Intentionally don't report offset_valid for the hole */
1128 return BDRV_BLOCK_ZERO
;
1131 if (s
->info
.min_block
) {
1132 assert(QEMU_IS_ALIGNED(request
.len
, s
->info
.min_block
));
1134 ret
= nbd_co_send_request(bs
, &request
, NULL
);
1139 ret
= nbd_co_receive_blockstatus_reply(s
, request
.handle
, bytes
,
1140 &extent
, &request_ret
, &local_err
);
1142 trace_nbd_co_request_fail(request
.from
, request
.len
, request
.handle
,
1143 request
.flags
, request
.type
,
1144 nbd_cmd_lookup(request
.type
),
1145 ret
, error_get_pretty(local_err
));
1146 error_free(local_err
);
1148 if (ret
< 0 || request_ret
< 0) {
1149 return ret
? ret
: request_ret
;
1152 assert(extent
.length
);
1153 *pnum
= extent
.length
;
1156 return (extent
.flags
& NBD_STATE_HOLE
? 0 : BDRV_BLOCK_DATA
) |
1157 (extent
.flags
& NBD_STATE_ZERO
? BDRV_BLOCK_ZERO
: 0) |
1158 BDRV_BLOCK_OFFSET_VALID
;
1161 static void nbd_client_close(BlockDriverState
*bs
)
1163 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1164 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1168 nbd_send_request(s
->ioc
, &request
);
1170 nbd_teardown_connection(bs
);
1173 static QIOChannelSocket
*nbd_establish_connection(SocketAddress
*saddr
,
1176 QIOChannelSocket
*sioc
;
1177 Error
*local_err
= NULL
;
1179 sioc
= qio_channel_socket_new();
1180 qio_channel_set_name(QIO_CHANNEL(sioc
), "nbd-client");
1182 qio_channel_socket_connect_sync(sioc
, saddr
, &local_err
);
1184 object_unref(OBJECT(sioc
));
1185 error_propagate(errp
, local_err
);
1189 qio_channel_set_delay(QIO_CHANNEL(sioc
), false);
1194 static int nbd_client_connect(BlockDriverState
*bs
, Error
**errp
)
1196 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1197 AioContext
*aio_context
= bdrv_get_aio_context(bs
);
1201 * establish TCP connection, return error if it fails
1202 * TODO: Configurable retry-until-timeout behaviour.
1204 QIOChannelSocket
*sioc
= nbd_establish_connection(s
->saddr
, errp
);
1207 return -ECONNREFUSED
;
1211 trace_nbd_client_connect(s
->export
);
1212 qio_channel_set_blocking(QIO_CHANNEL(sioc
), false, NULL
);
1213 qio_channel_attach_aio_context(QIO_CHANNEL(sioc
), aio_context
);
1215 s
->info
.request_sizes
= true;
1216 s
->info
.structured_reply
= true;
1217 s
->info
.base_allocation
= true;
1218 s
->info
.x_dirty_bitmap
= g_strdup(s
->x_dirty_bitmap
);
1219 s
->info
.name
= g_strdup(s
->export
?: "");
1220 ret
= nbd_receive_negotiate(aio_context
, QIO_CHANNEL(sioc
), s
->tlscreds
,
1221 s
->hostname
, &s
->ioc
, &s
->info
, errp
);
1222 g_free(s
->info
.x_dirty_bitmap
);
1223 g_free(s
->info
.name
);
1225 object_unref(OBJECT(sioc
));
1228 if (s
->x_dirty_bitmap
&& !s
->info
.base_allocation
) {
1229 error_setg(errp
, "requested x-dirty-bitmap %s not found",
1234 if (s
->info
.flags
& NBD_FLAG_READ_ONLY
) {
1235 ret
= bdrv_apply_auto_read_only(bs
, "NBD export is read-only", errp
);
1240 if (s
->info
.flags
& NBD_FLAG_SEND_FUA
) {
1241 bs
->supported_write_flags
= BDRV_REQ_FUA
;
1242 bs
->supported_zero_flags
|= BDRV_REQ_FUA
;
1244 if (s
->info
.flags
& NBD_FLAG_SEND_WRITE_ZEROES
) {
1245 bs
->supported_zero_flags
|= BDRV_REQ_MAY_UNMAP
;
1246 if (s
->info
.flags
& NBD_FLAG_SEND_FAST_ZERO
) {
1247 bs
->supported_zero_flags
|= BDRV_REQ_NO_FALLBACK
;
1254 s
->ioc
= QIO_CHANNEL(sioc
);
1255 object_ref(OBJECT(s
->ioc
));
1258 trace_nbd_client_connect_success(s
->export
);
1264 * We have connected, but must fail for other reasons.
1265 * Send NBD_CMD_DISC as a courtesy to the server.
1268 NBDRequest request
= { .type
= NBD_CMD_DISC
};
1270 nbd_send_request(s
->ioc
?: QIO_CHANNEL(sioc
), &request
);
1272 object_unref(OBJECT(sioc
));
1279 * Parse nbd_open options
1282 static int nbd_parse_uri(const char *filename
, QDict
*options
)
1286 QueryParams
*qp
= NULL
;
1290 uri
= uri_parse(filename
);
1296 if (!g_strcmp0(uri
->scheme
, "nbd")) {
1298 } else if (!g_strcmp0(uri
->scheme
, "nbd+tcp")) {
1300 } else if (!g_strcmp0(uri
->scheme
, "nbd+unix")) {
1307 p
= uri
->path
? uri
->path
: "/";
1308 p
+= strspn(p
, "/");
1310 qdict_put_str(options
, "export", p
);
1313 qp
= query_params_parse(uri
->query
);
1314 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
1320 /* nbd+unix:///export?socket=path */
1321 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
1325 qdict_put_str(options
, "server.type", "unix");
1326 qdict_put_str(options
, "server.path", qp
->p
[0].value
);
1331 /* nbd[+tcp]://host[:port]/export */
1337 /* strip braces from literal IPv6 address */
1338 if (uri
->server
[0] == '[') {
1339 host
= qstring_from_substr(uri
->server
, 1,
1340 strlen(uri
->server
) - 1);
1342 host
= qstring_from_str(uri
->server
);
1345 qdict_put_str(options
, "server.type", "inet");
1346 qdict_put(options
, "server.host", host
);
1348 port_str
= g_strdup_printf("%d", uri
->port
?: NBD_DEFAULT_PORT
);
1349 qdict_put_str(options
, "server.port", port_str
);
1355 query_params_free(qp
);
1361 static bool nbd_has_filename_options_conflict(QDict
*options
, Error
**errp
)
1363 const QDictEntry
*e
;
1365 for (e
= qdict_first(options
); e
; e
= qdict_next(options
, e
)) {
1366 if (!strcmp(e
->key
, "host") ||
1367 !strcmp(e
->key
, "port") ||
1368 !strcmp(e
->key
, "path") ||
1369 !strcmp(e
->key
, "export") ||
1370 strstart(e
->key
, "server.", NULL
))
1372 error_setg(errp
, "Option '%s' cannot be used with a file name",
1381 static void nbd_parse_filename(const char *filename
, QDict
*options
,
1384 g_autofree
char *file
= NULL
;
1386 const char *host_spec
;
1387 const char *unixpath
;
1389 if (nbd_has_filename_options_conflict(options
, errp
)) {
1393 if (strstr(filename
, "://")) {
1394 int ret
= nbd_parse_uri(filename
, options
);
1396 error_setg(errp
, "No valid URL specified");
1401 file
= g_strdup(filename
);
1403 export_name
= strstr(file
, EN_OPTSTR
);
1405 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
1408 export_name
[0] = 0; /* truncate 'file' */
1409 export_name
+= strlen(EN_OPTSTR
);
1411 qdict_put_str(options
, "export", export_name
);
1414 /* extract the host_spec - fail if it's not nbd:... */
1415 if (!strstart(file
, "nbd:", &host_spec
)) {
1416 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
1424 /* are we a UNIX or TCP socket? */
1425 if (strstart(host_spec
, "unix:", &unixpath
)) {
1426 qdict_put_str(options
, "server.type", "unix");
1427 qdict_put_str(options
, "server.path", unixpath
);
1429 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
1431 if (inet_parse(addr
, host_spec
, errp
)) {
1435 qdict_put_str(options
, "server.type", "inet");
1436 qdict_put_str(options
, "server.host", addr
->host
);
1437 qdict_put_str(options
, "server.port", addr
->port
);
1439 qapi_free_InetSocketAddress(addr
);
1443 static bool nbd_process_legacy_socket_options(QDict
*output_options
,
1444 QemuOpts
*legacy_opts
,
1447 const char *path
= qemu_opt_get(legacy_opts
, "path");
1448 const char *host
= qemu_opt_get(legacy_opts
, "host");
1449 const char *port
= qemu_opt_get(legacy_opts
, "port");
1450 const QDictEntry
*e
;
1452 if (!path
&& !host
&& !port
) {
1456 for (e
= qdict_first(output_options
); e
; e
= qdict_next(output_options
, e
))
1458 if (strstart(e
->key
, "server.", NULL
)) {
1459 error_setg(errp
, "Cannot use 'server' and path/host/port at the "
1466 error_setg(errp
, "path and host may not be used at the same time");
1470 error_setg(errp
, "port may not be used without host");
1474 qdict_put_str(output_options
, "server.type", "unix");
1475 qdict_put_str(output_options
, "server.path", path
);
1477 qdict_put_str(output_options
, "server.type", "inet");
1478 qdict_put_str(output_options
, "server.host", host
);
1479 qdict_put_str(output_options
, "server.port",
1480 port
?: stringify(NBD_DEFAULT_PORT
));
1486 static SocketAddress
*nbd_config(BDRVNBDState
*s
, QDict
*options
,
1489 SocketAddress
*saddr
= NULL
;
1492 Error
*local_err
= NULL
;
1494 qdict_extract_subqdict(options
, &addr
, "server.");
1495 if (!qdict_size(addr
)) {
1496 error_setg(errp
, "NBD server address missing");
1500 iv
= qobject_input_visitor_new_flat_confused(addr
, errp
);
1505 visit_type_SocketAddress(iv
, NULL
, &saddr
, &local_err
);
1507 error_propagate(errp
, local_err
);
1512 qobject_unref(addr
);
1517 static QCryptoTLSCreds
*nbd_get_tls_creds(const char *id
, Error
**errp
)
1520 QCryptoTLSCreds
*creds
;
1522 obj
= object_resolve_path_component(
1523 object_get_objects_root(), id
);
1525 error_setg(errp
, "No TLS credentials with id '%s'",
1529 creds
= (QCryptoTLSCreds
*)
1530 object_dynamic_cast(obj
, TYPE_QCRYPTO_TLS_CREDS
);
1532 error_setg(errp
, "Object with id '%s' is not TLS credentials",
1537 if (creds
->endpoint
!= QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
) {
1539 "Expecting TLS credentials with a client endpoint");
1547 static QemuOptsList nbd_runtime_opts
= {
1549 .head
= QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts
.head
),
1553 .type
= QEMU_OPT_STRING
,
1554 .help
= "TCP host to connect to",
1558 .type
= QEMU_OPT_STRING
,
1559 .help
= "TCP port to connect to",
1563 .type
= QEMU_OPT_STRING
,
1564 .help
= "Unix socket path to connect to",
1568 .type
= QEMU_OPT_STRING
,
1569 .help
= "Name of the NBD export to open",
1572 .name
= "tls-creds",
1573 .type
= QEMU_OPT_STRING
,
1574 .help
= "ID of the TLS credentials to use",
1577 .name
= "x-dirty-bitmap",
1578 .type
= QEMU_OPT_STRING
,
1579 .help
= "experimental: expose named dirty bitmap in place of "
1583 .name
= "reconnect-delay",
1584 .type
= QEMU_OPT_NUMBER
,
1585 .help
= "On an unexpected disconnect, the nbd client tries to "
1586 "connect again until succeeding or encountering a serious "
1587 "error. During the first @reconnect-delay seconds, all "
1588 "requests are paused and will be rerun on a successful "
1589 "reconnect. After that time, any delayed requests and all "
1590 "future requests before a successful reconnect will "
1591 "immediately fail. Default 0",
1593 { /* end of list */ }
1597 static int nbd_process_options(BlockDriverState
*bs
, QDict
*options
,
1600 BDRVNBDState
*s
= bs
->opaque
;
1602 Error
*local_err
= NULL
;
1605 opts
= qemu_opts_create(&nbd_runtime_opts
, NULL
, 0, &error_abort
);
1606 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
1608 error_propagate(errp
, local_err
);
1612 /* Translate @host, @port, and @path to a SocketAddress */
1613 if (!nbd_process_legacy_socket_options(options
, opts
, errp
)) {
1617 /* Pop the config into our state object. Exit if invalid. */
1618 s
->saddr
= nbd_config(s
, options
, errp
);
1623 s
->export
= g_strdup(qemu_opt_get(opts
, "export"));
1625 s
->tlscredsid
= g_strdup(qemu_opt_get(opts
, "tls-creds"));
1626 if (s
->tlscredsid
) {
1627 s
->tlscreds
= nbd_get_tls_creds(s
->tlscredsid
, errp
);
1632 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1633 if (s
->saddr
->type
!= SOCKET_ADDRESS_TYPE_INET
) {
1634 error_setg(errp
, "TLS only supported over IP sockets");
1637 s
->hostname
= s
->saddr
->u
.inet
.host
;
1640 s
->x_dirty_bitmap
= g_strdup(qemu_opt_get(opts
, "x-dirty-bitmap"));
1641 s
->reconnect_delay
= qemu_opt_get_number(opts
, "reconnect-delay", 0);
1647 object_unref(OBJECT(s
->tlscreds
));
1648 qapi_free_SocketAddress(s
->saddr
);
1650 g_free(s
->tlscredsid
);
1652 qemu_opts_del(opts
);
1656 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
1660 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1662 ret
= nbd_process_options(bs
, options
, errp
);
1668 qemu_co_mutex_init(&s
->send_mutex
);
1669 qemu_co_queue_init(&s
->free_sema
);
1671 ret
= nbd_client_connect(bs
, errp
);
1675 /* successfully connected */
1676 s
->state
= NBD_CLIENT_CONNECTED
;
1678 s
->connection_co
= qemu_coroutine_create(nbd_connection_entry
, s
);
1679 bdrv_inc_in_flight(bs
);
1680 aio_co_schedule(bdrv_get_aio_context(bs
), s
->connection_co
);
1685 static int nbd_co_flush(BlockDriverState
*bs
)
1687 return nbd_client_co_flush(bs
);
1690 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
1692 BDRVNBDState
*s
= (BDRVNBDState
*)bs
->opaque
;
1693 uint32_t min
= s
->info
.min_block
;
1694 uint32_t max
= MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE
, s
->info
.max_block
);
1697 * If the server did not advertise an alignment:
1698 * - a size that is not sector-aligned implies that an alignment
1699 * of 1 can be used to access those tail bytes
1700 * - advertisement of block status requires an alignment of 1, so
1701 * that we don't violate block layer constraints that block
1702 * status is always aligned (as we can't control whether the
1703 * server will report sub-sector extents, such as a hole at EOF
1704 * on an unaligned POSIX file)
1705 * - otherwise, assume the server is so old that we are safer avoiding
1706 * sub-sector requests
1709 min
= (!QEMU_IS_ALIGNED(s
->info
.size
, BDRV_SECTOR_SIZE
) ||
1710 s
->info
.base_allocation
) ? 1 : BDRV_SECTOR_SIZE
;
1713 bs
->bl
.request_alignment
= min
;
1714 bs
->bl
.max_pdiscard
= max
;
1715 bs
->bl
.max_pwrite_zeroes
= max
;
1716 bs
->bl
.max_transfer
= max
;
1718 if (s
->info
.opt_block
&&
1719 s
->info
.opt_block
> bs
->bl
.opt_transfer
) {
1720 bs
->bl
.opt_transfer
= s
->info
.opt_block
;
1724 static void nbd_close(BlockDriverState
*bs
)
1726 BDRVNBDState
*s
= bs
->opaque
;
1728 nbd_client_close(bs
);
1730 object_unref(OBJECT(s
->tlscreds
));
1731 qapi_free_SocketAddress(s
->saddr
);
1733 g_free(s
->tlscredsid
);
1734 g_free(s
->x_dirty_bitmap
);
1737 static int64_t nbd_getlength(BlockDriverState
*bs
)
1739 BDRVNBDState
*s
= bs
->opaque
;
1741 return s
->info
.size
;
1744 static void nbd_refresh_filename(BlockDriverState
*bs
)
1746 BDRVNBDState
*s
= bs
->opaque
;
1747 const char *host
= NULL
, *port
= NULL
, *path
= NULL
;
1749 if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_INET
) {
1750 const InetSocketAddress
*inet
= &s
->saddr
->u
.inet
;
1751 if (!inet
->has_ipv4
&& !inet
->has_ipv6
&& !inet
->has_to
) {
1755 } else if (s
->saddr
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
1756 path
= s
->saddr
->u
.q_unix
.path
;
1757 } /* else can't represent as pseudo-filename */
1759 if (path
&& s
->export
) {
1760 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
1761 "nbd+unix:///%s?socket=%s", s
->export
, path
);
1762 } else if (path
&& !s
->export
) {
1763 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
1764 "nbd+unix://?socket=%s", path
);
1765 } else if (host
&& s
->export
) {
1766 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
1767 "nbd://%s:%s/%s", host
, port
, s
->export
);
1768 } else if (host
&& !s
->export
) {
1769 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
1770 "nbd://%s:%s", host
, port
);
1774 static char *nbd_dirname(BlockDriverState
*bs
, Error
**errp
)
1776 /* The generic bdrv_dirname() implementation is able to work out some
1777 * directory name for NBD nodes, but that would be wrong. So far there is no
1778 * specification for how "export paths" would work, so NBD does not have
1779 * directory names. */
1780 error_setg(errp
, "Cannot generate a base directory for NBD nodes");
1784 static const char *const nbd_strong_runtime_opts
[] = {
1795 static BlockDriver bdrv_nbd
= {
1796 .format_name
= "nbd",
1797 .protocol_name
= "nbd",
1798 .instance_size
= sizeof(BDRVNBDState
),
1799 .bdrv_parse_filename
= nbd_parse_filename
,
1800 .bdrv_file_open
= nbd_open
,
1801 .bdrv_co_preadv
= nbd_client_co_preadv
,
1802 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
1803 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
1804 .bdrv_close
= nbd_close
,
1805 .bdrv_co_flush_to_os
= nbd_co_flush
,
1806 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
1807 .bdrv_refresh_limits
= nbd_refresh_limits
,
1808 .bdrv_getlength
= nbd_getlength
,
1809 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
1810 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
1811 .bdrv_refresh_filename
= nbd_refresh_filename
,
1812 .bdrv_co_block_status
= nbd_client_co_block_status
,
1813 .bdrv_dirname
= nbd_dirname
,
1814 .strong_runtime_opts
= nbd_strong_runtime_opts
,
1817 static BlockDriver bdrv_nbd_tcp
= {
1818 .format_name
= "nbd",
1819 .protocol_name
= "nbd+tcp",
1820 .instance_size
= sizeof(BDRVNBDState
),
1821 .bdrv_parse_filename
= nbd_parse_filename
,
1822 .bdrv_file_open
= nbd_open
,
1823 .bdrv_co_preadv
= nbd_client_co_preadv
,
1824 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
1825 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
1826 .bdrv_close
= nbd_close
,
1827 .bdrv_co_flush_to_os
= nbd_co_flush
,
1828 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
1829 .bdrv_refresh_limits
= nbd_refresh_limits
,
1830 .bdrv_getlength
= nbd_getlength
,
1831 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
1832 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
1833 .bdrv_refresh_filename
= nbd_refresh_filename
,
1834 .bdrv_co_block_status
= nbd_client_co_block_status
,
1835 .bdrv_dirname
= nbd_dirname
,
1836 .strong_runtime_opts
= nbd_strong_runtime_opts
,
1839 static BlockDriver bdrv_nbd_unix
= {
1840 .format_name
= "nbd",
1841 .protocol_name
= "nbd+unix",
1842 .instance_size
= sizeof(BDRVNBDState
),
1843 .bdrv_parse_filename
= nbd_parse_filename
,
1844 .bdrv_file_open
= nbd_open
,
1845 .bdrv_co_preadv
= nbd_client_co_preadv
,
1846 .bdrv_co_pwritev
= nbd_client_co_pwritev
,
1847 .bdrv_co_pwrite_zeroes
= nbd_client_co_pwrite_zeroes
,
1848 .bdrv_close
= nbd_close
,
1849 .bdrv_co_flush_to_os
= nbd_co_flush
,
1850 .bdrv_co_pdiscard
= nbd_client_co_pdiscard
,
1851 .bdrv_refresh_limits
= nbd_refresh_limits
,
1852 .bdrv_getlength
= nbd_getlength
,
1853 .bdrv_detach_aio_context
= nbd_client_detach_aio_context
,
1854 .bdrv_attach_aio_context
= nbd_client_attach_aio_context
,
1855 .bdrv_refresh_filename
= nbd_refresh_filename
,
1856 .bdrv_co_block_status
= nbd_client_co_block_status
,
1857 .bdrv_dirname
= nbd_dirname
,
1858 .strong_runtime_opts
= nbd_strong_runtime_opts
,
1861 static void bdrv_nbd_init(void)
1863 bdrv_register(&bdrv_nbd
);
1864 bdrv_register(&bdrv_nbd_tcp
);
1865 bdrv_register(&bdrv_nbd_unix
);
1868 block_init(bdrv_nbd_init
);