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.
14 #include "qemu-common.h"
15 #include "qemu-error.h"
17 #include "block_int.h"
19 #include <rbd/librbd.h>
24 * When specifying the image filename use:
26 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
28 * poolname must be the name of an existing rados pool
30 * devicename is the basename for all objects used to
31 * emulate the raw device.
33 * Each option given is used to configure rados, and may be
34 * any Ceph option, or "conf". The "conf" option specifies
35 * a Ceph configuration file to read.
37 * Metadata information (image size, ...) is stored in an
38 * object with the name "devicename.rbd".
40 * The raw device is split into 4MB sized objects by default.
41 * The sequencenumber is encoded in a 12 byte long hex-string,
42 * and is attached to the devicename, separated by a dot.
43 * e.g. "devicename.1234567890ab"
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 end
= strchr(src
, delim
);
115 error_report("%s too long", name
);
118 error_report("%s too short", name
);
122 pstrcpy(dst
, dst_len
, src
);
127 static int qemu_rbd_parsename(const char *filename
,
128 char *pool
, int pool_len
,
129 char *snap
, int snap_len
,
130 char *name
, int name_len
,
131 char *conf
, int conf_len
)
137 if (!strstart(filename
, "rbd:", &start
)) {
141 buf
= qemu_strdup(start
);
146 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
, '/', "pool name", &p
);
152 if (strchr(p
, '@')) {
153 ret
= qemu_rbd_next_tok(name
, name_len
, p
, '@', "object name", &p
);
157 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
, ':', "snap name", &p
);
159 ret
= qemu_rbd_next_tok(name
, name_len
, p
, ':', "object name", &p
);
165 ret
= qemu_rbd_next_tok(conf
, conf_len
, p
, '\0', "configuration", &p
);
172 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
)
175 char name
[RBD_MAX_CONF_NAME_SIZE
];
176 char value
[RBD_MAX_CONF_VAL_SIZE
];
179 buf
= qemu_strdup(conf
);
183 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
184 '=', "conf option name", &p
);
190 error_report("conf option %s has no value", name
);
195 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
196 ':', "conf option value", &p
);
201 if (strcmp(name
, "conf")) {
202 ret
= rados_conf_set(cluster
, name
, value
);
204 error_report("invalid conf option %s", name
);
209 ret
= rados_conf_read_file(cluster
, value
);
211 error_report("error reading conf file %s", value
);
221 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
)
226 char pool
[RBD_MAX_POOL_NAME_SIZE
];
227 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
228 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
229 char conf
[RBD_MAX_CONF_SIZE
];
231 rados_ioctx_t io_ctx
;
234 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
235 snap_buf
, sizeof(snap_buf
),
237 conf
, sizeof(conf
)) < 0) {
241 /* Read out options */
242 while (options
&& options
->name
) {
243 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
244 bytes
= options
->value
.n
;
245 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
246 if (options
->value
.n
) {
247 objsize
= options
->value
.n
;
248 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
249 error_report("obj size needs to be power of 2");
252 if (objsize
< 4096) {
253 error_report("obj size too small");
256 obj_order
= ffs(objsize
) - 1;
262 if (rados_create(&cluster
, NULL
) < 0) {
263 error_report("error initializing");
267 if (strstr(conf
, "conf=") == NULL
) {
268 if (rados_conf_read_file(cluster
, NULL
) < 0) {
269 error_report("error reading config file");
270 rados_shutdown(cluster
);
275 if (conf
[0] != '\0' &&
276 qemu_rbd_set_conf(cluster
, conf
) < 0) {
277 error_report("error setting config options");
278 rados_shutdown(cluster
);
282 if (rados_connect(cluster
) < 0) {
283 error_report("error connecting");
284 rados_shutdown(cluster
);
288 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
289 error_report("error opening pool %s", pool
);
290 rados_shutdown(cluster
);
294 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
295 rados_ioctx_destroy(io_ctx
);
296 rados_shutdown(cluster
);
302 * This aio completion is being called from qemu_rbd_aio_event_reader()
303 * and runs in qemu context. It schedules a bh, but just in case the aio
304 * was not cancelled before.
306 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
308 RBDAIOCB
*acb
= rcb
->acb
;
311 if (acb
->cancelled
) {
312 qemu_vfree(acb
->bounce
);
313 qemu_aio_release(acb
);
323 } else if (!acb
->error
) {
324 acb
->ret
= rcb
->size
;
328 memset(rcb
->buf
, 0, rcb
->size
);
331 } else if (r
< rcb
->size
) {
332 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
334 acb
->ret
= rcb
->size
;
336 } else if (!acb
->error
) {
340 /* Note that acb->bh can be NULL in case where the aio was cancelled */
341 acb
->bh
= qemu_bh_new(rbd_aio_bh_cb
, acb
);
342 qemu_bh_schedule(acb
->bh
);
348 * aio fd read handler. It runs in the qemu context and calls the
349 * completion handling of completed rados aio operations.
351 static void qemu_rbd_aio_event_reader(void *opaque
)
353 BDRVRBDState
*s
= opaque
;
358 char *p
= (char *)&s
->event_rcb
;
360 /* now read the rcb pointer that was sent from a non qemu thread */
361 if ((ret
= read(s
->fds
[RBD_FD_READ
], p
+ s
->event_reader_pos
,
362 sizeof(s
->event_rcb
) - s
->event_reader_pos
)) > 0) {
364 s
->event_reader_pos
+= ret
;
365 if (s
->event_reader_pos
== sizeof(s
->event_rcb
)) {
366 s
->event_reader_pos
= 0;
367 qemu_rbd_complete_aio(s
->event_rcb
);
372 } while (ret
< 0 && errno
== EINTR
);
375 static int qemu_rbd_aio_flush_cb(void *opaque
)
377 BDRVRBDState
*s
= opaque
;
379 return (s
->qemu_aio_count
> 0);
382 static int qemu_rbd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
384 BDRVRBDState
*s
= bs
->opaque
;
385 char pool
[RBD_MAX_POOL_NAME_SIZE
];
386 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
387 char conf
[RBD_MAX_CONF_SIZE
];
390 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
391 snap_buf
, sizeof(snap_buf
),
392 s
->name
, sizeof(s
->name
),
393 conf
, sizeof(conf
)) < 0) {
397 if (snap_buf
[0] != '\0') {
398 s
->snap
= qemu_strdup(snap_buf
);
401 r
= rados_create(&s
->cluster
, NULL
);
403 error_report("error initializing");
407 if (strstr(conf
, "conf=") == NULL
) {
408 r
= rados_conf_read_file(s
->cluster
, NULL
);
410 error_report("error reading config file");
411 rados_shutdown(s
->cluster
);
416 if (conf
[0] != '\0') {
417 r
= qemu_rbd_set_conf(s
->cluster
, conf
);
419 error_report("error setting config options");
420 rados_shutdown(s
->cluster
);
425 r
= rados_connect(s
->cluster
);
427 error_report("error connecting");
428 rados_shutdown(s
->cluster
);
432 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
434 error_report("error opening pool %s", pool
);
435 rados_shutdown(s
->cluster
);
439 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
441 error_report("error reading header from %s", s
->name
);
442 rados_ioctx_destroy(s
->io_ctx
);
443 rados_shutdown(s
->cluster
);
447 bs
->read_only
= (s
->snap
!= NULL
);
449 s
->event_reader_pos
= 0;
450 r
= qemu_pipe(s
->fds
);
452 error_report("error opening eventfd");
455 fcntl(s
->fds
[0], F_SETFL
, O_NONBLOCK
);
456 fcntl(s
->fds
[1], F_SETFL
, O_NONBLOCK
);
457 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], qemu_rbd_aio_event_reader
,
458 NULL
, qemu_rbd_aio_flush_cb
, NULL
, s
);
465 rados_ioctx_destroy(s
->io_ctx
);
466 rados_shutdown(s
->cluster
);
470 static void qemu_rbd_close(BlockDriverState
*bs
)
472 BDRVRBDState
*s
= bs
->opaque
;
476 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], NULL
, NULL
, NULL
, NULL
,
480 rados_ioctx_destroy(s
->io_ctx
);
482 rados_shutdown(s
->cluster
);
486 * Cancel aio. Since we don't reference acb in a non qemu threads,
487 * it is safe to access it here.
489 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
491 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
495 static AIOPool rbd_aio_pool
= {
496 .aiocb_size
= sizeof(RBDAIOCB
),
497 .cancel
= qemu_rbd_aio_cancel
,
500 static int qemu_rbd_send_pipe(BDRVRBDState
*s
, RADOSCB
*rcb
)
505 int fd
= s
->fds
[RBD_FD_WRITE
];
507 /* send the op pointer to the qemu thread that is responsible
508 for the aio/op completion. Must do it in a qemu thread context */
509 ret
= write(fd
, (void *)&rcb
, sizeof(rcb
));
513 if (errno
== EINTR
) {
516 if (errno
!= EAGAIN
) {
523 ret
= select(fd
+ 1, NULL
, &wfd
, NULL
, NULL
);
524 } while (ret
< 0 && errno
== EINTR
);
531 * This is the callback function for rbd_aio_read and _write
533 * Note: this function is being called from a non qemu thread so
534 * we need to be careful about what we do here. Generally we only
535 * write to the block notification pipe, and do the rest of the
536 * io completion handling from qemu_rbd_aio_event_reader() which
537 * runs in a qemu context.
539 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
542 rcb
->ret
= rbd_aio_get_return_value(c
);
544 ret
= qemu_rbd_send_pipe(rcb
->s
, rcb
);
546 error_report("failed writing to acb->s->fds");
551 /* Callback when all queued rbd_aio requests are complete */
553 static void rbd_aio_bh_cb(void *opaque
)
555 RBDAIOCB
*acb
= opaque
;
558 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
560 qemu_vfree(acb
->bounce
);
561 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
562 qemu_bh_delete(acb
->bh
);
565 qemu_aio_release(acb
);
568 static BlockDriverAIOCB
*rbd_aio_rw_vector(BlockDriverState
*bs
,
572 BlockDriverCompletionFunc
*cb
,
573 void *opaque
, int write
)
582 BDRVRBDState
*s
= bs
->opaque
;
584 acb
= qemu_aio_get(&rbd_aio_pool
, bs
, cb
, opaque
);
590 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
598 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
603 off
= sector_num
* BDRV_SECTOR_SIZE
;
604 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
606 s
->qemu_aio_count
++; /* All the RADOSCB */
608 rcb
= qemu_malloc(sizeof(RADOSCB
));
614 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
620 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
622 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
634 qemu_aio_release(acb
);
638 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
642 BlockDriverCompletionFunc
*cb
,
645 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
648 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
652 BlockDriverCompletionFunc
*cb
,
655 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
658 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
660 BDRVRBDState
*s
= bs
->opaque
;
661 rbd_image_info_t info
;
664 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
669 bdi
->cluster_size
= info
.obj_size
;
673 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
675 BDRVRBDState
*s
= bs
->opaque
;
676 rbd_image_info_t info
;
679 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
687 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
689 BDRVRBDState
*s
= bs
->opaque
;
692 r
= rbd_resize(s
->image
, offset
);
700 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
701 QEMUSnapshotInfo
*sn_info
)
703 BDRVRBDState
*s
= bs
->opaque
;
706 if (sn_info
->name
[0] == '\0') {
707 return -EINVAL
; /* we need a name for rbd snapshots */
711 * rbd snapshots are using the name as the user controlled unique identifier
712 * we can't use the rbd snapid for that purpose, as it can't be set
714 if (sn_info
->id_str
[0] != '\0' &&
715 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
719 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
723 r
= rbd_snap_create(s
->image
, sn_info
->name
);
725 error_report("failed to create snap: %s", strerror(-r
));
732 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
733 QEMUSnapshotInfo
**psn_tab
)
735 BDRVRBDState
*s
= bs
->opaque
;
736 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
738 rbd_snap_info_t
*snaps
;
739 int max_snaps
= RBD_MAX_SNAPS
;
742 snaps
= qemu_malloc(sizeof(*snaps
) * max_snaps
);
743 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
744 if (snap_count
< 0) {
747 } while (snap_count
== -ERANGE
);
749 if (snap_count
<= 0) {
753 sn_tab
= qemu_mallocz(snap_count
* sizeof(QEMUSnapshotInfo
));
755 for (i
= 0; i
< snap_count
; i
++) {
756 const char *snap_name
= snaps
[i
].name
;
758 sn_info
= sn_tab
+ i
;
759 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
760 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
762 sn_info
->vm_state_size
= snaps
[i
].size
;
763 sn_info
->date_sec
= 0;
764 sn_info
->date_nsec
= 0;
765 sn_info
->vm_clock_nsec
= 0;
767 rbd_snap_list_end(snaps
);
773 static QEMUOptionParameter qemu_rbd_create_options
[] = {
775 .name
= BLOCK_OPT_SIZE
,
777 .help
= "Virtual disk size"
780 .name
= BLOCK_OPT_CLUSTER_SIZE
,
782 .help
= "RBD object size"
787 static BlockDriver bdrv_rbd
= {
788 .format_name
= "rbd",
789 .instance_size
= sizeof(BDRVRBDState
),
790 .bdrv_file_open
= qemu_rbd_open
,
791 .bdrv_close
= qemu_rbd_close
,
792 .bdrv_create
= qemu_rbd_create
,
793 .bdrv_get_info
= qemu_rbd_getinfo
,
794 .create_options
= qemu_rbd_create_options
,
795 .bdrv_getlength
= qemu_rbd_getlength
,
796 .bdrv_truncate
= qemu_rbd_truncate
,
797 .protocol_name
= "rbd",
799 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
800 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
802 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
803 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
806 static void bdrv_rbd_init(void)
808 bdrv_register(&bdrv_rbd
);
811 block_init(bdrv_rbd_init
);