]>
Commit | Line | Data |
---|---|---|
2302c1ca MAL |
1 | /* |
2 | * QEMU Block driver for NBD | |
3 | * | |
b626b51a | 4 | * Copyright (C) 2016 Red Hat, Inc. |
2302c1ca MAL |
5 | * Copyright (C) 2008 Bull S.A.S. |
6 | * Author: Laurent Vivier <[email protected]> | |
7 | * | |
8 | * Some parts: | |
9 | * Copyright (C) 2007 Anthony Liguori <[email protected]> | |
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" |
be41c100 | 31 | #include "qapi/error.h" |
2302c1ca | 32 | #include "nbd-client.h" |
2302c1ca | 33 | |
cfa3ad63 EB |
34 | #define HANDLE_TO_INDEX(bs, handle) ((handle) ^ (uint64_t)(intptr_t)(bs)) |
35 | #define INDEX_TO_HANDLE(bs, index) ((index) ^ (uint64_t)(intptr_t)(bs)) | |
2302c1ca | 36 | |
07b1b99c | 37 | static void nbd_recv_coroutines_wake_all(NBDClientSession *s) |
69152c09 MAL |
38 | { |
39 | int i; | |
40 | ||
41 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
40f4a218 SH |
42 | NBDClientRequest *req = &s->requests[i]; |
43 | ||
44 | if (req->coroutine && req->receiving) { | |
45 | aio_co_wake(req->coroutine); | |
69152c09 MAL |
46 | } |
47 | } | |
48 | } | |
49 | ||
f53a829b | 50 | static void nbd_teardown_connection(BlockDriverState *bs) |
4a41a2d6 | 51 | { |
10676b81 | 52 | NBDClientSession *client = nbd_get_client_session(bs); |
f53a829b | 53 | |
064097d9 DB |
54 | if (!client->ioc) { /* Already closed */ |
55 | return; | |
56 | } | |
57 | ||
4a41a2d6 | 58 | /* finish any pending coroutines */ |
064097d9 DB |
59 | qio_channel_shutdown(client->ioc, |
60 | QIO_CHANNEL_SHUTDOWN_BOTH, | |
61 | NULL); | |
a12a712a | 62 | BDRV_POLL_WHILE(bs, client->read_reply_co); |
4a41a2d6 | 63 | |
f53a829b | 64 | nbd_client_detach_aio_context(bs); |
064097d9 DB |
65 | object_unref(OBJECT(client->sioc)); |
66 | client->sioc = NULL; | |
67 | object_unref(OBJECT(client->ioc)); | |
68 | client->ioc = NULL; | |
4a41a2d6 SH |
69 | } |
70 | ||
ff82911c | 71 | static coroutine_fn void nbd_read_reply_entry(void *opaque) |
2302c1ca | 72 | { |
ff82911c | 73 | NBDClientSession *s = opaque; |
2302c1ca | 74 | uint64_t i; |
d0a18013 | 75 | int ret = 0; |
be41c100 | 76 | Error *local_err = NULL; |
2302c1ca | 77 | |
72b6ffc7 | 78 | while (!s->quit) { |
ff82911c | 79 | assert(s->reply.handle == 0); |
be41c100 VSO |
80 | ret = nbd_receive_reply(s->ioc, &s->reply, &local_err); |
81 | if (ret < 0) { | |
82 | error_report_err(local_err); | |
83 | } | |
a12a712a | 84 | if (ret <= 0) { |
ff82911c | 85 | break; |
2302c1ca | 86 | } |
2302c1ca | 87 | |
ff82911c PB |
88 | /* There's no need for a mutex on the receive side, because the |
89 | * handler acts as a synchronization point and ensures that only | |
90 | * one coroutine is called until the reply finishes. | |
91 | */ | |
92 | i = HANDLE_TO_INDEX(s, s->reply.handle); | |
40f4a218 SH |
93 | if (i >= MAX_NBD_REQUESTS || |
94 | !s->requests[i].coroutine || | |
d2febedb | 95 | !s->requests[i].receiving || |
f140e300 | 96 | (nbd_reply_is_structured(&s->reply) && !s->info.structured_reply)) |
d2febedb | 97 | { |
ff82911c PB |
98 | break; |
99 | } | |
2302c1ca | 100 | |
40f4a218 | 101 | /* We're woken up again by the request itself. Note that there |
ff82911c PB |
102 | * is no race between yielding and reentering read_reply_co. This |
103 | * is because: | |
104 | * | |
40f4a218 | 105 | * - if the request runs on the same AioContext, it is only |
ff82911c PB |
106 | * entered after we yield |
107 | * | |
40f4a218 | 108 | * - if the request runs on a different AioContext, reentering |
ff82911c PB |
109 | * read_reply_co happens through a bottom half, which can only |
110 | * run after we yield. | |
111 | */ | |
40f4a218 | 112 | aio_co_wake(s->requests[i].coroutine); |
ff82911c | 113 | qemu_coroutine_yield(); |
2302c1ca | 114 | } |
a12a712a | 115 | |
40f4a218 | 116 | s->quit = true; |
07b1b99c | 117 | nbd_recv_coroutines_wake_all(s); |
ff82911c | 118 | s->read_reply_co = NULL; |
2302c1ca MAL |
119 | } |
120 | ||
f53a829b | 121 | static int nbd_co_send_request(BlockDriverState *bs, |
ed2dd912 | 122 | NBDRequest *request, |
1e2a77a8 | 123 | QEMUIOVector *qiov) |
2302c1ca | 124 | { |
10676b81 | 125 | NBDClientSession *s = nbd_get_client_session(bs); |
030fa7f6 | 126 | int rc, i; |
2302c1ca MAL |
127 | |
128 | qemu_co_mutex_lock(&s->send_mutex); | |
6bdcc018 PB |
129 | while (s->in_flight == MAX_NBD_REQUESTS) { |
130 | qemu_co_queue_wait(&s->free_sema, &s->send_mutex); | |
131 | } | |
132 | s->in_flight++; | |
141cabe6 BW |
133 | |
134 | for (i = 0; i < MAX_NBD_REQUESTS; i++) { | |
40f4a218 | 135 | if (s->requests[i].coroutine == NULL) { |
141cabe6 BW |
136 | break; |
137 | } | |
138 | } | |
139 | ||
1c778ef7 | 140 | g_assert(qemu_in_coroutine()); |
141cabe6 | 141 | assert(i < MAX_NBD_REQUESTS); |
40f4a218 SH |
142 | |
143 | s->requests[i].coroutine = qemu_coroutine_self(); | |
f140e300 | 144 | s->requests[i].offset = request->from; |
40f4a218 SH |
145 | s->requests[i].receiving = false; |
146 | ||
141cabe6 | 147 | request->handle = INDEX_TO_HANDLE(s, i); |
064097d9 | 148 | |
72b6ffc7 | 149 | if (s->quit) { |
3c2d5183 SH |
150 | rc = -EIO; |
151 | goto err; | |
72b6ffc7 | 152 | } |
064097d9 | 153 | if (!s->ioc) { |
3c2d5183 SH |
154 | rc = -EPIPE; |
155 | goto err; | |
064097d9 DB |
156 | } |
157 | ||
2302c1ca | 158 | if (qiov) { |
064097d9 | 159 | qio_channel_set_cork(s->ioc, true); |
1c778ef7 | 160 | rc = nbd_send_request(s->ioc, request); |
72b6ffc7 | 161 | if (rc >= 0 && !s->quit) { |
030fa7f6 EB |
162 | if (qio_channel_writev_all(s->ioc, qiov->iov, qiov->niov, |
163 | NULL) < 0) { | |
2302c1ca MAL |
164 | rc = -EIO; |
165 | } | |
a6934370 VSO |
166 | } else if (rc >= 0) { |
167 | rc = -EIO; | |
2302c1ca | 168 | } |
064097d9 | 169 | qio_channel_set_cork(s->ioc, false); |
2302c1ca | 170 | } else { |
1c778ef7 | 171 | rc = nbd_send_request(s->ioc, request); |
2302c1ca | 172 | } |
3c2d5183 SH |
173 | |
174 | err: | |
72b6ffc7 EB |
175 | if (rc < 0) { |
176 | s->quit = true; | |
3c2d5183 SH |
177 | s->requests[i].coroutine = NULL; |
178 | s->in_flight--; | |
179 | qemu_co_queue_next(&s->free_sema); | |
72b6ffc7 | 180 | } |
2302c1ca MAL |
181 | qemu_co_mutex_unlock(&s->send_mutex); |
182 | return rc; | |
183 | } | |
184 | ||
f140e300 VSO |
185 | static inline uint16_t payload_advance16(uint8_t **payload) |
186 | { | |
187 | *payload += 2; | |
188 | return lduw_be_p(*payload - 2); | |
189 | } | |
190 | ||
191 | static inline uint32_t payload_advance32(uint8_t **payload) | |
192 | { | |
193 | *payload += 4; | |
194 | return ldl_be_p(*payload - 4); | |
195 | } | |
196 | ||
197 | static inline uint64_t payload_advance64(uint8_t **payload) | |
198 | { | |
199 | *payload += 8; | |
200 | return ldq_be_p(*payload - 8); | |
201 | } | |
202 | ||
203 | static int nbd_parse_offset_hole_payload(NBDStructuredReplyChunk *chunk, | |
204 | uint8_t *payload, uint64_t orig_offset, | |
205 | QEMUIOVector *qiov, Error **errp) | |
206 | { | |
207 | uint64_t offset; | |
208 | uint32_t hole_size; | |
209 | ||
210 | if (chunk->length != sizeof(offset) + sizeof(hole_size)) { | |
211 | error_setg(errp, "Protocol error: invalid payload for " | |
212 | "NBD_REPLY_TYPE_OFFSET_HOLE"); | |
213 | return -EINVAL; | |
214 | } | |
215 | ||
216 | offset = payload_advance64(&payload); | |
217 | hole_size = payload_advance32(&payload); | |
218 | ||
219 | if (offset < orig_offset || hole_size > qiov->size || | |
220 | offset > orig_offset + qiov->size - hole_size) { | |
221 | error_setg(errp, "Protocol error: server sent chunk exceeding requested" | |
222 | " region"); | |
223 | return -EINVAL; | |
224 | } | |
225 | ||
226 | qemu_iovec_memset(qiov, offset - orig_offset, 0, hole_size); | |
227 | ||
228 | return 0; | |
229 | } | |
230 | ||
231 | /* nbd_parse_error_payload | |
232 | * on success @errp contains message describing nbd error reply | |
233 | */ | |
234 | static int nbd_parse_error_payload(NBDStructuredReplyChunk *chunk, | |
235 | uint8_t *payload, int *request_ret, | |
236 | Error **errp) | |
237 | { | |
238 | uint32_t error; | |
239 | uint16_t message_size; | |
240 | ||
241 | assert(chunk->type & (1 << 15)); | |
242 | ||
243 | if (chunk->length < sizeof(error) + sizeof(message_size)) { | |
244 | error_setg(errp, | |
245 | "Protocol error: invalid payload for structured error"); | |
246 | return -EINVAL; | |
247 | } | |
248 | ||
249 | error = nbd_errno_to_system_errno(payload_advance32(&payload)); | |
250 | if (error == 0) { | |
251 | error_setg(errp, "Protocol error: server sent structured error chunk" | |
252 | "with error = 0"); | |
253 | return -EINVAL; | |
254 | } | |
255 | ||
256 | *request_ret = -error; | |
257 | message_size = payload_advance16(&payload); | |
258 | ||
259 | if (message_size > chunk->length - sizeof(error) - sizeof(message_size)) { | |
260 | error_setg(errp, "Protocol error: server sent structured error chunk" | |
261 | "with incorrect message size"); | |
262 | return -EINVAL; | |
263 | } | |
264 | ||
265 | /* TODO: Add a trace point to mention the server complaint */ | |
266 | ||
267 | /* TODO handle ERROR_OFFSET */ | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
272 | static int nbd_co_receive_offset_data_payload(NBDClientSession *s, | |
273 | uint64_t orig_offset, | |
274 | QEMUIOVector *qiov, Error **errp) | |
275 | { | |
276 | QEMUIOVector sub_qiov; | |
277 | uint64_t offset; | |
278 | size_t data_size; | |
279 | int ret; | |
280 | NBDStructuredReplyChunk *chunk = &s->reply.structured; | |
281 | ||
282 | assert(nbd_reply_is_structured(&s->reply)); | |
283 | ||
284 | if (chunk->length < sizeof(offset)) { | |
285 | error_setg(errp, "Protocol error: invalid payload for " | |
286 | "NBD_REPLY_TYPE_OFFSET_DATA"); | |
287 | return -EINVAL; | |
288 | } | |
289 | ||
290 | if (nbd_read(s->ioc, &offset, sizeof(offset), errp) < 0) { | |
291 | return -EIO; | |
292 | } | |
293 | be64_to_cpus(&offset); | |
294 | ||
295 | data_size = chunk->length - sizeof(offset); | |
296 | if (offset < orig_offset || data_size > qiov->size || | |
297 | offset > orig_offset + qiov->size - data_size) { | |
298 | error_setg(errp, "Protocol error: server sent chunk exceeding requested" | |
299 | " region"); | |
300 | return -EINVAL; | |
301 | } | |
302 | ||
303 | qemu_iovec_init(&sub_qiov, qiov->niov); | |
304 | qemu_iovec_concat(&sub_qiov, qiov, offset - orig_offset, data_size); | |
305 | ret = qio_channel_readv_all(s->ioc, sub_qiov.iov, sub_qiov.niov, errp); | |
306 | qemu_iovec_destroy(&sub_qiov); | |
307 | ||
308 | return ret < 0 ? -EIO : 0; | |
309 | } | |
310 | ||
311 | #define NBD_MAX_MALLOC_PAYLOAD 1000 | |
312 | /* nbd_co_receive_structured_payload | |
313 | */ | |
314 | static coroutine_fn int nbd_co_receive_structured_payload( | |
315 | NBDClientSession *s, void **payload, Error **errp) | |
316 | { | |
317 | int ret; | |
318 | uint32_t len; | |
319 | ||
320 | assert(nbd_reply_is_structured(&s->reply)); | |
321 | ||
322 | len = s->reply.structured.length; | |
323 | ||
324 | if (len == 0) { | |
325 | return 0; | |
326 | } | |
327 | ||
328 | if (payload == NULL) { | |
329 | error_setg(errp, "Unexpected structured payload"); | |
330 | return -EINVAL; | |
331 | } | |
332 | ||
333 | if (len > NBD_MAX_MALLOC_PAYLOAD) { | |
334 | error_setg(errp, "Payload too large"); | |
335 | return -EINVAL; | |
336 | } | |
337 | ||
338 | *payload = g_new(char, len); | |
339 | ret = nbd_read(s->ioc, *payload, len, errp); | |
340 | if (ret < 0) { | |
341 | g_free(*payload); | |
342 | *payload = NULL; | |
343 | return ret; | |
344 | } | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
349 | /* nbd_co_do_receive_one_chunk | |
350 | * for simple reply: | |
351 | * set request_ret to received reply error | |
352 | * if qiov is not NULL: read payload to @qiov | |
353 | * for structured reply chunk: | |
354 | * if error chunk: read payload, set @request_ret, do not set @payload | |
355 | * else if offset_data chunk: read payload data to @qiov, do not set @payload | |
356 | * else: read payload to @payload | |
357 | * | |
358 | * If function fails, @errp contains corresponding error message, and the | |
359 | * connection with the server is suspect. If it returns 0, then the | |
360 | * transaction succeeded (although @request_ret may be a negative errno | |
361 | * corresponding to the server's error reply), and errp is unchanged. | |
362 | */ | |
363 | static coroutine_fn int nbd_co_do_receive_one_chunk( | |
364 | NBDClientSession *s, uint64_t handle, bool only_structured, | |
365 | int *request_ret, QEMUIOVector *qiov, void **payload, Error **errp) | |
2302c1ca | 366 | { |
319a56cd | 367 | int ret; |
ed397b2f | 368 | int i = HANDLE_TO_INDEX(s, handle); |
f140e300 VSO |
369 | void *local_payload = NULL; |
370 | NBDStructuredReplyChunk *chunk; | |
371 | ||
372 | if (payload) { | |
373 | *payload = NULL; | |
374 | } | |
375 | *request_ret = 0; | |
2302c1ca | 376 | |
ff82911c | 377 | /* Wait until we're woken up by nbd_read_reply_entry. */ |
40f4a218 | 378 | s->requests[i].receiving = true; |
2302c1ca | 379 | qemu_coroutine_yield(); |
40f4a218 | 380 | s->requests[i].receiving = false; |
93970672 | 381 | if (!s->ioc || s->quit) { |
f140e300 VSO |
382 | error_setg(errp, "Connection closed"); |
383 | return -EIO; | |
384 | } | |
385 | ||
386 | assert(s->reply.handle == handle); | |
387 | ||
388 | if (nbd_reply_is_simple(&s->reply)) { | |
389 | if (only_structured) { | |
390 | error_setg(errp, "Protocol error: simple reply when structured " | |
391 | "reply chunk was expected"); | |
392 | return -EINVAL; | |
2302c1ca MAL |
393 | } |
394 | ||
f140e300 VSO |
395 | *request_ret = -nbd_errno_to_system_errno(s->reply.simple.error); |
396 | if (*request_ret < 0 || !qiov) { | |
397 | return 0; | |
398 | } | |
399 | ||
400 | return qio_channel_readv_all(s->ioc, qiov->iov, qiov->niov, | |
401 | errp) < 0 ? -EIO : 0; | |
402 | } | |
403 | ||
404 | /* handle structured reply chunk */ | |
405 | assert(s->info.structured_reply); | |
406 | chunk = &s->reply.structured; | |
407 | ||
408 | if (chunk->type == NBD_REPLY_TYPE_NONE) { | |
409 | if (!(chunk->flags & NBD_REPLY_FLAG_DONE)) { | |
410 | error_setg(errp, "Protocol error: NBD_REPLY_TYPE_NONE chunk without" | |
411 | "NBD_REPLY_FLAG_DONE flag set"); | |
412 | return -EINVAL; | |
413 | } | |
414 | return 0; | |
415 | } | |
416 | ||
417 | if (chunk->type == NBD_REPLY_TYPE_OFFSET_DATA) { | |
418 | if (!qiov) { | |
419 | error_setg(errp, "Unexpected NBD_REPLY_TYPE_OFFSET_DATA chunk"); | |
420 | return -EINVAL; | |
421 | } | |
422 | ||
423 | return nbd_co_receive_offset_data_payload(s, s->requests[i].offset, | |
424 | qiov, errp); | |
425 | } | |
426 | ||
427 | if (nbd_reply_type_is_error(chunk->type)) { | |
428 | payload = &local_payload; | |
429 | } | |
430 | ||
431 | ret = nbd_co_receive_structured_payload(s, payload, errp); | |
432 | if (ret < 0) { | |
433 | return ret; | |
2302c1ca | 434 | } |
ff82911c | 435 | |
f140e300 VSO |
436 | if (nbd_reply_type_is_error(chunk->type)) { |
437 | ret = nbd_parse_error_payload(chunk, local_payload, request_ret, errp); | |
438 | g_free(local_payload); | |
439 | return ret; | |
440 | } | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
445 | /* nbd_co_receive_one_chunk | |
446 | * Read reply, wake up read_reply_co and set s->quit if needed. | |
447 | * Return value is a fatal error code or normal nbd reply error code | |
448 | */ | |
449 | static coroutine_fn int nbd_co_receive_one_chunk( | |
450 | NBDClientSession *s, uint64_t handle, bool only_structured, | |
451 | QEMUIOVector *qiov, NBDReply *reply, void **payload, Error **errp) | |
452 | { | |
453 | int request_ret; | |
454 | int ret = nbd_co_do_receive_one_chunk(s, handle, only_structured, | |
455 | &request_ret, qiov, payload, errp); | |
456 | ||
457 | if (ret < 0) { | |
458 | s->quit = true; | |
459 | } else { | |
460 | /* For assert at loop start in nbd_read_reply_entry */ | |
461 | if (reply) { | |
462 | *reply = s->reply; | |
463 | } | |
464 | s->reply.handle = 0; | |
465 | ret = request_ret; | |
466 | } | |
ff82911c | 467 | |
ff82911c PB |
468 | if (s->read_reply_co) { |
469 | aio_co_wake(s->read_reply_co); | |
2302c1ca | 470 | } |
6bdcc018 | 471 | |
f140e300 VSO |
472 | return ret; |
473 | } | |
474 | ||
475 | typedef struct NBDReplyChunkIter { | |
476 | int ret; | |
477 | Error *err; | |
478 | bool done, only_structured; | |
479 | } NBDReplyChunkIter; | |
480 | ||
481 | static void nbd_iter_error(NBDReplyChunkIter *iter, bool fatal, | |
482 | int ret, Error **local_err) | |
483 | { | |
484 | assert(ret < 0); | |
485 | ||
486 | if (fatal || iter->ret == 0) { | |
487 | if (iter->ret != 0) { | |
488 | error_free(iter->err); | |
489 | iter->err = NULL; | |
490 | } | |
491 | iter->ret = ret; | |
492 | error_propagate(&iter->err, *local_err); | |
493 | } else { | |
494 | error_free(*local_err); | |
495 | } | |
496 | ||
497 | *local_err = NULL; | |
498 | } | |
499 | ||
500 | /* NBD_FOREACH_REPLY_CHUNK | |
501 | */ | |
502 | #define NBD_FOREACH_REPLY_CHUNK(s, iter, handle, structured, \ | |
503 | qiov, reply, payload) \ | |
504 | for (iter = (NBDReplyChunkIter) { .only_structured = structured }; \ | |
505 | nbd_reply_chunk_iter_receive(s, &iter, handle, qiov, reply, payload);) | |
506 | ||
507 | /* nbd_reply_chunk_iter_receive | |
508 | */ | |
509 | static bool nbd_reply_chunk_iter_receive(NBDClientSession *s, | |
510 | NBDReplyChunkIter *iter, | |
511 | uint64_t handle, | |
512 | QEMUIOVector *qiov, NBDReply *reply, | |
513 | void **payload) | |
514 | { | |
515 | int ret; | |
516 | NBDReply local_reply; | |
517 | NBDStructuredReplyChunk *chunk; | |
518 | Error *local_err = NULL; | |
519 | if (s->quit) { | |
520 | error_setg(&local_err, "Connection closed"); | |
521 | nbd_iter_error(iter, true, -EIO, &local_err); | |
522 | goto break_loop; | |
523 | } | |
524 | ||
525 | if (iter->done) { | |
526 | /* Previous iteration was last. */ | |
527 | goto break_loop; | |
528 | } | |
529 | ||
530 | if (reply == NULL) { | |
531 | reply = &local_reply; | |
532 | } | |
533 | ||
534 | ret = nbd_co_receive_one_chunk(s, handle, iter->only_structured, | |
535 | qiov, reply, payload, &local_err); | |
536 | if (ret < 0) { | |
537 | /* If it is a fatal error s->quit is set by nbd_co_receive_one_chunk */ | |
538 | nbd_iter_error(iter, s->quit, ret, &local_err); | |
539 | } | |
540 | ||
541 | /* Do not execute the body of NBD_FOREACH_REPLY_CHUNK for simple reply. */ | |
542 | if (nbd_reply_is_simple(&s->reply) || s->quit) { | |
543 | goto break_loop; | |
544 | } | |
545 | ||
546 | chunk = &reply->structured; | |
547 | iter->only_structured = true; | |
548 | ||
549 | if (chunk->type == NBD_REPLY_TYPE_NONE) { | |
550 | /* NBD_REPLY_FLAG_DONE is already checked in nbd_co_receive_one_chunk */ | |
551 | assert(chunk->flags & NBD_REPLY_FLAG_DONE); | |
552 | goto break_loop; | |
553 | } | |
554 | ||
555 | if (chunk->flags & NBD_REPLY_FLAG_DONE) { | |
556 | /* This iteration is last. */ | |
557 | iter->done = true; | |
558 | } | |
559 | ||
560 | /* Execute the loop body */ | |
561 | return true; | |
562 | ||
563 | break_loop: | |
564 | s->requests[HANDLE_TO_INDEX(s, handle)].coroutine = NULL; | |
565 | ||
6bdcc018 PB |
566 | qemu_co_mutex_lock(&s->send_mutex); |
567 | s->in_flight--; | |
568 | qemu_co_queue_next(&s->free_sema); | |
569 | qemu_co_mutex_unlock(&s->send_mutex); | |
319a56cd | 570 | |
f140e300 | 571 | return false; |
2302c1ca MAL |
572 | } |
573 | ||
f140e300 VSO |
574 | static int nbd_co_receive_return_code(NBDClientSession *s, uint64_t handle, |
575 | Error **errp) | |
576 | { | |
577 | NBDReplyChunkIter iter; | |
578 | ||
579 | NBD_FOREACH_REPLY_CHUNK(s, iter, handle, false, NULL, NULL, NULL) { | |
580 | /* nbd_reply_chunk_iter_receive does all the work */ | |
581 | } | |
582 | ||
583 | error_propagate(errp, iter.err); | |
584 | return iter.ret; | |
585 | } | |
586 | ||
587 | static int nbd_co_receive_cmdread_reply(NBDClientSession *s, uint64_t handle, | |
588 | uint64_t offset, QEMUIOVector *qiov, | |
589 | Error **errp) | |
590 | { | |
591 | NBDReplyChunkIter iter; | |
592 | NBDReply reply; | |
593 | void *payload = NULL; | |
594 | Error *local_err = NULL; | |
595 | ||
596 | NBD_FOREACH_REPLY_CHUNK(s, iter, handle, s->info.structured_reply, | |
597 | qiov, &reply, &payload) | |
598 | { | |
599 | int ret; | |
600 | NBDStructuredReplyChunk *chunk = &reply.structured; | |
601 | ||
602 | assert(nbd_reply_is_structured(&reply)); | |
603 | ||
604 | switch (chunk->type) { | |
605 | case NBD_REPLY_TYPE_OFFSET_DATA: | |
606 | /* special cased in nbd_co_receive_one_chunk, data is already | |
607 | * in qiov */ | |
608 | break; | |
609 | case NBD_REPLY_TYPE_OFFSET_HOLE: | |
610 | ret = nbd_parse_offset_hole_payload(&reply.structured, payload, | |
611 | offset, qiov, &local_err); | |
612 | if (ret < 0) { | |
613 | s->quit = true; | |
614 | nbd_iter_error(&iter, true, ret, &local_err); | |
615 | } | |
616 | break; | |
617 | default: | |
618 | if (!nbd_reply_type_is_error(chunk->type)) { | |
619 | /* not allowed reply type */ | |
620 | s->quit = true; | |
621 | error_setg(&local_err, | |
622 | "Unexpected reply type: %d (%s) for CMD_READ", | |
623 | chunk->type, nbd_reply_type_lookup(chunk->type)); | |
624 | nbd_iter_error(&iter, true, -EINVAL, &local_err); | |
625 | } | |
626 | } | |
627 | ||
628 | g_free(payload); | |
629 | payload = NULL; | |
630 | } | |
631 | ||
632 | error_propagate(errp, iter.err); | |
633 | return iter.ret; | |
634 | } | |
635 | ||
636 | static int nbd_co_request(BlockDriverState *bs, NBDRequest *request, | |
637 | QEMUIOVector *write_qiov) | |
f35dff7e | 638 | { |
f35dff7e | 639 | int ret; |
f140e300 VSO |
640 | Error *local_err = NULL; |
641 | NBDClientSession *client = nbd_get_client_session(bs); | |
f35dff7e | 642 | |
f140e300 VSO |
643 | assert(request->type != NBD_CMD_READ); |
644 | if (write_qiov) { | |
645 | assert(request->type == NBD_CMD_WRITE); | |
646 | assert(request->len == iov_size(write_qiov->iov, write_qiov->niov)); | |
4bfe4478 | 647 | } else { |
f140e300 | 648 | assert(request->type != NBD_CMD_WRITE); |
4bfe4478 | 649 | } |
f140e300 | 650 | ret = nbd_co_send_request(bs, request, write_qiov); |
f35dff7e | 651 | if (ret < 0) { |
319a56cd | 652 | return ret; |
f35dff7e | 653 | } |
319a56cd | 654 | |
f140e300 VSO |
655 | ret = nbd_co_receive_return_code(client, request->handle, &local_err); |
656 | if (local_err) { | |
657 | error_report_err(local_err); | |
658 | } | |
659 | return ret; | |
f35dff7e VSO |
660 | } |
661 | ||
70c4fb26 EB |
662 | int nbd_client_co_preadv(BlockDriverState *bs, uint64_t offset, |
663 | uint64_t bytes, QEMUIOVector *qiov, int flags) | |
2302c1ca | 664 | { |
f140e300 VSO |
665 | int ret; |
666 | Error *local_err = NULL; | |
667 | NBDClientSession *client = nbd_get_client_session(bs); | |
ed2dd912 | 668 | NBDRequest request = { |
70c4fb26 EB |
669 | .type = NBD_CMD_READ, |
670 | .from = offset, | |
671 | .len = bytes, | |
672 | }; | |
2302c1ca | 673 | |
70c4fb26 EB |
674 | assert(bytes <= NBD_MAX_BUFFER_SIZE); |
675 | assert(!flags); | |
2302c1ca | 676 | |
f140e300 VSO |
677 | ret = nbd_co_send_request(bs, &request, NULL); |
678 | if (ret < 0) { | |
679 | return ret; | |
680 | } | |
681 | ||
682 | ret = nbd_co_receive_cmdread_reply(client, request.handle, offset, qiov, | |
683 | &local_err); | |
684 | if (ret < 0) { | |
685 | error_report_err(local_err); | |
686 | } | |
687 | return ret; | |
2302c1ca MAL |
688 | } |
689 | ||
70c4fb26 EB |
690 | int nbd_client_co_pwritev(BlockDriverState *bs, uint64_t offset, |
691 | uint64_t bytes, QEMUIOVector *qiov, int flags) | |
2302c1ca | 692 | { |
10676b81 | 693 | NBDClientSession *client = nbd_get_client_session(bs); |
ed2dd912 | 694 | NBDRequest request = { |
70c4fb26 EB |
695 | .type = NBD_CMD_WRITE, |
696 | .from = offset, | |
697 | .len = bytes, | |
698 | }; | |
2302c1ca | 699 | |
52a46505 | 700 | if (flags & BDRV_REQ_FUA) { |
004a89fc | 701 | assert(client->info.flags & NBD_FLAG_SEND_FUA); |
b626b51a | 702 | request.flags |= NBD_CMD_FLAG_FUA; |
2302c1ca MAL |
703 | } |
704 | ||
70c4fb26 | 705 | assert(bytes <= NBD_MAX_BUFFER_SIZE); |
2302c1ca | 706 | |
f35dff7e | 707 | return nbd_co_request(bs, &request, qiov); |
2302c1ca MAL |
708 | } |
709 | ||
fa778fff | 710 | int nbd_client_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, |
f5a5ca79 | 711 | int bytes, BdrvRequestFlags flags) |
fa778fff | 712 | { |
fa778fff EB |
713 | NBDClientSession *client = nbd_get_client_session(bs); |
714 | NBDRequest request = { | |
715 | .type = NBD_CMD_WRITE_ZEROES, | |
716 | .from = offset, | |
f5a5ca79 | 717 | .len = bytes, |
fa778fff | 718 | }; |
fa778fff | 719 | |
004a89fc | 720 | if (!(client->info.flags & NBD_FLAG_SEND_WRITE_ZEROES)) { |
fa778fff EB |
721 | return -ENOTSUP; |
722 | } | |
723 | ||
724 | if (flags & BDRV_REQ_FUA) { | |
004a89fc | 725 | assert(client->info.flags & NBD_FLAG_SEND_FUA); |
fa778fff EB |
726 | request.flags |= NBD_CMD_FLAG_FUA; |
727 | } | |
728 | if (!(flags & BDRV_REQ_MAY_UNMAP)) { | |
729 | request.flags |= NBD_CMD_FLAG_NO_HOLE; | |
730 | } | |
731 | ||
f35dff7e | 732 | return nbd_co_request(bs, &request, NULL); |
fa778fff EB |
733 | } |
734 | ||
f53a829b | 735 | int nbd_client_co_flush(BlockDriverState *bs) |
2302c1ca | 736 | { |
10676b81 | 737 | NBDClientSession *client = nbd_get_client_session(bs); |
ed2dd912 | 738 | NBDRequest request = { .type = NBD_CMD_FLUSH }; |
2302c1ca | 739 | |
004a89fc | 740 | if (!(client->info.flags & NBD_FLAG_SEND_FLUSH)) { |
2302c1ca MAL |
741 | return 0; |
742 | } | |
743 | ||
2302c1ca MAL |
744 | request.from = 0; |
745 | request.len = 0; | |
746 | ||
f35dff7e | 747 | return nbd_co_request(bs, &request, NULL); |
2302c1ca MAL |
748 | } |
749 | ||
f5a5ca79 | 750 | int nbd_client_co_pdiscard(BlockDriverState *bs, int64_t offset, int bytes) |
2302c1ca | 751 | { |
10676b81 | 752 | NBDClientSession *client = nbd_get_client_session(bs); |
ed2dd912 | 753 | NBDRequest request = { |
447e57c3 EB |
754 | .type = NBD_CMD_TRIM, |
755 | .from = offset, | |
f5a5ca79 | 756 | .len = bytes, |
447e57c3 | 757 | }; |
2302c1ca | 758 | |
004a89fc | 759 | if (!(client->info.flags & NBD_FLAG_SEND_TRIM)) { |
2302c1ca MAL |
760 | return 0; |
761 | } | |
2302c1ca | 762 | |
f35dff7e | 763 | return nbd_co_request(bs, &request, NULL); |
2302c1ca MAL |
764 | } |
765 | ||
f53a829b | 766 | void nbd_client_detach_aio_context(BlockDriverState *bs) |
69447cd8 | 767 | { |
ff82911c | 768 | NBDClientSession *client = nbd_get_client_session(bs); |
96d06835 | 769 | qio_channel_detach_aio_context(QIO_CHANNEL(client->ioc)); |
69447cd8 SH |
770 | } |
771 | ||
f53a829b HR |
772 | void nbd_client_attach_aio_context(BlockDriverState *bs, |
773 | AioContext *new_context) | |
69447cd8 | 774 | { |
ff82911c | 775 | NBDClientSession *client = nbd_get_client_session(bs); |
96d06835 | 776 | qio_channel_attach_aio_context(QIO_CHANNEL(client->ioc), new_context); |
ff82911c | 777 | aio_co_schedule(new_context, client->read_reply_co); |
69447cd8 SH |
778 | } |
779 | ||
f53a829b | 780 | void nbd_client_close(BlockDriverState *bs) |
2302c1ca | 781 | { |
10676b81 | 782 | NBDClientSession *client = nbd_get_client_session(bs); |
ed2dd912 | 783 | NBDRequest request = { .type = NBD_CMD_DISC }; |
2302c1ca | 784 | |
064097d9 | 785 | if (client->ioc == NULL) { |
4a41a2d6 SH |
786 | return; |
787 | } | |
788 | ||
1c778ef7 | 789 | nbd_send_request(client->ioc, &request); |
5ad283eb | 790 | |
f53a829b | 791 | nbd_teardown_connection(bs); |
2302c1ca MAL |
792 | } |
793 | ||
75822a12 DB |
794 | int nbd_client_init(BlockDriverState *bs, |
795 | QIOChannelSocket *sioc, | |
796 | const char *export, | |
797 | QCryptoTLSCreds *tlscreds, | |
798 | const char *hostname, | |
799 | Error **errp) | |
2302c1ca | 800 | { |
10676b81 | 801 | NBDClientSession *client = nbd_get_client_session(bs); |
2302c1ca MAL |
802 | int ret; |
803 | ||
804 | /* NBD handshake */ | |
e2bc625f | 805 | logout("session init %s\n", export); |
064097d9 DB |
806 | qio_channel_set_blocking(QIO_CHANNEL(sioc), true, NULL); |
807 | ||
081dd1fe | 808 | client->info.request_sizes = true; |
f140e300 | 809 | client->info.structured_reply = true; |
1c778ef7 | 810 | ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), export, |
75822a12 | 811 | tlscreds, hostname, |
004a89fc | 812 | &client->ioc, &client->info, errp); |
2302c1ca MAL |
813 | if (ret < 0) { |
814 | logout("Failed to negotiate with the NBD server\n"); | |
2302c1ca MAL |
815 | return ret; |
816 | } | |
004a89fc | 817 | if (client->info.flags & NBD_FLAG_SEND_FUA) { |
4df863f3 | 818 | bs->supported_write_flags = BDRV_REQ_FUA; |
169407e1 EB |
819 | bs->supported_zero_flags |= BDRV_REQ_FUA; |
820 | } | |
004a89fc | 821 | if (client->info.flags & NBD_FLAG_SEND_WRITE_ZEROES) { |
169407e1 | 822 | bs->supported_zero_flags |= BDRV_REQ_MAY_UNMAP; |
4df863f3 | 823 | } |
081dd1fe EB |
824 | if (client->info.min_block > bs->bl.request_alignment) { |
825 | bs->bl.request_alignment = client->info.min_block; | |
826 | } | |
2302c1ca MAL |
827 | |
828 | qemu_co_mutex_init(&client->send_mutex); | |
9bc9732f | 829 | qemu_co_queue_init(&client->free_sema); |
064097d9 DB |
830 | client->sioc = sioc; |
831 | object_ref(OBJECT(client->sioc)); | |
f95910fe DB |
832 | |
833 | if (!client->ioc) { | |
834 | client->ioc = QIO_CHANNEL(sioc); | |
835 | object_ref(OBJECT(client->ioc)); | |
836 | } | |
2302c1ca MAL |
837 | |
838 | /* Now that we're connected, set the socket to be non-blocking and | |
839 | * kick the reply mechanism. */ | |
064097d9 | 840 | qio_channel_set_blocking(QIO_CHANNEL(sioc), false, NULL); |
ff82911c | 841 | client->read_reply_co = qemu_coroutine_create(nbd_read_reply_entry, client); |
f53a829b | 842 | nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs)); |
2302c1ca MAL |
843 | |
844 | logout("Established connection with NBD server\n"); | |
845 | return 0; | |
846 | } |