2 * file/directory operations engine
4 * IO engine that doesn't do any IO, just operates files/directories
5 * and tracks the latency of the operation.
11 #include <sys/types.h>
15 #include "../optgroup.h"
16 #include "../oslib/statx.h"
19 UNKNOWN_OP_ENGINE = 0,
25 enum fio_ddir stat_ddir;
26 enum fio_engine op_engine;
29 struct filestat_options {
31 unsigned int stat_type;
35 FIO_FILESTAT_STAT = 1,
36 FIO_FILESTAT_LSTAT = 2,
37 FIO_FILESTAT_STATX = 3,
40 static struct fio_option options[] = {
45 .off1 = offsetof(struct filestat_options, stat_type),
46 .help = "Specify stat system call type to measure lookup/getattr performance",
50 .oval = FIO_FILESTAT_STAT,
51 .help = "Use stat(2)",
54 .oval = FIO_FILESTAT_LSTAT,
55 .help = "Use lstat(2)",
58 .oval = FIO_FILESTAT_STATX,
59 .help = "Use statx(2) if exists",
62 .category = FIO_OPT_C_ENGINE,
63 .group = FIO_OPT_G_FILESTAT,
70 static int setup_dirs(struct thread_data *td)
76 for_each_file(td, f, i) {
77 dprint(FD_FILE, "setup directory %s\n", f->file_name);
78 ret = fio_mkdir(f->file_name, 0700);
79 if ((ret && errno != EEXIST)) {
80 log_err("create directory %s failed with %d\n",
91 static int open_file(struct thread_data *td, struct fio_file *f)
93 struct timespec start;
94 int do_lat = !td->o.disable_lat;
96 dprint(FD_FILE, "fd open %s\n", f->file_name);
98 if (f->filetype != FIO_TYPE_FILE) {
99 log_err("fio: only files are supported\n");
102 if (!strcmp(f->file_name, "-")) {
103 log_err("fio: can't read/write to stdin/out\n");
108 fio_gettime(&start, NULL);
110 if (((struct fc_data *)td->io_ops_data)->op_engine == FILE_OP_ENGINE)
111 f->fd = open(f->file_name, O_CREAT|O_RDWR, 0600);
112 else if (((struct fc_data *)td->io_ops_data)->op_engine == DIR_OP_ENGINE)
113 f->fd = fio_mkdir(f->file_name, S_IFDIR);
115 log_err("fio: unknown file/directory operation engine\n");
120 char buf[FIO_VERROR_SIZE];
123 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
124 td_verror(td, e, buf);
129 struct fc_data *data = td->io_ops_data;
132 nsec = ntime_since_now(&start);
133 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
139 static int stat_file(struct thread_data *td, struct fio_file *f)
141 struct filestat_options *o = td->eo;
142 struct timespec start;
143 int do_lat = !td->o.disable_lat;
146 struct statx statxbuf;
151 dprint(FD_FILE, "fd stat %s\n", f->file_name);
153 if (f->filetype != FIO_TYPE_FILE) {
154 log_err("fio: only files are supported\n");
157 if (!strcmp(f->file_name, "-")) {
158 log_err("fio: can't read/write to stdin/out\n");
163 fio_gettime(&start, NULL);
165 switch (o->stat_type) {
166 case FIO_FILESTAT_STAT:
167 ret = stat(f->file_name, &statbuf);
169 case FIO_FILESTAT_LSTAT:
170 ret = lstat(f->file_name, &statbuf);
172 case FIO_FILESTAT_STATX:
174 abspath = realpath(f->file_name, NULL);
176 ret = statx(-1, abspath, 0, STATX_ALL, &statxbuf);
190 char buf[FIO_VERROR_SIZE];
193 snprintf(buf, sizeof(buf), "stat(%s) type=%u", f->file_name,
195 td_verror(td, e, buf);
200 struct fc_data *data = td->io_ops_data;
203 nsec = ntime_since_now(&start);
204 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
211 static int delete_file(struct thread_data *td, struct fio_file *f)
213 struct timespec start;
214 int do_lat = !td->o.disable_lat;
217 dprint(FD_FILE, "fd delete %s\n", f->file_name);
219 if (f->filetype != FIO_TYPE_FILE) {
220 log_err("fio: only files are supported\n");
223 if (!strcmp(f->file_name, "-")) {
224 log_err("fio: can't read/write to stdin/out\n");
229 fio_gettime(&start, NULL);
231 if (((struct fc_data *)td->io_ops_data)->op_engine == FILE_OP_ENGINE)
232 ret = unlink(f->file_name);
233 else if (((struct fc_data *)td->io_ops_data)->op_engine == DIR_OP_ENGINE)
234 ret = rmdir(f->file_name);
236 log_err("fio: unknown file/directory operation engine\n");
243 char buf[FIO_VERROR_SIZE];
246 snprintf(buf, sizeof(buf), "delete(%s)", f->file_name);
247 td_verror(td, e, buf);
252 struct fc_data *data = td->io_ops_data;
255 nsec = ntime_since_now(&start);
256 add_clat_sample(td, data->stat_ddir, nsec, 0, 0, 0, 0);
262 static int invalidate_do_nothing(struct thread_data *td, struct fio_file *f)
264 /* do nothing because file not opened */
268 static enum fio_q_status queue_io(struct thread_data *td, struct io_u *io_u)
270 return FIO_Q_COMPLETED;
274 * Ensure that we at least have a block size worth of IO to do for each
275 * file. If the job file has td->o.size < nr_files * block_size, then
276 * fio won't do anything.
278 static int get_file_size(struct thread_data *td, struct fio_file *f)
280 f->real_file_size = td_min_bs(td);
284 static int init(struct thread_data *td)
286 struct fc_data *data;
288 data = calloc(1, sizeof(*data));
291 data->stat_ddir = DDIR_READ;
292 else if (td_write(td))
293 data->stat_ddir = DDIR_WRITE;
295 data->op_engine = UNKNOWN_OP_ENGINE;
297 if (!strncmp(td->o.ioengine, "file", 4)) {
298 data->op_engine = FILE_OP_ENGINE;
299 dprint(FD_FILE, "Operate engine type: file\n");
301 if (!strncmp(td->o.ioengine, "dir", 3)) {
302 data->op_engine = DIR_OP_ENGINE;
303 dprint(FD_FILE, "Operate engine type: directory\n");
306 td->io_ops_data = data;
310 static void cleanup(struct thread_data *td)
312 struct fc_data *data = td->io_ops_data;
317 static int remove_dir(struct thread_data *td, struct fio_file *f)
319 dprint(FD_FILE, "remove directory %s\n", f->file_name);
320 return rmdir(f->file_name);
323 static struct ioengine_ops ioengine_filecreate = {
324 .name = "filecreate",
325 .version = FIO_IOOPS_VERSION,
329 .get_file_size = get_file_size,
330 .open_file = open_file,
331 .close_file = generic_close_file,
332 .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
333 FIO_NOSTATS | FIO_NOFILEHASH,
336 static struct ioengine_ops ioengine_filestat = {
338 .version = FIO_IOOPS_VERSION,
342 .invalidate = invalidate_do_nothing,
343 .get_file_size = generic_get_file_size,
344 .open_file = stat_file,
345 .flags = FIO_SYNCIO | FIO_FAKEIO |
346 FIO_NOSTATS | FIO_NOFILEHASH,
348 .option_struct_size = sizeof(struct filestat_options),
351 static struct ioengine_ops ioengine_filedelete = {
352 .name = "filedelete",
353 .version = FIO_IOOPS_VERSION,
355 .invalidate = invalidate_do_nothing,
358 .get_file_size = generic_get_file_size,
359 .open_file = delete_file,
360 .flags = FIO_SYNCIO | FIO_FAKEIO |
361 FIO_NOSTATS | FIO_NOFILEHASH,
364 static struct ioengine_ops ioengine_dircreate = {
366 .version = FIO_IOOPS_VERSION,
370 .get_file_size = get_file_size,
371 .open_file = open_file,
372 .close_file = generic_close_file,
373 .unlink_file = remove_dir,
374 .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
375 FIO_NOSTATS | FIO_NOFILEHASH,
378 static struct ioengine_ops ioengine_dirstat = {
380 .version = FIO_IOOPS_VERSION,
385 .invalidate = invalidate_do_nothing,
386 .get_file_size = generic_get_file_size,
387 .open_file = stat_file,
388 .unlink_file = remove_dir,
389 .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
390 FIO_NOSTATS | FIO_NOFILEHASH,
392 .option_struct_size = sizeof(struct filestat_options),
395 static struct ioengine_ops ioengine_dirdelete = {
397 .version = FIO_IOOPS_VERSION,
400 .invalidate = invalidate_do_nothing,
403 .get_file_size = get_file_size,
404 .open_file = delete_file,
405 .unlink_file = remove_dir,
406 .flags = FIO_DISKLESSIO | FIO_SYNCIO | FIO_FAKEIO |
407 FIO_NOSTATS | FIO_NOFILEHASH,
411 static void fio_init fio_fileoperations_register(void)
413 register_ioengine(&ioengine_filecreate);
414 register_ioengine(&ioengine_filestat);
415 register_ioengine(&ioengine_filedelete);
416 register_ioengine(&ioengine_dircreate);
417 register_ioengine(&ioengine_dirstat);
418 register_ioengine(&ioengine_dirdelete);
421 static void fio_exit fio_fileoperations_unregister(void)
423 unregister_ioengine(&ioengine_filecreate);
424 unregister_ioengine(&ioengine_filestat);
425 unregister_ioengine(&ioengine_filedelete);
426 unregister_ioengine(&ioengine_dircreate);
427 unregister_ioengine(&ioengine_dirstat);
428 unregister_ioengine(&ioengine_dirdelete);