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 int qemu_rbd_connect(rados_t
*cluster
, rados_ioctx_t
*io_ctx
,
107 BlockdevOptionsRbd
*opts
, bool cache
,
108 const char *keypairs
, const char *secretid
,
111 static char *qemu_rbd_next_tok(char *src
, char delim
, char **p
)
117 for (end
= src
; *end
; ++end
) {
121 if (*end
== '\\' && end
[1] != '\0') {
132 static void qemu_rbd_unescape(char *src
)
136 for (p
= src
; *src
; ++src
, ++p
) {
137 if (*src
== '\\' && src
[1] != '\0') {
145 static void qemu_rbd_parse_filename(const char *filename
, QDict
*options
,
150 QList
*keypairs
= NULL
;
153 if (!strstart(filename
, "rbd:", &start
)) {
154 error_setg(errp
, "File name must start with 'rbd:'");
158 buf
= g_strdup(start
);
161 found_str
= qemu_rbd_next_tok(p
, '/', &p
);
163 error_setg(errp
, "Pool name is required");
166 qemu_rbd_unescape(found_str
);
167 qdict_put_str(options
, "pool", found_str
);
169 if (strchr(p
, '@')) {
170 found_str
= qemu_rbd_next_tok(p
, '@', &p
);
171 qemu_rbd_unescape(found_str
);
172 qdict_put_str(options
, "image", found_str
);
174 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
175 qemu_rbd_unescape(found_str
);
176 qdict_put_str(options
, "snapshot", found_str
);
178 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
179 qemu_rbd_unescape(found_str
);
180 qdict_put_str(options
, "image", found_str
);
186 /* The following are essentially all key/value pairs, and we treat
187 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
190 name
= qemu_rbd_next_tok(p
, '=', &p
);
192 error_setg(errp
, "conf option %s has no value", name
);
196 qemu_rbd_unescape(name
);
198 value
= qemu_rbd_next_tok(p
, ':', &p
);
199 qemu_rbd_unescape(value
);
201 if (!strcmp(name
, "conf")) {
202 qdict_put_str(options
, "conf", value
);
203 } else if (!strcmp(name
, "id")) {
204 qdict_put_str(options
, "user", value
);
207 * We pass these internally to qemu_rbd_set_keypairs(), so
208 * we can get away with the simpler list of [ "key1",
209 * "value1", "key2", "value2" ] rather than a raw dict
210 * { "key1": "value1", "key2": "value2" } where we can't
211 * guarantee order, or even a more correct but complex
212 * [ { "key1": "value1" }, { "key2": "value2" } ]
215 keypairs
= qlist_new();
217 qlist_append_str(keypairs
, name
);
218 qlist_append_str(keypairs
, value
);
223 qdict_put(options
, "=keyvalue-pairs",
224 qobject_to_json(QOBJECT(keypairs
)));
234 static int qemu_rbd_set_auth(rados_t cluster
, const char *secretid
,
241 gchar
*secret
= qcrypto_secret_lookup_as_base64(secretid
,
247 rados_conf_set(cluster
, "key", secret
);
253 static int qemu_rbd_set_keypairs(rados_t cluster
, const char *keypairs_json
,
263 if (!keypairs_json
) {
266 keypairs
= qobject_to_qlist(qobject_from_json(keypairs_json
,
268 remaining
= qlist_size(keypairs
) / 2;
271 while (remaining
--) {
272 name
= qobject_to_qstring(qlist_pop(keypairs
));
273 value
= qobject_to_qstring(qlist_pop(keypairs
));
274 assert(name
&& value
);
275 key
= qstring_get_str(name
);
277 ret
= rados_conf_set(cluster
, key
, qstring_get_str(value
));
280 error_setg_errno(errp
, -ret
, "invalid conf option %s", key
);
292 static void qemu_rbd_memset(RADOSCB
*rcb
, int64_t offs
)
294 if (LIBRBD_USE_IOVEC
) {
295 RBDAIOCB
*acb
= rcb
->acb
;
296 iov_memset(acb
->qiov
->iov
, acb
->qiov
->niov
, offs
, 0,
297 acb
->qiov
->size
- offs
);
299 memset(rcb
->buf
+ offs
, 0, rcb
->size
- offs
);
303 static QemuOptsList runtime_opts
= {
305 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
309 .type
= QEMU_OPT_STRING
,
310 .help
= "Rados pool name",
314 .type
= QEMU_OPT_STRING
,
315 .help
= "Image name in the pool",
319 .type
= QEMU_OPT_STRING
,
320 .help
= "Rados config file location",
324 .type
= QEMU_OPT_STRING
,
325 .help
= "Ceph snapshot name",
328 /* maps to 'id' in rados_create() */
330 .type
= QEMU_OPT_STRING
,
331 .help
= "Rados id name",
334 * server.* extracted manually, see qemu_rbd_mon_host()
336 { /* end of list */ }
340 /* FIXME Deprecate and remove keypairs or make it available in QMP.
341 * password_secret should eventually be configurable in opts->location. Support
342 * for it in .bdrv_open will make it work here as well. */
343 static int qemu_rbd_do_create(BlockdevCreateOptions
*options
,
344 const char *keypairs
, const char *password_secret
,
347 BlockdevCreateOptionsRbd
*opts
= &options
->u
.rbd
;
349 rados_ioctx_t io_ctx
;
353 assert(options
->driver
== BLOCKDEV_DRIVER_RBD
);
354 if (opts
->location
->has_snapshot
) {
355 error_setg(errp
, "Can't use snapshot name for image creation");
359 if (opts
->has_cluster_size
) {
360 int64_t objsize
= opts
->cluster_size
;
361 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
362 error_setg(errp
, "obj size needs to be power of 2");
365 if (objsize
< 4096) {
366 error_setg(errp
, "obj size too small");
369 obj_order
= ctz32(objsize
);
372 ret
= qemu_rbd_connect(&cluster
, &io_ctx
, opts
->location
, false, keypairs
,
373 password_secret
, errp
);
378 ret
= rbd_create(io_ctx
, opts
->location
->image
, opts
->size
, &obj_order
);
380 error_setg_errno(errp
, -ret
, "error rbd create");
386 rados_ioctx_destroy(io_ctx
);
387 rados_shutdown(cluster
);
391 static int qemu_rbd_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
393 return qemu_rbd_do_create(options
, NULL
, NULL
, errp
);
396 static int coroutine_fn
qemu_rbd_co_create_opts(const char *filename
,
400 BlockdevCreateOptions
*create_options
;
401 BlockdevCreateOptionsRbd
*rbd_opts
;
402 BlockdevOptionsRbd
*loc
;
403 Error
*local_err
= NULL
;
404 const char *keypairs
, *password_secret
;
405 QDict
*options
= NULL
;
408 create_options
= g_new0(BlockdevCreateOptions
, 1);
409 create_options
->driver
= BLOCKDEV_DRIVER_RBD
;
410 rbd_opts
= &create_options
->u
.rbd
;
412 rbd_opts
->location
= g_new0(BlockdevOptionsRbd
, 1);
414 password_secret
= qemu_opt_get(opts
, "password-secret");
416 /* Read out options */
417 rbd_opts
->size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
419 rbd_opts
->cluster_size
= qemu_opt_get_size_del(opts
,
420 BLOCK_OPT_CLUSTER_SIZE
, 0);
421 rbd_opts
->has_cluster_size
= (rbd_opts
->cluster_size
!= 0);
423 options
= qdict_new();
424 qemu_rbd_parse_filename(filename
, options
, &local_err
);
427 error_propagate(errp
, local_err
);
432 * Caution: while qdict_get_try_str() is fine, getting non-string
433 * types would require more care. When @options come from -blockdev
434 * or blockdev_add, its members are typed according to the QAPI
435 * schema, but when they come from -drive, they're all QString.
437 loc
= rbd_opts
->location
;
438 loc
->pool
= g_strdup(qdict_get_try_str(options
, "pool"));
439 loc
->conf
= g_strdup(qdict_get_try_str(options
, "conf"));
440 loc
->has_conf
= !!loc
->conf
;
441 loc
->user
= g_strdup(qdict_get_try_str(options
, "user"));
442 loc
->has_user
= !!loc
->user
;
443 loc
->image
= g_strdup(qdict_get_try_str(options
, "image"));
444 keypairs
= qdict_get_try_str(options
, "=keyvalue-pairs");
446 ret
= qemu_rbd_do_create(create_options
, keypairs
, password_secret
, errp
);
453 qapi_free_BlockdevCreateOptions(create_options
);
458 * This aio completion is being called from rbd_finish_bh() and runs in qemu
461 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
463 RBDAIOCB
*acb
= rcb
->acb
;
468 if (acb
->cmd
!= RBD_AIO_READ
) {
472 } else if (!acb
->error
) {
473 acb
->ret
= rcb
->size
;
477 qemu_rbd_memset(rcb
, 0);
480 } else if (r
< rcb
->size
) {
481 qemu_rbd_memset(rcb
, r
);
483 acb
->ret
= rcb
->size
;
485 } else if (!acb
->error
) {
492 if (!LIBRBD_USE_IOVEC
) {
493 if (acb
->cmd
== RBD_AIO_READ
) {
494 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
496 qemu_vfree(acb
->bounce
);
499 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
504 static char *qemu_rbd_mon_host(BlockdevOptionsRbd
*opts
, Error
**errp
)
507 const char *host
, *port
;
509 InetSocketAddressBaseList
*p
;
512 if (!opts
->has_server
) {
516 for (cnt
= 0, p
= opts
->server
; p
; p
= p
->next
) {
520 vals
= g_new(const char *, cnt
+ 1);
522 for (i
= 0, p
= opts
->server
; p
; p
= p
->next
, i
++) {
523 host
= p
->value
->host
;
524 port
= p
->value
->port
;
526 if (strchr(host
, ':')) {
527 vals
[i
] = g_strdup_printf("[%s]:%s", host
, port
);
529 vals
[i
] = g_strdup_printf("%s:%s", host
, port
);
534 rados_str
= i
? g_strjoinv(";", (char **)vals
) : NULL
;
535 g_strfreev((char **)vals
);
539 static int qemu_rbd_connect(rados_t
*cluster
, rados_ioctx_t
*io_ctx
,
540 BlockdevOptionsRbd
*opts
, bool cache
,
541 const char *keypairs
, const char *secretid
,
544 char *mon_host
= NULL
;
545 Error
*local_err
= NULL
;
548 mon_host
= qemu_rbd_mon_host(opts
, &local_err
);
550 error_propagate(errp
, local_err
);
555 r
= rados_create(cluster
, opts
->user
);
557 error_setg_errno(errp
, -r
, "error initializing");
561 /* try default location when conf=NULL, but ignore failure */
562 r
= rados_conf_read_file(*cluster
, opts
->conf
);
563 if (opts
->has_conf
&& r
< 0) {
564 error_setg_errno(errp
, -r
, "error reading conf file %s", opts
->conf
);
565 goto failed_shutdown
;
568 r
= qemu_rbd_set_keypairs(*cluster
, keypairs
, errp
);
570 goto failed_shutdown
;
574 r
= rados_conf_set(*cluster
, "mon_host", mon_host
);
576 goto failed_shutdown
;
580 if (qemu_rbd_set_auth(*cluster
, secretid
, errp
) < 0) {
582 goto failed_shutdown
;
586 * Fallback to more conservative semantics if setting cache
587 * options fails. Ignore errors from setting rbd_cache because the
588 * only possible error is that the option does not exist, and
589 * librbd defaults to no caching. If write through caching cannot
590 * be set up, fall back to no caching.
593 rados_conf_set(*cluster
, "rbd_cache", "true");
595 rados_conf_set(*cluster
, "rbd_cache", "false");
598 r
= rados_connect(*cluster
);
600 error_setg_errno(errp
, -r
, "error connecting");
601 goto failed_shutdown
;
604 r
= rados_ioctx_create(*cluster
, opts
->pool
, io_ctx
);
606 error_setg_errno(errp
, -r
, "error opening pool %s", opts
->pool
);
607 goto failed_shutdown
;
613 rados_shutdown(*cluster
);
619 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
622 BDRVRBDState
*s
= bs
->opaque
;
623 BlockdevOptionsRbd
*opts
= NULL
;
625 QObject
*crumpled
= NULL
;
626 Error
*local_err
= NULL
;
627 const char *filename
;
628 char *keypairs
, *secretid
;
631 /* If we are given a filename, parse the filename, with precedence given to
632 * filename encoded options */
633 filename
= qdict_get_try_str(options
, "filename");
635 warn_report("'filename' option specified. "
636 "This is an unsupported option, and may be deprecated "
638 qemu_rbd_parse_filename(filename
, options
, &local_err
);
639 qdict_del(options
, "filename");
641 error_propagate(errp
, local_err
);
646 keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
648 qdict_del(options
, "=keyvalue-pairs");
651 secretid
= g_strdup(qdict_get_try_str(options
, "password-secret"));
653 qdict_del(options
, "password-secret");
656 /* Convert the remaining options into a QAPI object */
657 crumpled
= qdict_crumple(options
, errp
);
658 if (crumpled
== NULL
) {
663 v
= qobject_input_visitor_new_keyval(crumpled
);
664 visit_type_BlockdevOptionsRbd(v
, NULL
, &opts
, &local_err
);
666 qobject_decref(crumpled
);
669 error_propagate(errp
, local_err
);
674 r
= qemu_rbd_connect(&s
->cluster
, &s
->io_ctx
, opts
,
675 !(flags
& BDRV_O_NOCACHE
), keypairs
, secretid
, errp
);
680 s
->snap
= g_strdup(opts
->snapshot
);
681 s
->image_name
= g_strdup(opts
->image
);
683 /* rbd_open is always r/w */
684 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
686 error_setg_errno(errp
, -r
, "error reading header from %s",
691 /* If we are using an rbd snapshot, we must be r/o, otherwise
693 if (s
->snap
!= NULL
) {
694 if (!bdrv_is_read_only(bs
)) {
695 error_report("Opening rbd snapshots without an explicit "
696 "read-only=on option is deprecated. Future versions "
697 "will refuse to open the image instead of "
698 "automatically marking the image read-only.");
699 r
= bdrv_set_read_only(bs
, true, &local_err
);
701 error_propagate(errp
, local_err
);
711 rados_ioctx_destroy(s
->io_ctx
);
713 g_free(s
->image_name
);
714 rados_shutdown(s
->cluster
);
716 qapi_free_BlockdevOptionsRbd(opts
);
723 /* Since RBD is currently always opened R/W via the API,
724 * we just need to check if we are using a snapshot or not, in
725 * order to determine if we will allow it to be R/W */
726 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
727 BlockReopenQueue
*queue
, Error
**errp
)
729 BDRVRBDState
*s
= state
->bs
->opaque
;
732 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
734 "Cannot change node '%s' to r/w when using RBD snapshot",
735 bdrv_get_device_or_node_name(state
->bs
));
742 static void qemu_rbd_close(BlockDriverState
*bs
)
744 BDRVRBDState
*s
= bs
->opaque
;
747 rados_ioctx_destroy(s
->io_ctx
);
749 g_free(s
->image_name
);
750 rados_shutdown(s
->cluster
);
753 static const AIOCBInfo rbd_aiocb_info
= {
754 .aiocb_size
= sizeof(RBDAIOCB
),
757 static void rbd_finish_bh(void *opaque
)
759 RADOSCB
*rcb
= opaque
;
760 qemu_rbd_complete_aio(rcb
);
764 * This is the callback function for rbd_aio_read and _write
766 * Note: this function is being called from a non qemu thread so
767 * we need to be careful about what we do here. Generally we only
768 * schedule a BH, and do the rest of the io completion handling
769 * from rbd_finish_bh() which runs in a qemu context.
771 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
773 RBDAIOCB
*acb
= rcb
->acb
;
775 rcb
->ret
= rbd_aio_get_return_value(c
);
778 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
782 static int rbd_aio_discard_wrapper(rbd_image_t image
,
785 rbd_completion_t comp
)
787 #ifdef LIBRBD_SUPPORTS_DISCARD
788 return rbd_aio_discard(image
, off
, len
, comp
);
794 static int rbd_aio_flush_wrapper(rbd_image_t image
,
795 rbd_completion_t comp
)
797 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
798 return rbd_aio_flush(image
, comp
);
804 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
808 BlockCompletionFunc
*cb
,
817 BDRVRBDState
*s
= bs
->opaque
;
819 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
822 assert(!qiov
|| qiov
->size
== size
);
824 rcb
= g_new(RADOSCB
, 1);
826 if (!LIBRBD_USE_IOVEC
) {
827 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
830 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
831 if (acb
->bounce
== NULL
) {
835 if (cmd
== RBD_AIO_WRITE
) {
836 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
838 rcb
->buf
= acb
->bounce
;
848 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
855 #ifdef LIBRBD_SUPPORTS_IOVEC
856 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
858 r
= rbd_aio_write(s
->image
, off
, size
, rcb
->buf
, c
);
862 #ifdef LIBRBD_SUPPORTS_IOVEC
863 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
865 r
= rbd_aio_read(s
->image
, off
, size
, rcb
->buf
, c
);
868 case RBD_AIO_DISCARD
:
869 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
872 r
= rbd_aio_flush_wrapper(s
->image
, c
);
879 goto failed_completion
;
887 if (!LIBRBD_USE_IOVEC
) {
888 qemu_vfree(acb
->bounce
);
895 static BlockAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
899 BlockCompletionFunc
*cb
,
902 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
903 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
907 static BlockAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
911 BlockCompletionFunc
*cb
,
914 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
915 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
919 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
920 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
921 BlockCompletionFunc
*cb
,
924 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
929 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
931 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
932 /* rbd_flush added in 0.1.1 */
933 BDRVRBDState
*s
= bs
->opaque
;
934 return rbd_flush(s
->image
);
941 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
943 BDRVRBDState
*s
= bs
->opaque
;
944 rbd_image_info_t info
;
947 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
952 bdi
->cluster_size
= info
.obj_size
;
956 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
958 BDRVRBDState
*s
= bs
->opaque
;
959 rbd_image_info_t info
;
962 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
970 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
,
971 PreallocMode prealloc
, Error
**errp
)
973 BDRVRBDState
*s
= bs
->opaque
;
976 if (prealloc
!= PREALLOC_MODE_OFF
) {
977 error_setg(errp
, "Unsupported preallocation mode '%s'",
978 PreallocMode_str(prealloc
));
982 r
= rbd_resize(s
->image
, offset
);
984 error_setg_errno(errp
, -r
, "Failed to resize file");
991 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
992 QEMUSnapshotInfo
*sn_info
)
994 BDRVRBDState
*s
= bs
->opaque
;
997 if (sn_info
->name
[0] == '\0') {
998 return -EINVAL
; /* we need a name for rbd snapshots */
1002 * rbd snapshots are using the name as the user controlled unique identifier
1003 * we can't use the rbd snapid for that purpose, as it can't be set
1005 if (sn_info
->id_str
[0] != '\0' &&
1006 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
1010 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
1014 r
= rbd_snap_create(s
->image
, sn_info
->name
);
1016 error_report("failed to create snap: %s", strerror(-r
));
1023 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1024 const char *snapshot_id
,
1025 const char *snapshot_name
,
1028 BDRVRBDState
*s
= bs
->opaque
;
1031 if (!snapshot_name
) {
1032 error_setg(errp
, "rbd need a valid snapshot name");
1036 /* If snapshot_id is specified, it must be equal to name, see
1037 qemu_rbd_snap_list() */
1038 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1040 "rbd do not support snapshot id, it should be NULL or "
1041 "equal to snapshot name");
1045 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1047 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1052 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1053 const char *snapshot_name
)
1055 BDRVRBDState
*s
= bs
->opaque
;
1057 return rbd_snap_rollback(s
->image
, snapshot_name
);
1060 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1061 QEMUSnapshotInfo
**psn_tab
)
1063 BDRVRBDState
*s
= bs
->opaque
;
1064 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1066 rbd_snap_info_t
*snaps
;
1067 int max_snaps
= RBD_MAX_SNAPS
;
1070 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1071 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1072 if (snap_count
<= 0) {
1075 } while (snap_count
== -ERANGE
);
1077 if (snap_count
<= 0) {
1081 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1083 for (i
= 0; i
< snap_count
; i
++) {
1084 const char *snap_name
= snaps
[i
].name
;
1086 sn_info
= sn_tab
+ i
;
1087 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1088 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1090 sn_info
->vm_state_size
= snaps
[i
].size
;
1091 sn_info
->date_sec
= 0;
1092 sn_info
->date_nsec
= 0;
1093 sn_info
->vm_clock_nsec
= 0;
1095 rbd_snap_list_end(snaps
);
1103 #ifdef LIBRBD_SUPPORTS_DISCARD
1104 static BlockAIOCB
*qemu_rbd_aio_pdiscard(BlockDriverState
*bs
,
1107 BlockCompletionFunc
*cb
,
1110 return rbd_start_aio(bs
, offset
, NULL
, bytes
, cb
, opaque
,
1115 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1116 static void coroutine_fn
qemu_rbd_co_invalidate_cache(BlockDriverState
*bs
,
1119 BDRVRBDState
*s
= bs
->opaque
;
1120 int r
= rbd_invalidate_cache(s
->image
);
1122 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1127 static QemuOptsList qemu_rbd_create_opts
= {
1128 .name
= "rbd-create-opts",
1129 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1132 .name
= BLOCK_OPT_SIZE
,
1133 .type
= QEMU_OPT_SIZE
,
1134 .help
= "Virtual disk size"
1137 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1138 .type
= QEMU_OPT_SIZE
,
1139 .help
= "RBD object size"
1142 .name
= "password-secret",
1143 .type
= QEMU_OPT_STRING
,
1144 .help
= "ID of secret providing the password",
1146 { /* end of list */ }
1150 static BlockDriver bdrv_rbd
= {
1151 .format_name
= "rbd",
1152 .instance_size
= sizeof(BDRVRBDState
),
1153 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1154 .bdrv_file_open
= qemu_rbd_open
,
1155 .bdrv_close
= qemu_rbd_close
,
1156 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1157 .bdrv_co_create
= qemu_rbd_co_create
,
1158 .bdrv_co_create_opts
= qemu_rbd_co_create_opts
,
1159 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1160 .bdrv_get_info
= qemu_rbd_getinfo
,
1161 .create_opts
= &qemu_rbd_create_opts
,
1162 .bdrv_getlength
= qemu_rbd_getlength
,
1163 .bdrv_truncate
= qemu_rbd_truncate
,
1164 .protocol_name
= "rbd",
1166 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
1167 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
1169 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1170 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
1172 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1175 #ifdef LIBRBD_SUPPORTS_DISCARD
1176 .bdrv_aio_pdiscard
= qemu_rbd_aio_pdiscard
,
1179 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1180 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1181 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1182 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1183 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1184 .bdrv_co_invalidate_cache
= qemu_rbd_co_invalidate_cache
,
1188 static void bdrv_rbd_init(void)
1190 bdrv_register(&bdrv_rbd
);
1193 block_init(bdrv_rbd_init
);