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
= g_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 char *qemu_rbd_parse_clientname(const char *conf
, char *clientname
)
174 const char *p
= conf
;
178 const char *end
= strchr(p
, ':');
186 if (strncmp(p
, "id=", 3) == 0) {
188 strncpy(clientname
, p
+ 3, len
);
189 clientname
[len
] = '\0';
200 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
)
203 char name
[RBD_MAX_CONF_NAME_SIZE
];
204 char value
[RBD_MAX_CONF_VAL_SIZE
];
207 buf
= g_strdup(conf
);
211 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
212 '=', "conf option name", &p
);
218 error_report("conf option %s has no value", name
);
223 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
224 ':', "conf option value", &p
);
229 if (strcmp(name
, "conf") == 0) {
230 ret
= rados_conf_read_file(cluster
, value
);
232 error_report("error reading conf file %s", value
);
235 } else if (strcmp(name
, "id") == 0) {
236 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
238 ret
= rados_conf_set(cluster
, name
, value
);
240 error_report("invalid conf option %s", name
);
251 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
)
256 char pool
[RBD_MAX_POOL_NAME_SIZE
];
257 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
258 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
259 char conf
[RBD_MAX_CONF_SIZE
];
260 char clientname_buf
[RBD_MAX_CONF_SIZE
];
263 rados_ioctx_t io_ctx
;
266 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
267 snap_buf
, sizeof(snap_buf
),
269 conf
, sizeof(conf
)) < 0) {
273 /* Read out options */
274 while (options
&& options
->name
) {
275 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
276 bytes
= options
->value
.n
;
277 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
278 if (options
->value
.n
) {
279 objsize
= options
->value
.n
;
280 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
281 error_report("obj size needs to be power of 2");
284 if (objsize
< 4096) {
285 error_report("obj size too small");
288 obj_order
= ffs(objsize
) - 1;
294 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
295 if (rados_create(&cluster
, clientname
) < 0) {
296 error_report("error initializing");
300 if (strstr(conf
, "conf=") == NULL
) {
301 if (rados_conf_read_file(cluster
, NULL
) < 0) {
302 error_report("error reading config file");
303 rados_shutdown(cluster
);
308 if (conf
[0] != '\0' &&
309 qemu_rbd_set_conf(cluster
, conf
) < 0) {
310 error_report("error setting config options");
311 rados_shutdown(cluster
);
315 if (rados_connect(cluster
) < 0) {
316 error_report("error connecting");
317 rados_shutdown(cluster
);
321 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
322 error_report("error opening pool %s", pool
);
323 rados_shutdown(cluster
);
327 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
328 rados_ioctx_destroy(io_ctx
);
329 rados_shutdown(cluster
);
335 * This aio completion is being called from qemu_rbd_aio_event_reader()
336 * and runs in qemu context. It schedules a bh, but just in case the aio
337 * was not cancelled before.
339 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
341 RBDAIOCB
*acb
= rcb
->acb
;
344 if (acb
->cancelled
) {
345 qemu_vfree(acb
->bounce
);
346 qemu_aio_release(acb
);
356 } else if (!acb
->error
) {
357 acb
->ret
= rcb
->size
;
361 memset(rcb
->buf
, 0, rcb
->size
);
364 } else if (r
< rcb
->size
) {
365 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
367 acb
->ret
= rcb
->size
;
369 } else if (!acb
->error
) {
373 /* Note that acb->bh can be NULL in case where the aio was cancelled */
374 acb
->bh
= qemu_bh_new(rbd_aio_bh_cb
, acb
);
375 qemu_bh_schedule(acb
->bh
);
381 * aio fd read handler. It runs in the qemu context and calls the
382 * completion handling of completed rados aio operations.
384 static void qemu_rbd_aio_event_reader(void *opaque
)
386 BDRVRBDState
*s
= opaque
;
391 char *p
= (char *)&s
->event_rcb
;
393 /* now read the rcb pointer that was sent from a non qemu thread */
394 ret
= read(s
->fds
[RBD_FD_READ
], p
+ s
->event_reader_pos
,
395 sizeof(s
->event_rcb
) - s
->event_reader_pos
);
397 s
->event_reader_pos
+= ret
;
398 if (s
->event_reader_pos
== sizeof(s
->event_rcb
)) {
399 s
->event_reader_pos
= 0;
400 qemu_rbd_complete_aio(s
->event_rcb
);
404 } while (ret
< 0 && errno
== EINTR
);
407 static int qemu_rbd_aio_flush_cb(void *opaque
)
409 BDRVRBDState
*s
= opaque
;
411 return (s
->qemu_aio_count
> 0);
414 static int qemu_rbd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
416 BDRVRBDState
*s
= bs
->opaque
;
417 char pool
[RBD_MAX_POOL_NAME_SIZE
];
418 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
419 char conf
[RBD_MAX_CONF_SIZE
];
420 char clientname_buf
[RBD_MAX_CONF_SIZE
];
424 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
425 snap_buf
, sizeof(snap_buf
),
426 s
->name
, sizeof(s
->name
),
427 conf
, sizeof(conf
)) < 0) {
431 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
432 r
= rados_create(&s
->cluster
, clientname
);
434 error_report("error initializing");
439 if (snap_buf
[0] != '\0') {
440 s
->snap
= g_strdup(snap_buf
);
443 if (strstr(conf
, "conf=") == NULL
) {
444 r
= rados_conf_read_file(s
->cluster
, NULL
);
446 error_report("error reading config file");
447 goto failed_shutdown
;
451 if (conf
[0] != '\0') {
452 r
= qemu_rbd_set_conf(s
->cluster
, conf
);
454 error_report("error setting config options");
455 goto failed_shutdown
;
459 r
= rados_connect(s
->cluster
);
461 error_report("error connecting");
462 goto failed_shutdown
;
465 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
467 error_report("error opening pool %s", pool
);
468 goto failed_shutdown
;
471 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
473 error_report("error reading header from %s", s
->name
);
477 bs
->read_only
= (s
->snap
!= NULL
);
479 s
->event_reader_pos
= 0;
480 r
= qemu_pipe(s
->fds
);
482 error_report("error opening eventfd");
485 fcntl(s
->fds
[0], F_SETFL
, O_NONBLOCK
);
486 fcntl(s
->fds
[1], F_SETFL
, O_NONBLOCK
);
487 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], qemu_rbd_aio_event_reader
,
488 NULL
, qemu_rbd_aio_flush_cb
, NULL
, s
);
496 rados_ioctx_destroy(s
->io_ctx
);
498 rados_shutdown(s
->cluster
);
503 static void qemu_rbd_close(BlockDriverState
*bs
)
505 BDRVRBDState
*s
= bs
->opaque
;
509 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], NULL
, NULL
, NULL
, NULL
,
513 rados_ioctx_destroy(s
->io_ctx
);
515 rados_shutdown(s
->cluster
);
519 * Cancel aio. Since we don't reference acb in a non qemu threads,
520 * it is safe to access it here.
522 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
524 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
528 static AIOPool rbd_aio_pool
= {
529 .aiocb_size
= sizeof(RBDAIOCB
),
530 .cancel
= qemu_rbd_aio_cancel
,
533 static int qemu_rbd_send_pipe(BDRVRBDState
*s
, RADOSCB
*rcb
)
538 int fd
= s
->fds
[RBD_FD_WRITE
];
540 /* send the op pointer to the qemu thread that is responsible
541 for the aio/op completion. Must do it in a qemu thread context */
542 ret
= write(fd
, (void *)&rcb
, sizeof(rcb
));
546 if (errno
== EINTR
) {
549 if (errno
!= EAGAIN
) {
556 ret
= select(fd
+ 1, NULL
, &wfd
, NULL
, NULL
);
557 } while (ret
< 0 && errno
== EINTR
);
564 * This is the callback function for rbd_aio_read and _write
566 * Note: this function is being called from a non qemu thread so
567 * we need to be careful about what we do here. Generally we only
568 * write to the block notification pipe, and do the rest of the
569 * io completion handling from qemu_rbd_aio_event_reader() which
570 * runs in a qemu context.
572 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
575 rcb
->ret
= rbd_aio_get_return_value(c
);
577 ret
= qemu_rbd_send_pipe(rcb
->s
, rcb
);
579 error_report("failed writing to acb->s->fds");
584 /* Callback when all queued rbd_aio requests are complete */
586 static void rbd_aio_bh_cb(void *opaque
)
588 RBDAIOCB
*acb
= opaque
;
591 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
593 qemu_vfree(acb
->bounce
);
594 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
595 qemu_bh_delete(acb
->bh
);
598 qemu_aio_release(acb
);
601 static BlockDriverAIOCB
*rbd_aio_rw_vector(BlockDriverState
*bs
,
605 BlockDriverCompletionFunc
*cb
,
606 void *opaque
, int write
)
615 BDRVRBDState
*s
= bs
->opaque
;
617 acb
= qemu_aio_get(&rbd_aio_pool
, bs
, cb
, opaque
);
623 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
631 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
636 off
= sector_num
* BDRV_SECTOR_SIZE
;
637 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
639 s
->qemu_aio_count
++; /* All the RADOSCB */
641 rcb
= g_malloc(sizeof(RADOSCB
));
647 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
653 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
655 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
667 qemu_aio_release(acb
);
671 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
675 BlockDriverCompletionFunc
*cb
,
678 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
681 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
685 BlockDriverCompletionFunc
*cb
,
688 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
691 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
693 BDRVRBDState
*s
= bs
->opaque
;
694 rbd_image_info_t info
;
697 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
702 bdi
->cluster_size
= info
.obj_size
;
706 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
708 BDRVRBDState
*s
= bs
->opaque
;
709 rbd_image_info_t info
;
712 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
720 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
722 BDRVRBDState
*s
= bs
->opaque
;
725 r
= rbd_resize(s
->image
, offset
);
733 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
734 QEMUSnapshotInfo
*sn_info
)
736 BDRVRBDState
*s
= bs
->opaque
;
739 if (sn_info
->name
[0] == '\0') {
740 return -EINVAL
; /* we need a name for rbd snapshots */
744 * rbd snapshots are using the name as the user controlled unique identifier
745 * we can't use the rbd snapid for that purpose, as it can't be set
747 if (sn_info
->id_str
[0] != '\0' &&
748 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
752 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
756 r
= rbd_snap_create(s
->image
, sn_info
->name
);
758 error_report("failed to create snap: %s", strerror(-r
));
765 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
766 QEMUSnapshotInfo
**psn_tab
)
768 BDRVRBDState
*s
= bs
->opaque
;
769 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
771 rbd_snap_info_t
*snaps
;
772 int max_snaps
= RBD_MAX_SNAPS
;
775 snaps
= g_malloc(sizeof(*snaps
) * max_snaps
);
776 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
777 if (snap_count
< 0) {
780 } while (snap_count
== -ERANGE
);
782 if (snap_count
<= 0) {
786 sn_tab
= g_malloc0(snap_count
* sizeof(QEMUSnapshotInfo
));
788 for (i
= 0; i
< snap_count
; i
++) {
789 const char *snap_name
= snaps
[i
].name
;
791 sn_info
= sn_tab
+ i
;
792 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
793 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
795 sn_info
->vm_state_size
= snaps
[i
].size
;
796 sn_info
->date_sec
= 0;
797 sn_info
->date_nsec
= 0;
798 sn_info
->vm_clock_nsec
= 0;
800 rbd_snap_list_end(snaps
);
806 static QEMUOptionParameter qemu_rbd_create_options
[] = {
808 .name
= BLOCK_OPT_SIZE
,
810 .help
= "Virtual disk size"
813 .name
= BLOCK_OPT_CLUSTER_SIZE
,
815 .help
= "RBD object size"
820 static BlockDriver bdrv_rbd
= {
821 .format_name
= "rbd",
822 .instance_size
= sizeof(BDRVRBDState
),
823 .bdrv_file_open
= qemu_rbd_open
,
824 .bdrv_close
= qemu_rbd_close
,
825 .bdrv_create
= qemu_rbd_create
,
826 .bdrv_get_info
= qemu_rbd_getinfo
,
827 .create_options
= qemu_rbd_create_options
,
828 .bdrv_getlength
= qemu_rbd_getlength
,
829 .bdrv_truncate
= qemu_rbd_truncate
,
830 .protocol_name
= "rbd",
832 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
833 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
835 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
836 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
839 static void bdrv_rbd_init(void)
841 bdrv_register(&bdrv_rbd
);
844 block_init(bdrv_rbd_init
);