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
write_sync(int fd
, void *buffer
, size_t size
)
200 /* For writes, we do expect the socket to be writable. */
201 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
202 } while (ret
== -EAGAIN
);
206 /* Basic flow for negotiation
233 static int nbd_send_rep(int csock
, uint32_t type
, uint32_t opt
)
238 magic
= cpu_to_be64(NBD_REP_MAGIC
);
239 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
240 LOG("write failed (rep magic)");
243 opt
= cpu_to_be32(opt
);
244 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
245 LOG("write failed (rep opt)");
248 type
= cpu_to_be32(type
);
249 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
250 LOG("write failed (rep type)");
253 len
= cpu_to_be32(0);
254 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
255 LOG("write failed (rep data length)");
261 static int nbd_send_rep_list(int csock
, NBDExport
*exp
)
263 uint64_t magic
, name_len
;
264 uint32_t opt
, type
, len
;
266 name_len
= strlen(exp
->name
);
267 magic
= cpu_to_be64(NBD_REP_MAGIC
);
268 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
269 LOG("write failed (magic)");
272 opt
= cpu_to_be32(NBD_OPT_LIST
);
273 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
274 LOG("write failed (opt)");
277 type
= cpu_to_be32(NBD_REP_SERVER
);
278 if (write_sync(csock
, &type
, sizeof(type
)) != sizeof(type
)) {
279 LOG("write failed (reply type)");
282 len
= cpu_to_be32(name_len
+ sizeof(len
));
283 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
284 LOG("write failed (length)");
287 len
= cpu_to_be32(name_len
);
288 if (write_sync(csock
, &len
, sizeof(len
)) != sizeof(len
)) {
289 LOG("write failed (length)");
292 if (write_sync(csock
, exp
->name
, name_len
) != name_len
) {
293 LOG("write failed (buffer)");
299 static int nbd_handle_list(NBDClient
*client
, uint32_t length
)
304 csock
= client
->sock
;
306 return nbd_send_rep(csock
, NBD_REP_ERR_INVALID
, NBD_OPT_LIST
);
309 /* For each export, send a NBD_REP_SERVER reply. */
310 QTAILQ_FOREACH(exp
, &exports
, next
) {
311 if (nbd_send_rep_list(csock
, exp
)) {
315 /* Finish with a NBD_REP_ACK. */
316 return nbd_send_rep(csock
, NBD_REP_ACK
, NBD_OPT_LIST
);
319 static int nbd_handle_export_name(NBDClient
*client
, uint32_t length
)
321 int rc
= -EINVAL
, csock
= client
->sock
;
325 [20 .. xx] export name (length bytes)
327 TRACE("Checking length");
329 LOG("Bad length received");
332 if (read_sync(csock
, name
, length
) != length
) {
338 client
->exp
= nbd_export_find(name
);
340 LOG("export not found");
344 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
345 nbd_export_get(client
->exp
);
351 static int nbd_receive_options(NBDClient
*client
)
353 int csock
= client
->sock
;
357 [ 0 .. 3] client flags
359 [ 0 .. 7] NBD_OPTS_MAGIC
360 [ 8 .. 11] NBD option
361 [12 .. 15] Data length
364 [ 0 .. 7] NBD_OPTS_MAGIC
365 [ 8 .. 11] Second NBD option
366 [12 .. 15] Data length
370 if (read_sync(csock
, &flags
, sizeof(flags
)) != sizeof(flags
)) {
374 TRACE("Checking client flags");
375 be32_to_cpus(&flags
);
376 if (flags
!= 0 && flags
!= NBD_FLAG_C_FIXED_NEWSTYLE
) {
377 LOG("Bad client flags received");
383 uint32_t tmp
, length
;
386 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
390 TRACE("Checking opts magic");
391 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
392 LOG("Bad magic received");
396 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
401 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
405 length
= be32_to_cpu(length
);
407 TRACE("Checking option");
408 switch (be32_to_cpu(tmp
)) {
410 ret
= nbd_handle_list(client
, length
);
419 case NBD_OPT_EXPORT_NAME
:
420 return nbd_handle_export_name(client
, length
);
423 tmp
= be32_to_cpu(tmp
);
424 LOG("Unsupported option 0x%x", tmp
);
425 nbd_send_rep(client
->sock
, NBD_REP_ERR_UNSUP
, tmp
);
431 static int nbd_send_negotiate(NBDClient
*client
)
433 int csock
= client
->sock
;
434 char buf
[8 + 8 + 8 + 128];
436 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
437 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
439 /* Negotiation header without options:
440 [ 0 .. 7] passwd ("NBDMAGIC")
441 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
443 [24 .. 25] server flags (0)
444 [26 .. 27] export flags
445 [28 .. 151] reserved (0)
447 Negotiation header with options, part 1:
448 [ 0 .. 7] passwd ("NBDMAGIC")
449 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
450 [16 .. 17] server flags (0)
452 part 2 (after options are sent):
454 [26 .. 27] export flags
455 [28 .. 151] reserved (0)
458 qemu_set_block(csock
);
461 TRACE("Beginning negotiation.");
462 memset(buf
, 0, sizeof(buf
));
463 memcpy(buf
, "NBDMAGIC", 8);
465 assert ((client
->exp
->nbdflags
& ~65535) == 0);
466 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
467 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
468 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
470 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
471 cpu_to_be16w((uint16_t *)(buf
+ 16), NBD_FLAG_FIXED_NEWSTYLE
);
475 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
480 if (write_sync(csock
, buf
, 18) != 18) {
484 rc
= nbd_receive_options(client
);
486 LOG("option negotiation failed");
490 assert ((client
->exp
->nbdflags
& ~65535) == 0);
491 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
492 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
493 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
499 TRACE("Negotiation succeeded.");
502 qemu_set_nonblock(csock
);
506 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
507 off_t
*size
, Error
**errp
)
514 TRACE("Receiving negotiation.");
518 if (read_sync(csock
, buf
, 8) != 8) {
519 error_setg(errp
, "Failed to read data");
524 if (strlen(buf
) == 0) {
525 error_setg(errp
, "Server connection closed unexpectedly");
529 TRACE("Magic is %c%c%c%c%c%c%c%c",
530 qemu_isprint(buf
[0]) ? buf
[0] : '.',
531 qemu_isprint(buf
[1]) ? buf
[1] : '.',
532 qemu_isprint(buf
[2]) ? buf
[2] : '.',
533 qemu_isprint(buf
[3]) ? buf
[3] : '.',
534 qemu_isprint(buf
[4]) ? buf
[4] : '.',
535 qemu_isprint(buf
[5]) ? buf
[5] : '.',
536 qemu_isprint(buf
[6]) ? buf
[6] : '.',
537 qemu_isprint(buf
[7]) ? buf
[7] : '.');
539 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
540 error_setg(errp
, "Invalid magic received");
544 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
545 error_setg(errp
, "Failed to read magic");
548 magic
= be64_to_cpu(magic
);
549 TRACE("Magic is 0x%" PRIx64
, magic
);
552 uint32_t reserved
= 0;
556 TRACE("Checking magic (opts_magic)");
557 if (magic
!= NBD_OPTS_MAGIC
) {
558 if (magic
== NBD_CLIENT_MAGIC
) {
559 error_setg(errp
, "Server does not support export names");
561 error_setg(errp
, "Bad magic received");
565 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
566 error_setg(errp
, "Failed to read server flags");
569 *flags
= be16_to_cpu(tmp
) << 16;
570 /* reserved for future use */
571 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
573 error_setg(errp
, "Failed to read reserved field");
576 /* write the export name */
577 magic
= cpu_to_be64(magic
);
578 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
579 error_setg(errp
, "Failed to send export name magic");
582 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
583 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
584 error_setg(errp
, "Failed to send export name option number");
587 namesize
= cpu_to_be32(strlen(name
));
588 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
590 error_setg(errp
, "Failed to send export name length");
593 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
594 error_setg(errp
, "Failed to send export name");
598 TRACE("Checking magic (cli_magic)");
600 if (magic
!= NBD_CLIENT_MAGIC
) {
601 if (magic
== NBD_OPTS_MAGIC
) {
602 error_setg(errp
, "Server requires an export name");
604 error_setg(errp
, "Bad magic received");
610 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
611 error_setg(errp
, "Failed to read export length");
614 *size
= be64_to_cpu(s
);
615 TRACE("Size is %" PRIu64
, *size
);
618 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
619 error_setg(errp
, "Failed to read export flags");
622 *flags
= be32_to_cpup(flags
);
624 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
625 error_setg(errp
, "Failed to read export flags");
628 *flags
|= be16_to_cpu(tmp
);
630 if (read_sync(csock
, &buf
, 124) != 124) {
631 error_setg(errp
, "Failed to read reserved block");
641 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
643 TRACE("Setting NBD socket");
645 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
647 LOG("Failed to set NBD socket");
651 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE
);
653 if (ioctl(fd
, NBD_SET_BLKSIZE
, (size_t)BDRV_SECTOR_SIZE
) < 0) {
655 LOG("Failed setting NBD block size");
659 TRACE("Setting size to %zd block(s)", (size_t)(size
/ BDRV_SECTOR_SIZE
));
661 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ (size_t)BDRV_SECTOR_SIZE
) < 0) {
663 LOG("Failed setting size (in blocks)");
667 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
668 if (errno
== ENOTTY
) {
669 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
670 TRACE("Setting readonly attribute");
672 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
674 LOG("Failed setting read-only attribute");
679 LOG("Failed setting flags");
684 TRACE("Negotiation ended");
689 int nbd_disconnect(int fd
)
691 ioctl(fd
, NBD_CLEAR_QUE
);
692 ioctl(fd
, NBD_DISCONNECT
);
693 ioctl(fd
, NBD_CLEAR_SOCK
);
697 int nbd_client(int fd
)
702 TRACE("Doing NBD loop");
704 ret
= ioctl(fd
, NBD_DO_IT
);
705 if (ret
< 0 && errno
== EPIPE
) {
706 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
707 * the socket via NBD_DISCONNECT. We do not want to return 1 in
714 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
716 TRACE("Clearing NBD queue");
717 ioctl(fd
, NBD_CLEAR_QUE
);
719 TRACE("Clearing NBD socket");
720 ioctl(fd
, NBD_CLEAR_SOCK
);
726 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
)
731 int nbd_disconnect(int fd
)
736 int nbd_client(int fd
)
742 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
744 uint8_t buf
[NBD_REQUEST_SIZE
];
747 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
748 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
749 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
750 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
751 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
753 TRACE("Sending request to client: "
754 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
755 request
->from
, request
->len
, request
->handle
, request
->type
);
757 ret
= write_sync(csock
, buf
, sizeof(buf
));
762 if (ret
!= sizeof(buf
)) {
763 LOG("writing to socket failed");
769 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
771 uint8_t buf
[NBD_REQUEST_SIZE
];
775 ret
= read_sync(csock
, buf
, sizeof(buf
));
780 if (ret
!= sizeof(buf
)) {
786 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
787 [ 4 .. 7] type (0 == READ, 1 == WRITE)
793 magic
= be32_to_cpup((uint32_t*)buf
);
794 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
795 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
796 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
797 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
799 TRACE("Got request: "
800 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
801 magic
, request
->type
, request
->from
, request
->len
);
803 if (magic
!= NBD_REQUEST_MAGIC
) {
804 LOG("invalid magic (got 0x%x)", magic
);
810 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
812 uint8_t buf
[NBD_REPLY_SIZE
];
816 ret
= read_sync(csock
, buf
, sizeof(buf
));
821 if (ret
!= sizeof(buf
)) {
827 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
828 [ 4 .. 7] error (0 == no error)
832 magic
= be32_to_cpup((uint32_t*)buf
);
833 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
834 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
837 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
838 magic
, reply
->error
, reply
->handle
);
840 if (magic
!= NBD_REPLY_MAGIC
) {
841 LOG("invalid magic (got 0x%x)", magic
);
847 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
849 uint8_t buf
[NBD_REPLY_SIZE
];
853 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
854 [ 4 .. 7] error (0 == no error)
857 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
858 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
859 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
861 TRACE("Sending response to client");
863 ret
= write_sync(csock
, buf
, sizeof(buf
));
868 if (ret
!= sizeof(buf
)) {
869 LOG("writing to socket failed");
875 #define MAX_NBD_REQUESTS 16
877 void nbd_client_get(NBDClient
*client
)
882 void nbd_client_put(NBDClient
*client
)
884 if (--client
->refcount
== 0) {
885 /* The last reference should be dropped by client->close,
886 * which is called by client_close.
888 assert(client
->closing
);
890 nbd_unset_handlers(client
);
894 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
895 nbd_export_put(client
->exp
);
901 static void client_close(NBDClient
*client
)
903 if (client
->closing
) {
907 client
->closing
= true;
909 /* Force requests to finish. They will drop their own references,
910 * then we'll close the socket and free the NBDClient.
912 shutdown(client
->sock
, 2);
914 /* Also tell the client, so that they release their reference. */
916 client
->close(client
);
920 static NBDRequest
*nbd_request_get(NBDClient
*client
)
924 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
925 client
->nb_requests
++;
926 nbd_update_can_read(client
);
928 req
= g_slice_new0(NBDRequest
);
929 nbd_client_get(client
);
930 req
->client
= client
;
934 static void nbd_request_put(NBDRequest
*req
)
936 NBDClient
*client
= req
->client
;
939 qemu_vfree(req
->data
);
941 g_slice_free(NBDRequest
, req
);
943 client
->nb_requests
--;
944 nbd_update_can_read(client
);
945 nbd_client_put(client
);
948 static void blk_aio_attached(AioContext
*ctx
, void *opaque
)
950 NBDExport
*exp
= opaque
;
953 TRACE("Export %s: Attaching clients to AIO context %p\n", exp
->name
, ctx
);
957 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
958 nbd_set_handlers(client
);
962 static void blk_aio_detach(void *opaque
)
964 NBDExport
*exp
= opaque
;
967 TRACE("Export %s: Detaching clients from AIO context %p\n", exp
->name
, exp
->ctx
);
969 QTAILQ_FOREACH(client
, &exp
->clients
, next
) {
970 nbd_unset_handlers(client
);
976 NBDExport
*nbd_export_new(BlockBackend
*blk
, off_t dev_offset
, off_t size
,
977 uint32_t nbdflags
, void (*close
)(NBDExport
*),
980 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
982 QTAILQ_INIT(&exp
->clients
);
984 exp
->dev_offset
= dev_offset
;
985 exp
->nbdflags
= nbdflags
;
986 exp
->size
= size
< 0 ? blk_getlength(blk
) : size
;
988 error_setg_errno(errp
, -exp
->size
,
989 "Failed to determine the NBD export's length");
992 exp
->size
-= exp
->size
% BDRV_SECTOR_SIZE
;
995 exp
->ctx
= blk_get_aio_context(blk
);
997 blk_add_aio_context_notifier(blk
, blk_aio_attached
, blk_aio_detach
, exp
);
999 * NBD exports are used for non-shared storage migration. Make sure
1000 * that BDRV_O_INCOMING is cleared and the image is ready for write
1001 * access since the export could be available before migration handover.
1003 blk_invalidate_cache(blk
, NULL
);
1011 NBDExport
*nbd_export_find(const char *name
)
1014 QTAILQ_FOREACH(exp
, &exports
, next
) {
1015 if (strcmp(name
, exp
->name
) == 0) {
1023 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
1025 if (exp
->name
== name
) {
1029 nbd_export_get(exp
);
1030 if (exp
->name
!= NULL
) {
1033 QTAILQ_REMOVE(&exports
, exp
, next
);
1034 nbd_export_put(exp
);
1037 nbd_export_get(exp
);
1038 exp
->name
= g_strdup(name
);
1039 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
1041 nbd_export_put(exp
);
1044 void nbd_export_close(NBDExport
*exp
)
1046 NBDClient
*client
, *next
;
1048 nbd_export_get(exp
);
1049 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
1050 client_close(client
);
1052 nbd_export_set_name(exp
, NULL
);
1053 nbd_export_put(exp
);
1055 blk_remove_aio_context_notifier(exp
->blk
, blk_aio_attached
,
1056 blk_aio_detach
, exp
);
1057 blk_unref(exp
->blk
);
1062 void nbd_export_get(NBDExport
*exp
)
1064 assert(exp
->refcount
> 0);
1068 void nbd_export_put(NBDExport
*exp
)
1070 assert(exp
->refcount
> 0);
1071 if (exp
->refcount
== 1) {
1072 nbd_export_close(exp
);
1075 if (--exp
->refcount
== 0) {
1076 assert(exp
->name
== NULL
);
1086 BlockBackend
*nbd_export_get_blockdev(NBDExport
*exp
)
1091 void nbd_export_close_all(void)
1093 NBDExport
*exp
, *next
;
1095 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
1096 nbd_export_close(exp
);
1100 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
1103 NBDClient
*client
= req
->client
;
1104 int csock
= client
->sock
;
1107 qemu_co_mutex_lock(&client
->send_lock
);
1108 client
->send_coroutine
= qemu_coroutine_self();
1109 nbd_set_handlers(client
);
1112 rc
= nbd_send_reply(csock
, reply
);
1114 socket_set_cork(csock
, 1);
1115 rc
= nbd_send_reply(csock
, reply
);
1117 ret
= qemu_co_send(csock
, req
->data
, len
);
1122 socket_set_cork(csock
, 0);
1125 client
->send_coroutine
= NULL
;
1126 nbd_set_handlers(client
);
1127 qemu_co_mutex_unlock(&client
->send_lock
);
1131 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
1133 NBDClient
*client
= req
->client
;
1134 int csock
= client
->sock
;
1138 client
->recv_coroutine
= qemu_coroutine_self();
1139 nbd_update_can_read(client
);
1141 rc
= nbd_receive_request(csock
, request
);
1143 if (rc
!= -EAGAIN
) {
1149 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
1150 LOG("len (%u) is larger than max len (%u)",
1151 request
->len
, NBD_MAX_BUFFER_SIZE
);
1156 if ((request
->from
+ request
->len
) < request
->from
) {
1157 LOG("integer overflow detected! "
1158 "you're probably being attacked");
1163 TRACE("Decoding type");
1165 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
1166 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
1167 req
->data
= blk_blockalign(client
->exp
->blk
, request
->len
);
1169 if (command
== NBD_CMD_WRITE
) {
1170 TRACE("Reading %u byte(s)", request
->len
);
1172 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
1173 LOG("reading from socket failed");
1181 client
->recv_coroutine
= NULL
;
1182 nbd_update_can_read(client
);
1187 static void nbd_trip(void *opaque
)
1189 NBDClient
*client
= opaque
;
1190 NBDExport
*exp
= client
->exp
;
1192 struct nbd_request request
;
1193 struct nbd_reply reply
;
1197 TRACE("Reading request.");
1198 if (client
->closing
) {
1202 req
= nbd_request_get(client
);
1203 ret
= nbd_co_receive_request(req
, &request
);
1204 if (ret
== -EAGAIN
) {
1211 reply
.handle
= request
.handle
;
1218 command
= request
.type
& NBD_CMD_MASK_COMMAND
;
1219 if (command
!= NBD_CMD_DISC
&& (request
.from
+ request
.len
) > exp
->size
) {
1220 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1221 ", Offset: %" PRIu64
"\n",
1222 request
.from
, request
.len
,
1223 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1224 LOG("requested operation past EOF--bad client?");
1225 goto invalid_request
;
1230 TRACE("Request type is READ");
1232 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1233 ret
= blk_co_flush(exp
->blk
);
1235 LOG("flush failed");
1241 ret
= blk_read(exp
->blk
,
1242 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1243 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1245 LOG("reading from file failed");
1250 TRACE("Read %u byte(s)", request
.len
);
1251 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1255 TRACE("Request type is WRITE");
1257 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1258 TRACE("Server is read-only, return error");
1259 reply
.error
= EROFS
;
1263 TRACE("Writing to device");
1265 ret
= blk_write(exp
->blk
,
1266 (request
.from
+ exp
->dev_offset
) / BDRV_SECTOR_SIZE
,
1267 req
->data
, request
.len
/ BDRV_SECTOR_SIZE
);
1269 LOG("writing to file failed");
1274 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1275 ret
= blk_co_flush(exp
->blk
);
1277 LOG("flush failed");
1283 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1288 TRACE("Request type is DISCONNECT");
1292 TRACE("Request type is FLUSH");
1294 ret
= blk_co_flush(exp
->blk
);
1296 LOG("flush failed");
1299 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1304 TRACE("Request type is TRIM");
1305 ret
= blk_co_discard(exp
->blk
, (request
.from
+ exp
->dev_offset
)
1307 request
.len
/ BDRV_SECTOR_SIZE
);
1309 LOG("discard failed");
1312 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1317 LOG("invalid request type (%u) received", request
.type
);
1319 reply
.error
= EINVAL
;
1321 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1327 TRACE("Request/Reply complete");
1330 nbd_request_put(req
);
1334 nbd_request_put(req
);
1335 client_close(client
);
1338 static void nbd_read(void *opaque
)
1340 NBDClient
*client
= opaque
;
1342 if (client
->recv_coroutine
) {
1343 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1345 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1349 static void nbd_restart_write(void *opaque
)
1351 NBDClient
*client
= opaque
;
1353 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1356 static void nbd_set_handlers(NBDClient
*client
)
1358 if (client
->exp
&& client
->exp
->ctx
) {
1359 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
,
1360 client
->can_read
? nbd_read
: NULL
,
1361 client
->send_coroutine
? nbd_restart_write
: NULL
,
1366 static void nbd_unset_handlers(NBDClient
*client
)
1368 if (client
->exp
&& client
->exp
->ctx
) {
1369 aio_set_fd_handler(client
->exp
->ctx
, client
->sock
, NULL
, NULL
, NULL
);
1373 static void nbd_update_can_read(NBDClient
*client
)
1375 bool can_read
= client
->recv_coroutine
||
1376 client
->nb_requests
< MAX_NBD_REQUESTS
;
1378 if (can_read
!= client
->can_read
) {
1379 client
->can_read
= can_read
;
1380 nbd_set_handlers(client
);
1382 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1383 * in nbd_set_handlers() will have taken care of that */
1387 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1388 void (*close
)(NBDClient
*))
1391 client
= g_malloc0(sizeof(NBDClient
));
1392 client
->refcount
= 1;
1394 client
->sock
= csock
;
1395 client
->can_read
= true;
1396 if (nbd_send_negotiate(client
)) {
1400 client
->close
= close
;
1401 qemu_co_mutex_init(&client
->send_lock
);
1402 nbd_set_handlers(client
);
1405 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1406 nbd_export_get(exp
);