2 * QEMU Block driver for NBD
4 * Copyright (C) 2016 Red Hat, Inc.
5 * Copyright (C) 2008 Bull S.A.S.
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"
37 #include "qapi/qapi-visit-sockets.h"
38 #include "qapi/qmp/qstring.h"
40 #include "block/qdict.h"
41 #include "block/nbd.h"
42 #include "block/block_int.h"
44 #define EN_OPTSTR ":exportname="
45 #define MAX_NBD_REQUESTS 16
47 #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs))
48 #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs))
52 uint64_t offset; /* original offset of the request */
53 bool receiving; /* waiting for connection_co? */
56 typedef enum NBDClientState {
61 typedef struct BDRVNBDState {
62 QIOChannelSocket *sioc; /* The master data channel */
63 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
68 Coroutine *connection_co;
72 NBDClientRequest requests[MAX_NBD_REQUESTS];
76 /* Connection parameters */
77 uint32_t reconnect_delay;
79 char *export, *tlscredsid;
80 QCryptoTLSCreds *tlscreds;
85 /* @ret will be used for reconnect in future */
86 static void nbd_channel_error(BDRVNBDState *s, int ret)
88 s->state = NBD_CLIENT_QUIT;
91 static void nbd_recv_coroutines_wake_all(BDRVNBDState *s)
95 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
96 NBDClientRequest *req = &s->requests[i];
98 if (req->coroutine && req->receiving) {
99 aio_co_wake(req->coroutine);
104 static void nbd_client_detach_aio_context(BlockDriverState *bs)
106 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
108 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
111 static void nbd_client_attach_aio_context_bh(void *opaque)
113 BlockDriverState *bs = opaque;
114 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
117 * The node is still drained, so we know the coroutine has yielded in
118 * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is
119 * entered for the first time. Both places are safe for entering the
122 qemu_aio_coroutine_enter(bs->aio_context, s->connection_co);
123 bdrv_dec_in_flight(bs);
126 static void nbd_client_attach_aio_context(BlockDriverState *bs,
127 AioContext *new_context)
129 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
131 qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context);
133 bdrv_inc_in_flight(bs);
136 * Need to wait here for the BH to run because the BH must run while the
137 * node is still drained.
139 aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs);
143 static void nbd_teardown_connection(BlockDriverState *bs)
145 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
149 /* finish any pending coroutines */
150 qio_channel_shutdown(s->ioc,
151 QIO_CHANNEL_SHUTDOWN_BOTH,
153 BDRV_POLL_WHILE(bs, s->connection_co);
155 nbd_client_detach_aio_context(bs);
156 object_unref(OBJECT(s->sioc));
158 object_unref(OBJECT(s->ioc));
162 static coroutine_fn void nbd_connection_entry(void *opaque)
164 BDRVNBDState *s = opaque;
167 Error *local_err = NULL;
169 while (s->state != NBD_CLIENT_QUIT) {
171 * The NBD client can only really be considered idle when it has
172 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
173 * the point where the additional scheduled coroutine entry happens
174 * after nbd_client_attach_aio_context().
176 * Therefore we keep an additional in_flight reference all the time and
177 * only drop it temporarily here.
179 assert(s->reply.handle == 0);
180 ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err);
183 trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err));
184 error_free(local_err);
187 nbd_channel_error(s, ret ? ret : -EIO);
192 * There's no need for a mutex on the receive side, because the
193 * handler acts as a synchronization point and ensures that only
194 * one coroutine is called until the reply finishes.
196 i = HANDLE_TO_INDEX(s, s->reply.handle);
197 if (i >= MAX_NBD_REQUESTS ||
198 !s->requests[i].coroutine ||
199 !s->requests[i].receiving ||
200 (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply))
202 nbd_channel_error(s, -EINVAL);
207 * We're woken up again by the request itself. Note that there
208 * is no race between yielding and reentering connection_co. This
211 * - if the request runs on the same AioContext, it is only
212 * entered after we yield
214 * - if the request runs on a different AioContext, reentering
215 * connection_co happens through a bottom half, which can only
216 * run after we yield.
218 aio_co_wake(s->requests[i].coroutine);
219 qemu_coroutine_yield();
222 nbd_recv_coroutines_wake_all(s);
223 bdrv_dec_in_flight(s->bs);
225 s->connection_co = NULL;
229 static int nbd_co_send_request(BlockDriverState *bs,
233 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
236 qemu_co_mutex_lock(&s->send_mutex);
237 while (s->in_flight == MAX_NBD_REQUESTS) {
238 qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
241 if (s->state != NBD_CLIENT_CONNECTED) {
248 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
249 if (s->requests[i].coroutine == NULL) {
254 g_assert(qemu_in_coroutine());
255 assert(i < MAX_NBD_REQUESTS);
257 s->requests[i].coroutine = qemu_coroutine_self();
258 s->requests[i].offset = request->from;
259 s->requests[i].receiving = false;
261 request->handle = INDEX_TO_HANDLE(s, i);
266 qio_channel_set_cork(s->ioc, true);
267 rc = nbd_send_request(s->ioc, request);
268 if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) {
269 if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
273 } else if (rc >= 0) {
276 qio_channel_set_cork(s->ioc, false);
278 rc = nbd_send_request(s->ioc, request);
283 nbd_channel_error(s, rc);
285 s->requests[i].coroutine = NULL;
288 qemu_co_queue_next(&s->free_sema);
290 qemu_co_mutex_unlock(&s->send_mutex);
294 static inline uint16_t payload_advance16(uint8_t **payload)
297 return lduw_be_p(*payload - 2);
300 static inline uint32_t payload_advance32(uint8_t **payload)
303 return ldl_be_p(*payload - 4);
306 static inline uint64_t payload_advance64(uint8_t **payload)
309 return ldq_be_p(*payload - 8);
312 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
313 NBDStructuredReplyChunk *chunk,
314 uint8_t *payload, uint64_t orig_offset,
315 QEMUIOVector *qiov, Error **errp)
320 if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
321 error_setg(errp, "Protocol error: invalid payload for "
322 "NBD_REPLY_TYPE_OFFSET_HOLE");
326 offset = payload_advance64(&payload);
327 hole_size = payload_advance32(&payload);
329 if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
330 offset > orig_offset + qiov->size - hole_size) {
331 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
335 if (s->info.min_block &&
336 !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
337 trace_nbd_structured_read_compliance("hole");
340 qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
346 * nbd_parse_blockstatus_payload
347 * Based on our request, we expect only one extent in reply, for the
348 * base:allocation context.
350 static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
351 NBDStructuredReplyChunk *chunk,
352 uint8_t *payload, uint64_t orig_length,
353 NBDExtent *extent, Error **errp)
357 /* The server succeeded, so it must have sent [at least] one extent */
358 if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
359 error_setg(errp, "Protocol error: invalid payload for "
360 "NBD_REPLY_TYPE_BLOCK_STATUS");
364 context_id = payload_advance32(&payload);
365 if (s->info.context_id != context_id) {
366 error_setg(errp, "Protocol error: unexpected context id %d for "
367 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
368 "id is %d", context_id,
373 extent->length = payload_advance32(&payload);
374 extent->flags = payload_advance32(&payload);
376 if (extent->length == 0) {
377 error_setg(errp, "Protocol error: server sent status chunk with "
383 * A server sending unaligned block status is in violation of the
384 * protocol, but as qemu-nbd 3.1 is such a server (at least for
385 * POSIX files that are not a multiple of 512 bytes, since qemu
386 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
387 * still sees an implicit hole beyond the real EOF), it's nicer to
388 * work around the misbehaving server. If the request included
389 * more than the final unaligned block, truncate it back to an
390 * aligned result; if the request was only the final block, round
391 * up to the full block and change the status to fully-allocated
392 * (always a safe status, even if it loses information).
394 if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
395 s->info.min_block)) {
396 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
397 if (extent->length > s->info.min_block) {
398 extent->length = QEMU_ALIGN_DOWN(extent->length,
401 extent->length = s->info.min_block;
407 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
408 * sent us any more than one extent, nor should it have included
409 * status beyond our request in that extent. However, it's easy
410 * enough to ignore the server's noncompliance without killing the
411 * connection; just ignore trailing extents, and clamp things to
412 * the length of our request.
414 if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
415 trace_nbd_parse_blockstatus_compliance("more than one extent");
417 if (extent->length > orig_length) {
418 extent->length = orig_length;
419 trace_nbd_parse_blockstatus_compliance("extent length too large");
426 * nbd_parse_error_payload
427 * on success @errp contains message describing nbd error reply
429 static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
430 uint8_t *payload, int *request_ret,
434 uint16_t message_size;
436 assert(chunk->type & (1 << 15));
438 if (chunk->length < sizeof(error) + sizeof(message_size)) {
440 "Protocol error: invalid payload for structured error");
444 error = nbd_errno_to_system_errno(payload_advance32(&payload));
446 error_setg(errp, "Protocol error: server sent structured error chunk "
451 *request_ret = -error;
452 message_size = payload_advance16(&payload);
454 if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
455 error_setg(errp, "Protocol error: server sent structured error chunk "
456 "with incorrect message size");
460 /* TODO: Add a trace point to mention the server complaint */
462 /* TODO handle ERROR_OFFSET */
467 static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
468 uint64_t orig_offset,
469 QEMUIOVector *qiov, Error **errp)
471 QEMUIOVector sub_qiov;
475 NBDStructuredReplyChunk *chunk = &s->reply.structured;
477 assert(nbd_reply_is_structured(&s->reply));
479 /* The NBD spec requires at least one byte of payload */
480 if (chunk->length <= sizeof(offset)) {
481 error_setg(errp, "Protocol error: invalid payload for "
482 "NBD_REPLY_TYPE_OFFSET_DATA");
486 if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
490 data_size = chunk->length - sizeof(offset);
492 if (offset < orig_offset || data_size > qiov->size ||
493 offset > orig_offset + qiov->size - data_size) {
494 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
498 if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
499 trace_nbd_structured_read_compliance("data");
502 qemu_iovec_init(&sub_qiov, qiov->niov);
503 qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
504 ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
505 qemu_iovec_destroy(&sub_qiov);
507 return ret < 0 ? -EIO : 0;
510 #define NBD_MAX_MALLOC_PAYLOAD 1000
511 static coroutine_fn int nbd_co_receive_structured_payload(
512 BDRVNBDState *s, void **payload, Error **errp)
517 assert(nbd_reply_is_structured(&s->reply));
519 len = s->reply.structured.length;
525 if (payload == NULL) {
526 error_setg(errp, "Unexpected structured payload");
530 if (len > NBD_MAX_MALLOC_PAYLOAD) {
531 error_setg(errp, "Payload too large");
535 *payload = g_new(char, len);
536 ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
547 * nbd_co_do_receive_one_chunk
549 * set request_ret to received reply error
550 * if qiov is not NULL: read payload to @qiov
551 * for structured reply chunk:
552 * if error chunk: read payload, set @request_ret, do not set @payload
553 * else if offset_data chunk: read payload data to @qiov, do not set @payload
554 * else: read payload to @payload
556 * If function fails, @errp contains corresponding error message, and the
557 * connection with the server is suspect. If it returns 0, then the
558 * transaction succeeded (although @request_ret may be a negative errno
559 * corresponding to the server's error reply), and errp is unchanged.
561 static coroutine_fn int nbd_co_do_receive_one_chunk(
562 BDRVNBDState *s, uint64_t handle, bool only_structured,
563 int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
566 int i = HANDLE_TO_INDEX(s, handle);
567 void *local_payload = NULL;
568 NBDStructuredReplyChunk *chunk;
575 /* Wait until we're woken up by nbd_connection_entry. */
576 s->requests[i].receiving = true;
577 qemu_coroutine_yield();
578 s->requests[i].receiving = false;
579 if (s->state != NBD_CLIENT_CONNECTED) {
580 error_setg(errp, "Connection closed");
585 assert(s->reply.handle == handle);
587 if (nbd_reply_is_simple(&s->reply)) {
588 if (only_structured) {
589 error_setg(errp, "Protocol error: simple reply when structured "
590 "reply chunk was expected");
594 *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
595 if (*request_ret < 0 || !qiov) {
599 return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
600 errp) < 0 ? -EIO : 0;
603 /* handle structured reply chunk */
604 assert(s->info.structured_reply);
605 chunk = &s->reply.structured;
607 if (chunk->type == NBD_REPLY_TYPE_NONE) {
608 if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
609 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
610 " NBD_REPLY_FLAG_DONE flag set");
614 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
621 if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
623 error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
627 return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
631 if (nbd_reply_type_is_error(chunk->type)) {
632 payload = &local_payload;
635 ret = nbd_co_receive_structured_payload(s, payload, errp);
640 if (nbd_reply_type_is_error(chunk->type)) {
641 ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
642 g_free(local_payload);
650 * nbd_co_receive_one_chunk
651 * Read reply, wake up connection_co and set s->quit if needed.
652 * Return value is a fatal error code or normal nbd reply error code
654 static coroutine_fn int nbd_co_receive_one_chunk(
655 BDRVNBDState *s, uint64_t handle, bool only_structured,
656 int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
659 int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
660 request_ret, qiov, payload, errp);
663 memset(reply, 0, sizeof(*reply));
664 nbd_channel_error(s, ret);
666 /* For assert at loop start in nbd_connection_entry */
671 if (s->connection_co) {
672 aio_co_wake(s->connection_co);
678 typedef struct NBDReplyChunkIter {
682 bool done, only_structured;
685 static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
686 int ret, Error **local_err)
692 error_propagate(&iter->err, *local_err);
694 error_free(*local_err);
700 static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
704 if (!iter->request_ret) {
705 iter->request_ret = ret;
710 * NBD_FOREACH_REPLY_CHUNK
711 * The pointer stored in @payload requires g_free() to free it.
713 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
714 qiov, reply, payload) \
715 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
716 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
719 * nbd_reply_chunk_iter_receive
720 * The pointer stored in @payload requires g_free() to free it.
722 static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
723 NBDReplyChunkIter *iter,
725 QEMUIOVector *qiov, NBDReply *reply,
728 int ret, request_ret;
729 NBDReply local_reply;
730 NBDStructuredReplyChunk *chunk;
731 Error *local_err = NULL;
732 if (s->state != NBD_CLIENT_CONNECTED) {
733 error_setg(&local_err, "Connection closed");
734 nbd_iter_channel_error(iter, -EIO, &local_err);
739 /* Previous iteration was last. */
744 reply = &local_reply;
747 ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
748 &request_ret, qiov, reply, payload,
751 nbd_iter_channel_error(iter, ret, &local_err);
752 } else if (request_ret < 0) {
753 nbd_iter_request_error(iter, request_ret);
756 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
757 if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) {
761 chunk = &reply->structured;
762 iter->only_structured = true;
764 if (chunk->type == NBD_REPLY_TYPE_NONE) {
765 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
766 assert(chunk->flags & NBD_REPLY_FLAG_DONE);
770 if (chunk->flags & NBD_REPLY_FLAG_DONE) {
771 /* This iteration is last. */
775 /* Execute the loop body */
779 s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
781 qemu_co_mutex_lock(&s->send_mutex);
783 qemu_co_queue_next(&s->free_sema);
784 qemu_co_mutex_unlock(&s->send_mutex);
789 static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
790 int *request_ret, Error **errp)
792 NBDReplyChunkIter iter;
794 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
795 /* nbd_reply_chunk_iter_receive does all the work */
798 error_propagate(errp, iter.err);
799 *request_ret = iter.request_ret;
803 static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
804 uint64_t offset, QEMUIOVector *qiov,
805 int *request_ret, Error **errp)
807 NBDReplyChunkIter iter;
809 void *payload = NULL;
810 Error *local_err = NULL;
812 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
813 qiov, &reply, &payload)
816 NBDStructuredReplyChunk *chunk = &reply.structured;
818 assert(nbd_reply_is_structured(&reply));
820 switch (chunk->type) {
821 case NBD_REPLY_TYPE_OFFSET_DATA:
823 * special cased in nbd_co_receive_one_chunk, data is already
827 case NBD_REPLY_TYPE_OFFSET_HOLE:
828 ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
829 offset, qiov, &local_err);
831 nbd_channel_error(s, ret);
832 nbd_iter_channel_error(&iter, ret, &local_err);
836 if (!nbd_reply_type_is_error(chunk->type)) {
837 /* not allowed reply type */
838 nbd_channel_error(s, -EINVAL);
839 error_setg(&local_err,
840 "Unexpected reply type: %d (%s) for CMD_READ",
841 chunk->type, nbd_reply_type_lookup(chunk->type));
842 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
850 error_propagate(errp, iter.err);
851 *request_ret = iter.request_ret;
855 static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
856 uint64_t handle, uint64_t length,
858 int *request_ret, Error **errp)
860 NBDReplyChunkIter iter;
862 void *payload = NULL;
863 Error *local_err = NULL;
864 bool received = false;
866 assert(!extent->length);
867 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
869 NBDStructuredReplyChunk *chunk = &reply.structured;
871 assert(nbd_reply_is_structured(&reply));
873 switch (chunk->type) {
874 case NBD_REPLY_TYPE_BLOCK_STATUS:
876 nbd_channel_error(s, -EINVAL);
877 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
878 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
882 ret = nbd_parse_blockstatus_payload(s, &reply.structured,
883 payload, length, extent,
886 nbd_channel_error(s, ret);
887 nbd_iter_channel_error(&iter, ret, &local_err);
891 if (!nbd_reply_type_is_error(chunk->type)) {
892 nbd_channel_error(s, -EINVAL);
893 error_setg(&local_err,
894 "Unexpected reply type: %d (%s) "
895 "for CMD_BLOCK_STATUS",
896 chunk->type, nbd_reply_type_lookup(chunk->type));
897 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
905 if (!extent->length && !iter.request_ret) {
906 error_setg(&local_err, "Server did not reply with any status extents");
907 nbd_iter_channel_error(&iter, -EIO, &local_err);
910 error_propagate(errp, iter.err);
911 *request_ret = iter.request_ret;
915 static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
916 QEMUIOVector *write_qiov)
918 int ret, request_ret;
919 Error *local_err = NULL;
920 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
922 assert(request->type != NBD_CMD_READ);
924 assert(request->type == NBD_CMD_WRITE);
925 assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
927 assert(request->type != NBD_CMD_WRITE);
929 ret = nbd_co_send_request(bs, request, write_qiov);
934 ret = nbd_co_receive_return_code(s, request->handle,
935 &request_ret, &local_err);
937 trace_nbd_co_request_fail(request->from, request->len, request->handle,
938 request->flags, request->type,
939 nbd_cmd_lookup(request->type),
940 ret, error_get_pretty(local_err));
941 error_free(local_err);
943 return ret ? ret : request_ret;
946 static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
947 uint64_t bytes, QEMUIOVector *qiov, int flags)
949 int ret, request_ret;
950 Error *local_err = NULL;
951 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
952 NBDRequest request = {
953 .type = NBD_CMD_READ,
958 assert(bytes <= NBD_MAX_BUFFER_SIZE);
965 * Work around the fact that the block layer doesn't do
966 * byte-accurate sizing yet - if the read exceeds the server's
967 * advertised size because the block layer rounded size up, then
968 * truncate the request to the server and tail-pad with zero.
970 if (offset >= s->info.size) {
971 assert(bytes < BDRV_SECTOR_SIZE);
972 qemu_iovec_memset(qiov, 0, 0, bytes);
975 if (offset + bytes > s->info.size) {
976 uint64_t slop = offset + bytes - s->info.size;
978 assert(slop < BDRV_SECTOR_SIZE);
979 qemu_iovec_memset(qiov, bytes - slop, 0, slop);
983 ret = nbd_co_send_request(bs, &request, NULL);
988 ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
989 &request_ret, &local_err);
991 trace_nbd_co_request_fail(request.from, request.len, request.handle,
992 request.flags, request.type,
993 nbd_cmd_lookup(request.type),
994 ret, error_get_pretty(local_err));
995 error_free(local_err);
997 return ret ? ret : request_ret;
1000 static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
1001 uint64_t bytes, QEMUIOVector *qiov, int flags)
1003 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1004 NBDRequest request = {
1005 .type = NBD_CMD_WRITE,
1010 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1011 if (flags & BDRV_REQ_FUA) {
1012 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1013 request.flags |= NBD_CMD_FLAG_FUA;
1016 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1021 return nbd_co_request(bs, &request, qiov);
1024 static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1025 int bytes, BdrvRequestFlags flags)
1027 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1028 NBDRequest request = {
1029 .type = NBD_CMD_WRITE_ZEROES,
1034 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1035 if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1039 if (flags & BDRV_REQ_FUA) {
1040 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1041 request.flags |= NBD_CMD_FLAG_FUA;
1043 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1044 request.flags |= NBD_CMD_FLAG_NO_HOLE;
1050 return nbd_co_request(bs, &request, NULL);
1053 static int nbd_client_co_flush(BlockDriverState *bs)
1055 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1056 NBDRequest request = { .type = NBD_CMD_FLUSH };
1058 if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1065 return nbd_co_request(bs, &request, NULL);
1068 static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
1071 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1072 NBDRequest request = {
1073 .type = NBD_CMD_TRIM,
1078 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1079 if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1083 return nbd_co_request(bs, &request, NULL);
1086 static int coroutine_fn nbd_client_co_block_status(
1087 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
1088 int64_t *pnum, int64_t *map, BlockDriverState **file)
1090 int ret, request_ret;
1091 NBDExtent extent = { 0 };
1092 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1093 Error *local_err = NULL;
1095 NBDRequest request = {
1096 .type = NBD_CMD_BLOCK_STATUS,
1098 .len = MIN(MIN_NON_ZERO(QEMU_ALIGN_DOWN(INT_MAX,
1099 bs->bl.request_alignment),
1101 MIN(bytes, s->info.size - offset)),
1102 .flags = NBD_CMD_FLAG_REQ_ONE,
1105 if (!s->info.base_allocation) {
1109 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1113 * Work around the fact that the block layer doesn't do
1114 * byte-accurate sizing yet - if the status request exceeds the
1115 * server's advertised size because the block layer rounded size
1116 * up, we truncated the request to the server (above), or are
1117 * called on just the hole.
1119 if (offset >= s->info.size) {
1121 assert(bytes < BDRV_SECTOR_SIZE);
1122 /* Intentionally don't report offset_valid for the hole */
1123 return BDRV_BLOCK_ZERO;
1126 if (s->info.min_block) {
1127 assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1129 ret = nbd_co_send_request(bs, &request, NULL);
1134 ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1135 &extent, &request_ret, &local_err);
1137 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1138 request.flags, request.type,
1139 nbd_cmd_lookup(request.type),
1140 ret, error_get_pretty(local_err));
1141 error_free(local_err);
1143 if (ret < 0 || request_ret < 0) {
1144 return ret ? ret : request_ret;
1147 assert(extent.length);
1148 *pnum = extent.length;
1151 return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1152 (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1153 BDRV_BLOCK_OFFSET_VALID;
1156 static void nbd_client_close(BlockDriverState *bs)
1158 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1159 NBDRequest request = { .type = NBD_CMD_DISC };
1163 nbd_send_request(s->ioc, &request);
1165 nbd_teardown_connection(bs);
1168 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
1171 QIOChannelSocket *sioc;
1172 Error *local_err = NULL;
1174 sioc = qio_channel_socket_new();
1175 qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
1177 qio_channel_socket_connect_sync(sioc, saddr, &local_err);
1179 object_unref(OBJECT(sioc));
1180 error_propagate(errp, local_err);
1184 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
1189 static int nbd_client_connect(BlockDriverState *bs, Error **errp)
1191 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1192 AioContext *aio_context = bdrv_get_aio_context(bs);
1196 * establish TCP connection, return error if it fails
1197 * TODO: Configurable retry-until-timeout behaviour.
1199 QIOChannelSocket *sioc = nbd_establish_connection(s->saddr, errp);
1202 return -ECONNREFUSED;
1206 trace_nbd_client_connect(s->export);
1207 qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
1208 qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
1210 s->info.request_sizes = true;
1211 s->info.structured_reply = true;
1212 s->info.base_allocation = true;
1213 s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap);
1214 s->info.name = g_strdup(s->export ?: "");
1215 ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds,
1216 s->hostname, &s->ioc, &s->info, errp);
1217 g_free(s->info.x_dirty_bitmap);
1218 g_free(s->info.name);
1220 object_unref(OBJECT(sioc));
1223 if (s->x_dirty_bitmap && !s->info.base_allocation) {
1224 error_setg(errp, "requested x-dirty-bitmap %s not found",
1229 if (s->info.flags & NBD_FLAG_READ_ONLY) {
1230 ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
1235 if (s->info.flags & NBD_FLAG_SEND_FUA) {
1236 bs->supported_write_flags = BDRV_REQ_FUA;
1237 bs->supported_zero_flags |= BDRV_REQ_FUA;
1239 if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
1240 bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
1246 s->ioc = QIO_CHANNEL(sioc);
1247 object_ref(OBJECT(s->ioc));
1250 trace_nbd_client_connect_success(s->export);
1256 * We have connected, but must fail for other reasons.
1257 * Send NBD_CMD_DISC as a courtesy to the server.
1260 NBDRequest request = { .type = NBD_CMD_DISC };
1262 nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request);
1264 object_unref(OBJECT(sioc));
1271 * Parse nbd_open options
1274 static int nbd_parse_uri(const char *filename, QDict *options)
1278 QueryParams *qp = NULL;
1282 uri = uri_parse(filename);
1288 if (!g_strcmp0(uri->scheme, "nbd")) {
1290 } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1292 } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1299 p = uri->path ? uri->path : "/";
1300 p += strspn(p, "/");
1302 qdict_put_str(options, "export", p);
1305 qp = query_params_parse(uri->query);
1306 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1312 /* nbd+unix:///export?socket=path */
1313 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
1317 qdict_put_str(options, "server.type", "unix");
1318 qdict_put_str(options, "server.path", qp->p[0].value);
1323 /* nbd[+tcp]://host[:port]/export */
1329 /* strip braces from literal IPv6 address */
1330 if (uri->server[0] == '[') {
1331 host = qstring_from_substr(uri->server, 1,
1332 strlen(uri->server) - 1);
1334 host = qstring_from_str(uri->server);
1337 qdict_put_str(options, "server.type", "inet");
1338 qdict_put(options, "server.host", host);
1340 port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
1341 qdict_put_str(options, "server.port", port_str);
1347 query_params_free(qp);
1353 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1355 const QDictEntry *e;
1357 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1358 if (!strcmp(e->key, "host") ||
1359 !strcmp(e->key, "port") ||
1360 !strcmp(e->key, "path") ||
1361 !strcmp(e->key, "export") ||
1362 strstart(e->key, "server.", NULL))
1364 error_setg(errp, "Option '%s' cannot be used with a file name",
1373 static void nbd_parse_filename(const char *filename, QDict *options,
1378 const char *host_spec;
1379 const char *unixpath;
1381 if (nbd_has_filename_options_conflict(options, errp)) {
1385 if (strstr(filename, "://")) {
1386 int ret = nbd_parse_uri(filename, options);
1388 error_setg(errp, "No valid URL specified");
1393 file = g_strdup(filename);
1395 export_name = strstr(file, EN_OPTSTR);
1397 if (export_name[strlen(EN_OPTSTR)] == 0) {
1400 export_name[0] = 0; /* truncate 'file' */
1401 export_name += strlen(EN_OPTSTR);
1403 qdict_put_str(options, "export", export_name);
1406 /* extract the host_spec - fail if it's not nbd:... */
1407 if (!strstart(file, "nbd:", &host_spec)) {
1408 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1416 /* are we a UNIX or TCP socket? */
1417 if (strstart(host_spec, "unix:", &unixpath)) {
1418 qdict_put_str(options, "server.type", "unix");
1419 qdict_put_str(options, "server.path", unixpath);
1421 InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1423 if (inet_parse(addr, host_spec, errp)) {
1427 qdict_put_str(options, "server.type", "inet");
1428 qdict_put_str(options, "server.host", addr->host);
1429 qdict_put_str(options, "server.port", addr->port);
1431 qapi_free_InetSocketAddress(addr);
1438 static bool nbd_process_legacy_socket_options(QDict *output_options,
1439 QemuOpts *legacy_opts,
1442 const char *path = qemu_opt_get(legacy_opts, "path");
1443 const char *host = qemu_opt_get(legacy_opts, "host");
1444 const char *port = qemu_opt_get(legacy_opts, "port");
1445 const QDictEntry *e;
1447 if (!path && !host && !port) {
1451 for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1453 if (strstart(e->key, "server.", NULL)) {
1454 error_setg(errp, "Cannot use 'server' and path/host/port at the "
1461 error_setg(errp, "path and host may not be used at the same time");
1465 error_setg(errp, "port may not be used without host");
1469 qdict_put_str(output_options, "server.type", "unix");
1470 qdict_put_str(output_options, "server.path", path);
1472 qdict_put_str(output_options, "server.type", "inet");
1473 qdict_put_str(output_options, "server.host", host);
1474 qdict_put_str(output_options, "server.port",
1475 port ?: stringify(NBD_DEFAULT_PORT));
1481 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
1484 SocketAddress *saddr = NULL;
1487 Error *local_err = NULL;
1489 qdict_extract_subqdict(options, &addr, "server.");
1490 if (!qdict_size(addr)) {
1491 error_setg(errp, "NBD server address missing");
1495 iv = qobject_input_visitor_new_flat_confused(addr, errp);
1500 visit_type_SocketAddress(iv, NULL, &saddr, &local_err);
1502 error_propagate(errp, local_err);
1507 qobject_unref(addr);
1512 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
1515 QCryptoTLSCreds *creds;
1517 obj = object_resolve_path_component(
1518 object_get_objects_root(), id);
1520 error_setg(errp, "No TLS credentials with id '%s'",
1524 creds = (QCryptoTLSCreds *)
1525 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
1527 error_setg(errp, "Object with id '%s' is not TLS credentials",
1532 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1534 "Expecting TLS credentials with a client endpoint");
1542 static QemuOptsList nbd_runtime_opts = {
1544 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
1548 .type = QEMU_OPT_STRING,
1549 .help = "TCP host to connect to",
1553 .type = QEMU_OPT_STRING,
1554 .help = "TCP port to connect to",
1558 .type = QEMU_OPT_STRING,
1559 .help = "Unix socket path to connect to",
1563 .type = QEMU_OPT_STRING,
1564 .help = "Name of the NBD export to open",
1567 .name = "tls-creds",
1568 .type = QEMU_OPT_STRING,
1569 .help = "ID of the TLS credentials to use",
1572 .name = "x-dirty-bitmap",
1573 .type = QEMU_OPT_STRING,
1574 .help = "experimental: expose named dirty bitmap in place of "
1578 .name = "reconnect-delay",
1579 .type = QEMU_OPT_NUMBER,
1580 .help = "On an unexpected disconnect, the nbd client tries to "
1581 "connect again until succeeding or encountering a serious "
1582 "error. During the first @reconnect-delay seconds, all "
1583 "requests are paused and will be rerun on a successful "
1584 "reconnect. After that time, any delayed requests and all "
1585 "future requests before a successful reconnect will "
1586 "immediately fail. Default 0",
1588 { /* end of list */ }
1592 static int nbd_process_options(BlockDriverState *bs, QDict *options,
1595 BDRVNBDState *s = bs->opaque;
1597 Error *local_err = NULL;
1600 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
1601 qemu_opts_absorb_qdict(opts, options, &local_err);
1603 error_propagate(errp, local_err);
1607 /* Translate @host, @port, and @path to a SocketAddress */
1608 if (!nbd_process_legacy_socket_options(options, opts, errp)) {
1612 /* Pop the config into our state object. Exit if invalid. */
1613 s->saddr = nbd_config(s, options, errp);
1618 s->export = g_strdup(qemu_opt_get(opts, "export"));
1620 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
1621 if (s->tlscredsid) {
1622 s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
1627 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1628 if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
1629 error_setg(errp, "TLS only supported over IP sockets");
1632 s->hostname = s->saddr->u.inet.host;
1635 s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
1636 s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
1642 object_unref(OBJECT(s->tlscreds));
1643 qapi_free_SocketAddress(s->saddr);
1645 g_free(s->tlscredsid);
1647 qemu_opts_del(opts);
1651 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
1655 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1657 ret = nbd_process_options(bs, options, errp);
1663 qemu_co_mutex_init(&s->send_mutex);
1664 qemu_co_queue_init(&s->free_sema);
1666 ret = nbd_client_connect(bs, errp);
1670 /* successfully connected */
1671 s->state = NBD_CLIENT_CONNECTED;
1673 s->connection_co = qemu_coroutine_create(nbd_connection_entry, s);
1674 bdrv_inc_in_flight(bs);
1675 aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co);
1680 static int nbd_co_flush(BlockDriverState *bs)
1682 return nbd_client_co_flush(bs);
1685 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
1687 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1688 uint32_t min = s->info.min_block;
1689 uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
1692 * If the server did not advertise an alignment:
1693 * - a size that is not sector-aligned implies that an alignment
1694 * of 1 can be used to access those tail bytes
1695 * - advertisement of block status requires an alignment of 1, so
1696 * that we don't violate block layer constraints that block
1697 * status is always aligned (as we can't control whether the
1698 * server will report sub-sector extents, such as a hole at EOF
1699 * on an unaligned POSIX file)
1700 * - otherwise, assume the server is so old that we are safer avoiding
1701 * sub-sector requests
1704 min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
1705 s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
1708 bs->bl.request_alignment = min;
1709 bs->bl.max_pdiscard = max;
1710 bs->bl.max_pwrite_zeroes = max;
1711 bs->bl.max_transfer = max;
1713 if (s->info.opt_block &&
1714 s->info.opt_block > bs->bl.opt_transfer) {
1715 bs->bl.opt_transfer = s->info.opt_block;
1719 static void nbd_close(BlockDriverState *bs)
1721 BDRVNBDState *s = bs->opaque;
1723 nbd_client_close(bs);
1725 object_unref(OBJECT(s->tlscreds));
1726 qapi_free_SocketAddress(s->saddr);
1728 g_free(s->tlscredsid);
1729 g_free(s->x_dirty_bitmap);
1732 static int64_t nbd_getlength(BlockDriverState *bs)
1734 BDRVNBDState *s = bs->opaque;
1736 return s->info.size;
1739 static void nbd_refresh_filename(BlockDriverState *bs)
1741 BDRVNBDState *s = bs->opaque;
1742 const char *host = NULL, *port = NULL, *path = NULL;
1744 if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
1745 const InetSocketAddress *inet = &s->saddr->u.inet;
1746 if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
1750 } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
1751 path = s->saddr->u.q_unix.path;
1752 } /* else can't represent as pseudo-filename */
1754 if (path && s->export) {
1755 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1756 "nbd+unix:///%s?socket=%s", s->export, path);
1757 } else if (path && !s->export) {
1758 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1759 "nbd+unix://?socket=%s", path);
1760 } else if (host && s->export) {
1761 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1762 "nbd://%s:%s/%s", host, port, s->export);
1763 } else if (host && !s->export) {
1764 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
1765 "nbd://%s:%s", host, port);
1769 static char *nbd_dirname(BlockDriverState *bs, Error **errp)
1771 /* The generic bdrv_dirname() implementation is able to work out some
1772 * directory name for NBD nodes, but that would be wrong. So far there is no
1773 * specification for how "export paths" would work, so NBD does not have
1774 * directory names. */
1775 error_setg(errp, "Cannot generate a base directory for NBD nodes");
1779 static const char *const nbd_strong_runtime_opts[] = {
1790 static BlockDriver bdrv_nbd = {
1791 .format_name = "nbd",
1792 .protocol_name = "nbd",
1793 .instance_size = sizeof(BDRVNBDState),
1794 .bdrv_parse_filename = nbd_parse_filename,
1795 .bdrv_file_open = nbd_open,
1796 .bdrv_co_preadv = nbd_client_co_preadv,
1797 .bdrv_co_pwritev = nbd_client_co_pwritev,
1798 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
1799 .bdrv_close = nbd_close,
1800 .bdrv_co_flush_to_os = nbd_co_flush,
1801 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
1802 .bdrv_refresh_limits = nbd_refresh_limits,
1803 .bdrv_getlength = nbd_getlength,
1804 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
1805 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
1806 .bdrv_refresh_filename = nbd_refresh_filename,
1807 .bdrv_co_block_status = nbd_client_co_block_status,
1808 .bdrv_dirname = nbd_dirname,
1809 .strong_runtime_opts = nbd_strong_runtime_opts,
1812 static BlockDriver bdrv_nbd_tcp = {
1813 .format_name = "nbd",
1814 .protocol_name = "nbd+tcp",
1815 .instance_size = sizeof(BDRVNBDState),
1816 .bdrv_parse_filename = nbd_parse_filename,
1817 .bdrv_file_open = nbd_open,
1818 .bdrv_co_preadv = nbd_client_co_preadv,
1819 .bdrv_co_pwritev = nbd_client_co_pwritev,
1820 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
1821 .bdrv_close = nbd_close,
1822 .bdrv_co_flush_to_os = nbd_co_flush,
1823 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
1824 .bdrv_refresh_limits = nbd_refresh_limits,
1825 .bdrv_getlength = nbd_getlength,
1826 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
1827 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
1828 .bdrv_refresh_filename = nbd_refresh_filename,
1829 .bdrv_co_block_status = nbd_client_co_block_status,
1830 .bdrv_dirname = nbd_dirname,
1831 .strong_runtime_opts = nbd_strong_runtime_opts,
1834 static BlockDriver bdrv_nbd_unix = {
1835 .format_name = "nbd",
1836 .protocol_name = "nbd+unix",
1837 .instance_size = sizeof(BDRVNBDState),
1838 .bdrv_parse_filename = nbd_parse_filename,
1839 .bdrv_file_open = nbd_open,
1840 .bdrv_co_preadv = nbd_client_co_preadv,
1841 .bdrv_co_pwritev = nbd_client_co_pwritev,
1842 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
1843 .bdrv_close = nbd_close,
1844 .bdrv_co_flush_to_os = nbd_co_flush,
1845 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
1846 .bdrv_refresh_limits = nbd_refresh_limits,
1847 .bdrv_getlength = nbd_getlength,
1848 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
1849 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
1850 .bdrv_refresh_filename = nbd_refresh_filename,
1851 .bdrv_co_block_status = nbd_client_co_block_status,
1852 .bdrv_dirname = nbd_dirname,
1853 .strong_runtime_opts = nbd_strong_runtime_opts,
1856 static void bdrv_nbd_init(void)
1858 bdrv_register(&bdrv_nbd);
1859 bdrv_register(&bdrv_nbd_tcp);
1860 bdrv_register(&bdrv_nbd_unix);
1863 block_init(bdrv_nbd_init);