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