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