2 * Copyright (C) 2016 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"
23 #include "nbd-internal.h"
25 static int system_errno_to_nbd_errno(int err
)
51 /* Definitions for opaque data types */
53 typedef struct NBDRequestData NBDRequestData
;
55 struct NBDRequestData
{
56 QSIMPLEQ_ENTRY(NBDRequestData
) entry
;
64 void (*close
)(NBDExport
*exp
);
72 QTAILQ_HEAD(, NBDClient
) clients
;
73 QTAILQ_ENTRY(NBDExport
) next
;
77 BlockBackend
*eject_notifier_blk
;
78 Notifier eject_notifier
;
81 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
85 void (*close_fn
)(NBDClient
*client
, bool negotiated
);
89 QCryptoTLSCreds
*tlscreds
;
91 QIOChannelSocket
*sioc
; /* The underlying data channel */
92 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
94 Coroutine
*recv_coroutine
;
97 Coroutine
*send_coroutine
;
99 QTAILQ_ENTRY(NBDClient
) next
;
104 /* That's all folks */
106 static void nbd_client_receive_next_request(NBDClient
*client
);
108 /* Basic flow for negotiation
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
)
142 trace_nbd_negotiate_send_rep_len(opt
, type
, len
);
144 magic
= cpu_to_be64(NBD_REP_MAGIC
);
145 if (nbd_write(ioc
, &magic
, sizeof(magic
), errp
) < 0) {
146 error_prepend(errp
, "write failed (rep magic): ");
150 opt
= cpu_to_be32(opt
);
151 if (nbd_write(ioc
, &opt
, sizeof(opt
), errp
) < 0) {
152 error_prepend(errp
, "write failed (rep opt): ");
156 type
= cpu_to_be32(type
);
157 if (nbd_write(ioc
, &type
, sizeof(type
), errp
) < 0) {
158 error_prepend(errp
, "write failed (rep type): ");
162 len
= cpu_to_be32(len
);
163 if (nbd_write(ioc
, &len
, sizeof(len
), errp
) < 0) {
164 error_prepend(errp
, "write failed (rep data length): ");
170 /* Send a reply header with default 0 length.
171 * Return -errno on error, 0 on success. */
172 static int nbd_negotiate_send_rep(QIOChannel
*ioc
, uint32_t type
, uint32_t opt
,
175 return nbd_negotiate_send_rep_len(ioc
, type
, opt
, 0, errp
);
178 /* Send an error reply.
179 * Return -errno on error, 0 on success. */
180 static int GCC_FMT_ATTR(5, 6)
181 nbd_negotiate_send_rep_err(QIOChannel
*ioc
, uint32_t type
,
182 uint32_t opt
, Error
**errp
, const char *fmt
, ...)
190 msg
= g_strdup_vprintf(fmt
, va
);
194 trace_nbd_negotiate_send_rep_err(msg
);
195 ret
= nbd_negotiate_send_rep_len(ioc
, type
, opt
, len
, errp
);
199 if (nbd_write(ioc
, msg
, len
, errp
) < 0) {
200 error_prepend(errp
, "write failed (error message): ");
211 /* Send a single NBD_REP_SERVER reply to NBD_OPT_LIST, including payload.
212 * Return -errno on error, 0 on success. */
213 static int nbd_negotiate_send_rep_list(QIOChannel
*ioc
, NBDExport
*exp
,
216 size_t name_len
, desc_len
;
218 const char *name
= exp
->name
? exp
->name
: "";
219 const char *desc
= exp
->description
? exp
->description
: "";
222 trace_nbd_negotiate_send_rep_list(name
, desc
);
223 name_len
= strlen(name
);
224 desc_len
= strlen(desc
);
225 len
= name_len
+ desc_len
+ sizeof(len
);
226 ret
= nbd_negotiate_send_rep_len(ioc
, NBD_REP_SERVER
, NBD_OPT_LIST
, len
,
232 len
= cpu_to_be32(name_len
);
233 if (nbd_write(ioc
, &len
, sizeof(len
), errp
) < 0) {
234 error_prepend(errp
, "write failed (name length): ");
238 if (nbd_write(ioc
, name
, name_len
, errp
) < 0) {
239 error_prepend(errp
, "write failed (name buffer): ");
243 if (nbd_write(ioc
, desc
, desc_len
, errp
) < 0) {
244 error_prepend(errp
, "write failed (description buffer): ");
251 /* Process the NBD_OPT_LIST command, with a potential series of replies.
252 * Return -errno on error, 0 on success. */
253 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
,
259 if (nbd_drop(client
->ioc
, length
, errp
) < 0) {
262 return nbd_negotiate_send_rep_err(client
->ioc
,
263 NBD_REP_ERR_INVALID
, NBD_OPT_LIST
,
265 "OPT_LIST should not have length");
268 /* For each export, send a NBD_REP_SERVER reply. */
269 QTAILQ_FOREACH(exp
, &exports
, next
) {
270 if (nbd_negotiate_send_rep_list(client
->ioc
, exp
, errp
)) {
274 /* Finish with a NBD_REP_ACK. */
275 return nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_LIST
, errp
);
278 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
,
281 char name
[NBD_MAX_NAME_SIZE
+ 1];
284 [20 .. xx] export name (length bytes)
286 trace_nbd_negotiate_handle_export_name();
287 if (length
>= sizeof(name
)) {
288 error_setg(errp
, "Bad length received");
291 if (nbd_read(client
->ioc
, name
, length
, errp
) < 0) {
292 error_prepend(errp
, "read failed: ");
297 trace_nbd_negotiate_handle_export_name_request(name
);
299 client
->exp
= nbd_export_find(name
);
301 error_setg(errp
, "export not found");
305 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
306 nbd_export_get(client
->exp
);
311 /* Handle NBD_OPT_STARTTLS. Return NULL to drop connection, or else the
312 * new channel for all further (now-encrypted) communication. */
313 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
319 struct NBDTLSHandshakeData data
= { 0 };
321 trace_nbd_negotiate_handle_starttls();
324 if (nbd_drop(ioc
, length
, errp
) < 0) {
327 nbd_negotiate_send_rep_err(ioc
, NBD_REP_ERR_INVALID
, NBD_OPT_STARTTLS
,
329 "OPT_STARTTLS should not have length");
333 if (nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
,
334 NBD_OPT_STARTTLS
, errp
) < 0) {
338 tioc
= qio_channel_tls_new_server(ioc
,
346 qio_channel_set_name(QIO_CHANNEL(tioc
), "nbd-server-tls");
347 trace_nbd_negotiate_handle_starttls_handshake();
348 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
349 qio_channel_tls_handshake(tioc
,
354 if (!data
.complete
) {
355 g_main_loop_run(data
.loop
);
357 g_main_loop_unref(data
.loop
);
359 object_unref(OBJECT(tioc
));
360 error_propagate(errp
, data
.error
);
364 return QIO_CHANNEL(tioc
);
367 /* nbd_negotiate_options
368 * Process all NBD_OPT_* client option commands.
370 * -errno on error, errp is set
371 * 0 on successful negotiation, errp is not set
372 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
375 static int nbd_negotiate_options(NBDClient
*client
, Error
**errp
)
378 bool fixedNewstyle
= false;
379 Error
*local_err
= NULL
;
382 [ 0 .. 3] client flags
384 [ 0 .. 7] NBD_OPTS_MAGIC
385 [ 8 .. 11] NBD option
386 [12 .. 15] Data length
389 [ 0 .. 7] NBD_OPTS_MAGIC
390 [ 8 .. 11] Second NBD option
391 [12 .. 15] Data length
395 if (nbd_read(client
->ioc
, &flags
, sizeof(flags
), errp
) < 0) {
396 error_prepend(errp
, "read failed: ");
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
;
412 error_setg(errp
, "Unknown client flags 0x%" PRIx32
" received", flags
);
418 uint32_t option
, length
;
421 if (nbd_read(client
->ioc
, &magic
, sizeof(magic
), errp
) < 0) {
422 error_prepend(errp
, "read failed: ");
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");
432 if (nbd_read(client
->ioc
, &option
,
433 sizeof(option
), errp
) < 0) {
434 error_prepend(errp
, "read failed: ");
437 option
= be32_to_cpu(option
);
439 if (nbd_read(client
->ioc
, &length
, sizeof(length
), errp
) < 0) {
440 error_prepend(errp
, "read failed: ");
443 length
= be32_to_cpu(length
);
445 trace_nbd_negotiate_options_check_option(option
);
446 if (client
->tlscreds
&&
447 client
->ioc
== (QIOChannel
*)client
->sioc
) {
449 if (!fixedNewstyle
) {
450 error_setg(errp
, "Unsupported option 0x%" PRIx32
, option
);
454 case NBD_OPT_STARTTLS
:
455 tioc
= nbd_negotiate_handle_starttls(client
, length
, errp
);
459 object_unref(OBJECT(client
->ioc
));
460 client
->ioc
= QIO_CHANNEL(tioc
);
463 case NBD_OPT_EXPORT_NAME
:
464 /* No way to return an error to client, so drop connection */
465 error_setg(errp
, "Option 0x%x not permitted before TLS",
470 if (nbd_drop(client
->ioc
, length
, errp
) < 0) {
473 ret
= nbd_negotiate_send_rep_err(client
->ioc
,
474 NBD_REP_ERR_TLS_REQD
,
477 "not permitted before TLS",
482 /* Let the client keep trying, unless they asked to quit */
483 if (option
== NBD_OPT_ABORT
) {
488 } else if (fixedNewstyle
) {
491 ret
= nbd_negotiate_handle_list(client
, length
, errp
);
498 /* NBD spec says we must try to reply before
499 * disconnecting, but that we must also tolerate
500 * guests that don't wait for our reply. */
501 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, option
,
504 if (local_err
!= NULL
) {
505 const char *error
= error_get_pretty(local_err
);
506 trace_nbd_opt_abort_reply_failed(error
);
507 error_free(local_err
);
512 case NBD_OPT_EXPORT_NAME
:
513 return nbd_negotiate_handle_export_name(client
, length
, errp
);
515 case NBD_OPT_STARTTLS
:
516 if (nbd_drop(client
->ioc
, length
, errp
) < 0) {
519 if (client
->tlscreds
) {
520 ret
= nbd_negotiate_send_rep_err(client
->ioc
,
523 "TLS already enabled");
525 ret
= nbd_negotiate_send_rep_err(client
->ioc
,
528 "TLS not configured");
535 if (nbd_drop(client
->ioc
, length
, errp
) < 0) {
538 ret
= nbd_negotiate_send_rep_err(client
->ioc
,
541 "Unsupported option 0x%"
551 * If broken new-style we should drop the connection
552 * for anything except NBD_OPT_EXPORT_NAME
555 case NBD_OPT_EXPORT_NAME
:
556 return nbd_negotiate_handle_export_name(client
, length
, errp
);
559 error_setg(errp
, "Unsupported option 0x%" PRIx32
, option
);
568 * -errno on error, errp is set
569 * 0 on successful negotiation, errp is not set
570 * 1 if client sent NBD_OPT_ABORT, i.e. on valid disconnect,
573 static coroutine_fn
int nbd_negotiate(NBDClient
*client
, Error
**errp
)
575 char buf
[8 + 8 + 8 + 128];
577 const uint16_t myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
578 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
|
579 NBD_FLAG_SEND_WRITE_ZEROES
);
583 /* Old style negotiation header without options
584 [ 0 .. 7] passwd ("NBDMAGIC")
585 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
587 [24 .. 25] server flags (0)
588 [26 .. 27] export flags
589 [28 .. 151] reserved (0)
591 New style negotiation header with options
592 [ 0 .. 7] passwd ("NBDMAGIC")
593 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
594 [16 .. 17] server flags (0)
597 [26 .. 27] export flags
598 [28 .. 151] reserved (0, omit if no_zeroes)
601 qio_channel_set_blocking(client
->ioc
, false, NULL
);
603 trace_nbd_negotiate_begin();
604 memset(buf
, 0, sizeof(buf
));
605 memcpy(buf
, "NBDMAGIC", 8);
607 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
609 trace_nbd_negotiate_old_style(client
->exp
->size
,
610 client
->exp
->nbdflags
| myflags
);
611 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
612 stq_be_p(buf
+ 16, client
->exp
->size
);
613 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
615 if (nbd_write(client
->ioc
, buf
, sizeof(buf
), errp
) < 0) {
616 error_prepend(errp
, "write failed: ");
620 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
621 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
| NBD_FLAG_NO_ZEROES
);
623 if (nbd_write(client
->ioc
, buf
, 18, errp
) < 0) {
624 error_prepend(errp
, "write failed: ");
627 ret
= nbd_negotiate_options(client
, errp
);
630 error_prepend(errp
, "option negotiation failed: ");
635 trace_nbd_negotiate_new_style_size_flags(
636 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
637 stq_be_p(buf
+ 18, client
->exp
->size
);
638 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
639 len
= client
->no_zeroes
? 10 : sizeof(buf
) - 18;
640 ret
= nbd_write(client
->ioc
, buf
+ 18, len
, errp
);
642 error_prepend(errp
, "write failed: ");
647 trace_nbd_negotiate_success();
652 static int nbd_receive_request(QIOChannel
*ioc
, NBDRequest
*request
,
655 uint8_t buf
[NBD_REQUEST_SIZE
];
659 ret
= nbd_read(ioc
, buf
, sizeof(buf
), errp
);
665 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
666 [ 4 .. 5] flags (NBD_CMD_FLAG_FUA, ...)
667 [ 6 .. 7] type (NBD_CMD_READ, ...)
673 magic
= ldl_be_p(buf
);
674 request
->flags
= lduw_be_p(buf
+ 4);
675 request
->type
= lduw_be_p(buf
+ 6);
676 request
->handle
= ldq_be_p(buf
+ 8);
677 request
->from
= ldq_be_p(buf
+ 16);
678 request
->len
= ldl_be_p(buf
+ 24);
680 trace_nbd_receive_request(magic
, request
->flags
, request
->type
,
681 request
->from
, request
->len
);
683 if (magic
!= NBD_REQUEST_MAGIC
) {
684 error_setg(errp
, "invalid magic (got 0x%" PRIx32
")", magic
);
690 static int nbd_send_reply(QIOChannel
*ioc
, NBDReply
*reply
, Error
**errp
)
692 uint8_t buf
[NBD_REPLY_SIZE
];
694 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
696 trace_nbd_send_reply(reply
->error
, reply
->handle
);
699 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
700 [ 4 .. 7] error (0 == no error)
703 stl_be_p(buf
, NBD_REPLY_MAGIC
);
704 stl_be_p(buf
+ 4, reply
->error
);
705 stq_be_p(buf
+ 8, reply
->handle
);
707 return nbd_write(ioc
, buf
, sizeof(buf
), errp
);
710 #define MAX_NBD_REQUESTS 16
712 void nbd_client_get(NBDClient
*client
)
717 void nbd_client_put(NBDClient
*client
)
719 if (--client
->refcount
== 0) {
720 /* The last reference should be dropped by client->close,
721 * which is called by client_close.
723 assert(client
->closing
);
725 qio_channel_detach_aio_context(client
->ioc
);
726 object_unref(OBJECT(client
->sioc
));
727 object_unref(OBJECT(client
->ioc
));
728 if (client
->tlscreds
) {
729 object_unref(OBJECT(client
->tlscreds
));
731 g_free(client
->tlsaclname
);
733 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
734 nbd_export_put(client
->exp
);
740 static void client_close(NBDClient
*client
, bool negotiated
)
742 if (client
->closing
) {
746 client
->closing
= true;
748 /* Force requests to finish. They will drop their own references,
749 * then we'll close the socket and free the NBDClient.
751 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
754 /* Also tell the client, so that they release their reference. */
755 if (client
->close_fn
) {
756 client
->close_fn(client
, negotiated
);
760 static NBDRequestData
*nbd_request_get(NBDClient
*client
)
764 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
765 client
->nb_requests
++;
767 req
= g_new0(NBDRequestData
, 1);
768 nbd_client_get(client
);
769 req
->client
= client
;
773 static void nbd_request_put(NBDRequestData
*req
)
775 NBDClient
*client
= req
->client
;
778 qemu_vfree(req
->data
);
782 client
->nb_requests
--;
783 nbd_client_receive_next_request(client
);
785 nbd_client_put(client
);
788 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
790 NBDExport
*exp
= opaque
;
793 trace_nbd_blk_aio_attached(exp
->name
, ctx
);
797 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
798 qio_channel_attach_aio_context(client
->ioc
, ctx
);
799 if (client
->recv_coroutine
) {
800 aio_co_schedule(ctx
, client
->recv_coroutine
);
802 if (client
->send_coroutine
) {
803 aio_co_schedule(ctx
, client
->send_coroutine
);
808 static void blk_aio_detach(void *opaque
)
810 NBDExport
*exp
= opaque
;
813 trace_nbd_blk_aio_detach(exp
->name
, exp
->ctx
);
815 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
816 qio_channel_detach_aio_context(client
->ioc
);
822 static void nbd_eject_notifier(Notifier
*n
, void *data
)
824 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
825 nbd_export_close(exp
);
828 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
, off_t size
,
829 uint16_t nbdflags
, void (*close
)(NBDExport
*),
830 bool writethrough
, BlockBackend
*on_eject_blk
,
834 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
838 /* Don't allow resize while the NBD server is running, otherwise we don't
839 * care what happens with the node. */
840 perm
= BLK_PERM_CONSISTENT_READ
;
841 if ((nbdflags
& NBD_FLAG_READ_ONLY
) == 0) {
842 perm
|= BLK_PERM_WRITE
;
844 blk
= blk_new(perm
, BLK_PERM_CONSISTENT_READ
| BLK_PERM_WRITE_UNCHANGED
|
845 BLK_PERM_WRITE
| BLK_PERM_GRAPH_MOD
);
846 ret
= blk_insert_bs(blk
, bs
, errp
);
850 blk_set_enable_write_cache(blk
, !writethrough
);
853 QTAILQ_INIT(&exp
->clients
);
855 exp
->dev_offset
= dev_offset
;
856 exp
->nbdflags
= nbdflags
;
857 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
859 error_setg_errno(errp
, -exp
->size
,
860 "Failed to determine the NBD export's length");
863 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
866 exp
->ctx
= blk_get_aio_context(blk
);
867 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
870 blk_ref(on_eject_blk
);
871 exp
->eject_notifier_blk
= on_eject_blk
;
872 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
873 blk_add_remove_bs_notifier(on_eject_blk
, &exp
->eject_notifier
);
877 * NBD exports are used for non-shared storage migration. Make sure
878 * that BDRV_O_INACTIVE is cleared and the image is ready for write
879 * access since the export could be available before migration handover.
881 aio_context_acquire(exp
->ctx
);
882 blk_invalidate_cache(blk
, NULL
);
883 aio_context_release(exp
->ctx
);
892 NBDExport
*nbd_export_find(const char *name
)
895 QTAILQ_FOREACH(exp
, &exports
, next
) {
896 if (strcmp(name
, exp
->name
) == 0) {
904 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
906 if (exp
->name
== name
) {
911 if (exp
->name
!= NULL
) {
914 QTAILQ_REMOVE(&exports
, exp
, next
);
919 exp
->name
= g_strdup(name
);
920 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
925 void nbd_export_set_description(NBDExport
*exp
, const char *description
)
927 g_free(exp
->description
);
928 exp
->description
= g_strdup(description
);
931 void nbd_export_close(NBDExport
*exp
)
933 NBDClient
*client
, *next
;
936 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
937 client_close(client
, true);
939 nbd_export_set_name(exp
, NULL
);
940 nbd_export_set_description(exp
, NULL
);
944 void nbd_export_get(NBDExport
*exp
)
946 assert(exp
->refcount
> 0);
950 void nbd_export_put(NBDExport
*exp
)
952 assert(exp
->refcount
> 0);
953 if (exp
->refcount
== 1) {
954 nbd_export_close(exp
);
957 if (--exp
->refcount
== 0) {
958 assert(exp
->name
== NULL
);
959 assert(exp
->description
== NULL
);
966 if (exp
->eject_notifier_blk
) {
967 notifier_remove(&exp
->eject_notifier
);
968 blk_unref(exp
->eject_notifier_blk
);
970 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
971 blk_aio_detach
, exp
);
980 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
985 void nbd_export_close_all(void)
987 NBDExport
*exp
, *next
;
989 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
990 nbd_export_close(exp
);
994 static int nbd_co_send_reply(NBDRequestData
*req
, NBDReply
*reply
, int len
,
997 NBDClient
*client
= req
->client
;
1000 g_assert(qemu_in_coroutine());
1002 trace_nbd_co_send_reply(reply
->handle
, reply
->error
, len
);
1004 qemu_co_mutex_lock(&client
->send_lock
);
1005 client
->send_coroutine
= qemu_coroutine_self();
1008 ret
= nbd_send_reply(client
->ioc
, reply
, errp
);
1010 qio_channel_set_cork(client
->ioc
, true);
1011 ret
= nbd_send_reply(client
->ioc
, reply
, errp
);
1013 ret
= nbd_write(client
->ioc
, req
->data
, len
, errp
);
1018 qio_channel_set_cork(client
->ioc
, false);
1021 client
->send_coroutine
= NULL
;
1022 qemu_co_mutex_unlock(&client
->send_lock
);
1026 /* nbd_co_receive_request
1027 * Collect a client request. Return 0 if request looks valid, -EIO to drop
1028 * connection right away, and any other negative value to report an error to
1029 * the client (although the caller may still need to disconnect after reporting
1032 static int nbd_co_receive_request(NBDRequestData
*req
, NBDRequest
*request
,
1035 NBDClient
*client
= req
->client
;
1037 g_assert(qemu_in_coroutine());
1038 assert(client
->recv_coroutine
== qemu_coroutine_self());
1039 if (nbd_receive_request(client
->ioc
, request
, errp
) < 0) {
1043 trace_nbd_co_receive_request_decode_type(request
->handle
, request
->type
);
1045 if (request
->type
!= NBD_CMD_WRITE
) {
1046 /* No payload, we are ready to read the next request. */
1047 req
->complete
= true;
1050 if (request
->type
== NBD_CMD_DISC
) {
1051 /* Special case: we're going to disconnect without a reply,
1052 * whether or not flags, from, or len are bogus */
1056 /* Check for sanity in the parameters, part 1. Defer as many
1057 * checks as possible until after reading any NBD_CMD_WRITE
1058 * payload, so we can try and keep the connection alive. */
1059 if ((request
->from
+ request
->len
) < request
->from
) {
1061 "integer overflow detected, you're probably being attacked");
1065 if (request
->type
== NBD_CMD_READ
|| request
->type
== NBD_CMD_WRITE
) {
1066 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1067 error_setg(errp
, "len (%" PRIu32
" ) is larger than max len (%u)",
1068 request
->len
, NBD_MAX_BUFFER_SIZE
);
1072 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
1073 if (req
->data
== NULL
) {
1074 error_setg(errp
, "No memory");
1078 if (request
->type
== NBD_CMD_WRITE
) {
1079 if (nbd_read(client
->ioc
, req
->data
, request
->len
, errp
) < 0) {
1080 error_prepend(errp
, "reading from socket failed: ");
1083 req
->complete
= true;
1085 trace_nbd_co_receive_request_payload_received(request
->handle
,
1089 /* Sanity checks, part 2. */
1090 if (request
->from
+ request
->len
> client
->exp
->size
) {
1091 error_setg(errp
, "operation past EOF; From: %" PRIu64
", Len: %" PRIu32
1092 ", Size: %" PRIu64
, request
->from
, request
->len
,
1093 (uint64_t)client
->exp
->size
);
1094 return request
->type
== NBD_CMD_WRITE
? -ENOSPC
: -EINVAL
;
1096 if (request
->flags
& ~(NBD_CMD_FLAG_FUA
| NBD_CMD_FLAG_NO_HOLE
)) {
1097 error_setg(errp
, "unsupported flags (got 0x%x)", request
->flags
);
1100 if (request
->type
!= NBD_CMD_WRITE_ZEROES
&&
1101 (request
->flags
& NBD_CMD_FLAG_NO_HOLE
)) {
1102 error_setg(errp
, "unexpected flags (got 0x%x)", request
->flags
);
1109 /* Owns a reference to the NBDClient passed as opaque. */
1110 static coroutine_fn
void nbd_trip(void *opaque
)
1112 NBDClient
*client
= opaque
;
1113 NBDExport
*exp
= client
->exp
;
1114 NBDRequestData
*req
;
1115 NBDRequest request
= { 0 }; /* GCC thinks it can be used uninitialized */
1119 int reply_data_len
= 0;
1120 Error
*local_err
= NULL
;
1123 if (client
->closing
) {
1124 nbd_client_put(client
);
1128 req
= nbd_request_get(client
);
1129 ret
= nbd_co_receive_request(req
, &request
, &local_err
);
1130 client
->recv_coroutine
= NULL
;
1131 nbd_client_receive_next_request(client
);
1136 reply
.handle
= request
.handle
;
1144 if (client
->closing
) {
1146 * The client may be closed when we are blocked in
1147 * nbd_co_receive_request()
1152 switch (request
.type
) {
1154 /* XXX: NBD Protocol only documents use of FUA with WRITE */
1155 if (request
.flags
& NBD_CMD_FLAG_FUA
) {
1156 ret
= blk_co_flush(exp
->blk
);
1158 error_setg_errno(&local_err
, -ret
, "flush failed");
1164 ret
= blk_pread(exp
->blk
, request
.from
+ exp
->dev_offset
,
1165 req
->data
, request
.len
);
1167 error_setg_errno(&local_err
, -ret
, "reading from file failed");
1172 reply_data_len
= request
.len
;
1176 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1177 reply
.error
= EROFS
;
1182 if (request
.flags
& NBD_CMD_FLAG_FUA
) {
1183 flags
|= BDRV_REQ_FUA
;
1185 ret
= blk_pwrite(exp
->blk
, request
.from
+ exp
->dev_offset
,
1186 req
->data
, request
.len
, flags
);
1188 error_setg_errno(&local_err
, -ret
, "writing to file failed");
1193 case NBD_CMD_WRITE_ZEROES
:
1194 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1195 error_setg(&local_err
, "Server is read-only, return error");
1196 reply
.error
= EROFS
;
1201 if (request
.flags
& NBD_CMD_FLAG_FUA
) {
1202 flags
|= BDRV_REQ_FUA
;
1204 if (!(request
.flags
& NBD_CMD_FLAG_NO_HOLE
)) {
1205 flags
|= BDRV_REQ_MAY_UNMAP
;
1207 ret
= blk_pwrite_zeroes(exp
->blk
, request
.from
+ exp
->dev_offset
,
1208 request
.len
, flags
);
1210 error_setg_errno(&local_err
, -ret
, "writing to file failed");
1216 /* unreachable, thanks to special case in nbd_co_receive_request() */
1220 ret
= blk_co_flush(exp
->blk
);
1222 error_setg_errno(&local_err
, -ret
, "flush failed");
1228 ret
= blk_co_pdiscard(exp
->blk
, request
.from
+ exp
->dev_offset
,
1231 error_setg_errno(&local_err
, -ret
, "discard failed");
1237 error_setg(&local_err
, "invalid request type (%" PRIu32
") received",
1239 reply
.error
= EINVAL
;
1244 /* If we are here local_err is not fatal error, already stored in
1246 error_report_err(local_err
);
1250 if (nbd_co_send_reply(req
, &reply
, reply_data_len
, &local_err
) < 0) {
1251 error_prepend(&local_err
, "Failed to send reply: ");
1255 /* We must disconnect after NBD_CMD_WRITE if we did not
1258 if (!req
->complete
) {
1259 error_setg(&local_err
, "Request handling failed in intermediate state");
1264 nbd_request_put(req
);
1265 nbd_client_put(client
);
1270 error_reportf_err(local_err
, "Disconnect client, due to: ");
1272 nbd_request_put(req
);
1273 client_close(client
, true);
1274 nbd_client_put(client
);
1277 static void nbd_client_receive_next_request(NBDClient
*client
)
1279 if (!client
->recv_coroutine
&& client
->nb_requests
< MAX_NBD_REQUESTS
) {
1280 nbd_client_get(client
);
1281 client
->recv_coroutine
= qemu_coroutine_create(nbd_trip
, client
);
1282 aio_co_schedule(client
->exp
->ctx
, client
->recv_coroutine
);
1286 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1288 NBDClient
*client
= opaque
;
1289 NBDExport
*exp
= client
->exp
;
1290 Error
*local_err
= NULL
;
1293 nbd_export_get(exp
);
1294 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1296 qemu_co_mutex_init(&client
->send_lock
);
1298 if (nbd_negotiate(client
, &local_err
)) {
1300 error_report_err(local_err
);
1302 client_close(client
, false);
1306 nbd_client_receive_next_request(client
);
1310 * Create a new client listener on the given export @exp, using the
1311 * given channel @sioc. Begin servicing it in a coroutine. When the
1312 * connection closes, call @close_fn with an indication of whether the
1313 * client completed negotiation.
1315 void nbd_client_new(NBDExport
*exp
,
1316 QIOChannelSocket
*sioc
,
1317 QCryptoTLSCreds
*tlscreds
,
1318 const char *tlsaclname
,
1319 void (*close_fn
)(NBDClient
*, bool))
1324 client
= g_malloc0(sizeof(NBDClient
));
1325 client
->refcount
= 1;
1327 client
->tlscreds
= tlscreds
;
1329 object_ref(OBJECT(client
->tlscreds
));
1331 client
->tlsaclname
= g_strdup(tlsaclname
);
1332 client
->sioc
= sioc
;
1333 object_ref(OBJECT(client
->sioc
));
1334 client
->ioc
= QIO_CHANNEL(sioc
);
1335 object_ref(OBJECT(client
->ioc
));
1336 client
->close_fn
= close_fn
;
1338 co
= qemu_coroutine_create(nbd_co_client_start
, client
);
1339 qemu_coroutine_enter(co
);