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