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