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