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