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