xref: /openbmc/qemu/block/rbd.c (revision 0a55679b4a5061f4d74bdb1a0e81611ba3390b00)
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         },
408*0a55679bSJeff Cody         {
409*0a55679bSJeff Cody             .name = "host",
410*0a55679bSJeff Cody             .type = QEMU_OPT_STRING,
411*0a55679bSJeff Cody         },
412*0a55679bSJeff Cody         {
413*0a55679bSJeff Cody             .name = "port",
414*0a55679bSJeff Cody             .type = QEMU_OPT_STRING,
415*0a55679bSJeff Cody         },
416*0a55679bSJeff Cody         {
417*0a55679bSJeff Cody             .name = "auth",
418*0a55679bSJeff Cody             .type = QEMU_OPT_STRING,
419*0a55679bSJeff Cody             .help = "Supported authentication method, either cephx or none",
420*0a55679bSJeff 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 
581*0a55679bSJeff Cody #define RBD_MON_HOST          0
582*0a55679bSJeff Cody #define RBD_AUTH_SUPPORTED    1
583*0a55679bSJeff Cody 
584*0a55679bSJeff Cody static char *qemu_rbd_array_opts(QDict *options, const char *prefix, int type,
585*0a55679bSJeff Cody                                  Error **errp)
586*0a55679bSJeff Cody {
587*0a55679bSJeff Cody     int num_entries;
588*0a55679bSJeff Cody     QemuOpts *opts = NULL;
589*0a55679bSJeff Cody     QDict *sub_options;
590*0a55679bSJeff Cody     const char *host;
591*0a55679bSJeff Cody     const char *port;
592*0a55679bSJeff Cody     char *str;
593*0a55679bSJeff Cody     char *rados_str = NULL;
594*0a55679bSJeff Cody     Error *local_err = NULL;
595*0a55679bSJeff Cody     int i;
596*0a55679bSJeff Cody 
597*0a55679bSJeff Cody     assert(type == RBD_MON_HOST || type == RBD_AUTH_SUPPORTED);
598*0a55679bSJeff Cody 
599*0a55679bSJeff Cody     num_entries = qdict_array_entries(options, prefix);
600*0a55679bSJeff Cody 
601*0a55679bSJeff Cody     if (num_entries < 0) {
602*0a55679bSJeff Cody         error_setg(errp, "Parse error on RBD QDict array");
603*0a55679bSJeff Cody         return NULL;
604*0a55679bSJeff Cody     }
605*0a55679bSJeff Cody 
606*0a55679bSJeff Cody     for (i = 0; i < num_entries; i++) {
607*0a55679bSJeff Cody         char *strbuf = NULL;
608*0a55679bSJeff Cody         const char *value;
609*0a55679bSJeff Cody         char *rados_str_tmp;
610*0a55679bSJeff Cody 
611*0a55679bSJeff Cody         str = g_strdup_printf("%s%d.", prefix, i);
612*0a55679bSJeff Cody         qdict_extract_subqdict(options, &sub_options, str);
613*0a55679bSJeff Cody         g_free(str);
614*0a55679bSJeff Cody 
615*0a55679bSJeff Cody         opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
616*0a55679bSJeff Cody         qemu_opts_absorb_qdict(opts, sub_options, &local_err);
617*0a55679bSJeff Cody         QDECREF(sub_options);
618*0a55679bSJeff Cody         if (local_err) {
619*0a55679bSJeff Cody             error_propagate(errp, local_err);
620*0a55679bSJeff Cody             g_free(rados_str);
621*0a55679bSJeff Cody             rados_str = NULL;
622*0a55679bSJeff Cody             goto exit;
623*0a55679bSJeff Cody         }
624*0a55679bSJeff Cody 
625*0a55679bSJeff Cody         if (type == RBD_MON_HOST) {
626*0a55679bSJeff Cody             host = qemu_opt_get(opts, "host");
627*0a55679bSJeff Cody             port = qemu_opt_get(opts, "port");
628*0a55679bSJeff Cody 
629*0a55679bSJeff Cody             value = host;
630*0a55679bSJeff Cody             if (port) {
631*0a55679bSJeff Cody                 /* check for ipv6 */
632*0a55679bSJeff Cody                 if (strchr(host, ':')) {
633*0a55679bSJeff Cody                     strbuf = g_strdup_printf("[%s]:%s", host, port);
634*0a55679bSJeff Cody                 } else {
635*0a55679bSJeff Cody                     strbuf = g_strdup_printf("%s:%s", host, port);
636*0a55679bSJeff Cody                 }
637*0a55679bSJeff Cody                 value = strbuf;
638*0a55679bSJeff Cody             } else if (strchr(host, ':')) {
639*0a55679bSJeff Cody                 strbuf = g_strdup_printf("[%s]", host);
640*0a55679bSJeff Cody                 value = strbuf;
641*0a55679bSJeff Cody             }
642*0a55679bSJeff Cody         } else {
643*0a55679bSJeff Cody             value = qemu_opt_get(opts, "auth");
644*0a55679bSJeff Cody         }
645*0a55679bSJeff Cody 
646*0a55679bSJeff Cody 
647*0a55679bSJeff Cody         /* each iteration in the for loop will build upon the string, and if
648*0a55679bSJeff Cody          * rados_str is NULL then it is our first pass */
649*0a55679bSJeff Cody         if (rados_str) {
650*0a55679bSJeff Cody             /* separate options with ';', as that  is what rados_conf_set()
651*0a55679bSJeff Cody              * requires */
652*0a55679bSJeff Cody             rados_str_tmp = rados_str;
653*0a55679bSJeff Cody             rados_str = g_strdup_printf("%s;%s", rados_str_tmp, value);
654*0a55679bSJeff Cody             g_free(rados_str_tmp);
655*0a55679bSJeff Cody         } else {
656*0a55679bSJeff Cody             rados_str = g_strdup(value);
657*0a55679bSJeff Cody         }
658*0a55679bSJeff Cody 
659*0a55679bSJeff Cody         g_free(strbuf);
660*0a55679bSJeff Cody         qemu_opts_del(opts);
661*0a55679bSJeff Cody         opts = NULL;
662*0a55679bSJeff Cody     }
663*0a55679bSJeff Cody 
664*0a55679bSJeff Cody exit:
665*0a55679bSJeff Cody     qemu_opts_del(opts);
666*0a55679bSJeff Cody     return rados_str;
667*0a55679bSJeff Cody }
668*0a55679bSJeff 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;
677*0a55679bSJeff Cody     char *mon_host = NULL;
678*0a55679bSJeff 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 
689*0a55679bSJeff Cody     auth_supported = qemu_rbd_array_opts(options, "auth-supported.",
690*0a55679bSJeff Cody                                          RBD_AUTH_SUPPORTED, &local_err);
691*0a55679bSJeff Cody     if (local_err) {
692*0a55679bSJeff Cody         error_propagate(errp, local_err);
693*0a55679bSJeff Cody         r = -EINVAL;
694*0a55679bSJeff Cody         goto failed_opts;
695*0a55679bSJeff Cody     }
696*0a55679bSJeff Cody 
697*0a55679bSJeff Cody     mon_host = qemu_rbd_array_opts(options, "server.",
698*0a55679bSJeff Cody                                    RBD_MON_HOST, &local_err);
699*0a55679bSJeff Cody     if (local_err) {
700*0a55679bSJeff Cody         error_propagate(errp, local_err);
701*0a55679bSJeff Cody         r = -EINVAL;
702*0a55679bSJeff Cody         goto failed_opts;
703*0a55679bSJeff Cody     }
704*0a55679bSJeff 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 
7147c7e9df0SSage Weil     r = rados_create(&s->cluster, clientname);
715ad32e9c0SJosh Durgin     if (r < 0) {
71687cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
717c3ca988dSKevin Wolf         goto failed_opts;
718f27aaf4bSChristian Brunner     }
719f27aaf4bSChristian Brunner 
720c7cacb3eSJeff Cody     s->snap = g_strdup(snap);
721c7cacb3eSJeff Cody     if (name) {
722c7cacb3eSJeff Cody         pstrcpy(s->name, RBD_MAX_IMAGE_NAME_SIZE, name);
723eb93d5d9SSage Weil     }
724eb93d5d9SSage Weil 
725c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
726c7cacb3eSJeff Cody     r = rados_conf_read_file(s->cluster, conf);
727c7cacb3eSJeff Cody     if (conf && r < 0) {
728c7cacb3eSJeff Cody         error_setg_errno(errp, -r, "error reading conf file %s", conf);
729e34d8f29SJosh Durgin         goto failed_shutdown;
730e34d8f29SJosh Durgin     }
73199a3c89dSJosh Durgin 
732c7cacb3eSJeff Cody     r = qemu_rbd_set_keypairs(s->cluster, keypairs, errp);
73399a3c89dSJosh Durgin     if (r < 0) {
73499a3c89dSJosh Durgin         goto failed_shutdown;
73599a3c89dSJosh Durgin     }
73699a3c89dSJosh Durgin 
737*0a55679bSJeff Cody     if (mon_host) {
738*0a55679bSJeff Cody         r = rados_conf_set(s->cluster, "mon_host", mon_host);
739*0a55679bSJeff Cody         if (r < 0) {
740*0a55679bSJeff Cody             goto failed_shutdown;
741*0a55679bSJeff Cody         }
742*0a55679bSJeff Cody     }
743*0a55679bSJeff Cody 
744*0a55679bSJeff Cody     if (auth_supported) {
745*0a55679bSJeff Cody         r = rados_conf_set(s->cluster, "auth_supported", auth_supported);
746*0a55679bSJeff Cody         if (r < 0) {
747*0a55679bSJeff Cody             goto failed_shutdown;
748*0a55679bSJeff Cody         }
749*0a55679bSJeff Cody     }
750*0a55679bSJeff Cody 
75160390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
75260390a21SDaniel P. Berrange         r = -EIO;
75360390a21SDaniel P. Berrange         goto failed_shutdown;
75460390a21SDaniel P. Berrange     }
75560390a21SDaniel P. Berrange 
756b11f38fcSJosh Durgin     /*
757b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
758b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
759b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
760b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
761b11f38fcSJosh Durgin      * be set up, fall back to no caching.
762b11f38fcSJosh Durgin      */
763b11f38fcSJosh Durgin     if (flags & BDRV_O_NOCACHE) {
764b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "false");
765b11f38fcSJosh Durgin     } else {
766b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "true");
767b11f38fcSJosh Durgin     }
768b11f38fcSJosh Durgin 
769ad32e9c0SJosh Durgin     r = rados_connect(s->cluster);
770ad32e9c0SJosh Durgin     if (r < 0) {
77187cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
772eb93d5d9SSage Weil         goto failed_shutdown;
773ad32e9c0SJosh Durgin     }
774ad32e9c0SJosh Durgin 
775ad32e9c0SJosh Durgin     r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
776ad32e9c0SJosh Durgin     if (r < 0) {
77787cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error opening pool %s", pool);
778eb93d5d9SSage Weil         goto failed_shutdown;
779ad32e9c0SJosh Durgin     }
780ad32e9c0SJosh Durgin 
781ad32e9c0SJosh Durgin     r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
782ad32e9c0SJosh Durgin     if (r < 0) {
78387cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error reading header from %s", s->name);
784eb93d5d9SSage Weil         goto failed_open;
785ad32e9c0SJosh Durgin     }
786ad32e9c0SJosh Durgin 
787ad32e9c0SJosh Durgin     bs->read_only = (s->snap != NULL);
788f27aaf4bSChristian Brunner 
789c3ca988dSKevin Wolf     qemu_opts_del(opts);
790f27aaf4bSChristian Brunner     return 0;
791f27aaf4bSChristian Brunner 
792eb93d5d9SSage Weil failed_open:
793ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
794eb93d5d9SSage Weil failed_shutdown:
795ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
796eb93d5d9SSage Weil     g_free(s->snap);
797c3ca988dSKevin Wolf failed_opts:
798c3ca988dSKevin Wolf     qemu_opts_del(opts);
799*0a55679bSJeff Cody     g_free(mon_host);
800*0a55679bSJeff Cody     g_free(auth_supported);
801f27aaf4bSChristian Brunner     return r;
802f27aaf4bSChristian Brunner }
803f27aaf4bSChristian Brunner 
804ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
805f27aaf4bSChristian Brunner {
806f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
807f27aaf4bSChristian Brunner 
808ad32e9c0SJosh Durgin     rbd_close(s->image);
809ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
8107267c094SAnthony Liguori     g_free(s->snap);
811ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
812f27aaf4bSChristian Brunner }
813f27aaf4bSChristian Brunner 
814d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
815f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
816f27aaf4bSChristian Brunner };
817f27aaf4bSChristian Brunner 
818e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
819f27aaf4bSChristian Brunner {
820e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
821e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
822ad32e9c0SJosh Durgin }
823ad32e9c0SJosh Durgin 
824ad32e9c0SJosh Durgin /*
825ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
826ad32e9c0SJosh Durgin  *
827ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
828ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
829e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
830e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
831ad32e9c0SJosh Durgin  */
832ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
833ad32e9c0SJosh Durgin {
834e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
835e04fb07fSStefan Hajnoczi 
836ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
837ad32e9c0SJosh Durgin     rbd_aio_release(c);
838f27aaf4bSChristian Brunner 
839fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
840ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
841473c7f02SStefan Priebe }
842f27aaf4bSChristian Brunner 
843787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
844787f3133SJosh Durgin                                    uint64_t off,
845787f3133SJosh Durgin                                    uint64_t len,
846787f3133SJosh Durgin                                    rbd_completion_t comp)
847787f3133SJosh Durgin {
848787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
849787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
850787f3133SJosh Durgin #else
851787f3133SJosh Durgin     return -ENOTSUP;
852787f3133SJosh Durgin #endif
853787f3133SJosh Durgin }
854787f3133SJosh Durgin 
855dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
856dc7588c1SJosh Durgin                                  rbd_completion_t comp)
857dc7588c1SJosh Durgin {
858dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
859dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
860dc7588c1SJosh Durgin #else
861dc7588c1SJosh Durgin     return -ENOTSUP;
862dc7588c1SJosh Durgin #endif
863dc7588c1SJosh Durgin }
864dc7588c1SJosh Durgin 
8657c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8667bbca9e2SEric Blake                                  int64_t off,
867f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8687bbca9e2SEric Blake                                  int64_t size,
869097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
870787f3133SJosh Durgin                                  void *opaque,
871787f3133SJosh Durgin                                  RBDAIOCmd cmd)
872f27aaf4bSChristian Brunner {
873f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8740f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
875ad32e9c0SJosh Durgin     rbd_completion_t c;
87651a13528SJosh Durgin     int r;
877f27aaf4bSChristian Brunner 
878f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
879f27aaf4bSChristian Brunner 
880d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
881787f3133SJosh Durgin     acb->cmd = cmd;
882f27aaf4bSChristian Brunner     acb->qiov = qiov;
8837bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8841d393bdeStianqing 
8851d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8861d393bdeStianqing 
8871d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
888dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
889787f3133SJosh Durgin             acb->bounce = NULL;
890787f3133SJosh Durgin         } else {
8910f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8920f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8930f7a0237SKevin Wolf                 goto failed;
8940f7a0237SKevin Wolf             }
895787f3133SJosh Durgin         }
8961d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
8971d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
8981d393bdeStianqing         }
8991d393bdeStianqing         rcb->buf = acb->bounce;
9001d393bdeStianqing     }
9011d393bdeStianqing 
902f27aaf4bSChristian Brunner     acb->ret = 0;
903f27aaf4bSChristian Brunner     acb->error = 0;
904f27aaf4bSChristian Brunner     acb->s = s;
905f27aaf4bSChristian Brunner 
906f27aaf4bSChristian Brunner     rcb->acb = acb;
907f27aaf4bSChristian Brunner     rcb->s = acb->s;
908ad32e9c0SJosh Durgin     rcb->size = size;
90951a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
91051a13528SJosh Durgin     if (r < 0) {
91151a13528SJosh Durgin         goto failed;
91251a13528SJosh Durgin     }
913f27aaf4bSChristian Brunner 
914787f3133SJosh Durgin     switch (cmd) {
915787f3133SJosh Durgin     case RBD_AIO_WRITE:
9161d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9171d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
9181d393bdeStianqing #else
9191d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
9201d393bdeStianqing #endif
921787f3133SJosh Durgin         break;
922787f3133SJosh Durgin     case RBD_AIO_READ:
9231d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9241d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
9251d393bdeStianqing #else
9261d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
9271d393bdeStianqing #endif
928787f3133SJosh Durgin         break;
929787f3133SJosh Durgin     case RBD_AIO_DISCARD:
930787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
931787f3133SJosh Durgin         break;
932dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
933dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
934dc7588c1SJosh Durgin         break;
935787f3133SJosh Durgin     default:
936787f3133SJosh Durgin         r = -EINVAL;
93751a13528SJosh Durgin     }
93851a13528SJosh Durgin 
93951a13528SJosh Durgin     if (r < 0) {
940405a2764SKevin Wolf         goto failed_completion;
941f27aaf4bSChristian Brunner     }
942f27aaf4bSChristian Brunner     return &acb->common;
94351a13528SJosh Durgin 
944405a2764SKevin Wolf failed_completion:
945405a2764SKevin Wolf     rbd_aio_release(c);
94651a13528SJosh Durgin failed:
9477267c094SAnthony Liguori     g_free(rcb);
9481d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
949405a2764SKevin Wolf         qemu_vfree(acb->bounce);
9501d393bdeStianqing     }
9511d393bdeStianqing 
9528007429aSFam Zheng     qemu_aio_unref(acb);
95351a13528SJosh Durgin     return NULL;
954f27aaf4bSChristian Brunner }
955f27aaf4bSChristian Brunner 
9567c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
957ad32e9c0SJosh Durgin                                       int64_t sector_num,
958ad32e9c0SJosh Durgin                                       QEMUIOVector *qiov,
959f27aaf4bSChristian Brunner                                       int nb_sectors,
960097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
961f27aaf4bSChristian Brunner                                       void *opaque)
962f27aaf4bSChristian Brunner {
9637bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
964e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
965787f3133SJosh Durgin                          RBD_AIO_READ);
966f27aaf4bSChristian Brunner }
967f27aaf4bSChristian Brunner 
9687c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
969ad32e9c0SJosh Durgin                                        int64_t sector_num,
970ad32e9c0SJosh Durgin                                        QEMUIOVector *qiov,
971f27aaf4bSChristian Brunner                                        int nb_sectors,
972097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
973f27aaf4bSChristian Brunner                                        void *opaque)
974f27aaf4bSChristian Brunner {
9757bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
976e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
977787f3133SJosh Durgin                          RBD_AIO_WRITE);
978f27aaf4bSChristian Brunner }
979f27aaf4bSChristian Brunner 
980dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9817c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
982097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
983dc7588c1SJosh Durgin                                       void *opaque)
984dc7588c1SJosh Durgin {
985dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
986dc7588c1SJosh Durgin }
987dc7588c1SJosh Durgin 
988dc7588c1SJosh Durgin #else
989dc7588c1SJosh Durgin 
9908b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9917a3f5fe9SSage Weil {
9927a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9937a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9947a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9957a3f5fe9SSage Weil     return rbd_flush(s->image);
9967a3f5fe9SSage Weil #else
9977a3f5fe9SSage Weil     return 0;
9987a3f5fe9SSage Weil #endif
9997a3f5fe9SSage Weil }
1000dc7588c1SJosh Durgin #endif
10017a3f5fe9SSage Weil 
1002ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1003f27aaf4bSChristian Brunner {
1004f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1005ad32e9c0SJosh Durgin     rbd_image_info_t info;
1006ad32e9c0SJosh Durgin     int r;
1007ad32e9c0SJosh Durgin 
1008ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1009ad32e9c0SJosh Durgin     if (r < 0) {
1010ad32e9c0SJosh Durgin         return r;
1011ad32e9c0SJosh Durgin     }
1012ad32e9c0SJosh Durgin 
1013ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
1014f27aaf4bSChristian Brunner     return 0;
1015f27aaf4bSChristian Brunner }
1016f27aaf4bSChristian Brunner 
1017ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1018f27aaf4bSChristian Brunner {
1019f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1020ad32e9c0SJosh Durgin     rbd_image_info_t info;
1021ad32e9c0SJosh Durgin     int r;
1022f27aaf4bSChristian Brunner 
1023ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1024ad32e9c0SJosh Durgin     if (r < 0) {
1025ad32e9c0SJosh Durgin         return r;
1026f27aaf4bSChristian Brunner     }
1027f27aaf4bSChristian Brunner 
1028ad32e9c0SJosh Durgin     return info.size;
1029ad32e9c0SJosh Durgin }
1030ad32e9c0SJosh Durgin 
103130cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
103230cdc48cSJosh Durgin {
103330cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
103430cdc48cSJosh Durgin     int r;
103530cdc48cSJosh Durgin 
103630cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
103730cdc48cSJosh Durgin     if (r < 0) {
103830cdc48cSJosh Durgin         return r;
103930cdc48cSJosh Durgin     }
104030cdc48cSJosh Durgin 
104130cdc48cSJosh Durgin     return 0;
104230cdc48cSJosh Durgin }
104330cdc48cSJosh Durgin 
1044ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1045ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1046f27aaf4bSChristian Brunner {
1047f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1048f27aaf4bSChristian Brunner     int r;
1049f27aaf4bSChristian Brunner 
1050f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1051f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1052f27aaf4bSChristian Brunner     }
1053f27aaf4bSChristian Brunner 
1054f27aaf4bSChristian Brunner     /*
1055f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1056f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1057f27aaf4bSChristian Brunner      */
1058f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1059f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1060f27aaf4bSChristian Brunner         return -EINVAL;
1061f27aaf4bSChristian Brunner     }
1062f27aaf4bSChristian Brunner 
1063f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1064f27aaf4bSChristian Brunner         return -ERANGE;
1065f27aaf4bSChristian Brunner     }
1066f27aaf4bSChristian Brunner 
1067ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1068f27aaf4bSChristian Brunner     if (r < 0) {
1069ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1070f27aaf4bSChristian Brunner         return r;
1071f27aaf4bSChristian Brunner     }
1072f27aaf4bSChristian Brunner 
1073f27aaf4bSChristian Brunner     return 0;
1074f27aaf4bSChristian Brunner }
1075f27aaf4bSChristian Brunner 
1076bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1077a89d89d3SWenchao Xia                                 const char *snapshot_id,
1078a89d89d3SWenchao Xia                                 const char *snapshot_name,
1079a89d89d3SWenchao Xia                                 Error **errp)
1080bd603247SGregory Farnum {
1081bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1082bd603247SGregory Farnum     int r;
1083bd603247SGregory Farnum 
1084a89d89d3SWenchao Xia     if (!snapshot_name) {
1085a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1086a89d89d3SWenchao Xia         return -EINVAL;
1087a89d89d3SWenchao Xia     }
1088a89d89d3SWenchao Xia 
1089a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1090a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1091a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1092a89d89d3SWenchao Xia         error_setg(errp,
1093a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1094a89d89d3SWenchao Xia                    "equal to snapshot name");
1095a89d89d3SWenchao Xia         return -EINVAL;
1096a89d89d3SWenchao Xia     }
1097a89d89d3SWenchao Xia 
1098bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1099a89d89d3SWenchao Xia     if (r < 0) {
1100a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1101a89d89d3SWenchao Xia     }
1102bd603247SGregory Farnum     return r;
1103bd603247SGregory Farnum }
1104bd603247SGregory Farnum 
1105bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1106bd603247SGregory Farnum                                   const char *snapshot_name)
1107bd603247SGregory Farnum {
1108bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1109bd603247SGregory Farnum 
11109be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1111bd603247SGregory Farnum }
1112bd603247SGregory Farnum 
1113ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1114ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1115f27aaf4bSChristian Brunner {
1116f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1117f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1118ad32e9c0SJosh Durgin     int i, snap_count;
1119ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1120ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1121f27aaf4bSChristian Brunner 
1122ad32e9c0SJosh Durgin     do {
112302c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1124ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
11259e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
11267267c094SAnthony Liguori             g_free(snaps);
1127f27aaf4bSChristian Brunner         }
1128ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1129f27aaf4bSChristian Brunner 
1130ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1131b9c53290SJosh Durgin         goto done;
1132f27aaf4bSChristian Brunner     }
1133f27aaf4bSChristian Brunner 
11345839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1135f27aaf4bSChristian Brunner 
1136ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1137ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1138f27aaf4bSChristian Brunner 
1139f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1140f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1141f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1142f27aaf4bSChristian Brunner 
1143ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1144f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1145f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1146f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1147f27aaf4bSChristian Brunner     }
1148ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
11499e6337d0SStefan Hajnoczi     g_free(snaps);
1150ad32e9c0SJosh Durgin 
1151b9c53290SJosh Durgin  done:
1152f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1153f27aaf4bSChristian Brunner     return snap_count;
1154f27aaf4bSChristian Brunner }
1155f27aaf4bSChristian Brunner 
1156787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11574da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
11584da444a0SEric Blake                                          int64_t offset,
11594da444a0SEric Blake                                          int count,
1160097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1161787f3133SJosh Durgin                                          void *opaque)
1162787f3133SJosh Durgin {
11634da444a0SEric Blake     return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
1164787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1165787f3133SJosh Durgin }
1166787f3133SJosh Durgin #endif
1167787f3133SJosh Durgin 
1168be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1169be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
1170be217884SAdam Crume                                       Error **errp)
1171be217884SAdam Crume {
1172be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1173be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1174be217884SAdam Crume     if (r < 0) {
1175be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1176be217884SAdam Crume     }
1177be217884SAdam Crume }
1178be217884SAdam Crume #endif
1179be217884SAdam Crume 
1180bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1181bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1182bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1183bd0cf596SChunyan Liu     .desc = {
1184f27aaf4bSChristian Brunner         {
1185f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1186bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1187f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1188f27aaf4bSChristian Brunner         },
1189f27aaf4bSChristian Brunner         {
1190f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1191bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1192f27aaf4bSChristian Brunner             .help = "RBD object size"
1193f27aaf4bSChristian Brunner         },
119460390a21SDaniel P. Berrange         {
119560390a21SDaniel P. Berrange             .name = "password-secret",
119660390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
119760390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
119860390a21SDaniel P. Berrange         },
1199bd0cf596SChunyan Liu         { /* end of list */ }
1200bd0cf596SChunyan Liu     }
1201f27aaf4bSChristian Brunner };
1202f27aaf4bSChristian Brunner 
1203f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1204f27aaf4bSChristian Brunner     .format_name            = "rbd",
1205f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1206c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1207ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1208ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
1209c282e1fdSChunyan Liu     .bdrv_create            = qemu_rbd_create,
12103ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1211ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1212bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1213ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
121430cdc48cSJosh Durgin     .bdrv_truncate          = qemu_rbd_truncate,
1215f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1216f27aaf4bSChristian Brunner 
1217ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
1218ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
1219dc7588c1SJosh Durgin 
1220dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1221dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1222dc7588c1SJosh Durgin #else
1223c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1224dc7588c1SJosh Durgin #endif
1225f27aaf4bSChristian Brunner 
1226787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
12274da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1228787f3133SJosh Durgin #endif
1229787f3133SJosh Durgin 
1230ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1231bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1232ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1233bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1234be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1235be217884SAdam Crume     .bdrv_invalidate_cache  = qemu_rbd_invalidate_cache,
1236be217884SAdam Crume #endif
1237f27aaf4bSChristian Brunner };
1238f27aaf4bSChristian Brunner 
1239f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1240f27aaf4bSChristian Brunner {
1241f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1242f27aaf4bSChristian Brunner }
1243f27aaf4bSChristian Brunner 
1244f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1245