nbd: Expose and debug more NBD constants
[qemu/ar7.git] / nbd / server.c
blob27a0aabe3fbaff99fd6d7009828dbfd00a43e258
1 /*
2 * Copyright (C) 2016-2017 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Server Side
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "trace.h"
23 #include "nbd-internal.h"
25 static int system_errno_to_nbd_errno(int err)
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 ESHUTDOWN:
44 return NBD_ESHUTDOWN;
45 case EINVAL:
46 default:
47 return NBD_EINVAL;
51 /* Definitions for opaque data types */
53 typedef struct NBDRequestData NBDRequestData;
55 struct NBDRequestData {
56 QSIMPLEQ_ENTRY(NBDRequestData) entry;
57 NBDClient *client;
58 uint8_t *data;
59 bool complete;
62 struct NBDExport {
63 int refcount;
64 void (*close)(NBDExport *exp);
66 BlockBackend *blk;
67 char *name;
68 char *description;
69 off_t dev_offset;
70 off_t size;
71 uint16_t nbdflags;
72 QTAILQ_HEAD(, NBDClient) clients;
73 QTAILQ_ENTRY(NBDExport) next;
75 AioContext *ctx;
77 BlockBackend *eject_notifier_blk;
78 Notifier eject_notifier;
81 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
83 struct NBDClient {
84 int refcount;
85 void (*close_fn)(NBDClient *client, bool negotiated);
87 bool no_zeroes;
88 NBDExport *exp;
89 QCryptoTLSCreds *tlscreds;
90 char *tlsaclname;
91 QIOChannelSocket *sioc; /* The underlying data channel */
92 QIOChannel *ioc; /* The current I/O channel which may differ (eg TLS) */
94 Coroutine *recv_coroutine;
96 CoMutex send_lock;
97 Coroutine *send_coroutine;
99 QTAILQ_ENTRY(NBDClient) next;
100 int nb_requests;
101 bool closing;
104 /* That's all folks */
106 static void nbd_client_receive_next_request(NBDClient *client);
108 /* Basic flow for negotiation
110 Server Client
111 Negotiate
115 Server Client
116 Negotiate #1
117 Option
118 Negotiate #2
120 ----
122 followed by
124 Server Client
125 Request
126 Response
127 Request
128 Response
131 Request (type == 2)
135 /* Send a reply header, including length, but no payload.
136 * Return -errno on error, 0 on success. */
137 static int nbd_negotiate_send_rep_len(QIOChannel *ioc, uint32_t type,
138 uint32_t opt, uint32_t len, Error **errp)
140 uint64_t magic;
142 trace_nbd_negotiate_send_rep_len(opt, nbd_opt_lookup(opt),
143 type, nbd_rep_lookup(type), len);
145 magic = cpu_to_be64(NBD_REP_MAGIC);
146 if (nbd_write(ioc, &magic, sizeof(magic), errp) < 0) {
147 error_prepend(errp, "write failed (rep magic): ");
148 return -EINVAL;
151 opt = cpu_to_be32(opt);
152 if (nbd_write(ioc, &opt, sizeof(opt), errp) < 0) {
153 error_prepend(errp, "write failed (rep opt): ");
154 return -EINVAL;
157 type = cpu_to_be32(type);
158 if (nbd_write(ioc, &type, sizeof(type), errp) < 0) {
159 error_prepend(errp, "write failed (rep type): ");
160 return -EINVAL;
163 len = cpu_to_be32(len);
164 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
165 error_prepend(errp, "write failed (rep data length): ");
166 return -EINVAL;
168 return 0;
171 /* Send a reply header with default 0 length.
172 * Return -errno on error, 0 on success. */
173 static int nbd_negotiate_send_rep(QIOChannel *ioc, uint32_t type, uint32_t opt,
174 Error **errp)
176 return nbd_negotiate_send_rep_len(ioc, type, opt, 0, errp);
179 /* Send an error reply.
180 * Return -errno on error, 0 on success. */
181 static int GCC_FMT_ATTR(5, 6)
182 nbd_negotiate_send_rep_err(QIOChannel *ioc, uint32_t type,
183 uint32_t opt, Error **errp, const char *fmt, ...)
185 va_list va;
186 char *msg;
187 int ret;
188 size_t len;
190 va_start(va, fmt);
191 msg = g_strdup_vprintf(fmt, va);
192 va_end(va);
193 len = strlen(msg);
194 assert(len < 4096);
195 trace_nbd_negotiate_send_rep_err(msg);
196 ret = nbd_negotiate_send_rep_len(ioc, type, opt, len, errp);
197 if (ret < 0) {
198 goto out;
200 if (nbd_write(ioc, msg, len, errp) < 0) {
201 error_prepend(errp, "write failed (error message): ");
202 ret = -EIO;
203 } else {
204 ret = 0;
207 out:
208 g_free(msg);
209 return ret;
212 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
213 * Return -errno on error, 0 on success. */
214 static int nbd_negotiate_send_rep_list(QIOChannel *ioc, NBDExport *exp,
215 Error **errp)
217 size_t name_len, desc_len;
218 uint32_t len;
219 const char *name = exp->name ? exp->name : "";
220 const char *desc = exp->description ? exp->description : "";
221 int ret;
223 trace_nbd_negotiate_send_rep_list(name, desc);
224 name_len = strlen(name);
225 desc_len = strlen(desc);
226 len = name_len + desc_len + sizeof(len);
227 ret = nbd_negotiate_send_rep_len(ioc, NBD_REP_SERVER, NBD_OPT_LIST, len,
228 errp);
229 if (ret < 0) {
230 return ret;
233 len = cpu_to_be32(name_len);
234 if (nbd_write(ioc, &len, sizeof(len), errp) < 0) {
235 error_prepend(errp, "write failed (name length): ");
236 return -EINVAL;
239 if (nbd_write(ioc, name, name_len, errp) < 0) {
240 error_prepend(errp, "write failed (name buffer): ");
241 return -EINVAL;
244 if (nbd_write(ioc, desc, desc_len, errp) < 0) {
245 error_prepend(errp, "write failed (description buffer): ");
246 return -EINVAL;
249 return 0;
252 /* Process the NBD_OPT_LIST command, with a potential series of replies.
253 * Return -errno on error, 0 on success. */
254 static int nbd_negotiate_handle_list(NBDClient *client, uint32_t length,
255 Error **errp)
257 NBDExport *exp;
259 if (length) {
260 if (nbd_drop(client->ioc, length, errp) < 0) {
261 return -EIO;
263 return nbd_negotiate_send_rep_err(client->ioc,
264 NBD_REP_ERR_INVALID, NBD_OPT_LIST,
265 errp,
266 "OPT_LIST should not have length");
269 /* For each export, send a NBD_REP_SERVER reply. */
270 QTAILQ_FOREACH(exp, &exports, next) {
271 if (nbd_negotiate_send_rep_list(client->ioc, exp, errp)) {
272 return -EINVAL;
275 /* Finish with a NBD_REP_ACK. */
276 return nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, NBD_OPT_LIST, errp);
279 static int nbd_negotiate_handle_export_name(NBDClient *client, uint32_t length,
280 Error **errp)
282 char name[NBD_MAX_NAME_SIZE + 1];
284 /* Client sends:
285 [20 .. xx] export name (length bytes)
287 trace_nbd_negotiate_handle_export_name();
288 if (length >= sizeof(name)) {
289 error_setg(errp, "Bad length received");
290 return -EINVAL;
292 if (nbd_read(client->ioc, name, length, errp) < 0) {
293 error_prepend(errp, "read failed: ");
294 return -EINVAL;
296 name[length] = '\0';
298 trace_nbd_negotiate_handle_export_name_request(name);
300 client->exp = nbd_export_find(name);
301 if (!client->exp) {
302 error_setg(errp, "export not found");
303 return -EINVAL;
306 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
307 nbd_export_get(client->exp);
309 return 0;
312 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
313 * new channel for all further (now-encrypted) communication. */
314 static QIOChannel *nbd_negotiate_handle_starttls(NBDClient *client,
315 uint32_t length,
316 Error **errp)
318 QIOChannel *ioc;
319 QIOChannelTLS *tioc;
320 struct NBDTLSHandshakeData data = { 0 };
322 trace_nbd_negotiate_handle_starttls();
323 ioc = client->ioc;
324 if (length) {
325 if (nbd_drop(ioc, length, errp) < 0) {
326 return NULL;
328 nbd_negotiate_send_rep_err(ioc, NBD_REP_ERR_INVALID, NBD_OPT_STARTTLS,
329 errp,
330 "OPT_STARTTLS should not have length");
331 return NULL;
334 if (nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK,
335 NBD_OPT_STARTTLS, errp) < 0) {
336 return NULL;
339 tioc = qio_channel_tls_new_server(ioc,
340 client->tlscreds,
341 client->tlsaclname,
342 errp);
343 if (!tioc) {
344 return NULL;
347 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-server-tls");
348 trace_nbd_negotiate_handle_starttls_handshake();
349 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
350 qio_channel_tls_handshake(tioc,
351 nbd_tls_handshake,
352 &data,
353 NULL);
355 if (!data.complete) {
356 g_main_loop_run(data.loop);
358 g_main_loop_unref(data.loop);
359 if (data.error) {
360 object_unref(OBJECT(tioc));
361 error_propagate(errp, data.error);
362 return NULL;
365 return QIO_CHANNEL(tioc);
368 /* nbd_negotiate_options
369 * Process all NBD_OPT_* client option commands.
370 * Return:
371 * -errno on error, errp is set
372 * 0 on successful negotiation, errp is not set
373 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
374 * errp is not set
376 static int nbd_negotiate_options(NBDClient *client, Error **errp)
378 uint32_t flags;
379 bool fixedNewstyle = false;
381 /* Client sends:
382 [ 0 .. 3] client flags
384 [ 0 .. 7] NBD_OPTS_MAGIC
385 [ 8 .. 11] NBD option
386 [12 .. 15] Data length
387 ... Rest of request
389 [ 0 .. 7] NBD_OPTS_MAGIC
390 [ 8 .. 11] Second NBD option
391 [12 .. 15] Data length
392 ... Rest of request
395 if (nbd_read(client->ioc, &flags, sizeof(flags), errp) < 0) {
396 error_prepend(errp, "read failed: ");
397 return -EIO;
399 trace_nbd_negotiate_options_flags();
400 be32_to_cpus(&flags);
401 if (flags & NBD_FLAG_C_FIXED_NEWSTYLE) {
402 trace_nbd_negotiate_options_newstyle();
403 fixedNewstyle = true;
404 flags &= ~NBD_FLAG_C_FIXED_NEWSTYLE;
406 if (flags & NBD_FLAG_C_NO_ZEROES) {
407 trace_nbd_negotiate_options_no_zeroes();
408 client->no_zeroes = true;
409 flags &= ~NBD_FLAG_C_NO_ZEROES;
411 if (flags != 0) {
412 error_setg(errp, "Unknown client flags 0x%" PRIx32 " received", flags);
413 return -EIO;
416 while (1) {
417 int ret;
418 uint32_t option, length;
419 uint64_t magic;
421 if (nbd_read(client->ioc, &magic, sizeof(magic), errp) < 0) {
422 error_prepend(errp, "read failed: ");
423 return -EINVAL;
425 magic = be64_to_cpu(magic);
426 trace_nbd_negotiate_options_check_magic(magic);
427 if (magic != NBD_OPTS_MAGIC) {
428 error_setg(errp, "Bad magic received");
429 return -EINVAL;
432 if (nbd_read(client->ioc, &option,
433 sizeof(option), errp) < 0) {
434 error_prepend(errp, "read failed: ");
435 return -EINVAL;
437 option = be32_to_cpu(option);
439 if (nbd_read(client->ioc, &length, sizeof(length), errp) < 0) {
440 error_prepend(errp, "read failed: ");
441 return -EINVAL;
443 length = be32_to_cpu(length);
445 trace_nbd_negotiate_options_check_option(option,
446 nbd_opt_lookup(option));
447 if (client->tlscreds &&
448 client->ioc == (QIOChannel *)client->sioc) {
449 QIOChannel *tioc;
450 if (!fixedNewstyle) {
451 error_setg(errp, "Unsupported option 0x%" PRIx32, option);
452 return -EINVAL;
454 switch (option) {
455 case NBD_OPT_STARTTLS:
456 tioc = nbd_negotiate_handle_starttls(client, length, errp);
457 if (!tioc) {
458 return -EIO;
460 object_unref(OBJECT(client->ioc));
461 client->ioc = QIO_CHANNEL(tioc);
462 break;
464 case NBD_OPT_EXPORT_NAME:
465 /* No way to return an error to client, so drop connection */
466 error_setg(errp, "Option 0x%x not permitted before TLS",
467 option);
468 return -EINVAL;
470 default:
471 if (nbd_drop(client->ioc, length, errp) < 0) {
472 return -EIO;
474 ret = nbd_negotiate_send_rep_err(client->ioc,
475 NBD_REP_ERR_TLS_REQD,
476 option, errp,
477 "Option 0x%" PRIx32
478 "not permitted before TLS",
479 option);
480 if (ret < 0) {
481 return ret;
483 /* Let the client keep trying, unless they asked to
484 * quit. In this mode, we've already sent an error, so
485 * we can't ack the abort. */
486 if (option == NBD_OPT_ABORT) {
487 return 1;
489 break;
491 } else if (fixedNewstyle) {
492 switch (option) {
493 case NBD_OPT_LIST:
494 ret = nbd_negotiate_handle_list(client, length, errp);
495 if (ret < 0) {
496 return ret;
498 break;
500 case NBD_OPT_ABORT:
501 /* NBD spec says we must try to reply before
502 * disconnecting, but that we must also tolerate
503 * guests that don't wait for our reply. */
504 nbd_negotiate_send_rep(client->ioc, NBD_REP_ACK, option, NULL);
505 return 1;
507 case NBD_OPT_EXPORT_NAME:
508 return nbd_negotiate_handle_export_name(client, length, errp);
510 case NBD_OPT_STARTTLS:
511 if (nbd_drop(client->ioc, length, errp) < 0) {
512 return -EIO;
514 if (client->tlscreds) {
515 ret = nbd_negotiate_send_rep_err(client->ioc,
516 NBD_REP_ERR_INVALID,
517 option, errp,
518 "TLS already enabled");
519 } else {
520 ret = nbd_negotiate_send_rep_err(client->ioc,
521 NBD_REP_ERR_POLICY,
522 option, errp,
523 "TLS not configured");
525 if (ret < 0) {
526 return ret;
528 break;
529 default:
530 if (nbd_drop(client->ioc, length, errp) < 0) {
531 return -EIO;
533 ret = nbd_negotiate_send_rep_err(client->ioc,
534 NBD_REP_ERR_UNSUP,
535 option, errp,
536 "Unsupported option 0x%"
537 PRIx32 " (%s)", option,
538 nbd_opt_lookup(option));
539 if (ret < 0) {
540 return ret;
542 break;
544 } else {
546 * If broken new-style we should drop the connection
547 * for anything except NBD_OPT_EXPORT_NAME
549 switch (option) {
550 case NBD_OPT_EXPORT_NAME:
551 return nbd_negotiate_handle_export_name(client, length, errp);
553 default:
554 error_setg(errp, "Unsupported option 0x%" PRIx32 " (%s)",
555 option, nbd_opt_lookup(option));
556 return -EINVAL;
562 /* nbd_negotiate
563 * Return:
564 * -errno on error, errp is set
565 * 0 on successful negotiation, errp is not set
566 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
567 * errp is not set
569 static coroutine_fn int nbd_negotiate(NBDClient *client, Error **errp)
571 char buf[8 + 8 + 8 + 128];
572 int ret;
573 const uint16_t myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
574 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA |
575 NBD_FLAG_SEND_WRITE_ZEROES);
576 bool oldStyle;
577 size_t len;
579 /* Old style negotiation header without options
580 [ 0 .. 7] passwd ("NBDMAGIC")
581 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
582 [16 .. 23] size
583 [24 .. 25] server flags (0)
584 [26 .. 27] export flags
585 [28 .. 151] reserved (0)
587 New style negotiation header with options
588 [ 0 .. 7] passwd ("NBDMAGIC")
589 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
590 [16 .. 17] server flags (0)
591 ....options sent....
592 [18 .. 25] size
593 [26 .. 27] export flags
594 [28 .. 151] reserved (0, omit if no_zeroes)
597 qio_channel_set_blocking(client->ioc, false, NULL);
599 trace_nbd_negotiate_begin();
600 memset(buf, 0, sizeof(buf));
601 memcpy(buf, "NBDMAGIC", 8);
603 oldStyle = client->exp != NULL && !client->tlscreds;
604 if (oldStyle) {
605 trace_nbd_negotiate_old_style(client->exp->size,
606 client->exp->nbdflags | myflags);
607 stq_be_p(buf + 8, NBD_CLIENT_MAGIC);
608 stq_be_p(buf + 16, client->exp->size);
609 stw_be_p(buf + 26, client->exp->nbdflags | myflags);
611 if (nbd_write(client->ioc, buf, sizeof(buf), errp) < 0) {
612 error_prepend(errp, "write failed: ");
613 return -EINVAL;
615 } else {
616 stq_be_p(buf + 8, NBD_OPTS_MAGIC);
617 stw_be_p(buf + 16, NBD_FLAG_FIXED_NEWSTYLE | NBD_FLAG_NO_ZEROES);
619 if (nbd_write(client->ioc, buf, 18, errp) < 0) {
620 error_prepend(errp, "write failed: ");
621 return -EINVAL;
623 ret = nbd_negotiate_options(client, errp);
624 if (ret != 0) {
625 if (ret < 0) {
626 error_prepend(errp, "option negotiation failed: ");
628 return ret;
631 trace_nbd_negotiate_new_style_size_flags(
632 client->exp->size, client->exp->nbdflags | myflags);
633 stq_be_p(buf + 18, client->exp->size);
634 stw_be_p(buf + 26, client->exp->nbdflags | myflags);
635 len = client->no_zeroes ? 10 : sizeof(buf) - 18;
636 ret = nbd_write(client->ioc, buf + 18, len, errp);
637 if (ret < 0) {
638 error_prepend(errp, "write failed: ");
639 return ret;
643 trace_nbd_negotiate_success();
645 return 0;
648 static int nbd_receive_request(QIOChannel *ioc, NBDRequest *request,
649 Error **errp)
651 uint8_t buf[NBD_REQUEST_SIZE];
652 uint32_t magic;
653 int ret;
655 ret = nbd_read(ioc, buf, sizeof(buf), errp);
656 if (ret < 0) {
657 return ret;
660 /* Request
661 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
662 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
663 [ 6 .. 7] type (NBD_CMD_READ, ...)
664 [ 8 .. 15] handle
665 [16 .. 23] from
666 [24 .. 27] len
669 magic = ldl_be_p(buf);
670 request->flags = lduw_be_p(buf + 4);
671 request->type = lduw_be_p(buf + 6);
672 request->handle = ldq_be_p(buf + 8);
673 request->from = ldq_be_p(buf + 16);
674 request->len = ldl_be_p(buf + 24);
676 trace_nbd_receive_request(magic, request->flags, request->type,
677 request->from, request->len);
679 if (magic != NBD_REQUEST_MAGIC) {
680 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
681 return -EINVAL;
683 return 0;
686 static int nbd_send_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
688 uint8_t buf[NBD_REPLY_SIZE];
690 reply->error = system_errno_to_nbd_errno(reply->error);
692 trace_nbd_send_reply(reply->error, reply->handle);
694 /* Reply
695 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
696 [ 4 .. 7] error (0 == no error)
697 [ 7 .. 15] handle
699 stl_be_p(buf, NBD_REPLY_MAGIC);
700 stl_be_p(buf + 4, reply->error);
701 stq_be_p(buf + 8, reply->handle);
703 return nbd_write(ioc, buf, sizeof(buf), errp);
706 #define MAX_NBD_REQUESTS 16
708 void nbd_client_get(NBDClient *client)
710 client->refcount++;
713 void nbd_client_put(NBDClient *client)
715 if (--client->refcount == 0) {
716 /* The last reference should be dropped by client->close,
717 * which is called by client_close.
719 assert(client->closing);
721 qio_channel_detach_aio_context(client->ioc);
722 object_unref(OBJECT(client->sioc));
723 object_unref(OBJECT(client->ioc));
724 if (client->tlscreds) {
725 object_unref(OBJECT(client->tlscreds));
727 g_free(client->tlsaclname);
728 if (client->exp) {
729 QTAILQ_REMOVE(&client->exp->clients, client, next);
730 nbd_export_put(client->exp);
732 g_free(client);
736 static void client_close(NBDClient *client, bool negotiated)
738 if (client->closing) {
739 return;
742 client->closing = true;
744 /* Force requests to finish. They will drop their own references,
745 * then we'll close the socket and free the NBDClient.
747 qio_channel_shutdown(client->ioc, QIO_CHANNEL_SHUTDOWN_BOTH,
748 NULL);
750 /* Also tell the client, so that they release their reference. */
751 if (client->close_fn) {
752 client->close_fn(client, negotiated);
756 static NBDRequestData *nbd_request_get(NBDClient *client)
758 NBDRequestData *req;
760 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
761 client->nb_requests++;
763 req = g_new0(NBDRequestData, 1);
764 nbd_client_get(client);
765 req->client = client;
766 return req;
769 static void nbd_request_put(NBDRequestData *req)
771 NBDClient *client = req->client;
773 if (req->data) {
774 qemu_vfree(req->data);
776 g_free(req);
778 client->nb_requests--;
779 nbd_client_receive_next_request(client);
781 nbd_client_put(client);
784 static void blk_aio_attached(AioContext *ctx, void *opaque)
786 NBDExport *exp = opaque;
787 NBDClient *client;
789 trace_nbd_blk_aio_attached(exp->name, ctx);
791 exp->ctx = ctx;
793 QTAILQ_FOREACH(client, &exp->clients, next) {
794 qio_channel_attach_aio_context(client->ioc, ctx);
795 if (client->recv_coroutine) {
796 aio_co_schedule(ctx, client->recv_coroutine);
798 if (client->send_coroutine) {
799 aio_co_schedule(ctx, client->send_coroutine);
804 static void blk_aio_detach(void *opaque)
806 NBDExport *exp = opaque;
807 NBDClient *client;
809 trace_nbd_blk_aio_detach(exp->name, exp->ctx);
811 QTAILQ_FOREACH(client, &exp->clients, next) {
812 qio_channel_detach_aio_context(client->ioc);
815 exp->ctx = NULL;
818 static void nbd_eject_notifier(Notifier *n, void *data)
820 NBDExport *exp = container_of(n, NBDExport, eject_notifier);
821 nbd_export_close(exp);
824 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset, off_t size,
825 uint16_t nbdflags, void (*close)(NBDExport *),
826 bool writethrough, BlockBackend *on_eject_blk,
827 Error **errp)
829 BlockBackend *blk;
830 NBDExport *exp = g_malloc0(sizeof(NBDExport));
831 uint64_t perm;
832 int ret;
834 /* Don't allow resize while the NBD server is running, otherwise we don't
835 * care what happens with the node. */
836 perm = BLK_PERM_CONSISTENT_READ;
837 if ((nbdflags & NBD_FLAG_READ_ONLY) == 0) {
838 perm |= BLK_PERM_WRITE;
840 blk = blk_new(perm, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE_UNCHANGED |
841 BLK_PERM_WRITE | BLK_PERM_GRAPH_MOD);
842 ret = blk_insert_bs(blk, bs, errp);
843 if (ret < 0) {
844 goto fail;
846 blk_set_enable_write_cache(blk, !writethrough);
848 exp->refcount = 1;
849 QTAILQ_INIT(&exp->clients);
850 exp->blk = blk;
851 exp->dev_offset = dev_offset;
852 exp->nbdflags = nbdflags;
853 exp->size = size < 0 ? blk_getlength(blk) : size;
854 if (exp->size < 0) {
855 error_setg_errno(errp, -exp->size,
856 "Failed to determine the NBD export's length");
857 goto fail;
859 exp->size -= exp->size % BDRV_SECTOR_SIZE;
861 exp->close = close;
862 exp->ctx = blk_get_aio_context(blk);
863 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
865 if (on_eject_blk) {
866 blk_ref(on_eject_blk);
867 exp->eject_notifier_blk = on_eject_blk;
868 exp->eject_notifier.notify = nbd_eject_notifier;
869 blk_add_remove_bs_notifier(on_eject_blk, &exp->eject_notifier);
873 * NBD exports are used for non-shared storage migration. Make sure
874 * that BDRV_O_INACTIVE is cleared and the image is ready for write
875 * access since the export could be available before migration handover.
877 aio_context_acquire(exp->ctx);
878 blk_invalidate_cache(blk, NULL);
879 aio_context_release(exp->ctx);
880 return exp;
882 fail:
883 blk_unref(blk);
884 g_free(exp);
885 return NULL;
888 NBDExport *nbd_export_find(const char *name)
890 NBDExport *exp;
891 QTAILQ_FOREACH(exp, &exports, next) {
892 if (strcmp(name, exp->name) == 0) {
893 return exp;
897 return NULL;
900 void nbd_export_set_name(NBDExport *exp, const char *name)
902 if (exp->name == name) {
903 return;
906 nbd_export_get(exp);
907 if (exp->name != NULL) {
908 g_free(exp->name);
909 exp->name = NULL;
910 QTAILQ_REMOVE(&exports, exp, next);
911 nbd_export_put(exp);
913 if (name != NULL) {
914 nbd_export_get(exp);
915 exp->name = g_strdup(name);
916 QTAILQ_INSERT_TAIL(&exports, exp, next);
918 nbd_export_put(exp);
921 void nbd_export_set_description(NBDExport *exp, const char *description)
923 g_free(exp->description);
924 exp->description = g_strdup(description);
927 void nbd_export_close(NBDExport *exp)
929 NBDClient *client, *next;
931 nbd_export_get(exp);
932 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
933 client_close(client, true);
935 nbd_export_set_name(exp, NULL);
936 nbd_export_set_description(exp, NULL);
937 nbd_export_put(exp);
940 void nbd_export_get(NBDExport *exp)
942 assert(exp->refcount > 0);
943 exp->refcount++;
946 void nbd_export_put(NBDExport *exp)
948 assert(exp->refcount > 0);
949 if (exp->refcount == 1) {
950 nbd_export_close(exp);
953 if (--exp->refcount == 0) {
954 assert(exp->name == NULL);
955 assert(exp->description == NULL);
957 if (exp->close) {
958 exp->close(exp);
961 if (exp->blk) {
962 if (exp->eject_notifier_blk) {
963 notifier_remove(&exp->eject_notifier);
964 blk_unref(exp->eject_notifier_blk);
966 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
967 blk_aio_detach, exp);
968 blk_unref(exp->blk);
969 exp->blk = NULL;
972 g_free(exp);
976 BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
978 return exp->blk;
981 void nbd_export_close_all(void)
983 NBDExport *exp, *next;
985 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
986 nbd_export_close(exp);
990 static int nbd_co_send_reply(NBDRequestData *req, NBDReply *reply, int len,
991 Error **errp)
993 NBDClient *client = req->client;
994 int ret;
996 g_assert(qemu_in_coroutine());
998 trace_nbd_co_send_reply(reply->handle, reply->error, len);
1000 qemu_co_mutex_lock(&client->send_lock);
1001 client->send_coroutine = qemu_coroutine_self();
1003 if (!len) {
1004 ret = nbd_send_reply(client->ioc, reply, errp);
1005 } else {
1006 qio_channel_set_cork(client->ioc, true);
1007 ret = nbd_send_reply(client->ioc, reply, errp);
1008 if (ret == 0) {
1009 ret = nbd_write(client->ioc, req->data, len, errp);
1010 if (ret < 0) {
1011 ret = -EIO;
1014 qio_channel_set_cork(client->ioc, false);
1017 client->send_coroutine = NULL;
1018 qemu_co_mutex_unlock(&client->send_lock);
1019 return ret;
1022 /* nbd_co_receive_request
1023 * Collect a client request. Return 0 if request looks valid, -EIO to drop
1024 * connection right away, and any other negative value to report an error to
1025 * the client (although the caller may still need to disconnect after reporting
1026 * the error).
1028 static int nbd_co_receive_request(NBDRequestData *req, NBDRequest *request,
1029 Error **errp)
1031 NBDClient *client = req->client;
1033 g_assert(qemu_in_coroutine());
1034 assert(client->recv_coroutine == qemu_coroutine_self());
1035 if (nbd_receive_request(client->ioc, request, errp) < 0) {
1036 return -EIO;
1039 trace_nbd_co_receive_request_decode_type(request->handle, request->type,
1040 nbd_cmd_lookup(request->type));
1042 if (request->type != NBD_CMD_WRITE) {
1043 /* No payload, we are ready to read the next request. */
1044 req->complete = true;
1047 if (request->type == NBD_CMD_DISC) {
1048 /* Special case: we're going to disconnect without a reply,
1049 * whether or not flags, from, or len are bogus */
1050 return -EIO;
1053 /* Check for sanity in the parameters, part 1. Defer as many
1054 * checks as possible until after reading any NBD_CMD_WRITE
1055 * payload, so we can try and keep the connection alive. */
1056 if ((request->from + request->len) < request->from) {
1057 error_setg(errp,
1058 "integer overflow detected, you're probably being attacked");
1059 return -EINVAL;
1062 if (request->type == NBD_CMD_READ || request->type == NBD_CMD_WRITE) {
1063 if (request->len > NBD_MAX_BUFFER_SIZE) {
1064 error_setg(errp, "len (%" PRIu32" ) is larger than max len (%u)",
1065 request->len, NBD_MAX_BUFFER_SIZE);
1066 return -EINVAL;
1069 req->data = blk_try_blockalign(client->exp->blk, request->len);
1070 if (req->data == NULL) {
1071 error_setg(errp, "No memory");
1072 return -ENOMEM;
1075 if (request->type == NBD_CMD_WRITE) {
1076 if (nbd_read(client->ioc, req->data, request->len, errp) < 0) {
1077 error_prepend(errp, "reading from socket failed: ");
1078 return -EIO;
1080 req->complete = true;
1082 trace_nbd_co_receive_request_payload_received(request->handle,
1083 request->len);
1086 /* Sanity checks, part 2. */
1087 if (request->from + request->len > client->exp->size) {
1088 error_setg(errp, "operation past EOF; From: %" PRIu64 ", Len: %" PRIu32
1089 ", Size: %" PRIu64, request->from, request->len,
1090 (uint64_t)client->exp->size);
1091 return request->type == NBD_CMD_WRITE ? -ENOSPC : -EINVAL;
1093 if (request->flags & ~(NBD_CMD_FLAG_FUA | NBD_CMD_FLAG_NO_HOLE)) {
1094 error_setg(errp, "unsupported flags (got 0x%x)", request->flags);
1095 return -EINVAL;
1097 if (request->type != NBD_CMD_WRITE_ZEROES &&
1098 (request->flags & NBD_CMD_FLAG_NO_HOLE)) {
1099 error_setg(errp, "unexpected flags (got 0x%x)", request->flags);
1100 return -EINVAL;
1103 return 0;
1106 /* Owns a reference to the NBDClient passed as opaque. */
1107 static coroutine_fn void nbd_trip(void *opaque)
1109 NBDClient *client = opaque;
1110 NBDExport *exp = client->exp;
1111 NBDRequestData *req;
1112 NBDRequest request = { 0 }; /* GCC thinks it can be used uninitialized */
1113 NBDReply reply;
1114 int ret;
1115 int flags;
1116 int reply_data_len = 0;
1117 Error *local_err = NULL;
1119 trace_nbd_trip();
1120 if (client->closing) {
1121 nbd_client_put(client);
1122 return;
1125 req = nbd_request_get(client);
1126 ret = nbd_co_receive_request(req, &request, &local_err);
1127 client->recv_coroutine = NULL;
1128 nbd_client_receive_next_request(client);
1129 if (ret == -EIO) {
1130 goto disconnect;
1133 reply.handle = request.handle;
1134 reply.error = 0;
1136 if (ret < 0) {
1137 reply.error = -ret;
1138 goto reply;
1141 if (client->closing) {
1143 * The client may be closed when we are blocked in
1144 * nbd_co_receive_request()
1146 goto done;
1149 switch (request.type) {
1150 case NBD_CMD_READ:
1151 /* XXX: NBD Protocol only documents use of FUA with WRITE */
1152 if (request.flags & NBD_CMD_FLAG_FUA) {
1153 ret = blk_co_flush(exp->blk);
1154 if (ret < 0) {
1155 error_setg_errno(&local_err, -ret, "flush failed");
1156 reply.error = -ret;
1157 break;
1161 ret = blk_pread(exp->blk, request.from + exp->dev_offset,
1162 req->data, request.len);
1163 if (ret < 0) {
1164 error_setg_errno(&local_err, -ret, "reading from file failed");
1165 reply.error = -ret;
1166 break;
1169 reply_data_len = request.len;
1171 break;
1172 case NBD_CMD_WRITE:
1173 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
1174 reply.error = EROFS;
1175 break;
1178 flags = 0;
1179 if (request.flags & NBD_CMD_FLAG_FUA) {
1180 flags |= BDRV_REQ_FUA;
1182 ret = blk_pwrite(exp->blk, request.from + exp->dev_offset,
1183 req->data, request.len, flags);
1184 if (ret < 0) {
1185 error_setg_errno(&local_err, -ret, "writing to file failed");
1186 reply.error = -ret;
1189 break;
1190 case NBD_CMD_WRITE_ZEROES:
1191 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
1192 error_setg(&local_err, "Server is read-only, return error");
1193 reply.error = EROFS;
1194 break;
1197 flags = 0;
1198 if (request.flags & NBD_CMD_FLAG_FUA) {
1199 flags |= BDRV_REQ_FUA;
1201 if (!(request.flags & NBD_CMD_FLAG_NO_HOLE)) {
1202 flags |= BDRV_REQ_MAY_UNMAP;
1204 ret = blk_pwrite_zeroes(exp->blk, request.from + exp->dev_offset,
1205 request.len, flags);
1206 if (ret < 0) {
1207 error_setg_errno(&local_err, -ret, "writing to file failed");
1208 reply.error = -ret;
1211 break;
1212 case NBD_CMD_DISC:
1213 /* unreachable, thanks to special case in nbd_co_receive_request() */
1214 abort();
1216 case NBD_CMD_FLUSH:
1217 ret = blk_co_flush(exp->blk);
1218 if (ret < 0) {
1219 error_setg_errno(&local_err, -ret, "flush failed");
1220 reply.error = -ret;
1223 break;
1224 case NBD_CMD_TRIM:
1225 ret = blk_co_pdiscard(exp->blk, request.from + exp->dev_offset,
1226 request.len);
1227 if (ret < 0) {
1228 error_setg_errno(&local_err, -ret, "discard failed");
1229 reply.error = -ret;
1232 break;
1233 default:
1234 error_setg(&local_err, "invalid request type (%" PRIu32 ") received",
1235 request.type);
1236 reply.error = EINVAL;
1239 reply:
1240 if (local_err) {
1241 /* If we are here local_err is not fatal error, already stored in
1242 * reply.error */
1243 error_report_err(local_err);
1244 local_err = NULL;
1247 if (nbd_co_send_reply(req, &reply, reply_data_len, &local_err) < 0) {
1248 error_prepend(&local_err, "Failed to send reply: ");
1249 goto disconnect;
1252 /* We must disconnect after NBD_CMD_WRITE if we did not
1253 * read the payload.
1255 if (!req->complete) {
1256 error_setg(&local_err, "Request handling failed in intermediate state");
1257 goto disconnect;
1260 done:
1261 nbd_request_put(req);
1262 nbd_client_put(client);
1263 return;
1265 disconnect:
1266 if (local_err) {
1267 error_reportf_err(local_err, "Disconnect client, due to: ");
1269 nbd_request_put(req);
1270 client_close(client, true);
1271 nbd_client_put(client);
1274 static void nbd_client_receive_next_request(NBDClient *client)
1276 if (!client->recv_coroutine && client->nb_requests < MAX_NBD_REQUESTS) {
1277 nbd_client_get(client);
1278 client->recv_coroutine = qemu_coroutine_create(nbd_trip, client);
1279 aio_co_schedule(client->exp->ctx, client->recv_coroutine);
1283 static coroutine_fn void nbd_co_client_start(void *opaque)
1285 NBDClient *client = opaque;
1286 NBDExport *exp = client->exp;
1287 Error *local_err = NULL;
1289 if (exp) {
1290 nbd_export_get(exp);
1291 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1293 qemu_co_mutex_init(&client->send_lock);
1295 if (nbd_negotiate(client, &local_err)) {
1296 if (local_err) {
1297 error_report_err(local_err);
1299 client_close(client, false);
1300 return;
1303 nbd_client_receive_next_request(client);
1307 * Create a new client listener on the given export @exp, using the
1308 * given channel @sioc. Begin servicing it in a coroutine. When the
1309 * connection closes, call @close_fn with an indication of whether the
1310 * client completed negotiation.
1312 void nbd_client_new(NBDExport *exp,
1313 QIOChannelSocket *sioc,
1314 QCryptoTLSCreds *tlscreds,
1315 const char *tlsaclname,
1316 void (*close_fn)(NBDClient *, bool))
1318 NBDClient *client;
1319 Coroutine *co;
1321 client = g_malloc0(sizeof(NBDClient));
1322 client->refcount = 1;
1323 client->exp = exp;
1324 client->tlscreds = tlscreds;
1325 if (tlscreds) {
1326 object_ref(OBJECT(client->tlscreds));
1328 client->tlsaclname = g_strdup(tlsaclname);
1329 client->sioc = sioc;
1330 object_ref(OBJECT(client->sioc));
1331 client->ioc = QIO_CHANNEL(sioc);
1332 object_ref(OBJECT(client->ioc));
1333 client->close_fn = close_fn;
1335 co = qemu_coroutine_create(nbd_co_client_start, client);
1336 qemu_coroutine_enter(co);