4 * IO engine using Ceph's librbd to test RADOS Block Devices.
8 #include <rbd/librbd.h>
14 rbd_completion_t completion;
22 struct io_u **aio_events;
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,
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_aiocb(rbd_completion_t comp, void *data)
168 struct fio_rbd_iou *fri = data;
169 struct io_u *io_u = fri->io_u;
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.
177 ret = rbd_aio_get_return_value(fri->completion);
180 io_u->resid = io_u->xfer_buflen;
185 static struct io_u *fio_rbd_event(struct thread_data *td, int event)
187 struct rbd_data *rbd_data = td->io_ops->data;
189 return rbd_data->aio_events[event];
192 static inline int fri_check_complete(struct rbd_data *rbd_data,
194 unsigned int *events)
196 struct fio_rbd_iou *fri = io_u->engine_data;
198 if (rbd_aio_is_complete(fri->completion)) {
200 rbd_data->aio_events[*events] = io_u;
203 rbd_aio_release(fri->completion);
210 static int rbd_iter_events(struct thread_data *td, unsigned int *events,
211 unsigned int min_evts, int wait)
213 struct rbd_data *rbd_data = td->io_ops->data;
214 unsigned int this_events = 0;
218 io_u_qiter(&td->io_u_all, io_u, i) {
219 struct fio_rbd_iou *fri = io_u->engine_data;
221 if (!(io_u->flags & IO_U_F_FLIGHT))
226 if (fri_check_complete(rbd_data, io_u, events))
229 rbd_aio_wait_for_complete(fri->completion);
231 if (fri_check_complete(rbd_data, io_u, events))
234 if (*events >= min_evts)
241 static int fio_rbd_getevents(struct thread_data *td, unsigned int min,
242 unsigned int max, const struct timespec *t)
244 unsigned int this_events, events = 0;
248 this_events = rbd_iter_events(td, &events, min, wait);
261 static int fio_rbd_queue(struct thread_data *td, struct io_u *io_u)
263 struct rbd_data *rbd_data = td->io_ops->data;
264 struct fio_rbd_iou *fri = io_u->engine_data;
267 fio_ro_check(td, io_u);
271 r = rbd_aio_create_completion(fri, _fio_rbd_finish_aiocb,
274 log_err("rbd_aio_create_completion failed.\n");
278 if (io_u->ddir == DDIR_WRITE) {
279 r = rbd_aio_write(rbd_data->image, io_u->offset,
280 io_u->xfer_buflen, io_u->xfer_buf,
283 log_err("rbd_aio_write failed.\n");
287 } else if (io_u->ddir == DDIR_READ) {
288 r = rbd_aio_read(rbd_data->image, io_u->offset,
289 io_u->xfer_buflen, io_u->xfer_buf,
293 log_err("rbd_aio_read failed.\n");
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);
300 log_err("rbd_aio_discard failed.\n");
303 } else if (io_u->ddir == DDIR_SYNC) {
304 r = rbd_aio_flush(rbd_data->image, fri->completion);
306 log_err("rbd_flush failed.\n");
310 dprint(FD_IO, "%s: Warning: unhandled ddir: %d\n", __func__,
317 rbd_aio_release(fri->completion);
320 td_verror(td, io_u->error, "xfer");
321 return FIO_Q_COMPLETED;
324 static int fio_rbd_init(struct thread_data *td)
328 r = _fio_rbd_connect(td);
330 log_err("fio_rbd_connect failed, return code: %d .\n", r);
340 static void fio_rbd_cleanup(struct thread_data *td)
342 struct rbd_data *rbd_data = td->io_ops->data;
345 _fio_rbd_disconnect(rbd_data);
346 free(rbd_data->aio_events);
352 static int fio_rbd_setup(struct thread_data *td)
355 rbd_image_info_t info;
357 struct rbd_data *rbd_data = NULL;
358 int major, minor, extra;
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);
364 /* allocate engine specific structure to deal with librbd. */
365 r = _fio_setup_rbd_data(td, &rbd_data);
367 log_err("fio_setup_rbd_data failed.\n");
370 td->io_ops->data = rbd_data;
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
376 td->o.use_thread = 1;
378 /* connect in the main thread to determine to determine
379 * the size of the given RADOS block device. And disconnect
382 r = _fio_rbd_connect(td);
384 log_err("fio_rbd_connect failed.\n");
388 /* get size of the RADOS block device */
389 r = rbd_stat(rbd_data->image, &info, sizeof(info));
391 log_err("rbd_status failed.\n");
394 dprint(FD_IO, "rbd-engine: image size: %lu\n", info.size);
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.
400 if (!td->files_index) {
401 add_file(td, td->o.filename ? : "rbd", 0, 0);
402 td->o.nr_files = td->o.nr_files ? : 1;
406 f->real_file_size = info.size;
408 /* disconnect, then we were only connected to determine
409 * the size of the RBD.
411 _fio_rbd_disconnect(rbd_data);
415 _fio_rbd_disconnect(rbd_data);
421 static int fio_rbd_open(struct thread_data *td, struct fio_file *f)
426 static int fio_rbd_invalidate(struct thread_data *td, struct fio_file *f)
428 #if defined(CONFIG_RBD_INVAL)
429 struct rbd_data *rbd_data = td->io_ops->data;
431 return rbd_invalidate_cache(rbd_data->image);
437 static void fio_rbd_io_u_free(struct thread_data *td, struct io_u *io_u)
439 struct fio_rbd_iou *fri = io_u->engine_data;
442 io_u->engine_data = NULL;
447 static int fio_rbd_io_u_init(struct thread_data *td, struct io_u *io_u)
449 struct fio_rbd_iou *fri;
451 fri = calloc(1, sizeof(*fri));
453 io_u->engine_data = fri;
457 static struct ioengine_ops ioengine = {
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,
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),
474 static void fio_init fio_rbd_register(void)
476 register_ioengine(&ioengine);
479 static void fio_exit fio_rbd_unregister(void)
481 unregister_ioengine(&ioengine);