virtio-pci: error out when both legacy and modern modes are disabled
[qemu/kevin.git] / block / rbd.c
blob0106fea45f6515143962b02526ca5b678fd662af
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 QEMUBH *bh;
75 int64_t ret;
76 QEMUIOVector *qiov;
77 char *bounce;
78 RBDAIOCmd cmd;
79 int error;
80 struct BDRVRBDState *s;
81 } RBDAIOCB;
83 typedef struct RADOSCB {
84 RBDAIOCB *acb;
85 struct BDRVRBDState *s;
86 int64_t size;
87 char *buf;
88 int64_t ret;
89 } RADOSCB;
91 typedef struct BDRVRBDState {
92 rados_t cluster;
93 rados_ioctx_t io_ctx;
94 rbd_image_t image;
95 char name[RBD_MAX_IMAGE_NAME_SIZE];
96 char *snap;
97 } BDRVRBDState;
99 static int qemu_rbd_next_tok(char *dst, int dst_len,
100 char *src, char delim,
101 const char *name,
102 char **p, Error **errp)
104 int l;
105 char *end;
107 *p = NULL;
109 if (delim != '\0') {
110 for (end = src; *end; ++end) {
111 if (*end == delim) {
112 break;
114 if (*end == '\\' && end[1] != '\0') {
115 end++;
118 if (*end == delim) {
119 *p = end + 1;
120 *end = '\0';
123 l = strlen(src);
124 if (l >= dst_len) {
125 error_setg(errp, "%s too long", name);
126 return -EINVAL;
127 } else if (l == 0) {
128 error_setg(errp, "%s too short", name);
129 return -EINVAL;
132 pstrcpy(dst, dst_len, src);
134 return 0;
137 static void qemu_rbd_unescape(char *src)
139 char *p;
141 for (p = src; *src; ++src, ++p) {
142 if (*src == '\\' && src[1] != '\0') {
143 src++;
145 *p = *src;
147 *p = '\0';
150 static int qemu_rbd_parsename(const char *filename,
151 char *pool, int pool_len,
152 char *snap, int snap_len,
153 char *name, int name_len,
154 char *conf, int conf_len,
155 Error **errp)
157 const char *start;
158 char *p, *buf;
159 int ret;
161 if (!strstart(filename, "rbd:", &start)) {
162 error_setg(errp, "File name must start with 'rbd:'");
163 return -EINVAL;
166 buf = g_strdup(start);
167 p = buf;
168 *snap = '\0';
169 *conf = '\0';
171 ret = qemu_rbd_next_tok(pool, pool_len, p,
172 '/', "pool name", &p, errp);
173 if (ret < 0 || !p) {
174 ret = -EINVAL;
175 goto done;
177 qemu_rbd_unescape(pool);
179 if (strchr(p, '@')) {
180 ret = qemu_rbd_next_tok(name, name_len, p,
181 '@', "object name", &p, errp);
182 if (ret < 0) {
183 goto done;
185 ret = qemu_rbd_next_tok(snap, snap_len, p,
186 ':', "snap name", &p, errp);
187 qemu_rbd_unescape(snap);
188 } else {
189 ret = qemu_rbd_next_tok(name, name_len, p,
190 ':', "object name", &p, errp);
192 qemu_rbd_unescape(name);
193 if (ret < 0 || !p) {
194 goto done;
197 ret = qemu_rbd_next_tok(conf, conf_len, p,
198 '\0', "configuration", &p, errp);
200 done:
201 g_free(buf);
202 return ret;
205 static char *qemu_rbd_parse_clientname(const char *conf, char *clientname)
207 const char *p = conf;
209 while (*p) {
210 int len;
211 const char *end = strchr(p, ':');
213 if (end) {
214 len = end - p;
215 } else {
216 len = strlen(p);
219 if (strncmp(p, "id=", 3) == 0) {
220 len -= 3;
221 strncpy(clientname, p + 3, len);
222 clientname[len] = '\0';
223 return clientname;
225 if (end == NULL) {
226 break;
228 p = end + 1;
230 return NULL;
234 static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
235 Error **errp)
237 if (secretid == 0) {
238 return 0;
241 gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
242 errp);
243 if (!secret) {
244 return -1;
247 rados_conf_set(cluster, "key", secret);
248 g_free(secret);
250 return 0;
254 static int qemu_rbd_set_conf(rados_t cluster, const char *conf,
255 bool only_read_conf_file,
256 Error **errp)
258 char *p, *buf;
259 char name[RBD_MAX_CONF_NAME_SIZE];
260 char value[RBD_MAX_CONF_VAL_SIZE];
261 int ret = 0;
263 buf = g_strdup(conf);
264 p = buf;
266 while (p) {
267 ret = qemu_rbd_next_tok(name, sizeof(name), p,
268 '=', "conf option name", &p, errp);
269 if (ret < 0) {
270 break;
272 qemu_rbd_unescape(name);
274 if (!p) {
275 error_setg(errp, "conf option %s has no value", name);
276 ret = -EINVAL;
277 break;
280 ret = qemu_rbd_next_tok(value, sizeof(value), p,
281 ':', "conf option value", &p, errp);
282 if (ret < 0) {
283 break;
285 qemu_rbd_unescape(value);
287 if (strcmp(name, "conf") == 0) {
288 /* read the conf file alone, so it doesn't override more
289 specific settings for a particular device */
290 if (only_read_conf_file) {
291 ret = rados_conf_read_file(cluster, value);
292 if (ret < 0) {
293 error_setg_errno(errp, -ret, "error reading conf file %s",
294 value);
295 break;
298 } else if (strcmp(name, "id") == 0) {
299 /* ignore, this is parsed by qemu_rbd_parse_clientname() */
300 } else if (!only_read_conf_file) {
301 ret = rados_conf_set(cluster, name, value);
302 if (ret < 0) {
303 error_setg_errno(errp, -ret, "invalid conf option %s", name);
304 ret = -EINVAL;
305 break;
310 g_free(buf);
311 return ret;
314 static int qemu_rbd_create(const char *filename, QemuOpts *opts, Error **errp)
316 Error *local_err = NULL;
317 int64_t bytes = 0;
318 int64_t objsize;
319 int obj_order = 0;
320 char pool[RBD_MAX_POOL_NAME_SIZE];
321 char name[RBD_MAX_IMAGE_NAME_SIZE];
322 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
323 char conf[RBD_MAX_CONF_SIZE];
324 char clientname_buf[RBD_MAX_CONF_SIZE];
325 char *clientname;
326 const char *secretid;
327 rados_t cluster;
328 rados_ioctx_t io_ctx;
329 int ret;
331 secretid = qemu_opt_get(opts, "password-secret");
333 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
334 snap_buf, sizeof(snap_buf),
335 name, sizeof(name),
336 conf, sizeof(conf), &local_err) < 0) {
337 error_propagate(errp, local_err);
338 return -EINVAL;
341 /* Read out options */
342 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
343 BDRV_SECTOR_SIZE);
344 objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0);
345 if (objsize) {
346 if ((objsize - 1) & objsize) { /* not a power of 2? */
347 error_setg(errp, "obj size needs to be power of 2");
348 return -EINVAL;
350 if (objsize < 4096) {
351 error_setg(errp, "obj size too small");
352 return -EINVAL;
354 obj_order = ctz32(objsize);
357 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
358 ret = rados_create(&cluster, clientname);
359 if (ret < 0) {
360 error_setg_errno(errp, -ret, "error initializing");
361 return ret;
364 if (strstr(conf, "conf=") == NULL) {
365 /* try default location, but ignore failure */
366 rados_conf_read_file(cluster, NULL);
367 } else if (conf[0] != '\0' &&
368 qemu_rbd_set_conf(cluster, conf, true, &local_err) < 0) {
369 rados_shutdown(cluster);
370 error_propagate(errp, local_err);
371 return -EIO;
374 if (conf[0] != '\0' &&
375 qemu_rbd_set_conf(cluster, conf, false, &local_err) < 0) {
376 rados_shutdown(cluster);
377 error_propagate(errp, local_err);
378 return -EIO;
381 if (qemu_rbd_set_auth(cluster, secretid, errp) < 0) {
382 rados_shutdown(cluster);
383 return -EIO;
386 ret = rados_connect(cluster);
387 if (ret < 0) {
388 error_setg_errno(errp, -ret, "error connecting");
389 rados_shutdown(cluster);
390 return ret;
393 ret = rados_ioctx_create(cluster, pool, &io_ctx);
394 if (ret < 0) {
395 error_setg_errno(errp, -ret, "error opening pool %s", pool);
396 rados_shutdown(cluster);
397 return ret;
400 ret = rbd_create(io_ctx, name, bytes, &obj_order);
401 rados_ioctx_destroy(io_ctx);
402 rados_shutdown(cluster);
403 if (ret < 0) {
404 error_setg_errno(errp, -ret, "error rbd create");
405 return ret;
408 return ret;
412 * This aio completion is being called from rbd_finish_bh() and runs in qemu
413 * BH context.
415 static void qemu_rbd_complete_aio(RADOSCB *rcb)
417 RBDAIOCB *acb = rcb->acb;
418 int64_t r;
420 r = rcb->ret;
422 if (acb->cmd != RBD_AIO_READ) {
423 if (r < 0) {
424 acb->ret = r;
425 acb->error = 1;
426 } else if (!acb->error) {
427 acb->ret = rcb->size;
429 } else {
430 if (r < 0) {
431 memset(rcb->buf, 0, rcb->size);
432 acb->ret = r;
433 acb->error = 1;
434 } else if (r < rcb->size) {
435 memset(rcb->buf + r, 0, rcb->size - r);
436 if (!acb->error) {
437 acb->ret = rcb->size;
439 } else if (!acb->error) {
440 acb->ret = r;
444 g_free(rcb);
446 if (acb->cmd == RBD_AIO_READ) {
447 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
449 qemu_vfree(acb->bounce);
450 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
452 qemu_aio_unref(acb);
455 /* TODO Convert to fine grained options */
456 static QemuOptsList runtime_opts = {
457 .name = "rbd",
458 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
459 .desc = {
461 .name = "filename",
462 .type = QEMU_OPT_STRING,
463 .help = "Specification of the rbd image",
466 .name = "password-secret",
467 .type = QEMU_OPT_STRING,
468 .help = "ID of secret providing the password",
470 { /* end of list */ }
474 static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
475 Error **errp)
477 BDRVRBDState *s = bs->opaque;
478 char pool[RBD_MAX_POOL_NAME_SIZE];
479 char snap_buf[RBD_MAX_SNAP_NAME_SIZE];
480 char conf[RBD_MAX_CONF_SIZE];
481 char clientname_buf[RBD_MAX_CONF_SIZE];
482 char *clientname;
483 const char *secretid;
484 QemuOpts *opts;
485 Error *local_err = NULL;
486 const char *filename;
487 int r;
489 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
490 qemu_opts_absorb_qdict(opts, options, &local_err);
491 if (local_err) {
492 error_propagate(errp, local_err);
493 qemu_opts_del(opts);
494 return -EINVAL;
497 filename = qemu_opt_get(opts, "filename");
498 secretid = qemu_opt_get(opts, "password-secret");
500 if (qemu_rbd_parsename(filename, pool, sizeof(pool),
501 snap_buf, sizeof(snap_buf),
502 s->name, sizeof(s->name),
503 conf, sizeof(conf), errp) < 0) {
504 r = -EINVAL;
505 goto failed_opts;
508 clientname = qemu_rbd_parse_clientname(conf, clientname_buf);
509 r = rados_create(&s->cluster, clientname);
510 if (r < 0) {
511 error_setg_errno(errp, -r, "error initializing");
512 goto failed_opts;
515 s->snap = NULL;
516 if (snap_buf[0] != '\0') {
517 s->snap = g_strdup(snap_buf);
520 if (strstr(conf, "conf=") == NULL) {
521 /* try default location, but ignore failure */
522 rados_conf_read_file(s->cluster, NULL);
523 } else if (conf[0] != '\0') {
524 r = qemu_rbd_set_conf(s->cluster, conf, true, errp);
525 if (r < 0) {
526 goto failed_shutdown;
530 if (conf[0] != '\0') {
531 r = qemu_rbd_set_conf(s->cluster, conf, false, errp);
532 if (r < 0) {
533 goto failed_shutdown;
537 if (qemu_rbd_set_auth(s->cluster, secretid, errp) < 0) {
538 r = -EIO;
539 goto failed_shutdown;
543 * Fallback to more conservative semantics if setting cache
544 * options fails. Ignore errors from setting rbd_cache because the
545 * only possible error is that the option does not exist, and
546 * librbd defaults to no caching. If write through caching cannot
547 * be set up, fall back to no caching.
549 if (flags & BDRV_O_NOCACHE) {
550 rados_conf_set(s->cluster, "rbd_cache", "false");
551 } else {
552 rados_conf_set(s->cluster, "rbd_cache", "true");
555 r = rados_connect(s->cluster);
556 if (r < 0) {
557 error_setg_errno(errp, -r, "error connecting");
558 goto failed_shutdown;
561 r = rados_ioctx_create(s->cluster, pool, &s->io_ctx);
562 if (r < 0) {
563 error_setg_errno(errp, -r, "error opening pool %s", pool);
564 goto failed_shutdown;
567 r = rbd_open(s->io_ctx, s->name, &s->image, s->snap);
568 if (r < 0) {
569 error_setg_errno(errp, -r, "error reading header from %s", s->name);
570 goto failed_open;
573 bs->read_only = (s->snap != NULL);
575 qemu_opts_del(opts);
576 return 0;
578 failed_open:
579 rados_ioctx_destroy(s->io_ctx);
580 failed_shutdown:
581 rados_shutdown(s->cluster);
582 g_free(s->snap);
583 failed_opts:
584 qemu_opts_del(opts);
585 return r;
588 static void qemu_rbd_close(BlockDriverState *bs)
590 BDRVRBDState *s = bs->opaque;
592 rbd_close(s->image);
593 rados_ioctx_destroy(s->io_ctx);
594 g_free(s->snap);
595 rados_shutdown(s->cluster);
598 static const AIOCBInfo rbd_aiocb_info = {
599 .aiocb_size = sizeof(RBDAIOCB),
602 static void rbd_finish_bh(void *opaque)
604 RADOSCB *rcb = opaque;
605 qemu_bh_delete(rcb->acb->bh);
606 qemu_rbd_complete_aio(rcb);
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 * schedule a BH, and do the rest of the io completion handling
615 * from rbd_finish_bh() which runs in a qemu context.
617 static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
619 RBDAIOCB *acb = rcb->acb;
621 rcb->ret = rbd_aio_get_return_value(c);
622 rbd_aio_release(c);
624 acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
625 rbd_finish_bh, rcb);
626 qemu_bh_schedule(acb->bh);
629 static int rbd_aio_discard_wrapper(rbd_image_t image,
630 uint64_t off,
631 uint64_t len,
632 rbd_completion_t comp)
634 #ifdef LIBRBD_SUPPORTS_DISCARD
635 return rbd_aio_discard(image, off, len, comp);
636 #else
637 return -ENOTSUP;
638 #endif
641 static int rbd_aio_flush_wrapper(rbd_image_t image,
642 rbd_completion_t comp)
644 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
645 return rbd_aio_flush(image, comp);
646 #else
647 return -ENOTSUP;
648 #endif
651 static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
652 int64_t off,
653 QEMUIOVector *qiov,
654 int64_t size,
655 BlockCompletionFunc *cb,
656 void *opaque,
657 RBDAIOCmd cmd)
659 RBDAIOCB *acb;
660 RADOSCB *rcb = NULL;
661 rbd_completion_t c;
662 char *buf;
663 int r;
665 BDRVRBDState *s = bs->opaque;
667 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
668 acb->cmd = cmd;
669 acb->qiov = qiov;
670 assert(!qiov || qiov->size == size);
671 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
672 acb->bounce = NULL;
673 } else {
674 acb->bounce = qemu_try_blockalign(bs, qiov->size);
675 if (acb->bounce == NULL) {
676 goto failed;
679 acb->ret = 0;
680 acb->error = 0;
681 acb->s = s;
682 acb->bh = NULL;
684 if (cmd == RBD_AIO_WRITE) {
685 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
688 buf = acb->bounce;
690 rcb = g_new(RADOSCB, 1);
691 rcb->acb = acb;
692 rcb->buf = buf;
693 rcb->s = acb->s;
694 rcb->size = size;
695 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
696 if (r < 0) {
697 goto failed;
700 switch (cmd) {
701 case RBD_AIO_WRITE:
702 r = rbd_aio_write(s->image, off, size, buf, c);
703 break;
704 case RBD_AIO_READ:
705 r = rbd_aio_read(s->image, off, size, buf, c);
706 break;
707 case RBD_AIO_DISCARD:
708 r = rbd_aio_discard_wrapper(s->image, off, size, c);
709 break;
710 case RBD_AIO_FLUSH:
711 r = rbd_aio_flush_wrapper(s->image, c);
712 break;
713 default:
714 r = -EINVAL;
717 if (r < 0) {
718 goto failed_completion;
721 return &acb->common;
723 failed_completion:
724 rbd_aio_release(c);
725 failed:
726 g_free(rcb);
727 qemu_vfree(acb->bounce);
728 qemu_aio_unref(acb);
729 return NULL;
732 static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
733 int64_t sector_num,
734 QEMUIOVector *qiov,
735 int nb_sectors,
736 BlockCompletionFunc *cb,
737 void *opaque)
739 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
740 nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
741 RBD_AIO_READ);
744 static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
745 int64_t sector_num,
746 QEMUIOVector *qiov,
747 int nb_sectors,
748 BlockCompletionFunc *cb,
749 void *opaque)
751 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
752 nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
753 RBD_AIO_WRITE);
756 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
757 static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
758 BlockCompletionFunc *cb,
759 void *opaque)
761 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
764 #else
766 static int qemu_rbd_co_flush(BlockDriverState *bs)
768 #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
769 /* rbd_flush added in 0.1.1 */
770 BDRVRBDState *s = bs->opaque;
771 return rbd_flush(s->image);
772 #else
773 return 0;
774 #endif
776 #endif
778 static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
780 BDRVRBDState *s = bs->opaque;
781 rbd_image_info_t info;
782 int r;
784 r = rbd_stat(s->image, &info, sizeof(info));
785 if (r < 0) {
786 return r;
789 bdi->cluster_size = info.obj_size;
790 return 0;
793 static int64_t qemu_rbd_getlength(BlockDriverState *bs)
795 BDRVRBDState *s = bs->opaque;
796 rbd_image_info_t info;
797 int r;
799 r = rbd_stat(s->image, &info, sizeof(info));
800 if (r < 0) {
801 return r;
804 return info.size;
807 static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset)
809 BDRVRBDState *s = bs->opaque;
810 int r;
812 r = rbd_resize(s->image, offset);
813 if (r < 0) {
814 return r;
817 return 0;
820 static int qemu_rbd_snap_create(BlockDriverState *bs,
821 QEMUSnapshotInfo *sn_info)
823 BDRVRBDState *s = bs->opaque;
824 int r;
826 if (sn_info->name[0] == '\0') {
827 return -EINVAL; /* we need a name for rbd snapshots */
831 * rbd snapshots are using the name as the user controlled unique identifier
832 * we can't use the rbd snapid for that purpose, as it can't be set
834 if (sn_info->id_str[0] != '\0' &&
835 strcmp(sn_info->id_str, sn_info->name) != 0) {
836 return -EINVAL;
839 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
840 return -ERANGE;
843 r = rbd_snap_create(s->image, sn_info->name);
844 if (r < 0) {
845 error_report("failed to create snap: %s", strerror(-r));
846 return r;
849 return 0;
852 static int qemu_rbd_snap_remove(BlockDriverState *bs,
853 const char *snapshot_id,
854 const char *snapshot_name,
855 Error **errp)
857 BDRVRBDState *s = bs->opaque;
858 int r;
860 if (!snapshot_name) {
861 error_setg(errp, "rbd need a valid snapshot name");
862 return -EINVAL;
865 /* If snapshot_id is specified, it must be equal to name, see
866 qemu_rbd_snap_list() */
867 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
868 error_setg(errp,
869 "rbd do not support snapshot id, it should be NULL or "
870 "equal to snapshot name");
871 return -EINVAL;
874 r = rbd_snap_remove(s->image, snapshot_name);
875 if (r < 0) {
876 error_setg_errno(errp, -r, "Failed to remove the snapshot");
878 return r;
881 static int qemu_rbd_snap_rollback(BlockDriverState *bs,
882 const char *snapshot_name)
884 BDRVRBDState *s = bs->opaque;
886 return rbd_snap_rollback(s->image, snapshot_name);
889 static int qemu_rbd_snap_list(BlockDriverState *bs,
890 QEMUSnapshotInfo **psn_tab)
892 BDRVRBDState *s = bs->opaque;
893 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
894 int i, snap_count;
895 rbd_snap_info_t *snaps;
896 int max_snaps = RBD_MAX_SNAPS;
898 do {
899 snaps = g_new(rbd_snap_info_t, max_snaps);
900 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
901 if (snap_count <= 0) {
902 g_free(snaps);
904 } while (snap_count == -ERANGE);
906 if (snap_count <= 0) {
907 goto done;
910 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
912 for (i = 0; i < snap_count; i++) {
913 const char *snap_name = snaps[i].name;
915 sn_info = sn_tab + i;
916 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
917 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
919 sn_info->vm_state_size = snaps[i].size;
920 sn_info->date_sec = 0;
921 sn_info->date_nsec = 0;
922 sn_info->vm_clock_nsec = 0;
924 rbd_snap_list_end(snaps);
925 g_free(snaps);
927 done:
928 *psn_tab = sn_tab;
929 return snap_count;
932 #ifdef LIBRBD_SUPPORTS_DISCARD
933 static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
934 int64_t offset,
935 int count,
936 BlockCompletionFunc *cb,
937 void *opaque)
939 return rbd_start_aio(bs, offset, NULL, count, cb, opaque,
940 RBD_AIO_DISCARD);
942 #endif
944 #ifdef LIBRBD_SUPPORTS_INVALIDATE
945 static void qemu_rbd_invalidate_cache(BlockDriverState *bs,
946 Error **errp)
948 BDRVRBDState *s = bs->opaque;
949 int r = rbd_invalidate_cache(s->image);
950 if (r < 0) {
951 error_setg_errno(errp, -r, "Failed to invalidate the cache");
954 #endif
956 static QemuOptsList qemu_rbd_create_opts = {
957 .name = "rbd-create-opts",
958 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
959 .desc = {
961 .name = BLOCK_OPT_SIZE,
962 .type = QEMU_OPT_SIZE,
963 .help = "Virtual disk size"
966 .name = BLOCK_OPT_CLUSTER_SIZE,
967 .type = QEMU_OPT_SIZE,
968 .help = "RBD object size"
971 .name = "password-secret",
972 .type = QEMU_OPT_STRING,
973 .help = "ID of secret providing the password",
975 { /* end of list */ }
979 static BlockDriver bdrv_rbd = {
980 .format_name = "rbd",
981 .instance_size = sizeof(BDRVRBDState),
982 .bdrv_needs_filename = true,
983 .bdrv_file_open = qemu_rbd_open,
984 .bdrv_close = qemu_rbd_close,
985 .bdrv_create = qemu_rbd_create,
986 .bdrv_has_zero_init = bdrv_has_zero_init_1,
987 .bdrv_get_info = qemu_rbd_getinfo,
988 .create_opts = &qemu_rbd_create_opts,
989 .bdrv_getlength = qemu_rbd_getlength,
990 .bdrv_truncate = qemu_rbd_truncate,
991 .protocol_name = "rbd",
993 .bdrv_aio_readv = qemu_rbd_aio_readv,
994 .bdrv_aio_writev = qemu_rbd_aio_writev,
996 #ifdef LIBRBD_SUPPORTS_AIO_FLUSH
997 .bdrv_aio_flush = qemu_rbd_aio_flush,
998 #else
999 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
1000 #endif
1002 #ifdef LIBRBD_SUPPORTS_DISCARD
1003 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
1004 #endif
1006 .bdrv_snapshot_create = qemu_rbd_snap_create,
1007 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
1008 .bdrv_snapshot_list = qemu_rbd_snap_list,
1009 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
1010 #ifdef LIBRBD_SUPPORTS_INVALIDATE
1011 .bdrv_invalidate_cache = qemu_rbd_invalidate_cache,
1012 #endif
1015 static void bdrv_rbd_init(void)
1017 bdrv_register(&bdrv_rbd);
1020 block_init(bdrv_rbd_init);