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