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 "qemu/option.h"
20 #include "block/block_int.h"
21 #include "crypto/secret.h"
22 #include "qemu/cutils.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp/qdict.h"
25 #include "qapi/qmp/qjson.h"
26 #include "qapi/qmp/qlist.h"
27 #include "qapi/qobject-input-visitor.h"
28 #include "qapi/qapi-visit-block-core.h"
31 * When specifying the image filename use:
33 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
35 * poolname must be the name of an existing rados pool.
37 * devicename is the name of the rbd image.
39 * Each option given is used to configure rados, and may be any valid
40 * Ceph option, "id", or "conf".
42 * The "id" option indicates what user we should authenticate as to
43 * the Ceph cluster. If it is excluded we will use the Ceph default
46 * The "conf" option specifies a Ceph configuration file to read. If
47 * it is not specified, we will read from the default Ceph locations
48 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
49 * file, specify conf=/dev/null.
51 * Configuration values containing :, @, or = can be escaped with a
55 /* rbd_aio_discard added in 0.1.2 */
56 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
57 #define LIBRBD_SUPPORTS_DISCARD
59 #undef LIBRBD_SUPPORTS_DISCARD
62 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
64 #define RBD_MAX_SNAPS 100
66 /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
67 #ifdef LIBRBD_SUPPORTS_IOVEC
68 #define LIBRBD_USE_IOVEC 1
70 #define LIBRBD_USE_IOVEC 0
80 typedef struct RBDAIOCB
{
87 struct BDRVRBDState
*s
;
90 typedef struct RADOSCB
{
92 struct BDRVRBDState
*s
;
98 typedef struct BDRVRBDState
{
100 rados_ioctx_t io_ctx
;
106 static char *qemu_rbd_next_tok(char *src
, char delim
, char **p
)
112 for (end
= src
; *end
; ++end
) {
116 if (*end
== '\\' && end
[1] != '\0') {
127 static void qemu_rbd_unescape(char *src
)
131 for (p
= src
; *src
; ++src
, ++p
) {
132 if (*src
== '\\' && src
[1] != '\0') {
140 static void qemu_rbd_parse_filename(const char *filename
, QDict
*options
,
145 QList
*keypairs
= NULL
;
148 if (!strstart(filename
, "rbd:", &start
)) {
149 error_setg(errp
, "File name must start with 'rbd:'");
153 buf
= g_strdup(start
);
156 found_str
= qemu_rbd_next_tok(p
, '/', &p
);
158 error_setg(errp
, "Pool name is required");
161 qemu_rbd_unescape(found_str
);
162 qdict_put_str(options
, "pool", found_str
);
164 if (strchr(p
, '@')) {
165 found_str
= qemu_rbd_next_tok(p
, '@', &p
);
166 qemu_rbd_unescape(found_str
);
167 qdict_put_str(options
, "image", found_str
);
169 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
170 qemu_rbd_unescape(found_str
);
171 qdict_put_str(options
, "snapshot", found_str
);
173 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
174 qemu_rbd_unescape(found_str
);
175 qdict_put_str(options
, "image", found_str
);
181 /* The following are essentially all key/value pairs, and we treat
182 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
185 name
= qemu_rbd_next_tok(p
, '=', &p
);
187 error_setg(errp
, "conf option %s has no value", name
);
191 qemu_rbd_unescape(name
);
193 value
= qemu_rbd_next_tok(p
, ':', &p
);
194 qemu_rbd_unescape(value
);
196 if (!strcmp(name
, "conf")) {
197 qdict_put_str(options
, "conf", value
);
198 } else if (!strcmp(name
, "id")) {
199 qdict_put_str(options
, "user", value
);
202 * We pass these internally to qemu_rbd_set_keypairs(), so
203 * we can get away with the simpler list of [ "key1",
204 * "value1", "key2", "value2" ] rather than a raw dict
205 * { "key1": "value1", "key2": "value2" } where we can't
206 * guarantee order, or even a more correct but complex
207 * [ { "key1": "value1" }, { "key2": "value2" } ]
210 keypairs
= qlist_new();
212 qlist_append_str(keypairs
, name
);
213 qlist_append_str(keypairs
, value
);
218 qdict_put(options
, "=keyvalue-pairs",
219 qobject_to_json(QOBJECT(keypairs
)));
229 static int qemu_rbd_set_auth(rados_t cluster
, const char *secretid
,
236 gchar
*secret
= qcrypto_secret_lookup_as_base64(secretid
,
242 rados_conf_set(cluster
, "key", secret
);
248 static int qemu_rbd_set_keypairs(rados_t cluster
, const char *keypairs_json
,
258 if (!keypairs_json
) {
261 keypairs
= qobject_to_qlist(qobject_from_json(keypairs_json
,
263 remaining
= qlist_size(keypairs
) / 2;
266 while (remaining
--) {
267 name
= qobject_to_qstring(qlist_pop(keypairs
));
268 value
= qobject_to_qstring(qlist_pop(keypairs
));
269 assert(name
&& value
);
270 key
= qstring_get_str(name
);
272 ret
= rados_conf_set(cluster
, key
, qstring_get_str(value
));
275 error_setg_errno(errp
, -ret
, "invalid conf option %s", key
);
287 static void qemu_rbd_memset(RADOSCB
*rcb
, int64_t offs
)
289 if (LIBRBD_USE_IOVEC
) {
290 RBDAIOCB
*acb
= rcb
->acb
;
291 iov_memset(acb
->qiov
->iov
, acb
->qiov
->niov
, offs
, 0,
292 acb
->qiov
->size
- offs
);
294 memset(rcb
->buf
+ offs
, 0, rcb
->size
- offs
);
298 static QemuOptsList runtime_opts
= {
300 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
304 .type
= QEMU_OPT_STRING
,
305 .help
= "Rados pool name",
309 .type
= QEMU_OPT_STRING
,
310 .help
= "Image name in the pool",
314 .type
= QEMU_OPT_STRING
,
315 .help
= "Rados config file location",
319 .type
= QEMU_OPT_STRING
,
320 .help
= "Ceph snapshot name",
323 /* maps to 'id' in rados_create() */
325 .type
= QEMU_OPT_STRING
,
326 .help
= "Rados id name",
329 * server.* extracted manually, see qemu_rbd_mon_host()
331 { /* end of list */ }
335 static int coroutine_fn
qemu_rbd_co_create_opts(const char *filename
,
339 Error
*local_err
= NULL
;
343 const char *pool
, *image_name
, *conf
, *user
, *keypairs
;
344 const char *secretid
;
346 rados_ioctx_t io_ctx
;
347 QDict
*options
= NULL
;
350 secretid
= qemu_opt_get(opts
, "password-secret");
352 /* Read out options */
353 bytes
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
355 objsize
= qemu_opt_get_size_del(opts
, BLOCK_OPT_CLUSTER_SIZE
, 0);
357 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
358 error_setg(errp
, "obj size needs to be power of 2");
362 if (objsize
< 4096) {
363 error_setg(errp
, "obj size too small");
367 obj_order
= ctz32(objsize
);
370 options
= qdict_new();
371 qemu_rbd_parse_filename(filename
, options
, &local_err
);
374 error_propagate(errp
, local_err
);
379 * Caution: while qdict_get_try_str() is fine, getting non-string
380 * types would require more care. When @options come from -blockdev
381 * or blockdev_add, its members are typed according to the QAPI
382 * schema, but when they come from -drive, they're all QString.
384 pool
= qdict_get_try_str(options
, "pool");
385 conf
= qdict_get_try_str(options
, "conf");
386 user
= qdict_get_try_str(options
, "user");
387 image_name
= qdict_get_try_str(options
, "image");
388 keypairs
= qdict_get_try_str(options
, "=keyvalue-pairs");
390 ret
= rados_create(&cluster
, user
);
392 error_setg_errno(errp
, -ret
, "error initializing");
396 /* try default location when conf=NULL, but ignore failure */
397 ret
= rados_conf_read_file(cluster
, conf
);
398 if (conf
&& ret
< 0) {
399 error_setg_errno(errp
, -ret
, "error reading conf file %s", conf
);
404 ret
= qemu_rbd_set_keypairs(cluster
, keypairs
, errp
);
410 if (qemu_rbd_set_auth(cluster
, secretid
, errp
) < 0) {
415 ret
= rados_connect(cluster
);
417 error_setg_errno(errp
, -ret
, "error connecting");
421 ret
= rados_ioctx_create(cluster
, pool
, &io_ctx
);
423 error_setg_errno(errp
, -ret
, "error opening pool %s", pool
);
427 ret
= rbd_create(io_ctx
, image_name
, bytes
, &obj_order
);
429 error_setg_errno(errp
, -ret
, "error rbd create");
432 rados_ioctx_destroy(io_ctx
);
435 rados_shutdown(cluster
);
443 * This aio completion is being called from rbd_finish_bh() and runs in qemu
446 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
448 RBDAIOCB
*acb
= rcb
->acb
;
453 if (acb
->cmd
!= RBD_AIO_READ
) {
457 } else if (!acb
->error
) {
458 acb
->ret
= rcb
->size
;
462 qemu_rbd_memset(rcb
, 0);
465 } else if (r
< rcb
->size
) {
466 qemu_rbd_memset(rcb
, r
);
468 acb
->ret
= rcb
->size
;
470 } else if (!acb
->error
) {
477 if (!LIBRBD_USE_IOVEC
) {
478 if (acb
->cmd
== RBD_AIO_READ
) {
479 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
481 qemu_vfree(acb
->bounce
);
484 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
489 static char *qemu_rbd_mon_host(BlockdevOptionsRbd
*opts
, Error
**errp
)
492 const char *host
, *port
;
494 InetSocketAddressBaseList
*p
;
497 if (!opts
->has_server
) {
501 for (cnt
= 0, p
= opts
->server
; p
; p
= p
->next
) {
505 vals
= g_new(const char *, cnt
+ 1);
507 for (i
= 0, p
= opts
->server
; p
; p
= p
->next
, i
++) {
508 host
= p
->value
->host
;
509 port
= p
->value
->port
;
511 if (strchr(host
, ':')) {
512 vals
[i
] = g_strdup_printf("[%s]:%s", host
, port
);
514 vals
[i
] = g_strdup_printf("%s:%s", host
, port
);
519 rados_str
= i
? g_strjoinv(";", (char **)vals
) : NULL
;
520 g_strfreev((char **)vals
);
524 static int qemu_rbd_connect(rados_t
*cluster
, rados_ioctx_t
*io_ctx
,
525 char **s_snap
, char **s_image_name
,
526 BlockdevOptionsRbd
*opts
, bool cache
,
527 const char *keypairs
, const char *secretid
,
530 char *mon_host
= NULL
;
531 Error
*local_err
= NULL
;
534 mon_host
= qemu_rbd_mon_host(opts
, &local_err
);
536 error_propagate(errp
, local_err
);
541 r
= rados_create(cluster
, opts
->user
);
543 error_setg_errno(errp
, -r
, "error initializing");
547 *s_snap
= g_strdup(opts
->snapshot
);
548 *s_image_name
= g_strdup(opts
->image
);
550 /* try default location when conf=NULL, but ignore failure */
551 r
= rados_conf_read_file(*cluster
, opts
->conf
);
552 if (opts
->has_conf
&& r
< 0) {
553 error_setg_errno(errp
, -r
, "error reading conf file %s", opts
->conf
);
554 goto failed_shutdown
;
557 r
= qemu_rbd_set_keypairs(*cluster
, keypairs
, errp
);
559 goto failed_shutdown
;
563 r
= rados_conf_set(*cluster
, "mon_host", mon_host
);
565 goto failed_shutdown
;
569 if (qemu_rbd_set_auth(*cluster
, secretid
, errp
) < 0) {
571 goto failed_shutdown
;
575 * Fallback to more conservative semantics if setting cache
576 * options fails. Ignore errors from setting rbd_cache because the
577 * only possible error is that the option does not exist, and
578 * librbd defaults to no caching. If write through caching cannot
579 * be set up, fall back to no caching.
582 rados_conf_set(*cluster
, "rbd_cache", "true");
584 rados_conf_set(*cluster
, "rbd_cache", "false");
587 r
= rados_connect(*cluster
);
589 error_setg_errno(errp
, -r
, "error connecting");
590 goto failed_shutdown
;
593 r
= rados_ioctx_create(*cluster
, opts
->pool
, io_ctx
);
595 error_setg_errno(errp
, -r
, "error opening pool %s", opts
->pool
);
596 goto failed_shutdown
;
602 rados_shutdown(*cluster
);
604 g_free(*s_image_name
);
610 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
613 BDRVRBDState
*s
= bs
->opaque
;
614 BlockdevOptionsRbd
*opts
= NULL
;
616 QObject
*crumpled
= NULL
;
617 Error
*local_err
= NULL
;
618 const char *filename
;
619 char *keypairs
, *secretid
;
622 /* If we are given a filename, parse the filename, with precedence given to
623 * filename encoded options */
624 filename
= qdict_get_try_str(options
, "filename");
626 warn_report("'filename' option specified. "
627 "This is an unsupported option, and may be deprecated "
629 qemu_rbd_parse_filename(filename
, options
, &local_err
);
630 qdict_del(options
, "filename");
632 error_propagate(errp
, local_err
);
637 keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
639 qdict_del(options
, "=keyvalue-pairs");
642 secretid
= g_strdup(qdict_get_try_str(options
, "password-secret"));
644 qdict_del(options
, "password-secret");
647 /* Convert the remaining options into a QAPI object */
648 crumpled
= qdict_crumple(options
, errp
);
649 if (crumpled
== NULL
) {
654 v
= qobject_input_visitor_new_keyval(crumpled
);
655 visit_type_BlockdevOptionsRbd(v
, NULL
, &opts
, &local_err
);
657 qobject_decref(crumpled
);
660 error_propagate(errp
, local_err
);
665 r
= qemu_rbd_connect(&s
->cluster
, &s
->io_ctx
, &s
->snap
, &s
->image_name
,
666 opts
, !(flags
& BDRV_O_NOCACHE
), keypairs
, secretid
,
672 /* rbd_open is always r/w */
673 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
675 error_setg_errno(errp
, -r
, "error reading header from %s",
680 /* If we are using an rbd snapshot, we must be r/o, otherwise
682 if (s
->snap
!= NULL
) {
683 if (!bdrv_is_read_only(bs
)) {
684 error_report("Opening rbd snapshots without an explicit "
685 "read-only=on option is deprecated. Future versions "
686 "will refuse to open the image instead of "
687 "automatically marking the image read-only.");
688 r
= bdrv_set_read_only(bs
, true, &local_err
);
690 error_propagate(errp
, local_err
);
700 rados_ioctx_destroy(s
->io_ctx
);
702 g_free(s
->image_name
);
703 rados_shutdown(s
->cluster
);
705 qapi_free_BlockdevOptionsRbd(opts
);
712 /* Since RBD is currently always opened R/W via the API,
713 * we just need to check if we are using a snapshot or not, in
714 * order to determine if we will allow it to be R/W */
715 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
716 BlockReopenQueue
*queue
, Error
**errp
)
718 BDRVRBDState
*s
= state
->bs
->opaque
;
721 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
723 "Cannot change node '%s' to r/w when using RBD snapshot",
724 bdrv_get_device_or_node_name(state
->bs
));
731 static void qemu_rbd_close(BlockDriverState
*bs
)
733 BDRVRBDState
*s
= bs
->opaque
;
736 rados_ioctx_destroy(s
->io_ctx
);
738 g_free(s
->image_name
);
739 rados_shutdown(s
->cluster
);
742 static const AIOCBInfo rbd_aiocb_info
= {
743 .aiocb_size
= sizeof(RBDAIOCB
),
746 static void rbd_finish_bh(void *opaque
)
748 RADOSCB
*rcb
= opaque
;
749 qemu_rbd_complete_aio(rcb
);
753 * This is the callback function for rbd_aio_read and _write
755 * Note: this function is being called from a non qemu thread so
756 * we need to be careful about what we do here. Generally we only
757 * schedule a BH, and do the rest of the io completion handling
758 * from rbd_finish_bh() which runs in a qemu context.
760 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
762 RBDAIOCB
*acb
= rcb
->acb
;
764 rcb
->ret
= rbd_aio_get_return_value(c
);
767 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
771 static int rbd_aio_discard_wrapper(rbd_image_t image
,
774 rbd_completion_t comp
)
776 #ifdef LIBRBD_SUPPORTS_DISCARD
777 return rbd_aio_discard(image
, off
, len
, comp
);
783 static int rbd_aio_flush_wrapper(rbd_image_t image
,
784 rbd_completion_t comp
)
786 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
787 return rbd_aio_flush(image
, comp
);
793 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
797 BlockCompletionFunc
*cb
,
806 BDRVRBDState
*s
= bs
->opaque
;
808 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
811 assert(!qiov
|| qiov
->size
== size
);
813 rcb
= g_new(RADOSCB
, 1);
815 if (!LIBRBD_USE_IOVEC
) {
816 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
819 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
820 if (acb
->bounce
== NULL
) {
824 if (cmd
== RBD_AIO_WRITE
) {
825 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
827 rcb
->buf
= acb
->bounce
;
837 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
844 #ifdef LIBRBD_SUPPORTS_IOVEC
845 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
847 r
= rbd_aio_write(s
->image
, off
, size
, rcb
->buf
, c
);
851 #ifdef LIBRBD_SUPPORTS_IOVEC
852 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
854 r
= rbd_aio_read(s
->image
, off
, size
, rcb
->buf
, c
);
857 case RBD_AIO_DISCARD
:
858 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
861 r
= rbd_aio_flush_wrapper(s
->image
, c
);
868 goto failed_completion
;
876 if (!LIBRBD_USE_IOVEC
) {
877 qemu_vfree(acb
->bounce
);
884 static BlockAIOCB
*qemu_rbd_aio_readv(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 static BlockAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
900 BlockCompletionFunc
*cb
,
903 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
904 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
908 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
909 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
910 BlockCompletionFunc
*cb
,
913 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
918 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
920 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
921 /* rbd_flush added in 0.1.1 */
922 BDRVRBDState
*s
= bs
->opaque
;
923 return rbd_flush(s
->image
);
930 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
932 BDRVRBDState
*s
= bs
->opaque
;
933 rbd_image_info_t info
;
936 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
941 bdi
->cluster_size
= info
.obj_size
;
945 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
947 BDRVRBDState
*s
= bs
->opaque
;
948 rbd_image_info_t info
;
951 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
959 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
,
960 PreallocMode prealloc
, Error
**errp
)
962 BDRVRBDState
*s
= bs
->opaque
;
965 if (prealloc
!= PREALLOC_MODE_OFF
) {
966 error_setg(errp
, "Unsupported preallocation mode '%s'",
967 PreallocMode_str(prealloc
));
971 r
= rbd_resize(s
->image
, offset
);
973 error_setg_errno(errp
, -r
, "Failed to resize file");
980 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
981 QEMUSnapshotInfo
*sn_info
)
983 BDRVRBDState
*s
= bs
->opaque
;
986 if (sn_info
->name
[0] == '\0') {
987 return -EINVAL
; /* we need a name for rbd snapshots */
991 * rbd snapshots are using the name as the user controlled unique identifier
992 * we can't use the rbd snapid for that purpose, as it can't be set
994 if (sn_info
->id_str
[0] != '\0' &&
995 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
999 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
1003 r
= rbd_snap_create(s
->image
, sn_info
->name
);
1005 error_report("failed to create snap: %s", strerror(-r
));
1012 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1013 const char *snapshot_id
,
1014 const char *snapshot_name
,
1017 BDRVRBDState
*s
= bs
->opaque
;
1020 if (!snapshot_name
) {
1021 error_setg(errp
, "rbd need a valid snapshot name");
1025 /* If snapshot_id is specified, it must be equal to name, see
1026 qemu_rbd_snap_list() */
1027 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1029 "rbd do not support snapshot id, it should be NULL or "
1030 "equal to snapshot name");
1034 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1036 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1041 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1042 const char *snapshot_name
)
1044 BDRVRBDState
*s
= bs
->opaque
;
1046 return rbd_snap_rollback(s
->image
, snapshot_name
);
1049 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1050 QEMUSnapshotInfo
**psn_tab
)
1052 BDRVRBDState
*s
= bs
->opaque
;
1053 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1055 rbd_snap_info_t
*snaps
;
1056 int max_snaps
= RBD_MAX_SNAPS
;
1059 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1060 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1061 if (snap_count
<= 0) {
1064 } while (snap_count
== -ERANGE
);
1066 if (snap_count
<= 0) {
1070 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1072 for (i
= 0; i
< snap_count
; i
++) {
1073 const char *snap_name
= snaps
[i
].name
;
1075 sn_info
= sn_tab
+ i
;
1076 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1077 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1079 sn_info
->vm_state_size
= snaps
[i
].size
;
1080 sn_info
->date_sec
= 0;
1081 sn_info
->date_nsec
= 0;
1082 sn_info
->vm_clock_nsec
= 0;
1084 rbd_snap_list_end(snaps
);
1092 #ifdef LIBRBD_SUPPORTS_DISCARD
1093 static BlockAIOCB
*qemu_rbd_aio_pdiscard(BlockDriverState
*bs
,
1096 BlockCompletionFunc
*cb
,
1099 return rbd_start_aio(bs
, offset
, NULL
, bytes
, cb
, opaque
,
1104 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1105 static void coroutine_fn
qemu_rbd_co_invalidate_cache(BlockDriverState
*bs
,
1108 BDRVRBDState
*s
= bs
->opaque
;
1109 int r
= rbd_invalidate_cache(s
->image
);
1111 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1116 static QemuOptsList qemu_rbd_create_opts
= {
1117 .name
= "rbd-create-opts",
1118 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1121 .name
= BLOCK_OPT_SIZE
,
1122 .type
= QEMU_OPT_SIZE
,
1123 .help
= "Virtual disk size"
1126 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1127 .type
= QEMU_OPT_SIZE
,
1128 .help
= "RBD object size"
1131 .name
= "password-secret",
1132 .type
= QEMU_OPT_STRING
,
1133 .help
= "ID of secret providing the password",
1135 { /* end of list */ }
1139 static BlockDriver bdrv_rbd
= {
1140 .format_name
= "rbd",
1141 .instance_size
= sizeof(BDRVRBDState
),
1142 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1143 .bdrv_file_open
= qemu_rbd_open
,
1144 .bdrv_close
= qemu_rbd_close
,
1145 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1146 .bdrv_co_create_opts
= qemu_rbd_co_create_opts
,
1147 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1148 .bdrv_get_info
= qemu_rbd_getinfo
,
1149 .create_opts
= &qemu_rbd_create_opts
,
1150 .bdrv_getlength
= qemu_rbd_getlength
,
1151 .bdrv_truncate
= qemu_rbd_truncate
,
1152 .protocol_name
= "rbd",
1154 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
1155 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
1157 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1158 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
1160 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1163 #ifdef LIBRBD_SUPPORTS_DISCARD
1164 .bdrv_aio_pdiscard
= qemu_rbd_aio_pdiscard
,
1167 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1168 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1169 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1170 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1171 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1172 .bdrv_co_invalidate_cache
= qemu_rbd_co_invalidate_cache
,
1176 static void bdrv_rbd_init(void)
1178 bdrv_register(&bdrv_rbd
);
1181 block_init(bdrv_rbd_init
);