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
)
48 g_free(gconf
->server
);
49 g_free(gconf
->volname
);
51 g_free(gconf
->transport
);
55 static int parse_volume_options(GlusterConf
*gconf
, char *path
)
64 p
= q
= path
+ strspn(path
, "/");
69 gconf
->volname
= g_strndup(q
, p
- q
);
76 gconf
->image
= g_strdup(p
);
81 * file=gluster[+transport]://[server[:port]]/volname/image[?socket=...]
83 * 'gluster' is the protocol.
85 * 'transport' specifies the transport type used to connect to gluster
86 * management daemon (glusterd). Valid transport types are
87 * tcp, unix and rdma. If a transport type isn't specified, then tcp
90 * 'server' specifies the server where the volume file specification for
91 * the given volume resides. This can be either hostname, ipv4 address
92 * or ipv6 address. ipv6 address needs to be within square brackets [ ].
93 * If transport type is 'unix', then 'server' field should not be specifed.
94 * The 'socket' field needs to be populated with the path to unix domain
97 * 'port' is the port number on which glusterd is listening. This is optional
98 * and if not specified, QEMU will send 0 which will make gluster to use the
99 * default port. If the transport type is unix, then 'port' should not be
102 * 'volname' is the name of the gluster volume which contains the VM image.
104 * 'image' is the path to the actual VM image that resides on gluster volume.
108 * file=gluster://1.2.3.4/testvol/a.img
109 * file=gluster+tcp://1.2.3.4/testvol/a.img
110 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
111 * file=gluster+tcp://[1:2:3:4:5:6:7:8]/testvol/dir/a.img
112 * file=gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img
113 * file=gluster+tcp://server.domain.com:24007/testvol/dir/a.img
114 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
115 * file=gluster+rdma://1.2.3.4:24007/testvol/a.img
117 static int qemu_gluster_parseuri(GlusterConf
*gconf
, const char *filename
)
120 QueryParams
*qp
= NULL
;
121 bool is_unix
= false;
124 uri
= uri_parse(filename
);
130 if (!uri
->scheme
|| !strcmp(uri
->scheme
, "gluster")) {
131 gconf
->transport
= g_strdup("tcp");
132 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
133 gconf
->transport
= g_strdup("tcp");
134 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
135 gconf
->transport
= g_strdup("unix");
137 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
138 gconf
->transport
= g_strdup("rdma");
144 ret
= parse_volume_options(gconf
, uri
->path
);
149 qp
= query_params_parse(uri
->query
);
150 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
156 if (uri
->server
|| uri
->port
) {
160 if (strcmp(qp
->p
[0].name
, "socket")) {
164 gconf
->server
= g_strdup(qp
->p
[0].value
);
166 gconf
->server
= g_strdup(uri
->server
? uri
->server
: "localhost");
167 gconf
->port
= uri
->port
;
172 query_params_free(qp
);
178 static struct glfs
*qemu_gluster_init(GlusterConf
*gconf
, const char *filename
,
181 struct glfs
*glfs
= NULL
;
185 ret
= qemu_gluster_parseuri(gconf
, filename
);
187 error_setg(errp
, "Usage: file=gluster[+transport]://[server[:port]]/"
188 "volname/image[?socket=...]");
193 glfs
= glfs_new(gconf
->volname
);
198 ret
= glfs_set_volfile_server(glfs
, gconf
->transport
, gconf
->server
,
205 * TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
206 * GlusterFS makes GF_LOG_* macros available to libgfapi users.
208 ret
= glfs_set_logging(glfs
, "-", 4);
213 ret
= glfs_init(glfs
);
215 error_setg_errno(errp
, errno
,
216 "Gluster connection failed for server=%s port=%d "
217 "volume=%s image=%s transport=%s", gconf
->server
,
218 gconf
->port
, gconf
->volname
, gconf
->image
,
233 static void qemu_gluster_complete_aio(void *opaque
)
235 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
237 qemu_bh_delete(acb
->bh
);
239 qemu_coroutine_enter(acb
->coroutine
, NULL
);
243 * AIO callback routine called from GlusterFS thread.
245 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
247 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
249 if (!ret
|| ret
== acb
->size
) {
250 acb
->ret
= 0; /* Success */
251 } else if (ret
< 0) {
252 acb
->ret
= ret
; /* Read/Write failed */
254 acb
->ret
= -EIO
; /* Partial read/write - fail it */
257 acb
->bh
= qemu_bh_new(qemu_gluster_complete_aio
, acb
);
258 qemu_bh_schedule(acb
->bh
);
261 /* TODO Convert to fine grained options */
262 static QemuOptsList runtime_opts
= {
264 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
268 .type
= QEMU_OPT_STRING
,
269 .help
= "URL to the gluster image",
271 { /* end of list */ }
275 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
276 int bdrv_flags
, Error
**errp
)
278 BDRVGlusterState
*s
= bs
->opaque
;
279 int open_flags
= O_BINARY
;
281 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
283 Error
*local_err
= NULL
;
284 const char *filename
;
286 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
287 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
289 error_propagate(errp
, local_err
);
294 filename
= qemu_opt_get(opts
, "filename");
296 s
->glfs
= qemu_gluster_init(gconf
, filename
, errp
);
302 if (bdrv_flags
& BDRV_O_RDWR
) {
303 open_flags
|= O_RDWR
;
305 open_flags
|= O_RDONLY
;
308 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
309 open_flags
|= O_DIRECT
;
312 s
->fd
= glfs_open(s
->glfs
, gconf
->image
, open_flags
);
319 qemu_gluster_gconf_free(gconf
);
332 #ifdef CONFIG_GLUSTERFS_ZEROFILL
333 static coroutine_fn
int qemu_gluster_co_write_zeroes(BlockDriverState
*bs
,
334 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
337 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
338 BDRVGlusterState
*s
= bs
->opaque
;
339 off_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
340 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
344 acb
->coroutine
= qemu_coroutine_self();
346 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
352 qemu_coroutine_yield();
356 g_slice_free(GlusterAIOCB
, acb
);
360 static inline bool gluster_supports_zerofill(void)
365 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
368 return glfs_zerofill(fd
, offset
, size
);
372 static inline bool gluster_supports_zerofill(void)
377 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
384 static int qemu_gluster_create(const char *filename
,
385 QEMUOptionParameter
*options
, Error
**errp
)
391 int64_t total_size
= 0;
392 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
394 glfs
= qemu_gluster_init(gconf
, filename
, errp
);
400 while (options
&& options
->name
) {
401 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
402 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
403 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
404 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
406 } else if (!strcmp(options
->value
.s
, "full") &&
407 gluster_supports_zerofill()) {
410 error_setg(errp
, "Invalid preallocation mode: '%s'"
411 " or GlusterFS doesn't support zerofill API",
420 fd
= glfs_creat(glfs
, gconf
->image
,
421 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
425 if (!glfs_ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
)) {
426 if (prealloc
&& qemu_gluster_zerofill(fd
, 0,
427 total_size
* BDRV_SECTOR_SIZE
)) {
434 if (glfs_close(fd
) != 0) {
439 qemu_gluster_gconf_free(gconf
);
446 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
447 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
, int write
)
450 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
451 BDRVGlusterState
*s
= bs
->opaque
;
452 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
453 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
457 acb
->coroutine
= qemu_coroutine_self();
460 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
461 &gluster_finish_aiocb
, acb
);
463 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
464 &gluster_finish_aiocb
, acb
);
472 qemu_coroutine_yield();
476 g_slice_free(GlusterAIOCB
, acb
);
480 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
483 BDRVGlusterState
*s
= bs
->opaque
;
485 ret
= glfs_ftruncate(s
->fd
, offset
);
493 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
494 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
496 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
499 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
500 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
502 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
505 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
508 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
509 BDRVGlusterState
*s
= bs
->opaque
;
513 acb
->coroutine
= qemu_coroutine_self();
515 ret
= glfs_fsync_async(s
->fd
, &gluster_finish_aiocb
, acb
);
521 qemu_coroutine_yield();
525 g_slice_free(GlusterAIOCB
, acb
);
529 #ifdef CONFIG_GLUSTERFS_DISCARD
530 static coroutine_fn
int qemu_gluster_co_discard(BlockDriverState
*bs
,
531 int64_t sector_num
, int nb_sectors
)
534 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
535 BDRVGlusterState
*s
= bs
->opaque
;
536 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
537 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
541 acb
->coroutine
= qemu_coroutine_self();
543 ret
= glfs_discard_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
549 qemu_coroutine_yield();
553 g_slice_free(GlusterAIOCB
, acb
);
558 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
560 BDRVGlusterState
*s
= bs
->opaque
;
563 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
571 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
573 BDRVGlusterState
*s
= bs
->opaque
;
577 ret
= glfs_fstat(s
->fd
, &st
);
581 return st
.st_blocks
* 512;
585 static void qemu_gluster_close(BlockDriverState
*bs
)
587 BDRVGlusterState
*s
= bs
->opaque
;
596 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
598 /* GlusterFS volume could be backed by a block device */
602 static QEMUOptionParameter qemu_gluster_create_options
[] = {
604 .name
= BLOCK_OPT_SIZE
,
606 .help
= "Virtual disk size"
609 .name
= BLOCK_OPT_PREALLOC
,
611 .help
= "Preallocation mode (allowed values: off, full)"
616 static BlockDriver bdrv_gluster
= {
617 .format_name
= "gluster",
618 .protocol_name
= "gluster",
619 .instance_size
= sizeof(BDRVGlusterState
),
620 .bdrv_needs_filename
= true,
621 .bdrv_file_open
= qemu_gluster_open
,
622 .bdrv_close
= qemu_gluster_close
,
623 .bdrv_create
= qemu_gluster_create
,
624 .bdrv_getlength
= qemu_gluster_getlength
,
625 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
626 .bdrv_truncate
= qemu_gluster_truncate
,
627 .bdrv_co_readv
= qemu_gluster_co_readv
,
628 .bdrv_co_writev
= qemu_gluster_co_writev
,
629 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
630 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
631 #ifdef CONFIG_GLUSTERFS_DISCARD
632 .bdrv_co_discard
= qemu_gluster_co_discard
,
634 #ifdef CONFIG_GLUSTERFS_ZEROFILL
635 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
637 .create_options
= qemu_gluster_create_options
,
640 static BlockDriver bdrv_gluster_tcp
= {
641 .format_name
= "gluster",
642 .protocol_name
= "gluster+tcp",
643 .instance_size
= sizeof(BDRVGlusterState
),
644 .bdrv_needs_filename
= true,
645 .bdrv_file_open
= qemu_gluster_open
,
646 .bdrv_close
= qemu_gluster_close
,
647 .bdrv_create
= qemu_gluster_create
,
648 .bdrv_getlength
= qemu_gluster_getlength
,
649 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
650 .bdrv_truncate
= qemu_gluster_truncate
,
651 .bdrv_co_readv
= qemu_gluster_co_readv
,
652 .bdrv_co_writev
= qemu_gluster_co_writev
,
653 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
654 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
655 #ifdef CONFIG_GLUSTERFS_DISCARD
656 .bdrv_co_discard
= qemu_gluster_co_discard
,
658 #ifdef CONFIG_GLUSTERFS_ZEROFILL
659 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
661 .create_options
= qemu_gluster_create_options
,
664 static BlockDriver bdrv_gluster_unix
= {
665 .format_name
= "gluster",
666 .protocol_name
= "gluster+unix",
667 .instance_size
= sizeof(BDRVGlusterState
),
668 .bdrv_needs_filename
= true,
669 .bdrv_file_open
= qemu_gluster_open
,
670 .bdrv_close
= qemu_gluster_close
,
671 .bdrv_create
= qemu_gluster_create
,
672 .bdrv_getlength
= qemu_gluster_getlength
,
673 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
674 .bdrv_truncate
= qemu_gluster_truncate
,
675 .bdrv_co_readv
= qemu_gluster_co_readv
,
676 .bdrv_co_writev
= qemu_gluster_co_writev
,
677 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
678 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
679 #ifdef CONFIG_GLUSTERFS_DISCARD
680 .bdrv_co_discard
= qemu_gluster_co_discard
,
682 #ifdef CONFIG_GLUSTERFS_ZEROFILL
683 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
685 .create_options
= qemu_gluster_create_options
,
688 static BlockDriver bdrv_gluster_rdma
= {
689 .format_name
= "gluster",
690 .protocol_name
= "gluster+rdma",
691 .instance_size
= sizeof(BDRVGlusterState
),
692 .bdrv_needs_filename
= true,
693 .bdrv_file_open
= qemu_gluster_open
,
694 .bdrv_close
= qemu_gluster_close
,
695 .bdrv_create
= qemu_gluster_create
,
696 .bdrv_getlength
= qemu_gluster_getlength
,
697 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
698 .bdrv_truncate
= qemu_gluster_truncate
,
699 .bdrv_co_readv
= qemu_gluster_co_readv
,
700 .bdrv_co_writev
= qemu_gluster_co_writev
,
701 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
702 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
703 #ifdef CONFIG_GLUSTERFS_DISCARD
704 .bdrv_co_discard
= qemu_gluster_co_discard
,
706 #ifdef CONFIG_GLUSTERFS_ZEROFILL
707 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
709 .create_options
= qemu_gluster_create_options
,
712 static void bdrv_gluster_init(void)
714 bdrv_register(&bdrv_gluster_rdma
);
715 bdrv_register(&bdrv_gluster_unix
);
716 bdrv_register(&bdrv_gluster_tcp
);
717 bdrv_register(&bdrv_gluster
);
720 block_init(bdrv_gluster_init
);