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