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.
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.
14 #include "qemu/osdep.h"
16 #include <rbd/librbd.h>
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "block/block_int.h"
20 #include "crypto/secret.h"
21 #include "qemu/cutils.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qdict.h"
24 #include "qapi/qmp/qjson.h"
25 #include "qapi/qmp/qlist.h"
28 * When specifying the image filename use:
30 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
32 * poolname must be the name of an existing rados pool.
34 * devicename is the name of the rbd image.
36 * Each option given is used to configure rados, and may be any valid
37 * Ceph option, "id", or "conf".
39 * The "id" option indicates what user we should authenticate as to
40 * the Ceph cluster. If it is excluded we will use the Ceph default
43 * The "conf" option specifies a Ceph configuration file to read. If
44 * it is not specified, we will read from the default Ceph locations
45 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
46 * file, specify conf=/dev/null.
48 * Configuration values containing :, @, or = can be escaped with a
52 /* rbd_aio_discard added in 0.1.2 */
53 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
54 #define LIBRBD_SUPPORTS_DISCARD
56 #undef LIBRBD_SUPPORTS_DISCARD
59 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
61 #define RBD_MAX_SNAPS 100
63 /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
64 #ifdef LIBRBD_SUPPORTS_IOVEC
65 #define LIBRBD_USE_IOVEC 1
67 #define LIBRBD_USE_IOVEC 0
77 typedef struct RBDAIOCB
{
84 struct BDRVRBDState
*s
;
87 typedef struct RADOSCB
{
89 struct BDRVRBDState
*s
;
95 typedef struct BDRVRBDState
{
103 static char *qemu_rbd_next_tok(char *src
, char delim
, char **p
)
109 for (end
= src
; *end
; ++end
) {
113 if (*end
== '\\' && end
[1] != '\0') {
124 static void qemu_rbd_unescape(char *src
)
128 for (p
= src
; *src
; ++src
, ++p
) {
129 if (*src
== '\\' && src
[1] != '\0') {
137 static void qemu_rbd_parse_filename(const char *filename
, QDict
*options
,
142 QList
*keypairs
= NULL
;
145 if (!strstart(filename
, "rbd:", &start
)) {
146 error_setg(errp
, "File name must start with 'rbd:'");
150 buf
= g_strdup(start
);
153 found_str
= qemu_rbd_next_tok(p
, '/', &p
);
155 error_setg(errp
, "Pool name is required");
158 qemu_rbd_unescape(found_str
);
159 qdict_put_str(options
, "pool", found_str
);
161 if (strchr(p
, '@')) {
162 found_str
= qemu_rbd_next_tok(p
, '@', &p
);
163 qemu_rbd_unescape(found_str
);
164 qdict_put_str(options
, "image", found_str
);
166 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
167 qemu_rbd_unescape(found_str
);
168 qdict_put_str(options
, "snapshot", found_str
);
170 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
171 qemu_rbd_unescape(found_str
);
172 qdict_put_str(options
, "image", found_str
);
178 /* The following are essentially all key/value pairs, and we treat
179 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
182 name
= qemu_rbd_next_tok(p
, '=', &p
);
184 error_setg(errp
, "conf option %s has no value", name
);
188 qemu_rbd_unescape(name
);
190 value
= qemu_rbd_next_tok(p
, ':', &p
);
191 qemu_rbd_unescape(value
);
193 if (!strcmp(name
, "conf")) {
194 qdict_put_str(options
, "conf", value
);
195 } else if (!strcmp(name
, "id")) {
196 qdict_put_str(options
, "user", value
);
199 * We pass these internally to qemu_rbd_set_keypairs(), so
200 * we can get away with the simpler list of [ "key1",
201 * "value1", "key2", "value2" ] rather than a raw dict
202 * { "key1": "value1", "key2": "value2" } where we can't
203 * guarantee order, or even a more correct but complex
204 * [ { "key1": "value1" }, { "key2": "value2" } ]
207 keypairs
= qlist_new();
209 qlist_append_str(keypairs
, name
);
210 qlist_append_str(keypairs
, value
);
215 qdict_put(options
, "=keyvalue-pairs",
216 qobject_to_json(QOBJECT(keypairs
)));
226 static int qemu_rbd_set_auth(rados_t cluster
, const char *secretid
,
233 gchar
*secret
= qcrypto_secret_lookup_as_base64(secretid
,
239 rados_conf_set(cluster
, "key", secret
);
245 static int qemu_rbd_set_keypairs(rados_t cluster
, const char *keypairs_json
,
255 if (!keypairs_json
) {
258 keypairs
= qobject_to_qlist(qobject_from_json(keypairs_json
,
260 remaining
= qlist_size(keypairs
) / 2;
263 while (remaining
--) {
264 name
= qobject_to_qstring(qlist_pop(keypairs
));
265 value
= qobject_to_qstring(qlist_pop(keypairs
));
266 assert(name
&& value
);
267 key
= qstring_get_str(name
);
269 ret
= rados_conf_set(cluster
, key
, qstring_get_str(value
));
273 error_setg_errno(errp
, -ret
, "invalid conf option %s", key
);
283 static void qemu_rbd_memset(RADOSCB
*rcb
, int64_t offs
)
285 if (LIBRBD_USE_IOVEC
) {
286 RBDAIOCB
*acb
= rcb
->acb
;
287 iov_memset(acb
->qiov
->iov
, acb
->qiov
->niov
, offs
, 0,
288 acb
->qiov
->size
- offs
);
290 memset(rcb
->buf
+ offs
, 0, rcb
->size
- offs
);
294 static QemuOptsList runtime_opts
= {
296 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
300 .type
= QEMU_OPT_STRING
,
301 .help
= "Rados pool name",
305 .type
= QEMU_OPT_STRING
,
306 .help
= "Image name in the pool",
310 .type
= QEMU_OPT_STRING
,
311 .help
= "Rados config file location",
315 .type
= QEMU_OPT_STRING
,
316 .help
= "Ceph snapshot name",
319 /* maps to 'id' in rados_create() */
321 .type
= QEMU_OPT_STRING
,
322 .help
= "Rados id name",
325 * server.* extracted manually, see qemu_rbd_mon_host()
328 .name
= "password-secret",
329 .type
= QEMU_OPT_STRING
,
330 .help
= "ID of secret providing the password",
334 * Keys for qemu_rbd_parse_filename(), not in the QAPI schema
338 * HACK: name starts with '=' so that qemu_opts_parse()
341 .name
= "=keyvalue-pairs",
342 .type
= QEMU_OPT_STRING
,
343 .help
= "Legacy rados key/value option parameters",
347 .type
= QEMU_OPT_STRING
,
349 { /* end of list */ }
353 static int qemu_rbd_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
355 Error
*local_err
= NULL
;
359 const char *pool
, *image_name
, *conf
, *user
, *keypairs
;
360 const char *secretid
;
362 rados_ioctx_t io_ctx
;
363 QDict
*options
= NULL
;
366 secretid
= qemu_opt_get(opts
, "password-secret");
368 /* Read out options */
369 bytes
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
371 objsize
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
, 0);
373 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
374 error_setg(errp
, "obj size needs to be power of 2");
378 if (objsize
< 4096) {
379 error_setg(errp
, "obj size too small");
383 obj_order
= ctz32(objsize
);
386 options
= qdict_new();
387 qemu_rbd_parse_filename(filename
, options
, &local_err
);
390 error_propagate(errp
, local_err
);
395 * Caution: while qdict_get_try_str() is fine, getting non-string
396 * types would require more care. When @options come from -blockdev
397 * or blockdev_add, its members are typed according to the QAPI
398 * schema, but when they come from -drive, they're all QString.
400 pool
= qdict_get_try_str(options
, "pool");
401 conf
= qdict_get_try_str(options
, "conf");
402 user
= qdict_get_try_str(options
, "user");
403 image_name
= qdict_get_try_str(options
, "image");
404 keypairs
= qdict_get_try_str(options
, "=keyvalue-pairs");
406 ret
= rados_create(&cluster
, user
);
408 error_setg_errno(errp
, -ret
, "error initializing");
412 /* try default location when conf=NULL, but ignore failure */
413 ret
= rados_conf_read_file(cluster
, conf
);
414 if (conf
&& ret
< 0) {
415 error_setg_errno(errp
, -ret
, "error reading conf file %s", conf
);
420 ret
= qemu_rbd_set_keypairs(cluster
, keypairs
, errp
);
426 if (qemu_rbd_set_auth(cluster
, secretid
, errp
) < 0) {
431 ret
= rados_connect(cluster
);
433 error_setg_errno(errp
, -ret
, "error connecting");
437 ret
= rados_ioctx_create(cluster
, pool
, &io_ctx
);
439 error_setg_errno(errp
, -ret
, "error opening pool %s", pool
);
443 ret
= rbd_create(io_ctx
, image_name
, bytes
, &obj_order
);
445 error_setg_errno(errp
, -ret
, "error rbd create");
448 rados_ioctx_destroy(io_ctx
);
451 rados_shutdown(cluster
);
459 * This aio completion is being called from rbd_finish_bh() and runs in qemu
462 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
464 RBDAIOCB
*acb
= rcb
->acb
;
469 if (acb
->cmd
!= RBD_AIO_READ
) {
473 } else if (!acb
->error
) {
474 acb
->ret
= rcb
->size
;
478 qemu_rbd_memset(rcb
, 0);
481 } else if (r
< rcb
->size
) {
482 qemu_rbd_memset(rcb
, r
);
484 acb
->ret
= rcb
->size
;
486 } else if (!acb
->error
) {
493 if (!LIBRBD_USE_IOVEC
) {
494 if (acb
->cmd
== RBD_AIO_READ
) {
495 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
497 qemu_vfree(acb
->bounce
);
500 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
505 static char *qemu_rbd_mon_host(QDict
*options
, Error
**errp
)
507 const char **vals
= g_new(const char *, qdict_size(options
) + 1);
509 const char *host
, *port
;
514 sprintf(keybuf
, "server.%d.host", i
);
515 host
= qdict_get_try_str(options
, keybuf
);
516 qdict_del(options
, keybuf
);
517 sprintf(keybuf
, "server.%d.port", i
);
518 port
= qdict_get_try_str(options
, keybuf
);
519 qdict_del(options
, keybuf
);
520 if (!host
&& !port
) {
524 error_setg(errp
, "Parameter server.%d.host is missing", i
);
529 if (strchr(host
, ':')) {
530 vals
[i
] = port
? g_strdup_printf("[%s]:%s", host
, port
)
531 : g_strdup_printf("[%s]", host
);
533 vals
[i
] = port
? g_strdup_printf("%s:%s", host
, port
)
539 rados_str
= i
? g_strjoinv(";", (char **)vals
) : NULL
;
541 g_strfreev((char **)vals
);
545 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
548 BDRVRBDState
*s
= bs
->opaque
;
549 const char *pool
, *snap
, *conf
, *user
, *image_name
, *keypairs
;
550 const char *secretid
, *filename
;
552 Error
*local_err
= NULL
;
553 char *mon_host
= NULL
;
556 /* If we are given a filename, parse the filename, with precedence given to
557 * filename encoded options */
558 filename
= qdict_get_try_str(options
, "filename");
560 warn_report("'filename' option specified. "
561 "This is an unsupported option, and may be deprecated "
563 qemu_rbd_parse_filename(filename
, options
, &local_err
);
566 error_propagate(errp
, local_err
);
571 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
572 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
574 error_propagate(errp
, local_err
);
579 mon_host
= qemu_rbd_mon_host(options
, &local_err
);
581 error_propagate(errp
, local_err
);
586 secretid
= qemu_opt_get(opts
, "password-secret");
588 pool
= qemu_opt_get(opts
, "pool");
589 conf
= qemu_opt_get(opts
, "conf");
590 snap
= qemu_opt_get(opts
, "snapshot");
591 user
= qemu_opt_get(opts
, "user");
592 image_name
= qemu_opt_get(opts
, "image");
593 keypairs
= qemu_opt_get(opts
, "=keyvalue-pairs");
595 if (!pool
|| !image_name
) {
596 error_setg(errp
, "Parameters 'pool' and 'image' are required");
601 r
= rados_create(&s
->cluster
, user
);
603 error_setg_errno(errp
, -r
, "error initializing");
607 s
->snap
= g_strdup(snap
);
608 s
->image_name
= g_strdup(image_name
);
610 /* try default location when conf=NULL, but ignore failure */
611 r
= rados_conf_read_file(s
->cluster
, conf
);
613 error_setg_errno(errp
, -r
, "error reading conf file %s", conf
);
614 goto failed_shutdown
;
617 r
= qemu_rbd_set_keypairs(s
->cluster
, keypairs
, errp
);
619 goto failed_shutdown
;
623 r
= rados_conf_set(s
->cluster
, "mon_host", mon_host
);
625 goto failed_shutdown
;
629 if (qemu_rbd_set_auth(s
->cluster
, secretid
, errp
) < 0) {
631 goto failed_shutdown
;
635 * Fallback to more conservative semantics if setting cache
636 * options fails. Ignore errors from setting rbd_cache because the
637 * only possible error is that the option does not exist, and
638 * librbd defaults to no caching. If write through caching cannot
639 * be set up, fall back to no caching.
641 if (flags
& BDRV_O_NOCACHE
) {
642 rados_conf_set(s
->cluster
, "rbd_cache", "false");
644 rados_conf_set(s
->cluster
, "rbd_cache", "true");
647 r
= rados_connect(s
->cluster
);
649 error_setg_errno(errp
, -r
, "error connecting");
650 goto failed_shutdown
;
653 r
= rados_ioctx_create(s
->cluster
, pool
, &s
->io_ctx
);
655 error_setg_errno(errp
, -r
, "error opening pool %s", pool
);
656 goto failed_shutdown
;
659 /* rbd_open is always r/w */
660 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
662 error_setg_errno(errp
, -r
, "error reading header from %s",
667 /* If we are using an rbd snapshot, we must be r/o, otherwise
669 if (s
->snap
!= NULL
) {
670 if (!bdrv_is_read_only(bs
)) {
671 error_report("Opening rbd snapshots without an explicit "
672 "read-only=on option is deprecated. Future versions "
673 "will refuse to open the image instead of "
674 "automatically marking the image read-only.");
675 r
= bdrv_set_read_only(bs
, true, &local_err
);
677 error_propagate(errp
, local_err
);
687 rados_ioctx_destroy(s
->io_ctx
);
689 rados_shutdown(s
->cluster
);
691 g_free(s
->image_name
);
700 /* Since RBD is currently always opened R/W via the API,
701 * we just need to check if we are using a snapshot or not, in
702 * order to determine if we will allow it to be R/W */
703 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
704 BlockReopenQueue
*queue
, Error
**errp
)
706 BDRVRBDState
*s
= state
->bs
->opaque
;
709 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
711 "Cannot change node '%s' to r/w when using RBD snapshot",
712 bdrv_get_device_or_node_name(state
->bs
));
719 static void qemu_rbd_close(BlockDriverState
*bs
)
721 BDRVRBDState
*s
= bs
->opaque
;
724 rados_ioctx_destroy(s
->io_ctx
);
726 g_free(s
->image_name
);
727 rados_shutdown(s
->cluster
);
730 static const AIOCBInfo rbd_aiocb_info
= {
731 .aiocb_size
= sizeof(RBDAIOCB
),
734 static void rbd_finish_bh(void *opaque
)
736 RADOSCB
*rcb
= opaque
;
737 qemu_rbd_complete_aio(rcb
);
741 * This is the callback function for rbd_aio_read and _write
743 * Note: this function is being called from a non qemu thread so
744 * we need to be careful about what we do here. Generally we only
745 * schedule a BH, and do the rest of the io completion handling
746 * from rbd_finish_bh() which runs in a qemu context.
748 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
750 RBDAIOCB
*acb
= rcb
->acb
;
752 rcb
->ret
= rbd_aio_get_return_value(c
);
755 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
759 static int rbd_aio_discard_wrapper(rbd_image_t image
,
762 rbd_completion_t comp
)
764 #ifdef LIBRBD_SUPPORTS_DISCARD
765 return rbd_aio_discard(image
, off
, len
, comp
);
771 static int rbd_aio_flush_wrapper(rbd_image_t image
,
772 rbd_completion_t comp
)
774 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
775 return rbd_aio_flush(image
, comp
);
781 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
785 BlockCompletionFunc
*cb
,
794 BDRVRBDState
*s
= bs
->opaque
;
796 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
799 assert(!qiov
|| qiov
->size
== size
);
801 rcb
= g_new(RADOSCB
, 1);
803 if (!LIBRBD_USE_IOVEC
) {
804 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
807 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
808 if (acb
->bounce
== NULL
) {
812 if (cmd
== RBD_AIO_WRITE
) {
813 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
815 rcb
->buf
= acb
->bounce
;
825 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
832 #ifdef LIBRBD_SUPPORTS_IOVEC
833 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
835 r
= rbd_aio_write(s
->image
, off
, size
, rcb
->buf
, c
);
839 #ifdef LIBRBD_SUPPORTS_IOVEC
840 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
842 r
= rbd_aio_read(s
->image
, off
, size
, rcb
->buf
, c
);
845 case RBD_AIO_DISCARD
:
846 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
849 r
= rbd_aio_flush_wrapper(s
->image
, c
);
856 goto failed_completion
;
864 if (!LIBRBD_USE_IOVEC
) {
865 qemu_vfree(acb
->bounce
);
872 static BlockAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
876 BlockCompletionFunc
*cb
,
879 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
880 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
884 static BlockAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
888 BlockCompletionFunc
*cb
,
891 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
892 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
896 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
897 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
898 BlockCompletionFunc
*cb
,
901 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
906 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
908 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
909 /* rbd_flush added in 0.1.1 */
910 BDRVRBDState
*s
= bs
->opaque
;
911 return rbd_flush(s
->image
);
918 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
920 BDRVRBDState
*s
= bs
->opaque
;
921 rbd_image_info_t info
;
924 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
929 bdi
->cluster_size
= info
.obj_size
;
933 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
935 BDRVRBDState
*s
= bs
->opaque
;
936 rbd_image_info_t info
;
939 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
947 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
,
948 PreallocMode prealloc
, Error
**errp
)
950 BDRVRBDState
*s
= bs
->opaque
;
953 if (prealloc
!= PREALLOC_MODE_OFF
) {
954 error_setg(errp
, "Unsupported preallocation mode '%s'",
955 PreallocMode_str(prealloc
));
959 r
= rbd_resize(s
->image
, offset
);
961 error_setg_errno(errp
, -r
, "Failed to resize file");
968 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
969 QEMUSnapshotInfo
*sn_info
)
971 BDRVRBDState
*s
= bs
->opaque
;
974 if (sn_info
->name
[0] == '\0') {
975 return -EINVAL
; /* we need a name for rbd snapshots */
979 * rbd snapshots are using the name as the user controlled unique identifier
980 * we can't use the rbd snapid for that purpose, as it can't be set
982 if (sn_info
->id_str
[0] != '\0' &&
983 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
987 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
991 r
= rbd_snap_create(s
->image
, sn_info
->name
);
993 error_report("failed to create snap: %s", strerror(-r
));
1000 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1001 const char *snapshot_id
,
1002 const char *snapshot_name
,
1005 BDRVRBDState
*s
= bs
->opaque
;
1008 if (!snapshot_name
) {
1009 error_setg(errp
, "rbd need a valid snapshot name");
1013 /* If snapshot_id is specified, it must be equal to name, see
1014 qemu_rbd_snap_list() */
1015 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1017 "rbd do not support snapshot id, it should be NULL or "
1018 "equal to snapshot name");
1022 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1024 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1029 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1030 const char *snapshot_name
)
1032 BDRVRBDState
*s
= bs
->opaque
;
1034 return rbd_snap_rollback(s
->image
, snapshot_name
);
1037 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1038 QEMUSnapshotInfo
**psn_tab
)
1040 BDRVRBDState
*s
= bs
->opaque
;
1041 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1043 rbd_snap_info_t
*snaps
;
1044 int max_snaps
= RBD_MAX_SNAPS
;
1047 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1048 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1049 if (snap_count
<= 0) {
1052 } while (snap_count
== -ERANGE
);
1054 if (snap_count
<= 0) {
1058 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1060 for (i
= 0; i
< snap_count
; i
++) {
1061 const char *snap_name
= snaps
[i
].name
;
1063 sn_info
= sn_tab
+ i
;
1064 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1065 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1067 sn_info
->vm_state_size
= snaps
[i
].size
;
1068 sn_info
->date_sec
= 0;
1069 sn_info
->date_nsec
= 0;
1070 sn_info
->vm_clock_nsec
= 0;
1072 rbd_snap_list_end(snaps
);
1080 #ifdef LIBRBD_SUPPORTS_DISCARD
1081 static BlockAIOCB
*qemu_rbd_aio_pdiscard(BlockDriverState
*bs
,
1084 BlockCompletionFunc
*cb
,
1087 return rbd_start_aio(bs
, offset
, NULL
, bytes
, cb
, opaque
,
1092 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1093 static void qemu_rbd_invalidate_cache(BlockDriverState
*bs
,
1096 BDRVRBDState
*s
= bs
->opaque
;
1097 int r
= rbd_invalidate_cache(s
->image
);
1099 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1104 static QemuOptsList qemu_rbd_create_opts
= {
1105 .name
= "rbd-create-opts",
1106 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1109 .name
= BLOCK_OPT_SIZE
,
1110 .type
= QEMU_OPT_SIZE
,
1111 .help
= "Virtual disk size"
1114 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1115 .type
= QEMU_OPT_SIZE
,
1116 .help
= "RBD object size"
1119 .name
= "password-secret",
1120 .type
= QEMU_OPT_STRING
,
1121 .help
= "ID of secret providing the password",
1123 { /* end of list */ }
1127 static BlockDriver bdrv_rbd
= {
1128 .format_name
= "rbd",
1129 .instance_size
= sizeof(BDRVRBDState
),
1130 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1131 .bdrv_file_open
= qemu_rbd_open
,
1132 .bdrv_close
= qemu_rbd_close
,
1133 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1134 .bdrv_create
= qemu_rbd_create
,
1135 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1136 .bdrv_get_info
= qemu_rbd_getinfo
,
1137 .create_opts
= &qemu_rbd_create_opts
,
1138 .bdrv_getlength
= qemu_rbd_getlength
,
1139 .bdrv_truncate
= qemu_rbd_truncate
,
1140 .protocol_name
= "rbd",
1142 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
1143 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
1145 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1146 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
1148 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1151 #ifdef LIBRBD_SUPPORTS_DISCARD
1152 .bdrv_aio_pdiscard
= qemu_rbd_aio_pdiscard
,
1155 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1156 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1157 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1158 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1159 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1160 .bdrv_invalidate_cache
= qemu_rbd_invalidate_cache
,
1164 static void bdrv_rbd_init(void)
1166 bdrv_register(&bdrv_rbd
);
1169 block_init(bdrv_rbd_init
);