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