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