block/export: Allocate BlockExport in blk_exp_add()
[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
87 BlockBackend *blk;
88 char *name;
89 char *description;
90 uint64_t size;
91 uint16_t nbdflags;
92 QTAILQ_HEAD(, NBDClient) clients;
93 QTAILQ_ENTRY(NBDExport) next;
94
95 BlockBackend *eject_notifier_blk;
96 Notifier eject_notifier;
97
98 BdrvDirtyBitmap *export_bitmap;
99 char *export_bitmap_context;
100 };
101
102 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
103 static QTAILQ_HEAD(, NBDExport) closed_exports =
104 QTAILQ_HEAD_INITIALIZER(closed_exports);
105
106 /* NBDExportMetaContexts represents a list of contexts to be exported,
107 * as selected by NBD_OPT_SET_META_CONTEXT. Also used for
108 * NBD_OPT_LIST_META_CONTEXT. */
109 typedef struct NBDExportMetaContexts {
110 NBDExport *exp;
111 bool valid; /* means that negotiation of the option finished without
112 errors */
113 bool base_allocation; /* export base:allocation context (block status) */
114 bool bitmap; /* export qemu:dirty-bitmap:<export bitmap name> */
115 } NBDExportMetaContexts;
116
117 struct NBDClient {
118 int refcount;
119 void (*close_fn)(NBDClient *client, bool negotiated);
120
121 NBDExport *exp;
122 QCryptoTLSCreds *tlscreds;
123 char *tlsauthz;
124 QIOChannelSocket *sioc; /* The underlying data channel */
125 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
126
127 Coroutine *recv_coroutine;
128
129 CoMutex send_lock;
130 Coroutine *send_coroutine;
131
132 QTAILQ_ENTRY(NBDClient) next;
133 int nb_requests;
134 bool closing;
135
136 uint32_t check_align; /* If non-zero, check for aligned client requests */
137
138 bool structured_reply;
139 NBDExportMetaContexts export_meta;
140
141 uint32_t opt; /* Current option being negotiated */
142 uint32_t optlen; /* remaining length of data in ioc for the option being
143 negotiated now */
144 };
145
146 static void nbd_client_receive_next_request(NBDClient *client);
147
148 /* Basic flow for negotiation
149
150 Server Client
151 Negotiate
152
153 or
154
155 Server Client
156 Negotiate #1
157 Option
158 Negotiate #2
159
160 ----
161
162 followed by
163
164 Server Client
165 Request
166 Response
167 Request
168 Response
169 ...
170 ...
171 Request (type == 2)
172
173 */
174
175 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
176 uint32_t type, uint32_t length)
177 {
178 stq_be_p(&rep->magic, NBD_REP_MAGIC);
179 stl_be_p(&rep->option, option);
180 stl_be_p(&rep->type, type);
181 stl_be_p(&rep->length, length);
182 }
183
184 /* Send a reply header, including length, but no payload.
185 * Return -errno on error, 0 on success. */
186 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
187 uint32_t len, Error **errp)
188 {
189 NBDOptionReply rep;
190
191 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
192 type, nbd_rep_lookup(type), len);
193
194 assert(len < NBD_MAX_BUFFER_SIZE);
195
196 set_be_option_rep(&rep, client->opt, type, len);
197 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
198 }
199
200 /* Send a reply header with default 0 length.
201 * Return -errno on error, 0 on success. */
202 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
203 Error **errp)
204 {
205 return nbd_negotiate_send_rep_len(client, type, 0, errp);
206 }
207
208 /* Send an error reply.
209 * Return -errno on error, 0 on success. */
210 static int GCC_FMT_ATTR(4, 0)
211 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
212 Error **errp, const char *fmt, va_list va)
213 {
214 ERRP_GUARD();
215 g_autofree char *msg = NULL;
216 int ret;
217 size_t len;
218
219 msg = g_strdup_vprintf(fmt, va);
220 len = strlen(msg);
221 assert(len < NBD_MAX_STRING_SIZE);
222 trace_nbd_negotiate_send_rep_err(msg);
223 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
224 if (ret < 0) {
225 return ret;
226 }
227 if (nbd_write(client->ioc, msg, len, errp) < 0) {
228 error_prepend(errp, "write failed (error message): ");
229 return -EIO;
230 }
231
232 return 0;
233 }
234
235 /*
236 * Return a malloc'd copy of @name suitable for use in an error reply.
237 */
238 static char *
239 nbd_sanitize_name(const char *name)
240 {
241 if (strnlen(name, 80) < 80) {
242 return g_strdup(name);
243 }
244 /* XXX Should we also try to sanitize any control characters? */
245 return g_strdup_printf("%.80s...", name);
246 }
247
248 /* Send an error reply.
249 * Return -errno on error, 0 on success. */
250 static int GCC_FMT_ATTR(4, 5)
251 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
252 Error **errp, const char *fmt, ...)
253 {
254 va_list va;
255 int ret;
256
257 va_start(va, fmt);
258 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
259 va_end(va);
260 return ret;
261 }
262
263 /* Drop remainder of the current option, and send a reply with the
264 * given error type and message. Return -errno on read or write
265 * failure; or 0 if connection is still live. */
266 static int GCC_FMT_ATTR(4, 0)
267 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
268 const char *fmt, va_list va)
269 {
270 int ret = nbd_drop(client->ioc, client->optlen, errp);
271
272 client->optlen = 0;
273 if (!ret) {
274 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
275 }
276 return ret;
277 }
278
279 static int GCC_FMT_ATTR(4, 5)
280 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
281 const char *fmt, ...)
282 {
283 int ret;
284 va_list va;
285
286 va_start(va, fmt);
287 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
288 va_end(va);
289
290 return ret;
291 }
292
293 static int GCC_FMT_ATTR(3, 4)
294 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
295 {
296 int ret;
297 va_list va;
298
299 va_start(va, fmt);
300 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
301 va_end(va);
302
303 return ret;
304 }
305
306 /* Read size bytes from the unparsed payload of the current option.
307 * Return -errno on I/O error, 0 if option was completely handled by
308 * sending a reply about inconsistent lengths, or 1 on success. */
309 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
310 Error **errp)
311 {
312 if (size > client->optlen) {
313 return nbd_opt_invalid(client, errp,
314 "Inconsistent lengths in option %s",
315 nbd_opt_lookup(client->opt));
316 }
317 client->optlen -= size;
318 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
319 }
320
321 /* Drop size bytes from the unparsed payload of the current option.
322 * Return -errno on I/O error, 0 if option was completely handled by
323 * sending a reply about inconsistent lengths, or 1 on success. */
324 static int nbd_opt_skip(NBDClient *client, size_t size, Error **errp)
325 {
326 if (size > client->optlen) {
327 return nbd_opt_invalid(client, errp,
328 "Inconsistent lengths in option %s",
329 nbd_opt_lookup(client->opt));
330 }
331 client->optlen -= size;
332 return nbd_drop(client->ioc, size, errp) < 0 ? -EIO : 1;
333 }
334
335 /* nbd_opt_read_name
336 *
337 * Read a string with the format:
338 * uint32_t len (<= NBD_MAX_STRING_SIZE)
339 * len bytes string (not 0-terminated)
340 *
341 * On success, @name will be allocated.
342 * If @length is non-null, it will be set to the actual string length.
343 *
344 * Return -errno on I/O error, 0 if option was completely handled by
345 * sending a reply about inconsistent lengths, or 1 on success.
346 */
347 static int nbd_opt_read_name(NBDClient *client, char **name, uint32_t *length,
348 Error **errp)
349 {
350 int ret;
351 uint32_t len;
352 g_autofree char *local_name = NULL;
353
354 *name = NULL;
355 ret = nbd_opt_read(client, &len, sizeof(len), errp);
356 if (ret <= 0) {
357 return ret;
358 }
359 len = cpu_to_be32(len);
360
361 if (len > NBD_MAX_STRING_SIZE) {
362 return nbd_opt_invalid(client, errp,
363 "Invalid name length: %" PRIu32, len);
364 }
365
366 local_name = g_malloc(len + 1);
367 ret = nbd_opt_read(client, local_name, len, errp);
368 if (ret <= 0) {
369 return ret;
370 }
371 local_name[len] = '\0';
372
373 if (length) {
374 *length = len;
375 }
376 *name = g_steal_pointer(&local_name);
377
378 return 1;
379 }
380
381 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
382 * Return -errno on error, 0 on success. */
383 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
384 Error **errp)
385 {
386 ERRP_GUARD();
387 size_t name_len, desc_len;
388 uint32_t len;
389 const char *name = exp->name ? exp->name : "";
390 const char *desc = exp->description ? exp->description : "";
391 QIOChannel *ioc = client->ioc;
392 int ret;
393
394 trace_nbd_negotiate_send_rep_list(name, desc);
395 name_len = strlen(name);
396 desc_len = strlen(desc);
397 assert(name_len <= NBD_MAX_STRING_SIZE && desc_len <= NBD_MAX_STRING_SIZE);
398 len = name_len + desc_len + sizeof(len);
399 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
400 if (ret < 0) {
401 return ret;
402 }
403
404 len = cpu_to_be32(name_len);
405 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
406 error_prepend(errp, "write failed (name length): ");
407 return -EINVAL;
408 }
409
410 if (nbd_write(ioc, name, name_len, errp) < 0) {
411 error_prepend(errp, "write failed (name buffer): ");
412 return -EINVAL;
413 }
414
415 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
416 error_prepend(errp, "write failed (description buffer): ");
417 return -EINVAL;
418 }
419
420 return 0;
421 }
422
423 /* Process the NBD_OPT_LIST command, with a potential series of replies.
424 * Return -errno on error, 0 on success. */
425 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
426 {
427 NBDExport *exp;
428 assert(client->opt == NBD_OPT_LIST);
429
430 /* For each export, send a NBD_REP_SERVER reply. */
431 QTAILQ_FOREACH(exp, &exports, next) {
432 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
433 return -EINVAL;
434 }
435 }
436 /* Finish with a NBD_REP_ACK. */
437 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
438 }
439
440 static void nbd_check_meta_export(NBDClient *client)
441 {
442 client->export_meta.valid &= client->exp == client->export_meta.exp;
443 }
444
445 /* Send a reply to NBD_OPT_EXPORT_NAME.
446 * Return -errno on error, 0 on success. */
447 static int nbd_negotiate_handle_export_name(NBDClient *client, bool no_zeroes,
448 Error **errp)
449 {
450 ERRP_GUARD();
451 g_autofree char *name = NULL;
452 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
453 size_t len;
454 int ret;
455 uint16_t myflags;
456
457 /* Client sends:
458 [20 .. xx] export name (length bytes)
459 Server replies:
460 [ 0 .. 7] size
461 [ 8 .. 9] export flags
462 [10 .. 133] reserved (0) [unless no_zeroes]
463 */
464 trace_nbd_negotiate_handle_export_name();
465 if (client->optlen > NBD_MAX_STRING_SIZE) {
466 error_setg(errp, "Bad length received");
467 return -EINVAL;
468 }
469 name = g_malloc(client->optlen + 1);
470 if (nbd_read(client->ioc, name, client->optlen, "export name", errp) < 0) {
471 return -EIO;
472 }
473 name[client->optlen] = '\0';
474 client->optlen = 0;
475
476 trace_nbd_negotiate_handle_export_name_request(name);
477
478 client->exp = nbd_export_find(name);
479 if (!client->exp) {
480 error_setg(errp, "export not found");
481 return -EINVAL;
482 }
483
484 myflags = client->exp->nbdflags;
485 if (client->structured_reply) {
486 myflags |= NBD_FLAG_SEND_DF;
487 }
488 trace_nbd_negotiate_new_style_size_flags(client->exp->size, myflags);
489 stq_be_p(buf, client->exp->size);
490 stw_be_p(buf + 8, myflags);
491 len = no_zeroes ? 10 : sizeof(buf);
492 ret = nbd_write(client->ioc, buf, len, errp);
493 if (ret < 0) {
494 error_prepend(errp, "write failed: ");
495 return ret;
496 }
497
498 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
499 blk_exp_ref(&client->exp->common);
500 nbd_check_meta_export(client);
501
502 return 0;
503 }
504
505 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
506 * The buffer does NOT include the info type prefix.
507 * Return -errno on error, 0 if ready to send more. */
508 static int nbd_negotiate_send_info(NBDClient *client,
509 uint16_t info, uint32_t length, void *buf,
510 Error **errp)
511 {
512 int rc;
513
514 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
515 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
516 sizeof(info) + length, errp);
517 if (rc < 0) {
518 return rc;
519 }
520 info = cpu_to_be16(info);
521 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
522 return -EIO;
523 }
524 if (nbd_write(client->ioc, buf, length, errp) < 0) {
525 return -EIO;
526 }
527 return 0;
528 }
529
530 /* nbd_reject_length: Handle any unexpected payload.
531 * @fatal requests that we quit talking to the client, even if we are able
532 * to successfully send an error reply.
533 * Return:
534 * -errno transmission error occurred or @fatal was requested, errp is set
535 * 0 error message successfully sent to client, errp is not set
536 */
537 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
538 {
539 int ret;
540
541 assert(client->optlen);
542 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
543 nbd_opt_lookup(client->opt));
544 if (fatal && !ret) {
545 error_setg(errp, "option '%s' has unexpected length",
546 nbd_opt_lookup(client->opt));
547 return -EINVAL;
548 }
549 return ret;
550 }
551
552 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
553 * Return -errno on error, 0 if ready for next option, and 1 to move
554 * into transmission phase. */
555 static int nbd_negotiate_handle_info(NBDClient *client, Error **errp)
556 {
557 int rc;
558 g_autofree char *name = NULL;
559 NBDExport *exp;
560 uint16_t requests;
561 uint16_t request;
562 uint32_t namelen;
563 bool sendname = false;
564 bool blocksize = false;
565 uint32_t sizes[3];
566 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
567 uint32_t check_align = 0;
568 uint16_t myflags;
569
570 /* Client sends:
571 4 bytes: L, name length (can be 0)
572 L bytes: export name
573 2 bytes: N, number of requests (can be 0)
574 N * 2 bytes: N requests
575 */
576 rc = nbd_opt_read_name(client, &name, &namelen, errp);
577 if (rc <= 0) {
578 return rc;
579 }
580 trace_nbd_negotiate_handle_export_name_request(name);
581
582 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
583 if (rc <= 0) {
584 return rc;
585 }
586 requests = be16_to_cpu(requests);
587 trace_nbd_negotiate_handle_info_requests(requests);
588 while (requests--) {
589 rc = nbd_opt_read(client, &request, sizeof(request), errp);
590 if (rc <= 0) {
591 return rc;
592 }
593 request = be16_to_cpu(request);
594 trace_nbd_negotiate_handle_info_request(request,
595 nbd_info_lookup(request));
596 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
597 * everything else is either a request we don't know or
598 * something we send regardless of request */
599 switch (request) {
600 case NBD_INFO_NAME:
601 sendname = true;
602 break;
603 case NBD_INFO_BLOCK_SIZE:
604 blocksize = true;
605 break;
606 }
607 }
608 if (client->optlen) {
609 return nbd_reject_length(client, false, errp);
610 }
611
612 exp = nbd_export_find(name);
613 if (!exp) {
614 g_autofree char *sane_name = nbd_sanitize_name(name);
615
616 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
617 errp, "export '%s' not present",
618 sane_name);
619 }
620
621 /* Don't bother sending NBD_INFO_NAME unless client requested it */
622 if (sendname) {
623 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
624 errp);
625 if (rc < 0) {
626 return rc;
627 }
628 }
629
630 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
631 * client request */
632 if (exp->description) {
633 size_t len = strlen(exp->description);
634
635 assert(len <= NBD_MAX_STRING_SIZE);
636 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
637 len, exp->description, errp);
638 if (rc < 0) {
639 return rc;
640 }
641 }
642
643 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
644 * according to whether the client requested it, and according to
645 * whether this is OPT_INFO or OPT_GO. */
646 /* minimum - 1 for back-compat, or actual if client will obey it. */
647 if (client->opt == NBD_OPT_INFO || blocksize) {
648 check_align = sizes[0] = blk_get_request_alignment(exp->blk);
649 } else {
650 sizes[0] = 1;
651 }
652 assert(sizes[0] <= NBD_MAX_BUFFER_SIZE);
653 /* preferred - Hard-code to 4096 for now.
654 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
655 sizes[1] = MAX(4096, sizes[0]);
656 /* maximum - At most 32M, but smaller as appropriate. */
657 sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
658 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
659 sizes[0] = cpu_to_be32(sizes[0]);
660 sizes[1] = cpu_to_be32(sizes[1]);
661 sizes[2] = cpu_to_be32(sizes[2]);
662 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
663 sizeof(sizes), sizes, errp);
664 if (rc < 0) {
665 return rc;
666 }
667
668 /* Send NBD_INFO_EXPORT always */
669 myflags = exp->nbdflags;
670 if (client->structured_reply) {
671 myflags |= NBD_FLAG_SEND_DF;
672 }
673 trace_nbd_negotiate_new_style_size_flags(exp->size, myflags);
674 stq_be_p(buf, exp->size);
675 stw_be_p(buf + 8, myflags);
676 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
677 sizeof(buf), buf, errp);
678 if (rc < 0) {
679 return rc;
680 }
681
682 /*
683 * If the client is just asking for NBD_OPT_INFO, but forgot to
684 * request block sizes in a situation that would impact
685 * performance, then return an error. But for NBD_OPT_GO, we
686 * tolerate all clients, regardless of alignments.
687 */
688 if (client->opt == NBD_OPT_INFO && !blocksize &&
689 blk_get_request_alignment(exp->blk) > 1) {
690 return nbd_negotiate_send_rep_err(client,
691 NBD_REP_ERR_BLOCK_SIZE_REQD,
692 errp,
693 "request NBD_INFO_BLOCK_SIZE to "
694 "use this export");
695 }
696
697 /* Final reply */
698 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
699 if (rc < 0) {
700 return rc;
701 }
702
703 if (client->opt == NBD_OPT_GO) {
704 client->exp = exp;
705 client->check_align = check_align;
706 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
707 blk_exp_ref(&client->exp->common);
708 nbd_check_meta_export(client);
709 rc = 1;
710 }
711 return rc;
712 }
713
714
715 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
716 * new channel for all further (now-encrypted) communication. */
717 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
718 Error **errp)
719 {
720 QIOChannel *ioc;
721 QIOChannelTLS *tioc;
722 struct NBDTLSHandshakeData data = { 0 };
723
724 assert(client->opt == NBD_OPT_STARTTLS);
725
726 trace_nbd_negotiate_handle_starttls();
727 ioc = client->ioc;
728
729 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
730 return NULL;
731 }
732
733 tioc = qio_channel_tls_new_server(ioc,
734 client->tlscreds,
735 client->tlsauthz,
736 errp);
737 if (!tioc) {
738 return NULL;
739 }
740
741 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
742 trace_nbd_negotiate_handle_starttls_handshake();
743 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
744 qio_channel_tls_handshake(tioc,
745 nbd_tls_handshake,
746 &data,
747 NULL,
748 NULL);
749
750 if (!data.complete) {
751 g_main_loop_run(data.loop);
752 }
753 g_main_loop_unref(data.loop);
754 if (data.error) {
755 object_unref(OBJECT(tioc));
756 error_propagate(errp, data.error);
757 return NULL;
758 }
759
760 return QIO_CHANNEL(tioc);
761 }
762
763 /* nbd_negotiate_send_meta_context
764 *
765 * Send one chunk of reply to NBD_OPT_{LIST,SET}_META_CONTEXT
766 *
767 * For NBD_OPT_LIST_META_CONTEXT @context_id is ignored, 0 is used instead.
768 */
769 static int nbd_negotiate_send_meta_context(NBDClient *client,
770 const char *context,
771 uint32_t context_id,
772 Error **errp)
773 {
774 NBDOptionReplyMetaContext opt;
775 struct iovec iov[] = {
776 {.iov_base = &opt, .iov_len = sizeof(opt)},
777 {.iov_base = (void *)context, .iov_len = strlen(context)}
778 };
779
780 assert(iov[1].iov_len <= NBD_MAX_STRING_SIZE);
781 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
782 context_id = 0;
783 }
784
785 trace_nbd_negotiate_meta_query_reply(context, context_id);
786 set_be_option_rep(&opt.h, client->opt, NBD_REP_META_CONTEXT,
787 sizeof(opt) - sizeof(opt.h) + iov[1].iov_len);
788 stl_be_p(&opt.context_id, context_id);
789
790 return qio_channel_writev_all(client->ioc, iov, 2, errp) < 0 ? -EIO : 0;
791 }
792
793 /* Read strlen(@pattern) bytes, and set @match to true if they match @pattern.
794 * @match is never set to false.
795 *
796 * Return -errno on I/O error, 0 if option was completely handled by
797 * sending a reply about inconsistent lengths, or 1 on success.
798 *
799 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
800 * It only means that there are no errors.
801 */
802 static int nbd_meta_pattern(NBDClient *client, const char *pattern, bool *match,
803 Error **errp)
804 {
805 int ret;
806 char *query;
807 size_t len = strlen(pattern);
808
809 assert(len);
810
811 query = g_malloc(len);
812 ret = nbd_opt_read(client, query, len, errp);
813 if (ret <= 0) {
814 g_free(query);
815 return ret;
816 }
817
818 if (strncmp(query, pattern, len) == 0) {
819 trace_nbd_negotiate_meta_query_parse(pattern);
820 *match = true;
821 } else {
822 trace_nbd_negotiate_meta_query_skip("pattern not matched");
823 }
824 g_free(query);
825
826 return 1;
827 }
828
829 /*
830 * Read @len bytes, and set @match to true if they match @pattern, or if @len
831 * is 0 and the client is performing _LIST_. @match is never set to false.
832 *
833 * Return -errno on I/O error, 0 if option was completely handled by
834 * sending a reply about inconsistent lengths, or 1 on success.
835 *
836 * Note: return code = 1 doesn't mean that we've read exactly @pattern.
837 * It only means that there are no errors.
838 */
839 static int nbd_meta_empty_or_pattern(NBDClient *client, const char *pattern,
840 uint32_t len, bool *match, Error **errp)
841 {
842 if (len == 0) {
843 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
844 *match = true;
845 }
846 trace_nbd_negotiate_meta_query_parse("empty");
847 return 1;
848 }
849
850 if (len != strlen(pattern)) {
851 trace_nbd_negotiate_meta_query_skip("different lengths");
852 return nbd_opt_skip(client, len, errp);
853 }
854
855 return nbd_meta_pattern(client, pattern, match, errp);
856 }
857
858 /* nbd_meta_base_query
859 *
860 * Handle queries to 'base' namespace. For now, only the base:allocation
861 * context is available. 'len' is the amount of text remaining to be read from
862 * the current name, after the 'base:' portion has been stripped.
863 *
864 * Return -errno on I/O error, 0 if option was completely handled by
865 * sending a reply about inconsistent lengths, or 1 on success.
866 */
867 static int nbd_meta_base_query(NBDClient *client, NBDExportMetaContexts *meta,
868 uint32_t len, Error **errp)
869 {
870 return nbd_meta_empty_or_pattern(client, "allocation", len,
871 &meta->base_allocation, errp);
872 }
873
874 /* nbd_meta_bitmap_query
875 *
876 * Handle query to 'qemu:' namespace.
877 * @len is the amount of text remaining to be read from the current name, after
878 * the 'qemu:' portion has been stripped.
879 *
880 * Return -errno on I/O error, 0 if option was completely handled by
881 * sending a reply about inconsistent lengths, or 1 on success. */
882 static int nbd_meta_qemu_query(NBDClient *client, NBDExportMetaContexts *meta,
883 uint32_t len, Error **errp)
884 {
885 bool dirty_bitmap = false;
886 size_t dirty_bitmap_len = strlen("dirty-bitmap:");
887 int ret;
888
889 if (!meta->exp->export_bitmap) {
890 trace_nbd_negotiate_meta_query_skip("no dirty-bitmap exported");
891 return nbd_opt_skip(client, len, errp);
892 }
893
894 if (len == 0) {
895 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
896 meta->bitmap = true;
897 }
898 trace_nbd_negotiate_meta_query_parse("empty");
899 return 1;
900 }
901
902 if (len < dirty_bitmap_len) {
903 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
904 return nbd_opt_skip(client, len, errp);
905 }
906
907 len -= dirty_bitmap_len;
908 ret = nbd_meta_pattern(client, "dirty-bitmap:", &dirty_bitmap, errp);
909 if (ret <= 0) {
910 return ret;
911 }
912 if (!dirty_bitmap) {
913 trace_nbd_negotiate_meta_query_skip("not dirty-bitmap:");
914 return nbd_opt_skip(client, len, errp);
915 }
916
917 trace_nbd_negotiate_meta_query_parse("dirty-bitmap:");
918
919 return nbd_meta_empty_or_pattern(
920 client, meta->exp->export_bitmap_context +
921 strlen("qemu:dirty_bitmap:"), len, &meta->bitmap, errp);
922 }
923
924 /* nbd_negotiate_meta_query
925 *
926 * Parse namespace name and call corresponding function to parse body of the
927 * query.
928 *
929 * The only supported namespaces are 'base' and 'qemu'.
930 *
931 * The function aims not wasting time and memory to read long unknown namespace
932 * names.
933 *
934 * Return -errno on I/O error, 0 if option was completely handled by
935 * sending a reply about inconsistent lengths, or 1 on success. */
936 static int nbd_negotiate_meta_query(NBDClient *client,
937 NBDExportMetaContexts *meta, Error **errp)
938 {
939 /*
940 * Both 'qemu' and 'base' namespaces have length = 5 including a
941 * colon. If another length namespace is later introduced, this
942 * should certainly be refactored.
943 */
944 int ret;
945 size_t ns_len = 5;
946 char ns[5];
947 uint32_t len;
948
949 ret = nbd_opt_read(client, &len, sizeof(len), errp);
950 if (ret <= 0) {
951 return ret;
952 }
953 len = cpu_to_be32(len);
954
955 if (len > NBD_MAX_STRING_SIZE) {
956 trace_nbd_negotiate_meta_query_skip("length too long");
957 return nbd_opt_skip(client, len, errp);
958 }
959 if (len < ns_len) {
960 trace_nbd_negotiate_meta_query_skip("length too short");
961 return nbd_opt_skip(client, len, errp);
962 }
963
964 len -= ns_len;
965 ret = nbd_opt_read(client, ns, ns_len, errp);
966 if (ret <= 0) {
967 return ret;
968 }
969
970 if (!strncmp(ns, "base:", ns_len)) {
971 trace_nbd_negotiate_meta_query_parse("base:");
972 return nbd_meta_base_query(client, meta, len, errp);
973 } else if (!strncmp(ns, "qemu:", ns_len)) {
974 trace_nbd_negotiate_meta_query_parse("qemu:");
975 return nbd_meta_qemu_query(client, meta, len, errp);
976 }
977
978 trace_nbd_negotiate_meta_query_skip("unknown namespace");
979 return nbd_opt_skip(client, len, errp);
980 }
981
982 /* nbd_negotiate_meta_queries
983 * Handle NBD_OPT_LIST_META_CONTEXT and NBD_OPT_SET_META_CONTEXT
984 *
985 * Return -errno on I/O error, or 0 if option was completely handled. */
986 static int nbd_negotiate_meta_queries(NBDClient *client,
987 NBDExportMetaContexts *meta, Error **errp)
988 {
989 int ret;
990 g_autofree char *export_name = NULL;
991 NBDExportMetaContexts local_meta;
992 uint32_t nb_queries;
993 int i;
994
995 if (!client->structured_reply) {
996 return nbd_opt_invalid(client, errp,
997 "request option '%s' when structured reply "
998 "is not negotiated",
999 nbd_opt_lookup(client->opt));
1000 }
1001
1002 if (client->opt == NBD_OPT_LIST_META_CONTEXT) {
1003 /* Only change the caller's meta on SET. */
1004 meta = &local_meta;
1005 }
1006
1007 memset(meta, 0, sizeof(*meta));
1008
1009 ret = nbd_opt_read_name(client, &export_name, NULL, errp);
1010 if (ret <= 0) {
1011 return ret;
1012 }
1013
1014 meta->exp = nbd_export_find(export_name);
1015 if (meta->exp == NULL) {
1016 g_autofree char *sane_name = nbd_sanitize_name(export_name);
1017
1018 return nbd_opt_drop(client, NBD_REP_ERR_UNKNOWN, errp,
1019 "export '%s' not present", sane_name);
1020 }
1021
1022 ret = nbd_opt_read(client, &nb_queries, sizeof(nb_queries), errp);
1023 if (ret <= 0) {
1024 return ret;
1025 }
1026 nb_queries = cpu_to_be32(nb_queries);
1027 trace_nbd_negotiate_meta_context(nbd_opt_lookup(client->opt),
1028 export_name, nb_queries);
1029
1030 if (client->opt == NBD_OPT_LIST_META_CONTEXT && !nb_queries) {
1031 /* enable all known contexts */
1032 meta->base_allocation = true;
1033 meta->bitmap = !!meta->exp->export_bitmap;
1034 } else {
1035 for (i = 0; i < nb_queries; ++i) {
1036 ret = nbd_negotiate_meta_query(client, meta, errp);
1037 if (ret <= 0) {
1038 return ret;
1039 }
1040 }
1041 }
1042
1043 if (meta->base_allocation) {
1044 ret = nbd_negotiate_send_meta_context(client, "base:allocation",
1045 NBD_META_ID_BASE_ALLOCATION,
1046 errp);
1047 if (ret < 0) {
1048 return ret;
1049 }
1050 }
1051
1052 if (meta->bitmap) {
1053 ret = nbd_negotiate_send_meta_context(client,
1054 meta->exp->export_bitmap_context,
1055 NBD_META_ID_DIRTY_BITMAP,
1056 errp);
1057 if (ret < 0) {
1058 return ret;
1059 }
1060 }
1061
1062 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1063 if (ret == 0) {
1064 meta->valid = true;
1065 }
1066
1067 return ret;
1068 }
1069
1070 /* nbd_negotiate_options
1071 * Process all NBD_OPT_* client option commands, during fixed newstyle
1072 * negotiation.
1073 * Return:
1074 * -errno on error, errp is set
1075 * 0 on successful negotiation, errp is not set
1076 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1077 * errp is not set
1078 */
1079 static int nbd_negotiate_options(NBDClient *client, Error **errp)
1080 {
1081 uint32_t flags;
1082 bool fixedNewstyle = false;
1083 bool no_zeroes = false;
1084
1085 /* Client sends:
1086 [ 0 .. 3] client flags
1087
1088 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
1089 [ 0 .. 7] NBD_OPTS_MAGIC
1090 [ 8 .. 11] NBD option
1091 [12 .. 15] Data length
1092 ... Rest of request
1093
1094 [ 0 .. 7] NBD_OPTS_MAGIC
1095 [ 8 .. 11] Second NBD option
1096 [12 .. 15] Data length
1097 ... Rest of request
1098 */
1099
1100 if (nbd_read32(client->ioc, &flags, "flags", errp) < 0) {
1101 return -EIO;
1102 }
1103 trace_nbd_negotiate_options_flags(flags);
1104 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
1105 fixedNewstyle = true;
1106 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
1107 }
1108 if (flags & NBD_FLAG_C_NO_ZEROES) {
1109 no_zeroes = true;
1110 flags &= ~NBD_FLAG_C_NO_ZEROES;
1111 }
1112 if (flags != 0) {
1113 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
1114 return -EINVAL;
1115 }
1116
1117 while (1) {
1118 int ret;
1119 uint32_t option, length;
1120 uint64_t magic;
1121
1122 if (nbd_read64(client->ioc, &magic, "opts magic", errp) < 0) {
1123 return -EINVAL;
1124 }
1125 trace_nbd_negotiate_options_check_magic(magic);
1126 if (magic != NBD_OPTS_MAGIC) {
1127 error_setg(errp, "Bad magic received");
1128 return -EINVAL;
1129 }
1130
1131 if (nbd_read32(client->ioc, &option, "option", errp) < 0) {
1132 return -EINVAL;
1133 }
1134 client->opt = option;
1135
1136 if (nbd_read32(client->ioc, &length, "option length", errp) < 0) {
1137 return -EINVAL;
1138 }
1139 assert(!client->optlen);
1140 client->optlen = length;
1141
1142 if (length > NBD_MAX_BUFFER_SIZE) {
1143 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1144 length, NBD_MAX_BUFFER_SIZE);
1145 return -EINVAL;
1146 }
1147
1148 trace_nbd_negotiate_options_check_option(option,
1149 nbd_opt_lookup(option));
1150 if (client->tlscreds &&
1151 client->ioc == (QIOChannel *)client->sioc) {
1152 QIOChannel *tioc;
1153 if (!fixedNewstyle) {
1154 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
1155 return -EINVAL;
1156 }
1157 switch (option) {
1158 case NBD_OPT_STARTTLS:
1159 if (length) {
1160 /* Unconditionally drop the connection if the client
1161 * can't start a TLS negotiation correctly */
1162 return nbd_reject_length(client, true, errp);
1163 }
1164 tioc = nbd_negotiate_handle_starttls(client, errp);
1165 if (!tioc) {
1166 return -EIO;
1167 }
1168 ret = 0;
1169 object_unref(OBJECT(client->ioc));
1170 client->ioc = QIO_CHANNEL(tioc);
1171 break;
1172
1173 case NBD_OPT_EXPORT_NAME:
1174 /* No way to return an error to client, so drop connection */
1175 error_setg(errp, "Option 0x%x not permitted before TLS",
1176 option);
1177 return -EINVAL;
1178
1179 default:
1180 /* Let the client keep trying, unless they asked to
1181 * quit. Always try to give an error back to the
1182 * client; but when replying to OPT_ABORT, be aware
1183 * that the client may hang up before receiving the
1184 * error, in which case we are fine ignoring the
1185 * resulting EPIPE. */
1186 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD,
1187 option == NBD_OPT_ABORT ? NULL : errp,
1188 "Option 0x%" PRIx32
1189 " not permitted before TLS", option);
1190 if (option == NBD_OPT_ABORT) {
1191 return 1;
1192 }
1193 break;
1194 }
1195 } else if (fixedNewstyle) {
1196 switch (option) {
1197 case NBD_OPT_LIST:
1198 if (length) {
1199 ret = nbd_reject_length(client, false, errp);
1200 } else {
1201 ret = nbd_negotiate_handle_list(client, errp);
1202 }
1203 break;
1204
1205 case NBD_OPT_ABORT:
1206 /* NBD spec says we must try to reply before
1207 * disconnecting, but that we must also tolerate
1208 * guests that don't wait for our reply. */
1209 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
1210 return 1;
1211
1212 case NBD_OPT_EXPORT_NAME:
1213 return nbd_negotiate_handle_export_name(client, no_zeroes,
1214 errp);
1215
1216 case NBD_OPT_INFO:
1217 case NBD_OPT_GO:
1218 ret = nbd_negotiate_handle_info(client, errp);
1219 if (ret == 1) {
1220 assert(option == NBD_OPT_GO);
1221 return 0;
1222 }
1223 break;
1224
1225 case NBD_OPT_STARTTLS:
1226 if (length) {
1227 ret = nbd_reject_length(client, false, errp);
1228 } else if (client->tlscreds) {
1229 ret = nbd_negotiate_send_rep_err(client,
1230 NBD_REP_ERR_INVALID, errp,
1231 "TLS already enabled");
1232 } else {
1233 ret = nbd_negotiate_send_rep_err(client,
1234 NBD_REP_ERR_POLICY, errp,
1235 "TLS not configured");
1236 }
1237 break;
1238
1239 case NBD_OPT_STRUCTURED_REPLY:
1240 if (length) {
1241 ret = nbd_reject_length(client, false, errp);
1242 } else if (client->structured_reply) {
1243 ret = nbd_negotiate_send_rep_err(
1244 client, NBD_REP_ERR_INVALID, errp,
1245 "structured reply already negotiated");
1246 } else {
1247 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
1248 client->structured_reply = true;
1249 }
1250 break;
1251
1252 case NBD_OPT_LIST_META_CONTEXT:
1253 case NBD_OPT_SET_META_CONTEXT:
1254 ret = nbd_negotiate_meta_queries(client, &client->export_meta,
1255 errp);
1256 break;
1257
1258 default:
1259 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
1260 "Unsupported option %" PRIu32 " (%s)",
1261 option, nbd_opt_lookup(option));
1262 break;
1263 }
1264 } else {
1265 /*
1266 * If broken new-style we should drop the connection
1267 * for anything except NBD_OPT_EXPORT_NAME
1268 */
1269 switch (option) {
1270 case NBD_OPT_EXPORT_NAME:
1271 return nbd_negotiate_handle_export_name(client, no_zeroes,
1272 errp);
1273
1274 default:
1275 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
1276 option, nbd_opt_lookup(option));
1277 return -EINVAL;
1278 }
1279 }
1280 if (ret < 0) {
1281 return ret;
1282 }
1283 }
1284 }
1285
1286 /* nbd_negotiate
1287 * Return:
1288 * -errno on error, errp is set
1289 * 0 on successful negotiation, errp is not set
1290 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
1291 * errp is not set
1292 */
1293 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
1294 {
1295 ERRP_GUARD();
1296 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
1297 int ret;
1298
1299 /* Old style negotiation header, no room for options
1300 [ 0 .. 7] passwd ("NBDMAGIC")
1301 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
1302 [16 .. 23] size
1303 [24 .. 27] export flags (zero-extended)
1304 [28 .. 151] reserved (0)
1305
1306 New style negotiation header, client can send options
1307 [ 0 .. 7] passwd ("NBDMAGIC")
1308 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
1309 [16 .. 17] server flags (0)
1310 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
1311 */
1312
1313 qio_channel_set_blocking(client->ioc, false, NULL);
1314
1315 trace_nbd_negotiate_begin();
1316 memcpy(buf, "NBDMAGIC", 8);
1317
1318 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
1319 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
1320
1321 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
1322 error_prepend(errp, "write failed: ");
1323 return -EINVAL;
1324 }
1325 ret = nbd_negotiate_options(client, errp);
1326 if (ret != 0) {
1327 if (ret < 0) {
1328 error_prepend(errp, "option negotiation failed: ");
1329 }
1330 return ret;
1331 }
1332
1333 /* Attach the channel to the same AioContext as the export */
1334 if (client->exp && client->exp->common.ctx) {
1335 qio_channel_attach_aio_context(client->ioc, client->exp->common.ctx);
1336 }
1337
1338 assert(!client->optlen);
1339 trace_nbd_negotiate_success();
1340
1341 return 0;
1342 }
1343
1344 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
1345 Error **errp)
1346 {
1347 uint8_t buf[NBD_REQUEST_SIZE];
1348 uint32_t magic;
1349 int ret;
1350
1351 ret = nbd_read(ioc, buf, sizeof(buf), "request", errp);
1352 if (ret < 0) {
1353 return ret;
1354 }
1355
1356 /* Request
1357 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
1358 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
1359 [ 6 .. 7] type (NBD_CMD_READ, ...)
1360 [ 8 .. 15] handle
1361 [16 .. 23] from
1362 [24 .. 27] len
1363 */
1364
1365 magic = ldl_be_p(buf);
1366 request->flags = lduw_be_p(buf + 4);
1367 request->type = lduw_be_p(buf + 6);
1368 request->handle = ldq_be_p(buf + 8);
1369 request->from = ldq_be_p(buf + 16);
1370 request->len = ldl_be_p(buf + 24);
1371
1372 trace_nbd_receive_request(magic, request->flags, request->type,
1373 request->from, request->len);
1374
1375 if (magic != NBD_REQUEST_MAGIC) {
1376 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
1377 return -EINVAL;
1378 }
1379 return 0;
1380 }
1381
1382 #define MAX_NBD_REQUESTS 16
1383
1384 void nbd_client_get(NBDClient *client)
1385 {
1386 client->refcount++;
1387 }
1388
1389 void nbd_client_put(NBDClient *client)
1390 {
1391 if (--client->refcount == 0) {
1392 /* The last reference should be dropped by client->close,
1393 * which is called by client_close.
1394 */
1395 assert(client->closing);
1396
1397 qio_channel_detach_aio_context(client->ioc);
1398 object_unref(OBJECT(client->sioc));
1399 object_unref(OBJECT(client->ioc));
1400 if (client->tlscreds) {
1401 object_unref(OBJECT(client->tlscreds));
1402 }
1403 g_free(client->tlsauthz);
1404 if (client->exp) {
1405 QTAILQ_REMOVE(&client->exp->clients, client, next);
1406 blk_exp_unref(&client->exp->common);
1407 }
1408 g_free(client);
1409 }
1410 }
1411
1412 static void client_close(NBDClient *client, bool negotiated)
1413 {
1414 if (client->closing) {
1415 return;
1416 }
1417
1418 client->closing = true;
1419
1420 /* Force requests to finish. They will drop their own references,
1421 * then we'll close the socket and free the NBDClient.
1422 */
1423 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1424 NULL);
1425
1426 /* Also tell the client, so that they release their reference. */
1427 if (client->close_fn) {
1428 client->close_fn(client, negotiated);
1429 }
1430 }
1431
1432 static NBDRequestData *nbd_request_get(NBDClient *client)
1433 {
1434 NBDRequestData *req;
1435
1436 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1437 client->nb_requests++;
1438
1439 req = g_new0(NBDRequestData, 1);
1440 nbd_client_get(client);
1441 req->client = client;
1442 return req;
1443 }
1444
1445 static void nbd_request_put(NBDRequestData *req)
1446 {
1447 NBDClient *client = req->client;
1448
1449 if (req->data) {
1450 qemu_vfree(req->data);
1451 }
1452 g_free(req);
1453
1454 client->nb_requests--;
1455 nbd_client_receive_next_request(client);
1456
1457 nbd_client_put(client);
1458 }
1459
1460 static void blk_aio_attached(AioContext *ctx, void *opaque)
1461 {
1462 NBDExport *exp = opaque;
1463 NBDClient *client;
1464
1465 trace_nbd_blk_aio_attached(exp->name, ctx);
1466
1467 exp->common.ctx = ctx;
1468
1469 QTAILQ_FOREACH(client, &exp->clients, next) {
1470 qio_channel_attach_aio_context(client->ioc, ctx);
1471 if (client->recv_coroutine) {
1472 aio_co_schedule(ctx, client->recv_coroutine);
1473 }
1474 if (client->send_coroutine) {
1475 aio_co_schedule(ctx, client->send_coroutine);
1476 }
1477 }
1478 }
1479
1480 static void blk_aio_detach(void *opaque)
1481 {
1482 NBDExport *exp = opaque;
1483 NBDClient *client;
1484
1485 trace_nbd_blk_aio_detach(exp->name, exp->common.ctx);
1486
1487 QTAILQ_FOREACH(client, &exp->clients, next) {
1488 qio_channel_detach_aio_context(client->ioc);
1489 }
1490
1491 exp->common.ctx = NULL;
1492 }
1493
1494 static void nbd_eject_notifier(Notifier *n, void *data)
1495 {
1496 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1497 AioContext *aio_context;
1498
1499 aio_context = exp->common.ctx;
1500 aio_context_acquire(aio_context);
1501 nbd_export_close(exp);
1502 aio_context_release(aio_context);
1503 }
1504
1505 void nbd_export_set_on_eject_blk(BlockExport *exp, BlockBackend *blk)
1506 {
1507 NBDExport *nbd_exp = container_of(exp, NBDExport, common);
1508 assert(exp->drv == &blk_exp_nbd);
1509 assert(nbd_exp->eject_notifier_blk == NULL);
1510
1511 blk_ref(blk);
1512 nbd_exp->eject_notifier_blk = blk;
1513 nbd_exp->eject_notifier.notify = nbd_eject_notifier;
1514 blk_add_remove_bs_notifier(blk, &nbd_exp->eject_notifier);
1515 }
1516
1517 int nbd_export_new(BlockExport *blk_exp, BlockDriverState *bs,
1518 const char *name, const char *desc,
1519 const char *bitmap, bool readonly, bool shared,
1520 bool writethrough, Error **errp)
1521 {
1522 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1523 AioContext *ctx;
1524 BlockBackend *blk;
1525 int64_t size;
1526 uint64_t perm;
1527 int ret;
1528
1529 size = bdrv_getlength(bs);
1530 if (size < 0) {
1531 error_setg_errno(errp, -size,
1532 "Failed to determine the NBD export's length");
1533 return size;
1534 }
1535
1536 ctx = bdrv_get_aio_context(bs);
1537 blk_exp->ctx = ctx;
1538
1539 /*
1540 * NBD exports are used for non-shared storage migration. Make sure
1541 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1542 * access since the export could be available before migration handover.
1543 * ctx was acquired in the caller.
1544 */
1545 assert(name && strlen(name) <= NBD_MAX_STRING_SIZE);
1546
1547 bdrv_invalidate_cache(bs, NULL);
1548
1549 /* Don't allow resize while the NBD server is running, otherwise we don't
1550 * care what happens with the node. */
1551 perm = BLK_PERM_CONSISTENT_READ;
1552 if (!readonly) {
1553 perm |= BLK_PERM_WRITE;
1554 }
1555 blk = blk_new(ctx, perm,
1556 BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1557 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1558 ret = blk_insert_bs(blk, bs, errp);
1559 if (ret < 0) {
1560 goto fail;
1561 }
1562 blk_set_enable_write_cache(blk, !writethrough);
1563 blk_set_allow_aio_context_change(blk, true);
1564
1565 QTAILQ_INIT(&exp->clients);
1566 exp->blk = blk;
1567 exp->name = g_strdup(name);
1568 assert(!desc || strlen(desc) <= NBD_MAX_STRING_SIZE);
1569 exp->description = g_strdup(desc);
1570 exp->nbdflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_FLUSH |
1571 NBD_FLAG_SEND_FUA | NBD_FLAG_SEND_CACHE);
1572 if (readonly) {
1573 exp->nbdflags |= NBD_FLAG_READ_ONLY;
1574 if (shared) {
1575 exp->nbdflags |= NBD_FLAG_CAN_MULTI_CONN;
1576 }
1577 } else {
1578 exp->nbdflags |= (NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_WRITE_ZEROES |
1579 NBD_FLAG_SEND_FAST_ZERO);
1580 }
1581 exp->size = QEMU_ALIGN_DOWN(size, BDRV_SECTOR_SIZE);
1582
1583 if (bitmap) {
1584 BdrvDirtyBitmap *bm = NULL;
1585
1586 while (bs) {
1587 bm = bdrv_find_dirty_bitmap(bs, bitmap);
1588 if (bm != NULL) {
1589 break;
1590 }
1591
1592 bs = bdrv_filter_or_cow_bs(bs);
1593 }
1594
1595 if (bm == NULL) {
1596 ret = -ENOENT;
1597 error_setg(errp, "Bitmap '%s' is not found", bitmap);
1598 goto fail;
1599 }
1600
1601 if (bdrv_dirty_bitmap_check(bm, BDRV_BITMAP_ALLOW_RO, errp)) {
1602 ret = -EINVAL;
1603 goto fail;
1604 }
1605
1606 if (readonly && bdrv_is_writable(bs) &&
1607 bdrv_dirty_bitmap_enabled(bm)) {
1608 ret = -EINVAL;
1609 error_setg(errp,
1610 "Enabled bitmap '%s' incompatible with readonly export",
1611 bitmap);
1612 goto fail;
1613 }
1614
1615 bdrv_dirty_bitmap_set_busy(bm, true);
1616 exp->export_bitmap = bm;
1617 assert(strlen(bitmap) <= BDRV_BITMAP_MAX_NAME_SIZE);
1618 exp->export_bitmap_context = g_strdup_printf("qemu:dirty-bitmap:%s",
1619 bitmap);
1620 assert(strlen(exp->export_bitmap_context) < NBD_MAX_STRING_SIZE);
1621 }
1622
1623 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1624
1625 blk_exp_ref(&exp->common);
1626 QTAILQ_INSERT_TAIL(&exports, exp, next);
1627
1628 return 0;
1629
1630 fail:
1631 blk_unref(blk);
1632 g_free(exp->name);
1633 g_free(exp->description);
1634 return ret;
1635 }
1636
1637 NBDExport *nbd_export_find(const char *name)
1638 {
1639 NBDExport *exp;
1640 QTAILQ_FOREACH(exp, &exports, next) {
1641 if (strcmp(name, exp->name) == 0) {
1642 return exp;
1643 }
1644 }
1645
1646 return NULL;
1647 }
1648
1649 AioContext *
1650 nbd_export_aio_context(NBDExport *exp)
1651 {
1652 return exp->common.ctx;
1653 }
1654
1655 void nbd_export_close(NBDExport *exp)
1656 {
1657 NBDClient *client, *next;
1658
1659 blk_exp_ref(&exp->common);
1660 /*
1661 * TODO: Should we expand QMP NbdServerRemoveNode enum to allow a
1662 * close mode that stops advertising the export to new clients but
1663 * still permits existing clients to run to completion? Because of
1664 * that possibility, nbd_export_close() can be called more than
1665 * once on an export.
1666 */
1667 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1668 client_close(client, true);
1669 }
1670 if (exp->name) {
1671 blk_exp_unref(&exp->common);
1672 g_free(exp->name);
1673 exp->name = NULL;
1674 QTAILQ_REMOVE(&exports, exp, next);
1675 QTAILQ_INSERT_TAIL(&closed_exports, exp, next);
1676 }
1677 blk_exp_unref(&exp->common);
1678 }
1679
1680 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1681 {
1682 ERRP_GUARD();
1683 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1684 nbd_export_close(exp);
1685 return;
1686 }
1687
1688 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1689
1690 error_setg(errp, "export '%s' still in use", exp->name);
1691 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1692 }
1693
1694 static void nbd_export_delete(BlockExport *blk_exp)
1695 {
1696 NBDExport *exp = container_of(blk_exp, NBDExport, common);
1697
1698 assert(exp->name == NULL);
1699 assert(QTAILQ_EMPTY(&exp->clients));
1700
1701 g_free(exp->description);
1702 exp->description = NULL;
1703
1704 if (exp->blk) {
1705 if (exp->eject_notifier_blk) {
1706 notifier_remove(&exp->eject_notifier);
1707 blk_unref(exp->eject_notifier_blk);
1708 }
1709 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1710 blk_aio_detach, exp);
1711 blk_unref(exp->blk);
1712 exp->blk = NULL;
1713 }
1714
1715 if (exp->export_bitmap) {
1716 bdrv_dirty_bitmap_set_busy(exp->export_bitmap, false);
1717 g_free(exp->export_bitmap_context);
1718 }
1719
1720 QTAILQ_REMOVE(&closed_exports, exp, next);
1721 aio_wait_kick();
1722 }
1723
1724 const BlockExportDriver blk_exp_nbd = {
1725 .type = BLOCK_EXPORT_TYPE_NBD,
1726 .instance_size = sizeof(NBDExport),
1727 .create = nbd_export_create,
1728 .delete = nbd_export_delete,
1729 };
1730
1731 void nbd_export_close_all(void)
1732 {
1733 NBDExport *exp, *next;
1734 AioContext *aio_context;
1735
1736 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1737 aio_context = exp->common.ctx;
1738 aio_context_acquire(aio_context);
1739 nbd_export_close(exp);
1740 aio_context_release(aio_context);
1741 }
1742
1743 AIO_WAIT_WHILE(NULL, !(QTAILQ_EMPTY(&exports) &&
1744 QTAILQ_EMPTY(&closed_exports)));
1745 }
1746
1747 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1748 unsigned niov, Error **errp)
1749 {
1750 int ret;
1751
1752 g_assert(qemu_in_coroutine());
1753 qemu_co_mutex_lock(&client->send_lock);
1754 client->send_coroutine = qemu_coroutine_self();
1755
1756 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1757
1758 client->send_coroutine = NULL;
1759 qemu_co_mutex_unlock(&client->send_lock);
1760
1761 return ret;
1762 }
1763
1764 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1765 uint64_t handle)
1766 {
1767 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1768 stl_be_p(&reply->error, error);
1769 stq_be_p(&reply->handle, handle);
1770 }
1771
1772 static int nbd_co_send_simple_reply(NBDClient *client,
1773 uint64_t handle,
1774 uint32_t error,
1775 void *data,
1776 size_t len,
1777 Error **errp)
1778 {
1779 NBDSimpleReply reply;
1780 int nbd_err = system_errno_to_nbd_errno(error);
1781 struct iovec iov[] = {
1782 {.iov_base = &reply, .iov_len = sizeof(reply)},
1783 {.iov_base = data, .iov_len = len}
1784 };
1785
1786 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1787 len);
1788 set_be_simple_reply(&reply, nbd_err, handle);
1789
1790 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1791 }
1792
1793 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1794 uint16_t type, uint64_t handle, uint32_t length)
1795 {
1796 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1797 stw_be_p(&chunk->flags, flags);
1798 stw_be_p(&chunk->type, type);
1799 stq_be_p(&chunk->handle, handle);
1800 stl_be_p(&chunk->length, length);
1801 }
1802
1803 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1804 uint64_t handle,
1805 Error **errp)
1806 {
1807 NBDStructuredReplyChunk chunk;
1808 struct iovec iov[] = {
1809 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1810 };
1811
1812 trace_nbd_co_send_structured_done(handle);
1813 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1814
1815 return nbd_co_send_iov(client, iov, 1, errp);
1816 }
1817
1818 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1819 uint64_t handle,
1820 uint64_t offset,
1821 void *data,
1822 size_t size,
1823 bool final,
1824 Error **errp)
1825 {
1826 NBDStructuredReadData chunk;
1827 struct iovec iov[] = {
1828 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1829 {.iov_base = data, .iov_len = size}
1830 };
1831
1832 assert(size);
1833 trace_nbd_co_send_structured_read(handle, offset, data, size);
1834 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1835 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1836 sizeof(chunk) - sizeof(chunk.h) + size);
1837 stq_be_p(&chunk.offset, offset);
1838
1839 return nbd_co_send_iov(client, iov, 2, errp);
1840 }
1841
1842 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1843 uint64_t handle,
1844 uint32_t error,
1845 const char *msg,
1846 Error **errp)
1847 {
1848 NBDStructuredError chunk;
1849 int nbd_err = system_errno_to_nbd_errno(error);
1850 struct iovec iov[] = {
1851 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1852 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1853 };
1854
1855 assert(nbd_err);
1856 trace_nbd_co_send_structured_error(handle, nbd_err,
1857 nbd_err_lookup(nbd_err), msg ? msg : "");
1858 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1859 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1860 stl_be_p(&chunk.error, nbd_err);
1861 stw_be_p(&chunk.message_length, iov[1].iov_len);
1862
1863 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1864 }
1865
1866 /* Do a sparse read and send the structured reply to the client.
1867 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1868 * reported to the client, at which point this function succeeds.
1869 */
1870 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1871 uint64_t handle,
1872 uint64_t offset,
1873 uint8_t *data,
1874 size_t size,
1875 Error **errp)
1876 {
1877 int ret = 0;
1878 NBDExport *exp = client->exp;
1879 size_t progress = 0;
1880
1881 while (progress < size) {
1882 int64_t pnum;
1883 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1884 offset + progress,
1885 size - progress, &pnum, NULL,
1886 NULL);
1887 bool final;
1888
1889 if (status < 0) {
1890 char *msg = g_strdup_printf("unable to check for holes: %s",
1891 strerror(-status));
1892
1893 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1894 errp);
1895 g_free(msg);
1896 return ret;
1897 }
1898 assert(pnum && pnum <= size - progress);
1899 final = progress + pnum == size;
1900 if (status & BDRV_BLOCK_ZERO) {
1901 NBDStructuredReadHole chunk;
1902 struct iovec iov[] = {
1903 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1904 };
1905
1906 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1907 pnum);
1908 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1909 NBD_REPLY_TYPE_OFFSET_HOLE,
1910 handle, sizeof(chunk) - sizeof(chunk.h));
1911 stq_be_p(&chunk.offset, offset + progress);
1912 stl_be_p(&chunk.length, pnum);
1913 ret = nbd_co_send_iov(client, iov, 1, errp);
1914 } else {
1915 ret = blk_pread(exp->blk, offset + progress, data + progress, pnum);
1916 if (ret < 0) {
1917 error_setg_errno(errp, -ret, "reading from file failed");
1918 break;
1919 }
1920 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1921 data + progress, pnum, final,
1922 errp);
1923 }
1924
1925 if (ret < 0) {
1926 break;
1927 }
1928 progress += pnum;
1929 }
1930 return ret;
1931 }
1932
1933 typedef struct NBDExtentArray {
1934 NBDExtent *extents;
1935 unsigned int nb_alloc;
1936 unsigned int count;
1937 uint64_t total_length;
1938 bool can_add;
1939 bool converted_to_be;
1940 } NBDExtentArray;
1941
1942 static NBDExtentArray *nbd_extent_array_new(unsigned int nb_alloc)
1943 {
1944 NBDExtentArray *ea = g_new0(NBDExtentArray, 1);
1945
1946 ea->nb_alloc = nb_alloc;
1947 ea->extents = g_new(NBDExtent, nb_alloc);
1948 ea->can_add = true;
1949
1950 return ea;
1951 }
1952
1953 static void nbd_extent_array_free(NBDExtentArray *ea)
1954 {
1955 g_free(ea->extents);
1956 g_free(ea);
1957 }
1958 G_DEFINE_AUTOPTR_CLEANUP_FUNC(NBDExtentArray, nbd_extent_array_free);
1959
1960 /* Further modifications of the array after conversion are abandoned */
1961 static void nbd_extent_array_convert_to_be(NBDExtentArray *ea)
1962 {
1963 int i;
1964
1965 assert(!ea->converted_to_be);
1966 ea->can_add = false;
1967 ea->converted_to_be = true;
1968
1969 for (i = 0; i < ea->count; i++) {
1970 ea->extents[i].flags = cpu_to_be32(ea->extents[i].flags);
1971 ea->extents[i].length = cpu_to_be32(ea->extents[i].length);
1972 }
1973 }
1974
1975 /*
1976 * Add extent to NBDExtentArray. If extent can't be added (no available space),
1977 * return -1.
1978 * For safety, when returning -1 for the first time, .can_add is set to false,
1979 * further call to nbd_extent_array_add() will crash.
1980 * (to avoid the situation, when after failing to add an extent (returned -1),
1981 * user miss this failure and add another extent, which is successfully added
1982 * (array is full, but new extent may be squashed into the last one), then we
1983 * have invalid array with skipped extent)
1984 */
1985 static int nbd_extent_array_add(NBDExtentArray *ea,
1986 uint32_t length, uint32_t flags)
1987 {
1988 assert(ea->can_add);
1989
1990 if (!length) {
1991 return 0;
1992 }
1993
1994 /* Extend previous extent if flags are the same */
1995 if (ea->count > 0 && flags == ea->extents[ea->count - 1].flags) {
1996 uint64_t sum = (uint64_t)length + ea->extents[ea->count - 1].length;
1997
1998 if (sum <= UINT32_MAX) {
1999 ea->extents[ea->count - 1].length = sum;
2000 ea->total_length += length;
2001 return 0;
2002 }
2003 }
2004
2005 if (ea->count >= ea->nb_alloc) {
2006 ea->can_add = false;
2007 return -1;
2008 }
2009
2010 ea->total_length += length;
2011 ea->extents[ea->count] = (NBDExtent) {.length = length, .flags = flags};
2012 ea->count++;
2013
2014 return 0;
2015 }
2016
2017 static int blockstatus_to_extents(BlockDriverState *bs, uint64_t offset,
2018 uint64_t bytes, NBDExtentArray *ea)
2019 {
2020 while (bytes) {
2021 uint32_t flags;
2022 int64_t num;
2023 int ret = bdrv_block_status_above(bs, NULL, offset, bytes, &num,
2024 NULL, NULL);
2025
2026 if (ret < 0) {
2027 return ret;
2028 }
2029
2030 flags = (ret & BDRV_BLOCK_ALLOCATED ? 0 : NBD_STATE_HOLE) |
2031 (ret & BDRV_BLOCK_ZERO ? NBD_STATE_ZERO : 0);
2032
2033 if (nbd_extent_array_add(ea, num, flags) < 0) {
2034 return 0;
2035 }
2036
2037 offset += num;
2038 bytes -= num;
2039 }
2040
2041 return 0;
2042 }
2043
2044 /*
2045 * nbd_co_send_extents
2046 *
2047 * @ea is converted to BE by the function
2048 * @last controls whether NBD_REPLY_FLAG_DONE is sent.
2049 */
2050 static int nbd_co_send_extents(NBDClient *client, uint64_t handle,
2051 NBDExtentArray *ea,
2052 bool last, uint32_t context_id, Error **errp)
2053 {
2054 NBDStructuredMeta chunk;
2055 struct iovec iov[] = {
2056 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
2057 {.iov_base = ea->extents, .iov_len = ea->count * sizeof(ea->extents[0])}
2058 };
2059
2060 nbd_extent_array_convert_to_be(ea);
2061
2062 trace_nbd_co_send_extents(handle, ea->count, context_id, ea->total_length,
2063 last);
2064 set_be_chunk(&chunk.h, last ? NBD_REPLY_FLAG_DONE : 0,
2065 NBD_REPLY_TYPE_BLOCK_STATUS,
2066 handle, sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
2067 stl_be_p(&chunk.context_id, context_id);
2068
2069 return nbd_co_send_iov(client, iov, 2, errp);
2070 }
2071
2072 /* Get block status from the exported device and send it to the client */
2073 static int nbd_co_send_block_status(NBDClient *client, uint64_t handle,
2074 BlockDriverState *bs, uint64_t offset,
2075 uint32_t length, bool dont_fragment,
2076 bool last, uint32_t context_id,
2077 Error **errp)
2078 {
2079 int ret;
2080 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2081 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2082
2083 ret = blockstatus_to_extents(bs, offset, length, ea);
2084 if (ret < 0) {
2085 return nbd_co_send_structured_error(
2086 client, handle, -ret, "can't get block status", errp);
2087 }
2088
2089 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2090 }
2091
2092 /* Populate @ea from a dirty bitmap. */
2093 static void bitmap_to_extents(BdrvDirtyBitmap *bitmap,
2094 uint64_t offset, uint64_t length,
2095 NBDExtentArray *es)
2096 {
2097 int64_t start, dirty_start, dirty_count;
2098 int64_t end = offset + length;
2099 bool full = false;
2100
2101 bdrv_dirty_bitmap_lock(bitmap);
2102
2103 for (start = offset;
2104 bdrv_dirty_bitmap_next_dirty_area(bitmap, start, end, INT32_MAX,
2105 &dirty_start, &dirty_count);
2106 start = dirty_start + dirty_count)
2107 {
2108 if ((nbd_extent_array_add(es, dirty_start - start, 0) < 0) ||
2109 (nbd_extent_array_add(es, dirty_count, NBD_STATE_DIRTY) < 0))
2110 {
2111 full = true;
2112 break;
2113 }
2114 }
2115
2116 if (!full) {
2117 /* last non dirty extent */
2118 nbd_extent_array_add(es, end - start, 0);
2119 }
2120
2121 bdrv_dirty_bitmap_unlock(bitmap);
2122 }
2123
2124 static int nbd_co_send_bitmap(NBDClient *client, uint64_t handle,
2125 BdrvDirtyBitmap *bitmap, uint64_t offset,
2126 uint32_t length, bool dont_fragment, bool last,
2127 uint32_t context_id, Error **errp)
2128 {
2129 unsigned int nb_extents = dont_fragment ? 1 : NBD_MAX_BLOCK_STATUS_EXTENTS;
2130 g_autoptr(NBDExtentArray) ea = nbd_extent_array_new(nb_extents);
2131
2132 bitmap_to_extents(bitmap, offset, length, ea);
2133
2134 return nbd_co_send_extents(client, handle, ea, last, context_id, errp);
2135 }
2136
2137 /* nbd_co_receive_request
2138 * Collect a client request. Return 0 if request looks valid, -EIO to drop
2139 * connection right away, and any other negative value to report an error to
2140 * the client (although the caller may still need to disconnect after reporting
2141 * the error).
2142 */
2143 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
2144 Error **errp)
2145 {
2146 NBDClient *client = req->client;
2147 int valid_flags;
2148
2149 g_assert(qemu_in_coroutine());
2150 assert(client->recv_coroutine == qemu_coroutine_self());
2151 if (nbd_receive_request(client->ioc, request, errp) < 0) {
2152 return -EIO;
2153 }
2154
2155 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
2156 nbd_cmd_lookup(request->type));
2157
2158 if (request->type != NBD_CMD_WRITE) {
2159 /* No payload, we are ready to read the next request. */
2160 req->complete = true;
2161 }
2162
2163 if (request->type == NBD_CMD_DISC) {
2164 /* Special case: we're going to disconnect without a reply,
2165 * whether or not flags, from, or len are bogus */
2166 return -EIO;
2167 }
2168
2169 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE ||
2170 request->type == NBD_CMD_CACHE)
2171 {
2172 if (request->len > NBD_MAX_BUFFER_SIZE) {
2173 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
2174 request->len, NBD_MAX_BUFFER_SIZE);
2175 return -EINVAL;
2176 }
2177
2178 if (request->type != NBD_CMD_CACHE) {
2179 req->data = blk_try_blockalign(client->exp->blk, request->len);
2180 if (req->data == NULL) {
2181 error_setg(errp, "No memory");
2182 return -ENOMEM;
2183 }
2184 }
2185 }
2186
2187 if (request->type == NBD_CMD_WRITE) {
2188 if (nbd_read(client->ioc, req->data, request->len, "CMD_WRITE data",
2189 errp) < 0)
2190 {
2191 return -EIO;
2192 }
2193 req->complete = true;
2194
2195 trace_nbd_co_receive_request_payload_received(request->handle,
2196 request->len);
2197 }
2198
2199 /* Sanity checks. */
2200 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
2201 (request->type == NBD_CMD_WRITE ||
2202 request->type == NBD_CMD_WRITE_ZEROES ||
2203 request->type == NBD_CMD_TRIM)) {
2204 error_setg(errp, "Export is read-only");
2205 return -EROFS;
2206 }
2207 if (request->from > client->exp->size ||
2208 request->len > client->exp->size - request->from) {
2209 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
2210 ", Size: %" PRIu64, request->from, request->len,
2211 client->exp->size);
2212 return (request->type == NBD_CMD_WRITE ||
2213 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
2214 }
2215 if (client->check_align && !QEMU_IS_ALIGNED(request->from | request->len,
2216 client->check_align)) {
2217 /*
2218 * The block layer gracefully handles unaligned requests, but
2219 * it's still worth tracing client non-compliance
2220 */
2221 trace_nbd_co_receive_align_compliance(nbd_cmd_lookup(request->type),
2222 request->from,
2223 request->len,
2224 client->check_align);
2225 }
2226 valid_flags = NBD_CMD_FLAG_FUA;
2227 if (request->type == NBD_CMD_READ && client->structured_reply) {
2228 valid_flags |= NBD_CMD_FLAG_DF;
2229 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
2230 valid_flags |= NBD_CMD_FLAG_NO_HOLE | NBD_CMD_FLAG_FAST_ZERO;
2231 } else if (request->type == NBD_CMD_BLOCK_STATUS) {
2232 valid_flags |= NBD_CMD_FLAG_REQ_ONE;
2233 }
2234 if (request->flags & ~valid_flags) {
2235 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
2236 nbd_cmd_lookup(request->type), request->flags);
2237 return -EINVAL;
2238 }
2239
2240 return 0;
2241 }
2242
2243 /* Send simple reply without a payload, or a structured error
2244 * @error_msg is ignored if @ret >= 0
2245 * Returns 0 if connection is still live, -errno on failure to talk to client
2246 */
2247 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
2248 uint64_t handle,
2249 int ret,
2250 const char *error_msg,
2251 Error **errp)
2252 {
2253 if (client->structured_reply && ret < 0) {
2254 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
2255 errp);
2256 } else {
2257 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
2258 NULL, 0, errp);
2259 }
2260 }
2261
2262 /* Handle NBD_CMD_READ request.
2263 * Return -errno if sending fails. Other errors are reported directly to the
2264 * client as an error reply. */
2265 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
2266 uint8_t *data, Error **errp)
2267 {
2268 int ret;
2269 NBDExport *exp = client->exp;
2270
2271 assert(request->type == NBD_CMD_READ);
2272
2273 /* XXX: NBD Protocol only documents use of FUA with WRITE */
2274 if (request->flags & NBD_CMD_FLAG_FUA) {
2275 ret = blk_co_flush(exp->blk);
2276 if (ret < 0) {
2277 return nbd_send_generic_reply(client, request->handle, ret,
2278 "flush failed", errp);
2279 }
2280 }
2281
2282 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
2283 request->len)
2284 {
2285 return nbd_co_send_sparse_read(client, request->handle, request->from,
2286 data, request->len, errp);
2287 }
2288
2289 ret = blk_pread(exp->blk, request->from, data, request->len);
2290 if (ret < 0) {
2291 return nbd_send_generic_reply(client, request->handle, ret,
2292 "reading from file failed", errp);
2293 }
2294
2295 if (client->structured_reply) {
2296 if (request->len) {
2297 return nbd_co_send_structured_read(client, request->handle,
2298 request->from, data,
2299 request->len, true, errp);
2300 } else {
2301 return nbd_co_send_structured_done(client, request->handle, errp);
2302 }
2303 } else {
2304 return nbd_co_send_simple_reply(client, request->handle, 0,
2305 data, request->len, errp);
2306 }
2307 }
2308
2309 /*
2310 * nbd_do_cmd_cache
2311 *
2312 * Handle NBD_CMD_CACHE request.
2313 * Return -errno if sending fails. Other errors are reported directly to the
2314 * client as an error reply.
2315 */
2316 static coroutine_fn int nbd_do_cmd_cache(NBDClient *client, NBDRequest *request,
2317 Error **errp)
2318 {
2319 int ret;
2320 NBDExport *exp = client->exp;
2321
2322 assert(request->type == NBD_CMD_CACHE);
2323
2324 ret = blk_co_preadv(exp->blk, request->from, request->len,
2325 NULL, BDRV_REQ_COPY_ON_READ | BDRV_REQ_PREFETCH);
2326
2327 return nbd_send_generic_reply(client, request->handle, ret,
2328 "caching data failed", errp);
2329 }
2330
2331 /* Handle NBD request.
2332 * Return -errno if sending fails. Other errors are reported directly to the
2333 * client as an error reply. */
2334 static coroutine_fn int nbd_handle_request(NBDClient *client,
2335 NBDRequest *request,
2336 uint8_t *data, Error **errp)
2337 {
2338 int ret;
2339 int flags;
2340 NBDExport *exp = client->exp;
2341 char *msg;
2342
2343 switch (request->type) {
2344 case NBD_CMD_CACHE:
2345 return nbd_do_cmd_cache(client, request, errp);
2346
2347 case NBD_CMD_READ:
2348 return nbd_do_cmd_read(client, request, data, errp);
2349
2350 case NBD_CMD_WRITE:
2351 flags = 0;
2352 if (request->flags & NBD_CMD_FLAG_FUA) {
2353 flags |= BDRV_REQ_FUA;
2354 }
2355 ret = blk_pwrite(exp->blk, request->from, data, request->len, flags);
2356 return nbd_send_generic_reply(client, request->handle, ret,
2357 "writing to file failed", errp);
2358
2359 case NBD_CMD_WRITE_ZEROES:
2360 flags = 0;
2361 if (request->flags & NBD_CMD_FLAG_FUA) {
2362 flags |= BDRV_REQ_FUA;
2363 }
2364 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
2365 flags |= BDRV_REQ_MAY_UNMAP;
2366 }
2367 if (request->flags & NBD_CMD_FLAG_FAST_ZERO) {
2368 flags |= BDRV_REQ_NO_FALLBACK;
2369 }
2370 ret = 0;
2371 /* FIXME simplify this when blk_pwrite_zeroes switches to 64-bit */
2372 while (ret >= 0 && request->len) {
2373 int align = client->check_align ?: 1;
2374 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2375 align));
2376 ret = blk_pwrite_zeroes(exp->blk, request->from, len, flags);
2377 request->len -= len;
2378 request->from += len;
2379 }
2380 return nbd_send_generic_reply(client, request->handle, ret,
2381 "writing to file failed", errp);
2382
2383 case NBD_CMD_DISC:
2384 /* unreachable, thanks to special case in nbd_co_receive_request() */
2385 abort();
2386
2387 case NBD_CMD_FLUSH:
2388 ret = blk_co_flush(exp->blk);
2389 return nbd_send_generic_reply(client, request->handle, ret,
2390 "flush failed", errp);
2391
2392 case NBD_CMD_TRIM:
2393 ret = 0;
2394 /* FIXME simplify this when blk_co_pdiscard switches to 64-bit */
2395 while (ret >= 0 && request->len) {
2396 int align = client->check_align ?: 1;
2397 int len = MIN(request->len, QEMU_ALIGN_DOWN(BDRV_REQUEST_MAX_BYTES,
2398 align));
2399 ret = blk_co_pdiscard(exp->blk, request->from, len);
2400 request->len -= len;
2401 request->from += len;
2402 }
2403 if (ret >= 0 && request->flags & NBD_CMD_FLAG_FUA) {
2404 ret = blk_co_flush(exp->blk);
2405 }
2406 return nbd_send_generic_reply(client, request->handle, ret,
2407 "discard failed", errp);
2408
2409 case NBD_CMD_BLOCK_STATUS:
2410 if (!request->len) {
2411 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2412 "need non-zero length", errp);
2413 }
2414 if (client->export_meta.valid &&
2415 (client->export_meta.base_allocation ||
2416 client->export_meta.bitmap))
2417 {
2418 bool dont_fragment = request->flags & NBD_CMD_FLAG_REQ_ONE;
2419
2420 if (client->export_meta.base_allocation) {
2421 ret = nbd_co_send_block_status(client, request->handle,
2422 blk_bs(exp->blk), request->from,
2423 request->len, dont_fragment,
2424 !client->export_meta.bitmap,
2425 NBD_META_ID_BASE_ALLOCATION,
2426 errp);
2427 if (ret < 0) {
2428 return ret;
2429 }
2430 }
2431
2432 if (client->export_meta.bitmap) {
2433 ret = nbd_co_send_bitmap(client, request->handle,
2434 client->exp->export_bitmap,
2435 request->from, request->len,
2436 dont_fragment,
2437 true, NBD_META_ID_DIRTY_BITMAP, errp);
2438 if (ret < 0) {
2439 return ret;
2440 }
2441 }
2442
2443 return 0;
2444 } else {
2445 return nbd_send_generic_reply(client, request->handle, -EINVAL,
2446 "CMD_BLOCK_STATUS not negotiated",
2447 errp);
2448 }
2449
2450 default:
2451 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
2452 request->type);
2453 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
2454 errp);
2455 g_free(msg);
2456 return ret;
2457 }
2458 }
2459
2460 /* Owns a reference to the NBDClient passed as opaque. */
2461 static coroutine_fn void nbd_trip(void *opaque)
2462 {
2463 NBDClient *client = opaque;
2464 NBDRequestData *req;
2465 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
2466 int ret;
2467 Error *local_err = NULL;
2468
2469 trace_nbd_trip();
2470 if (client->closing) {
2471 nbd_client_put(client);
2472 return;
2473 }
2474
2475 req = nbd_request_get(client);
2476 ret = nbd_co_receive_request(req, &request, &local_err);
2477 client->recv_coroutine = NULL;
2478
2479 if (client->closing) {
2480 /*
2481 * The client may be closed when we are blocked in
2482 * nbd_co_receive_request()
2483 */
2484 goto done;
2485 }
2486
2487 nbd_client_receive_next_request(client);
2488 if (ret == -EIO) {
2489 goto disconnect;
2490 }
2491
2492 if (ret < 0) {
2493 /* It wans't -EIO, so, according to nbd_co_receive_request()
2494 * semantics, we should return the error to the client. */
2495 Error *export_err = local_err;
2496
2497 local_err = NULL;
2498 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
2499 error_get_pretty(export_err), &local_err);
2500 error_free(export_err);
2501 } else {
2502 ret = nbd_handle_request(client, &request, req->data, &local_err);
2503 }
2504 if (ret < 0) {
2505 error_prepend(&local_err, "Failed to send reply: ");
2506 goto disconnect;
2507 }
2508
2509 /* We must disconnect after NBD_CMD_WRITE if we did not
2510 * read the payload.
2511 */
2512 if (!req->complete) {
2513 error_setg(&local_err, "Request handling failed in intermediate state");
2514 goto disconnect;
2515 }
2516
2517 done:
2518 nbd_request_put(req);
2519 nbd_client_put(client);
2520 return;
2521
2522 disconnect:
2523 if (local_err) {
2524 error_reportf_err(local_err, "Disconnect client, due to: ");
2525 }
2526 nbd_request_put(req);
2527 client_close(client, true);
2528 nbd_client_put(client);
2529 }
2530
2531 static void nbd_client_receive_next_request(NBDClient *client)
2532 {
2533 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
2534 nbd_client_get(client);
2535 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
2536 aio_co_schedule(client->exp->common.ctx, client->recv_coroutine);
2537 }
2538 }
2539
2540 static coroutine_fn void nbd_co_client_start(void *opaque)
2541 {
2542 NBDClient *client = opaque;
2543 Error *local_err = NULL;
2544
2545 qemu_co_mutex_init(&client->send_lock);
2546
2547 if (nbd_negotiate(client, &local_err)) {
2548 if (local_err) {
2549 error_report_err(local_err);
2550 }
2551 client_close(client, false);
2552 return;
2553 }
2554
2555 nbd_client_receive_next_request(client);
2556 }
2557
2558 /*
2559 * Create a new client listener using the given channel @sioc.
2560 * Begin servicing it in a coroutine. When the connection closes, call
2561 * @close_fn with an indication of whether the client completed negotiation.
2562 */
2563 void nbd_client_new(QIOChannelSocket *sioc,
2564 QCryptoTLSCreds *tlscreds,
2565 const char *tlsauthz,
2566 void (*close_fn)(NBDClient *, bool))
2567 {
2568 NBDClient *client;
2569 Coroutine *co;
2570
2571 client = g_new0(NBDClient, 1);
2572 client->refcount = 1;
2573 client->tlscreds = tlscreds;
2574 if (tlscreds) {
2575 object_ref(OBJECT(client->tlscreds));
2576 }
2577 client->tlsauthz = g_strdup(tlsauthz);
2578 client->sioc = sioc;
2579 object_ref(OBJECT(client->sioc));
2580 client->ioc = QIO_CHANNEL(sioc);
2581 object_ref(OBJECT(client->ioc));
2582 client->close_fn = close_fn;
2583
2584 co = qemu_coroutine_create(nbd_co_client_start, client);
2585 qemu_coroutine_enter(co);
2586 }