nbd/server: Simplify export shutdown
[qemu/ar7.git] / nbd / server.c
blobfb70374df5d50633eab396b00fcb0fe1ddbbf677
1 /*
2 * Copyright (C) 2016-2018 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Server Side
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "block/export.h"
23 #include "qapi/error.h"
24 #include "qemu/queue.h"
25 #include "trace.h"
26 #include "nbd-internal.h"
27 #include "qemu/units.h"
29 #define NBD_META_ID_BASE_ALLOCATION 0
30 #define NBD_META_ID_DIRTY_BITMAP 1
33 * NBD_MAX_BLOCK_STATUS_EXTENTS: 1 MiB of extents data. An empirical
34 * constant. If an increase is needed, note that the NBD protocol
35 * recommends no larger than 32 mb, so that the client won't consider
36 * the reply as a denial of service attack.
38 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
40 static int system_errno_to_nbd_errno(int err)
42 switch (err) {
43 case 0:
44 return NBD_SUCCESS;
45 case EPERM:
46 case EROFS:
47 return NBD_EPERM;
48 case EIO:
49 return NBD_EIO;
50 case ENOMEM:
51 return NBD_ENOMEM;
52 #ifdef EDQUOT
53 case EDQUOT:
54 #endif
55 case EFBIG:
56 case ENOSPC:
57 return NBD_ENOSPC;
58 case EOVERFLOW:
59 return NBD_EOVERFLOW;
60 case ENOTSUP:
61 #if ENOTSUP != EOPNOTSUPP
62 case EOPNOTSUPP:
63 #endif
64 return NBD_ENOTSUP;
65 case ESHUTDOWN:
66 return NBD_ESHUTDOWN;
67 case EINVAL:
68 default:
69 return NBD_EINVAL;
73 /* Definitions for opaque data types */
75 typedef struct NBDRequestData NBDRequestData;
77 struct NBDRequestData {
78 QSIMPLEQ_ENTRY(NBDRequestData) entry;
79 NBDClient *client;
80 uint8_t *data;
81 bool complete;
84 struct NBDExport {
85 BlockExport common;
86 int refcount;
88 BlockBackend *blk;
89 char *name;
90 char *description;
91 uint64_t size;
92 uint16_t nbdflags;
93 QTAILQ_HEAD(, NBDClient) clients;
94 QTAILQ_ENTRY(NBDExport) next;
96 AioContext *ctx;
98 BlockBackend *eject_notifier_blk;
99 Notifier eject_notifier;
101 BdrvDirtyBitmap *export_bitmap;
102 char *export_bitmap_context;
105 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
106 static QTAILQ_HEAD(, NBDExport) closed_exports =
107 QTAILQ_HEAD_INITIALIZER(closed_exports);
109 /* NBDExportMetaContexts represents a list of contexts to be exported,
110 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
111 * NBD_OPT_LIST_META_CONTEXT. */
112 typedef struct NBDExportMetaContexts {
113 NBDExport *exp;
114 bool valid; /* means that negotiation of the option finished without
115 errors */
116 bool base_allocation; /* export base:allocation context (block status) */
117 bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
118 } NBDExportMetaContexts;
120 struct NBDClient {
121 int refcount;
122 void (*close_fn)(NBDClient *client, bool negotiated);
124 NBDExport *exp;
125 QCryptoTLSCreds *tlscreds;
126 char *tlsauthz;
127 QIOChannelSocket *sioc; /* The underlying data channel */
128 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
130 Coroutine *recv_coroutine;
132 CoMutex send_lock;
133 Coroutine *send_coroutine;
135 QTAILQ_ENTRY(NBDClient) next;
136 int nb_requests;
137 bool closing;
139 uint32_t check_align; /* If non-zero, check for aligned client requests */
141 bool structured_reply;
142 NBDExportMetaContexts export_meta;
144 uint32_t opt; /* Current option being negotiated */
145 uint32_t optlen; /* remaining length of data in ioc for the option being
146 negotiated now */
149 static void nbd_client_receive_next_request(NBDClient *client);
151 /* Basic flow for negotiation
153 Server Client
154 Negotiate
158 Server Client
159 Negotiate #1
160 Option
161 Negotiate #2
163 ----
165 followed by
167 Server Client
168 Request
169 Response
170 Request
171 Response
174 Request (type == 2)
178 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
179 uint32_t type, uint32_t length)
181 stq_be_p(&rep->magic, NBD_REP_MAGIC);
182 stl_be_p(&rep->option, option);
183 stl_be_p(&rep->type, type);
184 stl_be_p(&rep->length, length);
187 /* Send a reply header, including length, but no payload.
188 * Return -errno on error, 0 on success. */
189 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
190 uint32_t len, Error **errp)
192 NBDOptionReply rep;
194 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
195 type, nbd_rep_lookup(type), len);
197 assert(len < NBD_MAX_BUFFER_SIZE);
199 set_be_option_rep(&rep, client->opt, type, len);
200 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
203 /* Send a reply header with default 0 length.
204 * Return -errno on error, 0 on success. */
205 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
206 Error **errp)
208 return nbd_negotiate_send_rep_len(client, type, 0, errp);
211 /* Send an error reply.
212 * Return -errno on error, 0 on success. */
213 static int GCC_FMT_ATTR(4, 0)
214 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
215 Error **errp, const char *fmt, va_list va)
217 ERRP_GUARD();
218 g_autofree char *msg = NULL;
219 int ret;
220 size_t len;
222 msg = g_strdup_vprintf(fmt, va);
223 len = strlen(msg);
224 assert(len < NBD_MAX_STRING_SIZE);
225 trace_nbd_negotiate_send_rep_err(msg);
226 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
227 if (ret < 0) {
228 return ret;
230 if (nbd_write(client->ioc, msg, len, errp) < 0) {
231 error_prepend(errp, "write failed (error message): ");
232 return -EIO;
235 return 0;
239 * Return a malloc'd copy of @name suitable for use in an error reply.
241 static char *
242 nbd_sanitize_name(const char *name)
244 if (strnlen(name, 80) < 80) {
245 return g_strdup(name);
247 /* XXX Should we also try to sanitize any control characters? */
248 return g_strdup_printf("%.80s...", name);
251 /* Send an error reply.
252 * Return -errno on error, 0 on success. */
253 static int GCC_FMT_ATTR(4, 5)
254 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
255 Error **errp, const char *fmt, ...)
257 va_list va;
258 int ret;
260 va_start(va, fmt);
261 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
262 va_end(va);
263 return ret;
266 /* Drop remainder of the current option, and send a reply with the
267 * given error type and message. Return -errno on read or write
268 * failure; or 0 if connection is still live. */
269 static int GCC_FMT_ATTR(4, 0)
270 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
271 const char *fmt, va_list va)
273 int ret = nbd_drop(client->ioc, client->optlen, errp);
275 client->optlen = 0;
276 if (!ret) {
277 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
279 return ret;
282 static int GCC_FMT_ATTR(4, 5)
283 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
284 const char *fmt, ...)
286 int ret;
287 va_list va;
289 va_start(va, fmt);
290 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
291 va_end(va);
293 return ret;
296 static int GCC_FMT_ATTR(3, 4)
297 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
299 int ret;
300 va_list va;
302 va_start(va, fmt);
303 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
304 va_end(va);
306 return ret;
309 /* Read size bytes from the unparsed payload of the current option.
310 * Return -errno on I/O error, 0 if option was completely handled by
311 * sending a reply about inconsistent lengths, or 1 on success. */
312 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
313 Error **errp)
315 if (size > client->optlen) {
316 return nbd_opt_invalid(client, errp,
317 "Inconsistent lengths in option %s",
318 nbd_opt_lookup(client->opt));
320 client->optlen -= size;
321 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
324 /* Drop size bytes from the unparsed payload of the current option.
325 * Return -errno on I/O error, 0 if option was completely handled by
326 * sending a reply about inconsistent lengths, or 1 on success. */
327 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
329 if (size > client->optlen) {
330 return nbd_opt_invalid(client, errp,
331 "Inconsistent lengths in option %s",
332 nbd_opt_lookup(client->opt));
334 client->optlen -= size;
335 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
338 /* nbd_opt_read_name
340 * Read a string with the format:
341 * uint32_t len (<= NBD_MAX_STRING_SIZE)
342 * len bytes string (not 0-terminated)
344 * On success, @name will be allocated.
345 * If @length is non-null, it will be set to the actual string length.
347 * Return -errno on I/O error, 0 if option was completely handled by
348 * sending a reply about inconsistent lengths, or 1 on success.
350 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
351 Error **errp)
353 int ret;
354 uint32_t len;
355 g_autofree char *local_name = NULL;
357 *name = NULL;
358 ret = nbd_opt_read(client, &len, sizeof(len), errp);
359 if (ret <= 0) {
360 return ret;
362 len = cpu_to_be32(len);
364 if (len > NBD_MAX_STRING_SIZE) {
365 return nbd_opt_invalid(client, errp,
366 "Invalid name length: %" PRIu32, len);
369 local_name = g_malloc(len + 1);
370 ret = nbd_opt_read(client, local_name, len, errp);
371 if (ret <= 0) {
372 return ret;
374 local_name[len] = '\0';
376 if (length) {
377 *length = len;
379 *name = g_steal_pointer(&local_name);
381 return 1;
384 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
385 * Return -errno on error, 0 on success. */
386 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
387 Error **errp)
389 ERRP_GUARD();
390 size_t name_len, desc_len;
391 uint32_t len;
392 const char *name = exp->name ? exp->name : "";
393 const char *desc = exp->description ? exp->description : "";
394 QIOChannel *ioc = client->ioc;
395 int ret;
397 trace_nbd_negotiate_send_rep_list(name, desc);
398 name_len = strlen(name);
399 desc_len = strlen(desc);
400 assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
401 len = name_len + desc_len + sizeof(len);
402 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
403 if (ret < 0) {
404 return ret;
407 len = cpu_to_be32(name_len);
408 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
409 error_prepend(errp, "write failed (name length): ");
410 return -EINVAL;
413 if (nbd_write(ioc, name, name_len, errp) < 0) {
414 error_prepend(errp, "write failed (name buffer): ");
415 return -EINVAL;
418 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
419 error_prepend(errp, "write failed (description buffer): ");
420 return -EINVAL;
423 return 0;
426 /* Process the NBD_OPT_LIST command, with a potential series of replies.
427 * Return -errno on error, 0 on success. */
428 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
430 NBDExport *exp;
431 assert(client->opt == NBD_OPT_LIST);
433 /* For each export, send a NBD_REP_SERVER reply. */
434 QTAILQ_FOREACH(exp, &exports, next) {
435 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
436 return -EINVAL;
439 /* Finish with a NBD_REP_ACK. */
440 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
443 static void nbd_check_meta_export(NBDClient *client)
445 client->export_meta.valid &= client->exp == client->export_meta.exp;
448 /* Send a reply to NBD_OPT_EXPORT_NAME.
449 * Return -errno on error, 0 on success. */
450 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
451 Error **errp)
453 ERRP_GUARD();
454 g_autofree char *name = NULL;
455 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
456 size_t len;
457 int ret;
458 uint16_t myflags;
460 /* Client sends:
461 [20 .. xx] export name (length bytes)
462 Server replies:
463 [ 0 .. 7] size
464 [ 8 .. 9] export flags
465 [10 .. 133] reserved (0) [unless no_zeroes]
467 trace_nbd_negotiate_handle_export_name();
468 if (client->optlen > NBD_MAX_STRING_SIZE) {
469 error_setg(errp, "Bad length received");
470 return -EINVAL;
472 name = g_malloc(client->optlen + 1);
473 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
474 return -EIO;
476 name[client->optlen] = '\0';
477 client->optlen = 0;
479 trace_nbd_negotiate_handle_export_name_request(name);
481 client->exp = nbd_export_find(name);
482 if (!client->exp) {
483 error_setg(errp, "export not found");
484 return -EINVAL;
487 myflags = client->exp->nbdflags;
488 if (client->structured_reply) {
489 myflags |= NBD_FLAG_SEND_DF;
491 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
492 stq_be_p(buf, client->exp->size);
493 stw_be_p(buf + 8, myflags);
494 len = no_zeroes ? 10 : sizeof(buf);
495 ret = nbd_write(client->ioc, buf, len, errp);
496 if (ret < 0) {
497 error_prepend(errp, "write failed: ");
498 return ret;
501 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
502 nbd_export_get(client->exp);
503 nbd_check_meta_export(client);
505 return 0;
508 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
509 * The buffer does NOT include the info type prefix.
510 * Return -errno on error, 0 if ready to send more. */
511 static int nbd_negotiate_send_info(NBDClient *client,
512 uint16_t info, uint32_t length, void *buf,
513 Error **errp)
515 int rc;
517 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
518 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
519 sizeof(info) + length, errp);
520 if (rc < 0) {
521 return rc;
523 info = cpu_to_be16(info);
524 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
525 return -EIO;
527 if (nbd_write(client->ioc, buf, length, errp) < 0) {
528 return -EIO;
530 return 0;
533 /* nbd_reject_length: Handle any unexpected payload.
534 * @fatal requests that we quit talking to the client, even if we are able
535 * to successfully send an error reply.
536 * Return:
537 * -errno transmission error occurred or @fatal was requested, errp is set
538 * 0 error message successfully sent to client, errp is not set
540 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
542 int ret;
544 assert(client->optlen);
545 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
546 nbd_opt_lookup(client->opt));
547 if (fatal && !ret) {
548 error_setg(errp, "option '%s' has unexpected length",
549 nbd_opt_lookup(client->opt));
550 return -EINVAL;
552 return ret;
555 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
556 * Return -errno on error, 0 if ready for next option, and 1 to move
557 * into transmission phase. */
558 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
560 int rc;
561 g_autofree char *name = NULL;
562 NBDExport *exp;
563 uint16_t requests;
564 uint16_t request;
565 uint32_t namelen;
566 bool sendname = false;
567 bool blocksize = false;
568 uint32_t sizes[3];
569 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
570 uint32_t check_align = 0;
571 uint16_t myflags;
573 /* Client sends:
574 4 bytes: L, name length (can be 0)
575 L bytes: export name
576 2 bytes: N, number of requests (can be 0)
577 N * 2 bytes: N requests
579 rc = nbd_opt_read_name(client, &name, &namelen, errp);
580 if (rc <= 0) {
581 return rc;
583 trace_nbd_negotiate_handle_export_name_request(name);
585 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
586 if (rc <= 0) {
587 return rc;
589 requests = be16_to_cpu(requests);
590 trace_nbd_negotiate_handle_info_requests(requests);
591 while (requests--) {
592 rc = nbd_opt_read(client, &request, sizeof(request), errp);
593 if (rc <= 0) {
594 return rc;
596 request = be16_to_cpu(request);
597 trace_nbd_negotiate_handle_info_request(request,
598 nbd_info_lookup(request));
599 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
600 * everything else is either a request we don't know or
601 * something we send regardless of request */
602 switch (request) {
603 case NBD_INFO_NAME:
604 sendname = true;
605 break;
606 case NBD_INFO_BLOCK_SIZE:
607 blocksize = true;
608 break;
611 if (client->optlen) {
612 return nbd_reject_length(client, false, errp);
615 exp = nbd_export_find(name);
616 if (!exp) {
617 g_autofree char *sane_name = nbd_sanitize_name(name);
619 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
620 errp, "export '%s' not present",
621 sane_name);
624 /* Don't bother sending NBD_INFO_NAME unless client requested it */
625 if (sendname) {
626 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
627 errp);
628 if (rc < 0) {
629 return rc;
633 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
634 * client request */
635 if (exp->description) {
636 size_t len = strlen(exp->description);
638 assert(len <= NBD_MAX_STRING_SIZE);
639 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
640 len, exp->description, errp);
641 if (rc < 0) {
642 return rc;
646 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
647 * according to whether the client requested it, and according to
648 * whether this is OPT_INFO or OPT_GO. */
649 /* minimum - 1 for back-compat, or actual if client will obey it. */
650 if (client->opt == NBD_OPT_INFO || blocksize) {
651 check_align = sizes[0] = blk_get_request_alignment(exp->blk);
652 } else {
653 sizes[0] = 1;
655 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
656 /* preferred - Hard-code to 4096 for now.
657 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
658 sizes[1] = MAX(4096, sizes[0]);
659 /* maximum - At most 32M, but smaller as appropriate. */
660 sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
661 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
662 sizes[0] = cpu_to_be32(sizes[0]);
663 sizes[1] = cpu_to_be32(sizes[1]);
664 sizes[2] = cpu_to_be32(sizes[2]);
665 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
666 sizeof(sizes), sizes, errp);
667 if (rc < 0) {
668 return rc;
671 /* Send NBD_INFO_EXPORT always */
672 myflags = exp->nbdflags;
673 if (client->structured_reply) {
674 myflags |= NBD_FLAG_SEND_DF;
676 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
677 stq_be_p(buf, exp->size);
678 stw_be_p(buf + 8, myflags);
679 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
680 sizeof(buf), buf, errp);
681 if (rc < 0) {
682 return rc;
686 * If the client is just asking for NBD_OPT_INFO, but forgot to
687 * request block sizes in a situation that would impact
688 * performance, then return an error. But for NBD_OPT_GO, we
689 * tolerate all clients, regardless of alignments.
691 if (client->opt == NBD_OPT_INFO && !blocksize &&
692 blk_get_request_alignment(exp->blk) > 1) {
693 return nbd_negotiate_send_rep_err(client,
694 NBD_REP_ERR_BLOCK_SIZE_REQD,
695 errp,
696 "request NBD_INFO_BLOCK_SIZE to "
697 "use this export");
700 /* Final reply */
701 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
702 if (rc < 0) {
703 return rc;
706 if (client->opt == NBD_OPT_GO) {
707 client->exp = exp;
708 client->check_align = check_align;
709 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
710 nbd_export_get(client->exp);
711 nbd_check_meta_export(client);
712 rc = 1;
714 return rc;
718 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
719 * new channel for all further (now-encrypted) communication. */
720 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
721 Error **errp)
723 QIOChannel *ioc;
724 QIOChannelTLS *tioc;
725 struct NBDTLSHandshakeData data = { 0 };
727 assert(client->opt == NBD_OPT_STARTTLS);
729 trace_nbd_negotiate_handle_starttls();
730 ioc = client->ioc;
732 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
733 return NULL;
736 tioc = qio_channel_tls_new_server(ioc,
737 client->tlscreds,
738 client->tlsauthz,
739 errp);
740 if (!tioc) {
741 return NULL;
744 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
745 trace_nbd_negotiate_handle_starttls_handshake();
746 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
747 qio_channel_tls_handshake(tioc,
748 nbd_tls_handshake,
749 &data,
750 NULL,
751 NULL);
753 if (!data.complete) {
754 g_main_loop_run(data.loop);
756 g_main_loop_unref(data.loop);
757 if (data.error) {
758 object_unref(OBJECT(tioc));
759 error_propagate(errp, data.error);
760 return NULL;
763 return QIO_CHANNEL(tioc);
766 /* nbd_negotiate_send_meta_context
768 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
770 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
772 static int nbd_negotiate_send_meta_context(NBDClient *client,
773 const char *context,
774 uint32_t context_id,
775 Error **errp)
777 NBDOptionReplyMetaContext opt;
778 struct iovec iov[] = {
779 {.iov_base = &opt, .iov_len = sizeof(opt)},
780 {.iov_base = (void *)context, .iov_len = strlen(context)}
783 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
784 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
785 context_id = 0;
788 trace_nbd_negotiate_meta_query_reply(context, context_id);
789 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
790 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
791 stl_be_p(&opt.context_id, context_id);
793 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
796 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
797 * @match is never set to false.
799 * Return -errno on I/O error, 0 if option was completely handled by
800 * sending a reply about inconsistent lengths, or 1 on success.
802 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
803 * It only means that there are no errors.
805 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
806 Error **errp)
808 int ret;
809 char *query;
810 size_t len = strlen(pattern);
812 assert(len);
814 query = g_malloc(len);
815 ret = nbd_opt_read(client, query, len, errp);
816 if (ret <= 0) {
817 g_free(query);
818 return ret;
821 if (strncmp(query, pattern, len) == 0) {
822 trace_nbd_negotiate_meta_query_parse(pattern);
823 *match = true;
824 } else {
825 trace_nbd_negotiate_meta_query_skip("pattern not matched");
827 g_free(query);
829 return 1;
833 * Read @len bytes, and set @match to true if they match @pattern, or if @len
834 * is 0 and the client is performing _LIST_. @match is never set to false.
836 * Return -errno on I/O error, 0 if option was completely handled by
837 * sending a reply about inconsistent lengths, or 1 on success.
839 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
840 * It only means that there are no errors.
842 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
843 uint32_t len, bool *match, Error **errp)
845 if (len == 0) {
846 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
847 *match = true;
849 trace_nbd_negotiate_meta_query_parse("empty");
850 return 1;
853 if (len != strlen(pattern)) {
854 trace_nbd_negotiate_meta_query_skip("different lengths");
855 return nbd_opt_skip(client, len, errp);
858 return nbd_meta_pattern(client, pattern, match, errp);
861 /* nbd_meta_base_query
863 * Handle queries to 'base' namespace. For now, only the base:allocation
864 * context is available. 'len' is the amount of text remaining to be read from
865 * the current name, after the 'base:' portion has been stripped.
867 * Return -errno on I/O error, 0 if option was completely handled by
868 * sending a reply about inconsistent lengths, or 1 on success.
870 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
871 uint32_t len, Error **errp)
873 return nbd_meta_empty_or_pattern(client, "allocation", len,
874 &meta->base_allocation, errp);
877 /* nbd_meta_bitmap_query
879 * Handle query to 'qemu:' namespace.
880 * @len is the amount of text remaining to be read from the current name, after
881 * the 'qemu:' portion has been stripped.
883 * Return -errno on I/O error, 0 if option was completely handled by
884 * sending a reply about inconsistent lengths, or 1 on success. */
885 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
886 uint32_t len, Error **errp)
888 bool dirty_bitmap = false;
889 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
890 int ret;
892 if (!meta->exp->export_bitmap) {
893 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
894 return nbd_opt_skip(client, len, errp);
897 if (len == 0) {
898 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
899 meta->bitmap = true;
901 trace_nbd_negotiate_meta_query_parse("empty");
902 return 1;
905 if (len < dirty_bitmap_len) {
906 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
907 return nbd_opt_skip(client, len, errp);
910 len -= dirty_bitmap_len;
911 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
912 if (ret <= 0) {
913 return ret;
915 if (!dirty_bitmap) {
916 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
917 return nbd_opt_skip(client, len, errp);
920 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
922 return nbd_meta_empty_or_pattern(
923 client, meta->exp->export_bitmap_context +
924 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
927 /* nbd_negotiate_meta_query
929 * Parse namespace name and call corresponding function to parse body of the
930 * query.
932 * The only supported namespaces are 'base' and 'qemu'.
934 * The function aims not wasting time and memory to read long unknown namespace
935 * names.
937 * Return -errno on I/O error, 0 if option was completely handled by
938 * sending a reply about inconsistent lengths, or 1 on success. */
939 static int nbd_negotiate_meta_query(NBDClient *client,
940 NBDExportMetaContexts *meta, Error **errp)
943 * Both 'qemu' and 'base' namespaces have length = 5 including a
944 * colon. If another length namespace is later introduced, this
945 * should certainly be refactored.
947 int ret;
948 size_t ns_len = 5;
949 char ns[5];
950 uint32_t len;
952 ret = nbd_opt_read(client, &len, sizeof(len), errp);
953 if (ret <= 0) {
954 return ret;
956 len = cpu_to_be32(len);
958 if (len > NBD_MAX_STRING_SIZE) {
959 trace_nbd_negotiate_meta_query_skip("length too long");
960 return nbd_opt_skip(client, len, errp);
962 if (len < ns_len) {
963 trace_nbd_negotiate_meta_query_skip("length too short");
964 return nbd_opt_skip(client, len, errp);
967 len -= ns_len;
968 ret = nbd_opt_read(client, ns, ns_len, errp);
969 if (ret <= 0) {
970 return ret;
973 if (!strncmp(ns, "base:", ns_len)) {
974 trace_nbd_negotiate_meta_query_parse("base:");
975 return nbd_meta_base_query(client, meta, len, errp);
976 } else if (!strncmp(ns, "qemu:", ns_len)) {
977 trace_nbd_negotiate_meta_query_parse("qemu:");
978 return nbd_meta_qemu_query(client, meta, len, errp);
981 trace_nbd_negotiate_meta_query_skip("unknown namespace");
982 return nbd_opt_skip(client, len, errp);
985 /* nbd_negotiate_meta_queries
986 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
988 * Return -errno on I/O error, or 0 if option was completely handled. */
989 static int nbd_negotiate_meta_queries(NBDClient *client,
990 NBDExportMetaContexts *meta, Error **errp)
992 int ret;
993 g_autofree char *export_name = NULL;
994 NBDExportMetaContexts local_meta;
995 uint32_t nb_queries;
996 int i;
998 if (!client->structured_reply) {
999 return nbd_opt_invalid(client, errp,
1000 "request option '%s' when structured reply "
1001 "is not negotiated",
1002 nbd_opt_lookup(client->opt));
1005 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1006 /* Only change the caller's meta on SET. */
1007 meta = &local_meta;
1010 memset(meta, 0, sizeof(*meta));
1012 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1013 if (ret <= 0) {
1014 return ret;
1017 meta->exp = nbd_export_find(export_name);
1018 if (meta->exp == NULL) {
1019 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1021 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1022 "export '%s' not present", sane_name);
1025 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
1026 if (ret <= 0) {
1027 return ret;
1029 nb_queries = cpu_to_be32(nb_queries);
1030 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1031 export_name, nb_queries);
1033 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1034 /* enable all known contexts */
1035 meta->base_allocation = true;
1036 meta->bitmap = !!meta->exp->export_bitmap;
1037 } else {
1038 for (i = 0; i < nb_queries; ++i) {
1039 ret = nbd_negotiate_meta_query(client, meta, errp);
1040 if (ret <= 0) {
1041 return ret;
1046 if (meta->base_allocation) {
1047 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1048 NBD_META_ID_BASE_ALLOCATION,
1049 errp);
1050 if (ret < 0) {
1051 return ret;
1055 if (meta->bitmap) {
1056 ret = nbd_negotiate_send_meta_context(client,
1057 meta->exp->export_bitmap_context,
1058 NBD_META_ID_DIRTY_BITMAP,
1059 errp);
1060 if (ret < 0) {
1061 return ret;
1065 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1066 if (ret == 0) {
1067 meta->valid = true;
1070 return ret;
1073 /* nbd_negotiate_options
1074 * Process all NBD_OPT_* client option commands, during fixed newstyle
1075 * negotiation.
1076 * Return:
1077 * -errno on error, errp is set
1078 * 0 on successful negotiation, errp is not set
1079 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1080 * errp is not set
1082 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1084 uint32_t flags;
1085 bool fixedNewstyle = false;
1086 bool no_zeroes = false;
1088 /* Client sends:
1089 [ 0 .. 3] client flags
1091 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1092 [ 0 .. 7] NBD_OPTS_MAGIC
1093 [ 8 .. 11] NBD option
1094 [12 .. 15] Data length
1095 ... Rest of request
1097 [ 0 .. 7] NBD_OPTS_MAGIC
1098 [ 8 .. 11] Second NBD option
1099 [12 .. 15] Data length
1100 ... Rest of request
1103 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1104 return -EIO;
1106 trace_nbd_negotiate_options_flags(flags);
1107 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1108 fixedNewstyle = true;
1109 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1111 if (flags & NBD_FLAG_C_NO_ZEROES) {
1112 no_zeroes = true;
1113 flags &= ~NBD_FLAG_C_NO_ZEROES;
1115 if (flags != 0) {
1116 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1117 return -EINVAL;
1120 while (1) {
1121 int ret;
1122 uint32_t option, length;
1123 uint64_t magic;
1125 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1126 return -EINVAL;
1128 trace_nbd_negotiate_options_check_magic(magic);
1129 if (magic != NBD_OPTS_MAGIC) {
1130 error_setg(errp, "Bad magic received");
1131 return -EINVAL;
1134 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1135 return -EINVAL;
1137 client->opt = option;
1139 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1140 return -EINVAL;
1142 assert(!client->optlen);
1143 client->optlen = length;
1145 if (length > NBD_MAX_BUFFER_SIZE) {
1146 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1147 length, NBD_MAX_BUFFER_SIZE);
1148 return -EINVAL;
1151 trace_nbd_negotiate_options_check_option(option,
1152 nbd_opt_lookup(option));
1153 if (client->tlscreds &&
1154 client->ioc == (QIOChannel *)client->sioc) {
1155 QIOChannel *tioc;
1156 if (!fixedNewstyle) {
1157 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1158 return -EINVAL;
1160 switch (option) {
1161 case NBD_OPT_STARTTLS:
1162 if (length) {
1163 /* Unconditionally drop the connection if the client
1164 * can't start a TLS negotiation correctly */
1165 return nbd_reject_length(client, true, errp);
1167 tioc = nbd_negotiate_handle_starttls(client, errp);
1168 if (!tioc) {
1169 return -EIO;
1171 ret = 0;
1172 object_unref(OBJECT(client->ioc));
1173 client->ioc = QIO_CHANNEL(tioc);
1174 break;
1176 case NBD_OPT_EXPORT_NAME:
1177 /* No way to return an error to client, so drop connection */
1178 error_setg(errp, "Option 0x%x not permitted before TLS",
1179 option);
1180 return -EINVAL;
1182 default:
1183 /* Let the client keep trying, unless they asked to
1184 * quit. Always try to give an error back to the
1185 * client; but when replying to OPT_ABORT, be aware
1186 * that the client may hang up before receiving the
1187 * error, in which case we are fine ignoring the
1188 * resulting EPIPE. */
1189 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1190 option == NBD_OPT_ABORT ? NULL : errp,
1191 "Option 0x%" PRIx32
1192 " not permitted before TLS", option);
1193 if (option == NBD_OPT_ABORT) {
1194 return 1;
1196 break;
1198 } else if (fixedNewstyle) {
1199 switch (option) {
1200 case NBD_OPT_LIST:
1201 if (length) {
1202 ret = nbd_reject_length(client, false, errp);
1203 } else {
1204 ret = nbd_negotiate_handle_list(client, errp);
1206 break;
1208 case NBD_OPT_ABORT:
1209 /* NBD spec says we must try to reply before
1210 * disconnecting, but that we must also tolerate
1211 * guests that don't wait for our reply. */
1212 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1213 return 1;
1215 case NBD_OPT_EXPORT_NAME:
1216 return nbd_negotiate_handle_export_name(client, no_zeroes,
1217 errp);
1219 case NBD_OPT_INFO:
1220 case NBD_OPT_GO:
1221 ret = nbd_negotiate_handle_info(client, errp);
1222 if (ret == 1) {
1223 assert(option == NBD_OPT_GO);
1224 return 0;
1226 break;
1228 case NBD_OPT_STARTTLS:
1229 if (length) {
1230 ret = nbd_reject_length(client, false, errp);
1231 } else if (client->tlscreds) {
1232 ret = nbd_negotiate_send_rep_err(client,
1233 NBD_REP_ERR_INVALID, errp,
1234 "TLS already enabled");
1235 } else {
1236 ret = nbd_negotiate_send_rep_err(client,
1237 NBD_REP_ERR_POLICY, errp,
1238 "TLS not configured");
1240 break;
1242 case NBD_OPT_STRUCTURED_REPLY:
1243 if (length) {
1244 ret = nbd_reject_length(client, false, errp);
1245 } else if (client->structured_reply) {
1246 ret = nbd_negotiate_send_rep_err(
1247 client, NBD_REP_ERR_INVALID, errp,
1248 "structured reply already negotiated");
1249 } else {
1250 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1251 client->structured_reply = true;
1253 break;
1255 case NBD_OPT_LIST_META_CONTEXT:
1256 case NBD_OPT_SET_META_CONTEXT:
1257 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1258 errp);
1259 break;
1261 default:
1262 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1263 "Unsupported option %" PRIu32 " (%s)",
1264 option, nbd_opt_lookup(option));
1265 break;
1267 } else {
1269 * If broken new-style we should drop the connection
1270 * for anything except NBD_OPT_EXPORT_NAME
1272 switch (option) {
1273 case NBD_OPT_EXPORT_NAME:
1274 return nbd_negotiate_handle_export_name(client, no_zeroes,
1275 errp);
1277 default:
1278 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1279 option, nbd_opt_lookup(option));
1280 return -EINVAL;
1283 if (ret < 0) {
1284 return ret;
1289 /* nbd_negotiate
1290 * Return:
1291 * -errno on error, errp is set
1292 * 0 on successful negotiation, errp is not set
1293 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1294 * errp is not set
1296 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1298 ERRP_GUARD();
1299 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1300 int ret;
1302 /* Old style negotiation header, no room for options
1303 [ 0 .. 7] passwd ("NBDMAGIC")
1304 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1305 [16 .. 23] size
1306 [24 .. 27] export flags (zero-extended)
1307 [28 .. 151] reserved (0)
1309 New style negotiation header, client can send options
1310 [ 0 .. 7] passwd ("NBDMAGIC")
1311 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1312 [16 .. 17] server flags (0)
1313 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1316 qio_channel_set_blocking(client->ioc, false, NULL);
1318 trace_nbd_negotiate_begin();
1319 memcpy(buf, "NBDMAGIC", 8);
1321 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1322 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1324 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1325 error_prepend(errp, "write failed: ");
1326 return -EINVAL;
1328 ret = nbd_negotiate_options(client, errp);
1329 if (ret != 0) {
1330 if (ret < 0) {
1331 error_prepend(errp, "option negotiation failed: ");
1333 return ret;
1336 /* Attach the channel to the same AioContext as the export */
1337 if (client->exp && client->exp->ctx) {
1338 qio_channel_attach_aio_context(client->ioc, client->exp->ctx);
1341 assert(!client->optlen);
1342 trace_nbd_negotiate_success();
1344 return 0;
1347 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1348 Error **errp)
1350 uint8_t buf[NBD_REQUEST_SIZE];
1351 uint32_t magic;
1352 int ret;
1354 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1355 if (ret < 0) {
1356 return ret;
1359 /* Request
1360 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1361 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1362 [ 6 .. 7] type (NBD_CMD_READ, ...)
1363 [ 8 .. 15] handle
1364 [16 .. 23] from
1365 [24 .. 27] len
1368 magic = ldl_be_p(buf);
1369 request->flags = lduw_be_p(buf + 4);
1370 request->type = lduw_be_p(buf + 6);
1371 request->handle = ldq_be_p(buf + 8);
1372 request->from = ldq_be_p(buf + 16);
1373 request->len = ldl_be_p(buf + 24);
1375 trace_nbd_receive_request(magic, request->flags, request->type,
1376 request->from, request->len);
1378 if (magic != NBD_REQUEST_MAGIC) {
1379 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1380 return -EINVAL;
1382 return 0;
1385 #define MAX_NBD_REQUESTS 16
1387 void nbd_client_get(NBDClient *client)
1389 client->refcount++;
1392 void nbd_client_put(NBDClient *client)
1394 if (--client->refcount == 0) {
1395 /* The last reference should be dropped by client->close,
1396 * which is called by client_close.
1398 assert(client->closing);
1400 qio_channel_detach_aio_context(client->ioc);
1401 object_unref(OBJECT(client->sioc));
1402 object_unref(OBJECT(client->ioc));
1403 if (client->tlscreds) {
1404 object_unref(OBJECT(client->tlscreds));
1406 g_free(client->tlsauthz);
1407 if (client->exp) {
1408 QTAILQ_REMOVE(&client->exp->clients, client, next);
1409 nbd_export_put(client->exp);
1411 g_free(client);
1415 static void client_close(NBDClient *client, bool negotiated)
1417 if (client->closing) {
1418 return;
1421 client->closing = true;
1423 /* Force requests to finish. They will drop their own references,
1424 * then we'll close the socket and free the NBDClient.
1426 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1427 NULL);
1429 /* Also tell the client, so that they release their reference. */
1430 if (client->close_fn) {
1431 client->close_fn(client, negotiated);
1435 static NBDRequestData *nbd_request_get(NBDClient *client)
1437 NBDRequestData *req;
1439 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1440 client->nb_requests++;
1442 req = g_new0(NBDRequestData, 1);
1443 nbd_client_get(client);
1444 req->client = client;
1445 return req;
1448 static void nbd_request_put(NBDRequestData *req)
1450 NBDClient *client = req->client;
1452 if (req->data) {
1453 qemu_vfree(req->data);
1455 g_free(req);
1457 client->nb_requests--;
1458 nbd_client_receive_next_request(client);
1460 nbd_client_put(client);
1463 static void blk_aio_attached(AioContext *ctx, void *opaque)
1465 NBDExport *exp = opaque;
1466 NBDClient *client;
1468 trace_nbd_blk_aio_attached(exp->name, ctx);
1470 exp->ctx = ctx;
1472 QTAILQ_FOREACH(client, &exp->clients, next) {
1473 qio_channel_attach_aio_context(client->ioc, ctx);
1474 if (client->recv_coroutine) {
1475 aio_co_schedule(ctx, client->recv_coroutine);
1477 if (client->send_coroutine) {
1478 aio_co_schedule(ctx, client->send_coroutine);
1483 static void blk_aio_detach(void *opaque)
1485 NBDExport *exp = opaque;
1486 NBDClient *client;
1488 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1490 QTAILQ_FOREACH(client, &exp->clients, next) {
1491 qio_channel_detach_aio_context(client->ioc);
1494 exp->ctx = NULL;
1497 static void nbd_eject_notifier(Notifier *n, void *data)
1499 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1500 AioContext *aio_context;
1502 aio_context = exp->ctx;
1503 aio_context_acquire(aio_context);
1504 nbd_export_close(exp);
1505 aio_context_release(aio_context);
1508 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1510 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1511 assert(exp->drv == &blk_exp_nbd);
1512 assert(nbd_exp->eject_notifier_blk == NULL);
1514 blk_ref(blk);
1515 nbd_exp->eject_notifier_blk = blk;
1516 nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1517 blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1520 NBDExport *nbd_export_new(BlockDriverState *bs,
1521 const char *name, const char *desc,
1522 const char *bitmap, bool readonly, bool shared,
1523 bool writethrough, Error **errp)
1525 AioContext *ctx;
1526 BlockBackend *blk;
1527 NBDExport *exp;
1528 int64_t size;
1529 uint64_t perm;
1530 int ret;
1532 size = bdrv_getlength(bs);
1533 if (size < 0) {
1534 error_setg_errno(errp, -size,
1535 "Failed to determine the NBD export's length");
1536 return NULL;
1539 exp = g_new0(NBDExport, 1);
1540 exp->common = (BlockExport) {
1541 .drv = &blk_exp_nbd,
1545 * NBD exports are used for non-shared storage migration. Make sure
1546 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1547 * access since the export could be available before migration handover.
1548 * ctx was acquired in the caller.
1550 assert(name && strlen(name) <= NBD_MAX_STRING_SIZE);
1551 ctx = bdrv_get_aio_context(bs);
1552 bdrv_invalidate_cache(bs, NULL);
1554 /* Don't allow resize while the NBD server is running, otherwise we don't
1555 * care what happens with the node. */
1556 perm = BLK_PERM_CONSISTENT_READ;
1557 if (!readonly) {
1558 perm |= BLK_PERM_WRITE;
1560 blk = blk_new(ctx, perm,
1561 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1562 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1563 ret = blk_insert_bs(blk, bs, errp);
1564 if (ret < 0) {
1565 goto fail;
1567 blk_set_enable_write_cache(blk, !writethrough);
1568 blk_set_allow_aio_context_change(blk, true);
1570 exp->refcount = 1;
1571 QTAILQ_INIT(&exp->clients);
1572 exp->blk = blk;
1573 exp->name = g_strdup(name);
1574 assert(!desc || strlen(desc) <= NBD_MAX_STRING_SIZE);
1575 exp->description = g_strdup(desc);
1576 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1577 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1578 if (readonly) {
1579 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1580 if (shared) {
1581 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1583 } else {
1584 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1585 NBD_FLAG_SEND_FAST_ZERO);
1587 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1589 if (bitmap) {
1590 BdrvDirtyBitmap *bm = NULL;
1592 while (bs) {
1593 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1594 if (bm != NULL) {
1595 break;
1598 bs = bdrv_filter_or_cow_bs(bs);
1601 if (bm == NULL) {
1602 error_setg(errp, "Bitmap '%s' is not found", bitmap);
1603 goto fail;
1606 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1607 goto fail;
1610 if (readonly && bdrv_is_writable(bs) &&
1611 bdrv_dirty_bitmap_enabled(bm)) {
1612 error_setg(errp,
1613 "Enabled bitmap '%s' incompatible with readonly export",
1614 bitmap);
1615 goto fail;
1618 bdrv_dirty_bitmap_set_busy(bm, true);
1619 exp->export_bitmap = bm;
1620 assert(strlen(bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
1621 exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1622 bitmap);
1623 assert(strlen(exp->export_bitmap_context) < NBD_MAX_STRING_SIZE);
1626 exp->ctx = ctx;
1627 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1629 QTAILQ_INSERT_TAIL(&exports, exp, next);
1630 nbd_export_get(exp);
1631 return exp;
1633 fail:
1634 blk_unref(blk);
1635 g_free(exp->name);
1636 g_free(exp->description);
1637 g_free(exp);
1638 return NULL;
1641 NBDExport *nbd_export_find(const char *name)
1643 NBDExport *exp;
1644 QTAILQ_FOREACH(exp, &exports, next) {
1645 if (strcmp(name, exp->name) == 0) {
1646 return exp;
1650 return NULL;
1653 AioContext *
1654 nbd_export_aio_context(NBDExport *exp)
1656 return exp->ctx;
1659 void nbd_export_close(NBDExport *exp)
1661 NBDClient *client, *next;
1663 nbd_export_get(exp);
1665 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1666 * close mode that stops advertising the export to new clients but
1667 * still permits existing clients to run to completion? Because of
1668 * that possibility, nbd_export_close() can be called more than
1669 * once on an export.
1671 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1672 client_close(client, true);
1674 if (exp->name) {
1675 nbd_export_put(exp);
1676 g_free(exp->name);
1677 exp->name = NULL;
1678 QTAILQ_REMOVE(&exports, exp, next);
1679 QTAILQ_INSERT_TAIL(&closed_exports, exp, next);
1681 nbd_export_put(exp);
1684 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1686 ERRP_GUARD();
1687 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1688 nbd_export_close(exp);
1689 return;
1692 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1694 error_setg(errp, "export '%s' still in use", exp->name);
1695 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1698 void nbd_export_get(NBDExport *exp)
1700 assert(exp->refcount > 0);
1701 exp->refcount++;
1704 void nbd_export_put(NBDExport *exp)
1706 assert(exp->refcount > 0);
1707 if (--exp->refcount == 0) {
1708 assert(exp->name == NULL);
1709 assert(QTAILQ_EMPTY(&exp->clients));
1711 g_free(exp->description);
1712 exp->description = NULL;
1714 if (exp->blk) {
1715 if (exp->eject_notifier_blk) {
1716 notifier_remove(&exp->eject_notifier);
1717 blk_unref(exp->eject_notifier_blk);
1719 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1720 blk_aio_detach, exp);
1721 blk_unref(exp->blk);
1722 exp->blk = NULL;
1725 if (exp->export_bitmap) {
1726 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1727 g_free(exp->export_bitmap_context);
1730 QTAILQ_REMOVE(&closed_exports, exp, next);
1731 g_free(exp);
1732 aio_wait_kick();
1736 const BlockExportDriver blk_exp_nbd = {
1737 .type = BLOCK_EXPORT_TYPE_NBD,
1738 .create = nbd_export_create,
1741 void nbd_export_close_all(void)
1743 NBDExport *exp, *next;
1744 AioContext *aio_context;
1746 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1747 aio_context = exp->ctx;
1748 aio_context_acquire(aio_context);
1749 nbd_export_close(exp);
1750 aio_context_release(aio_context);
1753 AIO_WAIT_WHILE(NULL, !(QTAILQ_EMPTY(&exports) &&
1754 QTAILQ_EMPTY(&closed_exports)));
1757 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1758 unsigned niov, Error **errp)
1760 int ret;
1762 g_assert(qemu_in_coroutine());
1763 qemu_co_mutex_lock(&client->send_lock);
1764 client->send_coroutine = qemu_coroutine_self();
1766 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1768 client->send_coroutine = NULL;
1769 qemu_co_mutex_unlock(&client->send_lock);
1771 return ret;
1774 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1775 uint64_t handle)
1777 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1778 stl_be_p(&reply->error, error);
1779 stq_be_p(&reply->handle, handle);
1782 static int nbd_co_send_simple_reply(NBDClient *client,
1783 uint64_t handle,
1784 uint32_t error,
1785 void *data,
1786 size_t len,
1787 Error **errp)
1789 NBDSimpleReply reply;
1790 int nbd_err = system_errno_to_nbd_errno(error);
1791 struct iovec iov[] = {
1792 {.iov_base = &reply, .iov_len = sizeof(reply)},
1793 {.iov_base = data, .iov_len = len}
1796 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1797 len);
1798 set_be_simple_reply(&reply, nbd_err, handle);
1800 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1803 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1804 uint16_t type, uint64_t handle, uint32_t length)
1806 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1807 stw_be_p(&chunk->flags, flags);
1808 stw_be_p(&chunk->type, type);
1809 stq_be_p(&chunk->handle, handle);
1810 stl_be_p(&chunk->length, length);
1813 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1814 uint64_t handle,
1815 Error **errp)
1817 NBDStructuredReplyChunk chunk;
1818 struct iovec iov[] = {
1819 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1822 trace_nbd_co_send_structured_done(handle);
1823 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1825 return nbd_co_send_iov(client, iov, 1, errp);
1828 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1829 uint64_t handle,
1830 uint64_t offset,
1831 void *data,
1832 size_t size,
1833 bool final,
1834 Error **errp)
1836 NBDStructuredReadData chunk;
1837 struct iovec iov[] = {
1838 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1839 {.iov_base = data, .iov_len = size}
1842 assert(size);
1843 trace_nbd_co_send_structured_read(handle, offset, data, size);
1844 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1845 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1846 sizeof(chunk) - sizeof(chunk.h) + size);
1847 stq_be_p(&chunk.offset, offset);
1849 return nbd_co_send_iov(client, iov, 2, errp);
1852 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1853 uint64_t handle,
1854 uint32_t error,
1855 const char *msg,
1856 Error **errp)
1858 NBDStructuredError chunk;
1859 int nbd_err = system_errno_to_nbd_errno(error);
1860 struct iovec iov[] = {
1861 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1862 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1865 assert(nbd_err);
1866 trace_nbd_co_send_structured_error(handle, nbd_err,
1867 nbd_err_lookup(nbd_err), msg ? msg : "");
1868 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1869 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1870 stl_be_p(&chunk.error, nbd_err);
1871 stw_be_p(&chunk.message_length, iov[1].iov_len);
1873 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1876 /* Do a sparse read and send the structured reply to the client.
1877 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1878 * reported to the client, at which point this function succeeds.
1880 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1881 uint64_t handle,
1882 uint64_t offset,
1883 uint8_t *data,
1884 size_t size,
1885 Error **errp)
1887 int ret = 0;
1888 NBDExport *exp = client->exp;
1889 size_t progress = 0;
1891 while (progress < size) {
1892 int64_t pnum;
1893 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1894 offset + progress,
1895 size - progress, &pnum, NULL,
1896 NULL);
1897 bool final;
1899 if (status < 0) {
1900 char *msg = g_strdup_printf("unable to check for holes: %s",
1901 strerror(-status));
1903 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1904 errp);
1905 g_free(msg);
1906 return ret;
1908 assert(pnum && pnum <= size - progress);
1909 final = progress + pnum == size;
1910 if (status & BDRV_BLOCK_ZERO) {
1911 NBDStructuredReadHole chunk;
1912 struct iovec iov[] = {
1913 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1916 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1917 pnum);
1918 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1919 NBD_REPLY_TYPE_OFFSET_HOLE,
1920 handle, sizeof(chunk) - sizeof(chunk.h));
1921 stq_be_p(&chunk.offset, offset + progress);
1922 stl_be_p(&chunk.length, pnum);
1923 ret = nbd_co_send_iov(client, iov, 1, errp);
1924 } else {
1925 ret = blk_pread(exp->blk, offset + progress, data + progress, pnum);
1926 if (ret < 0) {
1927 error_setg_errno(errp, -ret, "reading from file failed");
1928 break;
1930 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1931 data + progress, pnum, final,
1932 errp);
1935 if (ret < 0) {
1936 break;
1938 progress += pnum;
1940 return ret;
1943 typedef struct NBDExtentArray {
1944 NBDExtent *extents;
1945 unsigned int nb_alloc;
1946 unsigned int count;
1947 uint64_t total_length;
1948 bool can_add;
1949 bool converted_to_be;
1950 } NBDExtentArray;
1952 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
1954 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
1956 ea->nb_alloc = nb_alloc;
1957 ea->extents = g_new(NBDExtent, nb_alloc);
1958 ea->can_add = true;
1960 return ea;
1963 static void nbd_extent_array_free(NBDExtentArray *ea)
1965 g_free(ea->extents);
1966 g_free(ea);
1968 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
1970 /* Further modifications of the array after conversion are abandoned */
1971 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
1973 int i;
1975 assert(!ea->converted_to_be);
1976 ea->can_add = false;
1977 ea->converted_to_be = true;
1979 for (i = 0; i < ea->count; i++) {
1980 ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
1981 ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
1986 * Add extent to NBDExtentArray. If extent can't be added (no available space),
1987 * return -1.
1988 * For safety, when returning -1 for the first time, .can_add is set to false,
1989 * further call to nbd_extent_array_add() will crash.
1990 * (to avoid the situation, when after failing to add an extent (returned -1),
1991 * user miss this failure and add another extent, which is successfully added
1992 * (array is full, but new extent may be squashed into the last one), then we
1993 * have invalid array with skipped extent)
1995 static int nbd_extent_array_add(NBDExtentArray *ea,
1996 uint32_t length, uint32_t flags)
1998 assert(ea->can_add);
2000 if (!length) {
2001 return 0;
2004 /* Extend previous extent if flags are the same */
2005 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
2006 uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
2008 if (sum <= UINT32_MAX) {
2009 ea->extents[ea->count - 1].length = sum;
2010 ea->total_length += length;
2011 return 0;
2015 if (ea->count >= ea->nb_alloc) {
2016 ea->can_add = false;
2017 return -1;
2020 ea->total_length += length;
2021 ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
2022 ea->count++;
2024 return 0;
2027 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
2028 uint64_t bytes, NBDExtentArray *ea)
2030 while (bytes) {
2031 uint32_t flags;
2032 int64_t num;
2033 int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
2034 NULL, NULL);
2036 if (ret < 0) {
2037 return ret;
2040 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
2041 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
2043 if (nbd_extent_array_add(ea, num, flags) < 0) {
2044 return 0;
2047 offset += num;
2048 bytes -= num;
2051 return 0;
2055 * nbd_co_send_extents
2057 * @ea is converted to BE by the function
2058 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2060 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
2061 NBDExtentArray *ea,
2062 bool last, uint32_t context_id, Error **errp)
2064 NBDStructuredMeta chunk;
2065 struct iovec iov[] = {
2066 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2067 {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
2070 nbd_extent_array_convert_to_be(ea);
2072 trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2073 last);
2074 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2075 NBD_REPLY_TYPE_BLOCK_STATUS,
2076 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2077 stl_be_p(&chunk.context_id, context_id);
2079 return nbd_co_send_iov(client, iov, 2, errp);
2082 /* Get block status from the exported device and send it to the client */
2083 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2084 BlockDriverState *bs, uint64_t offset,
2085 uint32_t length, bool dont_fragment,
2086 bool last, uint32_t context_id,
2087 Error **errp)
2089 int ret;
2090 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2091 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2093 ret = blockstatus_to_extents(bs, offset, length, ea);
2094 if (ret < 0) {
2095 return nbd_co_send_structured_error(
2096 client, handle, -ret, "can't get block status", errp);
2099 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2102 /* Populate @ea from a dirty bitmap. */
2103 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2104 uint64_t offset, uint64_t length,
2105 NBDExtentArray *es)
2107 int64_t start, dirty_start, dirty_count;
2108 int64_t end = offset + length;
2109 bool full = false;
2111 bdrv_dirty_bitmap_lock(bitmap);
2113 for (start = offset;
2114 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2115 &dirty_start, &dirty_count);
2116 start = dirty_start + dirty_count)
2118 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2119 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2121 full = true;
2122 break;
2126 if (!full) {
2127 /* last non dirty extent */
2128 nbd_extent_array_add(es, end - start, 0);
2131 bdrv_dirty_bitmap_unlock(bitmap);
2134 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2135 BdrvDirtyBitmap *bitmap, uint64_t offset,
2136 uint32_t length, bool dont_fragment, bool last,
2137 uint32_t context_id, Error **errp)
2139 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2140 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2142 bitmap_to_extents(bitmap, offset, length, ea);
2144 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2147 /* nbd_co_receive_request
2148 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2149 * connection right away, and any other negative value to report an error to
2150 * the client (although the caller may still need to disconnect after reporting
2151 * the error).
2153 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2154 Error **errp)
2156 NBDClient *client = req->client;
2157 int valid_flags;
2159 g_assert(qemu_in_coroutine());
2160 assert(client->recv_coroutine == qemu_coroutine_self());
2161 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2162 return -EIO;
2165 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2166 nbd_cmd_lookup(request->type));
2168 if (request->type != NBD_CMD_WRITE) {
2169 /* No payload, we are ready to read the next request. */
2170 req->complete = true;
2173 if (request->type == NBD_CMD_DISC) {
2174 /* Special case: we're going to disconnect without a reply,
2175 * whether or not flags, from, or len are bogus */
2176 return -EIO;
2179 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2180 request->type == NBD_CMD_CACHE)
2182 if (request->len > NBD_MAX_BUFFER_SIZE) {
2183 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2184 request->len, NBD_MAX_BUFFER_SIZE);
2185 return -EINVAL;
2188 if (request->type != NBD_CMD_CACHE) {
2189 req->data = blk_try_blockalign(client->exp->blk, request->len);
2190 if (req->data == NULL) {
2191 error_setg(errp, "No memory");
2192 return -ENOMEM;
2197 if (request->type == NBD_CMD_WRITE) {
2198 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2199 errp) < 0)
2201 return -EIO;
2203 req->complete = true;
2205 trace_nbd_co_receive_request_payload_received(request->handle,
2206 request->len);
2209 /* Sanity checks. */
2210 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2211 (request->type == NBD_CMD_WRITE ||
2212 request->type == NBD_CMD_WRITE_ZEROES ||
2213 request->type == NBD_CMD_TRIM)) {
2214 error_setg(errp, "Export is read-only");
2215 return -EROFS;
2217 if (request->from > client->exp->size ||
2218 request->len > client->exp->size - request->from) {
2219 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2220 ", Size: %" PRIu64, request->from, request->len,
2221 client->exp->size);
2222 return (request->type == NBD_CMD_WRITE ||
2223 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2225 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2226 client->check_align)) {
2228 * The block layer gracefully handles unaligned requests, but
2229 * it's still worth tracing client non-compliance
2231 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2232 request->from,
2233 request->len,
2234 client->check_align);
2236 valid_flags = NBD_CMD_FLAG_FUA;
2237 if (request->type == NBD_CMD_READ && client->structured_reply) {
2238 valid_flags |= NBD_CMD_FLAG_DF;
2239 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2240 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2241 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2242 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2244 if (request->flags & ~valid_flags) {
2245 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2246 nbd_cmd_lookup(request->type), request->flags);
2247 return -EINVAL;
2250 return 0;
2253 /* Send simple reply without a payload, or a structured error
2254 * @error_msg is ignored if @ret >= 0
2255 * Returns 0 if connection is still live, -errno on failure to talk to client
2257 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2258 uint64_t handle,
2259 int ret,
2260 const char *error_msg,
2261 Error **errp)
2263 if (client->structured_reply && ret < 0) {
2264 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2265 errp);
2266 } else {
2267 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2268 NULL, 0, errp);
2272 /* Handle NBD_CMD_READ request.
2273 * Return -errno if sending fails. Other errors are reported directly to the
2274 * client as an error reply. */
2275 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2276 uint8_t *data, Error **errp)
2278 int ret;
2279 NBDExport *exp = client->exp;
2281 assert(request->type == NBD_CMD_READ);
2283 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2284 if (request->flags & NBD_CMD_FLAG_FUA) {
2285 ret = blk_co_flush(exp->blk);
2286 if (ret < 0) {
2287 return nbd_send_generic_reply(client, request->handle, ret,
2288 "flush failed", errp);
2292 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2293 request->len)
2295 return nbd_co_send_sparse_read(client, request->handle, request->from,
2296 data, request->len, errp);
2299 ret = blk_pread(exp->blk, request->from, data, request->len);
2300 if (ret < 0) {
2301 return nbd_send_generic_reply(client, request->handle, ret,
2302 "reading from file failed", errp);
2305 if (client->structured_reply) {
2306 if (request->len) {
2307 return nbd_co_send_structured_read(client, request->handle,
2308 request->from, data,
2309 request->len, true, errp);
2310 } else {
2311 return nbd_co_send_structured_done(client, request->handle, errp);
2313 } else {
2314 return nbd_co_send_simple_reply(client, request->handle, 0,
2315 data, request->len, errp);
2320 * nbd_do_cmd_cache
2322 * Handle NBD_CMD_CACHE request.
2323 * Return -errno if sending fails. Other errors are reported directly to the
2324 * client as an error reply.
2326 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2327 Error **errp)
2329 int ret;
2330 NBDExport *exp = client->exp;
2332 assert(request->type == NBD_CMD_CACHE);
2334 ret = blk_co_preadv(exp->blk, request->from, request->len,
2335 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2337 return nbd_send_generic_reply(client, request->handle, ret,
2338 "caching data failed", errp);
2341 /* Handle NBD request.
2342 * Return -errno if sending fails. Other errors are reported directly to the
2343 * client as an error reply. */
2344 static coroutine_fn int nbd_handle_request(NBDClient *client,
2345 NBDRequest *request,
2346 uint8_t *data, Error **errp)
2348 int ret;
2349 int flags;
2350 NBDExport *exp = client->exp;
2351 char *msg;
2353 switch (request->type) {
2354 case NBD_CMD_CACHE:
2355 return nbd_do_cmd_cache(client, request, errp);
2357 case NBD_CMD_READ:
2358 return nbd_do_cmd_read(client, request, data, errp);
2360 case NBD_CMD_WRITE:
2361 flags = 0;
2362 if (request->flags & NBD_CMD_FLAG_FUA) {
2363 flags |= BDRV_REQ_FUA;
2365 ret = blk_pwrite(exp->blk, request->from, data, request->len, flags);
2366 return nbd_send_generic_reply(client, request->handle, ret,
2367 "writing to file failed", errp);
2369 case NBD_CMD_WRITE_ZEROES:
2370 flags = 0;
2371 if (request->flags & NBD_CMD_FLAG_FUA) {
2372 flags |= BDRV_REQ_FUA;
2374 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2375 flags |= BDRV_REQ_MAY_UNMAP;
2377 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2378 flags |= BDRV_REQ_NO_FALLBACK;
2380 ret = 0;
2381 /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2382 while (ret >= 0 && request->len) {
2383 int align = client->check_align ?: 1;
2384 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2385 align));
2386 ret = blk_pwrite_zeroes(exp->blk, request->from, len, flags);
2387 request->len -= len;
2388 request->from += len;
2390 return nbd_send_generic_reply(client, request->handle, ret,
2391 "writing to file failed", errp);
2393 case NBD_CMD_DISC:
2394 /* unreachable, thanks to special case in nbd_co_receive_request() */
2395 abort();
2397 case NBD_CMD_FLUSH:
2398 ret = blk_co_flush(exp->blk);
2399 return nbd_send_generic_reply(client, request->handle, ret,
2400 "flush failed", errp);
2402 case NBD_CMD_TRIM:
2403 ret = 0;
2404 /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2405 while (ret >= 0 && request->len) {
2406 int align = client->check_align ?: 1;
2407 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2408 align));
2409 ret = blk_co_pdiscard(exp->blk, request->from, len);
2410 request->len -= len;
2411 request->from += len;
2413 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2414 ret = blk_co_flush(exp->blk);
2416 return nbd_send_generic_reply(client, request->handle, ret,
2417 "discard failed", errp);
2419 case NBD_CMD_BLOCK_STATUS:
2420 if (!request->len) {
2421 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2422 "need non-zero length", errp);
2424 if (client->export_meta.valid &&
2425 (client->export_meta.base_allocation ||
2426 client->export_meta.bitmap))
2428 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2430 if (client->export_meta.base_allocation) {
2431 ret = nbd_co_send_block_status(client, request->handle,
2432 blk_bs(exp->blk), request->from,
2433 request->len, dont_fragment,
2434 !client->export_meta.bitmap,
2435 NBD_META_ID_BASE_ALLOCATION,
2436 errp);
2437 if (ret < 0) {
2438 return ret;
2442 if (client->export_meta.bitmap) {
2443 ret = nbd_co_send_bitmap(client, request->handle,
2444 client->exp->export_bitmap,
2445 request->from, request->len,
2446 dont_fragment,
2447 true, NBD_META_ID_DIRTY_BITMAP, errp);
2448 if (ret < 0) {
2449 return ret;
2453 return 0;
2454 } else {
2455 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2456 "CMD_BLOCK_STATUS not negotiated",
2457 errp);
2460 default:
2461 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2462 request->type);
2463 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2464 errp);
2465 g_free(msg);
2466 return ret;
2470 /* Owns a reference to the NBDClient passed as opaque. */
2471 static coroutine_fn void nbd_trip(void *opaque)
2473 NBDClient *client = opaque;
2474 NBDRequestData *req;
2475 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2476 int ret;
2477 Error *local_err = NULL;
2479 trace_nbd_trip();
2480 if (client->closing) {
2481 nbd_client_put(client);
2482 return;
2485 req = nbd_request_get(client);
2486 ret = nbd_co_receive_request(req, &request, &local_err);
2487 client->recv_coroutine = NULL;
2489 if (client->closing) {
2491 * The client may be closed when we are blocked in
2492 * nbd_co_receive_request()
2494 goto done;
2497 nbd_client_receive_next_request(client);
2498 if (ret == -EIO) {
2499 goto disconnect;
2502 if (ret < 0) {
2503 /* It wans't -EIO, so, according to nbd_co_receive_request()
2504 * semantics, we should return the error to the client. */
2505 Error *export_err = local_err;
2507 local_err = NULL;
2508 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2509 error_get_pretty(export_err), &local_err);
2510 error_free(export_err);
2511 } else {
2512 ret = nbd_handle_request(client, &request, req->data, &local_err);
2514 if (ret < 0) {
2515 error_prepend(&local_err, "Failed to send reply: ");
2516 goto disconnect;
2519 /* We must disconnect after NBD_CMD_WRITE if we did not
2520 * read the payload.
2522 if (!req->complete) {
2523 error_setg(&local_err, "Request handling failed in intermediate state");
2524 goto disconnect;
2527 done:
2528 nbd_request_put(req);
2529 nbd_client_put(client);
2530 return;
2532 disconnect:
2533 if (local_err) {
2534 error_reportf_err(local_err, "Disconnect client, due to: ");
2536 nbd_request_put(req);
2537 client_close(client, true);
2538 nbd_client_put(client);
2541 static void nbd_client_receive_next_request(NBDClient *client)
2543 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2544 nbd_client_get(client);
2545 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2546 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
2550 static coroutine_fn void nbd_co_client_start(void *opaque)
2552 NBDClient *client = opaque;
2553 Error *local_err = NULL;
2555 qemu_co_mutex_init(&client->send_lock);
2557 if (nbd_negotiate(client, &local_err)) {
2558 if (local_err) {
2559 error_report_err(local_err);
2561 client_close(client, false);
2562 return;
2565 nbd_client_receive_next_request(client);
2569 * Create a new client listener using the given channel @sioc.
2570 * Begin servicing it in a coroutine. When the connection closes, call
2571 * @close_fn with an indication of whether the client completed negotiation.
2573 void nbd_client_new(QIOChannelSocket *sioc,
2574 QCryptoTLSCreds *tlscreds,
2575 const char *tlsauthz,
2576 void (*close_fn)(NBDClient *, bool))
2578 NBDClient *client;
2579 Coroutine *co;
2581 client = g_new0(NBDClient, 1);
2582 client->refcount = 1;
2583 client->tlscreds = tlscreds;
2584 if (tlscreds) {
2585 object_ref(OBJECT(client->tlscreds));
2587 client->tlsauthz = g_strdup(tlsauthz);
2588 client->sioc = sioc;
2589 object_ref(OBJECT(client->sioc));
2590 client->ioc = QIO_CHANNEL(sioc);
2591 object_ref(OBJECT(client->ioc));
2592 client->close_fn = close_fn;
2594 co = qemu_coroutine_create(nbd_co_client_start, client);
2595 qemu_coroutine_enter(co);