Update version for v5.0.0-rc1 release
[qemu.git] / block / gluster.c
blob0aa1f2cda482c42f66577ee7beb85093bb22dfbb
1 /*
2 * GlusterFS backend for QEMU
4 * Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com>
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.
9 */
11 #include "qemu/osdep.h"
12 #include "qemu/units.h"
13 #include <glusterfs/api/glfs.h>
14 #include "block/block_int.h"
15 #include "block/qdict.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qemu/uri.h"
20 #include "qemu/error-report.h"
21 #include "qemu/module.h"
22 #include "qemu/option.h"
23 #include "qemu/cutils.h"
25 #ifdef CONFIG_GLUSTERFS_FTRUNCATE_HAS_STAT
26 # define glfs_ftruncate(fd, offset) glfs_ftruncate(fd, offset, NULL, NULL)
27 #endif
29 #define GLUSTER_OPT_FILENAME "filename"
30 #define GLUSTER_OPT_VOLUME "volume"
31 #define GLUSTER_OPT_PATH "path"
32 #define GLUSTER_OPT_TYPE "type"
33 #define GLUSTER_OPT_SERVER_PATTERN "server."
34 #define GLUSTER_OPT_HOST "host"
35 #define GLUSTER_OPT_PORT "port"
36 #define GLUSTER_OPT_TO "to"
37 #define GLUSTER_OPT_IPV4 "ipv4"
38 #define GLUSTER_OPT_IPV6 "ipv6"
39 #define GLUSTER_OPT_SOCKET "socket"
40 #define GLUSTER_OPT_DEBUG "debug"
41 #define GLUSTER_DEFAULT_PORT 24007
42 #define GLUSTER_DEBUG_DEFAULT 4
43 #define GLUSTER_DEBUG_MAX 9
44 #define GLUSTER_OPT_LOGFILE "logfile"
45 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
47 * Several versions of GlusterFS (3.12? -> 6.0.1) fail when the transfer size
48 * is greater or equal to 1024 MiB, so we are limiting the transfer size to 512
49 * MiB to avoid this rare issue.
51 #define GLUSTER_MAX_TRANSFER (512 * MiB)
53 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
55 typedef struct GlusterAIOCB {
56 int64_t size;
57 int ret;
58 Coroutine *coroutine;
59 AioContext *aio_context;
60 } GlusterAIOCB;
62 typedef struct BDRVGlusterState {
63 struct glfs *glfs;
64 struct glfs_fd *fd;
65 char *logfile;
66 bool supports_seek_data;
67 int debug;
68 } BDRVGlusterState;
70 typedef struct BDRVGlusterReopenState {
71 struct glfs *glfs;
72 struct glfs_fd *fd;
73 } BDRVGlusterReopenState;
76 typedef struct GlfsPreopened {
77 char *volume;
78 glfs_t *fs;
79 int ref;
80 } GlfsPreopened;
82 typedef struct ListElement {
83 QLIST_ENTRY(ListElement) list;
84 GlfsPreopened saved;
85 } ListElement;
87 static QLIST_HEAD(, ListElement) glfs_list;
89 static QemuOptsList qemu_gluster_create_opts = {
90 .name = "qemu-gluster-create-opts",
91 .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
92 .desc = {
94 .name = BLOCK_OPT_SIZE,
95 .type = QEMU_OPT_SIZE,
96 .help = "Virtual disk size"
99 .name = BLOCK_OPT_PREALLOC,
100 .type = QEMU_OPT_STRING,
101 .help = "Preallocation mode (allowed values: off"
102 #ifdef CONFIG_GLUSTERFS_FALLOCATE
103 ", falloc"
104 #endif
105 #ifdef CONFIG_GLUSTERFS_ZEROFILL
106 ", full"
107 #endif
111 .name = GLUSTER_OPT_DEBUG,
112 .type = QEMU_OPT_NUMBER,
113 .help = "Gluster log level, valid range is 0-9",
116 .name = GLUSTER_OPT_LOGFILE,
117 .type = QEMU_OPT_STRING,
118 .help = "Logfile path of libgfapi",
120 { /* end of list */ }
124 static QemuOptsList runtime_opts = {
125 .name = "gluster",
126 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
127 .desc = {
129 .name = GLUSTER_OPT_FILENAME,
130 .type = QEMU_OPT_STRING,
131 .help = "URL to the gluster image",
134 .name = GLUSTER_OPT_DEBUG,
135 .type = QEMU_OPT_NUMBER,
136 .help = "Gluster log level, valid range is 0-9",
139 .name = GLUSTER_OPT_LOGFILE,
140 .type = QEMU_OPT_STRING,
141 .help = "Logfile path of libgfapi",
143 { /* end of list */ }
147 static QemuOptsList runtime_json_opts = {
148 .name = "gluster_json",
149 .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head),
150 .desc = {
152 .name = GLUSTER_OPT_VOLUME,
153 .type = QEMU_OPT_STRING,
154 .help = "name of gluster volume where VM image resides",
157 .name = GLUSTER_OPT_PATH,
158 .type = QEMU_OPT_STRING,
159 .help = "absolute path to image file in gluster volume",
162 .name = GLUSTER_OPT_DEBUG,
163 .type = QEMU_OPT_NUMBER,
164 .help = "Gluster log level, valid range is 0-9",
166 { /* end of list */ }
170 static QemuOptsList runtime_type_opts = {
171 .name = "gluster_type",
172 .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head),
173 .desc = {
175 .name = GLUSTER_OPT_TYPE,
176 .type = QEMU_OPT_STRING,
177 .help = "inet|unix",
179 { /* end of list */ }
183 static QemuOptsList runtime_unix_opts = {
184 .name = "gluster_unix",
185 .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head),
186 .desc = {
188 .name = GLUSTER_OPT_SOCKET,
189 .type = QEMU_OPT_STRING,
190 .help = "socket file path (legacy)",
193 .name = GLUSTER_OPT_PATH,
194 .type = QEMU_OPT_STRING,
195 .help = "socket file path (QAPI)",
197 { /* end of list */ }
201 static QemuOptsList runtime_inet_opts = {
202 .name = "gluster_inet",
203 .head = QTAILQ_HEAD_INITIALIZER(runtime_inet_opts.head),
204 .desc = {
206 .name = GLUSTER_OPT_TYPE,
207 .type = QEMU_OPT_STRING,
208 .help = "inet|unix",
211 .name = GLUSTER_OPT_HOST,
212 .type = QEMU_OPT_STRING,
213 .help = "host address (hostname/ipv4/ipv6 addresses)",
216 .name = GLUSTER_OPT_PORT,
217 .type = QEMU_OPT_STRING,
218 .help = "port number on which glusterd is listening (default 24007)",
221 .name = "to",
222 .type = QEMU_OPT_NUMBER,
223 .help = "max port number, not supported by gluster",
226 .name = "ipv4",
227 .type = QEMU_OPT_BOOL,
228 .help = "ipv4 bool value, not supported by gluster",
231 .name = "ipv6",
232 .type = QEMU_OPT_BOOL,
233 .help = "ipv6 bool value, not supported by gluster",
235 { /* end of list */ }
239 static void glfs_set_preopened(const char *volume, glfs_t *fs)
241 ListElement *entry = NULL;
243 entry = g_new(ListElement, 1);
245 entry->saved.volume = g_strdup(volume);
247 entry->saved.fs = fs;
248 entry->saved.ref = 1;
250 QLIST_INSERT_HEAD(&glfs_list, entry, list);
253 static glfs_t *glfs_find_preopened(const char *volume)
255 ListElement *entry = NULL;
257 QLIST_FOREACH(entry, &glfs_list, list) {
258 if (strcmp(entry->saved.volume, volume) == 0) {
259 entry->saved.ref++;
260 return entry->saved.fs;
264 return NULL;
267 static void glfs_clear_preopened(glfs_t *fs)
269 ListElement *entry = NULL;
270 ListElement *next;
272 if (fs == NULL) {
273 return;
276 QLIST_FOREACH_SAFE(entry, &glfs_list, list, next) {
277 if (entry->saved.fs == fs) {
278 if (--entry->saved.ref) {
279 return;
282 QLIST_REMOVE(entry, list);
284 glfs_fini(entry->saved.fs);
285 g_free(entry->saved.volume);
286 g_free(entry);
291 static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path)
293 char *p, *q;
295 if (!path) {
296 return -EINVAL;
299 /* volume */
300 p = q = path + strspn(path, "/");
301 p += strcspn(p, "/");
302 if (*p == '\0') {
303 return -EINVAL;
305 gconf->volume = g_strndup(q, p - q);
307 /* path */
308 p += strspn(p, "/");
309 if (*p == '\0') {
310 return -EINVAL;
312 gconf->path = g_strdup(p);
313 return 0;
317 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
319 * 'gluster' is the protocol.
321 * 'transport' specifies the transport type used to connect to gluster
322 * management daemon (glusterd). Valid transport types are
323 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
325 * 'host' specifies the host where the volume file specification for
326 * the given volume resides. This can be either hostname or ipv4 address.
327 * If transport type is 'unix', then 'host' field should not be specified.
328 * The 'socket' field needs to be populated with the path to unix domain
329 * socket.
331 * 'port' is the port number on which glusterd is listening. This is optional
332 * and if not specified, QEMU will send 0 which will make gluster to use the
333 * default port. If the transport type is unix, then 'port' should not be
334 * specified.
336 * 'volume' is the name of the gluster volume which contains the VM image.
338 * 'path' is the path to the actual VM image that resides on gluster volume.
340 * Examples:
342 * file=gluster://1.2.3.4/testvol/a.img
343 * file=gluster+tcp://1.2.3.4/testvol/a.img
344 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
345 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
346 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
348 static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
349 const char *filename)
351 SocketAddress *gsconf;
352 URI *uri;
353 QueryParams *qp = NULL;
354 bool is_unix = false;
355 int ret = 0;
357 uri = uri_parse(filename);
358 if (!uri) {
359 return -EINVAL;
362 gconf->server = g_new0(SocketAddressList, 1);
363 gconf->server->value = gsconf = g_new0(SocketAddress, 1);
365 /* transport */
366 if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
367 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
368 } else if (!strcmp(uri->scheme, "gluster+tcp")) {
369 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
370 } else if (!strcmp(uri->scheme, "gluster+unix")) {
371 gsconf->type = SOCKET_ADDRESS_TYPE_UNIX;
372 is_unix = true;
373 } else if (!strcmp(uri->scheme, "gluster+rdma")) {
374 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
375 warn_report("rdma feature is not supported, falling back to tcp");
376 } else {
377 ret = -EINVAL;
378 goto out;
381 ret = parse_volume_options(gconf, uri->path);
382 if (ret < 0) {
383 goto out;
386 qp = query_params_parse(uri->query);
387 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
388 ret = -EINVAL;
389 goto out;
392 if (is_unix) {
393 if (uri->server || uri->port) {
394 ret = -EINVAL;
395 goto out;
397 if (strcmp(qp->p[0].name, "socket")) {
398 ret = -EINVAL;
399 goto out;
401 gsconf->u.q_unix.path = g_strdup(qp->p[0].value);
402 } else {
403 gsconf->u.inet.host = g_strdup(uri->server ? uri->server : "localhost");
404 if (uri->port) {
405 gsconf->u.inet.port = g_strdup_printf("%d", uri->port);
406 } else {
407 gsconf->u.inet.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT);
411 out:
412 if (qp) {
413 query_params_free(qp);
415 uri_free(uri);
416 return ret;
419 static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
420 Error **errp)
422 struct glfs *glfs;
423 int ret;
424 int old_errno;
425 SocketAddressList *server;
426 unsigned long long port;
428 glfs = glfs_find_preopened(gconf->volume);
429 if (glfs) {
430 return glfs;
433 glfs = glfs_new(gconf->volume);
434 if (!glfs) {
435 goto out;
438 glfs_set_preopened(gconf->volume, glfs);
440 for (server = gconf->server; server; server = server->next) {
441 switch (server->value->type) {
442 case SOCKET_ADDRESS_TYPE_UNIX:
443 ret = glfs_set_volfile_server(glfs, "unix",
444 server->value->u.q_unix.path, 0);
445 break;
446 case SOCKET_ADDRESS_TYPE_INET:
447 if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 ||
448 port > 65535) {
449 error_setg(errp, "'%s' is not a valid port number",
450 server->value->u.inet.port);
451 errno = EINVAL;
452 goto out;
454 ret = glfs_set_volfile_server(glfs, "tcp",
455 server->value->u.inet.host,
456 (int)port);
457 break;
458 case SOCKET_ADDRESS_TYPE_VSOCK:
459 case SOCKET_ADDRESS_TYPE_FD:
460 default:
461 abort();
464 if (ret < 0) {
465 goto out;
469 ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug);
470 if (ret < 0) {
471 goto out;
474 ret = glfs_init(glfs);
475 if (ret) {
476 error_setg(errp, "Gluster connection for volume %s, path %s failed"
477 " to connect", gconf->volume, gconf->path);
478 for (server = gconf->server; server; server = server->next) {
479 if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) {
480 error_append_hint(errp, "hint: failed on socket %s ",
481 server->value->u.q_unix.path);
482 } else {
483 error_append_hint(errp, "hint: failed on host %s and port %s ",
484 server->value->u.inet.host,
485 server->value->u.inet.port);
489 error_append_hint(errp, "Please refer to gluster logs for more info\n");
491 /* glfs_init sometimes doesn't set errno although docs suggest that */
492 if (errno == 0) {
493 errno = EINVAL;
496 goto out;
498 return glfs;
500 out:
501 if (glfs) {
502 old_errno = errno;
503 glfs_clear_preopened(glfs);
504 errno = old_errno;
506 return NULL;
510 * Convert the json formatted command line into qapi.
512 static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
513 QDict *options, Error **errp)
515 QemuOpts *opts;
516 SocketAddress *gsconf = NULL;
517 SocketAddressList *curr = NULL;
518 QDict *backing_options = NULL;
519 Error *local_err = NULL;
520 char *str = NULL;
521 const char *ptr;
522 int i, type, num_servers;
524 /* create opts info from runtime_json_opts list */
525 opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort);
526 qemu_opts_absorb_qdict(opts, options, &local_err);
527 if (local_err) {
528 goto out;
531 num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN);
532 if (num_servers < 1) {
533 error_setg(&local_err, QERR_MISSING_PARAMETER, "server");
534 goto out;
537 ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME);
538 if (!ptr) {
539 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME);
540 goto out;
542 gconf->volume = g_strdup(ptr);
544 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
545 if (!ptr) {
546 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH);
547 goto out;
549 gconf->path = g_strdup(ptr);
550 qemu_opts_del(opts);
552 for (i = 0; i < num_servers; i++) {
553 str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
554 qdict_extract_subqdict(options, &backing_options, str);
556 /* create opts info from runtime_type_opts list */
557 opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort);
558 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
559 if (local_err) {
560 goto out;
563 ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE);
564 if (!ptr) {
565 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE);
566 error_append_hint(&local_err, GERR_INDEX_HINT, i);
567 goto out;
570 gsconf = g_new0(SocketAddress, 1);
571 if (!strcmp(ptr, "tcp")) {
572 ptr = "inet"; /* accept legacy "tcp" */
574 type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
575 if (type != SOCKET_ADDRESS_TYPE_INET
576 && type != SOCKET_ADDRESS_TYPE_UNIX) {
577 error_setg(&local_err,
578 "Parameter '%s' may be 'inet' or 'unix'",
579 GLUSTER_OPT_TYPE);
580 error_append_hint(&local_err, GERR_INDEX_HINT, i);
581 goto out;
583 gsconf->type = type;
584 qemu_opts_del(opts);
586 if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) {
587 /* create opts info from runtime_inet_opts list */
588 opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort);
589 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
590 if (local_err) {
591 goto out;
594 ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST);
595 if (!ptr) {
596 error_setg(&local_err, QERR_MISSING_PARAMETER,
597 GLUSTER_OPT_HOST);
598 error_append_hint(&local_err, GERR_INDEX_HINT, i);
599 goto out;
601 gsconf->u.inet.host = g_strdup(ptr);
602 ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT);
603 if (!ptr) {
604 error_setg(&local_err, QERR_MISSING_PARAMETER,
605 GLUSTER_OPT_PORT);
606 error_append_hint(&local_err, GERR_INDEX_HINT, i);
607 goto out;
609 gsconf->u.inet.port = g_strdup(ptr);
611 /* defend for unsupported fields in InetSocketAddress,
612 * i.e. @ipv4, @ipv6 and @to
614 ptr = qemu_opt_get(opts, GLUSTER_OPT_TO);
615 if (ptr) {
616 gsconf->u.inet.has_to = true;
618 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4);
619 if (ptr) {
620 gsconf->u.inet.has_ipv4 = true;
622 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6);
623 if (ptr) {
624 gsconf->u.inet.has_ipv6 = true;
626 if (gsconf->u.inet.has_to) {
627 error_setg(&local_err, "Parameter 'to' not supported");
628 goto out;
630 if (gsconf->u.inet.has_ipv4 || gsconf->u.inet.has_ipv6) {
631 error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported");
632 goto out;
634 qemu_opts_del(opts);
635 } else {
636 /* create opts info from runtime_unix_opts list */
637 opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort);
638 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
639 if (local_err) {
640 goto out;
643 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
644 if (!ptr) {
645 ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET);
646 } else if (qemu_opt_get(opts, GLUSTER_OPT_SOCKET)) {
647 error_setg(&local_err,
648 "Conflicting parameters 'path' and 'socket'");
649 error_append_hint(&local_err, GERR_INDEX_HINT, i);
650 goto out;
652 if (!ptr) {
653 error_setg(&local_err, QERR_MISSING_PARAMETER,
654 GLUSTER_OPT_PATH);
655 error_append_hint(&local_err, GERR_INDEX_HINT, i);
656 goto out;
658 gsconf->u.q_unix.path = g_strdup(ptr);
659 qemu_opts_del(opts);
662 if (gconf->server == NULL) {
663 gconf->server = g_new0(SocketAddressList, 1);
664 gconf->server->value = gsconf;
665 curr = gconf->server;
666 } else {
667 curr->next = g_new0(SocketAddressList, 1);
668 curr->next->value = gsconf;
669 curr = curr->next;
671 gsconf = NULL;
673 qobject_unref(backing_options);
674 backing_options = NULL;
675 g_free(str);
676 str = NULL;
679 return 0;
681 out:
682 error_propagate(errp, local_err);
683 qapi_free_SocketAddress(gsconf);
684 qemu_opts_del(opts);
685 g_free(str);
686 qobject_unref(backing_options);
687 errno = EINVAL;
688 return -errno;
691 /* Converts options given in @filename and the @options QDict into the QAPI
692 * object @gconf. */
693 static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
694 const char *filename,
695 QDict *options, Error **errp)
697 int ret;
698 if (filename) {
699 ret = qemu_gluster_parse_uri(gconf, filename);
700 if (ret < 0) {
701 error_setg(errp, "invalid URI %s", filename);
702 error_append_hint(errp, "Usage: file=gluster[+transport]://"
703 "[host[:port]]volume/path[?socket=...]"
704 "[,file.debug=N]"
705 "[,file.logfile=/path/filename.log]\n");
706 return ret;
708 } else {
709 ret = qemu_gluster_parse_json(gconf, options, errp);
710 if (ret < 0) {
711 error_append_hint(errp, "Usage: "
712 "-drive driver=qcow2,file.driver=gluster,"
713 "file.volume=testvol,file.path=/path/a.qcow2"
714 "[,file.debug=9]"
715 "[,file.logfile=/path/filename.log],"
716 "file.server.0.type=inet,"
717 "file.server.0.host=1.2.3.4,"
718 "file.server.0.port=24007,"
719 "file.server.1.transport=unix,"
720 "file.server.1.path=/var/run/glusterd.socket ..."
721 "\n");
722 return ret;
726 return 0;
729 static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
730 const char *filename,
731 QDict *options, Error **errp)
733 int ret;
735 ret = qemu_gluster_parse(gconf, filename, options, errp);
736 if (ret < 0) {
737 errno = -ret;
738 return NULL;
741 return qemu_gluster_glfs_init(gconf, errp);
745 * AIO callback routine called from GlusterFS thread.
747 static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret,
748 #ifdef CONFIG_GLUSTERFS_IOCB_HAS_STAT
749 struct glfs_stat *pre, struct glfs_stat *post,
750 #endif
751 void *arg)
753 GlusterAIOCB *acb = (GlusterAIOCB *)arg;
755 if (!ret || ret == acb->size) {
756 acb->ret = 0; /* Success */
757 } else if (ret < 0) {
758 acb->ret = -errno; /* Read/Write failed */
759 } else {
760 acb->ret = -EIO; /* Partial read/write - fail it */
763 aio_co_schedule(acb->aio_context, acb->coroutine);
766 static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
768 assert(open_flags != NULL);
770 *open_flags |= O_BINARY;
772 if (bdrv_flags & BDRV_O_RDWR) {
773 *open_flags |= O_RDWR;
774 } else {
775 *open_flags |= O_RDONLY;
778 if ((bdrv_flags & BDRV_O_NOCACHE)) {
779 *open_flags |= O_DIRECT;
784 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
785 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
786 * - Corrected versions return -1 and set errno to EINVAL.
787 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
788 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
790 static bool qemu_gluster_test_seek(struct glfs_fd *fd)
792 off_t ret = 0;
794 #if defined SEEK_HOLE && defined SEEK_DATA
795 off_t eof;
797 eof = glfs_lseek(fd, 0, SEEK_END);
798 if (eof < 0) {
799 /* this should never occur */
800 return false;
803 /* this should always fail with ENXIO if SEEK_DATA is supported */
804 ret = glfs_lseek(fd, eof, SEEK_DATA);
805 #endif
807 return (ret < 0) && (errno == ENXIO);
810 static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
811 int bdrv_flags, Error **errp)
813 BDRVGlusterState *s = bs->opaque;
814 int open_flags = 0;
815 int ret = 0;
816 BlockdevOptionsGluster *gconf = NULL;
817 QemuOpts *opts;
818 Error *local_err = NULL;
819 const char *filename, *logfile;
821 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
822 qemu_opts_absorb_qdict(opts, options, &local_err);
823 if (local_err) {
824 error_propagate(errp, local_err);
825 ret = -EINVAL;
826 goto out;
829 filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME);
831 s->debug = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG,
832 GLUSTER_DEBUG_DEFAULT);
833 if (s->debug < 0) {
834 s->debug = 0;
835 } else if (s->debug > GLUSTER_DEBUG_MAX) {
836 s->debug = GLUSTER_DEBUG_MAX;
839 gconf = g_new0(BlockdevOptionsGluster, 1);
840 gconf->debug = s->debug;
841 gconf->has_debug = true;
843 logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE);
844 s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT);
846 gconf->logfile = g_strdup(s->logfile);
847 gconf->has_logfile = true;
849 s->glfs = qemu_gluster_init(gconf, filename, options, errp);
850 if (!s->glfs) {
851 ret = -errno;
852 goto out;
855 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
856 /* Without this, if fsync fails for a recoverable reason (for instance,
857 * ENOSPC), gluster will dump its cache, preventing retries. This means
858 * almost certain data loss. Not all gluster versions support the
859 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
860 * discover during runtime if it is supported (this api returns success for
861 * unknown key/value pairs) */
862 ret = glfs_set_xlator_option(s->glfs, "*-write-behind",
863 "resync-failed-syncs-after-fsync",
864 "on");
865 if (ret < 0) {
866 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
867 ret = -errno;
868 goto out;
870 #endif
872 qemu_gluster_parse_flags(bdrv_flags, &open_flags);
874 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
875 ret = s->fd ? 0 : -errno;
877 if (ret == -EACCES || ret == -EROFS) {
878 /* Try to degrade to read-only, but if it doesn't work, still use the
879 * normal error message. */
880 if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) {
881 open_flags = (open_flags & ~O_RDWR) | O_RDONLY;
882 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
883 ret = s->fd ? 0 : -errno;
887 s->supports_seek_data = qemu_gluster_test_seek(s->fd);
889 out:
890 qemu_opts_del(opts);
891 qapi_free_BlockdevOptionsGluster(gconf);
892 if (!ret) {
893 return ret;
895 g_free(s->logfile);
896 if (s->fd) {
897 glfs_close(s->fd);
900 glfs_clear_preopened(s->glfs);
902 return ret;
905 static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
907 bs->bl.max_transfer = GLUSTER_MAX_TRANSFER;
910 static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
911 BlockReopenQueue *queue, Error **errp)
913 int ret = 0;
914 BDRVGlusterState *s;
915 BDRVGlusterReopenState *reop_s;
916 BlockdevOptionsGluster *gconf;
917 int open_flags = 0;
919 assert(state != NULL);
920 assert(state->bs != NULL);
922 s = state->bs->opaque;
924 state->opaque = g_new0(BDRVGlusterReopenState, 1);
925 reop_s = state->opaque;
927 qemu_gluster_parse_flags(state->flags, &open_flags);
929 gconf = g_new0(BlockdevOptionsGluster, 1);
930 gconf->debug = s->debug;
931 gconf->has_debug = true;
932 gconf->logfile = g_strdup(s->logfile);
933 gconf->has_logfile = true;
936 * If 'state->bs->exact_filename' is empty, 'state->options' should contain
937 * the JSON parameters already parsed.
939 if (state->bs->exact_filename[0] != '\0') {
940 reop_s->glfs = qemu_gluster_init(gconf, state->bs->exact_filename, NULL,
941 errp);
942 } else {
943 reop_s->glfs = qemu_gluster_init(gconf, NULL, state->options, errp);
945 if (reop_s->glfs == NULL) {
946 ret = -errno;
947 goto exit;
950 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
951 ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind",
952 "resync-failed-syncs-after-fsync", "on");
953 if (ret < 0) {
954 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
955 ret = -errno;
956 goto exit;
958 #endif
960 reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags);
961 if (reop_s->fd == NULL) {
962 /* reops->glfs will be cleaned up in _abort */
963 ret = -errno;
964 goto exit;
967 exit:
968 /* state->opaque will be freed in either the _abort or _commit */
969 qapi_free_BlockdevOptionsGluster(gconf);
970 return ret;
973 static void qemu_gluster_reopen_commit(BDRVReopenState *state)
975 BDRVGlusterReopenState *reop_s = state->opaque;
976 BDRVGlusterState *s = state->bs->opaque;
979 /* close the old */
980 if (s->fd) {
981 glfs_close(s->fd);
984 glfs_clear_preopened(s->glfs);
986 /* use the newly opened image / connection */
987 s->fd = reop_s->fd;
988 s->glfs = reop_s->glfs;
990 g_free(state->opaque);
991 state->opaque = NULL;
993 return;
997 static void qemu_gluster_reopen_abort(BDRVReopenState *state)
999 BDRVGlusterReopenState *reop_s = state->opaque;
1001 if (reop_s == NULL) {
1002 return;
1005 if (reop_s->fd) {
1006 glfs_close(reop_s->fd);
1009 glfs_clear_preopened(reop_s->glfs);
1011 g_free(state->opaque);
1012 state->opaque = NULL;
1014 return;
1017 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1018 static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
1019 int64_t offset,
1020 int size,
1021 BdrvRequestFlags flags)
1023 int ret;
1024 GlusterAIOCB acb;
1025 BDRVGlusterState *s = bs->opaque;
1027 acb.size = size;
1028 acb.ret = 0;
1029 acb.coroutine = qemu_coroutine_self();
1030 acb.aio_context = bdrv_get_aio_context(bs);
1032 ret = glfs_zerofill_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
1033 if (ret < 0) {
1034 return -errno;
1037 qemu_coroutine_yield();
1038 return acb.ret;
1040 #endif
1042 static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
1043 PreallocMode prealloc, Error **errp)
1045 int64_t current_length;
1047 current_length = glfs_lseek(fd, 0, SEEK_END);
1048 if (current_length < 0) {
1049 error_setg_errno(errp, errno, "Failed to determine current size");
1050 return -errno;
1053 if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
1054 error_setg(errp, "Cannot use preallocation for shrinking files");
1055 return -ENOTSUP;
1058 if (current_length == offset) {
1059 return 0;
1062 switch (prealloc) {
1063 #ifdef CONFIG_GLUSTERFS_FALLOCATE
1064 case PREALLOC_MODE_FALLOC:
1065 if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
1066 error_setg_errno(errp, errno, "Could not preallocate data");
1067 return -errno;
1069 break;
1070 #endif /* CONFIG_GLUSTERFS_FALLOCATE */
1071 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1072 case PREALLOC_MODE_FULL:
1073 if (glfs_ftruncate(fd, offset)) {
1074 error_setg_errno(errp, errno, "Could not resize file");
1075 return -errno;
1077 if (glfs_zerofill(fd, current_length, offset - current_length)) {
1078 error_setg_errno(errp, errno, "Could not zerofill the new area");
1079 return -errno;
1081 break;
1082 #endif /* CONFIG_GLUSTERFS_ZEROFILL */
1083 case PREALLOC_MODE_OFF:
1084 if (glfs_ftruncate(fd, offset)) {
1085 error_setg_errno(errp, errno, "Could not resize file");
1086 return -errno;
1088 break;
1089 default:
1090 error_setg(errp, "Unsupported preallocation mode: %s",
1091 PreallocMode_str(prealloc));
1092 return -EINVAL;
1095 return 0;
1098 static int qemu_gluster_co_create(BlockdevCreateOptions *options,
1099 Error **errp)
1101 BlockdevCreateOptionsGluster *opts = &options->u.gluster;
1102 struct glfs *glfs;
1103 struct glfs_fd *fd = NULL;
1104 int ret = 0;
1106 assert(options->driver == BLOCKDEV_DRIVER_GLUSTER);
1108 glfs = qemu_gluster_glfs_init(opts->location, errp);
1109 if (!glfs) {
1110 ret = -errno;
1111 goto out;
1114 fd = glfs_creat(glfs, opts->location->path,
1115 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
1116 if (!fd) {
1117 ret = -errno;
1118 goto out;
1121 ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp);
1123 out:
1124 if (fd) {
1125 if (glfs_close(fd) != 0 && ret == 0) {
1126 ret = -errno;
1129 glfs_clear_preopened(glfs);
1130 return ret;
1133 static int coroutine_fn qemu_gluster_co_create_opts(BlockDriver *drv,
1134 const char *filename,
1135 QemuOpts *opts,
1136 Error **errp)
1138 BlockdevCreateOptions *options;
1139 BlockdevCreateOptionsGluster *gopts;
1140 BlockdevOptionsGluster *gconf;
1141 char *tmp = NULL;
1142 Error *local_err = NULL;
1143 int ret;
1145 options = g_new0(BlockdevCreateOptions, 1);
1146 options->driver = BLOCKDEV_DRIVER_GLUSTER;
1147 gopts = &options->u.gluster;
1149 gconf = g_new0(BlockdevOptionsGluster, 1);
1150 gopts->location = gconf;
1152 gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1153 BDRV_SECTOR_SIZE);
1155 tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1156 gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp,
1157 PREALLOC_MODE_OFF, &local_err);
1158 g_free(tmp);
1159 if (local_err) {
1160 error_propagate(errp, local_err);
1161 ret = -EINVAL;
1162 goto fail;
1165 gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
1166 GLUSTER_DEBUG_DEFAULT);
1167 if (gconf->debug < 0) {
1168 gconf->debug = 0;
1169 } else if (gconf->debug > GLUSTER_DEBUG_MAX) {
1170 gconf->debug = GLUSTER_DEBUG_MAX;
1172 gconf->has_debug = true;
1174 gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE);
1175 if (!gconf->logfile) {
1176 gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT);
1178 gconf->has_logfile = true;
1180 ret = qemu_gluster_parse(gconf, filename, NULL, errp);
1181 if (ret < 0) {
1182 goto fail;
1185 ret = qemu_gluster_co_create(options, errp);
1186 if (ret < 0) {
1187 goto fail;
1190 ret = 0;
1191 fail:
1192 qapi_free_BlockdevCreateOptions(options);
1193 return ret;
1196 static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
1197 int64_t sector_num, int nb_sectors,
1198 QEMUIOVector *qiov, int write)
1200 int ret;
1201 GlusterAIOCB acb;
1202 BDRVGlusterState *s = bs->opaque;
1203 size_t size = nb_sectors * BDRV_SECTOR_SIZE;
1204 off_t offset = sector_num * BDRV_SECTOR_SIZE;
1206 acb.size = size;
1207 acb.ret = 0;
1208 acb.coroutine = qemu_coroutine_self();
1209 acb.aio_context = bdrv_get_aio_context(bs);
1211 if (write) {
1212 ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1213 gluster_finish_aiocb, &acb);
1214 } else {
1215 ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1216 gluster_finish_aiocb, &acb);
1219 if (ret < 0) {
1220 return -errno;
1223 qemu_coroutine_yield();
1224 return acb.ret;
1227 static coroutine_fn int qemu_gluster_co_truncate(BlockDriverState *bs,
1228 int64_t offset,
1229 bool exact,
1230 PreallocMode prealloc,
1231 Error **errp)
1233 BDRVGlusterState *s = bs->opaque;
1234 return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
1237 static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
1238 int64_t sector_num,
1239 int nb_sectors,
1240 QEMUIOVector *qiov)
1242 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
1245 static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
1246 int64_t sector_num,
1247 int nb_sectors,
1248 QEMUIOVector *qiov,
1249 int flags)
1251 assert(!flags);
1252 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
1255 static void qemu_gluster_close(BlockDriverState *bs)
1257 BDRVGlusterState *s = bs->opaque;
1259 g_free(s->logfile);
1260 if (s->fd) {
1261 glfs_close(s->fd);
1262 s->fd = NULL;
1264 glfs_clear_preopened(s->glfs);
1267 static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
1269 int ret;
1270 GlusterAIOCB acb;
1271 BDRVGlusterState *s = bs->opaque;
1273 acb.size = 0;
1274 acb.ret = 0;
1275 acb.coroutine = qemu_coroutine_self();
1276 acb.aio_context = bdrv_get_aio_context(bs);
1278 ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb);
1279 if (ret < 0) {
1280 ret = -errno;
1281 goto error;
1284 qemu_coroutine_yield();
1285 if (acb.ret < 0) {
1286 ret = acb.ret;
1287 goto error;
1290 return acb.ret;
1292 error:
1293 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1294 * after a fsync failure, so we have no way of allowing the guest to safely
1295 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1296 * either, but will invalidate the fd on error, so this is again our only
1297 * option.
1299 * The 'resync-failed-syncs-after-fsync' xlator option for the
1300 * write-behind cache will cause later gluster versions to retain its
1301 * cache after error, so long as the fd remains open. However, we
1302 * currently have no way of knowing if this option is supported.
1304 * TODO: Once gluster provides a way for us to determine if the option
1305 * is supported, bypass the closure and setting drv to NULL. */
1306 qemu_gluster_close(bs);
1307 bs->drv = NULL;
1308 return ret;
1311 #ifdef CONFIG_GLUSTERFS_DISCARD
1312 static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs,
1313 int64_t offset, int size)
1315 int ret;
1316 GlusterAIOCB acb;
1317 BDRVGlusterState *s = bs->opaque;
1319 acb.size = 0;
1320 acb.ret = 0;
1321 acb.coroutine = qemu_coroutine_self();
1322 acb.aio_context = bdrv_get_aio_context(bs);
1324 ret = glfs_discard_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
1325 if (ret < 0) {
1326 return -errno;
1329 qemu_coroutine_yield();
1330 return acb.ret;
1332 #endif
1334 static int64_t qemu_gluster_getlength(BlockDriverState *bs)
1336 BDRVGlusterState *s = bs->opaque;
1337 int64_t ret;
1339 ret = glfs_lseek(s->fd, 0, SEEK_END);
1340 if (ret < 0) {
1341 return -errno;
1342 } else {
1343 return ret;
1347 static int64_t qemu_gluster_allocated_file_size(BlockDriverState *bs)
1349 BDRVGlusterState *s = bs->opaque;
1350 struct stat st;
1351 int ret;
1353 ret = glfs_fstat(s->fd, &st);
1354 if (ret < 0) {
1355 return -errno;
1356 } else {
1357 return st.st_blocks * 512;
1361 static int qemu_gluster_has_zero_init(BlockDriverState *bs)
1363 /* GlusterFS volume could be backed by a block device */
1364 return 0;
1368 * Find allocation range in @bs around offset @start.
1369 * May change underlying file descriptor's file offset.
1370 * If @start is not in a hole, store @start in @data, and the
1371 * beginning of the next hole in @hole, and return 0.
1372 * If @start is in a non-trailing hole, store @start in @hole and the
1373 * beginning of the next non-hole in @data, and return 0.
1374 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1375 * If we can't find out, return a negative errno other than -ENXIO.
1377 * (Shamefully copied from file-posix.c, only minuscule adaptions.)
1379 static int find_allocation(BlockDriverState *bs, off_t start,
1380 off_t *data, off_t *hole)
1382 BDRVGlusterState *s = bs->opaque;
1384 if (!s->supports_seek_data) {
1385 goto exit;
1388 #if defined SEEK_HOLE && defined SEEK_DATA
1389 off_t offs;
1392 * SEEK_DATA cases:
1393 * D1. offs == start: start is in data
1394 * D2. offs > start: start is in a hole, next data at offs
1395 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1396 * or start is beyond EOF
1397 * If the latter happens, the file has been truncated behind
1398 * our back since we opened it. All bets are off then.
1399 * Treating like a trailing hole is simplest.
1400 * D4. offs < 0, errno != ENXIO: we learned nothing
1402 offs = glfs_lseek(s->fd, start, SEEK_DATA);
1403 if (offs < 0) {
1404 return -errno; /* D3 or D4 */
1407 if (offs < start) {
1408 /* This is not a valid return by lseek(). We are safe to just return
1409 * -EIO in this case, and we'll treat it like D4. Unfortunately some
1410 * versions of gluster server will return offs < start, so an assert
1411 * here will unnecessarily abort QEMU. */
1412 return -EIO;
1415 if (offs > start) {
1416 /* D2: in hole, next data at offs */
1417 *hole = start;
1418 *data = offs;
1419 return 0;
1422 /* D1: in data, end not yet known */
1425 * SEEK_HOLE cases:
1426 * H1. offs == start: start is in a hole
1427 * If this happens here, a hole has been dug behind our back
1428 * since the previous lseek().
1429 * H2. offs > start: either start is in data, next hole at offs,
1430 * or start is in trailing hole, EOF at offs
1431 * Linux treats trailing holes like any other hole: offs ==
1432 * start. Solaris seeks to EOF instead: offs > start (blech).
1433 * If that happens here, a hole has been dug behind our back
1434 * since the previous lseek().
1435 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1436 * If this happens, the file has been truncated behind our
1437 * back since we opened it. Treat it like a trailing hole.
1438 * H4. offs < 0, errno != ENXIO: we learned nothing
1439 * Pretend we know nothing at all, i.e. "forget" about D1.
1441 offs = glfs_lseek(s->fd, start, SEEK_HOLE);
1442 if (offs < 0) {
1443 return -errno; /* D1 and (H3 or H4) */
1446 if (offs < start) {
1447 /* This is not a valid return by lseek(). We are safe to just return
1448 * -EIO in this case, and we'll treat it like H4. Unfortunately some
1449 * versions of gluster server will return offs < start, so an assert
1450 * here will unnecessarily abort QEMU. */
1451 return -EIO;
1454 if (offs > start) {
1456 * D1 and H2: either in data, next hole at offs, or it was in
1457 * data but is now in a trailing hole. In the latter case,
1458 * all bets are off. Treating it as if it there was data all
1459 * the way to EOF is safe, so simply do that.
1461 *data = start;
1462 *hole = offs;
1463 return 0;
1466 /* D1 and H1 */
1467 return -EBUSY;
1468 #endif
1470 exit:
1471 return -ENOTSUP;
1475 * Returns the allocation status of the specified offset.
1477 * The block layer guarantees 'offset' and 'bytes' are within bounds.
1479 * 'pnum' is set to the number of bytes (including and immediately following
1480 * the specified offset) that are known to be in the same
1481 * allocated/unallocated state.
1483 * 'bytes' is the max value 'pnum' should be set to.
1485 * (Based on raw_co_block_status() from file-posix.c.)
1487 static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs,
1488 bool want_zero,
1489 int64_t offset,
1490 int64_t bytes,
1491 int64_t *pnum,
1492 int64_t *map,
1493 BlockDriverState **file)
1495 BDRVGlusterState *s = bs->opaque;
1496 off_t data = 0, hole = 0;
1497 int ret = -EINVAL;
1499 if (!s->fd) {
1500 return ret;
1503 if (!want_zero) {
1504 *pnum = bytes;
1505 *map = offset;
1506 *file = bs;
1507 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1510 ret = find_allocation(bs, offset, &data, &hole);
1511 if (ret == -ENXIO) {
1512 /* Trailing hole */
1513 *pnum = bytes;
1514 ret = BDRV_BLOCK_ZERO;
1515 } else if (ret < 0) {
1516 /* No info available, so pretend there are no holes */
1517 *pnum = bytes;
1518 ret = BDRV_BLOCK_DATA;
1519 } else if (data == offset) {
1520 /* On a data extent, compute bytes to the end of the extent,
1521 * possibly including a partial sector at EOF. */
1522 *pnum = MIN(bytes, hole - offset);
1523 ret = BDRV_BLOCK_DATA;
1524 } else {
1525 /* On a hole, compute bytes to the beginning of the next extent. */
1526 assert(hole == offset);
1527 *pnum = MIN(bytes, data - offset);
1528 ret = BDRV_BLOCK_ZERO;
1531 *map = offset;
1532 *file = bs;
1534 return ret | BDRV_BLOCK_OFFSET_VALID;
1538 static const char *const gluster_strong_open_opts[] = {
1539 GLUSTER_OPT_VOLUME,
1540 GLUSTER_OPT_PATH,
1541 GLUSTER_OPT_TYPE,
1542 GLUSTER_OPT_SERVER_PATTERN,
1543 GLUSTER_OPT_HOST,
1544 GLUSTER_OPT_PORT,
1545 GLUSTER_OPT_TO,
1546 GLUSTER_OPT_IPV4,
1547 GLUSTER_OPT_IPV6,
1548 GLUSTER_OPT_SOCKET,
1550 NULL
1553 static BlockDriver bdrv_gluster = {
1554 .format_name = "gluster",
1555 .protocol_name = "gluster",
1556 .instance_size = sizeof(BDRVGlusterState),
1557 .bdrv_needs_filename = false,
1558 .bdrv_file_open = qemu_gluster_open,
1559 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1560 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1561 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1562 .bdrv_close = qemu_gluster_close,
1563 .bdrv_co_create = qemu_gluster_co_create,
1564 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1565 .bdrv_getlength = qemu_gluster_getlength,
1566 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1567 .bdrv_co_truncate = qemu_gluster_co_truncate,
1568 .bdrv_co_readv = qemu_gluster_co_readv,
1569 .bdrv_co_writev = qemu_gluster_co_writev,
1570 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1571 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1572 .bdrv_has_zero_init_truncate = qemu_gluster_has_zero_init,
1573 #ifdef CONFIG_GLUSTERFS_DISCARD
1574 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1575 #endif
1576 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1577 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1578 #endif
1579 .bdrv_co_block_status = qemu_gluster_co_block_status,
1580 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1581 .create_opts = &qemu_gluster_create_opts,
1582 .strong_runtime_opts = gluster_strong_open_opts,
1585 static BlockDriver bdrv_gluster_tcp = {
1586 .format_name = "gluster",
1587 .protocol_name = "gluster+tcp",
1588 .instance_size = sizeof(BDRVGlusterState),
1589 .bdrv_needs_filename = false,
1590 .bdrv_file_open = qemu_gluster_open,
1591 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1592 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1593 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1594 .bdrv_close = qemu_gluster_close,
1595 .bdrv_co_create = qemu_gluster_co_create,
1596 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1597 .bdrv_getlength = qemu_gluster_getlength,
1598 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1599 .bdrv_co_truncate = qemu_gluster_co_truncate,
1600 .bdrv_co_readv = qemu_gluster_co_readv,
1601 .bdrv_co_writev = qemu_gluster_co_writev,
1602 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1603 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1604 .bdrv_has_zero_init_truncate = qemu_gluster_has_zero_init,
1605 #ifdef CONFIG_GLUSTERFS_DISCARD
1606 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1607 #endif
1608 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1609 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1610 #endif
1611 .bdrv_co_block_status = qemu_gluster_co_block_status,
1612 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1613 .create_opts = &qemu_gluster_create_opts,
1614 .strong_runtime_opts = gluster_strong_open_opts,
1617 static BlockDriver bdrv_gluster_unix = {
1618 .format_name = "gluster",
1619 .protocol_name = "gluster+unix",
1620 .instance_size = sizeof(BDRVGlusterState),
1621 .bdrv_needs_filename = true,
1622 .bdrv_file_open = qemu_gluster_open,
1623 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1624 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1625 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1626 .bdrv_close = qemu_gluster_close,
1627 .bdrv_co_create = qemu_gluster_co_create,
1628 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1629 .bdrv_getlength = qemu_gluster_getlength,
1630 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1631 .bdrv_co_truncate = qemu_gluster_co_truncate,
1632 .bdrv_co_readv = qemu_gluster_co_readv,
1633 .bdrv_co_writev = qemu_gluster_co_writev,
1634 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1635 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1636 .bdrv_has_zero_init_truncate = qemu_gluster_has_zero_init,
1637 #ifdef CONFIG_GLUSTERFS_DISCARD
1638 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1639 #endif
1640 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1641 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1642 #endif
1643 .bdrv_co_block_status = qemu_gluster_co_block_status,
1644 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1645 .create_opts = &qemu_gluster_create_opts,
1646 .strong_runtime_opts = gluster_strong_open_opts,
1649 /* rdma is deprecated (actually never supported for volfile fetch).
1650 * Let's maintain it for the protocol compatibility, to make sure things
1651 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1652 * protocol with a warning.
1653 * TODO: remove gluster+rdma interface support
1655 static BlockDriver bdrv_gluster_rdma = {
1656 .format_name = "gluster",
1657 .protocol_name = "gluster+rdma",
1658 .instance_size = sizeof(BDRVGlusterState),
1659 .bdrv_needs_filename = true,
1660 .bdrv_file_open = qemu_gluster_open,
1661 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1662 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1663 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1664 .bdrv_close = qemu_gluster_close,
1665 .bdrv_co_create = qemu_gluster_co_create,
1666 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1667 .bdrv_getlength = qemu_gluster_getlength,
1668 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1669 .bdrv_co_truncate = qemu_gluster_co_truncate,
1670 .bdrv_co_readv = qemu_gluster_co_readv,
1671 .bdrv_co_writev = qemu_gluster_co_writev,
1672 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1673 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1674 .bdrv_has_zero_init_truncate = qemu_gluster_has_zero_init,
1675 #ifdef CONFIG_GLUSTERFS_DISCARD
1676 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1677 #endif
1678 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1679 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1680 #endif
1681 .bdrv_co_block_status = qemu_gluster_co_block_status,
1682 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1683 .create_opts = &qemu_gluster_create_opts,
1684 .strong_runtime_opts = gluster_strong_open_opts,
1687 static void bdrv_gluster_init(void)
1689 bdrv_register(&bdrv_gluster_rdma);
1690 bdrv_register(&bdrv_gluster_unix);
1691 bdrv_register(&bdrv_gluster_tcp);
1692 bdrv_register(&bdrv_gluster);
1695 block_init(bdrv_gluster_init);