13 static int extend_file(struct thread_data *td, struct fio_file *f)
15 int r, new_layout = 0, flags;
16 unsigned long long left;
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
25 if (td_read(td) || (td_write(td) && td->o.overwrite))
28 if (new_layout && (f->flags & FIO_FILE_EXISTS)) {
29 if (unlink(f->file_name) < 0) {
30 td_verror(td, errno, "unlink");
35 flags = O_WRONLY | O_CREAT;
39 f->fd = open(f->file_name, flags, 0644);
41 td_verror(td, errno, "open");
45 if (ftruncate(f->fd, f->real_file_size) == -1) {
46 td_verror(td, errno, "ftruncate");
50 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
51 td_verror(td, errno, "posix_fallocate");
58 b = malloc(td->o.max_bs[DDIR_WRITE]);
59 memset(b, 0, td->o.max_bs[DDIR_WRITE]);
61 left = f->real_file_size;
62 while (left && !td->terminate) {
63 bs = td->o.max_bs[DDIR_WRITE];
67 r = write(f->fd, b, bs);
74 td_verror(td, errno, "write");
76 td_verror(td, EIO, "write");
84 else if (td->o.create_fsync)
98 static unsigned long long get_rand_file_size(struct thread_data *td)
100 unsigned long long ret;
103 r = os_random_long(&td->file_size_state);
104 ret = td->o.file_size_low + (unsigned long long) ((double) td->o.file_size_high * (r / (RAND_MAX + 1.0)));
105 ret -= (ret % td->o.rw_min_bs);
109 static int file_size(struct thread_data *td, struct fio_file *f)
113 if (fstat(f->fd, &st) == -1) {
114 td_verror(td, errno, "fstat");
118 f->real_file_size = st.st_size;
122 static int bdev_size(struct thread_data *td, struct fio_file *f)
124 unsigned long long bytes;
127 r = blockdev_size(f->fd, &bytes);
129 td_verror(td, r, "blockdev_size");
133 f->real_file_size = bytes;
137 static int get_file_size(struct thread_data *td, struct fio_file *f)
141 if (f->filetype == FIO_TYPE_FILE)
142 ret = file_size(td, f);
143 else if (f->filetype == FIO_TYPE_BD)
144 ret = bdev_size(td, f);
146 f->real_file_size = -1;
151 if (f->file_offset > f->real_file_size) {
152 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
159 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
167 * FIXME: add blockdev flushing too
170 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
171 else if (f->filetype == FIO_TYPE_FILE)
172 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
173 else if (f->filetype == FIO_TYPE_BD) {
174 ret = blockdev_invalidate_cache(f->fd);
175 if (ret < 0 && errno == EACCES && geteuid()) {
176 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
179 } else if (f->filetype == FIO_TYPE_CHAR)
183 td_verror(td, errno, "invalidate_cache");
190 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
196 int generic_open_file(struct thread_data *td, struct fio_file *f)
201 flags |= OS_O_DIRECT;
205 if (td_write(td) || td_rw(td)) {
208 if (f->filetype == FIO_TYPE_FILE)
211 f->fd = open(f->file_name, flags, 0600);
213 if (f->filetype == FIO_TYPE_CHAR)
218 f->fd = open(f->file_name, flags);
222 char buf[FIO_VERROR_SIZE];
225 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
227 td_verror(td, __e, buf);
230 if (get_file_size(td, f))
239 int open_files(struct thread_data *td)
245 for_each_file(td, f, i) {
246 err = td_io_open_file(td, f);
250 if (td->o.open_files == td->nr_open_files)
257 for_each_file(td, f, i)
258 td_io_close_file(td, f);
264 * open/close all files, so that ->real_file_size gets set
266 static void get_file_sizes(struct thread_data *td)
271 for_each_file(td, f, i) {
272 if (td->io_ops->open_file(td, f)) {
274 memset(td->verror, 0, sizeof(td->verror));
276 td->io_ops->close_file(td, f);
281 * Open the files and setup files sizes, creating files if necessary.
283 int setup_files(struct thread_data *td)
285 unsigned long long total_size, extend_size;
288 int err = 0, need_extend;
291 * if ioengine defines a setup() method, it's responsible for
292 * opening the files and setting f->real_file_size to indicate
293 * the valid range for that file.
295 if (td->io_ops->setup)
296 err = td->io_ops->setup(td);
304 * check sizes. if the files/devices do not exist and the size
305 * isn't passed to fio, abort.
308 for_each_file(td, f, i) {
309 if (f->real_file_size == -1ULL)
312 total_size += f->real_file_size;
316 * device/file sizes are zero and no size given, punt
318 if (!total_size && !td->o.size) {
319 log_err("%s: you need to specify size=\n", td->o.name);
320 td_verror(td, EINVAL, "total_file_size");
325 * now file sizes are known, so we can set ->io_size. if size= is
326 * not given, ->io_size is just equal to ->real_file_size. if size
327 * is given, ->io_size is size / nr_files.
329 extend_size = total_size = 0;
331 for_each_file(td, f, i) {
332 if (!td->o.file_size_low) {
334 * no file size range given, file size is equal to
335 * total size divided by number of files. if that is
336 * zero, set it to the real file size.
338 f->io_size = td->o.size / td->o.nr_files;
340 f->io_size = f->real_file_size;
341 } else if (f->real_file_size < td->o.file_size_low ||
342 f->real_file_size > td->o.file_size_high) {
344 * file size given. if it's fixed, use that. if it's a
345 * range, generate a random size in-between.
347 if (td->o.file_size_low == td->o.file_size_high)
348 f->io_size = td->o.file_size_low;
350 f->io_size = get_rand_file_size(td);
352 f->io_size = f->real_file_size;
354 if (f->io_size == -1ULL)
357 total_size += f->io_size;
359 if (f->filetype == FIO_TYPE_FILE &&
360 f->io_size > f->real_file_size &&
361 !(td->io_ops->flags & FIO_DISKLESSIO)) {
363 extend_size += f->io_size;
364 f->flags |= FIO_FILE_EXTEND;
369 td->o.size = total_size;
372 * See if we need to extend some files
376 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
377 td->o.name, need_extend, extend_size >> 20);
379 for_each_file(td, f, i) {
380 if (!(f->flags & FIO_FILE_EXTEND))
383 f->flags &= ~FIO_FILE_EXTEND;
384 f->real_file_size = f->io_size;
385 err = extend_file(td, f);
395 if (!td->o.zone_size)
396 td->o.zone_size = td->o.size;
398 td->total_io_size = td->o.size * td->o.loops;
402 int init_random_map(struct thread_data *td)
404 int num_maps, blocks;
408 if (td->o.norandommap)
411 for_each_file(td, f, i) {
412 blocks = (f->real_file_size + td->o.rw_min_bs - 1) / td->o.rw_min_bs;
413 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
414 f->file_map = malloc(num_maps * sizeof(long));
416 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
419 f->num_maps = num_maps;
420 memset(f->file_map, 0, num_maps * sizeof(long));
426 void close_files(struct thread_data *td)
431 for_each_file(td, f, i) {
432 if ((f->flags & FIO_FILE_UNLINK) &&
433 f->filetype == FIO_TYPE_FILE)
434 unlink(f->file_name);
436 td_io_close_file(td, f);
447 td->o.filename = NULL;
453 static void get_file_type(struct fio_file *f)
457 f->filetype = FIO_TYPE_FILE;
459 if (!lstat(f->file_name, &sb)) {
460 if (S_ISBLK(sb.st_mode))
461 f->filetype = FIO_TYPE_BD;
462 else if (S_ISCHR(sb.st_mode))
463 f->filetype = FIO_TYPE_CHAR;
467 void add_file(struct thread_data *td, const char *fname)
469 int cur_files = td->files_index;
470 char file_name[PATH_MAX];
474 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
476 f = &td->files[cur_files];
477 memset(f, 0, sizeof(*f));
481 len = sprintf(file_name, "%s/", td->o.directory);
483 sprintf(file_name + len, "%s", fname);
484 f->file_name = strdup(file_name);
489 if (f->filetype == FIO_TYPE_FILE)
490 td->nr_normal_files++;
493 void get_file(struct fio_file *f)
498 void put_file(struct thread_data *td, struct fio_file *f)
500 if (!(f->flags & FIO_FILE_OPEN))
503 assert(f->references);
507 if (should_fsync(td) && td->o.fsync_on_close)
510 if (td->io_ops->close_file)
511 td->io_ops->close_file(td, f);
513 f->flags &= ~FIO_FILE_OPEN;
516 static int recurse_dir(struct thread_data *td, const char *dirname)
522 D = opendir(dirname);
524 char buf[FIO_VERROR_SIZE];
526 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
527 td_verror(td, errno, buf);
531 while ((dir = readdir(D)) != NULL) {
532 char full_path[PATH_MAX];
535 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
538 sprintf(full_path, "%s/%s", dirname, dir->d_name);
540 if (lstat(full_path, &sb) == -1) {
541 if (errno != ENOENT) {
542 td_verror(td, errno, "stat");
547 if (S_ISREG(sb.st_mode)) {
548 add_file(td, full_path);
552 if (!S_ISDIR(sb.st_mode))
555 if ((ret = recurse_dir(td, full_path)) != 0)
563 int add_dir_files(struct thread_data *td, const char *path)
565 int ret = recurse_dir(td, path);
568 log_info("fio: opendir added %d files\n", td->o.nr_files);
573 void dup_files(struct thread_data *td, struct thread_data *org)
582 bytes = org->files_index * sizeof(*f);
583 td->files = malloc(bytes);
584 memcpy(td->files, org->files, bytes);
586 for_each_file(td, f, i) {
588 f->file_name = strdup(f->file_name);