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)
66 /* Definitions for opaque data types */
68 typedef struct NBDRequestData NBDRequestData;
70 struct NBDRequestData {
71 QSIMPLEQ_ENTRY(NBDRequestData) entry;
79 void (*close)(NBDExport *exp);
87 QTAILQ_HEAD(, NBDClient) clients;
88 QTAILQ_ENTRY(NBDExport) next;
92 BlockBackend *eject_notifier_blk;
93 Notifier eject_notifier;
95 BdrvDirtyBitmap *export_bitmap;
96 char *export_bitmap_context;
99 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
101 /* NBDExportMetaContexts represents a list of contexts to be exported,
102 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
103 * NBD_OPT_LIST_META_CONTEXT. */
104 typedef struct NBDExportMetaContexts {
106 bool valid; /* means that negotiation of the option finished without
108 bool base_allocation; /* export base:allocation context (block status) */
109 bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
110 } NBDExportMetaContexts;
114 void (*close_fn)(NBDClient *client, bool negotiated);
117 QCryptoTLSCreds *tlscreds;
119 QIOChannelSocket *sioc; /* The underlying data channel */
120 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
122 Coroutine *recv_coroutine;
125 Coroutine *send_coroutine;
127 QTAILQ_ENTRY(NBDClient) next;
131 uint32_t check_align; /* If non-zero, check for aligned client requests */
133 bool structured_reply;
134 NBDExportMetaContexts export_meta;
136 uint32_t opt; /* Current option being negotiated */
137 uint32_t optlen; /* remaining length of data in ioc for the option being
141 static void nbd_client_receive_next_request(NBDClient *client);
143 /* Basic flow for negotiation
170 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
171 uint32_t type, uint32_t length)
173 stq_be_p(&rep->magic, NBD_REP_MAGIC);
174 stl_be_p(&rep->option, option);
175 stl_be_p(&rep->type, type);
176 stl_be_p(&rep->length, length);
179 /* Send a reply header, including length, but no payload.
180 * Return -errno on error, 0 on success. */
181 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
182 uint32_t len, Error **errp)
186 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
187 type, nbd_rep_lookup(type), len);
189 assert(len < NBD_MAX_BUFFER_SIZE);
191 set_be_option_rep(&rep, client->opt, type, len);
192 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
195 /* Send a reply header with default 0 length.
196 * Return -errno on error, 0 on success. */
197 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
200 return nbd_negotiate_send_rep_len(client, type, 0, errp);
203 /* Send an error reply.
204 * Return -errno on error, 0 on success. */
205 static int GCC_FMT_ATTR(4, 0)
206 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
207 Error **errp, const char *fmt, va_list va)
213 msg = g_strdup_vprintf(fmt, va);
216 trace_nbd_negotiate_send_rep_err(msg);
217 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
221 if (nbd_write(client->ioc, msg, len, errp) < 0) {
222 error_prepend(errp, "write failed (error message): ");
233 /* Send an error reply.
234 * Return -errno on error, 0 on success. */
235 static int GCC_FMT_ATTR(4, 5)
236 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
237 Error **errp, const char *fmt, ...)
243 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
248 /* Drop remainder of the current option, and send a reply with the
249 * given error type and message. Return -errno on read or write
250 * failure; or 0 if connection is still live. */
251 static int GCC_FMT_ATTR(4, 0)
252 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
253 const char *fmt, va_list va)
255 int ret = nbd_drop(client->ioc, client->optlen, errp);
259 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
264 static int GCC_FMT_ATTR(4, 5)
265 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
266 const char *fmt, ...)
272 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
278 static int GCC_FMT_ATTR(3, 4)
279 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
285 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
291 /* Read size bytes from the unparsed payload of the current option.
292 * Return -errno on I/O error, 0 if option was completely handled by
293 * sending a reply about inconsistent lengths, or 1 on success. */
294 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
297 if (size > client->optlen) {
298 return nbd_opt_invalid(client, errp,
299 "Inconsistent lengths in option %s",
300 nbd_opt_lookup(client->opt));
302 client->optlen -= size;
303 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
306 /* Drop size bytes from the unparsed payload of the current option.
307 * Return -errno on I/O error, 0 if option was completely handled by
308 * sending a reply about inconsistent lengths, or 1 on success. */
309 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
311 if (size > client->optlen) {
312 return nbd_opt_invalid(client, errp,
313 "Inconsistent lengths in option %s",
314 nbd_opt_lookup(client->opt));
316 client->optlen -= size;
317 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
322 * Read a string with the format:
323 * uint32_t len (<= NBD_MAX_NAME_SIZE)
324 * len bytes string (not 0-terminated)
326 * @name should be enough to store NBD_MAX_NAME_SIZE+1.
327 * If @length is non-null, it will be set to the actual string length.
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.
332 static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length,
338 ret = nbd_opt_read(client, &len, sizeof(len), errp);
342 len = cpu_to_be32(len);
344 if (len > NBD_MAX_NAME_SIZE) {
345 return nbd_opt_invalid(client, errp,
346 "Invalid name length: %" PRIu32, len);
349 ret = nbd_opt_read(client, name, len, errp);
362 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
363 * Return -errno on error, 0 on success. */
364 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
367 size_t name_len, desc_len;
369 const char *name = exp->name ? exp->name : "";
370 const char *desc = exp->description ? exp->description : "";
371 QIOChannel *ioc = client->ioc;
374 trace_nbd_negotiate_send_rep_list(name, desc);
375 name_len = strlen(name);
376 desc_len = strlen(desc);
377 len = name_len + desc_len + sizeof(len);
378 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
383 len = cpu_to_be32(name_len);
384 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
385 error_prepend(errp, "write failed (name length): ");
389 if (nbd_write(ioc, name, name_len, errp) < 0) {
390 error_prepend(errp, "write failed (name buffer): ");
394 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
395 error_prepend(errp, "write failed (description buffer): ");
402 /* Process the NBD_OPT_LIST command, with a potential series of replies.
403 * Return -errno on error, 0 on success. */
404 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
407 assert(client->opt == NBD_OPT_LIST);
409 /* For each export, send a NBD_REP_SERVER reply. */
410 QTAILQ_FOREACH(exp, &exports, next) {
411 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
415 /* Finish with a NBD_REP_ACK. */
416 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
419 static void nbd_check_meta_export(NBDClient *client)
421 client->export_meta.valid &= client->exp == client->export_meta.exp;
424 /* Send a reply to NBD_OPT_EXPORT_NAME.
425 * Return -errno on error, 0 on success. */
426 static int nbd_negotiate_handle_export_name(NBDClient *client,
427 uint16_t myflags, bool no_zeroes,
430 char name[NBD_MAX_NAME_SIZE + 1];
431 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
436 [20 .. xx] export name (length bytes)
439 [ 8 .. 9] export flags
440 [10 .. 133] reserved (0) [unless no_zeroes]
442 trace_nbd_negotiate_handle_export_name();
443 if (client->optlen >= sizeof(name)) {
444 error_setg(errp, "Bad length received");
447 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
450 name[client->optlen] = '\0';
453 trace_nbd_negotiate_handle_export_name_request(name);
455 client->exp = nbd_export_find(name);
457 error_setg(errp, "export not found");
461 trace_nbd_negotiate_new_style_size_flags(client->exp->size,
462 client->exp->nbdflags | myflags);
463 stq_be_p(buf, client->exp->size);
464 stw_be_p(buf + 8, client->exp->nbdflags | myflags);
465 len = no_zeroes ? 10 : sizeof(buf);
466 ret = nbd_write(client->ioc, buf, len, errp);
468 error_prepend(errp, "write failed: ");
472 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
473 nbd_export_get(client->exp);
474 nbd_check_meta_export(client);
479 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
480 * The buffer does NOT include the info type prefix.
481 * Return -errno on error, 0 if ready to send more. */
482 static int nbd_negotiate_send_info(NBDClient *client,
483 uint16_t info, uint32_t length, void *buf,
488 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
489 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
490 sizeof(info) + length, errp);
494 info = cpu_to_be16(info);
495 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
498 if (nbd_write(client->ioc, buf, length, errp) < 0) {
504 /* nbd_reject_length: Handle any unexpected payload.
505 * @fatal requests that we quit talking to the client, even if we are able
506 * to successfully send an error reply.
508 * -errno transmission error occurred or @fatal was requested, errp is set
509 * 0 error message successfully sent to client, errp is not set
511 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
515 assert(client->optlen);
516 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
517 nbd_opt_lookup(client->opt));
519 error_setg(errp, "option '%s' has unexpected length",
520 nbd_opt_lookup(client->opt));
526 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
527 * Return -errno on error, 0 if ready for next option, and 1 to move
528 * into transmission phase. */
529 static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
533 char name[NBD_MAX_NAME_SIZE + 1];
538 bool sendname = false;
539 bool blocksize = false;
541 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
542 uint32_t check_align = 0;
545 4 bytes: L, name length (can be 0)
547 2 bytes: N, number of requests (can be 0)
548 N * 2 bytes: N requests
550 rc = nbd_opt_read_name(client, name, &namelen, errp);
554 trace_nbd_negotiate_handle_export_name_request(name);
556 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
560 requests = be16_to_cpu(requests);
561 trace_nbd_negotiate_handle_info_requests(requests);
563 rc = nbd_opt_read(client, &request, sizeof(request), errp);
567 request = be16_to_cpu(request);
568 trace_nbd_negotiate_handle_info_request(request,
569 nbd_info_lookup(request));
570 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
571 * everything else is either a request we don't know or
572 * something we send regardless of request */
577 case NBD_INFO_BLOCK_SIZE:
582 if (client->optlen) {
583 return nbd_reject_length(client, false, errp);
586 exp = nbd_export_find(name);
588 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
589 errp, "export '%s' not present",
593 /* Don't bother sending NBD_INFO_NAME unless client requested it */
595 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
602 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
604 if (exp->description) {
605 size_t len = strlen(exp->description);
607 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
608 len, exp->description, errp);
614 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
615 * according to whether the client requested it, and according to
616 * whether this is OPT_INFO or OPT_GO. */
617 /* minimum - 1 for back-compat, or actual if client will obey it. */
618 if (client->opt == NBD_OPT_INFO || blocksize) {
619 check_align = sizes[0] = blk_get_request_alignment(exp->blk);
623 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
624 /* preferred - Hard-code to 4096 for now.
625 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
626 sizes[1] = MAX(4096, sizes[0]);
627 /* maximum - At most 32M, but smaller as appropriate. */
628 sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
629 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
630 sizes[0] = cpu_to_be32(sizes[0]);
631 sizes[1] = cpu_to_be32(sizes[1]);
632 sizes[2] = cpu_to_be32(sizes[2]);
633 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
634 sizeof(sizes), sizes, errp);
639 /* Send NBD_INFO_EXPORT always */
640 trace_nbd_negotiate_new_style_size_flags(exp->size,
641 exp->nbdflags | myflags);
642 stq_be_p(buf, exp->size);
643 stw_be_p(buf + 8, exp->nbdflags | myflags);
644 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
645 sizeof(buf), buf, errp);
651 * If the client is just asking for NBD_OPT_INFO, but forgot to
652 * request block sizes in a situation that would impact
653 * performance, then return an error. But for NBD_OPT_GO, we
654 * tolerate all clients, regardless of alignments.
656 if (client->opt == NBD_OPT_INFO && !blocksize &&
657 blk_get_request_alignment(exp->blk) > 1) {
658 return nbd_negotiate_send_rep_err(client,
659 NBD_REP_ERR_BLOCK_SIZE_REQD,
661 "request NBD_INFO_BLOCK_SIZE to "
666 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
671 if (client->opt == NBD_OPT_GO) {
673 client->check_align = check_align;
674 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
675 nbd_export_get(client->exp);
676 nbd_check_meta_export(client);
683 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
684 * new channel for all further (now-encrypted) communication. */
685 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
690 struct NBDTLSHandshakeData data = { 0 };
692 assert(client->opt == NBD_OPT_STARTTLS);
694 trace_nbd_negotiate_handle_starttls();
697 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
701 tioc = qio_channel_tls_new_server(ioc,
709 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
710 trace_nbd_negotiate_handle_starttls_handshake();
711 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
712 qio_channel_tls_handshake(tioc,
718 if (!data.complete) {
719 g_main_loop_run(data.loop);
721 g_main_loop_unref(data.loop);
723 object_unref(OBJECT(tioc));
724 error_propagate(errp, data.error);
728 return QIO_CHANNEL(tioc);
731 /* nbd_negotiate_send_meta_context
733 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
735 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
737 static int nbd_negotiate_send_meta_context(NBDClient *client,
742 NBDOptionReplyMetaContext opt;
743 struct iovec iov[] = {
744 {.iov_base = &opt, .iov_len = sizeof(opt)},
745 {.iov_base = (void *)context, .iov_len = strlen(context)}
748 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
752 trace_nbd_negotiate_meta_query_reply(context, context_id);
753 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
754 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
755 stl_be_p(&opt.context_id, context_id);
757 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
760 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
761 * @match is never set to false.
763 * Return -errno on I/O error, 0 if option was completely handled by
764 * sending a reply about inconsistent lengths, or 1 on success.
766 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
767 * It only means that there are no errors.
769 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
774 size_t len = strlen(pattern);
778 query = g_malloc(len);
779 ret = nbd_opt_read(client, query, len, errp);
785 if (strncmp(query, pattern, len) == 0) {
786 trace_nbd_negotiate_meta_query_parse(pattern);
789 trace_nbd_negotiate_meta_query_skip("pattern not matched");
797 * Read @len bytes, and set @match to true if they match @pattern, or if @len
798 * is 0 and the client is performing _LIST_. @match is never set to false.
800 * Return -errno on I/O error, 0 if option was completely handled by
801 * sending a reply about inconsistent lengths, or 1 on success.
803 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
804 * It only means that there are no errors.
806 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
807 uint32_t len, bool *match, Error **errp)
810 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
813 trace_nbd_negotiate_meta_query_parse("empty");
817 if (len != strlen(pattern)) {
818 trace_nbd_negotiate_meta_query_skip("different lengths");
819 return nbd_opt_skip(client, len, errp);
822 return nbd_meta_pattern(client, pattern, match, errp);
825 /* nbd_meta_base_query
827 * Handle queries to 'base' namespace. For now, only the base:allocation
828 * context is available. 'len' is the amount of text remaining to be read from
829 * the current name, after the 'base:' portion has been stripped.
831 * Return -errno on I/O error, 0 if option was completely handled by
832 * sending a reply about inconsistent lengths, or 1 on success.
834 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
835 uint32_t len, Error **errp)
837 return nbd_meta_empty_or_pattern(client, "allocation", len,
838 &meta->base_allocation, errp);
841 /* nbd_meta_bitmap_query
843 * Handle query to 'qemu:' namespace.
844 * @len is the amount of text remaining to be read from the current name, after
845 * the 'qemu:' portion has been stripped.
847 * Return -errno on I/O error, 0 if option was completely handled by
848 * sending a reply about inconsistent lengths, or 1 on success. */
849 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
850 uint32_t len, Error **errp)
852 bool dirty_bitmap = false;
853 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
856 if (!meta->exp->export_bitmap) {
857 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
858 return nbd_opt_skip(client, len, errp);
862 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
865 trace_nbd_negotiate_meta_query_parse("empty");
869 if (len < dirty_bitmap_len) {
870 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
871 return nbd_opt_skip(client, len, errp);
874 len -= dirty_bitmap_len;
875 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
880 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
881 return nbd_opt_skip(client, len, errp);
884 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
886 return nbd_meta_empty_or_pattern(
887 client, meta->exp->export_bitmap_context +
888 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
891 /* nbd_negotiate_meta_query
893 * Parse namespace name and call corresponding function to parse body of the
896 * The only supported namespace now is 'base'.
898 * The function aims not wasting time and memory to read long unknown namespace
901 * Return -errno on I/O error, 0 if option was completely handled by
902 * sending a reply about inconsistent lengths, or 1 on success. */
903 static int nbd_negotiate_meta_query(NBDClient *client,
904 NBDExportMetaContexts *meta, Error **errp)
907 * Both 'qemu' and 'base' namespaces have length = 5 including a
908 * colon. If another length namespace is later introduced, this
909 * should certainly be refactored.
916 ret = nbd_opt_read(client, &len, sizeof(len), errp);
920 len = cpu_to_be32(len);
923 trace_nbd_negotiate_meta_query_skip("length too short");
924 return nbd_opt_skip(client, len, errp);
928 ret = nbd_opt_read(client, ns, ns_len, errp);
933 if (!strncmp(ns, "base:", ns_len)) {
934 trace_nbd_negotiate_meta_query_parse("base:");
935 return nbd_meta_base_query(client, meta, len, errp);
936 } else if (!strncmp(ns, "qemu:", ns_len)) {
937 trace_nbd_negotiate_meta_query_parse("qemu:");
938 return nbd_meta_qemu_query(client, meta, len, errp);
941 trace_nbd_negotiate_meta_query_skip("unknown namespace");
942 return nbd_opt_skip(client, len, errp);
945 /* nbd_negotiate_meta_queries
946 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
948 * Return -errno on I/O error, or 0 if option was completely handled. */
949 static int nbd_negotiate_meta_queries(NBDClient *client,
950 NBDExportMetaContexts *meta, Error **errp)
953 char export_name[NBD_MAX_NAME_SIZE + 1];
954 NBDExportMetaContexts local_meta;
958 if (!client->structured_reply) {
959 return nbd_opt_invalid(client, errp,
960 "request option '%s' when structured reply "
962 nbd_opt_lookup(client->opt));
965 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
966 /* Only change the caller's meta on SET. */
970 memset(meta, 0, sizeof(*meta));
972 ret = nbd_opt_read_name(client, export_name, NULL, errp);
977 meta->exp = nbd_export_find(export_name);
978 if (meta->exp == NULL) {
979 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
980 "export '%s' not present", export_name);
983 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
987 nb_queries = cpu_to_be32(nb_queries);
988 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
989 export_name, nb_queries);
991 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
992 /* enable all known contexts */
993 meta->base_allocation = true;
994 meta->bitmap = !!meta->exp->export_bitmap;
996 for (i = 0; i < nb_queries; ++i) {
997 ret = nbd_negotiate_meta_query(client, meta, errp);
1004 if (meta->base_allocation) {
1005 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1006 NBD_META_ID_BASE_ALLOCATION,
1014 ret = nbd_negotiate_send_meta_context(client,
1015 meta->exp->export_bitmap_context,
1016 NBD_META_ID_DIRTY_BITMAP,
1023 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1031 /* nbd_negotiate_options
1032 * Process all NBD_OPT_* client option commands, during fixed newstyle
1035 * -errno on error, errp is set
1036 * 0 on successful negotiation, errp is not set
1037 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1040 static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
1044 bool fixedNewstyle = false;
1045 bool no_zeroes = false;
1048 [ 0 .. 3] client flags
1050 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1051 [ 0 .. 7] NBD_OPTS_MAGIC
1052 [ 8 .. 11] NBD option
1053 [12 .. 15] Data length
1056 [ 0 .. 7] NBD_OPTS_MAGIC
1057 [ 8 .. 11] Second NBD option
1058 [12 .. 15] Data length
1062 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1065 trace_nbd_negotiate_options_flags(flags);
1066 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1067 fixedNewstyle = true;
1068 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1070 if (flags & NBD_FLAG_C_NO_ZEROES) {
1072 flags &= ~NBD_FLAG_C_NO_ZEROES;
1075 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1081 uint32_t option, length;
1084 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1087 trace_nbd_negotiate_options_check_magic(magic);
1088 if (magic != NBD_OPTS_MAGIC) {
1089 error_setg(errp, "Bad magic received");
1093 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1096 client->opt = option;
1098 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1101 assert(!client->optlen);
1102 client->optlen = length;
1104 if (length > NBD_MAX_BUFFER_SIZE) {
1105 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1106 length, NBD_MAX_BUFFER_SIZE);
1110 trace_nbd_negotiate_options_check_option(option,
1111 nbd_opt_lookup(option));
1112 if (client->tlscreds &&
1113 client->ioc == (QIOChannel *)client->sioc) {
1115 if (!fixedNewstyle) {
1116 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1120 case NBD_OPT_STARTTLS:
1122 /* Unconditionally drop the connection if the client
1123 * can't start a TLS negotiation correctly */
1124 return nbd_reject_length(client, true, errp);
1126 tioc = nbd_negotiate_handle_starttls(client, errp);
1131 object_unref(OBJECT(client->ioc));
1132 client->ioc = QIO_CHANNEL(tioc);
1135 case NBD_OPT_EXPORT_NAME:
1136 /* No way to return an error to client, so drop connection */
1137 error_setg(errp, "Option 0x%x not permitted before TLS",
1142 /* Let the client keep trying, unless they asked to
1143 * quit. Always try to give an error back to the
1144 * client; but when replying to OPT_ABORT, be aware
1145 * that the client may hang up before receiving the
1146 * error, in which case we are fine ignoring the
1147 * resulting EPIPE. */
1148 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1149 option == NBD_OPT_ABORT ? NULL : errp,
1151 " not permitted before TLS", option);
1152 if (option == NBD_OPT_ABORT) {
1157 } else if (fixedNewstyle) {
1161 ret = nbd_reject_length(client, false, errp);
1163 ret = nbd_negotiate_handle_list(client, errp);
1168 /* NBD spec says we must try to reply before
1169 * disconnecting, but that we must also tolerate
1170 * guests that don't wait for our reply. */
1171 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1174 case NBD_OPT_EXPORT_NAME:
1175 return nbd_negotiate_handle_export_name(client,
1181 ret = nbd_negotiate_handle_info(client, myflags, errp);
1183 assert(option == NBD_OPT_GO);
1188 case NBD_OPT_STARTTLS:
1190 ret = nbd_reject_length(client, false, errp);
1191 } else if (client->tlscreds) {
1192 ret = nbd_negotiate_send_rep_err(client,
1193 NBD_REP_ERR_INVALID, errp,
1194 "TLS already enabled");
1196 ret = nbd_negotiate_send_rep_err(client,
1197 NBD_REP_ERR_POLICY, errp,
1198 "TLS not configured");
1202 case NBD_OPT_STRUCTURED_REPLY:
1204 ret = nbd_reject_length(client, false, errp);
1205 } else if (client->structured_reply) {
1206 ret = nbd_negotiate_send_rep_err(
1207 client, NBD_REP_ERR_INVALID, errp,
1208 "structured reply already negotiated");
1210 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1211 client->structured_reply = true;
1212 myflags |= NBD_FLAG_SEND_DF;
1216 case NBD_OPT_LIST_META_CONTEXT:
1217 case NBD_OPT_SET_META_CONTEXT:
1218 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1223 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1224 "Unsupported option %" PRIu32 " (%s)",
1225 option, nbd_opt_lookup(option));
1230 * If broken new-style we should drop the connection
1231 * for anything except NBD_OPT_EXPORT_NAME
1234 case NBD_OPT_EXPORT_NAME:
1235 return nbd_negotiate_handle_export_name(client,
1240 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1241 option, nbd_opt_lookup(option));
1253 * -errno on error, errp is set
1254 * 0 on successful negotiation, errp is not set
1255 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1258 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1260 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1262 const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
1263 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA |
1264 NBD_FLAG_SEND_WRITE_ZEROES | NBD_FLAG_SEND_CACHE);
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, myflags, errp);
1295 error_prepend(errp, "option negotiation failed: ");
1300 assert(!client->optlen);
1301 trace_nbd_negotiate_success();
1306 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1309 uint8_t buf[NBD_REQUEST_SIZE];
1313 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1319 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1320 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1321 [ 6 .. 7] type (NBD_CMD_READ, ...)
1327 magic = ldl_be_p(buf);
1328 request->flags = lduw_be_p(buf + 4);
1329 request->type = lduw_be_p(buf + 6);
1330 request->handle = ldq_be_p(buf + 8);
1331 request->from = ldq_be_p(buf + 16);
1332 request->len = ldl_be_p(buf + 24);
1334 trace_nbd_receive_request(magic, request->flags, request->type,
1335 request->from, request->len);
1337 if (magic != NBD_REQUEST_MAGIC) {
1338 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1344 #define MAX_NBD_REQUESTS 16
1346 void nbd_client_get(NBDClient *client)
1351 void nbd_client_put(NBDClient *client)
1353 if (--client->refcount == 0) {
1354 /* The last reference should be dropped by client->close,
1355 * which is called by client_close.
1357 assert(client->closing);
1359 qio_channel_detach_aio_context(client->ioc);
1360 object_unref(OBJECT(client->sioc));
1361 object_unref(OBJECT(client->ioc));
1362 if (client->tlscreds) {
1363 object_unref(OBJECT(client->tlscreds));
1365 g_free(client->tlsauthz);
1367 QTAILQ_REMOVE(&client->exp->clients, client, next);
1368 nbd_export_put(client->exp);
1374 static void client_close(NBDClient *client, bool negotiated)
1376 if (client->closing) {
1380 client->closing = true;
1382 /* Force requests to finish. They will drop their own references,
1383 * then we'll close the socket and free the NBDClient.
1385 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1388 /* Also tell the client, so that they release their reference. */
1389 if (client->close_fn) {
1390 client->close_fn(client, negotiated);
1394 static NBDRequestData *nbd_request_get(NBDClient *client)
1396 NBDRequestData *req;
1398 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1399 client->nb_requests++;
1401 req = g_new0(NBDRequestData, 1);
1402 nbd_client_get(client);
1403 req->client = client;
1407 static void nbd_request_put(NBDRequestData *req)
1409 NBDClient *client = req->client;
1412 qemu_vfree(req->data);
1416 client->nb_requests--;
1417 nbd_client_receive_next_request(client);
1419 nbd_client_put(client);
1422 static void blk_aio_attached(AioContext *ctx, void *opaque)
1424 NBDExport *exp = opaque;
1427 trace_nbd_blk_aio_attached(exp->name, ctx);
1431 QTAILQ_FOREACH(client, &exp->clients, next) {
1432 qio_channel_attach_aio_context(client->ioc, ctx);
1433 if (client->recv_coroutine) {
1434 aio_co_schedule(ctx, client->recv_coroutine);
1436 if (client->send_coroutine) {
1437 aio_co_schedule(ctx, client->send_coroutine);
1442 static void blk_aio_detach(void *opaque)
1444 NBDExport *exp = opaque;
1447 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1449 QTAILQ_FOREACH(client, &exp->clients, next) {
1450 qio_channel_detach_aio_context(client->ioc);
1456 static void nbd_eject_notifier(Notifier *n, void *data)
1458 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1459 nbd_export_close(exp);
1462 NBDExport *nbd_export_new(BlockDriverState *bs, uint64_t dev_offset,
1463 uint64_t size, const char *name, const char *desc,
1464 const char *bitmap, uint16_t nbdflags,
1465 void (*close)(NBDExport *), bool writethrough,
1466 BlockBackend *on_eject_blk, Error **errp)
1470 NBDExport *exp = g_new0(NBDExport, 1);
1475 * NBD exports are used for non-shared storage migration. Make sure
1476 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1477 * access since the export could be available before migration handover.
1480 ctx = bdrv_get_aio_context(bs);
1481 aio_context_acquire(ctx);
1482 bdrv_invalidate_cache(bs, NULL);
1483 aio_context_release(ctx);
1485 /* Don't allow resize while the NBD server is running, otherwise we don't
1486 * care what happens with the node. */
1487 perm = BLK_PERM_CONSISTENT_READ;
1488 if ((nbdflags & NBD_FLAG_READ_ONLY) == 0) {
1489 perm |= BLK_PERM_WRITE;
1491 blk = blk_new(bdrv_get_aio_context(bs), perm,
1492 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1493 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1494 ret = blk_insert_bs(blk, bs, errp);
1498 blk_set_enable_write_cache(blk, !writethrough);
1499 blk_set_allow_aio_context_change(blk, true);
1502 QTAILQ_INIT(&exp->clients);
1504 assert(dev_offset <= INT64_MAX);
1505 exp->dev_offset = dev_offset;
1506 exp->name = g_strdup(name);
1507 exp->description = g_strdup(desc);
1508 exp->nbdflags = nbdflags;
1509 assert(size <= INT64_MAX - dev_offset);
1510 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1513 BdrvDirtyBitmap *bm = NULL;
1516 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1517 if (bm != NULL || bs->backing == NULL) {
1521 bs = bs->backing->bs;
1525 error_setg(errp, "Bitmap '%s' is not found", bitmap);
1529 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1533 if ((nbdflags & NBD_FLAG_READ_ONLY) && bdrv_is_writable(bs) &&
1534 bdrv_dirty_bitmap_enabled(bm)) {
1536 "Enabled bitmap '%s' incompatible with readonly export",
1541 bdrv_dirty_bitmap_set_busy(bm, true);
1542 exp->export_bitmap = bm;
1543 exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1548 exp->ctx = blk_get_aio_context(blk);
1549 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1552 blk_ref(on_eject_blk);
1553 exp->eject_notifier_blk = on_eject_blk;
1554 exp->eject_notifier.notify = nbd_eject_notifier;
1555 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
1557 QTAILQ_INSERT_TAIL(&exports, exp, next);
1558 nbd_export_get(exp);
1564 g_free(exp->description);
1569 NBDExport *nbd_export_find(const char *name)
1572 QTAILQ_FOREACH(exp, &exports, next) {
1573 if (strcmp(name, exp->name) == 0) {
1581 void nbd_export_close(NBDExport *exp)
1583 NBDClient *client, *next;
1585 nbd_export_get(exp);
1587 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1588 * close mode that stops advertising the export to new clients but
1589 * still permits existing clients to run to completion? Because of
1590 * that possibility, nbd_export_close() can be called more than
1591 * once on an export.
1593 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1594 client_close(client, true);
1597 nbd_export_put(exp);
1600 QTAILQ_REMOVE(&exports, exp, next);
1602 g_free(exp->description);
1603 exp->description = NULL;
1604 nbd_export_put(exp);
1607 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1609 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1610 nbd_export_close(exp);
1614 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1616 error_setg(errp, "export '%s' still in use", exp->name);
1617 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1620 void nbd_export_get(NBDExport *exp)
1622 assert(exp->refcount > 0);
1626 void nbd_export_put(NBDExport *exp)
1628 assert(exp->refcount > 0);
1629 if (exp->refcount == 1) {
1630 nbd_export_close(exp);
1633 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1634 * if someone calls nbd_export_put() on named export not through
1635 * nbd_export_set_name() when refcount is 1. So, let's assert that
1638 assert(exp->refcount > 0);
1639 if (--exp->refcount == 0) {
1640 assert(exp->name == NULL);
1641 assert(exp->description == NULL);
1648 if (exp->eject_notifier_blk) {
1649 notifier_remove(&exp->eject_notifier);
1650 blk_unref(exp->eject_notifier_blk);
1652 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1653 blk_aio_detach, exp);
1654 blk_unref(exp->blk);
1658 if (exp->export_bitmap) {
1659 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1660 g_free(exp->export_bitmap_context);
1667 BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
1672 void nbd_export_close_all(void)
1674 NBDExport *exp, *next;
1676 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1677 nbd_export_close(exp);
1681 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1682 unsigned niov, Error **errp)
1686 g_assert(qemu_in_coroutine());
1687 qemu_co_mutex_lock(&client->send_lock);
1688 client->send_coroutine = qemu_coroutine_self();
1690 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1692 client->send_coroutine = NULL;
1693 qemu_co_mutex_unlock(&client->send_lock);
1698 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1701 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1702 stl_be_p(&reply->error, error);
1703 stq_be_p(&reply->handle, handle);
1706 static int nbd_co_send_simple_reply(NBDClient *client,
1713 NBDSimpleReply reply;
1714 int nbd_err = system_errno_to_nbd_errno(error);
1715 struct iovec iov[] = {
1716 {.iov_base = &reply, .iov_len = sizeof(reply)},
1717 {.iov_base = data, .iov_len = len}
1720 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1722 set_be_simple_reply(&reply, nbd_err, handle);
1724 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1727 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1728 uint16_t type, uint64_t handle, uint32_t length)
1730 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1731 stw_be_p(&chunk->flags, flags);
1732 stw_be_p(&chunk->type, type);
1733 stq_be_p(&chunk->handle, handle);
1734 stl_be_p(&chunk->length, length);
1737 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1741 NBDStructuredReplyChunk chunk;
1742 struct iovec iov[] = {
1743 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1746 trace_nbd_co_send_structured_done(handle);
1747 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1749 return nbd_co_send_iov(client, iov, 1, errp);
1752 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1760 NBDStructuredReadData chunk;
1761 struct iovec iov[] = {
1762 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1763 {.iov_base = data, .iov_len = size}
1767 trace_nbd_co_send_structured_read(handle, offset, data, size);
1768 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1769 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1770 sizeof(chunk) - sizeof(chunk.h) + size);
1771 stq_be_p(&chunk.offset, offset);
1773 return nbd_co_send_iov(client, iov, 2, errp);
1776 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1782 NBDStructuredError chunk;
1783 int nbd_err = system_errno_to_nbd_errno(error);
1784 struct iovec iov[] = {
1785 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1786 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1790 trace_nbd_co_send_structured_error(handle, nbd_err,
1791 nbd_err_lookup(nbd_err), msg ? msg : "");
1792 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1793 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1794 stl_be_p(&chunk.error, nbd_err);
1795 stw_be_p(&chunk.message_length, iov[1].iov_len);
1797 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1800 /* Do a sparse read and send the structured reply to the client.
1801 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1802 * reported to the client, at which point this function succeeds.
1804 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1812 NBDExport *exp = client->exp;
1813 size_t progress = 0;
1815 while (progress < size) {
1817 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1819 size - progress, &pnum, NULL,
1824 char *msg = g_strdup_printf("unable to check for holes: %s",
1827 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1832 assert(pnum && pnum <= size - progress);
1833 final = progress + pnum == size;
1834 if (status & BDRV_BLOCK_ZERO) {
1835 NBDStructuredReadHole chunk;
1836 struct iovec iov[] = {
1837 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1840 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1842 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1843 NBD_REPLY_TYPE_OFFSET_HOLE,
1844 handle, sizeof(chunk) - sizeof(chunk.h));
1845 stq_be_p(&chunk.offset, offset + progress);
1846 stl_be_p(&chunk.length, pnum);
1847 ret = nbd_co_send_iov(client, iov, 1, errp);
1849 ret = blk_pread(exp->blk, offset + progress + exp->dev_offset,
1850 data + progress, pnum);
1852 error_setg_errno(errp, -ret, "reading from file failed");
1855 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1856 data + progress, pnum, final,
1869 * Populate @extents from block status. Update @bytes to be the actual
1870 * length encoded (which may be smaller than the original), and update
1871 * @nb_extents to the number of extents used.
1873 * Returns zero on success and -errno on bdrv_block_status_above failure.
1875 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
1876 uint64_t *bytes, NBDExtent *extents,
1877 unsigned int *nb_extents)
1879 uint64_t remaining_bytes = *bytes;
1880 NBDExtent *extent = extents, *extents_end = extents + *nb_extents;
1881 bool first_extent = true;
1883 assert(*nb_extents);
1884 while (remaining_bytes) {
1887 int ret = bdrv_block_status_above(bs, NULL, offset, remaining_bytes,
1894 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
1895 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
1898 extent->flags = flags;
1899 extent->length = num;
1900 first_extent = false;
1901 } else if (flags == extent->flags) {
1902 /* extend current extent */
1903 extent->length += num;
1905 if (extent + 1 == extents_end) {
1909 /* start new extent */
1911 extent->flags = flags;
1912 extent->length = num;
1915 remaining_bytes -= num;
1918 extents_end = extent + 1;
1920 for (extent = extents; extent < extents_end; extent++) {
1921 extent->flags = cpu_to_be32(extent->flags);
1922 extent->length = cpu_to_be32(extent->length);
1925 *bytes -= remaining_bytes;
1926 *nb_extents = extents_end - extents;
1931 /* nbd_co_send_extents
1933 * @length is only for tracing purposes (and may be smaller or larger
1934 * than the client's original request). @last controls whether
1935 * NBD_REPLY_FLAG_DONE is sent. @extents should already be in
1936 * big-endian format.
1938 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
1939 NBDExtent *extents, unsigned int nb_extents,
1940 uint64_t length, bool last,
1941 uint32_t context_id, Error **errp)
1943 NBDStructuredMeta chunk;
1945 struct iovec iov[] = {
1946 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1947 {.iov_base = extents, .iov_len = nb_extents * sizeof(extents[0])}
1950 trace_nbd_co_send_extents(handle, nb_extents, context_id, length, last);
1951 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
1952 NBD_REPLY_TYPE_BLOCK_STATUS,
1953 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1954 stl_be_p(&chunk.context_id, context_id);
1956 return nbd_co_send_iov(client, iov, 2, errp);
1959 /* Get block status from the exported device and send it to the client */
1960 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
1961 BlockDriverState *bs, uint64_t offset,
1962 uint32_t length, bool dont_fragment,
1963 bool last, uint32_t context_id,
1967 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
1968 NBDExtent *extents = g_new(NBDExtent, nb_extents);
1969 uint64_t final_length = length;
1971 ret = blockstatus_to_extents(bs, offset, &final_length, extents,
1975 return nbd_co_send_structured_error(
1976 client, handle, -ret, "can't get block status", errp);
1979 ret = nbd_co_send_extents(client, handle, extents, nb_extents,
1980 final_length, last, context_id, errp);
1988 * Populate @extents from a dirty bitmap. Unless @dont_fragment, the
1989 * final extent may exceed the original @length. Store in @length the
1990 * byte length encoded (which may be smaller or larger than the
1991 * original), and return the number of extents used.
1993 static unsigned int bitmap_to_extents(BdrvDirtyBitmap *bitmap, uint64_t offset,
1994 uint64_t *length, NBDExtent *extents,
1995 unsigned int nb_extents,
1998 uint64_t begin = offset, end = offset;
1999 uint64_t overall_end = offset + *length;
2001 BdrvDirtyBitmapIter *it;
2004 bdrv_dirty_bitmap_lock(bitmap);
2006 it = bdrv_dirty_iter_new(bitmap);
2007 dirty = bdrv_dirty_bitmap_get_locked(bitmap, offset);
2009 assert(begin < overall_end && nb_extents);
2010 while (begin < overall_end && i < nb_extents) {
2011 bool next_dirty = !dirty;
2014 end = bdrv_dirty_bitmap_next_zero(bitmap, begin, UINT64_MAX);
2016 bdrv_set_dirty_iter(it, begin);
2017 end = bdrv_dirty_iter_next(it);
2019 if (end == -1 || end - begin > UINT32_MAX) {
2020 /* Cap to an aligned value < 4G beyond begin. */
2021 end = MIN(bdrv_dirty_bitmap_size(bitmap),
2022 begin + UINT32_MAX + 1 -
2023 bdrv_dirty_bitmap_granularity(bitmap));
2026 if (dont_fragment && end > overall_end) {
2030 extents[i].length = cpu_to_be32(end - begin);
2031 extents[i].flags = cpu_to_be32(dirty ? NBD_STATE_DIRTY : 0);
2037 bdrv_dirty_iter_free(it);
2039 bdrv_dirty_bitmap_unlock(bitmap);
2041 assert(offset < end);
2042 *length = end - offset;
2046 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2047 BdrvDirtyBitmap *bitmap, uint64_t offset,
2048 uint32_t length, bool dont_fragment, bool last,
2049 uint32_t context_id, Error **errp)
2052 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2053 NBDExtent *extents = g_new(NBDExtent, nb_extents);
2054 uint64_t final_length = length;
2056 nb_extents = bitmap_to_extents(bitmap, offset, &final_length, extents,
2057 nb_extents, dont_fragment);
2059 ret = nbd_co_send_extents(client, handle, extents, nb_extents,
2060 final_length, last, context_id, errp);
2067 /* nbd_co_receive_request
2068 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2069 * connection right away, and any other negative value to report an error to
2070 * the client (although the caller may still need to disconnect after reporting
2073 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2076 NBDClient *client = req->client;
2079 g_assert(qemu_in_coroutine());
2080 assert(client->recv_coroutine == qemu_coroutine_self());
2081 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2085 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2086 nbd_cmd_lookup(request->type));
2088 if (request->type != NBD_CMD_WRITE) {
2089 /* No payload, we are ready to read the next request. */
2090 req->complete = true;
2093 if (request->type == NBD_CMD_DISC) {
2094 /* Special case: we're going to disconnect without a reply,
2095 * whether or not flags, from, or len are bogus */
2099 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2100 request->type == NBD_CMD_CACHE)
2102 if (request->len > NBD_MAX_BUFFER_SIZE) {
2103 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2104 request->len, NBD_MAX_BUFFER_SIZE);
2108 if (request->type != NBD_CMD_CACHE) {
2109 req->data = blk_try_blockalign(client->exp->blk, request->len);
2110 if (req->data == NULL) {
2111 error_setg(errp, "No memory");
2117 if (request->type == NBD_CMD_WRITE) {
2118 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2123 req->complete = true;
2125 trace_nbd_co_receive_request_payload_received(request->handle,
2129 /* Sanity checks. */
2130 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2131 (request->type == NBD_CMD_WRITE ||
2132 request->type == NBD_CMD_WRITE_ZEROES ||
2133 request->type == NBD_CMD_TRIM)) {
2134 error_setg(errp, "Export is read-only");
2137 if (request->from > client->exp->size ||
2138 request->len > client->exp->size - request->from) {
2139 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2140 ", Size: %" PRIu64, request->from, request->len,
2142 return (request->type == NBD_CMD_WRITE ||
2143 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2145 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2146 client->check_align)) {
2148 * The block layer gracefully handles unaligned requests, but
2149 * it's still worth tracing client non-compliance
2151 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2154 client->check_align);
2156 valid_flags = NBD_CMD_FLAG_FUA;
2157 if (request->type == NBD_CMD_READ && client->structured_reply) {
2158 valid_flags |= NBD_CMD_FLAG_DF;
2159 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2160 valid_flags |= NBD_CMD_FLAG_NO_HOLE;
2161 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2162 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2164 if (request->flags & ~valid_flags) {
2165 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2166 nbd_cmd_lookup(request->type), request->flags);
2173 /* Send simple reply without a payload, or a structured error
2174 * @error_msg is ignored if @ret >= 0
2175 * Returns 0 if connection is still live, -errno on failure to talk to client
2177 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2180 const char *error_msg,
2183 if (client->structured_reply && ret < 0) {
2184 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2187 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2192 /* Handle NBD_CMD_READ request.
2193 * Return -errno if sending fails. Other errors are reported directly to the
2194 * client as an error reply. */
2195 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2196 uint8_t *data, Error **errp)
2199 NBDExport *exp = client->exp;
2201 assert(request->type == NBD_CMD_READ);
2203 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2204 if (request->flags & NBD_CMD_FLAG_FUA) {
2205 ret = blk_co_flush(exp->blk);
2207 return nbd_send_generic_reply(client, request->handle, ret,
2208 "flush failed", errp);
2212 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2215 return nbd_co_send_sparse_read(client, request->handle, request->from,
2216 data, request->len, errp);
2219 ret = blk_pread(exp->blk, request->from + exp->dev_offset, data,
2222 return nbd_send_generic_reply(client, request->handle, ret,
2223 "reading from file failed", errp);
2226 if (client->structured_reply) {
2228 return nbd_co_send_structured_read(client, request->handle,
2229 request->from, data,
2230 request->len, true, errp);
2232 return nbd_co_send_structured_done(client, request->handle, errp);
2235 return nbd_co_send_simple_reply(client, request->handle, 0,
2236 data, request->len, errp);
2243 * Handle NBD_CMD_CACHE request.
2244 * Return -errno if sending fails. Other errors are reported directly to the
2245 * client as an error reply.
2247 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2251 NBDExport *exp = client->exp;
2253 assert(request->type == NBD_CMD_CACHE);
2255 ret = blk_co_preadv(exp->blk, request->from + exp->dev_offset, request->len,
2256 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2258 return nbd_send_generic_reply(client, request->handle, ret,
2259 "caching data failed", errp);
2262 /* Handle NBD request.
2263 * Return -errno if sending fails. Other errors are reported directly to the
2264 * client as an error reply. */
2265 static coroutine_fn int nbd_handle_request(NBDClient *client,
2266 NBDRequest *request,
2267 uint8_t *data, Error **errp)
2271 NBDExport *exp = client->exp;
2274 switch (request->type) {
2276 return nbd_do_cmd_cache(client, request, errp);
2279 return nbd_do_cmd_read(client, request, data, errp);
2283 if (request->flags & NBD_CMD_FLAG_FUA) {
2284 flags |= BDRV_REQ_FUA;
2286 ret = blk_pwrite(exp->blk, request->from + exp->dev_offset,
2287 data, request->len, flags);
2288 return nbd_send_generic_reply(client, request->handle, ret,
2289 "writing to file failed", errp);
2291 case NBD_CMD_WRITE_ZEROES:
2293 if (request->flags & NBD_CMD_FLAG_FUA) {
2294 flags |= BDRV_REQ_FUA;
2296 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2297 flags |= BDRV_REQ_MAY_UNMAP;
2299 ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
2300 request->len, flags);
2301 return nbd_send_generic_reply(client, request->handle, ret,
2302 "writing to file failed", errp);
2305 /* unreachable, thanks to special case in nbd_co_receive_request() */
2309 ret = blk_co_flush(exp->blk);
2310 return nbd_send_generic_reply(client, request->handle, ret,
2311 "flush failed", errp);
2314 ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
2316 if (ret == 0 && request->flags & NBD_CMD_FLAG_FUA) {
2317 ret = blk_co_flush(exp->blk);
2319 return nbd_send_generic_reply(client, request->handle, ret,
2320 "discard failed", errp);
2322 case NBD_CMD_BLOCK_STATUS:
2323 if (!request->len) {
2324 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2325 "need non-zero length", errp);
2327 if (client->export_meta.valid &&
2328 (client->export_meta.base_allocation ||
2329 client->export_meta.bitmap))
2331 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2333 if (client->export_meta.base_allocation) {
2334 ret = nbd_co_send_block_status(client, request->handle,
2335 blk_bs(exp->blk), request->from,
2336 request->len, dont_fragment,
2337 !client->export_meta.bitmap,
2338 NBD_META_ID_BASE_ALLOCATION,
2345 if (client->export_meta.bitmap) {
2346 ret = nbd_co_send_bitmap(client, request->handle,
2347 client->exp->export_bitmap,
2348 request->from, request->len,
2350 true, NBD_META_ID_DIRTY_BITMAP, errp);
2358 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2359 "CMD_BLOCK_STATUS not negotiated",
2364 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2366 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2373 /* Owns a reference to the NBDClient passed as opaque. */
2374 static coroutine_fn void nbd_trip(void *opaque)
2376 NBDClient *client = opaque;
2377 NBDRequestData *req;
2378 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2380 Error *local_err = NULL;
2383 if (client->closing) {
2384 nbd_client_put(client);
2388 req = nbd_request_get(client);
2389 ret = nbd_co_receive_request(req, &request, &local_err);
2390 client->recv_coroutine = NULL;
2392 if (client->closing) {
2394 * The client may be closed when we are blocked in
2395 * nbd_co_receive_request()
2400 nbd_client_receive_next_request(client);
2406 /* It wans't -EIO, so, according to nbd_co_receive_request()
2407 * semantics, we should return the error to the client. */
2408 Error *export_err = local_err;
2411 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2412 error_get_pretty(export_err), &local_err);
2413 error_free(export_err);
2415 ret = nbd_handle_request(client, &request, req->data, &local_err);
2418 error_prepend(&local_err, "Failed to send reply: ");
2422 /* We must disconnect after NBD_CMD_WRITE if we did not
2425 if (!req->complete) {
2426 error_setg(&local_err, "Request handling failed in intermediate state");
2431 nbd_request_put(req);
2432 nbd_client_put(client);
2437 error_reportf_err(local_err, "Disconnect client, due to: ");
2439 nbd_request_put(req);
2440 client_close(client, true);
2441 nbd_client_put(client);
2444 static void nbd_client_receive_next_request(NBDClient *client)
2446 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2447 nbd_client_get(client);
2448 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2449 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
2453 static coroutine_fn void nbd_co_client_start(void *opaque)
2455 NBDClient *client = opaque;
2456 Error *local_err = NULL;
2458 qemu_co_mutex_init(&client->send_lock);
2460 if (nbd_negotiate(client, &local_err)) {
2462 error_report_err(local_err);
2464 client_close(client, false);
2468 nbd_client_receive_next_request(client);
2472 * Create a new client listener using the given channel @sioc.
2473 * Begin servicing it in a coroutine. When the connection closes, call
2474 * @close_fn with an indication of whether the client completed negotiation.
2476 void nbd_client_new(QIOChannelSocket *sioc,
2477 QCryptoTLSCreds *tlscreds,
2478 const char *tlsauthz,
2479 void (*close_fn)(NBDClient *, bool))
2484 client = g_new0(NBDClient, 1);
2485 client->refcount = 1;
2486 client->tlscreds = tlscreds;
2488 object_ref(OBJECT(client->tlscreds));
2490 client->tlsauthz = g_strdup(tlsauthz);
2491 client->sioc = sioc;
2492 object_ref(OBJECT(client->sioc));
2493 client->ioc = QIO_CHANNEL(sioc);
2494 object_ref(OBJECT(client->ioc));
2495 client->close_fn = close_fn;
2497 co = qemu_coroutine_create(nbd_co_client_start, client);
2498 qemu_coroutine_enter(co);