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