2 * Copyright (C) 2016-2018 Red Hat, Inc.
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"
21 #include "qapi/error.h"
22 #include "qemu/queue.h"
24 #include "nbd-internal.h"
25 #include "qemu/units.h"
27 #define NBD_META_ID_BASE_ALLOCATION 0
28 #define NBD_META_ID_DIRTY_BITMAP 1
31 * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
32 * constant. If an increase is needed, note that the NBD protocol
33 * recommends no larger than 32 mb, so that the client won't consider
34 * the reply as a denial of service attack.
36 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
38 static int system_errno_to_nbd_errno(int err)
59 #if ENOTSUP != EOPNOTSUPP
71 /* Definitions for opaque data types */
73 typedef struct NBDRequestData NBDRequestData;
75 struct NBDRequestData {
76 QSIMPLEQ_ENTRY(NBDRequestData) entry;
84 void (*close)(NBDExport *exp);
92 QTAILQ_HEAD(, NBDClient) clients;
93 QTAILQ_ENTRY(NBDExport) next;
97 BlockBackend *eject_notifier_blk;
98 Notifier eject_notifier;
100 BdrvDirtyBitmap *export_bitmap;
101 char *export_bitmap_context;
104 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
106 /* NBDExportMetaContexts represents a list of contexts to be exported,
107 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
108 * NBD_OPT_LIST_META_CONTEXT. */
109 typedef struct NBDExportMetaContexts {
111 bool valid; /* means that negotiation of the option finished without
113 bool base_allocation; /* export base:allocation context (block status) */
114 bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
115 } NBDExportMetaContexts;
119 void (*close_fn)(NBDClient *client, bool negotiated);
122 QCryptoTLSCreds *tlscreds;
124 QIOChannelSocket *sioc; /* The underlying data channel */
125 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
127 Coroutine *recv_coroutine;
130 Coroutine *send_coroutine;
132 QTAILQ_ENTRY(NBDClient) next;
136 uint32_t check_align; /* If non-zero, check for aligned client requests */
138 bool structured_reply;
139 NBDExportMetaContexts export_meta;
141 uint32_t opt; /* Current option being negotiated */
142 uint32_t optlen; /* remaining length of data in ioc for the option being
146 static void nbd_client_receive_next_request(NBDClient *client);
148 /* Basic flow for negotiation
175 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
176 uint32_t type, uint32_t length)
178 stq_be_p(&rep->magic, NBD_REP_MAGIC);
179 stl_be_p(&rep->option, option);
180 stl_be_p(&rep->type, type);
181 stl_be_p(&rep->length, length);
184 /* Send a reply header, including length, but no payload.
185 * Return -errno on error, 0 on success. */
186 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
187 uint32_t len, Error **errp)
191 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
192 type, nbd_rep_lookup(type), len);
194 assert(len < NBD_MAX_BUFFER_SIZE);
196 set_be_option_rep(&rep, client->opt, type, len);
197 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
200 /* Send a reply header with default 0 length.
201 * Return -errno on error, 0 on success. */
202 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
205 return nbd_negotiate_send_rep_len(client, type, 0, errp);
208 /* Send an error reply.
209 * Return -errno on error, 0 on success. */
210 static int GCC_FMT_ATTR(4, 0)
211 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
212 Error **errp, const char *fmt, va_list va)
214 g_autofree char *msg = NULL;
218 msg = g_strdup_vprintf(fmt, va);
221 trace_nbd_negotiate_send_rep_err(msg);
222 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
226 if (nbd_write(client->ioc, msg, len, errp) < 0) {
227 error_prepend(errp, "write failed (error message): ");
234 /* Send an error reply.
235 * Return -errno on error, 0 on success. */
236 static int GCC_FMT_ATTR(4, 5)
237 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
238 Error **errp, const char *fmt, ...)
244 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
249 /* Drop remainder of the current option, and send a reply with the
250 * given error type and message. Return -errno on read or write
251 * failure; or 0 if connection is still live. */
252 static int GCC_FMT_ATTR(4, 0)
253 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
254 const char *fmt, va_list va)
256 int ret = nbd_drop(client->ioc, client->optlen, errp);
260 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
265 static int GCC_FMT_ATTR(4, 5)
266 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
267 const char *fmt, ...)
273 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
279 static int GCC_FMT_ATTR(3, 4)
280 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
286 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
292 /* Read size bytes from the unparsed payload of the current option.
293 * Return -errno on I/O error, 0 if option was completely handled by
294 * sending a reply about inconsistent lengths, or 1 on success. */
295 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
298 if (size > client->optlen) {
299 return nbd_opt_invalid(client, errp,
300 "Inconsistent lengths in option %s",
301 nbd_opt_lookup(client->opt));
303 client->optlen -= size;
304 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
307 /* Drop size bytes from the unparsed payload of the current option.
308 * Return -errno on I/O error, 0 if option was completely handled by
309 * sending a reply about inconsistent lengths, or 1 on success. */
310 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
312 if (size > client->optlen) {
313 return nbd_opt_invalid(client, errp,
314 "Inconsistent lengths in option %s",
315 nbd_opt_lookup(client->opt));
317 client->optlen -= size;
318 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
323 * Read a string with the format:
324 * uint32_t len (<= NBD_MAX_NAME_SIZE)
325 * len bytes string (not 0-terminated)
327 * @name should be enough to store NBD_MAX_NAME_SIZE+1.
328 * If @length is non-null, it will be set to the actual string length.
330 * Return -errno on I/O error, 0 if option was completely handled by
331 * sending a reply about inconsistent lengths, or 1 on success.
333 static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length,
339 ret = nbd_opt_read(client, &len, sizeof(len), errp);
343 len = cpu_to_be32(len);
345 if (len > NBD_MAX_NAME_SIZE) {
346 return nbd_opt_invalid(client, errp,
347 "Invalid name length: %" PRIu32, len);
350 ret = nbd_opt_read(client, name, len, errp);
363 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
364 * Return -errno on error, 0 on success. */
365 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
368 size_t name_len, desc_len;
370 const char *name = exp->name ? exp->name : "";
371 const char *desc = exp->description ? exp->description : "";
372 QIOChannel *ioc = client->ioc;
375 trace_nbd_negotiate_send_rep_list(name, desc);
376 name_len = strlen(name);
377 desc_len = strlen(desc);
378 len = name_len + desc_len + sizeof(len);
379 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
384 len = cpu_to_be32(name_len);
385 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
386 error_prepend(errp, "write failed (name length): ");
390 if (nbd_write(ioc, name, name_len, errp) < 0) {
391 error_prepend(errp, "write failed (name buffer): ");
395 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
396 error_prepend(errp, "write failed (description buffer): ");
403 /* Process the NBD_OPT_LIST command, with a potential series of replies.
404 * Return -errno on error, 0 on success. */
405 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
408 assert(client->opt == NBD_OPT_LIST);
410 /* For each export, send a NBD_REP_SERVER reply. */
411 QTAILQ_FOREACH(exp, &exports, next) {
412 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
416 /* Finish with a NBD_REP_ACK. */
417 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
420 static void nbd_check_meta_export(NBDClient *client)
422 client->export_meta.valid &= client->exp == client->export_meta.exp;
425 /* Send a reply to NBD_OPT_EXPORT_NAME.
426 * Return -errno on error, 0 on success. */
427 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
430 char name[NBD_MAX_NAME_SIZE + 1];
431 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
437 [20 .. xx] export name (length bytes)
440 [ 8 .. 9] export flags
441 [10 .. 133] reserved (0) [unless no_zeroes]
443 trace_nbd_negotiate_handle_export_name();
444 if (client->optlen >= sizeof(name)) {
445 error_setg(errp, "Bad length received");
448 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
451 name[client->optlen] = '\0';
454 trace_nbd_negotiate_handle_export_name_request(name);
456 client->exp = nbd_export_find(name);
458 error_setg(errp, "export not found");
462 myflags = client->exp->nbdflags;
463 if (client->structured_reply) {
464 myflags |= NBD_FLAG_SEND_DF;
466 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
467 stq_be_p(buf, client->exp->size);
468 stw_be_p(buf + 8, myflags);
469 len = no_zeroes ? 10 : sizeof(buf);
470 ret = nbd_write(client->ioc, buf, len, errp);
472 error_prepend(errp, "write failed: ");
476 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
477 nbd_export_get(client->exp);
478 nbd_check_meta_export(client);
483 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
484 * The buffer does NOT include the info type prefix.
485 * Return -errno on error, 0 if ready to send more. */
486 static int nbd_negotiate_send_info(NBDClient *client,
487 uint16_t info, uint32_t length, void *buf,
492 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
493 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
494 sizeof(info) + length, errp);
498 info = cpu_to_be16(info);
499 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
502 if (nbd_write(client->ioc, buf, length, errp) < 0) {
508 /* nbd_reject_length: Handle any unexpected payload.
509 * @fatal requests that we quit talking to the client, even if we are able
510 * to successfully send an error reply.
512 * -errno transmission error occurred or @fatal was requested, errp is set
513 * 0 error message successfully sent to client, errp is not set
515 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
519 assert(client->optlen);
520 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
521 nbd_opt_lookup(client->opt));
523 error_setg(errp, "option '%s' has unexpected length",
524 nbd_opt_lookup(client->opt));
530 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
531 * Return -errno on error, 0 if ready for next option, and 1 to move
532 * into transmission phase. */
533 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
536 char name[NBD_MAX_NAME_SIZE + 1];
541 bool sendname = false;
542 bool blocksize = false;
544 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
545 uint32_t check_align = 0;
549 4 bytes: L, name length (can be 0)
551 2 bytes: N, number of requests (can be 0)
552 N * 2 bytes: N requests
554 rc = nbd_opt_read_name(client, name, &namelen, errp);
558 trace_nbd_negotiate_handle_export_name_request(name);
560 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
564 requests = be16_to_cpu(requests);
565 trace_nbd_negotiate_handle_info_requests(requests);
567 rc = nbd_opt_read(client, &request, sizeof(request), errp);
571 request = be16_to_cpu(request);
572 trace_nbd_negotiate_handle_info_request(request,
573 nbd_info_lookup(request));
574 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
575 * everything else is either a request we don't know or
576 * something we send regardless of request */
581 case NBD_INFO_BLOCK_SIZE:
586 if (client->optlen) {
587 return nbd_reject_length(client, false, errp);
590 exp = nbd_export_find(name);
592 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
593 errp, "export '%s' not present",
597 /* Don't bother sending NBD_INFO_NAME unless client requested it */
599 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
606 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
608 if (exp->description) {
609 size_t len = strlen(exp->description);
611 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
612 len, exp->description, errp);
618 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
619 * according to whether the client requested it, and according to
620 * whether this is OPT_INFO or OPT_GO. */
621 /* minimum - 1 for back-compat, or actual if client will obey it. */
622 if (client->opt == NBD_OPT_INFO || blocksize) {
623 check_align = sizes[0] = blk_get_request_alignment(exp->blk);
627 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
628 /* preferred - Hard-code to 4096 for now.
629 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
630 sizes[1] = MAX(4096, sizes[0]);
631 /* maximum - At most 32M, but smaller as appropriate. */
632 sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
633 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
634 sizes[0] = cpu_to_be32(sizes[0]);
635 sizes[1] = cpu_to_be32(sizes[1]);
636 sizes[2] = cpu_to_be32(sizes[2]);
637 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
638 sizeof(sizes), sizes, errp);
643 /* Send NBD_INFO_EXPORT always */
644 myflags = exp->nbdflags;
645 if (client->structured_reply) {
646 myflags |= NBD_FLAG_SEND_DF;
648 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
649 stq_be_p(buf, exp->size);
650 stw_be_p(buf + 8, myflags);
651 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
652 sizeof(buf), buf, errp);
658 * If the client is just asking for NBD_OPT_INFO, but forgot to
659 * request block sizes in a situation that would impact
660 * performance, then return an error. But for NBD_OPT_GO, we
661 * tolerate all clients, regardless of alignments.
663 if (client->opt == NBD_OPT_INFO && !blocksize &&
664 blk_get_request_alignment(exp->blk) > 1) {
665 return nbd_negotiate_send_rep_err(client,
666 NBD_REP_ERR_BLOCK_SIZE_REQD,
668 "request NBD_INFO_BLOCK_SIZE to "
673 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
678 if (client->opt == NBD_OPT_GO) {
680 client->check_align = check_align;
681 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
682 nbd_export_get(client->exp);
683 nbd_check_meta_export(client);
690 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
691 * new channel for all further (now-encrypted) communication. */
692 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
697 struct NBDTLSHandshakeData data = { 0 };
699 assert(client->opt == NBD_OPT_STARTTLS);
701 trace_nbd_negotiate_handle_starttls();
704 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
708 tioc = qio_channel_tls_new_server(ioc,
716 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
717 trace_nbd_negotiate_handle_starttls_handshake();
718 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
719 qio_channel_tls_handshake(tioc,
725 if (!data.complete) {
726 g_main_loop_run(data.loop);
728 g_main_loop_unref(data.loop);
730 object_unref(OBJECT(tioc));
731 error_propagate(errp, data.error);
735 return QIO_CHANNEL(tioc);
738 /* nbd_negotiate_send_meta_context
740 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
742 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
744 static int nbd_negotiate_send_meta_context(NBDClient *client,
749 NBDOptionReplyMetaContext opt;
750 struct iovec iov[] = {
751 {.iov_base = &opt, .iov_len = sizeof(opt)},
752 {.iov_base = (void *)context, .iov_len = strlen(context)}
755 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
759 trace_nbd_negotiate_meta_query_reply(context, context_id);
760 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
761 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
762 stl_be_p(&opt.context_id, context_id);
764 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
767 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
768 * @match is never set to false.
770 * Return -errno on I/O error, 0 if option was completely handled by
771 * sending a reply about inconsistent lengths, or 1 on success.
773 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
774 * It only means that there are no errors.
776 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
781 size_t len = strlen(pattern);
785 query = g_malloc(len);
786 ret = nbd_opt_read(client, query, len, errp);
792 if (strncmp(query, pattern, len) == 0) {
793 trace_nbd_negotiate_meta_query_parse(pattern);
796 trace_nbd_negotiate_meta_query_skip("pattern not matched");
804 * Read @len bytes, and set @match to true if they match @pattern, or if @len
805 * is 0 and the client is performing _LIST_. @match is never set to false.
807 * Return -errno on I/O error, 0 if option was completely handled by
808 * sending a reply about inconsistent lengths, or 1 on success.
810 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
811 * It only means that there are no errors.
813 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
814 uint32_t len, bool *match, Error **errp)
817 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
820 trace_nbd_negotiate_meta_query_parse("empty");
824 if (len != strlen(pattern)) {
825 trace_nbd_negotiate_meta_query_skip("different lengths");
826 return nbd_opt_skip(client, len, errp);
829 return nbd_meta_pattern(client, pattern, match, errp);
832 /* nbd_meta_base_query
834 * Handle queries to 'base' namespace. For now, only the base:allocation
835 * context is available. 'len' is the amount of text remaining to be read from
836 * the current name, after the 'base:' portion has been stripped.
838 * Return -errno on I/O error, 0 if option was completely handled by
839 * sending a reply about inconsistent lengths, or 1 on success.
841 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
842 uint32_t len, Error **errp)
844 return nbd_meta_empty_or_pattern(client, "allocation", len,
845 &meta->base_allocation, errp);
848 /* nbd_meta_bitmap_query
850 * Handle query to 'qemu:' namespace.
851 * @len is the amount of text remaining to be read from the current name, after
852 * the 'qemu:' portion has been stripped.
854 * Return -errno on I/O error, 0 if option was completely handled by
855 * sending a reply about inconsistent lengths, or 1 on success. */
856 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
857 uint32_t len, Error **errp)
859 bool dirty_bitmap = false;
860 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
863 if (!meta->exp->export_bitmap) {
864 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
865 return nbd_opt_skip(client, len, errp);
869 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
872 trace_nbd_negotiate_meta_query_parse("empty");
876 if (len < dirty_bitmap_len) {
877 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
878 return nbd_opt_skip(client, len, errp);
881 len -= dirty_bitmap_len;
882 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
887 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
888 return nbd_opt_skip(client, len, errp);
891 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
893 return nbd_meta_empty_or_pattern(
894 client, meta->exp->export_bitmap_context +
895 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
898 /* nbd_negotiate_meta_query
900 * Parse namespace name and call corresponding function to parse body of the
903 * The only supported namespace now is 'base'.
905 * The function aims not wasting time and memory to read long unknown namespace
908 * Return -errno on I/O error, 0 if option was completely handled by
909 * sending a reply about inconsistent lengths, or 1 on success. */
910 static int nbd_negotiate_meta_query(NBDClient *client,
911 NBDExportMetaContexts *meta, Error **errp)
914 * Both 'qemu' and 'base' namespaces have length = 5 including a
915 * colon. If another length namespace is later introduced, this
916 * should certainly be refactored.
923 ret = nbd_opt_read(client, &len, sizeof(len), errp);
927 len = cpu_to_be32(len);
930 trace_nbd_negotiate_meta_query_skip("length too short");
931 return nbd_opt_skip(client, len, errp);
935 ret = nbd_opt_read(client, ns, ns_len, errp);
940 if (!strncmp(ns, "base:", ns_len)) {
941 trace_nbd_negotiate_meta_query_parse("base:");
942 return nbd_meta_base_query(client, meta, len, errp);
943 } else if (!strncmp(ns, "qemu:", ns_len)) {
944 trace_nbd_negotiate_meta_query_parse("qemu:");
945 return nbd_meta_qemu_query(client, meta, len, errp);
948 trace_nbd_negotiate_meta_query_skip("unknown namespace");
949 return nbd_opt_skip(client, len, errp);
952 /* nbd_negotiate_meta_queries
953 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
955 * Return -errno on I/O error, or 0 if option was completely handled. */
956 static int nbd_negotiate_meta_queries(NBDClient *client,
957 NBDExportMetaContexts *meta, Error **errp)
960 char export_name[NBD_MAX_NAME_SIZE + 1];
961 NBDExportMetaContexts local_meta;
965 if (!client->structured_reply) {
966 return nbd_opt_invalid(client, errp,
967 "request option '%s' when structured reply "
969 nbd_opt_lookup(client->opt));
972 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
973 /* Only change the caller's meta on SET. */
977 memset(meta, 0, sizeof(*meta));
979 ret = nbd_opt_read_name(client, export_name, NULL, errp);
984 meta->exp = nbd_export_find(export_name);
985 if (meta->exp == NULL) {
986 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
987 "export '%s' not present", export_name);
990 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
994 nb_queries = cpu_to_be32(nb_queries);
995 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
996 export_name, nb_queries);
998 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
999 /* enable all known contexts */
1000 meta->base_allocation = true;
1001 meta->bitmap = !!meta->exp->export_bitmap;
1003 for (i = 0; i < nb_queries; ++i) {
1004 ret = nbd_negotiate_meta_query(client, meta, errp);
1011 if (meta->base_allocation) {
1012 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1013 NBD_META_ID_BASE_ALLOCATION,
1021 ret = nbd_negotiate_send_meta_context(client,
1022 meta->exp->export_bitmap_context,
1023 NBD_META_ID_DIRTY_BITMAP,
1030 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1038 /* nbd_negotiate_options
1039 * Process all NBD_OPT_* client option commands, during fixed newstyle
1042 * -errno on error, errp is set
1043 * 0 on successful negotiation, errp is not set
1044 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1047 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1050 bool fixedNewstyle = false;
1051 bool no_zeroes = false;
1054 [ 0 .. 3] client flags
1056 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1057 [ 0 .. 7] NBD_OPTS_MAGIC
1058 [ 8 .. 11] NBD option
1059 [12 .. 15] Data length
1062 [ 0 .. 7] NBD_OPTS_MAGIC
1063 [ 8 .. 11] Second NBD option
1064 [12 .. 15] Data length
1068 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1071 trace_nbd_negotiate_options_flags(flags);
1072 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1073 fixedNewstyle = true;
1074 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1076 if (flags & NBD_FLAG_C_NO_ZEROES) {
1078 flags &= ~NBD_FLAG_C_NO_ZEROES;
1081 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1087 uint32_t option, length;
1090 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1093 trace_nbd_negotiate_options_check_magic(magic);
1094 if (magic != NBD_OPTS_MAGIC) {
1095 error_setg(errp, "Bad magic received");
1099 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1102 client->opt = option;
1104 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1107 assert(!client->optlen);
1108 client->optlen = length;
1110 if (length > NBD_MAX_BUFFER_SIZE) {
1111 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1112 length, NBD_MAX_BUFFER_SIZE);
1116 trace_nbd_negotiate_options_check_option(option,
1117 nbd_opt_lookup(option));
1118 if (client->tlscreds &&
1119 client->ioc == (QIOChannel *)client->sioc) {
1121 if (!fixedNewstyle) {
1122 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1126 case NBD_OPT_STARTTLS:
1128 /* Unconditionally drop the connection if the client
1129 * can't start a TLS negotiation correctly */
1130 return nbd_reject_length(client, true, errp);
1132 tioc = nbd_negotiate_handle_starttls(client, errp);
1137 object_unref(OBJECT(client->ioc));
1138 client->ioc = QIO_CHANNEL(tioc);
1141 case NBD_OPT_EXPORT_NAME:
1142 /* No way to return an error to client, so drop connection */
1143 error_setg(errp, "Option 0x%x not permitted before TLS",
1148 /* Let the client keep trying, unless they asked to
1149 * quit. Always try to give an error back to the
1150 * client; but when replying to OPT_ABORT, be aware
1151 * that the client may hang up before receiving the
1152 * error, in which case we are fine ignoring the
1153 * resulting EPIPE. */
1154 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1155 option == NBD_OPT_ABORT ? NULL : errp,
1157 " not permitted before TLS", option);
1158 if (option == NBD_OPT_ABORT) {
1163 } else if (fixedNewstyle) {
1167 ret = nbd_reject_length(client, false, errp);
1169 ret = nbd_negotiate_handle_list(client, errp);
1174 /* NBD spec says we must try to reply before
1175 * disconnecting, but that we must also tolerate
1176 * guests that don't wait for our reply. */
1177 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1180 case NBD_OPT_EXPORT_NAME:
1181 return nbd_negotiate_handle_export_name(client, no_zeroes,
1186 ret = nbd_negotiate_handle_info(client, errp);
1188 assert(option == NBD_OPT_GO);
1193 case NBD_OPT_STARTTLS:
1195 ret = nbd_reject_length(client, false, errp);
1196 } else if (client->tlscreds) {
1197 ret = nbd_negotiate_send_rep_err(client,
1198 NBD_REP_ERR_INVALID, errp,
1199 "TLS already enabled");
1201 ret = nbd_negotiate_send_rep_err(client,
1202 NBD_REP_ERR_POLICY, errp,
1203 "TLS not configured");
1207 case NBD_OPT_STRUCTURED_REPLY:
1209 ret = nbd_reject_length(client, false, errp);
1210 } else if (client->structured_reply) {
1211 ret = nbd_negotiate_send_rep_err(
1212 client, NBD_REP_ERR_INVALID, errp,
1213 "structured reply already negotiated");
1215 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1216 client->structured_reply = true;
1220 case NBD_OPT_LIST_META_CONTEXT:
1221 case NBD_OPT_SET_META_CONTEXT:
1222 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1227 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1228 "Unsupported option %" PRIu32 " (%s)",
1229 option, nbd_opt_lookup(option));
1234 * If broken new-style we should drop the connection
1235 * for anything except NBD_OPT_EXPORT_NAME
1238 case NBD_OPT_EXPORT_NAME:
1239 return nbd_negotiate_handle_export_name(client, no_zeroes,
1243 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1244 option, nbd_opt_lookup(option));
1256 * -errno on error, errp is set
1257 * 0 on successful negotiation, errp is not set
1258 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1261 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1263 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1266 /* Old style negotiation header, no room for options
1267 [ 0 .. 7] passwd ("NBDMAGIC")
1268 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1270 [24 .. 27] export flags (zero-extended)
1271 [28 .. 151] reserved (0)
1273 New style negotiation header, client can send options
1274 [ 0 .. 7] passwd ("NBDMAGIC")
1275 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1276 [16 .. 17] server flags (0)
1277 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1280 qio_channel_set_blocking(client->ioc, false, NULL);
1282 trace_nbd_negotiate_begin();
1283 memcpy(buf, "NBDMAGIC", 8);
1285 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1286 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1288 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1289 error_prepend(errp, "write failed: ");
1292 ret = nbd_negotiate_options(client, errp);
1295 error_prepend(errp, "option negotiation failed: ");
1300 /* Attach the channel to the same AioContext as the export */
1301 if (client->exp && client->exp->ctx) {
1302 qio_channel_attach_aio_context(client->ioc, client->exp->ctx);
1305 assert(!client->optlen);
1306 trace_nbd_negotiate_success();
1311 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1314 uint8_t buf[NBD_REQUEST_SIZE];
1318 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1324 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1325 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1326 [ 6 .. 7] type (NBD_CMD_READ, ...)
1332 magic = ldl_be_p(buf);
1333 request->flags = lduw_be_p(buf + 4);
1334 request->type = lduw_be_p(buf + 6);
1335 request->handle = ldq_be_p(buf + 8);
1336 request->from = ldq_be_p(buf + 16);
1337 request->len = ldl_be_p(buf + 24);
1339 trace_nbd_receive_request(magic, request->flags, request->type,
1340 request->from, request->len);
1342 if (magic != NBD_REQUEST_MAGIC) {
1343 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1349 #define MAX_NBD_REQUESTS 16
1351 void nbd_client_get(NBDClient *client)
1356 void nbd_client_put(NBDClient *client)
1358 if (--client->refcount == 0) {
1359 /* The last reference should be dropped by client->close,
1360 * which is called by client_close.
1362 assert(client->closing);
1364 qio_channel_detach_aio_context(client->ioc);
1365 object_unref(OBJECT(client->sioc));
1366 object_unref(OBJECT(client->ioc));
1367 if (client->tlscreds) {
1368 object_unref(OBJECT(client->tlscreds));
1370 g_free(client->tlsauthz);
1372 QTAILQ_REMOVE(&client->exp->clients, client, next);
1373 nbd_export_put(client->exp);
1379 static void client_close(NBDClient *client, bool negotiated)
1381 if (client->closing) {
1385 client->closing = true;
1387 /* Force requests to finish. They will drop their own references,
1388 * then we'll close the socket and free the NBDClient.
1390 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1393 /* Also tell the client, so that they release their reference. */
1394 if (client->close_fn) {
1395 client->close_fn(client, negotiated);
1399 static NBDRequestData *nbd_request_get(NBDClient *client)
1401 NBDRequestData *req;
1403 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1404 client->nb_requests++;
1406 req = g_new0(NBDRequestData, 1);
1407 nbd_client_get(client);
1408 req->client = client;
1412 static void nbd_request_put(NBDRequestData *req)
1414 NBDClient *client = req->client;
1417 qemu_vfree(req->data);
1421 client->nb_requests--;
1422 nbd_client_receive_next_request(client);
1424 nbd_client_put(client);
1427 static void blk_aio_attached(AioContext *ctx, void *opaque)
1429 NBDExport *exp = opaque;
1432 trace_nbd_blk_aio_attached(exp->name, ctx);
1436 QTAILQ_FOREACH(client, &exp->clients, next) {
1437 qio_channel_attach_aio_context(client->ioc, ctx);
1438 if (client->recv_coroutine) {
1439 aio_co_schedule(ctx, client->recv_coroutine);
1441 if (client->send_coroutine) {
1442 aio_co_schedule(ctx, client->send_coroutine);
1447 static void blk_aio_detach(void *opaque)
1449 NBDExport *exp = opaque;
1452 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1454 QTAILQ_FOREACH(client, &exp->clients, next) {
1455 qio_channel_detach_aio_context(client->ioc);
1461 static void nbd_eject_notifier(Notifier *n, void *data)
1463 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1464 AioContext *aio_context;
1466 aio_context = exp->ctx;
1467 aio_context_acquire(aio_context);
1468 nbd_export_close(exp);
1469 aio_context_release(aio_context);
1472 NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
1473 uint64_t size, const char *name, const char *desc,
1474 const char *bitmap, bool readonly, bool shared,
1475 void (*close)(NBDExport *), bool writethrough,
1476 BlockBackend *on_eject_blk, Error **errp)
1480 NBDExport *exp = g_new0(NBDExport, 1);
1485 * NBD exports are used for non-shared storage migration. Make sure
1486 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1487 * access since the export could be available before migration handover.
1488 * ctx was acquired in the caller.
1491 ctx = bdrv_get_aio_context(bs);
1492 bdrv_invalidate_cache(bs, NULL);
1494 /* Don't allow resize while the NBD server is running, otherwise we don't
1495 * care what happens with the node. */
1496 perm = BLK_PERM_CONSISTENT_READ;
1498 perm |= BLK_PERM_WRITE;
1500 blk = blk_new(ctx, perm,
1501 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1502 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1503 ret = blk_insert_bs(blk, bs, errp);
1507 blk_set_enable_write_cache(blk, !writethrough);
1508 blk_set_allow_aio_context_change(blk, true);
1511 QTAILQ_INIT(&exp->clients);
1513 assert(dev_offset <= INT64_MAX);
1514 exp->dev_offset = dev_offset;
1515 exp->name = g_strdup(name);
1516 exp->description = g_strdup(desc);
1517 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1518 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1520 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1522 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1525 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1526 NBD_FLAG_SEND_FAST_ZERO);
1528 assert(size <= INT64_MAX - dev_offset);
1529 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1532 BdrvDirtyBitmap *bm = NULL;
1535 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1536 if (bm != NULL || bs->backing == NULL) {
1540 bs = bs->backing->bs;
1544 error_setg(errp, "Bitmap '%s' is not found", bitmap);
1548 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1552 if (readonly && bdrv_is_writable(bs) &&
1553 bdrv_dirty_bitmap_enabled(bm)) {
1555 "Enabled bitmap '%s' incompatible with readonly export",
1560 bdrv_dirty_bitmap_set_busy(bm, true);
1561 exp->export_bitmap = bm;
1562 exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1568 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1571 blk_ref(on_eject_blk);
1572 exp->eject_notifier_blk = on_eject_blk;
1573 exp->eject_notifier.notify = nbd_eject_notifier;
1574 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
1576 QTAILQ_INSERT_TAIL(&exports, exp, next);
1577 nbd_export_get(exp);
1583 g_free(exp->description);
1588 NBDExport *nbd_export_find(const char *name)
1591 QTAILQ_FOREACH(exp, &exports, next) {
1592 if (strcmp(name, exp->name) == 0) {
1601 nbd_export_aio_context(NBDExport *exp)
1606 void nbd_export_close(NBDExport *exp)
1608 NBDClient *client, *next;
1610 nbd_export_get(exp);
1612 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1613 * close mode that stops advertising the export to new clients but
1614 * still permits existing clients to run to completion? Because of
1615 * that possibility, nbd_export_close() can be called more than
1616 * once on an export.
1618 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1619 client_close(client, true);
1622 nbd_export_put(exp);
1625 QTAILQ_REMOVE(&exports, exp, next);
1627 g_free(exp->description);
1628 exp->description = NULL;
1629 nbd_export_put(exp);
1632 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1634 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1635 nbd_export_close(exp);
1639 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1641 error_setg(errp, "export '%s' still in use", exp->name);
1642 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1645 void nbd_export_get(NBDExport *exp)
1647 assert(exp->refcount > 0);
1651 void nbd_export_put(NBDExport *exp)
1653 assert(exp->refcount > 0);
1654 if (exp->refcount == 1) {
1655 nbd_export_close(exp);
1658 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1659 * if someone calls nbd_export_put() on named export not through
1660 * nbd_export_set_name() when refcount is 1. So, let's assert that
1663 assert(exp->refcount > 0);
1664 if (--exp->refcount == 0) {
1665 assert(exp->name == NULL);
1666 assert(exp->description == NULL);
1673 if (exp->eject_notifier_blk) {
1674 notifier_remove(&exp->eject_notifier);
1675 blk_unref(exp->eject_notifier_blk);
1677 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1678 blk_aio_detach, exp);
1679 blk_unref(exp->blk);
1683 if (exp->export_bitmap) {
1684 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1685 g_free(exp->export_bitmap_context);
1692 BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
1697 void nbd_export_close_all(void)
1699 NBDExport *exp, *next;
1700 AioContext *aio_context;
1702 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1703 aio_context = exp->ctx;
1704 aio_context_acquire(aio_context);
1705 nbd_export_close(exp);
1706 aio_context_release(aio_context);
1710 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1711 unsigned niov, Error **errp)
1715 g_assert(qemu_in_coroutine());
1716 qemu_co_mutex_lock(&client->send_lock);
1717 client->send_coroutine = qemu_coroutine_self();
1719 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1721 client->send_coroutine = NULL;
1722 qemu_co_mutex_unlock(&client->send_lock);
1727 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1730 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1731 stl_be_p(&reply->error, error);
1732 stq_be_p(&reply->handle, handle);
1735 static int nbd_co_send_simple_reply(NBDClient *client,
1742 NBDSimpleReply reply;
1743 int nbd_err = system_errno_to_nbd_errno(error);
1744 struct iovec iov[] = {
1745 {.iov_base = &reply, .iov_len = sizeof(reply)},
1746 {.iov_base = data, .iov_len = len}
1749 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1751 set_be_simple_reply(&reply, nbd_err, handle);
1753 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1756 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1757 uint16_t type, uint64_t handle, uint32_t length)
1759 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1760 stw_be_p(&chunk->flags, flags);
1761 stw_be_p(&chunk->type, type);
1762 stq_be_p(&chunk->handle, handle);
1763 stl_be_p(&chunk->length, length);
1766 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1770 NBDStructuredReplyChunk chunk;
1771 struct iovec iov[] = {
1772 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1775 trace_nbd_co_send_structured_done(handle);
1776 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1778 return nbd_co_send_iov(client, iov, 1, errp);
1781 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1789 NBDStructuredReadData chunk;
1790 struct iovec iov[] = {
1791 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1792 {.iov_base = data, .iov_len = size}
1796 trace_nbd_co_send_structured_read(handle, offset, data, size);
1797 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1798 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1799 sizeof(chunk) - sizeof(chunk.h) + size);
1800 stq_be_p(&chunk.offset, offset);
1802 return nbd_co_send_iov(client, iov, 2, errp);
1805 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1811 NBDStructuredError chunk;
1812 int nbd_err = system_errno_to_nbd_errno(error);
1813 struct iovec iov[] = {
1814 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1815 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1819 trace_nbd_co_send_structured_error(handle, nbd_err,
1820 nbd_err_lookup(nbd_err), msg ? msg : "");
1821 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1822 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1823 stl_be_p(&chunk.error, nbd_err);
1824 stw_be_p(&chunk.message_length, iov[1].iov_len);
1826 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1829 /* Do a sparse read and send the structured reply to the client.
1830 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1831 * reported to the client, at which point this function succeeds.
1833 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1841 NBDExport *exp = client->exp;
1842 size_t progress = 0;
1844 while (progress < size) {
1846 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1848 size - progress, &pnum, NULL,
1853 char *msg = g_strdup_printf("unable to check for holes: %s",
1856 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1861 assert(pnum && pnum <= size - progress);
1862 final = progress + pnum == size;
1863 if (status & BDRV_BLOCK_ZERO) {
1864 NBDStructuredReadHole chunk;
1865 struct iovec iov[] = {
1866 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1869 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1871 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1872 NBD_REPLY_TYPE_OFFSET_HOLE,
1873 handle, sizeof(chunk) - sizeof(chunk.h));
1874 stq_be_p(&chunk.offset, offset + progress);
1875 stl_be_p(&chunk.length, pnum);
1876 ret = nbd_co_send_iov(client, iov, 1, errp);
1878 ret = blk_pread(exp->blk, offset + progress + exp->dev_offset,
1879 data + progress, pnum);
1881 error_setg_errno(errp, -ret, "reading from file failed");
1884 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1885 data + progress, pnum, final,
1898 * Populate @extents from block status. Update @bytes to be the actual
1899 * length encoded (which may be smaller than the original), and update
1900 * @nb_extents to the number of extents used.
1902 * Returns zero on success and -errno on bdrv_block_status_above failure.
1904 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
1905 uint64_t *bytes, NBDExtent *extents,
1906 unsigned int *nb_extents)
1908 uint64_t remaining_bytes = *bytes;
1909 NBDExtent *extent = extents, *extents_end = extents + *nb_extents;
1910 bool first_extent = true;
1912 assert(*nb_extents);
1913 while (remaining_bytes) {
1916 int ret = bdrv_block_status_above(bs, NULL, offset, remaining_bytes,
1923 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
1924 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
1927 extent->flags = flags;
1928 extent->length = num;
1929 first_extent = false;
1930 } else if (flags == extent->flags) {
1931 /* extend current extent */
1932 extent->length += num;
1934 if (extent + 1 == extents_end) {
1938 /* start new extent */
1940 extent->flags = flags;
1941 extent->length = num;
1944 remaining_bytes -= num;
1947 extents_end = extent + 1;
1949 for (extent = extents; extent < extents_end; extent++) {
1950 extent->flags = cpu_to_be32(extent->flags);
1951 extent->length = cpu_to_be32(extent->length);
1954 *bytes -= remaining_bytes;
1955 *nb_extents = extents_end - extents;
1960 /* nbd_co_send_extents
1962 * @length is only for tracing purposes (and may be smaller or larger
1963 * than the client's original request). @last controls whether
1964 * NBD_REPLY_FLAG_DONE is sent. @extents should already be in
1965 * big-endian format.
1967 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
1968 NBDExtent *extents, unsigned int nb_extents,
1969 uint64_t length, bool last,
1970 uint32_t context_id, Error **errp)
1972 NBDStructuredMeta chunk;
1974 struct iovec iov[] = {
1975 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1976 {.iov_base = extents, .iov_len = nb_extents * sizeof(extents[0])}
1979 trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last);
1980 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
1981 NBD_REPLY_TYPE_BLOCK_STATUS,
1982 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1983 stl_be_p(&chunk.context_id, context_id);
1985 return nbd_co_send_iov(client, iov, 2, errp);
1988 /* Get block status from the exported device and send it to the client */
1989 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
1990 BlockDriverState *bs, uint64_t offset,
1991 uint32_t length, bool dont_fragment,
1992 bool last, uint32_t context_id,
1996 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
1997 NBDExtent *extents = g_new(NBDExtent, nb_extents);
1998 uint64_t final_length = length;
2000 ret = blockstatus_to_extents(bs, offset, &final_length, extents,
2004 return nbd_co_send_structured_error(
2005 client, handle, -ret, "can't get block status", errp);
2008 ret = nbd_co_send_extents(client, handle, extents, nb_extents,
2009 final_length, last, context_id, errp);
2017 * Populate @extents from a dirty bitmap. Unless @dont_fragment, the
2018 * final extent may exceed the original @length. Store in @length the
2019 * byte length encoded (which may be smaller or larger than the
2020 * original), and return the number of extents used.
2022 static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset,
2023 uint64_t *length, NBDExtent *extents,
2024 unsigned int nb_extents,
2027 uint64_t begin = offset, end = offset;
2028 uint64_t overall_end = offset + *length;
2030 BdrvDirtyBitmapIter *it;
2033 bdrv_dirty_bitmap_lock(bitmap);
2035 it = bdrv_dirty_iter_new(bitmap);
2036 dirty = bdrv_dirty_bitmap_get_locked(bitmap, offset);
2038 assert(begin < overall_end && nb_extents);
2039 while (begin < overall_end && i < nb_extents) {
2040 bool next_dirty = !dirty;
2043 end = bdrv_dirty_bitmap_next_zero(bitmap, begin, UINT64_MAX);
2045 bdrv_set_dirty_iter(it, begin);
2046 end = bdrv_dirty_iter_next(it);
2048 if (end == -1 || end - begin > UINT32_MAX) {
2049 /* Cap to an aligned value < 4G beyond begin. */
2050 end = MIN(bdrv_dirty_bitmap_size(bitmap),
2051 begin + UINT32_MAX + 1 -
2052 bdrv_dirty_bitmap_granularity(bitmap));
2055 if (dont_fragment && end > overall_end) {
2059 extents[i].length = cpu_to_be32(end - begin);
2060 extents[i].flags = cpu_to_be32(dirty ? NBD_STATE_DIRTY : 0);
2066 bdrv_dirty_iter_free(it);
2068 bdrv_dirty_bitmap_unlock(bitmap);
2070 assert(offset < end);
2071 *length = end - offset;
2075 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2076 BdrvDirtyBitmap *bitmap, uint64_t offset,
2077 uint32_t length, bool dont_fragment, bool last,
2078 uint32_t context_id, Error **errp)
2081 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2082 NBDExtent *extents = g_new(NBDExtent, nb_extents);
2083 uint64_t final_length = length;
2085 nb_extents = bitmap_to_extents(bitmap, offset, &final_length, extents,
2086 nb_extents, dont_fragment);
2088 ret = nbd_co_send_extents(client, handle, extents, nb_extents,
2089 final_length, last, context_id, errp);
2096 /* nbd_co_receive_request
2097 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2098 * connection right away, and any other negative value to report an error to
2099 * the client (although the caller may still need to disconnect after reporting
2102 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2105 NBDClient *client = req->client;
2108 g_assert(qemu_in_coroutine());
2109 assert(client->recv_coroutine == qemu_coroutine_self());
2110 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2114 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2115 nbd_cmd_lookup(request->type));
2117 if (request->type != NBD_CMD_WRITE) {
2118 /* No payload, we are ready to read the next request. */
2119 req->complete = true;
2122 if (request->type == NBD_CMD_DISC) {
2123 /* Special case: we're going to disconnect without a reply,
2124 * whether or not flags, from, or len are bogus */
2128 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2129 request->type == NBD_CMD_CACHE)
2131 if (request->len > NBD_MAX_BUFFER_SIZE) {
2132 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2133 request->len, NBD_MAX_BUFFER_SIZE);
2137 if (request->type != NBD_CMD_CACHE) {
2138 req->data = blk_try_blockalign(client->exp->blk, request->len);
2139 if (req->data == NULL) {
2140 error_setg(errp, "No memory");
2146 if (request->type == NBD_CMD_WRITE) {
2147 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2152 req->complete = true;
2154 trace_nbd_co_receive_request_payload_received(request->handle,
2158 /* Sanity checks. */
2159 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2160 (request->type == NBD_CMD_WRITE ||
2161 request->type == NBD_CMD_WRITE_ZEROES ||
2162 request->type == NBD_CMD_TRIM)) {
2163 error_setg(errp, "Export is read-only");
2166 if (request->from > client->exp->size ||
2167 request->len > client->exp->size - request->from) {
2168 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2169 ", Size: %" PRIu64, request->from, request->len,
2171 return (request->type == NBD_CMD_WRITE ||
2172 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2174 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2175 client->check_align)) {
2177 * The block layer gracefully handles unaligned requests, but
2178 * it's still worth tracing client non-compliance
2180 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2183 client->check_align);
2185 valid_flags = NBD_CMD_FLAG_FUA;
2186 if (request->type == NBD_CMD_READ && client->structured_reply) {
2187 valid_flags |= NBD_CMD_FLAG_DF;
2188 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2189 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2190 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2191 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2193 if (request->flags & ~valid_flags) {
2194 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2195 nbd_cmd_lookup(request->type), request->flags);
2202 /* Send simple reply without a payload, or a structured error
2203 * @error_msg is ignored if @ret >= 0
2204 * Returns 0 if connection is still live, -errno on failure to talk to client
2206 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2209 const char *error_msg,
2212 if (client->structured_reply && ret < 0) {
2213 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2216 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2221 /* Handle NBD_CMD_READ request.
2222 * Return -errno if sending fails. Other errors are reported directly to the
2223 * client as an error reply. */
2224 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2225 uint8_t *data, Error **errp)
2228 NBDExport *exp = client->exp;
2230 assert(request->type == NBD_CMD_READ);
2232 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2233 if (request->flags & NBD_CMD_FLAG_FUA) {
2234 ret = blk_co_flush(exp->blk);
2236 return nbd_send_generic_reply(client, request->handle, ret,
2237 "flush failed", errp);
2241 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2244 return nbd_co_send_sparse_read(client, request->handle, request->from,
2245 data, request->len, errp);
2248 ret = blk_pread(exp->blk, request->from + exp->dev_offset, data,
2251 return nbd_send_generic_reply(client, request->handle, ret,
2252 "reading from file failed", errp);
2255 if (client->structured_reply) {
2257 return nbd_co_send_structured_read(client, request->handle,
2258 request->from, data,
2259 request->len, true, errp);
2261 return nbd_co_send_structured_done(client, request->handle, errp);
2264 return nbd_co_send_simple_reply(client, request->handle, 0,
2265 data, request->len, errp);
2272 * Handle NBD_CMD_CACHE request.
2273 * Return -errno if sending fails. Other errors are reported directly to the
2274 * client as an error reply.
2276 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2280 NBDExport *exp = client->exp;
2282 assert(request->type == NBD_CMD_CACHE);
2284 ret = blk_co_preadv(exp->blk, request->from + exp->dev_offset, request->len,
2285 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2287 return nbd_send_generic_reply(client, request->handle, ret,
2288 "caching data failed", errp);
2291 /* Handle NBD request.
2292 * Return -errno if sending fails. Other errors are reported directly to the
2293 * client as an error reply. */
2294 static coroutine_fn int nbd_handle_request(NBDClient *client,
2295 NBDRequest *request,
2296 uint8_t *data, Error **errp)
2300 NBDExport *exp = client->exp;
2303 switch (request->type) {
2305 return nbd_do_cmd_cache(client, request, errp);
2308 return nbd_do_cmd_read(client, request, data, errp);
2312 if (request->flags & NBD_CMD_FLAG_FUA) {
2313 flags |= BDRV_REQ_FUA;
2315 ret = blk_pwrite(exp->blk, request->from + exp->dev_offset,
2316 data, request->len, flags);
2317 return nbd_send_generic_reply(client, request->handle, ret,
2318 "writing to file failed", errp);
2320 case NBD_CMD_WRITE_ZEROES:
2322 if (request->flags & NBD_CMD_FLAG_FUA) {
2323 flags |= BDRV_REQ_FUA;
2325 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2326 flags |= BDRV_REQ_MAY_UNMAP;
2328 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2329 flags |= BDRV_REQ_NO_FALLBACK;
2331 ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
2332 request->len, flags);
2333 return nbd_send_generic_reply(client, request->handle, ret,
2334 "writing to file failed", errp);
2337 /* unreachable, thanks to special case in nbd_co_receive_request() */
2341 ret = blk_co_flush(exp->blk);
2342 return nbd_send_generic_reply(client, request->handle, ret,
2343 "flush failed", errp);
2346 ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
2348 if (ret == 0 && request->flags & NBD_CMD_FLAG_FUA) {
2349 ret = blk_co_flush(exp->blk);
2351 return nbd_send_generic_reply(client, request->handle, ret,
2352 "discard failed", errp);
2354 case NBD_CMD_BLOCK_STATUS:
2355 if (!request->len) {
2356 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2357 "need non-zero length", errp);
2359 if (client->export_meta.valid &&
2360 (client->export_meta.base_allocation ||
2361 client->export_meta.bitmap))
2363 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2365 if (client->export_meta.base_allocation) {
2366 ret = nbd_co_send_block_status(client, request->handle,
2367 blk_bs(exp->blk), request->from,
2368 request->len, dont_fragment,
2369 !client->export_meta.bitmap,
2370 NBD_META_ID_BASE_ALLOCATION,
2377 if (client->export_meta.bitmap) {
2378 ret = nbd_co_send_bitmap(client, request->handle,
2379 client->exp->export_bitmap,
2380 request->from, request->len,
2382 true, NBD_META_ID_DIRTY_BITMAP, errp);
2390 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2391 "CMD_BLOCK_STATUS not negotiated",
2396 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2398 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2405 /* Owns a reference to the NBDClient passed as opaque. */
2406 static coroutine_fn void nbd_trip(void *opaque)
2408 NBDClient *client = opaque;
2409 NBDRequestData *req;
2410 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2412 Error *local_err = NULL;
2415 if (client->closing) {
2416 nbd_client_put(client);
2420 req = nbd_request_get(client);
2421 ret = nbd_co_receive_request(req, &request, &local_err);
2422 client->recv_coroutine = NULL;
2424 if (client->closing) {
2426 * The client may be closed when we are blocked in
2427 * nbd_co_receive_request()
2432 nbd_client_receive_next_request(client);
2438 /* It wans't -EIO, so, according to nbd_co_receive_request()
2439 * semantics, we should return the error to the client. */
2440 Error *export_err = local_err;
2443 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2444 error_get_pretty(export_err), &local_err);
2445 error_free(export_err);
2447 ret = nbd_handle_request(client, &request, req->data, &local_err);
2450 error_prepend(&local_err, "Failed to send reply: ");
2454 /* We must disconnect after NBD_CMD_WRITE if we did not
2457 if (!req->complete) {
2458 error_setg(&local_err, "Request handling failed in intermediate state");
2463 nbd_request_put(req);
2464 nbd_client_put(client);
2469 error_reportf_err(local_err, "Disconnect client, due to: ");
2471 nbd_request_put(req);
2472 client_close(client, true);
2473 nbd_client_put(client);
2476 static void nbd_client_receive_next_request(NBDClient *client)
2478 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2479 nbd_client_get(client);
2480 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2481 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
2485 static coroutine_fn void nbd_co_client_start(void *opaque)
2487 NBDClient *client = opaque;
2488 Error *local_err = NULL;
2490 qemu_co_mutex_init(&client->send_lock);
2492 if (nbd_negotiate(client, &local_err)) {
2494 error_report_err(local_err);
2496 client_close(client, false);
2500 nbd_client_receive_next_request(client);
2504 * Create a new client listener using the given channel @sioc.
2505 * Begin servicing it in a coroutine. When the connection closes, call
2506 * @close_fn with an indication of whether the client completed negotiation.
2508 void nbd_client_new(QIOChannelSocket *sioc,
2509 QCryptoTLSCreds *tlscreds,
2510 const char *tlsauthz,
2511 void (*close_fn)(NBDClient *, bool))
2516 client = g_new0(NBDClient, 1);
2517 client->refcount = 1;
2518 client->tlscreds = tlscreds;
2520 object_ref(OBJECT(client->tlscreds));
2522 client->tlsauthz = g_strdup(tlsauthz);
2523 client->sioc = sioc;
2524 object_ref(OBJECT(client->sioc));
2525 client->ioc = QIO_CHANNEL(sioc);
2526 object_ref(OBJECT(client->ioc));
2527 client->close_fn = close_fn;
2529 co = qemu_coroutine_create(nbd_co_client_start, client);
2530 qemu_coroutine_enter(co);