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
{
79 struct BDRVRBDState
*s
;
83 typedef struct RADOSCB
{
86 struct BDRVRBDState
*s
;
94 #define RBD_FD_WRITE 1
96 typedef struct BDRVRBDState
{
100 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
104 static int qemu_rbd_next_tok(char *dst
, int dst_len
,
105 char *src
, char delim
,
107 char **p
, Error
**errp
)
115 for (end
= src
; *end
; ++end
) {
119 if (*end
== '\\' && end
[1] != '\0') {
130 error_setg(errp
, "%s too long", name
);
133 error_setg(errp
, "%s too short", name
);
137 pstrcpy(dst
, dst_len
, src
);
142 static void qemu_rbd_unescape(char *src
)
146 for (p
= src
; *src
; ++src
, ++p
) {
147 if (*src
== '\\' && src
[1] != '\0') {
155 static int qemu_rbd_parsename(const char *filename
,
156 char *pool
, int pool_len
,
157 char *snap
, int snap_len
,
158 char *name
, int name_len
,
159 char *conf
, int conf_len
,
166 if (!strstart(filename
, "rbd:", &start
)) {
167 error_setg(errp
, "File name must start with 'rbd:'");
171 buf
= g_strdup(start
);
176 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
,
177 '/', "pool name", &p
, errp
);
182 qemu_rbd_unescape(pool
);
184 if (strchr(p
, '@')) {
185 ret
= qemu_rbd_next_tok(name
, name_len
, p
,
186 '@', "object name", &p
, errp
);
190 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
,
191 ':', "snap name", &p
, errp
);
192 qemu_rbd_unescape(snap
);
194 ret
= qemu_rbd_next_tok(name
, name_len
, p
,
195 ':', "object name", &p
, errp
);
197 qemu_rbd_unescape(name
);
202 ret
= qemu_rbd_next_tok(conf
, conf_len
, p
,
203 '\0', "configuration", &p
, errp
);
210 static char *qemu_rbd_parse_clientname(const char *conf
, char *clientname
)
212 const char *p
= conf
;
216 const char *end
= strchr(p
, ':');
224 if (strncmp(p
, "id=", 3) == 0) {
226 strncpy(clientname
, p
+ 3, len
);
227 clientname
[len
] = '\0';
238 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
, Error
**errp
)
241 char name
[RBD_MAX_CONF_NAME_SIZE
];
242 char value
[RBD_MAX_CONF_VAL_SIZE
];
245 buf
= g_strdup(conf
);
249 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
250 '=', "conf option name", &p
, errp
);
254 qemu_rbd_unescape(name
);
257 error_setg(errp
, "conf option %s has no value", name
);
262 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
263 ':', "conf option value", &p
, errp
);
267 qemu_rbd_unescape(value
);
269 if (strcmp(name
, "conf") == 0) {
270 ret
= rados_conf_read_file(cluster
, value
);
272 error_setg(errp
, "error reading conf file %s", value
);
275 } else if (strcmp(name
, "id") == 0) {
276 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
278 ret
= rados_conf_set(cluster
, name
, value
);
280 error_setg(errp
, "invalid conf option %s", name
);
291 static int qemu_rbd_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
293 Error
*local_err
= NULL
;
297 char pool
[RBD_MAX_POOL_NAME_SIZE
];
298 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
299 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
300 char conf
[RBD_MAX_CONF_SIZE
];
301 char clientname_buf
[RBD_MAX_CONF_SIZE
];
304 rados_ioctx_t io_ctx
;
307 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
308 snap_buf
, sizeof(snap_buf
),
310 conf
, sizeof(conf
), &local_err
) < 0) {
311 error_propagate(errp
, local_err
);
315 /* Read out options */
316 bytes
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
318 objsize
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
, 0);
320 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
321 error_setg(errp
, "obj size needs to be power of 2");
324 if (objsize
< 4096) {
325 error_setg(errp
, "obj size too small");
328 obj_order
= ctz32(objsize
);
331 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
332 if (rados_create(&cluster
, clientname
) < 0) {
333 error_setg(errp
, "error initializing");
337 if (strstr(conf
, "conf=") == NULL
) {
338 /* try default location, but ignore failure */
339 rados_conf_read_file(cluster
, NULL
);
342 if (conf
[0] != '\0' &&
343 qemu_rbd_set_conf(cluster
, conf
, &local_err
) < 0) {
344 rados_shutdown(cluster
);
345 error_propagate(errp
, local_err
);
349 if (rados_connect(cluster
) < 0) {
350 error_setg(errp
, "error connecting");
351 rados_shutdown(cluster
);
355 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
356 error_setg(errp
, "error opening pool %s", pool
);
357 rados_shutdown(cluster
);
361 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
362 rados_ioctx_destroy(io_ctx
);
363 rados_shutdown(cluster
);
369 * This aio completion is being called from rbd_finish_bh() and runs in qemu
372 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
374 RBDAIOCB
*acb
= rcb
->acb
;
379 if (acb
->cmd
!= RBD_AIO_READ
) {
383 } else if (!acb
->error
) {
384 acb
->ret
= rcb
->size
;
388 memset(rcb
->buf
, 0, rcb
->size
);
391 } else if (r
< rcb
->size
) {
392 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
394 acb
->ret
= rcb
->size
;
396 } else if (!acb
->error
) {
403 if (acb
->cmd
== RBD_AIO_READ
) {
404 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
406 qemu_vfree(acb
->bounce
);
407 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
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 error_propagate(errp
, local_err
);
449 filename
= qemu_opt_get(opts
, "filename");
451 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
452 snap_buf
, sizeof(snap_buf
),
453 s
->name
, sizeof(s
->name
),
454 conf
, sizeof(conf
), errp
) < 0) {
459 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
460 r
= rados_create(&s
->cluster
, clientname
);
462 error_setg(errp
, "error initializing");
467 if (snap_buf
[0] != '\0') {
468 s
->snap
= g_strdup(snap_buf
);
472 * Fallback to more conservative semantics if setting cache
473 * options fails. Ignore errors from setting rbd_cache because the
474 * only possible error is that the option does not exist, and
475 * librbd defaults to no caching. If write through caching cannot
476 * be set up, fall back to no caching.
478 if (flags
& BDRV_O_NOCACHE
) {
479 rados_conf_set(s
->cluster
, "rbd_cache", "false");
481 rados_conf_set(s
->cluster
, "rbd_cache", "true");
484 if (strstr(conf
, "conf=") == NULL
) {
485 /* try default location, but ignore failure */
486 rados_conf_read_file(s
->cluster
, NULL
);
489 if (conf
[0] != '\0') {
490 r
= qemu_rbd_set_conf(s
->cluster
, conf
, errp
);
492 goto failed_shutdown
;
496 r
= rados_connect(s
->cluster
);
498 error_setg(errp
, "error connecting");
499 goto failed_shutdown
;
502 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
504 error_setg(errp
, "error opening pool %s", pool
);
505 goto failed_shutdown
;
508 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
510 error_setg(errp
, "error reading header from %s", s
->name
);
514 bs
->read_only
= (s
->snap
!= NULL
);
520 rados_ioctx_destroy(s
->io_ctx
);
522 rados_shutdown(s
->cluster
);
529 static void qemu_rbd_close(BlockDriverState
*bs
)
531 BDRVRBDState
*s
= bs
->opaque
;
534 rados_ioctx_destroy(s
->io_ctx
);
536 rados_shutdown(s
->cluster
);
539 static const AIOCBInfo rbd_aiocb_info
= {
540 .aiocb_size
= sizeof(RBDAIOCB
),
543 static void rbd_finish_bh(void *opaque
)
545 RADOSCB
*rcb
= opaque
;
546 qemu_bh_delete(rcb
->acb
->bh
);
547 qemu_rbd_complete_aio(rcb
);
551 * This is the callback function for rbd_aio_read and _write
553 * Note: this function is being called from a non qemu thread so
554 * we need to be careful about what we do here. Generally we only
555 * schedule a BH, and do the rest of the io completion handling
556 * from rbd_finish_bh() which runs in a qemu context.
558 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
560 RBDAIOCB
*acb
= rcb
->acb
;
562 rcb
->ret
= rbd_aio_get_return_value(c
);
565 acb
->bh
= aio_bh_new(bdrv_get_aio_context(acb
->common
.bs
),
567 qemu_bh_schedule(acb
->bh
);
570 static int rbd_aio_discard_wrapper(rbd_image_t image
,
573 rbd_completion_t comp
)
575 #ifdef LIBRBD_SUPPORTS_DISCARD
576 return rbd_aio_discard(image
, off
, len
, comp
);
582 static int rbd_aio_flush_wrapper(rbd_image_t image
,
583 rbd_completion_t comp
)
585 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
586 return rbd_aio_flush(image
, comp
);
592 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
596 BlockCompletionFunc
*cb
,
607 BDRVRBDState
*s
= bs
->opaque
;
609 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
612 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
615 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
616 if (acb
->bounce
== NULL
) {
624 acb
->status
= -EINPROGRESS
;
626 if (cmd
== RBD_AIO_WRITE
) {
627 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
632 off
= sector_num
* BDRV_SECTOR_SIZE
;
633 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
635 rcb
= g_new(RADOSCB
, 1);
641 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
648 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
651 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
653 case RBD_AIO_DISCARD
:
654 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
657 r
= rbd_aio_flush_wrapper(s
->image
, c
);
664 goto failed_completion
;
673 qemu_vfree(acb
->bounce
);
678 static BlockAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
682 BlockCompletionFunc
*cb
,
685 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
689 static BlockAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
693 BlockCompletionFunc
*cb
,
696 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
700 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
701 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
702 BlockCompletionFunc
*cb
,
705 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
710 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
712 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
713 /* rbd_flush added in 0.1.1 */
714 BDRVRBDState
*s
= bs
->opaque
;
715 return rbd_flush(s
->image
);
722 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
724 BDRVRBDState
*s
= bs
->opaque
;
725 rbd_image_info_t info
;
728 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
733 bdi
->cluster_size
= info
.obj_size
;
737 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
739 BDRVRBDState
*s
= bs
->opaque
;
740 rbd_image_info_t info
;
743 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
751 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
753 BDRVRBDState
*s
= bs
->opaque
;
756 r
= rbd_resize(s
->image
, offset
);
764 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
765 QEMUSnapshotInfo
*sn_info
)
767 BDRVRBDState
*s
= bs
->opaque
;
770 if (sn_info
->name
[0] == '\0') {
771 return -EINVAL
; /* we need a name for rbd snapshots */
775 * rbd snapshots are using the name as the user controlled unique identifier
776 * we can't use the rbd snapid for that purpose, as it can't be set
778 if (sn_info
->id_str
[0] != '\0' &&
779 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
783 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
787 r
= rbd_snap_create(s
->image
, sn_info
->name
);
789 error_report("failed to create snap: %s", strerror(-r
));
796 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
797 const char *snapshot_id
,
798 const char *snapshot_name
,
801 BDRVRBDState
*s
= bs
->opaque
;
804 if (!snapshot_name
) {
805 error_setg(errp
, "rbd need a valid snapshot name");
809 /* If snapshot_id is specified, it must be equal to name, see
810 qemu_rbd_snap_list() */
811 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
813 "rbd do not support snapshot id, it should be NULL or "
814 "equal to snapshot name");
818 r
= rbd_snap_remove(s
->image
, snapshot_name
);
820 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
825 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
826 const char *snapshot_name
)
828 BDRVRBDState
*s
= bs
->opaque
;
831 r
= rbd_snap_rollback(s
->image
, snapshot_name
);
835 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
836 QEMUSnapshotInfo
**psn_tab
)
838 BDRVRBDState
*s
= bs
->opaque
;
839 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
841 rbd_snap_info_t
*snaps
;
842 int max_snaps
= RBD_MAX_SNAPS
;
845 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
846 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
847 if (snap_count
<= 0) {
850 } while (snap_count
== -ERANGE
);
852 if (snap_count
<= 0) {
856 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
858 for (i
= 0; i
< snap_count
; i
++) {
859 const char *snap_name
= snaps
[i
].name
;
861 sn_info
= sn_tab
+ i
;
862 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
863 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
865 sn_info
->vm_state_size
= snaps
[i
].size
;
866 sn_info
->date_sec
= 0;
867 sn_info
->date_nsec
= 0;
868 sn_info
->vm_clock_nsec
= 0;
870 rbd_snap_list_end(snaps
);
878 #ifdef LIBRBD_SUPPORTS_DISCARD
879 static BlockAIOCB
* qemu_rbd_aio_discard(BlockDriverState
*bs
,
882 BlockCompletionFunc
*cb
,
885 return rbd_start_aio(bs
, sector_num
, NULL
, nb_sectors
, cb
, opaque
,
890 #ifdef LIBRBD_SUPPORTS_INVALIDATE
891 static void qemu_rbd_invalidate_cache(BlockDriverState
*bs
,
894 BDRVRBDState
*s
= bs
->opaque
;
895 int r
= rbd_invalidate_cache(s
->image
);
897 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
902 static QemuOptsList qemu_rbd_create_opts
= {
903 .name
= "rbd-create-opts",
904 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
907 .name
= BLOCK_OPT_SIZE
,
908 .type
= QEMU_OPT_SIZE
,
909 .help
= "Virtual disk size"
912 .name
= BLOCK_OPT_CLUSTER_SIZE
,
913 .type
= QEMU_OPT_SIZE
,
914 .help
= "RBD object size"
916 { /* end of list */ }
920 static BlockDriver bdrv_rbd
= {
921 .format_name
= "rbd",
922 .instance_size
= sizeof(BDRVRBDState
),
923 .bdrv_needs_filename
= true,
924 .bdrv_file_open
= qemu_rbd_open
,
925 .bdrv_close
= qemu_rbd_close
,
926 .bdrv_create
= qemu_rbd_create
,
927 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
928 .bdrv_get_info
= qemu_rbd_getinfo
,
929 .create_opts
= &qemu_rbd_create_opts
,
930 .bdrv_getlength
= qemu_rbd_getlength
,
931 .bdrv_truncate
= qemu_rbd_truncate
,
932 .protocol_name
= "rbd",
934 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
935 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
937 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
938 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
940 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
943 #ifdef LIBRBD_SUPPORTS_DISCARD
944 .bdrv_aio_discard
= qemu_rbd_aio_discard
,
947 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
948 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
949 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
950 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
951 #ifdef LIBRBD_SUPPORTS_INVALIDATE
952 .bdrv_invalidate_cache
= qemu_rbd_invalidate_cache
,
956 static void bdrv_rbd_init(void)
958 bdrv_register(&bdrv_rbd
);
961 block_init(bdrv_rbd_init
);