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