Fix problem with too many opened files

If using raw block devices, we would leak the fd from the bdev_size()
function. Also add some debug triggers to catch this in the future.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/filesetup.c b/filesetup.c
index 5792813..15c4316 100644
--- a/filesetup.c
+++ b/filesetup.c
@@ -246,6 +246,7 @@
 	}
 
 	f->real_file_size = bytes;
+	td->io_ops->close_file(td, f);
 	return 0;
 err:
 	td->io_ops->close_file(td, f);
@@ -726,7 +727,9 @@
 			unlink(f->file_name);
 		}
 
-		td_io_close_file(td, f);
+		if (fio_file_open(f))
+			td_io_close_file(td, f);
+
 		remove_file_hash(f);
 
 		sfree(f->file_name);
@@ -847,8 +850,10 @@
 
 	dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
 
-	if (!fio_file_open(f))
+	if (!fio_file_open(f)) {
+		assert(f->fd == -1);
 		return 0;
+	}
 
 	assert(f->references);
 	if (--f->references)
@@ -865,6 +870,7 @@
 
 	td->nr_open_files--;
 	fio_file_clear_open(f);
+	assert(f->fd == -1);
 	return ret;
 }
 
@@ -1004,6 +1010,7 @@
 			log_err("fio: smalloc OOM\n");
 			assert(0);
 		}
+		__f->fd = -1;
 	
 		if (f->file_name) {
 			__f->file_name = smalloc_strdup(f->file_name);