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
)));
229 qobject_unref(keypairs
);
234 static void qemu_rbd_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
236 /* XXX Does RBD support AIO on less than 512-byte alignment? */
237 bs
->bl
.request_alignment
= 512;
241 static int qemu_rbd_set_auth(rados_t cluster
, const char *secretid
,
248 gchar
*secret
= qcrypto_secret_lookup_as_base64(secretid
,
254 rados_conf_set(cluster
, "key", secret
);
260 static int qemu_rbd_set_keypairs(rados_t cluster
, const char *keypairs_json
,
270 if (!keypairs_json
) {
273 keypairs
= qobject_to(QList
,
274 qobject_from_json(keypairs_json
, &error_abort
));
275 remaining
= qlist_size(keypairs
) / 2;
278 while (remaining
--) {
279 name
= qobject_to(QString
, qlist_pop(keypairs
));
280 value
= qobject_to(QString
, qlist_pop(keypairs
));
281 assert(name
&& value
);
282 key
= qstring_get_str(name
);
284 ret
= rados_conf_set(cluster
, key
, qstring_get_str(value
));
285 qobject_unref(value
);
287 error_setg_errno(errp
, -ret
, "invalid conf option %s", key
);
295 qobject_unref(keypairs
);
299 static void qemu_rbd_memset(RADOSCB
*rcb
, int64_t offs
)
301 if (LIBRBD_USE_IOVEC
) {
302 RBDAIOCB
*acb
= rcb
->acb
;
303 iov_memset(acb
->qiov
->iov
, acb
->qiov
->niov
, offs
, 0,
304 acb
->qiov
->size
- offs
);
306 memset(rcb
->buf
+ offs
, 0, rcb
->size
- offs
);
310 static QemuOptsList runtime_opts
= {
312 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
316 .type
= QEMU_OPT_STRING
,
317 .help
= "Rados pool name",
321 .type
= QEMU_OPT_STRING
,
322 .help
= "Image name in the pool",
326 .type
= QEMU_OPT_STRING
,
327 .help
= "Rados config file location",
331 .type
= QEMU_OPT_STRING
,
332 .help
= "Ceph snapshot name",
335 /* maps to 'id' in rados_create() */
337 .type
= QEMU_OPT_STRING
,
338 .help
= "Rados id name",
341 * server.* extracted manually, see qemu_rbd_mon_host()
343 { /* end of list */ }
347 /* FIXME Deprecate and remove keypairs or make it available in QMP.
348 * password_secret should eventually be configurable in opts->location. Support
349 * for it in .bdrv_open will make it work here as well. */
350 static int qemu_rbd_do_create(BlockdevCreateOptions
*options
,
351 const char *keypairs
, const char *password_secret
,
354 BlockdevCreateOptionsRbd
*opts
= &options
->u
.rbd
;
356 rados_ioctx_t io_ctx
;
360 assert(options
->driver
== BLOCKDEV_DRIVER_RBD
);
361 if (opts
->location
->has_snapshot
) {
362 error_setg(errp
, "Can't use snapshot name for image creation");
366 if (opts
->has_cluster_size
) {
367 int64_t objsize
= opts
->cluster_size
;
368 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
369 error_setg(errp
, "obj size needs to be power of 2");
372 if (objsize
< 4096) {
373 error_setg(errp
, "obj size too small");
376 obj_order
= ctz32(objsize
);
379 ret
= qemu_rbd_connect(&cluster
, &io_ctx
, opts
->location
, false, keypairs
,
380 password_secret
, errp
);
385 ret
= rbd_create(io_ctx
, opts
->location
->image
, opts
->size
, &obj_order
);
387 error_setg_errno(errp
, -ret
, "error rbd create");
393 rados_ioctx_destroy(io_ctx
);
394 rados_shutdown(cluster
);
398 static int qemu_rbd_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
400 return qemu_rbd_do_create(options
, NULL
, NULL
, errp
);
403 static int coroutine_fn
qemu_rbd_co_create_opts(const char *filename
,
407 BlockdevCreateOptions
*create_options
;
408 BlockdevCreateOptionsRbd
*rbd_opts
;
409 BlockdevOptionsRbd
*loc
;
410 Error
*local_err
= NULL
;
411 const char *keypairs
, *password_secret
;
412 QDict
*options
= NULL
;
415 create_options
= g_new0(BlockdevCreateOptions
, 1);
416 create_options
->driver
= BLOCKDEV_DRIVER_RBD
;
417 rbd_opts
= &create_options
->u
.rbd
;
419 rbd_opts
->location
= g_new0(BlockdevOptionsRbd
, 1);
421 password_secret
= qemu_opt_get(opts
, "password-secret");
423 /* Read out options */
424 rbd_opts
->size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
426 rbd_opts
->cluster_size
= qemu_opt_get_size_del(opts
,
427 BLOCK_OPT_CLUSTER_SIZE
, 0);
428 rbd_opts
->has_cluster_size
= (rbd_opts
->cluster_size
!= 0);
430 options
= qdict_new();
431 qemu_rbd_parse_filename(filename
, options
, &local_err
);
434 error_propagate(errp
, local_err
);
439 * Caution: while qdict_get_try_str() is fine, getting non-string
440 * types would require more care. When @options come from -blockdev
441 * or blockdev_add, its members are typed according to the QAPI
442 * schema, but when they come from -drive, they're all QString.
444 loc
= rbd_opts
->location
;
445 loc
->pool
= g_strdup(qdict_get_try_str(options
, "pool"));
446 loc
->conf
= g_strdup(qdict_get_try_str(options
, "conf"));
447 loc
->has_conf
= !!loc
->conf
;
448 loc
->user
= g_strdup(qdict_get_try_str(options
, "user"));
449 loc
->has_user
= !!loc
->user
;
450 loc
->image
= g_strdup(qdict_get_try_str(options
, "image"));
451 keypairs
= qdict_get_try_str(options
, "=keyvalue-pairs");
453 ret
= qemu_rbd_do_create(create_options
, keypairs
, password_secret
, errp
);
459 qobject_unref(options
);
460 qapi_free_BlockdevCreateOptions(create_options
);
465 * This aio completion is being called from rbd_finish_bh() and runs in qemu
468 static void qemu_rbd_complete_aio(RADOSCB
*rcb
)
470 RBDAIOCB
*acb
= rcb
->acb
;
475 if (acb
->cmd
!= RBD_AIO_READ
) {
479 } else if (!acb
->error
) {
480 acb
->ret
= rcb
->size
;
484 qemu_rbd_memset(rcb
, 0);
487 } else if (r
< rcb
->size
) {
488 qemu_rbd_memset(rcb
, r
);
490 acb
->ret
= rcb
->size
;
492 } else if (!acb
->error
) {
499 if (!LIBRBD_USE_IOVEC
) {
500 if (acb
->cmd
== RBD_AIO_READ
) {
501 qemu_iovec_from_buf(acb
->qiov
, 0, acb
->bounce
, acb
->qiov
->size
);
503 qemu_vfree(acb
->bounce
);
506 acb
->common
.cb(acb
->common
.opaque
, (acb
->ret
> 0 ? 0 : acb
->ret
));
511 static char *qemu_rbd_mon_host(BlockdevOptionsRbd
*opts
, Error
**errp
)
514 const char *host
, *port
;
516 InetSocketAddressBaseList
*p
;
519 if (!opts
->has_server
) {
523 for (cnt
= 0, p
= opts
->server
; p
; p
= p
->next
) {
527 vals
= g_new(const char *, cnt
+ 1);
529 for (i
= 0, p
= opts
->server
; p
; p
= p
->next
, i
++) {
530 host
= p
->value
->host
;
531 port
= p
->value
->port
;
533 if (strchr(host
, ':')) {
534 vals
[i
] = g_strdup_printf("[%s]:%s", host
, port
);
536 vals
[i
] = g_strdup_printf("%s:%s", host
, port
);
541 rados_str
= i
? g_strjoinv(";", (char **)vals
) : NULL
;
542 g_strfreev((char **)vals
);
546 static int qemu_rbd_connect(rados_t
*cluster
, rados_ioctx_t
*io_ctx
,
547 BlockdevOptionsRbd
*opts
, bool cache
,
548 const char *keypairs
, const char *secretid
,
551 char *mon_host
= NULL
;
552 Error
*local_err
= NULL
;
555 mon_host
= qemu_rbd_mon_host(opts
, &local_err
);
557 error_propagate(errp
, local_err
);
562 r
= rados_create(cluster
, opts
->user
);
564 error_setg_errno(errp
, -r
, "error initializing");
568 /* try default location when conf=NULL, but ignore failure */
569 r
= rados_conf_read_file(*cluster
, opts
->conf
);
570 if (opts
->has_conf
&& r
< 0) {
571 error_setg_errno(errp
, -r
, "error reading conf file %s", opts
->conf
);
572 goto failed_shutdown
;
575 r
= qemu_rbd_set_keypairs(*cluster
, keypairs
, errp
);
577 goto failed_shutdown
;
581 r
= rados_conf_set(*cluster
, "mon_host", mon_host
);
583 goto failed_shutdown
;
587 if (qemu_rbd_set_auth(*cluster
, secretid
, errp
) < 0) {
589 goto failed_shutdown
;
593 * Fallback to more conservative semantics if setting cache
594 * options fails. Ignore errors from setting rbd_cache because the
595 * only possible error is that the option does not exist, and
596 * librbd defaults to no caching. If write through caching cannot
597 * be set up, fall back to no caching.
600 rados_conf_set(*cluster
, "rbd_cache", "true");
602 rados_conf_set(*cluster
, "rbd_cache", "false");
605 r
= rados_connect(*cluster
);
607 error_setg_errno(errp
, -r
, "error connecting");
608 goto failed_shutdown
;
611 r
= rados_ioctx_create(*cluster
, opts
->pool
, io_ctx
);
613 error_setg_errno(errp
, -r
, "error opening pool %s", opts
->pool
);
614 goto failed_shutdown
;
620 rados_shutdown(*cluster
);
626 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
629 BDRVRBDState
*s
= bs
->opaque
;
630 BlockdevOptionsRbd
*opts
= NULL
;
632 QObject
*crumpled
= NULL
;
634 Error
*local_err
= NULL
;
635 const char *filename
;
636 char *keypairs
, *secretid
;
639 /* If we are given a filename, parse the filename, with precedence given to
640 * filename encoded options */
641 filename
= qdict_get_try_str(options
, "filename");
643 warn_report("'filename' option specified. "
644 "This is an unsupported option, and may be deprecated "
646 qemu_rbd_parse_filename(filename
, options
, &local_err
);
647 qdict_del(options
, "filename");
649 error_propagate(errp
, local_err
);
654 keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
656 qdict_del(options
, "=keyvalue-pairs");
659 secretid
= g_strdup(qdict_get_try_str(options
, "password-secret"));
661 qdict_del(options
, "password-secret");
664 /* Convert the remaining options into a QAPI object */
665 crumpled
= qdict_crumple(options
, errp
);
666 if (crumpled
== NULL
) {
671 v
= qobject_input_visitor_new_keyval(crumpled
);
672 visit_type_BlockdevOptionsRbd(v
, NULL
, &opts
, &local_err
);
674 qobject_unref(crumpled
);
677 error_propagate(errp
, local_err
);
682 /* Remove the processed options from the QDict (the visitor processes
683 * _all_ options in the QDict) */
684 while ((e
= qdict_first(options
))) {
685 qdict_del(options
, e
->key
);
688 r
= qemu_rbd_connect(&s
->cluster
, &s
->io_ctx
, opts
,
689 !(flags
& BDRV_O_NOCACHE
), keypairs
, secretid
, errp
);
694 s
->snap
= g_strdup(opts
->snapshot
);
695 s
->image_name
= g_strdup(opts
->image
);
697 /* rbd_open is always r/w */
698 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
700 error_setg_errno(errp
, -r
, "error reading header from %s",
705 /* If we are using an rbd snapshot, we must be r/o, otherwise
707 if (s
->snap
!= NULL
) {
708 if (!bdrv_is_read_only(bs
)) {
709 error_report("Opening rbd snapshots without an explicit "
710 "read-only=on option is deprecated. Future versions "
711 "will refuse to open the image instead of "
712 "automatically marking the image read-only.");
713 r
= bdrv_set_read_only(bs
, true, &local_err
);
715 error_propagate(errp
, local_err
);
725 rados_ioctx_destroy(s
->io_ctx
);
727 g_free(s
->image_name
);
728 rados_shutdown(s
->cluster
);
730 qapi_free_BlockdevOptionsRbd(opts
);
737 /* Since RBD is currently always opened R/W via the API,
738 * we just need to check if we are using a snapshot or not, in
739 * order to determine if we will allow it to be R/W */
740 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
741 BlockReopenQueue
*queue
, Error
**errp
)
743 BDRVRBDState
*s
= state
->bs
->opaque
;
746 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
748 "Cannot change node '%s' to r/w when using RBD snapshot",
749 bdrv_get_device_or_node_name(state
->bs
));
756 static void qemu_rbd_close(BlockDriverState
*bs
)
758 BDRVRBDState
*s
= bs
->opaque
;
761 rados_ioctx_destroy(s
->io_ctx
);
763 g_free(s
->image_name
);
764 rados_shutdown(s
->cluster
);
767 static const AIOCBInfo rbd_aiocb_info
= {
768 .aiocb_size
= sizeof(RBDAIOCB
),
771 static void rbd_finish_bh(void *opaque
)
773 RADOSCB
*rcb
= opaque
;
774 qemu_rbd_complete_aio(rcb
);
778 * This is the callback function for rbd_aio_read and _write
780 * Note: this function is being called from a non qemu thread so
781 * we need to be careful about what we do here. Generally we only
782 * schedule a BH, and do the rest of the io completion handling
783 * from rbd_finish_bh() which runs in a qemu context.
785 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
787 RBDAIOCB
*acb
= rcb
->acb
;
789 rcb
->ret
= rbd_aio_get_return_value(c
);
792 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
796 static int rbd_aio_discard_wrapper(rbd_image_t image
,
799 rbd_completion_t comp
)
801 #ifdef LIBRBD_SUPPORTS_DISCARD
802 return rbd_aio_discard(image
, off
, len
, comp
);
808 static int rbd_aio_flush_wrapper(rbd_image_t image
,
809 rbd_completion_t comp
)
811 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
812 return rbd_aio_flush(image
, comp
);
818 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
822 BlockCompletionFunc
*cb
,
831 BDRVRBDState
*s
= bs
->opaque
;
833 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
836 assert(!qiov
|| qiov
->size
== size
);
838 rcb
= g_new(RADOSCB
, 1);
840 if (!LIBRBD_USE_IOVEC
) {
841 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
844 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
845 if (acb
->bounce
== NULL
) {
849 if (cmd
== RBD_AIO_WRITE
) {
850 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
852 rcb
->buf
= acb
->bounce
;
862 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
869 #ifdef LIBRBD_SUPPORTS_IOVEC
870 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
872 r
= rbd_aio_write(s
->image
, off
, size
, rcb
->buf
, c
);
876 #ifdef LIBRBD_SUPPORTS_IOVEC
877 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
879 r
= rbd_aio_read(s
->image
, off
, size
, rcb
->buf
, c
);
882 case RBD_AIO_DISCARD
:
883 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
886 r
= rbd_aio_flush_wrapper(s
->image
, c
);
893 goto failed_completion
;
901 if (!LIBRBD_USE_IOVEC
) {
902 qemu_vfree(acb
->bounce
);
909 static BlockAIOCB
*qemu_rbd_aio_preadv(BlockDriverState
*bs
,
910 uint64_t offset
, uint64_t bytes
,
911 QEMUIOVector
*qiov
, int flags
,
912 BlockCompletionFunc
*cb
,
915 return rbd_start_aio(bs
, offset
, qiov
, bytes
, cb
, opaque
,
919 static BlockAIOCB
*qemu_rbd_aio_pwritev(BlockDriverState
*bs
,
920 uint64_t offset
, uint64_t bytes
,
921 QEMUIOVector
*qiov
, int flags
,
922 BlockCompletionFunc
*cb
,
925 return rbd_start_aio(bs
, offset
, qiov
, bytes
, cb
, opaque
,
929 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
930 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
931 BlockCompletionFunc
*cb
,
934 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
939 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
941 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
942 /* rbd_flush added in 0.1.1 */
943 BDRVRBDState
*s
= bs
->opaque
;
944 return rbd_flush(s
->image
);
951 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
953 BDRVRBDState
*s
= bs
->opaque
;
954 rbd_image_info_t info
;
957 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
962 bdi
->cluster_size
= info
.obj_size
;
966 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
968 BDRVRBDState
*s
= bs
->opaque
;
969 rbd_image_info_t info
;
972 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
980 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
,
981 PreallocMode prealloc
, Error
**errp
)
983 BDRVRBDState
*s
= bs
->opaque
;
986 if (prealloc
!= PREALLOC_MODE_OFF
) {
987 error_setg(errp
, "Unsupported preallocation mode '%s'",
988 PreallocMode_str(prealloc
));
992 r
= rbd_resize(s
->image
, offset
);
994 error_setg_errno(errp
, -r
, "Failed to resize file");
1001 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
1002 QEMUSnapshotInfo
*sn_info
)
1004 BDRVRBDState
*s
= bs
->opaque
;
1007 if (sn_info
->name
[0] == '\0') {
1008 return -EINVAL
; /* we need a name for rbd snapshots */
1012 * rbd snapshots are using the name as the user controlled unique identifier
1013 * we can't use the rbd snapid for that purpose, as it can't be set
1015 if (sn_info
->id_str
[0] != '\0' &&
1016 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
1020 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
1024 r
= rbd_snap_create(s
->image
, sn_info
->name
);
1026 error_report("failed to create snap: %s", strerror(-r
));
1033 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1034 const char *snapshot_id
,
1035 const char *snapshot_name
,
1038 BDRVRBDState
*s
= bs
->opaque
;
1041 if (!snapshot_name
) {
1042 error_setg(errp
, "rbd need a valid snapshot name");
1046 /* If snapshot_id is specified, it must be equal to name, see
1047 qemu_rbd_snap_list() */
1048 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1050 "rbd do not support snapshot id, it should be NULL or "
1051 "equal to snapshot name");
1055 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1057 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1062 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1063 const char *snapshot_name
)
1065 BDRVRBDState
*s
= bs
->opaque
;
1067 return rbd_snap_rollback(s
->image
, snapshot_name
);
1070 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1071 QEMUSnapshotInfo
**psn_tab
)
1073 BDRVRBDState
*s
= bs
->opaque
;
1074 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1076 rbd_snap_info_t
*snaps
;
1077 int max_snaps
= RBD_MAX_SNAPS
;
1080 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1081 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1082 if (snap_count
<= 0) {
1085 } while (snap_count
== -ERANGE
);
1087 if (snap_count
<= 0) {
1091 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1093 for (i
= 0; i
< snap_count
; i
++) {
1094 const char *snap_name
= snaps
[i
].name
;
1096 sn_info
= sn_tab
+ i
;
1097 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1098 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1100 sn_info
->vm_state_size
= snaps
[i
].size
;
1101 sn_info
->date_sec
= 0;
1102 sn_info
->date_nsec
= 0;
1103 sn_info
->vm_clock_nsec
= 0;
1105 rbd_snap_list_end(snaps
);
1113 #ifdef LIBRBD_SUPPORTS_DISCARD
1114 static BlockAIOCB
*qemu_rbd_aio_pdiscard(BlockDriverState
*bs
,
1117 BlockCompletionFunc
*cb
,
1120 return rbd_start_aio(bs
, offset
, NULL
, bytes
, cb
, opaque
,
1125 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1126 static void coroutine_fn
qemu_rbd_co_invalidate_cache(BlockDriverState
*bs
,
1129 BDRVRBDState
*s
= bs
->opaque
;
1130 int r
= rbd_invalidate_cache(s
->image
);
1132 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1137 static QemuOptsList qemu_rbd_create_opts
= {
1138 .name
= "rbd-create-opts",
1139 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1142 .name
= BLOCK_OPT_SIZE
,
1143 .type
= QEMU_OPT_SIZE
,
1144 .help
= "Virtual disk size"
1147 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1148 .type
= QEMU_OPT_SIZE
,
1149 .help
= "RBD object size"
1152 .name
= "password-secret",
1153 .type
= QEMU_OPT_STRING
,
1154 .help
= "ID of secret providing the password",
1156 { /* end of list */ }
1160 static BlockDriver bdrv_rbd
= {
1161 .format_name
= "rbd",
1162 .instance_size
= sizeof(BDRVRBDState
),
1163 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1164 .bdrv_refresh_limits
= qemu_rbd_refresh_limits
,
1165 .bdrv_file_open
= qemu_rbd_open
,
1166 .bdrv_close
= qemu_rbd_close
,
1167 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1168 .bdrv_co_create
= qemu_rbd_co_create
,
1169 .bdrv_co_create_opts
= qemu_rbd_co_create_opts
,
1170 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1171 .bdrv_get_info
= qemu_rbd_getinfo
,
1172 .create_opts
= &qemu_rbd_create_opts
,
1173 .bdrv_getlength
= qemu_rbd_getlength
,
1174 .bdrv_truncate
= qemu_rbd_truncate
,
1175 .protocol_name
= "rbd",
1177 .bdrv_aio_preadv
= qemu_rbd_aio_preadv
,
1178 .bdrv_aio_pwritev
= qemu_rbd_aio_pwritev
,
1180 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1181 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
1183 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1186 #ifdef LIBRBD_SUPPORTS_DISCARD
1187 .bdrv_aio_pdiscard
= qemu_rbd_aio_pdiscard
,
1190 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1191 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1192 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1193 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1194 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1195 .bdrv_co_invalidate_cache
= qemu_rbd_co_invalidate_cache
,
1199 static void bdrv_rbd_init(void)
1201 bdrv_register(&bdrv_rbd
);
1204 block_init(bdrv_rbd_init
);