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