2 * QEMU Block driver for RADOS (Ceph)
4 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
10 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
16 #include "qemu-common.h"
17 #include "qemu/error-report.h"
18 #include "block/block_int.h"
20 #include <rbd/librbd.h>
23 * When specifying the image filename use:
25 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
27 * poolname must be the name of an existing rados pool.
29 * devicename is the name of the rbd image.
31 * Each option given is used to configure rados, and may be any valid
32 * Ceph option, "id", or "conf".
34 * The "id" option indicates what user we should authenticate as to
35 * the Ceph cluster. If it is excluded we will use the Ceph default
38 * The "conf" option specifies a Ceph configuration file to read. If
39 * it is not specified, we will read from the default Ceph locations
40 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
41 * file, specify conf=/dev/null.
43 * Configuration values containing :, @, or = can be escaped with a
47 /* rbd_aio_discard added in 0.1.2 */
48 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
49 #define LIBRBD_SUPPORTS_DISCARD
51 #undef LIBRBD_SUPPORTS_DISCARD
54 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
56 #define RBD_MAX_CONF_NAME_SIZE 128
57 #define RBD_MAX_CONF_VAL_SIZE 512
58 #define RBD_MAX_CONF_SIZE 1024
59 #define RBD_MAX_POOL_NAME_SIZE 128
60 #define RBD_MAX_SNAP_NAME_SIZE 128
61 #define RBD_MAX_SNAPS 100
70 typedef struct RBDAIOCB
{
71 BlockDriverAIOCB common
;
79 struct BDRVRBDState
*s
;
84 typedef struct RADOSCB
{
87 struct BDRVRBDState
*s
;
95 #define RBD_FD_WRITE 1
97 typedef struct BDRVRBDState
{
101 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
105 static int qemu_rbd_next_tok(char *dst
, int dst_len
,
106 char *src
, char delim
,
116 for (end
= src
; *end
; ++end
) {
120 if (*end
== '\\' && end
[1] != '\0') {
131 error_report("%s too long", name
);
134 error_report("%s too short", name
);
138 pstrcpy(dst
, dst_len
, src
);
143 static void qemu_rbd_unescape(char *src
)
147 for (p
= src
; *src
; ++src
, ++p
) {
148 if (*src
== '\\' && src
[1] != '\0') {
156 static int qemu_rbd_parsename(const char *filename
,
157 char *pool
, int pool_len
,
158 char *snap
, int snap_len
,
159 char *name
, int name_len
,
160 char *conf
, int conf_len
)
166 if (!strstart(filename
, "rbd:", &start
)) {
170 buf
= g_strdup(start
);
175 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
, '/', "pool name", &p
);
180 qemu_rbd_unescape(pool
);
182 if (strchr(p
, '@')) {
183 ret
= qemu_rbd_next_tok(name
, name_len
, p
, '@', "object name", &p
);
187 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
, ':', "snap name", &p
);
188 qemu_rbd_unescape(snap
);
190 ret
= qemu_rbd_next_tok(name
, name_len
, p
, ':', "object name", &p
);
192 qemu_rbd_unescape(name
);
197 ret
= qemu_rbd_next_tok(conf
, conf_len
, p
, '\0', "configuration", &p
);
204 static char *qemu_rbd_parse_clientname(const char *conf
, char *clientname
)
206 const char *p
= conf
;
210 const char *end
= strchr(p
, ':');
218 if (strncmp(p
, "id=", 3) == 0) {
220 strncpy(clientname
, p
+ 3, len
);
221 clientname
[len
] = '\0';
232 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
)
235 char name
[RBD_MAX_CONF_NAME_SIZE
];
236 char value
[RBD_MAX_CONF_VAL_SIZE
];
239 buf
= g_strdup(conf
);
243 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
244 '=', "conf option name", &p
);
248 qemu_rbd_unescape(name
);
251 error_report("conf option %s has no value", name
);
256 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
257 ':', "conf option value", &p
);
261 qemu_rbd_unescape(value
);
263 if (strcmp(name
, "conf") == 0) {
264 ret
= rados_conf_read_file(cluster
, value
);
266 error_report("error reading conf file %s", value
);
269 } else if (strcmp(name
, "id") == 0) {
270 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
272 ret
= rados_conf_set(cluster
, name
, value
);
274 error_report("invalid conf option %s", name
);
285 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
,
291 char pool
[RBD_MAX_POOL_NAME_SIZE
];
292 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
293 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
294 char conf
[RBD_MAX_CONF_SIZE
];
295 char clientname_buf
[RBD_MAX_CONF_SIZE
];
298 rados_ioctx_t io_ctx
;
301 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
302 snap_buf
, sizeof(snap_buf
),
304 conf
, sizeof(conf
)) < 0) {
308 /* Read out options */
309 while (options
&& options
->name
) {
310 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
311 bytes
= options
->value
.n
;
312 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
313 if (options
->value
.n
) {
314 objsize
= options
->value
.n
;
315 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
316 error_report("obj size needs to be power of 2");
319 if (objsize
< 4096) {
320 error_report("obj size too small");
323 obj_order
= ffs(objsize
) - 1;
329 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
330 if (rados_create(&cluster
, clientname
) < 0) {
331 error_report("error initializing");
335 if (strstr(conf
, "conf=") == NULL
) {
336 /* try default location, but ignore failure */
337 rados_conf_read_file(cluster
, NULL
);
340 if (conf
[0] != '\0' &&
341 qemu_rbd_set_conf(cluster
, conf
) < 0) {
342 error_report("error setting config options");
343 rados_shutdown(cluster
);
347 if (rados_connect(cluster
) < 0) {
348 error_report("error connecting");
349 rados_shutdown(cluster
);
353 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
354 error_report("error opening pool %s", pool
);
355 rados_shutdown(cluster
);
359 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
360 rados_ioctx_destroy(io_ctx
);
361 rados_shutdown(cluster
);
367 * This aio completion is being called from rbd_finish_bh() and runs in qemu
370 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
372 RBDAIOCB
*acb
= rcb
->acb
;
377 if (acb
->cmd
!= RBD_AIO_READ
) {
381 } else if (!acb
->error
) {
382 acb
->ret
= rcb
->size
;
386 memset(rcb
->buf
, 0, rcb
->size
);
389 } else if (r
< rcb
->size
) {
390 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
392 acb
->ret
= rcb
->size
;
394 } else if (!acb
->error
) {
401 if (acb
->cmd
== RBD_AIO_READ
) {
402 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
404 qemu_vfree(acb
->bounce
);
405 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
408 if (!acb
->cancelled
) {
409 qemu_aio_release(acb
);
413 /* TODO Convert to fine grained options */
414 static QemuOptsList runtime_opts
= {
416 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
420 .type
= QEMU_OPT_STRING
,
421 .help
= "Specification of the rbd image",
423 { /* end of list */ }
427 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
430 BDRVRBDState
*s
= bs
->opaque
;
431 char pool
[RBD_MAX_POOL_NAME_SIZE
];
432 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
433 char conf
[RBD_MAX_CONF_SIZE
];
434 char clientname_buf
[RBD_MAX_CONF_SIZE
];
437 Error
*local_err
= NULL
;
438 const char *filename
;
441 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
442 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
444 qerror_report_err(local_err
);
445 error_free(local_err
);
450 filename
= qemu_opt_get(opts
, "filename");
452 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
453 snap_buf
, sizeof(snap_buf
),
454 s
->name
, sizeof(s
->name
),
455 conf
, sizeof(conf
)) < 0) {
460 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
461 r
= rados_create(&s
->cluster
, clientname
);
463 error_report("error initializing");
468 if (snap_buf
[0] != '\0') {
469 s
->snap
= g_strdup(snap_buf
);
473 * Fallback to more conservative semantics if setting cache
474 * options fails. Ignore errors from setting rbd_cache because the
475 * only possible error is that the option does not exist, and
476 * librbd defaults to no caching. If write through caching cannot
477 * be set up, fall back to no caching.
479 if (flags
& BDRV_O_NOCACHE
) {
480 rados_conf_set(s
->cluster
, "rbd_cache", "false");
482 rados_conf_set(s
->cluster
, "rbd_cache", "true");
485 if (strstr(conf
, "conf=") == NULL
) {
486 /* try default location, but ignore failure */
487 rados_conf_read_file(s
->cluster
, NULL
);
490 if (conf
[0] != '\0') {
491 r
= qemu_rbd_set_conf(s
->cluster
, conf
);
493 error_report("error setting config options");
494 goto failed_shutdown
;
498 r
= rados_connect(s
->cluster
);
500 error_report("error connecting");
501 goto failed_shutdown
;
504 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
506 error_report("error opening pool %s", pool
);
507 goto failed_shutdown
;
510 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
512 error_report("error reading header from %s", s
->name
);
516 bs
->read_only
= (s
->snap
!= NULL
);
522 rados_ioctx_destroy(s
->io_ctx
);
524 rados_shutdown(s
->cluster
);
531 static void qemu_rbd_close(BlockDriverState
*bs
)
533 BDRVRBDState
*s
= bs
->opaque
;
536 rados_ioctx_destroy(s
->io_ctx
);
538 rados_shutdown(s
->cluster
);
542 * Cancel aio. Since we don't reference acb in a non qemu threads,
543 * it is safe to access it here.
545 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
547 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
550 while (acb
->status
== -EINPROGRESS
) {
554 qemu_aio_release(acb
);
557 static const AIOCBInfo rbd_aiocb_info
= {
558 .aiocb_size
= sizeof(RBDAIOCB
),
559 .cancel
= qemu_rbd_aio_cancel
,
562 static void rbd_finish_bh(void *opaque
)
564 RADOSCB
*rcb
= opaque
;
565 qemu_bh_delete(rcb
->acb
->bh
);
566 qemu_rbd_complete_aio(rcb
);
570 * This is the callback function for rbd_aio_read and _write
572 * Note: this function is being called from a non qemu thread so
573 * we need to be careful about what we do here. Generally we only
574 * schedule a BH, and do the rest of the io completion handling
575 * from rbd_finish_bh() which runs in a qemu context.
577 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
579 RBDAIOCB
*acb
= rcb
->acb
;
581 rcb
->ret
= rbd_aio_get_return_value(c
);
584 acb
->bh
= qemu_bh_new(rbd_finish_bh
, rcb
);
585 qemu_bh_schedule(acb
->bh
);
588 static int rbd_aio_discard_wrapper(rbd_image_t image
,
591 rbd_completion_t comp
)
593 #ifdef LIBRBD_SUPPORTS_DISCARD
594 return rbd_aio_discard(image
, off
, len
, comp
);
600 static int rbd_aio_flush_wrapper(rbd_image_t image
,
601 rbd_completion_t comp
)
603 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
604 return rbd_aio_flush(image
, comp
);
610 static BlockDriverAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
614 BlockDriverCompletionFunc
*cb
,
625 BDRVRBDState
*s
= bs
->opaque
;
627 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
630 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
633 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
640 acb
->status
= -EINPROGRESS
;
642 if (cmd
== RBD_AIO_WRITE
) {
643 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
648 off
= sector_num
* BDRV_SECTOR_SIZE
;
649 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
651 rcb
= g_malloc(sizeof(RADOSCB
));
657 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
664 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
667 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
669 case RBD_AIO_DISCARD
:
670 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
673 r
= rbd_aio_flush_wrapper(s
->image
, c
);
687 qemu_aio_release(acb
);
691 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
695 BlockDriverCompletionFunc
*cb
,
698 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
702 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
706 BlockDriverCompletionFunc
*cb
,
709 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
713 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
714 static BlockDriverAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
715 BlockDriverCompletionFunc
*cb
,
718 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
723 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
725 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
726 /* rbd_flush added in 0.1.1 */
727 BDRVRBDState
*s
= bs
->opaque
;
728 return rbd_flush(s
->image
);
735 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
737 BDRVRBDState
*s
= bs
->opaque
;
738 rbd_image_info_t info
;
741 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
746 bdi
->cluster_size
= info
.obj_size
;
750 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
752 BDRVRBDState
*s
= bs
->opaque
;
753 rbd_image_info_t info
;
756 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
764 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
766 BDRVRBDState
*s
= bs
->opaque
;
769 r
= rbd_resize(s
->image
, offset
);
777 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
778 QEMUSnapshotInfo
*sn_info
)
780 BDRVRBDState
*s
= bs
->opaque
;
783 if (sn_info
->name
[0] == '\0') {
784 return -EINVAL
; /* we need a name for rbd snapshots */
788 * rbd snapshots are using the name as the user controlled unique identifier
789 * we can't use the rbd snapid for that purpose, as it can't be set
791 if (sn_info
->id_str
[0] != '\0' &&
792 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
796 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
800 r
= rbd_snap_create(s
->image
, sn_info
->name
);
802 error_report("failed to create snap: %s", strerror(-r
));
809 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
810 const char *snapshot_id
,
811 const char *snapshot_name
,
814 BDRVRBDState
*s
= bs
->opaque
;
817 if (!snapshot_name
) {
818 error_setg(errp
, "rbd need a valid snapshot name");
822 /* If snapshot_id is specified, it must be equal to name, see
823 qemu_rbd_snap_list() */
824 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
826 "rbd do not support snapshot id, it should be NULL or "
827 "equal to snapshot name");
831 r
= rbd_snap_remove(s
->image
, snapshot_name
);
833 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
838 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
839 const char *snapshot_name
)
841 BDRVRBDState
*s
= bs
->opaque
;
844 r
= rbd_snap_rollback(s
->image
, snapshot_name
);
848 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
849 QEMUSnapshotInfo
**psn_tab
)
851 BDRVRBDState
*s
= bs
->opaque
;
852 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
854 rbd_snap_info_t
*snaps
;
855 int max_snaps
= RBD_MAX_SNAPS
;
858 snaps
= g_malloc(sizeof(*snaps
) * max_snaps
);
859 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
860 if (snap_count
<= 0) {
863 } while (snap_count
== -ERANGE
);
865 if (snap_count
<= 0) {
869 sn_tab
= g_malloc0(snap_count
* sizeof(QEMUSnapshotInfo
));
871 for (i
= 0; i
< snap_count
; i
++) {
872 const char *snap_name
= snaps
[i
].name
;
874 sn_info
= sn_tab
+ i
;
875 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
876 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
878 sn_info
->vm_state_size
= snaps
[i
].size
;
879 sn_info
->date_sec
= 0;
880 sn_info
->date_nsec
= 0;
881 sn_info
->vm_clock_nsec
= 0;
883 rbd_snap_list_end(snaps
);
891 #ifdef LIBRBD_SUPPORTS_DISCARD
892 static BlockDriverAIOCB
* qemu_rbd_aio_discard(BlockDriverState
*bs
,
895 BlockDriverCompletionFunc
*cb
,
898 return rbd_start_aio(bs
, sector_num
, NULL
, nb_sectors
, cb
, opaque
,
903 static QEMUOptionParameter qemu_rbd_create_options
[] = {
905 .name
= BLOCK_OPT_SIZE
,
907 .help
= "Virtual disk size"
910 .name
= BLOCK_OPT_CLUSTER_SIZE
,
912 .help
= "RBD object size"
917 static BlockDriver bdrv_rbd
= {
918 .format_name
= "rbd",
919 .instance_size
= sizeof(BDRVRBDState
),
920 .bdrv_needs_filename
= true,
921 .bdrv_file_open
= qemu_rbd_open
,
922 .bdrv_close
= qemu_rbd_close
,
923 .bdrv_create
= qemu_rbd_create
,
924 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
925 .bdrv_get_info
= qemu_rbd_getinfo
,
926 .create_options
= qemu_rbd_create_options
,
927 .bdrv_getlength
= qemu_rbd_getlength
,
928 .bdrv_truncate
= qemu_rbd_truncate
,
929 .protocol_name
= "rbd",
931 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
932 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
934 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
935 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
937 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
940 #ifdef LIBRBD_SUPPORTS_DISCARD
941 .bdrv_aio_discard
= qemu_rbd_aio_discard
,
944 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
945 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
946 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
947 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
950 static void bdrv_rbd_init(void)
952 bdrv_register(&bdrv_rbd
);
955 block_init(bdrv_rbd_init
);