target-arm: aarch64: add support for ld lit
[qemu/cris-port.git] / block / nbd.c
blob4455a134dbb4677ff87a1e52f02070a8fda6c663
1 /*
2 * QEMU Block driver for NBD
4 * Copyright (C) 2008 Bull S.A.S.
5 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 * Some parts:
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
26 * THE SOFTWARE.
29 #include "block/nbd-client.h"
30 #include "qemu/uri.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>
38 #include <unistd.h>
40 #define EN_OPTSTR ":exportname="
42 typedef struct BDRVNBDState {
43 NbdClientSession client;
44 QemuOpts *socket_opts;
45 } BDRVNBDState;
47 static int nbd_parse_uri(const char *filename, QDict *options)
49 URI *uri;
50 const char *p;
51 QueryParams *qp = NULL;
52 int ret = 0;
53 bool is_unix;
55 uri = uri_parse(filename);
56 if (!uri) {
57 return -EINVAL;
60 /* transport */
61 if (!strcmp(uri->scheme, "nbd")) {
62 is_unix = false;
63 } else if (!strcmp(uri->scheme, "nbd+tcp")) {
64 is_unix = false;
65 } else if (!strcmp(uri->scheme, "nbd+unix")) {
66 is_unix = true;
67 } else {
68 ret = -EINVAL;
69 goto out;
72 p = uri->path ? uri->path : "/";
73 p += strspn(p, "/");
74 if (p[0]) {
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)) {
80 ret = -EINVAL;
81 goto out;
84 if (is_unix) {
85 /* nbd+unix:///export?socket=path */
86 if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
87 ret = -EINVAL;
88 goto out;
90 qdict_put(options, "path", qstring_from_str(qp->p[0].value));
91 } else {
92 QString *host;
93 /* nbd[+tcp]://host[:port]/export */
94 if (!uri->server) {
95 ret = -EINVAL;
96 goto out;
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);
103 } else {
104 host = qstring_from_str(uri->server);
107 qdict_put(options, "host", host);
108 if (uri->port) {
109 char* port_str = g_strdup_printf("%d", uri->port);
110 qdict_put(options, "port", qstring_from_str(port_str));
111 g_free(port_str);
115 out:
116 if (qp) {
117 query_params_free(qp);
119 uri_free(uri);
120 return ret;
123 static void nbd_parse_filename(const char *filename, QDict *options,
124 Error **errp)
126 char *file;
127 char *export_name;
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 "
136 "at the same time");
137 return;
140 if (strstr(filename, "://")) {
141 int ret = nbd_parse_uri(filename, options);
142 if (ret < 0) {
143 error_setg(errp, "No valid URL specified");
145 return;
148 file = g_strdup(filename);
150 export_name = strstr(file, EN_OPTSTR);
151 if (export_name) {
152 if (export_name[strlen(EN_OPTSTR)] == 0) {
153 goto out;
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:'");
164 goto out;
167 if (!*host_spec) {
168 goto out;
171 /* are we a UNIX or TCP socket? */
172 if (strstart(host_spec, "unix:", &unixpath)) {
173 qdict_put(options, "path", qstring_from_str(unixpath));
174 } else {
175 InetSocketAddress *addr = NULL;
177 addr = inet_parse(host_spec, errp);
178 if (error_is_set(errp)) {
179 goto out;
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);
187 out:
188 g_free(file);
191 static int nbd_config(BDRVNBDState *s, QDict *options, char **export)
193 Error *local_err = NULL;
195 if (qdict_haskey(options, "path")) {
196 if (qdict_haskey(options, "host")) {
197 qerror_report(ERROR_CLASS_GENERIC_ERROR, "path and host may not "
198 "be used at the same time.");
199 return -EINVAL;
201 s->client.is_unix = true;
202 } else if (qdict_haskey(options, "host")) {
203 s->client.is_unix = false;
204 } else {
205 return -EINVAL;
208 s->socket_opts = qemu_opts_create_nofail(&socket_optslist);
210 qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
211 if (error_is_set(&local_err)) {
212 qerror_report_err(local_err);
213 error_free(local_err);
214 return -EINVAL;
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"));
222 if (*export) {
223 qdict_del(options, "export");
226 return 0;
229 static int nbd_establish_connection(BlockDriverState *bs)
231 BDRVNBDState *s = bs->opaque;
232 int sock;
234 if (s->client.is_unix) {
235 sock = unix_socket_outgoing(qemu_opt_get(s->socket_opts, "path"));
236 } else {
237 sock = tcp_socket_outgoing_opts(s->socket_opts);
238 if (sock >= 0) {
239 socket_set_nodelay(sock);
243 /* Failed to establish connection */
244 if (sock < 0) {
245 logout("Failed to establish connection to NBD server\n");
246 return -errno;
249 return sock;
252 static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
253 Error **errp)
255 BDRVNBDState *s = bs->opaque;
256 char *export = NULL;
257 int result, sock;
259 /* Pop the config into our state object. Exit if invalid. */
260 result = nbd_config(s, options, &export);
261 if (result != 0) {
262 return result;
265 /* establish TCP connection, return error if it fails
266 * TODO: Configurable retry-until-timeout behaviour.
268 sock = nbd_establish_connection(bs);
269 if (sock < 0) {
270 return sock;
273 /* NBD handshake */
274 result = nbd_client_session_init(&s->client, bs, sock, export);
275 g_free(export);
276 return result;
279 static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
280 int nb_sectors, QEMUIOVector *qiov)
282 BDRVNBDState *s = bs->opaque;
284 return nbd_client_session_co_readv(&s->client, sector_num,
285 nb_sectors, qiov);
288 static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
289 int nb_sectors, QEMUIOVector *qiov)
291 BDRVNBDState *s = bs->opaque;
293 return nbd_client_session_co_writev(&s->client, sector_num,
294 nb_sectors, qiov);
297 static int nbd_co_flush(BlockDriverState *bs)
299 BDRVNBDState *s = bs->opaque;
301 return nbd_client_session_co_flush(&s->client);
304 static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
305 int nb_sectors)
307 BDRVNBDState *s = bs->opaque;
309 return nbd_client_session_co_discard(&s->client, sector_num,
310 nb_sectors);
313 static void nbd_close(BlockDriverState *bs)
315 BDRVNBDState *s = bs->opaque;
317 qemu_opts_del(s->socket_opts);
318 nbd_client_session_close(&s->client);
321 static int64_t nbd_getlength(BlockDriverState *bs)
323 BDRVNBDState *s = bs->opaque;
325 return s->client.size;
328 static BlockDriver bdrv_nbd = {
329 .format_name = "nbd",
330 .protocol_name = "nbd",
331 .instance_size = sizeof(BDRVNBDState),
332 .bdrv_parse_filename = nbd_parse_filename,
333 .bdrv_file_open = nbd_open,
334 .bdrv_co_readv = nbd_co_readv,
335 .bdrv_co_writev = nbd_co_writev,
336 .bdrv_close = nbd_close,
337 .bdrv_co_flush_to_os = nbd_co_flush,
338 .bdrv_co_discard = nbd_co_discard,
339 .bdrv_getlength = nbd_getlength,
342 static BlockDriver bdrv_nbd_tcp = {
343 .format_name = "nbd",
344 .protocol_name = "nbd+tcp",
345 .instance_size = sizeof(BDRVNBDState),
346 .bdrv_parse_filename = nbd_parse_filename,
347 .bdrv_file_open = nbd_open,
348 .bdrv_co_readv = nbd_co_readv,
349 .bdrv_co_writev = nbd_co_writev,
350 .bdrv_close = nbd_close,
351 .bdrv_co_flush_to_os = nbd_co_flush,
352 .bdrv_co_discard = nbd_co_discard,
353 .bdrv_getlength = nbd_getlength,
356 static BlockDriver bdrv_nbd_unix = {
357 .format_name = "nbd",
358 .protocol_name = "nbd+unix",
359 .instance_size = sizeof(BDRVNBDState),
360 .bdrv_parse_filename = nbd_parse_filename,
361 .bdrv_file_open = nbd_open,
362 .bdrv_co_readv = nbd_co_readv,
363 .bdrv_co_writev = nbd_co_writev,
364 .bdrv_close = nbd_close,
365 .bdrv_co_flush_to_os = nbd_co_flush,
366 .bdrv_co_discard = nbd_co_discard,
367 .bdrv_getlength = nbd_getlength,
370 static void bdrv_nbd_init(void)
372 bdrv_register(&bdrv_nbd);
373 bdrv_register(&bdrv_nbd_tcp);
374 bdrv_register(&bdrv_nbd_unix);
377 block_init(bdrv_nbd_init);