]>
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 | ||
80c71a24 | 29 | #include "qemu/osdep.h" |
0d8c41da | 30 | #include "nbd-client.h" |
609f45ea | 31 | #include "block/qdict.h" |
da34e65c | 32 | #include "qapi/error.h" |
1de7afc9 | 33 | #include "qemu/uri.h" |
737e150e | 34 | #include "block/block_int.h" |
1de7afc9 | 35 | #include "qemu/module.h" |
922a01a0 | 36 | #include "qemu/option.h" |
9af23989 | 37 | #include "qapi/qapi-visit-sockets.h" |
491d6c7c HR |
38 | #include "qapi/qobject-input-visitor.h" |
39 | #include "qapi/qobject-output-visitor.h" | |
2019d68b | 40 | #include "qapi/qmp/qdict.h" |
2019d68b | 41 | #include "qapi/qmp/qstring.h" |
f348b6d1 | 42 | #include "qemu/cutils.h" |
75818250 | 43 | |
1d45f8b5 LV |
44 | #define EN_OPTSTR ":exportname=" |
45 | ||
75818250 | 46 | typedef struct BDRVNBDState { |
10676b81 | 47 | NBDClientSession client; |
03504d05 HR |
48 | |
49 | /* For nbd_refresh_filename() */ | |
62cf396b | 50 | SocketAddress *saddr; |
491d6c7c | 51 | char *export, *tlscredsid; |
75818250 TS |
52 | } BDRVNBDState; |
53 | ||
f53a1feb | 54 | static int nbd_parse_uri(const char *filename, QDict *options) |
1d7d2a9d PB |
55 | { |
56 | URI *uri; | |
57 | const char *p; | |
58 | QueryParams *qp = NULL; | |
59 | int ret = 0; | |
f53a1feb | 60 | bool is_unix; |
1d7d2a9d PB |
61 | |
62 | uri = uri_parse(filename); | |
63 | if (!uri) { | |
64 | return -EINVAL; | |
65 | } | |
66 | ||
67 | /* transport */ | |
f69165a8 | 68 | if (!g_strcmp0(uri->scheme, "nbd")) { |
f53a1feb | 69 | is_unix = false; |
f69165a8 | 70 | } else if (!g_strcmp0(uri->scheme, "nbd+tcp")) { |
f53a1feb | 71 | is_unix = false; |
f69165a8 | 72 | } else if (!g_strcmp0(uri->scheme, "nbd+unix")) { |
f53a1feb | 73 | is_unix = true; |
1d7d2a9d PB |
74 | } else { |
75 | ret = -EINVAL; | |
76 | goto out; | |
77 | } | |
78 | ||
79 | p = uri->path ? uri->path : "/"; | |
80 | p += strspn(p, "/"); | |
81 | if (p[0]) { | |
46f5ac20 | 82 | qdict_put_str(options, "export", p); |
1d7d2a9d PB |
83 | } |
84 | ||
85 | qp = query_params_parse(uri->query); | |
f53a1feb | 86 | if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { |
1d7d2a9d PB |
87 | ret = -EINVAL; |
88 | goto out; | |
89 | } | |
90 | ||
f53a1feb | 91 | if (is_unix) { |
1d7d2a9d PB |
92 | /* nbd+unix:///export?socket=path */ |
93 | if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) { | |
94 | ret = -EINVAL; | |
95 | goto out; | |
96 | } | |
46f5ac20 EB |
97 | qdict_put_str(options, "server.type", "unix"); |
98 | qdict_put_str(options, "server.path", qp->p[0].value); | |
1d7d2a9d | 99 | } else { |
23307908 | 100 | QString *host; |
f84d431b HR |
101 | char *port_str; |
102 | ||
bebbf7fa | 103 | /* nbd[+tcp]://host[:port]/export */ |
1d7d2a9d PB |
104 | if (!uri->server) { |
105 | ret = -EINVAL; | |
106 | goto out; | |
107 | } | |
f17c90be | 108 | |
23307908 JT |
109 | /* strip braces from literal IPv6 address */ |
110 | if (uri->server[0] == '[') { | |
111 | host = qstring_from_substr(uri->server, 1, | |
ba891d68 | 112 | strlen(uri->server) - 1); |
23307908 JT |
113 | } else { |
114 | host = qstring_from_str(uri->server); | |
115 | } | |
116 | ||
46f5ac20 | 117 | qdict_put_str(options, "server.type", "inet"); |
9445673e | 118 | qdict_put(options, "server.host", host); |
f84d431b HR |
119 | |
120 | port_str = g_strdup_printf("%d", uri->port ?: NBD_DEFAULT_PORT); | |
46f5ac20 | 121 | qdict_put_str(options, "server.port", port_str); |
f84d431b | 122 | g_free(port_str); |
1d7d2a9d PB |
123 | } |
124 | ||
125 | out: | |
126 | if (qp) { | |
127 | query_params_free(qp); | |
128 | } | |
129 | uri_free(uri); | |
130 | return ret; | |
131 | } | |
132 | ||
48c38e0b HR |
133 | static bool nbd_has_filename_options_conflict(QDict *options, Error **errp) |
134 | { | |
135 | const QDictEntry *e; | |
136 | ||
137 | for (e = qdict_first(options); e; e = qdict_next(options, e)) { | |
138 | if (!strcmp(e->key, "host") || | |
139 | !strcmp(e->key, "port") || | |
140 | !strcmp(e->key, "path") || | |
491d6c7c HR |
141 | !strcmp(e->key, "export") || |
142 | strstart(e->key, "server.", NULL)) | |
48c38e0b HR |
143 | { |
144 | error_setg(errp, "Option '%s' cannot be used with a file name", | |
145 | e->key); | |
146 | return true; | |
147 | } | |
148 | } | |
149 | ||
150 | return false; | |
151 | } | |
152 | ||
6963a30d KW |
153 | static void nbd_parse_filename(const char *filename, QDict *options, |
154 | Error **errp) | |
75818250 | 155 | { |
1d45f8b5 | 156 | char *file; |
33897dc7 NT |
157 | char *export_name; |
158 | const char *host_spec; | |
75818250 | 159 | const char *unixpath; |
75818250 | 160 | |
48c38e0b | 161 | if (nbd_has_filename_options_conflict(options, errp)) { |
681e7ad0 KW |
162 | return; |
163 | } | |
164 | ||
1d7d2a9d | 165 | if (strstr(filename, "://")) { |
6963a30d KW |
166 | int ret = nbd_parse_uri(filename, options); |
167 | if (ret < 0) { | |
168 | error_setg(errp, "No valid URL specified"); | |
169 | } | |
170 | return; | |
1d7d2a9d PB |
171 | } |
172 | ||
7267c094 | 173 | file = g_strdup(filename); |
1d45f8b5 | 174 | |
33897dc7 NT |
175 | export_name = strstr(file, EN_OPTSTR); |
176 | if (export_name) { | |
177 | if (export_name[strlen(EN_OPTSTR)] == 0) { | |
1d45f8b5 LV |
178 | goto out; |
179 | } | |
33897dc7 NT |
180 | export_name[0] = 0; /* truncate 'file' */ |
181 | export_name += strlen(EN_OPTSTR); | |
f53a1feb | 182 | |
46f5ac20 | 183 | qdict_put_str(options, "export", export_name); |
1d45f8b5 LV |
184 | } |
185 | ||
33897dc7 NT |
186 | /* extract the host_spec - fail if it's not nbd:... */ |
187 | if (!strstart(file, "nbd:", &host_spec)) { | |
6963a30d | 188 | error_setg(errp, "File name string for NBD must start with 'nbd:'"); |
1d45f8b5 LV |
189 | goto out; |
190 | } | |
75818250 | 191 | |
f53a1feb | 192 | if (!*host_spec) { |
f53a1feb KW |
193 | goto out; |
194 | } | |
195 | ||
33897dc7 NT |
196 | /* are we a UNIX or TCP socket? */ |
197 | if (strstart(host_spec, "unix:", &unixpath)) { | |
46f5ac20 EB |
198 | qdict_put_str(options, "server.type", "unix"); |
199 | qdict_put_str(options, "server.path", unixpath); | |
75818250 | 200 | } else { |
0785bd7a | 201 | InetSocketAddress *addr = g_new(InetSocketAddress, 1); |
f53a1feb | 202 | |
0785bd7a MA |
203 | if (inet_parse(addr, host_spec, errp)) { |
204 | goto out_inet; | |
f17c90be | 205 | } |
75818250 | 206 | |
46f5ac20 EB |
207 | qdict_put_str(options, "server.type", "inet"); |
208 | qdict_put_str(options, "server.host", addr->host); | |
209 | qdict_put_str(options, "server.port", addr->port); | |
0785bd7a | 210 | out_inet: |
f53a1feb KW |
211 | qapi_free_InetSocketAddress(addr); |
212 | } | |
75818250 | 213 | |
33897dc7 | 214 | out: |
7267c094 | 215 | g_free(file); |
f53a1feb KW |
216 | } |
217 | ||
491d6c7c HR |
218 | static bool nbd_process_legacy_socket_options(QDict *output_options, |
219 | QemuOpts *legacy_opts, | |
220 | Error **errp) | |
f53a1feb | 221 | { |
491d6c7c HR |
222 | const char *path = qemu_opt_get(legacy_opts, "path"); |
223 | const char *host = qemu_opt_get(legacy_opts, "host"); | |
224 | const char *port = qemu_opt_get(legacy_opts, "port"); | |
225 | const QDictEntry *e; | |
f53a1feb | 226 | |
491d6c7c HR |
227 | if (!path && !host && !port) { |
228 | return true; | |
229 | } | |
03504d05 | 230 | |
491d6c7c HR |
231 | for (e = qdict_first(output_options); e; e = qdict_next(output_options, e)) |
232 | { | |
233 | if (strstart(e->key, "server.", NULL)) { | |
234 | error_setg(errp, "Cannot use 'server' and path/host/port at the " | |
235 | "same time"); | |
236 | return false; | |
681e7ad0 | 237 | } |
33897dc7 | 238 | } |
491d6c7c HR |
239 | |
240 | if (path && host) { | |
241 | error_setg(errp, "path and host may not be used at the same time"); | |
242 | return false; | |
243 | } else if (path) { | |
244 | if (port) { | |
245 | error_setg(errp, "port may not be used without host"); | |
246 | return false; | |
247 | } | |
248 | ||
46f5ac20 EB |
249 | qdict_put_str(output_options, "server.type", "unix"); |
250 | qdict_put_str(output_options, "server.path", path); | |
491d6c7c | 251 | } else if (host) { |
46f5ac20 EB |
252 | qdict_put_str(output_options, "server.type", "inet"); |
253 | qdict_put_str(output_options, "server.host", host); | |
254 | qdict_put_str(output_options, "server.port", | |
255 | port ?: stringify(NBD_DEFAULT_PORT)); | |
442045cb | 256 | } |
f53a1feb | 257 | |
491d6c7c HR |
258 | return true; |
259 | } | |
f53a1feb | 260 | |
62cf396b MA |
261 | static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, |
262 | Error **errp) | |
491d6c7c | 263 | { |
62cf396b | 264 | SocketAddress *saddr = NULL; |
491d6c7c | 265 | QDict *addr = NULL; |
491d6c7c HR |
266 | Visitor *iv = NULL; |
267 | Error *local_err = NULL; | |
268 | ||
269 | qdict_extract_subqdict(options, &addr, "server."); | |
270 | if (!qdict_size(addr)) { | |
271 | error_setg(errp, "NBD server address missing"); | |
272 | goto done; | |
f53a1feb KW |
273 | } |
274 | ||
af91062e MA |
275 | iv = qobject_input_visitor_new_flat_confused(addr, errp); |
276 | if (!iv) { | |
491d6c7c HR |
277 | goto done; |
278 | } | |
bebbf7fa | 279 | |
62cf396b | 280 | visit_type_SocketAddress(iv, NULL, &saddr, &local_err); |
491d6c7c HR |
281 | if (local_err) { |
282 | error_propagate(errp, local_err); | |
283 | goto done; | |
284 | } | |
7a5ed437 | 285 | |
491d6c7c | 286 | done: |
cb3e7f08 | 287 | qobject_unref(addr); |
491d6c7c | 288 | visit_free(iv); |
7a5ed437 | 289 | return saddr; |
33897dc7 | 290 | } |
1d45f8b5 | 291 | |
10676b81 | 292 | NBDClientSession *nbd_get_client_session(BlockDriverState *bs) |
f53a829b HR |
293 | { |
294 | BDRVNBDState *s = bs->opaque; | |
295 | return &s->client; | |
296 | } | |
297 | ||
75822a12 DB |
298 | static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp) |
299 | { | |
300 | Object *obj; | |
301 | QCryptoTLSCreds *creds; | |
302 | ||
303 | obj = object_resolve_path_component( | |
304 | object_get_objects_root(), id); | |
305 | if (!obj) { | |
306 | error_setg(errp, "No TLS credentials with id '%s'", | |
307 | id); | |
308 | return NULL; | |
309 | } | |
310 | creds = (QCryptoTLSCreds *) | |
311 | object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS); | |
312 | if (!creds) { | |
313 | error_setg(errp, "Object with id '%s' is not TLS credentials", | |
314 | id); | |
315 | return NULL; | |
316 | } | |
317 | ||
318 | if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) { | |
319 | error_setg(errp, | |
320 | "Expecting TLS credentials with a client endpoint"); | |
321 | return NULL; | |
322 | } | |
323 | object_ref(obj); | |
324 | return creds; | |
325 | } | |
326 | ||
327 | ||
7ccc44fd HR |
328 | static QemuOptsList nbd_runtime_opts = { |
329 | .name = "nbd", | |
330 | .head = QTAILQ_HEAD_INITIALIZER(nbd_runtime_opts.head), | |
331 | .desc = { | |
332 | { | |
333 | .name = "host", | |
334 | .type = QEMU_OPT_STRING, | |
335 | .help = "TCP host to connect to", | |
336 | }, | |
337 | { | |
338 | .name = "port", | |
339 | .type = QEMU_OPT_STRING, | |
340 | .help = "TCP port to connect to", | |
341 | }, | |
342 | { | |
343 | .name = "path", | |
344 | .type = QEMU_OPT_STRING, | |
345 | .help = "Unix socket path to connect to", | |
346 | }, | |
347 | { | |
348 | .name = "export", | |
349 | .type = QEMU_OPT_STRING, | |
350 | .help = "Name of the NBD export to open", | |
351 | }, | |
352 | { | |
353 | .name = "tls-creds", | |
354 | .type = QEMU_OPT_STRING, | |
355 | .help = "ID of the TLS credentials to use", | |
356 | }, | |
216ee365 EB |
357 | { |
358 | .name = "x-dirty-bitmap", | |
359 | .type = QEMU_OPT_STRING, | |
360 | .help = "experimental: expose named dirty bitmap in place of " | |
361 | "block status", | |
362 | }, | |
c4365735 | 363 | { /* end of list */ } |
7ccc44fd HR |
364 | }, |
365 | }; | |
366 | ||
015a1036 HR |
367 | static int nbd_open(BlockDriverState *bs, QDict *options, int flags, |
368 | Error **errp) | |
33897dc7 NT |
369 | { |
370 | BDRVNBDState *s = bs->opaque; | |
7ccc44fd HR |
371 | QemuOpts *opts = NULL; |
372 | Error *local_err = NULL; | |
75822a12 DB |
373 | QCryptoTLSCreds *tlscreds = NULL; |
374 | const char *hostname = NULL; | |
375 | int ret = -EINVAL; | |
ae255e52 | 376 | |
7ccc44fd HR |
377 | opts = qemu_opts_create(&nbd_runtime_opts, NULL, 0, &error_abort); |
378 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
379 | if (local_err) { | |
380 | error_propagate(errp, local_err); | |
381 | goto error; | |
382 | } | |
383 | ||
62cf396b | 384 | /* Translate @host, @port, and @path to a SocketAddress */ |
491d6c7c HR |
385 | if (!nbd_process_legacy_socket_options(options, opts, errp)) { |
386 | goto error; | |
387 | } | |
388 | ||
33897dc7 | 389 | /* Pop the config into our state object. Exit if invalid. */ |
491d6c7c HR |
390 | s->saddr = nbd_config(s, options, errp); |
391 | if (!s->saddr) { | |
75822a12 DB |
392 | goto error; |
393 | } | |
394 | ||
491d6c7c HR |
395 | s->export = g_strdup(qemu_opt_get(opts, "export")); |
396 | ||
03504d05 HR |
397 | s->tlscredsid = g_strdup(qemu_opt_get(opts, "tls-creds")); |
398 | if (s->tlscredsid) { | |
399 | tlscreds = nbd_get_tls_creds(s->tlscredsid, errp); | |
75822a12 DB |
400 | if (!tlscreds) { |
401 | goto error; | |
402 | } | |
403 | ||
ca0b64e5 | 404 | /* TODO SOCKET_ADDRESS_KIND_FD where fd has AF_INET or AF_INET6 */ |
62cf396b | 405 | if (s->saddr->type != SOCKET_ADDRESS_TYPE_INET) { |
75822a12 DB |
406 | error_setg(errp, "TLS only supported over IP sockets"); |
407 | goto error; | |
408 | } | |
9445673e | 409 | hostname = s->saddr->u.inet.host; |
33897dc7 NT |
410 | } |
411 | ||
2302c1ca | 412 | /* NBD handshake */ |
d42f78e9 | 413 | ret = nbd_client_init(bs, s->saddr, s->export, tlscreds, hostname, |
216ee365 | 414 | qemu_opt_get(opts, "x-dirty-bitmap"), errp); |
d42f78e9 | 415 | |
75822a12 | 416 | error: |
75822a12 DB |
417 | if (tlscreds) { |
418 | object_unref(OBJECT(tlscreds)); | |
419 | } | |
03504d05 | 420 | if (ret < 0) { |
62cf396b | 421 | qapi_free_SocketAddress(s->saddr); |
03504d05 HR |
422 | g_free(s->export); |
423 | g_free(s->tlscredsid); | |
424 | } | |
7ccc44fd | 425 | qemu_opts_del(opts); |
75822a12 | 426 | return ret; |
e183ef75 PB |
427 | } |
428 | ||
1486d04a PB |
429 | static int nbd_co_flush(BlockDriverState *bs) |
430 | { | |
f53a829b | 431 | return nbd_client_co_flush(bs); |
1486d04a PB |
432 | } |
433 | ||
fa21e6fa DL |
434 | static void nbd_refresh_limits(BlockDriverState *bs, Error **errp) |
435 | { | |
081dd1fe | 436 | NBDClientSession *s = nbd_get_client_session(bs); |
fd8d372d | 437 | uint32_t min = s->info.min_block; |
081dd1fe EB |
438 | uint32_t max = MIN_NON_ZERO(NBD_MAX_BUFFER_SIZE, s->info.max_block); |
439 | ||
fd8d372d | 440 | bs->bl.request_alignment = min ? min : BDRV_SECTOR_SIZE; |
081dd1fe EB |
441 | bs->bl.max_pdiscard = max; |
442 | bs->bl.max_pwrite_zeroes = max; | |
443 | bs->bl.max_transfer = max; | |
444 | ||
445 | if (s->info.opt_block && | |
446 | s->info.opt_block > bs->bl.opt_transfer) { | |
447 | bs->bl.opt_transfer = s->info.opt_block; | |
448 | } | |
fa21e6fa DL |
449 | } |
450 | ||
75818250 TS |
451 | static void nbd_close(BlockDriverState *bs) |
452 | { | |
03504d05 HR |
453 | BDRVNBDState *s = bs->opaque; |
454 | ||
f53a829b | 455 | nbd_client_close(bs); |
03504d05 | 456 | |
62cf396b | 457 | qapi_free_SocketAddress(s->saddr); |
03504d05 HR |
458 | g_free(s->export); |
459 | g_free(s->tlscredsid); | |
75818250 TS |
460 | } |
461 | ||
462 | static int64_t nbd_getlength(BlockDriverState *bs) | |
463 | { | |
464 | BDRVNBDState *s = bs->opaque; | |
465 | ||
004a89fc | 466 | return s->client.info.size; |
75818250 TS |
467 | } |
468 | ||
69447cd8 SH |
469 | static void nbd_detach_aio_context(BlockDriverState *bs) |
470 | { | |
f53a829b | 471 | nbd_client_detach_aio_context(bs); |
69447cd8 SH |
472 | } |
473 | ||
474 | static void nbd_attach_aio_context(BlockDriverState *bs, | |
475 | AioContext *new_context) | |
476 | { | |
f53a829b | 477 | nbd_client_attach_aio_context(bs, new_context); |
69447cd8 SH |
478 | } |
479 | ||
4cdd01d3 | 480 | static void nbd_refresh_filename(BlockDriverState *bs, QDict *options) |
2019d68b | 481 | { |
03504d05 | 482 | BDRVNBDState *s = bs->opaque; |
2019d68b | 483 | QDict *opts = qdict_new(); |
491d6c7c HR |
484 | QObject *saddr_qdict; |
485 | Visitor *ov; | |
486 | const char *host = NULL, *port = NULL, *path = NULL; | |
487 | ||
62cf396b | 488 | if (s->saddr->type == SOCKET_ADDRESS_TYPE_INET) { |
9445673e | 489 | const InetSocketAddress *inet = &s->saddr->u.inet; |
491d6c7c HR |
490 | if (!inet->has_ipv4 && !inet->has_ipv6 && !inet->has_to) { |
491 | host = inet->host; | |
492 | port = inet->port; | |
493 | } | |
62cf396b | 494 | } else if (s->saddr->type == SOCKET_ADDRESS_TYPE_UNIX) { |
9445673e MA |
495 | path = s->saddr->u.q_unix.path; |
496 | } /* else can't represent as pseudo-filename */ | |
2019d68b | 497 | |
46f5ac20 | 498 | qdict_put_str(opts, "driver", "nbd"); |
2019d68b | 499 | |
491d6c7c | 500 | if (path && s->export) { |
2019d68b | 501 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
502 | "nbd+unix:///%s?socket=%s", s->export, path); |
503 | } else if (path && !s->export) { | |
2019d68b | 504 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
505 | "nbd+unix://?socket=%s", path); |
506 | } else if (host && s->export) { | |
ec0de768 | 507 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c HR |
508 | "nbd://%s:%s/%s", host, port, s->export); |
509 | } else if (host && !s->export) { | |
ec0de768 | 510 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), |
491d6c7c | 511 | "nbd://%s:%s", host, port); |
ec0de768 HR |
512 | } |
513 | ||
491d6c7c | 514 | ov = qobject_output_visitor_new(&saddr_qdict); |
62cf396b | 515 | visit_type_SocketAddress(ov, NULL, &s->saddr, &error_abort); |
491d6c7c | 516 | visit_complete(ov, &saddr_qdict); |
a1d4e38a | 517 | visit_free(ov); |
491d6c7c HR |
518 | qdict_put_obj(opts, "server", saddr_qdict); |
519 | ||
03504d05 | 520 | if (s->export) { |
46f5ac20 | 521 | qdict_put_str(opts, "export", s->export); |
2019d68b | 522 | } |
03504d05 | 523 | if (s->tlscredsid) { |
46f5ac20 | 524 | qdict_put_str(opts, "tls-creds", s->tlscredsid); |
75822a12 | 525 | } |
2019d68b | 526 | |
491d6c7c | 527 | qdict_flatten(opts); |
2019d68b HR |
528 | bs->full_open_options = opts; |
529 | } | |
530 | ||
8a6239c0 HR |
531 | static char *nbd_dirname(BlockDriverState *bs, Error **errp) |
532 | { | |
533 | /* The generic bdrv_dirname() implementation is able to work out some | |
534 | * directory name for NBD nodes, but that would be wrong. So far there is no | |
535 | * specification for how "export paths" would work, so NBD does not have | |
536 | * directory names. */ | |
537 | error_setg(errp, "Cannot generate a base directory for NBD nodes"); | |
538 | return NULL; | |
539 | } | |
540 | ||
5efa9d5a | 541 | static BlockDriver bdrv_nbd = { |
69447cd8 SH |
542 | .format_name = "nbd", |
543 | .protocol_name = "nbd", | |
544 | .instance_size = sizeof(BDRVNBDState), | |
545 | .bdrv_parse_filename = nbd_parse_filename, | |
546 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
547 | .bdrv_co_preadv = nbd_client_co_preadv, |
548 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 549 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
550 | .bdrv_close = nbd_close, |
551 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 552 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 553 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
554 | .bdrv_getlength = nbd_getlength, |
555 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
556 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 557 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 558 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 559 | .bdrv_dirname = nbd_dirname, |
1d7d2a9d PB |
560 | }; |
561 | ||
562 | static BlockDriver bdrv_nbd_tcp = { | |
69447cd8 SH |
563 | .format_name = "nbd", |
564 | .protocol_name = "nbd+tcp", | |
565 | .instance_size = sizeof(BDRVNBDState), | |
566 | .bdrv_parse_filename = nbd_parse_filename, | |
567 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
568 | .bdrv_co_preadv = nbd_client_co_preadv, |
569 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 570 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
571 | .bdrv_close = nbd_close, |
572 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 573 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 574 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
575 | .bdrv_getlength = nbd_getlength, |
576 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
577 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 578 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 579 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 580 | .bdrv_dirname = nbd_dirname, |
1d7d2a9d PB |
581 | }; |
582 | ||
583 | static BlockDriver bdrv_nbd_unix = { | |
69447cd8 SH |
584 | .format_name = "nbd", |
585 | .protocol_name = "nbd+unix", | |
586 | .instance_size = sizeof(BDRVNBDState), | |
587 | .bdrv_parse_filename = nbd_parse_filename, | |
588 | .bdrv_file_open = nbd_open, | |
70c4fb26 EB |
589 | .bdrv_co_preadv = nbd_client_co_preadv, |
590 | .bdrv_co_pwritev = nbd_client_co_pwritev, | |
fa778fff | 591 | .bdrv_co_pwrite_zeroes = nbd_client_co_pwrite_zeroes, |
69447cd8 SH |
592 | .bdrv_close = nbd_close, |
593 | .bdrv_co_flush_to_os = nbd_co_flush, | |
447e57c3 | 594 | .bdrv_co_pdiscard = nbd_client_co_pdiscard, |
fa21e6fa | 595 | .bdrv_refresh_limits = nbd_refresh_limits, |
69447cd8 SH |
596 | .bdrv_getlength = nbd_getlength, |
597 | .bdrv_detach_aio_context = nbd_detach_aio_context, | |
598 | .bdrv_attach_aio_context = nbd_attach_aio_context, | |
2019d68b | 599 | .bdrv_refresh_filename = nbd_refresh_filename, |
78a33ab5 | 600 | .bdrv_co_block_status = nbd_client_co_block_status, |
8a6239c0 | 601 | .bdrv_dirname = nbd_dirname, |
75818250 | 602 | }; |
5efa9d5a AL |
603 | |
604 | static void bdrv_nbd_init(void) | |
605 | { | |
606 | bdrv_register(&bdrv_nbd); | |
1d7d2a9d PB |
607 | bdrv_register(&bdrv_nbd_tcp); |
608 | bdrv_register(&bdrv_nbd_unix); | |
5efa9d5a AL |
609 | } |
610 | ||
611 | block_init(bdrv_nbd_init); |