Add softrandommap and --alloc-size options
[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 }
2b386d25
JA
582 if (!td->o.softrandommap) {
583 log_err("fio: failed allocating random map. If running"
584 " a large number of jobs, try the 'norandommap'"
585 " option or set 'softrandommap'. Or give"
586 " a larger --alloc-size to fio.\n");
587 return 1;
588 }
303032ae
JA
589
590 log_info("fio: file %s failed allocating random map. Running "
591 "job without.\n", f->file_name);
592 f->num_maps = 0;
68727076
JA
593 }
594
595 return 0;
596}
597
53cdc686
JA
598void close_files(struct thread_data *td)
599{
0ab8db89 600 struct fio_file *f;
af52b345 601 unsigned int i;
53cdc686 602
24ffd2c2
JA
603 for_each_file(td, f, i)
604 td_io_close_file(td, f);
605}
606
607void close_and_free_files(struct thread_data *td)
608{
609 struct fio_file *f;
610 unsigned int i;
611
ee56ad50
JA
612 dprint(FD_FILE, "close files\n");
613
0ab8db89 614 for_each_file(td, f, i) {
ffdfabd4
JA
615 if (td->o.unlink && f->filetype == FIO_TYPE_FILE) {
616 dprint(FD_FILE, "free unlink %s\n", f->file_name);
132ad46d 617 unlink(f->file_name);
ffdfabd4 618 }
bdb4e2e9 619
b5af8293 620 td_io_close_file(td, f);
b3dc7f07 621
f17c4392 622 sfree(f->file_name);
fa1da865
JA
623 f->file_name = NULL;
624
c343981b 625 if (f->file_map) {
f17c4392 626 sfree(f->file_map);
c343981b
JA
627 f->file_map = NULL;
628 }
78d99e6a 629 sfree(f);
53cdc686 630 }
b4a6a59a 631
2dc1bbeb 632 td->o.filename = NULL;
cade3ef4 633 free(td->files);
9efef3c4 634 td->files_index = 0;
b4a6a59a 635 td->files = NULL;
2dc1bbeb 636 td->o.nr_files = 0;
53cdc686 637}
af52b345 638
e3bab463 639static void get_file_type(struct fio_file *f)
af52b345
JA
640{
641 struct stat sb;
642
66159828
JA
643 if (!strcmp(f->file_name, "-"))
644 f->filetype = FIO_TYPE_PIPE;
645 else
646 f->filetype = FIO_TYPE_FILE;
af52b345 647
e3bab463 648 if (!lstat(f->file_name, &sb)) {
af52b345
JA
649 if (S_ISBLK(sb.st_mode))
650 f->filetype = FIO_TYPE_BD;
651 else if (S_ISCHR(sb.st_mode))
652 f->filetype = FIO_TYPE_CHAR;
b5605e9d
JA
653 else if (S_ISFIFO(sb.st_mode))
654 f->filetype = FIO_TYPE_PIPE;
af52b345
JA
655 }
656}
657
f29b25a3 658int add_file(struct thread_data *td, const char *fname)
af52b345 659{
7b4e4fe5 660 int cur_files = td->files_index;
bd0ee748 661 char file_name[PATH_MAX];
af52b345 662 struct fio_file *f;
bd0ee748 663 int len = 0;
af52b345 664
ee56ad50
JA
665 dprint(FD_FILE, "add file %s\n", fname);
666
f17c4392 667 f = smalloc(sizeof(*f));
af52b345 668 f->fd = -1;
bd0ee748 669
9efef3c4 670 dprint(FD_FILE, "resize file array to %d files\n", cur_files + 1);
126d65c6 671
9efef3c4 672 td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
126d65c6
JA
673 td->files[cur_files] = f;
674
07eb79df
JA
675 /*
676 * init function, io engine may not be loaded yet
677 */
678 if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
679 f->real_file_size = -1ULL;
680
bd0ee748
JA
681 if (td->o.directory)
682 len = sprintf(file_name, "%s/", td->o.directory);
683
684 sprintf(file_name + len, "%s", fname);
f17c4392 685 f->file_name = smalloc_strdup(file_name);
af52b345 686
e3bab463 687 get_file_type(f);
af52b345 688
4d4e80f2
JA
689 switch (td->o.file_lock_mode) {
690 case FILE_LOCK_NONE:
691 break;
692 case FILE_LOCK_READWRITE:
693 f->lock = fio_mutex_rw_init();
694 break;
695 case FILE_LOCK_EXCLUSIVE:
696 f->lock = fio_mutex_init(1);
697 break;
698 default:
699 log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
700 assert(0);
701 }
29c1349f 702
7b4e4fe5 703 td->files_index++;
1549441c
JA
704 if (f->filetype == FIO_TYPE_FILE)
705 td->nr_normal_files++;
f29b25a3 706
5ec10eaa
JA
707 dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
708 cur_files);
9efef3c4 709
f29b25a3 710 return cur_files;
af52b345 711}
0ad920e7
JA
712
713void get_file(struct fio_file *f)
714{
8172fe97 715 dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
97af62ce 716 assert(f->flags & FIO_FILE_OPEN);
0ad920e7
JA
717 f->references++;
718}
719
6977bcd0 720int put_file(struct thread_data *td, struct fio_file *f)
0ad920e7 721{
98e1ac4e 722 int f_ret = 0, ret = 0;
6977bcd0 723
8172fe97 724 dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
ee56ad50 725
0ad920e7 726 if (!(f->flags & FIO_FILE_OPEN))
6977bcd0 727 return 0;
0ad920e7
JA
728
729 assert(f->references);
730 if (--f->references)
6977bcd0 731 return 0;
0ad920e7 732
d424d4dd 733 if (should_fsync(td) && td->o.fsync_on_close)
98e1ac4e 734 f_ret = fsync(f->fd);
ebb1415f 735
0ad920e7 736 if (td->io_ops->close_file)
6977bcd0 737 ret = td->io_ops->close_file(td, f);
1020a139 738
98e1ac4e
JA
739 if (!ret)
740 ret = !f_ret;
741
0ad920e7
JA
742 td->nr_open_files--;
743 f->flags &= ~FIO_FILE_OPEN;
6977bcd0 744 return ret;
0ad920e7 745}
bbf6b540 746
4d4e80f2 747void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
b2bd2bd9 748{
4d4e80f2
JA
749 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
750 return;
29c1349f 751
4d4e80f2
JA
752 if (f->lock_owner == td && f->lock_batch--)
753 return;
754
755 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
756 if (ddir == DDIR_READ)
757 fio_mutex_down_read(f->lock);
758 else
759 fio_mutex_down_write(f->lock);
760 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
761 fio_mutex_down(f->lock);
762
763 f->lock_owner = td;
764 f->lock_batch = td->o.lockfile_batch;
765 f->lock_ddir = ddir;
b2bd2bd9
JA
766}
767
4d4e80f2 768void unlock_file(struct thread_data *td, struct fio_file *f)
b2bd2bd9 769{
4d4e80f2
JA
770 if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
771 return;
772 if (f->lock_batch)
773 return;
774
775 if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
776 const int is_read = f->lock_ddir == DDIR_READ;
777 int val = fio_mutex_getval(f->lock);
29c1349f 778
4d4e80f2
JA
779 if ((is_read && val == 1) || (!is_read && val == -1))
780 f->lock_owner = NULL;
29c1349f 781
4d4e80f2
JA
782 if (is_read)
783 fio_mutex_up_read(f->lock);
784 else
785 fio_mutex_up_write(f->lock);
786 } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
787 int val = fio_mutex_getval(f->lock);
788
789 if (val == 0)
790 f->lock_owner = NULL;
791
792 fio_mutex_up(f->lock);
29c1349f 793 }
b2bd2bd9
JA
794}
795
4d4e80f2
JA
796void unlock_file_all(struct thread_data *td, struct fio_file *f)
797{
798 if (f->lock_owner != td)
799 return;
800
801 f->lock_batch = 0;
802 unlock_file(td, f);
803}
804
bbf6b540
JA
805static int recurse_dir(struct thread_data *td, const char *dirname)
806{
807 struct dirent *dir;
808 int ret = 0;
809 DIR *D;
810
811 D = opendir(dirname);
812 if (!D) {
0ddb270c
JA
813 char buf[FIO_VERROR_SIZE];
814
815 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
816 td_verror(td, errno, buf);
bbf6b540
JA
817 return 1;
818 }
819
820 while ((dir = readdir(D)) != NULL) {
821 char full_path[PATH_MAX];
822 struct stat sb;
823
e85b2b83
JA
824 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
825 continue;
96d32d51 826
bbf6b540
JA
827 sprintf(full_path, "%s/%s", dirname, dir->d_name);
828
829 if (lstat(full_path, &sb) == -1) {
830 if (errno != ENOENT) {
831 td_verror(td, errno, "stat");
832 return 1;
833 }
834 }
835
836 if (S_ISREG(sb.st_mode)) {
837 add_file(td, full_path);
2dc1bbeb 838 td->o.nr_files++;
bbf6b540
JA
839 continue;
840 }
0ddb270c
JA
841 if (!S_ISDIR(sb.st_mode))
842 continue;
bbf6b540 843
5ec10eaa
JA
844 ret = recurse_dir(td, full_path);
845 if (ret)
bbf6b540
JA
846 break;
847 }
848
849 closedir(D);
850 return ret;
851}
852
853int add_dir_files(struct thread_data *td, const char *path)
854{
0ddb270c
JA
855 int ret = recurse_dir(td, path);
856
857 if (!ret)
858 log_info("fio: opendir added %d files\n", td->o.nr_files);
859
860 return ret;
bbf6b540 861}
cade3ef4
JA
862
863void dup_files(struct thread_data *td, struct thread_data *org)
864{
865 struct fio_file *f;
866 unsigned int i;
9efef3c4
JA
867
868 dprint(FD_FILE, "dup files: %d\n", org->files_index);
cade3ef4
JA
869
870 if (!org->files)
871 return;
872
9efef3c4 873 td->files = malloc(org->files_index * sizeof(f));
cade3ef4 874
9efef3c4 875 for_each_file(org, f, i) {
b0fe421a
JA
876 struct fio_file *__f;
877
f17c4392 878 __f = smalloc(sizeof(*__f));
b0fe421a 879
cade3ef4 880 if (f->file_name)
f17c4392 881 __f->file_name = smalloc_strdup(f->file_name);
b0fe421a
JA
882
883 td->files[i] = __f;
cade3ef4
JA
884 }
885}
f29b25a3
JA
886
887/*
888 * Returns the index that matches the filename, or -1 if not there
889 */
890int get_fileno(struct thread_data *td, const char *fname)
891{
892 struct fio_file *f;
893 unsigned int i;
894
895 for_each_file(td, f, i)
896 if (!strcmp(f->file_name, fname))
897 return i;
898
899 return -1;
900}
901
902/*
903 * For log usage, where we add/open/close files automatically
904 */
905void free_release_files(struct thread_data *td)
906{
907 close_files(td);
908 td->files_index = 0;
909 td->nr_normal_files = 0;
910}