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