+ 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(f->flags & FIO_FILE_OPEN);
+ 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 (!(f->flags & FIO_FILE_OPEN))
+ 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--;
+ f->flags &= ~FIO_FILE_OPEN;
+ 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);