]> git.proxmox.com Git - qemu.git/blame - nbd.c
nbd: do not block in nbd_wr_sync if no data at all is available
[qemu.git] / nbd.c
CommitLineData
75818250 1/*
7a5ca864
FB
2 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
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.
9 *
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.
14 *
15 * You should have received a copy of the GNU General Public License
8167ee88 16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
75818250 17 */
7a5ca864
FB
18
19#include "nbd.h"
ab359cd1 20#include "block.h"
af49bbbe 21#include "block_int.h"
7a5ca864 22
262db388
PB
23#include "qemu-coroutine.h"
24
7a5ca864
FB
25#include <errno.h>
26#include <string.h>
03ff3ca3 27#ifndef _WIN32
7a5ca864 28#include <sys/ioctl.h>
03ff3ca3 29#endif
5dc2eec9 30#if defined(__sun__) || defined(__HAIKU__)
7e00eb9b
AL
31#include <sys/ioccom.h>
32#endif
7a5ca864
FB
33#include <ctype.h>
34#include <inttypes.h>
75818250 35
b90fb4b8
PB
36#ifdef __linux__
37#include <linux/fs.h>
38#endif
39
03ff3ca3 40#include "qemu_socket.h"
d9a73806 41#include "qemu-queue.h"
03ff3ca3
AL
42
43//#define DEBUG_NBD
44
45#ifdef DEBUG_NBD
75818250 46#define TRACE(msg, ...) do { \
03ff3ca3 47 LOG(msg, ## __VA_ARGS__); \
75818250 48} while(0)
03ff3ca3
AL
49#else
50#define TRACE(msg, ...) \
51 do { } while (0)
52#endif
7a5ca864
FB
53
54#define LOG(msg, ...) do { \
55 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
56 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
57} while(0)
58
7a5ca864
FB
59/* This is all part of the "official" NBD API */
60
b2e3d87f 61#define NBD_REPLY_SIZE (4 + 4 + 8)
7a5ca864
FB
62#define NBD_REQUEST_MAGIC 0x25609513
63#define NBD_REPLY_MAGIC 0x67446698
64
65#define NBD_SET_SOCK _IO(0xab, 0)
66#define NBD_SET_BLKSIZE _IO(0xab, 1)
67#define NBD_SET_SIZE _IO(0xab, 2)
68#define NBD_DO_IT _IO(0xab, 3)
69#define NBD_CLEAR_SOCK _IO(0xab, 4)
70#define NBD_CLEAR_QUE _IO(0xab, 5)
b2e3d87f
NT
71#define NBD_PRINT_DEBUG _IO(0xab, 6)
72#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
7a5ca864 73#define NBD_DISCONNECT _IO(0xab, 8)
bbb74edd
PB
74#define NBD_SET_TIMEOUT _IO(0xab, 9)
75#define NBD_SET_FLAGS _IO(0xab, 10)
7a5ca864 76
b2e3d87f 77#define NBD_OPT_EXPORT_NAME (1 << 0)
1d45f8b5 78
7a5ca864
FB
79/* That's all folks */
80
185b4338 81ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
7a5ca864
FB
82{
83 size_t offset = 0;
185b4338 84 int err;
7a5ca864 85
ae255e52
PB
86 if (qemu_in_coroutine()) {
87 if (do_read) {
88 return qemu_co_recv(fd, buffer, size);
89 } else {
90 return qemu_co_send(fd, buffer, size);
91 }
92 }
93
7a5ca864
FB
94 while (offset < size) {
95 ssize_t len;
96
97 if (do_read) {
00aa0040 98 len = qemu_recv(fd, buffer + offset, size - offset, 0);
7a5ca864 99 } else {
03ff3ca3 100 len = send(fd, buffer + offset, size - offset, 0);
7a5ca864
FB
101 }
102
fc19f8a0 103 if (len < 0) {
185b4338 104 err = socket_error();
03ff3ca3 105
fc19f8a0 106 /* recoverable error */
7fe7b68b 107 if (err == EINTR || (offset > 0 && err == EAGAIN)) {
fc19f8a0
PB
108 continue;
109 }
110
111 /* unrecoverable error */
185b4338 112 return -err;
7a5ca864
FB
113 }
114
115 /* eof */
116 if (len == 0) {
117 break;
118 }
119
7a5ca864
FB
120 offset += len;
121 }
122
123 return offset;
124}
125
7fe7b68b
PB
126static ssize_t read_sync(int fd, void *buffer, size_t size)
127{
128 /* Sockets are kept in blocking mode in the negotiation phase. After
129 * that, a non-readable socket simply means that another thread stole
130 * our request/reply. Synchronization is done with recv_coroutine, so
131 * that this is coroutine-safe.
132 */
133 return nbd_wr_sync(fd, buffer, size, true);
134}
135
136static ssize_t write_sync(int fd, void *buffer, size_t size)
137{
138 int ret;
139 do {
140 /* For writes, we do expect the socket to be writable. */
141 ret = nbd_wr_sync(fd, buffer, size, false);
142 } while (ret == -EAGAIN);
143 return ret;
144}
145
c12504ce
NT
146static void combine_addr(char *buf, size_t len, const char* address,
147 uint16_t port)
7a5ca864 148{
c12504ce
NT
149 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
150 if (strstr(address, ":")) {
151 snprintf(buf, len, "[%s]:%u", address, port);
152 } else {
153 snprintf(buf, len, "%s:%u", address, port);
7a5ca864 154 }
7a5ca864
FB
155}
156
c12504ce 157int tcp_socket_outgoing(const char *address, uint16_t port)
7a5ca864 158{
c12504ce
NT
159 char address_and_port[128];
160 combine_addr(address_and_port, 128, address, port);
161 return tcp_socket_outgoing_spec(address_and_port);
7a5ca864
FB
162}
163
c12504ce 164int tcp_socket_outgoing_spec(const char *address_and_port)
cd831bd7 165{
c12504ce 166 return inet_connect(address_and_port, SOCK_STREAM);
cd831bd7
TS
167}
168
c12504ce 169int tcp_socket_incoming(const char *address, uint16_t port)
cd831bd7 170{
c12504ce
NT
171 char address_and_port[128];
172 combine_addr(address_and_port, 128, address, port);
173 return tcp_socket_incoming_spec(address_and_port);
174}
cd831bd7 175
c12504ce
NT
176int tcp_socket_incoming_spec(const char *address_and_port)
177{
178 char *ostr = NULL;
179 int olen = 0;
180 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
03ff3ca3 181}
c12504ce 182
03ff3ca3
AL
183int unix_socket_incoming(const char *path)
184{
c12504ce
NT
185 char *ostr = NULL;
186 int olen = 0;
187
188 return unix_listen(path, ostr, olen);
cd831bd7
TS
189}
190
03ff3ca3
AL
191int unix_socket_outgoing(const char *path)
192{
c12504ce 193 return unix_connect(path);
03ff3ca3 194}
cd831bd7 195
7a5ca864
FB
196/* Basic flow
197
198 Server Client
199
200 Negotiate
201 Request
202 Response
203 Request
204 Response
205 ...
206 ...
207 Request (type == 2)
208*/
209
af49bbbe 210static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
7a5ca864 211{
b2e3d87f 212 char buf[8 + 8 + 8 + 128];
185b4338 213 int rc;
b2e3d87f
NT
214
215 /* Negotiate
216 [ 0 .. 7] passwd ("NBDMAGIC")
217 [ 8 .. 15] magic (0x00420281861253)
218 [16 .. 23] size
b90fb4b8
PB
219 [24 .. 27] flags
220 [28 .. 151] reserved (0)
b2e3d87f
NT
221 */
222
7fe7b68b 223 socket_set_block(csock);
185b4338
PB
224 rc = -EINVAL;
225
b2e3d87f
NT
226 TRACE("Beginning negotiation.");
227 memcpy(buf, "NBDMAGIC", 8);
228 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
229 cpu_to_be64w((uint64_t*)(buf + 16), size);
2c7989a9 230 cpu_to_be32w((uint32_t*)(buf + 24),
7a706633
PB
231 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
232 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
b90fb4b8 233 memset(buf + 28, 0, 124);
b2e3d87f
NT
234
235 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
236 LOG("write failed");
185b4338 237 goto fail;
b2e3d87f
NT
238 }
239
07f35073 240 TRACE("Negotiation succeeded.");
185b4338
PB
241 rc = 0;
242fail:
7fe7b68b 243 socket_set_nonblock(csock);
185b4338 244 return rc;
7a5ca864
FB
245}
246
1d45f8b5
LV
247int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
248 off_t *size, size_t *blocksize)
7a5ca864 249{
b2e3d87f
NT
250 char buf[256];
251 uint64_t magic, s;
252 uint16_t tmp;
185b4338 253 int rc;
b2e3d87f 254
07f35073 255 TRACE("Receiving negotiation.");
b2e3d87f 256
7fe7b68b 257 socket_set_block(csock);
185b4338
PB
258 rc = -EINVAL;
259
b2e3d87f
NT
260 if (read_sync(csock, buf, 8) != 8) {
261 LOG("read failed");
185b4338 262 goto fail;
b2e3d87f
NT
263 }
264
265 buf[8] = '\0';
266 if (strlen(buf) == 0) {
267 LOG("server connection closed");
185b4338 268 goto fail;
b2e3d87f
NT
269 }
270
271 TRACE("Magic is %c%c%c%c%c%c%c%c",
272 qemu_isprint(buf[0]) ? buf[0] : '.',
273 qemu_isprint(buf[1]) ? buf[1] : '.',
274 qemu_isprint(buf[2]) ? buf[2] : '.',
275 qemu_isprint(buf[3]) ? buf[3] : '.',
276 qemu_isprint(buf[4]) ? buf[4] : '.',
277 qemu_isprint(buf[5]) ? buf[5] : '.',
278 qemu_isprint(buf[6]) ? buf[6] : '.',
279 qemu_isprint(buf[7]) ? buf[7] : '.');
280
281 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
282 LOG("Invalid magic received");
185b4338 283 goto fail;
b2e3d87f
NT
284 }
285
286 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
287 LOG("read failed");
185b4338 288 goto fail;
b2e3d87f
NT
289 }
290 magic = be64_to_cpu(magic);
291 TRACE("Magic is 0x%" PRIx64, magic);
292
293 if (name) {
294 uint32_t reserved = 0;
295 uint32_t opt;
296 uint32_t namesize;
297
298 TRACE("Checking magic (opts_magic)");
299 if (magic != 0x49484156454F5054LL) {
300 LOG("Bad magic received");
185b4338 301 goto fail;
b2e3d87f
NT
302 }
303 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
304 LOG("flags read failed");
185b4338 305 goto fail;
b2e3d87f
NT
306 }
307 *flags = be16_to_cpu(tmp) << 16;
308 /* reserved for future use */
309 if (write_sync(csock, &reserved, sizeof(reserved)) !=
310 sizeof(reserved)) {
311 LOG("write failed (reserved)");
185b4338 312 goto fail;
b2e3d87f
NT
313 }
314 /* write the export name */
315 magic = cpu_to_be64(magic);
316 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
317 LOG("write failed (magic)");
185b4338 318 goto fail;
b2e3d87f
NT
319 }
320 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
321 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
322 LOG("write failed (opt)");
185b4338 323 goto fail;
b2e3d87f
NT
324 }
325 namesize = cpu_to_be32(strlen(name));
326 if (write_sync(csock, &namesize, sizeof(namesize)) !=
327 sizeof(namesize)) {
328 LOG("write failed (namesize)");
185b4338 329 goto fail;
b2e3d87f
NT
330 }
331 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
332 LOG("write failed (name)");
185b4338 333 goto fail;
b2e3d87f
NT
334 }
335 } else {
336 TRACE("Checking magic (cli_magic)");
337
338 if (magic != 0x00420281861253LL) {
339 LOG("Bad magic received");
185b4338 340 goto fail;
b2e3d87f
NT
341 }
342 }
343
344 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
345 LOG("read failed");
185b4338 346 goto fail;
b2e3d87f
NT
347 }
348 *size = be64_to_cpu(s);
349 *blocksize = 1024;
350 TRACE("Size is %" PRIu64, *size);
351
352 if (!name) {
353 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
354 LOG("read failed (flags)");
185b4338 355 goto fail;
b2e3d87f
NT
356 }
357 *flags = be32_to_cpup(flags);
358 } else {
359 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
360 LOG("read failed (tmp)");
185b4338 361 goto fail;
b2e3d87f
NT
362 }
363 *flags |= be32_to_cpu(tmp);
364 }
365 if (read_sync(csock, &buf, 124) != 124) {
366 LOG("read failed (buf)");
185b4338 367 goto fail;
b2e3d87f 368 }
185b4338
PB
369 rc = 0;
370
371fail:
7fe7b68b 372 socket_set_nonblock(csock);
185b4338 373 return rc;
cd831bd7 374}
7a5ca864 375
b90fb4b8
PB
376#ifdef __linux__
377int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
cd831bd7 378{
3e05c785
CL
379 TRACE("Setting NBD socket");
380
fc19f8a0 381 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
3e05c785
CL
382 int serrno = errno;
383 LOG("Failed to set NBD socket");
185b4338 384 return -serrno;
3e05c785
CL
385 }
386
b2e3d87f 387 TRACE("Setting block size to %lu", (unsigned long)blocksize);
7a5ca864 388
fc19f8a0 389 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) < 0) {
b2e3d87f
NT
390 int serrno = errno;
391 LOG("Failed setting NBD block size");
185b4338 392 return -serrno;
b2e3d87f 393 }
7a5ca864 394
0bfcd599 395 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
7a5ca864 396
fc19f8a0 397 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) < 0) {
b2e3d87f
NT
398 int serrno = errno;
399 LOG("Failed setting size (in blocks)");
185b4338 400 return -serrno;
b2e3d87f 401 }
7a5ca864 402
b90fb4b8
PB
403 if (flags & NBD_FLAG_READ_ONLY) {
404 int read_only = 1;
405 TRACE("Setting readonly attribute");
406
407 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
408 int serrno = errno;
409 LOG("Failed setting read-only attribute");
185b4338 410 return -serrno;
b90fb4b8
PB
411 }
412 }
413
973b3d0a
PB
414 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
415 && errno != ENOTTY) {
416 int serrno = errno;
417 LOG("Failed setting flags");
185b4338 418 return -serrno;
973b3d0a
PB
419 }
420
b2e3d87f 421 TRACE("Negotiation ended");
7a5ca864 422
b2e3d87f 423 return 0;
7a5ca864
FB
424}
425
426int nbd_disconnect(int fd)
427{
b2e3d87f
NT
428 ioctl(fd, NBD_CLEAR_QUE);
429 ioctl(fd, NBD_DISCONNECT);
430 ioctl(fd, NBD_CLEAR_SOCK);
431 return 0;
7a5ca864
FB
432}
433
0a4eb864 434int nbd_client(int fd)
7a5ca864 435{
b2e3d87f
NT
436 int ret;
437 int serrno;
7a5ca864 438
b2e3d87f 439 TRACE("Doing NBD loop");
7a5ca864 440
b2e3d87f 441 ret = ioctl(fd, NBD_DO_IT);
fc19f8a0 442 if (ret < 0 && errno == EPIPE) {
74624688
PB
443 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
444 * the socket via NBD_DISCONNECT. We do not want to return 1 in
445 * that case.
446 */
447 ret = 0;
448 }
b2e3d87f 449 serrno = errno;
7a5ca864 450
b2e3d87f 451 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
7a5ca864 452
b2e3d87f
NT
453 TRACE("Clearing NBD queue");
454 ioctl(fd, NBD_CLEAR_QUE);
7a5ca864 455
b2e3d87f
NT
456 TRACE("Clearing NBD socket");
457 ioctl(fd, NBD_CLEAR_SOCK);
7a5ca864 458
b2e3d87f
NT
459 errno = serrno;
460 return ret;
7a5ca864 461}
03ff3ca3 462#else
8e72506e 463int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
03ff3ca3 464{
185b4338 465 return -ENOTSUP;
03ff3ca3
AL
466}
467
468int nbd_disconnect(int fd)
469{
185b4338 470 return -ENOTSUP;
03ff3ca3
AL
471}
472
0a4eb864 473int nbd_client(int fd)
03ff3ca3 474{
185b4338 475 return -ENOTSUP;
03ff3ca3
AL
476}
477#endif
7a5ca864 478
94e7340b 479ssize_t nbd_send_request(int csock, struct nbd_request *request)
7a5ca864 480{
b2e3d87f 481 uint8_t buf[4 + 4 + 8 + 8 + 4];
185b4338 482 ssize_t ret;
b2e3d87f
NT
483
484 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
485 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
486 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
487 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
488 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
75818250 489
b2e3d87f
NT
490 TRACE("Sending request to client: "
491 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
492 request->from, request->len, request->handle, request->type);
493
185b4338
PB
494 ret = write_sync(csock, buf, sizeof(buf));
495 if (ret < 0) {
496 return ret;
497 }
498
499 if (ret != sizeof(buf)) {
b2e3d87f 500 LOG("writing to socket failed");
185b4338 501 return -EINVAL;
b2e3d87f
NT
502 }
503 return 0;
504}
75818250 505
94e7340b 506static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
75818250 507{
b2e3d87f
NT
508 uint8_t buf[4 + 4 + 8 + 8 + 4];
509 uint32_t magic;
185b4338 510 ssize_t ret;
b2e3d87f 511
185b4338
PB
512 ret = read_sync(csock, buf, sizeof(buf));
513 if (ret < 0) {
514 return ret;
515 }
516
517 if (ret != sizeof(buf)) {
b2e3d87f 518 LOG("read failed");
185b4338 519 return -EINVAL;
b2e3d87f
NT
520 }
521
522 /* Request
523 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
524 [ 4 .. 7] type (0 == READ, 1 == WRITE)
525 [ 8 .. 15] handle
526 [16 .. 23] from
527 [24 .. 27] len
528 */
529
530 magic = be32_to_cpup((uint32_t*)buf);
531 request->type = be32_to_cpup((uint32_t*)(buf + 4));
532 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
533 request->from = be64_to_cpup((uint64_t*)(buf + 16));
534 request->len = be32_to_cpup((uint32_t*)(buf + 24));
535
536 TRACE("Got request: "
537 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
538 magic, request->type, request->from, request->len);
539
540 if (magic != NBD_REQUEST_MAGIC) {
541 LOG("invalid magic (got 0x%x)", magic);
185b4338 542 return -EINVAL;
b2e3d87f
NT
543 }
544 return 0;
75818250
TS
545}
546
94e7340b 547ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
75818250 548{
b2e3d87f
NT
549 uint8_t buf[NBD_REPLY_SIZE];
550 uint32_t magic;
185b4338 551 ssize_t ret;
b2e3d87f 552
185b4338
PB
553 ret = read_sync(csock, buf, sizeof(buf));
554 if (ret < 0) {
555 return ret;
556 }
557
558 if (ret != sizeof(buf)) {
b2e3d87f 559 LOG("read failed");
185b4338 560 return -EINVAL;
b2e3d87f
NT
561 }
562
563 /* Reply
564 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
565 [ 4 .. 7] error (0 == no error)
566 [ 7 .. 15] handle
567 */
568
569 magic = be32_to_cpup((uint32_t*)buf);
570 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
571 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
572
573 TRACE("Got reply: "
574 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
575 magic, reply->error, reply->handle);
576
577 if (magic != NBD_REPLY_MAGIC) {
578 LOG("invalid magic (got 0x%x)", magic);
185b4338 579 return -EINVAL;
b2e3d87f
NT
580 }
581 return 0;
75818250
TS
582}
583
94e7340b 584static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
75818250 585{
b2e3d87f 586 uint8_t buf[4 + 4 + 8];
185b4338 587 ssize_t ret;
b2e3d87f
NT
588
589 /* Reply
590 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
591 [ 4 .. 7] error (0 == no error)
592 [ 7 .. 15] handle
593 */
594 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
595 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
596 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
597
598 TRACE("Sending response to client");
599
185b4338
PB
600 ret = write_sync(csock, buf, sizeof(buf));
601 if (ret < 0) {
602 return ret;
603 }
604
605 if (ret != sizeof(buf)) {
b2e3d87f 606 LOG("writing to socket failed");
185b4338 607 return -EINVAL;
b2e3d87f
NT
608 }
609 return 0;
75818250 610}
7a5ca864 611
41996e38
PB
612#define MAX_NBD_REQUESTS 16
613
d9a73806
PB
614typedef struct NBDRequest NBDRequest;
615
616struct NBDRequest {
617 QSIMPLEQ_ENTRY(NBDRequest) entry;
72deddc5 618 NBDClient *client;
d9a73806
PB
619 uint8_t *data;
620};
621
af49bbbe
PB
622struct NBDExport {
623 BlockDriverState *bs;
624 off_t dev_offset;
625 off_t size;
af49bbbe 626 uint32_t nbdflags;
d9a73806 627 QSIMPLEQ_HEAD(, NBDRequest) requests;
af49bbbe
PB
628};
629
1743b515
PB
630struct NBDClient {
631 int refcount;
632 void (*close)(NBDClient *client);
633
634 NBDExport *exp;
635 int sock;
262db388
PB
636
637 Coroutine *recv_coroutine;
638
639 CoMutex send_lock;
640 Coroutine *send_coroutine;
41996e38
PB
641
642 int nb_requests;
1743b515
PB
643};
644
645static void nbd_client_get(NBDClient *client)
646{
647 client->refcount++;
648}
649
650static void nbd_client_put(NBDClient *client)
651{
652 if (--client->refcount == 0) {
653 g_free(client);
654 }
655}
656
657static void nbd_client_close(NBDClient *client)
658{
659 qemu_set_fd_handler2(client->sock, NULL, NULL, NULL, NULL);
660 close(client->sock);
661 client->sock = -1;
662 if (client->close) {
663 client->close(client);
664 }
665 nbd_client_put(client);
666}
667
72deddc5 668static NBDRequest *nbd_request_get(NBDClient *client)
d9a73806
PB
669{
670 NBDRequest *req;
72deddc5
PB
671 NBDExport *exp = client->exp;
672
41996e38
PB
673 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
674 client->nb_requests++;
675
d9a73806
PB
676 if (QSIMPLEQ_EMPTY(&exp->requests)) {
677 req = g_malloc0(sizeof(NBDRequest));
678 req->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
679 } else {
680 req = QSIMPLEQ_FIRST(&exp->requests);
681 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
682 }
72deddc5
PB
683 nbd_client_get(client);
684 req->client = client;
d9a73806
PB
685 return req;
686}
687
72deddc5 688static void nbd_request_put(NBDRequest *req)
d9a73806 689{
72deddc5
PB
690 NBDClient *client = req->client;
691 QSIMPLEQ_INSERT_HEAD(&client->exp->requests, req, entry);
41996e38
PB
692 if (client->nb_requests-- == MAX_NBD_REQUESTS) {
693 qemu_notify_event();
694 }
72deddc5 695 nbd_client_put(client);
d9a73806
PB
696}
697
af49bbbe
PB
698NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
699 off_t size, uint32_t nbdflags)
700{
701 NBDExport *exp = g_malloc0(sizeof(NBDExport));
d9a73806 702 QSIMPLEQ_INIT(&exp->requests);
af49bbbe
PB
703 exp->bs = bs;
704 exp->dev_offset = dev_offset;
705 exp->nbdflags = nbdflags;
706 exp->size = size == -1 ? exp->bs->total_sectors * 512 : size;
af49bbbe
PB
707 return exp;
708}
709
710void nbd_export_close(NBDExport *exp)
711{
d9a73806
PB
712 while (!QSIMPLEQ_EMPTY(&exp->requests)) {
713 NBDRequest *first = QSIMPLEQ_FIRST(&exp->requests);
714 QSIMPLEQ_REMOVE_HEAD(&exp->requests, entry);
715 qemu_vfree(first->data);
716 g_free(first);
717 }
718
af49bbbe
PB
719 bdrv_close(exp->bs);
720 g_free(exp);
721}
722
41996e38 723static int nbd_can_read(void *opaque);
262db388
PB
724static void nbd_read(void *opaque);
725static void nbd_restart_write(void *opaque);
726
94e7340b
PB
727static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
728 int len)
22045592 729{
72deddc5
PB
730 NBDClient *client = req->client;
731 int csock = client->sock;
94e7340b 732 ssize_t rc, ret;
22045592 733
262db388 734 qemu_co_mutex_lock(&client->send_lock);
41996e38
PB
735 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read,
736 nbd_restart_write, client);
262db388
PB
737 client->send_coroutine = qemu_coroutine_self();
738
22045592
PB
739 if (!len) {
740 rc = nbd_send_reply(csock, reply);
22045592
PB
741 } else {
742 socket_set_cork(csock, 1);
743 rc = nbd_send_reply(csock, reply);
fc19f8a0 744 if (rc >= 0) {
262db388 745 ret = qemu_co_send(csock, req->data, len);
22045592 746 if (ret != len) {
185b4338 747 rc = -EIO;
22045592
PB
748 }
749 }
22045592
PB
750 socket_set_cork(csock, 0);
751 }
262db388
PB
752
753 client->send_coroutine = NULL;
41996e38 754 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
262db388 755 qemu_co_mutex_unlock(&client->send_lock);
22045592
PB
756 return rc;
757}
758
94e7340b 759static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
a030b347 760{
72deddc5
PB
761 NBDClient *client = req->client;
762 int csock = client->sock;
94e7340b 763 ssize_t rc;
a030b347 764
262db388 765 client->recv_coroutine = qemu_coroutine_self();
7fe7b68b
PB
766 rc = nbd_receive_request(csock, request);
767 if (rc < 0) {
768 if (rc != -EAGAIN) {
769 rc = -EIO;
770 }
a030b347
PB
771 goto out;
772 }
773
774 if (request->len > NBD_BUFFER_SIZE) {
775 LOG("len (%u) is larger than max len (%u)",
776 request->len, NBD_BUFFER_SIZE);
777 rc = -EINVAL;
778 goto out;
779 }
780
781 if ((request->from + request->len) < request->from) {
782 LOG("integer overflow detected! "
783 "you're probably being attacked");
784 rc = -EINVAL;
785 goto out;
786 }
787
788 TRACE("Decoding type");
789
790 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
791 TRACE("Reading %u byte(s)", request->len);
792
262db388 793 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
a030b347
PB
794 LOG("reading from socket failed");
795 rc = -EIO;
796 goto out;
797 }
798 }
799 rc = 0;
800
801out:
262db388 802 client->recv_coroutine = NULL;
a030b347
PB
803 return rc;
804}
805
262db388 806static void nbd_trip(void *opaque)
75818250 807{
262db388 808 NBDClient *client = opaque;
72deddc5 809 NBDRequest *req = nbd_request_get(client);
1743b515 810 NBDExport *exp = client->exp;
b2e3d87f
NT
811 struct nbd_request request;
812 struct nbd_reply reply;
94e7340b 813 ssize_t ret;
b2e3d87f
NT
814
815 TRACE("Reading request.");
816
262db388 817 ret = nbd_co_receive_request(req, &request);
7fe7b68b
PB
818 if (ret == -EAGAIN) {
819 goto done;
820 }
a030b347 821 if (ret == -EIO) {
d9a73806 822 goto out;
a030b347 823 }
b2e3d87f 824
fae69416
PB
825 reply.handle = request.handle;
826 reply.error = 0;
827
a030b347
PB
828 if (ret < 0) {
829 reply.error = -ret;
830 goto error_reply;
b2e3d87f
NT
831 }
832
af49bbbe 833 if ((request.from + request.len) > exp->size) {
b2e3d87f
NT
834 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
835 ", Offset: %" PRIu64 "\n",
af49bbbe 836 request.from, request.len,
0fee8f34 837 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
b2e3d87f 838 LOG("requested operation past EOF--bad client?");
fae69416 839 goto invalid_request;
b2e3d87f
NT
840 }
841
2c7989a9 842 switch (request.type & NBD_CMD_MASK_COMMAND) {
b2e3d87f
NT
843 case NBD_CMD_READ:
844 TRACE("Request type is READ");
845
af49bbbe 846 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
d9a73806 847 req->data, request.len / 512);
adcf6302 848 if (ret < 0) {
b2e3d87f 849 LOG("reading from file failed");
adcf6302 850 reply.error = -ret;
fae69416 851 goto error_reply;
b2e3d87f 852 }
b2e3d87f
NT
853
854 TRACE("Read %u byte(s)", request.len);
262db388 855 if (nbd_co_send_reply(req, &reply, request.len) < 0)
d9a73806 856 goto out;
b2e3d87f
NT
857 break;
858 case NBD_CMD_WRITE:
859 TRACE("Request type is WRITE");
860
af49bbbe 861 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
b2e3d87f 862 TRACE("Server is read-only, return error");
fae69416
PB
863 reply.error = EROFS;
864 goto error_reply;
865 }
866
867 TRACE("Writing to device");
868
af49bbbe 869 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
d9a73806 870 req->data, request.len / 512);
fae69416
PB
871 if (ret < 0) {
872 LOG("writing to file failed");
873 reply.error = -ret;
874 goto error_reply;
875 }
b2e3d87f 876
fae69416 877 if (request.type & NBD_CMD_FLAG_FUA) {
262db388 878 ret = bdrv_co_flush(exp->bs);
adcf6302 879 if (ret < 0) {
fae69416 880 LOG("flush failed");
adcf6302 881 reply.error = -ret;
fae69416 882 goto error_reply;
2c7989a9 883 }
b2e3d87f
NT
884 }
885
fc19f8a0 886 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 887 goto out;
fc19f8a0 888 }
b2e3d87f
NT
889 break;
890 case NBD_CMD_DISC:
891 TRACE("Request type is DISCONNECT");
892 errno = 0;
262db388 893 goto out;
1486d04a
PB
894 case NBD_CMD_FLUSH:
895 TRACE("Request type is FLUSH");
896
262db388 897 ret = bdrv_co_flush(exp->bs);
1486d04a
PB
898 if (ret < 0) {
899 LOG("flush failed");
900 reply.error = -ret;
901 }
fc19f8a0 902 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 903 goto out;
fc19f8a0 904 }
7a706633
PB
905 break;
906 case NBD_CMD_TRIM:
907 TRACE("Request type is TRIM");
262db388
PB
908 ret = bdrv_co_discard(exp->bs, (request.from + exp->dev_offset) / 512,
909 request.len / 512);
7a706633
PB
910 if (ret < 0) {
911 LOG("discard failed");
912 reply.error = -ret;
913 }
fc19f8a0 914 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 915 goto out;
fc19f8a0 916 }
1486d04a 917 break;
b2e3d87f
NT
918 default:
919 LOG("invalid request type (%u) received", request.type);
fae69416
PB
920 invalid_request:
921 reply.error = -EINVAL;
922 error_reply:
fc19f8a0 923 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 924 goto out;
fc19f8a0 925 }
fae69416 926 break;
b2e3d87f
NT
927 }
928
929 TRACE("Request/Reply complete");
930
7fe7b68b 931done:
262db388
PB
932 nbd_request_put(req);
933 return;
934
d9a73806 935out:
72deddc5 936 nbd_request_put(req);
262db388 937 nbd_client_close(client);
7a5ca864 938}
af49bbbe 939
41996e38
PB
940static int nbd_can_read(void *opaque)
941{
942 NBDClient *client = opaque;
943
944 return client->recv_coroutine || client->nb_requests < MAX_NBD_REQUESTS;
945}
946
1743b515
PB
947static void nbd_read(void *opaque)
948{
949 NBDClient *client = opaque;
950
262db388
PB
951 if (client->recv_coroutine) {
952 qemu_coroutine_enter(client->recv_coroutine, NULL);
953 } else {
954 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
1743b515 955 }
1743b515
PB
956}
957
262db388
PB
958static void nbd_restart_write(void *opaque)
959{
960 NBDClient *client = opaque;
961
962 qemu_coroutine_enter(client->send_coroutine, NULL);
963}
964
1743b515
PB
965NBDClient *nbd_client_new(NBDExport *exp, int csock,
966 void (*close)(NBDClient *))
af49bbbe 967{
1743b515 968 NBDClient *client;
fc19f8a0 969 if (nbd_send_negotiate(csock, exp->size, exp->nbdflags) < 0) {
1743b515
PB
970 return NULL;
971 }
972 client = g_malloc0(sizeof(NBDClient));
973 client->refcount = 1;
974 client->exp = exp;
975 client->sock = csock;
976 client->close = close;
262db388 977 qemu_co_mutex_init(&client->send_lock);
41996e38 978 qemu_set_fd_handler2(csock, nbd_can_read, nbd_read, NULL, client);
1743b515 979 return client;
af49bbbe 980}