Use unique seed for zipf/pareto init if rand_repeat is not set
[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"
ecc314ba 14#include "os/os.h"
2316296a 15#include "hash.h"
53cdc686 16
a596f047
EG
17#ifdef FIO_HAVE_LINUX_FALLOCATE
18#include <linux/falloc.h>
19#endif
20
7172cfe8
JA
21static int root_warn;
22
c592b9fe
JA
23static inline void clear_error(struct thread_data *td)
24{
25 td->error = 0;
26 td->verror[0] = '\0';
27}
28
3baddf24
JA
29/*
30 * Leaves f->fd open on success, caller must close
31 */
7bb48f84 32static int extend_file(struct thread_data *td, struct fio_file *f)
25205e97 33{
ea443657 34 int r, new_layout = 0, unlink_file = 0, flags;
25205e97
JA
35 unsigned long long left;
36 unsigned int bs;
37 char *b;
b2a15192 38
4241ea8f
JA
39 if (read_only) {
40 log_err("fio: refusing extend of file due to read-only\n");
41 return 0;
42 }
43
507a702f
JA
44 /*
45 * check if we need to lay the file out complete again. fio
46 * does that for operations involving reads, or for writes
47 * where overwrite is set
48 */
ffdfabd4
JA
49 if (td_read(td) || (td_write(td) && td->o.overwrite) ||
50 (td_write(td) && td->io_ops->flags & FIO_NOEXTEND))
507a702f 51 new_layout = 1;
ea443657
JA
52 if (td_write(td) && !td->o.overwrite)
53 unlink_file = 1;
507a702f 54
6ae1f57f 55 if (unlink_file || new_layout) {
bd199f2b 56 dprint(FD_FILE, "layout unlink %s\n", f->file_name);
982016d6 57 if ((unlink(f->file_name) < 0) && (errno != ENOENT)) {
7bb48f84
JA
58 td_verror(td, errno, "unlink");
59 return 1;
60 }
61 }
62
507a702f
JA
63 flags = O_WRONLY | O_CREAT;
64 if (new_layout)
65 flags |= O_TRUNC;
66
ee56ad50 67 dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags);
507a702f 68 f->fd = open(f->file_name, flags, 0644);
53cdc686 69 if (f->fd < 0) {
e1161c32 70 td_verror(td, errno, "open");
53cdc686
JA
71 return 1;
72 }
73
7bc8c2cf 74#ifdef FIO_HAVE_FALLOCATE
a596f047
EG
75 if (!td->o.fill_device) {
76 switch (td->o.fallocate_mode) {
77 case FIO_FALLOCATE_NONE:
78 break;
79 case FIO_FALLOCATE_POSIX:
80 dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
81 f->file_name, 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",
86 strerror(r));
87 }
88 break;
89#ifdef FIO_HAVE_LINUX_FALLOCATE
90 case FIO_FALLOCATE_KEEP_SIZE:
91 dprint(FD_FILE,
92 "fallocate(FALLOC_FL_KEEP_SIZE) "
93 "file %s size %llu\n",
94 f->file_name, f->real_file_size);
7bc8c2cf 95
a596f047
EG
96 r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
97 f->real_file_size);
98 if (r != 0) {
99 td_verror(td, errno, "fallocate");
100 }
101 break;
102#endif /* FIO_HAVE_LINUX_FALLOCATE */
103 default:
104 log_err("fio: unknown fallocate mode: %d\n",
105 td->o.fallocate_mode);
106 assert(0);
7bc8c2cf
JA
107 }
108 }
a596f047 109#endif /* FIO_HAVE_FALLOCATE */
9b836561 110
fc74ac1d
SL
111 if (!new_layout)
112 goto done;
113
5e0074c2
JA
114 /*
115 * The size will be -1ULL when fill_device is used, so don't truncate
116 * or fallocate this file, just write it
117 */
118 if (!td->o.fill_device) {
119 dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name,
ee56ad50 120 f->real_file_size);
5e0074c2
JA
121 if (ftruncate(f->fd, f->real_file_size) == -1) {
122 td_verror(td, errno, "ftruncate");
123 goto err;
124 }
5e0074c2 125 }
40f8298c 126
2dc1bbeb
JA
127 b = malloc(td->o.max_bs[DDIR_WRITE]);
128 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
53cdc686 129
7bb48f84 130 left = f->real_file_size;
53cdc686 131 while (left && !td->terminate) {
2dc1bbeb 132 bs = td->o.max_bs[DDIR_WRITE];
53cdc686
JA
133 if (bs > left)
134 bs = left;
135
136 r = write(f->fd, b, bs);
137
5e0074c2
JA
138 if (r > 0) {
139 left -= r;
53cdc686
JA
140 continue;
141 } else {
5e0074c2
JA
142 if (r < 0) {
143 int __e = errno;
144
145 if (__e == ENOSPC) {
146 if (td->o.fill_device)
147 break;
148 log_info("fio: ENOSPC on laying out "
149 "file, stopping\n");
150 break;
151 }
e1161c32 152 td_verror(td, errno, "write");
5e0074c2 153 } else
e1161c32 154 td_verror(td, EIO, "write");
53cdc686
JA
155
156 break;
157 }
158 }
159
ffdfabd4
JA
160 if (td->terminate) {
161 dprint(FD_FILE, "terminate unlink %s\n", f->file_name);
53cdc686 162 unlink(f->file_name);
ffdfabd4 163 } else if (td->o.create_fsync) {
98e1ac4e
JA
164 if (fsync(f->fd) < 0) {
165 td_verror(td, errno, "fsync");
166 goto err;
167 }
168 }
0d1cd207 169 if (td->o.fill_device && !td_write(td)) {
d6aed795 170 fio_file_clear_size_known(f);
5e0074c2
JA
171 if (td_io_get_file_size(td, f))
172 goto err;
173 if (f->io_size > f->real_file_size)
174 f->io_size = f->real_file_size;
175 }
53cdc686
JA
176
177 free(b);
507a702f 178done:
53cdc686
JA
179 return 0;
180err:
181 close(f->fd);
182 f->fd = -1;
183 return 1;
184}
185
afad68f7
ZY
186static int pre_read_file(struct thread_data *td, struct fio_file *f)
187{
b0f65863 188 int r, did_open = 0, old_runstate;
afad68f7
ZY
189 unsigned long long left;
190 unsigned int bs;
191 char *b;
192
9c0d2241
JA
193 if (td->io_ops->flags & FIO_PIPEIO)
194 return 0;
195
d6aed795 196 if (!fio_file_open(f)) {
b0f65863
JA
197 if (td->io_ops->open_file(td, f)) {
198 log_err("fio: cannot pre-read, failed to open file\n");
199 return 1;
200 }
201 did_open = 1;
202 }
203
204 old_runstate = td->runstate;
205 td_set_runstate(td, TD_PRE_READING);
206
afad68f7
ZY
207 bs = td->o.max_bs[DDIR_READ];
208 b = malloc(bs);
209 memset(b, 0, bs);
210
211 lseek(f->fd, f->file_offset, SEEK_SET);
212 left = f->io_size;
213
214 while (left && !td->terminate) {
215 if (bs > left)
216 bs = left;
217
218 r = read(f->fd, b, bs);
219
220 if (r == (int) bs) {
221 left -= bs;
222 continue;
223 } else {
224 td_verror(td, EIO, "pre_read");
225 break;
226 }
227 }
228
b0f65863
JA
229 td_set_runstate(td, old_runstate);
230
231 if (did_open)
232 td->io_ops->close_file(td, f);
afad68f7
ZY
233 free(b);
234 return 0;
235}
236
7bb48f84 237static unsigned long long get_rand_file_size(struct thread_data *td)
9c60ce64 238{
dc873b6f 239 unsigned long long ret, sized;
1294c3ec 240 unsigned long r;
9c60ce64 241
4c07ad86
JA
242 if (td->o.use_os_rand) {
243 r = os_random_long(&td->file_size_state);
244 sized = td->o.file_size_high - td->o.file_size_low;
245 ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
246 } else {
247 r = __rand(&td->__file_size_state);
248 sized = td->o.file_size_high - td->o.file_size_low;
249 ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0)));
250 }
251
5ec10eaa 252 ret += td->o.file_size_low;
2dc1bbeb 253 ret -= (ret % td->o.rw_min_bs);
9c60ce64
JA
254 return ret;
255}
256
53cdc686
JA
257static int file_size(struct thread_data *td, struct fio_file *f)
258{
259 struct stat st;
260
df9c26b1 261 if (stat(f->file_name, &st) == -1) {
7bb48f84
JA
262 td_verror(td, errno, "fstat");
263 return 1;
264 }
53cdc686 265
7bb48f84 266 f->real_file_size = st.st_size;
53cdc686
JA
267 return 0;
268}
269
270static int bdev_size(struct thread_data *td, struct fio_file *f)
271{
9b836561 272 unsigned long long bytes = 0;
53cdc686
JA
273 int r;
274
df9c26b1
JA
275 if (td->io_ops->open_file(td, f)) {
276 log_err("fio: failed opening blockdev %s for size check\n",
277 f->file_name);
278 return 1;
279 }
280
ecc314ba 281 r = blockdev_size(f, &bytes);
53cdc686 282 if (r) {
e1161c32 283 td_verror(td, r, "blockdev_size");
df9c26b1 284 goto err;
53cdc686
JA
285 }
286
7ab077ab
JA
287 if (!bytes) {
288 log_err("%s: zero sized block device?\n", f->file_name);
df9c26b1 289 goto err;
7ab077ab
JA
290 }
291
53cdc686 292 f->real_file_size = bytes;
22a57ba8 293 td->io_ops->close_file(td, f);
53cdc686 294 return 0;
df9c26b1
JA
295err:
296 td->io_ops->close_file(td, f);
297 return 1;
53cdc686
JA
298}
299
4ccdccd1
JA
300static int char_size(struct thread_data *td, struct fio_file *f)
301{
302#ifdef FIO_HAVE_CHARDEV_SIZE
9b836561 303 unsigned long long bytes = 0;
4ccdccd1
JA
304 int r;
305
306 if (td->io_ops->open_file(td, f)) {
307 log_err("fio: failed opening blockdev %s for size check\n",
308 f->file_name);
309 return 1;
310 }
311
ecc314ba 312 r = chardev_size(f, &bytes);
4ccdccd1
JA
313 if (r) {
314 td_verror(td, r, "chardev_size");
315 goto err;
316 }
317
318 if (!bytes) {
319 log_err("%s: zero sized char device?\n", f->file_name);
320 goto err;
321 }
322
323 f->real_file_size = bytes;
324 td->io_ops->close_file(td, f);
325 return 0;
326err:
327 td->io_ops->close_file(td, f);
328 return 1;
329#else
330 f->real_file_size = -1ULL;
331 return 0;
332#endif
333}
334
53cdc686
JA
335static int get_file_size(struct thread_data *td, struct fio_file *f)
336{
337 int ret = 0;
338
d6aed795 339 if (fio_file_size_known(f))
409b3417
JA
340 return 0;
341
7bb48f84
JA
342 if (f->filetype == FIO_TYPE_FILE)
343 ret = file_size(td, f);
344 else if (f->filetype == FIO_TYPE_BD)
53cdc686 345 ret = bdev_size(td, f);
4ccdccd1
JA
346 else if (f->filetype == FIO_TYPE_CHAR)
347 ret = char_size(td, f);
53cdc686
JA
348 else
349 f->real_file_size = -1;
350
351 if (ret)
352 return ret;
353
354 if (f->file_offset > f->real_file_size) {
0f2152c1 355 log_err("%s: offset extends end (%llu > %llu)\n", td->o.name,
5ec10eaa 356 f->file_offset, f->real_file_size);
53cdc686
JA
357 return 1;
358 }
359
d6aed795 360 fio_file_set_size_known(f);
53cdc686
JA
361 return 0;
362}
363
3baddf24
JA
364static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
365 unsigned long long off,
366 unsigned long long len)
e5b401d4
JA
367{
368 int ret = 0;
369
5e0074c2 370 if (len == -1ULL)
3baddf24
JA
371 len = f->io_size;
372 if (off == -1ULL)
373 off = f->file_offset;
ee56ad50 374
0d1cd207
JA
375 if (len == -1ULL || off == -1ULL)
376 return 0;
377
3baddf24
JA
378 dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off,
379 len);
b5af8293 380
e5b401d4
JA
381 /*
382 * FIXME: add blockdev flushing too
383 */
a1c58075 384 if (f->mmap_ptr) {
03e20d68 385 ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED);
a1c58075 386#ifdef FIO_MADV_FREE
03e20d68 387 (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE);
a1c58075
JA
388#endif
389 } else if (f->filetype == FIO_TYPE_FILE) {
ecc314ba 390 ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED);
5ec10eaa 391 } else if (f->filetype == FIO_TYPE_BD) {
ecc314ba 392 ret = blockdev_invalidate_cache(f);
7e0e25c9 393 if (ret < 0 && errno == EACCES && geteuid()) {
7172cfe8 394 if (!root_warn) {
5ec10eaa
JA
395 log_err("fio: only root may flush block "
396 "devices. Cache flush bypassed!\n");
7172cfe8
JA
397 root_warn = 1;
398 }
7e0e25c9
JA
399 ret = 0;
400 }
b5605e9d 401 } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE)
e5b401d4
JA
402 ret = 0;
403
404 if (ret < 0) {
e1161c32 405 td_verror(td, errno, "invalidate_cache");
e5b401d4 406 return 1;
3baddf24
JA
407 } else if (ret > 0) {
408 td_verror(td, ret, "invalidate_cache");
409 return 1;
e5b401d4
JA
410 }
411
ad2da605 412 return ret;
3baddf24
JA
413
414}
415
416int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
417{
d6aed795 418 if (!fio_file_open(f))
a5fb461f
JA
419 return 0;
420
5e0074c2 421 return __file_invalidate_cache(td, f, -1ULL, -1ULL);
e5b401d4
JA
422}
423
6977bcd0 424int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 425{
6977bcd0
JA
426 int ret = 0;
427
ee56ad50 428 dprint(FD_FILE, "fd close %s\n", f->file_name);
4906e0b5
JA
429
430 remove_file_hash(f);
431
6977bcd0
JA
432 if (close(f->fd) < 0)
433 ret = errno;
434
b5af8293 435 f->fd = -1;
6977bcd0 436 return ret;
53cdc686
JA
437}
438
1ccc6dc7 439int file_lookup_open(struct fio_file *f, int flags)
53cdc686 440{
29c1349f 441 struct fio_file *__f;
4d4e80f2
JA
442 int from_hash;
443
444 __f = lookup_file_hash(f->file_name);
445 if (__f) {
9efef3c4 446 dprint(FD_FILE, "found file in hash %s\n", f->file_name);
4d4e80f2
JA
447 /*
448 * racy, need the __f->lock locked
449 */
450 f->lock = __f->lock;
451 f->lock_owner = __f->lock_owner;
452 f->lock_batch = __f->lock_batch;
453 f->lock_ddir = __f->lock_ddir;
4d4e80f2
JA
454 from_hash = 1;
455 } else {
9efef3c4 456 dprint(FD_FILE, "file not found in hash %s\n", f->file_name);
4d4e80f2
JA
457 from_hash = 0;
458 }
459
e8670ef8 460 f->fd = open(f->file_name, flags, 0600);
4d4e80f2
JA
461 return from_hash;
462}
463
464int generic_open_file(struct thread_data *td, struct fio_file *f)
465{
66159828 466 int is_std = 0;
53cdc686 467 int flags = 0;
29c1349f 468 int from_hash = 0;
53cdc686 469
ee56ad50
JA
470 dprint(FD_FILE, "fd open %s\n", f->file_name);
471
6eaf09d6
SL
472 if (td_trim(td) && f->filetype != FIO_TYPE_BD) {
473 log_err("fio: trim only applies to block device\n");
474 return 1;
475 }
476
66159828
JA
477 if (!strcmp(f->file_name, "-")) {
478 if (td_rw(td)) {
479 log_err("fio: can't read/write to stdin/out\n");
480 return 1;
481 }
482 is_std = 1;
ce98fa66
JA
483
484 /*
485 * move output logging to stderr, if we are writing to stdout
486 */
487 if (td_write(td))
488 f_out = stderr;
66159828
JA
489 }
490
6eaf09d6
SL
491 if (td_trim(td))
492 goto skip_flags;
2dc1bbeb 493 if (td->o.odirect)
2fd233b7 494 flags |= OS_O_DIRECT;
2dc1bbeb 495 if (td->o.sync_io)
2fd233b7 496 flags |= O_SYNC;
814452bd
JA
497 if (td->o.create_on_open)
498 flags |= O_CREAT;
6eaf09d6
SL
499skip_flags:
500 if (f->filetype != FIO_TYPE_FILE)
501 flags |= FIO_O_NOATIME;
53cdc686 502
056f3459 503open_again:
660a1cb5 504 if (td_write(td)) {
17308158
JA
505 if (!read_only)
506 flags |= O_RDWR;
53cdc686 507
af52b345 508 if (f->filetype == FIO_TYPE_FILE)
2fd233b7 509 flags |= O_CREAT;
2fd233b7 510
66159828
JA
511 if (is_std)
512 f->fd = dup(STDOUT_FILENO);
4d4e80f2
JA
513 else
514 from_hash = file_lookup_open(f, flags);
6eaf09d6 515 } else if (td_read(td)) {
4241ea8f 516 if (f->filetype == FIO_TYPE_CHAR && !read_only)
2fd233b7
JA
517 flags |= O_RDWR;
518 else
519 flags |= O_RDONLY;
520
66159828
JA
521 if (is_std)
522 f->fd = dup(STDIN_FILENO);
4d4e80f2
JA
523 else
524 from_hash = file_lookup_open(f, flags);
6eaf09d6
SL
525 } else { //td trim
526 flags |= O_RDWR;
527 from_hash = file_lookup_open(f, flags);
53cdc686
JA
528 }
529
530 if (f->fd == -1) {
e4e33258 531 char buf[FIO_VERROR_SIZE];
e1161c32
JA
532 int __e = errno;
533
835d9b9e 534 if (__e == EPERM && (flags & FIO_O_NOATIME)) {
5921e80c 535 flags &= ~FIO_O_NOATIME;
056f3459
AC
536 goto open_again;
537 }
538
e8670ef8 539 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
e4e33258 540
a93c5f04
JA
541 if (__e == EINVAL && (flags & OS_O_DIRECT)) {
542 log_err("fio: looks like your file system does not " \
543 "support direct=1/buffered=0\n");
544 }
545
e4e33258 546 td_verror(td, __e, buf);
53cdc686
JA
547 }
548
29c1349f
JA
549 if (!from_hash && f->fd != -1) {
550 if (add_file_hash(f)) {
3f0ca9b9 551 int fio_unused ret;
29c1349f
JA
552
553 /*
554 * OK to ignore, we haven't done anything with it
555 */
556 ret = generic_close_file(td, f);
557 goto open_again;
558 }
559 }
4906e0b5 560
53cdc686 561 return 0;
b5af8293
JA
562}
563
df9c26b1 564int generic_get_file_size(struct thread_data *td, struct fio_file *f)
21972cde 565{
df9c26b1 566 return get_file_size(td, f);
21972cde
JA
567}
568
7bb48f84
JA
569/*
570 * open/close all files, so that ->real_file_size gets set
571 */
bab3fd58 572static int get_file_sizes(struct thread_data *td)
7bb48f84
JA
573{
574 struct fio_file *f;
575 unsigned int i;
bab3fd58 576 int err = 0;
7bb48f84
JA
577
578 for_each_file(td, f, i) {
5ec10eaa
JA
579 dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i,
580 f->file_name);
9efef3c4 581
99a47c69 582 if (td_io_get_file_size(td, f)) {
40b44f4a
JA
583 if (td->error != ENOENT) {
584 log_err("%s\n", td->verror);
585 err = 1;
586 }
541d66d7 587 clear_error(td);
07eb79df 588 }
409b3417
JA
589
590 if (f->real_file_size == -1ULL && td->o.size)
591 f->real_file_size = td->o.size / td->o.nr_files;
7bb48f84 592 }
bab3fd58
JA
593
594 return err;
7bb48f84
JA
595}
596
2e3bd4c2
JA
597struct fio_mount {
598 struct flist_head list;
599 const char *base;
600 char __base[256];
601 unsigned int key;
602};
603
604/*
605 * Get free number of bytes for each file on each unique mount.
606 */
607static unsigned long long get_fs_free_counts(struct thread_data *td)
608{
609 struct flist_head *n, *tmp;
68b0316f 610 unsigned long long ret = 0;
2e3bd4c2
JA
611 struct fio_mount *fm;
612 FLIST_HEAD(list);
613 struct fio_file *f;
614 unsigned int i;
615
616 for_each_file(td, f, i) {
617 struct stat sb;
618 char buf[256];
619
4ccdccd1
JA
620 if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) {
621 if (f->real_file_size != -1ULL)
622 ret += f->real_file_size;
68b0316f
JA
623 continue;
624 } else if (f->filetype != FIO_TYPE_FILE)
625 continue;
626
2e3bd4c2
JA
627 strcpy(buf, f->file_name);
628
629 if (stat(buf, &sb) < 0) {
630 if (errno != ENOENT)
631 break;
632 strcpy(buf, ".");
633 if (stat(buf, &sb) < 0)
634 break;
635 }
636
637 fm = NULL;
638 flist_for_each(n, &list) {
639 fm = flist_entry(n, struct fio_mount, list);
640 if (fm->key == sb.st_dev)
641 break;
642
643 fm = NULL;
644 }
645
646 if (fm)
647 continue;
648
649 fm = malloc(sizeof(*fm));
650 strcpy(fm->__base, buf);
651 fm->base = basename(fm->__base);
652 fm->key = sb.st_dev;
653 flist_add(&fm->list, &list);
654 }
655
2e3bd4c2
JA
656 flist_for_each_safe(n, tmp, &list) {
657 unsigned long long sz;
658
659 fm = flist_entry(n, struct fio_mount, list);
660 flist_del(&fm->list);
661
662 sz = get_fs_size(fm->base);
663 if (sz && sz != -1ULL)
664 ret += sz;
665
666 free(fm);
667 }
668
669 return ret;
670}
671
ce95d651
DE
672unsigned long long get_start_offset(struct thread_data *td)
673{
674 return td->o.start_offset +
675 (td->thread_number - 1) * td->o.offset_increment;
676}
677
7bb48f84
JA
678/*
679 * Open the files and setup files sizes, creating files if necessary.
680 */
53cdc686
JA
681int setup_files(struct thread_data *td)
682{
7bb48f84 683 unsigned long long total_size, extend_size;
53cdc686 684 struct fio_file *f;
af52b345 685 unsigned int i;
000b0803 686 int err = 0, need_extend;
53cdc686 687
ee56ad50
JA
688 dprint(FD_FILE, "setup files\n");
689
691c8fb0 690 if (td->o.read_iolog_file)
25460cf6 691 goto done;
691c8fb0 692
53cdc686
JA
693 /*
694 * if ioengine defines a setup() method, it's responsible for
7bb48f84
JA
695 * opening the files and setting f->real_file_size to indicate
696 * the valid range for that file.
53cdc686
JA
697 */
698 if (td->io_ops->setup)
7bb48f84
JA
699 err = td->io_ops->setup(td);
700 else
bab3fd58 701 err = get_file_sizes(td);
53cdc686 702
f1027063
JA
703 if (err)
704 return err;
705
0a7eb121 706 /*
7bb48f84
JA
707 * check sizes. if the files/devices do not exist and the size
708 * isn't passed to fio, abort.
0a7eb121 709 */
7bb48f84
JA
710 total_size = 0;
711 for_each_file(td, f, i) {
712 if (f->real_file_size == -1ULL)
713 total_size = -1ULL;
714 else
715 total_size += f->real_file_size;
716 }
0a7eb121 717
2e3bd4c2
JA
718 if (td->o.fill_device)
719 td->fill_device_size = get_fs_free_counts(td);
720
7bb48f84
JA
721 /*
722 * device/file sizes are zero and no size given, punt
723 */
1f809d15 724 if ((!total_size || total_size == -1ULL) && !td->o.size &&
aa31f1f1 725 !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) {
7bb48f84 726 log_err("%s: you need to specify size=\n", td->o.name);
e1161c32 727 td_verror(td, EINVAL, "total_file_size");
53cdc686
JA
728 return 1;
729 }
730
7bb48f84
JA
731 /*
732 * now file sizes are known, so we can set ->io_size. if size= is
733 * not given, ->io_size is just equal to ->real_file_size. if size
734 * is given, ->io_size is size / nr_files.
735 */
736 extend_size = total_size = 0;
737 need_extend = 0;
738 for_each_file(td, f, i) {
ce95d651 739 f->file_offset = get_start_offset(td);
bcdedd0a 740
7bb48f84
JA
741 if (!td->o.file_size_low) {
742 /*
743 * no file size range given, file size is equal to
744 * total size divided by number of files. if that is
745 * zero, set it to the real file size.
746 */
747 f->io_size = td->o.size / td->o.nr_files;
65bdb10a 748 if (!f->io_size)
273f8c91 749 f->io_size = f->real_file_size - f->file_offset;
7bb48f84
JA
750 } else if (f->real_file_size < td->o.file_size_low ||
751 f->real_file_size > td->o.file_size_high) {
5ec10eaa 752 if (f->file_offset > td->o.file_size_low)
bcdedd0a 753 goto err_offset;
7bb48f84
JA
754 /*
755 * file size given. if it's fixed, use that. if it's a
756 * range, generate a random size in-between.
757 */
5ec10eaa
JA
758 if (td->o.file_size_low == td->o.file_size_high) {
759 f->io_size = td->o.file_size_low
760 - f->file_offset;
761 } else {
762 f->io_size = get_rand_file_size(td)
763 - f->file_offset;
764 }
65bdb10a 765 } else
bcdedd0a 766 f->io_size = f->real_file_size - f->file_offset;
53cdc686 767
7bb48f84
JA
768 if (f->io_size == -1ULL)
769 total_size = -1ULL;
4d002569
SL
770 else {
771 if (td->o.size_percent)
772 f->io_size = (f->io_size * td->o.size_percent) / 100;
7bb48f84 773 total_size += f->io_size;
4d002569 774 }
7bb48f84
JA
775
776 if (f->filetype == FIO_TYPE_FILE &&
bcdedd0a 777 (f->io_size + f->file_offset) > f->real_file_size &&
7bb48f84 778 !(td->io_ops->flags & FIO_DISKLESSIO)) {
814452bd
JA
779 if (!td->o.create_on_open) {
780 need_extend++;
781 extend_size += (f->io_size + f->file_offset);
782 } else
783 f->real_file_size = f->io_size + f->file_offset;
d6aed795 784 fio_file_set_extend(f);
5ec10eaa 785 }
7bb48f84 786 }
53cdc686 787
2298290e 788 if (!td->o.size || td->o.size > total_size)
7bb48f84 789 td->o.size = total_size;
21972cde 790
7bb48f84
JA
791 /*
792 * See if we need to extend some files
793 */
794 if (need_extend) {
795 temp_stall_ts = 1;
f3afa57e 796 if (output_format == FIO_OUTPUT_NORMAL)
a7ba8c5f 797 log_info("%s: Laying out IO file(s) (%u file(s) /"
0f2152c1 798 " %lluMB)\n", td->o.name, need_extend,
a7ba8c5f 799 extend_size >> 20);
7bb48f84
JA
800
801 for_each_file(td, f, i) {
5e0074c2 802 unsigned long long old_len = -1ULL, extend_len = -1ULL;
3baddf24 803
d6aed795 804 if (!fio_file_extend(f))
7bb48f84
JA
805 continue;
806
409b3417 807 assert(f->filetype == FIO_TYPE_FILE);
d6aed795 808 fio_file_clear_extend(f);
5e0074c2
JA
809 if (!td->o.fill_device) {
810 old_len = f->real_file_size;
0b9d69ec
JA
811 extend_len = f->io_size + f->file_offset -
812 old_len;
5e0074c2 813 }
bcdedd0a 814 f->real_file_size = (f->io_size + f->file_offset);
7bb48f84
JA
815 err = extend_file(td, f);
816 if (err)
3baddf24 817 break;
5e0074c2 818
3baddf24
JA
819 err = __file_invalidate_cache(td, f, old_len,
820 extend_len);
821 close(f->fd);
822 f->fd = -1;
823 if (err)
7bb48f84
JA
824 break;
825 }
826 temp_stall_ts = 0;
827 }
828
829 if (err)
830 return err;
831
832 if (!td->o.zone_size)
833 td->o.zone_size = td->o.size;
834
ea966f81
JA
835 /*
836 * iolog already set the total io size, if we read back
837 * stored entries.
838 */
839 if (!td->o.read_iolog_file)
840 td->total_io_size = td->o.size * td->o.loops;
25460cf6
JA
841
842done:
843 if (td->o.create_only)
844 td->done = 1;
845
7bb48f84 846 return 0;
bcdedd0a
YHJT
847err_offset:
848 log_err("%s: you need to specify valid offset=\n", td->o.name);
849 return 1;
53cdc686
JA
850}
851
afad68f7
ZY
852int pre_read_files(struct thread_data *td)
853{
854 struct fio_file *f;
855 unsigned int i;
856
857 dprint(FD_FILE, "pre_read files\n");
858
859 for_each_file(td, f, i) {
860 pre_read_file(td, f);
861 }
862
863 return 1;
864}
865
9c6f6316
JA
866static int __init_rand_distribution(struct thread_data *td, struct fio_file *f)
867{
2316296a 868 unsigned int range_size, seed;
9c6f6316
JA
869 unsigned long nranges;
870
871 range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
872
873 nranges = (f->real_file_size + range_size - 1) / range_size;
874
2316296a 875 seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number;
8425687e
JA
876 if (!td->o.rand_repeatable)
877 seed = td->rand_seeds[4];
878
9c6f6316 879 if (td->o.random_distribution == FIO_RAND_DIST_ZIPF)
2316296a 880 zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed);
9c6f6316 881 else
2316296a 882 pareto_init(&f->zipf, nranges, td->o.pareto_h, seed);
9c6f6316
JA
883
884 return 1;
885}
886
887static int init_rand_distribution(struct thread_data *td)
888{
889 struct fio_file *f;
890 unsigned int i;
891 int state;
892
893 if (td->o.random_distribution == FIO_RAND_DIST_RANDOM)
894 return 0;
895
896 state = td->runstate;
897 td_set_runstate(td, TD_SETTING_UP);
898 for_each_file(td, f, i)
899 __init_rand_distribution(td, f);
900 td_set_runstate(td, state);
901
902 return 1;
903}
904
68727076
JA
905int init_random_map(struct thread_data *td)
906{
509eab12 907 unsigned long long blocks, num_maps;
68727076
JA
908 struct fio_file *f;
909 unsigned int i;
910
9c6f6316
JA
911 if (init_rand_distribution(td))
912 return 0;
de8dd119 913 if (td->o.norandommap || !td_random(td))
68727076
JA
914 return 0;
915
916 for_each_file(td, f, i) {
5ec10eaa
JA
917 blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
918 (unsigned long long) td->o.rw_min_bs;
919 num_maps = (blocks + BLOCKS_PER_MAP - 1) /
920 (unsigned long long) BLOCKS_PER_MAP;
ceadd59e
JA
921 if (num_maps == (unsigned long) num_maps) {
922 f->file_map = smalloc(num_maps * sizeof(unsigned long));
923 if (f->file_map) {
924 f->num_maps = num_maps;
925 continue;
926 }
927 } else
928 f->file_map = NULL;
929
2b386d25 930 if (!td->o.softrandommap) {
5ec10eaa
JA
931 log_err("fio: failed allocating random map. If running"
932 " a large number of jobs, try the 'norandommap'"
2b386d25
JA
933 " option or set 'softrandommap'. Or give"
934 " a larger --alloc-size to fio.\n");
68727076
JA
935 return 1;
936 }
303032ae
JA
937
938 log_info("fio: file %s failed allocating random map. Running "
939 "job without.\n", f->file_name);
940 f->num_maps = 0;
68727076
JA
941 }
942
943 return 0;
944}
945
53cdc686
JA
946void close_files(struct thread_data *td)
947{
0ab8db89 948 struct fio_file *f;
af52b345 949 unsigned int i;
53cdc686 950
2be3eec3
JA
951 for_each_file(td, f, i) {
952 if (fio_file_open(f))
953 td_io_close_file(td, f);
954 }
24ffd2c2
JA
955}
956
957void close_and_free_files(struct thread_data *td)
958{
959 struct fio_file *f;
960 unsigned int i;
961
ee56ad50
JA
962 dprint(FD_FILE, "close files\n");
963
0ab8db89 964 for_each_file(td, f, i) {
ffdfabd4
JA
965 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
966 dprint(FD_FILE, "free unlink %s\n", f->file_name);
132ad46d 967 unlink(f->file_name);
ffdfabd4 968 }
bdb4e2e9 969
22a57ba8
JA
970 if (fio_file_open(f))
971 td_io_close_file(td, f);
972
b9fbcf21 973 remove_file_hash(f);
b3dc7f07 974
f17c4392 975 sfree(f->file_name);
fa1da865 976 f->file_name = NULL;
61eb313e
JA
977 sfree(f->file_map);
978 f->file_map = NULL;
78d99e6a 979 sfree(f);
53cdc686 980 }
b4a6a59a 981
2dc1bbeb 982 td->o.filename = NULL;
cade3ef4 983 free(td->files);
9efef3c4 984 td->files_index = 0;
b4a6a59a 985 td->files = NULL;
2dc1bbeb 986 td->o.nr_files = 0;
53cdc686 987}
af52b345 988
e3bab463 989static void get_file_type(struct fio_file *f)
af52b345
JA
990{
991 struct stat sb;
992
66159828
JA
993 if (!strcmp(f->file_name, "-"))
994 f->filetype = FIO_TYPE_PIPE;
995 else
996 f->filetype = FIO_TYPE_FILE;
af52b345 997
3892182a
BC
998 /* \\.\ is the device namespace in Windows, where every file is
999 * a block device */
1000 if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
1001 f->filetype = FIO_TYPE_BD;
1002
b30d395e 1003 if (!stat(f->file_name, &sb)) {
3892182a 1004 if (S_ISBLK(sb.st_mode))
af52b345
JA
1005 f->filetype = FIO_TYPE_BD;
1006 else if (S_ISCHR(sb.st_mode))
1007 f->filetype = FIO_TYPE_CHAR;
b5605e9d
JA
1008 else if (S_ISFIFO(sb.st_mode))
1009 f->filetype = FIO_TYPE_PIPE;
af52b345
JA
1010 }
1011}
1012
f29b25a3 1013int add_file(struct thread_data *td, const char *fname)
af52b345 1014{
7b4e4fe5 1015 int cur_files = td->files_index;
bd0ee748 1016 char file_name[PATH_MAX];
af52b345 1017 struct fio_file *f;
bd0ee748 1018 int len = 0;
af52b345 1019
ee56ad50
JA
1020 dprint(FD_FILE, "add file %s\n", fname);
1021
f17c4392 1022 f = smalloc(sizeof(*f));
c48c0be7
JA
1023 if (!f) {
1024 log_err("fio: smalloc OOM\n");
1025 assert(0);
1026 }
0b9d69ec 1027
af52b345 1028 f->fd = -1;
38dad62d 1029 fio_file_reset(f);
bd0ee748 1030
fc99bc0d 1031 if (td->files_size <= td->files_index) {
4b341fca 1032 int new_size = td->o.nr_files + 1;
126d65c6 1033
fc99bc0d
JA
1034 dprint(FD_FILE, "resize file array to %d files\n", new_size);
1035
1036 td->files = realloc(td->files, new_size * sizeof(f));
fc99bc0d
JA
1037 td->files_size = new_size;
1038 }
8bb7679e 1039 td->files[cur_files] = f;
89ac1d48 1040 f->fileno = cur_files;
126d65c6 1041
07eb79df
JA
1042 /*
1043 * init function, io engine may not be loaded yet
1044 */
1045 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
1046 f->real_file_size = -1ULL;
1047
bd0ee748
JA
1048 if (td->o.directory)
1049 len = sprintf(file_name, "%s/", td->o.directory);
1050
1051 sprintf(file_name + len, "%s", fname);
f17c4392 1052 f->file_name = smalloc_strdup(file_name);
c48c0be7
JA
1053 if (!f->file_name) {
1054 log_err("fio: smalloc OOM\n");
1055 assert(0);
1056 }
0b9d69ec 1057
e3bab463 1058 get_file_type(f);
af52b345 1059
4d4e80f2
JA
1060 switch (td->o.file_lock_mode) {
1061 case FILE_LOCK_NONE:
1062 break;
1063 case FILE_LOCK_READWRITE:
1064 f->lock = fio_mutex_rw_init();
1065 break;
1066 case FILE_LOCK_EXCLUSIVE:
521da527 1067 f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
4d4e80f2
JA
1068 break;
1069 default:
1070 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
1071 assert(0);
1072 }
29c1349f 1073
7b4e4fe5 1074 td->files_index++;
1549441c
JA
1075 if (f->filetype == FIO_TYPE_FILE)
1076 td->nr_normal_files++;
f29b25a3 1077
5ec10eaa
JA
1078 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
1079 cur_files);
9efef3c4 1080
f29b25a3 1081 return cur_files;
af52b345 1082}
0ad920e7 1083
49ffb4a2
JA
1084int add_file_exclusive(struct thread_data *td, const char *fname)
1085{
1086 struct fio_file *f;
1087 unsigned int i;
1088
1089 for_each_file(td, f, i) {
1090 if (!strcmp(f->file_name, fname))
1091 return i;
1092 }
1093
1094 return add_file(td, fname);
1095}
1096
0ad920e7
JA
1097void get_file(struct fio_file *f)
1098{
8172fe97 1099 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
d6aed795 1100 assert(fio_file_open(f));
0ad920e7
JA
1101 f->references++;
1102}
1103
6977bcd0 1104int put_file(struct thread_data *td, struct fio_file *f)
0ad920e7 1105{
98e1ac4e 1106 int f_ret = 0, ret = 0;
6977bcd0 1107
8172fe97 1108 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
ee56ad50 1109
22a57ba8
JA
1110 if (!fio_file_open(f)) {
1111 assert(f->fd == -1);
6977bcd0 1112 return 0;
22a57ba8 1113 }
0ad920e7
JA
1114
1115 assert(f->references);
1116 if (--f->references)
6977bcd0 1117 return 0;
0ad920e7 1118
d424d4dd 1119 if (should_fsync(td) && td->o.fsync_on_close)
98e1ac4e 1120 f_ret = fsync(f->fd);
ebb1415f 1121
0ad920e7 1122 if (td->io_ops->close_file)
6977bcd0 1123 ret = td->io_ops->close_file(td, f);
1020a139 1124
98e1ac4e 1125 if (!ret)
a5fb461f 1126 ret = f_ret;
98e1ac4e 1127
0ad920e7 1128 td->nr_open_files--;
d6aed795 1129 fio_file_clear_open(f);
22a57ba8 1130 assert(f->fd == -1);
6977bcd0 1131 return ret;
0ad920e7 1132}
bbf6b540 1133
4d4e80f2 1134void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
b2bd2bd9 1135{
4d4e80f2
JA
1136 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1137 return;
29c1349f 1138
4d4e80f2
JA
1139 if (f->lock_owner == td && f->lock_batch--)
1140 return;
1141
1142 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1143 if (ddir == DDIR_READ)
1144 fio_mutex_down_read(f->lock);
1145 else
1146 fio_mutex_down_write(f->lock);
1147 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
1148 fio_mutex_down(f->lock);
1149
1150 f->lock_owner = td;
1151 f->lock_batch = td->o.lockfile_batch;
1152 f->lock_ddir = ddir;
b2bd2bd9
JA
1153}
1154
4d4e80f2 1155void unlock_file(struct thread_data *td, struct fio_file *f)
b2bd2bd9 1156{
4d4e80f2
JA
1157 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
1158 return;
1159 if (f->lock_batch)
1160 return;
1161
1162 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
1163 const int is_read = f->lock_ddir == DDIR_READ;
1164 int val = fio_mutex_getval(f->lock);
29c1349f 1165
4d4e80f2
JA
1166 if ((is_read && val == 1) || (!is_read && val == -1))
1167 f->lock_owner = NULL;
29c1349f 1168
4d4e80f2
JA
1169 if (is_read)
1170 fio_mutex_up_read(f->lock);
1171 else
1172 fio_mutex_up_write(f->lock);
1173 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
1174 int val = fio_mutex_getval(f->lock);
1175
1176 if (val == 0)
1177 f->lock_owner = NULL;
1178
1179 fio_mutex_up(f->lock);
29c1349f 1180 }
b2bd2bd9
JA
1181}
1182
4d4e80f2
JA
1183void unlock_file_all(struct thread_data *td, struct fio_file *f)
1184{
1185 if (f->lock_owner != td)
1186 return;
1187
1188 f->lock_batch = 0;
1189 unlock_file(td, f);
1190}
1191
bbf6b540
JA
1192static int recurse_dir(struct thread_data *td, const char *dirname)
1193{
1194 struct dirent *dir;
1195 int ret = 0;
1196 DIR *D;
1197
1198 D = opendir(dirname);
1199 if (!D) {
0ddb270c
JA
1200 char buf[FIO_VERROR_SIZE];
1201
1202 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
1203 td_verror(td, errno, buf);
bbf6b540
JA
1204 return 1;
1205 }
1206
1207 while ((dir = readdir(D)) != NULL) {
1208 char full_path[PATH_MAX];
1209 struct stat sb;
1210
e85b2b83
JA
1211 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1212 continue;
96d32d51 1213
b9fd788f 1214 sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
bbf6b540
JA
1215
1216 if (lstat(full_path, &sb) == -1) {
1217 if (errno != ENOENT) {
1218 td_verror(td, errno, "stat");
1219 return 1;
1220 }
1221 }
1222
1223 if (S_ISREG(sb.st_mode)) {
1224 add_file(td, full_path);
2dc1bbeb 1225 td->o.nr_files++;
bbf6b540
JA
1226 continue;
1227 }
0ddb270c
JA
1228 if (!S_ISDIR(sb.st_mode))
1229 continue;
bbf6b540 1230
5ec10eaa
JA
1231 ret = recurse_dir(td, full_path);
1232 if (ret)
bbf6b540
JA
1233 break;
1234 }
1235
1236 closedir(D);
1237 return ret;
1238}
1239
1240int add_dir_files(struct thread_data *td, const char *path)
1241{
0ddb270c
JA
1242 int ret = recurse_dir(td, path);
1243
1244 if (!ret)
1245 log_info("fio: opendir added %d files\n", td->o.nr_files);
1246
1247 return ret;
bbf6b540 1248}
cade3ef4
JA
1249
1250void dup_files(struct thread_data *td, struct thread_data *org)
1251{
1252 struct fio_file *f;
1253 unsigned int i;
9efef3c4
JA
1254
1255 dprint(FD_FILE, "dup files: %d\n", org->files_index);
cade3ef4
JA
1256
1257 if (!org->files)
1258 return;
1259
9efef3c4 1260 td->files = malloc(org->files_index * sizeof(f));
cade3ef4 1261
9efef3c4 1262 for_each_file(org, f, i) {
b0fe421a
JA
1263 struct fio_file *__f;
1264
f17c4392 1265 __f = smalloc(sizeof(*__f));
c48c0be7
JA
1266 if (!__f) {
1267 log_err("fio: smalloc OOM\n");
1268 assert(0);
1269 }
22a57ba8 1270 __f->fd = -1;
38dad62d 1271 fio_file_reset(__f);
0b9d69ec 1272
bc3456fa 1273 if (f->file_name) {
f17c4392 1274 __f->file_name = smalloc_strdup(f->file_name);
c48c0be7
JA
1275 if (!__f->file_name) {
1276 log_err("fio: smalloc OOM\n");
1277 assert(0);
1278 }
0b9d69ec 1279
bc3456fa
AC
1280 __f->filetype = f->filetype;
1281 }
b0fe421a
JA
1282
1283 td->files[i] = __f;
cade3ef4
JA
1284 }
1285}
f29b25a3
JA
1286
1287/*
1288 * Returns the index that matches the filename, or -1 if not there
1289 */
1290int get_fileno(struct thread_data *td, const char *fname)
1291{
1292 struct fio_file *f;
1293 unsigned int i;
1294
1295 for_each_file(td, f, i)
1296 if (!strcmp(f->file_name, fname))
1297 return i;
1298
1299 return -1;
1300}
1301
1302/*
1303 * For log usage, where we add/open/close files automatically
1304 */
1305void free_release_files(struct thread_data *td)
1306{
1307 close_files(td);
1308 td->files_index = 0;
1309 td->nr_normal_files = 0;
1310}