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