]>
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/>.
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 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_outgoing_opts(QemuOpts
*opts
)
223 Error
*local_err
= NULL
;
224 int fd
= inet_connect_opts(opts
, &local_err
, NULL
, NULL
);
225 if (local_err
!= NULL
) {
226 qerror_report_err(local_err
);
227 error_free(local_err
);
233 int tcp_socket_incoming(const char *address
, uint16_t port
)
235 char address_and_port
[128];
236 combine_addr(address_and_port
, 128, address
, port
);
237 return tcp_socket_incoming_spec(address_and_port
);
240 int tcp_socket_incoming_spec(const char *address_and_port
)
242 Error
*local_err
= NULL
;
243 int fd
= inet_listen(address_and_port
, NULL
, 0, SOCK_STREAM
, 0, &local_err
);
245 if (local_err
!= NULL
) {
246 qerror_report_err(local_err
);
247 error_free(local_err
);
252 int unix_socket_incoming(const char *path
)
254 Error
*local_err
= NULL
;
255 int fd
= unix_listen(path
, NULL
, 0, &local_err
);
257 if (local_err
!= NULL
) {
258 qerror_report_err(local_err
);
259 error_free(local_err
);
264 int unix_socket_outgoing(const char *path
)
266 Error
*local_err
= NULL
;
267 int fd
= unix_connect(path
, &local_err
);
269 if (local_err
!= NULL
) {
270 qerror_report_err(local_err
);
271 error_free(local_err
);
276 /* Basic flow for negotiation
303 static int nbd_receive_options(NBDClient
*client
)
305 int csock
= client
->sock
;
307 uint32_t tmp
, length
;
312 [ 0 .. 3] reserved (0)
313 [ 4 .. 11] NBD_OPTS_MAGIC
314 [12 .. 15] NBD_OPT_EXPORT_NAME
316 [20 .. xx] export name (length bytes)
320 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
324 TRACE("Checking reserved");
326 LOG("Bad reserved received");
330 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
334 TRACE("Checking reserved");
335 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
336 LOG("Bad magic received");
340 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
344 TRACE("Checking option");
345 if (tmp
!= be32_to_cpu(NBD_OPT_EXPORT_NAME
)) {
346 LOG("Bad option received");
350 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
354 TRACE("Checking length");
355 length
= be32_to_cpu(length
);
357 LOG("Bad length received");
360 if (read_sync(csock
, name
, length
) != length
) {
366 client
->exp
= nbd_export_find(name
);
368 LOG("export not found");
372 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
373 nbd_export_get(client
->exp
);
375 TRACE("Option negotiation succeeded.");
381 static int nbd_send_negotiate(NBDClient
*client
)
383 int csock
= client
->sock
;
384 char buf
[8 + 8 + 8 + 128];
386 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
387 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
389 /* Negotiation header without options:
390 [ 0 .. 7] passwd ("NBDMAGIC")
391 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
393 [24 .. 25] server flags (0)
394 [24 .. 27] export flags
395 [28 .. 151] reserved (0)
397 Negotiation header with options, part 1:
398 [ 0 .. 7] passwd ("NBDMAGIC")
399 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
400 [16 .. 17] server flags (0)
402 part 2 (after options are sent):
404 [26 .. 27] export flags
405 [28 .. 151] reserved (0)
408 socket_set_block(csock
);
411 TRACE("Beginning negotiation.");
412 memset(buf
, 0, sizeof(buf
));
413 memcpy(buf
, "NBDMAGIC", 8);
415 assert ((client
->exp
->nbdflags
& ~65535) == 0);
416 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
417 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
418 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
420 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
424 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
429 if (write_sync(csock
, buf
, 18) != 18) {
433 rc
= nbd_receive_options(client
);
435 LOG("option negotiation failed");
439 assert ((client
->exp
->nbdflags
& ~65535) == 0);
440 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
441 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
442 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
448 TRACE("Negotiation succeeded.");
451 socket_set_nonblock(csock
);
455 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
456 off_t
*size
, size_t *blocksize
)
463 TRACE("Receiving negotiation.");
465 socket_set_block(csock
);
468 if (read_sync(csock
, buf
, 8) != 8) {
474 if (strlen(buf
) == 0) {
475 LOG("server connection closed");
479 TRACE("Magic is %c%c%c%c%c%c%c%c",
480 qemu_isprint(buf
[0]) ? buf
[0] : '.',
481 qemu_isprint(buf
[1]) ? buf
[1] : '.',
482 qemu_isprint(buf
[2]) ? buf
[2] : '.',
483 qemu_isprint(buf
[3]) ? buf
[3] : '.',
484 qemu_isprint(buf
[4]) ? buf
[4] : '.',
485 qemu_isprint(buf
[5]) ? buf
[5] : '.',
486 qemu_isprint(buf
[6]) ? buf
[6] : '.',
487 qemu_isprint(buf
[7]) ? buf
[7] : '.');
489 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
490 LOG("Invalid magic received");
494 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
498 magic
= be64_to_cpu(magic
);
499 TRACE("Magic is 0x%" PRIx64
, magic
);
502 uint32_t reserved
= 0;
506 TRACE("Checking magic (opts_magic)");
507 if (magic
!= NBD_OPTS_MAGIC
) {
508 LOG("Bad magic received");
511 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
512 LOG("flags read failed");
515 *flags
= be16_to_cpu(tmp
) << 16;
516 /* reserved for future use */
517 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
519 LOG("write failed (reserved)");
522 /* write the export name */
523 magic
= cpu_to_be64(magic
);
524 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
525 LOG("write failed (magic)");
528 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
529 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
530 LOG("write failed (opt)");
533 namesize
= cpu_to_be32(strlen(name
));
534 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
536 LOG("write failed (namesize)");
539 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
540 LOG("write failed (name)");
544 TRACE("Checking magic (cli_magic)");
546 if (magic
!= NBD_CLIENT_MAGIC
) {
547 LOG("Bad magic received");
552 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
556 *size
= be64_to_cpu(s
);
558 TRACE("Size is %" PRIu64
, *size
);
561 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
562 LOG("read failed (flags)");
565 *flags
= be32_to_cpup(flags
);
567 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
568 LOG("read failed (tmp)");
571 *flags
|= be32_to_cpu(tmp
);
573 if (read_sync(csock
, &buf
, 124) != 124) {
574 LOG("read failed (buf)");
580 socket_set_nonblock(csock
);
585 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
587 TRACE("Setting NBD socket");
589 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
591 LOG("Failed to set NBD socket");
595 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
597 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
599 LOG("Failed setting NBD block size");
603 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
605 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
607 LOG("Failed setting size (in blocks)");
611 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
612 if (errno
== ENOTTY
) {
613 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
614 TRACE("Setting readonly attribute");
616 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
618 LOG("Failed setting read-only attribute");
623 LOG("Failed setting flags");
628 TRACE("Negotiation ended");
633 int nbd_disconnect(int fd
)
635 ioctl(fd
, NBD_CLEAR_QUE
);
636 ioctl(fd
, NBD_DISCONNECT
);
637 ioctl(fd
, NBD_CLEAR_SOCK
);
641 int nbd_client(int fd
)
646 TRACE("Doing NBD loop");
648 ret
= ioctl(fd
, NBD_DO_IT
);
649 if (ret
< 0 && errno
== EPIPE
) {
650 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
651 * the socket via NBD_DISCONNECT. We do not want to return 1 in
658 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
660 TRACE("Clearing NBD queue");
661 ioctl(fd
, NBD_CLEAR_QUE
);
663 TRACE("Clearing NBD socket");
664 ioctl(fd
, NBD_CLEAR_SOCK
);
670 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
675 int nbd_disconnect(int fd
)
680 int nbd_client(int fd
)
686 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
688 uint8_t buf
[NBD_REQUEST_SIZE
];
691 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
692 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
693 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
694 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
695 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
697 TRACE("Sending request to client: "
698 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
699 request
->from
, request
->len
, request
->handle
, request
->type
);
701 ret
= write_sync(csock
, buf
, sizeof(buf
));
706 if (ret
!= sizeof(buf
)) {
707 LOG("writing to socket failed");
713 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
715 uint8_t buf
[NBD_REQUEST_SIZE
];
719 ret
= read_sync(csock
, buf
, sizeof(buf
));
724 if (ret
!= sizeof(buf
)) {
730 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
731 [ 4 .. 7] type (0 == READ, 1 == WRITE)
737 magic
= be32_to_cpup((uint32_t*)buf
);
738 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
739 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
740 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
741 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
743 TRACE("Got request: "
744 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
745 magic
, request
->type
, request
->from
, request
->len
);
747 if (magic
!= NBD_REQUEST_MAGIC
) {
748 LOG("invalid magic (got 0x%x)", magic
);
754 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
756 uint8_t buf
[NBD_REPLY_SIZE
];
760 ret
= read_sync(csock
, buf
, sizeof(buf
));
765 if (ret
!= sizeof(buf
)) {
771 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
772 [ 4 .. 7] error (0 == no error)
776 magic
= be32_to_cpup((uint32_t*)buf
);
777 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
778 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
781 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
782 magic
, reply
->error
, reply
->handle
);
784 if (magic
!= NBD_REPLY_MAGIC
) {
785 LOG("invalid magic (got 0x%x)", magic
);
791 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
793 uint8_t buf
[NBD_REPLY_SIZE
];
797 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
798 [ 4 .. 7] error (0 == no error)
801 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
802 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
803 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
805 TRACE("Sending response to client");
807 ret
= write_sync(csock
, buf
, sizeof(buf
));
812 if (ret
!= sizeof(buf
)) {
813 LOG("writing to socket failed");
819 #define MAX_NBD_REQUESTS 16
821 void nbd_client_get(NBDClient
*client
)
826 void nbd_client_put(NBDClient
*client
)
828 if (--client
->refcount
== 0) {
829 /* The last reference should be dropped by client->close,
830 * which is called by nbd_client_close.
832 assert(client
->closing
);
834 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
838 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
839 nbd_export_put(client
->exp
);
845 void nbd_client_close(NBDClient
*client
)
847 if (client
->closing
) {
851 client
->closing
= true;
853 /* Force requests to finish. They will drop their own references,
854 * then we'll close the socket and free the NBDClient.
856 shutdown(client
->sock
, 2);
858 /* Also tell the client, so that they release their reference. */
860 client
->close(client
);
864 static NBDRequest
*nbd_request_get(NBDClient
*client
)
867 NBDExport
*exp
= client
->exp
;
869 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
870 client
->nb_requests
++;
872 if (QSIMPLEQ_EMPTY(&exp
->requests
)) {
873 req
= g_malloc0(sizeof(NBDRequest
));
874 req
->data
= qemu_blockalign(exp
->bs
, NBD_BUFFER_SIZE
);
876 req
= QSIMPLEQ_FIRST(&exp
->requests
);
877 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
879 nbd_client_get(client
);
880 req
->client
= client
;
884 static void nbd_request_put(NBDRequest
*req
)
886 NBDClient
*client
= req
->client
;
887 QSIMPLEQ_INSERT_HEAD(&client
->exp
->requests
, req
, entry
);
888 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
891 nbd_client_put(client
);
894 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
895 off_t size
, uint32_t nbdflags
,
896 void (*close
)(NBDExport
*))
898 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
899 QSIMPLEQ_INIT(&exp
->requests
);
901 QTAILQ_INIT(&exp
->clients
);
903 exp
->dev_offset
= dev_offset
;
904 exp
->nbdflags
= nbdflags
;
905 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
910 NBDExport
*nbd_export_find(const char *name
)
913 QTAILQ_FOREACH(exp
, &exports
, next
) {
914 if (strcmp(name
, exp
->name
) == 0) {
922 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
924 if (exp
->name
== name
) {
929 if (exp
->name
!= NULL
) {
932 QTAILQ_REMOVE(&exports
, exp
, next
);
937 exp
->name
= g_strdup(name
);
938 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
943 void nbd_export_close(NBDExport
*exp
)
945 NBDClient
*client
, *next
;
948 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
949 nbd_client_close(client
);
951 nbd_export_set_name(exp
, NULL
);
955 void nbd_export_get(NBDExport
*exp
)
957 assert(exp
->refcount
> 0);
961 void nbd_export_put(NBDExport
*exp
)
963 assert(exp
->refcount
> 0);
964 if (exp
->refcount
== 1) {
965 nbd_export_close(exp
);
968 if (--exp
->refcount
== 0) {
969 assert(exp
->name
== NULL
);
975 while (!QSIMPLEQ_EMPTY(&exp
->requests
)) {
976 NBDRequest
*first
= QSIMPLEQ_FIRST(&exp
->requests
);
977 QSIMPLEQ_REMOVE_HEAD(&exp
->requests
, entry
);
978 qemu_vfree(first
->data
);
986 BlockDriverState
*nbd_export_get_blockdev(NBDExport
*exp
)
991 void nbd_export_close_all(void)
993 NBDExport
*exp
, *next
;
995 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
996 nbd_export_close(exp
);
1000 static int nbd_can_read(void *opaque
);
1001 static void nbd_read(void *opaque
);
1002 static void nbd_restart_write(void *opaque
);
1004 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1007 NBDClient
*client
= req
->client
;
1008 int csock
= client
->sock
;
1011 qemu_co_mutex_lock(&client
->send_lock
);
1012 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
1013 nbd_restart_write
, client
);
1014 client
->send_coroutine
= qemu_coroutine_self();
1017 rc
= nbd_send_reply(csock
, reply
);
1019 socket_set_cork(csock
, 1);
1020 rc
= nbd_send_reply(csock
, reply
);
1022 ret
= qemu_co_send(csock
, req
->data
, len
);
1027 socket_set_cork(csock
, 0);
1030 client
->send_coroutine
= NULL
;
1031 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1032 qemu_co_mutex_unlock(&client
->send_lock
);
1036 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1038 NBDClient
*client
= req
->client
;
1039 int csock
= client
->sock
;
1042 client
->recv_coroutine
= qemu_coroutine_self();
1043 rc
= nbd_receive_request(csock
, request
);
1045 if (rc
!= -EAGAIN
) {
1051 if (request
->len
> NBD_BUFFER_SIZE
) {
1052 LOG("len (%u) is larger than max len (%u)",
1053 request
->len
, NBD_BUFFER_SIZE
);
1058 if ((request
->from
+ request
->len
) < request
->from
) {
1059 LOG("integer overflow detected! "
1060 "you're probably being attacked");
1065 TRACE("Decoding type");
1067 if ((request
->type
& NBD_CMD_MASK_COMMAND
) == NBD_CMD_WRITE
) {
1068 TRACE("Reading %u byte(s)", request
->len
);
1070 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1071 LOG("reading from socket failed");
1079 client
->recv_coroutine
= NULL
;
1083 static void nbd_trip(void *opaque
)
1085 NBDClient
*client
= opaque
;
1086 NBDExport
*exp
= client
->exp
;
1088 struct nbd_request request
;
1089 struct nbd_reply reply
;
1092 TRACE("Reading request.");
1093 if (client
->closing
) {
1097 req
= nbd_request_get(client
);
1098 ret
= nbd_co_receive_request(req
, &request
);
1099 if (ret
== -EAGAIN
) {
1106 reply
.handle
= request
.handle
;
1114 if ((request
.from
+ request
.len
) > exp
->size
) {
1115 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1116 ", Offset: %" PRIu64
"\n",
1117 request
.from
, request
.len
,
1118 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1119 LOG("requested operation past EOF--bad client?");
1120 goto invalid_request
;
1123 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
1125 TRACE("Request type is READ");
1127 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1128 ret
= bdrv_co_flush(exp
->bs
);
1130 LOG("flush failed");
1136 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1137 req
->data
, request
.len
/ 512);
1139 LOG("reading from file failed");
1144 TRACE("Read %u byte(s)", request
.len
);
1145 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1149 TRACE("Request type is WRITE");
1151 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1152 TRACE("Server is read-only, return error");
1153 reply
.error
= EROFS
;
1157 TRACE("Writing to device");
1159 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1160 req
->data
, request
.len
/ 512);
1162 LOG("writing to file failed");
1167 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1168 ret
= bdrv_co_flush(exp
->bs
);
1170 LOG("flush failed");
1176 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1181 TRACE("Request type is DISCONNECT");
1185 TRACE("Request type is FLUSH");
1187 ret
= bdrv_co_flush(exp
->bs
);
1189 LOG("flush failed");
1192 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1197 TRACE("Request type is TRIM");
1198 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1201 LOG("discard failed");
1204 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1209 LOG("invalid request type (%u) received", request
.type
);
1211 reply
.error
= -EINVAL
;
1213 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1219 TRACE("Request/Reply complete");
1222 nbd_request_put(req
);
1226 nbd_request_put(req
);
1227 nbd_client_close(client
);
1230 static int nbd_can_read(void *opaque
)
1232 NBDClient
*client
= opaque
;
1234 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1237 static void nbd_read(void *opaque
)
1239 NBDClient
*client
= opaque
;
1241 if (client
->recv_coroutine
) {
1242 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1244 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1248 static void nbd_restart_write(void *opaque
)
1250 NBDClient
*client
= opaque
;
1252 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1255 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1256 void (*close
)(NBDClient
*))
1259 client
= g_malloc0(sizeof(NBDClient
));
1260 client
->refcount
= 1;
1262 client
->sock
= csock
;
1263 if (nbd_send_negotiate(client
) < 0) {
1267 client
->close
= close
;
1268 qemu_co_mutex_init(&client
->send_lock
);
1269 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1272 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1273 nbd_export_get(exp
);