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