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 (!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
);
167 gconf
->port
= uri
->port
;
172 query_params_free(qp
);
178 static struct glfs
*qemu_gluster_init(GlusterConf
*gconf
, const char *filename
)
180 struct glfs
*glfs
= NULL
;
184 ret
= qemu_gluster_parseuri(gconf
, filename
);
186 error_report("Usage: file=gluster[+transport]://[server[:port]]/"
187 "volname/image[?socket=...]");
192 glfs
= glfs_new(gconf
->volname
);
197 ret
= glfs_set_volfile_server(glfs
, gconf
->transport
, gconf
->server
,
204 * TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
205 * GlusterFS makes GF_LOG_* macros available to libgfapi users.
207 ret
= glfs_set_logging(glfs
, "-", 4);
212 ret
= glfs_init(glfs
);
214 error_report("Gluster connection failed for server=%s port=%d "
215 "volume=%s image=%s transport=%s", gconf
->server
, gconf
->port
,
216 gconf
->volname
, gconf
->image
, gconf
->transport
);
230 static void qemu_gluster_complete_aio(void *opaque
)
232 GlusterAIOCB
*acb
= (GlusterAIOCB
*)opaque
;
234 qemu_bh_delete(acb
->bh
);
236 qemu_coroutine_enter(acb
->coroutine
, NULL
);
240 * AIO callback routine called from GlusterFS thread.
242 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
244 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
246 if (!ret
|| ret
== acb
->size
) {
247 acb
->ret
= 0; /* Success */
248 } else if (ret
< 0) {
249 acb
->ret
= ret
; /* Read/Write failed */
251 acb
->ret
= -EIO
; /* Partial read/write - fail it */
254 acb
->bh
= qemu_bh_new(qemu_gluster_complete_aio
, acb
);
255 qemu_bh_schedule(acb
->bh
);
258 /* TODO Convert to fine grained options */
259 static QemuOptsList runtime_opts
= {
261 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
265 .type
= QEMU_OPT_STRING
,
266 .help
= "URL to the gluster image",
268 { /* end of list */ }
272 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
273 int bdrv_flags
, Error
**errp
)
275 BDRVGlusterState
*s
= bs
->opaque
;
276 int open_flags
= O_BINARY
;
278 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
280 Error
*local_err
= NULL
;
281 const char *filename
;
283 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
284 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
285 if (error_is_set(&local_err
)) {
286 qerror_report_err(local_err
);
287 error_free(local_err
);
292 filename
= qemu_opt_get(opts
, "filename");
294 s
->glfs
= qemu_gluster_init(gconf
, filename
);
300 if (bdrv_flags
& BDRV_O_RDWR
) {
301 open_flags
|= O_RDWR
;
303 open_flags
|= O_RDONLY
;
306 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
307 open_flags
|= O_DIRECT
;
310 s
->fd
= glfs_open(s
->glfs
, gconf
->image
, open_flags
);
317 qemu_gluster_gconf_free(gconf
);
330 #ifdef CONFIG_GLUSTERFS_ZEROFILL
331 static coroutine_fn
int qemu_gluster_co_write_zeroes(BlockDriverState
*bs
,
332 int64_t sector_num
, int nb_sectors
, BdrvRequestFlags flags
)
335 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
336 BDRVGlusterState
*s
= bs
->opaque
;
337 off_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
338 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
342 acb
->coroutine
= qemu_coroutine_self();
344 ret
= glfs_zerofill_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
350 qemu_coroutine_yield();
354 g_slice_free(GlusterAIOCB
, acb
);
358 static inline bool gluster_supports_zerofill(void)
363 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
366 return glfs_zerofill(fd
, offset
, size
);
370 static inline bool gluster_supports_zerofill(void)
375 static inline int qemu_gluster_zerofill(struct glfs_fd
*fd
, int64_t offset
,
382 static int qemu_gluster_create(const char *filename
,
383 QEMUOptionParameter
*options
, Error
**errp
)
389 int64_t total_size
= 0;
390 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
392 glfs
= qemu_gluster_init(gconf
, filename
);
398 while (options
&& options
->name
) {
399 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
400 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
401 } else if (!strcmp(options
->name
, BLOCK_OPT_PREALLOC
)) {
402 if (!options
->value
.s
|| !strcmp(options
->value
.s
, "off")) {
404 } else if (!strcmp(options
->value
.s
, "full") &&
405 gluster_supports_zerofill()) {
408 error_setg(errp
, "Invalid preallocation mode: '%s'"
409 " or GlusterFS doesn't support zerofill API",
418 fd
= glfs_creat(glfs
, gconf
->image
,
419 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
423 if (!glfs_ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
)) {
424 if (prealloc
&& qemu_gluster_zerofill(fd
, 0,
425 total_size
* BDRV_SECTOR_SIZE
)) {
432 if (glfs_close(fd
) != 0) {
437 qemu_gluster_gconf_free(gconf
);
444 static coroutine_fn
int qemu_gluster_co_rw(BlockDriverState
*bs
,
445 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
, int write
)
448 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
449 BDRVGlusterState
*s
= bs
->opaque
;
450 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
451 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
455 acb
->coroutine
= qemu_coroutine_self();
458 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
459 &gluster_finish_aiocb
, acb
);
461 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
462 &gluster_finish_aiocb
, acb
);
470 qemu_coroutine_yield();
474 g_slice_free(GlusterAIOCB
, acb
);
478 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
481 BDRVGlusterState
*s
= bs
->opaque
;
483 ret
= glfs_ftruncate(s
->fd
, offset
);
491 static coroutine_fn
int qemu_gluster_co_readv(BlockDriverState
*bs
,
492 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
494 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 0);
497 static coroutine_fn
int qemu_gluster_co_writev(BlockDriverState
*bs
,
498 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
500 return qemu_gluster_co_rw(bs
, sector_num
, nb_sectors
, qiov
, 1);
503 static coroutine_fn
int qemu_gluster_co_flush_to_disk(BlockDriverState
*bs
)
506 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
507 BDRVGlusterState
*s
= bs
->opaque
;
511 acb
->coroutine
= qemu_coroutine_self();
513 ret
= glfs_fsync_async(s
->fd
, &gluster_finish_aiocb
, acb
);
519 qemu_coroutine_yield();
523 g_slice_free(GlusterAIOCB
, acb
);
527 #ifdef CONFIG_GLUSTERFS_DISCARD
528 static coroutine_fn
int qemu_gluster_co_discard(BlockDriverState
*bs
,
529 int64_t sector_num
, int nb_sectors
)
532 GlusterAIOCB
*acb
= g_slice_new(GlusterAIOCB
);
533 BDRVGlusterState
*s
= bs
->opaque
;
534 size_t size
= nb_sectors
* BDRV_SECTOR_SIZE
;
535 off_t offset
= sector_num
* BDRV_SECTOR_SIZE
;
539 acb
->coroutine
= qemu_coroutine_self();
541 ret
= glfs_discard_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
547 qemu_coroutine_yield();
551 g_slice_free(GlusterAIOCB
, acb
);
556 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
558 BDRVGlusterState
*s
= bs
->opaque
;
561 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
569 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
571 BDRVGlusterState
*s
= bs
->opaque
;
575 ret
= glfs_fstat(s
->fd
, &st
);
579 return st
.st_blocks
* 512;
583 static void qemu_gluster_close(BlockDriverState
*bs
)
585 BDRVGlusterState
*s
= bs
->opaque
;
594 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
596 /* GlusterFS volume could be backed by a block device */
600 static QEMUOptionParameter qemu_gluster_create_options
[] = {
602 .name
= BLOCK_OPT_SIZE
,
604 .help
= "Virtual disk size"
607 .name
= BLOCK_OPT_PREALLOC
,
609 .help
= "Preallocation mode (allowed values: off, full)"
614 static BlockDriver bdrv_gluster
= {
615 .format_name
= "gluster",
616 .protocol_name
= "gluster",
617 .instance_size
= sizeof(BDRVGlusterState
),
618 .bdrv_needs_filename
= true,
619 .bdrv_file_open
= qemu_gluster_open
,
620 .bdrv_close
= qemu_gluster_close
,
621 .bdrv_create
= qemu_gluster_create
,
622 .bdrv_getlength
= qemu_gluster_getlength
,
623 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
624 .bdrv_truncate
= qemu_gluster_truncate
,
625 .bdrv_co_readv
= qemu_gluster_co_readv
,
626 .bdrv_co_writev
= qemu_gluster_co_writev
,
627 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
628 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
629 #ifdef CONFIG_GLUSTERFS_DISCARD
630 .bdrv_co_discard
= qemu_gluster_co_discard
,
632 #ifdef CONFIG_GLUSTERFS_ZEROFILL
633 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
635 .create_options
= qemu_gluster_create_options
,
638 static BlockDriver bdrv_gluster_tcp
= {
639 .format_name
= "gluster",
640 .protocol_name
= "gluster+tcp",
641 .instance_size
= sizeof(BDRVGlusterState
),
642 .bdrv_needs_filename
= true,
643 .bdrv_file_open
= qemu_gluster_open
,
644 .bdrv_close
= qemu_gluster_close
,
645 .bdrv_create
= qemu_gluster_create
,
646 .bdrv_getlength
= qemu_gluster_getlength
,
647 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
648 .bdrv_truncate
= qemu_gluster_truncate
,
649 .bdrv_co_readv
= qemu_gluster_co_readv
,
650 .bdrv_co_writev
= qemu_gluster_co_writev
,
651 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
652 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
653 #ifdef CONFIG_GLUSTERFS_DISCARD
654 .bdrv_co_discard
= qemu_gluster_co_discard
,
656 #ifdef CONFIG_GLUSTERFS_ZEROFILL
657 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
659 .create_options
= qemu_gluster_create_options
,
662 static BlockDriver bdrv_gluster_unix
= {
663 .format_name
= "gluster",
664 .protocol_name
= "gluster+unix",
665 .instance_size
= sizeof(BDRVGlusterState
),
666 .bdrv_needs_filename
= true,
667 .bdrv_file_open
= qemu_gluster_open
,
668 .bdrv_close
= qemu_gluster_close
,
669 .bdrv_create
= qemu_gluster_create
,
670 .bdrv_getlength
= qemu_gluster_getlength
,
671 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
672 .bdrv_truncate
= qemu_gluster_truncate
,
673 .bdrv_co_readv
= qemu_gluster_co_readv
,
674 .bdrv_co_writev
= qemu_gluster_co_writev
,
675 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
676 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
677 #ifdef CONFIG_GLUSTERFS_DISCARD
678 .bdrv_co_discard
= qemu_gluster_co_discard
,
680 #ifdef CONFIG_GLUSTERFS_ZEROFILL
681 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
683 .create_options
= qemu_gluster_create_options
,
686 static BlockDriver bdrv_gluster_rdma
= {
687 .format_name
= "gluster",
688 .protocol_name
= "gluster+rdma",
689 .instance_size
= sizeof(BDRVGlusterState
),
690 .bdrv_needs_filename
= true,
691 .bdrv_file_open
= qemu_gluster_open
,
692 .bdrv_close
= qemu_gluster_close
,
693 .bdrv_create
= qemu_gluster_create
,
694 .bdrv_getlength
= qemu_gluster_getlength
,
695 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
696 .bdrv_truncate
= qemu_gluster_truncate
,
697 .bdrv_co_readv
= qemu_gluster_co_readv
,
698 .bdrv_co_writev
= qemu_gluster_co_writev
,
699 .bdrv_co_flush_to_disk
= qemu_gluster_co_flush_to_disk
,
700 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
701 #ifdef CONFIG_GLUSTERFS_DISCARD
702 .bdrv_co_discard
= qemu_gluster_co_discard
,
704 #ifdef CONFIG_GLUSTERFS_ZEROFILL
705 .bdrv_co_write_zeroes
= qemu_gluster_co_write_zeroes
,
707 .create_options
= qemu_gluster_create_options
,
710 static void bdrv_gluster_init(void)
712 bdrv_register(&bdrv_gluster_rdma
);
713 bdrv_register(&bdrv_gluster_unix
);
714 bdrv_register(&bdrv_gluster_tcp
);
715 bdrv_register(&bdrv_gluster
);
718 block_init(bdrv_gluster_init
);