Fix bug in file selection

We need to check both the OPEN and CLOSING flag, since CLOSE will not
be set until the file has really been closed.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/io_u.c b/io_u.c
index 0d55c09..efe8389 100644
--- a/io_u.c
+++ b/io_u.c
@@ -194,13 +194,20 @@
 	if (td_random(td) && (td->o.ddir_nr && !--td->ddir_nr)) {
 		td->ddir_nr = td->o.ddir_nr;
 
-		if (get_next_rand_offset(td, f, ddir, &b))
+		if (get_next_rand_offset(td, f, ddir, &b)) {
+			dprint(FD_IO, "%s: getting rand offset failed\n",
+				f->file_name);
 			return 1;
+		}
 	} else {
 		if (f->last_pos >= f->real_file_size) {
 			if (!td_random(td) ||
-			     get_next_rand_offset(td, f, ddir, &b))
+			     get_next_rand_offset(td, f, ddir, &b)) {
+				dprint(FD_IO, "%s: pos %llu > size %llu\n",
+						f->file_name, f->last_pos,
+						f->real_file_size);
 				return 1;
+			}
 		} else
 			b = (f->last_pos - f->file_offset) / td->o.min_bs[ddir];
 	}
@@ -681,7 +688,7 @@
 	}
 
 	f = td->file_service_file;
-	if (f && (f->flags & FIO_FILE_OPEN)) {
+	if (f && (f->flags & FIO_FILE_OPEN) && !(f->flags & FIO_FILE_CLOSING)) {
 		if (td->o.file_service_type == FIO_FSERVICE_SEQ)
 			goto out;
 		if (td->file_service_left--)
@@ -697,7 +704,7 @@
 	td->file_service_file = f;
 	td->file_service_left = td->file_service_nr - 1;
 out:
-	dprint(FD_FILE, "get_next_file: %p\n", f);
+	dprint(FD_FILE, "get_next_file: %p [%s]\n", f, f->file_name);
 	return f;
 }
 
@@ -746,6 +753,7 @@
 		 * td_io_close() does a put_file() as well, so no need to
 		 * do that here.
 		 */
+		dprint(FD_FILE, "%s: is done\n", f->file_name);
 		io_u->file = NULL;
 		td_io_close_file(td, f);
 		f->flags |= FIO_FILE_DONE;