]> git.proxmox.com Git - mirror_qemu.git/blame - block/nbd.c
include/qemu/osdep.h: Don't include qapi/error.h
[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"
75818250 39
75818250 40
1d45f8b5
LV
41#define EN_OPTSTR ":exportname="
42
75818250 43typedef struct BDRVNBDState {
2302c1ca 44 NbdClientSession client;
75818250
TS
45} BDRVNBDState;
46
f53a1feb 47static int nbd_parse_uri(const char *filename, QDict *options)
1d7d2a9d
PB
48{
49 URI *uri;
50 const char *p;
51 QueryParams *qp = NULL;
52 int ret = 0;
f53a1feb 53 bool is_unix;
1d7d2a9d
PB
54
55 uri = uri_parse(filename);
56 if (!uri) {
57 return -EINVAL;
58 }
59
60 /* transport */
61 if (!strcmp(uri->scheme, "nbd")) {
f53a1feb 62 is_unix = false;
1d7d2a9d 63 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
f53a1feb 64 is_unix = false;
1d7d2a9d 65 } else if (!strcmp(uri->scheme, "nbd+unix")) {
f53a1feb 66 is_unix = true;
1d7d2a9d
PB
67 } else {
68 ret = -EINVAL;
69 goto out;
70 }
71
72 p = uri->path ? uri->path : "/";
73 p += strspn(p, "/");
74 if (p[0]) {
f53a1feb 75 qdict_put(options, "export", qstring_from_str(p));
1d7d2a9d
PB
76 }
77
78 qp = query_params_parse(uri->query);
f53a1feb 79 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
1d7d2a9d
PB
80 ret = -EINVAL;
81 goto out;
82 }
83
f53a1feb 84 if (is_unix) {
1d7d2a9d
PB
85 /* nbd+unix:///export?socket=path */
86 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
87 ret = -EINVAL;
88 goto out;
89 }
f53a1feb 90 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
1d7d2a9d 91 } else {
23307908 92 QString *host;
bebbf7fa 93 /* nbd[+tcp]://host[:port]/export */
1d7d2a9d
PB
94 if (!uri->server) {
95 ret = -EINVAL;
96 goto out;
97 }
f17c90be 98
23307908
JT
99 /* strip braces from literal IPv6 address */
100 if (uri->server[0] == '[') {
101 host = qstring_from_substr(uri->server, 1,
102 strlen(uri->server) - 2);
103 } else {
104 host = qstring_from_str(uri->server);
105 }
106
107 qdict_put(options, "host", host);
bebbf7fa
KW
108 if (uri->port) {
109 char* port_str = g_strdup_printf("%d", uri->port);
110 qdict_put(options, "port", qstring_from_str(port_str));
111 g_free(port_str);
112 }
1d7d2a9d
PB
113 }
114
115out:
116 if (qp) {
117 query_params_free(qp);
118 }
119 uri_free(uri);
120 return ret;
121}
122
6963a30d
KW
123static void nbd_parse_filename(const char *filename, QDict *options,
124 Error **errp)
75818250 125{
1d45f8b5 126 char *file;
33897dc7
NT
127 char *export_name;
128 const char *host_spec;
75818250 129 const char *unixpath;
75818250 130
681e7ad0
KW
131 if (qdict_haskey(options, "host")
132 || qdict_haskey(options, "port")
133 || qdict_haskey(options, "path"))
134 {
135 error_setg(errp, "host/port/path and a file name may not be specified "
136 "at the same time");
137 return;
138 }
139
1d7d2a9d 140 if (strstr(filename, "://")) {
6963a30d
KW
141 int ret = nbd_parse_uri(filename, options);
142 if (ret < 0) {
143 error_setg(errp, "No valid URL specified");
144 }
145 return;
1d7d2a9d
PB
146 }
147
7267c094 148 file = g_strdup(filename);
1d45f8b5 149
33897dc7
NT
150 export_name = strstr(file, EN_OPTSTR);
151 if (export_name) {
152 if (export_name[strlen(EN_OPTSTR)] == 0) {
1d45f8b5
LV
153 goto out;
154 }
33897dc7
NT
155 export_name[0] = 0; /* truncate 'file' */
156 export_name += strlen(EN_OPTSTR);
f53a1feb
KW
157
158 qdict_put(options, "export", qstring_from_str(export_name));
1d45f8b5
LV
159 }
160
33897dc7
NT
161 /* extract the host_spec - fail if it's not nbd:... */
162 if (!strstart(file, "nbd:", &host_spec)) {
6963a30d 163 error_setg(errp, "File name string for NBD must start with 'nbd:'");
1d45f8b5
LV
164 goto out;
165 }
75818250 166
f53a1feb 167 if (!*host_spec) {
f53a1feb
KW
168 goto out;
169 }
170
33897dc7
NT
171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec, "unix:", &unixpath)) {
f53a1feb 173 qdict_put(options, "path", qstring_from_str(unixpath));
75818250 174 } else {
f53a1feb
KW
175 InetSocketAddress *addr = NULL;
176
6963a30d 177 addr = inet_parse(host_spec, errp);
92de9012 178 if (!addr) {
f17c90be
KW
179 goto out;
180 }
75818250 181
f53a1feb
KW
182 qdict_put(options, "host", qstring_from_str(addr->host));
183 qdict_put(options, "port", qstring_from_str(addr->port));
184 qapi_free_InetSocketAddress(addr);
185 }
75818250 186
33897dc7 187out:
7267c094 188 g_free(file);
f53a1feb
KW
189}
190
7a5ed437
DB
191static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export,
192 Error **errp)
f53a1feb 193{
7a5ed437 194 SocketAddress *saddr;
f53a1feb 195
a69d9af4
PB
196 if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
197 if (qdict_haskey(options, "path")) {
77e8b9ca 198 error_setg(errp, "path and host may not be used at the same time.");
a69d9af4 199 } else {
77e8b9ca 200 error_setg(errp, "one of path and host must be specified.");
681e7ad0 201 }
7a5ed437 202 return NULL;
33897dc7 203 }
f53a1feb 204
7a5ed437 205 saddr = g_new0(SocketAddress, 1);
f53a1feb 206
7a5ed437 207 if (qdict_haskey(options, "path")) {
0399293e 208 UnixSocketAddress *q_unix;
6a8f9661 209 saddr->type = SOCKET_ADDRESS_KIND_UNIX;
32bafa8f 210 q_unix = saddr->u.q_unix.data = g_new0(UnixSocketAddress, 1);
0399293e 211 q_unix->path = g_strdup(qdict_get_str(options, "path"));
7a5ed437
DB
212 qdict_del(options, "path");
213 } else {
0399293e 214 InetSocketAddress *inet;
6a8f9661 215 saddr->type = SOCKET_ADDRESS_KIND_INET;
32bafa8f 216 inet = saddr->u.inet.data = g_new0(InetSocketAddress, 1);
0399293e 217 inet->host = g_strdup(qdict_get_str(options, "host"));
7a5ed437 218 if (!qdict_get_try_str(options, "port")) {
0399293e 219 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
7a5ed437 220 } else {
0399293e 221 inet->port = g_strdup(qdict_get_str(options, "port"));
7a5ed437
DB
222 }
223 qdict_del(options, "host");
224 qdict_del(options, "port");
f53a1feb
KW
225 }
226
6a8f9661 227 s->client.is_unix = saddr->type == SOCKET_ADDRESS_KIND_UNIX;
bebbf7fa 228
e2bc625f
MAL
229 *export = g_strdup(qdict_get_try_str(options, "export"));
230 if (*export) {
f53a1feb
KW
231 qdict_del(options, "export");
232 }
7a5ed437
DB
233
234 return saddr;
33897dc7 235}
1d45f8b5 236
f53a829b
HR
237NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
238{
239 BDRVNBDState *s = bs->opaque;
240 return &s->client;
241}
242
064097d9
DB
243static QIOChannelSocket *nbd_establish_connection(SocketAddress *saddr,
244 Error **errp)
33897dc7 245{
064097d9
DB
246 QIOChannelSocket *sioc;
247 Error *local_err = NULL;
75818250 248
064097d9 249 sioc = qio_channel_socket_new();
75818250 250
064097d9
DB
251 qio_channel_socket_connect_sync(sioc,
252 saddr,
253 &local_err);
254 if (local_err) {
255 error_propagate(errp, local_err);
256 return NULL;
1d45f8b5 257 }
75818250 258
064097d9 259 qio_channel_set_delay(QIO_CHANNEL(sioc), false);
7a5ed437 260
064097d9 261 return sioc;
33897dc7
NT
262}
263
75822a12
DB
264
265static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
266{
267 Object *obj;
268 QCryptoTLSCreds *creds;
269
270 obj = object_resolve_path_component(
271 object_get_objects_root(), id);
272 if (!obj) {
273 error_setg(errp, "No TLS credentials with id '%s'",
274 id);
275 return NULL;
276 }
277 creds = (QCryptoTLSCreds *)
278 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
279 if (!creds) {
280 error_setg(errp, "Object with id '%s' is not TLS credentials",
281 id);
282 return NULL;
283 }
284
285 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
286 error_setg(errp,
287 "Expecting TLS credentials with a client endpoint");
288 return NULL;
289 }
290 object_ref(obj);
291 return creds;
292}
293
294
015a1036
HR
295static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
296 Error **errp)
33897dc7
NT
297{
298 BDRVNBDState *s = bs->opaque;
e2bc625f 299 char *export = NULL;
75822a12 300 QIOChannelSocket *sioc = NULL;
7a5ed437 301 SocketAddress *saddr;
75822a12
DB
302 const char *tlscredsid;
303 QCryptoTLSCreds *tlscreds = NULL;
304 const char *hostname = NULL;
305 int ret = -EINVAL;
ae255e52 306
33897dc7 307 /* Pop the config into our state object. Exit if invalid. */
7a5ed437
DB
308 saddr = nbd_config(s, options, &export, errp);
309 if (!saddr) {
75822a12
DB
310 goto error;
311 }
312
313 tlscredsid = g_strdup(qdict_get_try_str(options, "tls-creds"));
314 if (tlscredsid) {
315 qdict_del(options, "tls-creds");
316 tlscreds = nbd_get_tls_creds(tlscredsid, errp);
317 if (!tlscreds) {
318 goto error;
319 }
320
321 if (saddr->type != SOCKET_ADDRESS_KIND_INET) {
322 error_setg(errp, "TLS only supported over IP sockets");
323 goto error;
324 }
32bafa8f 325 hostname = saddr->u.inet.data->host;
33897dc7
NT
326 }
327
328 /* establish TCP connection, return error if it fails
329 * TODO: Configurable retry-until-timeout behaviour.
330 */
064097d9 331 sioc = nbd_establish_connection(saddr, errp);
064097d9 332 if (!sioc) {
75822a12
DB
333 ret = -ECONNREFUSED;
334 goto error;
2c7989a9
PB
335 }
336
2302c1ca 337 /* NBD handshake */
75822a12
DB
338 ret = nbd_client_init(bs, sioc, export,
339 tlscreds, hostname, errp);
340 error:
341 if (sioc) {
342 object_unref(OBJECT(sioc));
343 }
344 if (tlscreds) {
345 object_unref(OBJECT(tlscreds));
346 }
347 qapi_free_SocketAddress(saddr);
e2bc625f 348 g_free(export);
75822a12 349 return ret;
e183ef75
PB
350}
351
d9b09f13
PB
352static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
353 int nb_sectors, QEMUIOVector *qiov)
354{
f53a829b 355 return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov);
d9b09f13
PB
356}
357
358static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
359 int nb_sectors, QEMUIOVector *qiov)
360{
f53a829b 361 return nbd_client_co_writev(bs, sector_num, nb_sectors, qiov);
d9b09f13
PB
362}
363
1486d04a
PB
364static int nbd_co_flush(BlockDriverState *bs)
365{
f53a829b 366 return nbd_client_co_flush(bs);
1486d04a
PB
367}
368
fa21e6fa
DL
369static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
370{
371 bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS;
372 bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;
373}
374
7a706633
PB
375static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
376 int nb_sectors)
377{
f53a829b 378 return nbd_client_co_discard(bs, sector_num, nb_sectors);
7a706633
PB
379}
380
75818250
TS
381static void nbd_close(BlockDriverState *bs)
382{
f53a829b 383 nbd_client_close(bs);
75818250
TS
384}
385
386static int64_t nbd_getlength(BlockDriverState *bs)
387{
388 BDRVNBDState *s = bs->opaque;
389
2302c1ca 390 return s->client.size;
75818250
TS
391}
392
69447cd8
SH
393static void nbd_detach_aio_context(BlockDriverState *bs)
394{
f53a829b 395 nbd_client_detach_aio_context(bs);
69447cd8
SH
396}
397
398static void nbd_attach_aio_context(BlockDriverState *bs,
399 AioContext *new_context)
400{
f53a829b 401 nbd_client_attach_aio_context(bs, new_context);
69447cd8
SH
402}
403
4cdd01d3 404static void nbd_refresh_filename(BlockDriverState *bs, QDict *options)
2019d68b 405{
2019d68b 406 QDict *opts = qdict_new();
4cdd01d3
KW
407 const char *path = qdict_get_try_str(options, "path");
408 const char *host = qdict_get_try_str(options, "host");
409 const char *port = qdict_get_try_str(options, "port");
410 const char *export = qdict_get_try_str(options, "export");
75822a12 411 const char *tlscreds = qdict_get_try_str(options, "tls-creds");
2019d68b
HR
412
413 qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
414
ec0de768 415 if (path && export) {
2019d68b 416 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
417 "nbd+unix:///%s?socket=%s", export, path);
418 } else if (path && !export) {
2019d68b 419 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
420 "nbd+unix://?socket=%s", path);
421 } else if (!path && export && port) {
422 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
423 "nbd://%s:%s/%s", host, port, export);
424 } else if (!path && export && !port) {
425 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
426 "nbd://%s/%s", host, export);
427 } else if (!path && !export && port) {
428 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
429 "nbd://%s:%s", host, port);
430 } else if (!path && !export && !port) {
2019d68b 431 snprintf(bs->exact_filename, sizeof(bs->exact_filename),
ec0de768
HR
432 "nbd://%s", host);
433 }
434
435 if (path) {
436 qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path)));
437 } else if (port) {
2019d68b
HR
438 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
439 qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port)));
ec0de768
HR
440 } else {
441 qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
442 }
443 if (export) {
444 qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export)));
2019d68b 445 }
75822a12
DB
446 if (tlscreds) {
447 qdict_put_obj(opts, "tls-creds", QOBJECT(qstring_from_str(tlscreds)));
448 }
2019d68b
HR
449
450 bs->full_open_options = opts;
451}
452
5efa9d5a 453static BlockDriver bdrv_nbd = {
69447cd8
SH
454 .format_name = "nbd",
455 .protocol_name = "nbd",
456 .instance_size = sizeof(BDRVNBDState),
457 .bdrv_parse_filename = nbd_parse_filename,
458 .bdrv_file_open = nbd_open,
459 .bdrv_co_readv = nbd_co_readv,
460 .bdrv_co_writev = nbd_co_writev,
461 .bdrv_close = nbd_close,
462 .bdrv_co_flush_to_os = nbd_co_flush,
463 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 464 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
465 .bdrv_getlength = nbd_getlength,
466 .bdrv_detach_aio_context = nbd_detach_aio_context,
467 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 468 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
469};
470
471static BlockDriver bdrv_nbd_tcp = {
69447cd8
SH
472 .format_name = "nbd",
473 .protocol_name = "nbd+tcp",
474 .instance_size = sizeof(BDRVNBDState),
475 .bdrv_parse_filename = nbd_parse_filename,
476 .bdrv_file_open = nbd_open,
477 .bdrv_co_readv = nbd_co_readv,
478 .bdrv_co_writev = nbd_co_writev,
479 .bdrv_close = nbd_close,
480 .bdrv_co_flush_to_os = nbd_co_flush,
481 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 482 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
483 .bdrv_getlength = nbd_getlength,
484 .bdrv_detach_aio_context = nbd_detach_aio_context,
485 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 486 .bdrv_refresh_filename = nbd_refresh_filename,
1d7d2a9d
PB
487};
488
489static BlockDriver bdrv_nbd_unix = {
69447cd8
SH
490 .format_name = "nbd",
491 .protocol_name = "nbd+unix",
492 .instance_size = sizeof(BDRVNBDState),
493 .bdrv_parse_filename = nbd_parse_filename,
494 .bdrv_file_open = nbd_open,
495 .bdrv_co_readv = nbd_co_readv,
496 .bdrv_co_writev = nbd_co_writev,
497 .bdrv_close = nbd_close,
498 .bdrv_co_flush_to_os = nbd_co_flush,
499 .bdrv_co_discard = nbd_co_discard,
fa21e6fa 500 .bdrv_refresh_limits = nbd_refresh_limits,
69447cd8
SH
501 .bdrv_getlength = nbd_getlength,
502 .bdrv_detach_aio_context = nbd_detach_aio_context,
503 .bdrv_attach_aio_context = nbd_attach_aio_context,
2019d68b 504 .bdrv_refresh_filename = nbd_refresh_filename,
75818250 505};
5efa9d5a
AL
506
507static void bdrv_nbd_init(void)
508{
509 bdrv_register(&bdrv_nbd);
1d7d2a9d
PB
510 bdrv_register(&bdrv_nbd_tcp);
511 bdrv_register(&bdrv_nbd_unix);
5efa9d5a
AL
512}
513
514block_init(bdrv_nbd_init);