qcow2: add .bdrv_remove_persistent_dirty_bitmap
[qemu.git] / nbd / client.c
blob9c52b9b8852d2518e240609d566d7062dedef28c
1 /*
2 * Copyright (C) 2016 Red Hat, Inc.
3 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
5 * Network Block Device Client Side
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.
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.
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/>.
20 #include "qemu/osdep.h"
21 #include "qapi/error.h"
22 #include "trace.h"
23 #include "nbd-internal.h"
25 static int nbd_errno_to_system_errno(int err)
27 int ret;
28 switch (err) {
29 case NBD_SUCCESS:
30 ret = 0;
31 break;
32 case NBD_EPERM:
33 ret = EPERM;
34 break;
35 case NBD_EIO:
36 ret = EIO;
37 break;
38 case NBD_ENOMEM:
39 ret = ENOMEM;
40 break;
41 case NBD_ENOSPC:
42 ret = ENOSPC;
43 break;
44 case NBD_ESHUTDOWN:
45 ret = ESHUTDOWN;
46 break;
47 default:
48 trace_nbd_unknown_error(err);
49 /* fallthrough */
50 case NBD_EINVAL:
51 ret = EINVAL;
52 break;
54 return ret;
57 /* Definitions for opaque data types */
59 static QTAILQ_HEAD(, NBDExport) exports = QTAILQ_HEAD_INITIALIZER(exports);
61 /* That's all folks */
63 /* Basic flow for negotiation
65 Server Client
66 Negotiate
70 Server Client
71 Negotiate #1
72 Option
73 Negotiate #2
75 ----
77 followed by
79 Server Client
80 Request
81 Response
82 Request
83 Response
84 ...
85 ...
86 Request (type == 2)
90 /* Send an option request.
92 * The request is for option @opt, with @data containing @len bytes of
93 * additional payload for the request (@len may be -1 to treat @data as
94 * a C string; and @data may be NULL if @len is 0).
95 * Return 0 if successful, -1 with errp set if it is impossible to
96 * continue. */
97 static int nbd_send_option_request(QIOChannel *ioc, uint32_t opt,
98 uint32_t len, const char *data,
99 Error **errp)
101 nbd_option req;
102 QEMU_BUILD_BUG_ON(sizeof(req) != 16);
104 if (len == -1) {
105 req.length = len = strlen(data);
107 trace_nbd_send_option_request(opt, len);
109 stq_be_p(&req.magic, NBD_OPTS_MAGIC);
110 stl_be_p(&req.option, opt);
111 stl_be_p(&req.length, len);
113 if (nbd_write(ioc, &req, sizeof(req), errp) < 0) {
114 error_prepend(errp, "Failed to send option request header");
115 return -1;
118 if (len && nbd_write(ioc, (char *) data, len, errp) < 0) {
119 error_prepend(errp, "Failed to send option request data");
120 return -1;
123 return 0;
126 /* Send NBD_OPT_ABORT as a courtesy to let the server know that we are
127 * not going to attempt further negotiation. */
128 static void nbd_send_opt_abort(QIOChannel *ioc)
130 /* Technically, a compliant server is supposed to reply to us; but
131 * older servers disconnected instead. At any rate, we're allowed
132 * to disconnect without waiting for the server reply, so we don't
133 * even care if the request makes it to the server, let alone
134 * waiting around for whether the server replies. */
135 nbd_send_option_request(ioc, NBD_OPT_ABORT, 0, NULL, NULL);
139 /* Receive the header of an option reply, which should match the given
140 * opt. Read through the length field, but NOT the length bytes of
141 * payload. Return 0 if successful, -1 with errp set if it is
142 * impossible to continue. */
143 static int nbd_receive_option_reply(QIOChannel *ioc, uint32_t opt,
144 nbd_opt_reply *reply, Error **errp)
146 QEMU_BUILD_BUG_ON(sizeof(*reply) != 20);
147 if (nbd_read(ioc, reply, sizeof(*reply), errp) < 0) {
148 error_prepend(errp, "failed to read option reply");
149 nbd_send_opt_abort(ioc);
150 return -1;
152 be64_to_cpus(&reply->magic);
153 be32_to_cpus(&reply->option);
154 be32_to_cpus(&reply->type);
155 be32_to_cpus(&reply->length);
157 trace_nbd_receive_option_reply(reply->option, reply->type, reply->length);
159 if (reply->magic != NBD_REP_MAGIC) {
160 error_setg(errp, "Unexpected option reply magic");
161 nbd_send_opt_abort(ioc);
162 return -1;
164 if (reply->option != opt) {
165 error_setg(errp, "Unexpected option type %x expected %x",
166 reply->option, opt);
167 nbd_send_opt_abort(ioc);
168 return -1;
170 return 0;
173 /* If reply represents success, return 1 without further action.
174 * If reply represents an error, consume the optional payload of
175 * the packet on ioc. Then return 0 for unsupported (so the client
176 * can fall back to other approaches), or -1 with errp set for other
177 * errors.
179 static int nbd_handle_reply_err(QIOChannel *ioc, nbd_opt_reply *reply,
180 Error **errp)
182 char *msg = NULL;
183 int result = -1;
185 if (!(reply->type & (1 << 31))) {
186 return 1;
189 if (reply->length) {
190 if (reply->length > NBD_MAX_BUFFER_SIZE) {
191 error_setg(errp, "server's error message is too long");
192 goto cleanup;
194 msg = g_malloc(reply->length + 1);
195 if (nbd_read(ioc, msg, reply->length, errp) < 0) {
196 error_prepend(errp, "failed to read option error message");
197 goto cleanup;
199 msg[reply->length] = '\0';
202 switch (reply->type) {
203 case NBD_REP_ERR_UNSUP:
204 trace_nbd_reply_err_unsup(reply->option);
205 result = 0;
206 goto cleanup;
208 case NBD_REP_ERR_POLICY:
209 error_setg(errp, "Denied by server for option %" PRIx32,
210 reply->option);
211 break;
213 case NBD_REP_ERR_INVALID:
214 error_setg(errp, "Invalid data length for option %" PRIx32,
215 reply->option);
216 break;
218 case NBD_REP_ERR_PLATFORM:
219 error_setg(errp, "Server lacks support for option %" PRIx32,
220 reply->option);
221 break;
223 case NBD_REP_ERR_TLS_REQD:
224 error_setg(errp, "TLS negotiation required before option %" PRIx32,
225 reply->option);
226 break;
228 case NBD_REP_ERR_SHUTDOWN:
229 error_setg(errp, "Server shutting down before option %" PRIx32,
230 reply->option);
231 break;
233 default:
234 error_setg(errp, "Unknown error code when asking for option %" PRIx32,
235 reply->option);
236 break;
239 if (msg) {
240 error_append_hint(errp, "%s\n", msg);
243 cleanup:
244 g_free(msg);
245 if (result < 0) {
246 nbd_send_opt_abort(ioc);
248 return result;
251 /* Process another portion of the NBD_OPT_LIST reply. Set *@match if
252 * the current reply matches @want or if the server does not support
253 * NBD_OPT_LIST, otherwise leave @match alone. Return 0 if iteration
254 * is complete, positive if more replies are expected, or negative
255 * with @errp set if an unrecoverable error occurred. */
256 static int nbd_receive_list(QIOChannel *ioc, const char *want, bool *match,
257 Error **errp)
259 nbd_opt_reply reply;
260 uint32_t len;
261 uint32_t namelen;
262 char name[NBD_MAX_NAME_SIZE + 1];
263 int error;
265 if (nbd_receive_option_reply(ioc, NBD_OPT_LIST, &reply, errp) < 0) {
266 return -1;
268 error = nbd_handle_reply_err(ioc, &reply, errp);
269 if (error <= 0) {
270 /* The server did not support NBD_OPT_LIST, so set *match on
271 * the assumption that any name will be accepted. */
272 *match = true;
273 return error;
275 len = reply.length;
277 if (reply.type == NBD_REP_ACK) {
278 if (len != 0) {
279 error_setg(errp, "length too long for option end");
280 nbd_send_opt_abort(ioc);
281 return -1;
283 return 0;
284 } else if (reply.type != NBD_REP_SERVER) {
285 error_setg(errp, "Unexpected reply type %" PRIx32 " expected %x",
286 reply.type, NBD_REP_SERVER);
287 nbd_send_opt_abort(ioc);
288 return -1;
291 if (len < sizeof(namelen) || len > NBD_MAX_BUFFER_SIZE) {
292 error_setg(errp, "incorrect option length %" PRIu32, len);
293 nbd_send_opt_abort(ioc);
294 return -1;
296 if (nbd_read(ioc, &namelen, sizeof(namelen), errp) < 0) {
297 error_prepend(errp, "failed to read option name length");
298 nbd_send_opt_abort(ioc);
299 return -1;
301 namelen = be32_to_cpu(namelen);
302 len -= sizeof(namelen);
303 if (len < namelen) {
304 error_setg(errp, "incorrect option name length");
305 nbd_send_opt_abort(ioc);
306 return -1;
308 if (namelen != strlen(want)) {
309 if (nbd_drop(ioc, len, errp) < 0) {
310 error_prepend(errp, "failed to skip export name with wrong length");
311 nbd_send_opt_abort(ioc);
312 return -1;
314 return 1;
317 assert(namelen < sizeof(name));
318 if (nbd_read(ioc, name, namelen, errp) < 0) {
319 error_prepend(errp, "failed to read export name");
320 nbd_send_opt_abort(ioc);
321 return -1;
323 name[namelen] = '\0';
324 len -= namelen;
325 if (nbd_drop(ioc, len, errp) < 0) {
326 error_prepend(errp, "failed to read export description");
327 nbd_send_opt_abort(ioc);
328 return -1;
330 if (!strcmp(name, want)) {
331 *match = true;
333 return 1;
337 /* Return -1 on failure, 0 if wantname is an available export. */
338 static int nbd_receive_query_exports(QIOChannel *ioc,
339 const char *wantname,
340 Error **errp)
342 bool foundExport = false;
344 trace_nbd_receive_query_exports_start(wantname);
345 if (nbd_send_option_request(ioc, NBD_OPT_LIST, 0, NULL, errp) < 0) {
346 return -1;
349 while (1) {
350 int ret = nbd_receive_list(ioc, wantname, &foundExport, errp);
352 if (ret < 0) {
353 /* Server gave unexpected reply */
354 return -1;
355 } else if (ret == 0) {
356 /* Done iterating. */
357 if (!foundExport) {
358 error_setg(errp, "No export with name '%s' available",
359 wantname);
360 nbd_send_opt_abort(ioc);
361 return -1;
363 trace_nbd_receive_query_exports_success(wantname);
364 return 0;
369 static QIOChannel *nbd_receive_starttls(QIOChannel *ioc,
370 QCryptoTLSCreds *tlscreds,
371 const char *hostname, Error **errp)
373 nbd_opt_reply reply;
374 QIOChannelTLS *tioc;
375 struct NBDTLSHandshakeData data = { 0 };
377 trace_nbd_receive_starttls_request();
378 if (nbd_send_option_request(ioc, NBD_OPT_STARTTLS, 0, NULL, errp) < 0) {
379 return NULL;
382 trace_nbd_receive_starttls_reply();
383 if (nbd_receive_option_reply(ioc, NBD_OPT_STARTTLS, &reply, errp) < 0) {
384 return NULL;
387 if (reply.type != NBD_REP_ACK) {
388 error_setg(errp, "Server rejected request to start TLS %" PRIx32,
389 reply.type);
390 nbd_send_opt_abort(ioc);
391 return NULL;
394 if (reply.length != 0) {
395 error_setg(errp, "Start TLS response was not zero %" PRIu32,
396 reply.length);
397 nbd_send_opt_abort(ioc);
398 return NULL;
401 trace_nbd_receive_starttls_new_client();
402 tioc = qio_channel_tls_new_client(ioc, tlscreds, hostname, errp);
403 if (!tioc) {
404 return NULL;
406 qio_channel_set_name(QIO_CHANNEL(tioc), "nbd-client-tls");
407 data.loop = g_main_loop_new(g_main_context_default(), FALSE);
408 trace_nbd_receive_starttls_tls_handshake();
409 qio_channel_tls_handshake(tioc,
410 nbd_tls_handshake,
411 &data,
412 NULL);
414 if (!data.complete) {
415 g_main_loop_run(data.loop);
417 g_main_loop_unref(data.loop);
418 if (data.error) {
419 error_propagate(errp, data.error);
420 object_unref(OBJECT(tioc));
421 return NULL;
424 return QIO_CHANNEL(tioc);
428 int nbd_receive_negotiate(QIOChannel *ioc, const char *name, uint16_t *flags,
429 QCryptoTLSCreds *tlscreds, const char *hostname,
430 QIOChannel **outioc,
431 off_t *size, Error **errp)
433 char buf[256];
434 uint64_t magic, s;
435 int rc;
436 bool zeroes = true;
438 trace_nbd_receive_negotiate(tlscreds, hostname ? hostname : "<null>");
440 rc = -EINVAL;
442 if (outioc) {
443 *outioc = NULL;
445 if (tlscreds && !outioc) {
446 error_setg(errp, "Output I/O channel required for TLS");
447 goto fail;
450 if (nbd_read(ioc, buf, 8, errp) < 0) {
451 error_prepend(errp, "Failed to read data");
452 goto fail;
455 buf[8] = '\0';
456 if (strlen(buf) == 0) {
457 error_setg(errp, "Server connection closed unexpectedly");
458 goto fail;
461 magic = ldq_be_p(buf);
462 trace_nbd_receive_negotiate_magic(magic);
464 if (memcmp(buf, "NBDMAGIC", 8) != 0) {
465 error_setg(errp, "Invalid magic received");
466 goto fail;
469 if (nbd_read(ioc, &magic, sizeof(magic), errp) < 0) {
470 error_prepend(errp, "Failed to read magic");
471 goto fail;
473 magic = be64_to_cpu(magic);
474 trace_nbd_receive_negotiate_magic(magic);
476 if (magic == NBD_OPTS_MAGIC) {
477 uint32_t clientflags = 0;
478 uint16_t globalflags;
479 bool fixedNewStyle = false;
481 if (nbd_read(ioc, &globalflags, sizeof(globalflags), errp) < 0) {
482 error_prepend(errp, "Failed to read server flags");
483 goto fail;
485 globalflags = be16_to_cpu(globalflags);
486 trace_nbd_receive_negotiate_server_flags(globalflags);
487 if (globalflags & NBD_FLAG_FIXED_NEWSTYLE) {
488 fixedNewStyle = true;
489 clientflags |= NBD_FLAG_C_FIXED_NEWSTYLE;
491 if (globalflags & NBD_FLAG_NO_ZEROES) {
492 zeroes = false;
493 clientflags |= NBD_FLAG_C_NO_ZEROES;
495 /* client requested flags */
496 clientflags = cpu_to_be32(clientflags);
497 if (nbd_write(ioc, &clientflags, sizeof(clientflags), errp) < 0) {
498 error_prepend(errp, "Failed to send clientflags field");
499 goto fail;
501 if (tlscreds) {
502 if (fixedNewStyle) {
503 *outioc = nbd_receive_starttls(ioc, tlscreds, hostname, errp);
504 if (!*outioc) {
505 goto fail;
507 ioc = *outioc;
508 } else {
509 error_setg(errp, "Server does not support STARTTLS");
510 goto fail;
513 if (!name) {
514 trace_nbd_receive_negotiate_default_name();
515 name = "";
517 if (fixedNewStyle) {
518 /* Check our desired export is present in the
519 * server export list. Since NBD_OPT_EXPORT_NAME
520 * cannot return an error message, running this
521 * query gives us good error reporting if the
522 * server required TLS
524 if (nbd_receive_query_exports(ioc, name, errp) < 0) {
525 goto fail;
528 /* write the export name request */
529 if (nbd_send_option_request(ioc, NBD_OPT_EXPORT_NAME, -1, name,
530 errp) < 0) {
531 goto fail;
534 /* Read the response */
535 if (nbd_read(ioc, &s, sizeof(s), errp) < 0) {
536 error_prepend(errp, "Failed to read export length");
537 goto fail;
539 *size = be64_to_cpu(s);
541 if (nbd_read(ioc, flags, sizeof(*flags), errp) < 0) {
542 error_prepend(errp, "Failed to read export flags");
543 goto fail;
545 be16_to_cpus(flags);
546 } else if (magic == NBD_CLIENT_MAGIC) {
547 uint32_t oldflags;
549 if (name) {
550 error_setg(errp, "Server does not support export names");
551 goto fail;
553 if (tlscreds) {
554 error_setg(errp, "Server does not support STARTTLS");
555 goto fail;
558 if (nbd_read(ioc, &s, sizeof(s), errp) < 0) {
559 error_prepend(errp, "Failed to read export length");
560 goto fail;
562 *size = be64_to_cpu(s);
564 if (nbd_read(ioc, &oldflags, sizeof(oldflags), errp) < 0) {
565 error_prepend(errp, "Failed to read export flags");
566 goto fail;
568 be32_to_cpus(&oldflags);
569 if (oldflags & ~0xffff) {
570 error_setg(errp, "Unexpected export flags %0x" PRIx32, oldflags);
571 goto fail;
573 *flags = oldflags;
574 } else {
575 error_setg(errp, "Bad magic received");
576 goto fail;
579 trace_nbd_receive_negotiate_size_flags(*size, *flags);
580 if (zeroes && nbd_drop(ioc, 124, errp) < 0) {
581 error_prepend(errp, "Failed to read reserved block");
582 goto fail;
584 rc = 0;
586 fail:
587 return rc;
590 #ifdef __linux__
591 int nbd_init(int fd, QIOChannelSocket *sioc, uint16_t flags, off_t size,
592 Error **errp)
594 unsigned long sectors = size / BDRV_SECTOR_SIZE;
595 if (size / BDRV_SECTOR_SIZE != sectors) {
596 error_setg(errp, "Export size %lld too large for 32-bit kernel",
597 (long long) size);
598 return -E2BIG;
601 trace_nbd_init_set_socket();
603 if (ioctl(fd, NBD_SET_SOCK, (unsigned long) sioc->fd) < 0) {
604 int serrno = errno;
605 error_setg(errp, "Failed to set NBD socket");
606 return -serrno;
609 trace_nbd_init_set_block_size(BDRV_SECTOR_SIZE);
611 if (ioctl(fd, NBD_SET_BLKSIZE, (unsigned long)BDRV_SECTOR_SIZE) < 0) {
612 int serrno = errno;
613 error_setg(errp, "Failed setting NBD block size");
614 return -serrno;
617 trace_nbd_init_set_size(sectors);
618 if (size % BDRV_SECTOR_SIZE) {
619 trace_nbd_init_trailing_bytes(size % BDRV_SECTOR_SIZE);
622 if (ioctl(fd, NBD_SET_SIZE_BLOCKS, sectors) < 0) {
623 int serrno = errno;
624 error_setg(errp, "Failed setting size (in blocks)");
625 return -serrno;
628 if (ioctl(fd, NBD_SET_FLAGS, (unsigned long) flags) < 0) {
629 if (errno == ENOTTY) {
630 int read_only = (flags & NBD_FLAG_READ_ONLY) != 0;
631 trace_nbd_init_set_readonly();
633 if (ioctl(fd, BLKROSET, (unsigned long) &read_only) < 0) {
634 int serrno = errno;
635 error_setg(errp, "Failed setting read-only attribute");
636 return -serrno;
638 } else {
639 int serrno = errno;
640 error_setg(errp, "Failed setting flags");
641 return -serrno;
645 trace_nbd_init_finish();
647 return 0;
650 int nbd_client(int fd)
652 int ret;
653 int serrno;
655 trace_nbd_client_loop();
657 ret = ioctl(fd, NBD_DO_IT);
658 if (ret < 0 && errno == EPIPE) {
659 /* NBD_DO_IT normally returns EPIPE when someone has disconnected
660 * the socket via NBD_DISCONNECT. We do not want to return 1 in
661 * that case.
663 ret = 0;
665 serrno = errno;
667 trace_nbd_client_loop_ret(ret, strerror(serrno));
669 trace_nbd_client_clear_queue();
670 ioctl(fd, NBD_CLEAR_QUE);
672 trace_nbd_client_clear_socket();
673 ioctl(fd, NBD_CLEAR_SOCK);
675 errno = serrno;
676 return ret;
679 int nbd_disconnect(int fd)
681 ioctl(fd, NBD_CLEAR_QUE);
682 ioctl(fd, NBD_DISCONNECT);
683 ioctl(fd, NBD_CLEAR_SOCK);
684 return 0;
687 #else
688 int nbd_init(int fd, QIOChannelSocket *ioc, uint16_t flags, off_t size,
689 Error **errp)
691 error_setg(errp, "nbd_init is only supported on Linux");
692 return -ENOTSUP;
695 int nbd_client(int fd)
697 return -ENOTSUP;
699 int nbd_disconnect(int fd)
701 return -ENOTSUP;
703 #endif
705 ssize_t nbd_send_request(QIOChannel *ioc, NBDRequest *request)
707 uint8_t buf[NBD_REQUEST_SIZE];
709 trace_nbd_send_request(request->from, request->len, request->handle,
710 request->flags, request->type);
712 stl_be_p(buf, NBD_REQUEST_MAGIC);
713 stw_be_p(buf + 4, request->flags);
714 stw_be_p(buf + 6, request->type);
715 stq_be_p(buf + 8, request->handle);
716 stq_be_p(buf + 16, request->from);
717 stl_be_p(buf + 24, request->len);
719 return nbd_write(ioc, buf, sizeof(buf), NULL);
722 ssize_t nbd_receive_reply(QIOChannel *ioc, NBDReply *reply, Error **errp)
724 uint8_t buf[NBD_REPLY_SIZE];
725 uint32_t magic;
726 ssize_t ret;
728 ret = nbd_read_eof(ioc, buf, sizeof(buf), errp);
729 if (ret <= 0) {
730 return ret;
733 if (ret != sizeof(buf)) {
734 error_setg(errp, "read failed");
735 return -EINVAL;
738 /* Reply
739 [ 0 .. 3] magic (NBD_REPLY_MAGIC)
740 [ 4 .. 7] error (0 == no error)
741 [ 7 .. 15] handle
744 magic = ldl_be_p(buf);
745 reply->error = ldl_be_p(buf + 4);
746 reply->handle = ldq_be_p(buf + 8);
748 reply->error = nbd_errno_to_system_errno(reply->error);
750 if (reply->error == ESHUTDOWN) {
751 /* This works even on mingw which lacks a native ESHUTDOWN */
752 error_setg(errp, "server shutting down");
753 return -EINVAL;
755 trace_nbd_receive_reply(magic, reply->error, reply->handle);
757 if (magic != NBD_REPLY_MAGIC) {
758 error_setg(errp, "invalid magic (got 0x%" PRIx32 ")", magic);
759 return -EINVAL;
761 return sizeof(buf);