]>
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 | ||
d5124c00 HR |
292 | static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options, |
293 | Error **errp) | |
f27aaf4b | 294 | { |
d61563b2 | 295 | Error *local_err = NULL; |
f27aaf4b CB |
296 | int64_t bytes = 0; |
297 | int64_t objsize; | |
ad32e9c0 JD |
298 | int obj_order = 0; |
299 | char pool[RBD_MAX_POOL_NAME_SIZE]; | |
300 | char name[RBD_MAX_IMAGE_NAME_SIZE]; | |
301 | char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; | |
fab5cf59 | 302 | char conf[RBD_MAX_CONF_SIZE]; |
7c7e9df0 SW |
303 | char clientname_buf[RBD_MAX_CONF_SIZE]; |
304 | char *clientname; | |
ad32e9c0 JD |
305 | rados_t cluster; |
306 | rados_ioctx_t io_ctx; | |
f27aaf4b CB |
307 | int ret; |
308 | ||
ad32e9c0 JD |
309 | if (qemu_rbd_parsename(filename, pool, sizeof(pool), |
310 | snap_buf, sizeof(snap_buf), | |
fab5cf59 | 311 | name, sizeof(name), |
d61563b2 MA |
312 | conf, sizeof(conf), &local_err) < 0) { |
313 | error_propagate(errp, local_err); | |
f27aaf4b CB |
314 | return -EINVAL; |
315 | } | |
f27aaf4b | 316 | |
f27aaf4b CB |
317 | /* Read out options */ |
318 | while (options && options->name) { | |
319 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
320 | bytes = options->value.n; | |
321 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { | |
322 | if (options->value.n) { | |
323 | objsize = options->value.n; | |
324 | if ((objsize - 1) & objsize) { /* not a power of 2? */ | |
d61563b2 | 325 | error_setg(errp, "obj size needs to be power of 2"); |
f27aaf4b CB |
326 | return -EINVAL; |
327 | } | |
328 | if (objsize < 4096) { | |
d61563b2 | 329 | error_setg(errp, "obj size too small"); |
f27aaf4b CB |
330 | return -EINVAL; |
331 | } | |
ad32e9c0 | 332 | obj_order = ffs(objsize) - 1; |
f27aaf4b CB |
333 | } |
334 | } | |
335 | options++; | |
336 | } | |
337 | ||
7c7e9df0 SW |
338 | clientname = qemu_rbd_parse_clientname(conf, clientname_buf); |
339 | if (rados_create(&cluster, clientname) < 0) { | |
d61563b2 | 340 | error_setg(errp, "error initializing"); |
f27aaf4b CB |
341 | return -EIO; |
342 | } | |
343 | ||
fab5cf59 | 344 | if (strstr(conf, "conf=") == NULL) { |
f9fe18ec SW |
345 | /* try default location, but ignore failure */ |
346 | rados_conf_read_file(cluster, NULL); | |
fab5cf59 JD |
347 | } |
348 | ||
349 | if (conf[0] != '\0' && | |
d61563b2 | 350 | qemu_rbd_set_conf(cluster, conf, &local_err) < 0) { |
ad32e9c0 | 351 | rados_shutdown(cluster); |
d61563b2 | 352 | error_propagate(errp, local_err); |
f27aaf4b CB |
353 | return -EIO; |
354 | } | |
355 | ||
ad32e9c0 | 356 | if (rados_connect(cluster) < 0) { |
d61563b2 | 357 | error_setg(errp, "error connecting"); |
ad32e9c0 | 358 | rados_shutdown(cluster); |
f27aaf4b CB |
359 | return -EIO; |
360 | } | |
f27aaf4b | 361 | |
ad32e9c0 | 362 | if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) { |
d61563b2 | 363 | error_setg(errp, "error opening pool %s", pool); |
ad32e9c0 JD |
364 | rados_shutdown(cluster); |
365 | return -EIO; | |
f27aaf4b CB |
366 | } |
367 | ||
ad32e9c0 JD |
368 | ret = rbd_create(io_ctx, name, bytes, &obj_order); |
369 | rados_ioctx_destroy(io_ctx); | |
370 | rados_shutdown(cluster); | |
f27aaf4b CB |
371 | |
372 | return ret; | |
373 | } | |
374 | ||
375 | /* | |
e04fb07f SH |
376 | * This aio completion is being called from rbd_finish_bh() and runs in qemu |
377 | * BH context. | |
f27aaf4b | 378 | */ |
ad32e9c0 | 379 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
f27aaf4b CB |
380 | { |
381 | RBDAIOCB *acb = rcb->acb; | |
382 | int64_t r; | |
383 | ||
f27aaf4b CB |
384 | r = rcb->ret; |
385 | ||
dc7588c1 | 386 | if (acb->cmd != RBD_AIO_READ) { |
f27aaf4b CB |
387 | if (r < 0) { |
388 | acb->ret = r; | |
389 | acb->error = 1; | |
390 | } else if (!acb->error) { | |
ad32e9c0 | 391 | acb->ret = rcb->size; |
f27aaf4b CB |
392 | } |
393 | } else { | |
ad32e9c0 JD |
394 | if (r < 0) { |
395 | memset(rcb->buf, 0, rcb->size); | |
f27aaf4b CB |
396 | acb->ret = r; |
397 | acb->error = 1; | |
ad32e9c0 JD |
398 | } else if (r < rcb->size) { |
399 | memset(rcb->buf + r, 0, rcb->size - r); | |
f27aaf4b | 400 | if (!acb->error) { |
ad32e9c0 | 401 | acb->ret = rcb->size; |
f27aaf4b CB |
402 | } |
403 | } else if (!acb->error) { | |
ad32e9c0 | 404 | acb->ret = r; |
f27aaf4b CB |
405 | } |
406 | } | |
f27aaf4b | 407 | |
e04fb07f | 408 | g_free(rcb); |
f27aaf4b | 409 | |
e04fb07f SH |
410 | if (acb->cmd == RBD_AIO_READ) { |
411 | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); | |
412 | } | |
413 | qemu_vfree(acb->bounce); | |
414 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); | |
415 | acb->status = 0; | |
f27aaf4b | 416 | |
e04fb07f SH |
417 | if (!acb->cancelled) { |
418 | qemu_aio_release(acb); | |
419 | } | |
f27aaf4b CB |
420 | } |
421 | ||
a9ccedc3 KW |
422 | /* TODO Convert to fine grained options */ |
423 | static QemuOptsList runtime_opts = { | |
424 | .name = "rbd", | |
425 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
426 | .desc = { | |
427 | { | |
428 | .name = "filename", | |
429 | .type = QEMU_OPT_STRING, | |
430 | .help = "Specification of the rbd image", | |
431 | }, | |
432 | { /* end of list */ } | |
433 | }, | |
434 | }; | |
435 | ||
015a1036 HR |
436 | static int qemu_rbd_open(BlockDriverState *bs, QDict *options, int flags, |
437 | Error **errp) | |
f27aaf4b CB |
438 | { |
439 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
440 | char pool[RBD_MAX_POOL_NAME_SIZE]; |
441 | char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; | |
fab5cf59 | 442 | char conf[RBD_MAX_CONF_SIZE]; |
7c7e9df0 SW |
443 | char clientname_buf[RBD_MAX_CONF_SIZE]; |
444 | char *clientname; | |
a9ccedc3 KW |
445 | QemuOpts *opts; |
446 | Error *local_err = NULL; | |
447 | const char *filename; | |
f27aaf4b CB |
448 | int r; |
449 | ||
87ea75d5 | 450 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
a9ccedc3 | 451 | qemu_opts_absorb_qdict(opts, options, &local_err); |
84d18f06 | 452 | if (local_err) { |
d61563b2 | 453 | error_propagate(errp, local_err); |
a9ccedc3 KW |
454 | qemu_opts_del(opts); |
455 | return -EINVAL; | |
456 | } | |
457 | ||
458 | filename = qemu_opt_get(opts, "filename"); | |
a9ccedc3 | 459 | |
ad32e9c0 JD |
460 | if (qemu_rbd_parsename(filename, pool, sizeof(pool), |
461 | snap_buf, sizeof(snap_buf), | |
fab5cf59 | 462 | s->name, sizeof(s->name), |
d61563b2 | 463 | conf, sizeof(conf), errp) < 0) { |
c3ca988d KW |
464 | r = -EINVAL; |
465 | goto failed_opts; | |
f27aaf4b | 466 | } |
f27aaf4b | 467 | |
7c7e9df0 SW |
468 | clientname = qemu_rbd_parse_clientname(conf, clientname_buf); |
469 | r = rados_create(&s->cluster, clientname); | |
ad32e9c0 | 470 | if (r < 0) { |
d61563b2 | 471 | error_setg(&local_err, "error initializing"); |
c3ca988d | 472 | goto failed_opts; |
f27aaf4b CB |
473 | } |
474 | ||
eb93d5d9 SW |
475 | s->snap = NULL; |
476 | if (snap_buf[0] != '\0') { | |
477 | s->snap = g_strdup(snap_buf); | |
478 | } | |
479 | ||
b11f38fc JD |
480 | /* |
481 | * Fallback to more conservative semantics if setting cache | |
482 | * options fails. Ignore errors from setting rbd_cache because the | |
483 | * only possible error is that the option does not exist, and | |
484 | * librbd defaults to no caching. If write through caching cannot | |
485 | * be set up, fall back to no caching. | |
486 | */ | |
487 | if (flags & BDRV_O_NOCACHE) { | |
488 | rados_conf_set(s->cluster, "rbd_cache", "false"); | |
489 | } else { | |
490 | rados_conf_set(s->cluster, "rbd_cache", "true"); | |
b11f38fc JD |
491 | } |
492 | ||
fab5cf59 | 493 | if (strstr(conf, "conf=") == NULL) { |
f9fe18ec SW |
494 | /* try default location, but ignore failure */ |
495 | rados_conf_read_file(s->cluster, NULL); | |
fab5cf59 JD |
496 | } |
497 | ||
498 | if (conf[0] != '\0') { | |
d61563b2 | 499 | r = qemu_rbd_set_conf(s->cluster, conf, errp); |
fab5cf59 | 500 | if (r < 0) { |
eb93d5d9 | 501 | goto failed_shutdown; |
fab5cf59 | 502 | } |
f27aaf4b CB |
503 | } |
504 | ||
ad32e9c0 JD |
505 | r = rados_connect(s->cluster); |
506 | if (r < 0) { | |
d61563b2 | 507 | error_setg(&local_err, "error connecting"); |
eb93d5d9 | 508 | goto failed_shutdown; |
f27aaf4b CB |
509 | } |
510 | ||
ad32e9c0 JD |
511 | r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); |
512 | if (r < 0) { | |
d61563b2 | 513 | error_setg(&local_err, "error opening pool %s", pool); |
eb93d5d9 | 514 | goto failed_shutdown; |
f27aaf4b CB |
515 | } |
516 | ||
ad32e9c0 | 517 | r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); |
f27aaf4b | 518 | if (r < 0) { |
d61563b2 | 519 | error_setg(&local_err, "error reading header from %s", s->name); |
eb93d5d9 | 520 | goto failed_open; |
f27aaf4b CB |
521 | } |
522 | ||
ad32e9c0 | 523 | bs->read_only = (s->snap != NULL); |
f27aaf4b | 524 | |
c3ca988d | 525 | qemu_opts_del(opts); |
f27aaf4b CB |
526 | return 0; |
527 | ||
eb93d5d9 | 528 | failed_open: |
ad32e9c0 | 529 | rados_ioctx_destroy(s->io_ctx); |
eb93d5d9 | 530 | failed_shutdown: |
ad32e9c0 | 531 | rados_shutdown(s->cluster); |
eb93d5d9 | 532 | g_free(s->snap); |
c3ca988d KW |
533 | failed_opts: |
534 | qemu_opts_del(opts); | |
f27aaf4b CB |
535 | return r; |
536 | } | |
537 | ||
ad32e9c0 | 538 | static void qemu_rbd_close(BlockDriverState *bs) |
f27aaf4b CB |
539 | { |
540 | BDRVRBDState *s = bs->opaque; | |
541 | ||
ad32e9c0 JD |
542 | rbd_close(s->image); |
543 | rados_ioctx_destroy(s->io_ctx); | |
7267c094 | 544 | g_free(s->snap); |
ad32e9c0 | 545 | rados_shutdown(s->cluster); |
f27aaf4b CB |
546 | } |
547 | ||
548 | /* | |
549 | * Cancel aio. Since we don't reference acb in a non qemu threads, | |
550 | * it is safe to access it here. | |
551 | */ | |
ad32e9c0 | 552 | static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb) |
f27aaf4b CB |
553 | { |
554 | RBDAIOCB *acb = (RBDAIOCB *) blockacb; | |
555 | acb->cancelled = 1; | |
473c7f02 SP |
556 | |
557 | while (acb->status == -EINPROGRESS) { | |
ea800191 | 558 | aio_poll(bdrv_get_aio_context(acb->common.bs), true); |
473c7f02 SP |
559 | } |
560 | ||
561 | qemu_aio_release(acb); | |
f27aaf4b CB |
562 | } |
563 | ||
d7331bed | 564 | static const AIOCBInfo rbd_aiocb_info = { |
f27aaf4b | 565 | .aiocb_size = sizeof(RBDAIOCB), |
ad32e9c0 | 566 | .cancel = qemu_rbd_aio_cancel, |
f27aaf4b CB |
567 | }; |
568 | ||
e04fb07f | 569 | static void rbd_finish_bh(void *opaque) |
f27aaf4b | 570 | { |
e04fb07f SH |
571 | RADOSCB *rcb = opaque; |
572 | qemu_bh_delete(rcb->acb->bh); | |
573 | qemu_rbd_complete_aio(rcb); | |
ad32e9c0 JD |
574 | } |
575 | ||
576 | /* | |
577 | * This is the callback function for rbd_aio_read and _write | |
578 | * | |
579 | * Note: this function is being called from a non qemu thread so | |
580 | * we need to be careful about what we do here. Generally we only | |
e04fb07f SH |
581 | * schedule a BH, and do the rest of the io completion handling |
582 | * from rbd_finish_bh() which runs in a qemu context. | |
ad32e9c0 JD |
583 | */ |
584 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) | |
585 | { | |
e04fb07f SH |
586 | RBDAIOCB *acb = rcb->acb; |
587 | ||
ad32e9c0 JD |
588 | rcb->ret = rbd_aio_get_return_value(c); |
589 | rbd_aio_release(c); | |
f27aaf4b | 590 | |
ea800191 SH |
591 | acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs), |
592 | rbd_finish_bh, rcb); | |
e04fb07f | 593 | qemu_bh_schedule(acb->bh); |
f27aaf4b CB |
594 | } |
595 | ||
787f3133 JD |
596 | static int rbd_aio_discard_wrapper(rbd_image_t image, |
597 | uint64_t off, | |
598 | uint64_t len, | |
599 | rbd_completion_t comp) | |
600 | { | |
601 | #ifdef LIBRBD_SUPPORTS_DISCARD | |
602 | return rbd_aio_discard(image, off, len, comp); | |
603 | #else | |
604 | return -ENOTSUP; | |
605 | #endif | |
606 | } | |
607 | ||
dc7588c1 JD |
608 | static int rbd_aio_flush_wrapper(rbd_image_t image, |
609 | rbd_completion_t comp) | |
610 | { | |
611 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
612 | return rbd_aio_flush(image, comp); | |
613 | #else | |
614 | return -ENOTSUP; | |
615 | #endif | |
616 | } | |
617 | ||
787f3133 JD |
618 | static BlockDriverAIOCB *rbd_start_aio(BlockDriverState *bs, |
619 | int64_t sector_num, | |
620 | QEMUIOVector *qiov, | |
621 | int nb_sectors, | |
622 | BlockDriverCompletionFunc *cb, | |
623 | void *opaque, | |
624 | RBDAIOCmd cmd) | |
f27aaf4b CB |
625 | { |
626 | RBDAIOCB *acb; | |
627 | RADOSCB *rcb; | |
ad32e9c0 | 628 | rbd_completion_t c; |
f27aaf4b CB |
629 | int64_t off, size; |
630 | char *buf; | |
51a13528 | 631 | int r; |
f27aaf4b CB |
632 | |
633 | BDRVRBDState *s = bs->opaque; | |
634 | ||
d7331bed | 635 | acb = qemu_aio_get(&rbd_aiocb_info, bs, cb, opaque); |
787f3133 | 636 | acb->cmd = cmd; |
f27aaf4b | 637 | acb->qiov = qiov; |
dc7588c1 | 638 | if (cmd == RBD_AIO_DISCARD || cmd == RBD_AIO_FLUSH) { |
787f3133 JD |
639 | acb->bounce = NULL; |
640 | } else { | |
641 | acb->bounce = qemu_blockalign(bs, qiov->size); | |
642 | } | |
f27aaf4b CB |
643 | acb->ret = 0; |
644 | acb->error = 0; | |
645 | acb->s = s; | |
646 | acb->cancelled = 0; | |
647 | acb->bh = NULL; | |
473c7f02 | 648 | acb->status = -EINPROGRESS; |
f27aaf4b | 649 | |
787f3133 | 650 | if (cmd == RBD_AIO_WRITE) { |
d5e6b161 | 651 | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); |
f27aaf4b CB |
652 | } |
653 | ||
654 | buf = acb->bounce; | |
655 | ||
656 | off = sector_num * BDRV_SECTOR_SIZE; | |
657 | size = nb_sectors * BDRV_SECTOR_SIZE; | |
f27aaf4b | 658 | |
7267c094 | 659 | rcb = g_malloc(sizeof(RADOSCB)); |
ad32e9c0 JD |
660 | rcb->done = 0; |
661 | rcb->acb = acb; | |
662 | rcb->buf = buf; | |
663 | rcb->s = acb->s; | |
664 | rcb->size = size; | |
51a13528 JD |
665 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
666 | if (r < 0) { | |
667 | goto failed; | |
668 | } | |
f27aaf4b | 669 | |
787f3133 JD |
670 | switch (cmd) { |
671 | case RBD_AIO_WRITE: | |
51a13528 | 672 | r = rbd_aio_write(s->image, off, size, buf, c); |
787f3133 JD |
673 | break; |
674 | case RBD_AIO_READ: | |
51a13528 | 675 | r = rbd_aio_read(s->image, off, size, buf, c); |
787f3133 JD |
676 | break; |
677 | case RBD_AIO_DISCARD: | |
678 | r = rbd_aio_discard_wrapper(s->image, off, size, c); | |
679 | break; | |
dc7588c1 JD |
680 | case RBD_AIO_FLUSH: |
681 | r = rbd_aio_flush_wrapper(s->image, c); | |
682 | break; | |
787f3133 JD |
683 | default: |
684 | r = -EINVAL; | |
51a13528 JD |
685 | } |
686 | ||
687 | if (r < 0) { | |
688 | goto failed; | |
f27aaf4b CB |
689 | } |
690 | ||
691 | return &acb->common; | |
51a13528 JD |
692 | |
693 | failed: | |
7267c094 | 694 | g_free(rcb); |
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 { |
7267c094 | 866 | snaps = g_malloc(sizeof(*snaps) * 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 | ||
7267c094 | 877 | sn_tab = g_malloc0(snap_count * sizeof(QEMUSnapshotInfo)); |
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 | ||
ad32e9c0 | 911 | static QEMUOptionParameter qemu_rbd_create_options[] = { |
f27aaf4b CB |
912 | { |
913 | .name = BLOCK_OPT_SIZE, | |
914 | .type = OPT_SIZE, | |
915 | .help = "Virtual disk size" | |
916 | }, | |
917 | { | |
918 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
919 | .type = OPT_SIZE, | |
920 | .help = "RBD object size" | |
921 | }, | |
922 | {NULL} | |
923 | }; | |
924 | ||
925 | static BlockDriver bdrv_rbd = { | |
926 | .format_name = "rbd", | |
927 | .instance_size = sizeof(BDRVRBDState), | |
030be321 | 928 | .bdrv_needs_filename = true, |
ad32e9c0 JD |
929 | .bdrv_file_open = qemu_rbd_open, |
930 | .bdrv_close = qemu_rbd_close, | |
931 | .bdrv_create = qemu_rbd_create, | |
3ac21627 | 932 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
ad32e9c0 JD |
933 | .bdrv_get_info = qemu_rbd_getinfo, |
934 | .create_options = qemu_rbd_create_options, | |
935 | .bdrv_getlength = qemu_rbd_getlength, | |
30cdc48c | 936 | .bdrv_truncate = qemu_rbd_truncate, |
f27aaf4b CB |
937 | .protocol_name = "rbd", |
938 | ||
c68b89ac KW |
939 | .bdrv_aio_readv = qemu_rbd_aio_readv, |
940 | .bdrv_aio_writev = qemu_rbd_aio_writev, | |
dc7588c1 JD |
941 | |
942 | #ifdef LIBRBD_SUPPORTS_AIO_FLUSH | |
943 | .bdrv_aio_flush = qemu_rbd_aio_flush, | |
944 | #else | |
c68b89ac | 945 | .bdrv_co_flush_to_disk = qemu_rbd_co_flush, |
dc7588c1 | 946 | #endif |
f27aaf4b | 947 | |
787f3133 JD |
948 | #ifdef LIBRBD_SUPPORTS_DISCARD |
949 | .bdrv_aio_discard = qemu_rbd_aio_discard, | |
950 | #endif | |
951 | ||
c68b89ac | 952 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
bd603247 | 953 | .bdrv_snapshot_delete = qemu_rbd_snap_remove, |
c68b89ac | 954 | .bdrv_snapshot_list = qemu_rbd_snap_list, |
bd603247 | 955 | .bdrv_snapshot_goto = qemu_rbd_snap_rollback, |
f27aaf4b CB |
956 | }; |
957 | ||
958 | static void bdrv_rbd_init(void) | |
959 | { | |
960 | bdrv_register(&bdrv_rbd); | |
961 | } | |
962 | ||
963 | block_init(bdrv_rbd_init); |