]> git.proxmox.com Git - mirror_qemu.git/blob - nbd/client.c
nbd: Let server know when client gives up negotiation
[mirror_qemu.git] / nbd / client.c
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
4 *
5 * Network Block Device Client Side
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; under version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "nbd-internal.h"
23
24 static int nbd_errno_to_system_errno(int err)
25 {
26 switch (err) {
27 case NBD_SUCCESS:
28 return 0;
29 case NBD_EPERM:
30 return EPERM;
31 case NBD_EIO:
32 return EIO;
33 case NBD_ENOMEM:
34 return ENOMEM;
35 case NBD_ENOSPC:
36 return ENOSPC;
37 default:
38 TRACE("Squashing unexpected error %d to EINVAL", err);
39 /* fallthrough */
40 case NBD_EINVAL:
41 return EINVAL;
42 }
43 }
44
45 /* Definitions for opaque data types */
46
47 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
48
49 /* That's all folks */
50
51 /* Basic flow for negotiation
52
53 Server Client
54 Negotiate
55
56 or
57
58 Server Client
59 Negotiate #1
60 Option
61 Negotiate #2
62
63 ----
64
65 followed by
66
67 Server Client
68 Request
69 Response
70 Request
71 Response
72 ...
73 ...
74 Request (type == 2)
75
76 */
77
78 /* Send an option request.
79 *
80 * The request is for option @opt, with @data containing @len bytes of
81 * additional payload for the request (@len may be -1 to treat @data as
82 * a C string; and @data may be NULL if @len is 0).
83 * Return 0 if successful, -1 with errp set if it is impossible to
84 * continue. */
85 static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
86 uint32_t len, const char *data,
87 Error **errp)
88 {
89 nbd_option req;
90 QEMU_BUILD_BUG_ON(sizeof(req) != 16);
91
92 if (len == -1) {
93 req.length = len = strlen(data);
94 }
95 TRACE("Sending option request %" PRIu32", len %" PRIu32, opt, len);
96
97 stq_be_p(&req.magic, NBD_OPTS_MAGIC);
98 stl_be_p(&req.option, opt);
99 stl_be_p(&req.length, len);
100
101 if (write_sync(ioc, &req, sizeof(req)) != sizeof(req)) {
102 error_setg(errp, "Failed to send option request header");
103 return -1;
104 }
105
106 if (len && write_sync(ioc, (char *) data, len) != len) {
107 error_setg(errp, "Failed to send option request data");
108 return -1;
109 }
110
111 return 0;
112 }
113
114 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
115 * not going to attempt further negotiation. */
116 static void nbd_send_opt_abort(QIOChannel *ioc)
117 {
118 /* Technically, a compliant server is supposed to reply to us; but
119 * older servers disconnected instead. At any rate, we're allowed
120 * to disconnect without waiting for the server reply, so we don't
121 * even care if the request makes it to the server, let alone
122 * waiting around for whether the server replies. */
123 nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
124 }
125
126
127 /* Receive the header of an option reply, which should match the given
128 * opt. Read through the length field, but NOT the length bytes of
129 * payload. Return 0 if successful, -1 with errp set if it is
130 * impossible to continue. */
131 static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
132 nbd_opt_reply *reply, Error **errp)
133 {
134 QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
135 if (read_sync(ioc, reply, sizeof(*reply)) != sizeof(*reply)) {
136 error_setg(errp, "failed to read option reply");
137 nbd_send_opt_abort(ioc);
138 return -1;
139 }
140 be64_to_cpus(&reply->magic);
141 be32_to_cpus(&reply->option);
142 be32_to_cpus(&reply->type);
143 be32_to_cpus(&reply->length);
144
145 TRACE("Received option reply %" PRIx32", type %" PRIx32", len %" PRIu32,
146 reply->option, reply->type, reply->length);
147
148 if (reply->magic != NBD_REP_MAGIC) {
149 error_setg(errp, "Unexpected option reply magic");
150 nbd_send_opt_abort(ioc);
151 return -1;
152 }
153 if (reply->option != opt) {
154 error_setg(errp, "Unexpected option type %x expected %x",
155 reply->option, opt);
156 nbd_send_opt_abort(ioc);
157 return -1;
158 }
159 return 0;
160 }
161
162 /* If reply represents success, return 1 without further action.
163 * If reply represents an error, consume the optional payload of
164 * the packet on ioc. Then return 0 for unsupported (so the client
165 * can fall back to other approaches), or -1 with errp set for other
166 * errors.
167 */
168 static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
169 Error **errp)
170 {
171 char *msg = NULL;
172 int result = -1;
173
174 if (!(reply->type & (1 << 31))) {
175 return 1;
176 }
177
178 if (reply->length) {
179 if (reply->length > NBD_MAX_BUFFER_SIZE) {
180 error_setg(errp, "server's error message is too long");
181 goto cleanup;
182 }
183 msg = g_malloc(reply->length + 1);
184 if (read_sync(ioc, msg, reply->length) != reply->length) {
185 error_setg(errp, "failed to read option error message");
186 goto cleanup;
187 }
188 msg[reply->length] = '\0';
189 }
190
191 switch (reply->type) {
192 case NBD_REP_ERR_UNSUP:
193 TRACE("server doesn't understand request %" PRIx32
194 ", attempting fallback", reply->option);
195 result = 0;
196 goto cleanup;
197
198 case NBD_REP_ERR_POLICY:
199 error_setg(errp, "Denied by server for option %" PRIx32,
200 reply->option);
201 break;
202
203 case NBD_REP_ERR_INVALID:
204 error_setg(errp, "Invalid data length for option %" PRIx32,
205 reply->option);
206 break;
207
208 case NBD_REP_ERR_TLS_REQD:
209 error_setg(errp, "TLS negotiation required before option %" PRIx32,
210 reply->option);
211 break;
212
213 default:
214 error_setg(errp, "Unknown error code when asking for option %" PRIx32,
215 reply->option);
216 break;
217 }
218
219 if (msg) {
220 error_append_hint(errp, "%s\n", msg);
221 }
222
223 cleanup:
224 g_free(msg);
225 if (result < 0) {
226 nbd_send_opt_abort(ioc);
227 }
228 return result;
229 }
230
231 static int nbd_receive_list(QIOChannel *ioc, char **name, Error **errp)
232 {
233 nbd_opt_reply reply;
234 uint32_t len;
235 uint32_t namelen;
236 int error;
237
238 *name = NULL;
239 if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
240 return -1;
241 }
242 error = nbd_handle_reply_err(ioc, &reply, errp);
243 if (error <= 0) {
244 return error;
245 }
246 len = reply.length;
247
248 if (reply.type == NBD_REP_ACK) {
249 if (len != 0) {
250 error_setg(errp, "length too long for option end");
251 nbd_send_opt_abort(ioc);
252 return -1;
253 }
254 } else if (reply.type == NBD_REP_SERVER) {
255 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
256 error_setg(errp, "incorrect option length %" PRIu32, len);
257 nbd_send_opt_abort(ioc);
258 return -1;
259 }
260 if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
261 error_setg(errp, "failed to read option name length");
262 nbd_send_opt_abort(ioc);
263 return -1;
264 }
265 namelen = be32_to_cpu(namelen);
266 len -= sizeof(namelen);
267 if (len < namelen) {
268 error_setg(errp, "incorrect option name length");
269 nbd_send_opt_abort(ioc);
270 return -1;
271 }
272 if (namelen > NBD_MAX_NAME_SIZE) {
273 error_setg(errp, "export name length too long %" PRIu32, namelen);
274 nbd_send_opt_abort(ioc);
275 return -1;
276 }
277
278 *name = g_new0(char, namelen + 1);
279 if (read_sync(ioc, *name, namelen) != namelen) {
280 error_setg(errp, "failed to read export name");
281 g_free(*name);
282 *name = NULL;
283 nbd_send_opt_abort(ioc);
284 return -1;
285 }
286 (*name)[namelen] = '\0';
287 len -= namelen;
288 if (len) {
289 char *buf = g_malloc(len + 1);
290 if (read_sync(ioc, buf, len) != len) {
291 error_setg(errp, "failed to read export description");
292 g_free(*name);
293 g_free(buf);
294 *name = NULL;
295 nbd_send_opt_abort(ioc);
296 return -1;
297 }
298 buf[len] = '\0';
299 TRACE("Ignoring export description: %s", buf);
300 g_free(buf);
301 }
302 } else {
303 error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
304 reply.type, NBD_REP_SERVER);
305 nbd_send_opt_abort(ioc);
306 return -1;
307 }
308 return 1;
309 }
310
311
312 static int nbd_receive_query_exports(QIOChannel *ioc,
313 const char *wantname,
314 Error **errp)
315 {
316 bool foundExport = false;
317
318 TRACE("Querying export list");
319 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
320 return -1;
321 }
322
323 TRACE("Reading available export names");
324 while (1) {
325 char *name = NULL;
326 int ret = nbd_receive_list(ioc, &name, errp);
327
328 if (ret < 0) {
329 g_free(name);
330 name = NULL;
331 return -1;
332 }
333 if (ret == 0) {
334 /* Server doesn't support export listing, so
335 * we will just assume an export with our
336 * wanted name exists */
337 foundExport = true;
338 break;
339 }
340 if (name == NULL) {
341 TRACE("End of export name list");
342 break;
343 }
344 if (g_str_equal(name, wantname)) {
345 foundExport = true;
346 TRACE("Found desired export name '%s'", name);
347 } else {
348 TRACE("Ignored export name '%s'", name);
349 }
350 g_free(name);
351 }
352
353 if (!foundExport) {
354 error_setg(errp, "No export with name '%s' available", wantname);
355 nbd_send_opt_abort(ioc);
356 return -1;
357 }
358
359 return 0;
360 }
361
362 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
363 QCryptoTLSCreds *tlscreds,
364 const char *hostname, Error **errp)
365 {
366 nbd_opt_reply reply;
367 QIOChannelTLS *tioc;
368 struct NBDTLSHandshakeData data = { 0 };
369
370 TRACE("Requesting TLS from server");
371 if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
372 return NULL;
373 }
374
375 TRACE("Getting TLS reply from server");
376 if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
377 return NULL;
378 }
379
380 if (reply.type != NBD_REP_ACK) {
381 error_setg(errp, "Server rejected request to start TLS %" PRIx32,
382 reply.type);
383 nbd_send_opt_abort(ioc);
384 return NULL;
385 }
386
387 if (reply.length != 0) {
388 error_setg(errp, "Start TLS response was not zero %" PRIu32,
389 reply.length);
390 nbd_send_opt_abort(ioc);
391 return NULL;
392 }
393
394 TRACE("TLS request approved, setting up TLS");
395 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
396 if (!tioc) {
397 return NULL;
398 }
399 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
400 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
401 TRACE("Starting TLS handshake");
402 qio_channel_tls_handshake(tioc,
403 nbd_tls_handshake,
404 &data,
405 NULL);
406
407 if (!data.complete) {
408 g_main_loop_run(data.loop);
409 }
410 g_main_loop_unref(data.loop);
411 if (data.error) {
412 error_propagate(errp, data.error);
413 object_unref(OBJECT(tioc));
414 return NULL;
415 }
416
417 return QIO_CHANNEL(tioc);
418 }
419
420
421 int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
422 QCryptoTLSCreds *tlscreds, const char *hostname,
423 QIOChannel **outioc,
424 off_t *size, Error **errp)
425 {
426 char buf[256];
427 uint64_t magic, s;
428 int rc;
429
430 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
431 tlscreds, hostname ? hostname : "<null>");
432
433 rc = -EINVAL;
434
435 if (outioc) {
436 *outioc = NULL;
437 }
438 if (tlscreds && !outioc) {
439 error_setg(errp, "Output I/O channel required for TLS");
440 goto fail;
441 }
442
443 if (read_sync(ioc, buf, 8) != 8) {
444 error_setg(errp, "Failed to read data");
445 goto fail;
446 }
447
448 buf[8] = '\0';
449 if (strlen(buf) == 0) {
450 error_setg(errp, "Server connection closed unexpectedly");
451 goto fail;
452 }
453
454 TRACE("Magic is %c%c%c%c%c%c%c%c",
455 qemu_isprint(buf[0]) ? buf[0] : '.',
456 qemu_isprint(buf[1]) ? buf[1] : '.',
457 qemu_isprint(buf[2]) ? buf[2] : '.',
458 qemu_isprint(buf[3]) ? buf[3] : '.',
459 qemu_isprint(buf[4]) ? buf[4] : '.',
460 qemu_isprint(buf[5]) ? buf[5] : '.',
461 qemu_isprint(buf[6]) ? buf[6] : '.',
462 qemu_isprint(buf[7]) ? buf[7] : '.');
463
464 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
465 error_setg(errp, "Invalid magic received");
466 goto fail;
467 }
468
469 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
470 error_setg(errp, "Failed to read magic");
471 goto fail;
472 }
473 magic = be64_to_cpu(magic);
474 TRACE("Magic is 0x%" PRIx64, magic);
475
476 if (magic == NBD_OPTS_MAGIC) {
477 uint32_t clientflags = 0;
478 uint16_t globalflags;
479 bool fixedNewStyle = false;
480
481 if (read_sync(ioc, &globalflags, sizeof(globalflags)) !=
482 sizeof(globalflags)) {
483 error_setg(errp, "Failed to read server flags");
484 goto fail;
485 }
486 globalflags = be16_to_cpu(globalflags);
487 TRACE("Global flags are %" PRIx32, globalflags);
488 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
489 fixedNewStyle = true;
490 TRACE("Server supports fixed new style");
491 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
492 }
493 /* client requested flags */
494 clientflags = cpu_to_be32(clientflags);
495 if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
496 sizeof(clientflags)) {
497 error_setg(errp, "Failed to send clientflags field");
498 goto fail;
499 }
500 if (tlscreds) {
501 if (fixedNewStyle) {
502 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
503 if (!*outioc) {
504 goto fail;
505 }
506 ioc = *outioc;
507 } else {
508 error_setg(errp, "Server does not support STARTTLS");
509 goto fail;
510 }
511 }
512 if (!name) {
513 TRACE("Using default NBD export name \"\"");
514 name = "";
515 }
516 if (fixedNewStyle) {
517 /* Check our desired export is present in the
518 * server export list. Since NBD_OPT_EXPORT_NAME
519 * cannot return an error message, running this
520 * query gives us good error reporting if the
521 * server required TLS
522 */
523 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
524 goto fail;
525 }
526 }
527 /* write the export name request */
528 if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
529 errp) < 0) {
530 goto fail;
531 }
532
533 /* Read the response */
534 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
535 error_setg(errp, "Failed to read export length");
536 goto fail;
537 }
538 *size = be64_to_cpu(s);
539
540 if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
541 error_setg(errp, "Failed to read export flags");
542 goto fail;
543 }
544 be16_to_cpus(flags);
545 } else if (magic == NBD_CLIENT_MAGIC) {
546 uint32_t oldflags;
547
548 if (name) {
549 error_setg(errp, "Server does not support export names");
550 goto fail;
551 }
552 if (tlscreds) {
553 error_setg(errp, "Server does not support STARTTLS");
554 goto fail;
555 }
556
557 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
558 error_setg(errp, "Failed to read export length");
559 goto fail;
560 }
561 *size = be64_to_cpu(s);
562 TRACE("Size is %" PRIu64, *size);
563
564 if (read_sync(ioc, &oldflags, sizeof(oldflags)) != sizeof(oldflags)) {
565 error_setg(errp, "Failed to read export flags");
566 goto fail;
567 }
568 be32_to_cpus(&oldflags);
569 if (oldflags & ~0xffff) {
570 error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
571 goto fail;
572 }
573 *flags = oldflags;
574 } else {
575 error_setg(errp, "Bad magic received");
576 goto fail;
577 }
578
579 TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
580 if (read_sync(ioc, &buf, 124) != 124) {
581 error_setg(errp, "Failed to read reserved block");
582 goto fail;
583 }
584 rc = 0;
585
586 fail:
587 return rc;
588 }
589
590 #ifdef __linux__
591 int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size)
592 {
593 unsigned long sectors = size / BDRV_SECTOR_SIZE;
594 if (size / BDRV_SECTOR_SIZE != sectors) {
595 LOG("Export size %lld too large for 32-bit kernel", (long long) size);
596 return -E2BIG;
597 }
598
599 TRACE("Setting NBD socket");
600
601 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
602 int serrno = errno;
603 LOG("Failed to set NBD socket");
604 return -serrno;
605 }
606
607 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
608
609 if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
610 int serrno = errno;
611 LOG("Failed setting NBD block size");
612 return -serrno;
613 }
614
615 TRACE("Setting size to %lu block(s)", sectors);
616 if (size % BDRV_SECTOR_SIZE) {
617 TRACE("Ignoring trailing %d bytes of export",
618 (int) (size % BDRV_SECTOR_SIZE));
619 }
620
621 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
622 int serrno = errno;
623 LOG("Failed setting size (in blocks)");
624 return -serrno;
625 }
626
627 if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
628 if (errno == ENOTTY) {
629 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
630 TRACE("Setting readonly attribute");
631
632 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
633 int serrno = errno;
634 LOG("Failed setting read-only attribute");
635 return -serrno;
636 }
637 } else {
638 int serrno = errno;
639 LOG("Failed setting flags");
640 return -serrno;
641 }
642 }
643
644 TRACE("Negotiation ended");
645
646 return 0;
647 }
648
649 int nbd_client(int fd)
650 {
651 int ret;
652 int serrno;
653
654 TRACE("Doing NBD loop");
655
656 ret = ioctl(fd, NBD_DO_IT);
657 if (ret < 0 && errno == EPIPE) {
658 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
659 * the socket via NBD_DISCONNECT. We do not want to return 1 in
660 * that case.
661 */
662 ret = 0;
663 }
664 serrno = errno;
665
666 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
667
668 TRACE("Clearing NBD queue");
669 ioctl(fd, NBD_CLEAR_QUE);
670
671 TRACE("Clearing NBD socket");
672 ioctl(fd, NBD_CLEAR_SOCK);
673
674 errno = serrno;
675 return ret;
676 }
677
678 int nbd_disconnect(int fd)
679 {
680 ioctl(fd, NBD_CLEAR_QUE);
681 ioctl(fd, NBD_DISCONNECT);
682 ioctl(fd, NBD_CLEAR_SOCK);
683 return 0;
684 }
685
686 #else
687 int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size)
688 {
689 return -ENOTSUP;
690 }
691
692 int nbd_client(int fd)
693 {
694 return -ENOTSUP;
695 }
696 int nbd_disconnect(int fd)
697 {
698 return -ENOTSUP;
699 }
700 #endif
701
702 ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
703 {
704 uint8_t buf[NBD_REQUEST_SIZE];
705 ssize_t ret;
706
707 TRACE("Sending request to server: "
708 "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
709 ", .flags = %" PRIx16 ", .type = %" PRIu16 " }",
710 request->from, request->len, request->handle,
711 request->flags, request->type);
712
713 stl_be_p(buf, NBD_REQUEST_MAGIC);
714 stw_be_p(buf + 4, request->flags);
715 stw_be_p(buf + 6, request->type);
716 stq_be_p(buf + 8, request->handle);
717 stq_be_p(buf + 16, request->from);
718 stl_be_p(buf + 24, request->len);
719
720 ret = write_sync(ioc, buf, sizeof(buf));
721 if (ret < 0) {
722 return ret;
723 }
724
725 if (ret != sizeof(buf)) {
726 LOG("writing to socket failed");
727 return -EINVAL;
728 }
729 return 0;
730 }
731
732 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply)
733 {
734 uint8_t buf[NBD_REPLY_SIZE];
735 uint32_t magic;
736 ssize_t ret;
737
738 ret = read_sync(ioc, buf, sizeof(buf));
739 if (ret < 0) {
740 return ret;
741 }
742
743 if (ret != sizeof(buf)) {
744 LOG("read failed");
745 return -EINVAL;
746 }
747
748 /* Reply
749 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
750 [ 4 .. 7] error (0 == no error)
751 [ 7 .. 15] handle
752 */
753
754 magic = ldl_be_p(buf);
755 reply->error = ldl_be_p(buf + 4);
756 reply->handle = ldq_be_p(buf + 8);
757
758 reply->error = nbd_errno_to_system_errno(reply->error);
759
760 TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
761 ", handle = %" PRIu64" }",
762 magic, reply->error, reply->handle);
763
764 if (magic != NBD_REPLY_MAGIC) {
765 LOG("invalid magic (got 0x%" PRIx32 ")", magic);
766 return -EINVAL;
767 }
768 return 0;
769 }
770