memory: Replace skip_dump flag with "ram_device"
[qemu/kevin.git] / block / rbd.c
blobf6e1d4bc11275bcef4b1ca868574e29b0583a511
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 "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "block/block_int.h"
19 #include "crypto/secret.h"
20 #include "qemu/cutils.h"
22 #include <rbd/librbd.h>
25 * When specifying the image filename use:
27 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
29 * poolname must be the name of an existing rados pool.
31 * devicename is the name of the rbd image.
33 * Each option given is used to configure rados, and may be any valid
34 * Ceph option, "id", or "conf".
36 * The "id" option indicates what user we should authenticate as to
37 * the Ceph cluster. If it is excluded we will use the Ceph default
38 * (normally 'admin').
40 * The "conf" option specifies a Ceph configuration file to read. If
41 * it is not specified, we will read from the default Ceph locations
42 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
43 * file, specify conf=/dev/null.
45 * Configuration values containing :, @, or = can be escaped with a
46 * leading "\".
49 /* rbd_aio_discard added in 0.1.2 */
50 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
51 #define LIBRBD_SUPPORTS_DISCARD
52 #else
53 #undef LIBRBD_SUPPORTS_DISCARD
54 #endif
56 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
58 #define RBD_MAX_CONF_NAME_SIZE 128
59 #define RBD_MAX_CONF_VAL_SIZE 512
60 #define RBD_MAX_CONF_SIZE 1024
61 #define RBD_MAX_POOL_NAME_SIZE 128
62 #define RBD_MAX_SNAP_NAME_SIZE 128
63 #define RBD_MAX_SNAPS 100
65 typedef enum {
66 RBD_AIO_READ,
67 RBD_AIO_WRITE,
68 RBD_AIO_DISCARD,
69 RBD_AIO_FLUSH
70 } RBDAIOCmd;
72 typedef struct RBDAIOCB {
73 BlockAIOCB common;
74 int64_t ret;
75 QEMUIOVector *qiov;
76 char *bounce;
77 RBDAIOCmd cmd;
78 int error;
79 struct BDRVRBDState *s;
80 } RBDAIOCB;
82 typedef struct RADOSCB {
83 RBDAIOCB *acb;
84 struct BDRVRBDState *s;
85 int64_t size;
86 char *buf;
87 int64_t ret;
88 } RADOSCB;
90 typedef struct BDRVRBDState {
91 rados_t cluster;
92 rados_ioctx_t io_ctx;
93 rbd_image_t image;
94 char name[RBD_MAX_IMAGE_NAME_SIZE];
95 char *snap;
96 } BDRVRBDState;
98 static int qemu_rbd_next_tok(char *dst, int dst_len,
99 char *src, char delim,
100 const char *name,
101 char **p, Error **errp)
103 int l;
104 char *end;
106 *p = NULL;
108 if (delim != '\0') {
109 for (end = src; *end; ++end) {
110 if (*end == delim) {
111 break;
113 if (*end == '\\' && end[1] != '\0') {
114 end++;
117 if (*end == delim) {
118 *p = end + 1;
119 *end = '\0';
122 l = strlen(src);
123 if (l >= dst_len) {
124 error_setg(errp, "%s too long", name);
125 return -EINVAL;
126 } else if (l == 0) {
127 error_setg(errp, "%s too short", name);
128 return -EINVAL;
131 pstrcpy(dst, dst_len, src);
133 return 0;
136 static void qemu_rbd_unescape(char *src)
138 char *p;
140 for (p = src; *src; ++src, ++p) {
141 if (*src == '\\' && src[1] != '\0') {
142 src++;
144 *p = *src;
146 *p = '\0';
149 static int qemu_rbd_parsename(const char *filename,
150 char *pool, int pool_len,
151 char *snap, int snap_len,
152 char *name, int name_len,
153 char *conf, int conf_len,
154 Error **errp)
156 const char *start;
157 char *p, *buf;
158 int ret;
160 if (!strstart(filename, "rbd:", &start)) {
161 error_setg(errp, "File name must start with 'rbd:'");
162 return -EINVAL;
165 buf = g_strdup(start);
166 p = buf;
167 *snap = '\0';
168 *conf = '\0';
170 ret = qemu_rbd_next_tok(pool, pool_len, p,
171 '/', "pool name", &p, errp);
172 if (ret < 0 || !p) {
173 ret = -EINVAL;
174 goto done;
176 qemu_rbd_unescape(pool);
178 if (strchr(p, '@')) {
179 ret = qemu_rbd_next_tok(name, name_len, p,
180 '@', "object name", &p, errp);
181 if (ret < 0) {
182 goto done;
184 ret = qemu_rbd_next_tok(snap, snap_len, p,
185 ':', "snap name", &p, errp);
186 qemu_rbd_unescape(snap);
187 } else {
188 ret = qemu_rbd_next_tok(name, name_len, p,
189 ':', "object name", &p, errp);
191 qemu_rbd_unescape(name);
192 if (ret < 0 || !p) {
193 goto done;
196 ret = qemu_rbd_next_tok(conf, conf_len, p,
197 '\0', "configuration", &p, errp);
199 done:
200 g_free(buf);
201 return ret;
204 static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
206 const char *p = conf;
208 while (*p) {
209 int len;
210 const char *end = strchr(p, ':');
212 if (end) {
213 len = end - p;
214 } else {
215 len = strlen(p);
218 if (strncmp(p, "id=", 3) == 0) {
219 len -= 3;
220 strncpy(clientname, p + 3, len);
221 clientname[len] = '\0';
222 return clientname;
224 if (end == NULL) {
225 break;
227 p = end + 1;
229 return NULL;
233 static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
234 Error **errp)
236 if (secretid == 0) {
237 return 0;
240 gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
241 errp);
242 if (!secret) {
243 return -1;
246 rados_conf_set(cluster, "key", secret);
247 g_free(secret);
249 return 0;
253 static int qemu_rbd_set_conf(rados_t cluster, const char *conf,
254 bool only_read_conf_file,
255 Error **errp)
257 char *p, *buf;
258 char name[RBD_MAX_CONF_NAME_SIZE];
259 char value[RBD_MAX_CONF_VAL_SIZE];
260 int ret = 0;
262 buf = g_strdup(conf);
263 p = buf;
265 while (p) {
266 ret = qemu_rbd_next_tok(name, sizeof(name), p,
267 '=', "conf option name", &p, errp);
268 if (ret < 0) {
269 break;
271 qemu_rbd_unescape(name);
273 if (!p) {
274 error_setg(errp, "conf option %s has no value", name);
275 ret = -EINVAL;
276 break;
279 ret = qemu_rbd_next_tok(value, sizeof(value), p,
280 ':', "conf option value", &p, errp);
281 if (ret < 0) {
282 break;
284 qemu_rbd_unescape(value);
286 if (strcmp(name, "conf") == 0) {
287 /* read the conf file alone, so it doesn't override more
288 specific settings for a particular device */
289 if (only_read_conf_file) {
290 ret = rados_conf_read_file(cluster, value);
291 if (ret < 0) {
292 error_setg_errno(errp, -ret, "error reading conf file %s",
293 value);
294 break;
297 } else if (strcmp(name, "id") == 0) {
298 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
299 } else if (!only_read_conf_file) {
300 ret = rados_conf_set(cluster, name, value);
301 if (ret < 0) {
302 error_setg_errno(errp, -ret, "invalid conf option %s", name);
303 ret = -EINVAL;
304 break;
309 g_free(buf);
310 return ret;
313 static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
315 Error *local_err = NULL;
316 int64_t bytes = 0;
317 int64_t objsize;
318 int obj_order = 0;
319 char pool[RBD_MAX_POOL_NAME_SIZE];
320 char name[RBD_MAX_IMAGE_NAME_SIZE];
321 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
322 char conf[RBD_MAX_CONF_SIZE];
323 char clientname_buf[RBD_MAX_CONF_SIZE];
324 char *clientname;
325 const char *secretid;
326 rados_t cluster;
327 rados_ioctx_t io_ctx;
328 int ret;
330 secretid = qemu_opt_get(opts, "password-secret");
332 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
333 snap_buf, sizeof(snap_buf),
334 name, sizeof(name),
335 conf, sizeof(conf), &local_err) < 0) {
336 error_propagate(errp, local_err);
337 return -EINVAL;
340 /* Read out options */
341 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
342 BDRV_SECTOR_SIZE);
343 objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
344 if (objsize) {
345 if ((objsize - 1) & objsize) { /* not a power of 2? */
346 error_setg(errp, "obj size needs to be power of 2");
347 return -EINVAL;
349 if (objsize < 4096) {
350 error_setg(errp, "obj size too small");
351 return -EINVAL;
353 obj_order = ctz32(objsize);
356 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
357 ret = rados_create(&cluster, clientname);
358 if (ret < 0) {
359 error_setg_errno(errp, -ret, "error initializing");
360 return ret;
363 if (strstr(conf, "conf=") == NULL) {
364 /* try default location, but ignore failure */
365 rados_conf_read_file(cluster, NULL);
366 } else if (conf[0] != '\0' &&
367 qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
368 rados_shutdown(cluster);
369 error_propagate(errp, local_err);
370 return -EIO;
373 if (conf[0] != '\0' &&
374 qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
375 rados_shutdown(cluster);
376 error_propagate(errp, local_err);
377 return -EIO;
380 if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
381 rados_shutdown(cluster);
382 return -EIO;
385 ret = rados_connect(cluster);
386 if (ret < 0) {
387 error_setg_errno(errp, -ret, "error connecting");
388 rados_shutdown(cluster);
389 return ret;
392 ret = rados_ioctx_create(cluster, pool, &io_ctx);
393 if (ret < 0) {
394 error_setg_errno(errp, -ret, "error opening pool %s", pool);
395 rados_shutdown(cluster);
396 return ret;
399 ret = rbd_create(io_ctx, name, bytes, &obj_order);
400 rados_ioctx_destroy(io_ctx);
401 rados_shutdown(cluster);
402 if (ret < 0) {
403 error_setg_errno(errp, -ret, "error rbd create");
404 return ret;
407 return ret;
411 * This aio completion is being called from rbd_finish_bh() and runs in qemu
412 * BH context.
414 static void qemu_rbd_complete_aio(RADOSCB *rcb)
416 RBDAIOCB *acb = rcb->acb;
417 int64_t r;
419 r = rcb->ret;
421 if (acb->cmd != RBD_AIO_READ) {
422 if (r < 0) {
423 acb->ret = r;
424 acb->error = 1;
425 } else if (!acb->error) {
426 acb->ret = rcb->size;
428 } else {
429 if (r < 0) {
430 memset(rcb->buf, 0, rcb->size);
431 acb->ret = r;
432 acb->error = 1;
433 } else if (r < rcb->size) {
434 memset(rcb->buf + r, 0, rcb->size - r);
435 if (!acb->error) {
436 acb->ret = rcb->size;
438 } else if (!acb->error) {
439 acb->ret = r;
443 g_free(rcb);
445 if (acb->cmd == RBD_AIO_READ) {
446 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
448 qemu_vfree(acb->bounce);
449 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
451 qemu_aio_unref(acb);
454 /* TODO Convert to fine grained options */
455 static QemuOptsList runtime_opts = {
456 .name = "rbd",
457 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
458 .desc = {
460 .name = "filename",
461 .type = QEMU_OPT_STRING,
462 .help = "Specification of the rbd image",
465 .name = "password-secret",
466 .type = QEMU_OPT_STRING,
467 .help = "ID of secret providing the password",
469 { /* end of list */ }
473 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
474 Error **errp)
476 BDRVRBDState *s = bs->opaque;
477 char pool[RBD_MAX_POOL_NAME_SIZE];
478 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
479 char conf[RBD_MAX_CONF_SIZE];
480 char clientname_buf[RBD_MAX_CONF_SIZE];
481 char *clientname;
482 const char *secretid;
483 QemuOpts *opts;
484 Error *local_err = NULL;
485 const char *filename;
486 int r;
488 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
489 qemu_opts_absorb_qdict(opts, options, &local_err);
490 if (local_err) {
491 error_propagate(errp, local_err);
492 qemu_opts_del(opts);
493 return -EINVAL;
496 filename = qemu_opt_get(opts, "filename");
497 secretid = qemu_opt_get(opts, "password-secret");
499 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
500 snap_buf, sizeof(snap_buf),
501 s->name, sizeof(s->name),
502 conf, sizeof(conf), errp) < 0) {
503 r = -EINVAL;
504 goto failed_opts;
507 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
508 r = rados_create(&s->cluster, clientname);
509 if (r < 0) {
510 error_setg_errno(errp, -r, "error initializing");
511 goto failed_opts;
514 s->snap = NULL;
515 if (snap_buf[0] != '\0') {
516 s->snap = g_strdup(snap_buf);
519 if (strstr(conf, "conf=") == NULL) {
520 /* try default location, but ignore failure */
521 rados_conf_read_file(s->cluster, NULL);
522 } else if (conf[0] != '\0') {
523 r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
524 if (r < 0) {
525 goto failed_shutdown;
529 if (conf[0] != '\0') {
530 r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
531 if (r < 0) {
532 goto failed_shutdown;
536 if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
537 r = -EIO;
538 goto failed_shutdown;
542 * Fallback to more conservative semantics if setting cache
543 * options fails. Ignore errors from setting rbd_cache because the
544 * only possible error is that the option does not exist, and
545 * librbd defaults to no caching. If write through caching cannot
546 * be set up, fall back to no caching.
548 if (flags & BDRV_O_NOCACHE) {
549 rados_conf_set(s->cluster, "rbd_cache", "false");
550 } else {
551 rados_conf_set(s->cluster, "rbd_cache", "true");
554 r = rados_connect(s->cluster);
555 if (r < 0) {
556 error_setg_errno(errp, -r, "error connecting");
557 goto failed_shutdown;
560 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
561 if (r < 0) {
562 error_setg_errno(errp, -r, "error opening pool %s", pool);
563 goto failed_shutdown;
566 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
567 if (r < 0) {
568 error_setg_errno(errp, -r, "error reading header from %s", s->name);
569 goto failed_open;
572 bs->read_only = (s->snap != NULL);
574 qemu_opts_del(opts);
575 return 0;
577 failed_open:
578 rados_ioctx_destroy(s->io_ctx);
579 failed_shutdown:
580 rados_shutdown(s->cluster);
581 g_free(s->snap);
582 failed_opts:
583 qemu_opts_del(opts);
584 return r;
587 static void qemu_rbd_close(BlockDriverState *bs)
589 BDRVRBDState *s = bs->opaque;
591 rbd_close(s->image);
592 rados_ioctx_destroy(s->io_ctx);
593 g_free(s->snap);
594 rados_shutdown(s->cluster);
597 static const AIOCBInfo rbd_aiocb_info = {
598 .aiocb_size = sizeof(RBDAIOCB),
601 static void rbd_finish_bh(void *opaque)
603 RADOSCB *rcb = opaque;
604 qemu_rbd_complete_aio(rcb);
608 * This is the callback function for rbd_aio_read and _write
610 * Note: this function is being called from a non qemu thread so
611 * we need to be careful about what we do here. Generally we only
612 * schedule a BH, and do the rest of the io completion handling
613 * from rbd_finish_bh() which runs in a qemu context.
615 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
617 RBDAIOCB *acb = rcb->acb;
619 rcb->ret = rbd_aio_get_return_value(c);
620 rbd_aio_release(c);
622 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
623 rbd_finish_bh, rcb);
626 static int rbd_aio_discard_wrapper(rbd_image_t image,
627 uint64_t off,
628 uint64_t len,
629 rbd_completion_t comp)
631 #ifdef LIBRBD_SUPPORTS_DISCARD
632 return rbd_aio_discard(image, off, len, comp);
633 #else
634 return -ENOTSUP;
635 #endif
638 static int rbd_aio_flush_wrapper(rbd_image_t image,
639 rbd_completion_t comp)
641 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
642 return rbd_aio_flush(image, comp);
643 #else
644 return -ENOTSUP;
645 #endif
648 static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
649 int64_t off,
650 QEMUIOVector *qiov,
651 int64_t size,
652 BlockCompletionFunc *cb,
653 void *opaque,
654 RBDAIOCmd cmd)
656 RBDAIOCB *acb;
657 RADOSCB *rcb = NULL;
658 rbd_completion_t c;
659 char *buf;
660 int r;
662 BDRVRBDState *s = bs->opaque;
664 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
665 acb->cmd = cmd;
666 acb->qiov = qiov;
667 assert(!qiov || qiov->size == size);
668 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
669 acb->bounce = NULL;
670 } else {
671 acb->bounce = qemu_try_blockalign(bs, qiov->size);
672 if (acb->bounce == NULL) {
673 goto failed;
676 acb->ret = 0;
677 acb->error = 0;
678 acb->s = s;
680 if (cmd == RBD_AIO_WRITE) {
681 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
684 buf = acb->bounce;
686 rcb = g_new(RADOSCB, 1);
687 rcb->acb = acb;
688 rcb->buf = buf;
689 rcb->s = acb->s;
690 rcb->size = size;
691 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
692 if (r < 0) {
693 goto failed;
696 switch (cmd) {
697 case RBD_AIO_WRITE:
698 r = rbd_aio_write(s->image, off, size, buf, c);
699 break;
700 case RBD_AIO_READ:
701 r = rbd_aio_read(s->image, off, size, buf, c);
702 break;
703 case RBD_AIO_DISCARD:
704 r = rbd_aio_discard_wrapper(s->image, off, size, c);
705 break;
706 case RBD_AIO_FLUSH:
707 r = rbd_aio_flush_wrapper(s->image, c);
708 break;
709 default:
710 r = -EINVAL;
713 if (r < 0) {
714 goto failed_completion;
717 return &acb->common;
719 failed_completion:
720 rbd_aio_release(c);
721 failed:
722 g_free(rcb);
723 qemu_vfree(acb->bounce);
724 qemu_aio_unref(acb);
725 return NULL;
728 static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
729 int64_t sector_num,
730 QEMUIOVector *qiov,
731 int nb_sectors,
732 BlockCompletionFunc *cb,
733 void *opaque)
735 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
736 (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
737 RBD_AIO_READ);
740 static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
741 int64_t sector_num,
742 QEMUIOVector *qiov,
743 int nb_sectors,
744 BlockCompletionFunc *cb,
745 void *opaque)
747 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
748 (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
749 RBD_AIO_WRITE);
752 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
753 static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
754 BlockCompletionFunc *cb,
755 void *opaque)
757 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
760 #else
762 static int qemu_rbd_co_flush(BlockDriverState *bs)
764 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
765 /* rbd_flush added in 0.1.1 */
766 BDRVRBDState *s = bs->opaque;
767 return rbd_flush(s->image);
768 #else
769 return 0;
770 #endif
772 #endif
774 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
776 BDRVRBDState *s = bs->opaque;
777 rbd_image_info_t info;
778 int r;
780 r = rbd_stat(s->image, &info, sizeof(info));
781 if (r < 0) {
782 return r;
785 bdi->cluster_size = info.obj_size;
786 return 0;
789 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
791 BDRVRBDState *s = bs->opaque;
792 rbd_image_info_t info;
793 int r;
795 r = rbd_stat(s->image, &info, sizeof(info));
796 if (r < 0) {
797 return r;
800 return info.size;
803 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
805 BDRVRBDState *s = bs->opaque;
806 int r;
808 r = rbd_resize(s->image, offset);
809 if (r < 0) {
810 return r;
813 return 0;
816 static int qemu_rbd_snap_create(BlockDriverState *bs,
817 QEMUSnapshotInfo *sn_info)
819 BDRVRBDState *s = bs->opaque;
820 int r;
822 if (sn_info->name[0] == '\0') {
823 return -EINVAL; /* we need a name for rbd snapshots */
827 * rbd snapshots are using the name as the user controlled unique identifier
828 * we can't use the rbd snapid for that purpose, as it can't be set
830 if (sn_info->id_str[0] != '\0' &&
831 strcmp(sn_info->id_str, sn_info->name) != 0) {
832 return -EINVAL;
835 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
836 return -ERANGE;
839 r = rbd_snap_create(s->image, sn_info->name);
840 if (r < 0) {
841 error_report("failed to create snap: %s", strerror(-r));
842 return r;
845 return 0;
848 static int qemu_rbd_snap_remove(BlockDriverState *bs,
849 const char *snapshot_id,
850 const char *snapshot_name,
851 Error **errp)
853 BDRVRBDState *s = bs->opaque;
854 int r;
856 if (!snapshot_name) {
857 error_setg(errp, "rbd need a valid snapshot name");
858 return -EINVAL;
861 /* If snapshot_id is specified, it must be equal to name, see
862 qemu_rbd_snap_list() */
863 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
864 error_setg(errp,
865 "rbd do not support snapshot id, it should be NULL or "
866 "equal to snapshot name");
867 return -EINVAL;
870 r = rbd_snap_remove(s->image, snapshot_name);
871 if (r < 0) {
872 error_setg_errno(errp, -r, "Failed to remove the snapshot");
874 return r;
877 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
878 const char *snapshot_name)
880 BDRVRBDState *s = bs->opaque;
882 return rbd_snap_rollback(s->image, snapshot_name);
885 static int qemu_rbd_snap_list(BlockDriverState *bs,
886 QEMUSnapshotInfo **psn_tab)
888 BDRVRBDState *s = bs->opaque;
889 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
890 int i, snap_count;
891 rbd_snap_info_t *snaps;
892 int max_snaps = RBD_MAX_SNAPS;
894 do {
895 snaps = g_new(rbd_snap_info_t, max_snaps);
896 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
897 if (snap_count <= 0) {
898 g_free(snaps);
900 } while (snap_count == -ERANGE);
902 if (snap_count <= 0) {
903 goto done;
906 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
908 for (i = 0; i < snap_count; i++) {
909 const char *snap_name = snaps[i].name;
911 sn_info = sn_tab + i;
912 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
913 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
915 sn_info->vm_state_size = snaps[i].size;
916 sn_info->date_sec = 0;
917 sn_info->date_nsec = 0;
918 sn_info->vm_clock_nsec = 0;
920 rbd_snap_list_end(snaps);
921 g_free(snaps);
923 done:
924 *psn_tab = sn_tab;
925 return snap_count;
928 #ifdef LIBRBD_SUPPORTS_DISCARD
929 static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
930 int64_t offset,
931 int count,
932 BlockCompletionFunc *cb,
933 void *opaque)
935 return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
936 RBD_AIO_DISCARD);
938 #endif
940 #ifdef LIBRBD_SUPPORTS_INVALIDATE
941 static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
942 Error **errp)
944 BDRVRBDState *s = bs->opaque;
945 int r = rbd_invalidate_cache(s->image);
946 if (r < 0) {
947 error_setg_errno(errp, -r, "Failed to invalidate the cache");
950 #endif
952 static QemuOptsList qemu_rbd_create_opts = {
953 .name = "rbd-create-opts",
954 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
955 .desc = {
957 .name = BLOCK_OPT_SIZE,
958 .type = QEMU_OPT_SIZE,
959 .help = "Virtual disk size"
962 .name = BLOCK_OPT_CLUSTER_SIZE,
963 .type = QEMU_OPT_SIZE,
964 .help = "RBD object size"
967 .name = "password-secret",
968 .type = QEMU_OPT_STRING,
969 .help = "ID of secret providing the password",
971 { /* end of list */ }
975 static BlockDriver bdrv_rbd = {
976 .format_name = "rbd",
977 .instance_size = sizeof(BDRVRBDState),
978 .bdrv_needs_filename = true,
979 .bdrv_file_open = qemu_rbd_open,
980 .bdrv_close = qemu_rbd_close,
981 .bdrv_create = qemu_rbd_create,
982 .bdrv_has_zero_init = bdrv_has_zero_init_1,
983 .bdrv_get_info = qemu_rbd_getinfo,
984 .create_opts = &qemu_rbd_create_opts,
985 .bdrv_getlength = qemu_rbd_getlength,
986 .bdrv_truncate = qemu_rbd_truncate,
987 .protocol_name = "rbd",
989 .bdrv_aio_readv = qemu_rbd_aio_readv,
990 .bdrv_aio_writev = qemu_rbd_aio_writev,
992 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
993 .bdrv_aio_flush = qemu_rbd_aio_flush,
994 #else
995 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
996 #endif
998 #ifdef LIBRBD_SUPPORTS_DISCARD
999 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
1000 #endif
1002 .bdrv_snapshot_create = qemu_rbd_snap_create,
1003 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1004 .bdrv_snapshot_list = qemu_rbd_snap_list,
1005 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
1006 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1007 .bdrv_invalidate_cache = qemu_rbd_invalidate_cache,
1008 #endif
1011 static void bdrv_rbd_init(void)
1013 bdrv_register(&bdrv_rbd);
1016 block_init(bdrv_rbd_init);