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/option.h"
20 #include "block/block_int.h"
21 #include "block/qdict.h"
22 #include "crypto/secret.h"
23 #include "qemu/cutils.h"
24 #include "qapi/qmp/qstring.h"
25 #include "qapi/qmp/qdict.h"
26 #include "qapi/qmp/qjson.h"
27 #include "qapi/qmp/qlist.h"
28 #include "qapi/qobject-input-visitor.h"
29 #include "qapi/qapi-visit-block-core.h"
32 * When specifying the image filename use:
34 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
36 * poolname must be the name of an existing rados pool.
38 * devicename is the name of the rbd image.
40 * Each option given is used to configure rados, and may be any valid
41 * Ceph option, "id", or "conf".
43 * The "id" option indicates what user we should authenticate as to
44 * the Ceph cluster. If it is excluded we will use the Ceph default
47 * The "conf" option specifies a Ceph configuration file to read. If
48 * it is not specified, we will read from the default Ceph locations
49 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
50 * file, specify conf=/dev/null.
52 * Configuration values containing :, @, or = can be escaped with a
56 /* rbd_aio_discard added in 0.1.2 */
57 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
58 #define LIBRBD_SUPPORTS_DISCARD
60 #undef LIBRBD_SUPPORTS_DISCARD
63 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
65 #define RBD_MAX_SNAPS 100
67 /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
68 #ifdef LIBRBD_SUPPORTS_IOVEC
69 #define LIBRBD_USE_IOVEC 1
71 #define LIBRBD_USE_IOVEC 0
81 typedef struct RBDAIOCB {
88 struct BDRVRBDState *s;
91 typedef struct RADOSCB {
93 struct BDRVRBDState *s;
99 typedef struct BDRVRBDState {
101 rados_ioctx_t io_ctx;
107 static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
108 BlockdevOptionsRbd *opts, bool cache,
109 const char *keypairs, const char *secretid,
112 static char *qemu_rbd_next_tok(char *src, char delim, char **p)
118 for (end = src; *end; ++end) {
122 if (*end == '\\' && end[1] != '\0') {
133 static void qemu_rbd_unescape(char *src)
137 for (p = src; *src; ++src, ++p) {
138 if (*src == '\\' && src[1] != '\0') {
146 static void qemu_rbd_parse_filename(const char *filename, QDict *options,
151 QList *keypairs = NULL;
154 if (!strstart(filename, "rbd:", &start)) {
155 error_setg(errp, "File name must start with 'rbd:'");
159 buf = g_strdup(start);
162 found_str = qemu_rbd_next_tok(p, '/', &p);
164 error_setg(errp, "Pool name is required");
167 qemu_rbd_unescape(found_str);
168 qdict_put_str(options, "pool", found_str);
170 if (strchr(p, '@')) {
171 found_str = qemu_rbd_next_tok(p, '@', &p);
172 qemu_rbd_unescape(found_str);
173 qdict_put_str(options, "image", found_str);
175 found_str = qemu_rbd_next_tok(p, ':', &p);
176 qemu_rbd_unescape(found_str);
177 qdict_put_str(options, "snapshot", found_str);
179 found_str = qemu_rbd_next_tok(p, ':', &p);
180 qemu_rbd_unescape(found_str);
181 qdict_put_str(options, "image", found_str);
187 /* The following are essentially all key/value pairs, and we treat
188 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
191 name = qemu_rbd_next_tok(p, '=', &p);
193 error_setg(errp, "conf option %s has no value", name);
197 qemu_rbd_unescape(name);
199 value = qemu_rbd_next_tok(p, ':', &p);
200 qemu_rbd_unescape(value);
202 if (!strcmp(name, "conf")) {
203 qdict_put_str(options, "conf", value);
204 } else if (!strcmp(name, "id")) {
205 qdict_put_str(options, "user", value);
208 * We pass these internally to qemu_rbd_set_keypairs(), so
209 * we can get away with the simpler list of [ "key1",
210 * "value1", "key2", "value2" ] rather than a raw dict
211 * { "key1": "value1", "key2": "value2" } where we can't
212 * guarantee order, or even a more correct but complex
213 * [ { "key1": "value1" }, { "key2": "value2" } ]
216 keypairs = qlist_new();
218 qlist_append_str(keypairs, name);
219 qlist_append_str(keypairs, value);
224 qdict_put(options, "=keyvalue-pairs",
225 qobject_to_json(QOBJECT(keypairs)));
230 qobject_unref(keypairs);
235 static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
237 /* XXX Does RBD support AIO on less than 512-byte alignment? */
238 bs->bl.request_alignment = 512;
242 static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
248 RbdAuthModeList *auth;
250 if (opts->key_secret) {
251 key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
255 r = rados_conf_set(cluster, "key", key);
258 error_setg_errno(errp, -r, "Could not set 'key'");
263 if (opts->has_auth_client_required) {
264 accu = g_string_new("");
265 for (auth = opts->auth_client_required; auth; auth = auth->next) {
267 g_string_append_c(accu, ';');
269 g_string_append(accu, RbdAuthMode_str(auth->value));
271 acr = g_string_free(accu, FALSE);
272 r = rados_conf_set(cluster, "auth_client_required", acr);
275 error_setg_errno(errp, -r,
276 "Could not set 'auth_client_required'");
284 static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
294 if (!keypairs_json) {
297 keypairs = qobject_to(QList,
298 qobject_from_json(keypairs_json, &error_abort));
299 remaining = qlist_size(keypairs) / 2;
302 while (remaining--) {
303 name = qobject_to(QString, qlist_pop(keypairs));
304 value = qobject_to(QString, qlist_pop(keypairs));
305 assert(name && value);
306 key = qstring_get_str(name);
308 ret = rados_conf_set(cluster, key, qstring_get_str(value));
309 qobject_unref(value);
311 error_setg_errno(errp, -ret, "invalid conf option %s", key);
319 qobject_unref(keypairs);
323 static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
325 if (LIBRBD_USE_IOVEC) {
326 RBDAIOCB *acb = rcb->acb;
327 iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
328 acb->qiov->size - offs);
330 memset(rcb->buf + offs, 0, rcb->size - offs);
334 static QemuOptsList runtime_opts = {
336 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
340 .type = QEMU_OPT_STRING,
341 .help = "Rados pool name",
345 .type = QEMU_OPT_STRING,
346 .help = "Image name in the pool",
350 .type = QEMU_OPT_STRING,
351 .help = "Rados config file location",
355 .type = QEMU_OPT_STRING,
356 .help = "Ceph snapshot name",
359 /* maps to 'id' in rados_create() */
361 .type = QEMU_OPT_STRING,
362 .help = "Rados id name",
365 * server.* extracted manually, see qemu_rbd_mon_host()
367 { /* end of list */ }
371 /* FIXME Deprecate and remove keypairs or make it available in QMP. */
372 static int qemu_rbd_do_create(BlockdevCreateOptions *options,
373 const char *keypairs, const char *password_secret,
376 BlockdevCreateOptionsRbd *opts = &options->u.rbd;
378 rados_ioctx_t io_ctx;
382 assert(options->driver == BLOCKDEV_DRIVER_RBD);
383 if (opts->location->has_snapshot) {
384 error_setg(errp, "Can't use snapshot name for image creation");
388 if (opts->has_cluster_size) {
389 int64_t objsize = opts->cluster_size;
390 if ((objsize - 1) & objsize) { /* not a power of 2? */
391 error_setg(errp, "obj size needs to be power of 2");
394 if (objsize < 4096) {
395 error_setg(errp, "obj size too small");
398 obj_order = ctz32(objsize);
401 ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
402 password_secret, errp);
407 ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
409 error_setg_errno(errp, -ret, "error rbd create");
415 rados_ioctx_destroy(io_ctx);
416 rados_shutdown(cluster);
420 static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
422 return qemu_rbd_do_create(options, NULL, NULL, errp);
425 static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
429 BlockdevCreateOptions *create_options;
430 BlockdevCreateOptionsRbd *rbd_opts;
431 BlockdevOptionsRbd *loc;
432 Error *local_err = NULL;
433 const char *keypairs, *password_secret;
434 QDict *options = NULL;
437 create_options = g_new0(BlockdevCreateOptions, 1);
438 create_options->driver = BLOCKDEV_DRIVER_RBD;
439 rbd_opts = &create_options->u.rbd;
441 rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
443 password_secret = qemu_opt_get(opts, "password-secret");
445 /* Read out options */
446 rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
448 rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
449 BLOCK_OPT_CLUSTER_SIZE, 0);
450 rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
452 options = qdict_new();
453 qemu_rbd_parse_filename(filename, options, &local_err);
456 error_propagate(errp, local_err);
461 * Caution: while qdict_get_try_str() is fine, getting non-string
462 * types would require more care. When @options come from -blockdev
463 * or blockdev_add, its members are typed according to the QAPI
464 * schema, but when they come from -drive, they're all QString.
466 loc = rbd_opts->location;
467 loc->pool = g_strdup(qdict_get_try_str(options, "pool"));
468 loc->conf = g_strdup(qdict_get_try_str(options, "conf"));
469 loc->has_conf = !!loc->conf;
470 loc->user = g_strdup(qdict_get_try_str(options, "user"));
471 loc->has_user = !!loc->user;
472 loc->image = g_strdup(qdict_get_try_str(options, "image"));
473 keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
475 ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
481 qobject_unref(options);
482 qapi_free_BlockdevCreateOptions(create_options);
487 * This aio completion is being called from rbd_finish_bh() and runs in qemu
490 static void qemu_rbd_complete_aio(RADOSCB *rcb)
492 RBDAIOCB *acb = rcb->acb;
497 if (acb->cmd != RBD_AIO_READ) {
501 } else if (!acb->error) {
502 acb->ret = rcb->size;
506 qemu_rbd_memset(rcb, 0);
509 } else if (r < rcb->size) {
510 qemu_rbd_memset(rcb, r);
512 acb->ret = rcb->size;
514 } else if (!acb->error) {
521 if (!LIBRBD_USE_IOVEC) {
522 if (acb->cmd == RBD_AIO_READ) {
523 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
525 qemu_vfree(acb->bounce);
528 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
533 static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
536 const char *host, *port;
538 InetSocketAddressBaseList *p;
541 if (!opts->has_server) {
545 for (cnt = 0, p = opts->server; p; p = p->next) {
549 vals = g_new(const char *, cnt + 1);
551 for (i = 0, p = opts->server; p; p = p->next, i++) {
552 host = p->value->host;
553 port = p->value->port;
555 if (strchr(host, ':')) {
556 vals[i] = g_strdup_printf("[%s]:%s", host, port);
558 vals[i] = g_strdup_printf("%s:%s", host, port);
563 rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
564 g_strfreev((char **)vals);
568 static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
569 BlockdevOptionsRbd *opts, bool cache,
570 const char *keypairs, const char *secretid,
573 char *mon_host = NULL;
574 Error *local_err = NULL;
578 if (opts->key_secret) {
580 "Legacy 'password-secret' clashes with 'key-secret'");
583 opts->key_secret = g_strdup(secretid);
584 opts->has_key_secret = true;
587 mon_host = qemu_rbd_mon_host(opts, &local_err);
589 error_propagate(errp, local_err);
594 r = rados_create(cluster, opts->user);
596 error_setg_errno(errp, -r, "error initializing");
600 /* try default location when conf=NULL, but ignore failure */
601 r = rados_conf_read_file(*cluster, opts->conf);
602 if (opts->has_conf && r < 0) {
603 error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
604 goto failed_shutdown;
607 r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
609 goto failed_shutdown;
613 r = rados_conf_set(*cluster, "mon_host", mon_host);
615 goto failed_shutdown;
619 r = qemu_rbd_set_auth(*cluster, opts, errp);
621 goto failed_shutdown;
625 * Fallback to more conservative semantics if setting cache
626 * options fails. Ignore errors from setting rbd_cache because the
627 * only possible error is that the option does not exist, and
628 * librbd defaults to no caching. If write through caching cannot
629 * be set up, fall back to no caching.
632 rados_conf_set(*cluster, "rbd_cache", "true");
634 rados_conf_set(*cluster, "rbd_cache", "false");
637 r = rados_connect(*cluster);
639 error_setg_errno(errp, -r, "error connecting");
640 goto failed_shutdown;
643 r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
645 error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
646 goto failed_shutdown;
652 rados_shutdown(*cluster);
658 static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
662 Error *local_err = NULL;
664 /* Convert the remaining options into a QAPI object */
665 v = qobject_input_visitor_new_flat_confused(options, errp);
670 visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err);
674 error_propagate(errp, local_err);
681 static int qemu_rbd_attempt_legacy_options(QDict *options,
682 BlockdevOptionsRbd **opts,
688 filename = g_strdup(qdict_get_try_str(options, "filename"));
692 qdict_del(options, "filename");
694 qemu_rbd_parse_filename(filename, options, NULL);
696 /* keypairs freed by caller */
697 *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
699 qdict_del(options, "=keyvalue-pairs");
702 r = qemu_rbd_convert_options(options, opts, NULL);
708 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
711 BDRVRBDState *s = bs->opaque;
712 BlockdevOptionsRbd *opts = NULL;
714 Error *local_err = NULL;
715 char *keypairs, *secretid;
718 keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
720 qdict_del(options, "=keyvalue-pairs");
723 secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
725 qdict_del(options, "password-secret");
728 r = qemu_rbd_convert_options(options, &opts, &local_err);
730 /* If keypairs are present, that means some options are present in
731 * the modern option format. Don't attempt to parse legacy option
732 * formats, as we won't support mixed usage. */
734 error_propagate(errp, local_err);
738 /* If the initial attempt to convert and process the options failed,
739 * we may be attempting to open an image file that has the rbd options
740 * specified in the older format consisting of all key/value pairs
741 * encoded in the filename. Go ahead and attempt to parse the
742 * filename, and see if we can pull out the required options. */
743 r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
745 /* Propagate the original error, not the legacy parsing fallback
746 * error, as the latter was just a best-effort attempt. */
747 error_propagate(errp, local_err);
750 /* Take care whenever deciding to actually deprecate; once this ability
751 * is removed, we will not be able to open any images with legacy-styled
752 * backing image strings. */
753 error_report("RBD options encoded in the filename as keyvalue pairs "
757 /* Remove the processed options from the QDict (the visitor processes
758 * _all_ options in the QDict) */
759 while ((e = qdict_first(options))) {
760 qdict_del(options, e->key);
763 r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
764 !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
769 s->snap = g_strdup(opts->snapshot);
770 s->image_name = g_strdup(opts->image);
772 /* rbd_open is always r/w */
773 r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
775 error_setg_errno(errp, -r, "error reading header from %s",
780 /* If we are using an rbd snapshot, we must be r/o, otherwise
782 if (s->snap != NULL) {
783 if (!bdrv_is_read_only(bs)) {
784 error_report("Opening rbd snapshots without an explicit "
785 "read-only=on option is deprecated. Future versions "
786 "will refuse to open the image instead of "
787 "automatically marking the image read-only.");
788 r = bdrv_set_read_only(bs, true, &local_err);
790 error_propagate(errp, local_err);
800 rados_ioctx_destroy(s->io_ctx);
802 g_free(s->image_name);
803 rados_shutdown(s->cluster);
805 qapi_free_BlockdevOptionsRbd(opts);
812 /* Since RBD is currently always opened R/W via the API,
813 * we just need to check if we are using a snapshot or not, in
814 * order to determine if we will allow it to be R/W */
815 static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
816 BlockReopenQueue *queue, Error **errp)
818 BDRVRBDState *s = state->bs->opaque;
821 if (s->snap && state->flags & BDRV_O_RDWR) {
823 "Cannot change node '%s' to r/w when using RBD snapshot",
824 bdrv_get_device_or_node_name(state->bs));
831 static void qemu_rbd_close(BlockDriverState *bs)
833 BDRVRBDState *s = bs->opaque;
836 rados_ioctx_destroy(s->io_ctx);
838 g_free(s->image_name);
839 rados_shutdown(s->cluster);
842 static const AIOCBInfo rbd_aiocb_info = {
843 .aiocb_size = sizeof(RBDAIOCB),
846 static void rbd_finish_bh(void *opaque)
848 RADOSCB *rcb = opaque;
849 qemu_rbd_complete_aio(rcb);
853 * This is the callback function for rbd_aio_read and _write
855 * Note: this function is being called from a non qemu thread so
856 * we need to be careful about what we do here. Generally we only
857 * schedule a BH, and do the rest of the io completion handling
858 * from rbd_finish_bh() which runs in a qemu context.
860 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
862 RBDAIOCB *acb = rcb->acb;
864 rcb->ret = rbd_aio_get_return_value(c);
867 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
871 static int rbd_aio_discard_wrapper(rbd_image_t image,
874 rbd_completion_t comp)
876 #ifdef LIBRBD_SUPPORTS_DISCARD
877 return rbd_aio_discard(image, off, len, comp);
883 static int rbd_aio_flush_wrapper(rbd_image_t image,
884 rbd_completion_t comp)
886 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
887 return rbd_aio_flush(image, comp);
893 static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
897 BlockCompletionFunc *cb,
906 BDRVRBDState *s = bs->opaque;
908 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
911 assert(!qiov || qiov->size == size);
913 rcb = g_new(RADOSCB, 1);
915 if (!LIBRBD_USE_IOVEC) {
916 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
919 acb->bounce = qemu_try_blockalign(bs, qiov->size);
920 if (acb->bounce == NULL) {
924 if (cmd == RBD_AIO_WRITE) {
925 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
927 rcb->buf = acb->bounce;
937 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
944 #ifdef LIBRBD_SUPPORTS_IOVEC
945 r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
947 r = rbd_aio_write(s->image, off, size, rcb->buf, c);
951 #ifdef LIBRBD_SUPPORTS_IOVEC
952 r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
954 r = rbd_aio_read(s->image, off, size, rcb->buf, c);
957 case RBD_AIO_DISCARD:
958 r = rbd_aio_discard_wrapper(s->image, off, size, c);
961 r = rbd_aio_flush_wrapper(s->image, c);
968 goto failed_completion;
976 if (!LIBRBD_USE_IOVEC) {
977 qemu_vfree(acb->bounce);
984 static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
985 uint64_t offset, uint64_t bytes,
986 QEMUIOVector *qiov, int flags,
987 BlockCompletionFunc *cb,
990 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
994 static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
995 uint64_t offset, uint64_t bytes,
996 QEMUIOVector *qiov, int flags,
997 BlockCompletionFunc *cb,
1000 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1004 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1005 static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
1006 BlockCompletionFunc *cb,
1009 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
1014 static int qemu_rbd_co_flush(BlockDriverState *bs)
1016 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
1017 /* rbd_flush added in 0.1.1 */
1018 BDRVRBDState *s = bs->opaque;
1019 return rbd_flush(s->image);
1026 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1028 BDRVRBDState *s = bs->opaque;
1029 rbd_image_info_t info;
1032 r = rbd_stat(s->image, &info, sizeof(info));
1037 bdi->cluster_size = info.obj_size;
1041 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1043 BDRVRBDState *s = bs->opaque;
1044 rbd_image_info_t info;
1047 r = rbd_stat(s->image, &info, sizeof(info));
1055 static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1057 PreallocMode prealloc,
1060 BDRVRBDState *s = bs->opaque;
1063 if (prealloc != PREALLOC_MODE_OFF) {
1064 error_setg(errp, "Unsupported preallocation mode '%s'",
1065 PreallocMode_str(prealloc));
1069 r = rbd_resize(s->image, offset);
1071 error_setg_errno(errp, -r, "Failed to resize file");
1078 static int qemu_rbd_snap_create(BlockDriverState *bs,
1079 QEMUSnapshotInfo *sn_info)
1081 BDRVRBDState *s = bs->opaque;
1084 if (sn_info->name[0] == '\0') {
1085 return -EINVAL; /* we need a name for rbd snapshots */
1089 * rbd snapshots are using the name as the user controlled unique identifier
1090 * we can't use the rbd snapid for that purpose, as it can't be set
1092 if (sn_info->id_str[0] != '\0' &&
1093 strcmp(sn_info->id_str, sn_info->name) != 0) {
1097 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1101 r = rbd_snap_create(s->image, sn_info->name);
1103 error_report("failed to create snap: %s", strerror(-r));
1110 static int qemu_rbd_snap_remove(BlockDriverState *bs,
1111 const char *snapshot_id,
1112 const char *snapshot_name,
1115 BDRVRBDState *s = bs->opaque;
1118 if (!snapshot_name) {
1119 error_setg(errp, "rbd need a valid snapshot name");
1123 /* If snapshot_id is specified, it must be equal to name, see
1124 qemu_rbd_snap_list() */
1125 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1127 "rbd do not support snapshot id, it should be NULL or "
1128 "equal to snapshot name");
1132 r = rbd_snap_remove(s->image, snapshot_name);
1134 error_setg_errno(errp, -r, "Failed to remove the snapshot");
1139 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1140 const char *snapshot_name)
1142 BDRVRBDState *s = bs->opaque;
1144 return rbd_snap_rollback(s->image, snapshot_name);
1147 static int qemu_rbd_snap_list(BlockDriverState *bs,
1148 QEMUSnapshotInfo **psn_tab)
1150 BDRVRBDState *s = bs->opaque;
1151 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1153 rbd_snap_info_t *snaps;
1154 int max_snaps = RBD_MAX_SNAPS;
1157 snaps = g_new(rbd_snap_info_t, max_snaps);
1158 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
1159 if (snap_count <= 0) {
1162 } while (snap_count == -ERANGE);
1164 if (snap_count <= 0) {
1168 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1170 for (i = 0; i < snap_count; i++) {
1171 const char *snap_name = snaps[i].name;
1173 sn_info = sn_tab + i;
1174 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1175 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1177 sn_info->vm_state_size = snaps[i].size;
1178 sn_info->date_sec = 0;
1179 sn_info->date_nsec = 0;
1180 sn_info->vm_clock_nsec = 0;
1182 rbd_snap_list_end(snaps);
1190 #ifdef LIBRBD_SUPPORTS_DISCARD
1191 static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
1194 BlockCompletionFunc *cb,
1197 return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1202 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1203 static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1206 BDRVRBDState *s = bs->opaque;
1207 int r = rbd_invalidate_cache(s->image);
1209 error_setg_errno(errp, -r, "Failed to invalidate the cache");
1214 static QemuOptsList qemu_rbd_create_opts = {
1215 .name = "rbd-create-opts",
1216 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1219 .name = BLOCK_OPT_SIZE,
1220 .type = QEMU_OPT_SIZE,
1221 .help = "Virtual disk size"
1224 .name = BLOCK_OPT_CLUSTER_SIZE,
1225 .type = QEMU_OPT_SIZE,
1226 .help = "RBD object size"
1229 .name = "password-secret",
1230 .type = QEMU_OPT_STRING,
1231 .help = "ID of secret providing the password",
1233 { /* end of list */ }
1237 static BlockDriver bdrv_rbd = {
1238 .format_name = "rbd",
1239 .instance_size = sizeof(BDRVRBDState),
1240 .bdrv_parse_filename = qemu_rbd_parse_filename,
1241 .bdrv_refresh_limits = qemu_rbd_refresh_limits,
1242 .bdrv_file_open = qemu_rbd_open,
1243 .bdrv_close = qemu_rbd_close,
1244 .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
1245 .bdrv_co_create = qemu_rbd_co_create,
1246 .bdrv_co_create_opts = qemu_rbd_co_create_opts,
1247 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1248 .bdrv_get_info = qemu_rbd_getinfo,
1249 .create_opts = &qemu_rbd_create_opts,
1250 .bdrv_getlength = qemu_rbd_getlength,
1251 .bdrv_co_truncate = qemu_rbd_co_truncate,
1252 .protocol_name = "rbd",
1254 .bdrv_aio_preadv = qemu_rbd_aio_preadv,
1255 .bdrv_aio_pwritev = qemu_rbd_aio_pwritev,
1257 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1258 .bdrv_aio_flush = qemu_rbd_aio_flush,
1260 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
1263 #ifdef LIBRBD_SUPPORTS_DISCARD
1264 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
1267 .bdrv_snapshot_create = qemu_rbd_snap_create,
1268 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1269 .bdrv_snapshot_list = qemu_rbd_snap_list,
1270 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
1271 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1272 .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1276 static void bdrv_rbd_init(void)
1278 bdrv_register(&bdrv_rbd);
1281 block_init(bdrv_rbd_init);