Add file reference counting

We must not close a file, while io is in progress to it. That
will make the queuing engines barf.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/filesetup.c b/filesetup.c
index f6e9974..f3fccdb 100644
--- a/filesetup.c
+++ b/filesetup.c
@@ -445,3 +445,23 @@
 	td->open_files++;
 	td->nr_uniq_files = td->open_files;
 }
+
+void get_file(struct fio_file *f)
+{
+	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 (td->io_ops->close_file)
+		td->io_ops->close_file(td, f);
+	td->nr_open_files--;
+	f->flags &= ~FIO_FILE_OPEN;
+}