2 * QEMU Block driver for RADOS (Ceph)
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/module.h"
20 #include "qemu/option.h"
21 #include "block/block_int.h"
22 #include "block/qdict.h"
23 #include "crypto/secret.h"
24 #include "qemu/cutils.h"
25 #include "qapi/qmp/qstring.h"
26 #include "qapi/qmp/qdict.h"
27 #include "qapi/qmp/qjson.h"
28 #include "qapi/qmp/qlist.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/qapi-visit-block-core.h"
33 * When specifying the image filename use:
35 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
37 * poolname must be the name of an existing rados pool.
39 * devicename is the name of the rbd image.
41 * Each option given is used to configure rados, and may be any valid
42 * Ceph option, "id", or "conf".
44 * The "id" option indicates what user we should authenticate as to
45 * the Ceph cluster. If it is excluded we will use the Ceph default
48 * The "conf" option specifies a Ceph configuration file to read. If
49 * it is not specified, we will read from the default Ceph locations
50 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
51 * file, specify conf=/dev/null.
53 * Configuration values containing :, @, or = can be escaped with a
57 /* rbd_aio_discard added in 0.1.2 */
58 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
59 #define LIBRBD_SUPPORTS_DISCARD
61 #undef LIBRBD_SUPPORTS_DISCARD
64 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
66 #define RBD_MAX_SNAPS 100
68 /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
69 #ifdef LIBRBD_SUPPORTS_IOVEC
70 #define LIBRBD_USE_IOVEC 1
72 #define LIBRBD_USE_IOVEC 0
82 typedef struct RBDAIOCB {
89 struct BDRVRBDState *s;
92 typedef struct RADOSCB {
94 struct BDRVRBDState *s;
100 typedef struct BDRVRBDState {
102 rados_ioctx_t io_ctx;
108 static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
109 BlockdevOptionsRbd *opts, bool cache,
110 const char *keypairs, const char *secretid,
113 static char *qemu_rbd_next_tok(char *src, char delim, char **p)
119 for (end = src; *end; ++end) {
123 if (*end == '\\' && end[1] != '\0') {
134 static void qemu_rbd_unescape(char *src)
138 for (p = src; *src; ++src, ++p) {
139 if (*src == '\\' && src[1] != '\0') {
147 static void qemu_rbd_parse_filename(const char *filename, QDict *options,
152 QList *keypairs = NULL;
155 if (!strstart(filename, "rbd:", &start)) {
156 error_setg(errp, "File name must start with 'rbd:'");
160 buf = g_strdup(start);
163 found_str = qemu_rbd_next_tok(p, '/', &p);
165 error_setg(errp, "Pool name is required");
168 qemu_rbd_unescape(found_str);
169 qdict_put_str(options, "pool", found_str);
171 if (strchr(p, '@')) {
172 found_str = qemu_rbd_next_tok(p, '@', &p);
173 qemu_rbd_unescape(found_str);
174 qdict_put_str(options, "image", found_str);
176 found_str = qemu_rbd_next_tok(p, ':', &p);
177 qemu_rbd_unescape(found_str);
178 qdict_put_str(options, "snapshot", found_str);
180 found_str = qemu_rbd_next_tok(p, ':', &p);
181 qemu_rbd_unescape(found_str);
182 qdict_put_str(options, "image", found_str);
188 /* The following are essentially all key/value pairs, and we treat
189 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
192 name = qemu_rbd_next_tok(p, '=', &p);
194 error_setg(errp, "conf option %s has no value", name);
198 qemu_rbd_unescape(name);
200 value = qemu_rbd_next_tok(p, ':', &p);
201 qemu_rbd_unescape(value);
203 if (!strcmp(name, "conf")) {
204 qdict_put_str(options, "conf", value);
205 } else if (!strcmp(name, "id")) {
206 qdict_put_str(options, "user", value);
209 * We pass these internally to qemu_rbd_set_keypairs(), so
210 * we can get away with the simpler list of [ "key1",
211 * "value1", "key2", "value2" ] rather than a raw dict
212 * { "key1": "value1", "key2": "value2" } where we can't
213 * guarantee order, or even a more correct but complex
214 * [ { "key1": "value1" }, { "key2": "value2" } ]
217 keypairs = qlist_new();
219 qlist_append_str(keypairs, name);
220 qlist_append_str(keypairs, value);
225 qdict_put(options, "=keyvalue-pairs",
226 qobject_to_json(QOBJECT(keypairs)));
231 qobject_unref(keypairs);
236 static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
238 /* XXX Does RBD support AIO on less than 512-byte alignment? */
239 bs->bl.request_alignment = 512;
243 static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
249 RbdAuthModeList *auth;
251 if (opts->key_secret) {
252 key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
256 r = rados_conf_set(cluster, "key", key);
259 error_setg_errno(errp, -r, "Could not set 'key'");
264 if (opts->has_auth_client_required) {
265 accu = g_string_new("");
266 for (auth = opts->auth_client_required; auth; auth = auth->next) {
268 g_string_append_c(accu, ';');
270 g_string_append(accu, RbdAuthMode_str(auth->value));
272 acr = g_string_free(accu, FALSE);
273 r = rados_conf_set(cluster, "auth_client_required", acr);
276 error_setg_errno(errp, -r,
277 "Could not set 'auth_client_required'");
285 static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
295 if (!keypairs_json) {
298 keypairs = qobject_to(QList,
299 qobject_from_json(keypairs_json, &error_abort));
300 remaining = qlist_size(keypairs) / 2;
303 while (remaining--) {
304 name = qobject_to(QString, qlist_pop(keypairs));
305 value = qobject_to(QString, qlist_pop(keypairs));
306 assert(name && value);
307 key = qstring_get_str(name);
309 ret = rados_conf_set(cluster, key, qstring_get_str(value));
310 qobject_unref(value);
312 error_setg_errno(errp, -ret, "invalid conf option %s", key);
320 qobject_unref(keypairs);
324 static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
326 if (LIBRBD_USE_IOVEC) {
327 RBDAIOCB *acb = rcb->acb;
328 iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
329 acb->qiov->size - offs);
331 memset(rcb->buf + offs, 0, rcb->size - offs);
335 static QemuOptsList runtime_opts = {
337 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
341 .type = QEMU_OPT_STRING,
342 .help = "Rados pool name",
346 .type = QEMU_OPT_STRING,
347 .help = "Image name in the pool",
351 .type = QEMU_OPT_STRING,
352 .help = "Rados config file location",
356 .type = QEMU_OPT_STRING,
357 .help = "Ceph snapshot name",
360 /* maps to 'id' in rados_create() */
362 .type = QEMU_OPT_STRING,
363 .help = "Rados id name",
366 * server.* extracted manually, see qemu_rbd_mon_host()
368 { /* end of list */ }
372 /* FIXME Deprecate and remove keypairs or make it available in QMP. */
373 static int qemu_rbd_do_create(BlockdevCreateOptions *options,
374 const char *keypairs, const char *password_secret,
377 BlockdevCreateOptionsRbd *opts = &options->u.rbd;
379 rados_ioctx_t io_ctx;
383 assert(options->driver == BLOCKDEV_DRIVER_RBD);
384 if (opts->location->has_snapshot) {
385 error_setg(errp, "Can't use snapshot name for image creation");
389 if (opts->has_cluster_size) {
390 int64_t objsize = opts->cluster_size;
391 if ((objsize - 1) & objsize) { /* not a power of 2? */
392 error_setg(errp, "obj size needs to be power of 2");
395 if (objsize < 4096) {
396 error_setg(errp, "obj size too small");
399 obj_order = ctz32(objsize);
402 ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
403 password_secret, errp);
408 ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
410 error_setg_errno(errp, -ret, "error rbd create");
416 rados_ioctx_destroy(io_ctx);
417 rados_shutdown(cluster);
421 static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
423 return qemu_rbd_do_create(options, NULL, NULL, errp);
426 static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
430 BlockdevCreateOptions *create_options;
431 BlockdevCreateOptionsRbd *rbd_opts;
432 BlockdevOptionsRbd *loc;
433 Error *local_err = NULL;
434 const char *keypairs, *password_secret;
435 QDict *options = NULL;
438 create_options = g_new0(BlockdevCreateOptions, 1);
439 create_options->driver = BLOCKDEV_DRIVER_RBD;
440 rbd_opts = &create_options->u.rbd;
442 rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
444 password_secret = qemu_opt_get(opts, "password-secret");
446 /* Read out options */
447 rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
449 rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
450 BLOCK_OPT_CLUSTER_SIZE, 0);
451 rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
453 options = qdict_new();
454 qemu_rbd_parse_filename(filename, options, &local_err);
457 error_propagate(errp, local_err);
462 * Caution: while qdict_get_try_str() is fine, getting non-string
463 * types would require more care. When @options come from -blockdev
464 * or blockdev_add, its members are typed according to the QAPI
465 * schema, but when they come from -drive, they're all QString.
467 loc = rbd_opts->location;
468 loc->pool = g_strdup(qdict_get_try_str(options, "pool"));
469 loc->conf = g_strdup(qdict_get_try_str(options, "conf"));
470 loc->has_conf = !!loc->conf;
471 loc->user = g_strdup(qdict_get_try_str(options, "user"));
472 loc->has_user = !!loc->user;
473 loc->image = g_strdup(qdict_get_try_str(options, "image"));
474 keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
476 ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
482 qobject_unref(options);
483 qapi_free_BlockdevCreateOptions(create_options);
488 * This aio completion is being called from rbd_finish_bh() and runs in qemu
491 static void qemu_rbd_complete_aio(RADOSCB *rcb)
493 RBDAIOCB *acb = rcb->acb;
498 if (acb->cmd != RBD_AIO_READ) {
502 } else if (!acb->error) {
503 acb->ret = rcb->size;
507 qemu_rbd_memset(rcb, 0);
510 } else if (r < rcb->size) {
511 qemu_rbd_memset(rcb, r);
513 acb->ret = rcb->size;
515 } else if (!acb->error) {
522 if (!LIBRBD_USE_IOVEC) {
523 if (acb->cmd == RBD_AIO_READ) {
524 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
526 qemu_vfree(acb->bounce);
529 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
534 static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
537 const char *host, *port;
539 InetSocketAddressBaseList *p;
542 if (!opts->has_server) {
546 for (cnt = 0, p = opts->server; p; p = p->next) {
550 vals = g_new(const char *, cnt + 1);
552 for (i = 0, p = opts->server; p; p = p->next, i++) {
553 host = p->value->host;
554 port = p->value->port;
556 if (strchr(host, ':')) {
557 vals[i] = g_strdup_printf("[%s]:%s", host, port);
559 vals[i] = g_strdup_printf("%s:%s", host, port);
564 rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
565 g_strfreev((char **)vals);
569 static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
570 BlockdevOptionsRbd *opts, bool cache,
571 const char *keypairs, const char *secretid,
574 char *mon_host = NULL;
575 Error *local_err = NULL;
579 if (opts->key_secret) {
581 "Legacy 'password-secret' clashes with 'key-secret'");
584 opts->key_secret = g_strdup(secretid);
585 opts->has_key_secret = true;
588 mon_host = qemu_rbd_mon_host(opts, &local_err);
590 error_propagate(errp, local_err);
595 r = rados_create(cluster, opts->user);
597 error_setg_errno(errp, -r, "error initializing");
601 /* try default location when conf=NULL, but ignore failure */
602 r = rados_conf_read_file(*cluster, opts->conf);
603 if (opts->has_conf && r < 0) {
604 error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
605 goto failed_shutdown;
608 r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
610 goto failed_shutdown;
614 r = rados_conf_set(*cluster, "mon_host", mon_host);
616 goto failed_shutdown;
620 r = qemu_rbd_set_auth(*cluster, opts, errp);
622 goto failed_shutdown;
626 * Fallback to more conservative semantics if setting cache
627 * options fails. Ignore errors from setting rbd_cache because the
628 * only possible error is that the option does not exist, and
629 * librbd defaults to no caching. If write through caching cannot
630 * be set up, fall back to no caching.
633 rados_conf_set(*cluster, "rbd_cache", "true");
635 rados_conf_set(*cluster, "rbd_cache", "false");
638 r = rados_connect(*cluster);
640 error_setg_errno(errp, -r, "error connecting");
641 goto failed_shutdown;
644 r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
646 error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
647 goto failed_shutdown;
653 rados_shutdown(*cluster);
659 static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
663 Error *local_err = NULL;
665 /* Convert the remaining options into a QAPI object */
666 v = qobject_input_visitor_new_flat_confused(options, errp);
671 visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err);
675 error_propagate(errp, local_err);
682 static int qemu_rbd_attempt_legacy_options(QDict *options,
683 BlockdevOptionsRbd **opts,
689 filename = g_strdup(qdict_get_try_str(options, "filename"));
693 qdict_del(options, "filename");
695 qemu_rbd_parse_filename(filename, options, NULL);
697 /* keypairs freed by caller */
698 *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
700 qdict_del(options, "=keyvalue-pairs");
703 r = qemu_rbd_convert_options(options, opts, NULL);
709 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
712 BDRVRBDState *s = bs->opaque;
713 BlockdevOptionsRbd *opts = NULL;
715 Error *local_err = NULL;
716 char *keypairs, *secretid;
719 keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
721 qdict_del(options, "=keyvalue-pairs");
724 secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
726 qdict_del(options, "password-secret");
729 r = qemu_rbd_convert_options(options, &opts, &local_err);
731 /* If keypairs are present, that means some options are present in
732 * the modern option format. Don't attempt to parse legacy option
733 * formats, as we won't support mixed usage. */
735 error_propagate(errp, local_err);
739 /* If the initial attempt to convert and process the options failed,
740 * we may be attempting to open an image file that has the rbd options
741 * specified in the older format consisting of all key/value pairs
742 * encoded in the filename. Go ahead and attempt to parse the
743 * filename, and see if we can pull out the required options. */
744 r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
746 /* Propagate the original error, not the legacy parsing fallback
747 * error, as the latter was just a best-effort attempt. */
748 error_propagate(errp, local_err);
751 /* Take care whenever deciding to actually deprecate; once this ability
752 * is removed, we will not be able to open any images with legacy-styled
753 * backing image strings. */
754 warn_report("RBD options encoded in the filename as keyvalue pairs "
758 /* Remove the processed options from the QDict (the visitor processes
759 * _all_ options in the QDict) */
760 while ((e = qdict_first(options))) {
761 qdict_del(options, e->key);
764 r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
765 !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
770 s->snap = g_strdup(opts->snapshot);
771 s->image_name = g_strdup(opts->image);
773 /* rbd_open is always r/w */
774 r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
776 error_setg_errno(errp, -r, "error reading header from %s",
781 /* If we are using an rbd snapshot, we must be r/o, otherwise
783 if (s->snap != NULL) {
784 r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
795 rados_ioctx_destroy(s->io_ctx);
797 g_free(s->image_name);
798 rados_shutdown(s->cluster);
800 qapi_free_BlockdevOptionsRbd(opts);
807 /* Since RBD is currently always opened R/W via the API,
808 * we just need to check if we are using a snapshot or not, in
809 * order to determine if we will allow it to be R/W */
810 static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
811 BlockReopenQueue *queue, Error **errp)
813 BDRVRBDState *s = state->bs->opaque;
816 if (s->snap && state->flags & BDRV_O_RDWR) {
818 "Cannot change node '%s' to r/w when using RBD snapshot",
819 bdrv_get_device_or_node_name(state->bs));
826 static void qemu_rbd_close(BlockDriverState *bs)
828 BDRVRBDState *s = bs->opaque;
831 rados_ioctx_destroy(s->io_ctx);
833 g_free(s->image_name);
834 rados_shutdown(s->cluster);
837 static const AIOCBInfo rbd_aiocb_info = {
838 .aiocb_size = sizeof(RBDAIOCB),
841 static void rbd_finish_bh(void *opaque)
843 RADOSCB *rcb = opaque;
844 qemu_rbd_complete_aio(rcb);
848 * This is the callback function for rbd_aio_read and _write
850 * Note: this function is being called from a non qemu thread so
851 * we need to be careful about what we do here. Generally we only
852 * schedule a BH, and do the rest of the io completion handling
853 * from rbd_finish_bh() which runs in a qemu context.
855 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
857 RBDAIOCB *acb = rcb->acb;
859 rcb->ret = rbd_aio_get_return_value(c);
862 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
866 static int rbd_aio_discard_wrapper(rbd_image_t image,
869 rbd_completion_t comp)
871 #ifdef LIBRBD_SUPPORTS_DISCARD
872 return rbd_aio_discard(image, off, len, comp);
878 static int rbd_aio_flush_wrapper(rbd_image_t image,
879 rbd_completion_t comp)
881 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
882 return rbd_aio_flush(image, comp);
888 static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
892 BlockCompletionFunc *cb,
901 BDRVRBDState *s = bs->opaque;
903 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
906 assert(!qiov || qiov->size == size);
908 rcb = g_new(RADOSCB, 1);
910 if (!LIBRBD_USE_IOVEC) {
911 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
914 acb->bounce = qemu_try_blockalign(bs, qiov->size);
915 if (acb->bounce == NULL) {
919 if (cmd == RBD_AIO_WRITE) {
920 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
922 rcb->buf = acb->bounce;
932 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
939 #ifdef LIBRBD_SUPPORTS_IOVEC
940 r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
942 r = rbd_aio_write(s->image, off, size, rcb->buf, c);
946 #ifdef LIBRBD_SUPPORTS_IOVEC
947 r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
949 r = rbd_aio_read(s->image, off, size, rcb->buf, c);
952 case RBD_AIO_DISCARD:
953 r = rbd_aio_discard_wrapper(s->image, off, size, c);
956 r = rbd_aio_flush_wrapper(s->image, c);
963 goto failed_completion;
971 if (!LIBRBD_USE_IOVEC) {
972 qemu_vfree(acb->bounce);
979 static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
980 uint64_t offset, uint64_t bytes,
981 QEMUIOVector *qiov, int flags,
982 BlockCompletionFunc *cb,
985 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
989 static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
990 uint64_t offset, uint64_t bytes,
991 QEMUIOVector *qiov, int flags,
992 BlockCompletionFunc *cb,
995 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
999 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1000 static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
1001 BlockCompletionFunc *cb,
1004 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
1009 static int qemu_rbd_co_flush(BlockDriverState *bs)
1011 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
1012 /* rbd_flush added in 0.1.1 */
1013 BDRVRBDState *s = bs->opaque;
1014 return rbd_flush(s->image);
1021 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1023 BDRVRBDState *s = bs->opaque;
1024 rbd_image_info_t info;
1027 r = rbd_stat(s->image, &info, sizeof(info));
1032 bdi->cluster_size = info.obj_size;
1036 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1038 BDRVRBDState *s = bs->opaque;
1039 rbd_image_info_t info;
1042 r = rbd_stat(s->image, &info, sizeof(info));
1050 static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1052 PreallocMode prealloc,
1055 BDRVRBDState *s = bs->opaque;
1058 if (prealloc != PREALLOC_MODE_OFF) {
1059 error_setg(errp, "Unsupported preallocation mode '%s'",
1060 PreallocMode_str(prealloc));
1064 r = rbd_resize(s->image, offset);
1066 error_setg_errno(errp, -r, "Failed to resize file");
1073 static int qemu_rbd_snap_create(BlockDriverState *bs,
1074 QEMUSnapshotInfo *sn_info)
1076 BDRVRBDState *s = bs->opaque;
1079 if (sn_info->name[0] == '\0') {
1080 return -EINVAL; /* we need a name for rbd snapshots */
1084 * rbd snapshots are using the name as the user controlled unique identifier
1085 * we can't use the rbd snapid for that purpose, as it can't be set
1087 if (sn_info->id_str[0] != '\0' &&
1088 strcmp(sn_info->id_str, sn_info->name) != 0) {
1092 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1096 r = rbd_snap_create(s->image, sn_info->name);
1098 error_report("failed to create snap: %s", strerror(-r));
1105 static int qemu_rbd_snap_remove(BlockDriverState *bs,
1106 const char *snapshot_id,
1107 const char *snapshot_name,
1110 BDRVRBDState *s = bs->opaque;
1113 if (!snapshot_name) {
1114 error_setg(errp, "rbd need a valid snapshot name");
1118 /* If snapshot_id is specified, it must be equal to name, see
1119 qemu_rbd_snap_list() */
1120 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1122 "rbd do not support snapshot id, it should be NULL or "
1123 "equal to snapshot name");
1127 r = rbd_snap_remove(s->image, snapshot_name);
1129 error_setg_errno(errp, -r, "Failed to remove the snapshot");
1134 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1135 const char *snapshot_name)
1137 BDRVRBDState *s = bs->opaque;
1139 return rbd_snap_rollback(s->image, snapshot_name);
1142 static int qemu_rbd_snap_list(BlockDriverState *bs,
1143 QEMUSnapshotInfo **psn_tab)
1145 BDRVRBDState *s = bs->opaque;
1146 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1148 rbd_snap_info_t *snaps;
1149 int max_snaps = RBD_MAX_SNAPS;
1152 snaps = g_new(rbd_snap_info_t, max_snaps);
1153 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
1154 if (snap_count <= 0) {
1157 } while (snap_count == -ERANGE);
1159 if (snap_count <= 0) {
1163 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1165 for (i = 0; i < snap_count; i++) {
1166 const char *snap_name = snaps[i].name;
1168 sn_info = sn_tab + i;
1169 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1170 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1172 sn_info->vm_state_size = snaps[i].size;
1173 sn_info->date_sec = 0;
1174 sn_info->date_nsec = 0;
1175 sn_info->vm_clock_nsec = 0;
1177 rbd_snap_list_end(snaps);
1185 #ifdef LIBRBD_SUPPORTS_DISCARD
1186 static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
1189 BlockCompletionFunc *cb,
1192 return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1197 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1198 static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1201 BDRVRBDState *s = bs->opaque;
1202 int r = rbd_invalidate_cache(s->image);
1204 error_setg_errno(errp, -r, "Failed to invalidate the cache");
1209 static QemuOptsList qemu_rbd_create_opts = {
1210 .name = "rbd-create-opts",
1211 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1214 .name = BLOCK_OPT_SIZE,
1215 .type = QEMU_OPT_SIZE,
1216 .help = "Virtual disk size"
1219 .name = BLOCK_OPT_CLUSTER_SIZE,
1220 .type = QEMU_OPT_SIZE,
1221 .help = "RBD object size"
1224 .name = "password-secret",
1225 .type = QEMU_OPT_STRING,
1226 .help = "ID of secret providing the password",
1228 { /* end of list */ }
1232 static const char *const qemu_rbd_strong_runtime_opts[] = {
1244 static BlockDriver bdrv_rbd = {
1245 .format_name = "rbd",
1246 .instance_size = sizeof(BDRVRBDState),
1247 .bdrv_parse_filename = qemu_rbd_parse_filename,
1248 .bdrv_refresh_limits = qemu_rbd_refresh_limits,
1249 .bdrv_file_open = qemu_rbd_open,
1250 .bdrv_close = qemu_rbd_close,
1251 .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
1252 .bdrv_co_create = qemu_rbd_co_create,
1253 .bdrv_co_create_opts = qemu_rbd_co_create_opts,
1254 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1255 .bdrv_get_info = qemu_rbd_getinfo,
1256 .create_opts = &qemu_rbd_create_opts,
1257 .bdrv_getlength = qemu_rbd_getlength,
1258 .bdrv_co_truncate = qemu_rbd_co_truncate,
1259 .protocol_name = "rbd",
1261 .bdrv_aio_preadv = qemu_rbd_aio_preadv,
1262 .bdrv_aio_pwritev = qemu_rbd_aio_pwritev,
1264 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1265 .bdrv_aio_flush = qemu_rbd_aio_flush,
1267 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
1270 #ifdef LIBRBD_SUPPORTS_DISCARD
1271 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
1274 .bdrv_snapshot_create = qemu_rbd_snap_create,
1275 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1276 .bdrv_snapshot_list = qemu_rbd_snap_list,
1277 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
1278 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1279 .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1282 .strong_runtime_opts = qemu_rbd_strong_runtime_opts,
1285 static void bdrv_rbd_init(void)
1287 bdrv_register(&bdrv_rbd);
1290 block_init(bdrv_rbd_init);