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