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