Fix bug with file references

This could cause premature exit of fio with an error at the end of
the run.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/io_u.c b/io_u.c
index 99f0728..7fe5242 100644
--- a/io_u.c
+++ b/io_u.c
@@ -265,6 +265,9 @@
 	assert((io_u->flags & IO_U_F_FREE) == 0);
 	io_u->flags |= IO_U_F_FREE;
 
+	if (io_u->file)
+		put_file(td, io_u->file);
+
 	io_u->file = NULL;
 	list_del(&io_u->list);
 	list_add(&io_u->list, &td->io_u_freelist);
@@ -540,12 +543,14 @@
 
 set_file:
 		io_u->file = f;
+		get_file(f);
 
 		if (!fill_io_u(td, io_u))
 			break;
 
 		/*
-		 * No more to do for this file, close it
+		 * td_io_close() does a put_file() as well, so no need to
+		 * do that here.
 		 */
 		io_u->file = NULL;
 		td_io_close_file(td, f);
@@ -627,8 +632,6 @@
 	assert(io_u->flags & IO_U_F_FLIGHT);
 	io_u->flags &= ~IO_U_F_FLIGHT;
 
-	put_file(td, io_u->file);
-
 	if (io_u->ddir == DDIR_SYNC) {
 		td->last_was_sync = 1;
 		return;