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"
16 #include "qemu/error-report.h"
18 #define GLUSTER_OPT_FILENAME "filename"
19 #define GLUSTER_OPT_VOLUME "volume"
20 #define GLUSTER_OPT_PATH "path"
21 #define GLUSTER_OPT_TYPE "type"
22 #define GLUSTER_OPT_SERVER_PATTERN "server."
23 #define GLUSTER_OPT_HOST "host"
24 #define GLUSTER_OPT_PORT "port"
25 #define GLUSTER_OPT_TO "to"
26 #define GLUSTER_OPT_IPV4 "ipv4"
27 #define GLUSTER_OPT_IPV6 "ipv6"
28 #define GLUSTER_OPT_SOCKET "socket"
29 #define GLUSTER_OPT_DEBUG "debug"
30 #define GLUSTER_DEFAULT_PORT 24007
31 #define GLUSTER_DEBUG_DEFAULT 4
32 #define GLUSTER_DEBUG_MAX 9
33 #define GLUSTER_OPT_LOGFILE "logfile"
34 #define GLUSTER_LOGFILE_DEFAULT "-" /* handled in libgfapi as /dev/stderr */
36 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
38 typedef struct GlusterAIOCB
{
43 AioContext
*aio_context
;
46 typedef struct BDRVGlusterState
{
50 bool supports_seek_data
;
54 typedef struct BDRVGlusterReopenState
{
57 } BDRVGlusterReopenState
;
60 static QemuOptsList qemu_gluster_create_opts
= {
61 .name
= "qemu-gluster-create-opts",
62 .head
= QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts
.head
),
65 .name
= BLOCK_OPT_SIZE
,
66 .type
= QEMU_OPT_SIZE
,
67 .help
= "Virtual disk size"
70 .name
= BLOCK_OPT_PREALLOC
,
71 .type
= QEMU_OPT_STRING
,
72 .help
= "Preallocation mode (allowed values: off, full)"
75 .name
= GLUSTER_OPT_DEBUG
,
76 .type
= QEMU_OPT_NUMBER
,
77 .help
= "Gluster log level, valid range is 0-9",
80 .name
= GLUSTER_OPT_LOGFILE
,
81 .type
= QEMU_OPT_STRING
,
82 .help
= "Logfile path of libgfapi",
88 static QemuOptsList runtime_opts
= {
90 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
93 .name
= GLUSTER_OPT_FILENAME
,
94 .type
= QEMU_OPT_STRING
,
95 .help
= "URL to the gluster image",
98 .name
= GLUSTER_OPT_DEBUG
,
99 .type
= QEMU_OPT_NUMBER
,
100 .help
= "Gluster log level, valid range is 0-9",
103 .name
= GLUSTER_OPT_LOGFILE
,
104 .type
= QEMU_OPT_STRING
,
105 .help
= "Logfile path of libgfapi",
107 { /* end of list */ }
111 static QemuOptsList runtime_json_opts
= {
112 .name
= "gluster_json",
113 .head
= QTAILQ_HEAD_INITIALIZER(runtime_json_opts
.head
),
116 .name
= GLUSTER_OPT_VOLUME
,
117 .type
= QEMU_OPT_STRING
,
118 .help
= "name of gluster volume where VM image resides",
121 .name
= GLUSTER_OPT_PATH
,
122 .type
= QEMU_OPT_STRING
,
123 .help
= "absolute path to image file in gluster volume",
126 .name
= GLUSTER_OPT_DEBUG
,
127 .type
= QEMU_OPT_NUMBER
,
128 .help
= "Gluster log level, valid range is 0-9",
130 { /* end of list */ }
134 static QemuOptsList runtime_type_opts
= {
135 .name
= "gluster_type",
136 .head
= QTAILQ_HEAD_INITIALIZER(runtime_type_opts
.head
),
139 .name
= GLUSTER_OPT_TYPE
,
140 .type
= QEMU_OPT_STRING
,
143 { /* end of list */ }
147 static QemuOptsList runtime_unix_opts
= {
148 .name
= "gluster_unix",
149 .head
= QTAILQ_HEAD_INITIALIZER(runtime_unix_opts
.head
),
152 .name
= GLUSTER_OPT_SOCKET
,
153 .type
= QEMU_OPT_STRING
,
154 .help
= "socket file path)",
156 { /* end of list */ }
160 static QemuOptsList runtime_tcp_opts
= {
161 .name
= "gluster_tcp",
162 .head
= QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts
.head
),
165 .name
= GLUSTER_OPT_TYPE
,
166 .type
= QEMU_OPT_STRING
,
170 .name
= GLUSTER_OPT_HOST
,
171 .type
= QEMU_OPT_STRING
,
172 .help
= "host address (hostname/ipv4/ipv6 addresses)",
175 .name
= GLUSTER_OPT_PORT
,
176 .type
= QEMU_OPT_NUMBER
,
177 .help
= "port number on which glusterd is listening (default 24007)",
181 .type
= QEMU_OPT_NUMBER
,
182 .help
= "max port number, not supported by gluster",
186 .type
= QEMU_OPT_BOOL
,
187 .help
= "ipv4 bool value, not supported by gluster",
191 .type
= QEMU_OPT_BOOL
,
192 .help
= "ipv6 bool value, not supported by gluster",
194 { /* end of list */ }
198 static int parse_volume_options(BlockdevOptionsGluster
*gconf
, char *path
)
207 p
= q
= path
+ strspn(path
, "/");
208 p
+= strcspn(p
, "/");
212 gconf
->volume
= g_strndup(q
, p
- q
);
219 gconf
->path
= g_strdup(p
);
224 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
226 * 'gluster' is the protocol.
228 * 'transport' specifies the transport type used to connect to gluster
229 * management daemon (glusterd). Valid transport types are
230 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
232 * 'host' specifies the host where the volume file specification for
233 * the given volume resides. This can be either hostname or ipv4 address.
234 * If transport type is 'unix', then 'host' field should not be specified.
235 * The 'socket' field needs to be populated with the path to unix domain
238 * 'port' is the port number on which glusterd is listening. This is optional
239 * and if not specified, QEMU will send 0 which will make gluster to use the
240 * default port. If the transport type is unix, then 'port' should not be
243 * 'volume' is the name of the gluster volume which contains the VM image.
245 * 'path' is the path to the actual VM image that resides on gluster volume.
249 * file=gluster://1.2.3.4/testvol/a.img
250 * file=gluster+tcp://1.2.3.4/testvol/a.img
251 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
252 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
253 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
255 static int qemu_gluster_parse_uri(BlockdevOptionsGluster
*gconf
,
256 const char *filename
)
258 GlusterServer
*gsconf
;
260 QueryParams
*qp
= NULL
;
261 bool is_unix
= false;
264 uri
= uri_parse(filename
);
269 gconf
->server
= g_new0(GlusterServerList
, 1);
270 gconf
->server
->value
= gsconf
= g_new0(GlusterServer
, 1);
273 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
274 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
275 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
276 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
277 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
278 gsconf
->type
= GLUSTER_TRANSPORT_UNIX
;
280 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
281 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
282 error_report("Warning: rdma feature is not supported, falling "
289 ret
= parse_volume_options(gconf
, uri
->path
);
294 qp
= query_params_parse(uri
->query
);
295 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
301 if (uri
->server
|| uri
->port
) {
305 if (strcmp(qp
->p
[0].name
, "socket")) {
309 gsconf
->u
.q_unix
.path
= g_strdup(qp
->p
[0].value
);
311 gsconf
->u
.tcp
.host
= g_strdup(uri
->server
? uri
->server
: "localhost");
313 gsconf
->u
.tcp
.port
= g_strdup_printf("%d", uri
->port
);
315 gsconf
->u
.tcp
.port
= g_strdup_printf("%d", GLUSTER_DEFAULT_PORT
);
321 query_params_free(qp
);
327 static struct glfs
*qemu_gluster_glfs_init(BlockdevOptionsGluster
*gconf
,
333 GlusterServerList
*server
;
335 glfs
= glfs_new(gconf
->volume
);
340 for (server
= gconf
->server
; server
; server
= server
->next
) {
341 if (server
->value
->type
== GLUSTER_TRANSPORT_UNIX
) {
342 ret
= glfs_set_volfile_server(glfs
,
343 GlusterTransport_lookup
[server
->value
->type
],
344 server
->value
->u
.q_unix
.path
, 0);
346 ret
= glfs_set_volfile_server(glfs
,
347 GlusterTransport_lookup
[server
->value
->type
],
348 server
->value
->u
.tcp
.host
,
349 atoi(server
->value
->u
.tcp
.port
));
357 ret
= glfs_set_logging(glfs
, gconf
->logfile
, gconf
->debug_level
);
362 ret
= glfs_init(glfs
);
364 error_setg(errp
, "Gluster connection for volume %s, path %s failed"
365 " to connect", gconf
->volume
, gconf
->path
);
366 for (server
= gconf
->server
; server
; server
= server
->next
) {
367 if (server
->value
->type
== GLUSTER_TRANSPORT_UNIX
) {
368 error_append_hint(errp
, "hint: failed on socket %s ",
369 server
->value
->u
.q_unix
.path
);
371 error_append_hint(errp
, "hint: failed on host %s and port %s ",
372 server
->value
->u
.tcp
.host
,
373 server
->value
->u
.tcp
.port
);
377 error_append_hint(errp
, "Please refer to gluster logs for more info\n");
379 /* glfs_init sometimes doesn't set errno although docs suggest that */
397 static int qapi_enum_parse(const char *opt
)
402 return GLUSTER_TRANSPORT__MAX
;
405 for (i
= 0; i
< GLUSTER_TRANSPORT__MAX
; i
++) {
406 if (!strcmp(opt
, GlusterTransport_lookup
[i
])) {
415 * Convert the json formatted command line into qapi.
417 static int qemu_gluster_parse_json(BlockdevOptionsGluster
*gconf
,
418 QDict
*options
, Error
**errp
)
421 GlusterServer
*gsconf
;
422 GlusterServerList
*curr
= NULL
;
423 QDict
*backing_options
= NULL
;
424 Error
*local_err
= NULL
;
430 /* create opts info from runtime_json_opts list */
431 opts
= qemu_opts_create(&runtime_json_opts
, NULL
, 0, &error_abort
);
432 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
437 num_servers
= qdict_array_entries(options
, GLUSTER_OPT_SERVER_PATTERN
);
438 if (num_servers
< 1) {
439 error_setg(&local_err
, QERR_MISSING_PARAMETER
, "server");
443 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_VOLUME
);
445 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_VOLUME
);
448 gconf
->volume
= g_strdup(ptr
);
450 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PATH
);
452 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_PATH
);
455 gconf
->path
= g_strdup(ptr
);
458 for (i
= 0; i
< num_servers
; i
++) {
459 str
= g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN
"%d.", i
);
460 qdict_extract_subqdict(options
, &backing_options
, str
);
462 /* create opts info from runtime_type_opts list */
463 opts
= qemu_opts_create(&runtime_type_opts
, NULL
, 0, &error_abort
);
464 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
469 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TYPE
);
470 gsconf
= g_new0(GlusterServer
, 1);
471 gsconf
->type
= qapi_enum_parse(ptr
);
473 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_TYPE
);
474 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
478 if (gsconf
->type
== GLUSTER_TRANSPORT__MAX
) {
479 error_setg(&local_err
, QERR_INVALID_PARAMETER_VALUE
,
480 GLUSTER_OPT_TYPE
, "tcp or unix");
481 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
486 if (gsconf
->type
== GLUSTER_TRANSPORT_TCP
) {
487 /* create opts info from runtime_tcp_opts list */
488 opts
= qemu_opts_create(&runtime_tcp_opts
, NULL
, 0, &error_abort
);
489 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
494 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_HOST
);
496 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
498 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
501 gsconf
->u
.tcp
.host
= g_strdup(ptr
);
502 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PORT
);
504 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
506 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
509 gsconf
->u
.tcp
.port
= g_strdup(ptr
);
511 /* defend for unsupported fields in InetSocketAddress,
512 * i.e. @ipv4, @ipv6 and @to
514 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TO
);
516 gsconf
->u
.tcp
.has_to
= true;
518 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV4
);
520 gsconf
->u
.tcp
.has_ipv4
= true;
522 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV6
);
524 gsconf
->u
.tcp
.has_ipv6
= true;
526 if (gsconf
->u
.tcp
.has_to
) {
527 error_setg(&local_err
, "Parameter 'to' not supported");
530 if (gsconf
->u
.tcp
.has_ipv4
|| gsconf
->u
.tcp
.has_ipv6
) {
531 error_setg(&local_err
, "Parameters 'ipv4/ipv6' not supported");
536 /* create opts info from runtime_unix_opts list */
537 opts
= qemu_opts_create(&runtime_unix_opts
, NULL
, 0, &error_abort
);
538 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
543 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_SOCKET
);
545 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
547 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
550 gsconf
->u
.q_unix
.path
= g_strdup(ptr
);
554 if (gconf
->server
== NULL
) {
555 gconf
->server
= g_new0(GlusterServerList
, 1);
556 gconf
->server
->value
= gsconf
;
557 curr
= gconf
->server
;
559 curr
->next
= g_new0(GlusterServerList
, 1);
560 curr
->next
->value
= gsconf
;
564 qdict_del(backing_options
, str
);
572 error_propagate(errp
, local_err
);
575 qdict_del(backing_options
, str
);
582 static struct glfs
*qemu_gluster_init(BlockdevOptionsGluster
*gconf
,
583 const char *filename
,
584 QDict
*options
, Error
**errp
)
588 ret
= qemu_gluster_parse_uri(gconf
, filename
);
590 error_setg(errp
, "invalid URI");
591 error_append_hint(errp
, "Usage: file=gluster[+transport]://"
592 "[host[:port]]volume/path[?socket=...]"
594 "[,file.logfile=/path/filename.log]\n");
599 ret
= qemu_gluster_parse_json(gconf
, options
, errp
);
601 error_append_hint(errp
, "Usage: "
602 "-drive driver=qcow2,file.driver=gluster,"
603 "file.volume=testvol,file.path=/path/a.qcow2"
605 "[,file.logfile=/path/filename.log],"
606 "file.server.0.type=tcp,"
607 "file.server.0.host=1.2.3.4,"
608 "file.server.0.port=24007,"
609 "file.server.1.transport=unix,"
610 "file.server.1.socket=/var/run/glusterd.socket ..."
618 return qemu_gluster_glfs_init(gconf
, errp
);
621 static void qemu_gluster_complete_aio(void *opaque
)
623 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
625 qemu_bh_delete(acb
->bh
);
627 qemu_coroutine_enter(acb
->coroutine
);
631 * AIO callback routine called from GlusterFS thread.
633 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
635 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
637 if (!ret
|| ret
== acb
->size
) {
638 acb
->ret
= 0; /* Success */
639 } else if (ret
< 0) {
640 acb
->ret
= -errno
; /* Read/Write failed */
642 acb
->ret
= -EIO
; /* Partial read/write - fail it */
645 acb
->bh
= aio_bh_new(acb
->aio_context
, qemu_gluster_complete_aio
, acb
);
646 qemu_bh_schedule(acb
->bh
);
649 static void qemu_gluster_parse_flags(int bdrv_flags
, int *open_flags
)
651 assert(open_flags
!= NULL
);
653 *open_flags
|= O_BINARY
;
655 if (bdrv_flags
& BDRV_O_RDWR
) {
656 *open_flags
|= O_RDWR
;
658 *open_flags
|= O_RDONLY
;
661 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
662 *open_flags
|= O_DIRECT
;
667 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
668 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
669 * - Corrected versions return -1 and set errno to EINVAL.
670 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
671 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
673 static bool qemu_gluster_test_seek(struct glfs_fd
*fd
)
677 eof
= glfs_lseek(fd
, 0, SEEK_END
);
679 /* this should never occur */
683 /* this should always fail with ENXIO if SEEK_DATA is supported */
684 ret
= glfs_lseek(fd
, eof
, SEEK_DATA
);
685 return (ret
< 0) && (errno
== ENXIO
);
688 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
689 int bdrv_flags
, Error
**errp
)
691 BDRVGlusterState
*s
= bs
->opaque
;
694 BlockdevOptionsGluster
*gconf
= NULL
;
696 Error
*local_err
= NULL
;
697 const char *filename
, *logfile
;
699 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
700 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
702 error_propagate(errp
, local_err
);
707 filename
= qemu_opt_get(opts
, GLUSTER_OPT_FILENAME
);
709 s
->debug_level
= qemu_opt_get_number(opts
, GLUSTER_OPT_DEBUG
,
710 GLUSTER_DEBUG_DEFAULT
);
711 if (s
->debug_level
< 0) {
713 } else if (s
->debug_level
> GLUSTER_DEBUG_MAX
) {
714 s
->debug_level
= GLUSTER_DEBUG_MAX
;
717 gconf
= g_new0(BlockdevOptionsGluster
, 1);
718 gconf
->debug_level
= s
->debug_level
;
719 gconf
->has_debug_level
= true;
721 logfile
= qemu_opt_get(opts
, GLUSTER_OPT_LOGFILE
);
722 s
->logfile
= g_strdup(logfile
? logfile
: GLUSTER_LOGFILE_DEFAULT
);
724 gconf
->logfile
= g_strdup(s
->logfile
);
725 gconf
->has_logfile
= true;
727 s
->glfs
= qemu_gluster_init(gconf
, filename
, options
, errp
);
733 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
734 /* Without this, if fsync fails for a recoverable reason (for instance,
735 * ENOSPC), gluster will dump its cache, preventing retries. This means
736 * almost certain data loss. Not all gluster versions support the
737 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
738 * discover during runtime if it is supported (this api returns success for
739 * unknown key/value pairs) */
740 ret
= glfs_set_xlator_option(s
->glfs
, "*-write-behind",
741 "resync-failed-syncs-after-fsync",
744 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
750 qemu_gluster_parse_flags(bdrv_flags
, &open_flags
);
752 s
->fd
= glfs_open(s
->glfs
, gconf
->path
, open_flags
);
757 s
->supports_seek_data
= qemu_gluster_test_seek(s
->fd
);
761 qapi_free_BlockdevOptionsGluster(gconf
);
775 static int qemu_gluster_reopen_prepare(BDRVReopenState
*state
,
776 BlockReopenQueue
*queue
, Error
**errp
)
780 BDRVGlusterReopenState
*reop_s
;
781 BlockdevOptionsGluster
*gconf
;
784 assert(state
!= NULL
);
785 assert(state
->bs
!= NULL
);
787 s
= state
->bs
->opaque
;
789 state
->opaque
= g_new0(BDRVGlusterReopenState
, 1);
790 reop_s
= state
->opaque
;
792 qemu_gluster_parse_flags(state
->flags
, &open_flags
);
794 gconf
= g_new0(BlockdevOptionsGluster
, 1);
795 gconf
->debug_level
= s
->debug_level
;
796 gconf
->has_debug_level
= true;
797 gconf
->logfile
= g_strdup(s
->logfile
);
798 gconf
->has_logfile
= true;
799 reop_s
->glfs
= qemu_gluster_init(gconf
, state
->bs
->filename
, NULL
, errp
);
800 if (reop_s
->glfs
== NULL
) {
805 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
806 ret
= glfs_set_xlator_option(reop_s
->glfs
, "*-write-behind",
807 "resync-failed-syncs-after-fsync", "on");
809 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
815 reop_s
->fd
= glfs_open(reop_s
->glfs
, gconf
->path
, open_flags
);
816 if (reop_s
->fd
== NULL
) {
817 /* reops->glfs will be cleaned up in _abort */
823 /* state->opaque will be freed in either the _abort or _commit */
824 qapi_free_BlockdevOptionsGluster(gconf
);
828 static void qemu_gluster_reopen_commit(BDRVReopenState
*state
)
830 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
831 BDRVGlusterState
*s
= state
->bs
->opaque
;
842 /* use the newly opened image / connection */
844 s
->glfs
= reop_s
->glfs
;
846 g_free(state
->opaque
);
847 state
->opaque
= NULL
;
853 static void qemu_gluster_reopen_abort(BDRVReopenState
*state
)
855 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
857 if (reop_s
== NULL
) {
862 glfs_close(reop_s
->fd
);
866 glfs_fini(reop_s
->glfs
);
869 g_free(state
->opaque
);
870 state
->opaque
= NULL
;
875 #ifdef CONFIG_GLUSTERFS_ZEROFILL
876 static coroutine_fn
int qemu_gluster_co_pwrite_zeroes(BlockDriverState
*bs
,
879 BdrvRequestFlags flags
)
883 BDRVGlusterState
*s
= bs
->opaque
;
887 acb
.coroutine
= qemu_coroutine_self();
888 acb
.aio_context
= bdrv_get_aio_context(bs
);
890 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
895 qemu_coroutine_yield();
899 static inline bool gluster_supports_zerofill(void)
904 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
907 return glfs_zerofill(fd
, offset
, size
);
911 static inline bool gluster_supports_zerofill(void)
916 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
923 static int qemu_gluster_create(const char *filename
,
924 QemuOpts
*opts
, Error
**errp
)
926 BlockdevOptionsGluster
*gconf
;
931 int64_t total_size
= 0;
934 gconf
= g_new0(BlockdevOptionsGluster
, 1);
935 gconf
->debug_level
= qemu_opt_get_number_del(opts
, GLUSTER_OPT_DEBUG
,
936 GLUSTER_DEBUG_DEFAULT
);
937 if (gconf
->debug_level
< 0) {
938 gconf
->debug_level
= 0;
939 } else if (gconf
->debug_level
> GLUSTER_DEBUG_MAX
) {
940 gconf
->debug_level
= GLUSTER_DEBUG_MAX
;
942 gconf
->has_debug_level
= true;
944 gconf
->logfile
= qemu_opt_get_del(opts
, GLUSTER_OPT_LOGFILE
);
945 if (!gconf
->logfile
) {
946 gconf
->logfile
= g_strdup(GLUSTER_LOGFILE_DEFAULT
);
948 gconf
->has_logfile
= true;
950 glfs
= qemu_gluster_init(gconf
, filename
, NULL
, errp
);
956 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
959 tmp
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
960 if (!tmp
|| !strcmp(tmp
, "off")) {
962 } else if (!strcmp(tmp
, "full") && gluster_supports_zerofill()) {
965 error_setg(errp
, "Invalid preallocation mode: '%s'"
966 " or GlusterFS doesn't support zerofill API", tmp
);
971 fd
= glfs_creat(glfs
, gconf
->path
,
972 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
976 if (!glfs_ftruncate(fd
, total_size
)) {
977 if (prealloc
&& qemu_gluster_zerofill(fd
, 0, total_size
)) {
984 if (glfs_close(fd
) != 0) {
990 qapi_free_BlockdevOptionsGluster(gconf
);
997 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
998 int64_t sector_num
, int nb_sectors
,
999 QEMUIOVector
*qiov
, int write
)
1003 BDRVGlusterState
*s
= bs
->opaque
;
1004 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
1005 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
1009 acb
.coroutine
= qemu_coroutine_self();
1010 acb
.aio_context
= bdrv_get_aio_context(bs
);
1013 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
1014 gluster_finish_aiocb
, &acb
);
1016 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
1017 gluster_finish_aiocb
, &acb
);
1024 qemu_coroutine_yield();
1028 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
1031 BDRVGlusterState
*s
= bs
->opaque
;
1033 ret
= glfs_ftruncate(s
->fd
, offset
);
1041 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
1046 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
1049 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
1054 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
1057 static void qemu_gluster_close(BlockDriverState
*bs
)
1059 BDRVGlusterState
*s
= bs
->opaque
;
1069 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
1073 BDRVGlusterState
*s
= bs
->opaque
;
1077 acb
.coroutine
= qemu_coroutine_self();
1078 acb
.aio_context
= bdrv_get_aio_context(bs
);
1080 ret
= glfs_fsync_async(s
->fd
, gluster_finish_aiocb
, &acb
);
1086 qemu_coroutine_yield();
1095 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1096 * after a fsync failure, so we have no way of allowing the guest to safely
1097 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1098 * either, but will invalidate the fd on error, so this is again our only
1101 * The 'resync-failed-syncs-after-fsync' xlator option for the
1102 * write-behind cache will cause later gluster versions to retain its
1103 * cache after error, so long as the fd remains open. However, we
1104 * currently have no way of knowing if this option is supported.
1106 * TODO: Once gluster provides a way for us to determine if the option
1107 * is supported, bypass the closure and setting drv to NULL. */
1108 qemu_gluster_close(bs
);
1113 #ifdef CONFIG_GLUSTERFS_DISCARD
1114 static coroutine_fn
int qemu_gluster_co_pdiscard(BlockDriverState
*bs
,
1115 int64_t offset
, int size
)
1119 BDRVGlusterState
*s
= bs
->opaque
;
1123 acb
.coroutine
= qemu_coroutine_self();
1124 acb
.aio_context
= bdrv_get_aio_context(bs
);
1126 ret
= glfs_discard_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
1131 qemu_coroutine_yield();
1136 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
1138 BDRVGlusterState
*s
= bs
->opaque
;
1141 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
1149 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
1151 BDRVGlusterState
*s
= bs
->opaque
;
1155 ret
= glfs_fstat(s
->fd
, &st
);
1159 return st
.st_blocks
* 512;
1163 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
1165 /* GlusterFS volume could be backed by a block device */
1170 * Find allocation range in @bs around offset @start.
1171 * May change underlying file descriptor's file offset.
1172 * If @start is not in a hole, store @start in @data, and the
1173 * beginning of the next hole in @hole, and return 0.
1174 * If @start is in a non-trailing hole, store @start in @hole and the
1175 * beginning of the next non-hole in @data, and return 0.
1176 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1177 * If we can't find out, return a negative errno other than -ENXIO.
1179 * (Shamefully copied from raw-posix.c, only miniscule adaptions.)
1181 static int find_allocation(BlockDriverState
*bs
, off_t start
,
1182 off_t
*data
, off_t
*hole
)
1184 BDRVGlusterState
*s
= bs
->opaque
;
1187 if (!s
->supports_seek_data
) {
1193 * D1. offs == start: start is in data
1194 * D2. offs > start: start is in a hole, next data at offs
1195 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1196 * or start is beyond EOF
1197 * If the latter happens, the file has been truncated behind
1198 * our back since we opened it. All bets are off then.
1199 * Treating like a trailing hole is simplest.
1200 * D4. offs < 0, errno != ENXIO: we learned nothing
1202 offs
= glfs_lseek(s
->fd
, start
, SEEK_DATA
);
1204 return -errno
; /* D3 or D4 */
1206 assert(offs
>= start
);
1209 /* D2: in hole, next data at offs */
1215 /* D1: in data, end not yet known */
1219 * H1. offs == start: start is in a hole
1220 * If this happens here, a hole has been dug behind our back
1221 * since the previous lseek().
1222 * H2. offs > start: either start is in data, next hole at offs,
1223 * or start is in trailing hole, EOF at offs
1224 * Linux treats trailing holes like any other hole: offs ==
1225 * start. Solaris seeks to EOF instead: offs > start (blech).
1226 * If that happens here, a hole has been dug behind our back
1227 * since the previous lseek().
1228 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1229 * If this happens, the file has been truncated behind our
1230 * back since we opened it. Treat it like a trailing hole.
1231 * H4. offs < 0, errno != ENXIO: we learned nothing
1232 * Pretend we know nothing at all, i.e. "forget" about D1.
1234 offs
= glfs_lseek(s
->fd
, start
, SEEK_HOLE
);
1236 return -errno
; /* D1 and (H3 or H4) */
1238 assert(offs
>= start
);
1242 * D1 and H2: either in data, next hole at offs, or it was in
1243 * data but is now in a trailing hole. In the latter case,
1244 * all bets are off. Treating it as if it there was data all
1245 * the way to EOF is safe, so simply do that.
1257 * Returns the allocation status of the specified sectors.
1259 * If 'sector_num' is beyond the end of the disk image the return value is 0
1260 * and 'pnum' is set to 0.
1262 * 'pnum' is set to the number of sectors (including and immediately following
1263 * the specified sector) that are known to be in the same
1264 * allocated/unallocated state.
1266 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1267 * beyond the end of the disk image it will be clamped.
1269 * (Based on raw_co_get_block_status() from raw-posix.c.)
1271 static int64_t coroutine_fn
qemu_gluster_co_get_block_status(
1272 BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
, int *pnum
,
1273 BlockDriverState
**file
)
1275 BDRVGlusterState
*s
= bs
->opaque
;
1276 off_t start
, data
= 0, hole
= 0;
1284 start
= sector_num
* BDRV_SECTOR_SIZE
;
1285 total_size
= bdrv_getlength(bs
);
1286 if (total_size
< 0) {
1288 } else if (start
>= total_size
) {
1291 } else if (start
+ nb_sectors
* BDRV_SECTOR_SIZE
> total_size
) {
1292 nb_sectors
= DIV_ROUND_UP(total_size
- start
, BDRV_SECTOR_SIZE
);
1295 ret
= find_allocation(bs
, start
, &data
, &hole
);
1296 if (ret
== -ENXIO
) {
1299 ret
= BDRV_BLOCK_ZERO
;
1300 } else if (ret
< 0) {
1301 /* No info available, so pretend there are no holes */
1303 ret
= BDRV_BLOCK_DATA
;
1304 } else if (data
== start
) {
1305 /* On a data extent, compute sectors to the end of the extent,
1306 * possibly including a partial sector at EOF. */
1307 *pnum
= MIN(nb_sectors
, DIV_ROUND_UP(hole
- start
, BDRV_SECTOR_SIZE
));
1308 ret
= BDRV_BLOCK_DATA
;
1310 /* On a hole, compute sectors to the beginning of the next extent. */
1311 assert(hole
== start
);
1312 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1313 ret
= BDRV_BLOCK_ZERO
;
1318 return ret
| BDRV_BLOCK_OFFSET_VALID
| start
;
1322 static BlockDriver bdrv_gluster
= {
1323 .format_name
= "gluster",
1324 .protocol_name
= "gluster",
1325 .instance_size
= sizeof(BDRVGlusterState
),
1326 .bdrv_needs_filename
= false,
1327 .bdrv_file_open
= qemu_gluster_open
,
1328 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1329 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1330 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1331 .bdrv_close
= qemu_gluster_close
,
1332 .bdrv_create
= qemu_gluster_create
,
1333 .bdrv_getlength
= qemu_gluster_getlength
,
1334 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1335 .bdrv_truncate
= qemu_gluster_truncate
,
1336 .bdrv_co_readv
= qemu_gluster_co_readv
,
1337 .bdrv_co_writev
= qemu_gluster_co_writev
,
1338 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1339 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1340 #ifdef CONFIG_GLUSTERFS_DISCARD
1341 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1343 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1344 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1346 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1347 .create_opts
= &qemu_gluster_create_opts
,
1350 static BlockDriver bdrv_gluster_tcp
= {
1351 .format_name
= "gluster",
1352 .protocol_name
= "gluster+tcp",
1353 .instance_size
= sizeof(BDRVGlusterState
),
1354 .bdrv_needs_filename
= false,
1355 .bdrv_file_open
= qemu_gluster_open
,
1356 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1357 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1358 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1359 .bdrv_close
= qemu_gluster_close
,
1360 .bdrv_create
= qemu_gluster_create
,
1361 .bdrv_getlength
= qemu_gluster_getlength
,
1362 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1363 .bdrv_truncate
= qemu_gluster_truncate
,
1364 .bdrv_co_readv
= qemu_gluster_co_readv
,
1365 .bdrv_co_writev
= qemu_gluster_co_writev
,
1366 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1367 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1368 #ifdef CONFIG_GLUSTERFS_DISCARD
1369 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1371 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1372 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1374 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1375 .create_opts
= &qemu_gluster_create_opts
,
1378 static BlockDriver bdrv_gluster_unix
= {
1379 .format_name
= "gluster",
1380 .protocol_name
= "gluster+unix",
1381 .instance_size
= sizeof(BDRVGlusterState
),
1382 .bdrv_needs_filename
= true,
1383 .bdrv_file_open
= qemu_gluster_open
,
1384 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1385 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1386 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1387 .bdrv_close
= qemu_gluster_close
,
1388 .bdrv_create
= qemu_gluster_create
,
1389 .bdrv_getlength
= qemu_gluster_getlength
,
1390 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1391 .bdrv_truncate
= qemu_gluster_truncate
,
1392 .bdrv_co_readv
= qemu_gluster_co_readv
,
1393 .bdrv_co_writev
= qemu_gluster_co_writev
,
1394 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1395 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1396 #ifdef CONFIG_GLUSTERFS_DISCARD
1397 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1399 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1400 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1402 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1403 .create_opts
= &qemu_gluster_create_opts
,
1406 /* rdma is deprecated (actually never supported for volfile fetch).
1407 * Let's maintain it for the protocol compatibility, to make sure things
1408 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1409 * protocol with a warning.
1410 * TODO: remove gluster+rdma interface support
1412 static BlockDriver bdrv_gluster_rdma
= {
1413 .format_name
= "gluster",
1414 .protocol_name
= "gluster+rdma",
1415 .instance_size
= sizeof(BDRVGlusterState
),
1416 .bdrv_needs_filename
= true,
1417 .bdrv_file_open
= qemu_gluster_open
,
1418 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1419 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1420 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1421 .bdrv_close
= qemu_gluster_close
,
1422 .bdrv_create
= qemu_gluster_create
,
1423 .bdrv_getlength
= qemu_gluster_getlength
,
1424 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1425 .bdrv_truncate
= qemu_gluster_truncate
,
1426 .bdrv_co_readv
= qemu_gluster_co_readv
,
1427 .bdrv_co_writev
= qemu_gluster_co_writev
,
1428 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1429 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1430 #ifdef CONFIG_GLUSTERFS_DISCARD
1431 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1433 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1434 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1436 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1437 .create_opts
= &qemu_gluster_create_opts
,
1440 static void bdrv_gluster_init(void)
1442 bdrv_register(&bdrv_gluster_rdma
);
1443 bdrv_register(&bdrv_gluster_unix
);
1444 bdrv_register(&bdrv_gluster_tcp
);
1445 bdrv_register(&bdrv_gluster
);
1448 block_init(bdrv_gluster_init
);