2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
8 * Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
29 #include "block/nbd-client.h"
31 #include "block/block_int.h"
32 #include "qemu/module.h"
33 #include "qemu/sockets.h"
34 #include "qapi/qmp/qdict.h"
35 #include "qapi/qmp/qjson.h"
36 #include "qapi/qmp/qint.h"
37 #include "qapi/qmp/qstring.h"
39 #include <sys/types.h>
42 #define EN_OPTSTR ":exportname="
44 typedef struct BDRVNBDState
{
45 NbdClientSession client
;
46 QemuOpts
*socket_opts
;
49 static int nbd_parse_uri(const char *filename
, QDict
*options
)
53 QueryParams
*qp
= NULL
;
57 uri
= uri_parse(filename
);
63 if (!strcmp(uri
->scheme
, "nbd")) {
65 } else if (!strcmp(uri
->scheme
, "nbd+tcp")) {
67 } else if (!strcmp(uri
->scheme
, "nbd+unix")) {
74 p
= uri
->path
? uri
->path
: "/";
77 qdict_put(options
, "export", qstring_from_str(p
));
80 qp
= query_params_parse(uri
->query
);
81 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
87 /* nbd+unix:///export?socket=path */
88 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
92 qdict_put(options
, "path", qstring_from_str(qp
->p
[0].value
));
95 /* nbd[+tcp]://host[:port]/export */
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);
106 host
= qstring_from_str(uri
->server
);
109 qdict_put(options
, "host", host
);
111 char* port_str
= g_strdup_printf("%d", uri
->port
);
112 qdict_put(options
, "port", qstring_from_str(port_str
));
119 query_params_free(qp
);
125 static void nbd_parse_filename(const char *filename
, QDict
*options
,
130 const char *host_spec
;
131 const char *unixpath
;
133 if (qdict_haskey(options
, "host")
134 || qdict_haskey(options
, "port")
135 || qdict_haskey(options
, "path"))
137 error_setg(errp
, "host/port/path and a file name may not be specified "
142 if (strstr(filename
, "://")) {
143 int ret
= nbd_parse_uri(filename
, options
);
145 error_setg(errp
, "No valid URL specified");
150 file
= g_strdup(filename
);
152 export_name
= strstr(file
, EN_OPTSTR
);
154 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
157 export_name
[0] = 0; /* truncate 'file' */
158 export_name
+= strlen(EN_OPTSTR
);
160 qdict_put(options
, "export", qstring_from_str(export_name
));
163 /* extract the host_spec - fail if it's not nbd:... */
164 if (!strstart(file
, "nbd:", &host_spec
)) {
165 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
173 /* are we a UNIX or TCP socket? */
174 if (strstart(host_spec
, "unix:", &unixpath
)) {
175 qdict_put(options
, "path", qstring_from_str(unixpath
));
177 InetSocketAddress
*addr
= NULL
;
179 addr
= inet_parse(host_spec
, errp
);
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
);
193 static void nbd_config(BDRVNBDState
*s
, QDict
*options
, char **export
,
196 Error
*local_err
= NULL
;
198 if (qdict_haskey(options
, "path") == qdict_haskey(options
, "host")) {
199 if (qdict_haskey(options
, "path")) {
200 error_setg(errp
, "path and host may not be used at the same time.");
202 error_setg(errp
, "one of path and host must be specified.");
207 s
->client
.is_unix
= qdict_haskey(options
, "path");
208 s
->socket_opts
= qemu_opts_create(&socket_optslist
, NULL
, 0,
211 qemu_opts_absorb_qdict(s
->socket_opts
, options
, &local_err
);
213 error_propagate(errp
, local_err
);
217 if (!qemu_opt_get(s
->socket_opts
, "port")) {
218 qemu_opt_set_number(s
->socket_opts
, "port", NBD_DEFAULT_PORT
);
221 *export
= g_strdup(qdict_get_try_str(options
, "export"));
223 qdict_del(options
, "export");
227 NbdClientSession
*nbd_get_client_session(BlockDriverState
*bs
)
229 BDRVNBDState
*s
= bs
->opaque
;
233 static int nbd_establish_connection(BlockDriverState
*bs
, Error
**errp
)
235 BDRVNBDState
*s
= bs
->opaque
;
238 if (s
->client
.is_unix
) {
239 sock
= unix_connect_opts(s
->socket_opts
, errp
, NULL
, NULL
);
241 sock
= inet_connect_opts(s
->socket_opts
, errp
, NULL
, NULL
);
243 socket_set_nodelay(sock
);
247 /* Failed to establish connection */
249 logout("Failed to establish connection to NBD server\n");
256 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
259 BDRVNBDState
*s
= bs
->opaque
;
262 Error
*local_err
= NULL
;
264 /* Pop the config into our state object. Exit if invalid. */
265 nbd_config(s
, options
, &export
, &local_err
);
267 error_propagate(errp
, local_err
);
271 /* establish TCP connection, return error if it fails
272 * TODO: Configurable retry-until-timeout behaviour.
274 sock
= nbd_establish_connection(bs
, errp
);
280 result
= nbd_client_init(bs
, sock
, export
, errp
);
285 static int nbd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
286 int nb_sectors
, QEMUIOVector
*qiov
)
288 return nbd_client_co_readv(bs
, sector_num
, nb_sectors
, qiov
);
291 static int nbd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
292 int nb_sectors
, QEMUIOVector
*qiov
)
294 return nbd_client_co_writev(bs
, sector_num
, nb_sectors
, qiov
);
297 static int nbd_co_flush(BlockDriverState
*bs
)
299 return nbd_client_co_flush(bs
);
302 static void nbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
304 bs
->bl
.max_discard
= UINT32_MAX
>> BDRV_SECTOR_BITS
;
305 bs
->bl
.max_transfer_length
= UINT32_MAX
>> BDRV_SECTOR_BITS
;
308 static int nbd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
311 return nbd_client_co_discard(bs
, sector_num
, nb_sectors
);
314 static void nbd_close(BlockDriverState
*bs
)
316 BDRVNBDState
*s
= bs
->opaque
;
318 qemu_opts_del(s
->socket_opts
);
319 nbd_client_close(bs
);
322 static int64_t nbd_getlength(BlockDriverState
*bs
)
324 BDRVNBDState
*s
= bs
->opaque
;
326 return s
->client
.size
;
329 static void nbd_detach_aio_context(BlockDriverState
*bs
)
331 nbd_client_detach_aio_context(bs
);
334 static void nbd_attach_aio_context(BlockDriverState
*bs
,
335 AioContext
*new_context
)
337 nbd_client_attach_aio_context(bs
, new_context
);
340 static void nbd_refresh_filename(BlockDriverState
*bs
)
342 QDict
*opts
= qdict_new();
343 const char *path
= qdict_get_try_str(bs
->options
, "path");
344 const char *host
= qdict_get_try_str(bs
->options
, "host");
345 const char *port
= qdict_get_try_str(bs
->options
, "port");
346 const char *export
= qdict_get_try_str(bs
->options
, "export");
348 qdict_put_obj(opts
, "driver", QOBJECT(qstring_from_str("nbd")));
350 if (path
&& export
) {
351 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
352 "nbd+unix:///%s?socket=%s", export
, path
);
353 } else if (path
&& !export
) {
354 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
355 "nbd+unix://?socket=%s", path
);
356 } else if (!path
&& export
&& port
) {
357 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
358 "nbd://%s:%s/%s", host
, port
, export
);
359 } else if (!path
&& export
&& !port
) {
360 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
361 "nbd://%s/%s", host
, export
);
362 } else if (!path
&& !export
&& port
) {
363 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
364 "nbd://%s:%s", host
, port
);
365 } else if (!path
&& !export
&& !port
) {
366 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
),
371 qdict_put_obj(opts
, "path", QOBJECT(qstring_from_str(path
)));
373 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
374 qdict_put_obj(opts
, "port", QOBJECT(qstring_from_str(port
)));
376 qdict_put_obj(opts
, "host", QOBJECT(qstring_from_str(host
)));
379 qdict_put_obj(opts
, "export", QOBJECT(qstring_from_str(export
)));
382 bs
->full_open_options
= opts
;
385 static BlockDriver bdrv_nbd
= {
386 .format_name
= "nbd",
387 .protocol_name
= "nbd",
388 .instance_size
= sizeof(BDRVNBDState
),
389 .bdrv_parse_filename
= nbd_parse_filename
,
390 .bdrv_file_open
= nbd_open
,
391 .bdrv_co_readv
= nbd_co_readv
,
392 .bdrv_co_writev
= nbd_co_writev
,
393 .bdrv_close
= nbd_close
,
394 .bdrv_co_flush_to_os
= nbd_co_flush
,
395 .bdrv_co_discard
= nbd_co_discard
,
396 .bdrv_refresh_limits
= nbd_refresh_limits
,
397 .bdrv_getlength
= nbd_getlength
,
398 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
399 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
400 .bdrv_refresh_filename
= nbd_refresh_filename
,
403 static BlockDriver bdrv_nbd_tcp
= {
404 .format_name
= "nbd",
405 .protocol_name
= "nbd+tcp",
406 .instance_size
= sizeof(BDRVNBDState
),
407 .bdrv_parse_filename
= nbd_parse_filename
,
408 .bdrv_file_open
= nbd_open
,
409 .bdrv_co_readv
= nbd_co_readv
,
410 .bdrv_co_writev
= nbd_co_writev
,
411 .bdrv_close
= nbd_close
,
412 .bdrv_co_flush_to_os
= nbd_co_flush
,
413 .bdrv_co_discard
= nbd_co_discard
,
414 .bdrv_refresh_limits
= nbd_refresh_limits
,
415 .bdrv_getlength
= nbd_getlength
,
416 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
417 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
418 .bdrv_refresh_filename
= nbd_refresh_filename
,
421 static BlockDriver bdrv_nbd_unix
= {
422 .format_name
= "nbd",
423 .protocol_name
= "nbd+unix",
424 .instance_size
= sizeof(BDRVNBDState
),
425 .bdrv_parse_filename
= nbd_parse_filename
,
426 .bdrv_file_open
= nbd_open
,
427 .bdrv_co_readv
= nbd_co_readv
,
428 .bdrv_co_writev
= nbd_co_writev
,
429 .bdrv_close
= nbd_close
,
430 .bdrv_co_flush_to_os
= nbd_co_flush
,
431 .bdrv_co_discard
= nbd_co_discard
,
432 .bdrv_refresh_limits
= nbd_refresh_limits
,
433 .bdrv_getlength
= nbd_getlength
,
434 .bdrv_detach_aio_context
= nbd_detach_aio_context
,
435 .bdrv_attach_aio_context
= nbd_attach_aio_context
,
436 .bdrv_refresh_filename
= nbd_refresh_filename
,
439 static void bdrv_nbd_init(void)
441 bdrv_register(&bdrv_nbd
);
442 bdrv_register(&bdrv_nbd_tcp
);
443 bdrv_register(&bdrv_nbd_unix
);
446 block_init(bdrv_nbd_init
);