2 * QEMU Block driver for RADOS (Ceph)
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.h"
18 #include "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 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
49 #define RBD_MAX_CONF_NAME_SIZE 128
50 #define RBD_MAX_CONF_VAL_SIZE 512
51 #define RBD_MAX_CONF_SIZE 1024
52 #define RBD_MAX_POOL_NAME_SIZE 128
53 #define RBD_MAX_SNAP_NAME_SIZE 128
54 #define RBD_MAX_SNAPS 100
56 typedef struct RBDAIOCB {
57 BlockDriverAIOCB common;
65 struct BDRVRBDState *s;
69 typedef struct RADOSCB {
72 struct BDRVRBDState *s;
80 #define RBD_FD_WRITE 1
82 typedef struct BDRVRBDState {
87 char name[RBD_MAX_IMAGE_NAME_SIZE];
94 static void rbd_aio_bh_cb(void *opaque);
96 static int qemu_rbd_next_tok(char *dst, int dst_len,
97 char *src, char delim,
107 for (end = src; *end; ++end) {
111 if (*end == '\\' && end[1] != '\0') {
122 error_report("%s too long", name);
125 error_report("%s too short", name);
129 pstrcpy(dst, dst_len, src);
134 static void qemu_rbd_unescape(char *src)
138 for (p = src; *src; ++src, ++p) {
139 if (*src == '\\' && src[1] != '\0') {
147 static int qemu_rbd_parsename(const char *filename,
148 char *pool, int pool_len,
149 char *snap, int snap_len,
150 char *name, int name_len,
151 char *conf, int conf_len)
157 if (!strstart(filename, "rbd:", &start)) {
161 buf = g_strdup(start);
166 ret = qemu_rbd_next_tok(pool, pool_len, p, '/', "pool name", &p);
171 qemu_rbd_unescape(pool);
173 if (strchr(p, '@')) {
174 ret = qemu_rbd_next_tok(name, name_len, p, '@', "object name", &p);
178 ret = qemu_rbd_next_tok(snap, snap_len, p, ':', "snap name", &p);
179 qemu_rbd_unescape(snap);
181 ret = qemu_rbd_next_tok(name, name_len, p, ':', "object name", &p);
183 qemu_rbd_unescape(name);
188 ret = qemu_rbd_next_tok(conf, conf_len, p, '\0', "configuration", &p);
195 static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
197 const char *p = conf;
201 const char *end = strchr(p, ':');
209 if (strncmp(p, "id=", 3) == 0) {
211 strncpy(clientname, p + 3, len);
212 clientname[len] = '\0';
223 static int qemu_rbd_set_conf(rados_t cluster, const char *conf)
226 char name[RBD_MAX_CONF_NAME_SIZE];
227 char value[RBD_MAX_CONF_VAL_SIZE];
230 buf = g_strdup(conf);
234 ret = qemu_rbd_next_tok(name, sizeof(name), p,
235 '=', "conf option name", &p);
239 qemu_rbd_unescape(name);
242 error_report("conf option %s has no value", name);
247 ret = qemu_rbd_next_tok(value, sizeof(value), p,
248 ':', "conf option value", &p);
252 qemu_rbd_unescape(value);
254 if (strcmp(name, "conf") == 0) {
255 ret = rados_conf_read_file(cluster, value);
257 error_report("error reading conf file %s", value);
260 } else if (strcmp(name, "id") == 0) {
261 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
263 ret = rados_conf_set(cluster, name, value);
265 error_report("invalid conf option %s", name);
276 static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options)
281 char pool[RBD_MAX_POOL_NAME_SIZE];
282 char name[RBD_MAX_IMAGE_NAME_SIZE];
283 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
284 char conf[RBD_MAX_CONF_SIZE];
285 char clientname_buf[RBD_MAX_CONF_SIZE];
288 rados_ioctx_t io_ctx;
291 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
292 snap_buf, sizeof(snap_buf),
294 conf, sizeof(conf)) < 0) {
298 /* Read out options */
299 while (options && options->name) {
300 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
301 bytes = options->value.n;
302 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
303 if (options->value.n) {
304 objsize = options->value.n;
305 if ((objsize - 1) & objsize) { /* not a power of 2? */
306 error_report("obj size needs to be power of 2");
309 if (objsize < 4096) {
310 error_report("obj size too small");
313 obj_order = ffs(objsize) - 1;
319 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
320 if (rados_create(&cluster, clientname) < 0) {
321 error_report("error initializing");
325 if (strstr(conf, "conf=") == NULL) {
326 /* try default location, but ignore failure */
327 rados_conf_read_file(cluster, NULL);
330 if (conf[0] != '\0' &&
331 qemu_rbd_set_conf(cluster, conf) < 0) {
332 error_report("error setting config options");
333 rados_shutdown(cluster);
337 if (rados_connect(cluster) < 0) {
338 error_report("error connecting");
339 rados_shutdown(cluster);
343 if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
344 error_report("error opening pool %s", pool);
345 rados_shutdown(cluster);
349 ret = rbd_create(io_ctx, name, bytes, &obj_order);
350 rados_ioctx_destroy(io_ctx);
351 rados_shutdown(cluster);
357 * This aio completion is being called from qemu_rbd_aio_event_reader()
358 * and runs in qemu context. It schedules a bh, but just in case the aio
359 * was not cancelled before.
361 static void qemu_rbd_complete_aio(RADOSCB *rcb)
363 RBDAIOCB *acb = rcb->acb;
366 if (acb->cancelled) {
367 qemu_vfree(acb->bounce);
368 qemu_aio_release(acb);
378 } else if (!acb->error) {
379 acb->ret = rcb->size;
383 memset(rcb->buf, 0, rcb->size);
386 } else if (r < rcb->size) {
387 memset(rcb->buf + r, 0, rcb->size - r);
389 acb->ret = rcb->size;
391 } else if (!acb->error) {
395 /* Note that acb->bh can be NULL in case where the aio was cancelled */
396 acb->bh = qemu_bh_new(rbd_aio_bh_cb, acb);
397 qemu_bh_schedule(acb->bh);
403 * aio fd read handler. It runs in the qemu context and calls the
404 * completion handling of completed rados aio operations.
406 static void qemu_rbd_aio_event_reader(void *opaque)
408 BDRVRBDState *s = opaque;
413 char *p = (char *)&s->event_rcb;
415 /* now read the rcb pointer that was sent from a non qemu thread */
416 ret = read(s->fds[RBD_FD_READ], p + s->event_reader_pos,
417 sizeof(s->event_rcb) - s->event_reader_pos);
419 s->event_reader_pos += ret;
420 if (s->event_reader_pos == sizeof(s->event_rcb)) {
421 s->event_reader_pos = 0;
422 qemu_rbd_complete_aio(s->event_rcb);
426 } while (ret < 0 && errno == EINTR);
429 static int qemu_rbd_aio_flush_cb(void *opaque)
431 BDRVRBDState *s = opaque;
433 return (s->qemu_aio_count > 0);
436 static int qemu_rbd_open(BlockDriverState *bs, const char *filename, int flags)
438 BDRVRBDState *s = bs->opaque;
439 char pool[RBD_MAX_POOL_NAME_SIZE];
440 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
441 char conf[RBD_MAX_CONF_SIZE];
442 char clientname_buf[RBD_MAX_CONF_SIZE];
446 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
447 snap_buf, sizeof(snap_buf),
448 s->name, sizeof(s->name),
449 conf, sizeof(conf)) < 0) {
453 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
454 r = rados_create(&s->cluster, clientname);
456 error_report("error initializing");
461 if (snap_buf[0] != '\0') {
462 s->snap = g_strdup(snap_buf);
465 if (strstr(conf, "conf=") == NULL) {
466 /* try default location, but ignore failure */
467 rados_conf_read_file(s->cluster, NULL);
470 if (conf[0] != '\0') {
471 r = qemu_rbd_set_conf(s->cluster, conf);
473 error_report("error setting config options");
474 goto failed_shutdown;
478 r = rados_connect(s->cluster);
480 error_report("error connecting");
481 goto failed_shutdown;
484 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
486 error_report("error opening pool %s", pool);
487 goto failed_shutdown;
490 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
492 error_report("error reading header from %s", s->name);
496 bs->read_only = (s->snap != NULL);
498 s->event_reader_pos = 0;
499 r = qemu_pipe(s->fds);
501 error_report("error opening eventfd");
504 fcntl(s->fds[0], F_SETFL, O_NONBLOCK);
505 fcntl(s->fds[1], F_SETFL, O_NONBLOCK);
506 qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], qemu_rbd_aio_event_reader,
507 NULL, qemu_rbd_aio_flush_cb, s);
515 rados_ioctx_destroy(s->io_ctx);
517 rados_shutdown(s->cluster);
522 static void qemu_rbd_close(BlockDriverState *bs)
524 BDRVRBDState *s = bs->opaque;
528 qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], NULL, NULL, NULL, NULL);
531 rados_ioctx_destroy(s->io_ctx);
533 rados_shutdown(s->cluster);
537 * Cancel aio. Since we don't reference acb in a non qemu threads,
538 * it is safe to access it here.
540 static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb)
542 RBDAIOCB *acb = (RBDAIOCB *) blockacb;
546 static AIOPool rbd_aio_pool = {
547 .aiocb_size = sizeof(RBDAIOCB),
548 .cancel = qemu_rbd_aio_cancel,
551 static int qemu_rbd_send_pipe(BDRVRBDState *s, RADOSCB *rcb)
556 int fd = s->fds[RBD_FD_WRITE];
558 /* send the op pointer to the qemu thread that is responsible
559 for the aio/op completion. Must do it in a qemu thread context */
560 ret = write(fd, (void *)&rcb, sizeof(rcb));
564 if (errno == EINTR) {
567 if (errno != EAGAIN) {
574 ret = select(fd + 1, NULL, &wfd, NULL, NULL);
575 } while (ret < 0 && errno == EINTR);
582 * This is the callback function for rbd_aio_read and _write
584 * Note: this function is being called from a non qemu thread so
585 * we need to be careful about what we do here. Generally we only
586 * write to the block notification pipe, and do the rest of the
587 * io completion handling from qemu_rbd_aio_event_reader() which
588 * runs in a qemu context.
590 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
593 rcb->ret = rbd_aio_get_return_value(c);
595 ret = qemu_rbd_send_pipe(rcb->s, rcb);
597 error_report("failed writing to acb->s->fds");
602 /* Callback when all queued rbd_aio requests are complete */
604 static void rbd_aio_bh_cb(void *opaque)
606 RBDAIOCB *acb = opaque;
609 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
611 qemu_vfree(acb->bounce);
612 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
613 qemu_bh_delete(acb->bh);
616 qemu_aio_release(acb);
619 static BlockDriverAIOCB *rbd_aio_rw_vector(BlockDriverState *bs,
623 BlockDriverCompletionFunc *cb,
624 void *opaque, int write)
633 BDRVRBDState *s = bs->opaque;
635 acb = qemu_aio_get(&rbd_aio_pool, bs, cb, opaque);
638 acb->bounce = qemu_blockalign(bs, qiov->size);
646 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
651 off = sector_num * BDRV_SECTOR_SIZE;
652 size = nb_sectors * BDRV_SECTOR_SIZE;
654 s->qemu_aio_count++; /* All the RADOSCB */
656 rcb = g_malloc(sizeof(RADOSCB));
662 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
668 r = rbd_aio_write(s->image, off, size, buf, c);
670 r = rbd_aio_read(s->image, off, size, buf, c);
682 qemu_aio_release(acb);
686 static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
690 BlockDriverCompletionFunc *cb,
693 return rbd_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
696 static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
700 BlockDriverCompletionFunc *cb,
703 return rbd_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
706 static int qemu_rbd_co_flush(BlockDriverState *bs)
708 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
709 /* rbd_flush added in 0.1.1 */
710 BDRVRBDState *s = bs->opaque;
711 return rbd_flush(s->image);
717 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
719 BDRVRBDState *s = bs->opaque;
720 rbd_image_info_t info;
723 r = rbd_stat(s->image, &info, sizeof(info));
728 bdi->cluster_size = info.obj_size;
732 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
734 BDRVRBDState *s = bs->opaque;
735 rbd_image_info_t info;
738 r = rbd_stat(s->image, &info, sizeof(info));
746 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
748 BDRVRBDState *s = bs->opaque;
751 r = rbd_resize(s->image, offset);
759 static int qemu_rbd_snap_create(BlockDriverState *bs,
760 QEMUSnapshotInfo *sn_info)
762 BDRVRBDState *s = bs->opaque;
765 if (sn_info->name[0] == '\0') {
766 return -EINVAL; /* we need a name for rbd snapshots */
770 * rbd snapshots are using the name as the user controlled unique identifier
771 * we can't use the rbd snapid for that purpose, as it can't be set
773 if (sn_info->id_str[0] != '\0' &&
774 strcmp(sn_info->id_str, sn_info->name) != 0) {
778 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
782 r = rbd_snap_create(s->image, sn_info->name);
784 error_report("failed to create snap: %s", strerror(-r));
791 static int qemu_rbd_snap_remove(BlockDriverState *bs,
792 const char *snapshot_name)
794 BDRVRBDState *s = bs->opaque;
797 r = rbd_snap_remove(s->image, snapshot_name);
801 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
802 const char *snapshot_name)
804 BDRVRBDState *s = bs->opaque;
807 r = rbd_snap_rollback(s->image, snapshot_name);
811 static int qemu_rbd_snap_list(BlockDriverState *bs,
812 QEMUSnapshotInfo **psn_tab)
814 BDRVRBDState *s = bs->opaque;
815 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
817 rbd_snap_info_t *snaps;
818 int max_snaps = RBD_MAX_SNAPS;
821 snaps = g_malloc(sizeof(*snaps) * max_snaps);
822 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
823 if (snap_count < 0) {
826 } while (snap_count == -ERANGE);
828 if (snap_count <= 0) {
832 sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo));
834 for (i = 0; i < snap_count; i++) {
835 const char *snap_name = snaps[i].name;
837 sn_info = sn_tab + i;
838 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
839 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
841 sn_info->vm_state_size = snaps[i].size;
842 sn_info->date_sec = 0;
843 sn_info->date_nsec = 0;
844 sn_info->vm_clock_nsec = 0;
846 rbd_snap_list_end(snaps);
853 static QEMUOptionParameter qemu_rbd_create_options[] = {
855 .name = BLOCK_OPT_SIZE,
857 .help = "Virtual disk size"
860 .name = BLOCK_OPT_CLUSTER_SIZE,
862 .help = "RBD object size"
867 static BlockDriver bdrv_rbd = {
868 .format_name = "rbd",
869 .instance_size = sizeof(BDRVRBDState),
870 .bdrv_file_open = qemu_rbd_open,
871 .bdrv_close = qemu_rbd_close,
872 .bdrv_create = qemu_rbd_create,
873 .bdrv_get_info = qemu_rbd_getinfo,
874 .create_options = qemu_rbd_create_options,
875 .bdrv_getlength = qemu_rbd_getlength,
876 .bdrv_truncate = qemu_rbd_truncate,
877 .protocol_name = "rbd",
879 .bdrv_aio_readv = qemu_rbd_aio_readv,
880 .bdrv_aio_writev = qemu_rbd_aio_writev,
881 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
883 .bdrv_snapshot_create = qemu_rbd_snap_create,
884 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
885 .bdrv_snapshot_list = qemu_rbd_snap_list,
886 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
889 static void bdrv_rbd_init(void)
891 bdrv_register(&bdrv_rbd);
894 block_init(bdrv_rbd_init);