nbd/server: Add FLAG_PAYLOAD support to CMD_BLOCK_STATUS
[qemu/ericb.git] / nbd / server.c
blob3030ecfbc5946de2f02f2c93f10c61dc7e758d96
1 /*
2 * Copyright Red Hat
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Server Side
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "block/block_int.h"
23 #include "block/export.h"
24 #include "block/dirty-bitmap.h"
25 #include "qapi/error.h"
26 #include "qemu/queue.h"
27 #include "trace.h"
28 #include "nbd-internal.h"
29 #include "qemu/units.h"
30 #include "qemu/memalign.h"
32 #define NBD_META_ID_BASE_ALLOCATION 0
33 #define NBD_META_ID_ALLOCATION_DEPTH 1
34 /* Dirty bitmaps use 'NBD_META_ID_DIRTY_BITMAP + i', so keep this id last. */
35 #define NBD_META_ID_DIRTY_BITMAP 2
38 * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
39 * constant. If an increase is needed, note that the NBD protocol
40 * recommends no larger than 32 mb, so that the client won't consider
41 * the reply as a denial of service attack.
43 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
45 static int system_errno_to_nbd_errno(int err)
47 switch (err) {
48 case 0:
49 return NBD_SUCCESS;
50 case EPERM:
51 case EROFS:
52 return NBD_EPERM;
53 case EIO:
54 return NBD_EIO;
55 case ENOMEM:
56 return NBD_ENOMEM;
57 #ifdef EDQUOT
58 case EDQUOT:
59 #endif
60 case EFBIG:
61 case ENOSPC:
62 return NBD_ENOSPC;
63 case EOVERFLOW:
64 return NBD_EOVERFLOW;
65 case ENOTSUP:
66 #if ENOTSUP != EOPNOTSUPP
67 case EOPNOTSUPP:
68 #endif
69 return NBD_ENOTSUP;
70 case ESHUTDOWN:
71 return NBD_ESHUTDOWN;
72 case EINVAL:
73 default:
74 return NBD_EINVAL;
78 /* Definitions for opaque data types */
80 typedef struct NBDRequestData NBDRequestData;
82 struct NBDRequestData {
83 NBDClient *client;
84 uint8_t *data;
85 bool complete;
88 struct NBDExport {
89 BlockExport common;
91 char *name;
92 char *description;
93 uint64_t size;
94 uint16_t nbdflags;
95 QTAILQ_HEAD(, NBDClient) clients;
96 QTAILQ_ENTRY(NBDExport) next;
98 BlockBackend *eject_notifier_blk;
99 Notifier eject_notifier;
101 bool allocation_depth;
102 BdrvDirtyBitmap **export_bitmaps;
103 size_t nr_export_bitmaps;
106 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
109 * NBDMetaContexts represents a list of meta contexts in use,
110 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
111 * NBD_OPT_LIST_META_CONTEXT.
113 struct NBDMetaContexts {
114 const NBDExport *exp; /* associated export */
115 size_t count; /* number of negotiated contexts */
116 bool base_allocation; /* export base:allocation context (block status) */
117 bool allocation_depth; /* export qemu:allocation-depth */
118 bool *bitmaps; /*
119 * export qemu:dirty-bitmap:<export bitmap name>,
120 * sized by exp->nr_export_bitmaps
124 struct NBDClient {
125 int refcount;
126 void (*close_fn)(NBDClient *client, bool negotiated);
128 NBDExport *exp;
129 QCryptoTLSCreds *tlscreds;
130 char *tlsauthz;
131 QIOChannelSocket *sioc; /* The underlying data channel */
132 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
134 Coroutine *recv_coroutine;
136 CoMutex send_lock;
137 Coroutine *send_coroutine;
139 bool read_yielding;
140 bool quiescing;
142 QTAILQ_ENTRY(NBDClient) next;
143 int nb_requests;
144 bool closing;
146 uint32_t check_align; /* If non-zero, check for aligned client requests */
148 NBDMode mode;
149 NBDMetaContexts contexts; /* Negotiated meta contexts */
151 uint32_t opt; /* Current option being negotiated */
152 uint32_t optlen; /* remaining length of data in ioc for the option being
153 negotiated now */
156 static void nbd_client_receive_next_request(NBDClient *client);
158 /* Basic flow for negotiation
160 Server Client
161 Negotiate
165 Server Client
166 Negotiate #1
167 Option
168 Negotiate #2
170 ----
172 followed by
174 Server Client
175 Request
176 Response
177 Request
178 Response
181 Request (type == 2)
185 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
186 uint32_t type, uint32_t length)
188 stq_be_p(&rep->magic, NBD_REP_MAGIC);
189 stl_be_p(&rep->option, option);
190 stl_be_p(&rep->type, type);
191 stl_be_p(&rep->length, length);
194 /* Send a reply header, including length, but no payload.
195 * Return -errno on error, 0 on success. */
196 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
197 uint32_t len, Error **errp)
199 NBDOptionReply rep;
201 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
202 type, nbd_rep_lookup(type), len);
204 assert(len < NBD_MAX_BUFFER_SIZE);
206 set_be_option_rep(&rep, client->opt, type, len);
207 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
210 /* Send a reply header with default 0 length.
211 * Return -errno on error, 0 on success. */
212 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
213 Error **errp)
215 return nbd_negotiate_send_rep_len(client, type, 0, errp);
218 /* Send an error reply.
219 * Return -errno on error, 0 on success. */
220 static int G_GNUC_PRINTF(4, 0)
221 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
222 Error **errp, const char *fmt, va_list va)
224 ERRP_GUARD();
225 g_autofree char *msg = NULL;
226 int ret;
227 size_t len;
229 msg = g_strdup_vprintf(fmt, va);
230 len = strlen(msg);
231 assert(len < NBD_MAX_STRING_SIZE);
232 trace_nbd_negotiate_send_rep_err(msg);
233 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
234 if (ret < 0) {
235 return ret;
237 if (nbd_write(client->ioc, msg, len, errp) < 0) {
238 error_prepend(errp, "write failed (error message): ");
239 return -EIO;
242 return 0;
246 * Return a malloc'd copy of @name suitable for use in an error reply.
248 static char *
249 nbd_sanitize_name(const char *name)
251 if (strnlen(name, 80) < 80) {
252 return g_strdup(name);
254 /* XXX Should we also try to sanitize any control characters? */
255 return g_strdup_printf("%.80s...", name);
258 /* Send an error reply.
259 * Return -errno on error, 0 on success. */
260 static int G_GNUC_PRINTF(4, 5)
261 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
262 Error **errp, const char *fmt, ...)
264 va_list va;
265 int ret;
267 va_start(va, fmt);
268 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
269 va_end(va);
270 return ret;
273 /* Drop remainder of the current option, and send a reply with the
274 * given error type and message. Return -errno on read or write
275 * failure; or 0 if connection is still live. */
276 static int G_GNUC_PRINTF(4, 0)
277 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
278 const char *fmt, va_list va)
280 int ret = nbd_drop(client->ioc, client->optlen, errp);
282 client->optlen = 0;
283 if (!ret) {
284 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
286 return ret;
289 static int G_GNUC_PRINTF(4, 5)
290 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
291 const char *fmt, ...)
293 int ret;
294 va_list va;
296 va_start(va, fmt);
297 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
298 va_end(va);
300 return ret;
303 static int G_GNUC_PRINTF(3, 4)
304 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
306 int ret;
307 va_list va;
309 va_start(va, fmt);
310 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
311 va_end(va);
313 return ret;
316 /* Read size bytes from the unparsed payload of the current option.
317 * If @check_nul, require that no NUL bytes appear in buffer.
318 * Return -errno on I/O error, 0 if option was completely handled by
319 * sending a reply about inconsistent lengths, or 1 on success. */
320 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
321 bool check_nul, Error **errp)
323 if (size > client->optlen) {
324 return nbd_opt_invalid(client, errp,
325 "Inconsistent lengths in option %s",
326 nbd_opt_lookup(client->opt));
328 client->optlen -= size;
329 if (qio_channel_read_all(client->ioc, buffer, size, errp) < 0) {
330 return -EIO;
333 if (check_nul && strnlen(buffer, size) != size) {
334 return nbd_opt_invalid(client, errp,
335 "Unexpected embedded NUL in option %s",
336 nbd_opt_lookup(client->opt));
338 return 1;
341 /* Drop size bytes from the unparsed payload of the current option.
342 * Return -errno on I/O error, 0 if option was completely handled by
343 * sending a reply about inconsistent lengths, or 1 on success. */
344 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
346 if (size > client->optlen) {
347 return nbd_opt_invalid(client, errp,
348 "Inconsistent lengths in option %s",
349 nbd_opt_lookup(client->opt));
351 client->optlen -= size;
352 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
355 /* nbd_opt_read_name
357 * Read a string with the format:
358 * uint32_t len (<= NBD_MAX_STRING_SIZE)
359 * len bytes string (not 0-terminated)
361 * On success, @name will be allocated.
362 * If @length is non-null, it will be set to the actual string length.
364 * Return -errno on I/O error, 0 if option was completely handled by
365 * sending a reply about inconsistent lengths, or 1 on success.
367 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
368 Error **errp)
370 int ret;
371 uint32_t len;
372 g_autofree char *local_name = NULL;
374 *name = NULL;
375 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
376 if (ret <= 0) {
377 return ret;
379 len = cpu_to_be32(len);
381 if (len > NBD_MAX_STRING_SIZE) {
382 return nbd_opt_invalid(client, errp,
383 "Invalid name length: %" PRIu32, len);
386 local_name = g_malloc(len + 1);
387 ret = nbd_opt_read(client, local_name, len, true, errp);
388 if (ret <= 0) {
389 return ret;
391 local_name[len] = '\0';
393 if (length) {
394 *length = len;
396 *name = g_steal_pointer(&local_name);
398 return 1;
401 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
402 * Return -errno on error, 0 on success. */
403 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
404 Error **errp)
406 ERRP_GUARD();
407 size_t name_len, desc_len;
408 uint32_t len;
409 const char *name = exp->name ? exp->name : "";
410 const char *desc = exp->description ? exp->description : "";
411 QIOChannel *ioc = client->ioc;
412 int ret;
414 trace_nbd_negotiate_send_rep_list(name, desc);
415 name_len = strlen(name);
416 desc_len = strlen(desc);
417 assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
418 len = name_len + desc_len + sizeof(len);
419 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
420 if (ret < 0) {
421 return ret;
424 len = cpu_to_be32(name_len);
425 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
426 error_prepend(errp, "write failed (name length): ");
427 return -EINVAL;
430 if (nbd_write(ioc, name, name_len, errp) < 0) {
431 error_prepend(errp, "write failed (name buffer): ");
432 return -EINVAL;
435 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
436 error_prepend(errp, "write failed (description buffer): ");
437 return -EINVAL;
440 return 0;
443 /* Process the NBD_OPT_LIST command, with a potential series of replies.
444 * Return -errno on error, 0 on success. */
445 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
447 NBDExport *exp;
448 assert(client->opt == NBD_OPT_LIST);
450 /* For each export, send a NBD_REP_SERVER reply. */
451 QTAILQ_FOREACH(exp, &exports, next) {
452 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
453 return -EINVAL;
456 /* Finish with a NBD_REP_ACK. */
457 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
460 static void nbd_check_meta_export(NBDClient *client, NBDExport *exp)
462 if (exp != client->contexts.exp) {
463 client->contexts.count = 0;
467 /* Send a reply to NBD_OPT_EXPORT_NAME.
468 * Return -errno on error, 0 on success. */
469 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
470 Error **errp)
472 ERRP_GUARD();
473 g_autofree char *name = NULL;
474 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
475 size_t len;
476 int ret;
477 uint16_t myflags;
479 /* Client sends:
480 [20 .. xx] export name (length bytes)
481 Server replies:
482 [ 0 .. 7] size
483 [ 8 .. 9] export flags
484 [10 .. 133] reserved (0) [unless no_zeroes]
486 trace_nbd_negotiate_handle_export_name();
487 if (client->mode >= NBD_MODE_EXTENDED) {
488 error_setg(errp, "Extended headers already negotiated");
489 return -EINVAL;
491 if (client->optlen > NBD_MAX_STRING_SIZE) {
492 error_setg(errp, "Bad length received");
493 return -EINVAL;
495 name = g_malloc(client->optlen + 1);
496 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
497 return -EIO;
499 name[client->optlen] = '\0';
500 client->optlen = 0;
502 trace_nbd_negotiate_handle_export_name_request(name);
504 client->exp = nbd_export_find(name);
505 if (!client->exp) {
506 error_setg(errp, "export not found");
507 return -EINVAL;
509 nbd_check_meta_export(client, client->exp);
511 myflags = client->exp->nbdflags;
512 if (client->mode >= NBD_MODE_STRUCTURED) {
513 myflags |= NBD_FLAG_SEND_DF;
515 if (client->mode >= NBD_MODE_EXTENDED && client->contexts.count) {
516 myflags |= NBD_FLAG_BLOCK_STAT_PAYLOAD;
518 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
519 stq_be_p(buf, client->exp->size);
520 stw_be_p(buf + 8, myflags);
521 len = no_zeroes ? 10 : sizeof(buf);
522 ret = nbd_write(client->ioc, buf, len, errp);
523 if (ret < 0) {
524 error_prepend(errp, "write failed: ");
525 return ret;
528 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
529 blk_exp_ref(&client->exp->common);
531 return 0;
534 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
535 * The buffer does NOT include the info type prefix.
536 * Return -errno on error, 0 if ready to send more. */
537 static int nbd_negotiate_send_info(NBDClient *client,
538 uint16_t info, uint32_t length, void *buf,
539 Error **errp)
541 int rc;
543 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
544 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
545 sizeof(info) + length, errp);
546 if (rc < 0) {
547 return rc;
549 info = cpu_to_be16(info);
550 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
551 return -EIO;
553 if (nbd_write(client->ioc, buf, length, errp) < 0) {
554 return -EIO;
556 return 0;
559 /* nbd_reject_length: Handle any unexpected payload.
560 * @fatal requests that we quit talking to the client, even if we are able
561 * to successfully send an error reply.
562 * Return:
563 * -errno transmission error occurred or @fatal was requested, errp is set
564 * 0 error message successfully sent to client, errp is not set
566 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
568 int ret;
570 assert(client->optlen);
571 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
572 nbd_opt_lookup(client->opt));
573 if (fatal && !ret) {
574 error_setg(errp, "option '%s' has unexpected length",
575 nbd_opt_lookup(client->opt));
576 return -EINVAL;
578 return ret;
581 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
582 * Return -errno on error, 0 if ready for next option, and 1 to move
583 * into transmission phase. */
584 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
586 int rc;
587 g_autofree char *name = NULL;
588 NBDExport *exp;
589 uint16_t requests;
590 uint16_t request;
591 uint32_t namelen = 0;
592 bool sendname = false;
593 bool blocksize = false;
594 uint32_t sizes[3];
595 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
596 uint32_t check_align = 0;
597 uint16_t myflags;
599 /* Client sends:
600 4 bytes: L, name length (can be 0)
601 L bytes: export name
602 2 bytes: N, number of requests (can be 0)
603 N * 2 bytes: N requests
605 rc = nbd_opt_read_name(client, &name, &namelen, errp);
606 if (rc <= 0) {
607 return rc;
609 trace_nbd_negotiate_handle_export_name_request(name);
611 rc = nbd_opt_read(client, &requests, sizeof(requests), false, errp);
612 if (rc <= 0) {
613 return rc;
615 requests = be16_to_cpu(requests);
616 trace_nbd_negotiate_handle_info_requests(requests);
617 while (requests--) {
618 rc = nbd_opt_read(client, &request, sizeof(request), false, errp);
619 if (rc <= 0) {
620 return rc;
622 request = be16_to_cpu(request);
623 trace_nbd_negotiate_handle_info_request(request,
624 nbd_info_lookup(request));
625 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
626 * everything else is either a request we don't know or
627 * something we send regardless of request */
628 switch (request) {
629 case NBD_INFO_NAME:
630 sendname = true;
631 break;
632 case NBD_INFO_BLOCK_SIZE:
633 blocksize = true;
634 break;
637 if (client->optlen) {
638 return nbd_reject_length(client, false, errp);
641 exp = nbd_export_find(name);
642 if (!exp) {
643 g_autofree char *sane_name = nbd_sanitize_name(name);
645 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
646 errp, "export '%s' not present",
647 sane_name);
649 if (client->opt == NBD_OPT_GO) {
650 nbd_check_meta_export(client, exp);
653 /* Don't bother sending NBD_INFO_NAME unless client requested it */
654 if (sendname) {
655 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
656 errp);
657 if (rc < 0) {
658 return rc;
662 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
663 * client request */
664 if (exp->description) {
665 size_t len = strlen(exp->description);
667 assert(len <= NBD_MAX_STRING_SIZE);
668 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
669 len, exp->description, errp);
670 if (rc < 0) {
671 return rc;
675 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
676 * according to whether the client requested it, and according to
677 * whether this is OPT_INFO or OPT_GO. */
678 /* minimum - 1 for back-compat, or actual if client will obey it. */
679 if (client->opt == NBD_OPT_INFO || blocksize) {
680 check_align = sizes[0] = blk_get_request_alignment(exp->common.blk);
681 } else {
682 sizes[0] = 1;
684 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
685 /* preferred - Hard-code to 4096 for now.
686 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
687 sizes[1] = MAX(4096, sizes[0]);
688 /* maximum - At most 32M, but smaller as appropriate. */
689 sizes[2] = MIN(blk_get_max_transfer(exp->common.blk), NBD_MAX_BUFFER_SIZE);
690 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
691 sizes[0] = cpu_to_be32(sizes[0]);
692 sizes[1] = cpu_to_be32(sizes[1]);
693 sizes[2] = cpu_to_be32(sizes[2]);
694 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
695 sizeof(sizes), sizes, errp);
696 if (rc < 0) {
697 return rc;
700 /* Send NBD_INFO_EXPORT always */
701 myflags = exp->nbdflags;
702 if (client->mode >= NBD_MODE_STRUCTURED) {
703 myflags |= NBD_FLAG_SEND_DF;
705 if (client->mode >= NBD_MODE_EXTENDED &&
706 (client->contexts.count || client->opt == NBD_OPT_INFO)) {
707 myflags |= NBD_FLAG_BLOCK_STAT_PAYLOAD;
709 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
710 stq_be_p(buf, exp->size);
711 stw_be_p(buf + 8, myflags);
712 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
713 sizeof(buf), buf, errp);
714 if (rc < 0) {
715 return rc;
719 * If the client is just asking for NBD_OPT_INFO, but forgot to
720 * request block sizes in a situation that would impact
721 * performance, then return an error. But for NBD_OPT_GO, we
722 * tolerate all clients, regardless of alignments.
724 if (client->opt == NBD_OPT_INFO && !blocksize &&
725 blk_get_request_alignment(exp->common.blk) > 1) {
726 return nbd_negotiate_send_rep_err(client,
727 NBD_REP_ERR_BLOCK_SIZE_REQD,
728 errp,
729 "request NBD_INFO_BLOCK_SIZE to "
730 "use this export");
733 /* Final reply */
734 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
735 if (rc < 0) {
736 return rc;
739 if (client->opt == NBD_OPT_GO) {
740 client->exp = exp;
741 client->check_align = check_align;
742 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
743 blk_exp_ref(&client->exp->common);
744 rc = 1;
746 return rc;
750 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
751 * new channel for all further (now-encrypted) communication. */
752 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
753 Error **errp)
755 QIOChannel *ioc;
756 QIOChannelTLS *tioc;
757 struct NBDTLSHandshakeData data = { 0 };
759 assert(client->opt == NBD_OPT_STARTTLS);
761 trace_nbd_negotiate_handle_starttls();
762 ioc = client->ioc;
764 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
765 return NULL;
768 tioc = qio_channel_tls_new_server(ioc,
769 client->tlscreds,
770 client->tlsauthz,
771 errp);
772 if (!tioc) {
773 return NULL;
776 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
777 trace_nbd_negotiate_handle_starttls_handshake();
778 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
779 qio_channel_tls_handshake(tioc,
780 nbd_tls_handshake,
781 &data,
782 NULL,
783 NULL);
785 if (!data.complete) {
786 g_main_loop_run(data.loop);
788 g_main_loop_unref(data.loop);
789 if (data.error) {
790 object_unref(OBJECT(tioc));
791 error_propagate(errp, data.error);
792 return NULL;
795 return QIO_CHANNEL(tioc);
798 /* nbd_negotiate_send_meta_context
800 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
802 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
804 static int nbd_negotiate_send_meta_context(NBDClient *client,
805 const char *context,
806 uint32_t context_id,
807 Error **errp)
809 NBDOptionReplyMetaContext opt;
810 struct iovec iov[] = {
811 {.iov_base = &opt, .iov_len = sizeof(opt)},
812 {.iov_base = (void *)context, .iov_len = strlen(context)}
815 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
816 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
817 context_id = 0;
820 trace_nbd_negotiate_meta_query_reply(context, context_id);
821 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
822 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
823 stl_be_p(&opt.context_id, context_id);
825 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
829 * Return true if @query matches @pattern, or if @query is empty when
830 * the @client is performing _LIST_.
832 static bool nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
833 const char *query)
835 if (!*query) {
836 trace_nbd_negotiate_meta_query_parse("empty");
837 return client->opt == NBD_OPT_LIST_META_CONTEXT;
839 if (strcmp(query, pattern) == 0) {
840 trace_nbd_negotiate_meta_query_parse(pattern);
841 return true;
843 trace_nbd_negotiate_meta_query_skip("pattern not matched");
844 return false;
848 * Return true and adjust @str in place if it begins with @prefix.
850 static bool nbd_strshift(const char **str, const char *prefix)
852 size_t len = strlen(prefix);
854 if (strncmp(*str, prefix, len) == 0) {
855 *str += len;
856 return true;
858 return false;
861 /* nbd_meta_base_query
863 * Handle queries to 'base' namespace. For now, only the base:allocation
864 * context is available. Return true if @query has been handled.
866 static bool nbd_meta_base_query(NBDClient *client, NBDMetaContexts *meta,
867 const char *query)
869 if (!nbd_strshift(&query, "base:")) {
870 return false;
872 trace_nbd_negotiate_meta_query_parse("base:");
874 if (nbd_meta_empty_or_pattern(client, "allocation", query)) {
875 meta->base_allocation = true;
877 return true;
880 /* nbd_meta_qemu_query
882 * Handle queries to 'qemu' namespace. For now, only the qemu:dirty-bitmap:
883 * and qemu:allocation-depth contexts are available. Return true if @query
884 * has been handled.
886 static bool nbd_meta_qemu_query(NBDClient *client, NBDMetaContexts *meta,
887 const char *query)
889 size_t i;
891 if (!nbd_strshift(&query, "qemu:")) {
892 return false;
894 trace_nbd_negotiate_meta_query_parse("qemu:");
896 if (!*query) {
897 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
898 meta->allocation_depth = meta->exp->allocation_depth;
899 if (meta->exp->nr_export_bitmaps) {
900 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
903 trace_nbd_negotiate_meta_query_parse("empty");
904 return true;
907 if (strcmp(query, "allocation-depth") == 0) {
908 trace_nbd_negotiate_meta_query_parse("allocation-depth");
909 meta->allocation_depth = meta->exp->allocation_depth;
910 return true;
913 if (nbd_strshift(&query, "dirty-bitmap:")) {
914 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
915 if (!*query) {
916 if (client->opt == NBD_OPT_LIST_META_CONTEXT &&
917 meta->exp->nr_export_bitmaps) {
918 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
920 trace_nbd_negotiate_meta_query_parse("empty");
921 return true;
924 for (i = 0; i < meta->exp->nr_export_bitmaps; i++) {
925 const char *bm_name;
927 bm_name = bdrv_dirty_bitmap_name(meta->exp->export_bitmaps[i]);
928 if (strcmp(bm_name, query) == 0) {
929 meta->bitmaps[i] = true;
930 trace_nbd_negotiate_meta_query_parse(query);
931 return true;
934 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap match");
935 return true;
938 trace_nbd_negotiate_meta_query_skip("unknown qemu context");
939 return true;
942 /* nbd_negotiate_meta_query
944 * Parse namespace name and call corresponding function to parse body of the
945 * query.
947 * The only supported namespaces are 'base' and 'qemu'.
949 * Return -errno on I/O error, 0 if option was completely handled by
950 * sending a reply about inconsistent lengths, or 1 on success. */
951 static int nbd_negotiate_meta_query(NBDClient *client,
952 NBDMetaContexts *meta, Error **errp)
954 int ret;
955 g_autofree char *query = NULL;
956 uint32_t len;
958 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
959 if (ret <= 0) {
960 return ret;
962 len = cpu_to_be32(len);
964 if (len > NBD_MAX_STRING_SIZE) {
965 trace_nbd_negotiate_meta_query_skip("length too long");
966 return nbd_opt_skip(client, len, errp);
969 query = g_malloc(len + 1);
970 ret = nbd_opt_read(client, query, len, true, errp);
971 if (ret <= 0) {
972 return ret;
974 query[len] = '\0';
976 if (nbd_meta_base_query(client, meta, query)) {
977 return 1;
979 if (nbd_meta_qemu_query(client, meta, query)) {
980 return 1;
983 trace_nbd_negotiate_meta_query_skip("unknown namespace");
984 return 1;
987 /* nbd_negotiate_meta_queries
988 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
990 * Return -errno on I/O error, or 0 if option was completely handled. */
991 static int nbd_negotiate_meta_queries(NBDClient *client, Error **errp)
993 int ret;
994 g_autofree char *export_name = NULL;
995 /* Mark unused to work around https://bugs.llvm.org/show_bug.cgi?id=3888 */
996 g_autofree G_GNUC_UNUSED bool *bitmaps = NULL;
997 NBDMetaContexts local_meta = {0};
998 NBDMetaContexts *meta;
999 uint32_t nb_queries;
1000 size_t i;
1001 size_t count = 0;
1003 if (client->opt == NBD_OPT_SET_META_CONTEXT &&
1004 client->mode < NBD_MODE_STRUCTURED) {
1005 return nbd_opt_invalid(client, errp,
1006 "request option '%s' when structured reply "
1007 "is not negotiated",
1008 nbd_opt_lookup(client->opt));
1011 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1012 /* Only change the caller's meta on SET. */
1013 meta = &local_meta;
1014 } else {
1015 meta = &client->contexts;
1018 g_free(meta->bitmaps);
1019 memset(meta, 0, sizeof(*meta));
1021 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1022 if (ret <= 0) {
1023 return ret;
1026 meta->exp = nbd_export_find(export_name);
1027 if (meta->exp == NULL) {
1028 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1030 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1031 "export '%s' not present", sane_name);
1033 meta->bitmaps = g_new0(bool, meta->exp->nr_export_bitmaps);
1034 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1035 bitmaps = meta->bitmaps;
1038 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), false, errp);
1039 if (ret <= 0) {
1040 return ret;
1042 nb_queries = cpu_to_be32(nb_queries);
1043 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1044 export_name, nb_queries);
1046 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1047 /* enable all known contexts */
1048 meta->base_allocation = true;
1049 meta->allocation_depth = meta->exp->allocation_depth;
1050 if (meta->exp->nr_export_bitmaps) {
1051 memset(meta->bitmaps, 1, meta->exp->nr_export_bitmaps);
1053 } else {
1054 for (i = 0; i < nb_queries; ++i) {
1055 ret = nbd_negotiate_meta_query(client, meta, errp);
1056 if (ret <= 0) {
1057 return ret;
1062 if (meta->base_allocation) {
1063 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1064 NBD_META_ID_BASE_ALLOCATION,
1065 errp);
1066 if (ret < 0) {
1067 return ret;
1069 count++;
1072 if (meta->allocation_depth) {
1073 ret = nbd_negotiate_send_meta_context(client, "qemu:allocation-depth",
1074 NBD_META_ID_ALLOCATION_DEPTH,
1075 errp);
1076 if (ret < 0) {
1077 return ret;
1079 count++;
1082 for (i = 0; i < meta->exp->nr_export_bitmaps; i++) {
1083 const char *bm_name;
1084 g_autofree char *context = NULL;
1086 if (!meta->bitmaps[i]) {
1087 continue;
1090 bm_name = bdrv_dirty_bitmap_name(meta->exp->export_bitmaps[i]);
1091 context = g_strdup_printf("qemu:dirty-bitmap:%s", bm_name);
1093 ret = nbd_negotiate_send_meta_context(client, context,
1094 NBD_META_ID_DIRTY_BITMAP + i,
1095 errp);
1096 if (ret < 0) {
1097 return ret;
1099 count++;
1102 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1103 if (ret == 0) {
1104 meta->count = count;
1107 return ret;
1110 /* nbd_negotiate_options
1111 * Process all NBD_OPT_* client option commands, during fixed newstyle
1112 * negotiation.
1113 * Return:
1114 * -errno on error, errp is set
1115 * 0 on successful negotiation, errp is not set
1116 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1117 * errp is not set
1119 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1121 uint32_t flags;
1122 bool fixedNewstyle = false;
1123 bool no_zeroes = false;
1125 /* Client sends:
1126 [ 0 .. 3] client flags
1128 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1129 [ 0 .. 7] NBD_OPTS_MAGIC
1130 [ 8 .. 11] NBD option
1131 [12 .. 15] Data length
1132 ... Rest of request
1134 [ 0 .. 7] NBD_OPTS_MAGIC
1135 [ 8 .. 11] Second NBD option
1136 [12 .. 15] Data length
1137 ... Rest of request
1140 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1141 return -EIO;
1143 client->mode = NBD_MODE_EXPORT_NAME;
1144 trace_nbd_negotiate_options_flags(flags);
1145 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1146 fixedNewstyle = true;
1147 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1148 client->mode = NBD_MODE_SIMPLE;
1150 if (flags & NBD_FLAG_C_NO_ZEROES) {
1151 no_zeroes = true;
1152 flags &= ~NBD_FLAG_C_NO_ZEROES;
1154 if (flags != 0) {
1155 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1156 return -EINVAL;
1159 while (1) {
1160 int ret;
1161 uint32_t option, length;
1162 uint64_t magic;
1164 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1165 return -EINVAL;
1167 trace_nbd_negotiate_options_check_magic(magic);
1168 if (magic != NBD_OPTS_MAGIC) {
1169 error_setg(errp, "Bad magic received");
1170 return -EINVAL;
1173 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1174 return -EINVAL;
1176 client->opt = option;
1178 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1179 return -EINVAL;
1181 assert(!client->optlen);
1182 client->optlen = length;
1184 if (length > NBD_MAX_BUFFER_SIZE) {
1185 error_setg(errp, "len (%" PRIu32 ") is larger than max len (%u)",
1186 length, NBD_MAX_BUFFER_SIZE);
1187 return -EINVAL;
1190 trace_nbd_negotiate_options_check_option(option,
1191 nbd_opt_lookup(option));
1192 if (client->tlscreds &&
1193 client->ioc == (QIOChannel *)client->sioc) {
1194 QIOChannel *tioc;
1195 if (!fixedNewstyle) {
1196 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1197 return -EINVAL;
1199 switch (option) {
1200 case NBD_OPT_STARTTLS:
1201 if (length) {
1202 /* Unconditionally drop the connection if the client
1203 * can't start a TLS negotiation correctly */
1204 return nbd_reject_length(client, true, errp);
1206 tioc = nbd_negotiate_handle_starttls(client, errp);
1207 if (!tioc) {
1208 return -EIO;
1210 ret = 0;
1211 object_unref(OBJECT(client->ioc));
1212 client->ioc = tioc;
1213 break;
1215 case NBD_OPT_EXPORT_NAME:
1216 /* No way to return an error to client, so drop connection */
1217 error_setg(errp, "Option 0x%x not permitted before TLS",
1218 option);
1219 return -EINVAL;
1221 default:
1222 /* Let the client keep trying, unless they asked to
1223 * quit. Always try to give an error back to the
1224 * client; but when replying to OPT_ABORT, be aware
1225 * that the client may hang up before receiving the
1226 * error, in which case we are fine ignoring the
1227 * resulting EPIPE. */
1228 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1229 option == NBD_OPT_ABORT ? NULL : errp,
1230 "Option 0x%" PRIx32
1231 " not permitted before TLS", option);
1232 if (option == NBD_OPT_ABORT) {
1233 return 1;
1235 break;
1237 } else if (fixedNewstyle) {
1238 switch (option) {
1239 case NBD_OPT_LIST:
1240 if (length) {
1241 ret = nbd_reject_length(client, false, errp);
1242 } else {
1243 ret = nbd_negotiate_handle_list(client, errp);
1245 break;
1247 case NBD_OPT_ABORT:
1248 /* NBD spec says we must try to reply before
1249 * disconnecting, but that we must also tolerate
1250 * guests that don't wait for our reply. */
1251 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1252 return 1;
1254 case NBD_OPT_EXPORT_NAME:
1255 return nbd_negotiate_handle_export_name(client, no_zeroes,
1256 errp);
1258 case NBD_OPT_INFO:
1259 case NBD_OPT_GO:
1260 ret = nbd_negotiate_handle_info(client, errp);
1261 if (ret == 1) {
1262 assert(option == NBD_OPT_GO);
1263 return 0;
1265 break;
1267 case NBD_OPT_STARTTLS:
1268 if (length) {
1269 ret = nbd_reject_length(client, false, errp);
1270 } else if (client->tlscreds) {
1271 ret = nbd_negotiate_send_rep_err(client,
1272 NBD_REP_ERR_INVALID, errp,
1273 "TLS already enabled");
1274 } else {
1275 ret = nbd_negotiate_send_rep_err(client,
1276 NBD_REP_ERR_POLICY, errp,
1277 "TLS not configured");
1279 break;
1281 case NBD_OPT_STRUCTURED_REPLY:
1282 if (length) {
1283 ret = nbd_reject_length(client, false, errp);
1284 } else if (client->mode >= NBD_MODE_EXTENDED) {
1285 ret = nbd_negotiate_send_rep_err(
1286 client, NBD_REP_ERR_EXT_HEADER_REQD, errp,
1287 "extended headers already negotiated");
1288 } else if (client->mode >= NBD_MODE_STRUCTURED) {
1289 ret = nbd_negotiate_send_rep_err(
1290 client, NBD_REP_ERR_INVALID, errp,
1291 "structured reply already negotiated");
1292 } else {
1293 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1294 client->mode = NBD_MODE_STRUCTURED;
1296 break;
1298 case NBD_OPT_LIST_META_CONTEXT:
1299 case NBD_OPT_SET_META_CONTEXT:
1300 ret = nbd_negotiate_meta_queries(client, errp);
1301 break;
1303 case NBD_OPT_EXTENDED_HEADERS:
1304 if (length) {
1305 ret = nbd_reject_length(client, false, errp);
1306 } else if (client->mode >= NBD_MODE_EXTENDED) {
1307 ret = nbd_negotiate_send_rep_err(
1308 client, NBD_REP_ERR_INVALID, errp,
1309 "extended headers already negotiated");
1310 } else {
1311 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1312 client->mode = NBD_MODE_EXTENDED;
1314 break;
1316 default:
1317 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1318 "Unsupported option %" PRIu32 " (%s)",
1319 option, nbd_opt_lookup(option));
1320 break;
1322 } else {
1324 * If broken new-style we should drop the connection
1325 * for anything except NBD_OPT_EXPORT_NAME
1327 switch (option) {
1328 case NBD_OPT_EXPORT_NAME:
1329 return nbd_negotiate_handle_export_name(client, no_zeroes,
1330 errp);
1332 default:
1333 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1334 option, nbd_opt_lookup(option));
1335 return -EINVAL;
1338 if (ret < 0) {
1339 return ret;
1344 /* nbd_negotiate
1345 * Return:
1346 * -errno on error, errp is set
1347 * 0 on successful negotiation, errp is not set
1348 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1349 * errp is not set
1351 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1353 ERRP_GUARD();
1354 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1355 int ret;
1357 /* Old style negotiation header, no room for options
1358 [ 0 .. 7] passwd ("NBDMAGIC")
1359 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1360 [16 .. 23] size
1361 [24 .. 27] export flags (zero-extended)
1362 [28 .. 151] reserved (0)
1364 New style negotiation header, client can send options
1365 [ 0 .. 7] passwd ("NBDMAGIC")
1366 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1367 [16 .. 17] server flags (0)
1368 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1371 qio_channel_set_blocking(client->ioc, false, NULL);
1373 trace_nbd_negotiate_begin();
1374 memcpy(buf, "NBDMAGIC", 8);
1376 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1377 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1379 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1380 error_prepend(errp, "write failed: ");
1381 return -EINVAL;
1383 ret = nbd_negotiate_options(client, errp);
1384 if (ret != 0) {
1385 if (ret < 0) {
1386 error_prepend(errp, "option negotiation failed: ");
1388 return ret;
1391 /* Attach the channel to the same AioContext as the export */
1392 if (client->exp && client->exp->common.ctx) {
1393 qio_channel_attach_aio_context(client->ioc, client->exp->common.ctx);
1396 assert(!client->optlen);
1397 trace_nbd_negotiate_success();
1399 return 0;
1402 /* nbd_read_eof
1403 * Tries to read @size bytes from @ioc. This is a local implementation of
1404 * qio_channel_readv_all_eof. We have it here because we need it to be
1405 * interruptible and to know when the coroutine is yielding.
1406 * Returns 1 on success
1407 * 0 on eof, when no data was read (errp is not set)
1408 * negative errno on failure (errp is set)
1410 static inline int coroutine_fn
1411 nbd_read_eof(NBDClient *client, void *buffer, size_t size, Error **errp)
1413 bool partial = false;
1415 assert(size);
1416 while (size > 0) {
1417 struct iovec iov = { .iov_base = buffer, .iov_len = size };
1418 ssize_t len;
1420 len = qio_channel_readv(client->ioc, &iov, 1, errp);
1421 if (len == QIO_CHANNEL_ERR_BLOCK) {
1422 client->read_yielding = true;
1423 qio_channel_yield(client->ioc, G_IO_IN);
1424 client->read_yielding = false;
1425 if (client->quiescing) {
1426 return -EAGAIN;
1428 continue;
1429 } else if (len < 0) {
1430 return -EIO;
1431 } else if (len == 0) {
1432 if (partial) {
1433 error_setg(errp,
1434 "Unexpected end-of-file before all bytes were read");
1435 return -EIO;
1436 } else {
1437 return 0;
1441 partial = true;
1442 size -= len;
1443 buffer = (uint8_t *) buffer + len;
1445 return 1;
1448 static int coroutine_fn nbd_receive_request(NBDClient *client, NBDRequest *request,
1449 Error **errp)
1451 uint8_t buf[NBD_EXTENDED_REQUEST_SIZE];
1452 uint32_t magic, expect;
1453 int ret;
1454 size_t size = client->mode >= NBD_MODE_EXTENDED ?
1455 NBD_EXTENDED_REQUEST_SIZE : NBD_REQUEST_SIZE;
1457 ret = nbd_read_eof(client, buf, size, errp);
1458 if (ret < 0) {
1459 return ret;
1461 if (ret == 0) {
1462 return -EIO;
1466 * Compact request
1467 * [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1468 * [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1469 * [ 6 .. 7] type (NBD_CMD_READ, ...)
1470 * [ 8 .. 15] cookie
1471 * [16 .. 23] from
1472 * [24 .. 27] len
1473 * Extended request
1474 * [ 0 .. 3] magic (NBD_EXTENDED_REQUEST_MAGIC)
1475 * [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, NBD_CMD_FLAG_PAYLOAD_LEN, ...)
1476 * [ 6 .. 7] type (NBD_CMD_READ, ...)
1477 * [ 8 .. 15] cookie
1478 * [16 .. 23] from
1479 * [24 .. 31] len
1482 magic = ldl_be_p(buf);
1483 request->flags = lduw_be_p(buf + 4);
1484 request->type = lduw_be_p(buf + 6);
1485 request->cookie = ldq_be_p(buf + 8);
1486 request->from = ldq_be_p(buf + 16);
1487 if (client->mode >= NBD_MODE_EXTENDED) {
1488 request->len = ldq_be_p(buf + 24);
1489 expect = NBD_EXTENDED_REQUEST_MAGIC;
1490 } else {
1491 request->len = (uint32_t)ldl_be_p(buf + 24); /* widen 32 to 64 bits */
1492 expect = NBD_REQUEST_MAGIC;
1495 trace_nbd_receive_request(magic, request->flags, request->type,
1496 request->from, request->len);
1498 if (magic != expect) {
1499 error_setg(errp, "invalid magic (got 0x%" PRIx32 ", expected 0x%"
1500 PRIx32 ")", magic, expect);
1501 return -EINVAL;
1503 return 0;
1506 #define MAX_NBD_REQUESTS 16
1508 void nbd_client_get(NBDClient *client)
1510 client->refcount++;
1513 void nbd_client_put(NBDClient *client)
1515 if (--client->refcount == 0) {
1516 /* The last reference should be dropped by client->close,
1517 * which is called by client_close.
1519 assert(client->closing);
1521 qio_channel_detach_aio_context(client->ioc);
1522 object_unref(OBJECT(client->sioc));
1523 object_unref(OBJECT(client->ioc));
1524 if (client->tlscreds) {
1525 object_unref(OBJECT(client->tlscreds));
1527 g_free(client->tlsauthz);
1528 if (client->exp) {
1529 QTAILQ_REMOVE(&client->exp->clients, client, next);
1530 blk_exp_unref(&client->exp->common);
1532 g_free(client->contexts.bitmaps);
1533 g_free(client);
1537 static void client_close(NBDClient *client, bool negotiated)
1539 if (client->closing) {
1540 return;
1543 client->closing = true;
1545 /* Force requests to finish. They will drop their own references,
1546 * then we'll close the socket and free the NBDClient.
1548 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1549 NULL);
1551 /* Also tell the client, so that they release their reference. */
1552 if (client->close_fn) {
1553 client->close_fn(client, negotiated);
1557 static NBDRequestData *nbd_request_get(NBDClient *client)
1559 NBDRequestData *req;
1561 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1562 client->nb_requests++;
1564 req = g_new0(NBDRequestData, 1);
1565 nbd_client_get(client);
1566 req->client = client;
1567 return req;
1570 static void nbd_request_put(NBDRequestData *req)
1572 NBDClient *client = req->client;
1574 if (req->data) {
1575 qemu_vfree(req->data);
1577 g_free(req);
1579 client->nb_requests--;
1581 if (client->quiescing && client->nb_requests == 0) {
1582 aio_wait_kick();
1585 nbd_client_receive_next_request(client);
1587 nbd_client_put(client);
1590 static void blk_aio_attached(AioContext *ctx, void *opaque)
1592 NBDExport *exp = opaque;
1593 NBDClient *client;
1595 trace_nbd_blk_aio_attached(exp->name, ctx);
1597 exp->common.ctx = ctx;
1599 QTAILQ_FOREACH(client, &exp->clients, next) {
1600 qio_channel_attach_aio_context(client->ioc, ctx);
1602 assert(client->nb_requests == 0);
1603 assert(client->recv_coroutine == NULL);
1604 assert(client->send_coroutine == NULL);
1608 static void blk_aio_detach(void *opaque)
1610 NBDExport *exp = opaque;
1611 NBDClient *client;
1613 trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
1615 QTAILQ_FOREACH(client, &exp->clients, next) {
1616 qio_channel_detach_aio_context(client->ioc);
1619 exp->common.ctx = NULL;
1622 static void nbd_drained_begin(void *opaque)
1624 NBDExport *exp = opaque;
1625 NBDClient *client;
1627 QTAILQ_FOREACH(client, &exp->clients, next) {
1628 client->quiescing = true;
1632 static void nbd_drained_end(void *opaque)
1634 NBDExport *exp = opaque;
1635 NBDClient *client;
1637 QTAILQ_FOREACH(client, &exp->clients, next) {
1638 client->quiescing = false;
1639 nbd_client_receive_next_request(client);
1643 static bool nbd_drained_poll(void *opaque)
1645 NBDExport *exp = opaque;
1646 NBDClient *client;
1648 QTAILQ_FOREACH(client, &exp->clients, next) {
1649 if (client->nb_requests != 0) {
1651 * If there's a coroutine waiting for a request on nbd_read_eof()
1652 * enter it here so we don't depend on the client to wake it up.
1654 if (client->recv_coroutine != NULL && client->read_yielding) {
1655 qio_channel_wake_read(client->ioc);
1658 return true;
1662 return false;
1665 static void nbd_eject_notifier(Notifier *n, void *data)
1667 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1669 blk_exp_request_shutdown(&exp->common);
1672 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1674 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1675 assert(exp->drv == &blk_exp_nbd);
1676 assert(nbd_exp->eject_notifier_blk == NULL);
1678 blk_ref(blk);
1679 nbd_exp->eject_notifier_blk = blk;
1680 nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1681 blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1684 static const BlockDevOps nbd_block_ops = {
1685 .drained_begin = nbd_drained_begin,
1686 .drained_end = nbd_drained_end,
1687 .drained_poll = nbd_drained_poll,
1690 static int nbd_export_create(BlockExport *blk_exp, BlockExportOptions *exp_args,
1691 Error **errp)
1693 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1694 BlockExportOptionsNbd *arg = &exp_args->u.nbd;
1695 const char *name = arg->name ?: exp_args->node_name;
1696 BlockBackend *blk = blk_exp->blk;
1697 int64_t size;
1698 uint64_t perm, shared_perm;
1699 bool readonly = !exp_args->writable;
1700 BlockDirtyBitmapOrStrList *bitmaps;
1701 size_t i;
1702 int ret;
1704 assert(exp_args->type == BLOCK_EXPORT_TYPE_NBD);
1706 if (!nbd_server_is_running()) {
1707 error_setg(errp, "NBD server not running");
1708 return -EINVAL;
1711 if (strlen(name) > NBD_MAX_STRING_SIZE) {
1712 error_setg(errp, "export name '%s' too long", name);
1713 return -EINVAL;
1716 if (arg->description && strlen(arg->description) > NBD_MAX_STRING_SIZE) {
1717 error_setg(errp, "description '%s' too long", arg->description);
1718 return -EINVAL;
1721 if (nbd_export_find(name)) {
1722 error_setg(errp, "NBD server already has export named '%s'", name);
1723 return -EEXIST;
1726 size = blk_getlength(blk);
1727 if (size < 0) {
1728 error_setg_errno(errp, -size,
1729 "Failed to determine the NBD export's length");
1730 return size;
1733 /* Don't allow resize while the NBD server is running, otherwise we don't
1734 * care what happens with the node. */
1735 blk_get_perm(blk, &perm, &shared_perm);
1736 ret = blk_set_perm(blk, perm, shared_perm & ~BLK_PERM_RESIZE, errp);
1737 if (ret < 0) {
1738 return ret;
1741 QTAILQ_INIT(&exp->clients);
1742 exp->name = g_strdup(name);
1743 exp->description = g_strdup(arg->description);
1744 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1745 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1747 if (nbd_server_max_connections() != 1) {
1748 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1750 if (readonly) {
1751 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1752 } else {
1753 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1754 NBD_FLAG_SEND_FAST_ZERO);
1756 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1758 for (bitmaps = arg->bitmaps; bitmaps; bitmaps = bitmaps->next) {
1759 exp->nr_export_bitmaps++;
1761 exp->export_bitmaps = g_new0(BdrvDirtyBitmap *, exp->nr_export_bitmaps);
1762 for (i = 0, bitmaps = arg->bitmaps; bitmaps;
1763 i++, bitmaps = bitmaps->next)
1765 const char *bitmap;
1766 BlockDriverState *bs = blk_bs(blk);
1767 BdrvDirtyBitmap *bm = NULL;
1769 switch (bitmaps->value->type) {
1770 case QTYPE_QSTRING:
1771 bitmap = bitmaps->value->u.local;
1772 while (bs) {
1773 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1774 if (bm != NULL) {
1775 break;
1778 bs = bdrv_filter_or_cow_bs(bs);
1781 if (bm == NULL) {
1782 ret = -ENOENT;
1783 error_setg(errp, "Bitmap '%s' is not found",
1784 bitmaps->value->u.local);
1785 goto fail;
1788 if (readonly && bdrv_is_writable(bs) &&
1789 bdrv_dirty_bitmap_enabled(bm)) {
1790 ret = -EINVAL;
1791 error_setg(errp, "Enabled bitmap '%s' incompatible with "
1792 "readonly export", bitmap);
1793 goto fail;
1795 break;
1796 case QTYPE_QDICT:
1797 bitmap = bitmaps->value->u.external.name;
1798 bm = block_dirty_bitmap_lookup(bitmaps->value->u.external.node,
1799 bitmap, NULL, errp);
1800 if (!bm) {
1801 ret = -ENOENT;
1802 goto fail;
1804 break;
1805 default:
1806 abort();
1809 assert(bm);
1811 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1812 ret = -EINVAL;
1813 goto fail;
1816 exp->export_bitmaps[i] = bm;
1817 assert(strlen(bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
1820 /* Mark bitmaps busy in a separate loop, to simplify roll-back concerns. */
1821 for (i = 0; i < exp->nr_export_bitmaps; i++) {
1822 bdrv_dirty_bitmap_set_busy(exp->export_bitmaps[i], true);
1825 exp->allocation_depth = arg->allocation_depth;
1828 * We need to inhibit request queuing in the block layer to ensure we can
1829 * be properly quiesced when entering a drained section, as our coroutines
1830 * servicing pending requests might enter blk_pread().
1832 blk_set_disable_request_queuing(blk, true);
1834 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1836 blk_set_dev_ops(blk, &nbd_block_ops, exp);
1838 QTAILQ_INSERT_TAIL(&exports, exp, next);
1840 return 0;
1842 fail:
1843 g_free(exp->export_bitmaps);
1844 g_free(exp->name);
1845 g_free(exp->description);
1846 return ret;
1849 NBDExport *nbd_export_find(const char *name)
1851 NBDExport *exp;
1852 QTAILQ_FOREACH(exp, &exports, next) {
1853 if (strcmp(name, exp->name) == 0) {
1854 return exp;
1858 return NULL;
1861 AioContext *
1862 nbd_export_aio_context(NBDExport *exp)
1864 return exp->common.ctx;
1867 static void nbd_export_request_shutdown(BlockExport *blk_exp)
1869 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1870 NBDClient *client, *next;
1872 blk_exp_ref(&exp->common);
1874 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1875 * close mode that stops advertising the export to new clients but
1876 * still permits existing clients to run to completion? Because of
1877 * that possibility, nbd_export_close() can be called more than
1878 * once on an export.
1880 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1881 client_close(client, true);
1883 if (exp->name) {
1884 g_free(exp->name);
1885 exp->name = NULL;
1886 QTAILQ_REMOVE(&exports, exp, next);
1888 blk_exp_unref(&exp->common);
1891 static void nbd_export_delete(BlockExport *blk_exp)
1893 size_t i;
1894 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1896 assert(exp->name == NULL);
1897 assert(QTAILQ_EMPTY(&exp->clients));
1899 g_free(exp->description);
1900 exp->description = NULL;
1902 if (exp->eject_notifier_blk) {
1903 notifier_remove(&exp->eject_notifier);
1904 blk_unref(exp->eject_notifier_blk);
1906 blk_remove_aio_context_notifier(exp->common.blk, blk_aio_attached,
1907 blk_aio_detach, exp);
1908 blk_set_disable_request_queuing(exp->common.blk, false);
1910 for (i = 0; i < exp->nr_export_bitmaps; i++) {
1911 bdrv_dirty_bitmap_set_busy(exp->export_bitmaps[i], false);
1915 const BlockExportDriver blk_exp_nbd = {
1916 .type = BLOCK_EXPORT_TYPE_NBD,
1917 .instance_size = sizeof(NBDExport),
1918 .create = nbd_export_create,
1919 .delete = nbd_export_delete,
1920 .request_shutdown = nbd_export_request_shutdown,
1923 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1924 unsigned niov, Error **errp)
1926 int ret;
1928 g_assert(qemu_in_coroutine());
1929 qemu_co_mutex_lock(&client->send_lock);
1930 client->send_coroutine = qemu_coroutine_self();
1932 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1934 client->send_coroutine = NULL;
1935 qemu_co_mutex_unlock(&client->send_lock);
1937 return ret;
1940 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1941 uint64_t cookie)
1943 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1944 stl_be_p(&reply->error, error);
1945 stq_be_p(&reply->cookie, cookie);
1948 static int coroutine_fn nbd_co_send_simple_reply(NBDClient *client,
1949 NBDRequest *request,
1950 uint32_t error,
1951 void *data,
1952 uint64_t len,
1953 Error **errp)
1955 NBDSimpleReply reply;
1956 int nbd_err = system_errno_to_nbd_errno(error);
1957 struct iovec iov[] = {
1958 {.iov_base = &reply, .iov_len = sizeof(reply)},
1959 {.iov_base = data, .iov_len = len}
1962 assert(!len || !nbd_err);
1963 assert(len <= NBD_MAX_STRING_SIZE);
1964 assert(client->mode < NBD_MODE_STRUCTURED ||
1965 (client->mode == NBD_MODE_STRUCTURED &&
1966 request->type != NBD_CMD_READ));
1967 trace_nbd_co_send_simple_reply(request->cookie, nbd_err,
1968 nbd_err_lookup(nbd_err), len);
1969 set_be_simple_reply(&reply, nbd_err, request->cookie);
1971 return nbd_co_send_iov(client, iov, 2, errp);
1975 * Prepare the header of a reply chunk for network transmission.
1977 * On input, @iov is partially initialized: iov[0].iov_base must point
1978 * to an uninitialized NBDReply, while the remaining @niov elements
1979 * (if any) must be ready for transmission. This function then
1980 * populates iov[0] for transmission.
1982 static inline void set_be_chunk(NBDClient *client, struct iovec *iov,
1983 size_t niov, uint16_t flags, uint16_t type,
1984 NBDRequest *request)
1986 size_t i, length = 0;
1988 for (i = 1; i < niov; i++) {
1989 length += iov[i].iov_len;
1991 assert(length <= NBD_MAX_BUFFER_SIZE + sizeof(NBDStructuredReadData));
1993 if (client->mode >= NBD_MODE_EXTENDED) {
1994 NBDExtendedReplyChunk *chunk = iov->iov_base;
1996 iov[0].iov_len = sizeof(*chunk);
1997 stl_be_p(&chunk->magic, NBD_EXTENDED_REPLY_MAGIC);
1998 stw_be_p(&chunk->flags, flags);
1999 stw_be_p(&chunk->type, type);
2000 stq_be_p(&chunk->cookie, request->cookie);
2001 stq_be_p(&chunk->offset, request->from);
2002 stq_be_p(&chunk->length, length);
2003 } else {
2004 NBDStructuredReplyChunk *chunk = iov->iov_base;
2006 iov[0].iov_len = sizeof(*chunk);
2007 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
2008 stw_be_p(&chunk->flags, flags);
2009 stw_be_p(&chunk->type, type);
2010 stq_be_p(&chunk->cookie, request->cookie);
2011 stl_be_p(&chunk->length, length);
2015 static int coroutine_fn nbd_co_send_chunk_done(NBDClient *client,
2016 NBDRequest *request,
2017 Error **errp)
2019 NBDReply hdr;
2020 struct iovec iov[] = {
2021 {.iov_base = &hdr},
2024 trace_nbd_co_send_chunk_done(request->cookie);
2025 set_be_chunk(client, iov, 1, NBD_REPLY_FLAG_DONE,
2026 NBD_REPLY_TYPE_NONE, request);
2027 return nbd_co_send_iov(client, iov, 1, errp);
2030 static int coroutine_fn nbd_co_send_chunk_read(NBDClient *client,
2031 NBDRequest *request,
2032 uint64_t offset,
2033 void *data,
2034 uint64_t size,
2035 bool final,
2036 Error **errp)
2038 NBDReply hdr;
2039 NBDStructuredReadData chunk;
2040 struct iovec iov[] = {
2041 {.iov_base = &hdr},
2042 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2043 {.iov_base = data, .iov_len = size}
2046 assert(size && size <= NBD_MAX_BUFFER_SIZE);
2047 trace_nbd_co_send_chunk_read(request->cookie, offset, data, size);
2048 set_be_chunk(client, iov, 3, final ? NBD_REPLY_FLAG_DONE : 0,
2049 NBD_REPLY_TYPE_OFFSET_DATA, request);
2050 stq_be_p(&chunk.offset, offset);
2052 return nbd_co_send_iov(client, iov, 3, errp);
2055 static int coroutine_fn nbd_co_send_chunk_error(NBDClient *client,
2056 NBDRequest *request,
2057 uint32_t error,
2058 const char *msg,
2059 Error **errp)
2061 NBDReply hdr;
2062 NBDStructuredError chunk;
2063 int nbd_err = system_errno_to_nbd_errno(error);
2064 struct iovec iov[] = {
2065 {.iov_base = &hdr},
2066 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2067 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
2070 assert(nbd_err);
2071 trace_nbd_co_send_chunk_error(request->cookie, nbd_err,
2072 nbd_err_lookup(nbd_err), msg ? msg : "");
2073 set_be_chunk(client, iov, 3, NBD_REPLY_FLAG_DONE,
2074 NBD_REPLY_TYPE_ERROR, request);
2075 stl_be_p(&chunk.error, nbd_err);
2076 stw_be_p(&chunk.message_length, iov[2].iov_len);
2078 return nbd_co_send_iov(client, iov, 3, errp);
2081 /* Do a sparse read and send the structured reply to the client.
2082 * Returns -errno if sending fails. blk_co_block_status_above() failure is
2083 * reported to the client, at which point this function succeeds.
2085 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
2086 NBDRequest *request,
2087 uint64_t offset,
2088 uint8_t *data,
2089 uint64_t size,
2090 Error **errp)
2092 int ret = 0;
2093 NBDExport *exp = client->exp;
2094 size_t progress = 0;
2096 assert(size <= NBD_MAX_BUFFER_SIZE);
2097 while (progress < size) {
2098 int64_t pnum;
2099 int status = blk_co_block_status_above(exp->common.blk, NULL,
2100 offset + progress,
2101 size - progress, &pnum, NULL,
2102 NULL);
2103 bool final;
2105 if (status < 0) {
2106 char *msg = g_strdup_printf("unable to check for holes: %s",
2107 strerror(-status));
2109 ret = nbd_co_send_chunk_error(client, request, -status, msg, errp);
2110 g_free(msg);
2111 return ret;
2113 assert(pnum && pnum <= size - progress);
2114 final = progress + pnum == size;
2115 if (status & BDRV_BLOCK_ZERO) {
2116 NBDReply hdr;
2117 NBDStructuredReadHole chunk;
2118 struct iovec iov[] = {
2119 {.iov_base = &hdr},
2120 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2123 trace_nbd_co_send_chunk_read_hole(request->cookie,
2124 offset + progress, pnum);
2125 set_be_chunk(client, iov, 2,
2126 final ? NBD_REPLY_FLAG_DONE : 0,
2127 NBD_REPLY_TYPE_OFFSET_HOLE, request);
2128 stq_be_p(&chunk.offset, offset + progress);
2129 stl_be_p(&chunk.length, pnum);
2130 ret = nbd_co_send_iov(client, iov, 2, errp);
2131 } else {
2132 ret = blk_co_pread(exp->common.blk, offset + progress, pnum,
2133 data + progress, 0);
2134 if (ret < 0) {
2135 error_setg_errno(errp, -ret, "reading from file failed");
2136 break;
2138 ret = nbd_co_send_chunk_read(client, request, offset + progress,
2139 data + progress, pnum, final, errp);
2142 if (ret < 0) {
2143 break;
2145 progress += pnum;
2147 return ret;
2150 typedef struct NBDExtentArray {
2151 NBDExtent64 *extents;
2152 unsigned int nb_alloc;
2153 unsigned int count;
2154 uint64_t total_length;
2155 bool extended;
2156 bool can_add;
2157 bool converted_to_be;
2158 } NBDExtentArray;
2160 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc,
2161 NBDMode mode)
2163 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
2165 assert(mode >= NBD_MODE_STRUCTURED);
2166 ea->nb_alloc = nb_alloc;
2167 ea->extents = g_new(NBDExtent64, nb_alloc);
2168 ea->extended = mode >= NBD_MODE_EXTENDED;
2169 ea->can_add = true;
2171 return ea;
2174 static void nbd_extent_array_free(NBDExtentArray *ea)
2176 g_free(ea->extents);
2177 g_free(ea);
2179 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free)
2181 /* Further modifications of the array after conversion are abandoned */
2182 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
2184 int i;
2186 assert(!ea->converted_to_be);
2187 assert(ea->extended);
2188 ea->can_add = false;
2189 ea->converted_to_be = true;
2191 for (i = 0; i < ea->count; i++) {
2192 ea->extents[i].length = cpu_to_be64(ea->extents[i].length);
2193 ea->extents[i].flags = cpu_to_be64(ea->extents[i].flags);
2197 /* Further modifications of the array after conversion are abandoned */
2198 static NBDExtent32 *nbd_extent_array_convert_to_narrow(NBDExtentArray *ea)
2200 int i;
2201 NBDExtent32 *extents = g_new(NBDExtent32, ea->count);
2203 assert(!ea->converted_to_be);
2204 assert(!ea->extended);
2205 ea->can_add = false;
2206 ea->converted_to_be = true;
2208 for (i = 0; i < ea->count; i++) {
2209 assert((ea->extents[i].length | ea->extents[i].flags) <= UINT32_MAX);
2210 extents[i].length = cpu_to_be32(ea->extents[i].length);
2211 extents[i].flags = cpu_to_be32(ea->extents[i].flags);
2214 return extents;
2218 * Add extent to NBDExtentArray. If extent can't be added (no available space),
2219 * return -1.
2220 * For safety, when returning -1 for the first time, .can_add is set to false,
2221 * and further calls to nbd_extent_array_add() will crash.
2222 * (this avoids the situation where a caller ignores failure to add one extent,
2223 * where adding another extent that would squash into the last array entry
2224 * would result in an incorrect range reported to the client)
2226 static int nbd_extent_array_add(NBDExtentArray *ea,
2227 uint64_t length, uint32_t flags)
2229 assert(ea->can_add);
2231 if (!length) {
2232 return 0;
2234 if (!ea->extended) {
2235 assert(length <= UINT32_MAX);
2238 /* Extend previous extent if flags are the same */
2239 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
2240 uint64_t sum = length + ea->extents[ea->count - 1].length;
2243 * sum cannot overflow: the block layer bounds image size at
2244 * 2^63, and ea->extents[].length comes from the block layer.
2246 assert(sum >= length);
2247 if (sum <= UINT32_MAX || ea->extended) {
2248 ea->extents[ea->count - 1].length = sum;
2249 ea->total_length += length;
2250 return 0;
2254 if (ea->count >= ea->nb_alloc) {
2255 ea->can_add = false;
2256 return -1;
2259 ea->total_length += length;
2260 ea->extents[ea->count] = (NBDExtent64) {.length = length, .flags = flags};
2261 ea->count++;
2263 return 0;
2266 static int coroutine_fn blockstatus_to_extents(BlockBackend *blk,
2267 uint64_t offset, uint64_t bytes,
2268 NBDExtentArray *ea)
2270 while (bytes) {
2271 uint32_t flags;
2272 int64_t num;
2273 int ret = blk_co_block_status_above(blk, NULL, offset, bytes, &num,
2274 NULL, NULL);
2276 if (ret < 0) {
2277 return ret;
2280 flags = (ret & BDRV_BLOCK_DATA ? 0 : NBD_STATE_HOLE) |
2281 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
2283 if (nbd_extent_array_add(ea, num, flags) < 0) {
2284 return 0;
2287 offset += num;
2288 bytes -= num;
2291 return 0;
2294 static int coroutine_fn blockalloc_to_extents(BlockBackend *blk,
2295 uint64_t offset, uint64_t bytes,
2296 NBDExtentArray *ea)
2298 while (bytes) {
2299 int64_t num;
2300 int ret = blk_co_is_allocated_above(blk, NULL, false, offset, bytes,
2301 &num);
2303 if (ret < 0) {
2304 return ret;
2307 if (nbd_extent_array_add(ea, num, ret) < 0) {
2308 return 0;
2311 offset += num;
2312 bytes -= num;
2315 return 0;
2319 * nbd_co_send_extents
2321 * @ea is converted to BE by the function
2322 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2324 static int coroutine_fn
2325 nbd_co_send_extents(NBDClient *client, NBDRequest *request, NBDExtentArray *ea,
2326 bool last, uint32_t context_id, Error **errp)
2328 NBDReply hdr;
2329 NBDStructuredMeta meta;
2330 NBDExtendedMeta meta_ext;
2331 g_autofree NBDExtent32 *extents = NULL;
2332 uint16_t type;
2333 struct iovec iov[] = { {.iov_base = &hdr}, {0}, {0} };
2335 if (client->mode >= NBD_MODE_EXTENDED) {
2336 type = NBD_REPLY_TYPE_BLOCK_STATUS_EXT;
2338 iov[1].iov_base = &meta_ext;
2339 iov[1].iov_len = sizeof(meta_ext);
2340 stl_be_p(&meta_ext.context_id, context_id);
2341 stl_be_p(&meta_ext.count, ea->count);
2343 nbd_extent_array_convert_to_be(ea);
2344 iov[2].iov_base = ea->extents;
2345 iov[2].iov_len = ea->count * sizeof(ea->extents[0]);
2346 } else {
2347 type = NBD_REPLY_TYPE_BLOCK_STATUS;
2349 iov[1].iov_base = &meta;
2350 iov[1].iov_len = sizeof(meta);
2351 stl_be_p(&meta.context_id, context_id);
2353 extents = nbd_extent_array_convert_to_narrow(ea);
2354 iov[2].iov_base = extents;
2355 iov[2].iov_len = ea->count * sizeof(extents[0]);
2358 trace_nbd_co_send_extents(request->cookie, ea->count, context_id,
2359 ea->total_length, last);
2360 set_be_chunk(client, iov, 3, last ? NBD_REPLY_FLAG_DONE : 0, type,
2361 request);
2363 return nbd_co_send_iov(client, iov, 3, errp);
2366 /* Get block status from the exported device and send it to the client */
2367 static int
2368 coroutine_fn nbd_co_send_block_status(NBDClient *client, NBDRequest *request,
2369 BlockBackend *blk, uint64_t offset,
2370 uint64_t length, bool dont_fragment,
2371 bool last, uint32_t context_id,
2372 Error **errp)
2374 int ret;
2375 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2376 g_autoptr(NBDExtentArray) ea =
2377 nbd_extent_array_new(nb_extents, client->mode);
2379 if (context_id == NBD_META_ID_BASE_ALLOCATION) {
2380 ret = blockstatus_to_extents(blk, offset, length, ea);
2381 } else {
2382 ret = blockalloc_to_extents(blk, offset, length, ea);
2384 if (ret < 0) {
2385 return nbd_co_send_chunk_error(client, request, -ret,
2386 "can't get block status", errp);
2389 return nbd_co_send_extents(client, request, ea, last, context_id, errp);
2392 /* Populate @ea from a dirty bitmap. */
2393 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2394 uint64_t offset, uint64_t length,
2395 NBDExtentArray *es)
2397 int64_t start, dirty_start, dirty_count;
2398 int64_t end = offset + length;
2399 bool full = false;
2400 int64_t bound = es->extended ? INT64_MAX : INT32_MAX;
2402 bdrv_dirty_bitmap_lock(bitmap);
2404 for (start = offset;
2405 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, bound,
2406 &dirty_start, &dirty_count);
2407 start = dirty_start + dirty_count)
2409 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2410 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2412 full = true;
2413 break;
2417 if (!full) {
2418 /* last non dirty extent, nothing to do if array is now full */
2419 (void) nbd_extent_array_add(es, end - start, 0);
2422 bdrv_dirty_bitmap_unlock(bitmap);
2425 static int coroutine_fn nbd_co_send_bitmap(NBDClient *client,
2426 NBDRequest *request,
2427 BdrvDirtyBitmap *bitmap,
2428 uint64_t offset,
2429 uint64_t length, bool dont_fragment,
2430 bool last, uint32_t context_id,
2431 Error **errp)
2433 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2434 g_autoptr(NBDExtentArray) ea =
2435 nbd_extent_array_new(nb_extents, client->mode);
2437 bitmap_to_extents(bitmap, offset, length, ea);
2439 return nbd_co_send_extents(client, request, ea, last, context_id, errp);
2443 * nbd_co_block_status_payload_read
2444 * Called when a client wants a subset of negotiated contexts via a
2445 * BLOCK_STATUS payload. Check the payload for valid length and
2446 * contents. On success, return 0 with request updated to effective
2447 * length. If request was invalid but all payload consumed, return 0
2448 * with request->len and request->contexts->count set to 0 (which will
2449 * trigger an appropriate NBD_EINVAL response later on). Return
2450 * negative errno if the payload was not fully consumed.
2452 static int
2453 nbd_co_block_status_payload_read(NBDClient *client, NBDRequest *request,
2454 Error **errp)
2456 int payload_len = request->len;
2457 g_autofree char *buf = NULL;
2458 size_t count, i, nr_bitmaps;
2459 uint32_t id;
2461 if (payload_len > NBD_MAX_BUFFER_SIZE) {
2462 error_setg(errp, "len (%" PRIu64 ") is larger than max len (%u)",
2463 request->len, NBD_MAX_BUFFER_SIZE);
2464 return -EINVAL;
2467 assert(client->contexts.exp == client->exp);
2468 nr_bitmaps = client->exp->nr_export_bitmaps;
2469 request->contexts = g_new0(NBDMetaContexts, 1);
2470 request->contexts->exp = client->exp;
2472 if (payload_len % sizeof(uint32_t) ||
2473 payload_len < sizeof(NBDBlockStatusPayload) ||
2474 payload_len > (sizeof(NBDBlockStatusPayload) +
2475 sizeof(id) * client->contexts.count)) {
2476 goto skip;
2479 buf = g_malloc(payload_len);
2480 if (nbd_read(client->ioc, buf, payload_len,
2481 "CMD_BLOCK_STATUS data", errp) < 0) {
2482 return -EIO;
2484 trace_nbd_co_receive_request_payload_received(request->cookie,
2485 payload_len);
2486 request->contexts->bitmaps = g_new0(bool, nr_bitmaps);
2487 count = (payload_len - sizeof(NBDBlockStatusPayload)) / sizeof(id);
2488 payload_len = 0;
2490 for (i = 0; i < count; i++) {
2491 id = ldl_be_p(buf + sizeof(NBDBlockStatusPayload) + sizeof(id) * i);
2492 if (id == NBD_META_ID_BASE_ALLOCATION) {
2493 if (request->contexts->base_allocation) {
2494 goto skip;
2496 request->contexts->base_allocation = true;
2497 } else if (id == NBD_META_ID_ALLOCATION_DEPTH) {
2498 if (request->contexts->allocation_depth) {
2499 goto skip;
2501 request->contexts->allocation_depth = true;
2502 } else {
2503 int idx = id - NBD_META_ID_DIRTY_BITMAP;
2505 if (idx > nr_bitmaps || request->contexts->bitmaps[idx]) {
2506 goto skip;
2508 request->contexts->bitmaps[idx] = true;
2512 request->len = ldq_be_p(buf);
2513 request->contexts->count = count;
2514 return 0;
2516 skip:
2517 trace_nbd_co_receive_block_status_payload_compliance(request->from,
2518 request->len);
2519 request->len = request->contexts->count = 0;
2520 return nbd_drop(client->ioc, payload_len, errp);
2523 /* nbd_co_receive_request
2524 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2525 * connection right away, -EAGAIN to indicate we were interrupted and the
2526 * channel should be quiesced, and any other negative value to report an error
2527 * to the client (although the caller may still need to disconnect after
2528 * reporting the error).
2530 static int coroutine_fn nbd_co_receive_request(NBDRequestData *req,
2531 NBDRequest *request,
2532 Error **errp)
2534 NBDClient *client = req->client;
2535 bool extended_with_payload;
2536 bool check_length;
2537 bool check_rofs = false;
2538 bool allocate_buffer = false;
2539 unsigned payload_len = 0;
2540 int valid_flags = NBD_CMD_FLAG_FUA;
2541 int ret;
2543 g_assert(qemu_in_coroutine());
2544 assert(client->recv_coroutine == qemu_coroutine_self());
2545 ret = nbd_receive_request(client, request, errp);
2546 if (ret < 0) {
2547 return ret;
2550 trace_nbd_co_receive_request_decode_type(request->cookie, request->type,
2551 nbd_cmd_lookup(request->type));
2552 check_length = extended_with_payload = client->mode >= NBD_MODE_EXTENDED &&
2553 request->flags & NBD_CMD_FLAG_PAYLOAD_LEN;
2555 switch (request->type) {
2556 case NBD_CMD_DISC:
2557 /* Special case: we're going to disconnect without a reply,
2558 * whether or not flags, from, or len are bogus */
2559 req->complete = true;
2560 return -EIO;
2562 case NBD_CMD_READ:
2563 if (client->mode >= NBD_MODE_STRUCTURED) {
2564 valid_flags |= NBD_CMD_FLAG_DF;
2566 check_length = true;
2567 allocate_buffer = true;
2568 break;
2570 case NBD_CMD_WRITE:
2571 if (client->mode >= NBD_MODE_EXTENDED) {
2572 if (!extended_with_payload) {
2573 /* The client is noncompliant. Trace it, but proceed. */
2574 trace_nbd_co_receive_ext_payload_compliance(request->from,
2575 request->len);
2577 valid_flags |= NBD_CMD_FLAG_PAYLOAD_LEN;
2579 payload_len = request->len;
2580 check_length = true;
2581 allocate_buffer = true;
2582 check_rofs = true;
2583 break;
2585 case NBD_CMD_FLUSH:
2586 break;
2588 case NBD_CMD_TRIM:
2589 check_rofs = true;
2590 break;
2592 case NBD_CMD_CACHE:
2593 check_length = true;
2594 break;
2596 case NBD_CMD_WRITE_ZEROES:
2597 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2598 check_rofs = true;
2599 break;
2601 case NBD_CMD_BLOCK_STATUS:
2602 if (extended_with_payload) {
2603 ret = nbd_co_block_status_payload_read(client, request, errp);
2604 if (ret < 0) {
2605 return ret;
2607 /* payload now consumed */
2608 check_length = extended_with_payload = false;
2609 payload_len = 0;
2610 valid_flags |= NBD_CMD_FLAG_PAYLOAD_LEN;
2611 } else {
2612 request->contexts = &client->contexts;
2614 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2615 break;
2617 default:
2618 /* Unrecognized, will fail later */
2622 /* Payload and buffer handling. */
2623 if (!payload_len) {
2624 req->complete = true;
2626 if (check_length && request->len > NBD_MAX_BUFFER_SIZE) {
2627 /* READ, WRITE, CACHE */
2628 error_setg(errp, "len (%" PRIu64 ") is larger than max len (%u)",
2629 request->len, NBD_MAX_BUFFER_SIZE);
2630 return -EINVAL;
2632 if (extended_with_payload && !allocate_buffer) {
2634 * For now, we don't support payloads on other commands; but
2635 * we can keep the connection alive by ignoring the payload.
2637 assert(request->type != NBD_CMD_WRITE);
2638 payload_len = request->len;
2639 request->len = 0;
2641 if (allocate_buffer) {
2642 /* READ, WRITE */
2643 req->data = blk_try_blockalign(client->exp->common.blk,
2644 request->len);
2645 if (req->data == NULL) {
2646 error_setg(errp, "No memory");
2647 return -ENOMEM;
2650 if (payload_len) {
2651 if (req->data) {
2652 ret = nbd_read(client->ioc, req->data, payload_len,
2653 "CMD_WRITE data", errp);
2654 } else {
2655 ret = nbd_drop(client->ioc, payload_len, errp);
2657 if (ret < 0) {
2658 return -EIO;
2660 req->complete = true;
2661 trace_nbd_co_receive_request_payload_received(request->cookie,
2662 payload_len);
2665 /* Sanity checks. */
2666 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY && check_rofs) {
2667 /* WRITE, TRIM, WRITE_ZEROES */
2668 error_setg(errp, "Export is read-only");
2669 return -EROFS;
2671 if (request->from > client->exp->size ||
2672 request->len > client->exp->size - request->from) {
2673 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu64
2674 ", Size: %" PRIu64, request->from, request->len,
2675 client->exp->size);
2676 return (request->type == NBD_CMD_WRITE ||
2677 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2679 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2680 client->check_align)) {
2682 * The block layer gracefully handles unaligned requests, but
2683 * it's still worth tracing client non-compliance
2685 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2686 request->from,
2687 request->len,
2688 client->check_align);
2690 if (request->flags & ~valid_flags) {
2691 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2692 nbd_cmd_lookup(request->type), request->flags);
2693 return -EINVAL;
2696 return 0;
2699 /* Send simple reply without a payload, or a structured error
2700 * @error_msg is ignored if @ret >= 0
2701 * Returns 0 if connection is still live, -errno on failure to talk to client
2703 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2704 NBDRequest *request,
2705 int ret,
2706 const char *error_msg,
2707 Error **errp)
2709 if (client->mode >= NBD_MODE_STRUCTURED && ret < 0) {
2710 return nbd_co_send_chunk_error(client, request, -ret, error_msg, errp);
2711 } else if (client->mode >= NBD_MODE_EXTENDED) {
2712 return nbd_co_send_chunk_done(client, request, errp);
2713 } else {
2714 return nbd_co_send_simple_reply(client, request, ret < 0 ? -ret : 0,
2715 NULL, 0, errp);
2719 /* Handle NBD_CMD_READ request.
2720 * Return -errno if sending fails. Other errors are reported directly to the
2721 * client as an error reply. */
2722 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2723 uint8_t *data, Error **errp)
2725 int ret;
2726 NBDExport *exp = client->exp;
2728 assert(request->type == NBD_CMD_READ);
2729 assert(request->len <= NBD_MAX_BUFFER_SIZE);
2731 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2732 if (request->flags & NBD_CMD_FLAG_FUA) {
2733 ret = blk_co_flush(exp->common.blk);
2734 if (ret < 0) {
2735 return nbd_send_generic_reply(client, request, ret,
2736 "flush failed", errp);
2740 if (client->mode >= NBD_MODE_STRUCTURED &&
2741 !(request->flags & NBD_CMD_FLAG_DF) && request->len)
2743 return nbd_co_send_sparse_read(client, request, request->from,
2744 data, request->len, errp);
2747 ret = blk_co_pread(exp->common.blk, request->from, request->len, data, 0);
2748 if (ret < 0) {
2749 return nbd_send_generic_reply(client, request, ret,
2750 "reading from file failed", errp);
2753 if (client->mode >= NBD_MODE_STRUCTURED) {
2754 if (request->len) {
2755 return nbd_co_send_chunk_read(client, request, request->from, data,
2756 request->len, true, errp);
2757 } else {
2758 return nbd_co_send_chunk_done(client, request, errp);
2760 } else {
2761 return nbd_co_send_simple_reply(client, request, 0,
2762 data, request->len, errp);
2767 * nbd_do_cmd_cache
2769 * Handle NBD_CMD_CACHE request.
2770 * Return -errno if sending fails. Other errors are reported directly to the
2771 * client as an error reply.
2773 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2774 Error **errp)
2776 int ret;
2777 NBDExport *exp = client->exp;
2779 assert(request->type == NBD_CMD_CACHE);
2780 assert(request->len <= NBD_MAX_BUFFER_SIZE);
2782 ret = blk_co_preadv(exp->common.blk, request->from, request->len,
2783 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2785 return nbd_send_generic_reply(client, request, ret,
2786 "caching data failed", errp);
2789 /* Handle NBD request.
2790 * Return -errno if sending fails. Other errors are reported directly to the
2791 * client as an error reply. */
2792 static coroutine_fn int nbd_handle_request(NBDClient *client,
2793 NBDRequest *request,
2794 uint8_t *data, Error **errp)
2796 int ret;
2797 int flags;
2798 NBDExport *exp = client->exp;
2799 char *msg;
2800 size_t i;
2802 switch (request->type) {
2803 case NBD_CMD_CACHE:
2804 return nbd_do_cmd_cache(client, request, errp);
2806 case NBD_CMD_READ:
2807 return nbd_do_cmd_read(client, request, data, errp);
2809 case NBD_CMD_WRITE:
2810 flags = 0;
2811 if (request->flags & NBD_CMD_FLAG_FUA) {
2812 flags |= BDRV_REQ_FUA;
2814 assert(request->len <= NBD_MAX_BUFFER_SIZE);
2815 ret = blk_co_pwrite(exp->common.blk, request->from, request->len, data,
2816 flags);
2817 return nbd_send_generic_reply(client, request, ret,
2818 "writing to file failed", errp);
2820 case NBD_CMD_WRITE_ZEROES:
2821 flags = 0;
2822 if (request->flags & NBD_CMD_FLAG_FUA) {
2823 flags |= BDRV_REQ_FUA;
2825 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2826 flags |= BDRV_REQ_MAY_UNMAP;
2828 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2829 flags |= BDRV_REQ_NO_FALLBACK;
2831 ret = blk_co_pwrite_zeroes(exp->common.blk, request->from, request->len,
2832 flags);
2833 return nbd_send_generic_reply(client, request, ret,
2834 "writing to file failed", errp);
2836 case NBD_CMD_DISC:
2837 /* unreachable, thanks to special case in nbd_co_receive_request() */
2838 abort();
2840 case NBD_CMD_FLUSH:
2841 ret = blk_co_flush(exp->common.blk);
2842 return nbd_send_generic_reply(client, request, ret,
2843 "flush failed", errp);
2845 case NBD_CMD_TRIM:
2846 ret = blk_co_pdiscard(exp->common.blk, request->from, request->len);
2847 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2848 ret = blk_co_flush(exp->common.blk);
2850 return nbd_send_generic_reply(client, request, ret,
2851 "discard failed", errp);
2853 case NBD_CMD_BLOCK_STATUS:
2854 assert(request->contexts);
2855 assert(client->mode >= NBD_MODE_EXTENDED ||
2856 request->len <= UINT32_MAX);
2857 if (request->contexts->count) {
2858 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2859 int contexts_remaining = request->contexts->count;
2861 if (!request->len) {
2862 return nbd_send_generic_reply(client, request, -EINVAL,
2863 "need non-zero length", errp);
2865 if (request->contexts->base_allocation) {
2866 ret = nbd_co_send_block_status(client, request,
2867 exp->common.blk,
2868 request->from,
2869 request->len, dont_fragment,
2870 !--contexts_remaining,
2871 NBD_META_ID_BASE_ALLOCATION,
2872 errp);
2873 if (ret < 0) {
2874 return ret;
2878 if (request->contexts->allocation_depth) {
2879 ret = nbd_co_send_block_status(client, request,
2880 exp->common.blk,
2881 request->from, request->len,
2882 dont_fragment,
2883 !--contexts_remaining,
2884 NBD_META_ID_ALLOCATION_DEPTH,
2885 errp);
2886 if (ret < 0) {
2887 return ret;
2891 assert(request->contexts->exp == client->exp);
2892 for (i = 0; i < client->exp->nr_export_bitmaps; i++) {
2893 if (!request->contexts->bitmaps[i]) {
2894 continue;
2896 ret = nbd_co_send_bitmap(client, request,
2897 client->exp->export_bitmaps[i],
2898 request->from, request->len,
2899 dont_fragment, !--contexts_remaining,
2900 NBD_META_ID_DIRTY_BITMAP + i, errp);
2901 if (ret < 0) {
2902 return ret;
2906 assert(!contexts_remaining);
2908 return 0;
2909 } else if (client->contexts.count) {
2910 return nbd_send_generic_reply(client, request, -EINVAL,
2911 "CMD_BLOCK_STATUS payload not valid",
2912 errp);
2913 } else {
2914 return nbd_send_generic_reply(client, request, -EINVAL,
2915 "CMD_BLOCK_STATUS not negotiated",
2916 errp);
2919 default:
2920 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2921 request->type);
2922 ret = nbd_send_generic_reply(client, request, -EINVAL, msg,
2923 errp);
2924 g_free(msg);
2925 return ret;
2929 /* Owns a reference to the NBDClient passed as opaque. */
2930 static coroutine_fn void nbd_trip(void *opaque)
2932 NBDClient *client = opaque;
2933 NBDRequestData *req;
2934 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2935 int ret;
2936 Error *local_err = NULL;
2938 trace_nbd_trip();
2939 if (client->closing) {
2940 nbd_client_put(client);
2941 return;
2944 if (client->quiescing) {
2946 * We're switching between AIO contexts. Don't attempt to receive a new
2947 * request and kick the main context which may be waiting for us.
2949 nbd_client_put(client);
2950 client->recv_coroutine = NULL;
2951 aio_wait_kick();
2952 return;
2955 req = nbd_request_get(client);
2956 ret = nbd_co_receive_request(req, &request, &local_err);
2957 client->recv_coroutine = NULL;
2959 if (client->closing) {
2961 * The client may be closed when we are blocked in
2962 * nbd_co_receive_request()
2964 goto done;
2967 if (ret == -EAGAIN) {
2968 assert(client->quiescing);
2969 goto done;
2972 nbd_client_receive_next_request(client);
2973 if (ret == -EIO) {
2974 goto disconnect;
2977 qio_channel_set_cork(client->ioc, true);
2979 if (ret < 0) {
2980 /* It wasn't -EIO, so, according to nbd_co_receive_request()
2981 * semantics, we should return the error to the client. */
2982 Error *export_err = local_err;
2984 local_err = NULL;
2985 ret = nbd_send_generic_reply(client, &request, -EINVAL,
2986 error_get_pretty(export_err), &local_err);
2987 error_free(export_err);
2988 } else {
2989 ret = nbd_handle_request(client, &request, req->data, &local_err);
2991 if (request.contexts && request.contexts != &client->contexts) {
2992 assert(request.type == NBD_CMD_BLOCK_STATUS);
2993 g_free(request.contexts->bitmaps);
2994 g_free(request.contexts);
2996 if (ret < 0) {
2997 error_prepend(&local_err, "Failed to send reply: ");
2998 goto disconnect;
3002 * We must disconnect after NBD_CMD_WRITE or BLOCK_STATUS with
3003 * payload if we did not read the payload.
3005 if (!req->complete) {
3006 error_setg(&local_err, "Request handling failed in intermediate state");
3007 goto disconnect;
3010 qio_channel_set_cork(client->ioc, false);
3011 done:
3012 nbd_request_put(req);
3013 nbd_client_put(client);
3014 return;
3016 disconnect:
3017 if (local_err) {
3018 error_reportf_err(local_err, "Disconnect client, due to: ");
3020 nbd_request_put(req);
3021 client_close(client, true);
3022 nbd_client_put(client);
3025 static void nbd_client_receive_next_request(NBDClient *client)
3027 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS &&
3028 !client->quiescing) {
3029 nbd_client_get(client);
3030 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
3031 aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);
3035 static coroutine_fn void nbd_co_client_start(void *opaque)
3037 NBDClient *client = opaque;
3038 Error *local_err = NULL;
3040 qemu_co_mutex_init(&client->send_lock);
3042 if (nbd_negotiate(client, &local_err)) {
3043 if (local_err) {
3044 error_report_err(local_err);
3046 client_close(client, false);
3047 return;
3050 nbd_client_receive_next_request(client);
3054 * Create a new client listener using the given channel @sioc.
3055 * Begin servicing it in a coroutine. When the connection closes, call
3056 * @close_fn with an indication of whether the client completed negotiation.
3058 void nbd_client_new(QIOChannelSocket *sioc,
3059 QCryptoTLSCreds *tlscreds,
3060 const char *tlsauthz,
3061 void (*close_fn)(NBDClient *, bool))
3063 NBDClient *client;
3064 Coroutine *co;
3066 client = g_new0(NBDClient, 1);
3067 client->refcount = 1;
3068 client->tlscreds = tlscreds;
3069 if (tlscreds) {
3070 object_ref(OBJECT(client->tlscreds));
3072 client->tlsauthz = g_strdup(tlsauthz);
3073 client->sioc = sioc;
3074 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
3075 object_ref(OBJECT(client->sioc));
3076 client->ioc = QIO_CHANNEL(sioc);
3077 object_ref(OBJECT(client->ioc));
3078 client->close_fn = close_fn;
3080 co = qemu_coroutine_create(nbd_co_client_start, client);
3081 qemu_coroutine_enter(co);