Merge branch 'master' of ssh://brick.kernel.dk/data/git/fio
[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
100static int create_files(struct thread_data *td)
101{
102 struct fio_file *f;
af52b345 103 int err, need_create, can_extend;
96d32d51 104 unsigned long long total_file_size;
e407bec2 105 unsigned int i, new_files;
53cdc686 106
e407bec2 107 new_files = 0;
96d32d51 108 total_file_size = td->total_file_size;
1549441c 109 for_each_file(td, f, i) {
96d32d51
JA
110 unsigned long long s;
111
112 f->file_offset = td->start_offset;
113
1549441c
JA
114 if (f->filetype != FIO_TYPE_FILE)
115 continue;
116
96d32d51
JA
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;
e407bec2
JA
123 } else
124 new_files++;
1549441c 125 }
f697125a 126
53cdc686
JA
127 /*
128 * unless specifically asked for overwrite, let normal io extend it
129 */
0263882a
JA
130 can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND);
131 if (can_extend)
53cdc686 132 return 0;
53cdc686 133
25205e97 134 need_create = 0;
af52b345
JA
135 for_each_file(td, f, i) {
136 int file_there;
137
138 if (f->filetype != FIO_TYPE_FILE)
139 continue;
96d32d51
JA
140 if (f->flags & FIO_FILE_EXISTS)
141 continue;
142
e407bec2 143 f->file_size = total_file_size / new_files;
eff6861d 144
af52b345 145 file_there = !file_ok(td, f);
eff6861d 146
af52b345
JA
147 if (file_there && td_write(td) && !td->overwrite) {
148 unlink(f->file_name);
149 file_there = 0;
eff6861d 150 }
af52b345
JA
151
152 need_create += !file_there;
eff6861d 153 }
25205e97 154
25205e97
JA
155 if (!need_create)
156 return 0;
157
f1027063
JA
158 if (!td->total_file_size) {
159 log_err("Need size for create\n");
e1161c32 160 td_verror(td, EINVAL, "file_size");
f1027063
JA
161 return 1;
162 }
163
53cdc686 164 temp_stall_ts = 1;
1e97cce9 165 fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n",
e407bec2
JA
166 td->name, new_files,
167 (total_file_size >> 20) / new_files,
168 total_file_size >> 20);
53cdc686
JA
169
170 err = 0;
171 for_each_file(td, f, i) {
9b031dc8
JA
172 /*
173 * Only unlink files that we created.
174 */
f11bd94d 175 f->flags &= ~FIO_FILE_UNLINK;
25205e97 176 if (file_ok(td, f)) {
f11bd94d
JA
177 if (td->unlink)
178 f->flags |= FIO_FILE_UNLINK;
179
25205e97
JA
180 err = create_file(td, f);
181 if (err)
182 break;
183 }
53cdc686
JA
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) {
e1161c32 196 td_verror(td, errno, "fstat");
53cdc686
JA
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;
745508dd
JA
204 } else
205 f->real_file_size = f->file_size;
53cdc686 206
53cdc686
JA
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) {
e1161c32 217 td_verror(td, r, "blockdev_size");
53cdc686
JA
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
96d32d51
JA
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)
53cdc686
JA
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
53cdc686
JA
253 return 0;
254}
255
e5b401d4
JA
256int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
257{
258 int ret = 0;
259
da86ccb6 260 if (td->odirect)
b5af8293
JA
261 return 0;
262
e5b401d4
JA
263 /*
264 * FIXME: add blockdev flushing too
265 */
b5af8293 266 if (f->mmap)
e5b401d4 267 ret = madvise(f->mmap, f->file_size, MADV_DONTNEED);
af52b345 268 else if (f->filetype == FIO_TYPE_FILE) {
b5af8293 269 ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED);
af52b345 270 } else if (f->filetype == FIO_TYPE_BD) {
b5af8293 271 ret = blockdev_invalidate_cache(f->fd);
af52b345 272 } else if (f->filetype == FIO_TYPE_CHAR)
e5b401d4
JA
273 ret = 0;
274
275 if (ret < 0) {
e1161c32 276 td_verror(td, errno, "invalidate_cache");
e5b401d4
JA
277 return 1;
278 }
279
ad2da605 280 return ret;
e5b401d4
JA
281}
282
b5af8293 283void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
53cdc686 284{
b5af8293
JA
285 close(f->fd);
286 f->fd = -1;
53cdc686
JA
287}
288
b5af8293 289int generic_open_file(struct thread_data *td, struct fio_file *f)
53cdc686 290{
53cdc686
JA
291 int flags = 0;
292
2fd233b7
JA
293 if (td->odirect)
294 flags |= OS_O_DIRECT;
295 if (td->sync_io)
296 flags |= O_SYNC;
53cdc686 297
2fd233b7
JA
298 if (td_write(td) || td_rw(td)) {
299 flags |= O_RDWR;
53cdc686 300
af52b345 301 if (f->filetype == FIO_TYPE_FILE)
2fd233b7 302 flags |= O_CREAT;
2fd233b7 303
b5af8293 304 f->fd = open(f->file_name, flags, 0600);
2fd233b7 305 } else {
af52b345 306 if (f->filetype == FIO_TYPE_CHAR)
2fd233b7
JA
307 flags |= O_RDWR;
308 else
309 flags |= O_RDONLY;
310
b5af8293 311 f->fd = open(f->file_name, flags);
53cdc686
JA
312 }
313
314 if (f->fd == -1) {
e1161c32
JA
315 int __e = errno;
316
317 td_verror(td, __e, "open");
318 if (__e == EINVAL && td->odirect)
9d80e114 319 log_err("fio: destination does not support O_DIRECT\n");
8ab53873
JA
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);
53cdc686
JA
322 return 1;
323 }
324
b5af8293
JA
325 if (get_file_size(td, f))
326 goto err;
327
0263882a 328 if (td->invalidate_cache && file_invalidate_cache(td, f))
b5af8293
JA
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 }
bdb4e2e9 341 }
53cdc686
JA
342
343 return 0;
b5af8293
JA
344err:
345 close(f->fd);
346 return 1;
347}
348
21972cde
JA
349int open_files(struct thread_data *td)
350{
351 struct fio_file *f;
af52b345
JA
352 unsigned int i;
353 int err = 0;
21972cde
JA
354
355 for_each_file(td, f, i) {
b5af8293 356 err = td_io_open_file(td, f);
21972cde
JA
357 if (err)
358 break;
b5af8293
JA
359
360 if (td->open_files == td->nr_open_files)
361 break;
21972cde
JA
362 }
363
7abf833d
JA
364 if (!err)
365 return 0;
366
bdb4e2e9 367 for_each_file(td, f, i)
b5af8293 368 td_io_close_file(td, f);
7abf833d 369
21972cde
JA
370 return err;
371}
372
53cdc686
JA
373int setup_files(struct thread_data *td)
374{
375 struct fio_file *f;
af52b345
JA
376 unsigned int i;
377 int err;
53cdc686
JA
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
21972cde 389 err = open_files(td);
f1027063
JA
390 if (err)
391 return err;
392
0a7eb121
JA
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
f1027063 400 td->io_size = td->total_file_size;
53cdc686
JA
401 if (td->io_size == 0) {
402 log_err("%s: no io blocks\n", td->name);
e1161c32 403 td_verror(td, EINVAL, "total_file_size");
53cdc686
JA
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
bdb4e2e9 412 for_each_file(td, f, i)
b5af8293 413 td_io_close_file(td, f);
21972cde
JA
414
415 return err;
53cdc686
JA
416}
417
418void close_files(struct thread_data *td)
419{
0ab8db89 420 struct fio_file *f;
af52b345 421 unsigned int i;
53cdc686 422
0ab8db89 423 for_each_file(td, f, i) {
1315a68a 424 if (!f->file_name && (f->flags & FIO_FILE_UNLINK) &&
af52b345 425 f->filetype == FIO_TYPE_FILE) {
132ad46d 426 unlink(f->file_name);
1315a68a 427 free(f->file_name);
132ad46d
JA
428 f->file_name = NULL;
429 }
bdb4e2e9 430
b5af8293 431 td_io_close_file(td, f);
b3dc7f07
JA
432
433 if (f->file_map)
434 free(f->file_map);
53cdc686 435 }
b4a6a59a 436
132ad46d 437 td->filename = NULL;
b4a6a59a
JA
438 td->files = NULL;
439 td->nr_files = 0;
53cdc686 440}
af52b345 441
e3bab463 442static void get_file_type(struct fio_file *f)
af52b345
JA
443{
444 struct stat sb;
445
446 f->filetype = FIO_TYPE_FILE;
447
e3bab463 448 if (!lstat(f->file_name, &sb)) {
96d32d51
JA
449 f->flags |= FIO_FILE_EXISTS;
450
af52b345
JA
451 if (S_ISBLK(sb.st_mode))
452 f->filetype = FIO_TYPE_BD;
453 else if (S_ISCHR(sb.st_mode))
454 f->filetype = FIO_TYPE_CHAR;
96d32d51
JA
455 else {
456 /*
457 * might as well do this here, and save a stat later on
458 */
459 f->real_file_size = sb.st_size;
460 f->file_size = f->real_file_size;
461 }
af52b345
JA
462 }
463}
464
465void add_file(struct thread_data *td, const char *fname)
466{
7b4e4fe5 467 int cur_files = td->files_index;
af52b345
JA
468 struct fio_file *f;
469
470 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
471
472 f = &td->files[cur_files];
473 memset(f, 0, sizeof(*f));
474 f->fd = -1;
cae61953 475 f->file_name = strdup(fname);
af52b345 476
e3bab463 477 get_file_type(f);
af52b345 478
7b4e4fe5 479 td->files_index++;
1549441c
JA
480 if (f->filetype == FIO_TYPE_FILE)
481 td->nr_normal_files++;
af52b345 482}
0ad920e7
JA
483
484void get_file(struct fio_file *f)
485{
486 f->references++;
487}
488
489void put_file(struct thread_data *td, struct fio_file *f)
490{
491 if (!(f->flags & FIO_FILE_OPEN))
492 return;
493
494 assert(f->references);
495 if (--f->references)
496 return;
497
ebb1415f
JA
498 if (should_fsync(td) && td->fsync_on_close)
499 fsync(f->fd);
500
0ad920e7
JA
501 if (td->io_ops->close_file)
502 td->io_ops->close_file(td, f);
503 td->nr_open_files--;
504 f->flags &= ~FIO_FILE_OPEN;
505}
bbf6b540
JA
506
507static int recurse_dir(struct thread_data *td, const char *dirname)
508{
509 struct dirent *dir;
510 int ret = 0;
511 DIR *D;
512
513 D = opendir(dirname);
514 if (!D) {
515 td_verror(td, errno, "opendir");
516 return 1;
517 }
518
519 while ((dir = readdir(D)) != NULL) {
520 char full_path[PATH_MAX];
521 struct stat sb;
522
e85b2b83
JA
523 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
524 continue;
96d32d51 525
bbf6b540
JA
526 sprintf(full_path, "%s/%s", dirname, dir->d_name);
527
528 if (lstat(full_path, &sb) == -1) {
529 if (errno != ENOENT) {
530 td_verror(td, errno, "stat");
531 return 1;
532 }
533 }
534
535 if (S_ISREG(sb.st_mode)) {
536 add_file(td, full_path);
537 td->nr_files++;
538 continue;
539 }
540
bbf6b540
JA
541 if ((ret = recurse_dir(td, full_path)) != 0)
542 break;
543 }
544
545 closedir(D);
546 return ret;
547}
548
549int add_dir_files(struct thread_data *td, const char *path)
550{
551 return recurse_dir(td, path);
552}