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