]>
git.proxmox.com Git - qemu.git/blob - nbd.c
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
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/>.
22 #include "qemu-coroutine.h"
27 #include <sys/ioctl.h>
29 #if defined(__sun__) || defined(__HAIKU__)
30 #include <sys/ioccom.h>
39 #include "qemu_socket.h"
40 #include "qemu-queue.h"
45 #define TRACE(msg, ...) do { \
46 LOG(msg, ## __VA_ARGS__); \
49 #define TRACE(msg, ...) \
53 #define LOG(msg, ...) do { \
54 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
55 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
58 /* This is all part of the "official" NBD API */
60 #define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
61 #define NBD_REPLY_SIZE (4 + 4 + 8)
62 #define NBD_REQUEST_MAGIC 0x25609513
63 #define NBD_REPLY_MAGIC 0x67446698
64 #define NBD_OPTS_MAGIC 0x49484156454F5054LL
65 #define NBD_CLIENT_MAGIC 0x0000420281861253LL
67 #define NBD_SET_SOCK _IO(0xab, 0)
68 #define NBD_SET_BLKSIZE _IO(0xab, 1)
69 #define NBD_SET_SIZE _IO(0xab, 2)
70 #define NBD_DO_IT _IO(0xab, 3)
71 #define NBD_CLEAR_SOCK _IO(0xab, 4)
72 #define NBD_CLEAR_QUE _IO(0xab, 5)
73 #define NBD_PRINT_DEBUG _IO(0xab, 6)
74 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
75 #define NBD_DISCONNECT _IO(0xab, 8)
76 #define NBD_SET_TIMEOUT _IO(0xab, 9)
77 #define NBD_SET_FLAGS _IO(0xab, 10)
79 #define NBD_OPT_EXPORT_NAME (1 << 0)
81 /* Definitions for opaque data types */
83 typedef struct NBDRequest NBDRequest
;
86 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
93 void (*close
)(NBDExport
*exp
);
99 QTAILQ_HEAD(, NBDClient
) clients
;
100 QSIMPLEQ_HEAD(, NBDRequest
) requests
;
105 void (*close
)(NBDClient
*client
);
110 Coroutine
*recv_coroutine
;
113 Coroutine
*send_coroutine
;
115 QTAILQ_ENTRY(NBDClient
) next
;
120 /* That's all folks */
122 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
127 if (qemu_in_coroutine()) {
129 return qemu_co_recv(fd
, buffer
, size
);
131 return qemu_co_send(fd
, buffer
, size
);
135 while (offset
< size
) {
139 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
141 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
145 err
= socket_error();
147 /* recoverable error */
148 if (err
== EINTR
|| (offset
> 0 && err
== EAGAIN
)) {
152 /* unrecoverable error */
167 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
169 /* Sockets are kept in blocking mode in the negotiation phase. After
170 * that, a non-readable socket simply means that another thread stole
171 * our request/reply. Synchronization is done with recv_coroutine, so
172 * that this is coroutine-safe.
174 return nbd_wr_sync(fd
, buffer
, size
, true);
177 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
181 /* For writes, we do expect the socket to be writable. */
182 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
183 } while (ret
== -EAGAIN
);
187 static void combine_addr(char *buf
, size_t len
, const char* address
,
190 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
191 if (strstr(address
, ":")) {
192 snprintf(buf
, len
, "[%s]:%u", address
, port
);
194 snprintf(buf
, len
, "%s:%u", address
, port
);
198 int tcp_socket_outgoing(const char *address
, uint16_t port
)
200 char address_and_port
[128];
201 combine_addr(address_and_port
, 128, address
, port
);
202 return tcp_socket_outgoing_spec(address_and_port
);
205 int tcp_socket_outgoing_spec(const char *address_and_port
)
207 return inet_connect(address_and_port
, true, NULL
, NULL
);
210 int tcp_socket_incoming(const char *address
, uint16_t port
)
212 char address_and_port
[128];
213 combine_addr(address_and_port
, 128, address
, port
);
214 return tcp_socket_incoming_spec(address_and_port
);
217 int tcp_socket_incoming_spec(const char *address_and_port
)
221 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0, NULL
);
224 int unix_socket_incoming(const char *path
)
229 return unix_listen(path
, ostr
, olen
);
232 int unix_socket_outgoing(const char *path
)
234 return unix_connect(path
);
251 static int nbd_send_negotiate(NBDClient
*client
)
253 int csock
= client
->sock
;
254 char buf
[8 + 8 + 8 + 128];
258 [ 0 .. 7] passwd ("NBDMAGIC")
259 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
262 [28 .. 151] reserved (0)
265 socket_set_block(csock
);
268 TRACE("Beginning negotiation.");
269 memcpy(buf
, "NBDMAGIC", 8);
270 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
271 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
272 cpu_to_be32w((uint32_t*)(buf
+ 24),
273 client
->exp
->nbdflags
| NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
274 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
275 memset(buf
+ 28, 0, 124);
277 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
282 TRACE("Negotiation succeeded.");
285 socket_set_nonblock(csock
);
289 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
290 off_t
*size
, size_t *blocksize
)
297 TRACE("Receiving negotiation.");
299 socket_set_block(csock
);
302 if (read_sync(csock
, buf
, 8) != 8) {
308 if (strlen(buf
) == 0) {
309 LOG("server connection closed");
313 TRACE("Magic is %c%c%c%c%c%c%c%c",
314 qemu_isprint(buf
[0]) ? buf
[0] : '.',
315 qemu_isprint(buf
[1]) ? buf
[1] : '.',
316 qemu_isprint(buf
[2]) ? buf
[2] : '.',
317 qemu_isprint(buf
[3]) ? buf
[3] : '.',
318 qemu_isprint(buf
[4]) ? buf
[4] : '.',
319 qemu_isprint(buf
[5]) ? buf
[5] : '.',
320 qemu_isprint(buf
[6]) ? buf
[6] : '.',
321 qemu_isprint(buf
[7]) ? buf
[7] : '.');
323 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
324 LOG("Invalid magic received");
328 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
332 magic
= be64_to_cpu(magic
);
333 TRACE("Magic is 0x%" PRIx64
, magic
);
336 uint32_t reserved
= 0;
340 TRACE("Checking magic (opts_magic)");
341 if (magic
!= NBD_OPTS_MAGIC
) {
342 LOG("Bad magic received");
345 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
346 LOG("flags read failed");
349 *flags
= be16_to_cpu(tmp
) << 16;
350 /* reserved for future use */
351 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
353 LOG("write failed (reserved)");
356 /* write the export name */
357 magic
= cpu_to_be64(magic
);
358 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
359 LOG("write failed (magic)");
362 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
363 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
364 LOG("write failed (opt)");
367 namesize
= cpu_to_be32(strlen(name
));
368 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
370 LOG("write failed (namesize)");
373 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
374 LOG("write failed (name)");
378 TRACE("Checking magic (cli_magic)");
380 if (magic
!= NBD_CLIENT_MAGIC
) {
381 LOG("Bad magic received");
386 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
390 *size
= be64_to_cpu(s
);
392 TRACE("Size is %" PRIu64
, *size
);
395 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
396 LOG("read failed (flags)");
399 *flags
= be32_to_cpup(flags
);
401 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
402 LOG("read failed (tmp)");
405 *flags
|= be32_to_cpu(tmp
);
407 if (read_sync(csock
, &buf
, 124) != 124) {
408 LOG("read failed (buf)");
414 socket_set_nonblock(csock
);
419 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
421 TRACE("Setting NBD socket");
423 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
425 LOG("Failed to set NBD socket");
429 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
431 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
433 LOG("Failed setting NBD block size");
437 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
439 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
441 LOG("Failed setting size (in blocks)");
445 if (flags
& NBD_FLAG_READ_ONLY
) {
447 TRACE("Setting readonly attribute");
449 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
451 LOG("Failed setting read-only attribute");
456 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0
457 && errno
!= ENOTTY
) {
459 LOG("Failed setting flags");
463 TRACE("Negotiation ended");
468 int nbd_disconnect(int fd
)
470 ioctl(fd
, NBD_CLEAR_QUE
);
471 ioctl(fd
, NBD_DISCONNECT
);
472 ioctl(fd
, NBD_CLEAR_SOCK
);
476 int nbd_client(int fd
)
481 TRACE("Doing NBD loop");
483 ret
= ioctl(fd
, NBD_DO_IT
);
484 if (ret
< 0 && errno
== EPIPE
) {
485 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
486 * the socket via NBD_DISCONNECT. We do not want to return 1 in
493 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
495 TRACE("Clearing NBD queue");
496 ioctl(fd
, NBD_CLEAR_QUE
);
498 TRACE("Clearing NBD socket");
499 ioctl(fd
, NBD_CLEAR_SOCK
);
505 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
510 int nbd_disconnect(int fd
)
515 int nbd_client(int fd
)
521 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
523 uint8_t buf
[NBD_REQUEST_SIZE
];
526 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
527 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
528 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
529 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
530 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
532 TRACE("Sending request to client: "
533 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
534 request
->from
, request
->len
, request
->handle
, request
->type
);
536 ret
= write_sync(csock
, buf
, sizeof(buf
));
541 if (ret
!= sizeof(buf
)) {
542 LOG("writing to socket failed");
548 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
550 uint8_t buf
[NBD_REQUEST_SIZE
];
554 ret
= read_sync(csock
, buf
, sizeof(buf
));
559 if (ret
!= sizeof(buf
)) {
565 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
566 [ 4 .. 7] type (0 == READ, 1 == WRITE)
572 magic
= be32_to_cpup((uint32_t*)buf
);
573 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
574 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
575 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
576 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
578 TRACE("Got request: "
579 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
580 magic
, request
->type
, request
->from
, request
->len
);
582 if (magic
!= NBD_REQUEST_MAGIC
) {
583 LOG("invalid magic (got 0x%x)", magic
);
589 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
591 uint8_t buf
[NBD_REPLY_SIZE
];
595 ret
= read_sync(csock
, buf
, sizeof(buf
));
600 if (ret
!= sizeof(buf
)) {
606 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
607 [ 4 .. 7] error (0 == no error)
611 magic
= be32_to_cpup((uint32_t*)buf
);
612 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
613 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
616 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
617 magic
, reply
->error
, reply
->handle
);
619 if (magic
!= NBD_REPLY_MAGIC
) {
620 LOG("invalid magic (got 0x%x)", magic
);
626 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
628 uint8_t buf
[NBD_REPLY_SIZE
];
632 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
633 [ 4 .. 7] error (0 == no error)
636 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
637 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
638 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
640 TRACE("Sending response to client");
642 ret
= write_sync(csock
, buf
, sizeof(buf
));
647 if (ret
!= sizeof(buf
)) {
648 LOG("writing to socket failed");
654 #define MAX_NBD_REQUESTS 16
656 void nbd_client_get(NBDClient
*client
)
661 void nbd_client_put(NBDClient
*client
)
663 if (--client
->refcount
== 0) {
664 /* The last reference should be dropped by client->close,
665 * which is called by nbd_client_close.
667 assert(client
->closing
);
669 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
672 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
673 nbd_export_put(client
->exp
);
678 void nbd_client_close(NBDClient
*client
)
680 if (client
->closing
) {
684 client
->closing
= true;
686 /* Force requests to finish. They will drop their own references,
687 * then we'll close the socket and free the NBDClient.
689 shutdown(client
->sock
, 2);
691 /* Also tell the client, so that they release their reference. */
693 client
->close(client
);
697 static NBDRequest
*nbd_request_get(NBDClient
*client
)
700 NBDExport
*exp
= client
->exp
;
702 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
703 client
->nb_requests
++;
705 if (QSIMPLEQ_EMPTY(&exp
->requests
)) {
706 req
= g_malloc0(sizeof(NBDRequest
));
707 req
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
709 req
= QSIMPLEQ_FIRST(&exp
->requests
);
710 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
712 nbd_client_get(client
);
713 req
->client
= client
;
717 static void nbd_request_put(NBDRequest
*req
)
719 NBDClient
*client
= req
->client
;
720 QSIMPLEQ_INSERT_HEAD(&client
->exp
->requests
, req
, entry
);
721 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
724 nbd_client_put(client
);
727 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
728 off_t size
, uint32_t nbdflags
,
729 void (*close
)(NBDExport
*))
731 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
732 QSIMPLEQ_INIT(&exp
->requests
);
734 QTAILQ_INIT(&exp
->clients
);
736 exp
->dev_offset
= dev_offset
;
737 exp
->nbdflags
= nbdflags
;
738 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
743 void nbd_export_close(NBDExport
*exp
)
745 NBDClient
*client
, *next
;
748 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
749 nbd_client_close(client
);
754 void nbd_export_get(NBDExport
*exp
)
756 assert(exp
->refcount
> 0);
760 void nbd_export_put(NBDExport
*exp
)
762 assert(exp
->refcount
> 0);
763 if (exp
->refcount
== 1) {
764 nbd_export_close(exp
);
767 if (--exp
->refcount
== 0) {
772 while (!QSIMPLEQ_EMPTY(&exp
->requests
)) {
773 NBDRequest
*first
= QSIMPLEQ_FIRST(&exp
->requests
);
774 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
775 qemu_vfree(first
->data
);
783 static int nbd_can_read(void *opaque
);
784 static void nbd_read(void *opaque
);
785 static void nbd_restart_write(void *opaque
);
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 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
796 nbd_restart_write
, client
);
797 client
->send_coroutine
= qemu_coroutine_self();
800 rc
= nbd_send_reply(csock
, reply
);
802 socket_set_cork(csock
, 1);
803 rc
= nbd_send_reply(csock
, reply
);
805 ret
= qemu_co_send(csock
, req
->data
, len
);
810 socket_set_cork(csock
, 0);
813 client
->send_coroutine
= NULL
;
814 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
815 qemu_co_mutex_unlock(&client
->send_lock
);
819 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
821 NBDClient
*client
= req
->client
;
822 int csock
= client
->sock
;
825 client
->recv_coroutine
= qemu_coroutine_self();
826 rc
= nbd_receive_request(csock
, request
);
834 if (request
->len
> NBD_BUFFER_SIZE
) {
835 LOG("len (%u) is larger than max len (%u)",
836 request
->len
, NBD_BUFFER_SIZE
);
841 if ((request
->from
+ request
->len
) < request
->from
) {
842 LOG("integer overflow detected! "
843 "you're probably being attacked");
848 TRACE("Decoding type");
850 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
851 TRACE("Reading %u byte(s)", request
->len
);
853 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
854 LOG("reading from socket failed");
862 client
->recv_coroutine
= NULL
;
866 static void nbd_trip(void *opaque
)
868 NBDClient
*client
= opaque
;
869 NBDExport
*exp
= client
->exp
;
871 struct nbd_request request
;
872 struct nbd_reply reply
;
875 TRACE("Reading request.");
876 if (client
->closing
) {
880 req
= nbd_request_get(client
);
881 ret
= nbd_co_receive_request(req
, &request
);
882 if (ret
== -EAGAIN
) {
889 reply
.handle
= request
.handle
;
897 if ((request
.from
+ request
.len
) > exp
->size
) {
898 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
899 ", Offset: %" PRIu64
"\n",
900 request
.from
, request
.len
,
901 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
902 LOG("requested operation past EOF--bad client?");
903 goto invalid_request
;
906 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
908 TRACE("Request type is READ");
910 if (request
.type
& NBD_CMD_FLAG_FUA
) {
911 ret
= bdrv_co_flush(exp
->bs
);
919 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
920 req
->data
, request
.len
/ 512);
922 LOG("reading from file failed");
927 TRACE("Read %u byte(s)", request
.len
);
928 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
932 TRACE("Request type is WRITE");
934 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
935 TRACE("Server is read-only, return error");
940 TRACE("Writing to device");
942 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
943 req
->data
, request
.len
/ 512);
945 LOG("writing to file failed");
950 if (request
.type
& NBD_CMD_FLAG_FUA
) {
951 ret
= bdrv_co_flush(exp
->bs
);
959 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
964 TRACE("Request type is DISCONNECT");
968 TRACE("Request type is FLUSH");
970 ret
= bdrv_co_flush(exp
->bs
);
975 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
980 TRACE("Request type is TRIM");
981 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
984 LOG("discard failed");
987 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
992 LOG("invalid request type (%u) received", request
.type
);
994 reply
.error
= -EINVAL
;
996 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1002 TRACE("Request/Reply complete");
1005 nbd_request_put(req
);
1009 nbd_request_put(req
);
1010 nbd_client_close(client
);
1013 static int nbd_can_read(void *opaque
)
1015 NBDClient
*client
= opaque
;
1017 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1020 static void nbd_read(void *opaque
)
1022 NBDClient
*client
= opaque
;
1024 if (client
->recv_coroutine
) {
1025 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1027 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1031 static void nbd_restart_write(void *opaque
)
1033 NBDClient
*client
= opaque
;
1035 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1038 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1039 void (*close
)(NBDClient
*))
1042 client
= g_malloc0(sizeof(NBDClient
));
1043 client
->refcount
= 1;
1045 client
->sock
= csock
;
1046 if (nbd_send_negotiate(client
) < 0) {
1050 client
->close
= close
;
1051 qemu_co_mutex_init(&client
->send_lock
);
1052 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1054 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1055 nbd_export_get(exp
);