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