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