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
)
46 /* Definitions for opaque data types */
48 typedef struct NBDRequest NBDRequest
;
51 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
58 void (*close
)(NBDExport
*exp
);
65 QTAILQ_HEAD(, NBDClient
) clients
;
66 QTAILQ_ENTRY(NBDExport
) next
;
70 Notifier eject_notifier
;
73 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
77 void (*close
)(NBDClient
*client
);
80 QCryptoTLSCreds
*tlscreds
;
82 QIOChannelSocket
*sioc
; /* The underlying data channel */
83 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
85 Coroutine
*recv_coroutine
;
88 Coroutine
*send_coroutine
;
92 QTAILQ_ENTRY(NBDClient
) next
;
97 /* That's all folks */
99 static void nbd_set_handlers(NBDClient
*client
);
100 static void nbd_unset_handlers(NBDClient
*client
);
101 static void nbd_update_can_read(NBDClient
*client
);
103 static gboolean
nbd_negotiate_continue(QIOChannel
*ioc
,
104 GIOCondition condition
,
107 qemu_coroutine_enter(opaque
, NULL
);
111 static ssize_t
nbd_negotiate_read(QIOChannel
*ioc
, void *buffer
, size_t size
)
116 assert(qemu_in_coroutine());
117 /* Negotiation are always in main loop. */
118 watch
= qio_channel_add_watch(ioc
,
120 nbd_negotiate_continue
,
121 qemu_coroutine_self(),
123 ret
= read_sync(ioc
, buffer
, size
);
124 g_source_remove(watch
);
129 static ssize_t
nbd_negotiate_write(QIOChannel
*ioc
, void *buffer
, size_t size
)
134 assert(qemu_in_coroutine());
135 /* Negotiation are always in main loop. */
136 watch
= qio_channel_add_watch(ioc
,
138 nbd_negotiate_continue
,
139 qemu_coroutine_self(),
141 ret
= write_sync(ioc
, buffer
, size
);
142 g_source_remove(watch
);
146 static ssize_t
nbd_negotiate_drop_sync(QIOChannel
*ioc
, size_t size
)
148 ssize_t ret
, dropped
= size
;
149 uint8_t *buffer
= g_malloc(MIN(65536, size
));
152 ret
= nbd_negotiate_read(ioc
, buffer
, MIN(65536, size
));
166 /* Basic flow for negotiation
193 static int nbd_negotiate_send_rep(QIOChannel
*ioc
, uint32_t type
, uint32_t opt
)
198 TRACE("Reply opt=%x type=%x", type
, opt
);
200 magic
= cpu_to_be64(NBD_REP_MAGIC
);
201 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
202 LOG("write failed (rep magic)");
205 opt
= cpu_to_be32(opt
);
206 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
207 LOG("write failed (rep opt)");
210 type
= cpu_to_be32(type
);
211 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
212 LOG("write failed (rep type)");
215 len
= cpu_to_be32(0);
216 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
217 LOG("write failed (rep data length)");
223 static int nbd_negotiate_send_rep_list(QIOChannel
*ioc
, NBDExport
*exp
)
225 uint64_t magic
, name_len
;
226 uint32_t opt
, type
, len
;
228 TRACE("Advertizing export name '%s'", exp
->name
? exp
->name
: "");
229 name_len
= strlen(exp
->name
);
230 magic
= cpu_to_be64(NBD_REP_MAGIC
);
231 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
232 LOG("write failed (magic)");
235 opt
= cpu_to_be32(NBD_OPT_LIST
);
236 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
237 LOG("write failed (opt)");
240 type
= cpu_to_be32(NBD_REP_SERVER
);
241 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
242 LOG("write failed (reply type)");
245 len
= cpu_to_be32(name_len
+ sizeof(len
));
246 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
247 LOG("write failed (length)");
250 len
= cpu_to_be32(name_len
);
251 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
252 LOG("write failed (length)");
255 if (nbd_negotiate_write(ioc
, exp
->name
, name_len
) != name_len
) {
256 LOG("write failed (buffer)");
262 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
267 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
270 return nbd_negotiate_send_rep(client
->ioc
,
271 NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
274 /* For each export, send a NBD_REP_SERVER reply. */
275 QTAILQ_FOREACH(exp
, &exports
, next
) {
276 if (nbd_negotiate_send_rep_list(client
->ioc
, exp
)) {
280 /* Finish with a NBD_REP_ACK. */
281 return nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_LIST
);
284 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
290 [20 .. xx] export name (length bytes)
292 TRACE("Checking length");
294 LOG("Bad length received");
297 if (nbd_negotiate_read(client
->ioc
, name
, length
) != length
) {
303 TRACE("Client requested export '%s'", name
);
305 client
->exp
= nbd_export_find(name
);
307 LOG("export not found");
311 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
312 nbd_export_get(client
->exp
);
319 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
324 struct NBDTLSHandshakeData data
= { 0 };
326 TRACE("Setting up TLS");
329 if (nbd_negotiate_drop_sync(ioc
, length
) != length
) {
332 nbd_negotiate_send_rep(ioc
, NBD_REP_ERR_INVALID
, NBD_OPT_STARTTLS
);
336 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_STARTTLS
);
338 tioc
= qio_channel_tls_new_server(ioc
,
346 TRACE("Starting TLS handshake");
347 data
.loop
= g_main_loop_new(g_main_context_default(), FALSE
);
348 qio_channel_tls_handshake(tioc
,
353 if (!data
.complete
) {
354 g_main_loop_run(data
.loop
);
356 g_main_loop_unref(data
.loop
);
358 object_unref(OBJECT(tioc
));
359 error_free(data
.error
);
363 return QIO_CHANNEL(tioc
);
367 static int nbd_negotiate_options(NBDClient
*client
)
370 bool fixedNewstyle
= false;
373 [ 0 .. 3] client flags
375 [ 0 .. 7] NBD_OPTS_MAGIC
376 [ 8 .. 11] NBD option
377 [12 .. 15] Data length
380 [ 0 .. 7] NBD_OPTS_MAGIC
381 [ 8 .. 11] Second NBD option
382 [12 .. 15] Data length
386 if (nbd_negotiate_read(client
->ioc
, &flags
, sizeof(flags
)) !=
391 TRACE("Checking client flags");
392 be32_to_cpus(&flags
);
393 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
394 TRACE("Support supports fixed newstyle handshake");
395 fixedNewstyle
= true;
396 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
399 TRACE("Unknown client flags 0x%x received", flags
);
405 uint32_t clientflags
, length
;
408 if (nbd_negotiate_read(client
->ioc
, &magic
, sizeof(magic
)) !=
413 TRACE("Checking opts magic");
414 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
415 LOG("Bad magic received");
419 if (nbd_negotiate_read(client
->ioc
, &clientflags
,
420 sizeof(clientflags
)) != sizeof(clientflags
)) {
424 clientflags
= be32_to_cpu(clientflags
);
426 if (nbd_negotiate_read(client
->ioc
, &length
, sizeof(length
)) !=
431 length
= be32_to_cpu(length
);
433 TRACE("Checking option 0x%x", clientflags
);
434 if (client
->tlscreds
&&
435 client
->ioc
== (QIOChannel
*)client
->sioc
) {
437 if (!fixedNewstyle
) {
438 TRACE("Unsupported option 0x%x", clientflags
);
441 switch (clientflags
) {
442 case NBD_OPT_STARTTLS
:
443 tioc
= nbd_negotiate_handle_starttls(client
, length
);
447 object_unref(OBJECT(client
->ioc
));
448 client
->ioc
= QIO_CHANNEL(tioc
);
452 TRACE("Option 0x%x not permitted before TLS", clientflags
);
453 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_TLS_REQD
,
457 } else if (fixedNewstyle
) {
458 switch (clientflags
) {
460 ret
= nbd_negotiate_handle_list(client
, length
);
469 case NBD_OPT_EXPORT_NAME
:
470 return nbd_negotiate_handle_export_name(client
, length
);
472 case NBD_OPT_STARTTLS
:
473 if (client
->tlscreds
) {
474 TRACE("TLS already enabled");
475 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_INVALID
,
478 TRACE("TLS not configured");
479 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_POLICY
,
484 TRACE("Unsupported option 0x%x", clientflags
);
485 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_UNSUP
,
491 * If broken new-style we should drop the connection
492 * for anything except NBD_OPT_EXPORT_NAME
494 switch (clientflags
) {
495 case NBD_OPT_EXPORT_NAME
:
496 return nbd_negotiate_handle_export_name(client
, length
);
499 TRACE("Unsupported option 0x%x", clientflags
);
511 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
513 NBDClient
*client
= data
->client
;
514 char buf
[8 + 8 + 8 + 128];
516 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
517 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
520 /* Old style negotiation header without options
521 [ 0 .. 7] passwd ("NBDMAGIC")
522 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
524 [24 .. 25] server flags (0)
525 [26 .. 27] export flags
526 [28 .. 151] reserved (0)
528 New style negotiation header with options
529 [ 0 .. 7] passwd ("NBDMAGIC")
530 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
531 [16 .. 17] server flags (0)
534 [26 .. 27] export flags
535 [28 .. 151] reserved (0)
538 qio_channel_set_blocking(client
->ioc
, false, NULL
);
541 TRACE("Beginning negotiation.");
542 memset(buf
, 0, sizeof(buf
));
543 memcpy(buf
, "NBDMAGIC", 8);
545 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
547 assert ((client
->exp
->nbdflags
& ~65535) == 0);
548 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
549 stq_be_p(buf
+ 16, client
->exp
->size
);
550 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
552 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
553 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
);
557 if (client
->tlscreds
) {
558 TRACE("TLS cannot be enabled with oldstyle protocol");
561 if (nbd_negotiate_write(client
->ioc
, buf
, sizeof(buf
)) != sizeof(buf
)) {
566 if (nbd_negotiate_write(client
->ioc
, buf
, 18) != 18) {
570 rc
= nbd_negotiate_options(client
);
572 LOG("option negotiation failed");
576 assert ((client
->exp
->nbdflags
& ~65535) == 0);
577 stq_be_p(buf
+ 18, client
->exp
->size
);
578 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
579 if (nbd_negotiate_write(client
->ioc
, buf
+ 18, sizeof(buf
) - 18) !=
586 TRACE("Negotiation succeeded.");
594 int nbd_disconnect(int fd
)
596 ioctl(fd
, NBD_CLEAR_QUE
);
597 ioctl(fd
, NBD_DISCONNECT
);
598 ioctl(fd
, NBD_CLEAR_SOCK
);
604 int nbd_disconnect(int fd
)
610 static ssize_t
nbd_receive_request(QIOChannel
*ioc
, struct nbd_request
*request
)
612 uint8_t buf
[NBD_REQUEST_SIZE
];
616 ret
= read_sync(ioc
, buf
, sizeof(buf
));
621 if (ret
!= sizeof(buf
)) {
627 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
628 [ 4 .. 7] type (0 == READ, 1 == WRITE)
634 magic
= be32_to_cpup((uint32_t*)buf
);
635 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
636 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
637 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
638 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
640 TRACE("Got request: "
641 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
642 magic
, request
->type
, request
->from
, request
->len
);
644 if (magic
!= NBD_REQUEST_MAGIC
) {
645 LOG("invalid magic (got 0x%x)", magic
);
651 static ssize_t
nbd_send_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
653 uint8_t buf
[NBD_REPLY_SIZE
];
656 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
659 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
660 [ 4 .. 7] error (0 == no error)
663 stl_be_p(buf
, NBD_REPLY_MAGIC
);
664 stl_be_p(buf
+ 4, reply
->error
);
665 stq_be_p(buf
+ 8, reply
->handle
);
667 TRACE("Sending response to client");
669 ret
= write_sync(ioc
, buf
, sizeof(buf
));
674 if (ret
!= sizeof(buf
)) {
675 LOG("writing to socket failed");
681 #define MAX_NBD_REQUESTS 16
683 void nbd_client_get(NBDClient
*client
)
688 void nbd_client_put(NBDClient
*client
)
690 if (--client
->refcount
== 0) {
691 /* The last reference should be dropped by client->close,
692 * which is called by client_close.
694 assert(client
->closing
);
696 nbd_unset_handlers(client
);
697 object_unref(OBJECT(client
->sioc
));
698 object_unref(OBJECT(client
->ioc
));
699 if (client
->tlscreds
) {
700 object_unref(OBJECT(client
->tlscreds
));
702 g_free(client
->tlsaclname
);
704 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
705 nbd_export_put(client
->exp
);
711 static void client_close(NBDClient
*client
)
713 if (client
->closing
) {
717 client
->closing
= true;
719 /* Force requests to finish. They will drop their own references,
720 * then we'll close the socket and free the NBDClient.
722 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
725 /* Also tell the client, so that they release their reference. */
727 client
->close(client
);
731 static NBDRequest
*nbd_request_get(NBDClient
*client
)
735 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
736 client
->nb_requests
++;
737 nbd_update_can_read(client
);
739 req
= g_new0(NBDRequest
, 1);
740 nbd_client_get(client
);
741 req
->client
= client
;
745 static void nbd_request_put(NBDRequest
*req
)
747 NBDClient
*client
= req
->client
;
750 qemu_vfree(req
->data
);
754 client
->nb_requests
--;
755 nbd_update_can_read(client
);
756 nbd_client_put(client
);
759 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
761 NBDExport
*exp
= opaque
;
764 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
768 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
769 nbd_set_handlers(client
);
773 static void blk_aio_detach(void *opaque
)
775 NBDExport
*exp
= opaque
;
778 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
780 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
781 nbd_unset_handlers(client
);
787 static void nbd_eject_notifier(Notifier
*n
, void *data
)
789 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
790 nbd_export_close(exp
);
793 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
794 uint32_t nbdflags
, void (*close
)(NBDExport
*),
797 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
799 QTAILQ_INIT(&exp
->clients
);
801 exp
->dev_offset
= dev_offset
;
802 exp
->nbdflags
= nbdflags
;
803 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
805 error_setg_errno(errp
, -exp
->size
,
806 "Failed to determine the NBD export's length");
809 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
812 exp
->ctx
= blk_get_aio_context(blk
);
814 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
816 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
817 blk_add_remove_bs_notifier(blk
, &exp
->eject_notifier
);
820 * NBD exports are used for non-shared storage migration. Make sure
821 * that BDRV_O_INACTIVE is cleared and the image is ready for write
822 * access since the export could be available before migration handover.
824 aio_context_acquire(exp
->ctx
);
825 blk_invalidate_cache(blk
, NULL
);
826 aio_context_release(exp
->ctx
);
834 NBDExport
*nbd_export_find(const char *name
)
837 QTAILQ_FOREACH(exp
, &exports
, next
) {
838 if (strcmp(name
, exp
->name
) == 0) {
846 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
848 if (exp
->name
== name
) {
853 if (exp
->name
!= NULL
) {
856 QTAILQ_REMOVE(&exports
, exp
, next
);
861 exp
->name
= g_strdup(name
);
862 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
867 void nbd_export_close(NBDExport
*exp
)
869 NBDClient
*client
, *next
;
872 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
873 client_close(client
);
875 nbd_export_set_name(exp
, NULL
);
879 void nbd_export_get(NBDExport
*exp
)
881 assert(exp
->refcount
> 0);
885 void nbd_export_put(NBDExport
*exp
)
887 assert(exp
->refcount
> 0);
888 if (exp
->refcount
== 1) {
889 nbd_export_close(exp
);
892 if (--exp
->refcount
== 0) {
893 assert(exp
->name
== NULL
);
900 notifier_remove(&exp
->eject_notifier
);
901 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
902 blk_aio_detach
, exp
);
911 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
916 void nbd_export_close_all(void)
918 NBDExport
*exp
, *next
;
920 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
921 nbd_export_close(exp
);
925 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
928 NBDClient
*client
= req
->client
;
931 g_assert(qemu_in_coroutine());
932 qemu_co_mutex_lock(&client
->send_lock
);
933 client
->send_coroutine
= qemu_coroutine_self();
934 nbd_set_handlers(client
);
937 rc
= nbd_send_reply(client
->ioc
, reply
);
939 qio_channel_set_cork(client
->ioc
, true);
940 rc
= nbd_send_reply(client
->ioc
, reply
);
942 ret
= write_sync(client
->ioc
, req
->data
, len
);
947 qio_channel_set_cork(client
->ioc
, false);
950 client
->send_coroutine
= NULL
;
951 nbd_set_handlers(client
);
952 qemu_co_mutex_unlock(&client
->send_lock
);
956 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
958 NBDClient
*client
= req
->client
;
962 g_assert(qemu_in_coroutine());
963 client
->recv_coroutine
= qemu_coroutine_self();
964 nbd_update_can_read(client
);
966 rc
= nbd_receive_request(client
->ioc
, request
);
974 if ((request
->from
+ request
->len
) < request
->from
) {
975 LOG("integer overflow detected! "
976 "you're probably being attacked");
981 TRACE("Decoding type");
983 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
984 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
985 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
986 LOG("len (%u) is larger than max len (%u)",
987 request
->len
, NBD_MAX_BUFFER_SIZE
);
992 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
993 if (req
->data
== NULL
) {
998 if (command
== NBD_CMD_WRITE
) {
999 TRACE("Reading %u byte(s)", request
->len
);
1001 if (read_sync(client
->ioc
, req
->data
, request
->len
) != request
->len
) {
1002 LOG("reading from socket failed");
1010 client
->recv_coroutine
= NULL
;
1011 nbd_update_can_read(client
);
1016 static void nbd_trip(void *opaque
)
1018 NBDClient
*client
= opaque
;
1019 NBDExport
*exp
= client
->exp
;
1021 struct nbd_request request
;
1022 struct nbd_reply reply
;
1026 TRACE("Reading request.");
1027 if (client
->closing
) {
1031 req
= nbd_request_get(client
);
1032 ret
= nbd_co_receive_request(req
, &request
);
1033 if (ret
== -EAGAIN
) {
1040 reply
.handle
= request
.handle
;
1047 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1048 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1049 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1050 ", Offset: %" PRIu64
"\n",
1051 request
.from
, request
.len
,
1052 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1053 LOG("requested operation past EOF--bad client?");
1054 goto invalid_request
;
1057 if (client
->closing
) {
1059 * The client may be closed when we are blocked in
1060 * nbd_co_receive_request()
1067 TRACE("Request type is READ");
1069 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1070 ret
= blk_co_flush(exp
->blk
);
1072 LOG("flush failed");
1078 ret
= blk_read(exp
->blk
,
1079 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1080 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1082 LOG("reading from file failed");
1087 TRACE("Read %u byte(s)", request
.len
);
1088 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1092 TRACE("Request type is WRITE");
1094 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1095 TRACE("Server is read-only, return error");
1096 reply
.error
= EROFS
;
1100 TRACE("Writing to device");
1102 ret
= blk_write(exp
->blk
,
1103 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1104 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1106 LOG("writing to file failed");
1111 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1112 ret
= blk_co_flush(exp
->blk
);
1114 LOG("flush failed");
1120 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1125 TRACE("Request type is DISCONNECT");
1129 TRACE("Request type is FLUSH");
1131 ret
= blk_co_flush(exp
->blk
);
1133 LOG("flush failed");
1136 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1141 TRACE("Request type is TRIM");
1142 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
1144 request
.len
/ BDRV_SECTOR_SIZE
);
1146 LOG("discard failed");
1149 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1154 LOG("invalid request type (%u) received", request
.type
);
1156 reply
.error
= EINVAL
;
1158 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1164 TRACE("Request/Reply complete");
1167 nbd_request_put(req
);
1171 nbd_request_put(req
);
1172 client_close(client
);
1175 static void nbd_read(void *opaque
)
1177 NBDClient
*client
= opaque
;
1179 if (client
->recv_coroutine
) {
1180 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1182 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1186 static void nbd_restart_write(void *opaque
)
1188 NBDClient
*client
= opaque
;
1190 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1193 static void nbd_set_handlers(NBDClient
*client
)
1195 if (client
->exp
&& client
->exp
->ctx
) {
1196 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1198 client
->can_read
? nbd_read
: NULL
,
1199 client
->send_coroutine
? nbd_restart_write
: NULL
,
1204 static void nbd_unset_handlers(NBDClient
*client
)
1206 if (client
->exp
&& client
->exp
->ctx
) {
1207 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1208 true, NULL
, NULL
, NULL
);
1212 static void nbd_update_can_read(NBDClient
*client
)
1214 bool can_read
= client
->recv_coroutine
||
1215 client
->nb_requests
< MAX_NBD_REQUESTS
;
1217 if (can_read
!= client
->can_read
) {
1218 client
->can_read
= can_read
;
1219 nbd_set_handlers(client
);
1221 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1222 * in nbd_set_handlers() will have taken care of that */
1226 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1228 NBDClientNewData
*data
= opaque
;
1229 NBDClient
*client
= data
->client
;
1230 NBDExport
*exp
= client
->exp
;
1233 nbd_export_get(exp
);
1235 if (nbd_negotiate(data
)) {
1236 client_close(client
);
1239 qemu_co_mutex_init(&client
->send_lock
);
1240 nbd_set_handlers(client
);
1243 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1249 void nbd_client_new(NBDExport
*exp
,
1250 QIOChannelSocket
*sioc
,
1251 QCryptoTLSCreds
*tlscreds
,
1252 const char *tlsaclname
,
1253 void (*close_fn
)(NBDClient
*))
1256 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1258 client
= g_malloc0(sizeof(NBDClient
));
1259 client
->refcount
= 1;
1261 client
->tlscreds
= tlscreds
;
1263 object_ref(OBJECT(client
->tlscreds
));
1265 client
->tlsaclname
= g_strdup(tlsaclname
);
1266 client
->sioc
= sioc
;
1267 object_ref(OBJECT(client
->sioc
));
1268 client
->ioc
= QIO_CHANNEL(sioc
);
1269 object_ref(OBJECT(client
->ioc
));
1270 client
->can_read
= true;
1271 client
->close
= close_fn
;
1273 data
->client
= client
;
1274 data
->co
= qemu_coroutine_create(nbd_co_client_start
);
1275 qemu_coroutine_enter(data
->co
, data
);