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