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 * The most up-to-date documentation is available at:
62 * https://github.com/yoe/nbd/blob/master/doc/proto.txt
65 #define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
66 #define NBD_REPLY_SIZE (4 + 4 + 8)
67 #define NBD_REQUEST_MAGIC 0x25609513
68 #define NBD_REPLY_MAGIC 0x67446698
69 #define NBD_OPTS_MAGIC 0x49484156454F5054LL
70 #define NBD_CLIENT_MAGIC 0x0000420281861253LL
71 #define NBD_REP_MAGIC 0x3e889045565a9LL
73 #define NBD_SET_SOCK _IO(0xab, 0)
74 #define NBD_SET_BLKSIZE _IO(0xab, 1)
75 #define NBD_SET_SIZE _IO(0xab, 2)
76 #define NBD_DO_IT _IO(0xab, 3)
77 #define NBD_CLEAR_SOCK _IO(0xab, 4)
78 #define NBD_CLEAR_QUE _IO(0xab, 5)
79 #define NBD_PRINT_DEBUG _IO(0xab, 6)
80 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
81 #define NBD_DISCONNECT _IO(0xab, 8)
82 #define NBD_SET_TIMEOUT _IO(0xab, 9)
83 #define NBD_SET_FLAGS _IO(0xab, 10)
85 #define NBD_OPT_EXPORT_NAME (1)
86 #define NBD_OPT_ABORT (2)
87 #define NBD_OPT_LIST (3)
89 /* Definitions for opaque data types */
91 typedef struct NBDRequest NBDRequest
;
94 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
101 void (*close
)(NBDExport
*exp
);
103 BlockDriverState
*bs
;
108 QTAILQ_HEAD(, NBDClient
) clients
;
109 QTAILQ_ENTRY(NBDExport
) next
;
112 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
116 void (*close
)(NBDClient
*client
);
121 Coroutine
*recv_coroutine
;
124 Coroutine
*send_coroutine
;
126 QTAILQ_ENTRY(NBDClient
) next
;
131 /* That's all folks */
133 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
138 if (qemu_in_coroutine()) {
140 return qemu_co_recv(fd
, buffer
, size
);
142 return qemu_co_send(fd
, buffer
, size
);
146 while (offset
< size
) {
150 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
152 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
156 err
= socket_error();
158 /* recoverable error */
159 if (err
== EINTR
|| (offset
> 0 && err
== EAGAIN
)) {
163 /* unrecoverable error */
178 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
180 /* Sockets are kept in blocking mode in the negotiation phase. After
181 * that, a non-readable socket simply means that another thread stole
182 * our request/reply. Synchronization is done with recv_coroutine, so
183 * that this is coroutine-safe.
185 return nbd_wr_sync(fd
, buffer
, size
, true);
188 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
192 /* For writes, we do expect the socket to be writable. */
193 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
194 } while (ret
== -EAGAIN
);
198 /* Basic flow for negotiation
225 static int nbd_send_rep(int csock
, uint32_t type
, uint32_t opt
)
230 magic
= cpu_to_be64(NBD_REP_MAGIC
);
231 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
232 LOG("write failed (rep magic)");
235 opt
= cpu_to_be32(opt
);
236 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
237 LOG("write failed (rep opt)");
240 type
= cpu_to_be32(type
);
241 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
242 LOG("write failed (rep type)");
245 len
= cpu_to_be32(0);
246 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
247 LOG("write failed (rep data length)");
253 static int nbd_send_rep_list(int csock
, NBDExport
*exp
)
255 uint64_t magic
, name_len
;
256 uint32_t opt
, type
, len
;
258 name_len
= strlen(exp
->name
);
259 magic
= cpu_to_be64(NBD_REP_MAGIC
);
260 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
261 LOG("write failed (magic)");
264 opt
= cpu_to_be32(NBD_OPT_LIST
);
265 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
266 LOG("write failed (opt)");
269 type
= cpu_to_be32(NBD_REP_SERVER
);
270 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
271 LOG("write failed (reply type)");
274 len
= cpu_to_be32(name_len
+ sizeof(len
));
275 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
276 LOG("write failed (length)");
279 len
= cpu_to_be32(name_len
);
280 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
281 LOG("write failed (length)");
284 if (write_sync(csock
, exp
->name
, name_len
) != name_len
) {
285 LOG("write failed (buffer)");
291 static int nbd_handle_list(NBDClient
*client
, uint32_t length
)
296 csock
= client
->sock
;
298 return nbd_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
301 /* For each export, send a NBD_REP_SERVER reply. */
302 QTAILQ_FOREACH(exp
, &exports
, next
) {
303 if (nbd_send_rep_list(csock
, exp
)) {
307 /* Finish with a NBD_REP_ACK. */
308 return nbd_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
311 static int nbd_handle_export_name(NBDClient
*client
, uint32_t length
)
313 int rc
= -EINVAL
, csock
= client
->sock
;
317 [20 .. xx] export name (length bytes)
319 TRACE("Checking length");
321 LOG("Bad length received");
324 if (read_sync(csock
, name
, length
) != length
) {
330 client
->exp
= nbd_export_find(name
);
332 LOG("export not found");
336 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
337 nbd_export_get(client
->exp
);
343 static int nbd_receive_options(NBDClient
*client
)
346 int csock
= client
->sock
;
347 uint32_t tmp
, length
;
351 [ 0 .. 3] client flags
352 [ 4 .. 11] NBD_OPTS_MAGIC
353 [12 .. 15] NBD option
358 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
362 TRACE("Checking client flags");
363 tmp
= be32_to_cpu(tmp
);
364 if (tmp
!= 0 && tmp
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
365 LOG("Bad client flags received");
369 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
373 TRACE("Checking opts magic");
374 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
375 LOG("Bad magic received");
379 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
384 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
388 length
= be32_to_cpu(length
);
390 TRACE("Checking option");
391 switch (be32_to_cpu(tmp
)) {
393 if (nbd_handle_list(client
, length
) < 0) {
401 case NBD_OPT_EXPORT_NAME
:
402 return nbd_handle_export_name(client
, length
);
405 tmp
= be32_to_cpu(tmp
);
406 LOG("Unsupported option 0x%x", tmp
);
407 nbd_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
413 static int nbd_send_negotiate(NBDClient
*client
)
415 int csock
= client
->sock
;
416 char buf
[8 + 8 + 8 + 128];
418 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
419 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
421 /* Negotiation header without options:
422 [ 0 .. 7] passwd ("NBDMAGIC")
423 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
425 [24 .. 25] server flags (0)
426 [26 .. 27] export flags
427 [28 .. 151] reserved (0)
429 Negotiation header with options, part 1:
430 [ 0 .. 7] passwd ("NBDMAGIC")
431 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
432 [16 .. 17] server flags (0)
434 part 2 (after options are sent):
436 [26 .. 27] export flags
437 [28 .. 151] reserved (0)
440 qemu_set_block(csock
);
443 TRACE("Beginning negotiation.");
444 memset(buf
, 0, sizeof(buf
));
445 memcpy(buf
, "NBDMAGIC", 8);
447 assert ((client
->exp
->nbdflags
& ~65535) == 0);
448 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
449 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
450 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
452 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
453 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
457 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
462 if (write_sync(csock
, buf
, 18) != 18) {
466 rc
= nbd_receive_options(client
);
468 LOG("option negotiation failed");
472 assert ((client
->exp
->nbdflags
& ~65535) == 0);
473 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
474 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
475 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
481 TRACE("Negotiation succeeded.");
484 qemu_set_nonblock(csock
);
488 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
489 off_t
*size
, size_t *blocksize
)
496 TRACE("Receiving negotiation.");
500 if (read_sync(csock
, buf
, 8) != 8) {
506 if (strlen(buf
) == 0) {
507 LOG("server connection closed");
511 TRACE("Magic is %c%c%c%c%c%c%c%c",
512 qemu_isprint(buf
[0]) ? buf
[0] : '.',
513 qemu_isprint(buf
[1]) ? buf
[1] : '.',
514 qemu_isprint(buf
[2]) ? buf
[2] : '.',
515 qemu_isprint(buf
[3]) ? buf
[3] : '.',
516 qemu_isprint(buf
[4]) ? buf
[4] : '.',
517 qemu_isprint(buf
[5]) ? buf
[5] : '.',
518 qemu_isprint(buf
[6]) ? buf
[6] : '.',
519 qemu_isprint(buf
[7]) ? buf
[7] : '.');
521 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
522 LOG("Invalid magic received");
526 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
530 magic
= be64_to_cpu(magic
);
531 TRACE("Magic is 0x%" PRIx64
, magic
);
534 uint32_t reserved
= 0;
538 TRACE("Checking magic (opts_magic)");
539 if (magic
!= NBD_OPTS_MAGIC
) {
540 LOG("Bad magic received");
543 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
544 LOG("flags read failed");
547 *flags
= be16_to_cpu(tmp
) << 16;
548 /* reserved for future use */
549 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
551 LOG("write failed (reserved)");
554 /* write the export name */
555 magic
= cpu_to_be64(magic
);
556 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
557 LOG("write failed (magic)");
560 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
561 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
562 LOG("write failed (opt)");
565 namesize
= cpu_to_be32(strlen(name
));
566 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
568 LOG("write failed (namesize)");
571 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
572 LOG("write failed (name)");
576 TRACE("Checking magic (cli_magic)");
578 if (magic
!= NBD_CLIENT_MAGIC
) {
579 LOG("Bad magic received");
584 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
588 *size
= be64_to_cpu(s
);
590 TRACE("Size is %" PRIu64
, *size
);
593 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
594 LOG("read failed (flags)");
597 *flags
= be32_to_cpup(flags
);
599 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
600 LOG("read failed (tmp)");
603 *flags
|= be32_to_cpu(tmp
);
605 if (read_sync(csock
, &buf
, 124) != 124) {
606 LOG("read failed (buf)");
616 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
618 TRACE("Setting NBD socket");
620 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
622 LOG("Failed to set NBD socket");
626 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
628 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
630 LOG("Failed setting NBD block size");
634 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
636 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
638 LOG("Failed setting size (in blocks)");
642 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
643 if (errno
== ENOTTY
) {
644 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
645 TRACE("Setting readonly attribute");
647 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
649 LOG("Failed setting read-only attribute");
654 LOG("Failed setting flags");
659 TRACE("Negotiation ended");
664 int nbd_disconnect(int fd
)
666 ioctl(fd
, NBD_CLEAR_QUE
);
667 ioctl(fd
, NBD_DISCONNECT
);
668 ioctl(fd
, NBD_CLEAR_SOCK
);
672 int nbd_client(int fd
)
677 TRACE("Doing NBD loop");
679 ret
= ioctl(fd
, NBD_DO_IT
);
680 if (ret
< 0 && errno
== EPIPE
) {
681 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
682 * the socket via NBD_DISCONNECT. We do not want to return 1 in
689 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
691 TRACE("Clearing NBD queue");
692 ioctl(fd
, NBD_CLEAR_QUE
);
694 TRACE("Clearing NBD socket");
695 ioctl(fd
, NBD_CLEAR_SOCK
);
701 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
706 int nbd_disconnect(int fd
)
711 int nbd_client(int fd
)
717 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
719 uint8_t buf
[NBD_REQUEST_SIZE
];
722 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
723 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
724 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
725 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
726 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
728 TRACE("Sending request to client: "
729 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
730 request
->from
, request
->len
, request
->handle
, request
->type
);
732 ret
= write_sync(csock
, buf
, sizeof(buf
));
737 if (ret
!= sizeof(buf
)) {
738 LOG("writing to socket failed");
744 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
746 uint8_t buf
[NBD_REQUEST_SIZE
];
750 ret
= read_sync(csock
, buf
, sizeof(buf
));
755 if (ret
!= sizeof(buf
)) {
761 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
762 [ 4 .. 7] type (0 == READ, 1 == WRITE)
768 magic
= be32_to_cpup((uint32_t*)buf
);
769 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
770 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
771 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
772 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
774 TRACE("Got request: "
775 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
776 magic
, request
->type
, request
->from
, request
->len
);
778 if (magic
!= NBD_REQUEST_MAGIC
) {
779 LOG("invalid magic (got 0x%x)", magic
);
785 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
787 uint8_t buf
[NBD_REPLY_SIZE
];
791 ret
= read_sync(csock
, buf
, sizeof(buf
));
796 if (ret
!= sizeof(buf
)) {
802 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
803 [ 4 .. 7] error (0 == no error)
807 magic
= be32_to_cpup((uint32_t*)buf
);
808 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
809 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
812 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
813 magic
, reply
->error
, reply
->handle
);
815 if (magic
!= NBD_REPLY_MAGIC
) {
816 LOG("invalid magic (got 0x%x)", magic
);
822 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
824 uint8_t buf
[NBD_REPLY_SIZE
];
828 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
829 [ 4 .. 7] error (0 == no error)
832 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
833 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
834 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
836 TRACE("Sending response to client");
838 ret
= write_sync(csock
, buf
, sizeof(buf
));
843 if (ret
!= sizeof(buf
)) {
844 LOG("writing to socket failed");
850 #define MAX_NBD_REQUESTS 16
852 void nbd_client_get(NBDClient
*client
)
857 void nbd_client_put(NBDClient
*client
)
859 if (--client
->refcount
== 0) {
860 /* The last reference should be dropped by client->close,
861 * which is called by nbd_client_close.
863 assert(client
->closing
);
865 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
869 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
870 nbd_export_put(client
->exp
);
876 void nbd_client_close(NBDClient
*client
)
878 if (client
->closing
) {
882 client
->closing
= true;
884 /* Force requests to finish. They will drop their own references,
885 * then we'll close the socket and free the NBDClient.
887 shutdown(client
->sock
, 2);
889 /* Also tell the client, so that they release their reference. */
891 client
->close(client
);
895 static NBDRequest
*nbd_request_get(NBDClient
*client
)
899 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
900 client
->nb_requests
++;
902 req
= g_slice_new0(NBDRequest
);
903 nbd_client_get(client
);
904 req
->client
= client
;
908 static void nbd_request_put(NBDRequest
*req
)
910 NBDClient
*client
= req
->client
;
913 qemu_vfree(req
->data
);
915 g_slice_free(NBDRequest
, req
);
917 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
920 nbd_client_put(client
);
923 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
924 off_t size
, uint32_t nbdflags
,
925 void (*close
)(NBDExport
*))
927 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
929 QTAILQ_INIT(&exp
->clients
);
931 exp
->dev_offset
= dev_offset
;
932 exp
->nbdflags
= nbdflags
;
933 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
939 NBDExport
*nbd_export_find(const char *name
)
942 QTAILQ_FOREACH(exp
, &exports
, next
) {
943 if (strcmp(name
, exp
->name
) == 0) {
951 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
953 if (exp
->name
== name
) {
958 if (exp
->name
!= NULL
) {
961 QTAILQ_REMOVE(&exports
, exp
, next
);
966 exp
->name
= g_strdup(name
);
967 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
972 void nbd_export_close(NBDExport
*exp
)
974 NBDClient
*client
, *next
;
977 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
978 nbd_client_close(client
);
980 nbd_export_set_name(exp
, NULL
);
988 void nbd_export_get(NBDExport
*exp
)
990 assert(exp
->refcount
> 0);
994 void nbd_export_put(NBDExport
*exp
)
996 assert(exp
->refcount
> 0);
997 if (exp
->refcount
== 1) {
998 nbd_export_close(exp
);
1001 if (--exp
->refcount
== 0) {
1002 assert(exp
->name
== NULL
);
1012 BlockDriverState
*nbd_export_get_blockdev(NBDExport
*exp
)
1017 void nbd_export_close_all(void)
1019 NBDExport
*exp
, *next
;
1021 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1022 nbd_export_close(exp
);
1026 static int nbd_can_read(void *opaque
);
1027 static void nbd_read(void *opaque
);
1028 static void nbd_restart_write(void *opaque
);
1030 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1033 NBDClient
*client
= req
->client
;
1034 int csock
= client
->sock
;
1037 qemu_co_mutex_lock(&client
->send_lock
);
1038 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
1039 nbd_restart_write
, client
);
1040 client
->send_coroutine
= qemu_coroutine_self();
1043 rc
= nbd_send_reply(csock
, reply
);
1045 socket_set_cork(csock
, 1);
1046 rc
= nbd_send_reply(csock
, reply
);
1048 ret
= qemu_co_send(csock
, req
->data
, len
);
1053 socket_set_cork(csock
, 0);
1056 client
->send_coroutine
= NULL
;
1057 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1058 qemu_co_mutex_unlock(&client
->send_lock
);
1062 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1064 NBDClient
*client
= req
->client
;
1065 int csock
= client
->sock
;
1069 client
->recv_coroutine
= qemu_coroutine_self();
1070 rc
= nbd_receive_request(csock
, request
);
1072 if (rc
!= -EAGAIN
) {
1078 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1079 LOG("len (%u) is larger than max len (%u)",
1080 request
->len
, NBD_MAX_BUFFER_SIZE
);
1085 if ((request
->from
+ request
->len
) < request
->from
) {
1086 LOG("integer overflow detected! "
1087 "you're probably being attacked");
1092 TRACE("Decoding type");
1094 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1095 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1096 req
->data
= qemu_blockalign(client
->exp
->bs
, request
->len
);
1098 if (command
== NBD_CMD_WRITE
) {
1099 TRACE("Reading %u byte(s)", request
->len
);
1101 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1102 LOG("reading from socket failed");
1110 client
->recv_coroutine
= NULL
;
1114 static void nbd_trip(void *opaque
)
1116 NBDClient
*client
= opaque
;
1117 NBDExport
*exp
= client
->exp
;
1119 struct nbd_request request
;
1120 struct nbd_reply reply
;
1124 TRACE("Reading request.");
1125 if (client
->closing
) {
1129 req
= nbd_request_get(client
);
1130 ret
= nbd_co_receive_request(req
, &request
);
1131 if (ret
== -EAGAIN
) {
1138 reply
.handle
= request
.handle
;
1145 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1146 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1147 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1148 ", Offset: %" PRIu64
"\n",
1149 request
.from
, request
.len
,
1150 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1151 LOG("requested operation past EOF--bad client?");
1152 goto invalid_request
;
1157 TRACE("Request type is READ");
1159 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1160 ret
= bdrv_co_flush(exp
->bs
);
1162 LOG("flush failed");
1168 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1169 req
->data
, request
.len
/ 512);
1171 LOG("reading from file failed");
1176 TRACE("Read %u byte(s)", request
.len
);
1177 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1181 TRACE("Request type is WRITE");
1183 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1184 TRACE("Server is read-only, return error");
1185 reply
.error
= EROFS
;
1189 TRACE("Writing to device");
1191 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1192 req
->data
, request
.len
/ 512);
1194 LOG("writing to file failed");
1199 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1200 ret
= bdrv_co_flush(exp
->bs
);
1202 LOG("flush failed");
1208 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1213 TRACE("Request type is DISCONNECT");
1217 TRACE("Request type is FLUSH");
1219 ret
= bdrv_co_flush(exp
->bs
);
1221 LOG("flush failed");
1224 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1229 TRACE("Request type is TRIM");
1230 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1233 LOG("discard failed");
1236 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1241 LOG("invalid request type (%u) received", request
.type
);
1243 reply
.error
= -EINVAL
;
1245 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1251 TRACE("Request/Reply complete");
1254 nbd_request_put(req
);
1258 nbd_request_put(req
);
1259 nbd_client_close(client
);
1262 static int nbd_can_read(void *opaque
)
1264 NBDClient
*client
= opaque
;
1266 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1269 static void nbd_read(void *opaque
)
1271 NBDClient
*client
= opaque
;
1273 if (client
->recv_coroutine
) {
1274 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1276 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1280 static void nbd_restart_write(void *opaque
)
1282 NBDClient
*client
= opaque
;
1284 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1287 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1288 void (*close
)(NBDClient
*))
1291 client
= g_malloc0(sizeof(NBDClient
));
1292 client
->refcount
= 1;
1294 client
->sock
= csock
;
1295 if (nbd_send_negotiate(client
)) {
1299 client
->close
= close
;
1300 qemu_co_mutex_init(&client
->send_lock
);
1301 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1304 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1305 nbd_export_get(exp
);