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