]> Git Repo - qemu.git/blame - block/rbd.c
block: Fix -blockdev / blockdev-add for empty objects and arrays
[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;
bfb15b4b 633 const QDictEntry *e;
3d9136f9 634 Error *local_err = NULL;
4ff45049 635 char *keypairs, *secretid;
3d9136f9
KW
636 int r;
637
4ff45049
KW
638 keypairs = g_strdup(qdict_get_try_str(options, "=keyvalue-pairs"));
639 if (keypairs) {
640 qdict_del(options, "=keyvalue-pairs");
641 }
642
643 secretid = g_strdup(qdict_get_try_str(options, "password-secret"));
644 if (secretid) {
645 qdict_del(options, "password-secret");
646 }
647
4bfb2741 648 /* Convert the remaining options into a QAPI object */
af91062e
MA
649 v = qobject_input_visitor_new_flat_confused(options, errp);
650 if (!v) {
4bfb2741
KW
651 r = -EINVAL;
652 goto out;
653 }
654
4bfb2741
KW
655 visit_type_BlockdevOptionsRbd(v, NULL, &opts, &local_err);
656 visit_free(v);
4bfb2741
KW
657
658 if (local_err) {
659 error_propagate(errp, local_err);
660 r = -EINVAL;
661 goto out;
662 }
663
bfb15b4b
JC
664 /* Remove the processed options from the QDict (the visitor processes
665 * _all_ options in the QDict) */
666 while ((e = qdict_first(options))) {
667 qdict_del(options, e->key);
668 }
669
d41a5588
KW
670 r = qemu_rbd_connect(&s->cluster, &s->io_ctx, opts,
671 !(flags & BDRV_O_NOCACHE), keypairs, secretid, errp);
3d9136f9 672 if (r < 0) {
4ff45049 673 goto out;
3d9136f9
KW
674 }
675
d41a5588
KW
676 s->snap = g_strdup(opts->snapshot);
677 s->image_name = g_strdup(opts->image);
678
e2b8247a 679 /* rbd_open is always r/w */
80b61a27 680 r = rbd_open(s->io_ctx, s->image_name, &s->image, s->snap);
f27aaf4b 681 if (r < 0) {
80b61a27
JC
682 error_setg_errno(errp, -r, "error reading header from %s",
683 s->image_name);
eb93d5d9 684 goto failed_open;
f27aaf4b
CB
685 }
686
e2b8247a
JC
687 /* If we are using an rbd snapshot, we must be r/o, otherwise
688 * leave as-is */
689 if (s->snap != NULL) {
398e6ad0
KW
690 if (!bdrv_is_read_only(bs)) {
691 error_report("Opening rbd snapshots without an explicit "
692 "read-only=on option is deprecated. Future versions "
693 "will refuse to open the image instead of "
694 "automatically marking the image read-only.");
695 r = bdrv_set_read_only(bs, true, &local_err);
696 if (r < 0) {
697 error_propagate(errp, local_err);
698 goto failed_open;
699 }
e2b8247a
JC
700 }
701 }
f27aaf4b 702
4ff45049
KW
703 r = 0;
704 goto out;
f27aaf4b 705
eb93d5d9 706failed_open:
ad32e9c0 707 rados_ioctx_destroy(s->io_ctx);
eb93d5d9 708 g_free(s->snap);
80b61a27 709 g_free(s->image_name);
3d9136f9 710 rados_shutdown(s->cluster);
4ff45049 711out:
4bfb2741 712 qapi_free_BlockdevOptionsRbd(opts);
4ff45049
KW
713 g_free(keypairs);
714 g_free(secretid);
f27aaf4b
CB
715 return r;
716}
717
56e7cf8d
JC
718
719/* Since RBD is currently always opened R/W via the API,
720 * we just need to check if we are using a snapshot or not, in
721 * order to determine if we will allow it to be R/W */
722static int qemu_rbd_reopen_prepare(BDRVReopenState *state,
723 BlockReopenQueue *queue, Error **errp)
724{
725 BDRVRBDState *s = state->bs->opaque;
726 int ret = 0;
727
728 if (s->snap && state->flags & BDRV_O_RDWR) {
729 error_setg(errp,
730 "Cannot change node '%s' to r/w when using RBD snapshot",
731 bdrv_get_device_or_node_name(state->bs));
732 ret = -EINVAL;
733 }
734
735 return ret;
736}
737
ad32e9c0 738static void qemu_rbd_close(BlockDriverState *bs)
f27aaf4b
CB
739{
740 BDRVRBDState *s = bs->opaque;
741
ad32e9c0
JD
742 rbd_close(s->image);
743 rados_ioctx_destroy(s->io_ctx);
7267c094 744 g_free(s->snap);
80b61a27 745 g_free(s->image_name);
ad32e9c0 746 rados_shutdown(s->cluster);
f27aaf4b
CB
747}
748
d7331bed 749static const AIOCBInfo rbd_aiocb_info = {
f27aaf4b 750 .aiocb_size = sizeof(RBDAIOCB),
f27aaf4b
CB
751};
752
e04fb07f 753static void rbd_finish_bh(void *opaque)
f27aaf4b 754{
e04fb07f 755 RADOSCB *rcb = opaque;
e04fb07f 756 qemu_rbd_complete_aio(rcb);
ad32e9c0
JD
757}
758
759/*
760 * This is the callback function for rbd_aio_read and _write
761 *
762 * Note: this function is being called from a non qemu thread so
763 * we need to be careful about what we do here. Generally we only
e04fb07f
SH
764 * schedule a BH, and do the rest of the io completion handling
765 * from rbd_finish_bh() which runs in a qemu context.
ad32e9c0
JD
766 */
767static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb)
768{
e04fb07f
SH
769 RBDAIOCB *acb = rcb->acb;
770
ad32e9c0
JD
771 rcb->ret = rbd_aio_get_return_value(c);
772 rbd_aio_release(c);
f27aaf4b 773
fffb6e12
PB
774 aio_bh_schedule_oneshot(bdrv_get_aio_context(acb->common.bs),
775 rbd_finish_bh, rcb);
f27aaf4b
CB
776}
777
787f3133
JD
778static int rbd_aio_discard_wrapper(rbd_image_t image,
779 uint64_t off,
780 uint64_t len,
781 rbd_completion_t comp)
782{
783#ifdef LIBRBD_SUPPORTS_DISCARD
784 return rbd_aio_discard(image, off, len, comp);
785#else
786 return -ENOTSUP;
787#endif
788}
789
dc7588c1
JD
790static int rbd_aio_flush_wrapper(rbd_image_t image,
791 rbd_completion_t comp)
792{
793#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
794 return rbd_aio_flush(image, comp);
795#else
796 return -ENOTSUP;
797#endif
798}
799
7c84b1b8 800static BlockAIOCB *rbd_start_aio(BlockDriverState *bs,
7bbca9e2 801 int64_t off,
7c84b1b8 802 QEMUIOVector *qiov,
7bbca9e2 803 int64_t size,
097310b5 804 BlockCompletionFunc *cb,
7c84b1b8
MA
805 void *opaque,
806 RBDAIOCmd cmd)
f27aaf4b
CB
807{
808 RBDAIOCB *acb;
0f7a0237 809 RADOSCB *rcb = NULL;
ad32e9c0 810 rbd_completion_t c;
51a13528 811 int r;
f27aaf4b
CB
812
813 BDRVRBDState *s = bs->opaque;
814
d7331bed 815 acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque);
787f3133 816 acb->cmd = cmd;
f27aaf4b 817 acb->qiov = qiov;
7bbca9e2 818 assert(!qiov || qiov->size == size);
1d393bde 819
820 rcb = g_new(RADOSCB, 1);
821
822 if (!LIBRBD_USE_IOVEC) {
823 if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) {
824 acb->bounce = NULL;
825 } else {
826 acb->bounce = qemu_try_blockalign(bs, qiov->size);
827 if (acb->bounce == NULL) {
828 goto failed;
829 }
0f7a0237 830 }
1d393bde 831 if (cmd == RBD_AIO_WRITE) {
832 qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
833 }
834 rcb->buf = acb->bounce;
787f3133 835 }
1d393bde 836
f27aaf4b
CB
837 acb->ret = 0;
838 acb->error = 0;
839 acb->s = s;
f27aaf4b 840
ad32e9c0 841 rcb->acb = acb;
ad32e9c0
JD
842 rcb->s = acb->s;
843 rcb->size = size;
51a13528
JD
844 r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c);
845 if (r < 0) {
846 goto failed;
847 }
f27aaf4b 848
787f3133
JD
849 switch (cmd) {
850 case RBD_AIO_WRITE:
1d393bde 851#ifdef LIBRBD_SUPPORTS_IOVEC
852 r = rbd_aio_writev(s->image, qiov->iov, qiov->niov, off, c);
853#else
854 r = rbd_aio_write(s->image, off, size, rcb->buf, c);
855#endif
787f3133
JD
856 break;
857 case RBD_AIO_READ:
1d393bde 858#ifdef LIBRBD_SUPPORTS_IOVEC
859 r = rbd_aio_readv(s->image, qiov->iov, qiov->niov, off, c);
860#else
861 r = rbd_aio_read(s->image, off, size, rcb->buf, c);
862#endif
787f3133
JD
863 break;
864 case RBD_AIO_DISCARD:
865 r = rbd_aio_discard_wrapper(s->image, off, size, c);
866 break;
dc7588c1
JD
867 case RBD_AIO_FLUSH:
868 r = rbd_aio_flush_wrapper(s->image, c);
869 break;
787f3133
JD
870 default:
871 r = -EINVAL;
51a13528
JD
872 }
873
874 if (r < 0) {
405a2764 875 goto failed_completion;
f27aaf4b 876 }
f27aaf4b 877 return &acb->common;
51a13528 878
405a2764
KW
879failed_completion:
880 rbd_aio_release(c);
51a13528 881failed:
7267c094 882 g_free(rcb);
1d393bde 883 if (!LIBRBD_USE_IOVEC) {
884 qemu_vfree(acb->bounce);
885 }
886
8007429a 887 qemu_aio_unref(acb);
51a13528 888 return NULL;
f27aaf4b
CB
889}
890
e8e16d4b
EB
891static BlockAIOCB *qemu_rbd_aio_preadv(BlockDriverState *bs,
892 uint64_t offset, uint64_t bytes,
893 QEMUIOVector *qiov, int flags,
894 BlockCompletionFunc *cb,
895 void *opaque)
f27aaf4b 896{
e8e16d4b 897 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
787f3133 898 RBD_AIO_READ);
f27aaf4b
CB
899}
900
e8e16d4b
EB
901static BlockAIOCB *qemu_rbd_aio_pwritev(BlockDriverState *bs,
902 uint64_t offset, uint64_t bytes,
903 QEMUIOVector *qiov, int flags,
904 BlockCompletionFunc *cb,
905 void *opaque)
f27aaf4b 906{
e8e16d4b 907 return rbd_start_aio(bs, offset, qiov, bytes, cb, opaque,
787f3133 908 RBD_AIO_WRITE);
f27aaf4b
CB
909}
910
dc7588c1 911#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
7c84b1b8 912static BlockAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs,
097310b5 913 BlockCompletionFunc *cb,
7c84b1b8 914 void *opaque)
dc7588c1
JD
915{
916 return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH);
917}
918
919#else
920
8b94ff85 921static int qemu_rbd_co_flush(BlockDriverState *bs)
7a3f5fe9
SW
922{
923#if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1)
924 /* rbd_flush added in 0.1.1 */
925 BDRVRBDState *s = bs->opaque;
926 return rbd_flush(s->image);
927#else
928 return 0;
929#endif
930}
dc7588c1 931#endif
7a3f5fe9 932
ad32e9c0 933static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi)
f27aaf4b
CB
934{
935 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
936 rbd_image_info_t info;
937 int r;
938
939 r = rbd_stat(s->image, &info, sizeof(info));
940 if (r < 0) {
941 return r;
942 }
943
944 bdi->cluster_size = info.obj_size;
f27aaf4b
CB
945 return 0;
946}
947
ad32e9c0 948static int64_t qemu_rbd_getlength(BlockDriverState *bs)
f27aaf4b
CB
949{
950 BDRVRBDState *s = bs->opaque;
ad32e9c0
JD
951 rbd_image_info_t info;
952 int r;
f27aaf4b 953
ad32e9c0
JD
954 r = rbd_stat(s->image, &info, sizeof(info));
955 if (r < 0) {
956 return r;
957 }
958
959 return info.size;
f27aaf4b
CB
960}
961
8243ccb7
HR
962static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset,
963 PreallocMode prealloc, Error **errp)
30cdc48c
JD
964{
965 BDRVRBDState *s = bs->opaque;
966 int r;
967
8243ccb7
HR
968 if (prealloc != PREALLOC_MODE_OFF) {
969 error_setg(errp, "Unsupported preallocation mode '%s'",
977c736f 970 PreallocMode_str(prealloc));
8243ccb7
HR
971 return -ENOTSUP;
972 }
973
30cdc48c
JD
974 r = rbd_resize(s->image, offset);
975 if (r < 0) {
f59adb32 976 error_setg_errno(errp, -r, "Failed to resize file");
30cdc48c
JD
977 return r;
978 }
979
980 return 0;
981}
982
ad32e9c0
JD
983static int qemu_rbd_snap_create(BlockDriverState *bs,
984 QEMUSnapshotInfo *sn_info)
f27aaf4b
CB
985{
986 BDRVRBDState *s = bs->opaque;
f27aaf4b 987 int r;
f27aaf4b
CB
988
989 if (sn_info->name[0] == '\0') {
990 return -EINVAL; /* we need a name for rbd snapshots */
991 }
992
993 /*
994 * rbd snapshots are using the name as the user controlled unique identifier
995 * we can't use the rbd snapid for that purpose, as it can't be set
996 */
997 if (sn_info->id_str[0] != '\0' &&
998 strcmp(sn_info->id_str, sn_info->name) != 0) {
999 return -EINVAL;
1000 }
1001
1002 if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) {
1003 return -ERANGE;
1004 }
1005
ad32e9c0 1006 r = rbd_snap_create(s->image, sn_info->name);
f27aaf4b 1007 if (r < 0) {
ad32e9c0 1008 error_report("failed to create snap: %s", strerror(-r));
f27aaf4b
CB
1009 return r;
1010 }
1011
f27aaf4b
CB
1012 return 0;
1013}
1014
bd603247 1015static int qemu_rbd_snap_remove(BlockDriverState *bs,
a89d89d3
WX
1016 const char *snapshot_id,
1017 const char *snapshot_name,
1018 Error **errp)
bd603247
GF
1019{
1020 BDRVRBDState *s = bs->opaque;
1021 int r;
1022
a89d89d3
WX
1023 if (!snapshot_name) {
1024 error_setg(errp, "rbd need a valid snapshot name");
1025 return -EINVAL;
1026 }
1027
1028 /* If snapshot_id is specified, it must be equal to name, see
1029 qemu_rbd_snap_list() */
1030 if (snapshot_id && strcmp(snapshot_id, snapshot_name)) {
1031 error_setg(errp,
1032 "rbd do not support snapshot id, it should be NULL or "
1033 "equal to snapshot name");
1034 return -EINVAL;
1035 }
1036
bd603247 1037 r = rbd_snap_remove(s->image, snapshot_name);
a89d89d3
WX
1038 if (r < 0) {
1039 error_setg_errno(errp, -r, "Failed to remove the snapshot");
1040 }
bd603247
GF
1041 return r;
1042}
1043
1044static int qemu_rbd_snap_rollback(BlockDriverState *bs,
1045 const char *snapshot_name)
1046{
1047 BDRVRBDState *s = bs->opaque;
bd603247 1048
9be38598 1049 return rbd_snap_rollback(s->image, snapshot_name);
bd603247
GF
1050}
1051
ad32e9c0
JD
1052static int qemu_rbd_snap_list(BlockDriverState *bs,
1053 QEMUSnapshotInfo **psn_tab)
f27aaf4b
CB
1054{
1055 BDRVRBDState *s = bs->opaque;
f27aaf4b 1056 QEMUSnapshotInfo *sn_info, *sn_tab = NULL;
ad32e9c0
JD
1057 int i, snap_count;
1058 rbd_snap_info_t *snaps;
1059 int max_snaps = RBD_MAX_SNAPS;
f27aaf4b 1060
ad32e9c0 1061 do {
02c4f26b 1062 snaps = g_new(rbd_snap_info_t, max_snaps);
ad32e9c0 1063 snap_count = rbd_snap_list(s->image, snaps, &max_snaps);
9e6337d0 1064 if (snap_count <= 0) {
7267c094 1065 g_free(snaps);
f27aaf4b 1066 }
ad32e9c0 1067 } while (snap_count == -ERANGE);
f27aaf4b 1068
ad32e9c0 1069 if (snap_count <= 0) {
b9c53290 1070 goto done;
f27aaf4b
CB
1071 }
1072
5839e53b 1073 sn_tab = g_new0(QEMUSnapshotInfo, snap_count);
f27aaf4b 1074
ad32e9c0
JD
1075 for (i = 0; i < snap_count; i++) {
1076 const char *snap_name = snaps[i].name;
f27aaf4b
CB
1077
1078 sn_info = sn_tab + i;
1079 pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name);
1080 pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name);
f27aaf4b 1081
ad32e9c0 1082 sn_info->vm_state_size = snaps[i].size;
f27aaf4b
CB
1083 sn_info->date_sec = 0;
1084 sn_info->date_nsec = 0;
1085 sn_info->vm_clock_nsec = 0;
1086 }
ad32e9c0 1087 rbd_snap_list_end(snaps);
9e6337d0 1088 g_free(snaps);
ad32e9c0 1089
b9c53290 1090 done:
f27aaf4b 1091 *psn_tab = sn_tab;
f27aaf4b 1092 return snap_count;
f27aaf4b
CB
1093}
1094
787f3133 1095#ifdef LIBRBD_SUPPORTS_DISCARD
4da444a0
EB
1096static BlockAIOCB *qemu_rbd_aio_pdiscard(BlockDriverState *bs,
1097 int64_t offset,
f5a5ca79 1098 int bytes,
4da444a0
EB
1099 BlockCompletionFunc *cb,
1100 void *opaque)
787f3133 1101{
f5a5ca79 1102 return rbd_start_aio(bs, offset, NULL, bytes, cb, opaque,
787f3133
JD
1103 RBD_AIO_DISCARD);
1104}
1105#endif
1106
be217884 1107#ifdef LIBRBD_SUPPORTS_INVALIDATE
2b148f39
PB
1108static void coroutine_fn qemu_rbd_co_invalidate_cache(BlockDriverState *bs,
1109 Error **errp)
be217884
AC
1110{
1111 BDRVRBDState *s = bs->opaque;
1112 int r = rbd_invalidate_cache(s->image);
1113 if (r < 0) {
1114 error_setg_errno(errp, -r, "Failed to invalidate the cache");
1115 }
1116}
1117#endif
1118
bd0cf596
CL
1119static QemuOptsList qemu_rbd_create_opts = {
1120 .name = "rbd-create-opts",
1121 .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head),
1122 .desc = {
1123 {
1124 .name = BLOCK_OPT_SIZE,
1125 .type = QEMU_OPT_SIZE,
1126 .help = "Virtual disk size"
1127 },
1128 {
1129 .name = BLOCK_OPT_CLUSTER_SIZE,
1130 .type = QEMU_OPT_SIZE,
1131 .help = "RBD object size"
1132 },
60390a21
DB
1133 {
1134 .name = "password-secret",
1135 .type = QEMU_OPT_STRING,
1136 .help = "ID of secret providing the password",
1137 },
bd0cf596
CL
1138 { /* end of list */ }
1139 }
f27aaf4b
CB
1140};
1141
1142static BlockDriver bdrv_rbd = {
c7cacb3e
JC
1143 .format_name = "rbd",
1144 .instance_size = sizeof(BDRVRBDState),
1145 .bdrv_parse_filename = qemu_rbd_parse_filename,
e8e16d4b 1146 .bdrv_refresh_limits = qemu_rbd_refresh_limits,
c7cacb3e
JC
1147 .bdrv_file_open = qemu_rbd_open,
1148 .bdrv_close = qemu_rbd_close,
56e7cf8d 1149 .bdrv_reopen_prepare = qemu_rbd_reopen_prepare,
1bebea37 1150 .bdrv_co_create = qemu_rbd_co_create,
efc75e2a 1151 .bdrv_co_create_opts = qemu_rbd_co_create_opts,
c7cacb3e
JC
1152 .bdrv_has_zero_init = bdrv_has_zero_init_1,
1153 .bdrv_get_info = qemu_rbd_getinfo,
1154 .create_opts = &qemu_rbd_create_opts,
1155 .bdrv_getlength = qemu_rbd_getlength,
1156 .bdrv_truncate = qemu_rbd_truncate,
1157 .protocol_name = "rbd",
f27aaf4b 1158
e8e16d4b
EB
1159 .bdrv_aio_preadv = qemu_rbd_aio_preadv,
1160 .bdrv_aio_pwritev = qemu_rbd_aio_pwritev,
dc7588c1
JD
1161
1162#ifdef LIBRBD_SUPPORTS_AIO_FLUSH
1163 .bdrv_aio_flush = qemu_rbd_aio_flush,
1164#else
c68b89ac 1165 .bdrv_co_flush_to_disk = qemu_rbd_co_flush,
dc7588c1 1166#endif
f27aaf4b 1167
787f3133 1168#ifdef LIBRBD_SUPPORTS_DISCARD
4da444a0 1169 .bdrv_aio_pdiscard = qemu_rbd_aio_pdiscard,
787f3133
JD
1170#endif
1171
c68b89ac 1172 .bdrv_snapshot_create = qemu_rbd_snap_create,
bd603247 1173 .bdrv_snapshot_delete = qemu_rbd_snap_remove,
c68b89ac 1174 .bdrv_snapshot_list = qemu_rbd_snap_list,
bd603247 1175 .bdrv_snapshot_goto = qemu_rbd_snap_rollback,
be217884 1176#ifdef LIBRBD_SUPPORTS_INVALIDATE
2b148f39 1177 .bdrv_co_invalidate_cache = qemu_rbd_co_invalidate_cache,
be217884 1178#endif
f27aaf4b
CB
1179};
1180
1181static void bdrv_rbd_init(void)
1182{
1183 bdrv_register(&bdrv_rbd);
1184}
1185
1186block_init(bdrv_rbd_init);
This page took 0.567611 seconds and 4 git commands to generate.