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