]>
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 | * | |
10 | */ | |
11 | ||
ad32e9c0 JD |
12 | #include <inttypes.h> |
13 | ||
f27aaf4b CB |
14 | #include "qemu-common.h" |
15 | #include "qemu-error.h" | |
16 | ||
f27aaf4b CB |
17 | #include "block_int.h" |
18 | ||
ad32e9c0 | 19 | #include <rbd/librbd.h> |
f27aaf4b CB |
20 | |
21 | ||
22 | ||
23 | /* | |
24 | * When specifying the image filename use: | |
25 | * | |
fab5cf59 | 26 | * rbd:poolname/devicename[@snapshotname][:option1=value1[:option2=value2...]] |
f27aaf4b CB |
27 | * |
28 | * poolname must be the name of an existing rados pool | |
29 | * | |
30 | * devicename is the basename for all objects used to | |
31 | * emulate the raw device. | |
32 | * | |
fab5cf59 JD |
33 | * Each option given is used to configure rados, and may be |
34 | * any Ceph option, or "conf". The "conf" option specifies | |
35 | * a Ceph configuration file to read. | |
36 | * | |
f27aaf4b CB |
37 | * Metadata information (image size, ...) is stored in an |
38 | * object with the name "devicename.rbd". | |
39 | * | |
40 | * The raw device is split into 4MB sized objects by default. | |
41 | * The sequencenumber is encoded in a 12 byte long hex-string, | |
42 | * and is attached to the devicename, separated by a dot. | |
43 | * e.g. "devicename.1234567890ab" | |
44 | * | |
45 | */ | |
46 | ||
47 | #define OBJ_MAX_SIZE (1UL << OBJ_DEFAULT_OBJ_ORDER) | |
48 | ||
ad32e9c0 JD |
49 | #define RBD_MAX_CONF_NAME_SIZE 128 |
50 | #define RBD_MAX_CONF_VAL_SIZE 512 | |
51 | #define RBD_MAX_CONF_SIZE 1024 | |
52 | #define RBD_MAX_POOL_NAME_SIZE 128 | |
53 | #define RBD_MAX_SNAP_NAME_SIZE 128 | |
54 | #define RBD_MAX_SNAPS 100 | |
55 | ||
f27aaf4b CB |
56 | typedef struct RBDAIOCB { |
57 | BlockDriverAIOCB common; | |
58 | QEMUBH *bh; | |
59 | int ret; | |
60 | QEMUIOVector *qiov; | |
61 | char *bounce; | |
62 | int write; | |
63 | int64_t sector_num; | |
f27aaf4b CB |
64 | int error; |
65 | struct BDRVRBDState *s; | |
66 | int cancelled; | |
67 | } RBDAIOCB; | |
68 | ||
69 | typedef struct RADOSCB { | |
70 | int rcbid; | |
71 | RBDAIOCB *acb; | |
72 | struct BDRVRBDState *s; | |
73 | int done; | |
ad32e9c0 | 74 | int64_t size; |
f27aaf4b CB |
75 | char *buf; |
76 | int ret; | |
77 | } RADOSCB; | |
78 | ||
79 | #define RBD_FD_READ 0 | |
80 | #define RBD_FD_WRITE 1 | |
81 | ||
82 | typedef struct BDRVRBDState { | |
83 | int fds[2]; | |
ad32e9c0 JD |
84 | rados_t cluster; |
85 | rados_ioctx_t io_ctx; | |
86 | rbd_image_t image; | |
87 | char name[RBD_MAX_IMAGE_NAME_SIZE]; | |
f27aaf4b | 88 | int qemu_aio_count; |
ad32e9c0 | 89 | char *snap; |
f27aaf4b CB |
90 | int event_reader_pos; |
91 | RADOSCB *event_rcb; | |
92 | } BDRVRBDState; | |
93 | ||
f27aaf4b CB |
94 | static void rbd_aio_bh_cb(void *opaque); |
95 | ||
ad32e9c0 JD |
96 | static int qemu_rbd_next_tok(char *dst, int dst_len, |
97 | char *src, char delim, | |
98 | const char *name, | |
99 | char **p) | |
f27aaf4b CB |
100 | { |
101 | int l; | |
102 | char *end; | |
103 | ||
104 | *p = NULL; | |
105 | ||
106 | if (delim != '\0') { | |
107 | end = strchr(src, delim); | |
108 | if (end) { | |
109 | *p = end + 1; | |
110 | *end = '\0'; | |
111 | } | |
112 | } | |
113 | l = strlen(src); | |
114 | if (l >= dst_len) { | |
115 | error_report("%s too long", name); | |
116 | return -EINVAL; | |
117 | } else if (l == 0) { | |
118 | error_report("%s too short", name); | |
119 | return -EINVAL; | |
120 | } | |
121 | ||
122 | pstrcpy(dst, dst_len, src); | |
123 | ||
124 | return 0; | |
125 | } | |
126 | ||
ad32e9c0 JD |
127 | static int qemu_rbd_parsename(const char *filename, |
128 | char *pool, int pool_len, | |
129 | char *snap, int snap_len, | |
fab5cf59 JD |
130 | char *name, int name_len, |
131 | char *conf, int conf_len) | |
f27aaf4b CB |
132 | { |
133 | const char *start; | |
134 | char *p, *buf; | |
135 | int ret; | |
136 | ||
137 | if (!strstart(filename, "rbd:", &start)) { | |
138 | return -EINVAL; | |
139 | } | |
140 | ||
141 | buf = qemu_strdup(start); | |
142 | p = buf; | |
fab5cf59 JD |
143 | *snap = '\0'; |
144 | *conf = '\0'; | |
f27aaf4b | 145 | |
ad32e9c0 | 146 | ret = qemu_rbd_next_tok(pool, pool_len, p, '/', "pool name", &p); |
f27aaf4b CB |
147 | if (ret < 0 || !p) { |
148 | ret = -EINVAL; | |
149 | goto done; | |
150 | } | |
fab5cf59 JD |
151 | |
152 | if (strchr(p, '@')) { | |
153 | ret = qemu_rbd_next_tok(name, name_len, p, '@', "object name", &p); | |
154 | if (ret < 0) { | |
155 | goto done; | |
156 | } | |
157 | ret = qemu_rbd_next_tok(snap, snap_len, p, ':', "snap name", &p); | |
158 | } else { | |
159 | ret = qemu_rbd_next_tok(name, name_len, p, ':', "object name", &p); | |
f27aaf4b | 160 | } |
fab5cf59 | 161 | if (ret < 0 || !p) { |
f27aaf4b CB |
162 | goto done; |
163 | } | |
164 | ||
fab5cf59 | 165 | ret = qemu_rbd_next_tok(conf, conf_len, p, '\0', "configuration", &p); |
f27aaf4b CB |
166 | |
167 | done: | |
168 | qemu_free(buf); | |
169 | return ret; | |
170 | } | |
171 | ||
fab5cf59 JD |
172 | static int qemu_rbd_set_conf(rados_t cluster, const char *conf) |
173 | { | |
174 | char *p, *buf; | |
175 | char name[RBD_MAX_CONF_NAME_SIZE]; | |
176 | char value[RBD_MAX_CONF_VAL_SIZE]; | |
177 | int ret = 0; | |
178 | ||
179 | buf = qemu_strdup(conf); | |
180 | p = buf; | |
181 | ||
182 | while (p) { | |
183 | ret = qemu_rbd_next_tok(name, sizeof(name), p, | |
184 | '=', "conf option name", &p); | |
185 | if (ret < 0) { | |
186 | break; | |
187 | } | |
188 | ||
189 | if (!p) { | |
190 | error_report("conf option %s has no value", name); | |
191 | ret = -EINVAL; | |
192 | break; | |
193 | } | |
194 | ||
195 | ret = qemu_rbd_next_tok(value, sizeof(value), p, | |
196 | ':', "conf option value", &p); | |
197 | if (ret < 0) { | |
198 | break; | |
199 | } | |
200 | ||
201 | if (strcmp(name, "conf")) { | |
202 | ret = rados_conf_set(cluster, name, value); | |
203 | if (ret < 0) { | |
204 | error_report("invalid conf option %s", name); | |
205 | ret = -EINVAL; | |
206 | break; | |
207 | } | |
208 | } else { | |
209 | ret = rados_conf_read_file(cluster, value); | |
210 | if (ret < 0) { | |
211 | error_report("error reading conf file %s", value); | |
212 | break; | |
213 | } | |
214 | } | |
215 | } | |
216 | ||
217 | qemu_free(buf); | |
218 | return ret; | |
219 | } | |
220 | ||
ad32e9c0 | 221 | static int qemu_rbd_create(const char *filename, QEMUOptionParameter *options) |
f27aaf4b CB |
222 | { |
223 | int64_t bytes = 0; | |
224 | int64_t objsize; | |
ad32e9c0 JD |
225 | int obj_order = 0; |
226 | char pool[RBD_MAX_POOL_NAME_SIZE]; | |
227 | char name[RBD_MAX_IMAGE_NAME_SIZE]; | |
228 | char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; | |
fab5cf59 | 229 | char conf[RBD_MAX_CONF_SIZE]; |
ad32e9c0 JD |
230 | rados_t cluster; |
231 | rados_ioctx_t io_ctx; | |
f27aaf4b CB |
232 | int ret; |
233 | ||
ad32e9c0 JD |
234 | if (qemu_rbd_parsename(filename, pool, sizeof(pool), |
235 | snap_buf, sizeof(snap_buf), | |
fab5cf59 JD |
236 | name, sizeof(name), |
237 | conf, sizeof(conf)) < 0) { | |
f27aaf4b CB |
238 | return -EINVAL; |
239 | } | |
f27aaf4b | 240 | |
f27aaf4b CB |
241 | /* Read out options */ |
242 | while (options && options->name) { | |
243 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
244 | bytes = options->value.n; | |
245 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { | |
246 | if (options->value.n) { | |
247 | objsize = options->value.n; | |
248 | if ((objsize - 1) & objsize) { /* not a power of 2? */ | |
249 | error_report("obj size needs to be power of 2"); | |
250 | return -EINVAL; | |
251 | } | |
252 | if (objsize < 4096) { | |
253 | error_report("obj size too small"); | |
254 | return -EINVAL; | |
255 | } | |
ad32e9c0 | 256 | obj_order = ffs(objsize) - 1; |
f27aaf4b CB |
257 | } |
258 | } | |
259 | options++; | |
260 | } | |
261 | ||
ad32e9c0 | 262 | if (rados_create(&cluster, NULL) < 0) { |
f27aaf4b CB |
263 | error_report("error initializing"); |
264 | return -EIO; | |
265 | } | |
266 | ||
fab5cf59 JD |
267 | if (strstr(conf, "conf=") == NULL) { |
268 | if (rados_conf_read_file(cluster, NULL) < 0) { | |
269 | error_report("error reading config file"); | |
270 | rados_shutdown(cluster); | |
271 | return -EIO; | |
272 | } | |
273 | } | |
274 | ||
275 | if (conf[0] != '\0' && | |
276 | qemu_rbd_set_conf(cluster, conf) < 0) { | |
277 | error_report("error setting config options"); | |
ad32e9c0 | 278 | rados_shutdown(cluster); |
f27aaf4b CB |
279 | return -EIO; |
280 | } | |
281 | ||
ad32e9c0 JD |
282 | if (rados_connect(cluster) < 0) { |
283 | error_report("error connecting"); | |
284 | rados_shutdown(cluster); | |
f27aaf4b CB |
285 | return -EIO; |
286 | } | |
f27aaf4b | 287 | |
ad32e9c0 JD |
288 | if (rados_ioctx_create(cluster, pool, &io_ctx) < 0) { |
289 | error_report("error opening pool %s", pool); | |
290 | rados_shutdown(cluster); | |
291 | return -EIO; | |
f27aaf4b CB |
292 | } |
293 | ||
ad32e9c0 JD |
294 | ret = rbd_create(io_ctx, name, bytes, &obj_order); |
295 | rados_ioctx_destroy(io_ctx); | |
296 | rados_shutdown(cluster); | |
f27aaf4b CB |
297 | |
298 | return ret; | |
299 | } | |
300 | ||
301 | /* | |
ad32e9c0 JD |
302 | * This aio completion is being called from qemu_rbd_aio_event_reader() |
303 | * and runs in qemu context. It schedules a bh, but just in case the aio | |
f27aaf4b CB |
304 | * was not cancelled before. |
305 | */ | |
ad32e9c0 | 306 | static void qemu_rbd_complete_aio(RADOSCB *rcb) |
f27aaf4b CB |
307 | { |
308 | RBDAIOCB *acb = rcb->acb; | |
309 | int64_t r; | |
310 | ||
f27aaf4b | 311 | if (acb->cancelled) { |
ad32e9c0 JD |
312 | qemu_vfree(acb->bounce); |
313 | qemu_aio_release(acb); | |
f27aaf4b CB |
314 | goto done; |
315 | } | |
316 | ||
317 | r = rcb->ret; | |
318 | ||
319 | if (acb->write) { | |
320 | if (r < 0) { | |
321 | acb->ret = r; | |
322 | acb->error = 1; | |
323 | } else if (!acb->error) { | |
ad32e9c0 | 324 | acb->ret = rcb->size; |
f27aaf4b CB |
325 | } |
326 | } else { | |
ad32e9c0 JD |
327 | if (r < 0) { |
328 | memset(rcb->buf, 0, rcb->size); | |
f27aaf4b CB |
329 | acb->ret = r; |
330 | acb->error = 1; | |
ad32e9c0 JD |
331 | } else if (r < rcb->size) { |
332 | memset(rcb->buf + r, 0, rcb->size - r); | |
f27aaf4b | 333 | if (!acb->error) { |
ad32e9c0 | 334 | acb->ret = rcb->size; |
f27aaf4b CB |
335 | } |
336 | } else if (!acb->error) { | |
ad32e9c0 | 337 | acb->ret = r; |
f27aaf4b CB |
338 | } |
339 | } | |
340 | /* Note that acb->bh can be NULL in case where the aio was cancelled */ | |
ad32e9c0 JD |
341 | acb->bh = qemu_bh_new(rbd_aio_bh_cb, acb); |
342 | qemu_bh_schedule(acb->bh); | |
f27aaf4b CB |
343 | done: |
344 | qemu_free(rcb); | |
345 | } | |
346 | ||
347 | /* | |
348 | * aio fd read handler. It runs in the qemu context and calls the | |
349 | * completion handling of completed rados aio operations. | |
350 | */ | |
ad32e9c0 | 351 | static void qemu_rbd_aio_event_reader(void *opaque) |
f27aaf4b CB |
352 | { |
353 | BDRVRBDState *s = opaque; | |
354 | ||
355 | ssize_t ret; | |
356 | ||
357 | do { | |
358 | char *p = (char *)&s->event_rcb; | |
359 | ||
360 | /* now read the rcb pointer that was sent from a non qemu thread */ | |
361 | if ((ret = read(s->fds[RBD_FD_READ], p + s->event_reader_pos, | |
362 | sizeof(s->event_rcb) - s->event_reader_pos)) > 0) { | |
363 | if (ret > 0) { | |
364 | s->event_reader_pos += ret; | |
365 | if (s->event_reader_pos == sizeof(s->event_rcb)) { | |
366 | s->event_reader_pos = 0; | |
ad32e9c0 JD |
367 | qemu_rbd_complete_aio(s->event_rcb); |
368 | s->qemu_aio_count--; | |
f27aaf4b CB |
369 | } |
370 | } | |
371 | } | |
372 | } while (ret < 0 && errno == EINTR); | |
373 | } | |
374 | ||
ad32e9c0 | 375 | static int qemu_rbd_aio_flush_cb(void *opaque) |
f27aaf4b CB |
376 | { |
377 | BDRVRBDState *s = opaque; | |
378 | ||
379 | return (s->qemu_aio_count > 0); | |
380 | } | |
381 | ||
ad32e9c0 | 382 | static int qemu_rbd_open(BlockDriverState *bs, const char *filename, int flags) |
f27aaf4b CB |
383 | { |
384 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
385 | char pool[RBD_MAX_POOL_NAME_SIZE]; |
386 | char snap_buf[RBD_MAX_SNAP_NAME_SIZE]; | |
fab5cf59 | 387 | char conf[RBD_MAX_CONF_SIZE]; |
f27aaf4b CB |
388 | int r; |
389 | ||
ad32e9c0 JD |
390 | if (qemu_rbd_parsename(filename, pool, sizeof(pool), |
391 | snap_buf, sizeof(snap_buf), | |
fab5cf59 JD |
392 | s->name, sizeof(s->name), |
393 | conf, sizeof(conf)) < 0) { | |
f27aaf4b CB |
394 | return -EINVAL; |
395 | } | |
ad32e9c0 | 396 | s->snap = NULL; |
f27aaf4b | 397 | if (snap_buf[0] != '\0') { |
ad32e9c0 | 398 | s->snap = qemu_strdup(snap_buf); |
f27aaf4b CB |
399 | } |
400 | ||
ad32e9c0 JD |
401 | r = rados_create(&s->cluster, NULL); |
402 | if (r < 0) { | |
f27aaf4b CB |
403 | error_report("error initializing"); |
404 | return r; | |
405 | } | |
406 | ||
fab5cf59 JD |
407 | if (strstr(conf, "conf=") == NULL) { |
408 | r = rados_conf_read_file(s->cluster, NULL); | |
409 | if (r < 0) { | |
410 | error_report("error reading config file"); | |
411 | rados_shutdown(s->cluster); | |
412 | return r; | |
413 | } | |
414 | } | |
415 | ||
416 | if (conf[0] != '\0') { | |
417 | r = qemu_rbd_set_conf(s->cluster, conf); | |
418 | if (r < 0) { | |
419 | error_report("error setting config options"); | |
420 | rados_shutdown(s->cluster); | |
421 | return r; | |
422 | } | |
f27aaf4b CB |
423 | } |
424 | ||
ad32e9c0 JD |
425 | r = rados_connect(s->cluster); |
426 | if (r < 0) { | |
427 | error_report("error connecting"); | |
428 | rados_shutdown(s->cluster); | |
f27aaf4b CB |
429 | return r; |
430 | } | |
431 | ||
ad32e9c0 JD |
432 | r = rados_ioctx_create(s->cluster, pool, &s->io_ctx); |
433 | if (r < 0) { | |
434 | error_report("error opening pool %s", pool); | |
435 | rados_shutdown(s->cluster); | |
436 | return r; | |
f27aaf4b CB |
437 | } |
438 | ||
ad32e9c0 | 439 | r = rbd_open(s->io_ctx, s->name, &s->image, s->snap); |
f27aaf4b | 440 | if (r < 0) { |
ad32e9c0 JD |
441 | error_report("error reading header from %s", s->name); |
442 | rados_ioctx_destroy(s->io_ctx); | |
443 | rados_shutdown(s->cluster); | |
444 | return r; | |
f27aaf4b CB |
445 | } |
446 | ||
ad32e9c0 | 447 | bs->read_only = (s->snap != NULL); |
f27aaf4b CB |
448 | |
449 | s->event_reader_pos = 0; | |
450 | r = qemu_pipe(s->fds); | |
451 | if (r < 0) { | |
452 | error_report("error opening eventfd"); | |
453 | goto failed; | |
454 | } | |
455 | fcntl(s->fds[0], F_SETFL, O_NONBLOCK); | |
456 | fcntl(s->fds[1], F_SETFL, O_NONBLOCK); | |
ad32e9c0 JD |
457 | qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], qemu_rbd_aio_event_reader, |
458 | NULL, qemu_rbd_aio_flush_cb, NULL, s); | |
f27aaf4b | 459 | |
f27aaf4b CB |
460 | |
461 | return 0; | |
462 | ||
463 | failed: | |
ad32e9c0 JD |
464 | rbd_close(s->image); |
465 | rados_ioctx_destroy(s->io_ctx); | |
466 | rados_shutdown(s->cluster); | |
f27aaf4b CB |
467 | return r; |
468 | } | |
469 | ||
ad32e9c0 | 470 | static void qemu_rbd_close(BlockDriverState *bs) |
f27aaf4b CB |
471 | { |
472 | BDRVRBDState *s = bs->opaque; | |
473 | ||
474 | close(s->fds[0]); | |
475 | close(s->fds[1]); | |
476 | qemu_aio_set_fd_handler(s->fds[RBD_FD_READ], NULL , NULL, NULL, NULL, | |
477 | NULL); | |
478 | ||
ad32e9c0 JD |
479 | rbd_close(s->image); |
480 | rados_ioctx_destroy(s->io_ctx); | |
481 | qemu_free(s->snap); | |
482 | rados_shutdown(s->cluster); | |
f27aaf4b CB |
483 | } |
484 | ||
485 | /* | |
486 | * Cancel aio. Since we don't reference acb in a non qemu threads, | |
487 | * it is safe to access it here. | |
488 | */ | |
ad32e9c0 | 489 | static void qemu_rbd_aio_cancel(BlockDriverAIOCB *blockacb) |
f27aaf4b CB |
490 | { |
491 | RBDAIOCB *acb = (RBDAIOCB *) blockacb; | |
492 | acb->cancelled = 1; | |
493 | } | |
494 | ||
495 | static AIOPool rbd_aio_pool = { | |
496 | .aiocb_size = sizeof(RBDAIOCB), | |
ad32e9c0 | 497 | .cancel = qemu_rbd_aio_cancel, |
f27aaf4b CB |
498 | }; |
499 | ||
ad32e9c0 | 500 | static int qemu_rbd_send_pipe(BDRVRBDState *s, RADOSCB *rcb) |
f27aaf4b | 501 | { |
ad32e9c0 | 502 | int ret = 0; |
f27aaf4b CB |
503 | while (1) { |
504 | fd_set wfd; | |
ad32e9c0 | 505 | int fd = s->fds[RBD_FD_WRITE]; |
f27aaf4b | 506 | |
ad32e9c0 JD |
507 | /* send the op pointer to the qemu thread that is responsible |
508 | for the aio/op completion. Must do it in a qemu thread context */ | |
f27aaf4b CB |
509 | ret = write(fd, (void *)&rcb, sizeof(rcb)); |
510 | if (ret >= 0) { | |
511 | break; | |
512 | } | |
513 | if (errno == EINTR) { | |
514 | continue; | |
ad32e9c0 | 515 | } |
f27aaf4b CB |
516 | if (errno != EAGAIN) { |
517 | break; | |
ad32e9c0 | 518 | } |
f27aaf4b CB |
519 | |
520 | FD_ZERO(&wfd); | |
521 | FD_SET(fd, &wfd); | |
522 | do { | |
523 | ret = select(fd + 1, NULL, &wfd, NULL, NULL); | |
524 | } while (ret < 0 && errno == EINTR); | |
525 | } | |
526 | ||
ad32e9c0 JD |
527 | return ret; |
528 | } | |
529 | ||
530 | /* | |
531 | * This is the callback function for rbd_aio_read and _write | |
532 | * | |
533 | * Note: this function is being called from a non qemu thread so | |
534 | * we need to be careful about what we do here. Generally we only | |
535 | * write to the block notification pipe, and do the rest of the | |
536 | * io completion handling from qemu_rbd_aio_event_reader() which | |
537 | * runs in a qemu context. | |
538 | */ | |
539 | static void rbd_finish_aiocb(rbd_completion_t c, RADOSCB *rcb) | |
540 | { | |
541 | int ret; | |
542 | rcb->ret = rbd_aio_get_return_value(c); | |
543 | rbd_aio_release(c); | |
544 | ret = qemu_rbd_send_pipe(rcb->s, rcb); | |
f27aaf4b | 545 | if (ret < 0) { |
ad32e9c0 | 546 | error_report("failed writing to acb->s->fds"); |
f27aaf4b CB |
547 | qemu_free(rcb); |
548 | } | |
549 | } | |
550 | ||
ad32e9c0 | 551 | /* Callback when all queued rbd_aio requests are complete */ |
f27aaf4b CB |
552 | |
553 | static void rbd_aio_bh_cb(void *opaque) | |
554 | { | |
555 | RBDAIOCB *acb = opaque; | |
556 | ||
557 | if (!acb->write) { | |
558 | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); | |
559 | } | |
560 | qemu_vfree(acb->bounce); | |
561 | acb->common.cb(acb->common.opaque, (acb->ret > 0 ? 0 : acb->ret)); | |
562 | qemu_bh_delete(acb->bh); | |
563 | acb->bh = NULL; | |
564 | ||
565 | qemu_aio_release(acb); | |
566 | } | |
567 | ||
568 | static BlockDriverAIOCB *rbd_aio_rw_vector(BlockDriverState *bs, | |
569 | int64_t sector_num, | |
570 | QEMUIOVector *qiov, | |
571 | int nb_sectors, | |
572 | BlockDriverCompletionFunc *cb, | |
573 | void *opaque, int write) | |
574 | { | |
575 | RBDAIOCB *acb; | |
576 | RADOSCB *rcb; | |
ad32e9c0 | 577 | rbd_completion_t c; |
f27aaf4b CB |
578 | int64_t off, size; |
579 | char *buf; | |
51a13528 | 580 | int r; |
f27aaf4b CB |
581 | |
582 | BDRVRBDState *s = bs->opaque; | |
583 | ||
584 | acb = qemu_aio_get(&rbd_aio_pool, bs, cb, opaque); | |
51a13528 JD |
585 | if (!acb) { |
586 | return NULL; | |
587 | } | |
f27aaf4b CB |
588 | acb->write = write; |
589 | acb->qiov = qiov; | |
590 | acb->bounce = qemu_blockalign(bs, qiov->size); | |
f27aaf4b CB |
591 | acb->ret = 0; |
592 | acb->error = 0; | |
593 | acb->s = s; | |
594 | acb->cancelled = 0; | |
595 | acb->bh = NULL; | |
596 | ||
597 | if (write) { | |
598 | qemu_iovec_to_buffer(acb->qiov, acb->bounce); | |
599 | } | |
600 | ||
601 | buf = acb->bounce; | |
602 | ||
603 | off = sector_num * BDRV_SECTOR_SIZE; | |
604 | size = nb_sectors * BDRV_SECTOR_SIZE; | |
f27aaf4b | 605 | |
ad32e9c0 | 606 | s->qemu_aio_count++; /* All the RADOSCB */ |
f27aaf4b | 607 | |
ad32e9c0 JD |
608 | rcb = qemu_malloc(sizeof(RADOSCB)); |
609 | rcb->done = 0; | |
610 | rcb->acb = acb; | |
611 | rcb->buf = buf; | |
612 | rcb->s = acb->s; | |
613 | rcb->size = size; | |
51a13528 JD |
614 | r = rbd_aio_create_completion(rcb, (rbd_callback_t) rbd_finish_aiocb, &c); |
615 | if (r < 0) { | |
616 | goto failed; | |
617 | } | |
f27aaf4b | 618 | |
ad32e9c0 | 619 | if (write) { |
51a13528 | 620 | r = rbd_aio_write(s->image, off, size, buf, c); |
ad32e9c0 | 621 | } else { |
51a13528 JD |
622 | r = rbd_aio_read(s->image, off, size, buf, c); |
623 | } | |
624 | ||
625 | if (r < 0) { | |
626 | goto failed; | |
f27aaf4b CB |
627 | } |
628 | ||
629 | return &acb->common; | |
51a13528 JD |
630 | |
631 | failed: | |
632 | qemu_free(rcb); | |
633 | s->qemu_aio_count--; | |
634 | qemu_aio_release(acb); | |
635 | return NULL; | |
f27aaf4b CB |
636 | } |
637 | ||
ad32e9c0 JD |
638 | static BlockDriverAIOCB *qemu_rbd_aio_readv(BlockDriverState *bs, |
639 | int64_t sector_num, | |
640 | QEMUIOVector *qiov, | |
641 | int nb_sectors, | |
642 | BlockDriverCompletionFunc *cb, | |
643 | void *opaque) | |
f27aaf4b CB |
644 | { |
645 | return rbd_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); | |
646 | } | |
647 | ||
ad32e9c0 JD |
648 | static BlockDriverAIOCB *qemu_rbd_aio_writev(BlockDriverState *bs, |
649 | int64_t sector_num, | |
650 | QEMUIOVector *qiov, | |
651 | int nb_sectors, | |
652 | BlockDriverCompletionFunc *cb, | |
653 | void *opaque) | |
f27aaf4b CB |
654 | { |
655 | return rbd_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); | |
656 | } | |
657 | ||
ad32e9c0 | 658 | static int qemu_rbd_getinfo(BlockDriverState *bs, BlockDriverInfo *bdi) |
f27aaf4b CB |
659 | { |
660 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
661 | rbd_image_info_t info; |
662 | int r; | |
663 | ||
664 | r = rbd_stat(s->image, &info, sizeof(info)); | |
665 | if (r < 0) { | |
666 | return r; | |
667 | } | |
668 | ||
669 | bdi->cluster_size = info.obj_size; | |
f27aaf4b CB |
670 | return 0; |
671 | } | |
672 | ||
ad32e9c0 | 673 | static int64_t qemu_rbd_getlength(BlockDriverState *bs) |
f27aaf4b CB |
674 | { |
675 | BDRVRBDState *s = bs->opaque; | |
ad32e9c0 JD |
676 | rbd_image_info_t info; |
677 | int r; | |
f27aaf4b | 678 | |
ad32e9c0 JD |
679 | r = rbd_stat(s->image, &info, sizeof(info)); |
680 | if (r < 0) { | |
681 | return r; | |
682 | } | |
683 | ||
684 | return info.size; | |
f27aaf4b CB |
685 | } |
686 | ||
30cdc48c JD |
687 | static int qemu_rbd_truncate(BlockDriverState *bs, int64_t offset) |
688 | { | |
689 | BDRVRBDState *s = bs->opaque; | |
690 | int r; | |
691 | ||
692 | r = rbd_resize(s->image, offset); | |
693 | if (r < 0) { | |
694 | return r; | |
695 | } | |
696 | ||
697 | return 0; | |
698 | } | |
699 | ||
ad32e9c0 JD |
700 | static int qemu_rbd_snap_create(BlockDriverState *bs, |
701 | QEMUSnapshotInfo *sn_info) | |
f27aaf4b CB |
702 | { |
703 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 704 | int r; |
f27aaf4b CB |
705 | |
706 | if (sn_info->name[0] == '\0') { | |
707 | return -EINVAL; /* we need a name for rbd snapshots */ | |
708 | } | |
709 | ||
710 | /* | |
711 | * rbd snapshots are using the name as the user controlled unique identifier | |
712 | * we can't use the rbd snapid for that purpose, as it can't be set | |
713 | */ | |
714 | if (sn_info->id_str[0] != '\0' && | |
715 | strcmp(sn_info->id_str, sn_info->name) != 0) { | |
716 | return -EINVAL; | |
717 | } | |
718 | ||
719 | if (strlen(sn_info->name) >= sizeof(sn_info->id_str)) { | |
720 | return -ERANGE; | |
721 | } | |
722 | ||
ad32e9c0 | 723 | r = rbd_snap_create(s->image, sn_info->name); |
f27aaf4b | 724 | if (r < 0) { |
ad32e9c0 | 725 | error_report("failed to create snap: %s", strerror(-r)); |
f27aaf4b CB |
726 | return r; |
727 | } | |
728 | ||
f27aaf4b CB |
729 | return 0; |
730 | } | |
731 | ||
ad32e9c0 JD |
732 | static int qemu_rbd_snap_list(BlockDriverState *bs, |
733 | QEMUSnapshotInfo **psn_tab) | |
f27aaf4b CB |
734 | { |
735 | BDRVRBDState *s = bs->opaque; | |
f27aaf4b | 736 | QEMUSnapshotInfo *sn_info, *sn_tab = NULL; |
ad32e9c0 JD |
737 | int i, snap_count; |
738 | rbd_snap_info_t *snaps; | |
739 | int max_snaps = RBD_MAX_SNAPS; | |
f27aaf4b | 740 | |
ad32e9c0 JD |
741 | do { |
742 | snaps = qemu_malloc(sizeof(*snaps) * max_snaps); | |
743 | snap_count = rbd_snap_list(s->image, snaps, &max_snaps); | |
744 | if (snap_count < 0) { | |
745 | qemu_free(snaps); | |
f27aaf4b | 746 | } |
ad32e9c0 | 747 | } while (snap_count == -ERANGE); |
f27aaf4b | 748 | |
ad32e9c0 JD |
749 | if (snap_count <= 0) { |
750 | return snap_count; | |
f27aaf4b CB |
751 | } |
752 | ||
753 | sn_tab = qemu_mallocz(snap_count * sizeof(QEMUSnapshotInfo)); | |
f27aaf4b | 754 | |
ad32e9c0 JD |
755 | for (i = 0; i < snap_count; i++) { |
756 | const char *snap_name = snaps[i].name; | |
f27aaf4b CB |
757 | |
758 | sn_info = sn_tab + i; | |
759 | pstrcpy(sn_info->id_str, sizeof(sn_info->id_str), snap_name); | |
760 | pstrcpy(sn_info->name, sizeof(sn_info->name), snap_name); | |
f27aaf4b | 761 | |
ad32e9c0 | 762 | sn_info->vm_state_size = snaps[i].size; |
f27aaf4b CB |
763 | sn_info->date_sec = 0; |
764 | sn_info->date_nsec = 0; | |
765 | sn_info->vm_clock_nsec = 0; | |
766 | } | |
ad32e9c0 JD |
767 | rbd_snap_list_end(snaps); |
768 | ||
f27aaf4b | 769 | *psn_tab = sn_tab; |
f27aaf4b | 770 | return snap_count; |
f27aaf4b CB |
771 | } |
772 | ||
ad32e9c0 | 773 | static QEMUOptionParameter qemu_rbd_create_options[] = { |
f27aaf4b CB |
774 | { |
775 | .name = BLOCK_OPT_SIZE, | |
776 | .type = OPT_SIZE, | |
777 | .help = "Virtual disk size" | |
778 | }, | |
779 | { | |
780 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
781 | .type = OPT_SIZE, | |
782 | .help = "RBD object size" | |
783 | }, | |
784 | {NULL} | |
785 | }; | |
786 | ||
787 | static BlockDriver bdrv_rbd = { | |
788 | .format_name = "rbd", | |
789 | .instance_size = sizeof(BDRVRBDState), | |
ad32e9c0 JD |
790 | .bdrv_file_open = qemu_rbd_open, |
791 | .bdrv_close = qemu_rbd_close, | |
792 | .bdrv_create = qemu_rbd_create, | |
793 | .bdrv_get_info = qemu_rbd_getinfo, | |
794 | .create_options = qemu_rbd_create_options, | |
795 | .bdrv_getlength = qemu_rbd_getlength, | |
30cdc48c | 796 | .bdrv_truncate = qemu_rbd_truncate, |
f27aaf4b CB |
797 | .protocol_name = "rbd", |
798 | ||
ad32e9c0 JD |
799 | .bdrv_aio_readv = qemu_rbd_aio_readv, |
800 | .bdrv_aio_writev = qemu_rbd_aio_writev, | |
f27aaf4b | 801 | |
ad32e9c0 JD |
802 | .bdrv_snapshot_create = qemu_rbd_snap_create, |
803 | .bdrv_snapshot_list = qemu_rbd_snap_list, | |
f27aaf4b CB |
804 | }; |
805 | ||
806 | static void bdrv_rbd_init(void) | |
807 | { | |
808 | bdrv_register(&bdrv_rbd); | |
809 | } | |
810 | ||
811 | block_init(bdrv_rbd_init); |