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