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
;
69 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
73 void (*close
)(NBDClient
*client
);
78 Coroutine
*recv_coroutine
;
81 Coroutine
*send_coroutine
;
85 QTAILQ_ENTRY(NBDClient
) next
;
90 /* That's all folks */
92 static void nbd_set_handlers(NBDClient
*client
);
93 static void nbd_unset_handlers(NBDClient
*client
);
94 static void nbd_update_can_read(NBDClient
*client
);
96 static void nbd_negotiate_continue(void *opaque
)
98 qemu_coroutine_enter(opaque
, NULL
);
101 static ssize_t
nbd_negotiate_read(int fd
, void *buffer
, size_t size
)
105 assert(qemu_in_coroutine());
106 /* Negotiation are always in main loop. */
107 qemu_set_fd_handler(fd
, nbd_negotiate_continue
, NULL
,
108 qemu_coroutine_self());
109 ret
= read_sync(fd
, buffer
, size
);
110 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
115 static ssize_t
nbd_negotiate_write(int fd
, void *buffer
, size_t size
)
119 assert(qemu_in_coroutine());
120 /* Negotiation are always in main loop. */
121 qemu_set_fd_handler(fd
, NULL
, nbd_negotiate_continue
,
122 qemu_coroutine_self());
123 ret
= write_sync(fd
, buffer
, size
);
124 qemu_set_fd_handler(fd
, NULL
, NULL
, NULL
);
128 static ssize_t
nbd_negotiate_drop_sync(int fd
, size_t size
)
130 ssize_t ret
, dropped
= size
;
131 uint8_t *buffer
= g_malloc(MIN(65536, size
));
134 ret
= nbd_negotiate_read(fd
, buffer
, MIN(65536, size
));
148 /* Basic flow for negotiation
175 static int nbd_negotiate_send_rep(int csock
, uint32_t type
, uint32_t opt
)
180 magic
= cpu_to_be64(NBD_REP_MAGIC
);
181 if (nbd_negotiate_write(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
182 LOG("write failed (rep magic)");
185 opt
= cpu_to_be32(opt
);
186 if (nbd_negotiate_write(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
187 LOG("write failed (rep opt)");
190 type
= cpu_to_be32(type
);
191 if (nbd_negotiate_write(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
192 LOG("write failed (rep type)");
195 len
= cpu_to_be32(0);
196 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
197 LOG("write failed (rep data length)");
203 static int nbd_negotiate_send_rep_list(int csock
, NBDExport
*exp
)
205 uint64_t magic
, name_len
;
206 uint32_t opt
, type
, len
;
208 name_len
= strlen(exp
->name
);
209 magic
= cpu_to_be64(NBD_REP_MAGIC
);
210 if (nbd_negotiate_write(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
211 LOG("write failed (magic)");
214 opt
= cpu_to_be32(NBD_OPT_LIST
);
215 if (nbd_negotiate_write(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
216 LOG("write failed (opt)");
219 type
= cpu_to_be32(NBD_REP_SERVER
);
220 if (nbd_negotiate_write(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
221 LOG("write failed (reply type)");
224 len
= cpu_to_be32(name_len
+ sizeof(len
));
225 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
226 LOG("write failed (length)");
229 len
= cpu_to_be32(name_len
);
230 if (nbd_negotiate_write(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
231 LOG("write failed (length)");
234 if (nbd_negotiate_write(csock
, exp
->name
, name_len
) != name_len
) {
235 LOG("write failed (buffer)");
241 static int nbd_negotiate_handle_list(NBDClient
*client
, uint32_t length
)
246 csock
= client
->sock
;
248 if (nbd_negotiate_drop_sync(csock
, length
) != length
) {
251 return nbd_negotiate_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
254 /* For each export, send a NBD_REP_SERVER reply. */
255 QTAILQ_FOREACH(exp
, &exports
, next
) {
256 if (nbd_negotiate_send_rep_list(csock
, exp
)) {
260 /* Finish with a NBD_REP_ACK. */
261 return nbd_negotiate_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
264 static int nbd_negotiate_handle_export_name(NBDClient
*client
, uint32_t length
)
266 int rc
= -EINVAL
, csock
= client
->sock
;
270 [20 .. xx] export name (length bytes)
272 TRACE("Checking length");
274 LOG("Bad length received");
277 if (nbd_negotiate_read(csock
, name
, length
) != length
) {
283 client
->exp
= nbd_export_find(name
);
285 LOG("export not found");
289 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
290 nbd_export_get(client
->exp
);
296 static int nbd_negotiate_options(NBDClient
*client
)
298 int csock
= client
->sock
;
302 [ 0 .. 3] client flags
304 [ 0 .. 7] NBD_OPTS_MAGIC
305 [ 8 .. 11] NBD option
306 [12 .. 15] Data length
309 [ 0 .. 7] NBD_OPTS_MAGIC
310 [ 8 .. 11] Second NBD option
311 [12 .. 15] Data length
315 if (nbd_negotiate_read(csock
, &flags
, sizeof(flags
)) != sizeof(flags
)) {
319 TRACE("Checking client flags");
320 be32_to_cpus(&flags
);
321 if (flags
!= 0 && flags
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
322 LOG("Bad client flags received");
328 uint32_t tmp
, length
;
331 if (nbd_negotiate_read(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
335 TRACE("Checking opts magic");
336 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
337 LOG("Bad magic received");
341 if (nbd_negotiate_read(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
346 if (nbd_negotiate_read(csock
, &length
,
347 sizeof(length
)) != sizeof(length
)) {
351 length
= be32_to_cpu(length
);
353 TRACE("Checking option");
354 switch (be32_to_cpu(tmp
)) {
356 ret
= nbd_negotiate_handle_list(client
, length
);
365 case NBD_OPT_EXPORT_NAME
:
366 return nbd_negotiate_handle_export_name(client
, length
);
369 tmp
= be32_to_cpu(tmp
);
370 LOG("Unsupported option 0x%x", tmp
);
371 nbd_negotiate_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
382 static coroutine_fn
int nbd_negotiate(NBDClientNewData
*data
)
384 NBDClient
*client
= data
->client
;
385 int csock
= client
->sock
;
386 char buf
[8 + 8 + 8 + 128];
388 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
389 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
391 /* Negotiation header without options:
392 [ 0 .. 7] passwd ("NBDMAGIC")
393 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
395 [24 .. 25] server flags (0)
396 [26 .. 27] export flags
397 [28 .. 151] reserved (0)
399 Negotiation header with options, part 1:
400 [ 0 .. 7] passwd ("NBDMAGIC")
401 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
402 [16 .. 17] server flags (0)
404 part 2 (after options are sent):
406 [26 .. 27] export flags
407 [28 .. 151] reserved (0)
412 TRACE("Beginning negotiation.");
413 memset(buf
, 0, sizeof(buf
));
414 memcpy(buf
, "NBDMAGIC", 8);
416 assert ((client
->exp
->nbdflags
& ~65535) == 0);
417 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
418 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
419 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
421 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
422 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
426 if (nbd_negotiate_write(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
431 if (nbd_negotiate_write(csock
, buf
, 18) != 18) {
435 rc
= nbd_negotiate_options(client
);
437 LOG("option negotiation failed");
441 assert ((client
->exp
->nbdflags
& ~65535) == 0);
442 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
443 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
444 if (nbd_negotiate_write(csock
, buf
+ 18,
445 sizeof(buf
) - 18) != sizeof(buf
) - 18) {
451 TRACE("Negotiation succeeded.");
459 int nbd_disconnect(int fd
)
461 ioctl(fd
, NBD_CLEAR_QUE
);
462 ioctl(fd
, NBD_DISCONNECT
);
463 ioctl(fd
, NBD_CLEAR_SOCK
);
469 int nbd_disconnect(int fd
)
475 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
477 uint8_t buf
[NBD_REQUEST_SIZE
];
481 ret
= read_sync(csock
, buf
, sizeof(buf
));
486 if (ret
!= sizeof(buf
)) {
492 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
493 [ 4 .. 7] type (0 == READ, 1 == WRITE)
499 magic
= be32_to_cpup((uint32_t*)buf
);
500 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
501 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
502 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
503 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
505 TRACE("Got request: "
506 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
507 magic
, request
->type
, request
->from
, request
->len
);
509 if (magic
!= NBD_REQUEST_MAGIC
) {
510 LOG("invalid magic (got 0x%x)", magic
);
516 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
518 uint8_t buf
[NBD_REPLY_SIZE
];
521 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
524 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
525 [ 4 .. 7] error (0 == no error)
528 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
529 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
530 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
532 TRACE("Sending response to client");
534 ret
= write_sync(csock
, buf
, sizeof(buf
));
539 if (ret
!= sizeof(buf
)) {
540 LOG("writing to socket failed");
546 #define MAX_NBD_REQUESTS 16
548 void nbd_client_get(NBDClient
*client
)
553 void nbd_client_put(NBDClient
*client
)
555 if (--client
->refcount
== 0) {
556 /* The last reference should be dropped by client->close,
557 * which is called by client_close.
559 assert(client
->closing
);
561 nbd_unset_handlers(client
);
565 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
566 nbd_export_put(client
->exp
);
572 static void client_close(NBDClient
*client
)
574 if (client
->closing
) {
578 client
->closing
= true;
580 /* Force requests to finish. They will drop their own references,
581 * then we'll close the socket and free the NBDClient.
583 shutdown(client
->sock
, 2);
585 /* Also tell the client, so that they release their reference. */
587 client
->close(client
);
591 static NBDRequest
*nbd_request_get(NBDClient
*client
)
595 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
596 client
->nb_requests
++;
597 nbd_update_can_read(client
);
599 req
= g_new0(NBDRequest
, 1);
600 nbd_client_get(client
);
601 req
->client
= client
;
605 static void nbd_request_put(NBDRequest
*req
)
607 NBDClient
*client
= req
->client
;
610 qemu_vfree(req
->data
);
614 client
->nb_requests
--;
615 nbd_update_can_read(client
);
616 nbd_client_put(client
);
619 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
621 NBDExport
*exp
= opaque
;
624 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
628 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
629 nbd_set_handlers(client
);
633 static void blk_aio_detach(void *opaque
)
635 NBDExport
*exp
= opaque
;
638 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
640 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
641 nbd_unset_handlers(client
);
647 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
648 uint32_t nbdflags
, void (*close
)(NBDExport
*),
651 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
653 QTAILQ_INIT(&exp
->clients
);
655 exp
->dev_offset
= dev_offset
;
656 exp
->nbdflags
= nbdflags
;
657 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
659 error_setg_errno(errp
, -exp
->size
,
660 "Failed to determine the NBD export's length");
663 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
666 exp
->ctx
= blk_get_aio_context(blk
);
668 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
670 * NBD exports are used for non-shared storage migration. Make sure
671 * that BDRV_O_INCOMING is cleared and the image is ready for write
672 * access since the export could be available before migration handover.
674 blk_invalidate_cache(blk
, NULL
);
682 NBDExport
*nbd_export_find(const char *name
)
685 QTAILQ_FOREACH(exp
, &exports
, next
) {
686 if (strcmp(name
, exp
->name
) == 0) {
694 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
696 if (exp
->name
== name
) {
701 if (exp
->name
!= NULL
) {
704 QTAILQ_REMOVE(&exports
, exp
, next
);
709 exp
->name
= g_strdup(name
);
710 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
715 void nbd_export_close(NBDExport
*exp
)
717 NBDClient
*client
, *next
;
720 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
721 client_close(client
);
723 nbd_export_set_name(exp
, NULL
);
727 void nbd_export_get(NBDExport
*exp
)
729 assert(exp
->refcount
> 0);
733 void nbd_export_put(NBDExport
*exp
)
735 assert(exp
->refcount
> 0);
736 if (exp
->refcount
== 1) {
737 nbd_export_close(exp
);
740 if (--exp
->refcount
== 0) {
741 assert(exp
->name
== NULL
);
748 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
749 blk_aio_detach
, exp
);
758 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
763 void nbd_export_close_all(void)
765 NBDExport
*exp
, *next
;
767 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
768 nbd_export_close(exp
);
772 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
775 NBDClient
*client
= req
->client
;
776 int csock
= client
->sock
;
779 qemu_co_mutex_lock(&client
->send_lock
);
780 client
->send_coroutine
= qemu_coroutine_self();
781 nbd_set_handlers(client
);
784 rc
= nbd_send_reply(csock
, reply
);
786 socket_set_cork(csock
, 1);
787 rc
= nbd_send_reply(csock
, reply
);
789 ret
= qemu_co_send(csock
, req
->data
, len
);
794 socket_set_cork(csock
, 0);
797 client
->send_coroutine
= NULL
;
798 nbd_set_handlers(client
);
799 qemu_co_mutex_unlock(&client
->send_lock
);
803 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
805 NBDClient
*client
= req
->client
;
806 int csock
= client
->sock
;
810 client
->recv_coroutine
= qemu_coroutine_self();
811 nbd_update_can_read(client
);
813 rc
= nbd_receive_request(csock
, request
);
821 if ((request
->from
+ request
->len
) < request
->from
) {
822 LOG("integer overflow detected! "
823 "you're probably being attacked");
828 TRACE("Decoding type");
830 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
831 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
832 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
833 LOG("len (%u) is larger than max len (%u)",
834 request
->len
, NBD_MAX_BUFFER_SIZE
);
839 req
->data
= blk_try_blockalign(client
->exp
->blk
, request
->len
);
840 if (req
->data
== NULL
) {
845 if (command
== NBD_CMD_WRITE
) {
846 TRACE("Reading %u byte(s)", request
->len
);
848 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
849 LOG("reading from socket failed");
857 client
->recv_coroutine
= NULL
;
858 nbd_update_can_read(client
);
863 static void nbd_trip(void *opaque
)
865 NBDClient
*client
= opaque
;
866 NBDExport
*exp
= client
->exp
;
868 struct nbd_request request
;
869 struct nbd_reply reply
;
873 TRACE("Reading request.");
874 if (client
->closing
) {
878 req
= nbd_request_get(client
);
879 ret
= nbd_co_receive_request(req
, &request
);
880 if (ret
== -EAGAIN
) {
887 reply
.handle
= request
.handle
;
894 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
895 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
896 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
897 ", Offset: %" PRIu64
"\n",
898 request
.from
, request
.len
,
899 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
900 LOG("requested operation past EOF--bad client?");
901 goto invalid_request
;
904 if (client
->closing
) {
906 * The client may be closed when we are blocked in
907 * nbd_co_receive_request()
914 TRACE("Request type is READ");
916 if (request
.type
& NBD_CMD_FLAG_FUA
) {
917 ret
= blk_co_flush(exp
->blk
);
925 ret
= blk_read(exp
->blk
,
926 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
927 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
929 LOG("reading from file failed");
934 TRACE("Read %u byte(s)", request
.len
);
935 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
939 TRACE("Request type is WRITE");
941 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
942 TRACE("Server is read-only, return error");
947 TRACE("Writing to device");
949 ret
= blk_write(exp
->blk
,
950 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
951 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
953 LOG("writing to file failed");
958 if (request
.type
& NBD_CMD_FLAG_FUA
) {
959 ret
= blk_co_flush(exp
->blk
);
967 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
972 TRACE("Request type is DISCONNECT");
976 TRACE("Request type is FLUSH");
978 ret
= blk_co_flush(exp
->blk
);
983 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
988 TRACE("Request type is TRIM");
989 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
991 request
.len
/ BDRV_SECTOR_SIZE
);
993 LOG("discard failed");
996 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1001 LOG("invalid request type (%u) received", request
.type
);
1003 reply
.error
= EINVAL
;
1005 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1011 TRACE("Request/Reply complete");
1014 nbd_request_put(req
);
1018 nbd_request_put(req
);
1019 client_close(client
);
1022 static void nbd_read(void *opaque
)
1024 NBDClient
*client
= opaque
;
1026 if (client
->recv_coroutine
) {
1027 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1029 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1033 static void nbd_restart_write(void *opaque
)
1035 NBDClient
*client
= opaque
;
1037 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1040 static void nbd_set_handlers(NBDClient
*client
)
1042 if (client
->exp
&& client
->exp
->ctx
) {
1043 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1045 client
->can_read
? nbd_read
: NULL
,
1046 client
->send_coroutine
? nbd_restart_write
: NULL
,
1051 static void nbd_unset_handlers(NBDClient
*client
)
1053 if (client
->exp
&& client
->exp
->ctx
) {
1054 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1055 true, NULL
, NULL
, NULL
);
1059 static void nbd_update_can_read(NBDClient
*client
)
1061 bool can_read
= client
->recv_coroutine
||
1062 client
->nb_requests
< MAX_NBD_REQUESTS
;
1064 if (can_read
!= client
->can_read
) {
1065 client
->can_read
= can_read
;
1066 nbd_set_handlers(client
);
1068 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1069 * in nbd_set_handlers() will have taken care of that */
1073 static coroutine_fn
void nbd_co_client_start(void *opaque
)
1075 NBDClientNewData
*data
= opaque
;
1076 NBDClient
*client
= data
->client
;
1077 NBDExport
*exp
= client
->exp
;
1080 nbd_export_get(exp
);
1082 if (nbd_negotiate(data
)) {
1083 shutdown(client
->sock
, 2);
1084 client
->close(client
);
1087 qemu_co_mutex_init(&client
->send_lock
);
1088 nbd_set_handlers(client
);
1091 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1097 void nbd_client_new(NBDExport
*exp
, int csock
, void (*close_fn
)(NBDClient
*))
1100 NBDClientNewData
*data
= g_new(NBDClientNewData
, 1);
1102 client
= g_malloc0(sizeof(NBDClient
));
1103 client
->refcount
= 1;
1105 client
->sock
= csock
;
1106 client
->can_read
= true;
1107 client
->close
= close_fn
;
1109 data
->client
= client
;
1110 data
->co
= qemu_coroutine_create(nbd_co_client_start
);
1111 qemu_coroutine_enter(data
->co
, data
);