nbd/server: Reject embedded NUL in NBD strings
[qemu/ar7.git] / nbd / server.c
blob50f95abe31681e7b242051b18945cdaceb52cec8
1 /*
2 * Copyright (C) 2016-2018 Red Hat, Inc.
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/export.h"
23 #include "qapi/error.h"
24 #include "qemu/queue.h"
25 #include "trace.h"
26 #include "nbd-internal.h"
27 #include "qemu/units.h"
29 #define NBD_META_ID_BASE_ALLOCATION 0
30 #define NBD_META_ID_DIRTY_BITMAP 1
33 * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
34 * constant. If an increase is needed, note that the NBD protocol
35 * recommends no larger than 32 mb, so that the client won't consider
36 * the reply as a denial of service attack.
38 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
40 static int system_errno_to_nbd_errno(int err)
42 switch (err) {
43 case 0:
44 return NBD_SUCCESS;
45 case EPERM:
46 case EROFS:
47 return NBD_EPERM;
48 case EIO:
49 return NBD_EIO;
50 case ENOMEM:
51 return NBD_ENOMEM;
52 #ifdef EDQUOT
53 case EDQUOT:
54 #endif
55 case EFBIG:
56 case ENOSPC:
57 return NBD_ENOSPC;
58 case EOVERFLOW:
59 return NBD_EOVERFLOW;
60 case ENOTSUP:
61 #if ENOTSUP != EOPNOTSUPP
62 case EOPNOTSUPP:
63 #endif
64 return NBD_ENOTSUP;
65 case ESHUTDOWN:
66 return NBD_ESHUTDOWN;
67 case EINVAL:
68 default:
69 return NBD_EINVAL;
73 /* Definitions for opaque data types */
75 typedef struct NBDRequestData NBDRequestData;
77 struct NBDRequestData {
78 QSIMPLEQ_ENTRY(NBDRequestData) entry;
79 NBDClient *client;
80 uint8_t *data;
81 bool complete;
84 struct NBDExport {
85 BlockExport common;
87 char *name;
88 char *description;
89 uint64_t size;
90 uint16_t nbdflags;
91 QTAILQ_HEAD(, NBDClient) clients;
92 QTAILQ_ENTRY(NBDExport) next;
94 BlockBackend *eject_notifier_blk;
95 Notifier eject_notifier;
97 BdrvDirtyBitmap *export_bitmap;
98 char *export_bitmap_context;
101 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
103 /* NBDExportMetaContexts represents a list of contexts to be exported,
104 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
105 * NBD_OPT_LIST_META_CONTEXT. */
106 typedef struct NBDExportMetaContexts {
107 NBDExport *exp;
108 bool valid; /* means that negotiation of the option finished without
109 errors */
110 bool base_allocation; /* export base:allocation context (block status) */
111 bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
112 } NBDExportMetaContexts;
114 struct NBDClient {
115 int refcount;
116 void (*close_fn)(NBDClient *client, bool negotiated);
118 NBDExport *exp;
119 QCryptoTLSCreds *tlscreds;
120 char *tlsauthz;
121 QIOChannelSocket *sioc; /* The underlying data channel */
122 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
124 Coroutine *recv_coroutine;
126 CoMutex send_lock;
127 Coroutine *send_coroutine;
129 QTAILQ_ENTRY(NBDClient) next;
130 int nb_requests;
131 bool closing;
133 uint32_t check_align; /* If non-zero, check for aligned client requests */
135 bool structured_reply;
136 NBDExportMetaContexts export_meta;
138 uint32_t opt; /* Current option being negotiated */
139 uint32_t optlen; /* remaining length of data in ioc for the option being
140 negotiated now */
143 static void nbd_client_receive_next_request(NBDClient *client);
145 /* Basic flow for negotiation
147 Server Client
148 Negotiate
152 Server Client
153 Negotiate #1
154 Option
155 Negotiate #2
157 ----
159 followed by
161 Server Client
162 Request
163 Response
164 Request
165 Response
168 Request (type == 2)
172 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
173 uint32_t type, uint32_t length)
175 stq_be_p(&rep->magic, NBD_REP_MAGIC);
176 stl_be_p(&rep->option, option);
177 stl_be_p(&rep->type, type);
178 stl_be_p(&rep->length, length);
181 /* Send a reply header, including length, but no payload.
182 * Return -errno on error, 0 on success. */
183 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
184 uint32_t len, Error **errp)
186 NBDOptionReply rep;
188 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
189 type, nbd_rep_lookup(type), len);
191 assert(len < NBD_MAX_BUFFER_SIZE);
193 set_be_option_rep(&rep, client->opt, type, len);
194 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
197 /* Send a reply header with default 0 length.
198 * Return -errno on error, 0 on success. */
199 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
200 Error **errp)
202 return nbd_negotiate_send_rep_len(client, type, 0, errp);
205 /* Send an error reply.
206 * Return -errno on error, 0 on success. */
207 static int GCC_FMT_ATTR(4, 0)
208 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
209 Error **errp, const char *fmt, va_list va)
211 ERRP_GUARD();
212 g_autofree char *msg = NULL;
213 int ret;
214 size_t len;
216 msg = g_strdup_vprintf(fmt, va);
217 len = strlen(msg);
218 assert(len < NBD_MAX_STRING_SIZE);
219 trace_nbd_negotiate_send_rep_err(msg);
220 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
221 if (ret < 0) {
222 return ret;
224 if (nbd_write(client->ioc, msg, len, errp) < 0) {
225 error_prepend(errp, "write failed (error message): ");
226 return -EIO;
229 return 0;
233 * Return a malloc'd copy of @name suitable for use in an error reply.
235 static char *
236 nbd_sanitize_name(const char *name)
238 if (strnlen(name, 80) < 80) {
239 return g_strdup(name);
241 /* XXX Should we also try to sanitize any control characters? */
242 return g_strdup_printf("%.80s...", name);
245 /* Send an error reply.
246 * Return -errno on error, 0 on success. */
247 static int GCC_FMT_ATTR(4, 5)
248 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
249 Error **errp, const char *fmt, ...)
251 va_list va;
252 int ret;
254 va_start(va, fmt);
255 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
256 va_end(va);
257 return ret;
260 /* Drop remainder of the current option, and send a reply with the
261 * given error type and message. Return -errno on read or write
262 * failure; or 0 if connection is still live. */
263 static int GCC_FMT_ATTR(4, 0)
264 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
265 const char *fmt, va_list va)
267 int ret = nbd_drop(client->ioc, client->optlen, errp);
269 client->optlen = 0;
270 if (!ret) {
271 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
273 return ret;
276 static int GCC_FMT_ATTR(4, 5)
277 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
278 const char *fmt, ...)
280 int ret;
281 va_list va;
283 va_start(va, fmt);
284 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
285 va_end(va);
287 return ret;
290 static int GCC_FMT_ATTR(3, 4)
291 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
293 int ret;
294 va_list va;
296 va_start(va, fmt);
297 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
298 va_end(va);
300 return ret;
303 /* Read size bytes from the unparsed payload of the current option.
304 * If @check_nul, require that no NUL bytes appear in buffer.
305 * Return -errno on I/O error, 0 if option was completely handled by
306 * sending a reply about inconsistent lengths, or 1 on success. */
307 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
308 bool check_nul, Error **errp)
310 if (size > client->optlen) {
311 return nbd_opt_invalid(client, errp,
312 "Inconsistent lengths in option %s",
313 nbd_opt_lookup(client->opt));
315 client->optlen -= size;
316 if (qio_channel_read_all(client->ioc, buffer, size, errp) < 0) {
317 return -EIO;
320 if (check_nul && strnlen(buffer, size) != size) {
321 return nbd_opt_invalid(client, errp,
322 "Unexpected embedded NUL in option %s",
323 nbd_opt_lookup(client->opt));
325 return 1;
328 /* Drop size bytes from the unparsed payload of the current option.
329 * Return -errno on I/O error, 0 if option was completely handled by
330 * sending a reply about inconsistent lengths, or 1 on success. */
331 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
333 if (size > client->optlen) {
334 return nbd_opt_invalid(client, errp,
335 "Inconsistent lengths in option %s",
336 nbd_opt_lookup(client->opt));
338 client->optlen -= size;
339 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
342 /* nbd_opt_read_name
344 * Read a string with the format:
345 * uint32_t len (<= NBD_MAX_STRING_SIZE)
346 * len bytes string (not 0-terminated)
348 * On success, @name will be allocated.
349 * If @length is non-null, it will be set to the actual string length.
351 * Return -errno on I/O error, 0 if option was completely handled by
352 * sending a reply about inconsistent lengths, or 1 on success.
354 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
355 Error **errp)
357 int ret;
358 uint32_t len;
359 g_autofree char *local_name = NULL;
361 *name = NULL;
362 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
363 if (ret <= 0) {
364 return ret;
366 len = cpu_to_be32(len);
368 if (len > NBD_MAX_STRING_SIZE) {
369 return nbd_opt_invalid(client, errp,
370 "Invalid name length: %" PRIu32, len);
373 local_name = g_malloc(len + 1);
374 ret = nbd_opt_read(client, local_name, len, true, errp);
375 if (ret <= 0) {
376 return ret;
378 local_name[len] = '\0';
380 if (length) {
381 *length = len;
383 *name = g_steal_pointer(&local_name);
385 return 1;
388 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
389 * Return -errno on error, 0 on success. */
390 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
391 Error **errp)
393 ERRP_GUARD();
394 size_t name_len, desc_len;
395 uint32_t len;
396 const char *name = exp->name ? exp->name : "";
397 const char *desc = exp->description ? exp->description : "";
398 QIOChannel *ioc = client->ioc;
399 int ret;
401 trace_nbd_negotiate_send_rep_list(name, desc);
402 name_len = strlen(name);
403 desc_len = strlen(desc);
404 assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
405 len = name_len + desc_len + sizeof(len);
406 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
407 if (ret < 0) {
408 return ret;
411 len = cpu_to_be32(name_len);
412 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
413 error_prepend(errp, "write failed (name length): ");
414 return -EINVAL;
417 if (nbd_write(ioc, name, name_len, errp) < 0) {
418 error_prepend(errp, "write failed (name buffer): ");
419 return -EINVAL;
422 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
423 error_prepend(errp, "write failed (description buffer): ");
424 return -EINVAL;
427 return 0;
430 /* Process the NBD_OPT_LIST command, with a potential series of replies.
431 * Return -errno on error, 0 on success. */
432 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
434 NBDExport *exp;
435 assert(client->opt == NBD_OPT_LIST);
437 /* For each export, send a NBD_REP_SERVER reply. */
438 QTAILQ_FOREACH(exp, &exports, next) {
439 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
440 return -EINVAL;
443 /* Finish with a NBD_REP_ACK. */
444 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
447 static void nbd_check_meta_export(NBDClient *client)
449 client->export_meta.valid &= client->exp == client->export_meta.exp;
452 /* Send a reply to NBD_OPT_EXPORT_NAME.
453 * Return -errno on error, 0 on success. */
454 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
455 Error **errp)
457 ERRP_GUARD();
458 g_autofree char *name = NULL;
459 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
460 size_t len;
461 int ret;
462 uint16_t myflags;
464 /* Client sends:
465 [20 .. xx] export name (length bytes)
466 Server replies:
467 [ 0 .. 7] size
468 [ 8 .. 9] export flags
469 [10 .. 133] reserved (0) [unless no_zeroes]
471 trace_nbd_negotiate_handle_export_name();
472 if (client->optlen > NBD_MAX_STRING_SIZE) {
473 error_setg(errp, "Bad length received");
474 return -EINVAL;
476 name = g_malloc(client->optlen + 1);
477 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
478 return -EIO;
480 name[client->optlen] = '\0';
481 client->optlen = 0;
483 trace_nbd_negotiate_handle_export_name_request(name);
485 client->exp = nbd_export_find(name);
486 if (!client->exp) {
487 error_setg(errp, "export not found");
488 return -EINVAL;
491 myflags = client->exp->nbdflags;
492 if (client->structured_reply) {
493 myflags |= NBD_FLAG_SEND_DF;
495 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
496 stq_be_p(buf, client->exp->size);
497 stw_be_p(buf + 8, myflags);
498 len = no_zeroes ? 10 : sizeof(buf);
499 ret = nbd_write(client->ioc, buf, len, errp);
500 if (ret < 0) {
501 error_prepend(errp, "write failed: ");
502 return ret;
505 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
506 blk_exp_ref(&client->exp->common);
507 nbd_check_meta_export(client);
509 return 0;
512 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
513 * The buffer does NOT include the info type prefix.
514 * Return -errno on error, 0 if ready to send more. */
515 static int nbd_negotiate_send_info(NBDClient *client,
516 uint16_t info, uint32_t length, void *buf,
517 Error **errp)
519 int rc;
521 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
522 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
523 sizeof(info) + length, errp);
524 if (rc < 0) {
525 return rc;
527 info = cpu_to_be16(info);
528 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
529 return -EIO;
531 if (nbd_write(client->ioc, buf, length, errp) < 0) {
532 return -EIO;
534 return 0;
537 /* nbd_reject_length: Handle any unexpected payload.
538 * @fatal requests that we quit talking to the client, even if we are able
539 * to successfully send an error reply.
540 * Return:
541 * -errno transmission error occurred or @fatal was requested, errp is set
542 * 0 error message successfully sent to client, errp is not set
544 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
546 int ret;
548 assert(client->optlen);
549 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
550 nbd_opt_lookup(client->opt));
551 if (fatal && !ret) {
552 error_setg(errp, "option '%s' has unexpected length",
553 nbd_opt_lookup(client->opt));
554 return -EINVAL;
556 return ret;
559 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
560 * Return -errno on error, 0 if ready for next option, and 1 to move
561 * into transmission phase. */
562 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
564 int rc;
565 g_autofree char *name = NULL;
566 NBDExport *exp;
567 uint16_t requests;
568 uint16_t request;
569 uint32_t namelen = 0;
570 bool sendname = false;
571 bool blocksize = false;
572 uint32_t sizes[3];
573 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
574 uint32_t check_align = 0;
575 uint16_t myflags;
577 /* Client sends:
578 4 bytes: L, name length (can be 0)
579 L bytes: export name
580 2 bytes: N, number of requests (can be 0)
581 N * 2 bytes: N requests
583 rc = nbd_opt_read_name(client, &name, &namelen, errp);
584 if (rc <= 0) {
585 return rc;
587 trace_nbd_negotiate_handle_export_name_request(name);
589 rc = nbd_opt_read(client, &requests, sizeof(requests), false, errp);
590 if (rc <= 0) {
591 return rc;
593 requests = be16_to_cpu(requests);
594 trace_nbd_negotiate_handle_info_requests(requests);
595 while (requests--) {
596 rc = nbd_opt_read(client, &request, sizeof(request), false, errp);
597 if (rc <= 0) {
598 return rc;
600 request = be16_to_cpu(request);
601 trace_nbd_negotiate_handle_info_request(request,
602 nbd_info_lookup(request));
603 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
604 * everything else is either a request we don't know or
605 * something we send regardless of request */
606 switch (request) {
607 case NBD_INFO_NAME:
608 sendname = true;
609 break;
610 case NBD_INFO_BLOCK_SIZE:
611 blocksize = true;
612 break;
615 if (client->optlen) {
616 return nbd_reject_length(client, false, errp);
619 exp = nbd_export_find(name);
620 if (!exp) {
621 g_autofree char *sane_name = nbd_sanitize_name(name);
623 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
624 errp, "export '%s' not present",
625 sane_name);
628 /* Don't bother sending NBD_INFO_NAME unless client requested it */
629 if (sendname) {
630 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
631 errp);
632 if (rc < 0) {
633 return rc;
637 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
638 * client request */
639 if (exp->description) {
640 size_t len = strlen(exp->description);
642 assert(len <= NBD_MAX_STRING_SIZE);
643 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
644 len, exp->description, errp);
645 if (rc < 0) {
646 return rc;
650 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
651 * according to whether the client requested it, and according to
652 * whether this is OPT_INFO or OPT_GO. */
653 /* minimum - 1 for back-compat, or actual if client will obey it. */
654 if (client->opt == NBD_OPT_INFO || blocksize) {
655 check_align = sizes[0] = blk_get_request_alignment(exp->common.blk);
656 } else {
657 sizes[0] = 1;
659 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
660 /* preferred - Hard-code to 4096 for now.
661 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
662 sizes[1] = MAX(4096, sizes[0]);
663 /* maximum - At most 32M, but smaller as appropriate. */
664 sizes[2] = MIN(blk_get_max_transfer(exp->common.blk), NBD_MAX_BUFFER_SIZE);
665 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
666 sizes[0] = cpu_to_be32(sizes[0]);
667 sizes[1] = cpu_to_be32(sizes[1]);
668 sizes[2] = cpu_to_be32(sizes[2]);
669 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
670 sizeof(sizes), sizes, errp);
671 if (rc < 0) {
672 return rc;
675 /* Send NBD_INFO_EXPORT always */
676 myflags = exp->nbdflags;
677 if (client->structured_reply) {
678 myflags |= NBD_FLAG_SEND_DF;
680 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
681 stq_be_p(buf, exp->size);
682 stw_be_p(buf + 8, myflags);
683 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
684 sizeof(buf), buf, errp);
685 if (rc < 0) {
686 return rc;
690 * If the client is just asking for NBD_OPT_INFO, but forgot to
691 * request block sizes in a situation that would impact
692 * performance, then return an error. But for NBD_OPT_GO, we
693 * tolerate all clients, regardless of alignments.
695 if (client->opt == NBD_OPT_INFO && !blocksize &&
696 blk_get_request_alignment(exp->common.blk) > 1) {
697 return nbd_negotiate_send_rep_err(client,
698 NBD_REP_ERR_BLOCK_SIZE_REQD,
699 errp,
700 "request NBD_INFO_BLOCK_SIZE to "
701 "use this export");
704 /* Final reply */
705 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
706 if (rc < 0) {
707 return rc;
710 if (client->opt == NBD_OPT_GO) {
711 client->exp = exp;
712 client->check_align = check_align;
713 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
714 blk_exp_ref(&client->exp->common);
715 nbd_check_meta_export(client);
716 rc = 1;
718 return rc;
722 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
723 * new channel for all further (now-encrypted) communication. */
724 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
725 Error **errp)
727 QIOChannel *ioc;
728 QIOChannelTLS *tioc;
729 struct NBDTLSHandshakeData data = { 0 };
731 assert(client->opt == NBD_OPT_STARTTLS);
733 trace_nbd_negotiate_handle_starttls();
734 ioc = client->ioc;
736 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
737 return NULL;
740 tioc = qio_channel_tls_new_server(ioc,
741 client->tlscreds,
742 client->tlsauthz,
743 errp);
744 if (!tioc) {
745 return NULL;
748 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
749 trace_nbd_negotiate_handle_starttls_handshake();
750 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
751 qio_channel_tls_handshake(tioc,
752 nbd_tls_handshake,
753 &data,
754 NULL,
755 NULL);
757 if (!data.complete) {
758 g_main_loop_run(data.loop);
760 g_main_loop_unref(data.loop);
761 if (data.error) {
762 object_unref(OBJECT(tioc));
763 error_propagate(errp, data.error);
764 return NULL;
767 return QIO_CHANNEL(tioc);
770 /* nbd_negotiate_send_meta_context
772 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
774 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
776 static int nbd_negotiate_send_meta_context(NBDClient *client,
777 const char *context,
778 uint32_t context_id,
779 Error **errp)
781 NBDOptionReplyMetaContext opt;
782 struct iovec iov[] = {
783 {.iov_base = &opt, .iov_len = sizeof(opt)},
784 {.iov_base = (void *)context, .iov_len = strlen(context)}
787 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
788 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
789 context_id = 0;
792 trace_nbd_negotiate_meta_query_reply(context, context_id);
793 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
794 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
795 stl_be_p(&opt.context_id, context_id);
797 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
800 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
801 * @match is never set to false.
803 * Return -errno on I/O error, 0 if option was completely handled by
804 * sending a reply about inconsistent lengths, or 1 on success.
806 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
807 * It only means that there are no errors.
809 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
810 Error **errp)
812 int ret;
813 char *query;
814 size_t len = strlen(pattern);
816 assert(len);
818 query = g_malloc(len);
819 ret = nbd_opt_read(client, query, len, true, errp);
820 if (ret <= 0) {
821 g_free(query);
822 return ret;
825 if (strncmp(query, pattern, len) == 0) {
826 trace_nbd_negotiate_meta_query_parse(pattern);
827 *match = true;
828 } else {
829 trace_nbd_negotiate_meta_query_skip("pattern not matched");
831 g_free(query);
833 return 1;
837 * Read @len bytes, and set @match to true if they match @pattern, or if @len
838 * is 0 and the client is performing _LIST_. @match is never set to false.
840 * Return -errno on I/O error, 0 if option was completely handled by
841 * sending a reply about inconsistent lengths, or 1 on success.
843 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
844 * It only means that there are no errors.
846 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
847 uint32_t len, bool *match, Error **errp)
849 if (len == 0) {
850 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
851 *match = true;
853 trace_nbd_negotiate_meta_query_parse("empty");
854 return 1;
857 if (len != strlen(pattern)) {
858 trace_nbd_negotiate_meta_query_skip("different lengths");
859 return nbd_opt_skip(client, len, errp);
862 return nbd_meta_pattern(client, pattern, match, errp);
865 /* nbd_meta_base_query
867 * Handle queries to 'base' namespace. For now, only the base:allocation
868 * context is available. 'len' is the amount of text remaining to be read from
869 * the current name, after the 'base:' portion has been stripped.
871 * Return -errno on I/O error, 0 if option was completely handled by
872 * sending a reply about inconsistent lengths, or 1 on success.
874 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
875 uint32_t len, Error **errp)
877 return nbd_meta_empty_or_pattern(client, "allocation", len,
878 &meta->base_allocation, errp);
881 /* nbd_meta_bitmap_query
883 * Handle query to 'qemu:' namespace.
884 * @len is the amount of text remaining to be read from the current name, after
885 * the 'qemu:' portion has been stripped.
887 * Return -errno on I/O error, 0 if option was completely handled by
888 * sending a reply about inconsistent lengths, or 1 on success. */
889 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
890 uint32_t len, Error **errp)
892 bool dirty_bitmap = false;
893 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
894 int ret;
896 if (!meta->exp->export_bitmap) {
897 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
898 return nbd_opt_skip(client, len, errp);
901 if (len == 0) {
902 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
903 meta->bitmap = true;
905 trace_nbd_negotiate_meta_query_parse("empty");
906 return 1;
909 if (len < dirty_bitmap_len) {
910 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
911 return nbd_opt_skip(client, len, errp);
914 len -= dirty_bitmap_len;
915 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
916 if (ret <= 0) {
917 return ret;
919 if (!dirty_bitmap) {
920 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
921 return nbd_opt_skip(client, len, errp);
924 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
926 return nbd_meta_empty_or_pattern(
927 client, meta->exp->export_bitmap_context +
928 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
931 /* nbd_negotiate_meta_query
933 * Parse namespace name and call corresponding function to parse body of the
934 * query.
936 * The only supported namespaces are 'base' and 'qemu'.
938 * The function aims not wasting time and memory to read long unknown namespace
939 * names.
941 * Return -errno on I/O error, 0 if option was completely handled by
942 * sending a reply about inconsistent lengths, or 1 on success. */
943 static int nbd_negotiate_meta_query(NBDClient *client,
944 NBDExportMetaContexts *meta, Error **errp)
947 * Both 'qemu' and 'base' namespaces have length = 5 including a
948 * colon. If another length namespace is later introduced, this
949 * should certainly be refactored.
951 int ret;
952 size_t ns_len = 5;
953 char ns[5];
954 uint32_t len;
956 ret = nbd_opt_read(client, &len, sizeof(len), false, errp);
957 if (ret <= 0) {
958 return ret;
960 len = cpu_to_be32(len);
962 if (len > NBD_MAX_STRING_SIZE) {
963 trace_nbd_negotiate_meta_query_skip("length too long");
964 return nbd_opt_skip(client, len, errp);
966 if (len < ns_len) {
967 trace_nbd_negotiate_meta_query_skip("length too short");
968 return nbd_opt_skip(client, len, errp);
971 len -= ns_len;
972 ret = nbd_opt_read(client, ns, ns_len, true, errp);
973 if (ret <= 0) {
974 return ret;
977 if (!strncmp(ns, "base:", ns_len)) {
978 trace_nbd_negotiate_meta_query_parse("base:");
979 return nbd_meta_base_query(client, meta, len, errp);
980 } else if (!strncmp(ns, "qemu:", ns_len)) {
981 trace_nbd_negotiate_meta_query_parse("qemu:");
982 return nbd_meta_qemu_query(client, meta, len, errp);
985 trace_nbd_negotiate_meta_query_skip("unknown namespace");
986 return nbd_opt_skip(client, len, errp);
989 /* nbd_negotiate_meta_queries
990 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
992 * Return -errno on I/O error, or 0 if option was completely handled. */
993 static int nbd_negotiate_meta_queries(NBDClient *client,
994 NBDExportMetaContexts *meta, Error **errp)
996 int ret;
997 g_autofree char *export_name = NULL;
998 NBDExportMetaContexts local_meta;
999 uint32_t nb_queries;
1000 int i;
1002 if (!client->structured_reply) {
1003 return nbd_opt_invalid(client, errp,
1004 "request option '%s' when structured reply "
1005 "is not negotiated",
1006 nbd_opt_lookup(client->opt));
1009 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1010 /* Only change the caller's meta on SET. */
1011 meta = &local_meta;
1014 memset(meta, 0, sizeof(*meta));
1016 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1017 if (ret <= 0) {
1018 return ret;
1021 meta->exp = nbd_export_find(export_name);
1022 if (meta->exp == NULL) {
1023 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1025 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1026 "export '%s' not present", sane_name);
1029 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), false, errp);
1030 if (ret <= 0) {
1031 return ret;
1033 nb_queries = cpu_to_be32(nb_queries);
1034 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1035 export_name, nb_queries);
1037 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1038 /* enable all known contexts */
1039 meta->base_allocation = true;
1040 meta->bitmap = !!meta->exp->export_bitmap;
1041 } else {
1042 for (i = 0; i < nb_queries; ++i) {
1043 ret = nbd_negotiate_meta_query(client, meta, errp);
1044 if (ret <= 0) {
1045 return ret;
1050 if (meta->base_allocation) {
1051 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1052 NBD_META_ID_BASE_ALLOCATION,
1053 errp);
1054 if (ret < 0) {
1055 return ret;
1059 if (meta->bitmap) {
1060 ret = nbd_negotiate_send_meta_context(client,
1061 meta->exp->export_bitmap_context,
1062 NBD_META_ID_DIRTY_BITMAP,
1063 errp);
1064 if (ret < 0) {
1065 return ret;
1069 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1070 if (ret == 0) {
1071 meta->valid = true;
1074 return ret;
1077 /* nbd_negotiate_options
1078 * Process all NBD_OPT_* client option commands, during fixed newstyle
1079 * negotiation.
1080 * Return:
1081 * -errno on error, errp is set
1082 * 0 on successful negotiation, errp is not set
1083 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1084 * errp is not set
1086 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1088 uint32_t flags;
1089 bool fixedNewstyle = false;
1090 bool no_zeroes = false;
1092 /* Client sends:
1093 [ 0 .. 3] client flags
1095 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1096 [ 0 .. 7] NBD_OPTS_MAGIC
1097 [ 8 .. 11] NBD option
1098 [12 .. 15] Data length
1099 ... Rest of request
1101 [ 0 .. 7] NBD_OPTS_MAGIC
1102 [ 8 .. 11] Second NBD option
1103 [12 .. 15] Data length
1104 ... Rest of request
1107 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1108 return -EIO;
1110 trace_nbd_negotiate_options_flags(flags);
1111 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1112 fixedNewstyle = true;
1113 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1115 if (flags & NBD_FLAG_C_NO_ZEROES) {
1116 no_zeroes = true;
1117 flags &= ~NBD_FLAG_C_NO_ZEROES;
1119 if (flags != 0) {
1120 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1121 return -EINVAL;
1124 while (1) {
1125 int ret;
1126 uint32_t option, length;
1127 uint64_t magic;
1129 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1130 return -EINVAL;
1132 trace_nbd_negotiate_options_check_magic(magic);
1133 if (magic != NBD_OPTS_MAGIC) {
1134 error_setg(errp, "Bad magic received");
1135 return -EINVAL;
1138 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1139 return -EINVAL;
1141 client->opt = option;
1143 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1144 return -EINVAL;
1146 assert(!client->optlen);
1147 client->optlen = length;
1149 if (length > NBD_MAX_BUFFER_SIZE) {
1150 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1151 length, NBD_MAX_BUFFER_SIZE);
1152 return -EINVAL;
1155 trace_nbd_negotiate_options_check_option(option,
1156 nbd_opt_lookup(option));
1157 if (client->tlscreds &&
1158 client->ioc == (QIOChannel *)client->sioc) {
1159 QIOChannel *tioc;
1160 if (!fixedNewstyle) {
1161 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1162 return -EINVAL;
1164 switch (option) {
1165 case NBD_OPT_STARTTLS:
1166 if (length) {
1167 /* Unconditionally drop the connection if the client
1168 * can't start a TLS negotiation correctly */
1169 return nbd_reject_length(client, true, errp);
1171 tioc = nbd_negotiate_handle_starttls(client, errp);
1172 if (!tioc) {
1173 return -EIO;
1175 ret = 0;
1176 object_unref(OBJECT(client->ioc));
1177 client->ioc = QIO_CHANNEL(tioc);
1178 break;
1180 case NBD_OPT_EXPORT_NAME:
1181 /* No way to return an error to client, so drop connection */
1182 error_setg(errp, "Option 0x%x not permitted before TLS",
1183 option);
1184 return -EINVAL;
1186 default:
1187 /* Let the client keep trying, unless they asked to
1188 * quit. Always try to give an error back to the
1189 * client; but when replying to OPT_ABORT, be aware
1190 * that the client may hang up before receiving the
1191 * error, in which case we are fine ignoring the
1192 * resulting EPIPE. */
1193 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1194 option == NBD_OPT_ABORT ? NULL : errp,
1195 "Option 0x%" PRIx32
1196 " not permitted before TLS", option);
1197 if (option == NBD_OPT_ABORT) {
1198 return 1;
1200 break;
1202 } else if (fixedNewstyle) {
1203 switch (option) {
1204 case NBD_OPT_LIST:
1205 if (length) {
1206 ret = nbd_reject_length(client, false, errp);
1207 } else {
1208 ret = nbd_negotiate_handle_list(client, errp);
1210 break;
1212 case NBD_OPT_ABORT:
1213 /* NBD spec says we must try to reply before
1214 * disconnecting, but that we must also tolerate
1215 * guests that don't wait for our reply. */
1216 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1217 return 1;
1219 case NBD_OPT_EXPORT_NAME:
1220 return nbd_negotiate_handle_export_name(client, no_zeroes,
1221 errp);
1223 case NBD_OPT_INFO:
1224 case NBD_OPT_GO:
1225 ret = nbd_negotiate_handle_info(client, errp);
1226 if (ret == 1) {
1227 assert(option == NBD_OPT_GO);
1228 return 0;
1230 break;
1232 case NBD_OPT_STARTTLS:
1233 if (length) {
1234 ret = nbd_reject_length(client, false, errp);
1235 } else if (client->tlscreds) {
1236 ret = nbd_negotiate_send_rep_err(client,
1237 NBD_REP_ERR_INVALID, errp,
1238 "TLS already enabled");
1239 } else {
1240 ret = nbd_negotiate_send_rep_err(client,
1241 NBD_REP_ERR_POLICY, errp,
1242 "TLS not configured");
1244 break;
1246 case NBD_OPT_STRUCTURED_REPLY:
1247 if (length) {
1248 ret = nbd_reject_length(client, false, errp);
1249 } else if (client->structured_reply) {
1250 ret = nbd_negotiate_send_rep_err(
1251 client, NBD_REP_ERR_INVALID, errp,
1252 "structured reply already negotiated");
1253 } else {
1254 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1255 client->structured_reply = true;
1257 break;
1259 case NBD_OPT_LIST_META_CONTEXT:
1260 case NBD_OPT_SET_META_CONTEXT:
1261 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1262 errp);
1263 break;
1265 default:
1266 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1267 "Unsupported option %" PRIu32 " (%s)",
1268 option, nbd_opt_lookup(option));
1269 break;
1271 } else {
1273 * If broken new-style we should drop the connection
1274 * for anything except NBD_OPT_EXPORT_NAME
1276 switch (option) {
1277 case NBD_OPT_EXPORT_NAME:
1278 return nbd_negotiate_handle_export_name(client, no_zeroes,
1279 errp);
1281 default:
1282 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1283 option, nbd_opt_lookup(option));
1284 return -EINVAL;
1287 if (ret < 0) {
1288 return ret;
1293 /* nbd_negotiate
1294 * Return:
1295 * -errno on error, errp is set
1296 * 0 on successful negotiation, errp is not set
1297 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1298 * errp is not set
1300 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1302 ERRP_GUARD();
1303 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1304 int ret;
1306 /* Old style negotiation header, no room for options
1307 [ 0 .. 7] passwd ("NBDMAGIC")
1308 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1309 [16 .. 23] size
1310 [24 .. 27] export flags (zero-extended)
1311 [28 .. 151] reserved (0)
1313 New style negotiation header, client can send options
1314 [ 0 .. 7] passwd ("NBDMAGIC")
1315 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1316 [16 .. 17] server flags (0)
1317 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1320 qio_channel_set_blocking(client->ioc, false, NULL);
1322 trace_nbd_negotiate_begin();
1323 memcpy(buf, "NBDMAGIC", 8);
1325 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1326 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1328 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1329 error_prepend(errp, "write failed: ");
1330 return -EINVAL;
1332 ret = nbd_negotiate_options(client, errp);
1333 if (ret != 0) {
1334 if (ret < 0) {
1335 error_prepend(errp, "option negotiation failed: ");
1337 return ret;
1340 /* Attach the channel to the same AioContext as the export */
1341 if (client->exp && client->exp->common.ctx) {
1342 qio_channel_attach_aio_context(client->ioc, client->exp->common.ctx);
1345 assert(!client->optlen);
1346 trace_nbd_negotiate_success();
1348 return 0;
1351 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1352 Error **errp)
1354 uint8_t buf[NBD_REQUEST_SIZE];
1355 uint32_t magic;
1356 int ret;
1358 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1359 if (ret < 0) {
1360 return ret;
1363 /* Request
1364 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1365 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1366 [ 6 .. 7] type (NBD_CMD_READ, ...)
1367 [ 8 .. 15] handle
1368 [16 .. 23] from
1369 [24 .. 27] len
1372 magic = ldl_be_p(buf);
1373 request->flags = lduw_be_p(buf + 4);
1374 request->type = lduw_be_p(buf + 6);
1375 request->handle = ldq_be_p(buf + 8);
1376 request->from = ldq_be_p(buf + 16);
1377 request->len = ldl_be_p(buf + 24);
1379 trace_nbd_receive_request(magic, request->flags, request->type,
1380 request->from, request->len);
1382 if (magic != NBD_REQUEST_MAGIC) {
1383 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1384 return -EINVAL;
1386 return 0;
1389 #define MAX_NBD_REQUESTS 16
1391 void nbd_client_get(NBDClient *client)
1393 client->refcount++;
1396 void nbd_client_put(NBDClient *client)
1398 if (--client->refcount == 0) {
1399 /* The last reference should be dropped by client->close,
1400 * which is called by client_close.
1402 assert(client->closing);
1404 qio_channel_detach_aio_context(client->ioc);
1405 object_unref(OBJECT(client->sioc));
1406 object_unref(OBJECT(client->ioc));
1407 if (client->tlscreds) {
1408 object_unref(OBJECT(client->tlscreds));
1410 g_free(client->tlsauthz);
1411 if (client->exp) {
1412 QTAILQ_REMOVE(&client->exp->clients, client, next);
1413 blk_exp_unref(&client->exp->common);
1415 g_free(client);
1419 static void client_close(NBDClient *client, bool negotiated)
1421 if (client->closing) {
1422 return;
1425 client->closing = true;
1427 /* Force requests to finish. They will drop their own references,
1428 * then we'll close the socket and free the NBDClient.
1430 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1431 NULL);
1433 /* Also tell the client, so that they release their reference. */
1434 if (client->close_fn) {
1435 client->close_fn(client, negotiated);
1439 static NBDRequestData *nbd_request_get(NBDClient *client)
1441 NBDRequestData *req;
1443 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1444 client->nb_requests++;
1446 req = g_new0(NBDRequestData, 1);
1447 nbd_client_get(client);
1448 req->client = client;
1449 return req;
1452 static void nbd_request_put(NBDRequestData *req)
1454 NBDClient *client = req->client;
1456 if (req->data) {
1457 qemu_vfree(req->data);
1459 g_free(req);
1461 client->nb_requests--;
1462 nbd_client_receive_next_request(client);
1464 nbd_client_put(client);
1467 static void blk_aio_attached(AioContext *ctx, void *opaque)
1469 NBDExport *exp = opaque;
1470 NBDClient *client;
1472 trace_nbd_blk_aio_attached(exp->name, ctx);
1474 exp->common.ctx = ctx;
1476 QTAILQ_FOREACH(client, &exp->clients, next) {
1477 qio_channel_attach_aio_context(client->ioc, ctx);
1478 if (client->recv_coroutine) {
1479 aio_co_schedule(ctx, client->recv_coroutine);
1481 if (client->send_coroutine) {
1482 aio_co_schedule(ctx, client->send_coroutine);
1487 static void blk_aio_detach(void *opaque)
1489 NBDExport *exp = opaque;
1490 NBDClient *client;
1492 trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
1494 QTAILQ_FOREACH(client, &exp->clients, next) {
1495 qio_channel_detach_aio_context(client->ioc);
1498 exp->common.ctx = NULL;
1501 static void nbd_eject_notifier(Notifier *n, void *data)
1503 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1505 blk_exp_request_shutdown(&exp->common);
1508 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1510 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1511 assert(exp->drv == &blk_exp_nbd);
1512 assert(nbd_exp->eject_notifier_blk == NULL);
1514 blk_ref(blk);
1515 nbd_exp->eject_notifier_blk = blk;
1516 nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1517 blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1520 static int nbd_export_create(BlockExport *blk_exp, BlockExportOptions *exp_args,
1521 Error **errp)
1523 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1524 BlockExportOptionsNbd *arg = &exp_args->u.nbd;
1525 BlockBackend *blk = blk_exp->blk;
1526 int64_t size;
1527 uint64_t perm, shared_perm;
1528 bool readonly = !exp_args->writable;
1529 bool shared = !exp_args->writable;
1530 int ret;
1532 assert(exp_args->type == BLOCK_EXPORT_TYPE_NBD);
1534 if (!nbd_server_is_running()) {
1535 error_setg(errp, "NBD server not running");
1536 return -EINVAL;
1539 if (!arg->has_name) {
1540 arg->name = exp_args->node_name;
1543 if (strlen(arg->name) > NBD_MAX_STRING_SIZE) {
1544 error_setg(errp, "export name '%s' too long", arg->name);
1545 return -EINVAL;
1548 if (arg->description && strlen(arg->description) > NBD_MAX_STRING_SIZE) {
1549 error_setg(errp, "description '%s' too long", arg->description);
1550 return -EINVAL;
1553 if (nbd_export_find(arg->name)) {
1554 error_setg(errp, "NBD server already has export named '%s'", arg->name);
1555 return -EEXIST;
1558 size = blk_getlength(blk);
1559 if (size < 0) {
1560 error_setg_errno(errp, -size,
1561 "Failed to determine the NBD export's length");
1562 return size;
1565 /* Don't allow resize while the NBD server is running, otherwise we don't
1566 * care what happens with the node. */
1567 blk_get_perm(blk, &perm, &shared_perm);
1568 ret = blk_set_perm(blk, perm, shared_perm & ~BLK_PERM_RESIZE, errp);
1569 if (ret < 0) {
1570 return ret;
1573 blk_set_allow_aio_context_change(blk, true);
1575 QTAILQ_INIT(&exp->clients);
1576 exp->name = g_strdup(arg->name);
1577 exp->description = g_strdup(arg->description);
1578 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1579 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1580 if (readonly) {
1581 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1582 if (shared) {
1583 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1585 } else {
1586 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1587 NBD_FLAG_SEND_FAST_ZERO);
1589 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1591 if (arg->bitmap) {
1592 BlockDriverState *bs = blk_bs(blk);
1593 BdrvDirtyBitmap *bm = NULL;
1595 while (bs) {
1596 bm = bdrv_find_dirty_bitmap(bs, arg->bitmap);
1597 if (bm != NULL) {
1598 break;
1601 bs = bdrv_filter_or_cow_bs(bs);
1604 if (bm == NULL) {
1605 ret = -ENOENT;
1606 error_setg(errp, "Bitmap '%s' is not found", arg->bitmap);
1607 goto fail;
1610 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1611 ret = -EINVAL;
1612 goto fail;
1615 if (readonly && bdrv_is_writable(bs) &&
1616 bdrv_dirty_bitmap_enabled(bm)) {
1617 ret = -EINVAL;
1618 error_setg(errp,
1619 "Enabled bitmap '%s' incompatible with readonly export",
1620 arg->bitmap);
1621 goto fail;
1624 bdrv_dirty_bitmap_set_busy(bm, true);
1625 exp->export_bitmap = bm;
1626 assert(strlen(arg->bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
1627 exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1628 arg->bitmap);
1629 assert(strlen(exp->export_bitmap_context) < NBD_MAX_STRING_SIZE);
1632 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1634 QTAILQ_INSERT_TAIL(&exports, exp, next);
1636 return 0;
1638 fail:
1639 g_free(exp->name);
1640 g_free(exp->description);
1641 return ret;
1644 NBDExport *nbd_export_find(const char *name)
1646 NBDExport *exp;
1647 QTAILQ_FOREACH(exp, &exports, next) {
1648 if (strcmp(name, exp->name) == 0) {
1649 return exp;
1653 return NULL;
1656 AioContext *
1657 nbd_export_aio_context(NBDExport *exp)
1659 return exp->common.ctx;
1662 static void nbd_export_request_shutdown(BlockExport *blk_exp)
1664 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1665 NBDClient *client, *next;
1667 blk_exp_ref(&exp->common);
1669 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1670 * close mode that stops advertising the export to new clients but
1671 * still permits existing clients to run to completion? Because of
1672 * that possibility, nbd_export_close() can be called more than
1673 * once on an export.
1675 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1676 client_close(client, true);
1678 if (exp->name) {
1679 g_free(exp->name);
1680 exp->name = NULL;
1681 QTAILQ_REMOVE(&exports, exp, next);
1683 blk_exp_unref(&exp->common);
1686 static void nbd_export_delete(BlockExport *blk_exp)
1688 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1690 assert(exp->name == NULL);
1691 assert(QTAILQ_EMPTY(&exp->clients));
1693 g_free(exp->description);
1694 exp->description = NULL;
1696 if (exp->common.blk) {
1697 if (exp->eject_notifier_blk) {
1698 notifier_remove(&exp->eject_notifier);
1699 blk_unref(exp->eject_notifier_blk);
1701 blk_remove_aio_context_notifier(exp->common.blk, blk_aio_attached,
1702 blk_aio_detach, exp);
1705 if (exp->export_bitmap) {
1706 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1707 g_free(exp->export_bitmap_context);
1711 const BlockExportDriver blk_exp_nbd = {
1712 .type = BLOCK_EXPORT_TYPE_NBD,
1713 .instance_size = sizeof(NBDExport),
1714 .create = nbd_export_create,
1715 .delete = nbd_export_delete,
1716 .request_shutdown = nbd_export_request_shutdown,
1719 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1720 unsigned niov, Error **errp)
1722 int ret;
1724 g_assert(qemu_in_coroutine());
1725 qemu_co_mutex_lock(&client->send_lock);
1726 client->send_coroutine = qemu_coroutine_self();
1728 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1730 client->send_coroutine = NULL;
1731 qemu_co_mutex_unlock(&client->send_lock);
1733 return ret;
1736 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1737 uint64_t handle)
1739 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1740 stl_be_p(&reply->error, error);
1741 stq_be_p(&reply->handle, handle);
1744 static int nbd_co_send_simple_reply(NBDClient *client,
1745 uint64_t handle,
1746 uint32_t error,
1747 void *data,
1748 size_t len,
1749 Error **errp)
1751 NBDSimpleReply reply;
1752 int nbd_err = system_errno_to_nbd_errno(error);
1753 struct iovec iov[] = {
1754 {.iov_base = &reply, .iov_len = sizeof(reply)},
1755 {.iov_base = data, .iov_len = len}
1758 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1759 len);
1760 set_be_simple_reply(&reply, nbd_err, handle);
1762 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1765 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1766 uint16_t type, uint64_t handle, uint32_t length)
1768 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1769 stw_be_p(&chunk->flags, flags);
1770 stw_be_p(&chunk->type, type);
1771 stq_be_p(&chunk->handle, handle);
1772 stl_be_p(&chunk->length, length);
1775 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1776 uint64_t handle,
1777 Error **errp)
1779 NBDStructuredReplyChunk chunk;
1780 struct iovec iov[] = {
1781 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1784 trace_nbd_co_send_structured_done(handle);
1785 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1787 return nbd_co_send_iov(client, iov, 1, errp);
1790 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1791 uint64_t handle,
1792 uint64_t offset,
1793 void *data,
1794 size_t size,
1795 bool final,
1796 Error **errp)
1798 NBDStructuredReadData chunk;
1799 struct iovec iov[] = {
1800 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1801 {.iov_base = data, .iov_len = size}
1804 assert(size);
1805 trace_nbd_co_send_structured_read(handle, offset, data, size);
1806 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1807 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1808 sizeof(chunk) - sizeof(chunk.h) + size);
1809 stq_be_p(&chunk.offset, offset);
1811 return nbd_co_send_iov(client, iov, 2, errp);
1814 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1815 uint64_t handle,
1816 uint32_t error,
1817 const char *msg,
1818 Error **errp)
1820 NBDStructuredError chunk;
1821 int nbd_err = system_errno_to_nbd_errno(error);
1822 struct iovec iov[] = {
1823 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1824 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1827 assert(nbd_err);
1828 trace_nbd_co_send_structured_error(handle, nbd_err,
1829 nbd_err_lookup(nbd_err), msg ? msg : "");
1830 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1831 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1832 stl_be_p(&chunk.error, nbd_err);
1833 stw_be_p(&chunk.message_length, iov[1].iov_len);
1835 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1838 /* Do a sparse read and send the structured reply to the client.
1839 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1840 * reported to the client, at which point this function succeeds.
1842 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1843 uint64_t handle,
1844 uint64_t offset,
1845 uint8_t *data,
1846 size_t size,
1847 Error **errp)
1849 int ret = 0;
1850 NBDExport *exp = client->exp;
1851 size_t progress = 0;
1853 while (progress < size) {
1854 int64_t pnum;
1855 int status = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
1856 offset + progress,
1857 size - progress, &pnum, NULL,
1858 NULL);
1859 bool final;
1861 if (status < 0) {
1862 char *msg = g_strdup_printf("unable to check for holes: %s",
1863 strerror(-status));
1865 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1866 errp);
1867 g_free(msg);
1868 return ret;
1870 assert(pnum && pnum <= size - progress);
1871 final = progress + pnum == size;
1872 if (status & BDRV_BLOCK_ZERO) {
1873 NBDStructuredReadHole chunk;
1874 struct iovec iov[] = {
1875 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1878 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1879 pnum);
1880 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1881 NBD_REPLY_TYPE_OFFSET_HOLE,
1882 handle, sizeof(chunk) - sizeof(chunk.h));
1883 stq_be_p(&chunk.offset, offset + progress);
1884 stl_be_p(&chunk.length, pnum);
1885 ret = nbd_co_send_iov(client, iov, 1, errp);
1886 } else {
1887 ret = blk_pread(exp->common.blk, offset + progress,
1888 data + progress, pnum);
1889 if (ret < 0) {
1890 error_setg_errno(errp, -ret, "reading from file failed");
1891 break;
1893 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1894 data + progress, pnum, final,
1895 errp);
1898 if (ret < 0) {
1899 break;
1901 progress += pnum;
1903 return ret;
1906 typedef struct NBDExtentArray {
1907 NBDExtent *extents;
1908 unsigned int nb_alloc;
1909 unsigned int count;
1910 uint64_t total_length;
1911 bool can_add;
1912 bool converted_to_be;
1913 } NBDExtentArray;
1915 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
1917 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
1919 ea->nb_alloc = nb_alloc;
1920 ea->extents = g_new(NBDExtent, nb_alloc);
1921 ea->can_add = true;
1923 return ea;
1926 static void nbd_extent_array_free(NBDExtentArray *ea)
1928 g_free(ea->extents);
1929 g_free(ea);
1931 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
1933 /* Further modifications of the array after conversion are abandoned */
1934 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
1936 int i;
1938 assert(!ea->converted_to_be);
1939 ea->can_add = false;
1940 ea->converted_to_be = true;
1942 for (i = 0; i < ea->count; i++) {
1943 ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
1944 ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
1949 * Add extent to NBDExtentArray. If extent can't be added (no available space),
1950 * return -1.
1951 * For safety, when returning -1 for the first time, .can_add is set to false,
1952 * further call to nbd_extent_array_add() will crash.
1953 * (to avoid the situation, when after failing to add an extent (returned -1),
1954 * user miss this failure and add another extent, which is successfully added
1955 * (array is full, but new extent may be squashed into the last one), then we
1956 * have invalid array with skipped extent)
1958 static int nbd_extent_array_add(NBDExtentArray *ea,
1959 uint32_t length, uint32_t flags)
1961 assert(ea->can_add);
1963 if (!length) {
1964 return 0;
1967 /* Extend previous extent if flags are the same */
1968 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
1969 uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
1971 if (sum <= UINT32_MAX) {
1972 ea->extents[ea->count - 1].length = sum;
1973 ea->total_length += length;
1974 return 0;
1978 if (ea->count >= ea->nb_alloc) {
1979 ea->can_add = false;
1980 return -1;
1983 ea->total_length += length;
1984 ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
1985 ea->count++;
1987 return 0;
1990 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
1991 uint64_t bytes, NBDExtentArray *ea)
1993 while (bytes) {
1994 uint32_t flags;
1995 int64_t num;
1996 int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
1997 NULL, NULL);
1999 if (ret < 0) {
2000 return ret;
2003 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
2004 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
2006 if (nbd_extent_array_add(ea, num, flags) < 0) {
2007 return 0;
2010 offset += num;
2011 bytes -= num;
2014 return 0;
2018 * nbd_co_send_extents
2020 * @ea is converted to BE by the function
2021 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2023 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
2024 NBDExtentArray *ea,
2025 bool last, uint32_t context_id, Error **errp)
2027 NBDStructuredMeta chunk;
2028 struct iovec iov[] = {
2029 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2030 {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
2033 nbd_extent_array_convert_to_be(ea);
2035 trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2036 last);
2037 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2038 NBD_REPLY_TYPE_BLOCK_STATUS,
2039 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2040 stl_be_p(&chunk.context_id, context_id);
2042 return nbd_co_send_iov(client, iov, 2, errp);
2045 /* Get block status from the exported device and send it to the client */
2046 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2047 BlockDriverState *bs, uint64_t offset,
2048 uint32_t length, bool dont_fragment,
2049 bool last, uint32_t context_id,
2050 Error **errp)
2052 int ret;
2053 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2054 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2056 ret = blockstatus_to_extents(bs, offset, length, ea);
2057 if (ret < 0) {
2058 return nbd_co_send_structured_error(
2059 client, handle, -ret, "can't get block status", errp);
2062 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2065 /* Populate @ea from a dirty bitmap. */
2066 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2067 uint64_t offset, uint64_t length,
2068 NBDExtentArray *es)
2070 int64_t start, dirty_start, dirty_count;
2071 int64_t end = offset + length;
2072 bool full = false;
2074 bdrv_dirty_bitmap_lock(bitmap);
2076 for (start = offset;
2077 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2078 &dirty_start, &dirty_count);
2079 start = dirty_start + dirty_count)
2081 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2082 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2084 full = true;
2085 break;
2089 if (!full) {
2090 /* last non dirty extent */
2091 nbd_extent_array_add(es, end - start, 0);
2094 bdrv_dirty_bitmap_unlock(bitmap);
2097 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2098 BdrvDirtyBitmap *bitmap, uint64_t offset,
2099 uint32_t length, bool dont_fragment, bool last,
2100 uint32_t context_id, Error **errp)
2102 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2103 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2105 bitmap_to_extents(bitmap, offset, length, ea);
2107 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2110 /* nbd_co_receive_request
2111 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2112 * connection right away, and any other negative value to report an error to
2113 * the client (although the caller may still need to disconnect after reporting
2114 * the error).
2116 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2117 Error **errp)
2119 NBDClient *client = req->client;
2120 int valid_flags;
2122 g_assert(qemu_in_coroutine());
2123 assert(client->recv_coroutine == qemu_coroutine_self());
2124 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2125 return -EIO;
2128 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2129 nbd_cmd_lookup(request->type));
2131 if (request->type != NBD_CMD_WRITE) {
2132 /* No payload, we are ready to read the next request. */
2133 req->complete = true;
2136 if (request->type == NBD_CMD_DISC) {
2137 /* Special case: we're going to disconnect without a reply,
2138 * whether or not flags, from, or len are bogus */
2139 return -EIO;
2142 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2143 request->type == NBD_CMD_CACHE)
2145 if (request->len > NBD_MAX_BUFFER_SIZE) {
2146 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2147 request->len, NBD_MAX_BUFFER_SIZE);
2148 return -EINVAL;
2151 if (request->type != NBD_CMD_CACHE) {
2152 req->data = blk_try_blockalign(client->exp->common.blk,
2153 request->len);
2154 if (req->data == NULL) {
2155 error_setg(errp, "No memory");
2156 return -ENOMEM;
2161 if (request->type == NBD_CMD_WRITE) {
2162 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2163 errp) < 0)
2165 return -EIO;
2167 req->complete = true;
2169 trace_nbd_co_receive_request_payload_received(request->handle,
2170 request->len);
2173 /* Sanity checks. */
2174 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2175 (request->type == NBD_CMD_WRITE ||
2176 request->type == NBD_CMD_WRITE_ZEROES ||
2177 request->type == NBD_CMD_TRIM)) {
2178 error_setg(errp, "Export is read-only");
2179 return -EROFS;
2181 if (request->from > client->exp->size ||
2182 request->len > client->exp->size - request->from) {
2183 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2184 ", Size: %" PRIu64, request->from, request->len,
2185 client->exp->size);
2186 return (request->type == NBD_CMD_WRITE ||
2187 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2189 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2190 client->check_align)) {
2192 * The block layer gracefully handles unaligned requests, but
2193 * it's still worth tracing client non-compliance
2195 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2196 request->from,
2197 request->len,
2198 client->check_align);
2200 valid_flags = NBD_CMD_FLAG_FUA;
2201 if (request->type == NBD_CMD_READ && client->structured_reply) {
2202 valid_flags |= NBD_CMD_FLAG_DF;
2203 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2204 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2205 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2206 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2208 if (request->flags & ~valid_flags) {
2209 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2210 nbd_cmd_lookup(request->type), request->flags);
2211 return -EINVAL;
2214 return 0;
2217 /* Send simple reply without a payload, or a structured error
2218 * @error_msg is ignored if @ret >= 0
2219 * Returns 0 if connection is still live, -errno on failure to talk to client
2221 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2222 uint64_t handle,
2223 int ret,
2224 const char *error_msg,
2225 Error **errp)
2227 if (client->structured_reply && ret < 0) {
2228 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2229 errp);
2230 } else {
2231 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2232 NULL, 0, errp);
2236 /* Handle NBD_CMD_READ request.
2237 * Return -errno if sending fails. Other errors are reported directly to the
2238 * client as an error reply. */
2239 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2240 uint8_t *data, Error **errp)
2242 int ret;
2243 NBDExport *exp = client->exp;
2245 assert(request->type == NBD_CMD_READ);
2247 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2248 if (request->flags & NBD_CMD_FLAG_FUA) {
2249 ret = blk_co_flush(exp->common.blk);
2250 if (ret < 0) {
2251 return nbd_send_generic_reply(client, request->handle, ret,
2252 "flush failed", errp);
2256 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2257 request->len)
2259 return nbd_co_send_sparse_read(client, request->handle, request->from,
2260 data, request->len, errp);
2263 ret = blk_pread(exp->common.blk, request->from, data, request->len);
2264 if (ret < 0) {
2265 return nbd_send_generic_reply(client, request->handle, ret,
2266 "reading from file failed", errp);
2269 if (client->structured_reply) {
2270 if (request->len) {
2271 return nbd_co_send_structured_read(client, request->handle,
2272 request->from, data,
2273 request->len, true, errp);
2274 } else {
2275 return nbd_co_send_structured_done(client, request->handle, errp);
2277 } else {
2278 return nbd_co_send_simple_reply(client, request->handle, 0,
2279 data, request->len, errp);
2284 * nbd_do_cmd_cache
2286 * Handle NBD_CMD_CACHE request.
2287 * Return -errno if sending fails. Other errors are reported directly to the
2288 * client as an error reply.
2290 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2291 Error **errp)
2293 int ret;
2294 NBDExport *exp = client->exp;
2296 assert(request->type == NBD_CMD_CACHE);
2298 ret = blk_co_preadv(exp->common.blk, request->from, request->len,
2299 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2301 return nbd_send_generic_reply(client, request->handle, ret,
2302 "caching data failed", errp);
2305 /* Handle NBD request.
2306 * Return -errno if sending fails. Other errors are reported directly to the
2307 * client as an error reply. */
2308 static coroutine_fn int nbd_handle_request(NBDClient *client,
2309 NBDRequest *request,
2310 uint8_t *data, Error **errp)
2312 int ret;
2313 int flags;
2314 NBDExport *exp = client->exp;
2315 char *msg;
2317 switch (request->type) {
2318 case NBD_CMD_CACHE:
2319 return nbd_do_cmd_cache(client, request, errp);
2321 case NBD_CMD_READ:
2322 return nbd_do_cmd_read(client, request, data, errp);
2324 case NBD_CMD_WRITE:
2325 flags = 0;
2326 if (request->flags & NBD_CMD_FLAG_FUA) {
2327 flags |= BDRV_REQ_FUA;
2329 ret = blk_pwrite(exp->common.blk, request->from, data, request->len,
2330 flags);
2331 return nbd_send_generic_reply(client, request->handle, ret,
2332 "writing to file failed", errp);
2334 case NBD_CMD_WRITE_ZEROES:
2335 flags = 0;
2336 if (request->flags & NBD_CMD_FLAG_FUA) {
2337 flags |= BDRV_REQ_FUA;
2339 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2340 flags |= BDRV_REQ_MAY_UNMAP;
2342 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2343 flags |= BDRV_REQ_NO_FALLBACK;
2345 ret = 0;
2346 /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2347 while (ret >= 0 && request->len) {
2348 int align = client->check_align ?: 1;
2349 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2350 align));
2351 ret = blk_pwrite_zeroes(exp->common.blk, request->from, len, flags);
2352 request->len -= len;
2353 request->from += len;
2355 return nbd_send_generic_reply(client, request->handle, ret,
2356 "writing to file failed", errp);
2358 case NBD_CMD_DISC:
2359 /* unreachable, thanks to special case in nbd_co_receive_request() */
2360 abort();
2362 case NBD_CMD_FLUSH:
2363 ret = blk_co_flush(exp->common.blk);
2364 return nbd_send_generic_reply(client, request->handle, ret,
2365 "flush failed", errp);
2367 case NBD_CMD_TRIM:
2368 ret = 0;
2369 /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2370 while (ret >= 0 && request->len) {
2371 int align = client->check_align ?: 1;
2372 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2373 align));
2374 ret = blk_co_pdiscard(exp->common.blk, request->from, len);
2375 request->len -= len;
2376 request->from += len;
2378 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2379 ret = blk_co_flush(exp->common.blk);
2381 return nbd_send_generic_reply(client, request->handle, ret,
2382 "discard failed", errp);
2384 case NBD_CMD_BLOCK_STATUS:
2385 if (!request->len) {
2386 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2387 "need non-zero length", errp);
2389 if (client->export_meta.valid &&
2390 (client->export_meta.base_allocation ||
2391 client->export_meta.bitmap))
2393 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2395 if (client->export_meta.base_allocation) {
2396 ret = nbd_co_send_block_status(client, request->handle,
2397 blk_bs(exp->common.blk),
2398 request->from,
2399 request->len, dont_fragment,
2400 !client->export_meta.bitmap,
2401 NBD_META_ID_BASE_ALLOCATION,
2402 errp);
2403 if (ret < 0) {
2404 return ret;
2408 if (client->export_meta.bitmap) {
2409 ret = nbd_co_send_bitmap(client, request->handle,
2410 client->exp->export_bitmap,
2411 request->from, request->len,
2412 dont_fragment,
2413 true, NBD_META_ID_DIRTY_BITMAP, errp);
2414 if (ret < 0) {
2415 return ret;
2419 return 0;
2420 } else {
2421 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2422 "CMD_BLOCK_STATUS not negotiated",
2423 errp);
2426 default:
2427 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2428 request->type);
2429 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2430 errp);
2431 g_free(msg);
2432 return ret;
2436 /* Owns a reference to the NBDClient passed as opaque. */
2437 static coroutine_fn void nbd_trip(void *opaque)
2439 NBDClient *client = opaque;
2440 NBDRequestData *req;
2441 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2442 int ret;
2443 Error *local_err = NULL;
2445 trace_nbd_trip();
2446 if (client->closing) {
2447 nbd_client_put(client);
2448 return;
2451 req = nbd_request_get(client);
2452 ret = nbd_co_receive_request(req, &request, &local_err);
2453 client->recv_coroutine = NULL;
2455 if (client->closing) {
2457 * The client may be closed when we are blocked in
2458 * nbd_co_receive_request()
2460 goto done;
2463 nbd_client_receive_next_request(client);
2464 if (ret == -EIO) {
2465 goto disconnect;
2468 if (ret < 0) {
2469 /* It wans't -EIO, so, according to nbd_co_receive_request()
2470 * semantics, we should return the error to the client. */
2471 Error *export_err = local_err;
2473 local_err = NULL;
2474 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2475 error_get_pretty(export_err), &local_err);
2476 error_free(export_err);
2477 } else {
2478 ret = nbd_handle_request(client, &request, req->data, &local_err);
2480 if (ret < 0) {
2481 error_prepend(&local_err, "Failed to send reply: ");
2482 goto disconnect;
2485 /* We must disconnect after NBD_CMD_WRITE if we did not
2486 * read the payload.
2488 if (!req->complete) {
2489 error_setg(&local_err, "Request handling failed in intermediate state");
2490 goto disconnect;
2493 done:
2494 nbd_request_put(req);
2495 nbd_client_put(client);
2496 return;
2498 disconnect:
2499 if (local_err) {
2500 error_reportf_err(local_err, "Disconnect client, due to: ");
2502 nbd_request_put(req);
2503 client_close(client, true);
2504 nbd_client_put(client);
2507 static void nbd_client_receive_next_request(NBDClient *client)
2509 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2510 nbd_client_get(client);
2511 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2512 aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);
2516 static coroutine_fn void nbd_co_client_start(void *opaque)
2518 NBDClient *client = opaque;
2519 Error *local_err = NULL;
2521 qemu_co_mutex_init(&client->send_lock);
2523 if (nbd_negotiate(client, &local_err)) {
2524 if (local_err) {
2525 error_report_err(local_err);
2527 client_close(client, false);
2528 return;
2531 nbd_client_receive_next_request(client);
2535 * Create a new client listener using the given channel @sioc.
2536 * Begin servicing it in a coroutine. When the connection closes, call
2537 * @close_fn with an indication of whether the client completed negotiation.
2539 void nbd_client_new(QIOChannelSocket *sioc,
2540 QCryptoTLSCreds *tlscreds,
2541 const char *tlsauthz,
2542 void (*close_fn)(NBDClient *, bool))
2544 NBDClient *client;
2545 Coroutine *co;
2547 client = g_new0(NBDClient, 1);
2548 client->refcount = 1;
2549 client->tlscreds = tlscreds;
2550 if (tlscreds) {
2551 object_ref(OBJECT(client->tlscreds));
2553 client->tlsauthz = g_strdup(tlsauthz);
2554 client->sioc = sioc;
2555 object_ref(OBJECT(client->sioc));
2556 client->ioc = QIO_CHANNEL(sioc);
2557 object_ref(OBJECT(client->ioc));
2558 client->close_fn = close_fn;
2560 co = qemu_coroutine_create(nbd_co_client_start, client);
2561 qemu_coroutine_enter(co);