4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
21 struct io_u **aio_events;
25 struct thread_data *td;
31 static struct fio_option options[] = {
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,
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,
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,
64 static int _fio_setup_rbd_data(struct thread_data *td,
65 struct rbd_data **rbd_data_ptr)
67 struct rbd_data *rbd_data;
72 rbd_data = malloc(sizeof(struct rbd_data));
76 memset(rbd_data, 0, sizeof(struct rbd_data));
78 rbd_data->aio_events = malloc(td->o.iodepth * sizeof(struct io_u *));
79 if (!rbd_data->aio_events)
82 memset(rbd_data->aio_events, 0, td->o.iodepth * sizeof(struct io_u *));
84 *rbd_data_ptr = rbd_data;
95 static int _fio_rbd_connect(struct thread_data *td)
97 struct rbd_data *rbd_data = td->io_ops->data;
98 struct rbd_options *o = td->eo;
101 r = rados_create(&(rbd_data->cluster), o->client_name);
103 log_err("rados_create failed.\n");
107 r = rados_conf_read_file(rbd_data->cluster, NULL);
109 log_err("rados_conf_read_file failed.\n");
113 r = rados_connect(rbd_data->cluster);
115 log_err("rados_connect failed.\n");
116 goto failed_shutdown;
119 r = rados_ioctx_create(rbd_data->cluster, o->pool_name,
120 &(rbd_data->io_ctx));
122 log_err("rados_ioctx_create failed.\n");
123 goto failed_shutdown;
126 r = rbd_open(rbd_data->io_ctx, o->rbd_name, &(rbd_data->image),
129 log_err("rbd_open failed.\n");
135 rados_ioctx_destroy(rbd_data->io_ctx);
136 rbd_data->io_ctx = NULL;
138 rados_shutdown(rbd_data->cluster);
139 rbd_data->cluster = NULL;
144 static void _fio_rbd_disconnect(struct rbd_data *rbd_data)
149 /* shutdown everything */
150 if (rbd_data->image) {
151 rbd_close(rbd_data->image);
152 rbd_data->image = NULL;
155 if (rbd_data->io_ctx) {
156 rados_ioctx_destroy(rbd_data->io_ctx);
157 rbd_data->io_ctx = NULL;
160 if (rbd_data->cluster) {
161 rados_shutdown(rbd_data->cluster);
162 rbd_data->cluster = NULL;
166 static void _fio_rbd_finish_write_aiocb(rbd_completion_t comp, void *data)
168 struct io_u *io_u = (struct io_u *)data;
169 struct fio_rbd_iou *fio_rbd_iou =
170 (struct fio_rbd_iou *)io_u->engine_data;
172 fio_rbd_iou->io_complete = 1;
174 /* if write needs to be verified - we should not release comp here
175 without fetching the result */
177 rbd_aio_release(comp);
178 /* TODO handle error */
183 static void _fio_rbd_finish_read_aiocb(rbd_completion_t comp, void *data)
185 struct io_u *io_u = (struct io_u *)data;
186 struct fio_rbd_iou *fio_rbd_iou =
187 (struct fio_rbd_iou *)io_u->engine_data;
189 fio_rbd_iou->io_complete = 1;
191 /* if read needs to be verified - we should not release comp here
192 without fetching the result */
193 rbd_aio_release(comp);
195 /* TODO handle error */
200 static void _fio_rbd_finish_sync_aiocb(rbd_completion_t comp, void *data)
202 struct io_u *io_u = (struct io_u *)data;
203 struct fio_rbd_iou *fio_rbd_iou =
204 (struct fio_rbd_iou *)io_u->engine_data;
206 fio_rbd_iou->io_complete = 1;
208 /* if sync needs to be verified - we should not release comp here
209 without fetching the result */
210 rbd_aio_release(comp);
212 /* TODO handle error */
217 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
219 struct rbd_data *rbd_data = td->io_ops->data;
221 return rbd_data->aio_events[event];
224 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
225 unsigned int max, const struct timespec *t)
227 struct rbd_data *rbd_data = td->io_ops->data;
228 unsigned int events = 0;
231 struct fio_rbd_iou *fov;
234 io_u_qiter(&td->io_u_all, io_u, i) {
235 if (!(io_u->flags & IO_U_F_FLIGHT))
238 fov = (struct fio_rbd_iou *)io_u->engine_data;
240 if (fov->io_complete) {
241 fov->io_complete = 0;
242 rbd_data->aio_events[events] = io_u;
257 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
260 struct rbd_data *rbd_data = td->io_ops->data;
261 rbd_completion_t comp;
263 fio_ro_check(td, io_u);
265 if (io_u->ddir == DDIR_WRITE) {
266 r = rbd_aio_create_completion(io_u,
268 _fio_rbd_finish_write_aiocb,
272 ("rbd_aio_create_completion for DDIR_WRITE failed.\n");
276 r = rbd_aio_write(rbd_data->image, io_u->offset,
277 io_u->xfer_buflen, io_u->xfer_buf, comp);
279 log_err("rbd_aio_write failed.\n");
283 } else if (io_u->ddir == DDIR_READ) {
284 r = rbd_aio_create_completion(io_u,
286 _fio_rbd_finish_read_aiocb,
290 ("rbd_aio_create_completion for DDIR_READ failed.\n");
294 r = rbd_aio_read(rbd_data->image, io_u->offset,
295 io_u->xfer_buflen, io_u->xfer_buf, comp);
298 log_err("rbd_aio_read failed.\n");
302 } else if (io_u->ddir == DDIR_SYNC) {
303 r = rbd_aio_create_completion(io_u,
305 _fio_rbd_finish_sync_aiocb,
309 ("rbd_aio_create_completion for DDIR_SYNC failed.\n");
313 r = rbd_aio_flush(rbd_data->image, comp);
315 log_err("rbd_flush failed.\n");
320 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
322 return FIO_Q_COMPLETED;
329 td_verror(td, io_u->error, "xfer");
330 return FIO_Q_COMPLETED;
333 static int fio_rbd_init(struct thread_data *td)
337 r = _fio_rbd_connect(td);
339 log_err("fio_rbd_connect failed, return code: %d .\n", r);
350 static void fio_rbd_cleanup(struct thread_data *td)
352 struct rbd_data *rbd_data = td->io_ops->data;
355 _fio_rbd_disconnect(rbd_data);
356 free(rbd_data->aio_events);
362 static int fio_rbd_setup(struct thread_data *td)
365 rbd_image_info_t info;
367 struct rbd_data *rbd_data = NULL;
368 int major, minor, extra;
370 /* log version of librbd. No cluster connection required. */
371 rbd_version(&major, &minor, &extra);
372 log_info("rbd engine: RBD version: %d.%d.%d\n", major, minor, extra);
374 /* allocate engine specific structure to deal with librbd. */
375 r = _fio_setup_rbd_data(td, &rbd_data);
377 log_err("fio_setup_rbd_data failed.\n");
380 td->io_ops->data = rbd_data;
382 /* librbd does not allow us to run first in the main thread and later in a
383 * fork child. It needs to be the same process context all the time.
385 td->o.use_thread = 1;
387 /* connect in the main thread to determine to determine
388 * the size of the given RADOS block device. And disconnect
391 r = _fio_rbd_connect(td);
393 log_err("fio_rbd_connect failed.\n");
397 /* get size of the RADOS block device */
398 r = rbd_stat(rbd_data->image, &info, sizeof(info));
400 log_err("rbd_status failed.\n");
403 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
405 /* taken from "net" engine. Pretend we deal with files,
406 * even if we do not have any ideas about files.
407 * The size of the RBD is set instead of a artificial file.
409 if (!td->files_index) {
410 add_file(td, td->o.filename ? : "rbd", 0, 0);
411 td->o.nr_files = td->o.nr_files ? : 1;
415 f->real_file_size = info.size;
417 /* disconnect, then we were only connected to determine
418 * the size of the RBD.
420 _fio_rbd_disconnect(rbd_data);
424 _fio_rbd_disconnect(rbd_data);
430 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
435 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
440 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
442 struct fio_rbd_iou *o = io_u->engine_data;
445 io_u->engine_data = NULL;
450 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
452 struct fio_rbd_iou *o;
454 o = malloc(sizeof(*o));
457 io_u->engine_data = o;
461 static struct ioengine_ops ioengine = {
463 .version = FIO_IOOPS_VERSION,
464 .setup = fio_rbd_setup,
465 .init = fio_rbd_init,
466 .queue = fio_rbd_queue,
467 .getevents = fio_rbd_getevents,
468 .event = fio_rbd_event,
469 .cleanup = fio_rbd_cleanup,
470 .open_file = fio_rbd_open,
471 .invalidate = fio_rbd_invalidate,
473 .io_u_init = fio_rbd_io_u_init,
474 .io_u_free = fio_rbd_io_u_free,
475 .option_struct_size = sizeof(struct rbd_options),
478 static void fio_init fio_rbd_register(void)
480 register_ioengine(&ioengine);
483 static void fio_exit fio_rbd_unregister(void)
485 unregister_ioengine(&ioengine);