]>
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
);
100 QTAILQ_HEAD(, NBDClient
) clients
;
101 QSIMPLEQ_HEAD(, NBDRequest
) requests
;
102 QTAILQ_ENTRY(NBDExport
) next
;
105 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
109 void (*close
)(NBDClient
*client
);
114 Coroutine
*recv_coroutine
;
117 Coroutine
*send_coroutine
;
119 QTAILQ_ENTRY(NBDClient
) next
;
124 /* That's all folks */
126 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
131 if (qemu_in_coroutine()) {
133 return qemu_co_recv(fd
, buffer
, size
);
135 return qemu_co_send(fd
, buffer
, size
);
139 while (offset
< size
) {
143 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
145 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
149 err
= socket_error();
151 /* recoverable error */
152 if (err
== EINTR
|| (offset
> 0 && err
== EAGAIN
)) {
156 /* unrecoverable error */
171 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
173 /* Sockets are kept in blocking mode in the negotiation phase. After
174 * that, a non-readable socket simply means that another thread stole
175 * our request/reply. Synchronization is done with recv_coroutine, so
176 * that this is coroutine-safe.
178 return nbd_wr_sync(fd
, buffer
, size
, true);
181 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
185 /* For writes, we do expect the socket to be writable. */
186 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
187 } while (ret
== -EAGAIN
);
191 static void combine_addr(char *buf
, size_t len
, const char* address
,
194 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
195 if (strstr(address
, ":")) {
196 snprintf(buf
, len
, "[%s]:%u", address
, port
);
198 snprintf(buf
, len
, "%s:%u", address
, port
);
202 int tcp_socket_outgoing(const char *address
, uint16_t port
)
204 char address_and_port
[128];
205 combine_addr(address_and_port
, 128, address
, port
);
206 return tcp_socket_outgoing_spec(address_and_port
);
209 int tcp_socket_outgoing_spec(const char *address_and_port
)
211 return inet_connect(address_and_port
, true, NULL
, NULL
);
214 int tcp_socket_incoming(const char *address
, uint16_t port
)
216 char address_and_port
[128];
217 combine_addr(address_and_port
, 128, address
, port
);
218 return tcp_socket_incoming_spec(address_and_port
);
221 int tcp_socket_incoming_spec(const char *address_and_port
)
225 return inet_listen(address_and_port
, ostr
, olen
, SOCK_STREAM
, 0, NULL
);
228 int unix_socket_incoming(const char *path
)
233 return unix_listen(path
, ostr
, olen
);
236 int unix_socket_outgoing(const char *path
)
238 return unix_connect(path
);
255 static int nbd_send_negotiate(NBDClient
*client
)
257 int csock
= client
->sock
;
258 char buf
[8 + 8 + 8 + 128];
262 [ 0 .. 7] passwd ("NBDMAGIC")
263 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
266 [28 .. 151] reserved (0)
269 socket_set_block(csock
);
272 TRACE("Beginning negotiation.");
273 memcpy(buf
, "NBDMAGIC", 8);
274 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
275 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
276 cpu_to_be32w((uint32_t*)(buf
+ 24),
277 client
->exp
->nbdflags
| NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
278 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
279 memset(buf
+ 28, 0, 124);
281 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
286 TRACE("Negotiation succeeded.");
289 socket_set_nonblock(csock
);
293 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
294 off_t
*size
, size_t *blocksize
)
301 TRACE("Receiving negotiation.");
303 socket_set_block(csock
);
306 if (read_sync(csock
, buf
, 8) != 8) {
312 if (strlen(buf
) == 0) {
313 LOG("server connection closed");
317 TRACE("Magic is %c%c%c%c%c%c%c%c",
318 qemu_isprint(buf
[0]) ? buf
[0] : '.',
319 qemu_isprint(buf
[1]) ? buf
[1] : '.',
320 qemu_isprint(buf
[2]) ? buf
[2] : '.',
321 qemu_isprint(buf
[3]) ? buf
[3] : '.',
322 qemu_isprint(buf
[4]) ? buf
[4] : '.',
323 qemu_isprint(buf
[5]) ? buf
[5] : '.',
324 qemu_isprint(buf
[6]) ? buf
[6] : '.',
325 qemu_isprint(buf
[7]) ? buf
[7] : '.');
327 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
328 LOG("Invalid magic received");
332 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
336 magic
= be64_to_cpu(magic
);
337 TRACE("Magic is 0x%" PRIx64
, magic
);
340 uint32_t reserved
= 0;
344 TRACE("Checking magic (opts_magic)");
345 if (magic
!= NBD_OPTS_MAGIC
) {
346 LOG("Bad magic received");
349 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
350 LOG("flags read failed");
353 *flags
= be16_to_cpu(tmp
) << 16;
354 /* reserved for future use */
355 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
357 LOG("write failed (reserved)");
360 /* write the export name */
361 magic
= cpu_to_be64(magic
);
362 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
363 LOG("write failed (magic)");
366 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
367 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
368 LOG("write failed (opt)");
371 namesize
= cpu_to_be32(strlen(name
));
372 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
374 LOG("write failed (namesize)");
377 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
378 LOG("write failed (name)");
382 TRACE("Checking magic (cli_magic)");
384 if (magic
!= NBD_CLIENT_MAGIC
) {
385 LOG("Bad magic received");
390 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
394 *size
= be64_to_cpu(s
);
396 TRACE("Size is %" PRIu64
, *size
);
399 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
400 LOG("read failed (flags)");
403 *flags
= be32_to_cpup(flags
);
405 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
406 LOG("read failed (tmp)");
409 *flags
|= be32_to_cpu(tmp
);
411 if (read_sync(csock
, &buf
, 124) != 124) {
412 LOG("read failed (buf)");
418 socket_set_nonblock(csock
);
423 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
425 TRACE("Setting NBD socket");
427 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
429 LOG("Failed to set NBD socket");
433 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
435 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
437 LOG("Failed setting NBD block size");
441 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
443 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
445 LOG("Failed setting size (in blocks)");
449 if (flags
& NBD_FLAG_READ_ONLY
) {
451 TRACE("Setting readonly attribute");
453 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
455 LOG("Failed setting read-only attribute");
460 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0
461 && errno
!= ENOTTY
) {
463 LOG("Failed setting flags");
467 TRACE("Negotiation ended");
472 int nbd_disconnect(int fd
)
474 ioctl(fd
, NBD_CLEAR_QUE
);
475 ioctl(fd
, NBD_DISCONNECT
);
476 ioctl(fd
, NBD_CLEAR_SOCK
);
480 int nbd_client(int fd
)
485 TRACE("Doing NBD loop");
487 ret
= ioctl(fd
, NBD_DO_IT
);
488 if (ret
< 0 && errno
== EPIPE
) {
489 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
490 * the socket via NBD_DISCONNECT. We do not want to return 1 in
497 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
499 TRACE("Clearing NBD queue");
500 ioctl(fd
, NBD_CLEAR_QUE
);
502 TRACE("Clearing NBD socket");
503 ioctl(fd
, NBD_CLEAR_SOCK
);
509 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
514 int nbd_disconnect(int fd
)
519 int nbd_client(int fd
)
525 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
527 uint8_t buf
[NBD_REQUEST_SIZE
];
530 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
531 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
532 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
533 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
534 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
536 TRACE("Sending request to client: "
537 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
538 request
->from
, request
->len
, request
->handle
, request
->type
);
540 ret
= write_sync(csock
, buf
, sizeof(buf
));
545 if (ret
!= sizeof(buf
)) {
546 LOG("writing to socket failed");
552 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
554 uint8_t buf
[NBD_REQUEST_SIZE
];
558 ret
= read_sync(csock
, buf
, sizeof(buf
));
563 if (ret
!= sizeof(buf
)) {
569 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
570 [ 4 .. 7] type (0 == READ, 1 == WRITE)
576 magic
= be32_to_cpup((uint32_t*)buf
);
577 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
578 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
579 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
580 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
582 TRACE("Got request: "
583 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
584 magic
, request
->type
, request
->from
, request
->len
);
586 if (magic
!= NBD_REQUEST_MAGIC
) {
587 LOG("invalid magic (got 0x%x)", magic
);
593 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
595 uint8_t buf
[NBD_REPLY_SIZE
];
599 ret
= read_sync(csock
, buf
, sizeof(buf
));
604 if (ret
!= sizeof(buf
)) {
610 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
611 [ 4 .. 7] error (0 == no error)
615 magic
= be32_to_cpup((uint32_t*)buf
);
616 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
617 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
620 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
621 magic
, reply
->error
, reply
->handle
);
623 if (magic
!= NBD_REPLY_MAGIC
) {
624 LOG("invalid magic (got 0x%x)", magic
);
630 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
632 uint8_t buf
[NBD_REPLY_SIZE
];
636 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
637 [ 4 .. 7] error (0 == no error)
640 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
641 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
642 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
644 TRACE("Sending response to client");
646 ret
= write_sync(csock
, buf
, sizeof(buf
));
651 if (ret
!= sizeof(buf
)) {
652 LOG("writing to socket failed");
658 #define MAX_NBD_REQUESTS 16
660 void nbd_client_get(NBDClient
*client
)
665 void nbd_client_put(NBDClient
*client
)
667 if (--client
->refcount
== 0) {
668 /* The last reference should be dropped by client->close,
669 * which is called by nbd_client_close.
671 assert(client
->closing
);
673 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
676 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
677 nbd_export_put(client
->exp
);
682 void nbd_client_close(NBDClient
*client
)
684 if (client
->closing
) {
688 client
->closing
= true;
690 /* Force requests to finish. They will drop their own references,
691 * then we'll close the socket and free the NBDClient.
693 shutdown(client
->sock
, 2);
695 /* Also tell the client, so that they release their reference. */
697 client
->close(client
);
701 static NBDRequest
*nbd_request_get(NBDClient
*client
)
704 NBDExport
*exp
= client
->exp
;
706 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
707 client
->nb_requests
++;
709 if (QSIMPLEQ_EMPTY(&exp
->requests
)) {
710 req
= g_malloc0(sizeof(NBDRequest
));
711 req
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
713 req
= QSIMPLEQ_FIRST(&exp
->requests
);
714 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
716 nbd_client_get(client
);
717 req
->client
= client
;
721 static void nbd_request_put(NBDRequest
*req
)
723 NBDClient
*client
= req
->client
;
724 QSIMPLEQ_INSERT_HEAD(&client
->exp
->requests
, req
, entry
);
725 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
728 nbd_client_put(client
);
731 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
732 off_t size
, uint32_t nbdflags
,
733 void (*close
)(NBDExport
*))
735 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
736 QSIMPLEQ_INIT(&exp
->requests
);
738 QTAILQ_INIT(&exp
->clients
);
740 exp
->dev_offset
= dev_offset
;
741 exp
->nbdflags
= nbdflags
;
742 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
747 NBDExport
*nbd_export_find(const char *name
)
750 QTAILQ_FOREACH(exp
, &exports
, next
) {
751 if (strcmp(name
, exp
->name
) == 0) {
759 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
761 if (exp
->name
== name
) {
766 if (exp
->name
!= NULL
) {
769 QTAILQ_REMOVE(&exports
, exp
, next
);
774 exp
->name
= g_strdup(name
);
775 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
780 void nbd_export_close(NBDExport
*exp
)
782 NBDClient
*client
, *next
;
785 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
786 nbd_client_close(client
);
791 void nbd_export_get(NBDExport
*exp
)
793 assert(exp
->refcount
> 0);
797 void nbd_export_put(NBDExport
*exp
)
799 assert(exp
->refcount
> 0);
800 if (exp
->refcount
== 1) {
801 nbd_export_close(exp
);
804 if (--exp
->refcount
== 0) {
805 assert(exp
->name
== NULL
);
811 while (!QSIMPLEQ_EMPTY(&exp
->requests
)) {
812 NBDRequest
*first
= QSIMPLEQ_FIRST(&exp
->requests
);
813 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
814 qemu_vfree(first
->data
);
822 void nbd_export_close_all(void)
824 NBDExport
*exp
, *next
;
826 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
827 nbd_export_close(exp
);
828 nbd_export_set_name(exp
, NULL
);
832 static int nbd_can_read(void *opaque
);
833 static void nbd_read(void *opaque
);
834 static void nbd_restart_write(void *opaque
);
836 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
839 NBDClient
*client
= req
->client
;
840 int csock
= client
->sock
;
843 qemu_co_mutex_lock(&client
->send_lock
);
844 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
845 nbd_restart_write
, client
);
846 client
->send_coroutine
= qemu_coroutine_self();
849 rc
= nbd_send_reply(csock
, reply
);
851 socket_set_cork(csock
, 1);
852 rc
= nbd_send_reply(csock
, reply
);
854 ret
= qemu_co_send(csock
, req
->data
, len
);
859 socket_set_cork(csock
, 0);
862 client
->send_coroutine
= NULL
;
863 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
864 qemu_co_mutex_unlock(&client
->send_lock
);
868 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
870 NBDClient
*client
= req
->client
;
871 int csock
= client
->sock
;
874 client
->recv_coroutine
= qemu_coroutine_self();
875 rc
= nbd_receive_request(csock
, request
);
883 if (request
->len
> NBD_BUFFER_SIZE
) {
884 LOG("len (%u) is larger than max len (%u)",
885 request
->len
, NBD_BUFFER_SIZE
);
890 if ((request
->from
+ request
->len
) < request
->from
) {
891 LOG("integer overflow detected! "
892 "you're probably being attacked");
897 TRACE("Decoding type");
899 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
900 TRACE("Reading %u byte(s)", request
->len
);
902 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
903 LOG("reading from socket failed");
911 client
->recv_coroutine
= NULL
;
915 static void nbd_trip(void *opaque
)
917 NBDClient
*client
= opaque
;
918 NBDExport
*exp
= client
->exp
;
920 struct nbd_request request
;
921 struct nbd_reply reply
;
924 TRACE("Reading request.");
925 if (client
->closing
) {
929 req
= nbd_request_get(client
);
930 ret
= nbd_co_receive_request(req
, &request
);
931 if (ret
== -EAGAIN
) {
938 reply
.handle
= request
.handle
;
946 if ((request
.from
+ request
.len
) > exp
->size
) {
947 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
948 ", Offset: %" PRIu64
"\n",
949 request
.from
, request
.len
,
950 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
951 LOG("requested operation past EOF--bad client?");
952 goto invalid_request
;
955 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
957 TRACE("Request type is READ");
959 if (request
.type
& NBD_CMD_FLAG_FUA
) {
960 ret
= bdrv_co_flush(exp
->bs
);
968 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
969 req
->data
, request
.len
/ 512);
971 LOG("reading from file failed");
976 TRACE("Read %u byte(s)", request
.len
);
977 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
981 TRACE("Request type is WRITE");
983 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
984 TRACE("Server is read-only, return error");
989 TRACE("Writing to device");
991 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
992 req
->data
, request
.len
/ 512);
994 LOG("writing to file failed");
999 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1000 ret
= bdrv_co_flush(exp
->bs
);
1002 LOG("flush failed");
1008 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1013 TRACE("Request type is DISCONNECT");
1017 TRACE("Request type is FLUSH");
1019 ret
= bdrv_co_flush(exp
->bs
);
1021 LOG("flush failed");
1024 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1029 TRACE("Request type is TRIM");
1030 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1033 LOG("discard failed");
1036 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1041 LOG("invalid request type (%u) received", request
.type
);
1043 reply
.error
= -EINVAL
;
1045 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1051 TRACE("Request/Reply complete");
1054 nbd_request_put(req
);
1058 nbd_request_put(req
);
1059 nbd_client_close(client
);
1062 static int nbd_can_read(void *opaque
)
1064 NBDClient
*client
= opaque
;
1066 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1069 static void nbd_read(void *opaque
)
1071 NBDClient
*client
= opaque
;
1073 if (client
->recv_coroutine
) {
1074 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1076 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1080 static void nbd_restart_write(void *opaque
)
1082 NBDClient
*client
= opaque
;
1084 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1087 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1088 void (*close
)(NBDClient
*))
1091 client
= g_malloc0(sizeof(NBDClient
));
1092 client
->refcount
= 1;
1094 client
->sock
= csock
;
1095 if (nbd_send_negotiate(client
) < 0) {
1099 client
->close
= close
;
1100 qemu_co_mutex_init(&client
->send_lock
);
1101 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1103 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1104 nbd_export_get(exp
);