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