Make file structures dynamically allocated
[fio.git] / filesetup.c
index 3e11c4c651050889457e37ccca6160204e81d0dd..483226ae56e5d488d4601a14c1ba29b93f91d360 100644 (file)
@@ -209,11 +209,16 @@ int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
        return ret;
 }
 
-void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
+int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f)
 {
+       int ret = 0;
+
        dprint(FD_FILE, "fd close %s\n", f->file_name);
-       close(f->fd);
+       if (close(f->fd) < 0)
+               ret = errno;
+
        f->fd = -1;
+       return ret;
 }
 
 int generic_open_file(struct thread_data *td, struct fio_file *f)
@@ -580,12 +585,14 @@ int add_file(struct thread_data *td, const char *fname)
 
        dprint(FD_FILE, "add file %s\n", fname);
 
-       td->files = realloc(td->files, (cur_files + 1) * sizeof(*f));
-
-       f = &td->files[cur_files];
+       f = malloc(sizeof(*f));
        memset(f, 0, sizeof(*f));
        f->fd = -1;
 
+       td->files = realloc(td->files, (cur_files + 1) * sizeof(f));
+
+       td->files[cur_files] = f;
+
        /*
         * init function, io engine may not be loaded yet
         */
@@ -614,25 +621,28 @@ void get_file(struct fio_file *f)
        f->references++;
 }
 
-void put_file(struct thread_data *td, struct fio_file *f)
+int put_file(struct thread_data *td, struct fio_file *f)
 {
+       int ret = 0;
+
        dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references);
 
        if (!(f->flags & FIO_FILE_OPEN))
-               return;
+               return 0;
 
        assert(f->references);
        if (--f->references)
-               return;
+               return 0;
 
        if (should_fsync(td) && td->o.fsync_on_close)
                fsync(f->fd);
 
        if (td->io_ops->close_file)
-               td->io_ops->close_file(td, f);
+               ret = td->io_ops->close_file(td, f);
 
        td->nr_open_files--;
        f->flags &= ~FIO_FILE_OPEN;
+       return ret;
 }
 
 static int recurse_dir(struct thread_data *td, const char *dirname)