Merge remote-tracking branch 'remotes/jnsnow/tags/bitmaps-pull-request' into staging
[qemu/ar7.git] / block / gluster.c
bloba4e1c8ecd8abdc3403a71b9c2c24c2740291bdb1
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 <glusterfs/api/glfs.h>
13 #include "block/block_int.h"
14 #include "block/qdict.h"
15 #include "qapi/error.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/uri.h"
19 #include "qemu/error-report.h"
20 #include "qemu/option.h"
21 #include "qemu/cutils.h"
23 #define GLUSTER_OPT_FILENAME "filename"
24 #define GLUSTER_OPT_VOLUME "volume"
25 #define GLUSTER_OPT_PATH "path"
26 #define GLUSTER_OPT_TYPE "type"
27 #define GLUSTER_OPT_SERVER_PATTERN "server."
28 #define GLUSTER_OPT_HOST "host"
29 #define GLUSTER_OPT_PORT "port"
30 #define GLUSTER_OPT_TO "to"
31 #define GLUSTER_OPT_IPV4 "ipv4"
32 #define GLUSTER_OPT_IPV6 "ipv6"
33 #define GLUSTER_OPT_SOCKET "socket"
34 #define GLUSTER_OPT_DEBUG "debug"
35 #define GLUSTER_DEFAULT_PORT 24007
36 #define GLUSTER_DEBUG_DEFAULT 4
37 #define GLUSTER_DEBUG_MAX 9
38 #define GLUSTER_OPT_LOGFILE "logfile"
39 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
41 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
43 typedef struct GlusterAIOCB {
44 int64_t size;
45 int ret;
46 Coroutine *coroutine;
47 AioContext *aio_context;
48 } GlusterAIOCB;
50 typedef struct BDRVGlusterState {
51 struct glfs *glfs;
52 struct glfs_fd *fd;
53 char *logfile;
54 bool supports_seek_data;
55 int debug;
56 } BDRVGlusterState;
58 typedef struct BDRVGlusterReopenState {
59 struct glfs *glfs;
60 struct glfs_fd *fd;
61 } BDRVGlusterReopenState;
64 typedef struct GlfsPreopened {
65 char *volume;
66 glfs_t *fs;
67 int ref;
68 } GlfsPreopened;
70 typedef struct ListElement {
71 QLIST_ENTRY(ListElement) list;
72 GlfsPreopened saved;
73 } ListElement;
75 static QLIST_HEAD(glfs_list, ListElement) glfs_list;
77 static QemuOptsList qemu_gluster_create_opts = {
78 .name = "qemu-gluster-create-opts",
79 .head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
80 .desc = {
82 .name = BLOCK_OPT_SIZE,
83 .type = QEMU_OPT_SIZE,
84 .help = "Virtual disk size"
87 .name = BLOCK_OPT_PREALLOC,
88 .type = QEMU_OPT_STRING,
89 .help = "Preallocation mode (allowed values: off, full)"
92 .name = GLUSTER_OPT_DEBUG,
93 .type = QEMU_OPT_NUMBER,
94 .help = "Gluster log level, valid range is 0-9",
97 .name = GLUSTER_OPT_LOGFILE,
98 .type = QEMU_OPT_STRING,
99 .help = "Logfile path of libgfapi",
101 { /* end of list */ }
105 static QemuOptsList runtime_opts = {
106 .name = "gluster",
107 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
108 .desc = {
110 .name = GLUSTER_OPT_FILENAME,
111 .type = QEMU_OPT_STRING,
112 .help = "URL to the gluster image",
115 .name = GLUSTER_OPT_DEBUG,
116 .type = QEMU_OPT_NUMBER,
117 .help = "Gluster log level, valid range is 0-9",
120 .name = GLUSTER_OPT_LOGFILE,
121 .type = QEMU_OPT_STRING,
122 .help = "Logfile path of libgfapi",
124 { /* end of list */ }
128 static QemuOptsList runtime_json_opts = {
129 .name = "gluster_json",
130 .head = QTAILQ_HEAD_INITIALIZER(runtime_json_opts.head),
131 .desc = {
133 .name = GLUSTER_OPT_VOLUME,
134 .type = QEMU_OPT_STRING,
135 .help = "name of gluster volume where VM image resides",
138 .name = GLUSTER_OPT_PATH,
139 .type = QEMU_OPT_STRING,
140 .help = "absolute path to image file in gluster volume",
143 .name = GLUSTER_OPT_DEBUG,
144 .type = QEMU_OPT_NUMBER,
145 .help = "Gluster log level, valid range is 0-9",
147 { /* end of list */ }
151 static QemuOptsList runtime_type_opts = {
152 .name = "gluster_type",
153 .head = QTAILQ_HEAD_INITIALIZER(runtime_type_opts.head),
154 .desc = {
156 .name = GLUSTER_OPT_TYPE,
157 .type = QEMU_OPT_STRING,
158 .help = "inet|unix",
160 { /* end of list */ }
164 static QemuOptsList runtime_unix_opts = {
165 .name = "gluster_unix",
166 .head = QTAILQ_HEAD_INITIALIZER(runtime_unix_opts.head),
167 .desc = {
169 .name = GLUSTER_OPT_SOCKET,
170 .type = QEMU_OPT_STRING,
171 .help = "socket file path (legacy)",
174 .name = GLUSTER_OPT_PATH,
175 .type = QEMU_OPT_STRING,
176 .help = "socket file path (QAPI)",
178 { /* end of list */ }
182 static QemuOptsList runtime_inet_opts = {
183 .name = "gluster_inet",
184 .head = QTAILQ_HEAD_INITIALIZER(runtime_inet_opts.head),
185 .desc = {
187 .name = GLUSTER_OPT_TYPE,
188 .type = QEMU_OPT_STRING,
189 .help = "inet|unix",
192 .name = GLUSTER_OPT_HOST,
193 .type = QEMU_OPT_STRING,
194 .help = "host address (hostname/ipv4/ipv6 addresses)",
197 .name = GLUSTER_OPT_PORT,
198 .type = QEMU_OPT_STRING,
199 .help = "port number on which glusterd is listening (default 24007)",
202 .name = "to",
203 .type = QEMU_OPT_NUMBER,
204 .help = "max port number, not supported by gluster",
207 .name = "ipv4",
208 .type = QEMU_OPT_BOOL,
209 .help = "ipv4 bool value, not supported by gluster",
212 .name = "ipv6",
213 .type = QEMU_OPT_BOOL,
214 .help = "ipv6 bool value, not supported by gluster",
216 { /* end of list */ }
220 static void glfs_set_preopened(const char *volume, glfs_t *fs)
222 ListElement *entry = NULL;
224 entry = g_new(ListElement, 1);
226 entry->saved.volume = g_strdup(volume);
228 entry->saved.fs = fs;
229 entry->saved.ref = 1;
231 QLIST_INSERT_HEAD(&glfs_list, entry, list);
234 static glfs_t *glfs_find_preopened(const char *volume)
236 ListElement *entry = NULL;
238 QLIST_FOREACH(entry, &glfs_list, list) {
239 if (strcmp(entry->saved.volume, volume) == 0) {
240 entry->saved.ref++;
241 return entry->saved.fs;
245 return NULL;
248 static void glfs_clear_preopened(glfs_t *fs)
250 ListElement *entry = NULL;
251 ListElement *next;
253 if (fs == NULL) {
254 return;
257 QLIST_FOREACH_SAFE(entry, &glfs_list, list, next) {
258 if (entry->saved.fs == fs) {
259 if (--entry->saved.ref) {
260 return;
263 QLIST_REMOVE(entry, list);
265 glfs_fini(entry->saved.fs);
266 g_free(entry->saved.volume);
267 g_free(entry);
272 static int parse_volume_options(BlockdevOptionsGluster *gconf, char *path)
274 char *p, *q;
276 if (!path) {
277 return -EINVAL;
280 /* volume */
281 p = q = path + strspn(path, "/");
282 p += strcspn(p, "/");
283 if (*p == '\0') {
284 return -EINVAL;
286 gconf->volume = g_strndup(q, p - q);
288 /* path */
289 p += strspn(p, "/");
290 if (*p == '\0') {
291 return -EINVAL;
293 gconf->path = g_strdup(p);
294 return 0;
298 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
300 * 'gluster' is the protocol.
302 * 'transport' specifies the transport type used to connect to gluster
303 * management daemon (glusterd). Valid transport types are
304 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
306 * 'host' specifies the host where the volume file specification for
307 * the given volume resides. This can be either hostname or ipv4 address.
308 * If transport type is 'unix', then 'host' field should not be specified.
309 * The 'socket' field needs to be populated with the path to unix domain
310 * socket.
312 * 'port' is the port number on which glusterd is listening. This is optional
313 * and if not specified, QEMU will send 0 which will make gluster to use the
314 * default port. If the transport type is unix, then 'port' should not be
315 * specified.
317 * 'volume' is the name of the gluster volume which contains the VM image.
319 * 'path' is the path to the actual VM image that resides on gluster volume.
321 * Examples:
323 * file=gluster://1.2.3.4/testvol/a.img
324 * file=gluster+tcp://1.2.3.4/testvol/a.img
325 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
326 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
327 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
329 static int qemu_gluster_parse_uri(BlockdevOptionsGluster *gconf,
330 const char *filename)
332 SocketAddress *gsconf;
333 URI *uri;
334 QueryParams *qp = NULL;
335 bool is_unix = false;
336 int ret = 0;
338 uri = uri_parse(filename);
339 if (!uri) {
340 return -EINVAL;
343 gconf->server = g_new0(SocketAddressList, 1);
344 gconf->server->value = gsconf = g_new0(SocketAddress, 1);
346 /* transport */
347 if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
348 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
349 } else if (!strcmp(uri->scheme, "gluster+tcp")) {
350 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
351 } else if (!strcmp(uri->scheme, "gluster+unix")) {
352 gsconf->type = SOCKET_ADDRESS_TYPE_UNIX;
353 is_unix = true;
354 } else if (!strcmp(uri->scheme, "gluster+rdma")) {
355 gsconf->type = SOCKET_ADDRESS_TYPE_INET;
356 warn_report("rdma feature is not supported, falling back to tcp");
357 } else {
358 ret = -EINVAL;
359 goto out;
362 ret = parse_volume_options(gconf, uri->path);
363 if (ret < 0) {
364 goto out;
367 qp = query_params_parse(uri->query);
368 if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
369 ret = -EINVAL;
370 goto out;
373 if (is_unix) {
374 if (uri->server || uri->port) {
375 ret = -EINVAL;
376 goto out;
378 if (strcmp(qp->p[0].name, "socket")) {
379 ret = -EINVAL;
380 goto out;
382 gsconf->u.q_unix.path = g_strdup(qp->p[0].value);
383 } else {
384 gsconf->u.inet.host = g_strdup(uri->server ? uri->server : "localhost");
385 if (uri->port) {
386 gsconf->u.inet.port = g_strdup_printf("%d", uri->port);
387 } else {
388 gsconf->u.inet.port = g_strdup_printf("%d", GLUSTER_DEFAULT_PORT);
392 out:
393 if (qp) {
394 query_params_free(qp);
396 uri_free(uri);
397 return ret;
400 static struct glfs *qemu_gluster_glfs_init(BlockdevOptionsGluster *gconf,
401 Error **errp)
403 struct glfs *glfs;
404 int ret;
405 int old_errno;
406 SocketAddressList *server;
407 unsigned long long port;
409 glfs = glfs_find_preopened(gconf->volume);
410 if (glfs) {
411 return glfs;
414 glfs = glfs_new(gconf->volume);
415 if (!glfs) {
416 goto out;
419 glfs_set_preopened(gconf->volume, glfs);
421 for (server = gconf->server; server; server = server->next) {
422 switch (server->value->type) {
423 case SOCKET_ADDRESS_TYPE_UNIX:
424 ret = glfs_set_volfile_server(glfs, "unix",
425 server->value->u.q_unix.path, 0);
426 break;
427 case SOCKET_ADDRESS_TYPE_INET:
428 if (parse_uint_full(server->value->u.inet.port, &port, 10) < 0 ||
429 port > 65535) {
430 error_setg(errp, "'%s' is not a valid port number",
431 server->value->u.inet.port);
432 errno = EINVAL;
433 goto out;
435 ret = glfs_set_volfile_server(glfs, "tcp",
436 server->value->u.inet.host,
437 (int)port);
438 break;
439 case SOCKET_ADDRESS_TYPE_VSOCK:
440 case SOCKET_ADDRESS_TYPE_FD:
441 default:
442 abort();
445 if (ret < 0) {
446 goto out;
450 ret = glfs_set_logging(glfs, gconf->logfile, gconf->debug);
451 if (ret < 0) {
452 goto out;
455 ret = glfs_init(glfs);
456 if (ret) {
457 error_setg(errp, "Gluster connection for volume %s, path %s failed"
458 " to connect", gconf->volume, gconf->path);
459 for (server = gconf->server; server; server = server->next) {
460 if (server->value->type == SOCKET_ADDRESS_TYPE_UNIX) {
461 error_append_hint(errp, "hint: failed on socket %s ",
462 server->value->u.q_unix.path);
463 } else {
464 error_append_hint(errp, "hint: failed on host %s and port %s ",
465 server->value->u.inet.host,
466 server->value->u.inet.port);
470 error_append_hint(errp, "Please refer to gluster logs for more info\n");
472 /* glfs_init sometimes doesn't set errno although docs suggest that */
473 if (errno == 0) {
474 errno = EINVAL;
477 goto out;
479 return glfs;
481 out:
482 if (glfs) {
483 old_errno = errno;
484 glfs_clear_preopened(glfs);
485 errno = old_errno;
487 return NULL;
491 * Convert the json formatted command line into qapi.
493 static int qemu_gluster_parse_json(BlockdevOptionsGluster *gconf,
494 QDict *options, Error **errp)
496 QemuOpts *opts;
497 SocketAddress *gsconf = NULL;
498 SocketAddressList *curr = NULL;
499 QDict *backing_options = NULL;
500 Error *local_err = NULL;
501 char *str = NULL;
502 const char *ptr;
503 int i, type, num_servers;
505 /* create opts info from runtime_json_opts list */
506 opts = qemu_opts_create(&runtime_json_opts, NULL, 0, &error_abort);
507 qemu_opts_absorb_qdict(opts, options, &local_err);
508 if (local_err) {
509 goto out;
512 num_servers = qdict_array_entries(options, GLUSTER_OPT_SERVER_PATTERN);
513 if (num_servers < 1) {
514 error_setg(&local_err, QERR_MISSING_PARAMETER, "server");
515 goto out;
518 ptr = qemu_opt_get(opts, GLUSTER_OPT_VOLUME);
519 if (!ptr) {
520 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_VOLUME);
521 goto out;
523 gconf->volume = g_strdup(ptr);
525 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
526 if (!ptr) {
527 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_PATH);
528 goto out;
530 gconf->path = g_strdup(ptr);
531 qemu_opts_del(opts);
533 for (i = 0; i < num_servers; i++) {
534 str = g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN"%d.", i);
535 qdict_extract_subqdict(options, &backing_options, str);
537 /* create opts info from runtime_type_opts list */
538 opts = qemu_opts_create(&runtime_type_opts, NULL, 0, &error_abort);
539 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
540 if (local_err) {
541 goto out;
544 ptr = qemu_opt_get(opts, GLUSTER_OPT_TYPE);
545 if (!ptr) {
546 error_setg(&local_err, QERR_MISSING_PARAMETER, GLUSTER_OPT_TYPE);
547 error_append_hint(&local_err, GERR_INDEX_HINT, i);
548 goto out;
551 gsconf = g_new0(SocketAddress, 1);
552 if (!strcmp(ptr, "tcp")) {
553 ptr = "inet"; /* accept legacy "tcp" */
555 type = qapi_enum_parse(&SocketAddressType_lookup, ptr, -1, NULL);
556 if (type != SOCKET_ADDRESS_TYPE_INET
557 && type != SOCKET_ADDRESS_TYPE_UNIX) {
558 error_setg(&local_err,
559 "Parameter '%s' may be 'inet' or 'unix'",
560 GLUSTER_OPT_TYPE);
561 error_append_hint(&local_err, GERR_INDEX_HINT, i);
562 goto out;
564 gsconf->type = type;
565 qemu_opts_del(opts);
567 if (gsconf->type == SOCKET_ADDRESS_TYPE_INET) {
568 /* create opts info from runtime_inet_opts list */
569 opts = qemu_opts_create(&runtime_inet_opts, NULL, 0, &error_abort);
570 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
571 if (local_err) {
572 goto out;
575 ptr = qemu_opt_get(opts, GLUSTER_OPT_HOST);
576 if (!ptr) {
577 error_setg(&local_err, QERR_MISSING_PARAMETER,
578 GLUSTER_OPT_HOST);
579 error_append_hint(&local_err, GERR_INDEX_HINT, i);
580 goto out;
582 gsconf->u.inet.host = g_strdup(ptr);
583 ptr = qemu_opt_get(opts, GLUSTER_OPT_PORT);
584 if (!ptr) {
585 error_setg(&local_err, QERR_MISSING_PARAMETER,
586 GLUSTER_OPT_PORT);
587 error_append_hint(&local_err, GERR_INDEX_HINT, i);
588 goto out;
590 gsconf->u.inet.port = g_strdup(ptr);
592 /* defend for unsupported fields in InetSocketAddress,
593 * i.e. @ipv4, @ipv6 and @to
595 ptr = qemu_opt_get(opts, GLUSTER_OPT_TO);
596 if (ptr) {
597 gsconf->u.inet.has_to = true;
599 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV4);
600 if (ptr) {
601 gsconf->u.inet.has_ipv4 = true;
603 ptr = qemu_opt_get(opts, GLUSTER_OPT_IPV6);
604 if (ptr) {
605 gsconf->u.inet.has_ipv6 = true;
607 if (gsconf->u.inet.has_to) {
608 error_setg(&local_err, "Parameter 'to' not supported");
609 goto out;
611 if (gsconf->u.inet.has_ipv4 || gsconf->u.inet.has_ipv6) {
612 error_setg(&local_err, "Parameters 'ipv4/ipv6' not supported");
613 goto out;
615 qemu_opts_del(opts);
616 } else {
617 /* create opts info from runtime_unix_opts list */
618 opts = qemu_opts_create(&runtime_unix_opts, NULL, 0, &error_abort);
619 qemu_opts_absorb_qdict(opts, backing_options, &local_err);
620 if (local_err) {
621 goto out;
624 ptr = qemu_opt_get(opts, GLUSTER_OPT_PATH);
625 if (!ptr) {
626 ptr = qemu_opt_get(opts, GLUSTER_OPT_SOCKET);
627 } else if (qemu_opt_get(opts, GLUSTER_OPT_SOCKET)) {
628 error_setg(&local_err,
629 "Conflicting parameters 'path' and 'socket'");
630 error_append_hint(&local_err, GERR_INDEX_HINT, i);
631 goto out;
633 if (!ptr) {
634 error_setg(&local_err, QERR_MISSING_PARAMETER,
635 GLUSTER_OPT_PATH);
636 error_append_hint(&local_err, GERR_INDEX_HINT, i);
637 goto out;
639 gsconf->u.q_unix.path = g_strdup(ptr);
640 qemu_opts_del(opts);
643 if (gconf->server == NULL) {
644 gconf->server = g_new0(SocketAddressList, 1);
645 gconf->server->value = gsconf;
646 curr = gconf->server;
647 } else {
648 curr->next = g_new0(SocketAddressList, 1);
649 curr->next->value = gsconf;
650 curr = curr->next;
652 gsconf = NULL;
654 qobject_unref(backing_options);
655 backing_options = NULL;
656 g_free(str);
657 str = NULL;
660 return 0;
662 out:
663 error_propagate(errp, local_err);
664 qapi_free_SocketAddress(gsconf);
665 qemu_opts_del(opts);
666 g_free(str);
667 qobject_unref(backing_options);
668 errno = EINVAL;
669 return -errno;
672 /* Converts options given in @filename and the @options QDict into the QAPI
673 * object @gconf. */
674 static int qemu_gluster_parse(BlockdevOptionsGluster *gconf,
675 const char *filename,
676 QDict *options, Error **errp)
678 int ret;
679 if (filename) {
680 ret = qemu_gluster_parse_uri(gconf, filename);
681 if (ret < 0) {
682 error_setg(errp, "invalid URI %s", filename);
683 error_append_hint(errp, "Usage: file=gluster[+transport]://"
684 "[host[:port]]volume/path[?socket=...]"
685 "[,file.debug=N]"
686 "[,file.logfile=/path/filename.log]\n");
687 return ret;
689 } else {
690 ret = qemu_gluster_parse_json(gconf, options, errp);
691 if (ret < 0) {
692 error_append_hint(errp, "Usage: "
693 "-drive driver=qcow2,file.driver=gluster,"
694 "file.volume=testvol,file.path=/path/a.qcow2"
695 "[,file.debug=9]"
696 "[,file.logfile=/path/filename.log],"
697 "file.server.0.type=inet,"
698 "file.server.0.host=1.2.3.4,"
699 "file.server.0.port=24007,"
700 "file.server.1.transport=unix,"
701 "file.server.1.path=/var/run/glusterd.socket ..."
702 "\n");
703 return ret;
707 return 0;
710 static struct glfs *qemu_gluster_init(BlockdevOptionsGluster *gconf,
711 const char *filename,
712 QDict *options, Error **errp)
714 int ret;
716 ret = qemu_gluster_parse(gconf, filename, options, errp);
717 if (ret < 0) {
718 errno = -ret;
719 return NULL;
722 return qemu_gluster_glfs_init(gconf, errp);
726 * AIO callback routine called from GlusterFS thread.
728 static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg)
730 GlusterAIOCB *acb = (GlusterAIOCB *)arg;
732 if (!ret || ret == acb->size) {
733 acb->ret = 0; /* Success */
734 } else if (ret < 0) {
735 acb->ret = -errno; /* Read/Write failed */
736 } else {
737 acb->ret = -EIO; /* Partial read/write - fail it */
740 aio_co_schedule(acb->aio_context, acb->coroutine);
743 static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
745 assert(open_flags != NULL);
747 *open_flags |= O_BINARY;
749 if (bdrv_flags & BDRV_O_RDWR) {
750 *open_flags |= O_RDWR;
751 } else {
752 *open_flags |= O_RDONLY;
755 if ((bdrv_flags & BDRV_O_NOCACHE)) {
756 *open_flags |= O_DIRECT;
761 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
762 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
763 * - Corrected versions return -1 and set errno to EINVAL.
764 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
765 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
767 static bool qemu_gluster_test_seek(struct glfs_fd *fd)
769 off_t ret = 0;
771 #if defined SEEK_HOLE && defined SEEK_DATA
772 off_t eof;
774 eof = glfs_lseek(fd, 0, SEEK_END);
775 if (eof < 0) {
776 /* this should never occur */
777 return false;
780 /* this should always fail with ENXIO if SEEK_DATA is supported */
781 ret = glfs_lseek(fd, eof, SEEK_DATA);
782 #endif
784 return (ret < 0) && (errno == ENXIO);
787 static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
788 int bdrv_flags, Error **errp)
790 BDRVGlusterState *s = bs->opaque;
791 int open_flags = 0;
792 int ret = 0;
793 BlockdevOptionsGluster *gconf = NULL;
794 QemuOpts *opts;
795 Error *local_err = NULL;
796 const char *filename, *logfile;
798 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
799 qemu_opts_absorb_qdict(opts, options, &local_err);
800 if (local_err) {
801 error_propagate(errp, local_err);
802 ret = -EINVAL;
803 goto out;
806 filename = qemu_opt_get(opts, GLUSTER_OPT_FILENAME);
808 s->debug = qemu_opt_get_number(opts, GLUSTER_OPT_DEBUG,
809 GLUSTER_DEBUG_DEFAULT);
810 if (s->debug < 0) {
811 s->debug = 0;
812 } else if (s->debug > GLUSTER_DEBUG_MAX) {
813 s->debug = GLUSTER_DEBUG_MAX;
816 gconf = g_new0(BlockdevOptionsGluster, 1);
817 gconf->debug = s->debug;
818 gconf->has_debug = true;
820 logfile = qemu_opt_get(opts, GLUSTER_OPT_LOGFILE);
821 s->logfile = g_strdup(logfile ? logfile : GLUSTER_LOGFILE_DEFAULT);
823 gconf->logfile = g_strdup(s->logfile);
824 gconf->has_logfile = true;
826 s->glfs = qemu_gluster_init(gconf, filename, options, errp);
827 if (!s->glfs) {
828 ret = -errno;
829 goto out;
832 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
833 /* Without this, if fsync fails for a recoverable reason (for instance,
834 * ENOSPC), gluster will dump its cache, preventing retries. This means
835 * almost certain data loss. Not all gluster versions support the
836 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
837 * discover during runtime if it is supported (this api returns success for
838 * unknown key/value pairs) */
839 ret = glfs_set_xlator_option(s->glfs, "*-write-behind",
840 "resync-failed-syncs-after-fsync",
841 "on");
842 if (ret < 0) {
843 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
844 ret = -errno;
845 goto out;
847 #endif
849 qemu_gluster_parse_flags(bdrv_flags, &open_flags);
851 s->fd = glfs_open(s->glfs, gconf->path, open_flags);
852 if (!s->fd) {
853 ret = -errno;
856 s->supports_seek_data = qemu_gluster_test_seek(s->fd);
858 out:
859 qemu_opts_del(opts);
860 qapi_free_BlockdevOptionsGluster(gconf);
861 if (!ret) {
862 return ret;
864 g_free(s->logfile);
865 if (s->fd) {
866 glfs_close(s->fd);
869 glfs_clear_preopened(s->glfs);
871 return ret;
874 static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
875 BlockReopenQueue *queue, Error **errp)
877 int ret = 0;
878 BDRVGlusterState *s;
879 BDRVGlusterReopenState *reop_s;
880 BlockdevOptionsGluster *gconf;
881 int open_flags = 0;
883 assert(state != NULL);
884 assert(state->bs != NULL);
886 s = state->bs->opaque;
888 state->opaque = g_new0(BDRVGlusterReopenState, 1);
889 reop_s = state->opaque;
891 qemu_gluster_parse_flags(state->flags, &open_flags);
893 gconf = g_new0(BlockdevOptionsGluster, 1);
894 gconf->debug = s->debug;
895 gconf->has_debug = true;
896 gconf->logfile = g_strdup(s->logfile);
897 gconf->has_logfile = true;
898 reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename, NULL, errp);
899 if (reop_s->glfs == NULL) {
900 ret = -errno;
901 goto exit;
904 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
905 ret = glfs_set_xlator_option(reop_s->glfs, "*-write-behind",
906 "resync-failed-syncs-after-fsync", "on");
907 if (ret < 0) {
908 error_setg_errno(errp, errno, "Unable to set xlator key/value pair");
909 ret = -errno;
910 goto exit;
912 #endif
914 reop_s->fd = glfs_open(reop_s->glfs, gconf->path, open_flags);
915 if (reop_s->fd == NULL) {
916 /* reops->glfs will be cleaned up in _abort */
917 ret = -errno;
918 goto exit;
921 exit:
922 /* state->opaque will be freed in either the _abort or _commit */
923 qapi_free_BlockdevOptionsGluster(gconf);
924 return ret;
927 static void qemu_gluster_reopen_commit(BDRVReopenState *state)
929 BDRVGlusterReopenState *reop_s = state->opaque;
930 BDRVGlusterState *s = state->bs->opaque;
933 /* close the old */
934 if (s->fd) {
935 glfs_close(s->fd);
938 glfs_clear_preopened(s->glfs);
940 /* use the newly opened image / connection */
941 s->fd = reop_s->fd;
942 s->glfs = reop_s->glfs;
944 g_free(state->opaque);
945 state->opaque = NULL;
947 return;
951 static void qemu_gluster_reopen_abort(BDRVReopenState *state)
953 BDRVGlusterReopenState *reop_s = state->opaque;
955 if (reop_s == NULL) {
956 return;
959 if (reop_s->fd) {
960 glfs_close(reop_s->fd);
963 glfs_clear_preopened(reop_s->glfs);
965 g_free(state->opaque);
966 state->opaque = NULL;
968 return;
971 #ifdef CONFIG_GLUSTERFS_ZEROFILL
972 static coroutine_fn int qemu_gluster_co_pwrite_zeroes(BlockDriverState *bs,
973 int64_t offset,
974 int size,
975 BdrvRequestFlags flags)
977 int ret;
978 GlusterAIOCB acb;
979 BDRVGlusterState *s = bs->opaque;
981 acb.size = size;
982 acb.ret = 0;
983 acb.coroutine = qemu_coroutine_self();
984 acb.aio_context = bdrv_get_aio_context(bs);
986 ret = glfs_zerofill_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
987 if (ret < 0) {
988 return -errno;
991 qemu_coroutine_yield();
992 return acb.ret;
994 #endif
996 static int qemu_gluster_do_truncate(struct glfs_fd *fd, int64_t offset,
997 PreallocMode prealloc, Error **errp)
999 int64_t current_length;
1001 current_length = glfs_lseek(fd, 0, SEEK_END);
1002 if (current_length < 0) {
1003 error_setg_errno(errp, errno, "Failed to determine current size");
1004 return -errno;
1007 if (current_length > offset && prealloc != PREALLOC_MODE_OFF) {
1008 error_setg(errp, "Cannot use preallocation for shrinking files");
1009 return -ENOTSUP;
1012 if (current_length == offset) {
1013 return 0;
1016 switch (prealloc) {
1017 #ifdef CONFIG_GLUSTERFS_FALLOCATE
1018 case PREALLOC_MODE_FALLOC:
1019 if (glfs_fallocate(fd, 0, current_length, offset - current_length)) {
1020 error_setg_errno(errp, errno, "Could not preallocate data");
1021 return -errno;
1023 break;
1024 #endif /* CONFIG_GLUSTERFS_FALLOCATE */
1025 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1026 case PREALLOC_MODE_FULL:
1027 if (glfs_ftruncate(fd, offset)) {
1028 error_setg_errno(errp, errno, "Could not resize file");
1029 return -errno;
1031 if (glfs_zerofill(fd, current_length, offset - current_length)) {
1032 error_setg_errno(errp, errno, "Could not zerofill the new area");
1033 return -errno;
1035 break;
1036 #endif /* CONFIG_GLUSTERFS_ZEROFILL */
1037 case PREALLOC_MODE_OFF:
1038 if (glfs_ftruncate(fd, offset)) {
1039 error_setg_errno(errp, errno, "Could not resize file");
1040 return -errno;
1042 break;
1043 default:
1044 error_setg(errp, "Unsupported preallocation mode: %s",
1045 PreallocMode_str(prealloc));
1046 return -EINVAL;
1049 return 0;
1052 static int qemu_gluster_co_create(BlockdevCreateOptions *options,
1053 Error **errp)
1055 BlockdevCreateOptionsGluster *opts = &options->u.gluster;
1056 struct glfs *glfs;
1057 struct glfs_fd *fd = NULL;
1058 int ret = 0;
1060 assert(options->driver == BLOCKDEV_DRIVER_GLUSTER);
1062 glfs = qemu_gluster_glfs_init(opts->location, errp);
1063 if (!glfs) {
1064 ret = -errno;
1065 goto out;
1068 fd = glfs_creat(glfs, opts->location->path,
1069 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
1070 if (!fd) {
1071 ret = -errno;
1072 goto out;
1075 ret = qemu_gluster_do_truncate(fd, opts->size, opts->preallocation, errp);
1077 out:
1078 if (fd) {
1079 if (glfs_close(fd) != 0 && ret == 0) {
1080 ret = -errno;
1083 glfs_clear_preopened(glfs);
1084 return ret;
1087 static int coroutine_fn qemu_gluster_co_create_opts(const char *filename,
1088 QemuOpts *opts,
1089 Error **errp)
1091 BlockdevCreateOptions *options;
1092 BlockdevCreateOptionsGluster *gopts;
1093 BlockdevOptionsGluster *gconf;
1094 char *tmp = NULL;
1095 Error *local_err = NULL;
1096 int ret;
1098 options = g_new0(BlockdevCreateOptions, 1);
1099 options->driver = BLOCKDEV_DRIVER_GLUSTER;
1100 gopts = &options->u.gluster;
1102 gconf = g_new0(BlockdevOptionsGluster, 1);
1103 gopts->location = gconf;
1105 gopts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
1106 BDRV_SECTOR_SIZE);
1108 tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
1109 gopts->preallocation = qapi_enum_parse(&PreallocMode_lookup, tmp,
1110 PREALLOC_MODE_OFF, &local_err);
1111 g_free(tmp);
1112 if (local_err) {
1113 error_propagate(errp, local_err);
1114 ret = -EINVAL;
1115 goto fail;
1118 gconf->debug = qemu_opt_get_number_del(opts, GLUSTER_OPT_DEBUG,
1119 GLUSTER_DEBUG_DEFAULT);
1120 if (gconf->debug < 0) {
1121 gconf->debug = 0;
1122 } else if (gconf->debug > GLUSTER_DEBUG_MAX) {
1123 gconf->debug = GLUSTER_DEBUG_MAX;
1125 gconf->has_debug = true;
1127 gconf->logfile = qemu_opt_get_del(opts, GLUSTER_OPT_LOGFILE);
1128 if (!gconf->logfile) {
1129 gconf->logfile = g_strdup(GLUSTER_LOGFILE_DEFAULT);
1131 gconf->has_logfile = true;
1133 ret = qemu_gluster_parse(gconf, filename, NULL, errp);
1134 if (ret < 0) {
1135 goto fail;
1138 ret = qemu_gluster_co_create(options, errp);
1139 if (ret < 0) {
1140 goto fail;
1143 ret = 0;
1144 fail:
1145 qapi_free_BlockdevCreateOptions(options);
1146 return ret;
1149 static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
1150 int64_t sector_num, int nb_sectors,
1151 QEMUIOVector *qiov, int write)
1153 int ret;
1154 GlusterAIOCB acb;
1155 BDRVGlusterState *s = bs->opaque;
1156 size_t size = nb_sectors * BDRV_SECTOR_SIZE;
1157 off_t offset = sector_num * BDRV_SECTOR_SIZE;
1159 acb.size = size;
1160 acb.ret = 0;
1161 acb.coroutine = qemu_coroutine_self();
1162 acb.aio_context = bdrv_get_aio_context(bs);
1164 if (write) {
1165 ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1166 gluster_finish_aiocb, &acb);
1167 } else {
1168 ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
1169 gluster_finish_aiocb, &acb);
1172 if (ret < 0) {
1173 return -errno;
1176 qemu_coroutine_yield();
1177 return acb.ret;
1180 static coroutine_fn int qemu_gluster_co_truncate(BlockDriverState *bs,
1181 int64_t offset,
1182 PreallocMode prealloc,
1183 Error **errp)
1185 BDRVGlusterState *s = bs->opaque;
1186 return qemu_gluster_do_truncate(s->fd, offset, prealloc, errp);
1189 static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
1190 int64_t sector_num,
1191 int nb_sectors,
1192 QEMUIOVector *qiov)
1194 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
1197 static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
1198 int64_t sector_num,
1199 int nb_sectors,
1200 QEMUIOVector *qiov,
1201 int flags)
1203 assert(!flags);
1204 return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
1207 static void qemu_gluster_close(BlockDriverState *bs)
1209 BDRVGlusterState *s = bs->opaque;
1211 g_free(s->logfile);
1212 if (s->fd) {
1213 glfs_close(s->fd);
1214 s->fd = NULL;
1216 glfs_clear_preopened(s->glfs);
1219 static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
1221 int ret;
1222 GlusterAIOCB acb;
1223 BDRVGlusterState *s = bs->opaque;
1225 acb.size = 0;
1226 acb.ret = 0;
1227 acb.coroutine = qemu_coroutine_self();
1228 acb.aio_context = bdrv_get_aio_context(bs);
1230 ret = glfs_fsync_async(s->fd, gluster_finish_aiocb, &acb);
1231 if (ret < 0) {
1232 ret = -errno;
1233 goto error;
1236 qemu_coroutine_yield();
1237 if (acb.ret < 0) {
1238 ret = acb.ret;
1239 goto error;
1242 return acb.ret;
1244 error:
1245 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1246 * after a fsync failure, so we have no way of allowing the guest to safely
1247 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1248 * either, but will invalidate the fd on error, so this is again our only
1249 * option.
1251 * The 'resync-failed-syncs-after-fsync' xlator option for the
1252 * write-behind cache will cause later gluster versions to retain its
1253 * cache after error, so long as the fd remains open. However, we
1254 * currently have no way of knowing if this option is supported.
1256 * TODO: Once gluster provides a way for us to determine if the option
1257 * is supported, bypass the closure and setting drv to NULL. */
1258 qemu_gluster_close(bs);
1259 bs->drv = NULL;
1260 return ret;
1263 #ifdef CONFIG_GLUSTERFS_DISCARD
1264 static coroutine_fn int qemu_gluster_co_pdiscard(BlockDriverState *bs,
1265 int64_t offset, int size)
1267 int ret;
1268 GlusterAIOCB acb;
1269 BDRVGlusterState *s = bs->opaque;
1271 acb.size = 0;
1272 acb.ret = 0;
1273 acb.coroutine = qemu_coroutine_self();
1274 acb.aio_context = bdrv_get_aio_context(bs);
1276 ret = glfs_discard_async(s->fd, offset, size, gluster_finish_aiocb, &acb);
1277 if (ret < 0) {
1278 return -errno;
1281 qemu_coroutine_yield();
1282 return acb.ret;
1284 #endif
1286 static int64_t qemu_gluster_getlength(BlockDriverState *bs)
1288 BDRVGlusterState *s = bs->opaque;
1289 int64_t ret;
1291 ret = glfs_lseek(s->fd, 0, SEEK_END);
1292 if (ret < 0) {
1293 return -errno;
1294 } else {
1295 return ret;
1299 static int64_t qemu_gluster_allocated_file_size(BlockDriverState *bs)
1301 BDRVGlusterState *s = bs->opaque;
1302 struct stat st;
1303 int ret;
1305 ret = glfs_fstat(s->fd, &st);
1306 if (ret < 0) {
1307 return -errno;
1308 } else {
1309 return st.st_blocks * 512;
1313 static int qemu_gluster_has_zero_init(BlockDriverState *bs)
1315 /* GlusterFS volume could be backed by a block device */
1316 return 0;
1320 * Find allocation range in @bs around offset @start.
1321 * May change underlying file descriptor's file offset.
1322 * If @start is not in a hole, store @start in @data, and the
1323 * beginning of the next hole in @hole, and return 0.
1324 * If @start is in a non-trailing hole, store @start in @hole and the
1325 * beginning of the next non-hole in @data, and return 0.
1326 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1327 * If we can't find out, return a negative errno other than -ENXIO.
1329 * (Shamefully copied from file-posix.c, only miniscule adaptions.)
1331 static int find_allocation(BlockDriverState *bs, off_t start,
1332 off_t *data, off_t *hole)
1334 BDRVGlusterState *s = bs->opaque;
1336 if (!s->supports_seek_data) {
1337 goto exit;
1340 #if defined SEEK_HOLE && defined SEEK_DATA
1341 off_t offs;
1344 * SEEK_DATA cases:
1345 * D1. offs == start: start is in data
1346 * D2. offs > start: start is in a hole, next data at offs
1347 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1348 * or start is beyond EOF
1349 * If the latter happens, the file has been truncated behind
1350 * our back since we opened it. All bets are off then.
1351 * Treating like a trailing hole is simplest.
1352 * D4. offs < 0, errno != ENXIO: we learned nothing
1354 offs = glfs_lseek(s->fd, start, SEEK_DATA);
1355 if (offs < 0) {
1356 return -errno; /* D3 or D4 */
1359 if (offs < start) {
1360 /* This is not a valid return by lseek(). We are safe to just return
1361 * -EIO in this case, and we'll treat it like D4. Unfortunately some
1362 * versions of gluster server will return offs < start, so an assert
1363 * here will unnecessarily abort QEMU. */
1364 return -EIO;
1367 if (offs > start) {
1368 /* D2: in hole, next data at offs */
1369 *hole = start;
1370 *data = offs;
1371 return 0;
1374 /* D1: in data, end not yet known */
1377 * SEEK_HOLE cases:
1378 * H1. offs == start: start is in a hole
1379 * If this happens here, a hole has been dug behind our back
1380 * since the previous lseek().
1381 * H2. offs > start: either start is in data, next hole at offs,
1382 * or start is in trailing hole, EOF at offs
1383 * Linux treats trailing holes like any other hole: offs ==
1384 * start. Solaris seeks to EOF instead: offs > start (blech).
1385 * If that happens here, a hole has been dug behind our back
1386 * since the previous lseek().
1387 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1388 * If this happens, the file has been truncated behind our
1389 * back since we opened it. Treat it like a trailing hole.
1390 * H4. offs < 0, errno != ENXIO: we learned nothing
1391 * Pretend we know nothing at all, i.e. "forget" about D1.
1393 offs = glfs_lseek(s->fd, start, SEEK_HOLE);
1394 if (offs < 0) {
1395 return -errno; /* D1 and (H3 or H4) */
1398 if (offs < start) {
1399 /* This is not a valid return by lseek(). We are safe to just return
1400 * -EIO in this case, and we'll treat it like H4. Unfortunately some
1401 * versions of gluster server will return offs < start, so an assert
1402 * here will unnecessarily abort QEMU. */
1403 return -EIO;
1406 if (offs > start) {
1408 * D1 and H2: either in data, next hole at offs, or it was in
1409 * data but is now in a trailing hole. In the latter case,
1410 * all bets are off. Treating it as if it there was data all
1411 * the way to EOF is safe, so simply do that.
1413 *data = start;
1414 *hole = offs;
1415 return 0;
1418 /* D1 and H1 */
1419 return -EBUSY;
1420 #endif
1422 exit:
1423 return -ENOTSUP;
1427 * Returns the allocation status of the specified offset.
1429 * The block layer guarantees 'offset' and 'bytes' are within bounds.
1431 * 'pnum' is set to the number of bytes (including and immediately following
1432 * the specified offset) that are known to be in the same
1433 * allocated/unallocated state.
1435 * 'bytes' is the max value 'pnum' should be set to.
1437 * (Based on raw_co_block_status() from file-posix.c.)
1439 static int coroutine_fn qemu_gluster_co_block_status(BlockDriverState *bs,
1440 bool want_zero,
1441 int64_t offset,
1442 int64_t bytes,
1443 int64_t *pnum,
1444 int64_t *map,
1445 BlockDriverState **file)
1447 BDRVGlusterState *s = bs->opaque;
1448 off_t data = 0, hole = 0;
1449 int ret = -EINVAL;
1451 if (!s->fd) {
1452 return ret;
1455 if (!want_zero) {
1456 *pnum = bytes;
1457 *map = offset;
1458 *file = bs;
1459 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
1462 ret = find_allocation(bs, offset, &data, &hole);
1463 if (ret == -ENXIO) {
1464 /* Trailing hole */
1465 *pnum = bytes;
1466 ret = BDRV_BLOCK_ZERO;
1467 } else if (ret < 0) {
1468 /* No info available, so pretend there are no holes */
1469 *pnum = bytes;
1470 ret = BDRV_BLOCK_DATA;
1471 } else if (data == offset) {
1472 /* On a data extent, compute bytes to the end of the extent,
1473 * possibly including a partial sector at EOF. */
1474 *pnum = MIN(bytes, hole - offset);
1475 ret = BDRV_BLOCK_DATA;
1476 } else {
1477 /* On a hole, compute bytes to the beginning of the next extent. */
1478 assert(hole == offset);
1479 *pnum = MIN(bytes, data - offset);
1480 ret = BDRV_BLOCK_ZERO;
1483 *map = offset;
1484 *file = bs;
1486 return ret | BDRV_BLOCK_OFFSET_VALID;
1490 static BlockDriver bdrv_gluster = {
1491 .format_name = "gluster",
1492 .protocol_name = "gluster",
1493 .instance_size = sizeof(BDRVGlusterState),
1494 .bdrv_needs_filename = false,
1495 .bdrv_file_open = qemu_gluster_open,
1496 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1497 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1498 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1499 .bdrv_close = qemu_gluster_close,
1500 .bdrv_co_create = qemu_gluster_co_create,
1501 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1502 .bdrv_getlength = qemu_gluster_getlength,
1503 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1504 .bdrv_co_truncate = qemu_gluster_co_truncate,
1505 .bdrv_co_readv = qemu_gluster_co_readv,
1506 .bdrv_co_writev = qemu_gluster_co_writev,
1507 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1508 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1509 #ifdef CONFIG_GLUSTERFS_DISCARD
1510 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1511 #endif
1512 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1513 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1514 #endif
1515 .bdrv_co_block_status = qemu_gluster_co_block_status,
1516 .create_opts = &qemu_gluster_create_opts,
1519 static BlockDriver bdrv_gluster_tcp = {
1520 .format_name = "gluster",
1521 .protocol_name = "gluster+tcp",
1522 .instance_size = sizeof(BDRVGlusterState),
1523 .bdrv_needs_filename = false,
1524 .bdrv_file_open = qemu_gluster_open,
1525 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1526 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1527 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1528 .bdrv_close = qemu_gluster_close,
1529 .bdrv_co_create = qemu_gluster_co_create,
1530 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1531 .bdrv_getlength = qemu_gluster_getlength,
1532 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1533 .bdrv_co_truncate = qemu_gluster_co_truncate,
1534 .bdrv_co_readv = qemu_gluster_co_readv,
1535 .bdrv_co_writev = qemu_gluster_co_writev,
1536 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1537 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1538 #ifdef CONFIG_GLUSTERFS_DISCARD
1539 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1540 #endif
1541 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1542 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1543 #endif
1544 .bdrv_co_block_status = qemu_gluster_co_block_status,
1545 .create_opts = &qemu_gluster_create_opts,
1548 static BlockDriver bdrv_gluster_unix = {
1549 .format_name = "gluster",
1550 .protocol_name = "gluster+unix",
1551 .instance_size = sizeof(BDRVGlusterState),
1552 .bdrv_needs_filename = true,
1553 .bdrv_file_open = qemu_gluster_open,
1554 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1555 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1556 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1557 .bdrv_close = qemu_gluster_close,
1558 .bdrv_co_create = qemu_gluster_co_create,
1559 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1560 .bdrv_getlength = qemu_gluster_getlength,
1561 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1562 .bdrv_co_truncate = qemu_gluster_co_truncate,
1563 .bdrv_co_readv = qemu_gluster_co_readv,
1564 .bdrv_co_writev = qemu_gluster_co_writev,
1565 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1566 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1567 #ifdef CONFIG_GLUSTERFS_DISCARD
1568 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1569 #endif
1570 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1571 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1572 #endif
1573 .bdrv_co_block_status = qemu_gluster_co_block_status,
1574 .create_opts = &qemu_gluster_create_opts,
1577 /* rdma is deprecated (actually never supported for volfile fetch).
1578 * Let's maintain it for the protocol compatibility, to make sure things
1579 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1580 * protocol with a warning.
1581 * TODO: remove gluster+rdma interface support
1583 static BlockDriver bdrv_gluster_rdma = {
1584 .format_name = "gluster",
1585 .protocol_name = "gluster+rdma",
1586 .instance_size = sizeof(BDRVGlusterState),
1587 .bdrv_needs_filename = true,
1588 .bdrv_file_open = qemu_gluster_open,
1589 .bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
1590 .bdrv_reopen_commit = qemu_gluster_reopen_commit,
1591 .bdrv_reopen_abort = qemu_gluster_reopen_abort,
1592 .bdrv_close = qemu_gluster_close,
1593 .bdrv_co_create = qemu_gluster_co_create,
1594 .bdrv_co_create_opts = qemu_gluster_co_create_opts,
1595 .bdrv_getlength = qemu_gluster_getlength,
1596 .bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
1597 .bdrv_co_truncate = qemu_gluster_co_truncate,
1598 .bdrv_co_readv = qemu_gluster_co_readv,
1599 .bdrv_co_writev = qemu_gluster_co_writev,
1600 .bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
1601 .bdrv_has_zero_init = qemu_gluster_has_zero_init,
1602 #ifdef CONFIG_GLUSTERFS_DISCARD
1603 .bdrv_co_pdiscard = qemu_gluster_co_pdiscard,
1604 #endif
1605 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1606 .bdrv_co_pwrite_zeroes = qemu_gluster_co_pwrite_zeroes,
1607 #endif
1608 .bdrv_co_block_status = qemu_gluster_co_block_status,
1609 .create_opts = &qemu_gluster_create_opts,
1612 static void bdrv_gluster_init(void)
1614 bdrv_register(&bdrv_gluster_rdma);
1615 bdrv_register(&bdrv_gluster_unix);
1616 bdrv_register(&bdrv_gluster_tcp);
1617 bdrv_register(&bdrv_gluster);
1620 block_init(bdrv_gluster_init);