Better management of open files
[fio.git] / io_u.c
diff --git a/io_u.c b/io_u.c
index 426a5f8a38b49b1f2e7a1b18de199ddccf53a536..978771e1370378ada310bc303b686f20fd7a2763 100644 (file)
--- a/io_u.c
+++ b/io_u.c
@@ -365,6 +365,17 @@ static struct fio_file *get_next_file_rr(struct thread_data *td)
        return f;
 }
 
+static struct fio_file *get_next_file(struct thread_data *td)
+{
+       if (!td->nr_open_files)
+               return NULL;
+
+       if (td->file_service_type == FIO_FSERVICE_RR)
+               return get_next_file_rr(td);
+       else
+               return get_next_file_rand(td);
+}
+
 struct io_u *__get_io_u(struct thread_data *td)
 {
        struct io_u *io_u = NULL;
@@ -413,23 +424,24 @@ struct io_u *get_io_u(struct thread_data *td)
        if (io_u->file)
                goto out;
 
-       if (td->file_service_type == FIO_FSERVICE_RR)
-               f = get_next_file_rr(td);
-       else
-               f = get_next_file_rand(td);
-
-       if (!f) {
-               put_io_u(td, io_u);
-               return NULL;
-       }
+       do {
+               f = get_next_file(td);
+               if (!f) {
+                       put_io_u(td, io_u);
+                       return NULL;
+               }
 
-       io_u->file = f;
+               io_u->file = f;
 
+               if (!fill_io_u(td, io_u))
+                       break;
 
-       if (fill_io_u(td, io_u)) {
-               put_io_u(td, io_u);
-               return NULL;
-       }
+               /*
+                * No more to do for this file, close it
+                */
+               io_u->file = NULL;
+               close_file(td, f);
+       } while (1);
 
        if (td->zone_bytes >= td->zone_size) {
                td->zone_bytes = 0;