xref: /qemu/block/rbd.c (revision e948f663)
1f27aaf4bSChristian Brunner /*
2f27aaf4bSChristian Brunner  * QEMU Block driver for RADOS (Ceph)
3f27aaf4bSChristian Brunner  *
4ad32e9c0SJosh Durgin  * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5ad32e9c0SJosh Durgin  *                         Josh Durgin <josh.durgin@dreamhost.com>
6f27aaf4bSChristian Brunner  *
7f27aaf4bSChristian Brunner  * This work is licensed under the terms of the GNU GPL, version 2.  See
8f27aaf4bSChristian Brunner  * the COPYING file in the top-level directory.
9f27aaf4bSChristian Brunner  *
106b620ca3SPaolo Bonzini  * Contributions after 2012-01-13 are licensed under the terms of the
116b620ca3SPaolo Bonzini  * GNU GPL, version 2 or (at your option) any later version.
12f27aaf4bSChristian Brunner  */
13f27aaf4bSChristian Brunner 
1480c71a24SPeter Maydell #include "qemu/osdep.h"
15ad32e9c0SJosh Durgin 
16da34e65cSMarkus Armbruster #include "qapi/error.h"
171de7afc9SPaolo Bonzini #include "qemu/error-report.h"
18737e150eSPaolo Bonzini #include "block/block_int.h"
1960390a21SDaniel P. Berrange #include "crypto/secret.h"
20f348b6d1SVeronia Bahaa #include "qemu/cutils.h"
21f27aaf4bSChristian Brunner 
22ad32e9c0SJosh Durgin #include <rbd/librbd.h>
23f27aaf4bSChristian Brunner 
24f27aaf4bSChristian Brunner /*
25f27aaf4bSChristian Brunner  * When specifying the image filename use:
26f27aaf4bSChristian Brunner  *
27fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
28f27aaf4bSChristian Brunner  *
299e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
30f27aaf4bSChristian Brunner  *
319e1fbcdeSSage Weil  * devicename is the name of the rbd image.
32f27aaf4bSChristian Brunner  *
339e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
349e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
35fab5cf59SJosh Durgin  *
369e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
379e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
389e1fbcdeSSage Weil  * (normally 'admin').
39f27aaf4bSChristian Brunner  *
409e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
419e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
429e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
439e1fbcdeSSage Weil  * file, specify conf=/dev/null.
44f27aaf4bSChristian Brunner  *
459e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
469e1fbcdeSSage Weil  * leading "\".
47f27aaf4bSChristian Brunner  */
48f27aaf4bSChristian Brunner 
49787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
50787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
51787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
52787f3133SJosh Durgin #else
53787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
54787f3133SJosh Durgin #endif
55787f3133SJosh Durgin 
56f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
57f27aaf4bSChristian Brunner 
58ad32e9c0SJosh Durgin #define RBD_MAX_CONF_NAME_SIZE 128
59ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512
60ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024
61ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128
62ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128
63ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
64ad32e9c0SJosh Durgin 
65787f3133SJosh Durgin typedef enum {
66787f3133SJosh Durgin     RBD_AIO_READ,
67787f3133SJosh Durgin     RBD_AIO_WRITE,
68dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
69dc7588c1SJosh Durgin     RBD_AIO_FLUSH
70787f3133SJosh Durgin } RBDAIOCmd;
71787f3133SJosh Durgin 
72f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
737c84b1b8SMarkus Armbruster     BlockAIOCB common;
7408448d51SStefan Priebe     int64_t ret;
75f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
76f27aaf4bSChristian Brunner     char *bounce;
77787f3133SJosh Durgin     RBDAIOCmd cmd;
78f27aaf4bSChristian Brunner     int error;
79f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
80f27aaf4bSChristian Brunner } RBDAIOCB;
81f27aaf4bSChristian Brunner 
82f27aaf4bSChristian Brunner typedef struct RADOSCB {
83f27aaf4bSChristian Brunner     RBDAIOCB *acb;
84f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
85ad32e9c0SJosh Durgin     int64_t size;
86f27aaf4bSChristian Brunner     char *buf;
8708448d51SStefan Priebe     int64_t ret;
88f27aaf4bSChristian Brunner } RADOSCB;
89f27aaf4bSChristian Brunner 
90f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
91ad32e9c0SJosh Durgin     rados_t cluster;
92ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
93ad32e9c0SJosh Durgin     rbd_image_t image;
94ad32e9c0SJosh Durgin     char name[RBD_MAX_IMAGE_NAME_SIZE];
95ad32e9c0SJosh Durgin     char *snap;
96f27aaf4bSChristian Brunner } BDRVRBDState;
97f27aaf4bSChristian Brunner 
98ad32e9c0SJosh Durgin static int qemu_rbd_next_tok(char *dst, int dst_len,
99f27aaf4bSChristian Brunner                              char *src, char delim,
100f27aaf4bSChristian Brunner                              const char *name,
101d61563b2SMarkus Armbruster                              char **p, Error **errp)
102f27aaf4bSChristian Brunner {
103f27aaf4bSChristian Brunner     int l;
104f27aaf4bSChristian Brunner     char *end;
105f27aaf4bSChristian Brunner 
106f27aaf4bSChristian Brunner     *p = NULL;
107f27aaf4bSChristian Brunner 
108f27aaf4bSChristian Brunner     if (delim != '\0') {
10916a06b24SSage Weil         for (end = src; *end; ++end) {
11016a06b24SSage Weil             if (*end == delim) {
11116a06b24SSage Weil                 break;
11216a06b24SSage Weil             }
11316a06b24SSage Weil             if (*end == '\\' && end[1] != '\0') {
11416a06b24SSage Weil                 end++;
11516a06b24SSage Weil             }
11616a06b24SSage Weil         }
11716a06b24SSage Weil         if (*end == delim) {
118f27aaf4bSChristian Brunner             *p = end + 1;
119f27aaf4bSChristian Brunner             *end = '\0';
120f27aaf4bSChristian Brunner         }
121f27aaf4bSChristian Brunner     }
122f27aaf4bSChristian Brunner     l = strlen(src);
123f27aaf4bSChristian Brunner     if (l >= dst_len) {
124d61563b2SMarkus Armbruster         error_setg(errp, "%s too long", name);
125f27aaf4bSChristian Brunner         return -EINVAL;
126f27aaf4bSChristian Brunner     } else if (l == 0) {
127d61563b2SMarkus Armbruster         error_setg(errp, "%s too short", name);
128f27aaf4bSChristian Brunner         return -EINVAL;
129f27aaf4bSChristian Brunner     }
130f27aaf4bSChristian Brunner 
131f27aaf4bSChristian Brunner     pstrcpy(dst, dst_len, src);
132f27aaf4bSChristian Brunner 
133f27aaf4bSChristian Brunner     return 0;
134f27aaf4bSChristian Brunner }
135f27aaf4bSChristian Brunner 
13616a06b24SSage Weil static void qemu_rbd_unescape(char *src)
13716a06b24SSage Weil {
13816a06b24SSage Weil     char *p;
13916a06b24SSage Weil 
14016a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14116a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
14216a06b24SSage Weil             src++;
14316a06b24SSage Weil         }
14416a06b24SSage Weil         *p = *src;
14516a06b24SSage Weil     }
14616a06b24SSage Weil     *p = '\0';
14716a06b24SSage Weil }
14816a06b24SSage Weil 
149ad32e9c0SJosh Durgin static int qemu_rbd_parsename(const char *filename,
150f27aaf4bSChristian Brunner                               char *pool, int pool_len,
151f27aaf4bSChristian Brunner                               char *snap, int snap_len,
152fab5cf59SJosh Durgin                               char *name, int name_len,
153d61563b2SMarkus Armbruster                               char *conf, int conf_len,
154d61563b2SMarkus Armbruster                               Error **errp)
155f27aaf4bSChristian Brunner {
156f27aaf4bSChristian Brunner     const char *start;
157f27aaf4bSChristian Brunner     char *p, *buf;
158f27aaf4bSChristian Brunner     int ret;
159f27aaf4bSChristian Brunner 
160f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
161d61563b2SMarkus Armbruster         error_setg(errp, "File name must start with 'rbd:'");
162f27aaf4bSChristian Brunner         return -EINVAL;
163f27aaf4bSChristian Brunner     }
164f27aaf4bSChristian Brunner 
1657267c094SAnthony Liguori     buf = g_strdup(start);
166f27aaf4bSChristian Brunner     p = buf;
167fab5cf59SJosh Durgin     *snap = '\0';
168fab5cf59SJosh Durgin     *conf = '\0';
169f27aaf4bSChristian Brunner 
170d61563b2SMarkus Armbruster     ret = qemu_rbd_next_tok(pool, pool_len, p,
171d61563b2SMarkus Armbruster                             '/', "pool name", &p, errp);
172f27aaf4bSChristian Brunner     if (ret < 0 || !p) {
173f27aaf4bSChristian Brunner         ret = -EINVAL;
174f27aaf4bSChristian Brunner         goto done;
175f27aaf4bSChristian Brunner     }
17616a06b24SSage Weil     qemu_rbd_unescape(pool);
177fab5cf59SJosh Durgin 
178fab5cf59SJosh Durgin     if (strchr(p, '@')) {
179d61563b2SMarkus Armbruster         ret = qemu_rbd_next_tok(name, name_len, p,
180d61563b2SMarkus Armbruster                                 '@', "object name", &p, errp);
181f27aaf4bSChristian Brunner         if (ret < 0) {
182f27aaf4bSChristian Brunner             goto done;
183f27aaf4bSChristian Brunner         }
184d61563b2SMarkus Armbruster         ret = qemu_rbd_next_tok(snap, snap_len, p,
185d61563b2SMarkus Armbruster                                 ':', "snap name", &p, errp);
18616a06b24SSage Weil         qemu_rbd_unescape(snap);
187fab5cf59SJosh Durgin     } else {
188d61563b2SMarkus Armbruster         ret = qemu_rbd_next_tok(name, name_len, p,
189d61563b2SMarkus Armbruster                                 ':', "object name", &p, errp);
190fab5cf59SJosh Durgin     }
19116a06b24SSage Weil     qemu_rbd_unescape(name);
192fab5cf59SJosh Durgin     if (ret < 0 || !p) {
193f27aaf4bSChristian Brunner         goto done;
194f27aaf4bSChristian Brunner     }
195f27aaf4bSChristian Brunner 
196d61563b2SMarkus Armbruster     ret = qemu_rbd_next_tok(conf, conf_len, p,
197d61563b2SMarkus Armbruster                             '\0', "configuration", &p, errp);
198f27aaf4bSChristian Brunner 
199f27aaf4bSChristian Brunner done:
2007267c094SAnthony Liguori     g_free(buf);
201f27aaf4bSChristian Brunner     return ret;
202f27aaf4bSChristian Brunner }
203f27aaf4bSChristian Brunner 
2047c7e9df0SSage Weil static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
2057c7e9df0SSage Weil {
2067c7e9df0SSage Weil     const char *p = conf;
2077c7e9df0SSage Weil 
2087c7e9df0SSage Weil     while (*p) {
2097c7e9df0SSage Weil         int len;
2107c7e9df0SSage Weil         const char *end = strchr(p, ':');
2117c7e9df0SSage Weil 
2127c7e9df0SSage Weil         if (end) {
2137c7e9df0SSage Weil             len = end - p;
2147c7e9df0SSage Weil         } else {
2157c7e9df0SSage Weil             len = strlen(p);
2167c7e9df0SSage Weil         }
2177c7e9df0SSage Weil 
2187c7e9df0SSage Weil         if (strncmp(p, "id=", 3) == 0) {
2197c7e9df0SSage Weil             len -= 3;
2207c7e9df0SSage Weil             strncpy(clientname, p + 3, len);
2217c7e9df0SSage Weil             clientname[len] = '\0';
2227c7e9df0SSage Weil             return clientname;
2237c7e9df0SSage Weil         }
2247c7e9df0SSage Weil         if (end == NULL) {
2257c7e9df0SSage Weil             break;
2267c7e9df0SSage Weil         }
2277c7e9df0SSage Weil         p = end + 1;
2287c7e9df0SSage Weil     }
2297c7e9df0SSage Weil     return NULL;
2307c7e9df0SSage Weil }
2317c7e9df0SSage Weil 
23260390a21SDaniel P. Berrange 
23360390a21SDaniel P. Berrange static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
23460390a21SDaniel P. Berrange                              Error **errp)
23560390a21SDaniel P. Berrange {
23660390a21SDaniel P. Berrange     if (secretid == 0) {
23760390a21SDaniel P. Berrange         return 0;
23860390a21SDaniel P. Berrange     }
23960390a21SDaniel P. Berrange 
24060390a21SDaniel P. Berrange     gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
24160390a21SDaniel P. Berrange                                                     errp);
24260390a21SDaniel P. Berrange     if (!secret) {
24360390a21SDaniel P. Berrange         return -1;
24460390a21SDaniel P. Berrange     }
24560390a21SDaniel P. Berrange 
24660390a21SDaniel P. Berrange     rados_conf_set(cluster, "key", secret);
24760390a21SDaniel P. Berrange     g_free(secret);
24860390a21SDaniel P. Berrange 
24960390a21SDaniel P. Berrange     return 0;
25060390a21SDaniel P. Berrange }
25160390a21SDaniel P. Berrange 
25260390a21SDaniel P. Berrange 
253e34d8f29SJosh Durgin static int qemu_rbd_set_conf(rados_t cluster, const char *conf,
254e34d8f29SJosh Durgin                              bool only_read_conf_file,
255e34d8f29SJosh Durgin                              Error **errp)
256fab5cf59SJosh Durgin {
257fab5cf59SJosh Durgin     char *p, *buf;
258fab5cf59SJosh Durgin     char name[RBD_MAX_CONF_NAME_SIZE];
259fab5cf59SJosh Durgin     char value[RBD_MAX_CONF_VAL_SIZE];
260fab5cf59SJosh Durgin     int ret = 0;
261fab5cf59SJosh Durgin 
2627267c094SAnthony Liguori     buf = g_strdup(conf);
263fab5cf59SJosh Durgin     p = buf;
264fab5cf59SJosh Durgin 
265fab5cf59SJosh Durgin     while (p) {
266fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(name, sizeof(name), p,
267d61563b2SMarkus Armbruster                                 '=', "conf option name", &p, errp);
268fab5cf59SJosh Durgin         if (ret < 0) {
269fab5cf59SJosh Durgin             break;
270fab5cf59SJosh Durgin         }
27116a06b24SSage Weil         qemu_rbd_unescape(name);
272fab5cf59SJosh Durgin 
273fab5cf59SJosh Durgin         if (!p) {
274d61563b2SMarkus Armbruster             error_setg(errp, "conf option %s has no value", name);
275fab5cf59SJosh Durgin             ret = -EINVAL;
276fab5cf59SJosh Durgin             break;
277fab5cf59SJosh Durgin         }
278fab5cf59SJosh Durgin 
279fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(value, sizeof(value), p,
280d61563b2SMarkus Armbruster                                 ':', "conf option value", &p, errp);
281fab5cf59SJosh Durgin         if (ret < 0) {
282fab5cf59SJosh Durgin             break;
283fab5cf59SJosh Durgin         }
28416a06b24SSage Weil         qemu_rbd_unescape(value);
285fab5cf59SJosh Durgin 
2867c7e9df0SSage Weil         if (strcmp(name, "conf") == 0) {
287e34d8f29SJosh Durgin             /* read the conf file alone, so it doesn't override more
288e34d8f29SJosh Durgin                specific settings for a particular device */
289e34d8f29SJosh Durgin             if (only_read_conf_file) {
2907c7e9df0SSage Weil                 ret = rados_conf_read_file(cluster, value);
2917c7e9df0SSage Weil                 if (ret < 0) {
29287cd3d20SVikhyat Umrao                     error_setg_errno(errp, -ret, "error reading conf file %s",
29387cd3d20SVikhyat Umrao                                      value);
2947c7e9df0SSage Weil                     break;
2957c7e9df0SSage Weil                 }
296e34d8f29SJosh Durgin             }
2977c7e9df0SSage Weil         } else if (strcmp(name, "id") == 0) {
2987c7e9df0SSage Weil             /* ignore, this is parsed by qemu_rbd_parse_clientname() */
299e34d8f29SJosh Durgin         } else if (!only_read_conf_file) {
300fab5cf59SJosh Durgin             ret = rados_conf_set(cluster, name, value);
301fab5cf59SJosh Durgin             if (ret < 0) {
30287cd3d20SVikhyat Umrao                 error_setg_errno(errp, -ret, "invalid conf option %s", name);
303fab5cf59SJosh Durgin                 ret = -EINVAL;
304fab5cf59SJosh Durgin                 break;
305fab5cf59SJosh Durgin             }
306fab5cf59SJosh Durgin         }
307fab5cf59SJosh Durgin     }
308fab5cf59SJosh Durgin 
3097267c094SAnthony Liguori     g_free(buf);
310fab5cf59SJosh Durgin     return ret;
311fab5cf59SJosh Durgin }
312fab5cf59SJosh Durgin 
313bd0cf596SChunyan Liu static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
314f27aaf4bSChristian Brunner {
315d61563b2SMarkus Armbruster     Error *local_err = NULL;
316f27aaf4bSChristian Brunner     int64_t bytes = 0;
317f27aaf4bSChristian Brunner     int64_t objsize;
318ad32e9c0SJosh Durgin     int obj_order = 0;
319ad32e9c0SJosh Durgin     char pool[RBD_MAX_POOL_NAME_SIZE];
320ad32e9c0SJosh Durgin     char name[RBD_MAX_IMAGE_NAME_SIZE];
321ad32e9c0SJosh Durgin     char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
322fab5cf59SJosh Durgin     char conf[RBD_MAX_CONF_SIZE];
3237c7e9df0SSage Weil     char clientname_buf[RBD_MAX_CONF_SIZE];
3247c7e9df0SSage Weil     char *clientname;
32560390a21SDaniel P. Berrange     const char *secretid;
326ad32e9c0SJosh Durgin     rados_t cluster;
327ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
328f27aaf4bSChristian Brunner     int ret;
329f27aaf4bSChristian Brunner 
33060390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
33160390a21SDaniel P. Berrange 
332ad32e9c0SJosh Durgin     if (qemu_rbd_parsename(filename, pool, sizeof(pool),
333f27aaf4bSChristian Brunner                            snap_buf, sizeof(snap_buf),
334fab5cf59SJosh Durgin                            name, sizeof(name),
335d61563b2SMarkus Armbruster                            conf, sizeof(conf), &local_err) < 0) {
336d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
337f27aaf4bSChristian Brunner         return -EINVAL;
338f27aaf4bSChristian Brunner     }
339f27aaf4bSChristian Brunner 
340f27aaf4bSChristian Brunner     /* Read out options */
341c2eb918eSHu Tao     bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
342c2eb918eSHu Tao                      BDRV_SECTOR_SIZE);
343bd0cf596SChunyan Liu     objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
344bd0cf596SChunyan Liu     if (objsize) {
345f27aaf4bSChristian Brunner         if ((objsize - 1) & objsize) {    /* not a power of 2? */
346d61563b2SMarkus Armbruster             error_setg(errp, "obj size needs to be power of 2");
347f27aaf4bSChristian Brunner             return -EINVAL;
348f27aaf4bSChristian Brunner         }
349f27aaf4bSChristian Brunner         if (objsize < 4096) {
350d61563b2SMarkus Armbruster             error_setg(errp, "obj size too small");
351f27aaf4bSChristian Brunner             return -EINVAL;
352f27aaf4bSChristian Brunner         }
353786a4ea8SStefan Hajnoczi         obj_order = ctz32(objsize);
354f27aaf4bSChristian Brunner     }
355f27aaf4bSChristian Brunner 
3567c7e9df0SSage Weil     clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
35787cd3d20SVikhyat Umrao     ret = rados_create(&cluster, clientname);
35887cd3d20SVikhyat Umrao     if (ret < 0) {
35987cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error initializing");
36087cd3d20SVikhyat Umrao         return ret;
361f27aaf4bSChristian Brunner     }
362f27aaf4bSChristian Brunner 
363fab5cf59SJosh Durgin     if (strstr(conf, "conf=") == NULL) {
364f9fe18ecSSage Weil         /* try default location, but ignore failure */
365f9fe18ecSSage Weil         rados_conf_read_file(cluster, NULL);
366e34d8f29SJosh Durgin     } else if (conf[0] != '\0' &&
367e34d8f29SJosh Durgin                qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
368e34d8f29SJosh Durgin         rados_shutdown(cluster);
369e34d8f29SJosh Durgin         error_propagate(errp, local_err);
370e34d8f29SJosh Durgin         return -EIO;
371fab5cf59SJosh Durgin     }
372fab5cf59SJosh Durgin 
373fab5cf59SJosh Durgin     if (conf[0] != '\0' &&
374e34d8f29SJosh Durgin         qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
375fab5cf59SJosh Durgin         rados_shutdown(cluster);
376d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
377fab5cf59SJosh Durgin         return -EIO;
378fab5cf59SJosh Durgin     }
379ad32e9c0SJosh Durgin 
38060390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
38160390a21SDaniel P. Berrange         rados_shutdown(cluster);
38260390a21SDaniel P. Berrange         return -EIO;
38360390a21SDaniel P. Berrange     }
38460390a21SDaniel P. Berrange 
38587cd3d20SVikhyat Umrao     ret = rados_connect(cluster);
38687cd3d20SVikhyat Umrao     if (ret < 0) {
38787cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error connecting");
388ad32e9c0SJosh Durgin         rados_shutdown(cluster);
38987cd3d20SVikhyat Umrao         return ret;
390ad32e9c0SJosh Durgin     }
391ad32e9c0SJosh Durgin 
39287cd3d20SVikhyat Umrao     ret = rados_ioctx_create(cluster, pool, &io_ctx);
39387cd3d20SVikhyat Umrao     if (ret < 0) {
39487cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error opening pool %s", pool);
395ad32e9c0SJosh Durgin         rados_shutdown(cluster);
39687cd3d20SVikhyat Umrao         return ret;
397f27aaf4bSChristian Brunner     }
398f27aaf4bSChristian Brunner 
399ad32e9c0SJosh Durgin     ret = rbd_create(io_ctx, name, bytes, &obj_order);
400ad32e9c0SJosh Durgin     rados_ioctx_destroy(io_ctx);
401ad32e9c0SJosh Durgin     rados_shutdown(cluster);
40287cd3d20SVikhyat Umrao     if (ret < 0) {
40387cd3d20SVikhyat Umrao         error_setg_errno(errp, -ret, "error rbd create");
40487cd3d20SVikhyat Umrao         return ret;
40587cd3d20SVikhyat Umrao     }
406f27aaf4bSChristian Brunner 
407f27aaf4bSChristian Brunner     return ret;
408f27aaf4bSChristian Brunner }
409f27aaf4bSChristian Brunner 
410f27aaf4bSChristian Brunner /*
411e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
412e04fb07fSStefan Hajnoczi  * BH context.
413f27aaf4bSChristian Brunner  */
414ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
415f27aaf4bSChristian Brunner {
416f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
417f27aaf4bSChristian Brunner     int64_t r;
418f27aaf4bSChristian Brunner 
419f27aaf4bSChristian Brunner     r = rcb->ret;
420f27aaf4bSChristian Brunner 
421dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
422f27aaf4bSChristian Brunner         if (r < 0) {
423f27aaf4bSChristian Brunner             acb->ret = r;
424f27aaf4bSChristian Brunner             acb->error = 1;
425f27aaf4bSChristian Brunner         } else if (!acb->error) {
426ad32e9c0SJosh Durgin             acb->ret = rcb->size;
427f27aaf4bSChristian Brunner         }
428f27aaf4bSChristian Brunner     } else {
429ad32e9c0SJosh Durgin         if (r < 0) {
430ad32e9c0SJosh Durgin             memset(rcb->buf, 0, rcb->size);
431f27aaf4bSChristian Brunner             acb->ret = r;
432f27aaf4bSChristian Brunner             acb->error = 1;
433ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
434ad32e9c0SJosh Durgin             memset(rcb->buf + r, 0, rcb->size - r);
435f27aaf4bSChristian Brunner             if (!acb->error) {
436ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
437f27aaf4bSChristian Brunner             }
438f27aaf4bSChristian Brunner         } else if (!acb->error) {
439ad32e9c0SJosh Durgin             acb->ret = r;
440f27aaf4bSChristian Brunner         }
441f27aaf4bSChristian Brunner     }
442e04fb07fSStefan Hajnoczi 
4437267c094SAnthony Liguori     g_free(rcb);
444e04fb07fSStefan Hajnoczi 
445e04fb07fSStefan Hajnoczi     if (acb->cmd == RBD_AIO_READ) {
446e04fb07fSStefan Hajnoczi         qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
447f27aaf4bSChristian Brunner     }
448e04fb07fSStefan Hajnoczi     qemu_vfree(acb->bounce);
449e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
450f27aaf4bSChristian Brunner 
4518007429aSFam Zheng     qemu_aio_unref(acb);
452f27aaf4bSChristian Brunner }
453f27aaf4bSChristian Brunner 
454a9ccedc3SKevin Wolf /* TODO Convert to fine grained options */
455a9ccedc3SKevin Wolf static QemuOptsList runtime_opts = {
456a9ccedc3SKevin Wolf     .name = "rbd",
457a9ccedc3SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
458a9ccedc3SKevin Wolf     .desc = {
459a9ccedc3SKevin Wolf         {
460a9ccedc3SKevin Wolf             .name = "filename",
461a9ccedc3SKevin Wolf             .type = QEMU_OPT_STRING,
462a9ccedc3SKevin Wolf             .help = "Specification of the rbd image",
463a9ccedc3SKevin Wolf         },
46460390a21SDaniel P. Berrange         {
46560390a21SDaniel P. Berrange             .name = "password-secret",
46660390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
46760390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
46860390a21SDaniel P. Berrange         },
469a9ccedc3SKevin Wolf         { /* end of list */ }
470a9ccedc3SKevin Wolf     },
471a9ccedc3SKevin Wolf };
472a9ccedc3SKevin Wolf 
473015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
474015a1036SMax Reitz                          Error **errp)
475f27aaf4bSChristian Brunner {
476f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
477ad32e9c0SJosh Durgin     char pool[RBD_MAX_POOL_NAME_SIZE];
478ad32e9c0SJosh Durgin     char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
479fab5cf59SJosh Durgin     char conf[RBD_MAX_CONF_SIZE];
4807c7e9df0SSage Weil     char clientname_buf[RBD_MAX_CONF_SIZE];
4817c7e9df0SSage Weil     char *clientname;
48260390a21SDaniel P. Berrange     const char *secretid;
483a9ccedc3SKevin Wolf     QemuOpts *opts;
484a9ccedc3SKevin Wolf     Error *local_err = NULL;
485a9ccedc3SKevin Wolf     const char *filename;
486f27aaf4bSChristian Brunner     int r;
487f27aaf4bSChristian Brunner 
48887ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
489a9ccedc3SKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
49084d18f06SMarkus Armbruster     if (local_err) {
491d61563b2SMarkus Armbruster         error_propagate(errp, local_err);
492a9ccedc3SKevin Wolf         qemu_opts_del(opts);
493a9ccedc3SKevin Wolf         return -EINVAL;
494a9ccedc3SKevin Wolf     }
495a9ccedc3SKevin Wolf 
496a9ccedc3SKevin Wolf     filename = qemu_opt_get(opts, "filename");
49760390a21SDaniel P. Berrange     secretid = qemu_opt_get(opts, "password-secret");
498a9ccedc3SKevin Wolf 
499ad32e9c0SJosh Durgin     if (qemu_rbd_parsename(filename, pool, sizeof(pool),
500f27aaf4bSChristian Brunner                            snap_buf, sizeof(snap_buf),
501fab5cf59SJosh Durgin                            s->name, sizeof(s->name),
502d61563b2SMarkus Armbruster                            conf, sizeof(conf), errp) < 0) {
503c3ca988dSKevin Wolf         r = -EINVAL;
504c3ca988dSKevin Wolf         goto failed_opts;
505f27aaf4bSChristian Brunner     }
506f27aaf4bSChristian Brunner 
5077c7e9df0SSage Weil     clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
5087c7e9df0SSage Weil     r = rados_create(&s->cluster, clientname);
509ad32e9c0SJosh Durgin     if (r < 0) {
51087cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error initializing");
511c3ca988dSKevin Wolf         goto failed_opts;
512f27aaf4bSChristian Brunner     }
513f27aaf4bSChristian Brunner 
514eb93d5d9SSage Weil     s->snap = NULL;
515eb93d5d9SSage Weil     if (snap_buf[0] != '\0') {
516eb93d5d9SSage Weil         s->snap = g_strdup(snap_buf);
517eb93d5d9SSage Weil     }
518eb93d5d9SSage Weil 
51999a3c89dSJosh Durgin     if (strstr(conf, "conf=") == NULL) {
52099a3c89dSJosh Durgin         /* try default location, but ignore failure */
52199a3c89dSJosh Durgin         rados_conf_read_file(s->cluster, NULL);
522e34d8f29SJosh Durgin     } else if (conf[0] != '\0') {
523e34d8f29SJosh Durgin         r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
524e34d8f29SJosh Durgin         if (r < 0) {
525e34d8f29SJosh Durgin             goto failed_shutdown;
526e34d8f29SJosh Durgin         }
52799a3c89dSJosh Durgin     }
52899a3c89dSJosh Durgin 
52999a3c89dSJosh Durgin     if (conf[0] != '\0') {
530e34d8f29SJosh Durgin         r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
53199a3c89dSJosh Durgin         if (r < 0) {
53299a3c89dSJosh Durgin             goto failed_shutdown;
53399a3c89dSJosh Durgin         }
53499a3c89dSJosh Durgin     }
53599a3c89dSJosh Durgin 
53660390a21SDaniel P. Berrange     if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
53760390a21SDaniel P. Berrange         r = -EIO;
53860390a21SDaniel P. Berrange         goto failed_shutdown;
53960390a21SDaniel P. Berrange     }
54060390a21SDaniel P. Berrange 
541b11f38fcSJosh Durgin     /*
542b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
543b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
544b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
545b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
546b11f38fcSJosh Durgin      * be set up, fall back to no caching.
547b11f38fcSJosh Durgin      */
548b11f38fcSJosh Durgin     if (flags & BDRV_O_NOCACHE) {
549b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "false");
550b11f38fcSJosh Durgin     } else {
551b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "true");
552b11f38fcSJosh Durgin     }
553b11f38fcSJosh Durgin 
554ad32e9c0SJosh Durgin     r = rados_connect(s->cluster);
555ad32e9c0SJosh Durgin     if (r < 0) {
55687cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error connecting");
557eb93d5d9SSage Weil         goto failed_shutdown;
558ad32e9c0SJosh Durgin     }
559ad32e9c0SJosh Durgin 
560ad32e9c0SJosh Durgin     r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
561ad32e9c0SJosh Durgin     if (r < 0) {
56287cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error opening pool %s", pool);
563eb93d5d9SSage Weil         goto failed_shutdown;
564ad32e9c0SJosh Durgin     }
565ad32e9c0SJosh Durgin 
566ad32e9c0SJosh Durgin     r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
567ad32e9c0SJosh Durgin     if (r < 0) {
56887cd3d20SVikhyat Umrao         error_setg_errno(errp, -r, "error reading header from %s", s->name);
569eb93d5d9SSage Weil         goto failed_open;
570ad32e9c0SJosh Durgin     }
571ad32e9c0SJosh Durgin 
572ad32e9c0SJosh Durgin     bs->read_only = (s->snap != NULL);
573f27aaf4bSChristian Brunner 
574c3ca988dSKevin Wolf     qemu_opts_del(opts);
575f27aaf4bSChristian Brunner     return 0;
576f27aaf4bSChristian Brunner 
577eb93d5d9SSage Weil failed_open:
578ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
579eb93d5d9SSage Weil failed_shutdown:
580ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
581eb93d5d9SSage Weil     g_free(s->snap);
582c3ca988dSKevin Wolf failed_opts:
583c3ca988dSKevin Wolf     qemu_opts_del(opts);
584f27aaf4bSChristian Brunner     return r;
585f27aaf4bSChristian Brunner }
586f27aaf4bSChristian Brunner 
587ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
588f27aaf4bSChristian Brunner {
589f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
590f27aaf4bSChristian Brunner 
591ad32e9c0SJosh Durgin     rbd_close(s->image);
592ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
5937267c094SAnthony Liguori     g_free(s->snap);
594ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
595f27aaf4bSChristian Brunner }
596f27aaf4bSChristian Brunner 
597d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
598f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
599f27aaf4bSChristian Brunner };
600f27aaf4bSChristian Brunner 
601e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
602f27aaf4bSChristian Brunner {
603e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
604e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
605ad32e9c0SJosh Durgin }
606ad32e9c0SJosh Durgin 
607ad32e9c0SJosh Durgin /*
608ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
609ad32e9c0SJosh Durgin  *
610ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
611ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
612e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
613e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
614ad32e9c0SJosh Durgin  */
615ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
616ad32e9c0SJosh Durgin {
617e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
618e04fb07fSStefan Hajnoczi 
619ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
620ad32e9c0SJosh Durgin     rbd_aio_release(c);
621f27aaf4bSChristian Brunner 
622fffb6e12SPaolo Bonzini     aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
623ea800191SStefan Hajnoczi                             rbd_finish_bh, rcb);
624473c7f02SStefan Priebe }
625f27aaf4bSChristian Brunner 
626787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
627787f3133SJosh Durgin                                    uint64_t off,
628787f3133SJosh Durgin                                    uint64_t len,
629787f3133SJosh Durgin                                    rbd_completion_t comp)
630787f3133SJosh Durgin {
631787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
632787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
633787f3133SJosh Durgin #else
634787f3133SJosh Durgin     return -ENOTSUP;
635787f3133SJosh Durgin #endif
636787f3133SJosh Durgin }
637787f3133SJosh Durgin 
638dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
639dc7588c1SJosh Durgin                                  rbd_completion_t comp)
640dc7588c1SJosh Durgin {
641dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
642dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
643dc7588c1SJosh Durgin #else
644dc7588c1SJosh Durgin     return -ENOTSUP;
645dc7588c1SJosh Durgin #endif
646dc7588c1SJosh Durgin }
647dc7588c1SJosh Durgin 
6487c84b1b8SMarkus Armbruster static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
6497bbca9e2SEric Blake                                  int64_t off,
650f27aaf4bSChristian Brunner                                  QEMUIOVector *qiov,
6517bbca9e2SEric Blake                                  int64_t size,
652097310b5SMarkus Armbruster                                  BlockCompletionFunc *cb,
653787f3133SJosh Durgin                                  void *opaque,
654787f3133SJosh Durgin                                  RBDAIOCmd cmd)
655f27aaf4bSChristian Brunner {
656f27aaf4bSChristian Brunner     RBDAIOCB *acb;
6570f7a0237SKevin Wolf     RADOSCB *rcb = NULL;
658ad32e9c0SJosh Durgin     rbd_completion_t c;
659f27aaf4bSChristian Brunner     char *buf;
66051a13528SJosh Durgin     int r;
661f27aaf4bSChristian Brunner 
662f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
663f27aaf4bSChristian Brunner 
664d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
665787f3133SJosh Durgin     acb->cmd = cmd;
666f27aaf4bSChristian Brunner     acb->qiov = qiov;
6677bbca9e2SEric Blake     assert(!qiov || qiov->size == size);
668dc7588c1SJosh Durgin     if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
669787f3133SJosh Durgin         acb->bounce = NULL;
670787f3133SJosh Durgin     } else {
6710f7a0237SKevin Wolf         acb->bounce = qemu_try_blockalign(bs, qiov->size);
6720f7a0237SKevin Wolf         if (acb->bounce == NULL) {
6730f7a0237SKevin Wolf             goto failed;
6740f7a0237SKevin Wolf         }
675787f3133SJosh Durgin     }
676f27aaf4bSChristian Brunner     acb->ret = 0;
677f27aaf4bSChristian Brunner     acb->error = 0;
678f27aaf4bSChristian Brunner     acb->s = s;
679f27aaf4bSChristian Brunner 
680787f3133SJosh Durgin     if (cmd == RBD_AIO_WRITE) {
681d5e6b161SMichael Tokarev         qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
682f27aaf4bSChristian Brunner     }
683f27aaf4bSChristian Brunner 
684f27aaf4bSChristian Brunner     buf = acb->bounce;
685f27aaf4bSChristian Brunner 
6865839e53bSMarkus Armbruster     rcb = g_new(RADOSCB, 1);
687f27aaf4bSChristian Brunner     rcb->acb = acb;
688f27aaf4bSChristian Brunner     rcb->buf = buf;
689f27aaf4bSChristian Brunner     rcb->s = acb->s;
690ad32e9c0SJosh Durgin     rcb->size = size;
69151a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
69251a13528SJosh Durgin     if (r < 0) {
69351a13528SJosh Durgin         goto failed;
69451a13528SJosh Durgin     }
695f27aaf4bSChristian Brunner 
696787f3133SJosh Durgin     switch (cmd) {
697787f3133SJosh Durgin     case RBD_AIO_WRITE:
69851a13528SJosh Durgin         r = rbd_aio_write(s->image, off, size, buf, c);
699787f3133SJosh Durgin         break;
700787f3133SJosh Durgin     case RBD_AIO_READ:
70151a13528SJosh Durgin         r = rbd_aio_read(s->image, off, size, buf, c);
702787f3133SJosh Durgin         break;
703787f3133SJosh Durgin     case RBD_AIO_DISCARD:
704787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
705787f3133SJosh Durgin         break;
706dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
707dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
708dc7588c1SJosh Durgin         break;
709787f3133SJosh Durgin     default:
710787f3133SJosh Durgin         r = -EINVAL;
71151a13528SJosh Durgin     }
71251a13528SJosh Durgin 
71351a13528SJosh Durgin     if (r < 0) {
714405a2764SKevin Wolf         goto failed_completion;
715f27aaf4bSChristian Brunner     }
716f27aaf4bSChristian Brunner 
717f27aaf4bSChristian Brunner     return &acb->common;
71851a13528SJosh Durgin 
719405a2764SKevin Wolf failed_completion:
720405a2764SKevin Wolf     rbd_aio_release(c);
72151a13528SJosh Durgin failed:
7227267c094SAnthony Liguori     g_free(rcb);
723405a2764SKevin Wolf     qemu_vfree(acb->bounce);
7248007429aSFam Zheng     qemu_aio_unref(acb);
72551a13528SJosh Durgin     return NULL;
726f27aaf4bSChristian Brunner }
727f27aaf4bSChristian Brunner 
7287c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
729ad32e9c0SJosh Durgin                                       int64_t sector_num,
730ad32e9c0SJosh Durgin                                       QEMUIOVector *qiov,
731f27aaf4bSChristian Brunner                                       int nb_sectors,
732097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
733f27aaf4bSChristian Brunner                                       void *opaque)
734f27aaf4bSChristian Brunner {
7357bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
736*e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
737787f3133SJosh Durgin                          RBD_AIO_READ);
738f27aaf4bSChristian Brunner }
739f27aaf4bSChristian Brunner 
7407c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
741ad32e9c0SJosh Durgin                                        int64_t sector_num,
742ad32e9c0SJosh Durgin                                        QEMUIOVector *qiov,
743f27aaf4bSChristian Brunner                                        int nb_sectors,
744097310b5SMarkus Armbruster                                        BlockCompletionFunc *cb,
745f27aaf4bSChristian Brunner                                        void *opaque)
746f27aaf4bSChristian Brunner {
7477bbca9e2SEric Blake     return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
748*e948f663SPaolo Bonzini                          (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
749787f3133SJosh Durgin                          RBD_AIO_WRITE);
750f27aaf4bSChristian Brunner }
751f27aaf4bSChristian Brunner 
752dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
7537c84b1b8SMarkus Armbruster static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
754097310b5SMarkus Armbruster                                       BlockCompletionFunc *cb,
755dc7588c1SJosh Durgin                                       void *opaque)
756dc7588c1SJosh Durgin {
757dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
758dc7588c1SJosh Durgin }
759dc7588c1SJosh Durgin 
760dc7588c1SJosh Durgin #else
761dc7588c1SJosh Durgin 
7628b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
7637a3f5fe9SSage Weil {
7647a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
7657a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
7667a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
7677a3f5fe9SSage Weil     return rbd_flush(s->image);
7687a3f5fe9SSage Weil #else
7697a3f5fe9SSage Weil     return 0;
7707a3f5fe9SSage Weil #endif
7717a3f5fe9SSage Weil }
772dc7588c1SJosh Durgin #endif
7737a3f5fe9SSage Weil 
774ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
775f27aaf4bSChristian Brunner {
776f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
777ad32e9c0SJosh Durgin     rbd_image_info_t info;
778ad32e9c0SJosh Durgin     int r;
779ad32e9c0SJosh Durgin 
780ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
781ad32e9c0SJosh Durgin     if (r < 0) {
782ad32e9c0SJosh Durgin         return r;
783ad32e9c0SJosh Durgin     }
784ad32e9c0SJosh Durgin 
785ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
786f27aaf4bSChristian Brunner     return 0;
787f27aaf4bSChristian Brunner }
788f27aaf4bSChristian Brunner 
789ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
790f27aaf4bSChristian Brunner {
791f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
792ad32e9c0SJosh Durgin     rbd_image_info_t info;
793ad32e9c0SJosh Durgin     int r;
794f27aaf4bSChristian Brunner 
795ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
796ad32e9c0SJosh Durgin     if (r < 0) {
797ad32e9c0SJosh Durgin         return r;
798f27aaf4bSChristian Brunner     }
799f27aaf4bSChristian Brunner 
800ad32e9c0SJosh Durgin     return info.size;
801ad32e9c0SJosh Durgin }
802ad32e9c0SJosh Durgin 
80330cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
80430cdc48cSJosh Durgin {
80530cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
80630cdc48cSJosh Durgin     int r;
80730cdc48cSJosh Durgin 
80830cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
80930cdc48cSJosh Durgin     if (r < 0) {
81030cdc48cSJosh Durgin         return r;
81130cdc48cSJosh Durgin     }
81230cdc48cSJosh Durgin 
81330cdc48cSJosh Durgin     return 0;
81430cdc48cSJosh Durgin }
81530cdc48cSJosh Durgin 
816ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
817ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
818f27aaf4bSChristian Brunner {
819f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
820f27aaf4bSChristian Brunner     int r;
821f27aaf4bSChristian Brunner 
822f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
823f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
824f27aaf4bSChristian Brunner     }
825f27aaf4bSChristian Brunner 
826f27aaf4bSChristian Brunner     /*
827f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
828f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
829f27aaf4bSChristian Brunner      */
830f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
831f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
832f27aaf4bSChristian Brunner         return -EINVAL;
833f27aaf4bSChristian Brunner     }
834f27aaf4bSChristian Brunner 
835f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
836f27aaf4bSChristian Brunner         return -ERANGE;
837f27aaf4bSChristian Brunner     }
838f27aaf4bSChristian Brunner 
839ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
840f27aaf4bSChristian Brunner     if (r < 0) {
841ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
842f27aaf4bSChristian Brunner         return r;
843f27aaf4bSChristian Brunner     }
844f27aaf4bSChristian Brunner 
845f27aaf4bSChristian Brunner     return 0;
846f27aaf4bSChristian Brunner }
847f27aaf4bSChristian Brunner 
848bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
849a89d89d3SWenchao Xia                                 const char *snapshot_id,
850a89d89d3SWenchao Xia                                 const char *snapshot_name,
851a89d89d3SWenchao Xia                                 Error **errp)
852bd603247SGregory Farnum {
853bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
854bd603247SGregory Farnum     int r;
855bd603247SGregory Farnum 
856a89d89d3SWenchao Xia     if (!snapshot_name) {
857a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
858a89d89d3SWenchao Xia         return -EINVAL;
859a89d89d3SWenchao Xia     }
860a89d89d3SWenchao Xia 
861a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
862a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
863a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
864a89d89d3SWenchao Xia         error_setg(errp,
865a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
866a89d89d3SWenchao Xia                    "equal to snapshot name");
867a89d89d3SWenchao Xia         return -EINVAL;
868a89d89d3SWenchao Xia     }
869a89d89d3SWenchao Xia 
870bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
871a89d89d3SWenchao Xia     if (r < 0) {
872a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
873a89d89d3SWenchao Xia     }
874bd603247SGregory Farnum     return r;
875bd603247SGregory Farnum }
876bd603247SGregory Farnum 
877bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
878bd603247SGregory Farnum                                   const char *snapshot_name)
879bd603247SGregory Farnum {
880bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
881bd603247SGregory Farnum 
8829be38598SEduardo Habkost     return rbd_snap_rollback(s->image, snapshot_name);
883bd603247SGregory Farnum }
884bd603247SGregory Farnum 
885ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
886ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
887f27aaf4bSChristian Brunner {
888f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
889f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
890ad32e9c0SJosh Durgin     int i, snap_count;
891ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
892ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
893f27aaf4bSChristian Brunner 
894ad32e9c0SJosh Durgin     do {
89502c4f26bSMarkus Armbruster         snaps = g_new(rbd_snap_info_t, max_snaps);
896ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
8979e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
8987267c094SAnthony Liguori             g_free(snaps);
899f27aaf4bSChristian Brunner         }
900ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
901f27aaf4bSChristian Brunner 
902ad32e9c0SJosh Durgin     if (snap_count <= 0) {
903b9c53290SJosh Durgin         goto done;
904f27aaf4bSChristian Brunner     }
905f27aaf4bSChristian Brunner 
9065839e53bSMarkus Armbruster     sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
907f27aaf4bSChristian Brunner 
908ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
909ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
910f27aaf4bSChristian Brunner 
911f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
912f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
913f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
914f27aaf4bSChristian Brunner 
915ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
916f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
917f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
918f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
919f27aaf4bSChristian Brunner     }
920ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
9219e6337d0SStefan Hajnoczi     g_free(snaps);
922ad32e9c0SJosh Durgin 
923b9c53290SJosh Durgin  done:
924f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
925f27aaf4bSChristian Brunner     return snap_count;
926f27aaf4bSChristian Brunner }
927f27aaf4bSChristian Brunner 
928787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
9294da444a0SEric Blake static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
9304da444a0SEric Blake                                          int64_t offset,
9314da444a0SEric Blake                                          int count,
932097310b5SMarkus Armbruster                                          BlockCompletionFunc *cb,
933787f3133SJosh Durgin                                          void *opaque)
934787f3133SJosh Durgin {
9354da444a0SEric Blake     return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
936787f3133SJosh Durgin                          RBD_AIO_DISCARD);
937787f3133SJosh Durgin }
938787f3133SJosh Durgin #endif
939787f3133SJosh Durgin 
940be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
941be217884SAdam Crume static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
942be217884SAdam Crume                                       Error **errp)
943be217884SAdam Crume {
944be217884SAdam Crume     BDRVRBDState *s = bs->opaque;
945be217884SAdam Crume     int r = rbd_invalidate_cache(s->image);
946be217884SAdam Crume     if (r < 0) {
947be217884SAdam Crume         error_setg_errno(errp, -r, "Failed to invalidate the cache");
948be217884SAdam Crume     }
949be217884SAdam Crume }
950be217884SAdam Crume #endif
951be217884SAdam Crume 
952bd0cf596SChunyan Liu static QemuOptsList qemu_rbd_create_opts = {
953bd0cf596SChunyan Liu     .name = "rbd-create-opts",
954bd0cf596SChunyan Liu     .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
955bd0cf596SChunyan Liu     .desc = {
956f27aaf4bSChristian Brunner         {
957f27aaf4bSChristian Brunner             .name = BLOCK_OPT_SIZE,
958bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
959f27aaf4bSChristian Brunner             .help = "Virtual disk size"
960f27aaf4bSChristian Brunner         },
961f27aaf4bSChristian Brunner         {
962f27aaf4bSChristian Brunner             .name = BLOCK_OPT_CLUSTER_SIZE,
963bd0cf596SChunyan Liu             .type = QEMU_OPT_SIZE,
964f27aaf4bSChristian Brunner             .help = "RBD object size"
965f27aaf4bSChristian Brunner         },
96660390a21SDaniel P. Berrange         {
96760390a21SDaniel P. Berrange             .name = "password-secret",
96860390a21SDaniel P. Berrange             .type = QEMU_OPT_STRING,
96960390a21SDaniel P. Berrange             .help = "ID of secret providing the password",
97060390a21SDaniel P. Berrange         },
971bd0cf596SChunyan Liu         { /* end of list */ }
972bd0cf596SChunyan Liu     }
973f27aaf4bSChristian Brunner };
974f27aaf4bSChristian Brunner 
975f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
976f27aaf4bSChristian Brunner     .format_name        = "rbd",
977f27aaf4bSChristian Brunner     .instance_size      = sizeof(BDRVRBDState),
978030be321SBenoît Canet     .bdrv_needs_filename = true,
979ad32e9c0SJosh Durgin     .bdrv_file_open     = qemu_rbd_open,
980ad32e9c0SJosh Durgin     .bdrv_close         = qemu_rbd_close,
981c282e1fdSChunyan Liu     .bdrv_create        = qemu_rbd_create,
9823ac21627SPeter Lieven     .bdrv_has_zero_init = bdrv_has_zero_init_1,
983ad32e9c0SJosh Durgin     .bdrv_get_info      = qemu_rbd_getinfo,
984bd0cf596SChunyan Liu     .create_opts        = &qemu_rbd_create_opts,
985ad32e9c0SJosh Durgin     .bdrv_getlength     = qemu_rbd_getlength,
98630cdc48cSJosh Durgin     .bdrv_truncate      = qemu_rbd_truncate,
987f27aaf4bSChristian Brunner     .protocol_name      = "rbd",
988f27aaf4bSChristian Brunner 
989ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
990ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
991dc7588c1SJosh Durgin 
992dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
993dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
994dc7588c1SJosh Durgin #else
995c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
996dc7588c1SJosh Durgin #endif
997f27aaf4bSChristian Brunner 
998787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
9994da444a0SEric Blake     .bdrv_aio_pdiscard      = qemu_rbd_aio_pdiscard,
1000787f3133SJosh Durgin #endif
1001787f3133SJosh Durgin 
1002ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
1003bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
1004ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
1005bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
1006be217884SAdam Crume #ifdef LIBRBD_SUPPORTS_INVALIDATE
1007be217884SAdam Crume     .bdrv_invalidate_cache  = qemu_rbd_invalidate_cache,
1008be217884SAdam Crume #endif
1009f27aaf4bSChristian Brunner };
1010f27aaf4bSChristian Brunner 
1011f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
1012f27aaf4bSChristian Brunner {
1013f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
1014f27aaf4bSChristian Brunner }
1015f27aaf4bSChristian Brunner 
1016f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
1017