xref: /qemu/block/rbd.c (revision f51c363c)
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_CONF_NAME_SIZE 128
60ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512
61ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024
62ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128
63ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128
64ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
65ad32e9c0SJosh Durgin 
661d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
671d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
681d393bdeStianqing #define LIBRBD_USE_IOVEC 1
691d393bdeStianqing #else
701d393bdeStianqing #define LIBRBD_USE_IOVEC 0
711d393bdeStianqing #endif
721d393bdeStianqing 
73787f3133SJosh Durgin typedef enum {
74787f3133SJosh Durgin     RBD_AIO_READ,
75787f3133SJosh Durgin     RBD_AIO_WRITE,
76dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
77dc7588c1SJosh Durgin     RBD_AIO_FLUSH
78787f3133SJosh Durgin } RBDAIOCmd;
79787f3133SJosh Durgin 
80f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
817c84b1b8SMarkus Armbruster     BlockAIOCB common;
8208448d51SStefan Priebe     int64_t ret;
83f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
84f27aaf4bSChristian Brunner     char *bounce;
85787f3133SJosh Durgin     RBDAIOCmd cmd;
86f27aaf4bSChristian Brunner     int error;
87f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
88f27aaf4bSChristian Brunner } RBDAIOCB;
89f27aaf4bSChristian Brunner 
90f27aaf4bSChristian Brunner typedef struct RADOSCB {
91f27aaf4bSChristian Brunner     RBDAIOCB *acb;
92f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
93ad32e9c0SJosh Durgin     int64_t size;
94f27aaf4bSChristian Brunner     char *buf;
9508448d51SStefan Priebe     int64_t ret;
96f27aaf4bSChristian Brunner } RADOSCB;
97f27aaf4bSChristian Brunner 
98f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
99ad32e9c0SJosh Durgin     rados_t cluster;
100ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
101ad32e9c0SJosh Durgin     rbd_image_t image;
102ad32e9c0SJosh Durgin     char name[RBD_MAX_IMAGE_NAME_SIZE];
103ad32e9c0SJosh Durgin     char *snap;
104f27aaf4bSChristian Brunner } BDRVRBDState;
105f27aaf4bSChristian Brunner 
1067830f909SJeff Cody static char *qemu_rbd_next_tok(int max_len,
107f27aaf4bSChristian Brunner                                char *src, char delim,
108f27aaf4bSChristian Brunner                                const char *name,
109d61563b2SMarkus Armbruster                                char **p, Error **errp)
110f27aaf4bSChristian Brunner {
111f27aaf4bSChristian Brunner     int l;
112f27aaf4bSChristian Brunner     char *end;
113f27aaf4bSChristian Brunner 
114f27aaf4bSChristian Brunner     *p = NULL;
115f27aaf4bSChristian Brunner 
116f27aaf4bSChristian Brunner     if (delim != '\0') {
11716a06b24SSage Weil         for (end = src; *end; ++end) {
11816a06b24SSage Weil             if (*end == delim) {
11916a06b24SSage Weil                 break;
12016a06b24SSage Weil             }
12116a06b24SSage Weil             if (*end == '\\' && end[1] != '\0') {
12216a06b24SSage Weil                 end++;
12316a06b24SSage Weil             }
12416a06b24SSage Weil         }
12516a06b24SSage Weil         if (*end == delim) {
126f27aaf4bSChristian Brunner             *p = end + 1;
127f27aaf4bSChristian Brunner             *end = '\0';
128f27aaf4bSChristian Brunner         }
129f27aaf4bSChristian Brunner     }
130f27aaf4bSChristian Brunner     l = strlen(src);
1317830f909SJeff Cody     if (l >= max_len) {
132d61563b2SMarkus Armbruster         error_setg(errp, "%s too long", name);
1337830f909SJeff Cody         return NULL;
134f27aaf4bSChristian Brunner     } else if (l == 0) {
135d61563b2SMarkus Armbruster         error_setg(errp, "%s too short", name);
1367830f909SJeff Cody         return NULL;
137f27aaf4bSChristian Brunner     }
138f27aaf4bSChristian Brunner 
1397830f909SJeff Cody     return src;
140f27aaf4bSChristian Brunner }
141f27aaf4bSChristian Brunner 
14216a06b24SSage Weil static void qemu_rbd_unescape(char *src)
14316a06b24SSage Weil {
14416a06b24SSage Weil     char *p;
14516a06b24SSage Weil 
14616a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14716a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
14816a06b24SSage Weil             src++;
14916a06b24SSage Weil         }
15016a06b24SSage Weil         *p = *src;
15116a06b24SSage Weil     }
15216a06b24SSage Weil     *p = '\0';
15316a06b24SSage Weil }
15416a06b24SSage Weil 
155c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
156d61563b2SMarkus Armbruster                                     Error **errp)
157f27aaf4bSChristian Brunner {
158f27aaf4bSChristian Brunner     const char *start;
159c7cacb3eSJeff Cody     char *p, *buf, *keypairs;
1607830f909SJeff Cody     char *found_str;
161c7cacb3eSJeff Cody     size_t max_keypair_size;
1627830f909SJeff Cody     Error *local_err = NULL;
163f27aaf4bSChristian Brunner 
164f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
165d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
166c7cacb3eSJeff Cody         return;
167f27aaf4bSChristian Brunner     }
168f27aaf4bSChristian Brunner 
169c7cacb3eSJeff Cody     max_keypair_size = strlen(start) + 1;
1707267c094SAnthony Liguori     buf = g_strdup(start);
171c7cacb3eSJeff Cody     keypairs = g_malloc0(max_keypair_size);
172f27aaf4bSChristian Brunner     p = buf;
173f27aaf4bSChristian Brunner 
174c7cacb3eSJeff Cody     found_str = qemu_rbd_next_tok(RBD_MAX_POOL_NAME_SIZE, p,
1757830f909SJeff Cody                                   '/', "pool name", &p, &local_err);
1767830f909SJeff Cody     if (local_err) {
177f27aaf4bSChristian Brunner         goto done;
178f27aaf4bSChristian Brunner     }
1797830f909SJeff Cody     if (!p) {
1807830f909SJeff Cody         error_setg(errp, "Pool name is required");
1817830f909SJeff Cody         goto done;
1827830f909SJeff Cody     }
1837830f909SJeff Cody     qemu_rbd_unescape(found_str);
184c7cacb3eSJeff Cody     qdict_put(options, "pool", qstring_from_str(found_str));
185fab5cf59SJosh Durgin 
186fab5cf59SJosh Durgin     if (strchr(p, '@')) {
187c7cacb3eSJeff Cody         found_str = qemu_rbd_next_tok(RBD_MAX_IMAGE_NAME_SIZE, p,
1887830f909SJeff Cody                                       '@', "object name", &p, &local_err);
1897830f909SJeff Cody         if (local_err) {
190f27aaf4bSChristian Brunner             goto done;
191f27aaf4bSChristian Brunner         }
1927830f909SJeff Cody         qemu_rbd_unescape(found_str);
193c7cacb3eSJeff Cody         qdict_put(options, "image", qstring_from_str(found_str));
1947830f909SJeff Cody 
195c7cacb3eSJeff Cody         found_str = qemu_rbd_next_tok(RBD_MAX_SNAP_NAME_SIZE, p,
1967830f909SJeff Cody                                       ':', "snap name", &p, &local_err);
1977830f909SJeff Cody         if (local_err) {
1987830f909SJeff Cody             goto done;
199fab5cf59SJosh Durgin         }
2007830f909SJeff Cody         qemu_rbd_unescape(found_str);
201c7cacb3eSJeff Cody         qdict_put(options, "snapshot", qstring_from_str(found_str));
2027830f909SJeff Cody     } else {
203c7cacb3eSJeff Cody         found_str = qemu_rbd_next_tok(RBD_MAX_IMAGE_NAME_SIZE, p,
2047830f909SJeff Cody                                       ':', "object name", &p, &local_err);
2057830f909SJeff Cody         if (local_err) {
2067830f909SJeff Cody             goto done;
2077830f909SJeff Cody         }
2087830f909SJeff Cody         qemu_rbd_unescape(found_str);
209c7cacb3eSJeff Cody         qdict_put(options, "image", qstring_from_str(found_str));
2107830f909SJeff Cody     }
2117830f909SJeff Cody     if (!p) {
212f27aaf4bSChristian Brunner         goto done;
213f27aaf4bSChristian Brunner     }
214f27aaf4bSChristian Brunner 
215c7cacb3eSJeff Cody     found_str = qemu_rbd_next_tok(RBD_MAX_CONF_NAME_SIZE, p,
2167830f909SJeff Cody                                   '\0', "configuration", &p, &local_err);
2177830f909SJeff Cody     if (local_err) {
2187830f909SJeff Cody         goto done;
2197830f909SJeff Cody     }
220c7cacb3eSJeff Cody 
221c7cacb3eSJeff Cody     p = found_str;
222c7cacb3eSJeff Cody 
223c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
224c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
225c7cacb3eSJeff Cody     while (p) {
226c7cacb3eSJeff Cody         char *name, *value;
227c7cacb3eSJeff Cody         name = qemu_rbd_next_tok(RBD_MAX_CONF_NAME_SIZE, p,
228c7cacb3eSJeff Cody                                  '=', "conf option name", &p, &local_err);
229c7cacb3eSJeff Cody         if (local_err) {
230c7cacb3eSJeff Cody             break;
231c7cacb3eSJeff Cody         }
232c7cacb3eSJeff Cody 
233c7cacb3eSJeff Cody         if (!p) {
234c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
235c7cacb3eSJeff Cody             break;
236c7cacb3eSJeff Cody         }
237c7cacb3eSJeff Cody 
238c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
239c7cacb3eSJeff Cody 
240c7cacb3eSJeff Cody         value = qemu_rbd_next_tok(RBD_MAX_CONF_VAL_SIZE, p,
241c7cacb3eSJeff Cody                                   ':', "conf option value", &p, &local_err);
242c7cacb3eSJeff Cody         if (local_err) {
243c7cacb3eSJeff Cody             break;
244c7cacb3eSJeff Cody         }
245c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
246c7cacb3eSJeff Cody 
247c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
248c7cacb3eSJeff Cody             qdict_put(options, "conf", qstring_from_str(value));
249c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
250c7cacb3eSJeff Cody             qdict_put(options, "user" , qstring_from_str(value));
251c7cacb3eSJeff Cody         } else {
252c7cacb3eSJeff Cody             /* FIXME: This is pretty ugly, and not the right way to do this.
253c7cacb3eSJeff Cody              *        These should be contained in a structure, and then
254c7cacb3eSJeff Cody              *        passed explicitly as individual key/value pairs to
255c7cacb3eSJeff Cody              *        rados.  Consider this legacy code that needs to be
256c7cacb3eSJeff Cody              *        updated. */
257c7cacb3eSJeff Cody             char *tmp = g_malloc0(max_keypair_size);
258c7cacb3eSJeff Cody             /* only use a delimiter if it is not the first keypair found */
259c7cacb3eSJeff Cody             /* These are sets of unknown key/value pairs we'll pass along
260c7cacb3eSJeff Cody              * to ceph */
261c7cacb3eSJeff Cody             if (keypairs[0]) {
262c7cacb3eSJeff Cody                 snprintf(tmp, max_keypair_size, ":%s=%s", name, value);
263c7cacb3eSJeff Cody                 pstrcat(keypairs, max_keypair_size, tmp);
264c7cacb3eSJeff Cody             } else {
265c7cacb3eSJeff Cody                 snprintf(keypairs, max_keypair_size, "%s=%s", name, value);
266c7cacb3eSJeff Cody             }
267c7cacb3eSJeff Cody             g_free(tmp);
268c7cacb3eSJeff Cody         }
269c7cacb3eSJeff Cody     }
270c7cacb3eSJeff Cody 
271c7cacb3eSJeff Cody     if (keypairs[0]) {
272c7cacb3eSJeff Cody         qdict_put(options, "keyvalue-pairs", qstring_from_str(keypairs));
273c7cacb3eSJeff Cody     }
274c7cacb3eSJeff Cody 
275f27aaf4bSChristian Brunner 
276f27aaf4bSChristian Brunner done:
2777830f909SJeff Cody     if (local_err) {
2787830f909SJeff Cody         error_propagate(errp, local_err);
2797830f909SJeff Cody     }
2807267c094SAnthony Liguori     g_free(buf);
281c7cacb3eSJeff Cody     g_free(keypairs);
282c7cacb3eSJeff Cody     return;
2837c7e9df0SSage Weil }
2847c7e9df0SSage Weil 
28560390a21SDaniel P. Berrange 
28660390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
28760390a21SDaniel P. Berrange                              Error **errp)
28860390a21SDaniel P. Berrange {
28960390a21SDaniel P. Berrange     if (secretid == 0) {
29060390a21SDaniel P. Berrange         return 0;
29160390a21SDaniel P. Berrange     }
29260390a21SDaniel P. Berrange 
29360390a21SDaniel P. Berrange     gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
29460390a21SDaniel P. Berrange                                                     errp);
29560390a21SDaniel P. Berrange     if (!secret) {
29660390a21SDaniel P. Berrange         return -1;
29760390a21SDaniel P. Berrange     }
29860390a21SDaniel P. Berrange 
29960390a21SDaniel P. Berrange     rados_conf_set(cluster, "key", secret);
30060390a21SDaniel P. Berrange     g_free(secret);
30160390a21SDaniel P. Berrange 
30260390a21SDaniel P. Berrange     return 0;
30360390a21SDaniel P. Berrange }
30460390a21SDaniel P. Berrange 
305c7cacb3eSJeff Cody static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs,
306e34d8f29SJosh Durgin                                  Error **errp)
307fab5cf59SJosh Durgin {
308fab5cf59SJosh Durgin     char *p, *buf;
3097830f909SJeff Cody     char *name;
3107830f909SJeff Cody     char *value;
3117830f909SJeff Cody     Error *local_err = NULL;
312fab5cf59SJosh Durgin     int ret = 0;
313fab5cf59SJosh Durgin 
314c7cacb3eSJeff Cody     buf = g_strdup(keypairs);
315fab5cf59SJosh Durgin     p = buf;
316fab5cf59SJosh Durgin 
317fab5cf59SJosh Durgin     while (p) {
3187830f909SJeff Cody         name = qemu_rbd_next_tok(RBD_MAX_CONF_NAME_SIZE, p,
3197830f909SJeff Cody                                  '=', "conf option name", &p, &local_err);
3207830f909SJeff Cody         if (local_err) {
321fab5cf59SJosh Durgin             break;
322fab5cf59SJosh Durgin         }
323fab5cf59SJosh Durgin 
324fab5cf59SJosh Durgin         if (!p) {
325d61563b2SMarkus Armbruster             error_setg(errp, "conf option %s has no value", name);
326fab5cf59SJosh Durgin             ret = -EINVAL;
327fab5cf59SJosh Durgin             break;
328fab5cf59SJosh Durgin         }
329fab5cf59SJosh Durgin 
3307830f909SJeff Cody         value = qemu_rbd_next_tok(RBD_MAX_CONF_VAL_SIZE, p,
3317830f909SJeff Cody                                   ':', "conf option value", &p, &local_err);
3327830f909SJeff Cody         if (local_err) {
333fab5cf59SJosh Durgin             break;
334fab5cf59SJosh Durgin         }
335fab5cf59SJosh Durgin 
336fab5cf59SJosh Durgin         ret = rados_conf_set(cluster, name, value);
337fab5cf59SJosh Durgin         if (ret < 0) {
33887cd3d20SVikhyat Umrao             error_setg_errno(errp, -ret, "invalid conf option %s", name);
339fab5cf59SJosh Durgin             ret = -EINVAL;
340fab5cf59SJosh Durgin             break;
341fab5cf59SJosh Durgin         }
342fab5cf59SJosh Durgin     }
343fab5cf59SJosh Durgin 
3447830f909SJeff Cody     if (local_err) {
3457830f909SJeff Cody         error_propagate(errp, local_err);
3467830f909SJeff Cody         ret = -EINVAL;
3477830f909SJeff Cody     }
3487267c094SAnthony Liguori     g_free(buf);
349fab5cf59SJosh Durgin     return ret;
350fab5cf59SJosh Durgin }
351fab5cf59SJosh Durgin 
3521d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
3531d393bdeStianqing {
3541d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
3551d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
3561d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
3571d393bdeStianqing                    acb->qiov->size - offs);
3581d393bdeStianqing     } else {
3591d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
3601d393bdeStianqing     }
3611d393bdeStianqing }
3621d393bdeStianqing 
3630f9d252dSJeff Cody static QemuOptsList runtime_opts = {
3640f9d252dSJeff Cody     .name = "rbd",
3650f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3660f9d252dSJeff Cody     .desc = {
3670f9d252dSJeff Cody         {
3680f9d252dSJeff Cody             .name = "filename",
3690f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3700f9d252dSJeff Cody             .help = "Specification of the rbd image",
3710f9d252dSJeff Cody         },
3720f9d252dSJeff Cody         {
3730f9d252dSJeff Cody             .name = "password-secret",
3740f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3750f9d252dSJeff Cody             .help = "ID of secret providing the password",
3760f9d252dSJeff Cody         },
3770f9d252dSJeff Cody         {
3780f9d252dSJeff Cody             .name = "conf",
3790f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3800f9d252dSJeff Cody             .help = "Rados config file location",
3810f9d252dSJeff Cody         },
3820f9d252dSJeff Cody         {
3830f9d252dSJeff Cody             .name = "pool",
3840f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3850f9d252dSJeff Cody             .help = "Rados pool name",
3860f9d252dSJeff Cody         },
3870f9d252dSJeff Cody         {
3880f9d252dSJeff Cody             .name = "image",
3890f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3900f9d252dSJeff Cody             .help = "Image name in the pool",
3910f9d252dSJeff Cody         },
3920f9d252dSJeff Cody         {
3930f9d252dSJeff Cody             .name = "snapshot",
3940f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3950f9d252dSJeff Cody             .help = "Ceph snapshot name",
3960f9d252dSJeff Cody         },
3970f9d252dSJeff Cody         {
3980f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3990f9d252dSJeff Cody             .name = "user",
4000f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
4010f9d252dSJeff Cody             .help = "Rados id name",
4020f9d252dSJeff Cody         },
4030f9d252dSJeff Cody         {
4040f9d252dSJeff Cody             .name = "keyvalue-pairs",
4050f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
4060f9d252dSJeff Cody             .help = "Legacy rados key/value option parameters",
4070f9d252dSJeff Cody         },
4080a55679bSJeff Cody         {
4090a55679bSJeff Cody             .name = "host",
4100a55679bSJeff Cody             .type = QEMU_OPT_STRING,
4110a55679bSJeff Cody         },
4120a55679bSJeff Cody         {
4130a55679bSJeff Cody             .name = "port",
4140a55679bSJeff Cody             .type = QEMU_OPT_STRING,
4150a55679bSJeff Cody         },
4160a55679bSJeff Cody         {
4170a55679bSJeff Cody             .name = "auth",
4180a55679bSJeff Cody             .type = QEMU_OPT_STRING,
4190a55679bSJeff Cody             .help = "Supported authentication method, either cephx or none",
4200a55679bSJeff Cody         },
4210f9d252dSJeff Cody         { /* end of list */ }
4220f9d252dSJeff Cody     },
4230f9d252dSJeff Cody };
4240f9d252dSJeff Cody 
425bd0cf596SChunyan Liu static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
426f27aaf4bSChristian Brunner {
427d61563b2SMarkus Armbruster     Error *local_err = NULL;
428f27aaf4bSChristian Brunner     int64_t bytes = 0;
429f27aaf4bSChristian Brunner     int64_t objsize;
430ad32e9c0SJosh Durgin     int obj_order = 0;
431c7cacb3eSJeff Cody     const char *pool, *name, *conf, *clientname, *keypairs;
43260390a21SDaniel P. Berrange     const char *secretid;
433ad32e9c0SJosh Durgin     rados_t cluster;
434ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
435c7cacb3eSJeff Cody     QDict *options = NULL;
436c7cacb3eSJeff Cody     QemuOpts *rbd_opts = NULL;
437c7cacb3eSJeff Cody     int ret = 0;
438f27aaf4bSChristian Brunner 
43960390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
44060390a21SDaniel P. Berrange 
441f27aaf4bSChristian Brunner     /* Read out options */
442c2eb918eSHu Tao     bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
443c2eb918eSHu Tao                      BDRV_SECTOR_SIZE);
444bd0cf596SChunyan Liu     objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
445bd0cf596SChunyan Liu     if (objsize) {
446f27aaf4bSChristian Brunner         if ((objsize - 1) & objsize) {    /* not a power of 2? */
447d61563b2SMarkus Armbruster             error_setg(errp, "obj size needs to be power of 2");
448c7cacb3eSJeff Cody             ret = -EINVAL;
449c7cacb3eSJeff Cody             goto exit;
450f27aaf4bSChristian Brunner         }
451f27aaf4bSChristian Brunner         if (objsize < 4096) {
452d61563b2SMarkus Armbruster             error_setg(errp, "obj size too small");
453c7cacb3eSJeff Cody             ret = -EINVAL;
454c7cacb3eSJeff Cody             goto exit;
455f27aaf4bSChristian Brunner         }
456786a4ea8SStefan Hajnoczi         obj_order = ctz32(objsize);
457f27aaf4bSChristian Brunner     }
458f27aaf4bSChristian Brunner 
459c7cacb3eSJeff Cody     options = qdict_new();
460c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
461c7cacb3eSJeff Cody     if (local_err) {
462c7cacb3eSJeff Cody         ret = -EINVAL;
463c7cacb3eSJeff Cody         error_propagate(errp, local_err);
464c7cacb3eSJeff Cody         goto exit;
465c7cacb3eSJeff Cody     }
466c7cacb3eSJeff Cody 
467c7cacb3eSJeff Cody     rbd_opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
468c7cacb3eSJeff Cody     qemu_opts_absorb_qdict(rbd_opts, options, &local_err);
469c7cacb3eSJeff Cody     if (local_err) {
470c7cacb3eSJeff Cody         error_propagate(errp, local_err);
471c7cacb3eSJeff Cody         ret = -EINVAL;
472c7cacb3eSJeff Cody         goto exit;
473c7cacb3eSJeff Cody     }
474c7cacb3eSJeff Cody 
475c7cacb3eSJeff Cody     pool       = qemu_opt_get(rbd_opts, "pool");
476c7cacb3eSJeff Cody     conf       = qemu_opt_get(rbd_opts, "conf");
477c7cacb3eSJeff Cody     clientname = qemu_opt_get(rbd_opts, "user");
478c7cacb3eSJeff Cody     name       = qemu_opt_get(rbd_opts, "image");
479c7cacb3eSJeff Cody     keypairs   = qemu_opt_get(rbd_opts, "keyvalue-pairs");
480c7cacb3eSJeff Cody 
48187cd3d20SVikhyat Umrao     ret = rados_create(&cluster, clientname);
48287cd3d20SVikhyat Umrao     if (ret < 0) {
48387cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error initializing");
484c7cacb3eSJeff Cody         goto exit;
485f27aaf4bSChristian Brunner     }
486f27aaf4bSChristian Brunner 
487c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
488c7cacb3eSJeff Cody     ret = rados_conf_read_file(cluster, conf);
489c7cacb3eSJeff Cody     if (conf && ret < 0) {
490c7cacb3eSJeff Cody         error_setg_errno(errp, -ret, "error reading conf file %s", conf);
491e38f643aSXiubo Li         ret = -EIO;
492e38f643aSXiubo Li         goto shutdown;
493fab5cf59SJosh Durgin     }
494fab5cf59SJosh Durgin 
495c7cacb3eSJeff Cody     ret = qemu_rbd_set_keypairs(cluster, keypairs, errp);
496c7cacb3eSJeff Cody     if (ret < 0) {
497e38f643aSXiubo Li         ret = -EIO;
498e38f643aSXiubo Li         goto shutdown;
499fab5cf59SJosh Durgin     }
500ad32e9c0SJosh Durgin 
50160390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
502e38f643aSXiubo Li         ret = -EIO;
503e38f643aSXiubo Li         goto shutdown;
50460390a21SDaniel P. Berrange     }
50560390a21SDaniel P. Berrange 
50687cd3d20SVikhyat Umrao     ret = rados_connect(cluster);
50787cd3d20SVikhyat Umrao     if (ret < 0) {
50887cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error connecting");
509e38f643aSXiubo Li         goto shutdown;
510ad32e9c0SJosh Durgin     }
511ad32e9c0SJosh Durgin 
51287cd3d20SVikhyat Umrao     ret = rados_ioctx_create(cluster, pool, &io_ctx);
51387cd3d20SVikhyat Umrao     if (ret < 0) {
51487cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error opening pool %s", pool);
515e38f643aSXiubo Li         goto shutdown;
516f27aaf4bSChristian Brunner     }
517f27aaf4bSChristian Brunner 
518ad32e9c0SJosh Durgin     ret = rbd_create(io_ctx, name, bytes, &obj_order);
51987cd3d20SVikhyat Umrao     if (ret < 0) {
52087cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error rbd create");
52187cd3d20SVikhyat Umrao     }
522f27aaf4bSChristian Brunner 
523e38f643aSXiubo Li     rados_ioctx_destroy(io_ctx);
524e38f643aSXiubo Li 
525e38f643aSXiubo Li shutdown:
526e38f643aSXiubo Li     rados_shutdown(cluster);
527c7cacb3eSJeff Cody 
528c7cacb3eSJeff Cody exit:
529c7cacb3eSJeff Cody     QDECREF(options);
530c7cacb3eSJeff Cody     qemu_opts_del(rbd_opts);
531f27aaf4bSChristian Brunner     return ret;
532f27aaf4bSChristian Brunner }
533f27aaf4bSChristian Brunner 
534f27aaf4bSChristian Brunner /*
535e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
536e04fb07fSStefan Hajnoczi  * BH context.
537f27aaf4bSChristian Brunner  */
538ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
539f27aaf4bSChristian Brunner {
540f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
541f27aaf4bSChristian Brunner     int64_t r;
542f27aaf4bSChristian Brunner 
543f27aaf4bSChristian Brunner     r = rcb->ret;
544f27aaf4bSChristian Brunner 
545dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
546f27aaf4bSChristian Brunner         if (r < 0) {
547f27aaf4bSChristian Brunner             acb->ret = r;
548f27aaf4bSChristian Brunner             acb->error = 1;
549f27aaf4bSChristian Brunner         } else if (!acb->error) {
550ad32e9c0SJosh Durgin             acb->ret = rcb->size;
551f27aaf4bSChristian Brunner         }
552f27aaf4bSChristian Brunner     } else {
553ad32e9c0SJosh Durgin         if (r < 0) {
5541d393bdeStianqing             qemu_rbd_memset(rcb, 0);
555f27aaf4bSChristian Brunner             acb->ret = r;
556f27aaf4bSChristian Brunner             acb->error = 1;
557ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
5581d393bdeStianqing             qemu_rbd_memset(rcb, r);
559f27aaf4bSChristian Brunner             if (!acb->error) {
560ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
561f27aaf4bSChristian Brunner             }
562f27aaf4bSChristian Brunner         } else if (!acb->error) {
563ad32e9c0SJosh Durgin             acb->ret = r;
564f27aaf4bSChristian Brunner         }
565f27aaf4bSChristian Brunner     }
566e04fb07fSStefan Hajnoczi 
5677267c094SAnthony Liguori     g_free(rcb);
568e04fb07fSStefan Hajnoczi 
5691d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
570e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
571e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
572f27aaf4bSChristian Brunner         }
573e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5741d393bdeStianqing     }
5751d393bdeStianqing 
576e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
577f27aaf4bSChristian Brunner 
5788007429aSFam Zheng     qemu_aio_unref(acb);
579f27aaf4bSChristian Brunner }
580f27aaf4bSChristian Brunner 
5810a55679bSJeff Cody #define RBD_MON_HOST          0
5820a55679bSJeff Cody #define RBD_AUTH_SUPPORTED    1
5830a55679bSJeff Cody 
5840a55679bSJeff Cody static char *qemu_rbd_array_opts(QDict *options, const char *prefix, int type,
5850a55679bSJeff Cody                                  Error **errp)
5860a55679bSJeff Cody {
5870a55679bSJeff Cody     int num_entries;
5880a55679bSJeff Cody     QemuOpts *opts = NULL;
5890a55679bSJeff Cody     QDict *sub_options;
5900a55679bSJeff Cody     const char *host;
5910a55679bSJeff Cody     const char *port;
5920a55679bSJeff Cody     char *str;
5930a55679bSJeff Cody     char *rados_str = NULL;
5940a55679bSJeff Cody     Error *local_err = NULL;
5950a55679bSJeff Cody     int i;
5960a55679bSJeff Cody 
5970a55679bSJeff Cody     assert(type == RBD_MON_HOST || type == RBD_AUTH_SUPPORTED);
5980a55679bSJeff Cody 
5990a55679bSJeff Cody     num_entries = qdict_array_entries(options, prefix);
6000a55679bSJeff Cody 
6010a55679bSJeff Cody     if (num_entries < 0) {
6020a55679bSJeff Cody         error_setg(errp, "Parse error on RBD QDict array");
6030a55679bSJeff Cody         return NULL;
6040a55679bSJeff Cody     }
6050a55679bSJeff Cody 
6060a55679bSJeff Cody     for (i = 0; i < num_entries; i++) {
6070a55679bSJeff Cody         char *strbuf = NULL;
6080a55679bSJeff Cody         const char *value;
6090a55679bSJeff Cody         char *rados_str_tmp;
6100a55679bSJeff Cody 
6110a55679bSJeff Cody         str = g_strdup_printf("%s%d.", prefix, i);
6120a55679bSJeff Cody         qdict_extract_subqdict(options, &sub_options, str);
6130a55679bSJeff Cody         g_free(str);
6140a55679bSJeff Cody 
6150a55679bSJeff Cody         opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
6160a55679bSJeff Cody         qemu_opts_absorb_qdict(opts, sub_options, &local_err);
6170a55679bSJeff Cody         QDECREF(sub_options);
6180a55679bSJeff Cody         if (local_err) {
6190a55679bSJeff Cody             error_propagate(errp, local_err);
6200a55679bSJeff Cody             g_free(rados_str);
6210a55679bSJeff Cody             rados_str = NULL;
6220a55679bSJeff Cody             goto exit;
6230a55679bSJeff Cody         }
6240a55679bSJeff Cody 
6250a55679bSJeff Cody         if (type == RBD_MON_HOST) {
6260a55679bSJeff Cody             host = qemu_opt_get(opts, "host");
6270a55679bSJeff Cody             port = qemu_opt_get(opts, "port");
6280a55679bSJeff Cody 
6290a55679bSJeff Cody             value = host;
6300a55679bSJeff Cody             if (port) {
6310a55679bSJeff Cody                 /* check for ipv6 */
6320a55679bSJeff Cody                 if (strchr(host, ':')) {
6330a55679bSJeff Cody                     strbuf = g_strdup_printf("[%s]:%s", host, port);
6340a55679bSJeff Cody                 } else {
6350a55679bSJeff Cody                     strbuf = g_strdup_printf("%s:%s", host, port);
6360a55679bSJeff Cody                 }
6370a55679bSJeff Cody                 value = strbuf;
6380a55679bSJeff Cody             } else if (strchr(host, ':')) {
6390a55679bSJeff Cody                 strbuf = g_strdup_printf("[%s]", host);
6400a55679bSJeff Cody                 value = strbuf;
6410a55679bSJeff Cody             }
6420a55679bSJeff Cody         } else {
6430a55679bSJeff Cody             value = qemu_opt_get(opts, "auth");
6440a55679bSJeff Cody         }
6450a55679bSJeff Cody 
6460a55679bSJeff Cody 
6470a55679bSJeff Cody         /* each iteration in the for loop will build upon the string, and if
6480a55679bSJeff Cody          * rados_str is NULL then it is our first pass */
6490a55679bSJeff Cody         if (rados_str) {
6500a55679bSJeff Cody             /* separate options with ';', as that  is what rados_conf_set()
6510a55679bSJeff Cody              * requires */
6520a55679bSJeff Cody             rados_str_tmp = rados_str;
6530a55679bSJeff Cody             rados_str = g_strdup_printf("%s;%s", rados_str_tmp, value);
6540a55679bSJeff Cody             g_free(rados_str_tmp);
6550a55679bSJeff Cody         } else {
6560a55679bSJeff Cody             rados_str = g_strdup(value);
6570a55679bSJeff Cody         }
6580a55679bSJeff Cody 
6590a55679bSJeff Cody         g_free(strbuf);
6600a55679bSJeff Cody         qemu_opts_del(opts);
6610a55679bSJeff Cody         opts = NULL;
6620a55679bSJeff Cody     }
6630a55679bSJeff Cody 
6640a55679bSJeff Cody exit:
6650a55679bSJeff Cody     qemu_opts_del(opts);
6660a55679bSJeff Cody     return rados_str;
6670a55679bSJeff Cody }
6680a55679bSJeff Cody 
669015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
670015a1036SMax Reitz                          Error **errp)
671f27aaf4bSChristian Brunner {
672f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
673c7cacb3eSJeff Cody     const char *pool, *snap, *conf, *clientname, *name, *keypairs;
67460390a21SDaniel P. Berrange     const char *secretid;
675a9ccedc3SKevin Wolf     QemuOpts *opts;
676a9ccedc3SKevin Wolf     Error *local_err = NULL;
6770a55679bSJeff Cody     char *mon_host = NULL;
6780a55679bSJeff Cody     char *auth_supported = NULL;
679f27aaf4bSChristian Brunner     int r;
680f27aaf4bSChristian Brunner 
68187ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
682a9ccedc3SKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
68384d18f06SMarkus Armbruster     if (local_err) {
684d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
685a9ccedc3SKevin Wolf         qemu_opts_del(opts);
686a9ccedc3SKevin Wolf         return -EINVAL;
687a9ccedc3SKevin Wolf     }
688a9ccedc3SKevin Wolf 
6890a55679bSJeff Cody     auth_supported = qemu_rbd_array_opts(options, "auth-supported.",
6900a55679bSJeff Cody                                          RBD_AUTH_SUPPORTED, &local_err);
6910a55679bSJeff Cody     if (local_err) {
6920a55679bSJeff Cody         error_propagate(errp, local_err);
6930a55679bSJeff Cody         r = -EINVAL;
6940a55679bSJeff Cody         goto failed_opts;
6950a55679bSJeff Cody     }
6960a55679bSJeff Cody 
6970a55679bSJeff Cody     mon_host = qemu_rbd_array_opts(options, "server.",
6980a55679bSJeff Cody                                    RBD_MON_HOST, &local_err);
6990a55679bSJeff Cody     if (local_err) {
7000a55679bSJeff Cody         error_propagate(errp, local_err);
7010a55679bSJeff Cody         r = -EINVAL;
7020a55679bSJeff Cody         goto failed_opts;
7030a55679bSJeff Cody     }
7040a55679bSJeff Cody 
70560390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
706a9ccedc3SKevin Wolf 
707c7cacb3eSJeff Cody     pool           = qemu_opt_get(opts, "pool");
708c7cacb3eSJeff Cody     conf           = qemu_opt_get(opts, "conf");
709c7cacb3eSJeff Cody     snap           = qemu_opt_get(opts, "snapshot");
710c7cacb3eSJeff Cody     clientname     = qemu_opt_get(opts, "user");
711c7cacb3eSJeff Cody     name           = qemu_opt_get(opts, "image");
712c7cacb3eSJeff Cody     keypairs       = qemu_opt_get(opts, "keyvalue-pairs");
713f27aaf4bSChristian Brunner 
714*f51c363cSMarkus Armbruster     if (!pool || !name) {
715*f51c363cSMarkus Armbruster         error_setg(errp, "Parameters 'pool' and 'image' are required");
716*f51c363cSMarkus Armbruster         r = -EINVAL;
717*f51c363cSMarkus Armbruster         goto failed_opts;
718*f51c363cSMarkus Armbruster     }
719*f51c363cSMarkus Armbruster 
7207c7e9df0SSage Weil     r = rados_create(&s->cluster, clientname);
721ad32e9c0SJosh Durgin     if (r < 0) {
72287cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
723c3ca988dSKevin Wolf         goto failed_opts;
724f27aaf4bSChristian Brunner     }
725f27aaf4bSChristian Brunner 
726c7cacb3eSJeff Cody     s->snap = g_strdup(snap);
727c7cacb3eSJeff Cody     pstrcpy(s->name, RBD_MAX_IMAGE_NAME_SIZE, name);
728eb93d5d9SSage Weil 
729c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
730c7cacb3eSJeff Cody     r = rados_conf_read_file(s->cluster, conf);
731c7cacb3eSJeff Cody     if (conf && r < 0) {
732c7cacb3eSJeff Cody         error_setg_errno(errp, -r, "error reading conf file %s", conf);
733e34d8f29SJosh Durgin         goto failed_shutdown;
734e34d8f29SJosh Durgin     }
73599a3c89dSJosh Durgin 
736c7cacb3eSJeff Cody     r = qemu_rbd_set_keypairs(s->cluster, keypairs, errp);
73799a3c89dSJosh Durgin     if (r < 0) {
73899a3c89dSJosh Durgin         goto failed_shutdown;
73999a3c89dSJosh Durgin     }
74099a3c89dSJosh Durgin 
7410a55679bSJeff Cody     if (mon_host) {
7420a55679bSJeff Cody         r = rados_conf_set(s->cluster, "mon_host", mon_host);
7430a55679bSJeff Cody         if (r < 0) {
7440a55679bSJeff Cody             goto failed_shutdown;
7450a55679bSJeff Cody         }
7460a55679bSJeff Cody     }
7470a55679bSJeff Cody 
7480a55679bSJeff Cody     if (auth_supported) {
7490a55679bSJeff Cody         r = rados_conf_set(s->cluster, "auth_supported", auth_supported);
7500a55679bSJeff Cody         if (r < 0) {
7510a55679bSJeff Cody             goto failed_shutdown;
7520a55679bSJeff Cody         }
7530a55679bSJeff Cody     }
7540a55679bSJeff Cody 
75560390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
75660390a21SDaniel P. Berrange         r = -EIO;
75760390a21SDaniel P. Berrange         goto failed_shutdown;
75860390a21SDaniel P. Berrange     }
75960390a21SDaniel P. Berrange 
760b11f38fcSJosh Durgin     /*
761b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
762b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
763b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
764b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
765b11f38fcSJosh Durgin      * be set up, fall back to no caching.
766b11f38fcSJosh Durgin      */
767b11f38fcSJosh Durgin     if (flags & BDRV_O_NOCACHE) {
768b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "false");
769b11f38fcSJosh Durgin     } else {
770b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "true");
771b11f38fcSJosh Durgin     }
772b11f38fcSJosh Durgin 
773ad32e9c0SJosh Durgin     r = rados_connect(s->cluster);
774ad32e9c0SJosh Durgin     if (r < 0) {
77587cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
776eb93d5d9SSage Weil         goto failed_shutdown;
777ad32e9c0SJosh Durgin     }
778ad32e9c0SJosh Durgin 
779ad32e9c0SJosh Durgin     r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
780ad32e9c0SJosh Durgin     if (r < 0) {
78187cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error opening pool %s", pool);
782eb93d5d9SSage Weil         goto failed_shutdown;
783ad32e9c0SJosh Durgin     }
784ad32e9c0SJosh Durgin 
785ad32e9c0SJosh Durgin     r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
786ad32e9c0SJosh Durgin     if (r < 0) {
78787cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error reading header from %s", s->name);
788eb93d5d9SSage Weil         goto failed_open;
789ad32e9c0SJosh Durgin     }
790ad32e9c0SJosh Durgin 
791ad32e9c0SJosh Durgin     bs->read_only = (s->snap != NULL);
792f27aaf4bSChristian Brunner 
793c3ca988dSKevin Wolf     qemu_opts_del(opts);
794f27aaf4bSChristian Brunner     return 0;
795f27aaf4bSChristian Brunner 
796eb93d5d9SSage Weil failed_open:
797ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
798eb93d5d9SSage Weil failed_shutdown:
799ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
800eb93d5d9SSage Weil     g_free(s->snap);
801c3ca988dSKevin Wolf failed_opts:
802c3ca988dSKevin Wolf     qemu_opts_del(opts);
8030a55679bSJeff Cody     g_free(mon_host);
8040a55679bSJeff Cody     g_free(auth_supported);
805f27aaf4bSChristian Brunner     return r;
806f27aaf4bSChristian Brunner }
807f27aaf4bSChristian Brunner 
808ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
809f27aaf4bSChristian Brunner {
810f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
811f27aaf4bSChristian Brunner 
812ad32e9c0SJosh Durgin     rbd_close(s->image);
813ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
8147267c094SAnthony Liguori     g_free(s->snap);
815ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
816f27aaf4bSChristian Brunner }
817f27aaf4bSChristian Brunner 
818d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
819f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
820f27aaf4bSChristian Brunner };
821f27aaf4bSChristian Brunner 
822e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
823f27aaf4bSChristian Brunner {
824e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
825e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
826ad32e9c0SJosh Durgin }
827ad32e9c0SJosh Durgin 
828ad32e9c0SJosh Durgin /*
829ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
830ad32e9c0SJosh Durgin  *
831ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
832ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
833e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
834e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
835ad32e9c0SJosh Durgin  */
836ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
837ad32e9c0SJosh Durgin {
838e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
839e04fb07fSStefan Hajnoczi 
840ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
841ad32e9c0SJosh Durgin     rbd_aio_release(c);
842f27aaf4bSChristian Brunner 
843fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
844ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
845473c7f02SStefan Priebe }
846f27aaf4bSChristian Brunner 
847787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
848787f3133SJosh Durgin                                    uint64_t off,
849787f3133SJosh Durgin                                    uint64_t len,
850787f3133SJosh Durgin                                    rbd_completion_t comp)
851787f3133SJosh Durgin {
852787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
853787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
854787f3133SJosh Durgin #else
855787f3133SJosh Durgin     return -ENOTSUP;
856787f3133SJosh Durgin #endif
857787f3133SJosh Durgin }
858787f3133SJosh Durgin 
859dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
860dc7588c1SJosh Durgin                                  rbd_completion_t comp)
861dc7588c1SJosh Durgin {
862dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
863dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
864dc7588c1SJosh Durgin #else
865dc7588c1SJosh Durgin     return -ENOTSUP;
866dc7588c1SJosh Durgin #endif
867dc7588c1SJosh Durgin }
868dc7588c1SJosh Durgin 
8697c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8707bbca9e2SEric Blake                                  int64_t off,
871f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8727bbca9e2SEric Blake                                  int64_t size,
873097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
874787f3133SJosh Durgin                                  void *opaque,
875787f3133SJosh Durgin                                  RBDAIOCmd cmd)
876f27aaf4bSChristian Brunner {
877f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8780f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
879ad32e9c0SJosh Durgin     rbd_completion_t c;
88051a13528SJosh Durgin     int r;
881f27aaf4bSChristian Brunner 
882f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
883f27aaf4bSChristian Brunner 
884d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
885787f3133SJosh Durgin     acb->cmd = cmd;
886f27aaf4bSChristian Brunner     acb->qiov = qiov;
8877bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8881d393bdeStianqing 
8891d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8901d393bdeStianqing 
8911d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
892dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
893787f3133SJosh Durgin             acb->bounce = NULL;
894787f3133SJosh Durgin         } else {
8950f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8960f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8970f7a0237SKevin Wolf                 goto failed;
8980f7a0237SKevin Wolf             }
899787f3133SJosh Durgin         }
9001d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
9011d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
9021d393bdeStianqing         }
9031d393bdeStianqing         rcb->buf = acb->bounce;
9041d393bdeStianqing     }
9051d393bdeStianqing 
906f27aaf4bSChristian Brunner     acb->ret = 0;
907f27aaf4bSChristian Brunner     acb->error = 0;
908f27aaf4bSChristian Brunner     acb->s = s;
909f27aaf4bSChristian Brunner 
910f27aaf4bSChristian Brunner     rcb->acb = acb;
911f27aaf4bSChristian Brunner     rcb->s = acb->s;
912ad32e9c0SJosh Durgin     rcb->size = size;
91351a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
91451a13528SJosh Durgin     if (r < 0) {
91551a13528SJosh Durgin         goto failed;
91651a13528SJosh Durgin     }
917f27aaf4bSChristian Brunner 
918787f3133SJosh Durgin     switch (cmd) {
919787f3133SJosh Durgin     case RBD_AIO_WRITE:
9201d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9211d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
9221d393bdeStianqing #else
9231d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
9241d393bdeStianqing #endif
925787f3133SJosh Durgin         break;
926787f3133SJosh Durgin     case RBD_AIO_READ:
9271d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9281d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
9291d393bdeStianqing #else
9301d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
9311d393bdeStianqing #endif
932787f3133SJosh Durgin         break;
933787f3133SJosh Durgin     case RBD_AIO_DISCARD:
934787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
935787f3133SJosh Durgin         break;
936dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
937dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
938dc7588c1SJosh Durgin         break;
939787f3133SJosh Durgin     default:
940787f3133SJosh Durgin         r = -EINVAL;
94151a13528SJosh Durgin     }
94251a13528SJosh Durgin 
94351a13528SJosh Durgin     if (r < 0) {
944405a2764SKevin Wolf         goto failed_completion;
945f27aaf4bSChristian Brunner     }
946f27aaf4bSChristian Brunner     return &acb->common;
94751a13528SJosh Durgin 
948405a2764SKevin Wolf failed_completion:
949405a2764SKevin Wolf     rbd_aio_release(c);
95051a13528SJosh Durgin failed:
9517267c094SAnthony Liguori     g_free(rcb);
9521d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
953405a2764SKevin Wolf         qemu_vfree(acb->bounce);
9541d393bdeStianqing     }
9551d393bdeStianqing 
9568007429aSFam Zheng     qemu_aio_unref(acb);
95751a13528SJosh Durgin     return NULL;
958f27aaf4bSChristian Brunner }
959f27aaf4bSChristian Brunner 
9607c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
961ad32e9c0SJosh Durgin                                       int64_t sector_num,
962ad32e9c0SJosh Durgin                                       QEMUIOVector *qiov,
963f27aaf4bSChristian Brunner                                       int nb_sectors,
964097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
965f27aaf4bSChristian Brunner                                       void *opaque)
966f27aaf4bSChristian Brunner {
9677bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
968e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
969787f3133SJosh Durgin                          RBD_AIO_READ);
970f27aaf4bSChristian Brunner }
971f27aaf4bSChristian Brunner 
9727c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
973ad32e9c0SJosh Durgin                                        int64_t sector_num,
974ad32e9c0SJosh Durgin                                        QEMUIOVector *qiov,
975f27aaf4bSChristian Brunner                                        int nb_sectors,
976097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
977f27aaf4bSChristian Brunner                                        void *opaque)
978f27aaf4bSChristian Brunner {
9797bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
980e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
981787f3133SJosh Durgin                          RBD_AIO_WRITE);
982f27aaf4bSChristian Brunner }
983f27aaf4bSChristian Brunner 
984dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9857c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
986097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
987dc7588c1SJosh Durgin                                       void *opaque)
988dc7588c1SJosh Durgin {
989dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
990dc7588c1SJosh Durgin }
991dc7588c1SJosh Durgin 
992dc7588c1SJosh Durgin #else
993dc7588c1SJosh Durgin 
9948b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9957a3f5fe9SSage Weil {
9967a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9977a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9987a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9997a3f5fe9SSage Weil     return rbd_flush(s->image);
10007a3f5fe9SSage Weil #else
10017a3f5fe9SSage Weil     return 0;
10027a3f5fe9SSage Weil #endif
10037a3f5fe9SSage Weil }
1004dc7588c1SJosh Durgin #endif
10057a3f5fe9SSage Weil 
1006ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1007f27aaf4bSChristian Brunner {
1008f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1009ad32e9c0SJosh Durgin     rbd_image_info_t info;
1010ad32e9c0SJosh Durgin     int r;
1011ad32e9c0SJosh Durgin 
1012ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1013ad32e9c0SJosh Durgin     if (r < 0) {
1014ad32e9c0SJosh Durgin         return r;
1015ad32e9c0SJosh Durgin     }
1016ad32e9c0SJosh Durgin 
1017ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
1018f27aaf4bSChristian Brunner     return 0;
1019f27aaf4bSChristian Brunner }
1020f27aaf4bSChristian Brunner 
1021ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1022f27aaf4bSChristian Brunner {
1023f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1024ad32e9c0SJosh Durgin     rbd_image_info_t info;
1025ad32e9c0SJosh Durgin     int r;
1026f27aaf4bSChristian Brunner 
1027ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1028ad32e9c0SJosh Durgin     if (r < 0) {
1029ad32e9c0SJosh Durgin         return r;
1030f27aaf4bSChristian Brunner     }
1031f27aaf4bSChristian Brunner 
1032ad32e9c0SJosh Durgin     return info.size;
1033ad32e9c0SJosh Durgin }
1034ad32e9c0SJosh Durgin 
103530cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
103630cdc48cSJosh Durgin {
103730cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
103830cdc48cSJosh Durgin     int r;
103930cdc48cSJosh Durgin 
104030cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
104130cdc48cSJosh Durgin     if (r < 0) {
104230cdc48cSJosh Durgin         return r;
104330cdc48cSJosh Durgin     }
104430cdc48cSJosh Durgin 
104530cdc48cSJosh Durgin     return 0;
104630cdc48cSJosh Durgin }
104730cdc48cSJosh Durgin 
1048ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1049ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1050f27aaf4bSChristian Brunner {
1051f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1052f27aaf4bSChristian Brunner     int r;
1053f27aaf4bSChristian Brunner 
1054f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1055f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1056f27aaf4bSChristian Brunner     }
1057f27aaf4bSChristian Brunner 
1058f27aaf4bSChristian Brunner     /*
1059f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1060f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1061f27aaf4bSChristian Brunner      */
1062f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1063f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1064f27aaf4bSChristian Brunner         return -EINVAL;
1065f27aaf4bSChristian Brunner     }
1066f27aaf4bSChristian Brunner 
1067f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1068f27aaf4bSChristian Brunner         return -ERANGE;
1069f27aaf4bSChristian Brunner     }
1070f27aaf4bSChristian Brunner 
1071ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1072f27aaf4bSChristian Brunner     if (r < 0) {
1073ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1074f27aaf4bSChristian Brunner         return r;
1075f27aaf4bSChristian Brunner     }
1076f27aaf4bSChristian Brunner 
1077f27aaf4bSChristian Brunner     return 0;
1078f27aaf4bSChristian Brunner }
1079f27aaf4bSChristian Brunner 
1080bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1081a89d89d3SWenchao Xia                                 const char *snapshot_id,
1082a89d89d3SWenchao Xia                                 const char *snapshot_name,
1083a89d89d3SWenchao Xia                                 Error **errp)
1084bd603247SGregory Farnum {
1085bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1086bd603247SGregory Farnum     int r;
1087bd603247SGregory Farnum 
1088a89d89d3SWenchao Xia     if (!snapshot_name) {
1089a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1090a89d89d3SWenchao Xia         return -EINVAL;
1091a89d89d3SWenchao Xia     }
1092a89d89d3SWenchao Xia 
1093a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1094a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1095a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1096a89d89d3SWenchao Xia         error_setg(errp,
1097a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1098a89d89d3SWenchao Xia                    "equal to snapshot name");
1099a89d89d3SWenchao Xia         return -EINVAL;
1100a89d89d3SWenchao Xia     }
1101a89d89d3SWenchao Xia 
1102bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1103a89d89d3SWenchao Xia     if (r < 0) {
1104a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1105a89d89d3SWenchao Xia     }
1106bd603247SGregory Farnum     return r;
1107bd603247SGregory Farnum }
1108bd603247SGregory Farnum 
1109bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1110bd603247SGregory Farnum                                   const char *snapshot_name)
1111bd603247SGregory Farnum {
1112bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1113bd603247SGregory Farnum 
11149be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1115bd603247SGregory Farnum }
1116bd603247SGregory Farnum 
1117ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1118ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1119f27aaf4bSChristian Brunner {
1120f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1121f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1122ad32e9c0SJosh Durgin     int i, snap_count;
1123ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1124ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1125f27aaf4bSChristian Brunner 
1126ad32e9c0SJosh Durgin     do {
112702c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1128ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
11299e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
11307267c094SAnthony Liguori             g_free(snaps);
1131f27aaf4bSChristian Brunner         }
1132ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1133f27aaf4bSChristian Brunner 
1134ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1135b9c53290SJosh Durgin         goto done;
1136f27aaf4bSChristian Brunner     }
1137f27aaf4bSChristian Brunner 
11385839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1139f27aaf4bSChristian Brunner 
1140ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1141ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1142f27aaf4bSChristian Brunner 
1143f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1144f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1145f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1146f27aaf4bSChristian Brunner 
1147ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1148f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1149f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1150f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1151f27aaf4bSChristian Brunner     }
1152ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
11539e6337d0SStefan Hajnoczi     g_free(snaps);
1154ad32e9c0SJosh Durgin 
1155b9c53290SJosh Durgin  done:
1156f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1157f27aaf4bSChristian Brunner     return snap_count;
1158f27aaf4bSChristian Brunner }
1159f27aaf4bSChristian Brunner 
1160787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11614da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
11624da444a0SEric Blake                                          int64_t offset,
11634da444a0SEric Blake                                          int count,
1164097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1165787f3133SJosh Durgin                                          void *opaque)
1166787f3133SJosh Durgin {
11674da444a0SEric Blake     return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
1168787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1169787f3133SJosh Durgin }
1170787f3133SJosh Durgin #endif
1171787f3133SJosh Durgin 
1172be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1173be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
1174be217884SAdam Crume                                       Error **errp)
1175be217884SAdam Crume {
1176be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1177be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1178be217884SAdam Crume     if (r < 0) {
1179be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1180be217884SAdam Crume     }
1181be217884SAdam Crume }
1182be217884SAdam Crume #endif
1183be217884SAdam Crume 
1184bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1185bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1186bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1187bd0cf596SChunyan Liu     .desc = {
1188f27aaf4bSChristian Brunner         {
1189f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1190bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1191f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1192f27aaf4bSChristian Brunner         },
1193f27aaf4bSChristian Brunner         {
1194f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1195bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1196f27aaf4bSChristian Brunner             .help = "RBD object size"
1197f27aaf4bSChristian Brunner         },
119860390a21SDaniel P. Berrange         {
119960390a21SDaniel P. Berrange             .name = "password-secret",
120060390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
120160390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
120260390a21SDaniel P. Berrange         },
1203bd0cf596SChunyan Liu         { /* end of list */ }
1204bd0cf596SChunyan Liu     }
1205f27aaf4bSChristian Brunner };
1206f27aaf4bSChristian Brunner 
1207f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1208f27aaf4bSChristian Brunner     .format_name            = "rbd",
1209f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1210c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1211ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1212ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
1213c282e1fdSChunyan Liu     .bdrv_create            = qemu_rbd_create,
12143ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1215ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1216bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1217ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
121830cdc48cSJosh Durgin     .bdrv_truncate          = qemu_rbd_truncate,
1219f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1220f27aaf4bSChristian Brunner 
1221ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
1222ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
1223dc7588c1SJosh Durgin 
1224dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1225dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1226dc7588c1SJosh Durgin #else
1227c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1228dc7588c1SJosh Durgin #endif
1229f27aaf4bSChristian Brunner 
1230787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
12314da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1232787f3133SJosh Durgin #endif
1233787f3133SJosh Durgin 
1234ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1235bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1236ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1237bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1238be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1239be217884SAdam Crume     .bdrv_invalidate_cache  = qemu_rbd_invalidate_cache,
1240be217884SAdam Crume #endif
1241f27aaf4bSChristian Brunner };
1242f27aaf4bSChristian Brunner 
1243f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1244f27aaf4bSChristian Brunner {
1245f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1246f27aaf4bSChristian Brunner }
1247f27aaf4bSChristian Brunner 
1248f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1249