]>
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 */ |
c2eb918e HT |
317 | bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0), |
318 | BDRV_SECTOR_SIZE); | |
bd0cf596 CL |
319 | objsize = qemu_opt_get_size_del(opts, BLOCK_OPT_CLUSTER_SIZE, 0); |
320 | if (objsize) { | |
321 | if ((objsize - 1) & objsize) { /* not a power of 2? */ | |
322 | error_setg(errp, "obj size needs to be power of 2"); | |
323 | return -EINVAL; | |
324 | } | |
325 | if (objsize < 4096) { | |
326 | error_setg(errp, "obj size too small"); | |
327 | return -EINVAL; | |
f27aaf4b | 328 | } |
bd0cf596 | 329 | obj_order = ffs(objsize) - 1; |
f27aaf4b CB |
330 | } |
331 | ||
7c7e9df0 SW |
332 | clientname = qemu_rbd_parse_clientname(conf, clientname_buf); |
333 | if (rados_create(&cluster, clientname) < 0) { | |
d61563b2 | 334 | error_setg(errp, "error initializing"); |
f27aaf4b CB |
335 | return -EIO; |
336 | } | |
337 | ||
fab5cf59 | 338 | if (strstr(conf, "conf=") == NULL) { |
f9fe18ec SW |
339 | /* try default location, but ignore failure */ |
340 | rados_conf_read_file(cluster, NULL); | |
fab5cf59 JD |
341 | } |
342 | ||
343 | if (conf[0] != '\0' && | |
d61563b2 | 344 | qemu_rbd_set_conf(cluster, conf, &local_err) < 0) { |
ad32e9c0 | 345 | rados_shutdown(cluster); |
d61563b2 | 346 | error_propagate(errp, local_err); |
f27aaf4b CB |
347 | return -EIO; |
348 | } | |
349 | ||
ad32e9c0 | 350 | if (rados_connect(cluster) < 0) { |
d61563b2 | 351 | error_setg(errp, "error connecting"); |
ad32e9c0 | 352 | rados_shutdown(cluster); |
f27aaf4b CB |
353 | return -EIO; |
354 | } | |
f27aaf4b | 355 | |
ad32e9c0 | 356 | if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) { |
d61563b2 | 357 | error_setg(errp, "error opening pool %s", pool); |
ad32e9c0 JD |
358 | rados_shutdown(cluster); |
359 | return -EIO; | |
f27aaf4b CB |
360 | } |
361 | ||
ad32e9c0 JD |
362 | ret = rbd_create(io_ctx, name, bytes, &obj_order); |
363 | rados_ioctx_destroy(io_ctx); | |
364 | rados_shutdown(cluster); | |
f27aaf4b CB |
365 | |
366 | return ret; | |
367 | } | |
368 | ||
369 | /* | |
e04fb07f SH |
370 | * This aio completion is being called from rbd_finish_bh() and runs in qemu |
371 | * BH context. | |
f27aaf4b | 372 | */ |
ad32e9c0 | 373 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
f27aaf4b CB |
374 | { |
375 | RBDAIOCB *acb = rcb->acb; | |
376 | int64_t r; | |
377 | ||
f27aaf4b CB |
378 | r = rcb->ret; |
379 | ||
dc7588c1 | 380 | if (acb->cmd != RBD_AIO_READ) { |
f27aaf4b CB |
381 | if (r < 0) { |
382 | acb->ret = r; | |
383 | acb->error = 1; | |
384 | } else if (!acb->error) { | |
ad32e9c0 | 385 | acb->ret = rcb->size; |
f27aaf4b CB |
386 | } |
387 | } else { | |
ad32e9c0 JD |
388 | if (r < 0) { |
389 | memset(rcb->buf, 0, rcb->size); | |
f27aaf4b CB |
390 | acb->ret = r; |
391 | acb->error = 1; | |
ad32e9c0 JD |
392 | } else if (r < rcb->size) { |
393 | memset(rcb->buf + r, 0, rcb->size - r); | |
f27aaf4b | 394 | if (!acb->error) { |
ad32e9c0 | 395 | acb->ret = rcb->size; |
f27aaf4b CB |
396 | } |
397 | } else if (!acb->error) { | |
ad32e9c0 | 398 | acb->ret = r; |
f27aaf4b CB |
399 | } |
400 | } | |
f27aaf4b | 401 | |
e04fb07f | 402 | g_free(rcb); |
f27aaf4b | 403 | |
e04fb07f SH |
404 | if (acb->cmd == RBD_AIO_READ) { |
405 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); | |
406 | } | |
407 | qemu_vfree(acb->bounce); | |
408 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); | |
409 | acb->status = 0; | |
f27aaf4b | 410 | |
e04fb07f SH |
411 | if (!acb->cancelled) { |
412 | qemu_aio_release(acb); | |
413 | } | |
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) { |
d61563b2 | 465 | error_setg(&local_err, "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 | ||
b11f38fc JD |
474 | /* |
475 | * Fallback to more conservative semantics if setting cache | |
476 | * options fails. Ignore errors from setting rbd_cache because the | |
477 | * only possible error is that the option does not exist, and | |
478 | * librbd defaults to no caching. If write through caching cannot | |
479 | * be set up, fall back to no caching. | |
480 | */ | |
481 | if (flags & BDRV_O_NOCACHE) { | |
482 | rados_conf_set(s->cluster, "rbd_cache", "false"); | |
483 | } else { | |
484 | rados_conf_set(s->cluster, "rbd_cache", "true"); | |
b11f38fc JD |
485 | } |
486 | ||
fab5cf59 | 487 | if (strstr(conf, "conf=") == NULL) { |
f9fe18ec SW |
488 | /* try default location, but ignore failure */ |
489 | rados_conf_read_file(s->cluster, NULL); | |
fab5cf59 JD |
490 | } |
491 | ||
492 | if (conf[0] != '\0') { | |
d61563b2 | 493 | r = qemu_rbd_set_conf(s->cluster, conf, errp); |
fab5cf59 | 494 | if (r < 0) { |
eb93d5d9 | 495 | goto failed_shutdown; |
fab5cf59 | 496 | } |
f27aaf4b CB |
497 | } |
498 | ||
ad32e9c0 JD |
499 | r = rados_connect(s->cluster); |
500 | if (r < 0) { | |
d61563b2 | 501 | error_setg(&local_err, "error connecting"); |
eb93d5d9 | 502 | goto failed_shutdown; |
f27aaf4b CB |
503 | } |
504 | ||
ad32e9c0 JD |
505 | r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); |
506 | if (r < 0) { | |
d61563b2 | 507 | error_setg(&local_err, "error opening pool %s", pool); |
eb93d5d9 | 508 | goto failed_shutdown; |
f27aaf4b CB |
509 | } |
510 | ||
ad32e9c0 | 511 | r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); |
f27aaf4b | 512 | if (r < 0) { |
d61563b2 | 513 | error_setg(&local_err, "error reading header from %s", s->name); |
eb93d5d9 | 514 | goto failed_open; |
f27aaf4b CB |
515 | } |
516 | ||
ad32e9c0 | 517 | bs->read_only = (s->snap != NULL); |
f27aaf4b | 518 | |
c3ca988d | 519 | qemu_opts_del(opts); |
f27aaf4b CB |
520 | return 0; |
521 | ||
eb93d5d9 | 522 | failed_open: |
ad32e9c0 | 523 | rados_ioctx_destroy(s->io_ctx); |
eb93d5d9 | 524 | failed_shutdown: |
ad32e9c0 | 525 | rados_shutdown(s->cluster); |
eb93d5d9 | 526 | g_free(s->snap); |
c3ca988d KW |
527 | failed_opts: |
528 | qemu_opts_del(opts); | |
f27aaf4b CB |
529 | return r; |
530 | } | |
531 | ||
ad32e9c0 | 532 | static void qemu_rbd_close(BlockDriverState *bs) |
f27aaf4b CB |
533 | { |
534 | BDRVRBDState *s = bs->opaque; | |
535 | ||
ad32e9c0 JD |
536 | rbd_close(s->image); |
537 | rados_ioctx_destroy(s->io_ctx); | |
7267c094 | 538 | g_free(s->snap); |
ad32e9c0 | 539 | rados_shutdown(s->cluster); |
f27aaf4b CB |
540 | } |
541 | ||
542 | /* | |
543 | * Cancel aio. Since we don't reference acb in a non qemu threads, | |
544 | * it is safe to access it here. | |
545 | */ | |
ad32e9c0 | 546 | static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb) |
f27aaf4b CB |
547 | { |
548 | RBDAIOCB *acb = (RBDAIOCB *) blockacb; | |
549 | acb->cancelled = 1; | |
473c7f02 SP |
550 | |
551 | while (acb->status == -EINPROGRESS) { | |
ea800191 | 552 | aio_poll(bdrv_get_aio_context(acb->common.bs), true); |
473c7f02 SP |
553 | } |
554 | ||
555 | qemu_aio_release(acb); | |
f27aaf4b CB |
556 | } |
557 | ||
d7331bed | 558 | static const AIOCBInfo rbd_aiocb_info = { |
f27aaf4b | 559 | .aiocb_size = sizeof(RBDAIOCB), |
ad32e9c0 | 560 | .cancel = qemu_rbd_aio_cancel, |
f27aaf4b CB |
561 | }; |
562 | ||
e04fb07f | 563 | static void rbd_finish_bh(void *opaque) |
f27aaf4b | 564 | { |
e04fb07f SH |
565 | RADOSCB *rcb = opaque; |
566 | qemu_bh_delete(rcb->acb->bh); | |
567 | qemu_rbd_complete_aio(rcb); | |
ad32e9c0 JD |
568 | } |
569 | ||
570 | /* | |
571 | * This is the callback function for rbd_aio_read and _write | |
572 | * | |
573 | * Note: this function is being called from a non qemu thread so | |
574 | * we need to be careful about what we do here. Generally we only | |
e04fb07f SH |
575 | * schedule a BH, and do the rest of the io completion handling |
576 | * from rbd_finish_bh() which runs in a qemu context. | |
ad32e9c0 JD |
577 | */ |
578 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) | |
579 | { | |
e04fb07f SH |
580 | RBDAIOCB *acb = rcb->acb; |
581 | ||
ad32e9c0 JD |
582 | rcb->ret = rbd_aio_get_return_value(c); |
583 | rbd_aio_release(c); | |
f27aaf4b | 584 | |
ea800191 SH |
585 | acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), |
586 | rbd_finish_bh, rcb); | |
e04fb07f | 587 | qemu_bh_schedule(acb->bh); |
f27aaf4b CB |
588 | } |
589 | ||
787f3133 JD |
590 | static int rbd_aio_discard_wrapper(rbd_image_t image, |
591 | uint64_t off, | |
592 | uint64_t len, | |
593 | rbd_completion_t comp) | |
594 | { | |
595 | #ifdef LIBRBD_SUPPORTS_DISCARD | |
596 | return rbd_aio_discard(image, off, len, comp); | |
597 | #else | |
598 | return -ENOTSUP; | |
599 | #endif | |
600 | } | |
601 | ||
dc7588c1 JD |
602 | static int rbd_aio_flush_wrapper(rbd_image_t image, |
603 | rbd_completion_t comp) | |
604 | { | |
605 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
606 | return rbd_aio_flush(image, comp); | |
607 | #else | |
608 | return -ENOTSUP; | |
609 | #endif | |
610 | } | |
611 | ||
787f3133 JD |
612 | static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs, |
613 | int64_t sector_num, | |
614 | QEMUIOVector *qiov, | |
615 | int nb_sectors, | |
616 | BlockDriverCompletionFunc *cb, | |
617 | void *opaque, | |
618 | RBDAIOCmd cmd) | |
f27aaf4b CB |
619 | { |
620 | RBDAIOCB *acb; | |
0f7a0237 | 621 | RADOSCB *rcb = NULL; |
ad32e9c0 | 622 | rbd_completion_t c; |
f27aaf4b CB |
623 | int64_t off, size; |
624 | char *buf; | |
51a13528 | 625 | int r; |
f27aaf4b CB |
626 | |
627 | BDRVRBDState *s = bs->opaque; | |
628 | ||
d7331bed | 629 | acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); |
787f3133 | 630 | acb->cmd = cmd; |
f27aaf4b | 631 | acb->qiov = qiov; |
dc7588c1 | 632 | if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { |
787f3133 JD |
633 | acb->bounce = NULL; |
634 | } else { | |
0f7a0237 KW |
635 | acb->bounce = qemu_try_blockalign(bs, qiov->size); |
636 | if (acb->bounce == NULL) { | |
637 | goto failed; | |
638 | } | |
787f3133 | 639 | } |
f27aaf4b CB |
640 | acb->ret = 0; |
641 | acb->error = 0; | |
642 | acb->s = s; | |
643 | acb->cancelled = 0; | |
644 | acb->bh = NULL; | |
473c7f02 | 645 | acb->status = -EINPROGRESS; |
f27aaf4b | 646 | |
787f3133 | 647 | if (cmd == RBD_AIO_WRITE) { |
d5e6b161 | 648 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); |
f27aaf4b CB |
649 | } |
650 | ||
651 | buf = acb->bounce; | |
652 | ||
653 | off = sector_num * BDRV_SECTOR_SIZE; | |
654 | size = nb_sectors * BDRV_SECTOR_SIZE; | |
f27aaf4b | 655 | |
5839e53b | 656 | rcb = g_new(RADOSCB, 1); |
ad32e9c0 JD |
657 | rcb->done = 0; |
658 | rcb->acb = acb; | |
659 | rcb->buf = buf; | |
660 | rcb->s = acb->s; | |
661 | rcb->size = size; | |
51a13528 JD |
662 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
663 | if (r < 0) { | |
664 | goto failed; | |
665 | } | |
f27aaf4b | 666 | |
787f3133 JD |
667 | switch (cmd) { |
668 | case RBD_AIO_WRITE: | |
51a13528 | 669 | r = rbd_aio_write(s->image, off, size, buf, c); |
787f3133 JD |
670 | break; |
671 | case RBD_AIO_READ: | |
51a13528 | 672 | r = rbd_aio_read(s->image, off, size, buf, c); |
787f3133 JD |
673 | break; |
674 | case RBD_AIO_DISCARD: | |
675 | r = rbd_aio_discard_wrapper(s->image, off, size, c); | |
676 | break; | |
dc7588c1 JD |
677 | case RBD_AIO_FLUSH: |
678 | r = rbd_aio_flush_wrapper(s->image, c); | |
679 | break; | |
787f3133 JD |
680 | default: |
681 | r = -EINVAL; | |
51a13528 JD |
682 | } |
683 | ||
684 | if (r < 0) { | |
405a2764 | 685 | goto failed_completion; |
f27aaf4b CB |
686 | } |
687 | ||
688 | return &acb->common; | |
51a13528 | 689 | |
405a2764 KW |
690 | failed_completion: |
691 | rbd_aio_release(c); | |
51a13528 | 692 | failed: |
7267c094 | 693 | g_free(rcb); |
405a2764 | 694 | qemu_vfree(acb->bounce); |
51a13528 JD |
695 | qemu_aio_release(acb); |
696 | return NULL; | |
f27aaf4b CB |
697 | } |
698 | ||
ad32e9c0 JD |
699 | static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, |
700 | int64_t sector_num, | |
701 | QEMUIOVector *qiov, | |
702 | int nb_sectors, | |
703 | BlockDriverCompletionFunc *cb, | |
704 | void *opaque) | |
f27aaf4b | 705 | { |
787f3133 JD |
706 | return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, |
707 | RBD_AIO_READ); | |
f27aaf4b CB |
708 | } |
709 | ||
ad32e9c0 JD |
710 | static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, |
711 | int64_t sector_num, | |
712 | QEMUIOVector *qiov, | |
713 | int nb_sectors, | |
714 | BlockDriverCompletionFunc *cb, | |
715 | void *opaque) | |
f27aaf4b | 716 | { |
787f3133 JD |
717 | return rbd_start_aio(bs, sector_num, qiov, nb_sectors, cb, opaque, |
718 | RBD_AIO_WRITE); | |
f27aaf4b CB |
719 | } |
720 | ||
dc7588c1 JD |
721 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH |
722 | static BlockDriverAIOCB *qemu_rbd_aio_flush(BlockDriverState *bs, | |
723 | BlockDriverCompletionFunc *cb, | |
724 | void *opaque) | |
725 | { | |
726 | return rbd_start_aio(bs, 0, NULL, 0, cb, opaque, RBD_AIO_FLUSH); | |
727 | } | |
728 | ||
729 | #else | |
730 | ||
8b94ff85 | 731 | static int qemu_rbd_co_flush(BlockDriverState *bs) |
7a3f5fe9 SW |
732 | { |
733 | #if LIBRBD_VERSION_CODE >= LIBRBD_VERSION(0, 1, 1) | |
734 | /* rbd_flush added in 0.1.1 */ | |
735 | BDRVRBDState *s = bs->opaque; | |
736 | return rbd_flush(s->image); | |
737 | #else | |
738 | return 0; | |
739 | #endif | |
740 | } | |
dc7588c1 | 741 | #endif |
7a3f5fe9 | 742 | |
ad32e9c0 | 743 | static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) |
f27aaf4b CB |
744 | { |
745 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
746 | rbd_image_info_t info; |
747 | int r; | |
748 | ||
749 | r = rbd_stat(s->image, &info, sizeof(info)); | |
750 | if (r < 0) { | |
751 | return r; | |
752 | } | |
753 | ||
754 | bdi->cluster_size = info.obj_size; | |
f27aaf4b CB |
755 | return 0; |
756 | } | |
757 | ||
ad32e9c0 | 758 | static int64_t qemu_rbd_getlength(BlockDriverState *bs) |
f27aaf4b CB |
759 | { |
760 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
761 | rbd_image_info_t info; |
762 | int r; | |
f27aaf4b | 763 | |
ad32e9c0 JD |
764 | r = rbd_stat(s->image, &info, sizeof(info)); |
765 | if (r < 0) { | |
766 | return r; | |
767 | } | |
768 | ||
769 | return info.size; | |
f27aaf4b CB |
770 | } |
771 | ||
30cdc48c JD |
772 | static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) |
773 | { | |
774 | BDRVRBDState *s = bs->opaque; | |
775 | int r; | |
776 | ||
777 | r = rbd_resize(s->image, offset); | |
778 | if (r < 0) { | |
779 | return r; | |
780 | } | |
781 | ||
782 | return 0; | |
783 | } | |
784 | ||
ad32e9c0 JD |
785 | static int qemu_rbd_snap_create(BlockDriverState *bs, |
786 | QEMUSnapshotInfo *sn_info) | |
f27aaf4b CB |
787 | { |
788 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 789 | int r; |
f27aaf4b CB |
790 | |
791 | if (sn_info->name[0] == '\0') { | |
792 | return -EINVAL; /* we need a name for rbd snapshots */ | |
793 | } | |
794 | ||
795 | /* | |
796 | * rbd snapshots are using the name as the user controlled unique identifier | |
797 | * we can't use the rbd snapid for that purpose, as it can't be set | |
798 | */ | |
799 | if (sn_info->id_str[0] != '\0' && | |
800 | strcmp(sn_info->id_str, sn_info->name) != 0) { | |
801 | return -EINVAL; | |
802 | } | |
803 | ||
804 | if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { | |
805 | return -ERANGE; | |
806 | } | |
807 | ||
ad32e9c0 | 808 | r = rbd_snap_create(s->image, sn_info->name); |
f27aaf4b | 809 | if (r < 0) { |
ad32e9c0 | 810 | error_report("failed to create snap: %s", strerror(-r)); |
f27aaf4b CB |
811 | return r; |
812 | } | |
813 | ||
f27aaf4b CB |
814 | return 0; |
815 | } | |
816 | ||
bd603247 | 817 | static int qemu_rbd_snap_remove(BlockDriverState *bs, |
a89d89d3 WX |
818 | const char *snapshot_id, |
819 | const char *snapshot_name, | |
820 | Error **errp) | |
bd603247 GF |
821 | { |
822 | BDRVRBDState *s = bs->opaque; | |
823 | int r; | |
824 | ||
a89d89d3 WX |
825 | if (!snapshot_name) { |
826 | error_setg(errp, "rbd need a valid snapshot name"); | |
827 | return -EINVAL; | |
828 | } | |
829 | ||
830 | /* If snapshot_id is specified, it must be equal to name, see | |
831 | qemu_rbd_snap_list() */ | |
832 | if (snapshot_id && strcmp(snapshot_id, snapshot_name)) { | |
833 | error_setg(errp, | |
834 | "rbd do not support snapshot id, it should be NULL or " | |
835 | "equal to snapshot name"); | |
836 | return -EINVAL; | |
837 | } | |
838 | ||
bd603247 | 839 | r = rbd_snap_remove(s->image, snapshot_name); |
a89d89d3 WX |
840 | if (r < 0) { |
841 | error_setg_errno(errp, -r, "Failed to remove the snapshot"); | |
842 | } | |
bd603247 GF |
843 | return r; |
844 | } | |
845 | ||
846 | static int qemu_rbd_snap_rollback(BlockDriverState *bs, | |
847 | const char *snapshot_name) | |
848 | { | |
849 | BDRVRBDState *s = bs->opaque; | |
850 | int r; | |
851 | ||
852 | r = rbd_snap_rollback(s->image, snapshot_name); | |
853 | return r; | |
854 | } | |
855 | ||
ad32e9c0 JD |
856 | static int qemu_rbd_snap_list(BlockDriverState *bs, |
857 | QEMUSnapshotInfo **psn_tab) | |
f27aaf4b CB |
858 | { |
859 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 860 | QEMUSnapshotInfo *sn_info, *sn_tab = NULL; |
ad32e9c0 JD |
861 | int i, snap_count; |
862 | rbd_snap_info_t *snaps; | |
863 | int max_snaps = RBD_MAX_SNAPS; | |
f27aaf4b | 864 | |
ad32e9c0 | 865 | do { |
02c4f26b | 866 | snaps = g_new(rbd_snap_info_t, max_snaps); |
ad32e9c0 | 867 | snap_count = rbd_snap_list(s->image, snaps, &max_snaps); |
9e6337d0 | 868 | if (snap_count <= 0) { |
7267c094 | 869 | g_free(snaps); |
f27aaf4b | 870 | } |
ad32e9c0 | 871 | } while (snap_count == -ERANGE); |
f27aaf4b | 872 | |
ad32e9c0 | 873 | if (snap_count <= 0) { |
b9c53290 | 874 | goto done; |
f27aaf4b CB |
875 | } |
876 | ||
5839e53b | 877 | sn_tab = g_new0(QEMUSnapshotInfo, snap_count); |
f27aaf4b | 878 | |
ad32e9c0 JD |
879 | for (i = 0; i < snap_count; i++) { |
880 | const char *snap_name = snaps[i].name; | |
f27aaf4b CB |
881 | |
882 | sn_info = sn_tab + i; | |
883 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); | |
884 | pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); | |
f27aaf4b | 885 | |
ad32e9c0 | 886 | sn_info->vm_state_size = snaps[i].size; |
f27aaf4b CB |
887 | sn_info->date_sec = 0; |
888 | sn_info->date_nsec = 0; | |
889 | sn_info->vm_clock_nsec = 0; | |
890 | } | |
ad32e9c0 | 891 | rbd_snap_list_end(snaps); |
9e6337d0 | 892 | g_free(snaps); |
ad32e9c0 | 893 | |
b9c53290 | 894 | done: |
f27aaf4b | 895 | *psn_tab = sn_tab; |
f27aaf4b | 896 | return snap_count; |
f27aaf4b CB |
897 | } |
898 | ||
787f3133 JD |
899 | #ifdef LIBRBD_SUPPORTS_DISCARD |
900 | static BlockDriverAIOCB* qemu_rbd_aio_discard(BlockDriverState *bs, | |
901 | int64_t sector_num, | |
902 | int nb_sectors, | |
903 | BlockDriverCompletionFunc *cb, | |
904 | void *opaque) | |
905 | { | |
906 | return rbd_start_aio(bs, sector_num, NULL, nb_sectors, cb, opaque, | |
907 | RBD_AIO_DISCARD); | |
908 | } | |
909 | #endif | |
910 | ||
bd0cf596 CL |
911 | static QemuOptsList qemu_rbd_create_opts = { |
912 | .name = "rbd-create-opts", | |
913 | .head = QTAILQ_HEAD_INITIALIZER(qemu_rbd_create_opts.head), | |
914 | .desc = { | |
915 | { | |
916 | .name = BLOCK_OPT_SIZE, | |
917 | .type = QEMU_OPT_SIZE, | |
918 | .help = "Virtual disk size" | |
919 | }, | |
920 | { | |
921 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
922 | .type = QEMU_OPT_SIZE, | |
923 | .help = "RBD object size" | |
924 | }, | |
925 | { /* end of list */ } | |
926 | } | |
f27aaf4b CB |
927 | }; |
928 | ||
929 | static BlockDriver bdrv_rbd = { | |
930 | .format_name = "rbd", | |
931 | .instance_size = sizeof(BDRVRBDState), | |
030be321 | 932 | .bdrv_needs_filename = true, |
ad32e9c0 JD |
933 | .bdrv_file_open = qemu_rbd_open, |
934 | .bdrv_close = qemu_rbd_close, | |
c282e1fd | 935 | .bdrv_create = qemu_rbd_create, |
3ac21627 | 936 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
ad32e9c0 | 937 | .bdrv_get_info = qemu_rbd_getinfo, |
bd0cf596 | 938 | .create_opts = &qemu_rbd_create_opts, |
ad32e9c0 | 939 | .bdrv_getlength = qemu_rbd_getlength, |
30cdc48c | 940 | .bdrv_truncate = qemu_rbd_truncate, |
f27aaf4b CB |
941 | .protocol_name = "rbd", |
942 | ||
c68b89ac KW |
943 | .bdrv_aio_readv = qemu_rbd_aio_readv, |
944 | .bdrv_aio_writev = qemu_rbd_aio_writev, | |
dc7588c1 JD |
945 | |
946 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
947 | .bdrv_aio_flush = qemu_rbd_aio_flush, | |
948 | #else | |
c68b89ac | 949 | .bdrv_co_flush_to_disk = qemu_rbd_co_flush, |
dc7588c1 | 950 | #endif |
f27aaf4b | 951 | |
787f3133 JD |
952 | #ifdef LIBRBD_SUPPORTS_DISCARD |
953 | .bdrv_aio_discard = qemu_rbd_aio_discard, | |
954 | #endif | |
955 | ||
c68b89ac | 956 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
bd603247 | 957 | .bdrv_snapshot_delete = qemu_rbd_snap_remove, |
c68b89ac | 958 | .bdrv_snapshot_list = qemu_rbd_snap_list, |
bd603247 | 959 | .bdrv_snapshot_goto = qemu_rbd_snap_rollback, |
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); |