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