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