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