]> git.proxmox.com Git - qemu.git/blame - nbd.c
qemu-nbd: introduce NBDExport
[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
FB
22
23#include <errno.h>
24#include <string.h>
03ff3ca3 25#ifndef _WIN32
7a5ca864 26#include <sys/ioctl.h>
03ff3ca3 27#endif
5dc2eec9 28#if defined(__sun__) || defined(__HAIKU__)
7e00eb9b
AL
29#include <sys/ioccom.h>
30#endif
7a5ca864
FB
31#include <ctype.h>
32#include <inttypes.h>
75818250 33
b90fb4b8
PB
34#ifdef __linux__
35#include <linux/fs.h>
36#endif
37
03ff3ca3
AL
38#include "qemu_socket.h"
39
40//#define DEBUG_NBD
41
42#ifdef DEBUG_NBD
75818250 43#define TRACE(msg, ...) do { \
03ff3ca3 44 LOG(msg, ## __VA_ARGS__); \
75818250 45} while(0)
03ff3ca3
AL
46#else
47#define TRACE(msg, ...) \
48 do { } while (0)
49#endif
7a5ca864
FB
50
51#define LOG(msg, ...) do { \
52 fprintf(stderr, "%s:%s():L%d: " msg "\n", \
53 __FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__); \
54} while(0)
55
7a5ca864
FB
56/* This is all part of the "official" NBD API */
57
b2e3d87f 58#define NBD_REPLY_SIZE (4 + 4 + 8)
7a5ca864
FB
59#define NBD_REQUEST_MAGIC 0x25609513
60#define NBD_REPLY_MAGIC 0x67446698
61
62#define NBD_SET_SOCK _IO(0xab, 0)
63#define NBD_SET_BLKSIZE _IO(0xab, 1)
64#define NBD_SET_SIZE _IO(0xab, 2)
65#define NBD_DO_IT _IO(0xab, 3)
66#define NBD_CLEAR_SOCK _IO(0xab, 4)
67#define NBD_CLEAR_QUE _IO(0xab, 5)
b2e3d87f
NT
68#define NBD_PRINT_DEBUG _IO(0xab, 6)
69#define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
7a5ca864 70#define NBD_DISCONNECT _IO(0xab, 8)
bbb74edd
PB
71#define NBD_SET_TIMEOUT _IO(0xab, 9)
72#define NBD_SET_FLAGS _IO(0xab, 10)
7a5ca864 73
b2e3d87f 74#define NBD_OPT_EXPORT_NAME (1 << 0)
1d45f8b5 75
7a5ca864
FB
76/* That's all folks */
77
75818250
TS
78#define read_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, true)
79#define write_sync(fd, buffer, size) nbd_wr_sync(fd, buffer, size, false)
7a5ca864 80
75818250 81size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
7a5ca864
FB
82{
83 size_t offset = 0;
84
ae255e52
PB
85 if (qemu_in_coroutine()) {
86 if (do_read) {
87 return qemu_co_recv(fd, buffer, size);
88 } else {
89 return qemu_co_send(fd, buffer, size);
90 }
91 }
92
7a5ca864
FB
93 while (offset < size) {
94 ssize_t len;
95
96 if (do_read) {
00aa0040 97 len = qemu_recv(fd, buffer + offset, size - offset, 0);
7a5ca864 98 } else {
03ff3ca3 99 len = send(fd, buffer + offset, size - offset, 0);
7a5ca864
FB
100 }
101
03ff3ca3
AL
102 if (len == -1)
103 errno = socket_error();
104
7a5ca864 105 /* recoverable error */
75818250 106 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
7a5ca864
FB
107 continue;
108 }
109
110 /* eof */
111 if (len == 0) {
112 break;
113 }
114
115 /* unrecoverable error */
116 if (len == -1) {
117 return 0;
118 }
119
120 offset += len;
121 }
122
123 return offset;
124}
125
c12504ce
NT
126static void combine_addr(char *buf, size_t len, const char* address,
127 uint16_t port)
7a5ca864 128{
c12504ce
NT
129 /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
130 if (strstr(address, ":")) {
131 snprintf(buf, len, "[%s]:%u", address, port);
132 } else {
133 snprintf(buf, len, "%s:%u", address, port);
7a5ca864 134 }
7a5ca864
FB
135}
136
c12504ce 137int tcp_socket_outgoing(const char *address, uint16_t port)
7a5ca864 138{
c12504ce
NT
139 char address_and_port[128];
140 combine_addr(address_and_port, 128, address, port);
141 return tcp_socket_outgoing_spec(address_and_port);
7a5ca864
FB
142}
143
c12504ce 144int tcp_socket_outgoing_spec(const char *address_and_port)
cd831bd7 145{
c12504ce 146 return inet_connect(address_and_port, SOCK_STREAM);
cd831bd7
TS
147}
148
c12504ce 149int tcp_socket_incoming(const char *address, uint16_t port)
cd831bd7 150{
c12504ce
NT
151 char address_and_port[128];
152 combine_addr(address_and_port, 128, address, port);
153 return tcp_socket_incoming_spec(address_and_port);
154}
cd831bd7 155
c12504ce
NT
156int tcp_socket_incoming_spec(const char *address_and_port)
157{
158 char *ostr = NULL;
159 int olen = 0;
160 return inet_listen(address_and_port, ostr, olen, SOCK_STREAM, 0);
03ff3ca3 161}
c12504ce 162
03ff3ca3
AL
163int unix_socket_incoming(const char *path)
164{
c12504ce
NT
165 char *ostr = NULL;
166 int olen = 0;
167
168 return unix_listen(path, ostr, olen);
cd831bd7
TS
169}
170
03ff3ca3
AL
171int unix_socket_outgoing(const char *path)
172{
c12504ce 173 return unix_connect(path);
03ff3ca3 174}
cd831bd7 175
7a5ca864
FB
176/* Basic flow
177
178 Server Client
179
180 Negotiate
181 Request
182 Response
183 Request
184 Response
185 ...
186 ...
187 Request (type == 2)
188*/
189
af49bbbe 190static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
7a5ca864 191{
b2e3d87f
NT
192 char buf[8 + 8 + 8 + 128];
193
194 /* Negotiate
195 [ 0 .. 7] passwd ("NBDMAGIC")
196 [ 8 .. 15] magic (0x00420281861253)
197 [16 .. 23] size
b90fb4b8
PB
198 [24 .. 27] flags
199 [28 .. 151] reserved (0)
b2e3d87f
NT
200 */
201
202 TRACE("Beginning negotiation.");
203 memcpy(buf, "NBDMAGIC", 8);
204 cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL);
205 cpu_to_be64w((uint64_t*)(buf + 16), size);
2c7989a9 206 cpu_to_be32w((uint32_t*)(buf + 24),
7a706633
PB
207 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
208 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
b90fb4b8 209 memset(buf + 28, 0, 124);
b2e3d87f
NT
210
211 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
212 LOG("write failed");
213 errno = EINVAL;
214 return -1;
215 }
216
07f35073 217 TRACE("Negotiation succeeded.");
b2e3d87f
NT
218
219 return 0;
7a5ca864
FB
220}
221
1d45f8b5
LV
222int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
223 off_t *size, size_t *blocksize)
7a5ca864 224{
b2e3d87f
NT
225 char buf[256];
226 uint64_t magic, s;
227 uint16_t tmp;
228
07f35073 229 TRACE("Receiving negotiation.");
b2e3d87f
NT
230
231 if (read_sync(csock, buf, 8) != 8) {
232 LOG("read failed");
233 errno = EINVAL;
234 return -1;
235 }
236
237 buf[8] = '\0';
238 if (strlen(buf) == 0) {
239 LOG("server connection closed");
240 errno = EINVAL;
241 return -1;
242 }
243
244 TRACE("Magic is %c%c%c%c%c%c%c%c",
245 qemu_isprint(buf[0]) ? buf[0] : '.',
246 qemu_isprint(buf[1]) ? buf[1] : '.',
247 qemu_isprint(buf[2]) ? buf[2] : '.',
248 qemu_isprint(buf[3]) ? buf[3] : '.',
249 qemu_isprint(buf[4]) ? buf[4] : '.',
250 qemu_isprint(buf[5]) ? buf[5] : '.',
251 qemu_isprint(buf[6]) ? buf[6] : '.',
252 qemu_isprint(buf[7]) ? buf[7] : '.');
253
254 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
255 LOG("Invalid magic received");
256 errno = EINVAL;
257 return -1;
258 }
259
260 if (read_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
261 LOG("read failed");
262 errno = EINVAL;
263 return -1;
264 }
265 magic = be64_to_cpu(magic);
266 TRACE("Magic is 0x%" PRIx64, magic);
267
268 if (name) {
269 uint32_t reserved = 0;
270 uint32_t opt;
271 uint32_t namesize;
272
273 TRACE("Checking magic (opts_magic)");
274 if (magic != 0x49484156454F5054LL) {
275 LOG("Bad magic received");
276 errno = EINVAL;
277 return -1;
278 }
279 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
280 LOG("flags read failed");
281 errno = EINVAL;
282 return -1;
283 }
284 *flags = be16_to_cpu(tmp) << 16;
285 /* reserved for future use */
286 if (write_sync(csock, &reserved, sizeof(reserved)) !=
287 sizeof(reserved)) {
288 LOG("write failed (reserved)");
289 errno = EINVAL;
290 return -1;
291 }
292 /* write the export name */
293 magic = cpu_to_be64(magic);
294 if (write_sync(csock, &magic, sizeof(magic)) != sizeof(magic)) {
295 LOG("write failed (magic)");
296 errno = EINVAL;
297 return -1;
298 }
299 opt = cpu_to_be32(NBD_OPT_EXPORT_NAME);
300 if (write_sync(csock, &opt, sizeof(opt)) != sizeof(opt)) {
301 LOG("write failed (opt)");
302 errno = EINVAL;
303 return -1;
304 }
305 namesize = cpu_to_be32(strlen(name));
306 if (write_sync(csock, &namesize, sizeof(namesize)) !=
307 sizeof(namesize)) {
308 LOG("write failed (namesize)");
309 errno = EINVAL;
310 return -1;
311 }
312 if (write_sync(csock, (char*)name, strlen(name)) != strlen(name)) {
313 LOG("write failed (name)");
314 errno = EINVAL;
315 return -1;
316 }
317 } else {
318 TRACE("Checking magic (cli_magic)");
319
320 if (magic != 0x00420281861253LL) {
321 LOG("Bad magic received");
322 errno = EINVAL;
323 return -1;
324 }
325 }
326
327 if (read_sync(csock, &s, sizeof(s)) != sizeof(s)) {
328 LOG("read failed");
329 errno = EINVAL;
330 return -1;
331 }
332 *size = be64_to_cpu(s);
333 *blocksize = 1024;
334 TRACE("Size is %" PRIu64, *size);
335
336 if (!name) {
337 if (read_sync(csock, flags, sizeof(*flags)) != sizeof(*flags)) {
338 LOG("read failed (flags)");
339 errno = EINVAL;
340 return -1;
341 }
342 *flags = be32_to_cpup(flags);
343 } else {
344 if (read_sync(csock, &tmp, sizeof(tmp)) != sizeof(tmp)) {
345 LOG("read failed (tmp)");
346 errno = EINVAL;
347 return -1;
348 }
349 *flags |= be32_to_cpu(tmp);
350 }
351 if (read_sync(csock, &buf, 124) != 124) {
352 LOG("read failed (buf)");
353 errno = EINVAL;
354 return -1;
355 }
cd831bd7
TS
356 return 0;
357}
7a5ca864 358
b90fb4b8
PB
359#ifdef __linux__
360int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
cd831bd7 361{
3e05c785
CL
362 TRACE("Setting NBD socket");
363
364 if (ioctl(fd, NBD_SET_SOCK, csock) == -1) {
365 int serrno = errno;
366 LOG("Failed to set NBD socket");
367 errno = serrno;
368 return -1;
369 }
370
b2e3d87f 371 TRACE("Setting block size to %lu", (unsigned long)blocksize);
7a5ca864 372
b2e3d87f
NT
373 if (ioctl(fd, NBD_SET_BLKSIZE, blocksize) == -1) {
374 int serrno = errno;
375 LOG("Failed setting NBD block size");
376 errno = serrno;
377 return -1;
378 }
7a5ca864 379
0bfcd599 380 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
7a5ca864 381
b2e3d87f
NT
382 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, size / blocksize) == -1) {
383 int serrno = errno;
384 LOG("Failed setting size (in blocks)");
385 errno = serrno;
386 return -1;
387 }
7a5ca864 388
b90fb4b8
PB
389 if (flags & NBD_FLAG_READ_ONLY) {
390 int read_only = 1;
391 TRACE("Setting readonly attribute");
392
393 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
394 int serrno = errno;
395 LOG("Failed setting read-only attribute");
396 errno = serrno;
397 return -1;
398 }
399 }
400
973b3d0a
PB
401 if (ioctl(fd, NBD_SET_FLAGS, flags) < 0
402 && errno != ENOTTY) {
403 int serrno = errno;
404 LOG("Failed setting flags");
405 errno = serrno;
406 return -1;
407 }
408
b2e3d87f 409 TRACE("Negotiation ended");
7a5ca864 410
b2e3d87f 411 return 0;
7a5ca864
FB
412}
413
414int nbd_disconnect(int fd)
415{
b2e3d87f
NT
416 ioctl(fd, NBD_CLEAR_QUE);
417 ioctl(fd, NBD_DISCONNECT);
418 ioctl(fd, NBD_CLEAR_SOCK);
419 return 0;
7a5ca864
FB
420}
421
0a4eb864 422int nbd_client(int fd)
7a5ca864 423{
b2e3d87f
NT
424 int ret;
425 int serrno;
7a5ca864 426
b2e3d87f 427 TRACE("Doing NBD loop");
7a5ca864 428
b2e3d87f 429 ret = ioctl(fd, NBD_DO_IT);
74624688
PB
430 if (ret == -1 && errno == EPIPE) {
431 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
432 * the socket via NBD_DISCONNECT. We do not want to return 1 in
433 * that case.
434 */
435 ret = 0;
436 }
b2e3d87f 437 serrno = errno;
7a5ca864 438
b2e3d87f 439 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
7a5ca864 440
b2e3d87f
NT
441 TRACE("Clearing NBD queue");
442 ioctl(fd, NBD_CLEAR_QUE);
7a5ca864 443
b2e3d87f
NT
444 TRACE("Clearing NBD socket");
445 ioctl(fd, NBD_CLEAR_SOCK);
7a5ca864 446
b2e3d87f
NT
447 errno = serrno;
448 return ret;
7a5ca864 449}
03ff3ca3 450#else
8e72506e 451int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
03ff3ca3
AL
452{
453 errno = ENOTSUP;
454 return -1;
455}
456
457int nbd_disconnect(int fd)
458{
459 errno = ENOTSUP;
460 return -1;
461}
462
0a4eb864 463int nbd_client(int fd)
03ff3ca3
AL
464{
465 errno = ENOTSUP;
466 return -1;
467}
468#endif
7a5ca864 469
75818250 470int nbd_send_request(int csock, struct nbd_request *request)
7a5ca864 471{
b2e3d87f
NT
472 uint8_t buf[4 + 4 + 8 + 8 + 4];
473
474 cpu_to_be32w((uint32_t*)buf, NBD_REQUEST_MAGIC);
475 cpu_to_be32w((uint32_t*)(buf + 4), request->type);
476 cpu_to_be64w((uint64_t*)(buf + 8), request->handle);
477 cpu_to_be64w((uint64_t*)(buf + 16), request->from);
478 cpu_to_be32w((uint32_t*)(buf + 24), request->len);
75818250 479
b2e3d87f
NT
480 TRACE("Sending request to client: "
481 "{ .from = %" PRIu64", .len = %u, .handle = %" PRIu64", .type=%i}",
482 request->from, request->len, request->handle, request->type);
483
484 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
485 LOG("writing to socket failed");
486 errno = EINVAL;
487 return -1;
488 }
489 return 0;
490}
75818250
TS
491
492static int nbd_receive_request(int csock, struct nbd_request *request)
493{
b2e3d87f
NT
494 uint8_t buf[4 + 4 + 8 + 8 + 4];
495 uint32_t magic;
496
497 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
498 LOG("read failed");
499 errno = EINVAL;
500 return -1;
501 }
502
503 /* Request
504 [ 0 .. 3] magic (NBD_REQUEST_MAGIC)
505 [ 4 .. 7] type (0 == READ, 1 == WRITE)
506 [ 8 .. 15] handle
507 [16 .. 23] from
508 [24 .. 27] len
509 */
510
511 magic = be32_to_cpup((uint32_t*)buf);
512 request->type = be32_to_cpup((uint32_t*)(buf + 4));
513 request->handle = be64_to_cpup((uint64_t*)(buf + 8));
514 request->from = be64_to_cpup((uint64_t*)(buf + 16));
515 request->len = be32_to_cpup((uint32_t*)(buf + 24));
516
517 TRACE("Got request: "
518 "{ magic = 0x%x, .type = %d, from = %" PRIu64" , len = %u }",
519 magic, request->type, request->from, request->len);
520
521 if (magic != NBD_REQUEST_MAGIC) {
522 LOG("invalid magic (got 0x%x)", magic);
523 errno = EINVAL;
524 return -1;
525 }
526 return 0;
75818250
TS
527}
528
529int nbd_receive_reply(int csock, struct nbd_reply *reply)
530{
b2e3d87f
NT
531 uint8_t buf[NBD_REPLY_SIZE];
532 uint32_t magic;
533
534 memset(buf, 0xAA, sizeof(buf));
535
536 if (read_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
537 LOG("read failed");
538 errno = EINVAL;
539 return -1;
540 }
541
542 /* Reply
543 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
544 [ 4 .. 7] error (0 == no error)
545 [ 7 .. 15] handle
546 */
547
548 magic = be32_to_cpup((uint32_t*)buf);
549 reply->error = be32_to_cpup((uint32_t*)(buf + 4));
550 reply->handle = be64_to_cpup((uint64_t*)(buf + 8));
551
552 TRACE("Got reply: "
553 "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }",
554 magic, reply->error, reply->handle);
555
556 if (magic != NBD_REPLY_MAGIC) {
557 LOG("invalid magic (got 0x%x)", magic);
558 errno = EINVAL;
559 return -1;
560 }
561 return 0;
75818250
TS
562}
563
564static int nbd_send_reply(int csock, struct nbd_reply *reply)
565{
b2e3d87f
NT
566 uint8_t buf[4 + 4 + 8];
567
568 /* Reply
569 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
570 [ 4 .. 7] error (0 == no error)
571 [ 7 .. 15] handle
572 */
573 cpu_to_be32w((uint32_t*)buf, NBD_REPLY_MAGIC);
574 cpu_to_be32w((uint32_t*)(buf + 4), reply->error);
575 cpu_to_be64w((uint64_t*)(buf + 8), reply->handle);
576
577 TRACE("Sending response to client");
578
579 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
580 LOG("writing to socket failed");
581 errno = EINVAL;
582 return -1;
583 }
584 return 0;
75818250 585}
7a5ca864 586
af49bbbe
PB
587struct NBDExport {
588 BlockDriverState *bs;
589 off_t dev_offset;
590 off_t size;
591 uint8_t *data;
592 uint32_t nbdflags;
593};
594
595NBDExport *nbd_export_new(BlockDriverState *bs, off_t dev_offset,
596 off_t size, uint32_t nbdflags)
597{
598 NBDExport *exp = g_malloc0(sizeof(NBDExport));
599 exp->bs = bs;
600 exp->dev_offset = dev_offset;
601 exp->nbdflags = nbdflags;
602 exp->size = size == -1 ? exp->bs->total_sectors * 512 : size;
603 exp->data = qemu_blockalign(exp->bs, NBD_BUFFER_SIZE);
604 return exp;
605}
606
607void nbd_export_close(NBDExport *exp)
608{
609 qemu_vfree(exp->data);
610 bdrv_close(exp->bs);
611 g_free(exp);
612}
613
22045592
PB
614static int nbd_do_send_reply(int csock, struct nbd_reply *reply,
615 uint8_t *data, int len)
616{
617 int rc, ret;
618
619 if (!len) {
620 rc = nbd_send_reply(csock, reply);
621 if (rc == -1) {
622 rc = -errno;
623 }
624 } else {
625 socket_set_cork(csock, 1);
626 rc = nbd_send_reply(csock, reply);
627 if (rc != -1) {
628 ret = write_sync(csock, data, len);
629 if (ret != len) {
630 errno = EIO;
631 rc = -1;
632 }
633 }
634 if (rc == -1) {
635 rc = -errno;
636 }
637 socket_set_cork(csock, 0);
638 }
639 return rc;
640}
641
a030b347
PB
642static int nbd_do_receive_request(int csock, struct nbd_request *request,
643 uint8_t *data)
644{
645 int rc;
646
647 if (nbd_receive_request(csock, request) == -1) {
648 rc = -EIO;
649 goto out;
650 }
651
652 if (request->len > NBD_BUFFER_SIZE) {
653 LOG("len (%u) is larger than max len (%u)",
654 request->len, NBD_BUFFER_SIZE);
655 rc = -EINVAL;
656 goto out;
657 }
658
659 if ((request->from + request->len) < request->from) {
660 LOG("integer overflow detected! "
661 "you're probably being attacked");
662 rc = -EINVAL;
663 goto out;
664 }
665
666 TRACE("Decoding type");
667
668 if ((request->type & NBD_CMD_MASK_COMMAND) == NBD_CMD_WRITE) {
669 TRACE("Reading %u byte(s)", request->len);
670
671 if (read_sync(csock, data, request->len) != request->len) {
672 LOG("reading from socket failed");
673 rc = -EIO;
674 goto out;
675 }
676 }
677 rc = 0;
678
679out:
680 return rc;
681}
682
af49bbbe 683int nbd_trip(NBDExport *exp, int csock)
75818250 684{
b2e3d87f
NT
685 struct nbd_request request;
686 struct nbd_reply reply;
adcf6302 687 int ret;
b2e3d87f
NT
688
689 TRACE("Reading request.");
690
af49bbbe 691 ret = nbd_do_receive_request(csock, &request, exp->data);
a030b347 692 if (ret == -EIO) {
b2e3d87f 693 return -1;
a030b347 694 }
b2e3d87f 695
fae69416
PB
696 reply.handle = request.handle;
697 reply.error = 0;
698
a030b347
PB
699 if (ret < 0) {
700 reply.error = -ret;
701 goto error_reply;
b2e3d87f
NT
702 }
703
af49bbbe 704 if ((request.from + request.len) > exp->size) {
b2e3d87f
NT
705 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
706 ", Offset: %" PRIu64 "\n",
af49bbbe
PB
707 request.from, request.len,
708 (uint64_t)exp->size, exp->dev_offset);
b2e3d87f 709 LOG("requested operation past EOF--bad client?");
fae69416 710 goto invalid_request;
b2e3d87f
NT
711 }
712
2c7989a9 713 switch (request.type & NBD_CMD_MASK_COMMAND) {
b2e3d87f
NT
714 case NBD_CMD_READ:
715 TRACE("Request type is READ");
716
af49bbbe
PB
717 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
718 exp->data, request.len / 512);
adcf6302 719 if (ret < 0) {
b2e3d87f 720 LOG("reading from file failed");
adcf6302 721 reply.error = -ret;
fae69416 722 goto error_reply;
b2e3d87f 723 }
b2e3d87f
NT
724
725 TRACE("Read %u byte(s)", request.len);
af49bbbe 726 if (nbd_do_send_reply(csock, &reply, exp->data, request.len) < 0)
a478f6e5 727 return -1;
b2e3d87f
NT
728 break;
729 case NBD_CMD_WRITE:
730 TRACE("Request type is WRITE");
731
af49bbbe 732 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
b2e3d87f 733 TRACE("Server is read-only, return error");
fae69416
PB
734 reply.error = EROFS;
735 goto error_reply;
736 }
737
738 TRACE("Writing to device");
739
af49bbbe
PB
740 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
741 exp->data, request.len / 512);
fae69416
PB
742 if (ret < 0) {
743 LOG("writing to file failed");
744 reply.error = -ret;
745 goto error_reply;
746 }
b2e3d87f 747
fae69416 748 if (request.type & NBD_CMD_FLAG_FUA) {
af49bbbe 749 ret = bdrv_flush(exp->bs);
adcf6302 750 if (ret < 0) {
fae69416 751 LOG("flush failed");
adcf6302 752 reply.error = -ret;
fae69416 753 goto error_reply;
2c7989a9 754 }
b2e3d87f
NT
755 }
756
22045592 757 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
b2e3d87f
NT
758 return -1;
759 break;
760 case NBD_CMD_DISC:
761 TRACE("Request type is DISCONNECT");
762 errno = 0;
763 return 1;
1486d04a
PB
764 case NBD_CMD_FLUSH:
765 TRACE("Request type is FLUSH");
766
af49bbbe 767 ret = bdrv_flush(exp->bs);
1486d04a
PB
768 if (ret < 0) {
769 LOG("flush failed");
770 reply.error = -ret;
771 }
772
22045592 773 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
7a706633
PB
774 return -1;
775 break;
776 case NBD_CMD_TRIM:
777 TRACE("Request type is TRIM");
af49bbbe 778 ret = bdrv_discard(exp->bs, (request.from + exp->dev_offset) / 512,
7a706633
PB
779 request.len / 512);
780 if (ret < 0) {
781 LOG("discard failed");
782 reply.error = -ret;
783 }
22045592 784 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
1486d04a
PB
785 return -1;
786 break;
b2e3d87f
NT
787 default:
788 LOG("invalid request type (%u) received", request.type);
fae69416
PB
789 invalid_request:
790 reply.error = -EINVAL;
791 error_reply:
792 if (nbd_do_send_reply(csock, &reply, NULL, 0) == -1)
793 return -1;
794 break;
b2e3d87f
NT
795 }
796
797 TRACE("Request/Reply complete");
798
799 return 0;
7a5ca864 800}
af49bbbe
PB
801
802int nbd_negotiate(NBDExport *exp, int csock)
803{
804 return nbd_send_negotiate(csock, exp->size, exp->nbdflags);
805}