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
34 #define GERR_INDEX_HINT "hint: check in 'server' array index '%d'\n"
36 typedef struct GlusterAIOCB
{
41 AioContext
*aio_context
;
44 typedef struct BDRVGlusterState
{
47 bool supports_seek_data
;
51 typedef struct BDRVGlusterReopenState
{
54 } BDRVGlusterReopenState
;
57 static QemuOptsList qemu_gluster_create_opts
= {
58 .name
= "qemu-gluster-create-opts",
59 .head
= QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts
.head
),
62 .name
= BLOCK_OPT_SIZE
,
63 .type
= QEMU_OPT_SIZE
,
64 .help
= "Virtual disk size"
67 .name
= BLOCK_OPT_PREALLOC
,
68 .type
= QEMU_OPT_STRING
,
69 .help
= "Preallocation mode (allowed values: off, full)"
72 .name
= GLUSTER_OPT_DEBUG
,
73 .type
= QEMU_OPT_NUMBER
,
74 .help
= "Gluster log level, valid range is 0-9",
80 static QemuOptsList runtime_opts
= {
82 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
85 .name
= GLUSTER_OPT_FILENAME
,
86 .type
= QEMU_OPT_STRING
,
87 .help
= "URL to the gluster image",
90 .name
= GLUSTER_OPT_DEBUG
,
91 .type
= QEMU_OPT_NUMBER
,
92 .help
= "Gluster log level, valid range is 0-9",
98 static QemuOptsList runtime_json_opts
= {
99 .name
= "gluster_json",
100 .head
= QTAILQ_HEAD_INITIALIZER(runtime_json_opts
.head
),
103 .name
= GLUSTER_OPT_VOLUME
,
104 .type
= QEMU_OPT_STRING
,
105 .help
= "name of gluster volume where VM image resides",
108 .name
= GLUSTER_OPT_PATH
,
109 .type
= QEMU_OPT_STRING
,
110 .help
= "absolute path to image file in gluster volume",
113 .name
= GLUSTER_OPT_DEBUG
,
114 .type
= QEMU_OPT_NUMBER
,
115 .help
= "Gluster log level, valid range is 0-9",
117 { /* end of list */ }
121 static QemuOptsList runtime_type_opts
= {
122 .name
= "gluster_type",
123 .head
= QTAILQ_HEAD_INITIALIZER(runtime_type_opts
.head
),
126 .name
= GLUSTER_OPT_TYPE
,
127 .type
= QEMU_OPT_STRING
,
130 { /* end of list */ }
134 static QemuOptsList runtime_unix_opts
= {
135 .name
= "gluster_unix",
136 .head
= QTAILQ_HEAD_INITIALIZER(runtime_unix_opts
.head
),
139 .name
= GLUSTER_OPT_SOCKET
,
140 .type
= QEMU_OPT_STRING
,
141 .help
= "socket file path)",
143 { /* end of list */ }
147 static QemuOptsList runtime_tcp_opts
= {
148 .name
= "gluster_tcp",
149 .head
= QTAILQ_HEAD_INITIALIZER(runtime_tcp_opts
.head
),
152 .name
= GLUSTER_OPT_TYPE
,
153 .type
= QEMU_OPT_STRING
,
157 .name
= GLUSTER_OPT_HOST
,
158 .type
= QEMU_OPT_STRING
,
159 .help
= "host address (hostname/ipv4/ipv6 addresses)",
162 .name
= GLUSTER_OPT_PORT
,
163 .type
= QEMU_OPT_NUMBER
,
164 .help
= "port number on which glusterd is listening (default 24007)",
168 .type
= QEMU_OPT_NUMBER
,
169 .help
= "max port number, not supported by gluster",
173 .type
= QEMU_OPT_BOOL
,
174 .help
= "ipv4 bool value, not supported by gluster",
178 .type
= QEMU_OPT_BOOL
,
179 .help
= "ipv6 bool value, not supported by gluster",
181 { /* end of list */ }
185 static int parse_volume_options(BlockdevOptionsGluster
*gconf
, char *path
)
194 p
= q
= path
+ strspn(path
, "/");
195 p
+= strcspn(p
, "/");
199 gconf
->volume
= g_strndup(q
, p
- q
);
206 gconf
->path
= g_strdup(p
);
211 * file=gluster[+transport]://[host[:port]]/volume/path[?socket=...]
213 * 'gluster' is the protocol.
215 * 'transport' specifies the transport type used to connect to gluster
216 * management daemon (glusterd). Valid transport types are
217 * tcp or unix. If a transport type isn't specified, then tcp type is assumed.
219 * 'host' specifies the host where the volume file specification for
220 * the given volume resides. This can be either hostname or ipv4 address.
221 * If transport type is 'unix', then 'host' field should not be specified.
222 * The 'socket' field needs to be populated with the path to unix domain
225 * 'port' is the port number on which glusterd is listening. This is optional
226 * and if not specified, QEMU will send 0 which will make gluster to use the
227 * default port. If the transport type is unix, then 'port' should not be
230 * 'volume' is the name of the gluster volume which contains the VM image.
232 * 'path' is the path to the actual VM image that resides on gluster volume.
236 * file=gluster://1.2.3.4/testvol/a.img
237 * file=gluster+tcp://1.2.3.4/testvol/a.img
238 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
239 * file=gluster+tcp://host.domain.com:24007/testvol/dir/a.img
240 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
242 static int qemu_gluster_parse_uri(BlockdevOptionsGluster
*gconf
,
243 const char *filename
)
245 GlusterServer
*gsconf
;
247 QueryParams
*qp
= NULL
;
248 bool is_unix
= false;
251 uri
= uri_parse(filename
);
256 gconf
->server
= g_new0(GlusterServerList
, 1);
257 gconf
->server
->value
= gsconf
= g_new0(GlusterServer
, 1);
260 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
261 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
262 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
263 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
264 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
265 gsconf
->type
= GLUSTER_TRANSPORT_UNIX
;
267 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
268 gsconf
->type
= GLUSTER_TRANSPORT_TCP
;
269 error_report("Warning: rdma feature is not supported, falling "
276 ret
= parse_volume_options(gconf
, uri
->path
);
281 qp
= query_params_parse(uri
->query
);
282 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
288 if (uri
->server
|| uri
->port
) {
292 if (strcmp(qp
->p
[0].name
, "socket")) {
296 gsconf
->u
.q_unix
.path
= g_strdup(qp
->p
[0].value
);
298 gsconf
->u
.tcp
.host
= g_strdup(uri
->server
? uri
->server
: "localhost");
300 gsconf
->u
.tcp
.port
= g_strdup_printf("%d", uri
->port
);
302 gsconf
->u
.tcp
.port
= g_strdup_printf("%d", GLUSTER_DEFAULT_PORT
);
308 query_params_free(qp
);
314 static struct glfs
*qemu_gluster_glfs_init(BlockdevOptionsGluster
*gconf
,
320 GlusterServerList
*server
;
322 glfs
= glfs_new(gconf
->volume
);
327 for (server
= gconf
->server
; server
; server
= server
->next
) {
328 if (server
->value
->type
== GLUSTER_TRANSPORT_UNIX
) {
329 ret
= glfs_set_volfile_server(glfs
,
330 GlusterTransport_lookup
[server
->value
->type
],
331 server
->value
->u
.q_unix
.path
, 0);
333 ret
= glfs_set_volfile_server(glfs
,
334 GlusterTransport_lookup
[server
->value
->type
],
335 server
->value
->u
.tcp
.host
,
336 atoi(server
->value
->u
.tcp
.port
));
344 ret
= glfs_set_logging(glfs
, "-", gconf
->debug_level
);
349 ret
= glfs_init(glfs
);
351 error_setg(errp
, "Gluster connection for volume %s, path %s failed"
352 " to connect", gconf
->volume
, gconf
->path
);
353 for (server
= gconf
->server
; server
; server
= server
->next
) {
354 if (server
->value
->type
== GLUSTER_TRANSPORT_UNIX
) {
355 error_append_hint(errp
, "hint: failed on socket %s ",
356 server
->value
->u
.q_unix
.path
);
358 error_append_hint(errp
, "hint: failed on host %s and port %s ",
359 server
->value
->u
.tcp
.host
,
360 server
->value
->u
.tcp
.port
);
364 error_append_hint(errp
, "Please refer to gluster logs for more info\n");
366 /* glfs_init sometimes doesn't set errno although docs suggest that */
384 static int qapi_enum_parse(const char *opt
)
389 return GLUSTER_TRANSPORT__MAX
;
392 for (i
= 0; i
< GLUSTER_TRANSPORT__MAX
; i
++) {
393 if (!strcmp(opt
, GlusterTransport_lookup
[i
])) {
402 * Convert the json formatted command line into qapi.
404 static int qemu_gluster_parse_json(BlockdevOptionsGluster
*gconf
,
405 QDict
*options
, Error
**errp
)
408 GlusterServer
*gsconf
;
409 GlusterServerList
*curr
= NULL
;
410 QDict
*backing_options
= NULL
;
411 Error
*local_err
= NULL
;
417 /* create opts info from runtime_json_opts list */
418 opts
= qemu_opts_create(&runtime_json_opts
, NULL
, 0, &error_abort
);
419 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
424 num_servers
= qdict_array_entries(options
, GLUSTER_OPT_SERVER_PATTERN
);
425 if (num_servers
< 1) {
426 error_setg(&local_err
, QERR_MISSING_PARAMETER
, "server");
430 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_VOLUME
);
432 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_VOLUME
);
435 gconf
->volume
= g_strdup(ptr
);
437 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PATH
);
439 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_PATH
);
442 gconf
->path
= g_strdup(ptr
);
445 for (i
= 0; i
< num_servers
; i
++) {
446 str
= g_strdup_printf(GLUSTER_OPT_SERVER_PATTERN
"%d.", i
);
447 qdict_extract_subqdict(options
, &backing_options
, str
);
449 /* create opts info from runtime_type_opts list */
450 opts
= qemu_opts_create(&runtime_type_opts
, NULL
, 0, &error_abort
);
451 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
456 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TYPE
);
457 gsconf
= g_new0(GlusterServer
, 1);
458 gsconf
->type
= qapi_enum_parse(ptr
);
460 error_setg(&local_err
, QERR_MISSING_PARAMETER
, GLUSTER_OPT_TYPE
);
461 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
465 if (gsconf
->type
== GLUSTER_TRANSPORT__MAX
) {
466 error_setg(&local_err
, QERR_INVALID_PARAMETER_VALUE
,
467 GLUSTER_OPT_TYPE
, "tcp or unix");
468 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
473 if (gsconf
->type
== GLUSTER_TRANSPORT_TCP
) {
474 /* create opts info from runtime_tcp_opts list */
475 opts
= qemu_opts_create(&runtime_tcp_opts
, NULL
, 0, &error_abort
);
476 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
481 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_HOST
);
483 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
485 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
488 gsconf
->u
.tcp
.host
= g_strdup(ptr
);
489 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_PORT
);
491 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
493 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
496 gsconf
->u
.tcp
.port
= g_strdup(ptr
);
498 /* defend for unsupported fields in InetSocketAddress,
499 * i.e. @ipv4, @ipv6 and @to
501 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_TO
);
503 gsconf
->u
.tcp
.has_to
= true;
505 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV4
);
507 gsconf
->u
.tcp
.has_ipv4
= true;
509 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_IPV6
);
511 gsconf
->u
.tcp
.has_ipv6
= true;
513 if (gsconf
->u
.tcp
.has_to
) {
514 error_setg(&local_err
, "Parameter 'to' not supported");
517 if (gsconf
->u
.tcp
.has_ipv4
|| gsconf
->u
.tcp
.has_ipv6
) {
518 error_setg(&local_err
, "Parameters 'ipv4/ipv6' not supported");
523 /* create opts info from runtime_unix_opts list */
524 opts
= qemu_opts_create(&runtime_unix_opts
, NULL
, 0, &error_abort
);
525 qemu_opts_absorb_qdict(opts
, backing_options
, &local_err
);
530 ptr
= qemu_opt_get(opts
, GLUSTER_OPT_SOCKET
);
532 error_setg(&local_err
, QERR_MISSING_PARAMETER
,
534 error_append_hint(&local_err
, GERR_INDEX_HINT
, i
);
537 gsconf
->u
.q_unix
.path
= g_strdup(ptr
);
541 if (gconf
->server
== NULL
) {
542 gconf
->server
= g_new0(GlusterServerList
, 1);
543 gconf
->server
->value
= gsconf
;
544 curr
= gconf
->server
;
546 curr
->next
= g_new0(GlusterServerList
, 1);
547 curr
->next
->value
= gsconf
;
551 qdict_del(backing_options
, str
);
559 error_propagate(errp
, local_err
);
562 qdict_del(backing_options
, str
);
569 static struct glfs
*qemu_gluster_init(BlockdevOptionsGluster
*gconf
,
570 const char *filename
,
571 QDict
*options
, Error
**errp
)
575 ret
= qemu_gluster_parse_uri(gconf
, filename
);
577 error_setg(errp
, "invalid URI");
578 error_append_hint(errp
, "Usage: file=gluster[+transport]://"
579 "[host[:port]]/volume/path[?socket=...]\n");
584 ret
= qemu_gluster_parse_json(gconf
, options
, errp
);
586 error_append_hint(errp
, "Usage: "
587 "-drive driver=qcow2,file.driver=gluster,"
588 "file.volume=testvol,file.path=/path/a.qcow2"
589 "[,file.debug=9],file.server.0.type=tcp,"
590 "file.server.0.host=1.2.3.4,"
591 "file.server.0.port=24007,"
592 "file.server.1.transport=unix,"
593 "file.server.1.socket=/var/run/glusterd.socket ..."
601 return qemu_gluster_glfs_init(gconf
, errp
);
604 static void qemu_gluster_complete_aio(void *opaque
)
606 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
608 qemu_bh_delete(acb
->bh
);
610 qemu_coroutine_enter(acb
->coroutine
);
614 * AIO callback routine called from GlusterFS thread.
616 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
618 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
620 if (!ret
|| ret
== acb
->size
) {
621 acb
->ret
= 0; /* Success */
622 } else if (ret
< 0) {
623 acb
->ret
= -errno
; /* Read/Write failed */
625 acb
->ret
= -EIO
; /* Partial read/write - fail it */
628 acb
->bh
= aio_bh_new(acb
->aio_context
, qemu_gluster_complete_aio
, acb
);
629 qemu_bh_schedule(acb
->bh
);
632 static void qemu_gluster_parse_flags(int bdrv_flags
, int *open_flags
)
634 assert(open_flags
!= NULL
);
636 *open_flags
|= O_BINARY
;
638 if (bdrv_flags
& BDRV_O_RDWR
) {
639 *open_flags
|= O_RDWR
;
641 *open_flags
|= O_RDONLY
;
644 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
645 *open_flags
|= O_DIRECT
;
650 * Do SEEK_DATA/HOLE to detect if it is functional. Older broken versions of
651 * gfapi incorrectly return the current offset when SEEK_DATA/HOLE is used.
652 * - Corrected versions return -1 and set errno to EINVAL.
653 * - Versions that support SEEK_DATA/HOLE correctly, will return -1 and set
654 * errno to ENXIO when SEEK_DATA is called with a position of EOF.
656 static bool qemu_gluster_test_seek(struct glfs_fd
*fd
)
660 eof
= glfs_lseek(fd
, 0, SEEK_END
);
662 /* this should never occur */
666 /* this should always fail with ENXIO if SEEK_DATA is supported */
667 ret
= glfs_lseek(fd
, eof
, SEEK_DATA
);
668 return (ret
< 0) && (errno
== ENXIO
);
671 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
672 int bdrv_flags
, Error
**errp
)
674 BDRVGlusterState
*s
= bs
->opaque
;
677 BlockdevOptionsGluster
*gconf
= NULL
;
679 Error
*local_err
= NULL
;
680 const char *filename
;
682 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
683 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
685 error_propagate(errp
, local_err
);
690 filename
= qemu_opt_get(opts
, GLUSTER_OPT_FILENAME
);
692 s
->debug_level
= qemu_opt_get_number(opts
, GLUSTER_OPT_DEBUG
,
693 GLUSTER_DEBUG_DEFAULT
);
694 if (s
->debug_level
< 0) {
696 } else if (s
->debug_level
> GLUSTER_DEBUG_MAX
) {
697 s
->debug_level
= GLUSTER_DEBUG_MAX
;
700 gconf
= g_new0(BlockdevOptionsGluster
, 1);
701 gconf
->debug_level
= s
->debug_level
;
702 gconf
->has_debug_level
= true;
703 s
->glfs
= qemu_gluster_init(gconf
, filename
, options
, errp
);
709 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
710 /* Without this, if fsync fails for a recoverable reason (for instance,
711 * ENOSPC), gluster will dump its cache, preventing retries. This means
712 * almost certain data loss. Not all gluster versions support the
713 * 'resync-failed-syncs-after-fsync' key value, but there is no way to
714 * discover during runtime if it is supported (this api returns success for
715 * unknown key/value pairs) */
716 ret
= glfs_set_xlator_option(s
->glfs
, "*-write-behind",
717 "resync-failed-syncs-after-fsync",
720 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
726 qemu_gluster_parse_flags(bdrv_flags
, &open_flags
);
728 s
->fd
= glfs_open(s
->glfs
, gconf
->path
, open_flags
);
733 s
->supports_seek_data
= qemu_gluster_test_seek(s
->fd
);
737 qapi_free_BlockdevOptionsGluster(gconf
);
750 static int qemu_gluster_reopen_prepare(BDRVReopenState
*state
,
751 BlockReopenQueue
*queue
, Error
**errp
)
755 BDRVGlusterReopenState
*reop_s
;
756 BlockdevOptionsGluster
*gconf
;
759 assert(state
!= NULL
);
760 assert(state
->bs
!= NULL
);
762 s
= state
->bs
->opaque
;
764 state
->opaque
= g_new0(BDRVGlusterReopenState
, 1);
765 reop_s
= state
->opaque
;
767 qemu_gluster_parse_flags(state
->flags
, &open_flags
);
769 gconf
= g_new0(BlockdevOptionsGluster
, 1);
770 gconf
->debug_level
= s
->debug_level
;
771 gconf
->has_debug_level
= true;
772 reop_s
->glfs
= qemu_gluster_init(gconf
, state
->bs
->filename
, NULL
, errp
);
773 if (reop_s
->glfs
== NULL
) {
778 #ifdef CONFIG_GLUSTERFS_XLATOR_OPT
779 ret
= glfs_set_xlator_option(reop_s
->glfs
, "*-write-behind",
780 "resync-failed-syncs-after-fsync", "on");
782 error_setg_errno(errp
, errno
, "Unable to set xlator key/value pair");
788 reop_s
->fd
= glfs_open(reop_s
->glfs
, gconf
->path
, open_flags
);
789 if (reop_s
->fd
== NULL
) {
790 /* reops->glfs will be cleaned up in _abort */
796 /* state->opaque will be freed in either the _abort or _commit */
797 qapi_free_BlockdevOptionsGluster(gconf
);
801 static void qemu_gluster_reopen_commit(BDRVReopenState
*state
)
803 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
804 BDRVGlusterState
*s
= state
->bs
->opaque
;
815 /* use the newly opened image / connection */
817 s
->glfs
= reop_s
->glfs
;
819 g_free(state
->opaque
);
820 state
->opaque
= NULL
;
826 static void qemu_gluster_reopen_abort(BDRVReopenState
*state
)
828 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
830 if (reop_s
== NULL
) {
835 glfs_close(reop_s
->fd
);
839 glfs_fini(reop_s
->glfs
);
842 g_free(state
->opaque
);
843 state
->opaque
= NULL
;
848 #ifdef CONFIG_GLUSTERFS_ZEROFILL
849 static coroutine_fn
int qemu_gluster_co_pwrite_zeroes(BlockDriverState
*bs
,
852 BdrvRequestFlags flags
)
856 BDRVGlusterState
*s
= bs
->opaque
;
860 acb
.coroutine
= qemu_coroutine_self();
861 acb
.aio_context
= bdrv_get_aio_context(bs
);
863 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
868 qemu_coroutine_yield();
872 static inline bool gluster_supports_zerofill(void)
877 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
880 return glfs_zerofill(fd
, offset
, size
);
884 static inline bool gluster_supports_zerofill(void)
889 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
896 static int qemu_gluster_create(const char *filename
,
897 QemuOpts
*opts
, Error
**errp
)
899 BlockdevOptionsGluster
*gconf
;
904 int64_t total_size
= 0;
907 gconf
= g_new0(BlockdevOptionsGluster
, 1);
908 gconf
->debug_level
= qemu_opt_get_number_del(opts
, GLUSTER_OPT_DEBUG
,
909 GLUSTER_DEBUG_DEFAULT
);
910 if (gconf
->debug_level
< 0) {
911 gconf
->debug_level
= 0;
912 } else if (gconf
->debug_level
> GLUSTER_DEBUG_MAX
) {
913 gconf
->debug_level
= GLUSTER_DEBUG_MAX
;
915 gconf
->has_debug_level
= true;
917 glfs
= qemu_gluster_init(gconf
, filename
, NULL
, errp
);
923 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
926 tmp
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
927 if (!tmp
|| !strcmp(tmp
, "off")) {
929 } else if (!strcmp(tmp
, "full") && gluster_supports_zerofill()) {
932 error_setg(errp
, "Invalid preallocation mode: '%s'"
933 " or GlusterFS doesn't support zerofill API", tmp
);
938 fd
= glfs_creat(glfs
, gconf
->path
,
939 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
943 if (!glfs_ftruncate(fd
, total_size
)) {
944 if (prealloc
&& qemu_gluster_zerofill(fd
, 0, total_size
)) {
951 if (glfs_close(fd
) != 0) {
957 qapi_free_BlockdevOptionsGluster(gconf
);
964 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
965 int64_t sector_num
, int nb_sectors
,
966 QEMUIOVector
*qiov
, int write
)
970 BDRVGlusterState
*s
= bs
->opaque
;
971 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
972 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
976 acb
.coroutine
= qemu_coroutine_self();
977 acb
.aio_context
= bdrv_get_aio_context(bs
);
980 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
981 gluster_finish_aiocb
, &acb
);
983 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
984 gluster_finish_aiocb
, &acb
);
991 qemu_coroutine_yield();
995 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
998 BDRVGlusterState
*s
= bs
->opaque
;
1000 ret
= glfs_ftruncate(s
->fd
, offset
);
1008 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
1013 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
1016 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
1021 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
1024 static void qemu_gluster_close(BlockDriverState
*bs
)
1026 BDRVGlusterState
*s
= bs
->opaque
;
1035 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
1039 BDRVGlusterState
*s
= bs
->opaque
;
1043 acb
.coroutine
= qemu_coroutine_self();
1044 acb
.aio_context
= bdrv_get_aio_context(bs
);
1046 ret
= glfs_fsync_async(s
->fd
, gluster_finish_aiocb
, &acb
);
1052 qemu_coroutine_yield();
1061 /* Some versions of Gluster (3.5.6 -> 3.5.8?) will not retain its cache
1062 * after a fsync failure, so we have no way of allowing the guest to safely
1063 * continue. Gluster versions prior to 3.5.6 don't retain the cache
1064 * either, but will invalidate the fd on error, so this is again our only
1067 * The 'resync-failed-syncs-after-fsync' xlator option for the
1068 * write-behind cache will cause later gluster versions to retain its
1069 * cache after error, so long as the fd remains open. However, we
1070 * currently have no way of knowing if this option is supported.
1072 * TODO: Once gluster provides a way for us to determine if the option
1073 * is supported, bypass the closure and setting drv to NULL. */
1074 qemu_gluster_close(bs
);
1079 #ifdef CONFIG_GLUSTERFS_DISCARD
1080 static coroutine_fn
int qemu_gluster_co_pdiscard(BlockDriverState
*bs
,
1081 int64_t offset
, int size
)
1085 BDRVGlusterState
*s
= bs
->opaque
;
1089 acb
.coroutine
= qemu_coroutine_self();
1090 acb
.aio_context
= bdrv_get_aio_context(bs
);
1092 ret
= glfs_discard_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
1097 qemu_coroutine_yield();
1102 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
1104 BDRVGlusterState
*s
= bs
->opaque
;
1107 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
1115 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
1117 BDRVGlusterState
*s
= bs
->opaque
;
1121 ret
= glfs_fstat(s
->fd
, &st
);
1125 return st
.st_blocks
* 512;
1129 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
1131 /* GlusterFS volume could be backed by a block device */
1136 * Find allocation range in @bs around offset @start.
1137 * May change underlying file descriptor's file offset.
1138 * If @start is not in a hole, store @start in @data, and the
1139 * beginning of the next hole in @hole, and return 0.
1140 * If @start is in a non-trailing hole, store @start in @hole and the
1141 * beginning of the next non-hole in @data, and return 0.
1142 * If @start is in a trailing hole or beyond EOF, return -ENXIO.
1143 * If we can't find out, return a negative errno other than -ENXIO.
1145 * (Shamefully copied from raw-posix.c, only miniscule adaptions.)
1147 static int find_allocation(BlockDriverState
*bs
, off_t start
,
1148 off_t
*data
, off_t
*hole
)
1150 BDRVGlusterState
*s
= bs
->opaque
;
1153 if (!s
->supports_seek_data
) {
1159 * D1. offs == start: start is in data
1160 * D2. offs > start: start is in a hole, next data at offs
1161 * D3. offs < 0, errno = ENXIO: either start is in a trailing hole
1162 * or start is beyond EOF
1163 * If the latter happens, the file has been truncated behind
1164 * our back since we opened it. All bets are off then.
1165 * Treating like a trailing hole is simplest.
1166 * D4. offs < 0, errno != ENXIO: we learned nothing
1168 offs
= glfs_lseek(s
->fd
, start
, SEEK_DATA
);
1170 return -errno
; /* D3 or D4 */
1172 assert(offs
>= start
);
1175 /* D2: in hole, next data at offs */
1181 /* D1: in data, end not yet known */
1185 * H1. offs == start: start is in a hole
1186 * If this happens here, a hole has been dug behind our back
1187 * since the previous lseek().
1188 * H2. offs > start: either start is in data, next hole at offs,
1189 * or start is in trailing hole, EOF at offs
1190 * Linux treats trailing holes like any other hole: offs ==
1191 * start. Solaris seeks to EOF instead: offs > start (blech).
1192 * If that happens here, a hole has been dug behind our back
1193 * since the previous lseek().
1194 * H3. offs < 0, errno = ENXIO: start is beyond EOF
1195 * If this happens, the file has been truncated behind our
1196 * back since we opened it. Treat it like a trailing hole.
1197 * H4. offs < 0, errno != ENXIO: we learned nothing
1198 * Pretend we know nothing at all, i.e. "forget" about D1.
1200 offs
= glfs_lseek(s
->fd
, start
, SEEK_HOLE
);
1202 return -errno
; /* D1 and (H3 or H4) */
1204 assert(offs
>= start
);
1208 * D1 and H2: either in data, next hole at offs, or it was in
1209 * data but is now in a trailing hole. In the latter case,
1210 * all bets are off. Treating it as if it there was data all
1211 * the way to EOF is safe, so simply do that.
1223 * Returns the allocation status of the specified sectors.
1225 * If 'sector_num' is beyond the end of the disk image the return value is 0
1226 * and 'pnum' is set to 0.
1228 * 'pnum' is set to the number of sectors (including and immediately following
1229 * the specified sector) that are known to be in the same
1230 * allocated/unallocated state.
1232 * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
1233 * beyond the end of the disk image it will be clamped.
1235 * (Based on raw_co_get_block_status() from raw-posix.c.)
1237 static int64_t coroutine_fn
qemu_gluster_co_get_block_status(
1238 BlockDriverState
*bs
, int64_t sector_num
, int nb_sectors
, int *pnum
,
1239 BlockDriverState
**file
)
1241 BDRVGlusterState
*s
= bs
->opaque
;
1242 off_t start
, data
= 0, hole
= 0;
1250 start
= sector_num
* BDRV_SECTOR_SIZE
;
1251 total_size
= bdrv_getlength(bs
);
1252 if (total_size
< 0) {
1254 } else if (start
>= total_size
) {
1257 } else if (start
+ nb_sectors
* BDRV_SECTOR_SIZE
> total_size
) {
1258 nb_sectors
= DIV_ROUND_UP(total_size
- start
, BDRV_SECTOR_SIZE
);
1261 ret
= find_allocation(bs
, start
, &data
, &hole
);
1262 if (ret
== -ENXIO
) {
1265 ret
= BDRV_BLOCK_ZERO
;
1266 } else if (ret
< 0) {
1267 /* No info available, so pretend there are no holes */
1269 ret
= BDRV_BLOCK_DATA
;
1270 } else if (data
== start
) {
1271 /* On a data extent, compute sectors to the end of the extent,
1272 * possibly including a partial sector at EOF. */
1273 *pnum
= MIN(nb_sectors
, DIV_ROUND_UP(hole
- start
, BDRV_SECTOR_SIZE
));
1274 ret
= BDRV_BLOCK_DATA
;
1276 /* On a hole, compute sectors to the beginning of the next extent. */
1277 assert(hole
== start
);
1278 *pnum
= MIN(nb_sectors
, (data
- start
) / BDRV_SECTOR_SIZE
);
1279 ret
= BDRV_BLOCK_ZERO
;
1284 return ret
| BDRV_BLOCK_OFFSET_VALID
| start
;
1288 static BlockDriver bdrv_gluster
= {
1289 .format_name
= "gluster",
1290 .protocol_name
= "gluster",
1291 .instance_size
= sizeof(BDRVGlusterState
),
1292 .bdrv_needs_filename
= false,
1293 .bdrv_file_open
= qemu_gluster_open
,
1294 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1295 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1296 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1297 .bdrv_close
= qemu_gluster_close
,
1298 .bdrv_create
= qemu_gluster_create
,
1299 .bdrv_getlength
= qemu_gluster_getlength
,
1300 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1301 .bdrv_truncate
= qemu_gluster_truncate
,
1302 .bdrv_co_readv
= qemu_gluster_co_readv
,
1303 .bdrv_co_writev
= qemu_gluster_co_writev
,
1304 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1305 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1306 #ifdef CONFIG_GLUSTERFS_DISCARD
1307 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1309 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1310 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1312 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1313 .create_opts
= &qemu_gluster_create_opts
,
1316 static BlockDriver bdrv_gluster_tcp
= {
1317 .format_name
= "gluster",
1318 .protocol_name
= "gluster+tcp",
1319 .instance_size
= sizeof(BDRVGlusterState
),
1320 .bdrv_needs_filename
= false,
1321 .bdrv_file_open
= qemu_gluster_open
,
1322 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1323 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1324 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1325 .bdrv_close
= qemu_gluster_close
,
1326 .bdrv_create
= qemu_gluster_create
,
1327 .bdrv_getlength
= qemu_gluster_getlength
,
1328 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1329 .bdrv_truncate
= qemu_gluster_truncate
,
1330 .bdrv_co_readv
= qemu_gluster_co_readv
,
1331 .bdrv_co_writev
= qemu_gluster_co_writev
,
1332 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1333 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1334 #ifdef CONFIG_GLUSTERFS_DISCARD
1335 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1337 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1338 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1340 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1341 .create_opts
= &qemu_gluster_create_opts
,
1344 static BlockDriver bdrv_gluster_unix
= {
1345 .format_name
= "gluster",
1346 .protocol_name
= "gluster+unix",
1347 .instance_size
= sizeof(BDRVGlusterState
),
1348 .bdrv_needs_filename
= true,
1349 .bdrv_file_open
= qemu_gluster_open
,
1350 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
1351 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
1352 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
1353 .bdrv_close
= qemu_gluster_close
,
1354 .bdrv_create
= qemu_gluster_create
,
1355 .bdrv_getlength
= qemu_gluster_getlength
,
1356 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
1357 .bdrv_truncate
= qemu_gluster_truncate
,
1358 .bdrv_co_readv
= qemu_gluster_co_readv
,
1359 .bdrv_co_writev
= qemu_gluster_co_writev
,
1360 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
1361 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
1362 #ifdef CONFIG_GLUSTERFS_DISCARD
1363 .bdrv_co_pdiscard
= qemu_gluster_co_pdiscard
,
1365 #ifdef CONFIG_GLUSTERFS_ZEROFILL
1366 .bdrv_co_pwrite_zeroes
= qemu_gluster_co_pwrite_zeroes
,
1368 .bdrv_co_get_block_status
= qemu_gluster_co_get_block_status
,
1369 .create_opts
= &qemu_gluster_create_opts
,
1372 /* rdma is deprecated (actually never supported for volfile fetch).
1373 * Let's maintain it for the protocol compatibility, to make sure things
1374 * won't break immediately. For now, gluster+rdma will fall back to gluster+tcp
1375 * protocol with a warning.
1376 * TODO: remove gluster+rdma interface support
1378 static BlockDriver bdrv_gluster_rdma
= {
1379 .format_name
= "gluster",
1380 .protocol_name
= "gluster+rdma",
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 static void bdrv_gluster_init(void)
1408 bdrv_register(&bdrv_gluster_rdma
);
1409 bdrv_register(&bdrv_gluster_unix
);
1410 bdrv_register(&bdrv_gluster_tcp
);
1411 bdrv_register(&bdrv_gluster
);
1414 block_init(bdrv_gluster_init
);