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