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 "sysemu/block-backend.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
);
108 QTAILQ_HEAD(, NBDClient
) clients
;
109 QTAILQ_ENTRY(NBDExport
) next
;
114 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
118 void (*close
)(NBDClient
*client
);
123 Coroutine
*recv_coroutine
;
126 Coroutine
*send_coroutine
;
130 QTAILQ_ENTRY(NBDClient
) next
;
135 /* That's all folks */
137 static void nbd_set_handlers(NBDClient
*client
);
138 static void nbd_unset_handlers(NBDClient
*client
);
139 static void nbd_update_can_read(NBDClient
*client
);
141 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
146 if (qemu_in_coroutine()) {
148 return qemu_co_recv(fd
, buffer
, size
);
150 return qemu_co_send(fd
, buffer
, size
);
154 while (offset
< size
) {
158 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
160 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
164 err
= socket_error();
166 /* recoverable error */
167 if (err
== EINTR
|| (offset
> 0 && (err
== EAGAIN
|| err
== EWOULDBLOCK
))) {
171 /* unrecoverable error */
186 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
188 /* Sockets are kept in blocking mode in the negotiation phase. After
189 * that, a non-readable socket simply means that another thread stole
190 * our request/reply. Synchronization is done with recv_coroutine, so
191 * that this is coroutine-safe.
193 return nbd_wr_sync(fd
, buffer
, size
, true);
196 static ssize_t
drop_sync(int fd
, size_t size
)
198 ssize_t ret
, dropped
= size
;
199 uint8_t *buffer
= g_malloc(MIN(65536, size
));
202 ret
= read_sync(fd
, buffer
, MIN(65536, size
));
216 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
220 /* For writes, we do expect the socket to be writable. */
221 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
222 } while (ret
== -EAGAIN
);
226 /* Basic flow for negotiation
253 static int nbd_send_rep(int csock
, uint32_t type
, uint32_t opt
)
258 magic
= cpu_to_be64(NBD_REP_MAGIC
);
259 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
260 LOG("write failed (rep magic)");
263 opt
= cpu_to_be32(opt
);
264 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
265 LOG("write failed (rep opt)");
268 type
= cpu_to_be32(type
);
269 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
270 LOG("write failed (rep type)");
273 len
= cpu_to_be32(0);
274 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
275 LOG("write failed (rep data length)");
281 static int nbd_send_rep_list(int csock
, NBDExport
*exp
)
283 uint64_t magic
, name_len
;
284 uint32_t opt
, type
, len
;
286 name_len
= strlen(exp
->name
);
287 magic
= cpu_to_be64(NBD_REP_MAGIC
);
288 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
289 LOG("write failed (magic)");
292 opt
= cpu_to_be32(NBD_OPT_LIST
);
293 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
294 LOG("write failed (opt)");
297 type
= cpu_to_be32(NBD_REP_SERVER
);
298 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
299 LOG("write failed (reply type)");
302 len
= cpu_to_be32(name_len
+ sizeof(len
));
303 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
304 LOG("write failed (length)");
307 len
= cpu_to_be32(name_len
);
308 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
309 LOG("write failed (length)");
312 if (write_sync(csock
, exp
->name
, name_len
) != name_len
) {
313 LOG("write failed (buffer)");
319 static int nbd_handle_list(NBDClient
*client
, uint32_t length
)
324 csock
= client
->sock
;
326 if (drop_sync(csock
, length
) != length
) {
329 return nbd_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
332 /* For each export, send a NBD_REP_SERVER reply. */
333 QTAILQ_FOREACH(exp
, &exports
, next
) {
334 if (nbd_send_rep_list(csock
, exp
)) {
338 /* Finish with a NBD_REP_ACK. */
339 return nbd_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
342 static int nbd_handle_export_name(NBDClient
*client
, uint32_t length
)
344 int rc
= -EINVAL
, csock
= client
->sock
;
348 [20 .. xx] export name (length bytes)
350 TRACE("Checking length");
352 LOG("Bad length received");
355 if (read_sync(csock
, name
, length
) != length
) {
361 client
->exp
= nbd_export_find(name
);
363 LOG("export not found");
367 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
368 nbd_export_get(client
->exp
);
374 static int nbd_receive_options(NBDClient
*client
)
376 int csock
= client
->sock
;
380 [ 0 .. 3] client flags
382 [ 0 .. 7] NBD_OPTS_MAGIC
383 [ 8 .. 11] NBD option
384 [12 .. 15] Data length
387 [ 0 .. 7] NBD_OPTS_MAGIC
388 [ 8 .. 11] Second NBD option
389 [12 .. 15] Data length
393 if (read_sync(csock
, &flags
, sizeof(flags
)) != sizeof(flags
)) {
397 TRACE("Checking client flags");
398 be32_to_cpus(&flags
);
399 if (flags
!= 0 && flags
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
400 LOG("Bad client flags received");
406 uint32_t tmp
, length
;
409 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
413 TRACE("Checking opts magic");
414 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
415 LOG("Bad magic received");
419 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
424 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
428 length
= be32_to_cpu(length
);
430 TRACE("Checking option");
431 switch (be32_to_cpu(tmp
)) {
433 ret
= nbd_handle_list(client
, length
);
442 case NBD_OPT_EXPORT_NAME
:
443 return nbd_handle_export_name(client
, length
);
446 tmp
= be32_to_cpu(tmp
);
447 LOG("Unsupported option 0x%x", tmp
);
448 nbd_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
454 static int nbd_send_negotiate(NBDClient
*client
)
456 int csock
= client
->sock
;
457 char buf
[8 + 8 + 8 + 128];
459 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
460 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
462 /* Negotiation header without options:
463 [ 0 .. 7] passwd ("NBDMAGIC")
464 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
466 [24 .. 25] server flags (0)
467 [26 .. 27] export flags
468 [28 .. 151] reserved (0)
470 Negotiation header with options, part 1:
471 [ 0 .. 7] passwd ("NBDMAGIC")
472 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
473 [16 .. 17] server flags (0)
475 part 2 (after options are sent):
477 [26 .. 27] export flags
478 [28 .. 151] reserved (0)
481 qemu_set_block(csock
);
484 TRACE("Beginning negotiation.");
485 memset(buf
, 0, sizeof(buf
));
486 memcpy(buf
, "NBDMAGIC", 8);
488 assert ((client
->exp
->nbdflags
& ~65535) == 0);
489 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
490 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
491 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
493 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
494 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
498 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
503 if (write_sync(csock
, buf
, 18) != 18) {
507 rc
= nbd_receive_options(client
);
509 LOG("option negotiation failed");
513 assert ((client
->exp
->nbdflags
& ~65535) == 0);
514 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
515 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
516 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
522 TRACE("Negotiation succeeded.");
525 qemu_set_nonblock(csock
);
529 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
530 off_t
*size
, Error
**errp
)
537 TRACE("Receiving negotiation.");
541 if (read_sync(csock
, buf
, 8) != 8) {
542 error_setg(errp
, "Failed to read data");
547 if (strlen(buf
) == 0) {
548 error_setg(errp
, "Server connection closed unexpectedly");
552 TRACE("Magic is %c%c%c%c%c%c%c%c",
553 qemu_isprint(buf
[0]) ? buf
[0] : '.',
554 qemu_isprint(buf
[1]) ? buf
[1] : '.',
555 qemu_isprint(buf
[2]) ? buf
[2] : '.',
556 qemu_isprint(buf
[3]) ? buf
[3] : '.',
557 qemu_isprint(buf
[4]) ? buf
[4] : '.',
558 qemu_isprint(buf
[5]) ? buf
[5] : '.',
559 qemu_isprint(buf
[6]) ? buf
[6] : '.',
560 qemu_isprint(buf
[7]) ? buf
[7] : '.');
562 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
563 error_setg(errp
, "Invalid magic received");
567 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
568 error_setg(errp
, "Failed to read magic");
571 magic
= be64_to_cpu(magic
);
572 TRACE("Magic is 0x%" PRIx64
, magic
);
575 uint32_t reserved
= 0;
579 TRACE("Checking magic (opts_magic)");
580 if (magic
!= NBD_OPTS_MAGIC
) {
581 if (magic
== NBD_CLIENT_MAGIC
) {
582 error_setg(errp
, "Server does not support export names");
584 error_setg(errp
, "Bad magic received");
588 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
589 error_setg(errp
, "Failed to read server flags");
592 *flags
= be16_to_cpu(tmp
) << 16;
593 /* reserved for future use */
594 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
596 error_setg(errp
, "Failed to read reserved field");
599 /* write the export name */
600 magic
= cpu_to_be64(magic
);
601 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
602 error_setg(errp
, "Failed to send export name magic");
605 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
606 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
607 error_setg(errp
, "Failed to send export name option number");
610 namesize
= cpu_to_be32(strlen(name
));
611 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
613 error_setg(errp
, "Failed to send export name length");
616 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
617 error_setg(errp
, "Failed to send export name");
621 TRACE("Checking magic (cli_magic)");
623 if (magic
!= NBD_CLIENT_MAGIC
) {
624 if (magic
== NBD_OPTS_MAGIC
) {
625 error_setg(errp
, "Server requires an export name");
627 error_setg(errp
, "Bad magic received");
633 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
634 error_setg(errp
, "Failed to read export length");
637 *size
= be64_to_cpu(s
);
638 TRACE("Size is %" PRIu64
, *size
);
641 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
642 error_setg(errp
, "Failed to read export flags");
645 *flags
= be32_to_cpup(flags
);
647 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
648 error_setg(errp
, "Failed to read export flags");
651 *flags
|= be16_to_cpu(tmp
);
653 if (read_sync(csock
, &buf
, 124) != 124) {
654 error_setg(errp
, "Failed to read reserved block");
664 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
666 TRACE("Setting NBD socket");
668 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
670 LOG("Failed to set NBD socket");
674 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
676 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
678 LOG("Failed setting NBD block size");
682 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
684 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
686 LOG("Failed setting size (in blocks)");
690 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
691 if (errno
== ENOTTY
) {
692 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
693 TRACE("Setting readonly attribute");
695 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
697 LOG("Failed setting read-only attribute");
702 LOG("Failed setting flags");
707 TRACE("Negotiation ended");
712 int nbd_disconnect(int fd
)
714 ioctl(fd
, NBD_CLEAR_QUE
);
715 ioctl(fd
, NBD_DISCONNECT
);
716 ioctl(fd
, NBD_CLEAR_SOCK
);
720 int nbd_client(int fd
)
725 TRACE("Doing NBD loop");
727 ret
= ioctl(fd
, NBD_DO_IT
);
728 if (ret
< 0 && errno
== EPIPE
) {
729 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
730 * the socket via NBD_DISCONNECT. We do not want to return 1 in
737 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
739 TRACE("Clearing NBD queue");
740 ioctl(fd
, NBD_CLEAR_QUE
);
742 TRACE("Clearing NBD socket");
743 ioctl(fd
, NBD_CLEAR_SOCK
);
749 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
754 int nbd_disconnect(int fd
)
759 int nbd_client(int fd
)
765 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
767 uint8_t buf
[NBD_REQUEST_SIZE
];
770 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
771 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
772 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
773 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
774 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
776 TRACE("Sending request to client: "
777 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
778 request
->from
, request
->len
, request
->handle
, request
->type
);
780 ret
= write_sync(csock
, buf
, sizeof(buf
));
785 if (ret
!= sizeof(buf
)) {
786 LOG("writing to socket failed");
792 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
794 uint8_t buf
[NBD_REQUEST_SIZE
];
798 ret
= read_sync(csock
, buf
, sizeof(buf
));
803 if (ret
!= sizeof(buf
)) {
809 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
810 [ 4 .. 7] type (0 == READ, 1 == WRITE)
816 magic
= be32_to_cpup((uint32_t*)buf
);
817 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
818 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
819 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
820 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
822 TRACE("Got request: "
823 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
824 magic
, request
->type
, request
->from
, request
->len
);
826 if (magic
!= NBD_REQUEST_MAGIC
) {
827 LOG("invalid magic (got 0x%x)", magic
);
833 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
835 uint8_t buf
[NBD_REPLY_SIZE
];
839 ret
= read_sync(csock
, buf
, sizeof(buf
));
844 if (ret
!= sizeof(buf
)) {
850 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
851 [ 4 .. 7] error (0 == no error)
855 magic
= be32_to_cpup((uint32_t*)buf
);
856 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
857 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
860 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
861 magic
, reply
->error
, reply
->handle
);
863 if (magic
!= NBD_REPLY_MAGIC
) {
864 LOG("invalid magic (got 0x%x)", magic
);
870 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
872 uint8_t buf
[NBD_REPLY_SIZE
];
876 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
877 [ 4 .. 7] error (0 == no error)
880 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
881 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
882 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
884 TRACE("Sending response to client");
886 ret
= write_sync(csock
, buf
, sizeof(buf
));
891 if (ret
!= sizeof(buf
)) {
892 LOG("writing to socket failed");
898 #define MAX_NBD_REQUESTS 16
900 void nbd_client_get(NBDClient
*client
)
905 void nbd_client_put(NBDClient
*client
)
907 if (--client
->refcount
== 0) {
908 /* The last reference should be dropped by client->close,
909 * which is called by client_close.
911 assert(client
->closing
);
913 nbd_unset_handlers(client
);
917 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
918 nbd_export_put(client
->exp
);
924 static void client_close(NBDClient
*client
)
926 if (client
->closing
) {
930 client
->closing
= true;
932 /* Force requests to finish. They will drop their own references,
933 * then we'll close the socket and free the NBDClient.
935 shutdown(client
->sock
, 2);
937 /* Also tell the client, so that they release their reference. */
939 client
->close(client
);
943 static NBDRequest
*nbd_request_get(NBDClient
*client
)
947 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
948 client
->nb_requests
++;
949 nbd_update_can_read(client
);
951 req
= g_slice_new0(NBDRequest
);
952 nbd_client_get(client
);
953 req
->client
= client
;
957 static void nbd_request_put(NBDRequest
*req
)
959 NBDClient
*client
= req
->client
;
962 qemu_vfree(req
->data
);
964 g_slice_free(NBDRequest
, req
);
966 client
->nb_requests
--;
967 nbd_update_can_read(client
);
968 nbd_client_put(client
);
971 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
973 NBDExport
*exp
= opaque
;
976 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
980 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
981 nbd_set_handlers(client
);
985 static void blk_aio_detach(void *opaque
)
987 NBDExport
*exp
= opaque
;
990 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
992 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
993 nbd_unset_handlers(client
);
999 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
1000 uint32_t nbdflags
, void (*close
)(NBDExport
*),
1003 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
1005 QTAILQ_INIT(&exp
->clients
);
1007 exp
->dev_offset
= dev_offset
;
1008 exp
->nbdflags
= nbdflags
;
1009 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
1010 if (exp
->size
< 0) {
1011 error_setg_errno(errp
, -exp
->size
,
1012 "Failed to determine the NBD export's length");
1015 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
1018 exp
->ctx
= blk_get_aio_context(blk
);
1020 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
1022 * NBD exports are used for non-shared storage migration. Make sure
1023 * that BDRV_O_INCOMING is cleared and the image is ready for write
1024 * access since the export could be available before migration handover.
1026 blk_invalidate_cache(blk
, NULL
);
1034 NBDExport
*nbd_export_find(const char *name
)
1037 QTAILQ_FOREACH(exp
, &exports
, next
) {
1038 if (strcmp(name
, exp
->name
) == 0) {
1046 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
1048 if (exp
->name
== name
) {
1052 nbd_export_get(exp
);
1053 if (exp
->name
!= NULL
) {
1056 QTAILQ_REMOVE(&exports
, exp
, next
);
1057 nbd_export_put(exp
);
1060 nbd_export_get(exp
);
1061 exp
->name
= g_strdup(name
);
1062 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1064 nbd_export_put(exp
);
1067 void nbd_export_close(NBDExport
*exp
)
1069 NBDClient
*client
, *next
;
1071 nbd_export_get(exp
);
1072 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1073 client_close(client
);
1075 nbd_export_set_name(exp
, NULL
);
1076 nbd_export_put(exp
);
1078 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
1079 blk_aio_detach
, exp
);
1080 blk_unref(exp
->blk
);
1085 void nbd_export_get(NBDExport
*exp
)
1087 assert(exp
->refcount
> 0);
1091 void nbd_export_put(NBDExport
*exp
)
1093 assert(exp
->refcount
> 0);
1094 if (exp
->refcount
== 1) {
1095 nbd_export_close(exp
);
1098 if (--exp
->refcount
== 0) {
1099 assert(exp
->name
== NULL
);
1109 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1114 void nbd_export_close_all(void)
1116 NBDExport
*exp
, *next
;
1118 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1119 nbd_export_close(exp
);
1123 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1126 NBDClient
*client
= req
->client
;
1127 int csock
= client
->sock
;
1130 qemu_co_mutex_lock(&client
->send_lock
);
1131 client
->send_coroutine
= qemu_coroutine_self();
1132 nbd_set_handlers(client
);
1135 rc
= nbd_send_reply(csock
, reply
);
1137 socket_set_cork(csock
, 1);
1138 rc
= nbd_send_reply(csock
, reply
);
1140 ret
= qemu_co_send(csock
, req
->data
, len
);
1145 socket_set_cork(csock
, 0);
1148 client
->send_coroutine
= NULL
;
1149 nbd_set_handlers(client
);
1150 qemu_co_mutex_unlock(&client
->send_lock
);
1154 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1156 NBDClient
*client
= req
->client
;
1157 int csock
= client
->sock
;
1161 client
->recv_coroutine
= qemu_coroutine_self();
1162 nbd_update_can_read(client
);
1164 rc
= nbd_receive_request(csock
, request
);
1166 if (rc
!= -EAGAIN
) {
1172 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1173 LOG("len (%u) is larger than max len (%u)",
1174 request
->len
, NBD_MAX_BUFFER_SIZE
);
1179 if ((request
->from
+ request
->len
) < request
->from
) {
1180 LOG("integer overflow detected! "
1181 "you're probably being attacked");
1186 TRACE("Decoding type");
1188 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1189 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1190 req
->data
= blk_blockalign(client
->exp
->blk
, request
->len
);
1192 if (command
== NBD_CMD_WRITE
) {
1193 TRACE("Reading %u byte(s)", request
->len
);
1195 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1196 LOG("reading from socket failed");
1204 client
->recv_coroutine
= NULL
;
1205 nbd_update_can_read(client
);
1210 static void nbd_trip(void *opaque
)
1212 NBDClient
*client
= opaque
;
1213 NBDExport
*exp
= client
->exp
;
1215 struct nbd_request request
;
1216 struct nbd_reply reply
;
1220 TRACE("Reading request.");
1221 if (client
->closing
) {
1225 req
= nbd_request_get(client
);
1226 ret
= nbd_co_receive_request(req
, &request
);
1227 if (ret
== -EAGAIN
) {
1234 reply
.handle
= request
.handle
;
1241 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1242 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1243 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1244 ", Offset: %" PRIu64
"\n",
1245 request
.from
, request
.len
,
1246 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1247 LOG("requested operation past EOF--bad client?");
1248 goto invalid_request
;
1253 TRACE("Request type is READ");
1255 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1256 ret
= blk_co_flush(exp
->blk
);
1258 LOG("flush failed");
1264 ret
= blk_read(exp
->blk
,
1265 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1266 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1268 LOG("reading from file failed");
1273 TRACE("Read %u byte(s)", request
.len
);
1274 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1278 TRACE("Request type is WRITE");
1280 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1281 TRACE("Server is read-only, return error");
1282 reply
.error
= EROFS
;
1286 TRACE("Writing to device");
1288 ret
= blk_write(exp
->blk
,
1289 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1290 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1292 LOG("writing to file failed");
1297 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1298 ret
= blk_co_flush(exp
->blk
);
1300 LOG("flush failed");
1306 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1311 TRACE("Request type is DISCONNECT");
1315 TRACE("Request type is FLUSH");
1317 ret
= blk_co_flush(exp
->blk
);
1319 LOG("flush failed");
1322 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1327 TRACE("Request type is TRIM");
1328 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
1330 request
.len
/ BDRV_SECTOR_SIZE
);
1332 LOG("discard failed");
1335 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1340 LOG("invalid request type (%u) received", request
.type
);
1342 reply
.error
= EINVAL
;
1344 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1350 TRACE("Request/Reply complete");
1353 nbd_request_put(req
);
1357 nbd_request_put(req
);
1358 client_close(client
);
1361 static void nbd_read(void *opaque
)
1363 NBDClient
*client
= opaque
;
1365 if (client
->recv_coroutine
) {
1366 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1368 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1372 static void nbd_restart_write(void *opaque
)
1374 NBDClient
*client
= opaque
;
1376 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1379 static void nbd_set_handlers(NBDClient
*client
)
1381 if (client
->exp
&& client
->exp
->ctx
) {
1382 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1383 client
->can_read
? nbd_read
: NULL
,
1384 client
->send_coroutine
? nbd_restart_write
: NULL
,
1389 static void nbd_unset_handlers(NBDClient
*client
)
1391 if (client
->exp
&& client
->exp
->ctx
) {
1392 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
, NULL
, NULL
, NULL
);
1396 static void nbd_update_can_read(NBDClient
*client
)
1398 bool can_read
= client
->recv_coroutine
||
1399 client
->nb_requests
< MAX_NBD_REQUESTS
;
1401 if (can_read
!= client
->can_read
) {
1402 client
->can_read
= can_read
;
1403 nbd_set_handlers(client
);
1405 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1406 * in nbd_set_handlers() will have taken care of that */
1410 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1411 void (*close
)(NBDClient
*))
1414 client
= g_malloc0(sizeof(NBDClient
));
1415 client
->refcount
= 1;
1417 client
->sock
= csock
;
1418 client
->can_read
= true;
1419 if (nbd_send_negotiate(client
)) {
1423 client
->close
= close
;
1424 qemu_co_mutex_init(&client
->send_lock
);
1425 nbd_set_handlers(client
);
1428 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1429 nbd_export_get(exp
);