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"
21 #include "block/block_int.h"
22 #include "sysemu/block-backend.h"
24 #include "block/coroutine.h"
29 #include <sys/ioctl.h>
31 #if defined(__sun__) || defined(__HAIKU__)
32 #include <sys/ioccom.h>
41 #include "qemu/sockets.h"
42 #include "qemu/queue.h"
43 #include "qemu/main-loop.h"
48 #define TRACE(msg, ...) do { \
49 LOG(msg, ## __VA_ARGS__); \
52 #define TRACE(msg, ...) \
56 #define LOG(msg, ...) do { \
57 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
58 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
61 /* This is all part of the "official" NBD API.
63 * The most up-to-date documentation is available at:
64 * https://github.com/yoe/nbd/blob/master/doc/proto.txt
67 #define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
68 #define NBD_REPLY_SIZE (4 + 4 + 8)
69 #define NBD_REQUEST_MAGIC 0x25609513
70 #define NBD_REPLY_MAGIC 0x67446698
71 #define NBD_OPTS_MAGIC 0x49484156454F5054LL
72 #define NBD_CLIENT_MAGIC 0x0000420281861253LL
73 #define NBD_REP_MAGIC 0x3e889045565a9LL
75 #define NBD_SET_SOCK _IO(0xab, 0)
76 #define NBD_SET_BLKSIZE _IO(0xab, 1)
77 #define NBD_SET_SIZE _IO(0xab, 2)
78 #define NBD_DO_IT _IO(0xab, 3)
79 #define NBD_CLEAR_SOCK _IO(0xab, 4)
80 #define NBD_CLEAR_QUE _IO(0xab, 5)
81 #define NBD_PRINT_DEBUG _IO(0xab, 6)
82 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
83 #define NBD_DISCONNECT _IO(0xab, 8)
84 #define NBD_SET_TIMEOUT _IO(0xab, 9)
85 #define NBD_SET_FLAGS _IO(0xab, 10)
87 #define NBD_OPT_EXPORT_NAME (1)
88 #define NBD_OPT_ABORT (2)
89 #define NBD_OPT_LIST (3)
91 /* Definitions for opaque data types */
93 typedef struct NBDRequest NBDRequest
;
96 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
103 void (*close
)(NBDExport
*exp
);
105 BlockDriverState
*bs
;
110 QTAILQ_HEAD(, NBDClient
) clients
;
111 QTAILQ_ENTRY(NBDExport
) next
;
116 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
120 void (*close
)(NBDClient
*client
);
125 Coroutine
*recv_coroutine
;
128 Coroutine
*send_coroutine
;
132 QTAILQ_ENTRY(NBDClient
) next
;
137 /* That's all folks */
139 static void nbd_set_handlers(NBDClient
*client
);
140 static void nbd_unset_handlers(NBDClient
*client
);
141 static void nbd_update_can_read(NBDClient
*client
);
143 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
148 if (qemu_in_coroutine()) {
150 return qemu_co_recv(fd
, buffer
, size
);
152 return qemu_co_send(fd
, buffer
, size
);
156 while (offset
< size
) {
160 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
162 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
166 err
= socket_error();
168 /* recoverable error */
169 if (err
== EINTR
|| (offset
> 0 && (err
== EAGAIN
|| err
== EWOULDBLOCK
))) {
173 /* unrecoverable error */
188 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
190 /* Sockets are kept in blocking mode in the negotiation phase. After
191 * that, a non-readable socket simply means that another thread stole
192 * our request/reply. Synchronization is done with recv_coroutine, so
193 * that this is coroutine-safe.
195 return nbd_wr_sync(fd
, buffer
, size
, true);
198 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
202 /* For writes, we do expect the socket to be writable. */
203 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
204 } while (ret
== -EAGAIN
);
208 /* Basic flow for negotiation
235 static int nbd_send_rep(int csock
, uint32_t type
, uint32_t opt
)
240 magic
= cpu_to_be64(NBD_REP_MAGIC
);
241 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
242 LOG("write failed (rep magic)");
245 opt
= cpu_to_be32(opt
);
246 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
247 LOG("write failed (rep opt)");
250 type
= cpu_to_be32(type
);
251 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
252 LOG("write failed (rep type)");
255 len
= cpu_to_be32(0);
256 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
257 LOG("write failed (rep data length)");
263 static int nbd_send_rep_list(int csock
, NBDExport
*exp
)
265 uint64_t magic
, name_len
;
266 uint32_t opt
, type
, len
;
268 name_len
= strlen(exp
->name
);
269 magic
= cpu_to_be64(NBD_REP_MAGIC
);
270 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
271 LOG("write failed (magic)");
274 opt
= cpu_to_be32(NBD_OPT_LIST
);
275 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
276 LOG("write failed (opt)");
279 type
= cpu_to_be32(NBD_REP_SERVER
);
280 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
281 LOG("write failed (reply type)");
284 len
= cpu_to_be32(name_len
+ sizeof(len
));
285 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
286 LOG("write failed (length)");
289 len
= cpu_to_be32(name_len
);
290 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
291 LOG("write failed (length)");
294 if (write_sync(csock
, exp
->name
, name_len
) != name_len
) {
295 LOG("write failed (buffer)");
301 static int nbd_handle_list(NBDClient
*client
, uint32_t length
)
306 csock
= client
->sock
;
308 return nbd_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
311 /* For each export, send a NBD_REP_SERVER reply. */
312 QTAILQ_FOREACH(exp
, &exports
, next
) {
313 if (nbd_send_rep_list(csock
, exp
)) {
317 /* Finish with a NBD_REP_ACK. */
318 return nbd_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
321 static int nbd_handle_export_name(NBDClient
*client
, uint32_t length
)
323 int rc
= -EINVAL
, csock
= client
->sock
;
327 [20 .. xx] export name (length bytes)
329 TRACE("Checking length");
331 LOG("Bad length received");
334 if (read_sync(csock
, name
, length
) != length
) {
340 client
->exp
= nbd_export_find(name
);
342 LOG("export not found");
346 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
347 nbd_export_get(client
->exp
);
353 static int nbd_receive_options(NBDClient
*client
)
356 int csock
= client
->sock
;
357 uint32_t tmp
, length
;
361 [ 0 .. 3] client flags
362 [ 4 .. 11] NBD_OPTS_MAGIC
363 [12 .. 15] NBD option
368 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
372 TRACE("Checking client flags");
373 tmp
= be32_to_cpu(tmp
);
374 if (tmp
!= 0 && tmp
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
375 LOG("Bad client flags received");
379 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
383 TRACE("Checking opts magic");
384 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
385 LOG("Bad magic received");
389 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
394 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
398 length
= be32_to_cpu(length
);
400 TRACE("Checking option");
401 switch (be32_to_cpu(tmp
)) {
403 if (nbd_handle_list(client
, length
) < 0) {
411 case NBD_OPT_EXPORT_NAME
:
412 return nbd_handle_export_name(client
, length
);
415 tmp
= be32_to_cpu(tmp
);
416 LOG("Unsupported option 0x%x", tmp
);
417 nbd_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
423 static int nbd_send_negotiate(NBDClient
*client
)
425 int csock
= client
->sock
;
426 char buf
[8 + 8 + 8 + 128];
428 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
429 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
431 /* Negotiation header without options:
432 [ 0 .. 7] passwd ("NBDMAGIC")
433 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
435 [24 .. 25] server flags (0)
436 [26 .. 27] export flags
437 [28 .. 151] reserved (0)
439 Negotiation header with options, part 1:
440 [ 0 .. 7] passwd ("NBDMAGIC")
441 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
442 [16 .. 17] server flags (0)
444 part 2 (after options are sent):
446 [26 .. 27] export flags
447 [28 .. 151] reserved (0)
450 qemu_set_block(csock
);
453 TRACE("Beginning negotiation.");
454 memset(buf
, 0, sizeof(buf
));
455 memcpy(buf
, "NBDMAGIC", 8);
457 assert ((client
->exp
->nbdflags
& ~65535) == 0);
458 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
459 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
460 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
462 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
463 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
467 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
472 if (write_sync(csock
, buf
, 18) != 18) {
476 rc
= nbd_receive_options(client
);
478 LOG("option negotiation failed");
482 assert ((client
->exp
->nbdflags
& ~65535) == 0);
483 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
484 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
485 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
491 TRACE("Negotiation succeeded.");
494 qemu_set_nonblock(csock
);
498 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
499 off_t
*size
, size_t *blocksize
)
506 TRACE("Receiving negotiation.");
510 if (read_sync(csock
, buf
, 8) != 8) {
516 if (strlen(buf
) == 0) {
517 LOG("server connection closed");
521 TRACE("Magic is %c%c%c%c%c%c%c%c",
522 qemu_isprint(buf
[0]) ? buf
[0] : '.',
523 qemu_isprint(buf
[1]) ? buf
[1] : '.',
524 qemu_isprint(buf
[2]) ? buf
[2] : '.',
525 qemu_isprint(buf
[3]) ? buf
[3] : '.',
526 qemu_isprint(buf
[4]) ? buf
[4] : '.',
527 qemu_isprint(buf
[5]) ? buf
[5] : '.',
528 qemu_isprint(buf
[6]) ? buf
[6] : '.',
529 qemu_isprint(buf
[7]) ? buf
[7] : '.');
531 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
532 LOG("Invalid magic received");
536 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
540 magic
= be64_to_cpu(magic
);
541 TRACE("Magic is 0x%" PRIx64
, magic
);
544 uint32_t reserved
= 0;
548 TRACE("Checking magic (opts_magic)");
549 if (magic
!= NBD_OPTS_MAGIC
) {
550 LOG("Bad magic received");
553 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
554 LOG("flags read failed");
557 *flags
= be16_to_cpu(tmp
) << 16;
558 /* reserved for future use */
559 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
561 LOG("write failed (reserved)");
564 /* write the export name */
565 magic
= cpu_to_be64(magic
);
566 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
567 LOG("write failed (magic)");
570 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
571 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
572 LOG("write failed (opt)");
575 namesize
= cpu_to_be32(strlen(name
));
576 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
578 LOG("write failed (namesize)");
581 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
582 LOG("write failed (name)");
586 TRACE("Checking magic (cli_magic)");
588 if (magic
!= NBD_CLIENT_MAGIC
) {
589 LOG("Bad magic received");
594 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
598 *size
= be64_to_cpu(s
);
600 TRACE("Size is %" PRIu64
, *size
);
603 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
604 LOG("read failed (flags)");
607 *flags
= be32_to_cpup(flags
);
609 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
610 LOG("read failed (tmp)");
613 *flags
|= be32_to_cpu(tmp
);
615 if (read_sync(csock
, &buf
, 124) != 124) {
616 LOG("read failed (buf)");
626 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
628 TRACE("Setting NBD socket");
630 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
632 LOG("Failed to set NBD socket");
636 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
638 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
640 LOG("Failed setting NBD block size");
644 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
646 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
648 LOG("Failed setting size (in blocks)");
652 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
653 if (errno
== ENOTTY
) {
654 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
655 TRACE("Setting readonly attribute");
657 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
659 LOG("Failed setting read-only attribute");
664 LOG("Failed setting flags");
669 TRACE("Negotiation ended");
674 int nbd_disconnect(int fd
)
676 ioctl(fd
, NBD_CLEAR_QUE
);
677 ioctl(fd
, NBD_DISCONNECT
);
678 ioctl(fd
, NBD_CLEAR_SOCK
);
682 int nbd_client(int fd
)
687 TRACE("Doing NBD loop");
689 ret
= ioctl(fd
, NBD_DO_IT
);
690 if (ret
< 0 && errno
== EPIPE
) {
691 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
692 * the socket via NBD_DISCONNECT. We do not want to return 1 in
699 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
701 TRACE("Clearing NBD queue");
702 ioctl(fd
, NBD_CLEAR_QUE
);
704 TRACE("Clearing NBD socket");
705 ioctl(fd
, NBD_CLEAR_SOCK
);
711 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
716 int nbd_disconnect(int fd
)
721 int nbd_client(int fd
)
727 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
729 uint8_t buf
[NBD_REQUEST_SIZE
];
732 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
733 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
734 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
735 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
736 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
738 TRACE("Sending request to client: "
739 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
740 request
->from
, request
->len
, request
->handle
, request
->type
);
742 ret
= write_sync(csock
, buf
, sizeof(buf
));
747 if (ret
!= sizeof(buf
)) {
748 LOG("writing to socket failed");
754 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
756 uint8_t buf
[NBD_REQUEST_SIZE
];
760 ret
= read_sync(csock
, buf
, sizeof(buf
));
765 if (ret
!= sizeof(buf
)) {
771 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
772 [ 4 .. 7] type (0 == READ, 1 == WRITE)
778 magic
= be32_to_cpup((uint32_t*)buf
);
779 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
780 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
781 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
782 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
784 TRACE("Got request: "
785 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
786 magic
, request
->type
, request
->from
, request
->len
);
788 if (magic
!= NBD_REQUEST_MAGIC
) {
789 LOG("invalid magic (got 0x%x)", magic
);
795 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
797 uint8_t buf
[NBD_REPLY_SIZE
];
801 ret
= read_sync(csock
, buf
, sizeof(buf
));
806 if (ret
!= sizeof(buf
)) {
812 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
813 [ 4 .. 7] error (0 == no error)
817 magic
= be32_to_cpup((uint32_t*)buf
);
818 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
819 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
822 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
823 magic
, reply
->error
, reply
->handle
);
825 if (magic
!= NBD_REPLY_MAGIC
) {
826 LOG("invalid magic (got 0x%x)", magic
);
832 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
834 uint8_t buf
[NBD_REPLY_SIZE
];
838 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
839 [ 4 .. 7] error (0 == no error)
842 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
843 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
844 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
846 TRACE("Sending response to client");
848 ret
= write_sync(csock
, buf
, sizeof(buf
));
853 if (ret
!= sizeof(buf
)) {
854 LOG("writing to socket failed");
860 #define MAX_NBD_REQUESTS 16
862 void nbd_client_get(NBDClient
*client
)
867 void nbd_client_put(NBDClient
*client
)
869 if (--client
->refcount
== 0) {
870 /* The last reference should be dropped by client->close,
871 * which is called by nbd_client_close.
873 assert(client
->closing
);
875 nbd_unset_handlers(client
);
879 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
880 nbd_export_put(client
->exp
);
886 void nbd_client_close(NBDClient
*client
)
888 if (client
->closing
) {
892 client
->closing
= true;
894 /* Force requests to finish. They will drop their own references,
895 * then we'll close the socket and free the NBDClient.
897 shutdown(client
->sock
, 2);
899 /* Also tell the client, so that they release their reference. */
901 client
->close(client
);
905 static NBDRequest
*nbd_request_get(NBDClient
*client
)
909 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
910 client
->nb_requests
++;
911 nbd_update_can_read(client
);
913 req
= g_slice_new0(NBDRequest
);
914 nbd_client_get(client
);
915 req
->client
= client
;
919 static void nbd_request_put(NBDRequest
*req
)
921 NBDClient
*client
= req
->client
;
924 qemu_vfree(req
->data
);
926 g_slice_free(NBDRequest
, req
);
928 client
->nb_requests
--;
929 nbd_update_can_read(client
);
930 nbd_client_put(client
);
933 static void bs_aio_attached(AioContext
*ctx
, void *opaque
)
935 NBDExport
*exp
= opaque
;
938 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
942 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
943 nbd_set_handlers(client
);
947 static void bs_aio_detach(void *opaque
)
949 NBDExport
*exp
= opaque
;
952 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
954 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
955 nbd_unset_handlers(client
);
961 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
962 uint32_t nbdflags
, void (*close
)(NBDExport
*))
964 BlockDriverState
*bs
= blk_bs(blk
);
965 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
967 QTAILQ_INIT(&exp
->clients
);
969 exp
->dev_offset
= dev_offset
;
970 exp
->nbdflags
= nbdflags
;
971 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
973 exp
->ctx
= bdrv_get_aio_context(bs
);
975 bdrv_add_aio_context_notifier(bs
, bs_aio_attached
, bs_aio_detach
, exp
);
977 * NBD exports are used for non-shared storage migration. Make sure
978 * that BDRV_O_INCOMING is cleared and the image is ready for write
979 * access since the export could be available before migration handover.
981 bdrv_invalidate_cache(bs
, NULL
);
985 NBDExport
*nbd_export_find(const char *name
)
988 QTAILQ_FOREACH(exp
, &exports
, next
) {
989 if (strcmp(name
, exp
->name
) == 0) {
997 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
999 if (exp
->name
== name
) {
1003 nbd_export_get(exp
);
1004 if (exp
->name
!= NULL
) {
1007 QTAILQ_REMOVE(&exports
, exp
, next
);
1008 nbd_export_put(exp
);
1011 nbd_export_get(exp
);
1012 exp
->name
= g_strdup(name
);
1013 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1015 nbd_export_put(exp
);
1018 void nbd_export_close(NBDExport
*exp
)
1020 NBDClient
*client
, *next
;
1022 nbd_export_get(exp
);
1023 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1024 nbd_client_close(client
);
1026 nbd_export_set_name(exp
, NULL
);
1027 nbd_export_put(exp
);
1029 bdrv_remove_aio_context_notifier(exp
->bs
, bs_aio_attached
,
1030 bs_aio_detach
, exp
);
1031 bdrv_unref(exp
->bs
);
1036 void nbd_export_get(NBDExport
*exp
)
1038 assert(exp
->refcount
> 0);
1042 void nbd_export_put(NBDExport
*exp
)
1044 assert(exp
->refcount
> 0);
1045 if (exp
->refcount
== 1) {
1046 nbd_export_close(exp
);
1049 if (--exp
->refcount
== 0) {
1050 assert(exp
->name
== NULL
);
1060 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1062 return exp
->bs
->blk
;
1065 void nbd_export_close_all(void)
1067 NBDExport
*exp
, *next
;
1069 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1070 nbd_export_close(exp
);
1074 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1077 NBDClient
*client
= req
->client
;
1078 int csock
= client
->sock
;
1081 qemu_co_mutex_lock(&client
->send_lock
);
1082 client
->send_coroutine
= qemu_coroutine_self();
1083 nbd_set_handlers(client
);
1086 rc
= nbd_send_reply(csock
, reply
);
1088 socket_set_cork(csock
, 1);
1089 rc
= nbd_send_reply(csock
, reply
);
1091 ret
= qemu_co_send(csock
, req
->data
, len
);
1096 socket_set_cork(csock
, 0);
1099 client
->send_coroutine
= NULL
;
1100 nbd_set_handlers(client
);
1101 qemu_co_mutex_unlock(&client
->send_lock
);
1105 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1107 NBDClient
*client
= req
->client
;
1108 int csock
= client
->sock
;
1112 client
->recv_coroutine
= qemu_coroutine_self();
1113 nbd_update_can_read(client
);
1115 rc
= nbd_receive_request(csock
, request
);
1117 if (rc
!= -EAGAIN
) {
1123 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1124 LOG("len (%u) is larger than max len (%u)",
1125 request
->len
, NBD_MAX_BUFFER_SIZE
);
1130 if ((request
->from
+ request
->len
) < request
->from
) {
1131 LOG("integer overflow detected! "
1132 "you're probably being attacked");
1137 TRACE("Decoding type");
1139 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1140 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1141 req
->data
= qemu_blockalign(client
->exp
->bs
, request
->len
);
1143 if (command
== NBD_CMD_WRITE
) {
1144 TRACE("Reading %u byte(s)", request
->len
);
1146 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1147 LOG("reading from socket failed");
1155 client
->recv_coroutine
= NULL
;
1156 nbd_update_can_read(client
);
1161 static void nbd_trip(void *opaque
)
1163 NBDClient
*client
= opaque
;
1164 NBDExport
*exp
= client
->exp
;
1166 struct nbd_request request
;
1167 struct nbd_reply reply
;
1171 TRACE("Reading request.");
1172 if (client
->closing
) {
1176 req
= nbd_request_get(client
);
1177 ret
= nbd_co_receive_request(req
, &request
);
1178 if (ret
== -EAGAIN
) {
1185 reply
.handle
= request
.handle
;
1192 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1193 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1194 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1195 ", Offset: %" PRIu64
"\n",
1196 request
.from
, request
.len
,
1197 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1198 LOG("requested operation past EOF--bad client?");
1199 goto invalid_request
;
1204 TRACE("Request type is READ");
1206 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1207 ret
= bdrv_co_flush(exp
->bs
);
1209 LOG("flush failed");
1215 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1216 req
->data
, request
.len
/ 512);
1218 LOG("reading from file failed");
1223 TRACE("Read %u byte(s)", request
.len
);
1224 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1228 TRACE("Request type is WRITE");
1230 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1231 TRACE("Server is read-only, return error");
1232 reply
.error
= EROFS
;
1236 TRACE("Writing to device");
1238 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1239 req
->data
, request
.len
/ 512);
1241 LOG("writing to file failed");
1246 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1247 ret
= bdrv_co_flush(exp
->bs
);
1249 LOG("flush failed");
1255 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1260 TRACE("Request type is DISCONNECT");
1264 TRACE("Request type is FLUSH");
1266 ret
= bdrv_co_flush(exp
->bs
);
1268 LOG("flush failed");
1271 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1276 TRACE("Request type is TRIM");
1277 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1280 LOG("discard failed");
1283 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1288 LOG("invalid request type (%u) received", request
.type
);
1290 reply
.error
= -EINVAL
;
1292 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1298 TRACE("Request/Reply complete");
1301 nbd_request_put(req
);
1305 nbd_request_put(req
);
1306 nbd_client_close(client
);
1309 static void nbd_read(void *opaque
)
1311 NBDClient
*client
= opaque
;
1313 if (client
->recv_coroutine
) {
1314 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1316 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1320 static void nbd_restart_write(void *opaque
)
1322 NBDClient
*client
= opaque
;
1324 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1327 static void nbd_set_handlers(NBDClient
*client
)
1329 if (client
->exp
&& client
->exp
->ctx
) {
1330 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1331 client
->can_read
? nbd_read
: NULL
,
1332 client
->send_coroutine
? nbd_restart_write
: NULL
,
1337 static void nbd_unset_handlers(NBDClient
*client
)
1339 if (client
->exp
&& client
->exp
->ctx
) {
1340 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
, NULL
, NULL
, NULL
);
1344 static void nbd_update_can_read(NBDClient
*client
)
1346 bool can_read
= client
->recv_coroutine
||
1347 client
->nb_requests
< MAX_NBD_REQUESTS
;
1349 if (can_read
!= client
->can_read
) {
1350 client
->can_read
= can_read
;
1351 nbd_set_handlers(client
);
1353 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1354 * in nbd_set_handlers() will have taken care of that */
1358 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1359 void (*close
)(NBDClient
*))
1362 client
= g_malloc0(sizeof(NBDClient
));
1363 client
->refcount
= 1;
1365 client
->sock
= csock
;
1366 client
->can_read
= true;
1367 if (nbd_send_negotiate(client
)) {
1371 client
->close
= close
;
1372 qemu_co_mutex_init(&client
->send_lock
);
1373 nbd_set_handlers(client
);
1376 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1377 nbd_export_get(exp
);