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"
16 typedef struct GlusterAIOCB
{
21 AioContext
*aio_context
;
24 typedef struct BDRVGlusterState
{
29 typedef struct GlusterConf
{
37 static void qemu_gluster_gconf_free(GlusterConf
*gconf
)
40 g_free(gconf
->server
);
41 g_free(gconf
->volname
);
43 g_free(gconf
->transport
);
48 static int parse_volume_options(GlusterConf
*gconf
, char *path
)
57 p
= q
= path
+ strspn(path
, "/");
62 gconf
->volname
= g_strndup(q
, p
- q
);
69 gconf
->image
= g_strdup(p
);
74 * file=gluster[+transport]://[server[:port]]/volname/image[?socket=...]
76 * 'gluster' is the protocol.
78 * 'transport' specifies the transport type used to connect to gluster
79 * management daemon (glusterd). Valid transport types are
80 * tcp, unix and rdma. If a transport type isn't specified, then tcp
83 * 'server' specifies the server where the volume file specification for
84 * the given volume resides. This can be either hostname, ipv4 address
85 * or ipv6 address. ipv6 address needs to be within square brackets [ ].
86 * If transport type is 'unix', then 'server' field should not be specified.
87 * The 'socket' field needs to be populated with the path to unix domain
90 * 'port' is the port number on which glusterd is listening. This is optional
91 * and if not specified, QEMU will send 0 which will make gluster to use the
92 * default port. If the transport type is unix, then 'port' should not be
95 * 'volname' is the name of the gluster volume which contains the VM image.
97 * 'image' is the path to the actual VM image that resides on gluster volume.
101 * file=gluster://1.2.3.4/testvol/a.img
102 * file=gluster+tcp://1.2.3.4/testvol/a.img
103 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
104 * file=gluster+tcp://[1:2:3:4:5:6:7:8]/testvol/dir/a.img
105 * file=gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img
106 * file=gluster+tcp://server.domain.com:24007/testvol/dir/a.img
107 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
108 * file=gluster+rdma://1.2.3.4:24007/testvol/a.img
110 static int qemu_gluster_parseuri(GlusterConf
*gconf
, const char *filename
)
113 QueryParams
*qp
= NULL
;
114 bool is_unix
= false;
117 uri
= uri_parse(filename
);
123 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
124 gconf
->transport
= g_strdup("tcp");
125 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
126 gconf
->transport
= g_strdup("tcp");
127 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
128 gconf
->transport
= g_strdup("unix");
130 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
131 gconf
->transport
= g_strdup("rdma");
137 ret
= parse_volume_options(gconf
, uri
->path
);
142 qp
= query_params_parse(uri
->query
);
143 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
149 if (uri
->server
|| uri
->port
) {
153 if (strcmp(qp
->p
[0].name
, "socket")) {
157 gconf
->server
= g_strdup(qp
->p
[0].value
);
159 gconf
->server
= g_strdup(uri
->server
? uri
->server
: "localhost");
160 gconf
->port
= uri
->port
;
165 query_params_free(qp
);
171 static struct glfs
*qemu_gluster_init(GlusterConf
*gconf
, const char *filename
,
174 struct glfs
*glfs
= NULL
;
178 ret
= qemu_gluster_parseuri(gconf
, filename
);
180 error_setg(errp
, "Usage: file=gluster[+transport]://[server[:port]]/"
181 "volname/image[?socket=...]");
186 glfs
= glfs_new(gconf
->volname
);
191 ret
= glfs_set_volfile_server(glfs
, gconf
->transport
, gconf
->server
,
198 * TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
199 * GlusterFS makes GF_LOG_* macros available to libgfapi users.
201 ret
= glfs_set_logging(glfs
, "-", 4);
206 ret
= glfs_init(glfs
);
208 error_setg_errno(errp
, errno
,
209 "Gluster connection failed for server=%s port=%d "
210 "volume=%s image=%s transport=%s", gconf
->server
,
211 gconf
->port
, gconf
->volname
, gconf
->image
,
214 /* glfs_init sometimes doesn't set errno although docs suggest that */
231 static void qemu_gluster_complete_aio(void *opaque
)
233 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
235 qemu_bh_delete(acb
->bh
);
237 qemu_coroutine_enter(acb
->coroutine
, NULL
);
241 * AIO callback routine called from GlusterFS thread.
243 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
245 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
247 if (!ret
|| ret
== acb
->size
) {
248 acb
->ret
= 0; /* Success */
249 } else if (ret
< 0) {
250 acb
->ret
= ret
; /* Read/Write failed */
252 acb
->ret
= -EIO
; /* Partial read/write - fail it */
255 acb
->bh
= aio_bh_new(acb
->aio_context
, qemu_gluster_complete_aio
, acb
);
256 qemu_bh_schedule(acb
->bh
);
259 /* TODO Convert to fine grained options */
260 static QemuOptsList runtime_opts
= {
262 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
266 .type
= QEMU_OPT_STRING
,
267 .help
= "URL to the gluster image",
269 { /* end of list */ }
273 static void qemu_gluster_parse_flags(int bdrv_flags
, int *open_flags
)
275 assert(open_flags
!= NULL
);
277 *open_flags
|= O_BINARY
;
279 if (bdrv_flags
& BDRV_O_RDWR
) {
280 *open_flags
|= O_RDWR
;
282 *open_flags
|= O_RDONLY
;
285 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
286 *open_flags
|= O_DIRECT
;
290 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
291 int bdrv_flags
, Error
**errp
)
293 BDRVGlusterState
*s
= bs
->opaque
;
296 GlusterConf
*gconf
= g_new0(GlusterConf
, 1);
298 Error
*local_err
= NULL
;
299 const char *filename
;
301 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
302 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
304 error_propagate(errp
, local_err
);
309 filename
= qemu_opt_get(opts
, "filename");
311 s
->glfs
= qemu_gluster_init(gconf
, filename
, errp
);
317 qemu_gluster_parse_flags(bdrv_flags
, &open_flags
);
319 s
->fd
= glfs_open(s
->glfs
, gconf
->image
, open_flags
);
326 qemu_gluster_gconf_free(gconf
);
339 typedef struct BDRVGlusterReopenState
{
342 } BDRVGlusterReopenState
;
345 static int qemu_gluster_reopen_prepare(BDRVReopenState
*state
,
346 BlockReopenQueue
*queue
, Error
**errp
)
349 BDRVGlusterReopenState
*reop_s
;
350 GlusterConf
*gconf
= NULL
;
353 assert(state
!= NULL
);
354 assert(state
->bs
!= NULL
);
356 state
->opaque
= g_new0(BDRVGlusterReopenState
, 1);
357 reop_s
= state
->opaque
;
359 qemu_gluster_parse_flags(state
->flags
, &open_flags
);
361 gconf
= g_new0(GlusterConf
, 1);
363 reop_s
->glfs
= qemu_gluster_init(gconf
, state
->bs
->filename
, errp
);
364 if (reop_s
->glfs
== NULL
) {
369 reop_s
->fd
= glfs_open(reop_s
->glfs
, gconf
->image
, open_flags
);
370 if (reop_s
->fd
== NULL
) {
371 /* reops->glfs will be cleaned up in _abort */
377 /* state->opaque will be freed in either the _abort or _commit */
378 qemu_gluster_gconf_free(gconf
);
382 static void qemu_gluster_reopen_commit(BDRVReopenState
*state
)
384 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
385 BDRVGlusterState
*s
= state
->bs
->opaque
;
396 /* use the newly opened image / connection */
398 s
->glfs
= reop_s
->glfs
;
400 g_free(state
->opaque
);
401 state
->opaque
= NULL
;
407 static void qemu_gluster_reopen_abort(BDRVReopenState
*state
)
409 BDRVGlusterReopenState
*reop_s
= state
->opaque
;
411 if (reop_s
== NULL
) {
416 glfs_close(reop_s
->fd
);
420 glfs_fini(reop_s
->glfs
);
423 g_free(state
->opaque
);
424 state
->opaque
= NULL
;
429 #ifdef CONFIG_GLUSTERFS_ZEROFILL
430 static coroutine_fn
int qemu_gluster_co_write_zeroes(BlockDriverState
*bs
,
431 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
435 BDRVGlusterState
*s
= bs
->opaque
;
436 off_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
437 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
441 acb
.coroutine
= qemu_coroutine_self();
442 acb
.aio_context
= bdrv_get_aio_context(bs
);
444 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
449 qemu_coroutine_yield();
453 static inline bool gluster_supports_zerofill(void)
458 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
461 return glfs_zerofill(fd
, offset
, size
);
465 static inline bool gluster_supports_zerofill(void)
470 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
477 static int qemu_gluster_create(const char *filename
,
478 QemuOpts
*opts
, Error
**errp
)
484 int64_t total_size
= 0;
486 GlusterConf
*gconf
= g_new0(GlusterConf
, 1);
488 glfs
= qemu_gluster_init(gconf
, filename
, errp
);
494 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
497 tmp
= qemu_opt_get_del(opts
, BLOCK_OPT_PREALLOC
);
498 if (!tmp
|| !strcmp(tmp
, "off")) {
500 } else if (!strcmp(tmp
, "full") &&
501 gluster_supports_zerofill()) {
504 error_setg(errp
, "Invalid preallocation mode: '%s'"
505 " or GlusterFS doesn't support zerofill API",
511 fd
= glfs_creat(glfs
, gconf
->image
,
512 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
516 if (!glfs_ftruncate(fd
, total_size
)) {
517 if (prealloc
&& qemu_gluster_zerofill(fd
, 0, total_size
)) {
524 if (glfs_close(fd
) != 0) {
530 qemu_gluster_gconf_free(gconf
);
537 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
538 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
, int write
)
542 BDRVGlusterState
*s
= bs
->opaque
;
543 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
544 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
548 acb
.coroutine
= qemu_coroutine_self();
549 acb
.aio_context
= bdrv_get_aio_context(bs
);
552 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
553 gluster_finish_aiocb
, &acb
);
555 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
556 gluster_finish_aiocb
, &acb
);
563 qemu_coroutine_yield();
567 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
570 BDRVGlusterState
*s
= bs
->opaque
;
572 ret
= glfs_ftruncate(s
->fd
, offset
);
580 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
581 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
583 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
586 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
587 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
589 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
592 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
596 BDRVGlusterState
*s
= bs
->opaque
;
600 acb
.coroutine
= qemu_coroutine_self();
601 acb
.aio_context
= bdrv_get_aio_context(bs
);
603 ret
= glfs_fsync_async(s
->fd
, gluster_finish_aiocb
, &acb
);
608 qemu_coroutine_yield();
612 #ifdef CONFIG_GLUSTERFS_DISCARD
613 static coroutine_fn
int qemu_gluster_co_discard(BlockDriverState
*bs
,
614 int64_t sector_num
, int nb_sectors
)
618 BDRVGlusterState
*s
= bs
->opaque
;
619 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
620 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
624 acb
.coroutine
= qemu_coroutine_self();
625 acb
.aio_context
= bdrv_get_aio_context(bs
);
627 ret
= glfs_discard_async(s
->fd
, offset
, size
, gluster_finish_aiocb
, &acb
);
632 qemu_coroutine_yield();
637 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
639 BDRVGlusterState
*s
= bs
->opaque
;
642 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
650 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
652 BDRVGlusterState
*s
= bs
->opaque
;
656 ret
= glfs_fstat(s
->fd
, &st
);
660 return st
.st_blocks
* 512;
664 static void qemu_gluster_close(BlockDriverState
*bs
)
666 BDRVGlusterState
*s
= bs
->opaque
;
675 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
677 /* GlusterFS volume could be backed by a block device */
681 static QemuOptsList qemu_gluster_create_opts
= {
682 .name
= "qemu-gluster-create-opts",
683 .head
= QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts
.head
),
686 .name
= BLOCK_OPT_SIZE
,
687 .type
= QEMU_OPT_SIZE
,
688 .help
= "Virtual disk size"
691 .name
= BLOCK_OPT_PREALLOC
,
692 .type
= QEMU_OPT_STRING
,
693 .help
= "Preallocation mode (allowed values: off, full)"
695 { /* end of list */ }
699 static BlockDriver bdrv_gluster
= {
700 .format_name
= "gluster",
701 .protocol_name
= "gluster",
702 .instance_size
= sizeof(BDRVGlusterState
),
703 .bdrv_needs_filename
= true,
704 .bdrv_file_open
= qemu_gluster_open
,
705 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
706 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
707 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
708 .bdrv_close
= qemu_gluster_close
,
709 .bdrv_create
= qemu_gluster_create
,
710 .bdrv_getlength
= qemu_gluster_getlength
,
711 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
712 .bdrv_truncate
= qemu_gluster_truncate
,
713 .bdrv_co_readv
= qemu_gluster_co_readv
,
714 .bdrv_co_writev
= qemu_gluster_co_writev
,
715 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
716 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
717 #ifdef CONFIG_GLUSTERFS_DISCARD
718 .bdrv_co_discard
= qemu_gluster_co_discard
,
720 #ifdef CONFIG_GLUSTERFS_ZEROFILL
721 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
723 .create_opts
= &qemu_gluster_create_opts
,
726 static BlockDriver bdrv_gluster_tcp
= {
727 .format_name
= "gluster",
728 .protocol_name
= "gluster+tcp",
729 .instance_size
= sizeof(BDRVGlusterState
),
730 .bdrv_needs_filename
= true,
731 .bdrv_file_open
= qemu_gluster_open
,
732 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
733 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
734 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
735 .bdrv_close
= qemu_gluster_close
,
736 .bdrv_create
= qemu_gluster_create
,
737 .bdrv_getlength
= qemu_gluster_getlength
,
738 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
739 .bdrv_truncate
= qemu_gluster_truncate
,
740 .bdrv_co_readv
= qemu_gluster_co_readv
,
741 .bdrv_co_writev
= qemu_gluster_co_writev
,
742 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
743 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
744 #ifdef CONFIG_GLUSTERFS_DISCARD
745 .bdrv_co_discard
= qemu_gluster_co_discard
,
747 #ifdef CONFIG_GLUSTERFS_ZEROFILL
748 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
750 .create_opts
= &qemu_gluster_create_opts
,
753 static BlockDriver bdrv_gluster_unix
= {
754 .format_name
= "gluster",
755 .protocol_name
= "gluster+unix",
756 .instance_size
= sizeof(BDRVGlusterState
),
757 .bdrv_needs_filename
= true,
758 .bdrv_file_open
= qemu_gluster_open
,
759 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
760 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
761 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
762 .bdrv_close
= qemu_gluster_close
,
763 .bdrv_create
= qemu_gluster_create
,
764 .bdrv_getlength
= qemu_gluster_getlength
,
765 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
766 .bdrv_truncate
= qemu_gluster_truncate
,
767 .bdrv_co_readv
= qemu_gluster_co_readv
,
768 .bdrv_co_writev
= qemu_gluster_co_writev
,
769 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
770 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
771 #ifdef CONFIG_GLUSTERFS_DISCARD
772 .bdrv_co_discard
= qemu_gluster_co_discard
,
774 #ifdef CONFIG_GLUSTERFS_ZEROFILL
775 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
777 .create_opts
= &qemu_gluster_create_opts
,
780 static BlockDriver bdrv_gluster_rdma
= {
781 .format_name
= "gluster",
782 .protocol_name
= "gluster+rdma",
783 .instance_size
= sizeof(BDRVGlusterState
),
784 .bdrv_needs_filename
= true,
785 .bdrv_file_open
= qemu_gluster_open
,
786 .bdrv_reopen_prepare
= qemu_gluster_reopen_prepare
,
787 .bdrv_reopen_commit
= qemu_gluster_reopen_commit
,
788 .bdrv_reopen_abort
= qemu_gluster_reopen_abort
,
789 .bdrv_close
= qemu_gluster_close
,
790 .bdrv_create
= qemu_gluster_create
,
791 .bdrv_getlength
= qemu_gluster_getlength
,
792 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
793 .bdrv_truncate
= qemu_gluster_truncate
,
794 .bdrv_co_readv
= qemu_gluster_co_readv
,
795 .bdrv_co_writev
= qemu_gluster_co_writev
,
796 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
797 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
798 #ifdef CONFIG_GLUSTERFS_DISCARD
799 .bdrv_co_discard
= qemu_gluster_co_discard
,
801 #ifdef CONFIG_GLUSTERFS_ZEROFILL
802 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
804 .create_opts
= &qemu_gluster_create_opts
,
807 static void bdrv_gluster_init(void)
809 bdrv_register(&bdrv_gluster_rdma
);
810 bdrv_register(&bdrv_gluster_unix
);
811 bdrv_register(&bdrv_gluster_tcp
);
812 bdrv_register(&bdrv_gluster
);
815 block_init(bdrv_gluster_init
);