xref: /openbmc/qemu/block/rbd.c (revision 6d9214189e22205d42c4ad1fae5af265f0a94dd3)
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,
78dc7588c1SJosh Durgin     RBD_AIO_FLUSH
79787f3133SJosh Durgin } RBDAIOCmd;
80787f3133SJosh Durgin 
81f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
827c84b1b8SMarkus Armbruster     BlockAIOCB common;
8308448d51SStefan Priebe     int64_t ret;
84f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
85787f3133SJosh Durgin     RBDAIOCmd cmd;
86f27aaf4bSChristian Brunner     int error;
87f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
88f27aaf4bSChristian Brunner } RBDAIOCB;
89f27aaf4bSChristian Brunner 
90f27aaf4bSChristian Brunner typedef struct RADOSCB {
91f27aaf4bSChristian Brunner     RBDAIOCB *acb;
92f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
93ad32e9c0SJosh Durgin     int64_t size;
9408448d51SStefan Priebe     int64_t ret;
95f27aaf4bSChristian Brunner } RADOSCB;
96f27aaf4bSChristian Brunner 
97f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
98ad32e9c0SJosh Durgin     rados_t cluster;
99ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
100ad32e9c0SJosh Durgin     rbd_image_t image;
10180b61a27SJeff Cody     char *image_name;
102ad32e9c0SJosh Durgin     char *snap;
10319ae9ae0SFlorian Florensa     char *namespace;
104d24f8023SStefano Garzarella     uint64_t image_size;
105832a93dcSPeter Lieven     uint64_t object_size;
106f27aaf4bSChristian Brunner } BDRVRBDState;
107f27aaf4bSChristian Brunner 
108aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
109aa045c2dSKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
110aa045c2dSKevin Wolf                             const char *keypairs, const char *secretid,
111aa045c2dSKevin Wolf                             Error **errp);
112aa045c2dSKevin Wolf 
1132b99cfceSConnor Kuehl static char *qemu_rbd_strchr(char *src, char delim)
1142b99cfceSConnor Kuehl {
1152b99cfceSConnor Kuehl     char *p;
1162b99cfceSConnor Kuehl 
1172b99cfceSConnor Kuehl     for (p = src; *p; ++p) {
1182b99cfceSConnor Kuehl         if (*p == delim) {
1192b99cfceSConnor Kuehl             return p;
1202b99cfceSConnor Kuehl         }
1212b99cfceSConnor Kuehl         if (*p == '\\' && p[1] != '\0') {
1222b99cfceSConnor Kuehl             ++p;
1232b99cfceSConnor Kuehl         }
1242b99cfceSConnor Kuehl     }
1252b99cfceSConnor Kuehl 
1262b99cfceSConnor Kuehl     return NULL;
1272b99cfceSConnor Kuehl }
1282b99cfceSConnor Kuehl 
1292b99cfceSConnor Kuehl 
130730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
131f27aaf4bSChristian Brunner {
132f27aaf4bSChristian Brunner     char *end;
133f27aaf4bSChristian Brunner 
134f27aaf4bSChristian Brunner     *p = NULL;
135f27aaf4bSChristian Brunner 
1362b99cfceSConnor Kuehl     end = qemu_rbd_strchr(src, delim);
1372b99cfceSConnor Kuehl     if (end) {
138f27aaf4bSChristian Brunner         *p = end + 1;
139f27aaf4bSChristian Brunner         *end = '\0';
140f27aaf4bSChristian Brunner     }
1417830f909SJeff Cody     return src;
142f27aaf4bSChristian Brunner }
143f27aaf4bSChristian Brunner 
14416a06b24SSage Weil static void qemu_rbd_unescape(char *src)
14516a06b24SSage Weil {
14616a06b24SSage Weil     char *p;
14716a06b24SSage Weil 
14816a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14916a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
15016a06b24SSage Weil             src++;
15116a06b24SSage Weil         }
15216a06b24SSage Weil         *p = *src;
15316a06b24SSage Weil     }
15416a06b24SSage Weil     *p = '\0';
15516a06b24SSage Weil }
15616a06b24SSage Weil 
157c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
158d61563b2SMarkus Armbruster                                     Error **errp)
159f27aaf4bSChristian Brunner {
160f27aaf4bSChristian Brunner     const char *start;
161e98c6961SEric Blake     char *p, *buf;
162e98c6961SEric Blake     QList *keypairs = NULL;
16319ae9ae0SFlorian Florensa     char *found_str, *image_name;
164f27aaf4bSChristian Brunner 
165f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
166d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
167c7cacb3eSJeff Cody         return;
168f27aaf4bSChristian Brunner     }
169f27aaf4bSChristian Brunner 
1707267c094SAnthony Liguori     buf = g_strdup(start);
171f27aaf4bSChristian Brunner     p = buf;
172f27aaf4bSChristian Brunner 
173730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1747830f909SJeff Cody     if (!p) {
1757830f909SJeff Cody         error_setg(errp, "Pool name is required");
1767830f909SJeff Cody         goto done;
1777830f909SJeff Cody     }
1787830f909SJeff Cody     qemu_rbd_unescape(found_str);
17946f5ac20SEric Blake     qdict_put_str(options, "pool", found_str);
180fab5cf59SJosh Durgin 
1812b99cfceSConnor Kuehl     if (qemu_rbd_strchr(p, '@')) {
18219ae9ae0SFlorian Florensa         image_name = qemu_rbd_next_tok(p, '@', &p);
1837830f909SJeff Cody 
184730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1857830f909SJeff Cody         qemu_rbd_unescape(found_str);
18646f5ac20SEric Blake         qdict_put_str(options, "snapshot", found_str);
1877830f909SJeff Cody     } else {
18819ae9ae0SFlorian Florensa         image_name = qemu_rbd_next_tok(p, ':', &p);
1897830f909SJeff Cody     }
19019ae9ae0SFlorian Florensa     /* Check for namespace in the image_name */
1912b99cfceSConnor Kuehl     if (qemu_rbd_strchr(image_name, '/')) {
19219ae9ae0SFlorian Florensa         found_str = qemu_rbd_next_tok(image_name, '/', &image_name);
19319ae9ae0SFlorian Florensa         qemu_rbd_unescape(found_str);
19419ae9ae0SFlorian Florensa         qdict_put_str(options, "namespace", found_str);
19519ae9ae0SFlorian Florensa     } else {
19619ae9ae0SFlorian Florensa         qdict_put_str(options, "namespace", "");
19719ae9ae0SFlorian Florensa     }
19819ae9ae0SFlorian Florensa     qemu_rbd_unescape(image_name);
19919ae9ae0SFlorian Florensa     qdict_put_str(options, "image", image_name);
2007830f909SJeff Cody     if (!p) {
201f27aaf4bSChristian Brunner         goto done;
202f27aaf4bSChristian Brunner     }
203f27aaf4bSChristian Brunner 
204c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
205c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
206c7cacb3eSJeff Cody     while (p) {
207c7cacb3eSJeff Cody         char *name, *value;
208730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
209c7cacb3eSJeff Cody         if (!p) {
210c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
211c7cacb3eSJeff Cody             break;
212c7cacb3eSJeff Cody         }
213c7cacb3eSJeff Cody 
214c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
215c7cacb3eSJeff Cody 
216730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
217c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
218c7cacb3eSJeff Cody 
219c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
22046f5ac20SEric Blake             qdict_put_str(options, "conf", value);
221c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
22246f5ac20SEric Blake             qdict_put_str(options, "user", value);
223c7cacb3eSJeff Cody         } else {
224e98c6961SEric Blake             /*
225e98c6961SEric Blake              * We pass these internally to qemu_rbd_set_keypairs(), so
226e98c6961SEric Blake              * we can get away with the simpler list of [ "key1",
227e98c6961SEric Blake              * "value1", "key2", "value2" ] rather than a raw dict
228e98c6961SEric Blake              * { "key1": "value1", "key2": "value2" } where we can't
229e98c6961SEric Blake              * guarantee order, or even a more correct but complex
230e98c6961SEric Blake              * [ { "key1": "value1" }, { "key2": "value2" } ]
231e98c6961SEric Blake              */
232e98c6961SEric Blake             if (!keypairs) {
233e98c6961SEric Blake                 keypairs = qlist_new();
234c7cacb3eSJeff Cody             }
23546f5ac20SEric Blake             qlist_append_str(keypairs, name);
23646f5ac20SEric Blake             qlist_append_str(keypairs, value);
237c7cacb3eSJeff Cody         }
238c7cacb3eSJeff Cody     }
239c7cacb3eSJeff Cody 
240e98c6961SEric Blake     if (keypairs) {
241e98c6961SEric Blake         qdict_put(options, "=keyvalue-pairs",
242eab3a467SMarkus Armbruster                   qstring_from_gstring(qobject_to_json(QOBJECT(keypairs))));
243c7cacb3eSJeff Cody     }
244c7cacb3eSJeff Cody 
245f27aaf4bSChristian Brunner done:
2467267c094SAnthony Liguori     g_free(buf);
247cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
248c7cacb3eSJeff Cody     return;
2497c7e9df0SSage Weil }
2507c7e9df0SSage Weil 
25160390a21SDaniel P. Berrange 
252e8e16d4bSEric Blake static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
253e8e16d4bSEric Blake {
254e8e16d4bSEric Blake     /* XXX Does RBD support AIO on less than 512-byte alignment? */
255e8e16d4bSEric Blake     bs->bl.request_alignment = 512;
256e8e16d4bSEric Blake }
257e8e16d4bSEric Blake 
258e8e16d4bSEric Blake 
259d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
26060390a21SDaniel P. Berrange                              Error **errp)
26160390a21SDaniel P. Berrange {
262d083f954SMarkus Armbruster     char *key, *acr;
263a3699de4SMarkus Armbruster     int r;
264a3699de4SMarkus Armbruster     GString *accu;
265a3699de4SMarkus Armbruster     RbdAuthModeList *auth;
26660390a21SDaniel P. Berrange 
267d083f954SMarkus Armbruster     if (opts->key_secret) {
268d083f954SMarkus Armbruster         key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
269d083f954SMarkus Armbruster         if (!key) {
270d083f954SMarkus Armbruster             return -EIO;
27160390a21SDaniel P. Berrange         }
272d083f954SMarkus Armbruster         r = rados_conf_set(cluster, "key", key);
273d083f954SMarkus Armbruster         g_free(key);
274d083f954SMarkus Armbruster         if (r < 0) {
275d083f954SMarkus Armbruster             error_setg_errno(errp, -r, "Could not set 'key'");
276d083f954SMarkus Armbruster             return r;
277d083f954SMarkus Armbruster         }
278a3699de4SMarkus Armbruster     }
279a3699de4SMarkus Armbruster 
280a3699de4SMarkus Armbruster     if (opts->has_auth_client_required) {
281a3699de4SMarkus Armbruster         accu = g_string_new("");
282a3699de4SMarkus Armbruster         for (auth = opts->auth_client_required; auth; auth = auth->next) {
283a3699de4SMarkus Armbruster             if (accu->str[0]) {
284a3699de4SMarkus Armbruster                 g_string_append_c(accu, ';');
285a3699de4SMarkus Armbruster             }
286a3699de4SMarkus Armbruster             g_string_append(accu, RbdAuthMode_str(auth->value));
287a3699de4SMarkus Armbruster         }
288a3699de4SMarkus Armbruster         acr = g_string_free(accu, FALSE);
289a3699de4SMarkus Armbruster         r = rados_conf_set(cluster, "auth_client_required", acr);
290a3699de4SMarkus Armbruster         g_free(acr);
291a3699de4SMarkus Armbruster         if (r < 0) {
292a3699de4SMarkus Armbruster             error_setg_errno(errp, -r,
293a3699de4SMarkus Armbruster                              "Could not set 'auth_client_required'");
294a3699de4SMarkus Armbruster             return r;
295a3699de4SMarkus Armbruster         }
296a3699de4SMarkus Armbruster     }
29760390a21SDaniel P. Berrange 
29860390a21SDaniel P. Berrange     return 0;
29960390a21SDaniel P. Berrange }
30060390a21SDaniel P. Berrange 
301e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
302e34d8f29SJosh Durgin                                  Error **errp)
303fab5cf59SJosh Durgin {
304e98c6961SEric Blake     QList *keypairs;
305e98c6961SEric Blake     QString *name;
306e98c6961SEric Blake     QString *value;
307e98c6961SEric Blake     const char *key;
308e98c6961SEric Blake     size_t remaining;
309fab5cf59SJosh Durgin     int ret = 0;
310fab5cf59SJosh Durgin 
311e98c6961SEric Blake     if (!keypairs_json) {
312e98c6961SEric Blake         return ret;
313fab5cf59SJosh Durgin     }
3147dc847ebSMax Reitz     keypairs = qobject_to(QList,
3157dc847ebSMax Reitz                           qobject_from_json(keypairs_json, &error_abort));
316e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
317e98c6961SEric Blake     assert(remaining);
318fab5cf59SJosh Durgin 
319e98c6961SEric Blake     while (remaining--) {
3207dc847ebSMax Reitz         name = qobject_to(QString, qlist_pop(keypairs));
3217dc847ebSMax Reitz         value = qobject_to(QString, qlist_pop(keypairs));
322e98c6961SEric Blake         assert(name && value);
323e98c6961SEric Blake         key = qstring_get_str(name);
324fab5cf59SJosh Durgin 
325e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
326cb3e7f08SMarc-André Lureau         qobject_unref(value);
327fab5cf59SJosh Durgin         if (ret < 0) {
328e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
329cb3e7f08SMarc-André Lureau             qobject_unref(name);
330fab5cf59SJosh Durgin             ret = -EINVAL;
331fab5cf59SJosh Durgin             break;
332fab5cf59SJosh Durgin         }
333cb3e7f08SMarc-André Lureau         qobject_unref(name);
334fab5cf59SJosh Durgin     }
335fab5cf59SJosh Durgin 
336cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
337fab5cf59SJosh Durgin     return ret;
338fab5cf59SJosh Durgin }
339fab5cf59SJosh Durgin 
3401d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
3411d393bdeStianqing {
3421d393bdeStianqing     RBDAIOCB *acb = rcb->acb;
3431d393bdeStianqing     iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
3441d393bdeStianqing                acb->qiov->size - offs);
3451d393bdeStianqing }
3461d393bdeStianqing 
34742e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
34842e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_options(
34942e4ac9eSOr Ozeri         RbdEncryptionOptionsLUKSBase *luks_opts,
35042e4ac9eSOr Ozeri         char **passphrase,
35142e4ac9eSOr Ozeri         size_t *passphrase_len,
35242e4ac9eSOr Ozeri         Error **errp)
35342e4ac9eSOr Ozeri {
35442e4ac9eSOr Ozeri     return qcrypto_secret_lookup(luks_opts->key_secret, (uint8_t **)passphrase,
35542e4ac9eSOr Ozeri                                  passphrase_len, errp);
35642e4ac9eSOr Ozeri }
35742e4ac9eSOr Ozeri 
35842e4ac9eSOr Ozeri static int qemu_rbd_convert_luks_create_options(
35942e4ac9eSOr Ozeri         RbdEncryptionCreateOptionsLUKSBase *luks_opts,
36042e4ac9eSOr Ozeri         rbd_encryption_algorithm_t *alg,
36142e4ac9eSOr Ozeri         char **passphrase,
36242e4ac9eSOr Ozeri         size_t *passphrase_len,
36342e4ac9eSOr Ozeri         Error **errp)
36442e4ac9eSOr Ozeri {
36542e4ac9eSOr Ozeri     int r = 0;
36642e4ac9eSOr Ozeri 
36742e4ac9eSOr Ozeri     r = qemu_rbd_convert_luks_options(
36842e4ac9eSOr Ozeri             qapi_RbdEncryptionCreateOptionsLUKSBase_base(luks_opts),
36942e4ac9eSOr Ozeri             passphrase, passphrase_len, errp);
37042e4ac9eSOr Ozeri     if (r < 0) {
37142e4ac9eSOr Ozeri         return r;
37242e4ac9eSOr Ozeri     }
37342e4ac9eSOr Ozeri 
37442e4ac9eSOr Ozeri     if (luks_opts->has_cipher_alg) {
37542e4ac9eSOr Ozeri         switch (luks_opts->cipher_alg) {
37642e4ac9eSOr Ozeri             case QCRYPTO_CIPHER_ALG_AES_128: {
37742e4ac9eSOr Ozeri                 *alg = RBD_ENCRYPTION_ALGORITHM_AES128;
37842e4ac9eSOr Ozeri                 break;
37942e4ac9eSOr Ozeri             }
38042e4ac9eSOr Ozeri             case QCRYPTO_CIPHER_ALG_AES_256: {
38142e4ac9eSOr Ozeri                 *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
38242e4ac9eSOr Ozeri                 break;
38342e4ac9eSOr Ozeri             }
38442e4ac9eSOr Ozeri             default: {
38542e4ac9eSOr Ozeri                 r = -ENOTSUP;
38642e4ac9eSOr Ozeri                 error_setg_errno(errp, -r, "unknown encryption algorithm: %u",
38742e4ac9eSOr Ozeri                                  luks_opts->cipher_alg);
38842e4ac9eSOr Ozeri                 return r;
38942e4ac9eSOr Ozeri             }
39042e4ac9eSOr Ozeri         }
39142e4ac9eSOr Ozeri     } else {
39242e4ac9eSOr Ozeri         /* default alg */
39342e4ac9eSOr Ozeri         *alg = RBD_ENCRYPTION_ALGORITHM_AES256;
39442e4ac9eSOr Ozeri     }
39542e4ac9eSOr Ozeri 
39642e4ac9eSOr Ozeri     return 0;
39742e4ac9eSOr Ozeri }
39842e4ac9eSOr Ozeri 
39942e4ac9eSOr Ozeri static int qemu_rbd_encryption_format(rbd_image_t image,
40042e4ac9eSOr Ozeri                                       RbdEncryptionCreateOptions *encrypt,
40142e4ac9eSOr Ozeri                                       Error **errp)
40242e4ac9eSOr Ozeri {
40342e4ac9eSOr Ozeri     int r = 0;
40442e4ac9eSOr Ozeri     g_autofree char *passphrase = NULL;
40542e4ac9eSOr Ozeri     size_t passphrase_len;
40642e4ac9eSOr Ozeri     rbd_encryption_format_t format;
40742e4ac9eSOr Ozeri     rbd_encryption_options_t opts;
40842e4ac9eSOr Ozeri     rbd_encryption_luks1_format_options_t luks_opts;
40942e4ac9eSOr Ozeri     rbd_encryption_luks2_format_options_t luks2_opts;
41042e4ac9eSOr Ozeri     size_t opts_size;
41142e4ac9eSOr Ozeri     uint64_t raw_size, effective_size;
41242e4ac9eSOr Ozeri 
41342e4ac9eSOr Ozeri     r = rbd_get_size(image, &raw_size);
41442e4ac9eSOr Ozeri     if (r < 0) {
41542e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot get raw image size");
41642e4ac9eSOr Ozeri         return r;
41742e4ac9eSOr Ozeri     }
41842e4ac9eSOr Ozeri 
41942e4ac9eSOr Ozeri     switch (encrypt->format) {
42042e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
42142e4ac9eSOr Ozeri             memset(&luks_opts, 0, sizeof(luks_opts));
42242e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS1;
42342e4ac9eSOr Ozeri             opts = &luks_opts;
42442e4ac9eSOr Ozeri             opts_size = sizeof(luks_opts);
42542e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_create_options(
42642e4ac9eSOr Ozeri                     qapi_RbdEncryptionCreateOptionsLUKS_base(&encrypt->u.luks),
42742e4ac9eSOr Ozeri                     &luks_opts.alg, &passphrase, &passphrase_len, errp);
42842e4ac9eSOr Ozeri             if (r < 0) {
42942e4ac9eSOr Ozeri                 return r;
43042e4ac9eSOr Ozeri             }
43142e4ac9eSOr Ozeri             luks_opts.passphrase = passphrase;
43242e4ac9eSOr Ozeri             luks_opts.passphrase_size = passphrase_len;
43342e4ac9eSOr Ozeri             break;
43442e4ac9eSOr Ozeri         }
43542e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
43642e4ac9eSOr Ozeri             memset(&luks2_opts, 0, sizeof(luks2_opts));
43742e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS2;
43842e4ac9eSOr Ozeri             opts = &luks2_opts;
43942e4ac9eSOr Ozeri             opts_size = sizeof(luks2_opts);
44042e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_create_options(
44142e4ac9eSOr Ozeri                     qapi_RbdEncryptionCreateOptionsLUKS2_base(
44242e4ac9eSOr Ozeri                             &encrypt->u.luks2),
44342e4ac9eSOr Ozeri                     &luks2_opts.alg, &passphrase, &passphrase_len, errp);
44442e4ac9eSOr Ozeri             if (r < 0) {
44542e4ac9eSOr Ozeri                 return r;
44642e4ac9eSOr Ozeri             }
44742e4ac9eSOr Ozeri             luks2_opts.passphrase = passphrase;
44842e4ac9eSOr Ozeri             luks2_opts.passphrase_size = passphrase_len;
44942e4ac9eSOr Ozeri             break;
45042e4ac9eSOr Ozeri         }
45142e4ac9eSOr Ozeri         default: {
45242e4ac9eSOr Ozeri             r = -ENOTSUP;
45342e4ac9eSOr Ozeri             error_setg_errno(
45442e4ac9eSOr Ozeri                     errp, -r, "unknown image encryption format: %u",
45542e4ac9eSOr Ozeri                     encrypt->format);
45642e4ac9eSOr Ozeri             return r;
45742e4ac9eSOr Ozeri         }
45842e4ac9eSOr Ozeri     }
45942e4ac9eSOr Ozeri 
46042e4ac9eSOr Ozeri     r = rbd_encryption_format(image, format, opts, opts_size);
46142e4ac9eSOr Ozeri     if (r < 0) {
46242e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "encryption format fail");
46342e4ac9eSOr Ozeri         return r;
46442e4ac9eSOr Ozeri     }
46542e4ac9eSOr Ozeri 
46642e4ac9eSOr Ozeri     r = rbd_get_size(image, &effective_size);
46742e4ac9eSOr Ozeri     if (r < 0) {
46842e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot get effective image size");
46942e4ac9eSOr Ozeri         return r;
47042e4ac9eSOr Ozeri     }
47142e4ac9eSOr Ozeri 
47242e4ac9eSOr Ozeri     r = rbd_resize(image, raw_size + (raw_size - effective_size));
47342e4ac9eSOr Ozeri     if (r < 0) {
47442e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "cannot resize image after format");
47542e4ac9eSOr Ozeri         return r;
47642e4ac9eSOr Ozeri     }
47742e4ac9eSOr Ozeri 
47842e4ac9eSOr Ozeri     return 0;
47942e4ac9eSOr Ozeri }
48042e4ac9eSOr Ozeri 
48142e4ac9eSOr Ozeri static int qemu_rbd_encryption_load(rbd_image_t image,
48242e4ac9eSOr Ozeri                                     RbdEncryptionOptions *encrypt,
48342e4ac9eSOr Ozeri                                     Error **errp)
48442e4ac9eSOr Ozeri {
48542e4ac9eSOr Ozeri     int r = 0;
48642e4ac9eSOr Ozeri     g_autofree char *passphrase = NULL;
48742e4ac9eSOr Ozeri     size_t passphrase_len;
48842e4ac9eSOr Ozeri     rbd_encryption_luks1_format_options_t luks_opts;
48942e4ac9eSOr Ozeri     rbd_encryption_luks2_format_options_t luks2_opts;
49042e4ac9eSOr Ozeri     rbd_encryption_format_t format;
49142e4ac9eSOr Ozeri     rbd_encryption_options_t opts;
49242e4ac9eSOr Ozeri     size_t opts_size;
49342e4ac9eSOr Ozeri 
49442e4ac9eSOr Ozeri     switch (encrypt->format) {
49542e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS: {
49642e4ac9eSOr Ozeri             memset(&luks_opts, 0, sizeof(luks_opts));
49742e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS1;
49842e4ac9eSOr Ozeri             opts = &luks_opts;
49942e4ac9eSOr Ozeri             opts_size = sizeof(luks_opts);
50042e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_options(
50142e4ac9eSOr Ozeri                     qapi_RbdEncryptionOptionsLUKS_base(&encrypt->u.luks),
50242e4ac9eSOr Ozeri                     &passphrase, &passphrase_len, errp);
50342e4ac9eSOr Ozeri             if (r < 0) {
50442e4ac9eSOr Ozeri                 return r;
50542e4ac9eSOr Ozeri             }
50642e4ac9eSOr Ozeri             luks_opts.passphrase = passphrase;
50742e4ac9eSOr Ozeri             luks_opts.passphrase_size = passphrase_len;
50842e4ac9eSOr Ozeri             break;
50942e4ac9eSOr Ozeri         }
51042e4ac9eSOr Ozeri         case RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2: {
51142e4ac9eSOr Ozeri             memset(&luks2_opts, 0, sizeof(luks2_opts));
51242e4ac9eSOr Ozeri             format = RBD_ENCRYPTION_FORMAT_LUKS2;
51342e4ac9eSOr Ozeri             opts = &luks2_opts;
51442e4ac9eSOr Ozeri             opts_size = sizeof(luks2_opts);
51542e4ac9eSOr Ozeri             r = qemu_rbd_convert_luks_options(
51642e4ac9eSOr Ozeri                     qapi_RbdEncryptionOptionsLUKS2_base(&encrypt->u.luks2),
51742e4ac9eSOr Ozeri                     &passphrase, &passphrase_len, errp);
51842e4ac9eSOr Ozeri             if (r < 0) {
51942e4ac9eSOr Ozeri                 return r;
52042e4ac9eSOr Ozeri             }
52142e4ac9eSOr Ozeri             luks2_opts.passphrase = passphrase;
52242e4ac9eSOr Ozeri             luks2_opts.passphrase_size = passphrase_len;
52342e4ac9eSOr Ozeri             break;
52442e4ac9eSOr Ozeri         }
52542e4ac9eSOr Ozeri         default: {
52642e4ac9eSOr Ozeri             r = -ENOTSUP;
52742e4ac9eSOr Ozeri             error_setg_errno(
52842e4ac9eSOr Ozeri                     errp, -r, "unknown image encryption format: %u",
52942e4ac9eSOr Ozeri                     encrypt->format);
53042e4ac9eSOr Ozeri             return r;
53142e4ac9eSOr Ozeri         }
53242e4ac9eSOr Ozeri     }
53342e4ac9eSOr Ozeri 
53442e4ac9eSOr Ozeri     r = rbd_encryption_load(image, format, opts, opts_size);
53542e4ac9eSOr Ozeri     if (r < 0) {
53642e4ac9eSOr Ozeri         error_setg_errno(errp, -r, "encryption load fail");
53742e4ac9eSOr Ozeri         return r;
53842e4ac9eSOr Ozeri     }
53942e4ac9eSOr Ozeri 
54042e4ac9eSOr Ozeri     return 0;
54142e4ac9eSOr Ozeri }
54242e4ac9eSOr Ozeri #endif
54342e4ac9eSOr Ozeri 
544d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */
5451bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
5461bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
5471bebea37SKevin Wolf                               Error **errp)
5481bebea37SKevin Wolf {
5491bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
5501bebea37SKevin Wolf     rados_t cluster;
5511bebea37SKevin Wolf     rados_ioctx_t io_ctx;
5521bebea37SKevin Wolf     int obj_order = 0;
5531bebea37SKevin Wolf     int ret;
5541bebea37SKevin Wolf 
5551bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
5561bebea37SKevin Wolf     if (opts->location->has_snapshot) {
5571bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
5581bebea37SKevin Wolf         return -EINVAL;
5591bebea37SKevin Wolf     }
5601bebea37SKevin Wolf 
56142e4ac9eSOr Ozeri #ifndef LIBRBD_SUPPORTS_ENCRYPTION
56242e4ac9eSOr Ozeri     if (opts->has_encrypt) {
56342e4ac9eSOr Ozeri         error_setg(errp, "RBD library does not support image encryption");
56442e4ac9eSOr Ozeri         return -ENOTSUP;
56542e4ac9eSOr Ozeri     }
56642e4ac9eSOr Ozeri #endif
56742e4ac9eSOr Ozeri 
5681bebea37SKevin Wolf     if (opts->has_cluster_size) {
5691bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
5701bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
5711bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
5721bebea37SKevin Wolf             return -EINVAL;
5731bebea37SKevin Wolf         }
5741bebea37SKevin Wolf         if (objsize < 4096) {
5751bebea37SKevin Wolf             error_setg(errp, "obj size too small");
5761bebea37SKevin Wolf             return -EINVAL;
5771bebea37SKevin Wolf         }
5781bebea37SKevin Wolf         obj_order = ctz32(objsize);
5791bebea37SKevin Wolf     }
5801bebea37SKevin Wolf 
581aa045c2dSKevin Wolf     ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
582aa045c2dSKevin Wolf                            password_secret, errp);
5831bebea37SKevin Wolf     if (ret < 0) {
5841bebea37SKevin Wolf         return ret;
5851bebea37SKevin Wolf     }
5861bebea37SKevin Wolf 
5871bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
5881bebea37SKevin Wolf     if (ret < 0) {
5891bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
590aa045c2dSKevin Wolf         goto out;
5911bebea37SKevin Wolf     }
5921bebea37SKevin Wolf 
59342e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
59442e4ac9eSOr Ozeri     if (opts->has_encrypt) {
59542e4ac9eSOr Ozeri         rbd_image_t image;
59642e4ac9eSOr Ozeri 
59742e4ac9eSOr Ozeri         ret = rbd_open(io_ctx, opts->location->image, &image, NULL);
59842e4ac9eSOr Ozeri         if (ret < 0) {
59942e4ac9eSOr Ozeri             error_setg_errno(errp, -ret,
60042e4ac9eSOr Ozeri                              "error opening image '%s' for encryption format",
60142e4ac9eSOr Ozeri                              opts->location->image);
60242e4ac9eSOr Ozeri             goto out;
60342e4ac9eSOr Ozeri         }
60442e4ac9eSOr Ozeri 
60542e4ac9eSOr Ozeri         ret = qemu_rbd_encryption_format(image, opts->encrypt, errp);
60642e4ac9eSOr Ozeri         rbd_close(image);
60742e4ac9eSOr Ozeri         if (ret < 0) {
60842e4ac9eSOr Ozeri             /* encryption format fail, try removing the image */
60942e4ac9eSOr Ozeri             rbd_remove(io_ctx, opts->location->image);
61042e4ac9eSOr Ozeri             goto out;
61142e4ac9eSOr Ozeri         }
61242e4ac9eSOr Ozeri     }
61342e4ac9eSOr Ozeri #endif
61442e4ac9eSOr Ozeri 
6151bebea37SKevin Wolf     ret = 0;
616aa045c2dSKevin Wolf out:
617aa045c2dSKevin Wolf     rados_ioctx_destroy(io_ctx);
6181bebea37SKevin Wolf     rados_shutdown(cluster);
6191bebea37SKevin Wolf     return ret;
6201bebea37SKevin Wolf }
6211bebea37SKevin Wolf 
6221bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
6231bebea37SKevin Wolf {
6241bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
6251bebea37SKevin Wolf }
6261bebea37SKevin Wolf 
62742e4ac9eSOr Ozeri static int qemu_rbd_extract_encryption_create_options(
62842e4ac9eSOr Ozeri         QemuOpts *opts,
62942e4ac9eSOr Ozeri         RbdEncryptionCreateOptions **spec,
63042e4ac9eSOr Ozeri         Error **errp)
63142e4ac9eSOr Ozeri {
63242e4ac9eSOr Ozeri     QDict *opts_qdict;
63342e4ac9eSOr Ozeri     QDict *encrypt_qdict;
63442e4ac9eSOr Ozeri     Visitor *v;
63542e4ac9eSOr Ozeri     int ret = 0;
63642e4ac9eSOr Ozeri 
63742e4ac9eSOr Ozeri     opts_qdict = qemu_opts_to_qdict(opts, NULL);
63842e4ac9eSOr Ozeri     qdict_extract_subqdict(opts_qdict, &encrypt_qdict, "encrypt.");
63942e4ac9eSOr Ozeri     qobject_unref(opts_qdict);
64042e4ac9eSOr Ozeri     if (!qdict_size(encrypt_qdict)) {
64142e4ac9eSOr Ozeri         *spec = NULL;
64242e4ac9eSOr Ozeri         goto exit;
64342e4ac9eSOr Ozeri     }
64442e4ac9eSOr Ozeri 
64542e4ac9eSOr Ozeri     /* Convert options into a QAPI object */
64642e4ac9eSOr Ozeri     v = qobject_input_visitor_new_flat_confused(encrypt_qdict, errp);
64742e4ac9eSOr Ozeri     if (!v) {
64842e4ac9eSOr Ozeri         ret = -EINVAL;
64942e4ac9eSOr Ozeri         goto exit;
65042e4ac9eSOr Ozeri     }
65142e4ac9eSOr Ozeri 
65242e4ac9eSOr Ozeri     visit_type_RbdEncryptionCreateOptions(v, NULL, spec, errp);
65342e4ac9eSOr Ozeri     visit_free(v);
65442e4ac9eSOr Ozeri     if (!*spec) {
65542e4ac9eSOr Ozeri         ret = -EINVAL;
65642e4ac9eSOr Ozeri         goto exit;
65742e4ac9eSOr Ozeri     }
65842e4ac9eSOr Ozeri 
65942e4ac9eSOr Ozeri exit:
66042e4ac9eSOr Ozeri     qobject_unref(encrypt_qdict);
66142e4ac9eSOr Ozeri     return ret;
66242e4ac9eSOr Ozeri }
66342e4ac9eSOr Ozeri 
664b92902dfSMaxim Levitsky static int coroutine_fn qemu_rbd_co_create_opts(BlockDriver *drv,
665b92902dfSMaxim Levitsky                                                 const char *filename,
666efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
667efc75e2aSStefan Hajnoczi                                                 Error **errp)
668f27aaf4bSChristian Brunner {
6691bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
6701bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
6711bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
67242e4ac9eSOr Ozeri     RbdEncryptionCreateOptions *encrypt = NULL;
673d61563b2SMarkus Armbruster     Error *local_err = NULL;
6741bebea37SKevin Wolf     const char *keypairs, *password_secret;
675c7cacb3eSJeff Cody     QDict *options = NULL;
676c7cacb3eSJeff Cody     int ret = 0;
677f27aaf4bSChristian Brunner 
6781bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
6791bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
6801bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
6811bebea37SKevin Wolf 
6821bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
6831bebea37SKevin Wolf 
6841bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
68560390a21SDaniel P. Berrange 
686f27aaf4bSChristian Brunner     /* Read out options */
6871bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
688c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
6891bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
6901bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
6911bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
692f27aaf4bSChristian Brunner 
693c7cacb3eSJeff Cody     options = qdict_new();
694c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
695c7cacb3eSJeff Cody     if (local_err) {
696c7cacb3eSJeff Cody         ret = -EINVAL;
697c7cacb3eSJeff Cody         error_propagate(errp, local_err);
698c7cacb3eSJeff Cody         goto exit;
699c7cacb3eSJeff Cody     }
700c7cacb3eSJeff Cody 
70142e4ac9eSOr Ozeri     ret = qemu_rbd_extract_encryption_create_options(opts, &encrypt, errp);
70242e4ac9eSOr Ozeri     if (ret < 0) {
70342e4ac9eSOr Ozeri         goto exit;
70442e4ac9eSOr Ozeri     }
70542e4ac9eSOr Ozeri     rbd_opts->encrypt     = encrypt;
70642e4ac9eSOr Ozeri     rbd_opts->has_encrypt = !!encrypt;
70742e4ac9eSOr Ozeri 
708129c7d1cSMarkus Armbruster     /*
709129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
710129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
711129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
712129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
713129c7d1cSMarkus Armbruster      */
7141bebea37SKevin Wolf     loc = rbd_opts->location;
7151bebea37SKevin Wolf     loc->pool        = g_strdup(qdict_get_try_str(options, "pool"));
7161bebea37SKevin Wolf     loc->conf        = g_strdup(qdict_get_try_str(options, "conf"));
7171bebea37SKevin Wolf     loc->has_conf    = !!loc->conf;
7181bebea37SKevin Wolf     loc->user        = g_strdup(qdict_get_try_str(options, "user"));
7191bebea37SKevin Wolf     loc->has_user    = !!loc->user;
72019ae9ae0SFlorian Florensa     loc->q_namespace = g_strdup(qdict_get_try_str(options, "namespace"));
721b084b420SStefano Garzarella     loc->has_q_namespace = !!loc->q_namespace;
7221bebea37SKevin Wolf     loc->image       = g_strdup(qdict_get_try_str(options, "image"));
72307846397SMarkus Armbruster     keypairs         = qdict_get_try_str(options, "=keyvalue-pairs");
724c7cacb3eSJeff Cody 
7251bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
72687cd3d20SVikhyat Umrao     if (ret < 0) {
727c7cacb3eSJeff Cody         goto exit;
728f27aaf4bSChristian Brunner     }
729f27aaf4bSChristian Brunner 
730c7cacb3eSJeff Cody exit:
731cb3e7f08SMarc-André Lureau     qobject_unref(options);
7321bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
733f27aaf4bSChristian Brunner     return ret;
734f27aaf4bSChristian Brunner }
735f27aaf4bSChristian Brunner 
736f27aaf4bSChristian Brunner /*
737e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
738e04fb07fSStefan Hajnoczi  * BH context.
739f27aaf4bSChristian Brunner  */
740ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
741f27aaf4bSChristian Brunner {
742f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
743f27aaf4bSChristian Brunner     int64_t r;
744f27aaf4bSChristian Brunner 
745f27aaf4bSChristian Brunner     r = rcb->ret;
746f27aaf4bSChristian Brunner 
747dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
748f27aaf4bSChristian Brunner         if (r < 0) {
749f27aaf4bSChristian Brunner             acb->ret = r;
750f27aaf4bSChristian Brunner             acb->error = 1;
751f27aaf4bSChristian Brunner         } else if (!acb->error) {
752ad32e9c0SJosh Durgin             acb->ret = rcb->size;
753f27aaf4bSChristian Brunner         }
754f27aaf4bSChristian Brunner     } else {
755ad32e9c0SJosh Durgin         if (r < 0) {
7561d393bdeStianqing             qemu_rbd_memset(rcb, 0);
757f27aaf4bSChristian Brunner             acb->ret = r;
758f27aaf4bSChristian Brunner             acb->error = 1;
759ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
7601d393bdeStianqing             qemu_rbd_memset(rcb, r);
761f27aaf4bSChristian Brunner             if (!acb->error) {
762ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
763f27aaf4bSChristian Brunner             }
764f27aaf4bSChristian Brunner         } else if (!acb->error) {
765ad32e9c0SJosh Durgin             acb->ret = r;
766f27aaf4bSChristian Brunner         }
767f27aaf4bSChristian Brunner     }
768e04fb07fSStefan Hajnoczi 
7697267c094SAnthony Liguori     g_free(rcb);
770e04fb07fSStefan Hajnoczi 
771e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
772f27aaf4bSChristian Brunner 
7738007429aSFam Zheng     qemu_aio_unref(acb);
774f27aaf4bSChristian Brunner }
775f27aaf4bSChristian Brunner 
7764bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
7770a55679bSJeff Cody {
7784bfb2741SKevin Wolf     const char **vals;
7792836284dSMarkus Armbruster     const char *host, *port;
7802836284dSMarkus Armbruster     char *rados_str;
7814bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
7824bfb2741SKevin Wolf     int i, cnt;
7830a55679bSJeff Cody 
7844bfb2741SKevin Wolf     if (!opts->has_server) {
7854bfb2741SKevin Wolf         return NULL;
7860a55679bSJeff Cody     }
7874bfb2741SKevin Wolf 
7884bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
7894bfb2741SKevin Wolf         cnt++;
7900a55679bSJeff Cody     }
7910a55679bSJeff Cody 
7924bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
7934bfb2741SKevin Wolf 
7944bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
7954bfb2741SKevin Wolf         host = p->value->host;
7964bfb2741SKevin Wolf         port = p->value->port;
7974bfb2741SKevin Wolf 
7980a55679bSJeff Cody         if (strchr(host, ':')) {
7994bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
8000a55679bSJeff Cody         } else {
8014bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
8020a55679bSJeff Cody         }
8030a55679bSJeff Cody     }
8042836284dSMarkus Armbruster     vals[i] = NULL;
8050a55679bSJeff Cody 
8062836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
8072836284dSMarkus Armbruster     g_strfreev((char **)vals);
8080a55679bSJeff Cody     return rados_str;
8090a55679bSJeff Cody }
8100a55679bSJeff Cody 
8113d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
8124bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
8134ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
8144ff45049SKevin Wolf                             Error **errp)
815f27aaf4bSChristian Brunner {
8160a55679bSJeff Cody     char *mon_host = NULL;
8173d9136f9SKevin Wolf     Error *local_err = NULL;
818f27aaf4bSChristian Brunner     int r;
819f27aaf4bSChristian Brunner 
820d083f954SMarkus Armbruster     if (secretid) {
821d083f954SMarkus Armbruster         if (opts->key_secret) {
822d083f954SMarkus Armbruster             error_setg(errp,
823d083f954SMarkus Armbruster                        "Legacy 'password-secret' clashes with 'key-secret'");
824d083f954SMarkus Armbruster             return -EINVAL;
825d083f954SMarkus Armbruster         }
826d083f954SMarkus Armbruster         opts->key_secret = g_strdup(secretid);
827d083f954SMarkus Armbruster         opts->has_key_secret = true;
828d083f954SMarkus Armbruster     }
829d083f954SMarkus Armbruster 
8304bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
83184d18f06SMarkus Armbruster     if (local_err) {
832d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
8332836284dSMarkus Armbruster         r = -EINVAL;
834c1c1f6cfSStefano Garzarella         goto out;
835a9ccedc3SKevin Wolf     }
836a9ccedc3SKevin Wolf 
8374bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
838ad32e9c0SJosh Durgin     if (r < 0) {
83987cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
840c1c1f6cfSStefano Garzarella         goto out;
841f27aaf4bSChristian Brunner     }
842f27aaf4bSChristian Brunner 
843c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
8444bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
8454bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
8464bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
847e34d8f29SJosh Durgin         goto failed_shutdown;
848e34d8f29SJosh Durgin     }
84999a3c89dSJosh Durgin 
8503d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
85199a3c89dSJosh Durgin     if (r < 0) {
85299a3c89dSJosh Durgin         goto failed_shutdown;
85399a3c89dSJosh Durgin     }
85499a3c89dSJosh Durgin 
8550a55679bSJeff Cody     if (mon_host) {
8563d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
8570a55679bSJeff Cody         if (r < 0) {
8580a55679bSJeff Cody             goto failed_shutdown;
8590a55679bSJeff Cody         }
8600a55679bSJeff Cody     }
8610a55679bSJeff Cody 
862d083f954SMarkus Armbruster     r = qemu_rbd_set_auth(*cluster, opts, errp);
863d083f954SMarkus Armbruster     if (r < 0) {
86460390a21SDaniel P. Berrange         goto failed_shutdown;
86560390a21SDaniel P. Berrange     }
86660390a21SDaniel P. Berrange 
867b11f38fcSJosh Durgin     /*
868b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
869b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
870b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
871b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
872b11f38fcSJosh Durgin      * be set up, fall back to no caching.
873b11f38fcSJosh Durgin      */
8743d9136f9SKevin Wolf     if (cache) {
8753d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
876b11f38fcSJosh Durgin     } else {
8773d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
878b11f38fcSJosh Durgin     }
879b11f38fcSJosh Durgin 
8803d9136f9SKevin Wolf     r = rados_connect(*cluster);
881ad32e9c0SJosh Durgin     if (r < 0) {
88287cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
883eb93d5d9SSage Weil         goto failed_shutdown;
884ad32e9c0SJosh Durgin     }
885ad32e9c0SJosh Durgin 
8864bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
887ad32e9c0SJosh Durgin     if (r < 0) {
8884bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
889eb93d5d9SSage Weil         goto failed_shutdown;
890ad32e9c0SJosh Durgin     }
89119ae9ae0SFlorian Florensa     /*
89219ae9ae0SFlorian Florensa      * Set the namespace after opening the io context on the pool,
89319ae9ae0SFlorian Florensa      * if nspace == NULL or if nspace == "", it is just as we did nothing
89419ae9ae0SFlorian Florensa      */
89519ae9ae0SFlorian Florensa     rados_ioctx_set_namespace(*io_ctx, opts->q_namespace);
896ad32e9c0SJosh Durgin 
897c1c1f6cfSStefano Garzarella     r = 0;
898c1c1f6cfSStefano Garzarella     goto out;
8993d9136f9SKevin Wolf 
9003d9136f9SKevin Wolf failed_shutdown:
9013d9136f9SKevin Wolf     rados_shutdown(*cluster);
902c1c1f6cfSStefano Garzarella out:
9033d9136f9SKevin Wolf     g_free(mon_host);
9043d9136f9SKevin Wolf     return r;
9053d9136f9SKevin Wolf }
9063d9136f9SKevin Wolf 
907f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
908f24b03b5SJeff Cody                                     Error **errp)
909f24b03b5SJeff Cody {
910f24b03b5SJeff Cody     Visitor *v;
911f24b03b5SJeff Cody 
912f24b03b5SJeff Cody     /* Convert the remaining options into a QAPI object */
913f24b03b5SJeff Cody     v = qobject_input_visitor_new_flat_confused(options, errp);
914f24b03b5SJeff Cody     if (!v) {
915f24b03b5SJeff Cody         return -EINVAL;
916f24b03b5SJeff Cody     }
917f24b03b5SJeff Cody 
918b11a093cSMarkus Armbruster     visit_type_BlockdevOptionsRbd(v, NULL, opts, errp);
919f24b03b5SJeff Cody     visit_free(v);
920b11a093cSMarkus Armbruster     if (!opts) {
921f24b03b5SJeff Cody         return -EINVAL;
922f24b03b5SJeff Cody     }
923f24b03b5SJeff Cody 
924f24b03b5SJeff Cody     return 0;
925f24b03b5SJeff Cody }
926f24b03b5SJeff Cody 
927084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options,
928084d1d13SJeff Cody                                            BlockdevOptionsRbd **opts,
929084d1d13SJeff Cody                                            char **keypairs)
930084d1d13SJeff Cody {
931084d1d13SJeff Cody     char *filename;
932084d1d13SJeff Cody     int r;
933084d1d13SJeff Cody 
934084d1d13SJeff Cody     filename = g_strdup(qdict_get_try_str(options, "filename"));
935084d1d13SJeff Cody     if (!filename) {
936084d1d13SJeff Cody         return -EINVAL;
937084d1d13SJeff Cody     }
938084d1d13SJeff Cody     qdict_del(options, "filename");
939084d1d13SJeff Cody 
940084d1d13SJeff Cody     qemu_rbd_parse_filename(filename, options, NULL);
941084d1d13SJeff Cody 
942084d1d13SJeff Cody     /* keypairs freed by caller */
943084d1d13SJeff Cody     *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
944084d1d13SJeff Cody     if (*keypairs) {
945084d1d13SJeff Cody         qdict_del(options, "=keyvalue-pairs");
946084d1d13SJeff Cody     }
947084d1d13SJeff Cody 
948084d1d13SJeff Cody     r = qemu_rbd_convert_options(options, opts, NULL);
949084d1d13SJeff Cody 
950084d1d13SJeff Cody     g_free(filename);
951084d1d13SJeff Cody     return r;
952084d1d13SJeff Cody }
953084d1d13SJeff Cody 
9543d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
9553d9136f9SKevin Wolf                          Error **errp)
9563d9136f9SKevin Wolf {
9573d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
9584bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
959bfb15b4bSJeff Cody     const QDictEntry *e;
9603d9136f9SKevin Wolf     Error *local_err = NULL;
9614ff45049SKevin Wolf     char *keypairs, *secretid;
962832a93dcSPeter Lieven     rbd_image_info_t info;
9633d9136f9SKevin Wolf     int r;
9643d9136f9SKevin Wolf 
9654ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
9664ff45049SKevin Wolf     if (keypairs) {
9674ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
9684ff45049SKevin Wolf     }
9694ff45049SKevin Wolf 
9704ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
9714ff45049SKevin Wolf     if (secretid) {
9724ff45049SKevin Wolf         qdict_del(options, "password-secret");
9734ff45049SKevin Wolf     }
9744ff45049SKevin Wolf 
975f24b03b5SJeff Cody     r = qemu_rbd_convert_options(options, &opts, &local_err);
9764bfb2741SKevin Wolf     if (local_err) {
977084d1d13SJeff Cody         /* If keypairs are present, that means some options are present in
978084d1d13SJeff Cody          * the modern option format.  Don't attempt to parse legacy option
979084d1d13SJeff Cody          * formats, as we won't support mixed usage. */
980084d1d13SJeff Cody         if (keypairs) {
9814bfb2741SKevin Wolf             error_propagate(errp, local_err);
9824bfb2741SKevin Wolf             goto out;
9834bfb2741SKevin Wolf         }
9844bfb2741SKevin Wolf 
985084d1d13SJeff Cody         /* If the initial attempt to convert and process the options failed,
986084d1d13SJeff Cody          * we may be attempting to open an image file that has the rbd options
987084d1d13SJeff Cody          * specified in the older format consisting of all key/value pairs
988084d1d13SJeff Cody          * encoded in the filename.  Go ahead and attempt to parse the
989084d1d13SJeff Cody          * filename, and see if we can pull out the required options. */
990084d1d13SJeff Cody         r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
991084d1d13SJeff Cody         if (r < 0) {
992084d1d13SJeff Cody             /* Propagate the original error, not the legacy parsing fallback
993084d1d13SJeff Cody              * error, as the latter was just a best-effort attempt. */
994084d1d13SJeff Cody             error_propagate(errp, local_err);
995084d1d13SJeff Cody             goto out;
996084d1d13SJeff Cody         }
997084d1d13SJeff Cody         /* Take care whenever deciding to actually deprecate; once this ability
998084d1d13SJeff Cody          * is removed, we will not be able to open any images with legacy-styled
999084d1d13SJeff Cody          * backing image strings. */
10005197f445SMarkus Armbruster         warn_report("RBD options encoded in the filename as keyvalue pairs "
1001084d1d13SJeff Cody                     "is deprecated");
1002084d1d13SJeff Cody     }
1003084d1d13SJeff Cody 
1004bfb15b4bSJeff Cody     /* Remove the processed options from the QDict (the visitor processes
1005bfb15b4bSJeff Cody      * _all_ options in the QDict) */
1006bfb15b4bSJeff Cody     while ((e = qdict_first(options))) {
1007bfb15b4bSJeff Cody         qdict_del(options, e->key);
1008bfb15b4bSJeff Cody     }
1009bfb15b4bSJeff Cody 
1010d41a5588SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
1011d41a5588SKevin Wolf                          !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
10123d9136f9SKevin Wolf     if (r < 0) {
10134ff45049SKevin Wolf         goto out;
10143d9136f9SKevin Wolf     }
10153d9136f9SKevin Wolf 
1016d41a5588SKevin Wolf     s->snap = g_strdup(opts->snapshot);
1017d41a5588SKevin Wolf     s->image_name = g_strdup(opts->image);
1018d41a5588SKevin Wolf 
1019e2b8247aSJeff Cody     /* rbd_open is always r/w */
102080b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
1021ad32e9c0SJosh Durgin     if (r < 0) {
102280b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
102380b61a27SJeff Cody                          s->image_name);
1024eb93d5d9SSage Weil         goto failed_open;
1025ad32e9c0SJosh Durgin     }
1026ad32e9c0SJosh Durgin 
102742e4ac9eSOr Ozeri     if (opts->has_encrypt) {
102842e4ac9eSOr Ozeri #ifdef LIBRBD_SUPPORTS_ENCRYPTION
102942e4ac9eSOr Ozeri         r = qemu_rbd_encryption_load(s->image, opts->encrypt, errp);
103042e4ac9eSOr Ozeri         if (r < 0) {
103142e4ac9eSOr Ozeri             goto failed_post_open;
103242e4ac9eSOr Ozeri         }
103342e4ac9eSOr Ozeri #else
103442e4ac9eSOr Ozeri         r = -ENOTSUP;
103542e4ac9eSOr Ozeri         error_setg(errp, "RBD library does not support image encryption");
103642e4ac9eSOr Ozeri         goto failed_post_open;
103742e4ac9eSOr Ozeri #endif
103842e4ac9eSOr Ozeri     }
103942e4ac9eSOr Ozeri 
1040832a93dcSPeter Lieven     r = rbd_stat(s->image, &info, sizeof(info));
1041d24f8023SStefano Garzarella     if (r < 0) {
1042832a93dcSPeter Lieven         error_setg_errno(errp, -r, "error getting image info from %s",
1043d24f8023SStefano Garzarella                          s->image_name);
104442e4ac9eSOr Ozeri         goto failed_post_open;
1045d24f8023SStefano Garzarella     }
1046832a93dcSPeter Lieven     s->image_size = info.size;
1047832a93dcSPeter Lieven     s->object_size = info.obj_size;
1048d24f8023SStefano Garzarella 
1049e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
1050e2b8247aSJeff Cody      * leave as-is */
1051e2b8247aSJeff Cody     if (s->snap != NULL) {
1052eaa2410fSKevin Wolf         r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
1053e2b8247aSJeff Cody         if (r < 0) {
105442e4ac9eSOr Ozeri             goto failed_post_open;
1055e2b8247aSJeff Cody         }
1056e2b8247aSJeff Cody     }
1057f27aaf4bSChristian Brunner 
10582f98910dSEric Blake     /* When extending regular files, we get zeros from the OS */
10592f98910dSEric Blake     bs->supported_truncate_flags = BDRV_REQ_ZERO_WRITE;
10602f98910dSEric Blake 
10614ff45049SKevin Wolf     r = 0;
10624ff45049SKevin Wolf     goto out;
1063f27aaf4bSChristian Brunner 
106442e4ac9eSOr Ozeri failed_post_open:
106542e4ac9eSOr Ozeri     rbd_close(s->image);
1066eb93d5d9SSage Weil failed_open:
1067ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
1068eb93d5d9SSage Weil     g_free(s->snap);
106980b61a27SJeff Cody     g_free(s->image_name);
10703d9136f9SKevin Wolf     rados_shutdown(s->cluster);
10714ff45049SKevin Wolf out:
10724bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
10734ff45049SKevin Wolf     g_free(keypairs);
10744ff45049SKevin Wolf     g_free(secretid);
1075f27aaf4bSChristian Brunner     return r;
1076f27aaf4bSChristian Brunner }
1077f27aaf4bSChristian Brunner 
107856e7cf8dSJeff Cody 
107956e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
108056e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
108156e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
108256e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
108356e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
108456e7cf8dSJeff Cody {
108556e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
108656e7cf8dSJeff Cody     int ret = 0;
108756e7cf8dSJeff Cody 
108856e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
108956e7cf8dSJeff Cody         error_setg(errp,
109056e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
109156e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
109256e7cf8dSJeff Cody         ret = -EINVAL;
109356e7cf8dSJeff Cody     }
109456e7cf8dSJeff Cody 
109556e7cf8dSJeff Cody     return ret;
109656e7cf8dSJeff Cody }
109756e7cf8dSJeff Cody 
1098ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
1099f27aaf4bSChristian Brunner {
1100f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1101f27aaf4bSChristian Brunner 
1102ad32e9c0SJosh Durgin     rbd_close(s->image);
1103ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
11047267c094SAnthony Liguori     g_free(s->snap);
110580b61a27SJeff Cody     g_free(s->image_name);
1106ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
1107f27aaf4bSChristian Brunner }
1108f27aaf4bSChristian Brunner 
1109d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */
1110d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size)
1111d24f8023SStefano Garzarella {
1112d24f8023SStefano Garzarella     BDRVRBDState *s = bs->opaque;
1113d24f8023SStefano Garzarella     int r;
1114d24f8023SStefano Garzarella 
1115d24f8023SStefano Garzarella     r = rbd_resize(s->image, size);
1116d24f8023SStefano Garzarella     if (r < 0) {
1117d24f8023SStefano Garzarella         return r;
1118d24f8023SStefano Garzarella     }
1119d24f8023SStefano Garzarella 
1120d24f8023SStefano Garzarella     s->image_size = size;
1121d24f8023SStefano Garzarella 
1122d24f8023SStefano Garzarella     return 0;
1123d24f8023SStefano Garzarella }
1124d24f8023SStefano Garzarella 
1125d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
1126f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
1127f27aaf4bSChristian Brunner };
1128f27aaf4bSChristian Brunner 
1129e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
1130f27aaf4bSChristian Brunner {
1131e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
1132e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
1133ad32e9c0SJosh Durgin }
1134ad32e9c0SJosh Durgin 
1135ad32e9c0SJosh Durgin /*
1136ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
1137ad32e9c0SJosh Durgin  *
1138ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
1139ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
1140e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
1141e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
1142ad32e9c0SJosh Durgin  */
1143ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
1144ad32e9c0SJosh Durgin {
1145e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
1146e04fb07fSStefan Hajnoczi 
1147ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
1148ad32e9c0SJosh Durgin     rbd_aio_release(c);
1149f27aaf4bSChristian Brunner 
1150e4ec5ad4SPavel Dovgalyuk     replay_bh_schedule_oneshot_event(bdrv_get_aio_context(acb->common.bs),
1151ea800191SStefan Hajnoczi                                      rbd_finish_bh, rcb);
1152473c7f02SStefan Priebe }
1153f27aaf4bSChristian Brunner 
11547c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
11557bbca9e2SEric Blake                                  int64_t off,
1156f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
11577bbca9e2SEric Blake                                  int64_t size,
1158097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
1159787f3133SJosh Durgin                                  void *opaque,
1160787f3133SJosh Durgin                                  RBDAIOCmd cmd)
1161f27aaf4bSChristian Brunner {
1162f27aaf4bSChristian Brunner     RBDAIOCB *acb;
11630f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
1164ad32e9c0SJosh Durgin     rbd_completion_t c;
116551a13528SJosh Durgin     int r;
1166f27aaf4bSChristian Brunner 
1167f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1168f27aaf4bSChristian Brunner 
1169d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
1170787f3133SJosh Durgin     acb->cmd = cmd;
1171f27aaf4bSChristian Brunner     acb->qiov = qiov;
11727bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
11731d393bdeStianqing 
11741d393bdeStianqing     rcb = g_new(RADOSCB, 1);
11751d393bdeStianqing 
1176f27aaf4bSChristian Brunner     acb->ret = 0;
1177f27aaf4bSChristian Brunner     acb->error = 0;
1178f27aaf4bSChristian Brunner     acb->s = s;
1179f27aaf4bSChristian Brunner 
1180f27aaf4bSChristian Brunner     rcb->acb = acb;
1181f27aaf4bSChristian Brunner     rcb->s = acb->s;
1182ad32e9c0SJosh Durgin     rcb->size = size;
118351a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
118451a13528SJosh Durgin     if (r < 0) {
118551a13528SJosh Durgin         goto failed;
118651a13528SJosh Durgin     }
1187f27aaf4bSChristian Brunner 
1188787f3133SJosh Durgin     switch (cmd) {
118948672ac0SPeter Lieven     case RBD_AIO_WRITE:
1190d24f8023SStefano Garzarella         /*
1191d24f8023SStefano Garzarella          * RBD APIs don't allow us to write more than actual size, so in order
1192d24f8023SStefano Garzarella          * to support growing images, we resize the image before write
1193d24f8023SStefano Garzarella          * operations that exceed the current size.
1194d24f8023SStefano Garzarella          */
1195d24f8023SStefano Garzarella         if (off + size > s->image_size) {
1196d24f8023SStefano Garzarella             r = qemu_rbd_resize(bs, off + size);
1197d24f8023SStefano Garzarella             if (r < 0) {
1198d24f8023SStefano Garzarella                 goto failed_completion;
1199d24f8023SStefano Garzarella             }
1200d24f8023SStefano Garzarella         }
12011d393bdeStianqing         r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
1202787f3133SJosh Durgin         break;
1203787f3133SJosh Durgin     case RBD_AIO_READ:
12041d393bdeStianqing         r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
1205787f3133SJosh Durgin         break;
1206787f3133SJosh Durgin     case RBD_AIO_DISCARD:
120748672ac0SPeter Lieven         r = rbd_aio_discard(s->image, off, size, c);
1208787f3133SJosh Durgin         break;
1209dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
121048672ac0SPeter Lieven         r = rbd_aio_flush(s->image, c);
1211dc7588c1SJosh Durgin         break;
1212787f3133SJosh Durgin     default:
1213787f3133SJosh Durgin         r = -EINVAL;
121451a13528SJosh Durgin     }
121551a13528SJosh Durgin 
121651a13528SJosh Durgin     if (r < 0) {
1217405a2764SKevin Wolf         goto failed_completion;
1218f27aaf4bSChristian Brunner     }
1219f27aaf4bSChristian Brunner     return &acb->common;
122051a13528SJosh Durgin 
1221405a2764SKevin Wolf failed_completion:
1222405a2764SKevin Wolf     rbd_aio_release(c);
122351a13528SJosh Durgin failed:
12247267c094SAnthony Liguori     g_free(rcb);
12251d393bdeStianqing 
12268007429aSFam Zheng     qemu_aio_unref(acb);
122751a13528SJosh Durgin     return NULL;
1228f27aaf4bSChristian Brunner }
1229f27aaf4bSChristian Brunner 
1230e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
1231e8e16d4bSEric Blake                                        uint64_t offset, uint64_t bytes,
1232e8e16d4bSEric Blake                                        QEMUIOVector *qiov, int flags,
1233097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
1234f27aaf4bSChristian Brunner                                        void *opaque)
1235f27aaf4bSChristian Brunner {
1236e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1237787f3133SJosh Durgin                          RBD_AIO_READ);
1238f27aaf4bSChristian Brunner }
1239f27aaf4bSChristian Brunner 
1240e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
1241e8e16d4bSEric Blake                                         uint64_t offset, uint64_t bytes,
1242e8e16d4bSEric Blake                                         QEMUIOVector *qiov, int flags,
1243097310b5SMarkus Armbruster                                         BlockCompletionFunc *cb,
1244f27aaf4bSChristian Brunner                                         void *opaque)
1245f27aaf4bSChristian Brunner {
1246e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1247787f3133SJosh Durgin                          RBD_AIO_WRITE);
1248f27aaf4bSChristian Brunner }
1249f27aaf4bSChristian Brunner 
12507c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
1251097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
1252dc7588c1SJosh Durgin                                       void *opaque)
1253dc7588c1SJosh Durgin {
1254dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
1255dc7588c1SJosh Durgin }
1256dc7588c1SJosh Durgin 
1257ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1258f27aaf4bSChristian Brunner {
1259f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1260832a93dcSPeter Lieven     bdi->cluster_size = s->object_size;
1261f27aaf4bSChristian Brunner     return 0;
1262f27aaf4bSChristian Brunner }
1263f27aaf4bSChristian Brunner 
126442e4ac9eSOr Ozeri static ImageInfoSpecific *qemu_rbd_get_specific_info(BlockDriverState *bs,
126542e4ac9eSOr Ozeri                                                      Error **errp)
126642e4ac9eSOr Ozeri {
126742e4ac9eSOr Ozeri     BDRVRBDState *s = bs->opaque;
126842e4ac9eSOr Ozeri     ImageInfoSpecific *spec_info;
126942e4ac9eSOr Ozeri     char buf[RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN] = {0};
127042e4ac9eSOr Ozeri     int r;
127142e4ac9eSOr Ozeri 
127242e4ac9eSOr Ozeri     if (s->image_size >= RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) {
127342e4ac9eSOr Ozeri         r = rbd_read(s->image, 0,
127442e4ac9eSOr Ozeri                      RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN, buf);
127542e4ac9eSOr Ozeri         if (r < 0) {
127642e4ac9eSOr Ozeri             error_setg_errno(errp, -r, "cannot read image start for probe");
127742e4ac9eSOr Ozeri             return NULL;
127842e4ac9eSOr Ozeri         }
127942e4ac9eSOr Ozeri     }
128042e4ac9eSOr Ozeri 
128142e4ac9eSOr Ozeri     spec_info = g_new(ImageInfoSpecific, 1);
128242e4ac9eSOr Ozeri     *spec_info = (ImageInfoSpecific){
128342e4ac9eSOr Ozeri         .type  = IMAGE_INFO_SPECIFIC_KIND_RBD,
128442e4ac9eSOr Ozeri         .u.rbd.data = g_new0(ImageInfoSpecificRbd, 1),
128542e4ac9eSOr Ozeri     };
128642e4ac9eSOr Ozeri 
128742e4ac9eSOr Ozeri     if (memcmp(buf, rbd_luks_header_verification,
128842e4ac9eSOr Ozeri                RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
128942e4ac9eSOr Ozeri         spec_info->u.rbd.data->encryption_format =
129042e4ac9eSOr Ozeri                 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS;
129142e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = true;
129242e4ac9eSOr Ozeri     } else if (memcmp(buf, rbd_luks2_header_verification,
129342e4ac9eSOr Ozeri                RBD_ENCRYPTION_LUKS_HEADER_VERIFICATION_LEN) == 0) {
129442e4ac9eSOr Ozeri         spec_info->u.rbd.data->encryption_format =
129542e4ac9eSOr Ozeri                 RBD_IMAGE_ENCRYPTION_FORMAT_LUKS2;
129642e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = true;
129742e4ac9eSOr Ozeri     } else {
129842e4ac9eSOr Ozeri         spec_info->u.rbd.data->has_encryption_format = false;
129942e4ac9eSOr Ozeri     }
130042e4ac9eSOr Ozeri 
130142e4ac9eSOr Ozeri     return spec_info;
130242e4ac9eSOr Ozeri }
130342e4ac9eSOr Ozeri 
1304ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1305f27aaf4bSChristian Brunner {
1306f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1307ad32e9c0SJosh Durgin     int r;
1308f27aaf4bSChristian Brunner 
1309*6d921418SPeter Lieven     r = rbd_get_size(s->image, &s->image_size);
1310ad32e9c0SJosh Durgin     if (r < 0) {
1311ad32e9c0SJosh Durgin         return r;
1312f27aaf4bSChristian Brunner     }
1313f27aaf4bSChristian Brunner 
1314*6d921418SPeter Lieven     return s->image_size;
1315ad32e9c0SJosh Durgin }
1316ad32e9c0SJosh Durgin 
1317061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1318061ca8a3SKevin Wolf                                              int64_t offset,
1319c80d8b06SMax Reitz                                              bool exact,
1320061ca8a3SKevin Wolf                                              PreallocMode prealloc,
132192b92799SKevin Wolf                                              BdrvRequestFlags flags,
1322061ca8a3SKevin Wolf                                              Error **errp)
132330cdc48cSJosh Durgin {
132430cdc48cSJosh Durgin     int r;
132530cdc48cSJosh Durgin 
13268243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
13278243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
1328977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
13298243ccb7SMax Reitz         return -ENOTSUP;
13308243ccb7SMax Reitz     }
13318243ccb7SMax Reitz 
1332d24f8023SStefano Garzarella     r = qemu_rbd_resize(bs, offset);
133330cdc48cSJosh Durgin     if (r < 0) {
1334f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
133530cdc48cSJosh Durgin         return r;
133630cdc48cSJosh Durgin     }
133730cdc48cSJosh Durgin 
133830cdc48cSJosh Durgin     return 0;
133930cdc48cSJosh Durgin }
134030cdc48cSJosh Durgin 
1341ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1342ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1343f27aaf4bSChristian Brunner {
1344f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1345f27aaf4bSChristian Brunner     int r;
1346f27aaf4bSChristian Brunner 
1347f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1348f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1349f27aaf4bSChristian Brunner     }
1350f27aaf4bSChristian Brunner 
1351f27aaf4bSChristian Brunner     /*
1352f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1353f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1354f27aaf4bSChristian Brunner      */
1355f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1356f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1357f27aaf4bSChristian Brunner         return -EINVAL;
1358f27aaf4bSChristian Brunner     }
1359f27aaf4bSChristian Brunner 
1360f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1361f27aaf4bSChristian Brunner         return -ERANGE;
1362f27aaf4bSChristian Brunner     }
1363f27aaf4bSChristian Brunner 
1364ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1365f27aaf4bSChristian Brunner     if (r < 0) {
1366ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1367f27aaf4bSChristian Brunner         return r;
1368f27aaf4bSChristian Brunner     }
1369f27aaf4bSChristian Brunner 
1370f27aaf4bSChristian Brunner     return 0;
1371f27aaf4bSChristian Brunner }
1372f27aaf4bSChristian Brunner 
1373bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1374a89d89d3SWenchao Xia                                 const char *snapshot_id,
1375a89d89d3SWenchao Xia                                 const char *snapshot_name,
1376a89d89d3SWenchao Xia                                 Error **errp)
1377bd603247SGregory Farnum {
1378bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1379bd603247SGregory Farnum     int r;
1380bd603247SGregory Farnum 
1381a89d89d3SWenchao Xia     if (!snapshot_name) {
1382a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1383a89d89d3SWenchao Xia         return -EINVAL;
1384a89d89d3SWenchao Xia     }
1385a89d89d3SWenchao Xia 
1386a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1387a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1388a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1389a89d89d3SWenchao Xia         error_setg(errp,
1390a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1391a89d89d3SWenchao Xia                    "equal to snapshot name");
1392a89d89d3SWenchao Xia         return -EINVAL;
1393a89d89d3SWenchao Xia     }
1394a89d89d3SWenchao Xia 
1395bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1396a89d89d3SWenchao Xia     if (r < 0) {
1397a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1398a89d89d3SWenchao Xia     }
1399bd603247SGregory Farnum     return r;
1400bd603247SGregory Farnum }
1401bd603247SGregory Farnum 
1402bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1403bd603247SGregory Farnum                                   const char *snapshot_name)
1404bd603247SGregory Farnum {
1405bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1406bd603247SGregory Farnum 
14079be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1408bd603247SGregory Farnum }
1409bd603247SGregory Farnum 
1410ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1411ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1412f27aaf4bSChristian Brunner {
1413f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1414f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1415ad32e9c0SJosh Durgin     int i, snap_count;
1416ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1417ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1418f27aaf4bSChristian Brunner 
1419ad32e9c0SJosh Durgin     do {
142002c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1421ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
14229e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
14237267c094SAnthony Liguori             g_free(snaps);
1424f27aaf4bSChristian Brunner         }
1425ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1426f27aaf4bSChristian Brunner 
1427ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1428b9c53290SJosh Durgin         goto done;
1429f27aaf4bSChristian Brunner     }
1430f27aaf4bSChristian Brunner 
14315839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1432f27aaf4bSChristian Brunner 
1433ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1434ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1435f27aaf4bSChristian Brunner 
1436f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1437f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1438f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1439f27aaf4bSChristian Brunner 
1440ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1441f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1442f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1443f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1444f27aaf4bSChristian Brunner     }
1445ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
14469e6337d0SStefan Hajnoczi     g_free(snaps);
1447ad32e9c0SJosh Durgin 
1448b9c53290SJosh Durgin  done:
1449f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1450f27aaf4bSChristian Brunner     return snap_count;
1451f27aaf4bSChristian Brunner }
1452f27aaf4bSChristian Brunner 
14534da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
14544da444a0SEric Blake                                          int64_t offset,
1455f5a5ca79SManos Pitsidianakis                                          int bytes,
1456097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1457787f3133SJosh Durgin                                          void *opaque)
1458787f3133SJosh Durgin {
1459f5a5ca79SManos Pitsidianakis     return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1460787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1461787f3133SJosh Durgin }
1462787f3133SJosh Durgin 
14632b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1464be217884SAdam Crume                                                       Error **errp)
1465be217884SAdam Crume {
1466be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1467be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1468be217884SAdam Crume     if (r < 0) {
1469be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1470be217884SAdam Crume     }
1471be217884SAdam Crume }
1472be217884SAdam Crume 
1473bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1474bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1475bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1476bd0cf596SChunyan Liu     .desc = {
1477f27aaf4bSChristian Brunner         {
1478f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1479bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1480f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1481f27aaf4bSChristian Brunner         },
1482f27aaf4bSChristian Brunner         {
1483f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1484bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1485f27aaf4bSChristian Brunner             .help = "RBD object size"
1486f27aaf4bSChristian Brunner         },
148760390a21SDaniel P. Berrange         {
148860390a21SDaniel P. Berrange             .name = "password-secret",
148960390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
149060390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
149160390a21SDaniel P. Berrange         },
149242e4ac9eSOr Ozeri         {
149342e4ac9eSOr Ozeri             .name = "encrypt.format",
149442e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
149542e4ac9eSOr Ozeri             .help = "Encrypt the image, format choices: 'luks', 'luks2'",
149642e4ac9eSOr Ozeri         },
149742e4ac9eSOr Ozeri         {
149842e4ac9eSOr Ozeri             .name = "encrypt.cipher-alg",
149942e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
150042e4ac9eSOr Ozeri             .help = "Name of encryption cipher algorithm"
150142e4ac9eSOr Ozeri                     " (allowed values: aes-128, aes-256)",
150242e4ac9eSOr Ozeri         },
150342e4ac9eSOr Ozeri         {
150442e4ac9eSOr Ozeri             .name = "encrypt.key-secret",
150542e4ac9eSOr Ozeri             .type = QEMU_OPT_STRING,
150642e4ac9eSOr Ozeri             .help = "ID of secret providing LUKS passphrase",
150742e4ac9eSOr Ozeri         },
1508bd0cf596SChunyan Liu         { /* end of list */ }
1509bd0cf596SChunyan Liu     }
1510f27aaf4bSChristian Brunner };
1511f27aaf4bSChristian Brunner 
15122654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = {
15132654267cSMax Reitz     "pool",
15147bae7c80SStefano Garzarella     "namespace",
15152654267cSMax Reitz     "image",
15162654267cSMax Reitz     "conf",
15172654267cSMax Reitz     "snapshot",
15182654267cSMax Reitz     "user",
15192654267cSMax Reitz     "server.",
15202654267cSMax Reitz     "password-secret",
15212654267cSMax Reitz 
15222654267cSMax Reitz     NULL
15232654267cSMax Reitz };
15242654267cSMax Reitz 
1525f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1526f27aaf4bSChristian Brunner     .format_name            = "rbd",
1527f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1528c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1529e8e16d4bSEric Blake     .bdrv_refresh_limits    = qemu_rbd_refresh_limits,
1530ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1531ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
153256e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
15331bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1534efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
15353ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1536ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
153742e4ac9eSOr Ozeri     .bdrv_get_specific_info = qemu_rbd_get_specific_info,
1538bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1539ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
1540061ca8a3SKevin Wolf     .bdrv_co_truncate       = qemu_rbd_co_truncate,
1541f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1542f27aaf4bSChristian Brunner 
1543e8e16d4bSEric Blake     .bdrv_aio_preadv        = qemu_rbd_aio_preadv,
1544e8e16d4bSEric Blake     .bdrv_aio_pwritev       = qemu_rbd_aio_pwritev,
1545dc7588c1SJosh Durgin 
1546dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
15474da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1548787f3133SJosh Durgin 
1549ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1550bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1551ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1552bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
15532b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
15542654267cSMax Reitz 
15552654267cSMax Reitz     .strong_runtime_opts    = qemu_rbd_strong_runtime_opts,
1556f27aaf4bSChristian Brunner };
1557f27aaf4bSChristian Brunner 
1558f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1559f27aaf4bSChristian Brunner {
1560f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1561f27aaf4bSChristian Brunner }
1562f27aaf4bSChristian Brunner 
1563f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1564