qdev: Implement named GPIOs
[qemu/kevin.git] / block / rbd.c
blob09af48426e9d6d4458f879a736481d0ab363a5d0
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 RBD_AIO_FLUSH
68 } RBDAIOCmd;
70 typedef struct RBDAIOCB {
71 BlockDriverAIOCB common;
72 QEMUBH *bh;
73 int64_t ret;
74 QEMUIOVector *qiov;
75 char *bounce;
76 RBDAIOCmd cmd;
77 int64_t sector_num;
78 int error;
79 struct BDRVRBDState *s;
80 int cancelled;
81 int status;
82 } RBDAIOCB;
84 typedef struct RADOSCB {
85 int rcbid;
86 RBDAIOCB *acb;
87 struct BDRVRBDState *s;
88 int done;
89 int64_t size;
90 char *buf;
91 int64_t ret;
92 } RADOSCB;
94 #define RBD_FD_READ 0
95 #define RBD_FD_WRITE 1
97 typedef struct BDRVRBDState {
98 rados_t cluster;
99 rados_ioctx_t io_ctx;
100 rbd_image_t image;
101 char name[RBD_MAX_IMAGE_NAME_SIZE];
102 char *snap;
103 } BDRVRBDState;
105 static int qemu_rbd_next_tok(char *dst, int dst_len,
106 char *src, char delim,
107 const char *name,
108 char **p, Error **errp)
110 int l;
111 char *end;
113 *p = NULL;
115 if (delim != '\0') {
116 for (end = src; *end; ++end) {
117 if (*end == delim) {
118 break;
120 if (*end == '\\' && end[1] != '\0') {
121 end++;
124 if (*end == delim) {
125 *p = end + 1;
126 *end = '\0';
129 l = strlen(src);
130 if (l >= dst_len) {
131 error_setg(errp, "%s too long", name);
132 return -EINVAL;
133 } else if (l == 0) {
134 error_setg(errp, "%s too short", name);
135 return -EINVAL;
138 pstrcpy(dst, dst_len, src);
140 return 0;
143 static void qemu_rbd_unescape(char *src)
145 char *p;
147 for (p = src; *src; ++src, ++p) {
148 if (*src == '\\' && src[1] != '\0') {
149 src++;
151 *p = *src;
153 *p = '\0';
156 static int qemu_rbd_parsename(const char *filename,
157 char *pool, int pool_len,
158 char *snap, int snap_len,
159 char *name, int name_len,
160 char *conf, int conf_len,
161 Error **errp)
163 const char *start;
164 char *p, *buf;
165 int ret;
167 if (!strstart(filename, "rbd:", &start)) {
168 error_setg(errp, "File name must start with 'rbd:'");
169 return -EINVAL;
172 buf = g_strdup(start);
173 p = buf;
174 *snap = '\0';
175 *conf = '\0';
177 ret = qemu_rbd_next_tok(pool, pool_len, p,
178 '/', "pool name", &p, errp);
179 if (ret < 0 || !p) {
180 ret = -EINVAL;
181 goto done;
183 qemu_rbd_unescape(pool);
185 if (strchr(p, '@')) {
186 ret = qemu_rbd_next_tok(name, name_len, p,
187 '@', "object name", &p, errp);
188 if (ret < 0) {
189 goto done;
191 ret = qemu_rbd_next_tok(snap, snap_len, p,
192 ':', "snap name", &p, errp);
193 qemu_rbd_unescape(snap);
194 } else {
195 ret = qemu_rbd_next_tok(name, name_len, p,
196 ':', "object name", &p, errp);
198 qemu_rbd_unescape(name);
199 if (ret < 0 || !p) {
200 goto done;
203 ret = qemu_rbd_next_tok(conf, conf_len, p,
204 '\0', "configuration", &p, errp);
206 done:
207 g_free(buf);
208 return ret;
211 static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
213 const char *p = conf;
215 while (*p) {
216 int len;
217 const char *end = strchr(p, ':');
219 if (end) {
220 len = end - p;
221 } else {
222 len = strlen(p);
225 if (strncmp(p, "id=", 3) == 0) {
226 len -= 3;
227 strncpy(clientname, p + 3, len);
228 clientname[len] = '\0';
229 return clientname;
231 if (end == NULL) {
232 break;
234 p = end + 1;
236 return NULL;
239 static int qemu_rbd_set_conf(rados_t cluster, const char *conf, Error **errp)
241 char *p, *buf;
242 char name[RBD_MAX_CONF_NAME_SIZE];
243 char value[RBD_MAX_CONF_VAL_SIZE];
244 int ret = 0;
246 buf = g_strdup(conf);
247 p = buf;
249 while (p) {
250 ret = qemu_rbd_next_tok(name, sizeof(name), p,
251 '=', "conf option name", &p, errp);
252 if (ret < 0) {
253 break;
255 qemu_rbd_unescape(name);
257 if (!p) {
258 error_setg(errp, "conf option %s has no value", name);
259 ret = -EINVAL;
260 break;
263 ret = qemu_rbd_next_tok(value, sizeof(value), p,
264 ':', "conf option value", &p, errp);
265 if (ret < 0) {
266 break;
268 qemu_rbd_unescape(value);
270 if (strcmp(name, "conf") == 0) {
271 ret = rados_conf_read_file(cluster, value);
272 if (ret < 0) {
273 error_setg(errp, "error reading conf file %s", value);
274 break;
276 } else if (strcmp(name, "id") == 0) {
277 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
278 } else {
279 ret = rados_conf_set(cluster, name, value);
280 if (ret < 0) {
281 error_setg(errp, "invalid conf option %s", name);
282 ret = -EINVAL;
283 break;
288 g_free(buf);
289 return ret;
292 static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options,
293 Error **errp)
295 Error *local_err = NULL;
296 int64_t bytes = 0;
297 int64_t objsize;
298 int obj_order = 0;
299 char pool[RBD_MAX_POOL_NAME_SIZE];
300 char name[RBD_MAX_IMAGE_NAME_SIZE];
301 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
302 char conf[RBD_MAX_CONF_SIZE];
303 char clientname_buf[RBD_MAX_CONF_SIZE];
304 char *clientname;
305 rados_t cluster;
306 rados_ioctx_t io_ctx;
307 int ret;
309 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
310 snap_buf, sizeof(snap_buf),
311 name, sizeof(name),
312 conf, sizeof(conf), &local_err) < 0) {
313 error_propagate(errp, local_err);
314 return -EINVAL;
317 /* Read out options */
318 while (options && options->name) {
319 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
320 bytes = options->value.n;
321 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
322 if (options->value.n) {
323 objsize = options->value.n;
324 if ((objsize - 1) & objsize) { /* not a power of 2? */
325 error_setg(errp, "obj size needs to be power of 2");
326 return -EINVAL;
328 if (objsize < 4096) {
329 error_setg(errp, "obj size too small");
330 return -EINVAL;
332 obj_order = ffs(objsize) - 1;
335 options++;
338 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
339 if (rados_create(&cluster, clientname) < 0) {
340 error_setg(errp, "error initializing");
341 return -EIO;
344 if (strstr(conf, "conf=") == NULL) {
345 /* try default location, but ignore failure */
346 rados_conf_read_file(cluster, NULL);
349 if (conf[0] != '\0' &&
350 qemu_rbd_set_conf(cluster, conf, &local_err) < 0) {
351 rados_shutdown(cluster);
352 error_propagate(errp, local_err);
353 return -EIO;
356 if (rados_connect(cluster) < 0) {
357 error_setg(errp, "error connecting");
358 rados_shutdown(cluster);
359 return -EIO;
362 if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
363 error_setg(errp, "error opening pool %s", pool);
364 rados_shutdown(cluster);
365 return -EIO;
368 ret = rbd_create(io_ctx, name, bytes, &obj_order);
369 rados_ioctx_destroy(io_ctx);
370 rados_shutdown(cluster);
372 return ret;
376 * This aio completion is being called from rbd_finish_bh() and runs in qemu
377 * BH context.
379 static void qemu_rbd_complete_aio(RADOSCB *rcb)
381 RBDAIOCB *acb = rcb->acb;
382 int64_t r;
384 r = rcb->ret;
386 if (acb->cmd != RBD_AIO_READ) {
387 if (r < 0) {
388 acb->ret = r;
389 acb->error = 1;
390 } else if (!acb->error) {
391 acb->ret = rcb->size;
393 } else {
394 if (r < 0) {
395 memset(rcb->buf, 0, rcb->size);
396 acb->ret = r;
397 acb->error = 1;
398 } else if (r < rcb->size) {
399 memset(rcb->buf + r, 0, rcb->size - r);
400 if (!acb->error) {
401 acb->ret = rcb->size;
403 } else if (!acb->error) {
404 acb->ret = r;
408 g_free(rcb);
410 if (acb->cmd == RBD_AIO_READ) {
411 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
413 qemu_vfree(acb->bounce);
414 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
415 acb->status = 0;
417 if (!acb->cancelled) {
418 qemu_aio_release(acb);
422 /* TODO Convert to fine grained options */
423 static QemuOptsList runtime_opts = {
424 .name = "rbd",
425 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
426 .desc = {
428 .name = "filename",
429 .type = QEMU_OPT_STRING,
430 .help = "Specification of the rbd image",
432 { /* end of list */ }
436 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
437 Error **errp)
439 BDRVRBDState *s = bs->opaque;
440 char pool[RBD_MAX_POOL_NAME_SIZE];
441 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
442 char conf[RBD_MAX_CONF_SIZE];
443 char clientname_buf[RBD_MAX_CONF_SIZE];
444 char *clientname;
445 QemuOpts *opts;
446 Error *local_err = NULL;
447 const char *filename;
448 int r;
450 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
451 qemu_opts_absorb_qdict(opts, options, &local_err);
452 if (local_err) {
453 error_propagate(errp, local_err);
454 qemu_opts_del(opts);
455 return -EINVAL;
458 filename = qemu_opt_get(opts, "filename");
460 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
461 snap_buf, sizeof(snap_buf),
462 s->name, sizeof(s->name),
463 conf, sizeof(conf), errp) < 0) {
464 r = -EINVAL;
465 goto failed_opts;
468 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
469 r = rados_create(&s->cluster, clientname);
470 if (r < 0) {
471 error_setg(&local_err, "error initializing");
472 goto failed_opts;
475 s->snap = NULL;
476 if (snap_buf[0] != '\0') {
477 s->snap = g_strdup(snap_buf);
481 * Fallback to more conservative semantics if setting cache
482 * options fails. Ignore errors from setting rbd_cache because the
483 * only possible error is that the option does not exist, and
484 * librbd defaults to no caching. If write through caching cannot
485 * be set up, fall back to no caching.
487 if (flags & BDRV_O_NOCACHE) {
488 rados_conf_set(s->cluster, "rbd_cache", "false");
489 } else {
490 rados_conf_set(s->cluster, "rbd_cache", "true");
493 if (strstr(conf, "conf=") == NULL) {
494 /* try default location, but ignore failure */
495 rados_conf_read_file(s->cluster, NULL);
498 if (conf[0] != '\0') {
499 r = qemu_rbd_set_conf(s->cluster, conf, errp);
500 if (r < 0) {
501 goto failed_shutdown;
505 r = rados_connect(s->cluster);
506 if (r < 0) {
507 error_setg(&local_err, "error connecting");
508 goto failed_shutdown;
511 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
512 if (r < 0) {
513 error_setg(&local_err, "error opening pool %s", pool);
514 goto failed_shutdown;
517 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
518 if (r < 0) {
519 error_setg(&local_err, "error reading header from %s", s->name);
520 goto failed_open;
523 bs->read_only = (s->snap != NULL);
525 qemu_opts_del(opts);
526 return 0;
528 failed_open:
529 rados_ioctx_destroy(s->io_ctx);
530 failed_shutdown:
531 rados_shutdown(s->cluster);
532 g_free(s->snap);
533 failed_opts:
534 qemu_opts_del(opts);
535 return r;
538 static void qemu_rbd_close(BlockDriverState *bs)
540 BDRVRBDState *s = bs->opaque;
542 rbd_close(s->image);
543 rados_ioctx_destroy(s->io_ctx);
544 g_free(s->snap);
545 rados_shutdown(s->cluster);
549 * Cancel aio. Since we don't reference acb in a non qemu threads,
550 * it is safe to access it here.
552 static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb)
554 RBDAIOCB *acb = (RBDAIOCB *) blockacb;
555 acb->cancelled = 1;
557 while (acb->status == -EINPROGRESS) {
558 qemu_aio_wait();
561 qemu_aio_release(acb);
564 static const AIOCBInfo rbd_aiocb_info = {
565 .aiocb_size = sizeof(RBDAIOCB),
566 .cancel = qemu_rbd_aio_cancel,
569 static void rbd_finish_bh(void *opaque)
571 RADOSCB *rcb = opaque;
572 qemu_bh_delete(rcb->acb->bh);
573 qemu_rbd_complete_aio(rcb);
577 * This is the callback function for rbd_aio_read and _write
579 * Note: this function is being called from a non qemu thread so
580 * we need to be careful about what we do here. Generally we only
581 * schedule a BH, and do the rest of the io completion handling
582 * from rbd_finish_bh() which runs in a qemu context.
584 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
586 RBDAIOCB *acb = rcb->acb;
588 rcb->ret = rbd_aio_get_return_value(c);
589 rbd_aio_release(c);
591 acb->bh = qemu_bh_new(rbd_finish_bh, rcb);
592 qemu_bh_schedule(acb->bh);
595 static int rbd_aio_discard_wrapper(rbd_image_t image,
596 uint64_t off,
597 uint64_t len,
598 rbd_completion_t comp)
600 #ifdef LIBRBD_SUPPORTS_DISCARD
601 return rbd_aio_discard(image, off, len, comp);
602 #else
603 return -ENOTSUP;
604 #endif
607 static int rbd_aio_flush_wrapper(rbd_image_t image,
608 rbd_completion_t comp)
610 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
611 return rbd_aio_flush(image, comp);
612 #else
613 return -ENOTSUP;
614 #endif
617 static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs,
618 int64_t sector_num,
619 QEMUIOVector *qiov,
620 int nb_sectors,
621 BlockDriverCompletionFunc *cb,
622 void *opaque,
623 RBDAIOCmd cmd)
625 RBDAIOCB *acb;
626 RADOSCB *rcb;
627 rbd_completion_t c;
628 int64_t off, size;
629 char *buf;
630 int r;
632 BDRVRBDState *s = bs->opaque;
634 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
635 acb->cmd = cmd;
636 acb->qiov = qiov;
637 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
638 acb->bounce = NULL;
639 } else {
640 acb->bounce = qemu_blockalign(bs, qiov->size);
642 acb->ret = 0;
643 acb->error = 0;
644 acb->s = s;
645 acb->cancelled = 0;
646 acb->bh = NULL;
647 acb->status = -EINPROGRESS;
649 if (cmd == RBD_AIO_WRITE) {
650 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
653 buf = acb->bounce;
655 off = sector_num * BDRV_SECTOR_SIZE;
656 size = nb_sectors * BDRV_SECTOR_SIZE;
658 rcb = g_malloc(sizeof(RADOSCB));
659 rcb->done = 0;
660 rcb->acb = acb;
661 rcb->buf = buf;
662 rcb->s = acb->s;
663 rcb->size = size;
664 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
665 if (r < 0) {
666 goto failed;
669 switch (cmd) {
670 case RBD_AIO_WRITE:
671 r = rbd_aio_write(s->image, off, size, buf, c);
672 break;
673 case RBD_AIO_READ:
674 r = rbd_aio_read(s->image, off, size, buf, c);
675 break;
676 case RBD_AIO_DISCARD:
677 r = rbd_aio_discard_wrapper(s->image, off, size, c);
678 break;
679 case RBD_AIO_FLUSH:
680 r = rbd_aio_flush_wrapper(s->image, c);
681 break;
682 default:
683 r = -EINVAL;
686 if (r < 0) {
687 goto failed;
690 return &acb->common;
692 failed:
693 g_free(rcb);
694 qemu_aio_release(acb);
695 return NULL;
698 static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
699 int64_t sector_num,
700 QEMUIOVector *qiov,
701 int nb_sectors,
702 BlockDriverCompletionFunc *cb,
703 void *opaque)
705 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
706 RBD_AIO_READ);
709 static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
710 int64_t sector_num,
711 QEMUIOVector *qiov,
712 int nb_sectors,
713 BlockDriverCompletionFunc *cb,
714 void *opaque)
716 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
717 RBD_AIO_WRITE);
720 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
721 static BlockDriverAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
722 BlockDriverCompletionFunc *cb,
723 void *opaque)
725 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
728 #else
730 static int qemu_rbd_co_flush(BlockDriverState *bs)
732 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
733 /* rbd_flush added in 0.1.1 */
734 BDRVRBDState *s = bs->opaque;
735 return rbd_flush(s->image);
736 #else
737 return 0;
738 #endif
740 #endif
742 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
744 BDRVRBDState *s = bs->opaque;
745 rbd_image_info_t info;
746 int r;
748 r = rbd_stat(s->image, &info, sizeof(info));
749 if (r < 0) {
750 return r;
753 bdi->cluster_size = info.obj_size;
754 return 0;
757 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
759 BDRVRBDState *s = bs->opaque;
760 rbd_image_info_t info;
761 int r;
763 r = rbd_stat(s->image, &info, sizeof(info));
764 if (r < 0) {
765 return r;
768 return info.size;
771 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
773 BDRVRBDState *s = bs->opaque;
774 int r;
776 r = rbd_resize(s->image, offset);
777 if (r < 0) {
778 return r;
781 return 0;
784 static int qemu_rbd_snap_create(BlockDriverState *bs,
785 QEMUSnapshotInfo *sn_info)
787 BDRVRBDState *s = bs->opaque;
788 int r;
790 if (sn_info->name[0] == '\0') {
791 return -EINVAL; /* we need a name for rbd snapshots */
795 * rbd snapshots are using the name as the user controlled unique identifier
796 * we can't use the rbd snapid for that purpose, as it can't be set
798 if (sn_info->id_str[0] != '\0' &&
799 strcmp(sn_info->id_str, sn_info->name) != 0) {
800 return -EINVAL;
803 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
804 return -ERANGE;
807 r = rbd_snap_create(s->image, sn_info->name);
808 if (r < 0) {
809 error_report("failed to create snap: %s", strerror(-r));
810 return r;
813 return 0;
816 static int qemu_rbd_snap_remove(BlockDriverState *bs,
817 const char *snapshot_id,
818 const char *snapshot_name,
819 Error **errp)
821 BDRVRBDState *s = bs->opaque;
822 int r;
824 if (!snapshot_name) {
825 error_setg(errp, "rbd need a valid snapshot name");
826 return -EINVAL;
829 /* If snapshot_id is specified, it must be equal to name, see
830 qemu_rbd_snap_list() */
831 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
832 error_setg(errp,
833 "rbd do not support snapshot id, it should be NULL or "
834 "equal to snapshot name");
835 return -EINVAL;
838 r = rbd_snap_remove(s->image, snapshot_name);
839 if (r < 0) {
840 error_setg_errno(errp, -r, "Failed to remove the snapshot");
842 return r;
845 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
846 const char *snapshot_name)
848 BDRVRBDState *s = bs->opaque;
849 int r;
851 r = rbd_snap_rollback(s->image, snapshot_name);
852 return r;
855 static int qemu_rbd_snap_list(BlockDriverState *bs,
856 QEMUSnapshotInfo **psn_tab)
858 BDRVRBDState *s = bs->opaque;
859 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
860 int i, snap_count;
861 rbd_snap_info_t *snaps;
862 int max_snaps = RBD_MAX_SNAPS;
864 do {
865 snaps = g_malloc(sizeof(*snaps) * max_snaps);
866 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
867 if (snap_count <= 0) {
868 g_free(snaps);
870 } while (snap_count == -ERANGE);
872 if (snap_count <= 0) {
873 goto done;
876 sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo));
878 for (i = 0; i < snap_count; i++) {
879 const char *snap_name = snaps[i].name;
881 sn_info = sn_tab + i;
882 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
883 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
885 sn_info->vm_state_size = snaps[i].size;
886 sn_info->date_sec = 0;
887 sn_info->date_nsec = 0;
888 sn_info->vm_clock_nsec = 0;
890 rbd_snap_list_end(snaps);
891 g_free(snaps);
893 done:
894 *psn_tab = sn_tab;
895 return snap_count;
898 #ifdef LIBRBD_SUPPORTS_DISCARD
899 static BlockDriverAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
900 int64_t sector_num,
901 int nb_sectors,
902 BlockDriverCompletionFunc *cb,
903 void *opaque)
905 return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
906 RBD_AIO_DISCARD);
908 #endif
910 static QEMUOptionParameter qemu_rbd_create_options[] = {
912 .name = BLOCK_OPT_SIZE,
913 .type = OPT_SIZE,
914 .help = "Virtual disk size"
917 .name = BLOCK_OPT_CLUSTER_SIZE,
918 .type = OPT_SIZE,
919 .help = "RBD object size"
921 {NULL}
924 static BlockDriver bdrv_rbd = {
925 .format_name = "rbd",
926 .instance_size = sizeof(BDRVRBDState),
927 .bdrv_needs_filename = true,
928 .bdrv_file_open = qemu_rbd_open,
929 .bdrv_close = qemu_rbd_close,
930 .bdrv_create = qemu_rbd_create,
931 .bdrv_has_zero_init = bdrv_has_zero_init_1,
932 .bdrv_get_info = qemu_rbd_getinfo,
933 .create_options = qemu_rbd_create_options,
934 .bdrv_getlength = qemu_rbd_getlength,
935 .bdrv_truncate = qemu_rbd_truncate,
936 .protocol_name = "rbd",
938 .bdrv_aio_readv = qemu_rbd_aio_readv,
939 .bdrv_aio_writev = qemu_rbd_aio_writev,
941 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
942 .bdrv_aio_flush = qemu_rbd_aio_flush,
943 #else
944 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
945 #endif
947 #ifdef LIBRBD_SUPPORTS_DISCARD
948 .bdrv_aio_discard = qemu_rbd_aio_discard,
949 #endif
951 .bdrv_snapshot_create = qemu_rbd_snap_create,
952 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
953 .bdrv_snapshot_list = qemu_rbd_snap_list,
954 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
957 static void bdrv_rbd_init(void)
959 bdrv_register(&bdrv_rbd);
962 block_init(bdrv_rbd_init);