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