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