xref: /openbmc/qemu/block/rbd.c (revision 730b00bbfdc15f914f47e03a703fa7647c10c4a9)
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 
16da34e65cSMarkus Armbruster #include "qapi/error.h"
171de7afc9SPaolo Bonzini #include "qemu/error-report.h"
18737e150eSPaolo Bonzini #include "block/block_int.h"
1960390a21SDaniel P. Berrange #include "crypto/secret.h"
20f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
21c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
22f27aaf4bSChristian Brunner 
23ad32e9c0SJosh Durgin #include <rbd/librbd.h>
24f27aaf4bSChristian Brunner 
25f27aaf4bSChristian Brunner /*
26f27aaf4bSChristian Brunner  * When specifying the image filename use:
27f27aaf4bSChristian Brunner  *
28fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
29f27aaf4bSChristian Brunner  *
309e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
31f27aaf4bSChristian Brunner  *
329e1fbcdeSSage Weil  * devicename is the name of the rbd image.
33f27aaf4bSChristian Brunner  *
349e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
359e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
36fab5cf59SJosh Durgin  *
379e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
389e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
399e1fbcdeSSage Weil  * (normally 'admin').
40f27aaf4bSChristian Brunner  *
419e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
429e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
439e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
449e1fbcdeSSage Weil  * file, specify conf=/dev/null.
45f27aaf4bSChristian Brunner  *
469e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
479e1fbcdeSSage Weil  * leading "\".
48f27aaf4bSChristian Brunner  */
49f27aaf4bSChristian Brunner 
50787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
51787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
52787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
53787f3133SJosh Durgin #else
54787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
55787f3133SJosh Durgin #endif
56787f3133SJosh Durgin 
57f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
58f27aaf4bSChristian Brunner 
59ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
60ad32e9c0SJosh Durgin 
611d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
621d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
631d393bdeStianqing #define LIBRBD_USE_IOVEC 1
641d393bdeStianqing #else
651d393bdeStianqing #define LIBRBD_USE_IOVEC 0
661d393bdeStianqing #endif
671d393bdeStianqing 
68787f3133SJosh Durgin typedef enum {
69787f3133SJosh Durgin     RBD_AIO_READ,
70787f3133SJosh Durgin     RBD_AIO_WRITE,
71dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
72dc7588c1SJosh Durgin     RBD_AIO_FLUSH
73787f3133SJosh Durgin } RBDAIOCmd;
74787f3133SJosh Durgin 
75f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
767c84b1b8SMarkus Armbruster     BlockAIOCB common;
7708448d51SStefan Priebe     int64_t ret;
78f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
79f27aaf4bSChristian Brunner     char *bounce;
80787f3133SJosh Durgin     RBDAIOCmd cmd;
81f27aaf4bSChristian Brunner     int error;
82f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
83f27aaf4bSChristian Brunner } RBDAIOCB;
84f27aaf4bSChristian Brunner 
85f27aaf4bSChristian Brunner typedef struct RADOSCB {
86f27aaf4bSChristian Brunner     RBDAIOCB *acb;
87f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
88ad32e9c0SJosh Durgin     int64_t size;
89f27aaf4bSChristian Brunner     char *buf;
9008448d51SStefan Priebe     int64_t ret;
91f27aaf4bSChristian Brunner } RADOSCB;
92f27aaf4bSChristian Brunner 
93f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
94ad32e9c0SJosh Durgin     rados_t cluster;
95ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
96ad32e9c0SJosh Durgin     rbd_image_t image;
97*730b00bbSMarkus Armbruster     char *name;
98ad32e9c0SJosh Durgin     char *snap;
99f27aaf4bSChristian Brunner } BDRVRBDState;
100f27aaf4bSChristian Brunner 
101*730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
102f27aaf4bSChristian Brunner {
103f27aaf4bSChristian Brunner     char *end;
104f27aaf4bSChristian Brunner 
105f27aaf4bSChristian Brunner     *p = NULL;
106f27aaf4bSChristian Brunner 
107f27aaf4bSChristian Brunner     if (delim != '\0') {
10816a06b24SSage Weil         for (end = src; *end; ++end) {
10916a06b24SSage Weil             if (*end == delim) {
11016a06b24SSage Weil                 break;
11116a06b24SSage Weil             }
11216a06b24SSage Weil             if (*end == '\\' && end[1] != '\0') {
11316a06b24SSage Weil                 end++;
11416a06b24SSage Weil             }
11516a06b24SSage Weil         }
11616a06b24SSage Weil         if (*end == delim) {
117f27aaf4bSChristian Brunner             *p = end + 1;
118f27aaf4bSChristian Brunner             *end = '\0';
119f27aaf4bSChristian Brunner         }
120f27aaf4bSChristian Brunner     }
1217830f909SJeff Cody     return src;
122f27aaf4bSChristian Brunner }
123f27aaf4bSChristian Brunner 
12416a06b24SSage Weil static void qemu_rbd_unescape(char *src)
12516a06b24SSage Weil {
12616a06b24SSage Weil     char *p;
12716a06b24SSage Weil 
12816a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
12916a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
13016a06b24SSage Weil             src++;
13116a06b24SSage Weil         }
13216a06b24SSage Weil         *p = *src;
13316a06b24SSage Weil     }
13416a06b24SSage Weil     *p = '\0';
13516a06b24SSage Weil }
13616a06b24SSage Weil 
137c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
138d61563b2SMarkus Armbruster                                     Error **errp)
139f27aaf4bSChristian Brunner {
140f27aaf4bSChristian Brunner     const char *start;
141c7cacb3eSJeff Cody     char *p, *buf, *keypairs;
1427830f909SJeff Cody     char *found_str;
143c7cacb3eSJeff Cody     size_t max_keypair_size;
144f27aaf4bSChristian Brunner 
145f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
146d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
147c7cacb3eSJeff Cody         return;
148f27aaf4bSChristian Brunner     }
149f27aaf4bSChristian Brunner 
150c7cacb3eSJeff Cody     max_keypair_size = strlen(start) + 1;
1517267c094SAnthony Liguori     buf = g_strdup(start);
152c7cacb3eSJeff Cody     keypairs = g_malloc0(max_keypair_size);
153f27aaf4bSChristian Brunner     p = buf;
154f27aaf4bSChristian Brunner 
155*730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1567830f909SJeff Cody     if (!p) {
1577830f909SJeff Cody         error_setg(errp, "Pool name is required");
1587830f909SJeff Cody         goto done;
1597830f909SJeff Cody     }
1607830f909SJeff Cody     qemu_rbd_unescape(found_str);
161c7cacb3eSJeff Cody     qdict_put(options, "pool", qstring_from_str(found_str));
162fab5cf59SJosh Durgin 
163fab5cf59SJosh Durgin     if (strchr(p, '@')) {
164*730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, '@', &p);
1657830f909SJeff Cody         qemu_rbd_unescape(found_str);
166c7cacb3eSJeff Cody         qdict_put(options, "image", qstring_from_str(found_str));
1677830f909SJeff Cody 
168*730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1697830f909SJeff Cody         qemu_rbd_unescape(found_str);
170c7cacb3eSJeff Cody         qdict_put(options, "snapshot", qstring_from_str(found_str));
1717830f909SJeff Cody     } else {
172*730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1737830f909SJeff Cody         qemu_rbd_unescape(found_str);
174c7cacb3eSJeff Cody         qdict_put(options, "image", qstring_from_str(found_str));
1757830f909SJeff Cody     }
1767830f909SJeff Cody     if (!p) {
177f27aaf4bSChristian Brunner         goto done;
178f27aaf4bSChristian Brunner     }
179f27aaf4bSChristian Brunner 
180*730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '\0', &p);
181c7cacb3eSJeff Cody 
182c7cacb3eSJeff Cody     p = found_str;
183c7cacb3eSJeff Cody 
184c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
185c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
186c7cacb3eSJeff Cody     while (p) {
187c7cacb3eSJeff Cody         char *name, *value;
188*730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
189c7cacb3eSJeff Cody         if (!p) {
190c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
191c7cacb3eSJeff Cody             break;
192c7cacb3eSJeff Cody         }
193c7cacb3eSJeff Cody 
194c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
195c7cacb3eSJeff Cody 
196*730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
197c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
198c7cacb3eSJeff Cody 
199c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
200c7cacb3eSJeff Cody             qdict_put(options, "conf", qstring_from_str(value));
201c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
202c7cacb3eSJeff Cody             qdict_put(options, "user" , qstring_from_str(value));
203c7cacb3eSJeff Cody         } else {
204c7cacb3eSJeff Cody             /* FIXME: This is pretty ugly, and not the right way to do this.
205c7cacb3eSJeff Cody              *        These should be contained in a structure, and then
206c7cacb3eSJeff Cody              *        passed explicitly as individual key/value pairs to
207c7cacb3eSJeff Cody              *        rados.  Consider this legacy code that needs to be
208c7cacb3eSJeff Cody              *        updated. */
209c7cacb3eSJeff Cody             char *tmp = g_malloc0(max_keypair_size);
210c7cacb3eSJeff Cody             /* only use a delimiter if it is not the first keypair found */
211c7cacb3eSJeff Cody             /* These are sets of unknown key/value pairs we'll pass along
212c7cacb3eSJeff Cody              * to ceph */
213c7cacb3eSJeff Cody             if (keypairs[0]) {
214c7cacb3eSJeff Cody                 snprintf(tmp, max_keypair_size, ":%s=%s", name, value);
215c7cacb3eSJeff Cody                 pstrcat(keypairs, max_keypair_size, tmp);
216c7cacb3eSJeff Cody             } else {
217c7cacb3eSJeff Cody                 snprintf(keypairs, max_keypair_size, "%s=%s", name, value);
218c7cacb3eSJeff Cody             }
219c7cacb3eSJeff Cody             g_free(tmp);
220c7cacb3eSJeff Cody         }
221c7cacb3eSJeff Cody     }
222c7cacb3eSJeff Cody 
223c7cacb3eSJeff Cody     if (keypairs[0]) {
224c7cacb3eSJeff Cody         qdict_put(options, "keyvalue-pairs", qstring_from_str(keypairs));
225c7cacb3eSJeff Cody     }
226c7cacb3eSJeff Cody 
227f27aaf4bSChristian Brunner 
228f27aaf4bSChristian Brunner done:
2297267c094SAnthony Liguori     g_free(buf);
230c7cacb3eSJeff Cody     g_free(keypairs);
231c7cacb3eSJeff Cody     return;
2327c7e9df0SSage Weil }
2337c7e9df0SSage Weil 
23460390a21SDaniel P. Berrange 
23560390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
23660390a21SDaniel P. Berrange                              Error **errp)
23760390a21SDaniel P. Berrange {
23860390a21SDaniel P. Berrange     if (secretid == 0) {
23960390a21SDaniel P. Berrange         return 0;
24060390a21SDaniel P. Berrange     }
24160390a21SDaniel P. Berrange 
24260390a21SDaniel P. Berrange     gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
24360390a21SDaniel P. Berrange                                                     errp);
24460390a21SDaniel P. Berrange     if (!secret) {
24560390a21SDaniel P. Berrange         return -1;
24660390a21SDaniel P. Berrange     }
24760390a21SDaniel P. Berrange 
24860390a21SDaniel P. Berrange     rados_conf_set(cluster, "key", secret);
24960390a21SDaniel P. Berrange     g_free(secret);
25060390a21SDaniel P. Berrange 
25160390a21SDaniel P. Berrange     return 0;
25260390a21SDaniel P. Berrange }
25360390a21SDaniel P. Berrange 
254c7cacb3eSJeff Cody static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs,
255e34d8f29SJosh Durgin                                  Error **errp)
256fab5cf59SJosh Durgin {
257fab5cf59SJosh Durgin     char *p, *buf;
2587830f909SJeff Cody     char *name;
2597830f909SJeff Cody     char *value;
260fab5cf59SJosh Durgin     int ret = 0;
261fab5cf59SJosh Durgin 
262c7cacb3eSJeff Cody     buf = g_strdup(keypairs);
263fab5cf59SJosh Durgin     p = buf;
264fab5cf59SJosh Durgin 
265fab5cf59SJosh Durgin     while (p) {
266*730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
267fab5cf59SJosh Durgin         if (!p) {
268d61563b2SMarkus Armbruster             error_setg(errp, "conf option %s has no value", name);
269fab5cf59SJosh Durgin             ret = -EINVAL;
270fab5cf59SJosh Durgin             break;
271fab5cf59SJosh Durgin         }
272fab5cf59SJosh Durgin 
273*730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
274fab5cf59SJosh Durgin 
275fab5cf59SJosh Durgin         ret = rados_conf_set(cluster, name, value);
276fab5cf59SJosh Durgin         if (ret < 0) {
27787cd3d20SVikhyat Umrao             error_setg_errno(errp, -ret, "invalid conf option %s", name);
278fab5cf59SJosh Durgin             ret = -EINVAL;
279fab5cf59SJosh Durgin             break;
280fab5cf59SJosh Durgin         }
281fab5cf59SJosh Durgin     }
282fab5cf59SJosh Durgin 
2837267c094SAnthony Liguori     g_free(buf);
284fab5cf59SJosh Durgin     return ret;
285fab5cf59SJosh Durgin }
286fab5cf59SJosh Durgin 
2871d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
2881d393bdeStianqing {
2891d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
2901d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
2911d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
2921d393bdeStianqing                    acb->qiov->size - offs);
2931d393bdeStianqing     } else {
2941d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
2951d393bdeStianqing     }
2961d393bdeStianqing }
2971d393bdeStianqing 
2980f9d252dSJeff Cody static QemuOptsList runtime_opts = {
2990f9d252dSJeff Cody     .name = "rbd",
3000f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3010f9d252dSJeff Cody     .desc = {
3020f9d252dSJeff Cody         {
3030f9d252dSJeff Cody             .name = "filename",
3040f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3050f9d252dSJeff Cody             .help = "Specification of the rbd image",
3060f9d252dSJeff Cody         },
3070f9d252dSJeff Cody         {
3080f9d252dSJeff Cody             .name = "password-secret",
3090f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3100f9d252dSJeff Cody             .help = "ID of secret providing the password",
3110f9d252dSJeff Cody         },
3120f9d252dSJeff Cody         {
3130f9d252dSJeff Cody             .name = "conf",
3140f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3150f9d252dSJeff Cody             .help = "Rados config file location",
3160f9d252dSJeff Cody         },
3170f9d252dSJeff Cody         {
3180f9d252dSJeff Cody             .name = "pool",
3190f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3200f9d252dSJeff Cody             .help = "Rados pool name",
3210f9d252dSJeff Cody         },
3220f9d252dSJeff Cody         {
3230f9d252dSJeff Cody             .name = "image",
3240f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3250f9d252dSJeff Cody             .help = "Image name in the pool",
3260f9d252dSJeff Cody         },
3270f9d252dSJeff Cody         {
3280f9d252dSJeff Cody             .name = "snapshot",
3290f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3300f9d252dSJeff Cody             .help = "Ceph snapshot name",
3310f9d252dSJeff Cody         },
3320f9d252dSJeff Cody         {
3330f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3340f9d252dSJeff Cody             .name = "user",
3350f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3360f9d252dSJeff Cody             .help = "Rados id name",
3370f9d252dSJeff Cody         },
3380f9d252dSJeff Cody         {
3390f9d252dSJeff Cody             .name = "keyvalue-pairs",
3400f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3410f9d252dSJeff Cody             .help = "Legacy rados key/value option parameters",
3420f9d252dSJeff Cody         },
3430a55679bSJeff Cody         {
3440a55679bSJeff Cody             .name = "host",
3450a55679bSJeff Cody             .type = QEMU_OPT_STRING,
3460a55679bSJeff Cody         },
3470a55679bSJeff Cody         {
3480a55679bSJeff Cody             .name = "port",
3490a55679bSJeff Cody             .type = QEMU_OPT_STRING,
3500a55679bSJeff Cody         },
3510a55679bSJeff Cody         {
3520a55679bSJeff Cody             .name = "auth",
3530a55679bSJeff Cody             .type = QEMU_OPT_STRING,
3540a55679bSJeff Cody             .help = "Supported authentication method, either cephx or none",
3550a55679bSJeff Cody         },
3560f9d252dSJeff Cody         { /* end of list */ }
3570f9d252dSJeff Cody     },
3580f9d252dSJeff Cody };
3590f9d252dSJeff Cody 
360bd0cf596SChunyan Liu static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
361f27aaf4bSChristian Brunner {
362d61563b2SMarkus Armbruster     Error *local_err = NULL;
363f27aaf4bSChristian Brunner     int64_t bytes = 0;
364f27aaf4bSChristian Brunner     int64_t objsize;
365ad32e9c0SJosh Durgin     int obj_order = 0;
366c7cacb3eSJeff Cody     const char *pool, *name, *conf, *clientname, *keypairs;
36760390a21SDaniel P. Berrange     const char *secretid;
368ad32e9c0SJosh Durgin     rados_t cluster;
369ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
370c7cacb3eSJeff Cody     QDict *options = NULL;
371c7cacb3eSJeff Cody     QemuOpts *rbd_opts = NULL;
372c7cacb3eSJeff Cody     int ret = 0;
373f27aaf4bSChristian Brunner 
37460390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
37560390a21SDaniel P. Berrange 
376f27aaf4bSChristian Brunner     /* Read out options */
377c2eb918eSHu Tao     bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
378c2eb918eSHu Tao                      BDRV_SECTOR_SIZE);
379bd0cf596SChunyan Liu     objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
380bd0cf596SChunyan Liu     if (objsize) {
381f27aaf4bSChristian Brunner         if ((objsize - 1) & objsize) {    /* not a power of 2? */
382d61563b2SMarkus Armbruster             error_setg(errp, "obj size needs to be power of 2");
383c7cacb3eSJeff Cody             ret = -EINVAL;
384c7cacb3eSJeff Cody             goto exit;
385f27aaf4bSChristian Brunner         }
386f27aaf4bSChristian Brunner         if (objsize < 4096) {
387d61563b2SMarkus Armbruster             error_setg(errp, "obj size too small");
388c7cacb3eSJeff Cody             ret = -EINVAL;
389c7cacb3eSJeff Cody             goto exit;
390f27aaf4bSChristian Brunner         }
391786a4ea8SStefan Hajnoczi         obj_order = ctz32(objsize);
392f27aaf4bSChristian Brunner     }
393f27aaf4bSChristian Brunner 
394c7cacb3eSJeff Cody     options = qdict_new();
395c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
396c7cacb3eSJeff Cody     if (local_err) {
397c7cacb3eSJeff Cody         ret = -EINVAL;
398c7cacb3eSJeff Cody         error_propagate(errp, local_err);
399c7cacb3eSJeff Cody         goto exit;
400c7cacb3eSJeff Cody     }
401c7cacb3eSJeff Cody 
402c7cacb3eSJeff Cody     rbd_opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
403c7cacb3eSJeff Cody     qemu_opts_absorb_qdict(rbd_opts, options, &local_err);
404c7cacb3eSJeff Cody     if (local_err) {
405c7cacb3eSJeff Cody         error_propagate(errp, local_err);
406c7cacb3eSJeff Cody         ret = -EINVAL;
407c7cacb3eSJeff Cody         goto exit;
408c7cacb3eSJeff Cody     }
409c7cacb3eSJeff Cody 
410c7cacb3eSJeff Cody     pool       = qemu_opt_get(rbd_opts, "pool");
411c7cacb3eSJeff Cody     conf       = qemu_opt_get(rbd_opts, "conf");
412c7cacb3eSJeff Cody     clientname = qemu_opt_get(rbd_opts, "user");
413c7cacb3eSJeff Cody     name       = qemu_opt_get(rbd_opts, "image");
414c7cacb3eSJeff Cody     keypairs   = qemu_opt_get(rbd_opts, "keyvalue-pairs");
415c7cacb3eSJeff Cody 
41687cd3d20SVikhyat Umrao     ret = rados_create(&cluster, clientname);
41787cd3d20SVikhyat Umrao     if (ret < 0) {
41887cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error initializing");
419c7cacb3eSJeff Cody         goto exit;
420f27aaf4bSChristian Brunner     }
421f27aaf4bSChristian Brunner 
422c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
423c7cacb3eSJeff Cody     ret = rados_conf_read_file(cluster, conf);
424c7cacb3eSJeff Cody     if (conf && ret < 0) {
425c7cacb3eSJeff Cody         error_setg_errno(errp, -ret, "error reading conf file %s", conf);
426e38f643aSXiubo Li         ret = -EIO;
427e38f643aSXiubo Li         goto shutdown;
428fab5cf59SJosh Durgin     }
429fab5cf59SJosh Durgin 
430c7cacb3eSJeff Cody     ret = qemu_rbd_set_keypairs(cluster, keypairs, errp);
431c7cacb3eSJeff Cody     if (ret < 0) {
432e38f643aSXiubo Li         ret = -EIO;
433e38f643aSXiubo Li         goto shutdown;
434fab5cf59SJosh Durgin     }
435ad32e9c0SJosh Durgin 
43660390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
437e38f643aSXiubo Li         ret = -EIO;
438e38f643aSXiubo Li         goto shutdown;
43960390a21SDaniel P. Berrange     }
44060390a21SDaniel P. Berrange 
44187cd3d20SVikhyat Umrao     ret = rados_connect(cluster);
44287cd3d20SVikhyat Umrao     if (ret < 0) {
44387cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error connecting");
444e38f643aSXiubo Li         goto shutdown;
445ad32e9c0SJosh Durgin     }
446ad32e9c0SJosh Durgin 
44787cd3d20SVikhyat Umrao     ret = rados_ioctx_create(cluster, pool, &io_ctx);
44887cd3d20SVikhyat Umrao     if (ret < 0) {
44987cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error opening pool %s", pool);
450e38f643aSXiubo Li         goto shutdown;
451f27aaf4bSChristian Brunner     }
452f27aaf4bSChristian Brunner 
453ad32e9c0SJosh Durgin     ret = rbd_create(io_ctx, name, bytes, &obj_order);
45487cd3d20SVikhyat Umrao     if (ret < 0) {
45587cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error rbd create");
45687cd3d20SVikhyat Umrao     }
457f27aaf4bSChristian Brunner 
458e38f643aSXiubo Li     rados_ioctx_destroy(io_ctx);
459e38f643aSXiubo Li 
460e38f643aSXiubo Li shutdown:
461e38f643aSXiubo Li     rados_shutdown(cluster);
462c7cacb3eSJeff Cody 
463c7cacb3eSJeff Cody exit:
464c7cacb3eSJeff Cody     QDECREF(options);
465c7cacb3eSJeff Cody     qemu_opts_del(rbd_opts);
466f27aaf4bSChristian Brunner     return ret;
467f27aaf4bSChristian Brunner }
468f27aaf4bSChristian Brunner 
469f27aaf4bSChristian Brunner /*
470e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
471e04fb07fSStefan Hajnoczi  * BH context.
472f27aaf4bSChristian Brunner  */
473ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
474f27aaf4bSChristian Brunner {
475f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
476f27aaf4bSChristian Brunner     int64_t r;
477f27aaf4bSChristian Brunner 
478f27aaf4bSChristian Brunner     r = rcb->ret;
479f27aaf4bSChristian Brunner 
480dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
481f27aaf4bSChristian Brunner         if (r < 0) {
482f27aaf4bSChristian Brunner             acb->ret = r;
483f27aaf4bSChristian Brunner             acb->error = 1;
484f27aaf4bSChristian Brunner         } else if (!acb->error) {
485ad32e9c0SJosh Durgin             acb->ret = rcb->size;
486f27aaf4bSChristian Brunner         }
487f27aaf4bSChristian Brunner     } else {
488ad32e9c0SJosh Durgin         if (r < 0) {
4891d393bdeStianqing             qemu_rbd_memset(rcb, 0);
490f27aaf4bSChristian Brunner             acb->ret = r;
491f27aaf4bSChristian Brunner             acb->error = 1;
492ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
4931d393bdeStianqing             qemu_rbd_memset(rcb, r);
494f27aaf4bSChristian Brunner             if (!acb->error) {
495ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
496f27aaf4bSChristian Brunner             }
497f27aaf4bSChristian Brunner         } else if (!acb->error) {
498ad32e9c0SJosh Durgin             acb->ret = r;
499f27aaf4bSChristian Brunner         }
500f27aaf4bSChristian Brunner     }
501e04fb07fSStefan Hajnoczi 
5027267c094SAnthony Liguori     g_free(rcb);
503e04fb07fSStefan Hajnoczi 
5041d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
505e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
506e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
507f27aaf4bSChristian Brunner         }
508e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5091d393bdeStianqing     }
5101d393bdeStianqing 
511e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
512f27aaf4bSChristian Brunner 
5138007429aSFam Zheng     qemu_aio_unref(acb);
514f27aaf4bSChristian Brunner }
515f27aaf4bSChristian Brunner 
5160a55679bSJeff Cody #define RBD_MON_HOST          0
5170a55679bSJeff Cody #define RBD_AUTH_SUPPORTED    1
5180a55679bSJeff Cody 
5190a55679bSJeff Cody static char *qemu_rbd_array_opts(QDict *options, const char *prefix, int type,
5200a55679bSJeff Cody                                  Error **errp)
5210a55679bSJeff Cody {
5220a55679bSJeff Cody     int num_entries;
5230a55679bSJeff Cody     QemuOpts *opts = NULL;
5240a55679bSJeff Cody     QDict *sub_options;
5250a55679bSJeff Cody     const char *host;
5260a55679bSJeff Cody     const char *port;
5270a55679bSJeff Cody     char *str;
5280a55679bSJeff Cody     char *rados_str = NULL;
5290a55679bSJeff Cody     Error *local_err = NULL;
5300a55679bSJeff Cody     int i;
5310a55679bSJeff Cody 
5320a55679bSJeff Cody     assert(type == RBD_MON_HOST || type == RBD_AUTH_SUPPORTED);
5330a55679bSJeff Cody 
5340a55679bSJeff Cody     num_entries = qdict_array_entries(options, prefix);
5350a55679bSJeff Cody 
5360a55679bSJeff Cody     if (num_entries < 0) {
5370a55679bSJeff Cody         error_setg(errp, "Parse error on RBD QDict array");
5380a55679bSJeff Cody         return NULL;
5390a55679bSJeff Cody     }
5400a55679bSJeff Cody 
5410a55679bSJeff Cody     for (i = 0; i < num_entries; i++) {
5420a55679bSJeff Cody         char *strbuf = NULL;
5430a55679bSJeff Cody         const char *value;
5440a55679bSJeff Cody         char *rados_str_tmp;
5450a55679bSJeff Cody 
5460a55679bSJeff Cody         str = g_strdup_printf("%s%d.", prefix, i);
5470a55679bSJeff Cody         qdict_extract_subqdict(options, &sub_options, str);
5480a55679bSJeff Cody         g_free(str);
5490a55679bSJeff Cody 
5500a55679bSJeff Cody         opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
5510a55679bSJeff Cody         qemu_opts_absorb_qdict(opts, sub_options, &local_err);
5520a55679bSJeff Cody         QDECREF(sub_options);
5530a55679bSJeff Cody         if (local_err) {
5540a55679bSJeff Cody             error_propagate(errp, local_err);
5550a55679bSJeff Cody             g_free(rados_str);
5560a55679bSJeff Cody             rados_str = NULL;
5570a55679bSJeff Cody             goto exit;
5580a55679bSJeff Cody         }
5590a55679bSJeff Cody 
5600a55679bSJeff Cody         if (type == RBD_MON_HOST) {
5610a55679bSJeff Cody             host = qemu_opt_get(opts, "host");
5620a55679bSJeff Cody             port = qemu_opt_get(opts, "port");
5630a55679bSJeff Cody 
5640a55679bSJeff Cody             value = host;
5650a55679bSJeff Cody             if (port) {
5660a55679bSJeff Cody                 /* check for ipv6 */
5670a55679bSJeff Cody                 if (strchr(host, ':')) {
5680a55679bSJeff Cody                     strbuf = g_strdup_printf("[%s]:%s", host, port);
5690a55679bSJeff Cody                 } else {
5700a55679bSJeff Cody                     strbuf = g_strdup_printf("%s:%s", host, port);
5710a55679bSJeff Cody                 }
5720a55679bSJeff Cody                 value = strbuf;
5730a55679bSJeff Cody             } else if (strchr(host, ':')) {
5740a55679bSJeff Cody                 strbuf = g_strdup_printf("[%s]", host);
5750a55679bSJeff Cody                 value = strbuf;
5760a55679bSJeff Cody             }
5770a55679bSJeff Cody         } else {
5780a55679bSJeff Cody             value = qemu_opt_get(opts, "auth");
5790a55679bSJeff Cody         }
5800a55679bSJeff Cody 
5810a55679bSJeff Cody 
5820a55679bSJeff Cody         /* each iteration in the for loop will build upon the string, and if
5830a55679bSJeff Cody          * rados_str is NULL then it is our first pass */
5840a55679bSJeff Cody         if (rados_str) {
5850a55679bSJeff Cody             /* separate options with ';', as that  is what rados_conf_set()
5860a55679bSJeff Cody              * requires */
5870a55679bSJeff Cody             rados_str_tmp = rados_str;
5880a55679bSJeff Cody             rados_str = g_strdup_printf("%s;%s", rados_str_tmp, value);
5890a55679bSJeff Cody             g_free(rados_str_tmp);
5900a55679bSJeff Cody         } else {
5910a55679bSJeff Cody             rados_str = g_strdup(value);
5920a55679bSJeff Cody         }
5930a55679bSJeff Cody 
5940a55679bSJeff Cody         g_free(strbuf);
5950a55679bSJeff Cody         qemu_opts_del(opts);
5960a55679bSJeff Cody         opts = NULL;
5970a55679bSJeff Cody     }
5980a55679bSJeff Cody 
5990a55679bSJeff Cody exit:
6000a55679bSJeff Cody     qemu_opts_del(opts);
6010a55679bSJeff Cody     return rados_str;
6020a55679bSJeff Cody }
6030a55679bSJeff Cody 
604015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
605015a1036SMax Reitz                          Error **errp)
606f27aaf4bSChristian Brunner {
607f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
608c7cacb3eSJeff Cody     const char *pool, *snap, *conf, *clientname, *name, *keypairs;
60960390a21SDaniel P. Berrange     const char *secretid;
610a9ccedc3SKevin Wolf     QemuOpts *opts;
611a9ccedc3SKevin Wolf     Error *local_err = NULL;
6120a55679bSJeff Cody     char *mon_host = NULL;
6130a55679bSJeff Cody     char *auth_supported = NULL;
614f27aaf4bSChristian Brunner     int r;
615f27aaf4bSChristian Brunner 
61687ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
617a9ccedc3SKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
61884d18f06SMarkus Armbruster     if (local_err) {
619d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
620a9ccedc3SKevin Wolf         qemu_opts_del(opts);
621a9ccedc3SKevin Wolf         return -EINVAL;
622a9ccedc3SKevin Wolf     }
623a9ccedc3SKevin Wolf 
6240a55679bSJeff Cody     auth_supported = qemu_rbd_array_opts(options, "auth-supported.",
6250a55679bSJeff Cody                                          RBD_AUTH_SUPPORTED, &local_err);
6260a55679bSJeff Cody     if (local_err) {
6270a55679bSJeff Cody         error_propagate(errp, local_err);
6280a55679bSJeff Cody         r = -EINVAL;
6290a55679bSJeff Cody         goto failed_opts;
6300a55679bSJeff Cody     }
6310a55679bSJeff Cody 
6320a55679bSJeff Cody     mon_host = qemu_rbd_array_opts(options, "server.",
6330a55679bSJeff Cody                                    RBD_MON_HOST, &local_err);
6340a55679bSJeff Cody     if (local_err) {
6350a55679bSJeff Cody         error_propagate(errp, local_err);
6360a55679bSJeff Cody         r = -EINVAL;
6370a55679bSJeff Cody         goto failed_opts;
6380a55679bSJeff Cody     }
6390a55679bSJeff Cody 
64060390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
641a9ccedc3SKevin Wolf 
642c7cacb3eSJeff Cody     pool           = qemu_opt_get(opts, "pool");
643c7cacb3eSJeff Cody     conf           = qemu_opt_get(opts, "conf");
644c7cacb3eSJeff Cody     snap           = qemu_opt_get(opts, "snapshot");
645c7cacb3eSJeff Cody     clientname     = qemu_opt_get(opts, "user");
646c7cacb3eSJeff Cody     name           = qemu_opt_get(opts, "image");
647c7cacb3eSJeff Cody     keypairs       = qemu_opt_get(opts, "keyvalue-pairs");
648f27aaf4bSChristian Brunner 
649f51c363cSMarkus Armbruster     if (!pool || !name) {
650f51c363cSMarkus Armbruster         error_setg(errp, "Parameters 'pool' and 'image' are required");
651f51c363cSMarkus Armbruster         r = -EINVAL;
652f51c363cSMarkus Armbruster         goto failed_opts;
653f51c363cSMarkus Armbruster     }
654f51c363cSMarkus Armbruster 
6557c7e9df0SSage Weil     r = rados_create(&s->cluster, clientname);
656ad32e9c0SJosh Durgin     if (r < 0) {
65787cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
658c3ca988dSKevin Wolf         goto failed_opts;
659f27aaf4bSChristian Brunner     }
660f27aaf4bSChristian Brunner 
661c7cacb3eSJeff Cody     s->snap = g_strdup(snap);
662*730b00bbSMarkus Armbruster     s->name = g_strdup(name);
663eb93d5d9SSage Weil 
664c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
665c7cacb3eSJeff Cody     r = rados_conf_read_file(s->cluster, conf);
666c7cacb3eSJeff Cody     if (conf && r < 0) {
667c7cacb3eSJeff Cody         error_setg_errno(errp, -r, "error reading conf file %s", conf);
668e34d8f29SJosh Durgin         goto failed_shutdown;
669e34d8f29SJosh Durgin     }
67099a3c89dSJosh Durgin 
671c7cacb3eSJeff Cody     r = qemu_rbd_set_keypairs(s->cluster, keypairs, errp);
67299a3c89dSJosh Durgin     if (r < 0) {
67399a3c89dSJosh Durgin         goto failed_shutdown;
67499a3c89dSJosh Durgin     }
67599a3c89dSJosh Durgin 
6760a55679bSJeff Cody     if (mon_host) {
6770a55679bSJeff Cody         r = rados_conf_set(s->cluster, "mon_host", mon_host);
6780a55679bSJeff Cody         if (r < 0) {
6790a55679bSJeff Cody             goto failed_shutdown;
6800a55679bSJeff Cody         }
6810a55679bSJeff Cody     }
6820a55679bSJeff Cody 
6830a55679bSJeff Cody     if (auth_supported) {
6840a55679bSJeff Cody         r = rados_conf_set(s->cluster, "auth_supported", auth_supported);
6850a55679bSJeff Cody         if (r < 0) {
6860a55679bSJeff Cody             goto failed_shutdown;
6870a55679bSJeff Cody         }
6880a55679bSJeff Cody     }
6890a55679bSJeff Cody 
69060390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
69160390a21SDaniel P. Berrange         r = -EIO;
69260390a21SDaniel P. Berrange         goto failed_shutdown;
69360390a21SDaniel P. Berrange     }
69460390a21SDaniel P. Berrange 
695b11f38fcSJosh Durgin     /*
696b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
697b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
698b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
699b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
700b11f38fcSJosh Durgin      * be set up, fall back to no caching.
701b11f38fcSJosh Durgin      */
702b11f38fcSJosh Durgin     if (flags & BDRV_O_NOCACHE) {
703b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "false");
704b11f38fcSJosh Durgin     } else {
705b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "true");
706b11f38fcSJosh Durgin     }
707b11f38fcSJosh Durgin 
708ad32e9c0SJosh Durgin     r = rados_connect(s->cluster);
709ad32e9c0SJosh Durgin     if (r < 0) {
71087cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
711eb93d5d9SSage Weil         goto failed_shutdown;
712ad32e9c0SJosh Durgin     }
713ad32e9c0SJosh Durgin 
714ad32e9c0SJosh Durgin     r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
715ad32e9c0SJosh Durgin     if (r < 0) {
71687cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error opening pool %s", pool);
717eb93d5d9SSage Weil         goto failed_shutdown;
718ad32e9c0SJosh Durgin     }
719ad32e9c0SJosh Durgin 
720ad32e9c0SJosh Durgin     r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
721ad32e9c0SJosh Durgin     if (r < 0) {
72287cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error reading header from %s", s->name);
723eb93d5d9SSage Weil         goto failed_open;
724ad32e9c0SJosh Durgin     }
725ad32e9c0SJosh Durgin 
726ad32e9c0SJosh Durgin     bs->read_only = (s->snap != NULL);
727f27aaf4bSChristian Brunner 
728c3ca988dSKevin Wolf     qemu_opts_del(opts);
729f27aaf4bSChristian Brunner     return 0;
730f27aaf4bSChristian Brunner 
731eb93d5d9SSage Weil failed_open:
732ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
733eb93d5d9SSage Weil failed_shutdown:
734ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
735eb93d5d9SSage Weil     g_free(s->snap);
736*730b00bbSMarkus Armbruster     g_free(s->name);
737c3ca988dSKevin Wolf failed_opts:
738c3ca988dSKevin Wolf     qemu_opts_del(opts);
7390a55679bSJeff Cody     g_free(mon_host);
7400a55679bSJeff Cody     g_free(auth_supported);
741f27aaf4bSChristian Brunner     return r;
742f27aaf4bSChristian Brunner }
743f27aaf4bSChristian Brunner 
744ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
745f27aaf4bSChristian Brunner {
746f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
747f27aaf4bSChristian Brunner 
748ad32e9c0SJosh Durgin     rbd_close(s->image);
749ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
7507267c094SAnthony Liguori     g_free(s->snap);
751*730b00bbSMarkus Armbruster     g_free(s->name);
752ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
753f27aaf4bSChristian Brunner }
754f27aaf4bSChristian Brunner 
755d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
756f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
757f27aaf4bSChristian Brunner };
758f27aaf4bSChristian Brunner 
759e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
760f27aaf4bSChristian Brunner {
761e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
762e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
763ad32e9c0SJosh Durgin }
764ad32e9c0SJosh Durgin 
765ad32e9c0SJosh Durgin /*
766ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
767ad32e9c0SJosh Durgin  *
768ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
769ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
770e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
771e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
772ad32e9c0SJosh Durgin  */
773ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
774ad32e9c0SJosh Durgin {
775e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
776e04fb07fSStefan Hajnoczi 
777ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
778ad32e9c0SJosh Durgin     rbd_aio_release(c);
779f27aaf4bSChristian Brunner 
780fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
781ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
782473c7f02SStefan Priebe }
783f27aaf4bSChristian Brunner 
784787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
785787f3133SJosh Durgin                                    uint64_t off,
786787f3133SJosh Durgin                                    uint64_t len,
787787f3133SJosh Durgin                                    rbd_completion_t comp)
788787f3133SJosh Durgin {
789787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
790787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
791787f3133SJosh Durgin #else
792787f3133SJosh Durgin     return -ENOTSUP;
793787f3133SJosh Durgin #endif
794787f3133SJosh Durgin }
795787f3133SJosh Durgin 
796dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
797dc7588c1SJosh Durgin                                  rbd_completion_t comp)
798dc7588c1SJosh Durgin {
799dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
800dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
801dc7588c1SJosh Durgin #else
802dc7588c1SJosh Durgin     return -ENOTSUP;
803dc7588c1SJosh Durgin #endif
804dc7588c1SJosh Durgin }
805dc7588c1SJosh Durgin 
8067c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8077bbca9e2SEric Blake                                  int64_t off,
808f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8097bbca9e2SEric Blake                                  int64_t size,
810097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
811787f3133SJosh Durgin                                  void *opaque,
812787f3133SJosh Durgin                                  RBDAIOCmd cmd)
813f27aaf4bSChristian Brunner {
814f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8150f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
816ad32e9c0SJosh Durgin     rbd_completion_t c;
81751a13528SJosh Durgin     int r;
818f27aaf4bSChristian Brunner 
819f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
820f27aaf4bSChristian Brunner 
821d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
822787f3133SJosh Durgin     acb->cmd = cmd;
823f27aaf4bSChristian Brunner     acb->qiov = qiov;
8247bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8251d393bdeStianqing 
8261d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8271d393bdeStianqing 
8281d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
829dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
830787f3133SJosh Durgin             acb->bounce = NULL;
831787f3133SJosh Durgin         } else {
8320f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8330f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8340f7a0237SKevin Wolf                 goto failed;
8350f7a0237SKevin Wolf             }
836787f3133SJosh Durgin         }
8371d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
8381d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
8391d393bdeStianqing         }
8401d393bdeStianqing         rcb->buf = acb->bounce;
8411d393bdeStianqing     }
8421d393bdeStianqing 
843f27aaf4bSChristian Brunner     acb->ret = 0;
844f27aaf4bSChristian Brunner     acb->error = 0;
845f27aaf4bSChristian Brunner     acb->s = s;
846f27aaf4bSChristian Brunner 
847f27aaf4bSChristian Brunner     rcb->acb = acb;
848f27aaf4bSChristian Brunner     rcb->s = acb->s;
849ad32e9c0SJosh Durgin     rcb->size = size;
85051a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
85151a13528SJosh Durgin     if (r < 0) {
85251a13528SJosh Durgin         goto failed;
85351a13528SJosh Durgin     }
854f27aaf4bSChristian Brunner 
855787f3133SJosh Durgin     switch (cmd) {
856787f3133SJosh Durgin     case RBD_AIO_WRITE:
8571d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8581d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
8591d393bdeStianqing #else
8601d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
8611d393bdeStianqing #endif
862787f3133SJosh Durgin         break;
863787f3133SJosh Durgin     case RBD_AIO_READ:
8641d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8651d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
8661d393bdeStianqing #else
8671d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
8681d393bdeStianqing #endif
869787f3133SJosh Durgin         break;
870787f3133SJosh Durgin     case RBD_AIO_DISCARD:
871787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
872787f3133SJosh Durgin         break;
873dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
874dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
875dc7588c1SJosh Durgin         break;
876787f3133SJosh Durgin     default:
877787f3133SJosh Durgin         r = -EINVAL;
87851a13528SJosh Durgin     }
87951a13528SJosh Durgin 
88051a13528SJosh Durgin     if (r < 0) {
881405a2764SKevin Wolf         goto failed_completion;
882f27aaf4bSChristian Brunner     }
883f27aaf4bSChristian Brunner     return &acb->common;
88451a13528SJosh Durgin 
885405a2764SKevin Wolf failed_completion:
886405a2764SKevin Wolf     rbd_aio_release(c);
88751a13528SJosh Durgin failed:
8887267c094SAnthony Liguori     g_free(rcb);
8891d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
890405a2764SKevin Wolf         qemu_vfree(acb->bounce);
8911d393bdeStianqing     }
8921d393bdeStianqing 
8938007429aSFam Zheng     qemu_aio_unref(acb);
89451a13528SJosh Durgin     return NULL;
895f27aaf4bSChristian Brunner }
896f27aaf4bSChristian Brunner 
8977c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
898ad32e9c0SJosh Durgin                                       int64_t sector_num,
899ad32e9c0SJosh Durgin                                       QEMUIOVector *qiov,
900f27aaf4bSChristian Brunner                                       int nb_sectors,
901097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
902f27aaf4bSChristian Brunner                                       void *opaque)
903f27aaf4bSChristian Brunner {
9047bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
905e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
906787f3133SJosh Durgin                          RBD_AIO_READ);
907f27aaf4bSChristian Brunner }
908f27aaf4bSChristian Brunner 
9097c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
910ad32e9c0SJosh Durgin                                        int64_t sector_num,
911ad32e9c0SJosh Durgin                                        QEMUIOVector *qiov,
912f27aaf4bSChristian Brunner                                        int nb_sectors,
913097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
914f27aaf4bSChristian Brunner                                        void *opaque)
915f27aaf4bSChristian Brunner {
9167bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
917e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
918787f3133SJosh Durgin                          RBD_AIO_WRITE);
919f27aaf4bSChristian Brunner }
920f27aaf4bSChristian Brunner 
921dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9227c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
923097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
924dc7588c1SJosh Durgin                                       void *opaque)
925dc7588c1SJosh Durgin {
926dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
927dc7588c1SJosh Durgin }
928dc7588c1SJosh Durgin 
929dc7588c1SJosh Durgin #else
930dc7588c1SJosh Durgin 
9318b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9327a3f5fe9SSage Weil {
9337a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9347a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9357a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9367a3f5fe9SSage Weil     return rbd_flush(s->image);
9377a3f5fe9SSage Weil #else
9387a3f5fe9SSage Weil     return 0;
9397a3f5fe9SSage Weil #endif
9407a3f5fe9SSage Weil }
941dc7588c1SJosh Durgin #endif
9427a3f5fe9SSage Weil 
943ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
944f27aaf4bSChristian Brunner {
945f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
946ad32e9c0SJosh Durgin     rbd_image_info_t info;
947ad32e9c0SJosh Durgin     int r;
948ad32e9c0SJosh Durgin 
949ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
950ad32e9c0SJosh Durgin     if (r < 0) {
951ad32e9c0SJosh Durgin         return r;
952ad32e9c0SJosh Durgin     }
953ad32e9c0SJosh Durgin 
954ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
955f27aaf4bSChristian Brunner     return 0;
956f27aaf4bSChristian Brunner }
957f27aaf4bSChristian Brunner 
958ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
959f27aaf4bSChristian Brunner {
960f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
961ad32e9c0SJosh Durgin     rbd_image_info_t info;
962ad32e9c0SJosh Durgin     int r;
963f27aaf4bSChristian Brunner 
964ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
965ad32e9c0SJosh Durgin     if (r < 0) {
966ad32e9c0SJosh Durgin         return r;
967f27aaf4bSChristian Brunner     }
968f27aaf4bSChristian Brunner 
969ad32e9c0SJosh Durgin     return info.size;
970ad32e9c0SJosh Durgin }
971ad32e9c0SJosh Durgin 
97230cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
97330cdc48cSJosh Durgin {
97430cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
97530cdc48cSJosh Durgin     int r;
97630cdc48cSJosh Durgin 
97730cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
97830cdc48cSJosh Durgin     if (r < 0) {
97930cdc48cSJosh Durgin         return r;
98030cdc48cSJosh Durgin     }
98130cdc48cSJosh Durgin 
98230cdc48cSJosh Durgin     return 0;
98330cdc48cSJosh Durgin }
98430cdc48cSJosh Durgin 
985ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
986ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
987f27aaf4bSChristian Brunner {
988f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
989f27aaf4bSChristian Brunner     int r;
990f27aaf4bSChristian Brunner 
991f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
992f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
993f27aaf4bSChristian Brunner     }
994f27aaf4bSChristian Brunner 
995f27aaf4bSChristian Brunner     /*
996f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
997f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
998f27aaf4bSChristian Brunner      */
999f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1000f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1001f27aaf4bSChristian Brunner         return -EINVAL;
1002f27aaf4bSChristian Brunner     }
1003f27aaf4bSChristian Brunner 
1004f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1005f27aaf4bSChristian Brunner         return -ERANGE;
1006f27aaf4bSChristian Brunner     }
1007f27aaf4bSChristian Brunner 
1008ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1009f27aaf4bSChristian Brunner     if (r < 0) {
1010ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1011f27aaf4bSChristian Brunner         return r;
1012f27aaf4bSChristian Brunner     }
1013f27aaf4bSChristian Brunner 
1014f27aaf4bSChristian Brunner     return 0;
1015f27aaf4bSChristian Brunner }
1016f27aaf4bSChristian Brunner 
1017bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1018a89d89d3SWenchao Xia                                 const char *snapshot_id,
1019a89d89d3SWenchao Xia                                 const char *snapshot_name,
1020a89d89d3SWenchao Xia                                 Error **errp)
1021bd603247SGregory Farnum {
1022bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1023bd603247SGregory Farnum     int r;
1024bd603247SGregory Farnum 
1025a89d89d3SWenchao Xia     if (!snapshot_name) {
1026a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1027a89d89d3SWenchao Xia         return -EINVAL;
1028a89d89d3SWenchao Xia     }
1029a89d89d3SWenchao Xia 
1030a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1031a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1032a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1033a89d89d3SWenchao Xia         error_setg(errp,
1034a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1035a89d89d3SWenchao Xia                    "equal to snapshot name");
1036a89d89d3SWenchao Xia         return -EINVAL;
1037a89d89d3SWenchao Xia     }
1038a89d89d3SWenchao Xia 
1039bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1040a89d89d3SWenchao Xia     if (r < 0) {
1041a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1042a89d89d3SWenchao Xia     }
1043bd603247SGregory Farnum     return r;
1044bd603247SGregory Farnum }
1045bd603247SGregory Farnum 
1046bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1047bd603247SGregory Farnum                                   const char *snapshot_name)
1048bd603247SGregory Farnum {
1049bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1050bd603247SGregory Farnum 
10519be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1052bd603247SGregory Farnum }
1053bd603247SGregory Farnum 
1054ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1055ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1056f27aaf4bSChristian Brunner {
1057f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1058f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1059ad32e9c0SJosh Durgin     int i, snap_count;
1060ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1061ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1062f27aaf4bSChristian Brunner 
1063ad32e9c0SJosh Durgin     do {
106402c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1065ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
10669e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
10677267c094SAnthony Liguori             g_free(snaps);
1068f27aaf4bSChristian Brunner         }
1069ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1070f27aaf4bSChristian Brunner 
1071ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1072b9c53290SJosh Durgin         goto done;
1073f27aaf4bSChristian Brunner     }
1074f27aaf4bSChristian Brunner 
10755839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1076f27aaf4bSChristian Brunner 
1077ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1078ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1079f27aaf4bSChristian Brunner 
1080f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1081f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1082f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1083f27aaf4bSChristian Brunner 
1084ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1085f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1086f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1087f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1088f27aaf4bSChristian Brunner     }
1089ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
10909e6337d0SStefan Hajnoczi     g_free(snaps);
1091ad32e9c0SJosh Durgin 
1092b9c53290SJosh Durgin  done:
1093f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1094f27aaf4bSChristian Brunner     return snap_count;
1095f27aaf4bSChristian Brunner }
1096f27aaf4bSChristian Brunner 
1097787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
10984da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
10994da444a0SEric Blake                                          int64_t offset,
11004da444a0SEric Blake                                          int count,
1101097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1102787f3133SJosh Durgin                                          void *opaque)
1103787f3133SJosh Durgin {
11044da444a0SEric Blake     return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
1105787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1106787f3133SJosh Durgin }
1107787f3133SJosh Durgin #endif
1108787f3133SJosh Durgin 
1109be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1110be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
1111be217884SAdam Crume                                       Error **errp)
1112be217884SAdam Crume {
1113be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1114be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1115be217884SAdam Crume     if (r < 0) {
1116be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1117be217884SAdam Crume     }
1118be217884SAdam Crume }
1119be217884SAdam Crume #endif
1120be217884SAdam Crume 
1121bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1122bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1123bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1124bd0cf596SChunyan Liu     .desc = {
1125f27aaf4bSChristian Brunner         {
1126f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1127bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1128f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1129f27aaf4bSChristian Brunner         },
1130f27aaf4bSChristian Brunner         {
1131f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1132bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1133f27aaf4bSChristian Brunner             .help = "RBD object size"
1134f27aaf4bSChristian Brunner         },
113560390a21SDaniel P. Berrange         {
113660390a21SDaniel P. Berrange             .name = "password-secret",
113760390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
113860390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
113960390a21SDaniel P. Berrange         },
1140bd0cf596SChunyan Liu         { /* end of list */ }
1141bd0cf596SChunyan Liu     }
1142f27aaf4bSChristian Brunner };
1143f27aaf4bSChristian Brunner 
1144f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1145f27aaf4bSChristian Brunner     .format_name            = "rbd",
1146f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1147c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1148ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1149ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
1150c282e1fdSChunyan Liu     .bdrv_create            = qemu_rbd_create,
11513ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1152ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1153bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1154ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
115530cdc48cSJosh Durgin     .bdrv_truncate          = qemu_rbd_truncate,
1156f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1157f27aaf4bSChristian Brunner 
1158ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
1159ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
1160dc7588c1SJosh Durgin 
1161dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1162dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1163dc7588c1SJosh Durgin #else
1164c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1165dc7588c1SJosh Durgin #endif
1166f27aaf4bSChristian Brunner 
1167787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11684da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1169787f3133SJosh Durgin #endif
1170787f3133SJosh Durgin 
1171ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1172bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1173ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1174bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1175be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1176be217884SAdam Crume     .bdrv_invalidate_cache  = qemu_rbd_invalidate_cache,
1177be217884SAdam Crume #endif
1178f27aaf4bSChristian Brunner };
1179f27aaf4bSChristian Brunner 
1180f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1181f27aaf4bSChristian Brunner {
1182f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1183f27aaf4bSChristian Brunner }
1184f27aaf4bSChristian Brunner 
1185f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1186