2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 * Network Block Device Server Side
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "nbd-internal.h"
23 static int system_errno_to_nbd_errno(int err
)
47 /* Definitions for opaque data types */
49 typedef struct NBDRequest NBDRequest
;
52 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
60 void (*close
)(NBDExport
*exp
);
67 QTAILQ_HEAD(, NBDClient
) clients
;
68 QTAILQ_ENTRY(NBDExport
) next
;
72 Notifier eject_notifier
;
75 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
79 void (*close
)(NBDClient
*client
);
82 QCryptoTLSCreds
*tlscreds
;
84 QIOChannelSocket
*sioc
; /* The underlying data channel */
85 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
87 Coroutine
*recv_coroutine
;
90 Coroutine
*send_coroutine
;
94 QTAILQ_ENTRY(NBDClient
) next
;
99 /* That's all folks */
101 static void nbd_set_handlers(NBDClient
*client
);
102 static void nbd_unset_handlers(NBDClient
*client
);
103 static void nbd_update_can_read(NBDClient
*client
);
105 static gboolean
nbd_negotiate_continue(QIOChannel
*ioc
,
106 GIOCondition condition
,
109 qemu_coroutine_enter(opaque
);
113 static ssize_t
nbd_negotiate_read(QIOChannel
*ioc
, void *buffer
, size_t size
)
118 assert(qemu_in_coroutine());
119 /* Negotiation are always in main loop. */
120 watch
= qio_channel_add_watch(ioc
,
122 nbd_negotiate_continue
,
123 qemu_coroutine_self(),
125 ret
= read_sync(ioc
, buffer
, size
);
126 g_source_remove(watch
);
131 static ssize_t
nbd_negotiate_write(QIOChannel
*ioc
, void *buffer
, size_t size
)
136 assert(qemu_in_coroutine());
137 /* Negotiation are always in main loop. */
138 watch
= qio_channel_add_watch(ioc
,
140 nbd_negotiate_continue
,
141 qemu_coroutine_self(),
143 ret
= write_sync(ioc
, buffer
, size
);
144 g_source_remove(watch
);
148 static ssize_t
nbd_negotiate_drop_sync(QIOChannel
*ioc
, size_t size
)
150 ssize_t ret
, dropped
= size
;
151 uint8_t *buffer
= g_malloc(MIN(65536, size
));
154 ret
= nbd_negotiate_read(ioc
, buffer
, MIN(65536, size
));
168 /* Basic flow for negotiation
195 static int nbd_negotiate_send_rep(QIOChannel
*ioc
, uint32_t type
, uint32_t opt
)
200 TRACE("Reply opt=%" PRIx32
" type=%" PRIx32
, type
, opt
);
202 magic
= cpu_to_be64(NBD_REP_MAGIC
);
203 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
204 LOG("write failed (rep magic)");
207 opt
= cpu_to_be32(opt
);
208 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
209 LOG("write failed (rep opt)");
212 type
= cpu_to_be32(type
);
213 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
214 LOG("write failed (rep type)");
217 len
= cpu_to_be32(0);
218 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
219 LOG("write failed (rep data length)");
225 static int nbd_negotiate_send_rep_list(QIOChannel
*ioc
, NBDExport
*exp
)
227 uint64_t magic
, name_len
;
228 uint32_t opt
, type
, len
;
230 TRACE("Advertising export name '%s'", exp
->name
? exp
->name
: "");
231 name_len
= strlen(exp
->name
);
232 magic
= cpu_to_be64(NBD_REP_MAGIC
);
233 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
234 LOG("write failed (magic)");
237 opt
= cpu_to_be32(NBD_OPT_LIST
);
238 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
239 LOG("write failed (opt)");
242 type
= cpu_to_be32(NBD_REP_SERVER
);
243 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
244 LOG("write failed (reply type)");
247 len
= cpu_to_be32(name_len
+ sizeof(len
));
248 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
249 LOG("write failed (length)");
252 len
= cpu_to_be32(name_len
);
253 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
254 LOG("write failed (length)");
257 if (nbd_negotiate_write(ioc
, exp
->name
, name_len
) != name_len
) {
258 LOG("write failed (buffer)");
264 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
269 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
272 return nbd_negotiate_send_rep(client
->ioc
,
273 NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
276 /* For each export, send a NBD_REP_SERVER reply. */
277 QTAILQ_FOREACH(exp
, &exports
, next
) {
278 if (nbd_negotiate_send_rep_list(client
->ioc
, exp
)) {
282 /* Finish with a NBD_REP_ACK. */
283 return nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_LIST
);
286 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
289 char name
[NBD_MAX_NAME_SIZE
+ 1];
292 [20 .. xx] export name (length bytes)
294 TRACE("Checking length");
295 if (length
>= sizeof(name
)) {
296 LOG("Bad length received");
299 if (nbd_negotiate_read(client
->ioc
, name
, length
) != length
) {
305 TRACE("Client requested export '%s'", name
);
307 client
->exp
= nbd_export_find(name
);
309 LOG("export not found");
313 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
314 nbd_export_get(client
->exp
);
321 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
326 struct NBDTLSHandshakeData data
= { 0 };
328 TRACE("Setting up TLS");
331 if (nbd_negotiate_drop_sync(ioc
, length
) != length
) {
334 nbd_negotiate_send_rep(ioc
, NBD_REP_ERR_INVALID
, NBD_OPT_STARTTLS
);
338 if (nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
,
339 NBD_OPT_STARTTLS
) < 0) {
343 tioc
= qio_channel_tls_new_server(ioc
,
351 TRACE("Starting TLS handshake");
352 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
353 qio_channel_tls_handshake(tioc
,
358 if (!data
.complete
) {
359 g_main_loop_run(data
.loop
);
361 g_main_loop_unref(data
.loop
);
363 object_unref(OBJECT(tioc
));
364 error_free(data
.error
);
368 return QIO_CHANNEL(tioc
);
372 static int nbd_negotiate_options(NBDClient
*client
)
375 bool fixedNewstyle
= false;
378 [ 0 .. 3] client flags
380 [ 0 .. 7] NBD_OPTS_MAGIC
381 [ 8 .. 11] NBD option
382 [12 .. 15] Data length
385 [ 0 .. 7] NBD_OPTS_MAGIC
386 [ 8 .. 11] Second NBD option
387 [12 .. 15] Data length
391 if (nbd_negotiate_read(client
->ioc
, &flags
, sizeof(flags
)) !=
396 TRACE("Checking client flags");
397 be32_to_cpus(&flags
);
398 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
399 TRACE("Client supports fixed newstyle handshake");
400 fixedNewstyle
= true;
401 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
404 TRACE("Unknown client flags 0x%" PRIx32
" received", flags
);
410 uint32_t clientflags
, length
;
413 if (nbd_negotiate_read(client
->ioc
, &magic
, sizeof(magic
)) !=
418 TRACE("Checking opts magic");
419 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
420 LOG("Bad magic received");
424 if (nbd_negotiate_read(client
->ioc
, &clientflags
,
425 sizeof(clientflags
)) != sizeof(clientflags
)) {
429 clientflags
= be32_to_cpu(clientflags
);
431 if (nbd_negotiate_read(client
->ioc
, &length
, sizeof(length
)) !=
436 length
= be32_to_cpu(length
);
438 TRACE("Checking option 0x%" PRIx32
, clientflags
);
439 if (client
->tlscreds
&&
440 client
->ioc
== (QIOChannel
*)client
->sioc
) {
442 if (!fixedNewstyle
) {
443 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
446 switch (clientflags
) {
447 case NBD_OPT_STARTTLS
:
448 tioc
= nbd_negotiate_handle_starttls(client
, length
);
452 object_unref(OBJECT(client
->ioc
));
453 client
->ioc
= QIO_CHANNEL(tioc
);
456 case NBD_OPT_EXPORT_NAME
:
457 /* No way to return an error to client, so drop connection */
458 TRACE("Option 0x%x not permitted before TLS", clientflags
);
462 TRACE("Option 0x%" PRIx32
" not permitted before TLS",
464 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
467 ret
= nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_TLS_REQD
,
474 } else if (fixedNewstyle
) {
475 switch (clientflags
) {
477 ret
= nbd_negotiate_handle_list(client
, length
);
486 case NBD_OPT_EXPORT_NAME
:
487 return nbd_negotiate_handle_export_name(client
, length
);
489 case NBD_OPT_STARTTLS
:
490 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
493 if (client
->tlscreds
) {
494 TRACE("TLS already enabled");
495 ret
= nbd_negotiate_send_rep(client
->ioc
,
499 TRACE("TLS not configured");
500 ret
= nbd_negotiate_send_rep(client
->ioc
,
509 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
510 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
513 ret
= nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_UNSUP
,
522 * If broken new-style we should drop the connection
523 * for anything except NBD_OPT_EXPORT_NAME
525 switch (clientflags
) {
526 case NBD_OPT_EXPORT_NAME
:
527 return nbd_negotiate_handle_export_name(client
, length
);
530 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
542 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
544 NBDClient
*client
= data
->client
;
545 char buf
[8 + 8 + 8 + 128];
547 const uint16_t myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
548 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
551 /* Old style negotiation header without options
552 [ 0 .. 7] passwd ("NBDMAGIC")
553 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
555 [24 .. 25] server flags (0)
556 [26 .. 27] export flags
557 [28 .. 151] reserved (0)
559 New style negotiation header with options
560 [ 0 .. 7] passwd ("NBDMAGIC")
561 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
562 [16 .. 17] server flags (0)
565 [26 .. 27] export flags
566 [28 .. 151] reserved (0)
569 qio_channel_set_blocking(client
->ioc
, false, NULL
);
572 TRACE("Beginning negotiation.");
573 memset(buf
, 0, sizeof(buf
));
574 memcpy(buf
, "NBDMAGIC", 8);
576 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
578 TRACE("advertising size %" PRIu64
" and flags %x",
579 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
580 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
581 stq_be_p(buf
+ 16, client
->exp
->size
);
582 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
584 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
585 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
);
589 if (client
->tlscreds
) {
590 TRACE("TLS cannot be enabled with oldstyle protocol");
593 if (nbd_negotiate_write(client
->ioc
, buf
, sizeof(buf
)) != sizeof(buf
)) {
598 if (nbd_negotiate_write(client
->ioc
, buf
, 18) != 18) {
602 rc
= nbd_negotiate_options(client
);
604 LOG("option negotiation failed");
608 TRACE("advertising size %" PRIu64
" and flags %x",
609 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
610 stq_be_p(buf
+ 18, client
->exp
->size
);
611 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
612 if (nbd_negotiate_write(client
->ioc
, buf
+ 18, sizeof(buf
) - 18) !=
619 TRACE("Negotiation succeeded.");
625 static ssize_t
nbd_receive_request(QIOChannel
*ioc
, struct nbd_request
*request
)
627 uint8_t buf
[NBD_REQUEST_SIZE
];
631 ret
= read_sync(ioc
, buf
, sizeof(buf
));
636 if (ret
!= sizeof(buf
)) {
642 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
643 [ 4 .. 7] type (0 == READ, 1 == WRITE)
649 magic
= ldl_be_p(buf
);
650 request
->type
= ldl_be_p(buf
+ 4);
651 request
->handle
= ldq_be_p(buf
+ 8);
652 request
->from
= ldq_be_p(buf
+ 16);
653 request
->len
= ldl_be_p(buf
+ 24);
655 TRACE("Got request: { magic = 0x%" PRIx32
", .type = %" PRIx32
656 ", from = %" PRIu64
" , len = %" PRIu32
" }",
657 magic
, request
->type
, request
->from
, request
->len
);
659 if (magic
!= NBD_REQUEST_MAGIC
) {
660 LOG("invalid magic (got 0x%" PRIx32
")", magic
);
666 static ssize_t
nbd_send_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
668 uint8_t buf
[NBD_REPLY_SIZE
];
671 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
673 TRACE("Sending response to client: { .error = %" PRId32
674 ", handle = %" PRIu64
" }",
675 reply
->error
, reply
->handle
);
678 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
679 [ 4 .. 7] error (0 == no error)
682 stl_be_p(buf
, NBD_REPLY_MAGIC
);
683 stl_be_p(buf
+ 4, reply
->error
);
684 stq_be_p(buf
+ 8, reply
->handle
);
686 ret
= write_sync(ioc
, buf
, sizeof(buf
));
691 if (ret
!= sizeof(buf
)) {
692 LOG("writing to socket failed");
698 #define MAX_NBD_REQUESTS 16
700 void nbd_client_get(NBDClient
*client
)
705 void nbd_client_put(NBDClient
*client
)
707 if (--client
->refcount
== 0) {
708 /* The last reference should be dropped by client->close,
709 * which is called by client_close.
711 assert(client
->closing
);
713 nbd_unset_handlers(client
);
714 object_unref(OBJECT(client
->sioc
));
715 object_unref(OBJECT(client
->ioc
));
716 if (client
->tlscreds
) {
717 object_unref(OBJECT(client
->tlscreds
));
719 g_free(client
->tlsaclname
);
721 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
722 nbd_export_put(client
->exp
);
728 static void client_close(NBDClient
*client
)
730 if (client
->closing
) {
734 client
->closing
= true;
736 /* Force requests to finish. They will drop their own references,
737 * then we'll close the socket and free the NBDClient.
739 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
742 /* Also tell the client, so that they release their reference. */
744 client
->close(client
);
748 static NBDRequest
*nbd_request_get(NBDClient
*client
)
752 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
753 client
->nb_requests
++;
754 nbd_update_can_read(client
);
756 req
= g_new0(NBDRequest
, 1);
757 nbd_client_get(client
);
758 req
->client
= client
;
762 static void nbd_request_put(NBDRequest
*req
)
764 NBDClient
*client
= req
->client
;
767 qemu_vfree(req
->data
);
771 client
->nb_requests
--;
772 nbd_update_can_read(client
);
773 nbd_client_put(client
);
776 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
778 NBDExport
*exp
= opaque
;
781 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
785 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
786 nbd_set_handlers(client
);
790 static void blk_aio_detach(void *opaque
)
792 NBDExport
*exp
= opaque
;
795 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
797 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
798 nbd_unset_handlers(client
);
804 static void nbd_eject_notifier(Notifier
*n
, void *data
)
806 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
807 nbd_export_close(exp
);
810 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
811 uint16_t nbdflags
, void (*close
)(NBDExport
*),
814 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
816 QTAILQ_INIT(&exp
->clients
);
818 exp
->dev_offset
= dev_offset
;
819 exp
->nbdflags
= nbdflags
;
820 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
822 error_setg_errno(errp
, -exp
->size
,
823 "Failed to determine the NBD export's length");
826 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
829 exp
->ctx
= blk_get_aio_context(blk
);
831 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
833 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
834 blk_add_remove_bs_notifier(blk
, &exp
->eject_notifier
);
837 * NBD exports are used for non-shared storage migration. Make sure
838 * that BDRV_O_INACTIVE is cleared and the image is ready for write
839 * access since the export could be available before migration handover.
841 aio_context_acquire(exp
->ctx
);
842 blk_invalidate_cache(blk
, NULL
);
843 aio_context_release(exp
->ctx
);
851 NBDExport
*nbd_export_find(const char *name
)
854 QTAILQ_FOREACH(exp
, &exports
, next
) {
855 if (strcmp(name
, exp
->name
) == 0) {
863 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
865 if (exp
->name
== name
) {
870 if (exp
->name
!= NULL
) {
873 QTAILQ_REMOVE(&exports
, exp
, next
);
878 exp
->name
= g_strdup(name
);
879 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
884 void nbd_export_close(NBDExport
*exp
)
886 NBDClient
*client
, *next
;
889 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
890 client_close(client
);
892 nbd_export_set_name(exp
, NULL
);
896 void nbd_export_get(NBDExport
*exp
)
898 assert(exp
->refcount
> 0);
902 void nbd_export_put(NBDExport
*exp
)
904 assert(exp
->refcount
> 0);
905 if (exp
->refcount
== 1) {
906 nbd_export_close(exp
);
909 if (--exp
->refcount
== 0) {
910 assert(exp
->name
== NULL
);
917 notifier_remove(&exp
->eject_notifier
);
918 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
919 blk_aio_detach
, exp
);
928 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
933 void nbd_export_close_all(void)
935 NBDExport
*exp
, *next
;
937 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
938 nbd_export_close(exp
);
942 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
945 NBDClient
*client
= req
->client
;
948 g_assert(qemu_in_coroutine());
949 qemu_co_mutex_lock(&client
->send_lock
);
950 client
->send_coroutine
= qemu_coroutine_self();
951 nbd_set_handlers(client
);
954 rc
= nbd_send_reply(client
->ioc
, reply
);
956 qio_channel_set_cork(client
->ioc
, true);
957 rc
= nbd_send_reply(client
->ioc
, reply
);
959 ret
= write_sync(client
->ioc
, req
->data
, len
);
964 qio_channel_set_cork(client
->ioc
, false);
967 client
->send_coroutine
= NULL
;
968 nbd_set_handlers(client
);
969 qemu_co_mutex_unlock(&client
->send_lock
);
973 /* Collect a client request. Return 0 if request looks valid, -EAGAIN
974 * to keep trying the collection, -EIO to drop connection right away,
975 * and any other negative value to report an error to the client
976 * (although the caller may still need to disconnect after reporting
978 static ssize_t
nbd_co_receive_request(NBDRequest
*req
,
979 struct nbd_request
*request
)
981 NBDClient
*client
= req
->client
;
985 g_assert(qemu_in_coroutine());
986 client
->recv_coroutine
= qemu_coroutine_self();
987 nbd_update_can_read(client
);
989 rc
= nbd_receive_request(client
->ioc
, request
);
997 TRACE("Decoding type");
999 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1000 if (command
!= NBD_CMD_WRITE
) {
1001 /* No payload, we are ready to read the next request. */
1002 req
->complete
= true;
1005 if (command
== NBD_CMD_DISC
) {
1006 /* Special case: we're going to disconnect without a reply,
1007 * whether or not flags, from, or len are bogus */
1008 TRACE("Request type is DISCONNECT");
1013 /* Check for sanity in the parameters, part 1. Defer as many
1014 * checks as possible until after reading any NBD_CMD_WRITE
1015 * payload, so we can try and keep the connection alive. */
1016 if ((request
->from
+ request
->len
) < request
->from
) {
1017 LOG("integer overflow detected, you're probably being attacked");
1022 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1023 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1024 LOG("len (%" PRIu32
" ) is larger than max len (%u)",
1025 request
->len
, NBD_MAX_BUFFER_SIZE
);
1030 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
1031 if (req
->data
== NULL
) {
1036 if (command
== NBD_CMD_WRITE
) {
1037 TRACE("Reading %" PRIu32
" byte(s)", request
->len
);
1039 if (read_sync(client
->ioc
, req
->data
, request
->len
) != request
->len
) {
1040 LOG("reading from socket failed");
1044 req
->complete
= true;
1047 /* Sanity checks, part 2. */
1048 if (request
->from
+ request
->len
> client
->exp
->size
) {
1049 LOG("operation past EOF; From: %" PRIu64
", Len: %" PRIu32
1050 ", Size: %" PRIu64
, request
->from
, request
->len
,
1051 (uint64_t)client
->exp
->size
);
1052 rc
= command
== NBD_CMD_WRITE
? -ENOSPC
: -EINVAL
;
1055 if (request
->type
& ~NBD_CMD_MASK_COMMAND
& ~NBD_CMD_FLAG_FUA
) {
1056 LOG("unsupported flags (got 0x%x)",
1057 request
->type
& ~NBD_CMD_MASK_COMMAND
);
1065 client
->recv_coroutine
= NULL
;
1066 nbd_update_can_read(client
);
1071 static void nbd_trip(void *opaque
)
1073 NBDClient
*client
= opaque
;
1074 NBDExport
*exp
= client
->exp
;
1076 struct nbd_request request
;
1077 struct nbd_reply reply
;
1082 TRACE("Reading request.");
1083 if (client
->closing
) {
1087 req
= nbd_request_get(client
);
1088 ret
= nbd_co_receive_request(req
, &request
);
1089 if (ret
== -EAGAIN
) {
1096 reply
.handle
= request
.handle
;
1103 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1105 if (client
->closing
) {
1107 * The client may be closed when we are blocked in
1108 * nbd_co_receive_request()
1115 TRACE("Request type is READ");
1117 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1118 ret
= blk_co_flush(exp
->blk
);
1120 LOG("flush failed");
1126 ret
= blk_pread(exp
->blk
, request
.from
+ exp
->dev_offset
,
1127 req
->data
, request
.len
);
1129 LOG("reading from file failed");
1134 TRACE("Read %" PRIu32
" byte(s)", request
.len
);
1135 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1139 TRACE("Request type is WRITE");
1141 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1142 TRACE("Server is read-only, return error");
1143 reply
.error
= EROFS
;
1147 TRACE("Writing to device");
1150 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1151 flags
|= BDRV_REQ_FUA
;
1153 ret
= blk_pwrite(exp
->blk
, request
.from
+ exp
->dev_offset
,
1154 req
->data
, request
.len
, flags
);
1156 LOG("writing to file failed");
1161 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1167 /* unreachable, thanks to special case in nbd_co_receive_request() */
1171 TRACE("Request type is FLUSH");
1173 ret
= blk_co_flush(exp
->blk
);
1175 LOG("flush failed");
1178 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1183 TRACE("Request type is TRIM");
1184 ret
= blk_co_pdiscard(exp
->blk
, request
.from
+ exp
->dev_offset
,
1187 LOG("discard failed");
1190 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1195 LOG("invalid request type (%" PRIu32
") received", request
.type
);
1196 reply
.error
= EINVAL
;
1198 /* We must disconnect after NBD_CMD_WRITE if we did not
1201 if (nbd_co_send_reply(req
, &reply
, 0) < 0 || !req
->complete
) {
1207 TRACE("Request/Reply complete");
1210 nbd_request_put(req
);
1214 nbd_request_put(req
);
1215 client_close(client
);
1218 static void nbd_read(void *opaque
)
1220 NBDClient
*client
= opaque
;
1222 if (client
->recv_coroutine
) {
1223 qemu_coroutine_enter(client
->recv_coroutine
);
1225 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
, client
));
1229 static void nbd_restart_write(void *opaque
)
1231 NBDClient
*client
= opaque
;
1233 qemu_coroutine_enter(client
->send_coroutine
);
1236 static void nbd_set_handlers(NBDClient
*client
)
1238 if (client
->exp
&& client
->exp
->ctx
) {
1239 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1241 client
->can_read
? nbd_read
: NULL
,
1242 client
->send_coroutine
? nbd_restart_write
: NULL
,
1247 static void nbd_unset_handlers(NBDClient
*client
)
1249 if (client
->exp
&& client
->exp
->ctx
) {
1250 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1251 true, NULL
, NULL
, NULL
);
1255 static void nbd_update_can_read(NBDClient
*client
)
1257 bool can_read
= client
->recv_coroutine
||
1258 client
->nb_requests
< MAX_NBD_REQUESTS
;
1260 if (can_read
!= client
->can_read
) {
1261 client
->can_read
= can_read
;
1262 nbd_set_handlers(client
);
1264 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1265 * in nbd_set_handlers() will have taken care of that */
1269 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1271 NBDClientNewData
*data
= opaque
;
1272 NBDClient
*client
= data
->client
;
1273 NBDExport
*exp
= client
->exp
;
1276 nbd_export_get(exp
);
1278 if (nbd_negotiate(data
)) {
1279 client_close(client
);
1282 qemu_co_mutex_init(&client
->send_lock
);
1283 nbd_set_handlers(client
);
1286 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1292 void nbd_client_new(NBDExport
*exp
,
1293 QIOChannelSocket
*sioc
,
1294 QCryptoTLSCreds
*tlscreds
,
1295 const char *tlsaclname
,
1296 void (*close_fn
)(NBDClient
*))
1299 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1301 client
= g_malloc0(sizeof(NBDClient
));
1302 client
->refcount
= 1;
1304 client
->tlscreds
= tlscreds
;
1306 object_ref(OBJECT(client
->tlscreds
));
1308 client
->tlsaclname
= g_strdup(tlsaclname
);
1309 client
->sioc
= sioc
;
1310 object_ref(OBJECT(client
->sioc
));
1311 client
->ioc
= QIO_CHANNEL(sioc
);
1312 object_ref(OBJECT(client
->ioc
));
1313 client
->can_read
= true;
1314 client
->close
= close_fn
;
1316 data
->client
= client
;
1317 data
->co
= qemu_coroutine_create(nbd_co_client_start
, data
);
1318 qemu_coroutine_enter(data
->co
);