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