]> Git Repo - qemu.git/blame - nbd/client.c
nbd: Less allocation during NBD_OPT_LIST
[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
75368aab
EB
257/* Process another portion of the NBD_OPT_LIST reply. Set *@match if
258 * the current reply matches @want or if the server does not support
259 * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration
260 * is complete, positive if more replies are expected, or negative
261 * with @errp set if an unrecoverable error occurred. */
262static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
263 Error **errp)
9344e5f5 264{
c8a3a1b6 265 nbd_opt_reply reply;
9344e5f5
DB
266 uint32_t len;
267 uint32_t namelen;
75368aab 268 char name[NBD_MAX_NAME_SIZE + 1];
6ff58164 269 int error;
9344e5f5 270
c8a3a1b6 271 if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
9344e5f5
DB
272 return -1;
273 }
c8a3a1b6 274 error = nbd_handle_reply_err(ioc, &reply, errp);
6ff58164 275 if (error <= 0) {
75368aab
EB
276 /* The server did not support NBD_OPT_LIST, so set *match on
277 * the assumption that any name will be accepted. */
278 *match = true;
6ff58164 279 return error;
9344e5f5 280 }
c8a3a1b6 281 len = reply.length;
9344e5f5 282
c8a3a1b6 283 if (reply.type == NBD_REP_ACK) {
9344e5f5
DB
284 if (len != 0) {
285 error_setg(errp, "length too long for option end");
2cdbf413 286 nbd_send_opt_abort(ioc);
9344e5f5
DB
287 return -1;
288 }
75368aab
EB
289 return 0;
290 } else if (reply.type != NBD_REP_SERVER) {
291 error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
292 reply.type, NBD_REP_SERVER);
293 nbd_send_opt_abort(ioc);
294 return -1;
295 }
9344e5f5 296
75368aab
EB
297 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
298 error_setg(errp, "incorrect option length %" PRIu32, len);
299 nbd_send_opt_abort(ioc);
300 return -1;
301 }
302 if (read_sync(ioc, &namelen, sizeof(namelen)) != sizeof(namelen)) {
303 error_setg(errp, "failed to read option name length");
304 nbd_send_opt_abort(ioc);
305 return -1;
306 }
307 namelen = be32_to_cpu(namelen);
308 len -= sizeof(namelen);
309 if (len < namelen) {
310 error_setg(errp, "incorrect option name length");
311 nbd_send_opt_abort(ioc);
312 return -1;
313 }
314 if (namelen != strlen(want)) {
7d3123e1 315 if (drop_sync(ioc, len) != len) {
75368aab 316 error_setg(errp, "failed to skip export name with wrong length");
7d3123e1
EB
317 nbd_send_opt_abort(ioc);
318 return -1;
200650d4 319 }
75368aab
EB
320 return 1;
321 }
322
323 assert(namelen < sizeof(name));
324 if (read_sync(ioc, name, namelen) != namelen) {
325 error_setg(errp, "failed to read export name");
326 nbd_send_opt_abort(ioc);
327 return -1;
328 }
329 name[namelen] = '\0';
330 len -= namelen;
331 if (drop_sync(ioc, len) != len) {
332 error_setg(errp, "failed to read export description");
2cdbf413 333 nbd_send_opt_abort(ioc);
9344e5f5
DB
334 return -1;
335 }
75368aab
EB
336 if (!strcmp(name, want)) {
337 *match = true;
338 }
9344e5f5
DB
339 return 1;
340}
341
342
75368aab 343/* Return -1 on failure, 0 if wantname is an available export. */
9344e5f5
DB
344static int nbd_receive_query_exports(QIOChannel *ioc,
345 const char *wantname,
346 Error **errp)
347{
9344e5f5
DB
348 bool foundExport = false;
349
75368aab 350 TRACE("Querying export list for '%s'", wantname);
c8a3a1b6 351 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
9344e5f5
DB
352 return -1;
353 }
354
355 TRACE("Reading available export names");
356 while (1) {
75368aab 357 int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
9344e5f5
DB
358
359 if (ret < 0) {
75368aab 360 /* Server gave unexpected reply */
9344e5f5 361 return -1;
75368aab
EB
362 } else if (ret == 0) {
363 /* Done iterating. */
364 if (!foundExport) {
365 error_setg(errp, "No export with name '%s' available",
366 wantname);
367 nbd_send_opt_abort(ioc);
368 return -1;
369 }
370 TRACE("Found desired export name '%s'", wantname);
371 return 0;
9344e5f5 372 }
9344e5f5 373 }
9344e5f5
DB
374}
375
f95910fe
DB
376static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
377 QCryptoTLSCreds *tlscreds,
378 const char *hostname, Error **errp)
379{
c8a3a1b6 380 nbd_opt_reply reply;
f95910fe
DB
381 QIOChannelTLS *tioc;
382 struct NBDTLSHandshakeData data = { 0 };
383
384 TRACE("Requesting TLS from server");
c8a3a1b6 385 if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
f95910fe
DB
386 return NULL;
387 }
388
389 TRACE("Getting TLS reply from server");
c8a3a1b6 390 if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
f95910fe
DB
391 return NULL;
392 }
c8a3a1b6
EB
393
394 if (reply.type != NBD_REP_ACK) {
2cb34749 395 error_setg(errp, "Server rejected request to start TLS %" PRIx32,
c8a3a1b6 396 reply.type);
2cdbf413 397 nbd_send_opt_abort(ioc);
f95910fe
DB
398 return NULL;
399 }
400
c8a3a1b6 401 if (reply.length != 0) {
2cb34749 402 error_setg(errp, "Start TLS response was not zero %" PRIu32,
c8a3a1b6 403 reply.length);
2cdbf413 404 nbd_send_opt_abort(ioc);
f95910fe
DB
405 return NULL;
406 }
407
408 TRACE("TLS request approved, setting up TLS");
409 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
410 if (!tioc) {
411 return NULL;
412 }
0d73f725 413 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
f95910fe 414 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
2cb34749 415 TRACE("Starting TLS handshake");
f95910fe
DB
416 qio_channel_tls_handshake(tioc,
417 nbd_tls_handshake,
418 &data,
419 NULL);
420
421 if (!data.complete) {
422 g_main_loop_run(data.loop);
423 }
424 g_main_loop_unref(data.loop);
425 if (data.error) {
426 error_propagate(errp, data.error);
427 object_unref(OBJECT(tioc));
428 return NULL;
429 }
430
431 return QIO_CHANNEL(tioc);
432}
433
434
7423f417 435int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
f95910fe
DB
436 QCryptoTLSCreds *tlscreds, const char *hostname,
437 QIOChannel **outioc,
798bfe00
FZ
438 off_t *size, Error **errp)
439{
440 char buf[256];
441 uint64_t magic, s;
798bfe00
FZ
442 int rc;
443
f95910fe
DB
444 TRACE("Receiving negotiation tlscreds=%p hostname=%s.",
445 tlscreds, hostname ? hostname : "<null>");
798bfe00
FZ
446
447 rc = -EINVAL;
448
f95910fe
DB
449 if (outioc) {
450 *outioc = NULL;
451 }
452 if (tlscreds && !outioc) {
453 error_setg(errp, "Output I/O channel required for TLS");
454 goto fail;
455 }
456
1c778ef7 457 if (read_sync(ioc, buf, 8) != 8) {
798bfe00
FZ
458 error_setg(errp, "Failed to read data");
459 goto fail;
460 }
461
462 buf[8] = '\0';
463 if (strlen(buf) == 0) {
464 error_setg(errp, "Server connection closed unexpectedly");
465 goto fail;
466 }
467
468 TRACE("Magic is %c%c%c%c%c%c%c%c",
469 qemu_isprint(buf[0]) ? buf[0] : '.',
470 qemu_isprint(buf[1]) ? buf[1] : '.',
471 qemu_isprint(buf[2]) ? buf[2] : '.',
472 qemu_isprint(buf[3]) ? buf[3] : '.',
473 qemu_isprint(buf[4]) ? buf[4] : '.',
474 qemu_isprint(buf[5]) ? buf[5] : '.',
475 qemu_isprint(buf[6]) ? buf[6] : '.',
476 qemu_isprint(buf[7]) ? buf[7] : '.');
477
478 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
479 error_setg(errp, "Invalid magic received");
480 goto fail;
481 }
482
1c778ef7 483 if (read_sync(ioc, &magic, sizeof(magic)) != sizeof(magic)) {
798bfe00
FZ
484 error_setg(errp, "Failed to read magic");
485 goto fail;
486 }
487 magic = be64_to_cpu(magic);
488 TRACE("Magic is 0x%" PRIx64, magic);
489
f72d705f 490 if (magic == NBD_OPTS_MAGIC) {
e2a9d9a3 491 uint32_t clientflags = 0;
e2a9d9a3 492 uint16_t globalflags;
9344e5f5 493 bool fixedNewStyle = false;
798bfe00 494
e2a9d9a3
DB
495 if (read_sync(ioc, &globalflags, sizeof(globalflags)) !=
496 sizeof(globalflags)) {
798bfe00
FZ
497 error_setg(errp, "Failed to read server flags");
498 goto fail;
499 }
9344e5f5 500 globalflags = be16_to_cpu(globalflags);
2cb34749 501 TRACE("Global flags are %" PRIx32, globalflags);
e2a9d9a3 502 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
9344e5f5 503 fixedNewStyle = true;
e2a9d9a3
DB
504 TRACE("Server supports fixed new style");
505 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
506 }
507 /* client requested flags */
9344e5f5 508 clientflags = cpu_to_be32(clientflags);
e2a9d9a3
DB
509 if (write_sync(ioc, &clientflags, sizeof(clientflags)) !=
510 sizeof(clientflags)) {
511 error_setg(errp, "Failed to send clientflags field");
798bfe00
FZ
512 goto fail;
513 }
f95910fe
DB
514 if (tlscreds) {
515 if (fixedNewStyle) {
516 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
517 if (!*outioc) {
518 goto fail;
519 }
520 ioc = *outioc;
521 } else {
522 error_setg(errp, "Server does not support STARTTLS");
523 goto fail;
524 }
525 }
f72d705f 526 if (!name) {
69b49502
DB
527 TRACE("Using default NBD export name \"\"");
528 name = "";
f72d705f 529 }
9344e5f5
DB
530 if (fixedNewStyle) {
531 /* Check our desired export is present in the
532 * server export list. Since NBD_OPT_EXPORT_NAME
533 * cannot return an error message, running this
534 * query gives us good error reporting if the
535 * server required TLS
536 */
537 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
538 goto fail;
539 }
540 }
c8a3a1b6
EB
541 /* write the export name request */
542 if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
543 errp) < 0) {
798bfe00
FZ
544 goto fail;
545 }
f72d705f 546
c8a3a1b6 547 /* Read the response */
f72d705f
DB
548 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
549 error_setg(errp, "Failed to read export length");
798bfe00
FZ
550 goto fail;
551 }
f72d705f 552 *size = be64_to_cpu(s);
798bfe00 553
7423f417 554 if (read_sync(ioc, flags, sizeof(*flags)) != sizeof(*flags)) {
f72d705f
DB
555 error_setg(errp, "Failed to read export flags");
556 goto fail;
557 }
7423f417 558 be16_to_cpus(flags);
f72d705f 559 } else if (magic == NBD_CLIENT_MAGIC) {
7423f417
EB
560 uint32_t oldflags;
561
f72d705f
DB
562 if (name) {
563 error_setg(errp, "Server does not support export names");
564 goto fail;
565 }
f95910fe
DB
566 if (tlscreds) {
567 error_setg(errp, "Server does not support STARTTLS");
568 goto fail;
569 }
f72d705f
DB
570
571 if (read_sync(ioc, &s, sizeof(s)) != sizeof(s)) {
572 error_setg(errp, "Failed to read export length");
573 goto fail;
574 }
575 *size = be64_to_cpu(s);
576 TRACE("Size is %" PRIu64, *size);
798bfe00 577
7423f417 578 if (read_sync(ioc, &oldflags, sizeof(oldflags)) != sizeof(oldflags)) {
798bfe00
FZ
579 error_setg(errp, "Failed to read export flags");
580 goto fail;
581 }
7423f417
EB
582 be32_to_cpus(&oldflags);
583 if (oldflags & ~0xffff) {
584 error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
585 goto fail;
586 }
587 *flags = oldflags;
798bfe00 588 } else {
f72d705f
DB
589 error_setg(errp, "Bad magic received");
590 goto fail;
798bfe00 591 }
f72d705f 592
7423f417 593 TRACE("Size is %" PRIu64 ", export flags %" PRIx16, *size, *flags);
7d3123e1 594 if (drop_sync(ioc, 124) != 124) {
798bfe00
FZ
595 error_setg(errp, "Failed to read reserved block");
596 goto fail;
597 }
598 rc = 0;
599
600fail:
601 return rc;
602}
603
604#ifdef __linux__
7423f417 605int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size)
798bfe00 606{
f57e2416
EB
607 unsigned long sectors = size / BDRV_SECTOR_SIZE;
608 if (size / BDRV_SECTOR_SIZE != sectors) {
609 LOG("Export size %lld too large for 32-bit kernel", (long long) size);
610 return -E2BIG;
611 }
612
798bfe00
FZ
613 TRACE("Setting NBD socket");
614
f57e2416 615 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
798bfe00
FZ
616 int serrno = errno;
617 LOG("Failed to set NBD socket");
618 return -serrno;
619 }
620
621 TRACE("Setting block size to %lu", (unsigned long)BDRV_SECTOR_SIZE);
622
f57e2416 623 if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
798bfe00
FZ
624 int serrno = errno;
625 LOG("Failed setting NBD block size");
626 return -serrno;
627 }
628
f57e2416
EB
629 TRACE("Setting size to %lu block(s)", sectors);
630 if (size % BDRV_SECTOR_SIZE) {
631 TRACE("Ignoring trailing %d bytes of export",
632 (int) (size % BDRV_SECTOR_SIZE));
633 }
798bfe00 634
f57e2416 635 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
798bfe00
FZ
636 int serrno = errno;
637 LOG("Failed setting size (in blocks)");
638 return -serrno;
639 }
640
f57e2416 641 if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
798bfe00
FZ
642 if (errno == ENOTTY) {
643 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
644 TRACE("Setting readonly attribute");
645
646 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
647 int serrno = errno;
648 LOG("Failed setting read-only attribute");
649 return -serrno;
650 }
651 } else {
652 int serrno = errno;
653 LOG("Failed setting flags");
654 return -serrno;
655 }
656 }
657
658 TRACE("Negotiation ended");
659
660 return 0;
661}
662
663int nbd_client(int fd)
664{
665 int ret;
666 int serrno;
667
668 TRACE("Doing NBD loop");
669
670 ret = ioctl(fd, NBD_DO_IT);
671 if (ret < 0 && errno == EPIPE) {
672 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
673 * the socket via NBD_DISCONNECT. We do not want to return 1 in
674 * that case.
675 */
676 ret = 0;
677 }
678 serrno = errno;
679
680 TRACE("NBD loop returned %d: %s", ret, strerror(serrno));
681
682 TRACE("Clearing NBD queue");
683 ioctl(fd, NBD_CLEAR_QUE);
684
685 TRACE("Clearing NBD socket");
686 ioctl(fd, NBD_CLEAR_SOCK);
687
688 errno = serrno;
689 return ret;
690}
98494e3b
EB
691
692int nbd_disconnect(int fd)
693{
694 ioctl(fd, NBD_CLEAR_QUE);
695 ioctl(fd, NBD_DISCONNECT);
696 ioctl(fd, NBD_CLEAR_SOCK);
697 return 0;
698}
699
798bfe00 700#else
7423f417 701int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size)
798bfe00
FZ
702{
703 return -ENOTSUP;
704}
705
706int nbd_client(int fd)
707{
708 return -ENOTSUP;
709}
98494e3b
EB
710int nbd_disconnect(int fd)
711{
712 return -ENOTSUP;
713}
798bfe00
FZ
714#endif
715
ed2dd912 716ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
798bfe00
FZ
717{
718 uint8_t buf[NBD_REQUEST_SIZE];
719 ssize_t ret;
720
7548fe31 721 TRACE("Sending request to server: "
2cb34749 722 "{ .from = %" PRIu64", .len = %" PRIu32 ", .handle = %" PRIu64
b626b51a
EB
723 ", .flags = %" PRIx16 ", .type = %" PRIu16 " }",
724 request->from, request->len, request->handle,
725 request->flags, request->type);
7548fe31 726
f6be6720 727 stl_be_p(buf, NBD_REQUEST_MAGIC);
b626b51a
EB
728 stw_be_p(buf + 4, request->flags);
729 stw_be_p(buf + 6, request->type);
f6be6720
PM
730 stq_be_p(buf + 8, request->handle);
731 stq_be_p(buf + 16, request->from);
732 stl_be_p(buf + 24, request->len);
798bfe00 733
1c778ef7 734 ret = write_sync(ioc, buf, sizeof(buf));
798bfe00
FZ
735 if (ret < 0) {
736 return ret;
737 }
738
739 if (ret != sizeof(buf)) {
740 LOG("writing to socket failed");
741 return -EINVAL;
742 }
743 return 0;
744}
745
ed2dd912 746ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply)
798bfe00
FZ
747{
748 uint8_t buf[NBD_REPLY_SIZE];
749 uint32_t magic;
750 ssize_t ret;
751
1c778ef7 752 ret = read_sync(ioc, buf, sizeof(buf));
798bfe00
FZ
753 if (ret < 0) {
754 return ret;
755 }
756
757 if (ret != sizeof(buf)) {
758 LOG("read failed");
759 return -EINVAL;
760 }
761
762 /* Reply
763 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
764 [ 4 .. 7] error (0 == no error)
765 [ 7 .. 15] handle
766 */
767
773dce3c
PM
768 magic = ldl_be_p(buf);
769 reply->error = ldl_be_p(buf + 4);
770 reply->handle = ldq_be_p(buf + 8);
798bfe00
FZ
771
772 reply->error = nbd_errno_to_system_errno(reply->error);
773
2cb34749
EB
774 TRACE("Got reply: { magic = 0x%" PRIx32 ", .error = % " PRId32
775 ", handle = %" PRIu64" }",
798bfe00
FZ
776 magic, reply->error, reply->handle);
777
778 if (magic != NBD_REPLY_MAGIC) {
2cb34749 779 LOG("invalid magic (got 0x%" PRIx32 ")", magic);
798bfe00
FZ
780 return -EINVAL;
781 }
782 return 0;
783}
784
This page took 0.168024 seconds and 4 git commands to generate.