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 "qemu/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 /* NBD errors are based on errno numbers, so there is a 1:1 mapping,
90 * but only a limited set of errno values is specified in the protocol.
91 * Everything else is squashed to EINVAL.
100 static int system_errno_to_nbd_errno(int err
)
123 static int nbd_errno_to_system_errno(int err
)
142 /* Definitions for opaque data types */
144 typedef struct NBDRequest NBDRequest
;
147 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
154 void (*close
)(NBDExport
*exp
);
161 QTAILQ_HEAD(, NBDClient
) clients
;
162 QTAILQ_ENTRY(NBDExport
) next
;
167 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
171 void (*close
)(NBDClient
*client
);
176 Coroutine
*recv_coroutine
;
179 Coroutine
*send_coroutine
;
183 QTAILQ_ENTRY(NBDClient
) next
;
188 /* That's all folks */
190 static void nbd_set_handlers(NBDClient
*client
);
191 static void nbd_unset_handlers(NBDClient
*client
);
192 static void nbd_update_can_read(NBDClient
*client
);
194 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
199 if (qemu_in_coroutine()) {
201 return qemu_co_recv(fd
, buffer
, size
);
203 return qemu_co_send(fd
, buffer
, size
);
207 while (offset
< size
) {
211 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
213 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
217 err
= socket_error();
219 /* recoverable error */
220 if (err
== EINTR
|| (offset
> 0 && (err
== EAGAIN
|| err
== EWOULDBLOCK
))) {
224 /* unrecoverable error */
239 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
241 /* Sockets are kept in blocking mode in the negotiation phase. After
242 * that, a non-readable socket simply means that another thread stole
243 * our request/reply. Synchronization is done with recv_coroutine, so
244 * that this is coroutine-safe.
246 return nbd_wr_sync(fd
, buffer
, size
, true);
249 static ssize_t
drop_sync(int fd
, size_t size
)
251 ssize_t ret
, dropped
= size
;
252 uint8_t *buffer
= g_malloc(MIN(65536, size
));
255 ret
= read_sync(fd
, buffer
, MIN(65536, size
));
269 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
273 /* For writes, we do expect the socket to be writable. */
274 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
275 } while (ret
== -EAGAIN
);
279 /* Basic flow for negotiation
306 static int nbd_send_rep(int csock
, uint32_t type
, uint32_t opt
)
311 magic
= cpu_to_be64(NBD_REP_MAGIC
);
312 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
313 LOG("write failed (rep magic)");
316 opt
= cpu_to_be32(opt
);
317 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
318 LOG("write failed (rep opt)");
321 type
= cpu_to_be32(type
);
322 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
323 LOG("write failed (rep type)");
326 len
= cpu_to_be32(0);
327 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
328 LOG("write failed (rep data length)");
334 static int nbd_send_rep_list(int csock
, NBDExport
*exp
)
336 uint64_t magic
, name_len
;
337 uint32_t opt
, type
, len
;
339 name_len
= strlen(exp
->name
);
340 magic
= cpu_to_be64(NBD_REP_MAGIC
);
341 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
342 LOG("write failed (magic)");
345 opt
= cpu_to_be32(NBD_OPT_LIST
);
346 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
347 LOG("write failed (opt)");
350 type
= cpu_to_be32(NBD_REP_SERVER
);
351 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
352 LOG("write failed (reply type)");
355 len
= cpu_to_be32(name_len
+ sizeof(len
));
356 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
357 LOG("write failed (length)");
360 len
= cpu_to_be32(name_len
);
361 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
362 LOG("write failed (length)");
365 if (write_sync(csock
, exp
->name
, name_len
) != name_len
) {
366 LOG("write failed (buffer)");
372 static int nbd_handle_list(NBDClient
*client
, uint32_t length
)
377 csock
= client
->sock
;
379 if (drop_sync(csock
, length
) != length
) {
382 return nbd_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
385 /* For each export, send a NBD_REP_SERVER reply. */
386 QTAILQ_FOREACH(exp
, &exports
, next
) {
387 if (nbd_send_rep_list(csock
, exp
)) {
391 /* Finish with a NBD_REP_ACK. */
392 return nbd_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
395 static int nbd_handle_export_name(NBDClient
*client
, uint32_t length
)
397 int rc
= -EINVAL
, csock
= client
->sock
;
401 [20 .. xx] export name (length bytes)
403 TRACE("Checking length");
405 LOG("Bad length received");
408 if (read_sync(csock
, name
, length
) != length
) {
414 client
->exp
= nbd_export_find(name
);
416 LOG("export not found");
420 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
421 nbd_export_get(client
->exp
);
427 static int nbd_receive_options(NBDClient
*client
)
429 int csock
= client
->sock
;
433 [ 0 .. 3] client flags
435 [ 0 .. 7] NBD_OPTS_MAGIC
436 [ 8 .. 11] NBD option
437 [12 .. 15] Data length
440 [ 0 .. 7] NBD_OPTS_MAGIC
441 [ 8 .. 11] Second NBD option
442 [12 .. 15] Data length
446 if (read_sync(csock
, &flags
, sizeof(flags
)) != sizeof(flags
)) {
450 TRACE("Checking client flags");
451 be32_to_cpus(&flags
);
452 if (flags
!= 0 && flags
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
453 LOG("Bad client flags received");
459 uint32_t tmp
, length
;
462 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
466 TRACE("Checking opts magic");
467 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
468 LOG("Bad magic received");
472 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
477 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
481 length
= be32_to_cpu(length
);
483 TRACE("Checking option");
484 switch (be32_to_cpu(tmp
)) {
486 ret
= nbd_handle_list(client
, length
);
495 case NBD_OPT_EXPORT_NAME
:
496 return nbd_handle_export_name(client
, length
);
499 tmp
= be32_to_cpu(tmp
);
500 LOG("Unsupported option 0x%x", tmp
);
501 nbd_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
507 static int nbd_send_negotiate(NBDClient
*client
)
509 int csock
= client
->sock
;
510 char buf
[8 + 8 + 8 + 128];
512 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
513 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
515 /* Negotiation header without options:
516 [ 0 .. 7] passwd ("NBDMAGIC")
517 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
519 [24 .. 25] server flags (0)
520 [26 .. 27] export flags
521 [28 .. 151] reserved (0)
523 Negotiation header with options, part 1:
524 [ 0 .. 7] passwd ("NBDMAGIC")
525 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
526 [16 .. 17] server flags (0)
528 part 2 (after options are sent):
530 [26 .. 27] export flags
531 [28 .. 151] reserved (0)
534 qemu_set_block(csock
);
537 TRACE("Beginning negotiation.");
538 memset(buf
, 0, sizeof(buf
));
539 memcpy(buf
, "NBDMAGIC", 8);
541 assert ((client
->exp
->nbdflags
& ~65535) == 0);
542 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
543 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
544 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
546 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
547 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
551 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
556 if (write_sync(csock
, buf
, 18) != 18) {
560 rc
= nbd_receive_options(client
);
562 LOG("option negotiation failed");
566 assert ((client
->exp
->nbdflags
& ~65535) == 0);
567 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
568 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
569 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
575 TRACE("Negotiation succeeded.");
578 qemu_set_nonblock(csock
);
582 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
583 off_t
*size
, Error
**errp
)
590 TRACE("Receiving negotiation.");
594 if (read_sync(csock
, buf
, 8) != 8) {
595 error_setg(errp
, "Failed to read data");
600 if (strlen(buf
) == 0) {
601 error_setg(errp
, "Server connection closed unexpectedly");
605 TRACE("Magic is %c%c%c%c%c%c%c%c",
606 qemu_isprint(buf
[0]) ? buf
[0] : '.',
607 qemu_isprint(buf
[1]) ? buf
[1] : '.',
608 qemu_isprint(buf
[2]) ? buf
[2] : '.',
609 qemu_isprint(buf
[3]) ? buf
[3] : '.',
610 qemu_isprint(buf
[4]) ? buf
[4] : '.',
611 qemu_isprint(buf
[5]) ? buf
[5] : '.',
612 qemu_isprint(buf
[6]) ? buf
[6] : '.',
613 qemu_isprint(buf
[7]) ? buf
[7] : '.');
615 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
616 error_setg(errp
, "Invalid magic received");
620 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
621 error_setg(errp
, "Failed to read magic");
624 magic
= be64_to_cpu(magic
);
625 TRACE("Magic is 0x%" PRIx64
, magic
);
628 uint32_t reserved
= 0;
632 TRACE("Checking magic (opts_magic)");
633 if (magic
!= NBD_OPTS_MAGIC
) {
634 if (magic
== NBD_CLIENT_MAGIC
) {
635 error_setg(errp
, "Server does not support export names");
637 error_setg(errp
, "Bad magic received");
641 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
642 error_setg(errp
, "Failed to read server flags");
645 *flags
= be16_to_cpu(tmp
) << 16;
646 /* reserved for future use */
647 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
649 error_setg(errp
, "Failed to read reserved field");
652 /* write the export name */
653 magic
= cpu_to_be64(magic
);
654 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
655 error_setg(errp
, "Failed to send export name magic");
658 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
659 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
660 error_setg(errp
, "Failed to send export name option number");
663 namesize
= cpu_to_be32(strlen(name
));
664 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
666 error_setg(errp
, "Failed to send export name length");
669 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
670 error_setg(errp
, "Failed to send export name");
674 TRACE("Checking magic (cli_magic)");
676 if (magic
!= NBD_CLIENT_MAGIC
) {
677 if (magic
== NBD_OPTS_MAGIC
) {
678 error_setg(errp
, "Server requires an export name");
680 error_setg(errp
, "Bad magic received");
686 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
687 error_setg(errp
, "Failed to read export length");
690 *size
= be64_to_cpu(s
);
691 TRACE("Size is %" PRIu64
, *size
);
694 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
695 error_setg(errp
, "Failed to read export flags");
698 *flags
= be32_to_cpup(flags
);
700 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
701 error_setg(errp
, "Failed to read export flags");
704 *flags
|= be16_to_cpu(tmp
);
706 if (read_sync(csock
, &buf
, 124) != 124) {
707 error_setg(errp
, "Failed to read reserved block");
717 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
719 TRACE("Setting NBD socket");
721 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
723 LOG("Failed to set NBD socket");
727 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
729 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
731 LOG("Failed setting NBD block size");
735 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
737 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, (size_t)(size
/ BDRV_SECTOR_SIZE
)) < 0) {
739 LOG("Failed setting size (in blocks)");
743 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
744 if (errno
== ENOTTY
) {
745 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
746 TRACE("Setting readonly attribute");
748 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
750 LOG("Failed setting read-only attribute");
755 LOG("Failed setting flags");
760 TRACE("Negotiation ended");
765 int nbd_disconnect(int fd
)
767 ioctl(fd
, NBD_CLEAR_QUE
);
768 ioctl(fd
, NBD_DISCONNECT
);
769 ioctl(fd
, NBD_CLEAR_SOCK
);
773 int nbd_client(int fd
)
778 TRACE("Doing NBD loop");
780 ret
= ioctl(fd
, NBD_DO_IT
);
781 if (ret
< 0 && errno
== EPIPE
) {
782 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
783 * the socket via NBD_DISCONNECT. We do not want to return 1 in
790 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
792 TRACE("Clearing NBD queue");
793 ioctl(fd
, NBD_CLEAR_QUE
);
795 TRACE("Clearing NBD socket");
796 ioctl(fd
, NBD_CLEAR_SOCK
);
802 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
807 int nbd_disconnect(int fd
)
812 int nbd_client(int fd
)
818 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
820 uint8_t buf
[NBD_REQUEST_SIZE
];
823 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
824 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
825 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
826 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
827 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
829 TRACE("Sending request to client: "
830 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
831 request
->from
, request
->len
, request
->handle
, request
->type
);
833 ret
= write_sync(csock
, buf
, sizeof(buf
));
838 if (ret
!= sizeof(buf
)) {
839 LOG("writing to socket failed");
845 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
847 uint8_t buf
[NBD_REQUEST_SIZE
];
851 ret
= read_sync(csock
, buf
, sizeof(buf
));
856 if (ret
!= sizeof(buf
)) {
862 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
863 [ 4 .. 7] type (0 == READ, 1 == WRITE)
869 magic
= be32_to_cpup((uint32_t*)buf
);
870 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
871 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
872 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
873 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
875 TRACE("Got request: "
876 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
877 magic
, request
->type
, request
->from
, request
->len
);
879 if (magic
!= NBD_REQUEST_MAGIC
) {
880 LOG("invalid magic (got 0x%x)", magic
);
886 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
888 uint8_t buf
[NBD_REPLY_SIZE
];
892 ret
= read_sync(csock
, buf
, sizeof(buf
));
897 if (ret
!= sizeof(buf
)) {
903 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
904 [ 4 .. 7] error (0 == no error)
908 magic
= be32_to_cpup((uint32_t*)buf
);
909 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
910 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
912 reply
->error
= nbd_errno_to_system_errno(reply
->error
);
915 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
916 magic
, reply
->error
, reply
->handle
);
918 if (magic
!= NBD_REPLY_MAGIC
) {
919 LOG("invalid magic (got 0x%x)", magic
);
925 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
927 uint8_t buf
[NBD_REPLY_SIZE
];
930 reply
->error
= system_errno_to_nbd_errno(reply
->error
);
933 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
934 [ 4 .. 7] error (0 == no error)
937 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
938 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
939 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
941 TRACE("Sending response to client");
943 ret
= write_sync(csock
, buf
, sizeof(buf
));
948 if (ret
!= sizeof(buf
)) {
949 LOG("writing to socket failed");
955 #define MAX_NBD_REQUESTS 16
957 void nbd_client_get(NBDClient
*client
)
962 void nbd_client_put(NBDClient
*client
)
964 if (--client
->refcount
== 0) {
965 /* The last reference should be dropped by client->close,
966 * which is called by client_close.
968 assert(client
->closing
);
970 nbd_unset_handlers(client
);
974 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
975 nbd_export_put(client
->exp
);
981 static void client_close(NBDClient
*client
)
983 if (client
->closing
) {
987 client
->closing
= true;
989 /* Force requests to finish. They will drop their own references,
990 * then we'll close the socket and free the NBDClient.
992 shutdown(client
->sock
, 2);
994 /* Also tell the client, so that they release their reference. */
996 client
->close(client
);
1000 static NBDRequest
*nbd_request_get(NBDClient
*client
)
1004 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
1005 client
->nb_requests
++;
1006 nbd_update_can_read(client
);
1008 req
= g_new0(NBDRequest
, 1);
1009 nbd_client_get(client
);
1010 req
->client
= client
;
1014 static void nbd_request_put(NBDRequest
*req
)
1016 NBDClient
*client
= req
->client
;
1019 qemu_vfree(req
->data
);
1023 client
->nb_requests
--;
1024 nbd_update_can_read(client
);
1025 nbd_client_put(client
);
1028 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
1030 NBDExport
*exp
= opaque
;
1033 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
1037 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1038 nbd_set_handlers(client
);
1042 static void blk_aio_detach(void *opaque
)
1044 NBDExport
*exp
= opaque
;
1047 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
1049 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
1050 nbd_unset_handlers(client
);
1056 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
1057 uint32_t nbdflags
, void (*close
)(NBDExport
*),
1060 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
1062 QTAILQ_INIT(&exp
->clients
);
1064 exp
->dev_offset
= dev_offset
;
1065 exp
->nbdflags
= nbdflags
;
1066 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
1067 if (exp
->size
< 0) {
1068 error_setg_errno(errp
, -exp
->size
,
1069 "Failed to determine the NBD export's length");
1072 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
1075 exp
->ctx
= blk_get_aio_context(blk
);
1077 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
1079 * NBD exports are used for non-shared storage migration. Make sure
1080 * that BDRV_O_INCOMING is cleared and the image is ready for write
1081 * access since the export could be available before migration handover.
1083 blk_invalidate_cache(blk
, NULL
);
1091 NBDExport
*nbd_export_find(const char *name
)
1094 QTAILQ_FOREACH(exp
, &exports
, next
) {
1095 if (strcmp(name
, exp
->name
) == 0) {
1103 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
1105 if (exp
->name
== name
) {
1109 nbd_export_get(exp
);
1110 if (exp
->name
!= NULL
) {
1113 QTAILQ_REMOVE(&exports
, exp
, next
);
1114 nbd_export_put(exp
);
1117 nbd_export_get(exp
);
1118 exp
->name
= g_strdup(name
);
1119 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1121 nbd_export_put(exp
);
1124 void nbd_export_close(NBDExport
*exp
)
1126 NBDClient
*client
, *next
;
1128 nbd_export_get(exp
);
1129 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1130 client_close(client
);
1132 nbd_export_set_name(exp
, NULL
);
1133 nbd_export_put(exp
);
1136 void nbd_export_get(NBDExport
*exp
)
1138 assert(exp
->refcount
> 0);
1142 void nbd_export_put(NBDExport
*exp
)
1144 assert(exp
->refcount
> 0);
1145 if (exp
->refcount
== 1) {
1146 nbd_export_close(exp
);
1149 if (--exp
->refcount
== 0) {
1150 assert(exp
->name
== NULL
);
1157 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
1158 blk_aio_detach
, exp
);
1159 blk_unref(exp
->blk
);
1167 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1172 void nbd_export_close_all(void)
1174 NBDExport
*exp
, *next
;
1176 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1177 nbd_export_close(exp
);
1181 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1184 NBDClient
*client
= req
->client
;
1185 int csock
= client
->sock
;
1188 qemu_co_mutex_lock(&client
->send_lock
);
1189 client
->send_coroutine
= qemu_coroutine_self();
1190 nbd_set_handlers(client
);
1193 rc
= nbd_send_reply(csock
, reply
);
1195 socket_set_cork(csock
, 1);
1196 rc
= nbd_send_reply(csock
, reply
);
1198 ret
= qemu_co_send(csock
, req
->data
, len
);
1203 socket_set_cork(csock
, 0);
1206 client
->send_coroutine
= NULL
;
1207 nbd_set_handlers(client
);
1208 qemu_co_mutex_unlock(&client
->send_lock
);
1212 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1214 NBDClient
*client
= req
->client
;
1215 int csock
= client
->sock
;
1219 client
->recv_coroutine
= qemu_coroutine_self();
1220 nbd_update_can_read(client
);
1222 rc
= nbd_receive_request(csock
, request
);
1224 if (rc
!= -EAGAIN
) {
1230 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1231 LOG("len (%u) is larger than max len (%u)",
1232 request
->len
, NBD_MAX_BUFFER_SIZE
);
1237 if ((request
->from
+ request
->len
) < request
->from
) {
1238 LOG("integer overflow detected! "
1239 "you're probably being attacked");
1244 TRACE("Decoding type");
1246 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1247 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1248 req
->data
= blk_blockalign(client
->exp
->blk
, request
->len
);
1250 if (command
== NBD_CMD_WRITE
) {
1251 TRACE("Reading %u byte(s)", request
->len
);
1253 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1254 LOG("reading from socket failed");
1262 client
->recv_coroutine
= NULL
;
1263 nbd_update_can_read(client
);
1268 static void nbd_trip(void *opaque
)
1270 NBDClient
*client
= opaque
;
1271 NBDExport
*exp
= client
->exp
;
1273 struct nbd_request request
;
1274 struct nbd_reply reply
;
1278 TRACE("Reading request.");
1279 if (client
->closing
) {
1283 req
= nbd_request_get(client
);
1284 ret
= nbd_co_receive_request(req
, &request
);
1285 if (ret
== -EAGAIN
) {
1292 reply
.handle
= request
.handle
;
1299 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1300 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1301 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1302 ", Offset: %" PRIu64
"\n",
1303 request
.from
, request
.len
,
1304 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1305 LOG("requested operation past EOF--bad client?");
1306 goto invalid_request
;
1309 if (client
->closing
) {
1311 * The client may be closed when we are blocked in
1312 * nbd_co_receive_request()
1319 TRACE("Request type is READ");
1321 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1322 ret
= blk_co_flush(exp
->blk
);
1324 LOG("flush failed");
1330 ret
= blk_read(exp
->blk
,
1331 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1332 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1334 LOG("reading from file failed");
1339 TRACE("Read %u byte(s)", request
.len
);
1340 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1344 TRACE("Request type is WRITE");
1346 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1347 TRACE("Server is read-only, return error");
1348 reply
.error
= EROFS
;
1352 TRACE("Writing to device");
1354 ret
= blk_write(exp
->blk
,
1355 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1356 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1358 LOG("writing to file failed");
1363 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1364 ret
= blk_co_flush(exp
->blk
);
1366 LOG("flush failed");
1372 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1377 TRACE("Request type is DISCONNECT");
1381 TRACE("Request type is FLUSH");
1383 ret
= blk_co_flush(exp
->blk
);
1385 LOG("flush failed");
1388 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1393 TRACE("Request type is TRIM");
1394 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
1396 request
.len
/ BDRV_SECTOR_SIZE
);
1398 LOG("discard failed");
1401 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1406 LOG("invalid request type (%u) received", request
.type
);
1408 reply
.error
= EINVAL
;
1410 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1416 TRACE("Request/Reply complete");
1419 nbd_request_put(req
);
1423 nbd_request_put(req
);
1424 client_close(client
);
1427 static void nbd_read(void *opaque
)
1429 NBDClient
*client
= opaque
;
1431 if (client
->recv_coroutine
) {
1432 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1434 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1438 static void nbd_restart_write(void *opaque
)
1440 NBDClient
*client
= opaque
;
1442 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1445 static void nbd_set_handlers(NBDClient
*client
)
1447 if (client
->exp
&& client
->exp
->ctx
) {
1448 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1449 client
->can_read
? nbd_read
: NULL
,
1450 client
->send_coroutine
? nbd_restart_write
: NULL
,
1455 static void nbd_unset_handlers(NBDClient
*client
)
1457 if (client
->exp
&& client
->exp
->ctx
) {
1458 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
, NULL
, NULL
, NULL
);
1462 static void nbd_update_can_read(NBDClient
*client
)
1464 bool can_read
= client
->recv_coroutine
||
1465 client
->nb_requests
< MAX_NBD_REQUESTS
;
1467 if (can_read
!= client
->can_read
) {
1468 client
->can_read
= can_read
;
1469 nbd_set_handlers(client
);
1471 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1472 * in nbd_set_handlers() will have taken care of that */
1476 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1477 void (*close
)(NBDClient
*))
1480 client
= g_malloc0(sizeof(NBDClient
));
1481 client
->refcount
= 1;
1483 client
->sock
= csock
;
1484 client
->can_read
= true;
1485 if (nbd_send_negotiate(client
)) {
1489 client
->close
= close
;
1490 qemu_co_mutex_init(&client
->send_lock
);
1491 nbd_set_handlers(client
);
1494 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1495 nbd_export_get(exp
);