Untangle the file creation mess
[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 (f->filetype != FIO_TYPE_FILE)
159                         continue;
160
161                 if (f->flags & FIO_FILE_EXISTS) {
162                         s = f->file_size;
163                         if (s > total_file_size)
164                                 s = total_file_size;
165
166                         total_file_size -= s;
167                 } else
168                         new_files++;
169         }
170
171         /*
172          * unless specifically asked for overwrite, let normal io extend it
173          */
174         can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
175         if (can_extend) {
176                 for_each_file(td, f, i) {
177                         if (fill_file_size(td, f, &total_file_size, new_files)) {
178                                 log_info("fio: limited to %d files\n", i);
179                                 td->o.nr_files = i;
180                                 break;
181                         }
182                 }
183
184                 return 0;
185         }
186
187         local_file_size = total_file_size;
188         if (!local_file_size)
189                 local_file_size = -1;
190
191         total_file_size = 0;
192         need_create = 0;
193         create_size = 0;
194         for_each_file(td, f, i) {
195                 int file_there;
196
197                 if (f->filetype != FIO_TYPE_FILE)
198                         continue;
199                 if (f->flags & FIO_FILE_EXISTS) {
200                         total_file_size += f->file_size;
201                         continue;
202                 }
203
204                 if (fill_file_size(td, f, &local_file_size, new_files)) {
205                         log_info("fio: limited to %d files\n", i);
206                         new_files -= (td->o.nr_files - i);
207                         td->o.nr_files = i;
208                         break;
209                 }
210
211                 total_file_size += f->file_size;
212                 create_size += f->file_size;
213                 file_there = !file_ok(td, f);
214
215                 if (file_there && td_write(td) && !td->o.overwrite) {
216                         unlink(f->file_name);
217                         file_there = 0;
218                 }
219
220                 need_create += !file_there;
221         }
222
223         if (!need_create)
224                 return 0;
225
226         if (!td->o.size && !total_file_size) {
227                 log_err("Need size for create\n");
228                 td_verror(td, EINVAL, "file_size");
229                 return 1;
230         }
231
232         temp_stall_ts = 1;
233         log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
234                                 td->o.name, new_files, create_size >> 20);
235
236         err = 0;
237         for_each_file(td, f, i) {
238                 /*
239                  * Only unlink files that we created.
240                  */
241                 f->flags &= ~FIO_FILE_UNLINK;
242                 if (file_ok(td, f)) {
243                         if (td->o.unlink)
244                                 f->flags |= FIO_FILE_UNLINK;
245
246                         err = create_file(td, f);
247                         if (err)
248                                 break;
249                 }
250         }
251
252         temp_stall_ts = 0;
253         return err;
254 }
255
256 static int file_size(struct thread_data *td, struct fio_file *f)
257 {
258         struct stat st;
259
260         if (td->o.overwrite) {
261                 if (fstat(f->fd, &st) == -1) {
262                         td_verror(td, errno, "fstat");
263                         return 1;
264                 }
265
266                 f->real_file_size = st.st_size;
267
268                 if (!f->file_size || f->file_size > f->real_file_size)
269                         f->file_size = f->real_file_size;
270         } else
271                 f->real_file_size = f->file_size;
272
273         return 0;
274 }
275
276 static int bdev_size(struct thread_data *td, struct fio_file *f)
277 {
278         unsigned long long bytes;
279         int r;
280
281         r = blockdev_size(f->fd, &bytes);
282         if (r) {
283                 td_verror(td, r, "blockdev_size");
284                 return 1;
285         }
286
287         f->real_file_size = bytes;
288
289         /*
290          * no extend possibilities, so limit size to device size if too large
291          */
292         if (!f->file_size || f->file_size > f->real_file_size)
293                 f->file_size = f->real_file_size;
294
295         f->file_size -= f->file_offset;
296         return 0;
297 }
298
299 static int get_file_size(struct thread_data *td, struct fio_file *f)
300 {
301         int ret = 0;
302
303         if (f->filetype == FIO_TYPE_FILE) {
304                 if (!(f->flags & FIO_FILE_EXISTS))
305                         ret = file_size(td, f);
306         } else if (f->filetype == FIO_TYPE_BD)
307                 ret = bdev_size(td, f);
308         else
309                 f->real_file_size = -1;
310
311         if (ret)
312                 return ret;
313
314         if (f->file_offset > f->real_file_size) {
315                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
316                 return 1;
317         }
318
319         return 0;
320 }
321
322 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
323 {
324         int ret = 0;
325
326         if (td->o.odirect)
327                 return 0;
328
329         /*
330          * FIXME: add blockdev flushing too
331          */
332         if (f->mmap)
333                 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
334         else if (f->filetype == FIO_TYPE_FILE)
335                 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
336         else if (f->filetype == FIO_TYPE_BD)
337                 ret = blockdev_invalidate_cache(f->fd);
338         else if (f->filetype == FIO_TYPE_CHAR)
339                 ret = 0;
340
341         if (ret < 0) {
342                 td_verror(td, errno, "invalidate_cache");
343                 return 1;
344         }
345
346         return ret;
347 }
348
349 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
350 {
351         close(f->fd);
352         f->fd = -1;
353 }
354
355 int generic_open_file(struct thread_data *td, struct fio_file *f)
356 {
357         int flags = 0;
358
359         if (td->o.odirect)
360                 flags |= OS_O_DIRECT;
361         if (td->o.sync_io)
362                 flags |= O_SYNC;
363
364         if (td_write(td) || td_rw(td)) {
365                 flags |= O_RDWR;
366
367                 if (f->filetype == FIO_TYPE_FILE)
368                         flags |= O_CREAT;
369
370                 f->fd = open(f->file_name, flags, 0600);
371         } else {
372                 if (f->filetype == FIO_TYPE_CHAR)
373                         flags |= O_RDWR;
374                 else
375                         flags |= O_RDONLY;
376
377                 f->fd = open(f->file_name, flags);
378         }
379
380         if (f->fd == -1) {
381                 int __e = errno;
382
383                 td_verror(td, __e, "open");
384                 if (__e == EINVAL && td->o.odirect)
385                         log_err("fio: destination does not support O_DIRECT\n");
386                 if (__e == EMFILE)
387                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
388                 return 1;
389         }
390
391         if (get_file_size(td, f))
392                 goto err;
393
394         if (td->o.invalidate_cache && file_invalidate_cache(td, f))
395                 goto err;
396
397         if (!td_random(td)) {
398                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
399                         td_verror(td, errno, "fadvise");
400                         goto err;
401                 }
402         } else {
403                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
404                         td_verror(td, errno, "fadvise");
405                         goto err;
406                 }
407         }
408
409         return 0;
410 err:
411         close(f->fd);
412         return 1;
413 }
414
415 int open_files(struct thread_data *td)
416 {
417         struct fio_file *f;
418         unsigned int i;
419         int err = 0;
420
421         for_each_file(td, f, i) {
422                 err = td_io_open_file(td, f);
423                 if (err)
424                         break;
425
426                 if (td->o.open_files == td->nr_open_files)
427                         break;
428         }
429
430         if (!err)
431                 return 0;
432
433         for_each_file(td, f, i)
434                 td_io_close_file(td, f);
435
436         return err;
437 }
438
439 int setup_files(struct thread_data *td)
440 {
441         struct fio_file *f;
442         unsigned int i;
443         int err;
444
445         /*
446          * if ioengine defines a setup() method, it's responsible for
447          * setting up everything in the td->files[] area.
448          */
449         if (td->io_ops->setup)
450                 return td->io_ops->setup(td);
451
452         if (create_files(td))
453                 return 1;
454
455         err = open_files(td);
456         if (err)
457                 return err;
458
459         /*
460          * Recalculate the total file size now that files are set up.
461          */
462         td->o.size = 0;
463         for_each_file(td, f, i)
464                 td->o.size += f->file_size;
465
466         td->io_size = td->o.size;
467         if (td->io_size == 0) {
468                 log_err("%s: no io blocks\n", td->o.name);
469                 td_verror(td, EINVAL, "total_file_size");
470                 return 1;
471         }
472
473         if (!td->o.zone_size)
474                 td->o.zone_size = td->io_size;
475
476         td->total_io_size = td->io_size * td->o.loops;
477
478         for_each_file(td, f, i)
479                 td_io_close_file(td, f);
480
481         return err;
482 }
483
484 int init_random_map(struct thread_data *td)
485 {
486         int num_maps, blocks;
487         struct fio_file *f;
488         unsigned int i;
489
490         if (td->o.norandommap)
491                 return 0;
492
493         for_each_file(td, f, i) {
494                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
495                 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
496                 f->file_map = malloc(num_maps * sizeof(long));
497                 if (!f->file_map) {
498                         log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
499                         return 1;
500                 }
501                 f->num_maps = num_maps;
502                 memset(f->file_map, 0, num_maps * sizeof(long));
503         }
504
505         return 0;
506 }
507
508 void close_files(struct thread_data *td)
509 {
510         struct fio_file *f;
511         unsigned int i;
512
513         for_each_file(td, f, i) {
514                 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
515                     f->filetype == FIO_TYPE_FILE) {
516                         unlink(f->file_name);
517                         free(f->file_name);
518                         f->file_name = NULL;
519                 }
520
521                 td_io_close_file(td, f);
522
523                 if (f->file_map)
524                         free(f->file_map);
525         }
526
527         td->o.filename = NULL;
528         td->files = NULL;
529         td->o.nr_files = 0;
530 }
531
532 static void get_file_type(struct fio_file *f)
533 {
534         struct stat sb;
535
536         f->filetype = FIO_TYPE_FILE;
537
538         if (!lstat(f->file_name, &sb)) {
539                 f->flags |= FIO_FILE_EXISTS;
540
541                 if (S_ISBLK(sb.st_mode))
542                         f->filetype = FIO_TYPE_BD;
543                 else if (S_ISCHR(sb.st_mode))
544                         f->filetype = FIO_TYPE_CHAR;
545                 else {
546                         /*
547                          * might as well do this here, and save a stat later on
548                          */
549                         f->real_file_size = sb.st_size;
550                         f->file_size = f->real_file_size;
551                 }
552         }
553 }
554
555 void add_file(struct thread_data *td, const char *fname)
556 {
557         int cur_files = td->files_index;
558         struct fio_file *f;
559
560         td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
561
562         f = &td->files[cur_files];
563         memset(f, 0, sizeof(*f));
564         f->fd = -1;
565         f->file_name = strdup(fname);
566
567         get_file_type(f);
568
569         td->files_index++;
570         if (f->filetype == FIO_TYPE_FILE)
571                 td->nr_normal_files++;
572 }
573
574 void get_file(struct fio_file *f)
575 {
576         f->references++;
577 }
578
579 void put_file(struct thread_data *td, struct fio_file *f)
580 {
581         if (!(f->flags & FIO_FILE_OPEN))
582                 return;
583
584         assert(f->references);
585         if (--f->references)
586                 return;
587
588         if (should_fsync(td) && td->o.fsync_on_close)
589                 fsync(f->fd);
590
591         if (td->io_ops->close_file)
592                 td->io_ops->close_file(td, f);
593         td->nr_open_files--;
594         f->flags &= ~FIO_FILE_OPEN;
595 }
596
597 static int recurse_dir(struct thread_data *td, const char *dirname)
598 {
599         struct dirent *dir;
600         int ret = 0;
601         DIR *D;
602
603         D = opendir(dirname);
604         if (!D) {
605                 td_verror(td, errno, "opendir");
606                 return 1;
607         }
608
609         while ((dir = readdir(D)) != NULL) {
610                 char full_path[PATH_MAX];
611                 struct stat sb;
612
613                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
614                         continue;
615
616                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
617
618                 if (lstat(full_path, &sb) == -1) {
619                         if (errno != ENOENT) {
620                                 td_verror(td, errno, "stat");
621                                 return 1;
622                         }
623                 }
624
625                 if (S_ISREG(sb.st_mode)) {
626                         add_file(td, full_path);
627                         td->o.nr_files++;
628                         continue;
629                 }
630
631                 if ((ret = recurse_dir(td, full_path)) != 0)
632                         break;
633         }
634
635         closedir(D);
636         return ret;
637 }
638
639 int add_dir_files(struct thread_data *td, const char *path)
640 {
641         return recurse_dir(td, path);
642 }