Merge branch 'master' into gfio
[fio.git] / filesetup.c
index 0c104ed748186d5343a210e7a5d9786702943876..3054d9dab4803271de2bbee3369ac9ef66d1133d 100644 (file)
@@ -15,7 +15,7 @@
 #include "hash.h"
 #include "lib/axmap.h"
 
-#ifdef FIO_HAVE_LINUX_FALLOCATE
+#ifdef CONFIG_LINUX_FALLOCATE
 #include <linux/falloc.h>
 #endif
 
@@ -72,7 +72,7 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                return 1;
        }
 
-#ifdef FIO_HAVE_FALLOCATE
+#ifdef CONFIG_POSIX_FALLOCATE
        if (!td->o.fill_device) {
                switch (td->o.fallocate_mode) {
                case FIO_FALLOCATE_NONE:
@@ -87,7 +87,7 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                                                strerror(r));
                        }
                        break;
-#ifdef FIO_HAVE_LINUX_FALLOCATE
+#ifdef CONFIG_LINUX_FALLOCATE
                case FIO_FALLOCATE_KEEP_SIZE:
                        dprint(FD_FILE,
                                "fallocate(FALLOC_FL_KEEP_SIZE) "
@@ -100,14 +100,14 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                                td_verror(td, errno, "fallocate");
 
                        break;
-#endif /* FIO_HAVE_LINUX_FALLOCATE */
+#endif /* CONFIG_LINUX_FALLOCATE */
                default:
                        log_err("fio: unknown fallocate mode: %d\n",
                                td->o.fallocate_mode);
                        assert(0);
                }
        }
-#endif /* FIO_HAVE_FALLOCATE */
+#endif /* CONFIG_POSIX_FALLOCATE */
 
        if (!new_layout)
                goto done;
@@ -563,7 +563,7 @@ open_again:
                if (__e == EMFILE && file_close_shadow_fds(td))
                        goto open_again;
 
-               snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name);
+               snprintf(buf, sizeof(buf), "open(%s)", f->file_name);
 
                if (__e == EINVAL && (flags & OS_O_DIRECT)) {
                        log_err("fio: looks like your file system does not " \
@@ -956,7 +956,9 @@ int init_random_map(struct thread_data *td)
                return 0;
 
        for_each_file(td, f, i) {
-               blocks = (f->real_file_size + td->o.rw_min_bs - 1) /
+               uint64_t file_size = min(f->real_file_size, f->io_size);
+
+               blocks = (file_size + td->o.rw_min_bs - 1) /
                                (unsigned long long) td->o.rw_min_bs;
                if (td->o.random_generator == FIO_RAND_GEN_LFSR) {
                        unsigned long seed;
@@ -1071,14 +1073,18 @@ int add_file(struct thread_data *td, const char *fname)
 
        f->fd = -1;
        f->shadow_fd = -1;
-       fio_file_reset(f);
+       fio_file_reset(td, f);
 
        if (td->files_size <= td->files_index) {
-               int new_size = td->o.nr_files + 1;
+               unsigned int new_size = td->o.nr_files + 1;
 
                dprint(FD_FILE, "resize file array to %d files\n", new_size);
 
                td->files = realloc(td->files, new_size * sizeof(f));
+               if (td->files == NULL) {
+                       log_err("fio: realloc OOM\n");
+                       assert(0);
+               }
                td->files_size = new_size;
        }
        td->files[cur_files] = f;
@@ -1244,7 +1250,7 @@ static int recurse_dir(struct thread_data *td, const char *dirname)
        if (!D) {
                char buf[FIO_VERROR_SIZE];
 
-               snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname);
+               snprintf(buf, FIO_VERROR_SIZE, "opendir(%s)", dirname);
                td_verror(td, errno, buf);
                return 1;
        }
@@ -1313,7 +1319,7 @@ void dup_files(struct thread_data *td, struct thread_data *org)
                        assert(0);
                }
                __f->fd = -1;
-               fio_file_reset(__f);
+               fio_file_reset(td, __f);
 
                if (f->file_name) {
                        __f->file_name = smalloc_strdup(f->file_name);
@@ -1353,3 +1359,13 @@ void free_release_files(struct thread_data *td)
        td->files_index = 0;
        td->nr_normal_files = 0;
 }
+
+void fio_file_reset(struct thread_data *td, struct fio_file *f)
+{
+       f->last_pos = f->file_offset;
+       f->last_start = -1ULL;
+       if (f->io_axmap)
+               axmap_reset(f->io_axmap);
+       if (td->o.random_generator == FIO_RAND_GEN_LFSR)
+               lfsr_reset(&f->lfsr, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
+}