Commit | Line | Data |
---|---|---|
fc5c0345 DG |
1 | /* |
2 | * rbd engine | |
3 | * | |
4 | * IO engine using Ceph's librbd to test RADOS Block Devices. | |
5 | * | |
6 | */ | |
7 | ||
8 | #include <rbd/librbd.h> | |
9 | ||
10 | #include "../fio.h" | |
11 | ||
12 | struct fio_rbd_iou { | |
13 | struct io_u *io_u; | |
d8b64af2 | 14 | rbd_completion_t completion; |
d8b64af2 | 15 | int io_seen; |
fc5c0345 DG |
16 | }; |
17 | ||
18 | struct rbd_data { | |
19 | rados_t cluster; | |
20 | rados_ioctx_t io_ctx; | |
21 | rbd_image_t image; | |
22 | struct io_u **aio_events; | |
23 | }; | |
24 | ||
25 | struct rbd_options { | |
fc5c0345 DG |
26 | char *rbd_name; |
27 | char *pool_name; | |
28 | char *client_name; | |
29 | }; | |
30 | ||
31 | static struct fio_option options[] = { | |
32 | { | |
d8b64af2 JA |
33 | .name = "rbdname", |
34 | .lname = "rbd engine rbdname", | |
35 | .type = FIO_OPT_STR_STORE, | |
36 | .help = "RBD name for RBD engine", | |
37 | .off1 = offsetof(struct rbd_options, rbd_name), | |
38 | .category = FIO_OPT_C_ENGINE, | |
39 | .group = FIO_OPT_G_RBD, | |
40 | }, | |
fc5c0345 | 41 | { |
d8b64af2 JA |
42 | .name = "pool", |
43 | .lname = "rbd engine pool", | |
44 | .type = FIO_OPT_STR_STORE, | |
45 | .help = "Name of the pool hosting the RBD for the RBD engine", | |
46 | .off1 = offsetof(struct rbd_options, pool_name), | |
47 | .category = FIO_OPT_C_ENGINE, | |
48 | .group = FIO_OPT_G_RBD, | |
49 | }, | |
fc5c0345 | 50 | { |
d8b64af2 JA |
51 | .name = "clientname", |
52 | .lname = "rbd engine clientname", | |
53 | .type = FIO_OPT_STR_STORE, | |
54 | .help = "Name of the ceph client to access the RBD for the RBD engine", | |
55 | .off1 = offsetof(struct rbd_options, client_name), | |
56 | .category = FIO_OPT_C_ENGINE, | |
57 | .group = FIO_OPT_G_RBD, | |
58 | }, | |
fc5c0345 | 59 | { |
d8b64af2 JA |
60 | .name = NULL, |
61 | }, | |
fc5c0345 DG |
62 | }; |
63 | ||
64 | static int _fio_setup_rbd_data(struct thread_data *td, | |
65 | struct rbd_data **rbd_data_ptr) | |
66 | { | |
67 | struct rbd_data *rbd_data; | |
68 | ||
69 | if (td->io_ops->data) | |
70 | return 0; | |
71 | ||
72 | rbd_data = malloc(sizeof(struct rbd_data)); | |
73 | if (!rbd_data) | |
74 | goto failed; | |
75 | ||
76 | memset(rbd_data, 0, sizeof(struct rbd_data)); | |
77 | ||
78 | rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *)); | |
79 | if (!rbd_data->aio_events) | |
80 | goto failed; | |
81 | ||
82 | memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *)); | |
83 | ||
84 | *rbd_data_ptr = rbd_data; | |
85 | ||
86 | return 0; | |
87 | ||
88 | failed: | |
d9cb70a8 JA |
89 | if (rbd_data) |
90 | free(rbd_data); | |
fc5c0345 DG |
91 | return 1; |
92 | ||
93 | } | |
94 | ||
95 | static int _fio_rbd_connect(struct thread_data *td) | |
96 | { | |
97 | struct rbd_data *rbd_data = td->io_ops->data; | |
98 | struct rbd_options *o = td->eo; | |
99 | int r; | |
100 | ||
dbf388d2 | 101 | r = rados_create(&rbd_data->cluster, o->client_name); |
fc5c0345 DG |
102 | if (r < 0) { |
103 | log_err("rados_create failed.\n"); | |
104 | goto failed_early; | |
105 | } | |
106 | ||
107 | r = rados_conf_read_file(rbd_data->cluster, NULL); | |
108 | if (r < 0) { | |
109 | log_err("rados_conf_read_file failed.\n"); | |
110 | goto failed_early; | |
111 | } | |
112 | ||
113 | r = rados_connect(rbd_data->cluster); | |
114 | if (r < 0) { | |
115 | log_err("rados_connect failed.\n"); | |
116 | goto failed_shutdown; | |
117 | } | |
118 | ||
119 | r = rados_ioctx_create(rbd_data->cluster, o->pool_name, | |
dbf388d2 | 120 | &rbd_data->io_ctx); |
fc5c0345 DG |
121 | if (r < 0) { |
122 | log_err("rados_ioctx_create failed.\n"); | |
123 | goto failed_shutdown; | |
124 | } | |
125 | ||
dbf388d2 | 126 | r = rbd_open(rbd_data->io_ctx, o->rbd_name, &rbd_data->image, |
fc5c0345 DG |
127 | NULL /*snap */ ); |
128 | if (r < 0) { | |
129 | log_err("rbd_open failed.\n"); | |
130 | goto failed_open; | |
131 | } | |
132 | return 0; | |
133 | ||
134 | failed_open: | |
135 | rados_ioctx_destroy(rbd_data->io_ctx); | |
086f9935 | 136 | rbd_data->io_ctx = NULL; |
fc5c0345 DG |
137 | failed_shutdown: |
138 | rados_shutdown(rbd_data->cluster); | |
086f9935 | 139 | rbd_data->cluster = NULL; |
fc5c0345 DG |
140 | failed_early: |
141 | return 1; | |
142 | } | |
143 | ||
144 | static void _fio_rbd_disconnect(struct rbd_data *rbd_data) | |
145 | { | |
146 | if (!rbd_data) | |
147 | return; | |
148 | ||
149 | /* shutdown everything */ | |
150 | if (rbd_data->image) { | |
151 | rbd_close(rbd_data->image); | |
152 | rbd_data->image = NULL; | |
153 | } | |
154 | ||
155 | if (rbd_data->io_ctx) { | |
156 | rados_ioctx_destroy(rbd_data->io_ctx); | |
157 | rbd_data->io_ctx = NULL; | |
158 | } | |
159 | ||
160 | if (rbd_data->cluster) { | |
161 | rados_shutdown(rbd_data->cluster); | |
162 | rbd_data->cluster = NULL; | |
163 | } | |
164 | } | |
165 | ||
d8b64af2 | 166 | static void _fio_rbd_finish_aiocb(rbd_completion_t comp, void *data) |
fc5c0345 | 167 | { |
dbf388d2 JA |
168 | struct fio_rbd_iou *fri = data; |
169 | struct io_u *io_u = fri->io_u; | |
d8b64af2 | 170 | ssize_t ret; |
fc5c0345 | 171 | |
d8b64af2 JA |
172 | /* |
173 | * Looks like return value is 0 for success, or < 0 for | |
174 | * a specific error. So we have to assume that it can't do | |
175 | * partial completions. | |
176 | */ | |
177 | ret = rbd_aio_get_return_value(fri->completion); | |
178 | if (ret < 0) { | |
179 | io_u->error = ret; | |
180 | io_u->resid = io_u->xfer_buflen; | |
181 | } else | |
182 | io_u->error = 0; | |
183 | } | |
fc5c0345 | 184 | |
d8b64af2 JA |
185 | static struct io_u *fio_rbd_event(struct thread_data *td, int event) |
186 | { | |
187 | struct rbd_data *rbd_data = td->io_ops->data; | |
fc5c0345 | 188 | |
d8b64af2 | 189 | return rbd_data->aio_events[event]; |
fc5c0345 DG |
190 | } |
191 | ||
d8b64af2 JA |
192 | static inline int fri_check_complete(struct rbd_data *rbd_data, |
193 | struct io_u *io_u, | |
194 | unsigned int *events) | |
fc5c0345 | 195 | { |
d8b64af2 | 196 | struct fio_rbd_iou *fri = io_u->engine_data; |
fc5c0345 | 197 | |
dbf388d2 | 198 | if (rbd_aio_is_complete(fri->completion)) { |
d8b64af2 JA |
199 | fri->io_seen = 1; |
200 | rbd_data->aio_events[*events] = io_u; | |
201 | (*events)++; | |
fc5c0345 | 202 | |
d8b64af2 JA |
203 | rbd_aio_release(fri->completion); |
204 | return 1; | |
205 | } | |
fc5c0345 | 206 | |
d8b64af2 | 207 | return 0; |
fc5c0345 DG |
208 | } |
209 | ||
d8b64af2 JA |
210 | static int rbd_iter_events(struct thread_data *td, unsigned int *events, |
211 | unsigned int min_evts, int wait) | |
82340a9f | 212 | { |
d8b64af2 JA |
213 | struct rbd_data *rbd_data = td->io_ops->data; |
214 | unsigned int this_events = 0; | |
215 | struct io_u *io_u; | |
216 | int i; | |
82340a9f | 217 | |
d8b64af2 JA |
218 | io_u_qiter(&td->io_u_all, io_u, i) { |
219 | struct fio_rbd_iou *fri = io_u->engine_data; | |
82340a9f | 220 | |
d8b64af2 JA |
221 | if (!(io_u->flags & IO_U_F_FLIGHT)) |
222 | continue; | |
223 | if (fri->io_seen) | |
224 | continue; | |
82340a9f | 225 | |
d8b64af2 JA |
226 | if (fri_check_complete(rbd_data, io_u, events)) |
227 | this_events++; | |
228 | else if (wait) { | |
229 | rbd_aio_wait_for_complete(fri->completion); | |
82340a9f | 230 | |
d8b64af2 JA |
231 | if (fri_check_complete(rbd_data, io_u, events)) |
232 | this_events++; | |
233 | } | |
234 | if (*events >= min_evts) | |
235 | break; | |
236 | } | |
fc5c0345 | 237 | |
d8b64af2 | 238 | return this_events; |
fc5c0345 DG |
239 | } |
240 | ||
241 | static int fio_rbd_getevents(struct thread_data *td, unsigned int min, | |
1f440ece | 242 | unsigned int max, const struct timespec *t) |
fc5c0345 | 243 | { |
d8b64af2 JA |
244 | unsigned int this_events, events = 0; |
245 | int wait = 0; | |
fc5c0345 DG |
246 | |
247 | do { | |
d8b64af2 | 248 | this_events = rbd_iter_events(td, &events, min, wait); |
fc5c0345 | 249 | |
d8b64af2 | 250 | if (events >= min) |
fc5c0345 | 251 | break; |
d8b64af2 JA |
252 | if (this_events) |
253 | continue; | |
fc5c0345 | 254 | |
d8b64af2 | 255 | wait = 1; |
fc5c0345 DG |
256 | } while (1); |
257 | ||
258 | return events; | |
259 | } | |
260 | ||
261 | static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u) | |
262 | { | |
fc5c0345 | 263 | struct rbd_data *rbd_data = td->io_ops->data; |
d8b64af2 JA |
264 | struct fio_rbd_iou *fri = io_u->engine_data; |
265 | int r = -1; | |
fc5c0345 DG |
266 | |
267 | fio_ro_check(td, io_u); | |
268 | ||
d8b64af2 JA |
269 | fri->io_seen = 0; |
270 | ||
dbf388d2 | 271 | r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb, |
d8b64af2 | 272 | &fri->completion); |
dbf388d2 JA |
273 | if (r < 0) { |
274 | log_err("rbd_aio_create_completion failed.\n"); | |
275 | goto failed; | |
276 | } | |
fc5c0345 | 277 | |
dbf388d2 | 278 | if (io_u->ddir == DDIR_WRITE) { |
fc5c0345 | 279 | r = rbd_aio_write(rbd_data->image, io_u->offset, |
d8b64af2 JA |
280 | io_u->xfer_buflen, io_u->xfer_buf, |
281 | fri->completion); | |
fc5c0345 DG |
282 | if (r < 0) { |
283 | log_err("rbd_aio_write failed.\n"); | |
dbf388d2 | 284 | goto failed_comp; |
fc5c0345 DG |
285 | } |
286 | ||
287 | } else if (io_u->ddir == DDIR_READ) { | |
fc5c0345 | 288 | r = rbd_aio_read(rbd_data->image, io_u->offset, |
d8b64af2 JA |
289 | io_u->xfer_buflen, io_u->xfer_buf, |
290 | fri->completion); | |
fc5c0345 DG |
291 | |
292 | if (r < 0) { | |
293 | log_err("rbd_aio_read failed.\n"); | |
dbf388d2 | 294 | goto failed_comp; |
fc5c0345 | 295 | } |
dbf388d2 JA |
296 | } else if (io_u->ddir == DDIR_TRIM) { |
297 | r = rbd_aio_discard(rbd_data->image, io_u->offset, | |
298 | io_u->xfer_buflen, fri->completion); | |
82340a9f | 299 | if (r < 0) { |
dbf388d2 JA |
300 | log_err("rbd_aio_discard failed.\n"); |
301 | goto failed_comp; | |
82340a9f | 302 | } |
dbf388d2 | 303 | } else if (io_u->ddir == DDIR_SYNC) { |
d8b64af2 | 304 | r = rbd_aio_flush(rbd_data->image, fri->completion); |
fc5c0345 DG |
305 | if (r < 0) { |
306 | log_err("rbd_flush failed.\n"); | |
dbf388d2 | 307 | goto failed_comp; |
fc5c0345 | 308 | } |
fc5c0345 DG |
309 | } else { |
310 | dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__, | |
311 | io_u->ddir); | |
dbf388d2 | 312 | goto failed_comp; |
fc5c0345 DG |
313 | } |
314 | ||
315 | return FIO_Q_QUEUED; | |
dbf388d2 JA |
316 | failed_comp: |
317 | rbd_aio_release(fri->completion); | |
fc5c0345 DG |
318 | failed: |
319 | io_u->error = r; | |
320 | td_verror(td, io_u->error, "xfer"); | |
321 | return FIO_Q_COMPLETED; | |
322 | } | |
323 | ||
324 | static int fio_rbd_init(struct thread_data *td) | |
325 | { | |
326 | int r; | |
327 | ||
328 | r = _fio_rbd_connect(td); | |
329 | if (r) { | |
330 | log_err("fio_rbd_connect failed, return code: %d .\n", r); | |
331 | goto failed; | |
332 | } | |
333 | ||
334 | return 0; | |
335 | ||
336 | failed: | |
337 | return 1; | |
fc5c0345 DG |
338 | } |
339 | ||
340 | static void fio_rbd_cleanup(struct thread_data *td) | |
341 | { | |
342 | struct rbd_data *rbd_data = td->io_ops->data; | |
343 | ||
344 | if (rbd_data) { | |
345 | _fio_rbd_disconnect(rbd_data); | |
346 | free(rbd_data->aio_events); | |
347 | free(rbd_data); | |
348 | } | |
349 | ||
350 | } | |
351 | ||
352 | static int fio_rbd_setup(struct thread_data *td) | |
353 | { | |
354 | int r = 0; | |
355 | rbd_image_info_t info; | |
356 | struct fio_file *f; | |
357 | struct rbd_data *rbd_data = NULL; | |
358 | int major, minor, extra; | |
359 | ||
360 | /* log version of librbd. No cluster connection required. */ | |
361 | rbd_version(&major, &minor, &extra); | |
362 | log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra); | |
363 | ||
364 | /* allocate engine specific structure to deal with librbd. */ | |
365 | r = _fio_setup_rbd_data(td, &rbd_data); | |
366 | if (r) { | |
367 | log_err("fio_setup_rbd_data failed.\n"); | |
368 | goto cleanup; | |
369 | } | |
370 | td->io_ops->data = rbd_data; | |
371 | ||
d8b64af2 JA |
372 | /* librbd does not allow us to run first in the main thread and later |
373 | * in a fork child. It needs to be the same process context all the | |
374 | * time. | |
fc5c0345 DG |
375 | */ |
376 | td->o.use_thread = 1; | |
377 | ||
378 | /* connect in the main thread to determine to determine | |
379 | * the size of the given RADOS block device. And disconnect | |
380 | * later on. | |
381 | */ | |
382 | r = _fio_rbd_connect(td); | |
383 | if (r) { | |
384 | log_err("fio_rbd_connect failed.\n"); | |
385 | goto cleanup; | |
386 | } | |
387 | ||
388 | /* get size of the RADOS block device */ | |
389 | r = rbd_stat(rbd_data->image, &info, sizeof(info)); | |
390 | if (r < 0) { | |
391 | log_err("rbd_status failed.\n"); | |
392 | goto disconnect; | |
393 | } | |
394 | dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size); | |
395 | ||
396 | /* taken from "net" engine. Pretend we deal with files, | |
397 | * even if we do not have any ideas about files. | |
398 | * The size of the RBD is set instead of a artificial file. | |
399 | */ | |
400 | if (!td->files_index) { | |
5903e7b7 | 401 | add_file(td, td->o.filename ? : "rbd", 0, 0); |
fc5c0345 | 402 | td->o.nr_files = td->o.nr_files ? : 1; |
b53f2c54 | 403 | td->o.open_files++; |
fc5c0345 DG |
404 | } |
405 | f = td->files[0]; | |
406 | f->real_file_size = info.size; | |
407 | ||
408 | /* disconnect, then we were only connected to determine | |
409 | * the size of the RBD. | |
410 | */ | |
411 | _fio_rbd_disconnect(rbd_data); | |
412 | return 0; | |
413 | ||
414 | disconnect: | |
415 | _fio_rbd_disconnect(rbd_data); | |
416 | cleanup: | |
417 | fio_rbd_cleanup(td); | |
418 | return r; | |
419 | } | |
420 | ||
421 | static int fio_rbd_open(struct thread_data *td, struct fio_file *f) | |
422 | { | |
423 | return 0; | |
424 | } | |
425 | ||
d9b100fc JA |
426 | static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f) |
427 | { | |
903b2812 JA |
428 | #if defined(CONFIG_RBD_INVAL) |
429 | struct rbd_data *rbd_data = td->io_ops->data; | |
430 | ||
431 | return rbd_invalidate_cache(rbd_data->image); | |
432 | #else | |
d9b100fc | 433 | return 0; |
903b2812 | 434 | #endif |
d9b100fc JA |
435 | } |
436 | ||
fc5c0345 DG |
437 | static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u) |
438 | { | |
d8b64af2 | 439 | struct fio_rbd_iou *fri = io_u->engine_data; |
fc5c0345 | 440 | |
d8b64af2 | 441 | if (fri) { |
fc5c0345 | 442 | io_u->engine_data = NULL; |
d8b64af2 | 443 | free(fri); |
fc5c0345 DG |
444 | } |
445 | } | |
446 | ||
447 | static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u) | |
448 | { | |
d8b64af2 | 449 | struct fio_rbd_iou *fri; |
fc5c0345 | 450 | |
d8b64af2 JA |
451 | fri = calloc(1, sizeof(*fri)); |
452 | fri->io_u = io_u; | |
453 | io_u->engine_data = fri; | |
fc5c0345 DG |
454 | return 0; |
455 | } | |
456 | ||
10aa136b | 457 | static struct ioengine_ops ioengine = { |
d9b100fc JA |
458 | .name = "rbd", |
459 | .version = FIO_IOOPS_VERSION, | |
460 | .setup = fio_rbd_setup, | |
461 | .init = fio_rbd_init, | |
462 | .queue = fio_rbd_queue, | |
463 | .getevents = fio_rbd_getevents, | |
464 | .event = fio_rbd_event, | |
465 | .cleanup = fio_rbd_cleanup, | |
466 | .open_file = fio_rbd_open, | |
467 | .invalidate = fio_rbd_invalidate, | |
468 | .options = options, | |
469 | .io_u_init = fio_rbd_io_u_init, | |
470 | .io_u_free = fio_rbd_io_u_free, | |
471 | .option_struct_size = sizeof(struct rbd_options), | |
fc5c0345 DG |
472 | }; |
473 | ||
474 | static void fio_init fio_rbd_register(void) | |
475 | { | |
476 | register_ioengine(&ioengine); | |
477 | } | |
478 | ||
479 | static void fio_exit fio_rbd_unregister(void) | |
480 | { | |
481 | unregister_ioengine(&ioengine); | |
482 | } |