Add OS agnostic RAND_MAX
[fio.git] / filesetup.c
index 4cffd766a9f836ad910500d6f45aaece565e7da3..96d1b54f0a0f5bd6f6851b629e6f1e584460f5d3 100644 (file)
@@ -68,12 +68,13 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                goto err;
        }
 
+#ifdef FIO_HAVE_FALLOCATE
        dprint(FD_FILE, "fallocate file %s, size %llu\n", f->file_name,
                                                        f->real_file_size);
-       if (posix_fallocate(f->fd, 0, f->real_file_size) < 0) {
-               td_verror(td, errno, "posix_fallocate");
-               goto err;
-       }
+       r = posix_fallocate(f->fd, 0, f->real_file_size);
+       if (r < 0)
+               log_err("fio: posix_fallocate fails: %s\n", strerror(-r));
+#endif
 
        b = malloc(td->o.max_bs[DDIR_WRITE]);
        memset(b, 0, td->o.max_bs[DDIR_WRITE]);
@@ -120,12 +121,12 @@ err:
 
 static unsigned long long get_rand_file_size(struct thread_data *td)
 {
-       unsigned long long ret, size_d;
+       unsigned long long ret, sized;
        long r;
 
        r = os_random_long(&td->file_size_state);
-       size_d = td->o.file_size_high - td->o.file_size_low;
-       ret = (unsigned long long) ((double) size_d * (r / (RAND_MAX + 1.0)));
+       sized = td->o.file_size_high - td->o.file_size_low;
+       ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0)));
        ret += td->o.file_size_low;
        ret -= (ret % td->o.rw_min_bs);
        return ret;
@@ -234,6 +235,9 @@ static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f,
 
 int file_invalidate_cache(struct thread_data *td, struct fio_file *f)
 {
+       if (!(f->flags & FIO_FILE_OPEN))
+               return 0;
+
        return __file_invalidate_cache(td, f, -1, -1);
 }
 
@@ -304,7 +308,7 @@ int generic_open_file(struct thread_data *td, struct fio_file *f)
        if (td->o.sync_io)
                flags |= O_SYNC;
        if (f->filetype != FIO_TYPE_FILE)
-               flags |= O_NOATIME;
+               flags |= FIO_O_NOATIME;
 
 open_again:
        if (td_write(td)) {
@@ -334,8 +338,8 @@ open_again:
                char buf[FIO_VERROR_SIZE];
                int __e = errno;
 
-               if (errno == EPERM && (flags & O_NOATIME)) {
-                       flags &= ~O_NOATIME;
+               if (errno == EPERM && (flags & FIO_O_NOATIME)) {
+                       flags &= ~FIO_O_NOATIME;
                        goto open_again;
                }
 
@@ -599,7 +603,7 @@ 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(long));
+               f->file_map = smalloc(num_maps * sizeof(int));
                if (f->file_map) {
                        f->num_maps = num_maps;
                        continue;
@@ -762,7 +766,7 @@ int put_file(struct thread_data *td, struct fio_file *f)
                ret = td->io_ops->close_file(td, f);
 
        if (!ret)
-               ret = !f_ret;
+               ret = f_ret;
 
        td->nr_open_files--;
        f->flags &= ~FIO_FILE_OPEN;