]>
git.proxmox.com Git - mirror_qemu.git/blob - nbd.c
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "block/nbd.h"
20 #include "block/block.h"
22 #include "block/coroutine.h"
27 #include <sys/ioctl.h>
29 #if defined(__sun__) || defined(__HAIKU__)
30 #include <sys/ioccom.h>
39 #include "qemu/sockets.h"
40 #include "qemu/queue.h"
41 #include "qemu/main-loop.h"
46 #define TRACE(msg, ...) do { \
47 LOG(msg, ## __VA_ARGS__); \
50 #define TRACE(msg, ...) \
54 #define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
59 /* This is all part of the "official" NBD API */
61 #define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
62 #define NBD_REPLY_SIZE (4 + 4 + 8)
63 #define NBD_REQUEST_MAGIC 0x25609513
64 #define NBD_REPLY_MAGIC 0x67446698
65 #define NBD_OPTS_MAGIC 0x49484156454F5054LL
66 #define NBD_CLIENT_MAGIC 0x0000420281861253LL
68 #define NBD_SET_SOCK _IO(0xab, 0)
69 #define NBD_SET_BLKSIZE _IO(0xab, 1)
70 #define NBD_SET_SIZE _IO(0xab, 2)
71 #define NBD_DO_IT _IO(0xab, 3)
72 #define NBD_CLEAR_SOCK _IO(0xab, 4)
73 #define NBD_CLEAR_QUE _IO(0xab, 5)
74 #define NBD_PRINT_DEBUG _IO(0xab, 6)
75 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
76 #define NBD_DISCONNECT _IO(0xab, 8)
77 #define NBD_SET_TIMEOUT _IO(0xab, 9)
78 #define NBD_SET_FLAGS _IO(0xab, 10)
80 #define NBD_OPT_EXPORT_NAME (1 << 0)
82 /* Definitions for opaque data types */
84 typedef struct NBDRequest NBDRequest
;
87 QSIMPLEQ_ENTRY(NBDRequest
) entry
;
94 void (*close
)(NBDExport
*exp
);
101 QTAILQ_HEAD(, NBDClient
) clients
;
102 QTAILQ_ENTRY(NBDExport
) next
;
105 static QTAILQ_HEAD(, NBDExport
) exports
= QTAILQ_HEAD_INITIALIZER(exports
);
109 void (*close
)(NBDClient
*client
);
114 Coroutine
*recv_coroutine
;
117 Coroutine
*send_coroutine
;
119 QTAILQ_ENTRY(NBDClient
) next
;
124 /* That's all folks */
126 ssize_t
nbd_wr_sync(int fd
, void *buffer
, size_t size
, bool do_read
)
131 if (qemu_in_coroutine()) {
133 return qemu_co_recv(fd
, buffer
, size
);
135 return qemu_co_send(fd
, buffer
, size
);
139 while (offset
< size
) {
143 len
= qemu_recv(fd
, buffer
+ offset
, size
- offset
, 0);
145 len
= send(fd
, buffer
+ offset
, size
- offset
, 0);
149 err
= socket_error();
151 /* recoverable error */
152 if (err
== EINTR
|| (offset
> 0 && err
== EAGAIN
)) {
156 /* unrecoverable error */
171 static ssize_t
read_sync(int fd
, void *buffer
, size_t size
)
173 /* Sockets are kept in blocking mode in the negotiation phase. After
174 * that, a non-readable socket simply means that another thread stole
175 * our request/reply. Synchronization is done with recv_coroutine, so
176 * that this is coroutine-safe.
178 return nbd_wr_sync(fd
, buffer
, size
, true);
181 static ssize_t
write_sync(int fd
, void *buffer
, size_t size
)
185 /* For writes, we do expect the socket to be writable. */
186 ret
= nbd_wr_sync(fd
, buffer
, size
, false);
187 } while (ret
== -EAGAIN
);
191 /* Basic flow for negotiation
218 static int nbd_receive_options(NBDClient
*client
)
220 int csock
= client
->sock
;
222 uint32_t tmp
, length
;
227 [ 0 .. 3] reserved (0)
228 [ 4 .. 11] NBD_OPTS_MAGIC
229 [12 .. 15] NBD_OPT_EXPORT_NAME
231 [20 .. xx] export name (length bytes)
235 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
239 TRACE("Checking reserved");
241 LOG("Bad reserved received");
245 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
249 TRACE("Checking reserved");
250 if (magic
!= be64_to_cpu(NBD_OPTS_MAGIC
)) {
251 LOG("Bad magic received");
255 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
259 TRACE("Checking option");
260 if (tmp
!= be32_to_cpu(NBD_OPT_EXPORT_NAME
)) {
261 LOG("Bad option received");
265 if (read_sync(csock
, &length
, sizeof(length
)) != sizeof(length
)) {
269 TRACE("Checking length");
270 length
= be32_to_cpu(length
);
272 LOG("Bad length received");
275 if (read_sync(csock
, name
, length
) != length
) {
281 client
->exp
= nbd_export_find(name
);
283 LOG("export not found");
287 QTAILQ_INSERT_TAIL(&client
->exp
->clients
, client
, next
);
288 nbd_export_get(client
->exp
);
290 TRACE("Option negotiation succeeded.");
296 static int nbd_send_negotiate(NBDClient
*client
)
298 int csock
= client
->sock
;
299 char buf
[8 + 8 + 8 + 128];
301 const int myflags
= (NBD_FLAG_HAS_FLAGS
| NBD_FLAG_SEND_TRIM
|
302 NBD_FLAG_SEND_FLUSH
| NBD_FLAG_SEND_FUA
);
304 /* Negotiation header without options:
305 [ 0 .. 7] passwd ("NBDMAGIC")
306 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
308 [24 .. 25] server flags (0)
309 [26 .. 27] export flags
310 [28 .. 151] reserved (0)
312 Negotiation header with options, part 1:
313 [ 0 .. 7] passwd ("NBDMAGIC")
314 [ 8 .. 15] magic (NBD_OPTS_MAGIC)
315 [16 .. 17] server flags (0)
317 part 2 (after options are sent):
319 [26 .. 27] export flags
320 [28 .. 151] reserved (0)
323 qemu_set_block(csock
);
326 TRACE("Beginning negotiation.");
327 memset(buf
, 0, sizeof(buf
));
328 memcpy(buf
, "NBDMAGIC", 8);
330 assert ((client
->exp
->nbdflags
& ~65535) == 0);
331 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_CLIENT_MAGIC
);
332 cpu_to_be64w((uint64_t*)(buf
+ 16), client
->exp
->size
);
333 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
335 cpu_to_be64w((uint64_t*)(buf
+ 8), NBD_OPTS_MAGIC
);
339 if (write_sync(csock
, buf
, sizeof(buf
)) != sizeof(buf
)) {
344 if (write_sync(csock
, buf
, 18) != 18) {
348 rc
= nbd_receive_options(client
);
350 LOG("option negotiation failed");
354 assert ((client
->exp
->nbdflags
& ~65535) == 0);
355 cpu_to_be64w((uint64_t*)(buf
+ 18), client
->exp
->size
);
356 cpu_to_be16w((uint16_t*)(buf
+ 26), client
->exp
->nbdflags
| myflags
);
357 if (write_sync(csock
, buf
+ 18, sizeof(buf
) - 18) != sizeof(buf
) - 18) {
363 TRACE("Negotiation succeeded.");
366 qemu_set_nonblock(csock
);
370 int nbd_receive_negotiate(int csock
, const char *name
, uint32_t *flags
,
371 off_t
*size
, size_t *blocksize
)
378 TRACE("Receiving negotiation.");
382 if (read_sync(csock
, buf
, 8) != 8) {
388 if (strlen(buf
) == 0) {
389 LOG("server connection closed");
393 TRACE("Magic is %c%c%c%c%c%c%c%c",
394 qemu_isprint(buf
[0]) ? buf
[0] : '.',
395 qemu_isprint(buf
[1]) ? buf
[1] : '.',
396 qemu_isprint(buf
[2]) ? buf
[2] : '.',
397 qemu_isprint(buf
[3]) ? buf
[3] : '.',
398 qemu_isprint(buf
[4]) ? buf
[4] : '.',
399 qemu_isprint(buf
[5]) ? buf
[5] : '.',
400 qemu_isprint(buf
[6]) ? buf
[6] : '.',
401 qemu_isprint(buf
[7]) ? buf
[7] : '.');
403 if (memcmp(buf
, "NBDMAGIC", 8) != 0) {
404 LOG("Invalid magic received");
408 if (read_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
412 magic
= be64_to_cpu(magic
);
413 TRACE("Magic is 0x%" PRIx64
, magic
);
416 uint32_t reserved
= 0;
420 TRACE("Checking magic (opts_magic)");
421 if (magic
!= NBD_OPTS_MAGIC
) {
422 LOG("Bad magic received");
425 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
426 LOG("flags read failed");
429 *flags
= be16_to_cpu(tmp
) << 16;
430 /* reserved for future use */
431 if (write_sync(csock
, &reserved
, sizeof(reserved
)) !=
433 LOG("write failed (reserved)");
436 /* write the export name */
437 magic
= cpu_to_be64(magic
);
438 if (write_sync(csock
, &magic
, sizeof(magic
)) != sizeof(magic
)) {
439 LOG("write failed (magic)");
442 opt
= cpu_to_be32(NBD_OPT_EXPORT_NAME
);
443 if (write_sync(csock
, &opt
, sizeof(opt
)) != sizeof(opt
)) {
444 LOG("write failed (opt)");
447 namesize
= cpu_to_be32(strlen(name
));
448 if (write_sync(csock
, &namesize
, sizeof(namesize
)) !=
450 LOG("write failed (namesize)");
453 if (write_sync(csock
, (char*)name
, strlen(name
)) != strlen(name
)) {
454 LOG("write failed (name)");
458 TRACE("Checking magic (cli_magic)");
460 if (magic
!= NBD_CLIENT_MAGIC
) {
461 LOG("Bad magic received");
466 if (read_sync(csock
, &s
, sizeof(s
)) != sizeof(s
)) {
470 *size
= be64_to_cpu(s
);
472 TRACE("Size is %" PRIu64
, *size
);
475 if (read_sync(csock
, flags
, sizeof(*flags
)) != sizeof(*flags
)) {
476 LOG("read failed (flags)");
479 *flags
= be32_to_cpup(flags
);
481 if (read_sync(csock
, &tmp
, sizeof(tmp
)) != sizeof(tmp
)) {
482 LOG("read failed (tmp)");
485 *flags
|= be32_to_cpu(tmp
);
487 if (read_sync(csock
, &buf
, 124) != 124) {
488 LOG("read failed (buf)");
498 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
500 TRACE("Setting NBD socket");
502 if (ioctl(fd
, NBD_SET_SOCK
, csock
) < 0) {
504 LOG("Failed to set NBD socket");
508 TRACE("Setting block size to %lu", (unsigned long)blocksize
);
510 if (ioctl(fd
, NBD_SET_BLKSIZE
, blocksize
) < 0) {
512 LOG("Failed setting NBD block size");
516 TRACE("Setting size to %zd block(s)", (size_t)(size
/ blocksize
));
518 if (ioctl(fd
, NBD_SET_SIZE_BLOCKS
, size
/ blocksize
) < 0) {
520 LOG("Failed setting size (in blocks)");
524 if (ioctl(fd
, NBD_SET_FLAGS
, flags
) < 0) {
525 if (errno
== ENOTTY
) {
526 int read_only
= (flags
& NBD_FLAG_READ_ONLY
) != 0;
527 TRACE("Setting readonly attribute");
529 if (ioctl(fd
, BLKROSET
, (unsigned long) &read_only
) < 0) {
531 LOG("Failed setting read-only attribute");
536 LOG("Failed setting flags");
541 TRACE("Negotiation ended");
546 int nbd_disconnect(int fd
)
548 ioctl(fd
, NBD_CLEAR_QUE
);
549 ioctl(fd
, NBD_DISCONNECT
);
550 ioctl(fd
, NBD_CLEAR_SOCK
);
554 int nbd_client(int fd
)
559 TRACE("Doing NBD loop");
561 ret
= ioctl(fd
, NBD_DO_IT
);
562 if (ret
< 0 && errno
== EPIPE
) {
563 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
564 * the socket via NBD_DISCONNECT. We do not want to return 1 in
571 TRACE("NBD loop returned %d: %s", ret
, strerror(serrno
));
573 TRACE("Clearing NBD queue");
574 ioctl(fd
, NBD_CLEAR_QUE
);
576 TRACE("Clearing NBD socket");
577 ioctl(fd
, NBD_CLEAR_SOCK
);
583 int nbd_init(int fd
, int csock
, uint32_t flags
, off_t size
, size_t blocksize
)
588 int nbd_disconnect(int fd
)
593 int nbd_client(int fd
)
599 ssize_t
nbd_send_request(int csock
, struct nbd_request
*request
)
601 uint8_t buf
[NBD_REQUEST_SIZE
];
604 cpu_to_be32w((uint32_t*)buf
, NBD_REQUEST_MAGIC
);
605 cpu_to_be32w((uint32_t*)(buf
+ 4), request
->type
);
606 cpu_to_be64w((uint64_t*)(buf
+ 8), request
->handle
);
607 cpu_to_be64w((uint64_t*)(buf
+ 16), request
->from
);
608 cpu_to_be32w((uint32_t*)(buf
+ 24), request
->len
);
610 TRACE("Sending request to client: "
611 "{ .from = %" PRIu64
", .len = %u, .handle = %" PRIu64
", .type=%i}",
612 request
->from
, request
->len
, request
->handle
, request
->type
);
614 ret
= write_sync(csock
, buf
, sizeof(buf
));
619 if (ret
!= sizeof(buf
)) {
620 LOG("writing to socket failed");
626 static ssize_t
nbd_receive_request(int csock
, struct nbd_request
*request
)
628 uint8_t buf
[NBD_REQUEST_SIZE
];
632 ret
= read_sync(csock
, buf
, sizeof(buf
));
637 if (ret
!= sizeof(buf
)) {
643 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
644 [ 4 .. 7] type (0 == READ, 1 == WRITE)
650 magic
= be32_to_cpup((uint32_t*)buf
);
651 request
->type
= be32_to_cpup((uint32_t*)(buf
+ 4));
652 request
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
653 request
->from
= be64_to_cpup((uint64_t*)(buf
+ 16));
654 request
->len
= be32_to_cpup((uint32_t*)(buf
+ 24));
656 TRACE("Got request: "
657 "{ magic = 0x%x, .type = %d, from = %" PRIu64
" , len = %u }",
658 magic
, request
->type
, request
->from
, request
->len
);
660 if (magic
!= NBD_REQUEST_MAGIC
) {
661 LOG("invalid magic (got 0x%x)", magic
);
667 ssize_t
nbd_receive_reply(int csock
, struct nbd_reply
*reply
)
669 uint8_t buf
[NBD_REPLY_SIZE
];
673 ret
= read_sync(csock
, buf
, sizeof(buf
));
678 if (ret
!= sizeof(buf
)) {
684 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
685 [ 4 .. 7] error (0 == no error)
689 magic
= be32_to_cpup((uint32_t*)buf
);
690 reply
->error
= be32_to_cpup((uint32_t*)(buf
+ 4));
691 reply
->handle
= be64_to_cpup((uint64_t*)(buf
+ 8));
694 "{ magic = 0x%x, .error = %d, handle = %" PRIu64
" }",
695 magic
, reply
->error
, reply
->handle
);
697 if (magic
!= NBD_REPLY_MAGIC
) {
698 LOG("invalid magic (got 0x%x)", magic
);
704 static ssize_t
nbd_send_reply(int csock
, struct nbd_reply
*reply
)
706 uint8_t buf
[NBD_REPLY_SIZE
];
710 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
711 [ 4 .. 7] error (0 == no error)
714 cpu_to_be32w((uint32_t*)buf
, NBD_REPLY_MAGIC
);
715 cpu_to_be32w((uint32_t*)(buf
+ 4), reply
->error
);
716 cpu_to_be64w((uint64_t*)(buf
+ 8), reply
->handle
);
718 TRACE("Sending response to client");
720 ret
= write_sync(csock
, buf
, sizeof(buf
));
725 if (ret
!= sizeof(buf
)) {
726 LOG("writing to socket failed");
732 #define MAX_NBD_REQUESTS 16
734 void nbd_client_get(NBDClient
*client
)
739 void nbd_client_put(NBDClient
*client
)
741 if (--client
->refcount
== 0) {
742 /* The last reference should be dropped by client->close,
743 * which is called by nbd_client_close.
745 assert(client
->closing
);
747 qemu_set_fd_handler2(client
->sock
, NULL
, NULL
, NULL
, NULL
);
751 QTAILQ_REMOVE(&client
->exp
->clients
, client
, next
);
752 nbd_export_put(client
->exp
);
758 void nbd_client_close(NBDClient
*client
)
760 if (client
->closing
) {
764 client
->closing
= true;
766 /* Force requests to finish. They will drop their own references,
767 * then we'll close the socket and free the NBDClient.
769 shutdown(client
->sock
, 2);
771 /* Also tell the client, so that they release their reference. */
773 client
->close(client
);
777 static NBDRequest
*nbd_request_get(NBDClient
*client
)
781 assert(client
->nb_requests
<= MAX_NBD_REQUESTS
- 1);
782 client
->nb_requests
++;
784 req
= g_slice_new0(NBDRequest
);
785 nbd_client_get(client
);
786 req
->client
= client
;
790 static void nbd_request_put(NBDRequest
*req
)
792 NBDClient
*client
= req
->client
;
795 qemu_vfree(req
->data
);
797 g_slice_free(NBDRequest
, req
);
799 if (client
->nb_requests
-- == MAX_NBD_REQUESTS
) {
802 nbd_client_put(client
);
805 NBDExport
*nbd_export_new(BlockDriverState
*bs
, off_t dev_offset
,
806 off_t size
, uint32_t nbdflags
,
807 void (*close
)(NBDExport
*))
809 NBDExport
*exp
= g_malloc0(sizeof(NBDExport
));
811 QTAILQ_INIT(&exp
->clients
);
813 exp
->dev_offset
= dev_offset
;
814 exp
->nbdflags
= nbdflags
;
815 exp
->size
= size
== -1 ? bdrv_getlength(bs
) : size
;
821 NBDExport
*nbd_export_find(const char *name
)
824 QTAILQ_FOREACH(exp
, &exports
, next
) {
825 if (strcmp(name
, exp
->name
) == 0) {
833 void nbd_export_set_name(NBDExport
*exp
, const char *name
)
835 if (exp
->name
== name
) {
840 if (exp
->name
!= NULL
) {
843 QTAILQ_REMOVE(&exports
, exp
, next
);
848 exp
->name
= g_strdup(name
);
849 QTAILQ_INSERT_TAIL(&exports
, exp
, next
);
854 void nbd_export_close(NBDExport
*exp
)
856 NBDClient
*client
, *next
;
859 QTAILQ_FOREACH_SAFE(client
, &exp
->clients
, next
, next
) {
860 nbd_client_close(client
);
862 nbd_export_set_name(exp
, NULL
);
870 void nbd_export_get(NBDExport
*exp
)
872 assert(exp
->refcount
> 0);
876 void nbd_export_put(NBDExport
*exp
)
878 assert(exp
->refcount
> 0);
879 if (exp
->refcount
== 1) {
880 nbd_export_close(exp
);
883 if (--exp
->refcount
== 0) {
884 assert(exp
->name
== NULL
);
894 BlockDriverState
*nbd_export_get_blockdev(NBDExport
*exp
)
899 void nbd_export_close_all(void)
901 NBDExport
*exp
, *next
;
903 QTAILQ_FOREACH_SAFE(exp
, &exports
, next
, next
) {
904 nbd_export_close(exp
);
908 static int nbd_can_read(void *opaque
);
909 static void nbd_read(void *opaque
);
910 static void nbd_restart_write(void *opaque
);
912 static ssize_t
nbd_co_send_reply(NBDRequest
*req
, struct nbd_reply
*reply
,
915 NBDClient
*client
= req
->client
;
916 int csock
= client
->sock
;
919 qemu_co_mutex_lock(&client
->send_lock
);
920 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
,
921 nbd_restart_write
, client
);
922 client
->send_coroutine
= qemu_coroutine_self();
925 rc
= nbd_send_reply(csock
, reply
);
927 socket_set_cork(csock
, 1);
928 rc
= nbd_send_reply(csock
, reply
);
930 ret
= qemu_co_send(csock
, req
->data
, len
);
935 socket_set_cork(csock
, 0);
938 client
->send_coroutine
= NULL
;
939 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
940 qemu_co_mutex_unlock(&client
->send_lock
);
944 static ssize_t
nbd_co_receive_request(NBDRequest
*req
, struct nbd_request
*request
)
946 NBDClient
*client
= req
->client
;
947 int csock
= client
->sock
;
951 client
->recv_coroutine
= qemu_coroutine_self();
952 rc
= nbd_receive_request(csock
, request
);
960 if (request
->len
> NBD_MAX_BUFFER_SIZE
) {
961 LOG("len (%u) is larger than max len (%u)",
962 request
->len
, NBD_MAX_BUFFER_SIZE
);
967 if ((request
->from
+ request
->len
) < request
->from
) {
968 LOG("integer overflow detected! "
969 "you're probably being attacked");
974 TRACE("Decoding type");
976 command
= request
->type
& NBD_CMD_MASK_COMMAND
;
977 if (command
== NBD_CMD_READ
|| command
== NBD_CMD_WRITE
) {
978 req
->data
= qemu_blockalign(client
->exp
->bs
, request
->len
);
980 if (command
== NBD_CMD_WRITE
) {
981 TRACE("Reading %u byte(s)", request
->len
);
983 if (qemu_co_recv(csock
, req
->data
, request
->len
) != request
->len
) {
984 LOG("reading from socket failed");
992 client
->recv_coroutine
= NULL
;
996 static void nbd_trip(void *opaque
)
998 NBDClient
*client
= opaque
;
999 NBDExport
*exp
= client
->exp
;
1001 struct nbd_request request
;
1002 struct nbd_reply reply
;
1005 TRACE("Reading request.");
1006 if (client
->closing
) {
1010 req
= nbd_request_get(client
);
1011 ret
= nbd_co_receive_request(req
, &request
);
1012 if (ret
== -EAGAIN
) {
1019 reply
.handle
= request
.handle
;
1027 if ((request
.from
+ request
.len
) > exp
->size
) {
1028 LOG("From: %" PRIu64
", Len: %u, Size: %" PRIu64
1029 ", Offset: %" PRIu64
"\n",
1030 request
.from
, request
.len
,
1031 (uint64_t)exp
->size
, (uint64_t)exp
->dev_offset
);
1032 LOG("requested operation past EOF--bad client?");
1033 goto invalid_request
;
1036 switch (request
.type
& NBD_CMD_MASK_COMMAND
) {
1038 TRACE("Request type is READ");
1040 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1041 ret
= bdrv_co_flush(exp
->bs
);
1043 LOG("flush failed");
1049 ret
= bdrv_read(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1050 req
->data
, request
.len
/ 512);
1052 LOG("reading from file failed");
1057 TRACE("Read %u byte(s)", request
.len
);
1058 if (nbd_co_send_reply(req
, &reply
, request
.len
) < 0)
1062 TRACE("Request type is WRITE");
1064 if (exp
->nbdflags
& NBD_FLAG_READ_ONLY
) {
1065 TRACE("Server is read-only, return error");
1066 reply
.error
= EROFS
;
1070 TRACE("Writing to device");
1072 ret
= bdrv_write(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1073 req
->data
, request
.len
/ 512);
1075 LOG("writing to file failed");
1080 if (request
.type
& NBD_CMD_FLAG_FUA
) {
1081 ret
= bdrv_co_flush(exp
->bs
);
1083 LOG("flush failed");
1089 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1094 TRACE("Request type is DISCONNECT");
1098 TRACE("Request type is FLUSH");
1100 ret
= bdrv_co_flush(exp
->bs
);
1102 LOG("flush failed");
1105 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1110 TRACE("Request type is TRIM");
1111 ret
= bdrv_co_discard(exp
->bs
, (request
.from
+ exp
->dev_offset
) / 512,
1114 LOG("discard failed");
1117 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1122 LOG("invalid request type (%u) received", request
.type
);
1124 reply
.error
= -EINVAL
;
1126 if (nbd_co_send_reply(req
, &reply
, 0) < 0) {
1132 TRACE("Request/Reply complete");
1135 nbd_request_put(req
);
1139 nbd_request_put(req
);
1140 nbd_client_close(client
);
1143 static int nbd_can_read(void *opaque
)
1145 NBDClient
*client
= opaque
;
1147 return client
->recv_coroutine
|| client
->nb_requests
< MAX_NBD_REQUESTS
;
1150 static void nbd_read(void *opaque
)
1152 NBDClient
*client
= opaque
;
1154 if (client
->recv_coroutine
) {
1155 qemu_coroutine_enter(client
->recv_coroutine
, NULL
);
1157 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip
), client
);
1161 static void nbd_restart_write(void *opaque
)
1163 NBDClient
*client
= opaque
;
1165 qemu_coroutine_enter(client
->send_coroutine
, NULL
);
1168 NBDClient
*nbd_client_new(NBDExport
*exp
, int csock
,
1169 void (*close
)(NBDClient
*))
1172 client
= g_malloc0(sizeof(NBDClient
));
1173 client
->refcount
= 1;
1175 client
->sock
= csock
;
1176 if (nbd_send_negotiate(client
) < 0) {
1180 client
->close
= close
;
1181 qemu_co_mutex_init(&client
->send_lock
);
1182 qemu_set_fd_handler2(csock
, nbd_can_read
, nbd_read
, NULL
, client
);
1185 QTAILQ_INSERT_TAIL(&exp
->clients
, client
, next
);
1186 nbd_export_get(exp
);