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