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