]> git.proxmox.com Git - qemu.git/blob - nbd.c
8d2a3bc1138e523545e8935dc7d2cc3f4112aa15
[qemu.git] / nbd.c
1 /*
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
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "nbd.h"
20 #include "block.h"
21 #include "block_int.h"
22
23 #include <errno.h>
24 #include <string.h>
25 #ifndef _WIN32
26 #include <sys/ioctl.h>
27 #endif
28 #if defined(__sun__) || defined(__HAIKU__)
29 #include <sys/ioccom.h>
30 #endif
31 #include <ctype.h>
32 #include <inttypes.h>
33
34 #ifdef __linux__
35 #include <linux/fs.h>
36 #endif
37
38 #include "qemu_socket.h"
39
40 //#define DEBUG_NBD
41
42 #ifdef DEBUG_NBD
43 #define TRACE(msg, ...) do { \
44 LOG(msg, ## __VA_ARGS__); \
45 } while(0)
46 #else
47 #define TRACE(msg, ...) \
48 do { } while (0)
49 #endif
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
56 /* This is all part of the "official" NBD API */
57
58 #define NBD_REPLY_SIZE (4 + 4 + 8)
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)
68 #define NBD_PRINT_DEBUG _IO(0xab, 6)
69 #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
70 #define NBD_DISCONNECT _IO(0xab, 8)
71 #define NBD_SET_TIMEOUT _IO(0xab, 9)
72 #define NBD_SET_FLAGS _IO(0xab, 10)
73
74 #define NBD_OPT_EXPORT_NAME (1 << 0)
75
76 /* That's all folks */
77
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)
80
81 size_t nbd_wr_sync(int fd, void *buffer, size_t size, bool do_read)
82 {
83 size_t offset = 0;
84
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
93 while (offset < size) {
94 ssize_t len;
95
96 if (do_read) {
97 len = qemu_recv(fd, buffer + offset, size - offset, 0);
98 } else {
99 len = send(fd, buffer + offset, size - offset, 0);
100 }
101
102 if (len == -1)
103 errno = socket_error();
104
105 /* recoverable error */
106 if (len == -1 && (errno == EAGAIN || errno == EINTR)) {
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
126 static void combine_addr(char *buf, size_t len, const char* address,
127 uint16_t port)
128 {
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);
134 }
135 }
136
137 int tcp_socket_outgoing(const char *address, uint16_t port)
138 {
139 char address_and_port[128];
140 combine_addr(address_and_port, 128, address, port);
141 return tcp_socket_outgoing_spec(address_and_port);
142 }
143
144 int tcp_socket_outgoing_spec(const char *address_and_port)
145 {
146 return inet_connect(address_and_port, SOCK_STREAM);
147 }
148
149 int tcp_socket_incoming(const char *address, uint16_t port)
150 {
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 }
155
156 int 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);
161 }
162
163 int unix_socket_incoming(const char *path)
164 {
165 char *ostr = NULL;
166 int olen = 0;
167
168 return unix_listen(path, ostr, olen);
169 }
170
171 int unix_socket_outgoing(const char *path)
172 {
173 return unix_connect(path);
174 }
175
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
190 static int nbd_send_negotiate(int csock, off_t size, uint32_t flags)
191 {
192 char buf[8 + 8 + 8 + 128];
193
194 /* Negotiate
195 [ 0 .. 7] passwd ("NBDMAGIC")
196 [ 8 .. 15] magic (0x00420281861253)
197 [16 .. 23] size
198 [24 .. 27] flags
199 [28 .. 151] reserved (0)
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);
206 cpu_to_be32w((uint32_t*)(buf + 24),
207 flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM |
208 NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA);
209 memset(buf + 28, 0, 124);
210
211 if (write_sync(csock, buf, sizeof(buf)) != sizeof(buf)) {
212 LOG("write failed");
213 errno = EINVAL;
214 return -1;
215 }
216
217 TRACE("Negotiation succeeded.");
218
219 return 0;
220 }
221
222 int nbd_receive_negotiate(int csock, const char *name, uint32_t *flags,
223 off_t *size, size_t *blocksize)
224 {
225 char buf[256];
226 uint64_t magic, s;
227 uint16_t tmp;
228
229 TRACE("Receiving negotiation.");
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 }
356 return 0;
357 }
358
359 #ifdef __linux__
360 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
361 {
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
371 TRACE("Setting block size to %lu", (unsigned long)blocksize);
372
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 }
379
380 TRACE("Setting size to %zd block(s)", (size_t)(size / blocksize));
381
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 }
388
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
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
409 TRACE("Negotiation ended");
410
411 return 0;
412 }
413
414 int nbd_disconnect(int fd)
415 {
416 ioctl(fd, NBD_CLEAR_QUE);
417 ioctl(fd, NBD_DISCONNECT);
418 ioctl(fd, NBD_CLEAR_SOCK);
419 return 0;
420 }
421
422 int nbd_client(int fd)
423 {
424 int ret;
425 int serrno;
426
427 TRACE("Doing NBD loop");
428
429 ret = ioctl(fd, NBD_DO_IT);
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 }
437 serrno = errno;
438
439 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
440
441 TRACE("Clearing NBD queue");
442 ioctl(fd, NBD_CLEAR_QUE);
443
444 TRACE("Clearing NBD socket");
445 ioctl(fd, NBD_CLEAR_SOCK);
446
447 errno = serrno;
448 return ret;
449 }
450 #else
451 int nbd_init(int fd, int csock, uint32_t flags, off_t size, size_t blocksize)
452 {
453 errno = ENOTSUP;
454 return -1;
455 }
456
457 int nbd_disconnect(int fd)
458 {
459 errno = ENOTSUP;
460 return -1;
461 }
462
463 int nbd_client(int fd)
464 {
465 errno = ENOTSUP;
466 return -1;
467 }
468 #endif
469
470 int nbd_send_request(int csock, struct nbd_request *request)
471 {
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);
479
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 }
491
492 static int nbd_receive_request(int csock, struct nbd_request *request)
493 {
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;
527 }
528
529 int nbd_receive_reply(int csock, struct nbd_reply *reply)
530 {
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;
562 }
563
564 static int nbd_send_reply(int csock, struct nbd_reply *reply)
565 {
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;
585 }
586
587 struct NBDExport {
588 BlockDriverState *bs;
589 off_t dev_offset;
590 off_t size;
591 uint8_t *data;
592 uint32_t nbdflags;
593 };
594
595 NBDExport *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
607 void nbd_export_close(NBDExport *exp)
608 {
609 qemu_vfree(exp->data);
610 bdrv_close(exp->bs);
611 g_free(exp);
612 }
613
614 static 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
642 static 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
679 out:
680 return rc;
681 }
682
683 int nbd_trip(NBDExport *exp, int csock)
684 {
685 struct nbd_request request;
686 struct nbd_reply reply;
687 int ret;
688
689 TRACE("Reading request.");
690
691 ret = nbd_do_receive_request(csock, &request, exp->data);
692 if (ret == -EIO) {
693 return -1;
694 }
695
696 reply.handle = request.handle;
697 reply.error = 0;
698
699 if (ret < 0) {
700 reply.error = -ret;
701 goto error_reply;
702 }
703
704 if ((request.from + request.len) > exp->size) {
705 LOG("From: %" PRIu64 ", Len: %u, Size: %" PRIu64
706 ", Offset: %" PRIu64 "\n",
707 request.from, request.len,
708 (uint64_t)exp->size, exp->dev_offset);
709 LOG("requested operation past EOF--bad client?");
710 goto invalid_request;
711 }
712
713 switch (request.type & NBD_CMD_MASK_COMMAND) {
714 case NBD_CMD_READ:
715 TRACE("Request type is READ");
716
717 ret = bdrv_read(exp->bs, (request.from + exp->dev_offset) / 512,
718 exp->data, request.len / 512);
719 if (ret < 0) {
720 LOG("reading from file failed");
721 reply.error = -ret;
722 goto error_reply;
723 }
724
725 TRACE("Read %u byte(s)", request.len);
726 if (nbd_do_send_reply(csock, &reply, exp->data, request.len) < 0)
727 return -1;
728 break;
729 case NBD_CMD_WRITE:
730 TRACE("Request type is WRITE");
731
732 if (exp->nbdflags & NBD_FLAG_READ_ONLY) {
733 TRACE("Server is read-only, return error");
734 reply.error = EROFS;
735 goto error_reply;
736 }
737
738 TRACE("Writing to device");
739
740 ret = bdrv_write(exp->bs, (request.from + exp->dev_offset) / 512,
741 exp->data, request.len / 512);
742 if (ret < 0) {
743 LOG("writing to file failed");
744 reply.error = -ret;
745 goto error_reply;
746 }
747
748 if (request.type & NBD_CMD_FLAG_FUA) {
749 ret = bdrv_flush(exp->bs);
750 if (ret < 0) {
751 LOG("flush failed");
752 reply.error = -ret;
753 goto error_reply;
754 }
755 }
756
757 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
758 return -1;
759 break;
760 case NBD_CMD_DISC:
761 TRACE("Request type is DISCONNECT");
762 errno = 0;
763 return 1;
764 case NBD_CMD_FLUSH:
765 TRACE("Request type is FLUSH");
766
767 ret = bdrv_flush(exp->bs);
768 if (ret < 0) {
769 LOG("flush failed");
770 reply.error = -ret;
771 }
772
773 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
774 return -1;
775 break;
776 case NBD_CMD_TRIM:
777 TRACE("Request type is TRIM");
778 ret = bdrv_discard(exp->bs, (request.from + exp->dev_offset) / 512,
779 request.len / 512);
780 if (ret < 0) {
781 LOG("discard failed");
782 reply.error = -ret;
783 }
784 if (nbd_do_send_reply(csock, &reply, NULL, 0) < 0)
785 return -1;
786 break;
787 default:
788 LOG("invalid request type (%u) received", request.type);
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;
795 }
796
797 TRACE("Request/Reply complete");
798
799 return 0;
800 }
801
802 int nbd_negotiate(NBDExport *exp, int csock)
803 {
804 return nbd_send_negotiate(csock, exp->size, exp->nbdflags);
805 }