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"
41 #include "qemu/main-loop.h"
46 #define TRACE(msg, ...) do { \
47 LOG(msg, ## __VA_ARGS__); \
50 #define TRACE(msg, ...) \
54 #define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
59 /* This is all part of the "official" NBD API */
61 #define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
62 #define NBD_REPLY_SIZE (4 + 4 + 8)
63 #define NBD_REQUEST_MAGIC 0x25609513
64 #define NBD_REPLY_MAGIC 0x67446698
65 #define NBD_OPTS_MAGIC 0x49484156454F5054LL
66 #define NBD_CLIENT_MAGIC 0x0000420281861253LL
68 #define NBD_SET_SOCK _IO(0xab, 0)
69 #define NBD_SET_BLKSIZE _IO(0xab, 1)
70 #define NBD_SET_SIZE _IO(0xab, 2)
71 #define NBD_DO_IT _IO(0xab, 3)
72 #define NBD_CLEAR_SOCK _IO(0xab, 4)
73 #define NBD_CLEAR_QUE _IO(0xab, 5)
74 #define NBD_PRINT_DEBUG _IO(0xab, 6)
75 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
76 #define NBD_DISCONNECT _IO(0xab, 8)
77 #define NBD_SET_TIMEOUT _IO(0xab, 9)
78 #define NBD_SET_FLAGS _IO(0xab, 10)
80 #define NBD_OPT_EXPORT_NAME (1 << 0)
82 /* Definitions for opaque data types */
84 typedef struct NBDRequest NBDRequest
;
87 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
94 void (*close
)(NBDExport
*exp
);
101 QTAILQ_HEAD(, NBDClient
) clients
;
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_opts(QemuOpts
*opts
)
204 Error
*local_err
= NULL
;
205 int fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
206 if (local_err
!= NULL
) {
207 qerror_report_err(local_err
);
208 error_free(local_err
);
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
)
223 Error
*local_err
= NULL
;
224 int fd
= inet_listen(address_and_port
, NULL
, 0, SOCK_STREAM
, 0, &local_err
);
226 if (local_err
!= NULL
) {
227 qerror_report_err(local_err
);
228 error_free(local_err
);
233 int unix_socket_incoming(const char *path
)
235 Error
*local_err
= NULL
;
236 int fd
= unix_listen(path
, NULL
, 0, &local_err
);
238 if (local_err
!= NULL
) {
239 qerror_report_err(local_err
);
240 error_free(local_err
);
245 int unix_socket_outgoing(const char *path
)
247 Error
*local_err
= NULL
;
248 int fd
= unix_connect(path
, &local_err
);
250 if (local_err
!= NULL
) {
251 qerror_report_err(local_err
);
252 error_free(local_err
);
257 /* Basic flow for negotiation
284 static int nbd_receive_options(NBDClient
*client
)
286 int csock
= client
->sock
;
288 uint32_t tmp
, length
;
293 [ 0 .. 3] reserved (0)
294 [ 4 .. 11] NBD_OPTS_MAGIC
295 [12 .. 15] NBD_OPT_EXPORT_NAME
297 [20 .. xx] export name (length bytes)
301 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
305 TRACE("Checking reserved");
307 LOG("Bad reserved received");
311 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
315 TRACE("Checking reserved");
316 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
317 LOG("Bad magic received");
321 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
325 TRACE("Checking option");
326 if (tmp
!= be32_to_cpu(NBD_OPT_EXPORT_NAME
)) {
327 LOG("Bad option received");
331 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
335 TRACE("Checking length");
336 length
= be32_to_cpu(length
);
338 LOG("Bad length received");
341 if (read_sync(csock
, name
, length
) != length
) {
347 client
->exp
= nbd_export_find(name
);
349 LOG("export not found");
353 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
354 nbd_export_get(client
->exp
);
356 TRACE("Option negotiation succeeded.");
362 static int nbd_send_negotiate(NBDClient
*client
)
364 int csock
= client
->sock
;
365 char buf
[8 + 8 + 8 + 128];
367 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
368 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
370 /* Negotiation header without options:
371 [ 0 .. 7] passwd ("NBDMAGIC")
372 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
374 [24 .. 25] server flags (0)
375 [24 .. 27] export flags
376 [28 .. 151] reserved (0)
378 Negotiation header with options, part 1:
379 [ 0 .. 7] passwd ("NBDMAGIC")
380 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
381 [16 .. 17] server flags (0)
383 part 2 (after options are sent):
385 [26 .. 27] export flags
386 [28 .. 151] reserved (0)
389 qemu_set_block(csock
);
392 TRACE("Beginning negotiation.");
393 memset(buf
, 0, sizeof(buf
));
394 memcpy(buf
, "NBDMAGIC", 8);
396 assert ((client
->exp
->nbdflags
& ~65535) == 0);
397 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
398 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
399 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
401 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
405 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
410 if (write_sync(csock
, buf
, 18) != 18) {
414 rc
= nbd_receive_options(client
);
416 LOG("option negotiation failed");
420 assert ((client
->exp
->nbdflags
& ~65535) == 0);
421 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
422 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
423 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
429 TRACE("Negotiation succeeded.");
432 qemu_set_nonblock(csock
);
436 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
437 off_t
*size
, size_t *blocksize
)
444 TRACE("Receiving negotiation.");
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)");
564 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
566 TRACE("Setting NBD socket");
568 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
570 LOG("Failed to set NBD socket");
574 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
576 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
578 LOG("Failed setting NBD block size");
582 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
584 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
586 LOG("Failed setting size (in blocks)");
590 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
591 if (errno
== ENOTTY
) {
592 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
593 TRACE("Setting readonly attribute");
595 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
597 LOG("Failed setting read-only attribute");
602 LOG("Failed setting flags");
607 TRACE("Negotiation ended");
612 int nbd_disconnect(int fd
)
614 ioctl(fd
, NBD_CLEAR_QUE
);
615 ioctl(fd
, NBD_DISCONNECT
);
616 ioctl(fd
, NBD_CLEAR_SOCK
);
620 int nbd_client(int fd
)
625 TRACE("Doing NBD loop");
627 ret
= ioctl(fd
, NBD_DO_IT
);
628 if (ret
< 0 && errno
== EPIPE
) {
629 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
630 * the socket via NBD_DISCONNECT. We do not want to return 1 in
637 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
639 TRACE("Clearing NBD queue");
640 ioctl(fd
, NBD_CLEAR_QUE
);
642 TRACE("Clearing NBD socket");
643 ioctl(fd
, NBD_CLEAR_SOCK
);
649 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
654 int nbd_disconnect(int fd
)
659 int nbd_client(int fd
)
665 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
667 uint8_t buf
[NBD_REQUEST_SIZE
];
670 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
671 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
672 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
673 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
674 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
676 TRACE("Sending request to client: "
677 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
678 request
->from
, request
->len
, request
->handle
, request
->type
);
680 ret
= write_sync(csock
, buf
, sizeof(buf
));
685 if (ret
!= sizeof(buf
)) {
686 LOG("writing to socket failed");
692 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
694 uint8_t buf
[NBD_REQUEST_SIZE
];
698 ret
= read_sync(csock
, buf
, sizeof(buf
));
703 if (ret
!= sizeof(buf
)) {
709 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
710 [ 4 .. 7] type (0 == READ, 1 == WRITE)
716 magic
= be32_to_cpup((uint32_t*)buf
);
717 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
718 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
719 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
720 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
722 TRACE("Got request: "
723 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
724 magic
, request
->type
, request
->from
, request
->len
);
726 if (magic
!= NBD_REQUEST_MAGIC
) {
727 LOG("invalid magic (got 0x%x)", magic
);
733 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
735 uint8_t buf
[NBD_REPLY_SIZE
];
739 ret
= read_sync(csock
, buf
, sizeof(buf
));
744 if (ret
!= sizeof(buf
)) {
750 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
751 [ 4 .. 7] error (0 == no error)
755 magic
= be32_to_cpup((uint32_t*)buf
);
756 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
757 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
760 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
761 magic
, reply
->error
, reply
->handle
);
763 if (magic
!= NBD_REPLY_MAGIC
) {
764 LOG("invalid magic (got 0x%x)", magic
);
770 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
772 uint8_t buf
[NBD_REPLY_SIZE
];
776 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
777 [ 4 .. 7] error (0 == no error)
780 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
781 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
782 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
784 TRACE("Sending response to client");
786 ret
= write_sync(csock
, buf
, sizeof(buf
));
791 if (ret
!= sizeof(buf
)) {
792 LOG("writing to socket failed");
798 #define MAX_NBD_REQUESTS 16
800 void nbd_client_get(NBDClient
*client
)
805 void nbd_client_put(NBDClient
*client
)
807 if (--client
->refcount
== 0) {
808 /* The last reference should be dropped by client->close,
809 * which is called by nbd_client_close.
811 assert(client
->closing
);
813 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
817 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
818 nbd_export_put(client
->exp
);
824 void nbd_client_close(NBDClient
*client
)
826 if (client
->closing
) {
830 client
->closing
= true;
832 /* Force requests to finish. They will drop their own references,
833 * then we'll close the socket and free the NBDClient.
835 shutdown(client
->sock
, 2);
837 /* Also tell the client, so that they release their reference. */
839 client
->close(client
);
843 static NBDRequest
*nbd_request_get(NBDClient
*client
)
847 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
848 client
->nb_requests
++;
850 req
= g_slice_new0(NBDRequest
);
851 nbd_client_get(client
);
852 req
->client
= client
;
856 static void nbd_request_put(NBDRequest
*req
)
858 NBDClient
*client
= req
->client
;
861 qemu_vfree(req
->data
);
863 g_slice_free(NBDRequest
, req
);
865 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
868 nbd_client_put(client
);
871 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
872 off_t size
, uint32_t nbdflags
,
873 void (*close
)(NBDExport
*))
875 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
877 QTAILQ_INIT(&exp
->clients
);
879 exp
->dev_offset
= dev_offset
;
880 exp
->nbdflags
= nbdflags
;
881 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
);
936 void nbd_export_get(NBDExport
*exp
)
938 assert(exp
->refcount
> 0);
942 void nbd_export_put(NBDExport
*exp
)
944 assert(exp
->refcount
> 0);
945 if (exp
->refcount
== 1) {
946 nbd_export_close(exp
);
949 if (--exp
->refcount
== 0) {
950 assert(exp
->name
== NULL
);
960 BlockDriverState
*nbd_export_get_blockdev(NBDExport
*exp
)
965 void nbd_export_close_all(void)
967 NBDExport
*exp
, *next
;
969 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
970 nbd_export_close(exp
);
974 static int nbd_can_read(void *opaque
);
975 static void nbd_read(void *opaque
);
976 static void nbd_restart_write(void *opaque
);
978 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
981 NBDClient
*client
= req
->client
;
982 int csock
= client
->sock
;
985 qemu_co_mutex_lock(&client
->send_lock
);
986 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
987 nbd_restart_write
, client
);
988 client
->send_coroutine
= qemu_coroutine_self();
991 rc
= nbd_send_reply(csock
, reply
);
993 socket_set_cork(csock
, 1);
994 rc
= nbd_send_reply(csock
, reply
);
996 ret
= qemu_co_send(csock
, req
->data
, len
);
1001 socket_set_cork(csock
, 0);
1004 client
->send_coroutine
= NULL
;
1005 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1006 qemu_co_mutex_unlock(&client
->send_lock
);
1010 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1012 NBDClient
*client
= req
->client
;
1013 int csock
= client
->sock
;
1017 client
->recv_coroutine
= qemu_coroutine_self();
1018 rc
= nbd_receive_request(csock
, request
);
1020 if (rc
!= -EAGAIN
) {
1026 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1027 LOG("len (%u) is larger than max len (%u)",
1028 request
->len
, NBD_MAX_BUFFER_SIZE
);
1033 if ((request
->from
+ request
->len
) < request
->from
) {
1034 LOG("integer overflow detected! "
1035 "you're probably being attacked");
1040 TRACE("Decoding type");
1042 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1043 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1044 req
->data
= qemu_blockalign(client
->exp
->bs
, request
->len
);
1046 if (command
== NBD_CMD_WRITE
) {
1047 TRACE("Reading %u byte(s)", request
->len
);
1049 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1050 LOG("reading from socket failed");
1058 client
->recv_coroutine
= NULL
;
1062 static void nbd_trip(void *opaque
)
1064 NBDClient
*client
= opaque
;
1065 NBDExport
*exp
= client
->exp
;
1067 struct nbd_request request
;
1068 struct nbd_reply reply
;
1071 TRACE("Reading request.");
1072 if (client
->closing
) {
1076 req
= nbd_request_get(client
);
1077 ret
= nbd_co_receive_request(req
, &request
);
1078 if (ret
== -EAGAIN
) {
1085 reply
.handle
= request
.handle
;
1093 if ((request
.from
+ request
.len
) > exp
->size
) {
1094 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1095 ", Offset: %" PRIu64
"\n",
1096 request
.from
, request
.len
,
1097 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1098 LOG("requested operation past EOF--bad client?");
1099 goto invalid_request
;
1102 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
1104 TRACE("Request type is READ");
1106 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1107 ret
= bdrv_co_flush(exp
->bs
);
1109 LOG("flush failed");
1115 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1116 req
->data
, request
.len
/ 512);
1118 LOG("reading from file failed");
1123 TRACE("Read %u byte(s)", request
.len
);
1124 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1128 TRACE("Request type is WRITE");
1130 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1131 TRACE("Server is read-only, return error");
1132 reply
.error
= EROFS
;
1136 TRACE("Writing to device");
1138 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1139 req
->data
, request
.len
/ 512);
1141 LOG("writing to file failed");
1146 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1147 ret
= bdrv_co_flush(exp
->bs
);
1149 LOG("flush failed");
1155 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1160 TRACE("Request type is DISCONNECT");
1164 TRACE("Request type is FLUSH");
1166 ret
= bdrv_co_flush(exp
->bs
);
1168 LOG("flush failed");
1171 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1176 TRACE("Request type is TRIM");
1177 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1180 LOG("discard failed");
1183 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1188 LOG("invalid request type (%u) received", request
.type
);
1190 reply
.error
= -EINVAL
;
1192 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1198 TRACE("Request/Reply complete");
1201 nbd_request_put(req
);
1205 nbd_request_put(req
);
1206 nbd_client_close(client
);
1209 static int nbd_can_read(void *opaque
)
1211 NBDClient
*client
= opaque
;
1213 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1216 static void nbd_read(void *opaque
)
1218 NBDClient
*client
= opaque
;
1220 if (client
->recv_coroutine
) {
1221 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1223 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1227 static void nbd_restart_write(void *opaque
)
1229 NBDClient
*client
= opaque
;
1231 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1234 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1235 void (*close
)(NBDClient
*))
1238 client
= g_malloc0(sizeof(NBDClient
));
1239 client
->refcount
= 1;
1241 client
->sock
= csock
;
1242 if (nbd_send_negotiate(client
) < 0) {
1246 client
->close
= close
;
1247 qemu_co_mutex_init(&client
->send_lock
);
1248 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1251 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1252 nbd_export_get(exp
);