]> git.proxmox.com Git - mirror_qemu.git/blame - block/rbd.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / block / rbd.c
CommitLineData
f27aaf4b
CB
1/*
2 * QEMU Block driver for RADOS (Ceph)
3 *
ad32e9c0
JD
4 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
f27aaf4b
CB
6 *
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 *
6b620ca3
PB
10 * Contributions after 2012-01-13 are licensed under the terms of the
11 * GNU GPL, version 2 or (at your option) any later version.
f27aaf4b
CB
12 */
13
80c71a24 14#include "qemu/osdep.h"
ad32e9c0 15
da34e65c 16#include "qapi/error.h"
1de7afc9 17#include "qemu/error-report.h"
737e150e 18#include "block/block_int.h"
60390a21 19#include "crypto/secret.h"
f348b6d1 20#include "qemu/cutils.h"
f27aaf4b 21
ad32e9c0 22#include <rbd/librbd.h>
f27aaf4b 23
f27aaf4b
CB
24/*
25 * When specifying the image filename use:
26 *
fab5cf59 27 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
f27aaf4b 28 *
9e1fbcde 29 * poolname must be the name of an existing rados pool.
f27aaf4b 30 *
9e1fbcde 31 * devicename is the name of the rbd image.
f27aaf4b 32 *
9e1fbcde
SW
33 * Each option given is used to configure rados, and may be any valid
34 * Ceph option, "id", or "conf".
fab5cf59 35 *
9e1fbcde
SW
36 * The "id" option indicates what user we should authenticate as to
37 * the Ceph cluster. If it is excluded we will use the Ceph default
38 * (normally 'admin').
f27aaf4b 39 *
9e1fbcde
SW
40 * The "conf" option specifies a Ceph configuration file to read. If
41 * it is not specified, we will read from the default Ceph locations
42 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
43 * file, specify conf=/dev/null.
f27aaf4b 44 *
9e1fbcde
SW
45 * Configuration values containing :, @, or = can be escaped with a
46 * leading "\".
f27aaf4b
CB
47 */
48
787f3133
JD
49/* rbd_aio_discard added in 0.1.2 */
50#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
51#define LIBRBD_SUPPORTS_DISCARD
52#else
53#undef LIBRBD_SUPPORTS_DISCARD
54#endif
55
f27aaf4b
CB
56#define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
57
ad32e9c0
JD
58#define RBD_MAX_CONF_NAME_SIZE 128
59#define RBD_MAX_CONF_VAL_SIZE 512
60#define RBD_MAX_CONF_SIZE 1024
61#define RBD_MAX_POOL_NAME_SIZE 128
62#define RBD_MAX_SNAP_NAME_SIZE 128
63#define RBD_MAX_SNAPS 100
64
787f3133
JD
65typedef enum {
66 RBD_AIO_READ,
67 RBD_AIO_WRITE,
dc7588c1
JD
68 RBD_AIO_DISCARD,
69 RBD_AIO_FLUSH
787f3133
JD
70} RBDAIOCmd;
71
f27aaf4b 72typedef struct RBDAIOCB {
7c84b1b8 73 BlockAIOCB common;
f27aaf4b 74 QEMUBH *bh;
08448d51 75 int64_t ret;
f27aaf4b
CB
76 QEMUIOVector *qiov;
77 char *bounce;
787f3133 78 RBDAIOCmd cmd;
f27aaf4b
CB
79 int error;
80 struct BDRVRBDState *s;
f27aaf4b
CB
81} RBDAIOCB;
82
83typedef struct RADOSCB {
f27aaf4b
CB
84 RBDAIOCB *acb;
85 struct BDRVRBDState *s;
ad32e9c0 86 int64_t size;
f27aaf4b 87 char *buf;
08448d51 88 int64_t ret;
f27aaf4b
CB
89} RADOSCB;
90
f27aaf4b 91typedef struct BDRVRBDState {
ad32e9c0
JD
92 rados_t cluster;
93 rados_ioctx_t io_ctx;
94 rbd_image_t image;
95 char name[RBD_MAX_IMAGE_NAME_SIZE];
ad32e9c0 96 char *snap;
f27aaf4b
CB
97} BDRVRBDState;
98
ad32e9c0
JD
99static int qemu_rbd_next_tok(char *dst, int dst_len,
100 char *src, char delim,
101 const char *name,
d61563b2 102 char **p, Error **errp)
f27aaf4b
CB
103{
104 int l;
105 char *end;
106
107 *p = NULL;
108
109 if (delim != '\0') {
16a06b24
SW
110 for (end = src; *end; ++end) {
111 if (*end == delim) {
112 break;
113 }
114 if (*end == '\\' && end[1] != '\0') {
115 end++;
116 }
117 }
118 if (*end == delim) {
f27aaf4b
CB
119 *p = end + 1;
120 *end = '\0';
121 }
122 }
123 l = strlen(src);
124 if (l >= dst_len) {
d61563b2 125 error_setg(errp, "%s too long", name);
f27aaf4b
CB
126 return -EINVAL;
127 } else if (l == 0) {
d61563b2 128 error_setg(errp, "%s too short", name);
f27aaf4b
CB
129 return -EINVAL;
130 }
131
132 pstrcpy(dst, dst_len, src);
133
134 return 0;
135}
136
16a06b24
SW
137static void qemu_rbd_unescape(char *src)
138{
139 char *p;
140
141 for (p = src; *src; ++src, ++p) {
142 if (*src == '\\' && src[1] != '\0') {
143 src++;
144 }
145 *p = *src;
146 }
147 *p = '\0';
148}
149
ad32e9c0
JD
150static int qemu_rbd_parsename(const char *filename,
151 char *pool, int pool_len,
152 char *snap, int snap_len,
fab5cf59 153 char *name, int name_len,
d61563b2
MA
154 char *conf, int conf_len,
155 Error **errp)
f27aaf4b
CB
156{
157 const char *start;
158 char *p, *buf;
159 int ret;
160
161 if (!strstart(filename, "rbd:", &start)) {
d61563b2 162 error_setg(errp, "File name must start with 'rbd:'");
f27aaf4b
CB
163 return -EINVAL;
164 }
165
7267c094 166 buf = g_strdup(start);
f27aaf4b 167 p = buf;
fab5cf59
JD
168 *snap = '\0';
169 *conf = '\0';
f27aaf4b 170
d61563b2
MA
171 ret = qemu_rbd_next_tok(pool, pool_len, p,
172 '/', "pool name", &p, errp);
f27aaf4b
CB
173 if (ret < 0 || !p) {
174 ret = -EINVAL;
175 goto done;
176 }
16a06b24 177 qemu_rbd_unescape(pool);
fab5cf59
JD
178
179 if (strchr(p, '@')) {
d61563b2
MA
180 ret = qemu_rbd_next_tok(name, name_len, p,
181 '@', "object name", &p, errp);
fab5cf59
JD
182 if (ret < 0) {
183 goto done;
184 }
d61563b2
MA
185 ret = qemu_rbd_next_tok(snap, snap_len, p,
186 ':', "snap name", &p, errp);
16a06b24 187 qemu_rbd_unescape(snap);
fab5cf59 188 } else {
d61563b2
MA
189 ret = qemu_rbd_next_tok(name, name_len, p,
190 ':', "object name", &p, errp);
f27aaf4b 191 }
16a06b24 192 qemu_rbd_unescape(name);
fab5cf59 193 if (ret < 0 || !p) {
f27aaf4b
CB
194 goto done;
195 }
196
d61563b2
MA
197 ret = qemu_rbd_next_tok(conf, conf_len, p,
198 '\0', "configuration", &p, errp);
f27aaf4b
CB
199
200done:
7267c094 201 g_free(buf);
f27aaf4b
CB
202 return ret;
203}
204
7c7e9df0
SW
205static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
206{
207 const char *p = conf;
208
209 while (*p) {
210 int len;
211 const char *end = strchr(p, ':');
212
213 if (end) {
214 len = end - p;
215 } else {
216 len = strlen(p);
217 }
218
219 if (strncmp(p, "id=", 3) == 0) {
220 len -= 3;
221 strncpy(clientname, p + 3, len);
222 clientname[len] = '\0';
223 return clientname;
224 }
225 if (end == NULL) {
226 break;
227 }
228 p = end + 1;
229 }
230 return NULL;
231}
232
60390a21
DB
233
234static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
235 Error **errp)
236{
237 if (secretid == 0) {
238 return 0;
239 }
240
241 gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
242 errp);
243 if (!secret) {
244 return -1;
245 }
246
247 rados_conf_set(cluster, "key", secret);
248 g_free(secret);
249
250 return 0;
251}
252
253
e34d8f29
JD
254static int qemu_rbd_set_conf(rados_t cluster, const char *conf,
255 bool only_read_conf_file,
256 Error **errp)
fab5cf59
JD
257{
258 char *p, *buf;
259 char name[RBD_MAX_CONF_NAME_SIZE];
260 char value[RBD_MAX_CONF_VAL_SIZE];
261 int ret = 0;
262
7267c094 263 buf = g_strdup(conf);
fab5cf59
JD
264 p = buf;
265
266 while (p) {
267 ret = qemu_rbd_next_tok(name, sizeof(name), p,
d61563b2 268 '=', "conf option name", &p, errp);
fab5cf59
JD
269 if (ret < 0) {
270 break;
271 }
16a06b24 272 qemu_rbd_unescape(name);
fab5cf59
JD
273
274 if (!p) {
d61563b2 275 error_setg(errp, "conf option %s has no value", name);
fab5cf59
JD
276 ret = -EINVAL;
277 break;
278 }
279
280 ret = qemu_rbd_next_tok(value, sizeof(value), p,
d61563b2 281 ':', "conf option value", &p, errp);
fab5cf59
JD
282 if (ret < 0) {
283 break;
284 }
16a06b24 285 qemu_rbd_unescape(value);
fab5cf59 286
7c7e9df0 287 if (strcmp(name, "conf") == 0) {
e34d8f29
JD
288 /* read the conf file alone, so it doesn't override more
289 specific settings for a particular device */
290 if (only_read_conf_file) {
291 ret = rados_conf_read_file(cluster, value);
292 if (ret < 0) {
293 error_setg(errp, "error reading conf file %s", value);
294 break;
295 }
fab5cf59 296 }
7c7e9df0
SW
297 } else if (strcmp(name, "id") == 0) {
298 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
e34d8f29 299 } else if (!only_read_conf_file) {
7c7e9df0 300 ret = rados_conf_set(cluster, name, value);
fab5cf59 301 if (ret < 0) {
d61563b2 302 error_setg(errp, "invalid conf option %s", name);
7c7e9df0 303 ret = -EINVAL;
fab5cf59
JD
304 break;
305 }
306 }
307 }
308
7267c094 309 g_free(buf);
fab5cf59
JD
310 return ret;
311}
312
bd0cf596 313static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
f27aaf4b 314{
d61563b2 315 Error *local_err = NULL;
f27aaf4b
CB
316 int64_t bytes = 0;
317 int64_t objsize;
ad32e9c0
JD
318 int obj_order = 0;
319 char pool[RBD_MAX_POOL_NAME_SIZE];
320 char name[RBD_MAX_IMAGE_NAME_SIZE];
321 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
fab5cf59 322 char conf[RBD_MAX_CONF_SIZE];
7c7e9df0
SW
323 char clientname_buf[RBD_MAX_CONF_SIZE];
324 char *clientname;
60390a21 325 const char *secretid;
ad32e9c0
JD
326 rados_t cluster;
327 rados_ioctx_t io_ctx;
f27aaf4b
CB
328 int ret;
329
60390a21
DB
330 secretid = qemu_opt_get(opts, "password-secret");
331
ad32e9c0
JD
332 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
333 snap_buf, sizeof(snap_buf),
fab5cf59 334 name, sizeof(name),
d61563b2
MA
335 conf, sizeof(conf), &local_err) < 0) {
336 error_propagate(errp, local_err);
f27aaf4b
CB
337 return -EINVAL;
338 }
f27aaf4b 339
f27aaf4b 340 /* Read out options */
c2eb918e
HT
341 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
342 BDRV_SECTOR_SIZE);
bd0cf596
CL
343 objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
344 if (objsize) {
345 if ((objsize - 1) & objsize) { /* not a power of 2? */
346 error_setg(errp, "obj size needs to be power of 2");
347 return -EINVAL;
348 }
349 if (objsize < 4096) {
350 error_setg(errp, "obj size too small");
351 return -EINVAL;
f27aaf4b 352 }
786a4ea8 353 obj_order = ctz32(objsize);
f27aaf4b
CB
354 }
355
7c7e9df0
SW
356 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
357 if (rados_create(&cluster, clientname) < 0) {
d61563b2 358 error_setg(errp, "error initializing");
f27aaf4b
CB
359 return -EIO;
360 }
361
fab5cf59 362 if (strstr(conf, "conf=") == NULL) {
f9fe18ec
SW
363 /* try default location, but ignore failure */
364 rados_conf_read_file(cluster, NULL);
e34d8f29
JD
365 } else if (conf[0] != '\0' &&
366 qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
367 rados_shutdown(cluster);
368 error_propagate(errp, local_err);
369 return -EIO;
fab5cf59
JD
370 }
371
372 if (conf[0] != '\0' &&
e34d8f29 373 qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
ad32e9c0 374 rados_shutdown(cluster);
d61563b2 375 error_propagate(errp, local_err);
f27aaf4b
CB
376 return -EIO;
377 }
378
60390a21
DB
379 if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
380 rados_shutdown(cluster);
381 return -EIO;
382 }
383
ad32e9c0 384 if (rados_connect(cluster) < 0) {
d61563b2 385 error_setg(errp, "error connecting");
ad32e9c0 386 rados_shutdown(cluster);
f27aaf4b
CB
387 return -EIO;
388 }
f27aaf4b 389
ad32e9c0 390 if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
d61563b2 391 error_setg(errp, "error opening pool %s", pool);
ad32e9c0
JD
392 rados_shutdown(cluster);
393 return -EIO;
f27aaf4b
CB
394 }
395
ad32e9c0
JD
396 ret = rbd_create(io_ctx, name, bytes, &obj_order);
397 rados_ioctx_destroy(io_ctx);
398 rados_shutdown(cluster);
f27aaf4b
CB
399
400 return ret;
401}
402
403/*
e04fb07f
SH
404 * This aio completion is being called from rbd_finish_bh() and runs in qemu
405 * BH context.
f27aaf4b 406 */
ad32e9c0 407static void qemu_rbd_complete_aio(RADOSCB *rcb)
f27aaf4b
CB
408{
409 RBDAIOCB *acb = rcb->acb;
410 int64_t r;
411
f27aaf4b
CB
412 r = rcb->ret;
413
dc7588c1 414 if (acb->cmd != RBD_AIO_READ) {
f27aaf4b
CB
415 if (r < 0) {
416 acb->ret = r;
417 acb->error = 1;
418 } else if (!acb->error) {
ad32e9c0 419 acb->ret = rcb->size;
f27aaf4b
CB
420 }
421 } else {
ad32e9c0
JD
422 if (r < 0) {
423 memset(rcb->buf, 0, rcb->size);
f27aaf4b
CB
424 acb->ret = r;
425 acb->error = 1;
ad32e9c0
JD
426 } else if (r < rcb->size) {
427 memset(rcb->buf + r, 0, rcb->size - r);
f27aaf4b 428 if (!acb->error) {
ad32e9c0 429 acb->ret = rcb->size;
f27aaf4b
CB
430 }
431 } else if (!acb->error) {
ad32e9c0 432 acb->ret = r;
f27aaf4b
CB
433 }
434 }
f27aaf4b 435
e04fb07f 436 g_free(rcb);
f27aaf4b 437
e04fb07f
SH
438 if (acb->cmd == RBD_AIO_READ) {
439 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
440 }
441 qemu_vfree(acb->bounce);
442 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
f27aaf4b 443
8007429a 444 qemu_aio_unref(acb);
f27aaf4b
CB
445}
446
a9ccedc3
KW
447/* TODO Convert to fine grained options */
448static QemuOptsList runtime_opts = {
449 .name = "rbd",
450 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
451 .desc = {
452 {
453 .name = "filename",
454 .type = QEMU_OPT_STRING,
455 .help = "Specification of the rbd image",
456 },
60390a21
DB
457 {
458 .name = "password-secret",
459 .type = QEMU_OPT_STRING,
460 .help = "ID of secret providing the password",
461 },
a9ccedc3
KW
462 { /* end of list */ }
463 },
464};
465
015a1036
HR
466static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
467 Error **errp)
f27aaf4b
CB
468{
469 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
470 char pool[RBD_MAX_POOL_NAME_SIZE];
471 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
fab5cf59 472 char conf[RBD_MAX_CONF_SIZE];
7c7e9df0
SW
473 char clientname_buf[RBD_MAX_CONF_SIZE];
474 char *clientname;
60390a21 475 const char *secretid;
a9ccedc3
KW
476 QemuOpts *opts;
477 Error *local_err = NULL;
478 const char *filename;
f27aaf4b
CB
479 int r;
480
87ea75d5 481 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
a9ccedc3 482 qemu_opts_absorb_qdict(opts, options, &local_err);
84d18f06 483 if (local_err) {
d61563b2 484 error_propagate(errp, local_err);
a9ccedc3
KW
485 qemu_opts_del(opts);
486 return -EINVAL;
487 }
488
489 filename = qemu_opt_get(opts, "filename");
60390a21 490 secretid = qemu_opt_get(opts, "password-secret");
a9ccedc3 491
ad32e9c0
JD
492 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
493 snap_buf, sizeof(snap_buf),
fab5cf59 494 s->name, sizeof(s->name),
d61563b2 495 conf, sizeof(conf), errp) < 0) {
c3ca988d
KW
496 r = -EINVAL;
497 goto failed_opts;
f27aaf4b 498 }
f27aaf4b 499
7c7e9df0
SW
500 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
501 r = rados_create(&s->cluster, clientname);
ad32e9c0 502 if (r < 0) {
9281dbe6 503 error_setg(errp, "error initializing");
c3ca988d 504 goto failed_opts;
f27aaf4b
CB
505 }
506
eb93d5d9
SW
507 s->snap = NULL;
508 if (snap_buf[0] != '\0') {
509 s->snap = g_strdup(snap_buf);
510 }
511
99a3c89d
JD
512 if (strstr(conf, "conf=") == NULL) {
513 /* try default location, but ignore failure */
514 rados_conf_read_file(s->cluster, NULL);
e34d8f29
JD
515 } else if (conf[0] != '\0') {
516 r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
517 if (r < 0) {
518 goto failed_shutdown;
519 }
99a3c89d
JD
520 }
521
522 if (conf[0] != '\0') {
e34d8f29 523 r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
99a3c89d
JD
524 if (r < 0) {
525 goto failed_shutdown;
526 }
527 }
528
60390a21
DB
529 if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
530 r = -EIO;
531 goto failed_shutdown;
532 }
533
b11f38fc
JD
534 /*
535 * Fallback to more conservative semantics if setting cache
536 * options fails. Ignore errors from setting rbd_cache because the
537 * only possible error is that the option does not exist, and
538 * librbd defaults to no caching. If write through caching cannot
539 * be set up, fall back to no caching.
540 */
541 if (flags & BDRV_O_NOCACHE) {
542 rados_conf_set(s->cluster, "rbd_cache", "false");
543 } else {
544 rados_conf_set(s->cluster, "rbd_cache", "true");
b11f38fc
JD
545 }
546
ad32e9c0
JD
547 r = rados_connect(s->cluster);
548 if (r < 0) {
9281dbe6 549 error_setg(errp, "error connecting");
eb93d5d9 550 goto failed_shutdown;
f27aaf4b
CB
551 }
552
ad32e9c0
JD
553 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
554 if (r < 0) {
9281dbe6 555 error_setg(errp, "error opening pool %s", pool);
eb93d5d9 556 goto failed_shutdown;
f27aaf4b
CB
557 }
558
ad32e9c0 559 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
f27aaf4b 560 if (r < 0) {
9281dbe6 561 error_setg(errp, "error reading header from %s", s->name);
eb93d5d9 562 goto failed_open;
f27aaf4b
CB
563 }
564
ad32e9c0 565 bs->read_only = (s->snap != NULL);
f27aaf4b 566
c3ca988d 567 qemu_opts_del(opts);
f27aaf4b
CB
568 return 0;
569
eb93d5d9 570failed_open:
ad32e9c0 571 rados_ioctx_destroy(s->io_ctx);
eb93d5d9 572failed_shutdown:
ad32e9c0 573 rados_shutdown(s->cluster);
eb93d5d9 574 g_free(s->snap);
c3ca988d
KW
575failed_opts:
576 qemu_opts_del(opts);
f27aaf4b
CB
577 return r;
578}
579
ad32e9c0 580static void qemu_rbd_close(BlockDriverState *bs)
f27aaf4b
CB
581{
582 BDRVRBDState *s = bs->opaque;
583
ad32e9c0
JD
584 rbd_close(s->image);
585 rados_ioctx_destroy(s->io_ctx);
7267c094 586 g_free(s->snap);
ad32e9c0 587 rados_shutdown(s->cluster);
f27aaf4b
CB
588}
589
d7331bed 590static const AIOCBInfo rbd_aiocb_info = {
f27aaf4b 591 .aiocb_size = sizeof(RBDAIOCB),
f27aaf4b
CB
592};
593
e04fb07f 594static void rbd_finish_bh(void *opaque)
f27aaf4b 595{
e04fb07f
SH
596 RADOSCB *rcb = opaque;
597 qemu_bh_delete(rcb->acb->bh);
598 qemu_rbd_complete_aio(rcb);
ad32e9c0
JD
599}
600
601/*
602 * This is the callback function for rbd_aio_read and _write
603 *
604 * Note: this function is being called from a non qemu thread so
605 * we need to be careful about what we do here. Generally we only
e04fb07f
SH
606 * schedule a BH, and do the rest of the io completion handling
607 * from rbd_finish_bh() which runs in a qemu context.
ad32e9c0
JD
608 */
609static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
610{
e04fb07f
SH
611 RBDAIOCB *acb = rcb->acb;
612
ad32e9c0
JD
613 rcb->ret = rbd_aio_get_return_value(c);
614 rbd_aio_release(c);
f27aaf4b 615
ea800191
SH
616 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
617 rbd_finish_bh, rcb);
e04fb07f 618 qemu_bh_schedule(acb->bh);
f27aaf4b
CB
619}
620
787f3133
JD
621static int rbd_aio_discard_wrapper(rbd_image_t image,
622 uint64_t off,
623 uint64_t len,
624 rbd_completion_t comp)
625{
626#ifdef LIBRBD_SUPPORTS_DISCARD
627 return rbd_aio_discard(image, off, len, comp);
628#else
629 return -ENOTSUP;
630#endif
631}
632
dc7588c1
JD
633static int rbd_aio_flush_wrapper(rbd_image_t image,
634 rbd_completion_t comp)
635{
636#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
637 return rbd_aio_flush(image, comp);
638#else
639 return -ENOTSUP;
640#endif
641}
642
7c84b1b8
MA
643static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
644 int64_t sector_num,
645 QEMUIOVector *qiov,
646 int nb_sectors,
097310b5 647 BlockCompletionFunc *cb,
7c84b1b8
MA
648 void *opaque,
649 RBDAIOCmd cmd)
f27aaf4b
CB
650{
651 RBDAIOCB *acb;
0f7a0237 652 RADOSCB *rcb = NULL;
ad32e9c0 653 rbd_completion_t c;
f27aaf4b
CB
654 int64_t off, size;
655 char *buf;
51a13528 656 int r;
f27aaf4b
CB
657
658 BDRVRBDState *s = bs->opaque;
659
d7331bed 660 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
787f3133 661 acb->cmd = cmd;
f27aaf4b 662 acb->qiov = qiov;
dc7588c1 663 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
787f3133
JD
664 acb->bounce = NULL;
665 } else {
0f7a0237
KW
666 acb->bounce = qemu_try_blockalign(bs, qiov->size);
667 if (acb->bounce == NULL) {
668 goto failed;
669 }
787f3133 670 }
f27aaf4b
CB
671 acb->ret = 0;
672 acb->error = 0;
673 acb->s = s;
f27aaf4b
CB
674 acb->bh = NULL;
675
787f3133 676 if (cmd == RBD_AIO_WRITE) {
d5e6b161 677 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
f27aaf4b
CB
678 }
679
680 buf = acb->bounce;
681
682 off = sector_num * BDRV_SECTOR_SIZE;
683 size = nb_sectors * BDRV_SECTOR_SIZE;
f27aaf4b 684
5839e53b 685 rcb = g_new(RADOSCB, 1);
ad32e9c0
JD
686 rcb->acb = acb;
687 rcb->buf = buf;
688 rcb->s = acb->s;
689 rcb->size = size;
51a13528
JD
690 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
691 if (r < 0) {
692 goto failed;
693 }
f27aaf4b 694
787f3133
JD
695 switch (cmd) {
696 case RBD_AIO_WRITE:
51a13528 697 r = rbd_aio_write(s->image, off, size, buf, c);
787f3133
JD
698 break;
699 case RBD_AIO_READ:
51a13528 700 r = rbd_aio_read(s->image, off, size, buf, c);
787f3133
JD
701 break;
702 case RBD_AIO_DISCARD:
703 r = rbd_aio_discard_wrapper(s->image, off, size, c);
704 break;
dc7588c1
JD
705 case RBD_AIO_FLUSH:
706 r = rbd_aio_flush_wrapper(s->image, c);
707 break;
787f3133
JD
708 default:
709 r = -EINVAL;
51a13528
JD
710 }
711
712 if (r < 0) {
405a2764 713 goto failed_completion;
f27aaf4b
CB
714 }
715
716 return &acb->common;
51a13528 717
405a2764
KW
718failed_completion:
719 rbd_aio_release(c);
51a13528 720failed:
7267c094 721 g_free(rcb);
405a2764 722 qemu_vfree(acb->bounce);
8007429a 723 qemu_aio_unref(acb);
51a13528 724 return NULL;
f27aaf4b
CB
725}
726
7c84b1b8
MA
727static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
728 int64_t sector_num,
729 QEMUIOVector *qiov,
730 int nb_sectors,
097310b5 731 BlockCompletionFunc *cb,
7c84b1b8 732 void *opaque)
f27aaf4b 733{
787f3133
JD
734 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
735 RBD_AIO_READ);
f27aaf4b
CB
736}
737
7c84b1b8
MA
738static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
739 int64_t sector_num,
740 QEMUIOVector *qiov,
741 int nb_sectors,
097310b5 742 BlockCompletionFunc *cb,
7c84b1b8 743 void *opaque)
f27aaf4b 744{
787f3133
JD
745 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
746 RBD_AIO_WRITE);
f27aaf4b
CB
747}
748
dc7588c1 749#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
7c84b1b8 750static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
097310b5 751 BlockCompletionFunc *cb,
7c84b1b8 752 void *opaque)
dc7588c1
JD
753{
754 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
755}
756
757#else
758
8b94ff85 759static int qemu_rbd_co_flush(BlockDriverState *bs)
7a3f5fe9
SW
760{
761#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
762 /* rbd_flush added in 0.1.1 */
763 BDRVRBDState *s = bs->opaque;
764 return rbd_flush(s->image);
765#else
766 return 0;
767#endif
768}
dc7588c1 769#endif
7a3f5fe9 770
ad32e9c0 771static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
f27aaf4b
CB
772{
773 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
774 rbd_image_info_t info;
775 int r;
776
777 r = rbd_stat(s->image, &info, sizeof(info));
778 if (r < 0) {
779 return r;
780 }
781
782 bdi->cluster_size = info.obj_size;
f27aaf4b
CB
783 return 0;
784}
785
ad32e9c0 786static int64_t qemu_rbd_getlength(BlockDriverState *bs)
f27aaf4b
CB
787{
788 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
789 rbd_image_info_t info;
790 int r;
f27aaf4b 791
ad32e9c0
JD
792 r = rbd_stat(s->image, &info, sizeof(info));
793 if (r < 0) {
794 return r;
795 }
796
797 return info.size;
f27aaf4b
CB
798}
799
30cdc48c
JD
800static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
801{
802 BDRVRBDState *s = bs->opaque;
803 int r;
804
805 r = rbd_resize(s->image, offset);
806 if (r < 0) {
807 return r;
808 }
809
810 return 0;
811}
812
ad32e9c0
JD
813static int qemu_rbd_snap_create(BlockDriverState *bs,
814 QEMUSnapshotInfo *sn_info)
f27aaf4b
CB
815{
816 BDRVRBDState *s = bs->opaque;
f27aaf4b 817 int r;
f27aaf4b
CB
818
819 if (sn_info->name[0] == '\0') {
820 return -EINVAL; /* we need a name for rbd snapshots */
821 }
822
823 /*
824 * rbd snapshots are using the name as the user controlled unique identifier
825 * we can't use the rbd snapid for that purpose, as it can't be set
826 */
827 if (sn_info->id_str[0] != '\0' &&
828 strcmp(sn_info->id_str, sn_info->name) != 0) {
829 return -EINVAL;
830 }
831
832 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
833 return -ERANGE;
834 }
835
ad32e9c0 836 r = rbd_snap_create(s->image, sn_info->name);
f27aaf4b 837 if (r < 0) {
ad32e9c0 838 error_report("failed to create snap: %s", strerror(-r));
f27aaf4b
CB
839 return r;
840 }
841
f27aaf4b
CB
842 return 0;
843}
844
bd603247 845static int qemu_rbd_snap_remove(BlockDriverState *bs,
a89d89d3
WX
846 const char *snapshot_id,
847 const char *snapshot_name,
848 Error **errp)
bd603247
GF
849{
850 BDRVRBDState *s = bs->opaque;
851 int r;
852
a89d89d3
WX
853 if (!snapshot_name) {
854 error_setg(errp, "rbd need a valid snapshot name");
855 return -EINVAL;
856 }
857
858 /* If snapshot_id is specified, it must be equal to name, see
859 qemu_rbd_snap_list() */
860 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
861 error_setg(errp,
862 "rbd do not support snapshot id, it should be NULL or "
863 "equal to snapshot name");
864 return -EINVAL;
865 }
866
bd603247 867 r = rbd_snap_remove(s->image, snapshot_name);
a89d89d3
WX
868 if (r < 0) {
869 error_setg_errno(errp, -r, "Failed to remove the snapshot");
870 }
bd603247
GF
871 return r;
872}
873
874static int qemu_rbd_snap_rollback(BlockDriverState *bs,
875 const char *snapshot_name)
876{
877 BDRVRBDState *s = bs->opaque;
878 int r;
879
880 r = rbd_snap_rollback(s->image, snapshot_name);
881 return r;
882}
883
ad32e9c0
JD
884static int qemu_rbd_snap_list(BlockDriverState *bs,
885 QEMUSnapshotInfo **psn_tab)
f27aaf4b
CB
886{
887 BDRVRBDState *s = bs->opaque;
f27aaf4b 888 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
ad32e9c0
JD
889 int i, snap_count;
890 rbd_snap_info_t *snaps;
891 int max_snaps = RBD_MAX_SNAPS;
f27aaf4b 892
ad32e9c0 893 do {
02c4f26b 894 snaps = g_new(rbd_snap_info_t, max_snaps);
ad32e9c0 895 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
9e6337d0 896 if (snap_count <= 0) {
7267c094 897 g_free(snaps);
f27aaf4b 898 }
ad32e9c0 899 } while (snap_count == -ERANGE);
f27aaf4b 900
ad32e9c0 901 if (snap_count <= 0) {
b9c53290 902 goto done;
f27aaf4b
CB
903 }
904
5839e53b 905 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
f27aaf4b 906
ad32e9c0
JD
907 for (i = 0; i < snap_count; i++) {
908 const char *snap_name = snaps[i].name;
f27aaf4b
CB
909
910 sn_info = sn_tab + i;
911 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
912 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
f27aaf4b 913
ad32e9c0 914 sn_info->vm_state_size = snaps[i].size;
f27aaf4b
CB
915 sn_info->date_sec = 0;
916 sn_info->date_nsec = 0;
917 sn_info->vm_clock_nsec = 0;
918 }
ad32e9c0 919 rbd_snap_list_end(snaps);
9e6337d0 920 g_free(snaps);
ad32e9c0 921
b9c53290 922 done:
f27aaf4b 923 *psn_tab = sn_tab;
f27aaf4b 924 return snap_count;
f27aaf4b
CB
925}
926
787f3133 927#ifdef LIBRBD_SUPPORTS_DISCARD
7c84b1b8
MA
928static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
929 int64_t sector_num,
930 int nb_sectors,
097310b5 931 BlockCompletionFunc *cb,
7c84b1b8 932 void *opaque)
787f3133
JD
933{
934 return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
935 RBD_AIO_DISCARD);
936}
937#endif
938
be217884
AC
939#ifdef LIBRBD_SUPPORTS_INVALIDATE
940static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
941 Error **errp)
942{
943 BDRVRBDState *s = bs->opaque;
944 int r = rbd_invalidate_cache(s->image);
945 if (r < 0) {
946 error_setg_errno(errp, -r, "Failed to invalidate the cache");
947 }
948}
949#endif
950
bd0cf596
CL
951static QemuOptsList qemu_rbd_create_opts = {
952 .name = "rbd-create-opts",
953 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
954 .desc = {
955 {
956 .name = BLOCK_OPT_SIZE,
957 .type = QEMU_OPT_SIZE,
958 .help = "Virtual disk size"
959 },
960 {
961 .name = BLOCK_OPT_CLUSTER_SIZE,
962 .type = QEMU_OPT_SIZE,
963 .help = "RBD object size"
964 },
60390a21
DB
965 {
966 .name = "password-secret",
967 .type = QEMU_OPT_STRING,
968 .help = "ID of secret providing the password",
969 },
bd0cf596
CL
970 { /* end of list */ }
971 }
f27aaf4b
CB
972};
973
974static BlockDriver bdrv_rbd = {
975 .format_name = "rbd",
976 .instance_size = sizeof(BDRVRBDState),
030be321 977 .bdrv_needs_filename = true,
ad32e9c0
JD
978 .bdrv_file_open = qemu_rbd_open,
979 .bdrv_close = qemu_rbd_close,
c282e1fd 980 .bdrv_create = qemu_rbd_create,
3ac21627 981 .bdrv_has_zero_init = bdrv_has_zero_init_1,
ad32e9c0 982 .bdrv_get_info = qemu_rbd_getinfo,
bd0cf596 983 .create_opts = &qemu_rbd_create_opts,
ad32e9c0 984 .bdrv_getlength = qemu_rbd_getlength,
30cdc48c 985 .bdrv_truncate = qemu_rbd_truncate,
f27aaf4b
CB
986 .protocol_name = "rbd",
987
c68b89ac
KW
988 .bdrv_aio_readv = qemu_rbd_aio_readv,
989 .bdrv_aio_writev = qemu_rbd_aio_writev,
dc7588c1
JD
990
991#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
992 .bdrv_aio_flush = qemu_rbd_aio_flush,
993#else
c68b89ac 994 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
dc7588c1 995#endif
f27aaf4b 996
787f3133
JD
997#ifdef LIBRBD_SUPPORTS_DISCARD
998 .bdrv_aio_discard = qemu_rbd_aio_discard,
999#endif
1000
c68b89ac 1001 .bdrv_snapshot_create = qemu_rbd_snap_create,
bd603247 1002 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
c68b89ac 1003 .bdrv_snapshot_list = qemu_rbd_snap_list,
bd603247 1004 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
be217884
AC
1005#ifdef LIBRBD_SUPPORTS_INVALIDATE
1006 .bdrv_invalidate_cache = qemu_rbd_invalidate_cache,
1007#endif
f27aaf4b
CB
1008};
1009
1010static void bdrv_rbd_init(void)
1011{
1012 bdrv_register(&bdrv_rbd);
1013}
1014
1015block_init(bdrv_rbd_init);