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