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/module.h"
20 #include "qemu/option.h"
21 #include "block/block-io.h"
22 #include "block/block_int.h"
23 #include "block/qdict.h"
24 #include "crypto/secret.h"
25 #include "qemu/cutils.h"
26 #include "sysemu/replay.h"
27 #include "qapi/qmp/qstring.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qjson.h"
30 #include "qapi/qmp/qlist.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qapi-visit-block-core.h"
35 * When specifying the image filename use:
37 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
39 * poolname must be the name of an existing rados pool.
41 * devicename is the name of the rbd image.
43 * Each option given is used to configure rados, and may be any valid
44 * Ceph option, "id", or "conf".
46 * The "id" option indicates what user we should authenticate as to
47 * the Ceph cluster. If it is excluded we will use the Ceph default
50 * The "conf" option specifies a Ceph configuration file to read. If
51 * it is not specified, we will read from the default Ceph locations
52 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
53 * file, specify conf=/dev/null.
55 * Configuration values containing :, @, or = can be escaped with a
59 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
61 #define RBD_MAX_SNAPS 100
63 #define RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN 8
65 static const char rbd_luks_header_verification
[
66 RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
] = {
67 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 1
70 static const char rbd_luks2_header_verification
[
71 RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
] = {
72 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 2
83 typedef struct BDRVRBDState
{
94 typedef struct RBDTask
{
101 typedef struct RBDDiffIterateReq
{
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_strchr(char *src
, char delim
)
116 for (p
= src
; *p
; ++p
) {
120 if (*p
== '\\' && p
[1] != '\0') {
129 static char *qemu_rbd_next_tok(char *src
, char delim
, char **p
)
135 end
= qemu_rbd_strchr(src
, delim
);
143 static void qemu_rbd_unescape(char *src
)
147 for (p
= src
; *src
; ++src
, ++p
) {
148 if (*src
== '\\' && src
[1] != '\0') {
156 static void qemu_rbd_parse_filename(const char *filename
, QDict
*options
,
161 QList
*keypairs
= NULL
;
162 char *found_str
, *image_name
;
164 if (!strstart(filename
, "rbd:", &start
)) {
165 error_setg(errp
, "File name must start with 'rbd:'");
169 buf
= g_strdup(start
);
172 found_str
= qemu_rbd_next_tok(p
, '/', &p
);
174 error_setg(errp
, "Pool name is required");
177 qemu_rbd_unescape(found_str
);
178 qdict_put_str(options
, "pool", found_str
);
180 if (qemu_rbd_strchr(p
, '@')) {
181 image_name
= qemu_rbd_next_tok(p
, '@', &p
);
183 found_str
= qemu_rbd_next_tok(p
, ':', &p
);
184 qemu_rbd_unescape(found_str
);
185 qdict_put_str(options
, "snapshot", found_str
);
187 image_name
= qemu_rbd_next_tok(p
, ':', &p
);
189 /* Check for namespace in the image_name */
190 if (qemu_rbd_strchr(image_name
, '/')) {
191 found_str
= qemu_rbd_next_tok(image_name
, '/', &image_name
);
192 qemu_rbd_unescape(found_str
);
193 qdict_put_str(options
, "namespace", found_str
);
195 qdict_put_str(options
, "namespace", "");
197 qemu_rbd_unescape(image_name
);
198 qdict_put_str(options
, "image", image_name
);
203 /* The following are essentially all key/value pairs, and we treat
204 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
207 name
= qemu_rbd_next_tok(p
, '=', &p
);
209 error_setg(errp
, "conf option %s has no value", name
);
213 qemu_rbd_unescape(name
);
215 value
= qemu_rbd_next_tok(p
, ':', &p
);
216 qemu_rbd_unescape(value
);
218 if (!strcmp(name
, "conf")) {
219 qdict_put_str(options
, "conf", value
);
220 } else if (!strcmp(name
, "id")) {
221 qdict_put_str(options
, "user", value
);
224 * We pass these internally to qemu_rbd_set_keypairs(), so
225 * we can get away with the simpler list of [ "key1",
226 * "value1", "key2", "value2" ] rather than a raw dict
227 * { "key1": "value1", "key2": "value2" } where we can't
228 * guarantee order, or even a more correct but complex
229 * [ { "key1": "value1" }, { "key2": "value2" } ]
232 keypairs
= qlist_new();
234 qlist_append_str(keypairs
, name
);
235 qlist_append_str(keypairs
, value
);
240 qdict_put(options
, "=keyvalue-pairs",
241 qstring_from_gstring(qobject_to_json(QOBJECT(keypairs
))));
246 qobject_unref(keypairs
);
250 static int qemu_rbd_set_auth(rados_t cluster
, BlockdevOptionsRbd
*opts
,
256 RbdAuthModeList
*auth
;
258 if (opts
->key_secret
) {
259 key
= qcrypto_secret_lookup_as_base64(opts
->key_secret
, errp
);
263 r
= rados_conf_set(cluster
, "key", key
);
266 error_setg_errno(errp
, -r
, "Could not set 'key'");
271 if (opts
->has_auth_client_required
) {
272 accu
= g_string_new("");
273 for (auth
= opts
->auth_client_required
; auth
; auth
= auth
->next
) {
275 g_string_append_c(accu
, ';');
277 g_string_append(accu
, RbdAuthMode_str(auth
->value
));
279 acr
= g_string_free(accu
, FALSE
);
280 r
= rados_conf_set(cluster
, "auth_client_required", acr
);
283 error_setg_errno(errp
, -r
,
284 "Could not set 'auth_client_required'");
292 static int qemu_rbd_set_keypairs(rados_t cluster
, const char *keypairs_json
,
302 if (!keypairs_json
) {
305 keypairs
= qobject_to(QList
,
306 qobject_from_json(keypairs_json
, &error_abort
));
307 remaining
= qlist_size(keypairs
) / 2;
310 while (remaining
--) {
311 name
= qobject_to(QString
, qlist_pop(keypairs
));
312 value
= qobject_to(QString
, qlist_pop(keypairs
));
313 assert(name
&& value
);
314 key
= qstring_get_str(name
);
316 ret
= rados_conf_set(cluster
, key
, qstring_get_str(value
));
317 qobject_unref(value
);
319 error_setg_errno(errp
, -ret
, "invalid conf option %s", key
);
327 qobject_unref(keypairs
);
331 #ifdef LIBRBD_SUPPORTS_ENCRYPTION
332 static int qemu_rbd_convert_luks_options(
333 RbdEncryptionOptionsLUKSBase
*luks_opts
,
335 size_t *passphrase_len
,
338 return qcrypto_secret_lookup(luks_opts
->key_secret
, (uint8_t **)passphrase
,
339 passphrase_len
, errp
);
342 static int qemu_rbd_convert_luks_create_options(
343 RbdEncryptionCreateOptionsLUKSBase
*luks_opts
,
344 rbd_encryption_algorithm_t
*alg
,
346 size_t *passphrase_len
,
351 r
= qemu_rbd_convert_luks_options(
352 qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts
),
353 passphrase
, passphrase_len
, errp
);
358 if (luks_opts
->has_cipher_alg
) {
359 switch (luks_opts
->cipher_alg
) {
360 case QCRYPTO_CIPHER_ALG_AES_128
: {
361 *alg
= RBD_ENCRYPTION_ALGORITHM_AES128
;
364 case QCRYPTO_CIPHER_ALG_AES_256
: {
365 *alg
= RBD_ENCRYPTION_ALGORITHM_AES256
;
370 error_setg_errno(errp
, -r
, "unknown encryption algorithm: %u",
371 luks_opts
->cipher_alg
);
377 *alg
= RBD_ENCRYPTION_ALGORITHM_AES256
;
383 static int qemu_rbd_encryption_format(rbd_image_t image
,
384 RbdEncryptionCreateOptions
*encrypt
,
388 g_autofree
char *passphrase
= NULL
;
389 size_t passphrase_len
;
390 rbd_encryption_format_t format
;
391 rbd_encryption_options_t opts
;
392 rbd_encryption_luks1_format_options_t luks_opts
;
393 rbd_encryption_luks2_format_options_t luks2_opts
;
395 uint64_t raw_size
, effective_size
;
397 r
= rbd_get_size(image
, &raw_size
);
399 error_setg_errno(errp
, -r
, "cannot get raw image size");
403 switch (encrypt
->format
) {
404 case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS
: {
405 memset(&luks_opts
, 0, sizeof(luks_opts
));
406 format
= RBD_ENCRYPTION_FORMAT_LUKS1
;
408 opts_size
= sizeof(luks_opts
);
409 r
= qemu_rbd_convert_luks_create_options(
410 qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt
->u
.luks
),
411 &luks_opts
.alg
, &passphrase
, &passphrase_len
, errp
);
415 luks_opts
.passphrase
= passphrase
;
416 luks_opts
.passphrase_size
= passphrase_len
;
419 case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2
: {
420 memset(&luks2_opts
, 0, sizeof(luks2_opts
));
421 format
= RBD_ENCRYPTION_FORMAT_LUKS2
;
423 opts_size
= sizeof(luks2_opts
);
424 r
= qemu_rbd_convert_luks_create_options(
425 qapi_RbdEncryptionCreateOptionsLUKS2_base(
427 &luks2_opts
.alg
, &passphrase
, &passphrase_len
, errp
);
431 luks2_opts
.passphrase
= passphrase
;
432 luks2_opts
.passphrase_size
= passphrase_len
;
438 errp
, -r
, "unknown image encryption format: %u",
444 r
= rbd_encryption_format(image
, format
, opts
, opts_size
);
446 error_setg_errno(errp
, -r
, "encryption format fail");
450 r
= rbd_get_size(image
, &effective_size
);
452 error_setg_errno(errp
, -r
, "cannot get effective image size");
456 r
= rbd_resize(image
, raw_size
+ (raw_size
- effective_size
));
458 error_setg_errno(errp
, -r
, "cannot resize image after format");
465 static int qemu_rbd_encryption_load(rbd_image_t image
,
466 RbdEncryptionOptions
*encrypt
,
470 g_autofree
char *passphrase
= NULL
;
471 size_t passphrase_len
;
472 rbd_encryption_luks1_format_options_t luks_opts
;
473 rbd_encryption_luks2_format_options_t luks2_opts
;
474 rbd_encryption_format_t format
;
475 rbd_encryption_options_t opts
;
478 switch (encrypt
->format
) {
479 case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS
: {
480 memset(&luks_opts
, 0, sizeof(luks_opts
));
481 format
= RBD_ENCRYPTION_FORMAT_LUKS1
;
483 opts_size
= sizeof(luks_opts
);
484 r
= qemu_rbd_convert_luks_options(
485 qapi_RbdEncryptionOptionsLUKS_base(&encrypt
->u
.luks
),
486 &passphrase
, &passphrase_len
, errp
);
490 luks_opts
.passphrase
= passphrase
;
491 luks_opts
.passphrase_size
= passphrase_len
;
494 case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2
: {
495 memset(&luks2_opts
, 0, sizeof(luks2_opts
));
496 format
= RBD_ENCRYPTION_FORMAT_LUKS2
;
498 opts_size
= sizeof(luks2_opts
);
499 r
= qemu_rbd_convert_luks_options(
500 qapi_RbdEncryptionOptionsLUKS2_base(&encrypt
->u
.luks2
),
501 &passphrase
, &passphrase_len
, errp
);
505 luks2_opts
.passphrase
= passphrase
;
506 luks2_opts
.passphrase_size
= passphrase_len
;
512 errp
, -r
, "unknown image encryption format: %u",
518 r
= rbd_encryption_load(image
, format
, opts
, opts_size
);
520 error_setg_errno(errp
, -r
, "encryption load fail");
528 /* FIXME Deprecate and remove keypairs or make it available in QMP. */
529 static int qemu_rbd_do_create(BlockdevCreateOptions
*options
,
530 const char *keypairs
, const char *password_secret
,
533 BlockdevCreateOptionsRbd
*opts
= &options
->u
.rbd
;
535 rados_ioctx_t io_ctx
;
539 assert(options
->driver
== BLOCKDEV_DRIVER_RBD
);
540 if (opts
->location
->snapshot
) {
541 error_setg(errp
, "Can't use snapshot name for image creation");
545 #ifndef LIBRBD_SUPPORTS_ENCRYPTION
547 error_setg(errp
, "RBD library does not support image encryption");
552 if (opts
->has_cluster_size
) {
553 int64_t objsize
= opts
->cluster_size
;
554 if ((objsize
- 1) & objsize
) { /* not a power of 2? */
555 error_setg(errp
, "obj size needs to be power of 2");
558 if (objsize
< 4096) {
559 error_setg(errp
, "obj size too small");
562 obj_order
= ctz32(objsize
);
565 ret
= qemu_rbd_connect(&cluster
, &io_ctx
, opts
->location
, false, keypairs
,
566 password_secret
, errp
);
571 ret
= rbd_create(io_ctx
, opts
->location
->image
, opts
->size
, &obj_order
);
573 error_setg_errno(errp
, -ret
, "error rbd create");
577 #ifdef LIBRBD_SUPPORTS_ENCRYPTION
581 ret
= rbd_open(io_ctx
, opts
->location
->image
, &image
, NULL
);
583 error_setg_errno(errp
, -ret
,
584 "error opening image '%s' for encryption format",
585 opts
->location
->image
);
589 ret
= qemu_rbd_encryption_format(image
, opts
->encrypt
, errp
);
592 /* encryption format fail, try removing the image */
593 rbd_remove(io_ctx
, opts
->location
->image
);
601 rados_ioctx_destroy(io_ctx
);
602 rados_shutdown(cluster
);
606 static int qemu_rbd_co_create(BlockdevCreateOptions
*options
, Error
**errp
)
608 return qemu_rbd_do_create(options
, NULL
, NULL
, errp
);
611 static int qemu_rbd_extract_encryption_create_options(
613 RbdEncryptionCreateOptions
**spec
,
617 QDict
*encrypt_qdict
;
621 opts_qdict
= qemu_opts_to_qdict(opts
, NULL
);
622 qdict_extract_subqdict(opts_qdict
, &encrypt_qdict
, "encrypt.");
623 qobject_unref(opts_qdict
);
624 if (!qdict_size(encrypt_qdict
)) {
629 /* Convert options into a QAPI object */
630 v
= qobject_input_visitor_new_flat_confused(encrypt_qdict
, errp
);
636 visit_type_RbdEncryptionCreateOptions(v
, NULL
, spec
, errp
);
644 qobject_unref(encrypt_qdict
);
648 static int coroutine_fn
qemu_rbd_co_create_opts(BlockDriver
*drv
,
649 const char *filename
,
653 BlockdevCreateOptions
*create_options
;
654 BlockdevCreateOptionsRbd
*rbd_opts
;
655 BlockdevOptionsRbd
*loc
;
656 RbdEncryptionCreateOptions
*encrypt
= NULL
;
657 Error
*local_err
= NULL
;
658 const char *keypairs
, *password_secret
;
659 QDict
*options
= NULL
;
662 create_options
= g_new0(BlockdevCreateOptions
, 1);
663 create_options
->driver
= BLOCKDEV_DRIVER_RBD
;
664 rbd_opts
= &create_options
->u
.rbd
;
666 rbd_opts
->location
= g_new0(BlockdevOptionsRbd
, 1);
668 password_secret
= qemu_opt_get(opts
, "password-secret");
670 /* Read out options */
671 rbd_opts
->size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
673 rbd_opts
->cluster_size
= qemu_opt_get_size_del(opts
,
674 BLOCK_OPT_CLUSTER_SIZE
, 0);
675 rbd_opts
->has_cluster_size
= (rbd_opts
->cluster_size
!= 0);
677 options
= qdict_new();
678 qemu_rbd_parse_filename(filename
, options
, &local_err
);
681 error_propagate(errp
, local_err
);
685 ret
= qemu_rbd_extract_encryption_create_options(opts
, &encrypt
, errp
);
689 rbd_opts
->encrypt
= encrypt
;
692 * Caution: while qdict_get_try_str() is fine, getting non-string
693 * types would require more care. When @options come from -blockdev
694 * or blockdev_add, its members are typed according to the QAPI
695 * schema, but when they come from -drive, they're all QString.
697 loc
= rbd_opts
->location
;
698 loc
->pool
= g_strdup(qdict_get_try_str(options
, "pool"));
699 loc
->conf
= g_strdup(qdict_get_try_str(options
, "conf"));
700 loc
->user
= g_strdup(qdict_get_try_str(options
, "user"));
701 loc
->q_namespace
= g_strdup(qdict_get_try_str(options
, "namespace"));
702 loc
->image
= g_strdup(qdict_get_try_str(options
, "image"));
703 keypairs
= qdict_get_try_str(options
, "=keyvalue-pairs");
705 ret
= qemu_rbd_do_create(create_options
, keypairs
, password_secret
, errp
);
711 qobject_unref(options
);
712 qapi_free_BlockdevCreateOptions(create_options
);
716 static char *qemu_rbd_mon_host(BlockdevOptionsRbd
*opts
, Error
**errp
)
719 const char *host
, *port
;
721 InetSocketAddressBaseList
*p
;
724 if (!opts
->has_server
) {
728 for (cnt
= 0, p
= opts
->server
; p
; p
= p
->next
) {
732 vals
= g_new(const char *, cnt
+ 1);
734 for (i
= 0, p
= opts
->server
; p
; p
= p
->next
, i
++) {
735 host
= p
->value
->host
;
736 port
= p
->value
->port
;
738 if (strchr(host
, ':')) {
739 vals
[i
] = g_strdup_printf("[%s]:%s", host
, port
);
741 vals
[i
] = g_strdup_printf("%s:%s", host
, port
);
746 rados_str
= i
? g_strjoinv(";", (char **)vals
) : NULL
;
747 g_strfreev((char **)vals
);
751 static int qemu_rbd_connect(rados_t
*cluster
, rados_ioctx_t
*io_ctx
,
752 BlockdevOptionsRbd
*opts
, bool cache
,
753 const char *keypairs
, const char *secretid
,
756 char *mon_host
= NULL
;
757 Error
*local_err
= NULL
;
761 if (opts
->key_secret
) {
763 "Legacy 'password-secret' clashes with 'key-secret'");
766 opts
->key_secret
= g_strdup(secretid
);
769 mon_host
= qemu_rbd_mon_host(opts
, &local_err
);
771 error_propagate(errp
, local_err
);
776 r
= rados_create(cluster
, opts
->user
);
778 error_setg_errno(errp
, -r
, "error initializing");
782 /* try default location when conf=NULL, but ignore failure */
783 r
= rados_conf_read_file(*cluster
, opts
->conf
);
784 if (opts
->conf
&& r
< 0) {
785 error_setg_errno(errp
, -r
, "error reading conf file %s", opts
->conf
);
786 goto failed_shutdown
;
789 r
= qemu_rbd_set_keypairs(*cluster
, keypairs
, errp
);
791 goto failed_shutdown
;
795 r
= rados_conf_set(*cluster
, "mon_host", mon_host
);
797 goto failed_shutdown
;
801 r
= qemu_rbd_set_auth(*cluster
, opts
, errp
);
803 goto failed_shutdown
;
807 * Fallback to more conservative semantics if setting cache
808 * options fails. Ignore errors from setting rbd_cache because the
809 * only possible error is that the option does not exist, and
810 * librbd defaults to no caching. If write through caching cannot
811 * be set up, fall back to no caching.
814 rados_conf_set(*cluster
, "rbd_cache", "true");
816 rados_conf_set(*cluster
, "rbd_cache", "false");
819 r
= rados_connect(*cluster
);
821 error_setg_errno(errp
, -r
, "error connecting");
822 goto failed_shutdown
;
825 r
= rados_ioctx_create(*cluster
, opts
->pool
, io_ctx
);
827 error_setg_errno(errp
, -r
, "error opening pool %s", opts
->pool
);
828 goto failed_shutdown
;
831 #ifdef HAVE_RBD_NAMESPACE_EXISTS
832 if (opts
->q_namespace
&& strlen(opts
->q_namespace
) > 0) {
835 r
= rbd_namespace_exists(*io_ctx
, opts
->q_namespace
, &exists
);
837 error_setg_errno(errp
, -r
, "error checking namespace");
838 goto failed_ioctx_destroy
;
842 error_setg(errp
, "namespace '%s' does not exist",
845 goto failed_ioctx_destroy
;
851 * Set the namespace after opening the io context on the pool,
852 * if nspace == NULL or if nspace == "", it is just as we did nothing
854 rados_ioctx_set_namespace(*io_ctx
, opts
->q_namespace
);
859 #ifdef HAVE_RBD_NAMESPACE_EXISTS
860 failed_ioctx_destroy
:
861 rados_ioctx_destroy(*io_ctx
);
864 rados_shutdown(*cluster
);
870 static int qemu_rbd_convert_options(QDict
*options
, BlockdevOptionsRbd
**opts
,
875 /* Convert the remaining options into a QAPI object */
876 v
= qobject_input_visitor_new_flat_confused(options
, errp
);
881 visit_type_BlockdevOptionsRbd(v
, NULL
, opts
, errp
);
890 static int qemu_rbd_attempt_legacy_options(QDict
*options
,
891 BlockdevOptionsRbd
**opts
,
897 filename
= g_strdup(qdict_get_try_str(options
, "filename"));
901 qdict_del(options
, "filename");
903 qemu_rbd_parse_filename(filename
, options
, NULL
);
905 /* keypairs freed by caller */
906 *keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
908 qdict_del(options
, "=keyvalue-pairs");
911 r
= qemu_rbd_convert_options(options
, opts
, NULL
);
917 static int qemu_rbd_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
920 BDRVRBDState
*s
= bs
->opaque
;
921 BlockdevOptionsRbd
*opts
= NULL
;
923 Error
*local_err
= NULL
;
924 char *keypairs
, *secretid
;
925 rbd_image_info_t info
;
928 keypairs
= g_strdup(qdict_get_try_str(options
, "=keyvalue-pairs"));
930 qdict_del(options
, "=keyvalue-pairs");
933 secretid
= g_strdup(qdict_get_try_str(options
, "password-secret"));
935 qdict_del(options
, "password-secret");
938 r
= qemu_rbd_convert_options(options
, &opts
, &local_err
);
940 /* If keypairs are present, that means some options are present in
941 * the modern option format. Don't attempt to parse legacy option
942 * formats, as we won't support mixed usage. */
944 error_propagate(errp
, local_err
);
948 /* If the initial attempt to convert and process the options failed,
949 * we may be attempting to open an image file that has the rbd options
950 * specified in the older format consisting of all key/value pairs
951 * encoded in the filename. Go ahead and attempt to parse the
952 * filename, and see if we can pull out the required options. */
953 r
= qemu_rbd_attempt_legacy_options(options
, &opts
, &keypairs
);
955 /* Propagate the original error, not the legacy parsing fallback
956 * error, as the latter was just a best-effort attempt. */
957 error_propagate(errp
, local_err
);
960 /* Take care whenever deciding to actually deprecate; once this ability
961 * is removed, we will not be able to open any images with legacy-styled
962 * backing image strings. */
963 warn_report("RBD options encoded in the filename as keyvalue pairs "
967 /* Remove the processed options from the QDict (the visitor processes
968 * _all_ options in the QDict) */
969 while ((e
= qdict_first(options
))) {
970 qdict_del(options
, e
->key
);
973 r
= qemu_rbd_connect(&s
->cluster
, &s
->io_ctx
, opts
,
974 !(flags
& BDRV_O_NOCACHE
), keypairs
, secretid
, errp
);
979 s
->snap
= g_strdup(opts
->snapshot
);
980 s
->image_name
= g_strdup(opts
->image
);
982 /* rbd_open is always r/w */
983 r
= rbd_open(s
->io_ctx
, s
->image_name
, &s
->image
, s
->snap
);
985 error_setg_errno(errp
, -r
, "error reading header from %s",
991 #ifdef LIBRBD_SUPPORTS_ENCRYPTION
992 r
= qemu_rbd_encryption_load(s
->image
, opts
->encrypt
, errp
);
994 goto failed_post_open
;
998 error_setg(errp
, "RBD library does not support image encryption");
999 goto failed_post_open
;
1003 r
= rbd_stat(s
->image
, &info
, sizeof(info
));
1005 error_setg_errno(errp
, -r
, "error getting image info from %s",
1007 goto failed_post_open
;
1009 s
->image_size
= info
.size
;
1010 s
->object_size
= info
.obj_size
;
1012 /* If we are using an rbd snapshot, we must be r/o, otherwise
1014 if (s
->snap
!= NULL
) {
1015 r
= bdrv_apply_auto_read_only(bs
, "rbd snapshots are read-only", errp
);
1017 goto failed_post_open
;
1021 #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1022 bs
->supported_zero_flags
= BDRV_REQ_MAY_UNMAP
| BDRV_REQ_NO_FALLBACK
;
1025 /* When extending regular files, we get zeros from the OS */
1026 bs
->supported_truncate_flags
= BDRV_REQ_ZERO_WRITE
;
1032 rbd_close(s
->image
);
1034 rados_ioctx_destroy(s
->io_ctx
);
1036 g_free(s
->image_name
);
1037 rados_shutdown(s
->cluster
);
1039 qapi_free_BlockdevOptionsRbd(opts
);
1046 /* Since RBD is currently always opened R/W via the API,
1047 * we just need to check if we are using a snapshot or not, in
1048 * order to determine if we will allow it to be R/W */
1049 static int qemu_rbd_reopen_prepare(BDRVReopenState
*state
,
1050 BlockReopenQueue
*queue
, Error
**errp
)
1052 BDRVRBDState
*s
= state
->bs
->opaque
;
1055 if (s
->snap
&& state
->flags
& BDRV_O_RDWR
) {
1057 "Cannot change node '%s' to r/w when using RBD snapshot",
1058 bdrv_get_device_or_node_name(state
->bs
));
1065 static void qemu_rbd_close(BlockDriverState
*bs
)
1067 BDRVRBDState
*s
= bs
->opaque
;
1069 rbd_close(s
->image
);
1070 rados_ioctx_destroy(s
->io_ctx
);
1072 g_free(s
->image_name
);
1073 rados_shutdown(s
->cluster
);
1076 /* Resize the RBD image and update the 'image_size' with the current size */
1077 static int qemu_rbd_resize(BlockDriverState
*bs
, uint64_t size
)
1079 BDRVRBDState
*s
= bs
->opaque
;
1082 r
= rbd_resize(s
->image
, size
);
1087 s
->image_size
= size
;
1092 static void qemu_rbd_finish_bh(void *opaque
)
1094 RBDTask
*task
= opaque
;
1095 task
->complete
= true;
1096 aio_co_wake(task
->co
);
1100 * This is the completion callback function for all rbd aio calls
1101 * started from qemu_rbd_start_co().
1103 * Note: this function is being called from a non qemu thread so
1104 * we need to be careful about what we do here. Generally we only
1105 * schedule a BH, and do the rest of the io completion handling
1106 * from qemu_rbd_finish_bh() which runs in a qemu context.
1108 static void qemu_rbd_completion_cb(rbd_completion_t c
, RBDTask
*task
)
1110 task
->ret
= rbd_aio_get_return_value(c
);
1112 aio_bh_schedule_oneshot(bdrv_get_aio_context(task
->bs
),
1113 qemu_rbd_finish_bh
, task
);
1116 static int coroutine_fn
qemu_rbd_start_co(BlockDriverState
*bs
,
1123 BDRVRBDState
*s
= bs
->opaque
;
1124 RBDTask task
= { .bs
= bs
, .co
= qemu_coroutine_self() };
1128 assert(!qiov
|| qiov
->size
== bytes
);
1130 if (cmd
== RBD_AIO_WRITE
|| cmd
== RBD_AIO_WRITE_ZEROES
) {
1132 * RBD APIs don't allow us to write more than actual size, so in order
1133 * to support growing images, we resize the image before write
1134 * operations that exceed the current size.
1136 if (offset
+ bytes
> s
->image_size
) {
1137 int r
= qemu_rbd_resize(bs
, offset
+ bytes
);
1144 r
= rbd_aio_create_completion(&task
,
1145 (rbd_callback_t
) qemu_rbd_completion_cb
, &c
);
1152 r
= rbd_aio_readv(s
->image
, qiov
->iov
, qiov
->niov
, offset
, c
);
1155 r
= rbd_aio_writev(s
->image
, qiov
->iov
, qiov
->niov
, offset
, c
);
1157 case RBD_AIO_DISCARD
:
1158 r
= rbd_aio_discard(s
->image
, offset
, bytes
, c
);
1161 r
= rbd_aio_flush(s
->image
, c
);
1163 #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1164 case RBD_AIO_WRITE_ZEROES
: {
1166 #ifdef RBD_WRITE_ZEROES_FLAG_THICK_PROVISION
1167 if (!(flags
& BDRV_REQ_MAY_UNMAP
)) {
1168 zero_flags
= RBD_WRITE_ZEROES_FLAG_THICK_PROVISION
;
1171 r
= rbd_aio_write_zeroes(s
->image
, offset
, bytes
, c
, zero_flags
, 0);
1180 error_report("rbd request failed early: cmd %d offset %" PRIu64
1181 " bytes %" PRIu64
" flags %d r %d (%s)", cmd
, offset
,
1182 bytes
, flags
, r
, strerror(-r
));
1187 while (!task
.complete
) {
1188 qemu_coroutine_yield();
1192 error_report("rbd request failed: cmd %d offset %" PRIu64
" bytes %"
1193 PRIu64
" flags %d task.ret %" PRIi64
" (%s)", cmd
, offset
,
1194 bytes
, flags
, task
.ret
, strerror(-task
.ret
));
1198 /* zero pad short reads */
1199 if (cmd
== RBD_AIO_READ
&& task
.ret
< qiov
->size
) {
1200 qemu_iovec_memset(qiov
, task
.ret
, 0, qiov
->size
- task
.ret
);
1207 coroutine_fn
qemu_rbd_co_preadv(BlockDriverState
*bs
, int64_t offset
,
1208 int64_t bytes
, QEMUIOVector
*qiov
,
1209 BdrvRequestFlags flags
)
1211 return qemu_rbd_start_co(bs
, offset
, bytes
, qiov
, flags
, RBD_AIO_READ
);
1215 coroutine_fn
qemu_rbd_co_pwritev(BlockDriverState
*bs
, int64_t offset
,
1216 int64_t bytes
, QEMUIOVector
*qiov
,
1217 BdrvRequestFlags flags
)
1219 return qemu_rbd_start_co(bs
, offset
, bytes
, qiov
, flags
, RBD_AIO_WRITE
);
1222 static int coroutine_fn
qemu_rbd_co_flush(BlockDriverState
*bs
)
1224 return qemu_rbd_start_co(bs
, 0, 0, NULL
, 0, RBD_AIO_FLUSH
);
1227 static int coroutine_fn
qemu_rbd_co_pdiscard(BlockDriverState
*bs
,
1228 int64_t offset
, int64_t bytes
)
1230 return qemu_rbd_start_co(bs
, offset
, bytes
, NULL
, 0, RBD_AIO_DISCARD
);
1233 #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1235 coroutine_fn
qemu_rbd_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
1236 int64_t bytes
, BdrvRequestFlags flags
)
1238 return qemu_rbd_start_co(bs
, offset
, bytes
, NULL
, flags
,
1239 RBD_AIO_WRITE_ZEROES
);
1243 static int coroutine_fn
1244 qemu_rbd_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
1246 BDRVRBDState
*s
= bs
->opaque
;
1247 bdi
->cluster_size
= s
->object_size
;
1251 static ImageInfoSpecific
*qemu_rbd_get_specific_info(BlockDriverState
*bs
,
1254 BDRVRBDState
*s
= bs
->opaque
;
1255 ImageInfoSpecific
*spec_info
;
1256 char buf
[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
] = {0};
1259 if (s
->image_size
>= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
) {
1260 r
= rbd_read(s
->image
, 0,
1261 RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
, buf
);
1263 error_setg_errno(errp
, -r
, "cannot read image start for probe");
1268 spec_info
= g_new(ImageInfoSpecific
, 1);
1269 *spec_info
= (ImageInfoSpecific
){
1270 .type
= IMAGE_INFO_SPECIFIC_KIND_RBD
,
1271 .u
.rbd
.data
= g_new0(ImageInfoSpecificRbd
, 1),
1274 if (memcmp(buf
, rbd_luks_header_verification
,
1275 RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
) == 0) {
1276 spec_info
->u
.rbd
.data
->encryption_format
=
1277 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS
;
1278 spec_info
->u
.rbd
.data
->has_encryption_format
= true;
1279 } else if (memcmp(buf
, rbd_luks2_header_verification
,
1280 RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN
) == 0) {
1281 spec_info
->u
.rbd
.data
->encryption_format
=
1282 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2
;
1283 spec_info
->u
.rbd
.data
->has_encryption_format
= true;
1285 spec_info
->u
.rbd
.data
->has_encryption_format
= false;
1292 * rbd_diff_iterate2 allows to interrupt the exection by returning a negative
1293 * value in the callback routine. Choose a value that does not conflict with
1294 * an existing exitcode and return it if we want to prematurely stop the
1295 * execution because we detected a change in the allocation status.
1297 #define QEMU_RBD_EXIT_DIFF_ITERATE2 -9000
1299 static int qemu_rbd_diff_iterate_cb(uint64_t offs
, size_t len
,
1300 int exists
, void *opaque
)
1302 RBDDiffIterateReq
*req
= opaque
;
1304 assert(req
->offs
+ req
->bytes
<= offs
);
1306 /* treat a hole like an unallocated area and bail out */
1311 if (!req
->exists
&& offs
> req
->offs
) {
1313 * we started in an unallocated area and hit the first allocated
1314 * block. req->bytes must be set to the length of the unallocated area
1315 * before the allocated area. stop further processing.
1317 req
->bytes
= offs
- req
->offs
;
1318 return QEMU_RBD_EXIT_DIFF_ITERATE2
;
1321 if (req
->exists
&& offs
> req
->offs
+ req
->bytes
) {
1323 * we started in an allocated area and jumped over an unallocated area,
1324 * req->bytes contains the length of the allocated area before the
1325 * unallocated area. stop further processing.
1327 return QEMU_RBD_EXIT_DIFF_ITERATE2
;
1336 static int coroutine_fn
qemu_rbd_co_block_status(BlockDriverState
*bs
,
1337 bool want_zero
, int64_t offset
,
1338 int64_t bytes
, int64_t *pnum
,
1340 BlockDriverState
**file
)
1342 BDRVRBDState
*s
= bs
->opaque
;
1344 RBDDiffIterateReq req
= { .offs
= offset
};
1345 uint64_t features
, flags
;
1348 assert(offset
+ bytes
<= s
->image_size
);
1350 /* default to all sectors allocated */
1351 status
= BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
1356 /* check if RBD image supports fast-diff */
1357 r
= rbd_get_features(s
->image
, &features
);
1361 if (!(features
& RBD_FEATURE_FAST_DIFF
)) {
1365 /* check if RBD fast-diff result is valid */
1366 r
= rbd_get_flags(s
->image
, &flags
);
1370 if (flags
& RBD_FLAG_FAST_DIFF_INVALID
) {
1374 #if LIBRBD_VERSION_CODE < LIBRBD_VERSION(1, 17, 0)
1376 * librbd had a bug until early 2022 that affected all versions of ceph that
1377 * supported fast-diff. This bug results in reporting of incorrect offsets
1378 * if the offset parameter to rbd_diff_iterate2 is not object aligned.
1379 * Work around this bug by rounding down the offset to object boundaries.
1380 * This is OK because we call rbd_diff_iterate2 with whole_object = true.
1381 * However, this workaround only works for non cloned images with default
1384 * See: https://tracker.ceph.com/issues/53784
1387 /* check if RBD image has non-default striping enabled */
1388 if (features
& RBD_FEATURE_STRIPINGV2
) {
1392 #pragma GCC diagnostic push
1393 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1395 * check if RBD image is a clone (= has a parent).
1397 * rbd_get_parent_info is deprecated from Nautilus onwards, but the
1398 * replacement rbd_get_parent is not present in Luminous and Mimic.
1400 if (rbd_get_parent_info(s
->image
, NULL
, 0, NULL
, 0, NULL
, 0) != -ENOENT
) {
1403 #pragma GCC diagnostic pop
1405 head
= req
.offs
& (s
->object_size
- 1);
1410 r
= rbd_diff_iterate2(s
->image
, NULL
, req
.offs
, bytes
, true, true,
1411 qemu_rbd_diff_iterate_cb
, &req
);
1412 if (r
< 0 && r
!= QEMU_RBD_EXIT_DIFF_ITERATE2
) {
1415 assert(req
.bytes
<= bytes
);
1419 * rbd_diff_iterate2 does not invoke callbacks for unallocated
1420 * areas. This here catches the case where no callback was
1421 * invoked at all (req.bytes == 0).
1423 assert(req
.bytes
== 0);
1426 status
= BDRV_BLOCK_ZERO
| BDRV_BLOCK_OFFSET_VALID
;
1429 assert(req
.bytes
> head
);
1430 *pnum
= req
.bytes
- head
;
1434 static int64_t coroutine_fn
qemu_rbd_co_getlength(BlockDriverState
*bs
)
1436 BDRVRBDState
*s
= bs
->opaque
;
1439 r
= rbd_get_size(s
->image
, &s
->image_size
);
1444 return s
->image_size
;
1447 static int coroutine_fn
qemu_rbd_co_truncate(BlockDriverState
*bs
,
1450 PreallocMode prealloc
,
1451 BdrvRequestFlags flags
,
1456 if (prealloc
!= PREALLOC_MODE_OFF
) {
1457 error_setg(errp
, "Unsupported preallocation mode '%s'",
1458 PreallocMode_str(prealloc
));
1462 r
= qemu_rbd_resize(bs
, offset
);
1464 error_setg_errno(errp
, -r
, "Failed to resize file");
1471 static int qemu_rbd_snap_create(BlockDriverState
*bs
,
1472 QEMUSnapshotInfo
*sn_info
)
1474 BDRVRBDState
*s
= bs
->opaque
;
1477 if (sn_info
->name
[0] == '\0') {
1478 return -EINVAL
; /* we need a name for rbd snapshots */
1482 * rbd snapshots are using the name as the user controlled unique identifier
1483 * we can't use the rbd snapid for that purpose, as it can't be set
1485 if (sn_info
->id_str
[0] != '\0' &&
1486 strcmp(sn_info
->id_str
, sn_info
->name
) != 0) {
1490 if (strlen(sn_info
->name
) >= sizeof(sn_info
->id_str
)) {
1494 r
= rbd_snap_create(s
->image
, sn_info
->name
);
1496 error_report("failed to create snap: %s", strerror(-r
));
1503 static int qemu_rbd_snap_remove(BlockDriverState
*bs
,
1504 const char *snapshot_id
,
1505 const char *snapshot_name
,
1508 BDRVRBDState
*s
= bs
->opaque
;
1511 if (!snapshot_name
) {
1512 error_setg(errp
, "rbd need a valid snapshot name");
1516 /* If snapshot_id is specified, it must be equal to name, see
1517 qemu_rbd_snap_list() */
1518 if (snapshot_id
&& strcmp(snapshot_id
, snapshot_name
)) {
1520 "rbd do not support snapshot id, it should be NULL or "
1521 "equal to snapshot name");
1525 r
= rbd_snap_remove(s
->image
, snapshot_name
);
1527 error_setg_errno(errp
, -r
, "Failed to remove the snapshot");
1532 static int qemu_rbd_snap_rollback(BlockDriverState
*bs
,
1533 const char *snapshot_name
)
1535 BDRVRBDState
*s
= bs
->opaque
;
1537 return rbd_snap_rollback(s
->image
, snapshot_name
);
1540 static int qemu_rbd_snap_list(BlockDriverState
*bs
,
1541 QEMUSnapshotInfo
**psn_tab
)
1543 BDRVRBDState
*s
= bs
->opaque
;
1544 QEMUSnapshotInfo
*sn_info
, *sn_tab
= NULL
;
1546 rbd_snap_info_t
*snaps
;
1547 int max_snaps
= RBD_MAX_SNAPS
;
1550 snaps
= g_new(rbd_snap_info_t
, max_snaps
);
1551 snap_count
= rbd_snap_list(s
->image
, snaps
, &max_snaps
);
1552 if (snap_count
<= 0) {
1555 } while (snap_count
== -ERANGE
);
1557 if (snap_count
<= 0) {
1561 sn_tab
= g_new0(QEMUSnapshotInfo
, snap_count
);
1563 for (i
= 0; i
< snap_count
; i
++) {
1564 const char *snap_name
= snaps
[i
].name
;
1566 sn_info
= sn_tab
+ i
;
1567 pstrcpy(sn_info
->id_str
, sizeof(sn_info
->id_str
), snap_name
);
1568 pstrcpy(sn_info
->name
, sizeof(sn_info
->name
), snap_name
);
1570 sn_info
->vm_state_size
= snaps
[i
].size
;
1571 sn_info
->date_sec
= 0;
1572 sn_info
->date_nsec
= 0;
1573 sn_info
->vm_clock_nsec
= 0;
1575 rbd_snap_list_end(snaps
);
1583 static void coroutine_fn
qemu_rbd_co_invalidate_cache(BlockDriverState
*bs
,
1586 BDRVRBDState
*s
= bs
->opaque
;
1587 int r
= rbd_invalidate_cache(s
->image
);
1589 error_setg_errno(errp
, -r
, "Failed to invalidate the cache");
1593 static QemuOptsList qemu_rbd_create_opts
= {
1594 .name
= "rbd-create-opts",
1595 .head
= QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts
.head
),
1598 .name
= BLOCK_OPT_SIZE
,
1599 .type
= QEMU_OPT_SIZE
,
1600 .help
= "Virtual disk size"
1603 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1604 .type
= QEMU_OPT_SIZE
,
1605 .help
= "RBD object size"
1608 .name
= "password-secret",
1609 .type
= QEMU_OPT_STRING
,
1610 .help
= "ID of secret providing the password",
1613 .name
= "encrypt.format",
1614 .type
= QEMU_OPT_STRING
,
1615 .help
= "Encrypt the image, format choices: 'luks', 'luks2'",
1618 .name
= "encrypt.cipher-alg",
1619 .type
= QEMU_OPT_STRING
,
1620 .help
= "Name of encryption cipher algorithm"
1621 " (allowed values: aes-128, aes-256)",
1624 .name
= "encrypt.key-secret",
1625 .type
= QEMU_OPT_STRING
,
1626 .help
= "ID of secret providing LUKS passphrase",
1628 { /* end of list */ }
1632 static const char *const qemu_rbd_strong_runtime_opts
[] = {
1645 static BlockDriver bdrv_rbd
= {
1646 .format_name
= "rbd",
1647 .instance_size
= sizeof(BDRVRBDState
),
1648 .bdrv_parse_filename
= qemu_rbd_parse_filename
,
1649 .bdrv_file_open
= qemu_rbd_open
,
1650 .bdrv_close
= qemu_rbd_close
,
1651 .bdrv_reopen_prepare
= qemu_rbd_reopen_prepare
,
1652 .bdrv_co_create
= qemu_rbd_co_create
,
1653 .bdrv_co_create_opts
= qemu_rbd_co_create_opts
,
1654 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1655 .bdrv_co_get_info
= qemu_rbd_co_get_info
,
1656 .bdrv_get_specific_info
= qemu_rbd_get_specific_info
,
1657 .create_opts
= &qemu_rbd_create_opts
,
1658 .bdrv_co_getlength
= qemu_rbd_co_getlength
,
1659 .bdrv_co_truncate
= qemu_rbd_co_truncate
,
1660 .protocol_name
= "rbd",
1662 .bdrv_co_preadv
= qemu_rbd_co_preadv
,
1663 .bdrv_co_pwritev
= qemu_rbd_co_pwritev
,
1664 .bdrv_co_flush_to_disk
= qemu_rbd_co_flush
,
1665 .bdrv_co_pdiscard
= qemu_rbd_co_pdiscard
,
1666 #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1667 .bdrv_co_pwrite_zeroes
= qemu_rbd_co_pwrite_zeroes
,
1669 .bdrv_co_block_status
= qemu_rbd_co_block_status
,
1671 .bdrv_snapshot_create
= qemu_rbd_snap_create
,
1672 .bdrv_snapshot_delete
= qemu_rbd_snap_remove
,
1673 .bdrv_snapshot_list
= qemu_rbd_snap_list
,
1674 .bdrv_snapshot_goto
= qemu_rbd_snap_rollback
,
1675 .bdrv_co_invalidate_cache
= qemu_rbd_co_invalidate_cache
,
1677 .strong_runtime_opts
= qemu_rbd_strong_runtime_opts
,
1680 static void bdrv_rbd_init(void)
1682 bdrv_register(&bdrv_rbd
);
1685 block_init(bdrv_rbd_init
);