]> git.proxmox.com Git - mirror_qemu.git/blame - nbd.c
block/parallels: improve image reading performance
[mirror_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 18
737e150e 19#include "block/nbd.h"
e140177d 20#include "sysemu/block-backend.h"
7a5ca864 21
737e150e 22#include "block/coroutine.h"
262db388 23
7a5ca864
FB
24#include <errno.h>
25#include <string.h>
03ff3ca3 26#ifndef _WIN32
7a5ca864 27#include <sys/ioctl.h>
03ff3ca3 28#endif
5dc2eec9 29#if defined(__sun__) || defined(__HAIKU__)
7e00eb9b
AL
30#include <sys/ioccom.h>
31#endif
7a5ca864
FB
32#include <ctype.h>
33#include <inttypes.h>
75818250 34
b90fb4b8
PB
35#ifdef __linux__
36#include <linux/fs.h>
37#endif
38
1de7afc9
PB
39#include "qemu/sockets.h"
40#include "qemu/queue.h"
6a1751b7 41#include "qemu/main-loop.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
f5076b5a
HB
59/* This is all part of the "official" NBD API.
60 *
61 * The most up-to-date documentation is available at:
62 * https://github.com/yoe/nbd/blob/master/doc/proto.txt
63 */
7a5ca864 64
fa26c26b 65#define NBD_REQUEST_SIZE (4 + 4 + 8 + 8 + 4)
b2e3d87f 66#define NBD_REPLY_SIZE (4 + 4 + 8)
7a5ca864
FB
67#define NBD_REQUEST_MAGIC 0x25609513
68#define NBD_REPLY_MAGIC 0x67446698
fa26c26b
PB
69#define NBD_OPTS_MAGIC 0x49484156454F5054LL
70#define NBD_CLIENT_MAGIC 0x0000420281861253LL
f5076b5a 71#define NBD_REP_MAGIC 0x3e889045565a9LL
7a5ca864
FB
72
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)
b2e3d87f
NT
79#define NBD_PRINT_DEBUG _IO(0xab, 6)
80#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
7a5ca864 81#define NBD_DISCONNECT _IO(0xab, 8)
bbb74edd
PB
82#define NBD_SET_TIMEOUT _IO(0xab, 9)
83#define NBD_SET_FLAGS _IO(0xab, 10)
7a5ca864 84
f5076b5a
HB
85#define NBD_OPT_EXPORT_NAME (1)
86#define NBD_OPT_ABORT (2)
32d7d2e0 87#define NBD_OPT_LIST (3)
1d45f8b5 88
ca441480
PB
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.
92 */
93#define NBD_SUCCESS 0
94#define NBD_EPERM 1
95#define NBD_EIO 5
96#define NBD_ENOMEM 12
97#define NBD_EINVAL 22
98#define NBD_ENOSPC 28
99
100static int system_errno_to_nbd_errno(int err)
101{
102 switch (err) {
103 case 0:
104 return NBD_SUCCESS;
105 case EPERM:
106 return NBD_EPERM;
107 case EIO:
108 return NBD_EIO;
109 case ENOMEM:
110 return NBD_ENOMEM;
111#ifdef EDQUOT
112 case EDQUOT:
113#endif
114 case EFBIG:
115 case ENOSPC:
116 return NBD_ENOSPC;
117 case EINVAL:
118 default:
119 return NBD_EINVAL;
120 }
121}
122
123static int nbd_errno_to_system_errno(int err)
124{
125 switch (err) {
126 case NBD_SUCCESS:
127 return 0;
128 case NBD_EPERM:
129 return EPERM;
130 case NBD_EIO:
131 return EIO;
132 case NBD_ENOMEM:
133 return ENOMEM;
134 case NBD_ENOSPC:
135 return ENOSPC;
136 case NBD_EINVAL:
137 default:
138 return EINVAL;
139 }
140}
141
9a304d29
PB
142/* Definitions for opaque data types */
143
144typedef struct NBDRequest NBDRequest;
145
146struct NBDRequest {
147 QSIMPLEQ_ENTRY(NBDRequest) entry;
148 NBDClient *client;
149 uint8_t *data;
150};
151
152struct NBDExport {
2c8d9f06 153 int refcount;
0ddf08db
PB
154 void (*close)(NBDExport *exp);
155
aadf99a7 156 BlockBackend *blk;
ee0a19ec 157 char *name;
9a304d29
PB
158 off_t dev_offset;
159 off_t size;
160 uint32_t nbdflags;
4b9441f6 161 QTAILQ_HEAD(, NBDClient) clients;
ee0a19ec 162 QTAILQ_ENTRY(NBDExport) next;
958c717d
HR
163
164 AioContext *ctx;
9a304d29
PB
165};
166
ee0a19ec
PB
167static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
168
9a304d29
PB
169struct NBDClient {
170 int refcount;
171 void (*close)(NBDClient *client);
172
173 NBDExport *exp;
174 int sock;
175
176 Coroutine *recv_coroutine;
177
178 CoMutex send_lock;
179 Coroutine *send_coroutine;
180
958c717d
HR
181 bool can_read;
182
4b9441f6 183 QTAILQ_ENTRY(NBDClient) next;
9a304d29 184 int nb_requests;
ff2b68aa 185 bool closing;
9a304d29
PB
186};
187
7a5ca864
FB
188/* That's all folks */
189
958c717d
HR
190static void nbd_set_handlers(NBDClient *client);
191static void nbd_unset_handlers(NBDClient *client);
192static void nbd_update_can_read(NBDClient *client);
193
185b4338 194ssize_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
7a5ca864
FB
195{
196 size_t offset = 0;
185b4338 197 int err;
7a5ca864 198
ae255e52
PB
199 if (qemu_in_coroutine()) {
200 if (do_read) {
201 return qemu_co_recv(fd, buffer, size);
202 } else {
203 return qemu_co_send(fd, buffer, size);
204 }
205 }
206
7a5ca864
FB
207 while (offset < size) {
208 ssize_t len;
209
210 if (do_read) {
00aa0040 211 len = qemu_recv(fd, buffer + offset, size - offset, 0);
7a5ca864 212 } else {
03ff3ca3 213 len = send(fd, buffer + offset, size - offset, 0);
7a5ca864
FB
214 }
215
fc19f8a0 216 if (len < 0) {
185b4338 217 err = socket_error();
03ff3ca3 218
fc19f8a0 219 /* recoverable error */
79d9b656 220 if (err == EINTR || (offset > 0 && (err == EAGAIN || err == EWOULDBLOCK))) {
fc19f8a0
PB
221 continue;
222 }
223
224 /* unrecoverable error */
185b4338 225 return -err;
7a5ca864
FB
226 }
227
228 /* eof */
229 if (len == 0) {
230 break;
231 }
232
7a5ca864
FB
233 offset += len;
234 }
235
236 return offset;
237}
238
7fe7b68b
PB
239static ssize_t read_sync(int fd, void *buffer, size_t size)
240{
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.
245 */
246 return nbd_wr_sync(fd, buffer, size, true);
247}
248
0379f474
HR
249static ssize_t drop_sync(int fd, size_t size)
250{
251 ssize_t ret, dropped = size;
252 uint8_t *buffer = g_malloc(MIN(65536, size));
253
254 while (size > 0) {
255 ret = read_sync(fd, buffer, MIN(65536, size));
256 if (ret < 0) {
257 g_free(buffer);
258 return ret;
259 }
260
261 assert(ret <= size);
262 size -= ret;
263 }
264
265 g_free(buffer);
266 return dropped;
267}
268
7fe7b68b
PB
269static ssize_t write_sync(int fd, void *buffer, size_t size)
270{
271 int ret;
272 do {
273 /* For writes, we do expect the socket to be writable. */
274 ret = nbd_wr_sync(fd, buffer, size, false);
275 } while (ret == -EAGAIN);
276 return ret;
277}
278
6b8c01e7 279/* Basic flow for negotiation
7a5ca864
FB
280
281 Server Client
7a5ca864 282 Negotiate
6b8c01e7
PB
283
284 or
285
286 Server Client
287 Negotiate #1
288 Option
289 Negotiate #2
290
291 ----
292
293 followed by
294
295 Server Client
7a5ca864
FB
296 Request
297 Response
298 Request
299 Response
300 ...
301 ...
302 Request (type == 2)
6b8c01e7 303
7a5ca864
FB
304*/
305
f5076b5a 306static int nbd_send_rep(int csock, uint32_t type, uint32_t opt)
6b8c01e7 307{
6b8c01e7 308 uint64_t magic;
f5076b5a 309 uint32_t len;
6b8c01e7 310
f5076b5a
HB
311 magic = cpu_to_be64(NBD_REP_MAGIC);
312 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
313 LOG("write failed (rep magic)");
314 return -EINVAL;
6b8c01e7 315 }
f5076b5a
HB
316 opt = cpu_to_be32(opt);
317 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
318 LOG("write failed (rep opt)");
319 return -EINVAL;
6b8c01e7 320 }
f5076b5a
HB
321 type = cpu_to_be32(type);
322 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
323 LOG("write failed (rep type)");
324 return -EINVAL;
6b8c01e7 325 }
f5076b5a
HB
326 len = cpu_to_be32(0);
327 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
328 LOG("write failed (rep data length)");
329 return -EINVAL;
6b8c01e7 330 }
f5076b5a
HB
331 return 0;
332}
6b8c01e7 333
32d7d2e0
HB
334static int nbd_send_rep_list(int csock, NBDExport *exp)
335{
336 uint64_t magic, name_len;
337 uint32_t opt, type, len;
338
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)");
343 return -EINVAL;
344 }
345 opt = cpu_to_be32(NBD_OPT_LIST);
346 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
347 LOG("write failed (opt)");
348 return -EINVAL;
349 }
350 type = cpu_to_be32(NBD_REP_SERVER);
351 if (write_sync(csock, &type, sizeof(type)) != sizeof(type)) {
352 LOG("write failed (reply type)");
353 return -EINVAL;
354 }
355 len = cpu_to_be32(name_len + sizeof(len));
356 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
357 LOG("write failed (length)");
358 return -EINVAL;
359 }
360 len = cpu_to_be32(name_len);
361 if (write_sync(csock, &len, sizeof(len)) != sizeof(len)) {
362 LOG("write failed (length)");
363 return -EINVAL;
364 }
365 if (write_sync(csock, exp->name, name_len) != name_len) {
366 LOG("write failed (buffer)");
367 return -EINVAL;
368 }
369 return 0;
370}
371
372static int nbd_handle_list(NBDClient *client, uint32_t length)
373{
374 int csock;
375 NBDExport *exp;
376
377 csock = client->sock;
378 if (length) {
0379f474
HR
379 if (drop_sync(csock, length) != length) {
380 return -EIO;
381 }
32d7d2e0
HB
382 return nbd_send_rep(csock, NBD_REP_ERR_INVALID, NBD_OPT_LIST);
383 }
384
385 /* For each export, send a NBD_REP_SERVER reply. */
386 QTAILQ_FOREACH(exp, &exports, next) {
387 if (nbd_send_rep_list(csock, exp)) {
388 return -EINVAL;
389 }
390 }
391 /* Finish with a NBD_REP_ACK. */
392 return nbd_send_rep(csock, NBD_REP_ACK, NBD_OPT_LIST);
393}
394
f5076b5a
HB
395static int nbd_handle_export_name(NBDClient *client, uint32_t length)
396{
397 int rc = -EINVAL, csock = client->sock;
398 char name[256];
6b8c01e7 399
f5076b5a
HB
400 /* Client sends:
401 [20 .. xx] export name (length bytes)
402 */
6b8c01e7 403 TRACE("Checking length");
6b8c01e7
PB
404 if (length > 255) {
405 LOG("Bad length received");
406 goto fail;
407 }
408 if (read_sync(csock, name, length) != length) {
409 LOG("read failed");
410 goto fail;
411 }
412 name[length] = '\0';
413
414 client->exp = nbd_export_find(name);
415 if (!client->exp) {
416 LOG("export not found");
417 goto fail;
418 }
419
420 QTAILQ_INSERT_TAIL(&client->exp->clients, client, next);
421 nbd_export_get(client->exp);
6b8c01e7
PB
422 rc = 0;
423fail:
424 return rc;
425}
426
f5076b5a
HB
427static int nbd_receive_options(NBDClient *client)
428{
9c122ada
HR
429 int csock = client->sock;
430 uint32_t flags;
431
432 /* Client sends:
433 [ 0 .. 3] client flags
434
435 [ 0 .. 7] NBD_OPTS_MAGIC
436 [ 8 .. 11] NBD option
437 [12 .. 15] Data length
438 ... Rest of request
439
440 [ 0 .. 7] NBD_OPTS_MAGIC
441 [ 8 .. 11] Second NBD option
442 [12 .. 15] Data length
443 ... Rest of request
444 */
445
446 if (read_sync(csock, &flags, sizeof(flags)) != sizeof(flags)) {
447 LOG("read failed");
448 return -EIO;
449 }
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");
454 return -EIO;
455 }
456
f5076b5a 457 while (1) {
9c122ada 458 int ret;
f5076b5a
HB
459 uint32_t tmp, length;
460 uint64_t magic;
461
f5076b5a
HB
462 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
463 LOG("read failed");
464 return -EINVAL;
465 }
466 TRACE("Checking opts magic");
467 if (magic != be64_to_cpu(NBD_OPTS_MAGIC)) {
468 LOG("Bad magic received");
469 return -EINVAL;
470 }
471
472 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
473 LOG("read failed");
474 return -EINVAL;
475 }
476
477 if (read_sync(csock, &length, sizeof(length)) != sizeof(length)) {
478 LOG("read failed");
479 return -EINVAL;
480 }
481 length = be32_to_cpu(length);
482
483 TRACE("Checking option");
484 switch (be32_to_cpu(tmp)) {
32d7d2e0 485 case NBD_OPT_LIST:
892f5a52
HR
486 ret = nbd_handle_list(client, length);
487 if (ret < 0) {
488 return ret;
32d7d2e0
HB
489 }
490 break;
491
f5076b5a
HB
492 case NBD_OPT_ABORT:
493 return -EINVAL;
494
495 case NBD_OPT_EXPORT_NAME:
496 return nbd_handle_export_name(client, length);
497
498 default:
499 tmp = be32_to_cpu(tmp);
500 LOG("Unsupported option 0x%x", tmp);
501 nbd_send_rep(client->sock, NBD_REP_ERR_UNSUP, tmp);
502 return -EINVAL;
503 }
504 }
505}
506
9a304d29 507static int nbd_send_negotiate(NBDClient *client)
7a5ca864 508{
9a304d29 509 int csock = client->sock;
b2e3d87f 510 char buf[8 + 8 + 8 + 128];
185b4338 511 int rc;
6b8c01e7
PB
512 const int myflags = (NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
513 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
b2e3d87f 514
6b8c01e7
PB
515 /* Negotiation header without options:
516 [ 0 .. 7] passwd ("NBDMAGIC")
517 [ 8 .. 15] magic (NBD_CLIENT_MAGIC)
b2e3d87f 518 [16 .. 23] size
6b8c01e7 519 [24 .. 25] server flags (0)
5672ee54 520 [26 .. 27] export flags
6b8c01e7
PB
521 [28 .. 151] reserved (0)
522
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)
527
528 part 2 (after options are sent):
529 [18 .. 25] size
530 [26 .. 27] export flags
531 [28 .. 151] reserved (0)
b2e3d87f
NT
532 */
533
f9e8cacc 534 qemu_set_block(csock);
185b4338
PB
535 rc = -EINVAL;
536
b2e3d87f 537 TRACE("Beginning negotiation.");
8ffaaba0 538 memset(buf, 0, sizeof(buf));
b2e3d87f 539 memcpy(buf, "NBDMAGIC", 8);
6b8c01e7
PB
540 if (client->exp) {
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);
545 } else {
546 cpu_to_be64w((uint64_t*)(buf + 8), NBD_OPTS_MAGIC);
f5076b5a 547 cpu_to_be16w((uint16_t *)(buf + 16), NBD_FLAG_FIXED_NEWSTYLE);
6b8c01e7 548 }
b2e3d87f 549
6b8c01e7
PB
550 if (client->exp) {
551 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
552 LOG("write failed");
553 goto fail;
554 }
555 } else {
556 if (write_sync(csock, buf, 18) != 18) {
557 LOG("write failed");
558 goto fail;
559 }
560 rc = nbd_receive_options(client);
f5076b5a 561 if (rc != 0) {
6b8c01e7
PB
562 LOG("option negotiation failed");
563 goto fail;
564 }
565
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) {
570 LOG("write failed");
571 goto fail;
572 }
b2e3d87f
NT
573 }
574
07f35073 575 TRACE("Negotiation succeeded.");
185b4338
PB
576 rc = 0;
577fail:
f9e8cacc 578 qemu_set_nonblock(csock);
185b4338 579 return rc;
7a5ca864
FB
580}
581
1d45f8b5 582int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
3f472659 583 off_t *size, Error **errp)
7a5ca864 584{
b2e3d87f
NT
585 char buf[256];
586 uint64_t magic, s;
587 uint16_t tmp;
185b4338 588 int rc;
b2e3d87f 589
07f35073 590 TRACE("Receiving negotiation.");
b2e3d87f 591
185b4338
PB
592 rc = -EINVAL;
593
b2e3d87f 594 if (read_sync(csock, buf, 8) != 8) {
1ce52846 595 error_setg(errp, "Failed to read data");
185b4338 596 goto fail;
b2e3d87f
NT
597 }
598
599 buf[8] = '\0';
600 if (strlen(buf) == 0) {
1ce52846 601 error_setg(errp, "Server connection closed unexpectedly");
185b4338 602 goto fail;
b2e3d87f
NT
603 }
604
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] : '.');
614
615 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
1ce52846 616 error_setg(errp, "Invalid magic received");
185b4338 617 goto fail;
b2e3d87f
NT
618 }
619
620 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
1ce52846 621 error_setg(errp, "Failed to read magic");
185b4338 622 goto fail;
b2e3d87f
NT
623 }
624 magic = be64_to_cpu(magic);
625 TRACE("Magic is 0x%" PRIx64, magic);
626
627 if (name) {
628 uint32_t reserved = 0;
629 uint32_t opt;
630 uint32_t namesize;
631
632 TRACE("Checking magic (opts_magic)");
fa26c26b 633 if (magic != NBD_OPTS_MAGIC) {
1ce52846
HR
634 if (magic == NBD_CLIENT_MAGIC) {
635 error_setg(errp, "Server does not support export names");
636 } else {
637 error_setg(errp, "Bad magic received");
638 }
185b4338 639 goto fail;
b2e3d87f
NT
640 }
641 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
1ce52846 642 error_setg(errp, "Failed to read server flags");
185b4338 643 goto fail;
b2e3d87f
NT
644 }
645 *flags = be16_to_cpu(tmp) << 16;
646 /* reserved for future use */
647 if (write_sync(csock, &reserved, sizeof(reserved)) !=
648 sizeof(reserved)) {
1ce52846 649 error_setg(errp, "Failed to read reserved field");
185b4338 650 goto fail;
b2e3d87f
NT
651 }
652 /* write the export name */
653 magic = cpu_to_be64(magic);
654 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
1ce52846 655 error_setg(errp, "Failed to send export name magic");
185b4338 656 goto fail;
b2e3d87f
NT
657 }
658 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
659 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
1ce52846 660 error_setg(errp, "Failed to send export name option number");
185b4338 661 goto fail;
b2e3d87f
NT
662 }
663 namesize = cpu_to_be32(strlen(name));
664 if (write_sync(csock, &namesize, sizeof(namesize)) !=
665 sizeof(namesize)) {
1ce52846 666 error_setg(errp, "Failed to send export name length");
185b4338 667 goto fail;
b2e3d87f
NT
668 }
669 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
1ce52846 670 error_setg(errp, "Failed to send export name");
185b4338 671 goto fail;
b2e3d87f
NT
672 }
673 } else {
674 TRACE("Checking magic (cli_magic)");
675
fa26c26b 676 if (magic != NBD_CLIENT_MAGIC) {
1ce52846
HR
677 if (magic == NBD_OPTS_MAGIC) {
678 error_setg(errp, "Server requires an export name");
679 } else {
680 error_setg(errp, "Bad magic received");
681 }
185b4338 682 goto fail;
b2e3d87f
NT
683 }
684 }
685
686 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
1ce52846 687 error_setg(errp, "Failed to read export length");
185b4338 688 goto fail;
b2e3d87f
NT
689 }
690 *size = be64_to_cpu(s);
b2e3d87f
NT
691 TRACE("Size is %" PRIu64, *size);
692
693 if (!name) {
694 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
1ce52846 695 error_setg(errp, "Failed to read export flags");
185b4338 696 goto fail;
b2e3d87f
NT
697 }
698 *flags = be32_to_cpup(flags);
699 } else {
700 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
1ce52846 701 error_setg(errp, "Failed to read export flags");
185b4338 702 goto fail;
b2e3d87f 703 }
48c7d80d 704 *flags |= be16_to_cpu(tmp);
b2e3d87f
NT
705 }
706 if (read_sync(csock, &buf, 124) != 124) {
1ce52846 707 error_setg(errp, "Failed to read reserved block");
185b4338 708 goto fail;
b2e3d87f 709 }
185b4338
PB
710 rc = 0;
711
712fail:
713 return rc;
cd831bd7 714}
7a5ca864 715
b90fb4b8 716#ifdef __linux__
3f472659 717int nbd_init(int fd, int csock, uint32_t flags, off_t size)
cd831bd7 718{
3e05c785
CL
719 TRACE("Setting NBD socket");
720
fc19f8a0 721 if (ioctl(fd, NBD_SET_SOCK, csock) < 0) {
3e05c785
CL
722 int serrno = errno;
723 LOG("Failed to set NBD socket");
185b4338 724 return -serrno;
3e05c785
CL
725 }
726
3f472659 727 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
7a5ca864 728
3f472659 729 if (ioctl(fd, NBD_SET_BLKSIZE, (size_t)BDRV_SECTOR_SIZE) < 0) {
b2e3d87f
NT
730 int serrno = errno;
731 LOG("Failed setting NBD block size");
185b4338 732 return -serrno;
b2e3d87f 733 }
7a5ca864 734
3f472659 735 TRACE("Setting size to %zd block(s)", (size_t)(size / BDRV_SECTOR_SIZE));
7a5ca864 736
d064d9f3 737 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, (size_t)(size / BDRV_SECTOR_SIZE)) < 0) {
b2e3d87f
NT
738 int serrno = errno;
739 LOG("Failed setting size (in blocks)");
185b4338 740 return -serrno;
b2e3d87f 741 }
7a5ca864 742
c8969ede
PB
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");
747
748 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
749 int serrno = errno;
750 LOG("Failed setting read-only attribute");
751 return -serrno;
752 }
753 } else {
b90fb4b8 754 int serrno = errno;
c8969ede 755 LOG("Failed setting flags");
185b4338 756 return -serrno;
b90fb4b8
PB
757 }
758 }
759
b2e3d87f 760 TRACE("Negotiation ended");
7a5ca864 761
b2e3d87f 762 return 0;
7a5ca864
FB
763}
764
765int nbd_disconnect(int fd)
766{
b2e3d87f
NT
767 ioctl(fd, NBD_CLEAR_QUE);
768 ioctl(fd, NBD_DISCONNECT);
769 ioctl(fd, NBD_CLEAR_SOCK);
770 return 0;
7a5ca864
FB
771}
772
0a4eb864 773int nbd_client(int fd)
7a5ca864 774{
b2e3d87f
NT
775 int ret;
776 int serrno;
7a5ca864 777
b2e3d87f 778 TRACE("Doing NBD loop");
7a5ca864 779
b2e3d87f 780 ret = ioctl(fd, NBD_DO_IT);
fc19f8a0 781 if (ret < 0 && errno == EPIPE) {
74624688
PB
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
784 * that case.
785 */
786 ret = 0;
787 }
b2e3d87f 788 serrno = errno;
7a5ca864 789
b2e3d87f 790 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
7a5ca864 791
b2e3d87f
NT
792 TRACE("Clearing NBD queue");
793 ioctl(fd, NBD_CLEAR_QUE);
7a5ca864 794
b2e3d87f
NT
795 TRACE("Clearing NBD socket");
796 ioctl(fd, NBD_CLEAR_SOCK);
7a5ca864 797
b2e3d87f
NT
798 errno = serrno;
799 return ret;
7a5ca864 800}
03ff3ca3 801#else
3f472659 802int nbd_init(int fd, int csock, uint32_t flags, off_t size)
03ff3ca3 803{
185b4338 804 return -ENOTSUP;
03ff3ca3
AL
805}
806
807int nbd_disconnect(int fd)
808{
185b4338 809 return -ENOTSUP;
03ff3ca3
AL
810}
811
0a4eb864 812int nbd_client(int fd)
03ff3ca3 813{
185b4338 814 return -ENOTSUP;
03ff3ca3
AL
815}
816#endif
7a5ca864 817
94e7340b 818ssize_t nbd_send_request(int csock, struct nbd_request *request)
7a5ca864 819{
fa26c26b 820 uint8_t buf[NBD_REQUEST_SIZE];
185b4338 821 ssize_t ret;
b2e3d87f
NT
822
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);
75818250 828
b2e3d87f
NT
829 TRACE("Sending request to client: "
830 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
831 request->from, request->len, request->handle, request->type);
832
185b4338
PB
833 ret = write_sync(csock, buf, sizeof(buf));
834 if (ret < 0) {
835 return ret;
836 }
837
838 if (ret != sizeof(buf)) {
b2e3d87f 839 LOG("writing to socket failed");
185b4338 840 return -EINVAL;
b2e3d87f
NT
841 }
842 return 0;
843}
75818250 844
94e7340b 845static ssize_t nbd_receive_request(int csock, struct nbd_request *request)
75818250 846{
fa26c26b 847 uint8_t buf[NBD_REQUEST_SIZE];
b2e3d87f 848 uint32_t magic;
185b4338 849 ssize_t ret;
b2e3d87f 850
185b4338
PB
851 ret = read_sync(csock, buf, sizeof(buf));
852 if (ret < 0) {
853 return ret;
854 }
855
856 if (ret != sizeof(buf)) {
b2e3d87f 857 LOG("read failed");
185b4338 858 return -EINVAL;
b2e3d87f
NT
859 }
860
861 /* Request
862 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
863 [ 4 .. 7] type (0 == READ, 1 == WRITE)
864 [ 8 .. 15] handle
865 [16 .. 23] from
866 [24 .. 27] len
867 */
868
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));
874
875 TRACE("Got request: "
876 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
877 magic, request->type, request->from, request->len);
878
879 if (magic != NBD_REQUEST_MAGIC) {
880 LOG("invalid magic (got 0x%x)", magic);
185b4338 881 return -EINVAL;
b2e3d87f
NT
882 }
883 return 0;
75818250
TS
884}
885
94e7340b 886ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply)
75818250 887{
b2e3d87f
NT
888 uint8_t buf[NBD_REPLY_SIZE];
889 uint32_t magic;
185b4338 890 ssize_t ret;
b2e3d87f 891
185b4338
PB
892 ret = read_sync(csock, buf, sizeof(buf));
893 if (ret < 0) {
894 return ret;
895 }
896
897 if (ret != sizeof(buf)) {
b2e3d87f 898 LOG("read failed");
185b4338 899 return -EINVAL;
b2e3d87f
NT
900 }
901
902 /* Reply
903 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
904 [ 4 .. 7] error (0 == no error)
905 [ 7 .. 15] handle
906 */
907
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));
911
ca441480
PB
912 reply->error = nbd_errno_to_system_errno(reply->error);
913
b2e3d87f
NT
914 TRACE("Got reply: "
915 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
916 magic, reply->error, reply->handle);
917
918 if (magic != NBD_REPLY_MAGIC) {
919 LOG("invalid magic (got 0x%x)", magic);
185b4338 920 return -EINVAL;
b2e3d87f
NT
921 }
922 return 0;
75818250
TS
923}
924
94e7340b 925static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply)
75818250 926{
fa26c26b 927 uint8_t buf[NBD_REPLY_SIZE];
185b4338 928 ssize_t ret;
b2e3d87f 929
ca441480
PB
930 reply->error = system_errno_to_nbd_errno(reply->error);
931
b2e3d87f
NT
932 /* Reply
933 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
934 [ 4 .. 7] error (0 == no error)
935 [ 7 .. 15] handle
936 */
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);
940
941 TRACE("Sending response to client");
942
185b4338
PB
943 ret = write_sync(csock, buf, sizeof(buf));
944 if (ret < 0) {
945 return ret;
946 }
947
948 if (ret != sizeof(buf)) {
b2e3d87f 949 LOG("writing to socket failed");
185b4338 950 return -EINVAL;
b2e3d87f
NT
951 }
952 return 0;
75818250 953}
7a5ca864 954
41996e38
PB
955#define MAX_NBD_REQUESTS 16
956
ce33967a 957void nbd_client_get(NBDClient *client)
1743b515
PB
958{
959 client->refcount++;
960}
961
ce33967a 962void nbd_client_put(NBDClient *client)
1743b515
PB
963{
964 if (--client->refcount == 0) {
ff2b68aa 965 /* The last reference should be dropped by client->close,
f53a829b 966 * which is called by client_close.
ff2b68aa
PB
967 */
968 assert(client->closing);
969
958c717d 970 nbd_unset_handlers(client);
ff2b68aa
PB
971 close(client->sock);
972 client->sock = -1;
6b8c01e7
PB
973 if (client->exp) {
974 QTAILQ_REMOVE(&client->exp->clients, client, next);
975 nbd_export_put(client->exp);
976 }
1743b515
PB
977 g_free(client);
978 }
979}
980
f53a829b 981static void client_close(NBDClient *client)
1743b515 982{
ff2b68aa
PB
983 if (client->closing) {
984 return;
985 }
986
987 client->closing = true;
988
989 /* Force requests to finish. They will drop their own references,
990 * then we'll close the socket and free the NBDClient.
991 */
992 shutdown(client->sock, 2);
993
994 /* Also tell the client, so that they release their reference. */
1743b515
PB
995 if (client->close) {
996 client->close(client);
997 }
1743b515
PB
998}
999
72deddc5 1000static NBDRequest *nbd_request_get(NBDClient *client)
d9a73806
PB
1001{
1002 NBDRequest *req;
72deddc5 1003
41996e38
PB
1004 assert(client->nb_requests <= MAX_NBD_REQUESTS - 1);
1005 client->nb_requests++;
958c717d 1006 nbd_update_can_read(client);
41996e38 1007
e1adb27a 1008 req = g_slice_new0(NBDRequest);
72deddc5
PB
1009 nbd_client_get(client);
1010 req->client = client;
d9a73806
PB
1011 return req;
1012}
1013
72deddc5 1014static void nbd_request_put(NBDRequest *req)
d9a73806 1015{
72deddc5 1016 NBDClient *client = req->client;
e1adb27a 1017
2d821488
SH
1018 if (req->data) {
1019 qemu_vfree(req->data);
1020 }
e1adb27a
SH
1021 g_slice_free(NBDRequest, req);
1022
958c717d
HR
1023 client->nb_requests--;
1024 nbd_update_can_read(client);
72deddc5 1025 nbd_client_put(client);
d9a73806
PB
1026}
1027
aadf99a7 1028static void blk_aio_attached(AioContext *ctx, void *opaque)
f2149281
HR
1029{
1030 NBDExport *exp = opaque;
1031 NBDClient *client;
1032
1033 TRACE("Export %s: Attaching clients to AIO context %p\n", exp->name, ctx);
1034
1035 exp->ctx = ctx;
1036
1037 QTAILQ_FOREACH(client, &exp->clients, next) {
1038 nbd_set_handlers(client);
1039 }
1040}
1041
aadf99a7 1042static void blk_aio_detach(void *opaque)
f2149281
HR
1043{
1044 NBDExport *exp = opaque;
1045 NBDClient *client;
1046
1047 TRACE("Export %s: Detaching clients from AIO context %p\n", exp->name, exp->ctx);
1048
1049 QTAILQ_FOREACH(client, &exp->clients, next) {
1050 nbd_unset_handlers(client);
1051 }
1052
1053 exp->ctx = NULL;
1054}
1055
e140177d 1056NBDExport *nbd_export_new(BlockBackend *blk, off_t dev_offset, off_t size,
98f44bbe
HR
1057 uint32_t nbdflags, void (*close)(NBDExport *),
1058 Error **errp)
af49bbbe
PB
1059{
1060 NBDExport *exp = g_malloc0(sizeof(NBDExport));
2c8d9f06 1061 exp->refcount = 1;
4b9441f6 1062 QTAILQ_INIT(&exp->clients);
aadf99a7 1063 exp->blk = blk;
af49bbbe
PB
1064 exp->dev_offset = dev_offset;
1065 exp->nbdflags = nbdflags;
98f44bbe
HR
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");
1070 goto fail;
1071 }
1072 exp->size -= exp->size % BDRV_SECTOR_SIZE;
1073
0ddf08db 1074 exp->close = close;
aadf99a7
HR
1075 exp->ctx = blk_get_aio_context(blk);
1076 blk_ref(blk);
1077 blk_add_aio_context_notifier(blk, blk_aio_attached, blk_aio_detach, exp);
7ea2d269
AK
1078 /*
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.
1082 */
aadf99a7 1083 blk_invalidate_cache(blk, NULL);
af49bbbe 1084 return exp;
98f44bbe
HR
1085
1086fail:
1087 g_free(exp);
1088 return NULL;
af49bbbe
PB
1089}
1090
ee0a19ec
PB
1091NBDExport *nbd_export_find(const char *name)
1092{
1093 NBDExport *exp;
1094 QTAILQ_FOREACH(exp, &exports, next) {
1095 if (strcmp(name, exp->name) == 0) {
1096 return exp;
1097 }
1098 }
1099
1100 return NULL;
1101}
1102
1103void nbd_export_set_name(NBDExport *exp, const char *name)
1104{
1105 if (exp->name == name) {
1106 return;
1107 }
1108
1109 nbd_export_get(exp);
1110 if (exp->name != NULL) {
1111 g_free(exp->name);
1112 exp->name = NULL;
1113 QTAILQ_REMOVE(&exports, exp, next);
1114 nbd_export_put(exp);
1115 }
1116 if (name != NULL) {
1117 nbd_export_get(exp);
1118 exp->name = g_strdup(name);
1119 QTAILQ_INSERT_TAIL(&exports, exp, next);
1120 }
1121 nbd_export_put(exp);
1122}
1123
af49bbbe
PB
1124void nbd_export_close(NBDExport *exp)
1125{
4b9441f6 1126 NBDClient *client, *next;
2c8d9f06 1127
4b9441f6
PB
1128 nbd_export_get(exp);
1129 QTAILQ_FOREACH_SAFE(client, &exp->clients, next, next) {
f53a829b 1130 client_close(client);
4b9441f6 1131 }
125afda8 1132 nbd_export_set_name(exp, NULL);
4b9441f6 1133 nbd_export_put(exp);
aadf99a7
HR
1134 if (exp->blk) {
1135 blk_remove_aio_context_notifier(exp->blk, blk_aio_attached,
1136 blk_aio_detach, exp);
1137 blk_unref(exp->blk);
1138 exp->blk = NULL;
38b54b6d 1139 }
2c8d9f06
PB
1140}
1141
1142void nbd_export_get(NBDExport *exp)
1143{
1144 assert(exp->refcount > 0);
1145 exp->refcount++;
1146}
1147
1148void nbd_export_put(NBDExport *exp)
1149{
1150 assert(exp->refcount > 0);
1151 if (exp->refcount == 1) {
1152 nbd_export_close(exp);
d9a73806
PB
1153 }
1154
2c8d9f06 1155 if (--exp->refcount == 0) {
ee0a19ec
PB
1156 assert(exp->name == NULL);
1157
0ddf08db
PB
1158 if (exp->close) {
1159 exp->close(exp);
1160 }
1161
2c8d9f06
PB
1162 g_free(exp);
1163 }
af49bbbe
PB
1164}
1165
e140177d 1166BlockBackend *nbd_export_get_blockdev(NBDExport *exp)
125afda8 1167{
aadf99a7 1168 return exp->blk;
125afda8
PB
1169}
1170
ee0a19ec
PB
1171void nbd_export_close_all(void)
1172{
1173 NBDExport *exp, *next;
1174
1175 QTAILQ_FOREACH_SAFE(exp, &exports, next, next) {
1176 nbd_export_close(exp);
ee0a19ec
PB
1177 }
1178}
1179
94e7340b
PB
1180static ssize_t nbd_co_send_reply(NBDRequest *req, struct nbd_reply *reply,
1181 int len)
22045592 1182{
72deddc5
PB
1183 NBDClient *client = req->client;
1184 int csock = client->sock;
94e7340b 1185 ssize_t rc, ret;
22045592 1186
262db388 1187 qemu_co_mutex_lock(&client->send_lock);
262db388 1188 client->send_coroutine = qemu_coroutine_self();
958c717d 1189 nbd_set_handlers(client);
262db388 1190
22045592
PB
1191 if (!len) {
1192 rc = nbd_send_reply(csock, reply);
22045592
PB
1193 } else {
1194 socket_set_cork(csock, 1);
1195 rc = nbd_send_reply(csock, reply);
fc19f8a0 1196 if (rc >= 0) {
262db388 1197 ret = qemu_co_send(csock, req->data, len);
22045592 1198 if (ret != len) {
185b4338 1199 rc = -EIO;
22045592
PB
1200 }
1201 }
22045592
PB
1202 socket_set_cork(csock, 0);
1203 }
262db388
PB
1204
1205 client->send_coroutine = NULL;
958c717d 1206 nbd_set_handlers(client);
262db388 1207 qemu_co_mutex_unlock(&client->send_lock);
22045592
PB
1208 return rc;
1209}
1210
94e7340b 1211static ssize_t nbd_co_receive_request(NBDRequest *req, struct nbd_request *request)
a030b347 1212{
72deddc5
PB
1213 NBDClient *client = req->client;
1214 int csock = client->sock;
2d821488 1215 uint32_t command;
94e7340b 1216 ssize_t rc;
a030b347 1217
262db388 1218 client->recv_coroutine = qemu_coroutine_self();
958c717d
HR
1219 nbd_update_can_read(client);
1220
7fe7b68b
PB
1221 rc = nbd_receive_request(csock, request);
1222 if (rc < 0) {
1223 if (rc != -EAGAIN) {
1224 rc = -EIO;
1225 }
a030b347
PB
1226 goto out;
1227 }
1228
2d821488 1229 if (request->len > NBD_MAX_BUFFER_SIZE) {
a030b347 1230 LOG("len (%u) is larger than max len (%u)",
2d821488 1231 request->len, NBD_MAX_BUFFER_SIZE);
a030b347
PB
1232 rc = -EINVAL;
1233 goto out;
1234 }
1235
1236 if ((request->from + request->len) < request->from) {
1237 LOG("integer overflow detected! "
1238 "you're probably being attacked");
1239 rc = -EINVAL;
1240 goto out;
1241 }
1242
1243 TRACE("Decoding type");
1244
2d821488
SH
1245 command = request->type & NBD_CMD_MASK_COMMAND;
1246 if (command == NBD_CMD_READ || command == NBD_CMD_WRITE) {
aadf99a7 1247 req->data = blk_blockalign(client->exp->blk, request->len);
2d821488
SH
1248 }
1249 if (command == NBD_CMD_WRITE) {
a030b347
PB
1250 TRACE("Reading %u byte(s)", request->len);
1251
262db388 1252 if (qemu_co_recv(csock, req->data, request->len) != request->len) {
a030b347
PB
1253 LOG("reading from socket failed");
1254 rc = -EIO;
1255 goto out;
1256 }
1257 }
1258 rc = 0;
1259
1260out:
262db388 1261 client->recv_coroutine = NULL;
958c717d
HR
1262 nbd_update_can_read(client);
1263
a030b347
PB
1264 return rc;
1265}
1266
262db388 1267static void nbd_trip(void *opaque)
75818250 1268{
262db388 1269 NBDClient *client = opaque;
1743b515 1270 NBDExport *exp = client->exp;
ff2b68aa 1271 NBDRequest *req;
b2e3d87f
NT
1272 struct nbd_request request;
1273 struct nbd_reply reply;
94e7340b 1274 ssize_t ret;
8c5d1abb 1275 uint32_t command;
b2e3d87f
NT
1276
1277 TRACE("Reading request.");
ff2b68aa
PB
1278 if (client->closing) {
1279 return;
1280 }
b2e3d87f 1281
ff2b68aa 1282 req = nbd_request_get(client);
262db388 1283 ret = nbd_co_receive_request(req, &request);
7fe7b68b
PB
1284 if (ret == -EAGAIN) {
1285 goto done;
1286 }
a030b347 1287 if (ret == -EIO) {
d9a73806 1288 goto out;
a030b347 1289 }
b2e3d87f 1290
fae69416
PB
1291 reply.handle = request.handle;
1292 reply.error = 0;
1293
a030b347
PB
1294 if (ret < 0) {
1295 reply.error = -ret;
1296 goto error_reply;
b2e3d87f 1297 }
8c5d1abb
HB
1298 command = request.type & NBD_CMD_MASK_COMMAND;
1299 if (command != NBD_CMD_DISC && (request.from + request.len) > exp->size) {
b2e3d87f
NT
1300 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
1301 ", Offset: %" PRIu64 "\n",
af49bbbe 1302 request.from, request.len,
0fee8f34 1303 (uint64_t)exp->size, (uint64_t)exp->dev_offset);
b2e3d87f 1304 LOG("requested operation past EOF--bad client?");
fae69416 1305 goto invalid_request;
b2e3d87f
NT
1306 }
1307
8c5d1abb 1308 switch (command) {
b2e3d87f
NT
1309 case NBD_CMD_READ:
1310 TRACE("Request type is READ");
1311
e25ceb76 1312 if (request.type & NBD_CMD_FLAG_FUA) {
aadf99a7 1313 ret = blk_co_flush(exp->blk);
e25ceb76
PB
1314 if (ret < 0) {
1315 LOG("flush failed");
1316 reply.error = -ret;
1317 goto error_reply;
1318 }
1319 }
1320
aadf99a7
HR
1321 ret = blk_read(exp->blk,
1322 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1323 req->data, request.len / BDRV_SECTOR_SIZE);
adcf6302 1324 if (ret < 0) {
b2e3d87f 1325 LOG("reading from file failed");
adcf6302 1326 reply.error = -ret;
fae69416 1327 goto error_reply;
b2e3d87f 1328 }
b2e3d87f
NT
1329
1330 TRACE("Read %u byte(s)", request.len);
262db388 1331 if (nbd_co_send_reply(req, &reply, request.len) < 0)
d9a73806 1332 goto out;
b2e3d87f
NT
1333 break;
1334 case NBD_CMD_WRITE:
1335 TRACE("Request type is WRITE");
1336
af49bbbe 1337 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
b2e3d87f 1338 TRACE("Server is read-only, return error");
fae69416
PB
1339 reply.error = EROFS;
1340 goto error_reply;
1341 }
1342
1343 TRACE("Writing to device");
1344
aadf99a7
HR
1345 ret = blk_write(exp->blk,
1346 (request.from + exp->dev_offset) / BDRV_SECTOR_SIZE,
1347 req->data, request.len / BDRV_SECTOR_SIZE);
fae69416
PB
1348 if (ret < 0) {
1349 LOG("writing to file failed");
1350 reply.error = -ret;
1351 goto error_reply;
1352 }
b2e3d87f 1353
fae69416 1354 if (request.type & NBD_CMD_FLAG_FUA) {
aadf99a7 1355 ret = blk_co_flush(exp->blk);
adcf6302 1356 if (ret < 0) {
fae69416 1357 LOG("flush failed");
adcf6302 1358 reply.error = -ret;
fae69416 1359 goto error_reply;
2c7989a9 1360 }
b2e3d87f
NT
1361 }
1362
fc19f8a0 1363 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1364 goto out;
fc19f8a0 1365 }
b2e3d87f
NT
1366 break;
1367 case NBD_CMD_DISC:
1368 TRACE("Request type is DISCONNECT");
1369 errno = 0;
262db388 1370 goto out;
1486d04a
PB
1371 case NBD_CMD_FLUSH:
1372 TRACE("Request type is FLUSH");
1373
aadf99a7 1374 ret = blk_co_flush(exp->blk);
1486d04a
PB
1375 if (ret < 0) {
1376 LOG("flush failed");
1377 reply.error = -ret;
1378 }
fc19f8a0 1379 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1380 goto out;
fc19f8a0 1381 }
7a706633
PB
1382 break;
1383 case NBD_CMD_TRIM:
1384 TRACE("Request type is TRIM");
aadf99a7
HR
1385 ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset)
1386 / BDRV_SECTOR_SIZE,
1387 request.len / BDRV_SECTOR_SIZE);
7a706633
PB
1388 if (ret < 0) {
1389 LOG("discard failed");
1390 reply.error = -ret;
1391 }
fc19f8a0 1392 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1393 goto out;
fc19f8a0 1394 }
1486d04a 1395 break;
b2e3d87f
NT
1396 default:
1397 LOG("invalid request type (%u) received", request.type);
fae69416 1398 invalid_request:
8b2f0abf 1399 reply.error = EINVAL;
fae69416 1400 error_reply:
fc19f8a0 1401 if (nbd_co_send_reply(req, &reply, 0) < 0) {
d9a73806 1402 goto out;
fc19f8a0 1403 }
fae69416 1404 break;
b2e3d87f
NT
1405 }
1406
1407 TRACE("Request/Reply complete");
1408
7fe7b68b 1409done:
262db388
PB
1410 nbd_request_put(req);
1411 return;
1412
d9a73806 1413out:
72deddc5 1414 nbd_request_put(req);
f53a829b 1415 client_close(client);
7a5ca864 1416}
af49bbbe 1417
1743b515
PB
1418static void nbd_read(void *opaque)
1419{
1420 NBDClient *client = opaque;
1421
262db388
PB
1422 if (client->recv_coroutine) {
1423 qemu_coroutine_enter(client->recv_coroutine, NULL);
1424 } else {
1425 qemu_coroutine_enter(qemu_coroutine_create(nbd_trip), client);
1743b515 1426 }
1743b515
PB
1427}
1428
262db388
PB
1429static void nbd_restart_write(void *opaque)
1430{
1431 NBDClient *client = opaque;
1432
1433 qemu_coroutine_enter(client->send_coroutine, NULL);
1434}
1435
958c717d
HR
1436static void nbd_set_handlers(NBDClient *client)
1437{
1438 if (client->exp && client->exp->ctx) {
1439 aio_set_fd_handler(client->exp->ctx, client->sock,
1440 client->can_read ? nbd_read : NULL,
1441 client->send_coroutine ? nbd_restart_write : NULL,
1442 client);
1443 }
1444}
1445
1446static void nbd_unset_handlers(NBDClient *client)
1447{
1448 if (client->exp && client->exp->ctx) {
1449 aio_set_fd_handler(client->exp->ctx, client->sock, NULL, NULL, NULL);
1450 }
1451}
1452
1453static void nbd_update_can_read(NBDClient *client)
1454{
1455 bool can_read = client->recv_coroutine ||
1456 client->nb_requests < MAX_NBD_REQUESTS;
1457
1458 if (can_read != client->can_read) {
1459 client->can_read = can_read;
1460 nbd_set_handlers(client);
1461
1462 /* There is no need to invoke aio_notify(), since aio_set_fd_handler()
1463 * in nbd_set_handlers() will have taken care of that */
1464 }
1465}
1466
1743b515
PB
1467NBDClient *nbd_client_new(NBDExport *exp, int csock,
1468 void (*close)(NBDClient *))
af49bbbe 1469{
1743b515 1470 NBDClient *client;
1743b515
PB
1471 client = g_malloc0(sizeof(NBDClient));
1472 client->refcount = 1;
1473 client->exp = exp;
1474 client->sock = csock;
958c717d 1475 client->can_read = true;
f5076b5a 1476 if (nbd_send_negotiate(client)) {
9a304d29
PB
1477 g_free(client);
1478 return NULL;
1479 }
1743b515 1480 client->close = close;
262db388 1481 qemu_co_mutex_init(&client->send_lock);
958c717d 1482 nbd_set_handlers(client);
2c8d9f06 1483
6b8c01e7
PB
1484 if (exp) {
1485 QTAILQ_INSERT_TAIL(&exp->clients, client, next);
1486 nbd_export_get(exp);
1487 }
1743b515 1488 return client;
af49bbbe 1489}