nvdimm acpi: initialize the resource used by NVDIMM ACPI
[qemu/kevin.git] / block / rbd.c
blobabfea612ec2d1fa0e4f9c89e19a431d4cea4adf3
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 "qemu-common.h"
17 #include "qemu/error-report.h"
18 #include "block/block_int.h"
19 #include "crypto/secret.h"
21 #include <rbd/librbd.h>
24 * When specifying the image filename use:
26 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
28 * poolname must be the name of an existing rados pool.
30 * devicename is the name of the rbd image.
32 * Each option given is used to configure rados, and may be any valid
33 * Ceph option, "id", or "conf".
35 * The "id" option indicates what user we should authenticate as to
36 * the Ceph cluster. If it is excluded we will use the Ceph default
37 * (normally 'admin').
39 * The "conf" option specifies a Ceph configuration file to read. If
40 * it is not specified, we will read from the default Ceph locations
41 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
42 * file, specify conf=/dev/null.
44 * Configuration values containing :, @, or = can be escaped with a
45 * leading "\".
48 /* rbd_aio_discard added in 0.1.2 */
49 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
50 #define LIBRBD_SUPPORTS_DISCARD
51 #else
52 #undef LIBRBD_SUPPORTS_DISCARD
53 #endif
55 #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
57 #define RBD_MAX_CONF_NAME_SIZE 128
58 #define RBD_MAX_CONF_VAL_SIZE 512
59 #define RBD_MAX_CONF_SIZE 1024
60 #define RBD_MAX_POOL_NAME_SIZE 128
61 #define RBD_MAX_SNAP_NAME_SIZE 128
62 #define RBD_MAX_SNAPS 100
64 typedef enum {
65 RBD_AIO_READ,
66 RBD_AIO_WRITE,
67 RBD_AIO_DISCARD,
68 RBD_AIO_FLUSH
69 } RBDAIOCmd;
71 typedef struct RBDAIOCB {
72 BlockAIOCB common;
73 QEMUBH *bh;
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(errp, "error reading conf file %s", value);
293 break;
296 } else if (strcmp(name, "id") == 0) {
297 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
298 } else if (!only_read_conf_file) {
299 ret = rados_conf_set(cluster, name, value);
300 if (ret < 0) {
301 error_setg(errp, "invalid conf option %s", name);
302 ret = -EINVAL;
303 break;
308 g_free(buf);
309 return ret;
312 static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
314 Error *local_err = NULL;
315 int64_t bytes = 0;
316 int64_t objsize;
317 int obj_order = 0;
318 char pool[RBD_MAX_POOL_NAME_SIZE];
319 char name[RBD_MAX_IMAGE_NAME_SIZE];
320 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
321 char conf[RBD_MAX_CONF_SIZE];
322 char clientname_buf[RBD_MAX_CONF_SIZE];
323 char *clientname;
324 const char *secretid;
325 rados_t cluster;
326 rados_ioctx_t io_ctx;
327 int ret;
329 secretid = qemu_opt_get(opts, "password-secret");
331 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
332 snap_buf, sizeof(snap_buf),
333 name, sizeof(name),
334 conf, sizeof(conf), &local_err) < 0) {
335 error_propagate(errp, local_err);
336 return -EINVAL;
339 /* Read out options */
340 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
341 BDRV_SECTOR_SIZE);
342 objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
343 if (objsize) {
344 if ((objsize - 1) & objsize) { /* not a power of 2? */
345 error_setg(errp, "obj size needs to be power of 2");
346 return -EINVAL;
348 if (objsize < 4096) {
349 error_setg(errp, "obj size too small");
350 return -EINVAL;
352 obj_order = ctz32(objsize);
355 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
356 if (rados_create(&cluster, clientname) < 0) {
357 error_setg(errp, "error initializing");
358 return -EIO;
361 if (strstr(conf, "conf=") == NULL) {
362 /* try default location, but ignore failure */
363 rados_conf_read_file(cluster, NULL);
364 } else if (conf[0] != '\0' &&
365 qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
366 rados_shutdown(cluster);
367 error_propagate(errp, local_err);
368 return -EIO;
371 if (conf[0] != '\0' &&
372 qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
373 rados_shutdown(cluster);
374 error_propagate(errp, local_err);
375 return -EIO;
378 if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
379 rados_shutdown(cluster);
380 return -EIO;
383 if (rados_connect(cluster) < 0) {
384 error_setg(errp, "error connecting");
385 rados_shutdown(cluster);
386 return -EIO;
389 if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) {
390 error_setg(errp, "error opening pool %s", pool);
391 rados_shutdown(cluster);
392 return -EIO;
395 ret = rbd_create(io_ctx, name, bytes, &obj_order);
396 rados_ioctx_destroy(io_ctx);
397 rados_shutdown(cluster);
399 return ret;
403 * This aio completion is being called from rbd_finish_bh() and runs in qemu
404 * BH context.
406 static void qemu_rbd_complete_aio(RADOSCB *rcb)
408 RBDAIOCB *acb = rcb->acb;
409 int64_t r;
411 r = rcb->ret;
413 if (acb->cmd != RBD_AIO_READ) {
414 if (r < 0) {
415 acb->ret = r;
416 acb->error = 1;
417 } else if (!acb->error) {
418 acb->ret = rcb->size;
420 } else {
421 if (r < 0) {
422 memset(rcb->buf, 0, rcb->size);
423 acb->ret = r;
424 acb->error = 1;
425 } else if (r < rcb->size) {
426 memset(rcb->buf + r, 0, rcb->size - r);
427 if (!acb->error) {
428 acb->ret = rcb->size;
430 } else if (!acb->error) {
431 acb->ret = r;
435 g_free(rcb);
437 if (acb->cmd == RBD_AIO_READ) {
438 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
440 qemu_vfree(acb->bounce);
441 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
443 qemu_aio_unref(acb);
446 /* TODO Convert to fine grained options */
447 static QemuOptsList runtime_opts = {
448 .name = "rbd",
449 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
450 .desc = {
452 .name = "filename",
453 .type = QEMU_OPT_STRING,
454 .help = "Specification of the rbd image",
457 .name = "password-secret",
458 .type = QEMU_OPT_STRING,
459 .help = "ID of secret providing the password",
461 { /* end of list */ }
465 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
466 Error **errp)
468 BDRVRBDState *s = bs->opaque;
469 char pool[RBD_MAX_POOL_NAME_SIZE];
470 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
471 char conf[RBD_MAX_CONF_SIZE];
472 char clientname_buf[RBD_MAX_CONF_SIZE];
473 char *clientname;
474 const char *secretid;
475 QemuOpts *opts;
476 Error *local_err = NULL;
477 const char *filename;
478 int r;
480 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
481 qemu_opts_absorb_qdict(opts, options, &local_err);
482 if (local_err) {
483 error_propagate(errp, local_err);
484 qemu_opts_del(opts);
485 return -EINVAL;
488 filename = qemu_opt_get(opts, "filename");
489 secretid = qemu_opt_get(opts, "password-secret");
491 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
492 snap_buf, sizeof(snap_buf),
493 s->name, sizeof(s->name),
494 conf, sizeof(conf), errp) < 0) {
495 r = -EINVAL;
496 goto failed_opts;
499 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
500 r = rados_create(&s->cluster, clientname);
501 if (r < 0) {
502 error_setg(errp, "error initializing");
503 goto failed_opts;
506 s->snap = NULL;
507 if (snap_buf[0] != '\0') {
508 s->snap = g_strdup(snap_buf);
511 if (strstr(conf, "conf=") == NULL) {
512 /* try default location, but ignore failure */
513 rados_conf_read_file(s->cluster, NULL);
514 } else if (conf[0] != '\0') {
515 r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
516 if (r < 0) {
517 goto failed_shutdown;
521 if (conf[0] != '\0') {
522 r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
523 if (r < 0) {
524 goto failed_shutdown;
528 if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
529 r = -EIO;
530 goto failed_shutdown;
534 * Fallback to more conservative semantics if setting cache
535 * options fails. Ignore errors from setting rbd_cache because the
536 * only possible error is that the option does not exist, and
537 * librbd defaults to no caching. If write through caching cannot
538 * be set up, fall back to no caching.
540 if (flags & BDRV_O_NOCACHE) {
541 rados_conf_set(s->cluster, "rbd_cache", "false");
542 } else {
543 rados_conf_set(s->cluster, "rbd_cache", "true");
546 r = rados_connect(s->cluster);
547 if (r < 0) {
548 error_setg(errp, "error connecting");
549 goto failed_shutdown;
552 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
553 if (r < 0) {
554 error_setg(errp, "error opening pool %s", pool);
555 goto failed_shutdown;
558 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
559 if (r < 0) {
560 error_setg(errp, "error reading header from %s", s->name);
561 goto failed_open;
564 bs->read_only = (s->snap != NULL);
566 qemu_opts_del(opts);
567 return 0;
569 failed_open:
570 rados_ioctx_destroy(s->io_ctx);
571 failed_shutdown:
572 rados_shutdown(s->cluster);
573 g_free(s->snap);
574 failed_opts:
575 qemu_opts_del(opts);
576 return r;
579 static void qemu_rbd_close(BlockDriverState *bs)
581 BDRVRBDState *s = bs->opaque;
583 rbd_close(s->image);
584 rados_ioctx_destroy(s->io_ctx);
585 g_free(s->snap);
586 rados_shutdown(s->cluster);
589 static const AIOCBInfo rbd_aiocb_info = {
590 .aiocb_size = sizeof(RBDAIOCB),
593 static void rbd_finish_bh(void *opaque)
595 RADOSCB *rcb = opaque;
596 qemu_bh_delete(rcb->acb->bh);
597 qemu_rbd_complete_aio(rcb);
601 * This is the callback function for rbd_aio_read and _write
603 * Note: this function is being called from a non qemu thread so
604 * we need to be careful about what we do here. Generally we only
605 * schedule a BH, and do the rest of the io completion handling
606 * from rbd_finish_bh() which runs in a qemu context.
608 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
610 RBDAIOCB *acb = rcb->acb;
612 rcb->ret = rbd_aio_get_return_value(c);
613 rbd_aio_release(c);
615 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
616 rbd_finish_bh, rcb);
617 qemu_bh_schedule(acb->bh);
620 static int rbd_aio_discard_wrapper(rbd_image_t image,
621 uint64_t off,
622 uint64_t len,
623 rbd_completion_t comp)
625 #ifdef LIBRBD_SUPPORTS_DISCARD
626 return rbd_aio_discard(image, off, len, comp);
627 #else
628 return -ENOTSUP;
629 #endif
632 static int rbd_aio_flush_wrapper(rbd_image_t image,
633 rbd_completion_t comp)
635 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
636 return rbd_aio_flush(image, comp);
637 #else
638 return -ENOTSUP;
639 #endif
642 static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
643 int64_t sector_num,
644 QEMUIOVector *qiov,
645 int nb_sectors,
646 BlockCompletionFunc *cb,
647 void *opaque,
648 RBDAIOCmd cmd)
650 RBDAIOCB *acb;
651 RADOSCB *rcb = NULL;
652 rbd_completion_t c;
653 int64_t off, size;
654 char *buf;
655 int r;
657 BDRVRBDState *s = bs->opaque;
659 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
660 acb->cmd = cmd;
661 acb->qiov = qiov;
662 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
663 acb->bounce = NULL;
664 } else {
665 acb->bounce = qemu_try_blockalign(bs, qiov->size);
666 if (acb->bounce == NULL) {
667 goto failed;
670 acb->ret = 0;
671 acb->error = 0;
672 acb->s = s;
673 acb->bh = NULL;
675 if (cmd == RBD_AIO_WRITE) {
676 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
679 buf = acb->bounce;
681 off = sector_num * BDRV_SECTOR_SIZE;
682 size = nb_sectors * BDRV_SECTOR_SIZE;
684 rcb = g_new(RADOSCB, 1);
685 rcb->acb = acb;
686 rcb->buf = buf;
687 rcb->s = acb->s;
688 rcb->size = size;
689 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
690 if (r < 0) {
691 goto failed;
694 switch (cmd) {
695 case RBD_AIO_WRITE:
696 r = rbd_aio_write(s->image, off, size, buf, c);
697 break;
698 case RBD_AIO_READ:
699 r = rbd_aio_read(s->image, off, size, buf, c);
700 break;
701 case RBD_AIO_DISCARD:
702 r = rbd_aio_discard_wrapper(s->image, off, size, c);
703 break;
704 case RBD_AIO_FLUSH:
705 r = rbd_aio_flush_wrapper(s->image, c);
706 break;
707 default:
708 r = -EINVAL;
711 if (r < 0) {
712 goto failed_completion;
715 return &acb->common;
717 failed_completion:
718 rbd_aio_release(c);
719 failed:
720 g_free(rcb);
721 qemu_vfree(acb->bounce);
722 qemu_aio_unref(acb);
723 return NULL;
726 static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
727 int64_t sector_num,
728 QEMUIOVector *qiov,
729 int nb_sectors,
730 BlockCompletionFunc *cb,
731 void *opaque)
733 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
734 RBD_AIO_READ);
737 static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
738 int64_t sector_num,
739 QEMUIOVector *qiov,
740 int nb_sectors,
741 BlockCompletionFunc *cb,
742 void *opaque)
744 return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque,
745 RBD_AIO_WRITE);
748 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
749 static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
750 BlockCompletionFunc *cb,
751 void *opaque)
753 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
756 #else
758 static int qemu_rbd_co_flush(BlockDriverState *bs)
760 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
761 /* rbd_flush added in 0.1.1 */
762 BDRVRBDState *s = bs->opaque;
763 return rbd_flush(s->image);
764 #else
765 return 0;
766 #endif
768 #endif
770 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
772 BDRVRBDState *s = bs->opaque;
773 rbd_image_info_t info;
774 int r;
776 r = rbd_stat(s->image, &info, sizeof(info));
777 if (r < 0) {
778 return r;
781 bdi->cluster_size = info.obj_size;
782 return 0;
785 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
787 BDRVRBDState *s = bs->opaque;
788 rbd_image_info_t info;
789 int r;
791 r = rbd_stat(s->image, &info, sizeof(info));
792 if (r < 0) {
793 return r;
796 return info.size;
799 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
801 BDRVRBDState *s = bs->opaque;
802 int r;
804 r = rbd_resize(s->image, offset);
805 if (r < 0) {
806 return r;
809 return 0;
812 static int qemu_rbd_snap_create(BlockDriverState *bs,
813 QEMUSnapshotInfo *sn_info)
815 BDRVRBDState *s = bs->opaque;
816 int r;
818 if (sn_info->name[0] == '\0') {
819 return -EINVAL; /* we need a name for rbd snapshots */
823 * rbd snapshots are using the name as the user controlled unique identifier
824 * we can't use the rbd snapid for that purpose, as it can't be set
826 if (sn_info->id_str[0] != '\0' &&
827 strcmp(sn_info->id_str, sn_info->name) != 0) {
828 return -EINVAL;
831 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
832 return -ERANGE;
835 r = rbd_snap_create(s->image, sn_info->name);
836 if (r < 0) {
837 error_report("failed to create snap: %s", strerror(-r));
838 return r;
841 return 0;
844 static int qemu_rbd_snap_remove(BlockDriverState *bs,
845 const char *snapshot_id,
846 const char *snapshot_name,
847 Error **errp)
849 BDRVRBDState *s = bs->opaque;
850 int r;
852 if (!snapshot_name) {
853 error_setg(errp, "rbd need a valid snapshot name");
854 return -EINVAL;
857 /* If snapshot_id is specified, it must be equal to name, see
858 qemu_rbd_snap_list() */
859 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
860 error_setg(errp,
861 "rbd do not support snapshot id, it should be NULL or "
862 "equal to snapshot name");
863 return -EINVAL;
866 r = rbd_snap_remove(s->image, snapshot_name);
867 if (r < 0) {
868 error_setg_errno(errp, -r, "Failed to remove the snapshot");
870 return r;
873 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
874 const char *snapshot_name)
876 BDRVRBDState *s = bs->opaque;
877 int r;
879 r = rbd_snap_rollback(s->image, snapshot_name);
880 return r;
883 static int qemu_rbd_snap_list(BlockDriverState *bs,
884 QEMUSnapshotInfo **psn_tab)
886 BDRVRBDState *s = bs->opaque;
887 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
888 int i, snap_count;
889 rbd_snap_info_t *snaps;
890 int max_snaps = RBD_MAX_SNAPS;
892 do {
893 snaps = g_new(rbd_snap_info_t, max_snaps);
894 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
895 if (snap_count <= 0) {
896 g_free(snaps);
898 } while (snap_count == -ERANGE);
900 if (snap_count <= 0) {
901 goto done;
904 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
906 for (i = 0; i < snap_count; i++) {
907 const char *snap_name = snaps[i].name;
909 sn_info = sn_tab + i;
910 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
911 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
913 sn_info->vm_state_size = snaps[i].size;
914 sn_info->date_sec = 0;
915 sn_info->date_nsec = 0;
916 sn_info->vm_clock_nsec = 0;
918 rbd_snap_list_end(snaps);
919 g_free(snaps);
921 done:
922 *psn_tab = sn_tab;
923 return snap_count;
926 #ifdef LIBRBD_SUPPORTS_DISCARD
927 static BlockAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs,
928 int64_t sector_num,
929 int nb_sectors,
930 BlockCompletionFunc *cb,
931 void *opaque)
933 return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque,
934 RBD_AIO_DISCARD);
936 #endif
938 #ifdef LIBRBD_SUPPORTS_INVALIDATE
939 static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
940 Error **errp)
942 BDRVRBDState *s = bs->opaque;
943 int r = rbd_invalidate_cache(s->image);
944 if (r < 0) {
945 error_setg_errno(errp, -r, "Failed to invalidate the cache");
948 #endif
950 static QemuOptsList qemu_rbd_create_opts = {
951 .name = "rbd-create-opts",
952 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
953 .desc = {
955 .name = BLOCK_OPT_SIZE,
956 .type = QEMU_OPT_SIZE,
957 .help = "Virtual disk size"
960 .name = BLOCK_OPT_CLUSTER_SIZE,
961 .type = QEMU_OPT_SIZE,
962 .help = "RBD object size"
965 .name = "password-secret",
966 .type = QEMU_OPT_STRING,
967 .help = "ID of secret providing the password",
969 { /* end of list */ }
973 static BlockDriver bdrv_rbd = {
974 .format_name = "rbd",
975 .instance_size = sizeof(BDRVRBDState),
976 .bdrv_needs_filename = true,
977 .bdrv_file_open = qemu_rbd_open,
978 .bdrv_close = qemu_rbd_close,
979 .bdrv_create = qemu_rbd_create,
980 .bdrv_has_zero_init = bdrv_has_zero_init_1,
981 .bdrv_get_info = qemu_rbd_getinfo,
982 .create_opts = &qemu_rbd_create_opts,
983 .bdrv_getlength = qemu_rbd_getlength,
984 .bdrv_truncate = qemu_rbd_truncate,
985 .protocol_name = "rbd",
987 .bdrv_aio_readv = qemu_rbd_aio_readv,
988 .bdrv_aio_writev = qemu_rbd_aio_writev,
990 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
991 .bdrv_aio_flush = qemu_rbd_aio_flush,
992 #else
993 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
994 #endif
996 #ifdef LIBRBD_SUPPORTS_DISCARD
997 .bdrv_aio_discard = qemu_rbd_aio_discard,
998 #endif
1000 .bdrv_snapshot_create = qemu_rbd_snap_create,
1001 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1002 .bdrv_snapshot_list = qemu_rbd_snap_list,
1003 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
1004 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1005 .bdrv_invalidate_cache = qemu_rbd_invalidate_cache,
1006 #endif
1009 static void bdrv_rbd_init(void)
1011 bdrv_register(&bdrv_rbd);
1014 block_init(bdrv_rbd_init);