ioengines: add helper for trims with async ioengines
[fio.git] / ioengines.c
CommitLineData
ebac4655
JA
1/*
2 * The io parts of the fio tool, includes workers for sync and mmap'ed
3 * io, as well as both posix and linux libaio support.
4 *
5 * sync io is implemented on top of aio.
6 *
7 * This is not really specific to fio, if the get_io_u/put_io_u and
8 * structures was pulled into this as well it would be a perfectly
9 * generic io engine that could be used for other projects.
10 *
11 */
ebac4655
JA
12#include <stdlib.h>
13#include <unistd.h>
5c4e1dbc 14#include <string.h>
2866c82d 15#include <dlfcn.h>
ecc314ba 16#include <fcntl.h>
0c6e7517 17#include <assert.h>
35300d5f
ES
18#include <sys/types.h>
19#include <dirent.h>
8c16d840 20
ebac4655 21#include "fio.h"
7c9b1bce 22#include "diskutil.h"
bfbdd35b 23#include "zbd.h"
ebac4655 24
01743ee1 25static FLIST_HEAD(engine_list);
5f350952 26
d46b4565
VF
27static inline bool async_ioengine_sync_trim(struct thread_data *td,
28 struct io_u *io_u)
29{
30 return td_ioengine_flagged(td, FIO_ASYNCIO_SYNC_TRIM) &&
31 io_u->ddir == DDIR_TRIM;
32}
33
abfd235a 34static bool check_engine_ops(struct thread_data *td, struct ioengine_ops *ops)
8c16d840 35{
5f350952 36 if (ops->version != FIO_IOOPS_VERSION) {
5ec10eaa
JA
37 log_err("bad ioops version %d (want %d)\n", ops->version,
38 FIO_IOOPS_VERSION);
1cc954ba 39 return true;
5f350952
JA
40 }
41
36167d82
JA
42 if (!ops->queue) {
43 log_err("%s: no queue handler\n", ops->name);
1cc954ba 44 return true;
36167d82
JA
45 }
46
47 /*
48 * sync engines only need a ->queue()
49 */
50 if (ops->flags & FIO_SYNCIO)
1cc954ba 51 return false;
5ec10eaa 52
abfd235a
JA
53 /*
54 * async engines aren't reliable with offload
55 */
695611a9 56 if ((td->o.io_submit_mode == IO_MODE_OFFLOAD) &&
8bfe330e 57 (ops->flags & FIO_NO_OFFLOAD)) {
abfd235a
JA
58 log_err("%s: can't be used with offloaded submit. Use a sync "
59 "engine\n", ops->name);
60 return true;
61 }
62
1cc954ba
JA
63 if (!ops->event || !ops->getevents) {
64 log_err("%s: no event/getevents handler\n", ops->name);
65 return true;
8c16d840 66 }
5ec10eaa 67
1cc954ba 68 return false;
8c16d840
JA
69}
70
5f350952 71void unregister_ioengine(struct ioengine_ops *ops)
ebac4655 72{
ee56ad50 73 dprint(FD_IO, "ioengine %s unregistered\n", ops->name);
adf075fa 74 flist_del_init(&ops->list);
5f350952
JA
75}
76
b2fdda43 77void register_ioengine(struct ioengine_ops *ops)
5f350952 78{
ee56ad50 79 dprint(FD_IO, "ioengine %s registered\n", ops->name);
01743ee1 80 flist_add_tail(&ops->list, &engine_list);
5f350952
JA
81}
82
83static struct ioengine_ops *find_ioengine(const char *name)
84{
85 struct ioengine_ops *ops;
01743ee1 86 struct flist_head *entry;
ebac4655 87
01743ee1
JA
88 flist_for_each(entry, &engine_list) {
89 ops = flist_entry(entry, struct ioengine_ops, list);
bc5b77a8 90 if (!strcmp(name, ops->name))
5f350952
JA
91 return ops;
92 }
93
94 return NULL;
95}
96
5a8a6a03
YK
97#ifdef CONFIG_DYNAMIC_ENGINES
98static void *dlopen_external(struct thread_data *td, const char *engine)
99{
100 char engine_path[PATH_MAX];
7dc1183d 101 void *dlhandle;
5a8a6a03 102
25b9b58e 103 sprintf(engine_path, "%s/fio-%s.so", FIO_EXT_ENG_DIR, engine);
5a8a6a03 104
48ff7df9 105 dprint(FD_IO, "dlopen external %s\n", engine_path);
7dc1183d
YK
106 dlhandle = dlopen(engine_path, RTLD_LAZY);
107 if (!dlhandle)
108 log_info("Engine %s not found; Either name is invalid, was not built, or fio-engine-%s package is missing.\n",
109 engine, engine);
110
111 return dlhandle;
5a8a6a03
YK
112}
113#else
114#define dlopen_external(td, engine) (NULL)
115#endif
116
5f350952
JA
117static struct ioengine_ops *dlopen_ioengine(struct thread_data *td,
118 const char *engine_lib)
119{
120 struct ioengine_ops *ops;
121 void *dlhandle;
122
8bdf7a84
JA
123 if (!strncmp(engine_lib, "linuxaio", 8) ||
124 !strncmp(engine_lib, "aio", 3))
125 engine_lib = "libaio";
126
48ff7df9 127 dprint(FD_IO, "dlopen engine %s\n", engine_lib);
ee56ad50 128
2866c82d
JA
129 dlerror();
130 dlhandle = dlopen(engine_lib, RTLD_LAZY);
d4dbaaa8 131 if (!dlhandle) {
5a8a6a03
YK
132 dlhandle = dlopen_external(td, engine_lib);
133 if (!dlhandle) {
134 td_vmsg(td, -1, dlerror(), "dlopen");
135 return NULL;
136 }
d4dbaaa8 137 }
8756e4d4 138
da51c050
JA
139 /*
140 * Unlike the included modules, external engines should have a
141 * non-static ioengine structure that we can reference.
142 */
0abea0b7
DM
143 ops = dlsym(dlhandle, engine_lib);
144 if (!ops)
145 ops = dlsym(dlhandle, "ioengine");
a8075704
DG
146
147 /*
148 * For some external engines (like C++ ones) it is not that trivial
149 * to provide a non-static ionengine structure that we can reference.
150 * Instead we call a method which allocates the required ioengine
151 * structure.
152 */
153 if (!ops) {
154 get_ioengine_t get_ioengine = dlsym(dlhandle, "get_ioengine");
155
156 if (get_ioengine)
157 get_ioengine(&ops);
158 }
159
d4dbaaa8 160 if (!ops) {
e1161c32 161 td_vmsg(td, -1, dlerror(), "dlsym");
d4dbaaa8
JA
162 dlclose(dlhandle);
163 return NULL;
164 }
8756e4d4 165
f6931a1d 166 ops->dlhandle = dlhandle;
5f350952
JA
167 return ops;
168}
169
044be36e 170static struct ioengine_ops *__load_ioengine(const char *engine)
97bb54c9 171{
97bb54c9
TK
172 /*
173 * linux libaio has alias names, so convert to what we want
174 */
8bdf7a84 175 if (!strncmp(engine, "linuxaio", 8) || !strncmp(engine, "aio", 3)) {
044be36e
BVA
176 dprint(FD_IO, "converting ioengine name: %s -> libaio\n",
177 engine);
178 engine = "libaio";
966fcbd4 179 }
97bb54c9
TK
180
181 dprint(FD_IO, "load ioengine %s\n", engine);
182 return find_ioengine(engine);
183}
184
4fedf59a 185struct ioengine_ops *load_ioengine(struct thread_data *td)
5f350952 186{
81647a9a 187 struct ioengine_ops *ops = NULL;
ba872a0b 188 const char *name;
81647a9a 189
ba872a0b
TK
190 /*
191 * Use ->ioengine_so_path if an external ioengine path is specified.
192 * In this case, ->ioengine is "external" which also means the prefix
193 * for external ioengines "external:" is properly used.
194 */
195 name = td->o.ioengine_so_path ?: td->o.ioengine;
196
197 /*
198 * Try to load ->ioengine first, and if failed try to dlopen(3) either
199 * ->ioengine or ->ioengine_so_path. This is redundant for an external
200 * ioengine with prefix, and also leaves the possibility of unexpected
201 * behavior (e.g. if the "external" ioengine exists), but we do this
202 * so as not to break job files not using the prefix.
203 */
204 ops = __load_ioengine(td->o.ioengine);
48ff7df9
ES
205
206 /* We do re-dlopen existing handles, for reference counting */
207 if (!ops || ops->dlhandle)
5f350952
JA
208 ops = dlopen_ioengine(td, name);
209
ba872a0b
TK
210 /*
211 * If ops is NULL, we failed to load ->ioengine, and also failed to
212 * dlopen(3) either ->ioengine or ->ioengine_so_path as a path.
213 */
5f350952
JA
214 if (!ops) {
215 log_err("fio: engine %s not loadable\n", name);
b902ceb5
JA
216 return NULL;
217 }
218
8c16d840
JA
219 /*
220 * Check that the required methods are there.
221 */
abfd235a 222 if (check_engine_ops(td, ops))
8c16d840 223 return NULL;
8c16d840 224
565e784d 225 return ops;
8756e4d4
JA
226}
227
de890a1e
SL
228/*
229 * For cleaning up an ioengine which never made it to init().
230 */
231void free_ioengine(struct thread_data *td)
8756e4d4 232{
6203763e
DP
233 assert(td != NULL && td->io_ops != NULL);
234
de890a1e 235 dprint(FD_IO, "free ioengine %s\n", td->io_ops->name);
ee56ad50 236
de890a1e
SL
237 if (td->eo && td->io_ops->options) {
238 options_free(td->io_ops->options, td->eo);
239 free(td->eo);
240 td->eo = NULL;
2992b059 241 }
b990b5c0 242
f6931a1d 243 if (td->io_ops->dlhandle) {
48ff7df9 244 dprint(FD_IO, "dlclose ioengine %s\n", td->io_ops->name);
f6931a1d 245 dlclose(td->io_ops->dlhandle);
9b50942e 246 }
5f350952 247
84585003 248 td->io_ops = NULL;
b990b5c0 249}
10ba535a 250
de890a1e
SL
251void close_ioengine(struct thread_data *td)
252{
253 dprint(FD_IO, "close ioengine %s\n", td->io_ops->name);
254
255 if (td->io_ops->cleanup) {
256 td->io_ops->cleanup(td);
565e784d 257 td->io_ops_data = NULL;
de890a1e
SL
258 }
259
260 free_ioengine(td);
261}
262
10ba535a
JA
263int td_io_prep(struct thread_data *td, struct io_u *io_u)
264{
ee56ad50 265 dprint_io_u(io_u, "prep");
7101d9c2
JA
266 fio_ro_check(td, io_u);
267
4d4e80f2 268 lock_file(td, io_u->file, io_u->ddir);
b2bd2bd9 269
2ba1c290
JA
270 if (td->io_ops->prep) {
271 int ret = td->io_ops->prep(td, io_u);
272
e5f9a813
RE
273 dprint(FD_IO, "prep: io_u %p: ret=%d\n", io_u, ret);
274
b2bd2bd9 275 if (ret)
4d4e80f2 276 unlock_file(td, io_u->file);
2ba1c290
JA
277 return ret;
278 }
10ba535a
JA
279
280 return 0;
281}
282
e7d2e616 283int td_io_getevents(struct thread_data *td, unsigned int min, unsigned int max,
1f440ece 284 const struct timespec *t)
10ba535a 285{
ee56ad50 286 int r = 0;
face81b2 287
a05d62b2
YR
288 /*
289 * For ioengine=rdma one side operation RDMA_WRITE or RDMA_READ,
290 * server side gets a message from the client
291 * side that the task is finished, and
292 * td->done is set to 1 after td_io_commit(). In this case,
293 * there is no need to reap complete event in server side.
294 */
295 if (td->done)
296 return 0;
297
ee56ad50
JA
298 if (min > 0 && td->io_ops->commit) {
299 r = td->io_ops->commit(td);
face81b2 300 if (r < 0)
ee56ad50 301 goto out;
face81b2 302 }
4950421a
JA
303 if (max > td->cur_depth)
304 max = td->cur_depth;
305 if (min > max)
306 max = min;
36167d82 307
ee56ad50 308 r = 0;
4950421a 309 if (max && td->io_ops->getevents)
ee56ad50
JA
310 r = td->io_ops->getevents(td, min, max, t);
311out:
422f9e4b
RM
312 if (r >= 0) {
313 /*
3fd9efbc 314 * Reflect that our submitted requests were retrieved with
422f9e4b
RM
315 * whatever OS async calls are in the underlying engine.
316 */
317 td->io_u_in_flight -= r;
838bc709 318 io_u_mark_complete(td, r);
422f9e4b 319 } else
7c639b14 320 td_verror(td, r, "get_events");
f3e11d05 321
ee56ad50
JA
322 dprint(FD_IO, "getevents: %d\n", r);
323 return r;
10ba535a
JA
324}
325
d3b07186 326enum fio_q_status td_io_queue(struct thread_data *td, struct io_u *io_u)
10ba535a 327{
a9da8ab2 328 const enum fio_ddir ddir = acct_ddir(io_u);
5fff9543 329 unsigned long long buflen = io_u->xfer_buflen;
d3b07186 330 enum fio_q_status ret;
7e77dd02 331
ee56ad50 332 dprint_io_u(io_u, "queue");
7101d9c2
JA
333 fio_ro_check(td, io_u);
334
0c6e7517 335 assert((io_u->flags & IO_U_F_FLIGHT) == 0);
1651e431 336 io_u_set(td, io_u, IO_U_F_FLIGHT);
817ae977
VF
337
338 /*
339 * If overlap checking was enabled in offload mode we
340 * can release this lock that was acquired when we
341 * started the overlap check because the IO_U_F_FLIGHT
342 * flag is now set
343 */
a38c70a8
BVA
344 if (td_offload_overlap(td)) {
345 int res = pthread_mutex_unlock(&overlap_check);
346 assert(res == 0);
347 }
0c6e7517 348
d6aed795 349 assert(fio_file_open(io_u->file));
3d7b485f 350
bcd5abfa
JA
351 /*
352 * If using a write iolog, store this entry.
353 */
354 log_io_u(td, io_u);
355
11786802
JA
356 io_u->error = 0;
357 io_u->resid = 0;
358
04ba61df 359 if (td_ioengine_flagged(td, FIO_SYNCIO) ||
d46b4565 360 async_ioengine_sync_trim(td, io_u)) {
12d9d841 361 if (fio_fill_issue_time(td))
9520ebb9 362 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
363
364 /*
365 * only used for iolog
366 */
367 if (td->o.read_iolog_file)
368 memcpy(&td->last_issue, &io_u->issue_time,
8b6a404c 369 sizeof(io_u->issue_time));
433afcb4
JA
370 }
371
b2a432bf 372
a9da8ab2 373 if (ddir_rw(ddir)) {
a7d01f02
JA
374 if (!(io_u->flags & IO_U_F_VER_LIST)) {
375 td->io_issues[ddir]++;
376 td->io_issue_bytes[ddir] += buflen;
377 }
50a8ce86 378 td->rate_io_issue_bytes[ddir] += buflen;
74d6277f 379 }
755200a3 380
7e77dd02 381 ret = td->io_ops->queue(td, io_u);
b2da58c4 382 zbd_queue_io_u(td, io_u, ret);
5aeb77df 383
4d4e80f2 384 unlock_file(td, io_u->file);
b2bd2bd9 385
a9da8ab2
JA
386 if (ret == FIO_Q_BUSY && ddir_rw(ddir)) {
387 td->io_issues[ddir]--;
388 td->io_issue_bytes[ddir] -= buflen;
50a8ce86 389 td->rate_io_issue_bytes[ddir] -= buflen;
871467d9 390 io_u_clear(td, io_u, IO_U_F_FLIGHT);
cd8a19e6
JA
391 }
392
39a43d34
JA
393 /*
394 * If an error was seen and the io engine didn't propagate it
395 * back to 'td', do so.
396 */
397 if (io_u->error && !td->error)
398 td_verror(td, io_u->error, "td_io_queue");
399
cb211682
JA
400 /*
401 * Add warning for O_DIRECT so that users have an easier time
402 * spotting potentially bad alignment. If this triggers for the first
403 * IO, then it's likely an alignment problem or because the host fs
404 * does not support O_DIRECT
405 */
ff58fced 406 if (io_u->error == EINVAL && td->io_issues[io_u->ddir & 1] == 1 &&
cb211682 407 td->o.odirect) {
214ac7e0 408
cb211682 409 log_info("fio: first direct IO errored. File system may not "
78d4a262
TK
410 "support direct IO, or iomem_align= is bad, or "
411 "invalid block size. Try setting direct=0.\n");
cb211682
JA
412 }
413
bfbdd35b
BVA
414 if (zbd_unaligned_write(io_u->error) &&
415 td->io_issues[io_u->ddir & 1] == 1 &&
416 td->o.zone_mode != ZONE_MODE_ZBD) {
417 log_info("fio: first I/O failed. If %s is a zoned block device, consider --zonemode=zbd\n",
418 io_u->file->file_name);
419 }
420
c0681c9d 421 if (!td->io_ops->commit) {
838bc709
JA
422 io_u_mark_submit(td, 1);
423 io_u_mark_complete(td, 1);
424 }
425
d8005759 426 if (ret == FIO_Q_COMPLETED) {
00615bfb
SW
427 if (ddir_rw(io_u->ddir) ||
428 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING)) {
d8005759
JA
429 io_u_mark_depth(td, 1);
430 td->ts.total_io_u[io_u->ddir]++;
6eaf09d6 431 }
d8005759 432 } else if (ret == FIO_Q_QUEUED) {
e6727cbd
JA
433 td->io_u_queued++;
434
00615bfb
SW
435 if (ddir_rw(io_u->ddir) ||
436 (ddir_sync(io_u->ddir) && td->runstate != TD_FSYNCING))
d8005759 437 td->ts.total_io_u[io_u->ddir]++;
d8005759 438
a80cb54b
BVA
439 if (td->io_u_queued >= td->o.iodepth_batch)
440 td_io_commit(td);
eb7c8ae2 441 }
cb5ab512 442
04ba61df 443 if (!td_ioengine_flagged(td, FIO_SYNCIO) &&
d46b4565 444 !async_ioengine_sync_trim(td, io_u)) {
12d9d841 445 if (fio_fill_issue_time(td))
9520ebb9 446 fio_gettime(&io_u->issue_time, NULL);
d0c15328
JA
447
448 /*
449 * only used for iolog
450 */
451 if (td->o.read_iolog_file)
452 memcpy(&td->last_issue, &io_u->issue_time,
8b6a404c 453 sizeof(io_u->issue_time));
433afcb4
JA
454 }
455
7e77dd02 456 return ret;
10ba535a 457}
8c16d840
JA
458
459int td_io_init(struct thread_data *td)
460{
eeb12160 461 int ret = 0;
8c16d840 462
eeb12160
JA
463 if (td->io_ops->init) {
464 ret = td->io_ops->init(td);
356ef1a1
TK
465 if (ret)
466 log_err("fio: io engine %s init failed.%s\n",
467 td->io_ops->name,
468 td->o.iodepth > 1 ?
469 " Perhaps try reducing io depth?" : "");
470 else
471 td->io_ops_init = 1;
7c973896
JA
472 if (!td->error)
473 td->error = ret;
eeb12160
JA
474 }
475
476 return ret;
8c16d840 477}
755200a3 478
a80cb54b 479void td_io_commit(struct thread_data *td)
755200a3 480{
f3e11d05
JA
481 int ret;
482
ee56ad50
JA
483 dprint(FD_IO, "calling ->commit(), depth %d\n", td->cur_depth);
484
d8005759 485 if (!td->cur_depth || !td->io_u_queued)
a80cb54b 486 return;
cb5ab512 487
3fd9efbc 488 io_u_mark_depth(td, td->io_u_queued);
d8005759 489
f3e11d05
JA
490 if (td->io_ops->commit) {
491 ret = td->io_ops->commit(td);
492 if (ret)
493 td_verror(td, -ret, "io commit");
494 }
3fd9efbc 495
422f9e4b
RM
496 /*
497 * Reflect that events were submitted as async IO requests.
498 */
499 td->io_u_in_flight += td->io_u_queued;
500 td->io_u_queued = 0;
755200a3 501}
b5af8293
JA
502
503int td_io_open_file(struct thread_data *td, struct fio_file *f)
504{
230f33fb
AK
505 if (fio_file_closing(f)) {
506 /*
507 * Open translates to undo closing.
508 */
509 fio_file_clear_closing(f);
510 get_file(f);
511 return 0;
512 }
22a57ba8
JA
513 assert(!fio_file_open(f));
514 assert(f->fd == -1);
966fcbd4 515 assert(td->io_ops->open_file);
22a57ba8 516
413d6693
JA
517 if (td->io_ops->open_file(td, f)) {
518 if (td->error == EINVAL && td->o.odirect)
519 log_err("fio: destination does not support O_DIRECT\n");
5ec10eaa
JA
520 if (td->error == EMFILE) {
521 log_err("fio: try reducing/setting openfiles (failed"
522 " at %u of %u)\n", td->nr_open_files,
523 td->o.nr_files);
524 }
413d6693 525
22a57ba8
JA
526 assert(f->fd == -1);
527 assert(!fio_file_open(f));
413d6693
JA
528 return 1;
529 }
530
33c48814 531 fio_file_reset(td, f);
d6aed795
JA
532 fio_file_set_open(f);
533 fio_file_clear_closing(f);
c97bd0fa 534 disk_util_inc(f->du);
d5707a35
JA
535
536 td->nr_open_files++;
537 get_file(f);
538
66159828
JA
539 if (f->filetype == FIO_TYPE_PIPE) {
540 if (td_random(td)) {
541 log_err("fio: can't seek on pipes (no random io)\n");
542 goto err;
543 }
544 }
545
9b87f09b 546 if (td_ioengine_flagged(td, FIO_DISKLESSIO))
413d6693
JA
547 goto done;
548
549 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
550 goto err;
551
ecb2083d 552 if (td->o.fadvise_hint != F_ADV_NONE &&
686fbd31 553 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
413d6693
JA
554 int flags;
555
ecb2083d
JA
556 if (td->o.fadvise_hint == F_ADV_TYPE) {
557 if (td_random(td))
558 flags = POSIX_FADV_RANDOM;
559 else
560 flags = POSIX_FADV_SEQUENTIAL;
561 } else if (td->o.fadvise_hint == F_ADV_RANDOM)
413d6693 562 flags = POSIX_FADV_RANDOM;
ecb2083d 563 else if (td->o.fadvise_hint == F_ADV_SEQUENTIAL)
413d6693 564 flags = POSIX_FADV_SEQUENTIAL;
ecb2083d
JA
565 else {
566 log_err("fio: unknown fadvise type %d\n",
567 td->o.fadvise_hint);
568 flags = POSIX_FADV_NORMAL;
569 }
413d6693 570
ecc314ba 571 if (posix_fadvise(f->fd, f->file_offset, f->io_size, flags) < 0) {
825b0a75
JA
572 if (!fio_did_warn(FIO_WARN_FADVISE))
573 log_err("fio: fadvise hint failed\n");
413d6693 574 }
7bb48f84 575 }
ae8e559e
JA
576#ifdef FIO_HAVE_WRITE_HINT
577 if (fio_option_is_set(&td->o, write_hint) &&
686fbd31 578 (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_FILE)) {
ae8e559e 579 uint64_t hint = td->o.write_hint;
bd553af6 580 int cmd;
37659335 581
bd553af6
JA
582 /*
583 * For direct IO, we just need/want to set the hint on
584 * the file descriptor. For buffered IO, we need to set
585 * it on the inode.
586 */
587 if (td->o.odirect)
588 cmd = F_SET_FILE_RW_HINT;
589 else
590 cmd = F_SET_RW_HINT;
591
592 if (fcntl(f->fd, cmd, &hint) < 0) {
ae8e559e 593 td_verror(td, errno, "fcntl write hint");
37659335
JA
594 goto err;
595 }
596 }
597#endif
a978ba68 598
47534cda
TK
599 if (td->o.odirect && !OS_O_DIRECT && fio_set_directio(td, f))
600 goto err;
e116f2b9 601
413d6693 602done:
f29b25a3 603 log_file(td, f, FIO_LOG_OPEN_FILE);
413d6693
JA
604 return 0;
605err:
c97bd0fa 606 disk_util_dec(f->du);
b284075a
JA
607 if (td->io_ops->close_file)
608 td->io_ops->close_file(td, f);
7bb48f84 609 return 1;
b5af8293
JA
610}
611
6977bcd0 612int td_io_close_file(struct thread_data *td, struct fio_file *f)
b5af8293 613{
d6aed795 614 if (!fio_file_closing(f))
f29b25a3
JA
615 log_file(td, f, FIO_LOG_CLOSE_FILE);
616
0ad920e7
JA
617 /*
618 * mark as closing, do real close when last io on it has completed
619 */
d6aed795 620 fio_file_set_closing(f);
0ad920e7 621
6977bcd0 622 return put_file(td, f);
b5af8293 623}
df9c26b1 624
38ef9c90
CF
625int td_io_unlink_file(struct thread_data *td, struct fio_file *f)
626{
627 if (td->io_ops->unlink_file)
628 return td->io_ops->unlink_file(td, f);
2442c935
JA
629 else {
630 int ret;
631
632 ret = unlink(f->file_name);
633 if (ret < 0)
634 return errno;
635
636 return 0;
637 }
38ef9c90
CF
638}
639
df9c26b1
JA
640int td_io_get_file_size(struct thread_data *td, struct fio_file *f)
641{
642 if (!td->io_ops->get_file_size)
643 return 0;
644
645 return td->io_ops->get_file_size(td, f);
646}
44f29692 647
35300d5f
ES
648#ifdef CONFIG_DYNAMIC_ENGINES
649/* Load all dynamic engines in FIO_EXT_ENG_DIR for enghelp command */
650static void
651fio_load_dynamic_engines(struct thread_data *td)
652{
653 DIR *dirhandle = NULL;
654 struct dirent *dirent = NULL;
655 char engine_path[PATH_MAX];
656
657 dirhandle = opendir(FIO_EXT_ENG_DIR);
658 if (!dirhandle)
659 return;
660
661 while ((dirent = readdir(dirhandle)) != NULL) {
662 if (!strcmp(dirent->d_name, ".") ||
663 !strcmp(dirent->d_name, ".."))
664 continue;
665
666 sprintf(engine_path, "%s/%s", FIO_EXT_ENG_DIR, dirent->d_name);
667 dlopen_ioengine(td, engine_path);
668 }
669
670 closedir(dirhandle);
671}
672#else
673#define fio_load_dynamic_engines(td) do { } while (0)
674#endif
675
de890a1e
SL
676int fio_show_ioengine_help(const char *engine)
677{
678 struct flist_head *entry;
d04f1d5b 679 struct thread_data td;
755dcbbd 680 struct ioengine_ops *io_ops;
de890a1e
SL
681 char *sep;
682 int ret = 1;
683
35300d5f
ES
684 memset(&td, 0, sizeof(struct thread_data));
685
de890a1e
SL
686 if (!engine || !*engine) {
687 log_info("Available IO engines:\n");
35300d5f 688 fio_load_dynamic_engines(&td);
de890a1e 689 flist_for_each(entry, &engine_list) {
755dcbbd
TK
690 io_ops = flist_entry(entry, struct ioengine_ops, list);
691 log_info("\t%s\n", io_ops->name);
de890a1e
SL
692 }
693 return 0;
694 }
695 sep = strchr(engine, ',');
696 if (sep) {
697 *sep = 0;
698 sep++;
699 }
700
d04f1d5b 701 td.o.ioengine = (char *)engine;
2459bd33 702 td.io_ops = load_ioengine(&td);
d04f1d5b 703
2459bd33 704 if (!td.io_ops) {
de890a1e
SL
705 log_info("IO engine %s not found\n", engine);
706 return 1;
707 }
708
2459bd33
VF
709 if (td.io_ops->options)
710 ret = show_cmd_help(td.io_ops->options, sep);
de890a1e 711 else
2459bd33 712 log_info("IO engine %s has no options\n", td.io_ops->name);
de890a1e 713
d04f1d5b 714 free_ioengine(&td);
de890a1e
SL
715 return ret;
716}