xref: /openbmc/qemu/block/rbd.c (revision e75abedab7e10043ed81b1cbc3033409aff0159e)
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"
21737e150eSPaolo Bonzini #include "block/block_int.h"
22609f45eaSMax Reitz #include "block/qdict.h"
2360390a21SDaniel P. Berrange #include "crypto/secret.h"
24f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
25e4ec5ad4SPavel Dovgalyuk #include "sysemu/replay.h"
26c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
27452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
28e98c6961SEric Blake #include "qapi/qmp/qjson.h"
2947e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
304bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h"
314bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h"
32f27aaf4bSChristian Brunner 
33f27aaf4bSChristian Brunner /*
34f27aaf4bSChristian Brunner  * When specifying the image filename use:
35f27aaf4bSChristian Brunner  *
36fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
37f27aaf4bSChristian Brunner  *
389e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
39f27aaf4bSChristian Brunner  *
409e1fbcdeSSage Weil  * devicename is the name of the rbd image.
41f27aaf4bSChristian Brunner  *
429e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
439e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
44fab5cf59SJosh Durgin  *
459e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
469e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
479e1fbcdeSSage Weil  * (normally 'admin').
48f27aaf4bSChristian Brunner  *
499e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
509e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
519e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
529e1fbcdeSSage Weil  * file, specify conf=/dev/null.
53f27aaf4bSChristian Brunner  *
549e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
559e1fbcdeSSage Weil  * leading "\".
56f27aaf4bSChristian Brunner  */
57f27aaf4bSChristian Brunner 
58f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
59f27aaf4bSChristian Brunner 
60ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
61ad32e9c0SJosh Durgin 
6242e4ac9eSOr Ozeri #define RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN 8
6342e4ac9eSOr Ozeri 
6442e4ac9eSOr Ozeri static const char rbd_luks_header_verification[
6542e4ac9eSOr Ozeri         RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
6642e4ac9eSOr Ozeri     'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 1
6742e4ac9eSOr Ozeri };
6842e4ac9eSOr Ozeri 
6942e4ac9eSOr Ozeri static const char rbd_luks2_header_verification[
7042e4ac9eSOr Ozeri         RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {
7142e4ac9eSOr Ozeri     'L', 'U', 'K', 'S', 0xBA, 0xBE, 0, 2
7242e4ac9eSOr Ozeri };
7342e4ac9eSOr Ozeri 
74787f3133SJosh Durgin typedef enum {
75787f3133SJosh Durgin     RBD_AIO_READ,
76787f3133SJosh Durgin     RBD_AIO_WRITE,
77dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
78c56ac27dSPeter Lieven     RBD_AIO_FLUSH,
79c56ac27dSPeter Lieven     RBD_AIO_WRITE_ZEROES
80787f3133SJosh Durgin } RBDAIOCmd;
81787f3133SJosh Durgin 
82f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
83ad32e9c0SJosh Durgin     rados_t cluster;
84ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
85ad32e9c0SJosh Durgin     rbd_image_t image;
8680b61a27SJeff Cody     char *image_name;
87ad32e9c0SJosh Durgin     char *snap;
8819ae9ae0SFlorian Florensa     char *namespace;
89d24f8023SStefano Garzarella     uint64_t image_size;
90832a93dcSPeter Lieven     uint64_t object_size;
91f27aaf4bSChristian Brunner } BDRVRBDState;
92f27aaf4bSChristian Brunner 
93c3e5fac5SPeter Lieven typedef struct RBDTask {
94c3e5fac5SPeter Lieven     BlockDriverState *bs;
95c3e5fac5SPeter Lieven     Coroutine *co;
96c3e5fac5SPeter Lieven     bool complete;
97c3e5fac5SPeter Lieven     int64_t ret;
98c3e5fac5SPeter Lieven } RBDTask;
99c3e5fac5SPeter Lieven 
100aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
101aa045c2dSKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
102aa045c2dSKevin Wolf                             const char *keypairs, const char *secretid,
103aa045c2dSKevin Wolf                             Error **errp);
104aa045c2dSKevin Wolf 
1052b99cfceSConnor Kuehl static char *qemu_rbd_strchr(char *src, char delim)
1062b99cfceSConnor Kuehl {
1072b99cfceSConnor Kuehl     char *p;
1082b99cfceSConnor Kuehl 
1092b99cfceSConnor Kuehl     for (p = src; *p; ++p) {
1102b99cfceSConnor Kuehl         if (*p == delim) {
1112b99cfceSConnor Kuehl             return p;
1122b99cfceSConnor Kuehl         }
1132b99cfceSConnor Kuehl         if (*p == '\\' && p[1] != '\0') {
1142b99cfceSConnor Kuehl             ++p;
1152b99cfceSConnor Kuehl         }
1162b99cfceSConnor Kuehl     }
1172b99cfceSConnor Kuehl 
1182b99cfceSConnor Kuehl     return NULL;
1192b99cfceSConnor Kuehl }
1202b99cfceSConnor Kuehl 
1212b99cfceSConnor Kuehl 
122730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
123f27aaf4bSChristian Brunner {
124f27aaf4bSChristian Brunner     char *end;
125f27aaf4bSChristian Brunner 
126f27aaf4bSChristian Brunner     *p = NULL;
127f27aaf4bSChristian Brunner 
1282b99cfceSConnor Kuehl     end = qemu_rbd_strchr(src, delim);
1292b99cfceSConnor Kuehl     if (end) {
130f27aaf4bSChristian Brunner         *p = end + 1;
131f27aaf4bSChristian Brunner         *end = '\0';
132f27aaf4bSChristian Brunner     }
1337830f909SJeff Cody     return src;
134f27aaf4bSChristian Brunner }
135f27aaf4bSChristian Brunner 
13616a06b24SSage Weil static void qemu_rbd_unescape(char *src)
13716a06b24SSage Weil {
13816a06b24SSage Weil     char *p;
13916a06b24SSage Weil 
14016a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14116a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
14216a06b24SSage Weil             src++;
14316a06b24SSage Weil         }
14416a06b24SSage Weil         *p = *src;
14516a06b24SSage Weil     }
14616a06b24SSage Weil     *p = '\0';
14716a06b24SSage Weil }
14816a06b24SSage Weil 
149c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
150d61563b2SMarkus Armbruster                                     Error **errp)
151f27aaf4bSChristian Brunner {
152f27aaf4bSChristian Brunner     const char *start;
153e98c6961SEric Blake     char *p, *buf;
154e98c6961SEric Blake     QList *keypairs = NULL;
15519ae9ae0SFlorian Florensa     char *found_str, *image_name;
156f27aaf4bSChristian Brunner 
157f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
158d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
159c7cacb3eSJeff Cody         return;
160f27aaf4bSChristian Brunner     }
161f27aaf4bSChristian Brunner 
1627267c094SAnthony Liguori     buf = g_strdup(start);
163f27aaf4bSChristian Brunner     p = buf;
164f27aaf4bSChristian Brunner 
165730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1667830f909SJeff Cody     if (!p) {
1677830f909SJeff Cody         error_setg(errp, "Pool name is required");
1687830f909SJeff Cody         goto done;
1697830f909SJeff Cody     }
1707830f909SJeff Cody     qemu_rbd_unescape(found_str);
17146f5ac20SEric Blake     qdict_put_str(options, "pool", found_str);
172fab5cf59SJosh Durgin 
1732b99cfceSConnor Kuehl     if (qemu_rbd_strchr(p, '@')) {
17419ae9ae0SFlorian Florensa         image_name = qemu_rbd_next_tok(p, '@', &p);
1757830f909SJeff Cody 
176730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1777830f909SJeff Cody         qemu_rbd_unescape(found_str);
17846f5ac20SEric Blake         qdict_put_str(options, "snapshot", found_str);
1797830f909SJeff Cody     } else {
18019ae9ae0SFlorian Florensa         image_name = qemu_rbd_next_tok(p, ':', &p);
1817830f909SJeff Cody     }
18219ae9ae0SFlorian Florensa     /* Check for namespace in the image_name */
1832b99cfceSConnor Kuehl     if (qemu_rbd_strchr(image_name, '/')) {
18419ae9ae0SFlorian Florensa         found_str = qemu_rbd_next_tok(image_name, '/', &image_name);
18519ae9ae0SFlorian Florensa         qemu_rbd_unescape(found_str);
18619ae9ae0SFlorian Florensa         qdict_put_str(options, "namespace", found_str);
18719ae9ae0SFlorian Florensa     } else {
18819ae9ae0SFlorian Florensa         qdict_put_str(options, "namespace", "");
18919ae9ae0SFlorian Florensa     }
19019ae9ae0SFlorian Florensa     qemu_rbd_unescape(image_name);
19119ae9ae0SFlorian Florensa     qdict_put_str(options, "image", image_name);
1927830f909SJeff Cody     if (!p) {
193f27aaf4bSChristian Brunner         goto done;
194f27aaf4bSChristian Brunner     }
195f27aaf4bSChristian Brunner 
196c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
197c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
198c7cacb3eSJeff Cody     while (p) {
199c7cacb3eSJeff Cody         char *name, *value;
200730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
201c7cacb3eSJeff Cody         if (!p) {
202c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
203c7cacb3eSJeff Cody             break;
204c7cacb3eSJeff Cody         }
205c7cacb3eSJeff Cody 
206c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
207c7cacb3eSJeff Cody 
208730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
209c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
210c7cacb3eSJeff Cody 
211c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
21246f5ac20SEric Blake             qdict_put_str(options, "conf", value);
213c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
21446f5ac20SEric Blake             qdict_put_str(options, "user", value);
215c7cacb3eSJeff Cody         } else {
216e98c6961SEric Blake             /*
217e98c6961SEric Blake              * We pass these internally to qemu_rbd_set_keypairs(), so
218e98c6961SEric Blake              * we can get away with the simpler list of [ "key1",
219e98c6961SEric Blake              * "value1", "key2", "value2" ] rather than a raw dict
220e98c6961SEric Blake              * { "key1": "value1", "key2": "value2" } where we can't
221e98c6961SEric Blake              * guarantee order, or even a more correct but complex
222e98c6961SEric Blake              * [ { "key1": "value1" }, { "key2": "value2" } ]
223e98c6961SEric Blake              */
224e98c6961SEric Blake             if (!keypairs) {
225e98c6961SEric Blake                 keypairs = qlist_new();
226c7cacb3eSJeff Cody             }
22746f5ac20SEric Blake             qlist_append_str(keypairs, name);
22846f5ac20SEric Blake             qlist_append_str(keypairs, value);
229c7cacb3eSJeff Cody         }
230c7cacb3eSJeff Cody     }
231c7cacb3eSJeff Cody 
232e98c6961SEric Blake     if (keypairs) {
233e98c6961SEric Blake         qdict_put(options, "=keyvalue-pairs",
234eab3a467SMarkus Armbruster                   qstring_from_gstring(qobject_to_json(QOBJECT(keypairs))));
235c7cacb3eSJeff Cody     }
236c7cacb3eSJeff Cody 
237f27aaf4bSChristian Brunner done:
2387267c094SAnthony Liguori     g_free(buf);
239cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
240c7cacb3eSJeff Cody     return;
2417c7e9df0SSage Weil }
2427c7e9df0SSage Weil 
243d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
24460390a21SDaniel P. Berrange                              Error **errp)
24560390a21SDaniel P. Berrange {
246d083f954SMarkus Armbruster     char *key, *acr;
247a3699de4SMarkus Armbruster     int r;
248a3699de4SMarkus Armbruster     GString *accu;
249a3699de4SMarkus Armbruster     RbdAuthModeList *auth;
25060390a21SDaniel P. Berrange 
251d083f954SMarkus Armbruster     if (opts->key_secret) {
252d083f954SMarkus Armbruster         key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
253d083f954SMarkus Armbruster         if (!key) {
254d083f954SMarkus Armbruster             return -EIO;
25560390a21SDaniel P. Berrange         }
256d083f954SMarkus Armbruster         r = rados_conf_set(cluster, "key", key);
257d083f954SMarkus Armbruster         g_free(key);
258d083f954SMarkus Armbruster         if (r < 0) {
259d083f954SMarkus Armbruster             error_setg_errno(errp, -r, "Could not set 'key'");
260d083f954SMarkus Armbruster             return r;
261d083f954SMarkus Armbruster         }
262a3699de4SMarkus Armbruster     }
263a3699de4SMarkus Armbruster 
264a3699de4SMarkus Armbruster     if (opts->has_auth_client_required) {
265a3699de4SMarkus Armbruster         accu = g_string_new("");
266a3699de4SMarkus Armbruster         for (auth = opts->auth_client_required; auth; auth = auth->next) {
267a3699de4SMarkus Armbruster             if (accu->str[0]) {
268a3699de4SMarkus Armbruster                 g_string_append_c(accu, ';');
269a3699de4SMarkus Armbruster             }
270a3699de4SMarkus Armbruster             g_string_append(accu, RbdAuthMode_str(auth->value));
271a3699de4SMarkus Armbruster         }
272a3699de4SMarkus Armbruster         acr = g_string_free(accu, FALSE);
273a3699de4SMarkus Armbruster         r = rados_conf_set(cluster, "auth_client_required", acr);
274a3699de4SMarkus Armbruster         g_free(acr);
275a3699de4SMarkus Armbruster         if (r < 0) {
276a3699de4SMarkus Armbruster             error_setg_errno(errp, -r,
277a3699de4SMarkus Armbruster                              "Could not set 'auth_client_required'");
278a3699de4SMarkus Armbruster             return r;
279a3699de4SMarkus Armbruster         }
280a3699de4SMarkus Armbruster     }
28160390a21SDaniel P. Berrange 
28260390a21SDaniel P. Berrange     return 0;
28360390a21SDaniel P. Berrange }
28460390a21SDaniel P. Berrange 
285e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
286e34d8f29SJosh Durgin                                  Error **errp)
287fab5cf59SJosh Durgin {
288e98c6961SEric Blake     QList *keypairs;
289e98c6961SEric Blake     QString *name;
290e98c6961SEric Blake     QString *value;
291e98c6961SEric Blake     const char *key;
292e98c6961SEric Blake     size_t remaining;
293fab5cf59SJosh Durgin     int ret = 0;
294fab5cf59SJosh Durgin 
295e98c6961SEric Blake     if (!keypairs_json) {
296e98c6961SEric Blake         return ret;
297fab5cf59SJosh Durgin     }
2987dc847ebSMax Reitz     keypairs = qobject_to(QList,
2997dc847ebSMax Reitz                           qobject_from_json(keypairs_json, &error_abort));
300e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
301e98c6961SEric Blake     assert(remaining);
302fab5cf59SJosh Durgin 
303e98c6961SEric Blake     while (remaining--) {
3047dc847ebSMax Reitz         name = qobject_to(QString, qlist_pop(keypairs));
3057dc847ebSMax Reitz         value = qobject_to(QString, qlist_pop(keypairs));
306e98c6961SEric Blake         assert(name && value);
307e98c6961SEric Blake         key = qstring_get_str(name);
308fab5cf59SJosh Durgin 
309e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
310cb3e7f08SMarc-André Lureau         qobject_unref(value);
311fab5cf59SJosh Durgin         if (ret < 0) {
312e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
313cb3e7f08SMarc-André Lureau             qobject_unref(name);
314fab5cf59SJosh Durgin             ret = -EINVAL;
315fab5cf59SJosh Durgin             break;
316fab5cf59SJosh Durgin         }
317cb3e7f08SMarc-André Lureau         qobject_unref(name);
318fab5cf59SJosh Durgin     }
319fab5cf59SJosh Durgin 
320cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
321fab5cf59SJosh Durgin     return ret;
322fab5cf59SJosh Durgin }
323fab5cf59SJosh Durgin 
32442e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
32542e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_options(
32642e4ac9eSOr Ozeri         RbdEncryptionOptionsLUKSBase *luks_opts,
32742e4ac9eSOr Ozeri         char **passphrase,
32842e4ac9eSOr Ozeri         size_t *passphrase_len,
32942e4ac9eSOr Ozeri         Error **errp)
33042e4ac9eSOr Ozeri {
33142e4ac9eSOr Ozeri     return qcrypto_secret_lookup(luks_opts->key_secret, (uint8_t **)passphrase,
33242e4ac9eSOr Ozeri                                  passphrase_len, errp);
33342e4ac9eSOr Ozeri }
33442e4ac9eSOr Ozeri 
33542e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_create_options(
33642e4ac9eSOr Ozeri         RbdEncryptionCreateOptionsLUKSBase *luks_opts,
33742e4ac9eSOr Ozeri         rbd_encryption_algorithm_t *alg,
33842e4ac9eSOr Ozeri         char **passphrase,
33942e4ac9eSOr Ozeri         size_t *passphrase_len,
34042e4ac9eSOr Ozeri         Error **errp)
34142e4ac9eSOr Ozeri {
34242e4ac9eSOr Ozeri     int r = 0;
34342e4ac9eSOr Ozeri 
34442e4ac9eSOr Ozeri     r = qemu_rbd_convert_luks_options(
34542e4ac9eSOr Ozeri             qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts),
34642e4ac9eSOr Ozeri             passphrase, passphrase_len, errp);
34742e4ac9eSOr Ozeri     if (r < 0) {
34842e4ac9eSOr Ozeri         return r;
34942e4ac9eSOr Ozeri     }
35042e4ac9eSOr Ozeri 
35142e4ac9eSOr Ozeri     if (luks_opts->has_cipher_alg) {
35242e4ac9eSOr Ozeri         switch (luks_opts->cipher_alg) {
35342e4ac9eSOr Ozeri             case QCRYPTO_CIPHER_ALG_AES_128: {
35442e4ac9eSOr Ozeri                 *alg = RBD_ENCRYPTION_ALGORITHM_AES128;
35542e4ac9eSOr Ozeri                 break;
35642e4ac9eSOr Ozeri             }
35742e4ac9eSOr Ozeri             case QCRYPTO_CIPHER_ALG_AES_256: {
35842e4ac9eSOr Ozeri                 *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
35942e4ac9eSOr Ozeri                 break;
36042e4ac9eSOr Ozeri             }
36142e4ac9eSOr Ozeri             default: {
36242e4ac9eSOr Ozeri                 r = -ENOTSUP;
36342e4ac9eSOr Ozeri                 error_setg_errno(errp, -r, "unknown encryption algorithm: %u",
36442e4ac9eSOr Ozeri                                  luks_opts->cipher_alg);
36542e4ac9eSOr Ozeri                 return r;
36642e4ac9eSOr Ozeri             }
36742e4ac9eSOr Ozeri         }
36842e4ac9eSOr Ozeri     } else {
36942e4ac9eSOr Ozeri         /* default alg */
37042e4ac9eSOr Ozeri         *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
37142e4ac9eSOr Ozeri     }
37242e4ac9eSOr Ozeri 
37342e4ac9eSOr Ozeri     return 0;
37442e4ac9eSOr Ozeri }
37542e4ac9eSOr Ozeri 
37642e4ac9eSOr Ozeri static int qemu_rbd_encryption_format(rbd_image_t image,
37742e4ac9eSOr Ozeri                                       RbdEncryptionCreateOptions *encrypt,
37842e4ac9eSOr Ozeri                                       Error **errp)
37942e4ac9eSOr Ozeri {
38042e4ac9eSOr Ozeri     int r = 0;
38142e4ac9eSOr Ozeri     g_autofree char *passphrase = NULL;
38242e4ac9eSOr Ozeri     size_t passphrase_len;
38342e4ac9eSOr Ozeri     rbd_encryption_format_t format;
38442e4ac9eSOr Ozeri     rbd_encryption_options_t opts;
38542e4ac9eSOr Ozeri     rbd_encryption_luks1_format_options_t luks_opts;
38642e4ac9eSOr Ozeri     rbd_encryption_luks2_format_options_t luks2_opts;
38742e4ac9eSOr Ozeri     size_t opts_size;
38842e4ac9eSOr Ozeri     uint64_t raw_size, effective_size;
38942e4ac9eSOr Ozeri 
39042e4ac9eSOr Ozeri     r = rbd_get_size(image, &raw_size);
39142e4ac9eSOr Ozeri     if (r < 0) {
39242e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot get raw image size");
39342e4ac9eSOr Ozeri         return r;
39442e4ac9eSOr Ozeri     }
39542e4ac9eSOr Ozeri 
39642e4ac9eSOr Ozeri     switch (encrypt->format) {
39742e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
39842e4ac9eSOr Ozeri             memset(&luks_opts, 0, sizeof(luks_opts));
39942e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS1;
40042e4ac9eSOr Ozeri             opts = &luks_opts;
40142e4ac9eSOr Ozeri             opts_size = sizeof(luks_opts);
40242e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_create_options(
40342e4ac9eSOr Ozeri                     qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt->u.luks),
40442e4ac9eSOr Ozeri                     &luks_opts.alg, &passphrase, &passphrase_len, errp);
40542e4ac9eSOr Ozeri             if (r < 0) {
40642e4ac9eSOr Ozeri                 return r;
40742e4ac9eSOr Ozeri             }
40842e4ac9eSOr Ozeri             luks_opts.passphrase = passphrase;
40942e4ac9eSOr Ozeri             luks_opts.passphrase_size = passphrase_len;
41042e4ac9eSOr Ozeri             break;
41142e4ac9eSOr Ozeri         }
41242e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
41342e4ac9eSOr Ozeri             memset(&luks2_opts, 0, sizeof(luks2_opts));
41442e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS2;
41542e4ac9eSOr Ozeri             opts = &luks2_opts;
41642e4ac9eSOr Ozeri             opts_size = sizeof(luks2_opts);
41742e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_create_options(
41842e4ac9eSOr Ozeri                     qapi_RbdEncryptionCreateOptionsLUKS2_base(
41942e4ac9eSOr Ozeri                             &encrypt->u.luks2),
42042e4ac9eSOr Ozeri                     &luks2_opts.alg, &passphrase, &passphrase_len, errp);
42142e4ac9eSOr Ozeri             if (r < 0) {
42242e4ac9eSOr Ozeri                 return r;
42342e4ac9eSOr Ozeri             }
42442e4ac9eSOr Ozeri             luks2_opts.passphrase = passphrase;
42542e4ac9eSOr Ozeri             luks2_opts.passphrase_size = passphrase_len;
42642e4ac9eSOr Ozeri             break;
42742e4ac9eSOr Ozeri         }
42842e4ac9eSOr Ozeri         default: {
42942e4ac9eSOr Ozeri             r = -ENOTSUP;
43042e4ac9eSOr Ozeri             error_setg_errno(
43142e4ac9eSOr Ozeri                     errp, -r, "unknown image encryption format: %u",
43242e4ac9eSOr Ozeri                     encrypt->format);
43342e4ac9eSOr Ozeri             return r;
43442e4ac9eSOr Ozeri         }
43542e4ac9eSOr Ozeri     }
43642e4ac9eSOr Ozeri 
43742e4ac9eSOr Ozeri     r = rbd_encryption_format(image, format, opts, opts_size);
43842e4ac9eSOr Ozeri     if (r < 0) {
43942e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "encryption format fail");
44042e4ac9eSOr Ozeri         return r;
44142e4ac9eSOr Ozeri     }
44242e4ac9eSOr Ozeri 
44342e4ac9eSOr Ozeri     r = rbd_get_size(image, &effective_size);
44442e4ac9eSOr Ozeri     if (r < 0) {
44542e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot get effective image size");
44642e4ac9eSOr Ozeri         return r;
44742e4ac9eSOr Ozeri     }
44842e4ac9eSOr Ozeri 
44942e4ac9eSOr Ozeri     r = rbd_resize(image, raw_size + (raw_size - effective_size));
45042e4ac9eSOr Ozeri     if (r < 0) {
45142e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot resize image after format");
45242e4ac9eSOr Ozeri         return r;
45342e4ac9eSOr Ozeri     }
45442e4ac9eSOr Ozeri 
45542e4ac9eSOr Ozeri     return 0;
45642e4ac9eSOr Ozeri }
45742e4ac9eSOr Ozeri 
45842e4ac9eSOr Ozeri static int qemu_rbd_encryption_load(rbd_image_t image,
45942e4ac9eSOr Ozeri                                     RbdEncryptionOptions *encrypt,
46042e4ac9eSOr Ozeri                                     Error **errp)
46142e4ac9eSOr Ozeri {
46242e4ac9eSOr Ozeri     int r = 0;
46342e4ac9eSOr Ozeri     g_autofree char *passphrase = NULL;
46442e4ac9eSOr Ozeri     size_t passphrase_len;
46542e4ac9eSOr Ozeri     rbd_encryption_luks1_format_options_t luks_opts;
46642e4ac9eSOr Ozeri     rbd_encryption_luks2_format_options_t luks2_opts;
46742e4ac9eSOr Ozeri     rbd_encryption_format_t format;
46842e4ac9eSOr Ozeri     rbd_encryption_options_t opts;
46942e4ac9eSOr Ozeri     size_t opts_size;
47042e4ac9eSOr Ozeri 
47142e4ac9eSOr Ozeri     switch (encrypt->format) {
47242e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
47342e4ac9eSOr Ozeri             memset(&luks_opts, 0, sizeof(luks_opts));
47442e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS1;
47542e4ac9eSOr Ozeri             opts = &luks_opts;
47642e4ac9eSOr Ozeri             opts_size = sizeof(luks_opts);
47742e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_options(
47842e4ac9eSOr Ozeri                     qapi_RbdEncryptionOptionsLUKS_base(&encrypt->u.luks),
47942e4ac9eSOr Ozeri                     &passphrase, &passphrase_len, errp);
48042e4ac9eSOr Ozeri             if (r < 0) {
48142e4ac9eSOr Ozeri                 return r;
48242e4ac9eSOr Ozeri             }
48342e4ac9eSOr Ozeri             luks_opts.passphrase = passphrase;
48442e4ac9eSOr Ozeri             luks_opts.passphrase_size = passphrase_len;
48542e4ac9eSOr Ozeri             break;
48642e4ac9eSOr Ozeri         }
48742e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
48842e4ac9eSOr Ozeri             memset(&luks2_opts, 0, sizeof(luks2_opts));
48942e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS2;
49042e4ac9eSOr Ozeri             opts = &luks2_opts;
49142e4ac9eSOr Ozeri             opts_size = sizeof(luks2_opts);
49242e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_options(
49342e4ac9eSOr Ozeri                     qapi_RbdEncryptionOptionsLUKS2_base(&encrypt->u.luks2),
49442e4ac9eSOr Ozeri                     &passphrase, &passphrase_len, errp);
49542e4ac9eSOr Ozeri             if (r < 0) {
49642e4ac9eSOr Ozeri                 return r;
49742e4ac9eSOr Ozeri             }
49842e4ac9eSOr Ozeri             luks2_opts.passphrase = passphrase;
49942e4ac9eSOr Ozeri             luks2_opts.passphrase_size = passphrase_len;
50042e4ac9eSOr Ozeri             break;
50142e4ac9eSOr Ozeri         }
50242e4ac9eSOr Ozeri         default: {
50342e4ac9eSOr Ozeri             r = -ENOTSUP;
50442e4ac9eSOr Ozeri             error_setg_errno(
50542e4ac9eSOr Ozeri                     errp, -r, "unknown image encryption format: %u",
50642e4ac9eSOr Ozeri                     encrypt->format);
50742e4ac9eSOr Ozeri             return r;
50842e4ac9eSOr Ozeri         }
50942e4ac9eSOr Ozeri     }
51042e4ac9eSOr Ozeri 
51142e4ac9eSOr Ozeri     r = rbd_encryption_load(image, format, opts, opts_size);
51242e4ac9eSOr Ozeri     if (r < 0) {
51342e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "encryption load fail");
51442e4ac9eSOr Ozeri         return r;
51542e4ac9eSOr Ozeri     }
51642e4ac9eSOr Ozeri 
51742e4ac9eSOr Ozeri     return 0;
51842e4ac9eSOr Ozeri }
51942e4ac9eSOr Ozeri #endif
52042e4ac9eSOr Ozeri 
521d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */
5221bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
5231bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
5241bebea37SKevin Wolf                               Error **errp)
5251bebea37SKevin Wolf {
5261bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
5271bebea37SKevin Wolf     rados_t cluster;
5281bebea37SKevin Wolf     rados_ioctx_t io_ctx;
5291bebea37SKevin Wolf     int obj_order = 0;
5301bebea37SKevin Wolf     int ret;
5311bebea37SKevin Wolf 
5321bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
5331bebea37SKevin Wolf     if (opts->location->has_snapshot) {
5341bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
5351bebea37SKevin Wolf         return -EINVAL;
5361bebea37SKevin Wolf     }
5371bebea37SKevin Wolf 
53842e4ac9eSOr Ozeri #ifndef LIBRBD_SUPPORTS_ENCRYPTION
53942e4ac9eSOr Ozeri     if (opts->has_encrypt) {
54042e4ac9eSOr Ozeri         error_setg(errp, "RBD library does not support image encryption");
54142e4ac9eSOr Ozeri         return -ENOTSUP;
54242e4ac9eSOr Ozeri     }
54342e4ac9eSOr Ozeri #endif
54442e4ac9eSOr Ozeri 
5451bebea37SKevin Wolf     if (opts->has_cluster_size) {
5461bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
5471bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
5481bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
5491bebea37SKevin Wolf             return -EINVAL;
5501bebea37SKevin Wolf         }
5511bebea37SKevin Wolf         if (objsize < 4096) {
5521bebea37SKevin Wolf             error_setg(errp, "obj size too small");
5531bebea37SKevin Wolf             return -EINVAL;
5541bebea37SKevin Wolf         }
5551bebea37SKevin Wolf         obj_order = ctz32(objsize);
5561bebea37SKevin Wolf     }
5571bebea37SKevin Wolf 
558aa045c2dSKevin Wolf     ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
559aa045c2dSKevin Wolf                            password_secret, errp);
5601bebea37SKevin Wolf     if (ret < 0) {
5611bebea37SKevin Wolf         return ret;
5621bebea37SKevin Wolf     }
5631bebea37SKevin Wolf 
5641bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
5651bebea37SKevin Wolf     if (ret < 0) {
5661bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
567aa045c2dSKevin Wolf         goto out;
5681bebea37SKevin Wolf     }
5691bebea37SKevin Wolf 
57042e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
57142e4ac9eSOr Ozeri     if (opts->has_encrypt) {
57242e4ac9eSOr Ozeri         rbd_image_t image;
57342e4ac9eSOr Ozeri 
57442e4ac9eSOr Ozeri         ret = rbd_open(io_ctx, opts->location->image, &image, NULL);
57542e4ac9eSOr Ozeri         if (ret < 0) {
57642e4ac9eSOr Ozeri             error_setg_errno(errp, -ret,
57742e4ac9eSOr Ozeri                              "error opening image '%s' for encryption format",
57842e4ac9eSOr Ozeri                              opts->location->image);
57942e4ac9eSOr Ozeri             goto out;
58042e4ac9eSOr Ozeri         }
58142e4ac9eSOr Ozeri 
58242e4ac9eSOr Ozeri         ret = qemu_rbd_encryption_format(image, opts->encrypt, errp);
58342e4ac9eSOr Ozeri         rbd_close(image);
58442e4ac9eSOr Ozeri         if (ret < 0) {
58542e4ac9eSOr Ozeri             /* encryption format fail, try removing the image */
58642e4ac9eSOr Ozeri             rbd_remove(io_ctx, opts->location->image);
58742e4ac9eSOr Ozeri             goto out;
58842e4ac9eSOr Ozeri         }
58942e4ac9eSOr Ozeri     }
59042e4ac9eSOr Ozeri #endif
59142e4ac9eSOr Ozeri 
5921bebea37SKevin Wolf     ret = 0;
593aa045c2dSKevin Wolf out:
594aa045c2dSKevin Wolf     rados_ioctx_destroy(io_ctx);
5951bebea37SKevin Wolf     rados_shutdown(cluster);
5961bebea37SKevin Wolf     return ret;
5971bebea37SKevin Wolf }
5981bebea37SKevin Wolf 
5991bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
6001bebea37SKevin Wolf {
6011bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
6021bebea37SKevin Wolf }
6031bebea37SKevin Wolf 
60442e4ac9eSOr Ozeri static int qemu_rbd_extract_encryption_create_options(
60542e4ac9eSOr Ozeri         QemuOpts *opts,
60642e4ac9eSOr Ozeri         RbdEncryptionCreateOptions **spec,
60742e4ac9eSOr Ozeri         Error **errp)
60842e4ac9eSOr Ozeri {
60942e4ac9eSOr Ozeri     QDict *opts_qdict;
61042e4ac9eSOr Ozeri     QDict *encrypt_qdict;
61142e4ac9eSOr Ozeri     Visitor *v;
61242e4ac9eSOr Ozeri     int ret = 0;
61342e4ac9eSOr Ozeri 
61442e4ac9eSOr Ozeri     opts_qdict = qemu_opts_to_qdict(opts, NULL);
61542e4ac9eSOr Ozeri     qdict_extract_subqdict(opts_qdict, &encrypt_qdict, "encrypt.");
61642e4ac9eSOr Ozeri     qobject_unref(opts_qdict);
61742e4ac9eSOr Ozeri     if (!qdict_size(encrypt_qdict)) {
61842e4ac9eSOr Ozeri         *spec = NULL;
61942e4ac9eSOr Ozeri         goto exit;
62042e4ac9eSOr Ozeri     }
62142e4ac9eSOr Ozeri 
62242e4ac9eSOr Ozeri     /* Convert options into a QAPI object */
62342e4ac9eSOr Ozeri     v = qobject_input_visitor_new_flat_confused(encrypt_qdict, errp);
62442e4ac9eSOr Ozeri     if (!v) {
62542e4ac9eSOr Ozeri         ret = -EINVAL;
62642e4ac9eSOr Ozeri         goto exit;
62742e4ac9eSOr Ozeri     }
62842e4ac9eSOr Ozeri 
62942e4ac9eSOr Ozeri     visit_type_RbdEncryptionCreateOptions(v, NULL, spec, errp);
63042e4ac9eSOr Ozeri     visit_free(v);
63142e4ac9eSOr Ozeri     if (!*spec) {
63242e4ac9eSOr Ozeri         ret = -EINVAL;
63342e4ac9eSOr Ozeri         goto exit;
63442e4ac9eSOr Ozeri     }
63542e4ac9eSOr Ozeri 
63642e4ac9eSOr Ozeri exit:
63742e4ac9eSOr Ozeri     qobject_unref(encrypt_qdict);
63842e4ac9eSOr Ozeri     return ret;
63942e4ac9eSOr Ozeri }
64042e4ac9eSOr Ozeri 
641b92902dfSMaxim Levitsky static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv,
642b92902dfSMaxim Levitsky                                                 const char *filename,
643efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
644efc75e2aSStefan Hajnoczi                                                 Error **errp)
645f27aaf4bSChristian Brunner {
6461bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
6471bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
6481bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
64942e4ac9eSOr Ozeri     RbdEncryptionCreateOptions *encrypt = NULL;
650d61563b2SMarkus Armbruster     Error *local_err = NULL;
6511bebea37SKevin Wolf     const char *keypairs, *password_secret;
652c7cacb3eSJeff Cody     QDict *options = NULL;
653c7cacb3eSJeff Cody     int ret = 0;
654f27aaf4bSChristian Brunner 
6551bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
6561bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
6571bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
6581bebea37SKevin Wolf 
6591bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
6601bebea37SKevin Wolf 
6611bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
66260390a21SDaniel P. Berrange 
663f27aaf4bSChristian Brunner     /* Read out options */
6641bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
665c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
6661bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
6671bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
6681bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
669f27aaf4bSChristian Brunner 
670c7cacb3eSJeff Cody     options = qdict_new();
671c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
672c7cacb3eSJeff Cody     if (local_err) {
673c7cacb3eSJeff Cody         ret = -EINVAL;
674c7cacb3eSJeff Cody         error_propagate(errp, local_err);
675c7cacb3eSJeff Cody         goto exit;
676c7cacb3eSJeff Cody     }
677c7cacb3eSJeff Cody 
67842e4ac9eSOr Ozeri     ret = qemu_rbd_extract_encryption_create_options(opts, &encrypt, errp);
67942e4ac9eSOr Ozeri     if (ret < 0) {
68042e4ac9eSOr Ozeri         goto exit;
68142e4ac9eSOr Ozeri     }
68242e4ac9eSOr Ozeri     rbd_opts->encrypt     = encrypt;
68342e4ac9eSOr Ozeri     rbd_opts->has_encrypt = !!encrypt;
68442e4ac9eSOr Ozeri 
685129c7d1cSMarkus Armbruster     /*
686129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
687129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
688129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
689129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
690129c7d1cSMarkus Armbruster      */
6911bebea37SKevin Wolf     loc = rbd_opts->location;
6921bebea37SKevin Wolf     loc->pool        = g_strdup(qdict_get_try_str(options, "pool"));
6931bebea37SKevin Wolf     loc->conf        = g_strdup(qdict_get_try_str(options, "conf"));
6941bebea37SKevin Wolf     loc->has_conf    = !!loc->conf;
6951bebea37SKevin Wolf     loc->user        = g_strdup(qdict_get_try_str(options, "user"));
6961bebea37SKevin Wolf     loc->has_user    = !!loc->user;
69719ae9ae0SFlorian Florensa     loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace"));
698b084b420SStefano Garzarella     loc->has_q_namespace = !!loc->q_namespace;
6991bebea37SKevin Wolf     loc->image       = g_strdup(qdict_get_try_str(options, "image"));
70007846397SMarkus Armbruster     keypairs         = qdict_get_try_str(options, "=keyvalue-pairs");
701c7cacb3eSJeff Cody 
7021bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
70387cd3d20SVikhyat Umrao     if (ret < 0) {
704c7cacb3eSJeff Cody         goto exit;
705f27aaf4bSChristian Brunner     }
706f27aaf4bSChristian Brunner 
707c7cacb3eSJeff Cody exit:
708cb3e7f08SMarc-André Lureau     qobject_unref(options);
7091bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
710f27aaf4bSChristian Brunner     return ret;
711f27aaf4bSChristian Brunner }
712f27aaf4bSChristian Brunner 
7134bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
7140a55679bSJeff Cody {
7154bfb2741SKevin Wolf     const char **vals;
7162836284dSMarkus Armbruster     const char *host, *port;
7172836284dSMarkus Armbruster     char *rados_str;
7184bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
7194bfb2741SKevin Wolf     int i, cnt;
7200a55679bSJeff Cody 
7214bfb2741SKevin Wolf     if (!opts->has_server) {
7224bfb2741SKevin Wolf         return NULL;
7230a55679bSJeff Cody     }
7244bfb2741SKevin Wolf 
7254bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
7264bfb2741SKevin Wolf         cnt++;
7270a55679bSJeff Cody     }
7280a55679bSJeff Cody 
7294bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
7304bfb2741SKevin Wolf 
7314bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
7324bfb2741SKevin Wolf         host = p->value->host;
7334bfb2741SKevin Wolf         port = p->value->port;
7344bfb2741SKevin Wolf 
7350a55679bSJeff Cody         if (strchr(host, ':')) {
7364bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
7370a55679bSJeff Cody         } else {
7384bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
7390a55679bSJeff Cody         }
7400a55679bSJeff Cody     }
7412836284dSMarkus Armbruster     vals[i] = NULL;
7420a55679bSJeff Cody 
7432836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
7442836284dSMarkus Armbruster     g_strfreev((char **)vals);
7450a55679bSJeff Cody     return rados_str;
7460a55679bSJeff Cody }
7470a55679bSJeff Cody 
7483d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
7494bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
7504ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
7514ff45049SKevin Wolf                             Error **errp)
752f27aaf4bSChristian Brunner {
7530a55679bSJeff Cody     char *mon_host = NULL;
7543d9136f9SKevin Wolf     Error *local_err = NULL;
755f27aaf4bSChristian Brunner     int r;
756f27aaf4bSChristian Brunner 
757d083f954SMarkus Armbruster     if (secretid) {
758d083f954SMarkus Armbruster         if (opts->key_secret) {
759d083f954SMarkus Armbruster             error_setg(errp,
760d083f954SMarkus Armbruster                        "Legacy 'password-secret' clashes with 'key-secret'");
761d083f954SMarkus Armbruster             return -EINVAL;
762d083f954SMarkus Armbruster         }
763d083f954SMarkus Armbruster         opts->key_secret = g_strdup(secretid);
764d083f954SMarkus Armbruster         opts->has_key_secret = true;
765d083f954SMarkus Armbruster     }
766d083f954SMarkus Armbruster 
7674bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
76884d18f06SMarkus Armbruster     if (local_err) {
769d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
7702836284dSMarkus Armbruster         r = -EINVAL;
771c1c1f6cfSStefano Garzarella         goto out;
772a9ccedc3SKevin Wolf     }
773a9ccedc3SKevin Wolf 
7744bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
775ad32e9c0SJosh Durgin     if (r < 0) {
77687cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
777c1c1f6cfSStefano Garzarella         goto out;
778f27aaf4bSChristian Brunner     }
779f27aaf4bSChristian Brunner 
780c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
7814bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
7824bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
7834bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
784e34d8f29SJosh Durgin         goto failed_shutdown;
785e34d8f29SJosh Durgin     }
78699a3c89dSJosh Durgin 
7873d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
78899a3c89dSJosh Durgin     if (r < 0) {
78999a3c89dSJosh Durgin         goto failed_shutdown;
79099a3c89dSJosh Durgin     }
79199a3c89dSJosh Durgin 
7920a55679bSJeff Cody     if (mon_host) {
7933d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
7940a55679bSJeff Cody         if (r < 0) {
7950a55679bSJeff Cody             goto failed_shutdown;
7960a55679bSJeff Cody         }
7970a55679bSJeff Cody     }
7980a55679bSJeff Cody 
799d083f954SMarkus Armbruster     r = qemu_rbd_set_auth(*cluster, opts, errp);
800d083f954SMarkus Armbruster     if (r < 0) {
80160390a21SDaniel P. Berrange         goto failed_shutdown;
80260390a21SDaniel P. Berrange     }
80360390a21SDaniel P. Berrange 
804b11f38fcSJosh Durgin     /*
805b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
806b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
807b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
808b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
809b11f38fcSJosh Durgin      * be set up, fall back to no caching.
810b11f38fcSJosh Durgin      */
8113d9136f9SKevin Wolf     if (cache) {
8123d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
813b11f38fcSJosh Durgin     } else {
8143d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
815b11f38fcSJosh Durgin     }
816b11f38fcSJosh Durgin 
8173d9136f9SKevin Wolf     r = rados_connect(*cluster);
818ad32e9c0SJosh Durgin     if (r < 0) {
81987cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
820eb93d5d9SSage Weil         goto failed_shutdown;
821ad32e9c0SJosh Durgin     }
822ad32e9c0SJosh Durgin 
8234bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
824ad32e9c0SJosh Durgin     if (r < 0) {
8254bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
826eb93d5d9SSage Weil         goto failed_shutdown;
827ad32e9c0SJosh Durgin     }
82819ae9ae0SFlorian Florensa     /*
82919ae9ae0SFlorian Florensa      * Set the namespace after opening the io context on the pool,
83019ae9ae0SFlorian Florensa      * if nspace == NULL or if nspace == "", it is just as we did nothing
83119ae9ae0SFlorian Florensa      */
83219ae9ae0SFlorian Florensa     rados_ioctx_set_namespace(*io_ctx, opts->q_namespace);
833ad32e9c0SJosh Durgin 
834c1c1f6cfSStefano Garzarella     r = 0;
835c1c1f6cfSStefano Garzarella     goto out;
8363d9136f9SKevin Wolf 
8373d9136f9SKevin Wolf failed_shutdown:
8383d9136f9SKevin Wolf     rados_shutdown(*cluster);
839c1c1f6cfSStefano Garzarella out:
8403d9136f9SKevin Wolf     g_free(mon_host);
8413d9136f9SKevin Wolf     return r;
8423d9136f9SKevin Wolf }
8433d9136f9SKevin Wolf 
844f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
845f24b03b5SJeff Cody                                     Error **errp)
846f24b03b5SJeff Cody {
847f24b03b5SJeff Cody     Visitor *v;
848f24b03b5SJeff Cody 
849f24b03b5SJeff Cody     /* Convert the remaining options into a QAPI object */
850f24b03b5SJeff Cody     v = qobject_input_visitor_new_flat_confused(options, errp);
851f24b03b5SJeff Cody     if (!v) {
852f24b03b5SJeff Cody         return -EINVAL;
853f24b03b5SJeff Cody     }
854f24b03b5SJeff Cody 
855b11a093cSMarkus Armbruster     visit_type_BlockdevOptionsRbd(v, NULL, opts, errp);
856f24b03b5SJeff Cody     visit_free(v);
857b11a093cSMarkus Armbruster     if (!opts) {
858f24b03b5SJeff Cody         return -EINVAL;
859f24b03b5SJeff Cody     }
860f24b03b5SJeff Cody 
861f24b03b5SJeff Cody     return 0;
862f24b03b5SJeff Cody }
863f24b03b5SJeff Cody 
864084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options,
865084d1d13SJeff Cody                                            BlockdevOptionsRbd **opts,
866084d1d13SJeff Cody                                            char **keypairs)
867084d1d13SJeff Cody {
868084d1d13SJeff Cody     char *filename;
869084d1d13SJeff Cody     int r;
870084d1d13SJeff Cody 
871084d1d13SJeff Cody     filename = g_strdup(qdict_get_try_str(options, "filename"));
872084d1d13SJeff Cody     if (!filename) {
873084d1d13SJeff Cody         return -EINVAL;
874084d1d13SJeff Cody     }
875084d1d13SJeff Cody     qdict_del(options, "filename");
876084d1d13SJeff Cody 
877084d1d13SJeff Cody     qemu_rbd_parse_filename(filename, options, NULL);
878084d1d13SJeff Cody 
879084d1d13SJeff Cody     /* keypairs freed by caller */
880084d1d13SJeff Cody     *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
881084d1d13SJeff Cody     if (*keypairs) {
882084d1d13SJeff Cody         qdict_del(options, "=keyvalue-pairs");
883084d1d13SJeff Cody     }
884084d1d13SJeff Cody 
885084d1d13SJeff Cody     r = qemu_rbd_convert_options(options, opts, NULL);
886084d1d13SJeff Cody 
887084d1d13SJeff Cody     g_free(filename);
888084d1d13SJeff Cody     return r;
889084d1d13SJeff Cody }
890084d1d13SJeff Cody 
8913d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
8923d9136f9SKevin Wolf                          Error **errp)
8933d9136f9SKevin Wolf {
8943d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
8954bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
896bfb15b4bSJeff Cody     const QDictEntry *e;
8973d9136f9SKevin Wolf     Error *local_err = NULL;
8984ff45049SKevin Wolf     char *keypairs, *secretid;
899832a93dcSPeter Lieven     rbd_image_info_t info;
9003d9136f9SKevin Wolf     int r;
9013d9136f9SKevin Wolf 
9024ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
9034ff45049SKevin Wolf     if (keypairs) {
9044ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
9054ff45049SKevin Wolf     }
9064ff45049SKevin Wolf 
9074ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
9084ff45049SKevin Wolf     if (secretid) {
9094ff45049SKevin Wolf         qdict_del(options, "password-secret");
9104ff45049SKevin Wolf     }
9114ff45049SKevin Wolf 
912f24b03b5SJeff Cody     r = qemu_rbd_convert_options(options, &opts, &local_err);
9134bfb2741SKevin Wolf     if (local_err) {
914084d1d13SJeff Cody         /* If keypairs are present, that means some options are present in
915084d1d13SJeff Cody          * the modern option format.  Don't attempt to parse legacy option
916084d1d13SJeff Cody          * formats, as we won't support mixed usage. */
917084d1d13SJeff Cody         if (keypairs) {
9184bfb2741SKevin Wolf             error_propagate(errp, local_err);
9194bfb2741SKevin Wolf             goto out;
9204bfb2741SKevin Wolf         }
9214bfb2741SKevin Wolf 
922084d1d13SJeff Cody         /* If the initial attempt to convert and process the options failed,
923084d1d13SJeff Cody          * we may be attempting to open an image file that has the rbd options
924084d1d13SJeff Cody          * specified in the older format consisting of all key/value pairs
925084d1d13SJeff Cody          * encoded in the filename.  Go ahead and attempt to parse the
926084d1d13SJeff Cody          * filename, and see if we can pull out the required options. */
927084d1d13SJeff Cody         r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
928084d1d13SJeff Cody         if (r < 0) {
929084d1d13SJeff Cody             /* Propagate the original error, not the legacy parsing fallback
930084d1d13SJeff Cody              * error, as the latter was just a best-effort attempt. */
931084d1d13SJeff Cody             error_propagate(errp, local_err);
932084d1d13SJeff Cody             goto out;
933084d1d13SJeff Cody         }
934084d1d13SJeff Cody         /* Take care whenever deciding to actually deprecate; once this ability
935084d1d13SJeff Cody          * is removed, we will not be able to open any images with legacy-styled
936084d1d13SJeff Cody          * backing image strings. */
9375197f445SMarkus Armbruster         warn_report("RBD options encoded in the filename as keyvalue pairs "
938084d1d13SJeff Cody                     "is deprecated");
939084d1d13SJeff Cody     }
940084d1d13SJeff Cody 
941bfb15b4bSJeff Cody     /* Remove the processed options from the QDict (the visitor processes
942bfb15b4bSJeff Cody      * _all_ options in the QDict) */
943bfb15b4bSJeff Cody     while ((e = qdict_first(options))) {
944bfb15b4bSJeff Cody         qdict_del(options, e->key);
945bfb15b4bSJeff Cody     }
946bfb15b4bSJeff Cody 
947d41a5588SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
948d41a5588SKevin Wolf                          !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
9493d9136f9SKevin Wolf     if (r < 0) {
9504ff45049SKevin Wolf         goto out;
9513d9136f9SKevin Wolf     }
9523d9136f9SKevin Wolf 
953d41a5588SKevin Wolf     s->snap = g_strdup(opts->snapshot);
954d41a5588SKevin Wolf     s->image_name = g_strdup(opts->image);
955d41a5588SKevin Wolf 
956e2b8247aSJeff Cody     /* rbd_open is always r/w */
95780b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
958ad32e9c0SJosh Durgin     if (r < 0) {
95980b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
96080b61a27SJeff Cody                          s->image_name);
961eb93d5d9SSage Weil         goto failed_open;
962ad32e9c0SJosh Durgin     }
963ad32e9c0SJosh Durgin 
96442e4ac9eSOr Ozeri     if (opts->has_encrypt) {
96542e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
96642e4ac9eSOr Ozeri         r = qemu_rbd_encryption_load(s->image, opts->encrypt, errp);
96742e4ac9eSOr Ozeri         if (r < 0) {
96842e4ac9eSOr Ozeri             goto failed_post_open;
96942e4ac9eSOr Ozeri         }
97042e4ac9eSOr Ozeri #else
97142e4ac9eSOr Ozeri         r = -ENOTSUP;
97242e4ac9eSOr Ozeri         error_setg(errp, "RBD library does not support image encryption");
97342e4ac9eSOr Ozeri         goto failed_post_open;
97442e4ac9eSOr Ozeri #endif
97542e4ac9eSOr Ozeri     }
97642e4ac9eSOr Ozeri 
977832a93dcSPeter Lieven     r = rbd_stat(s->image, &info, sizeof(info));
978d24f8023SStefano Garzarella     if (r < 0) {
979832a93dcSPeter Lieven         error_setg_errno(errp, -r, "error getting image info from %s",
980d24f8023SStefano Garzarella                          s->image_name);
98142e4ac9eSOr Ozeri         goto failed_post_open;
982d24f8023SStefano Garzarella     }
983832a93dcSPeter Lieven     s->image_size = info.size;
984832a93dcSPeter Lieven     s->object_size = info.obj_size;
985d24f8023SStefano Garzarella 
986e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
987e2b8247aSJeff Cody      * leave as-is */
988e2b8247aSJeff Cody     if (s->snap != NULL) {
989eaa2410fSKevin Wolf         r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
990e2b8247aSJeff Cody         if (r < 0) {
99142e4ac9eSOr Ozeri             goto failed_post_open;
992e2b8247aSJeff Cody         }
993e2b8247aSJeff Cody     }
994f27aaf4bSChristian Brunner 
995c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
996c56ac27dSPeter Lieven     bs->supported_zero_flags = BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK;
997c56ac27dSPeter Lieven #endif
998c56ac27dSPeter Lieven 
9992f98910dSEric Blake     /* When extending regular files, we get zeros from the OS */
10002f98910dSEric Blake     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
10012f98910dSEric Blake 
10024ff45049SKevin Wolf     r = 0;
10034ff45049SKevin Wolf     goto out;
1004f27aaf4bSChristian Brunner 
100542e4ac9eSOr Ozeri failed_post_open:
100642e4ac9eSOr Ozeri     rbd_close(s->image);
1007eb93d5d9SSage Weil failed_open:
1008ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
1009eb93d5d9SSage Weil     g_free(s->snap);
101080b61a27SJeff Cody     g_free(s->image_name);
10113d9136f9SKevin Wolf     rados_shutdown(s->cluster);
10124ff45049SKevin Wolf out:
10134bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
10144ff45049SKevin Wolf     g_free(keypairs);
10154ff45049SKevin Wolf     g_free(secretid);
1016f27aaf4bSChristian Brunner     return r;
1017f27aaf4bSChristian Brunner }
1018f27aaf4bSChristian Brunner 
101956e7cf8dSJeff Cody 
102056e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
102156e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
102256e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
102356e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
102456e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
102556e7cf8dSJeff Cody {
102656e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
102756e7cf8dSJeff Cody     int ret = 0;
102856e7cf8dSJeff Cody 
102956e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
103056e7cf8dSJeff Cody         error_setg(errp,
103156e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
103256e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
103356e7cf8dSJeff Cody         ret = -EINVAL;
103456e7cf8dSJeff Cody     }
103556e7cf8dSJeff Cody 
103656e7cf8dSJeff Cody     return ret;
103756e7cf8dSJeff Cody }
103856e7cf8dSJeff Cody 
1039ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
1040f27aaf4bSChristian Brunner {
1041f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1042f27aaf4bSChristian Brunner 
1043ad32e9c0SJosh Durgin     rbd_close(s->image);
1044ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
10457267c094SAnthony Liguori     g_free(s->snap);
104680b61a27SJeff Cody     g_free(s->image_name);
1047ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
1048f27aaf4bSChristian Brunner }
1049f27aaf4bSChristian Brunner 
1050d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */
1051d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size)
1052d24f8023SStefano Garzarella {
1053d24f8023SStefano Garzarella     BDRVRBDState *s = bs->opaque;
1054d24f8023SStefano Garzarella     int r;
1055d24f8023SStefano Garzarella 
1056d24f8023SStefano Garzarella     r = rbd_resize(s->image, size);
1057d24f8023SStefano Garzarella     if (r < 0) {
1058d24f8023SStefano Garzarella         return r;
1059d24f8023SStefano Garzarella     }
1060d24f8023SStefano Garzarella 
1061d24f8023SStefano Garzarella     s->image_size = size;
1062d24f8023SStefano Garzarella 
1063d24f8023SStefano Garzarella     return 0;
1064d24f8023SStefano Garzarella }
1065d24f8023SStefano Garzarella 
1066c3e5fac5SPeter Lieven static void qemu_rbd_finish_bh(void *opaque)
1067f27aaf4bSChristian Brunner {
1068c3e5fac5SPeter Lieven     RBDTask *task = opaque;
106964cc845bSPeter Lieven     task->complete = true;
1070c3e5fac5SPeter Lieven     aio_co_wake(task->co);
1071ad32e9c0SJosh Durgin }
1072ad32e9c0SJosh Durgin 
1073ad32e9c0SJosh Durgin /*
1074c3e5fac5SPeter Lieven  * This is the completion callback function for all rbd aio calls
1075c3e5fac5SPeter Lieven  * started from qemu_rbd_start_co().
1076ad32e9c0SJosh Durgin  *
1077ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
1078ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
1079e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
1080c3e5fac5SPeter Lieven  * from qemu_rbd_finish_bh() which runs in a qemu context.
1081ad32e9c0SJosh Durgin  */
1082c3e5fac5SPeter Lieven static void qemu_rbd_completion_cb(rbd_completion_t c, RBDTask *task)
1083ad32e9c0SJosh Durgin {
1084c3e5fac5SPeter Lieven     task->ret = rbd_aio_get_return_value(c);
1085ad32e9c0SJosh Durgin     rbd_aio_release(c);
1086c3e5fac5SPeter Lieven     aio_bh_schedule_oneshot(bdrv_get_aio_context(task->bs),
1087c3e5fac5SPeter Lieven                             qemu_rbd_finish_bh, task);
1088473c7f02SStefan Priebe }
1089f27aaf4bSChristian Brunner 
1090c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_start_co(BlockDriverState *bs,
1091c3e5fac5SPeter Lieven                                           uint64_t offset,
1092c3e5fac5SPeter Lieven                                           uint64_t bytes,
1093f27aaf4bSChristian Brunner                                           QEMUIOVector *qiov,
1094c3e5fac5SPeter Lieven                                           int flags,
1095787f3133SJosh Durgin                                           RBDAIOCmd cmd)
1096f27aaf4bSChristian Brunner {
1097c3e5fac5SPeter Lieven     BDRVRBDState *s = bs->opaque;
1098c3e5fac5SPeter Lieven     RBDTask task = { .bs = bs, .co = qemu_coroutine_self() };
1099ad32e9c0SJosh Durgin     rbd_completion_t c;
110051a13528SJosh Durgin     int r;
1101f27aaf4bSChristian Brunner 
1102c3e5fac5SPeter Lieven     assert(!qiov || qiov->size == bytes);
1103f27aaf4bSChristian Brunner 
1104c3e5fac5SPeter Lieven     r = rbd_aio_create_completion(&task,
1105c3e5fac5SPeter Lieven                                   (rbd_callback_t) qemu_rbd_completion_cb, &c);
110651a13528SJosh Durgin     if (r < 0) {
1107c3e5fac5SPeter Lieven         return r;
110851a13528SJosh Durgin     }
1109f27aaf4bSChristian Brunner 
1110787f3133SJosh Durgin     switch (cmd) {
1111787f3133SJosh Durgin     case RBD_AIO_READ:
1112c3e5fac5SPeter Lieven         r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, offset, c);
1113c3e5fac5SPeter Lieven         break;
1114c3e5fac5SPeter Lieven     case RBD_AIO_WRITE:
1115c3e5fac5SPeter Lieven         r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, offset, c);
1116787f3133SJosh Durgin         break;
1117787f3133SJosh Durgin     case RBD_AIO_DISCARD:
1118c3e5fac5SPeter Lieven         r = rbd_aio_discard(s->image, offset, bytes, c);
1119787f3133SJosh Durgin         break;
1120dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
112148672ac0SPeter Lieven         r = rbd_aio_flush(s->image, c);
1122dc7588c1SJosh Durgin         break;
1123c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1124c56ac27dSPeter Lieven     case RBD_AIO_WRITE_ZEROES: {
1125c56ac27dSPeter Lieven         int zero_flags = 0;
1126c56ac27dSPeter Lieven #ifdef RBD_WRITE_ZEROES_FLAG_THICK_PROVISION
1127c56ac27dSPeter Lieven         if (!(flags & BDRV_REQ_MAY_UNMAP)) {
1128c56ac27dSPeter Lieven             zero_flags = RBD_WRITE_ZEROES_FLAG_THICK_PROVISION;
1129c56ac27dSPeter Lieven         }
1130c56ac27dSPeter Lieven #endif
1131c56ac27dSPeter Lieven         r = rbd_aio_write_zeroes(s->image, offset, bytes, c, zero_flags, 0);
1132c56ac27dSPeter Lieven         break;
1133c56ac27dSPeter Lieven     }
1134c56ac27dSPeter Lieven #endif
1135787f3133SJosh Durgin     default:
1136787f3133SJosh Durgin         r = -EINVAL;
113751a13528SJosh Durgin     }
113851a13528SJosh Durgin 
113951a13528SJosh Durgin     if (r < 0) {
1140c3e5fac5SPeter Lieven         error_report("rbd request failed early: cmd %d offset %" PRIu64
1141c3e5fac5SPeter Lieven                      " bytes %" PRIu64 " flags %d r %d (%s)", cmd, offset,
1142c3e5fac5SPeter Lieven                      bytes, flags, r, strerror(-r));
1143405a2764SKevin Wolf         rbd_aio_release(c);
1144c3e5fac5SPeter Lieven         return r;
1145f27aaf4bSChristian Brunner     }
1146f27aaf4bSChristian Brunner 
1147c3e5fac5SPeter Lieven     while (!task.complete) {
1148c3e5fac5SPeter Lieven         qemu_coroutine_yield();
1149f27aaf4bSChristian Brunner     }
1150f27aaf4bSChristian Brunner 
1151c3e5fac5SPeter Lieven     if (task.ret < 0) {
1152c3e5fac5SPeter Lieven         error_report("rbd request failed: cmd %d offset %" PRIu64 " bytes %"
1153c3e5fac5SPeter Lieven                      PRIu64 " flags %d task.ret %" PRIi64 " (%s)", cmd, offset,
1154c3e5fac5SPeter Lieven                      bytes, flags, task.ret, strerror(-task.ret));
1155c3e5fac5SPeter Lieven         return task.ret;
1156f27aaf4bSChristian Brunner     }
1157f27aaf4bSChristian Brunner 
1158c3e5fac5SPeter Lieven     /* zero pad short reads */
1159c3e5fac5SPeter Lieven     if (cmd == RBD_AIO_READ && task.ret < qiov->size) {
1160c3e5fac5SPeter Lieven         qemu_iovec_memset(qiov, task.ret, 0, qiov->size - task.ret);
1161c3e5fac5SPeter Lieven     }
1162c3e5fac5SPeter Lieven 
1163c3e5fac5SPeter Lieven     return 0;
1164c3e5fac5SPeter Lieven }
1165c3e5fac5SPeter Lieven 
1166c3e5fac5SPeter Lieven static int
1167f7ef38ddSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_preadv(BlockDriverState *bs, int64_t offset,
1168f7ef38ddSVladimir Sementsov-Ogievskiy                                 int64_t bytes, QEMUIOVector *qiov,
1169f7ef38ddSVladimir Sementsov-Ogievskiy                                 BdrvRequestFlags flags)
1170dc7588c1SJosh Durgin {
1171c3e5fac5SPeter Lieven     return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_READ);
1172c3e5fac5SPeter Lieven }
1173c3e5fac5SPeter Lieven 
1174c3e5fac5SPeter Lieven static int
1175*e75abedaSVladimir Sementsov-Ogievskiy coroutine_fn qemu_rbd_co_pwritev(BlockDriverState *bs, int64_t offset,
1176*e75abedaSVladimir Sementsov-Ogievskiy                                  int64_t bytes, QEMUIOVector *qiov,
1177*e75abedaSVladimir Sementsov-Ogievskiy                                  BdrvRequestFlags flags)
1178c3e5fac5SPeter Lieven {
1179c3e5fac5SPeter Lieven     BDRVRBDState *s = bs->opaque;
1180c3e5fac5SPeter Lieven     /*
1181c3e5fac5SPeter Lieven      * RBD APIs don't allow us to write more than actual size, so in order
1182c3e5fac5SPeter Lieven      * to support growing images, we resize the image before write
1183c3e5fac5SPeter Lieven      * operations that exceed the current size.
1184c3e5fac5SPeter Lieven      */
1185c3e5fac5SPeter Lieven     if (offset + bytes > s->image_size) {
1186c3e5fac5SPeter Lieven         int r = qemu_rbd_resize(bs, offset + bytes);
1187c3e5fac5SPeter Lieven         if (r < 0) {
1188c3e5fac5SPeter Lieven             return r;
1189c3e5fac5SPeter Lieven         }
1190c3e5fac5SPeter Lieven     }
1191c3e5fac5SPeter Lieven     return qemu_rbd_start_co(bs, offset, bytes, qiov, flags, RBD_AIO_WRITE);
1192c3e5fac5SPeter Lieven }
1193c3e5fac5SPeter Lieven 
1194c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_flush(BlockDriverState *bs)
1195c3e5fac5SPeter Lieven {
1196c3e5fac5SPeter Lieven     return qemu_rbd_start_co(bs, 0, 0, NULL, 0, RBD_AIO_FLUSH);
1197c3e5fac5SPeter Lieven }
1198c3e5fac5SPeter Lieven 
1199c3e5fac5SPeter Lieven static int coroutine_fn qemu_rbd_co_pdiscard(BlockDriverState *bs,
1200c3e5fac5SPeter Lieven                                              int64_t offset, int count)
1201c3e5fac5SPeter Lieven {
1202c3e5fac5SPeter Lieven     return qemu_rbd_start_co(bs, offset, count, NULL, 0, RBD_AIO_DISCARD);
1203dc7588c1SJosh Durgin }
1204dc7588c1SJosh Durgin 
1205c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1206c56ac27dSPeter Lieven static int
1207c56ac27dSPeter Lieven coroutine_fn qemu_rbd_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
1208c56ac27dSPeter Lieven                                       int count, BdrvRequestFlags flags)
1209c56ac27dSPeter Lieven {
1210c56ac27dSPeter Lieven     return qemu_rbd_start_co(bs, offset, count, NULL, flags,
1211c56ac27dSPeter Lieven                              RBD_AIO_WRITE_ZEROES);
1212c56ac27dSPeter Lieven }
1213c56ac27dSPeter Lieven #endif
1214c56ac27dSPeter Lieven 
1215ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1216f27aaf4bSChristian Brunner {
1217f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1218832a93dcSPeter Lieven     bdi->cluster_size = s->object_size;
1219f27aaf4bSChristian Brunner     return 0;
1220f27aaf4bSChristian Brunner }
1221f27aaf4bSChristian Brunner 
122242e4ac9eSOr Ozeri static ImageInfoSpecific *qemu_rbd_get_specific_info(BlockDriverState *bs,
122342e4ac9eSOr Ozeri                                                      Error **errp)
122442e4ac9eSOr Ozeri {
122542e4ac9eSOr Ozeri     BDRVRBDState *s = bs->opaque;
122642e4ac9eSOr Ozeri     ImageInfoSpecific *spec_info;
122742e4ac9eSOr Ozeri     char buf[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {0};
122842e4ac9eSOr Ozeri     int r;
122942e4ac9eSOr Ozeri 
123042e4ac9eSOr Ozeri     if (s->image_size >= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) {
123142e4ac9eSOr Ozeri         r = rbd_read(s->image, 0,
123242e4ac9eSOr Ozeri                      RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN, buf);
123342e4ac9eSOr Ozeri         if (r < 0) {
123442e4ac9eSOr Ozeri             error_setg_errno(errp, -r, "cannot read image start for probe");
123542e4ac9eSOr Ozeri             return NULL;
123642e4ac9eSOr Ozeri         }
123742e4ac9eSOr Ozeri     }
123842e4ac9eSOr Ozeri 
123942e4ac9eSOr Ozeri     spec_info = g_new(ImageInfoSpecific, 1);
124042e4ac9eSOr Ozeri     *spec_info = (ImageInfoSpecific){
124142e4ac9eSOr Ozeri         .type  = IMAGE_INFO_SPECIFIC_KIND_RBD,
124242e4ac9eSOr Ozeri         .u.rbd.data = g_new0(ImageInfoSpecificRbd, 1),
124342e4ac9eSOr Ozeri     };
124442e4ac9eSOr Ozeri 
124542e4ac9eSOr Ozeri     if (memcmp(buf, rbd_luks_header_verification,
124642e4ac9eSOr Ozeri                RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
124742e4ac9eSOr Ozeri         spec_info->u.rbd.data->encryption_format =
124842e4ac9eSOr Ozeri                 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS;
124942e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = true;
125042e4ac9eSOr Ozeri     } else if (memcmp(buf, rbd_luks2_header_verification,
125142e4ac9eSOr Ozeri                RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
125242e4ac9eSOr Ozeri         spec_info->u.rbd.data->encryption_format =
125342e4ac9eSOr Ozeri                 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2;
125442e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = true;
125542e4ac9eSOr Ozeri     } else {
125642e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = false;
125742e4ac9eSOr Ozeri     }
125842e4ac9eSOr Ozeri 
125942e4ac9eSOr Ozeri     return spec_info;
126042e4ac9eSOr Ozeri }
126142e4ac9eSOr Ozeri 
1262ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1263f27aaf4bSChristian Brunner {
1264f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1265ad32e9c0SJosh Durgin     int r;
1266f27aaf4bSChristian Brunner 
12676d921418SPeter Lieven     r = rbd_get_size(s->image, &s->image_size);
1268ad32e9c0SJosh Durgin     if (r < 0) {
1269ad32e9c0SJosh Durgin         return r;
1270f27aaf4bSChristian Brunner     }
1271f27aaf4bSChristian Brunner 
12726d921418SPeter Lieven     return s->image_size;
1273ad32e9c0SJosh Durgin }
1274ad32e9c0SJosh Durgin 
1275061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1276061ca8a3SKevin Wolf                                              int64_t offset,
1277c80d8b06SMax Reitz                                              bool exact,
1278061ca8a3SKevin Wolf                                              PreallocMode prealloc,
127992b92799SKevin Wolf                                              BdrvRequestFlags flags,
1280061ca8a3SKevin Wolf                                              Error **errp)
128130cdc48cSJosh Durgin {
128230cdc48cSJosh Durgin     int r;
128330cdc48cSJosh Durgin 
12848243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
12858243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
1286977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
12878243ccb7SMax Reitz         return -ENOTSUP;
12888243ccb7SMax Reitz     }
12898243ccb7SMax Reitz 
1290d24f8023SStefano Garzarella     r = qemu_rbd_resize(bs, offset);
129130cdc48cSJosh Durgin     if (r < 0) {
1292f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
129330cdc48cSJosh Durgin         return r;
129430cdc48cSJosh Durgin     }
129530cdc48cSJosh Durgin 
129630cdc48cSJosh Durgin     return 0;
129730cdc48cSJosh Durgin }
129830cdc48cSJosh Durgin 
1299ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1300ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1301f27aaf4bSChristian Brunner {
1302f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1303f27aaf4bSChristian Brunner     int r;
1304f27aaf4bSChristian Brunner 
1305f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1306f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1307f27aaf4bSChristian Brunner     }
1308f27aaf4bSChristian Brunner 
1309f27aaf4bSChristian Brunner     /*
1310f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1311f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1312f27aaf4bSChristian Brunner      */
1313f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1314f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1315f27aaf4bSChristian Brunner         return -EINVAL;
1316f27aaf4bSChristian Brunner     }
1317f27aaf4bSChristian Brunner 
1318f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1319f27aaf4bSChristian Brunner         return -ERANGE;
1320f27aaf4bSChristian Brunner     }
1321f27aaf4bSChristian Brunner 
1322ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1323f27aaf4bSChristian Brunner     if (r < 0) {
1324ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1325f27aaf4bSChristian Brunner         return r;
1326f27aaf4bSChristian Brunner     }
1327f27aaf4bSChristian Brunner 
1328f27aaf4bSChristian Brunner     return 0;
1329f27aaf4bSChristian Brunner }
1330f27aaf4bSChristian Brunner 
1331bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1332a89d89d3SWenchao Xia                                 const char *snapshot_id,
1333a89d89d3SWenchao Xia                                 const char *snapshot_name,
1334a89d89d3SWenchao Xia                                 Error **errp)
1335bd603247SGregory Farnum {
1336bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1337bd603247SGregory Farnum     int r;
1338bd603247SGregory Farnum 
1339a89d89d3SWenchao Xia     if (!snapshot_name) {
1340a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1341a89d89d3SWenchao Xia         return -EINVAL;
1342a89d89d3SWenchao Xia     }
1343a89d89d3SWenchao Xia 
1344a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1345a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1346a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1347a89d89d3SWenchao Xia         error_setg(errp,
1348a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1349a89d89d3SWenchao Xia                    "equal to snapshot name");
1350a89d89d3SWenchao Xia         return -EINVAL;
1351a89d89d3SWenchao Xia     }
1352a89d89d3SWenchao Xia 
1353bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1354a89d89d3SWenchao Xia     if (r < 0) {
1355a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1356a89d89d3SWenchao Xia     }
1357bd603247SGregory Farnum     return r;
1358bd603247SGregory Farnum }
1359bd603247SGregory Farnum 
1360bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1361bd603247SGregory Farnum                                   const char *snapshot_name)
1362bd603247SGregory Farnum {
1363bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1364bd603247SGregory Farnum 
13659be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1366bd603247SGregory Farnum }
1367bd603247SGregory Farnum 
1368ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1369ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1370f27aaf4bSChristian Brunner {
1371f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1372f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1373ad32e9c0SJosh Durgin     int i, snap_count;
1374ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1375ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1376f27aaf4bSChristian Brunner 
1377ad32e9c0SJosh Durgin     do {
137802c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1379ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
13809e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
13817267c094SAnthony Liguori             g_free(snaps);
1382f27aaf4bSChristian Brunner         }
1383ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1384f27aaf4bSChristian Brunner 
1385ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1386b9c53290SJosh Durgin         goto done;
1387f27aaf4bSChristian Brunner     }
1388f27aaf4bSChristian Brunner 
13895839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1390f27aaf4bSChristian Brunner 
1391ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1392ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1393f27aaf4bSChristian Brunner 
1394f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1395f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1396f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1397f27aaf4bSChristian Brunner 
1398ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1399f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1400f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1401f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1402f27aaf4bSChristian Brunner     }
1403ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
14049e6337d0SStefan Hajnoczi     g_free(snaps);
1405ad32e9c0SJosh Durgin 
1406b9c53290SJosh Durgin  done:
1407f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1408f27aaf4bSChristian Brunner     return snap_count;
1409f27aaf4bSChristian Brunner }
1410f27aaf4bSChristian Brunner 
14112b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1412be217884SAdam Crume                                                       Error **errp)
1413be217884SAdam Crume {
1414be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1415be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1416be217884SAdam Crume     if (r < 0) {
1417be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1418be217884SAdam Crume     }
1419be217884SAdam Crume }
1420be217884SAdam Crume 
1421bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1422bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1423bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1424bd0cf596SChunyan Liu     .desc = {
1425f27aaf4bSChristian Brunner         {
1426f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1427bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1428f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1429f27aaf4bSChristian Brunner         },
1430f27aaf4bSChristian Brunner         {
1431f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1432bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1433f27aaf4bSChristian Brunner             .help = "RBD object size"
1434f27aaf4bSChristian Brunner         },
143560390a21SDaniel P. Berrange         {
143660390a21SDaniel P. Berrange             .name = "password-secret",
143760390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
143860390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
143960390a21SDaniel P. Berrange         },
144042e4ac9eSOr Ozeri         {
144142e4ac9eSOr Ozeri             .name = "encrypt.format",
144242e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
144342e4ac9eSOr Ozeri             .help = "Encrypt the image, format choices: 'luks', 'luks2'",
144442e4ac9eSOr Ozeri         },
144542e4ac9eSOr Ozeri         {
144642e4ac9eSOr Ozeri             .name = "encrypt.cipher-alg",
144742e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
144842e4ac9eSOr Ozeri             .help = "Name of encryption cipher algorithm"
144942e4ac9eSOr Ozeri                     " (allowed values: aes-128, aes-256)",
145042e4ac9eSOr Ozeri         },
145142e4ac9eSOr Ozeri         {
145242e4ac9eSOr Ozeri             .name = "encrypt.key-secret",
145342e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
145442e4ac9eSOr Ozeri             .help = "ID of secret providing LUKS passphrase",
145542e4ac9eSOr Ozeri         },
1456bd0cf596SChunyan Liu         { /* end of list */ }
1457bd0cf596SChunyan Liu     }
1458f27aaf4bSChristian Brunner };
1459f27aaf4bSChristian Brunner 
14602654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = {
14612654267cSMax Reitz     "pool",
14627bae7c80SStefano Garzarella     "namespace",
14632654267cSMax Reitz     "image",
14642654267cSMax Reitz     "conf",
14652654267cSMax Reitz     "snapshot",
14662654267cSMax Reitz     "user",
14672654267cSMax Reitz     "server.",
14682654267cSMax Reitz     "password-secret",
14692654267cSMax Reitz 
14702654267cSMax Reitz     NULL
14712654267cSMax Reitz };
14722654267cSMax Reitz 
1473f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1474f27aaf4bSChristian Brunner     .format_name            = "rbd",
1475f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1476c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1477ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1478ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
147956e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
14801bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1481efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
14823ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1483ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
148442e4ac9eSOr Ozeri     .bdrv_get_specific_info = qemu_rbd_get_specific_info,
1485bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1486ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
1487061ca8a3SKevin Wolf     .bdrv_co_truncate       = qemu_rbd_co_truncate,
1488f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1489f27aaf4bSChristian Brunner 
1490c3e5fac5SPeter Lieven     .bdrv_co_preadv         = qemu_rbd_co_preadv,
1491c3e5fac5SPeter Lieven     .bdrv_co_pwritev        = qemu_rbd_co_pwritev,
1492c3e5fac5SPeter Lieven     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1493c3e5fac5SPeter Lieven     .bdrv_co_pdiscard       = qemu_rbd_co_pdiscard,
1494c56ac27dSPeter Lieven #ifdef LIBRBD_SUPPORTS_WRITE_ZEROES
1495c56ac27dSPeter Lieven     .bdrv_co_pwrite_zeroes  = qemu_rbd_co_pwrite_zeroes,
1496c56ac27dSPeter Lieven #endif
1497787f3133SJosh Durgin 
1498ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1499bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1500ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1501bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
15022b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
15032654267cSMax Reitz 
15042654267cSMax Reitz     .strong_runtime_opts    = qemu_rbd_strong_runtime_opts,
1505f27aaf4bSChristian Brunner };
1506f27aaf4bSChristian Brunner 
1507f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1508f27aaf4bSChristian Brunner {
1509f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1510f27aaf4bSChristian Brunner }
1511f27aaf4bSChristian Brunner 
1512f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1513