nbd: Remove NBDExport.close callback
[qemu.git] / nbd / server.c
1 /*
2 * Copyright (C) 2016-2018 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 *
5 * Network Block Device Server Side
6 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include "qemu/osdep.h"
21
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"
28
29 #define NBD_META_ID_BASE_ALLOCATION 0
30 #define NBD_META_ID_DIRTY_BITMAP 1
31
32 /*
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.
37 */
38 #define NBD_MAX_BLOCK_STATUS_EXTENTS (1 * MiB / 8)
39
40 static int system_errno_to_nbd_errno(int err)
41 {
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;
70 }
71 }
72
73 /* Definitions for opaque data types */
74
75 typedef struct NBDRequestData NBDRequestData;
76
77 struct NBDRequestData {
78 QSIMPLEQ_ENTRY(NBDRequestData) entry;
79 NBDClient *client;
80 uint8_t *data;
81 bool complete;
82 };
83
84 struct NBDExport {
85 BlockExport common;
86 int refcount;
87
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;
95
96 AioContext *ctx;
97
98 BlockBackend *eject_notifier_blk;
99 Notifier eject_notifier;
100
101 BdrvDirtyBitmap *export_bitmap;
102 char *export_bitmap_context;
103 };
104
105 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
106 static QTAILQ_HEAD(, NBDExport) closed_exports =
107 QTAILQ_HEAD_INITIALIZER(closed_exports);
108
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;
119
120 struct NBDClient {
121 int refcount;
122 void (*close_fn)(NBDClient *client, bool negotiated);
123
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) */
129
130 Coroutine *recv_coroutine;
131
132 CoMutex send_lock;
133 Coroutine *send_coroutine;
134
135 QTAILQ_ENTRY(NBDClient) next;
136 int nb_requests;
137 bool closing;
138
139 uint32_t check_align; /* If non-zero, check for aligned client requests */
140
141 bool structured_reply;
142 NBDExportMetaContexts export_meta;
143
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 */
147 };
148
149 static void nbd_client_receive_next_request(NBDClient *client);
150
151 /* Basic flow for negotiation
152
153 Server Client
154 Negotiate
155
156 or
157
158 Server Client
159 Negotiate #1
160 Option
161 Negotiate #2
162
163 ----
164
165 followed by
166
167 Server Client
168 Request
169 Response
170 Request
171 Response
172 ...
173 ...
174 Request (type == 2)
175
176 */
177
178 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
179 uint32_t type, uint32_t length)
180 {
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);
185 }
186
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)
191 {
192 NBDOptionReply rep;
193
194 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
195 type, nbd_rep_lookup(type), len);
196
197 assert(len < NBD_MAX_BUFFER_SIZE);
198
199 set_be_option_rep(&rep, client->opt, type, len);
200 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
201 }
202
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)
207 {
208 return nbd_negotiate_send_rep_len(client, type, 0, errp);
209 }
210
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)
216 {
217 ERRP_GUARD();
218 g_autofree char *msg = NULL;
219 int ret;
220 size_t len;
221
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;
229 }
230 if (nbd_write(client->ioc, msg, len, errp) < 0) {
231 error_prepend(errp, "write failed (error message): ");
232 return -EIO;
233 }
234
235 return 0;
236 }
237
238 /*
239 * Return a malloc'd copy of @name suitable for use in an error reply.
240 */
241 static char *
242 nbd_sanitize_name(const char *name)
243 {
244 if (strnlen(name, 80) < 80) {
245 return g_strdup(name);
246 }
247 /* XXX Should we also try to sanitize any control characters? */
248 return g_strdup_printf("%.80s...", name);
249 }
250
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, ...)
256 {
257 va_list va;
258 int ret;
259
260 va_start(va, fmt);
261 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
262 va_end(va);
263 return ret;
264 }
265
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)
272 {
273 int ret = nbd_drop(client->ioc, client->optlen, errp);
274
275 client->optlen = 0;
276 if (!ret) {
277 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
278 }
279 return ret;
280 }
281
282 static int GCC_FMT_ATTR(4, 5)
283 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
284 const char *fmt, ...)
285 {
286 int ret;
287 va_list va;
288
289 va_start(va, fmt);
290 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
291 va_end(va);
292
293 return ret;
294 }
295
296 static int GCC_FMT_ATTR(3, 4)
297 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
298 {
299 int ret;
300 va_list va;
301
302 va_start(va, fmt);
303 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
304 va_end(va);
305
306 return ret;
307 }
308
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)
314 {
315 if (size > client->optlen) {
316 return nbd_opt_invalid(client, errp,
317 "Inconsistent lengths in option %s",
318 nbd_opt_lookup(client->opt));
319 }
320 client->optlen -= size;
321 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
322 }
323
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)
328 {
329 if (size > client->optlen) {
330 return nbd_opt_invalid(client, errp,
331 "Inconsistent lengths in option %s",
332 nbd_opt_lookup(client->opt));
333 }
334 client->optlen -= size;
335 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
336 }
337
338 /* nbd_opt_read_name
339 *
340 * Read a string with the format:
341 * uint32_t len (<= NBD_MAX_STRING_SIZE)
342 * len bytes string (not 0-terminated)
343 *
344 * On success, @name will be allocated.
345 * If @length is non-null, it will be set to the actual string length.
346 *
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.
349 */
350 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
351 Error **errp)
352 {
353 int ret;
354 uint32_t len;
355 g_autofree char *local_name = NULL;
356
357 *name = NULL;
358 ret = nbd_opt_read(client, &len, sizeof(len), errp);
359 if (ret <= 0) {
360 return ret;
361 }
362 len = cpu_to_be32(len);
363
364 if (len > NBD_MAX_STRING_SIZE) {
365 return nbd_opt_invalid(client, errp,
366 "Invalid name length: %" PRIu32, len);
367 }
368
369 local_name = g_malloc(len + 1);
370 ret = nbd_opt_read(client, local_name, len, errp);
371 if (ret <= 0) {
372 return ret;
373 }
374 local_name[len] = '\0';
375
376 if (length) {
377 *length = len;
378 }
379 *name = g_steal_pointer(&local_name);
380
381 return 1;
382 }
383
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)
388 {
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;
396
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;
405 }
406
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;
411 }
412
413 if (nbd_write(ioc, name, name_len, errp) < 0) {
414 error_prepend(errp, "write failed (name buffer): ");
415 return -EINVAL;
416 }
417
418 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
419 error_prepend(errp, "write failed (description buffer): ");
420 return -EINVAL;
421 }
422
423 return 0;
424 }
425
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)
429 {
430 NBDExport *exp;
431 assert(client->opt == NBD_OPT_LIST);
432
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;
437 }
438 }
439 /* Finish with a NBD_REP_ACK. */
440 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
441 }
442
443 static void nbd_check_meta_export(NBDClient *client)
444 {
445 client->export_meta.valid &= client->exp == client->export_meta.exp;
446 }
447
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)
452 {
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;
459
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]
466 */
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;
471 }
472 name = g_malloc(client->optlen + 1);
473 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
474 return -EIO;
475 }
476 name[client->optlen] = '\0';
477 client->optlen = 0;
478
479 trace_nbd_negotiate_handle_export_name_request(name);
480
481 client->exp = nbd_export_find(name);
482 if (!client->exp) {
483 error_setg(errp, "export not found");
484 return -EINVAL;
485 }
486
487 myflags = client->exp->nbdflags;
488 if (client->structured_reply) {
489 myflags |= NBD_FLAG_SEND_DF;
490 }
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;
499 }
500
501 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
502 nbd_export_get(client->exp);
503 nbd_check_meta_export(client);
504
505 return 0;
506 }
507
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)
514 {
515 int rc;
516
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;
522 }
523 info = cpu_to_be16(info);
524 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
525 return -EIO;
526 }
527 if (nbd_write(client->ioc, buf, length, errp) < 0) {
528 return -EIO;
529 }
530 return 0;
531 }
532
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
539 */
540 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
541 {
542 int ret;
543
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;
551 }
552 return ret;
553 }
554
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)
559 {
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;
572
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
578 */
579 rc = nbd_opt_read_name(client, &name, &namelen, errp);
580 if (rc <= 0) {
581 return rc;
582 }
583 trace_nbd_negotiate_handle_export_name_request(name);
584
585 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
586 if (rc <= 0) {
587 return rc;
588 }
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;
595 }
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;
609 }
610 }
611 if (client->optlen) {
612 return nbd_reject_length(client, false, errp);
613 }
614
615 exp = nbd_export_find(name);
616 if (!exp) {
617 g_autofree char *sane_name = nbd_sanitize_name(name);
618
619 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
620 errp, "export '%s' not present",
621 sane_name);
622 }
623
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;
630 }
631 }
632
633 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
634 * client request */
635 if (exp->description) {
636 size_t len = strlen(exp->description);
637
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;
643 }
644 }
645
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;
654 }
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;
669 }
670
671 /* Send NBD_INFO_EXPORT always */
672 myflags = exp->nbdflags;
673 if (client->structured_reply) {
674 myflags |= NBD_FLAG_SEND_DF;
675 }
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;
683 }
684
685 /*
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.
690 */
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");
698 }
699
700 /* Final reply */
701 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
702 if (rc < 0) {
703 return rc;
704 }
705
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;
713 }
714 return rc;
715 }
716
717
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)
722 {
723 QIOChannel *ioc;
724 QIOChannelTLS *tioc;
725 struct NBDTLSHandshakeData data = { 0 };
726
727 assert(client->opt == NBD_OPT_STARTTLS);
728
729 trace_nbd_negotiate_handle_starttls();
730 ioc = client->ioc;
731
732 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
733 return NULL;
734 }
735
736 tioc = qio_channel_tls_new_server(ioc,
737 client->tlscreds,
738 client->tlsauthz,
739 errp);
740 if (!tioc) {
741 return NULL;
742 }
743
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);
752
753 if (!data.complete) {
754 g_main_loop_run(data.loop);
755 }
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;
761 }
762
763 return QIO_CHANNEL(tioc);
764 }
765
766 /* nbd_negotiate_send_meta_context
767 *
768 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
769 *
770 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
771 */
772 static int nbd_negotiate_send_meta_context(NBDClient *client,
773 const char *context,
774 uint32_t context_id,
775 Error **errp)
776 {
777 NBDOptionReplyMetaContext opt;
778 struct iovec iov[] = {
779 {.iov_base = &opt, .iov_len = sizeof(opt)},
780 {.iov_base = (void *)context, .iov_len = strlen(context)}
781 };
782
783 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
784 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
785 context_id = 0;
786 }
787
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);
792
793 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
794 }
795
796 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
797 * @match is never set to false.
798 *
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.
801 *
802 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
803 * It only means that there are no errors.
804 */
805 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
806 Error **errp)
807 {
808 int ret;
809 char *query;
810 size_t len = strlen(pattern);
811
812 assert(len);
813
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;
819 }
820
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");
826 }
827 g_free(query);
828
829 return 1;
830 }
831
832 /*
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.
835 *
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.
838 *
839 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
840 * It only means that there are no errors.
841 */
842 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
843 uint32_t len, bool *match, Error **errp)
844 {
845 if (len == 0) {
846 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
847 *match = true;
848 }
849 trace_nbd_negotiate_meta_query_parse("empty");
850 return 1;
851 }
852
853 if (len != strlen(pattern)) {
854 trace_nbd_negotiate_meta_query_skip("different lengths");
855 return nbd_opt_skip(client, len, errp);
856 }
857
858 return nbd_meta_pattern(client, pattern, match, errp);
859 }
860
861 /* nbd_meta_base_query
862 *
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.
866 *
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.
869 */
870 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
871 uint32_t len, Error **errp)
872 {
873 return nbd_meta_empty_or_pattern(client, "allocation", len,
874 &meta->base_allocation, errp);
875 }
876
877 /* nbd_meta_bitmap_query
878 *
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.
882 *
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)
887 {
888 bool dirty_bitmap = false;
889 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
890 int ret;
891
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);
895 }
896
897 if (len == 0) {
898 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
899 meta->bitmap = true;
900 }
901 trace_nbd_negotiate_meta_query_parse("empty");
902 return 1;
903 }
904
905 if (len < dirty_bitmap_len) {
906 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
907 return nbd_opt_skip(client, len, errp);
908 }
909
910 len -= dirty_bitmap_len;
911 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
912 if (ret <= 0) {
913 return ret;
914 }
915 if (!dirty_bitmap) {
916 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
917 return nbd_opt_skip(client, len, errp);
918 }
919
920 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
921
922 return nbd_meta_empty_or_pattern(
923 client, meta->exp->export_bitmap_context +
924 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
925 }
926
927 /* nbd_negotiate_meta_query
928 *
929 * Parse namespace name and call corresponding function to parse body of the
930 * query.
931 *
932 * The only supported namespaces are 'base' and 'qemu'.
933 *
934 * The function aims not wasting time and memory to read long unknown namespace
935 * names.
936 *
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)
941 {
942 /*
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.
946 */
947 int ret;
948 size_t ns_len = 5;
949 char ns[5];
950 uint32_t len;
951
952 ret = nbd_opt_read(client, &len, sizeof(len), errp);
953 if (ret <= 0) {
954 return ret;
955 }
956 len = cpu_to_be32(len);
957
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);
961 }
962 if (len < ns_len) {
963 trace_nbd_negotiate_meta_query_skip("length too short");
964 return nbd_opt_skip(client, len, errp);
965 }
966
967 len -= ns_len;
968 ret = nbd_opt_read(client, ns, ns_len, errp);
969 if (ret <= 0) {
970 return ret;
971 }
972
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);
979 }
980
981 trace_nbd_negotiate_meta_query_skip("unknown namespace");
982 return nbd_opt_skip(client, len, errp);
983 }
984
985 /* nbd_negotiate_meta_queries
986 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
987 *
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)
991 {
992 int ret;
993 g_autofree char *export_name = NULL;
994 NBDExportMetaContexts local_meta;
995 uint32_t nb_queries;
996 int i;
997
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));
1003 }
1004
1005 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1006 /* Only change the caller's meta on SET. */
1007 meta = &local_meta;
1008 }
1009
1010 memset(meta, 0, sizeof(*meta));
1011
1012 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1013 if (ret <= 0) {
1014 return ret;
1015 }
1016
1017 meta->exp = nbd_export_find(export_name);
1018 if (meta->exp == NULL) {
1019 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1020
1021 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1022 "export '%s' not present", sane_name);
1023 }
1024
1025 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
1026 if (ret <= 0) {
1027 return ret;
1028 }
1029 nb_queries = cpu_to_be32(nb_queries);
1030 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1031 export_name, nb_queries);
1032
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;
1042 }
1043 }
1044 }
1045
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;
1052 }
1053 }
1054
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;
1062 }
1063 }
1064
1065 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1066 if (ret == 0) {
1067 meta->valid = true;
1068 }
1069
1070 return ret;
1071 }
1072
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
1081 */
1082 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1083 {
1084 uint32_t flags;
1085 bool fixedNewstyle = false;
1086 bool no_zeroes = false;
1087
1088 /* Client sends:
1089 [ 0 .. 3] client flags
1090
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
1096
1097 [ 0 .. 7] NBD_OPTS_MAGIC
1098 [ 8 .. 11] Second NBD option
1099 [12 .. 15] Data length
1100 ... Rest of request
1101 */
1102
1103 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1104 return -EIO;
1105 }
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;
1110 }
1111 if (flags & NBD_FLAG_C_NO_ZEROES) {
1112 no_zeroes = true;
1113 flags &= ~NBD_FLAG_C_NO_ZEROES;
1114 }
1115 if (flags != 0) {
1116 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1117 return -EINVAL;
1118 }
1119
1120 while (1) {
1121 int ret;
1122 uint32_t option, length;
1123 uint64_t magic;
1124
1125 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1126 return -EINVAL;
1127 }
1128 trace_nbd_negotiate_options_check_magic(magic);
1129 if (magic != NBD_OPTS_MAGIC) {
1130 error_setg(errp, "Bad magic received");
1131 return -EINVAL;
1132 }
1133
1134 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1135 return -EINVAL;
1136 }
1137 client->opt = option;
1138
1139 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1140 return -EINVAL;
1141 }
1142 assert(!client->optlen);
1143 client->optlen = length;
1144
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;
1149 }
1150
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;
1159 }
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);
1166 }
1167 tioc = nbd_negotiate_handle_starttls(client, errp);
1168 if (!tioc) {
1169 return -EIO;
1170 }
1171 ret = 0;
1172 object_unref(OBJECT(client->ioc));
1173 client->ioc = QIO_CHANNEL(tioc);
1174 break;
1175
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;
1181
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;
1195 }
1196 break;
1197 }
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);
1205 }
1206 break;
1207
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;
1214
1215 case NBD_OPT_EXPORT_NAME:
1216 return nbd_negotiate_handle_export_name(client, no_zeroes,
1217 errp);
1218
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;
1225 }
1226 break;
1227
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");
1239 }
1240 break;
1241
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;
1252 }
1253 break;
1254
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;
1260
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;
1266 }
1267 } else {
1268 /*
1269 * If broken new-style we should drop the connection
1270 * for anything except NBD_OPT_EXPORT_NAME
1271 */
1272 switch (option) {
1273 case NBD_OPT_EXPORT_NAME:
1274 return nbd_negotiate_handle_export_name(client, no_zeroes,
1275 errp);
1276
1277 default:
1278 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1279 option, nbd_opt_lookup(option));
1280 return -EINVAL;
1281 }
1282 }
1283 if (ret < 0) {
1284 return ret;
1285 }
1286 }
1287 }
1288
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
1295 */
1296 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1297 {
1298 ERRP_GUARD();
1299 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1300 int ret;
1301
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)
1308
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....
1314 */
1315
1316 qio_channel_set_blocking(client->ioc, false, NULL);
1317
1318 trace_nbd_negotiate_begin();
1319 memcpy(buf, "NBDMAGIC", 8);
1320
1321 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1322 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1323
1324 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1325 error_prepend(errp, "write failed: ");
1326 return -EINVAL;
1327 }
1328 ret = nbd_negotiate_options(client, errp);
1329 if (ret != 0) {
1330 if (ret < 0) {
1331 error_prepend(errp, "option negotiation failed: ");
1332 }
1333 return ret;
1334 }
1335
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);
1339 }
1340
1341 assert(!client->optlen);
1342 trace_nbd_negotiate_success();
1343
1344 return 0;
1345 }
1346
1347 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1348 Error **errp)
1349 {
1350 uint8_t buf[NBD_REQUEST_SIZE];
1351 uint32_t magic;
1352 int ret;
1353
1354 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1355 if (ret < 0) {
1356 return ret;
1357 }
1358
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
1366 */
1367
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);
1374
1375 trace_nbd_receive_request(magic, request->flags, request->type,
1376 request->from, request->len);
1377
1378 if (magic != NBD_REQUEST_MAGIC) {
1379 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1380 return -EINVAL;
1381 }
1382 return 0;
1383 }
1384
1385 #define MAX_NBD_REQUESTS 16
1386
1387 void nbd_client_get(NBDClient *client)
1388 {
1389 client->refcount++;
1390 }
1391
1392 void nbd_client_put(NBDClient *client)
1393 {
1394 if (--client->refcount == 0) {
1395 /* The last reference should be dropped by client->close,
1396 * which is called by client_close.
1397 */
1398 assert(client->closing);
1399
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));
1405 }
1406 g_free(client->tlsauthz);
1407 if (client->exp) {
1408 QTAILQ_REMOVE(&client->exp->clients, client, next);
1409 nbd_export_put(client->exp);
1410 }
1411 g_free(client);
1412 }
1413 }
1414
1415 static void client_close(NBDClient *client, bool negotiated)
1416 {
1417 if (client->closing) {
1418 return;
1419 }
1420
1421 client->closing = true;
1422
1423 /* Force requests to finish. They will drop their own references,
1424 * then we'll close the socket and free the NBDClient.
1425 */
1426 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1427 NULL);
1428
1429 /* Also tell the client, so that they release their reference. */
1430 if (client->close_fn) {
1431 client->close_fn(client, negotiated);
1432 }
1433 }
1434
1435 static NBDRequestData *nbd_request_get(NBDClient *client)
1436 {
1437 NBDRequestData *req;
1438
1439 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1440 client->nb_requests++;
1441
1442 req = g_new0(NBDRequestData, 1);
1443 nbd_client_get(client);
1444 req->client = client;
1445 return req;
1446 }
1447
1448 static void nbd_request_put(NBDRequestData *req)
1449 {
1450 NBDClient *client = req->client;
1451
1452 if (req->data) {
1453 qemu_vfree(req->data);
1454 }
1455 g_free(req);
1456
1457 client->nb_requests--;
1458 nbd_client_receive_next_request(client);
1459
1460 nbd_client_put(client);
1461 }
1462
1463 static void blk_aio_attached(AioContext *ctx, void *opaque)
1464 {
1465 NBDExport *exp = opaque;
1466 NBDClient *client;
1467
1468 trace_nbd_blk_aio_attached(exp->name, ctx);
1469
1470 exp->ctx = ctx;
1471
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);
1476 }
1477 if (client->send_coroutine) {
1478 aio_co_schedule(ctx, client->send_coroutine);
1479 }
1480 }
1481 }
1482
1483 static void blk_aio_detach(void *opaque)
1484 {
1485 NBDExport *exp = opaque;
1486 NBDClient *client;
1487
1488 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1489
1490 QTAILQ_FOREACH(client, &exp->clients, next) {
1491 qio_channel_detach_aio_context(client->ioc);
1492 }
1493
1494 exp->ctx = NULL;
1495 }
1496
1497 static void nbd_eject_notifier(Notifier *n, void *data)
1498 {
1499 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1500 AioContext *aio_context;
1501
1502 aio_context = exp->ctx;
1503 aio_context_acquire(aio_context);
1504 nbd_export_close(exp);
1505 aio_context_release(aio_context);
1506 }
1507
1508 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1509 {
1510 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1511 assert(exp->drv == &blk_exp_nbd);
1512 assert(nbd_exp->eject_notifier_blk == NULL);
1513
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);
1518 }
1519
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)
1524 {
1525 AioContext *ctx;
1526 BlockBackend *blk;
1527 NBDExport *exp;
1528 int64_t size;
1529 uint64_t perm;
1530 int ret;
1531
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;
1537 }
1538
1539 exp = g_new0(NBDExport, 1);
1540 exp->common = (BlockExport) {
1541 .drv = &blk_exp_nbd,
1542 };
1543
1544 /*
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.
1549 */
1550 assert(name && strlen(name) <= NBD_MAX_STRING_SIZE);
1551 ctx = bdrv_get_aio_context(bs);
1552 bdrv_invalidate_cache(bs, NULL);
1553
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;
1559 }
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;
1566 }
1567 blk_set_enable_write_cache(blk, !writethrough);
1568 blk_set_allow_aio_context_change(blk, true);
1569
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;
1582 }
1583 } else {
1584 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1585 NBD_FLAG_SEND_FAST_ZERO);
1586 }
1587 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1588
1589 if (bitmap) {
1590 BdrvDirtyBitmap *bm = NULL;
1591
1592 while (bs) {
1593 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1594 if (bm != NULL) {
1595 break;
1596 }
1597
1598 bs = bdrv_filter_or_cow_bs(bs);
1599 }
1600
1601 if (bm == NULL) {
1602 error_setg(errp, "Bitmap '%s' is not found", bitmap);
1603 goto fail;
1604 }
1605
1606 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1607 goto fail;
1608 }
1609
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;
1616 }
1617
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);
1624 }
1625
1626 exp->ctx = ctx;
1627 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1628
1629 QTAILQ_INSERT_TAIL(&exports, exp, next);
1630 nbd_export_get(exp);
1631 return exp;
1632
1633 fail:
1634 blk_unref(blk);
1635 g_free(exp->name);
1636 g_free(exp->description);
1637 g_free(exp);
1638 return NULL;
1639 }
1640
1641 NBDExport *nbd_export_find(const char *name)
1642 {
1643 NBDExport *exp;
1644 QTAILQ_FOREACH(exp, &exports, next) {
1645 if (strcmp(name, exp->name) == 0) {
1646 return exp;
1647 }
1648 }
1649
1650 return NULL;
1651 }
1652
1653 AioContext *
1654 nbd_export_aio_context(NBDExport *exp)
1655 {
1656 return exp->ctx;
1657 }
1658
1659 void nbd_export_close(NBDExport *exp)
1660 {
1661 NBDClient *client, *next;
1662
1663 nbd_export_get(exp);
1664 /*
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.
1670 */
1671 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1672 client_close(client, true);
1673 }
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);
1680 }
1681 g_free(exp->description);
1682 exp->description = NULL;
1683 nbd_export_put(exp);
1684 }
1685
1686 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1687 {
1688 ERRP_GUARD();
1689 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1690 nbd_export_close(exp);
1691 return;
1692 }
1693
1694 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1695
1696 error_setg(errp, "export '%s' still in use", exp->name);
1697 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1698 }
1699
1700 void nbd_export_get(NBDExport *exp)
1701 {
1702 assert(exp->refcount > 0);
1703 exp->refcount++;
1704 }
1705
1706 void nbd_export_put(NBDExport *exp)
1707 {
1708 assert(exp->refcount > 0);
1709 if (exp->refcount == 1) {
1710 nbd_export_close(exp);
1711 }
1712
1713 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1714 * if someone calls nbd_export_put() on named export not through
1715 * nbd_export_set_name() when refcount is 1. So, let's assert that
1716 * it is > 0.
1717 */
1718 assert(exp->refcount > 0);
1719 if (--exp->refcount == 0) {
1720 assert(exp->name == NULL);
1721 assert(exp->description == NULL);
1722
1723 if (exp->blk) {
1724 if (exp->eject_notifier_blk) {
1725 notifier_remove(&exp->eject_notifier);
1726 blk_unref(exp->eject_notifier_blk);
1727 }
1728 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1729 blk_aio_detach, exp);
1730 blk_unref(exp->blk);
1731 exp->blk = NULL;
1732 }
1733
1734 if (exp->export_bitmap) {
1735 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1736 g_free(exp->export_bitmap_context);
1737 }
1738
1739 QTAILQ_REMOVE(&closed_exports, exp, next);
1740 g_free(exp);
1741 aio_wait_kick();
1742 }
1743 }
1744
1745 const BlockExportDriver blk_exp_nbd = {
1746 .type = BLOCK_EXPORT_TYPE_NBD,
1747 .create = nbd_export_create,
1748 };
1749
1750 void nbd_export_close_all(void)
1751 {
1752 NBDExport *exp, *next;
1753 AioContext *aio_context;
1754
1755 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1756 aio_context = exp->ctx;
1757 aio_context_acquire(aio_context);
1758 nbd_export_close(exp);
1759 aio_context_release(aio_context);
1760 }
1761
1762 AIO_WAIT_WHILE(NULL, !(QTAILQ_EMPTY(&exports) &&
1763 QTAILQ_EMPTY(&closed_exports)));
1764 }
1765
1766 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1767 unsigned niov, Error **errp)
1768 {
1769 int ret;
1770
1771 g_assert(qemu_in_coroutine());
1772 qemu_co_mutex_lock(&client->send_lock);
1773 client->send_coroutine = qemu_coroutine_self();
1774
1775 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1776
1777 client->send_coroutine = NULL;
1778 qemu_co_mutex_unlock(&client->send_lock);
1779
1780 return ret;
1781 }
1782
1783 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1784 uint64_t handle)
1785 {
1786 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1787 stl_be_p(&reply->error, error);
1788 stq_be_p(&reply->handle, handle);
1789 }
1790
1791 static int nbd_co_send_simple_reply(NBDClient *client,
1792 uint64_t handle,
1793 uint32_t error,
1794 void *data,
1795 size_t len,
1796 Error **errp)
1797 {
1798 NBDSimpleReply reply;
1799 int nbd_err = system_errno_to_nbd_errno(error);
1800 struct iovec iov[] = {
1801 {.iov_base = &reply, .iov_len = sizeof(reply)},
1802 {.iov_base = data, .iov_len = len}
1803 };
1804
1805 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1806 len);
1807 set_be_simple_reply(&reply, nbd_err, handle);
1808
1809 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1810 }
1811
1812 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1813 uint16_t type, uint64_t handle, uint32_t length)
1814 {
1815 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1816 stw_be_p(&chunk->flags, flags);
1817 stw_be_p(&chunk->type, type);
1818 stq_be_p(&chunk->handle, handle);
1819 stl_be_p(&chunk->length, length);
1820 }
1821
1822 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1823 uint64_t handle,
1824 Error **errp)
1825 {
1826 NBDStructuredReplyChunk chunk;
1827 struct iovec iov[] = {
1828 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1829 };
1830
1831 trace_nbd_co_send_structured_done(handle);
1832 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1833
1834 return nbd_co_send_iov(client, iov, 1, errp);
1835 }
1836
1837 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1838 uint64_t handle,
1839 uint64_t offset,
1840 void *data,
1841 size_t size,
1842 bool final,
1843 Error **errp)
1844 {
1845 NBDStructuredReadData chunk;
1846 struct iovec iov[] = {
1847 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1848 {.iov_base = data, .iov_len = size}
1849 };
1850
1851 assert(size);
1852 trace_nbd_co_send_structured_read(handle, offset, data, size);
1853 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1854 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1855 sizeof(chunk) - sizeof(chunk.h) + size);
1856 stq_be_p(&chunk.offset, offset);
1857
1858 return nbd_co_send_iov(client, iov, 2, errp);
1859 }
1860
1861 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1862 uint64_t handle,
1863 uint32_t error,
1864 const char *msg,
1865 Error **errp)
1866 {
1867 NBDStructuredError chunk;
1868 int nbd_err = system_errno_to_nbd_errno(error);
1869 struct iovec iov[] = {
1870 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1871 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1872 };
1873
1874 assert(nbd_err);
1875 trace_nbd_co_send_structured_error(handle, nbd_err,
1876 nbd_err_lookup(nbd_err), msg ? msg : "");
1877 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1878 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1879 stl_be_p(&chunk.error, nbd_err);
1880 stw_be_p(&chunk.message_length, iov[1].iov_len);
1881
1882 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1883 }
1884
1885 /* Do a sparse read and send the structured reply to the client.
1886 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1887 * reported to the client, at which point this function succeeds.
1888 */
1889 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1890 uint64_t handle,
1891 uint64_t offset,
1892 uint8_t *data,
1893 size_t size,
1894 Error **errp)
1895 {
1896 int ret = 0;
1897 NBDExport *exp = client->exp;
1898 size_t progress = 0;
1899
1900 while (progress < size) {
1901 int64_t pnum;
1902 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1903 offset + progress,
1904 size - progress, &pnum, NULL,
1905 NULL);
1906 bool final;
1907
1908 if (status < 0) {
1909 char *msg = g_strdup_printf("unable to check for holes: %s",
1910 strerror(-status));
1911
1912 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1913 errp);
1914 g_free(msg);
1915 return ret;
1916 }
1917 assert(pnum && pnum <= size - progress);
1918 final = progress + pnum == size;
1919 if (status & BDRV_BLOCK_ZERO) {
1920 NBDStructuredReadHole chunk;
1921 struct iovec iov[] = {
1922 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1923 };
1924
1925 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1926 pnum);
1927 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1928 NBD_REPLY_TYPE_OFFSET_HOLE,
1929 handle, sizeof(chunk) - sizeof(chunk.h));
1930 stq_be_p(&chunk.offset, offset + progress);
1931 stl_be_p(&chunk.length, pnum);
1932 ret = nbd_co_send_iov(client, iov, 1, errp);
1933 } else {
1934 ret = blk_pread(exp->blk, offset + progress, data + progress, pnum);
1935 if (ret < 0) {
1936 error_setg_errno(errp, -ret, "reading from file failed");
1937 break;
1938 }
1939 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1940 data + progress, pnum, final,
1941 errp);
1942 }
1943
1944 if (ret < 0) {
1945 break;
1946 }
1947 progress += pnum;
1948 }
1949 return ret;
1950 }
1951
1952 typedef struct NBDExtentArray {
1953 NBDExtent *extents;
1954 unsigned int nb_alloc;
1955 unsigned int count;
1956 uint64_t total_length;
1957 bool can_add;
1958 bool converted_to_be;
1959 } NBDExtentArray;
1960
1961 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
1962 {
1963 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
1964
1965 ea->nb_alloc = nb_alloc;
1966 ea->extents = g_new(NBDExtent, nb_alloc);
1967 ea->can_add = true;
1968
1969 return ea;
1970 }
1971
1972 static void nbd_extent_array_free(NBDExtentArray *ea)
1973 {
1974 g_free(ea->extents);
1975 g_free(ea);
1976 }
1977 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
1978
1979 /* Further modifications of the array after conversion are abandoned */
1980 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
1981 {
1982 int i;
1983
1984 assert(!ea->converted_to_be);
1985 ea->can_add = false;
1986 ea->converted_to_be = true;
1987
1988 for (i = 0; i < ea->count; i++) {
1989 ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
1990 ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
1991 }
1992 }
1993
1994 /*
1995 * Add extent to NBDExtentArray. If extent can't be added (no available space),
1996 * return -1.
1997 * For safety, when returning -1 for the first time, .can_add is set to false,
1998 * further call to nbd_extent_array_add() will crash.
1999 * (to avoid the situation, when after failing to add an extent (returned -1),
2000 * user miss this failure and add another extent, which is successfully added
2001 * (array is full, but new extent may be squashed into the last one), then we
2002 * have invalid array with skipped extent)
2003 */
2004 static int nbd_extent_array_add(NBDExtentArray *ea,
2005 uint32_t length, uint32_t flags)
2006 {
2007 assert(ea->can_add);
2008
2009 if (!length) {
2010 return 0;
2011 }
2012
2013 /* Extend previous extent if flags are the same */
2014 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
2015 uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
2016
2017 if (sum <= UINT32_MAX) {
2018 ea->extents[ea->count - 1].length = sum;
2019 ea->total_length += length;
2020 return 0;
2021 }
2022 }
2023
2024 if (ea->count >= ea->nb_alloc) {
2025 ea->can_add = false;
2026 return -1;
2027 }
2028
2029 ea->total_length += length;
2030 ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
2031 ea->count++;
2032
2033 return 0;
2034 }
2035
2036 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
2037 uint64_t bytes, NBDExtentArray *ea)
2038 {
2039 while (bytes) {
2040 uint32_t flags;
2041 int64_t num;
2042 int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
2043 NULL, NULL);
2044
2045 if (ret < 0) {
2046 return ret;
2047 }
2048
2049 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
2050 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
2051
2052 if (nbd_extent_array_add(ea, num, flags) < 0) {
2053 return 0;
2054 }
2055
2056 offset += num;
2057 bytes -= num;
2058 }
2059
2060 return 0;
2061 }
2062
2063 /*
2064 * nbd_co_send_extents
2065 *
2066 * @ea is converted to BE by the function
2067 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2068 */
2069 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
2070 NBDExtentArray *ea,
2071 bool last, uint32_t context_id, Error **errp)
2072 {
2073 NBDStructuredMeta chunk;
2074 struct iovec iov[] = {
2075 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2076 {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
2077 };
2078
2079 nbd_extent_array_convert_to_be(ea);
2080
2081 trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2082 last);
2083 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2084 NBD_REPLY_TYPE_BLOCK_STATUS,
2085 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2086 stl_be_p(&chunk.context_id, context_id);
2087
2088 return nbd_co_send_iov(client, iov, 2, errp);
2089 }
2090
2091 /* Get block status from the exported device and send it to the client */
2092 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2093 BlockDriverState *bs, uint64_t offset,
2094 uint32_t length, bool dont_fragment,
2095 bool last, uint32_t context_id,
2096 Error **errp)
2097 {
2098 int ret;
2099 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2100 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2101
2102 ret = blockstatus_to_extents(bs, offset, length, ea);
2103 if (ret < 0) {
2104 return nbd_co_send_structured_error(
2105 client, handle, -ret, "can't get block status", errp);
2106 }
2107
2108 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2109 }
2110
2111 /* Populate @ea from a dirty bitmap. */
2112 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2113 uint64_t offset, uint64_t length,
2114 NBDExtentArray *es)
2115 {
2116 int64_t start, dirty_start, dirty_count;
2117 int64_t end = offset + length;
2118 bool full = false;
2119
2120 bdrv_dirty_bitmap_lock(bitmap);
2121
2122 for (start = offset;
2123 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2124 &dirty_start, &dirty_count);
2125 start = dirty_start + dirty_count)
2126 {
2127 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2128 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2129 {
2130 full = true;
2131 break;
2132 }
2133 }
2134
2135 if (!full) {
2136 /* last non dirty extent */
2137 nbd_extent_array_add(es, end - start, 0);
2138 }
2139
2140 bdrv_dirty_bitmap_unlock(bitmap);
2141 }
2142
2143 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2144 BdrvDirtyBitmap *bitmap, uint64_t offset,
2145 uint32_t length, bool dont_fragment, bool last,
2146 uint32_t context_id, Error **errp)
2147 {
2148 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2149 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2150
2151 bitmap_to_extents(bitmap, offset, length, ea);
2152
2153 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2154 }
2155
2156 /* nbd_co_receive_request
2157 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2158 * connection right away, and any other negative value to report an error to
2159 * the client (although the caller may still need to disconnect after reporting
2160 * the error).
2161 */
2162 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2163 Error **errp)
2164 {
2165 NBDClient *client = req->client;
2166 int valid_flags;
2167
2168 g_assert(qemu_in_coroutine());
2169 assert(client->recv_coroutine == qemu_coroutine_self());
2170 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2171 return -EIO;
2172 }
2173
2174 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2175 nbd_cmd_lookup(request->type));
2176
2177 if (request->type != NBD_CMD_WRITE) {
2178 /* No payload, we are ready to read the next request. */
2179 req->complete = true;
2180 }
2181
2182 if (request->type == NBD_CMD_DISC) {
2183 /* Special case: we're going to disconnect without a reply,
2184 * whether or not flags, from, or len are bogus */
2185 return -EIO;
2186 }
2187
2188 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2189 request->type == NBD_CMD_CACHE)
2190 {
2191 if (request->len > NBD_MAX_BUFFER_SIZE) {
2192 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2193 request->len, NBD_MAX_BUFFER_SIZE);
2194 return -EINVAL;
2195 }
2196
2197 if (request->type != NBD_CMD_CACHE) {
2198 req->data = blk_try_blockalign(client->exp->blk, request->len);
2199 if (req->data == NULL) {
2200 error_setg(errp, "No memory");
2201 return -ENOMEM;
2202 }
2203 }
2204 }
2205
2206 if (request->type == NBD_CMD_WRITE) {
2207 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2208 errp) < 0)
2209 {
2210 return -EIO;
2211 }
2212 req->complete = true;
2213
2214 trace_nbd_co_receive_request_payload_received(request->handle,
2215 request->len);
2216 }
2217
2218 /* Sanity checks. */
2219 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2220 (request->type == NBD_CMD_WRITE ||
2221 request->type == NBD_CMD_WRITE_ZEROES ||
2222 request->type == NBD_CMD_TRIM)) {
2223 error_setg(errp, "Export is read-only");
2224 return -EROFS;
2225 }
2226 if (request->from > client->exp->size ||
2227 request->len > client->exp->size - request->from) {
2228 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2229 ", Size: %" PRIu64, request->from, request->len,
2230 client->exp->size);
2231 return (request->type == NBD_CMD_WRITE ||
2232 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2233 }
2234 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2235 client->check_align)) {
2236 /*
2237 * The block layer gracefully handles unaligned requests, but
2238 * it's still worth tracing client non-compliance
2239 */
2240 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2241 request->from,
2242 request->len,
2243 client->check_align);
2244 }
2245 valid_flags = NBD_CMD_FLAG_FUA;
2246 if (request->type == NBD_CMD_READ && client->structured_reply) {
2247 valid_flags |= NBD_CMD_FLAG_DF;
2248 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2249 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2250 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2251 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2252 }
2253 if (request->flags & ~valid_flags) {
2254 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2255 nbd_cmd_lookup(request->type), request->flags);
2256 return -EINVAL;
2257 }
2258
2259 return 0;
2260 }
2261
2262 /* Send simple reply without a payload, or a structured error
2263 * @error_msg is ignored if @ret >= 0
2264 * Returns 0 if connection is still live, -errno on failure to talk to client
2265 */
2266 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2267 uint64_t handle,
2268 int ret,
2269 const char *error_msg,
2270 Error **errp)
2271 {
2272 if (client->structured_reply && ret < 0) {
2273 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2274 errp);
2275 } else {
2276 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2277 NULL, 0, errp);
2278 }
2279 }
2280
2281 /* Handle NBD_CMD_READ request.
2282 * Return -errno if sending fails. Other errors are reported directly to the
2283 * client as an error reply. */
2284 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2285 uint8_t *data, Error **errp)
2286 {
2287 int ret;
2288 NBDExport *exp = client->exp;
2289
2290 assert(request->type == NBD_CMD_READ);
2291
2292 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2293 if (request->flags & NBD_CMD_FLAG_FUA) {
2294 ret = blk_co_flush(exp->blk);
2295 if (ret < 0) {
2296 return nbd_send_generic_reply(client, request->handle, ret,
2297 "flush failed", errp);
2298 }
2299 }
2300
2301 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2302 request->len)
2303 {
2304 return nbd_co_send_sparse_read(client, request->handle, request->from,
2305 data, request->len, errp);
2306 }
2307
2308 ret = blk_pread(exp->blk, request->from, data, request->len);
2309 if (ret < 0) {
2310 return nbd_send_generic_reply(client, request->handle, ret,
2311 "reading from file failed", errp);
2312 }
2313
2314 if (client->structured_reply) {
2315 if (request->len) {
2316 return nbd_co_send_structured_read(client, request->handle,
2317 request->from, data,
2318 request->len, true, errp);
2319 } else {
2320 return nbd_co_send_structured_done(client, request->handle, errp);
2321 }
2322 } else {
2323 return nbd_co_send_simple_reply(client, request->handle, 0,
2324 data, request->len, errp);
2325 }
2326 }
2327
2328 /*
2329 * nbd_do_cmd_cache
2330 *
2331 * Handle NBD_CMD_CACHE request.
2332 * Return -errno if sending fails. Other errors are reported directly to the
2333 * client as an error reply.
2334 */
2335 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2336 Error **errp)
2337 {
2338 int ret;
2339 NBDExport *exp = client->exp;
2340
2341 assert(request->type == NBD_CMD_CACHE);
2342
2343 ret = blk_co_preadv(exp->blk, request->from, request->len,
2344 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2345
2346 return nbd_send_generic_reply(client, request->handle, ret,
2347 "caching data failed", errp);
2348 }
2349
2350 /* Handle NBD request.
2351 * Return -errno if sending fails. Other errors are reported directly to the
2352 * client as an error reply. */
2353 static coroutine_fn int nbd_handle_request(NBDClient *client,
2354 NBDRequest *request,
2355 uint8_t *data, Error **errp)
2356 {
2357 int ret;
2358 int flags;
2359 NBDExport *exp = client->exp;
2360 char *msg;
2361
2362 switch (request->type) {
2363 case NBD_CMD_CACHE:
2364 return nbd_do_cmd_cache(client, request, errp);
2365
2366 case NBD_CMD_READ:
2367 return nbd_do_cmd_read(client, request, data, errp);
2368
2369 case NBD_CMD_WRITE:
2370 flags = 0;
2371 if (request->flags & NBD_CMD_FLAG_FUA) {
2372 flags |= BDRV_REQ_FUA;
2373 }
2374 ret = blk_pwrite(exp->blk, request->from, data, request->len, flags);
2375 return nbd_send_generic_reply(client, request->handle, ret,
2376 "writing to file failed", errp);
2377
2378 case NBD_CMD_WRITE_ZEROES:
2379 flags = 0;
2380 if (request->flags & NBD_CMD_FLAG_FUA) {
2381 flags |= BDRV_REQ_FUA;
2382 }
2383 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2384 flags |= BDRV_REQ_MAY_UNMAP;
2385 }
2386 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2387 flags |= BDRV_REQ_NO_FALLBACK;
2388 }
2389 ret = 0;
2390 /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2391 while (ret >= 0 && request->len) {
2392 int align = client->check_align ?: 1;
2393 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2394 align));
2395 ret = blk_pwrite_zeroes(exp->blk, request->from, len, flags);
2396 request->len -= len;
2397 request->from += len;
2398 }
2399 return nbd_send_generic_reply(client, request->handle, ret,
2400 "writing to file failed", errp);
2401
2402 case NBD_CMD_DISC:
2403 /* unreachable, thanks to special case in nbd_co_receive_request() */
2404 abort();
2405
2406 case NBD_CMD_FLUSH:
2407 ret = blk_co_flush(exp->blk);
2408 return nbd_send_generic_reply(client, request->handle, ret,
2409 "flush failed", errp);
2410
2411 case NBD_CMD_TRIM:
2412 ret = 0;
2413 /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2414 while (ret >= 0 && request->len) {
2415 int align = client->check_align ?: 1;
2416 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2417 align));
2418 ret = blk_co_pdiscard(exp->blk, request->from, len);
2419 request->len -= len;
2420 request->from += len;
2421 }
2422 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2423 ret = blk_co_flush(exp->blk);
2424 }
2425 return nbd_send_generic_reply(client, request->handle, ret,
2426 "discard failed", errp);
2427
2428 case NBD_CMD_BLOCK_STATUS:
2429 if (!request->len) {
2430 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2431 "need non-zero length", errp);
2432 }
2433 if (client->export_meta.valid &&
2434 (client->export_meta.base_allocation ||
2435 client->export_meta.bitmap))
2436 {
2437 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2438
2439 if (client->export_meta.base_allocation) {
2440 ret = nbd_co_send_block_status(client, request->handle,
2441 blk_bs(exp->blk), request->from,
2442 request->len, dont_fragment,
2443 !client->export_meta.bitmap,
2444 NBD_META_ID_BASE_ALLOCATION,
2445 errp);
2446 if (ret < 0) {
2447 return ret;
2448 }
2449 }
2450
2451 if (client->export_meta.bitmap) {
2452 ret = nbd_co_send_bitmap(client, request->handle,
2453 client->exp->export_bitmap,
2454 request->from, request->len,
2455 dont_fragment,
2456 true, NBD_META_ID_DIRTY_BITMAP, errp);
2457 if (ret < 0) {
2458 return ret;
2459 }
2460 }
2461
2462 return 0;
2463 } else {
2464 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2465 "CMD_BLOCK_STATUS not negotiated",
2466 errp);
2467 }
2468
2469 default:
2470 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2471 request->type);
2472 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2473 errp);
2474 g_free(msg);
2475 return ret;
2476 }
2477 }
2478
2479 /* Owns a reference to the NBDClient passed as opaque. */
2480 static coroutine_fn void nbd_trip(void *opaque)
2481 {
2482 NBDClient *client = opaque;
2483 NBDRequestData *req;
2484 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2485 int ret;
2486 Error *local_err = NULL;
2487
2488 trace_nbd_trip();
2489 if (client->closing) {
2490 nbd_client_put(client);
2491 return;
2492 }
2493
2494 req = nbd_request_get(client);
2495 ret = nbd_co_receive_request(req, &request, &local_err);
2496 client->recv_coroutine = NULL;
2497
2498 if (client->closing) {
2499 /*
2500 * The client may be closed when we are blocked in
2501 * nbd_co_receive_request()
2502 */
2503 goto done;
2504 }
2505
2506 nbd_client_receive_next_request(client);
2507 if (ret == -EIO) {
2508 goto disconnect;
2509 }
2510
2511 if (ret < 0) {
2512 /* It wans't -EIO, so, according to nbd_co_receive_request()
2513 * semantics, we should return the error to the client. */
2514 Error *export_err = local_err;
2515
2516 local_err = NULL;
2517 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2518 error_get_pretty(export_err), &local_err);
2519 error_free(export_err);
2520 } else {
2521 ret = nbd_handle_request(client, &request, req->data, &local_err);
2522 }
2523 if (ret < 0) {
2524 error_prepend(&local_err, "Failed to send reply: ");
2525 goto disconnect;
2526 }
2527
2528 /* We must disconnect after NBD_CMD_WRITE if we did not
2529 * read the payload.
2530 */
2531 if (!req->complete) {
2532 error_setg(&local_err, "Request handling failed in intermediate state");
2533 goto disconnect;
2534 }
2535
2536 done:
2537 nbd_request_put(req);
2538 nbd_client_put(client);
2539 return;
2540
2541 disconnect:
2542 if (local_err) {
2543 error_reportf_err(local_err, "Disconnect client, due to: ");
2544 }
2545 nbd_request_put(req);
2546 client_close(client, true);
2547 nbd_client_put(client);
2548 }
2549
2550 static void nbd_client_receive_next_request(NBDClient *client)
2551 {
2552 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2553 nbd_client_get(client);
2554 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2555 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
2556 }
2557 }
2558
2559 static coroutine_fn void nbd_co_client_start(void *opaque)
2560 {
2561 NBDClient *client = opaque;
2562 Error *local_err = NULL;
2563
2564 qemu_co_mutex_init(&client->send_lock);
2565
2566 if (nbd_negotiate(client, &local_err)) {
2567 if (local_err) {
2568 error_report_err(local_err);
2569 }
2570 client_close(client, false);
2571 return;
2572 }
2573
2574 nbd_client_receive_next_request(client);
2575 }
2576
2577 /*
2578 * Create a new client listener using the given channel @sioc.
2579 * Begin servicing it in a coroutine. When the connection closes, call
2580 * @close_fn with an indication of whether the client completed negotiation.
2581 */
2582 void nbd_client_new(QIOChannelSocket *sioc,
2583 QCryptoTLSCreds *tlscreds,
2584 const char *tlsauthz,
2585 void (*close_fn)(NBDClient *, bool))
2586 {
2587 NBDClient *client;
2588 Coroutine *co;
2589
2590 client = g_new0(NBDClient, 1);
2591 client->refcount = 1;
2592 client->tlscreds = tlscreds;
2593 if (tlscreds) {
2594 object_ref(OBJECT(client->tlscreds));
2595 }
2596 client->tlsauthz = g_strdup(tlsauthz);
2597 client->sioc = sioc;
2598 object_ref(OBJECT(client->sioc));
2599 client->ioc = QIO_CHANNEL(sioc);
2600 object_ref(OBJECT(client->ioc));
2601 client->close_fn = close_fn;
2602
2603 co = qemu_coroutine_create(nbd_co_client_start, client);
2604 qemu_coroutine_enter(co);
2605 }