xref: /openbmc/qemu/block/rbd.c (revision af91062ee1408f7f5bb58389d355d29a5040c648)
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 
24260390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
24360390a21SDaniel P. Berrange                              Error **errp)
24460390a21SDaniel P. Berrange {
24560390a21SDaniel P. Berrange     if (secretid == 0) {
24660390a21SDaniel P. Berrange         return 0;
24760390a21SDaniel P. Berrange     }
24860390a21SDaniel P. Berrange 
24960390a21SDaniel P. Berrange     gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
25060390a21SDaniel P. Berrange                                                     errp);
25160390a21SDaniel P. Berrange     if (!secret) {
25260390a21SDaniel P. Berrange         return -1;
25360390a21SDaniel P. Berrange     }
25460390a21SDaniel P. Berrange 
25560390a21SDaniel P. Berrange     rados_conf_set(cluster, "key", secret);
25660390a21SDaniel P. Berrange     g_free(secret);
25760390a21SDaniel P. Berrange 
25860390a21SDaniel P. Berrange     return 0;
25960390a21SDaniel P. Berrange }
26060390a21SDaniel P. Berrange 
261e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
262e34d8f29SJosh Durgin                                  Error **errp)
263fab5cf59SJosh Durgin {
264e98c6961SEric Blake     QList *keypairs;
265e98c6961SEric Blake     QString *name;
266e98c6961SEric Blake     QString *value;
267e98c6961SEric Blake     const char *key;
268e98c6961SEric Blake     size_t remaining;
269fab5cf59SJosh Durgin     int ret = 0;
270fab5cf59SJosh Durgin 
271e98c6961SEric Blake     if (!keypairs_json) {
272e98c6961SEric Blake         return ret;
273fab5cf59SJosh Durgin     }
2747dc847ebSMax Reitz     keypairs = qobject_to(QList,
2757dc847ebSMax Reitz                           qobject_from_json(keypairs_json, &error_abort));
276e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
277e98c6961SEric Blake     assert(remaining);
278fab5cf59SJosh Durgin 
279e98c6961SEric Blake     while (remaining--) {
2807dc847ebSMax Reitz         name = qobject_to(QString, qlist_pop(keypairs));
2817dc847ebSMax Reitz         value = qobject_to(QString, qlist_pop(keypairs));
282e98c6961SEric Blake         assert(name && value);
283e98c6961SEric Blake         key = qstring_get_str(name);
284fab5cf59SJosh Durgin 
285e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
286cb3e7f08SMarc-André Lureau         qobject_unref(value);
287fab5cf59SJosh Durgin         if (ret < 0) {
288e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
289cb3e7f08SMarc-André Lureau             qobject_unref(name);
290fab5cf59SJosh Durgin             ret = -EINVAL;
291fab5cf59SJosh Durgin             break;
292fab5cf59SJosh Durgin         }
293cb3e7f08SMarc-André Lureau         qobject_unref(name);
294fab5cf59SJosh Durgin     }
295fab5cf59SJosh Durgin 
296cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
297fab5cf59SJosh Durgin     return ret;
298fab5cf59SJosh Durgin }
299fab5cf59SJosh Durgin 
3001d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
3011d393bdeStianqing {
3021d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
3031d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
3041d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
3051d393bdeStianqing                    acb->qiov->size - offs);
3061d393bdeStianqing     } else {
3071d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
3081d393bdeStianqing     }
3091d393bdeStianqing }
3101d393bdeStianqing 
3110f9d252dSJeff Cody static QemuOptsList runtime_opts = {
3120f9d252dSJeff Cody     .name = "rbd",
3130f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3140f9d252dSJeff Cody     .desc = {
3150f9d252dSJeff Cody         {
3160f9d252dSJeff Cody             .name = "pool",
3170f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3180f9d252dSJeff Cody             .help = "Rados pool name",
3190f9d252dSJeff Cody         },
3200f9d252dSJeff Cody         {
3210f9d252dSJeff Cody             .name = "image",
3220f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3230f9d252dSJeff Cody             .help = "Image name in the pool",
3240f9d252dSJeff Cody         },
3250f9d252dSJeff Cody         {
326cbf036b4SMarkus Armbruster             .name = "conf",
327cbf036b4SMarkus Armbruster             .type = QEMU_OPT_STRING,
328cbf036b4SMarkus Armbruster             .help = "Rados config file location",
329cbf036b4SMarkus Armbruster         },
330cbf036b4SMarkus Armbruster         {
3310f9d252dSJeff Cody             .name = "snapshot",
3320f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3330f9d252dSJeff Cody             .help = "Ceph snapshot name",
3340f9d252dSJeff Cody         },
3350f9d252dSJeff Cody         {
3360f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3370f9d252dSJeff Cody             .name = "user",
3380f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3390f9d252dSJeff Cody             .help = "Rados id name",
3400f9d252dSJeff Cody         },
341cbf036b4SMarkus Armbruster         /*
3422836284dSMarkus Armbruster          * server.* extracted manually, see qemu_rbd_mon_host()
343cbf036b4SMarkus Armbruster          */
3440f9d252dSJeff Cody         { /* end of list */ }
3450f9d252dSJeff Cody     },
3460f9d252dSJeff Cody };
3470f9d252dSJeff Cody 
3481bebea37SKevin Wolf /* FIXME Deprecate and remove keypairs or make it available in QMP.
3491bebea37SKevin Wolf  * password_secret should eventually be configurable in opts->location. Support
3501bebea37SKevin Wolf  * for it in .bdrv_open will make it work here as well. */
3511bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
3521bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
3531bebea37SKevin Wolf                               Error **errp)
3541bebea37SKevin Wolf {
3551bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
3561bebea37SKevin Wolf     rados_t cluster;
3571bebea37SKevin Wolf     rados_ioctx_t io_ctx;
3581bebea37SKevin Wolf     int obj_order = 0;
3591bebea37SKevin Wolf     int ret;
3601bebea37SKevin Wolf 
3611bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
3621bebea37SKevin Wolf     if (opts->location->has_snapshot) {
3631bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
3641bebea37SKevin Wolf         return -EINVAL;
3651bebea37SKevin Wolf     }
3661bebea37SKevin Wolf 
3671bebea37SKevin Wolf     if (opts->has_cluster_size) {
3681bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
3691bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
3701bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
3711bebea37SKevin Wolf             return -EINVAL;
3721bebea37SKevin Wolf         }
3731bebea37SKevin Wolf         if (objsize < 4096) {
3741bebea37SKevin Wolf             error_setg(errp, "obj size too small");
3751bebea37SKevin Wolf             return -EINVAL;
3761bebea37SKevin Wolf         }
3771bebea37SKevin Wolf         obj_order = ctz32(objsize);
3781bebea37SKevin Wolf     }
3791bebea37SKevin Wolf 
380aa045c2dSKevin Wolf     ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
381aa045c2dSKevin Wolf                            password_secret, errp);
3821bebea37SKevin Wolf     if (ret < 0) {
3831bebea37SKevin Wolf         return ret;
3841bebea37SKevin Wolf     }
3851bebea37SKevin Wolf 
3861bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
3871bebea37SKevin Wolf     if (ret < 0) {
3881bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
389aa045c2dSKevin Wolf         goto out;
3901bebea37SKevin Wolf     }
3911bebea37SKevin Wolf 
3921bebea37SKevin Wolf     ret = 0;
393aa045c2dSKevin Wolf out:
394aa045c2dSKevin Wolf     rados_ioctx_destroy(io_ctx);
3951bebea37SKevin Wolf     rados_shutdown(cluster);
3961bebea37SKevin Wolf     return ret;
3971bebea37SKevin Wolf }
3981bebea37SKevin Wolf 
3991bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
4001bebea37SKevin Wolf {
4011bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
4021bebea37SKevin Wolf }
4031bebea37SKevin Wolf 
404efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
405efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
406efc75e2aSStefan Hajnoczi                                                 Error **errp)
407f27aaf4bSChristian Brunner {
4081bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
4091bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
4101bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
411d61563b2SMarkus Armbruster     Error *local_err = NULL;
4121bebea37SKevin Wolf     const char *keypairs, *password_secret;
413c7cacb3eSJeff Cody     QDict *options = NULL;
414c7cacb3eSJeff Cody     int ret = 0;
415f27aaf4bSChristian Brunner 
4161bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
4171bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
4181bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
4191bebea37SKevin Wolf 
4201bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
4211bebea37SKevin Wolf 
4221bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
42360390a21SDaniel P. Berrange 
424f27aaf4bSChristian Brunner     /* Read out options */
4251bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
426c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
4271bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
4281bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
4291bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
430f27aaf4bSChristian Brunner 
431c7cacb3eSJeff Cody     options = qdict_new();
432c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
433c7cacb3eSJeff Cody     if (local_err) {
434c7cacb3eSJeff Cody         ret = -EINVAL;
435c7cacb3eSJeff Cody         error_propagate(errp, local_err);
436c7cacb3eSJeff Cody         goto exit;
437c7cacb3eSJeff Cody     }
438c7cacb3eSJeff Cody 
439129c7d1cSMarkus Armbruster     /*
440129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
441129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
442129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
443129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
444129c7d1cSMarkus Armbruster      */
4451bebea37SKevin Wolf     loc = rbd_opts->location;
4461bebea37SKevin Wolf     loc->pool     = g_strdup(qdict_get_try_str(options, "pool"));
4471bebea37SKevin Wolf     loc->conf     = g_strdup(qdict_get_try_str(options, "conf"));
4481bebea37SKevin Wolf     loc->has_conf = !!loc->conf;
4491bebea37SKevin Wolf     loc->user     = g_strdup(qdict_get_try_str(options, "user"));
4501bebea37SKevin Wolf     loc->has_user = !!loc->user;
4511bebea37SKevin Wolf     loc->image    = g_strdup(qdict_get_try_str(options, "image"));
45207846397SMarkus Armbruster     keypairs      = qdict_get_try_str(options, "=keyvalue-pairs");
453c7cacb3eSJeff Cody 
4541bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
45587cd3d20SVikhyat Umrao     if (ret < 0) {
456c7cacb3eSJeff Cody         goto exit;
457f27aaf4bSChristian Brunner     }
458f27aaf4bSChristian Brunner 
459c7cacb3eSJeff Cody exit:
460cb3e7f08SMarc-André Lureau     qobject_unref(options);
4611bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
462f27aaf4bSChristian Brunner     return ret;
463f27aaf4bSChristian Brunner }
464f27aaf4bSChristian Brunner 
465f27aaf4bSChristian Brunner /*
466e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
467e04fb07fSStefan Hajnoczi  * BH context.
468f27aaf4bSChristian Brunner  */
469ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
470f27aaf4bSChristian Brunner {
471f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
472f27aaf4bSChristian Brunner     int64_t r;
473f27aaf4bSChristian Brunner 
474f27aaf4bSChristian Brunner     r = rcb->ret;
475f27aaf4bSChristian Brunner 
476dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
477f27aaf4bSChristian Brunner         if (r < 0) {
478f27aaf4bSChristian Brunner             acb->ret = r;
479f27aaf4bSChristian Brunner             acb->error = 1;
480f27aaf4bSChristian Brunner         } else if (!acb->error) {
481ad32e9c0SJosh Durgin             acb->ret = rcb->size;
482f27aaf4bSChristian Brunner         }
483f27aaf4bSChristian Brunner     } else {
484ad32e9c0SJosh Durgin         if (r < 0) {
4851d393bdeStianqing             qemu_rbd_memset(rcb, 0);
486f27aaf4bSChristian Brunner             acb->ret = r;
487f27aaf4bSChristian Brunner             acb->error = 1;
488ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
4891d393bdeStianqing             qemu_rbd_memset(rcb, r);
490f27aaf4bSChristian Brunner             if (!acb->error) {
491ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
492f27aaf4bSChristian Brunner             }
493f27aaf4bSChristian Brunner         } else if (!acb->error) {
494ad32e9c0SJosh Durgin             acb->ret = r;
495f27aaf4bSChristian Brunner         }
496f27aaf4bSChristian Brunner     }
497e04fb07fSStefan Hajnoczi 
4987267c094SAnthony Liguori     g_free(rcb);
499e04fb07fSStefan Hajnoczi 
5001d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
501e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
502e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
503f27aaf4bSChristian Brunner         }
504e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5051d393bdeStianqing     }
5061d393bdeStianqing 
507e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
508f27aaf4bSChristian Brunner 
5098007429aSFam Zheng     qemu_aio_unref(acb);
510f27aaf4bSChristian Brunner }
511f27aaf4bSChristian Brunner 
5124bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
5130a55679bSJeff Cody {
5144bfb2741SKevin Wolf     const char **vals;
5152836284dSMarkus Armbruster     const char *host, *port;
5162836284dSMarkus Armbruster     char *rados_str;
5174bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
5184bfb2741SKevin Wolf     int i, cnt;
5190a55679bSJeff Cody 
5204bfb2741SKevin Wolf     if (!opts->has_server) {
5214bfb2741SKevin Wolf         return NULL;
5220a55679bSJeff Cody     }
5234bfb2741SKevin Wolf 
5244bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
5254bfb2741SKevin Wolf         cnt++;
5260a55679bSJeff Cody     }
5270a55679bSJeff Cody 
5284bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
5294bfb2741SKevin Wolf 
5304bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
5314bfb2741SKevin Wolf         host = p->value->host;
5324bfb2741SKevin Wolf         port = p->value->port;
5334bfb2741SKevin Wolf 
5340a55679bSJeff Cody         if (strchr(host, ':')) {
5354bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
5360a55679bSJeff Cody         } else {
5374bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
5380a55679bSJeff Cody         }
5390a55679bSJeff Cody     }
5402836284dSMarkus Armbruster     vals[i] = NULL;
5410a55679bSJeff Cody 
5422836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
5432836284dSMarkus Armbruster     g_strfreev((char **)vals);
5440a55679bSJeff Cody     return rados_str;
5450a55679bSJeff Cody }
5460a55679bSJeff Cody 
5473d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
5484bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
5494ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
5504ff45049SKevin Wolf                             Error **errp)
551f27aaf4bSChristian Brunner {
5520a55679bSJeff Cody     char *mon_host = NULL;
5533d9136f9SKevin Wolf     Error *local_err = NULL;
554f27aaf4bSChristian Brunner     int r;
555f27aaf4bSChristian Brunner 
5564bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
55784d18f06SMarkus Armbruster     if (local_err) {
558d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
5592836284dSMarkus Armbruster         r = -EINVAL;
5602836284dSMarkus Armbruster         goto failed_opts;
561a9ccedc3SKevin Wolf     }
562a9ccedc3SKevin Wolf 
5634bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
564ad32e9c0SJosh Durgin     if (r < 0) {
56587cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
566c3ca988dSKevin Wolf         goto failed_opts;
567f27aaf4bSChristian Brunner     }
568f27aaf4bSChristian Brunner 
569c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
5704bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
5714bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
5724bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
573e34d8f29SJosh Durgin         goto failed_shutdown;
574e34d8f29SJosh Durgin     }
57599a3c89dSJosh Durgin 
5763d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
57799a3c89dSJosh Durgin     if (r < 0) {
57899a3c89dSJosh Durgin         goto failed_shutdown;
57999a3c89dSJosh Durgin     }
58099a3c89dSJosh Durgin 
5810a55679bSJeff Cody     if (mon_host) {
5823d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
5830a55679bSJeff Cody         if (r < 0) {
5840a55679bSJeff Cody             goto failed_shutdown;
5850a55679bSJeff Cody         }
5860a55679bSJeff Cody     }
5870a55679bSJeff Cody 
5883d9136f9SKevin Wolf     if (qemu_rbd_set_auth(*cluster, secretid, errp) < 0) {
58960390a21SDaniel P. Berrange         r = -EIO;
59060390a21SDaniel P. Berrange         goto failed_shutdown;
59160390a21SDaniel P. Berrange     }
59260390a21SDaniel P. Berrange 
593b11f38fcSJosh Durgin     /*
594b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
595b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
596b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
597b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
598b11f38fcSJosh Durgin      * be set up, fall back to no caching.
599b11f38fcSJosh Durgin      */
6003d9136f9SKevin Wolf     if (cache) {
6013d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
602b11f38fcSJosh Durgin     } else {
6033d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
604b11f38fcSJosh Durgin     }
605b11f38fcSJosh Durgin 
6063d9136f9SKevin Wolf     r = rados_connect(*cluster);
607ad32e9c0SJosh Durgin     if (r < 0) {
60887cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
609eb93d5d9SSage Weil         goto failed_shutdown;
610ad32e9c0SJosh Durgin     }
611ad32e9c0SJosh Durgin 
6124bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
613ad32e9c0SJosh Durgin     if (r < 0) {
6144bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
615eb93d5d9SSage Weil         goto failed_shutdown;
616ad32e9c0SJosh Durgin     }
617ad32e9c0SJosh Durgin 
6183d9136f9SKevin Wolf     return 0;
6193d9136f9SKevin Wolf 
6203d9136f9SKevin Wolf failed_shutdown:
6213d9136f9SKevin Wolf     rados_shutdown(*cluster);
6223d9136f9SKevin Wolf failed_opts:
6233d9136f9SKevin Wolf     g_free(mon_host);
6243d9136f9SKevin Wolf     return r;
6253d9136f9SKevin Wolf }
6263d9136f9SKevin Wolf 
6273d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
6283d9136f9SKevin Wolf                          Error **errp)
6293d9136f9SKevin Wolf {
6303d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
6314bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
6324bfb2741SKevin Wolf     Visitor *v;
633bfb15b4bSJeff Cody     const QDictEntry *e;
6343d9136f9SKevin Wolf     Error *local_err = NULL;
6354ff45049SKevin Wolf     char *keypairs, *secretid;
6363d9136f9SKevin Wolf     int r;
6373d9136f9SKevin Wolf 
6384ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
6394ff45049SKevin Wolf     if (keypairs) {
6404ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
6414ff45049SKevin Wolf     }
6424ff45049SKevin Wolf 
6434ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
6444ff45049SKevin Wolf     if (secretid) {
6454ff45049SKevin Wolf         qdict_del(options, "password-secret");
6464ff45049SKevin Wolf     }
6474ff45049SKevin Wolf 
6484bfb2741SKevin Wolf     /* Convert the remaining options into a QAPI object */
649*af91062eSMarkus Armbruster     v = qobject_input_visitor_new_flat_confused(options, errp);
650*af91062eSMarkus Armbruster     if (!v) {
6514bfb2741SKevin Wolf         r = -EINVAL;
6524bfb2741SKevin Wolf         goto out;
6534bfb2741SKevin Wolf     }
6544bfb2741SKevin Wolf 
6554bfb2741SKevin Wolf     visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err);
6564bfb2741SKevin Wolf     visit_free(v);
6574bfb2741SKevin Wolf 
6584bfb2741SKevin Wolf     if (local_err) {
6594bfb2741SKevin Wolf         error_propagate(errp, local_err);
6604bfb2741SKevin Wolf         r = -EINVAL;
6614bfb2741SKevin Wolf         goto out;
6624bfb2741SKevin Wolf     }
6634bfb2741SKevin Wolf 
664bfb15b4bSJeff Cody     /* Remove the processed options from the QDict (the visitor processes
665bfb15b4bSJeff Cody      * _all_ options in the QDict) */
666bfb15b4bSJeff Cody     while ((e = qdict_first(options))) {
667bfb15b4bSJeff Cody         qdict_del(options, e->key);
668bfb15b4bSJeff Cody     }
669bfb15b4bSJeff Cody 
670d41a5588SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
671d41a5588SKevin Wolf                          !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
6723d9136f9SKevin Wolf     if (r < 0) {
6734ff45049SKevin Wolf         goto out;
6743d9136f9SKevin Wolf     }
6753d9136f9SKevin Wolf 
676d41a5588SKevin Wolf     s->snap = g_strdup(opts->snapshot);
677d41a5588SKevin Wolf     s->image_name = g_strdup(opts->image);
678d41a5588SKevin Wolf 
679e2b8247aSJeff Cody     /* rbd_open is always r/w */
68080b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
681ad32e9c0SJosh Durgin     if (r < 0) {
68280b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
68380b61a27SJeff Cody                          s->image_name);
684eb93d5d9SSage Weil         goto failed_open;
685ad32e9c0SJosh Durgin     }
686ad32e9c0SJosh Durgin 
687e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
688e2b8247aSJeff Cody      * leave as-is */
689e2b8247aSJeff Cody     if (s->snap != NULL) {
690398e6ad0SKevin Wolf         if (!bdrv_is_read_only(bs)) {
691398e6ad0SKevin Wolf             error_report("Opening rbd snapshots without an explicit "
692398e6ad0SKevin Wolf                          "read-only=on option is deprecated. Future versions "
693398e6ad0SKevin Wolf                          "will refuse to open the image instead of "
694398e6ad0SKevin Wolf                          "automatically marking the image read-only.");
695e2b8247aSJeff Cody             r = bdrv_set_read_only(bs, true, &local_err);
696e2b8247aSJeff Cody             if (r < 0) {
697e2b8247aSJeff Cody                 error_propagate(errp, local_err);
698e2b8247aSJeff Cody                 goto failed_open;
699e2b8247aSJeff Cody             }
700e2b8247aSJeff Cody         }
701398e6ad0SKevin Wolf     }
702f27aaf4bSChristian Brunner 
7034ff45049SKevin Wolf     r = 0;
7044ff45049SKevin Wolf     goto out;
705f27aaf4bSChristian Brunner 
706eb93d5d9SSage Weil failed_open:
707ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
708eb93d5d9SSage Weil     g_free(s->snap);
70980b61a27SJeff Cody     g_free(s->image_name);
7103d9136f9SKevin Wolf     rados_shutdown(s->cluster);
7114ff45049SKevin Wolf out:
7124bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
7134ff45049SKevin Wolf     g_free(keypairs);
7144ff45049SKevin Wolf     g_free(secretid);
715f27aaf4bSChristian Brunner     return r;
716f27aaf4bSChristian Brunner }
717f27aaf4bSChristian Brunner 
71856e7cf8dSJeff Cody 
71956e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
72056e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
72156e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
72256e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
72356e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
72456e7cf8dSJeff Cody {
72556e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
72656e7cf8dSJeff Cody     int ret = 0;
72756e7cf8dSJeff Cody 
72856e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
72956e7cf8dSJeff Cody         error_setg(errp,
73056e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
73156e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
73256e7cf8dSJeff Cody         ret = -EINVAL;
73356e7cf8dSJeff Cody     }
73456e7cf8dSJeff Cody 
73556e7cf8dSJeff Cody     return ret;
73656e7cf8dSJeff Cody }
73756e7cf8dSJeff Cody 
738ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
739f27aaf4bSChristian Brunner {
740f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
741f27aaf4bSChristian Brunner 
742ad32e9c0SJosh Durgin     rbd_close(s->image);
743ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
7447267c094SAnthony Liguori     g_free(s->snap);
74580b61a27SJeff Cody     g_free(s->image_name);
746ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
747f27aaf4bSChristian Brunner }
748f27aaf4bSChristian Brunner 
749d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
750f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
751f27aaf4bSChristian Brunner };
752f27aaf4bSChristian Brunner 
753e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
754f27aaf4bSChristian Brunner {
755e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
756e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
757ad32e9c0SJosh Durgin }
758ad32e9c0SJosh Durgin 
759ad32e9c0SJosh Durgin /*
760ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
761ad32e9c0SJosh Durgin  *
762ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
763ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
764e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
765e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
766ad32e9c0SJosh Durgin  */
767ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
768ad32e9c0SJosh Durgin {
769e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
770e04fb07fSStefan Hajnoczi 
771ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
772ad32e9c0SJosh Durgin     rbd_aio_release(c);
773f27aaf4bSChristian Brunner 
774fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
775ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
776473c7f02SStefan Priebe }
777f27aaf4bSChristian Brunner 
778787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
779787f3133SJosh Durgin                                    uint64_t off,
780787f3133SJosh Durgin                                    uint64_t len,
781787f3133SJosh Durgin                                    rbd_completion_t comp)
782787f3133SJosh Durgin {
783787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
784787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
785787f3133SJosh Durgin #else
786787f3133SJosh Durgin     return -ENOTSUP;
787787f3133SJosh Durgin #endif
788787f3133SJosh Durgin }
789787f3133SJosh Durgin 
790dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
791dc7588c1SJosh Durgin                                  rbd_completion_t comp)
792dc7588c1SJosh Durgin {
793dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
794dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
795dc7588c1SJosh Durgin #else
796dc7588c1SJosh Durgin     return -ENOTSUP;
797dc7588c1SJosh Durgin #endif
798dc7588c1SJosh Durgin }
799dc7588c1SJosh Durgin 
8007c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8017bbca9e2SEric Blake                                  int64_t off,
802f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8037bbca9e2SEric Blake                                  int64_t size,
804097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
805787f3133SJosh Durgin                                  void *opaque,
806787f3133SJosh Durgin                                  RBDAIOCmd cmd)
807f27aaf4bSChristian Brunner {
808f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8090f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
810ad32e9c0SJosh Durgin     rbd_completion_t c;
81151a13528SJosh Durgin     int r;
812f27aaf4bSChristian Brunner 
813f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
814f27aaf4bSChristian Brunner 
815d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
816787f3133SJosh Durgin     acb->cmd = cmd;
817f27aaf4bSChristian Brunner     acb->qiov = qiov;
8187bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8191d393bdeStianqing 
8201d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8211d393bdeStianqing 
8221d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
823dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
824787f3133SJosh Durgin             acb->bounce = NULL;
825787f3133SJosh Durgin         } else {
8260f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8270f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8280f7a0237SKevin Wolf                 goto failed;
8290f7a0237SKevin Wolf             }
830787f3133SJosh Durgin         }
8311d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
8321d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
8331d393bdeStianqing         }
8341d393bdeStianqing         rcb->buf = acb->bounce;
8351d393bdeStianqing     }
8361d393bdeStianqing 
837f27aaf4bSChristian Brunner     acb->ret = 0;
838f27aaf4bSChristian Brunner     acb->error = 0;
839f27aaf4bSChristian Brunner     acb->s = s;
840f27aaf4bSChristian Brunner 
841f27aaf4bSChristian Brunner     rcb->acb = acb;
842f27aaf4bSChristian Brunner     rcb->s = acb->s;
843ad32e9c0SJosh Durgin     rcb->size = size;
84451a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
84551a13528SJosh Durgin     if (r < 0) {
84651a13528SJosh Durgin         goto failed;
84751a13528SJosh Durgin     }
848f27aaf4bSChristian Brunner 
849787f3133SJosh Durgin     switch (cmd) {
850787f3133SJosh Durgin     case RBD_AIO_WRITE:
8511d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8521d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
8531d393bdeStianqing #else
8541d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
8551d393bdeStianqing #endif
856787f3133SJosh Durgin         break;
857787f3133SJosh Durgin     case RBD_AIO_READ:
8581d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8591d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
8601d393bdeStianqing #else
8611d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
8621d393bdeStianqing #endif
863787f3133SJosh Durgin         break;
864787f3133SJosh Durgin     case RBD_AIO_DISCARD:
865787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
866787f3133SJosh Durgin         break;
867dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
868dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
869dc7588c1SJosh Durgin         break;
870787f3133SJosh Durgin     default:
871787f3133SJosh Durgin         r = -EINVAL;
87251a13528SJosh Durgin     }
87351a13528SJosh Durgin 
87451a13528SJosh Durgin     if (r < 0) {
875405a2764SKevin Wolf         goto failed_completion;
876f27aaf4bSChristian Brunner     }
877f27aaf4bSChristian Brunner     return &acb->common;
87851a13528SJosh Durgin 
879405a2764SKevin Wolf failed_completion:
880405a2764SKevin Wolf     rbd_aio_release(c);
88151a13528SJosh Durgin failed:
8827267c094SAnthony Liguori     g_free(rcb);
8831d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
884405a2764SKevin Wolf         qemu_vfree(acb->bounce);
8851d393bdeStianqing     }
8861d393bdeStianqing 
8878007429aSFam Zheng     qemu_aio_unref(acb);
88851a13528SJosh Durgin     return NULL;
889f27aaf4bSChristian Brunner }
890f27aaf4bSChristian Brunner 
891e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
892e8e16d4bSEric Blake                                        uint64_t offset, uint64_t bytes,
893e8e16d4bSEric Blake                                        QEMUIOVector *qiov, int flags,
894097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
895f27aaf4bSChristian Brunner                                        void *opaque)
896f27aaf4bSChristian Brunner {
897e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
898787f3133SJosh Durgin                          RBD_AIO_READ);
899f27aaf4bSChristian Brunner }
900f27aaf4bSChristian Brunner 
901e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
902e8e16d4bSEric Blake                                         uint64_t offset, uint64_t bytes,
903e8e16d4bSEric Blake                                         QEMUIOVector *qiov, int flags,
904097310b5SMarkus Armbruster                                         BlockCompletionFunc *cb,
905f27aaf4bSChristian Brunner                                         void *opaque)
906f27aaf4bSChristian Brunner {
907e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
908787f3133SJosh Durgin                          RBD_AIO_WRITE);
909f27aaf4bSChristian Brunner }
910f27aaf4bSChristian Brunner 
911dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9127c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
913097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
914dc7588c1SJosh Durgin                                       void *opaque)
915dc7588c1SJosh Durgin {
916dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
917dc7588c1SJosh Durgin }
918dc7588c1SJosh Durgin 
919dc7588c1SJosh Durgin #else
920dc7588c1SJosh Durgin 
9218b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9227a3f5fe9SSage Weil {
9237a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9247a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9257a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9267a3f5fe9SSage Weil     return rbd_flush(s->image);
9277a3f5fe9SSage Weil #else
9287a3f5fe9SSage Weil     return 0;
9297a3f5fe9SSage Weil #endif
9307a3f5fe9SSage Weil }
931dc7588c1SJosh Durgin #endif
9327a3f5fe9SSage Weil 
933ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
934f27aaf4bSChristian Brunner {
935f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
936ad32e9c0SJosh Durgin     rbd_image_info_t info;
937ad32e9c0SJosh Durgin     int r;
938ad32e9c0SJosh Durgin 
939ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
940ad32e9c0SJosh Durgin     if (r < 0) {
941ad32e9c0SJosh Durgin         return r;
942ad32e9c0SJosh Durgin     }
943ad32e9c0SJosh Durgin 
944ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
945f27aaf4bSChristian Brunner     return 0;
946f27aaf4bSChristian Brunner }
947f27aaf4bSChristian Brunner 
948ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
949f27aaf4bSChristian Brunner {
950f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
951ad32e9c0SJosh Durgin     rbd_image_info_t info;
952ad32e9c0SJosh Durgin     int r;
953f27aaf4bSChristian Brunner 
954ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
955ad32e9c0SJosh Durgin     if (r < 0) {
956ad32e9c0SJosh Durgin         return r;
957f27aaf4bSChristian Brunner     }
958f27aaf4bSChristian Brunner 
959ad32e9c0SJosh Durgin     return info.size;
960ad32e9c0SJosh Durgin }
961ad32e9c0SJosh Durgin 
9628243ccb7SMax Reitz static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset,
9638243ccb7SMax Reitz                              PreallocMode prealloc, Error **errp)
96430cdc48cSJosh Durgin {
96530cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
96630cdc48cSJosh Durgin     int r;
96730cdc48cSJosh Durgin 
9688243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
9698243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
970977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
9718243ccb7SMax Reitz         return -ENOTSUP;
9728243ccb7SMax Reitz     }
9738243ccb7SMax Reitz 
97430cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
97530cdc48cSJosh Durgin     if (r < 0) {
976f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
97730cdc48cSJosh Durgin         return r;
97830cdc48cSJosh Durgin     }
97930cdc48cSJosh Durgin 
98030cdc48cSJosh Durgin     return 0;
98130cdc48cSJosh Durgin }
98230cdc48cSJosh Durgin 
983ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
984ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
985f27aaf4bSChristian Brunner {
986f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
987f27aaf4bSChristian Brunner     int r;
988f27aaf4bSChristian Brunner 
989f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
990f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
991f27aaf4bSChristian Brunner     }
992f27aaf4bSChristian Brunner 
993f27aaf4bSChristian Brunner     /*
994f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
995f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
996f27aaf4bSChristian Brunner      */
997f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
998f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
999f27aaf4bSChristian Brunner         return -EINVAL;
1000f27aaf4bSChristian Brunner     }
1001f27aaf4bSChristian Brunner 
1002f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1003f27aaf4bSChristian Brunner         return -ERANGE;
1004f27aaf4bSChristian Brunner     }
1005f27aaf4bSChristian Brunner 
1006ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1007f27aaf4bSChristian Brunner     if (r < 0) {
1008ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1009f27aaf4bSChristian Brunner         return r;
1010f27aaf4bSChristian Brunner     }
1011f27aaf4bSChristian Brunner 
1012f27aaf4bSChristian Brunner     return 0;
1013f27aaf4bSChristian Brunner }
1014f27aaf4bSChristian Brunner 
1015bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1016a89d89d3SWenchao Xia                                 const char *snapshot_id,
1017a89d89d3SWenchao Xia                                 const char *snapshot_name,
1018a89d89d3SWenchao Xia                                 Error **errp)
1019bd603247SGregory Farnum {
1020bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1021bd603247SGregory Farnum     int r;
1022bd603247SGregory Farnum 
1023a89d89d3SWenchao Xia     if (!snapshot_name) {
1024a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1025a89d89d3SWenchao Xia         return -EINVAL;
1026a89d89d3SWenchao Xia     }
1027a89d89d3SWenchao Xia 
1028a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1029a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1030a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1031a89d89d3SWenchao Xia         error_setg(errp,
1032a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1033a89d89d3SWenchao Xia                    "equal to snapshot name");
1034a89d89d3SWenchao Xia         return -EINVAL;
1035a89d89d3SWenchao Xia     }
1036a89d89d3SWenchao Xia 
1037bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1038a89d89d3SWenchao Xia     if (r < 0) {
1039a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1040a89d89d3SWenchao Xia     }
1041bd603247SGregory Farnum     return r;
1042bd603247SGregory Farnum }
1043bd603247SGregory Farnum 
1044bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1045bd603247SGregory Farnum                                   const char *snapshot_name)
1046bd603247SGregory Farnum {
1047bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1048bd603247SGregory Farnum 
10499be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1050bd603247SGregory Farnum }
1051bd603247SGregory Farnum 
1052ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1053ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1054f27aaf4bSChristian Brunner {
1055f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1056f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1057ad32e9c0SJosh Durgin     int i, snap_count;
1058ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1059ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1060f27aaf4bSChristian Brunner 
1061ad32e9c0SJosh Durgin     do {
106202c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1063ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
10649e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
10657267c094SAnthony Liguori             g_free(snaps);
1066f27aaf4bSChristian Brunner         }
1067ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1068f27aaf4bSChristian Brunner 
1069ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1070b9c53290SJosh Durgin         goto done;
1071f27aaf4bSChristian Brunner     }
1072f27aaf4bSChristian Brunner 
10735839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1074f27aaf4bSChristian Brunner 
1075ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1076ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1077f27aaf4bSChristian Brunner 
1078f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1079f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1080f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1081f27aaf4bSChristian Brunner 
1082ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1083f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1084f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1085f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1086f27aaf4bSChristian Brunner     }
1087ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
10889e6337d0SStefan Hajnoczi     g_free(snaps);
1089ad32e9c0SJosh Durgin 
1090b9c53290SJosh Durgin  done:
1091f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1092f27aaf4bSChristian Brunner     return snap_count;
1093f27aaf4bSChristian Brunner }
1094f27aaf4bSChristian Brunner 
1095787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
10964da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
10974da444a0SEric Blake                                          int64_t offset,
1098f5a5ca79SManos Pitsidianakis                                          int bytes,
1099097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1100787f3133SJosh Durgin                                          void *opaque)
1101787f3133SJosh Durgin {
1102f5a5ca79SManos Pitsidianakis     return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1103787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1104787f3133SJosh Durgin }
1105787f3133SJosh Durgin #endif
1106787f3133SJosh Durgin 
1107be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
11082b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1109be217884SAdam Crume                                                       Error **errp)
1110be217884SAdam Crume {
1111be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1112be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1113be217884SAdam Crume     if (r < 0) {
1114be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1115be217884SAdam Crume     }
1116be217884SAdam Crume }
1117be217884SAdam Crume #endif
1118be217884SAdam Crume 
1119bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1120bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1121bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1122bd0cf596SChunyan Liu     .desc = {
1123f27aaf4bSChristian Brunner         {
1124f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1125bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1126f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1127f27aaf4bSChristian Brunner         },
1128f27aaf4bSChristian Brunner         {
1129f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1130bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1131f27aaf4bSChristian Brunner             .help = "RBD object size"
1132f27aaf4bSChristian Brunner         },
113360390a21SDaniel P. Berrange         {
113460390a21SDaniel P. Berrange             .name = "password-secret",
113560390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
113660390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
113760390a21SDaniel P. Berrange         },
1138bd0cf596SChunyan Liu         { /* end of list */ }
1139bd0cf596SChunyan Liu     }
1140f27aaf4bSChristian Brunner };
1141f27aaf4bSChristian Brunner 
1142f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1143f27aaf4bSChristian Brunner     .format_name            = "rbd",
1144f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1145c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1146e8e16d4bSEric Blake     .bdrv_refresh_limits    = qemu_rbd_refresh_limits,
1147ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1148ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
114956e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
11501bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1151efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
11523ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1153ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1154bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1155ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
115630cdc48cSJosh Durgin     .bdrv_truncate          = qemu_rbd_truncate,
1157f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1158f27aaf4bSChristian Brunner 
1159e8e16d4bSEric Blake     .bdrv_aio_preadv        = qemu_rbd_aio_preadv,
1160e8e16d4bSEric Blake     .bdrv_aio_pwritev       = qemu_rbd_aio_pwritev,
1161dc7588c1SJosh Durgin 
1162dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1163dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1164dc7588c1SJosh Durgin #else
1165c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1166dc7588c1SJosh Durgin #endif
1167f27aaf4bSChristian Brunner 
1168787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11694da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1170787f3133SJosh Durgin #endif
1171787f3133SJosh Durgin 
1172ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1173bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1174ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1175bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1176be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
11772b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1178be217884SAdam Crume #endif
1179f27aaf4bSChristian Brunner };
1180f27aaf4bSChristian Brunner 
1181f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1182f27aaf4bSChristian Brunner {
1183f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1184f27aaf4bSChristian Brunner }
1185f27aaf4bSChristian Brunner 
1186f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1187