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