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 Error *local_err = NULL;
212 int fd = inet_connect(address_and_port, &local_err);
214 if (local_err != NULL) {
215 qerror_report_err(local_err);
216 error_free(local_err);
221 int tcp_socket_incoming(const char *address, uint16_t port)
223 char address_and_port[128];
224 combine_addr(address_and_port, 128, address, port);
225 return tcp_socket_incoming_spec(address_and_port);
228 int tcp_socket_incoming_spec(const char *address_and_port)
230 Error *local_err = NULL;
231 int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
233 if (local_err != NULL) {
234 qerror_report_err(local_err);
235 error_free(local_err);
240 int unix_socket_incoming(const char *path)
242 Error *local_err = NULL;
243 int fd = unix_listen(path, NULL, 0, &local_err);
245 if (local_err != NULL) {
246 qerror_report_err(local_err);
247 error_free(local_err);
252 int unix_socket_outgoing(const char *path)
254 Error *local_err = NULL;
255 int fd = unix_connect(path, &local_err);
257 if (local_err != NULL) {
258 qerror_report_err(local_err);
259 error_free(local_err);
264 /* Basic flow for negotiation
291 static int nbd_receive_options(NBDClient *client)
293 int csock = client->sock;
295 uint32_t tmp, length;
300 [ 0 .. 3] reserved (0)
301 [ 4 .. 11] NBD_OPTS_MAGIC
302 [12 .. 15] NBD_OPT_EXPORT_NAME
304 [20 .. xx] export name (length bytes)
308 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
312 TRACE("Checking reserved");
314 LOG("Bad reserved received");
318 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
322 TRACE("Checking reserved");
323 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
324 LOG("Bad magic received");
328 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
332 TRACE("Checking option");
333 if (tmp != be32_to_cpu(NBD_OPT_EXPORT_NAME)) {
334 LOG("Bad option received");
338 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
342 TRACE("Checking length");
343 length = be32_to_cpu(length);
345 LOG("Bad length received");
348 if (read_sync(csock, name, length) != length) {
354 client->exp = nbd_export_find(name);
356 LOG("export not found");
360 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
361 nbd_export_get(client->exp);
363 TRACE("Option negotiation succeeded.");
369 static int nbd_send_negotiate(NBDClient *client)
371 int csock = client->sock;
372 char buf[8 + 8 + 8 + 128];
374 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
375 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
377 /* Negotiation header without options:
378 [ 0 .. 7] passwd ("NBDMAGIC")
379 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
381 [24 .. 25] server flags (0)
382 [24 .. 27] export flags
383 [28 .. 151] reserved (0)
385 Negotiation header with options, part 1:
386 [ 0 .. 7] passwd ("NBDMAGIC")
387 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
388 [16 .. 17] server flags (0)
390 part 2 (after options are sent):
392 [26 .. 27] export flags
393 [28 .. 151] reserved (0)
396 socket_set_block(csock);
399 TRACE("Beginning negotiation.");
400 memcpy(buf, "NBDMAGIC", 8);
402 assert ((client->exp->nbdflags & ~65535) == 0);
403 cpu_to_be64w((uint64_t*)(buf + 8), NBD_CLIENT_MAGIC);
404 cpu_to_be64w((uint64_t*)(buf + 16), client->exp->size);
405 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
407 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
409 memset(buf + 28, 0, 124);
412 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
417 if (write_sync(csock, buf, 18) != 18) {
421 rc = nbd_receive_options(client);
423 LOG("option negotiation failed");
427 assert ((client->exp->nbdflags & ~65535) == 0);
428 cpu_to_be64w((uint64_t*)(buf + 18), client->exp->size);
429 cpu_to_be16w((uint16_t*)(buf + 26), client->exp->nbdflags | myflags);
430 if (write_sync(csock, buf + 18, sizeof(buf) - 18) != sizeof(buf) - 18) {
436 TRACE("Negotiation succeeded.");
439 socket_set_nonblock(csock);
443 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
444 off_t *size, size_t *blocksize)
451 TRACE("Receiving negotiation.");
453 socket_set_block(csock);
456 if (read_sync(csock, buf, 8) != 8) {
462 if (strlen(buf) == 0) {
463 LOG("server connection closed");
467 TRACE("Magic is %c%c%c%c%c%c%c%c",
468 qemu_isprint(buf[0]) ? buf[0] : '.',
469 qemu_isprint(buf[1]) ? buf[1] : '.',
470 qemu_isprint(buf[2]) ? buf[2] : '.',
471 qemu_isprint(buf[3]) ? buf[3] : '.',
472 qemu_isprint(buf[4]) ? buf[4] : '.',
473 qemu_isprint(buf[5]) ? buf[5] : '.',
474 qemu_isprint(buf[6]) ? buf[6] : '.',
475 qemu_isprint(buf[7]) ? buf[7] : '.');
477 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
478 LOG("Invalid magic received");
482 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
486 magic = be64_to_cpu(magic);
487 TRACE("Magic is 0x%" PRIx64, magic);
490 uint32_t reserved = 0;
494 TRACE("Checking magic (opts_magic)");
495 if (magic != NBD_OPTS_MAGIC) {
496 LOG("Bad magic received");
499 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
500 LOG("flags read failed");
503 *flags = be16_to_cpu(tmp) << 16;
504 /* reserved for future use */
505 if (write_sync(csock, &reserved, sizeof(reserved)) !=
507 LOG("write failed (reserved)");
510 /* write the export name */
511 magic = cpu_to_be64(magic);
512 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
513 LOG("write failed (magic)");
516 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
517 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
518 LOG("write failed (opt)");
521 namesize = cpu_to_be32(strlen(name));
522 if (write_sync(csock, &namesize, sizeof(namesize)) !=
524 LOG("write failed (namesize)");
527 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
528 LOG("write failed (name)");
532 TRACE("Checking magic (cli_magic)");
534 if (magic != NBD_CLIENT_MAGIC) {
535 LOG("Bad magic received");
540 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
544 *size = be64_to_cpu(s);
546 TRACE("Size is %" PRIu64, *size);
549 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
550 LOG("read failed (flags)");
553 *flags = be32_to_cpup(flags);
555 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
556 LOG("read failed (tmp)");
559 *flags |= be32_to_cpu(tmp);
561 if (read_sync(csock, &buf, 124) != 124) {
562 LOG("read failed (buf)");
568 socket_set_nonblock(csock);
573 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
575 TRACE("Setting NBD socket");
577 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
579 LOG("Failed to set NBD socket");
583 TRACE("Setting block size to %lu", (unsigned long)blocksize);
585 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
587 LOG("Failed setting NBD block size");
591 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
593 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
595 LOG("Failed setting size (in blocks)");
599 if (flags & NBD_FLAG_READ_ONLY) {
601 TRACE("Setting readonly attribute");
603 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
605 LOG("Failed setting read-only attribute");
610 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
611 && errno != ENOTTY) {
613 LOG("Failed setting flags");
617 TRACE("Negotiation ended");
622 int nbd_disconnect(int fd)
624 ioctl(fd, NBD_CLEAR_QUE);
625 ioctl(fd, NBD_DISCONNECT);
626 ioctl(fd, NBD_CLEAR_SOCK);
630 int nbd_client(int fd)
635 TRACE("Doing NBD loop");
637 ret = ioctl(fd, NBD_DO_IT);
638 if (ret < 0 && errno == EPIPE) {
639 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
640 * the socket via NBD_DISCONNECT. We do not want to return 1 in
647 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
649 TRACE("Clearing NBD queue");
650 ioctl(fd, NBD_CLEAR_QUE);
652 TRACE("Clearing NBD socket");
653 ioctl(fd, NBD_CLEAR_SOCK);
659 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
664 int nbd_disconnect(int fd)
669 int nbd_client(int fd)
675 ssize_t nbd_send_request(int csock, struct nbd_request *request)
677 uint8_t buf[NBD_REQUEST_SIZE];
680 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
681 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
682 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
683 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
684 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
686 TRACE("Sending request to client: "
687 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
688 request->from, request->len, request->handle, request->type);
690 ret = write_sync(csock, buf, sizeof(buf));
695 if (ret != sizeof(buf)) {
696 LOG("writing to socket failed");
702 static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
704 uint8_t buf[NBD_REQUEST_SIZE];
708 ret = read_sync(csock, buf, sizeof(buf));
713 if (ret != sizeof(buf)) {
719 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
720 [ 4 .. 7] type (0 == READ, 1 == WRITE)
726 magic = be32_to_cpup((uint32_t*)buf);
727 request->type = be32_to_cpup((uint32_t*)(buf + 4));
728 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
729 request->from = be64_to_cpup((uint64_t*)(buf + 16));
730 request->len = be32_to_cpup((uint32_t*)(buf + 24));
732 TRACE("Got request: "
733 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
734 magic, request->type, request->from, request->len);
736 if (magic != NBD_REQUEST_MAGIC) {
737 LOG("invalid magic (got 0x%x)", magic);
743 ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
745 uint8_t buf[NBD_REPLY_SIZE];
749 ret = read_sync(csock, buf, sizeof(buf));
754 if (ret != sizeof(buf)) {
760 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
761 [ 4 .. 7] error (0 == no error)
765 magic = be32_to_cpup((uint32_t*)buf);
766 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
767 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
770 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
771 magic, reply->error, reply->handle);
773 if (magic != NBD_REPLY_MAGIC) {
774 LOG("invalid magic (got 0x%x)", magic);
780 static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
782 uint8_t buf[NBD_REPLY_SIZE];
786 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
787 [ 4 .. 7] error (0 == no error)
790 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
791 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
792 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
794 TRACE("Sending response to client");
796 ret = write_sync(csock, buf, sizeof(buf));
801 if (ret != sizeof(buf)) {
802 LOG("writing to socket failed");
808 #define MAX_NBD_REQUESTS 16
810 void nbd_client_get(NBDClient *client)
815 void nbd_client_put(NBDClient *client)
817 if (--client->refcount == 0) {
818 /* The last reference should be dropped by client->close,
819 * which is called by nbd_client_close.
821 assert(client->closing);
823 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
827 QTAILQ_REMOVE(&client->exp->clients, client, next);
828 nbd_export_put(client->exp);
834 void nbd_client_close(NBDClient *client)
836 if (client->closing) {
840 client->closing = true;
842 /* Force requests to finish. They will drop their own references,
843 * then we'll close the socket and free the NBDClient.
845 shutdown(client->sock, 2);
847 /* Also tell the client, so that they release their reference. */
849 client->close(client);
853 static NBDRequest *nbd_request_get(NBDClient *client)
856 NBDExport *exp = client->exp;
858 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
859 client->nb_requests++;
861 if (QSIMPLEQ_EMPTY(&exp->requests)) {
862 req = g_malloc0(sizeof(NBDRequest));
863 req->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
865 req = QSIMPLEQ_FIRST(&exp->requests);
866 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
868 nbd_client_get(client);
869 req->client = client;
873 static void nbd_request_put(NBDRequest *req)
875 NBDClient *client = req->client;
876 QSIMPLEQ_INSERT_HEAD(&client->exp->requests, req, entry);
877 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
880 nbd_client_put(client);
883 NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
884 off_t size, uint32_t nbdflags,
885 void (*close)(NBDExport *))
887 NBDExport *exp = g_malloc0(sizeof(NBDExport));
888 QSIMPLEQ_INIT(&exp->requests);
890 QTAILQ_INIT(&exp->clients);
892 exp->dev_offset = dev_offset;
893 exp->nbdflags = nbdflags;
894 exp->size = size == -1 ? bdrv_getlength(bs) : size;
899 NBDExport *nbd_export_find(const char *name)
902 QTAILQ_FOREACH(exp, &exports, next) {
903 if (strcmp(name, exp->name) == 0) {
911 void nbd_export_set_name(NBDExport *exp, const char *name)
913 if (exp->name == name) {
918 if (exp->name != NULL) {
921 QTAILQ_REMOVE(&exports, exp, next);
926 exp->name = g_strdup(name);
927 QTAILQ_INSERT_TAIL(&exports, exp, next);
932 void nbd_export_close(NBDExport *exp)
934 NBDClient *client, *next;
937 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
938 nbd_client_close(client);
940 nbd_export_set_name(exp, NULL);
944 void nbd_export_get(NBDExport *exp)
946 assert(exp->refcount > 0);
950 void nbd_export_put(NBDExport *exp)
952 assert(exp->refcount > 0);
953 if (exp->refcount == 1) {
954 nbd_export_close(exp);
957 if (--exp->refcount == 0) {
958 assert(exp->name == NULL);
964 while (!QSIMPLEQ_EMPTY(&exp->requests)) {
965 NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
966 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
967 qemu_vfree(first->data);
975 BlockDriverState *nbd_export_get_blockdev(NBDExport *exp)
980 void nbd_export_close_all(void)
982 NBDExport *exp, *next;
984 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
985 nbd_export_close(exp);
989 static int nbd_can_read(void *opaque);
990 static void nbd_read(void *opaque);
991 static void nbd_restart_write(void *opaque);
993 static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
996 NBDClient *client = req->client;
997 int csock = client->sock;
1000 qemu_co_mutex_lock(&client->send_lock);
1001 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
1002 nbd_restart_write, client);
1003 client->send_coroutine = qemu_coroutine_self();
1006 rc = nbd_send_reply(csock, reply);
1008 socket_set_cork(csock, 1);
1009 rc = nbd_send_reply(csock, reply);
1011 ret = qemu_co_send(csock, req->data, len);
1016 socket_set_cork(csock, 0);
1019 client->send_coroutine = NULL;
1020 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
1021 qemu_co_mutex_unlock(&client->send_lock);
1025 static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
1027 NBDClient *client = req->client;
1028 int csock = client->sock;
1031 client->recv_coroutine = qemu_coroutine_self();
1032 rc = nbd_receive_request(csock, request);
1034 if (rc != -EAGAIN) {
1040 if (request->len > NBD_BUFFER_SIZE) {
1041 LOG("len (%u) is larger than max len (%u)",
1042 request->len, NBD_BUFFER_SIZE);
1047 if ((request->from + request->len) < request->from) {
1048 LOG("integer overflow detected! "
1049 "you're probably being attacked");
1054 TRACE("Decoding type");
1056 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
1057 TRACE("Reading %u byte(s)", request->len);
1059 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
1060 LOG("reading from socket failed");
1068 client->recv_coroutine = NULL;
1072 static void nbd_trip(void *opaque)
1074 NBDClient *client = opaque;
1075 NBDExport *exp = client->exp;
1077 struct nbd_request request;
1078 struct nbd_reply reply;
1081 TRACE("Reading request.");
1082 if (client->closing) {
1086 req = nbd_request_get(client);
1087 ret = nbd_co_receive_request(req, &request);
1088 if (ret == -EAGAIN) {
1095 reply.handle = request.handle;
1103 if ((request.from + request.len) > exp->size) {
1104 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1105 ", Offset: %" PRIu64 "\n",
1106 request.from, request.len,
1107 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
1108 LOG("requested operation past EOF--bad client?");
1109 goto invalid_request;
1112 switch (request.type & NBD_CMD_MASK_COMMAND) {
1114 TRACE("Request type is READ");
1116 if (request.type & NBD_CMD_FLAG_FUA) {
1117 ret = bdrv_co_flush(exp->bs);
1119 LOG("flush failed");
1125 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
1126 req->data, request.len / 512);
1128 LOG("reading from file failed");
1133 TRACE("Read %u byte(s)", request.len);
1134 if (nbd_co_send_reply(req, &reply, request.len) < 0)
1138 TRACE("Request type is WRITE");
1140 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
1141 TRACE("Server is read-only, return error");
1142 reply.error = EROFS;
1146 TRACE("Writing to device");
1148 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
1149 req->data, request.len / 512);
1151 LOG("writing to file failed");
1156 if (request.type & NBD_CMD_FLAG_FUA) {
1157 ret = bdrv_co_flush(exp->bs);
1159 LOG("flush failed");
1165 if (nbd_co_send_reply(req, &reply, 0) < 0) {
1170 TRACE("Request type is DISCONNECT");
1174 TRACE("Request type is FLUSH");
1176 ret = bdrv_co_flush(exp->bs);
1178 LOG("flush failed");
1181 if (nbd_co_send_reply(req, &reply, 0) < 0) {
1186 TRACE("Request type is TRIM");
1187 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
1190 LOG("discard failed");
1193 if (nbd_co_send_reply(req, &reply, 0) < 0) {
1198 LOG("invalid request type (%u) received", request.type);
1200 reply.error = -EINVAL;
1202 if (nbd_co_send_reply(req, &reply, 0) < 0) {
1208 TRACE("Request/Reply complete");
1211 nbd_request_put(req);
1215 nbd_request_put(req);
1216 nbd_client_close(client);
1219 static int nbd_can_read(void *opaque)
1221 NBDClient *client = opaque;
1223 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
1226 static void nbd_read(void *opaque)
1228 NBDClient *client = opaque;
1230 if (client->recv_coroutine) {
1231 qemu_coroutine_enter(client->recv_coroutine, NULL);
1233 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
1237 static void nbd_restart_write(void *opaque)
1239 NBDClient *client = opaque;
1241 qemu_coroutine_enter(client->send_coroutine, NULL);
1244 NBDClient *nbd_client_new(NBDExport *exp, int csock,
1245 void (*close)(NBDClient *))
1248 client = g_malloc0(sizeof(NBDClient));
1249 client->refcount = 1;
1251 client->sock = csock;
1252 if (nbd_send_negotiate(client) < 0) {
1256 client->close = close;
1257 qemu_co_mutex_init(&client->send_lock);
1258 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
1261 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1262 nbd_export_get(exp);