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
,
108 char **p
, Error
**errp
)
116 for (end
= src
; *end
; ++end
) {
120 if (*end
== '\\' && end
[1] != '\0') {
131 error_setg(errp
, "%s too long", name
);
134 error_setg(errp
, "%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
,
167 if (!strstart(filename
, "rbd:", &start
)) {
168 error_setg(errp
, "File name must start with 'rbd:'");
172 buf
= g_strdup(start
);
177 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
,
178 '/', "pool name", &p
, errp
);
183 qemu_rbd_unescape(pool
);
185 if (strchr(p
, '@')) {
186 ret
= qemu_rbd_next_tok(name
, name_len
, p
,
187 '@', "object name", &p
, errp
);
191 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
,
192 ':', "snap name", &p
, errp
);
193 qemu_rbd_unescape(snap
);
195 ret
= qemu_rbd_next_tok(name
, name_len
, p
,
196 ':', "object name", &p
, errp
);
198 qemu_rbd_unescape(name
);
203 ret
= qemu_rbd_next_tok(conf
, conf_len
, p
,
204 '\0', "configuration", &p
, errp
);
211 static char *qemu_rbd_parse_clientname(const char *conf
, char *clientname
)
213 const char *p
= conf
;
217 const char *end
= strchr(p
, ':');
225 if (strncmp(p
, "id=", 3) == 0) {
227 strncpy(clientname
, p
+ 3, len
);
228 clientname
[len
] = '\0';
239 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
, Error
**errp
)
242 char name
[RBD_MAX_CONF_NAME_SIZE
];
243 char value
[RBD_MAX_CONF_VAL_SIZE
];
246 buf
= g_strdup(conf
);
250 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
251 '=', "conf option name", &p
, errp
);
255 qemu_rbd_unescape(name
);
258 error_setg(errp
, "conf option %s has no value", name
);
263 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
264 ':', "conf option value", &p
, errp
);
268 qemu_rbd_unescape(value
);
270 if (strcmp(name
, "conf") == 0) {
271 ret
= rados_conf_read_file(cluster
, value
);
273 error_setg(errp
, "error reading conf file %s", value
);
276 } else if (strcmp(name
, "id") == 0) {
277 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
279 ret
= rados_conf_set(cluster
, name
, value
);
281 error_setg(errp
, "invalid conf option %s", name
);
292 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
,
295 Error
*local_err
= NULL
;
299 char pool
[RBD_MAX_POOL_NAME_SIZE
];
300 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
301 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
302 char conf
[RBD_MAX_CONF_SIZE
];
303 char clientname_buf
[RBD_MAX_CONF_SIZE
];
306 rados_ioctx_t io_ctx
;
309 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
310 snap_buf
, sizeof(snap_buf
),
312 conf
, sizeof(conf
), &local_err
) < 0) {
313 error_propagate(errp
, local_err
);
317 /* Read out options */
318 while (options
&& options
->name
) {
319 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
320 bytes
= options
->value
.n
;
321 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
322 if (options
->value
.n
) {
323 objsize
= options
->value
.n
;
324 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
325 error_setg(errp
, "obj size needs to be power of 2");
328 if (objsize
< 4096) {
329 error_setg(errp
, "obj size too small");
332 obj_order
= ffs(objsize
) - 1;
338 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
339 if (rados_create(&cluster
, clientname
) < 0) {
340 error_setg(errp
, "error initializing");
344 if (strstr(conf
, "conf=") == NULL
) {
345 /* try default location, but ignore failure */
346 rados_conf_read_file(cluster
, NULL
);
349 if (conf
[0] != '\0' &&
350 qemu_rbd_set_conf(cluster
, conf
, &local_err
) < 0) {
351 rados_shutdown(cluster
);
352 error_propagate(errp
, local_err
);
356 if (rados_connect(cluster
) < 0) {
357 error_setg(errp
, "error connecting");
358 rados_shutdown(cluster
);
362 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
363 error_setg(errp
, "error opening pool %s", pool
);
364 rados_shutdown(cluster
);
368 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
369 rados_ioctx_destroy(io_ctx
);
370 rados_shutdown(cluster
);
376 * This aio completion is being called from rbd_finish_bh() and runs in qemu
379 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
381 RBDAIOCB
*acb
= rcb
->acb
;
386 if (acb
->cmd
!= RBD_AIO_READ
) {
390 } else if (!acb
->error
) {
391 acb
->ret
= rcb
->size
;
395 memset(rcb
->buf
, 0, rcb
->size
);
398 } else if (r
< rcb
->size
) {
399 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
401 acb
->ret
= rcb
->size
;
403 } else if (!acb
->error
) {
410 if (acb
->cmd
== RBD_AIO_READ
) {
411 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
413 qemu_vfree(acb
->bounce
);
414 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
417 if (!acb
->cancelled
) {
418 qemu_aio_release(acb
);
422 /* TODO Convert to fine grained options */
423 static QemuOptsList runtime_opts
= {
425 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
429 .type
= QEMU_OPT_STRING
,
430 .help
= "Specification of the rbd image",
432 { /* end of list */ }
436 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
439 BDRVRBDState
*s
= bs
->opaque
;
440 char pool
[RBD_MAX_POOL_NAME_SIZE
];
441 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
442 char conf
[RBD_MAX_CONF_SIZE
];
443 char clientname_buf
[RBD_MAX_CONF_SIZE
];
446 Error
*local_err
= NULL
;
447 const char *filename
;
450 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
451 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
453 error_propagate(errp
, local_err
);
458 filename
= qemu_opt_get(opts
, "filename");
460 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
461 snap_buf
, sizeof(snap_buf
),
462 s
->name
, sizeof(s
->name
),
463 conf
, sizeof(conf
), errp
) < 0) {
468 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
469 r
= rados_create(&s
->cluster
, clientname
);
471 error_setg(&local_err
, "error initializing");
476 if (snap_buf
[0] != '\0') {
477 s
->snap
= g_strdup(snap_buf
);
481 * Fallback to more conservative semantics if setting cache
482 * options fails. Ignore errors from setting rbd_cache because the
483 * only possible error is that the option does not exist, and
484 * librbd defaults to no caching. If write through caching cannot
485 * be set up, fall back to no caching.
487 if (flags
& BDRV_O_NOCACHE
) {
488 rados_conf_set(s
->cluster
, "rbd_cache", "false");
490 rados_conf_set(s
->cluster
, "rbd_cache", "true");
493 if (strstr(conf
, "conf=") == NULL
) {
494 /* try default location, but ignore failure */
495 rados_conf_read_file(s
->cluster
, NULL
);
498 if (conf
[0] != '\0') {
499 r
= qemu_rbd_set_conf(s
->cluster
, conf
, errp
);
501 goto failed_shutdown
;
505 r
= rados_connect(s
->cluster
);
507 error_setg(&local_err
, "error connecting");
508 goto failed_shutdown
;
511 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
513 error_setg(&local_err
, "error opening pool %s", pool
);
514 goto failed_shutdown
;
517 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
519 error_setg(&local_err
, "error reading header from %s", s
->name
);
523 bs
->read_only
= (s
->snap
!= NULL
);
529 rados_ioctx_destroy(s
->io_ctx
);
531 rados_shutdown(s
->cluster
);
538 static void qemu_rbd_close(BlockDriverState
*bs
)
540 BDRVRBDState
*s
= bs
->opaque
;
543 rados_ioctx_destroy(s
->io_ctx
);
545 rados_shutdown(s
->cluster
);
549 * Cancel aio. Since we don't reference acb in a non qemu threads,
550 * it is safe to access it here.
552 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
554 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
557 while (acb
->status
== -EINPROGRESS
) {
561 qemu_aio_release(acb
);
564 static const AIOCBInfo rbd_aiocb_info
= {
565 .aiocb_size
= sizeof(RBDAIOCB
),
566 .cancel
= qemu_rbd_aio_cancel
,
569 static void rbd_finish_bh(void *opaque
)
571 RADOSCB
*rcb
= opaque
;
572 qemu_bh_delete(rcb
->acb
->bh
);
573 qemu_rbd_complete_aio(rcb
);
577 * This is the callback function for rbd_aio_read and _write
579 * Note: this function is being called from a non qemu thread so
580 * we need to be careful about what we do here. Generally we only
581 * schedule a BH, and do the rest of the io completion handling
582 * from rbd_finish_bh() which runs in a qemu context.
584 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
586 RBDAIOCB
*acb
= rcb
->acb
;
588 rcb
->ret
= rbd_aio_get_return_value(c
);
591 acb
->bh
= qemu_bh_new(rbd_finish_bh
, rcb
);
592 qemu_bh_schedule(acb
->bh
);
595 static int rbd_aio_discard_wrapper(rbd_image_t image
,
598 rbd_completion_t comp
)
600 #ifdef LIBRBD_SUPPORTS_DISCARD
601 return rbd_aio_discard(image
, off
, len
, comp
);
607 static int rbd_aio_flush_wrapper(rbd_image_t image
,
608 rbd_completion_t comp
)
610 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
611 return rbd_aio_flush(image
, comp
);
617 static BlockDriverAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
621 BlockDriverCompletionFunc
*cb
,
632 BDRVRBDState
*s
= bs
->opaque
;
634 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
637 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
640 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
647 acb
->status
= -EINPROGRESS
;
649 if (cmd
== RBD_AIO_WRITE
) {
650 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
655 off
= sector_num
* BDRV_SECTOR_SIZE
;
656 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
658 rcb
= g_malloc(sizeof(RADOSCB
));
664 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
671 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
674 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
676 case RBD_AIO_DISCARD
:
677 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
680 r
= rbd_aio_flush_wrapper(s
->image
, c
);
694 qemu_aio_release(acb
);
698 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
702 BlockDriverCompletionFunc
*cb
,
705 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
709 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
713 BlockDriverCompletionFunc
*cb
,
716 return rbd_start_aio(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
,
720 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
721 static BlockDriverAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
722 BlockDriverCompletionFunc
*cb
,
725 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
730 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
732 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
733 /* rbd_flush added in 0.1.1 */
734 BDRVRBDState
*s
= bs
->opaque
;
735 return rbd_flush(s
->image
);
742 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
744 BDRVRBDState
*s
= bs
->opaque
;
745 rbd_image_info_t info
;
748 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
753 bdi
->cluster_size
= info
.obj_size
;
757 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
759 BDRVRBDState
*s
= bs
->opaque
;
760 rbd_image_info_t info
;
763 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
771 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
773 BDRVRBDState
*s
= bs
->opaque
;
776 r
= rbd_resize(s
->image
, offset
);
784 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
785 QEMUSnapshotInfo
*sn_info
)
787 BDRVRBDState
*s
= bs
->opaque
;
790 if (sn_info
->name
[0] == '\0') {
791 return -EINVAL
; /* we need a name for rbd snapshots */
795 * rbd snapshots are using the name as the user controlled unique identifier
796 * we can't use the rbd snapid for that purpose, as it can't be set
798 if (sn_info
->id_str
[0] != '\0' &&
799 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
803 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
807 r
= rbd_snap_create(s
->image
, sn_info
->name
);
809 error_report("failed to create snap: %s", strerror(-r
));
816 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
817 const char *snapshot_id
,
818 const char *snapshot_name
,
821 BDRVRBDState
*s
= bs
->opaque
;
824 if (!snapshot_name
) {
825 error_setg(errp
, "rbd need a valid snapshot name");
829 /* If snapshot_id is specified, it must be equal to name, see
830 qemu_rbd_snap_list() */
831 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
833 "rbd do not support snapshot id, it should be NULL or "
834 "equal to snapshot name");
838 r
= rbd_snap_remove(s
->image
, snapshot_name
);
840 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
845 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
846 const char *snapshot_name
)
848 BDRVRBDState
*s
= bs
->opaque
;
851 r
= rbd_snap_rollback(s
->image
, snapshot_name
);
855 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
856 QEMUSnapshotInfo
**psn_tab
)
858 BDRVRBDState
*s
= bs
->opaque
;
859 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
861 rbd_snap_info_t
*snaps
;
862 int max_snaps
= RBD_MAX_SNAPS
;
865 snaps
= g_malloc(sizeof(*snaps
) * max_snaps
);
866 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
867 if (snap_count
<= 0) {
870 } while (snap_count
== -ERANGE
);
872 if (snap_count
<= 0) {
876 sn_tab
= g_malloc0(snap_count
* sizeof(QEMUSnapshotInfo
));
878 for (i
= 0; i
< snap_count
; i
++) {
879 const char *snap_name
= snaps
[i
].name
;
881 sn_info
= sn_tab
+ i
;
882 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
883 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
885 sn_info
->vm_state_size
= snaps
[i
].size
;
886 sn_info
->date_sec
= 0;
887 sn_info
->date_nsec
= 0;
888 sn_info
->vm_clock_nsec
= 0;
890 rbd_snap_list_end(snaps
);
898 #ifdef LIBRBD_SUPPORTS_DISCARD
899 static BlockDriverAIOCB
* qemu_rbd_aio_discard(BlockDriverState
*bs
,
902 BlockDriverCompletionFunc
*cb
,
905 return rbd_start_aio(bs
, sector_num
, NULL
, nb_sectors
, cb
, opaque
,
910 static QEMUOptionParameter qemu_rbd_create_options
[] = {
912 .name
= BLOCK_OPT_SIZE
,
914 .help
= "Virtual disk size"
917 .name
= BLOCK_OPT_CLUSTER_SIZE
,
919 .help
= "RBD object size"
924 static BlockDriver bdrv_rbd
= {
925 .format_name
= "rbd",
926 .instance_size
= sizeof(BDRVRBDState
),
927 .bdrv_needs_filename
= true,
928 .bdrv_file_open
= qemu_rbd_open
,
929 .bdrv_close
= qemu_rbd_close
,
930 .bdrv_create
= qemu_rbd_create
,
931 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
932 .bdrv_get_info
= qemu_rbd_getinfo
,
933 .create_options
= qemu_rbd_create_options
,
934 .bdrv_getlength
= qemu_rbd_getlength
,
935 .bdrv_truncate
= qemu_rbd_truncate
,
936 .protocol_name
= "rbd",
938 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
939 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
941 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
942 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
944 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
947 #ifdef LIBRBD_SUPPORTS_DISCARD
948 .bdrv_aio_discard
= qemu_rbd_aio_discard
,
951 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
952 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
953 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
954 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
957 static void bdrv_rbd_init(void)
959 bdrv_register(&bdrv_rbd
);
962 block_init(bdrv_rbd_init
);