Merge branch 'master' into gfio
[fio.git] / filesetup.c
index 799202f0da5bbccf9b7aca0e90fc7f7ed5293004..d30b61639afa62548c0147613facbeff3854c724 100644 (file)
 #include "filehash.h"
 #include "os/os.h"
 
+#ifdef FIO_HAVE_LINUX_FALLOCATE
+#include <linux/falloc.h>
+#endif
+
 static int root_warn;
 
 static inline void clear_error(struct thread_data *td)
@@ -67,17 +71,41 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
        }
 
 #ifdef FIO_HAVE_FALLOCATE
-       if (td->o.fallocate && !td->o.fill_device) {
-               dprint(FD_FILE, "fallocate file %s size %llu\n", f->file_name,
-                                                       f->real_file_size);
+       if (!td->o.fill_device) {
+               switch (td->o.fallocate_mode) {
+               case FIO_FALLOCATE_NONE:
+                       break;
+               case FIO_FALLOCATE_POSIX:
+                       dprint(FD_FILE, "posix_fallocate file %s size %llu\n",
+                                f->file_name, f->real_file_size);
+
+                       r = posix_fallocate(f->fd, 0, f->real_file_size);
+                       if (r > 0) {
+                               log_err("fio: posix_fallocate fails: %s\n",
+                                               strerror(r));
+                       }
+                       break;
+#ifdef FIO_HAVE_LINUX_FALLOCATE
+               case FIO_FALLOCATE_KEEP_SIZE:
+                       dprint(FD_FILE,
+                               "fallocate(FALLOC_FL_KEEP_SIZE) "
+                               "file %s size %llu\n",
+                               f->file_name, f->real_file_size);
+
+                       r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0,
+                                       f->real_file_size);
+                       if (r != 0)
+                               td_verror(td, errno, "fallocate");
 
-               r = posix_fallocate(f->fd, 0, f->real_file_size);
-               if (r > 0) {
-                       log_err("fio: posix_fallocate fails: %s\n",
-                                       strerror(r));
+                       break;
+#endif /* FIO_HAVE_LINUX_FALLOCATE */
+               default:
+                       log_err("fio: unknown fallocate mode: %d\n",
+                               td->o.fallocate_mode);
+                       assert(0);
                }
        }
-#endif
+#endif /* FIO_HAVE_FALLOCATE */
 
        if (!new_layout)
                goto done;
@@ -252,7 +280,6 @@ static int bdev_size(struct thread_data *td, struct fio_file *f)
        r = blockdev_size(f, &bytes);
        if (r) {
                td_verror(td, r, "blockdev_size");
-               printf("fd is %d\n", f->fd);
                goto err;
        }
 
@@ -638,7 +665,7 @@ int setup_files(struct thread_data *td)
        dprint(FD_FILE, "setup files\n");
 
        if (td->o.read_iolog_file)
-               return 0;
+               goto done;
 
        /*
         * if ioengine defines a setup() method, it's responsible for
@@ -686,7 +713,8 @@ int setup_files(struct thread_data *td)
        extend_size = total_size = 0;
        need_extend = 0;
        for_each_file(td, f, i) {
-               f->file_offset = td->o.start_offset;
+               f->file_offset = td->o.start_offset +
+                       (td->thread_number - 1) * td->o.offset_increment;
 
                if (!td->o.file_size_low) {
                        /*
@@ -732,6 +760,9 @@ int setup_files(struct thread_data *td)
                }
        }
 
+       if (td->o.size_percent)
+               total_size = (total_size * td->o.size_percent) / 100;
+
        if (!td->o.size || td->o.size > total_size)
                td->o.size = total_size;
 
@@ -785,6 +816,11 @@ int setup_files(struct thread_data *td)
         */
        if (!td->o.read_iolog_file)
                td->total_io_size = td->o.size * td->o.loops;
+
+done:
+       if (td->o.create_only)
+               td->done = 1;
+
        return 0;
 err_offset:
        log_err("%s: you need to specify valid offset=\n", td->o.name);
@@ -819,11 +855,15 @@ int init_random_map(struct thread_data *td)
                                (unsigned long long) td->o.rw_min_bs;
                num_maps = (blocks + BLOCKS_PER_MAP - 1) /
                                (unsigned long long) BLOCKS_PER_MAP;
-               f->file_map = smalloc(num_maps * sizeof(unsigned long));
-               if (f->file_map) {
-                       f->num_maps = num_maps;
-                       continue;
-               }
+               if (num_maps == (unsigned long) num_maps) {
+                       f->file_map = smalloc(num_maps * sizeof(unsigned long));
+                       if (f->file_map) {
+                               f->num_maps = num_maps;
+                               continue;
+                       }
+               } else
+                       f->file_map = NULL;
+
                if (!td->o.softrandommap) {
                        log_err("fio: failed allocating random map. If running"
                                " a large number of jobs, try the 'norandommap'"
@@ -892,10 +932,13 @@ static void get_file_type(struct fio_file *f)
        else
                f->filetype = FIO_TYPE_FILE;
 
+       /* \\.\ is the device namespace in Windows, where every file is
+        * a block device */
+       if (strncmp(f->file_name, "\\\\.\\", 4) == 0)
+               f->filetype = FIO_TYPE_BD;
+
        if (!stat(f->file_name, &sb)) {
-               /* \\.\ is the device namespace in Windows, where every file is
-                * a block device */
-               if (S_ISBLK(sb.st_mode) || strncmp(f->file_name, "\\\\.\\", 4) == 0)
+               if (S_ISBLK(sb.st_mode))
                        f->filetype = FIO_TYPE_BD;
                else if (S_ISCHR(sb.st_mode))
                        f->filetype = FIO_TYPE_CHAR;
@@ -1104,7 +1147,7 @@ static int recurse_dir(struct thread_data *td, const char *dirname)
                if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
                        continue;
 
-               sprintf(full_path, "%s/%s", dirname, dir->d_name);
+               sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name);
 
                if (lstat(full_path, &sb) == -1) {
                        if (errno != ENOENT) {