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
;
59 void (*close
)(NBDExport
*exp
);
66 QTAILQ_HEAD(, NBDClient
) clients
;
67 QTAILQ_ENTRY(NBDExport
) next
;
71 Notifier eject_notifier
;
74 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
78 void (*close
)(NBDClient
*client
);
81 QCryptoTLSCreds
*tlscreds
;
83 QIOChannelSocket
*sioc
; /* The underlying data channel */
84 QIOChannel
*ioc
; /* The current I/O channel which may differ (eg TLS) */
86 Coroutine
*recv_coroutine
;
89 Coroutine
*send_coroutine
;
93 QTAILQ_ENTRY(NBDClient
) next
;
98 /* That's all folks */
100 static void nbd_set_handlers(NBDClient
*client
);
101 static void nbd_unset_handlers(NBDClient
*client
);
102 static void nbd_update_can_read(NBDClient
*client
);
104 static gboolean
nbd_negotiate_continue(QIOChannel
*ioc
,
105 GIOCondition condition
,
108 qemu_coroutine_enter(opaque
, NULL
);
112 static ssize_t
nbd_negotiate_read(QIOChannel
*ioc
, void *buffer
, size_t size
)
117 assert(qemu_in_coroutine());
118 /* Negotiation are always in main loop. */
119 watch
= qio_channel_add_watch(ioc
,
121 nbd_negotiate_continue
,
122 qemu_coroutine_self(),
124 ret
= read_sync(ioc
, buffer
, size
);
125 g_source_remove(watch
);
130 static ssize_t
nbd_negotiate_write(QIOChannel
*ioc
, void *buffer
, size_t size
)
135 assert(qemu_in_coroutine());
136 /* Negotiation are always in main loop. */
137 watch
= qio_channel_add_watch(ioc
,
139 nbd_negotiate_continue
,
140 qemu_coroutine_self(),
142 ret
= write_sync(ioc
, buffer
, size
);
143 g_source_remove(watch
);
147 static ssize_t
nbd_negotiate_drop_sync(QIOChannel
*ioc
, size_t size
)
149 ssize_t ret
, dropped
= size
;
150 uint8_t *buffer
= g_malloc(MIN(65536, size
));
153 ret
= nbd_negotiate_read(ioc
, buffer
, MIN(65536, size
));
167 /* Basic flow for negotiation
194 static int nbd_negotiate_send_rep(QIOChannel
*ioc
, uint32_t type
, uint32_t opt
)
199 TRACE("Reply opt=%" PRIx32
" type=%" PRIx32
, type
, opt
);
201 magic
= cpu_to_be64(NBD_REP_MAGIC
);
202 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
203 LOG("write failed (rep magic)");
206 opt
= cpu_to_be32(opt
);
207 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
208 LOG("write failed (rep opt)");
211 type
= cpu_to_be32(type
);
212 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
213 LOG("write failed (rep type)");
216 len
= cpu_to_be32(0);
217 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
218 LOG("write failed (rep data length)");
224 static int nbd_negotiate_send_rep_list(QIOChannel
*ioc
, NBDExport
*exp
)
226 uint64_t magic
, name_len
;
227 uint32_t opt
, type
, len
;
229 TRACE("Advertising export name '%s'", exp
->name
? exp
->name
: "");
230 name_len
= strlen(exp
->name
);
231 magic
= cpu_to_be64(NBD_REP_MAGIC
);
232 if (nbd_negotiate_write(ioc
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
233 LOG("write failed (magic)");
236 opt
= cpu_to_be32(NBD_OPT_LIST
);
237 if (nbd_negotiate_write(ioc
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
238 LOG("write failed (opt)");
241 type
= cpu_to_be32(NBD_REP_SERVER
);
242 if (nbd_negotiate_write(ioc
, &type
, sizeof(type
)) != sizeof(type
)) {
243 LOG("write failed (reply type)");
246 len
= cpu_to_be32(name_len
+ sizeof(len
));
247 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
248 LOG("write failed (length)");
251 len
= cpu_to_be32(name_len
);
252 if (nbd_negotiate_write(ioc
, &len
, sizeof(len
)) != sizeof(len
)) {
253 LOG("write failed (length)");
256 if (nbd_negotiate_write(ioc
, exp
->name
, name_len
) != name_len
) {
257 LOG("write failed (buffer)");
263 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
268 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
271 return nbd_negotiate_send_rep(client
->ioc
,
272 NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
275 /* For each export, send a NBD_REP_SERVER reply. */
276 QTAILQ_FOREACH(exp
, &exports
, next
) {
277 if (nbd_negotiate_send_rep_list(client
->ioc
, exp
)) {
281 /* Finish with a NBD_REP_ACK. */
282 return nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_LIST
);
285 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
291 [20 .. xx] export name (length bytes)
293 TRACE("Checking length");
295 LOG("Bad length received");
298 if (nbd_negotiate_read(client
->ioc
, name
, length
) != length
) {
304 TRACE("Client requested export '%s'", name
);
306 client
->exp
= nbd_export_find(name
);
308 LOG("export not found");
312 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
313 nbd_export_get(client
->exp
);
320 static QIOChannel
*nbd_negotiate_handle_starttls(NBDClient
*client
,
325 struct NBDTLSHandshakeData data
= { 0 };
327 TRACE("Setting up TLS");
330 if (nbd_negotiate_drop_sync(ioc
, length
) != length
) {
333 nbd_negotiate_send_rep(ioc
, NBD_REP_ERR_INVALID
, NBD_OPT_STARTTLS
);
337 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ACK
, NBD_OPT_STARTTLS
);
339 tioc
= qio_channel_tls_new_server(ioc
,
347 TRACE("Starting TLS 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_free(data
.error
);
364 return QIO_CHANNEL(tioc
);
368 static int nbd_negotiate_options(NBDClient
*client
)
371 bool fixedNewstyle
= false;
374 [ 0 .. 3] client flags
376 [ 0 .. 7] NBD_OPTS_MAGIC
377 [ 8 .. 11] NBD option
378 [12 .. 15] Data length
381 [ 0 .. 7] NBD_OPTS_MAGIC
382 [ 8 .. 11] Second NBD option
383 [12 .. 15] Data length
387 if (nbd_negotiate_read(client
->ioc
, &flags
, sizeof(flags
)) !=
392 TRACE("Checking client flags");
393 be32_to_cpus(&flags
);
394 if (flags
& NBD_FLAG_C_FIXED_NEWSTYLE
) {
395 TRACE("Client supports fixed newstyle handshake");
396 fixedNewstyle
= true;
397 flags
&= ~NBD_FLAG_C_FIXED_NEWSTYLE
;
400 TRACE("Unknown client flags 0x%" PRIx32
" received", flags
);
406 uint32_t clientflags
, length
;
409 if (nbd_negotiate_read(client
->ioc
, &magic
, sizeof(magic
)) !=
414 TRACE("Checking opts magic");
415 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
416 LOG("Bad magic received");
420 if (nbd_negotiate_read(client
->ioc
, &clientflags
,
421 sizeof(clientflags
)) != sizeof(clientflags
)) {
425 clientflags
= be32_to_cpu(clientflags
);
427 if (nbd_negotiate_read(client
->ioc
, &length
, sizeof(length
)) !=
432 length
= be32_to_cpu(length
);
434 TRACE("Checking option 0x%" PRIx32
, clientflags
);
435 if (client
->tlscreds
&&
436 client
->ioc
== (QIOChannel
*)client
->sioc
) {
438 if (!fixedNewstyle
) {
439 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
442 switch (clientflags
) {
443 case NBD_OPT_STARTTLS
:
444 tioc
= nbd_negotiate_handle_starttls(client
, length
);
448 object_unref(OBJECT(client
->ioc
));
449 client
->ioc
= QIO_CHANNEL(tioc
);
452 case NBD_OPT_EXPORT_NAME
:
453 /* No way to return an error to client, so drop connection */
454 TRACE("Option 0x%x not permitted before TLS", clientflags
);
458 TRACE("Option 0x%" PRIx32
" not permitted before TLS",
460 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
463 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_TLS_REQD
,
467 } else if (fixedNewstyle
) {
468 switch (clientflags
) {
470 ret
= nbd_negotiate_handle_list(client
, length
);
479 case NBD_OPT_EXPORT_NAME
:
480 return nbd_negotiate_handle_export_name(client
, length
);
482 case NBD_OPT_STARTTLS
:
483 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
486 if (client
->tlscreds
) {
487 TRACE("TLS already enabled");
488 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_INVALID
,
491 TRACE("TLS not configured");
492 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_POLICY
,
497 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
498 if (nbd_negotiate_drop_sync(client
->ioc
, length
) != length
) {
501 nbd_negotiate_send_rep(client
->ioc
, NBD_REP_ERR_UNSUP
,
507 * If broken new-style we should drop the connection
508 * for anything except NBD_OPT_EXPORT_NAME
510 switch (clientflags
) {
511 case NBD_OPT_EXPORT_NAME
:
512 return nbd_negotiate_handle_export_name(client
, length
);
515 TRACE("Unsupported option 0x%" PRIx32
, clientflags
);
527 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
529 NBDClient
*client
= data
->client
;
530 char buf
[8 + 8 + 8 + 128];
532 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
533 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
536 /* Old style negotiation header without options
537 [ 0 .. 7] passwd ("NBDMAGIC")
538 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
540 [24 .. 25] server flags (0)
541 [26 .. 27] export flags
542 [28 .. 151] reserved (0)
544 New style negotiation header with options
545 [ 0 .. 7] passwd ("NBDMAGIC")
546 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
547 [16 .. 17] server flags (0)
550 [26 .. 27] export flags
551 [28 .. 151] reserved (0)
554 qio_channel_set_blocking(client
->ioc
, false, NULL
);
557 TRACE("Beginning negotiation.");
558 memset(buf
, 0, sizeof(buf
));
559 memcpy(buf
, "NBDMAGIC", 8);
561 oldStyle
= client
->exp
!= NULL
&& !client
->tlscreds
;
563 assert ((client
->exp
->nbdflags
& ~65535) == 0);
564 TRACE("advertising size %" PRIu64
" and flags %x",
565 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
566 stq_be_p(buf
+ 8, NBD_CLIENT_MAGIC
);
567 stq_be_p(buf
+ 16, client
->exp
->size
);
568 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
570 stq_be_p(buf
+ 8, NBD_OPTS_MAGIC
);
571 stw_be_p(buf
+ 16, NBD_FLAG_FIXED_NEWSTYLE
);
575 if (client
->tlscreds
) {
576 TRACE("TLS cannot be enabled with oldstyle protocol");
579 if (nbd_negotiate_write(client
->ioc
, buf
, sizeof(buf
)) != sizeof(buf
)) {
584 if (nbd_negotiate_write(client
->ioc
, buf
, 18) != 18) {
588 rc
= nbd_negotiate_options(client
);
590 LOG("option negotiation failed");
594 assert ((client
->exp
->nbdflags
& ~65535) == 0);
595 TRACE("advertising size %" PRIu64
" and flags %x",
596 client
->exp
->size
, client
->exp
->nbdflags
| myflags
);
597 stq_be_p(buf
+ 18, client
->exp
->size
);
598 stw_be_p(buf
+ 26, client
->exp
->nbdflags
| myflags
);
599 if (nbd_negotiate_write(client
->ioc
, buf
+ 18, sizeof(buf
) - 18) !=
606 TRACE("Negotiation succeeded.");
614 int nbd_disconnect(int fd
)
616 ioctl(fd
, NBD_CLEAR_QUE
);
617 ioctl(fd
, NBD_DISCONNECT
);
618 ioctl(fd
, NBD_CLEAR_SOCK
);
624 int nbd_disconnect(int fd
)
630 static ssize_t
nbd_receive_request(QIOChannel
*ioc
, struct nbd_request
*request
)
632 uint8_t buf
[NBD_REQUEST_SIZE
];
636 ret
= read_sync(ioc
, buf
, sizeof(buf
));
641 if (ret
!= sizeof(buf
)) {
647 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
648 [ 4 .. 7] type (0 == READ, 1 == WRITE)
654 magic
= be32_to_cpup((uint32_t*)buf
);
655 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
656 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
657 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
658 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
660 TRACE("Got request: { magic = 0x%" PRIx32
", .type = %" PRIx32
661 ", from = %" PRIu64
" , len = %" PRIu32
" }",
662 magic
, request
->type
, request
->from
, request
->len
);
664 if (magic
!= NBD_REQUEST_MAGIC
) {
665 LOG("invalid magic (got 0x%" PRIx32
")", magic
);
671 static ssize_t
nbd_send_reply(QIOChannel
*ioc
, struct nbd_reply
*reply
)
673 uint8_t buf
[NBD_REPLY_SIZE
];
676 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
678 TRACE("Sending response to client: { .error = %" PRId32
679 ", handle = %" PRIu64
" }",
680 reply
->error
, reply
->handle
);
683 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
684 [ 4 .. 7] error (0 == no error)
687 stl_be_p(buf
, NBD_REPLY_MAGIC
);
688 stl_be_p(buf
+ 4, reply
->error
);
689 stq_be_p(buf
+ 8, reply
->handle
);
691 ret
= write_sync(ioc
, buf
, sizeof(buf
));
696 if (ret
!= sizeof(buf
)) {
697 LOG("writing to socket failed");
703 #define MAX_NBD_REQUESTS 16
705 void nbd_client_get(NBDClient
*client
)
710 void nbd_client_put(NBDClient
*client
)
712 if (--client
->refcount
== 0) {
713 /* The last reference should be dropped by client->close,
714 * which is called by client_close.
716 assert(client
->closing
);
718 nbd_unset_handlers(client
);
719 object_unref(OBJECT(client
->sioc
));
720 object_unref(OBJECT(client
->ioc
));
721 if (client
->tlscreds
) {
722 object_unref(OBJECT(client
->tlscreds
));
724 g_free(client
->tlsaclname
);
726 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
727 nbd_export_put(client
->exp
);
733 static void client_close(NBDClient
*client
)
735 if (client
->closing
) {
739 client
->closing
= true;
741 /* Force requests to finish. They will drop their own references,
742 * then we'll close the socket and free the NBDClient.
744 qio_channel_shutdown(client
->ioc
, QIO_CHANNEL_SHUTDOWN_BOTH
,
747 /* Also tell the client, so that they release their reference. */
749 client
->close(client
);
753 static NBDRequest
*nbd_request_get(NBDClient
*client
)
757 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
758 client
->nb_requests
++;
759 nbd_update_can_read(client
);
761 req
= g_new0(NBDRequest
, 1);
762 nbd_client_get(client
);
763 req
->client
= client
;
767 static void nbd_request_put(NBDRequest
*req
)
769 NBDClient
*client
= req
->client
;
772 qemu_vfree(req
->data
);
776 client
->nb_requests
--;
777 nbd_update_can_read(client
);
778 nbd_client_put(client
);
781 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
783 NBDExport
*exp
= opaque
;
786 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
790 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
791 nbd_set_handlers(client
);
795 static void blk_aio_detach(void *opaque
)
797 NBDExport
*exp
= opaque
;
800 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
802 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
803 nbd_unset_handlers(client
);
809 static void nbd_eject_notifier(Notifier
*n
, void *data
)
811 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
812 nbd_export_close(exp
);
815 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
816 uint32_t nbdflags
, void (*close
)(NBDExport
*),
819 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
821 QTAILQ_INIT(&exp
->clients
);
823 exp
->dev_offset
= dev_offset
;
824 exp
->nbdflags
= nbdflags
;
825 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
827 error_setg_errno(errp
, -exp
->size
,
828 "Failed to determine the NBD export's length");
831 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
834 exp
->ctx
= blk_get_aio_context(blk
);
836 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
838 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
839 blk_add_remove_bs_notifier(blk
, &exp
->eject_notifier
);
842 * NBD exports are used for non-shared storage migration. Make sure
843 * that BDRV_O_INACTIVE is cleared and the image is ready for write
844 * access since the export could be available before migration handover.
846 aio_context_acquire(exp
->ctx
);
847 blk_invalidate_cache(blk
, NULL
);
848 aio_context_release(exp
->ctx
);
856 NBDExport
*nbd_export_find(const char *name
)
859 QTAILQ_FOREACH(exp
, &exports
, next
) {
860 if (strcmp(name
, exp
->name
) == 0) {
868 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
870 if (exp
->name
== name
) {
875 if (exp
->name
!= NULL
) {
878 QTAILQ_REMOVE(&exports
, exp
, next
);
883 exp
->name
= g_strdup(name
);
884 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
889 void nbd_export_close(NBDExport
*exp
)
891 NBDClient
*client
, *next
;
894 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
895 client_close(client
);
897 nbd_export_set_name(exp
, NULL
);
901 void nbd_export_get(NBDExport
*exp
)
903 assert(exp
->refcount
> 0);
907 void nbd_export_put(NBDExport
*exp
)
909 assert(exp
->refcount
> 0);
910 if (exp
->refcount
== 1) {
911 nbd_export_close(exp
);
914 if (--exp
->refcount
== 0) {
915 assert(exp
->name
== NULL
);
922 notifier_remove(&exp
->eject_notifier
);
923 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
924 blk_aio_detach
, exp
);
933 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
938 void nbd_export_close_all(void)
940 NBDExport
*exp
, *next
;
942 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
943 nbd_export_close(exp
);
947 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
950 NBDClient
*client
= req
->client
;
953 g_assert(qemu_in_coroutine());
954 qemu_co_mutex_lock(&client
->send_lock
);
955 client
->send_coroutine
= qemu_coroutine_self();
956 nbd_set_handlers(client
);
959 rc
= nbd_send_reply(client
->ioc
, reply
);
961 qio_channel_set_cork(client
->ioc
, true);
962 rc
= nbd_send_reply(client
->ioc
, reply
);
964 ret
= write_sync(client
->ioc
, req
->data
, len
);
969 qio_channel_set_cork(client
->ioc
, false);
972 client
->send_coroutine
= NULL
;
973 nbd_set_handlers(client
);
974 qemu_co_mutex_unlock(&client
->send_lock
);
978 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
980 NBDClient
*client
= req
->client
;
984 g_assert(qemu_in_coroutine());
985 client
->recv_coroutine
= qemu_coroutine_self();
986 nbd_update_can_read(client
);
988 rc
= nbd_receive_request(client
->ioc
, request
);
996 if ((request
->from
+ request
->len
) < request
->from
) {
997 LOG("integer overflow detected! "
998 "you're probably being attacked");
1003 TRACE("Decoding type");
1005 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1006 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1007 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1008 LOG("len (%" PRIu32
" ) is larger than max len (%u)",
1009 request
->len
, NBD_MAX_BUFFER_SIZE
);
1014 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
1015 if (req
->data
== NULL
) {
1020 if (command
== NBD_CMD_WRITE
) {
1021 TRACE("Reading %" PRIu32
" byte(s)", request
->len
);
1023 if (read_sync(client
->ioc
, req
->data
, request
->len
) != request
->len
) {
1024 LOG("reading from socket failed");
1032 client
->recv_coroutine
= NULL
;
1033 nbd_update_can_read(client
);
1038 static void nbd_trip(void *opaque
)
1040 NBDClient
*client
= opaque
;
1041 NBDExport
*exp
= client
->exp
;
1043 struct nbd_request request
;
1044 struct nbd_reply reply
;
1048 TRACE("Reading request.");
1049 if (client
->closing
) {
1053 req
= nbd_request_get(client
);
1054 ret
= nbd_co_receive_request(req
, &request
);
1055 if (ret
== -EAGAIN
) {
1062 reply
.handle
= request
.handle
;
1069 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1070 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1071 LOG("From: %" PRIu64
", Len: %" PRIu32
", Size: %" PRIu64
1072 ", Offset: %" PRIu64
"\n",
1073 request
.from
, request
.len
,
1074 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1075 LOG("requested operation past EOF--bad client?");
1076 goto invalid_request
;
1079 if (client
->closing
) {
1081 * The client may be closed when we are blocked in
1082 * nbd_co_receive_request()
1089 TRACE("Request type is READ");
1091 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1092 ret
= blk_co_flush(exp
->blk
);
1094 LOG("flush failed");
1100 ret
= blk_pread(exp
->blk
, request
.from
+ exp
->dev_offset
,
1101 req
->data
, request
.len
);
1103 LOG("reading from file failed");
1108 TRACE("Read %" PRIu32
" byte(s)", request
.len
);
1109 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1113 TRACE("Request type is WRITE");
1115 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1116 TRACE("Server is read-only, return error");
1117 reply
.error
= EROFS
;
1121 TRACE("Writing to device");
1123 ret
= blk_pwrite(exp
->blk
, request
.from
+ exp
->dev_offset
,
1124 req
->data
, request
.len
);
1126 LOG("writing to file failed");
1131 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1132 ret
= blk_co_flush(exp
->blk
);
1134 LOG("flush failed");
1140 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1145 TRACE("Request type is DISCONNECT");
1149 TRACE("Request type is FLUSH");
1151 ret
= blk_co_flush(exp
->blk
);
1153 LOG("flush failed");
1156 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1161 TRACE("Request type is TRIM");
1162 /* Ignore unaligned head or tail, until block layer adds byte
1164 if (request
.len
>= BDRV_SECTOR_SIZE
) {
1165 request
.len
-= (request
.from
+ request
.len
) % BDRV_SECTOR_SIZE
;
1166 ret
= blk_co_discard(exp
->blk
,
1167 DIV_ROUND_UP(request
.from
+ exp
->dev_offset
,
1169 request
.len
/ BDRV_SECTOR_SIZE
);
1171 LOG("discard failed");
1175 TRACE("trim request too small, ignoring");
1177 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1182 LOG("invalid request type (%" PRIu32
") received", request
.type
);
1184 reply
.error
= EINVAL
;
1186 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1192 TRACE("Request/Reply complete");
1195 nbd_request_put(req
);
1199 nbd_request_put(req
);
1200 client_close(client
);
1203 static void nbd_read(void *opaque
)
1205 NBDClient
*client
= opaque
;
1207 if (client
->recv_coroutine
) {
1208 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1210 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1214 static void nbd_restart_write(void *opaque
)
1216 NBDClient
*client
= opaque
;
1218 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1221 static void nbd_set_handlers(NBDClient
*client
)
1223 if (client
->exp
&& client
->exp
->ctx
) {
1224 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1226 client
->can_read
? nbd_read
: NULL
,
1227 client
->send_coroutine
? nbd_restart_write
: NULL
,
1232 static void nbd_unset_handlers(NBDClient
*client
)
1234 if (client
->exp
&& client
->exp
->ctx
) {
1235 aio_set_fd_handler(client
->exp
->ctx
, client
->sioc
->fd
,
1236 true, NULL
, NULL
, NULL
);
1240 static void nbd_update_can_read(NBDClient
*client
)
1242 bool can_read
= client
->recv_coroutine
||
1243 client
->nb_requests
< MAX_NBD_REQUESTS
;
1245 if (can_read
!= client
->can_read
) {
1246 client
->can_read
= can_read
;
1247 nbd_set_handlers(client
);
1249 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1250 * in nbd_set_handlers() will have taken care of that */
1254 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1256 NBDClientNewData
*data
= opaque
;
1257 NBDClient
*client
= data
->client
;
1258 NBDExport
*exp
= client
->exp
;
1261 nbd_export_get(exp
);
1263 if (nbd_negotiate(data
)) {
1264 client_close(client
);
1267 qemu_co_mutex_init(&client
->send_lock
);
1268 nbd_set_handlers(client
);
1271 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1277 void nbd_client_new(NBDExport
*exp
,
1278 QIOChannelSocket
*sioc
,
1279 QCryptoTLSCreds
*tlscreds
,
1280 const char *tlsaclname
,
1281 void (*close_fn
)(NBDClient
*))
1284 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1286 client
= g_malloc0(sizeof(NBDClient
));
1287 client
->refcount
= 1;
1289 client
->tlscreds
= tlscreds
;
1291 object_ref(OBJECT(client
->tlscreds
));
1293 client
->tlsaclname
= g_strdup(tlsaclname
);
1294 client
->sioc
= sioc
;
1295 object_ref(OBJECT(client
->sioc
));
1296 client
->ioc
= QIO_CHANNEL(sioc
);
1297 object_ref(OBJECT(client
->ioc
));
1298 client
->can_read
= true;
1299 client
->close
= close_fn
;
1301 data
->client
= client
;
1302 data
->co
= qemu_coroutine_create(nbd_co_client_start
);
1303 qemu_coroutine_enter(data
->co
, data
);