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
{
24 BlockDriverAIOCB common
;
31 typedef struct BDRVGlusterState
{
36 GlusterAIOCB
*event_acb
;
39 #define GLUSTER_FD_READ 0
40 #define GLUSTER_FD_WRITE 1
42 typedef struct GlusterConf
{
50 static void qemu_gluster_gconf_free(GlusterConf
*gconf
)
52 g_free(gconf
->server
);
53 g_free(gconf
->volname
);
55 g_free(gconf
->transport
);
59 static int parse_volume_options(GlusterConf
*gconf
, char *path
)
68 p
= q
= path
+ strspn(path
, "/");
73 gconf
->volname
= g_strndup(q
, p
- q
);
80 gconf
->image
= g_strdup(p
);
85 * file=gluster[+transport]://[server[:port]]/volname/image[?socket=...]
87 * 'gluster' is the protocol.
89 * 'transport' specifies the transport type used to connect to gluster
90 * management daemon (glusterd). Valid transport types are
91 * tcp, unix and rdma. If a transport type isn't specified, then tcp
94 * 'server' specifies the server where the volume file specification for
95 * the given volume resides. This can be either hostname, ipv4 address
96 * or ipv6 address. ipv6 address needs to be within square brackets [ ].
97 * If transport type is 'unix', then 'server' field should not be specifed.
98 * The 'socket' field needs to be populated with the path to unix domain
101 * 'port' is the port number on which glusterd is listening. This is optional
102 * and if not specified, QEMU will send 0 which will make gluster to use the
103 * default port. If the transport type is unix, then 'port' should not be
106 * 'volname' is the name of the gluster volume which contains the VM image.
108 * 'image' is the path to the actual VM image that resides on gluster volume.
112 * file=gluster://1.2.3.4/testvol/a.img
113 * file=gluster+tcp://1.2.3.4/testvol/a.img
114 * file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
115 * file=gluster+tcp://[1:2:3:4:5:6:7:8]/testvol/dir/a.img
116 * file=gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img
117 * file=gluster+tcp://server.domain.com:24007/testvol/dir/a.img
118 * file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
119 * file=gluster+rdma://1.2.3.4:24007/testvol/a.img
121 static int qemu_gluster_parseuri(GlusterConf
*gconf
, const char *filename
)
124 QueryParams
*qp
= NULL
;
125 bool is_unix
= false;
128 uri
= uri_parse(filename
);
134 if (!strcmp(uri
->scheme
, "gluster")) {
135 gconf
->transport
= g_strdup("tcp");
136 } else if (!strcmp(uri
->scheme
, "gluster+tcp")) {
137 gconf
->transport
= g_strdup("tcp");
138 } else if (!strcmp(uri
->scheme
, "gluster+unix")) {
139 gconf
->transport
= g_strdup("unix");
141 } else if (!strcmp(uri
->scheme
, "gluster+rdma")) {
142 gconf
->transport
= g_strdup("rdma");
148 ret
= parse_volume_options(gconf
, uri
->path
);
153 qp
= query_params_parse(uri
->query
);
154 if (qp
->n
> 1 || (is_unix
&& !qp
->n
) || (!is_unix
&& qp
->n
)) {
160 if (uri
->server
|| uri
->port
) {
164 if (strcmp(qp
->p
[0].name
, "socket")) {
168 gconf
->server
= g_strdup(qp
->p
[0].value
);
170 gconf
->server
= g_strdup(uri
->server
);
171 gconf
->port
= uri
->port
;
176 query_params_free(qp
);
182 static struct glfs
*qemu_gluster_init(GlusterConf
*gconf
, const char *filename
)
184 struct glfs
*glfs
= NULL
;
188 ret
= qemu_gluster_parseuri(gconf
, filename
);
190 error_report("Usage: file=gluster[+transport]://[server[:port]]/"
191 "volname/image[?socket=...]");
196 glfs
= glfs_new(gconf
->volname
);
201 ret
= glfs_set_volfile_server(glfs
, gconf
->transport
, gconf
->server
,
208 * TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
209 * GlusterFS makes GF_LOG_* macros available to libgfapi users.
211 ret
= glfs_set_logging(glfs
, "-", 4);
216 ret
= glfs_init(glfs
);
218 error_report("Gluster connection failed for server=%s port=%d "
219 "volume=%s image=%s transport=%s", gconf
->server
, gconf
->port
,
220 gconf
->volname
, gconf
->image
, gconf
->transport
);
234 static void qemu_gluster_complete_aio(GlusterAIOCB
*acb
, BDRVGlusterState
*s
)
237 bool *finished
= acb
->finished
;
238 BlockDriverCompletionFunc
*cb
= acb
->common
.cb
;
239 void *opaque
= acb
->common
.opaque
;
241 if (!acb
->ret
|| acb
->ret
== acb
->size
) {
242 ret
= 0; /* Success */
243 } else if (acb
->ret
< 0) {
244 ret
= acb
->ret
; /* Read/Write failed */
246 ret
= -EIO
; /* Partial read/write - fail it */
249 qemu_aio_release(acb
);
256 static void qemu_gluster_aio_event_reader(void *opaque
)
258 BDRVGlusterState
*s
= opaque
;
262 char *p
= (char *)&s
->event_acb
;
264 ret
= read(s
->fds
[GLUSTER_FD_READ
], p
+ s
->event_reader_pos
,
265 sizeof(s
->event_acb
) - s
->event_reader_pos
);
267 s
->event_reader_pos
+= ret
;
268 if (s
->event_reader_pos
== sizeof(s
->event_acb
)) {
269 s
->event_reader_pos
= 0;
270 qemu_gluster_complete_aio(s
->event_acb
, s
);
273 } while (ret
< 0 && errno
== EINTR
);
276 /* TODO Convert to fine grained options */
277 static QemuOptsList runtime_opts
= {
279 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
283 .type
= QEMU_OPT_STRING
,
284 .help
= "URL to the gluster image",
286 { /* end of list */ }
290 static int qemu_gluster_open(BlockDriverState
*bs
, QDict
*options
,
291 int bdrv_flags
, Error
**errp
)
293 BDRVGlusterState
*s
= bs
->opaque
;
294 int open_flags
= O_BINARY
;
296 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
298 Error
*local_err
= NULL
;
299 const char *filename
;
301 opts
= qemu_opts_create_nofail(&runtime_opts
);
302 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
303 if (error_is_set(&local_err
)) {
304 qerror_report_err(local_err
);
305 error_free(local_err
);
310 filename
= qemu_opt_get(opts
, "filename");
313 s
->glfs
= qemu_gluster_init(gconf
, filename
);
319 if (bdrv_flags
& BDRV_O_RDWR
) {
320 open_flags
|= O_RDWR
;
322 open_flags
|= O_RDONLY
;
325 if ((bdrv_flags
& BDRV_O_NOCACHE
)) {
326 open_flags
|= O_DIRECT
;
329 s
->fd
= glfs_open(s
->glfs
, gconf
->image
, open_flags
);
335 ret
= qemu_pipe(s
->fds
);
340 fcntl(s
->fds
[GLUSTER_FD_READ
], F_SETFL
, O_NONBLOCK
);
341 qemu_aio_set_fd_handler(s
->fds
[GLUSTER_FD_READ
],
342 qemu_gluster_aio_event_reader
, NULL
, s
);
346 qemu_gluster_gconf_free(gconf
);
359 static int qemu_gluster_create(const char *filename
,
360 QEMUOptionParameter
*options
, Error
**errp
)
365 int64_t total_size
= 0;
366 GlusterConf
*gconf
= g_malloc0(sizeof(GlusterConf
));
368 glfs
= qemu_gluster_init(gconf
, filename
);
374 while (options
&& options
->name
) {
375 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
376 total_size
= options
->value
.n
/ BDRV_SECTOR_SIZE
;
381 fd
= glfs_creat(glfs
, gconf
->image
,
382 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
, S_IRUSR
| S_IWUSR
);
386 if (glfs_ftruncate(fd
, total_size
* BDRV_SECTOR_SIZE
) != 0) {
389 if (glfs_close(fd
) != 0) {
394 qemu_gluster_gconf_free(gconf
);
401 static void qemu_gluster_aio_cancel(BlockDriverAIOCB
*blockacb
)
403 GlusterAIOCB
*acb
= (GlusterAIOCB
*)blockacb
;
404 bool finished
= false;
406 acb
->finished
= &finished
;
412 static const AIOCBInfo gluster_aiocb_info
= {
413 .aiocb_size
= sizeof(GlusterAIOCB
),
414 .cancel
= qemu_gluster_aio_cancel
,
417 static void gluster_finish_aiocb(struct glfs_fd
*fd
, ssize_t ret
, void *arg
)
419 GlusterAIOCB
*acb
= (GlusterAIOCB
*)arg
;
420 BlockDriverState
*bs
= acb
->common
.bs
;
421 BDRVGlusterState
*s
= bs
->opaque
;
425 retval
= qemu_write_full(s
->fds
[GLUSTER_FD_WRITE
], &acb
, sizeof(acb
));
426 if (retval
!= sizeof(acb
)) {
428 * Gluster AIO callback thread failed to notify the waiting
429 * QEMU thread about IO completion.
431 error_report("Gluster AIO completion failed: %s", strerror(errno
));
436 static BlockDriverAIOCB
*qemu_gluster_aio_rw(BlockDriverState
*bs
,
437 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
438 BlockDriverCompletionFunc
*cb
, void *opaque
, int write
)
442 BDRVGlusterState
*s
= bs
->opaque
;
446 offset
= sector_num
* BDRV_SECTOR_SIZE
;
447 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
449 acb
= qemu_aio_get(&gluster_aiocb_info
, bs
, cb
, opaque
);
452 acb
->finished
= NULL
;
455 ret
= glfs_pwritev_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
456 &gluster_finish_aiocb
, acb
);
458 ret
= glfs_preadv_async(s
->fd
, qiov
->iov
, qiov
->niov
, offset
, 0,
459 &gluster_finish_aiocb
, acb
);
468 qemu_aio_release(acb
);
472 static int qemu_gluster_truncate(BlockDriverState
*bs
, int64_t offset
)
475 BDRVGlusterState
*s
= bs
->opaque
;
477 ret
= glfs_ftruncate(s
->fd
, offset
);
485 static BlockDriverAIOCB
*qemu_gluster_aio_readv(BlockDriverState
*bs
,
486 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
487 BlockDriverCompletionFunc
*cb
, void *opaque
)
489 return qemu_gluster_aio_rw(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
492 static BlockDriverAIOCB
*qemu_gluster_aio_writev(BlockDriverState
*bs
,
493 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
494 BlockDriverCompletionFunc
*cb
, void *opaque
)
496 return qemu_gluster_aio_rw(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
499 static BlockDriverAIOCB
*qemu_gluster_aio_flush(BlockDriverState
*bs
,
500 BlockDriverCompletionFunc
*cb
, void *opaque
)
504 BDRVGlusterState
*s
= bs
->opaque
;
506 acb
= qemu_aio_get(&gluster_aiocb_info
, bs
, cb
, opaque
);
509 acb
->finished
= NULL
;
511 ret
= glfs_fsync_async(s
->fd
, &gluster_finish_aiocb
, acb
);
518 qemu_aio_release(acb
);
522 #ifdef CONFIG_GLUSTERFS_DISCARD
523 static BlockDriverAIOCB
*qemu_gluster_aio_discard(BlockDriverState
*bs
,
524 int64_t sector_num
, int nb_sectors
, BlockDriverCompletionFunc
*cb
,
529 BDRVGlusterState
*s
= bs
->opaque
;
533 offset
= sector_num
* BDRV_SECTOR_SIZE
;
534 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
536 acb
= qemu_aio_get(&gluster_aiocb_info
, bs
, cb
, opaque
);
539 acb
->finished
= NULL
;
541 ret
= glfs_discard_async(s
->fd
, offset
, size
, &gluster_finish_aiocb
, acb
);
548 qemu_aio_release(acb
);
553 static int64_t qemu_gluster_getlength(BlockDriverState
*bs
)
555 BDRVGlusterState
*s
= bs
->opaque
;
558 ret
= glfs_lseek(s
->fd
, 0, SEEK_END
);
566 static int64_t qemu_gluster_allocated_file_size(BlockDriverState
*bs
)
568 BDRVGlusterState
*s
= bs
->opaque
;
572 ret
= glfs_fstat(s
->fd
, &st
);
576 return st
.st_blocks
* 512;
580 static void qemu_gluster_close(BlockDriverState
*bs
)
582 BDRVGlusterState
*s
= bs
->opaque
;
584 close(s
->fds
[GLUSTER_FD_READ
]);
585 close(s
->fds
[GLUSTER_FD_WRITE
]);
586 qemu_aio_set_fd_handler(s
->fds
[GLUSTER_FD_READ
], NULL
, NULL
, NULL
);
595 static int qemu_gluster_has_zero_init(BlockDriverState
*bs
)
597 /* GlusterFS volume could be backed by a block device */
601 static QEMUOptionParameter qemu_gluster_create_options
[] = {
603 .name
= BLOCK_OPT_SIZE
,
605 .help
= "Virtual disk size"
610 static BlockDriver bdrv_gluster
= {
611 .format_name
= "gluster",
612 .protocol_name
= "gluster",
613 .instance_size
= sizeof(BDRVGlusterState
),
614 .bdrv_needs_filename
= true,
615 .bdrv_file_open
= qemu_gluster_open
,
616 .bdrv_close
= qemu_gluster_close
,
617 .bdrv_create
= qemu_gluster_create
,
618 .bdrv_getlength
= qemu_gluster_getlength
,
619 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
620 .bdrv_truncate
= qemu_gluster_truncate
,
621 .bdrv_aio_readv
= qemu_gluster_aio_readv
,
622 .bdrv_aio_writev
= qemu_gluster_aio_writev
,
623 .bdrv_aio_flush
= qemu_gluster_aio_flush
,
624 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
625 #ifdef CONFIG_GLUSTERFS_DISCARD
626 .bdrv_aio_discard
= qemu_gluster_aio_discard
,
628 .create_options
= qemu_gluster_create_options
,
631 static BlockDriver bdrv_gluster_tcp
= {
632 .format_name
= "gluster",
633 .protocol_name
= "gluster+tcp",
634 .instance_size
= sizeof(BDRVGlusterState
),
635 .bdrv_needs_filename
= true,
636 .bdrv_file_open
= qemu_gluster_open
,
637 .bdrv_close
= qemu_gluster_close
,
638 .bdrv_create
= qemu_gluster_create
,
639 .bdrv_getlength
= qemu_gluster_getlength
,
640 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
641 .bdrv_truncate
= qemu_gluster_truncate
,
642 .bdrv_aio_readv
= qemu_gluster_aio_readv
,
643 .bdrv_aio_writev
= qemu_gluster_aio_writev
,
644 .bdrv_aio_flush
= qemu_gluster_aio_flush
,
645 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
646 #ifdef CONFIG_GLUSTERFS_DISCARD
647 .bdrv_aio_discard
= qemu_gluster_aio_discard
,
649 .create_options
= qemu_gluster_create_options
,
652 static BlockDriver bdrv_gluster_unix
= {
653 .format_name
= "gluster",
654 .protocol_name
= "gluster+unix",
655 .instance_size
= sizeof(BDRVGlusterState
),
656 .bdrv_needs_filename
= true,
657 .bdrv_file_open
= qemu_gluster_open
,
658 .bdrv_close
= qemu_gluster_close
,
659 .bdrv_create
= qemu_gluster_create
,
660 .bdrv_getlength
= qemu_gluster_getlength
,
661 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
662 .bdrv_truncate
= qemu_gluster_truncate
,
663 .bdrv_aio_readv
= qemu_gluster_aio_readv
,
664 .bdrv_aio_writev
= qemu_gluster_aio_writev
,
665 .bdrv_aio_flush
= qemu_gluster_aio_flush
,
666 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
667 #ifdef CONFIG_GLUSTERFS_DISCARD
668 .bdrv_aio_discard
= qemu_gluster_aio_discard
,
670 .create_options
= qemu_gluster_create_options
,
673 static BlockDriver bdrv_gluster_rdma
= {
674 .format_name
= "gluster",
675 .protocol_name
= "gluster+rdma",
676 .instance_size
= sizeof(BDRVGlusterState
),
677 .bdrv_needs_filename
= true,
678 .bdrv_file_open
= qemu_gluster_open
,
679 .bdrv_close
= qemu_gluster_close
,
680 .bdrv_create
= qemu_gluster_create
,
681 .bdrv_getlength
= qemu_gluster_getlength
,
682 .bdrv_get_allocated_file_size
= qemu_gluster_allocated_file_size
,
683 .bdrv_truncate
= qemu_gluster_truncate
,
684 .bdrv_aio_readv
= qemu_gluster_aio_readv
,
685 .bdrv_aio_writev
= qemu_gluster_aio_writev
,
686 .bdrv_aio_flush
= qemu_gluster_aio_flush
,
687 .bdrv_has_zero_init
= qemu_gluster_has_zero_init
,
688 #ifdef CONFIG_GLUSTERFS_DISCARD
689 .bdrv_aio_discard
= qemu_gluster_aio_discard
,
691 .create_options
= qemu_gluster_create_options
,
694 static void bdrv_gluster_init(void)
696 bdrv_register(&bdrv_gluster_rdma
);
697 bdrv_register(&bdrv_gluster_unix
);
698 bdrv_register(&bdrv_gluster_tcp
);
699 bdrv_register(&bdrv_gluster
);
702 block_init(bdrv_gluster_init
);