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/qjson.h"
35 #include "qapi/qmp/qint.h"
37 #include <sys/types.h>
40 #define EN_OPTSTR ":exportname="
42 typedef struct BDRVNBDState
{
43 NbdClientSession client
;
44 QemuOpts
*socket_opts
;
47 static int nbd_parse_uri(const char *filename
, QDict
*options
)
51 QueryParams
*qp
= NULL
;
55 uri
= uri_parse(filename
);
61 if (!strcmp(uri
->scheme
, "nbd")) {
63 } else if (!strcmp(uri
->scheme
, "nbd+tcp")) {
65 } else if (!strcmp(uri
->scheme
, "nbd+unix")) {
72 p
= uri
->path
? uri
->path
: "/";
75 qdict_put(options
, "export", qstring_from_str(p
));
78 qp
= query_params_parse(uri
->query
);
79 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
85 /* nbd+unix:///export?socket=path */
86 if (uri
->server
|| uri
->port
|| strcmp(qp
->p
[0].name
, "socket")) {
90 qdict_put(options
, "path", qstring_from_str(qp
->p
[0].value
));
93 /* nbd[+tcp]://host[:port]/export */
99 /* strip braces from literal IPv6 address */
100 if (uri
->server
[0] == '[') {
101 host
= qstring_from_substr(uri
->server
, 1,
102 strlen(uri
->server
) - 2);
104 host
= qstring_from_str(uri
->server
);
107 qdict_put(options
, "host", host
);
109 char* port_str
= g_strdup_printf("%d", uri
->port
);
110 qdict_put(options
, "port", qstring_from_str(port_str
));
117 query_params_free(qp
);
123 static void nbd_parse_filename(const char *filename
, QDict
*options
,
128 const char *host_spec
;
129 const char *unixpath
;
131 if (qdict_haskey(options
, "host")
132 || qdict_haskey(options
, "port")
133 || qdict_haskey(options
, "path"))
135 error_setg(errp
, "host/port/path and a file name may not be specified "
140 if (strstr(filename
, "://")) {
141 int ret
= nbd_parse_uri(filename
, options
);
143 error_setg(errp
, "No valid URL specified");
148 file
= g_strdup(filename
);
150 export_name
= strstr(file
, EN_OPTSTR
);
152 if (export_name
[strlen(EN_OPTSTR
)] == 0) {
155 export_name
[0] = 0; /* truncate 'file' */
156 export_name
+= strlen(EN_OPTSTR
);
158 qdict_put(options
, "export", qstring_from_str(export_name
));
161 /* extract the host_spec - fail if it's not nbd:... */
162 if (!strstart(file
, "nbd:", &host_spec
)) {
163 error_setg(errp
, "File name string for NBD must start with 'nbd:'");
171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec
, "unix:", &unixpath
)) {
173 qdict_put(options
, "path", qstring_from_str(unixpath
));
175 InetSocketAddress
*addr
= NULL
;
177 addr
= inet_parse(host_spec
, errp
);
182 qdict_put(options
, "host", qstring_from_str(addr
->host
));
183 qdict_put(options
, "port", qstring_from_str(addr
->port
));
184 qapi_free_InetSocketAddress(addr
);
191 static void nbd_config(BDRVNBDState
*s
, QDict
*options
, char **export
,
194 Error
*local_err
= NULL
;
196 if (qdict_haskey(options
, "path") == qdict_haskey(options
, "host")) {
197 if (qdict_haskey(options
, "path")) {
198 error_setg(errp
, "path and host may not be used at the same time.");
200 error_setg(errp
, "one of path and host must be specified.");
205 s
->client
.is_unix
= qdict_haskey(options
, "path");
206 s
->socket_opts
= qemu_opts_create(&socket_optslist
, NULL
, 0,
209 qemu_opts_absorb_qdict(s
->socket_opts
, options
, &local_err
);
211 error_propagate(errp
, local_err
);
215 if (!qemu_opt_get(s
->socket_opts
, "port")) {
216 qemu_opt_set_number(s
->socket_opts
, "port", NBD_DEFAULT_PORT
);
219 *export
= g_strdup(qdict_get_try_str(options
, "export"));
221 qdict_del(options
, "export");
225 static int nbd_establish_connection(BlockDriverState
*bs
, Error
**errp
)
227 BDRVNBDState
*s
= bs
->opaque
;
230 if (s
->client
.is_unix
) {
231 sock
= unix_connect_opts(s
->socket_opts
, errp
, NULL
, NULL
);
233 sock
= inet_connect_opts(s
->socket_opts
, errp
, NULL
, NULL
);
235 socket_set_nodelay(sock
);
239 /* Failed to establish connection */
241 logout("Failed to establish connection to NBD server\n");
248 static int nbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
251 BDRVNBDState
*s
= bs
->opaque
;
254 Error
*local_err
= NULL
;
256 /* Pop the config into our state object. Exit if invalid. */
257 nbd_config(s
, options
, &export
, &local_err
);
259 error_propagate(errp
, local_err
);
263 /* establish TCP connection, return error if it fails
264 * TODO: Configurable retry-until-timeout behaviour.
266 sock
= nbd_establish_connection(bs
, errp
);
272 result
= nbd_client_session_init(&s
->client
, bs
, sock
, export
);
277 static int nbd_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
278 int nb_sectors
, QEMUIOVector
*qiov
)
280 BDRVNBDState
*s
= bs
->opaque
;
282 return nbd_client_session_co_readv(&s
->client
, sector_num
,
286 static int nbd_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
287 int nb_sectors
, QEMUIOVector
*qiov
)
289 BDRVNBDState
*s
= bs
->opaque
;
291 return nbd_client_session_co_writev(&s
->client
, sector_num
,
295 static int nbd_co_flush(BlockDriverState
*bs
)
297 BDRVNBDState
*s
= bs
->opaque
;
299 return nbd_client_session_co_flush(&s
->client
);
302 static int nbd_co_discard(BlockDriverState
*bs
, int64_t sector_num
,
305 BDRVNBDState
*s
= bs
->opaque
;
307 return nbd_client_session_co_discard(&s
->client
, sector_num
,
311 static void nbd_close(BlockDriverState
*bs
)
313 BDRVNBDState
*s
= bs
->opaque
;
315 qemu_opts_del(s
->socket_opts
);
316 nbd_client_session_close(&s
->client
);
319 static int64_t nbd_getlength(BlockDriverState
*bs
)
321 BDRVNBDState
*s
= bs
->opaque
;
323 return s
->client
.size
;
326 static BlockDriver bdrv_nbd
= {
327 .format_name
= "nbd",
328 .protocol_name
= "nbd",
329 .instance_size
= sizeof(BDRVNBDState
),
330 .bdrv_parse_filename
= nbd_parse_filename
,
331 .bdrv_file_open
= nbd_open
,
332 .bdrv_co_readv
= nbd_co_readv
,
333 .bdrv_co_writev
= nbd_co_writev
,
334 .bdrv_close
= nbd_close
,
335 .bdrv_co_flush_to_os
= nbd_co_flush
,
336 .bdrv_co_discard
= nbd_co_discard
,
337 .bdrv_getlength
= nbd_getlength
,
340 static BlockDriver bdrv_nbd_tcp
= {
341 .format_name
= "nbd",
342 .protocol_name
= "nbd+tcp",
343 .instance_size
= sizeof(BDRVNBDState
),
344 .bdrv_parse_filename
= nbd_parse_filename
,
345 .bdrv_file_open
= nbd_open
,
346 .bdrv_co_readv
= nbd_co_readv
,
347 .bdrv_co_writev
= nbd_co_writev
,
348 .bdrv_close
= nbd_close
,
349 .bdrv_co_flush_to_os
= nbd_co_flush
,
350 .bdrv_co_discard
= nbd_co_discard
,
351 .bdrv_getlength
= nbd_getlength
,
354 static BlockDriver bdrv_nbd_unix
= {
355 .format_name
= "nbd",
356 .protocol_name
= "nbd+unix",
357 .instance_size
= sizeof(BDRVNBDState
),
358 .bdrv_parse_filename
= nbd_parse_filename
,
359 .bdrv_file_open
= nbd_open
,
360 .bdrv_co_readv
= nbd_co_readv
,
361 .bdrv_co_writev
= nbd_co_writev
,
362 .bdrv_close
= nbd_close
,
363 .bdrv_co_flush_to_os
= nbd_co_flush
,
364 .bdrv_co_discard
= nbd_co_discard
,
365 .bdrv_getlength
= nbd_getlength
,
368 static void bdrv_nbd_init(void)
370 bdrv_register(&bdrv_nbd
);
371 bdrv_register(&bdrv_nbd_tcp
);
372 bdrv_register(&bdrv_nbd_unix
);
375 block_init(bdrv_nbd_init
);