Revamp file open/close handling

Some IO engines need special handling for opening and closing
files, and this has complicated the fio filesetup.c file. Instead
have the io engine provide hooks for file open/close. This also
greatly cleans up the flags (we can get rid of SELFOPEN and MMAPIO)
and moves private knowledge into the engines where it belongs.

This potentially destabilizes fio somewhat, so testing is needed.
The new openfiles option that is introduced with this change isn't
verified working yet, hence it isn't documented.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/io_u.c b/io_u.c
index 79a0ce8..0c604df 100644
--- a/io_u.c
+++ b/io_u.c
@@ -107,7 +107,7 @@
 
 	if (td_random(td)) {
 		unsigned long long max_blocks = f->file_size / td->min_bs[ddir];
-		int loops = 5;
+		int loops = 2;
 
 		do {
 			r = os_random_long(&td->random_state);
@@ -335,7 +335,7 @@
 	do {
 		long r = os_random_long(&td->next_file_state);
 
-		fileno = (unsigned int) ((double) (td->nr_files - 1) * r / (RAND_MAX + 1.0));
+		fileno = (unsigned int) ((double) (td->open_files - 1) * r / (RAND_MAX + 1.0));
 		f = &td->files[fileno];
 		if (f->fd != -1)
 			return f;
@@ -354,7 +354,7 @@
 		f = &td->files[td->next_file];
 
 		td->next_file++;
-		if (td->next_file >= td->nr_files)
+		if (td->next_file >= td->open_files)
 			td->next_file = 0;
 
 		if (f->fd != -1)
@@ -441,7 +441,15 @@
 		 * No more to do for this file, close it
 		 */
 		io_u->file = NULL;
-		close_file(td, f);
+		td_io_close_file(td, f);
+
+		/*
+		 * probably not the right place to do this, but see
+		 * if we need to open a new file
+		 */
+		if (td->nr_open_files < td->nr_files &&
+		    td->open_files != td->nr_files)
+			reopen_file(td, f);
 	} while (1);
 
 	if (td->zone_bytes >= td->zone_size) {