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.
10 #include "qemu/osdep.h"
11 #include <glusterfs/api/glfs.h>
12 #include "block/block_int.h"
13 #include "qapi/error.h"
14 #include "qapi/qmp/qerror.h"
15 #include "qapi/util.h"
17 #include "qemu/error-report.h"
18 #include "qemu/cutils.h"
20 #define GLUSTER_OPT_FILENAME "filename"
21 #define GLUSTER_OPT_VOLUME "volume"
22 #define GLUSTER_OPT_PATH "path"
23 #define GLUSTER_OPT_TYPE "type"
24 #define GLUSTER_OPT_SERVER_PATTERN "server."
25 #define GLUSTER_OPT_HOST "host"
26 #define GLUSTER_OPT_PORT "port"
27 #define GLUSTER_OPT_TO "to"
28 #define GLUSTER_OPT_IPV4 "ipv4"
29 #define GLUSTER_OPT_IPV6 "ipv6"
30 #define GLUSTER_OPT_SOCKET "socket"
31 #define GLUSTER_OPT_DEBUG "debug"
32 #define GLUSTER_DEFAULT_PORT 24007
33 #define GLUSTER_DEBUG_DEFAULT 4
34 #define GLUSTER_DEBUG_MAX 9
35 #define GLUSTER_OPT_LOGFILE "logfile"
36 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
38 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
40 typedef struct GlusterAIOCB
{
44 AioContext
*aio_context
;
47 typedef struct BDRVGlusterState
{
51 bool supports_seek_data
;
55 typedef struct BDRVGlusterReopenState
{
58 } BDRVGlusterReopenState
;
61 typedef struct GlfsPreopened
{
67 typedef struct ListElement
{
68 QLIST_ENTRY(ListElement
) list
;
72 static QLIST_HEAD(glfs_list
, ListElement
) glfs_list
;
74 static QemuOptsList qemu_gluster_create_opts
= {
75 .name
= "qemu-gluster-create-opts",
76 .head
= QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts
.head
),
79 .name
= BLOCK_OPT_SIZE
,
80 .type
= QEMU_OPT_SIZE
,
81 .help
= "Virtual disk size"
84 .name
= BLOCK_OPT_PREALLOC
,
85 .type
= QEMU_OPT_STRING
,
86 .help
= "Preallocation mode (allowed values: off, full)"
89 .name
= GLUSTER_OPT_DEBUG
,
90 .type
= QEMU_OPT_NUMBER
,
91 .help
= "Gluster log level, valid range is 0-9",
94 .name
= GLUSTER_OPT_LOGFILE
,
95 .type
= QEMU_OPT_STRING
,
96 .help
= "Logfile path of libgfapi",
102 static QemuOptsList runtime_opts
= {
104 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
107 .name
= GLUSTER_OPT_FILENAME
,
108 .type
= QEMU_OPT_STRING
,
109 .help
= "URL to the gluster image",
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_json_opts
= {
126 .name
= "gluster_json",
127 .head
= QTAILQ_HEAD_INITIALIZER(runtime_json_opts
.head
),
130 .name
= GLUSTER_OPT_VOLUME
,
131 .type
= QEMU_OPT_STRING
,
132 .help
= "name of gluster volume where VM image resides",
135 .name
= GLUSTER_OPT_PATH
,
136 .type
= QEMU_OPT_STRING
,
137 .help
= "absolute path to image file in gluster volume",
140 .name
= GLUSTER_OPT_DEBUG
,
141 .type
= QEMU_OPT_NUMBER
,
142 .help
= "Gluster log level, valid range is 0-9",
144 { /* end of list */ }
148 static QemuOptsList runtime_type_opts
= {
149 .name
= "gluster_type",
150 .head
= QTAILQ_HEAD_INITIALIZER(runtime_type_opts
.head
),
153 .name
= GLUSTER_OPT_TYPE
,
154 .type
= QEMU_OPT_STRING
,
157 { /* end of list */ }
161 static QemuOptsList runtime_unix_opts
= {
162 .name
= "gluster_unix",
163 .head
= QTAILQ_HEAD_INITIALIZER(runtime_unix_opts
.head
),
166 .name
= GLUSTER_OPT_SOCKET
,
167 .type
= QEMU_OPT_STRING
,
168 .help
= "socket file path)",
170 { /* end of list */ }
174 static QemuOptsList runtime_inet_opts
= {
175 .name
= "gluster_inet",
176 .head
= QTAILQ_HEAD_INITIALIZER(runtime_inet_opts
.head
),
179 .name
= GLUSTER_OPT_TYPE
,
180 .type
= QEMU_OPT_STRING
,
184 .name
= GLUSTER_OPT_HOST
,
185 .type
= QEMU_OPT_STRING
,
186 .help
= "host address (hostname/ipv4/ipv6 addresses)",
189 .name
= GLUSTER_OPT_PORT
,
190 .type
= QEMU_OPT_STRING
,
191 .help
= "port number on which glusterd is listening (default 24007)",
195 .type
= QEMU_OPT_NUMBER
,
196 .help
= "max port number, not supported by gluster",
200 .type
= QEMU_OPT_BOOL
,
201 .help
= "ipv4 bool value, not supported by gluster",
205 .type
= QEMU_OPT_BOOL
,
206 .help
= "ipv6 bool value, not supported by gluster",
208 { /* end of list */ }
212 static void glfs_set_preopened(const char *volume
, glfs_t
*fs
)
214 ListElement
*entry
= NULL
;
216 entry
= g_new(ListElement
, 1);
218 entry
->saved
.volume
= g_strdup(volume
);
220 entry
->saved
.fs
= fs
;
221 entry
->saved
.ref
= 1;
223 QLIST_INSERT_HEAD(&glfs_list
, entry
, list
);
226 static glfs_t
*glfs_find_preopened(const char *volume
)
228 ListElement
*entry
= NULL
;
230 QLIST_FOREACH(entry
, &glfs_list
, list
) {
231 if (strcmp(entry
->saved
.volume
, volume
) == 0) {
233 return entry
->saved
.fs
;
240 static void glfs_clear_preopened(glfs_t
*fs
)
242 ListElement
*entry
= NULL
;
249 QLIST_FOREACH_SAFE(entry
, &glfs_list
, list
, next
) {
250 if (entry
->saved
.fs
== fs
) {
251 if (--entry
->saved
.ref
) {
255 QLIST_REMOVE(entry
, list
);
257 glfs_fini(entry
->saved
.fs
);
258 g_free(entry
->saved
.volume
);
264 static int parse_volume_options(BlockdevOptionsGluster
*gconf
, char *path
)
273 p
= q
= path
+ strspn(path
, "/");
274 p
+= strcspn(p
, "/");
278 gconf
->volume
= g_strndup(q
, p
- q
);
285 gconf
->path
= g_strdup(p
);
290 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
292 * 'gluster' is the protocol.
294 * 'transport' specifies the transport type used to connect to gluster
295 * management daemon (glusterd). Valid transport types are
296 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
298 * 'host' specifies the host where the volume file specification for
299 * the given volume resides. This can be either hostname or ipv4 address.
300 * If transport type is 'unix', then 'host' field should not be specified.
301 * The 'socket' field needs to be populated with the path to unix domain
304 * 'port' is the port number on which glusterd is listening. This is optional
305 * and if not specified, QEMU will send 0 which will make gluster to use the
306 * default port. If the transport type is unix, then 'port' should not be
309 * 'volume' is the name of the gluster volume which contains the VM image.
311 * 'path' is the path to the actual VM image that resides on gluster volume.
315 * file=gluster://1.2.3.4/testvol/a.img
316 * file=gluster+tcp://1.2.3.4/testvol/a.img
317 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
318 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
319 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
321 static int qemu_gluster_parse_uri(BlockdevOptionsGluster
*gconf
,
322 const char *filename
)
324 SocketAddress
*gsconf
;
326 QueryParams
*qp
= NULL
;
327 bool is_unix
= false;
330 uri
= uri_parse(filename
);
335 gconf
->server
= g_new0(SocketAddressList
, 1);
336 gconf
->server
->value
= gsconf
= g_new0(SocketAddress
, 1);
339 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
340 gsconf
->type
= SOCKET_ADDRESS_TYPE_INET
;
341 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
342 gsconf
->type
= SOCKET_ADDRESS_TYPE_INET
;
343 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
344 gsconf
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
346 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
347 gsconf
->type
= SOCKET_ADDRESS_TYPE_INET
;
348 warn_report("rdma feature is not supported, falling back to tcp");
354 ret
= parse_volume_options(gconf
, uri
->path
);
359 qp
= query_params_parse(uri
->query
);
360 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
366 if (uri
->server
|| uri
->port
) {
370 if (strcmp(qp
->p
[0].name
, "socket")) {
374 gsconf
->u
.q_unix
.path
= g_strdup(qp
->p
[0].value
);
376 gsconf
->u
.inet
.host
= g_strdup(uri
->server
? uri
->server
: "localhost");
378 gsconf
->u
.inet
.port
= g_strdup_printf("%d", uri
->port
);
380 gsconf
->u
.inet
.port
= g_strdup_printf("%d", GLUSTER_DEFAULT_PORT
);
386 query_params_free(qp
);
392 static struct glfs
*qemu_gluster_glfs_init(BlockdevOptionsGluster
*gconf
,
398 SocketAddressList
*server
;
399 unsigned long long port
;
401 glfs
= glfs_find_preopened(gconf
->volume
);
406 glfs
= glfs_new(gconf
->volume
);
411 glfs_set_preopened(gconf
->volume
, glfs
);
413 for (server
= gconf
->server
; server
; server
= server
->next
) {
414 switch (server
->value
->type
) {
415 case SOCKET_ADDRESS_TYPE_UNIX
:
416 ret
= glfs_set_volfile_server(glfs
, "unix",
417 server
->value
->u
.q_unix
.path
, 0);
419 case SOCKET_ADDRESS_TYPE_INET
:
420 if (parse_uint_full(server
->value
->u
.inet
.port
, &port
, 10) < 0 ||
422 error_setg(errp
, "'%s' is not a valid port number",
423 server
->value
->u
.inet
.port
);
427 ret
= glfs_set_volfile_server(glfs
, "tcp",
428 server
->value
->u
.inet
.host
,
431 case SOCKET_ADDRESS_TYPE_VSOCK
:
432 case SOCKET_ADDRESS_TYPE_FD
:
442 ret
= glfs_set_logging(glfs
, gconf
->logfile
, gconf
->debug
);
447 ret
= glfs_init(glfs
);
449 error_setg(errp
, "Gluster connection for volume %s, path %s failed"
450 " to connect", gconf
->volume
, gconf
->path
);
451 for (server
= gconf
->server
; server
; server
= server
->next
) {
452 if (server
->value
->type
== SOCKET_ADDRESS_TYPE_UNIX
) {
453 error_append_hint(errp
, "hint: failed on socket %s ",
454 server
->value
->u
.q_unix
.path
);
456 error_append_hint(errp
, "hint: failed on host %s and port %s ",
457 server
->value
->u
.inet
.host
,
458 server
->value
->u
.inet
.port
);
462 error_append_hint(errp
, "Please refer to gluster logs for more info\n");
464 /* glfs_init sometimes doesn't set errno although docs suggest that */
476 glfs_clear_preopened(glfs
);
483 * Convert the json formatted command line into qapi.
485 static int qemu_gluster_parse_json(BlockdevOptionsGluster
*gconf
,
486 QDict
*options
, Error
**errp
)
489 SocketAddress
*gsconf
= NULL
;
490 SocketAddressList
*curr
= NULL
;
491 QDict
*backing_options
= NULL
;
492 Error
*local_err
= NULL
;
495 int i
, type
, num_servers
;
497 /* create opts info from runtime_json_opts list */
498 opts
= qemu_opts_create(&runtime_json_opts
, NULL
, 0, &error_abort
);
499 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
504 num_servers
= qdict_array_entries(options
, GLUSTER_OPT_SERVER_PATTERN
);
505 if (num_servers
< 1) {
506 error_setg(&local_err
, QERR_MISSING_PARAMETER
, "server");
510 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_VOLUME
);
512 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_VOLUME
);
515 gconf
->volume
= g_strdup(ptr
);
517 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PATH
);
519 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_PATH
);
522 gconf
->path
= g_strdup(ptr
);
525 for (i
= 0; i
< num_servers
; i
++) {
526 str
= g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN
"%d.", i
);
527 qdict_extract_subqdict(options
, &backing_options
, str
);
529 /* create opts info from runtime_type_opts list */
530 opts
= qemu_opts_create(&runtime_type_opts
, NULL
, 0, &error_abort
);
531 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
536 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TYPE
);
538 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_TYPE
);
539 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
543 gsconf
= g_new0(SocketAddress
, 1);
544 if (!strcmp(ptr
, "tcp")) {
545 ptr
= "inet"; /* accept legacy "tcp" */
547 type
= qapi_enum_parse(SocketAddressType_lookup
, ptr
,
548 SOCKET_ADDRESS_TYPE__MAX
, -1, NULL
);
549 if (type
!= SOCKET_ADDRESS_TYPE_INET
550 && type
!= SOCKET_ADDRESS_TYPE_UNIX
) {
551 error_setg(&local_err
,
552 "Parameter '%s' may be 'inet' or 'unix'",
554 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
560 if (gsconf
->type
== SOCKET_ADDRESS_TYPE_INET
) {
561 /* create opts info from runtime_inet_opts list */
562 opts
= qemu_opts_create(&runtime_inet_opts
, NULL
, 0, &error_abort
);
563 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
568 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_HOST
);
570 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
572 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
575 gsconf
->u
.inet
.host
= g_strdup(ptr
);
576 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PORT
);
578 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
580 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
583 gsconf
->u
.inet
.port
= g_strdup(ptr
);
585 /* defend for unsupported fields in InetSocketAddress,
586 * i.e. @ipv4, @ipv6 and @to
588 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TO
);
590 gsconf
->u
.inet
.has_to
= true;
592 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV4
);
594 gsconf
->u
.inet
.has_ipv4
= true;
596 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV6
);
598 gsconf
->u
.inet
.has_ipv6
= true;
600 if (gsconf
->u
.inet
.has_to
) {
601 error_setg(&local_err
, "Parameter 'to' not supported");
604 if (gsconf
->u
.inet
.has_ipv4
|| gsconf
->u
.inet
.has_ipv6
) {
605 error_setg(&local_err
, "Parameters 'ipv4/ipv6' not supported");
610 /* create opts info from runtime_unix_opts list */
611 opts
= qemu_opts_create(&runtime_unix_opts
, NULL
, 0, &error_abort
);
612 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
617 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_SOCKET
);
619 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
621 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
624 gsconf
->u
.q_unix
.path
= g_strdup(ptr
);
628 if (gconf
->server
== NULL
) {
629 gconf
->server
= g_new0(SocketAddressList
, 1);
630 gconf
->server
->value
= gsconf
;
631 curr
= gconf
->server
;
633 curr
->next
= g_new0(SocketAddressList
, 1);
634 curr
->next
->value
= gsconf
;
639 QDECREF(backing_options
);
640 backing_options
= NULL
;
648 error_propagate(errp
, local_err
);
649 qapi_free_SocketAddress(gsconf
);
652 QDECREF(backing_options
);
657 static struct glfs
*qemu_gluster_init(BlockdevOptionsGluster
*gconf
,
658 const char *filename
,
659 QDict
*options
, Error
**errp
)
663 ret
= qemu_gluster_parse_uri(gconf
, filename
);
665 error_setg(errp
, "invalid URI");
666 error_append_hint(errp
, "Usage: file=gluster[+transport]://"
667 "[host[:port]]volume/path[?socket=...]"
669 "[,file.logfile=/path/filename.log]\n");
674 ret
= qemu_gluster_parse_json(gconf
, options
, errp
);
676 error_append_hint(errp
, "Usage: "
677 "-drive driver=qcow2,file.driver=gluster,"
678 "file.volume=testvol,file.path=/path/a.qcow2"
680 "[,file.logfile=/path/filename.log],"
681 "file.server.0.type=inet,"
682 "file.server.0.host=1.2.3.4,"
683 "file.server.0.port=24007,"
684 "file.server.1.transport=unix,"
685 "file.server.1.socket=/var/run/glusterd.socket ..."
693 return qemu_gluster_glfs_init(gconf
, errp
);
697 * AIO callback routine called from GlusterFS thread.
699 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
701 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
703 if (!ret
|| ret
== acb
->size
) {
704 acb
->ret
= 0; /* Success */
705 } else if (ret
< 0) {
706 acb
->ret
= -errno
; /* Read/Write failed */
708 acb
->ret
= -EIO
; /* Partial read/write - fail it */
711 aio_co_schedule(acb
->aio_context
, acb
->coroutine
);
714 static void qemu_gluster_parse_flags(int bdrv_flags
, int *open_flags
)
716 assert(open_flags
!= NULL
);
718 *open_flags
|= O_BINARY
;
720 if (bdrv_flags
& BDRV_O_RDWR
) {
721 *open_flags
|= O_RDWR
;
723 *open_flags
|= O_RDONLY
;
726 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
727 *open_flags
|= O_DIRECT
;
732 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
733 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
734 * - Corrected versions return -1 and set errno to EINVAL.
735 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
736 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
738 static bool qemu_gluster_test_seek(struct glfs_fd
*fd
)
742 #if defined SEEK_HOLE && defined SEEK_DATA
745 eof
= glfs_lseek(fd
, 0, SEEK_END
);
747 /* this should never occur */
751 /* this should always fail with ENXIO if SEEK_DATA is supported */
752 ret
= glfs_lseek(fd
, eof
, SEEK_DATA
);
755 return (ret
< 0) && (errno
== ENXIO
);
758 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
759 int bdrv_flags
, Error
**errp
)
761 BDRVGlusterState
*s
= bs
->opaque
;
764 BlockdevOptionsGluster
*gconf
= NULL
;
766 Error
*local_err
= NULL
;
767 const char *filename
, *logfile
;
769 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
770 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
772 error_propagate(errp
, local_err
);
777 filename
= qemu_opt_get(opts
, GLUSTER_OPT_FILENAME
);
779 s
->debug
= qemu_opt_get_number(opts
, GLUSTER_OPT_DEBUG
,
780 GLUSTER_DEBUG_DEFAULT
);
783 } else if (s
->debug
> GLUSTER_DEBUG_MAX
) {
784 s
->debug
= GLUSTER_DEBUG_MAX
;
787 gconf
= g_new0(BlockdevOptionsGluster
, 1);
788 gconf
->debug
= s
->debug
;
789 gconf
->has_debug
= true;
791 logfile
= qemu_opt_get(opts
, GLUSTER_OPT_LOGFILE
);
792 s
->logfile
= g_strdup(logfile
? logfile
: GLUSTER_LOGFILE_DEFAULT
);
794 gconf
->logfile
= g_strdup(s
->logfile
);
795 gconf
->has_logfile
= true;
797 s
->glfs
= qemu_gluster_init(gconf
, filename
, options
, errp
);
803 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
804 /* Without this, if fsync fails for a recoverable reason (for instance,
805 * ENOSPC), gluster will dump its cache, preventing retries. This means
806 * almost certain data loss. Not all gluster versions support the
807 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
808 * discover during runtime if it is supported (this api returns success for
809 * unknown key/value pairs) */
810 ret
= glfs_set_xlator_option(s
->glfs
, "*-write-behind",
811 "resync-failed-syncs-after-fsync",
814 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
820 qemu_gluster_parse_flags(bdrv_flags
, &open_flags
);
822 s
->fd
= glfs_open(s
->glfs
, gconf
->path
, open_flags
);
827 s
->supports_seek_data
= qemu_gluster_test_seek(s
->fd
);
831 qapi_free_BlockdevOptionsGluster(gconf
);
840 glfs_clear_preopened(s
->glfs
);
845 static int qemu_gluster_reopen_prepare(BDRVReopenState
*state
,
846 BlockReopenQueue
*queue
, Error
**errp
)
850 BDRVGlusterReopenState
*reop_s
;
851 BlockdevOptionsGluster
*gconf
;
854 assert(state
!= NULL
);
855 assert(state
->bs
!= NULL
);
857 s
= state
->bs
->opaque
;
859 state
->opaque
= g_new0(BDRVGlusterReopenState
, 1);
860 reop_s
= state
->opaque
;
862 qemu_gluster_parse_flags(state
->flags
, &open_flags
);
864 gconf
= g_new0(BlockdevOptionsGluster
, 1);
865 gconf
->debug
= s
->debug
;
866 gconf
->has_debug
= true;
867 gconf
->logfile
= g_strdup(s
->logfile
);
868 gconf
->has_logfile
= true;
869 reop_s
->glfs
= qemu_gluster_init(gconf
, state
->bs
->filename
, NULL
, errp
);
870 if (reop_s
->glfs
== NULL
) {
875 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
876 ret
= glfs_set_xlator_option(reop_s
->glfs
, "*-write-behind",
877 "resync-failed-syncs-after-fsync", "on");
879 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
885 reop_s
->fd
= glfs_open(reop_s
->glfs
, gconf
->path
, open_flags
);
886 if (reop_s
->fd
== NULL
) {
887 /* reops->glfs will be cleaned up in _abort */
893 /* state->opaque will be freed in either the _abort or _commit */
894 qapi_free_BlockdevOptionsGluster(gconf
);
898 static void qemu_gluster_reopen_commit(BDRVReopenState
*state
)
900 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
901 BDRVGlusterState
*s
= state
->bs
->opaque
;
909 glfs_clear_preopened(s
->glfs
);
911 /* use the newly opened image / connection */
913 s
->glfs
= reop_s
->glfs
;
915 g_free(state
->opaque
);
916 state
->opaque
= NULL
;
922 static void qemu_gluster_reopen_abort(BDRVReopenState
*state
)
924 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
926 if (reop_s
== NULL
) {
931 glfs_close(reop_s
->fd
);
934 glfs_clear_preopened(reop_s
->glfs
);
936 g_free(state
->opaque
);
937 state
->opaque
= NULL
;
942 #ifdef CONFIG_GLUSTERFS_ZEROFILL
943 static coroutine_fn
int qemu_gluster_co_pwrite_zeroes(BlockDriverState
*bs
,
946 BdrvRequestFlags flags
)
950 BDRVGlusterState
*s
= bs
->opaque
;
954 acb
.coroutine
= qemu_coroutine_self();
955 acb
.aio_context
= bdrv_get_aio_context(bs
);
957 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
962 qemu_coroutine_yield();
967 static int qemu_gluster_create(const char *filename
,
968 QemuOpts
*opts
, Error
**errp
)
970 BlockdevOptionsGluster
*gconf
;
974 PreallocMode prealloc
;
975 int64_t total_size
= 0;
977 Error
*local_err
= NULL
;
979 gconf
= g_new0(BlockdevOptionsGluster
, 1);
980 gconf
->debug
= qemu_opt_get_number_del(opts
, GLUSTER_OPT_DEBUG
,
981 GLUSTER_DEBUG_DEFAULT
);
982 if (gconf
->debug
< 0) {
984 } else if (gconf
->debug
> GLUSTER_DEBUG_MAX
) {
985 gconf
->debug
= GLUSTER_DEBUG_MAX
;
987 gconf
->has_debug
= true;
989 gconf
->logfile
= qemu_opt_get_del(opts
, GLUSTER_OPT_LOGFILE
);
990 if (!gconf
->logfile
) {
991 gconf
->logfile
= g_strdup(GLUSTER_LOGFILE_DEFAULT
);
993 gconf
->has_logfile
= true;
995 glfs
= qemu_gluster_init(gconf
, filename
, NULL
, errp
);
1001 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
1004 tmp
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
1005 prealloc
= qapi_enum_parse(PreallocMode_lookup
, tmp
,
1006 PREALLOC_MODE__MAX
, PREALLOC_MODE_OFF
,
1010 error_propagate(errp
, local_err
);
1015 fd
= glfs_creat(glfs
, gconf
->path
,
1016 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
1023 #ifdef CONFIG_GLUSTERFS_FALLOCATE
1024 case PREALLOC_MODE_FALLOC
:
1025 if (glfs_fallocate(fd
, 0, 0, total_size
)) {
1026 error_setg(errp
, "Could not preallocate data for the new file");
1030 #endif /* CONFIG_GLUSTERFS_FALLOCATE */
1031 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1032 case PREALLOC_MODE_FULL
:
1033 if (!glfs_ftruncate(fd
, total_size
)) {
1034 if (glfs_zerofill(fd
, 0, total_size
)) {
1035 error_setg(errp
, "Could not zerofill the new file");
1039 error_setg(errp
, "Could not resize file");
1043 #endif /* CONFIG_GLUSTERFS_ZEROFILL */
1044 case PREALLOC_MODE_OFF
:
1045 if (glfs_ftruncate(fd
, total_size
) != 0) {
1047 error_setg(errp
, "Could not resize file");
1052 error_setg(errp
, "Unsupported preallocation mode: %s",
1053 PreallocMode_lookup
[prealloc
]);
1057 if (glfs_close(fd
) != 0) {
1061 qapi_free_BlockdevOptionsGluster(gconf
);
1062 glfs_clear_preopened(glfs
);
1066 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
1067 int64_t sector_num
, int nb_sectors
,
1068 QEMUIOVector
*qiov
, int write
)
1072 BDRVGlusterState
*s
= bs
->opaque
;
1073 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
1074 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
1078 acb
.coroutine
= qemu_coroutine_self();
1079 acb
.aio_context
= bdrv_get_aio_context(bs
);
1082 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
1083 gluster_finish_aiocb
, &acb
);
1085 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
1086 gluster_finish_aiocb
, &acb
);
1093 qemu_coroutine_yield();
1097 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
,
1098 PreallocMode prealloc
, Error
**errp
)
1101 BDRVGlusterState
*s
= bs
->opaque
;
1103 if (prealloc
!= PREALLOC_MODE_OFF
) {
1104 error_setg(errp
, "Unsupported preallocation mode '%s'",
1105 PreallocMode_lookup
[prealloc
]);
1109 ret
= glfs_ftruncate(s
->fd
, offset
);
1112 error_setg_errno(errp
, -ret
, "Failed to truncate file");
1119 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
1124 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
1127 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
1132 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
1135 static void qemu_gluster_close(BlockDriverState
*bs
)
1137 BDRVGlusterState
*s
= bs
->opaque
;
1144 glfs_clear_preopened(s
->glfs
);
1147 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
1151 BDRVGlusterState
*s
= bs
->opaque
;
1155 acb
.coroutine
= qemu_coroutine_self();
1156 acb
.aio_context
= bdrv_get_aio_context(bs
);
1158 ret
= glfs_fsync_async(s
->fd
, gluster_finish_aiocb
, &acb
);
1164 qemu_coroutine_yield();
1173 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1174 * after a fsync failure, so we have no way of allowing the guest to safely
1175 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1176 * either, but will invalidate the fd on error, so this is again our only
1179 * The 'resync-failed-syncs-after-fsync' xlator option for the
1180 * write-behind cache will cause later gluster versions to retain its
1181 * cache after error, so long as the fd remains open. However, we
1182 * currently have no way of knowing if this option is supported.
1184 * TODO: Once gluster provides a way for us to determine if the option
1185 * is supported, bypass the closure and setting drv to NULL. */
1186 qemu_gluster_close(bs
);
1191 #ifdef CONFIG_GLUSTERFS_DISCARD
1192 static coroutine_fn
int qemu_gluster_co_pdiscard(BlockDriverState
*bs
,
1193 int64_t offset
, int size
)
1197 BDRVGlusterState
*s
= bs
->opaque
;
1201 acb
.coroutine
= qemu_coroutine_self();
1202 acb
.aio_context
= bdrv_get_aio_context(bs
);
1204 ret
= glfs_discard_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
1209 qemu_coroutine_yield();
1214 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
1216 BDRVGlusterState
*s
= bs
->opaque
;
1219 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
1227 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
1229 BDRVGlusterState
*s
= bs
->opaque
;
1233 ret
= glfs_fstat(s
->fd
, &st
);
1237 return st
.st_blocks
* 512;
1241 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
1243 /* GlusterFS volume could be backed by a block device */
1248 * Find allocation range in @bs around offset @start.
1249 * May change underlying file descriptor's file offset.
1250 * If @start is not in a hole, store @start in @data, and the
1251 * beginning of the next hole in @hole, and return 0.
1252 * If @start is in a non-trailing hole, store @start in @hole and the
1253 * beginning of the next non-hole in @data, and return 0.
1254 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1255 * If we can't find out, return a negative errno other than -ENXIO.
1257 * (Shamefully copied from file-posix.c, only miniscule adaptions.)
1259 static int find_allocation(BlockDriverState
*bs
, off_t start
,
1260 off_t
*data
, off_t
*hole
)
1262 BDRVGlusterState
*s
= bs
->opaque
;
1264 if (!s
->supports_seek_data
) {
1268 #if defined SEEK_HOLE && defined SEEK_DATA
1273 * D1. offs == start: start is in data
1274 * D2. offs > start: start is in a hole, next data at offs
1275 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1276 * or start is beyond EOF
1277 * If the latter happens, the file has been truncated behind
1278 * our back since we opened it. All bets are off then.
1279 * Treating like a trailing hole is simplest.
1280 * D4. offs < 0, errno != ENXIO: we learned nothing
1282 offs
= glfs_lseek(s
->fd
, start
, SEEK_DATA
);
1284 return -errno
; /* D3 or D4 */
1288 /* This is not a valid return by lseek(). We are safe to just return
1289 * -EIO in this case, and we'll treat it like D4. Unfortunately some
1290 * versions of gluster server will return offs < start, so an assert
1291 * here will unnecessarily abort QEMU. */
1296 /* D2: in hole, next data at offs */
1302 /* D1: in data, end not yet known */
1306 * H1. offs == start: start is in a hole
1307 * If this happens here, a hole has been dug behind our back
1308 * since the previous lseek().
1309 * H2. offs > start: either start is in data, next hole at offs,
1310 * or start is in trailing hole, EOF at offs
1311 * Linux treats trailing holes like any other hole: offs ==
1312 * start. Solaris seeks to EOF instead: offs > start (blech).
1313 * If that happens here, a hole has been dug behind our back
1314 * since the previous lseek().
1315 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1316 * If this happens, the file has been truncated behind our
1317 * back since we opened it. Treat it like a trailing hole.
1318 * H4. offs < 0, errno != ENXIO: we learned nothing
1319 * Pretend we know nothing at all, i.e. "forget" about D1.
1321 offs
= glfs_lseek(s
->fd
, start
, SEEK_HOLE
);
1323 return -errno
; /* D1 and (H3 or H4) */
1327 /* This is not a valid return by lseek(). We are safe to just return
1328 * -EIO in this case, and we'll treat it like H4. Unfortunately some
1329 * versions of gluster server will return offs < start, so an assert
1330 * here will unnecessarily abort QEMU. */
1336 * D1 and H2: either in data, next hole at offs, or it was in
1337 * data but is now in a trailing hole. In the latter case,
1338 * all bets are off. Treating it as if it there was data all
1339 * the way to EOF is safe, so simply do that.
1355 * Returns the allocation status of the specified sectors.
1357 * If 'sector_num' is beyond the end of the disk image the return value is 0
1358 * and 'pnum' is set to 0.
1360 * 'pnum' is set to the number of sectors (including and immediately following
1361 * the specified sector) that are known to be in the same
1362 * allocated/unallocated state.
1364 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1365 * beyond the end of the disk image it will be clamped.
1367 * (Based on raw_co_get_block_status() from file-posix.c.)
1369 static int64_t coroutine_fn
qemu_gluster_co_get_block_status(
1370 BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
, int *pnum
,
1371 BlockDriverState
**file
)
1373 BDRVGlusterState
*s
= bs
->opaque
;
1374 off_t start
, data
= 0, hole
= 0;
1382 start
= sector_num
* BDRV_SECTOR_SIZE
;
1383 total_size
= bdrv_getlength(bs
);
1384 if (total_size
< 0) {
1386 } else if (start
>= total_size
) {
1389 } else if (start
+ nb_sectors
* BDRV_SECTOR_SIZE
> total_size
) {
1390 nb_sectors
= DIV_ROUND_UP(total_size
- start
, BDRV_SECTOR_SIZE
);
1393 ret
= find_allocation(bs
, start
, &data
, &hole
);
1394 if (ret
== -ENXIO
) {
1397 ret
= BDRV_BLOCK_ZERO
;
1398 } else if (ret
< 0) {
1399 /* No info available, so pretend there are no holes */
1401 ret
= BDRV_BLOCK_DATA
;
1402 } else if (data
== start
) {
1403 /* On a data extent, compute sectors to the end of the extent,
1404 * possibly including a partial sector at EOF. */
1405 *pnum
= MIN(nb_sectors
, DIV_ROUND_UP(hole
- start
, BDRV_SECTOR_SIZE
));
1406 ret
= BDRV_BLOCK_DATA
;
1408 /* On a hole, compute sectors to the beginning of the next extent. */
1409 assert(hole
== start
);
1410 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1411 ret
= BDRV_BLOCK_ZERO
;
1416 return ret
| BDRV_BLOCK_OFFSET_VALID
| start
;
1420 static BlockDriver bdrv_gluster
= {
1421 .format_name
= "gluster",
1422 .protocol_name
= "gluster",
1423 .instance_size
= sizeof(BDRVGlusterState
),
1424 .bdrv_needs_filename
= false,
1425 .bdrv_file_open
= qemu_gluster_open
,
1426 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1427 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1428 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1429 .bdrv_close
= qemu_gluster_close
,
1430 .bdrv_create
= qemu_gluster_create
,
1431 .bdrv_getlength
= qemu_gluster_getlength
,
1432 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1433 .bdrv_truncate
= qemu_gluster_truncate
,
1434 .bdrv_co_readv
= qemu_gluster_co_readv
,
1435 .bdrv_co_writev
= qemu_gluster_co_writev
,
1436 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1437 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1438 #ifdef CONFIG_GLUSTERFS_DISCARD
1439 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1441 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1442 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1444 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1445 .create_opts
= &qemu_gluster_create_opts
,
1448 static BlockDriver bdrv_gluster_tcp
= {
1449 .format_name
= "gluster",
1450 .protocol_name
= "gluster+tcp",
1451 .instance_size
= sizeof(BDRVGlusterState
),
1452 .bdrv_needs_filename
= false,
1453 .bdrv_file_open
= qemu_gluster_open
,
1454 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1455 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1456 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1457 .bdrv_close
= qemu_gluster_close
,
1458 .bdrv_create
= qemu_gluster_create
,
1459 .bdrv_getlength
= qemu_gluster_getlength
,
1460 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1461 .bdrv_truncate
= qemu_gluster_truncate
,
1462 .bdrv_co_readv
= qemu_gluster_co_readv
,
1463 .bdrv_co_writev
= qemu_gluster_co_writev
,
1464 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1465 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1466 #ifdef CONFIG_GLUSTERFS_DISCARD
1467 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1469 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1470 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1472 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1473 .create_opts
= &qemu_gluster_create_opts
,
1476 static BlockDriver bdrv_gluster_unix
= {
1477 .format_name
= "gluster",
1478 .protocol_name
= "gluster+unix",
1479 .instance_size
= sizeof(BDRVGlusterState
),
1480 .bdrv_needs_filename
= true,
1481 .bdrv_file_open
= qemu_gluster_open
,
1482 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1483 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1484 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1485 .bdrv_close
= qemu_gluster_close
,
1486 .bdrv_create
= qemu_gluster_create
,
1487 .bdrv_getlength
= qemu_gluster_getlength
,
1488 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1489 .bdrv_truncate
= qemu_gluster_truncate
,
1490 .bdrv_co_readv
= qemu_gluster_co_readv
,
1491 .bdrv_co_writev
= qemu_gluster_co_writev
,
1492 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1493 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1494 #ifdef CONFIG_GLUSTERFS_DISCARD
1495 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1497 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1498 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1500 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1501 .create_opts
= &qemu_gluster_create_opts
,
1504 /* rdma is deprecated (actually never supported for volfile fetch).
1505 * Let's maintain it for the protocol compatibility, to make sure things
1506 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1507 * protocol with a warning.
1508 * TODO: remove gluster+rdma interface support
1510 static BlockDriver bdrv_gluster_rdma
= {
1511 .format_name
= "gluster",
1512 .protocol_name
= "gluster+rdma",
1513 .instance_size
= sizeof(BDRVGlusterState
),
1514 .bdrv_needs_filename
= true,
1515 .bdrv_file_open
= qemu_gluster_open
,
1516 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1517 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1518 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1519 .bdrv_close
= qemu_gluster_close
,
1520 .bdrv_create
= qemu_gluster_create
,
1521 .bdrv_getlength
= qemu_gluster_getlength
,
1522 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1523 .bdrv_truncate
= qemu_gluster_truncate
,
1524 .bdrv_co_readv
= qemu_gluster_co_readv
,
1525 .bdrv_co_writev
= qemu_gluster_co_writev
,
1526 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1527 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1528 #ifdef CONFIG_GLUSTERFS_DISCARD
1529 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1531 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1532 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1534 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1535 .create_opts
= &qemu_gluster_create_opts
,
1538 static void bdrv_gluster_init(void)
1540 bdrv_register(&bdrv_gluster_rdma
);
1541 bdrv_register(&bdrv_gluster_unix
);
1542 bdrv_register(&bdrv_gluster_tcp
);
1543 bdrv_register(&bdrv_gluster
);
1546 block_init(bdrv_gluster_init
);