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");
53 if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
54 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->flags & FIO_SIZE_KNOWN)
144 if (f->filetype == FIO_TYPE_FILE)
145 ret = file_size(td, f);
146 else if (f->filetype == FIO_TYPE_BD)
147 ret = bdev_size(td, f);
149 f->real_file_size = -1;
154 if (f->file_offset > f->real_file_size) {
155 log_err("%s: offset extends end (%Lu > %Lu)\n", td->o.name, f->file_offset, f->real_file_size);
159 f->flags |= FIO_SIZE_KNOWN;
163 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
171 * FIXME: add blockdev flushing too
174 ret = madvise(f->mmap, f->io_size, MADV_DONTNEED);
175 else if (f->filetype == FIO_TYPE_FILE)
176 ret = fadvise(f->fd, f->file_offset, f->io_size, POSIX_FADV_DONTNEED);
177 else if (f->filetype == FIO_TYPE_BD) {
178 ret = blockdev_invalidate_cache(f->fd);
179 if (ret < 0 && errno == EACCES && geteuid()) {
180 log_err("fio: only root may flush block devices. Cache flush bypassed!\n");
183 } else if (f->filetype == FIO_TYPE_CHAR)
187 td_verror(td, errno, "invalidate_cache");
194 void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
200 int generic_open_file(struct thread_data *td, struct fio_file *f)
205 flags |= OS_O_DIRECT;
209 if (td_write(td) || td_rw(td)) {
212 if (f->filetype == FIO_TYPE_FILE)
215 f->fd = open(f->file_name, flags, 0600);
217 if (f->filetype == FIO_TYPE_CHAR)
222 f->fd = open(f->file_name, flags);
226 char buf[FIO_VERROR_SIZE];
229 snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
231 td_verror(td, __e, buf);
234 if (get_file_size(td, f))
243 int open_files(struct thread_data *td)
249 for_each_file(td, f, i) {
250 err = td_io_open_file(td, f);
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;
261 if (td->o.open_files == td->nr_open_files)
268 for_each_file(td, f, i)
269 td_io_close_file(td, f);
275 * open/close all files, so that ->real_file_size gets set
277 static void get_file_sizes(struct thread_data *td)
282 for_each_file(td, f, i) {
283 if (td->io_ops->open_file(td, f))
286 td->io_ops->close_file(td, f);
288 if (f->real_file_size == -1ULL && td->o.size)
289 f->real_file_size = td->o.size / td->o.nr_files;
294 * Open the files and setup files sizes, creating files if necessary.
296 int setup_files(struct thread_data *td)
298 unsigned long long total_size, extend_size;
301 int err = 0, need_extend;
304 * if ioengine defines a setup() method, it's responsible for
305 * opening the files and setting f->real_file_size to indicate
306 * the valid range for that file.
308 if (td->io_ops->setup)
309 err = td->io_ops->setup(td);
317 * check sizes. if the files/devices do not exist and the size
318 * isn't passed to fio, abort.
321 for_each_file(td, f, i) {
322 if (f->real_file_size == -1ULL)
325 total_size += f->real_file_size;
329 * device/file sizes are zero and no size given, punt
331 if ((!total_size || total_size == -1ULL) && !td->o.size) {
332 log_err("%s: you need to specify size=\n", td->o.name);
333 td_verror(td, EINVAL, "total_file_size");
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.
342 extend_size = total_size = 0;
344 for_each_file(td, f, i) {
345 if (!td->o.file_size_low) {
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.
351 f->io_size = td->o.size / td->o.nr_files;
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) {
357 * file size given. if it's fixed, use that. if it's a
358 * range, generate a random size in-between.
360 if (td->o.file_size_low == td->o.file_size_high)
361 f->io_size = td->o.file_size_low;
363 f->io_size = get_rand_file_size(td);
365 f->io_size = f->real_file_size;
367 if (f->io_size == -1ULL)
370 total_size += f->io_size;
372 if (f->filetype == FIO_TYPE_FILE &&
373 f->io_size > f->real_file_size &&
374 !(td->io_ops->flags & FIO_DISKLESSIO)) {
376 extend_size += f->io_size;
377 f->flags |= FIO_FILE_EXTEND;
382 td->o.size = total_size;
385 * See if we need to extend some files
389 log_info("%s: Laying out IO file(s) (%u files / %LuMiB)\n",
390 td->o.name, need_extend, extend_size >> 20);
392 for_each_file(td, f, i) {
393 if (!(f->flags & FIO_FILE_EXTEND))
396 assert(f->filetype == FIO_TYPE_FILE);
397 f->flags &= ~FIO_FILE_EXTEND;
398 f->real_file_size = f->io_size;
399 err = extend_file(td, f);
409 if (!td->o.zone_size)
410 td->o.zone_size = td->o.size;
412 td->total_io_size = td->o.size * td->o.loops;
416 int init_random_map(struct thread_data *td)
418 int num_maps, blocks;
422 if (td->o.norandommap)
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));
430 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
433 f->num_maps = num_maps;
434 memset(f->file_map, 0, num_maps * sizeof(long));
440 void close_files(struct thread_data *td)
445 for_each_file(td, f, i) {
446 if ((f->flags & FIO_FILE_UNLINK) &&
447 f->filetype == FIO_TYPE_FILE)
448 unlink(f->file_name);
450 td_io_close_file(td, f);
461 td->o.filename = NULL;
467 static void get_file_type(struct fio_file *f)
471 f->filetype = FIO_TYPE_FILE;
473 if (!lstat(f->file_name, &sb)) {
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;
481 void add_file(struct thread_data *td, const char *fname)
483 int cur_files = td->files_index;
484 char file_name[PATH_MAX];
488 td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
490 f = &td->files[cur_files];
491 memset(f, 0, sizeof(*f));
495 len = sprintf(file_name, "%s/", td->o.directory);
497 sprintf(file_name + len, "%s", fname);
498 f->file_name = strdup(file_name);
503 if (f->filetype == FIO_TYPE_FILE)
504 td->nr_normal_files++;
507 void get_file(struct fio_file *f)
512 void put_file(struct thread_data *td, struct fio_file *f)
514 if (!(f->flags & FIO_FILE_OPEN))
517 assert(f->references);
521 if (should_fsync(td) && td->o.fsync_on_close)
524 if (td->io_ops->close_file)
525 td->io_ops->close_file(td, f);
528 f->flags &= ~FIO_FILE_OPEN;
531 static int recurse_dir(struct thread_data *td, const char *dirname)
537 D = opendir(dirname);
539 char buf[FIO_VERROR_SIZE];
541 snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
542 td_verror(td, errno, buf);
546 while ((dir = readdir(D)) != NULL) {
547 char full_path[PATH_MAX];
550 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
553 sprintf(full_path, "%s/%s", dirname, dir->d_name);
555 if (lstat(full_path, &sb) == -1) {
556 if (errno != ENOENT) {
557 td_verror(td, errno, "stat");
562 if (S_ISREG(sb.st_mode)) {
563 add_file(td, full_path);
567 if (!S_ISDIR(sb.st_mode))
570 if ((ret = recurse_dir(td, full_path)) != 0)
578 int add_dir_files(struct thread_data *td, const char *path)
580 int ret = recurse_dir(td, path);
583 log_info("fio: opendir added %d files\n", td->o.nr_files);
588 void dup_files(struct thread_data *td, struct thread_data *org)
597 bytes = org->files_index * sizeof(*f);
598 td->files = malloc(bytes);
599 memcpy(td->files, org->files, bytes);
601 for_each_file(td, f, i) {
603 f->file_name = strdup(f->file_name);