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
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 * Metadata information (image size, ...) is stored in an
34 * object with the name "devicename.rbd".
36 * The raw device is split into 4MB sized objects by default.
37 * The sequencenumber is encoded in a 12 byte long hex-string,
38 * and is attached to the devicename, separated by a dot.
39 * e.g. "devicename.1234567890ab"
43 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
45 #define RBD_MAX_CONF_NAME_SIZE 128
46 #define RBD_MAX_CONF_VAL_SIZE 512
47 #define RBD_MAX_CONF_SIZE 1024
48 #define RBD_MAX_POOL_NAME_SIZE 128
49 #define RBD_MAX_SNAP_NAME_SIZE 128
50 #define RBD_MAX_SNAPS 100
52 typedef struct RBDAIOCB
{
53 BlockDriverAIOCB common
;
61 struct BDRVRBDState
*s
;
65 typedef struct RADOSCB
{
68 struct BDRVRBDState
*s
;
76 #define RBD_FD_WRITE 1
78 typedef struct BDRVRBDState
{
83 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
90 static void rbd_aio_bh_cb(void *opaque
);
92 static int qemu_rbd_next_tok(char *dst
, int dst_len
,
93 char *src
, char delim
,
103 end
= strchr(src
, delim
);
111 error_report("%s too long", name
);
114 error_report("%s too short", name
);
118 pstrcpy(dst
, dst_len
, src
);
123 static int qemu_rbd_parsename(const char *filename
,
124 char *pool
, int pool_len
,
125 char *snap
, int snap_len
,
126 char *name
, int name_len
)
132 if (!strstart(filename
, "rbd:", &start
)) {
136 buf
= qemu_strdup(start
);
139 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
, '/', "pool name", &p
);
144 ret
= qemu_rbd_next_tok(name
, name_len
, p
, '@', "object name", &p
);
153 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
, '\0', "snap name", &p
);
160 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
)
165 char pool
[RBD_MAX_POOL_NAME_SIZE
];
166 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
167 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
170 rados_ioctx_t io_ctx
;
173 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
174 snap_buf
, sizeof(snap_buf
),
175 name
, sizeof(name
)) < 0) {
178 if (snap_buf
[0] != '\0') {
182 /* Read out options */
183 while (options
&& options
->name
) {
184 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
185 bytes
= options
->value
.n
;
186 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
187 if (options
->value
.n
) {
188 objsize
= options
->value
.n
;
189 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
190 error_report("obj size needs to be power of 2");
193 if (objsize
< 4096) {
194 error_report("obj size too small");
197 obj_order
= ffs(objsize
) - 1;
203 if (rados_create(&cluster
, NULL
) < 0) {
204 error_report("error initializing");
208 if (rados_conf_read_file(cluster
, NULL
) < 0) {
209 error_report("error reading config file");
210 rados_shutdown(cluster
);
214 if (rados_connect(cluster
) < 0) {
215 error_report("error connecting");
216 rados_shutdown(cluster
);
220 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
221 error_report("error opening pool %s", pool
);
222 rados_shutdown(cluster
);
226 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
227 rados_ioctx_destroy(io_ctx
);
228 rados_shutdown(cluster
);
234 * This aio completion is being called from qemu_rbd_aio_event_reader()
235 * and runs in qemu context. It schedules a bh, but just in case the aio
236 * was not cancelled before.
238 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
240 RBDAIOCB
*acb
= rcb
->acb
;
243 if (acb
->cancelled
) {
244 qemu_vfree(acb
->bounce
);
245 qemu_aio_release(acb
);
255 } else if (!acb
->error
) {
256 acb
->ret
= rcb
->size
;
260 memset(rcb
->buf
, 0, rcb
->size
);
263 } else if (r
< rcb
->size
) {
264 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
266 acb
->ret
= rcb
->size
;
268 } else if (!acb
->error
) {
272 /* Note that acb->bh can be NULL in case where the aio was cancelled */
273 acb
->bh
= qemu_bh_new(rbd_aio_bh_cb
, acb
);
274 qemu_bh_schedule(acb
->bh
);
280 * aio fd read handler. It runs in the qemu context and calls the
281 * completion handling of completed rados aio operations.
283 static void qemu_rbd_aio_event_reader(void *opaque
)
285 BDRVRBDState
*s
= opaque
;
290 char *p
= (char *)&s
->event_rcb
;
292 /* now read the rcb pointer that was sent from a non qemu thread */
293 if ((ret
= read(s
->fds
[RBD_FD_READ
], p
+ s
->event_reader_pos
,
294 sizeof(s
->event_rcb
) - s
->event_reader_pos
)) > 0) {
296 s
->event_reader_pos
+= ret
;
297 if (s
->event_reader_pos
== sizeof(s
->event_rcb
)) {
298 s
->event_reader_pos
= 0;
299 qemu_rbd_complete_aio(s
->event_rcb
);
304 } while (ret
< 0 && errno
== EINTR
);
307 static int qemu_rbd_aio_flush_cb(void *opaque
)
309 BDRVRBDState
*s
= opaque
;
311 return (s
->qemu_aio_count
> 0);
314 static int qemu_rbd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
316 BDRVRBDState
*s
= bs
->opaque
;
317 char pool
[RBD_MAX_POOL_NAME_SIZE
];
318 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
321 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
322 snap_buf
, sizeof(snap_buf
),
323 s
->name
, sizeof(s
->name
)) < 0) {
327 if (snap_buf
[0] != '\0') {
328 s
->snap
= qemu_strdup(snap_buf
);
331 r
= rados_create(&s
->cluster
, NULL
);
333 error_report("error initializing");
337 r
= rados_conf_read_file(s
->cluster
, NULL
);
339 error_report("error reading config file");
340 rados_shutdown(s
->cluster
);
344 r
= rados_connect(s
->cluster
);
346 error_report("error connecting");
347 rados_shutdown(s
->cluster
);
351 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
353 error_report("error opening pool %s", pool
);
354 rados_shutdown(s
->cluster
);
358 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
360 error_report("error reading header from %s", s
->name
);
361 rados_ioctx_destroy(s
->io_ctx
);
362 rados_shutdown(s
->cluster
);
366 bs
->read_only
= (s
->snap
!= NULL
);
368 s
->event_reader_pos
= 0;
369 r
= qemu_pipe(s
->fds
);
371 error_report("error opening eventfd");
374 fcntl(s
->fds
[0], F_SETFL
, O_NONBLOCK
);
375 fcntl(s
->fds
[1], F_SETFL
, O_NONBLOCK
);
376 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], qemu_rbd_aio_event_reader
,
377 NULL
, qemu_rbd_aio_flush_cb
, NULL
, s
);
384 rados_ioctx_destroy(s
->io_ctx
);
385 rados_shutdown(s
->cluster
);
389 static void qemu_rbd_close(BlockDriverState
*bs
)
391 BDRVRBDState
*s
= bs
->opaque
;
395 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], NULL
, NULL
, NULL
, NULL
,
399 rados_ioctx_destroy(s
->io_ctx
);
401 rados_shutdown(s
->cluster
);
405 * Cancel aio. Since we don't reference acb in a non qemu threads,
406 * it is safe to access it here.
408 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
410 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
414 static AIOPool rbd_aio_pool
= {
415 .aiocb_size
= sizeof(RBDAIOCB
),
416 .cancel
= qemu_rbd_aio_cancel
,
419 static int qemu_rbd_send_pipe(BDRVRBDState
*s
, RADOSCB
*rcb
)
424 int fd
= s
->fds
[RBD_FD_WRITE
];
426 /* send the op pointer to the qemu thread that is responsible
427 for the aio/op completion. Must do it in a qemu thread context */
428 ret
= write(fd
, (void *)&rcb
, sizeof(rcb
));
432 if (errno
== EINTR
) {
435 if (errno
!= EAGAIN
) {
442 ret
= select(fd
+ 1, NULL
, &wfd
, NULL
, NULL
);
443 } while (ret
< 0 && errno
== EINTR
);
450 * This is the callback function for rbd_aio_read and _write
452 * Note: this function is being called from a non qemu thread so
453 * we need to be careful about what we do here. Generally we only
454 * write to the block notification pipe, and do the rest of the
455 * io completion handling from qemu_rbd_aio_event_reader() which
456 * runs in a qemu context.
458 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
461 rcb
->ret
= rbd_aio_get_return_value(c
);
463 ret
= qemu_rbd_send_pipe(rcb
->s
, rcb
);
465 error_report("failed writing to acb->s->fds");
470 /* Callback when all queued rbd_aio requests are complete */
472 static void rbd_aio_bh_cb(void *opaque
)
474 RBDAIOCB
*acb
= opaque
;
477 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
479 qemu_vfree(acb
->bounce
);
480 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
481 qemu_bh_delete(acb
->bh
);
484 qemu_aio_release(acb
);
487 static BlockDriverAIOCB
*rbd_aio_rw_vector(BlockDriverState
*bs
,
491 BlockDriverCompletionFunc
*cb
,
492 void *opaque
, int write
)
500 BDRVRBDState
*s
= bs
->opaque
;
502 acb
= qemu_aio_get(&rbd_aio_pool
, bs
, cb
, opaque
);
505 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
513 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
518 off
= sector_num
* BDRV_SECTOR_SIZE
;
519 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
521 s
->qemu_aio_count
++; /* All the RADOSCB */
523 rcb
= qemu_malloc(sizeof(RADOSCB
));
531 rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
532 rbd_aio_write(s
->image
, off
, size
, buf
, c
);
534 rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
535 rbd_aio_read(s
->image
, off
, size
, buf
, c
);
541 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
545 BlockDriverCompletionFunc
*cb
,
548 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
551 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
555 BlockDriverCompletionFunc
*cb
,
558 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
561 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
563 BDRVRBDState
*s
= bs
->opaque
;
564 rbd_image_info_t info
;
567 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
572 bdi
->cluster_size
= info
.obj_size
;
576 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
578 BDRVRBDState
*s
= bs
->opaque
;
579 rbd_image_info_t info
;
582 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
590 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
591 QEMUSnapshotInfo
*sn_info
)
593 BDRVRBDState
*s
= bs
->opaque
;
596 if (sn_info
->name
[0] == '\0') {
597 return -EINVAL
; /* we need a name for rbd snapshots */
601 * rbd snapshots are using the name as the user controlled unique identifier
602 * we can't use the rbd snapid for that purpose, as it can't be set
604 if (sn_info
->id_str
[0] != '\0' &&
605 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
609 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
613 r
= rbd_snap_create(s
->image
, sn_info
->name
);
615 error_report("failed to create snap: %s", strerror(-r
));
622 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
623 QEMUSnapshotInfo
**psn_tab
)
625 BDRVRBDState
*s
= bs
->opaque
;
626 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
628 rbd_snap_info_t
*snaps
;
629 int max_snaps
= RBD_MAX_SNAPS
;
632 snaps
= qemu_malloc(sizeof(*snaps
) * max_snaps
);
633 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
634 if (snap_count
< 0) {
637 } while (snap_count
== -ERANGE
);
639 if (snap_count
<= 0) {
643 sn_tab
= qemu_mallocz(snap_count
* sizeof(QEMUSnapshotInfo
));
645 for (i
= 0; i
< snap_count
; i
++) {
646 const char *snap_name
= snaps
[i
].name
;
648 sn_info
= sn_tab
+ i
;
649 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
650 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
652 sn_info
->vm_state_size
= snaps
[i
].size
;
653 sn_info
->date_sec
= 0;
654 sn_info
->date_nsec
= 0;
655 sn_info
->vm_clock_nsec
= 0;
657 rbd_snap_list_end(snaps
);
663 static QEMUOptionParameter qemu_rbd_create_options
[] = {
665 .name
= BLOCK_OPT_SIZE
,
667 .help
= "Virtual disk size"
670 .name
= BLOCK_OPT_CLUSTER_SIZE
,
672 .help
= "RBD object size"
677 static BlockDriver bdrv_rbd
= {
678 .format_name
= "rbd",
679 .instance_size
= sizeof(BDRVRBDState
),
680 .bdrv_file_open
= qemu_rbd_open
,
681 .bdrv_close
= qemu_rbd_close
,
682 .bdrv_create
= qemu_rbd_create
,
683 .bdrv_get_info
= qemu_rbd_getinfo
,
684 .create_options
= qemu_rbd_create_options
,
685 .bdrv_getlength
= qemu_rbd_getlength
,
686 .protocol_name
= "rbd",
688 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
689 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
691 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
692 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
695 static void bdrv_rbd_init(void)
697 bdrv_register(&bdrv_rbd
);
700 block_init(bdrv_rbd_init
);