xref: /openbmc/qemu/block/rbd.c (revision 1bebea37b43223a45c39dcbc158f3cec6164fdbf)
1f27aaf4bSChristian Brunner /*
2f27aaf4bSChristian Brunner  * QEMU Block driver for RADOS (Ceph)
3f27aaf4bSChristian Brunner  *
4ad32e9c0SJosh Durgin  * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5ad32e9c0SJosh Durgin  *                         Josh Durgin <josh.durgin@dreamhost.com>
6f27aaf4bSChristian Brunner  *
7f27aaf4bSChristian Brunner  * This work is licensed under the terms of the GNU GPL, version 2.  See
8f27aaf4bSChristian Brunner  * the COPYING file in the top-level directory.
9f27aaf4bSChristian Brunner  *
106b620ca3SPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
116b620ca3SPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
12f27aaf4bSChristian Brunner  */
13f27aaf4bSChristian Brunner 
1480c71a24SPeter Maydell #include "qemu/osdep.h"
15ad32e9c0SJosh Durgin 
162836284dSMarkus Armbruster #include <rbd/librbd.h>
17da34e65cSMarkus Armbruster #include "qapi/error.h"
181de7afc9SPaolo Bonzini #include "qemu/error-report.h"
19922a01a0SMarkus Armbruster #include "qemu/option.h"
20737e150eSPaolo Bonzini #include "block/block_int.h"
2160390a21SDaniel P. Berrange #include "crypto/secret.h"
22f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
23c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
24452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
25e98c6961SEric Blake #include "qapi/qmp/qjson.h"
2647e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
274bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h"
284bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h"
29f27aaf4bSChristian Brunner 
30f27aaf4bSChristian Brunner /*
31f27aaf4bSChristian Brunner  * When specifying the image filename use:
32f27aaf4bSChristian Brunner  *
33fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
34f27aaf4bSChristian Brunner  *
359e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
36f27aaf4bSChristian Brunner  *
379e1fbcdeSSage Weil  * devicename is the name of the rbd image.
38f27aaf4bSChristian Brunner  *
399e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
409e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
41fab5cf59SJosh Durgin  *
429e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
439e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
449e1fbcdeSSage Weil  * (normally 'admin').
45f27aaf4bSChristian Brunner  *
469e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
479e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
489e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
499e1fbcdeSSage Weil  * file, specify conf=/dev/null.
50f27aaf4bSChristian Brunner  *
519e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
529e1fbcdeSSage Weil  * leading "\".
53f27aaf4bSChristian Brunner  */
54f27aaf4bSChristian Brunner 
55787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
56787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
57787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
58787f3133SJosh Durgin #else
59787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
60787f3133SJosh Durgin #endif
61787f3133SJosh Durgin 
62f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
63f27aaf4bSChristian Brunner 
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;
10280b61a27SJeff Cody     char *image_name;
103ad32e9c0SJosh Durgin     char *snap;
104f27aaf4bSChristian Brunner } BDRVRBDState;
105f27aaf4bSChristian Brunner 
106730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
107f27aaf4bSChristian Brunner {
108f27aaf4bSChristian Brunner     char *end;
109f27aaf4bSChristian Brunner 
110f27aaf4bSChristian Brunner     *p = NULL;
111f27aaf4bSChristian Brunner 
11216a06b24SSage Weil     for (end = src; *end; ++end) {
11316a06b24SSage Weil         if (*end == delim) {
11416a06b24SSage Weil             break;
11516a06b24SSage Weil         }
11616a06b24SSage Weil         if (*end == '\\' && end[1] != '\0') {
11716a06b24SSage Weil             end++;
11816a06b24SSage Weil         }
11916a06b24SSage Weil     }
12016a06b24SSage Weil     if (*end == delim) {
121f27aaf4bSChristian Brunner         *p = end + 1;
122f27aaf4bSChristian Brunner         *end = '\0';
123f27aaf4bSChristian Brunner     }
1247830f909SJeff Cody     return src;
125f27aaf4bSChristian Brunner }
126f27aaf4bSChristian Brunner 
12716a06b24SSage Weil static void qemu_rbd_unescape(char *src)
12816a06b24SSage Weil {
12916a06b24SSage Weil     char *p;
13016a06b24SSage Weil 
13116a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
13216a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
13316a06b24SSage Weil             src++;
13416a06b24SSage Weil         }
13516a06b24SSage Weil         *p = *src;
13616a06b24SSage Weil     }
13716a06b24SSage Weil     *p = '\0';
13816a06b24SSage Weil }
13916a06b24SSage Weil 
140c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
141d61563b2SMarkus Armbruster                                     Error **errp)
142f27aaf4bSChristian Brunner {
143f27aaf4bSChristian Brunner     const char *start;
144e98c6961SEric Blake     char *p, *buf;
145e98c6961SEric Blake     QList *keypairs = NULL;
1467830f909SJeff Cody     char *found_str;
147f27aaf4bSChristian Brunner 
148f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
149d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
150c7cacb3eSJeff Cody         return;
151f27aaf4bSChristian Brunner     }
152f27aaf4bSChristian Brunner 
1537267c094SAnthony Liguori     buf = g_strdup(start);
154f27aaf4bSChristian Brunner     p = buf;
155f27aaf4bSChristian Brunner 
156730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1577830f909SJeff Cody     if (!p) {
1587830f909SJeff Cody         error_setg(errp, "Pool name is required");
1597830f909SJeff Cody         goto done;
1607830f909SJeff Cody     }
1617830f909SJeff Cody     qemu_rbd_unescape(found_str);
16246f5ac20SEric Blake     qdict_put_str(options, "pool", found_str);
163fab5cf59SJosh Durgin 
164fab5cf59SJosh Durgin     if (strchr(p, '@')) {
165730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, '@', &p);
1667830f909SJeff Cody         qemu_rbd_unescape(found_str);
16746f5ac20SEric Blake         qdict_put_str(options, "image", found_str);
1687830f909SJeff Cody 
169730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1707830f909SJeff Cody         qemu_rbd_unescape(found_str);
17146f5ac20SEric Blake         qdict_put_str(options, "snapshot", found_str);
1727830f909SJeff Cody     } else {
173730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1747830f909SJeff Cody         qemu_rbd_unescape(found_str);
17546f5ac20SEric Blake         qdict_put_str(options, "image", found_str);
1767830f909SJeff Cody     }
1777830f909SJeff Cody     if (!p) {
178f27aaf4bSChristian Brunner         goto done;
179f27aaf4bSChristian Brunner     }
180f27aaf4bSChristian Brunner 
181c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
182c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
183c7cacb3eSJeff Cody     while (p) {
184c7cacb3eSJeff Cody         char *name, *value;
185730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
186c7cacb3eSJeff Cody         if (!p) {
187c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
188c7cacb3eSJeff Cody             break;
189c7cacb3eSJeff Cody         }
190c7cacb3eSJeff Cody 
191c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
192c7cacb3eSJeff Cody 
193730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
194c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
195c7cacb3eSJeff Cody 
196c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
19746f5ac20SEric Blake             qdict_put_str(options, "conf", value);
198c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
19946f5ac20SEric Blake             qdict_put_str(options, "user", value);
200c7cacb3eSJeff Cody         } else {
201e98c6961SEric Blake             /*
202e98c6961SEric Blake              * We pass these internally to qemu_rbd_set_keypairs(), so
203e98c6961SEric Blake              * we can get away with the simpler list of [ "key1",
204e98c6961SEric Blake              * "value1", "key2", "value2" ] rather than a raw dict
205e98c6961SEric Blake              * { "key1": "value1", "key2": "value2" } where we can't
206e98c6961SEric Blake              * guarantee order, or even a more correct but complex
207e98c6961SEric Blake              * [ { "key1": "value1" }, { "key2": "value2" } ]
208e98c6961SEric Blake              */
209e98c6961SEric Blake             if (!keypairs) {
210e98c6961SEric Blake                 keypairs = qlist_new();
211c7cacb3eSJeff Cody             }
21246f5ac20SEric Blake             qlist_append_str(keypairs, name);
21346f5ac20SEric Blake             qlist_append_str(keypairs, value);
214c7cacb3eSJeff Cody         }
215c7cacb3eSJeff Cody     }
216c7cacb3eSJeff Cody 
217e98c6961SEric Blake     if (keypairs) {
218e98c6961SEric Blake         qdict_put(options, "=keyvalue-pairs",
219e98c6961SEric Blake                   qobject_to_json(QOBJECT(keypairs)));
220c7cacb3eSJeff Cody     }
221c7cacb3eSJeff Cody 
222f27aaf4bSChristian Brunner done:
2237267c094SAnthony Liguori     g_free(buf);
224e98c6961SEric Blake     QDECREF(keypairs);
225c7cacb3eSJeff Cody     return;
2267c7e9df0SSage Weil }
2277c7e9df0SSage Weil 
22860390a21SDaniel P. Berrange 
22960390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
23060390a21SDaniel P. Berrange                              Error **errp)
23160390a21SDaniel P. Berrange {
23260390a21SDaniel P. Berrange     if (secretid == 0) {
23360390a21SDaniel P. Berrange         return 0;
23460390a21SDaniel P. Berrange     }
23560390a21SDaniel P. Berrange 
23660390a21SDaniel P. Berrange     gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
23760390a21SDaniel P. Berrange                                                     errp);
23860390a21SDaniel P. Berrange     if (!secret) {
23960390a21SDaniel P. Berrange         return -1;
24060390a21SDaniel P. Berrange     }
24160390a21SDaniel P. Berrange 
24260390a21SDaniel P. Berrange     rados_conf_set(cluster, "key", secret);
24360390a21SDaniel P. Berrange     g_free(secret);
24460390a21SDaniel P. Berrange 
24560390a21SDaniel P. Berrange     return 0;
24660390a21SDaniel P. Berrange }
24760390a21SDaniel P. Berrange 
248e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
249e34d8f29SJosh Durgin                                  Error **errp)
250fab5cf59SJosh Durgin {
251e98c6961SEric Blake     QList *keypairs;
252e98c6961SEric Blake     QString *name;
253e98c6961SEric Blake     QString *value;
254e98c6961SEric Blake     const char *key;
255e98c6961SEric Blake     size_t remaining;
256fab5cf59SJosh Durgin     int ret = 0;
257fab5cf59SJosh Durgin 
258e98c6961SEric Blake     if (!keypairs_json) {
259e98c6961SEric Blake         return ret;
260fab5cf59SJosh Durgin     }
261e98c6961SEric Blake     keypairs = qobject_to_qlist(qobject_from_json(keypairs_json,
262e98c6961SEric Blake                                                   &error_abort));
263e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
264e98c6961SEric Blake     assert(remaining);
265fab5cf59SJosh Durgin 
266e98c6961SEric Blake     while (remaining--) {
267e98c6961SEric Blake         name = qobject_to_qstring(qlist_pop(keypairs));
268e98c6961SEric Blake         value = qobject_to_qstring(qlist_pop(keypairs));
269e98c6961SEric Blake         assert(name && value);
270e98c6961SEric Blake         key = qstring_get_str(name);
271fab5cf59SJosh Durgin 
272e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
273e98c6961SEric Blake         QDECREF(value);
274fab5cf59SJosh Durgin         if (ret < 0) {
275e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
27671c87815SKevin Wolf             QDECREF(name);
277fab5cf59SJosh Durgin             ret = -EINVAL;
278fab5cf59SJosh Durgin             break;
279fab5cf59SJosh Durgin         }
28071c87815SKevin Wolf         QDECREF(name);
281fab5cf59SJosh Durgin     }
282fab5cf59SJosh Durgin 
283e98c6961SEric Blake     QDECREF(keypairs);
284fab5cf59SJosh Durgin     return ret;
285fab5cf59SJosh Durgin }
286fab5cf59SJosh Durgin 
2871d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
2881d393bdeStianqing {
2891d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
2901d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
2911d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
2921d393bdeStianqing                    acb->qiov->size - offs);
2931d393bdeStianqing     } else {
2941d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
2951d393bdeStianqing     }
2961d393bdeStianqing }
2971d393bdeStianqing 
2980f9d252dSJeff Cody static QemuOptsList runtime_opts = {
2990f9d252dSJeff Cody     .name = "rbd",
3000f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3010f9d252dSJeff Cody     .desc = {
3020f9d252dSJeff Cody         {
3030f9d252dSJeff Cody             .name = "pool",
3040f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3050f9d252dSJeff Cody             .help = "Rados pool name",
3060f9d252dSJeff Cody         },
3070f9d252dSJeff Cody         {
3080f9d252dSJeff Cody             .name = "image",
3090f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3100f9d252dSJeff Cody             .help = "Image name in the pool",
3110f9d252dSJeff Cody         },
3120f9d252dSJeff Cody         {
313cbf036b4SMarkus Armbruster             .name = "conf",
314cbf036b4SMarkus Armbruster             .type = QEMU_OPT_STRING,
315cbf036b4SMarkus Armbruster             .help = "Rados config file location",
316cbf036b4SMarkus Armbruster         },
317cbf036b4SMarkus Armbruster         {
3180f9d252dSJeff Cody             .name = "snapshot",
3190f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3200f9d252dSJeff Cody             .help = "Ceph snapshot name",
3210f9d252dSJeff Cody         },
3220f9d252dSJeff Cody         {
3230f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3240f9d252dSJeff Cody             .name = "user",
3250f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3260f9d252dSJeff Cody             .help = "Rados id name",
3270f9d252dSJeff Cody         },
328cbf036b4SMarkus Armbruster         /*
3292836284dSMarkus Armbruster          * server.* extracted manually, see qemu_rbd_mon_host()
330cbf036b4SMarkus Armbruster          */
3310f9d252dSJeff Cody         { /* end of list */ }
3320f9d252dSJeff Cody     },
3330f9d252dSJeff Cody };
3340f9d252dSJeff Cody 
335*1bebea37SKevin Wolf /* FIXME Deprecate and remove keypairs or make it available in QMP.
336*1bebea37SKevin Wolf  * password_secret should eventually be configurable in opts->location. Support
337*1bebea37SKevin Wolf  * for it in .bdrv_open will make it work here as well. */
338*1bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
339*1bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
340*1bebea37SKevin Wolf                               Error **errp)
341*1bebea37SKevin Wolf {
342*1bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
343*1bebea37SKevin Wolf     rados_t cluster;
344*1bebea37SKevin Wolf     rados_ioctx_t io_ctx;
345*1bebea37SKevin Wolf     int obj_order = 0;
346*1bebea37SKevin Wolf     int ret;
347*1bebea37SKevin Wolf 
348*1bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
349*1bebea37SKevin Wolf     if (opts->location->has_snapshot) {
350*1bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
351*1bebea37SKevin Wolf         return -EINVAL;
352*1bebea37SKevin Wolf     }
353*1bebea37SKevin Wolf 
354*1bebea37SKevin Wolf     /* TODO Remove the limitation */
355*1bebea37SKevin Wolf     if (opts->location->has_server) {
356*1bebea37SKevin Wolf         error_setg(errp, "Can't specify server for image creation");
357*1bebea37SKevin Wolf         return -EINVAL;
358*1bebea37SKevin Wolf     }
359*1bebea37SKevin Wolf 
360*1bebea37SKevin Wolf     if (opts->has_cluster_size) {
361*1bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
362*1bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
363*1bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
364*1bebea37SKevin Wolf             return -EINVAL;
365*1bebea37SKevin Wolf         }
366*1bebea37SKevin Wolf         if (objsize < 4096) {
367*1bebea37SKevin Wolf             error_setg(errp, "obj size too small");
368*1bebea37SKevin Wolf             return -EINVAL;
369*1bebea37SKevin Wolf         }
370*1bebea37SKevin Wolf         obj_order = ctz32(objsize);
371*1bebea37SKevin Wolf     }
372*1bebea37SKevin Wolf 
373*1bebea37SKevin Wolf     ret = rados_create(&cluster, opts->location->user);
374*1bebea37SKevin Wolf     if (ret < 0) {
375*1bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error initializing");
376*1bebea37SKevin Wolf         return ret;
377*1bebea37SKevin Wolf     }
378*1bebea37SKevin Wolf 
379*1bebea37SKevin Wolf     /* try default location when conf=NULL, but ignore failure */
380*1bebea37SKevin Wolf     ret = rados_conf_read_file(cluster, opts->location->conf);
381*1bebea37SKevin Wolf     if (opts->location->conf && ret < 0) {
382*1bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error reading conf file %s",
383*1bebea37SKevin Wolf                          opts->location->conf);
384*1bebea37SKevin Wolf         ret = -EIO;
385*1bebea37SKevin Wolf         goto shutdown;
386*1bebea37SKevin Wolf     }
387*1bebea37SKevin Wolf 
388*1bebea37SKevin Wolf     ret = qemu_rbd_set_keypairs(cluster, keypairs, errp);
389*1bebea37SKevin Wolf     if (ret < 0) {
390*1bebea37SKevin Wolf         ret = -EIO;
391*1bebea37SKevin Wolf         goto shutdown;
392*1bebea37SKevin Wolf     }
393*1bebea37SKevin Wolf 
394*1bebea37SKevin Wolf     if (qemu_rbd_set_auth(cluster, password_secret, errp) < 0) {
395*1bebea37SKevin Wolf         ret = -EIO;
396*1bebea37SKevin Wolf         goto shutdown;
397*1bebea37SKevin Wolf     }
398*1bebea37SKevin Wolf 
399*1bebea37SKevin Wolf     ret = rados_connect(cluster);
400*1bebea37SKevin Wolf     if (ret < 0) {
401*1bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error connecting");
402*1bebea37SKevin Wolf         goto shutdown;
403*1bebea37SKevin Wolf     }
404*1bebea37SKevin Wolf 
405*1bebea37SKevin Wolf     ret = rados_ioctx_create(cluster, opts->location->pool, &io_ctx);
406*1bebea37SKevin Wolf     if (ret < 0) {
407*1bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error opening pool %s",
408*1bebea37SKevin Wolf                          opts->location->pool);
409*1bebea37SKevin Wolf         goto shutdown;
410*1bebea37SKevin Wolf     }
411*1bebea37SKevin Wolf 
412*1bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
413*1bebea37SKevin Wolf     if (ret < 0) {
414*1bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
415*1bebea37SKevin Wolf     }
416*1bebea37SKevin Wolf 
417*1bebea37SKevin Wolf     rados_ioctx_destroy(io_ctx);
418*1bebea37SKevin Wolf 
419*1bebea37SKevin Wolf     ret = 0;
420*1bebea37SKevin Wolf shutdown:
421*1bebea37SKevin Wolf     rados_shutdown(cluster);
422*1bebea37SKevin Wolf     return ret;
423*1bebea37SKevin Wolf }
424*1bebea37SKevin Wolf 
425*1bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
426*1bebea37SKevin Wolf {
427*1bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
428*1bebea37SKevin Wolf }
429*1bebea37SKevin Wolf 
430efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
431efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
432efc75e2aSStefan Hajnoczi                                                 Error **errp)
433f27aaf4bSChristian Brunner {
434*1bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
435*1bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
436*1bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
437d61563b2SMarkus Armbruster     Error *local_err = NULL;
438*1bebea37SKevin Wolf     const char *keypairs, *password_secret;
439c7cacb3eSJeff Cody     QDict *options = NULL;
440c7cacb3eSJeff Cody     int ret = 0;
441f27aaf4bSChristian Brunner 
442*1bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
443*1bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
444*1bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
445*1bebea37SKevin Wolf 
446*1bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
447*1bebea37SKevin Wolf 
448*1bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
44960390a21SDaniel P. Berrange 
450f27aaf4bSChristian Brunner     /* Read out options */
451*1bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
452c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
453*1bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
454*1bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
455*1bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
456f27aaf4bSChristian Brunner 
457c7cacb3eSJeff Cody     options = qdict_new();
458c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
459c7cacb3eSJeff Cody     if (local_err) {
460c7cacb3eSJeff Cody         ret = -EINVAL;
461c7cacb3eSJeff Cody         error_propagate(errp, local_err);
462c7cacb3eSJeff Cody         goto exit;
463c7cacb3eSJeff Cody     }
464c7cacb3eSJeff Cody 
465129c7d1cSMarkus Armbruster     /*
466129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
467129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
468129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
469129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
470129c7d1cSMarkus Armbruster      */
471*1bebea37SKevin Wolf     loc = rbd_opts->location;
472*1bebea37SKevin Wolf     loc->pool     = g_strdup(qdict_get_try_str(options, "pool"));
473*1bebea37SKevin Wolf     loc->conf     = g_strdup(qdict_get_try_str(options, "conf"));
474*1bebea37SKevin Wolf     loc->has_conf = !!loc->conf;
475*1bebea37SKevin Wolf     loc->user     = g_strdup(qdict_get_try_str(options, "user"));
476*1bebea37SKevin Wolf     loc->has_user = !!loc->user;
477*1bebea37SKevin Wolf     loc->image    = g_strdup(qdict_get_try_str(options, "image"));
47807846397SMarkus Armbruster     keypairs      = qdict_get_try_str(options, "=keyvalue-pairs");
479c7cacb3eSJeff Cody 
480*1bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
48187cd3d20SVikhyat Umrao     if (ret < 0) {
482c7cacb3eSJeff Cody         goto exit;
483f27aaf4bSChristian Brunner     }
484f27aaf4bSChristian Brunner 
485c7cacb3eSJeff Cody exit:
486c7cacb3eSJeff Cody     QDECREF(options);
487*1bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
488f27aaf4bSChristian Brunner     return ret;
489f27aaf4bSChristian Brunner }
490f27aaf4bSChristian Brunner 
491f27aaf4bSChristian Brunner /*
492e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
493e04fb07fSStefan Hajnoczi  * BH context.
494f27aaf4bSChristian Brunner  */
495ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
496f27aaf4bSChristian Brunner {
497f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
498f27aaf4bSChristian Brunner     int64_t r;
499f27aaf4bSChristian Brunner 
500f27aaf4bSChristian Brunner     r = rcb->ret;
501f27aaf4bSChristian Brunner 
502dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
503f27aaf4bSChristian Brunner         if (r < 0) {
504f27aaf4bSChristian Brunner             acb->ret = r;
505f27aaf4bSChristian Brunner             acb->error = 1;
506f27aaf4bSChristian Brunner         } else if (!acb->error) {
507ad32e9c0SJosh Durgin             acb->ret = rcb->size;
508f27aaf4bSChristian Brunner         }
509f27aaf4bSChristian Brunner     } else {
510ad32e9c0SJosh Durgin         if (r < 0) {
5111d393bdeStianqing             qemu_rbd_memset(rcb, 0);
512f27aaf4bSChristian Brunner             acb->ret = r;
513f27aaf4bSChristian Brunner             acb->error = 1;
514ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
5151d393bdeStianqing             qemu_rbd_memset(rcb, r);
516f27aaf4bSChristian Brunner             if (!acb->error) {
517ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
518f27aaf4bSChristian Brunner             }
519f27aaf4bSChristian Brunner         } else if (!acb->error) {
520ad32e9c0SJosh Durgin             acb->ret = r;
521f27aaf4bSChristian Brunner         }
522f27aaf4bSChristian Brunner     }
523e04fb07fSStefan Hajnoczi 
5247267c094SAnthony Liguori     g_free(rcb);
525e04fb07fSStefan Hajnoczi 
5261d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
527e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
528e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
529f27aaf4bSChristian Brunner         }
530e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5311d393bdeStianqing     }
5321d393bdeStianqing 
533e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
534f27aaf4bSChristian Brunner 
5358007429aSFam Zheng     qemu_aio_unref(acb);
536f27aaf4bSChristian Brunner }
537f27aaf4bSChristian Brunner 
5384bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
5390a55679bSJeff Cody {
5404bfb2741SKevin Wolf     const char **vals;
5412836284dSMarkus Armbruster     const char *host, *port;
5422836284dSMarkus Armbruster     char *rados_str;
5434bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
5444bfb2741SKevin Wolf     int i, cnt;
5450a55679bSJeff Cody 
5464bfb2741SKevin Wolf     if (!opts->has_server) {
5474bfb2741SKevin Wolf         return NULL;
5480a55679bSJeff Cody     }
5494bfb2741SKevin Wolf 
5504bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
5514bfb2741SKevin Wolf         cnt++;
5520a55679bSJeff Cody     }
5530a55679bSJeff Cody 
5544bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
5554bfb2741SKevin Wolf 
5564bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
5574bfb2741SKevin Wolf         host = p->value->host;
5584bfb2741SKevin Wolf         port = p->value->port;
5594bfb2741SKevin Wolf 
5600a55679bSJeff Cody         if (strchr(host, ':')) {
5614bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
5620a55679bSJeff Cody         } else {
5634bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
5640a55679bSJeff Cody         }
5650a55679bSJeff Cody     }
5662836284dSMarkus Armbruster     vals[i] = NULL;
5670a55679bSJeff Cody 
5682836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
5692836284dSMarkus Armbruster     g_strfreev((char **)vals);
5700a55679bSJeff Cody     return rados_str;
5710a55679bSJeff Cody }
5720a55679bSJeff Cody 
5733d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
5743d9136f9SKevin Wolf                             char **s_snap, char **s_image_name,
5754bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
5764ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
5774ff45049SKevin Wolf                             Error **errp)
578f27aaf4bSChristian Brunner {
5790a55679bSJeff Cody     char *mon_host = NULL;
5803d9136f9SKevin Wolf     Error *local_err = NULL;
581f27aaf4bSChristian Brunner     int r;
582f27aaf4bSChristian Brunner 
5834bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
58484d18f06SMarkus Armbruster     if (local_err) {
585d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
5862836284dSMarkus Armbruster         r = -EINVAL;
5872836284dSMarkus Armbruster         goto failed_opts;
588a9ccedc3SKevin Wolf     }
589a9ccedc3SKevin Wolf 
5904bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
591ad32e9c0SJosh Durgin     if (r < 0) {
59287cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
593c3ca988dSKevin Wolf         goto failed_opts;
594f27aaf4bSChristian Brunner     }
595f27aaf4bSChristian Brunner 
5964bfb2741SKevin Wolf     *s_snap = g_strdup(opts->snapshot);
5974bfb2741SKevin Wolf     *s_image_name = g_strdup(opts->image);
598eb93d5d9SSage Weil 
599c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
6004bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
6014bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
6024bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
603e34d8f29SJosh Durgin         goto failed_shutdown;
604e34d8f29SJosh Durgin     }
60599a3c89dSJosh Durgin 
6063d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
60799a3c89dSJosh Durgin     if (r < 0) {
60899a3c89dSJosh Durgin         goto failed_shutdown;
60999a3c89dSJosh Durgin     }
61099a3c89dSJosh Durgin 
6110a55679bSJeff Cody     if (mon_host) {
6123d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
6130a55679bSJeff Cody         if (r < 0) {
6140a55679bSJeff Cody             goto failed_shutdown;
6150a55679bSJeff Cody         }
6160a55679bSJeff Cody     }
6170a55679bSJeff Cody 
6183d9136f9SKevin Wolf     if (qemu_rbd_set_auth(*cluster, secretid, errp) < 0) {
61960390a21SDaniel P. Berrange         r = -EIO;
62060390a21SDaniel P. Berrange         goto failed_shutdown;
62160390a21SDaniel P. Berrange     }
62260390a21SDaniel P. Berrange 
623b11f38fcSJosh Durgin     /*
624b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
625b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
626b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
627b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
628b11f38fcSJosh Durgin      * be set up, fall back to no caching.
629b11f38fcSJosh Durgin      */
6303d9136f9SKevin Wolf     if (cache) {
6313d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
632b11f38fcSJosh Durgin     } else {
6333d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
634b11f38fcSJosh Durgin     }
635b11f38fcSJosh Durgin 
6363d9136f9SKevin Wolf     r = rados_connect(*cluster);
637ad32e9c0SJosh Durgin     if (r < 0) {
63887cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
639eb93d5d9SSage Weil         goto failed_shutdown;
640ad32e9c0SJosh Durgin     }
641ad32e9c0SJosh Durgin 
6424bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
643ad32e9c0SJosh Durgin     if (r < 0) {
6444bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
645eb93d5d9SSage Weil         goto failed_shutdown;
646ad32e9c0SJosh Durgin     }
647ad32e9c0SJosh Durgin 
6483d9136f9SKevin Wolf     return 0;
6493d9136f9SKevin Wolf 
6503d9136f9SKevin Wolf failed_shutdown:
6513d9136f9SKevin Wolf     rados_shutdown(*cluster);
6523d9136f9SKevin Wolf     g_free(*s_snap);
6533d9136f9SKevin Wolf     g_free(*s_image_name);
6543d9136f9SKevin Wolf failed_opts:
6553d9136f9SKevin Wolf     g_free(mon_host);
6563d9136f9SKevin Wolf     return r;
6573d9136f9SKevin Wolf }
6583d9136f9SKevin Wolf 
6593d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
6603d9136f9SKevin Wolf                          Error **errp)
6613d9136f9SKevin Wolf {
6623d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
6634bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
6644bfb2741SKevin Wolf     Visitor *v;
6654bfb2741SKevin Wolf     QObject *crumpled = NULL;
6663d9136f9SKevin Wolf     Error *local_err = NULL;
6673d9136f9SKevin Wolf     const char *filename;
6684ff45049SKevin Wolf     char *keypairs, *secretid;
6693d9136f9SKevin Wolf     int r;
6703d9136f9SKevin Wolf 
6713d9136f9SKevin Wolf     /* If we are given a filename, parse the filename, with precedence given to
6723d9136f9SKevin Wolf      * filename encoded options */
6733d9136f9SKevin Wolf     filename = qdict_get_try_str(options, "filename");
6743d9136f9SKevin Wolf     if (filename) {
6753d9136f9SKevin Wolf         warn_report("'filename' option specified. "
6763d9136f9SKevin Wolf                     "This is an unsupported option, and may be deprecated "
6773d9136f9SKevin Wolf                     "in the future");
6783d9136f9SKevin Wolf         qemu_rbd_parse_filename(filename, options, &local_err);
6794ff45049SKevin Wolf         qdict_del(options, "filename");
6803d9136f9SKevin Wolf         if (local_err) {
6813d9136f9SKevin Wolf             error_propagate(errp, local_err);
6823d9136f9SKevin Wolf             return -EINVAL;
6833d9136f9SKevin Wolf         }
6843d9136f9SKevin Wolf     }
6853d9136f9SKevin Wolf 
6864ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
6874ff45049SKevin Wolf     if (keypairs) {
6884ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
6894ff45049SKevin Wolf     }
6904ff45049SKevin Wolf 
6914ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
6924ff45049SKevin Wolf     if (secretid) {
6934ff45049SKevin Wolf         qdict_del(options, "password-secret");
6944ff45049SKevin Wolf     }
6954ff45049SKevin Wolf 
6964bfb2741SKevin Wolf     /* Convert the remaining options into a QAPI object */
6974bfb2741SKevin Wolf     crumpled = qdict_crumple(options, errp);
6984bfb2741SKevin Wolf     if (crumpled == NULL) {
6994bfb2741SKevin Wolf         r = -EINVAL;
7004bfb2741SKevin Wolf         goto out;
7014bfb2741SKevin Wolf     }
7024bfb2741SKevin Wolf 
7034bfb2741SKevin Wolf     v = qobject_input_visitor_new_keyval(crumpled);
7044bfb2741SKevin Wolf     visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err);
7054bfb2741SKevin Wolf     visit_free(v);
7064bfb2741SKevin Wolf     qobject_decref(crumpled);
7074bfb2741SKevin Wolf 
7084bfb2741SKevin Wolf     if (local_err) {
7094bfb2741SKevin Wolf         error_propagate(errp, local_err);
7104bfb2741SKevin Wolf         r = -EINVAL;
7114bfb2741SKevin Wolf         goto out;
7124bfb2741SKevin Wolf     }
7134bfb2741SKevin Wolf 
7143d9136f9SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, &s->snap, &s->image_name,
7154bfb2741SKevin Wolf                          opts, !(flags & BDRV_O_NOCACHE), keypairs, secretid,
7164ff45049SKevin Wolf                          errp);
7173d9136f9SKevin Wolf     if (r < 0) {
7184ff45049SKevin Wolf         goto out;
7193d9136f9SKevin Wolf     }
7203d9136f9SKevin Wolf 
721e2b8247aSJeff Cody     /* rbd_open is always r/w */
72280b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
723ad32e9c0SJosh Durgin     if (r < 0) {
72480b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
72580b61a27SJeff Cody                          s->image_name);
726eb93d5d9SSage Weil         goto failed_open;
727ad32e9c0SJosh Durgin     }
728ad32e9c0SJosh Durgin 
729e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
730e2b8247aSJeff Cody      * leave as-is */
731e2b8247aSJeff Cody     if (s->snap != NULL) {
732398e6ad0SKevin Wolf         if (!bdrv_is_read_only(bs)) {
733398e6ad0SKevin Wolf             error_report("Opening rbd snapshots without an explicit "
734398e6ad0SKevin Wolf                          "read-only=on option is deprecated. Future versions "
735398e6ad0SKevin Wolf                          "will refuse to open the image instead of "
736398e6ad0SKevin Wolf                          "automatically marking the image read-only.");
737e2b8247aSJeff Cody             r = bdrv_set_read_only(bs, true, &local_err);
738e2b8247aSJeff Cody             if (r < 0) {
739e2b8247aSJeff Cody                 error_propagate(errp, local_err);
740e2b8247aSJeff Cody                 goto failed_open;
741e2b8247aSJeff Cody             }
742e2b8247aSJeff Cody         }
743398e6ad0SKevin Wolf     }
744f27aaf4bSChristian Brunner 
7454ff45049SKevin Wolf     r = 0;
7464ff45049SKevin Wolf     goto out;
747f27aaf4bSChristian Brunner 
748eb93d5d9SSage Weil failed_open:
749ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
750eb93d5d9SSage Weil     g_free(s->snap);
75180b61a27SJeff Cody     g_free(s->image_name);
7523d9136f9SKevin Wolf     rados_shutdown(s->cluster);
7534ff45049SKevin Wolf out:
7544bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
7554ff45049SKevin Wolf     g_free(keypairs);
7564ff45049SKevin Wolf     g_free(secretid);
757f27aaf4bSChristian Brunner     return r;
758f27aaf4bSChristian Brunner }
759f27aaf4bSChristian Brunner 
76056e7cf8dSJeff Cody 
76156e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
76256e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
76356e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
76456e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
76556e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
76656e7cf8dSJeff Cody {
76756e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
76856e7cf8dSJeff Cody     int ret = 0;
76956e7cf8dSJeff Cody 
77056e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
77156e7cf8dSJeff Cody         error_setg(errp,
77256e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
77356e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
77456e7cf8dSJeff Cody         ret = -EINVAL;
77556e7cf8dSJeff Cody     }
77656e7cf8dSJeff Cody 
77756e7cf8dSJeff Cody     return ret;
77856e7cf8dSJeff Cody }
77956e7cf8dSJeff Cody 
780ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
781f27aaf4bSChristian Brunner {
782f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
783f27aaf4bSChristian Brunner 
784ad32e9c0SJosh Durgin     rbd_close(s->image);
785ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
7867267c094SAnthony Liguori     g_free(s->snap);
78780b61a27SJeff Cody     g_free(s->image_name);
788ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
789f27aaf4bSChristian Brunner }
790f27aaf4bSChristian Brunner 
791d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
792f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
793f27aaf4bSChristian Brunner };
794f27aaf4bSChristian Brunner 
795e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
796f27aaf4bSChristian Brunner {
797e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
798e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
799ad32e9c0SJosh Durgin }
800ad32e9c0SJosh Durgin 
801ad32e9c0SJosh Durgin /*
802ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
803ad32e9c0SJosh Durgin  *
804ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
805ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
806e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
807e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
808ad32e9c0SJosh Durgin  */
809ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
810ad32e9c0SJosh Durgin {
811e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
812e04fb07fSStefan Hajnoczi 
813ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
814ad32e9c0SJosh Durgin     rbd_aio_release(c);
815f27aaf4bSChristian Brunner 
816fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
817ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
818473c7f02SStefan Priebe }
819f27aaf4bSChristian Brunner 
820787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
821787f3133SJosh Durgin                                    uint64_t off,
822787f3133SJosh Durgin                                    uint64_t len,
823787f3133SJosh Durgin                                    rbd_completion_t comp)
824787f3133SJosh Durgin {
825787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
826787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
827787f3133SJosh Durgin #else
828787f3133SJosh Durgin     return -ENOTSUP;
829787f3133SJosh Durgin #endif
830787f3133SJosh Durgin }
831787f3133SJosh Durgin 
832dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
833dc7588c1SJosh Durgin                                  rbd_completion_t comp)
834dc7588c1SJosh Durgin {
835dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
836dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
837dc7588c1SJosh Durgin #else
838dc7588c1SJosh Durgin     return -ENOTSUP;
839dc7588c1SJosh Durgin #endif
840dc7588c1SJosh Durgin }
841dc7588c1SJosh Durgin 
8427c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
8437bbca9e2SEric Blake                                  int64_t off,
844f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
8457bbca9e2SEric Blake                                  int64_t size,
846097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
847787f3133SJosh Durgin                                  void *opaque,
848787f3133SJosh Durgin                                  RBDAIOCmd cmd)
849f27aaf4bSChristian Brunner {
850f27aaf4bSChristian Brunner     RBDAIOCB *acb;
8510f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
852ad32e9c0SJosh Durgin     rbd_completion_t c;
85351a13528SJosh Durgin     int r;
854f27aaf4bSChristian Brunner 
855f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
856f27aaf4bSChristian Brunner 
857d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
858787f3133SJosh Durgin     acb->cmd = cmd;
859f27aaf4bSChristian Brunner     acb->qiov = qiov;
8607bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
8611d393bdeStianqing 
8621d393bdeStianqing     rcb = g_new(RADOSCB, 1);
8631d393bdeStianqing 
8641d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
865dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
866787f3133SJosh Durgin             acb->bounce = NULL;
867787f3133SJosh Durgin         } else {
8680f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
8690f7a0237SKevin Wolf             if (acb->bounce == NULL) {
8700f7a0237SKevin Wolf                 goto failed;
8710f7a0237SKevin Wolf             }
872787f3133SJosh Durgin         }
8731d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
8741d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
8751d393bdeStianqing         }
8761d393bdeStianqing         rcb->buf = acb->bounce;
8771d393bdeStianqing     }
8781d393bdeStianqing 
879f27aaf4bSChristian Brunner     acb->ret = 0;
880f27aaf4bSChristian Brunner     acb->error = 0;
881f27aaf4bSChristian Brunner     acb->s = s;
882f27aaf4bSChristian Brunner 
883f27aaf4bSChristian Brunner     rcb->acb = acb;
884f27aaf4bSChristian Brunner     rcb->s = acb->s;
885ad32e9c0SJosh Durgin     rcb->size = size;
88651a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
88751a13528SJosh Durgin     if (r < 0) {
88851a13528SJosh Durgin         goto failed;
88951a13528SJosh Durgin     }
890f27aaf4bSChristian Brunner 
891787f3133SJosh Durgin     switch (cmd) {
892787f3133SJosh Durgin     case RBD_AIO_WRITE:
8931d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
8941d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
8951d393bdeStianqing #else
8961d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
8971d393bdeStianqing #endif
898787f3133SJosh Durgin         break;
899787f3133SJosh Durgin     case RBD_AIO_READ:
9001d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9011d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
9021d393bdeStianqing #else
9031d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
9041d393bdeStianqing #endif
905787f3133SJosh Durgin         break;
906787f3133SJosh Durgin     case RBD_AIO_DISCARD:
907787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
908787f3133SJosh Durgin         break;
909dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
910dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
911dc7588c1SJosh Durgin         break;
912787f3133SJosh Durgin     default:
913787f3133SJosh Durgin         r = -EINVAL;
91451a13528SJosh Durgin     }
91551a13528SJosh Durgin 
91651a13528SJosh Durgin     if (r < 0) {
917405a2764SKevin Wolf         goto failed_completion;
918f27aaf4bSChristian Brunner     }
919f27aaf4bSChristian Brunner     return &acb->common;
92051a13528SJosh Durgin 
921405a2764SKevin Wolf failed_completion:
922405a2764SKevin Wolf     rbd_aio_release(c);
92351a13528SJosh Durgin failed:
9247267c094SAnthony Liguori     g_free(rcb);
9251d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
926405a2764SKevin Wolf         qemu_vfree(acb->bounce);
9271d393bdeStianqing     }
9281d393bdeStianqing 
9298007429aSFam Zheng     qemu_aio_unref(acb);
93051a13528SJosh Durgin     return NULL;
931f27aaf4bSChristian Brunner }
932f27aaf4bSChristian Brunner 
9337c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
934ad32e9c0SJosh Durgin                                       int64_t sector_num,
935ad32e9c0SJosh Durgin                                       QEMUIOVector *qiov,
936f27aaf4bSChristian Brunner                                       int nb_sectors,
937097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
938f27aaf4bSChristian Brunner                                       void *opaque)
939f27aaf4bSChristian Brunner {
9407bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
941e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
942787f3133SJosh Durgin                          RBD_AIO_READ);
943f27aaf4bSChristian Brunner }
944f27aaf4bSChristian Brunner 
9457c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
946ad32e9c0SJosh Durgin                                        int64_t sector_num,
947ad32e9c0SJosh Durgin                                        QEMUIOVector *qiov,
948f27aaf4bSChristian Brunner                                        int nb_sectors,
949097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
950f27aaf4bSChristian Brunner                                        void *opaque)
951f27aaf4bSChristian Brunner {
9527bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
953e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
954787f3133SJosh Durgin                          RBD_AIO_WRITE);
955f27aaf4bSChristian Brunner }
956f27aaf4bSChristian Brunner 
957dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
9587c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
959097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
960dc7588c1SJosh Durgin                                       void *opaque)
961dc7588c1SJosh Durgin {
962dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
963dc7588c1SJosh Durgin }
964dc7588c1SJosh Durgin 
965dc7588c1SJosh Durgin #else
966dc7588c1SJosh Durgin 
9678b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
9687a3f5fe9SSage Weil {
9697a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
9707a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
9717a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
9727a3f5fe9SSage Weil     return rbd_flush(s->image);
9737a3f5fe9SSage Weil #else
9747a3f5fe9SSage Weil     return 0;
9757a3f5fe9SSage Weil #endif
9767a3f5fe9SSage Weil }
977dc7588c1SJosh Durgin #endif
9787a3f5fe9SSage Weil 
979ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
980f27aaf4bSChristian Brunner {
981f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
982ad32e9c0SJosh Durgin     rbd_image_info_t info;
983ad32e9c0SJosh Durgin     int r;
984ad32e9c0SJosh Durgin 
985ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
986ad32e9c0SJosh Durgin     if (r < 0) {
987ad32e9c0SJosh Durgin         return r;
988ad32e9c0SJosh Durgin     }
989ad32e9c0SJosh Durgin 
990ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
991f27aaf4bSChristian Brunner     return 0;
992f27aaf4bSChristian Brunner }
993f27aaf4bSChristian Brunner 
994ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
995f27aaf4bSChristian Brunner {
996f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
997ad32e9c0SJosh Durgin     rbd_image_info_t info;
998ad32e9c0SJosh Durgin     int r;
999f27aaf4bSChristian Brunner 
1000ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1001ad32e9c0SJosh Durgin     if (r < 0) {
1002ad32e9c0SJosh Durgin         return r;
1003f27aaf4bSChristian Brunner     }
1004f27aaf4bSChristian Brunner 
1005ad32e9c0SJosh Durgin     return info.size;
1006ad32e9c0SJosh Durgin }
1007ad32e9c0SJosh Durgin 
10088243ccb7SMax Reitz static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset,
10098243ccb7SMax Reitz                              PreallocMode prealloc, Error **errp)
101030cdc48cSJosh Durgin {
101130cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
101230cdc48cSJosh Durgin     int r;
101330cdc48cSJosh Durgin 
10148243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
10158243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
1016977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
10178243ccb7SMax Reitz         return -ENOTSUP;
10188243ccb7SMax Reitz     }
10198243ccb7SMax Reitz 
102030cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
102130cdc48cSJosh Durgin     if (r < 0) {
1022f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
102330cdc48cSJosh Durgin         return r;
102430cdc48cSJosh Durgin     }
102530cdc48cSJosh Durgin 
102630cdc48cSJosh Durgin     return 0;
102730cdc48cSJosh Durgin }
102830cdc48cSJosh Durgin 
1029ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1030ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1031f27aaf4bSChristian Brunner {
1032f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1033f27aaf4bSChristian Brunner     int r;
1034f27aaf4bSChristian Brunner 
1035f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1036f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1037f27aaf4bSChristian Brunner     }
1038f27aaf4bSChristian Brunner 
1039f27aaf4bSChristian Brunner     /*
1040f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1041f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1042f27aaf4bSChristian Brunner      */
1043f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1044f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1045f27aaf4bSChristian Brunner         return -EINVAL;
1046f27aaf4bSChristian Brunner     }
1047f27aaf4bSChristian Brunner 
1048f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1049f27aaf4bSChristian Brunner         return -ERANGE;
1050f27aaf4bSChristian Brunner     }
1051f27aaf4bSChristian Brunner 
1052ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1053f27aaf4bSChristian Brunner     if (r < 0) {
1054ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1055f27aaf4bSChristian Brunner         return r;
1056f27aaf4bSChristian Brunner     }
1057f27aaf4bSChristian Brunner 
1058f27aaf4bSChristian Brunner     return 0;
1059f27aaf4bSChristian Brunner }
1060f27aaf4bSChristian Brunner 
1061bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1062a89d89d3SWenchao Xia                                 const char *snapshot_id,
1063a89d89d3SWenchao Xia                                 const char *snapshot_name,
1064a89d89d3SWenchao Xia                                 Error **errp)
1065bd603247SGregory Farnum {
1066bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1067bd603247SGregory Farnum     int r;
1068bd603247SGregory Farnum 
1069a89d89d3SWenchao Xia     if (!snapshot_name) {
1070a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1071a89d89d3SWenchao Xia         return -EINVAL;
1072a89d89d3SWenchao Xia     }
1073a89d89d3SWenchao Xia 
1074a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1075a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1076a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1077a89d89d3SWenchao Xia         error_setg(errp,
1078a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1079a89d89d3SWenchao Xia                    "equal to snapshot name");
1080a89d89d3SWenchao Xia         return -EINVAL;
1081a89d89d3SWenchao Xia     }
1082a89d89d3SWenchao Xia 
1083bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1084a89d89d3SWenchao Xia     if (r < 0) {
1085a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1086a89d89d3SWenchao Xia     }
1087bd603247SGregory Farnum     return r;
1088bd603247SGregory Farnum }
1089bd603247SGregory Farnum 
1090bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1091bd603247SGregory Farnum                                   const char *snapshot_name)
1092bd603247SGregory Farnum {
1093bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1094bd603247SGregory Farnum 
10959be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1096bd603247SGregory Farnum }
1097bd603247SGregory Farnum 
1098ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1099ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1100f27aaf4bSChristian Brunner {
1101f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1102f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1103ad32e9c0SJosh Durgin     int i, snap_count;
1104ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1105ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1106f27aaf4bSChristian Brunner 
1107ad32e9c0SJosh Durgin     do {
110802c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1109ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
11109e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
11117267c094SAnthony Liguori             g_free(snaps);
1112f27aaf4bSChristian Brunner         }
1113ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1114f27aaf4bSChristian Brunner 
1115ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1116b9c53290SJosh Durgin         goto done;
1117f27aaf4bSChristian Brunner     }
1118f27aaf4bSChristian Brunner 
11195839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1120f27aaf4bSChristian Brunner 
1121ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1122ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1123f27aaf4bSChristian Brunner 
1124f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1125f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1126f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1127f27aaf4bSChristian Brunner 
1128ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1129f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1130f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1131f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1132f27aaf4bSChristian Brunner     }
1133ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
11349e6337d0SStefan Hajnoczi     g_free(snaps);
1135ad32e9c0SJosh Durgin 
1136b9c53290SJosh Durgin  done:
1137f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1138f27aaf4bSChristian Brunner     return snap_count;
1139f27aaf4bSChristian Brunner }
1140f27aaf4bSChristian Brunner 
1141787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
11424da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
11434da444a0SEric Blake                                          int64_t offset,
1144f5a5ca79SManos Pitsidianakis                                          int bytes,
1145097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1146787f3133SJosh Durgin                                          void *opaque)
1147787f3133SJosh Durgin {
1148f5a5ca79SManos Pitsidianakis     return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1149787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1150787f3133SJosh Durgin }
1151787f3133SJosh Durgin #endif
1152787f3133SJosh Durgin 
1153be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
11542b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1155be217884SAdam Crume                                                       Error **errp)
1156be217884SAdam Crume {
1157be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1158be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1159be217884SAdam Crume     if (r < 0) {
1160be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1161be217884SAdam Crume     }
1162be217884SAdam Crume }
1163be217884SAdam Crume #endif
1164be217884SAdam Crume 
1165bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1166bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1167bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1168bd0cf596SChunyan Liu     .desc = {
1169f27aaf4bSChristian Brunner         {
1170f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1171bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1172f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1173f27aaf4bSChristian Brunner         },
1174f27aaf4bSChristian Brunner         {
1175f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1176bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1177f27aaf4bSChristian Brunner             .help = "RBD object size"
1178f27aaf4bSChristian Brunner         },
117960390a21SDaniel P. Berrange         {
118060390a21SDaniel P. Berrange             .name = "password-secret",
118160390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
118260390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
118360390a21SDaniel P. Berrange         },
1184bd0cf596SChunyan Liu         { /* end of list */ }
1185bd0cf596SChunyan Liu     }
1186f27aaf4bSChristian Brunner };
1187f27aaf4bSChristian Brunner 
1188f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1189f27aaf4bSChristian Brunner     .format_name            = "rbd",
1190f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1191c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1192ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1193ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
119456e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
1195*1bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1196efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
11973ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1198ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1199bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1200ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
120130cdc48cSJosh Durgin     .bdrv_truncate          = qemu_rbd_truncate,
1202f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1203f27aaf4bSChristian Brunner 
1204ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
1205ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
1206dc7588c1SJosh Durgin 
1207dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1208dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1209dc7588c1SJosh Durgin #else
1210c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1211dc7588c1SJosh Durgin #endif
1212f27aaf4bSChristian Brunner 
1213787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
12144da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1215787f3133SJosh Durgin #endif
1216787f3133SJosh Durgin 
1217ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1218bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1219ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1220bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1221be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
12222b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1223be217884SAdam Crume #endif
1224f27aaf4bSChristian Brunner };
1225f27aaf4bSChristian Brunner 
1226f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1227f27aaf4bSChristian Brunner {
1228f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1229f27aaf4bSChristian Brunner }
1230f27aaf4bSChristian Brunner 
1231f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1232