+ td->o.nr_files = 0;
+}
+
+static void get_file_type(struct fio_file *f)
+{
+ struct stat sb;
+
+ if (!strcmp(f->file_name, "-"))
+ f->filetype = FIO_TYPE_PIPE;
+ else
+ f->filetype = FIO_TYPE_FILE;
+
+ if (!lstat(f->file_name, &sb)) {
+ if (S_ISBLK(sb.st_mode))
+ f->filetype = FIO_TYPE_BD;
+ else if (S_ISCHR(sb.st_mode))
+ f->filetype = FIO_TYPE_CHAR;
+ else if (S_ISFIFO(sb.st_mode))
+ f->filetype = FIO_TYPE_PIPE;
+ }
+}
+
+int add_file(struct thread_data *td, const char *fname)
+{
+ int cur_files = td->files_index;
+ char file_name[PATH_MAX];
+ struct fio_file *f;
+ int len = 0;
+
+ td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
+
+ f = &td->files[cur_files];
+ memset(f, 0, sizeof(*f));
+ f->fd = -1;
+
+ /*
+ * init function, io engine may not be loaded yet
+ */
+ if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO))
+ f->real_file_size = -1ULL;
+
+ if (td->o.directory)
+ len = sprintf(file_name, "%s/", td->o.directory);
+
+ sprintf(file_name + len, "%s", fname);
+ f->file_name = strdup(file_name);
+
+ get_file_type(f);
+
+ td->files_index++;
+ if (f->filetype == FIO_TYPE_FILE)
+ td->nr_normal_files++;
+
+ return cur_files;
+}
+
+void get_file(struct fio_file *f)
+{
+ assert(f->flags & FIO_FILE_OPEN);
+ f->references++;
+}
+
+void put_file(struct thread_data *td, struct fio_file *f)
+{
+ if (!(f->flags & FIO_FILE_OPEN))
+ return;
+
+ assert(f->references);
+ if (--f->references)
+ return;
+
+ if (should_fsync(td) && td->o.fsync_on_close)
+ fsync(f->fd);
+
+ if (td->io_ops->close_file)
+ td->io_ops->close_file(td, f);
+
+ td->nr_open_files--;
+ f->flags &= ~FIO_FILE_OPEN;
+}
+
+static int recurse_dir(struct thread_data *td, const char *dirname)
+{
+ struct dirent *dir;
+ int ret = 0;
+ DIR *D;
+
+ D = opendir(dirname);
+ if (!D) {
+ char buf[FIO_VERROR_SIZE];
+
+ snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
+ td_verror(td, errno, buf);
+ return 1;
+ }
+
+ while ((dir = readdir(D)) != NULL) {
+ char full_path[PATH_MAX];
+ struct stat sb;
+
+ if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
+ continue;
+
+ sprintf(full_path, "%s/%s", dirname, dir->d_name);
+
+ if (lstat(full_path, &sb) == -1) {
+ if (errno != ENOENT) {
+ td_verror(td, errno, "stat");
+ return 1;
+ }
+ }
+
+ if (S_ISREG(sb.st_mode)) {
+ add_file(td, full_path);
+ td->o.nr_files++;
+ continue;
+ }
+ if (!S_ISDIR(sb.st_mode))
+ continue;
+
+ if ((ret = recurse_dir(td, full_path)) != 0)
+ break;
+ }
+
+ closedir(D);
+ return ret;
+}
+
+int add_dir_files(struct thread_data *td, const char *path)
+{
+ int ret = recurse_dir(td, path);
+
+ if (!ret)
+ log_info("fio: opendir added %d files\n", td->o.nr_files);
+
+ return ret;
+}
+
+void dup_files(struct thread_data *td, struct thread_data *org)
+{
+ struct fio_file *f;
+ unsigned int i;
+ size_t bytes;
+
+ if (!org->files)
+ return;
+
+ bytes = org->files_index * sizeof(*f);
+ td->files = malloc(bytes);
+ memcpy(td->files, org->files, bytes);
+
+ for_each_file(td, f, i) {
+ if (f->file_name)
+ f->file_name = strdup(f->file_name);
+ }
+}
+
+/*
+ * Returns the index that matches the filename, or -1 if not there
+ */
+int get_fileno(struct thread_data *td, const char *fname)
+{
+ struct fio_file *f;
+ unsigned int i;
+
+ for_each_file(td, f, i)
+ if (!strcmp(f->file_name, fname))
+ return i;
+
+ return -1;
+}
+
+/*
+ * For log usage, where we add/open/close files automatically
+ */
+void free_release_files(struct thread_data *td)
+{
+ close_files(td);
+ td->files_index = 0;
+ td->nr_normal_files = 0;