+ 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 (!stat(f->file_name, &sb)) {
+ /* \\.\ is the device namespace in Windows, where every file is
+ * a block device */
+ if (S_ISBLK(sb.st_mode) || strncmp(f->file_name, "\\\\.\\", 4) == 0)
+ 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;
+
+ dprint(FD_FILE, "add file %s\n", fname);
+
+ f = smalloc(sizeof(*f));
+ if (!f) {
+ log_err("fio: smalloc OOM\n");
+ assert(0);
+ }
+
+ f->fd = -1;
+ fio_file_reset(f);
+
+ if (td->files_size <= td->files_index) {
+ int new_size = td->o.nr_files + 1;
+
+ dprint(FD_FILE, "resize file array to %d files\n", new_size);
+
+ td->files = realloc(td->files, new_size * sizeof(f));
+ td->files_size = new_size;
+ }
+ td->files[cur_files] = f;
+
+ /*
+ * 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 = smalloc_strdup(file_name);
+ if (!f->file_name) {
+ log_err("fio: smalloc OOM\n");
+ assert(0);
+ }
+
+ get_file_type(f);
+
+ switch (td->o.file_lock_mode) {
+ case FILE_LOCK_NONE:
+ break;
+ case FILE_LOCK_READWRITE:
+ f->lock = fio_mutex_rw_init();
+ break;
+ case FILE_LOCK_EXCLUSIVE:
+ f->lock = fio_mutex_init(1);
+ break;
+ default:
+ log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode);
+ assert(0);
+ }
+
+ td->files_index++;
+ if (f->filetype == FIO_TYPE_FILE)
+ td->nr_normal_files++;
+
+ dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name,
+ cur_files);
+
+ return cur_files;
+}
+
+int add_file_exclusive(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 add_file(td, fname);
+}
+
+void get_file(struct fio_file *f)
+{
+ dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references);
+ assert(fio_file_open(f));
+ f->references++;
+}
+
+int put_file(struct thread_data *td, struct fio_file *f)
+{
+ int f_ret = 0, ret = 0;
+
+ dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
+
+ if (!fio_file_open(f)) {
+ assert(f->fd == -1);
+ return 0;
+ }
+
+ assert(f->references);
+ if (--f->references)
+ return 0;
+
+ if (should_fsync(td) && td->o.fsync_on_close)
+ f_ret = fsync(f->fd);
+
+ if (td->io_ops->close_file)
+ ret = td->io_ops->close_file(td, f);
+
+ if (!ret)
+ ret = f_ret;
+
+ td->nr_open_files--;
+ fio_file_clear_open(f);
+ assert(f->fd == -1);
+ return ret;
+}
+
+void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir)
+{
+ if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
+ return;
+
+ if (f->lock_owner == td && f->lock_batch--)
+ return;
+
+ if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
+ if (ddir == DDIR_READ)
+ fio_mutex_down_read(f->lock);
+ else
+ fio_mutex_down_write(f->lock);
+ } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE)
+ fio_mutex_down(f->lock);
+
+ f->lock_owner = td;
+ f->lock_batch = td->o.lockfile_batch;
+ f->lock_ddir = ddir;
+}
+
+void unlock_file(struct thread_data *td, struct fio_file *f)
+{
+ if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE)
+ return;
+ if (f->lock_batch)
+ return;
+
+ if (td->o.file_lock_mode == FILE_LOCK_READWRITE) {
+ const int is_read = f->lock_ddir == DDIR_READ;
+ int val = fio_mutex_getval(f->lock);
+
+ if ((is_read && val == 1) || (!is_read && val == -1))
+ f->lock_owner = NULL;
+
+ if (is_read)
+ fio_mutex_up_read(f->lock);
+ else
+ fio_mutex_up_write(f->lock);
+ } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) {
+ int val = fio_mutex_getval(f->lock);
+
+ if (val == 0)
+ f->lock_owner = NULL;
+
+ fio_mutex_up(f->lock);
+ }
+}
+
+void unlock_file_all(struct thread_data *td, struct fio_file *f)
+{
+ if (f->lock_owner != td)
+ return;
+
+ f->lock_batch = 0;
+ unlock_file(td, f);
+}
+
+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;
+
+ ret = recurse_dir(td, full_path);
+ if (ret)
+ 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;
+
+ dprint(FD_FILE, "dup files: %d\n", org->files_index);
+
+ if (!org->files)
+ return;
+
+ td->files = malloc(org->files_index * sizeof(f));
+
+ for_each_file(org, f, i) {
+ struct fio_file *__f;
+
+ __f = smalloc(sizeof(*__f));
+ if (!__f) {
+ log_err("fio: smalloc OOM\n");
+ assert(0);
+ }
+ __f->fd = -1;
+ fio_file_reset(__f);
+
+ if (f->file_name) {
+ __f->file_name = smalloc_strdup(f->file_name);
+ if (!__f->file_name) {
+ log_err("fio: smalloc OOM\n");
+ assert(0);
+ }
+
+ __f->filetype = f->filetype;
+ }
+
+ td->files[i] = __f;
+ }
+}
+
+/*
+ * 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;