]>
Commit | Line | Data |
---|---|---|
75818250 TS |
1 | /* |
2 | * QEMU Block driver for NBD | |
3 | * | |
4 | * Copyright (C) 2008 Bull S.A.S. | |
bd5921b4 | 5 | * Author: Laurent Vivier <[email protected]> |
75818250 TS |
6 | * |
7 | * Some parts: | |
8 | * Copyright (C) 2007 Anthony Liguori <[email protected]> | |
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 | ||
2302c1ca | 29 | #include "block/nbd-client.h" |
1de7afc9 | 30 | #include "qemu/uri.h" |
737e150e | 31 | #include "block/block_int.h" |
1de7afc9 PB |
32 | #include "qemu/module.h" |
33 | #include "qemu/sockets.h" | |
2019d68b | 34 | #include "qapi/qmp/qdict.h" |
f53a1feb KW |
35 | #include "qapi/qmp/qjson.h" |
36 | #include "qapi/qmp/qint.h" | |
2019d68b | 37 | #include "qapi/qmp/qstring.h" |
75818250 TS |
38 | |
39 | #include <sys/types.h> | |
40 | #include <unistd.h> | |
75818250 | 41 | |
1d45f8b5 LV |
42 | #define EN_OPTSTR ":exportname=" |
43 | ||
75818250 | 44 | typedef struct BDRVNBDState { |
2302c1ca | 45 | NbdClientSession client; |
f53a1feb | 46 | QemuOpts *socket_opts; |
75818250 TS |
47 | } BDRVNBDState; |
48 | ||
f53a1feb | 49 | static int nbd_parse_uri(const char *filename, QDict *options) |
1d7d2a9d PB |
50 | { |
51 | URI *uri; | |
52 | const char *p; | |
53 | QueryParams *qp = NULL; | |
54 | int ret = 0; | |
f53a1feb | 55 | bool is_unix; |
1d7d2a9d PB |
56 | |
57 | uri = uri_parse(filename); | |
58 | if (!uri) { | |
59 | return -EINVAL; | |
60 | } | |
61 | ||
62 | /* transport */ | |
63 | if (!strcmp(uri->scheme, "nbd")) { | |
f53a1feb | 64 | is_unix = false; |
1d7d2a9d | 65 | } else if (!strcmp(uri->scheme, "nbd+tcp")) { |
f53a1feb | 66 | is_unix = false; |
1d7d2a9d | 67 | } else if (!strcmp(uri->scheme, "nbd+unix")) { |
f53a1feb | 68 | is_unix = true; |
1d7d2a9d PB |
69 | } else { |
70 | ret = -EINVAL; | |
71 | goto out; | |
72 | } | |
73 | ||
74 | p = uri->path ? uri->path : "/"; | |
75 | p += strspn(p, "/"); | |
76 | if (p[0]) { | |
f53a1feb | 77 | qdict_put(options, "export", qstring_from_str(p)); |
1d7d2a9d PB |
78 | } |
79 | ||
80 | qp = query_params_parse(uri->query); | |
f53a1feb | 81 | if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { |
1d7d2a9d PB |
82 | ret = -EINVAL; |
83 | goto out; | |
84 | } | |
85 | ||
f53a1feb | 86 | if (is_unix) { |
1d7d2a9d PB |
87 | /* nbd+unix:///export?socket=path */ |
88 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
89 | ret = -EINVAL; | |
90 | goto out; | |
91 | } | |
f53a1feb | 92 | qdict_put(options, "path", qstring_from_str(qp->p[0].value)); |
1d7d2a9d | 93 | } else { |
23307908 | 94 | QString *host; |
bebbf7fa | 95 | /* nbd[+tcp]://host[:port]/export */ |
1d7d2a9d PB |
96 | if (!uri->server) { |
97 | ret = -EINVAL; | |
98 | goto out; | |
99 | } | |
f17c90be | 100 | |
23307908 JT |
101 | /* strip braces from literal IPv6 address */ |
102 | if (uri->server[0] == '[') { | |
103 | host = qstring_from_substr(uri->server, 1, | |
104 | strlen(uri->server) - 2); | |
105 | } else { | |
106 | host = qstring_from_str(uri->server); | |
107 | } | |
108 | ||
109 | qdict_put(options, "host", host); | |
bebbf7fa KW |
110 | if (uri->port) { |
111 | char* port_str = g_strdup_printf("%d", uri->port); | |
112 | qdict_put(options, "port", qstring_from_str(port_str)); | |
113 | g_free(port_str); | |
114 | } | |
1d7d2a9d PB |
115 | } |
116 | ||
117 | out: | |
118 | if (qp) { | |
119 | query_params_free(qp); | |
120 | } | |
121 | uri_free(uri); | |
122 | return ret; | |
123 | } | |
124 | ||
6963a30d KW |
125 | static void nbd_parse_filename(const char *filename, QDict *options, |
126 | Error **errp) | |
75818250 | 127 | { |
1d45f8b5 | 128 | char *file; |
33897dc7 NT |
129 | char *export_name; |
130 | const char *host_spec; | |
75818250 | 131 | const char *unixpath; |
75818250 | 132 | |
681e7ad0 KW |
133 | if (qdict_haskey(options, "host") |
134 | || qdict_haskey(options, "port") | |
135 | || qdict_haskey(options, "path")) | |
136 | { | |
137 | error_setg(errp, "host/port/path and a file name may not be specified " | |
138 | "at the same time"); | |
139 | return; | |
140 | } | |
141 | ||
1d7d2a9d | 142 | if (strstr(filename, "://")) { |
6963a30d KW |
143 | int ret = nbd_parse_uri(filename, options); |
144 | if (ret < 0) { | |
145 | error_setg(errp, "No valid URL specified"); | |
146 | } | |
147 | return; | |
1d7d2a9d PB |
148 | } |
149 | ||
7267c094 | 150 | file = g_strdup(filename); |
1d45f8b5 | 151 | |
33897dc7 NT |
152 | export_name = strstr(file, EN_OPTSTR); |
153 | if (export_name) { | |
154 | if (export_name[strlen(EN_OPTSTR)] == 0) { | |
1d45f8b5 LV |
155 | goto out; |
156 | } | |
33897dc7 NT |
157 | export_name[0] = 0; /* truncate 'file' */ |
158 | export_name += strlen(EN_OPTSTR); | |
f53a1feb KW |
159 | |
160 | qdict_put(options, "export", qstring_from_str(export_name)); | |
1d45f8b5 LV |
161 | } |
162 | ||
33897dc7 NT |
163 | /* extract the host_spec - fail if it's not nbd:... */ |
164 | if (!strstart(file, "nbd:", &host_spec)) { | |
6963a30d | 165 | error_setg(errp, "File name string for NBD must start with 'nbd:'"); |
1d45f8b5 LV |
166 | goto out; |
167 | } | |
75818250 | 168 | |
f53a1feb | 169 | if (!*host_spec) { |
f53a1feb KW |
170 | goto out; |
171 | } | |
172 | ||
33897dc7 NT |
173 | /* are we a UNIX or TCP socket? */ |
174 | if (strstart(host_spec, "unix:", &unixpath)) { | |
f53a1feb | 175 | qdict_put(options, "path", qstring_from_str(unixpath)); |
75818250 | 176 | } else { |
f53a1feb KW |
177 | InetSocketAddress *addr = NULL; |
178 | ||
6963a30d | 179 | addr = inet_parse(host_spec, errp); |
92de9012 | 180 | if (!addr) { |
f17c90be KW |
181 | goto out; |
182 | } | |
75818250 | 183 | |
f53a1feb KW |
184 | qdict_put(options, "host", qstring_from_str(addr->host)); |
185 | qdict_put(options, "port", qstring_from_str(addr->port)); | |
186 | qapi_free_InetSocketAddress(addr); | |
187 | } | |
75818250 | 188 | |
33897dc7 | 189 | out: |
7267c094 | 190 | g_free(file); |
f53a1feb KW |
191 | } |
192 | ||
77e8b9ca PB |
193 | static void nbd_config(BDRVNBDState *s, QDict *options, char **export, |
194 | Error **errp) | |
f53a1feb KW |
195 | { |
196 | Error *local_err = NULL; | |
197 | ||
a69d9af4 PB |
198 | if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) { |
199 | if (qdict_haskey(options, "path")) { | |
77e8b9ca | 200 | error_setg(errp, "path and host may not be used at the same time."); |
a69d9af4 | 201 | } else { |
77e8b9ca | 202 | error_setg(errp, "one of path and host must be specified."); |
681e7ad0 | 203 | } |
77e8b9ca | 204 | return; |
33897dc7 | 205 | } |
f53a1feb | 206 | |
a69d9af4 | 207 | s->client.is_unix = qdict_haskey(options, "path"); |
87ea75d5 PC |
208 | s->socket_opts = qemu_opts_create(&socket_optslist, NULL, 0, |
209 | &error_abort); | |
f53a1feb KW |
210 | |
211 | qemu_opts_absorb_qdict(s->socket_opts, options, &local_err); | |
84d18f06 | 212 | if (local_err) { |
77e8b9ca PB |
213 | error_propagate(errp, local_err); |
214 | return; | |
f53a1feb KW |
215 | } |
216 | ||
bebbf7fa | 217 | if (!qemu_opt_get(s->socket_opts, "port")) { |
39101f25 MA |
218 | qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT, |
219 | &error_abort); | |
bebbf7fa KW |
220 | } |
221 | ||
e2bc625f MAL |
222 | *export = g_strdup(qdict_get_try_str(options, "export")); |
223 | if (*export) { | |
f53a1feb KW |
224 | qdict_del(options, "export"); |
225 | } | |
33897dc7 | 226 | } |
1d45f8b5 | 227 | |
f53a829b HR |
228 | NbdClientSession *nbd_get_client_session(BlockDriverState *bs) |
229 | { | |
230 | BDRVNBDState *s = bs->opaque; | |
231 | return &s->client; | |
232 | } | |
233 | ||
77e8b9ca | 234 | static int nbd_establish_connection(BlockDriverState *bs, Error **errp) |
33897dc7 NT |
235 | { |
236 | BDRVNBDState *s = bs->opaque; | |
237 | int sock; | |
75818250 | 238 | |
2302c1ca | 239 | if (s->client.is_unix) { |
77e8b9ca | 240 | sock = unix_connect_opts(s->socket_opts, errp, NULL, NULL); |
33897dc7 | 241 | } else { |
77e8b9ca | 242 | sock = inet_connect_opts(s->socket_opts, errp, NULL, NULL); |
97ebbab0 SH |
243 | if (sock >= 0) { |
244 | socket_set_nodelay(sock); | |
245 | } | |
75818250 TS |
246 | } |
247 | ||
33897dc7 | 248 | /* Failed to establish connection */ |
fc19f8a0 | 249 | if (sock < 0) { |
33897dc7 NT |
250 | logout("Failed to establish connection to NBD server\n"); |
251 | return -errno; | |
1d45f8b5 | 252 | } |
75818250 | 253 | |
2302c1ca | 254 | return sock; |
33897dc7 NT |
255 | } |
256 | ||
015a1036 HR |
257 | static int nbd_open(BlockDriverState *bs, QDict *options, int flags, |
258 | Error **errp) | |
33897dc7 NT |
259 | { |
260 | BDRVNBDState *s = bs->opaque; | |
e2bc625f | 261 | char *export = NULL; |
2302c1ca | 262 | int result, sock; |
77e8b9ca | 263 | Error *local_err = NULL; |
ae255e52 | 264 | |
33897dc7 | 265 | /* Pop the config into our state object. Exit if invalid. */ |
77e8b9ca PB |
266 | nbd_config(s, options, &export, &local_err); |
267 | if (local_err) { | |
268 | error_propagate(errp, local_err); | |
269 | return -EINVAL; | |
33897dc7 NT |
270 | } |
271 | ||
272 | /* establish TCP connection, return error if it fails | |
273 | * TODO: Configurable retry-until-timeout behaviour. | |
274 | */ | |
77e8b9ca | 275 | sock = nbd_establish_connection(bs, errp); |
2302c1ca | 276 | if (sock < 0) { |
9d0b65e6 | 277 | g_free(export); |
2302c1ca | 278 | return sock; |
2c7989a9 PB |
279 | } |
280 | ||
2302c1ca | 281 | /* NBD handshake */ |
f53a829b | 282 | result = nbd_client_init(bs, sock, export, errp); |
e2bc625f MAL |
283 | g_free(export); |
284 | return result; | |
e183ef75 PB |
285 | } |
286 | ||
d9b09f13 PB |
287 | static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num, |
288 | int nb_sectors, QEMUIOVector *qiov) | |
289 | { | |
f53a829b | 290 | return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov); |
d9b09f13 PB |
291 | } |
292 | ||
293 | static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num, | |
294 | int nb_sectors, QEMUIOVector *qiov) | |
295 | { | |
f53a829b | 296 | return nbd_client_co_writev(bs, sector_num, nb_sectors, qiov); |
d9b09f13 PB |
297 | } |
298 | ||
1486d04a PB |
299 | static int nbd_co_flush(BlockDriverState *bs) |
300 | { | |
f53a829b | 301 | return nbd_client_co_flush(bs); |
1486d04a PB |
302 | } |
303 | ||
fa21e6fa DL |
304 | static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) |
305 | { | |
306 | bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS; | |
307 | bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS; | |
308 | } | |
309 | ||
7a706633 PB |
310 | static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num, |
311 | int nb_sectors) | |
312 | { | |
f53a829b | 313 | return nbd_client_co_discard(bs, sector_num, nb_sectors); |
7a706633 PB |
314 | } |
315 | ||
75818250 TS |
316 | static void nbd_close(BlockDriverState *bs) |
317 | { | |
d2d979c6 | 318 | BDRVNBDState *s = bs->opaque; |
d2d979c6 | 319 | |
2302c1ca | 320 | qemu_opts_del(s->socket_opts); |
f53a829b | 321 | nbd_client_close(bs); |
75818250 TS |
322 | } |
323 | ||
324 | static int64_t nbd_getlength(BlockDriverState *bs) | |
325 | { | |
326 | BDRVNBDState *s = bs->opaque; | |
327 | ||
2302c1ca | 328 | return s->client.size; |
75818250 TS |
329 | } |
330 | ||
69447cd8 SH |
331 | static void nbd_detach_aio_context(BlockDriverState *bs) |
332 | { | |
f53a829b | 333 | nbd_client_detach_aio_context(bs); |
69447cd8 SH |
334 | } |
335 | ||
336 | static void nbd_attach_aio_context(BlockDriverState *bs, | |
337 | AioContext *new_context) | |
338 | { | |
f53a829b | 339 | nbd_client_attach_aio_context(bs, new_context); |
69447cd8 SH |
340 | } |
341 | ||
2019d68b HR |
342 | static void nbd_refresh_filename(BlockDriverState *bs) |
343 | { | |
2019d68b | 344 | QDict *opts = qdict_new(); |
ec0de768 HR |
345 | const char *path = qdict_get_try_str(bs->options, "path"); |
346 | const char *host = qdict_get_try_str(bs->options, "host"); | |
347 | const char *port = qdict_get_try_str(bs->options, "port"); | |
348 | const char *export = qdict_get_try_str(bs->options, "export"); | |
2019d68b HR |
349 | |
350 | qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd"))); | |
351 | ||
ec0de768 | 352 | if (path && export) { |
2019d68b | 353 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
ec0de768 HR |
354 | "nbd+unix:///%s?socket=%s", export, path); |
355 | } else if (path && !export) { | |
2019d68b | 356 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
ec0de768 HR |
357 | "nbd+unix://?socket=%s", path); |
358 | } else if (!path && export && port) { | |
359 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
360 | "nbd://%s:%s/%s", host, port, export); | |
361 | } else if (!path && export && !port) { | |
362 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
363 | "nbd://%s/%s", host, export); | |
364 | } else if (!path && !export && port) { | |
365 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), | |
366 | "nbd://%s:%s", host, port); | |
367 | } else if (!path && !export && !port) { | |
2019d68b | 368 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
ec0de768 HR |
369 | "nbd://%s", host); |
370 | } | |
371 | ||
372 | if (path) { | |
373 | qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path))); | |
374 | } else if (port) { | |
2019d68b HR |
375 | qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host))); |
376 | qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port))); | |
ec0de768 HR |
377 | } else { |
378 | qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host))); | |
379 | } | |
380 | if (export) { | |
381 | qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export))); | |
2019d68b HR |
382 | } |
383 | ||
384 | bs->full_open_options = opts; | |
385 | } | |
386 | ||
5efa9d5a | 387 | static BlockDriver bdrv_nbd = { |
69447cd8 SH |
388 | .format_name = "nbd", |
389 | .protocol_name = "nbd", | |
390 | .instance_size = sizeof(BDRVNBDState), | |
391 | .bdrv_parse_filename = nbd_parse_filename, | |
392 | .bdrv_file_open = nbd_open, | |
393 | .bdrv_co_readv = nbd_co_readv, | |
394 | .bdrv_co_writev = nbd_co_writev, | |
395 | .bdrv_close = nbd_close, | |
396 | .bdrv_co_flush_to_os = nbd_co_flush, | |
397 | .bdrv_co_discard = nbd_co_discard, | |
fa21e6fa | 398 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
399 | .bdrv_getlength = nbd_getlength, |
400 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
401 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 402 | .bdrv_refresh_filename = nbd_refresh_filename, |
1d7d2a9d PB |
403 | }; |
404 | ||
405 | static BlockDriver bdrv_nbd_tcp = { | |
69447cd8 SH |
406 | .format_name = "nbd", |
407 | .protocol_name = "nbd+tcp", | |
408 | .instance_size = sizeof(BDRVNBDState), | |
409 | .bdrv_parse_filename = nbd_parse_filename, | |
410 | .bdrv_file_open = nbd_open, | |
411 | .bdrv_co_readv = nbd_co_readv, | |
412 | .bdrv_co_writev = nbd_co_writev, | |
413 | .bdrv_close = nbd_close, | |
414 | .bdrv_co_flush_to_os = nbd_co_flush, | |
415 | .bdrv_co_discard = nbd_co_discard, | |
fa21e6fa | 416 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
417 | .bdrv_getlength = nbd_getlength, |
418 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
419 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 420 | .bdrv_refresh_filename = nbd_refresh_filename, |
1d7d2a9d PB |
421 | }; |
422 | ||
423 | static BlockDriver bdrv_nbd_unix = { | |
69447cd8 SH |
424 | .format_name = "nbd", |
425 | .protocol_name = "nbd+unix", | |
426 | .instance_size = sizeof(BDRVNBDState), | |
427 | .bdrv_parse_filename = nbd_parse_filename, | |
428 | .bdrv_file_open = nbd_open, | |
429 | .bdrv_co_readv = nbd_co_readv, | |
430 | .bdrv_co_writev = nbd_co_writev, | |
431 | .bdrv_close = nbd_close, | |
432 | .bdrv_co_flush_to_os = nbd_co_flush, | |
433 | .bdrv_co_discard = nbd_co_discard, | |
fa21e6fa | 434 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
435 | .bdrv_getlength = nbd_getlength, |
436 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
437 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 438 | .bdrv_refresh_filename = nbd_refresh_filename, |
75818250 | 439 | }; |
5efa9d5a AL |
440 | |
441 | static void bdrv_nbd_init(void) | |
442 | { | |
443 | bdrv_register(&bdrv_nbd); | |
1d7d2a9d PB |
444 | bdrv_register(&bdrv_nbd_tcp); |
445 | bdrv_register(&bdrv_nbd_unix); | |
5efa9d5a AL |
446 | } |
447 | ||
448 | block_init(bdrv_nbd_init); |