Make sure to set io size on non-files
[fio.git] / filesetup.c
1 #include <unistd.h>
2 #include <fcntl.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <dirent.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <sys/types.h>
9
10 #include "fio.h"
11 #include "os.h"
12
13 /*
14  * Check if the file exists and it's large enough.
15  */
16 static int file_ok(struct thread_data *td, struct fio_file *f)
17 {
18         struct stat st;
19
20         if (f->filetype != FIO_TYPE_FILE ||
21             (td->io_ops->flags & FIO_DISKLESSIO))
22                 return 0;
23
24         if (lstat(f->file_name, &st) == -1)
25                 return 1;
26
27         /*
28          * if it's a special file, size is always ok for now
29          */
30         if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
31                 return 0;
32         if (st.st_size < (off_t) f->file_size)
33                 return 1;
34
35         return 0;
36 }
37
38 static int create_file(struct thread_data *td, struct fio_file *f)
39 {
40         unsigned long long left;
41         unsigned int bs;
42         char *b;
43         int r;
44
45         f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
46         if (f->fd < 0) {
47                 td_verror(td, errno, "open");
48                 return 1;
49         }
50
51         if (ftruncate(f->fd, f->file_size) == -1) {
52                 td_verror(td, errno, "ftruncate");
53                 goto err;
54         }
55
56         if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
57                 td_verror(td, errno, "posix_fallocate");
58                 goto err;
59         }
60
61         b = malloc(td->o.max_bs[DDIR_WRITE]);
62         memset(b, 0, td->o.max_bs[DDIR_WRITE]);
63
64         left = f->file_size;
65         while (left && !td->terminate) {
66                 bs = td->o.max_bs[DDIR_WRITE];
67                 if (bs > left)
68                         bs = left;
69
70                 r = write(f->fd, b, bs);
71
72                 if (r == (int) bs) {
73                         left -= bs;
74                         continue;
75                 } else {
76                         if (r < 0)
77                                 td_verror(td, errno, "write");
78                         else
79                                 td_verror(td, EIO, "write");
80
81                         break;
82                 }
83         }
84
85         if (td->terminate)
86                 unlink(f->file_name);
87         else if (td->o.create_fsync)
88                 fsync(f->fd);
89
90         free(b);
91         close(f->fd);
92         f->fd = -1;
93         return 0;
94 err:
95         close(f->fd);
96         f->fd = -1;
97         return 1;
98 }
99
100 static unsigned long long set_rand_file_size(struct thread_data *td,
101                                              unsigned long long total_size)
102 {
103         unsigned long long upper = total_size;
104         unsigned long long ret;
105         long r;
106
107         if (upper > td->o.file_size_high)
108                 upper = td->o.file_size_high;
109         else if (upper < td->o.file_size_low)
110                 return 0;
111         else if (!upper)
112                 return 0;
113
114         r = os_random_long(&td->file_size_state);
115         ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116         ret -= (ret % td->o.rw_min_bs);
117         if (ret > upper)
118                 ret = upper;
119         return ret;
120 }
121
122 static int fill_file_size(struct thread_data *td, struct fio_file *f,
123                           unsigned long long *file_size, int new_files)
124 {
125         if (!td->o.file_size_low) {
126                 f->file_size = *file_size / new_files;
127                 f->real_file_size = f->file_size;
128         } else {
129                 /*
130                  * If we don't have enough space left for a file
131                  * of the minimum size, bail.
132                  */
133                 if (*file_size < td->o.file_size_low)
134                         return 1;
135
136                 f->file_size = set_rand_file_size(td, *file_size);
137                 f->real_file_size = f->file_size;
138                 *file_size -= f->file_size;
139         }
140
141         return 0;
142 }
143
144 static int create_files(struct thread_data *td)
145 {
146         struct fio_file *f;
147         int err, need_create, can_extend;
148         unsigned long long total_file_size, local_file_size, create_size;
149         unsigned int i, new_files;
150
151         new_files = 0;
152         total_file_size = td->o.size;
153         for_each_file(td, f, i) {
154                 unsigned long long s;
155
156                 f->file_offset = td->o.start_offset;
157
158                 if (!total_file_size)
159                         continue;
160
161                 if (f->filetype != FIO_TYPE_FILE) {
162                         if (!f->file_size)
163                                 f->file_size = total_file_size / td->o.nr_files;
164                         continue;
165                 }
166
167                 if (f->flags & FIO_FILE_EXISTS) {
168                         if ((f->file_size > td->o.size / td->o.nr_files) ||
169                             !f->file_size)
170                                 f->file_size = td->o.size / td->o.nr_files;
171
172                         s = f->file_size;
173                         if (s > total_file_size)
174                                 s = total_file_size;
175
176                         total_file_size -= s;
177                 } else
178                         new_files++;
179         }
180
181         /*
182          * unless specifically asked for overwrite, let normal io extend it
183          */
184         can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
185         if (can_extend && new_files) {
186                 for_each_file(td, f, i) {
187                         if (fill_file_size(td, f, &total_file_size, new_files)) {
188                                 log_info("fio: limited to %d files\n", i);
189                                 td->o.nr_files = i;
190                                 break;
191                         }
192                 }
193
194                 return 0;
195         }
196
197         local_file_size = total_file_size;
198         if (!local_file_size)
199                 local_file_size = -1;
200
201         total_file_size = 0;
202         need_create = 0;
203         create_size = 0;
204         for_each_file(td, f, i) {
205                 int file_there;
206
207                 if (f->filetype != FIO_TYPE_FILE)
208                         continue;
209                 if (f->flags & FIO_FILE_EXISTS) {
210                         total_file_size += f->file_size;
211                         continue;
212                 }
213
214                 if (fill_file_size(td, f, &local_file_size, new_files)) {
215                         log_info("fio: limited to %d files\n", i);
216                         new_files -= (td->o.nr_files - i);
217                         td->o.nr_files = i;
218                         break;
219                 }
220
221                 total_file_size += f->file_size;
222                 create_size += f->file_size;
223                 file_there = !file_ok(td, f);
224
225                 if (file_there && td_write(td) && !td->o.overwrite) {
226                         unlink(f->file_name);
227                         file_there = 0;
228                 }
229
230                 need_create += !file_there;
231         }
232
233         if (!need_create)
234                 return 0;
235
236         if (!td->o.size && !total_file_size) {
237                 log_err("Need size for create\n");
238                 td_verror(td, EINVAL, "file_size");
239                 return 1;
240         }
241
242         temp_stall_ts = 1;
243         log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
244                                 td->o.name, new_files, create_size >> 20);
245
246         err = 0;
247         for_each_file(td, f, i) {
248                 /*
249                  * Only unlink files that we created.
250                  */
251                 f->flags &= ~FIO_FILE_UNLINK;
252                 if (file_ok(td, f)) {
253                         if (td->o.unlink)
254                                 f->flags |= FIO_FILE_UNLINK;
255
256                         f->flags |= FIO_FILE_NOSORT;
257                         err = create_file(td, f);
258                         if (err)
259                                 break;
260                 }
261         }
262
263         temp_stall_ts = 0;
264         return err;
265 }
266
267 static int file_size(struct thread_data *td, struct fio_file *f)
268 {
269         struct stat st;
270
271         if (td->o.overwrite) {
272                 if (fstat(f->fd, &st) == -1) {
273                         td_verror(td, errno, "fstat");
274                         return 1;
275                 }
276
277                 f->real_file_size = st.st_size;
278
279                 if (!f->file_size || f->file_size > f->real_file_size)
280                         f->file_size = f->real_file_size;
281         } else
282                 f->real_file_size = f->file_size;
283
284         return 0;
285 }
286
287 static int bdev_size(struct thread_data *td, struct fio_file *f)
288 {
289         unsigned long long bytes;
290         int r;
291
292         r = blockdev_size(f->fd, &bytes);
293         if (r) {
294                 td_verror(td, r, "blockdev_size");
295                 return 1;
296         }
297
298         f->real_file_size = bytes;
299
300         /*
301          * no extend possibilities, so limit size to device size if too large
302          */
303         if (!f->file_size || f->file_size > f->real_file_size)
304                 f->file_size = f->real_file_size;
305
306         f->file_size -= f->file_offset;
307         return 0;
308 }
309
310 static int get_file_size(struct thread_data *td, struct fio_file *f)
311 {
312         int ret = 0;
313
314         if (f->filetype == FIO_TYPE_FILE) {
315                 if (!(f->flags & FIO_FILE_EXISTS))
316                         ret = file_size(td, f);
317         } else if (f->filetype == FIO_TYPE_BD)
318                 ret = bdev_size(td, f);
319         else
320                 f->real_file_size = -1;
321
322         if (ret)
323                 return ret;
324
325         if (f->file_offset > f->real_file_size) {
326                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
327                 return 1;
328         }
329
330         return 0;
331 }
332
333 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
334 {
335         int ret = 0;
336
337         if (td->o.odirect)
338                 return 0;
339
340         /*
341          * FIXME: add blockdev flushing too
342          */
343         if (f->mmap)
344                 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
345         else if (f->filetype == FIO_TYPE_FILE)
346                 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
347         else if (f->filetype == FIO_TYPE_BD)
348                 ret = blockdev_invalidate_cache(f->fd);
349         else if (f->filetype == FIO_TYPE_CHAR)
350                 ret = 0;
351
352         if (ret < 0) {
353                 td_verror(td, errno, "invalidate_cache");
354                 return 1;
355         }
356
357         return ret;
358 }
359
360 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
361 {
362         close(f->fd);
363         f->fd = -1;
364 }
365
366 int generic_open_file(struct thread_data *td, struct fio_file *f)
367 {
368         int flags = 0;
369
370         if (td->o.odirect)
371                 flags |= OS_O_DIRECT;
372         if (td->o.sync_io)
373                 flags |= O_SYNC;
374
375         if (td_write(td) || td_rw(td)) {
376                 flags |= O_RDWR;
377
378                 if (f->filetype == FIO_TYPE_FILE)
379                         flags |= O_CREAT;
380
381                 f->fd = open(f->file_name, flags, 0600);
382         } else {
383                 if (f->filetype == FIO_TYPE_CHAR)
384                         flags |= O_RDWR;
385                 else
386                         flags |= O_RDONLY;
387
388                 f->fd = open(f->file_name, flags);
389         }
390
391         if (f->fd == -1) {
392                 char buf[FIO_VERROR_SIZE];
393                 int __e = errno;
394
395                 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
396
397                 td_verror(td, __e, buf);
398                 if (__e == EINVAL && td->o.odirect)
399                         log_err("fio: destination does not support O_DIRECT\n");
400                 if (__e == EMFILE)
401                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
402                 return 1;
403         }
404
405         if (get_file_size(td, f))
406                 goto err;
407
408         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
409                 goto err;
410
411         if (!td->o.fadvise_hint)
412                 return 0;
413
414         if (!td_random(td)) {
415                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
416                         td_verror(td, errno, "fadvise");
417                         goto err;
418                 }
419         } else {
420                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
421                         td_verror(td, errno, "fadvise");
422                         goto err;
423                 }
424         }
425
426         return 0;
427 err:
428         close(f->fd);
429         return 1;
430 }
431
432 int open_files(struct thread_data *td)
433 {
434         struct fio_file *f;
435         unsigned int i;
436         int err = 0;
437
438         for_each_file(td, f, i) {
439                 err = td_io_open_file(td, f);
440                 if (err)
441                         break;
442
443                 if (td->o.open_files == td->nr_open_files)
444                         break;
445         }
446
447         if (!err)
448                 return 0;
449
450         for_each_file(td, f, i)
451                 td_io_close_file(td, f);
452
453         return err;
454 }
455
456 int setup_files(struct thread_data *td)
457 {
458         struct fio_file *f;
459         unsigned int i;
460         int err;
461
462         /*
463          * if ioengine defines a setup() method, it's responsible for
464          * setting up everything in the td->files[] area.
465          */
466         if (td->io_ops->setup)
467                 return td->io_ops->setup(td);
468
469         if (create_files(td))
470                 return 1;
471
472         err = open_files(td);
473         if (err)
474                 return err;
475
476         /*
477          * Recalculate the total file size now that files are set up.
478          */
479         td->o.size = 0;
480         for_each_file(td, f, i)
481                 td->o.size += f->file_size;
482
483         td->io_size = td->o.size;
484         if (td->io_size == 0) {
485                 log_err("%s: no io blocks\n", td->o.name);
486                 td_verror(td, EINVAL, "total_file_size");
487                 return 1;
488         }
489
490         if (!td->o.zone_size)
491                 td->o.zone_size = td->io_size;
492
493         td->total_io_size = td->io_size * td->o.loops;
494
495         for_each_file(td, f, i)
496                 td_io_close_file(td, f);
497
498         return err;
499 }
500
501 int init_random_map(struct thread_data *td)
502 {
503         int num_maps, blocks;
504         struct fio_file *f;
505         unsigned int i;
506
507         if (td->o.norandommap)
508                 return 0;
509
510         for_each_file(td, f, i) {
511                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
512                 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
513                 f->file_map = malloc(num_maps * sizeof(long));
514                 if (!f->file_map) {
515                         log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
516                         return 1;
517                 }
518                 f->num_maps = num_maps;
519                 memset(f->file_map, 0, num_maps * sizeof(long));
520         }
521
522         return 0;
523 }
524
525 void close_files(struct thread_data *td)
526 {
527         struct fio_file *f;
528         unsigned int i;
529
530         for_each_file(td, f, i) {
531                 if ((f->flags & FIO_FILE_UNLINK) &&
532                     f->filetype == FIO_TYPE_FILE)
533                         unlink(f->file_name);
534
535                 td_io_close_file(td, f);
536
537                 free(f->file_name);
538                 f->file_name = NULL;
539
540                 if (f->file_map) {
541                         free(f->file_map);
542                         f->file_map = NULL;
543                 }
544         }
545
546         td->o.filename = NULL;
547         free(td->files);
548         td->files = NULL;
549         td->o.nr_files = 0;
550 }
551
552 static void get_file_type(struct fio_file *f)
553 {
554         struct stat sb;
555
556         f->filetype = FIO_TYPE_FILE;
557
558         if (!lstat(f->file_name, &sb)) {
559                 f->flags |= FIO_FILE_EXISTS;
560
561                 if (S_ISBLK(sb.st_mode))
562                         f->filetype = FIO_TYPE_BD;
563                 else if (S_ISCHR(sb.st_mode))
564                         f->filetype = FIO_TYPE_CHAR;
565                 else {
566                         /*
567                          * might as well do this here, and save a stat later on
568                          */
569                         f->real_file_size = sb.st_size;
570                         f->file_size = f->real_file_size;
571                 }
572         }
573 }
574
575 void add_file(struct thread_data *td, const char *fname)
576 {
577         int cur_files = td->files_index;
578         char file_name[PATH_MAX];
579         struct fio_file *f;
580         int len = 0;
581
582         td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
583
584         f = &td->files[cur_files];
585         memset(f, 0, sizeof(*f));
586         f->fd = -1;
587
588         if (td->o.directory)
589                 len = sprintf(file_name, "%s/", td->o.directory);
590
591         sprintf(file_name + len, "%s", fname);
592         f->file_name = strdup(file_name);
593
594         get_file_type(f);
595
596         td->files_index++;
597         if (f->filetype == FIO_TYPE_FILE)
598                 td->nr_normal_files++;
599 }
600
601 void get_file(struct fio_file *f)
602 {
603         f->references++;
604 }
605
606 void put_file(struct thread_data *td, struct fio_file *f)
607 {
608         if (!(f->flags & FIO_FILE_OPEN))
609                 return;
610
611         assert(f->references);
612         if (--f->references)
613                 return;
614
615         if (should_fsync(td) && td->o.fsync_on_close)
616                 fsync(f->fd);
617
618         if (td->io_ops->close_file)
619                 td->io_ops->close_file(td, f);
620         td->nr_open_files--;
621         f->flags &= ~FIO_FILE_OPEN;
622 }
623
624 static int recurse_dir(struct thread_data *td, const char *dirname)
625 {
626         struct dirent *dir;
627         int ret = 0;
628         DIR *D;
629
630         D = opendir(dirname);
631         if (!D) {
632                 td_verror(td, errno, "opendir");
633                 return 1;
634         }
635
636         while ((dir = readdir(D)) != NULL) {
637                 char full_path[PATH_MAX];
638                 struct stat sb;
639
640                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
641                         continue;
642
643                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
644
645                 if (lstat(full_path, &sb) == -1) {
646                         if (errno != ENOENT) {
647                                 td_verror(td, errno, "stat");
648                                 return 1;
649                         }
650                 }
651
652                 if (S_ISREG(sb.st_mode)) {
653                         add_file(td, full_path);
654                         td->o.nr_files++;
655                         continue;
656                 }
657
658                 if ((ret = recurse_dir(td, full_path)) != 0)
659                         break;
660         }
661
662         closedir(D);
663         return ret;
664 }
665
666 int add_dir_files(struct thread_data *td, const char *path)
667 {
668         return recurse_dir(td, path);
669 }
670
671 void dup_files(struct thread_data *td, struct thread_data *org)
672 {
673         struct fio_file *f;
674         unsigned int i;
675         size_t bytes;
676
677         if (!org->files)
678                 return;
679
680         bytes = org->files_index * sizeof(*f);
681         td->files = malloc(bytes);
682         memcpy(td->files, org->files, bytes);
683
684         for_each_file(td, f, i) {
685                 if (f->file_name)
686                         f->file_name = strdup(f->file_name);
687         }
688 }