xref: /openbmc/qemu/block/rbd.c (revision 1dcaf52760ccaa2e019164c887b8436ac6c5d8ea)
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"
190b8fa32fSMarkus Armbruster #include "qemu/module.h"
20922a01a0SMarkus Armbruster #include "qemu/option.h"
21737e150eSPaolo Bonzini #include "block/block_int.h"
22609f45eaSMax Reitz #include "block/qdict.h"
2360390a21SDaniel P. Berrange #include "crypto/secret.h"
24f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
25c7cacb3eSJeff Cody #include "qapi/qmp/qstring.h"
26452fcdbcSMarkus Armbruster #include "qapi/qmp/qdict.h"
27e98c6961SEric Blake #include "qapi/qmp/qjson.h"
2847e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h"
294bfb2741SKevin Wolf #include "qapi/qobject-input-visitor.h"
304bfb2741SKevin Wolf #include "qapi/qapi-visit-block-core.h"
31f27aaf4bSChristian Brunner 
32f27aaf4bSChristian Brunner /*
33f27aaf4bSChristian Brunner  * When specifying the image filename use:
34f27aaf4bSChristian Brunner  *
35fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
36f27aaf4bSChristian Brunner  *
379e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
38f27aaf4bSChristian Brunner  *
399e1fbcdeSSage Weil  * devicename is the name of the rbd image.
40f27aaf4bSChristian Brunner  *
419e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
429e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
43fab5cf59SJosh Durgin  *
449e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
459e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
469e1fbcdeSSage Weil  * (normally 'admin').
47f27aaf4bSChristian Brunner  *
489e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
499e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
509e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
519e1fbcdeSSage Weil  * file, specify conf=/dev/null.
52f27aaf4bSChristian Brunner  *
539e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
549e1fbcdeSSage Weil  * leading "\".
55f27aaf4bSChristian Brunner  */
56f27aaf4bSChristian Brunner 
57787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
58787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
59787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
60787f3133SJosh Durgin #else
61787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
62787f3133SJosh Durgin #endif
63787f3133SJosh Durgin 
64f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
65f27aaf4bSChristian Brunner 
66ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
67ad32e9c0SJosh Durgin 
681d393bdeStianqing /* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
691d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
701d393bdeStianqing #define LIBRBD_USE_IOVEC 1
711d393bdeStianqing #else
721d393bdeStianqing #define LIBRBD_USE_IOVEC 0
731d393bdeStianqing #endif
741d393bdeStianqing 
75787f3133SJosh Durgin typedef enum {
76787f3133SJosh Durgin     RBD_AIO_READ,
77787f3133SJosh Durgin     RBD_AIO_WRITE,
78dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
79dc7588c1SJosh Durgin     RBD_AIO_FLUSH
80787f3133SJosh Durgin } RBDAIOCmd;
81787f3133SJosh Durgin 
82f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
837c84b1b8SMarkus Armbruster     BlockAIOCB common;
8408448d51SStefan Priebe     int64_t ret;
85f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
86f27aaf4bSChristian Brunner     char *bounce;
87787f3133SJosh Durgin     RBDAIOCmd cmd;
88f27aaf4bSChristian Brunner     int error;
89f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
90f27aaf4bSChristian Brunner } RBDAIOCB;
91f27aaf4bSChristian Brunner 
92f27aaf4bSChristian Brunner typedef struct RADOSCB {
93f27aaf4bSChristian Brunner     RBDAIOCB *acb;
94f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
95ad32e9c0SJosh Durgin     int64_t size;
96f27aaf4bSChristian Brunner     char *buf;
9708448d51SStefan Priebe     int64_t ret;
98f27aaf4bSChristian Brunner } RADOSCB;
99f27aaf4bSChristian Brunner 
100f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
101ad32e9c0SJosh Durgin     rados_t cluster;
102ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
103ad32e9c0SJosh Durgin     rbd_image_t image;
10480b61a27SJeff Cody     char *image_name;
105ad32e9c0SJosh Durgin     char *snap;
106d24f8023SStefano Garzarella     uint64_t image_size;
107f27aaf4bSChristian Brunner } BDRVRBDState;
108f27aaf4bSChristian Brunner 
109aa045c2dSKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
110aa045c2dSKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
111aa045c2dSKevin Wolf                             const char *keypairs, const char *secretid,
112aa045c2dSKevin Wolf                             Error **errp);
113aa045c2dSKevin Wolf 
114730b00bbSMarkus Armbruster static char *qemu_rbd_next_tok(char *src, char delim, char **p)
115f27aaf4bSChristian Brunner {
116f27aaf4bSChristian Brunner     char *end;
117f27aaf4bSChristian Brunner 
118f27aaf4bSChristian Brunner     *p = NULL;
119f27aaf4bSChristian Brunner 
12016a06b24SSage Weil     for (end = src; *end; ++end) {
12116a06b24SSage Weil         if (*end == delim) {
12216a06b24SSage Weil             break;
12316a06b24SSage Weil         }
12416a06b24SSage Weil         if (*end == '\\' && end[1] != '\0') {
12516a06b24SSage Weil             end++;
12616a06b24SSage Weil         }
12716a06b24SSage Weil     }
12816a06b24SSage Weil     if (*end == delim) {
129f27aaf4bSChristian Brunner         *p = end + 1;
130f27aaf4bSChristian Brunner         *end = '\0';
131f27aaf4bSChristian Brunner     }
1327830f909SJeff Cody     return src;
133f27aaf4bSChristian Brunner }
134f27aaf4bSChristian Brunner 
13516a06b24SSage Weil static void qemu_rbd_unescape(char *src)
13616a06b24SSage Weil {
13716a06b24SSage Weil     char *p;
13816a06b24SSage Weil 
13916a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14016a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
14116a06b24SSage Weil             src++;
14216a06b24SSage Weil         }
14316a06b24SSage Weil         *p = *src;
14416a06b24SSage Weil     }
14516a06b24SSage Weil     *p = '\0';
14616a06b24SSage Weil }
14716a06b24SSage Weil 
148c7cacb3eSJeff Cody static void qemu_rbd_parse_filename(const char *filename, QDict *options,
149d61563b2SMarkus Armbruster                                     Error **errp)
150f27aaf4bSChristian Brunner {
151f27aaf4bSChristian Brunner     const char *start;
152e98c6961SEric Blake     char *p, *buf;
153e98c6961SEric Blake     QList *keypairs = NULL;
1547830f909SJeff Cody     char *found_str;
155f27aaf4bSChristian Brunner 
156f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
157d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
158c7cacb3eSJeff Cody         return;
159f27aaf4bSChristian Brunner     }
160f27aaf4bSChristian Brunner 
1617267c094SAnthony Liguori     buf = g_strdup(start);
162f27aaf4bSChristian Brunner     p = buf;
163f27aaf4bSChristian Brunner 
164730b00bbSMarkus Armbruster     found_str = qemu_rbd_next_tok(p, '/', &p);
1657830f909SJeff Cody     if (!p) {
1667830f909SJeff Cody         error_setg(errp, "Pool name is required");
1677830f909SJeff Cody         goto done;
1687830f909SJeff Cody     }
1697830f909SJeff Cody     qemu_rbd_unescape(found_str);
17046f5ac20SEric Blake     qdict_put_str(options, "pool", found_str);
171fab5cf59SJosh Durgin 
172fab5cf59SJosh Durgin     if (strchr(p, '@')) {
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 
177730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1787830f909SJeff Cody         qemu_rbd_unescape(found_str);
17946f5ac20SEric Blake         qdict_put_str(options, "snapshot", found_str);
1807830f909SJeff Cody     } else {
181730b00bbSMarkus Armbruster         found_str = qemu_rbd_next_tok(p, ':', &p);
1827830f909SJeff Cody         qemu_rbd_unescape(found_str);
18346f5ac20SEric Blake         qdict_put_str(options, "image", found_str);
1847830f909SJeff Cody     }
1857830f909SJeff Cody     if (!p) {
186f27aaf4bSChristian Brunner         goto done;
187f27aaf4bSChristian Brunner     }
188f27aaf4bSChristian Brunner 
189c7cacb3eSJeff Cody     /* The following are essentially all key/value pairs, and we treat
190c7cacb3eSJeff Cody      * 'id' and 'conf' a bit special.  Key/value pairs may be in any order. */
191c7cacb3eSJeff Cody     while (p) {
192c7cacb3eSJeff Cody         char *name, *value;
193730b00bbSMarkus Armbruster         name = qemu_rbd_next_tok(p, '=', &p);
194c7cacb3eSJeff Cody         if (!p) {
195c7cacb3eSJeff Cody             error_setg(errp, "conf option %s has no value", name);
196c7cacb3eSJeff Cody             break;
197c7cacb3eSJeff Cody         }
198c7cacb3eSJeff Cody 
199c7cacb3eSJeff Cody         qemu_rbd_unescape(name);
200c7cacb3eSJeff Cody 
201730b00bbSMarkus Armbruster         value = qemu_rbd_next_tok(p, ':', &p);
202c7cacb3eSJeff Cody         qemu_rbd_unescape(value);
203c7cacb3eSJeff Cody 
204c7cacb3eSJeff Cody         if (!strcmp(name, "conf")) {
20546f5ac20SEric Blake             qdict_put_str(options, "conf", value);
206c7cacb3eSJeff Cody         } else if (!strcmp(name, "id")) {
20746f5ac20SEric Blake             qdict_put_str(options, "user", value);
208c7cacb3eSJeff Cody         } else {
209e98c6961SEric Blake             /*
210e98c6961SEric Blake              * We pass these internally to qemu_rbd_set_keypairs(), so
211e98c6961SEric Blake              * we can get away with the simpler list of [ "key1",
212e98c6961SEric Blake              * "value1", "key2", "value2" ] rather than a raw dict
213e98c6961SEric Blake              * { "key1": "value1", "key2": "value2" } where we can't
214e98c6961SEric Blake              * guarantee order, or even a more correct but complex
215e98c6961SEric Blake              * [ { "key1": "value1" }, { "key2": "value2" } ]
216e98c6961SEric Blake              */
217e98c6961SEric Blake             if (!keypairs) {
218e98c6961SEric Blake                 keypairs = qlist_new();
219c7cacb3eSJeff Cody             }
22046f5ac20SEric Blake             qlist_append_str(keypairs, name);
22146f5ac20SEric Blake             qlist_append_str(keypairs, value);
222c7cacb3eSJeff Cody         }
223c7cacb3eSJeff Cody     }
224c7cacb3eSJeff Cody 
225e98c6961SEric Blake     if (keypairs) {
226e98c6961SEric Blake         qdict_put(options, "=keyvalue-pairs",
227e98c6961SEric Blake                   qobject_to_json(QOBJECT(keypairs)));
228c7cacb3eSJeff Cody     }
229c7cacb3eSJeff Cody 
230f27aaf4bSChristian Brunner done:
2317267c094SAnthony Liguori     g_free(buf);
232cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
233c7cacb3eSJeff Cody     return;
2347c7e9df0SSage Weil }
2357c7e9df0SSage Weil 
23660390a21SDaniel P. Berrange 
237e8e16d4bSEric Blake static void qemu_rbd_refresh_limits(BlockDriverState *bs, Error **errp)
238e8e16d4bSEric Blake {
239e8e16d4bSEric Blake     /* XXX Does RBD support AIO on less than 512-byte alignment? */
240e8e16d4bSEric Blake     bs->bl.request_alignment = 512;
241e8e16d4bSEric Blake }
242e8e16d4bSEric Blake 
243e8e16d4bSEric Blake 
244d083f954SMarkus Armbruster static int qemu_rbd_set_auth(rados_t cluster, BlockdevOptionsRbd *opts,
24560390a21SDaniel P. Berrange                              Error **errp)
24660390a21SDaniel P. Berrange {
247d083f954SMarkus Armbruster     char *key, *acr;
248a3699de4SMarkus Armbruster     int r;
249a3699de4SMarkus Armbruster     GString *accu;
250a3699de4SMarkus Armbruster     RbdAuthModeList *auth;
25160390a21SDaniel P. Berrange 
252d083f954SMarkus Armbruster     if (opts->key_secret) {
253d083f954SMarkus Armbruster         key = qcrypto_secret_lookup_as_base64(opts->key_secret, errp);
254d083f954SMarkus Armbruster         if (!key) {
255d083f954SMarkus Armbruster             return -EIO;
25660390a21SDaniel P. Berrange         }
257d083f954SMarkus Armbruster         r = rados_conf_set(cluster, "key", key);
258d083f954SMarkus Armbruster         g_free(key);
259d083f954SMarkus Armbruster         if (r < 0) {
260d083f954SMarkus Armbruster             error_setg_errno(errp, -r, "Could not set 'key'");
261d083f954SMarkus Armbruster             return r;
262d083f954SMarkus Armbruster         }
263a3699de4SMarkus Armbruster     }
264a3699de4SMarkus Armbruster 
265a3699de4SMarkus Armbruster     if (opts->has_auth_client_required) {
266a3699de4SMarkus Armbruster         accu = g_string_new("");
267a3699de4SMarkus Armbruster         for (auth = opts->auth_client_required; auth; auth = auth->next) {
268a3699de4SMarkus Armbruster             if (accu->str[0]) {
269a3699de4SMarkus Armbruster                 g_string_append_c(accu, ';');
270a3699de4SMarkus Armbruster             }
271a3699de4SMarkus Armbruster             g_string_append(accu, RbdAuthMode_str(auth->value));
272a3699de4SMarkus Armbruster         }
273a3699de4SMarkus Armbruster         acr = g_string_free(accu, FALSE);
274a3699de4SMarkus Armbruster         r = rados_conf_set(cluster, "auth_client_required", acr);
275a3699de4SMarkus Armbruster         g_free(acr);
276a3699de4SMarkus Armbruster         if (r < 0) {
277a3699de4SMarkus Armbruster             error_setg_errno(errp, -r,
278a3699de4SMarkus Armbruster                              "Could not set 'auth_client_required'");
279a3699de4SMarkus Armbruster             return r;
280a3699de4SMarkus Armbruster         }
281a3699de4SMarkus Armbruster     }
28260390a21SDaniel P. Berrange 
28360390a21SDaniel P. Berrange     return 0;
28460390a21SDaniel P. Berrange }
28560390a21SDaniel P. Berrange 
286e98c6961SEric Blake static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
287e34d8f29SJosh Durgin                                  Error **errp)
288fab5cf59SJosh Durgin {
289e98c6961SEric Blake     QList *keypairs;
290e98c6961SEric Blake     QString *name;
291e98c6961SEric Blake     QString *value;
292e98c6961SEric Blake     const char *key;
293e98c6961SEric Blake     size_t remaining;
294fab5cf59SJosh Durgin     int ret = 0;
295fab5cf59SJosh Durgin 
296e98c6961SEric Blake     if (!keypairs_json) {
297e98c6961SEric Blake         return ret;
298fab5cf59SJosh Durgin     }
2997dc847ebSMax Reitz     keypairs = qobject_to(QList,
3007dc847ebSMax Reitz                           qobject_from_json(keypairs_json, &error_abort));
301e98c6961SEric Blake     remaining = qlist_size(keypairs) / 2;
302e98c6961SEric Blake     assert(remaining);
303fab5cf59SJosh Durgin 
304e98c6961SEric Blake     while (remaining--) {
3057dc847ebSMax Reitz         name = qobject_to(QString, qlist_pop(keypairs));
3067dc847ebSMax Reitz         value = qobject_to(QString, qlist_pop(keypairs));
307e98c6961SEric Blake         assert(name && value);
308e98c6961SEric Blake         key = qstring_get_str(name);
309fab5cf59SJosh Durgin 
310e98c6961SEric Blake         ret = rados_conf_set(cluster, key, qstring_get_str(value));
311cb3e7f08SMarc-André Lureau         qobject_unref(value);
312fab5cf59SJosh Durgin         if (ret < 0) {
313e98c6961SEric Blake             error_setg_errno(errp, -ret, "invalid conf option %s", key);
314cb3e7f08SMarc-André Lureau             qobject_unref(name);
315fab5cf59SJosh Durgin             ret = -EINVAL;
316fab5cf59SJosh Durgin             break;
317fab5cf59SJosh Durgin         }
318cb3e7f08SMarc-André Lureau         qobject_unref(name);
319fab5cf59SJosh Durgin     }
320fab5cf59SJosh Durgin 
321cb3e7f08SMarc-André Lureau     qobject_unref(keypairs);
322fab5cf59SJosh Durgin     return ret;
323fab5cf59SJosh Durgin }
324fab5cf59SJosh Durgin 
3251d393bdeStianqing static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
3261d393bdeStianqing {
3271d393bdeStianqing     if (LIBRBD_USE_IOVEC) {
3281d393bdeStianqing         RBDAIOCB *acb = rcb->acb;
3291d393bdeStianqing         iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
3301d393bdeStianqing                    acb->qiov->size - offs);
3311d393bdeStianqing     } else {
3321d393bdeStianqing         memset(rcb->buf + offs, 0, rcb->size - offs);
3331d393bdeStianqing     }
3341d393bdeStianqing }
3351d393bdeStianqing 
3360f9d252dSJeff Cody static QemuOptsList runtime_opts = {
3370f9d252dSJeff Cody     .name = "rbd",
3380f9d252dSJeff Cody     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
3390f9d252dSJeff Cody     .desc = {
3400f9d252dSJeff Cody         {
3410f9d252dSJeff Cody             .name = "pool",
3420f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3430f9d252dSJeff Cody             .help = "Rados pool name",
3440f9d252dSJeff Cody         },
3450f9d252dSJeff Cody         {
3460f9d252dSJeff Cody             .name = "image",
3470f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3480f9d252dSJeff Cody             .help = "Image name in the pool",
3490f9d252dSJeff Cody         },
3500f9d252dSJeff Cody         {
351cbf036b4SMarkus Armbruster             .name = "conf",
352cbf036b4SMarkus Armbruster             .type = QEMU_OPT_STRING,
353cbf036b4SMarkus Armbruster             .help = "Rados config file location",
354cbf036b4SMarkus Armbruster         },
355cbf036b4SMarkus Armbruster         {
3560f9d252dSJeff Cody             .name = "snapshot",
3570f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3580f9d252dSJeff Cody             .help = "Ceph snapshot name",
3590f9d252dSJeff Cody         },
3600f9d252dSJeff Cody         {
3610f9d252dSJeff Cody             /* maps to 'id' in rados_create() */
3620f9d252dSJeff Cody             .name = "user",
3630f9d252dSJeff Cody             .type = QEMU_OPT_STRING,
3640f9d252dSJeff Cody             .help = "Rados id name",
3650f9d252dSJeff Cody         },
366cbf036b4SMarkus Armbruster         /*
3672836284dSMarkus Armbruster          * server.* extracted manually, see qemu_rbd_mon_host()
368cbf036b4SMarkus Armbruster          */
3690f9d252dSJeff Cody         { /* end of list */ }
3700f9d252dSJeff Cody     },
3710f9d252dSJeff Cody };
3720f9d252dSJeff Cody 
373d083f954SMarkus Armbruster /* FIXME Deprecate and remove keypairs or make it available in QMP. */
3741bebea37SKevin Wolf static int qemu_rbd_do_create(BlockdevCreateOptions *options,
3751bebea37SKevin Wolf                               const char *keypairs, const char *password_secret,
3761bebea37SKevin Wolf                               Error **errp)
3771bebea37SKevin Wolf {
3781bebea37SKevin Wolf     BlockdevCreateOptionsRbd *opts = &options->u.rbd;
3791bebea37SKevin Wolf     rados_t cluster;
3801bebea37SKevin Wolf     rados_ioctx_t io_ctx;
3811bebea37SKevin Wolf     int obj_order = 0;
3821bebea37SKevin Wolf     int ret;
3831bebea37SKevin Wolf 
3841bebea37SKevin Wolf     assert(options->driver == BLOCKDEV_DRIVER_RBD);
3851bebea37SKevin Wolf     if (opts->location->has_snapshot) {
3861bebea37SKevin Wolf         error_setg(errp, "Can't use snapshot name for image creation");
3871bebea37SKevin Wolf         return -EINVAL;
3881bebea37SKevin Wolf     }
3891bebea37SKevin Wolf 
3901bebea37SKevin Wolf     if (opts->has_cluster_size) {
3911bebea37SKevin Wolf         int64_t objsize = opts->cluster_size;
3921bebea37SKevin Wolf         if ((objsize - 1) & objsize) {    /* not a power of 2? */
3931bebea37SKevin Wolf             error_setg(errp, "obj size needs to be power of 2");
3941bebea37SKevin Wolf             return -EINVAL;
3951bebea37SKevin Wolf         }
3961bebea37SKevin Wolf         if (objsize < 4096) {
3971bebea37SKevin Wolf             error_setg(errp, "obj size too small");
3981bebea37SKevin Wolf             return -EINVAL;
3991bebea37SKevin Wolf         }
4001bebea37SKevin Wolf         obj_order = ctz32(objsize);
4011bebea37SKevin Wolf     }
4021bebea37SKevin Wolf 
403aa045c2dSKevin Wolf     ret = qemu_rbd_connect(&cluster, &io_ctx, opts->location, false, keypairs,
404aa045c2dSKevin Wolf                            password_secret, errp);
4051bebea37SKevin Wolf     if (ret < 0) {
4061bebea37SKevin Wolf         return ret;
4071bebea37SKevin Wolf     }
4081bebea37SKevin Wolf 
4091bebea37SKevin Wolf     ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
4101bebea37SKevin Wolf     if (ret < 0) {
4111bebea37SKevin Wolf         error_setg_errno(errp, -ret, "error rbd create");
412aa045c2dSKevin Wolf         goto out;
4131bebea37SKevin Wolf     }
4141bebea37SKevin Wolf 
4151bebea37SKevin Wolf     ret = 0;
416aa045c2dSKevin Wolf out:
417aa045c2dSKevin Wolf     rados_ioctx_destroy(io_ctx);
4181bebea37SKevin Wolf     rados_shutdown(cluster);
4191bebea37SKevin Wolf     return ret;
4201bebea37SKevin Wolf }
4211bebea37SKevin Wolf 
4221bebea37SKevin Wolf static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
4231bebea37SKevin Wolf {
4241bebea37SKevin Wolf     return qemu_rbd_do_create(options, NULL, NULL, errp);
4251bebea37SKevin Wolf }
4261bebea37SKevin Wolf 
427efc75e2aSStefan Hajnoczi static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
428efc75e2aSStefan Hajnoczi                                                 QemuOpts *opts,
429efc75e2aSStefan Hajnoczi                                                 Error **errp)
430f27aaf4bSChristian Brunner {
4311bebea37SKevin Wolf     BlockdevCreateOptions *create_options;
4321bebea37SKevin Wolf     BlockdevCreateOptionsRbd *rbd_opts;
4331bebea37SKevin Wolf     BlockdevOptionsRbd *loc;
434d61563b2SMarkus Armbruster     Error *local_err = NULL;
4351bebea37SKevin Wolf     const char *keypairs, *password_secret;
436c7cacb3eSJeff Cody     QDict *options = NULL;
437c7cacb3eSJeff Cody     int ret = 0;
438f27aaf4bSChristian Brunner 
4391bebea37SKevin Wolf     create_options = g_new0(BlockdevCreateOptions, 1);
4401bebea37SKevin Wolf     create_options->driver = BLOCKDEV_DRIVER_RBD;
4411bebea37SKevin Wolf     rbd_opts = &create_options->u.rbd;
4421bebea37SKevin Wolf 
4431bebea37SKevin Wolf     rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
4441bebea37SKevin Wolf 
4451bebea37SKevin Wolf     password_secret = qemu_opt_get(opts, "password-secret");
44660390a21SDaniel P. Berrange 
447f27aaf4bSChristian Brunner     /* Read out options */
4481bebea37SKevin Wolf     rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
449c2eb918eSHu Tao                               BDRV_SECTOR_SIZE);
4501bebea37SKevin Wolf     rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
4511bebea37SKevin Wolf                                                    BLOCK_OPT_CLUSTER_SIZE, 0);
4521bebea37SKevin Wolf     rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
453f27aaf4bSChristian Brunner 
454c7cacb3eSJeff Cody     options = qdict_new();
455c7cacb3eSJeff Cody     qemu_rbd_parse_filename(filename, options, &local_err);
456c7cacb3eSJeff Cody     if (local_err) {
457c7cacb3eSJeff Cody         ret = -EINVAL;
458c7cacb3eSJeff Cody         error_propagate(errp, local_err);
459c7cacb3eSJeff Cody         goto exit;
460c7cacb3eSJeff Cody     }
461c7cacb3eSJeff Cody 
462129c7d1cSMarkus Armbruster     /*
463129c7d1cSMarkus Armbruster      * Caution: while qdict_get_try_str() is fine, getting non-string
464129c7d1cSMarkus Armbruster      * types would require more care.  When @options come from -blockdev
465129c7d1cSMarkus Armbruster      * or blockdev_add, its members are typed according to the QAPI
466129c7d1cSMarkus Armbruster      * schema, but when they come from -drive, they're all QString.
467129c7d1cSMarkus Armbruster      */
4681bebea37SKevin Wolf     loc = rbd_opts->location;
4691bebea37SKevin Wolf     loc->pool     = g_strdup(qdict_get_try_str(options, "pool"));
4701bebea37SKevin Wolf     loc->conf     = g_strdup(qdict_get_try_str(options, "conf"));
4711bebea37SKevin Wolf     loc->has_conf = !!loc->conf;
4721bebea37SKevin Wolf     loc->user     = g_strdup(qdict_get_try_str(options, "user"));
4731bebea37SKevin Wolf     loc->has_user = !!loc->user;
4741bebea37SKevin Wolf     loc->image    = g_strdup(qdict_get_try_str(options, "image"));
47507846397SMarkus Armbruster     keypairs      = qdict_get_try_str(options, "=keyvalue-pairs");
476c7cacb3eSJeff Cody 
4771bebea37SKevin Wolf     ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
47887cd3d20SVikhyat Umrao     if (ret < 0) {
479c7cacb3eSJeff Cody         goto exit;
480f27aaf4bSChristian Brunner     }
481f27aaf4bSChristian Brunner 
482c7cacb3eSJeff Cody exit:
483cb3e7f08SMarc-André Lureau     qobject_unref(options);
4841bebea37SKevin Wolf     qapi_free_BlockdevCreateOptions(create_options);
485f27aaf4bSChristian Brunner     return ret;
486f27aaf4bSChristian Brunner }
487f27aaf4bSChristian Brunner 
488f27aaf4bSChristian Brunner /*
489e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
490e04fb07fSStefan Hajnoczi  * BH context.
491f27aaf4bSChristian Brunner  */
492ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
493f27aaf4bSChristian Brunner {
494f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
495f27aaf4bSChristian Brunner     int64_t r;
496f27aaf4bSChristian Brunner 
497f27aaf4bSChristian Brunner     r = rcb->ret;
498f27aaf4bSChristian Brunner 
499dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
500f27aaf4bSChristian Brunner         if (r < 0) {
501f27aaf4bSChristian Brunner             acb->ret = r;
502f27aaf4bSChristian Brunner             acb->error = 1;
503f27aaf4bSChristian Brunner         } else if (!acb->error) {
504ad32e9c0SJosh Durgin             acb->ret = rcb->size;
505f27aaf4bSChristian Brunner         }
506f27aaf4bSChristian Brunner     } else {
507ad32e9c0SJosh Durgin         if (r < 0) {
5081d393bdeStianqing             qemu_rbd_memset(rcb, 0);
509f27aaf4bSChristian Brunner             acb->ret = r;
510f27aaf4bSChristian Brunner             acb->error = 1;
511ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
5121d393bdeStianqing             qemu_rbd_memset(rcb, r);
513f27aaf4bSChristian Brunner             if (!acb->error) {
514ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
515f27aaf4bSChristian Brunner             }
516f27aaf4bSChristian Brunner         } else if (!acb->error) {
517ad32e9c0SJosh Durgin             acb->ret = r;
518f27aaf4bSChristian Brunner         }
519f27aaf4bSChristian Brunner     }
520e04fb07fSStefan Hajnoczi 
5217267c094SAnthony Liguori     g_free(rcb);
522e04fb07fSStefan Hajnoczi 
5231d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
524e04fb07fSStefan Hajnoczi         if (acb->cmd == RBD_AIO_READ) {
525e04fb07fSStefan Hajnoczi             qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
526f27aaf4bSChristian Brunner         }
527e04fb07fSStefan Hajnoczi         qemu_vfree(acb->bounce);
5281d393bdeStianqing     }
5291d393bdeStianqing 
530e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
531f27aaf4bSChristian Brunner 
5328007429aSFam Zheng     qemu_aio_unref(acb);
533f27aaf4bSChristian Brunner }
534f27aaf4bSChristian Brunner 
5354bfb2741SKevin Wolf static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
5360a55679bSJeff Cody {
5374bfb2741SKevin Wolf     const char **vals;
5382836284dSMarkus Armbruster     const char *host, *port;
5392836284dSMarkus Armbruster     char *rados_str;
5404bfb2741SKevin Wolf     InetSocketAddressBaseList *p;
5414bfb2741SKevin Wolf     int i, cnt;
5420a55679bSJeff Cody 
5434bfb2741SKevin Wolf     if (!opts->has_server) {
5444bfb2741SKevin Wolf         return NULL;
5450a55679bSJeff Cody     }
5464bfb2741SKevin Wolf 
5474bfb2741SKevin Wolf     for (cnt = 0, p = opts->server; p; p = p->next) {
5484bfb2741SKevin Wolf         cnt++;
5490a55679bSJeff Cody     }
5500a55679bSJeff Cody 
5514bfb2741SKevin Wolf     vals = g_new(const char *, cnt + 1);
5524bfb2741SKevin Wolf 
5534bfb2741SKevin Wolf     for (i = 0, p = opts->server; p; p = p->next, i++) {
5544bfb2741SKevin Wolf         host = p->value->host;
5554bfb2741SKevin Wolf         port = p->value->port;
5564bfb2741SKevin Wolf 
5570a55679bSJeff Cody         if (strchr(host, ':')) {
5584bfb2741SKevin Wolf             vals[i] = g_strdup_printf("[%s]:%s", host, port);
5590a55679bSJeff Cody         } else {
5604bfb2741SKevin Wolf             vals[i] = g_strdup_printf("%s:%s", host, port);
5610a55679bSJeff Cody         }
5620a55679bSJeff Cody     }
5632836284dSMarkus Armbruster     vals[i] = NULL;
5640a55679bSJeff Cody 
5652836284dSMarkus Armbruster     rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
5662836284dSMarkus Armbruster     g_strfreev((char **)vals);
5670a55679bSJeff Cody     return rados_str;
5680a55679bSJeff Cody }
5690a55679bSJeff Cody 
5703d9136f9SKevin Wolf static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
5714bfb2741SKevin Wolf                             BlockdevOptionsRbd *opts, bool cache,
5724ff45049SKevin Wolf                             const char *keypairs, const char *secretid,
5734ff45049SKevin Wolf                             Error **errp)
574f27aaf4bSChristian Brunner {
5750a55679bSJeff Cody     char *mon_host = NULL;
5763d9136f9SKevin Wolf     Error *local_err = NULL;
577f27aaf4bSChristian Brunner     int r;
578f27aaf4bSChristian Brunner 
579d083f954SMarkus Armbruster     if (secretid) {
580d083f954SMarkus Armbruster         if (opts->key_secret) {
581d083f954SMarkus Armbruster             error_setg(errp,
582d083f954SMarkus Armbruster                        "Legacy 'password-secret' clashes with 'key-secret'");
583d083f954SMarkus Armbruster             return -EINVAL;
584d083f954SMarkus Armbruster         }
585d083f954SMarkus Armbruster         opts->key_secret = g_strdup(secretid);
586d083f954SMarkus Armbruster         opts->has_key_secret = true;
587d083f954SMarkus Armbruster     }
588d083f954SMarkus Armbruster 
5894bfb2741SKevin Wolf     mon_host = qemu_rbd_mon_host(opts, &local_err);
59084d18f06SMarkus Armbruster     if (local_err) {
591d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
5922836284dSMarkus Armbruster         r = -EINVAL;
5932836284dSMarkus Armbruster         goto failed_opts;
594a9ccedc3SKevin Wolf     }
595a9ccedc3SKevin Wolf 
5964bfb2741SKevin Wolf     r = rados_create(cluster, opts->user);
597ad32e9c0SJosh Durgin     if (r < 0) {
59887cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
599c3ca988dSKevin Wolf         goto failed_opts;
600f27aaf4bSChristian Brunner     }
601f27aaf4bSChristian Brunner 
602c7cacb3eSJeff Cody     /* try default location when conf=NULL, but ignore failure */
6034bfb2741SKevin Wolf     r = rados_conf_read_file(*cluster, opts->conf);
6044bfb2741SKevin Wolf     if (opts->has_conf && r < 0) {
6054bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
606e34d8f29SJosh Durgin         goto failed_shutdown;
607e34d8f29SJosh Durgin     }
60899a3c89dSJosh Durgin 
6093d9136f9SKevin Wolf     r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
61099a3c89dSJosh Durgin     if (r < 0) {
61199a3c89dSJosh Durgin         goto failed_shutdown;
61299a3c89dSJosh Durgin     }
61399a3c89dSJosh Durgin 
6140a55679bSJeff Cody     if (mon_host) {
6153d9136f9SKevin Wolf         r = rados_conf_set(*cluster, "mon_host", mon_host);
6160a55679bSJeff Cody         if (r < 0) {
6170a55679bSJeff Cody             goto failed_shutdown;
6180a55679bSJeff Cody         }
6190a55679bSJeff Cody     }
6200a55679bSJeff Cody 
621d083f954SMarkus Armbruster     r = qemu_rbd_set_auth(*cluster, opts, errp);
622d083f954SMarkus Armbruster     if (r < 0) {
62360390a21SDaniel P. Berrange         goto failed_shutdown;
62460390a21SDaniel P. Berrange     }
62560390a21SDaniel P. Berrange 
626b11f38fcSJosh Durgin     /*
627b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
628b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
629b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
630b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
631b11f38fcSJosh Durgin      * be set up, fall back to no caching.
632b11f38fcSJosh Durgin      */
6333d9136f9SKevin Wolf     if (cache) {
6343d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "true");
635b11f38fcSJosh Durgin     } else {
6363d9136f9SKevin Wolf         rados_conf_set(*cluster, "rbd_cache", "false");
637b11f38fcSJosh Durgin     }
638b11f38fcSJosh Durgin 
6393d9136f9SKevin Wolf     r = rados_connect(*cluster);
640ad32e9c0SJosh Durgin     if (r < 0) {
64187cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
642eb93d5d9SSage Weil         goto failed_shutdown;
643ad32e9c0SJosh Durgin     }
644ad32e9c0SJosh Durgin 
6454bfb2741SKevin Wolf     r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
646ad32e9c0SJosh Durgin     if (r < 0) {
6474bfb2741SKevin Wolf         error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
648eb93d5d9SSage Weil         goto failed_shutdown;
649ad32e9c0SJosh Durgin     }
650ad32e9c0SJosh Durgin 
6513d9136f9SKevin Wolf     return 0;
6523d9136f9SKevin Wolf 
6533d9136f9SKevin Wolf failed_shutdown:
6543d9136f9SKevin Wolf     rados_shutdown(*cluster);
6553d9136f9SKevin Wolf failed_opts:
6563d9136f9SKevin Wolf     g_free(mon_host);
6573d9136f9SKevin Wolf     return r;
6583d9136f9SKevin Wolf }
6593d9136f9SKevin Wolf 
660f24b03b5SJeff Cody static int qemu_rbd_convert_options(QDict *options, BlockdevOptionsRbd **opts,
661f24b03b5SJeff Cody                                     Error **errp)
662f24b03b5SJeff Cody {
663f24b03b5SJeff Cody     Visitor *v;
664f24b03b5SJeff Cody     Error *local_err = NULL;
665f24b03b5SJeff Cody 
666f24b03b5SJeff Cody     /* Convert the remaining options into a QAPI object */
667f24b03b5SJeff Cody     v = qobject_input_visitor_new_flat_confused(options, errp);
668f24b03b5SJeff Cody     if (!v) {
669f24b03b5SJeff Cody         return -EINVAL;
670f24b03b5SJeff Cody     }
671f24b03b5SJeff Cody 
672f24b03b5SJeff Cody     visit_type_BlockdevOptionsRbd(v, NULL, opts, &local_err);
673f24b03b5SJeff Cody     visit_free(v);
674f24b03b5SJeff Cody 
675f24b03b5SJeff Cody     if (local_err) {
676f24b03b5SJeff Cody         error_propagate(errp, local_err);
677f24b03b5SJeff Cody         return -EINVAL;
678f24b03b5SJeff Cody     }
679f24b03b5SJeff Cody 
680f24b03b5SJeff Cody     return 0;
681f24b03b5SJeff Cody }
682f24b03b5SJeff Cody 
683084d1d13SJeff Cody static int qemu_rbd_attempt_legacy_options(QDict *options,
684084d1d13SJeff Cody                                            BlockdevOptionsRbd **opts,
685084d1d13SJeff Cody                                            char **keypairs)
686084d1d13SJeff Cody {
687084d1d13SJeff Cody     char *filename;
688084d1d13SJeff Cody     int r;
689084d1d13SJeff Cody 
690084d1d13SJeff Cody     filename = g_strdup(qdict_get_try_str(options, "filename"));
691084d1d13SJeff Cody     if (!filename) {
692084d1d13SJeff Cody         return -EINVAL;
693084d1d13SJeff Cody     }
694084d1d13SJeff Cody     qdict_del(options, "filename");
695084d1d13SJeff Cody 
696084d1d13SJeff Cody     qemu_rbd_parse_filename(filename, options, NULL);
697084d1d13SJeff Cody 
698084d1d13SJeff Cody     /* keypairs freed by caller */
699084d1d13SJeff Cody     *keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
700084d1d13SJeff Cody     if (*keypairs) {
701084d1d13SJeff Cody         qdict_del(options, "=keyvalue-pairs");
702084d1d13SJeff Cody     }
703084d1d13SJeff Cody 
704084d1d13SJeff Cody     r = qemu_rbd_convert_options(options, opts, NULL);
705084d1d13SJeff Cody 
706084d1d13SJeff Cody     g_free(filename);
707084d1d13SJeff Cody     return r;
708084d1d13SJeff Cody }
709084d1d13SJeff Cody 
7103d9136f9SKevin Wolf static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
7113d9136f9SKevin Wolf                          Error **errp)
7123d9136f9SKevin Wolf {
7133d9136f9SKevin Wolf     BDRVRBDState *s = bs->opaque;
7144bfb2741SKevin Wolf     BlockdevOptionsRbd *opts = NULL;
715bfb15b4bSJeff Cody     const QDictEntry *e;
7163d9136f9SKevin Wolf     Error *local_err = NULL;
7174ff45049SKevin Wolf     char *keypairs, *secretid;
7183d9136f9SKevin Wolf     int r;
7193d9136f9SKevin Wolf 
7204ff45049SKevin Wolf     keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
7214ff45049SKevin Wolf     if (keypairs) {
7224ff45049SKevin Wolf         qdict_del(options, "=keyvalue-pairs");
7234ff45049SKevin Wolf     }
7244ff45049SKevin Wolf 
7254ff45049SKevin Wolf     secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
7264ff45049SKevin Wolf     if (secretid) {
7274ff45049SKevin Wolf         qdict_del(options, "password-secret");
7284ff45049SKevin Wolf     }
7294ff45049SKevin Wolf 
730f24b03b5SJeff Cody     r = qemu_rbd_convert_options(options, &opts, &local_err);
7314bfb2741SKevin Wolf     if (local_err) {
732084d1d13SJeff Cody         /* If keypairs are present, that means some options are present in
733084d1d13SJeff Cody          * the modern option format.  Don't attempt to parse legacy option
734084d1d13SJeff Cody          * formats, as we won't support mixed usage. */
735084d1d13SJeff Cody         if (keypairs) {
7364bfb2741SKevin Wolf             error_propagate(errp, local_err);
7374bfb2741SKevin Wolf             goto out;
7384bfb2741SKevin Wolf         }
7394bfb2741SKevin Wolf 
740084d1d13SJeff Cody         /* If the initial attempt to convert and process the options failed,
741084d1d13SJeff Cody          * we may be attempting to open an image file that has the rbd options
742084d1d13SJeff Cody          * specified in the older format consisting of all key/value pairs
743084d1d13SJeff Cody          * encoded in the filename.  Go ahead and attempt to parse the
744084d1d13SJeff Cody          * filename, and see if we can pull out the required options. */
745084d1d13SJeff Cody         r = qemu_rbd_attempt_legacy_options(options, &opts, &keypairs);
746084d1d13SJeff Cody         if (r < 0) {
747084d1d13SJeff Cody             /* Propagate the original error, not the legacy parsing fallback
748084d1d13SJeff Cody              * error, as the latter was just a best-effort attempt. */
749084d1d13SJeff Cody             error_propagate(errp, local_err);
750084d1d13SJeff Cody             goto out;
751084d1d13SJeff Cody         }
752084d1d13SJeff Cody         /* Take care whenever deciding to actually deprecate; once this ability
753084d1d13SJeff Cody          * is removed, we will not be able to open any images with legacy-styled
754084d1d13SJeff Cody          * backing image strings. */
7555197f445SMarkus Armbruster         warn_report("RBD options encoded in the filename as keyvalue pairs "
756084d1d13SJeff Cody                     "is deprecated");
757084d1d13SJeff Cody     }
758084d1d13SJeff Cody 
759bfb15b4bSJeff Cody     /* Remove the processed options from the QDict (the visitor processes
760bfb15b4bSJeff Cody      * _all_ options in the QDict) */
761bfb15b4bSJeff Cody     while ((e = qdict_first(options))) {
762bfb15b4bSJeff Cody         qdict_del(options, e->key);
763bfb15b4bSJeff Cody     }
764bfb15b4bSJeff Cody 
765d41a5588SKevin Wolf     r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
766d41a5588SKevin Wolf                          !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
7673d9136f9SKevin Wolf     if (r < 0) {
7684ff45049SKevin Wolf         goto out;
7693d9136f9SKevin Wolf     }
7703d9136f9SKevin Wolf 
771d41a5588SKevin Wolf     s->snap = g_strdup(opts->snapshot);
772d41a5588SKevin Wolf     s->image_name = g_strdup(opts->image);
773d41a5588SKevin Wolf 
774e2b8247aSJeff Cody     /* rbd_open is always r/w */
77580b61a27SJeff Cody     r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
776ad32e9c0SJosh Durgin     if (r < 0) {
77780b61a27SJeff Cody         error_setg_errno(errp, -r, "error reading header from %s",
77880b61a27SJeff Cody                          s->image_name);
779eb93d5d9SSage Weil         goto failed_open;
780ad32e9c0SJosh Durgin     }
781ad32e9c0SJosh Durgin 
782d24f8023SStefano Garzarella     r = rbd_get_size(s->image, &s->image_size);
783d24f8023SStefano Garzarella     if (r < 0) {
784d24f8023SStefano Garzarella         error_setg_errno(errp, -r, "error getting image size from %s",
785d24f8023SStefano Garzarella                          s->image_name);
786d24f8023SStefano Garzarella         rbd_close(s->image);
787d24f8023SStefano Garzarella         goto failed_open;
788d24f8023SStefano Garzarella     }
789d24f8023SStefano Garzarella 
790e2b8247aSJeff Cody     /* If we are using an rbd snapshot, we must be r/o, otherwise
791e2b8247aSJeff Cody      * leave as-is */
792e2b8247aSJeff Cody     if (s->snap != NULL) {
793eaa2410fSKevin Wolf         r = bdrv_apply_auto_read_only(bs, "rbd snapshots are read-only", errp);
794e2b8247aSJeff Cody         if (r < 0) {
795a51b9c48SKevin Wolf             rbd_close(s->image);
796e2b8247aSJeff Cody             goto failed_open;
797e2b8247aSJeff Cody         }
798e2b8247aSJeff Cody     }
799f27aaf4bSChristian Brunner 
8004ff45049SKevin Wolf     r = 0;
8014ff45049SKevin Wolf     goto out;
802f27aaf4bSChristian Brunner 
803eb93d5d9SSage Weil failed_open:
804ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
805eb93d5d9SSage Weil     g_free(s->snap);
80680b61a27SJeff Cody     g_free(s->image_name);
8073d9136f9SKevin Wolf     rados_shutdown(s->cluster);
8084ff45049SKevin Wolf out:
8094bfb2741SKevin Wolf     qapi_free_BlockdevOptionsRbd(opts);
8104ff45049SKevin Wolf     g_free(keypairs);
8114ff45049SKevin Wolf     g_free(secretid);
812f27aaf4bSChristian Brunner     return r;
813f27aaf4bSChristian Brunner }
814f27aaf4bSChristian Brunner 
81556e7cf8dSJeff Cody 
81656e7cf8dSJeff Cody /* Since RBD is currently always opened R/W via the API,
81756e7cf8dSJeff Cody  * we just need to check if we are using a snapshot or not, in
81856e7cf8dSJeff Cody  * order to determine if we will allow it to be R/W */
81956e7cf8dSJeff Cody static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
82056e7cf8dSJeff Cody                                    BlockReopenQueue *queue, Error **errp)
82156e7cf8dSJeff Cody {
82256e7cf8dSJeff Cody     BDRVRBDState *s = state->bs->opaque;
82356e7cf8dSJeff Cody     int ret = 0;
82456e7cf8dSJeff Cody 
82556e7cf8dSJeff Cody     if (s->snap && state->flags & BDRV_O_RDWR) {
82656e7cf8dSJeff Cody         error_setg(errp,
82756e7cf8dSJeff Cody                    "Cannot change node '%s' to r/w when using RBD snapshot",
82856e7cf8dSJeff Cody                    bdrv_get_device_or_node_name(state->bs));
82956e7cf8dSJeff Cody         ret = -EINVAL;
83056e7cf8dSJeff Cody     }
83156e7cf8dSJeff Cody 
83256e7cf8dSJeff Cody     return ret;
83356e7cf8dSJeff Cody }
83456e7cf8dSJeff Cody 
835ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
836f27aaf4bSChristian Brunner {
837f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
838f27aaf4bSChristian Brunner 
839ad32e9c0SJosh Durgin     rbd_close(s->image);
840ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
8417267c094SAnthony Liguori     g_free(s->snap);
84280b61a27SJeff Cody     g_free(s->image_name);
843ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
844f27aaf4bSChristian Brunner }
845f27aaf4bSChristian Brunner 
846d24f8023SStefano Garzarella /* Resize the RBD image and update the 'image_size' with the current size */
847d24f8023SStefano Garzarella static int qemu_rbd_resize(BlockDriverState *bs, uint64_t size)
848d24f8023SStefano Garzarella {
849d24f8023SStefano Garzarella     BDRVRBDState *s = bs->opaque;
850d24f8023SStefano Garzarella     int r;
851d24f8023SStefano Garzarella 
852d24f8023SStefano Garzarella     r = rbd_resize(s->image, size);
853d24f8023SStefano Garzarella     if (r < 0) {
854d24f8023SStefano Garzarella         return r;
855d24f8023SStefano Garzarella     }
856d24f8023SStefano Garzarella 
857d24f8023SStefano Garzarella     s->image_size = size;
858d24f8023SStefano Garzarella 
859d24f8023SStefano Garzarella     return 0;
860d24f8023SStefano Garzarella }
861d24f8023SStefano Garzarella 
862d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
863f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
864f27aaf4bSChristian Brunner };
865f27aaf4bSChristian Brunner 
866e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
867f27aaf4bSChristian Brunner {
868e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
869e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
870ad32e9c0SJosh Durgin }
871ad32e9c0SJosh Durgin 
872ad32e9c0SJosh Durgin /*
873ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
874ad32e9c0SJosh Durgin  *
875ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
876ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
877e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
878e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
879ad32e9c0SJosh Durgin  */
880ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
881ad32e9c0SJosh Durgin {
882e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
883e04fb07fSStefan Hajnoczi 
884ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
885ad32e9c0SJosh Durgin     rbd_aio_release(c);
886f27aaf4bSChristian Brunner 
887fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
888ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
889473c7f02SStefan Priebe }
890f27aaf4bSChristian Brunner 
891787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
892787f3133SJosh Durgin                                    uint64_t off,
893787f3133SJosh Durgin                                    uint64_t len,
894787f3133SJosh Durgin                                    rbd_completion_t comp)
895787f3133SJosh Durgin {
896787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
897787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
898787f3133SJosh Durgin #else
899787f3133SJosh Durgin     return -ENOTSUP;
900787f3133SJosh Durgin #endif
901787f3133SJosh Durgin }
902787f3133SJosh Durgin 
903dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
904dc7588c1SJosh Durgin                                  rbd_completion_t comp)
905dc7588c1SJosh Durgin {
906dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
907dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
908dc7588c1SJosh Durgin #else
909dc7588c1SJosh Durgin     return -ENOTSUP;
910dc7588c1SJosh Durgin #endif
911dc7588c1SJosh Durgin }
912dc7588c1SJosh Durgin 
9137c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
9147bbca9e2SEric Blake                                  int64_t off,
915f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
9167bbca9e2SEric Blake                                  int64_t size,
917097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
918787f3133SJosh Durgin                                  void *opaque,
919787f3133SJosh Durgin                                  RBDAIOCmd cmd)
920f27aaf4bSChristian Brunner {
921f27aaf4bSChristian Brunner     RBDAIOCB *acb;
9220f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
923ad32e9c0SJosh Durgin     rbd_completion_t c;
92451a13528SJosh Durgin     int r;
925f27aaf4bSChristian Brunner 
926f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
927f27aaf4bSChristian Brunner 
928d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
929787f3133SJosh Durgin     acb->cmd = cmd;
930f27aaf4bSChristian Brunner     acb->qiov = qiov;
9317bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
9321d393bdeStianqing 
9331d393bdeStianqing     rcb = g_new(RADOSCB, 1);
9341d393bdeStianqing 
9351d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
936dc7588c1SJosh Durgin         if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
937787f3133SJosh Durgin             acb->bounce = NULL;
938787f3133SJosh Durgin         } else {
9390f7a0237SKevin Wolf             acb->bounce = qemu_try_blockalign(bs, qiov->size);
9400f7a0237SKevin Wolf             if (acb->bounce == NULL) {
9410f7a0237SKevin Wolf                 goto failed;
9420f7a0237SKevin Wolf             }
943787f3133SJosh Durgin         }
9441d393bdeStianqing         if (cmd == RBD_AIO_WRITE) {
9451d393bdeStianqing             qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
9461d393bdeStianqing         }
9471d393bdeStianqing         rcb->buf = acb->bounce;
9481d393bdeStianqing     }
9491d393bdeStianqing 
950f27aaf4bSChristian Brunner     acb->ret = 0;
951f27aaf4bSChristian Brunner     acb->error = 0;
952f27aaf4bSChristian Brunner     acb->s = s;
953f27aaf4bSChristian Brunner 
954f27aaf4bSChristian Brunner     rcb->acb = acb;
955f27aaf4bSChristian Brunner     rcb->s = acb->s;
956ad32e9c0SJosh Durgin     rcb->size = size;
95751a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
95851a13528SJosh Durgin     if (r < 0) {
95951a13528SJosh Durgin         goto failed;
96051a13528SJosh Durgin     }
961f27aaf4bSChristian Brunner 
962787f3133SJosh Durgin     switch (cmd) {
963d24f8023SStefano Garzarella     case RBD_AIO_WRITE: {
964d24f8023SStefano Garzarella         /*
965d24f8023SStefano Garzarella          * RBD APIs don't allow us to write more than actual size, so in order
966d24f8023SStefano Garzarella          * to support growing images, we resize the image before write
967d24f8023SStefano Garzarella          * operations that exceed the current size.
968d24f8023SStefano Garzarella          */
969d24f8023SStefano Garzarella         if (off + size > s->image_size) {
970d24f8023SStefano Garzarella             r = qemu_rbd_resize(bs, off + size);
971d24f8023SStefano Garzarella             if (r < 0) {
972d24f8023SStefano Garzarella                 goto failed_completion;
973d24f8023SStefano Garzarella             }
974d24f8023SStefano Garzarella         }
9751d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9761d393bdeStianqing             r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
9771d393bdeStianqing #else
9781d393bdeStianqing             r = rbd_aio_write(s->image, off, size, rcb->buf, c);
9791d393bdeStianqing #endif
980787f3133SJosh Durgin         break;
981d24f8023SStefano Garzarella     }
982787f3133SJosh Durgin     case RBD_AIO_READ:
9831d393bdeStianqing #ifdef LIBRBD_SUPPORTS_IOVEC
9841d393bdeStianqing             r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
9851d393bdeStianqing #else
9861d393bdeStianqing             r = rbd_aio_read(s->image, off, size, rcb->buf, c);
9871d393bdeStianqing #endif
988787f3133SJosh Durgin         break;
989787f3133SJosh Durgin     case RBD_AIO_DISCARD:
990787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
991787f3133SJosh Durgin         break;
992dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
993dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
994dc7588c1SJosh Durgin         break;
995787f3133SJosh Durgin     default:
996787f3133SJosh Durgin         r = -EINVAL;
99751a13528SJosh Durgin     }
99851a13528SJosh Durgin 
99951a13528SJosh Durgin     if (r < 0) {
1000405a2764SKevin Wolf         goto failed_completion;
1001f27aaf4bSChristian Brunner     }
1002f27aaf4bSChristian Brunner     return &acb->common;
100351a13528SJosh Durgin 
1004405a2764SKevin Wolf failed_completion:
1005405a2764SKevin Wolf     rbd_aio_release(c);
100651a13528SJosh Durgin failed:
10077267c094SAnthony Liguori     g_free(rcb);
10081d393bdeStianqing     if (!LIBRBD_USE_IOVEC) {
1009405a2764SKevin Wolf         qemu_vfree(acb->bounce);
10101d393bdeStianqing     }
10111d393bdeStianqing 
10128007429aSFam Zheng     qemu_aio_unref(acb);
101351a13528SJosh Durgin     return NULL;
1014f27aaf4bSChristian Brunner }
1015f27aaf4bSChristian Brunner 
1016e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
1017e8e16d4bSEric Blake                                        uint64_t offset, uint64_t bytes,
1018e8e16d4bSEric Blake                                        QEMUIOVector *qiov, int flags,
1019097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
1020f27aaf4bSChristian Brunner                                        void *opaque)
1021f27aaf4bSChristian Brunner {
1022e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1023787f3133SJosh Durgin                          RBD_AIO_READ);
1024f27aaf4bSChristian Brunner }
1025f27aaf4bSChristian Brunner 
1026e8e16d4bSEric Blake static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
1027e8e16d4bSEric Blake                                         uint64_t offset, uint64_t bytes,
1028e8e16d4bSEric Blake                                         QEMUIOVector *qiov, int flags,
1029097310b5SMarkus Armbruster                                         BlockCompletionFunc *cb,
1030f27aaf4bSChristian Brunner                                         void *opaque)
1031f27aaf4bSChristian Brunner {
1032e8e16d4bSEric Blake     return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
1033787f3133SJosh Durgin                          RBD_AIO_WRITE);
1034f27aaf4bSChristian Brunner }
1035f27aaf4bSChristian Brunner 
1036dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
10377c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
1038097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
1039dc7588c1SJosh Durgin                                       void *opaque)
1040dc7588c1SJosh Durgin {
1041dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
1042dc7588c1SJosh Durgin }
1043dc7588c1SJosh Durgin 
1044dc7588c1SJosh Durgin #else
1045dc7588c1SJosh Durgin 
10468b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
10477a3f5fe9SSage Weil {
10487a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
10497a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
10507a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
10517a3f5fe9SSage Weil     return rbd_flush(s->image);
10527a3f5fe9SSage Weil #else
10537a3f5fe9SSage Weil     return 0;
10547a3f5fe9SSage Weil #endif
10557a3f5fe9SSage Weil }
1056dc7588c1SJosh Durgin #endif
10577a3f5fe9SSage Weil 
1058ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
1059f27aaf4bSChristian Brunner {
1060f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1061ad32e9c0SJosh Durgin     rbd_image_info_t info;
1062ad32e9c0SJosh Durgin     int r;
1063ad32e9c0SJosh Durgin 
1064ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1065ad32e9c0SJosh Durgin     if (r < 0) {
1066ad32e9c0SJosh Durgin         return r;
1067ad32e9c0SJosh Durgin     }
1068ad32e9c0SJosh Durgin 
1069ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
1070f27aaf4bSChristian Brunner     return 0;
1071f27aaf4bSChristian Brunner }
1072f27aaf4bSChristian Brunner 
1073ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
1074f27aaf4bSChristian Brunner {
1075f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1076ad32e9c0SJosh Durgin     rbd_image_info_t info;
1077ad32e9c0SJosh Durgin     int r;
1078f27aaf4bSChristian Brunner 
1079ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
1080ad32e9c0SJosh Durgin     if (r < 0) {
1081ad32e9c0SJosh Durgin         return r;
1082f27aaf4bSChristian Brunner     }
1083f27aaf4bSChristian Brunner 
1084ad32e9c0SJosh Durgin     return info.size;
1085ad32e9c0SJosh Durgin }
1086ad32e9c0SJosh Durgin 
1087061ca8a3SKevin Wolf static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
1088061ca8a3SKevin Wolf                                              int64_t offset,
1089061ca8a3SKevin Wolf                                              PreallocMode prealloc,
1090061ca8a3SKevin Wolf                                              Error **errp)
109130cdc48cSJosh Durgin {
109230cdc48cSJosh Durgin     int r;
109330cdc48cSJosh Durgin 
10948243ccb7SMax Reitz     if (prealloc != PREALLOC_MODE_OFF) {
10958243ccb7SMax Reitz         error_setg(errp, "Unsupported preallocation mode '%s'",
1096977c736fSMarkus Armbruster                    PreallocMode_str(prealloc));
10978243ccb7SMax Reitz         return -ENOTSUP;
10988243ccb7SMax Reitz     }
10998243ccb7SMax Reitz 
1100d24f8023SStefano Garzarella     r = qemu_rbd_resize(bs, offset);
110130cdc48cSJosh Durgin     if (r < 0) {
1102f59adb32SMax Reitz         error_setg_errno(errp, -r, "Failed to resize file");
110330cdc48cSJosh Durgin         return r;
110430cdc48cSJosh Durgin     }
110530cdc48cSJosh Durgin 
110630cdc48cSJosh Durgin     return 0;
110730cdc48cSJosh Durgin }
110830cdc48cSJosh Durgin 
1109ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
1110ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
1111f27aaf4bSChristian Brunner {
1112f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1113f27aaf4bSChristian Brunner     int r;
1114f27aaf4bSChristian Brunner 
1115f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
1116f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
1117f27aaf4bSChristian Brunner     }
1118f27aaf4bSChristian Brunner 
1119f27aaf4bSChristian Brunner     /*
1120f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
1121f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
1122f27aaf4bSChristian Brunner      */
1123f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
1124f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
1125f27aaf4bSChristian Brunner         return -EINVAL;
1126f27aaf4bSChristian Brunner     }
1127f27aaf4bSChristian Brunner 
1128f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1129f27aaf4bSChristian Brunner         return -ERANGE;
1130f27aaf4bSChristian Brunner     }
1131f27aaf4bSChristian Brunner 
1132ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
1133f27aaf4bSChristian Brunner     if (r < 0) {
1134ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
1135f27aaf4bSChristian Brunner         return r;
1136f27aaf4bSChristian Brunner     }
1137f27aaf4bSChristian Brunner 
1138f27aaf4bSChristian Brunner     return 0;
1139f27aaf4bSChristian Brunner }
1140f27aaf4bSChristian Brunner 
1141bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
1142a89d89d3SWenchao Xia                                 const char *snapshot_id,
1143a89d89d3SWenchao Xia                                 const char *snapshot_name,
1144a89d89d3SWenchao Xia                                 Error **errp)
1145bd603247SGregory Farnum {
1146bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1147bd603247SGregory Farnum     int r;
1148bd603247SGregory Farnum 
1149a89d89d3SWenchao Xia     if (!snapshot_name) {
1150a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
1151a89d89d3SWenchao Xia         return -EINVAL;
1152a89d89d3SWenchao Xia     }
1153a89d89d3SWenchao Xia 
1154a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
1155a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
1156a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1157a89d89d3SWenchao Xia         error_setg(errp,
1158a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
1159a89d89d3SWenchao Xia                    "equal to snapshot name");
1160a89d89d3SWenchao Xia         return -EINVAL;
1161a89d89d3SWenchao Xia     }
1162a89d89d3SWenchao Xia 
1163bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
1164a89d89d3SWenchao Xia     if (r < 0) {
1165a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
1166a89d89d3SWenchao Xia     }
1167bd603247SGregory Farnum     return r;
1168bd603247SGregory Farnum }
1169bd603247SGregory Farnum 
1170bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1171bd603247SGregory Farnum                                   const char *snapshot_name)
1172bd603247SGregory Farnum {
1173bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
1174bd603247SGregory Farnum 
11759be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
1176bd603247SGregory Farnum }
1177bd603247SGregory Farnum 
1178ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
1179ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
1180f27aaf4bSChristian Brunner {
1181f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
1182f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
1183ad32e9c0SJosh Durgin     int i, snap_count;
1184ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
1185ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
1186f27aaf4bSChristian Brunner 
1187ad32e9c0SJosh Durgin     do {
118802c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
1189ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
11909e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
11917267c094SAnthony Liguori             g_free(snaps);
1192f27aaf4bSChristian Brunner         }
1193ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
1194f27aaf4bSChristian Brunner 
1195ad32e9c0SJosh Durgin     if (snap_count <= 0) {
1196b9c53290SJosh Durgin         goto done;
1197f27aaf4bSChristian Brunner     }
1198f27aaf4bSChristian Brunner 
11995839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
1200f27aaf4bSChristian Brunner 
1201ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
1202ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
1203f27aaf4bSChristian Brunner 
1204f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
1205f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1206f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
1207f27aaf4bSChristian Brunner 
1208ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
1209f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
1210f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
1211f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
1212f27aaf4bSChristian Brunner     }
1213ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
12149e6337d0SStefan Hajnoczi     g_free(snaps);
1215ad32e9c0SJosh Durgin 
1216b9c53290SJosh Durgin  done:
1217f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
1218f27aaf4bSChristian Brunner     return snap_count;
1219f27aaf4bSChristian Brunner }
1220f27aaf4bSChristian Brunner 
1221787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
12224da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
12234da444a0SEric Blake                                          int64_t offset,
1224f5a5ca79SManos Pitsidianakis                                          int bytes,
1225097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
1226787f3133SJosh Durgin                                          void *opaque)
1227787f3133SJosh Durgin {
1228f5a5ca79SManos Pitsidianakis     return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
1229787f3133SJosh Durgin                          RBD_AIO_DISCARD);
1230787f3133SJosh Durgin }
1231787f3133SJosh Durgin #endif
1232787f3133SJosh Durgin 
1233be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
12342b148f39SPaolo Bonzini static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1235be217884SAdam Crume                                                       Error **errp)
1236be217884SAdam Crume {
1237be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
1238be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
1239be217884SAdam Crume     if (r < 0) {
1240be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
1241be217884SAdam Crume     }
1242be217884SAdam Crume }
1243be217884SAdam Crume #endif
1244be217884SAdam Crume 
1245bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
1246bd0cf596SChunyan Liu     .name = "rbd-create-opts",
1247bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1248bd0cf596SChunyan Liu     .desc = {
1249f27aaf4bSChristian Brunner         {
1250f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
1251bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1252f27aaf4bSChristian Brunner             .help = "Virtual disk size"
1253f27aaf4bSChristian Brunner         },
1254f27aaf4bSChristian Brunner         {
1255f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
1256bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
1257f27aaf4bSChristian Brunner             .help = "RBD object size"
1258f27aaf4bSChristian Brunner         },
125960390a21SDaniel P. Berrange         {
126060390a21SDaniel P. Berrange             .name = "password-secret",
126160390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
126260390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
126360390a21SDaniel P. Berrange         },
1264bd0cf596SChunyan Liu         { /* end of list */ }
1265bd0cf596SChunyan Liu     }
1266f27aaf4bSChristian Brunner };
1267f27aaf4bSChristian Brunner 
12682654267cSMax Reitz static const char *const qemu_rbd_strong_runtime_opts[] = {
12692654267cSMax Reitz     "pool",
12702654267cSMax Reitz     "image",
12712654267cSMax Reitz     "conf",
12722654267cSMax Reitz     "snapshot",
12732654267cSMax Reitz     "user",
12742654267cSMax Reitz     "server.",
12752654267cSMax Reitz     "password-secret",
12762654267cSMax Reitz 
12772654267cSMax Reitz     NULL
12782654267cSMax Reitz };
12792654267cSMax Reitz 
1280f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
1281f27aaf4bSChristian Brunner     .format_name            = "rbd",
1282f27aaf4bSChristian Brunner     .instance_size          = sizeof(BDRVRBDState),
1283c7cacb3eSJeff Cody     .bdrv_parse_filename    = qemu_rbd_parse_filename,
1284e8e16d4bSEric Blake     .bdrv_refresh_limits    = qemu_rbd_refresh_limits,
1285ad32e9c0SJosh Durgin     .bdrv_file_open         = qemu_rbd_open,
1286ad32e9c0SJosh Durgin     .bdrv_close             = qemu_rbd_close,
128756e7cf8dSJeff Cody     .bdrv_reopen_prepare    = qemu_rbd_reopen_prepare,
12881bebea37SKevin Wolf     .bdrv_co_create         = qemu_rbd_co_create,
1289efc75e2aSStefan Hajnoczi     .bdrv_co_create_opts    = qemu_rbd_co_create_opts,
12903ac21627SPeter Lieven     .bdrv_has_zero_init     = bdrv_has_zero_init_1,
1291*1dcaf527SMax Reitz     .bdrv_has_zero_init_truncate = bdrv_has_zero_init_1,
1292ad32e9c0SJosh Durgin     .bdrv_get_info          = qemu_rbd_getinfo,
1293bd0cf596SChunyan Liu     .create_opts            = &qemu_rbd_create_opts,
1294ad32e9c0SJosh Durgin     .bdrv_getlength         = qemu_rbd_getlength,
1295061ca8a3SKevin Wolf     .bdrv_co_truncate       = qemu_rbd_co_truncate,
1296f27aaf4bSChristian Brunner     .protocol_name          = "rbd",
1297f27aaf4bSChristian Brunner 
1298e8e16d4bSEric Blake     .bdrv_aio_preadv        = qemu_rbd_aio_preadv,
1299e8e16d4bSEric Blake     .bdrv_aio_pwritev       = qemu_rbd_aio_pwritev,
1300dc7588c1SJosh Durgin 
1301dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1302dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
1303dc7588c1SJosh Durgin #else
1304c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
1305dc7588c1SJosh Durgin #endif
1306f27aaf4bSChristian Brunner 
1307787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
13084da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1309787f3133SJosh Durgin #endif
1310787f3133SJosh Durgin 
1311ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1312bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1313ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1314bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1315be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
13162b148f39SPaolo Bonzini     .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
1317be217884SAdam Crume #endif
13182654267cSMax Reitz 
13192654267cSMax Reitz     .strong_runtime_opts    = qemu_rbd_strong_runtime_opts,
1320f27aaf4bSChristian Brunner };
1321f27aaf4bSChristian Brunner 
1322f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1323f27aaf4bSChristian Brunner {
1324f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1325f27aaf4bSChristian Brunner }
1326f27aaf4bSChristian Brunner 
1327f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1328