xref: /openbmc/qemu/block/rbd.c (revision f24b03b56cdb28d753b4ff9ae210d555f14cb0d8)
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"
19922a01a0SMarkus Armbruster #include "qemu/option.h"
20737e150eSPaolo Bonzini #include "block/block_int.h"
21609f45eaSMax Reitz #include "block/qdict.h"
2260390a21SDaniel P. Berrange #include "crypto/secret.h"
23f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
24c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
25452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
26e98c6961SEric Blake #include "qapi/qmp/qjson.h"
2747e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
284bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h"
294bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h"
30f27aaf4bSChristian Brunner 
31f27aaf4bSChristian Brunner /*
32f27aaf4bSChristian Brunner  * When specifying the image filename use:
33f27aaf4bSChristian Brunner  *
34fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
35f27aaf4bSChristian Brunner  *
369e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
37f27aaf4bSChristian Brunner  *
389e1fbcdeSSage Weil  * devicename is the name of the rbd image.
39f27aaf4bSChristian Brunner  *
409e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
419e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
42fab5cf59SJosh Durgin  *
439e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
449e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
459e1fbcdeSSage Weil  * (normally 'admin').
46f27aaf4bSChristian Brunner  *
479e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
489e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
499e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
509e1fbcdeSSage Weil  * file, specify conf=/dev/null.
51f27aaf4bSChristian Brunner  *
529e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
539e1fbcdeSSage Weil  * leading "\".
54f27aaf4bSChristian Brunner  */
55f27aaf4bSChristian Brunner 
56787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
57787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
58787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
59787f3133SJosh Durgin #else
60787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
61787f3133SJosh Durgin #endif
62787f3133SJosh Durgin 
63f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
64f27aaf4bSChristian Brunner 
65ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
66ad32e9c0SJosh Durgin 
671d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
681d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
691d393bdeStianqing #define LIBRBD_USE_IOVEC 1
701d393bdeStianqing #else
711d393bdeStianqing #define LIBRBD_USE_IOVEC 0
721d393bdeStianqing #endif
731d393bdeStianqing 
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;
85f27aaf4bSChristian Brunner     char *bounce;
86787f3133SJosh Durgin     RBDAIOCmd cmd;
87f27aaf4bSChristian Brunner     int error;
88f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
89f27aaf4bSChristian Brunner } RBDAIOCB;
90f27aaf4bSChristian Brunner 
91f27aaf4bSChristian Brunner typedef struct RADOSCB {
92f27aaf4bSChristian Brunner     RBDAIOCB *acb;
93f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
94ad32e9c0SJosh Durgin     int64_t size;
95f27aaf4bSChristian Brunner     char *buf;
9608448d51SStefan Priebe     int64_t ret;
97f27aaf4bSChristian Brunner } RADOSCB;
98f27aaf4bSChristian Brunner 
99f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
100ad32e9c0SJosh Durgin     rados_t cluster;
101ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
102ad32e9c0SJosh Durgin     rbd_image_t image;
10380b61a27SJeff Cody     char *image_name;
104ad32e9c0SJosh Durgin     char *snap;
105f27aaf4bSChristian Brunner } BDRVRBDState;
106f27aaf4bSChristian Brunner 
107aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
108aa045c2dSKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
109aa045c2dSKevin Wolf                             const char *keypairs, const char *secretid,
110aa045c2dSKevin Wolf                             Error **errp);
111aa045c2dSKevin Wolf 
112730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
113f27aaf4bSChristian Brunner {
114f27aaf4bSChristian Brunner     char *end;
115f27aaf4bSChristian Brunner 
116f27aaf4bSChristian Brunner     *p = NULL;
117f27aaf4bSChristian Brunner 
11816a06b24SSage Weil     for (end = src; *end; ++end) {
11916a06b24SSage Weil         if (*end == delim) {
12016a06b24SSage Weil             break;
12116a06b24SSage Weil         }
12216a06b24SSage Weil         if (*end == '\\' && end[1] != '\0') {
12316a06b24SSage Weil             end++;
12416a06b24SSage Weil         }
12516a06b24SSage Weil     }
12616a06b24SSage Weil     if (*end == delim) {
127f27aaf4bSChristian Brunner         *p = end + 1;
128f27aaf4bSChristian Brunner         *end = '\0';
129f27aaf4bSChristian Brunner     }
1307830f909SJeff Cody     return src;
131f27aaf4bSChristian Brunner }
132f27aaf4bSChristian Brunner 
13316a06b24SSage Weil static void qemu_rbd_unescape(char *src)
13416a06b24SSage Weil {
13516a06b24SSage Weil     char *p;
13616a06b24SSage Weil 
13716a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
13816a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
13916a06b24SSage Weil             src++;
14016a06b24SSage Weil         }
14116a06b24SSage Weil         *p = *src;
14216a06b24SSage Weil     }
14316a06b24SSage Weil     *p = '\0';
14416a06b24SSage Weil }
14516a06b24SSage Weil 
146c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
147d61563b2SMarkus Armbruster                                     Error **errp)
148f27aaf4bSChristian Brunner {
149f27aaf4bSChristian Brunner     const char *start;
150e98c6961SEric Blake     char *p, *buf;
151e98c6961SEric Blake     QList *keypairs = NULL;
1527830f909SJeff Cody     char *found_str;
153f27aaf4bSChristian Brunner 
154f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
155d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
156c7cacb3eSJeff Cody         return;
157f27aaf4bSChristian Brunner     }
158f27aaf4bSChristian Brunner 
1597267c094SAnthony Liguori     buf = g_strdup(start);
160f27aaf4bSChristian Brunner     p = buf;
161f27aaf4bSChristian Brunner 
162730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1637830f909SJeff Cody     if (!p) {
1647830f909SJeff Cody         error_setg(errp, "Pool name is required");
1657830f909SJeff Cody         goto done;
1667830f909SJeff Cody     }
1677830f909SJeff Cody     qemu_rbd_unescape(found_str);
16846f5ac20SEric Blake     qdict_put_str(options, "pool", found_str);
169fab5cf59SJosh Durgin 
170fab5cf59SJosh Durgin     if (strchr(p, '@')) {
171730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, '@', &p);
1727830f909SJeff Cody         qemu_rbd_unescape(found_str);
17346f5ac20SEric Blake         qdict_put_str(options, "image", found_str);
1747830f909SJeff Cody 
175730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1767830f909SJeff Cody         qemu_rbd_unescape(found_str);
17746f5ac20SEric Blake         qdict_put_str(options, "snapshot", found_str);
1787830f909SJeff Cody     } else {
179730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1807830f909SJeff Cody         qemu_rbd_unescape(found_str);
18146f5ac20SEric Blake         qdict_put_str(options, "image", found_str);
1827830f909SJeff Cody     }
1837830f909SJeff Cody     if (!p) {
184f27aaf4bSChristian Brunner         goto done;
185f27aaf4bSChristian Brunner     }
186f27aaf4bSChristian Brunner 
187c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
188c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
189c7cacb3eSJeff Cody     while (p) {
190c7cacb3eSJeff Cody         char *name, *value;
191730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
192c7cacb3eSJeff Cody         if (!p) {
193c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
194c7cacb3eSJeff Cody             break;
195c7cacb3eSJeff Cody         }
196c7cacb3eSJeff Cody 
197c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
198c7cacb3eSJeff Cody 
199730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
200c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
201c7cacb3eSJeff Cody 
202c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
20346f5ac20SEric Blake             qdict_put_str(options, "conf", value);
204c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
20546f5ac20SEric Blake             qdict_put_str(options, "user", value);
206c7cacb3eSJeff Cody         } else {
207e98c6961SEric Blake             /*
208e98c6961SEric Blake              * We pass these internally to qemu_rbd_set_keypairs(), so
209e98c6961SEric Blake              * we can get away with the simpler list of [ "key1",
210e98c6961SEric Blake              * "value1", "key2", "value2" ] rather than a raw dict
211e98c6961SEric Blake              * { "key1": "value1", "key2": "value2" } where we can't
212e98c6961SEric Blake              * guarantee order, or even a more correct but complex
213e98c6961SEric Blake              * [ { "key1": "value1" }, { "key2": "value2" } ]
214e98c6961SEric Blake              */
215e98c6961SEric Blake             if (!keypairs) {
216e98c6961SEric Blake                 keypairs = qlist_new();
217c7cacb3eSJeff Cody             }
21846f5ac20SEric Blake             qlist_append_str(keypairs, name);
21946f5ac20SEric Blake             qlist_append_str(keypairs, value);
220c7cacb3eSJeff Cody         }
221c7cacb3eSJeff Cody     }
222c7cacb3eSJeff Cody 
223e98c6961SEric Blake     if (keypairs) {
224e98c6961SEric Blake         qdict_put(options, "=keyvalue-pairs",
225e98c6961SEric Blake                   qobject_to_json(QOBJECT(keypairs)));
226c7cacb3eSJeff Cody     }
227c7cacb3eSJeff Cody 
228f27aaf4bSChristian Brunner done:
2297267c094SAnthony Liguori     g_free(buf);
230cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
231c7cacb3eSJeff Cody     return;
2327c7e9df0SSage Weil }
2337c7e9df0SSage Weil 
23460390a21SDaniel P. Berrange 
235e8e16d4bSEric Blake static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
236e8e16d4bSEric Blake {
237e8e16d4bSEric Blake     /* XXX Does RBD support AIO on less than 512-byte alignment? */
238e8e16d4bSEric Blake     bs->bl.request_alignment = 512;
239e8e16d4bSEric Blake }
240e8e16d4bSEric Blake 
241e8e16d4bSEric Blake 
242d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
24360390a21SDaniel P. Berrange                              Error **errp)
24460390a21SDaniel P. Berrange {
245d083f954SMarkus Armbruster     char *key, *acr;
246a3699de4SMarkus Armbruster     int r;
247a3699de4SMarkus Armbruster     GString *accu;
248a3699de4SMarkus Armbruster     RbdAuthModeList *auth;
24960390a21SDaniel P. Berrange 
250d083f954SMarkus Armbruster     if (opts->key_secret) {
251d083f954SMarkus Armbruster         key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
252d083f954SMarkus Armbruster         if (!key) {
253d083f954SMarkus Armbruster             return -EIO;
25460390a21SDaniel P. Berrange         }
255d083f954SMarkus Armbruster         r = rados_conf_set(cluster, "key", key);
256d083f954SMarkus Armbruster         g_free(key);
257d083f954SMarkus Armbruster         if (r < 0) {
258d083f954SMarkus Armbruster             error_setg_errno(errp, -r, "Could not set 'key'");
259d083f954SMarkus Armbruster             return r;
260d083f954SMarkus Armbruster         }
261a3699de4SMarkus Armbruster     }
262a3699de4SMarkus Armbruster 
263a3699de4SMarkus Armbruster     if (opts->has_auth_client_required) {
264a3699de4SMarkus Armbruster         accu = g_string_new("");
265a3699de4SMarkus Armbruster         for (auth = opts->auth_client_required; auth; auth = auth->next) {
266a3699de4SMarkus Armbruster             if (accu->str[0]) {
267a3699de4SMarkus Armbruster                 g_string_append_c(accu, ';');
268a3699de4SMarkus Armbruster             }
269a3699de4SMarkus Armbruster             g_string_append(accu, RbdAuthMode_str(auth->value));
270a3699de4SMarkus Armbruster         }
271a3699de4SMarkus Armbruster         acr = g_string_free(accu, FALSE);
272a3699de4SMarkus Armbruster         r = rados_conf_set(cluster, "auth_client_required", acr);
273a3699de4SMarkus Armbruster         g_free(acr);
274a3699de4SMarkus Armbruster         if (r < 0) {
275a3699de4SMarkus Armbruster             error_setg_errno(errp, -r,
276a3699de4SMarkus Armbruster                              "Could not set 'auth_client_required'");
277a3699de4SMarkus Armbruster             return r;
278a3699de4SMarkus Armbruster         }
279a3699de4SMarkus Armbruster     }
28060390a21SDaniel P. Berrange 
28160390a21SDaniel P. Berrange     return 0;
28260390a21SDaniel P. Berrange }
28360390a21SDaniel P. Berrange 
284e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
285e34d8f29SJosh Durgin                                  Error **errp)
286fab5cf59SJosh Durgin {
287e98c6961SEric Blake     QList *keypairs;
288e98c6961SEric Blake     QString *name;
289e98c6961SEric Blake     QString *value;
290e98c6961SEric Blake     const char *key;
291e98c6961SEric Blake     size_t remaining;
292fab5cf59SJosh Durgin     int ret = 0;
293fab5cf59SJosh Durgin 
294e98c6961SEric Blake     if (!keypairs_json) {
295e98c6961SEric Blake         return ret;
296fab5cf59SJosh Durgin     }
2977dc847ebSMax Reitz     keypairs = qobject_to(QList,
2987dc847ebSMax Reitz                           qobject_from_json(keypairs_json, &error_abort));
299e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
300e98c6961SEric Blake     assert(remaining);
301fab5cf59SJosh Durgin 
302e98c6961SEric Blake     while (remaining--) {
3037dc847ebSMax Reitz         name = qobject_to(QString, qlist_pop(keypairs));
3047dc847ebSMax Reitz         value = qobject_to(QString, qlist_pop(keypairs));
305e98c6961SEric Blake         assert(name && value);
306e98c6961SEric Blake         key = qstring_get_str(name);
307fab5cf59SJosh Durgin 
308e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
309cb3e7f08SMarc-André Lureau         qobject_unref(value);
310fab5cf59SJosh Durgin         if (ret < 0) {
311e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
312cb3e7f08SMarc-André Lureau             qobject_unref(name);
313fab5cf59SJosh Durgin             ret = -EINVAL;
314fab5cf59SJosh Durgin             break;
315fab5cf59SJosh Durgin         }
316cb3e7f08SMarc-André Lureau         qobject_unref(name);
317fab5cf59SJosh Durgin     }
318fab5cf59SJosh Durgin 
319cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
320fab5cf59SJosh Durgin     return ret;
321fab5cf59SJosh Durgin }
322fab5cf59SJosh Durgin 
3231d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
3241d393bdeStianqing {
3251d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
3261d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
3271d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
3281d393bdeStianqing                    acb->qiov->size - offs);
3291d393bdeStianqing     } else {
3301d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
3311d393bdeStianqing     }
3321d393bdeStianqing }
3331d393bdeStianqing 
3340f9d252dSJeff Cody static QemuOptsList runtime_opts = {
3350f9d252dSJeff Cody     .name = "rbd",
3360f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3370f9d252dSJeff Cody     .desc = {
3380f9d252dSJeff Cody         {
3390f9d252dSJeff Cody             .name = "pool",
3400f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3410f9d252dSJeff Cody             .help = "Rados pool name",
3420f9d252dSJeff Cody         },
3430f9d252dSJeff Cody         {
3440f9d252dSJeff Cody             .name = "image",
3450f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3460f9d252dSJeff Cody             .help = "Image name in the pool",
3470f9d252dSJeff Cody         },
3480f9d252dSJeff Cody         {
349cbf036b4SMarkus Armbruster             .name = "conf",
350cbf036b4SMarkus Armbruster             .type = QEMU_OPT_STRING,
351cbf036b4SMarkus Armbruster             .help = "Rados config file location",
352cbf036b4SMarkus Armbruster         },
353cbf036b4SMarkus Armbruster         {
3540f9d252dSJeff Cody             .name = "snapshot",
3550f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3560f9d252dSJeff Cody             .help = "Ceph snapshot name",
3570f9d252dSJeff Cody         },
3580f9d252dSJeff Cody         {
3590f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3600f9d252dSJeff Cody             .name = "user",
3610f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3620f9d252dSJeff Cody             .help = "Rados id name",
3630f9d252dSJeff Cody         },
364cbf036b4SMarkus Armbruster         /*
3652836284dSMarkus Armbruster          * server.* extracted manually, see qemu_rbd_mon_host()
366cbf036b4SMarkus Armbruster          */
3670f9d252dSJeff Cody         { /* end of list */ }
3680f9d252dSJeff Cody     },
3690f9d252dSJeff Cody };
3700f9d252dSJeff Cody 
371d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */
3721bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
3731bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
3741bebea37SKevin Wolf                               Error **errp)
3751bebea37SKevin Wolf {
3761bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
3771bebea37SKevin Wolf     rados_t cluster;
3781bebea37SKevin Wolf     rados_ioctx_t io_ctx;
3791bebea37SKevin Wolf     int obj_order = 0;
3801bebea37SKevin Wolf     int ret;
3811bebea37SKevin Wolf 
3821bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
3831bebea37SKevin Wolf     if (opts->location->has_snapshot) {
3841bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
3851bebea37SKevin Wolf         return -EINVAL;
3861bebea37SKevin Wolf     }
3871bebea37SKevin Wolf 
3881bebea37SKevin Wolf     if (opts->has_cluster_size) {
3891bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
3901bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
3911bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
3921bebea37SKevin Wolf             return -EINVAL;
3931bebea37SKevin Wolf         }
3941bebea37SKevin Wolf         if (objsize < 4096) {
3951bebea37SKevin Wolf             error_setg(errp, "obj size too small");
3961bebea37SKevin Wolf             return -EINVAL;
3971bebea37SKevin Wolf         }
3981bebea37SKevin Wolf         obj_order = ctz32(objsize);
3991bebea37SKevin Wolf     }
4001bebea37SKevin Wolf 
401aa045c2dSKevin Wolf     ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
402aa045c2dSKevin Wolf                            password_secret, errp);
4031bebea37SKevin Wolf     if (ret < 0) {
4041bebea37SKevin Wolf         return ret;
4051bebea37SKevin Wolf     }
4061bebea37SKevin Wolf 
4071bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
4081bebea37SKevin Wolf     if (ret < 0) {
4091bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
410aa045c2dSKevin Wolf         goto out;
4111bebea37SKevin Wolf     }
4121bebea37SKevin Wolf 
4131bebea37SKevin Wolf     ret = 0;
414aa045c2dSKevin Wolf out:
415aa045c2dSKevin Wolf     rados_ioctx_destroy(io_ctx);
4161bebea37SKevin Wolf     rados_shutdown(cluster);
4171bebea37SKevin Wolf     return ret;
4181bebea37SKevin Wolf }
4191bebea37SKevin Wolf 
4201bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
4211bebea37SKevin Wolf {
4221bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
4231bebea37SKevin Wolf }
4241bebea37SKevin Wolf 
425efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
426efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
427efc75e2aSStefan Hajnoczi                                                 Error **errp)
428f27aaf4bSChristian Brunner {
4291bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
4301bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
4311bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
432d61563b2SMarkus Armbruster     Error *local_err = NULL;
4331bebea37SKevin Wolf     const char *keypairs, *password_secret;
434c7cacb3eSJeff Cody     QDict *options = NULL;
435c7cacb3eSJeff Cody     int ret = 0;
436f27aaf4bSChristian Brunner 
4371bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
4381bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
4391bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
4401bebea37SKevin Wolf 
4411bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
4421bebea37SKevin Wolf 
4431bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
44460390a21SDaniel P. Berrange 
445f27aaf4bSChristian Brunner     /* Read out options */
4461bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
447c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
4481bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
4491bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
4501bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
451f27aaf4bSChristian Brunner 
452c7cacb3eSJeff Cody     options = qdict_new();
453c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
454c7cacb3eSJeff Cody     if (local_err) {
455c7cacb3eSJeff Cody         ret = -EINVAL;
456c7cacb3eSJeff Cody         error_propagate(errp, local_err);
457c7cacb3eSJeff Cody         goto exit;
458c7cacb3eSJeff Cody     }
459c7cacb3eSJeff Cody 
460129c7d1cSMarkus Armbruster     /*
461129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
462129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
463129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
464129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
465129c7d1cSMarkus Armbruster      */
4661bebea37SKevin Wolf     loc = rbd_opts->location;
4671bebea37SKevin Wolf     loc->pool     = g_strdup(qdict_get_try_str(options, "pool"));
4681bebea37SKevin Wolf     loc->conf     = g_strdup(qdict_get_try_str(options, "conf"));
4691bebea37SKevin Wolf     loc->has_conf = !!loc->conf;
4701bebea37SKevin Wolf     loc->user     = g_strdup(qdict_get_try_str(options, "user"));
4711bebea37SKevin Wolf     loc->has_user = !!loc->user;
4721bebea37SKevin Wolf     loc->image    = g_strdup(qdict_get_try_str(options, "image"));
47307846397SMarkus Armbruster     keypairs      = qdict_get_try_str(options, "=keyvalue-pairs");
474c7cacb3eSJeff Cody 
4751bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
47687cd3d20SVikhyat Umrao     if (ret < 0) {
477c7cacb3eSJeff Cody         goto exit;
478f27aaf4bSChristian Brunner     }
479f27aaf4bSChristian Brunner 
480c7cacb3eSJeff Cody exit:
481cb3e7f08SMarc-André Lureau     qobject_unref(options);
4821bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
483f27aaf4bSChristian Brunner     return ret;
484f27aaf4bSChristian Brunner }
485f27aaf4bSChristian Brunner 
486f27aaf4bSChristian Brunner /*
487e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
488e04fb07fSStefan Hajnoczi  * BH context.
489f27aaf4bSChristian Brunner  */
490ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
491f27aaf4bSChristian Brunner {
492f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
493f27aaf4bSChristian Brunner     int64_t r;
494f27aaf4bSChristian Brunner 
495f27aaf4bSChristian Brunner     r = rcb->ret;
496f27aaf4bSChristian Brunner 
497dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
498f27aaf4bSChristian Brunner         if (r < 0) {
499f27aaf4bSChristian Brunner             acb->ret = r;
500f27aaf4bSChristian Brunner             acb->error = 1;
501f27aaf4bSChristian Brunner         } else if (!acb->error) {
502ad32e9c0SJosh Durgin             acb->ret = rcb->size;
503f27aaf4bSChristian Brunner         }
504f27aaf4bSChristian Brunner     } else {
505ad32e9c0SJosh Durgin         if (r < 0) {
5061d393bdeStianqing             qemu_rbd_memset(rcb, 0);
507f27aaf4bSChristian Brunner             acb->ret = r;
508f27aaf4bSChristian Brunner             acb->error = 1;
509ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
5101d393bdeStianqing             qemu_rbd_memset(rcb, r);
511f27aaf4bSChristian Brunner             if (!acb->error) {
512ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
513f27aaf4bSChristian Brunner             }
514f27aaf4bSChristian Brunner         } else if (!acb->error) {
515ad32e9c0SJosh Durgin             acb->ret = r;
516f27aaf4bSChristian Brunner         }
517f27aaf4bSChristian Brunner     }
518e04fb07fSStefan Hajnoczi 
5197267c094SAnthony Liguori     g_free(rcb);
520e04fb07fSStefan Hajnoczi 
5211d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
522e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
523e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
524f27aaf4bSChristian Brunner         }
525e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5261d393bdeStianqing     }
5271d393bdeStianqing 
528e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
529f27aaf4bSChristian Brunner 
5308007429aSFam Zheng     qemu_aio_unref(acb);
531f27aaf4bSChristian Brunner }
532f27aaf4bSChristian Brunner 
5334bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
5340a55679bSJeff Cody {
5354bfb2741SKevin Wolf     const char **vals;
5362836284dSMarkus Armbruster     const char *host, *port;
5372836284dSMarkus Armbruster     char *rados_str;
5384bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
5394bfb2741SKevin Wolf     int i, cnt;
5400a55679bSJeff Cody 
5414bfb2741SKevin Wolf     if (!opts->has_server) {
5424bfb2741SKevin Wolf         return NULL;
5430a55679bSJeff Cody     }
5444bfb2741SKevin Wolf 
5454bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
5464bfb2741SKevin Wolf         cnt++;
5470a55679bSJeff Cody     }
5480a55679bSJeff Cody 
5494bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
5504bfb2741SKevin Wolf 
5514bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
5524bfb2741SKevin Wolf         host = p->value->host;
5534bfb2741SKevin Wolf         port = p->value->port;
5544bfb2741SKevin Wolf 
5550a55679bSJeff Cody         if (strchr(host, ':')) {
5564bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
5570a55679bSJeff Cody         } else {
5584bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
5590a55679bSJeff Cody         }
5600a55679bSJeff Cody     }
5612836284dSMarkus Armbruster     vals[i] = NULL;
5620a55679bSJeff Cody 
5632836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
5642836284dSMarkus Armbruster     g_strfreev((char **)vals);
5650a55679bSJeff Cody     return rados_str;
5660a55679bSJeff Cody }
5670a55679bSJeff Cody 
5683d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
5694bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
5704ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
5714ff45049SKevin Wolf                             Error **errp)
572f27aaf4bSChristian Brunner {
5730a55679bSJeff Cody     char *mon_host = NULL;
5743d9136f9SKevin Wolf     Error *local_err = NULL;
575f27aaf4bSChristian Brunner     int r;
576f27aaf4bSChristian Brunner 
577d083f954SMarkus Armbruster     if (secretid) {
578d083f954SMarkus Armbruster         if (opts->key_secret) {
579d083f954SMarkus Armbruster             error_setg(errp,
580d083f954SMarkus Armbruster                        "Legacy 'password-secret' clashes with 'key-secret'");
581d083f954SMarkus Armbruster             return -EINVAL;
582d083f954SMarkus Armbruster         }
583d083f954SMarkus Armbruster         opts->key_secret = g_strdup(secretid);
584d083f954SMarkus Armbruster         opts->has_key_secret = true;
585d083f954SMarkus Armbruster     }
586d083f954SMarkus Armbruster 
5874bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
58884d18f06SMarkus Armbruster     if (local_err) {
589d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
5902836284dSMarkus Armbruster         r = -EINVAL;
5912836284dSMarkus Armbruster         goto failed_opts;
592a9ccedc3SKevin Wolf     }
593a9ccedc3SKevin Wolf 
5944bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
595ad32e9c0SJosh Durgin     if (r < 0) {
59687cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
597c3ca988dSKevin Wolf         goto failed_opts;
598f27aaf4bSChristian Brunner     }
599f27aaf4bSChristian Brunner 
600c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
6014bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
6024bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
6034bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
604e34d8f29SJosh Durgin         goto failed_shutdown;
605e34d8f29SJosh Durgin     }
60699a3c89dSJosh Durgin 
6073d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
60899a3c89dSJosh Durgin     if (r < 0) {
60999a3c89dSJosh Durgin         goto failed_shutdown;
61099a3c89dSJosh Durgin     }
61199a3c89dSJosh Durgin 
6120a55679bSJeff Cody     if (mon_host) {
6133d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
6140a55679bSJeff Cody         if (r < 0) {
6150a55679bSJeff Cody             goto failed_shutdown;
6160a55679bSJeff Cody         }
6170a55679bSJeff Cody     }
6180a55679bSJeff Cody 
619d083f954SMarkus Armbruster     r = qemu_rbd_set_auth(*cluster, opts, errp);
620d083f954SMarkus Armbruster     if (r < 0) {
62160390a21SDaniel P. Berrange         goto failed_shutdown;
62260390a21SDaniel P. Berrange     }
62360390a21SDaniel P. Berrange 
624b11f38fcSJosh Durgin     /*
625b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
626b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
627b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
628b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
629b11f38fcSJosh Durgin      * be set up, fall back to no caching.
630b11f38fcSJosh Durgin      */
6313d9136f9SKevin Wolf     if (cache) {
6323d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
633b11f38fcSJosh Durgin     } else {
6343d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
635b11f38fcSJosh Durgin     }
636b11f38fcSJosh Durgin 
6373d9136f9SKevin Wolf     r = rados_connect(*cluster);
638ad32e9c0SJosh Durgin     if (r < 0) {
63987cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
640eb93d5d9SSage Weil         goto failed_shutdown;
641ad32e9c0SJosh Durgin     }
642ad32e9c0SJosh Durgin 
6434bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
644ad32e9c0SJosh Durgin     if (r < 0) {
6454bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
646eb93d5d9SSage Weil         goto failed_shutdown;
647ad32e9c0SJosh Durgin     }
648ad32e9c0SJosh Durgin 
6493d9136f9SKevin Wolf     return 0;
6503d9136f9SKevin Wolf 
6513d9136f9SKevin Wolf failed_shutdown:
6523d9136f9SKevin Wolf     rados_shutdown(*cluster);
6533d9136f9SKevin Wolf failed_opts:
6543d9136f9SKevin Wolf     g_free(mon_host);
6553d9136f9SKevin Wolf     return r;
6563d9136f9SKevin Wolf }
6573d9136f9SKevin Wolf 
658*f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
659*f24b03b5SJeff Cody                                     Error **errp)
660*f24b03b5SJeff Cody {
661*f24b03b5SJeff Cody     Visitor *v;
662*f24b03b5SJeff Cody     Error *local_err = NULL;
663*f24b03b5SJeff Cody 
664*f24b03b5SJeff Cody     /* Convert the remaining options into a QAPI object */
665*f24b03b5SJeff Cody     v = qobject_input_visitor_new_flat_confused(options, errp);
666*f24b03b5SJeff Cody     if (!v) {
667*f24b03b5SJeff Cody         return -EINVAL;
668*f24b03b5SJeff Cody     }
669*f24b03b5SJeff Cody 
670*f24b03b5SJeff Cody     visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err);
671*f24b03b5SJeff Cody     visit_free(v);
672*f24b03b5SJeff Cody 
673*f24b03b5SJeff Cody     if (local_err) {
674*f24b03b5SJeff Cody         error_propagate(errp, local_err);
675*f24b03b5SJeff Cody         return -EINVAL;
676*f24b03b5SJeff Cody     }
677*f24b03b5SJeff Cody 
678*f24b03b5SJeff Cody     return 0;
679*f24b03b5SJeff Cody }
680*f24b03b5SJeff Cody 
6813d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
6823d9136f9SKevin Wolf                          Error **errp)
6833d9136f9SKevin Wolf {
6843d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
6854bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
686bfb15b4bSJeff Cody     const QDictEntry *e;
6873d9136f9SKevin Wolf     Error *local_err = NULL;
6884ff45049SKevin Wolf     char *keypairs, *secretid;
6893d9136f9SKevin Wolf     int r;
6903d9136f9SKevin Wolf 
6914ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
6924ff45049SKevin Wolf     if (keypairs) {
6934ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
6944ff45049SKevin Wolf     }
6954ff45049SKevin Wolf 
6964ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
6974ff45049SKevin Wolf     if (secretid) {
6984ff45049SKevin Wolf         qdict_del(options, "password-secret");
6994ff45049SKevin Wolf     }
7004ff45049SKevin Wolf 
701*f24b03b5SJeff Cody     r = qemu_rbd_convert_options(options, &opts, &local_err);
7024bfb2741SKevin Wolf     if (local_err) {
7034bfb2741SKevin Wolf         error_propagate(errp, local_err);
7044bfb2741SKevin Wolf         goto out;
7054bfb2741SKevin Wolf     }
7064bfb2741SKevin Wolf 
707bfb15b4bSJeff Cody     /* Remove the processed options from the QDict (the visitor processes
708bfb15b4bSJeff Cody      * _all_ options in the QDict) */
709bfb15b4bSJeff Cody     while ((e = qdict_first(options))) {
710bfb15b4bSJeff Cody         qdict_del(options, e->key);
711bfb15b4bSJeff Cody     }
712bfb15b4bSJeff Cody 
713d41a5588SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
714d41a5588SKevin Wolf                          !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
7153d9136f9SKevin Wolf     if (r < 0) {
7164ff45049SKevin Wolf         goto out;
7173d9136f9SKevin Wolf     }
7183d9136f9SKevin Wolf 
719d41a5588SKevin Wolf     s->snap = g_strdup(opts->snapshot);
720d41a5588SKevin Wolf     s->image_name = g_strdup(opts->image);
721d41a5588SKevin Wolf 
722e2b8247aSJeff Cody     /* rbd_open is always r/w */
72380b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
724ad32e9c0SJosh Durgin     if (r < 0) {
72580b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
72680b61a27SJeff Cody                          s->image_name);
727eb93d5d9SSage Weil         goto failed_open;
728ad32e9c0SJosh Durgin     }
729ad32e9c0SJosh Durgin 
730e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
731e2b8247aSJeff Cody      * leave as-is */
732e2b8247aSJeff Cody     if (s->snap != NULL) {
733398e6ad0SKevin Wolf         if (!bdrv_is_read_only(bs)) {
734398e6ad0SKevin Wolf             error_report("Opening rbd snapshots without an explicit "
735398e6ad0SKevin Wolf                          "read-only=on option is deprecated. Future versions "
736398e6ad0SKevin Wolf                          "will refuse to open the image instead of "
737398e6ad0SKevin Wolf                          "automatically marking the image read-only.");
738e2b8247aSJeff Cody             r = bdrv_set_read_only(bs, true, &local_err);
739e2b8247aSJeff Cody             if (r < 0) {
740e2b8247aSJeff Cody                 error_propagate(errp, local_err);
741e2b8247aSJeff Cody                 goto failed_open;
742e2b8247aSJeff Cody             }
743e2b8247aSJeff Cody         }
744398e6ad0SKevin Wolf     }
745f27aaf4bSChristian Brunner 
7464ff45049SKevin Wolf     r = 0;
7474ff45049SKevin Wolf     goto out;
748f27aaf4bSChristian Brunner 
749eb93d5d9SSage Weil failed_open:
750ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
751eb93d5d9SSage Weil     g_free(s->snap);
75280b61a27SJeff Cody     g_free(s->image_name);
7533d9136f9SKevin Wolf     rados_shutdown(s->cluster);
7544ff45049SKevin Wolf out:
7554bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
7564ff45049SKevin Wolf     g_free(keypairs);
7574ff45049SKevin Wolf     g_free(secretid);
758f27aaf4bSChristian Brunner     return r;
759f27aaf4bSChristian Brunner }
760f27aaf4bSChristian Brunner 
76156e7cf8dSJeff Cody 
76256e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
76356e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
76456e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
76556e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
76656e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
76756e7cf8dSJeff Cody {
76856e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
76956e7cf8dSJeff Cody     int ret = 0;
77056e7cf8dSJeff Cody 
77156e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
77256e7cf8dSJeff Cody         error_setg(errp,
77356e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
77456e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
77556e7cf8dSJeff Cody         ret = -EINVAL;
77656e7cf8dSJeff Cody     }
77756e7cf8dSJeff Cody 
77856e7cf8dSJeff Cody     return ret;
77956e7cf8dSJeff Cody }
78056e7cf8dSJeff Cody 
781ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
782f27aaf4bSChristian Brunner {
783f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
784f27aaf4bSChristian Brunner 
785ad32e9c0SJosh Durgin     rbd_close(s->image);
786ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
7877267c094SAnthony Liguori     g_free(s->snap);
78880b61a27SJeff Cody     g_free(s->image_name);
789ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
790f27aaf4bSChristian Brunner }
791f27aaf4bSChristian Brunner 
792d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
793f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
794f27aaf4bSChristian Brunner };
795f27aaf4bSChristian Brunner 
796e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
797f27aaf4bSChristian Brunner {
798e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
799e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
800ad32e9c0SJosh Durgin }
801ad32e9c0SJosh Durgin 
802ad32e9c0SJosh Durgin /*
803ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
804ad32e9c0SJosh Durgin  *
805ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
806ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
807e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
808e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
809ad32e9c0SJosh Durgin  */
810ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
811ad32e9c0SJosh Durgin {
812e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
813e04fb07fSStefan Hajnoczi 
814ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
815ad32e9c0SJosh Durgin     rbd_aio_release(c);
816f27aaf4bSChristian Brunner 
817fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
818ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
819473c7f02SStefan Priebe }
820f27aaf4bSChristian Brunner 
821787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
822787f3133SJosh Durgin                                    uint64_t off,
823787f3133SJosh Durgin                                    uint64_t len,
824787f3133SJosh Durgin                                    rbd_completion_t comp)
825787f3133SJosh Durgin {
826787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
827787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
828787f3133SJosh Durgin #else
829787f3133SJosh Durgin     return -ENOTSUP;
830787f3133SJosh Durgin #endif
831787f3133SJosh Durgin }
832787f3133SJosh Durgin 
833dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
834dc7588c1SJosh Durgin                                  rbd_completion_t comp)
835dc7588c1SJosh Durgin {
836dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
837dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
838dc7588c1SJosh Durgin #else
839dc7588c1SJosh Durgin     return -ENOTSUP;
840dc7588c1SJosh Durgin #endif
841dc7588c1SJosh Durgin }
842dc7588c1SJosh Durgin 
8437c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8447bbca9e2SEric Blake                                  int64_t off,
845f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8467bbca9e2SEric Blake                                  int64_t size,
847097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
848787f3133SJosh Durgin                                  void *opaque,
849787f3133SJosh Durgin                                  RBDAIOCmd cmd)
850f27aaf4bSChristian Brunner {
851f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8520f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
853ad32e9c0SJosh Durgin     rbd_completion_t c;
85451a13528SJosh Durgin     int r;
855f27aaf4bSChristian Brunner 
856f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
857f27aaf4bSChristian Brunner 
858d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
859787f3133SJosh Durgin     acb->cmd = cmd;
860f27aaf4bSChristian Brunner     acb->qiov = qiov;
8617bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8621d393bdeStianqing 
8631d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8641d393bdeStianqing 
8651d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
866dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
867787f3133SJosh Durgin             acb->bounce = NULL;
868787f3133SJosh Durgin         } else {
8690f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8700f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8710f7a0237SKevin Wolf                 goto failed;
8720f7a0237SKevin Wolf             }
873787f3133SJosh Durgin         }
8741d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
8751d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
8761d393bdeStianqing         }
8771d393bdeStianqing         rcb->buf = acb->bounce;
8781d393bdeStianqing     }
8791d393bdeStianqing 
880f27aaf4bSChristian Brunner     acb->ret = 0;
881f27aaf4bSChristian Brunner     acb->error = 0;
882f27aaf4bSChristian Brunner     acb->s = s;
883f27aaf4bSChristian Brunner 
884f27aaf4bSChristian Brunner     rcb->acb = acb;
885f27aaf4bSChristian Brunner     rcb->s = acb->s;
886ad32e9c0SJosh Durgin     rcb->size = size;
88751a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
88851a13528SJosh Durgin     if (r < 0) {
88951a13528SJosh Durgin         goto failed;
89051a13528SJosh Durgin     }
891f27aaf4bSChristian Brunner 
892787f3133SJosh Durgin     switch (cmd) {
893787f3133SJosh Durgin     case RBD_AIO_WRITE:
8941d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8951d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
8961d393bdeStianqing #else
8971d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
8981d393bdeStianqing #endif
899787f3133SJosh Durgin         break;
900787f3133SJosh Durgin     case RBD_AIO_READ:
9011d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9021d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
9031d393bdeStianqing #else
9041d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
9051d393bdeStianqing #endif
906787f3133SJosh Durgin         break;
907787f3133SJosh Durgin     case RBD_AIO_DISCARD:
908787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
909787f3133SJosh Durgin         break;
910dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
911dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
912dc7588c1SJosh Durgin         break;
913787f3133SJosh Durgin     default:
914787f3133SJosh Durgin         r = -EINVAL;
91551a13528SJosh Durgin     }
91651a13528SJosh Durgin 
91751a13528SJosh Durgin     if (r < 0) {
918405a2764SKevin Wolf         goto failed_completion;
919f27aaf4bSChristian Brunner     }
920f27aaf4bSChristian Brunner     return &acb->common;
92151a13528SJosh Durgin 
922405a2764SKevin Wolf failed_completion:
923405a2764SKevin Wolf     rbd_aio_release(c);
92451a13528SJosh Durgin failed:
9257267c094SAnthony Liguori     g_free(rcb);
9261d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
927405a2764SKevin Wolf         qemu_vfree(acb->bounce);
9281d393bdeStianqing     }
9291d393bdeStianqing 
9308007429aSFam Zheng     qemu_aio_unref(acb);
93151a13528SJosh Durgin     return NULL;
932f27aaf4bSChristian Brunner }
933f27aaf4bSChristian Brunner 
934e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
935e8e16d4bSEric Blake                                        uint64_t offset, uint64_t bytes,
936e8e16d4bSEric Blake                                        QEMUIOVector *qiov, int flags,
937097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
938f27aaf4bSChristian Brunner                                        void *opaque)
939f27aaf4bSChristian Brunner {
940e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
941787f3133SJosh Durgin                          RBD_AIO_READ);
942f27aaf4bSChristian Brunner }
943f27aaf4bSChristian Brunner 
944e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
945e8e16d4bSEric Blake                                         uint64_t offset, uint64_t bytes,
946e8e16d4bSEric Blake                                         QEMUIOVector *qiov, int flags,
947097310b5SMarkus Armbruster                                         BlockCompletionFunc *cb,
948f27aaf4bSChristian Brunner                                         void *opaque)
949f27aaf4bSChristian Brunner {
950e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
951787f3133SJosh Durgin                          RBD_AIO_WRITE);
952f27aaf4bSChristian Brunner }
953f27aaf4bSChristian Brunner 
954dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9557c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
956097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
957dc7588c1SJosh Durgin                                       void *opaque)
958dc7588c1SJosh Durgin {
959dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
960dc7588c1SJosh Durgin }
961dc7588c1SJosh Durgin 
962dc7588c1SJosh Durgin #else
963dc7588c1SJosh Durgin 
9648b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9657a3f5fe9SSage Weil {
9667a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9677a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9687a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9697a3f5fe9SSage Weil     return rbd_flush(s->image);
9707a3f5fe9SSage Weil #else
9717a3f5fe9SSage Weil     return 0;
9727a3f5fe9SSage Weil #endif
9737a3f5fe9SSage Weil }
974dc7588c1SJosh Durgin #endif
9757a3f5fe9SSage Weil 
976ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
977f27aaf4bSChristian Brunner {
978f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
979ad32e9c0SJosh Durgin     rbd_image_info_t info;
980ad32e9c0SJosh Durgin     int r;
981ad32e9c0SJosh Durgin 
982ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
983ad32e9c0SJosh Durgin     if (r < 0) {
984ad32e9c0SJosh Durgin         return r;
985ad32e9c0SJosh Durgin     }
986ad32e9c0SJosh Durgin 
987ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
988f27aaf4bSChristian Brunner     return 0;
989f27aaf4bSChristian Brunner }
990f27aaf4bSChristian Brunner 
991ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
992f27aaf4bSChristian Brunner {
993f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
994ad32e9c0SJosh Durgin     rbd_image_info_t info;
995ad32e9c0SJosh Durgin     int r;
996f27aaf4bSChristian Brunner 
997ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
998ad32e9c0SJosh Durgin     if (r < 0) {
999ad32e9c0SJosh Durgin         return r;
1000f27aaf4bSChristian Brunner     }
1001f27aaf4bSChristian Brunner 
1002ad32e9c0SJosh Durgin     return info.size;
1003ad32e9c0SJosh Durgin }
1004ad32e9c0SJosh Durgin 
1005061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1006061ca8a3SKevin Wolf                                              int64_t offset,
1007061ca8a3SKevin Wolf                                              PreallocMode prealloc,
1008061ca8a3SKevin Wolf                                              Error **errp)
100930cdc48cSJosh Durgin {
101030cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
101130cdc48cSJosh Durgin     int r;
101230cdc48cSJosh Durgin 
10138243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
10148243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
1015977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
10168243ccb7SMax Reitz         return -ENOTSUP;
10178243ccb7SMax Reitz     }
10188243ccb7SMax Reitz 
101930cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
102030cdc48cSJosh Durgin     if (r < 0) {
1021f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
102230cdc48cSJosh Durgin         return r;
102330cdc48cSJosh Durgin     }
102430cdc48cSJosh Durgin 
102530cdc48cSJosh Durgin     return 0;
102630cdc48cSJosh Durgin }
102730cdc48cSJosh Durgin 
1028ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1029ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1030f27aaf4bSChristian Brunner {
1031f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1032f27aaf4bSChristian Brunner     int r;
1033f27aaf4bSChristian Brunner 
1034f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1035f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1036f27aaf4bSChristian Brunner     }
1037f27aaf4bSChristian Brunner 
1038f27aaf4bSChristian Brunner     /*
1039f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1040f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1041f27aaf4bSChristian Brunner      */
1042f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1043f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1044f27aaf4bSChristian Brunner         return -EINVAL;
1045f27aaf4bSChristian Brunner     }
1046f27aaf4bSChristian Brunner 
1047f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1048f27aaf4bSChristian Brunner         return -ERANGE;
1049f27aaf4bSChristian Brunner     }
1050f27aaf4bSChristian Brunner 
1051ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1052f27aaf4bSChristian Brunner     if (r < 0) {
1053ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1054f27aaf4bSChristian Brunner         return r;
1055f27aaf4bSChristian Brunner     }
1056f27aaf4bSChristian Brunner 
1057f27aaf4bSChristian Brunner     return 0;
1058f27aaf4bSChristian Brunner }
1059f27aaf4bSChristian Brunner 
1060bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1061a89d89d3SWenchao Xia                                 const char *snapshot_id,
1062a89d89d3SWenchao Xia                                 const char *snapshot_name,
1063a89d89d3SWenchao Xia                                 Error **errp)
1064bd603247SGregory Farnum {
1065bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1066bd603247SGregory Farnum     int r;
1067bd603247SGregory Farnum 
1068a89d89d3SWenchao Xia     if (!snapshot_name) {
1069a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1070a89d89d3SWenchao Xia         return -EINVAL;
1071a89d89d3SWenchao Xia     }
1072a89d89d3SWenchao Xia 
1073a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1074a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1075a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1076a89d89d3SWenchao Xia         error_setg(errp,
1077a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1078a89d89d3SWenchao Xia                    "equal to snapshot name");
1079a89d89d3SWenchao Xia         return -EINVAL;
1080a89d89d3SWenchao Xia     }
1081a89d89d3SWenchao Xia 
1082bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1083a89d89d3SWenchao Xia     if (r < 0) {
1084a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1085a89d89d3SWenchao Xia     }
1086bd603247SGregory Farnum     return r;
1087bd603247SGregory Farnum }
1088bd603247SGregory Farnum 
1089bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1090bd603247SGregory Farnum                                   const char *snapshot_name)
1091bd603247SGregory Farnum {
1092bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1093bd603247SGregory Farnum 
10949be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1095bd603247SGregory Farnum }
1096bd603247SGregory Farnum 
1097ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1098ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1099f27aaf4bSChristian Brunner {
1100f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1101f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1102ad32e9c0SJosh Durgin     int i, snap_count;
1103ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1104ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1105f27aaf4bSChristian Brunner 
1106ad32e9c0SJosh Durgin     do {
110702c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1108ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
11099e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
11107267c094SAnthony Liguori             g_free(snaps);
1111f27aaf4bSChristian Brunner         }
1112ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1113f27aaf4bSChristian Brunner 
1114ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1115b9c53290SJosh Durgin         goto done;
1116f27aaf4bSChristian Brunner     }
1117f27aaf4bSChristian Brunner 
11185839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1119f27aaf4bSChristian Brunner 
1120ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1121ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1122f27aaf4bSChristian Brunner 
1123f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1124f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1125f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1126f27aaf4bSChristian Brunner 
1127ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1128f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1129f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1130f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1131f27aaf4bSChristian Brunner     }
1132ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
11339e6337d0SStefan Hajnoczi     g_free(snaps);
1134ad32e9c0SJosh Durgin 
1135b9c53290SJosh Durgin  done:
1136f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1137f27aaf4bSChristian Brunner     return snap_count;
1138f27aaf4bSChristian Brunner }
1139f27aaf4bSChristian Brunner 
1140787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11414da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
11424da444a0SEric Blake                                          int64_t offset,
1143f5a5ca79SManos Pitsidianakis                                          int bytes,
1144097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1145787f3133SJosh Durgin                                          void *opaque)
1146787f3133SJosh Durgin {
1147f5a5ca79SManos Pitsidianakis     return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1148787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1149787f3133SJosh Durgin }
1150787f3133SJosh Durgin #endif
1151787f3133SJosh Durgin 
1152be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
11532b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1154be217884SAdam Crume                                                       Error **errp)
1155be217884SAdam Crume {
1156be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1157be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1158be217884SAdam Crume     if (r < 0) {
1159be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1160be217884SAdam Crume     }
1161be217884SAdam Crume }
1162be217884SAdam Crume #endif
1163be217884SAdam Crume 
1164bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1165bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1166bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1167bd0cf596SChunyan Liu     .desc = {
1168f27aaf4bSChristian Brunner         {
1169f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1170bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1171f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1172f27aaf4bSChristian Brunner         },
1173f27aaf4bSChristian Brunner         {
1174f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1175bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1176f27aaf4bSChristian Brunner             .help = "RBD object size"
1177f27aaf4bSChristian Brunner         },
117860390a21SDaniel P. Berrange         {
117960390a21SDaniel P. Berrange             .name = "password-secret",
118060390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
118160390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
118260390a21SDaniel P. Berrange         },
1183bd0cf596SChunyan Liu         { /* end of list */ }
1184bd0cf596SChunyan Liu     }
1185f27aaf4bSChristian Brunner };
1186f27aaf4bSChristian Brunner 
1187f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1188f27aaf4bSChristian Brunner     .format_name            = "rbd",
1189f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1190c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1191e8e16d4bSEric Blake     .bdrv_refresh_limits    = qemu_rbd_refresh_limits,
1192ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1193ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
119456e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
11951bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1196efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
11973ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1198ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1199bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1200ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
1201061ca8a3SKevin Wolf     .bdrv_co_truncate       = qemu_rbd_co_truncate,
1202f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1203f27aaf4bSChristian Brunner 
1204e8e16d4bSEric Blake     .bdrv_aio_preadv        = qemu_rbd_aio_preadv,
1205e8e16d4bSEric Blake     .bdrv_aio_pwritev       = qemu_rbd_aio_pwritev,
1206dc7588c1SJosh Durgin 
1207dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1208dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1209dc7588c1SJosh Durgin #else
1210c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1211dc7588c1SJosh Durgin #endif
1212f27aaf4bSChristian Brunner 
1213787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
12144da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1215787f3133SJosh Durgin #endif
1216787f3133SJosh Durgin 
1217ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1218bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1219ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1220bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1221be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
12222b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1223be217884SAdam Crume #endif
1224f27aaf4bSChristian Brunner };
1225f27aaf4bSChristian Brunner 
1226f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1227f27aaf4bSChristian Brunner {
1228f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1229f27aaf4bSChristian Brunner }
1230f27aaf4bSChristian Brunner 
1231f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1232