+ 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;
+}
+
+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);