]> git.proxmox.com Git - mirror_qemu.git/blame - block/nbd.c
9pfs: use coroutine_fn annotation in hw/9pfs/co*.[ch]
[mirror_qemu.git] / block / nbd.c
CommitLineData
75818250
TS
1/*
2 * QEMU Block driver for NBD
3 *
4 * Copyright (C) 2008 Bull S.A.S.
bd5921b4 5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
75818250
TS
6 *
7 * Some parts:
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 * THE SOFTWARE.
27 */
28
80c71a24 29#include "qemu/osdep.h"
2302c1ca 30#include "block/nbd-client.h"
da34e65c 31#include "qapi/error.h"
1de7afc9 32#include "qemu/uri.h"
737e150e 33#include "block/block_int.h"
1de7afc9 34#include "qemu/module.h"
2019d68b 35#include "qapi/qmp/qdict.h"
f53a1feb
KW
36#include "qapi/qmp/qjson.h"
37#include "qapi/qmp/qint.h"
2019d68b 38#include "qapi/qmp/qstring.h"
f348b6d1 39#include "qemu/cutils.h"
75818250 40
1d45f8b5
LV
41#define EN_OPTSTR ":exportname="
42
75818250 43typedef struct BDRVNBDState {
2302c1ca 44 NbdClientSession client;
03504d05
HR
45
46 /* For nbd_refresh_filename() */
47 char *path, *host, *port, *export, *tlscredsid;
75818250
TS
48} BDRVNBDState;
49
f53a1feb 50static int nbd_parse_uri(const char *filename, QDict *options)
1d7d2a9d
PB
51{
52 URI *uri;
53 const char *p;
54 QueryParams *qp = NULL;
55 int ret = 0;
f53a1feb 56 bool is_unix;
1d7d2a9d
PB
57
58 uri = uri_parse(filename);
59 if (!uri) {
60 return -EINVAL;
61 }
62
63 /* transport */
64 if (!strcmp(uri->scheme, "nbd")) {
f53a1feb 65 is_unix = false;
1d7d2a9d 66 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
f53a1feb 67 is_unix = false;
1d7d2a9d 68 } else if (!strcmp(uri->scheme, "nbd+unix")) {
f53a1feb 69 is_unix = true;
1d7d2a9d
PB
70 } else {
71 ret = -EINVAL;
72 goto out;
73 }
74
75 p = uri->path ? uri->path : "/";
76 p += strspn(p, "/");
77 if (p[0]) {
f53a1feb 78 qdict_put(options, "export", qstring_from_str(p));
1d7d2a9d
PB
79 }
80
81 qp = query_params_parse(uri->query);
f53a1feb 82 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1d7d2a9d
PB
83 ret = -EINVAL;
84 goto out;
85 }
86
f53a1feb 87 if (is_unix) {
1d7d2a9d
PB
88 /* nbd+unix:///export?socket=path */
89 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
90 ret = -EINVAL;
91 goto out;
92 }
f53a1feb 93 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
1d7d2a9d 94 } else {
23307908 95 QString *host;
bebbf7fa 96 /* nbd[+tcp]://host[:port]/export */
1d7d2a9d
PB
97 if (!uri->server) {
98 ret = -EINVAL;
99 goto out;
100 }
f17c90be 101
23307908
JT
102 /* strip braces from literal IPv6 address */
103 if (uri->server[0] == '[') {
104 host = qstring_from_substr(uri->server, 1,
105 strlen(uri->server) - 2);
106 } else {
107 host = qstring_from_str(uri->server);
108 }
109
110 qdict_put(options, "host", host);
bebbf7fa
KW
111 if (uri->port) {
112 char* port_str = g_strdup_printf("%d", uri->port);
113 qdict_put(options, "port", qstring_from_str(port_str));
114 g_free(port_str);
115 }
1d7d2a9d
PB
116 }
117
118out:
119 if (qp) {
120 query_params_free(qp);
121 }
122 uri_free(uri);
123 return ret;
124}
125
6963a30d
KW
126static void nbd_parse_filename(const char *filename, QDict *options,
127 Error **errp)
75818250 128{
1d45f8b5 129 char *file;
33897dc7
NT
130 char *export_name;
131 const char *host_spec;
75818250 132 const char *unixpath;
75818250 133
681e7ad0
KW
134 if (qdict_haskey(options, "host")
135 || qdict_haskey(options, "port")
136 || qdict_haskey(options, "path"))
137 {
138 error_setg(errp, "host/port/path and a file name may not be specified "
139 "at the same time");
140 return;
141 }
142
1d7d2a9d 143 if (strstr(filename, "://")) {
6963a30d
KW
144 int ret = nbd_parse_uri(filename, options);
145 if (ret < 0) {
146 error_setg(errp, "No valid URL specified");
147 }
148 return;
1d7d2a9d
PB
149 }
150
7267c094 151 file = g_strdup(filename);
1d45f8b5 152
33897dc7
NT
153 export_name = strstr(file, EN_OPTSTR);
154 if (export_name) {
155 if (export_name[strlen(EN_OPTSTR)] == 0) {
1d45f8b5
LV
156 goto out;
157 }
33897dc7
NT
158 export_name[0] = 0; /* truncate 'file' */
159 export_name += strlen(EN_OPTSTR);
f53a1feb
KW
160
161 qdict_put(options, "export", qstring_from_str(export_name));
1d45f8b5
LV
162 }
163
33897dc7
NT
164 /* extract the host_spec - fail if it's not nbd:... */
165 if (!strstart(file, "nbd:", &host_spec)) {
6963a30d 166 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1d45f8b5
LV
167 goto out;
168 }
75818250 169
f53a1feb 170 if (!*host_spec) {
f53a1feb
KW
171 goto out;
172 }
173
33897dc7
NT
174 /* are we a UNIX or TCP socket? */
175 if (strstart(host_spec, "unix:", &unixpath)) {
f53a1feb 176 qdict_put(options, "path", qstring_from_str(unixpath));
75818250 177 } else {
f53a1feb
KW
178 InetSocketAddress *addr = NULL;
179
6963a30d 180 addr = inet_parse(host_spec, errp);
92de9012 181 if (!addr) {
f17c90be
KW
182 goto out;
183 }
75818250 184
f53a1feb
KW
185 qdict_put(options, "host", qstring_from_str(addr->host));
186 qdict_put(options, "port", qstring_from_str(addr->port));
187 qapi_free_InetSocketAddress(addr);
188 }
75818250 189
33897dc7 190out:
7267c094 191 g_free(file);
f53a1feb
KW
192}
193
03504d05 194static SocketAddress *nbd_config(BDRVNBDState *s, QemuOpts *opts, Error **errp)
f53a1feb 195{
7a5ed437 196 SocketAddress *saddr;
f53a1feb 197
03504d05
HR
198 s->path = g_strdup(qemu_opt_get(opts, "path"));
199 s->host = g_strdup(qemu_opt_get(opts, "host"));
200
201 if (!s->path == !s->host) {
202 if (s->path) {
77e8b9ca 203 error_setg(errp, "path and host may not be used at the same time.");
a69d9af4 204 } else {
77e8b9ca 205 error_setg(errp, "one of path and host must be specified.");
681e7ad0 206 }
7a5ed437 207 return NULL;
33897dc7 208 }
f53a1feb 209
7a5ed437 210 saddr = g_new0(SocketAddress, 1);
f53a1feb 211
03504d05 212 if (s->path) {
0399293e 213 UnixSocketAddress *q_unix;
6a8f9661 214 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
32bafa8f 215 q_unix = saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
03504d05 216 q_unix->path = g_strdup(s->path);
7a5ed437 217 } else {
0399293e 218 InetSocketAddress *inet;
03504d05
HR
219
220 s->port = g_strdup(qemu_opt_get(opts, "port"));
221
6a8f9661 222 saddr->type = SOCKET_ADDRESS_KIND_INET;
32bafa8f 223 inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1);
03504d05
HR
224 inet->host = g_strdup(s->host);
225 inet->port = g_strdup(s->port);
7ccc44fd 226 if (!inet->port) {
0399293e 227 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
7a5ed437 228 }
f53a1feb
KW
229 }
230
6a8f9661 231 s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
bebbf7fa 232
03504d05 233 s->export = g_strdup(qemu_opt_get(opts, "export"));
7a5ed437
DB
234
235 return saddr;
33897dc7 236}
1d45f8b5 237
f53a829b
HR
238NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
239{
240 BDRVNBDState *s = bs->opaque;
241 return &s->client;
242}
243
064097d9
DB
244static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
245 Error **errp)
33897dc7 246{
064097d9
DB
247 QIOChannelSocket *sioc;
248 Error *local_err = NULL;
75818250 249
064097d9 250 sioc = qio_channel_socket_new();
75818250 251
064097d9
DB
252 qio_channel_socket_connect_sync(sioc,
253 saddr,
254 &local_err);
255 if (local_err) {
256 error_propagate(errp, local_err);
257 return NULL;
1d45f8b5 258 }
75818250 259
064097d9 260 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
7a5ed437 261
064097d9 262 return sioc;
33897dc7
NT
263}
264
75822a12
DB
265
266static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
267{
268 Object *obj;
269 QCryptoTLSCreds *creds;
270
271 obj = object_resolve_path_component(
272 object_get_objects_root(), id);
273 if (!obj) {
274 error_setg(errp, "No TLS credentials with id '%s'",
275 id);
276 return NULL;
277 }
278 creds = (QCryptoTLSCreds *)
279 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
280 if (!creds) {
281 error_setg(errp, "Object with id '%s' is not TLS credentials",
282 id);
283 return NULL;
284 }
285
286 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
287 error_setg(errp,
288 "Expecting TLS credentials with a client endpoint");
289 return NULL;
290 }
291 object_ref(obj);
292 return creds;
293}
294
295
7ccc44fd
HR
296static QemuOptsList nbd_runtime_opts = {
297 .name = "nbd",
298 .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head),
299 .desc = {
300 {
301 .name = "host",
302 .type = QEMU_OPT_STRING,
303 .help = "TCP host to connect to",
304 },
305 {
306 .name = "port",
307 .type = QEMU_OPT_STRING,
308 .help = "TCP port to connect to",
309 },
310 {
311 .name = "path",
312 .type = QEMU_OPT_STRING,
313 .help = "Unix socket path to connect to",
314 },
315 {
316 .name = "export",
317 .type = QEMU_OPT_STRING,
318 .help = "Name of the NBD export to open",
319 },
320 {
321 .name = "tls-creds",
322 .type = QEMU_OPT_STRING,
323 .help = "ID of the TLS credentials to use",
324 },
325 },
326};
327
015a1036
HR
328static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
329 Error **errp)
33897dc7
NT
330{
331 BDRVNBDState *s = bs->opaque;
7ccc44fd
HR
332 QemuOpts *opts = NULL;
333 Error *local_err = NULL;
75822a12 334 QIOChannelSocket *sioc = NULL;
7ccc44fd 335 SocketAddress *saddr = NULL;
75822a12
DB
336 QCryptoTLSCreds *tlscreds = NULL;
337 const char *hostname = NULL;
338 int ret = -EINVAL;
ae255e52 339
7ccc44fd
HR
340 opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort);
341 qemu_opts_absorb_qdict(opts, options, &local_err);
342 if (local_err) {
343 error_propagate(errp, local_err);
344 goto error;
345 }
346
33897dc7 347 /* Pop the config into our state object. Exit if invalid. */
03504d05 348 saddr = nbd_config(s, opts, errp);
7a5ed437 349 if (!saddr) {
75822a12
DB
350 goto error;
351 }
352
03504d05
HR
353 s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds"));
354 if (s->tlscredsid) {
355 tlscreds = nbd_get_tls_creds(s->tlscredsid, errp);
75822a12
DB
356 if (!tlscreds) {
357 goto error;
358 }
359
360 if (saddr->type != SOCKET_ADDRESS_KIND_INET) {
361 error_setg(errp, "TLS only supported over IP sockets");
362 goto error;
363 }
32bafa8f 364 hostname = saddr->u.inet.data->host;
33897dc7
NT
365 }
366
367 /* establish TCP connection, return error if it fails
368 * TODO: Configurable retry-until-timeout behaviour.
369 */
064097d9 370 sioc = nbd_establish_connection(saddr, errp);
064097d9 371 if (!sioc) {
75822a12
DB
372 ret = -ECONNREFUSED;
373 goto error;
2c7989a9
PB
374 }
375
2302c1ca 376 /* NBD handshake */
03504d05 377 ret = nbd_client_init(bs, sioc, s->export,
75822a12
DB
378 tlscreds, hostname, errp);
379 error:
380 if (sioc) {
381 object_unref(OBJECT(sioc));
382 }
383 if (tlscreds) {
384 object_unref(OBJECT(tlscreds));
385 }
03504d05
HR
386 if (ret < 0) {
387 g_free(s->path);
388 g_free(s->host);
389 g_free(s->port);
390 g_free(s->export);
391 g_free(s->tlscredsid);
392 }
75822a12 393 qapi_free_SocketAddress(saddr);
7ccc44fd 394 qemu_opts_del(opts);
75822a12 395 return ret;
e183ef75
PB
396}
397
1486d04a
PB
398static int nbd_co_flush(BlockDriverState *bs)
399{
f53a829b 400 return nbd_client_co_flush(bs);
1486d04a
PB
401}
402
fa21e6fa
DL
403static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
404{
b9f7855a 405 bs->bl.max_pdiscard = NBD_MAX_BUFFER_SIZE;
5def6b80 406 bs->bl.max_transfer = NBD_MAX_BUFFER_SIZE;
fa21e6fa
DL
407}
408
75818250
TS
409static void nbd_close(BlockDriverState *bs)
410{
03504d05
HR
411 BDRVNBDState *s = bs->opaque;
412
f53a829b 413 nbd_client_close(bs);
03504d05
HR
414
415 g_free(s->path);
416 g_free(s->host);
417 g_free(s->port);
418 g_free(s->export);
419 g_free(s->tlscredsid);
75818250
TS
420}
421
422static int64_t nbd_getlength(BlockDriverState *bs)
423{
424 BDRVNBDState *s = bs->opaque;
425
2302c1ca 426 return s->client.size;
75818250
TS
427}
428
69447cd8
SH
429static void nbd_detach_aio_context(BlockDriverState *bs)
430{
f53a829b 431 nbd_client_detach_aio_context(bs);
69447cd8
SH
432}
433
434static void nbd_attach_aio_context(BlockDriverState *bs,
435 AioContext *new_context)
436{
f53a829b 437 nbd_client_attach_aio_context(bs, new_context);
69447cd8
SH
438}
439
4cdd01d3 440static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
2019d68b 441{
03504d05 442 BDRVNBDState *s = bs->opaque;
2019d68b 443 QDict *opts = qdict_new();
2019d68b
HR
444
445 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
446
03504d05 447 if (s->path && s->export) {
2019d68b 448 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05
HR
449 "nbd+unix:///%s?socket=%s", s->export, s->path);
450 } else if (s->path && !s->export) {
2019d68b 451 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05
HR
452 "nbd+unix://?socket=%s", s->path);
453 } else if (!s->path && s->export && s->port) {
ec0de768 454 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05
HR
455 "nbd://%s:%s/%s", s->host, s->port, s->export);
456 } else if (!s->path && s->export && !s->port) {
ec0de768 457 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05
HR
458 "nbd://%s/%s", s->host, s->export);
459 } else if (!s->path && !s->export && s->port) {
ec0de768 460 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05
HR
461 "nbd://%s:%s", s->host, s->port);
462 } else if (!s->path && !s->export && !s->port) {
2019d68b 463 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
03504d05 464 "nbd://%s", s->host);
ec0de768
HR
465 }
466
03504d05
HR
467 if (s->path) {
468 qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(s->path)));
469 } else if (s->port) {
470 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(s->host)));
471 qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(s->port)));
ec0de768 472 } else {
03504d05 473 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(s->host)));
ec0de768 474 }
03504d05
HR
475 if (s->export) {
476 qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(s->export)));
2019d68b 477 }
03504d05
HR
478 if (s->tlscredsid) {
479 qdict_put_obj(opts, "tls-creds",
480 QOBJECT(qstring_from_str(s->tlscredsid)));
75822a12 481 }
2019d68b
HR
482
483 bs->full_open_options = opts;
484}
485
5efa9d5a 486static BlockDriver bdrv_nbd = {
69447cd8
SH
487 .format_name = "nbd",
488 .protocol_name = "nbd",
489 .instance_size = sizeof(BDRVNBDState),
490 .bdrv_parse_filename = nbd_parse_filename,
491 .bdrv_file_open = nbd_open,
70c4fb26
EB
492 .bdrv_co_preadv = nbd_client_co_preadv,
493 .bdrv_co_pwritev = nbd_client_co_pwritev,
69447cd8
SH
494 .bdrv_close = nbd_close,
495 .bdrv_co_flush_to_os = nbd_co_flush,
447e57c3 496 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
fa21e6fa 497 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
498 .bdrv_getlength = nbd_getlength,
499 .bdrv_detach_aio_context = nbd_detach_aio_context,
500 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 501 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
502};
503
504static BlockDriver bdrv_nbd_tcp = {
69447cd8
SH
505 .format_name = "nbd",
506 .protocol_name = "nbd+tcp",
507 .instance_size = sizeof(BDRVNBDState),
508 .bdrv_parse_filename = nbd_parse_filename,
509 .bdrv_file_open = nbd_open,
70c4fb26
EB
510 .bdrv_co_preadv = nbd_client_co_preadv,
511 .bdrv_co_pwritev = nbd_client_co_pwritev,
69447cd8
SH
512 .bdrv_close = nbd_close,
513 .bdrv_co_flush_to_os = nbd_co_flush,
447e57c3 514 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
fa21e6fa 515 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
516 .bdrv_getlength = nbd_getlength,
517 .bdrv_detach_aio_context = nbd_detach_aio_context,
518 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 519 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
520};
521
522static BlockDriver bdrv_nbd_unix = {
69447cd8
SH
523 .format_name = "nbd",
524 .protocol_name = "nbd+unix",
525 .instance_size = sizeof(BDRVNBDState),
526 .bdrv_parse_filename = nbd_parse_filename,
527 .bdrv_file_open = nbd_open,
70c4fb26
EB
528 .bdrv_co_preadv = nbd_client_co_preadv,
529 .bdrv_co_pwritev = nbd_client_co_pwritev,
69447cd8
SH
530 .bdrv_close = nbd_close,
531 .bdrv_co_flush_to_os = nbd_co_flush,
447e57c3 532 .bdrv_co_pdiscard = nbd_client_co_pdiscard,
fa21e6fa 533 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
534 .bdrv_getlength = nbd_getlength,
535 .bdrv_detach_aio_context = nbd_detach_aio_context,
536 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 537 .bdrv_refresh_filename = nbd_refresh_filename,
75818250 538};
5efa9d5a
AL
539
540static void bdrv_nbd_init(void)
541{
542 bdrv_register(&bdrv_nbd);
1d7d2a9d
PB
543 bdrv_register(&bdrv_nbd_tcp);
544 bdrv_register(&bdrv_nbd_unix);
5efa9d5a
AL
545}
546
547block_init(bdrv_nbd_init);