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