block/nbd: on shutdown terminate connection attempt
[qemu.git] / block / nbd.c
1 /*
2 * QEMU Block driver for NBD
3 *
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>
8 *
9 * Some parts:
10 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
11 *
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:
18 *
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
21 *
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
28 * THE SOFTWARE.
29 */
30
31 #include "qemu/osdep.h"
32
33 #include "trace.h"
34 #include "qemu/uri.h"
35 #include "qemu/option.h"
36 #include "qemu/cutils.h"
37 #include "qemu/main-loop.h"
38
39 #include "qapi/qapi-visit-sockets.h"
40 #include "qapi/qmp/qstring.h"
41
42 #include "block/qdict.h"
43 #include "block/nbd.h"
44 #include "block/block_int.h"
45
46 #define EN_OPTSTR ":exportname="
47 #define MAX_NBD_REQUESTS 16
48
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))
51
52 typedef struct {
53 Coroutine *coroutine;
54 uint64_t offset; /* original offset of the request */
55 bool receiving; /* waiting for connection_co? */
56 } NBDClientRequest;
57
58 typedef enum NBDClientState {
59 NBD_CLIENT_CONNECTING_WAIT,
60 NBD_CLIENT_CONNECTING_NOWAIT,
61 NBD_CLIENT_CONNECTED,
62 NBD_CLIENT_QUIT
63 } NBDClientState;
64
65 typedef struct BDRVNBDState {
66 QIOChannelSocket *sioc; /* The master data channel */
67 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
68 NBDExportInfo info;
69
70 CoMutex send_mutex;
71 CoQueue free_sema;
72 Coroutine *connection_co;
73 Coroutine *teardown_co;
74 QemuCoSleepState *connection_co_sleep_ns_state;
75 bool drained;
76 bool wait_drained_end;
77 int in_flight;
78 NBDClientState state;
79 int connect_status;
80 Error *connect_err;
81 bool wait_in_flight;
82
83 NBDClientRequest requests[MAX_NBD_REQUESTS];
84 NBDReply reply;
85 BlockDriverState *bs;
86
87 /* Connection parameters */
88 uint32_t reconnect_delay;
89 SocketAddress *saddr;
90 char *export, *tlscredsid;
91 QCryptoTLSCreds *tlscreds;
92 const char *hostname;
93 char *x_dirty_bitmap;
94 } BDRVNBDState;
95
96 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
97 Error **errp);
98 static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
99 Error **errp);
100
101 static void nbd_clear_bdrvstate(BDRVNBDState *s)
102 {
103 object_unref(OBJECT(s->tlscreds));
104 qapi_free_SocketAddress(s->saddr);
105 s->saddr = NULL;
106 g_free(s->export);
107 s->export = NULL;
108 g_free(s->tlscredsid);
109 s->tlscredsid = NULL;
110 g_free(s->x_dirty_bitmap);
111 s->x_dirty_bitmap = NULL;
112 }
113
114 static void nbd_channel_error(BDRVNBDState *s, int ret)
115 {
116 if (ret == -EIO) {
117 if (s->state == NBD_CLIENT_CONNECTED) {
118 s->state = s->reconnect_delay ? NBD_CLIENT_CONNECTING_WAIT :
119 NBD_CLIENT_CONNECTING_NOWAIT;
120 }
121 } else {
122 if (s->state == NBD_CLIENT_CONNECTED) {
123 qio_channel_shutdown(s->ioc, QIO_CHANNEL_SHUTDOWN_BOTH, NULL);
124 }
125 s->state = NBD_CLIENT_QUIT;
126 }
127 }
128
129 static void nbd_recv_coroutines_wake_all(BDRVNBDState *s)
130 {
131 int i;
132
133 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
134 NBDClientRequest *req = &s->requests[i];
135
136 if (req->coroutine && req->receiving) {
137 aio_co_wake(req->coroutine);
138 }
139 }
140 }
141
142 static void nbd_client_detach_aio_context(BlockDriverState *bs)
143 {
144 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
145
146 qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc));
147 }
148
149 static void nbd_client_attach_aio_context_bh(void *opaque)
150 {
151 BlockDriverState *bs = opaque;
152 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
153
154 /*
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
158 * coroutine.
159 */
160 qemu_aio_coroutine_enter(bs->aio_context, s->connection_co);
161 bdrv_dec_in_flight(bs);
162 }
163
164 static void nbd_client_attach_aio_context(BlockDriverState *bs,
165 AioContext *new_context)
166 {
167 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
168
169 /*
170 * s->connection_co is either yielded from nbd_receive_reply or from
171 * nbd_co_reconnect_loop()
172 */
173 if (s->state == NBD_CLIENT_CONNECTED) {
174 qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context);
175 }
176
177 bdrv_inc_in_flight(bs);
178
179 /*
180 * Need to wait here for the BH to run because the BH must run while the
181 * node is still drained.
182 */
183 aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs);
184 }
185
186 static void coroutine_fn nbd_client_co_drain_begin(BlockDriverState *bs)
187 {
188 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
189
190 s->drained = true;
191 if (s->connection_co_sleep_ns_state) {
192 qemu_co_sleep_wake(s->connection_co_sleep_ns_state);
193 }
194 }
195
196 static void coroutine_fn nbd_client_co_drain_end(BlockDriverState *bs)
197 {
198 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
199
200 s->drained = false;
201 if (s->wait_drained_end) {
202 s->wait_drained_end = false;
203 aio_co_wake(s->connection_co);
204 }
205 }
206
207
208 static void nbd_teardown_connection(BlockDriverState *bs)
209 {
210 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
211
212 if (s->ioc) {
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,
218 NULL);
219 }
220
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);
225 }
226 }
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;
232 } else {
233 BDRV_POLL_WHILE(bs, s->connection_co);
234 }
235 assert(!s->connection_co);
236 }
237
238 static bool nbd_client_connecting(BDRVNBDState *s)
239 {
240 return s->state == NBD_CLIENT_CONNECTING_WAIT ||
241 s->state == NBD_CLIENT_CONNECTING_NOWAIT;
242 }
243
244 static bool nbd_client_connecting_wait(BDRVNBDState *s)
245 {
246 return s->state == NBD_CLIENT_CONNECTING_WAIT;
247 }
248
249 static coroutine_fn void nbd_reconnect_attempt(BDRVNBDState *s)
250 {
251 int ret;
252 Error *local_err = NULL;
253 QIOChannelSocket *sioc;
254
255 if (!nbd_client_connecting(s)) {
256 return;
257 }
258
259 /* Wait for completion of all in-flight requests */
260
261 qemu_co_mutex_lock(&s->send_mutex);
262
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);
270 }
271
272 qemu_co_mutex_unlock(&s->send_mutex);
273
274 if (!nbd_client_connecting(s)) {
275 return;
276 }
277
278 /*
279 * Now we are sure that nobody is accessing the channel, and no one will
280 * try until we set the state to CONNECTED.
281 */
282
283 /* Finalize previous connection if any */
284 if (s->ioc) {
285 nbd_client_detach_aio_context(s->bs);
286 object_unref(OBJECT(s->sioc));
287 s->sioc = NULL;
288 object_unref(OBJECT(s->ioc));
289 s->ioc = NULL;
290 }
291
292 sioc = nbd_establish_connection(s->saddr, &local_err);
293 if (!sioc) {
294 ret = -ECONNREFUSED;
295 goto out;
296 }
297
298 bdrv_dec_in_flight(s->bs);
299
300 ret = nbd_client_handshake(s->bs, sioc, &local_err);
301
302 if (s->drained) {
303 s->wait_drained_end = true;
304 while (s->drained) {
305 /*
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.
308 */
309 qemu_coroutine_yield();
310 }
311 }
312 bdrv_inc_in_flight(s->bs);
313
314 out:
315 s->connect_status = ret;
316 error_free(s->connect_err);
317 s->connect_err = NULL;
318 error_propagate(&s->connect_err, local_err);
319
320 if (ret >= 0) {
321 /* successfully connected */
322 s->state = NBD_CLIENT_CONNECTED;
323 qemu_co_queue_restart_all(&s->free_sema);
324 }
325 }
326
327 static coroutine_fn void nbd_co_reconnect_loop(BDRVNBDState *s)
328 {
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;
333
334 nbd_reconnect_attempt(s);
335
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)
339 {
340 s->state = NBD_CLIENT_CONNECTING_NOWAIT;
341 qemu_co_queue_restart_all(&s->free_sema);
342 }
343
344 qemu_co_sleep_ns_wakeable(QEMU_CLOCK_REALTIME, timeout,
345 &s->connection_co_sleep_ns_state);
346 if (s->drained) {
347 bdrv_dec_in_flight(s->bs);
348 s->wait_drained_end = true;
349 while (s->drained) {
350 /*
351 * We may be entered once from nbd_client_attach_aio_context_bh
352 * and then from nbd_client_co_drain_end. So here is a loop.
353 */
354 qemu_coroutine_yield();
355 }
356 bdrv_inc_in_flight(s->bs);
357 }
358 if (timeout < max_timeout) {
359 timeout *= 2;
360 }
361
362 nbd_reconnect_attempt(s);
363 }
364 }
365
366 static coroutine_fn void nbd_connection_entry(void *opaque)
367 {
368 BDRVNBDState *s = opaque;
369 uint64_t i;
370 int ret = 0;
371 Error *local_err = NULL;
372
373 while (s->state != NBD_CLIENT_QUIT) {
374 /*
375 * The NBD client can only really be considered idle when it has
376 * yielded from qio_channel_readv_all_eof(), waiting for data. This is
377 * the point where the additional scheduled coroutine entry happens
378 * after nbd_client_attach_aio_context().
379 *
380 * Therefore we keep an additional in_flight reference all the time and
381 * only drop it temporarily here.
382 */
383
384 if (nbd_client_connecting(s)) {
385 nbd_co_reconnect_loop(s);
386 }
387
388 if (s->state != NBD_CLIENT_CONNECTED) {
389 continue;
390 }
391
392 assert(s->reply.handle == 0);
393 ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err);
394
395 if (local_err) {
396 trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err));
397 error_free(local_err);
398 local_err = NULL;
399 }
400 if (ret <= 0) {
401 nbd_channel_error(s, ret ? ret : -EIO);
402 continue;
403 }
404
405 /*
406 * There's no need for a mutex on the receive side, because the
407 * handler acts as a synchronization point and ensures that only
408 * one coroutine is called until the reply finishes.
409 */
410 i = HANDLE_TO_INDEX(s, s->reply.handle);
411 if (i >= MAX_NBD_REQUESTS ||
412 !s->requests[i].coroutine ||
413 !s->requests[i].receiving ||
414 (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply))
415 {
416 nbd_channel_error(s, -EINVAL);
417 continue;
418 }
419
420 /*
421 * We're woken up again by the request itself. Note that there
422 * is no race between yielding and reentering connection_co. This
423 * is because:
424 *
425 * - if the request runs on the same AioContext, it is only
426 * entered after we yield
427 *
428 * - if the request runs on a different AioContext, reentering
429 * connection_co happens through a bottom half, which can only
430 * run after we yield.
431 */
432 aio_co_wake(s->requests[i].coroutine);
433 qemu_coroutine_yield();
434 }
435
436 qemu_co_queue_restart_all(&s->free_sema);
437 nbd_recv_coroutines_wake_all(s);
438 bdrv_dec_in_flight(s->bs);
439
440 s->connection_co = NULL;
441 if (s->ioc) {
442 nbd_client_detach_aio_context(s->bs);
443 object_unref(OBJECT(s->sioc));
444 s->sioc = NULL;
445 object_unref(OBJECT(s->ioc));
446 s->ioc = NULL;
447 }
448
449 if (s->teardown_co) {
450 aio_co_wake(s->teardown_co);
451 }
452 aio_wait_kick();
453 }
454
455 static int nbd_co_send_request(BlockDriverState *bs,
456 NBDRequest *request,
457 QEMUIOVector *qiov)
458 {
459 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
460 int rc, i = -1;
461
462 qemu_co_mutex_lock(&s->send_mutex);
463 while (s->in_flight == MAX_NBD_REQUESTS || nbd_client_connecting_wait(s)) {
464 qemu_co_queue_wait(&s->free_sema, &s->send_mutex);
465 }
466
467 if (s->state != NBD_CLIENT_CONNECTED) {
468 rc = -EIO;
469 goto err;
470 }
471
472 s->in_flight++;
473
474 for (i = 0; i < MAX_NBD_REQUESTS; i++) {
475 if (s->requests[i].coroutine == NULL) {
476 break;
477 }
478 }
479
480 g_assert(qemu_in_coroutine());
481 assert(i < MAX_NBD_REQUESTS);
482
483 s->requests[i].coroutine = qemu_coroutine_self();
484 s->requests[i].offset = request->from;
485 s->requests[i].receiving = false;
486
487 request->handle = INDEX_TO_HANDLE(s, i);
488
489 assert(s->ioc);
490
491 if (qiov) {
492 qio_channel_set_cork(s->ioc, true);
493 rc = nbd_send_request(s->ioc, request);
494 if (rc >= 0 && s->state == NBD_CLIENT_CONNECTED) {
495 if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov,
496 NULL) < 0) {
497 rc = -EIO;
498 }
499 } else if (rc >= 0) {
500 rc = -EIO;
501 }
502 qio_channel_set_cork(s->ioc, false);
503 } else {
504 rc = nbd_send_request(s->ioc, request);
505 }
506
507 err:
508 if (rc < 0) {
509 nbd_channel_error(s, rc);
510 if (i != -1) {
511 s->requests[i].coroutine = NULL;
512 s->in_flight--;
513 }
514 if (s->in_flight == 0 && s->wait_in_flight) {
515 aio_co_wake(s->connection_co);
516 } else {
517 qemu_co_queue_next(&s->free_sema);
518 }
519 }
520 qemu_co_mutex_unlock(&s->send_mutex);
521 return rc;
522 }
523
524 static inline uint16_t payload_advance16(uint8_t **payload)
525 {
526 *payload += 2;
527 return lduw_be_p(*payload - 2);
528 }
529
530 static inline uint32_t payload_advance32(uint8_t **payload)
531 {
532 *payload += 4;
533 return ldl_be_p(*payload - 4);
534 }
535
536 static inline uint64_t payload_advance64(uint8_t **payload)
537 {
538 *payload += 8;
539 return ldq_be_p(*payload - 8);
540 }
541
542 static int nbd_parse_offset_hole_payload(BDRVNBDState *s,
543 NBDStructuredReplyChunk *chunk,
544 uint8_t *payload, uint64_t orig_offset,
545 QEMUIOVector *qiov, Error **errp)
546 {
547 uint64_t offset;
548 uint32_t hole_size;
549
550 if (chunk->length != sizeof(offset) + sizeof(hole_size)) {
551 error_setg(errp, "Protocol error: invalid payload for "
552 "NBD_REPLY_TYPE_OFFSET_HOLE");
553 return -EINVAL;
554 }
555
556 offset = payload_advance64(&payload);
557 hole_size = payload_advance32(&payload);
558
559 if (!hole_size || offset < orig_offset || hole_size > qiov->size ||
560 offset > orig_offset + qiov->size - hole_size) {
561 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
562 " region");
563 return -EINVAL;
564 }
565 if (s->info.min_block &&
566 !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) {
567 trace_nbd_structured_read_compliance("hole");
568 }
569
570 qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size);
571
572 return 0;
573 }
574
575 /*
576 * nbd_parse_blockstatus_payload
577 * Based on our request, we expect only one extent in reply, for the
578 * base:allocation context.
579 */
580 static int nbd_parse_blockstatus_payload(BDRVNBDState *s,
581 NBDStructuredReplyChunk *chunk,
582 uint8_t *payload, uint64_t orig_length,
583 NBDExtent *extent, Error **errp)
584 {
585 uint32_t context_id;
586
587 /* The server succeeded, so it must have sent [at least] one extent */
588 if (chunk->length < sizeof(context_id) + sizeof(*extent)) {
589 error_setg(errp, "Protocol error: invalid payload for "
590 "NBD_REPLY_TYPE_BLOCK_STATUS");
591 return -EINVAL;
592 }
593
594 context_id = payload_advance32(&payload);
595 if (s->info.context_id != context_id) {
596 error_setg(errp, "Protocol error: unexpected context id %d for "
597 "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context "
598 "id is %d", context_id,
599 s->info.context_id);
600 return -EINVAL;
601 }
602
603 extent->length = payload_advance32(&payload);
604 extent->flags = payload_advance32(&payload);
605
606 if (extent->length == 0) {
607 error_setg(errp, "Protocol error: server sent status chunk with "
608 "zero length");
609 return -EINVAL;
610 }
611
612 /*
613 * A server sending unaligned block status is in violation of the
614 * protocol, but as qemu-nbd 3.1 is such a server (at least for
615 * POSIX files that are not a multiple of 512 bytes, since qemu
616 * rounds files up to 512-byte multiples but lseek(SEEK_HOLE)
617 * still sees an implicit hole beyond the real EOF), it's nicer to
618 * work around the misbehaving server. If the request included
619 * more than the final unaligned block, truncate it back to an
620 * aligned result; if the request was only the final block, round
621 * up to the full block and change the status to fully-allocated
622 * (always a safe status, even if it loses information).
623 */
624 if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length,
625 s->info.min_block)) {
626 trace_nbd_parse_blockstatus_compliance("extent length is unaligned");
627 if (extent->length > s->info.min_block) {
628 extent->length = QEMU_ALIGN_DOWN(extent->length,
629 s->info.min_block);
630 } else {
631 extent->length = s->info.min_block;
632 extent->flags = 0;
633 }
634 }
635
636 /*
637 * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have
638 * sent us any more than one extent, nor should it have included
639 * status beyond our request in that extent. However, it's easy
640 * enough to ignore the server's noncompliance without killing the
641 * connection; just ignore trailing extents, and clamp things to
642 * the length of our request.
643 */
644 if (chunk->length > sizeof(context_id) + sizeof(*extent)) {
645 trace_nbd_parse_blockstatus_compliance("more than one extent");
646 }
647 if (extent->length > orig_length) {
648 extent->length = orig_length;
649 trace_nbd_parse_blockstatus_compliance("extent length too large");
650 }
651
652 return 0;
653 }
654
655 /*
656 * nbd_parse_error_payload
657 * on success @errp contains message describing nbd error reply
658 */
659 static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk,
660 uint8_t *payload, int *request_ret,
661 Error **errp)
662 {
663 uint32_t error;
664 uint16_t message_size;
665
666 assert(chunk->type & (1 << 15));
667
668 if (chunk->length < sizeof(error) + sizeof(message_size)) {
669 error_setg(errp,
670 "Protocol error: invalid payload for structured error");
671 return -EINVAL;
672 }
673
674 error = nbd_errno_to_system_errno(payload_advance32(&payload));
675 if (error == 0) {
676 error_setg(errp, "Protocol error: server sent structured error chunk "
677 "with error = 0");
678 return -EINVAL;
679 }
680
681 *request_ret = -error;
682 message_size = payload_advance16(&payload);
683
684 if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) {
685 error_setg(errp, "Protocol error: server sent structured error chunk "
686 "with incorrect message size");
687 return -EINVAL;
688 }
689
690 /* TODO: Add a trace point to mention the server complaint */
691
692 /* TODO handle ERROR_OFFSET */
693
694 return 0;
695 }
696
697 static int nbd_co_receive_offset_data_payload(BDRVNBDState *s,
698 uint64_t orig_offset,
699 QEMUIOVector *qiov, Error **errp)
700 {
701 QEMUIOVector sub_qiov;
702 uint64_t offset;
703 size_t data_size;
704 int ret;
705 NBDStructuredReplyChunk *chunk = &s->reply.structured;
706
707 assert(nbd_reply_is_structured(&s->reply));
708
709 /* The NBD spec requires at least one byte of payload */
710 if (chunk->length <= sizeof(offset)) {
711 error_setg(errp, "Protocol error: invalid payload for "
712 "NBD_REPLY_TYPE_OFFSET_DATA");
713 return -EINVAL;
714 }
715
716 if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) {
717 return -EIO;
718 }
719
720 data_size = chunk->length - sizeof(offset);
721 assert(data_size);
722 if (offset < orig_offset || data_size > qiov->size ||
723 offset > orig_offset + qiov->size - data_size) {
724 error_setg(errp, "Protocol error: server sent chunk exceeding requested"
725 " region");
726 return -EINVAL;
727 }
728 if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) {
729 trace_nbd_structured_read_compliance("data");
730 }
731
732 qemu_iovec_init(&sub_qiov, qiov->niov);
733 qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size);
734 ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp);
735 qemu_iovec_destroy(&sub_qiov);
736
737 return ret < 0 ? -EIO : 0;
738 }
739
740 #define NBD_MAX_MALLOC_PAYLOAD 1000
741 static coroutine_fn int nbd_co_receive_structured_payload(
742 BDRVNBDState *s, void **payload, Error **errp)
743 {
744 int ret;
745 uint32_t len;
746
747 assert(nbd_reply_is_structured(&s->reply));
748
749 len = s->reply.structured.length;
750
751 if (len == 0) {
752 return 0;
753 }
754
755 if (payload == NULL) {
756 error_setg(errp, "Unexpected structured payload");
757 return -EINVAL;
758 }
759
760 if (len > NBD_MAX_MALLOC_PAYLOAD) {
761 error_setg(errp, "Payload too large");
762 return -EINVAL;
763 }
764
765 *payload = g_new(char, len);
766 ret = nbd_read(s->ioc, *payload, len, "structured payload", errp);
767 if (ret < 0) {
768 g_free(*payload);
769 *payload = NULL;
770 return ret;
771 }
772
773 return 0;
774 }
775
776 /*
777 * nbd_co_do_receive_one_chunk
778 * for simple reply:
779 * set request_ret to received reply error
780 * if qiov is not NULL: read payload to @qiov
781 * for structured reply chunk:
782 * if error chunk: read payload, set @request_ret, do not set @payload
783 * else if offset_data chunk: read payload data to @qiov, do not set @payload
784 * else: read payload to @payload
785 *
786 * If function fails, @errp contains corresponding error message, and the
787 * connection with the server is suspect. If it returns 0, then the
788 * transaction succeeded (although @request_ret may be a negative errno
789 * corresponding to the server's error reply), and errp is unchanged.
790 */
791 static coroutine_fn int nbd_co_do_receive_one_chunk(
792 BDRVNBDState *s, uint64_t handle, bool only_structured,
793 int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp)
794 {
795 int ret;
796 int i = HANDLE_TO_INDEX(s, handle);
797 void *local_payload = NULL;
798 NBDStructuredReplyChunk *chunk;
799
800 if (payload) {
801 *payload = NULL;
802 }
803 *request_ret = 0;
804
805 /* Wait until we're woken up by nbd_connection_entry. */
806 s->requests[i].receiving = true;
807 qemu_coroutine_yield();
808 s->requests[i].receiving = false;
809 if (s->state != NBD_CLIENT_CONNECTED) {
810 error_setg(errp, "Connection closed");
811 return -EIO;
812 }
813 assert(s->ioc);
814
815 assert(s->reply.handle == handle);
816
817 if (nbd_reply_is_simple(&s->reply)) {
818 if (only_structured) {
819 error_setg(errp, "Protocol error: simple reply when structured "
820 "reply chunk was expected");
821 return -EINVAL;
822 }
823
824 *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error);
825 if (*request_ret < 0 || !qiov) {
826 return 0;
827 }
828
829 return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov,
830 errp) < 0 ? -EIO : 0;
831 }
832
833 /* handle structured reply chunk */
834 assert(s->info.structured_reply);
835 chunk = &s->reply.structured;
836
837 if (chunk->type == NBD_REPLY_TYPE_NONE) {
838 if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) {
839 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without"
840 " NBD_REPLY_FLAG_DONE flag set");
841 return -EINVAL;
842 }
843 if (chunk->length) {
844 error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with"
845 " nonzero length");
846 return -EINVAL;
847 }
848 return 0;
849 }
850
851 if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) {
852 if (!qiov) {
853 error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk");
854 return -EINVAL;
855 }
856
857 return nbd_co_receive_offset_data_payload(s, s->requests[i].offset,
858 qiov, errp);
859 }
860
861 if (nbd_reply_type_is_error(chunk->type)) {
862 payload = &local_payload;
863 }
864
865 ret = nbd_co_receive_structured_payload(s, payload, errp);
866 if (ret < 0) {
867 return ret;
868 }
869
870 if (nbd_reply_type_is_error(chunk->type)) {
871 ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp);
872 g_free(local_payload);
873 return ret;
874 }
875
876 return 0;
877 }
878
879 /*
880 * nbd_co_receive_one_chunk
881 * Read reply, wake up connection_co and set s->quit if needed.
882 * Return value is a fatal error code or normal nbd reply error code
883 */
884 static coroutine_fn int nbd_co_receive_one_chunk(
885 BDRVNBDState *s, uint64_t handle, bool only_structured,
886 int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload,
887 Error **errp)
888 {
889 int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured,
890 request_ret, qiov, payload, errp);
891
892 if (ret < 0) {
893 memset(reply, 0, sizeof(*reply));
894 nbd_channel_error(s, ret);
895 } else {
896 /* For assert at loop start in nbd_connection_entry */
897 *reply = s->reply;
898 }
899 s->reply.handle = 0;
900
901 if (s->connection_co && !s->wait_in_flight) {
902 /*
903 * We must check s->wait_in_flight, because we may entered by
904 * nbd_recv_coroutines_wake_all(), in this case we should not
905 * wake connection_co here, it will woken by last request.
906 */
907 aio_co_wake(s->connection_co);
908 }
909
910 return ret;
911 }
912
913 typedef struct NBDReplyChunkIter {
914 int ret;
915 int request_ret;
916 Error *err;
917 bool done, only_structured;
918 } NBDReplyChunkIter;
919
920 static void nbd_iter_channel_error(NBDReplyChunkIter *iter,
921 int ret, Error **local_err)
922 {
923 assert(local_err && *local_err);
924 assert(ret < 0);
925
926 if (!iter->ret) {
927 iter->ret = ret;
928 error_propagate(&iter->err, *local_err);
929 } else {
930 error_free(*local_err);
931 }
932
933 *local_err = NULL;
934 }
935
936 static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret)
937 {
938 assert(ret < 0);
939
940 if (!iter->request_ret) {
941 iter->request_ret = ret;
942 }
943 }
944
945 /*
946 * NBD_FOREACH_REPLY_CHUNK
947 * The pointer stored in @payload requires g_free() to free it.
948 */
949 #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \
950 qiov, reply, payload) \
951 for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \
952 nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);)
953
954 /*
955 * nbd_reply_chunk_iter_receive
956 * The pointer stored in @payload requires g_free() to free it.
957 */
958 static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s,
959 NBDReplyChunkIter *iter,
960 uint64_t handle,
961 QEMUIOVector *qiov, NBDReply *reply,
962 void **payload)
963 {
964 int ret, request_ret;
965 NBDReply local_reply;
966 NBDStructuredReplyChunk *chunk;
967 Error *local_err = NULL;
968 if (s->state != NBD_CLIENT_CONNECTED) {
969 error_setg(&local_err, "Connection closed");
970 nbd_iter_channel_error(iter, -EIO, &local_err);
971 goto break_loop;
972 }
973
974 if (iter->done) {
975 /* Previous iteration was last. */
976 goto break_loop;
977 }
978
979 if (reply == NULL) {
980 reply = &local_reply;
981 }
982
983 ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured,
984 &request_ret, qiov, reply, payload,
985 &local_err);
986 if (ret < 0) {
987 nbd_iter_channel_error(iter, ret, &local_err);
988 } else if (request_ret < 0) {
989 nbd_iter_request_error(iter, request_ret);
990 }
991
992 /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */
993 if (nbd_reply_is_simple(reply) || s->state != NBD_CLIENT_CONNECTED) {
994 goto break_loop;
995 }
996
997 chunk = &reply->structured;
998 iter->only_structured = true;
999
1000 if (chunk->type == NBD_REPLY_TYPE_NONE) {
1001 /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */
1002 assert(chunk->flags & NBD_REPLY_FLAG_DONE);
1003 goto break_loop;
1004 }
1005
1006 if (chunk->flags & NBD_REPLY_FLAG_DONE) {
1007 /* This iteration is last. */
1008 iter->done = true;
1009 }
1010
1011 /* Execute the loop body */
1012 return true;
1013
1014 break_loop:
1015 s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL;
1016
1017 qemu_co_mutex_lock(&s->send_mutex);
1018 s->in_flight--;
1019 if (s->in_flight == 0 && s->wait_in_flight) {
1020 aio_co_wake(s->connection_co);
1021 } else {
1022 qemu_co_queue_next(&s->free_sema);
1023 }
1024 qemu_co_mutex_unlock(&s->send_mutex);
1025
1026 return false;
1027 }
1028
1029 static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle,
1030 int *request_ret, Error **errp)
1031 {
1032 NBDReplyChunkIter iter;
1033
1034 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) {
1035 /* nbd_reply_chunk_iter_receive does all the work */
1036 }
1037
1038 error_propagate(errp, iter.err);
1039 *request_ret = iter.request_ret;
1040 return iter.ret;
1041 }
1042
1043 static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle,
1044 uint64_t offset, QEMUIOVector *qiov,
1045 int *request_ret, Error **errp)
1046 {
1047 NBDReplyChunkIter iter;
1048 NBDReply reply;
1049 void *payload = NULL;
1050 Error *local_err = NULL;
1051
1052 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply,
1053 qiov, &reply, &payload)
1054 {
1055 int ret;
1056 NBDStructuredReplyChunk *chunk = &reply.structured;
1057
1058 assert(nbd_reply_is_structured(&reply));
1059
1060 switch (chunk->type) {
1061 case NBD_REPLY_TYPE_OFFSET_DATA:
1062 /*
1063 * special cased in nbd_co_receive_one_chunk, data is already
1064 * in qiov
1065 */
1066 break;
1067 case NBD_REPLY_TYPE_OFFSET_HOLE:
1068 ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload,
1069 offset, qiov, &local_err);
1070 if (ret < 0) {
1071 nbd_channel_error(s, ret);
1072 nbd_iter_channel_error(&iter, ret, &local_err);
1073 }
1074 break;
1075 default:
1076 if (!nbd_reply_type_is_error(chunk->type)) {
1077 /* not allowed reply type */
1078 nbd_channel_error(s, -EINVAL);
1079 error_setg(&local_err,
1080 "Unexpected reply type: %d (%s) for CMD_READ",
1081 chunk->type, nbd_reply_type_lookup(chunk->type));
1082 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1083 }
1084 }
1085
1086 g_free(payload);
1087 payload = NULL;
1088 }
1089
1090 error_propagate(errp, iter.err);
1091 *request_ret = iter.request_ret;
1092 return iter.ret;
1093 }
1094
1095 static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s,
1096 uint64_t handle, uint64_t length,
1097 NBDExtent *extent,
1098 int *request_ret, Error **errp)
1099 {
1100 NBDReplyChunkIter iter;
1101 NBDReply reply;
1102 void *payload = NULL;
1103 Error *local_err = NULL;
1104 bool received = false;
1105
1106 assert(!extent->length);
1107 NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) {
1108 int ret;
1109 NBDStructuredReplyChunk *chunk = &reply.structured;
1110
1111 assert(nbd_reply_is_structured(&reply));
1112
1113 switch (chunk->type) {
1114 case NBD_REPLY_TYPE_BLOCK_STATUS:
1115 if (received) {
1116 nbd_channel_error(s, -EINVAL);
1117 error_setg(&local_err, "Several BLOCK_STATUS chunks in reply");
1118 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1119 }
1120 received = true;
1121
1122 ret = nbd_parse_blockstatus_payload(s, &reply.structured,
1123 payload, length, extent,
1124 &local_err);
1125 if (ret < 0) {
1126 nbd_channel_error(s, ret);
1127 nbd_iter_channel_error(&iter, ret, &local_err);
1128 }
1129 break;
1130 default:
1131 if (!nbd_reply_type_is_error(chunk->type)) {
1132 nbd_channel_error(s, -EINVAL);
1133 error_setg(&local_err,
1134 "Unexpected reply type: %d (%s) "
1135 "for CMD_BLOCK_STATUS",
1136 chunk->type, nbd_reply_type_lookup(chunk->type));
1137 nbd_iter_channel_error(&iter, -EINVAL, &local_err);
1138 }
1139 }
1140
1141 g_free(payload);
1142 payload = NULL;
1143 }
1144
1145 if (!extent->length && !iter.request_ret) {
1146 error_setg(&local_err, "Server did not reply with any status extents");
1147 nbd_iter_channel_error(&iter, -EIO, &local_err);
1148 }
1149
1150 error_propagate(errp, iter.err);
1151 *request_ret = iter.request_ret;
1152 return iter.ret;
1153 }
1154
1155 static int nbd_co_request(BlockDriverState *bs, NBDRequest *request,
1156 QEMUIOVector *write_qiov)
1157 {
1158 int ret, request_ret;
1159 Error *local_err = NULL;
1160 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1161
1162 assert(request->type != NBD_CMD_READ);
1163 if (write_qiov) {
1164 assert(request->type == NBD_CMD_WRITE);
1165 assert(request->len == iov_size(write_qiov->iov, write_qiov->niov));
1166 } else {
1167 assert(request->type != NBD_CMD_WRITE);
1168 }
1169
1170 do {
1171 ret = nbd_co_send_request(bs, request, write_qiov);
1172 if (ret < 0) {
1173 continue;
1174 }
1175
1176 ret = nbd_co_receive_return_code(s, request->handle,
1177 &request_ret, &local_err);
1178 if (local_err) {
1179 trace_nbd_co_request_fail(request->from, request->len,
1180 request->handle, request->flags,
1181 request->type,
1182 nbd_cmd_lookup(request->type),
1183 ret, error_get_pretty(local_err));
1184 error_free(local_err);
1185 local_err = NULL;
1186 }
1187 } while (ret < 0 && nbd_client_connecting_wait(s));
1188
1189 return ret ? ret : request_ret;
1190 }
1191
1192 static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset,
1193 uint64_t bytes, QEMUIOVector *qiov, int flags)
1194 {
1195 int ret, request_ret;
1196 Error *local_err = NULL;
1197 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1198 NBDRequest request = {
1199 .type = NBD_CMD_READ,
1200 .from = offset,
1201 .len = bytes,
1202 };
1203
1204 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1205 assert(!flags);
1206
1207 if (!bytes) {
1208 return 0;
1209 }
1210 /*
1211 * Work around the fact that the block layer doesn't do
1212 * byte-accurate sizing yet - if the read exceeds the server's
1213 * advertised size because the block layer rounded size up, then
1214 * truncate the request to the server and tail-pad with zero.
1215 */
1216 if (offset >= s->info.size) {
1217 assert(bytes < BDRV_SECTOR_SIZE);
1218 qemu_iovec_memset(qiov, 0, 0, bytes);
1219 return 0;
1220 }
1221 if (offset + bytes > s->info.size) {
1222 uint64_t slop = offset + bytes - s->info.size;
1223
1224 assert(slop < BDRV_SECTOR_SIZE);
1225 qemu_iovec_memset(qiov, bytes - slop, 0, slop);
1226 request.len -= slop;
1227 }
1228
1229 do {
1230 ret = nbd_co_send_request(bs, &request, NULL);
1231 if (ret < 0) {
1232 continue;
1233 }
1234
1235 ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov,
1236 &request_ret, &local_err);
1237 if (local_err) {
1238 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1239 request.flags, request.type,
1240 nbd_cmd_lookup(request.type),
1241 ret, error_get_pretty(local_err));
1242 error_free(local_err);
1243 local_err = NULL;
1244 }
1245 } while (ret < 0 && nbd_client_connecting_wait(s));
1246
1247 return ret ? ret : request_ret;
1248 }
1249
1250 static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset,
1251 uint64_t bytes, QEMUIOVector *qiov, int flags)
1252 {
1253 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1254 NBDRequest request = {
1255 .type = NBD_CMD_WRITE,
1256 .from = offset,
1257 .len = bytes,
1258 };
1259
1260 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1261 if (flags & BDRV_REQ_FUA) {
1262 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1263 request.flags |= NBD_CMD_FLAG_FUA;
1264 }
1265
1266 assert(bytes <= NBD_MAX_BUFFER_SIZE);
1267
1268 if (!bytes) {
1269 return 0;
1270 }
1271 return nbd_co_request(bs, &request, qiov);
1272 }
1273
1274 static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1275 int bytes, BdrvRequestFlags flags)
1276 {
1277 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1278 NBDRequest request = {
1279 .type = NBD_CMD_WRITE_ZEROES,
1280 .from = offset,
1281 .len = bytes,
1282 };
1283
1284 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1285 if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) {
1286 return -ENOTSUP;
1287 }
1288
1289 if (flags & BDRV_REQ_FUA) {
1290 assert(s->info.flags & NBD_FLAG_SEND_FUA);
1291 request.flags |= NBD_CMD_FLAG_FUA;
1292 }
1293 if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1294 request.flags |= NBD_CMD_FLAG_NO_HOLE;
1295 }
1296 if (flags & BDRV_REQ_NO_FALLBACK) {
1297 assert(s->info.flags & NBD_FLAG_SEND_FAST_ZERO);
1298 request.flags |= NBD_CMD_FLAG_FAST_ZERO;
1299 }
1300
1301 if (!bytes) {
1302 return 0;
1303 }
1304 return nbd_co_request(bs, &request, NULL);
1305 }
1306
1307 static int nbd_client_co_flush(BlockDriverState *bs)
1308 {
1309 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1310 NBDRequest request = { .type = NBD_CMD_FLUSH };
1311
1312 if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) {
1313 return 0;
1314 }
1315
1316 request.from = 0;
1317 request.len = 0;
1318
1319 return nbd_co_request(bs, &request, NULL);
1320 }
1321
1322 static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset,
1323 int bytes)
1324 {
1325 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1326 NBDRequest request = {
1327 .type = NBD_CMD_TRIM,
1328 .from = offset,
1329 .len = bytes,
1330 };
1331
1332 assert(!(s->info.flags & NBD_FLAG_READ_ONLY));
1333 if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) {
1334 return 0;
1335 }
1336
1337 return nbd_co_request(bs, &request, NULL);
1338 }
1339
1340 static int coroutine_fn nbd_client_co_block_status(
1341 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
1342 int64_t *pnum, int64_t *map, BlockDriverState **file)
1343 {
1344 int ret, request_ret;
1345 NBDExtent extent = { 0 };
1346 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1347 Error *local_err = NULL;
1348
1349 NBDRequest request = {
1350 .type = NBD_CMD_BLOCK_STATUS,
1351 .from = offset,
1352 .len = MIN(QEMU_ALIGN_DOWN(INT_MAX, bs->bl.request_alignment),
1353 MIN(bytes, s->info.size - offset)),
1354 .flags = NBD_CMD_FLAG_REQ_ONE,
1355 };
1356
1357 if (!s->info.base_allocation) {
1358 *pnum = bytes;
1359 *map = offset;
1360 *file = bs;
1361 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1362 }
1363
1364 /*
1365 * Work around the fact that the block layer doesn't do
1366 * byte-accurate sizing yet - if the status request exceeds the
1367 * server's advertised size because the block layer rounded size
1368 * up, we truncated the request to the server (above), or are
1369 * called on just the hole.
1370 */
1371 if (offset >= s->info.size) {
1372 *pnum = bytes;
1373 assert(bytes < BDRV_SECTOR_SIZE);
1374 /* Intentionally don't report offset_valid for the hole */
1375 return BDRV_BLOCK_ZERO;
1376 }
1377
1378 if (s->info.min_block) {
1379 assert(QEMU_IS_ALIGNED(request.len, s->info.min_block));
1380 }
1381 do {
1382 ret = nbd_co_send_request(bs, &request, NULL);
1383 if (ret < 0) {
1384 continue;
1385 }
1386
1387 ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes,
1388 &extent, &request_ret,
1389 &local_err);
1390 if (local_err) {
1391 trace_nbd_co_request_fail(request.from, request.len, request.handle,
1392 request.flags, request.type,
1393 nbd_cmd_lookup(request.type),
1394 ret, error_get_pretty(local_err));
1395 error_free(local_err);
1396 local_err = NULL;
1397 }
1398 } while (ret < 0 && nbd_client_connecting_wait(s));
1399
1400 if (ret < 0 || request_ret < 0) {
1401 return ret ? ret : request_ret;
1402 }
1403
1404 assert(extent.length);
1405 *pnum = extent.length;
1406 *map = offset;
1407 *file = bs;
1408 return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) |
1409 (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) |
1410 BDRV_BLOCK_OFFSET_VALID;
1411 }
1412
1413 static int nbd_client_reopen_prepare(BDRVReopenState *state,
1414 BlockReopenQueue *queue, Error **errp)
1415 {
1416 BDRVNBDState *s = (BDRVNBDState *)state->bs->opaque;
1417
1418 if ((state->flags & BDRV_O_RDWR) && (s->info.flags & NBD_FLAG_READ_ONLY)) {
1419 error_setg(errp, "Can't reopen read-only NBD mount as read/write");
1420 return -EACCES;
1421 }
1422 return 0;
1423 }
1424
1425 static void nbd_client_close(BlockDriverState *bs)
1426 {
1427 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1428 NBDRequest request = { .type = NBD_CMD_DISC };
1429
1430 if (s->ioc) {
1431 nbd_send_request(s->ioc, &request);
1432 }
1433
1434 nbd_teardown_connection(bs);
1435 }
1436
1437 static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
1438 Error **errp)
1439 {
1440 ERRP_GUARD();
1441 QIOChannelSocket *sioc;
1442
1443 sioc = qio_channel_socket_new();
1444 qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client");
1445
1446 qio_channel_socket_connect_sync(sioc, saddr, errp);
1447 if (*errp) {
1448 object_unref(OBJECT(sioc));
1449 return NULL;
1450 }
1451
1452 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
1453
1454 return sioc;
1455 }
1456
1457 /* nbd_client_handshake takes ownership on sioc. On failure it is unref'ed. */
1458 static int nbd_client_handshake(BlockDriverState *bs, QIOChannelSocket *sioc,
1459 Error **errp)
1460 {
1461 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1462 AioContext *aio_context = bdrv_get_aio_context(bs);
1463 int ret;
1464
1465 trace_nbd_client_handshake(s->export);
1466
1467 s->sioc = sioc;
1468
1469 qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL);
1470 qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context);
1471
1472 s->info.request_sizes = true;
1473 s->info.structured_reply = true;
1474 s->info.base_allocation = true;
1475 s->info.x_dirty_bitmap = g_strdup(s->x_dirty_bitmap);
1476 s->info.name = g_strdup(s->export ?: "");
1477 ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), s->tlscreds,
1478 s->hostname, &s->ioc, &s->info, errp);
1479 g_free(s->info.x_dirty_bitmap);
1480 g_free(s->info.name);
1481 if (ret < 0) {
1482 object_unref(OBJECT(sioc));
1483 s->sioc = NULL;
1484 return ret;
1485 }
1486 if (s->x_dirty_bitmap && !s->info.base_allocation) {
1487 error_setg(errp, "requested x-dirty-bitmap %s not found",
1488 s->x_dirty_bitmap);
1489 ret = -EINVAL;
1490 goto fail;
1491 }
1492 if (s->info.flags & NBD_FLAG_READ_ONLY) {
1493 ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp);
1494 if (ret < 0) {
1495 goto fail;
1496 }
1497 }
1498 if (s->info.flags & NBD_FLAG_SEND_FUA) {
1499 bs->supported_write_flags = BDRV_REQ_FUA;
1500 bs->supported_zero_flags |= BDRV_REQ_FUA;
1501 }
1502 if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) {
1503 bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP;
1504 if (s->info.flags & NBD_FLAG_SEND_FAST_ZERO) {
1505 bs->supported_zero_flags |= BDRV_REQ_NO_FALLBACK;
1506 }
1507 }
1508
1509 if (!s->ioc) {
1510 s->ioc = QIO_CHANNEL(sioc);
1511 object_ref(OBJECT(s->ioc));
1512 }
1513
1514 trace_nbd_client_handshake_success(s->export);
1515
1516 return 0;
1517
1518 fail:
1519 /*
1520 * We have connected, but must fail for other reasons.
1521 * Send NBD_CMD_DISC as a courtesy to the server.
1522 */
1523 {
1524 NBDRequest request = { .type = NBD_CMD_DISC };
1525
1526 nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request);
1527
1528 object_unref(OBJECT(sioc));
1529 s->sioc = NULL;
1530
1531 return ret;
1532 }
1533 }
1534
1535 /*
1536 * Parse nbd_open options
1537 */
1538
1539 static int nbd_parse_uri(const char *filename, QDict *options)
1540 {
1541 URI *uri;
1542 const char *p;
1543 QueryParams *qp = NULL;
1544 int ret = 0;
1545 bool is_unix;
1546
1547 uri = uri_parse(filename);
1548 if (!uri) {
1549 return -EINVAL;
1550 }
1551
1552 /* transport */
1553 if (!g_strcmp0(uri->scheme, "nbd")) {
1554 is_unix = false;
1555 } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) {
1556 is_unix = false;
1557 } else if (!g_strcmp0(uri->scheme, "nbd+unix")) {
1558 is_unix = true;
1559 } else {
1560 ret = -EINVAL;
1561 goto out;
1562 }
1563
1564 p = uri->path ? uri->path : "";
1565 if (p[0] == '/') {
1566 p++;
1567 }
1568 if (p[0]) {
1569 qdict_put_str(options, "export", p);
1570 }
1571
1572 qp = query_params_parse(uri->query);
1573 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1574 ret = -EINVAL;
1575 goto out;
1576 }
1577
1578 if (is_unix) {
1579 /* nbd+unix:///export?socket=path */
1580 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
1581 ret = -EINVAL;
1582 goto out;
1583 }
1584 qdict_put_str(options, "server.type", "unix");
1585 qdict_put_str(options, "server.path", qp->p[0].value);
1586 } else {
1587 QString *host;
1588 char *port_str;
1589
1590 /* nbd[+tcp]://host[:port]/export */
1591 if (!uri->server) {
1592 ret = -EINVAL;
1593 goto out;
1594 }
1595
1596 /* strip braces from literal IPv6 address */
1597 if (uri->server[0] == '[') {
1598 host = qstring_from_substr(uri->server, 1,
1599 strlen(uri->server) - 1);
1600 } else {
1601 host = qstring_from_str(uri->server);
1602 }
1603
1604 qdict_put_str(options, "server.type", "inet");
1605 qdict_put(options, "server.host", host);
1606
1607 port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT);
1608 qdict_put_str(options, "server.port", port_str);
1609 g_free(port_str);
1610 }
1611
1612 out:
1613 if (qp) {
1614 query_params_free(qp);
1615 }
1616 uri_free(uri);
1617 return ret;
1618 }
1619
1620 static bool nbd_has_filename_options_conflict(QDict *options, Error **errp)
1621 {
1622 const QDictEntry *e;
1623
1624 for (e = qdict_first(options); e; e = qdict_next(options, e)) {
1625 if (!strcmp(e->key, "host") ||
1626 !strcmp(e->key, "port") ||
1627 !strcmp(e->key, "path") ||
1628 !strcmp(e->key, "export") ||
1629 strstart(e->key, "server.", NULL))
1630 {
1631 error_setg(errp, "Option '%s' cannot be used with a file name",
1632 e->key);
1633 return true;
1634 }
1635 }
1636
1637 return false;
1638 }
1639
1640 static void nbd_parse_filename(const char *filename, QDict *options,
1641 Error **errp)
1642 {
1643 g_autofree char *file = NULL;
1644 char *export_name;
1645 const char *host_spec;
1646 const char *unixpath;
1647
1648 if (nbd_has_filename_options_conflict(options, errp)) {
1649 return;
1650 }
1651
1652 if (strstr(filename, "://")) {
1653 int ret = nbd_parse_uri(filename, options);
1654 if (ret < 0) {
1655 error_setg(errp, "No valid URL specified");
1656 }
1657 return;
1658 }
1659
1660 file = g_strdup(filename);
1661
1662 export_name = strstr(file, EN_OPTSTR);
1663 if (export_name) {
1664 if (export_name[strlen(EN_OPTSTR)] == 0) {
1665 return;
1666 }
1667 export_name[0] = 0; /* truncate 'file' */
1668 export_name += strlen(EN_OPTSTR);
1669
1670 qdict_put_str(options, "export", export_name);
1671 }
1672
1673 /* extract the host_spec - fail if it's not nbd:... */
1674 if (!strstart(file, "nbd:", &host_spec)) {
1675 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1676 return;
1677 }
1678
1679 if (!*host_spec) {
1680 return;
1681 }
1682
1683 /* are we a UNIX or TCP socket? */
1684 if (strstart(host_spec, "unix:", &unixpath)) {
1685 qdict_put_str(options, "server.type", "unix");
1686 qdict_put_str(options, "server.path", unixpath);
1687 } else {
1688 InetSocketAddress *addr = g_new(InetSocketAddress, 1);
1689
1690 if (inet_parse(addr, host_spec, errp)) {
1691 goto out_inet;
1692 }
1693
1694 qdict_put_str(options, "server.type", "inet");
1695 qdict_put_str(options, "server.host", addr->host);
1696 qdict_put_str(options, "server.port", addr->port);
1697 out_inet:
1698 qapi_free_InetSocketAddress(addr);
1699 }
1700 }
1701
1702 static bool nbd_process_legacy_socket_options(QDict *output_options,
1703 QemuOpts *legacy_opts,
1704 Error **errp)
1705 {
1706 const char *path = qemu_opt_get(legacy_opts, "path");
1707 const char *host = qemu_opt_get(legacy_opts, "host");
1708 const char *port = qemu_opt_get(legacy_opts, "port");
1709 const QDictEntry *e;
1710
1711 if (!path && !host && !port) {
1712 return true;
1713 }
1714
1715 for (e = qdict_first(output_options); e; e = qdict_next(output_options, e))
1716 {
1717 if (strstart(e->key, "server.", NULL)) {
1718 error_setg(errp, "Cannot use 'server' and path/host/port at the "
1719 "same time");
1720 return false;
1721 }
1722 }
1723
1724 if (path && host) {
1725 error_setg(errp, "path and host may not be used at the same time");
1726 return false;
1727 } else if (path) {
1728 if (port) {
1729 error_setg(errp, "port may not be used without host");
1730 return false;
1731 }
1732
1733 qdict_put_str(output_options, "server.type", "unix");
1734 qdict_put_str(output_options, "server.path", path);
1735 } else if (host) {
1736 qdict_put_str(output_options, "server.type", "inet");
1737 qdict_put_str(output_options, "server.host", host);
1738 qdict_put_str(output_options, "server.port",
1739 port ?: stringify(NBD_DEFAULT_PORT));
1740 }
1741
1742 return true;
1743 }
1744
1745 static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options,
1746 Error **errp)
1747 {
1748 SocketAddress *saddr = NULL;
1749 QDict *addr = NULL;
1750 Visitor *iv = NULL;
1751
1752 qdict_extract_subqdict(options, &addr, "server.");
1753 if (!qdict_size(addr)) {
1754 error_setg(errp, "NBD server address missing");
1755 goto done;
1756 }
1757
1758 iv = qobject_input_visitor_new_flat_confused(addr, errp);
1759 if (!iv) {
1760 goto done;
1761 }
1762
1763 if (!visit_type_SocketAddress(iv, NULL, &saddr, errp)) {
1764 goto done;
1765 }
1766
1767 done:
1768 qobject_unref(addr);
1769 visit_free(iv);
1770 return saddr;
1771 }
1772
1773 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
1774 {
1775 Object *obj;
1776 QCryptoTLSCreds *creds;
1777
1778 obj = object_resolve_path_component(
1779 object_get_objects_root(), id);
1780 if (!obj) {
1781 error_setg(errp, "No TLS credentials with id '%s'",
1782 id);
1783 return NULL;
1784 }
1785 creds = (QCryptoTLSCreds *)
1786 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
1787 if (!creds) {
1788 error_setg(errp, "Object with id '%s' is not TLS credentials",
1789 id);
1790 return NULL;
1791 }
1792
1793 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
1794 error_setg(errp,
1795 "Expecting TLS credentials with a client endpoint");
1796 return NULL;
1797 }
1798 object_ref(obj);
1799 return creds;
1800 }
1801
1802
1803 static QemuOptsList nbd_runtime_opts = {
1804 .name = "nbd",
1805 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
1806 .desc = {
1807 {
1808 .name = "host",
1809 .type = QEMU_OPT_STRING,
1810 .help = "TCP host to connect to",
1811 },
1812 {
1813 .name = "port",
1814 .type = QEMU_OPT_STRING,
1815 .help = "TCP port to connect to",
1816 },
1817 {
1818 .name = "path",
1819 .type = QEMU_OPT_STRING,
1820 .help = "Unix socket path to connect to",
1821 },
1822 {
1823 .name = "export",
1824 .type = QEMU_OPT_STRING,
1825 .help = "Name of the NBD export to open",
1826 },
1827 {
1828 .name = "tls-creds",
1829 .type = QEMU_OPT_STRING,
1830 .help = "ID of the TLS credentials to use",
1831 },
1832 {
1833 .name = "x-dirty-bitmap",
1834 .type = QEMU_OPT_STRING,
1835 .help = "experimental: expose named dirty bitmap in place of "
1836 "block status",
1837 },
1838 {
1839 .name = "reconnect-delay",
1840 .type = QEMU_OPT_NUMBER,
1841 .help = "On an unexpected disconnect, the nbd client tries to "
1842 "connect again until succeeding or encountering a serious "
1843 "error. During the first @reconnect-delay seconds, all "
1844 "requests are paused and will be rerun on a successful "
1845 "reconnect. After that time, any delayed requests and all "
1846 "future requests before a successful reconnect will "
1847 "immediately fail. Default 0",
1848 },
1849 { /* end of list */ }
1850 },
1851 };
1852
1853 static int nbd_process_options(BlockDriverState *bs, QDict *options,
1854 Error **errp)
1855 {
1856 BDRVNBDState *s = bs->opaque;
1857 QemuOpts *opts;
1858 int ret = -EINVAL;
1859
1860 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
1861 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
1862 goto error;
1863 }
1864
1865 /* Translate @host, @port, and @path to a SocketAddress */
1866 if (!nbd_process_legacy_socket_options(options, opts, errp)) {
1867 goto error;
1868 }
1869
1870 /* Pop the config into our state object. Exit if invalid. */
1871 s->saddr = nbd_config(s, options, errp);
1872 if (!s->saddr) {
1873 goto error;
1874 }
1875
1876 s->export = g_strdup(qemu_opt_get(opts, "export"));
1877 if (s->export && strlen(s->export) > NBD_MAX_STRING_SIZE) {
1878 error_setg(errp, "export name too long to send to server");
1879 goto error;
1880 }
1881
1882 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
1883 if (s->tlscredsid) {
1884 s->tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
1885 if (!s->tlscreds) {
1886 goto error;
1887 }
1888
1889 /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */
1890 if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) {
1891 error_setg(errp, "TLS only supported over IP sockets");
1892 goto error;
1893 }
1894 s->hostname = s->saddr->u.inet.host;
1895 }
1896
1897 s->x_dirty_bitmap = g_strdup(qemu_opt_get(opts, "x-dirty-bitmap"));
1898 if (s->x_dirty_bitmap && strlen(s->x_dirty_bitmap) > NBD_MAX_STRING_SIZE) {
1899 error_setg(errp, "x-dirty-bitmap query too long to send to server");
1900 goto error;
1901 }
1902
1903 s->reconnect_delay = qemu_opt_get_number(opts, "reconnect-delay", 0);
1904
1905 ret = 0;
1906
1907 error:
1908 if (ret < 0) {
1909 nbd_clear_bdrvstate(s);
1910 }
1911 qemu_opts_del(opts);
1912 return ret;
1913 }
1914
1915 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
1916 Error **errp)
1917 {
1918 int ret;
1919 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1920 QIOChannelSocket *sioc;
1921
1922 ret = nbd_process_options(bs, options, errp);
1923 if (ret < 0) {
1924 return ret;
1925 }
1926
1927 s->bs = bs;
1928 qemu_co_mutex_init(&s->send_mutex);
1929 qemu_co_queue_init(&s->free_sema);
1930
1931 /*
1932 * establish TCP connection, return error if it fails
1933 * TODO: Configurable retry-until-timeout behaviour.
1934 */
1935 sioc = nbd_establish_connection(s->saddr, errp);
1936 if (!sioc) {
1937 return -ECONNREFUSED;
1938 }
1939
1940 ret = nbd_client_handshake(bs, sioc, errp);
1941 if (ret < 0) {
1942 nbd_clear_bdrvstate(s);
1943 return ret;
1944 }
1945 /* successfully connected */
1946 s->state = NBD_CLIENT_CONNECTED;
1947
1948 s->connection_co = qemu_coroutine_create(nbd_connection_entry, s);
1949 bdrv_inc_in_flight(bs);
1950 aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co);
1951
1952 return 0;
1953 }
1954
1955 static int nbd_co_flush(BlockDriverState *bs)
1956 {
1957 return nbd_client_co_flush(bs);
1958 }
1959
1960 static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
1961 {
1962 BDRVNBDState *s = (BDRVNBDState *)bs->opaque;
1963 uint32_t min = s->info.min_block;
1964 uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block);
1965
1966 /*
1967 * If the server did not advertise an alignment:
1968 * - a size that is not sector-aligned implies that an alignment
1969 * of 1 can be used to access those tail bytes
1970 * - advertisement of block status requires an alignment of 1, so
1971 * that we don't violate block layer constraints that block
1972 * status is always aligned (as we can't control whether the
1973 * server will report sub-sector extents, such as a hole at EOF
1974 * on an unaligned POSIX file)
1975 * - otherwise, assume the server is so old that we are safer avoiding
1976 * sub-sector requests
1977 */
1978 if (!min) {
1979 min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) ||
1980 s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE;
1981 }
1982
1983 bs->bl.request_alignment = min;
1984 bs->bl.max_pdiscard = QEMU_ALIGN_DOWN(INT_MAX, min);
1985 bs->bl.max_pwrite_zeroes = max;
1986 bs->bl.max_transfer = max;
1987
1988 if (s->info.opt_block &&
1989 s->info.opt_block > bs->bl.opt_transfer) {
1990 bs->bl.opt_transfer = s->info.opt_block;
1991 }
1992 }
1993
1994 static void nbd_close(BlockDriverState *bs)
1995 {
1996 BDRVNBDState *s = bs->opaque;
1997
1998 nbd_client_close(bs);
1999 nbd_clear_bdrvstate(s);
2000 }
2001
2002 /*
2003 * NBD cannot truncate, but if the caller asks to truncate to the same size, or
2004 * to a smaller size with exact=false, there is no reason to fail the
2005 * operation.
2006 *
2007 * Preallocation mode is ignored since it does not seems useful to fail when
2008 * we never change anything.
2009 */
2010 static int coroutine_fn nbd_co_truncate(BlockDriverState *bs, int64_t offset,
2011 bool exact, PreallocMode prealloc,
2012 BdrvRequestFlags flags, Error **errp)
2013 {
2014 BDRVNBDState *s = bs->opaque;
2015
2016 if (offset != s->info.size && exact) {
2017 error_setg(errp, "Cannot resize NBD nodes");
2018 return -ENOTSUP;
2019 }
2020
2021 if (offset > s->info.size) {
2022 error_setg(errp, "Cannot grow NBD nodes");
2023 return -EINVAL;
2024 }
2025
2026 return 0;
2027 }
2028
2029 static int64_t nbd_getlength(BlockDriverState *bs)
2030 {
2031 BDRVNBDState *s = bs->opaque;
2032
2033 return s->info.size;
2034 }
2035
2036 static void nbd_refresh_filename(BlockDriverState *bs)
2037 {
2038 BDRVNBDState *s = bs->opaque;
2039 const char *host = NULL, *port = NULL, *path = NULL;
2040 size_t len = 0;
2041
2042 if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) {
2043 const InetSocketAddress *inet = &s->saddr->u.inet;
2044 if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) {
2045 host = inet->host;
2046 port = inet->port;
2047 }
2048 } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) {
2049 path = s->saddr->u.q_unix.path;
2050 } /* else can't represent as pseudo-filename */
2051
2052 if (path && s->export) {
2053 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2054 "nbd+unix:///%s?socket=%s", s->export, path);
2055 } else if (path && !s->export) {
2056 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2057 "nbd+unix://?socket=%s", path);
2058 } else if (host && s->export) {
2059 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2060 "nbd://%s:%s/%s", host, port, s->export);
2061 } else if (host && !s->export) {
2062 len = snprintf(bs->exact_filename, sizeof(bs->exact_filename),
2063 "nbd://%s:%s", host, port);
2064 }
2065 if (len >= sizeof(bs->exact_filename)) {
2066 /* Name is too long to represent exactly, so leave it empty. */
2067 bs->exact_filename[0] = '\0';
2068 }
2069 }
2070
2071 static char *nbd_dirname(BlockDriverState *bs, Error **errp)
2072 {
2073 /* The generic bdrv_dirname() implementation is able to work out some
2074 * directory name for NBD nodes, but that would be wrong. So far there is no
2075 * specification for how "export paths" would work, so NBD does not have
2076 * directory names. */
2077 error_setg(errp, "Cannot generate a base directory for NBD nodes");
2078 return NULL;
2079 }
2080
2081 static const char *const nbd_strong_runtime_opts[] = {
2082 "path",
2083 "host",
2084 "port",
2085 "export",
2086 "tls-creds",
2087 "server.",
2088
2089 NULL
2090 };
2091
2092 static BlockDriver bdrv_nbd = {
2093 .format_name = "nbd",
2094 .protocol_name = "nbd",
2095 .instance_size = sizeof(BDRVNBDState),
2096 .bdrv_parse_filename = nbd_parse_filename,
2097 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2098 .create_opts = &bdrv_create_opts_simple,
2099 .bdrv_file_open = nbd_open,
2100 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2101 .bdrv_co_preadv = nbd_client_co_preadv,
2102 .bdrv_co_pwritev = nbd_client_co_pwritev,
2103 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2104 .bdrv_close = nbd_close,
2105 .bdrv_co_flush_to_os = nbd_co_flush,
2106 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2107 .bdrv_refresh_limits = nbd_refresh_limits,
2108 .bdrv_co_truncate = nbd_co_truncate,
2109 .bdrv_getlength = nbd_getlength,
2110 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2111 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2112 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2113 .bdrv_co_drain_end = nbd_client_co_drain_end,
2114 .bdrv_refresh_filename = nbd_refresh_filename,
2115 .bdrv_co_block_status = nbd_client_co_block_status,
2116 .bdrv_dirname = nbd_dirname,
2117 .strong_runtime_opts = nbd_strong_runtime_opts,
2118 };
2119
2120 static BlockDriver bdrv_nbd_tcp = {
2121 .format_name = "nbd",
2122 .protocol_name = "nbd+tcp",
2123 .instance_size = sizeof(BDRVNBDState),
2124 .bdrv_parse_filename = nbd_parse_filename,
2125 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2126 .create_opts = &bdrv_create_opts_simple,
2127 .bdrv_file_open = nbd_open,
2128 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2129 .bdrv_co_preadv = nbd_client_co_preadv,
2130 .bdrv_co_pwritev = nbd_client_co_pwritev,
2131 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2132 .bdrv_close = nbd_close,
2133 .bdrv_co_flush_to_os = nbd_co_flush,
2134 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2135 .bdrv_refresh_limits = nbd_refresh_limits,
2136 .bdrv_co_truncate = nbd_co_truncate,
2137 .bdrv_getlength = nbd_getlength,
2138 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2139 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2140 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2141 .bdrv_co_drain_end = nbd_client_co_drain_end,
2142 .bdrv_refresh_filename = nbd_refresh_filename,
2143 .bdrv_co_block_status = nbd_client_co_block_status,
2144 .bdrv_dirname = nbd_dirname,
2145 .strong_runtime_opts = nbd_strong_runtime_opts,
2146 };
2147
2148 static BlockDriver bdrv_nbd_unix = {
2149 .format_name = "nbd",
2150 .protocol_name = "nbd+unix",
2151 .instance_size = sizeof(BDRVNBDState),
2152 .bdrv_parse_filename = nbd_parse_filename,
2153 .bdrv_co_create_opts = bdrv_co_create_opts_simple,
2154 .create_opts = &bdrv_create_opts_simple,
2155 .bdrv_file_open = nbd_open,
2156 .bdrv_reopen_prepare = nbd_client_reopen_prepare,
2157 .bdrv_co_preadv = nbd_client_co_preadv,
2158 .bdrv_co_pwritev = nbd_client_co_pwritev,
2159 .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes,
2160 .bdrv_close = nbd_close,
2161 .bdrv_co_flush_to_os = nbd_co_flush,
2162 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
2163 .bdrv_refresh_limits = nbd_refresh_limits,
2164 .bdrv_co_truncate = nbd_co_truncate,
2165 .bdrv_getlength = nbd_getlength,
2166 .bdrv_detach_aio_context = nbd_client_detach_aio_context,
2167 .bdrv_attach_aio_context = nbd_client_attach_aio_context,
2168 .bdrv_co_drain_begin = nbd_client_co_drain_begin,
2169 .bdrv_co_drain_end = nbd_client_co_drain_end,
2170 .bdrv_refresh_filename = nbd_refresh_filename,
2171 .bdrv_co_block_status = nbd_client_co_block_status,
2172 .bdrv_dirname = nbd_dirname,
2173 .strong_runtime_opts = nbd_strong_runtime_opts,
2174 };
2175
2176 static void bdrv_nbd_init(void)
2177 {
2178 bdrv_register(&bdrv_nbd);
2179 bdrv_register(&bdrv_nbd_tcp);
2180 bdrv_register(&bdrv_nbd_unix);
2181 }
2182
2183 block_init(bdrv_nbd_init);