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