Windows mkdir() fix
[fio.git] / filesetup.c
CommitLineData
53cdc686
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
bbf6b540 5#include <dirent.h>
d74ac843 6#include <libgen.h>
53cdc686
JA
7#include <sys/stat.h>
8#include <sys/mman.h>
bbf6b540 9#include <sys/types.h>
53cdc686
JA
10
11#include "fio.h"
f17c4392 12#include "smalloc.h"
4906e0b5 13#include "filehash.h"
bcbfeefa 14#include "options.h"
ecc314ba 15#include "os/os.h"
2316296a 16#include "hash.h"
7ebd796f 17#include "lib/axmap.h"
53cdc686 18
97ac992c 19#ifdef CONFIG_LINUX_FALLOCATE
a596f047
EG
20#include <linux/falloc.h>
21#endif
22
7172cfe8
JA
23static int root_warn;
24
bcbfeefa
CE
25static FLIST_HEAD(filename_list);
26
bf651609
TK
27/*
28 * List entry for filename_list
29 */
30struct file_name {
31 struct flist_head list;
32 char *filename;
33};
34
c592b9fe
JA
35static inline void clear_error(struct thread_data *td)
36{
37 td->error = 0;
38 td->verror[0] = '\0';
39}
40
2c3e17be
SW
41static inline int native_fallocate(struct thread_data *td, struct fio_file *f)
42{
43 bool success;
44
45 success = fio_fallocate(f, 0, f->real_file_size);
46 dprint(FD_FILE, "native fallocate of file %s size %llu was "
47 "%ssuccessful\n", f->file_name,
48 (unsigned long long) f->real_file_size,
49 !success ? "un": "");
50
51 if (success)
52 return 0;
53
54 if (errno == ENOSYS)
55 dprint(FD_FILE, "native fallocate is not implemented\n");
56
57 return -1;
58}
59
005eb303
JA
60static void fallocate_file(struct thread_data *td, struct fio_file *f)
61{
62 int r;
63
64 if (td->o.fill_device)
65 return;
66
005eb303 67 switch (td->o.fallocate_mode) {
2c3e17be
SW
68 case FIO_FALLOCATE_NATIVE:
69 r = native_fallocate(td, f);
f271a3f2 70 if (r != 0 && errno != ENOSYS)
2c3e17be
SW
71 log_err("fio: native_fallocate call failed: %s\n",
72 strerror(errno));
73 break;
005eb303
JA
74 case FIO_FALLOCATE_NONE:
75 break;
2c3e17be 76#ifdef CONFIG_POSIX_FALLOCATE
005eb303
JA
77 case FIO_FALLOCATE_POSIX:
78 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
79 f->file_name,
80 (unsigned long long) f->real_file_size);
81
82 r = posix_fallocate(f->fd, 0, f->real_file_size);
83 if (r > 0)
84 log_err("fio: posix_fallocate fails: %s\n", strerror(r));
85 break;
2c3e17be 86#endif /* CONFIG_POSIX_FALLOCATE */
005eb303
JA
87#ifdef CONFIG_LINUX_FALLOCATE
88 case FIO_FALLOCATE_KEEP_SIZE:
89 dprint(FD_FILE, "fallocate(FALLOC_FL_KEEP_SIZE) "
90 "file %s size %llu\n", f->file_name,
91 (unsigned long long) f->real_file_size);
92
93 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, f->real_file_size);
94 if (r != 0)
95 td_verror(td, errno, "fallocate");
96
97 break;
98#endif /* CONFIG_LINUX_FALLOCATE */
99 default:
100 log_err("fio: unknown fallocate mode: %d\n", td->o.fallocate_mode);
101 assert(0);
102 }
005eb303
JA
103}
104
3baddf24
JA
105/*
106 * Leaves f->fd open on success, caller must close
107 */
7bb48f84 108static int extend_file(struct thread_data *td, struct fio_file *f)
25205e97 109{
005eb303 110 int new_layout = 0, unlink_file = 0, flags;
25205e97 111 unsigned long long left;
323876fb 112 unsigned int bs;
f3e1eb23 113 char *b = NULL;
b2a15192 114
4241ea8f
JA
115 if (read_only) {
116 log_err("fio: refusing extend of file due to read-only\n");
117 return 0;
118 }
119
507a702f
JA
120 /*
121 * check if we need to lay the file out complete again. fio
122 * does that for operations involving reads, or for writes
123 * where overwrite is set
124 */
1417daeb
JA
125 if (td_read(td) ||
126 (td_write(td) && td->o.overwrite && !td->o.file_append) ||
9b87f09b 127 (td_write(td) && td_ioengine_flagged(td, FIO_NOEXTEND)))
507a702f 128 new_layout = 1;
1417daeb 129 if (td_write(td) && !td->o.overwrite && !td->o.file_append)
ea443657 130 unlink_file = 1;
507a702f 131
6ae1f57f 132 if (unlink_file || new_layout) {
2442c935
JA
133 int ret;
134
bd199f2b 135 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
2442c935
JA
136
137 ret = td_io_unlink_file(td, f);
138 if (ret != 0 && ret != ENOENT) {
7bb48f84
JA
139 td_verror(td, errno, "unlink");
140 return 1;
141 }
142 }
143
2378826d
JA
144 flags = O_WRONLY;
145 if (td->o.allow_create)
146 flags |= O_CREAT;
507a702f
JA
147 if (new_layout)
148 flags |= O_TRUNC;
149
9c0f3f32
BC
150#ifdef WIN32
151 flags |= _O_BINARY;
152#endif
153
ee56ad50 154 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
507a702f 155 f->fd = open(f->file_name, flags, 0644);
53cdc686 156 if (f->fd < 0) {
2378826d
JA
157 int err = errno;
158
159 if (err == ENOENT && !td->o.allow_create)
160 log_err("fio: file creation disallowed by "
161 "allow_file_create=0\n");
323876fb 162 else
2378826d 163 td_verror(td, err, "open");
53cdc686
JA
164 return 1;
165 }
166
005eb303 167 fallocate_file(td, f);
9b836561 168
79591fa9
TK
169 /*
170 * If our jobs don't require regular files initially, we're done.
171 */
fc74ac1d
SL
172 if (!new_layout)
173 goto done;
174
5e0074c2
JA
175 /*
176 * The size will be -1ULL when fill_device is used, so don't truncate
177 * or fallocate this file, just write it
178 */
179 if (!td->o.fill_device) {
180 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
4b91ee8f 181 (unsigned long long) f->real_file_size);
5e0074c2 182 if (ftruncate(f->fd, f->real_file_size) == -1) {
3cd4c66f
J
183 if (errno != EFBIG) {
184 td_verror(td, errno, "ftruncate");
185 goto err;
186 }
5e0074c2 187 }
5e0074c2 188 }
40f8298c 189
82d8de31
TK
190 left = f->real_file_size;
191 bs = td->o.max_bs[DDIR_WRITE];
192 if (bs > left)
193 bs = left;
194
323876fb 195 b = malloc(bs);
84af30a1 196 if (!b) {
323876fb 197 td_verror(td, errno, "malloc");
84af30a1
TK
198 goto err;
199 }
53cdc686 200
53cdc686 201 while (left && !td->terminate) {
005eb303
JA
202 ssize_t r;
203
53cdc686
JA
204 if (bs > left)
205 bs = left;
206
cc86c395
JA
207 fill_io_buffer(td, b, bs, bs);
208
53cdc686
JA
209 r = write(f->fd, b, bs);
210
5e0074c2
JA
211 if (r > 0) {
212 left -= r;
53cdc686
JA
213 continue;
214 } else {
5e0074c2
JA
215 if (r < 0) {
216 int __e = errno;
217
218 if (__e == ENOSPC) {
219 if (td->o.fill_device)
220 break;
221 log_info("fio: ENOSPC on laying out "
222 "file, stopping\n");
223 break;
224 }
e1161c32 225 td_verror(td, errno, "write");
5e0074c2 226 } else
e1161c32 227 td_verror(td, EIO, "write");
53cdc686
JA
228
229 break;
230 }
231 }
232
ffdfabd4
JA
233 if (td->terminate) {
234 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
38ef9c90 235 td_io_unlink_file(td, f);
ffdfabd4 236 } else if (td->o.create_fsync) {
98e1ac4e
JA
237 if (fsync(f->fd) < 0) {
238 td_verror(td, errno, "fsync");
239 goto err;
240 }
241 }
0d1cd207 242 if (td->o.fill_device && !td_write(td)) {
d6aed795 243 fio_file_clear_size_known(f);
5e0074c2
JA
244 if (td_io_get_file_size(td, f))
245 goto err;
246 if (f->io_size > f->real_file_size)
247 f->io_size = f->real_file_size;
248 }
53cdc686 249
323876fb 250 free(b);
507a702f 251done:
53cdc686
JA
252 return 0;
253err:
254 close(f->fd);
255 f->fd = -1;
f3e1eb23 256 if (b)
323876fb 257 free(b);
53cdc686
JA
258 return 1;
259}
260
afad68f7
ZY
261static int pre_read_file(struct thread_data *td, struct fio_file *f)
262{
ef799a64 263 int ret = 0, r, did_open = 0, old_runstate;
afad68f7
ZY
264 unsigned long long left;
265 unsigned int bs;
266 char *b;
267
736f1188
TK
268 if (td_ioengine_flagged(td, FIO_PIPEIO) ||
269 td_ioengine_flagged(td, FIO_NOIO))
9c0d2241
JA
270 return 0;
271
e658078b
TK
272 if (f->filetype == FIO_TYPE_CHAR)
273 return 0;
274
d6aed795 275 if (!fio_file_open(f)) {
b0f65863
JA
276 if (td->io_ops->open_file(td, f)) {
277 log_err("fio: cannot pre-read, failed to open file\n");
278 return 1;
279 }
280 did_open = 1;
281 }
282
8edd973d 283 old_runstate = td_bump_runstate(td, TD_PRE_READING);
b0f65863 284
82d8de31 285 left = f->io_size;
afad68f7 286 bs = td->o.max_bs[DDIR_READ];
82d8de31
TK
287 if (bs > left)
288 bs = left;
289
afad68f7 290 b = malloc(bs);
84af30a1
TK
291 if (!b) {
292 td_verror(td, errno, "malloc");
293 ret = 1;
294 goto error;
295 }
afad68f7
ZY
296 memset(b, 0, bs);
297
ef799a64
JA
298 if (lseek(f->fd, f->file_offset, SEEK_SET) < 0) {
299 td_verror(td, errno, "lseek");
300 log_err("fio: failed to lseek pre-read file\n");
301 ret = 1;
302 goto error;
303 }
304
afad68f7
ZY
305 while (left && !td->terminate) {
306 if (bs > left)
307 bs = left;
308
309 r = read(f->fd, b, bs);
310
311 if (r == (int) bs) {
312 left -= bs;
313 continue;
314 } else {
315 td_verror(td, EIO, "pre_read");
316 break;
317 }
318 }
319
ef799a64 320error:
8edd973d 321 td_restore_runstate(td, old_runstate);
b0f65863
JA
322
323 if (did_open)
324 td->io_ops->close_file(td, f);
ef799a64 325
afad68f7 326 free(b);
ef799a64 327 return ret;
afad68f7
ZY
328}
329
a3f001f5 330unsigned long long get_rand_file_size(struct thread_data *td)
9c60ce64 331{
dc873b6f 332 unsigned long long ret, sized;
c3546b53 333 uint64_t frand_max;
1294c3ec 334 unsigned long r;
9c60ce64 335
c3546b53 336 frand_max = rand_max(&td->file_size_state);
d6b72507 337 r = __rand(&td->file_size_state);
54a21917 338 sized = td->o.file_size_high - td->o.file_size_low;
c3546b53 339 ret = (unsigned long long) ((double) sized * (r / (frand_max + 1.0)));
5ec10eaa 340 ret += td->o.file_size_low;
2dc1bbeb 341 ret -= (ret % td->o.rw_min_bs);
9c60ce64
JA
342 return ret;
343}
344
53cdc686
JA
345static int file_size(struct thread_data *td, struct fio_file *f)
346{
347 struct stat st;
348
df9c26b1 349 if (stat(f->file_name, &st) == -1) {
7bb48f84
JA
350 td_verror(td, errno, "fstat");
351 return 1;
352 }
53cdc686 353
7bb48f84 354 f->real_file_size = st.st_size;
53cdc686
JA
355 return 0;
356}
357
358static int bdev_size(struct thread_data *td, struct fio_file *f)
359{
9b836561 360 unsigned long long bytes = 0;
53cdc686
JA
361 int r;
362
df9c26b1
JA
363 if (td->io_ops->open_file(td, f)) {
364 log_err("fio: failed opening blockdev %s for size check\n",
365 f->file_name);
366 return 1;
367 }
368
ecc314ba 369 r = blockdev_size(f, &bytes);
53cdc686 370 if (r) {
e1161c32 371 td_verror(td, r, "blockdev_size");
df9c26b1 372 goto err;
53cdc686
JA
373 }
374
7ab077ab
JA
375 if (!bytes) {
376 log_err("%s: zero sized block device?\n", f->file_name);
df9c26b1 377 goto err;
7ab077ab
JA
378 }
379
53cdc686 380 f->real_file_size = bytes;
22a57ba8 381 td->io_ops->close_file(td, f);
53cdc686 382 return 0;
df9c26b1
JA
383err:
384 td->io_ops->close_file(td, f);
385 return 1;
53cdc686
JA
386}
387
4ccdccd1
JA
388static int char_size(struct thread_data *td, struct fio_file *f)
389{
390#ifdef FIO_HAVE_CHARDEV_SIZE
9b836561 391 unsigned long long bytes = 0;
4ccdccd1
JA
392 int r;
393
394 if (td->io_ops->open_file(td, f)) {
b3ec877c 395 log_err("fio: failed opening chardev %s for size check\n",
4ccdccd1
JA
396 f->file_name);
397 return 1;
398 }
399
ecc314ba 400 r = chardev_size(f, &bytes);
4ccdccd1
JA
401 if (r) {
402 td_verror(td, r, "chardev_size");
403 goto err;
404 }
405
406 if (!bytes) {
407 log_err("%s: zero sized char device?\n", f->file_name);
408 goto err;
409 }
410
411 f->real_file_size = bytes;
412 td->io_ops->close_file(td, f);
413 return 0;
414err:
415 td->io_ops->close_file(td, f);
416 return 1;
417#else
418 f->real_file_size = -1ULL;
419 return 0;
420#endif
421}
422
53cdc686
JA
423static int get_file_size(struct thread_data *td, struct fio_file *f)
424{
425 int ret = 0;
426
d6aed795 427 if (fio_file_size_known(f))
409b3417
JA
428 return 0;
429
7bb48f84
JA
430 if (f->filetype == FIO_TYPE_FILE)
431 ret = file_size(td, f);
686fbd31 432 else if (f->filetype == FIO_TYPE_BLOCK)
53cdc686 433 ret = bdev_size(td, f);
4ccdccd1
JA
434 else if (f->filetype == FIO_TYPE_CHAR)
435 ret = char_size(td, f);
0f34169a
TK
436 else
437 f->real_file_size = -1ULL;
53cdc686 438
79591fa9
TK
439 /*
440 * Leave ->real_file_size with 0 since it could be expectation
441 * of initial setup for regular files.
442 */
a0cb220b 443 if (ret)
53cdc686
JA
444 return ret;
445
0f34169a
TK
446 /*
447 * If ->real_file_size is -1, a conditional for the message
448 * "offset extends end" is always true, but it makes no sense,
449 * so just return the same value here.
450 */
451 if (f->real_file_size == -1ULL) {
452 log_info("%s: failed to get file size of %s\n", td->o.name,
453 f->file_name);
454 return 1;
455 }
456
457 if (td->o.start_offset && f->file_offset == 0)
458 dprint(FD_FILE, "offset of file %s not initialized yet\n",
459 f->file_name);
957c81b9
TK
460 /*
461 * ->file_offset normally hasn't been initialized yet, so this
0f34169a 462 * is basically always false.
957c81b9 463 */
53cdc686 464 if (f->file_offset > f->real_file_size) {
0f2152c1 465 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
4e0a8fa2
JA
466 (unsigned long long) f->file_offset,
467 (unsigned long long) f->real_file_size);
53cdc686
JA
468 return 1;
469 }
470
d6aed795 471 fio_file_set_size_known(f);
53cdc686
JA
472 return 0;
473}
474
3baddf24
JA
475static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
476 unsigned long long off,
477 unsigned long long len)
e5b401d4 478{
942a851a 479 int errval = 0, ret = 0;
e5b401d4 480
c8931876
JA
481#ifdef CONFIG_ESX
482 return 0;
483#endif
484
5e0074c2 485 if (len == -1ULL)
3baddf24
JA
486 len = f->io_size;
487 if (off == -1ULL)
488 off = f->file_offset;
ee56ad50 489
0d1cd207
JA
490 if (len == -1ULL || off == -1ULL)
491 return 0;
492
942a851a 493 if (td->io_ops->invalidate) {
4499dbef
TK
494 dprint(FD_IO, "invalidate %s cache %s\n", td->io_ops->name,
495 f->file_name);
d9b100fc 496 ret = td->io_ops->invalidate(td, f);
942a851a 497 if (ret < 0)
afd118db 498 errval = -ret;
942a851a 499 } else if (f->filetype == FIO_TYPE_FILE) {
4499dbef
TK
500 dprint(FD_IO, "declare unneeded cache %s: %llu/%llu\n",
501 f->file_name, off, len);
ecc314ba 502 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
0b139881 503 if (ret)
942a851a 504 errval = ret;
686fbd31 505 } else if (f->filetype == FIO_TYPE_BLOCK) {
2b9f9141
KR
506 int retry_count = 0;
507
4499dbef 508 dprint(FD_IO, "drop page cache %s\n", f->file_name);
ecc314ba 509 ret = blockdev_invalidate_cache(f);
2b9f9141
KR
510 while (ret < 0 && errno == EAGAIN && retry_count++ < 25) {
511 /*
512 * Linux multipath devices reject ioctl while
513 * the maps are being updated. That window can
514 * last tens of milliseconds; we'll try up to
515 * a quarter of a second.
516 */
517 usleep(10000);
518 ret = blockdev_invalidate_cache(f);
519 }
7e0e25c9 520 if (ret < 0 && errno == EACCES && geteuid()) {
7172cfe8 521 if (!root_warn) {
5ec10eaa
JA
522 log_err("fio: only root may flush block "
523 "devices. Cache flush bypassed!\n");
7172cfe8
JA
524 root_warn = 1;
525 }
7e0e25c9
JA
526 ret = 0;
527 }
942a851a
JA
528 if (ret < 0)
529 errval = errno;
4499dbef
TK
530 } else if (f->filetype == FIO_TYPE_CHAR ||
531 f->filetype == FIO_TYPE_PIPE) {
532 dprint(FD_IO, "invalidate not supported %s\n", f->file_name);
e5b401d4 533 ret = 0;
4499dbef 534 }
e5b401d4 535
dfe11fd1
JA
536 /*
537 * Cache flushing isn't a fatal condition, and we know it will
538 * happen on some platforms where we don't have the proper
539 * function to flush eg block device caches. So just warn and
540 * continue on our way.
541 */
942a851a 542 if (errval)
22de5d77
TK
543 log_info("fio: cache invalidation of %s failed: %s\n",
544 f->file_name, strerror(errval));
e5b401d4 545
dfe11fd1 546 return 0;
3baddf24
JA
547
548}
549
550int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
551{
d6aed795 552 if (!fio_file_open(f))
a5fb461f
JA
553 return 0;
554
5e0074c2 555 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
e5b401d4
JA
556}
557
6977bcd0 558int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 559{
6977bcd0
JA
560 int ret = 0;
561
ee56ad50 562 dprint(FD_FILE, "fd close %s\n", f->file_name);
4906e0b5
JA
563
564 remove_file_hash(f);
565
6977bcd0
JA
566 if (close(f->fd) < 0)
567 ret = errno;
568
b5af8293 569 f->fd = -1;
e6c4d732
JA
570
571 if (f->shadow_fd != -1) {
572 close(f->shadow_fd);
573 f->shadow_fd = -1;
574 }
575
710bf9c5 576 f->engine_pos = 0;
6977bcd0 577 return ret;
53cdc686
JA
578}
579
1ccc6dc7 580int file_lookup_open(struct fio_file *f, int flags)
53cdc686 581{
29c1349f 582 struct fio_file *__f;
4d4e80f2
JA
583 int from_hash;
584
585 __f = lookup_file_hash(f->file_name);
586 if (__f) {
9efef3c4 587 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
4d4e80f2 588 f->lock = __f->lock;
4d4e80f2
JA
589 from_hash = 1;
590 } else {
9efef3c4 591 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
4d4e80f2
JA
592 from_hash = 0;
593 }
594
9c0f3f32
BC
595#ifdef WIN32
596 flags |= _O_BINARY;
597#endif
598
e8670ef8 599 f->fd = open(f->file_name, flags, 0600);
4d4e80f2
JA
600 return from_hash;
601}
602
e6c4d732
JA
603static int file_close_shadow_fds(struct thread_data *td)
604{
605 struct fio_file *f;
606 int num_closed = 0;
607 unsigned int i;
608
609 for_each_file(td, f, i) {
610 if (f->shadow_fd == -1)
611 continue;
612
613 close(f->shadow_fd);
614 f->shadow_fd = -1;
615 num_closed++;
616 }
617
618 return num_closed;
619}
620
4d4e80f2
JA
621int generic_open_file(struct thread_data *td, struct fio_file *f)
622{
66159828 623 int is_std = 0;
53cdc686 624 int flags = 0;
29c1349f 625 int from_hash = 0;
53cdc686 626
ee56ad50
JA
627 dprint(FD_FILE, "fd open %s\n", f->file_name);
628
66159828
JA
629 if (!strcmp(f->file_name, "-")) {
630 if (td_rw(td)) {
631 log_err("fio: can't read/write to stdin/out\n");
632 return 1;
633 }
634 is_std = 1;
ce98fa66
JA
635
636 /*
637 * move output logging to stderr, if we are writing to stdout
638 */
639 if (td_write(td))
640 f_out = stderr;
66159828
JA
641 }
642
6eaf09d6
SL
643 if (td_trim(td))
644 goto skip_flags;
2dc1bbeb 645 if (td->o.odirect)
2fd233b7 646 flags |= OS_O_DIRECT;
d01612f3
CM
647 if (td->o.oatomic) {
648 if (!FIO_O_ATOMIC) {
649 td_verror(td, EINVAL, "OS does not support atomic IO");
650 return 1;
651 }
652 flags |= OS_O_DIRECT | FIO_O_ATOMIC;
653 }
2dc1bbeb 654 if (td->o.sync_io)
2fd233b7 655 flags |= O_SYNC;
2378826d 656 if (td->o.create_on_open && td->o.allow_create)
814452bd 657 flags |= O_CREAT;
6eaf09d6
SL
658skip_flags:
659 if (f->filetype != FIO_TYPE_FILE)
660 flags |= FIO_O_NOATIME;
53cdc686 661
056f3459 662open_again:
660a1cb5 663 if (td_write(td)) {
17308158
JA
664 if (!read_only)
665 flags |= O_RDWR;
53cdc686 666
2378826d 667 if (f->filetype == FIO_TYPE_FILE && td->o.allow_create)
2fd233b7 668 flags |= O_CREAT;
2fd233b7 669
66159828
JA
670 if (is_std)
671 f->fd = dup(STDOUT_FILENO);
4d4e80f2
JA
672 else
673 from_hash = file_lookup_open(f, flags);
6eaf09d6 674 } else if (td_read(td)) {
4241ea8f 675 if (f->filetype == FIO_TYPE_CHAR && !read_only)
2fd233b7
JA
676 flags |= O_RDWR;
677 else
678 flags |= O_RDONLY;
679
66159828
JA
680 if (is_std)
681 f->fd = dup(STDIN_FILENO);
4d4e80f2
JA
682 else
683 from_hash = file_lookup_open(f, flags);
49cff020
TK
684 } else if (td_trim(td)) {
685 assert(!td_rw(td)); /* should have matched above */
6eaf09d6
SL
686 flags |= O_RDWR;
687 from_hash = file_lookup_open(f, flags);
53cdc686
JA
688 }
689
690 if (f->fd == -1) {
e4e33258 691 char buf[FIO_VERROR_SIZE];
e1161c32
JA
692 int __e = errno;
693
835d9b9e 694 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
5921e80c 695 flags &= ~FIO_O_NOATIME;
056f3459
AC
696 goto open_again;
697 }
e6c4d732
JA
698 if (__e == EMFILE && file_close_shadow_fds(td))
699 goto open_again;
056f3459 700
98ffb8f3 701 snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
e4e33258 702
a93c5f04
JA
703 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
704 log_err("fio: looks like your file system does not " \
705 "support direct=1/buffered=0\n");
706 }
707
e4e33258 708 td_verror(td, __e, buf);
3ce3ad33 709 return 1;
53cdc686
JA
710 }
711
29c1349f
JA
712 if (!from_hash && f->fd != -1) {
713 if (add_file_hash(f)) {
3f0ca9b9 714 int fio_unused ret;
29c1349f
JA
715
716 /*
e6c4d732
JA
717 * Stash away descriptor for later close. This is to
718 * work-around a "feature" on Linux, where a close of
719 * an fd that has been opened for write will trigger
720 * udev to call blkid to check partitions, fs id, etc.
de8f6de9 721 * That pollutes the device cache, which can slow down
e6c4d732 722 * unbuffered accesses.
29c1349f 723 */
e6c4d732
JA
724 if (f->shadow_fd == -1)
725 f->shadow_fd = f->fd;
726 else {
727 /*
728 * OK to ignore, we haven't done anything
729 * with it
730 */
731 ret = generic_close_file(td, f);
732 }
29c1349f
JA
733 goto open_again;
734 }
735 }
4906e0b5 736
53cdc686 737 return 0;
b5af8293
JA
738}
739
79591fa9
TK
740/*
741 * This function i.e. get_file_size() is the default .get_file_size
742 * implementation of majority of I/O engines.
743 */
df9c26b1 744int generic_get_file_size(struct thread_data *td, struct fio_file *f)
21972cde 745{
df9c26b1 746 return get_file_size(td, f);
21972cde
JA
747}
748
7bb48f84
JA
749/*
750 * open/close all files, so that ->real_file_size gets set
751 */
bab3fd58 752static int get_file_sizes(struct thread_data *td)
7bb48f84
JA
753{
754 struct fio_file *f;
755 unsigned int i;
bab3fd58 756 int err = 0;
7bb48f84
JA
757
758 for_each_file(td, f, i) {
08c44a55 759 dprint(FD_FILE, "get file size for %p/%d/%s\n", f, i,
5ec10eaa 760 f->file_name);
9efef3c4 761
99a47c69 762 if (td_io_get_file_size(td, f)) {
40b44f4a
JA
763 if (td->error != ENOENT) {
764 log_err("%s\n", td->verror);
765 err = 1;
3ce3ad33 766 break;
40b44f4a 767 }
541d66d7 768 clear_error(td);
07eb79df 769 }
409b3417 770
79591fa9
TK
771 /*
772 * There are corner cases where we end up with -1 for
773 * ->real_file_size due to unsupported file type, etc.
774 * We then just set to size option value divided by number
775 * of files, similar to the way file ->io_size is set.
776 * stat(2) failure doesn't set ->real_file_size to -1.
777 */
409b3417
JA
778 if (f->real_file_size == -1ULL && td->o.size)
779 f->real_file_size = td->o.size / td->o.nr_files;
7bb48f84 780 }
bab3fd58
JA
781
782 return err;
7bb48f84
JA
783}
784
2e3bd4c2
JA
785struct fio_mount {
786 struct flist_head list;
787 const char *base;
788 char __base[256];
789 unsigned int key;
790};
791
792/*
793 * Get free number of bytes for each file on each unique mount.
794 */
795static unsigned long long get_fs_free_counts(struct thread_data *td)
796{
797 struct flist_head *n, *tmp;
68b0316f 798 unsigned long long ret = 0;
2e3bd4c2
JA
799 struct fio_mount *fm;
800 FLIST_HEAD(list);
801 struct fio_file *f;
802 unsigned int i;
803
804 for_each_file(td, f, i) {
805 struct stat sb;
806 char buf[256];
807
686fbd31 808 if (f->filetype == FIO_TYPE_BLOCK || f->filetype == FIO_TYPE_CHAR) {
4ccdccd1
JA
809 if (f->real_file_size != -1ULL)
810 ret += f->real_file_size;
68b0316f
JA
811 continue;
812 } else if (f->filetype != FIO_TYPE_FILE)
813 continue;
814
da27a4bf
JA
815 buf[255] = '\0';
816 strncpy(buf, f->file_name, 255);
2e3bd4c2
JA
817
818 if (stat(buf, &sb) < 0) {
819 if (errno != ENOENT)
820 break;
821 strcpy(buf, ".");
822 if (stat(buf, &sb) < 0)
823 break;
824 }
825
826 fm = NULL;
827 flist_for_each(n, &list) {
828 fm = flist_entry(n, struct fio_mount, list);
829 if (fm->key == sb.st_dev)
830 break;
831
832 fm = NULL;
833 }
834
835 if (fm)
836 continue;
837
3660ceae
JA
838 fm = calloc(1, sizeof(*fm));
839 strncpy(fm->__base, buf, sizeof(fm->__base) - 1);
2e3bd4c2
JA
840 fm->base = basename(fm->__base);
841 fm->key = sb.st_dev;
842 flist_add(&fm->list, &list);
843 }
844
2e3bd4c2
JA
845 flist_for_each_safe(n, tmp, &list) {
846 unsigned long long sz;
847
848 fm = flist_entry(n, struct fio_mount, list);
849 flist_del(&fm->list);
850
c08ad04c 851 sz = get_fs_free_size(fm->base);
2e3bd4c2
JA
852 if (sz && sz != -1ULL)
853 ret += sz;
854
855 free(fm);
856 }
857
858 return ret;
859}
860
bedc9dc2 861uint64_t get_start_offset(struct thread_data *td, struct fio_file *f)
ce95d651 862{
bedc9dc2 863 struct thread_options *o = &td->o;
872e0415
JA
864 unsigned long long align_bs;
865 unsigned long long offset;
bedc9dc2
JA
866
867 if (o->file_append && f->filetype == FIO_TYPE_FILE)
868 return f->real_file_size;
869
89978a6b 870 if (o->start_offset_percent > 0) {
872e0415
JA
871 /*
872 * if blockalign is provided, find the min across read, write,
873 * and trim
874 */
89978a6b
BW
875 if (fio_option_is_set(o, ba)) {
876 align_bs = (unsigned long long) min(o->ba[DDIR_READ], o->ba[DDIR_WRITE]);
877 align_bs = min((unsigned long long) o->ba[DDIR_TRIM], align_bs);
872e0415
JA
878 } else {
879 /* else take the minimum block size */
89978a6b
BW
880 align_bs = td_min_bs(td);
881 }
882
883 /* calculate the raw offset */
884 offset = (f->real_file_size * o->start_offset_percent / 100) +
885 (td->subjob_number * o->offset_increment);
886
872e0415
JA
887 /*
888 * block align the offset at the next available boundary at
889 * ceiling(offset / align_bs) * align_bs
890 */
89978a6b
BW
891 offset = (offset / align_bs + (offset % align_bs != 0)) * align_bs;
892
872e0415
JA
893 } else {
894 /* start_offset_percent not set */
f0d13935
JA
895 offset = o->start_offset +
896 td->subjob_number * o->offset_increment;
89978a6b 897 }
872e0415 898
89978a6b 899 return offset;
ce95d651
DE
900}
901
7bb48f84
JA
902/*
903 * Open the files and setup files sizes, creating files if necessary.
904 */
53cdc686
JA
905int setup_files(struct thread_data *td)
906{
7bb48f84 907 unsigned long long total_size, extend_size;
de98bd30 908 struct thread_options *o = &td->o;
53cdc686 909 struct fio_file *f;
002fe734 910 unsigned int i, nr_fs_extra = 0;
000b0803 911 int err = 0, need_extend;
e90391eb 912 int old_state;
002fe734
JA
913 const unsigned int bs = td_min_bs(td);
914 uint64_t fs = 0;
53cdc686 915
ee56ad50
JA
916 dprint(FD_FILE, "setup files\n");
917
8edd973d 918 old_state = td_bump_runstate(td, TD_SETTING_UP);
e90391eb 919
de98bd30 920 if (o->read_iolog_file)
25460cf6 921 goto done;
691c8fb0 922
53cdc686 923 /*
79591fa9
TK
924 * Find out physical size of files or devices for this thread,
925 * before we determine I/O size and range of our targets.
926 * If ioengine defines a setup() method, it's responsible for
7bb48f84
JA
927 * opening the files and setting f->real_file_size to indicate
928 * the valid range for that file.
53cdc686
JA
929 */
930 if (td->io_ops->setup)
7bb48f84
JA
931 err = td->io_ops->setup(td);
932 else
bab3fd58 933 err = get_file_sizes(td);
53cdc686 934
f1027063 935 if (err)
e90391eb 936 goto err_out;
f1027063 937
0a7eb121 938 /*
7bb48f84
JA
939 * check sizes. if the files/devices do not exist and the size
940 * isn't passed to fio, abort.
0a7eb121 941 */
7bb48f84
JA
942 total_size = 0;
943 for_each_file(td, f, i) {
49a416bd 944 f->fileno = i;
7bb48f84
JA
945 if (f->real_file_size == -1ULL)
946 total_size = -1ULL;
947 else
948 total_size += f->real_file_size;
949 }
0a7eb121 950
de98bd30 951 if (o->fill_device)
2e3bd4c2
JA
952 td->fill_device_size = get_fs_free_counts(td);
953
7bb48f84
JA
954 /*
955 * device/file sizes are zero and no size given, punt
956 */
de98bd30 957 if ((!total_size || total_size == -1ULL) && !o->size &&
9b87f09b 958 !td_ioengine_flagged(td, FIO_NOIO) && !o->fill_device &&
de98bd30
JA
959 !(o->nr_files && (o->file_size_low || o->file_size_high))) {
960 log_err("%s: you need to specify size=\n", o->name);
e1161c32 961 td_verror(td, EINVAL, "total_file_size");
e90391eb 962 goto err_out;
53cdc686
JA
963 }
964
002fe734
JA
965 /*
966 * Calculate per-file size and potential extra size for the
79591fa9 967 * first files, if needed (i.e. if we don't have a fixed size).
002fe734 968 */
d1e78e6b 969 if (!o->file_size_low && o->nr_files) {
002fe734
JA
970 uint64_t all_fs;
971
972 fs = o->size / o->nr_files;
973 all_fs = fs * o->nr_files;
974
975 if (all_fs < o->size)
976 nr_fs_extra = (o->size - all_fs) / bs;
977 }
978
7bb48f84
JA
979 /*
980 * now file sizes are known, so we can set ->io_size. if size= is
981 * not given, ->io_size is just equal to ->real_file_size. if size
982 * is given, ->io_size is size / nr_files.
983 */
984 extend_size = total_size = 0;
985 need_extend = 0;
986 for_each_file(td, f, i) {
bedc9dc2 987 f->file_offset = get_start_offset(td, f);
bcdedd0a 988
79591fa9
TK
989 /*
990 * Update ->io_size depending on options specified.
991 * ->file_size_low being 0 means filesize option isn't set.
992 * Non zero ->file_size_low equals ->file_size_high means
993 * filesize option is set in a fixed size format.
994 * Non zero ->file_size_low not equals ->file_size_high means
995 * filesize option is set in a range format.
996 */
de98bd30 997 if (!o->file_size_low) {
7bb48f84 998 /*
79591fa9 999 * no file size or range given, file size is equal to
c4aa2d08 1000 * total size divided by number of files. If the size
002fe734
JA
1001 * doesn't divide nicely with the min blocksize,
1002 * make the first files bigger.
7bb48f84 1003 */
002fe734
JA
1004 f->io_size = fs;
1005 if (nr_fs_extra) {
1006 nr_fs_extra--;
1007 f->io_size += bs;
1008 }
1009
c4aa2d08 1010 /*
c6dfd837
TK
1011 * We normally don't come here for regular files, but
1012 * if the result is 0 for a regular file, set it to the
1013 * real file size. This could be size of the existing
1014 * one if it already exists, but otherwise will be set
1015 * to 0. A new file won't be created because
c4aa2d08
TK
1016 * ->io_size + ->file_offset equals ->real_file_size.
1017 */
1018 if (!f->io_size) {
1019 if (f->file_offset > f->real_file_size)
1020 goto err_offset;
273f8c91 1021 f->io_size = f->real_file_size - f->file_offset;
c4aa2d08
TK
1022 if (!f->io_size)
1023 log_info("fio: file %s may be ignored\n",
1024 f->file_name);
1025 }
de98bd30
JA
1026 } else if (f->real_file_size < o->file_size_low ||
1027 f->real_file_size > o->file_size_high) {
1028 if (f->file_offset > o->file_size_low)
bcdedd0a 1029 goto err_offset;
7bb48f84
JA
1030 /*
1031 * file size given. if it's fixed, use that. if it's a
1032 * range, generate a random size in-between.
1033 */
de98bd30
JA
1034 if (o->file_size_low == o->file_size_high)
1035 f->io_size = o->file_size_low - f->file_offset;
1036 else {
5ec10eaa
JA
1037 f->io_size = get_rand_file_size(td)
1038 - f->file_offset;
1039 }
65bdb10a 1040 } else
bcdedd0a 1041 f->io_size = f->real_file_size - f->file_offset;
53cdc686 1042
7bb48f84
JA
1043 if (f->io_size == -1ULL)
1044 total_size = -1ULL;
4d002569 1045 else {
e391c704 1046 if (o->size_percent) {
76e4bb4f
SW
1047 uint64_t file_size;
1048
1049 file_size = f->io_size + f->file_offset;
1050 f->io_size = (file_size *
1051 o->size_percent) / 100;
1052 if (f->io_size > (file_size - f->file_offset))
1053 f->io_size = file_size - f->file_offset;
1054
e391c704
JA
1055 f->io_size -= (f->io_size % td_min_bs(td));
1056 }
7bb48f84 1057 total_size += f->io_size;
4d002569 1058 }
7bb48f84
JA
1059
1060 if (f->filetype == FIO_TYPE_FILE &&
bcdedd0a 1061 (f->io_size + f->file_offset) > f->real_file_size &&
9b87f09b 1062 !td_ioengine_flagged(td, FIO_DISKLESSIO)) {
5fd31680
JA
1063 if (!o->create_on_open) {
1064 need_extend++;
814452bd 1065 extend_size += (f->io_size + f->file_offset);
43558406 1066 fio_file_set_extend(f);
5fd31680 1067 } else
814452bd 1068 f->real_file_size = f->io_size + f->file_offset;
5ec10eaa 1069 }
7bb48f84 1070 }
53cdc686 1071
66347cfa
DE
1072 if (td->o.block_error_hist) {
1073 int len;
1074
1075 assert(td->o.nr_files == 1); /* checked in fixup_options */
1076 f = td->files[0];
1077 len = f->io_size / td->o.bs[DDIR_TRIM];
1078 if (len > MAX_NR_BLOCK_INFOS || len <= 0) {
1079 log_err("fio: cannot calculate block histogram with "
1080 "%d trim blocks, maximum %d\n",
1081 len, MAX_NR_BLOCK_INFOS);
1082 td_verror(td, EINVAL, "block_error_hist");
1083 goto err_out;
1084 }
1085
1086 td->ts.nr_block_infos = len;
8a68c41c 1087 for (i = 0; i < len; i++)
66347cfa
DE
1088 td->ts.block_infos[i] =
1089 BLOCK_INFO(0, BLOCK_STATE_UNINIT);
1090 } else
1091 td->ts.nr_block_infos = 0;
1092
0bc27b0b 1093 if (!o->size || (total_size && o->size > total_size))
de98bd30 1094 o->size = total_size;
21972cde 1095
d1faa06d
JA
1096 if (o->size < td_min_bs(td)) {
1097 log_err("fio: blocksize too large for data set\n");
1098 goto err_out;
1099 }
1100
7bb48f84 1101 /*
79591fa9
TK
1102 * See if we need to extend some files, typically needed when our
1103 * target regular files don't exist yet, but our jobs require them
1104 * initially due to read I/Os.
7bb48f84
JA
1105 */
1106 if (need_extend) {
1107 temp_stall_ts = 1;
7c5f4cdb
TK
1108 if (output_format & FIO_OUTPUT_NORMAL) {
1109 log_info("%s: Laying out IO file%s (%u file%s / %s%lluMiB)\n",
1110 o->name,
1111 need_extend > 1 ? "s" : "",
1112 need_extend,
1113 need_extend > 1 ? "s" : "",
1114 need_extend > 1 ? "total " : "",
1115 extend_size >> 20);
1116 }
7bb48f84
JA
1117
1118 for_each_file(td, f, i) {
5e0074c2 1119 unsigned long long old_len = -1ULL, extend_len = -1ULL;
3baddf24 1120
d6aed795 1121 if (!fio_file_extend(f))
7bb48f84
JA
1122 continue;
1123
409b3417 1124 assert(f->filetype == FIO_TYPE_FILE);
d6aed795 1125 fio_file_clear_extend(f);
de98bd30 1126 if (!o->fill_device) {
5e0074c2 1127 old_len = f->real_file_size;
0b9d69ec
JA
1128 extend_len = f->io_size + f->file_offset -
1129 old_len;
5e0074c2 1130 }
bcdedd0a 1131 f->real_file_size = (f->io_size + f->file_offset);
7bb48f84
JA
1132 err = extend_file(td, f);
1133 if (err)
3baddf24 1134 break;
5e0074c2 1135
3baddf24
JA
1136 err = __file_invalidate_cache(td, f, old_len,
1137 extend_len);
9824f73b
JA
1138
1139 /*
1140 * Shut up static checker
1141 */
1142 if (f->fd != -1)
1143 close(f->fd);
1144
3baddf24
JA
1145 f->fd = -1;
1146 if (err)
7bb48f84
JA
1147 break;
1148 }
1149 temp_stall_ts = 0;
1150 }
1151
1152 if (err)
e90391eb 1153 goto err_out;
7bb48f84 1154
de98bd30
JA
1155 if (!o->zone_size)
1156 o->zone_size = o->size;
7bb48f84 1157
ea966f81
JA
1158 /*
1159 * iolog already set the total io size, if we read back
1160 * stored entries.
1161 */
77731b29 1162 if (!o->read_iolog_file) {
5be9bf09
TK
1163 if (o->io_size)
1164 td->total_io_size = o->io_size * o->loops;
77731b29
JA
1165 else
1166 td->total_io_size = o->size * o->loops;
1167 }
25460cf6
JA
1168
1169done:
de98bd30 1170 if (o->create_only)
25460cf6
JA
1171 td->done = 1;
1172
8edd973d 1173 td_restore_runstate(td, old_state);
7bb48f84 1174 return 0;
bcdedd0a 1175err_offset:
de98bd30 1176 log_err("%s: you need to specify valid offset=\n", o->name);
e90391eb 1177err_out:
8edd973d 1178 td_restore_runstate(td, old_state);
bcdedd0a 1179 return 1;
53cdc686
JA
1180}
1181
afad68f7
ZY
1182int pre_read_files(struct thread_data *td)
1183{
1184 struct fio_file *f;
1185 unsigned int i;
1186
1187 dprint(FD_FILE, "pre_read files\n");
1188
1189 for_each_file(td, f, i) {
afe0d59a
TK
1190 if (pre_read_file(td, f))
1191 return -1;
afad68f7
ZY
1192 }
1193
afe0d59a 1194 return 0;
afad68f7
ZY
1195}
1196
9c6f6316
JA
1197static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
1198{
2316296a 1199 unsigned int range_size, seed;
9c6f6316 1200 unsigned long nranges;
42da5c8b 1201 uint64_t fsize;
9c6f6316
JA
1202
1203 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
42da5c8b 1204 fsize = min(f->real_file_size, f->io_size);
9c6f6316 1205
42da5c8b 1206 nranges = (fsize + range_size - 1) / range_size;
9c6f6316 1207
2316296a 1208 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
8425687e
JA
1209 if (!td->o.rand_repeatable)
1210 seed = td->rand_seeds[4];
1211
9c6f6316 1212 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
888677a4 1213 zipf_init(&f->zipf, nranges, td->o.zipf_theta.u.f, seed);
56d9fa4b 1214 else if (td->o.random_distribution == FIO_RAND_DIST_PARETO)
888677a4 1215 pareto_init(&f->zipf, nranges, td->o.pareto_h.u.f, seed);
56d9fa4b 1216 else if (td->o.random_distribution == FIO_RAND_DIST_GAUSS)
f88cd222 1217 gauss_init(&f->gauss, nranges, td->o.gauss_dev.u.f, seed);
9c6f6316
JA
1218
1219 return 1;
1220}
1221
1222static int init_rand_distribution(struct thread_data *td)
1223{
1224 struct fio_file *f;
1225 unsigned int i;
1226 int state;
1227
1228 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
1229 return 0;
1230
8edd973d
JA
1231 state = td_bump_runstate(td, TD_SETTING_UP);
1232
9c6f6316
JA
1233 for_each_file(td, f, i)
1234 __init_rand_distribution(td, f);
8edd973d
JA
1235
1236 td_restore_runstate(td, state);
9c6f6316
JA
1237
1238 return 1;
1239}
1240
75f90d5f
JA
1241/*
1242 * Check if the number of blocks exceeds the randomness capability of
1243 * the selected generator. Tausworthe is 32-bit, the others are fullly
1244 * 64-bit capable.
1245 */
1246static int check_rand_gen_limits(struct thread_data *td, struct fio_file *f,
1247 uint64_t blocks)
1248{
1249 if (blocks <= FRAND32_MAX)
1250 return 0;
1251 if (td->o.random_generator != FIO_RAND_GEN_TAUSWORTHE)
1252 return 0;
1253
1254 /*
1255 * If the user hasn't specified a random generator, switch
1256 * to tausworthe64 with informational warning. If the user did
1257 * specify one, just warn.
1258 */
1259 log_info("fio: file %s exceeds 32-bit tausworthe random generator.\n",
1260 f->file_name);
1261
1262 if (!fio_option_is_set(&td->o, random_generator)) {
1263 log_info("fio: Switching to tausworthe64. Use the "
1264 "random_generator= option to get rid of this "
4e795a3e 1265 "warning.\n");
75f90d5f
JA
1266 td->o.random_generator = FIO_RAND_GEN_TAUSWORTHE64;
1267 return 0;
1268 }
1269
1270 /*
1271 * Just make this information to avoid breaking scripts.
1272 */
1273 log_info("fio: Use the random_generator= option to switch to lfsr or "
1274 "tausworthe64.\n");
1275 return 0;
1276}
1277
68727076
JA
1278int init_random_map(struct thread_data *td)
1279{
51ede0b1 1280 unsigned long long blocks;
68727076
JA
1281 struct fio_file *f;
1282 unsigned int i;
1283
9c6f6316
JA
1284 if (init_rand_distribution(td))
1285 return 0;
3831a843 1286 if (!td_random(td))
68727076
JA
1287 return 0;
1288
1289 for_each_file(td, f, i) {
42da5c8b 1290 uint64_t fsize = min(f->real_file_size, f->io_size);
38f30c81 1291
42da5c8b 1292 blocks = fsize / (unsigned long long) td->o.rw_min_bs;
53737ae0 1293
75f90d5f 1294 if (check_rand_gen_limits(td, f, blocks))
37fbd7e9 1295 return 1;
37fbd7e9 1296
8055e41d 1297 if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
82af46be
JA
1298 unsigned long seed;
1299
1300 seed = td->rand_seeds[FIO_RAND_BLOCK_OFF];
9c0f3f32 1301
967d1b63
JA
1302 if (!lfsr_init(&f->lfsr, blocks, seed, 0)) {
1303 fio_file_set_lfsr(f);
8055e41d 1304 continue;
967d1b63 1305 }
3831a843 1306 } else if (!td->o.norandommap) {
7ebd796f 1307 f->io_axmap = axmap_new(blocks);
967d1b63
JA
1308 if (f->io_axmap) {
1309 fio_file_set_axmap(f);
8055e41d 1310 continue;
967d1b63 1311 }
1cad7121
JA
1312 } else if (td->o.norandommap)
1313 continue;
ceadd59e 1314
2b386d25 1315 if (!td->o.softrandommap) {
5ec10eaa
JA
1316 log_err("fio: failed allocating random map. If running"
1317 " a large number of jobs, try the 'norandommap'"
2b386d25
JA
1318 " option or set 'softrandommap'. Or give"
1319 " a larger --alloc-size to fio.\n");
68727076
JA
1320 return 1;
1321 }
303032ae
JA
1322
1323 log_info("fio: file %s failed allocating random map. Running "
1324 "job without.\n", f->file_name);
68727076
JA
1325 }
1326
1327 return 0;
1328}
1329
53cdc686
JA
1330void close_files(struct thread_data *td)
1331{
0ab8db89 1332 struct fio_file *f;
af52b345 1333 unsigned int i;
53cdc686 1334
2be3eec3
JA
1335 for_each_file(td, f, i) {
1336 if (fio_file_open(f))
1337 td_io_close_file(td, f);
1338 }
24ffd2c2
JA
1339}
1340
1341void close_and_free_files(struct thread_data *td)
1342{
1343 struct fio_file *f;
1344 unsigned int i;
cb9bf647 1345 bool use_free = td_ioengine_flagged(td, FIO_NOFILEHASH);
24ffd2c2 1346
ee56ad50
JA
1347 dprint(FD_FILE, "close files\n");
1348
0ab8db89 1349 for_each_file(td, f, i) {
38ef9c90
CF
1350 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1351 dprint(FD_FILE, "free unlink %s\n", f->file_name);
1352 td_io_unlink_file(td, f);
1353 }
1354
22a57ba8
JA
1355 if (fio_file_open(f))
1356 td_io_close_file(td, f);
1357
b9fbcf21 1358 remove_file_hash(f);
b3dc7f07 1359
b5f4d8ba
JA
1360 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
1361 dprint(FD_FILE, "free unlink %s\n", f->file_name);
38ef9c90 1362 td_io_unlink_file(td, f);
b5f4d8ba
JA
1363 }
1364
cb9bf647
JB
1365 if (use_free)
1366 free(f->file_name);
1367 else
1368 sfree(f->file_name);
fa1da865 1369 f->file_name = NULL;
967d1b63
JA
1370 if (fio_file_axmap(f)) {
1371 axmap_free(f->io_axmap);
1372 f->io_axmap = NULL;
1373 }
cb9bf647
JB
1374 if (use_free)
1375 free(f);
1376 else
1377 sfree(f);
53cdc686 1378 }
b4a6a59a 1379
2dc1bbeb 1380 td->o.filename = NULL;
cade3ef4 1381 free(td->files);
d7df1d13 1382 free(td->file_locks);
9efef3c4 1383 td->files_index = 0;
b4a6a59a 1384 td->files = NULL;
d7df1d13 1385 td->file_locks = NULL;
27ddbfa0 1386 td->o.file_lock_mode = FILE_LOCK_NONE;
2dc1bbeb 1387 td->o.nr_files = 0;
53cdc686 1388}
af52b345 1389
e3bab463 1390static void get_file_type(struct fio_file *f)
af52b345
JA
1391{
1392 struct stat sb;
1393
66159828
JA
1394 if (!strcmp(f->file_name, "-"))
1395 f->filetype = FIO_TYPE_PIPE;
1396 else
1397 f->filetype = FIO_TYPE_FILE;
af52b345 1398
d5b78f5a 1399#ifdef WIN32
3892182a
BC
1400 /* \\.\ is the device namespace in Windows, where every file is
1401 * a block device */
1402 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
686fbd31 1403 f->filetype = FIO_TYPE_BLOCK;
d5b78f5a 1404#endif
3892182a 1405
b30d395e 1406 if (!stat(f->file_name, &sb)) {
3892182a 1407 if (S_ISBLK(sb.st_mode))
686fbd31 1408 f->filetype = FIO_TYPE_BLOCK;
af52b345
JA
1409 else if (S_ISCHR(sb.st_mode))
1410 f->filetype = FIO_TYPE_CHAR;
b5605e9d
JA
1411 else if (S_ISFIFO(sb.st_mode))
1412 f->filetype = FIO_TYPE_PIPE;
af52b345
JA
1413 }
1414}
1415
1b2a83dc 1416static bool __is_already_allocated(const char *fname, bool set)
190b8f0c 1417{
90426237 1418 struct flist_head *entry;
1b2a83dc 1419 bool ret;
bcbfeefa 1420
1b2a83dc
JA
1421 ret = file_bloom_exists(fname, set);
1422 if (!ret)
1423 return ret;
90426237
JA
1424
1425 flist_for_each(entry, &filename_list) {
04d6530f 1426 struct file_name *fn;
90426237 1427
04d6530f
JA
1428 fn = flist_entry(entry, struct file_name, list);
1429
1430 if (!strcmp(fn->filename, fname))
1431 return true;
90426237
JA
1432 }
1433
04d6530f 1434 return false;
bcbfeefa
CE
1435}
1436
04d6530f 1437static bool is_already_allocated(const char *fname)
bcbfeefa 1438{
04d6530f 1439 bool ret;
bcbfeefa 1440
90426237 1441 fio_file_hash_lock();
1b2a83dc 1442 ret = __is_already_allocated(fname, false);
90426237 1443 fio_file_hash_unlock();
04d6530f 1444
90426237
JA
1445 return ret;
1446}
bcbfeefa 1447
90426237
JA
1448static void set_already_allocated(const char *fname)
1449{
1450 struct file_name *fn;
1451
1452 fn = malloc(sizeof(struct file_name));
1453 fn->filename = strdup(fname);
1454
1455 fio_file_hash_lock();
1b2a83dc 1456 if (!__is_already_allocated(fname, true)) {
90426237
JA
1457 flist_add_tail(&fn->list, &filename_list);
1458 fn = NULL;
bcbfeefa 1459 }
90426237 1460 fio_file_hash_unlock();
bcbfeefa 1461
90426237
JA
1462 if (fn) {
1463 free(fn->filename);
1464 free(fn);
1465 }
bcbfeefa
CE
1466}
1467
190b8f0c
CF
1468static void free_already_allocated(void)
1469{
bcbfeefa
CE
1470 struct flist_head *entry, *tmp;
1471 struct file_name *fn;
1472
90426237
JA
1473 if (flist_empty(&filename_list))
1474 return;
1475
1476 fio_file_hash_lock();
1477 flist_for_each_safe(entry, tmp, &filename_list) {
1478 fn = flist_entry(entry, struct file_name, list);
1479 free(fn->filename);
1480 flist_del(&fn->list);
1481 free(fn);
bcbfeefa 1482 }
90426237
JA
1483
1484 fio_file_hash_unlock();
bcbfeefa
CE
1485}
1486
7b5cb700
JA
1487static struct fio_file *alloc_new_file(struct thread_data *td)
1488{
1489 struct fio_file *f;
1490
cb9bf647
JB
1491 if (td_ioengine_flagged(td, FIO_NOFILEHASH))
1492 f = calloc(1, sizeof(*f));
1493 else
1494 f = smalloc(sizeof(*f));
7b5cb700 1495 if (!f) {
7b5cb700
JA
1496 assert(0);
1497 return NULL;
1498 }
1499
1500 f->fd = -1;
1501 f->shadow_fd = -1;
1502 fio_file_reset(td, f);
1503 return f;
1504}
1505
04d6530f
JA
1506bool exists_and_not_regfile(const char *filename)
1507{
1508 struct stat sb;
1509
1510 if (lstat(filename, &sb) == -1)
1511 return false;
1512
1513#ifndef WIN32 /* NOT Windows */
1514 if (S_ISREG(sb.st_mode))
1515 return false;
1516#else
1517 /* \\.\ is the device namespace in Windows, where every file
1518 * is a device node */
1519 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
1520 return false;
1521#endif
1522
1523 return true;
1524}
1525
0b801f22 1526static int create_work_dirs(struct thread_data *td, const char *fname)
f1c71016
JB
1527{
1528 char path[PATH_MAX];
1529 char *start, *end;
f1c71016
JB
1530
1531 if (td->o.directory) {
0b801f22
JB
1532 snprintf(path, PATH_MAX, "%s%c%s", td->o.directory,
1533 FIO_OS_PATH_SEPARATOR, fname);
1534 start = strstr(path, fname);
f1c71016 1535 } else {
0b801f22
JB
1536 snprintf(path, PATH_MAX, "%s", fname);
1537 start = path;
f1c71016
JB
1538 }
1539
0b801f22 1540 end = start;
f1c71016
JB
1541 while ((end = strchr(end, FIO_OS_PATH_SEPARATOR)) != NULL) {
1542 if (end == start)
1543 break;
1544 *end = '\0';
1545 errno = 0;
4252396e 1546#ifdef CONFIG_HAVE_MKDIR_TWO
0b801f22 1547 if (mkdir(path, 0600) && errno != EEXIST) {
4252396e
JA
1548#else
1549 if (mkdir(path) && errno != EEXIST) {
1550#endif
f1c71016
JB
1551 log_err("fio: failed to create dir (%s): %d\n",
1552 start, errno);
0b801f22 1553 return 1;
f1c71016
JB
1554 }
1555 *end = FIO_OS_PATH_SEPARATOR;
1556 end++;
1557 }
f1c71016 1558 td->flags |= TD_F_DIRS_CREATED;
0b801f22 1559 return 0;
f1c71016
JB
1560}
1561
5903e7b7 1562int add_file(struct thread_data *td, const char *fname, int numjob, int inc)
af52b345 1563{
7b4e4fe5 1564 int cur_files = td->files_index;
bd0ee748 1565 char file_name[PATH_MAX];
af52b345 1566 struct fio_file *f;
bd0ee748 1567 int len = 0;
af52b345 1568
ee56ad50
JA
1569 dprint(FD_FILE, "add file %s\n", fname);
1570
bcbfeefa 1571 if (td->o.directory)
922a5be8
JA
1572 len = set_name_idx(file_name, PATH_MAX, td->o.directory, numjob,
1573 td->o.unique_filename);
bcbfeefa
CE
1574
1575 sprintf(file_name + len, "%s", fname);
1576
f1c71016 1577 if (strchr(fname, FIO_OS_PATH_SEPARATOR) &&
0b801f22
JB
1578 !(td->flags & TD_F_DIRS_CREATED) &&
1579 create_work_dirs(td, fname))
1580 return 1;
f1c71016 1581
bcbfeefa 1582 /* clean cloned siblings using existing files */
04d6530f
JA
1583 if (numjob && is_already_allocated(file_name) &&
1584 !exists_and_not_regfile(fname))
bcbfeefa
CE
1585 return 0;
1586
7b5cb700 1587 f = alloc_new_file(td);
bd0ee748 1588
fc99bc0d 1589 if (td->files_size <= td->files_index) {
1983e327 1590 unsigned int new_size = td->o.nr_files + 1;
126d65c6 1591
fc99bc0d
JA
1592 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1593
1594 td->files = realloc(td->files, new_size * sizeof(f));
d537c08b
JM
1595 if (td->files == NULL) {
1596 log_err("fio: realloc OOM\n");
1597 assert(0);
1598 }
d7df1d13
JA
1599 if (td->o.file_lock_mode != FILE_LOCK_NONE) {
1600 td->file_locks = realloc(td->file_locks, new_size);
1601 if (!td->file_locks) {
1602 log_err("fio: realloc OOM\n");
1603 assert(0);
1604 }
1605 td->file_locks[cur_files] = FILE_LOCK_NONE;
1606 }
fc99bc0d
JA
1607 td->files_size = new_size;
1608 }
8bb7679e 1609 td->files[cur_files] = f;
89ac1d48 1610 f->fileno = cur_files;
126d65c6 1611
07eb79df
JA
1612 /*
1613 * init function, io engine may not be loaded yet
1614 */
9b87f09b 1615 if (td->io_ops && td_ioengine_flagged(td, FIO_DISKLESSIO))
07eb79df
JA
1616 f->real_file_size = -1ULL;
1617
cb9bf647
JB
1618 if (td_ioengine_flagged(td, FIO_NOFILEHASH))
1619 f->file_name = strdup(file_name);
1620 else
1621 f->file_name = smalloc_strdup(file_name);
81b3c86f 1622 if (!f->file_name)
c48c0be7 1623 assert(0);
0b9d69ec 1624
e3bab463 1625 get_file_type(f);
af52b345 1626
4d4e80f2
JA
1627 switch (td->o.file_lock_mode) {
1628 case FILE_LOCK_NONE:
1629 break;
1630 case FILE_LOCK_READWRITE:
d7df1d13 1631 f->rwlock = fio_rwlock_init();
4d4e80f2
JA
1632 break;
1633 case FILE_LOCK_EXCLUSIVE:
521da527 1634 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
4d4e80f2
JA
1635 break;
1636 default:
1637 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1638 assert(0);
1639 }
29c1349f 1640
7b4e4fe5 1641 td->files_index++;
1549441c
JA
1642 if (f->filetype == FIO_TYPE_FILE)
1643 td->nr_normal_files++;
f29b25a3 1644
084a5475
JA
1645 if (td->o.numjobs > 1)
1646 set_already_allocated(file_name);
bcbfeefa 1647
5903e7b7
JA
1648 if (inc)
1649 td->o.nr_files++;
1650
5ec10eaa
JA
1651 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1652 cur_files);
9efef3c4 1653
f29b25a3 1654 return cur_files;
af52b345 1655}
0ad920e7 1656
49ffb4a2
JA
1657int add_file_exclusive(struct thread_data *td, const char *fname)
1658{
1659 struct fio_file *f;
1660 unsigned int i;
1661
1662 for_each_file(td, f, i) {
1663 if (!strcmp(f->file_name, fname))
1664 return i;
1665 }
1666
5903e7b7 1667 return add_file(td, fname, 0, 1);
49ffb4a2
JA
1668}
1669
0ad920e7
JA
1670void get_file(struct fio_file *f)
1671{
8172fe97 1672 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
d6aed795 1673 assert(fio_file_open(f));
0ad920e7
JA
1674 f->references++;
1675}
1676
6977bcd0 1677int put_file(struct thread_data *td, struct fio_file *f)
0ad920e7 1678{
98e1ac4e 1679 int f_ret = 0, ret = 0;
6977bcd0 1680
8172fe97 1681 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
ee56ad50 1682
22a57ba8
JA
1683 if (!fio_file_open(f)) {
1684 assert(f->fd == -1);
6977bcd0 1685 return 0;
22a57ba8 1686 }
0ad920e7
JA
1687
1688 assert(f->references);
1689 if (--f->references)
6977bcd0 1690 return 0;
0ad920e7 1691
71b84caa 1692 if (should_fsync(td) && td->o.fsync_on_close) {
98e1ac4e 1693 f_ret = fsync(f->fd);
71b84caa
JA
1694 if (f_ret < 0)
1695 f_ret = errno;
1696 }
ebb1415f 1697
0ad920e7 1698 if (td->io_ops->close_file)
6977bcd0 1699 ret = td->io_ops->close_file(td, f);
1020a139 1700
98e1ac4e 1701 if (!ret)
a5fb461f 1702 ret = f_ret;
98e1ac4e 1703
0ad920e7 1704 td->nr_open_files--;
d6aed795 1705 fio_file_clear_open(f);
22a57ba8 1706 assert(f->fd == -1);
6977bcd0 1707 return ret;
0ad920e7 1708}
bbf6b540 1709
4d4e80f2 1710void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
b2bd2bd9 1711{
4d4e80f2
JA
1712 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1713 return;
29c1349f 1714
4d4e80f2
JA
1715 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1716 if (ddir == DDIR_READ)
d7df1d13 1717 fio_rwlock_read(f->rwlock);
4d4e80f2 1718 else
d7df1d13 1719 fio_rwlock_write(f->rwlock);
4d4e80f2
JA
1720 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1721 fio_mutex_down(f->lock);
1722
d7df1d13 1723 td->file_locks[f->fileno] = td->o.file_lock_mode;
b2bd2bd9
JA
1724}
1725
4d4e80f2 1726void unlock_file(struct thread_data *td, struct fio_file *f)
b2bd2bd9 1727{
4d4e80f2
JA
1728 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1729 return;
4d4e80f2 1730
d7df1d13
JA
1731 if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1732 fio_rwlock_unlock(f->rwlock);
1733 else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
4d4e80f2 1734 fio_mutex_up(f->lock);
d7df1d13
JA
1735
1736 td->file_locks[f->fileno] = FILE_LOCK_NONE;
b2bd2bd9
JA
1737}
1738
4d4e80f2
JA
1739void unlock_file_all(struct thread_data *td, struct fio_file *f)
1740{
f2a2803a 1741 if (td->o.file_lock_mode == FILE_LOCK_NONE || !td->file_locks)
27ddbfa0 1742 return;
d7df1d13
JA
1743 if (td->file_locks[f->fileno] != FILE_LOCK_NONE)
1744 unlock_file(td, f);
4d4e80f2
JA
1745}
1746
bbf6b540
JA
1747static int recurse_dir(struct thread_data *td, const char *dirname)
1748{
1749 struct dirent *dir;
1750 int ret = 0;
1751 DIR *D;
1752
1753 D = opendir(dirname);
1754 if (!D) {
0ddb270c
JA
1755 char buf[FIO_VERROR_SIZE];
1756
98ffb8f3 1757 snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
0ddb270c 1758 td_verror(td, errno, buf);
bbf6b540
JA
1759 return 1;
1760 }
1761
1762 while ((dir = readdir(D)) != NULL) {
1763 char full_path[PATH_MAX];
1764 struct stat sb;
1765
e85b2b83
JA
1766 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1767 continue;
96d32d51 1768
53a7af85 1769 sprintf(full_path, "%s%c%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
bbf6b540
JA
1770
1771 if (lstat(full_path, &sb) == -1) {
1772 if (errno != ENOENT) {
1773 td_verror(td, errno, "stat");
68ace9e0
JA
1774 ret = 1;
1775 break;
bbf6b540
JA
1776 }
1777 }
1778
1779 if (S_ISREG(sb.st_mode)) {
5903e7b7 1780 add_file(td, full_path, 0, 1);
bbf6b540
JA
1781 continue;
1782 }
0ddb270c
JA
1783 if (!S_ISDIR(sb.st_mode))
1784 continue;
bbf6b540 1785
5ec10eaa
JA
1786 ret = recurse_dir(td, full_path);
1787 if (ret)
bbf6b540
JA
1788 break;
1789 }
1790
1791 closedir(D);
1792 return ret;
1793}
1794
1795int add_dir_files(struct thread_data *td, const char *path)
1796{
0ddb270c
JA
1797 int ret = recurse_dir(td, path);
1798
1799 if (!ret)
1800 log_info("fio: opendir added %d files\n", td->o.nr_files);
1801
1802 return ret;
bbf6b540 1803}
cade3ef4
JA
1804
1805void dup_files(struct thread_data *td, struct thread_data *org)
1806{
1807 struct fio_file *f;
1808 unsigned int i;
9efef3c4
JA
1809
1810 dprint(FD_FILE, "dup files: %d\n", org->files_index);
cade3ef4
JA
1811
1812 if (!org->files)
1813 return;
1814
9efef3c4 1815 td->files = malloc(org->files_index * sizeof(f));
cade3ef4 1816
d7df1d13
JA
1817 if (td->o.file_lock_mode != FILE_LOCK_NONE)
1818 td->file_locks = malloc(org->files_index);
1819
9efef3c4 1820 for_each_file(org, f, i) {
b0fe421a
JA
1821 struct fio_file *__f;
1822
7b5cb700 1823 __f = alloc_new_file(td);
0b9d69ec 1824
bc3456fa 1825 if (f->file_name) {
cb9bf647
JB
1826 if (td_ioengine_flagged(td, FIO_NOFILEHASH))
1827 __f->file_name = strdup(f->file_name);
1828 else
1829 __f->file_name = smalloc_strdup(f->file_name);
81b3c86f 1830 if (!__f->file_name)
c48c0be7 1831 assert(0);
0b9d69ec 1832
bc3456fa
AC
1833 __f->filetype = f->filetype;
1834 }
b0fe421a 1835
67ad9242
JA
1836 if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1837 __f->lock = f->lock;
1838 else if (td->o.file_lock_mode == FILE_LOCK_READWRITE)
1839 __f->rwlock = f->rwlock;
1840
b0fe421a 1841 td->files[i] = __f;
cade3ef4
JA
1842 }
1843}
f29b25a3
JA
1844
1845/*
1846 * Returns the index that matches the filename, or -1 if not there
1847 */
1848int get_fileno(struct thread_data *td, const char *fname)
1849{
1850 struct fio_file *f;
1851 unsigned int i;
1852
1853 for_each_file(td, f, i)
1854 if (!strcmp(f->file_name, fname))
1855 return i;
1856
1857 return -1;
1858}
1859
1860/*
1861 * For log usage, where we add/open/close files automatically
1862 */
1863void free_release_files(struct thread_data *td)
1864{
1865 close_files(td);
66ee4002
JA
1866 td->o.nr_files = 0;
1867 td->o.open_files = 0;
f29b25a3
JA
1868 td->files_index = 0;
1869 td->nr_normal_files = 0;
1870}
33c48814
JA
1871
1872void fio_file_reset(struct thread_data *td, struct fio_file *f)
1873{
f1dfb668
JA
1874 int i;
1875
1876 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1877 f->last_pos[i] = f->file_offset;
1878 f->last_start[i] = -1ULL;
1879 }
1880
967d1b63 1881 if (fio_file_axmap(f))
33c48814 1882 axmap_reset(f->io_axmap);
967d1b63 1883 else if (fio_file_lfsr(f))
33c48814
JA
1884 lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
1885}
002fe734 1886
747b3105 1887bool fio_files_done(struct thread_data *td)
002fe734
JA
1888{
1889 struct fio_file *f;
1890 unsigned int i;
1891
1892 for_each_file(td, f, i)
1893 if (!fio_file_done(f))
747b3105 1894 return false;
002fe734 1895
747b3105 1896 return true;
002fe734 1897}
bcbfeefa
CE
1898
1899/* free memory used in initialization phase only */
190b8f0c
CF
1900void filesetup_mem_free(void)
1901{
bcbfeefa
CE
1902 free_already_allocated();
1903}
47534cda
TK
1904
1905/*
1906 * This function is for platforms which support direct I/O but not O_DIRECT.
1907 */
1908int fio_set_directio(struct thread_data *td, struct fio_file *f)
1909{
1910#ifdef FIO_OS_DIRECTIO
2905de74 1911 int ret = fio_set_odirect(f);
47534cda
TK
1912
1913 if (ret) {
1914 td_verror(td, ret, "fio_set_directio");
1915#if defined(__sun__)
1916 if (ret == ENOTTY) { /* ENOTTY suggests RAW device or ZFS */
1917 log_err("fio: doing directIO to RAW devices or ZFS not supported\n");
1918 } else {
1919 log_err("fio: the file system does not seem to support direct IO\n");
1920 }
1921#else
1922 log_err("fio: the file system does not seem to support direct IO\n");
1923#endif
1924 return -1;
1925 }
1926
1927 return 0;
1928#else
51102e0d 1929 log_err("fio: direct IO is not supported on this host operating system\n");
47534cda
TK
1930 return -1;
1931#endif
1932}