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 "nbd-internal.h"
21 static int system_errno_to_nbd_errno(int err
)
44 /* Definitions for opaque data types */
46 typedef struct NBDRequest NBDRequest
;
49 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
56 void (*close
)(NBDExport
*exp
);
63 QTAILQ_HEAD(, NBDClient
) clients
;
64 QTAILQ_ENTRY(NBDExport
) next
;
68 Notifier eject_notifier
;
71 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
75 void (*close
)(NBDClient
*client
);
80 Coroutine
*recv_coroutine
;
83 Coroutine
*send_coroutine
;
87 QTAILQ_ENTRY(NBDClient
) next
;
92 /* That's all folks */
94 static void nbd_set_handlers(NBDClient
*client
);
95 static void nbd_unset_handlers(NBDClient
*client
);
96 static void nbd_update_can_read(NBDClient
*client
);
98 static void nbd_negotiate_continue(void *opaque
)
100 qemu_coroutine_enter(opaque
, NULL
);
103 static ssize_t
nbd_negotiate_read(int fd
, void *buffer
, size_t size
)
107 assert(qemu_in_coroutine());
108 /* Negotiation are always in main loop. */
109 qemu_set_fd_handler(fd
, nbd_negotiate_continue
, NULL
,
110 qemu_coroutine_self());
111 ret
= read_sync(fd
, buffer
, size
);
112 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
117 static ssize_t
nbd_negotiate_write(int fd
, void *buffer
, size_t size
)
121 assert(qemu_in_coroutine());
122 /* Negotiation are always in main loop. */
123 qemu_set_fd_handler(fd
, NULL
, nbd_negotiate_continue
,
124 qemu_coroutine_self());
125 ret
= write_sync(fd
, buffer
, size
);
126 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
130 static ssize_t
nbd_negotiate_drop_sync(int fd
, size_t size
)
132 ssize_t ret
, dropped
= size
;
133 uint8_t *buffer
= g_malloc(MIN(65536, size
));
136 ret
= nbd_negotiate_read(fd
, buffer
, MIN(65536, size
));
150 /* Basic flow for negotiation
177 static int nbd_negotiate_send_rep(int csock
, uint32_t type
, uint32_t opt
)
182 magic
= cpu_to_be64(NBD_REP_MAGIC
);
183 if (nbd_negotiate_write(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
184 LOG("write failed (rep magic)");
187 opt
= cpu_to_be32(opt
);
188 if (nbd_negotiate_write(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
189 LOG("write failed (rep opt)");
192 type
= cpu_to_be32(type
);
193 if (nbd_negotiate_write(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
194 LOG("write failed (rep type)");
197 len
= cpu_to_be32(0);
198 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
199 LOG("write failed (rep data length)");
205 static int nbd_negotiate_send_rep_list(int csock
, NBDExport
*exp
)
207 uint64_t magic
, name_len
;
208 uint32_t opt
, type
, len
;
210 name_len
= strlen(exp
->name
);
211 magic
= cpu_to_be64(NBD_REP_MAGIC
);
212 if (nbd_negotiate_write(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
213 LOG("write failed (magic)");
216 opt
= cpu_to_be32(NBD_OPT_LIST
);
217 if (nbd_negotiate_write(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
218 LOG("write failed (opt)");
221 type
= cpu_to_be32(NBD_REP_SERVER
);
222 if (nbd_negotiate_write(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
223 LOG("write failed (reply type)");
226 len
= cpu_to_be32(name_len
+ sizeof(len
));
227 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
228 LOG("write failed (length)");
231 len
= cpu_to_be32(name_len
);
232 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
233 LOG("write failed (length)");
236 if (nbd_negotiate_write(csock
, exp
->name
, name_len
) != name_len
) {
237 LOG("write failed (buffer)");
243 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
248 csock
= client
->sock
;
250 if (nbd_negotiate_drop_sync(csock
, length
) != length
) {
253 return nbd_negotiate_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
256 /* For each export, send a NBD_REP_SERVER reply. */
257 QTAILQ_FOREACH(exp
, &exports
, next
) {
258 if (nbd_negotiate_send_rep_list(csock
, exp
)) {
262 /* Finish with a NBD_REP_ACK. */
263 return nbd_negotiate_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
266 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
268 int rc
= -EINVAL
, csock
= client
->sock
;
272 [20 .. xx] export name (length bytes)
274 TRACE("Checking length");
276 LOG("Bad length received");
279 if (nbd_negotiate_read(csock
, name
, length
) != length
) {
285 client
->exp
= nbd_export_find(name
);
287 LOG("export not found");
291 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
292 nbd_export_get(client
->exp
);
298 static int nbd_negotiate_options(NBDClient
*client
)
300 int csock
= client
->sock
;
304 [ 0 .. 3] client flags
306 [ 0 .. 7] NBD_OPTS_MAGIC
307 [ 8 .. 11] NBD option
308 [12 .. 15] Data length
311 [ 0 .. 7] NBD_OPTS_MAGIC
312 [ 8 .. 11] Second NBD option
313 [12 .. 15] Data length
317 if (nbd_negotiate_read(csock
, &flags
, sizeof(flags
)) != sizeof(flags
)) {
321 TRACE("Checking client flags");
322 be32_to_cpus(&flags
);
323 if (flags
!= 0 && flags
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
324 LOG("Bad client flags received");
330 uint32_t tmp
, length
;
333 if (nbd_negotiate_read(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
337 TRACE("Checking opts magic");
338 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
339 LOG("Bad magic received");
343 if (nbd_negotiate_read(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
348 if (nbd_negotiate_read(csock
, &length
,
349 sizeof(length
)) != sizeof(length
)) {
353 length
= be32_to_cpu(length
);
355 TRACE("Checking option");
356 switch (be32_to_cpu(tmp
)) {
358 ret
= nbd_negotiate_handle_list(client
, length
);
367 case NBD_OPT_EXPORT_NAME
:
368 return nbd_negotiate_handle_export_name(client
, length
);
371 tmp
= be32_to_cpu(tmp
);
372 LOG("Unsupported option 0x%x", tmp
);
373 nbd_negotiate_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
384 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
386 NBDClient
*client
= data
->client
;
387 int csock
= client
->sock
;
388 char buf
[8 + 8 + 8 + 128];
390 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
391 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
393 /* Negotiation header without options:
394 [ 0 .. 7] passwd ("NBDMAGIC")
395 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
397 [24 .. 25] server flags (0)
398 [26 .. 27] export flags
399 [28 .. 151] reserved (0)
401 Negotiation header with options, part 1:
402 [ 0 .. 7] passwd ("NBDMAGIC")
403 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
404 [16 .. 17] server flags (0)
406 part 2 (after options are sent):
408 [26 .. 27] export flags
409 [28 .. 151] reserved (0)
414 TRACE("Beginning negotiation.");
415 memset(buf
, 0, sizeof(buf
));
416 memcpy(buf
, "NBDMAGIC", 8);
418 assert ((client
->exp
->nbdflags
& ~65535) == 0);
419 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
420 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
421 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
423 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
424 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
428 if (nbd_negotiate_write(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
433 if (nbd_negotiate_write(csock
, buf
, 18) != 18) {
437 rc
= nbd_negotiate_options(client
);
439 LOG("option negotiation failed");
443 assert ((client
->exp
->nbdflags
& ~65535) == 0);
444 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
445 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
446 if (nbd_negotiate_write(csock
, buf
+ 18,
447 sizeof(buf
) - 18) != sizeof(buf
) - 18) {
453 TRACE("Negotiation succeeded.");
461 int nbd_disconnect(int fd
)
463 ioctl(fd
, NBD_CLEAR_QUE
);
464 ioctl(fd
, NBD_DISCONNECT
);
465 ioctl(fd
, NBD_CLEAR_SOCK
);
471 int nbd_disconnect(int fd
)
477 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
479 uint8_t buf
[NBD_REQUEST_SIZE
];
483 ret
= read_sync(csock
, buf
, sizeof(buf
));
488 if (ret
!= sizeof(buf
)) {
494 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
495 [ 4 .. 7] type (0 == READ, 1 == WRITE)
501 magic
= be32_to_cpup((uint32_t*)buf
);
502 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
503 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
504 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
505 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
507 TRACE("Got request: "
508 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
509 magic
, request
->type
, request
->from
, request
->len
);
511 if (magic
!= NBD_REQUEST_MAGIC
) {
512 LOG("invalid magic (got 0x%x)", magic
);
518 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
520 uint8_t buf
[NBD_REPLY_SIZE
];
523 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
526 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
527 [ 4 .. 7] error (0 == no error)
530 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
531 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
532 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
534 TRACE("Sending response to client");
536 ret
= write_sync(csock
, buf
, sizeof(buf
));
541 if (ret
!= sizeof(buf
)) {
542 LOG("writing to socket failed");
548 #define MAX_NBD_REQUESTS 16
550 void nbd_client_get(NBDClient
*client
)
555 void nbd_client_put(NBDClient
*client
)
557 if (--client
->refcount
== 0) {
558 /* The last reference should be dropped by client->close,
559 * which is called by client_close.
561 assert(client
->closing
);
563 nbd_unset_handlers(client
);
567 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
568 nbd_export_put(client
->exp
);
574 static void client_close(NBDClient
*client
)
576 if (client
->closing
) {
580 client
->closing
= true;
582 /* Force requests to finish. They will drop their own references,
583 * then we'll close the socket and free the NBDClient.
585 shutdown(client
->sock
, 2);
587 /* Also tell the client, so that they release their reference. */
589 client
->close(client
);
593 static NBDRequest
*nbd_request_get(NBDClient
*client
)
597 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
598 client
->nb_requests
++;
599 nbd_update_can_read(client
);
601 req
= g_new0(NBDRequest
, 1);
602 nbd_client_get(client
);
603 req
->client
= client
;
607 static void nbd_request_put(NBDRequest
*req
)
609 NBDClient
*client
= req
->client
;
612 qemu_vfree(req
->data
);
616 client
->nb_requests
--;
617 nbd_update_can_read(client
);
618 nbd_client_put(client
);
621 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
623 NBDExport
*exp
= opaque
;
626 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
630 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
631 nbd_set_handlers(client
);
635 static void blk_aio_detach(void *opaque
)
637 NBDExport
*exp
= opaque
;
640 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
642 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
643 nbd_unset_handlers(client
);
649 static void nbd_eject_notifier(Notifier
*n
, void *data
)
651 NBDExport
*exp
= container_of(n
, NBDExport
, eject_notifier
);
652 nbd_export_close(exp
);
655 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
656 uint32_t nbdflags
, void (*close
)(NBDExport
*),
659 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
661 QTAILQ_INIT(&exp
->clients
);
663 exp
->dev_offset
= dev_offset
;
664 exp
->nbdflags
= nbdflags
;
665 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
667 error_setg_errno(errp
, -exp
->size
,
668 "Failed to determine the NBD export's length");
671 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
674 exp
->ctx
= blk_get_aio_context(blk
);
676 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
678 exp
->eject_notifier
.notify
= nbd_eject_notifier
;
679 blk_add_remove_bs_notifier(blk
, &exp
->eject_notifier
);
682 * NBD exports are used for non-shared storage migration. Make sure
683 * that BDRV_O_INACTIVE is cleared and the image is ready for write
684 * access since the export could be available before migration handover.
686 aio_context_acquire(exp
->ctx
);
687 blk_invalidate_cache(blk
, NULL
);
688 aio_context_release(exp
->ctx
);
696 NBDExport
*nbd_export_find(const char *name
)
699 QTAILQ_FOREACH(exp
, &exports
, next
) {
700 if (strcmp(name
, exp
->name
) == 0) {
708 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
710 if (exp
->name
== name
) {
715 if (exp
->name
!= NULL
) {
718 QTAILQ_REMOVE(&exports
, exp
, next
);
723 exp
->name
= g_strdup(name
);
724 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
729 void nbd_export_close(NBDExport
*exp
)
731 NBDClient
*client
, *next
;
734 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
735 client_close(client
);
737 nbd_export_set_name(exp
, NULL
);
741 void nbd_export_get(NBDExport
*exp
)
743 assert(exp
->refcount
> 0);
747 void nbd_export_put(NBDExport
*exp
)
749 assert(exp
->refcount
> 0);
750 if (exp
->refcount
== 1) {
751 nbd_export_close(exp
);
754 if (--exp
->refcount
== 0) {
755 assert(exp
->name
== NULL
);
762 notifier_remove(&exp
->eject_notifier
);
763 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
764 blk_aio_detach
, exp
);
773 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
778 void nbd_export_close_all(void)
780 NBDExport
*exp
, *next
;
782 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
783 nbd_export_close(exp
);
787 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
790 NBDClient
*client
= req
->client
;
791 int csock
= client
->sock
;
794 qemu_co_mutex_lock(&client
->send_lock
);
795 client
->send_coroutine
= qemu_coroutine_self();
796 nbd_set_handlers(client
);
799 rc
= nbd_send_reply(csock
, reply
);
801 socket_set_cork(csock
, 1);
802 rc
= nbd_send_reply(csock
, reply
);
804 ret
= qemu_co_send(csock
, req
->data
, len
);
809 socket_set_cork(csock
, 0);
812 client
->send_coroutine
= NULL
;
813 nbd_set_handlers(client
);
814 qemu_co_mutex_unlock(&client
->send_lock
);
818 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
820 NBDClient
*client
= req
->client
;
821 int csock
= client
->sock
;
825 client
->recv_coroutine
= qemu_coroutine_self();
826 nbd_update_can_read(client
);
828 rc
= nbd_receive_request(csock
, request
);
836 if ((request
->from
+ request
->len
) < request
->from
) {
837 LOG("integer overflow detected! "
838 "you're probably being attacked");
843 TRACE("Decoding type");
845 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
846 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
847 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
848 LOG("len (%u) is larger than max len (%u)",
849 request
->len
, NBD_MAX_BUFFER_SIZE
);
854 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
855 if (req
->data
== NULL
) {
860 if (command
== NBD_CMD_WRITE
) {
861 TRACE("Reading %u byte(s)", request
->len
);
863 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
864 LOG("reading from socket failed");
872 client
->recv_coroutine
= NULL
;
873 nbd_update_can_read(client
);
878 static void nbd_trip(void *opaque
)
880 NBDClient
*client
= opaque
;
881 NBDExport
*exp
= client
->exp
;
883 struct nbd_request request
;
884 struct nbd_reply reply
;
888 TRACE("Reading request.");
889 if (client
->closing
) {
893 req
= nbd_request_get(client
);
894 ret
= nbd_co_receive_request(req
, &request
);
895 if (ret
== -EAGAIN
) {
902 reply
.handle
= request
.handle
;
909 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
910 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
911 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
912 ", Offset: %" PRIu64
"\n",
913 request
.from
, request
.len
,
914 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
915 LOG("requested operation past EOF--bad client?");
916 goto invalid_request
;
919 if (client
->closing
) {
921 * The client may be closed when we are blocked in
922 * nbd_co_receive_request()
929 TRACE("Request type is READ");
931 if (request
.type
& NBD_CMD_FLAG_FUA
) {
932 ret
= blk_co_flush(exp
->blk
);
940 ret
= blk_read(exp
->blk
,
941 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
942 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
944 LOG("reading from file failed");
949 TRACE("Read %u byte(s)", request
.len
);
950 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
954 TRACE("Request type is WRITE");
956 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
957 TRACE("Server is read-only, return error");
962 TRACE("Writing to device");
964 ret
= blk_write(exp
->blk
,
965 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
966 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
968 LOG("writing to file failed");
973 if (request
.type
& NBD_CMD_FLAG_FUA
) {
974 ret
= blk_co_flush(exp
->blk
);
982 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
987 TRACE("Request type is DISCONNECT");
991 TRACE("Request type is FLUSH");
993 ret
= blk_co_flush(exp
->blk
);
998 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1003 TRACE("Request type is TRIM");
1004 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
1006 request
.len
/ BDRV_SECTOR_SIZE
);
1008 LOG("discard failed");
1011 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1016 LOG("invalid request type (%u) received", request
.type
);
1018 reply
.error
= EINVAL
;
1020 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1026 TRACE("Request/Reply complete");
1029 nbd_request_put(req
);
1033 nbd_request_put(req
);
1034 client_close(client
);
1037 static void nbd_read(void *opaque
)
1039 NBDClient
*client
= opaque
;
1041 if (client
->recv_coroutine
) {
1042 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1044 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1048 static void nbd_restart_write(void *opaque
)
1050 NBDClient
*client
= opaque
;
1052 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1055 static void nbd_set_handlers(NBDClient
*client
)
1057 if (client
->exp
&& client
->exp
->ctx
) {
1058 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1060 client
->can_read
? nbd_read
: NULL
,
1061 client
->send_coroutine
? nbd_restart_write
: NULL
,
1066 static void nbd_unset_handlers(NBDClient
*client
)
1068 if (client
->exp
&& client
->exp
->ctx
) {
1069 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1070 true, NULL
, NULL
, NULL
);
1074 static void nbd_update_can_read(NBDClient
*client
)
1076 bool can_read
= client
->recv_coroutine
||
1077 client
->nb_requests
< MAX_NBD_REQUESTS
;
1079 if (can_read
!= client
->can_read
) {
1080 client
->can_read
= can_read
;
1081 nbd_set_handlers(client
);
1083 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1084 * in nbd_set_handlers() will have taken care of that */
1088 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1090 NBDClientNewData
*data
= opaque
;
1091 NBDClient
*client
= data
->client
;
1092 NBDExport
*exp
= client
->exp
;
1095 nbd_export_get(exp
);
1097 if (nbd_negotiate(data
)) {
1098 client_close(client
);
1101 qemu_co_mutex_init(&client
->send_lock
);
1102 nbd_set_handlers(client
);
1105 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1111 void nbd_client_new(NBDExport
*exp
, int csock
, void (*close_fn
)(NBDClient
*))
1114 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1116 client
= g_malloc0(sizeof(NBDClient
));
1117 client
->refcount
= 1;
1119 client
->sock
= csock
;
1120 client
->can_read
= true;
1121 client
->close
= close_fn
;
1123 data
->client
= client
;
1124 data
->co
= qemu_coroutine_create(nbd_co_client_start
);
1125 qemu_coroutine_enter(data
->co
, data
);