blob: abfea612ec2d1fa0e4f9c89e19a431d4cea4adf3 [file] [log] [blame]
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001/*
2 * QEMU Block driver for RADOS (Ceph)
3 *
Josh Durginad32e9c2011-05-26 16:07:31 -07004 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
Christian Brunnerf27aaf42010-12-06 20:53:01 +01006 *
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
9 *
Paolo Bonzini6b620ca2012-01-13 17:44:23 +010010 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010012 */
13
Peter Maydell80c71a22016-01-18 18:01:42 +000014#include "qemu/osdep.h"
Josh Durginad32e9c2011-05-26 16:07:31 -070015
Christian Brunnerf27aaf42010-12-06 20:53:01 +010016#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010017#include "qemu/error-report.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010018#include "block/block_int.h"
Daniel P. Berrange60390a22016-01-21 14:19:19 +000019#include "crypto/secret.h"
Christian Brunnerf27aaf42010-12-06 20:53:01 +010020
Josh Durginad32e9c2011-05-26 16:07:31 -070021#include <rbd/librbd.h>
Christian Brunnerf27aaf42010-12-06 20:53:01 +010022
Christian Brunnerf27aaf42010-12-06 20:53:01 +010023/*
24 * When specifying the image filename use:
25 *
Josh Durginfab5cf52011-05-26 16:07:32 -070026 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
Christian Brunnerf27aaf42010-12-06 20:53:01 +010027 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070028 * poolname must be the name of an existing rados pool.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010029 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070030 * devicename is the name of the rbd image.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010031 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070032 * Each option given is used to configure rados, and may be any valid
33 * Ceph option, "id", or "conf".
Josh Durginfab5cf52011-05-26 16:07:32 -070034 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070035 * The "id" option indicates what user we should authenticate as to
36 * the Ceph cluster. If it is excluded we will use the Ceph default
37 * (normally 'admin').
Christian Brunnerf27aaf42010-12-06 20:53:01 +010038 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070039 * The "conf" option specifies a Ceph configuration file to read. If
40 * it is not specified, we will read from the default Ceph locations
41 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
42 * file, specify conf=/dev/null.
Christian Brunnerf27aaf42010-12-06 20:53:01 +010043 *
Sage Weil9e1fbcd2011-09-15 14:11:10 -070044 * Configuration values containing :, @, or = can be escaped with a
45 * leading "\".
Christian Brunnerf27aaf42010-12-06 20:53:01 +010046 */
47
Josh Durgin787f3132012-04-30 23:16:45 -070048/* rbd_aio_discard added in 0.1.2 */
49#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
50#define LIBRBD_SUPPORTS_DISCARD
51#else
52#undef LIBRBD_SUPPORTS_DISCARD
53#endif
54
Christian Brunnerf27aaf42010-12-06 20:53:01 +010055#define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
56
Josh Durginad32e9c2011-05-26 16:07:31 -070057#define RBD_MAX_CONF_NAME_SIZE 128
58#define RBD_MAX_CONF_VAL_SIZE 512
59#define RBD_MAX_CONF_SIZE 1024
60#define RBD_MAX_POOL_NAME_SIZE 128
61#define RBD_MAX_SNAP_NAME_SIZE 128
62#define RBD_MAX_SNAPS 100
63
Josh Durgin787f3132012-04-30 23:16:45 -070064typedef enum {
65 RBD_AIO_READ,
66 RBD_AIO_WRITE,
Josh Durgindc7588c2013-03-29 13:03:23 -070067 RBD_AIO_DISCARD,
68 RBD_AIO_FLUSH
Josh Durgin787f3132012-04-30 23:16:45 -070069} RBDAIOCmd;
70
Christian Brunnerf27aaf42010-12-06 20:53:01 +010071typedef struct RBDAIOCB {
Markus Armbruster7c84b1b2014-10-07 13:59:14 +020072 BlockAIOCB common;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010073 QEMUBH *bh;
Stefan Priebe08448d52012-11-20 13:44:55 +010074 int64_t ret;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010075 QEMUIOVector *qiov;
76 char *bounce;
Josh Durgin787f3132012-04-30 23:16:45 -070077 RBDAIOCmd cmd;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010078 int error;
79 struct BDRVRBDState *s;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010080} RBDAIOCB;
81
82typedef struct RADOSCB {
Christian Brunnerf27aaf42010-12-06 20:53:01 +010083 RBDAIOCB *acb;
84 struct BDRVRBDState *s;
Josh Durginad32e9c2011-05-26 16:07:31 -070085 int64_t size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010086 char *buf;
Stefan Priebe08448d52012-11-20 13:44:55 +010087 int64_t ret;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010088} RADOSCB;
89
Christian Brunnerf27aaf42010-12-06 20:53:01 +010090typedef struct BDRVRBDState {
Josh Durginad32e9c2011-05-26 16:07:31 -070091 rados_t cluster;
92 rados_ioctx_t io_ctx;
93 rbd_image_t image;
94 char name[RBD_MAX_IMAGE_NAME_SIZE];
Josh Durginad32e9c2011-05-26 16:07:31 -070095 char *snap;
Christian Brunnerf27aaf42010-12-06 20:53:01 +010096} BDRVRBDState;
97
Josh Durginad32e9c2011-05-26 16:07:31 -070098static int qemu_rbd_next_tok(char *dst, int dst_len,
99 char *src, char delim,
100 const char *name,
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200101 char **p, Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100102{
103 int l;
104 char *end;
105
106 *p = NULL;
107
108 if (delim != '\0') {
Sage Weil16a06b22011-09-19 13:35:26 -0700109 for (end = src; *end; ++end) {
110 if (*end == delim) {
111 break;
112 }
113 if (*end == '\\' && end[1] != '\0') {
114 end++;
115 }
116 }
117 if (*end == delim) {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100118 *p = end + 1;
119 *end = '\0';
120 }
121 }
122 l = strlen(src);
123 if (l >= dst_len) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200124 error_setg(errp, "%s too long", name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100125 return -EINVAL;
126 } else if (l == 0) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200127 error_setg(errp, "%s too short", name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100128 return -EINVAL;
129 }
130
131 pstrcpy(dst, dst_len, src);
132
133 return 0;
134}
135
Sage Weil16a06b22011-09-19 13:35:26 -0700136static void qemu_rbd_unescape(char *src)
137{
138 char *p;
139
140 for (p = src; *src; ++src, ++p) {
141 if (*src == '\\' && src[1] != '\0') {
142 src++;
143 }
144 *p = *src;
145 }
146 *p = '\0';
147}
148
Josh Durginad32e9c2011-05-26 16:07:31 -0700149static int qemu_rbd_parsename(const char *filename,
150 char *pool, int pool_len,
151 char *snap, int snap_len,
Josh Durginfab5cf52011-05-26 16:07:32 -0700152 char *name, int name_len,
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200153 char *conf, int conf_len,
154 Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100155{
156 const char *start;
157 char *p, *buf;
158 int ret;
159
160 if (!strstart(filename, "rbd:", &start)) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200161 error_setg(errp, "File name must start with 'rbd:'");
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100162 return -EINVAL;
163 }
164
Anthony Liguori7267c092011-08-20 22:09:37 -0500165 buf = g_strdup(start);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100166 p = buf;
Josh Durginfab5cf52011-05-26 16:07:32 -0700167 *snap = '\0';
168 *conf = '\0';
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100169
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200170 ret = qemu_rbd_next_tok(pool, pool_len, p,
171 '/', "pool name", &p, errp);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100172 if (ret < 0 || !p) {
173 ret = -EINVAL;
174 goto done;
175 }
Sage Weil16a06b22011-09-19 13:35:26 -0700176 qemu_rbd_unescape(pool);
Josh Durginfab5cf52011-05-26 16:07:32 -0700177
178 if (strchr(p, '@')) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200179 ret = qemu_rbd_next_tok(name, name_len, p,
180 '@', "object name", &p, errp);
Josh Durginfab5cf52011-05-26 16:07:32 -0700181 if (ret < 0) {
182 goto done;
183 }
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200184 ret = qemu_rbd_next_tok(snap, snap_len, p,
185 ':', "snap name", &p, errp);
Sage Weil16a06b22011-09-19 13:35:26 -0700186 qemu_rbd_unescape(snap);
Josh Durginfab5cf52011-05-26 16:07:32 -0700187 } else {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200188 ret = qemu_rbd_next_tok(name, name_len, p,
189 ':', "object name", &p, errp);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100190 }
Sage Weil16a06b22011-09-19 13:35:26 -0700191 qemu_rbd_unescape(name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700192 if (ret < 0 || !p) {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100193 goto done;
194 }
195
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200196 ret = qemu_rbd_next_tok(conf, conf_len, p,
197 '\0', "configuration", &p, errp);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100198
199done:
Anthony Liguori7267c092011-08-20 22:09:37 -0500200 g_free(buf);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100201 return ret;
202}
203
Sage Weil7c7e9df2011-09-07 09:28:04 -0700204static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
205{
206 const char *p = conf;
207
208 while (*p) {
209 int len;
210 const char *end = strchr(p, ':');
211
212 if (end) {
213 len = end - p;
214 } else {
215 len = strlen(p);
216 }
217
218 if (strncmp(p, "id=", 3) == 0) {
219 len -= 3;
220 strncpy(clientname, p + 3, len);
221 clientname[len] = '\0';
222 return clientname;
223 }
224 if (end == NULL) {
225 break;
226 }
227 p = end + 1;
228 }
229 return NULL;
230}
231
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000232
233static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
234 Error **errp)
235{
236 if (secretid == 0) {
237 return 0;
238 }
239
240 gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
241 errp);
242 if (!secret) {
243 return -1;
244 }
245
246 rados_conf_set(cluster, "key", secret);
247 g_free(secret);
248
249 return 0;
250}
251
252
Josh Durgine34d8f22015-06-10 20:28:46 -0700253static int qemu_rbd_set_conf(rados_t cluster, const char *conf,
254 bool only_read_conf_file,
255 Error **errp)
Josh Durginfab5cf52011-05-26 16:07:32 -0700256{
257 char *p, *buf;
258 char name[RBD_MAX_CONF_NAME_SIZE];
259 char value[RBD_MAX_CONF_VAL_SIZE];
260 int ret = 0;
261
Anthony Liguori7267c092011-08-20 22:09:37 -0500262 buf = g_strdup(conf);
Josh Durginfab5cf52011-05-26 16:07:32 -0700263 p = buf;
264
265 while (p) {
266 ret = qemu_rbd_next_tok(name, sizeof(name), p,
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200267 '=', "conf option name", &p, errp);
Josh Durginfab5cf52011-05-26 16:07:32 -0700268 if (ret < 0) {
269 break;
270 }
Sage Weil16a06b22011-09-19 13:35:26 -0700271 qemu_rbd_unescape(name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700272
273 if (!p) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200274 error_setg(errp, "conf option %s has no value", name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700275 ret = -EINVAL;
276 break;
277 }
278
279 ret = qemu_rbd_next_tok(value, sizeof(value), p,
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200280 ':', "conf option value", &p, errp);
Josh Durginfab5cf52011-05-26 16:07:32 -0700281 if (ret < 0) {
282 break;
283 }
Sage Weil16a06b22011-09-19 13:35:26 -0700284 qemu_rbd_unescape(value);
Josh Durginfab5cf52011-05-26 16:07:32 -0700285
Sage Weil7c7e9df2011-09-07 09:28:04 -0700286 if (strcmp(name, "conf") == 0) {
Josh Durgine34d8f22015-06-10 20:28:46 -0700287 /* read the conf file alone, so it doesn't override more
288 specific settings for a particular device */
289 if (only_read_conf_file) {
290 ret = rados_conf_read_file(cluster, value);
291 if (ret < 0) {
292 error_setg(errp, "error reading conf file %s", value);
293 break;
294 }
Sage Weil7c7e9df2011-09-07 09:28:04 -0700295 }
296 } else if (strcmp(name, "id") == 0) {
297 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
Josh Durgine34d8f22015-06-10 20:28:46 -0700298 } else if (!only_read_conf_file) {
Josh Durginfab5cf52011-05-26 16:07:32 -0700299 ret = rados_conf_set(cluster, name, value);
300 if (ret < 0) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200301 error_setg(errp, "invalid conf option %s", name);
Josh Durginfab5cf52011-05-26 16:07:32 -0700302 ret = -EINVAL;
303 break;
304 }
Josh Durginfab5cf52011-05-26 16:07:32 -0700305 }
306 }
307
Anthony Liguori7267c092011-08-20 22:09:37 -0500308 g_free(buf);
Josh Durginfab5cf52011-05-26 16:07:32 -0700309 return ret;
310}
311
Chunyan Liubd0cf592014-06-05 17:21:04 +0800312static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100313{
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200314 Error *local_err = NULL;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100315 int64_t bytes = 0;
316 int64_t objsize;
Josh Durginad32e9c2011-05-26 16:07:31 -0700317 int obj_order = 0;
318 char pool[RBD_MAX_POOL_NAME_SIZE];
319 char name[RBD_MAX_IMAGE_NAME_SIZE];
320 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
Josh Durginfab5cf52011-05-26 16:07:32 -0700321 char conf[RBD_MAX_CONF_SIZE];
Sage Weil7c7e9df2011-09-07 09:28:04 -0700322 char clientname_buf[RBD_MAX_CONF_SIZE];
323 char *clientname;
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000324 const char *secretid;
Josh Durginad32e9c2011-05-26 16:07:31 -0700325 rados_t cluster;
326 rados_ioctx_t io_ctx;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100327 int ret;
328
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000329 secretid = qemu_opt_get(opts, "password-secret");
330
Josh Durginad32e9c2011-05-26 16:07:31 -0700331 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
332 snap_buf, sizeof(snap_buf),
Josh Durginfab5cf52011-05-26 16:07:32 -0700333 name, sizeof(name),
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200334 conf, sizeof(conf), &local_err) < 0) {
335 error_propagate(errp, local_err);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100336 return -EINVAL;
337 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100338
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100339 /* Read out options */
Hu Taoc2eb9182014-09-10 17:05:45 +0800340 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
341 BDRV_SECTOR_SIZE);
Chunyan Liubd0cf592014-06-05 17:21:04 +0800342 objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
343 if (objsize) {
344 if ((objsize - 1) & objsize) { /* not a power of 2? */
345 error_setg(errp, "obj size needs to be power of 2");
346 return -EINVAL;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100347 }
Chunyan Liubd0cf592014-06-05 17:21:04 +0800348 if (objsize < 4096) {
349 error_setg(errp, "obj size too small");
350 return -EINVAL;
351 }
Stefan Hajnoczi786a4ea2015-03-23 15:29:26 +0000352 obj_order = ctz32(objsize);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100353 }
354
Sage Weil7c7e9df2011-09-07 09:28:04 -0700355 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
356 if (rados_create(&cluster, clientname) < 0) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200357 error_setg(errp, "error initializing");
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100358 return -EIO;
359 }
360
Josh Durginfab5cf52011-05-26 16:07:32 -0700361 if (strstr(conf, "conf=") == NULL) {
Sage Weilf9fe18e2011-09-15 14:11:08 -0700362 /* try default location, but ignore failure */
363 rados_conf_read_file(cluster, NULL);
Josh Durgine34d8f22015-06-10 20:28:46 -0700364 } else if (conf[0] != '\0' &&
365 qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
366 rados_shutdown(cluster);
367 error_propagate(errp, local_err);
368 return -EIO;
Josh Durginfab5cf52011-05-26 16:07:32 -0700369 }
370
371 if (conf[0] != '\0' &&
Josh Durgine34d8f22015-06-10 20:28:46 -0700372 qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700373 rados_shutdown(cluster);
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200374 error_propagate(errp, local_err);
Josh Durginad32e9c2011-05-26 16:07:31 -0700375 return -EIO;
376 }
377
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000378 if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
379 rados_shutdown(cluster);
380 return -EIO;
381 }
382
Josh Durginad32e9c2011-05-26 16:07:31 -0700383 if (rados_connect(cluster) < 0) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200384 error_setg(errp, "error connecting");
Josh Durginad32e9c2011-05-26 16:07:31 -0700385 rados_shutdown(cluster);
386 return -EIO;
387 }
388
389 if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200390 error_setg(errp, "error opening pool %s", pool);
Josh Durginad32e9c2011-05-26 16:07:31 -0700391 rados_shutdown(cluster);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100392 return -EIO;
393 }
394
Josh Durginad32e9c2011-05-26 16:07:31 -0700395 ret = rbd_create(io_ctx, name, bytes, &obj_order);
396 rados_ioctx_destroy(io_ctx);
397 rados_shutdown(cluster);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100398
399 return ret;
400}
401
402/*
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100403 * This aio completion is being called from rbd_finish_bh() and runs in qemu
404 * BH context.
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100405 */
Josh Durginad32e9c2011-05-26 16:07:31 -0700406static void qemu_rbd_complete_aio(RADOSCB *rcb)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100407{
408 RBDAIOCB *acb = rcb->acb;
409 int64_t r;
410
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100411 r = rcb->ret;
412
Josh Durgindc7588c2013-03-29 13:03:23 -0700413 if (acb->cmd != RBD_AIO_READ) {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100414 if (r < 0) {
415 acb->ret = r;
416 acb->error = 1;
417 } else if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700418 acb->ret = rcb->size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100419 }
420 } else {
Josh Durginad32e9c2011-05-26 16:07:31 -0700421 if (r < 0) {
422 memset(rcb->buf, 0, rcb->size);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100423 acb->ret = r;
424 acb->error = 1;
Josh Durginad32e9c2011-05-26 16:07:31 -0700425 } else if (r < rcb->size) {
426 memset(rcb->buf + r, 0, rcb->size - r);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100427 if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700428 acb->ret = rcb->size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100429 }
430 } else if (!acb->error) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700431 acb->ret = r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100432 }
433 }
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100434
Anthony Liguori7267c092011-08-20 22:09:37 -0500435 g_free(rcb);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100436
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100437 if (acb->cmd == RBD_AIO_READ) {
438 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
439 }
440 qemu_vfree(acb->bounce);
441 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100442
Fam Zheng80074292014-09-11 13:41:28 +0800443 qemu_aio_unref(acb);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100444}
445
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200446/* TODO Convert to fine grained options */
447static QemuOptsList runtime_opts = {
448 .name = "rbd",
449 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
450 .desc = {
451 {
452 .name = "filename",
453 .type = QEMU_OPT_STRING,
454 .help = "Specification of the rbd image",
455 },
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000456 {
457 .name = "password-secret",
458 .type = QEMU_OPT_STRING,
459 .help = "ID of secret providing the password",
460 },
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200461 { /* end of list */ }
462 },
463};
464
Max Reitz015a1032013-09-05 14:22:29 +0200465static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
466 Error **errp)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100467{
468 BDRVRBDState *s = bs->opaque;
Josh Durginad32e9c2011-05-26 16:07:31 -0700469 char pool[RBD_MAX_POOL_NAME_SIZE];
470 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
Josh Durginfab5cf52011-05-26 16:07:32 -0700471 char conf[RBD_MAX_CONF_SIZE];
Sage Weil7c7e9df2011-09-07 09:28:04 -0700472 char clientname_buf[RBD_MAX_CONF_SIZE];
473 char *clientname;
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000474 const char *secretid;
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200475 QemuOpts *opts;
476 Error *local_err = NULL;
477 const char *filename;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100478 int r;
479
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -0800480 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200481 qemu_opts_absorb_qdict(opts, options, &local_err);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100482 if (local_err) {
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200483 error_propagate(errp, local_err);
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200484 qemu_opts_del(opts);
485 return -EINVAL;
486 }
487
488 filename = qemu_opt_get(opts, "filename");
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000489 secretid = qemu_opt_get(opts, "password-secret");
Kevin Wolfa9ccedc2013-04-12 18:05:35 +0200490
Josh Durginad32e9c2011-05-26 16:07:31 -0700491 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
492 snap_buf, sizeof(snap_buf),
Josh Durginfab5cf52011-05-26 16:07:32 -0700493 s->name, sizeof(s->name),
Markus Armbrusterd61563b2014-05-16 11:00:11 +0200494 conf, sizeof(conf), errp) < 0) {
Kevin Wolfc3ca9882013-04-25 15:59:27 +0200495 r = -EINVAL;
496 goto failed_opts;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100497 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100498
Sage Weil7c7e9df2011-09-07 09:28:04 -0700499 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
500 r = rados_create(&s->cluster, clientname);
Josh Durginad32e9c2011-05-26 16:07:31 -0700501 if (r < 0) {
Gonglei9281dbe2014-12-04 14:34:11 +0800502 error_setg(errp, "error initializing");
Kevin Wolfc3ca9882013-04-25 15:59:27 +0200503 goto failed_opts;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100504 }
505
Sage Weileb93d5d2011-09-07 09:28:06 -0700506 s->snap = NULL;
507 if (snap_buf[0] != '\0') {
508 s->snap = g_strdup(snap_buf);
509 }
510
Josh Durgin99a3c892015-06-10 20:28:45 -0700511 if (strstr(conf, "conf=") == NULL) {
512 /* try default location, but ignore failure */
513 rados_conf_read_file(s->cluster, NULL);
Josh Durgine34d8f22015-06-10 20:28:46 -0700514 } else if (conf[0] != '\0') {
515 r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
516 if (r < 0) {
517 goto failed_shutdown;
518 }
Josh Durgin99a3c892015-06-10 20:28:45 -0700519 }
520
521 if (conf[0] != '\0') {
Josh Durgine34d8f22015-06-10 20:28:46 -0700522 r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
Josh Durgin99a3c892015-06-10 20:28:45 -0700523 if (r < 0) {
524 goto failed_shutdown;
525 }
526 }
527
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000528 if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
529 r = -EIO;
530 goto failed_shutdown;
531 }
532
Josh Durginb11f38f2012-05-17 13:42:29 -0700533 /*
534 * Fallback to more conservative semantics if setting cache
535 * options fails. Ignore errors from setting rbd_cache because the
536 * only possible error is that the option does not exist, and
537 * librbd defaults to no caching. If write through caching cannot
538 * be set up, fall back to no caching.
539 */
540 if (flags & BDRV_O_NOCACHE) {
541 rados_conf_set(s->cluster, "rbd_cache", "false");
542 } else {
543 rados_conf_set(s->cluster, "rbd_cache", "true");
Josh Durginb11f38f2012-05-17 13:42:29 -0700544 }
545
Josh Durginad32e9c2011-05-26 16:07:31 -0700546 r = rados_connect(s->cluster);
547 if (r < 0) {
Gonglei9281dbe2014-12-04 14:34:11 +0800548 error_setg(errp, "error connecting");
Sage Weileb93d5d2011-09-07 09:28:06 -0700549 goto failed_shutdown;
Josh Durginad32e9c2011-05-26 16:07:31 -0700550 }
551
552 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
553 if (r < 0) {
Gonglei9281dbe2014-12-04 14:34:11 +0800554 error_setg(errp, "error opening pool %s", pool);
Sage Weileb93d5d2011-09-07 09:28:06 -0700555 goto failed_shutdown;
Josh Durginad32e9c2011-05-26 16:07:31 -0700556 }
557
558 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
559 if (r < 0) {
Gonglei9281dbe2014-12-04 14:34:11 +0800560 error_setg(errp, "error reading header from %s", s->name);
Sage Weileb93d5d2011-09-07 09:28:06 -0700561 goto failed_open;
Josh Durginad32e9c2011-05-26 16:07:31 -0700562 }
563
564 bs->read_only = (s->snap != NULL);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100565
Kevin Wolfc3ca9882013-04-25 15:59:27 +0200566 qemu_opts_del(opts);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100567 return 0;
568
Sage Weileb93d5d2011-09-07 09:28:06 -0700569failed_open:
Josh Durginad32e9c2011-05-26 16:07:31 -0700570 rados_ioctx_destroy(s->io_ctx);
Sage Weileb93d5d2011-09-07 09:28:06 -0700571failed_shutdown:
Josh Durginad32e9c2011-05-26 16:07:31 -0700572 rados_shutdown(s->cluster);
Sage Weileb93d5d2011-09-07 09:28:06 -0700573 g_free(s->snap);
Kevin Wolfc3ca9882013-04-25 15:59:27 +0200574failed_opts:
575 qemu_opts_del(opts);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100576 return r;
577}
578
Josh Durginad32e9c2011-05-26 16:07:31 -0700579static void qemu_rbd_close(BlockDriverState *bs)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100580{
581 BDRVRBDState *s = bs->opaque;
582
Josh Durginad32e9c2011-05-26 16:07:31 -0700583 rbd_close(s->image);
584 rados_ioctx_destroy(s->io_ctx);
Anthony Liguori7267c092011-08-20 22:09:37 -0500585 g_free(s->snap);
Josh Durginad32e9c2011-05-26 16:07:31 -0700586 rados_shutdown(s->cluster);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100587}
588
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100589static const AIOCBInfo rbd_aiocb_info = {
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100590 .aiocb_size = sizeof(RBDAIOCB),
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100591};
592
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100593static void rbd_finish_bh(void *opaque)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100594{
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100595 RADOSCB *rcb = opaque;
596 qemu_bh_delete(rcb->acb->bh);
597 qemu_rbd_complete_aio(rcb);
Josh Durginad32e9c2011-05-26 16:07:31 -0700598}
599
600/*
601 * This is the callback function for rbd_aio_read and _write
602 *
603 * Note: this function is being called from a non qemu thread so
604 * we need to be careful about what we do here. Generally we only
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100605 * schedule a BH, and do the rest of the io completion handling
606 * from rbd_finish_bh() which runs in a qemu context.
Josh Durginad32e9c2011-05-26 16:07:31 -0700607 */
608static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
609{
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100610 RBDAIOCB *acb = rcb->acb;
611
Josh Durginad32e9c2011-05-26 16:07:31 -0700612 rcb->ret = rbd_aio_get_return_value(c);
613 rbd_aio_release(c);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100614
Stefan Hajnocziea800192014-05-08 16:34:51 +0200615 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
616 rbd_finish_bh, rcb);
Stefan Hajnoczie04fb072013-12-05 16:38:33 +0100617 qemu_bh_schedule(acb->bh);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100618}
619
Josh Durgin787f3132012-04-30 23:16:45 -0700620static int rbd_aio_discard_wrapper(rbd_image_t image,
621 uint64_t off,
622 uint64_t len,
623 rbd_completion_t comp)
624{
625#ifdef LIBRBD_SUPPORTS_DISCARD
626 return rbd_aio_discard(image, off, len, comp);
627#else
628 return -ENOTSUP;
629#endif
630}
631
Josh Durgindc7588c2013-03-29 13:03:23 -0700632static int rbd_aio_flush_wrapper(rbd_image_t image,
633 rbd_completion_t comp)
634{
635#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
636 return rbd_aio_flush(image, comp);
637#else
638 return -ENOTSUP;
639#endif
640}
641
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200642static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
643 int64_t sector_num,
644 QEMUIOVector *qiov,
645 int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200646 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200647 void *opaque,
648 RBDAIOCmd cmd)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100649{
650 RBDAIOCB *acb;
Kevin Wolf0f7a0232014-05-21 18:11:48 +0200651 RADOSCB *rcb = NULL;
Josh Durginad32e9c2011-05-26 16:07:31 -0700652 rbd_completion_t c;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100653 int64_t off, size;
654 char *buf;
Josh Durgin51a13522011-05-26 16:07:33 -0700655 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100656
657 BDRVRBDState *s = bs->opaque;
658
Stefan Hajnoczid7331be2012-10-31 16:34:37 +0100659 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
Josh Durgin787f3132012-04-30 23:16:45 -0700660 acb->cmd = cmd;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100661 acb->qiov = qiov;
Josh Durgindc7588c2013-03-29 13:03:23 -0700662 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
Josh Durgin787f3132012-04-30 23:16:45 -0700663 acb->bounce = NULL;
664 } else {
Kevin Wolf0f7a0232014-05-21 18:11:48 +0200665 acb->bounce = qemu_try_blockalign(bs, qiov->size);
666 if (acb->bounce == NULL) {
667 goto failed;
668 }
Josh Durgin787f3132012-04-30 23:16:45 -0700669 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100670 acb->ret = 0;
671 acb->error = 0;
672 acb->s = s;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100673 acb->bh = NULL;
674
Josh Durgin787f3132012-04-30 23:16:45 -0700675 if (cmd == RBD_AIO_WRITE) {
Michael Tokarevd5e6b162012-06-07 20:21:06 +0400676 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100677 }
678
679 buf = acb->bounce;
680
681 off = sector_num * BDRV_SECTOR_SIZE;
682 size = nb_sectors * BDRV_SECTOR_SIZE;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100683
Markus Armbruster5839e532014-08-19 10:31:08 +0200684 rcb = g_new(RADOSCB, 1);
Josh Durginad32e9c2011-05-26 16:07:31 -0700685 rcb->acb = acb;
686 rcb->buf = buf;
687 rcb->s = acb->s;
688 rcb->size = size;
Josh Durgin51a13522011-05-26 16:07:33 -0700689 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
690 if (r < 0) {
691 goto failed;
692 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100693
Josh Durgin787f3132012-04-30 23:16:45 -0700694 switch (cmd) {
695 case RBD_AIO_WRITE:
Josh Durgin51a13522011-05-26 16:07:33 -0700696 r = rbd_aio_write(s->image, off, size, buf, c);
Josh Durgin787f3132012-04-30 23:16:45 -0700697 break;
698 case RBD_AIO_READ:
Josh Durgin51a13522011-05-26 16:07:33 -0700699 r = rbd_aio_read(s->image, off, size, buf, c);
Josh Durgin787f3132012-04-30 23:16:45 -0700700 break;
701 case RBD_AIO_DISCARD:
702 r = rbd_aio_discard_wrapper(s->image, off, size, c);
703 break;
Josh Durgindc7588c2013-03-29 13:03:23 -0700704 case RBD_AIO_FLUSH:
705 r = rbd_aio_flush_wrapper(s->image, c);
706 break;
Josh Durgin787f3132012-04-30 23:16:45 -0700707 default:
708 r = -EINVAL;
Josh Durgin51a13522011-05-26 16:07:33 -0700709 }
710
711 if (r < 0) {
Kevin Wolf405a2762014-06-05 16:19:26 +0200712 goto failed_completion;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100713 }
714
715 return &acb->common;
Josh Durgin51a13522011-05-26 16:07:33 -0700716
Kevin Wolf405a2762014-06-05 16:19:26 +0200717failed_completion:
718 rbd_aio_release(c);
Josh Durgin51a13522011-05-26 16:07:33 -0700719failed:
Anthony Liguori7267c092011-08-20 22:09:37 -0500720 g_free(rcb);
Kevin Wolf405a2762014-06-05 16:19:26 +0200721 qemu_vfree(acb->bounce);
Fam Zheng80074292014-09-11 13:41:28 +0800722 qemu_aio_unref(acb);
Josh Durgin51a13522011-05-26 16:07:33 -0700723 return NULL;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100724}
725
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200726static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
727 int64_t sector_num,
728 QEMUIOVector *qiov,
729 int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200730 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200731 void *opaque)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100732{
Josh Durgin787f3132012-04-30 23:16:45 -0700733 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
734 RBD_AIO_READ);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100735}
736
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200737static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
738 int64_t sector_num,
739 QEMUIOVector *qiov,
740 int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200741 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200742 void *opaque)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100743{
Josh Durgin787f3132012-04-30 23:16:45 -0700744 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
745 RBD_AIO_WRITE);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100746}
747
Josh Durgindc7588c2013-03-29 13:03:23 -0700748#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200749static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
Markus Armbruster097310b2014-10-07 13:59:15 +0200750 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200751 void *opaque)
Josh Durgindc7588c2013-03-29 13:03:23 -0700752{
753 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
754}
755
756#else
757
Paolo Bonzini8b94ff82011-10-20 13:16:24 +0200758static int qemu_rbd_co_flush(BlockDriverState *bs)
Sage Weil7a3f5fe2011-09-15 14:11:11 -0700759{
760#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
761 /* rbd_flush added in 0.1.1 */
762 BDRVRBDState *s = bs->opaque;
763 return rbd_flush(s->image);
764#else
765 return 0;
766#endif
767}
Josh Durgindc7588c2013-03-29 13:03:23 -0700768#endif
Sage Weil7a3f5fe2011-09-15 14:11:11 -0700769
Josh Durginad32e9c2011-05-26 16:07:31 -0700770static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100771{
772 BDRVRBDState *s = bs->opaque;
Josh Durginad32e9c2011-05-26 16:07:31 -0700773 rbd_image_info_t info;
774 int r;
775
776 r = rbd_stat(s->image, &info, sizeof(info));
777 if (r < 0) {
778 return r;
779 }
780
781 bdi->cluster_size = info.obj_size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100782 return 0;
783}
784
Josh Durginad32e9c2011-05-26 16:07:31 -0700785static int64_t qemu_rbd_getlength(BlockDriverState *bs)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100786{
787 BDRVRBDState *s = bs->opaque;
Josh Durginad32e9c2011-05-26 16:07:31 -0700788 rbd_image_info_t info;
789 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100790
Josh Durginad32e9c2011-05-26 16:07:31 -0700791 r = rbd_stat(s->image, &info, sizeof(info));
792 if (r < 0) {
793 return r;
794 }
795
796 return info.size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100797}
798
Josh Durgin30cdc482011-05-26 16:07:34 -0700799static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
800{
801 BDRVRBDState *s = bs->opaque;
802 int r;
803
804 r = rbd_resize(s->image, offset);
805 if (r < 0) {
806 return r;
807 }
808
809 return 0;
810}
811
Josh Durginad32e9c2011-05-26 16:07:31 -0700812static int qemu_rbd_snap_create(BlockDriverState *bs,
813 QEMUSnapshotInfo *sn_info)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100814{
815 BDRVRBDState *s = bs->opaque;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100816 int r;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100817
818 if (sn_info->name[0] == '\0') {
819 return -EINVAL; /* we need a name for rbd snapshots */
820 }
821
822 /*
823 * rbd snapshots are using the name as the user controlled unique identifier
824 * we can't use the rbd snapid for that purpose, as it can't be set
825 */
826 if (sn_info->id_str[0] != '\0' &&
827 strcmp(sn_info->id_str, sn_info->name) != 0) {
828 return -EINVAL;
829 }
830
831 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
832 return -ERANGE;
833 }
834
Josh Durginad32e9c2011-05-26 16:07:31 -0700835 r = rbd_snap_create(s->image, sn_info->name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100836 if (r < 0) {
Josh Durginad32e9c2011-05-26 16:07:31 -0700837 error_report("failed to create snap: %s", strerror(-r));
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100838 return r;
839 }
840
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100841 return 0;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100842}
843
Gregory Farnumbd603242012-01-11 11:53:52 -0800844static int qemu_rbd_snap_remove(BlockDriverState *bs,
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800845 const char *snapshot_id,
846 const char *snapshot_name,
847 Error **errp)
Gregory Farnumbd603242012-01-11 11:53:52 -0800848{
849 BDRVRBDState *s = bs->opaque;
850 int r;
851
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800852 if (!snapshot_name) {
853 error_setg(errp, "rbd need a valid snapshot name");
854 return -EINVAL;
855 }
856
857 /* If snapshot_id is specified, it must be equal to name, see
858 qemu_rbd_snap_list() */
859 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
860 error_setg(errp,
861 "rbd do not support snapshot id, it should be NULL or "
862 "equal to snapshot name");
863 return -EINVAL;
864 }
865
Gregory Farnumbd603242012-01-11 11:53:52 -0800866 r = rbd_snap_remove(s->image, snapshot_name);
Wenchao Xiaa89d89d2013-09-11 14:04:33 +0800867 if (r < 0) {
868 error_setg_errno(errp, -r, "Failed to remove the snapshot");
869 }
Gregory Farnumbd603242012-01-11 11:53:52 -0800870 return r;
871}
872
873static int qemu_rbd_snap_rollback(BlockDriverState *bs,
874 const char *snapshot_name)
875{
876 BDRVRBDState *s = bs->opaque;
877 int r;
878
879 r = rbd_snap_rollback(s->image, snapshot_name);
880 return r;
881}
882
Josh Durginad32e9c2011-05-26 16:07:31 -0700883static int qemu_rbd_snap_list(BlockDriverState *bs,
884 QEMUSnapshotInfo **psn_tab)
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100885{
886 BDRVRBDState *s = bs->opaque;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100887 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
Josh Durginad32e9c2011-05-26 16:07:31 -0700888 int i, snap_count;
889 rbd_snap_info_t *snaps;
890 int max_snaps = RBD_MAX_SNAPS;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100891
Josh Durginad32e9c2011-05-26 16:07:31 -0700892 do {
Markus Armbruster02c4f262014-08-19 10:31:09 +0200893 snaps = g_new(rbd_snap_info_t, max_snaps);
Josh Durginad32e9c2011-05-26 16:07:31 -0700894 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
Stefan Hajnoczi9e6337d2013-09-25 16:00:48 +0200895 if (snap_count <= 0) {
Anthony Liguori7267c092011-08-20 22:09:37 -0500896 g_free(snaps);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100897 }
Josh Durginad32e9c2011-05-26 16:07:31 -0700898 } while (snap_count == -ERANGE);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100899
Josh Durginad32e9c2011-05-26 16:07:31 -0700900 if (snap_count <= 0) {
Josh Durginb9c53292011-12-06 17:05:10 -0800901 goto done;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100902 }
903
Markus Armbruster5839e532014-08-19 10:31:08 +0200904 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100905
Josh Durginad32e9c2011-05-26 16:07:31 -0700906 for (i = 0; i < snap_count; i++) {
907 const char *snap_name = snaps[i].name;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100908
909 sn_info = sn_tab + i;
910 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
911 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100912
Josh Durginad32e9c2011-05-26 16:07:31 -0700913 sn_info->vm_state_size = snaps[i].size;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100914 sn_info->date_sec = 0;
915 sn_info->date_nsec = 0;
916 sn_info->vm_clock_nsec = 0;
917 }
Josh Durginad32e9c2011-05-26 16:07:31 -0700918 rbd_snap_list_end(snaps);
Stefan Hajnoczi9e6337d2013-09-25 16:00:48 +0200919 g_free(snaps);
Josh Durginad32e9c2011-05-26 16:07:31 -0700920
Josh Durginb9c53292011-12-06 17:05:10 -0800921 done:
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100922 *psn_tab = sn_tab;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100923 return snap_count;
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100924}
925
Josh Durgin787f3132012-04-30 23:16:45 -0700926#ifdef LIBRBD_SUPPORTS_DISCARD
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200927static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
928 int64_t sector_num,
929 int nb_sectors,
Markus Armbruster097310b2014-10-07 13:59:15 +0200930 BlockCompletionFunc *cb,
Markus Armbruster7c84b1b2014-10-07 13:59:14 +0200931 void *opaque)
Josh Durgin787f3132012-04-30 23:16:45 -0700932{
933 return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
934 RBD_AIO_DISCARD);
935}
936#endif
937
Adam Crumebe217882014-10-09 11:44:32 -0700938#ifdef LIBRBD_SUPPORTS_INVALIDATE
939static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
940 Error **errp)
941{
942 BDRVRBDState *s = bs->opaque;
943 int r = rbd_invalidate_cache(s->image);
944 if (r < 0) {
945 error_setg_errno(errp, -r, "Failed to invalidate the cache");
946 }
947}
948#endif
949
Chunyan Liubd0cf592014-06-05 17:21:04 +0800950static QemuOptsList qemu_rbd_create_opts = {
951 .name = "rbd-create-opts",
952 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
953 .desc = {
954 {
955 .name = BLOCK_OPT_SIZE,
956 .type = QEMU_OPT_SIZE,
957 .help = "Virtual disk size"
958 },
959 {
960 .name = BLOCK_OPT_CLUSTER_SIZE,
961 .type = QEMU_OPT_SIZE,
962 .help = "RBD object size"
963 },
Daniel P. Berrange60390a22016-01-21 14:19:19 +0000964 {
965 .name = "password-secret",
966 .type = QEMU_OPT_STRING,
967 .help = "ID of secret providing the password",
968 },
Chunyan Liubd0cf592014-06-05 17:21:04 +0800969 { /* end of list */ }
970 }
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100971};
972
973static BlockDriver bdrv_rbd = {
974 .format_name = "rbd",
975 .instance_size = sizeof(BDRVRBDState),
BenoƮt Canet030be322013-09-24 17:07:04 +0200976 .bdrv_needs_filename = true,
Josh Durginad32e9c2011-05-26 16:07:31 -0700977 .bdrv_file_open = qemu_rbd_open,
978 .bdrv_close = qemu_rbd_close,
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800979 .bdrv_create = qemu_rbd_create,
Peter Lieven3ac21622013-06-28 12:47:42 +0200980 .bdrv_has_zero_init = bdrv_has_zero_init_1,
Josh Durginad32e9c2011-05-26 16:07:31 -0700981 .bdrv_get_info = qemu_rbd_getinfo,
Chunyan Liubd0cf592014-06-05 17:21:04 +0800982 .create_opts = &qemu_rbd_create_opts,
Josh Durginad32e9c2011-05-26 16:07:31 -0700983 .bdrv_getlength = qemu_rbd_getlength,
Josh Durgin30cdc482011-05-26 16:07:34 -0700984 .bdrv_truncate = qemu_rbd_truncate,
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100985 .protocol_name = "rbd",
986
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100987 .bdrv_aio_readv = qemu_rbd_aio_readv,
988 .bdrv_aio_writev = qemu_rbd_aio_writev,
Josh Durgindc7588c2013-03-29 13:03:23 -0700989
990#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
991 .bdrv_aio_flush = qemu_rbd_aio_flush,
992#else
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100993 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
Josh Durgindc7588c2013-03-29 13:03:23 -0700994#endif
Christian Brunnerf27aaf42010-12-06 20:53:01 +0100995
Josh Durgin787f3132012-04-30 23:16:45 -0700996#ifdef LIBRBD_SUPPORTS_DISCARD
997 .bdrv_aio_discard = qemu_rbd_aio_discard,
998#endif
999
Kevin Wolfc68b89a2011-11-10 17:25:44 +01001000 .bdrv_snapshot_create = qemu_rbd_snap_create,
Gregory Farnumbd603242012-01-11 11:53:52 -08001001 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
Kevin Wolfc68b89a2011-11-10 17:25:44 +01001002 .bdrv_snapshot_list = qemu_rbd_snap_list,
Gregory Farnumbd603242012-01-11 11:53:52 -08001003 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
Adam Crumebe217882014-10-09 11:44:32 -07001004#ifdef LIBRBD_SUPPORTS_INVALIDATE
1005 .bdrv_invalidate_cache = qemu_rbd_invalidate_cache,
1006#endif
Christian Brunnerf27aaf42010-12-06 20:53:01 +01001007};
1008
1009static void bdrv_rbd_init(void)
1010{
1011 bdrv_register(&bdrv_rbd);
1012}
1013
1014block_init(bdrv_rbd_init);