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
,
267 qobject_from_json(keypairs_json
, &error_abort
));
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
;
627 Error
*local_err
= NULL
;
628 const char *filename
;
629 char *keypairs
, *secretid
;
632 /* If we are given a filename, parse the filename, with precedence given to
633 * filename encoded options */
634 filename
= qdict_get_try_str(options
, "filename");
636 warn_report("'filename' option specified. "
637 "This is an unsupported option, and may be deprecated "
639 qemu_rbd_parse_filename(filename
, options
, &local_err
);
640 qdict_del(options
, "filename");
642 error_propagate(errp
, local_err
);
647 keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
649 qdict_del(options
, "=keyvalue-pairs");
652 secretid
= g_strdup(qdict_get_try_str(options
, "password-secret"));
654 qdict_del(options
, "password-secret");
657 /* Convert the remaining options into a QAPI object */
658 crumpled
= qdict_crumple(options
, errp
);
659 if (crumpled
== NULL
) {
664 v
= qobject_input_visitor_new_keyval(crumpled
);
665 visit_type_BlockdevOptionsRbd(v
, NULL
, &opts
, &local_err
);
667 qobject_decref(crumpled
);
670 error_propagate(errp
, local_err
);
675 /* Remove the processed options from the QDict (the visitor processes
676 * _all_ options in the QDict) */
677 while ((e
= qdict_first(options
))) {
678 qdict_del(options
, e
->key
);
681 r
= qemu_rbd_connect(&s
->cluster
, &s
->io_ctx
, opts
,
682 !(flags
& BDRV_O_NOCACHE
), keypairs
, secretid
, errp
);
687 s
->snap
= g_strdup(opts
->snapshot
);
688 s
->image_name
= g_strdup(opts
->image
);
690 /* rbd_open is always r/w */
691 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
693 error_setg_errno(errp
, -r
, "error reading header from %s",
698 /* If we are using an rbd snapshot, we must be r/o, otherwise
700 if (s
->snap
!= NULL
) {
701 if (!bdrv_is_read_only(bs
)) {
702 error_report("Opening rbd snapshots without an explicit "
703 "read-only=on option is deprecated. Future versions "
704 "will refuse to open the image instead of "
705 "automatically marking the image read-only.");
706 r
= bdrv_set_read_only(bs
, true, &local_err
);
708 error_propagate(errp
, local_err
);
718 rados_ioctx_destroy(s
->io_ctx
);
720 g_free(s
->image_name
);
721 rados_shutdown(s
->cluster
);
723 qapi_free_BlockdevOptionsRbd(opts
);
730 /* Since RBD is currently always opened R/W via the API,
731 * we just need to check if we are using a snapshot or not, in
732 * order to determine if we will allow it to be R/W */
733 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
734 BlockReopenQueue
*queue
, Error
**errp
)
736 BDRVRBDState
*s
= state
->bs
->opaque
;
739 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
741 "Cannot change node '%s' to r/w when using RBD snapshot",
742 bdrv_get_device_or_node_name(state
->bs
));
749 static void qemu_rbd_close(BlockDriverState
*bs
)
751 BDRVRBDState
*s
= bs
->opaque
;
754 rados_ioctx_destroy(s
->io_ctx
);
756 g_free(s
->image_name
);
757 rados_shutdown(s
->cluster
);
760 static const AIOCBInfo rbd_aiocb_info
= {
761 .aiocb_size
= sizeof(RBDAIOCB
),
764 static void rbd_finish_bh(void *opaque
)
766 RADOSCB
*rcb
= opaque
;
767 qemu_rbd_complete_aio(rcb
);
771 * This is the callback function for rbd_aio_read and _write
773 * Note: this function is being called from a non qemu thread so
774 * we need to be careful about what we do here. Generally we only
775 * schedule a BH, and do the rest of the io completion handling
776 * from rbd_finish_bh() which runs in a qemu context.
778 static void rbd_finish_aiocb(rbd_completion_t c
, RADOSCB
*rcb
)
780 RBDAIOCB
*acb
= rcb
->acb
;
782 rcb
->ret
= rbd_aio_get_return_value(c
);
785 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb
->common
.bs
),
789 static int rbd_aio_discard_wrapper(rbd_image_t image
,
792 rbd_completion_t comp
)
794 #ifdef LIBRBD_SUPPORTS_DISCARD
795 return rbd_aio_discard(image
, off
, len
, comp
);
801 static int rbd_aio_flush_wrapper(rbd_image_t image
,
802 rbd_completion_t comp
)
804 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
805 return rbd_aio_flush(image
, comp
);
811 static BlockAIOCB
*rbd_start_aio(BlockDriverState
*bs
,
815 BlockCompletionFunc
*cb
,
824 BDRVRBDState
*s
= bs
->opaque
;
826 acb
= qemu_aio_get(&rbd_aiocb_info
, bs
, cb
, opaque
);
829 assert(!qiov
|| qiov
->size
== size
);
831 rcb
= g_new(RADOSCB
, 1);
833 if (!LIBRBD_USE_IOVEC
) {
834 if (cmd
== RBD_AIO_DISCARD
|| cmd
== RBD_AIO_FLUSH
) {
837 acb
->bounce
= qemu_try_blockalign(bs
, qiov
->size
);
838 if (acb
->bounce
== NULL
) {
842 if (cmd
== RBD_AIO_WRITE
) {
843 qemu_iovec_to_buf(acb
->qiov
, 0, acb
->bounce
, qiov
->size
);
845 rcb
->buf
= acb
->bounce
;
855 r
= rbd_aio_create_completion(rcb
, (rbd_callback_t
) rbd_finish_aiocb
, &c
);
862 #ifdef LIBRBD_SUPPORTS_IOVEC
863 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
865 r
= rbd_aio_write(s
->image
, off
, size
, rcb
->buf
, c
);
869 #ifdef LIBRBD_SUPPORTS_IOVEC
870 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, off
, c
);
872 r
= rbd_aio_read(s
->image
, off
, size
, rcb
->buf
, c
);
875 case RBD_AIO_DISCARD
:
876 r
= rbd_aio_discard_wrapper(s
->image
, off
, size
, c
);
879 r
= rbd_aio_flush_wrapper(s
->image
, c
);
886 goto failed_completion
;
894 if (!LIBRBD_USE_IOVEC
) {
895 qemu_vfree(acb
->bounce
);
902 static BlockAIOCB
*qemu_rbd_aio_readv(BlockDriverState
*bs
,
906 BlockCompletionFunc
*cb
,
909 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
910 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
914 static BlockAIOCB
*qemu_rbd_aio_writev(BlockDriverState
*bs
,
918 BlockCompletionFunc
*cb
,
921 return rbd_start_aio(bs
, sector_num
<< BDRV_SECTOR_BITS
, qiov
,
922 (int64_t) nb_sectors
<< BDRV_SECTOR_BITS
, cb
, opaque
,
926 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
927 static BlockAIOCB
*qemu_rbd_aio_flush(BlockDriverState
*bs
,
928 BlockCompletionFunc
*cb
,
931 return rbd_start_aio(bs
, 0, NULL
, 0, cb
, opaque
, RBD_AIO_FLUSH
);
936 static int qemu_rbd_co_flush(BlockDriverState
*bs
)
938 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
939 /* rbd_flush added in 0.1.1 */
940 BDRVRBDState
*s
= bs
->opaque
;
941 return rbd_flush(s
->image
);
948 static int qemu_rbd_getinfo(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
950 BDRVRBDState
*s
= bs
->opaque
;
951 rbd_image_info_t info
;
954 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
959 bdi
->cluster_size
= info
.obj_size
;
963 static int64_t qemu_rbd_getlength(BlockDriverState
*bs
)
965 BDRVRBDState
*s
= bs
->opaque
;
966 rbd_image_info_t info
;
969 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
977 static int qemu_rbd_truncate(BlockDriverState
*bs
, int64_t offset
,
978 PreallocMode prealloc
, Error
**errp
)
980 BDRVRBDState
*s
= bs
->opaque
;
983 if (prealloc
!= PREALLOC_MODE_OFF
) {
984 error_setg(errp
, "Unsupported preallocation mode '%s'",
985 PreallocMode_str(prealloc
));
989 r
= rbd_resize(s
->image
, offset
);
991 error_setg_errno(errp
, -r
, "Failed to resize file");
998 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
999 QEMUSnapshotInfo
*sn_info
)
1001 BDRVRBDState
*s
= bs
->opaque
;
1004 if (sn_info
->name
[0] == '\0') {
1005 return -EINVAL
; /* we need a name for rbd snapshots */
1009 * rbd snapshots are using the name as the user controlled unique identifier
1010 * we can't use the rbd snapid for that purpose, as it can't be set
1012 if (sn_info
->id_str
[0] != '\0' &&
1013 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
1017 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
1021 r
= rbd_snap_create(s
->image
, sn_info
->name
);
1023 error_report("failed to create snap: %s", strerror(-r
));
1030 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1031 const char *snapshot_id
,
1032 const char *snapshot_name
,
1035 BDRVRBDState
*s
= bs
->opaque
;
1038 if (!snapshot_name
) {
1039 error_setg(errp
, "rbd need a valid snapshot name");
1043 /* If snapshot_id is specified, it must be equal to name, see
1044 qemu_rbd_snap_list() */
1045 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1047 "rbd do not support snapshot id, it should be NULL or "
1048 "equal to snapshot name");
1052 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1054 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1059 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1060 const char *snapshot_name
)
1062 BDRVRBDState
*s
= bs
->opaque
;
1064 return rbd_snap_rollback(s
->image
, snapshot_name
);
1067 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1068 QEMUSnapshotInfo
**psn_tab
)
1070 BDRVRBDState
*s
= bs
->opaque
;
1071 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1073 rbd_snap_info_t
*snaps
;
1074 int max_snaps
= RBD_MAX_SNAPS
;
1077 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1078 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1079 if (snap_count
<= 0) {
1082 } while (snap_count
== -ERANGE
);
1084 if (snap_count
<= 0) {
1088 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1090 for (i
= 0; i
< snap_count
; i
++) {
1091 const char *snap_name
= snaps
[i
].name
;
1093 sn_info
= sn_tab
+ i
;
1094 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1095 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1097 sn_info
->vm_state_size
= snaps
[i
].size
;
1098 sn_info
->date_sec
= 0;
1099 sn_info
->date_nsec
= 0;
1100 sn_info
->vm_clock_nsec
= 0;
1102 rbd_snap_list_end(snaps
);
1110 #ifdef LIBRBD_SUPPORTS_DISCARD
1111 static BlockAIOCB
*qemu_rbd_aio_pdiscard(BlockDriverState
*bs
,
1114 BlockCompletionFunc
*cb
,
1117 return rbd_start_aio(bs
, offset
, NULL
, bytes
, cb
, opaque
,
1122 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1123 static void coroutine_fn
qemu_rbd_co_invalidate_cache(BlockDriverState
*bs
,
1126 BDRVRBDState
*s
= bs
->opaque
;
1127 int r
= rbd_invalidate_cache(s
->image
);
1129 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1134 static QemuOptsList qemu_rbd_create_opts
= {
1135 .name
= "rbd-create-opts",
1136 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1139 .name
= BLOCK_OPT_SIZE
,
1140 .type
= QEMU_OPT_SIZE
,
1141 .help
= "Virtual disk size"
1144 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1145 .type
= QEMU_OPT_SIZE
,
1146 .help
= "RBD object size"
1149 .name
= "password-secret",
1150 .type
= QEMU_OPT_STRING
,
1151 .help
= "ID of secret providing the password",
1153 { /* end of list */ }
1157 static BlockDriver bdrv_rbd
= {
1158 .format_name
= "rbd",
1159 .instance_size
= sizeof(BDRVRBDState
),
1160 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1161 .bdrv_file_open
= qemu_rbd_open
,
1162 .bdrv_close
= qemu_rbd_close
,
1163 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1164 .bdrv_co_create
= qemu_rbd_co_create
,
1165 .bdrv_co_create_opts
= qemu_rbd_co_create_opts
,
1166 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1167 .bdrv_get_info
= qemu_rbd_getinfo
,
1168 .create_opts
= &qemu_rbd_create_opts
,
1169 .bdrv_getlength
= qemu_rbd_getlength
,
1170 .bdrv_truncate
= qemu_rbd_truncate
,
1171 .protocol_name
= "rbd",
1173 .bdrv_aio_readv
= qemu_rbd_aio_readv
,
1174 .bdrv_aio_writev
= qemu_rbd_aio_writev
,
1176 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1177 .bdrv_aio_flush
= qemu_rbd_aio_flush
,
1179 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1182 #ifdef LIBRBD_SUPPORTS_DISCARD
1183 .bdrv_aio_pdiscard
= qemu_rbd_aio_pdiscard
,
1186 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1187 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1188 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1189 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1190 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1191 .bdrv_co_invalidate_cache
= qemu_rbd_co_invalidate_cache
,
1195 static void bdrv_rbd_init(void)
1197 bdrv_register(&bdrv_rbd
);
1200 block_init(bdrv_rbd_init
);