xref: /openbmc/qemu/block/rbd.c (revision 84d18f065fb041a1c0d78d20320d740ae0673c8a)
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 
14ad32e9c0SJosh Durgin #include <inttypes.h>
15ad32e9c0SJosh Durgin 
16f27aaf4bSChristian Brunner #include "qemu-common.h"
171de7afc9SPaolo Bonzini #include "qemu/error-report.h"
18737e150eSPaolo Bonzini #include "block/block_int.h"
19f27aaf4bSChristian Brunner 
20ad32e9c0SJosh Durgin #include <rbd/librbd.h>
21f27aaf4bSChristian Brunner 
22f27aaf4bSChristian Brunner /*
23f27aaf4bSChristian Brunner  * When specifying the image filename use:
24f27aaf4bSChristian Brunner  *
25fab5cf59SJosh Durgin  * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
26f27aaf4bSChristian Brunner  *
279e1fbcdeSSage Weil  * poolname must be the name of an existing rados pool.
28f27aaf4bSChristian Brunner  *
299e1fbcdeSSage Weil  * devicename is the name of the rbd image.
30f27aaf4bSChristian Brunner  *
319e1fbcdeSSage Weil  * Each option given is used to configure rados, and may be any valid
329e1fbcdeSSage Weil  * Ceph option, "id", or "conf".
33fab5cf59SJosh Durgin  *
349e1fbcdeSSage Weil  * The "id" option indicates what user we should authenticate as to
359e1fbcdeSSage Weil  * the Ceph cluster.  If it is excluded we will use the Ceph default
369e1fbcdeSSage Weil  * (normally 'admin').
37f27aaf4bSChristian Brunner  *
389e1fbcdeSSage Weil  * The "conf" option specifies a Ceph configuration file to read.  If
399e1fbcdeSSage Weil  * it is not specified, we will read from the default Ceph locations
409e1fbcdeSSage Weil  * (e.g., /etc/ceph/ceph.conf).  To avoid reading _any_ configuration
419e1fbcdeSSage Weil  * file, specify conf=/dev/null.
42f27aaf4bSChristian Brunner  *
439e1fbcdeSSage Weil  * Configuration values containing :, @, or = can be escaped with a
449e1fbcdeSSage Weil  * leading "\".
45f27aaf4bSChristian Brunner  */
46f27aaf4bSChristian Brunner 
47787f3133SJosh Durgin /* rbd_aio_discard added in 0.1.2 */
48787f3133SJosh Durgin #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
49787f3133SJosh Durgin #define LIBRBD_SUPPORTS_DISCARD
50787f3133SJosh Durgin #else
51787f3133SJosh Durgin #undef LIBRBD_SUPPORTS_DISCARD
52787f3133SJosh Durgin #endif
53787f3133SJosh Durgin 
54f27aaf4bSChristian Brunner #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
55f27aaf4bSChristian Brunner 
56ad32e9c0SJosh Durgin #define RBD_MAX_CONF_NAME_SIZE 128
57ad32e9c0SJosh Durgin #define RBD_MAX_CONF_VAL_SIZE 512
58ad32e9c0SJosh Durgin #define RBD_MAX_CONF_SIZE 1024
59ad32e9c0SJosh Durgin #define RBD_MAX_POOL_NAME_SIZE 128
60ad32e9c0SJosh Durgin #define RBD_MAX_SNAP_NAME_SIZE 128
61ad32e9c0SJosh Durgin #define RBD_MAX_SNAPS 100
62ad32e9c0SJosh Durgin 
63787f3133SJosh Durgin typedef enum {
64787f3133SJosh Durgin     RBD_AIO_READ,
65787f3133SJosh Durgin     RBD_AIO_WRITE,
66dc7588c1SJosh Durgin     RBD_AIO_DISCARD,
67dc7588c1SJosh Durgin     RBD_AIO_FLUSH
68787f3133SJosh Durgin } RBDAIOCmd;
69787f3133SJosh Durgin 
70f27aaf4bSChristian Brunner typedef struct RBDAIOCB {
71f27aaf4bSChristian Brunner     BlockDriverAIOCB common;
72f27aaf4bSChristian Brunner     QEMUBH *bh;
7308448d51SStefan Priebe     int64_t ret;
74f27aaf4bSChristian Brunner     QEMUIOVector *qiov;
75f27aaf4bSChristian Brunner     char *bounce;
76787f3133SJosh Durgin     RBDAIOCmd cmd;
77f27aaf4bSChristian Brunner     int64_t sector_num;
78f27aaf4bSChristian Brunner     int error;
79f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
80f27aaf4bSChristian Brunner     int cancelled;
81473c7f02SStefan Priebe     int status;
82f27aaf4bSChristian Brunner } RBDAIOCB;
83f27aaf4bSChristian Brunner 
84f27aaf4bSChristian Brunner typedef struct RADOSCB {
85f27aaf4bSChristian Brunner     int rcbid;
86f27aaf4bSChristian Brunner     RBDAIOCB *acb;
87f27aaf4bSChristian Brunner     struct BDRVRBDState *s;
88f27aaf4bSChristian Brunner     int done;
89ad32e9c0SJosh Durgin     int64_t size;
90f27aaf4bSChristian Brunner     char *buf;
9108448d51SStefan Priebe     int64_t ret;
92f27aaf4bSChristian Brunner } RADOSCB;
93f27aaf4bSChristian Brunner 
94f27aaf4bSChristian Brunner #define RBD_FD_READ 0
95f27aaf4bSChristian Brunner #define RBD_FD_WRITE 1
96f27aaf4bSChristian Brunner 
97f27aaf4bSChristian Brunner typedef struct BDRVRBDState {
98ad32e9c0SJosh Durgin     rados_t cluster;
99ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
100ad32e9c0SJosh Durgin     rbd_image_t image;
101ad32e9c0SJosh Durgin     char name[RBD_MAX_IMAGE_NAME_SIZE];
102ad32e9c0SJosh Durgin     char *snap;
103f27aaf4bSChristian Brunner } BDRVRBDState;
104f27aaf4bSChristian Brunner 
105ad32e9c0SJosh Durgin static int qemu_rbd_next_tok(char *dst, int dst_len,
106f27aaf4bSChristian Brunner                              char *src, char delim,
107f27aaf4bSChristian Brunner                              const char *name,
108f27aaf4bSChristian Brunner                              char **p)
109f27aaf4bSChristian Brunner {
110f27aaf4bSChristian Brunner     int l;
111f27aaf4bSChristian Brunner     char *end;
112f27aaf4bSChristian Brunner 
113f27aaf4bSChristian Brunner     *p = NULL;
114f27aaf4bSChristian Brunner 
115f27aaf4bSChristian Brunner     if (delim != '\0') {
11616a06b24SSage Weil         for (end = src; *end; ++end) {
11716a06b24SSage Weil             if (*end == delim) {
11816a06b24SSage Weil                 break;
11916a06b24SSage Weil             }
12016a06b24SSage Weil             if (*end == '\\' && end[1] != '\0') {
12116a06b24SSage Weil                 end++;
12216a06b24SSage Weil             }
12316a06b24SSage Weil         }
12416a06b24SSage Weil         if (*end == delim) {
125f27aaf4bSChristian Brunner             *p = end + 1;
126f27aaf4bSChristian Brunner             *end = '\0';
127f27aaf4bSChristian Brunner         }
128f27aaf4bSChristian Brunner     }
129f27aaf4bSChristian Brunner     l = strlen(src);
130f27aaf4bSChristian Brunner     if (l >= dst_len) {
131f27aaf4bSChristian Brunner         error_report("%s too long", name);
132f27aaf4bSChristian Brunner         return -EINVAL;
133f27aaf4bSChristian Brunner     } else if (l == 0) {
134f27aaf4bSChristian Brunner         error_report("%s too short", name);
135f27aaf4bSChristian Brunner         return -EINVAL;
136f27aaf4bSChristian Brunner     }
137f27aaf4bSChristian Brunner 
138f27aaf4bSChristian Brunner     pstrcpy(dst, dst_len, src);
139f27aaf4bSChristian Brunner 
140f27aaf4bSChristian Brunner     return 0;
141f27aaf4bSChristian Brunner }
142f27aaf4bSChristian Brunner 
14316a06b24SSage Weil static void qemu_rbd_unescape(char *src)
14416a06b24SSage Weil {
14516a06b24SSage Weil     char *p;
14616a06b24SSage Weil 
14716a06b24SSage Weil     for (p = src; *src; ++src, ++p) {
14816a06b24SSage Weil         if (*src == '\\' && src[1] != '\0') {
14916a06b24SSage Weil             src++;
15016a06b24SSage Weil         }
15116a06b24SSage Weil         *p = *src;
15216a06b24SSage Weil     }
15316a06b24SSage Weil     *p = '\0';
15416a06b24SSage Weil }
15516a06b24SSage Weil 
156ad32e9c0SJosh Durgin static int qemu_rbd_parsename(const char *filename,
157f27aaf4bSChristian Brunner                               char *pool, int pool_len,
158f27aaf4bSChristian Brunner                               char *snap, int snap_len,
159fab5cf59SJosh Durgin                               char *name, int name_len,
160fab5cf59SJosh Durgin                               char *conf, int conf_len)
161f27aaf4bSChristian Brunner {
162f27aaf4bSChristian Brunner     const char *start;
163f27aaf4bSChristian Brunner     char *p, *buf;
164f27aaf4bSChristian Brunner     int ret;
165f27aaf4bSChristian Brunner 
166f27aaf4bSChristian Brunner     if (!strstart(filename, "rbd:", &start)) {
167f27aaf4bSChristian Brunner         return -EINVAL;
168f27aaf4bSChristian Brunner     }
169f27aaf4bSChristian Brunner 
1707267c094SAnthony Liguori     buf = g_strdup(start);
171f27aaf4bSChristian Brunner     p = buf;
172fab5cf59SJosh Durgin     *snap = '\0';
173fab5cf59SJosh Durgin     *conf = '\0';
174f27aaf4bSChristian Brunner 
175ad32e9c0SJosh Durgin     ret = qemu_rbd_next_tok(pool, pool_len, p, '/', "pool name", &p);
176f27aaf4bSChristian Brunner     if (ret < 0 || !p) {
177f27aaf4bSChristian Brunner         ret = -EINVAL;
178f27aaf4bSChristian Brunner         goto done;
179f27aaf4bSChristian Brunner     }
18016a06b24SSage Weil     qemu_rbd_unescape(pool);
181fab5cf59SJosh Durgin 
182fab5cf59SJosh Durgin     if (strchr(p, '@')) {
183ad32e9c0SJosh Durgin         ret = qemu_rbd_next_tok(name, name_len, p, '@', "object name", &p);
184f27aaf4bSChristian Brunner         if (ret < 0) {
185f27aaf4bSChristian Brunner             goto done;
186f27aaf4bSChristian Brunner         }
187fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(snap, snap_len, p, ':', "snap name", &p);
18816a06b24SSage Weil         qemu_rbd_unescape(snap);
189fab5cf59SJosh Durgin     } else {
190fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(name, name_len, p, ':', "object name", &p);
191fab5cf59SJosh Durgin     }
19216a06b24SSage Weil     qemu_rbd_unescape(name);
193fab5cf59SJosh Durgin     if (ret < 0 || !p) {
194f27aaf4bSChristian Brunner         goto done;
195f27aaf4bSChristian Brunner     }
196f27aaf4bSChristian Brunner 
197fab5cf59SJosh Durgin     ret = qemu_rbd_next_tok(conf, conf_len, p, '\0', "configuration", &p);
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 
232fab5cf59SJosh Durgin static int qemu_rbd_set_conf(rados_t cluster, const char *conf)
233fab5cf59SJosh Durgin {
234fab5cf59SJosh Durgin     char *p, *buf;
235fab5cf59SJosh Durgin     char name[RBD_MAX_CONF_NAME_SIZE];
236fab5cf59SJosh Durgin     char value[RBD_MAX_CONF_VAL_SIZE];
237fab5cf59SJosh Durgin     int ret = 0;
238fab5cf59SJosh Durgin 
2397267c094SAnthony Liguori     buf = g_strdup(conf);
240fab5cf59SJosh Durgin     p = buf;
241fab5cf59SJosh Durgin 
242fab5cf59SJosh Durgin     while (p) {
243fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(name, sizeof(name), p,
244fab5cf59SJosh Durgin                                 '=', "conf option name", &p);
245fab5cf59SJosh Durgin         if (ret < 0) {
246fab5cf59SJosh Durgin             break;
247fab5cf59SJosh Durgin         }
24816a06b24SSage Weil         qemu_rbd_unescape(name);
249fab5cf59SJosh Durgin 
250fab5cf59SJosh Durgin         if (!p) {
251fab5cf59SJosh Durgin             error_report("conf option %s has no value", name);
252fab5cf59SJosh Durgin             ret = -EINVAL;
253fab5cf59SJosh Durgin             break;
254fab5cf59SJosh Durgin         }
255fab5cf59SJosh Durgin 
256fab5cf59SJosh Durgin         ret = qemu_rbd_next_tok(value, sizeof(value), p,
257fab5cf59SJosh Durgin                                 ':', "conf option value", &p);
258fab5cf59SJosh Durgin         if (ret < 0) {
259fab5cf59SJosh Durgin             break;
260fab5cf59SJosh Durgin         }
26116a06b24SSage Weil         qemu_rbd_unescape(value);
262fab5cf59SJosh Durgin 
2637c7e9df0SSage Weil         if (strcmp(name, "conf") == 0) {
2647c7e9df0SSage Weil             ret = rados_conf_read_file(cluster, value);
2657c7e9df0SSage Weil             if (ret < 0) {
2667c7e9df0SSage Weil                 error_report("error reading conf file %s", value);
2677c7e9df0SSage Weil                 break;
2687c7e9df0SSage Weil             }
2697c7e9df0SSage Weil         } else if (strcmp(name, "id") == 0) {
2707c7e9df0SSage Weil             /* ignore, this is parsed by qemu_rbd_parse_clientname() */
2717c7e9df0SSage Weil         } else {
272fab5cf59SJosh Durgin             ret = rados_conf_set(cluster, name, value);
273fab5cf59SJosh Durgin             if (ret < 0) {
274fab5cf59SJosh Durgin                 error_report("invalid conf option %s", name);
275fab5cf59SJosh Durgin                 ret = -EINVAL;
276fab5cf59SJosh Durgin                 break;
277fab5cf59SJosh Durgin             }
278fab5cf59SJosh Durgin         }
279fab5cf59SJosh Durgin     }
280fab5cf59SJosh Durgin 
2817267c094SAnthony Liguori     g_free(buf);
282fab5cf59SJosh Durgin     return ret;
283fab5cf59SJosh Durgin }
284fab5cf59SJosh Durgin 
285d5124c00SMax Reitz static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options,
286d5124c00SMax Reitz                            Error **errp)
287f27aaf4bSChristian Brunner {
288f27aaf4bSChristian Brunner     int64_t bytes = 0;
289f27aaf4bSChristian Brunner     int64_t objsize;
290ad32e9c0SJosh Durgin     int obj_order = 0;
291ad32e9c0SJosh Durgin     char pool[RBD_MAX_POOL_NAME_SIZE];
292ad32e9c0SJosh Durgin     char name[RBD_MAX_IMAGE_NAME_SIZE];
293ad32e9c0SJosh Durgin     char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
294fab5cf59SJosh Durgin     char conf[RBD_MAX_CONF_SIZE];
2957c7e9df0SSage Weil     char clientname_buf[RBD_MAX_CONF_SIZE];
2967c7e9df0SSage Weil     char *clientname;
297ad32e9c0SJosh Durgin     rados_t cluster;
298ad32e9c0SJosh Durgin     rados_ioctx_t io_ctx;
299f27aaf4bSChristian Brunner     int ret;
300f27aaf4bSChristian Brunner 
301ad32e9c0SJosh Durgin     if (qemu_rbd_parsename(filename, pool, sizeof(pool),
302f27aaf4bSChristian Brunner                            snap_buf, sizeof(snap_buf),
303fab5cf59SJosh Durgin                            name, sizeof(name),
304fab5cf59SJosh Durgin                            conf, sizeof(conf)) < 0) {
305f27aaf4bSChristian Brunner         return -EINVAL;
306f27aaf4bSChristian Brunner     }
307f27aaf4bSChristian Brunner 
308f27aaf4bSChristian Brunner     /* Read out options */
309f27aaf4bSChristian Brunner     while (options && options->name) {
310f27aaf4bSChristian Brunner         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
311f27aaf4bSChristian Brunner             bytes = options->value.n;
312f27aaf4bSChristian Brunner         } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
313f27aaf4bSChristian Brunner             if (options->value.n) {
314f27aaf4bSChristian Brunner                 objsize = options->value.n;
315f27aaf4bSChristian Brunner                 if ((objsize - 1) & objsize) {    /* not a power of 2? */
316f27aaf4bSChristian Brunner                     error_report("obj size needs to be power of 2");
317f27aaf4bSChristian Brunner                     return -EINVAL;
318f27aaf4bSChristian Brunner                 }
319f27aaf4bSChristian Brunner                 if (objsize < 4096) {
320f27aaf4bSChristian Brunner                     error_report("obj size too small");
321f27aaf4bSChristian Brunner                     return -EINVAL;
322f27aaf4bSChristian Brunner                 }
323f27aaf4bSChristian Brunner                 obj_order = ffs(objsize) - 1;
324f27aaf4bSChristian Brunner             }
325f27aaf4bSChristian Brunner         }
326f27aaf4bSChristian Brunner         options++;
327f27aaf4bSChristian Brunner     }
328f27aaf4bSChristian Brunner 
3297c7e9df0SSage Weil     clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
3307c7e9df0SSage Weil     if (rados_create(&cluster, clientname) < 0) {
331f27aaf4bSChristian Brunner         error_report("error initializing");
332f27aaf4bSChristian Brunner         return -EIO;
333f27aaf4bSChristian Brunner     }
334f27aaf4bSChristian Brunner 
335fab5cf59SJosh Durgin     if (strstr(conf, "conf=") == NULL) {
336f9fe18ecSSage Weil         /* try default location, but ignore failure */
337f9fe18ecSSage Weil         rados_conf_read_file(cluster, NULL);
338fab5cf59SJosh Durgin     }
339fab5cf59SJosh Durgin 
340fab5cf59SJosh Durgin     if (conf[0] != '\0' &&
341fab5cf59SJosh Durgin         qemu_rbd_set_conf(cluster, conf) < 0) {
342fab5cf59SJosh Durgin         error_report("error setting config options");
343fab5cf59SJosh Durgin         rados_shutdown(cluster);
344fab5cf59SJosh Durgin         return -EIO;
345fab5cf59SJosh Durgin     }
346ad32e9c0SJosh Durgin 
347ad32e9c0SJosh Durgin     if (rados_connect(cluster) < 0) {
348ad32e9c0SJosh Durgin         error_report("error connecting");
349ad32e9c0SJosh Durgin         rados_shutdown(cluster);
350ad32e9c0SJosh Durgin         return -EIO;
351ad32e9c0SJosh Durgin     }
352ad32e9c0SJosh Durgin 
353ad32e9c0SJosh Durgin     if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
354f27aaf4bSChristian Brunner         error_report("error opening pool %s", pool);
355ad32e9c0SJosh Durgin         rados_shutdown(cluster);
356f27aaf4bSChristian Brunner         return -EIO;
357f27aaf4bSChristian Brunner     }
358f27aaf4bSChristian Brunner 
359ad32e9c0SJosh Durgin     ret = rbd_create(io_ctx, name, bytes, &obj_order);
360ad32e9c0SJosh Durgin     rados_ioctx_destroy(io_ctx);
361ad32e9c0SJosh Durgin     rados_shutdown(cluster);
362f27aaf4bSChristian Brunner 
363f27aaf4bSChristian Brunner     return ret;
364f27aaf4bSChristian Brunner }
365f27aaf4bSChristian Brunner 
366f27aaf4bSChristian Brunner /*
367e04fb07fSStefan Hajnoczi  * This aio completion is being called from rbd_finish_bh() and runs in qemu
368e04fb07fSStefan Hajnoczi  * BH context.
369f27aaf4bSChristian Brunner  */
370ad32e9c0SJosh Durgin static void qemu_rbd_complete_aio(RADOSCB *rcb)
371f27aaf4bSChristian Brunner {
372f27aaf4bSChristian Brunner     RBDAIOCB *acb = rcb->acb;
373f27aaf4bSChristian Brunner     int64_t r;
374f27aaf4bSChristian Brunner 
375f27aaf4bSChristian Brunner     r = rcb->ret;
376f27aaf4bSChristian Brunner 
377dc7588c1SJosh Durgin     if (acb->cmd != RBD_AIO_READ) {
378f27aaf4bSChristian Brunner         if (r < 0) {
379f27aaf4bSChristian Brunner             acb->ret = r;
380f27aaf4bSChristian Brunner             acb->error = 1;
381f27aaf4bSChristian Brunner         } else if (!acb->error) {
382ad32e9c0SJosh Durgin             acb->ret = rcb->size;
383f27aaf4bSChristian Brunner         }
384f27aaf4bSChristian Brunner     } else {
385ad32e9c0SJosh Durgin         if (r < 0) {
386ad32e9c0SJosh Durgin             memset(rcb->buf, 0, rcb->size);
387f27aaf4bSChristian Brunner             acb->ret = r;
388f27aaf4bSChristian Brunner             acb->error = 1;
389ad32e9c0SJosh Durgin         } else if (r < rcb->size) {
390ad32e9c0SJosh Durgin             memset(rcb->buf + r, 0, rcb->size - r);
391f27aaf4bSChristian Brunner             if (!acb->error) {
392ad32e9c0SJosh Durgin                 acb->ret = rcb->size;
393f27aaf4bSChristian Brunner             }
394f27aaf4bSChristian Brunner         } else if (!acb->error) {
395ad32e9c0SJosh Durgin             acb->ret = r;
396f27aaf4bSChristian Brunner         }
397f27aaf4bSChristian Brunner     }
398e04fb07fSStefan Hajnoczi 
3997267c094SAnthony Liguori     g_free(rcb);
400e04fb07fSStefan Hajnoczi 
401e04fb07fSStefan Hajnoczi     if (acb->cmd == RBD_AIO_READ) {
402e04fb07fSStefan Hajnoczi         qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
403f27aaf4bSChristian Brunner     }
404e04fb07fSStefan Hajnoczi     qemu_vfree(acb->bounce);
405e04fb07fSStefan Hajnoczi     acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
406e04fb07fSStefan Hajnoczi     acb->status = 0;
407f27aaf4bSChristian Brunner 
408e04fb07fSStefan Hajnoczi     if (!acb->cancelled) {
409e04fb07fSStefan Hajnoczi         qemu_aio_release(acb);
410f27aaf4bSChristian Brunner     }
411f27aaf4bSChristian Brunner }
412f27aaf4bSChristian Brunner 
413a9ccedc3SKevin Wolf /* TODO Convert to fine grained options */
414a9ccedc3SKevin Wolf static QemuOptsList runtime_opts = {
415a9ccedc3SKevin Wolf     .name = "rbd",
416a9ccedc3SKevin Wolf     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
417a9ccedc3SKevin Wolf     .desc = {
418a9ccedc3SKevin Wolf         {
419a9ccedc3SKevin Wolf             .name = "filename",
420a9ccedc3SKevin Wolf             .type = QEMU_OPT_STRING,
421a9ccedc3SKevin Wolf             .help = "Specification of the rbd image",
422a9ccedc3SKevin Wolf         },
423a9ccedc3SKevin Wolf         { /* end of list */ }
424a9ccedc3SKevin Wolf     },
425a9ccedc3SKevin Wolf };
426a9ccedc3SKevin Wolf 
427015a1036SMax Reitz static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
428015a1036SMax Reitz                          Error **errp)
429f27aaf4bSChristian Brunner {
430f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
431ad32e9c0SJosh Durgin     char pool[RBD_MAX_POOL_NAME_SIZE];
432ad32e9c0SJosh Durgin     char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
433fab5cf59SJosh Durgin     char conf[RBD_MAX_CONF_SIZE];
4347c7e9df0SSage Weil     char clientname_buf[RBD_MAX_CONF_SIZE];
4357c7e9df0SSage Weil     char *clientname;
436a9ccedc3SKevin Wolf     QemuOpts *opts;
437a9ccedc3SKevin Wolf     Error *local_err = NULL;
438a9ccedc3SKevin Wolf     const char *filename;
439f27aaf4bSChristian Brunner     int r;
440f27aaf4bSChristian Brunner 
44187ea75d5SPeter Crosthwaite     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
442a9ccedc3SKevin Wolf     qemu_opts_absorb_qdict(opts, options, &local_err);
443*84d18f06SMarkus Armbruster     if (local_err) {
444a9ccedc3SKevin Wolf         qerror_report_err(local_err);
445a9ccedc3SKevin Wolf         error_free(local_err);
446a9ccedc3SKevin Wolf         qemu_opts_del(opts);
447a9ccedc3SKevin Wolf         return -EINVAL;
448a9ccedc3SKevin Wolf     }
449a9ccedc3SKevin Wolf 
450a9ccedc3SKevin Wolf     filename = qemu_opt_get(opts, "filename");
451a9ccedc3SKevin Wolf 
452ad32e9c0SJosh Durgin     if (qemu_rbd_parsename(filename, pool, sizeof(pool),
453f27aaf4bSChristian Brunner                            snap_buf, sizeof(snap_buf),
454fab5cf59SJosh Durgin                            s->name, sizeof(s->name),
455fab5cf59SJosh Durgin                            conf, sizeof(conf)) < 0) {
456c3ca988dSKevin Wolf         r = -EINVAL;
457c3ca988dSKevin Wolf         goto failed_opts;
458f27aaf4bSChristian Brunner     }
459f27aaf4bSChristian Brunner 
4607c7e9df0SSage Weil     clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
4617c7e9df0SSage Weil     r = rados_create(&s->cluster, clientname);
462ad32e9c0SJosh Durgin     if (r < 0) {
463f27aaf4bSChristian Brunner         error_report("error initializing");
464c3ca988dSKevin Wolf         goto failed_opts;
465f27aaf4bSChristian Brunner     }
466f27aaf4bSChristian Brunner 
467eb93d5d9SSage Weil     s->snap = NULL;
468eb93d5d9SSage Weil     if (snap_buf[0] != '\0') {
469eb93d5d9SSage Weil         s->snap = g_strdup(snap_buf);
470eb93d5d9SSage Weil     }
471eb93d5d9SSage Weil 
472b11f38fcSJosh Durgin     /*
473b11f38fcSJosh Durgin      * Fallback to more conservative semantics if setting cache
474b11f38fcSJosh Durgin      * options fails. Ignore errors from setting rbd_cache because the
475b11f38fcSJosh Durgin      * only possible error is that the option does not exist, and
476b11f38fcSJosh Durgin      * librbd defaults to no caching. If write through caching cannot
477b11f38fcSJosh Durgin      * be set up, fall back to no caching.
478b11f38fcSJosh Durgin      */
479b11f38fcSJosh Durgin     if (flags & BDRV_O_NOCACHE) {
480b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "false");
481b11f38fcSJosh Durgin     } else {
482b11f38fcSJosh Durgin         rados_conf_set(s->cluster, "rbd_cache", "true");
483b11f38fcSJosh Durgin     }
484b11f38fcSJosh Durgin 
485fab5cf59SJosh Durgin     if (strstr(conf, "conf=") == NULL) {
486f9fe18ecSSage Weil         /* try default location, but ignore failure */
487f9fe18ecSSage Weil         rados_conf_read_file(s->cluster, NULL);
488fab5cf59SJosh Durgin     }
489fab5cf59SJosh Durgin 
490fab5cf59SJosh Durgin     if (conf[0] != '\0') {
491fab5cf59SJosh Durgin         r = qemu_rbd_set_conf(s->cluster, conf);
492fab5cf59SJosh Durgin         if (r < 0) {
493fab5cf59SJosh Durgin             error_report("error setting config options");
494eb93d5d9SSage Weil             goto failed_shutdown;
495fab5cf59SJosh Durgin         }
496fab5cf59SJosh Durgin     }
497f27aaf4bSChristian Brunner 
498ad32e9c0SJosh Durgin     r = rados_connect(s->cluster);
499ad32e9c0SJosh Durgin     if (r < 0) {
500ad32e9c0SJosh Durgin         error_report("error connecting");
501eb93d5d9SSage Weil         goto failed_shutdown;
502ad32e9c0SJosh Durgin     }
503ad32e9c0SJosh Durgin 
504ad32e9c0SJosh Durgin     r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
505ad32e9c0SJosh Durgin     if (r < 0) {
506ad32e9c0SJosh Durgin         error_report("error opening pool %s", pool);
507eb93d5d9SSage Weil         goto failed_shutdown;
508ad32e9c0SJosh Durgin     }
509ad32e9c0SJosh Durgin 
510ad32e9c0SJosh Durgin     r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
511ad32e9c0SJosh Durgin     if (r < 0) {
512ad32e9c0SJosh Durgin         error_report("error reading header from %s", s->name);
513eb93d5d9SSage Weil         goto failed_open;
514ad32e9c0SJosh Durgin     }
515ad32e9c0SJosh Durgin 
516ad32e9c0SJosh Durgin     bs->read_only = (s->snap != NULL);
517f27aaf4bSChristian Brunner 
518c3ca988dSKevin Wolf     qemu_opts_del(opts);
519f27aaf4bSChristian Brunner     return 0;
520f27aaf4bSChristian Brunner 
521eb93d5d9SSage Weil failed_open:
522ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
523eb93d5d9SSage Weil failed_shutdown:
524ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
525eb93d5d9SSage Weil     g_free(s->snap);
526c3ca988dSKevin Wolf failed_opts:
527c3ca988dSKevin Wolf     qemu_opts_del(opts);
528f27aaf4bSChristian Brunner     return r;
529f27aaf4bSChristian Brunner }
530f27aaf4bSChristian Brunner 
531ad32e9c0SJosh Durgin static void qemu_rbd_close(BlockDriverState *bs)
532f27aaf4bSChristian Brunner {
533f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
534f27aaf4bSChristian Brunner 
535ad32e9c0SJosh Durgin     rbd_close(s->image);
536ad32e9c0SJosh Durgin     rados_ioctx_destroy(s->io_ctx);
5377267c094SAnthony Liguori     g_free(s->snap);
538ad32e9c0SJosh Durgin     rados_shutdown(s->cluster);
539f27aaf4bSChristian Brunner }
540f27aaf4bSChristian Brunner 
541f27aaf4bSChristian Brunner /*
542f27aaf4bSChristian Brunner  * Cancel aio. Since we don't reference acb in a non qemu threads,
543f27aaf4bSChristian Brunner  * it is safe to access it here.
544f27aaf4bSChristian Brunner  */
545ad32e9c0SJosh Durgin static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb)
546f27aaf4bSChristian Brunner {
547f27aaf4bSChristian Brunner     RBDAIOCB *acb = (RBDAIOCB *) blockacb;
548f27aaf4bSChristian Brunner     acb->cancelled = 1;
549473c7f02SStefan Priebe 
550473c7f02SStefan Priebe     while (acb->status == -EINPROGRESS) {
551473c7f02SStefan Priebe         qemu_aio_wait();
552473c7f02SStefan Priebe     }
553473c7f02SStefan Priebe 
554473c7f02SStefan Priebe     qemu_aio_release(acb);
555f27aaf4bSChristian Brunner }
556f27aaf4bSChristian Brunner 
557d7331bedSStefan Hajnoczi static const AIOCBInfo rbd_aiocb_info = {
558f27aaf4bSChristian Brunner     .aiocb_size = sizeof(RBDAIOCB),
559ad32e9c0SJosh Durgin     .cancel = qemu_rbd_aio_cancel,
560f27aaf4bSChristian Brunner };
561f27aaf4bSChristian Brunner 
562e04fb07fSStefan Hajnoczi static void rbd_finish_bh(void *opaque)
563f27aaf4bSChristian Brunner {
564e04fb07fSStefan Hajnoczi     RADOSCB *rcb = opaque;
565e04fb07fSStefan Hajnoczi     qemu_bh_delete(rcb->acb->bh);
566e04fb07fSStefan Hajnoczi     qemu_rbd_complete_aio(rcb);
567ad32e9c0SJosh Durgin }
568ad32e9c0SJosh Durgin 
569ad32e9c0SJosh Durgin /*
570ad32e9c0SJosh Durgin  * This is the callback function for rbd_aio_read and _write
571ad32e9c0SJosh Durgin  *
572ad32e9c0SJosh Durgin  * Note: this function is being called from a non qemu thread so
573ad32e9c0SJosh Durgin  * we need to be careful about what we do here. Generally we only
574e04fb07fSStefan Hajnoczi  * schedule a BH, and do the rest of the io completion handling
575e04fb07fSStefan Hajnoczi  * from rbd_finish_bh() which runs in a qemu context.
576ad32e9c0SJosh Durgin  */
577ad32e9c0SJosh Durgin static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
578ad32e9c0SJosh Durgin {
579e04fb07fSStefan Hajnoczi     RBDAIOCB *acb = rcb->acb;
580e04fb07fSStefan Hajnoczi 
581ad32e9c0SJosh Durgin     rcb->ret = rbd_aio_get_return_value(c);
582ad32e9c0SJosh Durgin     rbd_aio_release(c);
583f27aaf4bSChristian Brunner 
584e04fb07fSStefan Hajnoczi     acb->bh = qemu_bh_new(rbd_finish_bh, rcb);
585e04fb07fSStefan Hajnoczi     qemu_bh_schedule(acb->bh);
586473c7f02SStefan Priebe }
587f27aaf4bSChristian Brunner 
588787f3133SJosh Durgin static int rbd_aio_discard_wrapper(rbd_image_t image,
589787f3133SJosh Durgin                                    uint64_t off,
590787f3133SJosh Durgin                                    uint64_t len,
591787f3133SJosh Durgin                                    rbd_completion_t comp)
592787f3133SJosh Durgin {
593787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
594787f3133SJosh Durgin     return rbd_aio_discard(image, off, len, comp);
595787f3133SJosh Durgin #else
596787f3133SJosh Durgin     return -ENOTSUP;
597787f3133SJosh Durgin #endif
598787f3133SJosh Durgin }
599787f3133SJosh Durgin 
600dc7588c1SJosh Durgin static int rbd_aio_flush_wrapper(rbd_image_t image,
601dc7588c1SJosh Durgin                                  rbd_completion_t comp)
602dc7588c1SJosh Durgin {
603dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
604dc7588c1SJosh Durgin     return rbd_aio_flush(image, comp);
605dc7588c1SJosh Durgin #else
606dc7588c1SJosh Durgin     return -ENOTSUP;
607dc7588c1SJosh Durgin #endif
608dc7588c1SJosh Durgin }
609dc7588c1SJosh Durgin 
610787f3133SJosh Durgin static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs,
611f27aaf4bSChristian Brunner                                        int64_t sector_num,
612f27aaf4bSChristian Brunner                                        QEMUIOVector *qiov,
613f27aaf4bSChristian Brunner                                        int nb_sectors,
614f27aaf4bSChristian Brunner                                        BlockDriverCompletionFunc *cb,
615787f3133SJosh Durgin                                        void *opaque,
616787f3133SJosh Durgin                                        RBDAIOCmd cmd)
617f27aaf4bSChristian Brunner {
618f27aaf4bSChristian Brunner     RBDAIOCB *acb;
619f27aaf4bSChristian Brunner     RADOSCB *rcb;
620ad32e9c0SJosh Durgin     rbd_completion_t c;
621f27aaf4bSChristian Brunner     int64_t off, size;
622f27aaf4bSChristian Brunner     char *buf;
62351a13528SJosh Durgin     int r;
624f27aaf4bSChristian Brunner 
625f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
626f27aaf4bSChristian Brunner 
627d7331bedSStefan Hajnoczi     acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
628787f3133SJosh Durgin     acb->cmd = cmd;
629f27aaf4bSChristian Brunner     acb->qiov = qiov;
630dc7588c1SJosh Durgin     if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
631787f3133SJosh Durgin         acb->bounce = NULL;
632787f3133SJosh Durgin     } else {
633f27aaf4bSChristian Brunner         acb->bounce = qemu_blockalign(bs, qiov->size);
634787f3133SJosh Durgin     }
635f27aaf4bSChristian Brunner     acb->ret = 0;
636f27aaf4bSChristian Brunner     acb->error = 0;
637f27aaf4bSChristian Brunner     acb->s = s;
638f27aaf4bSChristian Brunner     acb->cancelled = 0;
639f27aaf4bSChristian Brunner     acb->bh = NULL;
640473c7f02SStefan Priebe     acb->status = -EINPROGRESS;
641f27aaf4bSChristian Brunner 
642787f3133SJosh Durgin     if (cmd == RBD_AIO_WRITE) {
643d5e6b161SMichael Tokarev         qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
644f27aaf4bSChristian Brunner     }
645f27aaf4bSChristian Brunner 
646f27aaf4bSChristian Brunner     buf = acb->bounce;
647f27aaf4bSChristian Brunner 
648f27aaf4bSChristian Brunner     off = sector_num * BDRV_SECTOR_SIZE;
649f27aaf4bSChristian Brunner     size = nb_sectors * BDRV_SECTOR_SIZE;
650f27aaf4bSChristian Brunner 
6517267c094SAnthony Liguori     rcb = g_malloc(sizeof(RADOSCB));
652f27aaf4bSChristian Brunner     rcb->done = 0;
653f27aaf4bSChristian Brunner     rcb->acb = acb;
654f27aaf4bSChristian Brunner     rcb->buf = buf;
655f27aaf4bSChristian Brunner     rcb->s = acb->s;
656ad32e9c0SJosh Durgin     rcb->size = size;
65751a13528SJosh Durgin     r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
65851a13528SJosh Durgin     if (r < 0) {
65951a13528SJosh Durgin         goto failed;
66051a13528SJosh Durgin     }
661f27aaf4bSChristian Brunner 
662787f3133SJosh Durgin     switch (cmd) {
663787f3133SJosh Durgin     case RBD_AIO_WRITE:
66451a13528SJosh Durgin         r = rbd_aio_write(s->image, off, size, buf, c);
665787f3133SJosh Durgin         break;
666787f3133SJosh Durgin     case RBD_AIO_READ:
66751a13528SJosh Durgin         r = rbd_aio_read(s->image, off, size, buf, c);
668787f3133SJosh Durgin         break;
669787f3133SJosh Durgin     case RBD_AIO_DISCARD:
670787f3133SJosh Durgin         r = rbd_aio_discard_wrapper(s->image, off, size, c);
671787f3133SJosh Durgin         break;
672dc7588c1SJosh Durgin     case RBD_AIO_FLUSH:
673dc7588c1SJosh Durgin         r = rbd_aio_flush_wrapper(s->image, c);
674dc7588c1SJosh Durgin         break;
675787f3133SJosh Durgin     default:
676787f3133SJosh Durgin         r = -EINVAL;
67751a13528SJosh Durgin     }
67851a13528SJosh Durgin 
67951a13528SJosh Durgin     if (r < 0) {
68051a13528SJosh Durgin         goto failed;
681f27aaf4bSChristian Brunner     }
682f27aaf4bSChristian Brunner 
683f27aaf4bSChristian Brunner     return &acb->common;
68451a13528SJosh Durgin 
68551a13528SJosh Durgin failed:
6867267c094SAnthony Liguori     g_free(rcb);
68751a13528SJosh Durgin     qemu_aio_release(acb);
68851a13528SJosh Durgin     return NULL;
689f27aaf4bSChristian Brunner }
690f27aaf4bSChristian Brunner 
691ad32e9c0SJosh Durgin static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
692ad32e9c0SJosh Durgin                                             int64_t sector_num,
693ad32e9c0SJosh Durgin                                             QEMUIOVector *qiov,
694f27aaf4bSChristian Brunner                                             int nb_sectors,
695f27aaf4bSChristian Brunner                                             BlockDriverCompletionFunc *cb,
696f27aaf4bSChristian Brunner                                             void *opaque)
697f27aaf4bSChristian Brunner {
698787f3133SJosh Durgin     return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
699787f3133SJosh Durgin                          RBD_AIO_READ);
700f27aaf4bSChristian Brunner }
701f27aaf4bSChristian Brunner 
702ad32e9c0SJosh Durgin static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
703ad32e9c0SJosh Durgin                                              int64_t sector_num,
704ad32e9c0SJosh Durgin                                              QEMUIOVector *qiov,
705f27aaf4bSChristian Brunner                                              int nb_sectors,
706f27aaf4bSChristian Brunner                                              BlockDriverCompletionFunc *cb,
707f27aaf4bSChristian Brunner                                              void *opaque)
708f27aaf4bSChristian Brunner {
709787f3133SJosh Durgin     return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
710787f3133SJosh Durgin                          RBD_AIO_WRITE);
711f27aaf4bSChristian Brunner }
712f27aaf4bSChristian Brunner 
713dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
714dc7588c1SJosh Durgin static BlockDriverAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
715dc7588c1SJosh Durgin                                             BlockDriverCompletionFunc *cb,
716dc7588c1SJosh Durgin                                             void *opaque)
717dc7588c1SJosh Durgin {
718dc7588c1SJosh Durgin     return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
719dc7588c1SJosh Durgin }
720dc7588c1SJosh Durgin 
721dc7588c1SJosh Durgin #else
722dc7588c1SJosh Durgin 
7238b94ff85SPaolo Bonzini static int qemu_rbd_co_flush(BlockDriverState *bs)
7247a3f5fe9SSage Weil {
7257a3f5fe9SSage Weil #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
7267a3f5fe9SSage Weil     /* rbd_flush added in 0.1.1 */
7277a3f5fe9SSage Weil     BDRVRBDState *s = bs->opaque;
7287a3f5fe9SSage Weil     return rbd_flush(s->image);
7297a3f5fe9SSage Weil #else
7307a3f5fe9SSage Weil     return 0;
7317a3f5fe9SSage Weil #endif
7327a3f5fe9SSage Weil }
733dc7588c1SJosh Durgin #endif
7347a3f5fe9SSage Weil 
735ad32e9c0SJosh Durgin static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
736f27aaf4bSChristian Brunner {
737f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
738ad32e9c0SJosh Durgin     rbd_image_info_t info;
739ad32e9c0SJosh Durgin     int r;
740ad32e9c0SJosh Durgin 
741ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
742ad32e9c0SJosh Durgin     if (r < 0) {
743ad32e9c0SJosh Durgin         return r;
744ad32e9c0SJosh Durgin     }
745ad32e9c0SJosh Durgin 
746ad32e9c0SJosh Durgin     bdi->cluster_size = info.obj_size;
747f27aaf4bSChristian Brunner     return 0;
748f27aaf4bSChristian Brunner }
749f27aaf4bSChristian Brunner 
750ad32e9c0SJosh Durgin static int64_t qemu_rbd_getlength(BlockDriverState *bs)
751f27aaf4bSChristian Brunner {
752f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
753ad32e9c0SJosh Durgin     rbd_image_info_t info;
754ad32e9c0SJosh Durgin     int r;
755f27aaf4bSChristian Brunner 
756ad32e9c0SJosh Durgin     r = rbd_stat(s->image, &info, sizeof(info));
757ad32e9c0SJosh Durgin     if (r < 0) {
758ad32e9c0SJosh Durgin         return r;
759f27aaf4bSChristian Brunner     }
760f27aaf4bSChristian Brunner 
761ad32e9c0SJosh Durgin     return info.size;
762ad32e9c0SJosh Durgin }
763ad32e9c0SJosh Durgin 
76430cdc48cSJosh Durgin static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
76530cdc48cSJosh Durgin {
76630cdc48cSJosh Durgin     BDRVRBDState *s = bs->opaque;
76730cdc48cSJosh Durgin     int r;
76830cdc48cSJosh Durgin 
76930cdc48cSJosh Durgin     r = rbd_resize(s->image, offset);
77030cdc48cSJosh Durgin     if (r < 0) {
77130cdc48cSJosh Durgin         return r;
77230cdc48cSJosh Durgin     }
77330cdc48cSJosh Durgin 
77430cdc48cSJosh Durgin     return 0;
77530cdc48cSJosh Durgin }
77630cdc48cSJosh Durgin 
777ad32e9c0SJosh Durgin static int qemu_rbd_snap_create(BlockDriverState *bs,
778ad32e9c0SJosh Durgin                                 QEMUSnapshotInfo *sn_info)
779f27aaf4bSChristian Brunner {
780f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
781f27aaf4bSChristian Brunner     int r;
782f27aaf4bSChristian Brunner 
783f27aaf4bSChristian Brunner     if (sn_info->name[0] == '\0') {
784f27aaf4bSChristian Brunner         return -EINVAL; /* we need a name for rbd snapshots */
785f27aaf4bSChristian Brunner     }
786f27aaf4bSChristian Brunner 
787f27aaf4bSChristian Brunner     /*
788f27aaf4bSChristian Brunner      * rbd snapshots are using the name as the user controlled unique identifier
789f27aaf4bSChristian Brunner      * we can't use the rbd snapid for that purpose, as it can't be set
790f27aaf4bSChristian Brunner      */
791f27aaf4bSChristian Brunner     if (sn_info->id_str[0] != '\0' &&
792f27aaf4bSChristian Brunner         strcmp(sn_info->id_str, sn_info->name) != 0) {
793f27aaf4bSChristian Brunner         return -EINVAL;
794f27aaf4bSChristian Brunner     }
795f27aaf4bSChristian Brunner 
796f27aaf4bSChristian Brunner     if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
797f27aaf4bSChristian Brunner         return -ERANGE;
798f27aaf4bSChristian Brunner     }
799f27aaf4bSChristian Brunner 
800ad32e9c0SJosh Durgin     r = rbd_snap_create(s->image, sn_info->name);
801f27aaf4bSChristian Brunner     if (r < 0) {
802ad32e9c0SJosh Durgin         error_report("failed to create snap: %s", strerror(-r));
803f27aaf4bSChristian Brunner         return r;
804f27aaf4bSChristian Brunner     }
805f27aaf4bSChristian Brunner 
806f27aaf4bSChristian Brunner     return 0;
807f27aaf4bSChristian Brunner }
808f27aaf4bSChristian Brunner 
809bd603247SGregory Farnum static int qemu_rbd_snap_remove(BlockDriverState *bs,
810a89d89d3SWenchao Xia                                 const char *snapshot_id,
811a89d89d3SWenchao Xia                                 const char *snapshot_name,
812a89d89d3SWenchao Xia                                 Error **errp)
813bd603247SGregory Farnum {
814bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
815bd603247SGregory Farnum     int r;
816bd603247SGregory Farnum 
817a89d89d3SWenchao Xia     if (!snapshot_name) {
818a89d89d3SWenchao Xia         error_setg(errp, "rbd need a valid snapshot name");
819a89d89d3SWenchao Xia         return -EINVAL;
820a89d89d3SWenchao Xia     }
821a89d89d3SWenchao Xia 
822a89d89d3SWenchao Xia     /* If snapshot_id is specified, it must be equal to name, see
823a89d89d3SWenchao Xia        qemu_rbd_snap_list() */
824a89d89d3SWenchao Xia     if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
825a89d89d3SWenchao Xia         error_setg(errp,
826a89d89d3SWenchao Xia                    "rbd do not support snapshot id, it should be NULL or "
827a89d89d3SWenchao Xia                    "equal to snapshot name");
828a89d89d3SWenchao Xia         return -EINVAL;
829a89d89d3SWenchao Xia     }
830a89d89d3SWenchao Xia 
831bd603247SGregory Farnum     r = rbd_snap_remove(s->image, snapshot_name);
832a89d89d3SWenchao Xia     if (r < 0) {
833a89d89d3SWenchao Xia         error_setg_errno(errp, -r, "Failed to remove the snapshot");
834a89d89d3SWenchao Xia     }
835bd603247SGregory Farnum     return r;
836bd603247SGregory Farnum }
837bd603247SGregory Farnum 
838bd603247SGregory Farnum static int qemu_rbd_snap_rollback(BlockDriverState *bs,
839bd603247SGregory Farnum                                   const char *snapshot_name)
840bd603247SGregory Farnum {
841bd603247SGregory Farnum     BDRVRBDState *s = bs->opaque;
842bd603247SGregory Farnum     int r;
843bd603247SGregory Farnum 
844bd603247SGregory Farnum     r = rbd_snap_rollback(s->image, snapshot_name);
845bd603247SGregory Farnum     return r;
846bd603247SGregory Farnum }
847bd603247SGregory Farnum 
848ad32e9c0SJosh Durgin static int qemu_rbd_snap_list(BlockDriverState *bs,
849ad32e9c0SJosh Durgin                               QEMUSnapshotInfo **psn_tab)
850f27aaf4bSChristian Brunner {
851f27aaf4bSChristian Brunner     BDRVRBDState *s = bs->opaque;
852f27aaf4bSChristian Brunner     QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
853ad32e9c0SJosh Durgin     int i, snap_count;
854ad32e9c0SJosh Durgin     rbd_snap_info_t *snaps;
855ad32e9c0SJosh Durgin     int max_snaps = RBD_MAX_SNAPS;
856f27aaf4bSChristian Brunner 
857ad32e9c0SJosh Durgin     do {
8587267c094SAnthony Liguori         snaps = g_malloc(sizeof(*snaps) * max_snaps);
859ad32e9c0SJosh Durgin         snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
8609e6337d0SStefan Hajnoczi         if (snap_count <= 0) {
8617267c094SAnthony Liguori             g_free(snaps);
862f27aaf4bSChristian Brunner         }
863ad32e9c0SJosh Durgin     } while (snap_count == -ERANGE);
864f27aaf4bSChristian Brunner 
865ad32e9c0SJosh Durgin     if (snap_count <= 0) {
866b9c53290SJosh Durgin         goto done;
867f27aaf4bSChristian Brunner     }
868f27aaf4bSChristian Brunner 
8697267c094SAnthony Liguori     sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo));
870f27aaf4bSChristian Brunner 
871ad32e9c0SJosh Durgin     for (i = 0; i < snap_count; i++) {
872ad32e9c0SJosh Durgin         const char *snap_name = snaps[i].name;
873f27aaf4bSChristian Brunner 
874f27aaf4bSChristian Brunner         sn_info = sn_tab + i;
875f27aaf4bSChristian Brunner         pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
876f27aaf4bSChristian Brunner         pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
877f27aaf4bSChristian Brunner 
878ad32e9c0SJosh Durgin         sn_info->vm_state_size = snaps[i].size;
879f27aaf4bSChristian Brunner         sn_info->date_sec = 0;
880f27aaf4bSChristian Brunner         sn_info->date_nsec = 0;
881f27aaf4bSChristian Brunner         sn_info->vm_clock_nsec = 0;
882f27aaf4bSChristian Brunner     }
883ad32e9c0SJosh Durgin     rbd_snap_list_end(snaps);
8849e6337d0SStefan Hajnoczi     g_free(snaps);
885ad32e9c0SJosh Durgin 
886b9c53290SJosh Durgin  done:
887f27aaf4bSChristian Brunner     *psn_tab = sn_tab;
888f27aaf4bSChristian Brunner     return snap_count;
889f27aaf4bSChristian Brunner }
890f27aaf4bSChristian Brunner 
891787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
892787f3133SJosh Durgin static BlockDriverAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
893787f3133SJosh Durgin                                               int64_t sector_num,
894787f3133SJosh Durgin                                               int nb_sectors,
895787f3133SJosh Durgin                                               BlockDriverCompletionFunc *cb,
896787f3133SJosh Durgin                                               void *opaque)
897787f3133SJosh Durgin {
898787f3133SJosh Durgin     return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
899787f3133SJosh Durgin                          RBD_AIO_DISCARD);
900787f3133SJosh Durgin }
901787f3133SJosh Durgin #endif
902787f3133SJosh Durgin 
903ad32e9c0SJosh Durgin static QEMUOptionParameter qemu_rbd_create_options[] = {
904f27aaf4bSChristian Brunner     {
905f27aaf4bSChristian Brunner      .name = BLOCK_OPT_SIZE,
906f27aaf4bSChristian Brunner      .type = OPT_SIZE,
907f27aaf4bSChristian Brunner      .help = "Virtual disk size"
908f27aaf4bSChristian Brunner     },
909f27aaf4bSChristian Brunner     {
910f27aaf4bSChristian Brunner      .name = BLOCK_OPT_CLUSTER_SIZE,
911f27aaf4bSChristian Brunner      .type = OPT_SIZE,
912f27aaf4bSChristian Brunner      .help = "RBD object size"
913f27aaf4bSChristian Brunner     },
914f27aaf4bSChristian Brunner     {NULL}
915f27aaf4bSChristian Brunner };
916f27aaf4bSChristian Brunner 
917f27aaf4bSChristian Brunner static BlockDriver bdrv_rbd = {
918f27aaf4bSChristian Brunner     .format_name        = "rbd",
919f27aaf4bSChristian Brunner     .instance_size      = sizeof(BDRVRBDState),
920030be321SBenoît Canet     .bdrv_needs_filename = true,
921ad32e9c0SJosh Durgin     .bdrv_file_open     = qemu_rbd_open,
922ad32e9c0SJosh Durgin     .bdrv_close         = qemu_rbd_close,
923ad32e9c0SJosh Durgin     .bdrv_create        = qemu_rbd_create,
9243ac21627SPeter Lieven     .bdrv_has_zero_init = bdrv_has_zero_init_1,
925ad32e9c0SJosh Durgin     .bdrv_get_info      = qemu_rbd_getinfo,
926ad32e9c0SJosh Durgin     .create_options     = qemu_rbd_create_options,
927ad32e9c0SJosh Durgin     .bdrv_getlength     = qemu_rbd_getlength,
92830cdc48cSJosh Durgin     .bdrv_truncate      = qemu_rbd_truncate,
929f27aaf4bSChristian Brunner     .protocol_name      = "rbd",
930f27aaf4bSChristian Brunner 
931ad32e9c0SJosh Durgin     .bdrv_aio_readv         = qemu_rbd_aio_readv,
932ad32e9c0SJosh Durgin     .bdrv_aio_writev        = qemu_rbd_aio_writev,
933dc7588c1SJosh Durgin 
934dc7588c1SJosh Durgin #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
935dc7588c1SJosh Durgin     .bdrv_aio_flush         = qemu_rbd_aio_flush,
936dc7588c1SJosh Durgin #else
937c68b89acSKevin Wolf     .bdrv_co_flush_to_disk  = qemu_rbd_co_flush,
938dc7588c1SJosh Durgin #endif
939f27aaf4bSChristian Brunner 
940787f3133SJosh Durgin #ifdef LIBRBD_SUPPORTS_DISCARD
941787f3133SJosh Durgin     .bdrv_aio_discard       = qemu_rbd_aio_discard,
942787f3133SJosh Durgin #endif
943787f3133SJosh Durgin 
944ad32e9c0SJosh Durgin     .bdrv_snapshot_create   = qemu_rbd_snap_create,
945bd603247SGregory Farnum     .bdrv_snapshot_delete   = qemu_rbd_snap_remove,
946ad32e9c0SJosh Durgin     .bdrv_snapshot_list     = qemu_rbd_snap_list,
947bd603247SGregory Farnum     .bdrv_snapshot_goto     = qemu_rbd_snap_rollback,
948f27aaf4bSChristian Brunner };
949f27aaf4bSChristian Brunner 
950f27aaf4bSChristian Brunner static void bdrv_rbd_init(void)
951f27aaf4bSChristian Brunner {
952f27aaf4bSChristian Brunner     bdrv_register(&bdrv_rbd);
953f27aaf4bSChristian Brunner }
954f27aaf4bSChristian Brunner 
955f27aaf4bSChristian Brunner block_init(bdrv_rbd_init);
956