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