hmp: human-monitor-command: stop using the Memory chardev driver
[qemu/ar7.git] / block / rbd.c
blob1a8ea6d492c4cd369bf6b421a6d567d489d79183
1 /*
2 * QEMU Block driver for RADOS (Ceph)
4 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
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.
14 #include <inttypes.h>
16 #include "qemu-common.h"
17 #include "qemu/error-report.h"
18 #include "block/block_int.h"
20 #include <rbd/librbd.h>
23 * When specifying the image filename use:
25 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
27 * poolname must be the name of an existing rados pool.
29 * devicename is the name of the rbd image.
31 * Each option given is used to configure rados, and may be any valid
32 * Ceph option, "id", or "conf".
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').
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.
43 * Configuration values containing :, @, or = can be escaped with a
44 * leading "\".
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
54 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
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
63 typedef enum {
64 RBD_AIO_READ,
65 RBD_AIO_WRITE,
66 RBD_AIO_DISCARD
67 } RBDAIOCmd;
69 typedef struct RBDAIOCB {
70 BlockDriverAIOCB common;
71 QEMUBH *bh;
72 int64_t ret;
73 QEMUIOVector *qiov;
74 char *bounce;
75 RBDAIOCmd cmd;
76 int64_t sector_num;
77 int error;
78 struct BDRVRBDState *s;
79 int cancelled;
80 int status;
81 } RBDAIOCB;
83 typedef struct RADOSCB {
84 int rcbid;
85 RBDAIOCB *acb;
86 struct BDRVRBDState *s;
87 int done;
88 int64_t size;
89 char *buf;
90 int64_t ret;
91 } RADOSCB;
93 #define RBD_FD_READ 0
94 #define RBD_FD_WRITE 1
96 typedef struct BDRVRBDState {
97 int fds[2];
98 rados_t cluster;
99 rados_ioctx_t io_ctx;
100 rbd_image_t image;
101 char name[RBD_MAX_IMAGE_NAME_SIZE];
102 int qemu_aio_count;
103 char *snap;
104 int event_reader_pos;
105 RADOSCB *event_rcb;
106 } BDRVRBDState;
108 static void rbd_aio_bh_cb(void *opaque);
110 static int qemu_rbd_next_tok(char *dst, int dst_len,
111 char *src, char delim,
112 const char *name,
113 char **p)
115 int l;
116 char *end;
118 *p = NULL;
120 if (delim != '\0') {
121 for (end = src; *end; ++end) {
122 if (*end == delim) {
123 break;
125 if (*end == '\\' && end[1] != '\0') {
126 end++;
129 if (*end == delim) {
130 *p = end + 1;
131 *end = '\0';
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;
143 pstrcpy(dst, dst_len, src);
145 return 0;
148 static void qemu_rbd_unescape(char *src)
150 char *p;
152 for (p = src; *src; ++src, ++p) {
153 if (*src == '\\' && src[1] != '\0') {
154 src++;
156 *p = *src;
158 *p = '\0';
161 static int qemu_rbd_parsename(const char *filename,
162 char *pool, int pool_len,
163 char *snap, int snap_len,
164 char *name, int name_len,
165 char *conf, int conf_len)
167 const char *start;
168 char *p, *buf;
169 int ret;
171 if (!strstart(filename, "rbd:", &start)) {
172 return -EINVAL;
175 buf = g_strdup(start);
176 p = buf;
177 *snap = '\0';
178 *conf = '\0';
180 ret = qemu_rbd_next_tok(pool, pool_len, p, '/', "pool name", &p);
181 if (ret < 0 || !p) {
182 ret = -EINVAL;
183 goto done;
185 qemu_rbd_unescape(pool);
187 if (strchr(p, '@')) {
188 ret = qemu_rbd_next_tok(name, name_len, p, '@', "object name", &p);
189 if (ret < 0) {
190 goto done;
192 ret = qemu_rbd_next_tok(snap, snap_len, p, ':', "snap name", &p);
193 qemu_rbd_unescape(snap);
194 } else {
195 ret = qemu_rbd_next_tok(name, name_len, p, ':', "object name", &p);
197 qemu_rbd_unescape(name);
198 if (ret < 0 || !p) {
199 goto done;
202 ret = qemu_rbd_next_tok(conf, conf_len, p, '\0', "configuration", &p);
204 done:
205 g_free(buf);
206 return ret;
209 static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
211 const char *p = conf;
213 while (*p) {
214 int len;
215 const char *end = strchr(p, ':');
217 if (end) {
218 len = end - p;
219 } else {
220 len = strlen(p);
223 if (strncmp(p, "id=", 3) == 0) {
224 len -= 3;
225 strncpy(clientname, p + 3, len);
226 clientname[len] = '\0';
227 return clientname;
229 if (end == NULL) {
230 break;
232 p = end + 1;
234 return NULL;
237 static int qemu_rbd_set_conf(rados_t cluster, const char *conf)
239 char *p, *buf;
240 char name[RBD_MAX_CONF_NAME_SIZE];
241 char value[RBD_MAX_CONF_VAL_SIZE];
242 int ret = 0;
244 buf = g_strdup(conf);
245 p = buf;
247 while (p) {
248 ret = qemu_rbd_next_tok(name, sizeof(name), p,
249 '=', "conf option name", &p);
250 if (ret < 0) {
251 break;
253 qemu_rbd_unescape(name);
255 if (!p) {
256 error_report("conf option %s has no value", name);
257 ret = -EINVAL;
258 break;
261 ret = qemu_rbd_next_tok(value, sizeof(value), p,
262 ':', "conf option value", &p);
263 if (ret < 0) {
264 break;
266 qemu_rbd_unescape(value);
268 if (strcmp(name, "conf") == 0) {
269 ret = rados_conf_read_file(cluster, value);
270 if (ret < 0) {
271 error_report("error reading conf file %s", value);
272 break;
274 } else if (strcmp(name, "id") == 0) {
275 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
276 } else {
277 ret = rados_conf_set(cluster, name, value);
278 if (ret < 0) {
279 error_report("invalid conf option %s", name);
280 ret = -EINVAL;
281 break;
286 g_free(buf);
287 return ret;
290 static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options)
292 int64_t bytes = 0;
293 int64_t objsize;
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];
298 char conf[RBD_MAX_CONF_SIZE];
299 char clientname_buf[RBD_MAX_CONF_SIZE];
300 char *clientname;
301 rados_t cluster;
302 rados_ioctx_t io_ctx;
303 int ret;
305 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
306 snap_buf, sizeof(snap_buf),
307 name, sizeof(name),
308 conf, sizeof(conf)) < 0) {
309 return -EINVAL;
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;
323 if (objsize < 4096) {
324 error_report("obj size too small");
325 return -EINVAL;
327 obj_order = ffs(objsize) - 1;
330 options++;
333 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
334 if (rados_create(&cluster, clientname) < 0) {
335 error_report("error initializing");
336 return -EIO;
339 if (strstr(conf, "conf=") == NULL) {
340 /* try default location, but ignore failure */
341 rados_conf_read_file(cluster, NULL);
344 if (conf[0] != '\0' &&
345 qemu_rbd_set_conf(cluster, conf) < 0) {
346 error_report("error setting config options");
347 rados_shutdown(cluster);
348 return -EIO;
351 if (rados_connect(cluster) < 0) {
352 error_report("error connecting");
353 rados_shutdown(cluster);
354 return -EIO;
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;
363 ret = rbd_create(io_ctx, name, bytes, &obj_order);
364 rados_ioctx_destroy(io_ctx);
365 rados_shutdown(cluster);
367 return ret;
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
373 * was not cancelled before.
375 static void qemu_rbd_complete_aio(RADOSCB *rcb)
377 RBDAIOCB *acb = rcb->acb;
378 int64_t r;
380 r = rcb->ret;
382 if (acb->cmd == RBD_AIO_WRITE ||
383 acb->cmd == RBD_AIO_DISCARD) {
384 if (r < 0) {
385 acb->ret = r;
386 acb->error = 1;
387 } else if (!acb->error) {
388 acb->ret = rcb->size;
390 } else {
391 if (r < 0) {
392 memset(rcb->buf, 0, rcb->size);
393 acb->ret = r;
394 acb->error = 1;
395 } else if (r < rcb->size) {
396 memset(rcb->buf + r, 0, rcb->size - r);
397 if (!acb->error) {
398 acb->ret = rcb->size;
400 } else if (!acb->error) {
401 acb->ret = r;
404 /* Note that acb->bh can be NULL in case where the aio was cancelled */
405 acb->bh = qemu_bh_new(rbd_aio_bh_cb, acb);
406 qemu_bh_schedule(acb->bh);
407 g_free(rcb);
411 * aio fd read handler. It runs in the qemu context and calls the
412 * completion handling of completed rados aio operations.
414 static void qemu_rbd_aio_event_reader(void *opaque)
416 BDRVRBDState *s = opaque;
418 ssize_t ret;
420 do {
421 char *p = (char *)&s->event_rcb;
423 /* now read the rcb pointer that was sent from a non qemu thread */
424 ret = read(s->fds[RBD_FD_READ], p + s->event_reader_pos,
425 sizeof(s->event_rcb) - s->event_reader_pos);
426 if (ret > 0) {
427 s->event_reader_pos += ret;
428 if (s->event_reader_pos == sizeof(s->event_rcb)) {
429 s->event_reader_pos = 0;
430 qemu_rbd_complete_aio(s->event_rcb);
431 s->qemu_aio_count--;
434 } while (ret < 0 && errno == EINTR);
437 static int qemu_rbd_aio_flush_cb(void *opaque)
439 BDRVRBDState *s = opaque;
441 return (s->qemu_aio_count > 0);
444 static int qemu_rbd_open(BlockDriverState *bs, const char *filename,
445 QDict *options, int flags)
447 BDRVRBDState *s = bs->opaque;
448 char pool[RBD_MAX_POOL_NAME_SIZE];
449 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
450 char conf[RBD_MAX_CONF_SIZE];
451 char clientname_buf[RBD_MAX_CONF_SIZE];
452 char *clientname;
453 int r;
455 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
456 snap_buf, sizeof(snap_buf),
457 s->name, sizeof(s->name),
458 conf, sizeof(conf)) < 0) {
459 return -EINVAL;
462 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
463 r = rados_create(&s->cluster, clientname);
464 if (r < 0) {
465 error_report("error initializing");
466 return r;
469 s->snap = NULL;
470 if (snap_buf[0] != '\0') {
471 s->snap = g_strdup(snap_buf);
475 * Fallback to more conservative semantics if setting cache
476 * options fails. Ignore errors from setting rbd_cache because the
477 * only possible error is that the option does not exist, and
478 * librbd defaults to no caching. If write through caching cannot
479 * be set up, fall back to no caching.
481 if (flags & BDRV_O_NOCACHE) {
482 rados_conf_set(s->cluster, "rbd_cache", "false");
483 } else {
484 rados_conf_set(s->cluster, "rbd_cache", "true");
487 if (strstr(conf, "conf=") == NULL) {
488 /* try default location, but ignore failure */
489 rados_conf_read_file(s->cluster, NULL);
492 if (conf[0] != '\0') {
493 r = qemu_rbd_set_conf(s->cluster, conf);
494 if (r < 0) {
495 error_report("error setting config options");
496 goto failed_shutdown;
500 r = rados_connect(s->cluster);
501 if (r < 0) {
502 error_report("error connecting");
503 goto failed_shutdown;
506 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
507 if (r < 0) {
508 error_report("error opening pool %s", pool);
509 goto failed_shutdown;
512 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
513 if (r < 0) {
514 error_report("error reading header from %s", s->name);
515 goto failed_open;
518 bs->read_only = (s->snap != NULL);
520 s->event_reader_pos = 0;
521 r = qemu_pipe(s->fds);
522 if (r < 0) {
523 error_report("error opening eventfd");
524 goto failed;
526 fcntl(s->fds[0], F_SETFL, O_NONBLOCK);
527 fcntl(s->fds[1], F_SETFL, O_NONBLOCK);
528 qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], qemu_rbd_aio_event_reader,
529 NULL, qemu_rbd_aio_flush_cb, s);
532 return 0;
534 failed:
535 rbd_close(s->image);
536 failed_open:
537 rados_ioctx_destroy(s->io_ctx);
538 failed_shutdown:
539 rados_shutdown(s->cluster);
540 g_free(s->snap);
541 return r;
544 static void qemu_rbd_close(BlockDriverState *bs)
546 BDRVRBDState *s = bs->opaque;
548 close(s->fds[0]);
549 close(s->fds[1]);
550 qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], NULL, NULL, NULL, NULL);
552 rbd_close(s->image);
553 rados_ioctx_destroy(s->io_ctx);
554 g_free(s->snap);
555 rados_shutdown(s->cluster);
559 * Cancel aio. Since we don't reference acb in a non qemu threads,
560 * it is safe to access it here.
562 static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb)
564 RBDAIOCB *acb = (RBDAIOCB *) blockacb;
565 acb->cancelled = 1;
567 while (acb->status == -EINPROGRESS) {
568 qemu_aio_wait();
571 qemu_aio_release(acb);
574 static const AIOCBInfo rbd_aiocb_info = {
575 .aiocb_size = sizeof(RBDAIOCB),
576 .cancel = qemu_rbd_aio_cancel,
579 static int qemu_rbd_send_pipe(BDRVRBDState *s, RADOSCB *rcb)
581 int ret = 0;
582 while (1) {
583 fd_set wfd;
584 int fd = s->fds[RBD_FD_WRITE];
586 /* send the op pointer to the qemu thread that is responsible
587 for the aio/op completion. Must do it in a qemu thread context */
588 ret = write(fd, (void *)&rcb, sizeof(rcb));
589 if (ret >= 0) {
590 break;
592 if (errno == EINTR) {
593 continue;
595 if (errno != EAGAIN) {
596 break;
599 FD_ZERO(&wfd);
600 FD_SET(fd, &wfd);
601 do {
602 ret = select(fd + 1, NULL, &wfd, NULL, NULL);
603 } while (ret < 0 && errno == EINTR);
606 return ret;
610 * This is the callback function for rbd_aio_read and _write
612 * Note: this function is being called from a non qemu thread so
613 * we need to be careful about what we do here. Generally we only
614 * write to the block notification pipe, and do the rest of the
615 * io completion handling from qemu_rbd_aio_event_reader() which
616 * runs in a qemu context.
618 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
620 int ret;
621 rcb->ret = rbd_aio_get_return_value(c);
622 rbd_aio_release(c);
623 ret = qemu_rbd_send_pipe(rcb->s, rcb);
624 if (ret < 0) {
625 error_report("failed writing to acb->s->fds");
626 g_free(rcb);
630 /* Callback when all queued rbd_aio requests are complete */
632 static void rbd_aio_bh_cb(void *opaque)
634 RBDAIOCB *acb = opaque;
636 if (acb->cmd == RBD_AIO_READ) {
637 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
639 qemu_vfree(acb->bounce);
640 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
641 qemu_bh_delete(acb->bh);
642 acb->bh = NULL;
643 acb->status = 0;
645 if (!acb->cancelled) {
646 qemu_aio_release(acb);
650 static int rbd_aio_discard_wrapper(rbd_image_t image,
651 uint64_t off,
652 uint64_t len,
653 rbd_completion_t comp)
655 #ifdef LIBRBD_SUPPORTS_DISCARD
656 return rbd_aio_discard(image, off, len, comp);
657 #else
658 return -ENOTSUP;
659 #endif
662 static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs,
663 int64_t sector_num,
664 QEMUIOVector *qiov,
665 int nb_sectors,
666 BlockDriverCompletionFunc *cb,
667 void *opaque,
668 RBDAIOCmd cmd)
670 RBDAIOCB *acb;
671 RADOSCB *rcb;
672 rbd_completion_t c;
673 int64_t off, size;
674 char *buf;
675 int r;
677 BDRVRBDState *s = bs->opaque;
679 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
680 acb->cmd = cmd;
681 acb->qiov = qiov;
682 if (cmd == RBD_AIO_DISCARD) {
683 acb->bounce = NULL;
684 } else {
685 acb->bounce = qemu_blockalign(bs, qiov->size);
687 acb->ret = 0;
688 acb->error = 0;
689 acb->s = s;
690 acb->cancelled = 0;
691 acb->bh = NULL;
692 acb->status = -EINPROGRESS;
694 if (cmd == RBD_AIO_WRITE) {
695 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
698 buf = acb->bounce;
700 off = sector_num * BDRV_SECTOR_SIZE;
701 size = nb_sectors * BDRV_SECTOR_SIZE;
703 s->qemu_aio_count++; /* All the RADOSCB */
705 rcb = g_malloc(sizeof(RADOSCB));
706 rcb->done = 0;
707 rcb->acb = acb;
708 rcb->buf = buf;
709 rcb->s = acb->s;
710 rcb->size = size;
711 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
712 if (r < 0) {
713 goto failed;
716 switch (cmd) {
717 case RBD_AIO_WRITE:
718 r = rbd_aio_write(s->image, off, size, buf, c);
719 break;
720 case RBD_AIO_READ:
721 r = rbd_aio_read(s->image, off, size, buf, c);
722 break;
723 case RBD_AIO_DISCARD:
724 r = rbd_aio_discard_wrapper(s->image, off, size, c);
725 break;
726 default:
727 r = -EINVAL;
730 if (r < 0) {
731 goto failed;
734 return &acb->common;
736 failed:
737 g_free(rcb);
738 s->qemu_aio_count--;
739 qemu_aio_release(acb);
740 return NULL;
743 static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
744 int64_t sector_num,
745 QEMUIOVector *qiov,
746 int nb_sectors,
747 BlockDriverCompletionFunc *cb,
748 void *opaque)
750 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
751 RBD_AIO_READ);
754 static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
755 int64_t sector_num,
756 QEMUIOVector *qiov,
757 int nb_sectors,
758 BlockDriverCompletionFunc *cb,
759 void *opaque)
761 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
762 RBD_AIO_WRITE);
765 static int qemu_rbd_co_flush(BlockDriverState *bs)
767 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
768 /* rbd_flush added in 0.1.1 */
769 BDRVRBDState *s = bs->opaque;
770 return rbd_flush(s->image);
771 #else
772 return 0;
773 #endif
776 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
778 BDRVRBDState *s = bs->opaque;
779 rbd_image_info_t info;
780 int r;
782 r = rbd_stat(s->image, &info, sizeof(info));
783 if (r < 0) {
784 return r;
787 bdi->cluster_size = info.obj_size;
788 return 0;
791 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
793 BDRVRBDState *s = bs->opaque;
794 rbd_image_info_t info;
795 int r;
797 r = rbd_stat(s->image, &info, sizeof(info));
798 if (r < 0) {
799 return r;
802 return info.size;
805 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
807 BDRVRBDState *s = bs->opaque;
808 int r;
810 r = rbd_resize(s->image, offset);
811 if (r < 0) {
812 return r;
815 return 0;
818 static int qemu_rbd_snap_create(BlockDriverState *bs,
819 QEMUSnapshotInfo *sn_info)
821 BDRVRBDState *s = bs->opaque;
822 int r;
824 if (sn_info->name[0] == '\0') {
825 return -EINVAL; /* we need a name for rbd snapshots */
829 * rbd snapshots are using the name as the user controlled unique identifier
830 * we can't use the rbd snapid for that purpose, as it can't be set
832 if (sn_info->id_str[0] != '\0' &&
833 strcmp(sn_info->id_str, sn_info->name) != 0) {
834 return -EINVAL;
837 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
838 return -ERANGE;
841 r = rbd_snap_create(s->image, sn_info->name);
842 if (r < 0) {
843 error_report("failed to create snap: %s", strerror(-r));
844 return r;
847 return 0;
850 static int qemu_rbd_snap_remove(BlockDriverState *bs,
851 const char *snapshot_name)
853 BDRVRBDState *s = bs->opaque;
854 int r;
856 r = rbd_snap_remove(s->image, snapshot_name);
857 return r;
860 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
861 const char *snapshot_name)
863 BDRVRBDState *s = bs->opaque;
864 int r;
866 r = rbd_snap_rollback(s->image, snapshot_name);
867 return r;
870 static int qemu_rbd_snap_list(BlockDriverState *bs,
871 QEMUSnapshotInfo **psn_tab)
873 BDRVRBDState *s = bs->opaque;
874 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
875 int i, snap_count;
876 rbd_snap_info_t *snaps;
877 int max_snaps = RBD_MAX_SNAPS;
879 do {
880 snaps = g_malloc(sizeof(*snaps) * max_snaps);
881 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
882 if (snap_count < 0) {
883 g_free(snaps);
885 } while (snap_count == -ERANGE);
887 if (snap_count <= 0) {
888 goto done;
891 sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo));
893 for (i = 0; i < snap_count; i++) {
894 const char *snap_name = snaps[i].name;
896 sn_info = sn_tab + i;
897 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
898 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
900 sn_info->vm_state_size = snaps[i].size;
901 sn_info->date_sec = 0;
902 sn_info->date_nsec = 0;
903 sn_info->vm_clock_nsec = 0;
905 rbd_snap_list_end(snaps);
907 done:
908 *psn_tab = sn_tab;
909 return snap_count;
912 #ifdef LIBRBD_SUPPORTS_DISCARD
913 static BlockDriverAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
914 int64_t sector_num,
915 int nb_sectors,
916 BlockDriverCompletionFunc *cb,
917 void *opaque)
919 return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
920 RBD_AIO_DISCARD);
922 #endif
924 static QEMUOptionParameter qemu_rbd_create_options[] = {
926 .name = BLOCK_OPT_SIZE,
927 .type = OPT_SIZE,
928 .help = "Virtual disk size"
931 .name = BLOCK_OPT_CLUSTER_SIZE,
932 .type = OPT_SIZE,
933 .help = "RBD object size"
935 {NULL}
938 static BlockDriver bdrv_rbd = {
939 .format_name = "rbd",
940 .instance_size = sizeof(BDRVRBDState),
941 .bdrv_file_open = qemu_rbd_open,
942 .bdrv_close = qemu_rbd_close,
943 .bdrv_create = qemu_rbd_create,
944 .bdrv_get_info = qemu_rbd_getinfo,
945 .create_options = qemu_rbd_create_options,
946 .bdrv_getlength = qemu_rbd_getlength,
947 .bdrv_truncate = qemu_rbd_truncate,
948 .protocol_name = "rbd",
950 .bdrv_aio_readv = qemu_rbd_aio_readv,
951 .bdrv_aio_writev = qemu_rbd_aio_writev,
952 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
954 #ifdef LIBRBD_SUPPORTS_DISCARD
955 .bdrv_aio_discard = qemu_rbd_aio_discard,
956 #endif
958 .bdrv_snapshot_create = qemu_rbd_snap_create,
959 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
960 .bdrv_snapshot_list = qemu_rbd_snap_list,
961 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
964 static void bdrv_rbd_init(void)
966 bdrv_register(&bdrv_rbd);
969 block_init(bdrv_rbd_init);