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