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