4 * IO engine using Glusterfs's gfapi interface
8 #include <glusterfs/api/glfs.h>
9 #include <glusterfs/api/glfs-handles.h>
13 struct thread_data *td;
23 static struct fio_option options[] = {
26 .lname = "Glusterfs volume",
27 .type = FIO_OPT_STR_STORE,
28 .help = "Name of the Glusterfs volume",
29 .off1 = offsetof(struct gf_options, gf_vol),
30 .category = FIO_OPT_C_ENGINE,
31 .group = FIO_OPT_G_GFAPI,
35 .lname = "Glusterfs brick name",
36 .type = FIO_OPT_STR_STORE,
37 .help = "Name of the Glusterfs brick to connect",
38 .off1 = offsetof(struct gf_options, gf_brick),
39 .category = FIO_OPT_C_ENGINE,
40 .group = FIO_OPT_G_GFAPI,
47 static int fio_gf_setup(struct thread_data *td)
50 struct gf_data *g = NULL;
51 struct gf_options *opt = td->eo;
52 struct stat sb = {0, };
54 dprint(FD_IO, "fio setup\n");
59 g = malloc(sizeof(struct gf_data));
61 log_err("malloc failed.\n");
64 g->fs = NULL; g->fd = NULL;
66 g->fs = glfs_new (opt->gf_vol);
68 log_err("glfs_new failed.\n");
71 glfs_set_logging (g->fs, "/tmp/fio_gfapi.log", 7);
73 r = glfs_set_volfile_server(g->fs, "tcp", opt->gf_brick, 0);
75 log_err("glfs_set_volfile_server failed.\n");
80 log_err("glfs_init failed. Is glusterd running on brick?\n");
84 r = glfs_lstat (g->fs, ".", &sb);
86 log_err("glfs_lstat failed.\n");
89 dprint(FD_FILE, "fio setup %p\n", g->fs);
103 static void fio_gf_cleanup(struct thread_data *td)
107 static int fio_gf_get_file_size(struct thread_data *td, struct fio_file *f)
111 struct gf_data *g = td->io_ops->data;
113 dprint(FD_FILE, "get file size %s\n", f->file_name);
119 if (fio_file_size_known(f))
122 ret = glfs_lstat (g->fs, f->file_name, &buf);
124 log_err("glfs_lstat failed.\n");
128 f->real_file_size = buf.st_size;
129 fio_file_set_size_known(f);
135 static int fio_gf_open_file(struct thread_data *td, struct fio_file *f)
140 struct gf_data *g = td->io_ops->data;
141 struct stat sb = {0, };
146 } else if (td_read(td)) {
152 dprint(FD_FILE, "fio file %s open mode %s td rw %s\n", f->file_name,
153 flags == O_RDONLY? "ro":"rw", td_read(td)? "read":"write");
154 g->fd = glfs_creat(g->fs, f->file_name, flags, 0644);
156 log_err("glfs_creat failed.\n");
159 /* file for read doesn't exist or shorter than required, create/extend it */
161 if (glfs_lstat (g->fs, f->file_name, &sb) || sb.st_size < f->real_file_size){
162 dprint(FD_FILE, "fio extend file %s from %ld to %ld\n", f->file_name, sb.st_size, f->real_file_size);
163 ret = glfs_ftruncate (g->fd, f->real_file_size);
165 log_err("failed fio extend file %s to %ld\n", f->file_name, f->real_file_size);
167 unsigned long long left;
172 /* fill the file, copied from extend_file */
173 b = malloc(td->o.max_bs[DDIR_WRITE]);
175 left = f->real_file_size;
176 while (left && !td->terminate) {
177 bs = td->o.max_bs[DDIR_WRITE];
181 fill_io_buffer(td, b, bs, bs);
183 r = glfs_write(g->fd, b, bs, 0);
184 dprint(FD_IO, "fio write %d of %ld file %s\n", r, f->real_file_size, f->file_name);
194 if (td->o.fill_device)
196 log_info("fio: ENOSPC on laying out "
200 td_verror(td, errno, "write");
202 td_verror(td, EIO, "write");
209 glfs_lseek(g->fd, 0, SEEK_SET);
212 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
213 unlink(f->file_name);
214 } else if (td->o.create_fsync) {
215 if (glfs_fsync(g->fd) < 0) {
216 dprint(FD_FILE, "failed to sync, close %s\n", f->file_name);
217 td_verror(td, errno, "fsync");
226 dprint(FD_FILE, "fio %p created %s\n", g->fs, f->file_name);
233 static int fio_gf_close_file(struct thread_data *td, struct fio_file *f)
236 struct gf_data *g = td->io_ops->data;
238 dprint(FD_FILE, "fd close %s\n", f->file_name);
240 if (g->fd && glfs_close(g->fd) < 0)
248 td->io_ops->data = NULL;
254 #define LAST_POS(f) ((f)->engine_data)
255 static int fio_gf_prep(struct thread_data *td, struct io_u *io_u)
257 struct fio_file *f = io_u->file;
258 struct gf_data *g = td->io_ops->data;
260 dprint(FD_FILE, "fio prep\n");
262 if (!ddir_rw(io_u->ddir))
265 if (LAST_POS(f) != -1ULL && LAST_POS(f) == io_u->offset)
268 if (glfs_lseek(g->fd, io_u->offset, SEEK_SET) < 0) {
269 td_verror(td, errno, "lseek");
276 static int fio_gf_queue(struct thread_data *td, struct io_u *io_u)
278 struct gf_data *g = td->io_ops->data;
281 dprint(FD_FILE, "fio queue len %lu\n", io_u->xfer_buflen);
282 fio_ro_check(td, io_u);
284 if (io_u->ddir == DDIR_READ)
285 ret = glfs_read(g->fd, io_u->xfer_buf, io_u->xfer_buflen, 0);
286 else if (io_u->ddir == DDIR_WRITE)
287 ret = glfs_write(g->fd, io_u->xfer_buf, io_u->xfer_buflen, 0);
289 log_err("unsupported operation.\n");
292 dprint(FD_FILE, "fio len %lu ret %d\n", io_u->xfer_buflen, ret);
293 if (io_u->file && ret >= 0 && ddir_rw(io_u->ddir))
294 LAST_POS(io_u->file) = io_u->offset + ret;
296 if (ret != (int) io_u->xfer_buflen) {
298 io_u->resid = io_u->xfer_buflen - ret;
300 return FIO_Q_COMPLETED;
306 log_err("IO failed.\n");
307 td_verror(td, io_u->error, "xfer");
310 return FIO_Q_COMPLETED;
314 static struct ioengine_ops ioengine = {
316 .version = FIO_IOOPS_VERSION,
317 .init = fio_gf_setup,
318 .cleanup = fio_gf_cleanup,
320 .queue = fio_gf_queue,
321 .open_file = fio_gf_open_file,
322 .close_file = fio_gf_close_file,
323 .get_file_size = fio_gf_get_file_size,
325 .option_struct_size = sizeof(struct gf_options),
326 .flags = FIO_SYNCIO | FIO_DISKLESSIO,
329 static void fio_init fio_gf_register(void)
331 register_ioengine(&ioengine);
334 static void fio_exit fio_gf_unregister(void)
336 unregister_ioengine(&ioengine);