]> git.proxmox.com Git - mirror_qemu.git/blame - block/rbd.c
rbd: Assign s->snap/image_name in qemu_rbd_open()
[mirror_qemu.git] / block / rbd.c
CommitLineData
f27aaf4b
CB
1/*
2 * QEMU Block driver for RADOS (Ceph)
3 *
ad32e9c0
JD
4 * Copyright (C) 2010-2011 Christian Brunner <chb@muc.de>,
5 * Josh Durgin <josh.durgin@dreamhost.com>
f27aaf4b
CB
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2. See
8 * the COPYING file in the top-level directory.
9 *
6b620ca3
PB
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.
f27aaf4b
CB
12 */
13
80c71a24 14#include "qemu/osdep.h"
ad32e9c0 15
2836284d 16#include <rbd/librbd.h>
da34e65c 17#include "qapi/error.h"
1de7afc9 18#include "qemu/error-report.h"
922a01a0 19#include "qemu/option.h"
737e150e 20#include "block/block_int.h"
60390a21 21#include "crypto/secret.h"
f348b6d1 22#include "qemu/cutils.h"
c7cacb3e 23#include "qapi/qmp/qstring.h"
452fcdbc 24#include "qapi/qmp/qdict.h"
e98c6961 25#include "qapi/qmp/qjson.h"
47e6b297 26#include "qapi/qmp/qlist.h"
4bfb2741
KW
27#include "qapi/qobject-input-visitor.h"
28#include "qapi/qapi-visit-block-core.h"
f27aaf4b 29
f27aaf4b
CB
30/*
31 * When specifying the image filename use:
32 *
fab5cf59 33 * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]]
f27aaf4b 34 *
9e1fbcde 35 * poolname must be the name of an existing rados pool.
f27aaf4b 36 *
9e1fbcde 37 * devicename is the name of the rbd image.
f27aaf4b 38 *
9e1fbcde
SW
39 * Each option given is used to configure rados, and may be any valid
40 * Ceph option, "id", or "conf".
fab5cf59 41 *
9e1fbcde
SW
42 * The "id" option indicates what user we should authenticate as to
43 * the Ceph cluster. If it is excluded we will use the Ceph default
44 * (normally 'admin').
f27aaf4b 45 *
9e1fbcde
SW
46 * The "conf" option specifies a Ceph configuration file to read. If
47 * it is not specified, we will read from the default Ceph locations
48 * (e.g., /etc/ceph/ceph.conf). To avoid reading _any_ configuration
49 * file, specify conf=/dev/null.
f27aaf4b 50 *
9e1fbcde
SW
51 * Configuration values containing :, @, or = can be escaped with a
52 * leading "\".
f27aaf4b
CB
53 */
54
787f3133
JD
55/* rbd_aio_discard added in 0.1.2 */
56#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 2)
57#define LIBRBD_SUPPORTS_DISCARD
58#else
59#undef LIBRBD_SUPPORTS_DISCARD
60#endif
61
f27aaf4b
CB
62#define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER)
63
ad32e9c0
JD
64#define RBD_MAX_SNAPS 100
65
1d393bde 66/* The LIBRBD_SUPPORTS_IOVEC is defined in librbd.h */
67#ifdef LIBRBD_SUPPORTS_IOVEC
68#define LIBRBD_USE_IOVEC 1
69#else
70#define LIBRBD_USE_IOVEC 0
71#endif
72
787f3133
JD
73typedef enum {
74 RBD_AIO_READ,
75 RBD_AIO_WRITE,
dc7588c1
JD
76 RBD_AIO_DISCARD,
77 RBD_AIO_FLUSH
787f3133
JD
78} RBDAIOCmd;
79
f27aaf4b 80typedef struct RBDAIOCB {
7c84b1b8 81 BlockAIOCB common;
08448d51 82 int64_t ret;
f27aaf4b
CB
83 QEMUIOVector *qiov;
84 char *bounce;
787f3133 85 RBDAIOCmd cmd;
f27aaf4b
CB
86 int error;
87 struct BDRVRBDState *s;
f27aaf4b
CB
88} RBDAIOCB;
89
90typedef struct RADOSCB {
f27aaf4b
CB
91 RBDAIOCB *acb;
92 struct BDRVRBDState *s;
ad32e9c0 93 int64_t size;
f27aaf4b 94 char *buf;
08448d51 95 int64_t ret;
f27aaf4b
CB
96} RADOSCB;
97
f27aaf4b 98typedef struct BDRVRBDState {
ad32e9c0
JD
99 rados_t cluster;
100 rados_ioctx_t io_ctx;
101 rbd_image_t image;
80b61a27 102 char *image_name;
ad32e9c0 103 char *snap;
f27aaf4b
CB
104} BDRVRBDState;
105
730b00bb 106static char *qemu_rbd_next_tok(char *src, char delim, char **p)
f27aaf4b 107{
f27aaf4b
CB
108 char *end;
109
110 *p = NULL;
111
8efb339d 112 for (end = src; *end; ++end) {
16a06b24 113 if (*end == delim) {
8efb339d
MA
114 break;
115 }
116 if (*end == '\\' && end[1] != '\0') {
117 end++;
f27aaf4b
CB
118 }
119 }
8efb339d
MA
120 if (*end == delim) {
121 *p = end + 1;
122 *end = '\0';
123 }
7830f909 124 return src;
f27aaf4b
CB
125}
126
16a06b24
SW
127static void qemu_rbd_unescape(char *src)
128{
129 char *p;
130
131 for (p = src; *src; ++src, ++p) {
132 if (*src == '\\' && src[1] != '\0') {
133 src++;
134 }
135 *p = *src;
136 }
137 *p = '\0';
138}
139
c7cacb3e
JC
140static void qemu_rbd_parse_filename(const char *filename, QDict *options,
141 Error **errp)
f27aaf4b
CB
142{
143 const char *start;
e98c6961
EB
144 char *p, *buf;
145 QList *keypairs = NULL;
7830f909 146 char *found_str;
f27aaf4b
CB
147
148 if (!strstart(filename, "rbd:", &start)) {
d61563b2 149 error_setg(errp, "File name must start with 'rbd:'");
c7cacb3e 150 return;
f27aaf4b
CB
151 }
152
7267c094 153 buf = g_strdup(start);
f27aaf4b
CB
154 p = buf;
155
730b00bb 156 found_str = qemu_rbd_next_tok(p, '/', &p);
7830f909 157 if (!p) {
7830f909 158 error_setg(errp, "Pool name is required");
f27aaf4b
CB
159 goto done;
160 }
7830f909 161 qemu_rbd_unescape(found_str);
46f5ac20 162 qdict_put_str(options, "pool", found_str);
fab5cf59
JD
163
164 if (strchr(p, '@')) {
730b00bb 165 found_str = qemu_rbd_next_tok(p, '@', &p);
7830f909 166 qemu_rbd_unescape(found_str);
46f5ac20 167 qdict_put_str(options, "image", found_str);
7830f909 168
730b00bb 169 found_str = qemu_rbd_next_tok(p, ':', &p);
7830f909 170 qemu_rbd_unescape(found_str);
46f5ac20 171 qdict_put_str(options, "snapshot", found_str);
fab5cf59 172 } else {
730b00bb 173 found_str = qemu_rbd_next_tok(p, ':', &p);
7830f909 174 qemu_rbd_unescape(found_str);
46f5ac20 175 qdict_put_str(options, "image", found_str);
f27aaf4b 176 }
7830f909 177 if (!p) {
f27aaf4b
CB
178 goto done;
179 }
180
c7cacb3e
JC
181 /* The following are essentially all key/value pairs, and we treat
182 * 'id' and 'conf' a bit special. Key/value pairs may be in any order. */
183 while (p) {
184 char *name, *value;
730b00bb 185 name = qemu_rbd_next_tok(p, '=', &p);
c7cacb3e
JC
186 if (!p) {
187 error_setg(errp, "conf option %s has no value", name);
188 break;
7c7e9df0 189 }
c7cacb3e
JC
190
191 qemu_rbd_unescape(name);
192
730b00bb 193 value = qemu_rbd_next_tok(p, ':', &p);
c7cacb3e
JC
194 qemu_rbd_unescape(value);
195
196 if (!strcmp(name, "conf")) {
46f5ac20 197 qdict_put_str(options, "conf", value);
c7cacb3e 198 } else if (!strcmp(name, "id")) {
46f5ac20 199 qdict_put_str(options, "user", value);
c7cacb3e 200 } else {
e98c6961
EB
201 /*
202 * We pass these internally to qemu_rbd_set_keypairs(), so
203 * we can get away with the simpler list of [ "key1",
204 * "value1", "key2", "value2" ] rather than a raw dict
205 * { "key1": "value1", "key2": "value2" } where we can't
206 * guarantee order, or even a more correct but complex
207 * [ { "key1": "value1" }, { "key2": "value2" } ]
208 */
209 if (!keypairs) {
210 keypairs = qlist_new();
c7cacb3e 211 }
46f5ac20
EB
212 qlist_append_str(keypairs, name);
213 qlist_append_str(keypairs, value);
c7cacb3e 214 }
7c7e9df0 215 }
c7cacb3e 216
e98c6961
EB
217 if (keypairs) {
218 qdict_put(options, "=keyvalue-pairs",
219 qobject_to_json(QOBJECT(keypairs)));
c7cacb3e
JC
220 }
221
c7cacb3e 222done:
c7cacb3e 223 g_free(buf);
e98c6961 224 QDECREF(keypairs);
c7cacb3e 225 return;
7c7e9df0
SW
226}
227
60390a21
DB
228
229static int qemu_rbd_set_auth(rados_t cluster, const char *secretid,
230 Error **errp)
231{
232 if (secretid == 0) {
233 return 0;
234 }
235
236 gchar *secret = qcrypto_secret_lookup_as_base64(secretid,
237 errp);
238 if (!secret) {
239 return -1;
240 }
241
242 rados_conf_set(cluster, "key", secret);
243 g_free(secret);
244
245 return 0;
246}
247
e98c6961 248static int qemu_rbd_set_keypairs(rados_t cluster, const char *keypairs_json,
c7cacb3e 249 Error **errp)
fab5cf59 250{
e98c6961
EB
251 QList *keypairs;
252 QString *name;
253 QString *value;
254 const char *key;
255 size_t remaining;
fab5cf59
JD
256 int ret = 0;
257
e98c6961
EB
258 if (!keypairs_json) {
259 return ret;
260 }
261 keypairs = qobject_to_qlist(qobject_from_json(keypairs_json,
262 &error_abort));
263 remaining = qlist_size(keypairs) / 2;
264 assert(remaining);
265
266 while (remaining--) {
267 name = qobject_to_qstring(qlist_pop(keypairs));
268 value = qobject_to_qstring(qlist_pop(keypairs));
269 assert(name && value);
270 key = qstring_get_str(name);
271
272 ret = rados_conf_set(cluster, key, qstring_get_str(value));
e98c6961 273 QDECREF(value);
c7cacb3e 274 if (ret < 0) {
e98c6961 275 error_setg_errno(errp, -ret, "invalid conf option %s", key);
71c87815 276 QDECREF(name);
c7cacb3e
JC
277 ret = -EINVAL;
278 break;
fab5cf59 279 }
71c87815 280 QDECREF(name);
fab5cf59
JD
281 }
282
e98c6961 283 QDECREF(keypairs);
fab5cf59
JD
284 return ret;
285}
286
1d393bde 287static void qemu_rbd_memset(RADOSCB *rcb, int64_t offs)
288{
289 if (LIBRBD_USE_IOVEC) {
290 RBDAIOCB *acb = rcb->acb;
291 iov_memset(acb->qiov->iov, acb->qiov->niov, offs, 0,
292 acb->qiov->size - offs);
293 } else {
294 memset(rcb->buf + offs, 0, rcb->size - offs);
295 }
296}
297
0f9d252d
JC
298static QemuOptsList runtime_opts = {
299 .name = "rbd",
300 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
301 .desc = {
302 {
cbf036b4 303 .name = "pool",
0f9d252d 304 .type = QEMU_OPT_STRING,
cbf036b4 305 .help = "Rados pool name",
0f9d252d
JC
306 },
307 {
cbf036b4 308 .name = "image",
0f9d252d 309 .type = QEMU_OPT_STRING,
cbf036b4 310 .help = "Image name in the pool",
0f9d252d
JC
311 },
312 {
313 .name = "conf",
314 .type = QEMU_OPT_STRING,
315 .help = "Rados config file location",
316 },
0f9d252d
JC
317 {
318 .name = "snapshot",
319 .type = QEMU_OPT_STRING,
320 .help = "Ceph snapshot name",
321 },
322 {
323 /* maps to 'id' in rados_create() */
324 .name = "user",
325 .type = QEMU_OPT_STRING,
326 .help = "Rados id name",
327 },
cbf036b4 328 /*
2836284d 329 * server.* extracted manually, see qemu_rbd_mon_host()
cbf036b4 330 */
0f9d252d
JC
331 { /* end of list */ }
332 },
333};
334
1bebea37
KW
335/* FIXME Deprecate and remove keypairs or make it available in QMP.
336 * password_secret should eventually be configurable in opts->location. Support
337 * for it in .bdrv_open will make it work here as well. */
338static int qemu_rbd_do_create(BlockdevCreateOptions *options,
339 const char *keypairs, const char *password_secret,
340 Error **errp)
f27aaf4b 341{
1bebea37 342 BlockdevCreateOptionsRbd *opts = &options->u.rbd;
ad32e9c0
JD
343 rados_t cluster;
344 rados_ioctx_t io_ctx;
1bebea37
KW
345 int obj_order = 0;
346 int ret;
347
348 assert(options->driver == BLOCKDEV_DRIVER_RBD);
349 if (opts->location->has_snapshot) {
350 error_setg(errp, "Can't use snapshot name for image creation");
351 return -EINVAL;
352 }
f27aaf4b 353
1bebea37
KW
354 /* TODO Remove the limitation */
355 if (opts->location->has_server) {
356 error_setg(errp, "Can't specify server for image creation");
357 return -EINVAL;
358 }
60390a21 359
1bebea37
KW
360 if (opts->has_cluster_size) {
361 int64_t objsize = opts->cluster_size;
bd0cf596
CL
362 if ((objsize - 1) & objsize) { /* not a power of 2? */
363 error_setg(errp, "obj size needs to be power of 2");
1bebea37 364 return -EINVAL;
bd0cf596
CL
365 }
366 if (objsize < 4096) {
367 error_setg(errp, "obj size too small");
1bebea37 368 return -EINVAL;
f27aaf4b 369 }
786a4ea8 370 obj_order = ctz32(objsize);
f27aaf4b
CB
371 }
372
1bebea37 373 ret = rados_create(&cluster, opts->location->user);
87cd3d20
VU
374 if (ret < 0) {
375 error_setg_errno(errp, -ret, "error initializing");
1bebea37 376 return ret;
f27aaf4b
CB
377 }
378
c7cacb3e 379 /* try default location when conf=NULL, but ignore failure */
1bebea37
KW
380 ret = rados_conf_read_file(cluster, opts->location->conf);
381 if (opts->location->conf && ret < 0) {
382 error_setg_errno(errp, -ret, "error reading conf file %s",
383 opts->location->conf);
e38f643a
XL
384 ret = -EIO;
385 goto shutdown;
fab5cf59
JD
386 }
387
c7cacb3e
JC
388 ret = qemu_rbd_set_keypairs(cluster, keypairs, errp);
389 if (ret < 0) {
e38f643a
XL
390 ret = -EIO;
391 goto shutdown;
f27aaf4b
CB
392 }
393
1bebea37 394 if (qemu_rbd_set_auth(cluster, password_secret, errp) < 0) {
e38f643a
XL
395 ret = -EIO;
396 goto shutdown;
60390a21
DB
397 }
398
87cd3d20
VU
399 ret = rados_connect(cluster);
400 if (ret < 0) {
401 error_setg_errno(errp, -ret, "error connecting");
e38f643a 402 goto shutdown;
f27aaf4b 403 }
f27aaf4b 404
1bebea37 405 ret = rados_ioctx_create(cluster, opts->location->pool, &io_ctx);
87cd3d20 406 if (ret < 0) {
1bebea37
KW
407 error_setg_errno(errp, -ret, "error opening pool %s",
408 opts->location->pool);
e38f643a 409 goto shutdown;
f27aaf4b
CB
410 }
411
1bebea37 412 ret = rbd_create(io_ctx, opts->location->image, opts->size, &obj_order);
87cd3d20
VU
413 if (ret < 0) {
414 error_setg_errno(errp, -ret, "error rbd create");
87cd3d20 415 }
f27aaf4b 416
e38f643a
XL
417 rados_ioctx_destroy(io_ctx);
418
1bebea37 419 ret = 0;
e38f643a
XL
420shutdown:
421 rados_shutdown(cluster);
1bebea37
KW
422 return ret;
423}
424
425static int qemu_rbd_co_create(BlockdevCreateOptions *options, Error **errp)
426{
427 return qemu_rbd_do_create(options, NULL, NULL, errp);
428}
429
430static int coroutine_fn qemu_rbd_co_create_opts(const char *filename,
431 QemuOpts *opts,
432 Error **errp)
433{
434 BlockdevCreateOptions *create_options;
435 BlockdevCreateOptionsRbd *rbd_opts;
436 BlockdevOptionsRbd *loc;
437 Error *local_err = NULL;
438 const char *keypairs, *password_secret;
439 QDict *options = NULL;
440 int ret = 0;
441
442 create_options = g_new0(BlockdevCreateOptions, 1);
443 create_options->driver = BLOCKDEV_DRIVER_RBD;
444 rbd_opts = &create_options->u.rbd;
445
446 rbd_opts->location = g_new0(BlockdevOptionsRbd, 1);
447
448 password_secret = qemu_opt_get(opts, "password-secret");
449
450 /* Read out options */
451 rbd_opts->size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
452 BDRV_SECTOR_SIZE);
453 rbd_opts->cluster_size = qemu_opt_get_size_del(opts,
454 BLOCK_OPT_CLUSTER_SIZE, 0);
455 rbd_opts->has_cluster_size = (rbd_opts->cluster_size != 0);
456
457 options = qdict_new();
458 qemu_rbd_parse_filename(filename, options, &local_err);
459 if (local_err) {
460 ret = -EINVAL;
461 error_propagate(errp, local_err);
462 goto exit;
463 }
464
465 /*
466 * Caution: while qdict_get_try_str() is fine, getting non-string
467 * types would require more care. When @options come from -blockdev
468 * or blockdev_add, its members are typed according to the QAPI
469 * schema, but when they come from -drive, they're all QString.
470 */
471 loc = rbd_opts->location;
472 loc->pool = g_strdup(qdict_get_try_str(options, "pool"));
473 loc->conf = g_strdup(qdict_get_try_str(options, "conf"));
474 loc->has_conf = !!loc->conf;
475 loc->user = g_strdup(qdict_get_try_str(options, "user"));
476 loc->has_user = !!loc->user;
477 loc->image = g_strdup(qdict_get_try_str(options, "image"));
478 keypairs = qdict_get_try_str(options, "=keyvalue-pairs");
479
480 ret = qemu_rbd_do_create(create_options, keypairs, password_secret, errp);
481 if (ret < 0) {
482 goto exit;
483 }
c7cacb3e
JC
484
485exit:
486 QDECREF(options);
1bebea37 487 qapi_free_BlockdevCreateOptions(create_options);
f27aaf4b
CB
488 return ret;
489}
490
491/*
e04fb07f
SH
492 * This aio completion is being called from rbd_finish_bh() and runs in qemu
493 * BH context.
f27aaf4b 494 */
ad32e9c0 495static void qemu_rbd_complete_aio(RADOSCB *rcb)
f27aaf4b
CB
496{
497 RBDAIOCB *acb = rcb->acb;
498 int64_t r;
499
f27aaf4b
CB
500 r = rcb->ret;
501
dc7588c1 502 if (acb->cmd != RBD_AIO_READ) {
f27aaf4b
CB
503 if (r < 0) {
504 acb->ret = r;
505 acb->error = 1;
506 } else if (!acb->error) {
ad32e9c0 507 acb->ret = rcb->size;
f27aaf4b
CB
508 }
509 } else {
ad32e9c0 510 if (r < 0) {
1d393bde 511 qemu_rbd_memset(rcb, 0);
f27aaf4b
CB
512 acb->ret = r;
513 acb->error = 1;
ad32e9c0 514 } else if (r < rcb->size) {
1d393bde 515 qemu_rbd_memset(rcb, r);
f27aaf4b 516 if (!acb->error) {
ad32e9c0 517 acb->ret = rcb->size;
f27aaf4b
CB
518 }
519 } else if (!acb->error) {
ad32e9c0 520 acb->ret = r;
f27aaf4b
CB
521 }
522 }
f27aaf4b 523
e04fb07f 524 g_free(rcb);
f27aaf4b 525
1d393bde 526 if (!LIBRBD_USE_IOVEC) {
527 if (acb->cmd == RBD_AIO_READ) {
528 qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
529 }
530 qemu_vfree(acb->bounce);
e04fb07f 531 }
1d393bde 532
e04fb07f 533 acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret));
f27aaf4b 534
8007429a 535 qemu_aio_unref(acb);
f27aaf4b
CB
536}
537
4bfb2741 538static char *qemu_rbd_mon_host(BlockdevOptionsRbd *opts, Error **errp)
0a55679b 539{
4bfb2741 540 const char **vals;
2836284d
MA
541 const char *host, *port;
542 char *rados_str;
4bfb2741
KW
543 InetSocketAddressBaseList *p;
544 int i, cnt;
545
546 if (!opts->has_server) {
547 return NULL;
548 }
549
550 for (cnt = 0, p = opts->server; p; p = p->next) {
551 cnt++;
552 }
553
554 vals = g_new(const char *, cnt + 1);
555
556 for (i = 0, p = opts->server; p; p = p->next, i++) {
557 host = p->value->host;
558 port = p->value->port;
0a55679b 559
2836284d 560 if (strchr(host, ':')) {
4bfb2741 561 vals[i] = g_strdup_printf("[%s]:%s", host, port);
0a55679b 562 } else {
4bfb2741 563 vals[i] = g_strdup_printf("%s:%s", host, port);
0a55679b 564 }
0a55679b 565 }
2836284d 566 vals[i] = NULL;
0a55679b 567
2836284d 568 rados_str = i ? g_strjoinv(";", (char **)vals) : NULL;
2836284d 569 g_strfreev((char **)vals);
0a55679b
JC
570 return rados_str;
571}
572
3d9136f9 573static int qemu_rbd_connect(rados_t *cluster, rados_ioctx_t *io_ctx,
4bfb2741 574 BlockdevOptionsRbd *opts, bool cache,
4ff45049
KW
575 const char *keypairs, const char *secretid,
576 Error **errp)
f27aaf4b 577{
0a55679b 578 char *mon_host = NULL;
3d9136f9 579 Error *local_err = NULL;
f27aaf4b
CB
580 int r;
581
4bfb2741 582 mon_host = qemu_rbd_mon_host(opts, &local_err);
0a55679b
JC
583 if (local_err) {
584 error_propagate(errp, local_err);
585 r = -EINVAL;
586 goto failed_opts;
587 }
588
4bfb2741 589 r = rados_create(cluster, opts->user);
ad32e9c0 590 if (r < 0) {
87cd3d20 591 error_setg_errno(errp, -r, "error initializing");
c3ca988d 592 goto failed_opts;
f27aaf4b
CB
593 }
594
c7cacb3e 595 /* try default location when conf=NULL, but ignore failure */
4bfb2741
KW
596 r = rados_conf_read_file(*cluster, opts->conf);
597 if (opts->has_conf && r < 0) {
598 error_setg_errno(errp, -r, "error reading conf file %s", opts->conf);
c7cacb3e 599 goto failed_shutdown;
99a3c89d
JD
600 }
601
3d9136f9 602 r = qemu_rbd_set_keypairs(*cluster, keypairs, errp);
c7cacb3e
JC
603 if (r < 0) {
604 goto failed_shutdown;
99a3c89d
JD
605 }
606
0a55679b 607 if (mon_host) {
3d9136f9 608 r = rados_conf_set(*cluster, "mon_host", mon_host);
0a55679b
JC
609 if (r < 0) {
610 goto failed_shutdown;
611 }
612 }
613
3d9136f9 614 if (qemu_rbd_set_auth(*cluster, secretid, errp) < 0) {
60390a21
DB
615 r = -EIO;
616 goto failed_shutdown;
617 }
618
b11f38fc
JD
619 /*
620 * Fallback to more conservative semantics if setting cache
621 * options fails. Ignore errors from setting rbd_cache because the
622 * only possible error is that the option does not exist, and
623 * librbd defaults to no caching. If write through caching cannot
624 * be set up, fall back to no caching.
625 */
3d9136f9
KW
626 if (cache) {
627 rados_conf_set(*cluster, "rbd_cache", "true");
b11f38fc 628 } else {
3d9136f9 629 rados_conf_set(*cluster, "rbd_cache", "false");
b11f38fc
JD
630 }
631
3d9136f9 632 r = rados_connect(*cluster);
ad32e9c0 633 if (r < 0) {
87cd3d20 634 error_setg_errno(errp, -r, "error connecting");
eb93d5d9 635 goto failed_shutdown;
f27aaf4b
CB
636 }
637
4bfb2741 638 r = rados_ioctx_create(*cluster, opts->pool, io_ctx);
ad32e9c0 639 if (r < 0) {
4bfb2741 640 error_setg_errno(errp, -r, "error opening pool %s", opts->pool);
eb93d5d9 641 goto failed_shutdown;
f27aaf4b
CB
642 }
643
3d9136f9
KW
644 return 0;
645
646failed_shutdown:
647 rados_shutdown(*cluster);
3d9136f9 648failed_opts:
3d9136f9
KW
649 g_free(mon_host);
650 return r;
651}
652
653static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags,
654 Error **errp)
655{
656 BDRVRBDState *s = bs->opaque;
4bfb2741
KW
657 BlockdevOptionsRbd *opts = NULL;
658 Visitor *v;
659 QObject *crumpled = NULL;
3d9136f9
KW
660 Error *local_err = NULL;
661 const char *filename;
4ff45049 662 char *keypairs, *secretid;
3d9136f9
KW
663 int r;
664
665 /* If we are given a filename, parse the filename, with precedence given to
666 * filename encoded options */
667 filename = qdict_get_try_str(options, "filename");
668 if (filename) {
669 warn_report("'filename' option specified. "
670 "This is an unsupported option, and may be deprecated "
671 "in the future");
672 qemu_rbd_parse_filename(filename, options, &local_err);
4ff45049 673 qdict_del(options, "filename");
3d9136f9
KW
674 if (local_err) {
675 error_propagate(errp, local_err);
676 return -EINVAL;
677 }
678 }
679
4ff45049
KW
680 keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
681 if (keypairs) {
682 qdict_del(options, "=keyvalue-pairs");
683 }
684
685 secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
686 if (secretid) {
687 qdict_del(options, "password-secret");
688 }
689
4bfb2741
KW
690 /* Convert the remaining options into a QAPI object */
691 crumpled = qdict_crumple(options, errp);
692 if (crumpled == NULL) {
693 r = -EINVAL;
694 goto out;
695 }
696
697 v = qobject_input_visitor_new_keyval(crumpled);
698 visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err);
699 visit_free(v);
700 qobject_decref(crumpled);
701
702 if (local_err) {
703 error_propagate(errp, local_err);
704 r = -EINVAL;
705 goto out;
706 }
707
d41a5588
KW
708 r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
709 !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
3d9136f9 710 if (r < 0) {
4ff45049 711 goto out;
3d9136f9
KW
712 }
713
d41a5588
KW
714 s->snap = g_strdup(opts->snapshot);
715 s->image_name = g_strdup(opts->image);
716
e2b8247a 717 /* rbd_open is always r/w */
80b61a27 718 r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
f27aaf4b 719 if (r < 0) {
80b61a27
JC
720 error_setg_errno(errp, -r, "error reading header from %s",
721 s->image_name);
eb93d5d9 722 goto failed_open;
f27aaf4b
CB
723 }
724
e2b8247a
JC
725 /* If we are using an rbd snapshot, we must be r/o, otherwise
726 * leave as-is */
727 if (s->snap != NULL) {
398e6ad0
KW
728 if (!bdrv_is_read_only(bs)) {
729 error_report("Opening rbd snapshots without an explicit "
730 "read-only=on option is deprecated. Future versions "
731 "will refuse to open the image instead of "
732 "automatically marking the image read-only.");
733 r = bdrv_set_read_only(bs, true, &local_err);
734 if (r < 0) {
735 error_propagate(errp, local_err);
736 goto failed_open;
737 }
e2b8247a
JC
738 }
739 }
f27aaf4b 740
4ff45049
KW
741 r = 0;
742 goto out;
f27aaf4b 743
eb93d5d9 744failed_open:
ad32e9c0 745 rados_ioctx_destroy(s->io_ctx);
eb93d5d9 746 g_free(s->snap);
80b61a27 747 g_free(s->image_name);
3d9136f9 748 rados_shutdown(s->cluster);
4ff45049 749out:
4bfb2741 750 qapi_free_BlockdevOptionsRbd(opts);
4ff45049
KW
751 g_free(keypairs);
752 g_free(secretid);
f27aaf4b
CB
753 return r;
754}
755
56e7cf8d
JC
756
757/* Since RBD is currently always opened R/W via the API,
758 * we just need to check if we are using a snapshot or not, in
759 * order to determine if we will allow it to be R/W */
760static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
761 BlockReopenQueue *queue, Error **errp)
762{
763 BDRVRBDState *s = state->bs->opaque;
764 int ret = 0;
765
766 if (s->snap && state->flags & BDRV_O_RDWR) {
767 error_setg(errp,
768 "Cannot change node '%s' to r/w when using RBD snapshot",
769 bdrv_get_device_or_node_name(state->bs));
770 ret = -EINVAL;
771 }
772
773 return ret;
774}
775
ad32e9c0 776static void qemu_rbd_close(BlockDriverState *bs)
f27aaf4b
CB
777{
778 BDRVRBDState *s = bs->opaque;
779
ad32e9c0
JD
780 rbd_close(s->image);
781 rados_ioctx_destroy(s->io_ctx);
7267c094 782 g_free(s->snap);
80b61a27 783 g_free(s->image_name);
ad32e9c0 784 rados_shutdown(s->cluster);
f27aaf4b
CB
785}
786
d7331bed 787static const AIOCBInfo rbd_aiocb_info = {
f27aaf4b 788 .aiocb_size = sizeof(RBDAIOCB),
f27aaf4b
CB
789};
790
e04fb07f 791static void rbd_finish_bh(void *opaque)
f27aaf4b 792{
e04fb07f 793 RADOSCB *rcb = opaque;
e04fb07f 794 qemu_rbd_complete_aio(rcb);
ad32e9c0
JD
795}
796
797/*
798 * This is the callback function for rbd_aio_read and _write
799 *
800 * Note: this function is being called from a non qemu thread so
801 * we need to be careful about what we do here. Generally we only
e04fb07f
SH
802 * schedule a BH, and do the rest of the io completion handling
803 * from rbd_finish_bh() which runs in a qemu context.
ad32e9c0
JD
804 */
805static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
806{
e04fb07f
SH
807 RBDAIOCB *acb = rcb->acb;
808
ad32e9c0
JD
809 rcb->ret = rbd_aio_get_return_value(c);
810 rbd_aio_release(c);
f27aaf4b 811
fffb6e12
PB
812 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
813 rbd_finish_bh, rcb);
f27aaf4b
CB
814}
815
787f3133
JD
816static int rbd_aio_discard_wrapper(rbd_image_t image,
817 uint64_t off,
818 uint64_t len,
819 rbd_completion_t comp)
820{
821#ifdef LIBRBD_SUPPORTS_DISCARD
822 return rbd_aio_discard(image, off, len, comp);
823#else
824 return -ENOTSUP;
825#endif
826}
827
dc7588c1
JD
828static int rbd_aio_flush_wrapper(rbd_image_t image,
829 rbd_completion_t comp)
830{
831#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
832 return rbd_aio_flush(image, comp);
833#else
834 return -ENOTSUP;
835#endif
836}
837
7c84b1b8 838static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
7bbca9e2 839 int64_t off,
7c84b1b8 840 QEMUIOVector *qiov,
7bbca9e2 841 int64_t size,
097310b5 842 BlockCompletionFunc *cb,
7c84b1b8
MA
843 void *opaque,
844 RBDAIOCmd cmd)
f27aaf4b
CB
845{
846 RBDAIOCB *acb;
0f7a0237 847 RADOSCB *rcb = NULL;
ad32e9c0 848 rbd_completion_t c;
51a13528 849 int r;
f27aaf4b
CB
850
851 BDRVRBDState *s = bs->opaque;
852
d7331bed 853 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
787f3133 854 acb->cmd = cmd;
f27aaf4b 855 acb->qiov = qiov;
7bbca9e2 856 assert(!qiov || qiov->size == size);
1d393bde 857
858 rcb = g_new(RADOSCB, 1);
859
860 if (!LIBRBD_USE_IOVEC) {
861 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
862 acb->bounce = NULL;
863 } else {
864 acb->bounce = qemu_try_blockalign(bs, qiov->size);
865 if (acb->bounce == NULL) {
866 goto failed;
867 }
0f7a0237 868 }
1d393bde 869 if (cmd == RBD_AIO_WRITE) {
870 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
871 }
872 rcb->buf = acb->bounce;
787f3133 873 }
1d393bde 874
f27aaf4b
CB
875 acb->ret = 0;
876 acb->error = 0;
877 acb->s = s;
f27aaf4b 878
ad32e9c0 879 rcb->acb = acb;
ad32e9c0
JD
880 rcb->s = acb->s;
881 rcb->size = size;
51a13528
JD
882 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
883 if (r < 0) {
884 goto failed;
885 }
f27aaf4b 886
787f3133
JD
887 switch (cmd) {
888 case RBD_AIO_WRITE:
1d393bde 889#ifdef LIBRBD_SUPPORTS_IOVEC
890 r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
891#else
892 r = rbd_aio_write(s->image, off, size, rcb->buf, c);
893#endif
787f3133
JD
894 break;
895 case RBD_AIO_READ:
1d393bde 896#ifdef LIBRBD_SUPPORTS_IOVEC
897 r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
898#else
899 r = rbd_aio_read(s->image, off, size, rcb->buf, c);
900#endif
787f3133
JD
901 break;
902 case RBD_AIO_DISCARD:
903 r = rbd_aio_discard_wrapper(s->image, off, size, c);
904 break;
dc7588c1
JD
905 case RBD_AIO_FLUSH:
906 r = rbd_aio_flush_wrapper(s->image, c);
907 break;
787f3133
JD
908 default:
909 r = -EINVAL;
51a13528
JD
910 }
911
912 if (r < 0) {
405a2764 913 goto failed_completion;
f27aaf4b 914 }
f27aaf4b 915 return &acb->common;
51a13528 916
405a2764
KW
917failed_completion:
918 rbd_aio_release(c);
51a13528 919failed:
7267c094 920 g_free(rcb);
1d393bde 921 if (!LIBRBD_USE_IOVEC) {
922 qemu_vfree(acb->bounce);
923 }
924
8007429a 925 qemu_aio_unref(acb);
51a13528 926 return NULL;
f27aaf4b
CB
927}
928
7c84b1b8
MA
929static BlockAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs,
930 int64_t sector_num,
931 QEMUIOVector *qiov,
932 int nb_sectors,
097310b5 933 BlockCompletionFunc *cb,
7c84b1b8 934 void *opaque)
f27aaf4b 935{
7bbca9e2 936 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
e948f663 937 (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
787f3133 938 RBD_AIO_READ);
f27aaf4b
CB
939}
940
7c84b1b8
MA
941static BlockAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs,
942 int64_t sector_num,
943 QEMUIOVector *qiov,
944 int nb_sectors,
097310b5 945 BlockCompletionFunc *cb,
7c84b1b8 946 void *opaque)
f27aaf4b 947{
7bbca9e2 948 return rbd_start_aio(bs, sector_num << BDRV_SECTOR_BITS, qiov,
e948f663 949 (int64_t) nb_sectors << BDRV_SECTOR_BITS, cb, opaque,
787f3133 950 RBD_AIO_WRITE);
f27aaf4b
CB
951}
952
dc7588c1 953#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
7c84b1b8 954static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
097310b5 955 BlockCompletionFunc *cb,
7c84b1b8 956 void *opaque)
dc7588c1
JD
957{
958 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
959}
960
961#else
962
8b94ff85 963static int qemu_rbd_co_flush(BlockDriverState *bs)
7a3f5fe9
SW
964{
965#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
966 /* rbd_flush added in 0.1.1 */
967 BDRVRBDState *s = bs->opaque;
968 return rbd_flush(s->image);
969#else
970 return 0;
971#endif
972}
dc7588c1 973#endif
7a3f5fe9 974
ad32e9c0 975static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
f27aaf4b
CB
976{
977 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
978 rbd_image_info_t info;
979 int r;
980
981 r = rbd_stat(s->image, &info, sizeof(info));
982 if (r < 0) {
983 return r;
984 }
985
986 bdi->cluster_size = info.obj_size;
f27aaf4b
CB
987 return 0;
988}
989
ad32e9c0 990static int64_t qemu_rbd_getlength(BlockDriverState *bs)
f27aaf4b
CB
991{
992 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
993 rbd_image_info_t info;
994 int r;
f27aaf4b 995
ad32e9c0
JD
996 r = rbd_stat(s->image, &info, sizeof(info));
997 if (r < 0) {
998 return r;
999 }
1000
1001 return info.size;
f27aaf4b
CB
1002}
1003
8243ccb7
HR
1004static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset,
1005 PreallocMode prealloc, Error **errp)
30cdc48c
JD
1006{
1007 BDRVRBDState *s = bs->opaque;
1008 int r;
1009
8243ccb7
HR
1010 if (prealloc != PREALLOC_MODE_OFF) {
1011 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 1012 PreallocMode_str(prealloc));
8243ccb7
HR
1013 return -ENOTSUP;
1014 }
1015
30cdc48c
JD
1016 r = rbd_resize(s->image, offset);
1017 if (r < 0) {
f59adb32 1018 error_setg_errno(errp, -r, "Failed to resize file");
30cdc48c
JD
1019 return r;
1020 }
1021
1022 return 0;
1023}
1024
ad32e9c0
JD
1025static int qemu_rbd_snap_create(BlockDriverState *bs,
1026 QEMUSnapshotInfo *sn_info)
f27aaf4b
CB
1027{
1028 BDRVRBDState *s = bs->opaque;
f27aaf4b 1029 int r;
f27aaf4b
CB
1030
1031 if (sn_info->name[0] == '\0') {
1032 return -EINVAL; /* we need a name for rbd snapshots */
1033 }
1034
1035 /*
1036 * rbd snapshots are using the name as the user controlled unique identifier
1037 * we can't use the rbd snapid for that purpose, as it can't be set
1038 */
1039 if (sn_info->id_str[0] != '\0' &&
1040 strcmp(sn_info->id_str, sn_info->name) != 0) {
1041 return -EINVAL;
1042 }
1043
1044 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1045 return -ERANGE;
1046 }
1047
ad32e9c0 1048 r = rbd_snap_create(s->image, sn_info->name);
f27aaf4b 1049 if (r < 0) {
ad32e9c0 1050 error_report("failed to create snap: %s", strerror(-r));
f27aaf4b
CB
1051 return r;
1052 }
1053
f27aaf4b
CB
1054 return 0;
1055}
1056
bd603247 1057static int qemu_rbd_snap_remove(BlockDriverState *bs,
a89d89d3
WX
1058 const char *snapshot_id,
1059 const char *snapshot_name,
1060 Error **errp)
bd603247
GF
1061{
1062 BDRVRBDState *s = bs->opaque;
1063 int r;
1064
a89d89d3
WX
1065 if (!snapshot_name) {
1066 error_setg(errp, "rbd need a valid snapshot name");
1067 return -EINVAL;
1068 }
1069
1070 /* If snapshot_id is specified, it must be equal to name, see
1071 qemu_rbd_snap_list() */
1072 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1073 error_setg(errp,
1074 "rbd do not support snapshot id, it should be NULL or "
1075 "equal to snapshot name");
1076 return -EINVAL;
1077 }
1078
bd603247 1079 r = rbd_snap_remove(s->image, snapshot_name);
a89d89d3
WX
1080 if (r < 0) {
1081 error_setg_errno(errp, -r, "Failed to remove the snapshot");
1082 }
bd603247
GF
1083 return r;
1084}
1085
1086static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1087 const char *snapshot_name)
1088{
1089 BDRVRBDState *s = bs->opaque;
bd603247 1090
9be38598 1091 return rbd_snap_rollback(s->image, snapshot_name);
bd603247
GF
1092}
1093
ad32e9c0
JD
1094static int qemu_rbd_snap_list(BlockDriverState *bs,
1095 QEMUSnapshotInfo **psn_tab)
f27aaf4b
CB
1096{
1097 BDRVRBDState *s = bs->opaque;
f27aaf4b 1098 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
ad32e9c0
JD
1099 int i, snap_count;
1100 rbd_snap_info_t *snaps;
1101 int max_snaps = RBD_MAX_SNAPS;
f27aaf4b 1102
ad32e9c0 1103 do {
02c4f26b 1104 snaps = g_new(rbd_snap_info_t, max_snaps);
ad32e9c0 1105 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
9e6337d0 1106 if (snap_count <= 0) {
7267c094 1107 g_free(snaps);
f27aaf4b 1108 }
ad32e9c0 1109 } while (snap_count == -ERANGE);
f27aaf4b 1110
ad32e9c0 1111 if (snap_count <= 0) {
b9c53290 1112 goto done;
f27aaf4b
CB
1113 }
1114
5839e53b 1115 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
f27aaf4b 1116
ad32e9c0
JD
1117 for (i = 0; i < snap_count; i++) {
1118 const char *snap_name = snaps[i].name;
f27aaf4b
CB
1119
1120 sn_info = sn_tab + i;
1121 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1122 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
f27aaf4b 1123
ad32e9c0 1124 sn_info->vm_state_size = snaps[i].size;
f27aaf4b
CB
1125 sn_info->date_sec = 0;
1126 sn_info->date_nsec = 0;
1127 sn_info->vm_clock_nsec = 0;
1128 }
ad32e9c0 1129 rbd_snap_list_end(snaps);
9e6337d0 1130 g_free(snaps);
ad32e9c0 1131
b9c53290 1132 done:
f27aaf4b 1133 *psn_tab = sn_tab;
f27aaf4b 1134 return snap_count;
f27aaf4b
CB
1135}
1136
787f3133 1137#ifdef LIBRBD_SUPPORTS_DISCARD
4da444a0
EB
1138static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
1139 int64_t offset,
f5a5ca79 1140 int bytes,
4da444a0
EB
1141 BlockCompletionFunc *cb,
1142 void *opaque)
787f3133 1143{
f5a5ca79 1144 return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
787f3133
JD
1145 RBD_AIO_DISCARD);
1146}
1147#endif
1148
be217884 1149#ifdef LIBRBD_SUPPORTS_INVALIDATE
2b148f39
PB
1150static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1151 Error **errp)
be217884
AC
1152{
1153 BDRVRBDState *s = bs->opaque;
1154 int r = rbd_invalidate_cache(s->image);
1155 if (r < 0) {
1156 error_setg_errno(errp, -r, "Failed to invalidate the cache");
1157 }
1158}
1159#endif
1160
bd0cf596
CL
1161static QemuOptsList qemu_rbd_create_opts = {
1162 .name = "rbd-create-opts",
1163 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1164 .desc = {
1165 {
1166 .name = BLOCK_OPT_SIZE,
1167 .type = QEMU_OPT_SIZE,
1168 .help = "Virtual disk size"
1169 },
1170 {
1171 .name = BLOCK_OPT_CLUSTER_SIZE,
1172 .type = QEMU_OPT_SIZE,
1173 .help = "RBD object size"
1174 },
60390a21
DB
1175 {
1176 .name = "password-secret",
1177 .type = QEMU_OPT_STRING,
1178 .help = "ID of secret providing the password",
1179 },
bd0cf596
CL
1180 { /* end of list */ }
1181 }
f27aaf4b
CB
1182};
1183
1184static BlockDriver bdrv_rbd = {
c7cacb3e
JC
1185 .format_name = "rbd",
1186 .instance_size = sizeof(BDRVRBDState),
1187 .bdrv_parse_filename = qemu_rbd_parse_filename,
1188 .bdrv_file_open = qemu_rbd_open,
1189 .bdrv_close = qemu_rbd_close,
56e7cf8d 1190 .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
1bebea37 1191 .bdrv_co_create = qemu_rbd_co_create,
efc75e2a 1192 .bdrv_co_create_opts = qemu_rbd_co_create_opts,
c7cacb3e
JC
1193 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1194 .bdrv_get_info = qemu_rbd_getinfo,
1195 .create_opts = &qemu_rbd_create_opts,
1196 .bdrv_getlength = qemu_rbd_getlength,
1197 .bdrv_truncate = qemu_rbd_truncate,
1198 .protocol_name = "rbd",
f27aaf4b 1199
c68b89ac
KW
1200 .bdrv_aio_readv = qemu_rbd_aio_readv,
1201 .bdrv_aio_writev = qemu_rbd_aio_writev,
dc7588c1
JD
1202
1203#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1204 .bdrv_aio_flush = qemu_rbd_aio_flush,
1205#else
c68b89ac 1206 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
dc7588c1 1207#endif
f27aaf4b 1208
787f3133 1209#ifdef LIBRBD_SUPPORTS_DISCARD
4da444a0 1210 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
787f3133
JD
1211#endif
1212
c68b89ac 1213 .bdrv_snapshot_create = qemu_rbd_snap_create,
bd603247 1214 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
c68b89ac 1215 .bdrv_snapshot_list = qemu_rbd_snap_list,
bd603247 1216 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
be217884 1217#ifdef LIBRBD_SUPPORTS_INVALIDATE
2b148f39 1218 .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
be217884 1219#endif
f27aaf4b
CB
1220};
1221
1222static void bdrv_rbd_init(void)
1223{
1224 bdrv_register(&bdrv_rbd);
1225}
1226
1227block_init(bdrv_rbd_init);