Don't completely fail for block device flushing failure
[fio.git] / filesetup.c
CommitLineData
53cdc686
JA
1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <assert.h>
bbf6b540 5#include <dirent.h>
53cdc686
JA
6#include <sys/stat.h>
7#include <sys/mman.h>
bbf6b540 8#include <sys/types.h>
53cdc686
JA
9
10#include "fio.h"
11#include "os.h"
12
25205e97
JA
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)
53cdc686 17{
b2a15192 18 struct stat st;
53cdc686 19
af52b345 20 if (f->filetype != FIO_TYPE_FILE ||
b5af8293 21 (td->io_ops->flags & FIO_DISKLESSIO))
b2a15192
JA
22 return 0;
23
fa01d139 24 if (lstat(f->file_name, &st) == -1)
25205e97 25 return 1;
fa01d139
JA
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)
25205e97
JA
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;
b2a15192 44
53cdc686
JA
45 f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
46 if (f->fd < 0) {
e1161c32 47 td_verror(td, errno, "open");
53cdc686
JA
48 return 1;
49 }
50
51 if (ftruncate(f->fd, f->file_size) == -1) {
e1161c32 52 td_verror(td, errno, "ftruncate");
53cdc686
JA
53 goto err;
54 }
55
40f8298c 56 if (posix_fallocate(f->fd, 0, f->file_size) < 0) {
e1161c32 57 td_verror(td, errno, "posix_fallocate");
40f8298c
JA
58 goto err;
59 }
60
2dc1bbeb
JA
61 b = malloc(td->o.max_bs[DDIR_WRITE]);
62 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
53cdc686
JA
63
64 left = f->file_size;
65 while (left && !td->terminate) {
2dc1bbeb 66 bs = td->o.max_bs[DDIR_WRITE];
53cdc686
JA
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)
e1161c32 77 td_verror(td, errno, "write");
53cdc686 78 else
e1161c32 79 td_verror(td, EIO, "write");
53cdc686
JA
80
81 break;
82 }
83 }
84
85 if (td->terminate)
86 unlink(f->file_name);
2dc1bbeb 87 else if (td->o.create_fsync)
53cdc686
JA
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
9c60ce64
JA
100static unsigned long long set_rand_file_size(struct thread_data *td,
101 unsigned long long total_size)
102{
103 unsigned long long upper = total_size;
104 unsigned long long ret;
105 long r;
106
2dc1bbeb
JA
107 if (upper > td->o.file_size_high)
108 upper = td->o.file_size_high;
109 else if (upper < td->o.file_size_low)
9c60ce64
JA
110 return 0;
111 else if (!upper)
112 return 0;
113
114 r = os_random_long(&td->file_size_state);
2dc1bbeb
JA
115 ret = td->o.file_size_low + (unsigned long long) ((double) upper * (r / (RAND_MAX + 1.0)));
116 ret -= (ret % td->o.rw_min_bs);
9c60ce64
JA
117 if (ret > upper)
118 ret = upper;
119 return ret;
120}
121
68727076
JA
122static int fill_file_size(struct thread_data *td, struct fio_file *f,
123 unsigned long long *file_size, int new_files)
124{
125 if (!td->o.file_size_low) {
126 f->file_size = *file_size / new_files;
127 f->real_file_size = f->file_size;
128 } else {
129 /*
130 * If we don't have enough space left for a file
131 * of the minimum size, bail.
132 */
133 if (*file_size < td->o.file_size_low)
134 return 1;
135
136 f->file_size = set_rand_file_size(td, *file_size);
137 f->real_file_size = f->file_size;
138 *file_size -= f->file_size;
139 }
140
141 return 0;
142}
143
53cdc686
JA
144static int create_files(struct thread_data *td)
145{
146 struct fio_file *f;
af52b345 147 int err, need_create, can_extend;
e8be395b 148 unsigned long long total_file_size, local_file_size, create_size;
e407bec2 149 unsigned int i, new_files;
53cdc686 150
e407bec2 151 new_files = 0;
2dc1bbeb 152 total_file_size = td->o.size;
1549441c 153 for_each_file(td, f, i) {
96d32d51
JA
154 unsigned long long s;
155
2dc1bbeb 156 f->file_offset = td->o.start_offset;
96d32d51 157
ee3dcd95
JA
158 if (!total_file_size)
159 continue;
1549441c 160
1d56dfee
JA
161 if (f->filetype != FIO_TYPE_FILE) {
162 if (!f->file_size)
163 f->file_size = total_file_size / td->o.nr_files;
164 continue;
165 }
166
96d32d51 167 if (f->flags & FIO_FILE_EXISTS) {
ee3dcd95
JA
168 if ((f->file_size > td->o.size / td->o.nr_files) ||
169 !f->file_size)
170 f->file_size = td->o.size / td->o.nr_files;
171
96d32d51
JA
172 s = f->file_size;
173 if (s > total_file_size)
174 s = total_file_size;
175
176 total_file_size -= s;
e407bec2
JA
177 } else
178 new_files++;
1549441c 179 }
f697125a 180
53cdc686
JA
181 /*
182 * unless specifically asked for overwrite, let normal io extend it
183 */
2dc1bbeb 184 can_extend = !td->o.overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
3f344202 185 if (can_extend && new_files) {
68727076
JA
186 for_each_file(td, f, i) {
187 if (fill_file_size(td, f, &total_file_size, new_files)) {
188 log_info("fio: limited to %d files\n", i);
189 td->o.nr_files = i;
190 break;
191 }
192 }
193
53cdc686 194 return 0;
68727076 195 }
53cdc686 196
9c60ce64
JA
197 local_file_size = total_file_size;
198 if (!local_file_size)
199 local_file_size = -1;
200
f4c4f4d7
JA
201 total_file_size = 0;
202 need_create = 0;
e8be395b 203 create_size = 0;
af52b345
JA
204 for_each_file(td, f, i) {
205 int file_there;
206
207 if (f->filetype != FIO_TYPE_FILE)
208 continue;
f4c4f4d7
JA
209 if (f->flags & FIO_FILE_EXISTS) {
210 total_file_size += f->file_size;
96d32d51 211 continue;
f4c4f4d7 212 }
96d32d51 213
68727076
JA
214 if (fill_file_size(td, f, &local_file_size, new_files)) {
215 log_info("fio: limited to %d files\n", i);
216 new_files -= (td->o.nr_files - i);
217 td->o.nr_files = i;
218 break;
9c60ce64 219 }
eff6861d 220
f4c4f4d7 221 total_file_size += f->file_size;
e8be395b 222 create_size += f->file_size;
af52b345 223 file_there = !file_ok(td, f);
eff6861d 224
2dc1bbeb 225 if (file_there && td_write(td) && !td->o.overwrite) {
af52b345
JA
226 unlink(f->file_name);
227 file_there = 0;
eff6861d 228 }
af52b345
JA
229
230 need_create += !file_there;
eff6861d 231 }
25205e97 232
25205e97
JA
233 if (!need_create)
234 return 0;
235
2dc1bbeb 236 if (!td->o.size && !total_file_size) {
f1027063 237 log_err("Need size for create\n");
e1161c32 238 td_verror(td, EINVAL, "file_size");
f1027063
JA
239 return 1;
240 }
241
53cdc686 242 temp_stall_ts = 1;
6d86144d 243 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
2dc1bbeb 244 td->o.name, new_files, create_size >> 20);
53cdc686
JA
245
246 err = 0;
247 for_each_file(td, f, i) {
9b031dc8
JA
248 /*
249 * Only unlink files that we created.
250 */
f11bd94d 251 f->flags &= ~FIO_FILE_UNLINK;
25205e97 252 if (file_ok(td, f)) {
2dc1bbeb 253 if (td->o.unlink)
f11bd94d
JA
254 f->flags |= FIO_FILE_UNLINK;
255
160b966d 256 f->flags |= FIO_FILE_NOSORT;
25205e97
JA
257 err = create_file(td, f);
258 if (err)
259 break;
260 }
53cdc686
JA
261 }
262
263 temp_stall_ts = 0;
264 return err;
265}
266
267static int file_size(struct thread_data *td, struct fio_file *f)
268{
269 struct stat st;
270
2dc1bbeb 271 if (td->o.overwrite) {
53cdc686 272 if (fstat(f->fd, &st) == -1) {
e1161c32 273 td_verror(td, errno, "fstat");
53cdc686
JA
274 return 1;
275 }
276
277 f->real_file_size = st.st_size;
278
279 if (!f->file_size || f->file_size > f->real_file_size)
280 f->file_size = f->real_file_size;
745508dd
JA
281 } else
282 f->real_file_size = f->file_size;
53cdc686 283
53cdc686
JA
284 return 0;
285}
286
287static int bdev_size(struct thread_data *td, struct fio_file *f)
288{
289 unsigned long long bytes;
290 int r;
291
292 r = blockdev_size(f->fd, &bytes);
293 if (r) {
e1161c32 294 td_verror(td, r, "blockdev_size");
53cdc686
JA
295 return 1;
296 }
297
298 f->real_file_size = bytes;
299
300 /*
301 * no extend possibilities, so limit size to device size if too large
302 */
303 if (!f->file_size || f->file_size > f->real_file_size)
304 f->file_size = f->real_file_size;
305
306 f->file_size -= f->file_offset;
307 return 0;
308}
309
310static int get_file_size(struct thread_data *td, struct fio_file *f)
311{
312 int ret = 0;
313
96d32d51
JA
314 if (f->filetype == FIO_TYPE_FILE) {
315 if (!(f->flags & FIO_FILE_EXISTS))
316 ret = file_size(td, f);
317 } else if (f->filetype == FIO_TYPE_BD)
53cdc686
JA
318 ret = bdev_size(td, f);
319 else
320 f->real_file_size = -1;
321
322 if (ret)
323 return ret;
324
325 if (f->file_offset > f->real_file_size) {
2dc1bbeb 326 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
53cdc686
JA
327 return 1;
328 }
329
53cdc686
JA
330 return 0;
331}
332
e5b401d4
JA
333int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
334{
335 int ret = 0;
336
2dc1bbeb 337 if (td->o.odirect)
b5af8293
JA
338 return 0;
339
e5b401d4
JA
340 /*
341 * FIXME: add blockdev flushing too
342 */
b5af8293 343 if (f->mmap)
e5b401d4 344 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
467d1b6b 345 else if (f->filetype == FIO_TYPE_FILE)
b5af8293 346 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
7e0e25c9 347 else if (f->filetype == FIO_TYPE_BD) {
b5af8293 348 ret = blockdev_invalidate_cache(f->fd);
7e0e25c9
JA
349 if (ret < 0 && errno == EACCES && geteuid()) {
350 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
351 ret = 0;
352 }
353 } else if (f->filetype == FIO_TYPE_CHAR)
e5b401d4
JA
354 ret = 0;
355
356 if (ret < 0) {
e1161c32 357 td_verror(td, errno, "invalidate_cache");
e5b401d4
JA
358 return 1;
359 }
360
ad2da605 361 return ret;
e5b401d4
JA
362}
363
b5af8293 364void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 365{
b5af8293
JA
366 close(f->fd);
367 f->fd = -1;
53cdc686
JA
368}
369
b5af8293 370int generic_open_file(struct thread_data *td, struct fio_file *f)
53cdc686 371{
53cdc686
JA
372 int flags = 0;
373
2dc1bbeb 374 if (td->o.odirect)
2fd233b7 375 flags |= OS_O_DIRECT;
2dc1bbeb 376 if (td->o.sync_io)
2fd233b7 377 flags |= O_SYNC;
53cdc686 378
2fd233b7
JA
379 if (td_write(td) || td_rw(td)) {
380 flags |= O_RDWR;
53cdc686 381
af52b345 382 if (f->filetype == FIO_TYPE_FILE)
2fd233b7 383 flags |= O_CREAT;
2fd233b7 384
b5af8293 385 f->fd = open(f->file_name, flags, 0600);
2fd233b7 386 } else {
af52b345 387 if (f->filetype == FIO_TYPE_CHAR)
2fd233b7
JA
388 flags |= O_RDWR;
389 else
390 flags |= O_RDONLY;
391
b5af8293 392 f->fd = open(f->file_name, flags);
53cdc686
JA
393 }
394
395 if (f->fd == -1) {
e4e33258 396 char buf[FIO_VERROR_SIZE];
e1161c32
JA
397 int __e = errno;
398
e4e33258
JA
399 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
400
401 td_verror(td, __e, buf);
2dc1bbeb 402 if (__e == EINVAL && td->o.odirect)
9d80e114 403 log_err("fio: destination does not support O_DIRECT\n");
8ab53873 404 if (__e == EMFILE)
2dc1bbeb 405 log_err("fio: try reducing/setting openfiles (failed at %u of %u)\n", td->nr_open_files, td->o.nr_files);
53cdc686
JA
406 return 1;
407 }
408
b5af8293
JA
409 if (get_file_size(td, f))
410 goto err;
411
2dc1bbeb 412 if (td->o.invalidate_cache && file_invalidate_cache(td, f))
b5af8293
JA
413 goto err;
414
d2f3ac35
JA
415 if (!td->o.fadvise_hint)
416 return 0;
417
b5af8293
JA
418 if (!td_random(td)) {
419 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
420 td_verror(td, errno, "fadvise");
421 goto err;
422 }
423 } else {
424 if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) {
425 td_verror(td, errno, "fadvise");
426 goto err;
427 }
bdb4e2e9 428 }
53cdc686
JA
429
430 return 0;
b5af8293
JA
431err:
432 close(f->fd);
433 return 1;
434}
435
21972cde
JA
436int open_files(struct thread_data *td)
437{
438 struct fio_file *f;
af52b345
JA
439 unsigned int i;
440 int err = 0;
21972cde
JA
441
442 for_each_file(td, f, i) {
b5af8293 443 err = td_io_open_file(td, f);
21972cde
JA
444 if (err)
445 break;
b5af8293 446
2dc1bbeb 447 if (td->o.open_files == td->nr_open_files)
b5af8293 448 break;
21972cde
JA
449 }
450
7abf833d
JA
451 if (!err)
452 return 0;
453
bdb4e2e9 454 for_each_file(td, f, i)
b5af8293 455 td_io_close_file(td, f);
7abf833d 456
21972cde
JA
457 return err;
458}
459
53cdc686
JA
460int setup_files(struct thread_data *td)
461{
462 struct fio_file *f;
af52b345
JA
463 unsigned int i;
464 int err;
53cdc686
JA
465
466 /*
467 * if ioengine defines a setup() method, it's responsible for
468 * setting up everything in the td->files[] area.
469 */
470 if (td->io_ops->setup)
471 return td->io_ops->setup(td);
472
473 if (create_files(td))
474 return 1;
475
21972cde 476 err = open_files(td);
f1027063
JA
477 if (err)
478 return err;
479
0a7eb121
JA
480 /*
481 * Recalculate the total file size now that files are set up.
482 */
2dc1bbeb 483 td->o.size = 0;
0a7eb121 484 for_each_file(td, f, i)
2dc1bbeb 485 td->o.size += f->file_size;
0a7eb121 486
2dc1bbeb 487 td->io_size = td->o.size;
53cdc686 488 if (td->io_size == 0) {
2dc1bbeb 489 log_err("%s: no io blocks\n", td->o.name);
e1161c32 490 td_verror(td, EINVAL, "total_file_size");
53cdc686
JA
491 return 1;
492 }
493
2dc1bbeb
JA
494 if (!td->o.zone_size)
495 td->o.zone_size = td->io_size;
53cdc686 496
2dc1bbeb 497 td->total_io_size = td->io_size * td->o.loops;
53cdc686 498
bdb4e2e9 499 for_each_file(td, f, i)
b5af8293 500 td_io_close_file(td, f);
21972cde
JA
501
502 return err;
53cdc686
JA
503}
504
68727076
JA
505int init_random_map(struct thread_data *td)
506{
507 int num_maps, blocks;
508 struct fio_file *f;
509 unsigned int i;
510
511 if (td->o.norandommap)
512 return 0;
513
514 for_each_file(td, f, i) {
515 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
516 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
517 f->file_map = malloc(num_maps * sizeof(long));
518 if (!f->file_map) {
519 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
520 return 1;
521 }
522 f->num_maps = num_maps;
523 memset(f->file_map, 0, num_maps * sizeof(long));
524 }
525
526 return 0;
527}
528
53cdc686
JA
529void close_files(struct thread_data *td)
530{
0ab8db89 531 struct fio_file *f;
af52b345 532 unsigned int i;
53cdc686 533
0ab8db89 534 for_each_file(td, f, i) {
fa1da865
JA
535 if ((f->flags & FIO_FILE_UNLINK) &&
536 f->filetype == FIO_TYPE_FILE)
132ad46d 537 unlink(f->file_name);
bdb4e2e9 538
b5af8293 539 td_io_close_file(td, f);
b3dc7f07 540
fa1da865
JA
541 free(f->file_name);
542 f->file_name = NULL;
543
c343981b 544 if (f->file_map) {
b3dc7f07 545 free(f->file_map);
c343981b
JA
546 f->file_map = NULL;
547 }
53cdc686 548 }
b4a6a59a 549
2dc1bbeb 550 td->o.filename = NULL;
cade3ef4 551 free(td->files);
b4a6a59a 552 td->files = NULL;
2dc1bbeb 553 td->o.nr_files = 0;
53cdc686 554}
af52b345 555
e3bab463 556static void get_file_type(struct fio_file *f)
af52b345
JA
557{
558 struct stat sb;
559
560 f->filetype = FIO_TYPE_FILE;
561
e3bab463 562 if (!lstat(f->file_name, &sb)) {
96d32d51
JA
563 f->flags |= FIO_FILE_EXISTS;
564
af52b345
JA
565 if (S_ISBLK(sb.st_mode))
566 f->filetype = FIO_TYPE_BD;
567 else if (S_ISCHR(sb.st_mode))
568 f->filetype = FIO_TYPE_CHAR;
96d32d51
JA
569 else {
570 /*
571 * might as well do this here, and save a stat later on
572 */
573 f->real_file_size = sb.st_size;
574 f->file_size = f->real_file_size;
575 }
af52b345
JA
576 }
577}
578
579void add_file(struct thread_data *td, const char *fname)
580{
7b4e4fe5 581 int cur_files = td->files_index;
bd0ee748 582 char file_name[PATH_MAX];
af52b345 583 struct fio_file *f;
bd0ee748 584 int len = 0;
af52b345
JA
585
586 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
587
588 f = &td->files[cur_files];
589 memset(f, 0, sizeof(*f));
590 f->fd = -1;
bd0ee748
JA
591
592 if (td->o.directory)
593 len = sprintf(file_name, "%s/", td->o.directory);
594
595 sprintf(file_name + len, "%s", fname);
596 f->file_name = strdup(file_name);
af52b345 597
e3bab463 598 get_file_type(f);
af52b345 599
7b4e4fe5 600 td->files_index++;
1549441c
JA
601 if (f->filetype == FIO_TYPE_FILE)
602 td->nr_normal_files++;
af52b345 603}
0ad920e7
JA
604
605void get_file(struct fio_file *f)
606{
607 f->references++;
608}
609
610void put_file(struct thread_data *td, struct fio_file *f)
611{
612 if (!(f->flags & FIO_FILE_OPEN))
613 return;
614
615 assert(f->references);
616 if (--f->references)
617 return;
618
2dc1bbeb 619 if (should_fsync(td) && td->o.fsync_on_close)
ebb1415f
JA
620 fsync(f->fd);
621
0ad920e7
JA
622 if (td->io_ops->close_file)
623 td->io_ops->close_file(td, f);
624 td->nr_open_files--;
625 f->flags &= ~FIO_FILE_OPEN;
626}
bbf6b540
JA
627
628static int recurse_dir(struct thread_data *td, const char *dirname)
629{
630 struct dirent *dir;
631 int ret = 0;
632 DIR *D;
633
634 D = opendir(dirname);
635 if (!D) {
636 td_verror(td, errno, "opendir");
637 return 1;
638 }
639
640 while ((dir = readdir(D)) != NULL) {
641 char full_path[PATH_MAX];
642 struct stat sb;
643
e85b2b83
JA
644 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
645 continue;
96d32d51 646
bbf6b540
JA
647 sprintf(full_path, "%s/%s", dirname, dir->d_name);
648
649 if (lstat(full_path, &sb) == -1) {
650 if (errno != ENOENT) {
651 td_verror(td, errno, "stat");
652 return 1;
653 }
654 }
655
656 if (S_ISREG(sb.st_mode)) {
657 add_file(td, full_path);
2dc1bbeb 658 td->o.nr_files++;
bbf6b540
JA
659 continue;
660 }
661
bbf6b540
JA
662 if ((ret = recurse_dir(td, full_path)) != 0)
663 break;
664 }
665
666 closedir(D);
667 return ret;
668}
669
670int add_dir_files(struct thread_data *td, const char *path)
671{
672 return recurse_dir(td, path);
673}
cade3ef4
JA
674
675void dup_files(struct thread_data *td, struct thread_data *org)
676{
677 struct fio_file *f;
678 unsigned int i;
679 size_t bytes;
680
681 if (!org->files)
682 return;
683
684 bytes = org->files_index * sizeof(*f);
685 td->files = malloc(bytes);
686 memcpy(td->files, org->files, bytes);
687
688 for_each_file(td, f, i) {
689 if (f->file_name)
690 f->file_name = strdup(f->file_name);
691 }
692}