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