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