2 * GlusterFS backend for QEMU
4 * Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.ibm.com>
6 * Pipe handling mechanism in AIO implementation is derived from
9 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
10 * Josh Durgin <josh.durgin@dreamhost.com>
12 * This work is licensed under the terms of the GNU GPL, version 2. See
13 * the COPYING file in the top-level directory.
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
18 #include <glusterfs/api/glfs.h>
19 #include "block/block_int.h"
20 #include "qemu/sockets.h"
23 typedef struct GlusterAIOCB
{
30 typedef struct BDRVGlusterState
{
35 #define GLUSTER_FD_READ 0
36 #define GLUSTER_FD_WRITE 1
38 typedef struct GlusterConf
{
46 static void qemu_gluster_gconf_free(GlusterConf
*gconf
)
49 g_free(gconf
->server
);
50 g_free(gconf
->volname
);
52 g_free(gconf
->transport
);
57 static int parse_volume_options(GlusterConf
*gconf
, char *path
)
66 p
= q
= path
+ strspn(path
, "/");
71 gconf
->volname
= g_strndup(q
, p
- q
);
78 gconf
->image
= g_strdup(p
);
83 * file=gluster[+transport]://[server[:port]]/volname/image[?socket=...]
85 * 'gluster' is the protocol.
87 * 'transport' specifies the transport type used to connect to gluster
88 * management daemon (glusterd). Valid transport types are
89 * tcp, unix and rdma. If a transport type isn't specified, then tcp
92 * 'server' specifies the server where the volume file specification for
93 * the given volume resides. This can be either hostname, ipv4 address
94 * or ipv6 address. ipv6 address needs to be within square brackets [ ].
95 * If transport type is 'unix', then 'server' field should not be specifed.
96 * The 'socket' field needs to be populated with the path to unix domain
99 * 'port' is the port number on which glusterd is listening. This is optional
100 * and if not specified, QEMU will send 0 which will make gluster to use the
101 * default port. If the transport type is unix, then 'port' should not be
104 * 'volname' is the name of the gluster volume which contains the VM image.
106 * 'image' is the path to the actual VM image that resides on gluster volume.
110 * file=gluster://1.2.3.4/testvol/a.img
111 * file=gluster+tcp://1.2.3.4/testvol/a.img
112 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
113 * file=gluster+tcp://[1:2:3:4:5:6:7:8]/testvol/dir/a.img
114 * file=gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img
115 * file=gluster+tcp://server.domain.com:24007/testvol/dir/a.img
116 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
117 * file=gluster+rdma://1.2.3.4:24007/testvol/a.img
119 static int qemu_gluster_parseuri(GlusterConf
*gconf
, const char *filename
)
122 QueryParams
*qp
= NULL
;
123 bool is_unix
= false;
126 uri
= uri_parse(filename
);
132 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
133 gconf
->transport
= g_strdup("tcp");
134 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
135 gconf
->transport
= g_strdup("tcp");
136 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
137 gconf
->transport
= g_strdup("unix");
139 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
140 gconf
->transport
= g_strdup("rdma");
146 ret
= parse_volume_options(gconf
, uri
->path
);
151 qp
= query_params_parse(uri
->query
);
152 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
158 if (uri
->server
|| uri
->port
) {
162 if (strcmp(qp
->p
[0].name
, "socket")) {
166 gconf
->server
= g_strdup(qp
->p
[0].value
);
168 gconf
->server
= g_strdup(uri
->server
? uri
->server
: "localhost");
169 gconf
->port
= uri
->port
;
174 query_params_free(qp
);
180 static struct glfs
*qemu_gluster_init(GlusterConf
*gconf
, const char *filename
,
183 struct glfs
*glfs
= NULL
;
187 ret
= qemu_gluster_parseuri(gconf
, filename
);
189 error_setg(errp
, "Usage: file=gluster[+transport]://[server[:port]]/"
190 "volname/image[?socket=...]");
195 glfs
= glfs_new(gconf
->volname
);
200 ret
= glfs_set_volfile_server(glfs
, gconf
->transport
, gconf
->server
,
207 * TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
208 * GlusterFS makes GF_LOG_* macros available to libgfapi users.
210 ret
= glfs_set_logging(glfs
, "-", 4);
215 ret
= glfs_init(glfs
);
217 error_setg_errno(errp
, errno
,
218 "Gluster connection failed for server=%s port=%d "
219 "volume=%s image=%s transport=%s", gconf
->server
,
220 gconf
->port
, gconf
->volname
, gconf
->image
,
235 static void qemu_gluster_complete_aio(void *opaque
)
237 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
239 qemu_bh_delete(acb
->bh
);
241 qemu_coroutine_enter(acb
->coroutine
, NULL
);
245 * AIO callback routine called from GlusterFS thread.
247 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
249 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
251 if (!ret
|| ret
== acb
->size
) {
252 acb
->ret
= 0; /* Success */
253 } else if (ret
< 0) {
254 acb
->ret
= ret
; /* Read/Write failed */
256 acb
->ret
= -EIO
; /* Partial read/write - fail it */
259 acb
->bh
= qemu_bh_new(qemu_gluster_complete_aio
, acb
);
260 qemu_bh_schedule(acb
->bh
);
263 /* TODO Convert to fine grained options */
264 static QemuOptsList runtime_opts
= {
266 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
270 .type
= QEMU_OPT_STRING
,
271 .help
= "URL to the gluster image",
273 { /* end of list */ }
277 static void qemu_gluster_parse_flags(int bdrv_flags
, int *open_flags
)
279 assert(open_flags
!= NULL
);
281 *open_flags
|= O_BINARY
;
283 if (bdrv_flags
& BDRV_O_RDWR
) {
284 *open_flags
|= O_RDWR
;
286 *open_flags
|= O_RDONLY
;
289 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
290 *open_flags
|= O_DIRECT
;
294 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
295 int bdrv_flags
, Error
**errp
)
297 BDRVGlusterState
*s
= bs
->opaque
;
300 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
302 Error
*local_err
= NULL
;
303 const char *filename
;
305 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
306 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
308 error_propagate(errp
, local_err
);
313 filename
= qemu_opt_get(opts
, "filename");
315 s
->glfs
= qemu_gluster_init(gconf
, filename
, errp
);
321 qemu_gluster_parse_flags(bdrv_flags
, &open_flags
);
323 s
->fd
= glfs_open(s
->glfs
, gconf
->image
, open_flags
);
330 qemu_gluster_gconf_free(gconf
);
343 #ifdef CONFIG_GLUSTERFS_ZEROFILL
344 static coroutine_fn
int qemu_gluster_co_write_zeroes(BlockDriverState
*bs
,
345 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
348 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
349 BDRVGlusterState
*s
= bs
->opaque
;
350 off_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
351 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
355 acb
->coroutine
= qemu_coroutine_self();
357 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
363 qemu_coroutine_yield();
367 g_slice_free(GlusterAIOCB
, acb
);
371 static inline bool gluster_supports_zerofill(void)
376 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
379 return glfs_zerofill(fd
, offset
, size
);
383 static inline bool gluster_supports_zerofill(void)
388 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
395 static int qemu_gluster_create(const char *filename
,
396 QEMUOptionParameter
*options
, Error
**errp
)
402 int64_t total_size
= 0;
403 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
405 glfs
= qemu_gluster_init(gconf
, filename
, errp
);
411 while (options
&& options
->name
) {
412 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
413 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
414 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
415 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
417 } else if (!strcmp(options
->value
.s
, "full") &&
418 gluster_supports_zerofill()) {
421 error_setg(errp
, "Invalid preallocation mode: '%s'"
422 " or GlusterFS doesn't support zerofill API",
431 fd
= glfs_creat(glfs
, gconf
->image
,
432 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
436 if (!glfs_ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
)) {
437 if (prealloc
&& qemu_gluster_zerofill(fd
, 0,
438 total_size
* BDRV_SECTOR_SIZE
)) {
445 if (glfs_close(fd
) != 0) {
450 qemu_gluster_gconf_free(gconf
);
457 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
458 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
, int write
)
461 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
462 BDRVGlusterState
*s
= bs
->opaque
;
463 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
464 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
468 acb
->coroutine
= qemu_coroutine_self();
471 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
472 &gluster_finish_aiocb
, acb
);
474 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
475 &gluster_finish_aiocb
, acb
);
483 qemu_coroutine_yield();
487 g_slice_free(GlusterAIOCB
, acb
);
491 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
494 BDRVGlusterState
*s
= bs
->opaque
;
496 ret
= glfs_ftruncate(s
->fd
, offset
);
504 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
505 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
507 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
510 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
511 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
513 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
516 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
519 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
520 BDRVGlusterState
*s
= bs
->opaque
;
524 acb
->coroutine
= qemu_coroutine_self();
526 ret
= glfs_fsync_async(s
->fd
, &gluster_finish_aiocb
, acb
);
532 qemu_coroutine_yield();
536 g_slice_free(GlusterAIOCB
, acb
);
540 #ifdef CONFIG_GLUSTERFS_DISCARD
541 static coroutine_fn
int qemu_gluster_co_discard(BlockDriverState
*bs
,
542 int64_t sector_num
, int nb_sectors
)
545 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
546 BDRVGlusterState
*s
= bs
->opaque
;
547 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
548 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
552 acb
->coroutine
= qemu_coroutine_self();
554 ret
= glfs_discard_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
560 qemu_coroutine_yield();
564 g_slice_free(GlusterAIOCB
, acb
);
569 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
571 BDRVGlusterState
*s
= bs
->opaque
;
574 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
582 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
584 BDRVGlusterState
*s
= bs
->opaque
;
588 ret
= glfs_fstat(s
->fd
, &st
);
592 return st
.st_blocks
* 512;
596 static void qemu_gluster_close(BlockDriverState
*bs
)
598 BDRVGlusterState
*s
= bs
->opaque
;
607 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
609 /* GlusterFS volume could be backed by a block device */
613 static QEMUOptionParameter qemu_gluster_create_options
[] = {
615 .name
= BLOCK_OPT_SIZE
,
617 .help
= "Virtual disk size"
620 .name
= BLOCK_OPT_PREALLOC
,
622 .help
= "Preallocation mode (allowed values: off, full)"
627 static BlockDriver bdrv_gluster
= {
628 .format_name
= "gluster",
629 .protocol_name
= "gluster",
630 .instance_size
= sizeof(BDRVGlusterState
),
631 .bdrv_needs_filename
= true,
632 .bdrv_file_open
= qemu_gluster_open
,
633 .bdrv_close
= qemu_gluster_close
,
634 .bdrv_create
= qemu_gluster_create
,
635 .bdrv_getlength
= qemu_gluster_getlength
,
636 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
637 .bdrv_truncate
= qemu_gluster_truncate
,
638 .bdrv_co_readv
= qemu_gluster_co_readv
,
639 .bdrv_co_writev
= qemu_gluster_co_writev
,
640 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
641 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
642 #ifdef CONFIG_GLUSTERFS_DISCARD
643 .bdrv_co_discard
= qemu_gluster_co_discard
,
645 #ifdef CONFIG_GLUSTERFS_ZEROFILL
646 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
648 .create_options
= qemu_gluster_create_options
,
651 static BlockDriver bdrv_gluster_tcp
= {
652 .format_name
= "gluster",
653 .protocol_name
= "gluster+tcp",
654 .instance_size
= sizeof(BDRVGlusterState
),
655 .bdrv_needs_filename
= true,
656 .bdrv_file_open
= qemu_gluster_open
,
657 .bdrv_close
= qemu_gluster_close
,
658 .bdrv_create
= qemu_gluster_create
,
659 .bdrv_getlength
= qemu_gluster_getlength
,
660 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
661 .bdrv_truncate
= qemu_gluster_truncate
,
662 .bdrv_co_readv
= qemu_gluster_co_readv
,
663 .bdrv_co_writev
= qemu_gluster_co_writev
,
664 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
665 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
666 #ifdef CONFIG_GLUSTERFS_DISCARD
667 .bdrv_co_discard
= qemu_gluster_co_discard
,
669 #ifdef CONFIG_GLUSTERFS_ZEROFILL
670 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
672 .create_options
= qemu_gluster_create_options
,
675 static BlockDriver bdrv_gluster_unix
= {
676 .format_name
= "gluster",
677 .protocol_name
= "gluster+unix",
678 .instance_size
= sizeof(BDRVGlusterState
),
679 .bdrv_needs_filename
= true,
680 .bdrv_file_open
= qemu_gluster_open
,
681 .bdrv_close
= qemu_gluster_close
,
682 .bdrv_create
= qemu_gluster_create
,
683 .bdrv_getlength
= qemu_gluster_getlength
,
684 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
685 .bdrv_truncate
= qemu_gluster_truncate
,
686 .bdrv_co_readv
= qemu_gluster_co_readv
,
687 .bdrv_co_writev
= qemu_gluster_co_writev
,
688 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
689 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
690 #ifdef CONFIG_GLUSTERFS_DISCARD
691 .bdrv_co_discard
= qemu_gluster_co_discard
,
693 #ifdef CONFIG_GLUSTERFS_ZEROFILL
694 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
696 .create_options
= qemu_gluster_create_options
,
699 static BlockDriver bdrv_gluster_rdma
= {
700 .format_name
= "gluster",
701 .protocol_name
= "gluster+rdma",
702 .instance_size
= sizeof(BDRVGlusterState
),
703 .bdrv_needs_filename
= true,
704 .bdrv_file_open
= qemu_gluster_open
,
705 .bdrv_close
= qemu_gluster_close
,
706 .bdrv_create
= qemu_gluster_create
,
707 .bdrv_getlength
= qemu_gluster_getlength
,
708 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
709 .bdrv_truncate
= qemu_gluster_truncate
,
710 .bdrv_co_readv
= qemu_gluster_co_readv
,
711 .bdrv_co_writev
= qemu_gluster_co_writev
,
712 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
713 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
714 #ifdef CONFIG_GLUSTERFS_DISCARD
715 .bdrv_co_discard
= qemu_gluster_co_discard
,
717 #ifdef CONFIG_GLUSTERFS_ZEROFILL
718 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
720 .create_options
= qemu_gluster_create_options
,
723 static void bdrv_gluster_init(void)
725 bdrv_register(&bdrv_gluster_rdma
);
726 bdrv_register(&bdrv_gluster_unix
);
727 bdrv_register(&bdrv_gluster_tcp
);
728 bdrv_register(&bdrv_gluster
);
731 block_init(bdrv_gluster_init
);