5134fdbb8af5a52e08e5ad8b60ea820e0caeab9f
[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 (ftruncate(f->fd, f->real_file_size) == -1) {
54                 td_verror(td, errno, "ftruncate");
55                 goto err;
56         }
57
58         if (!new_layout)
59                 goto done;
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         if (td_write(td)) {
238                 assert(!read_only);
239
240                 flags |= O_RDWR;
241
242                 if (f->filetype == FIO_TYPE_FILE)
243                         flags |= O_CREAT;
244
245                 if (is_std)
246                         f->fd = dup(STDOUT_FILENO);
247                 else
248                         f->fd = open(f->file_name, flags, 0600);
249         } else {
250                 if (f->filetype == FIO_TYPE_CHAR && !read_only)
251                         flags |= O_RDWR;
252                 else
253                         flags |= O_RDONLY;
254
255                 if (is_std)
256                         f->fd = dup(STDIN_FILENO);
257                 else
258                         f->fd = open(f->file_name, flags);
259         }
260
261         if (f->fd == -1) {
262                 char buf[FIO_VERROR_SIZE];
263                 int __e = errno;
264
265                 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
266
267                 td_verror(td, __e, buf);
268         }
269
270         if (get_file_size(td, f))
271                 goto err;
272
273         return 0;
274 err:
275         close(f->fd);
276         return 1;
277 }
278
279 int open_files(struct thread_data *td)
280 {
281         struct fio_file *f;
282         unsigned int i;
283         int err = 0;
284
285         for_each_file(td, f, i) {
286                 err = td_io_open_file(td, f);
287                 if (err) {
288                         if (td->error == EMFILE) {
289                                 log_err("fio: limited open files to: %d\n", td->nr_open_files);
290                                 td->o.open_files = td->nr_open_files;
291                                 err = 0;
292                                 clear_error(td);
293                         }
294                         break;
295                 }
296
297                 if (td->o.open_files == td->nr_open_files)
298                         break;
299         }
300
301         if (!err)
302                 return 0;
303
304         for_each_file(td, f, i)
305                 td_io_close_file(td, f);
306
307         return err;
308 }
309
310 /*
311  * open/close all files, so that ->real_file_size gets set
312  */
313 static int get_file_sizes(struct thread_data *td)
314 {
315         struct fio_file *f;
316         unsigned int i;
317         int err = 0;
318
319         for_each_file(td, f, i) {
320                 if (td->io_ops->open_file(td, f)) {
321                         if (td->error != ENOENT) {
322                                 log_err("%s\n", td->verror);
323                                 err = 1;
324                         }
325                         clear_error(td);
326                 } else {
327                         if (td->io_ops->close_file)
328                                 td->io_ops->close_file(td, f);
329                 }
330
331                 if (f->real_file_size == -1ULL && td->o.size)
332                         f->real_file_size = td->o.size / td->o.nr_files;
333         }
334
335         return err;
336 }
337
338 /*
339  * Open the files and setup files sizes, creating files if necessary.
340  */
341 int setup_files(struct thread_data *td)
342 {
343         unsigned long long total_size, extend_size;
344         struct fio_file *f;
345         unsigned int i;
346         int err = 0, need_extend;
347
348         /*
349          * if ioengine defines a setup() method, it's responsible for
350          * opening the files and setting f->real_file_size to indicate
351          * the valid range for that file.
352          */
353         if (td->io_ops->setup)
354                 err = td->io_ops->setup(td);
355         else
356                 err = get_file_sizes(td);
357
358         if (err)
359                 return err;
360
361         /*
362          * check sizes. if the files/devices do not exist and the size
363          * isn't passed to fio, abort.
364          */
365         total_size = 0;
366         for_each_file(td, f, i) {
367                 if (f->real_file_size == -1ULL)
368                         total_size = -1ULL;
369                 else
370                         total_size += f->real_file_size;
371         }
372
373         /*
374          * device/file sizes are zero and no size given, punt
375          */
376         if ((!total_size || total_size == -1ULL) && !td->o.size &&
377             !(td->io_ops->flags & FIO_NOIO)) {
378                 log_err("%s: you need to specify size=\n", td->o.name);
379                 td_verror(td, EINVAL, "total_file_size");
380                 return 1;
381         }
382
383         /*
384          * now file sizes are known, so we can set ->io_size. if size= is
385          * not given, ->io_size is just equal to ->real_file_size. if size
386          * is given, ->io_size is size / nr_files.
387          */
388         extend_size = total_size = 0;
389         need_extend = 0;
390         for_each_file(td, f, i) {
391                 f->file_offset = td->o.start_offset;
392
393                 if (!td->o.file_size_low) {
394                         /*
395                          * no file size range given, file size is equal to
396                          * total size divided by number of files. if that is
397                          * zero, set it to the real file size.
398                          */
399                         f->io_size = td->o.size / td->o.nr_files;
400                         if (!f->io_size) {
401                                 if (f->file_offset > f->real_file_size)
402                                         goto err_offset;
403                                 f->io_size = f->real_file_size - f->file_offset;
404                         }
405                 } else if (f->real_file_size < td->o.file_size_low ||
406                            f->real_file_size > td->o.file_size_high) {
407                         if (f->file_offset > td->o.file_size_low) 
408                                 goto err_offset;
409                         /*
410                          * file size given. if it's fixed, use that. if it's a
411                          * range, generate a random size in-between.
412                          */
413                         if (td->o.file_size_low == td->o.file_size_high)
414                                 f->io_size = td->o.file_size_low - f->file_offset;
415                         else
416                                 f->io_size = get_rand_file_size(td) - f->file_offset;
417                 } else if (f->file_offset > f->real_file_size)
418                         goto err_offset;
419                 else
420                         f->io_size = f->real_file_size - f->file_offset;
421
422                 if (f->io_size == -1ULL)
423                         total_size = -1ULL;
424                 else
425                         total_size += f->io_size;
426
427                 if (f->filetype == FIO_TYPE_FILE &&
428                     (f->io_size + f->file_offset) > f->real_file_size &&
429                     !(td->io_ops->flags & FIO_DISKLESSIO)) {
430                         need_extend++;
431                         extend_size += (f->io_size + f->file_offset);
432                         f->flags |= FIO_FILE_EXTEND;
433                 }       
434         }
435
436         if (!td->o.size || td->o.size > total_size)
437                 td->o.size = total_size;
438
439         /*
440          * See if we need to extend some files
441          */
442         if (need_extend) {
443                 temp_stall_ts = 1;
444                 log_info("%s: Laying out IO file(s) (%u file(s) / %LuMiB)\n",
445                         td->o.name, need_extend, extend_size >> 20);
446
447                 for_each_file(td, f, i) {
448                         if (!(f->flags & FIO_FILE_EXTEND))
449                                 continue;
450
451                         assert(f->filetype == FIO_TYPE_FILE);
452                         f->flags &= ~FIO_FILE_EXTEND;
453                         f->real_file_size = (f->io_size + f->file_offset);
454                         err = extend_file(td, f);
455                         if (err)
456                                 break;
457                 }
458                 temp_stall_ts = 0;
459         }
460
461         if (err)
462                 return err;
463
464         if (!td->o.zone_size)
465                 td->o.zone_size = td->o.size;
466
467         /*
468          * iolog already set the total io size, if we read back
469          * stored entries.
470          */
471         if (!td->o.read_iolog_file)
472                 td->total_io_size = td->o.size * td->o.loops;
473         return 0;
474 err_offset:
475         log_err("%s: you need to specify valid offset=\n", td->o.name);
476         return 1;
477 }
478
479 int init_random_map(struct thread_data *td)
480 {
481         int num_maps, blocks;
482         struct fio_file *f;
483         unsigned int i;
484
485         if (td->o.norandommap)
486                 return 0;
487
488         for_each_file(td, f, i) {
489                 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
490                 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
491                 f->file_map = malloc(num_maps * sizeof(long));
492                 if (!f->file_map) {
493                         log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
494                         return 1;
495                 }
496                 f->num_maps = num_maps;
497                 memset(f->file_map, 0, num_maps * sizeof(long));
498         }
499
500         return 0;
501 }
502
503 void close_files(struct thread_data *td)
504 {
505         struct fio_file *f;
506         unsigned int i;
507
508         for_each_file(td, f, i) {
509                 if (td->o.unlink && f->filetype == FIO_TYPE_FILE)
510                         unlink(f->file_name);
511
512                 td_io_close_file(td, f);
513
514                 free(f->file_name);
515                 f->file_name = NULL;
516
517                 if (f->file_map) {
518                         free(f->file_map);
519                         f->file_map = NULL;
520                 }
521         }
522
523         td->o.filename = NULL;
524         free(td->files);
525         td->files = NULL;
526         td->o.nr_files = 0;
527 }
528
529 static void get_file_type(struct fio_file *f)
530 {
531         struct stat sb;
532
533         if (!strcmp(f->file_name, "-"))
534                 f->filetype = FIO_TYPE_PIPE;
535         else
536                 f->filetype = FIO_TYPE_FILE;
537
538         if (!lstat(f->file_name, &sb)) {
539                 if (S_ISBLK(sb.st_mode))
540                         f->filetype = FIO_TYPE_BD;
541                 else if (S_ISCHR(sb.st_mode))
542                         f->filetype = FIO_TYPE_CHAR;
543                 else if (S_ISFIFO(sb.st_mode))
544                         f->filetype = FIO_TYPE_PIPE;
545         }
546 }
547
548 int add_file(struct thread_data *td, const char *fname)
549 {
550         int cur_files = td->files_index;
551         char file_name[PATH_MAX];
552         struct fio_file *f;
553         int len = 0;
554
555         td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
556
557         f = &td->files[cur_files];
558         memset(f, 0, sizeof(*f));
559         f->fd = -1;
560
561         /*
562          * init function, io engine may not be loaded yet
563          */
564         if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
565                 f->real_file_size = -1ULL;
566
567         if (td->o.directory)
568                 len = sprintf(file_name, "%s/", td->o.directory);
569
570         sprintf(file_name + len, "%s", fname);
571         f->file_name = strdup(file_name);
572
573         get_file_type(f);
574
575         td->files_index++;
576         if (f->filetype == FIO_TYPE_FILE)
577                 td->nr_normal_files++;
578
579         return cur_files;
580 }
581
582 void get_file(struct fio_file *f)
583 {
584         assert(f->flags & FIO_FILE_OPEN);
585         f->references++;
586 }
587
588 void put_file(struct thread_data *td, struct fio_file *f)
589 {
590         if (!(f->flags & FIO_FILE_OPEN))
591                 return;
592
593         assert(f->references);
594         if (--f->references)
595                 return;
596
597         if (should_fsync(td) && td->o.fsync_on_close)
598                 fsync(f->fd);
599
600         if (td->io_ops->close_file)
601                 td->io_ops->close_file(td, f);
602
603         td->nr_open_files--;
604         f->flags &= ~FIO_FILE_OPEN;
605 }
606
607 static int recurse_dir(struct thread_data *td, const char *dirname)
608 {
609         struct dirent *dir;
610         int ret = 0;
611         DIR *D;
612
613         D = opendir(dirname);
614         if (!D) {
615                 char buf[FIO_VERROR_SIZE];
616
617                 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
618                 td_verror(td, errno, buf);
619                 return 1;
620         }
621
622         while ((dir = readdir(D)) != NULL) {
623                 char full_path[PATH_MAX];
624                 struct stat sb;
625
626                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
627                         continue;
628
629                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
630
631                 if (lstat(full_path, &sb) == -1) {
632                         if (errno != ENOENT) {
633                                 td_verror(td, errno, "stat");
634                                 return 1;
635                         }
636                 }
637
638                 if (S_ISREG(sb.st_mode)) {
639                         add_file(td, full_path);
640                         td->o.nr_files++;
641                         continue;
642                 }
643                 if (!S_ISDIR(sb.st_mode))
644                         continue;
645
646                 if ((ret = recurse_dir(td, full_path)) != 0)
647                         break;
648         }
649
650         closedir(D);
651         return ret;
652 }
653
654 int add_dir_files(struct thread_data *td, const char *path)
655 {
656         int ret = recurse_dir(td, path);
657
658         if (!ret)
659                 log_info("fio: opendir added %d files\n", td->o.nr_files);
660
661         return ret;
662 }
663
664 void dup_files(struct thread_data *td, struct thread_data *org)
665 {
666         struct fio_file *f;
667         unsigned int i;
668         size_t bytes;
669
670         if (!org->files)
671                 return;
672
673         bytes = org->files_index * sizeof(*f);
674         td->files = malloc(bytes);
675         memcpy(td->files, org->files, bytes);
676
677         for_each_file(td, f, i) {
678                 if (f->file_name)
679                         f->file_name = strdup(f->file_name);
680         }
681 }
682
683 /*
684  * Returns the index that matches the filename, or -1 if not there
685  */
686 int get_fileno(struct thread_data *td, const char *fname)
687 {
688         struct fio_file *f;
689         unsigned int i;
690
691         for_each_file(td, f, i)
692                 if (!strcmp(f->file_name, fname))
693                         return i;
694
695         return -1;
696 }
697
698 /*
699  * For log usage, where we add/open/close files automatically
700  */
701 void free_release_files(struct thread_data *td)
702 {
703         close_files(td);
704         td->files_index = 0;
705         td->nr_normal_files = 0;
706 }