]> git.proxmox.com Git - mirror_qemu.git/blob - nbd/server.c
nbd/server: add nbd_read_opt_name helper
[mirror_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 #include "qapi/error.h"
22 #include "trace.h"
23 #include "nbd-internal.h"
24
25 static int system_errno_to_nbd_errno(int err)
26 {
27 switch (err) {
28 case 0:
29 return NBD_SUCCESS;
30 case EPERM:
31 case EROFS:
32 return NBD_EPERM;
33 case EIO:
34 return NBD_EIO;
35 case ENOMEM:
36 return NBD_ENOMEM;
37 #ifdef EDQUOT
38 case EDQUOT:
39 #endif
40 case EFBIG:
41 case ENOSPC:
42 return NBD_ENOSPC;
43 case EOVERFLOW:
44 return NBD_EOVERFLOW;
45 case ESHUTDOWN:
46 return NBD_ESHUTDOWN;
47 case EINVAL:
48 default:
49 return NBD_EINVAL;
50 }
51 }
52
53 /* Definitions for opaque data types */
54
55 typedef struct NBDRequestData NBDRequestData;
56
57 struct NBDRequestData {
58 QSIMPLEQ_ENTRY(NBDRequestData) entry;
59 NBDClient *client;
60 uint8_t *data;
61 bool complete;
62 };
63
64 struct NBDExport {
65 int refcount;
66 void (*close)(NBDExport *exp);
67
68 BlockBackend *blk;
69 char *name;
70 char *description;
71 off_t dev_offset;
72 off_t size;
73 uint16_t nbdflags;
74 QTAILQ_HEAD(, NBDClient) clients;
75 QTAILQ_ENTRY(NBDExport) next;
76
77 AioContext *ctx;
78
79 BlockBackend *eject_notifier_blk;
80 Notifier eject_notifier;
81 };
82
83 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
84
85 struct NBDClient {
86 int refcount;
87 void (*close_fn)(NBDClient *client, bool negotiated);
88
89 NBDExport *exp;
90 QCryptoTLSCreds *tlscreds;
91 char *tlsaclname;
92 QIOChannelSocket *sioc; /* The underlying data channel */
93 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
94
95 Coroutine *recv_coroutine;
96
97 CoMutex send_lock;
98 Coroutine *send_coroutine;
99
100 QTAILQ_ENTRY(NBDClient) next;
101 int nb_requests;
102 bool closing;
103
104 bool structured_reply;
105
106 uint32_t opt; /* Current option being negotiated */
107 uint32_t optlen; /* remaining length of data in ioc for the option being
108 negotiated now */
109 };
110
111 static void nbd_client_receive_next_request(NBDClient *client);
112
113 /* Basic flow for negotiation
114
115 Server Client
116 Negotiate
117
118 or
119
120 Server Client
121 Negotiate #1
122 Option
123 Negotiate #2
124
125 ----
126
127 followed by
128
129 Server Client
130 Request
131 Response
132 Request
133 Response
134 ...
135 ...
136 Request (type == 2)
137
138 */
139
140 static inline void set_be_option_rep(NBDOptionReply *rep, uint32_t option,
141 uint32_t type, uint32_t length)
142 {
143 stq_be_p(&rep->magic, NBD_REP_MAGIC);
144 stl_be_p(&rep->option, option);
145 stl_be_p(&rep->type, type);
146 stl_be_p(&rep->length, length);
147 }
148
149 /* Send a reply header, including length, but no payload.
150 * Return -errno on error, 0 on success. */
151 static int nbd_negotiate_send_rep_len(NBDClient *client, uint32_t type,
152 uint32_t len, Error **errp)
153 {
154 NBDOptionReply rep;
155
156 trace_nbd_negotiate_send_rep_len(client->opt, nbd_opt_lookup(client->opt),
157 type, nbd_rep_lookup(type), len);
158
159 assert(len < NBD_MAX_BUFFER_SIZE);
160
161 set_be_option_rep(&rep, client->opt, type, len);
162 return nbd_write(client->ioc, &rep, sizeof(rep), errp);
163 }
164
165 /* Send a reply header with default 0 length.
166 * Return -errno on error, 0 on success. */
167 static int nbd_negotiate_send_rep(NBDClient *client, uint32_t type,
168 Error **errp)
169 {
170 return nbd_negotiate_send_rep_len(client, type, 0, errp);
171 }
172
173 /* Send an error reply.
174 * Return -errno on error, 0 on success. */
175 static int GCC_FMT_ATTR(4, 0)
176 nbd_negotiate_send_rep_verr(NBDClient *client, uint32_t type,
177 Error **errp, const char *fmt, va_list va)
178 {
179 char *msg;
180 int ret;
181 size_t len;
182
183 msg = g_strdup_vprintf(fmt, va);
184 len = strlen(msg);
185 assert(len < 4096);
186 trace_nbd_negotiate_send_rep_err(msg);
187 ret = nbd_negotiate_send_rep_len(client, type, len, errp);
188 if (ret < 0) {
189 goto out;
190 }
191 if (nbd_write(client->ioc, msg, len, errp) < 0) {
192 error_prepend(errp, "write failed (error message): ");
193 ret = -EIO;
194 } else {
195 ret = 0;
196 }
197
198 out:
199 g_free(msg);
200 return ret;
201 }
202
203 /* Send an error reply.
204 * Return -errno on error, 0 on success. */
205 static int GCC_FMT_ATTR(4, 5)
206 nbd_negotiate_send_rep_err(NBDClient *client, uint32_t type,
207 Error **errp, const char *fmt, ...)
208 {
209 va_list va;
210 int ret;
211
212 va_start(va, fmt);
213 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
214 va_end(va);
215 return ret;
216 }
217
218 /* Drop remainder of the current option, and send a reply with the
219 * given error type and message. Return -errno on read or write
220 * failure; or 0 if connection is still live. */
221 static int GCC_FMT_ATTR(4, 0)
222 nbd_opt_vdrop(NBDClient *client, uint32_t type, Error **errp,
223 const char *fmt, va_list va)
224 {
225 int ret = nbd_drop(client->ioc, client->optlen, errp);
226
227 client->optlen = 0;
228 if (!ret) {
229 ret = nbd_negotiate_send_rep_verr(client, type, errp, fmt, va);
230 }
231 return ret;
232 }
233
234 static int GCC_FMT_ATTR(4, 5)
235 nbd_opt_drop(NBDClient *client, uint32_t type, Error **errp,
236 const char *fmt, ...)
237 {
238 int ret;
239 va_list va;
240
241 va_start(va, fmt);
242 ret = nbd_opt_vdrop(client, type, errp, fmt, va);
243 va_end(va);
244
245 return ret;
246 }
247
248 static int GCC_FMT_ATTR(3, 4)
249 nbd_opt_invalid(NBDClient *client, Error **errp, const char *fmt, ...)
250 {
251 int ret;
252 va_list va;
253
254 va_start(va, fmt);
255 ret = nbd_opt_vdrop(client, NBD_REP_ERR_INVALID, errp, fmt, va);
256 va_end(va);
257
258 return ret;
259 }
260
261 /* Read size bytes from the unparsed payload of the current option.
262 * Return -errno on I/O error, 0 if option was completely handled by
263 * sending a reply about inconsistent lengths, or 1 on success. */
264 static int nbd_opt_read(NBDClient *client, void *buffer, size_t size,
265 Error **errp)
266 {
267 if (size > client->optlen) {
268 return nbd_opt_invalid(client, errp,
269 "Inconsistent lengths in option %s",
270 nbd_opt_lookup(client->opt));
271 }
272 client->optlen -= size;
273 return qio_channel_read_all(client->ioc, buffer, size, errp) < 0 ? -EIO : 1;
274 }
275
276 /* nbd_opt_read_name
277 *
278 * Read a string with the format:
279 * uint32_t len (<= NBD_MAX_NAME_SIZE)
280 * len bytes string (not 0-terminated)
281 *
282 * @name should be enough to store NBD_MAX_NAME_SIZE+1.
283 * If @length is non-null, it will be set to the actual string length.
284 *
285 * Return -errno on I/O error, 0 if option was completely handled by
286 * sending a reply about inconsistent lengths, or 1 on success.
287 */
288 static int nbd_opt_read_name(NBDClient *client, char *name, uint32_t *length,
289 Error **errp)
290 {
291 int ret;
292 uint32_t len;
293
294 ret = nbd_opt_read(client, &len, sizeof(len), errp);
295 if (ret <= 0) {
296 return ret;
297 }
298 cpu_to_be32s(&len);
299
300 if (len > NBD_MAX_NAME_SIZE) {
301 return nbd_opt_invalid(client, errp,
302 "Invalid name length: %" PRIu32, len);
303 }
304
305 ret = nbd_opt_read(client, name, len, errp);
306 if (ret <= 0) {
307 return ret;
308 }
309 name[len] = '\0';
310
311 if (length) {
312 *length = len;
313 }
314
315 return 1;
316 }
317
318 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
319 * Return -errno on error, 0 on success. */
320 static int nbd_negotiate_send_rep_list(NBDClient *client, NBDExport *exp,
321 Error **errp)
322 {
323 size_t name_len, desc_len;
324 uint32_t len;
325 const char *name = exp->name ? exp->name : "";
326 const char *desc = exp->description ? exp->description : "";
327 QIOChannel *ioc = client->ioc;
328 int ret;
329
330 trace_nbd_negotiate_send_rep_list(name, desc);
331 name_len = strlen(name);
332 desc_len = strlen(desc);
333 len = name_len + desc_len + sizeof(len);
334 ret = nbd_negotiate_send_rep_len(client, NBD_REP_SERVER, len, errp);
335 if (ret < 0) {
336 return ret;
337 }
338
339 len = cpu_to_be32(name_len);
340 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
341 error_prepend(errp, "write failed (name length): ");
342 return -EINVAL;
343 }
344
345 if (nbd_write(ioc, name, name_len, errp) < 0) {
346 error_prepend(errp, "write failed (name buffer): ");
347 return -EINVAL;
348 }
349
350 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
351 error_prepend(errp, "write failed (description buffer): ");
352 return -EINVAL;
353 }
354
355 return 0;
356 }
357
358 /* Process the NBD_OPT_LIST command, with a potential series of replies.
359 * Return -errno on error, 0 on success. */
360 static int nbd_negotiate_handle_list(NBDClient *client, Error **errp)
361 {
362 NBDExport *exp;
363 assert(client->opt == NBD_OPT_LIST);
364
365 /* For each export, send a NBD_REP_SERVER reply. */
366 QTAILQ_FOREACH(exp, &exports, next) {
367 if (nbd_negotiate_send_rep_list(client, exp, errp)) {
368 return -EINVAL;
369 }
370 }
371 /* Finish with a NBD_REP_ACK. */
372 return nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
373 }
374
375 /* Send a reply to NBD_OPT_EXPORT_NAME.
376 * Return -errno on error, 0 on success. */
377 static int nbd_negotiate_handle_export_name(NBDClient *client,
378 uint16_t myflags, bool no_zeroes,
379 Error **errp)
380 {
381 char name[NBD_MAX_NAME_SIZE + 1];
382 char buf[NBD_REPLY_EXPORT_NAME_SIZE] = "";
383 size_t len;
384 int ret;
385
386 /* Client sends:
387 [20 .. xx] export name (length bytes)
388 Server replies:
389 [ 0 .. 7] size
390 [ 8 .. 9] export flags
391 [10 .. 133] reserved (0) [unless no_zeroes]
392 */
393 trace_nbd_negotiate_handle_export_name();
394 if (client->optlen >= sizeof(name)) {
395 error_setg(errp, "Bad length received");
396 return -EINVAL;
397 }
398 if (nbd_read(client->ioc, name, client->optlen, errp) < 0) {
399 error_prepend(errp, "read failed: ");
400 return -EIO;
401 }
402 name[client->optlen] = '\0';
403 client->optlen = 0;
404
405 trace_nbd_negotiate_handle_export_name_request(name);
406
407 client->exp = nbd_export_find(name);
408 if (!client->exp) {
409 error_setg(errp, "export not found");
410 return -EINVAL;
411 }
412
413 trace_nbd_negotiate_new_style_size_flags(client->exp->size,
414 client->exp->nbdflags | myflags);
415 stq_be_p(buf, client->exp->size);
416 stw_be_p(buf + 8, client->exp->nbdflags | myflags);
417 len = no_zeroes ? 10 : sizeof(buf);
418 ret = nbd_write(client->ioc, buf, len, errp);
419 if (ret < 0) {
420 error_prepend(errp, "write failed: ");
421 return ret;
422 }
423
424 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
425 nbd_export_get(client->exp);
426
427 return 0;
428 }
429
430 /* Send a single NBD_REP_INFO, with a buffer @buf of @length bytes.
431 * The buffer does NOT include the info type prefix.
432 * Return -errno on error, 0 if ready to send more. */
433 static int nbd_negotiate_send_info(NBDClient *client,
434 uint16_t info, uint32_t length, void *buf,
435 Error **errp)
436 {
437 int rc;
438
439 trace_nbd_negotiate_send_info(info, nbd_info_lookup(info), length);
440 rc = nbd_negotiate_send_rep_len(client, NBD_REP_INFO,
441 sizeof(info) + length, errp);
442 if (rc < 0) {
443 return rc;
444 }
445 cpu_to_be16s(&info);
446 if (nbd_write(client->ioc, &info, sizeof(info), errp) < 0) {
447 return -EIO;
448 }
449 if (nbd_write(client->ioc, buf, length, errp) < 0) {
450 return -EIO;
451 }
452 return 0;
453 }
454
455 /* nbd_reject_length: Handle any unexpected payload.
456 * @fatal requests that we quit talking to the client, even if we are able
457 * to successfully send an error reply.
458 * Return:
459 * -errno transmission error occurred or @fatal was requested, errp is set
460 * 0 error message successfully sent to client, errp is not set
461 */
462 static int nbd_reject_length(NBDClient *client, bool fatal, Error **errp)
463 {
464 int ret;
465
466 assert(client->optlen);
467 ret = nbd_opt_invalid(client, errp, "option '%s' has unexpected length",
468 nbd_opt_lookup(client->opt));
469 if (fatal && !ret) {
470 error_setg(errp, "option '%s' has unexpected length",
471 nbd_opt_lookup(client->opt));
472 return -EINVAL;
473 }
474 return ret;
475 }
476
477 /* Handle NBD_OPT_INFO and NBD_OPT_GO.
478 * Return -errno on error, 0 if ready for next option, and 1 to move
479 * into transmission phase. */
480 static int nbd_negotiate_handle_info(NBDClient *client, uint16_t myflags,
481 Error **errp)
482 {
483 int rc;
484 char name[NBD_MAX_NAME_SIZE + 1];
485 NBDExport *exp;
486 uint16_t requests;
487 uint16_t request;
488 uint32_t namelen;
489 bool sendname = false;
490 bool blocksize = false;
491 uint32_t sizes[3];
492 char buf[sizeof(uint64_t) + sizeof(uint16_t)];
493
494 /* Client sends:
495 4 bytes: L, name length (can be 0)
496 L bytes: export name
497 2 bytes: N, number of requests (can be 0)
498 N * 2 bytes: N requests
499 */
500 rc = nbd_opt_read_name(client, name, &namelen, errp);
501 if (rc <= 0) {
502 return rc;
503 }
504 trace_nbd_negotiate_handle_export_name_request(name);
505
506 rc = nbd_opt_read(client, &requests, sizeof(requests), errp);
507 if (rc <= 0) {
508 return rc;
509 }
510 be16_to_cpus(&requests);
511 trace_nbd_negotiate_handle_info_requests(requests);
512 while (requests--) {
513 rc = nbd_opt_read(client, &request, sizeof(request), errp);
514 if (rc <= 0) {
515 return rc;
516 }
517 be16_to_cpus(&request);
518 trace_nbd_negotiate_handle_info_request(request,
519 nbd_info_lookup(request));
520 /* We care about NBD_INFO_NAME and NBD_INFO_BLOCK_SIZE;
521 * everything else is either a request we don't know or
522 * something we send regardless of request */
523 switch (request) {
524 case NBD_INFO_NAME:
525 sendname = true;
526 break;
527 case NBD_INFO_BLOCK_SIZE:
528 blocksize = true;
529 break;
530 }
531 }
532 if (client->optlen) {
533 return nbd_reject_length(client, false, errp);
534 }
535
536 exp = nbd_export_find(name);
537 if (!exp) {
538 return nbd_negotiate_send_rep_err(client, NBD_REP_ERR_UNKNOWN,
539 errp, "export '%s' not present",
540 name);
541 }
542
543 /* Don't bother sending NBD_INFO_NAME unless client requested it */
544 if (sendname) {
545 rc = nbd_negotiate_send_info(client, NBD_INFO_NAME, namelen, name,
546 errp);
547 if (rc < 0) {
548 return rc;
549 }
550 }
551
552 /* Send NBD_INFO_DESCRIPTION only if available, regardless of
553 * client request */
554 if (exp->description) {
555 size_t len = strlen(exp->description);
556
557 rc = nbd_negotiate_send_info(client, NBD_INFO_DESCRIPTION,
558 len, exp->description, errp);
559 if (rc < 0) {
560 return rc;
561 }
562 }
563
564 /* Send NBD_INFO_BLOCK_SIZE always, but tweak the minimum size
565 * according to whether the client requested it, and according to
566 * whether this is OPT_INFO or OPT_GO. */
567 /* minimum - 1 for back-compat, or 512 if client is new enough.
568 * TODO: consult blk_bs(blk)->bl.request_alignment? */
569 sizes[0] =
570 (client->opt == NBD_OPT_INFO || blocksize) ? BDRV_SECTOR_SIZE : 1;
571 /* preferred - Hard-code to 4096 for now.
572 * TODO: is blk_bs(blk)->bl.opt_transfer appropriate? */
573 sizes[1] = 4096;
574 /* maximum - At most 32M, but smaller as appropriate. */
575 sizes[2] = MIN(blk_get_max_transfer(exp->blk), NBD_MAX_BUFFER_SIZE);
576 trace_nbd_negotiate_handle_info_block_size(sizes[0], sizes[1], sizes[2]);
577 cpu_to_be32s(&sizes[0]);
578 cpu_to_be32s(&sizes[1]);
579 cpu_to_be32s(&sizes[2]);
580 rc = nbd_negotiate_send_info(client, NBD_INFO_BLOCK_SIZE,
581 sizeof(sizes), sizes, errp);
582 if (rc < 0) {
583 return rc;
584 }
585
586 /* Send NBD_INFO_EXPORT always */
587 trace_nbd_negotiate_new_style_size_flags(exp->size,
588 exp->nbdflags | myflags);
589 stq_be_p(buf, exp->size);
590 stw_be_p(buf + 8, exp->nbdflags | myflags);
591 rc = nbd_negotiate_send_info(client, NBD_INFO_EXPORT,
592 sizeof(buf), buf, errp);
593 if (rc < 0) {
594 return rc;
595 }
596
597 /* If the client is just asking for NBD_OPT_INFO, but forgot to
598 * request block sizes, return an error.
599 * TODO: consult blk_bs(blk)->request_align, and only error if it
600 * is not 1? */
601 if (client->opt == NBD_OPT_INFO && !blocksize) {
602 return nbd_negotiate_send_rep_err(client,
603 NBD_REP_ERR_BLOCK_SIZE_REQD,
604 errp,
605 "request NBD_INFO_BLOCK_SIZE to "
606 "use this export");
607 }
608
609 /* Final reply */
610 rc = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
611 if (rc < 0) {
612 return rc;
613 }
614
615 if (client->opt == NBD_OPT_GO) {
616 client->exp = exp;
617 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
618 nbd_export_get(client->exp);
619 rc = 1;
620 }
621 return rc;
622 }
623
624
625 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
626 * new channel for all further (now-encrypted) communication. */
627 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
628 Error **errp)
629 {
630 QIOChannel *ioc;
631 QIOChannelTLS *tioc;
632 struct NBDTLSHandshakeData data = { 0 };
633
634 assert(client->opt == NBD_OPT_STARTTLS);
635
636 trace_nbd_negotiate_handle_starttls();
637 ioc = client->ioc;
638
639 if (nbd_negotiate_send_rep(client, NBD_REP_ACK, errp) < 0) {
640 return NULL;
641 }
642
643 tioc = qio_channel_tls_new_server(ioc,
644 client->tlscreds,
645 client->tlsaclname,
646 errp);
647 if (!tioc) {
648 return NULL;
649 }
650
651 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
652 trace_nbd_negotiate_handle_starttls_handshake();
653 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
654 qio_channel_tls_handshake(tioc,
655 nbd_tls_handshake,
656 &data,
657 NULL,
658 NULL);
659
660 if (!data.complete) {
661 g_main_loop_run(data.loop);
662 }
663 g_main_loop_unref(data.loop);
664 if (data.error) {
665 object_unref(OBJECT(tioc));
666 error_propagate(errp, data.error);
667 return NULL;
668 }
669
670 return QIO_CHANNEL(tioc);
671 }
672
673 /* nbd_negotiate_options
674 * Process all NBD_OPT_* client option commands, during fixed newstyle
675 * negotiation.
676 * Return:
677 * -errno on error, errp is set
678 * 0 on successful negotiation, errp is not set
679 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
680 * errp is not set
681 */
682 static int nbd_negotiate_options(NBDClient *client, uint16_t myflags,
683 Error **errp)
684 {
685 uint32_t flags;
686 bool fixedNewstyle = false;
687 bool no_zeroes = false;
688
689 /* Client sends:
690 [ 0 .. 3] client flags
691
692 Then we loop until NBD_OPT_EXPORT_NAME or NBD_OPT_GO:
693 [ 0 .. 7] NBD_OPTS_MAGIC
694 [ 8 .. 11] NBD option
695 [12 .. 15] Data length
696 ... Rest of request
697
698 [ 0 .. 7] NBD_OPTS_MAGIC
699 [ 8 .. 11] Second NBD option
700 [12 .. 15] Data length
701 ... Rest of request
702 */
703
704 if (nbd_read(client->ioc, &flags, sizeof(flags), errp) < 0) {
705 error_prepend(errp, "read failed: ");
706 return -EIO;
707 }
708 be32_to_cpus(&flags);
709 trace_nbd_negotiate_options_flags(flags);
710 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
711 fixedNewstyle = true;
712 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
713 }
714 if (flags & NBD_FLAG_C_NO_ZEROES) {
715 no_zeroes = true;
716 flags &= ~NBD_FLAG_C_NO_ZEROES;
717 }
718 if (flags != 0) {
719 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
720 return -EINVAL;
721 }
722
723 while (1) {
724 int ret;
725 uint32_t option, length;
726 uint64_t magic;
727
728 if (nbd_read(client->ioc, &magic, sizeof(magic), errp) < 0) {
729 error_prepend(errp, "read failed: ");
730 return -EINVAL;
731 }
732 magic = be64_to_cpu(magic);
733 trace_nbd_negotiate_options_check_magic(magic);
734 if (magic != NBD_OPTS_MAGIC) {
735 error_setg(errp, "Bad magic received");
736 return -EINVAL;
737 }
738
739 if (nbd_read(client->ioc, &option,
740 sizeof(option), errp) < 0) {
741 error_prepend(errp, "read failed: ");
742 return -EINVAL;
743 }
744 option = be32_to_cpu(option);
745 client->opt = option;
746
747 if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) {
748 error_prepend(errp, "read failed: ");
749 return -EINVAL;
750 }
751 length = be32_to_cpu(length);
752 assert(!client->optlen);
753 client->optlen = length;
754
755 if (length > NBD_MAX_BUFFER_SIZE) {
756 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
757 length, NBD_MAX_BUFFER_SIZE);
758 return -EINVAL;
759 }
760
761 trace_nbd_negotiate_options_check_option(option,
762 nbd_opt_lookup(option));
763 if (client->tlscreds &&
764 client->ioc == (QIOChannel *)client->sioc) {
765 QIOChannel *tioc;
766 if (!fixedNewstyle) {
767 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
768 return -EINVAL;
769 }
770 switch (option) {
771 case NBD_OPT_STARTTLS:
772 if (length) {
773 /* Unconditionally drop the connection if the client
774 * can't start a TLS negotiation correctly */
775 return nbd_reject_length(client, true, errp);
776 }
777 tioc = nbd_negotiate_handle_starttls(client, errp);
778 if (!tioc) {
779 return -EIO;
780 }
781 ret = 0;
782 object_unref(OBJECT(client->ioc));
783 client->ioc = QIO_CHANNEL(tioc);
784 break;
785
786 case NBD_OPT_EXPORT_NAME:
787 /* No way to return an error to client, so drop connection */
788 error_setg(errp, "Option 0x%x not permitted before TLS",
789 option);
790 return -EINVAL;
791
792 default:
793 ret = nbd_opt_drop(client, NBD_REP_ERR_TLS_REQD, errp,
794 "Option 0x%" PRIx32
795 "not permitted before TLS", option);
796 /* Let the client keep trying, unless they asked to
797 * quit. In this mode, we've already sent an error, so
798 * we can't ack the abort. */
799 if (option == NBD_OPT_ABORT) {
800 return 1;
801 }
802 break;
803 }
804 } else if (fixedNewstyle) {
805 switch (option) {
806 case NBD_OPT_LIST:
807 if (length) {
808 ret = nbd_reject_length(client, false, errp);
809 } else {
810 ret = nbd_negotiate_handle_list(client, errp);
811 }
812 break;
813
814 case NBD_OPT_ABORT:
815 /* NBD spec says we must try to reply before
816 * disconnecting, but that we must also tolerate
817 * guests that don't wait for our reply. */
818 nbd_negotiate_send_rep(client, NBD_REP_ACK, NULL);
819 return 1;
820
821 case NBD_OPT_EXPORT_NAME:
822 return nbd_negotiate_handle_export_name(client,
823 myflags, no_zeroes,
824 errp);
825
826 case NBD_OPT_INFO:
827 case NBD_OPT_GO:
828 ret = nbd_negotiate_handle_info(client, myflags, errp);
829 if (ret == 1) {
830 assert(option == NBD_OPT_GO);
831 return 0;
832 }
833 break;
834
835 case NBD_OPT_STARTTLS:
836 if (length) {
837 ret = nbd_reject_length(client, false, errp);
838 } else if (client->tlscreds) {
839 ret = nbd_negotiate_send_rep_err(client,
840 NBD_REP_ERR_INVALID, errp,
841 "TLS already enabled");
842 } else {
843 ret = nbd_negotiate_send_rep_err(client,
844 NBD_REP_ERR_POLICY, errp,
845 "TLS not configured");
846 }
847 break;
848
849 case NBD_OPT_STRUCTURED_REPLY:
850 if (length) {
851 ret = nbd_reject_length(client, false, errp);
852 } else if (client->structured_reply) {
853 ret = nbd_negotiate_send_rep_err(
854 client, NBD_REP_ERR_INVALID, errp,
855 "structured reply already negotiated");
856 } else {
857 ret = nbd_negotiate_send_rep(client, NBD_REP_ACK, errp);
858 client->structured_reply = true;
859 myflags |= NBD_FLAG_SEND_DF;
860 }
861 break;
862
863 default:
864 ret = nbd_opt_drop(client, NBD_REP_ERR_UNSUP, errp,
865 "Unsupported option %" PRIu32 " (%s)",
866 option, nbd_opt_lookup(option));
867 break;
868 }
869 } else {
870 /*
871 * If broken new-style we should drop the connection
872 * for anything except NBD_OPT_EXPORT_NAME
873 */
874 switch (option) {
875 case NBD_OPT_EXPORT_NAME:
876 return nbd_negotiate_handle_export_name(client,
877 myflags, no_zeroes,
878 errp);
879
880 default:
881 error_setg(errp, "Unsupported option %" PRIu32 " (%s)",
882 option, nbd_opt_lookup(option));
883 return -EINVAL;
884 }
885 }
886 if (ret < 0) {
887 return ret;
888 }
889 }
890 }
891
892 /* nbd_negotiate
893 * Return:
894 * -errno on error, errp is set
895 * 0 on successful negotiation, errp is not set
896 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
897 * errp is not set
898 */
899 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
900 {
901 char buf[NBD_OLDSTYLE_NEGOTIATE_SIZE] = "";
902 int ret;
903 const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
904 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA |
905 NBD_FLAG_SEND_WRITE_ZEROES);
906 bool oldStyle;
907
908 /* Old style negotiation header, no room for options
909 [ 0 .. 7] passwd ("NBDMAGIC")
910 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
911 [16 .. 23] size
912 [24 .. 27] export flags (zero-extended)
913 [28 .. 151] reserved (0)
914
915 New style negotiation header, client can send options
916 [ 0 .. 7] passwd ("NBDMAGIC")
917 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
918 [16 .. 17] server flags (0)
919 ....options sent, ending in NBD_OPT_EXPORT_NAME or NBD_OPT_GO....
920 */
921
922 qio_channel_set_blocking(client->ioc, false, NULL);
923
924 trace_nbd_negotiate_begin();
925 memcpy(buf, "NBDMAGIC", 8);
926
927 oldStyle = client->exp != NULL && !client->tlscreds;
928 if (oldStyle) {
929 trace_nbd_negotiate_old_style(client->exp->size,
930 client->exp->nbdflags | myflags);
931 stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
932 stq_be_p(buf + 16, client->exp->size);
933 stl_be_p(buf + 24, client->exp->nbdflags | myflags);
934
935 if (nbd_write(client->ioc, buf, sizeof(buf), errp) < 0) {
936 error_prepend(errp, "write failed: ");
937 return -EINVAL;
938 }
939 } else {
940 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
941 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
942
943 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
944 error_prepend(errp, "write failed: ");
945 return -EINVAL;
946 }
947 ret = nbd_negotiate_options(client, myflags, errp);
948 if (ret != 0) {
949 if (ret < 0) {
950 error_prepend(errp, "option negotiation failed: ");
951 }
952 return ret;
953 }
954 }
955
956 assert(!client->optlen);
957 trace_nbd_negotiate_success();
958
959 return 0;
960 }
961
962 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
963 Error **errp)
964 {
965 uint8_t buf[NBD_REQUEST_SIZE];
966 uint32_t magic;
967 int ret;
968
969 ret = nbd_read(ioc, buf, sizeof(buf), errp);
970 if (ret < 0) {
971 return ret;
972 }
973
974 /* Request
975 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
976 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
977 [ 6 .. 7] type (NBD_CMD_READ, ...)
978 [ 8 .. 15] handle
979 [16 .. 23] from
980 [24 .. 27] len
981 */
982
983 magic = ldl_be_p(buf);
984 request->flags = lduw_be_p(buf + 4);
985 request->type = lduw_be_p(buf + 6);
986 request->handle = ldq_be_p(buf + 8);
987 request->from = ldq_be_p(buf + 16);
988 request->len = ldl_be_p(buf + 24);
989
990 trace_nbd_receive_request(magic, request->flags, request->type,
991 request->from, request->len);
992
993 if (magic != NBD_REQUEST_MAGIC) {
994 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
995 return -EINVAL;
996 }
997 return 0;
998 }
999
1000 #define MAX_NBD_REQUESTS 16
1001
1002 void nbd_client_get(NBDClient *client)
1003 {
1004 client->refcount++;
1005 }
1006
1007 void nbd_client_put(NBDClient *client)
1008 {
1009 if (--client->refcount == 0) {
1010 /* The last reference should be dropped by client->close,
1011 * which is called by client_close.
1012 */
1013 assert(client->closing);
1014
1015 qio_channel_detach_aio_context(client->ioc);
1016 object_unref(OBJECT(client->sioc));
1017 object_unref(OBJECT(client->ioc));
1018 if (client->tlscreds) {
1019 object_unref(OBJECT(client->tlscreds));
1020 }
1021 g_free(client->tlsaclname);
1022 if (client->exp) {
1023 QTAILQ_REMOVE(&client->exp->clients, client, next);
1024 nbd_export_put(client->exp);
1025 }
1026 g_free(client);
1027 }
1028 }
1029
1030 static void client_close(NBDClient *client, bool negotiated)
1031 {
1032 if (client->closing) {
1033 return;
1034 }
1035
1036 client->closing = true;
1037
1038 /* Force requests to finish. They will drop their own references,
1039 * then we'll close the socket and free the NBDClient.
1040 */
1041 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
1042 NULL);
1043
1044 /* Also tell the client, so that they release their reference. */
1045 if (client->close_fn) {
1046 client->close_fn(client, negotiated);
1047 }
1048 }
1049
1050 static NBDRequestData *nbd_request_get(NBDClient *client)
1051 {
1052 NBDRequestData *req;
1053
1054 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1055 client->nb_requests++;
1056
1057 req = g_new0(NBDRequestData, 1);
1058 nbd_client_get(client);
1059 req->client = client;
1060 return req;
1061 }
1062
1063 static void nbd_request_put(NBDRequestData *req)
1064 {
1065 NBDClient *client = req->client;
1066
1067 if (req->data) {
1068 qemu_vfree(req->data);
1069 }
1070 g_free(req);
1071
1072 client->nb_requests--;
1073 nbd_client_receive_next_request(client);
1074
1075 nbd_client_put(client);
1076 }
1077
1078 static void blk_aio_attached(AioContext *ctx, void *opaque)
1079 {
1080 NBDExport *exp = opaque;
1081 NBDClient *client;
1082
1083 trace_nbd_blk_aio_attached(exp->name, ctx);
1084
1085 exp->ctx = ctx;
1086
1087 QTAILQ_FOREACH(client, &exp->clients, next) {
1088 qio_channel_attach_aio_context(client->ioc, ctx);
1089 if (client->recv_coroutine) {
1090 aio_co_schedule(ctx, client->recv_coroutine);
1091 }
1092 if (client->send_coroutine) {
1093 aio_co_schedule(ctx, client->send_coroutine);
1094 }
1095 }
1096 }
1097
1098 static void blk_aio_detach(void *opaque)
1099 {
1100 NBDExport *exp = opaque;
1101 NBDClient *client;
1102
1103 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
1104
1105 QTAILQ_FOREACH(client, &exp->clients, next) {
1106 qio_channel_detach_aio_context(client->ioc);
1107 }
1108
1109 exp->ctx = NULL;
1110 }
1111
1112 static void nbd_eject_notifier(Notifier *n, void *data)
1113 {
1114 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
1115 nbd_export_close(exp);
1116 }
1117
1118 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
1119 uint16_t nbdflags, void (*close)(NBDExport *),
1120 bool writethrough, BlockBackend *on_eject_blk,
1121 Error **errp)
1122 {
1123 AioContext *ctx;
1124 BlockBackend *blk;
1125 NBDExport *exp = g_new0(NBDExport, 1);
1126 uint64_t perm;
1127 int ret;
1128
1129 /*
1130 * NBD exports are used for non-shared storage migration. Make sure
1131 * that BDRV_O_INACTIVE is cleared and the image is ready for write
1132 * access since the export could be available before migration handover.
1133 */
1134 ctx = bdrv_get_aio_context(bs);
1135 aio_context_acquire(ctx);
1136 bdrv_invalidate_cache(bs, NULL);
1137 aio_context_release(ctx);
1138
1139 /* Don't allow resize while the NBD server is running, otherwise we don't
1140 * care what happens with the node. */
1141 perm = BLK_PERM_CONSISTENT_READ;
1142 if ((nbdflags & NBD_FLAG_READ_ONLY) == 0) {
1143 perm |= BLK_PERM_WRITE;
1144 }
1145 blk = blk_new(perm, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
1146 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
1147 ret = blk_insert_bs(blk, bs, errp);
1148 if (ret < 0) {
1149 goto fail;
1150 }
1151 blk_set_enable_write_cache(blk, !writethrough);
1152
1153 exp->refcount = 1;
1154 QTAILQ_INIT(&exp->clients);
1155 exp->blk = blk;
1156 exp->dev_offset = dev_offset;
1157 exp->nbdflags = nbdflags;
1158 exp->size = size < 0 ? blk_getlength(blk) : size;
1159 if (exp->size < 0) {
1160 error_setg_errno(errp, -exp->size,
1161 "Failed to determine the NBD export's length");
1162 goto fail;
1163 }
1164 exp->size -= exp->size % BDRV_SECTOR_SIZE;
1165
1166 exp->close = close;
1167 exp->ctx = blk_get_aio_context(blk);
1168 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
1169
1170 if (on_eject_blk) {
1171 blk_ref(on_eject_blk);
1172 exp->eject_notifier_blk = on_eject_blk;
1173 exp->eject_notifier.notify = nbd_eject_notifier;
1174 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
1175 }
1176 return exp;
1177
1178 fail:
1179 blk_unref(blk);
1180 g_free(exp);
1181 return NULL;
1182 }
1183
1184 NBDExport *nbd_export_find(const char *name)
1185 {
1186 NBDExport *exp;
1187 QTAILQ_FOREACH(exp, &exports, next) {
1188 if (strcmp(name, exp->name) == 0) {
1189 return exp;
1190 }
1191 }
1192
1193 return NULL;
1194 }
1195
1196 void nbd_export_set_name(NBDExport *exp, const char *name)
1197 {
1198 if (exp->name == name) {
1199 return;
1200 }
1201
1202 nbd_export_get(exp);
1203 if (exp->name != NULL) {
1204 g_free(exp->name);
1205 exp->name = NULL;
1206 QTAILQ_REMOVE(&exports, exp, next);
1207 nbd_export_put(exp);
1208 }
1209 if (name != NULL) {
1210 nbd_export_get(exp);
1211 exp->name = g_strdup(name);
1212 QTAILQ_INSERT_TAIL(&exports, exp, next);
1213 }
1214 nbd_export_put(exp);
1215 }
1216
1217 void nbd_export_set_description(NBDExport *exp, const char *description)
1218 {
1219 g_free(exp->description);
1220 exp->description = g_strdup(description);
1221 }
1222
1223 void nbd_export_close(NBDExport *exp)
1224 {
1225 NBDClient *client, *next;
1226
1227 nbd_export_get(exp);
1228 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
1229 client_close(client, true);
1230 }
1231 nbd_export_set_name(exp, NULL);
1232 nbd_export_set_description(exp, NULL);
1233 nbd_export_put(exp);
1234 }
1235
1236 void nbd_export_remove(NBDExport *exp, NbdServerRemoveMode mode, Error **errp)
1237 {
1238 if (mode == NBD_SERVER_REMOVE_MODE_HARD || QTAILQ_EMPTY(&exp->clients)) {
1239 nbd_export_close(exp);
1240 return;
1241 }
1242
1243 assert(mode == NBD_SERVER_REMOVE_MODE_SAFE);
1244
1245 error_setg(errp, "export '%s' still in use", exp->name);
1246 error_append_hint(errp, "Use mode='hard' to force client disconnect\n");
1247 }
1248
1249 void nbd_export_get(NBDExport *exp)
1250 {
1251 assert(exp->refcount > 0);
1252 exp->refcount++;
1253 }
1254
1255 void nbd_export_put(NBDExport *exp)
1256 {
1257 assert(exp->refcount > 0);
1258 if (exp->refcount == 1) {
1259 nbd_export_close(exp);
1260 }
1261
1262 /* nbd_export_close() may theoretically reduce refcount to 0. It may happen
1263 * if someone calls nbd_export_put() on named export not through
1264 * nbd_export_set_name() when refcount is 1. So, let's assert that
1265 * it is > 0.
1266 */
1267 assert(exp->refcount > 0);
1268 if (--exp->refcount == 0) {
1269 assert(exp->name == NULL);
1270 assert(exp->description == NULL);
1271
1272 if (exp->close) {
1273 exp->close(exp);
1274 }
1275
1276 if (exp->blk) {
1277 if (exp->eject_notifier_blk) {
1278 notifier_remove(&exp->eject_notifier);
1279 blk_unref(exp->eject_notifier_blk);
1280 }
1281 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1282 blk_aio_detach, exp);
1283 blk_unref(exp->blk);
1284 exp->blk = NULL;
1285 }
1286
1287 g_free(exp);
1288 }
1289 }
1290
1291 BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
1292 {
1293 return exp->blk;
1294 }
1295
1296 void nbd_export_close_all(void)
1297 {
1298 NBDExport *exp, *next;
1299
1300 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1301 nbd_export_close(exp);
1302 }
1303 }
1304
1305 static int coroutine_fn nbd_co_send_iov(NBDClient *client, struct iovec *iov,
1306 unsigned niov, Error **errp)
1307 {
1308 int ret;
1309
1310 g_assert(qemu_in_coroutine());
1311 qemu_co_mutex_lock(&client->send_lock);
1312 client->send_coroutine = qemu_coroutine_self();
1313
1314 ret = qio_channel_writev_all(client->ioc, iov, niov, errp) < 0 ? -EIO : 0;
1315
1316 client->send_coroutine = NULL;
1317 qemu_co_mutex_unlock(&client->send_lock);
1318
1319 return ret;
1320 }
1321
1322 static inline void set_be_simple_reply(NBDSimpleReply *reply, uint64_t error,
1323 uint64_t handle)
1324 {
1325 stl_be_p(&reply->magic, NBD_SIMPLE_REPLY_MAGIC);
1326 stl_be_p(&reply->error, error);
1327 stq_be_p(&reply->handle, handle);
1328 }
1329
1330 static int nbd_co_send_simple_reply(NBDClient *client,
1331 uint64_t handle,
1332 uint32_t error,
1333 void *data,
1334 size_t len,
1335 Error **errp)
1336 {
1337 NBDSimpleReply reply;
1338 int nbd_err = system_errno_to_nbd_errno(error);
1339 struct iovec iov[] = {
1340 {.iov_base = &reply, .iov_len = sizeof(reply)},
1341 {.iov_base = data, .iov_len = len}
1342 };
1343
1344 trace_nbd_co_send_simple_reply(handle, nbd_err, nbd_err_lookup(nbd_err),
1345 len);
1346 set_be_simple_reply(&reply, nbd_err, handle);
1347
1348 return nbd_co_send_iov(client, iov, len ? 2 : 1, errp);
1349 }
1350
1351 static inline void set_be_chunk(NBDStructuredReplyChunk *chunk, uint16_t flags,
1352 uint16_t type, uint64_t handle, uint32_t length)
1353 {
1354 stl_be_p(&chunk->magic, NBD_STRUCTURED_REPLY_MAGIC);
1355 stw_be_p(&chunk->flags, flags);
1356 stw_be_p(&chunk->type, type);
1357 stq_be_p(&chunk->handle, handle);
1358 stl_be_p(&chunk->length, length);
1359 }
1360
1361 static int coroutine_fn nbd_co_send_structured_done(NBDClient *client,
1362 uint64_t handle,
1363 Error **errp)
1364 {
1365 NBDStructuredReplyChunk chunk;
1366 struct iovec iov[] = {
1367 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1368 };
1369
1370 trace_nbd_co_send_structured_done(handle);
1371 set_be_chunk(&chunk, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_NONE, handle, 0);
1372
1373 return nbd_co_send_iov(client, iov, 1, errp);
1374 }
1375
1376 static int coroutine_fn nbd_co_send_structured_read(NBDClient *client,
1377 uint64_t handle,
1378 uint64_t offset,
1379 void *data,
1380 size_t size,
1381 bool final,
1382 Error **errp)
1383 {
1384 NBDStructuredReadData chunk;
1385 struct iovec iov[] = {
1386 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1387 {.iov_base = data, .iov_len = size}
1388 };
1389
1390 assert(size);
1391 trace_nbd_co_send_structured_read(handle, offset, data, size);
1392 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1393 NBD_REPLY_TYPE_OFFSET_DATA, handle,
1394 sizeof(chunk) - sizeof(chunk.h) + size);
1395 stq_be_p(&chunk.offset, offset);
1396
1397 return nbd_co_send_iov(client, iov, 2, errp);
1398 }
1399
1400 static int coroutine_fn nbd_co_send_structured_error(NBDClient *client,
1401 uint64_t handle,
1402 uint32_t error,
1403 const char *msg,
1404 Error **errp)
1405 {
1406 NBDStructuredError chunk;
1407 int nbd_err = system_errno_to_nbd_errno(error);
1408 struct iovec iov[] = {
1409 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1410 {.iov_base = (char *)msg, .iov_len = msg ? strlen(msg) : 0},
1411 };
1412
1413 assert(nbd_err);
1414 trace_nbd_co_send_structured_error(handle, nbd_err,
1415 nbd_err_lookup(nbd_err), msg ? msg : "");
1416 set_be_chunk(&chunk.h, NBD_REPLY_FLAG_DONE, NBD_REPLY_TYPE_ERROR, handle,
1417 sizeof(chunk) - sizeof(chunk.h) + iov[1].iov_len);
1418 stl_be_p(&chunk.error, nbd_err);
1419 stw_be_p(&chunk.message_length, iov[1].iov_len);
1420
1421 return nbd_co_send_iov(client, iov, 1 + !!iov[1].iov_len, errp);
1422 }
1423
1424 /* Do a sparse read and send the structured reply to the client.
1425 * Returns -errno if sending fails. bdrv_block_status_above() failure is
1426 * reported to the client, at which point this function succeeds.
1427 */
1428 static int coroutine_fn nbd_co_send_sparse_read(NBDClient *client,
1429 uint64_t handle,
1430 uint64_t offset,
1431 uint8_t *data,
1432 size_t size,
1433 Error **errp)
1434 {
1435 int ret = 0;
1436 NBDExport *exp = client->exp;
1437 size_t progress = 0;
1438
1439 while (progress < size) {
1440 int64_t pnum;
1441 int status = bdrv_block_status_above(blk_bs(exp->blk), NULL,
1442 offset + progress,
1443 size - progress, &pnum, NULL,
1444 NULL);
1445 bool final;
1446
1447 if (status < 0) {
1448 char *msg = g_strdup_printf("unable to check for holes: %s",
1449 strerror(-status));
1450
1451 ret = nbd_co_send_structured_error(client, handle, -status, msg,
1452 errp);
1453 g_free(msg);
1454 return ret;
1455 }
1456 assert(pnum && pnum <= size - progress);
1457 final = progress + pnum == size;
1458 if (status & BDRV_BLOCK_ZERO) {
1459 NBDStructuredReadHole chunk;
1460 struct iovec iov[] = {
1461 {.iov_base = &chunk, .iov_len = sizeof(chunk)},
1462 };
1463
1464 trace_nbd_co_send_structured_read_hole(handle, offset + progress,
1465 pnum);
1466 set_be_chunk(&chunk.h, final ? NBD_REPLY_FLAG_DONE : 0,
1467 NBD_REPLY_TYPE_OFFSET_HOLE,
1468 handle, sizeof(chunk) - sizeof(chunk.h));
1469 stq_be_p(&chunk.offset, offset + progress);
1470 stl_be_p(&chunk.length, pnum);
1471 ret = nbd_co_send_iov(client, iov, 1, errp);
1472 } else {
1473 ret = blk_pread(exp->blk, offset + progress + exp->dev_offset,
1474 data + progress, pnum);
1475 if (ret < 0) {
1476 error_setg_errno(errp, -ret, "reading from file failed");
1477 break;
1478 }
1479 ret = nbd_co_send_structured_read(client, handle, offset + progress,
1480 data + progress, pnum, final,
1481 errp);
1482 }
1483
1484 if (ret < 0) {
1485 break;
1486 }
1487 progress += pnum;
1488 }
1489 return ret;
1490 }
1491
1492 /* nbd_co_receive_request
1493 * Collect a client request. Return 0 if request looks valid, -EIO to drop
1494 * connection right away, and any other negative value to report an error to
1495 * the client (although the caller may still need to disconnect after reporting
1496 * the error).
1497 */
1498 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
1499 Error **errp)
1500 {
1501 NBDClient *client = req->client;
1502 int valid_flags;
1503
1504 g_assert(qemu_in_coroutine());
1505 assert(client->recv_coroutine == qemu_coroutine_self());
1506 if (nbd_receive_request(client->ioc, request, errp) < 0) {
1507 return -EIO;
1508 }
1509
1510 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
1511 nbd_cmd_lookup(request->type));
1512
1513 if (request->type != NBD_CMD_WRITE) {
1514 /* No payload, we are ready to read the next request. */
1515 req->complete = true;
1516 }
1517
1518 if (request->type == NBD_CMD_DISC) {
1519 /* Special case: we're going to disconnect without a reply,
1520 * whether or not flags, from, or len are bogus */
1521 return -EIO;
1522 }
1523
1524 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) {
1525 if (request->len > NBD_MAX_BUFFER_SIZE) {
1526 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1527 request->len, NBD_MAX_BUFFER_SIZE);
1528 return -EINVAL;
1529 }
1530
1531 req->data = blk_try_blockalign(client->exp->blk, request->len);
1532 if (req->data == NULL) {
1533 error_setg(errp, "No memory");
1534 return -ENOMEM;
1535 }
1536 }
1537 if (request->type == NBD_CMD_WRITE) {
1538 if (nbd_read(client->ioc, req->data, request->len, errp) < 0) {
1539 error_prepend(errp, "reading from socket failed: ");
1540 return -EIO;
1541 }
1542 req->complete = true;
1543
1544 trace_nbd_co_receive_request_payload_received(request->handle,
1545 request->len);
1546 }
1547
1548 /* Sanity checks. */
1549 if (client->exp->nbdflags & NBD_FLAG_READ_ONLY &&
1550 (request->type == NBD_CMD_WRITE ||
1551 request->type == NBD_CMD_WRITE_ZEROES ||
1552 request->type == NBD_CMD_TRIM)) {
1553 error_setg(errp, "Export is read-only");
1554 return -EROFS;
1555 }
1556 if (request->from > client->exp->size ||
1557 request->from + request->len > client->exp->size) {
1558 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
1559 ", Size: %" PRIu64, request->from, request->len,
1560 (uint64_t)client->exp->size);
1561 return (request->type == NBD_CMD_WRITE ||
1562 request->type == NBD_CMD_WRITE_ZEROES) ? -ENOSPC : -EINVAL;
1563 }
1564 valid_flags = NBD_CMD_FLAG_FUA;
1565 if (request->type == NBD_CMD_READ && client->structured_reply) {
1566 valid_flags |= NBD_CMD_FLAG_DF;
1567 } else if (request->type == NBD_CMD_WRITE_ZEROES) {
1568 valid_flags |= NBD_CMD_FLAG_NO_HOLE;
1569 }
1570 if (request->flags & ~valid_flags) {
1571 error_setg(errp, "unsupported flags for command %s (got 0x%x)",
1572 nbd_cmd_lookup(request->type), request->flags);
1573 return -EINVAL;
1574 }
1575
1576 return 0;
1577 }
1578
1579 /* Send simple reply without a payload, or a structured error
1580 * @error_msg is ignored if @ret >= 0
1581 * Returns 0 if connection is still live, -errno on failure to talk to client
1582 */
1583 static coroutine_fn int nbd_send_generic_reply(NBDClient *client,
1584 uint64_t handle,
1585 int ret,
1586 const char *error_msg,
1587 Error **errp)
1588 {
1589 if (client->structured_reply && ret < 0) {
1590 return nbd_co_send_structured_error(client, handle, -ret, error_msg,
1591 errp);
1592 } else {
1593 return nbd_co_send_simple_reply(client, handle, ret < 0 ? -ret : 0,
1594 NULL, 0, errp);
1595 }
1596 }
1597
1598 /* Handle NBD_CMD_READ request.
1599 * Return -errno if sending fails. Other errors are reported directly to the
1600 * client as an error reply. */
1601 static coroutine_fn int nbd_do_cmd_read(NBDClient *client, NBDRequest *request,
1602 uint8_t *data, Error **errp)
1603 {
1604 int ret;
1605 NBDExport *exp = client->exp;
1606
1607 assert(request->type == NBD_CMD_READ);
1608
1609 /* XXX: NBD Protocol only documents use of FUA with WRITE */
1610 if (request->flags & NBD_CMD_FLAG_FUA) {
1611 ret = blk_co_flush(exp->blk);
1612 if (ret < 0) {
1613 return nbd_send_generic_reply(client, request->handle, ret,
1614 "flush failed", errp);
1615 }
1616 }
1617
1618 if (client->structured_reply && !(request->flags & NBD_CMD_FLAG_DF) &&
1619 request->len) {
1620 return nbd_co_send_sparse_read(client, request->handle, request->from,
1621 data, request->len, errp);
1622 }
1623
1624 ret = blk_pread(exp->blk, request->from + exp->dev_offset, data,
1625 request->len);
1626 if (ret < 0) {
1627 return nbd_send_generic_reply(client, request->handle, ret,
1628 "reading from file failed", errp);
1629 }
1630
1631 if (client->structured_reply) {
1632 if (request->len) {
1633 return nbd_co_send_structured_read(client, request->handle,
1634 request->from, data,
1635 request->len, true, errp);
1636 } else {
1637 return nbd_co_send_structured_done(client, request->handle, errp);
1638 }
1639 } else {
1640 return nbd_co_send_simple_reply(client, request->handle, 0,
1641 data, request->len, errp);
1642 }
1643 }
1644
1645 /* Handle NBD request.
1646 * Return -errno if sending fails. Other errors are reported directly to the
1647 * client as an error reply. */
1648 static coroutine_fn int nbd_handle_request(NBDClient *client,
1649 NBDRequest *request,
1650 uint8_t *data, Error **errp)
1651 {
1652 int ret;
1653 int flags;
1654 NBDExport *exp = client->exp;
1655 char *msg;
1656
1657 switch (request->type) {
1658 case NBD_CMD_READ:
1659 return nbd_do_cmd_read(client, request, data, errp);
1660
1661 case NBD_CMD_WRITE:
1662 flags = 0;
1663 if (request->flags & NBD_CMD_FLAG_FUA) {
1664 flags |= BDRV_REQ_FUA;
1665 }
1666 ret = blk_pwrite(exp->blk, request->from + exp->dev_offset,
1667 data, request->len, flags);
1668 return nbd_send_generic_reply(client, request->handle, ret,
1669 "writing to file failed", errp);
1670
1671 case NBD_CMD_WRITE_ZEROES:
1672 flags = 0;
1673 if (request->flags & NBD_CMD_FLAG_FUA) {
1674 flags |= BDRV_REQ_FUA;
1675 }
1676 if (!(request->flags & NBD_CMD_FLAG_NO_HOLE)) {
1677 flags |= BDRV_REQ_MAY_UNMAP;
1678 }
1679 ret = blk_pwrite_zeroes(exp->blk, request->from + exp->dev_offset,
1680 request->len, flags);
1681 return nbd_send_generic_reply(client, request->handle, ret,
1682 "writing to file failed", errp);
1683
1684 case NBD_CMD_DISC:
1685 /* unreachable, thanks to special case in nbd_co_receive_request() */
1686 abort();
1687
1688 case NBD_CMD_FLUSH:
1689 ret = blk_co_flush(exp->blk);
1690 return nbd_send_generic_reply(client, request->handle, ret,
1691 "flush failed", errp);
1692
1693 case NBD_CMD_TRIM:
1694 ret = blk_co_pdiscard(exp->blk, request->from + exp->dev_offset,
1695 request->len);
1696 if (ret == 0 && request->flags & NBD_CMD_FLAG_FUA) {
1697 ret = blk_co_flush(exp->blk);
1698 }
1699 return nbd_send_generic_reply(client, request->handle, ret,
1700 "discard failed", errp);
1701
1702 default:
1703 msg = g_strdup_printf("invalid request type (%" PRIu32 ") received",
1704 request->type);
1705 ret = nbd_send_generic_reply(client, request->handle, -EINVAL, msg,
1706 errp);
1707 g_free(msg);
1708 return ret;
1709 }
1710 }
1711
1712 /* Owns a reference to the NBDClient passed as opaque. */
1713 static coroutine_fn void nbd_trip(void *opaque)
1714 {
1715 NBDClient *client = opaque;
1716 NBDRequestData *req;
1717 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
1718 int ret;
1719 Error *local_err = NULL;
1720
1721 trace_nbd_trip();
1722 if (client->closing) {
1723 nbd_client_put(client);
1724 return;
1725 }
1726
1727 req = nbd_request_get(client);
1728 ret = nbd_co_receive_request(req, &request, &local_err);
1729 client->recv_coroutine = NULL;
1730
1731 if (client->closing) {
1732 /*
1733 * The client may be closed when we are blocked in
1734 * nbd_co_receive_request()
1735 */
1736 goto done;
1737 }
1738
1739 nbd_client_receive_next_request(client);
1740 if (ret == -EIO) {
1741 goto disconnect;
1742 }
1743
1744 if (ret < 0) {
1745 /* It wans't -EIO, so, according to nbd_co_receive_request()
1746 * semantics, we should return the error to the client. */
1747 Error *export_err = local_err;
1748
1749 local_err = NULL;
1750 ret = nbd_send_generic_reply(client, request.handle, -EINVAL,
1751 error_get_pretty(export_err), &local_err);
1752 error_free(export_err);
1753 } else {
1754 ret = nbd_handle_request(client, &request, req->data, &local_err);
1755 }
1756 if (ret < 0) {
1757 error_prepend(&local_err, "Failed to send reply: ");
1758 goto disconnect;
1759 }
1760
1761 /* We must disconnect after NBD_CMD_WRITE if we did not
1762 * read the payload.
1763 */
1764 if (!req->complete) {
1765 error_setg(&local_err, "Request handling failed in intermediate state");
1766 goto disconnect;
1767 }
1768
1769 done:
1770 nbd_request_put(req);
1771 nbd_client_put(client);
1772 return;
1773
1774 disconnect:
1775 if (local_err) {
1776 error_reportf_err(local_err, "Disconnect client, due to: ");
1777 }
1778 nbd_request_put(req);
1779 client_close(client, true);
1780 nbd_client_put(client);
1781 }
1782
1783 static void nbd_client_receive_next_request(NBDClient *client)
1784 {
1785 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
1786 nbd_client_get(client);
1787 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
1788 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
1789 }
1790 }
1791
1792 static coroutine_fn void nbd_co_client_start(void *opaque)
1793 {
1794 NBDClient *client = opaque;
1795 NBDExport *exp = client->exp;
1796 Error *local_err = NULL;
1797
1798 if (exp) {
1799 nbd_export_get(exp);
1800 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1801 }
1802 qemu_co_mutex_init(&client->send_lock);
1803
1804 if (nbd_negotiate(client, &local_err)) {
1805 if (local_err) {
1806 error_report_err(local_err);
1807 }
1808 client_close(client, false);
1809 return;
1810 }
1811
1812 nbd_client_receive_next_request(client);
1813 }
1814
1815 /*
1816 * Create a new client listener on the given export @exp, using the
1817 * given channel @sioc. Begin servicing it in a coroutine. When the
1818 * connection closes, call @close_fn with an indication of whether the
1819 * client completed negotiation.
1820 */
1821 void nbd_client_new(NBDExport *exp,
1822 QIOChannelSocket *sioc,
1823 QCryptoTLSCreds *tlscreds,
1824 const char *tlsaclname,
1825 void (*close_fn)(NBDClient *, bool))
1826 {
1827 NBDClient *client;
1828 Coroutine *co;
1829
1830 client = g_new0(NBDClient, 1);
1831 client->refcount = 1;
1832 client->exp = exp;
1833 client->tlscreds = tlscreds;
1834 if (tlscreds) {
1835 object_ref(OBJECT(client->tlscreds));
1836 }
1837 client->tlsaclname = g_strdup(tlsaclname);
1838 client->sioc = sioc;
1839 object_ref(OBJECT(client->sioc));
1840 client->ioc = QIO_CHANNEL(sioc);
1841 object_ref(OBJECT(client->ioc));
1842 client->close_fn = close_fn;
1843
1844 co = qemu_coroutine_create(nbd_co_client_start, client);
1845 qemu_coroutine_enter(co);
1846 }