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