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