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