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