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