1f27aaf4bSChristian Brunner /*
2f27aaf4bSChristian Brunner * QEMU Block driver for RADOS (Ceph)
3f27aaf4bSChristian Brunner *
4ad32e9c0SJosh Durgin * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5ad32e9c0SJosh Durgin * Josh Durgin <josh.durgin@dreamhost.com>
6f27aaf4bSChristian Brunner *
7f27aaf4bSChristian Brunner * This work is licensed under the terms of the GNU GPL, version 2. See
8f27aaf4bSChristian Brunner * the COPYING file in the top-level directory.
9f27aaf4bSChristian Brunner *
106b620ca3SPaolo Bonzini * Contributions after 2012-01-13 are licensed under the terms of the
116b620ca3SPaolo Bonzini * GNU GPL, version 2 or (at your option) any later version.
12f27aaf4bSChristian Brunner */
13f27aaf4bSChristian Brunner
1480c71a24SPeter Maydell #include "qemu/osdep.h"
15ad32e9c0SJosh Durgin
162836284dSMarkus Armbruster #include <rbd/librbd.h>
17da34e65cSMarkus Armbruster #include "qapi/error.h"
181de7afc9SPaolo Bonzini #include "qemu/error-report.h"
190b8fa32fSMarkus Armbruster #include "qemu/module.h"
20922a01a0SMarkus Armbruster #include "qemu/option.h"
21e2c1c34fSMarkus Armbruster #include "block/block-io.h"
22737e150eSPaolo Bonzini #include "block/block_int.h"
23609f45eaSMax Reitz #include "block/qdict.h"
2460390a21SDaniel P. Berrange #include "crypto/secret.h"
25f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
26e4ec5ad4SPavel Dovgalyuk #include "sysemu/replay.h"
27c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
28452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
29e98c6961SEric Blake #include "qapi/qmp/qjson.h"
3047e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
314bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h"
324bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h"
33f27aaf4bSChristian Brunner
34f27aaf4bSChristian Brunner /*
35f27aaf4bSChristian Brunner * When specifying the image filename use:
36f27aaf4bSChristian Brunner *
37fab5cf59SJosh Durgin * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
38f27aaf4bSChristian Brunner *
399e1fbcdeSSage Weil * poolname must be the name of an existing rados pool.
40f27aaf4bSChristian Brunner *
419e1fbcdeSSage Weil * devicename is the name of the rbd image.
42f27aaf4bSChristian Brunner *
439e1fbcdeSSage Weil * Each option given is used to configure rados, and may be any valid
449e1fbcdeSSage Weil * Ceph option, "id", or "conf".
45fab5cf59SJosh Durgin *
469e1fbcdeSSage Weil * The "id" option indicates what user we should authenticate as to
479e1fbcdeSSage Weil * the Ceph cluster. If it is excluded we will use the Ceph default
489e1fbcdeSSage Weil * (normally 'admin').
49f27aaf4bSChristian Brunner *
509e1fbcdeSSage Weil * The "conf" option specifies a Ceph configuration file to read. If
519e1fbcdeSSage Weil * it is not specified, we will read from the default Ceph locations
529e1fbcdeSSage Weil * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
539e1fbcdeSSage Weil * file, specify conf=/dev/null.
54f27aaf4bSChristian Brunner *
559e1fbcdeSSage Weil * Configuration values containing :, @, or = can be escaped with a
569e1fbcdeSSage Weil * leading "\".
57f27aaf4bSChristian Brunner */
58f27aaf4bSChristian Brunner
59f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
60f27aaf4bSChristian Brunner
61ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
62ad32e9c0SJosh Durgin
6342e4ac9eSOr Ozeri #define RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN 8
6442e4ac9eSOr Ozeri
6542e4ac9eSOr Ozeri static const char rbd_luks_header_verification[
6642e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
6742e4ac9eSOr Ozeri 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 1
6842e4ac9eSOr Ozeri };
6942e4ac9eSOr Ozeri
7042e4ac9eSOr Ozeri static const char rbd_luks2_header_verification[
7142e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
7242e4ac9eSOr Ozeri 'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 2
7342e4ac9eSOr Ozeri };
7442e4ac9eSOr Ozeri
750f385a24SOr Ozeri static const char rbd_layered_luks_header_verification[
760f385a24SOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
770f385a24SOr Ozeri 'R', 'B', 'D', 'L', 0xBA, 0xBE, 0, 1
780f385a24SOr Ozeri };
790f385a24SOr Ozeri
800f385a24SOr Ozeri static const char rbd_layered_luks2_header_verification[
810f385a24SOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
820f385a24SOr Ozeri 'R', 'B', 'D', 'L', 0xBA, 0xBE, 0, 2
830f385a24SOr Ozeri };
840f385a24SOr Ozeri
85787f3133SJosh Durgin typedef enum {
86787f3133SJosh Durgin RBD_AIO_READ,
87787f3133SJosh Durgin RBD_AIO_WRITE,
88dc7588c1SJosh Durgin RBD_AIO_DISCARD,
89c56ac27dSPeter Lieven RBD_AIO_FLUSH,
90c56ac27dSPeter Lieven RBD_AIO_WRITE_ZEROES
91787f3133SJosh Durgin } RBDAIOCmd;
92787f3133SJosh Durgin
93f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
94ad32e9c0SJosh Durgin rados_t cluster;
95ad32e9c0SJosh Durgin rados_ioctx_t io_ctx;
96ad32e9c0SJosh Durgin rbd_image_t image;
9780b61a27SJeff Cody char *image_name;
98ad32e9c0SJosh Durgin char *snap;
9919ae9ae0SFlorian Florensa char *namespace;
100d24f8023SStefano Garzarella uint64_t image_size;
101832a93dcSPeter Lieven uint64_t object_size;
102f27aaf4bSChristian Brunner } BDRVRBDState;
103f27aaf4bSChristian Brunner
104c3e5fac5SPeter Lieven typedef struct RBDTask {
105c3e5fac5SPeter Lieven BlockDriverState *bs;
106c3e5fac5SPeter Lieven Coroutine *co;
107c3e5fac5SPeter Lieven bool complete;
108c3e5fac5SPeter Lieven int64_t ret;
109c3e5fac5SPeter Lieven } RBDTask;
110c3e5fac5SPeter Lieven
1110347a8fdSPeter Lieven typedef struct RBDDiffIterateReq {
1120347a8fdSPeter Lieven uint64_t offs;
1130347a8fdSPeter Lieven uint64_t bytes;
1140347a8fdSPeter Lieven bool exists;
1150347a8fdSPeter Lieven } RBDDiffIterateReq;
1160347a8fdSPeter Lieven
117aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
118aa045c2dSKevin Wolf BlockdevOptionsRbd *opts, bool cache,
119aa045c2dSKevin Wolf const char *keypairs, const char *secretid,
120aa045c2dSKevin Wolf Error **errp);
121aa045c2dSKevin Wolf
qemu_rbd_strchr(char * src,char delim)1222b99cfceSConnor Kuehl static char *qemu_rbd_strchr(char *src, char delim)
1232b99cfceSConnor Kuehl {
1242b99cfceSConnor Kuehl char *p;
1252b99cfceSConnor Kuehl
1262b99cfceSConnor Kuehl for (p = src; *p; ++p) {
1272b99cfceSConnor Kuehl if (*p == delim) {
1282b99cfceSConnor Kuehl return p;
1292b99cfceSConnor Kuehl }
1302b99cfceSConnor Kuehl if (*p == '\\' && p[1] != '\0') {
1312b99cfceSConnor Kuehl ++p;
1322b99cfceSConnor Kuehl }
1332b99cfceSConnor Kuehl }
1342b99cfceSConnor Kuehl
1352b99cfceSConnor Kuehl return NULL;
1362b99cfceSConnor Kuehl }
1372b99cfceSConnor Kuehl
1382b99cfceSConnor Kuehl
qemu_rbd_next_tok(char * src,char delim,char ** p)139730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
140f27aaf4bSChristian Brunner {
141f27aaf4bSChristian Brunner char *end;
142f27aaf4bSChristian Brunner
143f27aaf4bSChristian Brunner *p = NULL;
144f27aaf4bSChristian Brunner
1452b99cfceSConnor Kuehl end = qemu_rbd_strchr(src, delim);
1462b99cfceSConnor Kuehl if (end) {
147f27aaf4bSChristian Brunner *p = end + 1;
148f27aaf4bSChristian Brunner *end = '\0';
149f27aaf4bSChristian Brunner }
1507830f909SJeff Cody return src;
151f27aaf4bSChristian Brunner }
152f27aaf4bSChristian Brunner
qemu_rbd_unescape(char * src)15316a06b24SSage Weil static void qemu_rbd_unescape(char *src)
15416a06b24SSage Weil {
15516a06b24SSage Weil char *p;
15616a06b24SSage Weil
15716a06b24SSage Weil for (p = src; *src; ++src, ++p) {
15816a06b24SSage Weil if (*src == '\\' && src[1] != '\0') {
15916a06b24SSage Weil src++;
16016a06b24SSage Weil }
16116a06b24SSage Weil *p = *src;
16216a06b24SSage Weil }
16316a06b24SSage Weil *p = '\0';
16416a06b24SSage Weil }
16516a06b24SSage Weil
qemu_rbd_parse_filename(const char * filename,QDict * options,Error ** errp)166c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
167d61563b2SMarkus Armbruster Error **errp)
168f27aaf4bSChristian Brunner {
169f27aaf4bSChristian Brunner const char *start;
170e98c6961SEric Blake char *p, *buf;
171e98c6961SEric Blake QList *keypairs = NULL;
17219ae9ae0SFlorian Florensa char *found_str, *image_name;
173f27aaf4bSChristian Brunner
174f27aaf4bSChristian Brunner if (!strstart(filename, "rbd:", &start)) {
175d61563b2SMarkus Armbruster error_setg(errp, "File name must start with 'rbd:'");
176c7cacb3eSJeff Cody return;
177f27aaf4bSChristian Brunner }
178f27aaf4bSChristian Brunner
1797267c094SAnthony Liguori buf = g_strdup(start);
180f27aaf4bSChristian Brunner p = buf;
181f27aaf4bSChristian Brunner
182730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, '/', &p);
1837830f909SJeff Cody if (!p) {
1847830f909SJeff Cody error_setg(errp, "Pool name is required");
1857830f909SJeff Cody goto done;
1867830f909SJeff Cody }
1877830f909SJeff Cody qemu_rbd_unescape(found_str);
18846f5ac20SEric Blake qdict_put_str(options, "pool", found_str);
189fab5cf59SJosh Durgin
1902b99cfceSConnor Kuehl if (qemu_rbd_strchr(p, '@')) {
19119ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, '@', &p);
1927830f909SJeff Cody
193730b00bbSMarkus Armbruster found_str = qemu_rbd_next_tok(p, ':', &p);
1947830f909SJeff Cody qemu_rbd_unescape(found_str);
19546f5ac20SEric Blake qdict_put_str(options, "snapshot", found_str);
1967830f909SJeff Cody } else {
19719ae9ae0SFlorian Florensa image_name = qemu_rbd_next_tok(p, ':', &p);
1987830f909SJeff Cody }
19919ae9ae0SFlorian Florensa /* Check for namespace in the image_name */
2002b99cfceSConnor Kuehl if (qemu_rbd_strchr(image_name, '/')) {
20119ae9ae0SFlorian Florensa found_str = qemu_rbd_next_tok(image_name, '/', &image_name);
20219ae9ae0SFlorian Florensa qemu_rbd_unescape(found_str);
20319ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", found_str);
20419ae9ae0SFlorian Florensa } else {
20519ae9ae0SFlorian Florensa qdict_put_str(options, "namespace", "");
20619ae9ae0SFlorian Florensa }
20719ae9ae0SFlorian Florensa qemu_rbd_unescape(image_name);
20819ae9ae0SFlorian Florensa qdict_put_str(options, "image", image_name);
2097830f909SJeff Cody if (!p) {
210f27aaf4bSChristian Brunner goto done;
211f27aaf4bSChristian Brunner }
212f27aaf4bSChristian Brunner
213c7cacb3eSJeff Cody /* The following are essentially all key/value pairs, and we treat
214c7cacb3eSJeff Cody * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
215c7cacb3eSJeff Cody while (p) {
216c7cacb3eSJeff Cody char *name, *value;
217730b00bbSMarkus Armbruster name = qemu_rbd_next_tok(p, '=', &p);
218c7cacb3eSJeff Cody if (!p) {
219c7cacb3eSJeff Cody error_setg(errp, "conf option %s has no value", name);
220c7cacb3eSJeff Cody break;
221c7cacb3eSJeff Cody }
222c7cacb3eSJeff Cody
223c7cacb3eSJeff Cody qemu_rbd_unescape(name);
224c7cacb3eSJeff Cody
225730b00bbSMarkus Armbruster value = qemu_rbd_next_tok(p, ':', &p);
226c7cacb3eSJeff Cody qemu_rbd_unescape(value);
227c7cacb3eSJeff Cody
228c7cacb3eSJeff Cody if (!strcmp(name, "conf")) {
22946f5ac20SEric Blake qdict_put_str(options, "conf", value);
230c7cacb3eSJeff Cody } else if (!strcmp(name, "id")) {
23146f5ac20SEric Blake qdict_put_str(options, "user", value);
232c7cacb3eSJeff Cody } else {
233e98c6961SEric Blake /*
234e98c6961SEric Blake * We pass these internally to qemu_rbd_set_keypairs(), so
235e98c6961SEric Blake * we can get away with the simpler list of [ "key1",
236e98c6961SEric Blake * "value1", "key2", "value2" ] rather than a raw dict
237e98c6961SEric Blake * { "key1": "value1", "key2": "value2" } where we can't
238e98c6961SEric Blake * guarantee order, or even a more correct but complex
239e98c6961SEric Blake * [ { "key1": "value1" }, { "key2": "value2" } ]
240e98c6961SEric Blake */
241e98c6961SEric Blake if (!keypairs) {
242e98c6961SEric Blake keypairs = qlist_new();
243c7cacb3eSJeff Cody }
24446f5ac20SEric Blake qlist_append_str(keypairs, name);
24546f5ac20SEric Blake qlist_append_str(keypairs, value);
246c7cacb3eSJeff Cody }
247c7cacb3eSJeff Cody }
248c7cacb3eSJeff Cody
249e98c6961SEric Blake if (keypairs) {
250e98c6961SEric Blake qdict_put(options, "=keyvalue-pairs",
251eab3a467SMarkus Armbruster qstring_from_gstring(qobject_to_json(QOBJECT(keypairs))));
252c7cacb3eSJeff Cody }
253c7cacb3eSJeff Cody
254f27aaf4bSChristian Brunner done:
2557267c094SAnthony Liguori g_free(buf);
256cb3e7f08SMarc-André Lureau qobject_unref(keypairs);
257c7cacb3eSJeff Cody return;
2587c7e9df0SSage Weil }
2597c7e9df0SSage Weil
qemu_rbd_set_auth(rados_t cluster,BlockdevOptionsRbd * opts,Error ** errp)260d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
26160390a21SDaniel P. Berrange Error **errp)
26260390a21SDaniel P. Berrange {
263d083f954SMarkus Armbruster char *key, *acr;
264a3699de4SMarkus Armbruster int r;
265a3699de4SMarkus Armbruster GString *accu;
266a3699de4SMarkus Armbruster RbdAuthModeList *auth;
26760390a21SDaniel P. Berrange
268d083f954SMarkus Armbruster if (opts->key_secret) {
269d083f954SMarkus Armbruster key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
270d083f954SMarkus Armbruster if (!key) {
271d083f954SMarkus Armbruster return -EIO;
27260390a21SDaniel P. Berrange }
273d083f954SMarkus Armbruster r = rados_conf_set(cluster, "key", key);
274d083f954SMarkus Armbruster g_free(key);
275d083f954SMarkus Armbruster if (r < 0) {
276d083f954SMarkus Armbruster error_setg_errno(errp, -r, "Could not set 'key'");
277d083f954SMarkus Armbruster return r;
278d083f954SMarkus Armbruster }
279a3699de4SMarkus Armbruster }
280a3699de4SMarkus Armbruster
281a3699de4SMarkus Armbruster if (opts->has_auth_client_required) {
282a3699de4SMarkus Armbruster accu = g_string_new("");
283a3699de4SMarkus Armbruster for (auth = opts->auth_client_required; auth; auth = auth->next) {
284a3699de4SMarkus Armbruster if (accu->str[0]) {
285a3699de4SMarkus Armbruster g_string_append_c(accu, ';');
286a3699de4SMarkus Armbruster }
287a3699de4SMarkus Armbruster g_string_append(accu, RbdAuthMode_str(auth->value));
288a3699de4SMarkus Armbruster }
289a3699de4SMarkus Armbruster acr = g_string_free(accu, FALSE);
290a3699de4SMarkus Armbruster r = rados_conf_set(cluster, "auth_client_required", acr);
291a3699de4SMarkus Armbruster g_free(acr);
292a3699de4SMarkus Armbruster if (r < 0) {
293a3699de4SMarkus Armbruster error_setg_errno(errp, -r,
294a3699de4SMarkus Armbruster "Could not set 'auth_client_required'");
295a3699de4SMarkus Armbruster return r;
296a3699de4SMarkus Armbruster }
297a3699de4SMarkus Armbruster }
29860390a21SDaniel P. Berrange
29960390a21SDaniel P. Berrange return 0;
30060390a21SDaniel P. Berrange }
30160390a21SDaniel P. Berrange
qemu_rbd_set_keypairs(rados_t cluster,const char * keypairs_json,Error ** errp)302e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
303e34d8f29SJosh Durgin Error **errp)
304fab5cf59SJosh Durgin {
305e98c6961SEric Blake QList *keypairs;
306e98c6961SEric Blake QString *name;
307e98c6961SEric Blake QString *value;
308e98c6961SEric Blake const char *key;
309e98c6961SEric Blake size_t remaining;
310fab5cf59SJosh Durgin int ret = 0;
311fab5cf59SJosh Durgin
312e98c6961SEric Blake if (!keypairs_json) {
313e98c6961SEric Blake return ret;
314fab5cf59SJosh Durgin }
3157dc847ebSMax Reitz keypairs = qobject_to(QList,
3167dc847ebSMax Reitz qobject_from_json(keypairs_json, &error_abort));
317e98c6961SEric Blake remaining = qlist_size(keypairs) / 2;
318e98c6961SEric Blake assert(remaining);
319fab5cf59SJosh Durgin
320e98c6961SEric Blake while (remaining--) {
3217dc847ebSMax Reitz name = qobject_to(QString, qlist_pop(keypairs));
3227dc847ebSMax Reitz value = qobject_to(QString, qlist_pop(keypairs));
323e98c6961SEric Blake assert(name && value);
324e98c6961SEric Blake key = qstring_get_str(name);
325fab5cf59SJosh Durgin
326e98c6961SEric Blake ret = rados_conf_set(cluster, key, qstring_get_str(value));
327cb3e7f08SMarc-André Lureau qobject_unref(value);
328fab5cf59SJosh Durgin if (ret < 0) {
329e98c6961SEric Blake error_setg_errno(errp, -ret, "invalid conf option %s", key);
330cb3e7f08SMarc-André Lureau qobject_unref(name);
331fab5cf59SJosh Durgin ret = -EINVAL;
332fab5cf59SJosh Durgin break;
333fab5cf59SJosh Durgin }
334cb3e7f08SMarc-André Lureau qobject_unref(name);
335fab5cf59SJosh Durgin }
336fab5cf59SJosh Durgin
337cb3e7f08SMarc-André Lureau qobject_unref(keypairs);
338fab5cf59SJosh Durgin return ret;
339fab5cf59SJosh Durgin }
340fab5cf59SJosh Durgin
34142e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
qemu_rbd_convert_luks_options(RbdEncryptionOptionsLUKSBase * luks_opts,char ** passphrase,size_t * passphrase_len,Error ** errp)34242e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_options(
34342e4ac9eSOr Ozeri RbdEncryptionOptionsLUKSBase *luks_opts,
34442e4ac9eSOr Ozeri char **passphrase,
34542e4ac9eSOr Ozeri size_t *passphrase_len,
34642e4ac9eSOr Ozeri Error **errp)
34742e4ac9eSOr Ozeri {
34842e4ac9eSOr Ozeri return qcrypto_secret_lookup(luks_opts->key_secret, (uint8_t **)passphrase,
34942e4ac9eSOr Ozeri passphrase_len, errp);
35042e4ac9eSOr Ozeri }
35142e4ac9eSOr Ozeri
qemu_rbd_convert_luks_create_options(RbdEncryptionCreateOptionsLUKSBase * luks_opts,rbd_encryption_algorithm_t * alg,char ** passphrase,size_t * passphrase_len,Error ** errp)35242e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_create_options(
35342e4ac9eSOr Ozeri RbdEncryptionCreateOptionsLUKSBase *luks_opts,
35442e4ac9eSOr Ozeri rbd_encryption_algorithm_t *alg,
35542e4ac9eSOr Ozeri char **passphrase,
35642e4ac9eSOr Ozeri size_t *passphrase_len,
35742e4ac9eSOr Ozeri Error **errp)
35842e4ac9eSOr Ozeri {
35942e4ac9eSOr Ozeri int r = 0;
36042e4ac9eSOr Ozeri
36142e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options(
36242e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts),
36342e4ac9eSOr Ozeri passphrase, passphrase_len, errp);
36442e4ac9eSOr Ozeri if (r < 0) {
36542e4ac9eSOr Ozeri return r;
36642e4ac9eSOr Ozeri }
36742e4ac9eSOr Ozeri
36842e4ac9eSOr Ozeri if (luks_opts->has_cipher_alg) {
36942e4ac9eSOr Ozeri switch (luks_opts->cipher_alg) {
370*a092c513SMarkus Armbruster case QCRYPTO_CIPHER_ALGO_AES_128: {
37142e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES128;
37242e4ac9eSOr Ozeri break;
37342e4ac9eSOr Ozeri }
374*a092c513SMarkus Armbruster case QCRYPTO_CIPHER_ALGO_AES_256: {
37542e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
37642e4ac9eSOr Ozeri break;
37742e4ac9eSOr Ozeri }
37842e4ac9eSOr Ozeri default: {
37942e4ac9eSOr Ozeri r = -ENOTSUP;
38042e4ac9eSOr Ozeri error_setg_errno(errp, -r, "unknown encryption algorithm: %u",
38142e4ac9eSOr Ozeri luks_opts->cipher_alg);
38242e4ac9eSOr Ozeri return r;
38342e4ac9eSOr Ozeri }
38442e4ac9eSOr Ozeri }
38542e4ac9eSOr Ozeri } else {
38642e4ac9eSOr Ozeri /* default alg */
38742e4ac9eSOr Ozeri *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
38842e4ac9eSOr Ozeri }
38942e4ac9eSOr Ozeri
39042e4ac9eSOr Ozeri return 0;
39142e4ac9eSOr Ozeri }
39242e4ac9eSOr Ozeri
qemu_rbd_encryption_format(rbd_image_t image,RbdEncryptionCreateOptions * encrypt,Error ** errp)39342e4ac9eSOr Ozeri static int qemu_rbd_encryption_format(rbd_image_t image,
39442e4ac9eSOr Ozeri RbdEncryptionCreateOptions *encrypt,
39542e4ac9eSOr Ozeri Error **errp)
39642e4ac9eSOr Ozeri {
39742e4ac9eSOr Ozeri int r = 0;
39842e4ac9eSOr Ozeri g_autofree char *passphrase = NULL;
39942e4ac9eSOr Ozeri rbd_encryption_format_t format;
40042e4ac9eSOr Ozeri rbd_encryption_options_t opts;
40142e4ac9eSOr Ozeri rbd_encryption_luks1_format_options_t luks_opts;
40242e4ac9eSOr Ozeri rbd_encryption_luks2_format_options_t luks2_opts;
40342e4ac9eSOr Ozeri size_t opts_size;
40442e4ac9eSOr Ozeri uint64_t raw_size, effective_size;
40542e4ac9eSOr Ozeri
40642e4ac9eSOr Ozeri r = rbd_get_size(image, &raw_size);
40742e4ac9eSOr Ozeri if (r < 0) {
40842e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot get raw image size");
40942e4ac9eSOr Ozeri return r;
41042e4ac9eSOr Ozeri }
41142e4ac9eSOr Ozeri
41242e4ac9eSOr Ozeri switch (encrypt->format) {
41342e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
41442e4ac9eSOr Ozeri memset(&luks_opts, 0, sizeof(luks_opts));
41542e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS1;
41642e4ac9eSOr Ozeri opts = &luks_opts;
41742e4ac9eSOr Ozeri opts_size = sizeof(luks_opts);
41842e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_create_options(
41942e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt->u.luks),
420a4ac51acSOr Ozeri &luks_opts.alg, &passphrase, &luks_opts.passphrase_size,
421a4ac51acSOr Ozeri errp);
42242e4ac9eSOr Ozeri if (r < 0) {
42342e4ac9eSOr Ozeri return r;
42442e4ac9eSOr Ozeri }
42542e4ac9eSOr Ozeri luks_opts.passphrase = passphrase;
42642e4ac9eSOr Ozeri break;
42742e4ac9eSOr Ozeri }
42842e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
42942e4ac9eSOr Ozeri memset(&luks2_opts, 0, sizeof(luks2_opts));
43042e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS2;
43142e4ac9eSOr Ozeri opts = &luks2_opts;
43242e4ac9eSOr Ozeri opts_size = sizeof(luks2_opts);
43342e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_create_options(
43442e4ac9eSOr Ozeri qapi_RbdEncryptionCreateOptionsLUKS2_base(
43542e4ac9eSOr Ozeri &encrypt->u.luks2),
436a4ac51acSOr Ozeri &luks2_opts.alg, &passphrase, &luks2_opts.passphrase_size,
437a4ac51acSOr Ozeri errp);
43842e4ac9eSOr Ozeri if (r < 0) {
43942e4ac9eSOr Ozeri return r;
44042e4ac9eSOr Ozeri }
44142e4ac9eSOr Ozeri luks2_opts.passphrase = passphrase;
44242e4ac9eSOr Ozeri break;
44342e4ac9eSOr Ozeri }
44442e4ac9eSOr Ozeri default: {
44542e4ac9eSOr Ozeri r = -ENOTSUP;
44642e4ac9eSOr Ozeri error_setg_errno(
44742e4ac9eSOr Ozeri errp, -r, "unknown image encryption format: %u",
44842e4ac9eSOr Ozeri encrypt->format);
44942e4ac9eSOr Ozeri return r;
45042e4ac9eSOr Ozeri }
45142e4ac9eSOr Ozeri }
45242e4ac9eSOr Ozeri
45342e4ac9eSOr Ozeri r = rbd_encryption_format(image, format, opts, opts_size);
45442e4ac9eSOr Ozeri if (r < 0) {
45542e4ac9eSOr Ozeri error_setg_errno(errp, -r, "encryption format fail");
45642e4ac9eSOr Ozeri return r;
45742e4ac9eSOr Ozeri }
45842e4ac9eSOr Ozeri
45942e4ac9eSOr Ozeri r = rbd_get_size(image, &effective_size);
46042e4ac9eSOr Ozeri if (r < 0) {
46142e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot get effective image size");
46242e4ac9eSOr Ozeri return r;
46342e4ac9eSOr Ozeri }
46442e4ac9eSOr Ozeri
46542e4ac9eSOr Ozeri r = rbd_resize(image, raw_size + (raw_size - effective_size));
46642e4ac9eSOr Ozeri if (r < 0) {
46742e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot resize image after format");
46842e4ac9eSOr Ozeri return r;
46942e4ac9eSOr Ozeri }
47042e4ac9eSOr Ozeri
47142e4ac9eSOr Ozeri return 0;
47242e4ac9eSOr Ozeri }
47342e4ac9eSOr Ozeri
qemu_rbd_encryption_load(rbd_image_t image,RbdEncryptionOptions * encrypt,Error ** errp)47442e4ac9eSOr Ozeri static int qemu_rbd_encryption_load(rbd_image_t image,
47542e4ac9eSOr Ozeri RbdEncryptionOptions *encrypt,
47642e4ac9eSOr Ozeri Error **errp)
47742e4ac9eSOr Ozeri {
47842e4ac9eSOr Ozeri int r = 0;
47942e4ac9eSOr Ozeri g_autofree char *passphrase = NULL;
48042e4ac9eSOr Ozeri rbd_encryption_luks1_format_options_t luks_opts;
48142e4ac9eSOr Ozeri rbd_encryption_luks2_format_options_t luks2_opts;
482b8f218efSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2
483b8f218efSOr Ozeri rbd_encryption_luks_format_options_t luks_any_opts;
484b8f218efSOr Ozeri #endif
48542e4ac9eSOr Ozeri rbd_encryption_format_t format;
48642e4ac9eSOr Ozeri rbd_encryption_options_t opts;
48742e4ac9eSOr Ozeri size_t opts_size;
48842e4ac9eSOr Ozeri
48942e4ac9eSOr Ozeri switch (encrypt->format) {
49042e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
49142e4ac9eSOr Ozeri memset(&luks_opts, 0, sizeof(luks_opts));
49242e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS1;
49342e4ac9eSOr Ozeri opts = &luks_opts;
49442e4ac9eSOr Ozeri opts_size = sizeof(luks_opts);
49542e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options(
49642e4ac9eSOr Ozeri qapi_RbdEncryptionOptionsLUKS_base(&encrypt->u.luks),
497a4ac51acSOr Ozeri &passphrase, &luks_opts.passphrase_size, errp);
49842e4ac9eSOr Ozeri if (r < 0) {
49942e4ac9eSOr Ozeri return r;
50042e4ac9eSOr Ozeri }
50142e4ac9eSOr Ozeri luks_opts.passphrase = passphrase;
50242e4ac9eSOr Ozeri break;
50342e4ac9eSOr Ozeri }
50442e4ac9eSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
50542e4ac9eSOr Ozeri memset(&luks2_opts, 0, sizeof(luks2_opts));
50642e4ac9eSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS2;
50742e4ac9eSOr Ozeri opts = &luks2_opts;
50842e4ac9eSOr Ozeri opts_size = sizeof(luks2_opts);
50942e4ac9eSOr Ozeri r = qemu_rbd_convert_luks_options(
51042e4ac9eSOr Ozeri qapi_RbdEncryptionOptionsLUKS2_base(&encrypt->u.luks2),
511a4ac51acSOr Ozeri &passphrase, &luks2_opts.passphrase_size, errp);
51242e4ac9eSOr Ozeri if (r < 0) {
51342e4ac9eSOr Ozeri return r;
51442e4ac9eSOr Ozeri }
51542e4ac9eSOr Ozeri luks2_opts.passphrase = passphrase;
51642e4ac9eSOr Ozeri break;
51742e4ac9eSOr Ozeri }
518b8f218efSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2
519b8f218efSOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS_ANY: {
520b8f218efSOr Ozeri memset(&luks_any_opts, 0, sizeof(luks_any_opts));
521b8f218efSOr Ozeri format = RBD_ENCRYPTION_FORMAT_LUKS;
522b8f218efSOr Ozeri opts = &luks_any_opts;
523b8f218efSOr Ozeri opts_size = sizeof(luks_any_opts);
524b8f218efSOr Ozeri r = qemu_rbd_convert_luks_options(
525b8f218efSOr Ozeri qapi_RbdEncryptionOptionsLUKSAny_base(&encrypt->u.luks_any),
526b8f218efSOr Ozeri &passphrase, &luks_any_opts.passphrase_size, errp);
527b8f218efSOr Ozeri if (r < 0) {
528b8f218efSOr Ozeri return r;
529b8f218efSOr Ozeri }
530b8f218efSOr Ozeri luks_any_opts.passphrase = passphrase;
531b8f218efSOr Ozeri break;
532b8f218efSOr Ozeri }
533b8f218efSOr Ozeri #endif
53442e4ac9eSOr Ozeri default: {
53542e4ac9eSOr Ozeri r = -ENOTSUP;
53642e4ac9eSOr Ozeri error_setg_errno(
53742e4ac9eSOr Ozeri errp, -r, "unknown image encryption format: %u",
53842e4ac9eSOr Ozeri encrypt->format);
53942e4ac9eSOr Ozeri return r;
54042e4ac9eSOr Ozeri }
54142e4ac9eSOr Ozeri }
54242e4ac9eSOr Ozeri
54342e4ac9eSOr Ozeri r = rbd_encryption_load(image, format, opts, opts_size);
54442e4ac9eSOr Ozeri if (r < 0) {
54542e4ac9eSOr Ozeri error_setg_errno(errp, -r, "encryption load fail");
54642e4ac9eSOr Ozeri return r;
54742e4ac9eSOr Ozeri }
54842e4ac9eSOr Ozeri
54942e4ac9eSOr Ozeri return 0;
55042e4ac9eSOr Ozeri }
5510f385a24SOr Ozeri
5520f385a24SOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2
qemu_rbd_encryption_load2(rbd_image_t image,RbdEncryptionOptions * encrypt,Error ** errp)5530f385a24SOr Ozeri static int qemu_rbd_encryption_load2(rbd_image_t image,
5540f385a24SOr Ozeri RbdEncryptionOptions *encrypt,
5550f385a24SOr Ozeri Error **errp)
5560f385a24SOr Ozeri {
5570f385a24SOr Ozeri int r = 0;
5580f385a24SOr Ozeri int encrypt_count = 1;
5590f385a24SOr Ozeri int i;
5600f385a24SOr Ozeri RbdEncryptionOptions *curr_encrypt;
5610f385a24SOr Ozeri rbd_encryption_spec_t *specs;
5620f385a24SOr Ozeri rbd_encryption_luks1_format_options_t *luks_opts;
5630f385a24SOr Ozeri rbd_encryption_luks2_format_options_t *luks2_opts;
5640f385a24SOr Ozeri rbd_encryption_luks_format_options_t *luks_any_opts;
5650f385a24SOr Ozeri
5660f385a24SOr Ozeri /* count encryption options */
5670f385a24SOr Ozeri for (curr_encrypt = encrypt->parent; curr_encrypt;
5680f385a24SOr Ozeri curr_encrypt = curr_encrypt->parent) {
5690f385a24SOr Ozeri ++encrypt_count;
5700f385a24SOr Ozeri }
5710f385a24SOr Ozeri
5720f385a24SOr Ozeri specs = g_new0(rbd_encryption_spec_t, encrypt_count);
5730f385a24SOr Ozeri
5740f385a24SOr Ozeri curr_encrypt = encrypt;
5750f385a24SOr Ozeri for (i = 0; i < encrypt_count; ++i) {
5760f385a24SOr Ozeri switch (curr_encrypt->format) {
5770f385a24SOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
5780f385a24SOr Ozeri specs[i].format = RBD_ENCRYPTION_FORMAT_LUKS1;
5790f385a24SOr Ozeri
5800f385a24SOr Ozeri luks_opts = g_new0(rbd_encryption_luks1_format_options_t, 1);
5810f385a24SOr Ozeri specs[i].opts = luks_opts;
5820f385a24SOr Ozeri specs[i].opts_size = sizeof(*luks_opts);
5830f385a24SOr Ozeri
5840f385a24SOr Ozeri r = qemu_rbd_convert_luks_options(
5850f385a24SOr Ozeri qapi_RbdEncryptionOptionsLUKS_base(
5860f385a24SOr Ozeri &curr_encrypt->u.luks),
5870f385a24SOr Ozeri (char **)&luks_opts->passphrase,
5880f385a24SOr Ozeri &luks_opts->passphrase_size,
5890f385a24SOr Ozeri errp);
5900f385a24SOr Ozeri break;
5910f385a24SOr Ozeri }
5920f385a24SOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
5930f385a24SOr Ozeri specs[i].format = RBD_ENCRYPTION_FORMAT_LUKS2;
5940f385a24SOr Ozeri
5950f385a24SOr Ozeri luks2_opts = g_new0(rbd_encryption_luks2_format_options_t, 1);
5960f385a24SOr Ozeri specs[i].opts = luks2_opts;
5970f385a24SOr Ozeri specs[i].opts_size = sizeof(*luks2_opts);
5980f385a24SOr Ozeri
5990f385a24SOr Ozeri r = qemu_rbd_convert_luks_options(
6000f385a24SOr Ozeri qapi_RbdEncryptionOptionsLUKS2_base(
6010f385a24SOr Ozeri &curr_encrypt->u.luks2),
6020f385a24SOr Ozeri (char **)&luks2_opts->passphrase,
6030f385a24SOr Ozeri &luks2_opts->passphrase_size,
6040f385a24SOr Ozeri errp);
6050f385a24SOr Ozeri break;
6060f385a24SOr Ozeri }
6070f385a24SOr Ozeri case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS_ANY: {
6080f385a24SOr Ozeri specs[i].format = RBD_ENCRYPTION_FORMAT_LUKS;
6090f385a24SOr Ozeri
6100f385a24SOr Ozeri luks_any_opts = g_new0(rbd_encryption_luks_format_options_t, 1);
6110f385a24SOr Ozeri specs[i].opts = luks_any_opts;
6120f385a24SOr Ozeri specs[i].opts_size = sizeof(*luks_any_opts);
6130f385a24SOr Ozeri
6140f385a24SOr Ozeri r = qemu_rbd_convert_luks_options(
6150f385a24SOr Ozeri qapi_RbdEncryptionOptionsLUKSAny_base(
6160f385a24SOr Ozeri &curr_encrypt->u.luks_any),
6170f385a24SOr Ozeri (char **)&luks_any_opts->passphrase,
6180f385a24SOr Ozeri &luks_any_opts->passphrase_size,
6190f385a24SOr Ozeri errp);
6200f385a24SOr Ozeri break;
6210f385a24SOr Ozeri }
6220f385a24SOr Ozeri default: {
6230f385a24SOr Ozeri r = -ENOTSUP;
6240f385a24SOr Ozeri error_setg_errno(
6250f385a24SOr Ozeri errp, -r, "unknown image encryption format: %u",
6260f385a24SOr Ozeri curr_encrypt->format);
6270f385a24SOr Ozeri }
6280f385a24SOr Ozeri }
6290f385a24SOr Ozeri
6300f385a24SOr Ozeri if (r < 0) {
6310f385a24SOr Ozeri goto exit;
6320f385a24SOr Ozeri }
6330f385a24SOr Ozeri
6340f385a24SOr Ozeri curr_encrypt = curr_encrypt->parent;
6350f385a24SOr Ozeri }
6360f385a24SOr Ozeri
6370f385a24SOr Ozeri r = rbd_encryption_load2(image, specs, encrypt_count);
6380f385a24SOr Ozeri if (r < 0) {
6390f385a24SOr Ozeri error_setg_errno(errp, -r, "layered encryption load fail");
6400f385a24SOr Ozeri goto exit;
6410f385a24SOr Ozeri }
6420f385a24SOr Ozeri
6430f385a24SOr Ozeri exit:
6440f385a24SOr Ozeri for (i = 0; i < encrypt_count; ++i) {
6450f385a24SOr Ozeri if (!specs[i].opts) {
6460f385a24SOr Ozeri break;
6470f385a24SOr Ozeri }
6480f385a24SOr Ozeri
6490f385a24SOr Ozeri switch (specs[i].format) {
6500f385a24SOr Ozeri case RBD_ENCRYPTION_FORMAT_LUKS1: {
6510f385a24SOr Ozeri luks_opts = specs[i].opts;
6520f385a24SOr Ozeri g_free((void *)luks_opts->passphrase);
6530f385a24SOr Ozeri break;
6540f385a24SOr Ozeri }
6550f385a24SOr Ozeri case RBD_ENCRYPTION_FORMAT_LUKS2: {
6560f385a24SOr Ozeri luks2_opts = specs[i].opts;
6570f385a24SOr Ozeri g_free((void *)luks2_opts->passphrase);
6580f385a24SOr Ozeri break;
6590f385a24SOr Ozeri }
6600f385a24SOr Ozeri case RBD_ENCRYPTION_FORMAT_LUKS: {
6610f385a24SOr Ozeri luks_any_opts = specs[i].opts;
6620f385a24SOr Ozeri g_free((void *)luks_any_opts->passphrase);
6630f385a24SOr Ozeri break;
6640f385a24SOr Ozeri }
6650f385a24SOr Ozeri }
6660f385a24SOr Ozeri
6670f385a24SOr Ozeri g_free(specs[i].opts);
6680f385a24SOr Ozeri }
6690f385a24SOr Ozeri g_free(specs);
6700f385a24SOr Ozeri return r;
6710f385a24SOr Ozeri }
6720f385a24SOr Ozeri #endif
67342e4ac9eSOr Ozeri #endif
67442e4ac9eSOr Ozeri
675d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */
qemu_rbd_do_create(BlockdevCreateOptions * options,const char * keypairs,const char * password_secret,Error ** errp)6761bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
6771bebea37SKevin Wolf const char *keypairs, const char *password_secret,
6781bebea37SKevin Wolf Error **errp)
6791bebea37SKevin Wolf {
6801bebea37SKevin Wolf BlockdevCreateOptionsRbd *opts = &options->u.rbd;
6811bebea37SKevin Wolf rados_t cluster;
6821bebea37SKevin Wolf rados_ioctx_t io_ctx;
6831bebea37SKevin Wolf int obj_order = 0;
6841bebea37SKevin Wolf int ret;
6851bebea37SKevin Wolf
6861bebea37SKevin Wolf assert(options->driver == BLOCKDEV_DRIVER_RBD);
68754fde4ffSMarkus Armbruster if (opts->location->snapshot) {
6881bebea37SKevin Wolf error_setg(errp, "Can't use snapshot name for image creation");
6891bebea37SKevin Wolf return -EINVAL;
6901bebea37SKevin Wolf }
6911bebea37SKevin Wolf
69242e4ac9eSOr Ozeri #ifndef LIBRBD_SUPPORTS_ENCRYPTION
69354fde4ffSMarkus Armbruster if (opts->encrypt) {
69442e4ac9eSOr Ozeri error_setg(errp, "RBD library does not support image encryption");
69542e4ac9eSOr Ozeri return -ENOTSUP;
69642e4ac9eSOr Ozeri }
69742e4ac9eSOr Ozeri #endif
69842e4ac9eSOr Ozeri
6991bebea37SKevin Wolf if (opts->has_cluster_size) {
7001bebea37SKevin Wolf int64_t objsize = opts->cluster_size;
7011bebea37SKevin Wolf if ((objsize - 1) & objsize) { /* not a power of 2? */
7021bebea37SKevin Wolf error_setg(errp, "obj size needs to be power of 2");
7031bebea37SKevin Wolf return -EINVAL;
7041bebea37SKevin Wolf }
7051bebea37SKevin Wolf if (objsize < 4096) {
7061bebea37SKevin Wolf error_setg(errp, "obj size too small");
7071bebea37SKevin Wolf return -EINVAL;
7081bebea37SKevin Wolf }
7091bebea37SKevin Wolf obj_order = ctz32(objsize);
7101bebea37SKevin Wolf }
7111bebea37SKevin Wolf
712aa045c2dSKevin Wolf ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
713aa045c2dSKevin Wolf password_secret, errp);
7141bebea37SKevin Wolf if (ret < 0) {
7151bebea37SKevin Wolf return ret;
7161bebea37SKevin Wolf }
7171bebea37SKevin Wolf
7181bebea37SKevin Wolf ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
7191bebea37SKevin Wolf if (ret < 0) {
7201bebea37SKevin Wolf error_setg_errno(errp, -ret, "error rbd create");
721aa045c2dSKevin Wolf goto out;
7221bebea37SKevin Wolf }
7231bebea37SKevin Wolf
72442e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
72554fde4ffSMarkus Armbruster if (opts->encrypt) {
72642e4ac9eSOr Ozeri rbd_image_t image;
72742e4ac9eSOr Ozeri
72842e4ac9eSOr Ozeri ret = rbd_open(io_ctx, opts->location->image, &image, NULL);
72942e4ac9eSOr Ozeri if (ret < 0) {
73042e4ac9eSOr Ozeri error_setg_errno(errp, -ret,
73142e4ac9eSOr Ozeri "error opening image '%s' for encryption format",
73242e4ac9eSOr Ozeri opts->location->image);
73342e4ac9eSOr Ozeri goto out;
73442e4ac9eSOr Ozeri }
73542e4ac9eSOr Ozeri
73642e4ac9eSOr Ozeri ret = qemu_rbd_encryption_format(image, opts->encrypt, errp);
73742e4ac9eSOr Ozeri rbd_close(image);
73842e4ac9eSOr Ozeri if (ret < 0) {
73942e4ac9eSOr Ozeri /* encryption format fail, try removing the image */
74042e4ac9eSOr Ozeri rbd_remove(io_ctx, opts->location->image);
74142e4ac9eSOr Ozeri goto out;
74242e4ac9eSOr Ozeri }
74342e4ac9eSOr Ozeri }
74442e4ac9eSOr Ozeri #endif
74542e4ac9eSOr Ozeri
7461bebea37SKevin Wolf ret = 0;
747aa045c2dSKevin Wolf out:
748aa045c2dSKevin Wolf rados_ioctx_destroy(io_ctx);
7491bebea37SKevin Wolf rados_shutdown(cluster);
7501bebea37SKevin Wolf return ret;
7511bebea37SKevin Wolf }
7521bebea37SKevin Wolf
qemu_rbd_co_create(BlockdevCreateOptions * options,Error ** errp)7531bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
7541bebea37SKevin Wolf {
7551bebea37SKevin Wolf return qemu_rbd_do_create(options, NULL, NULL, errp);
7561bebea37SKevin Wolf }
7571bebea37SKevin Wolf
qemu_rbd_extract_encryption_create_options(QemuOpts * opts,RbdEncryptionCreateOptions ** spec,Error ** errp)75842e4ac9eSOr Ozeri static int qemu_rbd_extract_encryption_create_options(
75942e4ac9eSOr Ozeri QemuOpts *opts,
76042e4ac9eSOr Ozeri RbdEncryptionCreateOptions **spec,
76142e4ac9eSOr Ozeri Error **errp)
76242e4ac9eSOr Ozeri {
76342e4ac9eSOr Ozeri QDict *opts_qdict;
76442e4ac9eSOr Ozeri QDict *encrypt_qdict;
76542e4ac9eSOr Ozeri Visitor *v;
76642e4ac9eSOr Ozeri int ret = 0;
76742e4ac9eSOr Ozeri
76842e4ac9eSOr Ozeri opts_qdict = qemu_opts_to_qdict(opts, NULL);
76942e4ac9eSOr Ozeri qdict_extract_subqdict(opts_qdict, &encrypt_qdict, "encrypt.");
77042e4ac9eSOr Ozeri qobject_unref(opts_qdict);
77142e4ac9eSOr Ozeri if (!qdict_size(encrypt_qdict)) {
77242e4ac9eSOr Ozeri *spec = NULL;
77342e4ac9eSOr Ozeri goto exit;
77442e4ac9eSOr Ozeri }
77542e4ac9eSOr Ozeri
77642e4ac9eSOr Ozeri /* Convert options into a QAPI object */
77742e4ac9eSOr Ozeri v = qobject_input_visitor_new_flat_confused(encrypt_qdict, errp);
77842e4ac9eSOr Ozeri if (!v) {
77942e4ac9eSOr Ozeri ret = -EINVAL;
78042e4ac9eSOr Ozeri goto exit;
78142e4ac9eSOr Ozeri }
78242e4ac9eSOr Ozeri
78342e4ac9eSOr Ozeri visit_type_RbdEncryptionCreateOptions(v, NULL, spec, errp);
78442e4ac9eSOr Ozeri visit_free(v);
78542e4ac9eSOr Ozeri if (!*spec) {
78642e4ac9eSOr Ozeri ret = -EINVAL;
78742e4ac9eSOr Ozeri goto exit;
78842e4ac9eSOr Ozeri }
78942e4ac9eSOr Ozeri
79042e4ac9eSOr Ozeri exit:
79142e4ac9eSOr Ozeri qobject_unref(encrypt_qdict);
79242e4ac9eSOr Ozeri return ret;
79342e4ac9eSOr Ozeri }
79442e4ac9eSOr Ozeri
qemu_rbd_co_create_opts(BlockDriver * drv,const char * filename,QemuOpts * opts,Error ** errp)795b92902dfSMaxim Levitsky static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv,
796b92902dfSMaxim Levitsky const char *filename,
797efc75e2aSStefan Hajnoczi QemuOpts *opts,
798efc75e2aSStefan Hajnoczi Error **errp)
799f27aaf4bSChristian Brunner {
8001bebea37SKevin Wolf BlockdevCreateOptions *create_options;
8011bebea37SKevin Wolf BlockdevCreateOptionsRbd *rbd_opts;
8021bebea37SKevin Wolf BlockdevOptionsRbd *loc;
80342e4ac9eSOr Ozeri RbdEncryptionCreateOptions *encrypt = NULL;
804d61563b2SMarkus Armbruster Error *local_err = NULL;
8051bebea37SKevin Wolf const char *keypairs, *password_secret;
806c7cacb3eSJeff Cody QDict *options = NULL;
807c7cacb3eSJeff Cody int ret = 0;
808f27aaf4bSChristian Brunner
8091bebea37SKevin Wolf create_options = g_new0(BlockdevCreateOptions, 1);
8101bebea37SKevin Wolf create_options->driver = BLOCKDEV_DRIVER_RBD;
8111bebea37SKevin Wolf rbd_opts = &create_options->u.rbd;
8121bebea37SKevin Wolf
8131bebea37SKevin Wolf rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
8141bebea37SKevin Wolf
8151bebea37SKevin Wolf password_secret = qemu_opt_get(opts, "password-secret");
81660390a21SDaniel P. Berrange
817f27aaf4bSChristian Brunner /* Read out options */
8181bebea37SKevin Wolf rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
819c2eb918eSHu Tao BDRV_SECTOR_SIZE);
8201bebea37SKevin Wolf rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
8211bebea37SKevin Wolf BLOCK_OPT_CLUSTER_SIZE, 0);
8221bebea37SKevin Wolf rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
823f27aaf4bSChristian Brunner
824c7cacb3eSJeff Cody options = qdict_new();
825c7cacb3eSJeff Cody qemu_rbd_parse_filename(filename, options, &local_err);
826c7cacb3eSJeff Cody if (local_err) {
827c7cacb3eSJeff Cody ret = -EINVAL;
828c7cacb3eSJeff Cody error_propagate(errp, local_err);
829c7cacb3eSJeff Cody goto exit;
830c7cacb3eSJeff Cody }
831c7cacb3eSJeff Cody
83242e4ac9eSOr Ozeri ret = qemu_rbd_extract_encryption_create_options(opts, &encrypt, errp);
83342e4ac9eSOr Ozeri if (ret < 0) {
83442e4ac9eSOr Ozeri goto exit;
83542e4ac9eSOr Ozeri }
83642e4ac9eSOr Ozeri rbd_opts->encrypt = encrypt;
83742e4ac9eSOr Ozeri
838129c7d1cSMarkus Armbruster /*
839129c7d1cSMarkus Armbruster * Caution: while qdict_get_try_str() is fine, getting non-string
840129c7d1cSMarkus Armbruster * types would require more care. When @options come from -blockdev
841129c7d1cSMarkus Armbruster * or blockdev_add, its members are typed according to the QAPI
842129c7d1cSMarkus Armbruster * schema, but when they come from -drive, they're all QString.
843129c7d1cSMarkus Armbruster */
8441bebea37SKevin Wolf loc = rbd_opts->location;
8451bebea37SKevin Wolf loc->pool = g_strdup(qdict_get_try_str(options, "pool"));
8461bebea37SKevin Wolf loc->conf = g_strdup(qdict_get_try_str(options, "conf"));
8471bebea37SKevin Wolf loc->user = g_strdup(qdict_get_try_str(options, "user"));
84819ae9ae0SFlorian Florensa loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace"));
8491bebea37SKevin Wolf loc->image = g_strdup(qdict_get_try_str(options, "image"));
85007846397SMarkus Armbruster keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
851c7cacb3eSJeff Cody
8521bebea37SKevin Wolf ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
85387cd3d20SVikhyat Umrao if (ret < 0) {
854c7cacb3eSJeff Cody goto exit;
855f27aaf4bSChristian Brunner }
856f27aaf4bSChristian Brunner
857c7cacb3eSJeff Cody exit:
858cb3e7f08SMarc-André Lureau qobject_unref(options);
8591bebea37SKevin Wolf qapi_free_BlockdevCreateOptions(create_options);
860f27aaf4bSChristian Brunner return ret;
861f27aaf4bSChristian Brunner }
862f27aaf4bSChristian Brunner
qemu_rbd_mon_host(BlockdevOptionsRbd * opts,Error ** errp)8634bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
8640a55679bSJeff Cody {
8654bfb2741SKevin Wolf const char **vals;
8662836284dSMarkus Armbruster const char *host, *port;
8672836284dSMarkus Armbruster char *rados_str;
8684bfb2741SKevin Wolf InetSocketAddressBaseList *p;
8694bfb2741SKevin Wolf int i, cnt;
8700a55679bSJeff Cody
8714bfb2741SKevin Wolf if (!opts->has_server) {
8724bfb2741SKevin Wolf return NULL;
8730a55679bSJeff Cody }
8744bfb2741SKevin Wolf
8754bfb2741SKevin Wolf for (cnt = 0, p = opts->server; p; p = p->next) {
8764bfb2741SKevin Wolf cnt++;
8770a55679bSJeff Cody }
8780a55679bSJeff Cody
8794bfb2741SKevin Wolf vals = g_new(const char *, cnt + 1);
8804bfb2741SKevin Wolf
8814bfb2741SKevin Wolf for (i = 0, p = opts->server; p; p = p->next, i++) {
8824bfb2741SKevin Wolf host = p->value->host;
8834bfb2741SKevin Wolf port = p->value->port;
8844bfb2741SKevin Wolf
8850a55679bSJeff Cody if (strchr(host, ':')) {
8864bfb2741SKevin Wolf vals[i] = g_strdup_printf("[%s]:%s", host, port);
8870a55679bSJeff Cody } else {
8884bfb2741SKevin Wolf vals[i] = g_strdup_printf("%s:%s", host, port);
8890a55679bSJeff Cody }
8900a55679bSJeff Cody }
8912836284dSMarkus Armbruster vals[i] = NULL;
8920a55679bSJeff Cody
8932836284dSMarkus Armbruster rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
8942836284dSMarkus Armbruster g_strfreev((char **)vals);
8950a55679bSJeff Cody return rados_str;
8960a55679bSJeff Cody }
8970a55679bSJeff Cody
qemu_rbd_connect(rados_t * cluster,rados_ioctx_t * io_ctx,BlockdevOptionsRbd * opts,bool cache,const char * keypairs,const char * secretid,Error ** errp)8983d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
8994bfb2741SKevin Wolf BlockdevOptionsRbd *opts, bool cache,
9004ff45049SKevin Wolf const char *keypairs, const char *secretid,
9014ff45049SKevin Wolf Error **errp)
902f27aaf4bSChristian Brunner {
9030a55679bSJeff Cody char *mon_host = NULL;
9043d9136f9SKevin Wolf Error *local_err = NULL;
905f27aaf4bSChristian Brunner int r;
906f27aaf4bSChristian Brunner
907d083f954SMarkus Armbruster if (secretid) {
908d083f954SMarkus Armbruster if (opts->key_secret) {
909d083f954SMarkus Armbruster error_setg(errp,
910d083f954SMarkus Armbruster "Legacy 'password-secret' clashes with 'key-secret'");
911d083f954SMarkus Armbruster return -EINVAL;
912d083f954SMarkus Armbruster }
913d083f954SMarkus Armbruster opts->key_secret = g_strdup(secretid);
914d083f954SMarkus Armbruster }
915d083f954SMarkus Armbruster
9164bfb2741SKevin Wolf mon_host = qemu_rbd_mon_host(opts, &local_err);
91784d18f06SMarkus Armbruster if (local_err) {
918d61563b2SMarkus Armbruster error_propagate(errp, local_err);
9192836284dSMarkus Armbruster r = -EINVAL;
920c1c1f6cfSStefano Garzarella goto out;
921a9ccedc3SKevin Wolf }
922a9ccedc3SKevin Wolf
9234bfb2741SKevin Wolf r = rados_create(cluster, opts->user);
924ad32e9c0SJosh Durgin if (r < 0) {
92587cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error initializing");
926c1c1f6cfSStefano Garzarella goto out;
927f27aaf4bSChristian Brunner }
928f27aaf4bSChristian Brunner
929c7cacb3eSJeff Cody /* try default location when conf=NULL, but ignore failure */
9304bfb2741SKevin Wolf r = rados_conf_read_file(*cluster, opts->conf);
93154fde4ffSMarkus Armbruster if (opts->conf && r < 0) {
9324bfb2741SKevin Wolf error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
933e34d8f29SJosh Durgin goto failed_shutdown;
934e34d8f29SJosh Durgin }
93599a3c89dSJosh Durgin
9363d9136f9SKevin Wolf r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
93799a3c89dSJosh Durgin if (r < 0) {
93899a3c89dSJosh Durgin goto failed_shutdown;
93999a3c89dSJosh Durgin }
94099a3c89dSJosh Durgin
9410a55679bSJeff Cody if (mon_host) {
9423d9136f9SKevin Wolf r = rados_conf_set(*cluster, "mon_host", mon_host);
9430a55679bSJeff Cody if (r < 0) {
9440a55679bSJeff Cody goto failed_shutdown;
9450a55679bSJeff Cody }
9460a55679bSJeff Cody }
9470a55679bSJeff Cody
948d083f954SMarkus Armbruster r = qemu_rbd_set_auth(*cluster, opts, errp);
949d083f954SMarkus Armbruster if (r < 0) {
95060390a21SDaniel P. Berrange goto failed_shutdown;
95160390a21SDaniel P. Berrange }
95260390a21SDaniel P. Berrange
953b11f38fcSJosh Durgin /*
954b11f38fcSJosh Durgin * Fallback to more conservative semantics if setting cache
955b11f38fcSJosh Durgin * options fails. Ignore errors from setting rbd_cache because the
956b11f38fcSJosh Durgin * only possible error is that the option does not exist, and
957b11f38fcSJosh Durgin * librbd defaults to no caching. If write through caching cannot
958b11f38fcSJosh Durgin * be set up, fall back to no caching.
959b11f38fcSJosh Durgin */
9603d9136f9SKevin Wolf if (cache) {
9613d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "true");
962b11f38fcSJosh Durgin } else {
9633d9136f9SKevin Wolf rados_conf_set(*cluster, "rbd_cache", "false");
964b11f38fcSJosh Durgin }
965b11f38fcSJosh Durgin
9663d9136f9SKevin Wolf r = rados_connect(*cluster);
967ad32e9c0SJosh Durgin if (r < 0) {
96887cd3d20SVikhyat Umrao error_setg_errno(errp, -r, "error connecting");
969eb93d5d9SSage Weil goto failed_shutdown;
970ad32e9c0SJosh Durgin }
971ad32e9c0SJosh Durgin
9724bfb2741SKevin Wolf r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
973ad32e9c0SJosh Durgin if (r < 0) {
9744bfb2741SKevin Wolf error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
975eb93d5d9SSage Weil goto failed_shutdown;
976ad32e9c0SJosh Durgin }
97766dc5f96SStefano Garzarella
97866dc5f96SStefano Garzarella #ifdef HAVE_RBD_NAMESPACE_EXISTS
97954fde4ffSMarkus Armbruster if (opts->q_namespace && strlen(opts->q_namespace) > 0) {
98066dc5f96SStefano Garzarella bool exists;
98166dc5f96SStefano Garzarella
98266dc5f96SStefano Garzarella r = rbd_namespace_exists(*io_ctx, opts->q_namespace, &exists);
98366dc5f96SStefano Garzarella if (r < 0) {
98466dc5f96SStefano Garzarella error_setg_errno(errp, -r, "error checking namespace");
98566dc5f96SStefano Garzarella goto failed_ioctx_destroy;
98666dc5f96SStefano Garzarella }
98766dc5f96SStefano Garzarella
98866dc5f96SStefano Garzarella if (!exists) {
98966dc5f96SStefano Garzarella error_setg(errp, "namespace '%s' does not exist",
99066dc5f96SStefano Garzarella opts->q_namespace);
99166dc5f96SStefano Garzarella r = -ENOENT;
99266dc5f96SStefano Garzarella goto failed_ioctx_destroy;
99366dc5f96SStefano Garzarella }
99466dc5f96SStefano Garzarella }
99566dc5f96SStefano Garzarella #endif
99666dc5f96SStefano Garzarella
99719ae9ae0SFlorian Florensa /*
99819ae9ae0SFlorian Florensa * Set the namespace after opening the io context on the pool,
99919ae9ae0SFlorian Florensa * if nspace == NULL or if nspace == "", it is just as we did nothing
100019ae9ae0SFlorian Florensa */
100119ae9ae0SFlorian Florensa rados_ioctx_set_namespace(*io_ctx, opts->q_namespace);
1002ad32e9c0SJosh Durgin
1003c1c1f6cfSStefano Garzarella r = 0;
1004c1c1f6cfSStefano Garzarella goto out;
10053d9136f9SKevin Wolf
100666dc5f96SStefano Garzarella #ifdef HAVE_RBD_NAMESPACE_EXISTS
100766dc5f96SStefano Garzarella failed_ioctx_destroy:
100866dc5f96SStefano Garzarella rados_ioctx_destroy(*io_ctx);
100966dc5f96SStefano Garzarella #endif
10103d9136f9SKevin Wolf failed_shutdown:
10113d9136f9SKevin Wolf rados_shutdown(*cluster);
1012c1c1f6cfSStefano Garzarella out:
10133d9136f9SKevin Wolf g_free(mon_host);
10143d9136f9SKevin Wolf return r;
10153d9136f9SKevin Wolf }
10163d9136f9SKevin Wolf
qemu_rbd_convert_options(QDict * options,BlockdevOptionsRbd ** opts,Error ** errp)1017f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
1018f24b03b5SJeff Cody Error **errp)
1019f24b03b5SJeff Cody {
1020f24b03b5SJeff Cody Visitor *v;
1021f24b03b5SJeff Cody
1022f24b03b5SJeff Cody /* Convert the remaining options into a QAPI object */
1023f24b03b5SJeff Cody v = qobject_input_visitor_new_flat_confused(options, errp);
1024f24b03b5SJeff Cody if (!v) {
1025f24b03b5SJeff Cody return -EINVAL;
1026f24b03b5SJeff Cody }
1027f24b03b5SJeff Cody
1028b11a093cSMarkus Armbruster visit_type_BlockdevOptionsRbd(v, NULL, opts, errp);
1029f24b03b5SJeff Cody visit_free(v);
1030b11a093cSMarkus Armbruster if (!opts) {
1031f24b03b5SJeff Cody return -EINVAL;
1032f24b03b5SJeff Cody }
1033f24b03b5SJeff Cody
1034f24b03b5SJeff Cody return 0;
1035f24b03b5SJeff Cody }
1036f24b03b5SJeff Cody
qemu_rbd_attempt_legacy_options(QDict * options,BlockdevOptionsRbd ** opts,char ** keypairs)1037084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options,
1038084d1d13SJeff Cody BlockdevOptionsRbd **opts,
1039084d1d13SJeff Cody char **keypairs)
1040084d1d13SJeff Cody {
1041084d1d13SJeff Cody char *filename;
1042084d1d13SJeff Cody int r;
1043084d1d13SJeff Cody
1044084d1d13SJeff Cody filename = g_strdup(qdict_get_try_str(options, "filename"));
1045084d1d13SJeff Cody if (!filename) {
1046084d1d13SJeff Cody return -EINVAL;
1047084d1d13SJeff Cody }
1048084d1d13SJeff Cody qdict_del(options, "filename");
1049084d1d13SJeff Cody
1050084d1d13SJeff Cody qemu_rbd_parse_filename(filename, options, NULL);
1051084d1d13SJeff Cody
1052084d1d13SJeff Cody /* keypairs freed by caller */
1053084d1d13SJeff Cody *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
1054084d1d13SJeff Cody if (*keypairs) {
1055084d1d13SJeff Cody qdict_del(options, "=keyvalue-pairs");
1056084d1d13SJeff Cody }
1057084d1d13SJeff Cody
1058084d1d13SJeff Cody r = qemu_rbd_convert_options(options, opts, NULL);
1059084d1d13SJeff Cody
1060084d1d13SJeff Cody g_free(filename);
1061084d1d13SJeff Cody return r;
1062084d1d13SJeff Cody }
1063084d1d13SJeff Cody
qemu_rbd_open(BlockDriverState * bs,QDict * options,int flags,Error ** errp)10643d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
10653d9136f9SKevin Wolf Error **errp)
10663d9136f9SKevin Wolf {
10673d9136f9SKevin Wolf BDRVRBDState *s = bs->opaque;
10684bfb2741SKevin Wolf BlockdevOptionsRbd *opts = NULL;
1069bfb15b4bSJeff Cody const QDictEntry *e;
10703d9136f9SKevin Wolf Error *local_err = NULL;
10714ff45049SKevin Wolf char *keypairs, *secretid;
1072832a93dcSPeter Lieven rbd_image_info_t info;
10733d9136f9SKevin Wolf int r;
10743d9136f9SKevin Wolf
10754ff45049SKevin Wolf keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
10764ff45049SKevin Wolf if (keypairs) {
10774ff45049SKevin Wolf qdict_del(options, "=keyvalue-pairs");
10784ff45049SKevin Wolf }
10794ff45049SKevin Wolf
10804ff45049SKevin Wolf secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
10814ff45049SKevin Wolf if (secretid) {
10824ff45049SKevin Wolf qdict_del(options, "password-secret");
10834ff45049SKevin Wolf }
10844ff45049SKevin Wolf
1085f24b03b5SJeff Cody r = qemu_rbd_convert_options(options, &opts, &local_err);
10864bfb2741SKevin Wolf if (local_err) {
1087084d1d13SJeff Cody /* If keypairs are present, that means some options are present in
1088084d1d13SJeff Cody * the modern option format. Don't attempt to parse legacy option
1089084d1d13SJeff Cody * formats, as we won't support mixed usage. */
1090084d1d13SJeff Cody if (keypairs) {
10914bfb2741SKevin Wolf error_propagate(errp, local_err);
10924bfb2741SKevin Wolf goto out;
10934bfb2741SKevin Wolf }
10944bfb2741SKevin Wolf
1095084d1d13SJeff Cody /* If the initial attempt to convert and process the options failed,
1096084d1d13SJeff Cody * we may be attempting to open an image file that has the rbd options
1097084d1d13SJeff Cody * specified in the older format consisting of all key/value pairs
1098084d1d13SJeff Cody * encoded in the filename. Go ahead and attempt to parse the
1099084d1d13SJeff Cody * filename, and see if we can pull out the required options. */
1100084d1d13SJeff Cody r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
1101084d1d13SJeff Cody if (r < 0) {
1102084d1d13SJeff Cody /* Propagate the original error, not the legacy parsing fallback
1103084d1d13SJeff Cody * error, as the latter was just a best-effort attempt. */
1104084d1d13SJeff Cody error_propagate(errp, local_err);
1105084d1d13SJeff Cody goto out;
1106084d1d13SJeff Cody }
1107084d1d13SJeff Cody /* Take care whenever deciding to actually deprecate; once this ability
1108084d1d13SJeff Cody * is removed, we will not be able to open any images with legacy-styled
1109084d1d13SJeff Cody * backing image strings. */
11105197f445SMarkus Armbruster warn_report("RBD options encoded in the filename as keyvalue pairs "
1111084d1d13SJeff Cody "is deprecated");
1112084d1d13SJeff Cody }
1113084d1d13SJeff Cody
1114bfb15b4bSJeff Cody /* Remove the processed options from the QDict (the visitor processes
1115bfb15b4bSJeff Cody * _all_ options in the QDict) */
1116bfb15b4bSJeff Cody while ((e = qdict_first(options))) {
1117bfb15b4bSJeff Cody qdict_del(options, e->key);
1118bfb15b4bSJeff Cody }
1119bfb15b4bSJeff Cody
1120d41a5588SKevin Wolf r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
1121d41a5588SKevin Wolf !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
11223d9136f9SKevin Wolf if (r < 0) {
11234ff45049SKevin Wolf goto out;
11243d9136f9SKevin Wolf }
11253d9136f9SKevin Wolf
1126d41a5588SKevin Wolf s->snap = g_strdup(opts->snapshot);
1127d41a5588SKevin Wolf s->image_name = g_strdup(opts->image);
1128d41a5588SKevin Wolf
1129e2b8247aSJeff Cody /* rbd_open is always r/w */
113080b61a27SJeff Cody r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
1131ad32e9c0SJosh Durgin if (r < 0) {
113280b61a27SJeff Cody error_setg_errno(errp, -r, "error reading header from %s",
113380b61a27SJeff Cody s->image_name);
1134eb93d5d9SSage Weil goto failed_open;
1135ad32e9c0SJosh Durgin }
1136ad32e9c0SJosh Durgin
113754fde4ffSMarkus Armbruster if (opts->encrypt) {
113842e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
11390f385a24SOr Ozeri if (opts->encrypt->parent) {
11400f385a24SOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION_LOAD2
11410f385a24SOr Ozeri r = qemu_rbd_encryption_load2(s->image, opts->encrypt, errp);
11420f385a24SOr Ozeri #else
11430f385a24SOr Ozeri r = -ENOTSUP;
11440f385a24SOr Ozeri error_setg(errp, "RBD library does not support layered encryption");
11450f385a24SOr Ozeri #endif
11460f385a24SOr Ozeri } else {
114742e4ac9eSOr Ozeri r = qemu_rbd_encryption_load(s->image, opts->encrypt, errp);
11480f385a24SOr Ozeri }
114942e4ac9eSOr Ozeri if (r < 0) {
115042e4ac9eSOr Ozeri goto failed_post_open;
115142e4ac9eSOr Ozeri }
115242e4ac9eSOr Ozeri #else
115342e4ac9eSOr Ozeri r = -ENOTSUP;
115442e4ac9eSOr Ozeri error_setg(errp, "RBD library does not support image encryption");
115542e4ac9eSOr Ozeri goto failed_post_open;
115642e4ac9eSOr Ozeri #endif
115742e4ac9eSOr Ozeri }
115842e4ac9eSOr Ozeri
1159832a93dcSPeter Lieven r = rbd_stat(s->image, &info, sizeof(info));
1160d24f8023SStefano Garzarella if (r < 0) {
1161832a93dcSPeter Lieven error_setg_errno(errp, -r, "error getting image info from %s",
1162d24f8023SStefano Garzarella s->image_name);
116342e4ac9eSOr Ozeri goto failed_post_open;
1164d24f8023SStefano Garzarella }
1165832a93dcSPeter Lieven s->image_size = info.size;
1166832a93dcSPeter Lieven s->object_size = info.obj_size;
1167d24f8023SStefano Garzarella
1168e2b8247aSJeff Cody /* If we are using an rbd snapshot, we must be r/o, otherwise
1169e2b8247aSJeff Cody * leave as-is */
1170e2b8247aSJeff Cody if (s->snap != NULL) {
1171018f9deaSKevin Wolf bdrv_graph_rdlock_main_loop();
1172eaa2410fSKevin Wolf r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
1173018f9deaSKevin Wolf bdrv_graph_rdunlock_main_loop();
1174e2b8247aSJeff Cody if (r < 0) {
117542e4ac9eSOr Ozeri goto failed_post_open;
1176e2b8247aSJeff Cody }
1177e2b8247aSJeff Cody }
1178f27aaf4bSChristian Brunner
1179c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1180c56ac27dSPeter Lieven bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
1181c56ac27dSPeter Lieven #endif
1182c56ac27dSPeter Lieven
11832f98910dSEric Blake /* When extending regular files, we get zeros from the OS */
11842f98910dSEric Blake bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
11852f98910dSEric Blake
11864ff45049SKevin Wolf r = 0;
11874ff45049SKevin Wolf goto out;
1188f27aaf4bSChristian Brunner
118942e4ac9eSOr Ozeri failed_post_open:
119042e4ac9eSOr Ozeri rbd_close(s->image);
1191eb93d5d9SSage Weil failed_open:
1192ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx);
1193eb93d5d9SSage Weil g_free(s->snap);
119480b61a27SJeff Cody g_free(s->image_name);
11953d9136f9SKevin Wolf rados_shutdown(s->cluster);
11964ff45049SKevin Wolf out:
11974bfb2741SKevin Wolf qapi_free_BlockdevOptionsRbd(opts);
11984ff45049SKevin Wolf g_free(keypairs);
11994ff45049SKevin Wolf g_free(secretid);
1200f27aaf4bSChristian Brunner return r;
1201f27aaf4bSChristian Brunner }
1202f27aaf4bSChristian Brunner
120356e7cf8dSJeff Cody
120456e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
120556e7cf8dSJeff Cody * we just need to check if we are using a snapshot or not, in
120656e7cf8dSJeff Cody * order to determine if we will allow it to be R/W */
qemu_rbd_reopen_prepare(BDRVReopenState * state,BlockReopenQueue * queue,Error ** errp)120756e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
120856e7cf8dSJeff Cody BlockReopenQueue *queue, Error **errp)
120956e7cf8dSJeff Cody {
121056e7cf8dSJeff Cody BDRVRBDState *s = state->bs->opaque;
121156e7cf8dSJeff Cody int ret = 0;
121256e7cf8dSJeff Cody
12134026f1c4SKevin Wolf GRAPH_RDLOCK_GUARD_MAINLOOP();
12144026f1c4SKevin Wolf
121556e7cf8dSJeff Cody if (s->snap && state->flags & BDRV_O_RDWR) {
121656e7cf8dSJeff Cody error_setg(errp,
121756e7cf8dSJeff Cody "Cannot change node '%s' to r/w when using RBD snapshot",
121856e7cf8dSJeff Cody bdrv_get_device_or_node_name(state->bs));
121956e7cf8dSJeff Cody ret = -EINVAL;
122056e7cf8dSJeff Cody }
122156e7cf8dSJeff Cody
122256e7cf8dSJeff Cody return ret;
122356e7cf8dSJeff Cody }
122456e7cf8dSJeff Cody
qemu_rbd_close(BlockDriverState * bs)1225ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
1226f27aaf4bSChristian Brunner {
1227f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque;
1228f27aaf4bSChristian Brunner
1229ad32e9c0SJosh Durgin rbd_close(s->image);
1230ad32e9c0SJosh Durgin rados_ioctx_destroy(s->io_ctx);
12317267c094SAnthony Liguori g_free(s->snap);
123280b61a27SJeff Cody g_free(s->image_name);
1233ad32e9c0SJosh Durgin rados_shutdown(s->cluster);
1234f27aaf4bSChristian Brunner }
1235f27aaf4bSChristian Brunner
1236d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */
qemu_rbd_resize(BlockDriverState * bs,uint64_t size)1237d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size)
1238d24f8023SStefano Garzarella {
1239d24f8023SStefano Garzarella BDRVRBDState *s = bs->opaque;
1240d24f8023SStefano Garzarella int r;
1241d24f8023SStefano Garzarella
1242d24f8023SStefano Garzarella r = rbd_resize(s->image, size);
1243d24f8023SStefano Garzarella if (r < 0) {
1244d24f8023SStefano Garzarella return r;
1245d24f8023SStefano Garzarella }
1246d24f8023SStefano Garzarella
1247d24f8023SStefano Garzarella s->image_size = size;
1248d24f8023SStefano Garzarella
1249d24f8023SStefano Garzarella return 0;
1250d24f8023SStefano Garzarella }
1251d24f8023SStefano Garzarella
qemu_rbd_finish_bh(void * opaque)1252c3e5fac5SPeter Lieven static void qemu_rbd_finish_bh(void *opaque)
1253f27aaf4bSChristian Brunner {
1254c3e5fac5SPeter Lieven RBDTask *task = opaque;
125564cc845bSPeter Lieven task->complete = true;
1256c3e5fac5SPeter Lieven aio_co_wake(task->co);
1257ad32e9c0SJosh Durgin }
1258ad32e9c0SJosh Durgin
1259ad32e9c0SJosh Durgin /*
1260c3e5fac5SPeter Lieven * This is the completion callback function for all rbd aio calls
1261c3e5fac5SPeter Lieven * started from qemu_rbd_start_co().
1262ad32e9c0SJosh Durgin *
1263ad32e9c0SJosh Durgin * Note: this function is being called from a non qemu thread so
1264ad32e9c0SJosh Durgin * we need to be careful about what we do here. Generally we only
1265e04fb07fSStefan Hajnoczi * schedule a BH, and do the rest of the io completion handling
1266c3e5fac5SPeter Lieven * from qemu_rbd_finish_bh() which runs in a qemu context.
1267ad32e9c0SJosh Durgin */
qemu_rbd_completion_cb(rbd_completion_t c,RBDTask * task)1268c3e5fac5SPeter Lieven static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
1269ad32e9c0SJosh Durgin {
1270c3e5fac5SPeter Lieven task->ret = rbd_aio_get_return_value(c);
1271ad32e9c0SJosh Durgin rbd_aio_release(c);
1272c3e5fac5SPeter Lieven aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
1273c3e5fac5SPeter Lieven qemu_rbd_finish_bh, task);
1274473c7f02SStefan Priebe }
1275f27aaf4bSChristian Brunner
qemu_rbd_start_co(BlockDriverState * bs,uint64_t offset,uint64_t bytes,QEMUIOVector * qiov,int flags,RBDAIOCmd cmd)1276c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
1277c3e5fac5SPeter Lieven uint64_t offset,
1278c3e5fac5SPeter Lieven uint64_t bytes,
1279f27aaf4bSChristian Brunner QEMUIOVector *qiov,
1280c3e5fac5SPeter Lieven int flags,
1281787f3133SJosh Durgin RBDAIOCmd cmd)
1282f27aaf4bSChristian Brunner {
1283c3e5fac5SPeter Lieven BDRVRBDState *s = bs->opaque;
1284c3e5fac5SPeter Lieven RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
1285ad32e9c0SJosh Durgin rbd_completion_t c;
128651a13528SJosh Durgin int r;
1287f27aaf4bSChristian Brunner
1288c3e5fac5SPeter Lieven assert(!qiov || qiov->size == bytes);
1289f27aaf4bSChristian Brunner
1290cc5387a5SStefano Garzarella if (cmd == RBD_AIO_WRITE || cmd == RBD_AIO_WRITE_ZEROES) {
1291cc5387a5SStefano Garzarella /*
1292cc5387a5SStefano Garzarella * RBD APIs don't allow us to write more than actual size, so in order
1293cc5387a5SStefano Garzarella * to support growing images, we resize the image before write
1294cc5387a5SStefano Garzarella * operations that exceed the current size.
1295cc5387a5SStefano Garzarella */
1296cc5387a5SStefano Garzarella if (offset + bytes > s->image_size) {
1297fb2575f9SMarkus Armbruster r = qemu_rbd_resize(bs, offset + bytes);
1298cc5387a5SStefano Garzarella if (r < 0) {
1299cc5387a5SStefano Garzarella return r;
1300cc5387a5SStefano Garzarella }
1301cc5387a5SStefano Garzarella }
1302cc5387a5SStefano Garzarella }
1303cc5387a5SStefano Garzarella
1304c3e5fac5SPeter Lieven r = rbd_aio_create_completion(&task,
1305c3e5fac5SPeter Lieven (rbd_callback_t) qemu_rbd_completion_cb, &c);
130651a13528SJosh Durgin if (r < 0) {
1307c3e5fac5SPeter Lieven return r;
130851a13528SJosh Durgin }
1309f27aaf4bSChristian Brunner
1310787f3133SJosh Durgin switch (cmd) {
1311787f3133SJosh Durgin case RBD_AIO_READ:
1312c3e5fac5SPeter Lieven r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, offset, c);
1313c3e5fac5SPeter Lieven break;
1314c3e5fac5SPeter Lieven case RBD_AIO_WRITE:
1315c3e5fac5SPeter Lieven r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, offset, c);
1316787f3133SJosh Durgin break;
1317787f3133SJosh Durgin case RBD_AIO_DISCARD:
1318c3e5fac5SPeter Lieven r = rbd_aio_discard(s->image, offset, bytes, c);
1319787f3133SJosh Durgin break;
1320dc7588c1SJosh Durgin case RBD_AIO_FLUSH:
132148672ac0SPeter Lieven r = rbd_aio_flush(s->image, c);
1322dc7588c1SJosh Durgin break;
1323c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1324c56ac27dSPeter Lieven case RBD_AIO_WRITE_ZEROES: {
1325c56ac27dSPeter Lieven int zero_flags = 0;
1326c56ac27dSPeter Lieven #ifdef RBD_WRITE_ZEROES_FLAG_THICK_PROVISION
1327c56ac27dSPeter Lieven if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1328c56ac27dSPeter Lieven zero_flags = RBD_WRITE_ZEROES_FLAG_THICK_PROVISION;
1329c56ac27dSPeter Lieven }
1330c56ac27dSPeter Lieven #endif
1331c56ac27dSPeter Lieven r = rbd_aio_write_zeroes(s->image, offset, bytes, c, zero_flags, 0);
1332c56ac27dSPeter Lieven break;
1333c56ac27dSPeter Lieven }
1334c56ac27dSPeter Lieven #endif
1335787f3133SJosh Durgin default:
1336787f3133SJosh Durgin r = -EINVAL;
133751a13528SJosh Durgin }
133851a13528SJosh Durgin
133951a13528SJosh Durgin if (r < 0) {
1340c3e5fac5SPeter Lieven error_report("rbd request failed early: cmd %d offset %" PRIu64
1341c3e5fac5SPeter Lieven " bytes %" PRIu64 " flags %d r %d (%s)", cmd, offset,
1342c3e5fac5SPeter Lieven bytes, flags, r, strerror(-r));
1343405a2764SKevin Wolf rbd_aio_release(c);
1344c3e5fac5SPeter Lieven return r;
1345f27aaf4bSChristian Brunner }
1346f27aaf4bSChristian Brunner
1347c3e5fac5SPeter Lieven while (!task.complete) {
1348c3e5fac5SPeter Lieven qemu_coroutine_yield();
1349f27aaf4bSChristian Brunner }
1350f27aaf4bSChristian Brunner
1351c3e5fac5SPeter Lieven if (task.ret < 0) {
1352c3e5fac5SPeter Lieven error_report("rbd request failed: cmd %d offset %" PRIu64 " bytes %"
1353c3e5fac5SPeter Lieven PRIu64 " flags %d task.ret %" PRIi64 " (%s)", cmd, offset,
1354c3e5fac5SPeter Lieven bytes, flags, task.ret, strerror(-task.ret));
1355c3e5fac5SPeter Lieven return task.ret;
1356f27aaf4bSChristian Brunner }
1357f27aaf4bSChristian Brunner
1358c3e5fac5SPeter Lieven /* zero pad short reads */
1359c3e5fac5SPeter Lieven if (cmd == RBD_AIO_READ && task.ret < qiov->size) {
1360c3e5fac5SPeter Lieven qemu_iovec_memset(qiov, task.ret, 0, qiov->size - task.ret);
1361c3e5fac5SPeter Lieven }
1362c3e5fac5SPeter Lieven
1363c3e5fac5SPeter Lieven return 0;
1364c3e5fac5SPeter Lieven }
1365c3e5fac5SPeter Lieven
1366c3e5fac5SPeter Lieven static int
qemu_rbd_co_preadv(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)1367f7ef38ddSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_preadv(BlockDriverState *bs, int64_t offset,
1368f7ef38ddSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov,
1369f7ef38ddSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags)
1370dc7588c1SJosh Durgin {
1371c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_READ);
1372c3e5fac5SPeter Lieven }
1373c3e5fac5SPeter Lieven
1374c3e5fac5SPeter Lieven static int
qemu_rbd_co_pwritev(BlockDriverState * bs,int64_t offset,int64_t bytes,QEMUIOVector * qiov,BdrvRequestFlags flags)1375e75abedaSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_pwritev(BlockDriverState *bs, int64_t offset,
1376e75abedaSVladimir Sementsov-Ogievskiy int64_t bytes, QEMUIOVector *qiov,
1377e75abedaSVladimir Sementsov-Ogievskiy BdrvRequestFlags flags)
1378c3e5fac5SPeter Lieven {
1379c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_WRITE);
1380c3e5fac5SPeter Lieven }
1381c3e5fac5SPeter Lieven
qemu_rbd_co_flush(BlockDriverState * bs)1382c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_flush(BlockDriverState *bs)
1383c3e5fac5SPeter Lieven {
1384c3e5fac5SPeter Lieven return qemu_rbd_start_co(bs, 0, 0, NULL, 0, RBD_AIO_FLUSH);
1385c3e5fac5SPeter Lieven }
1386c3e5fac5SPeter Lieven
qemu_rbd_co_pdiscard(BlockDriverState * bs,int64_t offset,int64_t bytes)1387c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_pdiscard(BlockDriverState *bs,
13880c802287SVladimir Sementsov-Ogievskiy int64_t offset, int64_t bytes)
1389c3e5fac5SPeter Lieven {
13900c802287SVladimir Sementsov-Ogievskiy return qemu_rbd_start_co(bs, offset, bytes, NULL, 0, RBD_AIO_DISCARD);
1391dc7588c1SJosh Durgin }
1392dc7588c1SJosh Durgin
1393c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1394c56ac27dSPeter Lieven static int
qemu_rbd_co_pwrite_zeroes(BlockDriverState * bs,int64_t offset,int64_t bytes,BdrvRequestFlags flags)1395c56ac27dSPeter Lieven coroutine_fn qemu_rbd_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1396f34b2bcfSVladimir Sementsov-Ogievskiy int64_t bytes, BdrvRequestFlags flags)
1397c56ac27dSPeter Lieven {
1398f34b2bcfSVladimir Sementsov-Ogievskiy return qemu_rbd_start_co(bs, offset, bytes, NULL, flags,
1399c56ac27dSPeter Lieven RBD_AIO_WRITE_ZEROES);
1400c56ac27dSPeter Lieven }
1401c56ac27dSPeter Lieven #endif
1402c56ac27dSPeter Lieven
14033d47eb0aSEmanuele Giuseppe Esposito static int coroutine_fn
qemu_rbd_co_get_info(BlockDriverState * bs,BlockDriverInfo * bdi)14043d47eb0aSEmanuele Giuseppe Esposito qemu_rbd_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1405f27aaf4bSChristian Brunner {
1406f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque;
1407832a93dcSPeter Lieven bdi->cluster_size = s->object_size;
1408f27aaf4bSChristian Brunner return 0;
1409f27aaf4bSChristian Brunner }
1410f27aaf4bSChristian Brunner
qemu_rbd_get_specific_info(BlockDriverState * bs,Error ** errp)141142e4ac9eSOr Ozeri static ImageInfoSpecific *qemu_rbd_get_specific_info(BlockDriverState *bs,
141242e4ac9eSOr Ozeri Error **errp)
141342e4ac9eSOr Ozeri {
141442e4ac9eSOr Ozeri BDRVRBDState *s = bs->opaque;
141542e4ac9eSOr Ozeri ImageInfoSpecific *spec_info;
141642e4ac9eSOr Ozeri char buf[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {0};
141742e4ac9eSOr Ozeri int r;
141842e4ac9eSOr Ozeri
141942e4ac9eSOr Ozeri if (s->image_size >= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) {
142042e4ac9eSOr Ozeri r = rbd_read(s->image, 0,
142142e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN, buf);
142242e4ac9eSOr Ozeri if (r < 0) {
142342e4ac9eSOr Ozeri error_setg_errno(errp, -r, "cannot read image start for probe");
142442e4ac9eSOr Ozeri return NULL;
142542e4ac9eSOr Ozeri }
142642e4ac9eSOr Ozeri }
142742e4ac9eSOr Ozeri
142842e4ac9eSOr Ozeri spec_info = g_new(ImageInfoSpecific, 1);
142942e4ac9eSOr Ozeri *spec_info = (ImageInfoSpecific){
143042e4ac9eSOr Ozeri .type = IMAGE_INFO_SPECIFIC_KIND_RBD,
143142e4ac9eSOr Ozeri .u.rbd.data = g_new0(ImageInfoSpecificRbd, 1),
143242e4ac9eSOr Ozeri };
143342e4ac9eSOr Ozeri
143442e4ac9eSOr Ozeri if (memcmp(buf, rbd_luks_header_verification,
143542e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
143642e4ac9eSOr Ozeri spec_info->u.rbd.data->encryption_format =
143742e4ac9eSOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS;
143842e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = true;
143942e4ac9eSOr Ozeri } else if (memcmp(buf, rbd_luks2_header_verification,
144042e4ac9eSOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
144142e4ac9eSOr Ozeri spec_info->u.rbd.data->encryption_format =
144242e4ac9eSOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2;
144342e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = true;
14440f385a24SOr Ozeri } else if (memcmp(buf, rbd_layered_luks_header_verification,
14450f385a24SOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
14460f385a24SOr Ozeri spec_info->u.rbd.data->encryption_format =
14470f385a24SOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS;
14480f385a24SOr Ozeri spec_info->u.rbd.data->has_encryption_format = true;
14490f385a24SOr Ozeri } else if (memcmp(buf, rbd_layered_luks2_header_verification,
14500f385a24SOr Ozeri RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
14510f385a24SOr Ozeri spec_info->u.rbd.data->encryption_format =
14520f385a24SOr Ozeri RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2;
14530f385a24SOr Ozeri spec_info->u.rbd.data->has_encryption_format = true;
145442e4ac9eSOr Ozeri } else {
145542e4ac9eSOr Ozeri spec_info->u.rbd.data->has_encryption_format = false;
145642e4ac9eSOr Ozeri }
145742e4ac9eSOr Ozeri
145842e4ac9eSOr Ozeri return spec_info;
145942e4ac9eSOr Ozeri }
146042e4ac9eSOr Ozeri
14610347a8fdSPeter Lieven /*
14620347a8fdSPeter Lieven * rbd_diff_iterate2 allows to interrupt the exection by returning a negative
14630347a8fdSPeter Lieven * value in the callback routine. Choose a value that does not conflict with
14640347a8fdSPeter Lieven * an existing exitcode and return it if we want to prematurely stop the
14650347a8fdSPeter Lieven * execution because we detected a change in the allocation status.
14660347a8fdSPeter Lieven */
14670347a8fdSPeter Lieven #define QEMU_RBD_EXIT_DIFF_ITERATE2 -9000
14680347a8fdSPeter Lieven
qemu_rbd_diff_iterate_cb(uint64_t offs,size_t len,int exists,void * opaque)14690347a8fdSPeter Lieven static int qemu_rbd_diff_iterate_cb(uint64_t offs, size_t len,
14700347a8fdSPeter Lieven int exists, void *opaque)
14710347a8fdSPeter Lieven {
14720347a8fdSPeter Lieven RBDDiffIterateReq *req = opaque;
14730347a8fdSPeter Lieven
14740347a8fdSPeter Lieven assert(req->offs + req->bytes <= offs);
14759e302f64SPeter Lieven
14769e302f64SPeter Lieven /* treat a hole like an unallocated area and bail out */
14779e302f64SPeter Lieven if (!exists) {
14789e302f64SPeter Lieven return 0;
14799e302f64SPeter Lieven }
14800347a8fdSPeter Lieven
14810347a8fdSPeter Lieven if (!req->exists && offs > req->offs) {
14820347a8fdSPeter Lieven /*
14830347a8fdSPeter Lieven * we started in an unallocated area and hit the first allocated
14840347a8fdSPeter Lieven * block. req->bytes must be set to the length of the unallocated area
14850347a8fdSPeter Lieven * before the allocated area. stop further processing.
14860347a8fdSPeter Lieven */
14870347a8fdSPeter Lieven req->bytes = offs - req->offs;
14880347a8fdSPeter Lieven return QEMU_RBD_EXIT_DIFF_ITERATE2;
14890347a8fdSPeter Lieven }
14900347a8fdSPeter Lieven
14910347a8fdSPeter Lieven if (req->exists && offs > req->offs + req->bytes) {
14920347a8fdSPeter Lieven /*
14930347a8fdSPeter Lieven * we started in an allocated area and jumped over an unallocated area,
14940347a8fdSPeter Lieven * req->bytes contains the length of the allocated area before the
14950347a8fdSPeter Lieven * unallocated area. stop further processing.
14960347a8fdSPeter Lieven */
14970347a8fdSPeter Lieven return QEMU_RBD_EXIT_DIFF_ITERATE2;
14980347a8fdSPeter Lieven }
14990347a8fdSPeter Lieven
15000347a8fdSPeter Lieven req->bytes += len;
15010347a8fdSPeter Lieven req->exists = true;
15020347a8fdSPeter Lieven
15030347a8fdSPeter Lieven return 0;
15040347a8fdSPeter Lieven }
15050347a8fdSPeter Lieven
qemu_rbd_co_block_status(BlockDriverState * bs,bool want_zero,int64_t offset,int64_t bytes,int64_t * pnum,int64_t * map,BlockDriverState ** file)15060347a8fdSPeter Lieven static int coroutine_fn qemu_rbd_co_block_status(BlockDriverState *bs,
15070347a8fdSPeter Lieven bool want_zero, int64_t offset,
15080347a8fdSPeter Lieven int64_t bytes, int64_t *pnum,
15090347a8fdSPeter Lieven int64_t *map,
15100347a8fdSPeter Lieven BlockDriverState **file)
15110347a8fdSPeter Lieven {
15120347a8fdSPeter Lieven BDRVRBDState *s = bs->opaque;
15130347a8fdSPeter Lieven int status, r;
15140347a8fdSPeter Lieven RBDDiffIterateReq req = { .offs = offset };
15150347a8fdSPeter Lieven uint64_t features, flags;
1516fc176116SPeter Lieven uint64_t head = 0;
15170347a8fdSPeter Lieven
15180347a8fdSPeter Lieven assert(offset + bytes <= s->image_size);
15190347a8fdSPeter Lieven
15200347a8fdSPeter Lieven /* default to all sectors allocated */
15210347a8fdSPeter Lieven status = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID;
15220347a8fdSPeter Lieven *map = offset;
15230347a8fdSPeter Lieven *file = bs;
15240347a8fdSPeter Lieven *pnum = bytes;
15250347a8fdSPeter Lieven
15260347a8fdSPeter Lieven /* check if RBD image supports fast-diff */
15270347a8fdSPeter Lieven r = rbd_get_features(s->image, &features);
15280347a8fdSPeter Lieven if (r < 0) {
15290347a8fdSPeter Lieven return status;
15300347a8fdSPeter Lieven }
15310347a8fdSPeter Lieven if (!(features & RBD_FEATURE_FAST_DIFF)) {
15320347a8fdSPeter Lieven return status;
15330347a8fdSPeter Lieven }
15340347a8fdSPeter Lieven
15350347a8fdSPeter Lieven /* check if RBD fast-diff result is valid */
15360347a8fdSPeter Lieven r = rbd_get_flags(s->image, &flags);
15370347a8fdSPeter Lieven if (r < 0) {
15380347a8fdSPeter Lieven return status;
15390347a8fdSPeter Lieven }
15400347a8fdSPeter Lieven if (flags & RBD_FLAG_FAST_DIFF_INVALID) {
15410347a8fdSPeter Lieven return status;
15420347a8fdSPeter Lieven }
15430347a8fdSPeter Lieven
1544fc176116SPeter Lieven #if LIBRBD_VERSION_CODE < LIBRBD_VERSION(1, 17, 0)
1545fc176116SPeter Lieven /*
1546fc176116SPeter Lieven * librbd had a bug until early 2022 that affected all versions of ceph that
1547fc176116SPeter Lieven * supported fast-diff. This bug results in reporting of incorrect offsets
1548fc176116SPeter Lieven * if the offset parameter to rbd_diff_iterate2 is not object aligned.
1549fc176116SPeter Lieven * Work around this bug by rounding down the offset to object boundaries.
1550fc176116SPeter Lieven * This is OK because we call rbd_diff_iterate2 with whole_object = true.
1551fc176116SPeter Lieven * However, this workaround only works for non cloned images with default
1552fc176116SPeter Lieven * striping.
1553fc176116SPeter Lieven *
1554fc176116SPeter Lieven * See: https://tracker.ceph.com/issues/53784
1555fc176116SPeter Lieven */
1556fc176116SPeter Lieven
1557fc176116SPeter Lieven /* check if RBD image has non-default striping enabled */
1558fc176116SPeter Lieven if (features & RBD_FEATURE_STRIPINGV2) {
1559fc176116SPeter Lieven return status;
1560fc176116SPeter Lieven }
1561fc176116SPeter Lieven
1562fc176116SPeter Lieven #pragma GCC diagnostic push
1563fc176116SPeter Lieven #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1564fc176116SPeter Lieven /*
1565fc176116SPeter Lieven * check if RBD image is a clone (= has a parent).
1566fc176116SPeter Lieven *
1567fc176116SPeter Lieven * rbd_get_parent_info is deprecated from Nautilus onwards, but the
1568fc176116SPeter Lieven * replacement rbd_get_parent is not present in Luminous and Mimic.
1569fc176116SPeter Lieven */
1570fc176116SPeter Lieven if (rbd_get_parent_info(s->image, NULL, 0, NULL, 0, NULL, 0) != -ENOENT) {
1571fc176116SPeter Lieven return status;
1572fc176116SPeter Lieven }
1573fc176116SPeter Lieven #pragma GCC diagnostic pop
1574fc176116SPeter Lieven
1575fc176116SPeter Lieven head = req.offs & (s->object_size - 1);
1576fc176116SPeter Lieven req.offs -= head;
1577fc176116SPeter Lieven bytes += head;
1578fc176116SPeter Lieven #endif
1579fc176116SPeter Lieven
1580fc176116SPeter Lieven r = rbd_diff_iterate2(s->image, NULL, req.offs, bytes, true, true,
15810347a8fdSPeter Lieven qemu_rbd_diff_iterate_cb, &req);
15820347a8fdSPeter Lieven if (r < 0 && r != QEMU_RBD_EXIT_DIFF_ITERATE2) {
15830347a8fdSPeter Lieven return status;
15840347a8fdSPeter Lieven }
15850347a8fdSPeter Lieven assert(req.bytes <= bytes);
15860347a8fdSPeter Lieven if (!req.exists) {
15870347a8fdSPeter Lieven if (r == 0) {
15880347a8fdSPeter Lieven /*
15890347a8fdSPeter Lieven * rbd_diff_iterate2 does not invoke callbacks for unallocated
15900347a8fdSPeter Lieven * areas. This here catches the case where no callback was
15910347a8fdSPeter Lieven * invoked at all (req.bytes == 0).
15920347a8fdSPeter Lieven */
15930347a8fdSPeter Lieven assert(req.bytes == 0);
15940347a8fdSPeter Lieven req.bytes = bytes;
15950347a8fdSPeter Lieven }
15960347a8fdSPeter Lieven status = BDRV_BLOCK_ZERO | BDRV_BLOCK_OFFSET_VALID;
15970347a8fdSPeter Lieven }
15980347a8fdSPeter Lieven
1599fc176116SPeter Lieven assert(req.bytes > head);
1600fc176116SPeter Lieven *pnum = req.bytes - head;
16010347a8fdSPeter Lieven return status;
16020347a8fdSPeter Lieven }
16030347a8fdSPeter Lieven
qemu_rbd_co_getlength(BlockDriverState * bs)1604c86422c5SEmanuele Giuseppe Esposito static int64_t coroutine_fn qemu_rbd_co_getlength(BlockDriverState *bs)
1605f27aaf4bSChristian Brunner {
1606f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque;
1607ad32e9c0SJosh Durgin int r;
1608f27aaf4bSChristian Brunner
16096d921418SPeter Lieven r = rbd_get_size(s->image, &s->image_size);
1610ad32e9c0SJosh Durgin if (r < 0) {
1611ad32e9c0SJosh Durgin return r;
1612f27aaf4bSChristian Brunner }
1613f27aaf4bSChristian Brunner
16146d921418SPeter Lieven return s->image_size;
1615ad32e9c0SJosh Durgin }
1616ad32e9c0SJosh Durgin
qemu_rbd_co_truncate(BlockDriverState * bs,int64_t offset,bool exact,PreallocMode prealloc,BdrvRequestFlags flags,Error ** errp)1617061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1618061ca8a3SKevin Wolf int64_t offset,
1619c80d8b06SMax Reitz bool exact,
1620061ca8a3SKevin Wolf PreallocMode prealloc,
162192b92799SKevin Wolf BdrvRequestFlags flags,
1622061ca8a3SKevin Wolf Error **errp)
162330cdc48cSJosh Durgin {
162430cdc48cSJosh Durgin int r;
162530cdc48cSJosh Durgin
16268243ccb7SMax Reitz if (prealloc != PREALLOC_MODE_OFF) {
16278243ccb7SMax Reitz error_setg(errp, "Unsupported preallocation mode '%s'",
1628977c736fSMarkus Armbruster PreallocMode_str(prealloc));
16298243ccb7SMax Reitz return -ENOTSUP;
16308243ccb7SMax Reitz }
16318243ccb7SMax Reitz
1632d24f8023SStefano Garzarella r = qemu_rbd_resize(bs, offset);
163330cdc48cSJosh Durgin if (r < 0) {
1634f59adb32SMax Reitz error_setg_errno(errp, -r, "Failed to resize file");
163530cdc48cSJosh Durgin return r;
163630cdc48cSJosh Durgin }
163730cdc48cSJosh Durgin
163830cdc48cSJosh Durgin return 0;
163930cdc48cSJosh Durgin }
164030cdc48cSJosh Durgin
qemu_rbd_snap_create(BlockDriverState * bs,QEMUSnapshotInfo * sn_info)1641ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1642ad32e9c0SJosh Durgin QEMUSnapshotInfo *sn_info)
1643f27aaf4bSChristian Brunner {
1644f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque;
1645f27aaf4bSChristian Brunner int r;
1646f27aaf4bSChristian Brunner
1647f27aaf4bSChristian Brunner if (sn_info->name[0] == '\0') {
1648f27aaf4bSChristian Brunner return -EINVAL; /* we need a name for rbd snapshots */
1649f27aaf4bSChristian Brunner }
1650f27aaf4bSChristian Brunner
1651f27aaf4bSChristian Brunner /*
1652f27aaf4bSChristian Brunner * rbd snapshots are using the name as the user controlled unique identifier
1653f27aaf4bSChristian Brunner * we can't use the rbd snapid for that purpose, as it can't be set
1654f27aaf4bSChristian Brunner */
1655f27aaf4bSChristian Brunner if (sn_info->id_str[0] != '\0' &&
1656f27aaf4bSChristian Brunner strcmp(sn_info->id_str, sn_info->name) != 0) {
1657f27aaf4bSChristian Brunner return -EINVAL;
1658f27aaf4bSChristian Brunner }
1659f27aaf4bSChristian Brunner
1660f27aaf4bSChristian Brunner if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1661f27aaf4bSChristian Brunner return -ERANGE;
1662f27aaf4bSChristian Brunner }
1663f27aaf4bSChristian Brunner
1664ad32e9c0SJosh Durgin r = rbd_snap_create(s->image, sn_info->name);
1665f27aaf4bSChristian Brunner if (r < 0) {
1666ad32e9c0SJosh Durgin error_report("failed to create snap: %s", strerror(-r));
1667f27aaf4bSChristian Brunner return r;
1668f27aaf4bSChristian Brunner }
1669f27aaf4bSChristian Brunner
1670f27aaf4bSChristian Brunner return 0;
1671f27aaf4bSChristian Brunner }
1672f27aaf4bSChristian Brunner
qemu_rbd_snap_remove(BlockDriverState * bs,const char * snapshot_id,const char * snapshot_name,Error ** errp)1673bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1674a89d89d3SWenchao Xia const char *snapshot_id,
1675a89d89d3SWenchao Xia const char *snapshot_name,
1676a89d89d3SWenchao Xia Error **errp)
1677bd603247SGregory Farnum {
1678bd603247SGregory Farnum BDRVRBDState *s = bs->opaque;
1679bd603247SGregory Farnum int r;
1680bd603247SGregory Farnum
1681a89d89d3SWenchao Xia if (!snapshot_name) {
1682a89d89d3SWenchao Xia error_setg(errp, "rbd need a valid snapshot name");
1683a89d89d3SWenchao Xia return -EINVAL;
1684a89d89d3SWenchao Xia }
1685a89d89d3SWenchao Xia
1686a89d89d3SWenchao Xia /* If snapshot_id is specified, it must be equal to name, see
1687a89d89d3SWenchao Xia qemu_rbd_snap_list() */
1688a89d89d3SWenchao Xia if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1689a89d89d3SWenchao Xia error_setg(errp,
1690a89d89d3SWenchao Xia "rbd do not support snapshot id, it should be NULL or "
1691a89d89d3SWenchao Xia "equal to snapshot name");
1692a89d89d3SWenchao Xia return -EINVAL;
1693a89d89d3SWenchao Xia }
1694a89d89d3SWenchao Xia
1695bd603247SGregory Farnum r = rbd_snap_remove(s->image, snapshot_name);
1696a89d89d3SWenchao Xia if (r < 0) {
1697a89d89d3SWenchao Xia error_setg_errno(errp, -r, "Failed to remove the snapshot");
1698a89d89d3SWenchao Xia }
1699bd603247SGregory Farnum return r;
1700bd603247SGregory Farnum }
1701bd603247SGregory Farnum
qemu_rbd_snap_rollback(BlockDriverState * bs,const char * snapshot_name)1702bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1703bd603247SGregory Farnum const char *snapshot_name)
1704bd603247SGregory Farnum {
1705bd603247SGregory Farnum BDRVRBDState *s = bs->opaque;
1706bd603247SGregory Farnum
17079be38598SEduardo Habkost return rbd_snap_rollback(s->image, snapshot_name);
1708bd603247SGregory Farnum }
1709bd603247SGregory Farnum
qemu_rbd_snap_list(BlockDriverState * bs,QEMUSnapshotInfo ** psn_tab)1710ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1711ad32e9c0SJosh Durgin QEMUSnapshotInfo **psn_tab)
1712f27aaf4bSChristian Brunner {
1713f27aaf4bSChristian Brunner BDRVRBDState *s = bs->opaque;
1714f27aaf4bSChristian Brunner QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1715ad32e9c0SJosh Durgin int i, snap_count;
1716ad32e9c0SJosh Durgin rbd_snap_info_t *snaps;
1717ad32e9c0SJosh Durgin int max_snaps = RBD_MAX_SNAPS;
1718f27aaf4bSChristian Brunner
1719ad32e9c0SJosh Durgin do {
172002c4f26bSMarkus Armbruster snaps = g_new(rbd_snap_info_t, max_snaps);
1721ad32e9c0SJosh Durgin snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
17229e6337d0SStefan Hajnoczi if (snap_count <= 0) {
17237267c094SAnthony Liguori g_free(snaps);
1724f27aaf4bSChristian Brunner }
1725ad32e9c0SJosh Durgin } while (snap_count == -ERANGE);
1726f27aaf4bSChristian Brunner
1727ad32e9c0SJosh Durgin if (snap_count <= 0) {
1728b9c53290SJosh Durgin goto done;
1729f27aaf4bSChristian Brunner }
1730f27aaf4bSChristian Brunner
17315839e53bSMarkus Armbruster sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1732f27aaf4bSChristian Brunner
1733ad32e9c0SJosh Durgin for (i = 0; i < snap_count; i++) {
1734ad32e9c0SJosh Durgin const char *snap_name = snaps[i].name;
1735f27aaf4bSChristian Brunner
1736f27aaf4bSChristian Brunner sn_info = sn_tab + i;
1737f27aaf4bSChristian Brunner pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1738f27aaf4bSChristian Brunner pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1739f27aaf4bSChristian Brunner
1740ad32e9c0SJosh Durgin sn_info->vm_state_size = snaps[i].size;
1741f27aaf4bSChristian Brunner sn_info->date_sec = 0;
1742f27aaf4bSChristian Brunner sn_info->date_nsec = 0;
1743f27aaf4bSChristian Brunner sn_info->vm_clock_nsec = 0;
1744f27aaf4bSChristian Brunner }
1745ad32e9c0SJosh Durgin rbd_snap_list_end(snaps);
17469e6337d0SStefan Hajnoczi g_free(snaps);
1747ad32e9c0SJosh Durgin
1748b9c53290SJosh Durgin done:
1749f27aaf4bSChristian Brunner *psn_tab = sn_tab;
1750f27aaf4bSChristian Brunner return snap_count;
1751f27aaf4bSChristian Brunner }
1752f27aaf4bSChristian Brunner
qemu_rbd_co_invalidate_cache(BlockDriverState * bs,Error ** errp)17532b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1754be217884SAdam Crume Error **errp)
1755be217884SAdam Crume {
1756be217884SAdam Crume BDRVRBDState *s = bs->opaque;
1757be217884SAdam Crume int r = rbd_invalidate_cache(s->image);
1758be217884SAdam Crume if (r < 0) {
1759be217884SAdam Crume error_setg_errno(errp, -r, "Failed to invalidate the cache");
1760be217884SAdam Crume }
1761be217884SAdam Crume }
1762be217884SAdam Crume
1763bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1764bd0cf596SChunyan Liu .name = "rbd-create-opts",
1765bd0cf596SChunyan Liu .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1766bd0cf596SChunyan Liu .desc = {
1767f27aaf4bSChristian Brunner {
1768f27aaf4bSChristian Brunner .name = BLOCK_OPT_SIZE,
1769bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE,
1770f27aaf4bSChristian Brunner .help = "Virtual disk size"
1771f27aaf4bSChristian Brunner },
1772f27aaf4bSChristian Brunner {
1773f27aaf4bSChristian Brunner .name = BLOCK_OPT_CLUSTER_SIZE,
1774bd0cf596SChunyan Liu .type = QEMU_OPT_SIZE,
1775f27aaf4bSChristian Brunner .help = "RBD object size"
1776f27aaf4bSChristian Brunner },
177760390a21SDaniel P. Berrange {
177860390a21SDaniel P. Berrange .name = "password-secret",
177960390a21SDaniel P. Berrange .type = QEMU_OPT_STRING,
178060390a21SDaniel P. Berrange .help = "ID of secret providing the password",
178160390a21SDaniel P. Berrange },
178242e4ac9eSOr Ozeri {
178342e4ac9eSOr Ozeri .name = "encrypt.format",
178442e4ac9eSOr Ozeri .type = QEMU_OPT_STRING,
178542e4ac9eSOr Ozeri .help = "Encrypt the image, format choices: 'luks', 'luks2'",
178642e4ac9eSOr Ozeri },
178742e4ac9eSOr Ozeri {
178842e4ac9eSOr Ozeri .name = "encrypt.cipher-alg",
178942e4ac9eSOr Ozeri .type = QEMU_OPT_STRING,
179042e4ac9eSOr Ozeri .help = "Name of encryption cipher algorithm"
179142e4ac9eSOr Ozeri " (allowed values: aes-128, aes-256)",
179242e4ac9eSOr Ozeri },
179342e4ac9eSOr Ozeri {
179442e4ac9eSOr Ozeri .name = "encrypt.key-secret",
179542e4ac9eSOr Ozeri .type = QEMU_OPT_STRING,
179642e4ac9eSOr Ozeri .help = "ID of secret providing LUKS passphrase",
179742e4ac9eSOr Ozeri },
1798bd0cf596SChunyan Liu { /* end of list */ }
1799bd0cf596SChunyan Liu }
1800f27aaf4bSChristian Brunner };
1801f27aaf4bSChristian Brunner
18022654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = {
18032654267cSMax Reitz "pool",
18047bae7c80SStefano Garzarella "namespace",
18052654267cSMax Reitz "image",
18062654267cSMax Reitz "conf",
18072654267cSMax Reitz "snapshot",
18082654267cSMax Reitz "user",
18092654267cSMax Reitz "server.",
18102654267cSMax Reitz "password-secret",
18112654267cSMax Reitz
18122654267cSMax Reitz NULL
18132654267cSMax Reitz };
18142654267cSMax Reitz
1815f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1816f27aaf4bSChristian Brunner .format_name = "rbd",
1817f27aaf4bSChristian Brunner .instance_size = sizeof(BDRVRBDState),
181844b424dcSPaolo Bonzini
1819c7cacb3eSJeff Cody .bdrv_parse_filename = qemu_rbd_parse_filename,
182044b424dcSPaolo Bonzini .bdrv_open = qemu_rbd_open,
1821ad32e9c0SJosh Durgin .bdrv_close = qemu_rbd_close,
182256e7cf8dSJeff Cody .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
18231bebea37SKevin Wolf .bdrv_co_create = qemu_rbd_co_create,
1824efc75e2aSStefan Hajnoczi .bdrv_co_create_opts = qemu_rbd_co_create_opts,
18253ac21627SPeter Lieven .bdrv_has_zero_init = bdrv_has_zero_init_1,
18263d47eb0aSEmanuele Giuseppe Esposito .bdrv_co_get_info = qemu_rbd_co_get_info,
182742e4ac9eSOr Ozeri .bdrv_get_specific_info = qemu_rbd_get_specific_info,
1828bd0cf596SChunyan Liu .create_opts = &qemu_rbd_create_opts,
1829c86422c5SEmanuele Giuseppe Esposito .bdrv_co_getlength = qemu_rbd_co_getlength,
1830061ca8a3SKevin Wolf .bdrv_co_truncate = qemu_rbd_co_truncate,
1831f27aaf4bSChristian Brunner .protocol_name = "rbd",
1832f27aaf4bSChristian Brunner
1833c3e5fac5SPeter Lieven .bdrv_co_preadv = qemu_rbd_co_preadv,
1834c3e5fac5SPeter Lieven .bdrv_co_pwritev = qemu_rbd_co_pwritev,
1835c3e5fac5SPeter Lieven .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
1836c3e5fac5SPeter Lieven .bdrv_co_pdiscard = qemu_rbd_co_pdiscard,
1837c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1838c56ac27dSPeter Lieven .bdrv_co_pwrite_zeroes = qemu_rbd_co_pwrite_zeroes,
1839c56ac27dSPeter Lieven #endif
18400347a8fdSPeter Lieven .bdrv_co_block_status = qemu_rbd_co_block_status,
1841787f3133SJosh Durgin
1842ad32e9c0SJosh Durgin .bdrv_snapshot_create = qemu_rbd_snap_create,
1843bd603247SGregory Farnum .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1844ad32e9c0SJosh Durgin .bdrv_snapshot_list = qemu_rbd_snap_list,
1845bd603247SGregory Farnum .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
18462b148f39SPaolo Bonzini .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
18472654267cSMax Reitz
18482654267cSMax Reitz .strong_runtime_opts = qemu_rbd_strong_runtime_opts,
1849f27aaf4bSChristian Brunner };
1850f27aaf4bSChristian Brunner
bdrv_rbd_init(void)1851f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1852f27aaf4bSChristian Brunner {
1853f27aaf4bSChristian Brunner bdrv_register(&bdrv_rbd);
1854f27aaf4bSChristian Brunner }
1855f27aaf4bSChristian Brunner
1856f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1857