Save a division, if delta is non-zero.
[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"
11#include "os.h"
12
7bb48f84 13static int extend_file(struct thread_data *td, struct fio_file *f)
25205e97 14{
ea443657 15 int r, new_layout = 0, unlink_file = 0, flags;
25205e97
JA
16 unsigned long long left;
17 unsigned int bs;
18 char *b;
b2a15192 19
507a702f
JA
20 /*
21 * check if we need to lay the file out complete again. fio
22 * does that for operations involving reads, or for writes
23 * where overwrite is set
24 */
25 if (td_read(td) || (td_write(td) && td->o.overwrite))
26 new_layout = 1;
ea443657
JA
27 if (td_write(td) && !td->o.overwrite)
28 unlink_file = 1;
507a702f 29
ea443657 30 if ((unlink_file || new_layout) && (f->flags & FIO_FILE_EXISTS)) {
7bb48f84
JA
31 if (unlink(f->file_name) < 0) {
32 td_verror(td, errno, "unlink");
33 return 1;
34 }
35 }
36
507a702f
JA
37 flags = O_WRONLY | O_CREAT;
38 if (new_layout)
39 flags |= O_TRUNC;
40
41 f->fd = open(f->file_name, flags, 0644);
53cdc686 42 if (f->fd < 0) {
e1161c32 43 td_verror(td, errno, "open");
53cdc686
JA
44 return 1;
45 }
46
7bb48f84 47 if (ftruncate(f->fd, f->real_file_size) == -1) {
e1161c32 48 td_verror(td, errno, "ftruncate");
53cdc686
JA
49 goto err;
50 }
51
e8be2ad7
JA
52 if (!new_layout)
53 goto done;
54
7bb48f84 55 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
e1161c32 56 td_verror(td, errno, "posix_fallocate");
40f8298c
JA
57 goto err;
58 }
59
2dc1bbeb
JA
60 b = malloc(td->o.max_bs[DDIR_WRITE]);
61 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
53cdc686 62
7bb48f84 63 left = f->real_file_size;
53cdc686 64 while (left && !td->terminate) {
2dc1bbeb 65 bs = td->o.max_bs[DDIR_WRITE];
53cdc686
JA
66 if (bs > left)
67 bs = left;
68
69 r = write(f->fd, b, bs);
70
71 if (r == (int) bs) {
72 left -= bs;
73 continue;
74 } else {
75 if (r < 0)
e1161c32 76 td_verror(td, errno, "write");
53cdc686 77 else
e1161c32 78 td_verror(td, EIO, "write");
53cdc686
JA
79
80 break;
81 }
82 }
83
84 if (td->terminate)
85 unlink(f->file_name);
2dc1bbeb 86 else if (td->o.create_fsync)
53cdc686
JA
87 fsync(f->fd);
88
89 free(b);
507a702f 90done:
53cdc686
JA
91 close(f->fd);
92 f->fd = -1;
93 return 0;
94err:
95 close(f->fd);
96 f->fd = -1;
97 return 1;
98}
99
7bb48f84 100static unsigned long long get_rand_file_size(struct thread_data *td)
9c60ce64 101{
9c60ce64
JA
102 unsigned long long ret;
103 long r;
104
9c60ce64 105 r = os_random_long(&td->file_size_state);
7bb48f84 106 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
2dc1bbeb 107 ret -= (ret % td->o.rw_min_bs);
9c60ce64
JA
108 return ret;
109}
110
53cdc686
JA
111static int file_size(struct thread_data *td, struct fio_file *f)
112{
113 struct stat st;
114
7bb48f84
JA
115 if (fstat(f->fd, &st) == -1) {
116 td_verror(td, errno, "fstat");
117 return 1;
118 }
53cdc686 119
7bb48f84 120 f->real_file_size = st.st_size;
53cdc686
JA
121 return 0;
122}
123
124static int bdev_size(struct thread_data *td, struct fio_file *f)
125{
126 unsigned long long bytes;
127 int r;
128
129 r = blockdev_size(f->fd, &bytes);
130 if (r) {
e1161c32 131 td_verror(td, r, "blockdev_size");
53cdc686
JA
132 return 1;
133 }
134
135 f->real_file_size = bytes;
53cdc686
JA
136 return 0;
137}
138
139static int get_file_size(struct thread_data *td, struct fio_file *f)
140{
141 int ret = 0;
142
409b3417
JA
143 if (f->flags & FIO_SIZE_KNOWN)
144 return 0;
145
7bb48f84
JA
146 if (f->filetype == FIO_TYPE_FILE)
147 ret = file_size(td, f);
148 else if (f->filetype == FIO_TYPE_BD)
53cdc686
JA
149 ret = bdev_size(td, f);
150 else
151 f->real_file_size = -1;
152
153 if (ret)
154 return ret;
155
156 if (f->file_offset > f->real_file_size) {
2dc1bbeb 157 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
53cdc686
JA
158 return 1;
159 }
160
409b3417 161 f->flags |= FIO_SIZE_KNOWN;
53cdc686
JA
162 return 0;
163}
164
e5b401d4
JA
165int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
166{
167 int ret = 0;
168
2dc1bbeb 169 if (td->o.odirect)
b5af8293
JA
170 return 0;
171
e5b401d4
JA
172 /*
173 * FIXME: add blockdev flushing too
174 */
b5af8293 175 if (f->mmap)
7bb48f84 176 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
467d1b6b 177 else if (f->filetype == FIO_TYPE_FILE)
7bb48f84 178 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
7e0e25c9 179 else if (f->filetype == FIO_TYPE_BD) {
b5af8293 180 ret = blockdev_invalidate_cache(f->fd);
7e0e25c9
JA
181 if (ret < 0 && errno == EACCES && geteuid()) {
182 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
183 ret = 0;
184 }
185 } else if (f->filetype == FIO_TYPE_CHAR)
e5b401d4
JA
186 ret = 0;
187
188 if (ret < 0) {
e1161c32 189 td_verror(td, errno, "invalidate_cache");
e5b401d4
JA
190 return 1;
191 }
192
ad2da605 193 return ret;
e5b401d4
JA
194}
195
b5af8293 196void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 197{
b5af8293
JA
198 close(f->fd);
199 f->fd = -1;
53cdc686
JA
200}
201
b5af8293 202int generic_open_file(struct thread_data *td, struct fio_file *f)
53cdc686 203{
53cdc686
JA
204 int flags = 0;
205
2dc1bbeb 206 if (td->o.odirect)
2fd233b7 207 flags |= OS_O_DIRECT;
2dc1bbeb 208 if (td->o.sync_io)
2fd233b7 209 flags |= O_SYNC;
53cdc686 210
2fd233b7
JA
211 if (td_write(td) || td_rw(td)) {
212 flags |= O_RDWR;
53cdc686 213
af52b345 214 if (f->filetype == FIO_TYPE_FILE)
2fd233b7 215 flags |= O_CREAT;
2fd233b7 216
b5af8293 217 f->fd = open(f->file_name, flags, 0600);
2fd233b7 218 } else {
af52b345 219 if (f->filetype == FIO_TYPE_CHAR)
2fd233b7
JA
220 flags |= O_RDWR;
221 else
222 flags |= O_RDONLY;
223
b5af8293 224 f->fd = open(f->file_name, flags);
53cdc686
JA
225 }
226
227 if (f->fd == -1) {
e4e33258 228 char buf[FIO_VERROR_SIZE];
e1161c32
JA
229 int __e = errno;
230
e4e33258
JA
231 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
232
233 td_verror(td, __e, buf);
53cdc686
JA
234 }
235
b5af8293
JA
236 if (get_file_size(td, f))
237 goto err;
238
53cdc686 239 return 0;
b5af8293
JA
240err:
241 close(f->fd);
242 return 1;
243}
244
21972cde
JA
245int open_files(struct thread_data *td)
246{
247 struct fio_file *f;
af52b345
JA
248 unsigned int i;
249 int err = 0;
21972cde
JA
250
251 for_each_file(td, f, i) {
b5af8293 252 err = td_io_open_file(td, f);
9bf27b45
JA
253 if (err) {
254 if (td->error == EMFILE) {
255 log_err("fio: limited open files to: %d\n", td->nr_open_files);
256 td->o.open_files = td->nr_open_files;
257 err = 0;
258 clear_error(td);
259 }
21972cde 260 break;
9bf27b45 261 }
b5af8293 262
2dc1bbeb 263 if (td->o.open_files == td->nr_open_files)
b5af8293 264 break;
21972cde
JA
265 }
266
7abf833d
JA
267 if (!err)
268 return 0;
269
bdb4e2e9 270 for_each_file(td, f, i)
b5af8293 271 td_io_close_file(td, f);
7abf833d 272
21972cde
JA
273 return err;
274}
275
7bb48f84
JA
276/*
277 * open/close all files, so that ->real_file_size gets set
278 */
000b0803 279static void get_file_sizes(struct thread_data *td)
7bb48f84
JA
280{
281 struct fio_file *f;
282 unsigned int i;
7bb48f84
JA
283
284 for_each_file(td, f, i) {
541d66d7
JA
285 if (td->io_ops->open_file(td, f))
286 clear_error(td);
287 else
000b0803 288 td->io_ops->close_file(td, f);
409b3417
JA
289
290 if (f->real_file_size == -1ULL && td->o.size)
291 f->real_file_size = td->o.size / td->o.nr_files;
7bb48f84 292 }
7bb48f84
JA
293}
294
295/*
296 * Open the files and setup files sizes, creating files if necessary.
297 */
53cdc686
JA
298int setup_files(struct thread_data *td)
299{
7bb48f84 300 unsigned long long total_size, extend_size;
53cdc686 301 struct fio_file *f;
af52b345 302 unsigned int i;
000b0803 303 int err = 0, need_extend;
53cdc686
JA
304
305 /*
306 * if ioengine defines a setup() method, it's responsible for
7bb48f84
JA
307 * opening the files and setting f->real_file_size to indicate
308 * the valid range for that file.
53cdc686
JA
309 */
310 if (td->io_ops->setup)
7bb48f84
JA
311 err = td->io_ops->setup(td);
312 else
000b0803 313 get_file_sizes(td);
53cdc686 314
f1027063
JA
315 if (err)
316 return err;
317
0a7eb121 318 /*
7bb48f84
JA
319 * check sizes. if the files/devices do not exist and the size
320 * isn't passed to fio, abort.
0a7eb121 321 */
7bb48f84
JA
322 total_size = 0;
323 for_each_file(td, f, i) {
324 if (f->real_file_size == -1ULL)
325 total_size = -1ULL;
326 else
327 total_size += f->real_file_size;
328 }
0a7eb121 329
7bb48f84
JA
330 /*
331 * device/file sizes are zero and no size given, punt
332 */
409b3417 333 if ((!total_size || total_size == -1ULL) && !td->o.size) {
7bb48f84 334 log_err("%s: you need to specify size=\n", td->o.name);
e1161c32 335 td_verror(td, EINVAL, "total_file_size");
53cdc686
JA
336 return 1;
337 }
338
7bb48f84
JA
339 /*
340 * now file sizes are known, so we can set ->io_size. if size= is
341 * not given, ->io_size is just equal to ->real_file_size. if size
342 * is given, ->io_size is size / nr_files.
343 */
344 extend_size = total_size = 0;
345 need_extend = 0;
346 for_each_file(td, f, i) {
347 if (!td->o.file_size_low) {
348 /*
349 * no file size range given, file size is equal to
350 * total size divided by number of files. if that is
351 * zero, set it to the real file size.
352 */
353 f->io_size = td->o.size / td->o.nr_files;
354 if (!f->io_size)
355 f->io_size = f->real_file_size;
356 } else if (f->real_file_size < td->o.file_size_low ||
357 f->real_file_size > td->o.file_size_high) {
358 /*
359 * file size given. if it's fixed, use that. if it's a
360 * range, generate a random size in-between.
361 */
362 if (td->o.file_size_low == td->o.file_size_high)
363 f->io_size = td->o.file_size_low;
364 else
365 f->io_size = get_rand_file_size(td);
366 } else
367 f->io_size = f->real_file_size;
53cdc686 368
7bb48f84
JA
369 if (f->io_size == -1ULL)
370 total_size = -1ULL;
371 else
372 total_size += f->io_size;
373
374 if (f->filetype == FIO_TYPE_FILE &&
375 f->io_size > f->real_file_size &&
376 !(td->io_ops->flags & FIO_DISKLESSIO)) {
377 need_extend++;
378 extend_size += f->io_size;
379 f->flags |= FIO_FILE_EXTEND;
380 }
381 }
53cdc686 382
7bb48f84
JA
383 if (!td->o.size)
384 td->o.size = total_size;
21972cde 385
7bb48f84
JA
386 /*
387 * See if we need to extend some files
388 */
389 if (need_extend) {
390 temp_stall_ts = 1;
391 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
392 td->o.name, need_extend, extend_size >> 20);
393
394 for_each_file(td, f, i) {
395 if (!(f->flags & FIO_FILE_EXTEND))
396 continue;
397
409b3417 398 assert(f->filetype == FIO_TYPE_FILE);
7bb48f84
JA
399 f->flags &= ~FIO_FILE_EXTEND;
400 f->real_file_size = f->io_size;
401 err = extend_file(td, f);
402 if (err)
403 break;
404 }
405 temp_stall_ts = 0;
406 }
407
408 if (err)
409 return err;
410
411 if (!td->o.zone_size)
412 td->o.zone_size = td->o.size;
413
414 td->total_io_size = td->o.size * td->o.loops;
415 return 0;
53cdc686
JA
416}
417
68727076
JA
418int init_random_map(struct thread_data *td)
419{
420 int num_maps, blocks;
421 struct fio_file *f;
422 unsigned int i;
423
424 if (td->o.norandommap)
425 return 0;
426
427 for_each_file(td, f, i) {
428 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
429 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
430 f->file_map = malloc(num_maps * sizeof(long));
431 if (!f->file_map) {
432 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
433 return 1;
434 }
435 f->num_maps = num_maps;
436 memset(f->file_map, 0, num_maps * sizeof(long));
437 }
438
439 return 0;
440}
441
53cdc686
JA
442void close_files(struct thread_data *td)
443{
0ab8db89 444 struct fio_file *f;
af52b345 445 unsigned int i;
53cdc686 446
0ab8db89 447 for_each_file(td, f, i) {
fa1da865
JA
448 if ((f->flags & FIO_FILE_UNLINK) &&
449 f->filetype == FIO_TYPE_FILE)
132ad46d 450 unlink(f->file_name);
bdb4e2e9 451
b5af8293 452 td_io_close_file(td, f);
b3dc7f07 453
fa1da865
JA
454 free(f->file_name);
455 f->file_name = NULL;
456
c343981b 457 if (f->file_map) {
b3dc7f07 458 free(f->file_map);
c343981b
JA
459 f->file_map = NULL;
460 }
53cdc686 461 }
b4a6a59a 462
2dc1bbeb 463 td->o.filename = NULL;
cade3ef4 464 free(td->files);
b4a6a59a 465 td->files = NULL;
2dc1bbeb 466 td->o.nr_files = 0;
53cdc686 467}
af52b345 468
e3bab463 469static void get_file_type(struct fio_file *f)
af52b345
JA
470{
471 struct stat sb;
472
473 f->filetype = FIO_TYPE_FILE;
474
e3bab463 475 if (!lstat(f->file_name, &sb)) {
af52b345
JA
476 if (S_ISBLK(sb.st_mode))
477 f->filetype = FIO_TYPE_BD;
478 else if (S_ISCHR(sb.st_mode))
479 f->filetype = FIO_TYPE_CHAR;
480 }
481}
482
483void add_file(struct thread_data *td, const char *fname)
484{
7b4e4fe5 485 int cur_files = td->files_index;
bd0ee748 486 char file_name[PATH_MAX];
af52b345 487 struct fio_file *f;
bd0ee748 488 int len = 0;
af52b345
JA
489
490 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
491
492 f = &td->files[cur_files];
493 memset(f, 0, sizeof(*f));
494 f->fd = -1;
bd0ee748
JA
495
496 if (td->o.directory)
497 len = sprintf(file_name, "%s/", td->o.directory);
498
499 sprintf(file_name + len, "%s", fname);
500 f->file_name = strdup(file_name);
af52b345 501
e3bab463 502 get_file_type(f);
af52b345 503
7b4e4fe5 504 td->files_index++;
1549441c
JA
505 if (f->filetype == FIO_TYPE_FILE)
506 td->nr_normal_files++;
af52b345 507}
0ad920e7
JA
508
509void get_file(struct fio_file *f)
510{
511 f->references++;
512}
513
514void put_file(struct thread_data *td, struct fio_file *f)
515{
516 if (!(f->flags & FIO_FILE_OPEN))
517 return;
518
519 assert(f->references);
520 if (--f->references)
521 return;
522
2dc1bbeb 523 if (should_fsync(td) && td->o.fsync_on_close)
ebb1415f
JA
524 fsync(f->fd);
525
0ad920e7
JA
526 if (td->io_ops->close_file)
527 td->io_ops->close_file(td, f);
1020a139 528
0ad920e7
JA
529 td->nr_open_files--;
530 f->flags &= ~FIO_FILE_OPEN;
531}
bbf6b540
JA
532
533static int recurse_dir(struct thread_data *td, const char *dirname)
534{
535 struct dirent *dir;
536 int ret = 0;
537 DIR *D;
538
539 D = opendir(dirname);
540 if (!D) {
0ddb270c
JA
541 char buf[FIO_VERROR_SIZE];
542
543 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
544 td_verror(td, errno, buf);
bbf6b540
JA
545 return 1;
546 }
547
548 while ((dir = readdir(D)) != NULL) {
549 char full_path[PATH_MAX];
550 struct stat sb;
551
e85b2b83
JA
552 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
553 continue;
96d32d51 554
bbf6b540
JA
555 sprintf(full_path, "%s/%s", dirname, dir->d_name);
556
557 if (lstat(full_path, &sb) == -1) {
558 if (errno != ENOENT) {
559 td_verror(td, errno, "stat");
560 return 1;
561 }
562 }
563
564 if (S_ISREG(sb.st_mode)) {
565 add_file(td, full_path);
2dc1bbeb 566 td->o.nr_files++;
bbf6b540
JA
567 continue;
568 }
0ddb270c
JA
569 if (!S_ISDIR(sb.st_mode))
570 continue;
bbf6b540 571
bbf6b540
JA
572 if ((ret = recurse_dir(td, full_path)) != 0)
573 break;
574 }
575
576 closedir(D);
577 return ret;
578}
579
580int add_dir_files(struct thread_data *td, const char *path)
581{
0ddb270c
JA
582 int ret = recurse_dir(td, path);
583
584 if (!ret)
585 log_info("fio: opendir added %d files\n", td->o.nr_files);
586
587 return ret;
bbf6b540 588}
cade3ef4
JA
589
590void dup_files(struct thread_data *td, struct thread_data *org)
591{
592 struct fio_file *f;
593 unsigned int i;
594 size_t bytes;
595
596 if (!org->files)
597 return;
598
599 bytes = org->files_index * sizeof(*f);
600 td->files = malloc(bytes);
601 memcpy(td->files, org->files, bytes);
602
603 for_each_file(td, f, i) {
604 if (f->file_name)
605 f->file_name = strdup(f->file_name);
606 }
607}