]>
Commit | Line | Data |
---|---|---|
75818250 TS |
1 | /* |
2 | * QEMU Block driver for NBD | |
3 | * | |
86f8cdf3 | 4 | * Copyright (C) 2016 Red Hat, Inc. |
75818250 | 5 | * Copyright (C) 2008 Bull S.A.S. |
bd5921b4 | 6 | * Author: Laurent Vivier <Laurent.Vivier@bull.net> |
75818250 TS |
7 | * |
8 | * Some parts: | |
9 | * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws> | |
10 | * | |
11 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
12 | * of this software and associated documentation files (the "Software"), to deal | |
13 | * in the Software without restriction, including without limitation the rights | |
14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
15 | * copies of the Software, and to permit persons to whom the Software is | |
16 | * furnished to do so, subject to the following conditions: | |
17 | * | |
18 | * The above copyright notice and this permission notice shall be included in | |
19 | * all copies or substantial portions of the Software. | |
20 | * | |
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
24 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
27 | * THE SOFTWARE. | |
28 | */ | |
29 | ||
80c71a24 | 30 | #include "qemu/osdep.h" |
86f8cdf3 VSO |
31 | |
32 | #include "trace.h" | |
1de7afc9 | 33 | #include "qemu/uri.h" |
922a01a0 | 34 | #include "qemu/option.h" |
86f8cdf3 VSO |
35 | #include "qemu/cutils.h" |
36 | ||
9af23989 | 37 | #include "qapi/qapi-visit-sockets.h" |
2019d68b | 38 | #include "qapi/qmp/qstring.h" |
86f8cdf3 VSO |
39 | |
40 | #include "block/qdict.h" | |
41 | #include "block/nbd.h" | |
42 | #include "block/block_int.h" | |
75818250 | 43 | |
1d45f8b5 | 44 | #define EN_OPTSTR ":exportname=" |
86f8cdf3 VSO |
45 | #define MAX_NBD_REQUESTS 16 |
46 | ||
47 | #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) | |
48 | #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) | |
49 | ||
50 | typedef struct { | |
51 | Coroutine *coroutine; | |
52 | uint64_t offset; /* original offset of the request */ | |
53 | bool receiving; /* waiting for connection_co? */ | |
54 | } NBDClientRequest; | |
55 | ||
611ae1d7 | 56 | typedef struct BDRVNBDState { |
86f8cdf3 VSO |
57 | QIOChannelSocket *sioc; /* The master data channel */ |
58 | QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */ | |
59 | NBDExportInfo info; | |
60 | ||
61 | CoMutex send_mutex; | |
62 | CoQueue free_sema; | |
63 | Coroutine *connection_co; | |
64 | int in_flight; | |
65 | ||
66 | NBDClientRequest requests[MAX_NBD_REQUESTS]; | |
67 | NBDReply reply; | |
68 | BlockDriverState *bs; | |
69 | bool quit; | |
03504d05 HR |
70 | |
71 | /* For nbd_refresh_filename() */ | |
62cf396b | 72 | SocketAddress *saddr; |
491d6c7c | 73 | char *export, *tlscredsid; |
75818250 TS |
74 | } BDRVNBDState; |
75 | ||
611ae1d7 | 76 | static void nbd_recv_coroutines_wake_all(BDRVNBDState *s) |
86f8cdf3 VSO |
77 | { |
78 | int i; | |
79 | ||
80 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
81 | NBDClientRequest *req = &s->requests[i]; | |
82 | ||
83 | if (req->coroutine && req->receiving) { | |
84 | aio_co_wake(req->coroutine); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | static void nbd_client_detach_aio_context(BlockDriverState *bs) | |
90 | { | |
611ae1d7 VSO |
91 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
92 | ||
93 | qio_channel_detach_aio_context(QIO_CHANNEL(s->ioc)); | |
86f8cdf3 VSO |
94 | } |
95 | ||
96 | static void nbd_client_attach_aio_context_bh(void *opaque) | |
97 | { | |
98 | BlockDriverState *bs = opaque; | |
611ae1d7 | 99 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
100 | |
101 | /* | |
102 | * The node is still drained, so we know the coroutine has yielded in | |
103 | * nbd_read_eof(), the only place where bs->in_flight can reach 0, or it is | |
104 | * entered for the first time. Both places are safe for entering the | |
105 | * coroutine. | |
106 | */ | |
611ae1d7 | 107 | qemu_aio_coroutine_enter(bs->aio_context, s->connection_co); |
86f8cdf3 VSO |
108 | bdrv_dec_in_flight(bs); |
109 | } | |
110 | ||
111 | static void nbd_client_attach_aio_context(BlockDriverState *bs, | |
112 | AioContext *new_context) | |
113 | { | |
611ae1d7 VSO |
114 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
115 | ||
116 | qio_channel_attach_aio_context(QIO_CHANNEL(s->ioc), new_context); | |
86f8cdf3 VSO |
117 | |
118 | bdrv_inc_in_flight(bs); | |
119 | ||
120 | /* | |
121 | * Need to wait here for the BH to run because the BH must run while the | |
122 | * node is still drained. | |
123 | */ | |
124 | aio_wait_bh_oneshot(new_context, nbd_client_attach_aio_context_bh, bs); | |
125 | } | |
126 | ||
127 | ||
128 | static void nbd_teardown_connection(BlockDriverState *bs) | |
129 | { | |
611ae1d7 | 130 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 | 131 | |
611ae1d7 | 132 | assert(s->ioc); |
86f8cdf3 VSO |
133 | |
134 | /* finish any pending coroutines */ | |
611ae1d7 | 135 | qio_channel_shutdown(s->ioc, |
86f8cdf3 VSO |
136 | QIO_CHANNEL_SHUTDOWN_BOTH, |
137 | NULL); | |
611ae1d7 | 138 | BDRV_POLL_WHILE(bs, s->connection_co); |
86f8cdf3 VSO |
139 | |
140 | nbd_client_detach_aio_context(bs); | |
611ae1d7 VSO |
141 | object_unref(OBJECT(s->sioc)); |
142 | s->sioc = NULL; | |
143 | object_unref(OBJECT(s->ioc)); | |
144 | s->ioc = NULL; | |
86f8cdf3 VSO |
145 | } |
146 | ||
147 | static coroutine_fn void nbd_connection_entry(void *opaque) | |
148 | { | |
611ae1d7 | 149 | BDRVNBDState *s = opaque; |
86f8cdf3 VSO |
150 | uint64_t i; |
151 | int ret = 0; | |
152 | Error *local_err = NULL; | |
153 | ||
154 | while (!s->quit) { | |
155 | /* | |
156 | * The NBD client can only really be considered idle when it has | |
157 | * yielded from qio_channel_readv_all_eof(), waiting for data. This is | |
158 | * the point where the additional scheduled coroutine entry happens | |
159 | * after nbd_client_attach_aio_context(). | |
160 | * | |
161 | * Therefore we keep an additional in_flight reference all the time and | |
162 | * only drop it temporarily here. | |
163 | */ | |
164 | assert(s->reply.handle == 0); | |
165 | ret = nbd_receive_reply(s->bs, s->ioc, &s->reply, &local_err); | |
166 | ||
167 | if (local_err) { | |
168 | trace_nbd_read_reply_entry_fail(ret, error_get_pretty(local_err)); | |
169 | error_free(local_err); | |
170 | } | |
171 | if (ret <= 0) { | |
172 | break; | |
173 | } | |
174 | ||
175 | /* | |
176 | * There's no need for a mutex on the receive side, because the | |
177 | * handler acts as a synchronization point and ensures that only | |
178 | * one coroutine is called until the reply finishes. | |
179 | */ | |
180 | i = HANDLE_TO_INDEX(s, s->reply.handle); | |
181 | if (i >= MAX_NBD_REQUESTS || | |
182 | !s->requests[i].coroutine || | |
183 | !s->requests[i].receiving || | |
184 | (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) | |
185 | { | |
186 | break; | |
187 | } | |
188 | ||
189 | /* | |
190 | * We're woken up again by the request itself. Note that there | |
191 | * is no race between yielding and reentering connection_co. This | |
192 | * is because: | |
193 | * | |
194 | * - if the request runs on the same AioContext, it is only | |
195 | * entered after we yield | |
196 | * | |
197 | * - if the request runs on a different AioContext, reentering | |
198 | * connection_co happens through a bottom half, which can only | |
199 | * run after we yield. | |
200 | */ | |
201 | aio_co_wake(s->requests[i].coroutine); | |
202 | qemu_coroutine_yield(); | |
203 | } | |
204 | ||
205 | s->quit = true; | |
206 | nbd_recv_coroutines_wake_all(s); | |
207 | bdrv_dec_in_flight(s->bs); | |
208 | ||
209 | s->connection_co = NULL; | |
210 | aio_wait_kick(); | |
211 | } | |
212 | ||
213 | static int nbd_co_send_request(BlockDriverState *bs, | |
214 | NBDRequest *request, | |
215 | QEMUIOVector *qiov) | |
216 | { | |
611ae1d7 | 217 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
218 | int rc, i; |
219 | ||
220 | qemu_co_mutex_lock(&s->send_mutex); | |
221 | while (s->in_flight == MAX_NBD_REQUESTS) { | |
222 | qemu_co_queue_wait(&s->free_sema, &s->send_mutex); | |
223 | } | |
224 | s->in_flight++; | |
225 | ||
226 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
227 | if (s->requests[i].coroutine == NULL) { | |
228 | break; | |
229 | } | |
230 | } | |
231 | ||
232 | g_assert(qemu_in_coroutine()); | |
233 | assert(i < MAX_NBD_REQUESTS); | |
234 | ||
235 | s->requests[i].coroutine = qemu_coroutine_self(); | |
236 | s->requests[i].offset = request->from; | |
237 | s->requests[i].receiving = false; | |
238 | ||
239 | request->handle = INDEX_TO_HANDLE(s, i); | |
240 | ||
241 | if (s->quit) { | |
242 | rc = -EIO; | |
243 | goto err; | |
244 | } | |
245 | assert(s->ioc); | |
246 | ||
247 | if (qiov) { | |
248 | qio_channel_set_cork(s->ioc, true); | |
249 | rc = nbd_send_request(s->ioc, request); | |
250 | if (rc >= 0 && !s->quit) { | |
251 | if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, | |
252 | NULL) < 0) { | |
253 | rc = -EIO; | |
254 | } | |
255 | } else if (rc >= 0) { | |
256 | rc = -EIO; | |
257 | } | |
258 | qio_channel_set_cork(s->ioc, false); | |
259 | } else { | |
260 | rc = nbd_send_request(s->ioc, request); | |
261 | } | |
262 | ||
263 | err: | |
264 | if (rc < 0) { | |
265 | s->quit = true; | |
266 | s->requests[i].coroutine = NULL; | |
267 | s->in_flight--; | |
268 | qemu_co_queue_next(&s->free_sema); | |
269 | } | |
270 | qemu_co_mutex_unlock(&s->send_mutex); | |
271 | return rc; | |
272 | } | |
273 | ||
274 | static inline uint16_t payload_advance16(uint8_t **payload) | |
275 | { | |
276 | *payload += 2; | |
277 | return lduw_be_p(*payload - 2); | |
278 | } | |
279 | ||
280 | static inline uint32_t payload_advance32(uint8_t **payload) | |
281 | { | |
282 | *payload += 4; | |
283 | return ldl_be_p(*payload - 4); | |
284 | } | |
285 | ||
286 | static inline uint64_t payload_advance64(uint8_t **payload) | |
287 | { | |
288 | *payload += 8; | |
289 | return ldq_be_p(*payload - 8); | |
290 | } | |
291 | ||
611ae1d7 | 292 | static int nbd_parse_offset_hole_payload(BDRVNBDState *s, |
86f8cdf3 VSO |
293 | NBDStructuredReplyChunk *chunk, |
294 | uint8_t *payload, uint64_t orig_offset, | |
295 | QEMUIOVector *qiov, Error **errp) | |
296 | { | |
297 | uint64_t offset; | |
298 | uint32_t hole_size; | |
299 | ||
300 | if (chunk->length != sizeof(offset) + sizeof(hole_size)) { | |
301 | error_setg(errp, "Protocol error: invalid payload for " | |
302 | "NBD_REPLY_TYPE_OFFSET_HOLE"); | |
303 | return -EINVAL; | |
304 | } | |
305 | ||
306 | offset = payload_advance64(&payload); | |
307 | hole_size = payload_advance32(&payload); | |
308 | ||
309 | if (!hole_size || offset < orig_offset || hole_size > qiov->size || | |
310 | offset > orig_offset + qiov->size - hole_size) { | |
311 | error_setg(errp, "Protocol error: server sent chunk exceeding requested" | |
312 | " region"); | |
313 | return -EINVAL; | |
314 | } | |
611ae1d7 VSO |
315 | if (s->info.min_block && |
316 | !QEMU_IS_ALIGNED(hole_size, s->info.min_block)) { | |
86f8cdf3 VSO |
317 | trace_nbd_structured_read_compliance("hole"); |
318 | } | |
319 | ||
320 | qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); | |
321 | ||
322 | return 0; | |
323 | } | |
324 | ||
325 | /* | |
326 | * nbd_parse_blockstatus_payload | |
327 | * Based on our request, we expect only one extent in reply, for the | |
328 | * base:allocation context. | |
329 | */ | |
611ae1d7 | 330 | static int nbd_parse_blockstatus_payload(BDRVNBDState *s, |
86f8cdf3 VSO |
331 | NBDStructuredReplyChunk *chunk, |
332 | uint8_t *payload, uint64_t orig_length, | |
333 | NBDExtent *extent, Error **errp) | |
334 | { | |
335 | uint32_t context_id; | |
336 | ||
337 | /* The server succeeded, so it must have sent [at least] one extent */ | |
338 | if (chunk->length < sizeof(context_id) + sizeof(*extent)) { | |
339 | error_setg(errp, "Protocol error: invalid payload for " | |
340 | "NBD_REPLY_TYPE_BLOCK_STATUS"); | |
341 | return -EINVAL; | |
342 | } | |
343 | ||
344 | context_id = payload_advance32(&payload); | |
611ae1d7 | 345 | if (s->info.context_id != context_id) { |
86f8cdf3 VSO |
346 | error_setg(errp, "Protocol error: unexpected context id %d for " |
347 | "NBD_REPLY_TYPE_BLOCK_STATUS, when negotiated context " | |
348 | "id is %d", context_id, | |
611ae1d7 | 349 | s->info.context_id); |
86f8cdf3 VSO |
350 | return -EINVAL; |
351 | } | |
352 | ||
353 | extent->length = payload_advance32(&payload); | |
354 | extent->flags = payload_advance32(&payload); | |
355 | ||
356 | if (extent->length == 0) { | |
357 | error_setg(errp, "Protocol error: server sent status chunk with " | |
358 | "zero length"); | |
359 | return -EINVAL; | |
360 | } | |
361 | ||
362 | /* | |
363 | * A server sending unaligned block status is in violation of the | |
364 | * protocol, but as qemu-nbd 3.1 is such a server (at least for | |
365 | * POSIX files that are not a multiple of 512 bytes, since qemu | |
366 | * rounds files up to 512-byte multiples but lseek(SEEK_HOLE) | |
367 | * still sees an implicit hole beyond the real EOF), it's nicer to | |
368 | * work around the misbehaving server. If the request included | |
369 | * more than the final unaligned block, truncate it back to an | |
370 | * aligned result; if the request was only the final block, round | |
371 | * up to the full block and change the status to fully-allocated | |
372 | * (always a safe status, even if it loses information). | |
373 | */ | |
611ae1d7 VSO |
374 | if (s->info.min_block && !QEMU_IS_ALIGNED(extent->length, |
375 | s->info.min_block)) { | |
86f8cdf3 | 376 | trace_nbd_parse_blockstatus_compliance("extent length is unaligned"); |
611ae1d7 | 377 | if (extent->length > s->info.min_block) { |
86f8cdf3 | 378 | extent->length = QEMU_ALIGN_DOWN(extent->length, |
611ae1d7 | 379 | s->info.min_block); |
86f8cdf3 | 380 | } else { |
611ae1d7 | 381 | extent->length = s->info.min_block; |
86f8cdf3 VSO |
382 | extent->flags = 0; |
383 | } | |
384 | } | |
385 | ||
386 | /* | |
387 | * We used NBD_CMD_FLAG_REQ_ONE, so the server should not have | |
388 | * sent us any more than one extent, nor should it have included | |
389 | * status beyond our request in that extent. However, it's easy | |
390 | * enough to ignore the server's noncompliance without killing the | |
391 | * connection; just ignore trailing extents, and clamp things to | |
392 | * the length of our request. | |
393 | */ | |
394 | if (chunk->length > sizeof(context_id) + sizeof(*extent)) { | |
395 | trace_nbd_parse_blockstatus_compliance("more than one extent"); | |
396 | } | |
397 | if (extent->length > orig_length) { | |
398 | extent->length = orig_length; | |
399 | trace_nbd_parse_blockstatus_compliance("extent length too large"); | |
400 | } | |
401 | ||
402 | return 0; | |
403 | } | |
404 | ||
405 | /* | |
406 | * nbd_parse_error_payload | |
407 | * on success @errp contains message describing nbd error reply | |
408 | */ | |
409 | static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, | |
410 | uint8_t *payload, int *request_ret, | |
411 | Error **errp) | |
412 | { | |
413 | uint32_t error; | |
414 | uint16_t message_size; | |
415 | ||
416 | assert(chunk->type & (1 << 15)); | |
417 | ||
418 | if (chunk->length < sizeof(error) + sizeof(message_size)) { | |
419 | error_setg(errp, | |
420 | "Protocol error: invalid payload for structured error"); | |
421 | return -EINVAL; | |
422 | } | |
423 | ||
424 | error = nbd_errno_to_system_errno(payload_advance32(&payload)); | |
425 | if (error == 0) { | |
426 | error_setg(errp, "Protocol error: server sent structured error chunk " | |
427 | "with error = 0"); | |
428 | return -EINVAL; | |
429 | } | |
430 | ||
431 | *request_ret = -error; | |
432 | message_size = payload_advance16(&payload); | |
433 | ||
434 | if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { | |
435 | error_setg(errp, "Protocol error: server sent structured error chunk " | |
436 | "with incorrect message size"); | |
437 | return -EINVAL; | |
438 | } | |
439 | ||
440 | /* TODO: Add a trace point to mention the server complaint */ | |
441 | ||
442 | /* TODO handle ERROR_OFFSET */ | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
611ae1d7 | 447 | static int nbd_co_receive_offset_data_payload(BDRVNBDState *s, |
86f8cdf3 VSO |
448 | uint64_t orig_offset, |
449 | QEMUIOVector *qiov, Error **errp) | |
450 | { | |
451 | QEMUIOVector sub_qiov; | |
452 | uint64_t offset; | |
453 | size_t data_size; | |
454 | int ret; | |
455 | NBDStructuredReplyChunk *chunk = &s->reply.structured; | |
456 | ||
457 | assert(nbd_reply_is_structured(&s->reply)); | |
458 | ||
459 | /* The NBD spec requires at least one byte of payload */ | |
460 | if (chunk->length <= sizeof(offset)) { | |
461 | error_setg(errp, "Protocol error: invalid payload for " | |
462 | "NBD_REPLY_TYPE_OFFSET_DATA"); | |
463 | return -EINVAL; | |
464 | } | |
465 | ||
466 | if (nbd_read64(s->ioc, &offset, "OFFSET_DATA offset", errp) < 0) { | |
467 | return -EIO; | |
468 | } | |
469 | ||
470 | data_size = chunk->length - sizeof(offset); | |
471 | assert(data_size); | |
472 | if (offset < orig_offset || data_size > qiov->size || | |
473 | offset > orig_offset + qiov->size - data_size) { | |
474 | error_setg(errp, "Protocol error: server sent chunk exceeding requested" | |
475 | " region"); | |
476 | return -EINVAL; | |
477 | } | |
478 | if (s->info.min_block && !QEMU_IS_ALIGNED(data_size, s->info.min_block)) { | |
479 | trace_nbd_structured_read_compliance("data"); | |
480 | } | |
481 | ||
482 | qemu_iovec_init(&sub_qiov, qiov->niov); | |
483 | qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); | |
484 | ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); | |
485 | qemu_iovec_destroy(&sub_qiov); | |
486 | ||
487 | return ret < 0 ? -EIO : 0; | |
488 | } | |
489 | ||
490 | #define NBD_MAX_MALLOC_PAYLOAD 1000 | |
491 | static coroutine_fn int nbd_co_receive_structured_payload( | |
611ae1d7 | 492 | BDRVNBDState *s, void **payload, Error **errp) |
86f8cdf3 VSO |
493 | { |
494 | int ret; | |
495 | uint32_t len; | |
496 | ||
497 | assert(nbd_reply_is_structured(&s->reply)); | |
498 | ||
499 | len = s->reply.structured.length; | |
500 | ||
501 | if (len == 0) { | |
502 | return 0; | |
503 | } | |
504 | ||
505 | if (payload == NULL) { | |
506 | error_setg(errp, "Unexpected structured payload"); | |
507 | return -EINVAL; | |
508 | } | |
509 | ||
510 | if (len > NBD_MAX_MALLOC_PAYLOAD) { | |
511 | error_setg(errp, "Payload too large"); | |
512 | return -EINVAL; | |
513 | } | |
514 | ||
515 | *payload = g_new(char, len); | |
516 | ret = nbd_read(s->ioc, *payload, len, "structured payload", errp); | |
517 | if (ret < 0) { | |
518 | g_free(*payload); | |
519 | *payload = NULL; | |
520 | return ret; | |
521 | } | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
526 | /* | |
527 | * nbd_co_do_receive_one_chunk | |
528 | * for simple reply: | |
529 | * set request_ret to received reply error | |
530 | * if qiov is not NULL: read payload to @qiov | |
531 | * for structured reply chunk: | |
532 | * if error chunk: read payload, set @request_ret, do not set @payload | |
533 | * else if offset_data chunk: read payload data to @qiov, do not set @payload | |
534 | * else: read payload to @payload | |
535 | * | |
536 | * If function fails, @errp contains corresponding error message, and the | |
537 | * connection with the server is suspect. If it returns 0, then the | |
538 | * transaction succeeded (although @request_ret may be a negative errno | |
539 | * corresponding to the server's error reply), and errp is unchanged. | |
540 | */ | |
541 | static coroutine_fn int nbd_co_do_receive_one_chunk( | |
611ae1d7 | 542 | BDRVNBDState *s, uint64_t handle, bool only_structured, |
86f8cdf3 VSO |
543 | int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) |
544 | { | |
545 | int ret; | |
546 | int i = HANDLE_TO_INDEX(s, handle); | |
547 | void *local_payload = NULL; | |
548 | NBDStructuredReplyChunk *chunk; | |
549 | ||
550 | if (payload) { | |
551 | *payload = NULL; | |
552 | } | |
553 | *request_ret = 0; | |
554 | ||
555 | /* Wait until we're woken up by nbd_connection_entry. */ | |
556 | s->requests[i].receiving = true; | |
557 | qemu_coroutine_yield(); | |
558 | s->requests[i].receiving = false; | |
559 | if (s->quit) { | |
560 | error_setg(errp, "Connection closed"); | |
561 | return -EIO; | |
562 | } | |
563 | assert(s->ioc); | |
564 | ||
565 | assert(s->reply.handle == handle); | |
566 | ||
567 | if (nbd_reply_is_simple(&s->reply)) { | |
568 | if (only_structured) { | |
569 | error_setg(errp, "Protocol error: simple reply when structured " | |
570 | "reply chunk was expected"); | |
571 | return -EINVAL; | |
572 | } | |
573 | ||
574 | *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); | |
575 | if (*request_ret < 0 || !qiov) { | |
576 | return 0; | |
577 | } | |
578 | ||
579 | return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, | |
580 | errp) < 0 ? -EIO : 0; | |
581 | } | |
582 | ||
583 | /* handle structured reply chunk */ | |
584 | assert(s->info.structured_reply); | |
585 | chunk = &s->reply.structured; | |
586 | ||
587 | if (chunk->type == NBD_REPLY_TYPE_NONE) { | |
588 | if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { | |
589 | error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" | |
590 | " NBD_REPLY_FLAG_DONE flag set"); | |
591 | return -EINVAL; | |
592 | } | |
593 | if (chunk->length) { | |
594 | error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk with" | |
595 | " nonzero length"); | |
596 | return -EINVAL; | |
597 | } | |
598 | return 0; | |
599 | } | |
600 | ||
601 | if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { | |
602 | if (!qiov) { | |
603 | error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); | |
604 | return -EINVAL; | |
605 | } | |
606 | ||
607 | return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, | |
608 | qiov, errp); | |
609 | } | |
610 | ||
611 | if (nbd_reply_type_is_error(chunk->type)) { | |
612 | payload = &local_payload; | |
613 | } | |
614 | ||
615 | ret = nbd_co_receive_structured_payload(s, payload, errp); | |
616 | if (ret < 0) { | |
617 | return ret; | |
618 | } | |
619 | ||
620 | if (nbd_reply_type_is_error(chunk->type)) { | |
621 | ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); | |
622 | g_free(local_payload); | |
623 | return ret; | |
624 | } | |
625 | ||
626 | return 0; | |
627 | } | |
628 | ||
629 | /* | |
630 | * nbd_co_receive_one_chunk | |
631 | * Read reply, wake up connection_co and set s->quit if needed. | |
632 | * Return value is a fatal error code or normal nbd reply error code | |
633 | */ | |
634 | static coroutine_fn int nbd_co_receive_one_chunk( | |
611ae1d7 | 635 | BDRVNBDState *s, uint64_t handle, bool only_structured, |
86f8cdf3 VSO |
636 | int *request_ret, QEMUIOVector *qiov, NBDReply *reply, void **payload, |
637 | Error **errp) | |
638 | { | |
639 | int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured, | |
640 | request_ret, qiov, payload, errp); | |
641 | ||
642 | if (ret < 0) { | |
5cf42b1c | 643 | memset(reply, 0, sizeof(*reply)); |
86f8cdf3 VSO |
644 | s->quit = true; |
645 | } else { | |
646 | /* For assert at loop start in nbd_connection_entry */ | |
5cf42b1c | 647 | *reply = s->reply; |
86f8cdf3 VSO |
648 | s->reply.handle = 0; |
649 | } | |
650 | ||
651 | if (s->connection_co) { | |
652 | aio_co_wake(s->connection_co); | |
653 | } | |
654 | ||
655 | return ret; | |
656 | } | |
657 | ||
658 | typedef struct NBDReplyChunkIter { | |
659 | int ret; | |
660 | int request_ret; | |
661 | Error *err; | |
662 | bool done, only_structured; | |
663 | } NBDReplyChunkIter; | |
664 | ||
665 | static void nbd_iter_channel_error(NBDReplyChunkIter *iter, | |
666 | int ret, Error **local_err) | |
667 | { | |
668 | assert(ret < 0); | |
669 | ||
670 | if (!iter->ret) { | |
671 | iter->ret = ret; | |
672 | error_propagate(&iter->err, *local_err); | |
673 | } else { | |
674 | error_free(*local_err); | |
675 | } | |
676 | ||
677 | *local_err = NULL; | |
678 | } | |
679 | ||
680 | static void nbd_iter_request_error(NBDReplyChunkIter *iter, int ret) | |
681 | { | |
682 | assert(ret < 0); | |
683 | ||
684 | if (!iter->request_ret) { | |
685 | iter->request_ret = ret; | |
686 | } | |
687 | } | |
688 | ||
689 | /* | |
690 | * NBD_FOREACH_REPLY_CHUNK | |
691 | * The pointer stored in @payload requires g_free() to free it. | |
692 | */ | |
693 | #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ | |
694 | qiov, reply, payload) \ | |
695 | for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ | |
696 | nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) | |
697 | ||
698 | /* | |
699 | * nbd_reply_chunk_iter_receive | |
700 | * The pointer stored in @payload requires g_free() to free it. | |
701 | */ | |
611ae1d7 | 702 | static bool nbd_reply_chunk_iter_receive(BDRVNBDState *s, |
86f8cdf3 VSO |
703 | NBDReplyChunkIter *iter, |
704 | uint64_t handle, | |
705 | QEMUIOVector *qiov, NBDReply *reply, | |
706 | void **payload) | |
707 | { | |
708 | int ret, request_ret; | |
709 | NBDReply local_reply; | |
710 | NBDStructuredReplyChunk *chunk; | |
711 | Error *local_err = NULL; | |
712 | if (s->quit) { | |
713 | error_setg(&local_err, "Connection closed"); | |
714 | nbd_iter_channel_error(iter, -EIO, &local_err); | |
715 | goto break_loop; | |
716 | } | |
717 | ||
718 | if (iter->done) { | |
719 | /* Previous iteration was last. */ | |
720 | goto break_loop; | |
721 | } | |
722 | ||
723 | if (reply == NULL) { | |
724 | reply = &local_reply; | |
725 | } | |
726 | ||
727 | ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured, | |
728 | &request_ret, qiov, reply, payload, | |
729 | &local_err); | |
730 | if (ret < 0) { | |
731 | nbd_iter_channel_error(iter, ret, &local_err); | |
732 | } else if (request_ret < 0) { | |
733 | nbd_iter_request_error(iter, request_ret); | |
734 | } | |
735 | ||
736 | /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ | |
737 | if (nbd_reply_is_simple(reply) || s->quit) { | |
738 | goto break_loop; | |
739 | } | |
740 | ||
741 | chunk = &reply->structured; | |
742 | iter->only_structured = true; | |
743 | ||
744 | if (chunk->type == NBD_REPLY_TYPE_NONE) { | |
745 | /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ | |
746 | assert(chunk->flags & NBD_REPLY_FLAG_DONE); | |
747 | goto break_loop; | |
748 | } | |
749 | ||
750 | if (chunk->flags & NBD_REPLY_FLAG_DONE) { | |
751 | /* This iteration is last. */ | |
752 | iter->done = true; | |
753 | } | |
754 | ||
755 | /* Execute the loop body */ | |
756 | return true; | |
757 | ||
758 | break_loop: | |
759 | s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL; | |
760 | ||
761 | qemu_co_mutex_lock(&s->send_mutex); | |
762 | s->in_flight--; | |
763 | qemu_co_queue_next(&s->free_sema); | |
764 | qemu_co_mutex_unlock(&s->send_mutex); | |
765 | ||
766 | return false; | |
767 | } | |
768 | ||
611ae1d7 | 769 | static int nbd_co_receive_return_code(BDRVNBDState *s, uint64_t handle, |
86f8cdf3 VSO |
770 | int *request_ret, Error **errp) |
771 | { | |
772 | NBDReplyChunkIter iter; | |
773 | ||
774 | NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { | |
775 | /* nbd_reply_chunk_iter_receive does all the work */ | |
776 | } | |
777 | ||
778 | error_propagate(errp, iter.err); | |
779 | *request_ret = iter.request_ret; | |
780 | return iter.ret; | |
781 | } | |
782 | ||
611ae1d7 | 783 | static int nbd_co_receive_cmdread_reply(BDRVNBDState *s, uint64_t handle, |
86f8cdf3 VSO |
784 | uint64_t offset, QEMUIOVector *qiov, |
785 | int *request_ret, Error **errp) | |
786 | { | |
787 | NBDReplyChunkIter iter; | |
788 | NBDReply reply; | |
789 | void *payload = NULL; | |
790 | Error *local_err = NULL; | |
791 | ||
792 | NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, | |
793 | qiov, &reply, &payload) | |
794 | { | |
795 | int ret; | |
796 | NBDStructuredReplyChunk *chunk = &reply.structured; | |
797 | ||
798 | assert(nbd_reply_is_structured(&reply)); | |
799 | ||
800 | switch (chunk->type) { | |
801 | case NBD_REPLY_TYPE_OFFSET_DATA: | |
802 | /* | |
803 | * special cased in nbd_co_receive_one_chunk, data is already | |
804 | * in qiov | |
805 | */ | |
806 | break; | |
807 | case NBD_REPLY_TYPE_OFFSET_HOLE: | |
808 | ret = nbd_parse_offset_hole_payload(s, &reply.structured, payload, | |
809 | offset, qiov, &local_err); | |
810 | if (ret < 0) { | |
811 | s->quit = true; | |
812 | nbd_iter_channel_error(&iter, ret, &local_err); | |
813 | } | |
814 | break; | |
815 | default: | |
816 | if (!nbd_reply_type_is_error(chunk->type)) { | |
817 | /* not allowed reply type */ | |
818 | s->quit = true; | |
819 | error_setg(&local_err, | |
820 | "Unexpected reply type: %d (%s) for CMD_READ", | |
821 | chunk->type, nbd_reply_type_lookup(chunk->type)); | |
822 | nbd_iter_channel_error(&iter, -EINVAL, &local_err); | |
823 | } | |
824 | } | |
825 | ||
826 | g_free(payload); | |
827 | payload = NULL; | |
828 | } | |
829 | ||
830 | error_propagate(errp, iter.err); | |
831 | *request_ret = iter.request_ret; | |
832 | return iter.ret; | |
833 | } | |
834 | ||
611ae1d7 | 835 | static int nbd_co_receive_blockstatus_reply(BDRVNBDState *s, |
86f8cdf3 VSO |
836 | uint64_t handle, uint64_t length, |
837 | NBDExtent *extent, | |
838 | int *request_ret, Error **errp) | |
839 | { | |
840 | NBDReplyChunkIter iter; | |
841 | NBDReply reply; | |
842 | void *payload = NULL; | |
843 | Error *local_err = NULL; | |
844 | bool received = false; | |
845 | ||
846 | assert(!extent->length); | |
847 | NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, &reply, &payload) { | |
848 | int ret; | |
849 | NBDStructuredReplyChunk *chunk = &reply.structured; | |
850 | ||
851 | assert(nbd_reply_is_structured(&reply)); | |
852 | ||
853 | switch (chunk->type) { | |
854 | case NBD_REPLY_TYPE_BLOCK_STATUS: | |
855 | if (received) { | |
856 | s->quit = true; | |
857 | error_setg(&local_err, "Several BLOCK_STATUS chunks in reply"); | |
858 | nbd_iter_channel_error(&iter, -EINVAL, &local_err); | |
859 | } | |
860 | received = true; | |
861 | ||
862 | ret = nbd_parse_blockstatus_payload(s, &reply.structured, | |
863 | payload, length, extent, | |
864 | &local_err); | |
865 | if (ret < 0) { | |
866 | s->quit = true; | |
867 | nbd_iter_channel_error(&iter, ret, &local_err); | |
868 | } | |
869 | break; | |
870 | default: | |
871 | if (!nbd_reply_type_is_error(chunk->type)) { | |
872 | s->quit = true; | |
873 | error_setg(&local_err, | |
874 | "Unexpected reply type: %d (%s) " | |
875 | "for CMD_BLOCK_STATUS", | |
876 | chunk->type, nbd_reply_type_lookup(chunk->type)); | |
877 | nbd_iter_channel_error(&iter, -EINVAL, &local_err); | |
878 | } | |
879 | } | |
880 | ||
881 | g_free(payload); | |
882 | payload = NULL; | |
883 | } | |
884 | ||
885 | if (!extent->length && !iter.request_ret) { | |
886 | error_setg(&local_err, "Server did not reply with any status extents"); | |
887 | nbd_iter_channel_error(&iter, -EIO, &local_err); | |
888 | } | |
889 | ||
890 | error_propagate(errp, iter.err); | |
891 | *request_ret = iter.request_ret; | |
892 | return iter.ret; | |
893 | } | |
894 | ||
895 | static int nbd_co_request(BlockDriverState *bs, NBDRequest *request, | |
896 | QEMUIOVector *write_qiov) | |
897 | { | |
898 | int ret, request_ret; | |
899 | Error *local_err = NULL; | |
611ae1d7 | 900 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
901 | |
902 | assert(request->type != NBD_CMD_READ); | |
903 | if (write_qiov) { | |
904 | assert(request->type == NBD_CMD_WRITE); | |
905 | assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); | |
906 | } else { | |
907 | assert(request->type != NBD_CMD_WRITE); | |
908 | } | |
909 | ret = nbd_co_send_request(bs, request, write_qiov); | |
910 | if (ret < 0) { | |
911 | return ret; | |
912 | } | |
913 | ||
611ae1d7 | 914 | ret = nbd_co_receive_return_code(s, request->handle, |
86f8cdf3 VSO |
915 | &request_ret, &local_err); |
916 | if (local_err) { | |
917 | trace_nbd_co_request_fail(request->from, request->len, request->handle, | |
918 | request->flags, request->type, | |
919 | nbd_cmd_lookup(request->type), | |
920 | ret, error_get_pretty(local_err)); | |
921 | error_free(local_err); | |
922 | } | |
923 | return ret ? ret : request_ret; | |
924 | } | |
925 | ||
926 | static int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, | |
927 | uint64_t bytes, QEMUIOVector *qiov, int flags) | |
928 | { | |
929 | int ret, request_ret; | |
930 | Error *local_err = NULL; | |
611ae1d7 | 931 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
932 | NBDRequest request = { |
933 | .type = NBD_CMD_READ, | |
934 | .from = offset, | |
935 | .len = bytes, | |
936 | }; | |
937 | ||
938 | assert(bytes <= NBD_MAX_BUFFER_SIZE); | |
939 | assert(!flags); | |
940 | ||
941 | if (!bytes) { | |
942 | return 0; | |
943 | } | |
944 | /* | |
945 | * Work around the fact that the block layer doesn't do | |
946 | * byte-accurate sizing yet - if the read exceeds the server's | |
947 | * advertised size because the block layer rounded size up, then | |
948 | * truncate the request to the server and tail-pad with zero. | |
949 | */ | |
611ae1d7 | 950 | if (offset >= s->info.size) { |
86f8cdf3 VSO |
951 | assert(bytes < BDRV_SECTOR_SIZE); |
952 | qemu_iovec_memset(qiov, 0, 0, bytes); | |
953 | return 0; | |
954 | } | |
611ae1d7 VSO |
955 | if (offset + bytes > s->info.size) { |
956 | uint64_t slop = offset + bytes - s->info.size; | |
86f8cdf3 VSO |
957 | |
958 | assert(slop < BDRV_SECTOR_SIZE); | |
959 | qemu_iovec_memset(qiov, bytes - slop, 0, slop); | |
960 | request.len -= slop; | |
961 | } | |
962 | ||
963 | ret = nbd_co_send_request(bs, &request, NULL); | |
964 | if (ret < 0) { | |
965 | return ret; | |
966 | } | |
967 | ||
611ae1d7 | 968 | ret = nbd_co_receive_cmdread_reply(s, request.handle, offset, qiov, |
86f8cdf3 VSO |
969 | &request_ret, &local_err); |
970 | if (local_err) { | |
971 | trace_nbd_co_request_fail(request.from, request.len, request.handle, | |
972 | request.flags, request.type, | |
973 | nbd_cmd_lookup(request.type), | |
974 | ret, error_get_pretty(local_err)); | |
975 | error_free(local_err); | |
976 | } | |
977 | return ret ? ret : request_ret; | |
978 | } | |
979 | ||
980 | static int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset, | |
981 | uint64_t bytes, QEMUIOVector *qiov, int flags) | |
982 | { | |
611ae1d7 | 983 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
984 | NBDRequest request = { |
985 | .type = NBD_CMD_WRITE, | |
986 | .from = offset, | |
987 | .len = bytes, | |
988 | }; | |
989 | ||
611ae1d7 | 990 | assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); |
86f8cdf3 | 991 | if (flags & BDRV_REQ_FUA) { |
611ae1d7 | 992 | assert(s->info.flags & NBD_FLAG_SEND_FUA); |
86f8cdf3 VSO |
993 | request.flags |= NBD_CMD_FLAG_FUA; |
994 | } | |
995 | ||
996 | assert(bytes <= NBD_MAX_BUFFER_SIZE); | |
997 | ||
998 | if (!bytes) { | |
999 | return 0; | |
1000 | } | |
1001 | return nbd_co_request(bs, &request, qiov); | |
1002 | } | |
1003 | ||
1004 | static int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, | |
1005 | int bytes, BdrvRequestFlags flags) | |
1006 | { | |
611ae1d7 | 1007 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
1008 | NBDRequest request = { |
1009 | .type = NBD_CMD_WRITE_ZEROES, | |
1010 | .from = offset, | |
1011 | .len = bytes, | |
1012 | }; | |
1013 | ||
611ae1d7 VSO |
1014 | assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); |
1015 | if (!(s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { | |
86f8cdf3 VSO |
1016 | return -ENOTSUP; |
1017 | } | |
1018 | ||
1019 | if (flags & BDRV_REQ_FUA) { | |
611ae1d7 | 1020 | assert(s->info.flags & NBD_FLAG_SEND_FUA); |
86f8cdf3 VSO |
1021 | request.flags |= NBD_CMD_FLAG_FUA; |
1022 | } | |
1023 | if (!(flags & BDRV_REQ_MAY_UNMAP)) { | |
1024 | request.flags |= NBD_CMD_FLAG_NO_HOLE; | |
1025 | } | |
1026 | ||
1027 | if (!bytes) { | |
1028 | return 0; | |
1029 | } | |
1030 | return nbd_co_request(bs, &request, NULL); | |
1031 | } | |
1032 | ||
1033 | static int nbd_client_co_flush(BlockDriverState *bs) | |
1034 | { | |
611ae1d7 | 1035 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
1036 | NBDRequest request = { .type = NBD_CMD_FLUSH }; |
1037 | ||
611ae1d7 | 1038 | if (!(s->info.flags & NBD_FLAG_SEND_FLUSH)) { |
86f8cdf3 VSO |
1039 | return 0; |
1040 | } | |
1041 | ||
1042 | request.from = 0; | |
1043 | request.len = 0; | |
1044 | ||
1045 | return nbd_co_request(bs, &request, NULL); | |
1046 | } | |
1047 | ||
1048 | static int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, | |
1049 | int bytes) | |
1050 | { | |
611ae1d7 | 1051 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
1052 | NBDRequest request = { |
1053 | .type = NBD_CMD_TRIM, | |
1054 | .from = offset, | |
1055 | .len = bytes, | |
1056 | }; | |
1057 | ||
611ae1d7 VSO |
1058 | assert(!(s->info.flags & NBD_FLAG_READ_ONLY)); |
1059 | if (!(s->info.flags & NBD_FLAG_SEND_TRIM) || !bytes) { | |
86f8cdf3 VSO |
1060 | return 0; |
1061 | } | |
1062 | ||
1063 | return nbd_co_request(bs, &request, NULL); | |
1064 | } | |
1065 | ||
1066 | static int coroutine_fn nbd_client_co_block_status( | |
1067 | BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes, | |
1068 | int64_t *pnum, int64_t *map, BlockDriverState **file) | |
1069 | { | |
1070 | int ret, request_ret; | |
1071 | NBDExtent extent = { 0 }; | |
611ae1d7 | 1072 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
1073 | Error *local_err = NULL; |
1074 | ||
1075 | NBDRequest request = { | |
1076 | .type = NBD_CMD_BLOCK_STATUS, | |
1077 | .from = offset, | |
1078 | .len = MIN(MIN_NON_ZERO(QEMU_ALIGN_DOWN(INT_MAX, | |
1079 | bs->bl.request_alignment), | |
611ae1d7 VSO |
1080 | s->info.max_block), |
1081 | MIN(bytes, s->info.size - offset)), | |
86f8cdf3 VSO |
1082 | .flags = NBD_CMD_FLAG_REQ_ONE, |
1083 | }; | |
1084 | ||
611ae1d7 | 1085 | if (!s->info.base_allocation) { |
86f8cdf3 VSO |
1086 | *pnum = bytes; |
1087 | *map = offset; | |
1088 | *file = bs; | |
1089 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Work around the fact that the block layer doesn't do | |
1094 | * byte-accurate sizing yet - if the status request exceeds the | |
1095 | * server's advertised size because the block layer rounded size | |
1096 | * up, we truncated the request to the server (above), or are | |
1097 | * called on just the hole. | |
1098 | */ | |
611ae1d7 | 1099 | if (offset >= s->info.size) { |
86f8cdf3 VSO |
1100 | *pnum = bytes; |
1101 | assert(bytes < BDRV_SECTOR_SIZE); | |
1102 | /* Intentionally don't report offset_valid for the hole */ | |
1103 | return BDRV_BLOCK_ZERO; | |
1104 | } | |
1105 | ||
611ae1d7 VSO |
1106 | if (s->info.min_block) { |
1107 | assert(QEMU_IS_ALIGNED(request.len, s->info.min_block)); | |
86f8cdf3 VSO |
1108 | } |
1109 | ret = nbd_co_send_request(bs, &request, NULL); | |
1110 | if (ret < 0) { | |
1111 | return ret; | |
1112 | } | |
1113 | ||
611ae1d7 | 1114 | ret = nbd_co_receive_blockstatus_reply(s, request.handle, bytes, |
86f8cdf3 VSO |
1115 | &extent, &request_ret, &local_err); |
1116 | if (local_err) { | |
1117 | trace_nbd_co_request_fail(request.from, request.len, request.handle, | |
1118 | request.flags, request.type, | |
1119 | nbd_cmd_lookup(request.type), | |
1120 | ret, error_get_pretty(local_err)); | |
1121 | error_free(local_err); | |
1122 | } | |
1123 | if (ret < 0 || request_ret < 0) { | |
1124 | return ret ? ret : request_ret; | |
1125 | } | |
1126 | ||
1127 | assert(extent.length); | |
1128 | *pnum = extent.length; | |
1129 | *map = offset; | |
1130 | *file = bs; | |
1131 | return (extent.flags & NBD_STATE_HOLE ? 0 : BDRV_BLOCK_DATA) | | |
1132 | (extent.flags & NBD_STATE_ZERO ? BDRV_BLOCK_ZERO : 0) | | |
1133 | BDRV_BLOCK_OFFSET_VALID; | |
1134 | } | |
1135 | ||
1136 | static void nbd_client_close(BlockDriverState *bs) | |
1137 | { | |
611ae1d7 | 1138 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 VSO |
1139 | NBDRequest request = { .type = NBD_CMD_DISC }; |
1140 | ||
611ae1d7 | 1141 | assert(s->ioc); |
86f8cdf3 | 1142 | |
611ae1d7 | 1143 | nbd_send_request(s->ioc, &request); |
86f8cdf3 VSO |
1144 | |
1145 | nbd_teardown_connection(bs); | |
1146 | } | |
1147 | ||
1148 | static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr, | |
1149 | Error **errp) | |
1150 | { | |
1151 | QIOChannelSocket *sioc; | |
1152 | Error *local_err = NULL; | |
1153 | ||
1154 | sioc = qio_channel_socket_new(); | |
1155 | qio_channel_set_name(QIO_CHANNEL(sioc), "nbd-client"); | |
1156 | ||
1157 | qio_channel_socket_connect_sync(sioc, saddr, &local_err); | |
1158 | if (local_err) { | |
1159 | object_unref(OBJECT(sioc)); | |
1160 | error_propagate(errp, local_err); | |
1161 | return NULL; | |
1162 | } | |
1163 | ||
1164 | qio_channel_set_delay(QIO_CHANNEL(sioc), false); | |
1165 | ||
1166 | return sioc; | |
1167 | } | |
1168 | ||
1169 | static int nbd_client_connect(BlockDriverState *bs, | |
1170 | SocketAddress *saddr, | |
1171 | const char *export, | |
1172 | QCryptoTLSCreds *tlscreds, | |
1173 | const char *hostname, | |
1174 | const char *x_dirty_bitmap, | |
1175 | Error **errp) | |
1176 | { | |
611ae1d7 | 1177 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
a8e2bb6a | 1178 | AioContext *aio_context = bdrv_get_aio_context(bs); |
86f8cdf3 VSO |
1179 | int ret; |
1180 | ||
1181 | /* | |
1182 | * establish TCP connection, return error if it fails | |
1183 | * TODO: Configurable retry-until-timeout behaviour. | |
1184 | */ | |
1185 | QIOChannelSocket *sioc = nbd_establish_connection(saddr, errp); | |
1186 | ||
1187 | if (!sioc) { | |
1188 | return -ECONNREFUSED; | |
1189 | } | |
1190 | ||
1191 | /* NBD handshake */ | |
1192 | trace_nbd_client_connect(export); | |
a8e2bb6a VSO |
1193 | qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL); |
1194 | qio_channel_attach_aio_context(QIO_CHANNEL(sioc), aio_context); | |
86f8cdf3 | 1195 | |
611ae1d7 VSO |
1196 | s->info.request_sizes = true; |
1197 | s->info.structured_reply = true; | |
1198 | s->info.base_allocation = true; | |
1199 | s->info.x_dirty_bitmap = g_strdup(x_dirty_bitmap); | |
1200 | s->info.name = g_strdup(export ?: ""); | |
a8e2bb6a VSO |
1201 | ret = nbd_receive_negotiate(aio_context, QIO_CHANNEL(sioc), tlscreds, |
1202 | hostname, &s->ioc, &s->info, errp); | |
611ae1d7 VSO |
1203 | g_free(s->info.x_dirty_bitmap); |
1204 | g_free(s->info.name); | |
86f8cdf3 VSO |
1205 | if (ret < 0) { |
1206 | object_unref(OBJECT(sioc)); | |
1207 | return ret; | |
1208 | } | |
611ae1d7 | 1209 | if (x_dirty_bitmap && !s->info.base_allocation) { |
86f8cdf3 VSO |
1210 | error_setg(errp, "requested x-dirty-bitmap %s not found", |
1211 | x_dirty_bitmap); | |
1212 | ret = -EINVAL; | |
1213 | goto fail; | |
1214 | } | |
611ae1d7 | 1215 | if (s->info.flags & NBD_FLAG_READ_ONLY) { |
86f8cdf3 VSO |
1216 | ret = bdrv_apply_auto_read_only(bs, "NBD export is read-only", errp); |
1217 | if (ret < 0) { | |
1218 | goto fail; | |
1219 | } | |
1220 | } | |
611ae1d7 | 1221 | if (s->info.flags & NBD_FLAG_SEND_FUA) { |
86f8cdf3 VSO |
1222 | bs->supported_write_flags = BDRV_REQ_FUA; |
1223 | bs->supported_zero_flags |= BDRV_REQ_FUA; | |
1224 | } | |
611ae1d7 | 1225 | if (s->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { |
86f8cdf3 VSO |
1226 | bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; |
1227 | } | |
1228 | ||
611ae1d7 | 1229 | s->sioc = sioc; |
86f8cdf3 | 1230 | |
611ae1d7 VSO |
1231 | if (!s->ioc) { |
1232 | s->ioc = QIO_CHANNEL(sioc); | |
1233 | object_ref(OBJECT(s->ioc)); | |
86f8cdf3 VSO |
1234 | } |
1235 | ||
86f8cdf3 VSO |
1236 | trace_nbd_client_connect_success(export); |
1237 | ||
1238 | return 0; | |
1239 | ||
1240 | fail: | |
1241 | /* | |
a8e2bb6a VSO |
1242 | * We have connected, but must fail for other reasons. |
1243 | * Send NBD_CMD_DISC as a courtesy to the server. | |
86f8cdf3 VSO |
1244 | */ |
1245 | { | |
1246 | NBDRequest request = { .type = NBD_CMD_DISC }; | |
1247 | ||
611ae1d7 | 1248 | nbd_send_request(s->ioc ?: QIO_CHANNEL(sioc), &request); |
86f8cdf3 VSO |
1249 | |
1250 | object_unref(OBJECT(sioc)); | |
1251 | ||
1252 | return ret; | |
1253 | } | |
1254 | } | |
1255 | ||
1256 | static int nbd_client_init(BlockDriverState *bs, | |
1257 | SocketAddress *saddr, | |
1258 | const char *export, | |
1259 | QCryptoTLSCreds *tlscreds, | |
1260 | const char *hostname, | |
1261 | const char *x_dirty_bitmap, | |
1262 | Error **errp) | |
1263 | { | |
962b7b3d | 1264 | int ret; |
611ae1d7 | 1265 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
86f8cdf3 | 1266 | |
611ae1d7 VSO |
1267 | s->bs = bs; |
1268 | qemu_co_mutex_init(&s->send_mutex); | |
1269 | qemu_co_queue_init(&s->free_sema); | |
86f8cdf3 | 1270 | |
962b7b3d VSO |
1271 | ret = nbd_client_connect(bs, saddr, export, tlscreds, hostname, |
1272 | x_dirty_bitmap, errp); | |
1273 | if (ret < 0) { | |
1274 | return ret; | |
1275 | } | |
1276 | ||
1277 | s->connection_co = qemu_coroutine_create(nbd_connection_entry, s); | |
1278 | bdrv_inc_in_flight(bs); | |
1279 | aio_co_schedule(bdrv_get_aio_context(bs), s->connection_co); | |
1280 | ||
1281 | return 0; | |
86f8cdf3 VSO |
1282 | } |
1283 | ||
f53a1feb | 1284 | static int nbd_parse_uri(const char *filename, QDict *options) |
1d7d2a9d PB |
1285 | { |
1286 | URI *uri; | |
1287 | const char *p; | |
1288 | QueryParams *qp = NULL; | |
1289 | int ret = 0; | |
f53a1feb | 1290 | bool is_unix; |
1d7d2a9d PB |
1291 | |
1292 | uri = uri_parse(filename); | |
1293 | if (!uri) { | |
1294 | return -EINVAL; | |
1295 | } | |
1296 | ||
1297 | /* transport */ | |
f69165a8 | 1298 | if (!g_strcmp0(uri->scheme, "nbd")) { |
f53a1feb | 1299 | is_unix = false; |
f69165a8 | 1300 | } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { |
f53a1feb | 1301 | is_unix = false; |
f69165a8 | 1302 | } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { |
f53a1feb | 1303 | is_unix = true; |
1d7d2a9d PB |
1304 | } else { |
1305 | ret = -EINVAL; | |
1306 | goto out; | |
1307 | } | |
1308 | ||
1309 | p = uri->path ? uri->path : "/"; | |
1310 | p += strspn(p, "/"); | |
1311 | if (p[0]) { | |
46f5ac20 | 1312 | qdict_put_str(options, "export", p); |
1d7d2a9d PB |
1313 | } |
1314 | ||
1315 | qp = query_params_parse(uri->query); | |
f53a1feb | 1316 | if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { |
1d7d2a9d PB |
1317 | ret = -EINVAL; |
1318 | goto out; | |
1319 | } | |
1320 | ||
f53a1feb | 1321 | if (is_unix) { |
1d7d2a9d PB |
1322 | /* nbd+unix:///export?socket=path */ |
1323 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
1324 | ret = -EINVAL; | |
1325 | goto out; | |
1326 | } | |
46f5ac20 EB |
1327 | qdict_put_str(options, "server.type", "unix"); |
1328 | qdict_put_str(options, "server.path", qp->p[0].value); | |
1d7d2a9d | 1329 | } else { |
23307908 | 1330 | QString *host; |
f84d431b HR |
1331 | char *port_str; |
1332 | ||
bebbf7fa | 1333 | /* nbd[+tcp]://host[:port]/export */ |
1d7d2a9d PB |
1334 | if (!uri->server) { |
1335 | ret = -EINVAL; | |
1336 | goto out; | |
1337 | } | |
f17c90be | 1338 | |
23307908 JT |
1339 | /* strip braces from literal IPv6 address */ |
1340 | if (uri->server[0] == '[') { | |
1341 | host = qstring_from_substr(uri->server, 1, | |
ba891d68 | 1342 | strlen(uri->server) - 1); |
23307908 JT |
1343 | } else { |
1344 | host = qstring_from_str(uri->server); | |
1345 | } | |
1346 | ||
46f5ac20 | 1347 | qdict_put_str(options, "server.type", "inet"); |
9445673e | 1348 | qdict_put(options, "server.host", host); |
f84d431b HR |
1349 | |
1350 | port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); | |
46f5ac20 | 1351 | qdict_put_str(options, "server.port", port_str); |
f84d431b | 1352 | g_free(port_str); |
1d7d2a9d PB |
1353 | } |
1354 | ||
1355 | out: | |
1356 | if (qp) { | |
1357 | query_params_free(qp); | |
1358 | } | |
1359 | uri_free(uri); | |
1360 | return ret; | |
1361 | } | |
1362 | ||
48c38e0b HR |
1363 | static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) |
1364 | { | |
1365 | const QDictEntry *e; | |
1366 | ||
1367 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { | |
1368 | if (!strcmp(e->key, "host") || | |
1369 | !strcmp(e->key, "port") || | |
1370 | !strcmp(e->key, "path") || | |
491d6c7c HR |
1371 | !strcmp(e->key, "export") || |
1372 | strstart(e->key, "server.", NULL)) | |
48c38e0b HR |
1373 | { |
1374 | error_setg(errp, "Option '%s' cannot be used with a file name", | |
1375 | e->key); | |
1376 | return true; | |
1377 | } | |
1378 | } | |
1379 | ||
1380 | return false; | |
1381 | } | |
1382 | ||
6963a30d KW |
1383 | static void nbd_parse_filename(const char *filename, QDict *options, |
1384 | Error **errp) | |
75818250 | 1385 | { |
1d45f8b5 | 1386 | char *file; |
33897dc7 NT |
1387 | char *export_name; |
1388 | const char *host_spec; | |
75818250 | 1389 | const char *unixpath; |
75818250 | 1390 | |
48c38e0b | 1391 | if (nbd_has_filename_options_conflict(options, errp)) { |
681e7ad0 KW |
1392 | return; |
1393 | } | |
1394 | ||
1d7d2a9d | 1395 | if (strstr(filename, "://")) { |
6963a30d KW |
1396 | int ret = nbd_parse_uri(filename, options); |
1397 | if (ret < 0) { | |
1398 | error_setg(errp, "No valid URL specified"); | |
1399 | } | |
1400 | return; | |
1d7d2a9d PB |
1401 | } |
1402 | ||
7267c094 | 1403 | file = g_strdup(filename); |
1d45f8b5 | 1404 | |
33897dc7 NT |
1405 | export_name = strstr(file, EN_OPTSTR); |
1406 | if (export_name) { | |
1407 | if (export_name[strlen(EN_OPTSTR)] == 0) { | |
1d45f8b5 LV |
1408 | goto out; |
1409 | } | |
33897dc7 NT |
1410 | export_name[0] = 0; /* truncate 'file' */ |
1411 | export_name += strlen(EN_OPTSTR); | |
f53a1feb | 1412 | |
46f5ac20 | 1413 | qdict_put_str(options, "export", export_name); |
1d45f8b5 LV |
1414 | } |
1415 | ||
33897dc7 NT |
1416 | /* extract the host_spec - fail if it's not nbd:... */ |
1417 | if (!strstart(file, "nbd:", &host_spec)) { | |
6963a30d | 1418 | error_setg(errp, "File name string for NBD must start with 'nbd:'"); |
1d45f8b5 LV |
1419 | goto out; |
1420 | } | |
75818250 | 1421 | |
f53a1feb | 1422 | if (!*host_spec) { |
f53a1feb KW |
1423 | goto out; |
1424 | } | |
1425 | ||
33897dc7 NT |
1426 | /* are we a UNIX or TCP socket? */ |
1427 | if (strstart(host_spec, "unix:", &unixpath)) { | |
46f5ac20 EB |
1428 | qdict_put_str(options, "server.type", "unix"); |
1429 | qdict_put_str(options, "server.path", unixpath); | |
75818250 | 1430 | } else { |
0785bd7a | 1431 | InetSocketAddress *addr = g_new(InetSocketAddress, 1); |
f53a1feb | 1432 | |
0785bd7a MA |
1433 | if (inet_parse(addr, host_spec, errp)) { |
1434 | goto out_inet; | |
f17c90be | 1435 | } |
75818250 | 1436 | |
46f5ac20 EB |
1437 | qdict_put_str(options, "server.type", "inet"); |
1438 | qdict_put_str(options, "server.host", addr->host); | |
1439 | qdict_put_str(options, "server.port", addr->port); | |
0785bd7a | 1440 | out_inet: |
f53a1feb KW |
1441 | qapi_free_InetSocketAddress(addr); |
1442 | } | |
75818250 | 1443 | |
33897dc7 | 1444 | out: |
7267c094 | 1445 | g_free(file); |
f53a1feb KW |
1446 | } |
1447 | ||
491d6c7c HR |
1448 | static bool nbd_process_legacy_socket_options(QDict *output_options, |
1449 | QemuOpts *legacy_opts, | |
1450 | Error **errp) | |
f53a1feb | 1451 | { |
491d6c7c HR |
1452 | const char *path = qemu_opt_get(legacy_opts, "path"); |
1453 | const char *host = qemu_opt_get(legacy_opts, "host"); | |
1454 | const char *port = qemu_opt_get(legacy_opts, "port"); | |
1455 | const QDictEntry *e; | |
f53a1feb | 1456 | |
491d6c7c HR |
1457 | if (!path && !host && !port) { |
1458 | return true; | |
1459 | } | |
03504d05 | 1460 | |
491d6c7c HR |
1461 | for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) |
1462 | { | |
1463 | if (strstart(e->key, "server.", NULL)) { | |
1464 | error_setg(errp, "Cannot use 'server' and path/host/port at the " | |
1465 | "same time"); | |
1466 | return false; | |
681e7ad0 | 1467 | } |
33897dc7 | 1468 | } |
491d6c7c HR |
1469 | |
1470 | if (path && host) { | |
1471 | error_setg(errp, "path and host may not be used at the same time"); | |
1472 | return false; | |
1473 | } else if (path) { | |
1474 | if (port) { | |
1475 | error_setg(errp, "port may not be used without host"); | |
1476 | return false; | |
1477 | } | |
1478 | ||
46f5ac20 EB |
1479 | qdict_put_str(output_options, "server.type", "unix"); |
1480 | qdict_put_str(output_options, "server.path", path); | |
491d6c7c | 1481 | } else if (host) { |
46f5ac20 EB |
1482 | qdict_put_str(output_options, "server.type", "inet"); |
1483 | qdict_put_str(output_options, "server.host", host); | |
1484 | qdict_put_str(output_options, "server.port", | |
1485 | port ?: stringify(NBD_DEFAULT_PORT)); | |
442045cb | 1486 | } |
f53a1feb | 1487 | |
491d6c7c HR |
1488 | return true; |
1489 | } | |
f53a1feb | 1490 | |
62cf396b MA |
1491 | static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, |
1492 | Error **errp) | |
491d6c7c | 1493 | { |
62cf396b | 1494 | SocketAddress *saddr = NULL; |
491d6c7c | 1495 | QDict *addr = NULL; |
491d6c7c HR |
1496 | Visitor *iv = NULL; |
1497 | Error *local_err = NULL; | |
1498 | ||
1499 | qdict_extract_subqdict(options, &addr, "server."); | |
1500 | if (!qdict_size(addr)) { | |
1501 | error_setg(errp, "NBD server address missing"); | |
1502 | goto done; | |
f53a1feb KW |
1503 | } |
1504 | ||
af91062e MA |
1505 | iv = qobject_input_visitor_new_flat_confused(addr, errp); |
1506 | if (!iv) { | |
491d6c7c HR |
1507 | goto done; |
1508 | } | |
bebbf7fa | 1509 | |
62cf396b | 1510 | visit_type_SocketAddress(iv, NULL, &saddr, &local_err); |
491d6c7c HR |
1511 | if (local_err) { |
1512 | error_propagate(errp, local_err); | |
1513 | goto done; | |
1514 | } | |
7a5ed437 | 1515 | |
491d6c7c | 1516 | done: |
cb3e7f08 | 1517 | qobject_unref(addr); |
491d6c7c | 1518 | visit_free(iv); |
7a5ed437 | 1519 | return saddr; |
33897dc7 | 1520 | } |
1d45f8b5 | 1521 | |
75822a12 DB |
1522 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) |
1523 | { | |
1524 | Object *obj; | |
1525 | QCryptoTLSCreds *creds; | |
1526 | ||
1527 | obj = object_resolve_path_component( | |
1528 | object_get_objects_root(), id); | |
1529 | if (!obj) { | |
1530 | error_setg(errp, "No TLS credentials with id '%s'", | |
1531 | id); | |
1532 | return NULL; | |
1533 | } | |
1534 | creds = (QCryptoTLSCreds *) | |
1535 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
1536 | if (!creds) { | |
1537 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
1538 | id); | |
1539 | return NULL; | |
1540 | } | |
1541 | ||
1542 | if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { | |
1543 | error_setg(errp, | |
1544 | "Expecting TLS credentials with a client endpoint"); | |
1545 | return NULL; | |
1546 | } | |
1547 | object_ref(obj); | |
1548 | return creds; | |
1549 | } | |
1550 | ||
1551 | ||
7ccc44fd HR |
1552 | static QemuOptsList nbd_runtime_opts = { |
1553 | .name = "nbd", | |
1554 | .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), | |
1555 | .desc = { | |
1556 | { | |
1557 | .name = "host", | |
1558 | .type = QEMU_OPT_STRING, | |
1559 | .help = "TCP host to connect to", | |
1560 | }, | |
1561 | { | |
1562 | .name = "port", | |
1563 | .type = QEMU_OPT_STRING, | |
1564 | .help = "TCP port to connect to", | |
1565 | }, | |
1566 | { | |
1567 | .name = "path", | |
1568 | .type = QEMU_OPT_STRING, | |
1569 | .help = "Unix socket path to connect to", | |
1570 | }, | |
1571 | { | |
1572 | .name = "export", | |
1573 | .type = QEMU_OPT_STRING, | |
1574 | .help = "Name of the NBD export to open", | |
1575 | }, | |
1576 | { | |
1577 | .name = "tls-creds", | |
1578 | .type = QEMU_OPT_STRING, | |
1579 | .help = "ID of the TLS credentials to use", | |
1580 | }, | |
216ee365 EB |
1581 | { |
1582 | .name = "x-dirty-bitmap", | |
1583 | .type = QEMU_OPT_STRING, | |
1584 | .help = "experimental: expose named dirty bitmap in place of " | |
1585 | "block status", | |
1586 | }, | |
c4365735 | 1587 | { /* end of list */ } |
7ccc44fd HR |
1588 | }, |
1589 | }; | |
1590 | ||
015a1036 HR |
1591 | static int nbd_open(BlockDriverState *bs, QDict *options, int flags, |
1592 | Error **errp) | |
33897dc7 NT |
1593 | { |
1594 | BDRVNBDState *s = bs->opaque; | |
7ccc44fd HR |
1595 | QemuOpts *opts = NULL; |
1596 | Error *local_err = NULL; | |
75822a12 DB |
1597 | QCryptoTLSCreds *tlscreds = NULL; |
1598 | const char *hostname = NULL; | |
1599 | int ret = -EINVAL; | |
ae255e52 | 1600 | |
7ccc44fd HR |
1601 | opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); |
1602 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
1603 | if (local_err) { | |
1604 | error_propagate(errp, local_err); | |
1605 | goto error; | |
1606 | } | |
1607 | ||
62cf396b | 1608 | /* Translate @host, @port, and @path to a SocketAddress */ |
491d6c7c HR |
1609 | if (!nbd_process_legacy_socket_options(options, opts, errp)) { |
1610 | goto error; | |
1611 | } | |
1612 | ||
33897dc7 | 1613 | /* Pop the config into our state object. Exit if invalid. */ |
491d6c7c HR |
1614 | s->saddr = nbd_config(s, options, errp); |
1615 | if (!s->saddr) { | |
75822a12 DB |
1616 | goto error; |
1617 | } | |
1618 | ||
491d6c7c HR |
1619 | s->export = g_strdup(qemu_opt_get(opts, "export")); |
1620 | ||
03504d05 HR |
1621 | s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); |
1622 | if (s->tlscredsid) { | |
1623 | tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); | |
75822a12 DB |
1624 | if (!tlscreds) { |
1625 | goto error; | |
1626 | } | |
1627 | ||
ca0b64e5 | 1628 | /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */ |
62cf396b | 1629 | if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) { |
75822a12 DB |
1630 | error_setg(errp, "TLS only supported over IP sockets"); |
1631 | goto error; | |
1632 | } | |
9445673e | 1633 | hostname = s->saddr->u.inet.host; |
33897dc7 NT |
1634 | } |
1635 | ||
2302c1ca | 1636 | /* NBD handshake */ |
d42f78e9 | 1637 | ret = nbd_client_init(bs, s->saddr, s->export, tlscreds, hostname, |
216ee365 | 1638 | qemu_opt_get(opts, "x-dirty-bitmap"), errp); |
d42f78e9 | 1639 | |
75822a12 | 1640 | error: |
75822a12 DB |
1641 | if (tlscreds) { |
1642 | object_unref(OBJECT(tlscreds)); | |
1643 | } | |
03504d05 | 1644 | if (ret < 0) { |
62cf396b | 1645 | qapi_free_SocketAddress(s->saddr); |
03504d05 HR |
1646 | g_free(s->export); |
1647 | g_free(s->tlscredsid); | |
1648 | } | |
7ccc44fd | 1649 | qemu_opts_del(opts); |
75822a12 | 1650 | return ret; |
e183ef75 PB |
1651 | } |
1652 | ||
1486d04a PB |
1653 | static int nbd_co_flush(BlockDriverState *bs) |
1654 | { | |
f53a829b | 1655 | return nbd_client_co_flush(bs); |
1486d04a PB |
1656 | } |
1657 | ||
fa21e6fa DL |
1658 | static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) |
1659 | { | |
611ae1d7 | 1660 | BDRVNBDState *s = (BDRVNBDState *)bs->opaque; |
fd8d372d | 1661 | uint32_t min = s->info.min_block; |
081dd1fe EB |
1662 | uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); |
1663 | ||
7da537f7 EB |
1664 | /* |
1665 | * If the server did not advertise an alignment: | |
1666 | * - a size that is not sector-aligned implies that an alignment | |
1667 | * of 1 can be used to access those tail bytes | |
1668 | * - advertisement of block status requires an alignment of 1, so | |
1669 | * that we don't violate block layer constraints that block | |
1670 | * status is always aligned (as we can't control whether the | |
1671 | * server will report sub-sector extents, such as a hole at EOF | |
1672 | * on an unaligned POSIX file) | |
1673 | * - otherwise, assume the server is so old that we are safer avoiding | |
1674 | * sub-sector requests | |
1675 | */ | |
1676 | if (!min) { | |
1677 | min = (!QEMU_IS_ALIGNED(s->info.size, BDRV_SECTOR_SIZE) || | |
1678 | s->info.base_allocation) ? 1 : BDRV_SECTOR_SIZE; | |
1679 | } | |
1680 | ||
1681 | bs->bl.request_alignment = min; | |
081dd1fe EB |
1682 | bs->bl.max_pdiscard = max; |
1683 | bs->bl.max_pwrite_zeroes = max; | |
1684 | bs->bl.max_transfer = max; | |
1685 | ||
1686 | if (s->info.opt_block && | |
1687 | s->info.opt_block > bs->bl.opt_transfer) { | |
1688 | bs->bl.opt_transfer = s->info.opt_block; | |
1689 | } | |
fa21e6fa DL |
1690 | } |
1691 | ||
75818250 TS |
1692 | static void nbd_close(BlockDriverState *bs) |
1693 | { | |
03504d05 HR |
1694 | BDRVNBDState *s = bs->opaque; |
1695 | ||
f53a829b | 1696 | nbd_client_close(bs); |
03504d05 | 1697 | |
62cf396b | 1698 | qapi_free_SocketAddress(s->saddr); |
03504d05 HR |
1699 | g_free(s->export); |
1700 | g_free(s->tlscredsid); | |
75818250 TS |
1701 | } |
1702 | ||
1703 | static int64_t nbd_getlength(BlockDriverState *bs) | |
1704 | { | |
1705 | BDRVNBDState *s = bs->opaque; | |
1706 | ||
611ae1d7 | 1707 | return s->info.size; |
75818250 TS |
1708 | } |
1709 | ||
998b3a1e | 1710 | static void nbd_refresh_filename(BlockDriverState *bs) |
2019d68b | 1711 | { |
03504d05 | 1712 | BDRVNBDState *s = bs->opaque; |
491d6c7c HR |
1713 | const char *host = NULL, *port = NULL, *path = NULL; |
1714 | ||
62cf396b | 1715 | if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { |
9445673e | 1716 | const InetSocketAddress *inet = &s->saddr->u.inet; |
491d6c7c HR |
1717 | if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { |
1718 | host = inet->host; | |
1719 | port = inet->port; | |
1720 | } | |
62cf396b | 1721 | } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { |
9445673e MA |
1722 | path = s->saddr->u.q_unix.path; |
1723 | } /* else can't represent as pseudo-filename */ | |
2019d68b | 1724 | |
491d6c7c | 1725 | if (path && s->export) { |
2019d68b | 1726 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
1727 | "nbd+unix:///%s?socket=%s", s->export, path); |
1728 | } else if (path && !s->export) { | |
2019d68b | 1729 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
1730 | "nbd+unix://?socket=%s", path); |
1731 | } else if (host && s->export) { | |
ec0de768 | 1732 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
1733 | "nbd://%s:%s/%s", host, port, s->export); |
1734 | } else if (host && !s->export) { | |
ec0de768 | 1735 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c | 1736 | "nbd://%s:%s", host, port); |
ec0de768 | 1737 | } |
2019d68b HR |
1738 | } |
1739 | ||
8a6239c0 HR |
1740 | static char *nbd_dirname(BlockDriverState *bs, Error **errp) |
1741 | { | |
1742 | /* The generic bdrv_dirname() implementation is able to work out some | |
1743 | * directory name for NBD nodes, but that would be wrong. So far there is no | |
1744 | * specification for how "export paths" would work, so NBD does not have | |
1745 | * directory names. */ | |
1746 | error_setg(errp, "Cannot generate a base directory for NBD nodes"); | |
1747 | return NULL; | |
1748 | } | |
1749 | ||
2654267c HR |
1750 | static const char *const nbd_strong_runtime_opts[] = { |
1751 | "path", | |
1752 | "host", | |
1753 | "port", | |
1754 | "export", | |
1755 | "tls-creds", | |
1756 | "server.", | |
1757 | ||
1758 | NULL | |
1759 | }; | |
1760 | ||
5efa9d5a | 1761 | static BlockDriver bdrv_nbd = { |
69447cd8 SH |
1762 | .format_name = "nbd", |
1763 | .protocol_name = "nbd", | |
1764 | .instance_size = sizeof(BDRVNBDState), | |
1765 | .bdrv_parse_filename = nbd_parse_filename, | |
1766 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
1767 | .bdrv_co_preadv = nbd_client_co_preadv, |
1768 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 1769 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
1770 | .bdrv_close = nbd_close, |
1771 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 1772 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 1773 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 | 1774 | .bdrv_getlength = nbd_getlength, |
86f8cdf3 VSO |
1775 | .bdrv_detach_aio_context = nbd_client_detach_aio_context, |
1776 | .bdrv_attach_aio_context = nbd_client_attach_aio_context, | |
2019d68b | 1777 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 1778 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 1779 | .bdrv_dirname = nbd_dirname, |
2654267c | 1780 | .strong_runtime_opts = nbd_strong_runtime_opts, |
1d7d2a9d PB |
1781 | }; |
1782 | ||
1783 | static BlockDriver bdrv_nbd_tcp = { | |
69447cd8 SH |
1784 | .format_name = "nbd", |
1785 | .protocol_name = "nbd+tcp", | |
1786 | .instance_size = sizeof(BDRVNBDState), | |
1787 | .bdrv_parse_filename = nbd_parse_filename, | |
1788 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
1789 | .bdrv_co_preadv = nbd_client_co_preadv, |
1790 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 1791 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
1792 | .bdrv_close = nbd_close, |
1793 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 1794 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 1795 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 | 1796 | .bdrv_getlength = nbd_getlength, |
86f8cdf3 VSO |
1797 | .bdrv_detach_aio_context = nbd_client_detach_aio_context, |
1798 | .bdrv_attach_aio_context = nbd_client_attach_aio_context, | |
2019d68b | 1799 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 1800 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 1801 | .bdrv_dirname = nbd_dirname, |
2654267c | 1802 | .strong_runtime_opts = nbd_strong_runtime_opts, |
1d7d2a9d PB |
1803 | }; |
1804 | ||
1805 | static BlockDriver bdrv_nbd_unix = { | |
69447cd8 SH |
1806 | .format_name = "nbd", |
1807 | .protocol_name = "nbd+unix", | |
1808 | .instance_size = sizeof(BDRVNBDState), | |
1809 | .bdrv_parse_filename = nbd_parse_filename, | |
1810 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
1811 | .bdrv_co_preadv = nbd_client_co_preadv, |
1812 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 1813 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
1814 | .bdrv_close = nbd_close, |
1815 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 1816 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 1817 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 | 1818 | .bdrv_getlength = nbd_getlength, |
86f8cdf3 VSO |
1819 | .bdrv_detach_aio_context = nbd_client_detach_aio_context, |
1820 | .bdrv_attach_aio_context = nbd_client_attach_aio_context, | |
2019d68b | 1821 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 1822 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 1823 | .bdrv_dirname = nbd_dirname, |
2654267c | 1824 | .strong_runtime_opts = nbd_strong_runtime_opts, |
75818250 | 1825 | }; |
5efa9d5a AL |
1826 | |
1827 | static void bdrv_nbd_init(void) | |
1828 | { | |
1829 | bdrv_register(&bdrv_nbd); | |
1d7d2a9d PB |
1830 | bdrv_register(&bdrv_nbd_tcp); |
1831 | bdrv_register(&bdrv_nbd_unix); | |
5efa9d5a AL |
1832 | } |
1833 | ||
1834 | block_init(bdrv_nbd_init); |