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"
16 #include "block_int.h"
18 #include <rbd/librbd.h>
21 * When specifying the image filename use:
23 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
25 * poolname must be the name of an existing rados pool.
27 * devicename is the name of the rbd image.
29 * Each option given is used to configure rados, and may be any valid
30 * Ceph option, "id", or "conf".
32 * The "id" option indicates what user we should authenticate as to
33 * the Ceph cluster. If it is excluded we will use the Ceph default
36 * The "conf" option specifies a Ceph configuration file to read. If
37 * it is not specified, we will read from the default Ceph locations
38 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
39 * file, specify conf=/dev/null.
41 * Configuration values containing :, @, or = can be escaped with a
45 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
47 #define RBD_MAX_CONF_NAME_SIZE 128
48 #define RBD_MAX_CONF_VAL_SIZE 512
49 #define RBD_MAX_CONF_SIZE 1024
50 #define RBD_MAX_POOL_NAME_SIZE 128
51 #define RBD_MAX_SNAP_NAME_SIZE 128
52 #define RBD_MAX_SNAPS 100
54 typedef struct RBDAIOCB
{
55 BlockDriverAIOCB common
;
63 struct BDRVRBDState
*s
;
67 typedef struct RADOSCB
{
70 struct BDRVRBDState
*s
;
78 #define RBD_FD_WRITE 1
80 typedef struct BDRVRBDState
{
85 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
92 static void rbd_aio_bh_cb(void *opaque
);
94 static int qemu_rbd_next_tok(char *dst
, int dst_len
,
95 char *src
, char delim
,
105 end
= strchr(src
, delim
);
113 error_report("%s too long", name
);
116 error_report("%s too short", name
);
120 pstrcpy(dst
, dst_len
, src
);
125 static int qemu_rbd_parsename(const char *filename
,
126 char *pool
, int pool_len
,
127 char *snap
, int snap_len
,
128 char *name
, int name_len
,
129 char *conf
, int conf_len
)
135 if (!strstart(filename
, "rbd:", &start
)) {
139 buf
= g_strdup(start
);
144 ret
= qemu_rbd_next_tok(pool
, pool_len
, p
, '/', "pool name", &p
);
150 if (strchr(p
, '@')) {
151 ret
= qemu_rbd_next_tok(name
, name_len
, p
, '@', "object name", &p
);
155 ret
= qemu_rbd_next_tok(snap
, snap_len
, p
, ':', "snap name", &p
);
157 ret
= qemu_rbd_next_tok(name
, name_len
, p
, ':', "object name", &p
);
163 ret
= qemu_rbd_next_tok(conf
, conf_len
, p
, '\0', "configuration", &p
);
170 static char *qemu_rbd_parse_clientname(const char *conf
, char *clientname
)
172 const char *p
= conf
;
176 const char *end
= strchr(p
, ':');
184 if (strncmp(p
, "id=", 3) == 0) {
186 strncpy(clientname
, p
+ 3, len
);
187 clientname
[len
] = '\0';
198 static int qemu_rbd_set_conf(rados_t cluster
, const char *conf
)
201 char name
[RBD_MAX_CONF_NAME_SIZE
];
202 char value
[RBD_MAX_CONF_VAL_SIZE
];
205 buf
= g_strdup(conf
);
209 ret
= qemu_rbd_next_tok(name
, sizeof(name
), p
,
210 '=', "conf option name", &p
);
216 error_report("conf option %s has no value", name
);
221 ret
= qemu_rbd_next_tok(value
, sizeof(value
), p
,
222 ':', "conf option value", &p
);
227 if (strcmp(name
, "conf") == 0) {
228 ret
= rados_conf_read_file(cluster
, value
);
230 error_report("error reading conf file %s", value
);
233 } else if (strcmp(name
, "id") == 0) {
234 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
236 ret
= rados_conf_set(cluster
, name
, value
);
238 error_report("invalid conf option %s", name
);
249 static int qemu_rbd_create(const char *filename
, QEMUOptionParameter
*options
)
254 char pool
[RBD_MAX_POOL_NAME_SIZE
];
255 char name
[RBD_MAX_IMAGE_NAME_SIZE
];
256 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
257 char conf
[RBD_MAX_CONF_SIZE
];
258 char clientname_buf
[RBD_MAX_CONF_SIZE
];
261 rados_ioctx_t io_ctx
;
264 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
265 snap_buf
, sizeof(snap_buf
),
267 conf
, sizeof(conf
)) < 0) {
271 /* Read out options */
272 while (options
&& options
->name
) {
273 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
274 bytes
= options
->value
.n
;
275 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
276 if (options
->value
.n
) {
277 objsize
= options
->value
.n
;
278 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
279 error_report("obj size needs to be power of 2");
282 if (objsize
< 4096) {
283 error_report("obj size too small");
286 obj_order
= ffs(objsize
) - 1;
292 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
293 if (rados_create(&cluster
, clientname
) < 0) {
294 error_report("error initializing");
298 if (strstr(conf
, "conf=") == NULL
) {
299 /* try default location, but ignore failure */
300 rados_conf_read_file(cluster
, NULL
);
303 if (conf
[0] != '\0' &&
304 qemu_rbd_set_conf(cluster
, conf
) < 0) {
305 error_report("error setting config options");
306 rados_shutdown(cluster
);
310 if (rados_connect(cluster
) < 0) {
311 error_report("error connecting");
312 rados_shutdown(cluster
);
316 if (rados_ioctx_create(cluster
, pool
, &io_ctx
) < 0) {
317 error_report("error opening pool %s", pool
);
318 rados_shutdown(cluster
);
322 ret
= rbd_create(io_ctx
, name
, bytes
, &obj_order
);
323 rados_ioctx_destroy(io_ctx
);
324 rados_shutdown(cluster
);
330 * This aio completion is being called from qemu_rbd_aio_event_reader()
331 * and runs in qemu context. It schedules a bh, but just in case the aio
332 * was not cancelled before.
334 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
336 RBDAIOCB
*acb
= rcb
->acb
;
339 if (acb
->cancelled
) {
340 qemu_vfree(acb
->bounce
);
341 qemu_aio_release(acb
);
351 } else if (!acb
->error
) {
352 acb
->ret
= rcb
->size
;
356 memset(rcb
->buf
, 0, rcb
->size
);
359 } else if (r
< rcb
->size
) {
360 memset(rcb
->buf
+ r
, 0, rcb
->size
- r
);
362 acb
->ret
= rcb
->size
;
364 } else if (!acb
->error
) {
368 /* Note that acb->bh can be NULL in case where the aio was cancelled */
369 acb
->bh
= qemu_bh_new(rbd_aio_bh_cb
, acb
);
370 qemu_bh_schedule(acb
->bh
);
376 * aio fd read handler. It runs in the qemu context and calls the
377 * completion handling of completed rados aio operations.
379 static void qemu_rbd_aio_event_reader(void *opaque
)
381 BDRVRBDState
*s
= opaque
;
386 char *p
= (char *)&s
->event_rcb
;
388 /* now read the rcb pointer that was sent from a non qemu thread */
389 ret
= read(s
->fds
[RBD_FD_READ
], p
+ s
->event_reader_pos
,
390 sizeof(s
->event_rcb
) - s
->event_reader_pos
);
392 s
->event_reader_pos
+= ret
;
393 if (s
->event_reader_pos
== sizeof(s
->event_rcb
)) {
394 s
->event_reader_pos
= 0;
395 qemu_rbd_complete_aio(s
->event_rcb
);
399 } while (ret
< 0 && errno
== EINTR
);
402 static int qemu_rbd_aio_flush_cb(void *opaque
)
404 BDRVRBDState
*s
= opaque
;
406 return (s
->qemu_aio_count
> 0);
409 static int qemu_rbd_open(BlockDriverState
*bs
, const char *filename
, int flags
)
411 BDRVRBDState
*s
= bs
->opaque
;
412 char pool
[RBD_MAX_POOL_NAME_SIZE
];
413 char snap_buf
[RBD_MAX_SNAP_NAME_SIZE
];
414 char conf
[RBD_MAX_CONF_SIZE
];
415 char clientname_buf
[RBD_MAX_CONF_SIZE
];
419 if (qemu_rbd_parsename(filename
, pool
, sizeof(pool
),
420 snap_buf
, sizeof(snap_buf
),
421 s
->name
, sizeof(s
->name
),
422 conf
, sizeof(conf
)) < 0) {
426 clientname
= qemu_rbd_parse_clientname(conf
, clientname_buf
);
427 r
= rados_create(&s
->cluster
, clientname
);
429 error_report("error initializing");
434 if (snap_buf
[0] != '\0') {
435 s
->snap
= g_strdup(snap_buf
);
438 if (strstr(conf
, "conf=") == NULL
) {
439 /* try default location, but ignore failure */
440 rados_conf_read_file(s
->cluster
, NULL
);
443 if (conf
[0] != '\0') {
444 r
= qemu_rbd_set_conf(s
->cluster
, conf
);
446 error_report("error setting config options");
447 goto failed_shutdown
;
451 r
= rados_connect(s
->cluster
);
453 error_report("error connecting");
454 goto failed_shutdown
;
457 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
459 error_report("error opening pool %s", pool
);
460 goto failed_shutdown
;
463 r
= rbd_open(s
->io_ctx
, s
->name
, &s
->image
, s
->snap
);
465 error_report("error reading header from %s", s
->name
);
469 bs
->read_only
= (s
->snap
!= NULL
);
471 s
->event_reader_pos
= 0;
472 r
= qemu_pipe(s
->fds
);
474 error_report("error opening eventfd");
477 fcntl(s
->fds
[0], F_SETFL
, O_NONBLOCK
);
478 fcntl(s
->fds
[1], F_SETFL
, O_NONBLOCK
);
479 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], qemu_rbd_aio_event_reader
,
480 NULL
, qemu_rbd_aio_flush_cb
, NULL
, s
);
488 rados_ioctx_destroy(s
->io_ctx
);
490 rados_shutdown(s
->cluster
);
495 static void qemu_rbd_close(BlockDriverState
*bs
)
497 BDRVRBDState
*s
= bs
->opaque
;
501 qemu_aio_set_fd_handler(s
->fds
[RBD_FD_READ
], NULL
, NULL
, NULL
, NULL
,
505 rados_ioctx_destroy(s
->io_ctx
);
507 rados_shutdown(s
->cluster
);
511 * Cancel aio. Since we don't reference acb in a non qemu threads,
512 * it is safe to access it here.
514 static void qemu_rbd_aio_cancel(BlockDriverAIOCB
*blockacb
)
516 RBDAIOCB
*acb
= (RBDAIOCB
*) blockacb
;
520 static AIOPool rbd_aio_pool
= {
521 .aiocb_size
= sizeof(RBDAIOCB
),
522 .cancel
= qemu_rbd_aio_cancel
,
525 static int qemu_rbd_send_pipe(BDRVRBDState
*s
, RADOSCB
*rcb
)
530 int fd
= s
->fds
[RBD_FD_WRITE
];
532 /* send the op pointer to the qemu thread that is responsible
533 for the aio/op completion. Must do it in a qemu thread context */
534 ret
= write(fd
, (void *)&rcb
, sizeof(rcb
));
538 if (errno
== EINTR
) {
541 if (errno
!= EAGAIN
) {
548 ret
= select(fd
+ 1, NULL
, &wfd
, NULL
, NULL
);
549 } while (ret
< 0 && errno
== EINTR
);
556 * This is the callback function for rbd_aio_read and _write
558 * Note: this function is being called from a non qemu thread so
559 * we need to be careful about what we do here. Generally we only
560 * write to the block notification pipe, and do the rest of the
561 * io completion handling from qemu_rbd_aio_event_reader() which
562 * runs in a qemu context.
564 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
567 rcb
->ret
= rbd_aio_get_return_value(c
);
569 ret
= qemu_rbd_send_pipe(rcb
->s
, rcb
);
571 error_report("failed writing to acb->s->fds");
576 /* Callback when all queued rbd_aio requests are complete */
578 static void rbd_aio_bh_cb(void *opaque
)
580 RBDAIOCB
*acb
= opaque
;
583 qemu_iovec_from_buffer(acb
->qiov
, acb
->bounce
, acb
->qiov
->size
);
585 qemu_vfree(acb
->bounce
);
586 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
587 qemu_bh_delete(acb
->bh
);
590 qemu_aio_release(acb
);
593 static BlockDriverAIOCB
*rbd_aio_rw_vector(BlockDriverState
*bs
,
597 BlockDriverCompletionFunc
*cb
,
598 void *opaque
, int write
)
607 BDRVRBDState
*s
= bs
->opaque
;
609 acb
= qemu_aio_get(&rbd_aio_pool
, bs
, cb
, opaque
);
615 acb
->bounce
= qemu_blockalign(bs
, qiov
->size
);
623 qemu_iovec_to_buffer(acb
->qiov
, acb
->bounce
);
628 off
= sector_num
* BDRV_SECTOR_SIZE
;
629 size
= nb_sectors
* BDRV_SECTOR_SIZE
;
631 s
->qemu_aio_count
++; /* All the RADOSCB */
633 rcb
= g_malloc(sizeof(RADOSCB
));
639 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
645 r
= rbd_aio_write(s
->image
, off
, size
, buf
, c
);
647 r
= rbd_aio_read(s
->image
, off
, size
, buf
, c
);
659 qemu_aio_release(acb
);
663 static BlockDriverAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
667 BlockDriverCompletionFunc
*cb
,
670 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
673 static BlockDriverAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
677 BlockDriverCompletionFunc
*cb
,
680 return rbd_aio_rw_vector(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
683 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
685 BDRVRBDState
*s
= bs
->opaque
;
686 rbd_image_info_t info
;
689 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
694 bdi
->cluster_size
= info
.obj_size
;
698 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
700 BDRVRBDState
*s
= bs
->opaque
;
701 rbd_image_info_t info
;
704 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
712 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
)
714 BDRVRBDState
*s
= bs
->opaque
;
717 r
= rbd_resize(s
->image
, offset
);
725 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
726 QEMUSnapshotInfo
*sn_info
)
728 BDRVRBDState
*s
= bs
->opaque
;
731 if (sn_info
->name
[0] == '\0') {
732 return -EINVAL
; /* we need a name for rbd snapshots */
736 * rbd snapshots are using the name as the user controlled unique identifier
737 * we can't use the rbd snapid for that purpose, as it can't be set
739 if (sn_info
->id_str
[0] != '\0' &&
740 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
744 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
748 r
= rbd_snap_create(s
->image
, sn_info
->name
);
750 error_report("failed to create snap: %s", strerror(-r
));
757 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
758 QEMUSnapshotInfo
**psn_tab
)
760 BDRVRBDState
*s
= bs
->opaque
;
761 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
763 rbd_snap_info_t
*snaps
;
764 int max_snaps
= RBD_MAX_SNAPS
;
767 snaps
= g_malloc(sizeof(*snaps
) * max_snaps
);
768 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
769 if (snap_count
< 0) {
772 } while (snap_count
== -ERANGE
);
774 if (snap_count
<= 0) {
778 sn_tab
= g_malloc0(snap_count
* sizeof(QEMUSnapshotInfo
));
780 for (i
= 0; i
< snap_count
; i
++) {
781 const char *snap_name
= snaps
[i
].name
;
783 sn_info
= sn_tab
+ i
;
784 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
785 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
787 sn_info
->vm_state_size
= snaps
[i
].size
;
788 sn_info
->date_sec
= 0;
789 sn_info
->date_nsec
= 0;
790 sn_info
->vm_clock_nsec
= 0;
792 rbd_snap_list_end(snaps
);
798 static QEMUOptionParameter qemu_rbd_create_options
[] = {
800 .name
= BLOCK_OPT_SIZE
,
802 .help
= "Virtual disk size"
805 .name
= BLOCK_OPT_CLUSTER_SIZE
,
807 .help
= "RBD object size"
812 static BlockDriver bdrv_rbd
= {
813 .format_name
= "rbd",
814 .instance_size
= sizeof(BDRVRBDState
),
815 .bdrv_file_open
= qemu_rbd_open
,
816 .bdrv_close
= qemu_rbd_close
,
817 .bdrv_create
= qemu_rbd_create
,
818 .bdrv_get_info
= qemu_rbd_getinfo
,
819 .create_options
= qemu_rbd_create_options
,
820 .bdrv_getlength
= qemu_rbd_getlength
,
821 .bdrv_truncate
= qemu_rbd_truncate
,
822 .protocol_name
= "rbd",
824 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
825 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
827 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
828 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
831 static void bdrv_rbd_init(void)
833 bdrv_register(&bdrv_rbd
);
836 block_init(bdrv_rbd_init
);