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