Update io engine comments
[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 /*
14  * Check if the file exists and it's large enough.
15  */
16 static int file_ok(struct thread_data *td, struct fio_file *f)
17 {
18         struct stat st;
19
20         if (f->filetype != FIO_TYPE_FILE ||
21             (td->io_ops->flags & FIO_DISKLESSIO))
22                 return 0;
23
24         if (lstat(f->file_name, &st) == -1)
25                 return 1;
26
27         /*
28          * if it's a special file, size is always ok for now
29          */
30         if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
31                 return 0;
32         if (st.st_size < (off_t) f->file_size)
33                 return 1;
34
35         return 0;
36 }
37
38 static int create_file(struct thread_data *td, struct fio_file *f)
39 {
40         unsigned long long left;
41         unsigned int bs;
42         char *b;
43         int r;
44
45         f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
46         if (f->fd < 0) {
47                 td_verror(td, errno, "open");
48                 return 1;
49         }
50
51         if (ftruncate(f->fd, f->file_size) == -1) {
52                 td_verror(td, errno, "ftruncate");
53                 goto err;
54         }
55
56         if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
57                 td_verror(td, errno, "posix_fallocate");
58                 goto err;
59         }
60
61         b = malloc(td->max_bs[DDIR_WRITE]);
62         memset(b, 0, td->max_bs[DDIR_WRITE]);
63
64         left = f->file_size;
65         while (left && !td->terminate) {
66                 bs = td->max_bs[DDIR_WRITE];
67                 if (bs > left)
68                         bs = left;
69
70                 r = write(f->fd, b, bs);
71
72                 if (r == (int) bs) {
73                         left -= bs;
74                         continue;
75                 } else {
76                         if (r < 0)
77                                 td_verror(td, errno, "write");
78                         else
79                                 td_verror(td, EIO, "write");
80
81                         break;
82                 }
83         }
84
85         if (td->terminate)
86                 unlink(f->file_name);
87         else if (td->create_fsync)
88                 fsync(f->fd);
89
90         free(b);
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 int create_files(struct thread_data *td)
101 {
102         struct fio_file *f;
103         int err, need_create, can_extend;
104         unsigned long long total_file_size;
105         unsigned int i;
106
107         total_file_size = td->total_file_size;
108         for_each_file(td, f, i) {
109                 unsigned long long s;
110
111                 f->file_offset = td->start_offset;
112
113                 if (f->filetype != FIO_TYPE_FILE)
114                         continue;
115
116                 if (f->flags & FIO_FILE_EXISTS) {
117                         s = f->file_size;
118                         if (s > total_file_size)
119                                 s = total_file_size;
120
121                         total_file_size -= s;
122                 }
123         }
124
125         /*
126          * unless specifically asked for overwrite, let normal io extend it
127          */
128         can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
129         if (can_extend)
130                 return 0;
131
132         need_create = 0;
133         for_each_file(td, f, i) {
134                 int file_there;
135
136                 if (f->filetype != FIO_TYPE_FILE)
137                         continue;
138                 if (f->flags & FIO_FILE_EXISTS)
139                         continue;
140
141                 f->file_size = total_file_size / td->nr_normal_files;
142
143                 file_there = !file_ok(td, f);
144
145                 if (file_there && td_write(td) && !td->overwrite) {
146                         unlink(f->file_name);
147                         file_there = 0;
148                 }
149
150                 need_create += !file_there;
151         }
152
153         if (!need_create)
154                 return 0;
155
156         if (!td->total_file_size) {
157                 log_err("Need size for create\n");
158                 td_verror(td, EINVAL, "file_size");
159                 return 1;
160         }
161
162         temp_stall_ts = 1;
163         fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
164                                 td->name, td->nr_normal_files,
165                                 (td->total_file_size >> 20) / td->nr_normal_files,
166                                 td->total_file_size >> 20);
167
168         err = 0;
169         for_each_file(td, f, i) {
170                 /*
171                  * Only unlink files that we created.
172                  */
173                 f->flags &= ~FIO_FILE_UNLINK;
174                 if (file_ok(td, f)) {
175                         if (td->unlink)
176                                 f->flags |= FIO_FILE_UNLINK;
177
178                         err = create_file(td, f);
179                         if (err)
180                                 break;
181                 }
182         }
183
184         temp_stall_ts = 0;
185         return err;
186 }
187
188 static int file_size(struct thread_data *td, struct fio_file *f)
189 {
190         struct stat st;
191
192         if (td->overwrite) {
193                 if (fstat(f->fd, &st) == -1) {
194                         td_verror(td, errno, "fstat");
195                         return 1;
196                 }
197
198                 f->real_file_size = st.st_size;
199
200                 if (!f->file_size || f->file_size > f->real_file_size)
201                         f->file_size = f->real_file_size;
202         } else
203                 f->real_file_size = f->file_size;
204
205         return 0;
206 }
207
208 static int bdev_size(struct thread_data *td, struct fio_file *f)
209 {
210         unsigned long long bytes;
211         int r;
212
213         r = blockdev_size(f->fd, &bytes);
214         if (r) {
215                 td_verror(td, r, "blockdev_size");
216                 return 1;
217         }
218
219         f->real_file_size = bytes;
220
221         /*
222          * no extend possibilities, so limit size to device size if too large
223          */
224         if (!f->file_size || f->file_size > f->real_file_size)
225                 f->file_size = f->real_file_size;
226
227         f->file_size -= f->file_offset;
228         return 0;
229 }
230
231 static int get_file_size(struct thread_data *td, struct fio_file *f)
232 {
233         int ret = 0;
234
235         if (f->filetype == FIO_TYPE_FILE) {
236                 if (!(f->flags & FIO_FILE_EXISTS))
237                         ret = file_size(td, f);
238         } else if (f->filetype == FIO_TYPE_BD)
239                 ret = bdev_size(td, f);
240         else
241                 f->real_file_size = -1;
242
243         if (ret)
244                 return ret;
245
246         if (f->file_offset > f->real_file_size) {
247                 log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size);
248                 return 1;
249         }
250
251         return 0;
252 }
253
254 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
255 {
256         int ret = 0;
257
258         if (td->odirect)
259                 return 0;
260
261         /*
262          * FIXME: add blockdev flushing too
263          */
264         if (f->mmap)
265                 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
266         else if (f->filetype == FIO_TYPE_FILE) {
267                 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
268         } else if (f->filetype == FIO_TYPE_BD) {
269                 ret = blockdev_invalidate_cache(f->fd);
270         } else if (f->filetype == FIO_TYPE_CHAR)
271                 ret = 0;
272
273         if (ret < 0) {
274                 td_verror(td, errno, "invalidate_cache");
275                 return 1;
276         }
277
278         return ret;
279 }
280
281 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
282 {
283         close(f->fd);
284         f->fd = -1;
285 }
286
287 int generic_open_file(struct thread_data *td, struct fio_file *f)
288 {
289         int flags = 0;
290
291         if (td->odirect)
292                 flags |= OS_O_DIRECT;
293         if (td->sync_io)
294                 flags |= O_SYNC;
295
296         if (td_write(td) || td_rw(td)) {
297                 flags |= O_RDWR;
298
299                 if (f->filetype == FIO_TYPE_FILE)
300                         flags |= O_CREAT;
301
302                 f->fd = open(f->file_name, flags, 0600);
303         } else {
304                 if (f->filetype == FIO_TYPE_CHAR)
305                         flags |= O_RDWR;
306                 else
307                         flags |= O_RDONLY;
308
309                 f->fd = open(f->file_name, flags);
310         }
311
312         if (f->fd == -1) {
313                 int __e = errno;
314
315                 td_verror(td, __e, "open");
316                 if (__e == EINVAL && td->odirect)
317                         log_err("fio: destination does not support O_DIRECT\n");
318                 if (__e == EMFILE)
319                         log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->nr_files);
320                 return 1;
321         }
322
323         if (get_file_size(td, f))
324                 goto err;
325
326         if (td->invalidate_cache && file_invalidate_cache(td, f))
327                 goto err;
328
329         if (!td_random(td)) {
330                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
331                         td_verror(td, errno, "fadvise");
332                         goto err;
333                 }
334         } else {
335                 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
336                         td_verror(td, errno, "fadvise");
337                         goto err;
338                 }
339         }
340
341         return 0;
342 err:
343         close(f->fd);
344         return 1;
345 }
346
347 int open_files(struct thread_data *td)
348 {
349         struct fio_file *f;
350         unsigned int i;
351         int err = 0;
352
353         for_each_file(td, f, i) {
354                 err = td_io_open_file(td, f);
355                 if (err)
356                         break;
357
358                 if (td->open_files == td->nr_open_files)
359                         break;
360         }
361
362         if (!err)
363                 return 0;
364
365         for_each_file(td, f, i)
366                 td_io_close_file(td, f);
367
368         return err;
369 }
370
371 int setup_files(struct thread_data *td)
372 {
373         struct fio_file *f;
374         unsigned int i;
375         int err;
376
377         /*
378          * if ioengine defines a setup() method, it's responsible for
379          * setting up everything in the td->files[] area.
380          */
381         if (td->io_ops->setup)
382                 return td->io_ops->setup(td);
383
384         if (create_files(td))
385                 return 1;
386
387         err = open_files(td);
388         if (err)
389                 return err;
390
391         /*
392          * Recalculate the total file size now that files are set up.
393          */
394         td->total_file_size = 0;
395         for_each_file(td, f, i)
396                 td->total_file_size += f->file_size;
397
398         td->io_size = td->total_file_size;
399         if (td->io_size == 0) {
400                 log_err("%s: no io blocks\n", td->name);
401                 td_verror(td, EINVAL, "total_file_size");
402                 return 1;
403         }
404
405         if (!td->zone_size)
406                 td->zone_size = td->io_size;
407
408         td->total_io_size = td->io_size * td->loops;
409
410         for_each_file(td, f, i)
411                 td_io_close_file(td, f);
412
413         return err;
414 }
415
416 void close_files(struct thread_data *td)
417 {
418         struct fio_file *f;
419         unsigned int i;
420
421         for_each_file(td, f, i) {
422                 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
423                     f->filetype == FIO_TYPE_FILE) {
424                         unlink(f->file_name);
425                         free(f->file_name);
426                         f->file_name = NULL;
427                 }
428
429                 td_io_close_file(td, f);
430
431                 if (f->file_map)
432                         free(f->file_map);
433         }
434
435         td->filename = NULL;
436         free(td->files);
437         td->files = NULL;
438         td->nr_files = 0;
439 }
440
441 static void get_file_type(struct fio_file *f)
442 {
443         struct stat sb;
444
445         f->filetype = FIO_TYPE_FILE;
446
447         if (!lstat(f->file_name, &sb)) {
448                 f->flags |= FIO_FILE_EXISTS;
449
450                 if (S_ISBLK(sb.st_mode))
451                         f->filetype = FIO_TYPE_BD;
452                 else if (S_ISCHR(sb.st_mode))
453                         f->filetype = FIO_TYPE_CHAR;
454                 else {
455                         /*
456                          * might as well do this here, and save a stat later on
457                          */
458                         f->real_file_size = sb.st_size;
459                         f->file_size = f->real_file_size;
460                 }
461         }
462 }
463
464 void add_file(struct thread_data *td, const char *fname)
465 {
466         int cur_files = td->files_index;
467         struct fio_file *f;
468
469         td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
470
471         f = &td->files[cur_files];
472         memset(f, 0, sizeof(*f));
473         f->fd = -1;
474         f->file_name = strdup(fname);
475
476         get_file_type(f);
477
478         td->files_index++;
479         if (f->filetype == FIO_TYPE_FILE)
480                 td->nr_normal_files++;
481 }
482
483 void get_file(struct fio_file *f)
484 {
485         f->references++;
486 }
487
488 void put_file(struct thread_data *td, struct fio_file *f)
489 {
490         if (!(f->flags & FIO_FILE_OPEN))
491                 return;
492
493         assert(f->references);
494         if (--f->references)
495                 return;
496
497         if (should_fsync(td) && td->fsync_on_close)
498                 fsync(f->fd);
499
500         if (td->io_ops->close_file)
501                 td->io_ops->close_file(td, f);
502         td->nr_open_files--;
503         f->flags &= ~FIO_FILE_OPEN;
504 }
505
506 static int recurse_dir(struct thread_data *td, const char *dirname)
507 {
508         struct dirent *dir;
509         int ret = 0;
510         DIR *D;
511
512         D = opendir(dirname);
513         if (!D) {
514                 td_verror(td, errno, "opendir");
515                 return 1;
516         }
517
518         while ((dir = readdir(D)) != NULL) {
519                 char full_path[PATH_MAX];
520                 struct stat sb;
521
522                 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
523                         continue;
524
525                 sprintf(full_path, "%s/%s", dirname, dir->d_name);
526
527                 if (lstat(full_path, &sb) == -1) {
528                         if (errno != ENOENT) {
529                                 td_verror(td, errno, "stat");
530                                 return 1;
531                         }
532                 }
533
534                 if (S_ISREG(sb.st_mode)) {
535                         add_file(td, full_path);
536                         td->nr_files++;
537                         continue;
538                 }
539
540                 if ((ret = recurse_dir(td, full_path)) != 0)
541                         break;
542         }
543
544         closedir(D);
545         return ret;
546 }
547
548 int add_dir_files(struct thread_data *td, const char *path)
549 {
550         return recurse_dir(td, path);
551 }