]>
Commit | Line | Data |
---|---|---|
8d6d89cb BR |
1 | /* |
2 | * GlusterFS backend for QEMU | |
3 | * | |
4 | * Copyright (C) 2012 Bharata B Rao <[email protected]> | |
5 | * | |
85c09bc0 BR |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
7 | * See the COPYING file in the top-level directory. | |
8d6d89cb | 8 | * |
8d6d89cb | 9 | */ |
80c71a24 | 10 | #include "qemu/osdep.h" |
8d6d89cb | 11 | #include <glusterfs/api/glfs.h> |
737e150e | 12 | #include "block/block_int.h" |
da34e65c | 13 | #include "qapi/error.h" |
6c7189bb | 14 | #include "qapi/qmp/qerror.h" |
1de7afc9 | 15 | #include "qemu/uri.h" |
0552ff24 | 16 | #include "qemu/error-report.h" |
8d6d89cb | 17 | |
f70c50c8 | 18 | #define GLUSTER_OPT_FILENAME "filename" |
6c7189bb PKK |
19 | #define GLUSTER_OPT_VOLUME "volume" |
20 | #define GLUSTER_OPT_PATH "path" | |
21 | #define GLUSTER_OPT_TYPE "type" | |
22 | #define GLUSTER_OPT_SERVER_PATTERN "server." | |
23 | #define GLUSTER_OPT_HOST "host" | |
24 | #define GLUSTER_OPT_PORT "port" | |
25 | #define GLUSTER_OPT_TO "to" | |
26 | #define GLUSTER_OPT_IPV4 "ipv4" | |
27 | #define GLUSTER_OPT_IPV6 "ipv6" | |
28 | #define GLUSTER_OPT_SOCKET "socket" | |
f70c50c8 | 29 | #define GLUSTER_OPT_DEBUG "debug" |
7edac2dd | 30 | #define GLUSTER_DEFAULT_PORT 24007 |
f70c50c8 PKK |
31 | #define GLUSTER_DEBUG_DEFAULT 4 |
32 | #define GLUSTER_DEBUG_MAX 9 | |
e9db8ff3 PKK |
33 | #define GLUSTER_OPT_LOGFILE "logfile" |
34 | #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */ | |
f70c50c8 | 35 | |
6c7189bb | 36 | #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n" |
f70c50c8 | 37 | |
8d6d89cb | 38 | typedef struct GlusterAIOCB { |
8d6d89cb BR |
39 | int64_t size; |
40 | int ret; | |
15744b0b | 41 | Coroutine *coroutine; |
6ee50af2 | 42 | AioContext *aio_context; |
8d6d89cb BR |
43 | } GlusterAIOCB; |
44 | ||
45 | typedef struct BDRVGlusterState { | |
46 | struct glfs *glfs; | |
8d6d89cb | 47 | struct glfs_fd *fd; |
e9db8ff3 | 48 | char *logfile; |
947eb203 | 49 | bool supports_seek_data; |
7eac868a | 50 | int debug_level; |
8d6d89cb BR |
51 | } BDRVGlusterState; |
52 | ||
f70c50c8 PKK |
53 | typedef struct BDRVGlusterReopenState { |
54 | struct glfs *glfs; | |
55 | struct glfs_fd *fd; | |
56 | } BDRVGlusterReopenState; | |
57 | ||
f70c50c8 PKK |
58 | |
59 | static QemuOptsList qemu_gluster_create_opts = { | |
60 | .name = "qemu-gluster-create-opts", | |
61 | .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head), | |
62 | .desc = { | |
63 | { | |
64 | .name = BLOCK_OPT_SIZE, | |
65 | .type = QEMU_OPT_SIZE, | |
66 | .help = "Virtual disk size" | |
67 | }, | |
68 | { | |
69 | .name = BLOCK_OPT_PREALLOC, | |
70 | .type = QEMU_OPT_STRING, | |
71 | .help = "Preallocation mode (allowed values: off, full)" | |
72 | }, | |
73 | { | |
74 | .name = GLUSTER_OPT_DEBUG, | |
75 | .type = QEMU_OPT_NUMBER, | |
76 | .help = "Gluster log level, valid range is 0-9", | |
77 | }, | |
e9db8ff3 PKK |
78 | { |
79 | .name = GLUSTER_OPT_LOGFILE, | |
80 | .type = QEMU_OPT_STRING, | |
81 | .help = "Logfile path of libgfapi", | |
82 | }, | |
f70c50c8 PKK |
83 | { /* end of list */ } |
84 | } | |
85 | }; | |
86 | ||
87 | static QemuOptsList runtime_opts = { | |
88 | .name = "gluster", | |
89 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
90 | .desc = { | |
91 | { | |
92 | .name = GLUSTER_OPT_FILENAME, | |
93 | .type = QEMU_OPT_STRING, | |
94 | .help = "URL to the gluster image", | |
95 | }, | |
96 | { | |
97 | .name = GLUSTER_OPT_DEBUG, | |
98 | .type = QEMU_OPT_NUMBER, | |
99 | .help = "Gluster log level, valid range is 0-9", | |
100 | }, | |
e9db8ff3 PKK |
101 | { |
102 | .name = GLUSTER_OPT_LOGFILE, | |
103 | .type = QEMU_OPT_STRING, | |
104 | .help = "Logfile path of libgfapi", | |
105 | }, | |
f70c50c8 PKK |
106 | { /* end of list */ } |
107 | }, | |
108 | }; | |
109 | ||
6c7189bb PKK |
110 | static QemuOptsList runtime_json_opts = { |
111 | .name = "gluster_json", | |
112 | .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head), | |
113 | .desc = { | |
114 | { | |
115 | .name = GLUSTER_OPT_VOLUME, | |
116 | .type = QEMU_OPT_STRING, | |
117 | .help = "name of gluster volume where VM image resides", | |
118 | }, | |
119 | { | |
120 | .name = GLUSTER_OPT_PATH, | |
121 | .type = QEMU_OPT_STRING, | |
122 | .help = "absolute path to image file in gluster volume", | |
123 | }, | |
124 | { | |
125 | .name = GLUSTER_OPT_DEBUG, | |
126 | .type = QEMU_OPT_NUMBER, | |
127 | .help = "Gluster log level, valid range is 0-9", | |
128 | }, | |
129 | { /* end of list */ } | |
130 | }, | |
131 | }; | |
132 | ||
133 | static QemuOptsList runtime_type_opts = { | |
134 | .name = "gluster_type", | |
135 | .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head), | |
136 | .desc = { | |
137 | { | |
138 | .name = GLUSTER_OPT_TYPE, | |
139 | .type = QEMU_OPT_STRING, | |
140 | .help = "tcp|unix", | |
141 | }, | |
142 | { /* end of list */ } | |
143 | }, | |
144 | }; | |
145 | ||
146 | static QemuOptsList runtime_unix_opts = { | |
147 | .name = "gluster_unix", | |
148 | .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head), | |
149 | .desc = { | |
150 | { | |
151 | .name = GLUSTER_OPT_SOCKET, | |
152 | .type = QEMU_OPT_STRING, | |
153 | .help = "socket file path)", | |
154 | }, | |
155 | { /* end of list */ } | |
156 | }, | |
157 | }; | |
158 | ||
159 | static QemuOptsList runtime_tcp_opts = { | |
160 | .name = "gluster_tcp", | |
161 | .head = QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts.head), | |
162 | .desc = { | |
163 | { | |
164 | .name = GLUSTER_OPT_TYPE, | |
165 | .type = QEMU_OPT_STRING, | |
166 | .help = "tcp|unix", | |
167 | }, | |
168 | { | |
169 | .name = GLUSTER_OPT_HOST, | |
170 | .type = QEMU_OPT_STRING, | |
171 | .help = "host address (hostname/ipv4/ipv6 addresses)", | |
172 | }, | |
173 | { | |
174 | .name = GLUSTER_OPT_PORT, | |
175 | .type = QEMU_OPT_NUMBER, | |
176 | .help = "port number on which glusterd is listening (default 24007)", | |
177 | }, | |
178 | { | |
179 | .name = "to", | |
180 | .type = QEMU_OPT_NUMBER, | |
181 | .help = "max port number, not supported by gluster", | |
182 | }, | |
183 | { | |
184 | .name = "ipv4", | |
185 | .type = QEMU_OPT_BOOL, | |
186 | .help = "ipv4 bool value, not supported by gluster", | |
187 | }, | |
188 | { | |
189 | .name = "ipv6", | |
190 | .type = QEMU_OPT_BOOL, | |
191 | .help = "ipv6 bool value, not supported by gluster", | |
192 | }, | |
193 | { /* end of list */ } | |
194 | }, | |
195 | }; | |
f70c50c8 | 196 | |
7edac2dd | 197 | static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path) |
8d6d89cb BR |
198 | { |
199 | char *p, *q; | |
200 | ||
201 | if (!path) { | |
202 | return -EINVAL; | |
203 | } | |
204 | ||
205 | /* volume */ | |
206 | p = q = path + strspn(path, "/"); | |
207 | p += strcspn(p, "/"); | |
208 | if (*p == '\0') { | |
209 | return -EINVAL; | |
210 | } | |
d5cf4079 | 211 | gconf->volume = g_strndup(q, p - q); |
8d6d89cb | 212 | |
d5cf4079 | 213 | /* path */ |
8d6d89cb BR |
214 | p += strspn(p, "/"); |
215 | if (*p == '\0') { | |
216 | return -EINVAL; | |
217 | } | |
d5cf4079 | 218 | gconf->path = g_strdup(p); |
8d6d89cb BR |
219 | return 0; |
220 | } | |
221 | ||
222 | /* | |
d5cf4079 | 223 | * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...] |
8d6d89cb BR |
224 | * |
225 | * 'gluster' is the protocol. | |
226 | * | |
227 | * 'transport' specifies the transport type used to connect to gluster | |
228 | * management daemon (glusterd). Valid transport types are | |
0552ff24 | 229 | * tcp or unix. If a transport type isn't specified, then tcp type is assumed. |
8d6d89cb | 230 | * |
d5cf4079 | 231 | * 'host' specifies the host where the volume file specification for |
0552ff24 | 232 | * the given volume resides. This can be either hostname or ipv4 address. |
d5cf4079 | 233 | * If transport type is 'unix', then 'host' field should not be specified. |
8d6d89cb BR |
234 | * The 'socket' field needs to be populated with the path to unix domain |
235 | * socket. | |
236 | * | |
237 | * 'port' is the port number on which glusterd is listening. This is optional | |
238 | * and if not specified, QEMU will send 0 which will make gluster to use the | |
239 | * default port. If the transport type is unix, then 'port' should not be | |
240 | * specified. | |
241 | * | |
d5cf4079 | 242 | * 'volume' is the name of the gluster volume which contains the VM image. |
8d6d89cb | 243 | * |
d5cf4079 | 244 | * 'path' is the path to the actual VM image that resides on gluster volume. |
8d6d89cb BR |
245 | * |
246 | * Examples: | |
247 | * | |
248 | * file=gluster://1.2.3.4/testvol/a.img | |
249 | * file=gluster+tcp://1.2.3.4/testvol/a.img | |
250 | * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img | |
d5cf4079 | 251 | * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img |
8d6d89cb | 252 | * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket |
8d6d89cb | 253 | */ |
7edac2dd PKK |
254 | static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf, |
255 | const char *filename) | |
8d6d89cb | 256 | { |
7edac2dd | 257 | GlusterServer *gsconf; |
8d6d89cb BR |
258 | URI *uri; |
259 | QueryParams *qp = NULL; | |
260 | bool is_unix = false; | |
261 | int ret = 0; | |
262 | ||
263 | uri = uri_parse(filename); | |
264 | if (!uri) { | |
265 | return -EINVAL; | |
266 | } | |
267 | ||
6c7189bb PKK |
268 | gconf->server = g_new0(GlusterServerList, 1); |
269 | gconf->server->value = gsconf = g_new0(GlusterServer, 1); | |
7edac2dd | 270 | |
8d6d89cb | 271 | /* transport */ |
24897a76 | 272 | if (!uri->scheme || !strcmp(uri->scheme, "gluster")) { |
7edac2dd | 273 | gsconf->type = GLUSTER_TRANSPORT_TCP; |
8d6d89cb | 274 | } else if (!strcmp(uri->scheme, "gluster+tcp")) { |
7edac2dd | 275 | gsconf->type = GLUSTER_TRANSPORT_TCP; |
8d6d89cb | 276 | } else if (!strcmp(uri->scheme, "gluster+unix")) { |
7edac2dd | 277 | gsconf->type = GLUSTER_TRANSPORT_UNIX; |
8d6d89cb BR |
278 | is_unix = true; |
279 | } else if (!strcmp(uri->scheme, "gluster+rdma")) { | |
7edac2dd | 280 | gsconf->type = GLUSTER_TRANSPORT_TCP; |
0552ff24 PKK |
281 | error_report("Warning: rdma feature is not supported, falling " |
282 | "back to tcp"); | |
8d6d89cb BR |
283 | } else { |
284 | ret = -EINVAL; | |
285 | goto out; | |
286 | } | |
287 | ||
288 | ret = parse_volume_options(gconf, uri->path); | |
289 | if (ret < 0) { | |
290 | goto out; | |
291 | } | |
292 | ||
293 | qp = query_params_parse(uri->query); | |
294 | if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) { | |
295 | ret = -EINVAL; | |
296 | goto out; | |
297 | } | |
298 | ||
299 | if (is_unix) { | |
300 | if (uri->server || uri->port) { | |
301 | ret = -EINVAL; | |
302 | goto out; | |
303 | } | |
304 | if (strcmp(qp->p[0].name, "socket")) { | |
305 | ret = -EINVAL; | |
306 | goto out; | |
307 | } | |
7edac2dd | 308 | gsconf->u.q_unix.path = g_strdup(qp->p[0].value); |
8d6d89cb | 309 | } else { |
7edac2dd PKK |
310 | gsconf->u.tcp.host = g_strdup(uri->server ? uri->server : "localhost"); |
311 | if (uri->port) { | |
312 | gsconf->u.tcp.port = g_strdup_printf("%d", uri->port); | |
313 | } else { | |
314 | gsconf->u.tcp.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT); | |
315 | } | |
8d6d89cb BR |
316 | } |
317 | ||
318 | out: | |
319 | if (qp) { | |
320 | query_params_free(qp); | |
321 | } | |
322 | uri_free(uri); | |
323 | return ret; | |
324 | } | |
325 | ||
6c7189bb PKK |
326 | static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf, |
327 | Error **errp) | |
8d6d89cb | 328 | { |
6c7189bb | 329 | struct glfs *glfs; |
8d6d89cb BR |
330 | int ret; |
331 | int old_errno; | |
6c7189bb | 332 | GlusterServerList *server; |
8d6d89cb | 333 | |
d5cf4079 | 334 | glfs = glfs_new(gconf->volume); |
8d6d89cb BR |
335 | if (!glfs) { |
336 | goto out; | |
337 | } | |
338 | ||
6c7189bb PKK |
339 | for (server = gconf->server; server; server = server->next) { |
340 | if (server->value->type == GLUSTER_TRANSPORT_UNIX) { | |
341 | ret = glfs_set_volfile_server(glfs, | |
342 | GlusterTransport_lookup[server->value->type], | |
343 | server->value->u.q_unix.path, 0); | |
344 | } else { | |
345 | ret = glfs_set_volfile_server(glfs, | |
346 | GlusterTransport_lookup[server->value->type], | |
347 | server->value->u.tcp.host, | |
348 | atoi(server->value->u.tcp.port)); | |
349 | } | |
350 | ||
351 | if (ret < 0) { | |
352 | goto out; | |
353 | } | |
8d6d89cb BR |
354 | } |
355 | ||
e9db8ff3 | 356 | ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug_level); |
8d6d89cb BR |
357 | if (ret < 0) { |
358 | goto out; | |
359 | } | |
360 | ||
361 | ret = glfs_init(glfs); | |
362 | if (ret) { | |
6c7189bb PKK |
363 | error_setg(errp, "Gluster connection for volume %s, path %s failed" |
364 | " to connect", gconf->volume, gconf->path); | |
365 | for (server = gconf->server; server; server = server->next) { | |
366 | if (server->value->type == GLUSTER_TRANSPORT_UNIX) { | |
367 | error_append_hint(errp, "hint: failed on socket %s ", | |
368 | server->value->u.q_unix.path); | |
369 | } else { | |
370 | error_append_hint(errp, "hint: failed on host %s and port %s ", | |
371 | server->value->u.tcp.host, | |
372 | server->value->u.tcp.port); | |
373 | } | |
7edac2dd | 374 | } |
4557117d | 375 | |
6c7189bb PKK |
376 | error_append_hint(errp, "Please refer to gluster logs for more info\n"); |
377 | ||
4557117d | 378 | /* glfs_init sometimes doesn't set errno although docs suggest that */ |
7edac2dd | 379 | if (errno == 0) { |
4557117d | 380 | errno = EINVAL; |
7edac2dd | 381 | } |
4557117d | 382 | |
8d6d89cb BR |
383 | goto out; |
384 | } | |
385 | return glfs; | |
386 | ||
387 | out: | |
388 | if (glfs) { | |
389 | old_errno = errno; | |
390 | glfs_fini(glfs); | |
391 | errno = old_errno; | |
392 | } | |
393 | return NULL; | |
394 | } | |
395 | ||
6c7189bb PKK |
396 | static int qapi_enum_parse(const char *opt) |
397 | { | |
398 | int i; | |
399 | ||
400 | if (!opt) { | |
401 | return GLUSTER_TRANSPORT__MAX; | |
402 | } | |
403 | ||
404 | for (i = 0; i < GLUSTER_TRANSPORT__MAX; i++) { | |
405 | if (!strcmp(opt, GlusterTransport_lookup[i])) { | |
406 | return i; | |
407 | } | |
408 | } | |
409 | ||
410 | return i; | |
411 | } | |
412 | ||
413 | /* | |
414 | * Convert the json formatted command line into qapi. | |
415 | */ | |
416 | static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf, | |
417 | QDict *options, Error **errp) | |
418 | { | |
419 | QemuOpts *opts; | |
420 | GlusterServer *gsconf; | |
421 | GlusterServerList *curr = NULL; | |
422 | QDict *backing_options = NULL; | |
423 | Error *local_err = NULL; | |
424 | char *str = NULL; | |
425 | const char *ptr; | |
426 | size_t num_servers; | |
427 | int i; | |
428 | ||
429 | /* create opts info from runtime_json_opts list */ | |
430 | opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort); | |
431 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
432 | if (local_err) { | |
433 | goto out; | |
434 | } | |
435 | ||
436 | num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN); | |
437 | if (num_servers < 1) { | |
438 | error_setg(&local_err, QERR_MISSING_PARAMETER, "server"); | |
439 | goto out; | |
440 | } | |
441 | ||
442 | ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME); | |
443 | if (!ptr) { | |
444 | error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME); | |
445 | goto out; | |
446 | } | |
447 | gconf->volume = g_strdup(ptr); | |
448 | ||
449 | ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH); | |
450 | if (!ptr) { | |
451 | error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH); | |
452 | goto out; | |
453 | } | |
454 | gconf->path = g_strdup(ptr); | |
455 | qemu_opts_del(opts); | |
456 | ||
457 | for (i = 0; i < num_servers; i++) { | |
458 | str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i); | |
459 | qdict_extract_subqdict(options, &backing_options, str); | |
460 | ||
461 | /* create opts info from runtime_type_opts list */ | |
462 | opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort); | |
463 | qemu_opts_absorb_qdict(opts, backing_options, &local_err); | |
464 | if (local_err) { | |
465 | goto out; | |
466 | } | |
467 | ||
468 | ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE); | |
469 | gsconf = g_new0(GlusterServer, 1); | |
470 | gsconf->type = qapi_enum_parse(ptr); | |
471 | if (!ptr) { | |
472 | error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE); | |
473 | error_append_hint(&local_err, GERR_INDEX_HINT, i); | |
474 | goto out; | |
475 | ||
476 | } | |
477 | if (gsconf->type == GLUSTER_TRANSPORT__MAX) { | |
478 | error_setg(&local_err, QERR_INVALID_PARAMETER_VALUE, | |
479 | GLUSTER_OPT_TYPE, "tcp or unix"); | |
480 | error_append_hint(&local_err, GERR_INDEX_HINT, i); | |
481 | goto out; | |
482 | } | |
483 | qemu_opts_del(opts); | |
484 | ||
485 | if (gsconf->type == GLUSTER_TRANSPORT_TCP) { | |
486 | /* create opts info from runtime_tcp_opts list */ | |
487 | opts = qemu_opts_create(&runtime_tcp_opts, NULL, 0, &error_abort); | |
488 | qemu_opts_absorb_qdict(opts, backing_options, &local_err); | |
489 | if (local_err) { | |
490 | goto out; | |
491 | } | |
492 | ||
493 | ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST); | |
494 | if (!ptr) { | |
495 | error_setg(&local_err, QERR_MISSING_PARAMETER, | |
496 | GLUSTER_OPT_HOST); | |
497 | error_append_hint(&local_err, GERR_INDEX_HINT, i); | |
498 | goto out; | |
499 | } | |
500 | gsconf->u.tcp.host = g_strdup(ptr); | |
501 | ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT); | |
502 | if (!ptr) { | |
503 | error_setg(&local_err, QERR_MISSING_PARAMETER, | |
504 | GLUSTER_OPT_PORT); | |
505 | error_append_hint(&local_err, GERR_INDEX_HINT, i); | |
506 | goto out; | |
507 | } | |
508 | gsconf->u.tcp.port = g_strdup(ptr); | |
509 | ||
510 | /* defend for unsupported fields in InetSocketAddress, | |
511 | * i.e. @ipv4, @ipv6 and @to | |
512 | */ | |
513 | ptr = qemu_opt_get(opts, GLUSTER_OPT_TO); | |
514 | if (ptr) { | |
515 | gsconf->u.tcp.has_to = true; | |
516 | } | |
517 | ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4); | |
518 | if (ptr) { | |
519 | gsconf->u.tcp.has_ipv4 = true; | |
520 | } | |
521 | ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6); | |
522 | if (ptr) { | |
523 | gsconf->u.tcp.has_ipv6 = true; | |
524 | } | |
525 | if (gsconf->u.tcp.has_to) { | |
526 | error_setg(&local_err, "Parameter 'to' not supported"); | |
527 | goto out; | |
528 | } | |
529 | if (gsconf->u.tcp.has_ipv4 || gsconf->u.tcp.has_ipv6) { | |
530 | error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported"); | |
531 | goto out; | |
532 | } | |
533 | qemu_opts_del(opts); | |
534 | } else { | |
535 | /* create opts info from runtime_unix_opts list */ | |
536 | opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort); | |
537 | qemu_opts_absorb_qdict(opts, backing_options, &local_err); | |
538 | if (local_err) { | |
539 | goto out; | |
540 | } | |
541 | ||
542 | ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET); | |
543 | if (!ptr) { | |
544 | error_setg(&local_err, QERR_MISSING_PARAMETER, | |
545 | GLUSTER_OPT_SOCKET); | |
546 | error_append_hint(&local_err, GERR_INDEX_HINT, i); | |
547 | goto out; | |
548 | } | |
549 | gsconf->u.q_unix.path = g_strdup(ptr); | |
550 | qemu_opts_del(opts); | |
551 | } | |
552 | ||
553 | if (gconf->server == NULL) { | |
554 | gconf->server = g_new0(GlusterServerList, 1); | |
555 | gconf->server->value = gsconf; | |
556 | curr = gconf->server; | |
557 | } else { | |
558 | curr->next = g_new0(GlusterServerList, 1); | |
559 | curr->next->value = gsconf; | |
560 | curr = curr->next; | |
561 | } | |
562 | ||
563 | qdict_del(backing_options, str); | |
564 | g_free(str); | |
565 | str = NULL; | |
566 | } | |
567 | ||
568 | return 0; | |
569 | ||
570 | out: | |
571 | error_propagate(errp, local_err); | |
572 | qemu_opts_del(opts); | |
573 | if (str) { | |
574 | qdict_del(backing_options, str); | |
575 | g_free(str); | |
576 | } | |
577 | errno = EINVAL; | |
578 | return -errno; | |
579 | } | |
580 | ||
581 | static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf, | |
582 | const char *filename, | |
583 | QDict *options, Error **errp) | |
584 | { | |
585 | int ret; | |
586 | if (filename) { | |
587 | ret = qemu_gluster_parse_uri(gconf, filename); | |
588 | if (ret < 0) { | |
589 | error_setg(errp, "invalid URI"); | |
590 | error_append_hint(errp, "Usage: file=gluster[+transport]://" | |
e9db8ff3 PKK |
591 | "[host[:port]]volume/path[?socket=...]" |
592 | "[,file.debug=N]" | |
593 | "[,file.logfile=/path/filename.log]\n"); | |
6c7189bb PKK |
594 | errno = -ret; |
595 | return NULL; | |
596 | } | |
597 | } else { | |
598 | ret = qemu_gluster_parse_json(gconf, options, errp); | |
599 | if (ret < 0) { | |
600 | error_append_hint(errp, "Usage: " | |
601 | "-drive driver=qcow2,file.driver=gluster," | |
602 | "file.volume=testvol,file.path=/path/a.qcow2" | |
e9db8ff3 PKK |
603 | "[,file.debug=9]" |
604 | "[,file.logfile=/path/filename.log]," | |
605 | "file.server.0.type=tcp," | |
6c7189bb PKK |
606 | "file.server.0.host=1.2.3.4," |
607 | "file.server.0.port=24007," | |
608 | "file.server.1.transport=unix," | |
609 | "file.server.1.socket=/var/run/glusterd.socket ..." | |
610 | "\n"); | |
611 | errno = -ret; | |
612 | return NULL; | |
613 | } | |
614 | ||
615 | } | |
616 | ||
617 | return qemu_gluster_glfs_init(gconf, errp); | |
618 | } | |
619 | ||
15744b0b | 620 | static void qemu_gluster_complete_aio(void *opaque) |
8d6d89cb | 621 | { |
15744b0b | 622 | GlusterAIOCB *acb = (GlusterAIOCB *)opaque; |
8d6d89cb | 623 | |
0b8b8753 | 624 | qemu_coroutine_enter(acb->coroutine); |
8d6d89cb BR |
625 | } |
626 | ||
7c815372 BR |
627 | /* |
628 | * AIO callback routine called from GlusterFS thread. | |
629 | */ | |
630 | static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg) | |
631 | { | |
632 | GlusterAIOCB *acb = (GlusterAIOCB *)arg; | |
633 | ||
634 | if (!ret || ret == acb->size) { | |
635 | acb->ret = 0; /* Success */ | |
636 | } else if (ret < 0) { | |
a8827453 | 637 | acb->ret = -errno; /* Read/Write failed */ |
7c815372 BR |
638 | } else { |
639 | acb->ret = -EIO; /* Partial read/write - fail it */ | |
640 | } | |
641 | ||
fffb6e12 | 642 | aio_bh_schedule_oneshot(acb->aio_context, qemu_gluster_complete_aio, acb); |
7c815372 BR |
643 | } |
644 | ||
1b37b344 JC |
645 | static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags) |
646 | { | |
647 | assert(open_flags != NULL); | |
648 | ||
649 | *open_flags |= O_BINARY; | |
650 | ||
651 | if (bdrv_flags & BDRV_O_RDWR) { | |
652 | *open_flags |= O_RDWR; | |
653 | } else { | |
654 | *open_flags |= O_RDONLY; | |
655 | } | |
656 | ||
657 | if ((bdrv_flags & BDRV_O_NOCACHE)) { | |
658 | *open_flags |= O_DIRECT; | |
659 | } | |
660 | } | |
661 | ||
947eb203 NV |
662 | /* |
663 | * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of | |
664 | * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used. | |
665 | * - Corrected versions return -1 and set errno to EINVAL. | |
666 | * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set | |
667 | * errno to ENXIO when SEEK_DATA is called with a position of EOF. | |
668 | */ | |
669 | static bool qemu_gluster_test_seek(struct glfs_fd *fd) | |
670 | { | |
d9b78974 JC |
671 | off_t ret = 0; |
672 | ||
673 | #if defined SEEK_HOLE && defined SEEK_DATA | |
674 | off_t eof; | |
947eb203 NV |
675 | |
676 | eof = glfs_lseek(fd, 0, SEEK_END); | |
677 | if (eof < 0) { | |
678 | /* this should never occur */ | |
679 | return false; | |
680 | } | |
681 | ||
682 | /* this should always fail with ENXIO if SEEK_DATA is supported */ | |
683 | ret = glfs_lseek(fd, eof, SEEK_DATA); | |
d9b78974 JC |
684 | #endif |
685 | ||
947eb203 NV |
686 | return (ret < 0) && (errno == ENXIO); |
687 | } | |
688 | ||
56d1b4d2 | 689 | static int qemu_gluster_open(BlockDriverState *bs, QDict *options, |
015a1036 | 690 | int bdrv_flags, Error **errp) |
8d6d89cb BR |
691 | { |
692 | BDRVGlusterState *s = bs->opaque; | |
1b37b344 | 693 | int open_flags = 0; |
8d6d89cb | 694 | int ret = 0; |
7edac2dd | 695 | BlockdevOptionsGluster *gconf = NULL; |
b4894776 KW |
696 | QemuOpts *opts; |
697 | Error *local_err = NULL; | |
e9db8ff3 | 698 | const char *filename, *logfile; |
b4894776 | 699 | |
87ea75d5 | 700 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
b4894776 | 701 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 702 | if (local_err) { |
a7451cb8 | 703 | error_propagate(errp, local_err); |
b4894776 KW |
704 | ret = -EINVAL; |
705 | goto out; | |
706 | } | |
707 | ||
7eac868a JC |
708 | filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME); |
709 | ||
710 | s->debug_level = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG, | |
711 | GLUSTER_DEBUG_DEFAULT); | |
712 | if (s->debug_level < 0) { | |
713 | s->debug_level = 0; | |
714 | } else if (s->debug_level > GLUSTER_DEBUG_MAX) { | |
715 | s->debug_level = GLUSTER_DEBUG_MAX; | |
716 | } | |
b4894776 | 717 | |
7edac2dd | 718 | gconf = g_new0(BlockdevOptionsGluster, 1); |
7eac868a | 719 | gconf->debug_level = s->debug_level; |
7edac2dd | 720 | gconf->has_debug_level = true; |
e9db8ff3 PKK |
721 | |
722 | logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE); | |
723 | s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT); | |
724 | ||
725 | gconf->logfile = g_strdup(s->logfile); | |
726 | gconf->has_logfile = true; | |
727 | ||
6c7189bb | 728 | s->glfs = qemu_gluster_init(gconf, filename, options, errp); |
8d6d89cb BR |
729 | if (!s->glfs) { |
730 | ret = -errno; | |
731 | goto out; | |
732 | } | |
733 | ||
d85fa9eb JC |
734 | #ifdef CONFIG_GLUSTERFS_XLATOR_OPT |
735 | /* Without this, if fsync fails for a recoverable reason (for instance, | |
736 | * ENOSPC), gluster will dump its cache, preventing retries. This means | |
737 | * almost certain data loss. Not all gluster versions support the | |
738 | * 'resync-failed-syncs-after-fsync' key value, but there is no way to | |
739 | * discover during runtime if it is supported (this api returns success for | |
740 | * unknown key/value pairs) */ | |
741 | ret = glfs_set_xlator_option(s->glfs, "*-write-behind", | |
742 | "resync-failed-syncs-after-fsync", | |
743 | "on"); | |
744 | if (ret < 0) { | |
745 | error_setg_errno(errp, errno, "Unable to set xlator key/value pair"); | |
746 | ret = -errno; | |
747 | goto out; | |
748 | } | |
749 | #endif | |
750 | ||
1b37b344 | 751 | qemu_gluster_parse_flags(bdrv_flags, &open_flags); |
8d6d89cb | 752 | |
d5cf4079 | 753 | s->fd = glfs_open(s->glfs, gconf->path, open_flags); |
8d6d89cb BR |
754 | if (!s->fd) { |
755 | ret = -errno; | |
8d6d89cb | 756 | } |
8d6d89cb | 757 | |
947eb203 NV |
758 | s->supports_seek_data = qemu_gluster_test_seek(s->fd); |
759 | ||
8d6d89cb | 760 | out: |
b4894776 | 761 | qemu_opts_del(opts); |
7edac2dd | 762 | qapi_free_BlockdevOptionsGluster(gconf); |
8d6d89cb BR |
763 | if (!ret) { |
764 | return ret; | |
765 | } | |
e9db8ff3 | 766 | g_free(s->logfile); |
8d6d89cb BR |
767 | if (s->fd) { |
768 | glfs_close(s->fd); | |
769 | } | |
770 | if (s->glfs) { | |
771 | glfs_fini(s->glfs); | |
772 | } | |
773 | return ret; | |
774 | } | |
775 | ||
adccfbcd JC |
776 | static int qemu_gluster_reopen_prepare(BDRVReopenState *state, |
777 | BlockReopenQueue *queue, Error **errp) | |
778 | { | |
779 | int ret = 0; | |
7eac868a | 780 | BDRVGlusterState *s; |
adccfbcd | 781 | BDRVGlusterReopenState *reop_s; |
7edac2dd | 782 | BlockdevOptionsGluster *gconf; |
adccfbcd JC |
783 | int open_flags = 0; |
784 | ||
785 | assert(state != NULL); | |
786 | assert(state->bs != NULL); | |
787 | ||
7eac868a JC |
788 | s = state->bs->opaque; |
789 | ||
5839e53b | 790 | state->opaque = g_new0(BDRVGlusterReopenState, 1); |
adccfbcd JC |
791 | reop_s = state->opaque; |
792 | ||
793 | qemu_gluster_parse_flags(state->flags, &open_flags); | |
794 | ||
7edac2dd | 795 | gconf = g_new0(BlockdevOptionsGluster, 1); |
7eac868a | 796 | gconf->debug_level = s->debug_level; |
7edac2dd | 797 | gconf->has_debug_level = true; |
e9db8ff3 PKK |
798 | gconf->logfile = g_strdup(s->logfile); |
799 | gconf->has_logfile = true; | |
6c7189bb | 800 | reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename, NULL, errp); |
adccfbcd JC |
801 | if (reop_s->glfs == NULL) { |
802 | ret = -errno; | |
803 | goto exit; | |
804 | } | |
805 | ||
d85fa9eb JC |
806 | #ifdef CONFIG_GLUSTERFS_XLATOR_OPT |
807 | ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind", | |
808 | "resync-failed-syncs-after-fsync", "on"); | |
809 | if (ret < 0) { | |
810 | error_setg_errno(errp, errno, "Unable to set xlator key/value pair"); | |
811 | ret = -errno; | |
812 | goto exit; | |
813 | } | |
814 | #endif | |
815 | ||
d5cf4079 | 816 | reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags); |
adccfbcd JC |
817 | if (reop_s->fd == NULL) { |
818 | /* reops->glfs will be cleaned up in _abort */ | |
819 | ret = -errno; | |
820 | goto exit; | |
821 | } | |
822 | ||
823 | exit: | |
824 | /* state->opaque will be freed in either the _abort or _commit */ | |
7edac2dd | 825 | qapi_free_BlockdevOptionsGluster(gconf); |
adccfbcd JC |
826 | return ret; |
827 | } | |
828 | ||
829 | static void qemu_gluster_reopen_commit(BDRVReopenState *state) | |
830 | { | |
831 | BDRVGlusterReopenState *reop_s = state->opaque; | |
832 | BDRVGlusterState *s = state->bs->opaque; | |
833 | ||
834 | ||
835 | /* close the old */ | |
836 | if (s->fd) { | |
837 | glfs_close(s->fd); | |
838 | } | |
839 | if (s->glfs) { | |
840 | glfs_fini(s->glfs); | |
841 | } | |
842 | ||
843 | /* use the newly opened image / connection */ | |
844 | s->fd = reop_s->fd; | |
845 | s->glfs = reop_s->glfs; | |
846 | ||
847 | g_free(state->opaque); | |
848 | state->opaque = NULL; | |
849 | ||
850 | return; | |
851 | } | |
852 | ||
853 | ||
854 | static void qemu_gluster_reopen_abort(BDRVReopenState *state) | |
855 | { | |
856 | BDRVGlusterReopenState *reop_s = state->opaque; | |
857 | ||
858 | if (reop_s == NULL) { | |
859 | return; | |
860 | } | |
861 | ||
862 | if (reop_s->fd) { | |
863 | glfs_close(reop_s->fd); | |
864 | } | |
865 | ||
866 | if (reop_s->glfs) { | |
867 | glfs_fini(reop_s->glfs); | |
868 | } | |
869 | ||
870 | g_free(state->opaque); | |
871 | state->opaque = NULL; | |
872 | ||
873 | return; | |
874 | } | |
875 | ||
7c815372 | 876 | #ifdef CONFIG_GLUSTERFS_ZEROFILL |
e88a36eb | 877 | static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs, |
f70c50c8 PKK |
878 | int64_t offset, |
879 | int size, | |
880 | BdrvRequestFlags flags) | |
7c815372 BR |
881 | { |
882 | int ret; | |
c833d1e8 | 883 | GlusterAIOCB acb; |
7c815372 | 884 | BDRVGlusterState *s = bs->opaque; |
7c815372 | 885 | |
c833d1e8 PB |
886 | acb.size = size; |
887 | acb.ret = 0; | |
888 | acb.coroutine = qemu_coroutine_self(); | |
889 | acb.aio_context = bdrv_get_aio_context(bs); | |
7c815372 | 890 | |
c833d1e8 | 891 | ret = glfs_zerofill_async(s->fd, offset, size, gluster_finish_aiocb, &acb); |
7c815372 | 892 | if (ret < 0) { |
c833d1e8 | 893 | return -errno; |
7c815372 BR |
894 | } |
895 | ||
896 | qemu_coroutine_yield(); | |
c833d1e8 | 897 | return acb.ret; |
7c815372 | 898 | } |
cf7f616b BR |
899 | |
900 | static inline bool gluster_supports_zerofill(void) | |
901 | { | |
902 | return 1; | |
903 | } | |
904 | ||
905 | static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset, | |
f70c50c8 | 906 | int64_t size) |
cf7f616b BR |
907 | { |
908 | return glfs_zerofill(fd, offset, size); | |
909 | } | |
910 | ||
911 | #else | |
912 | static inline bool gluster_supports_zerofill(void) | |
913 | { | |
914 | return 0; | |
915 | } | |
916 | ||
917 | static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset, | |
f70c50c8 | 918 | int64_t size) |
cf7f616b BR |
919 | { |
920 | return 0; | |
921 | } | |
7c815372 BR |
922 | #endif |
923 | ||
8d6d89cb | 924 | static int qemu_gluster_create(const char *filename, |
90c772de | 925 | QemuOpts *opts, Error **errp) |
8d6d89cb | 926 | { |
7edac2dd | 927 | BlockdevOptionsGluster *gconf; |
8d6d89cb BR |
928 | struct glfs *glfs; |
929 | struct glfs_fd *fd; | |
930 | int ret = 0; | |
cf7f616b | 931 | int prealloc = 0; |
8d6d89cb | 932 | int64_t total_size = 0; |
90c772de | 933 | char *tmp = NULL; |
8d6d89cb | 934 | |
7edac2dd | 935 | gconf = g_new0(BlockdevOptionsGluster, 1); |
7eac868a JC |
936 | gconf->debug_level = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG, |
937 | GLUSTER_DEBUG_DEFAULT); | |
938 | if (gconf->debug_level < 0) { | |
939 | gconf->debug_level = 0; | |
940 | } else if (gconf->debug_level > GLUSTER_DEBUG_MAX) { | |
941 | gconf->debug_level = GLUSTER_DEBUG_MAX; | |
942 | } | |
7edac2dd | 943 | gconf->has_debug_level = true; |
7eac868a | 944 | |
e9db8ff3 PKK |
945 | gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE); |
946 | if (!gconf->logfile) { | |
947 | gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT); | |
948 | } | |
949 | gconf->has_logfile = true; | |
950 | ||
6c7189bb | 951 | glfs = qemu_gluster_init(gconf, filename, NULL, errp); |
8d6d89cb | 952 | if (!glfs) { |
4557117d | 953 | ret = -errno; |
8d6d89cb BR |
954 | goto out; |
955 | } | |
956 | ||
180e9526 HT |
957 | total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
958 | BDRV_SECTOR_SIZE); | |
90c772de CL |
959 | |
960 | tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC); | |
961 | if (!tmp || !strcmp(tmp, "off")) { | |
962 | prealloc = 0; | |
f70c50c8 | 963 | } else if (!strcmp(tmp, "full") && gluster_supports_zerofill()) { |
90c772de CL |
964 | prealloc = 1; |
965 | } else { | |
966 | error_setg(errp, "Invalid preallocation mode: '%s'" | |
f70c50c8 | 967 | " or GlusterFS doesn't support zerofill API", tmp); |
90c772de CL |
968 | ret = -EINVAL; |
969 | goto out; | |
8d6d89cb BR |
970 | } |
971 | ||
d5cf4079 | 972 | fd = glfs_creat(glfs, gconf->path, |
f70c50c8 | 973 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR); |
8d6d89cb BR |
974 | if (!fd) { |
975 | ret = -errno; | |
976 | } else { | |
180e9526 HT |
977 | if (!glfs_ftruncate(fd, total_size)) { |
978 | if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) { | |
cf7f616b BR |
979 | ret = -errno; |
980 | } | |
981 | } else { | |
8d6d89cb BR |
982 | ret = -errno; |
983 | } | |
cf7f616b | 984 | |
8d6d89cb BR |
985 | if (glfs_close(fd) != 0) { |
986 | ret = -errno; | |
987 | } | |
988 | } | |
989 | out: | |
90c772de | 990 | g_free(tmp); |
7edac2dd | 991 | qapi_free_BlockdevOptionsGluster(gconf); |
8d6d89cb BR |
992 | if (glfs) { |
993 | glfs_fini(glfs); | |
994 | } | |
995 | return ret; | |
996 | } | |
997 | ||
15744b0b | 998 | static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs, |
f70c50c8 PKK |
999 | int64_t sector_num, int nb_sectors, |
1000 | QEMUIOVector *qiov, int write) | |
8d6d89cb BR |
1001 | { |
1002 | int ret; | |
c833d1e8 | 1003 | GlusterAIOCB acb; |
8d6d89cb | 1004 | BDRVGlusterState *s = bs->opaque; |
15744b0b BR |
1005 | size_t size = nb_sectors * BDRV_SECTOR_SIZE; |
1006 | off_t offset = sector_num * BDRV_SECTOR_SIZE; | |
8d6d89cb | 1007 | |
c833d1e8 PB |
1008 | acb.size = size; |
1009 | acb.ret = 0; | |
1010 | acb.coroutine = qemu_coroutine_self(); | |
1011 | acb.aio_context = bdrv_get_aio_context(bs); | |
8d6d89cb BR |
1012 | |
1013 | if (write) { | |
1014 | ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0, | |
f70c50c8 | 1015 | gluster_finish_aiocb, &acb); |
8d6d89cb BR |
1016 | } else { |
1017 | ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0, | |
f70c50c8 | 1018 | gluster_finish_aiocb, &acb); |
8d6d89cb BR |
1019 | } |
1020 | ||
1021 | if (ret < 0) { | |
c833d1e8 | 1022 | return -errno; |
8d6d89cb | 1023 | } |
15744b0b BR |
1024 | |
1025 | qemu_coroutine_yield(); | |
c833d1e8 | 1026 | return acb.ret; |
8d6d89cb BR |
1027 | } |
1028 | ||
42ec24e2 PB |
1029 | static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset) |
1030 | { | |
1031 | int ret; | |
1032 | BDRVGlusterState *s = bs->opaque; | |
1033 | ||
1034 | ret = glfs_ftruncate(s->fd, offset); | |
1035 | if (ret < 0) { | |
1036 | return -errno; | |
1037 | } | |
1038 | ||
1039 | return 0; | |
1040 | } | |
1041 | ||
15744b0b | 1042 | static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs, |
f70c50c8 PKK |
1043 | int64_t sector_num, |
1044 | int nb_sectors, | |
1045 | QEMUIOVector *qiov) | |
8d6d89cb | 1046 | { |
15744b0b | 1047 | return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0); |
8d6d89cb BR |
1048 | } |
1049 | ||
15744b0b | 1050 | static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs, |
f70c50c8 PKK |
1051 | int64_t sector_num, |
1052 | int nb_sectors, | |
1053 | QEMUIOVector *qiov) | |
8d6d89cb | 1054 | { |
15744b0b | 1055 | return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1); |
8d6d89cb BR |
1056 | } |
1057 | ||
5d4343e6 JC |
1058 | static void qemu_gluster_close(BlockDriverState *bs) |
1059 | { | |
1060 | BDRVGlusterState *s = bs->opaque; | |
1061 | ||
e9db8ff3 | 1062 | g_free(s->logfile); |
5d4343e6 JC |
1063 | if (s->fd) { |
1064 | glfs_close(s->fd); | |
1065 | s->fd = NULL; | |
1066 | } | |
1067 | glfs_fini(s->glfs); | |
1068 | } | |
1069 | ||
15744b0b | 1070 | static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs) |
8d6d89cb BR |
1071 | { |
1072 | int ret; | |
c833d1e8 | 1073 | GlusterAIOCB acb; |
8d6d89cb BR |
1074 | BDRVGlusterState *s = bs->opaque; |
1075 | ||
c833d1e8 PB |
1076 | acb.size = 0; |
1077 | acb.ret = 0; | |
1078 | acb.coroutine = qemu_coroutine_self(); | |
1079 | acb.aio_context = bdrv_get_aio_context(bs); | |
8d6d89cb | 1080 | |
c833d1e8 | 1081 | ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb); |
8d6d89cb | 1082 | if (ret < 0) { |
d85fa9eb JC |
1083 | ret = -errno; |
1084 | goto error; | |
8d6d89cb | 1085 | } |
15744b0b BR |
1086 | |
1087 | qemu_coroutine_yield(); | |
d85fa9eb JC |
1088 | if (acb.ret < 0) { |
1089 | ret = acb.ret; | |
1090 | goto error; | |
1091 | } | |
1092 | ||
c833d1e8 | 1093 | return acb.ret; |
d85fa9eb JC |
1094 | |
1095 | error: | |
1096 | /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache | |
1097 | * after a fsync failure, so we have no way of allowing the guest to safely | |
1098 | * continue. Gluster versions prior to 3.5.6 don't retain the cache | |
1099 | * either, but will invalidate the fd on error, so this is again our only | |
1100 | * option. | |
1101 | * | |
1102 | * The 'resync-failed-syncs-after-fsync' xlator option for the | |
1103 | * write-behind cache will cause later gluster versions to retain its | |
1104 | * cache after error, so long as the fd remains open. However, we | |
1105 | * currently have no way of knowing if this option is supported. | |
1106 | * | |
1107 | * TODO: Once gluster provides a way for us to determine if the option | |
1108 | * is supported, bypass the closure and setting drv to NULL. */ | |
1109 | qemu_gluster_close(bs); | |
1110 | bs->drv = NULL; | |
1111 | return ret; | |
8d6d89cb BR |
1112 | } |
1113 | ||
0c14fb47 | 1114 | #ifdef CONFIG_GLUSTERFS_DISCARD |
1014170b EB |
1115 | static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs, |
1116 | int64_t offset, int size) | |
0c14fb47 BR |
1117 | { |
1118 | int ret; | |
c833d1e8 | 1119 | GlusterAIOCB acb; |
0c14fb47 | 1120 | BDRVGlusterState *s = bs->opaque; |
0c14fb47 | 1121 | |
c833d1e8 PB |
1122 | acb.size = 0; |
1123 | acb.ret = 0; | |
1124 | acb.coroutine = qemu_coroutine_self(); | |
1125 | acb.aio_context = bdrv_get_aio_context(bs); | |
0c14fb47 | 1126 | |
c833d1e8 | 1127 | ret = glfs_discard_async(s->fd, offset, size, gluster_finish_aiocb, &acb); |
0c14fb47 | 1128 | if (ret < 0) { |
c833d1e8 | 1129 | return -errno; |
0c14fb47 | 1130 | } |
15744b0b BR |
1131 | |
1132 | qemu_coroutine_yield(); | |
c833d1e8 | 1133 | return acb.ret; |
0c14fb47 BR |
1134 | } |
1135 | #endif | |
1136 | ||
8d6d89cb BR |
1137 | static int64_t qemu_gluster_getlength(BlockDriverState *bs) |
1138 | { | |
1139 | BDRVGlusterState *s = bs->opaque; | |
1140 | int64_t ret; | |
1141 | ||
1142 | ret = glfs_lseek(s->fd, 0, SEEK_END); | |
1143 | if (ret < 0) { | |
1144 | return -errno; | |
1145 | } else { | |
1146 | return ret; | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | static int64_t qemu_gluster_allocated_file_size(BlockDriverState *bs) | |
1151 | { | |
1152 | BDRVGlusterState *s = bs->opaque; | |
1153 | struct stat st; | |
1154 | int ret; | |
1155 | ||
1156 | ret = glfs_fstat(s->fd, &st); | |
1157 | if (ret < 0) { | |
1158 | return -errno; | |
1159 | } else { | |
1160 | return st.st_blocks * 512; | |
1161 | } | |
1162 | } | |
1163 | ||
8ab6feec KW |
1164 | static int qemu_gluster_has_zero_init(BlockDriverState *bs) |
1165 | { | |
1166 | /* GlusterFS volume could be backed by a block device */ | |
1167 | return 0; | |
1168 | } | |
1169 | ||
947eb203 NV |
1170 | /* |
1171 | * Find allocation range in @bs around offset @start. | |
1172 | * May change underlying file descriptor's file offset. | |
1173 | * If @start is not in a hole, store @start in @data, and the | |
1174 | * beginning of the next hole in @hole, and return 0. | |
1175 | * If @start is in a non-trailing hole, store @start in @hole and the | |
1176 | * beginning of the next non-hole in @data, and return 0. | |
1177 | * If @start is in a trailing hole or beyond EOF, return -ENXIO. | |
1178 | * If we can't find out, return a negative errno other than -ENXIO. | |
1179 | * | |
1180 | * (Shamefully copied from raw-posix.c, only miniscule adaptions.) | |
1181 | */ | |
1182 | static int find_allocation(BlockDriverState *bs, off_t start, | |
1183 | off_t *data, off_t *hole) | |
1184 | { | |
1185 | BDRVGlusterState *s = bs->opaque; | |
947eb203 NV |
1186 | |
1187 | if (!s->supports_seek_data) { | |
d9b78974 | 1188 | goto exit; |
947eb203 NV |
1189 | } |
1190 | ||
d9b78974 JC |
1191 | #if defined SEEK_HOLE && defined SEEK_DATA |
1192 | off_t offs; | |
1193 | ||
947eb203 NV |
1194 | /* |
1195 | * SEEK_DATA cases: | |
1196 | * D1. offs == start: start is in data | |
1197 | * D2. offs > start: start is in a hole, next data at offs | |
1198 | * D3. offs < 0, errno = ENXIO: either start is in a trailing hole | |
1199 | * or start is beyond EOF | |
1200 | * If the latter happens, the file has been truncated behind | |
1201 | * our back since we opened it. All bets are off then. | |
1202 | * Treating like a trailing hole is simplest. | |
1203 | * D4. offs < 0, errno != ENXIO: we learned nothing | |
1204 | */ | |
1205 | offs = glfs_lseek(s->fd, start, SEEK_DATA); | |
1206 | if (offs < 0) { | |
1207 | return -errno; /* D3 or D4 */ | |
1208 | } | |
1209 | assert(offs >= start); | |
1210 | ||
1211 | if (offs > start) { | |
1212 | /* D2: in hole, next data at offs */ | |
1213 | *hole = start; | |
1214 | *data = offs; | |
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | /* D1: in data, end not yet known */ | |
1219 | ||
1220 | /* | |
1221 | * SEEK_HOLE cases: | |
1222 | * H1. offs == start: start is in a hole | |
1223 | * If this happens here, a hole has been dug behind our back | |
1224 | * since the previous lseek(). | |
1225 | * H2. offs > start: either start is in data, next hole at offs, | |
1226 | * or start is in trailing hole, EOF at offs | |
1227 | * Linux treats trailing holes like any other hole: offs == | |
1228 | * start. Solaris seeks to EOF instead: offs > start (blech). | |
1229 | * If that happens here, a hole has been dug behind our back | |
1230 | * since the previous lseek(). | |
1231 | * H3. offs < 0, errno = ENXIO: start is beyond EOF | |
1232 | * If this happens, the file has been truncated behind our | |
1233 | * back since we opened it. Treat it like a trailing hole. | |
1234 | * H4. offs < 0, errno != ENXIO: we learned nothing | |
1235 | * Pretend we know nothing at all, i.e. "forget" about D1. | |
1236 | */ | |
1237 | offs = glfs_lseek(s->fd, start, SEEK_HOLE); | |
1238 | if (offs < 0) { | |
1239 | return -errno; /* D1 and (H3 or H4) */ | |
1240 | } | |
1241 | assert(offs >= start); | |
1242 | ||
1243 | if (offs > start) { | |
1244 | /* | |
1245 | * D1 and H2: either in data, next hole at offs, or it was in | |
1246 | * data but is now in a trailing hole. In the latter case, | |
1247 | * all bets are off. Treating it as if it there was data all | |
1248 | * the way to EOF is safe, so simply do that. | |
1249 | */ | |
1250 | *data = start; | |
1251 | *hole = offs; | |
1252 | return 0; | |
1253 | } | |
1254 | ||
1255 | /* D1 and H1 */ | |
1256 | return -EBUSY; | |
d9b78974 JC |
1257 | #endif |
1258 | ||
1259 | exit: | |
1260 | return -ENOTSUP; | |
947eb203 NV |
1261 | } |
1262 | ||
1263 | /* | |
1264 | * Returns the allocation status of the specified sectors. | |
1265 | * | |
1266 | * If 'sector_num' is beyond the end of the disk image the return value is 0 | |
1267 | * and 'pnum' is set to 0. | |
1268 | * | |
1269 | * 'pnum' is set to the number of sectors (including and immediately following | |
1270 | * the specified sector) that are known to be in the same | |
1271 | * allocated/unallocated state. | |
1272 | * | |
1273 | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes | |
1274 | * beyond the end of the disk image it will be clamped. | |
1275 | * | |
1276 | * (Based on raw_co_get_block_status() from raw-posix.c.) | |
1277 | */ | |
1278 | static int64_t coroutine_fn qemu_gluster_co_get_block_status( | |
1279 | BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum, | |
1280 | BlockDriverState **file) | |
1281 | { | |
1282 | BDRVGlusterState *s = bs->opaque; | |
1283 | off_t start, data = 0, hole = 0; | |
1284 | int64_t total_size; | |
1285 | int ret = -EINVAL; | |
1286 | ||
1287 | if (!s->fd) { | |
1288 | return ret; | |
1289 | } | |
1290 | ||
1291 | start = sector_num * BDRV_SECTOR_SIZE; | |
1292 | total_size = bdrv_getlength(bs); | |
1293 | if (total_size < 0) { | |
1294 | return total_size; | |
1295 | } else if (start >= total_size) { | |
1296 | *pnum = 0; | |
1297 | return 0; | |
1298 | } else if (start + nb_sectors * BDRV_SECTOR_SIZE > total_size) { | |
1299 | nb_sectors = DIV_ROUND_UP(total_size - start, BDRV_SECTOR_SIZE); | |
1300 | } | |
1301 | ||
1302 | ret = find_allocation(bs, start, &data, &hole); | |
1303 | if (ret == -ENXIO) { | |
1304 | /* Trailing hole */ | |
1305 | *pnum = nb_sectors; | |
1306 | ret = BDRV_BLOCK_ZERO; | |
1307 | } else if (ret < 0) { | |
1308 | /* No info available, so pretend there are no holes */ | |
1309 | *pnum = nb_sectors; | |
1310 | ret = BDRV_BLOCK_DATA; | |
1311 | } else if (data == start) { | |
1312 | /* On a data extent, compute sectors to the end of the extent, | |
1313 | * possibly including a partial sector at EOF. */ | |
1314 | *pnum = MIN(nb_sectors, DIV_ROUND_UP(hole - start, BDRV_SECTOR_SIZE)); | |
1315 | ret = BDRV_BLOCK_DATA; | |
1316 | } else { | |
1317 | /* On a hole, compute sectors to the beginning of the next extent. */ | |
1318 | assert(hole == start); | |
1319 | *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE); | |
1320 | ret = BDRV_BLOCK_ZERO; | |
1321 | } | |
1322 | ||
1323 | *file = bs; | |
1324 | ||
1325 | return ret | BDRV_BLOCK_OFFSET_VALID | start; | |
1326 | } | |
1327 | ||
1328 | ||
8d6d89cb BR |
1329 | static BlockDriver bdrv_gluster = { |
1330 | .format_name = "gluster", | |
1331 | .protocol_name = "gluster", | |
1332 | .instance_size = sizeof(BDRVGlusterState), | |
6c7189bb | 1333 | .bdrv_needs_filename = false, |
8d6d89cb | 1334 | .bdrv_file_open = qemu_gluster_open, |
adccfbcd JC |
1335 | .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, |
1336 | .bdrv_reopen_commit = qemu_gluster_reopen_commit, | |
1337 | .bdrv_reopen_abort = qemu_gluster_reopen_abort, | |
8d6d89cb | 1338 | .bdrv_close = qemu_gluster_close, |
c282e1fd | 1339 | .bdrv_create = qemu_gluster_create, |
8d6d89cb BR |
1340 | .bdrv_getlength = qemu_gluster_getlength, |
1341 | .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, | |
42ec24e2 | 1342 | .bdrv_truncate = qemu_gluster_truncate, |
15744b0b BR |
1343 | .bdrv_co_readv = qemu_gluster_co_readv, |
1344 | .bdrv_co_writev = qemu_gluster_co_writev, | |
1345 | .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, | |
8ab6feec | 1346 | .bdrv_has_zero_init = qemu_gluster_has_zero_init, |
0c14fb47 | 1347 | #ifdef CONFIG_GLUSTERFS_DISCARD |
1014170b | 1348 | .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, |
7c815372 BR |
1349 | #endif |
1350 | #ifdef CONFIG_GLUSTERFS_ZEROFILL | |
e88a36eb | 1351 | .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, |
0c14fb47 | 1352 | #endif |
947eb203 | 1353 | .bdrv_co_get_block_status = qemu_gluster_co_get_block_status, |
90c772de | 1354 | .create_opts = &qemu_gluster_create_opts, |
8d6d89cb BR |
1355 | }; |
1356 | ||
1357 | static BlockDriver bdrv_gluster_tcp = { | |
1358 | .format_name = "gluster", | |
1359 | .protocol_name = "gluster+tcp", | |
1360 | .instance_size = sizeof(BDRVGlusterState), | |
6c7189bb | 1361 | .bdrv_needs_filename = false, |
8d6d89cb | 1362 | .bdrv_file_open = qemu_gluster_open, |
adccfbcd JC |
1363 | .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, |
1364 | .bdrv_reopen_commit = qemu_gluster_reopen_commit, | |
1365 | .bdrv_reopen_abort = qemu_gluster_reopen_abort, | |
8d6d89cb | 1366 | .bdrv_close = qemu_gluster_close, |
c282e1fd | 1367 | .bdrv_create = qemu_gluster_create, |
8d6d89cb BR |
1368 | .bdrv_getlength = qemu_gluster_getlength, |
1369 | .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, | |
42ec24e2 | 1370 | .bdrv_truncate = qemu_gluster_truncate, |
15744b0b BR |
1371 | .bdrv_co_readv = qemu_gluster_co_readv, |
1372 | .bdrv_co_writev = qemu_gluster_co_writev, | |
1373 | .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, | |
8ab6feec | 1374 | .bdrv_has_zero_init = qemu_gluster_has_zero_init, |
0c14fb47 | 1375 | #ifdef CONFIG_GLUSTERFS_DISCARD |
1014170b | 1376 | .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, |
7c815372 BR |
1377 | #endif |
1378 | #ifdef CONFIG_GLUSTERFS_ZEROFILL | |
e88a36eb | 1379 | .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, |
0c14fb47 | 1380 | #endif |
947eb203 | 1381 | .bdrv_co_get_block_status = qemu_gluster_co_get_block_status, |
90c772de | 1382 | .create_opts = &qemu_gluster_create_opts, |
8d6d89cb BR |
1383 | }; |
1384 | ||
1385 | static BlockDriver bdrv_gluster_unix = { | |
1386 | .format_name = "gluster", | |
1387 | .protocol_name = "gluster+unix", | |
1388 | .instance_size = sizeof(BDRVGlusterState), | |
030be321 | 1389 | .bdrv_needs_filename = true, |
8d6d89cb | 1390 | .bdrv_file_open = qemu_gluster_open, |
adccfbcd JC |
1391 | .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, |
1392 | .bdrv_reopen_commit = qemu_gluster_reopen_commit, | |
1393 | .bdrv_reopen_abort = qemu_gluster_reopen_abort, | |
8d6d89cb | 1394 | .bdrv_close = qemu_gluster_close, |
c282e1fd | 1395 | .bdrv_create = qemu_gluster_create, |
8d6d89cb BR |
1396 | .bdrv_getlength = qemu_gluster_getlength, |
1397 | .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, | |
42ec24e2 | 1398 | .bdrv_truncate = qemu_gluster_truncate, |
15744b0b BR |
1399 | .bdrv_co_readv = qemu_gluster_co_readv, |
1400 | .bdrv_co_writev = qemu_gluster_co_writev, | |
1401 | .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, | |
8ab6feec | 1402 | .bdrv_has_zero_init = qemu_gluster_has_zero_init, |
0c14fb47 | 1403 | #ifdef CONFIG_GLUSTERFS_DISCARD |
1014170b | 1404 | .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, |
7c815372 BR |
1405 | #endif |
1406 | #ifdef CONFIG_GLUSTERFS_ZEROFILL | |
e88a36eb | 1407 | .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, |
0c14fb47 | 1408 | #endif |
947eb203 | 1409 | .bdrv_co_get_block_status = qemu_gluster_co_get_block_status, |
90c772de | 1410 | .create_opts = &qemu_gluster_create_opts, |
8d6d89cb BR |
1411 | }; |
1412 | ||
0552ff24 PKK |
1413 | /* rdma is deprecated (actually never supported for volfile fetch). |
1414 | * Let's maintain it for the protocol compatibility, to make sure things | |
1415 | * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp | |
1416 | * protocol with a warning. | |
1417 | * TODO: remove gluster+rdma interface support | |
1418 | */ | |
8d6d89cb BR |
1419 | static BlockDriver bdrv_gluster_rdma = { |
1420 | .format_name = "gluster", | |
1421 | .protocol_name = "gluster+rdma", | |
1422 | .instance_size = sizeof(BDRVGlusterState), | |
030be321 | 1423 | .bdrv_needs_filename = true, |
8d6d89cb | 1424 | .bdrv_file_open = qemu_gluster_open, |
adccfbcd JC |
1425 | .bdrv_reopen_prepare = qemu_gluster_reopen_prepare, |
1426 | .bdrv_reopen_commit = qemu_gluster_reopen_commit, | |
1427 | .bdrv_reopen_abort = qemu_gluster_reopen_abort, | |
8d6d89cb | 1428 | .bdrv_close = qemu_gluster_close, |
c282e1fd | 1429 | .bdrv_create = qemu_gluster_create, |
8d6d89cb BR |
1430 | .bdrv_getlength = qemu_gluster_getlength, |
1431 | .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size, | |
42ec24e2 | 1432 | .bdrv_truncate = qemu_gluster_truncate, |
15744b0b BR |
1433 | .bdrv_co_readv = qemu_gluster_co_readv, |
1434 | .bdrv_co_writev = qemu_gluster_co_writev, | |
1435 | .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk, | |
8ab6feec | 1436 | .bdrv_has_zero_init = qemu_gluster_has_zero_init, |
0c14fb47 | 1437 | #ifdef CONFIG_GLUSTERFS_DISCARD |
1014170b | 1438 | .bdrv_co_pdiscard = qemu_gluster_co_pdiscard, |
7c815372 BR |
1439 | #endif |
1440 | #ifdef CONFIG_GLUSTERFS_ZEROFILL | |
e88a36eb | 1441 | .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes, |
0c14fb47 | 1442 | #endif |
947eb203 | 1443 | .bdrv_co_get_block_status = qemu_gluster_co_get_block_status, |
90c772de | 1444 | .create_opts = &qemu_gluster_create_opts, |
8d6d89cb BR |
1445 | }; |
1446 | ||
1447 | static void bdrv_gluster_init(void) | |
1448 | { | |
1449 | bdrv_register(&bdrv_gluster_rdma); | |
1450 | bdrv_register(&bdrv_gluster_unix); | |
1451 | bdrv_register(&bdrv_gluster_tcp); | |
1452 | bdrv_register(&bdrv_gluster); | |
1453 | } | |
1454 | ||
1455 | block_init(bdrv_gluster_init); |