Merge tag 'accel-sh4-ui-20240503' of https://github.com/philmd/qemu into staging
[qemu/armbru.git] / block / gluster.c
blob4253c8db5e44f8ea3c934a3bce2653d880e30f74
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-io.h"
15 #include "block/block_int.h"
16 #include "block/qdict.h"
17 #include "qapi/error.h"
18 #include "qapi/qmp/qdict.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qemu/uri.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "qemu/option.h"
24 #include "qemu/cutils.h"
26 #ifdef CONFIG_GLUSTERFS_FTRUNCATE_HAS_STAT
27 # define glfs_ftruncate(fd, offset) glfs_ftruncate(fd, offset, NULL, NULL)
28 #endif
30 #define GLUSTER_OPT_FILENAME "filename"
31 #define GLUSTER_OPT_VOLUME "volume"
32 #define GLUSTER_OPT_PATH "path"
33 #define GLUSTER_OPT_TYPE "type"
34 #define GLUSTER_OPT_SERVER_PATTERN "server."
35 #define GLUSTER_OPT_HOST "host"
36 #define GLUSTER_OPT_PORT "port"
37 #define GLUSTER_OPT_TO "to"
38 #define GLUSTER_OPT_IPV4 "ipv4"
39 #define GLUSTER_OPT_IPV6 "ipv6"
40 #define GLUSTER_OPT_SOCKET "socket"
41 #define GLUSTER_OPT_DEBUG "debug"
42 #define GLUSTER_DEFAULT_PORT 24007
43 #define GLUSTER_DEBUG_DEFAULT 4
44 #define GLUSTER_DEBUG_MAX 9
45 #define GLUSTER_OPT_LOGFILE "logfile"
46 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
48 * Several versions of GlusterFS (3.12? -> 6.0.1) fail when the transfer size
49 * is greater or equal to 1024 MiB, so we are limiting the transfer size to 512
50 * MiB to avoid this rare issue.
52 #define GLUSTER_MAX_TRANSFER (512 * MiB)
54 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
56 typedef struct GlusterAIOCB {
57 int64_t size;
58 int ret;
59 Coroutine *coroutine;
60 AioContext *aio_context;
61 } GlusterAIOCB;
63 typedef struct BDRVGlusterState {
64 struct glfs *glfs;
65 struct glfs_fd *fd;
66 char *logfile;
67 bool supports_seek_data;
68 int debug;
69 } BDRVGlusterState;
71 typedef struct BDRVGlusterReopenState {
72 struct glfs *glfs;
73 struct glfs_fd *fd;
74 } BDRVGlusterReopenState;
77 typedef struct GlfsPreopened {
78 char *volume;
79 glfs_t *fs;
80 int ref;
81 } GlfsPreopened;
83 typedef struct ListElement {
84 QLIST_ENTRY(ListElement) list;
85 GlfsPreopened saved;
86 } ListElement;
88 static QLIST_HEAD(, ListElement) glfs_list;
90 static QemuOptsList qemu_gluster_create_opts = {
91 .name = "qemu-gluster-create-opts",
92 .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
93 .desc = {
95 .name = BLOCK_OPT_SIZE,
96 .type = QEMU_OPT_SIZE,
97 .help = "Virtual disk size"
100 .name = BLOCK_OPT_PREALLOC,
101 .type = QEMU_OPT_STRING,
102 .help = "Preallocation mode (allowed values: off"
103 #ifdef CONFIG_GLUSTERFS_FALLOCATE
104 ", falloc"
105 #endif
106 #ifdef CONFIG_GLUSTERFS_ZEROFILL
107 ", full"
108 #endif
112 .name = GLUSTER_OPT_DEBUG,
113 .type = QEMU_OPT_NUMBER,
114 .help = "Gluster log level, valid range is 0-9",
117 .name = GLUSTER_OPT_LOGFILE,
118 .type = QEMU_OPT_STRING,
119 .help = "Logfile path of libgfapi",
121 { /* end of list */ }
125 static QemuOptsList runtime_opts = {
126 .name = "gluster",
127 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
128 .desc = {
130 .name = GLUSTER_OPT_FILENAME,
131 .type = QEMU_OPT_STRING,
132 .help = "URL to the gluster image",
135 .name = GLUSTER_OPT_DEBUG,
136 .type = QEMU_OPT_NUMBER,
137 .help = "Gluster log level, valid range is 0-9",
140 .name = GLUSTER_OPT_LOGFILE,
141 .type = QEMU_OPT_STRING,
142 .help = "Logfile path of libgfapi",
144 { /* end of list */ }
148 static QemuOptsList runtime_json_opts = {
149 .name = "gluster_json",
150 .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head),
151 .desc = {
153 .name = GLUSTER_OPT_VOLUME,
154 .type = QEMU_OPT_STRING,
155 .help = "name of gluster volume where VM image resides",
158 .name = GLUSTER_OPT_PATH,
159 .type = QEMU_OPT_STRING,
160 .help = "absolute path to image file in gluster volume",
163 .name = GLUSTER_OPT_DEBUG,
164 .type = QEMU_OPT_NUMBER,
165 .help = "Gluster log level, valid range is 0-9",
167 { /* end of list */ }
171 static QemuOptsList runtime_type_opts = {
172 .name = "gluster_type",
173 .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head),
174 .desc = {
176 .name = GLUSTER_OPT_TYPE,
177 .type = QEMU_OPT_STRING,
178 .help = "inet|unix",
180 { /* end of list */ }
184 static QemuOptsList runtime_unix_opts = {
185 .name = "gluster_unix",
186 .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head),
187 .desc = {
189 .name = GLUSTER_OPT_SOCKET,
190 .type = QEMU_OPT_STRING,
191 .help = "socket file path (legacy)",
194 .name = GLUSTER_OPT_PATH,
195 .type = QEMU_OPT_STRING,
196 .help = "socket file path (QAPI)",
198 { /* end of list */ }
202 static QemuOptsList runtime_inet_opts = {
203 .name = "gluster_inet",
204 .head = QTAILQ_HEAD_INITIALIZER(runtime_inet_opts.head),
205 .desc = {
207 .name = GLUSTER_OPT_TYPE,
208 .type = QEMU_OPT_STRING,
209 .help = "inet|unix",
212 .name = GLUSTER_OPT_HOST,
213 .type = QEMU_OPT_STRING,
214 .help = "host address (hostname/ipv4/ipv6 addresses)",
217 .name = GLUSTER_OPT_PORT,
218 .type = QEMU_OPT_STRING,
219 .help = "port number on which glusterd is listening (default 24007)",
222 .name = "to",
223 .type = QEMU_OPT_NUMBER,
224 .help = "max port number, not supported by gluster",
227 .name = "ipv4",
228 .type = QEMU_OPT_BOOL,
229 .help = "ipv4 bool value, not supported by gluster",
232 .name = "ipv6",
233 .type = QEMU_OPT_BOOL,
234 .help = "ipv6 bool value, not supported by gluster",
236 { /* end of list */ }
240 static void glfs_set_preopened(const char *volume, glfs_t *fs)
242 ListElement *entry = NULL;
244 entry = g_new(ListElement, 1);
246 entry->saved.volume = g_strdup(volume);
248 entry->saved.fs = fs;
249 entry->saved.ref = 1;
251 QLIST_INSERT_HEAD(&glfs_list, entry, list);
254 static glfs_t *glfs_find_preopened(const char *volume)
256 ListElement *entry = NULL;
258 QLIST_FOREACH(entry, &glfs_list, list) {
259 if (strcmp(entry->saved.volume, volume) == 0) {
260 entry->saved.ref++;
261 return entry->saved.fs;
265 return NULL;
268 static void glfs_clear_preopened(glfs_t *fs)
270 ListElement *entry = NULL;
271 ListElement *next;
273 if (fs == NULL) {
274 return;
277 QLIST_FOREACH_SAFE(entry, &glfs_list, list, next) {
278 if (entry->saved.fs == fs) {
279 if (--entry->saved.ref) {
280 return;
283 QLIST_REMOVE(entry, list);
285 glfs_fini(entry->saved.fs);
286 g_free(entry->saved.volume);
287 g_free(entry);
292 static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path)
294 char *p, *q;
296 if (!path) {
297 return -EINVAL;
300 /* volume */
301 p = q = path + strspn(path, "/");
302 p += strcspn(p, "/");
303 if (*p == '\0') {
304 return -EINVAL;
306 gconf->volume = g_strndup(q, p - q);
308 /* path */
309 p += strspn(p, "/");
310 if (*p == '\0') {
311 return -EINVAL;
313 gconf->path = g_strdup(p);
314 return 0;
318 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
320 * 'gluster' is the protocol.
322 * 'transport' specifies the transport type used to connect to gluster
323 * management daemon (glusterd). Valid transport types are
324 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
326 * 'host' specifies the host where the volume file specification for
327 * the given volume resides. This can be either hostname or ipv4 address.
328 * If transport type is 'unix', then 'host' field should not be specified.
329 * The 'socket' field needs to be populated with the path to unix domain
330 * socket.
332 * 'port' is the port number on which glusterd is listening. This is optional
333 * and if not specified, QEMU will send 0 which will make gluster to use the
334 * default port. If the transport type is unix, then 'port' should not be
335 * specified.
337 * 'volume' is the name of the gluster volume which contains the VM image.
339 * 'path' is the path to the actual VM image that resides on gluster volume.
341 * Examples:
343 * file=gluster://1.2.3.4/testvol/a.img
344 * file=gluster+tcp://1.2.3.4/testvol/a.img
345 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
346 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
347 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
349 static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
350 const char *filename)
352 SocketAddress *gsconf;
353 URI *uri;
354 QueryParams *qp = NULL;
355 bool is_unix = false;
356 int ret = 0;
358 uri = uri_parse(filename);
359 if (!uri) {
360 return -EINVAL;
363 gsconf = g_new0(SocketAddress, 1);
364 QAPI_LIST_PREPEND(gconf->server, gsconf);
366 /* transport */
367 if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
368 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
369 } else if (!strcmp(uri->scheme, "gluster+tcp")) {
370 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
371 } else if (!strcmp(uri->scheme, "gluster+unix")) {
372 gsconf->type = SOCKET_ADDRESS_TYPE_UNIX;
373 is_unix = true;
374 } else {
375 ret = -EINVAL;
376 goto out;
379 ret = parse_volume_options(gconf, uri->path);
380 if (ret < 0) {
381 goto out;
384 qp = query_params_parse(uri->query);
385 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
386 ret = -EINVAL;
387 goto out;
390 if (is_unix) {
391 if (uri->server || uri->port) {
392 ret = -EINVAL;
393 goto out;
395 if (strcmp(qp->p[0].name, "socket")) {
396 ret = -EINVAL;
397 goto out;
399 gsconf->u.q_unix.path = g_strdup(qp->p[0].value);
400 } else {
401 gsconf->u.inet.host = g_strdup(uri->server ? uri->server : "localhost");
402 if (uri->port) {
403 gsconf->u.inet.port = g_strdup_printf("%d", uri->port);
404 } else {
405 gsconf->u.inet.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT);
409 out:
410 if (qp) {
411 query_params_free(qp);
413 uri_free(uri);
414 return ret;
417 static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
418 Error **errp)
420 struct glfs *glfs;
421 int ret;
422 int old_errno;
423 SocketAddressList *server;
424 uint64_t port;
426 glfs = glfs_find_preopened(gconf->volume);
427 if (glfs) {
428 return glfs;
431 glfs = glfs_new(gconf->volume);
432 if (!glfs) {
433 goto out;
436 glfs_set_preopened(gconf->volume, glfs);
438 for (server = gconf->server; server; server = server->next) {
439 switch (server->value->type) {
440 case SOCKET_ADDRESS_TYPE_UNIX:
441 ret = glfs_set_volfile_server(glfs, "unix",
442 server->value->u.q_unix.path, 0);
443 break;
444 case SOCKET_ADDRESS_TYPE_INET:
445 if (parse_uint_full(server->value->u.inet.port, 10, &port) < 0 ||
446 port > 65535) {
447 error_setg(errp, "'%s' is not a valid port number",
448 server->value->u.inet.port);
449 errno = EINVAL;
450 goto out;
452 ret = glfs_set_volfile_server(glfs, "tcp",
453 server->value->u.inet.host,
454 (int)port);
455 break;
456 case SOCKET_ADDRESS_TYPE_VSOCK:
457 case SOCKET_ADDRESS_TYPE_FD:
458 default:
459 abort();
462 if (ret < 0) {
463 goto out;
467 ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug);
468 if (ret < 0) {
469 goto out;
472 ret = glfs_init(glfs);
473 if (ret) {
474 error_setg(errp, "Gluster connection for volume %s, path %s failed"
475 " to connect", gconf->volume, gconf->path);
476 for (server = gconf->server; server; server = server->next) {
477 if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) {
478 error_append_hint(errp, "hint: failed on socket %s ",
479 server->value->u.q_unix.path);
480 } else {
481 error_append_hint(errp, "hint: failed on host %s and port %s ",
482 server->value->u.inet.host,
483 server->value->u.inet.port);
487 error_append_hint(errp, "Please refer to gluster logs for more info\n");
489 /* glfs_init sometimes doesn't set errno although docs suggest that */
490 if (errno == 0) {
491 errno = EINVAL;
494 goto out;
496 return glfs;
498 out:
499 if (glfs) {
500 old_errno = errno;
501 glfs_clear_preopened(glfs);
502 errno = old_errno;
504 return NULL;
508 * Convert the json formatted command line into qapi.
510 static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
511 QDict *options, Error **errp)
513 QemuOpts *opts;
514 SocketAddress *gsconf = NULL;
515 SocketAddressList **tail;
516 QDict *backing_options = NULL;
517 Error *local_err = NULL;
518 char *str = NULL;
519 const char *ptr;
520 int i, type, num_servers;
522 /* create opts info from runtime_json_opts list */
523 opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort);
524 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
525 goto out;
528 num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN);
529 if (num_servers < 1) {
530 error_setg(&local_err, QERR_MISSING_PARAMETER, "server");
531 goto out;
534 ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME);
535 if (!ptr) {
536 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME);
537 goto out;
539 gconf->volume = g_strdup(ptr);
541 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
542 if (!ptr) {
543 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH);
544 goto out;
546 gconf->path = g_strdup(ptr);
547 qemu_opts_del(opts);
548 tail = &gconf->server;
550 for (i = 0; i < num_servers; i++) {
551 str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
552 qdict_extract_subqdict(options, &backing_options, str);
554 /* create opts info from runtime_type_opts list */
555 opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort);
556 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
557 goto out;
560 ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE);
561 if (!ptr) {
562 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE);
563 error_append_hint(&local_err, GERR_INDEX_HINT, i);
564 goto out;
567 gsconf = g_new0(SocketAddress, 1);
568 if (!strcmp(ptr, "tcp")) {
569 ptr = "inet"; /* accept legacy "tcp" */
571 type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
572 if (type != SOCKET_ADDRESS_TYPE_INET
573 && type != SOCKET_ADDRESS_TYPE_UNIX) {
574 error_setg(&local_err,
575 "Parameter '%s' may be 'inet' or 'unix'",
576 GLUSTER_OPT_TYPE);
577 error_append_hint(&local_err, GERR_INDEX_HINT, i);
578 goto out;
580 gsconf->type = type;
581 qemu_opts_del(opts);
583 if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) {
584 /* create opts info from runtime_inet_opts list */
585 opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort);
586 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
587 goto out;
590 ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST);
591 if (!ptr) {
592 error_setg(&local_err, QERR_MISSING_PARAMETER,
593 GLUSTER_OPT_HOST);
594 error_append_hint(&local_err, GERR_INDEX_HINT, i);
595 goto out;
597 gsconf->u.inet.host = g_strdup(ptr);
598 ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT);
599 if (!ptr) {
600 error_setg(&local_err, QERR_MISSING_PARAMETER,
601 GLUSTER_OPT_PORT);
602 error_append_hint(&local_err, GERR_INDEX_HINT, i);
603 goto out;
605 gsconf->u.inet.port = g_strdup(ptr);
607 /* defend for unsupported fields in InetSocketAddress,
608 * i.e. @ipv4, @ipv6 and @to
610 ptr = qemu_opt_get(opts, GLUSTER_OPT_TO);
611 if (ptr) {
612 gsconf->u.inet.has_to = true;
614 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4);
615 if (ptr) {
616 gsconf->u.inet.has_ipv4 = true;
618 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6);
619 if (ptr) {
620 gsconf->u.inet.has_ipv6 = true;
622 if (gsconf->u.inet.has_to) {
623 error_setg(&local_err, "Parameter 'to' not supported");
624 goto out;
626 if (gsconf->u.inet.has_ipv4 || gsconf->u.inet.has_ipv6) {
627 error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported");
628 goto out;
630 qemu_opts_del(opts);
631 } else {
632 /* create opts info from runtime_unix_opts list */
633 opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort);
634 if (!qemu_opts_absorb_qdict(opts, backing_options, errp)) {
635 goto out;
638 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
639 if (!ptr) {
640 ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET);
641 } else if (qemu_opt_get(opts, GLUSTER_OPT_SOCKET)) {
642 error_setg(&local_err,
643 "Conflicting parameters 'path' and 'socket'");
644 error_append_hint(&local_err, GERR_INDEX_HINT, i);
645 goto out;
647 if (!ptr) {
648 error_setg(&local_err, QERR_MISSING_PARAMETER,
649 GLUSTER_OPT_PATH);
650 error_append_hint(&local_err, GERR_INDEX_HINT, i);
651 goto out;
653 gsconf->u.q_unix.path = g_strdup(ptr);
654 qemu_opts_del(opts);
657 QAPI_LIST_APPEND(tail, gsconf);
658 gsconf = NULL;
660 qobject_unref(backing_options);
661 backing_options = NULL;
662 g_free(str);
663 str = NULL;
666 return 0;
668 out:
669 error_propagate(errp, local_err);
670 qapi_free_SocketAddress(gsconf);
671 qemu_opts_del(opts);
672 g_free(str);
673 qobject_unref(backing_options);
674 errno = EINVAL;
675 return -errno;
678 /* Converts options given in @filename and the @options QDict into the QAPI
679 * object @gconf. */
680 static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
681 const char *filename,
682 QDict *options, Error **errp)
684 int ret;
685 if (filename) {
686 ret = qemu_gluster_parse_uri(gconf, filename);
687 if (ret < 0) {
688 error_setg(errp, "invalid URI %s", filename);
689 error_append_hint(errp, "Usage: file=gluster[+transport]://"
690 "[host[:port]]volume/path[?socket=...]"
691 "[,file.debug=N]"
692 "[,file.logfile=/path/filename.log]\n");
693 return ret;
695 } else {
696 ret = qemu_gluster_parse_json(gconf, options, errp);
697 if (ret < 0) {
698 error_append_hint(errp, "Usage: "
699 "-drive driver=qcow2,file.driver=gluster,"
700 "file.volume=testvol,file.path=/path/a.qcow2"
701 "[,file.debug=9]"
702 "[,file.logfile=/path/filename.log],"
703 "file.server.0.type=inet,"
704 "file.server.0.host=1.2.3.4,"
705 "file.server.0.port=24007,"
706 "file.server.1.transport=unix,"
707 "file.server.1.path=/var/run/glusterd.socket ..."
708 "\n");
709 return ret;
713 return 0;
716 static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
717 const char *filename,
718 QDict *options, Error **errp)
720 int ret;
722 ret = qemu_gluster_parse(gconf, filename, options, errp);
723 if (ret < 0) {
724 errno = -ret;
725 return NULL;
728 return qemu_gluster_glfs_init(gconf, errp);
732 * AIO callback routine called from GlusterFS thread.
734 static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret,
735 #ifdef CONFIG_GLUSTERFS_IOCB_HAS_STAT
736 struct glfs_stat *pre, struct glfs_stat *post,
737 #endif
738 void *arg)
740 GlusterAIOCB *acb = (GlusterAIOCB *)arg;
742 if (!ret || ret == acb->size) {
743 acb->ret = 0; /* Success */
744 } else if (ret < 0) {
745 acb->ret = -errno; /* Read/Write failed */
746 } else {
747 acb->ret = -EIO; /* Partial read/write - fail it */
750 aio_co_schedule(acb->aio_context, acb->coroutine);
753 static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
755 assert(open_flags != NULL);
757 *open_flags |= O_BINARY;
759 if (bdrv_flags & BDRV_O_RDWR) {
760 *open_flags |= O_RDWR;
761 } else {
762 *open_flags |= O_RDONLY;
765 if ((bdrv_flags & BDRV_O_NOCACHE)) {
766 *open_flags |= O_DIRECT;
771 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
772 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
773 * - Corrected versions return -1 and set errno to EINVAL.
774 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
775 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
777 static bool qemu_gluster_test_seek(struct glfs_fd *fd)
779 off_t ret = 0;
781 #if defined SEEK_HOLE && defined SEEK_DATA
782 off_t eof;
784 eof = glfs_lseek(fd, 0, SEEK_END);
785 if (eof < 0) {
786 /* this should never occur */
787 return false;
790 /* this should always fail with ENXIO if SEEK_DATA is supported */
791 ret = glfs_lseek(fd, eof, SEEK_DATA);
792 #endif
794 return (ret < 0) && (errno == ENXIO);
797 static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
798 int bdrv_flags, Error **errp)
800 BDRVGlusterState *s = bs->opaque;
801 int open_flags = 0;
802 int ret = 0;
803 BlockdevOptionsGluster *gconf = NULL;
804 QemuOpts *opts;
805 const char *filename, *logfile;
807 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
808 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
809 ret = -EINVAL;
810 goto out;
813 filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME);
815 s->debug = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG,
816 GLUSTER_DEBUG_DEFAULT);
817 if (s->debug < 0) {
818 s->debug = 0;
819 } else if (s->debug > GLUSTER_DEBUG_MAX) {
820 s->debug = GLUSTER_DEBUG_MAX;
823 gconf = g_new0(BlockdevOptionsGluster, 1);
824 gconf->debug = s->debug;
825 gconf->has_debug = true;
827 logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE);
828 s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT);
830 gconf->logfile = g_strdup(s->logfile);
832 s->glfs = qemu_gluster_init(gconf, filename, options, errp);
833 if (!s->glfs) {
834 ret = -errno;
835 goto out;
838 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
839 /* Without this, if fsync fails for a recoverable reason (for instance,
840 * ENOSPC), gluster will dump its cache, preventing retries. This means
841 * almost certain data loss. Not all gluster versions support the
842 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
843 * discover during runtime if it is supported (this api returns success for
844 * unknown key/value pairs) */
845 ret = glfs_set_xlator_option(s->glfs, "*-write-behind",
846 "resync-failed-syncs-after-fsync",
847 "on");
848 if (ret < 0) {
849 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
850 ret = -errno;
851 goto out;
853 #endif
855 qemu_gluster_parse_flags(bdrv_flags, &open_flags);
857 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
858 ret = s->fd ? 0 : -errno;
860 if (ret == -EACCES || ret == -EROFS) {
861 /* Try to degrade to read-only, but if it doesn't work, still use the
862 * normal error message. */
863 bdrv_graph_rdlock_main_loop();
864 if (bdrv_apply_auto_read_only(bs, NULL, NULL) == 0) {
865 open_flags = (open_flags & ~O_RDWR) | O_RDONLY;
866 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
867 ret = s->fd ? 0 : -errno;
869 bdrv_graph_rdunlock_main_loop();
872 s->supports_seek_data = qemu_gluster_test_seek(s->fd);
874 out:
875 qemu_opts_del(opts);
876 qapi_free_BlockdevOptionsGluster(gconf);
877 if (!ret) {
878 return ret;
880 g_free(s->logfile);
881 if (s->fd) {
882 glfs_close(s->fd);
885 glfs_clear_preopened(s->glfs);
887 return ret;
890 static void qemu_gluster_refresh_limits(BlockDriverState *bs, Error **errp)
892 bs->bl.max_transfer = GLUSTER_MAX_TRANSFER;
893 bs->bl.max_pdiscard = MIN(SIZE_MAX, INT64_MAX);
896 static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
897 BlockReopenQueue *queue, Error **errp)
899 int ret = 0;
900 BDRVGlusterState *s;
901 BDRVGlusterReopenState *reop_s;
902 BlockdevOptionsGluster *gconf;
903 int open_flags = 0;
905 assert(state != NULL);
906 assert(state->bs != NULL);
908 s = state->bs->opaque;
910 state->opaque = g_new0(BDRVGlusterReopenState, 1);
911 reop_s = state->opaque;
913 qemu_gluster_parse_flags(state->flags, &open_flags);
915 gconf = g_new0(BlockdevOptionsGluster, 1);
916 gconf->debug = s->debug;
917 gconf->has_debug = true;
918 gconf->logfile = g_strdup(s->logfile);
921 * If 'state->bs->exact_filename' is empty, 'state->options' should contain
922 * the JSON parameters already parsed.
924 if (state->bs->exact_filename[0] != '\0') {
925 reop_s->glfs = qemu_gluster_init(gconf, state->bs->exact_filename, NULL,
926 errp);
927 } else {
928 reop_s->glfs = qemu_gluster_init(gconf, NULL, state->options, errp);
930 if (reop_s->glfs == NULL) {
931 ret = -errno;
932 goto exit;
935 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
936 ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind",
937 "resync-failed-syncs-after-fsync", "on");
938 if (ret < 0) {
939 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
940 ret = -errno;
941 goto exit;
943 #endif
945 reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags);
946 if (reop_s->fd == NULL) {
947 /* reops->glfs will be cleaned up in _abort */
948 ret = -errno;
949 goto exit;
952 exit:
953 /* state->opaque will be freed in either the _abort or _commit */
954 qapi_free_BlockdevOptionsGluster(gconf);
955 return ret;
958 static void qemu_gluster_reopen_commit(BDRVReopenState *state)
960 BDRVGlusterReopenState *reop_s = state->opaque;
961 BDRVGlusterState *s = state->bs->opaque;
964 /* close the old */
965 if (s->fd) {
966 glfs_close(s->fd);
969 glfs_clear_preopened(s->glfs);
971 /* use the newly opened image / connection */
972 s->fd = reop_s->fd;
973 s->glfs = reop_s->glfs;
975 g_free(state->opaque);
976 state->opaque = NULL;
978 return;
982 static void qemu_gluster_reopen_abort(BDRVReopenState *state)
984 BDRVGlusterReopenState *reop_s = state->opaque;
986 if (reop_s == NULL) {
987 return;
990 if (reop_s->fd) {
991 glfs_close(reop_s->fd);
994 glfs_clear_preopened(reop_s->glfs);
996 g_free(state->opaque);
997 state->opaque = NULL;
999 return;
1002 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1003 static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
1004 int64_t offset,
1005 int64_t bytes,
1006 BdrvRequestFlags flags)
1008 int ret;
1009 GlusterAIOCB acb;
1010 BDRVGlusterState *s = bs->opaque;
1012 acb.size = bytes;
1013 acb.ret = 0;
1014 acb.coroutine = qemu_coroutine_self();
1015 acb.aio_context = bdrv_get_aio_context(bs);
1017 ret = glfs_zerofill_async(s->fd, offset, bytes, gluster_finish_aiocb, &acb);
1018 if (ret < 0) {
1019 return -errno;
1022 qemu_coroutine_yield();
1023 return acb.ret;
1025 #endif
1027 static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
1028 PreallocMode prealloc, Error **errp)
1030 int64_t current_length;
1032 current_length = glfs_lseek(fd, 0, SEEK_END);
1033 if (current_length < 0) {
1034 error_setg_errno(errp, errno, "Failed to determine current size");
1035 return -errno;
1038 if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
1039 error_setg(errp, "Cannot use preallocation for shrinking files");
1040 return -ENOTSUP;
1043 if (current_length == offset) {
1044 return 0;
1047 switch (prealloc) {
1048 #ifdef CONFIG_GLUSTERFS_FALLOCATE
1049 case PREALLOC_MODE_FALLOC:
1050 if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
1051 error_setg_errno(errp, errno, "Could not preallocate data");
1052 return -errno;
1054 break;
1055 #endif /* CONFIG_GLUSTERFS_FALLOCATE */
1056 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1057 case PREALLOC_MODE_FULL:
1058 if (glfs_ftruncate(fd, offset)) {
1059 error_setg_errno(errp, errno, "Could not resize file");
1060 return -errno;
1062 if (glfs_zerofill(fd, current_length, offset - current_length)) {
1063 error_setg_errno(errp, errno, "Could not zerofill the new area");
1064 return -errno;
1066 break;
1067 #endif /* CONFIG_GLUSTERFS_ZEROFILL */
1068 case PREALLOC_MODE_OFF:
1069 if (glfs_ftruncate(fd, offset)) {
1070 error_setg_errno(errp, errno, "Could not resize file");
1071 return -errno;
1073 break;
1074 default:
1075 error_setg(errp, "Unsupported preallocation mode: %s",
1076 PreallocMode_str(prealloc));
1077 return -EINVAL;
1080 return 0;
1083 static int qemu_gluster_co_create(BlockdevCreateOptions *options,
1084 Error **errp)
1086 BlockdevCreateOptionsGluster *opts = &options->u.gluster;
1087 struct glfs *glfs;
1088 struct glfs_fd *fd = NULL;
1089 int ret = 0;
1091 assert(options->driver == BLOCKDEV_DRIVER_GLUSTER);
1093 glfs = qemu_gluster_glfs_init(opts->location, errp);
1094 if (!glfs) {
1095 ret = -errno;
1096 goto out;
1099 fd = glfs_creat(glfs, opts->location->path,
1100 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
1101 if (!fd) {
1102 ret = -errno;
1103 goto out;
1106 ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp);
1108 out:
1109 if (fd) {
1110 if (glfs_close(fd) != 0 && ret == 0) {
1111 ret = -errno;
1114 glfs_clear_preopened(glfs);
1115 return ret;
1118 static int coroutine_fn qemu_gluster_co_create_opts(BlockDriver *drv,
1119 const char *filename,
1120 QemuOpts *opts,
1121 Error **errp)
1123 BlockdevCreateOptions *options;
1124 BlockdevCreateOptionsGluster *gopts;
1125 BlockdevOptionsGluster *gconf;
1126 char *tmp = NULL;
1127 Error *local_err = NULL;
1128 int ret;
1130 options = g_new0(BlockdevCreateOptions, 1);
1131 options->driver = BLOCKDEV_DRIVER_GLUSTER;
1132 gopts = &options->u.gluster;
1134 gconf = g_new0(BlockdevOptionsGluster, 1);
1135 gopts->location = gconf;
1137 gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1138 BDRV_SECTOR_SIZE);
1140 tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1141 gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp,
1142 PREALLOC_MODE_OFF, &local_err);
1143 g_free(tmp);
1144 if (local_err) {
1145 error_propagate(errp, local_err);
1146 ret = -EINVAL;
1147 goto fail;
1150 gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
1151 GLUSTER_DEBUG_DEFAULT);
1152 if (gconf->debug < 0) {
1153 gconf->debug = 0;
1154 } else if (gconf->debug > GLUSTER_DEBUG_MAX) {
1155 gconf->debug = GLUSTER_DEBUG_MAX;
1157 gconf->has_debug = true;
1159 gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE);
1160 if (!gconf->logfile) {
1161 gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT);
1164 ret = qemu_gluster_parse(gconf, filename, NULL, errp);
1165 if (ret < 0) {
1166 goto fail;
1169 ret = qemu_gluster_co_create(options, errp);
1170 if (ret < 0) {
1171 goto fail;
1174 ret = 0;
1175 fail:
1176 qapi_free_BlockdevCreateOptions(options);
1177 return ret;
1180 static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
1181 int64_t sector_num, int nb_sectors,
1182 QEMUIOVector *qiov, int write)
1184 int ret;
1185 GlusterAIOCB acb;
1186 BDRVGlusterState *s = bs->opaque;
1187 size_t size = nb_sectors * BDRV_SECTOR_SIZE;
1188 off_t offset = sector_num * BDRV_SECTOR_SIZE;
1190 acb.size = size;
1191 acb.ret = 0;
1192 acb.coroutine = qemu_coroutine_self();
1193 acb.aio_context = bdrv_get_aio_context(bs);
1195 if (write) {
1196 ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1197 gluster_finish_aiocb, &acb);
1198 } else {
1199 ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1200 gluster_finish_aiocb, &acb);
1203 if (ret < 0) {
1204 return -errno;
1207 qemu_coroutine_yield();
1208 return acb.ret;
1211 static coroutine_fn int qemu_gluster_co_truncate(BlockDriverState *bs,
1212 int64_t offset,
1213 bool exact,
1214 PreallocMode prealloc,
1215 BdrvRequestFlags flags,
1216 Error **errp)
1218 BDRVGlusterState *s = bs->opaque;
1219 return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
1222 static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
1223 int64_t sector_num,
1224 int nb_sectors,
1225 QEMUIOVector *qiov)
1227 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
1230 static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
1231 int64_t sector_num,
1232 int nb_sectors,
1233 QEMUIOVector *qiov,
1234 int flags)
1236 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
1239 static void qemu_gluster_close(BlockDriverState *bs)
1241 BDRVGlusterState *s = bs->opaque;
1243 g_free(s->logfile);
1244 if (s->fd) {
1245 glfs_close(s->fd);
1246 s->fd = NULL;
1248 glfs_clear_preopened(s->glfs);
1251 static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
1253 int ret;
1254 GlusterAIOCB acb;
1255 BDRVGlusterState *s = bs->opaque;
1257 acb.size = 0;
1258 acb.ret = 0;
1259 acb.coroutine = qemu_coroutine_self();
1260 acb.aio_context = bdrv_get_aio_context(bs);
1262 ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb);
1263 if (ret < 0) {
1264 ret = -errno;
1265 goto error;
1268 qemu_coroutine_yield();
1269 if (acb.ret < 0) {
1270 ret = acb.ret;
1271 goto error;
1274 return acb.ret;
1276 error:
1277 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1278 * after a fsync failure, so we have no way of allowing the guest to safely
1279 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1280 * either, but will invalidate the fd on error, so this is again our only
1281 * option.
1283 * The 'resync-failed-syncs-after-fsync' xlator option for the
1284 * write-behind cache will cause later gluster versions to retain its
1285 * cache after error, so long as the fd remains open. However, we
1286 * currently have no way of knowing if this option is supported.
1288 * TODO: Once gluster provides a way for us to determine if the option
1289 * is supported, bypass the closure and setting drv to NULL. */
1290 qemu_gluster_close(bs);
1291 bs->drv = NULL;
1292 return ret;
1295 #ifdef CONFIG_GLUSTERFS_DISCARD
1296 static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs,
1297 int64_t offset, int64_t bytes)
1299 int ret;
1300 GlusterAIOCB acb;
1301 BDRVGlusterState *s = bs->opaque;
1303 assert(bytes <= SIZE_MAX); /* rely on max_pdiscard */
1305 acb.size = 0;
1306 acb.ret = 0;
1307 acb.coroutine = qemu_coroutine_self();
1308 acb.aio_context = bdrv_get_aio_context(bs);
1310 ret = glfs_discard_async(s->fd, offset, bytes, gluster_finish_aiocb, &acb);
1311 if (ret < 0) {
1312 return -errno;
1315 qemu_coroutine_yield();
1316 return acb.ret;
1318 #endif
1320 static int64_t coroutine_fn qemu_gluster_co_getlength(BlockDriverState *bs)
1322 BDRVGlusterState *s = bs->opaque;
1323 int64_t ret;
1325 ret = glfs_lseek(s->fd, 0, SEEK_END);
1326 if (ret < 0) {
1327 return -errno;
1328 } else {
1329 return ret;
1333 static int64_t coroutine_fn
1334 qemu_gluster_co_get_allocated_file_size(BlockDriverState *bs)
1336 BDRVGlusterState *s = bs->opaque;
1337 struct stat st;
1338 int ret;
1340 ret = glfs_fstat(s->fd, &st);
1341 if (ret < 0) {
1342 return -errno;
1343 } else {
1344 return st.st_blocks * 512;
1349 * Find allocation range in @bs around offset @start.
1350 * May change underlying file descriptor's file offset.
1351 * If @start is not in a hole, store @start in @data, and the
1352 * beginning of the next hole in @hole, and return 0.
1353 * If @start is in a non-trailing hole, store @start in @hole and the
1354 * beginning of the next non-hole in @data, and return 0.
1355 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1356 * If we can't find out, return a negative errno other than -ENXIO.
1358 * (Shamefully copied from file-posix.c, only minuscule adaptions.)
1360 static int find_allocation(BlockDriverState *bs, off_t start,
1361 off_t *data, off_t *hole)
1363 BDRVGlusterState *s = bs->opaque;
1365 if (!s->supports_seek_data) {
1366 goto exit;
1369 #if defined SEEK_HOLE && defined SEEK_DATA
1370 off_t offs;
1373 * SEEK_DATA cases:
1374 * D1. offs == start: start is in data
1375 * D2. offs > start: start is in a hole, next data at offs
1376 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1377 * or start is beyond EOF
1378 * If the latter happens, the file has been truncated behind
1379 * our back since we opened it. All bets are off then.
1380 * Treating like a trailing hole is simplest.
1381 * D4. offs < 0, errno != ENXIO: we learned nothing
1383 offs = glfs_lseek(s->fd, start, SEEK_DATA);
1384 if (offs < 0) {
1385 return -errno; /* D3 or D4 */
1388 if (offs < start) {
1389 /* This is not a valid return by lseek(). We are safe to just return
1390 * -EIO in this case, and we'll treat it like D4. Unfortunately some
1391 * versions of gluster server will return offs < start, so an assert
1392 * here will unnecessarily abort QEMU. */
1393 return -EIO;
1396 if (offs > start) {
1397 /* D2: in hole, next data at offs */
1398 *hole = start;
1399 *data = offs;
1400 return 0;
1403 /* D1: in data, end not yet known */
1406 * SEEK_HOLE cases:
1407 * H1. offs == start: start is in a hole
1408 * If this happens here, a hole has been dug behind our back
1409 * since the previous lseek().
1410 * H2. offs > start: either start is in data, next hole at offs,
1411 * or start is in trailing hole, EOF at offs
1412 * Linux treats trailing holes like any other hole: offs ==
1413 * start. Solaris seeks to EOF instead: offs > start (blech).
1414 * If that happens here, a hole has been dug behind our back
1415 * since the previous lseek().
1416 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1417 * If this happens, the file has been truncated behind our
1418 * back since we opened it. Treat it like a trailing hole.
1419 * H4. offs < 0, errno != ENXIO: we learned nothing
1420 * Pretend we know nothing at all, i.e. "forget" about D1.
1422 offs = glfs_lseek(s->fd, start, SEEK_HOLE);
1423 if (offs < 0) {
1424 return -errno; /* D1 and (H3 or H4) */
1427 if (offs < start) {
1428 /* This is not a valid return by lseek(). We are safe to just return
1429 * -EIO in this case, and we'll treat it like H4. Unfortunately some
1430 * versions of gluster server will return offs < start, so an assert
1431 * here will unnecessarily abort QEMU. */
1432 return -EIO;
1435 if (offs > start) {
1437 * D1 and H2: either in data, next hole at offs, or it was in
1438 * data but is now in a trailing hole. In the latter case,
1439 * all bets are off. Treating it as if it there was data all
1440 * the way to EOF is safe, so simply do that.
1442 *data = start;
1443 *hole = offs;
1444 return 0;
1447 /* D1 and H1 */
1448 return -EBUSY;
1449 #endif
1451 exit:
1452 return -ENOTSUP;
1456 * Returns the allocation status of the specified offset.
1458 * The block layer guarantees 'offset' and 'bytes' are within bounds.
1460 * 'pnum' is set to the number of bytes (including and immediately following
1461 * the specified offset) that are known to be in the same
1462 * allocated/unallocated state.
1464 * 'bytes' is a soft cap for 'pnum'. If the information is free, 'pnum' may
1465 * well exceed it.
1467 * (Based on raw_co_block_status() from file-posix.c.)
1469 static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs,
1470 bool want_zero,
1471 int64_t offset,
1472 int64_t bytes,
1473 int64_t *pnum,
1474 int64_t *map,
1475 BlockDriverState **file)
1477 BDRVGlusterState *s = bs->opaque;
1478 off_t data = 0, hole = 0;
1479 int ret = -EINVAL;
1481 assert(QEMU_IS_ALIGNED(offset | bytes, bs->bl.request_alignment));
1483 if (!s->fd) {
1484 return ret;
1487 if (!want_zero) {
1488 *pnum = bytes;
1489 *map = offset;
1490 *file = bs;
1491 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1494 ret = find_allocation(bs, offset, &data, &hole);
1495 if (ret == -ENXIO) {
1496 /* Trailing hole */
1497 *pnum = bytes;
1498 ret = BDRV_BLOCK_ZERO;
1499 } else if (ret < 0) {
1500 /* No info available, so pretend there are no holes */
1501 *pnum = bytes;
1502 ret = BDRV_BLOCK_DATA;
1503 } else if (data == offset) {
1504 /* On a data extent, compute bytes to the end of the extent,
1505 * possibly including a partial sector at EOF. */
1506 *pnum = hole - offset;
1509 * We are not allowed to return partial sectors, though, so
1510 * round up if necessary.
1512 if (!QEMU_IS_ALIGNED(*pnum, bs->bl.request_alignment)) {
1513 int64_t file_length = qemu_gluster_co_getlength(bs);
1514 if (file_length > 0) {
1515 /* Ignore errors, this is just a safeguard */
1516 assert(hole == file_length);
1518 *pnum = ROUND_UP(*pnum, bs->bl.request_alignment);
1521 ret = BDRV_BLOCK_DATA;
1522 } else {
1523 /* On a hole, compute bytes to the beginning of the next extent. */
1524 assert(hole == offset);
1525 *pnum = data - offset;
1526 ret = BDRV_BLOCK_ZERO;
1529 *map = offset;
1530 *file = bs;
1532 return ret | BDRV_BLOCK_OFFSET_VALID;
1536 static const char *const gluster_strong_open_opts[] = {
1537 GLUSTER_OPT_VOLUME,
1538 GLUSTER_OPT_PATH,
1539 GLUSTER_OPT_TYPE,
1540 GLUSTER_OPT_SERVER_PATTERN,
1541 GLUSTER_OPT_HOST,
1542 GLUSTER_OPT_PORT,
1543 GLUSTER_OPT_TO,
1544 GLUSTER_OPT_IPV4,
1545 GLUSTER_OPT_IPV6,
1546 GLUSTER_OPT_SOCKET,
1548 NULL
1551 static BlockDriver bdrv_gluster = {
1552 .format_name = "gluster",
1553 .protocol_name = "gluster",
1554 .instance_size = sizeof(BDRVGlusterState),
1555 .bdrv_file_open = qemu_gluster_open,
1556 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1557 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1558 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1559 .bdrv_close = qemu_gluster_close,
1560 .bdrv_co_create = qemu_gluster_co_create,
1561 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1562 .bdrv_co_getlength = qemu_gluster_co_getlength,
1563 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
1564 .bdrv_co_truncate = qemu_gluster_co_truncate,
1565 .bdrv_co_readv = qemu_gluster_co_readv,
1566 .bdrv_co_writev = qemu_gluster_co_writev,
1567 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1568 #ifdef CONFIG_GLUSTERFS_DISCARD
1569 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1570 #endif
1571 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1572 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1573 #endif
1574 .bdrv_co_block_status = qemu_gluster_co_block_status,
1575 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1576 .create_opts = &qemu_gluster_create_opts,
1577 .strong_runtime_opts = gluster_strong_open_opts,
1580 static BlockDriver bdrv_gluster_tcp = {
1581 .format_name = "gluster",
1582 .protocol_name = "gluster+tcp",
1583 .instance_size = sizeof(BDRVGlusterState),
1584 .bdrv_file_open = qemu_gluster_open,
1585 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1586 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1587 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1588 .bdrv_close = qemu_gluster_close,
1589 .bdrv_co_create = qemu_gluster_co_create,
1590 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1591 .bdrv_co_getlength = qemu_gluster_co_getlength,
1592 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
1593 .bdrv_co_truncate = qemu_gluster_co_truncate,
1594 .bdrv_co_readv = qemu_gluster_co_readv,
1595 .bdrv_co_writev = qemu_gluster_co_writev,
1596 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1597 #ifdef CONFIG_GLUSTERFS_DISCARD
1598 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1599 #endif
1600 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1601 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1602 #endif
1603 .bdrv_co_block_status = qemu_gluster_co_block_status,
1604 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1605 .create_opts = &qemu_gluster_create_opts,
1606 .strong_runtime_opts = gluster_strong_open_opts,
1609 static BlockDriver bdrv_gluster_unix = {
1610 .format_name = "gluster",
1611 .protocol_name = "gluster+unix",
1612 .instance_size = sizeof(BDRVGlusterState),
1613 .bdrv_file_open = qemu_gluster_open,
1614 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1615 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1616 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1617 .bdrv_close = qemu_gluster_close,
1618 .bdrv_co_create = qemu_gluster_co_create,
1619 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1620 .bdrv_co_getlength = qemu_gluster_co_getlength,
1621 .bdrv_co_get_allocated_file_size = qemu_gluster_co_get_allocated_file_size,
1622 .bdrv_co_truncate = qemu_gluster_co_truncate,
1623 .bdrv_co_readv = qemu_gluster_co_readv,
1624 .bdrv_co_writev = qemu_gluster_co_writev,
1625 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1626 #ifdef CONFIG_GLUSTERFS_DISCARD
1627 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1628 #endif
1629 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1630 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1631 #endif
1632 .bdrv_co_block_status = qemu_gluster_co_block_status,
1633 .bdrv_refresh_limits = qemu_gluster_refresh_limits,
1634 .create_opts = &qemu_gluster_create_opts,
1635 .strong_runtime_opts = gluster_strong_open_opts,
1638 static void bdrv_gluster_init(void)
1640 bdrv_register(&bdrv_gluster_unix);
1641 bdrv_register(&bdrv_gluster_tcp);
1642 bdrv_register(&bdrv_gluster);
1645 block_init(bdrv_gluster_init);