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/>.
19 #include "block/nbd.h"
20 #include "block/block.h"
22 #include "block/coroutine.h"
27 #include <sys/ioctl.h>
29 #if defined(__sun__) || defined(__HAIKU__)
30 #include <sys/ioccom.h>
39 #include "qemu/sockets.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 QTAILQ_ENTRY(NBDExport
) next
;
104 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
108 void (*close
)(NBDClient
*client
);
113 Coroutine
*recv_coroutine
;
116 Coroutine
*send_coroutine
;
118 QTAILQ_ENTRY(NBDClient
) next
;
123 /* That's all folks */
125 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
130 if (qemu_in_coroutine()) {
132 return qemu_co_recv(fd
, buffer
, size
);
134 return qemu_co_send(fd
, buffer
, size
);
138 while (offset
< size
) {
142 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
144 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
148 err
= socket_error();
150 /* recoverable error */
151 if (err
== EINTR
|| (offset
> 0 && err
== EAGAIN
)) {
155 /* unrecoverable error */
170 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
172 /* Sockets are kept in blocking mode in the negotiation phase. After
173 * that, a non-readable socket simply means that another thread stole
174 * our request/reply. Synchronization is done with recv_coroutine, so
175 * that this is coroutine-safe.
177 return nbd_wr_sync(fd
, buffer
, size
, true);
180 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
184 /* For writes, we do expect the socket to be writable. */
185 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
186 } while (ret
== -EAGAIN
);
190 static void combine_addr(char *buf
, size_t len
, const char* address
,
193 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
194 if (strstr(address
, ":")) {
195 snprintf(buf
, len
, "[%s]:%u", address
, port
);
197 snprintf(buf
, len
, "%s:%u", address
, port
);
201 int tcp_socket_outgoing_opts(QemuOpts
*opts
)
203 Error
*local_err
= NULL
;
204 int fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
205 if (local_err
!= NULL
) {
206 qerror_report_err(local_err
);
207 error_free(local_err
);
213 int tcp_socket_incoming(const char *address
, uint16_t port
)
215 char address_and_port
[128];
216 combine_addr(address_and_port
, 128, address
, port
);
217 return tcp_socket_incoming_spec(address_and_port
);
220 int tcp_socket_incoming_spec(const char *address_and_port
)
222 Error
*local_err
= NULL
;
223 int fd
= inet_listen(address_and_port
, NULL
, 0, SOCK_STREAM
, 0, &local_err
);
225 if (local_err
!= NULL
) {
226 qerror_report_err(local_err
);
227 error_free(local_err
);
232 int unix_socket_incoming(const char *path
)
234 Error
*local_err
= NULL
;
235 int fd
= unix_listen(path
, NULL
, 0, &local_err
);
237 if (local_err
!= NULL
) {
238 qerror_report_err(local_err
);
239 error_free(local_err
);
244 int unix_socket_outgoing(const char *path
)
246 Error
*local_err
= NULL
;
247 int fd
= unix_connect(path
, &local_err
);
249 if (local_err
!= NULL
) {
250 qerror_report_err(local_err
);
251 error_free(local_err
);
256 /* Basic flow for negotiation
283 static int nbd_receive_options(NBDClient
*client
)
285 int csock
= client
->sock
;
287 uint32_t tmp
, length
;
292 [ 0 .. 3] reserved (0)
293 [ 4 .. 11] NBD_OPTS_MAGIC
294 [12 .. 15] NBD_OPT_EXPORT_NAME
296 [20 .. xx] export name (length bytes)
300 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
304 TRACE("Checking reserved");
306 LOG("Bad reserved received");
310 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
314 TRACE("Checking reserved");
315 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
316 LOG("Bad magic received");
320 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
324 TRACE("Checking option");
325 if (tmp
!= be32_to_cpu(NBD_OPT_EXPORT_NAME
)) {
326 LOG("Bad option received");
330 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
334 TRACE("Checking length");
335 length
= be32_to_cpu(length
);
337 LOG("Bad length received");
340 if (read_sync(csock
, name
, length
) != length
) {
346 client
->exp
= nbd_export_find(name
);
348 LOG("export not found");
352 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
353 nbd_export_get(client
->exp
);
355 TRACE("Option negotiation succeeded.");
361 static int nbd_send_negotiate(NBDClient
*client
)
363 int csock
= client
->sock
;
364 char buf
[8 + 8 + 8 + 128];
366 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
367 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
369 /* Negotiation header without options:
370 [ 0 .. 7] passwd ("NBDMAGIC")
371 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
373 [24 .. 25] server flags (0)
374 [24 .. 27] export flags
375 [28 .. 151] reserved (0)
377 Negotiation header with options, part 1:
378 [ 0 .. 7] passwd ("NBDMAGIC")
379 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
380 [16 .. 17] server flags (0)
382 part 2 (after options are sent):
384 [26 .. 27] export flags
385 [28 .. 151] reserved (0)
388 qemu_set_block(csock
);
391 TRACE("Beginning negotiation.");
392 memset(buf
, 0, sizeof(buf
));
393 memcpy(buf
, "NBDMAGIC", 8);
395 assert ((client
->exp
->nbdflags
& ~65535) == 0);
396 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
397 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
398 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
400 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
404 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
409 if (write_sync(csock
, buf
, 18) != 18) {
413 rc
= nbd_receive_options(client
);
415 LOG("option negotiation failed");
419 assert ((client
->exp
->nbdflags
& ~65535) == 0);
420 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
421 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
422 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
428 TRACE("Negotiation succeeded.");
431 qemu_set_nonblock(csock
);
435 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
436 off_t
*size
, size_t *blocksize
)
443 TRACE("Receiving negotiation.");
445 qemu_set_block(csock
);
448 if (read_sync(csock
, buf
, 8) != 8) {
454 if (strlen(buf
) == 0) {
455 LOG("server connection closed");
459 TRACE("Magic is %c%c%c%c%c%c%c%c",
460 qemu_isprint(buf
[0]) ? buf
[0] : '.',
461 qemu_isprint(buf
[1]) ? buf
[1] : '.',
462 qemu_isprint(buf
[2]) ? buf
[2] : '.',
463 qemu_isprint(buf
[3]) ? buf
[3] : '.',
464 qemu_isprint(buf
[4]) ? buf
[4] : '.',
465 qemu_isprint(buf
[5]) ? buf
[5] : '.',
466 qemu_isprint(buf
[6]) ? buf
[6] : '.',
467 qemu_isprint(buf
[7]) ? buf
[7] : '.');
469 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
470 LOG("Invalid magic received");
474 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
478 magic
= be64_to_cpu(magic
);
479 TRACE("Magic is 0x%" PRIx64
, magic
);
482 uint32_t reserved
= 0;
486 TRACE("Checking magic (opts_magic)");
487 if (magic
!= NBD_OPTS_MAGIC
) {
488 LOG("Bad magic received");
491 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
492 LOG("flags read failed");
495 *flags
= be16_to_cpu(tmp
) << 16;
496 /* reserved for future use */
497 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
499 LOG("write failed (reserved)");
502 /* write the export name */
503 magic
= cpu_to_be64(magic
);
504 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
505 LOG("write failed (magic)");
508 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
509 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
510 LOG("write failed (opt)");
513 namesize
= cpu_to_be32(strlen(name
));
514 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
516 LOG("write failed (namesize)");
519 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
520 LOG("write failed (name)");
524 TRACE("Checking magic (cli_magic)");
526 if (magic
!= NBD_CLIENT_MAGIC
) {
527 LOG("Bad magic received");
532 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
536 *size
= be64_to_cpu(s
);
538 TRACE("Size is %" PRIu64
, *size
);
541 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
542 LOG("read failed (flags)");
545 *flags
= be32_to_cpup(flags
);
547 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
548 LOG("read failed (tmp)");
551 *flags
|= be32_to_cpu(tmp
);
553 if (read_sync(csock
, &buf
, 124) != 124) {
554 LOG("read failed (buf)");
560 qemu_set_nonblock(csock
);
565 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
567 TRACE("Setting NBD socket");
569 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
571 LOG("Failed to set NBD socket");
575 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
577 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
579 LOG("Failed setting NBD block size");
583 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
585 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
587 LOG("Failed setting size (in blocks)");
591 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
592 if (errno
== ENOTTY
) {
593 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
594 TRACE("Setting readonly attribute");
596 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
598 LOG("Failed setting read-only attribute");
603 LOG("Failed setting flags");
608 TRACE("Negotiation ended");
613 int nbd_disconnect(int fd
)
615 ioctl(fd
, NBD_CLEAR_QUE
);
616 ioctl(fd
, NBD_DISCONNECT
);
617 ioctl(fd
, NBD_CLEAR_SOCK
);
621 int nbd_client(int fd
)
626 TRACE("Doing NBD loop");
628 ret
= ioctl(fd
, NBD_DO_IT
);
629 if (ret
< 0 && errno
== EPIPE
) {
630 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
631 * the socket via NBD_DISCONNECT. We do not want to return 1 in
638 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
640 TRACE("Clearing NBD queue");
641 ioctl(fd
, NBD_CLEAR_QUE
);
643 TRACE("Clearing NBD socket");
644 ioctl(fd
, NBD_CLEAR_SOCK
);
650 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
655 int nbd_disconnect(int fd
)
660 int nbd_client(int fd
)
666 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
668 uint8_t buf
[NBD_REQUEST_SIZE
];
671 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
672 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
673 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
674 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
675 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
677 TRACE("Sending request to client: "
678 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
679 request
->from
, request
->len
, request
->handle
, request
->type
);
681 ret
= write_sync(csock
, buf
, sizeof(buf
));
686 if (ret
!= sizeof(buf
)) {
687 LOG("writing to socket failed");
693 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
695 uint8_t buf
[NBD_REQUEST_SIZE
];
699 ret
= read_sync(csock
, buf
, sizeof(buf
));
704 if (ret
!= sizeof(buf
)) {
710 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
711 [ 4 .. 7] type (0 == READ, 1 == WRITE)
717 magic
= be32_to_cpup((uint32_t*)buf
);
718 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
719 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
720 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
721 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
723 TRACE("Got request: "
724 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
725 magic
, request
->type
, request
->from
, request
->len
);
727 if (magic
!= NBD_REQUEST_MAGIC
) {
728 LOG("invalid magic (got 0x%x)", magic
);
734 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
736 uint8_t buf
[NBD_REPLY_SIZE
];
740 ret
= read_sync(csock
, buf
, sizeof(buf
));
745 if (ret
!= sizeof(buf
)) {
751 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
752 [ 4 .. 7] error (0 == no error)
756 magic
= be32_to_cpup((uint32_t*)buf
);
757 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
758 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
761 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
762 magic
, reply
->error
, reply
->handle
);
764 if (magic
!= NBD_REPLY_MAGIC
) {
765 LOG("invalid magic (got 0x%x)", magic
);
771 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
773 uint8_t buf
[NBD_REPLY_SIZE
];
777 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
778 [ 4 .. 7] error (0 == no error)
781 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
782 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
783 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
785 TRACE("Sending response to client");
787 ret
= write_sync(csock
, buf
, sizeof(buf
));
792 if (ret
!= sizeof(buf
)) {
793 LOG("writing to socket failed");
799 #define MAX_NBD_REQUESTS 16
801 void nbd_client_get(NBDClient
*client
)
806 void nbd_client_put(NBDClient
*client
)
808 if (--client
->refcount
== 0) {
809 /* The last reference should be dropped by client->close,
810 * which is called by nbd_client_close.
812 assert(client
->closing
);
814 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
818 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
819 nbd_export_put(client
->exp
);
825 void nbd_client_close(NBDClient
*client
)
827 if (client
->closing
) {
831 client
->closing
= true;
833 /* Force requests to finish. They will drop their own references,
834 * then we'll close the socket and free the NBDClient.
836 shutdown(client
->sock
, 2);
838 /* Also tell the client, so that they release their reference. */
840 client
->close(client
);
844 static NBDRequest
*nbd_request_get(NBDClient
*client
)
848 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
849 client
->nb_requests
++;
851 req
= g_slice_new0(NBDRequest
);
852 nbd_client_get(client
);
853 req
->client
= client
;
857 static void nbd_request_put(NBDRequest
*req
)
859 NBDClient
*client
= req
->client
;
862 qemu_vfree(req
->data
);
864 g_slice_free(NBDRequest
, req
);
866 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
869 nbd_client_put(client
);
872 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
873 off_t size
, uint32_t nbdflags
,
874 void (*close
)(NBDExport
*))
876 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
878 QTAILQ_INIT(&exp
->clients
);
880 exp
->dev_offset
= dev_offset
;
881 exp
->nbdflags
= nbdflags
;
882 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
887 NBDExport
*nbd_export_find(const char *name
)
890 QTAILQ_FOREACH(exp
, &exports
, next
) {
891 if (strcmp(name
, exp
->name
) == 0) {
899 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
901 if (exp
->name
== name
) {
906 if (exp
->name
!= NULL
) {
909 QTAILQ_REMOVE(&exports
, exp
, next
);
914 exp
->name
= g_strdup(name
);
915 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
920 void nbd_export_close(NBDExport
*exp
)
922 NBDClient
*client
, *next
;
925 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
926 nbd_client_close(client
);
928 nbd_export_set_name(exp
, NULL
);
932 void nbd_export_get(NBDExport
*exp
)
934 assert(exp
->refcount
> 0);
938 void nbd_export_put(NBDExport
*exp
)
940 assert(exp
->refcount
> 0);
941 if (exp
->refcount
== 1) {
942 nbd_export_close(exp
);
945 if (--exp
->refcount
== 0) {
946 assert(exp
->name
== NULL
);
956 BlockDriverState
*nbd_export_get_blockdev(NBDExport
*exp
)
961 void nbd_export_close_all(void)
963 NBDExport
*exp
, *next
;
965 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
966 nbd_export_close(exp
);
970 static int nbd_can_read(void *opaque
);
971 static void nbd_read(void *opaque
);
972 static void nbd_restart_write(void *opaque
);
974 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
977 NBDClient
*client
= req
->client
;
978 int csock
= client
->sock
;
981 qemu_co_mutex_lock(&client
->send_lock
);
982 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
983 nbd_restart_write
, client
);
984 client
->send_coroutine
= qemu_coroutine_self();
987 rc
= nbd_send_reply(csock
, reply
);
989 socket_set_cork(csock
, 1);
990 rc
= nbd_send_reply(csock
, reply
);
992 ret
= qemu_co_send(csock
, req
->data
, len
);
997 socket_set_cork(csock
, 0);
1000 client
->send_coroutine
= NULL
;
1001 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1002 qemu_co_mutex_unlock(&client
->send_lock
);
1006 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1008 NBDClient
*client
= req
->client
;
1009 int csock
= client
->sock
;
1013 client
->recv_coroutine
= qemu_coroutine_self();
1014 rc
= nbd_receive_request(csock
, request
);
1016 if (rc
!= -EAGAIN
) {
1022 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1023 LOG("len (%u) is larger than max len (%u)",
1024 request
->len
, NBD_MAX_BUFFER_SIZE
);
1029 if ((request
->from
+ request
->len
) < request
->from
) {
1030 LOG("integer overflow detected! "
1031 "you're probably being attacked");
1036 TRACE("Decoding type");
1038 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1039 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1040 req
->data
= qemu_blockalign(client
->exp
->bs
, request
->len
);
1042 if (command
== NBD_CMD_WRITE
) {
1043 TRACE("Reading %u byte(s)", request
->len
);
1045 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1046 LOG("reading from socket failed");
1054 client
->recv_coroutine
= NULL
;
1058 static void nbd_trip(void *opaque
)
1060 NBDClient
*client
= opaque
;
1061 NBDExport
*exp
= client
->exp
;
1063 struct nbd_request request
;
1064 struct nbd_reply reply
;
1067 TRACE("Reading request.");
1068 if (client
->closing
) {
1072 req
= nbd_request_get(client
);
1073 ret
= nbd_co_receive_request(req
, &request
);
1074 if (ret
== -EAGAIN
) {
1081 reply
.handle
= request
.handle
;
1089 if ((request
.from
+ request
.len
) > exp
->size
) {
1090 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1091 ", Offset: %" PRIu64
"\n",
1092 request
.from
, request
.len
,
1093 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1094 LOG("requested operation past EOF--bad client?");
1095 goto invalid_request
;
1098 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
1100 TRACE("Request type is READ");
1102 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1103 ret
= bdrv_co_flush(exp
->bs
);
1105 LOG("flush failed");
1111 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1112 req
->data
, request
.len
/ 512);
1114 LOG("reading from file failed");
1119 TRACE("Read %u byte(s)", request
.len
);
1120 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1124 TRACE("Request type is WRITE");
1126 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1127 TRACE("Server is read-only, return error");
1128 reply
.error
= EROFS
;
1132 TRACE("Writing to device");
1134 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1135 req
->data
, request
.len
/ 512);
1137 LOG("writing to file failed");
1142 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1143 ret
= bdrv_co_flush(exp
->bs
);
1145 LOG("flush failed");
1151 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1156 TRACE("Request type is DISCONNECT");
1160 TRACE("Request type is FLUSH");
1162 ret
= bdrv_co_flush(exp
->bs
);
1164 LOG("flush failed");
1167 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1172 TRACE("Request type is TRIM");
1173 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1176 LOG("discard failed");
1179 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1184 LOG("invalid request type (%u) received", request
.type
);
1186 reply
.error
= -EINVAL
;
1188 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1194 TRACE("Request/Reply complete");
1197 nbd_request_put(req
);
1201 nbd_request_put(req
);
1202 nbd_client_close(client
);
1205 static int nbd_can_read(void *opaque
)
1207 NBDClient
*client
= opaque
;
1209 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1212 static void nbd_read(void *opaque
)
1214 NBDClient
*client
= opaque
;
1216 if (client
->recv_coroutine
) {
1217 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1219 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1223 static void nbd_restart_write(void *opaque
)
1225 NBDClient
*client
= opaque
;
1227 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1230 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1231 void (*close
)(NBDClient
*))
1234 client
= g_malloc0(sizeof(NBDClient
));
1235 client
->refcount
= 1;
1237 client
->sock
= csock
;
1238 if (nbd_send_negotiate(client
) < 0) {
1242 client
->close
= close
;
1243 qemu_co_mutex_init(&client
->send_lock
);
1244 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1247 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1248 nbd_export_get(exp
);