Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 5 | #include <dirent.h> |
YAMAMOTO Takashi | d74ac84 | 2010-06-09 09:44:53 +0200 | [diff] [blame] | 6 | #include <libgen.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 7 | #include <sys/stat.h> |
| 8 | #include <sys/mman.h> |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 9 | #include <sys/types.h> |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 10 | |
| 11 | #include "fio.h" |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 12 | #include "smalloc.h" |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 13 | #include "filehash.h" |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 14 | #include "os/os.h" |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 15 | #include "hash.h" |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 16 | #include "lib/axmap.h" |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 17 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 18 | #ifdef FIO_HAVE_LINUX_FALLOCATE |
| 19 | #include <linux/falloc.h> |
| 20 | #endif |
| 21 | |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 22 | static int root_warn; |
| 23 | |
Jens Axboe | c592b9f | 2009-06-03 09:29:28 +0200 | [diff] [blame] | 24 | static inline void clear_error(struct thread_data *td) |
| 25 | { |
| 26 | td->error = 0; |
| 27 | td->verror[0] = '\0'; |
| 28 | } |
| 29 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 30 | /* |
| 31 | * Leaves f->fd open on success, caller must close |
| 32 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 33 | static int extend_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 34 | { |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 35 | int r, new_layout = 0, unlink_file = 0, flags; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 36 | unsigned long long left; |
| 37 | unsigned int bs; |
| 38 | char *b; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 39 | |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 40 | if (read_only) { |
| 41 | log_err("fio: refusing extend of file due to read-only\n"); |
| 42 | return 0; |
| 43 | } |
| 44 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 45 | /* |
| 46 | * check if we need to lay the file out complete again. fio |
| 47 | * does that for operations involving reads, or for writes |
| 48 | * where overwrite is set |
| 49 | */ |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 50 | if (td_read(td) || (td_write(td) && td->o.overwrite) || |
| 51 | (td_write(td) && td->io_ops->flags & FIO_NOEXTEND)) |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 52 | new_layout = 1; |
Jens Axboe | ea44365 | 2007-03-29 14:52:13 +0200 | [diff] [blame] | 53 | if (td_write(td) && !td->o.overwrite) |
| 54 | unlink_file = 1; |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 55 | |
Jens Axboe | 6ae1f57 | 2008-02-21 10:20:00 +0100 | [diff] [blame] | 56 | if (unlink_file || new_layout) { |
Jens Axboe | bd199f2 | 2008-03-26 09:57:18 +0100 | [diff] [blame] | 57 | dprint(FD_FILE, "layout unlink %s\n", f->file_name); |
Zhang, Yanmin | 982016d | 2008-02-26 15:35:52 +0100 | [diff] [blame] | 58 | if ((unlink(f->file_name) < 0) && (errno != ENOENT)) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 59 | td_verror(td, errno, "unlink"); |
| 60 | return 1; |
| 61 | } |
| 62 | } |
| 63 | |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 64 | flags = O_WRONLY | O_CREAT; |
| 65 | if (new_layout) |
| 66 | flags |= O_TRUNC; |
| 67 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 68 | dprint(FD_FILE, "open file %s, flags %x\n", f->file_name, flags); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 69 | f->fd = open(f->file_name, flags, 0644); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 70 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 71 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 72 | return 1; |
| 73 | } |
| 74 | |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 75 | #ifdef FIO_HAVE_FALLOCATE |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 76 | if (!td->o.fill_device) { |
| 77 | switch (td->o.fallocate_mode) { |
| 78 | case FIO_FALLOCATE_NONE: |
| 79 | break; |
| 80 | case FIO_FALLOCATE_POSIX: |
| 81 | dprint(FD_FILE, "posix_fallocate file %s size %llu\n", |
| 82 | f->file_name, f->real_file_size); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 83 | |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 84 | r = posix_fallocate(f->fd, 0, f->real_file_size); |
| 85 | if (r > 0) { |
| 86 | log_err("fio: posix_fallocate fails: %s\n", |
| 87 | strerror(r)); |
| 88 | } |
| 89 | break; |
| 90 | #ifdef FIO_HAVE_LINUX_FALLOCATE |
| 91 | case FIO_FALLOCATE_KEEP_SIZE: |
| 92 | dprint(FD_FILE, |
| 93 | "fallocate(FALLOC_FL_KEEP_SIZE) " |
| 94 | "file %s size %llu\n", |
| 95 | f->file_name, f->real_file_size); |
| 96 | |
| 97 | r = fallocate(f->fd, FALLOC_FL_KEEP_SIZE, 0, |
| 98 | f->real_file_size); |
| 99 | if (r != 0) { |
| 100 | td_verror(td, errno, "fallocate"); |
| 101 | } |
| 102 | break; |
| 103 | #endif /* FIO_HAVE_LINUX_FALLOCATE */ |
| 104 | default: |
| 105 | log_err("fio: unknown fallocate mode: %d\n", |
| 106 | td->o.fallocate_mode); |
| 107 | assert(0); |
Jens Axboe | 7bc8c2c | 2010-01-28 11:31:31 +0100 | [diff] [blame] | 108 | } |
| 109 | } |
Eric Gouriou | a596f04 | 2011-06-17 09:11:45 +0200 | [diff] [blame] | 110 | #endif /* FIO_HAVE_FALLOCATE */ |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 111 | |
Shawn Lewis | fc74ac1 | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 112 | if (!new_layout) |
| 113 | goto done; |
| 114 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 115 | /* |
| 116 | * The size will be -1ULL when fill_device is used, so don't truncate |
| 117 | * or fallocate this file, just write it |
| 118 | */ |
| 119 | if (!td->o.fill_device) { |
| 120 | dprint(FD_FILE, "truncate file %s, size %llu\n", f->file_name, |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 121 | f->real_file_size); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 122 | if (ftruncate(f->fd, f->real_file_size) == -1) { |
| 123 | td_verror(td, errno, "ftruncate"); |
| 124 | goto err; |
| 125 | } |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 126 | } |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 127 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 128 | b = malloc(td->o.max_bs[DDIR_WRITE]); |
| 129 | memset(b, 0, td->o.max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 130 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 131 | left = f->real_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 132 | while (left && !td->terminate) { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 133 | bs = td->o.max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 134 | if (bs > left) |
| 135 | bs = left; |
| 136 | |
| 137 | r = write(f->fd, b, bs); |
| 138 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 139 | if (r > 0) { |
| 140 | left -= r; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 141 | continue; |
| 142 | } else { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 143 | if (r < 0) { |
| 144 | int __e = errno; |
| 145 | |
| 146 | if (__e == ENOSPC) { |
| 147 | if (td->o.fill_device) |
| 148 | break; |
| 149 | log_info("fio: ENOSPC on laying out " |
| 150 | "file, stopping\n"); |
| 151 | break; |
| 152 | } |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 153 | td_verror(td, errno, "write"); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 154 | } else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 155 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 156 | |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 161 | if (td->terminate) { |
| 162 | dprint(FD_FILE, "terminate unlink %s\n", f->file_name); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 163 | unlink(f->file_name); |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 164 | } else if (td->o.create_fsync) { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 165 | if (fsync(f->fd) < 0) { |
| 166 | td_verror(td, errno, "fsync"); |
| 167 | goto err; |
| 168 | } |
| 169 | } |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 170 | if (td->o.fill_device && !td_write(td)) { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 171 | fio_file_clear_size_known(f); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 172 | if (td_io_get_file_size(td, f)) |
| 173 | goto err; |
| 174 | if (f->io_size > f->real_file_size) |
| 175 | f->io_size = f->real_file_size; |
| 176 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 177 | |
| 178 | free(b); |
Jens Axboe | 507a702 | 2007-03-27 15:52:46 +0200 | [diff] [blame] | 179 | done: |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | err: |
| 182 | close(f->fd); |
| 183 | f->fd = -1; |
| 184 | return 1; |
| 185 | } |
| 186 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 187 | static int pre_read_file(struct thread_data *td, struct fio_file *f) |
| 188 | { |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 189 | int r, did_open = 0, old_runstate; |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 190 | unsigned long long left; |
| 191 | unsigned int bs; |
| 192 | char *b; |
| 193 | |
Jens Axboe | 9c0d224 | 2009-07-01 12:26:28 +0200 | [diff] [blame] | 194 | if (td->io_ops->flags & FIO_PIPEIO) |
| 195 | return 0; |
| 196 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 197 | if (!fio_file_open(f)) { |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 198 | if (td->io_ops->open_file(td, f)) { |
| 199 | log_err("fio: cannot pre-read, failed to open file\n"); |
| 200 | return 1; |
| 201 | } |
| 202 | did_open = 1; |
| 203 | } |
| 204 | |
| 205 | old_runstate = td->runstate; |
| 206 | td_set_runstate(td, TD_PRE_READING); |
| 207 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 208 | bs = td->o.max_bs[DDIR_READ]; |
| 209 | b = malloc(bs); |
| 210 | memset(b, 0, bs); |
| 211 | |
| 212 | lseek(f->fd, f->file_offset, SEEK_SET); |
| 213 | left = f->io_size; |
| 214 | |
| 215 | while (left && !td->terminate) { |
| 216 | if (bs > left) |
| 217 | bs = left; |
| 218 | |
| 219 | r = read(f->fd, b, bs); |
| 220 | |
| 221 | if (r == (int) bs) { |
| 222 | left -= bs; |
| 223 | continue; |
| 224 | } else { |
| 225 | td_verror(td, EIO, "pre_read"); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
Jens Axboe | b0f6586 | 2009-05-20 11:52:15 +0200 | [diff] [blame] | 230 | td_set_runstate(td, old_runstate); |
| 231 | |
| 232 | if (did_open) |
| 233 | td->io_ops->close_file(td, f); |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 234 | free(b); |
| 235 | return 0; |
| 236 | } |
| 237 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 238 | static unsigned long long get_rand_file_size(struct thread_data *td) |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 239 | { |
Jens Axboe | dc873b6 | 2008-06-04 20:13:04 +0200 | [diff] [blame] | 240 | unsigned long long ret, sized; |
Jens Axboe | 1294c3e | 2011-05-11 08:15:18 +0200 | [diff] [blame] | 241 | unsigned long r; |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 242 | |
Jens Axboe | 4c07ad8 | 2011-03-28 09:51:09 +0200 | [diff] [blame] | 243 | if (td->o.use_os_rand) { |
| 244 | r = os_random_long(&td->file_size_state); |
| 245 | sized = td->o.file_size_high - td->o.file_size_low; |
| 246 | ret = (unsigned long long) ((double) sized * (r / (OS_RAND_MAX + 1.0))); |
| 247 | } else { |
| 248 | r = __rand(&td->__file_size_state); |
| 249 | sized = td->o.file_size_high - td->o.file_size_low; |
| 250 | ret = (unsigned long long) ((double) sized * (r / (FRAND_MAX + 1.0))); |
| 251 | } |
| 252 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 253 | ret += td->o.file_size_low; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 254 | ret -= (ret % td->o.rw_min_bs); |
Jens Axboe | 9c60ce6 | 2007-03-15 09:14:47 +0100 | [diff] [blame] | 255 | return ret; |
| 256 | } |
| 257 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 258 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 259 | { |
| 260 | struct stat st; |
| 261 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 262 | if (stat(f->file_name, &st) == -1) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 263 | td_verror(td, errno, "fstat"); |
| 264 | return 1; |
| 265 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 266 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 267 | f->real_file_size = st.st_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 272 | { |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 273 | unsigned long long bytes = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 274 | int r; |
| 275 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 276 | if (td->io_ops->open_file(td, f)) { |
| 277 | log_err("fio: failed opening blockdev %s for size check\n", |
| 278 | f->file_name); |
| 279 | return 1; |
| 280 | } |
| 281 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 282 | r = blockdev_size(f, &bytes); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 283 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 284 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 285 | goto err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 286 | } |
| 287 | |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 288 | if (!bytes) { |
| 289 | log_err("%s: zero sized block device?\n", f->file_name); |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 290 | goto err; |
Jens Axboe | 7ab077a | 2009-02-02 15:07:37 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 293 | f->real_file_size = bytes; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 294 | td->io_ops->close_file(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 295 | return 0; |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 296 | err: |
| 297 | td->io_ops->close_file(td, f); |
| 298 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 299 | } |
| 300 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 301 | static int char_size(struct thread_data *td, struct fio_file *f) |
| 302 | { |
| 303 | #ifdef FIO_HAVE_CHARDEV_SIZE |
Bruce Cran | 9b83656 | 2011-01-08 19:49:54 +0100 | [diff] [blame] | 304 | unsigned long long bytes = 0; |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 305 | int r; |
| 306 | |
| 307 | if (td->io_ops->open_file(td, f)) { |
| 308 | log_err("fio: failed opening blockdev %s for size check\n", |
| 309 | f->file_name); |
| 310 | return 1; |
| 311 | } |
| 312 | |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 313 | r = chardev_size(f, &bytes); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 314 | if (r) { |
| 315 | td_verror(td, r, "chardev_size"); |
| 316 | goto err; |
| 317 | } |
| 318 | |
| 319 | if (!bytes) { |
| 320 | log_err("%s: zero sized char device?\n", f->file_name); |
| 321 | goto err; |
| 322 | } |
| 323 | |
| 324 | f->real_file_size = bytes; |
| 325 | td->io_ops->close_file(td, f); |
| 326 | return 0; |
| 327 | err: |
| 328 | td->io_ops->close_file(td, f); |
| 329 | return 1; |
| 330 | #else |
| 331 | f->real_file_size = -1ULL; |
| 332 | return 0; |
| 333 | #endif |
| 334 | } |
| 335 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 336 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 337 | { |
| 338 | int ret = 0; |
| 339 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 340 | if (fio_file_size_known(f)) |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 341 | return 0; |
| 342 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 343 | if (f->filetype == FIO_TYPE_FILE) |
| 344 | ret = file_size(td, f); |
| 345 | else if (f->filetype == FIO_TYPE_BD) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 346 | ret = bdev_size(td, f); |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 347 | else if (f->filetype == FIO_TYPE_CHAR) |
| 348 | ret = char_size(td, f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 349 | else |
| 350 | f->real_file_size = -1; |
| 351 | |
| 352 | if (ret) |
| 353 | return ret; |
| 354 | |
| 355 | if (f->file_offset > f->real_file_size) { |
Bruce Cran | 0f2152c | 2010-12-15 10:33:03 +0100 | [diff] [blame] | 356 | log_err("%s: offset extends end (%llu > %llu)\n", td->o.name, |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 357 | f->file_offset, f->real_file_size); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 358 | return 1; |
| 359 | } |
| 360 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 361 | fio_file_set_size_known(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 362 | return 0; |
| 363 | } |
| 364 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 365 | static int __file_invalidate_cache(struct thread_data *td, struct fio_file *f, |
| 366 | unsigned long long off, |
| 367 | unsigned long long len) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 368 | { |
| 369 | int ret = 0; |
| 370 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 371 | if (len == -1ULL) |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 372 | len = f->io_size; |
| 373 | if (off == -1ULL) |
| 374 | off = f->file_offset; |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 375 | |
Jens Axboe | 0d1cd20 | 2009-06-05 22:11:52 +0200 | [diff] [blame] | 376 | if (len == -1ULL || off == -1ULL) |
| 377 | return 0; |
| 378 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 379 | dprint(FD_IO, "invalidate cache %s: %llu/%llu\n", f->file_name, off, |
| 380 | len); |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 381 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 382 | /* |
| 383 | * FIXME: add blockdev flushing too |
| 384 | */ |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 385 | if (f->mmap_ptr) { |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 386 | ret = posix_madvise(f->mmap_ptr, f->mmap_sz, POSIX_MADV_DONTNEED); |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 387 | #ifdef FIO_MADV_FREE |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 388 | (void) posix_madvise(f->mmap_ptr, f->mmap_sz, FIO_MADV_FREE); |
Jens Axboe | a1c5807 | 2009-08-04 23:17:02 +0200 | [diff] [blame] | 389 | #endif |
| 390 | } else if (f->filetype == FIO_TYPE_FILE) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 391 | ret = posix_fadvise(f->fd, off, len, POSIX_FADV_DONTNEED); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 392 | } else if (f->filetype == FIO_TYPE_BD) { |
Bruce Cran | ecc314b | 2011-01-04 10:59:30 +0100 | [diff] [blame] | 393 | ret = blockdev_invalidate_cache(f); |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 394 | if (ret < 0 && errno == EACCES && geteuid()) { |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 395 | if (!root_warn) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 396 | log_err("fio: only root may flush block " |
| 397 | "devices. Cache flush bypassed!\n"); |
Jens Axboe | 7172cfe | 2007-07-23 14:36:00 +0200 | [diff] [blame] | 398 | root_warn = 1; |
| 399 | } |
Jens Axboe | 7e0e25c | 2007-03-27 12:49:56 +0200 | [diff] [blame] | 400 | ret = 0; |
| 401 | } |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 402 | } else if (f->filetype == FIO_TYPE_CHAR || f->filetype == FIO_TYPE_PIPE) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 403 | ret = 0; |
| 404 | |
| 405 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 406 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 407 | return 1; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 408 | } else if (ret > 0) { |
| 409 | td_verror(td, ret, "invalidate_cache"); |
| 410 | return 1; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 411 | } |
| 412 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 413 | return ret; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 414 | |
| 415 | } |
| 416 | |
| 417 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 418 | { |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 419 | if (!fio_file_open(f)) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 420 | return 0; |
| 421 | |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 422 | return __file_invalidate_cache(td, f, -1ULL, -1ULL); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 423 | } |
| 424 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 425 | int generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 426 | { |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 427 | int ret = 0; |
| 428 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 429 | dprint(FD_FILE, "fd close %s\n", f->file_name); |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 430 | |
| 431 | remove_file_hash(f); |
| 432 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 433 | if (close(f->fd) < 0) |
| 434 | ret = errno; |
| 435 | |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 436 | f->fd = -1; |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 437 | |
| 438 | if (f->shadow_fd != -1) { |
| 439 | close(f->shadow_fd); |
| 440 | f->shadow_fd = -1; |
| 441 | } |
| 442 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 443 | return ret; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Dmitry Monakhov | 1ccc6dc | 2012-09-19 23:22:53 +0400 | [diff] [blame] | 446 | int file_lookup_open(struct fio_file *f, int flags) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 447 | { |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 448 | struct fio_file *__f; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 449 | int from_hash; |
| 450 | |
| 451 | __f = lookup_file_hash(f->file_name); |
| 452 | if (__f) { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 453 | dprint(FD_FILE, "found file in hash %s\n", f->file_name); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 454 | /* |
| 455 | * racy, need the __f->lock locked |
| 456 | */ |
| 457 | f->lock = __f->lock; |
| 458 | f->lock_owner = __f->lock_owner; |
| 459 | f->lock_batch = __f->lock_batch; |
| 460 | f->lock_ddir = __f->lock_ddir; |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 461 | from_hash = 1; |
| 462 | } else { |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 463 | dprint(FD_FILE, "file not found in hash %s\n", f->file_name); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 464 | from_hash = 0; |
| 465 | } |
| 466 | |
Jens Axboe | e8670ef | 2008-03-06 12:06:30 +0100 | [diff] [blame] | 467 | f->fd = open(f->file_name, flags, 0600); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 468 | return from_hash; |
| 469 | } |
| 470 | |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 471 | static int file_close_shadow_fds(struct thread_data *td) |
| 472 | { |
| 473 | struct fio_file *f; |
| 474 | int num_closed = 0; |
| 475 | unsigned int i; |
| 476 | |
| 477 | for_each_file(td, f, i) { |
| 478 | if (f->shadow_fd == -1) |
| 479 | continue; |
| 480 | |
| 481 | close(f->shadow_fd); |
| 482 | f->shadow_fd = -1; |
| 483 | num_closed++; |
| 484 | } |
| 485 | |
| 486 | return num_closed; |
| 487 | } |
| 488 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 489 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
| 490 | { |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 491 | int is_std = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 492 | int flags = 0; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 493 | int from_hash = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 494 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 495 | dprint(FD_FILE, "fd open %s\n", f->file_name); |
| 496 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 497 | if (td_trim(td) && f->filetype != FIO_TYPE_BD) { |
| 498 | log_err("fio: trim only applies to block device\n"); |
| 499 | return 1; |
| 500 | } |
| 501 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 502 | if (!strcmp(f->file_name, "-")) { |
| 503 | if (td_rw(td)) { |
| 504 | log_err("fio: can't read/write to stdin/out\n"); |
| 505 | return 1; |
| 506 | } |
| 507 | is_std = 1; |
Jens Axboe | ce98fa6 | 2007-04-17 13:27:32 +0200 | [diff] [blame] | 508 | |
| 509 | /* |
| 510 | * move output logging to stderr, if we are writing to stdout |
| 511 | */ |
| 512 | if (td_write(td)) |
| 513 | f_out = stderr; |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 514 | } |
| 515 | |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 516 | if (td_trim(td)) |
| 517 | goto skip_flags; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 518 | if (td->o.odirect) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 519 | flags |= OS_O_DIRECT; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 520 | if (td->o.sync_io) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 521 | flags |= O_SYNC; |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 522 | if (td->o.create_on_open) |
| 523 | flags |= O_CREAT; |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 524 | skip_flags: |
| 525 | if (f->filetype != FIO_TYPE_FILE) |
| 526 | flags |= FIO_O_NOATIME; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 527 | |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 528 | open_again: |
Jens Axboe | 660a1cb | 2007-04-17 15:37:47 +0200 | [diff] [blame] | 529 | if (td_write(td)) { |
Jens Axboe | 1730815 | 2008-03-07 13:42:31 +0100 | [diff] [blame] | 530 | if (!read_only) |
| 531 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 532 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 533 | if (f->filetype == FIO_TYPE_FILE) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 534 | flags |= O_CREAT; |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 535 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 536 | if (is_std) |
| 537 | f->fd = dup(STDOUT_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 538 | else |
| 539 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 540 | } else if (td_read(td)) { |
Jens Axboe | 4241ea8 | 2007-09-12 08:18:36 +0200 | [diff] [blame] | 541 | if (f->filetype == FIO_TYPE_CHAR && !read_only) |
Jens Axboe | 2fd233b | 2007-03-02 08:44:25 +0100 | [diff] [blame] | 542 | flags |= O_RDWR; |
| 543 | else |
| 544 | flags |= O_RDONLY; |
| 545 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 546 | if (is_std) |
| 547 | f->fd = dup(STDIN_FILENO); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 548 | else |
| 549 | from_hash = file_lookup_open(f, flags); |
Shaohua Li | 6eaf09d | 2012-09-14 08:49:43 +0200 | [diff] [blame] | 550 | } else { //td trim |
| 551 | flags |= O_RDWR; |
| 552 | from_hash = file_lookup_open(f, flags); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if (f->fd == -1) { |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 556 | char buf[FIO_VERROR_SIZE]; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 557 | int __e = errno; |
| 558 | |
Jens Axboe | 835d9b9 | 2009-06-09 15:23:38 +0200 | [diff] [blame] | 559 | if (__e == EPERM && (flags & FIO_O_NOATIME)) { |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 560 | flags &= ~FIO_O_NOATIME; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 561 | goto open_again; |
| 562 | } |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 563 | if (__e == EMFILE && file_close_shadow_fds(td)) |
| 564 | goto open_again; |
Aaron Carroll | 056f345 | 2007-11-26 09:08:53 +0100 | [diff] [blame] | 565 | |
Jens Axboe | e8670ef | 2008-03-06 12:06:30 +0100 | [diff] [blame] | 566 | snprintf(buf, sizeof(buf) - 1, "open(%s)", f->file_name); |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 567 | |
Jens Axboe | a93c5f0 | 2012-06-11 08:53:53 +0200 | [diff] [blame] | 568 | if (__e == EINVAL && (flags & OS_O_DIRECT)) { |
| 569 | log_err("fio: looks like your file system does not " \ |
| 570 | "support direct=1/buffered=0\n"); |
| 571 | } |
| 572 | |
Jens Axboe | e4e3325 | 2007-03-21 13:07:54 +0100 | [diff] [blame] | 573 | td_verror(td, __e, buf); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 576 | if (!from_hash && f->fd != -1) { |
| 577 | if (add_file_hash(f)) { |
Jens Axboe | 3f0ca9b | 2011-05-24 21:36:24 +0200 | [diff] [blame] | 578 | int fio_unused ret; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 579 | |
| 580 | /* |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 581 | * Stash away descriptor for later close. This is to |
| 582 | * work-around a "feature" on Linux, where a close of |
| 583 | * an fd that has been opened for write will trigger |
| 584 | * udev to call blkid to check partitions, fs id, etc. |
| 585 | * That polutes the device cache, which can slow down |
| 586 | * unbuffered accesses. |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 587 | */ |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 588 | if (f->shadow_fd == -1) |
| 589 | f->shadow_fd = f->fd; |
| 590 | else { |
| 591 | /* |
| 592 | * OK to ignore, we haven't done anything |
| 593 | * with it |
| 594 | */ |
| 595 | ret = generic_close_file(td, f); |
| 596 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 597 | goto open_again; |
| 598 | } |
| 599 | } |
Jens Axboe | 4906e0b | 2008-03-01 18:59:51 +0100 | [diff] [blame] | 600 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 601 | return 0; |
Jens Axboe | b5af829 | 2007-03-08 12:43:13 +0100 | [diff] [blame] | 602 | } |
| 603 | |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 604 | int generic_get_file_size(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 605 | { |
Jens Axboe | df9c26b | 2009-03-05 10:13:58 +0100 | [diff] [blame] | 606 | return get_file_size(td, f); |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 607 | } |
| 608 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 609 | /* |
| 610 | * open/close all files, so that ->real_file_size gets set |
| 611 | */ |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 612 | static int get_file_sizes(struct thread_data *td) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 613 | { |
| 614 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 615 | unsigned int i; |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 616 | int err = 0; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 617 | |
| 618 | for_each_file(td, f, i) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 619 | dprint(FD_FILE, "get file size for %p/%d/%p\n", f, i, |
| 620 | f->file_name); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 621 | |
Jens Axboe | 99a47c6 | 2009-04-07 22:20:56 +0200 | [diff] [blame] | 622 | if (td_io_get_file_size(td, f)) { |
Jens Axboe | 40b44f4 | 2007-04-11 12:43:35 +0200 | [diff] [blame] | 623 | if (td->error != ENOENT) { |
| 624 | log_err("%s\n", td->verror); |
| 625 | err = 1; |
| 626 | } |
Jens Axboe | 541d66d | 2007-03-27 19:50:11 +0200 | [diff] [blame] | 627 | clear_error(td); |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 628 | } |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 629 | |
| 630 | if (f->real_file_size == -1ULL && td->o.size) |
| 631 | f->real_file_size = td->o.size / td->o.nr_files; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 632 | } |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 633 | |
| 634 | return err; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 635 | } |
| 636 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 637 | struct fio_mount { |
| 638 | struct flist_head list; |
| 639 | const char *base; |
| 640 | char __base[256]; |
| 641 | unsigned int key; |
| 642 | }; |
| 643 | |
| 644 | /* |
| 645 | * Get free number of bytes for each file on each unique mount. |
| 646 | */ |
| 647 | static unsigned long long get_fs_free_counts(struct thread_data *td) |
| 648 | { |
| 649 | struct flist_head *n, *tmp; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 650 | unsigned long long ret = 0; |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 651 | struct fio_mount *fm; |
| 652 | FLIST_HEAD(list); |
| 653 | struct fio_file *f; |
| 654 | unsigned int i; |
| 655 | |
| 656 | for_each_file(td, f, i) { |
| 657 | struct stat sb; |
| 658 | char buf[256]; |
| 659 | |
Jens Axboe | 4ccdccd | 2010-06-24 10:27:22 +0200 | [diff] [blame] | 660 | if (f->filetype == FIO_TYPE_BD || f->filetype == FIO_TYPE_CHAR) { |
| 661 | if (f->real_file_size != -1ULL) |
| 662 | ret += f->real_file_size; |
Jens Axboe | 68b0316 | 2010-06-24 09:22:41 +0200 | [diff] [blame] | 663 | continue; |
| 664 | } else if (f->filetype != FIO_TYPE_FILE) |
| 665 | continue; |
| 666 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 667 | strcpy(buf, f->file_name); |
| 668 | |
| 669 | if (stat(buf, &sb) < 0) { |
| 670 | if (errno != ENOENT) |
| 671 | break; |
| 672 | strcpy(buf, "."); |
| 673 | if (stat(buf, &sb) < 0) |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | fm = NULL; |
| 678 | flist_for_each(n, &list) { |
| 679 | fm = flist_entry(n, struct fio_mount, list); |
| 680 | if (fm->key == sb.st_dev) |
| 681 | break; |
| 682 | |
| 683 | fm = NULL; |
| 684 | } |
| 685 | |
| 686 | if (fm) |
| 687 | continue; |
| 688 | |
| 689 | fm = malloc(sizeof(*fm)); |
| 690 | strcpy(fm->__base, buf); |
| 691 | fm->base = basename(fm->__base); |
| 692 | fm->key = sb.st_dev; |
| 693 | flist_add(&fm->list, &list); |
| 694 | } |
| 695 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 696 | flist_for_each_safe(n, tmp, &list) { |
| 697 | unsigned long long sz; |
| 698 | |
| 699 | fm = flist_entry(n, struct fio_mount, list); |
| 700 | flist_del(&fm->list); |
| 701 | |
| 702 | sz = get_fs_size(fm->base); |
| 703 | if (sz && sz != -1ULL) |
| 704 | ret += sz; |
| 705 | |
| 706 | free(fm); |
| 707 | } |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 712 | unsigned long long get_start_offset(struct thread_data *td) |
| 713 | { |
| 714 | return td->o.start_offset + |
| 715 | (td->thread_number - 1) * td->o.offset_increment; |
| 716 | } |
| 717 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 718 | /* |
| 719 | * Open the files and setup files sizes, creating files if necessary. |
| 720 | */ |
| 721 | int setup_files(struct thread_data *td) |
| 722 | { |
| 723 | unsigned long long total_size, extend_size; |
| 724 | struct fio_file *f; |
| 725 | unsigned int i; |
Jens Axboe | 000b080 | 2007-03-27 15:56:10 +0200 | [diff] [blame] | 726 | int err = 0, need_extend; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 727 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 728 | dprint(FD_FILE, "setup files\n"); |
| 729 | |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 730 | if (td->o.read_iolog_file) |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 731 | goto done; |
Jens Axboe | 691c8fb | 2008-03-07 14:26:26 +0100 | [diff] [blame] | 732 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 733 | /* |
| 734 | * if ioengine defines a setup() method, it's responsible for |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 735 | * opening the files and setting f->real_file_size to indicate |
| 736 | * the valid range for that file. |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 737 | */ |
| 738 | if (td->io_ops->setup) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 739 | err = td->io_ops->setup(td); |
| 740 | else |
Jens Axboe | bab3fd5 | 2007-04-02 10:34:18 +0200 | [diff] [blame] | 741 | err = get_file_sizes(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 742 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 743 | if (err) |
| 744 | return err; |
| 745 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 746 | /* |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 747 | * check sizes. if the files/devices do not exist and the size |
| 748 | * isn't passed to fio, abort. |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 749 | */ |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 750 | total_size = 0; |
| 751 | for_each_file(td, f, i) { |
| 752 | if (f->real_file_size == -1ULL) |
| 753 | total_size = -1ULL; |
| 754 | else |
| 755 | total_size += f->real_file_size; |
| 756 | } |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 757 | |
Jens Axboe | 2e3bd4c | 2010-05-17 12:29:57 +0200 | [diff] [blame] | 758 | if (td->o.fill_device) |
| 759 | td->fill_device_size = get_fs_free_counts(td); |
| 760 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 761 | /* |
| 762 | * device/file sizes are zero and no size given, punt |
| 763 | */ |
Jens Axboe | 1f809d1 | 2007-10-25 18:34:02 +0200 | [diff] [blame] | 764 | if ((!total_size || total_size == -1ULL) && !td->o.size && |
Shawn Lewis | aa31f1f | 2008-01-11 09:45:11 +0100 | [diff] [blame] | 765 | !(td->io_ops->flags & FIO_NOIO) && !td->o.fill_device) { |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 766 | log_err("%s: you need to specify size=\n", td->o.name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 767 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 768 | return 1; |
| 769 | } |
| 770 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 771 | /* |
| 772 | * now file sizes are known, so we can set ->io_size. if size= is |
| 773 | * not given, ->io_size is just equal to ->real_file_size. if size |
| 774 | * is given, ->io_size is size / nr_files. |
| 775 | */ |
| 776 | extend_size = total_size = 0; |
| 777 | need_extend = 0; |
| 778 | for_each_file(td, f, i) { |
Dan Ehrenberg | ce95d65 | 2012-08-16 08:58:21 +0200 | [diff] [blame] | 779 | f->file_offset = get_start_offset(td); |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 780 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 781 | if (!td->o.file_size_low) { |
| 782 | /* |
| 783 | * no file size range given, file size is equal to |
| 784 | * total size divided by number of files. if that is |
| 785 | * zero, set it to the real file size. |
| 786 | */ |
| 787 | f->io_size = td->o.size / td->o.nr_files; |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 788 | if (!f->io_size) |
Jens Axboe | 273f8c9 | 2008-01-25 14:02:15 +0100 | [diff] [blame] | 789 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 790 | } else if (f->real_file_size < td->o.file_size_low || |
| 791 | f->real_file_size > td->o.file_size_high) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 792 | if (f->file_offset > td->o.file_size_low) |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 793 | goto err_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 794 | /* |
| 795 | * file size given. if it's fixed, use that. if it's a |
| 796 | * range, generate a random size in-between. |
| 797 | */ |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 798 | if (td->o.file_size_low == td->o.file_size_high) { |
| 799 | f->io_size = td->o.file_size_low |
| 800 | - f->file_offset; |
| 801 | } else { |
| 802 | f->io_size = get_rand_file_size(td) |
| 803 | - f->file_offset; |
| 804 | } |
Jens Axboe | 65bdb10 | 2008-01-24 13:13:12 +0100 | [diff] [blame] | 805 | } else |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 806 | f->io_size = f->real_file_size - f->file_offset; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 807 | |
| 808 | if (f->io_size == -1ULL) |
| 809 | total_size = -1ULL; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 810 | else { |
| 811 | if (td->o.size_percent) |
| 812 | f->io_size = (f->io_size * td->o.size_percent) / 100; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 813 | total_size += f->io_size; |
Shaohua Li | 4d00256 | 2012-09-21 08:32:32 +0200 | [diff] [blame] | 814 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 815 | |
| 816 | if (f->filetype == FIO_TYPE_FILE && |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 817 | (f->io_size + f->file_offset) > f->real_file_size && |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 818 | !(td->io_ops->flags & FIO_DISKLESSIO)) { |
Jens Axboe | 814452b | 2009-03-04 12:53:13 +0100 | [diff] [blame] | 819 | if (!td->o.create_on_open) { |
| 820 | need_extend++; |
| 821 | extend_size += (f->io_size + f->file_offset); |
| 822 | } else |
| 823 | f->real_file_size = f->io_size + f->file_offset; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 824 | fio_file_set_extend(f); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 825 | } |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 826 | } |
| 827 | |
ljzhang,Yaxin Hu,Jianchao Tang | 2298290 | 2007-07-27 13:21:28 +0200 | [diff] [blame] | 828 | if (!td->o.size || td->o.size > total_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 829 | td->o.size = total_size; |
| 830 | |
| 831 | /* |
| 832 | * See if we need to extend some files |
| 833 | */ |
| 834 | if (need_extend) { |
| 835 | temp_stall_ts = 1; |
Jens Axboe | f3afa57 | 2012-09-17 13:34:16 +0200 | [diff] [blame] | 836 | if (output_format == FIO_OUTPUT_NORMAL) |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 837 | log_info("%s: Laying out IO file(s) (%u file(s) /" |
Bruce Cran | 0f2152c | 2010-12-15 10:33:03 +0100 | [diff] [blame] | 838 | " %lluMB)\n", td->o.name, need_extend, |
Shaozhi Shawn Ye | a7ba8c5 | 2008-09-10 09:04:18 +0200 | [diff] [blame] | 839 | extend_size >> 20); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 840 | |
| 841 | for_each_file(td, f, i) { |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 842 | unsigned long long old_len = -1ULL, extend_len = -1ULL; |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 843 | |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 844 | if (!fio_file_extend(f)) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 845 | continue; |
| 846 | |
Jens Axboe | 409b341 | 2007-03-28 09:57:01 +0200 | [diff] [blame] | 847 | assert(f->filetype == FIO_TYPE_FILE); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 848 | fio_file_clear_extend(f); |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 849 | if (!td->o.fill_device) { |
| 850 | old_len = f->real_file_size; |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 851 | extend_len = f->io_size + f->file_offset - |
| 852 | old_len; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 853 | } |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 854 | f->real_file_size = (f->io_size + f->file_offset); |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 855 | err = extend_file(td, f); |
| 856 | if (err) |
| 857 | break; |
Jens Axboe | 5e0074c | 2009-06-02 21:40:09 +0200 | [diff] [blame] | 858 | |
Jens Axboe | 3baddf2 | 2008-04-04 11:10:30 +0200 | [diff] [blame] | 859 | err = __file_invalidate_cache(td, f, old_len, |
| 860 | extend_len); |
| 861 | close(f->fd); |
| 862 | f->fd = -1; |
| 863 | if (err) |
| 864 | break; |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 865 | } |
| 866 | temp_stall_ts = 0; |
| 867 | } |
| 868 | |
| 869 | if (err) |
| 870 | return err; |
| 871 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 872 | if (!td->o.zone_size) |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 873 | td->o.zone_size = td->o.size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 874 | |
Jens Axboe | ea966f8 | 2007-09-03 10:09:37 +0200 | [diff] [blame] | 875 | /* |
| 876 | * iolog already set the total io size, if we read back |
| 877 | * stored entries. |
| 878 | */ |
| 879 | if (!td->o.read_iolog_file) |
| 880 | td->total_io_size = td->o.size * td->o.loops; |
Jens Axboe | 25460cf | 2012-05-02 13:58:02 +0200 | [diff] [blame] | 881 | |
| 882 | done: |
| 883 | if (td->o.create_only) |
| 884 | td->done = 1; |
| 885 | |
Jens Axboe | 7bb48f8 | 2007-03-27 15:30:28 +0200 | [diff] [blame] | 886 | return 0; |
ljzhang,Yaxin Hu,Jianchao Tang | bcdedd0 | 2007-07-27 13:28:26 +0200 | [diff] [blame] | 887 | err_offset: |
| 888 | log_err("%s: you need to specify valid offset=\n", td->o.name); |
| 889 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Zhang, Yanmin | afad68f | 2009-05-20 11:30:55 +0200 | [diff] [blame] | 892 | int pre_read_files(struct thread_data *td) |
| 893 | { |
| 894 | struct fio_file *f; |
| 895 | unsigned int i; |
| 896 | |
| 897 | dprint(FD_FILE, "pre_read files\n"); |
| 898 | |
| 899 | for_each_file(td, f, i) { |
| 900 | pre_read_file(td, f); |
| 901 | } |
| 902 | |
| 903 | return 1; |
| 904 | } |
| 905 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 906 | static int __init_rand_distribution(struct thread_data *td, struct fio_file *f) |
| 907 | { |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 908 | unsigned int range_size, seed; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 909 | unsigned long nranges; |
| 910 | |
| 911 | range_size = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]); |
| 912 | |
Jens Axboe | 898cf6c | 2013-01-04 08:26:10 +0100 | [diff] [blame] | 913 | nranges = (f->io_size + range_size - 1) / range_size; |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 914 | |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 915 | seed = jhash(f->file_name, strlen(f->file_name), 0) * td->thread_number; |
Jens Axboe | 8425687 | 2012-11-17 10:06:36 -0700 | [diff] [blame] | 916 | if (!td->o.rand_repeatable) |
| 917 | seed = td->rand_seeds[4]; |
| 918 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 919 | if (td->o.random_distribution == FIO_RAND_DIST_ZIPF) |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 920 | zipf_init(&f->zipf, nranges, td->o.zipf_theta, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 921 | else |
Jens Axboe | 2316296 | 2012-11-07 19:47:47 +0100 | [diff] [blame] | 922 | pareto_init(&f->zipf, nranges, td->o.pareto_h, seed); |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 923 | |
| 924 | return 1; |
| 925 | } |
| 926 | |
| 927 | static int init_rand_distribution(struct thread_data *td) |
| 928 | { |
| 929 | struct fio_file *f; |
| 930 | unsigned int i; |
| 931 | int state; |
| 932 | |
| 933 | if (td->o.random_distribution == FIO_RAND_DIST_RANDOM) |
| 934 | return 0; |
| 935 | |
| 936 | state = td->runstate; |
| 937 | td_set_runstate(td, TD_SETTING_UP); |
| 938 | for_each_file(td, f, i) |
| 939 | __init_rand_distribution(td, f); |
| 940 | td_set_runstate(td, state); |
| 941 | |
| 942 | return 1; |
| 943 | } |
| 944 | |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 945 | int init_random_map(struct thread_data *td) |
| 946 | { |
Jens Axboe | 51ede0b | 2012-11-22 13:50:29 +0100 | [diff] [blame] | 947 | unsigned long long blocks; |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 948 | struct fio_file *f; |
| 949 | unsigned int i; |
| 950 | |
Jens Axboe | 9c6f631 | 2012-11-07 09:15:45 +0100 | [diff] [blame] | 951 | if (init_rand_distribution(td)) |
| 952 | return 0; |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 953 | if (!td_random(td)) |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 954 | return 0; |
| 955 | |
| 956 | for_each_file(td, f, i) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 957 | blocks = (f->real_file_size + td->o.rw_min_bs - 1) / |
| 958 | (unsigned long long) td->o.rw_min_bs; |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 959 | if (td->o.random_generator == FIO_RAND_GEN_LFSR) { |
Jens Axboe | 82af46b | 2012-12-04 13:12:25 +0100 | [diff] [blame] | 960 | unsigned long seed; |
| 961 | |
| 962 | seed = td->rand_seeds[FIO_RAND_BLOCK_OFF]; |
| 963 | |
| 964 | if (!lfsr_init(&f->lfsr, blocks, seed)) |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 965 | continue; |
Jens Axboe | 3831a84 | 2012-11-26 19:59:14 +0100 | [diff] [blame] | 966 | } else if (!td->o.norandommap) { |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 967 | f->io_axmap = axmap_new(blocks); |
| 968 | if (f->io_axmap) |
Jens Axboe | 8055e41 | 2012-11-26 08:43:47 +0100 | [diff] [blame] | 969 | continue; |
Jens Axboe | 1cad712 | 2012-11-29 21:37:11 +0100 | [diff] [blame] | 970 | } else if (td->o.norandommap) |
| 971 | continue; |
Jens Axboe | ceadd59 | 2012-02-02 08:41:28 +0100 | [diff] [blame] | 972 | |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 973 | if (!td->o.softrandommap) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 974 | log_err("fio: failed allocating random map. If running" |
| 975 | " a large number of jobs, try the 'norandommap'" |
Jens Axboe | 2b386d2 | 2008-03-26 10:32:57 +0100 | [diff] [blame] | 976 | " option or set 'softrandommap'. Or give" |
| 977 | " a larger --alloc-size to fio.\n"); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 978 | return 1; |
| 979 | } |
Jens Axboe | 303032a | 2008-03-26 10:11:10 +0100 | [diff] [blame] | 980 | |
| 981 | log_info("fio: file %s failed allocating random map. Running " |
| 982 | "job without.\n", f->file_name); |
Jens Axboe | 6872707 | 2007-03-15 20:49:25 +0100 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | return 0; |
| 986 | } |
| 987 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 988 | void close_files(struct thread_data *td) |
| 989 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 990 | struct fio_file *f; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 991 | unsigned int i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 992 | |
Jens Axboe | 2be3eec | 2009-06-10 09:05:13 +0200 | [diff] [blame] | 993 | for_each_file(td, f, i) { |
| 994 | if (fio_file_open(f)) |
| 995 | td_io_close_file(td, f); |
| 996 | } |
Jens Axboe | 24ffd2c | 2008-03-01 15:47:08 +0100 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | void close_and_free_files(struct thread_data *td) |
| 1000 | { |
| 1001 | struct fio_file *f; |
| 1002 | unsigned int i; |
| 1003 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1004 | dprint(FD_FILE, "close files\n"); |
| 1005 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 1006 | for_each_file(td, f, i) { |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 1007 | if (td->o.unlink && f->filetype == FIO_TYPE_FILE) { |
| 1008 | dprint(FD_FILE, "free unlink %s\n", f->file_name); |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 1009 | unlink(f->file_name); |
Jens Axboe | ffdfabd | 2008-03-26 09:18:14 +0100 | [diff] [blame] | 1010 | } |
Jens Axboe | bdb4e2e | 2007-03-01 09:54:57 +0100 | [diff] [blame] | 1011 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1012 | if (fio_file_open(f)) |
| 1013 | td_io_close_file(td, f); |
| 1014 | |
Shaozhi Shawn Ye | b9fbcf2 | 2008-09-10 09:05:17 +0200 | [diff] [blame] | 1015 | remove_file_hash(f); |
Jens Axboe | b3dc7f0 | 2007-03-09 12:52:15 +0100 | [diff] [blame] | 1016 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1017 | sfree(f->file_name); |
Jens Axboe | fa1da86 | 2007-03-23 15:20:54 +0100 | [diff] [blame] | 1018 | f->file_name = NULL; |
Jens Axboe | 7ebd796 | 2012-11-28 21:24:46 +0100 | [diff] [blame] | 1019 | axmap_free(f->io_axmap); |
| 1020 | f->io_axmap = NULL; |
Jens Axboe | 78d99e6 | 2008-03-01 18:41:50 +0100 | [diff] [blame] | 1021 | sfree(f); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1022 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1023 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1024 | td->o.filename = NULL; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1025 | free(td->files); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1026 | td->files_index = 0; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 1027 | td->files = NULL; |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1028 | td->o.nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 1029 | } |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1030 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1031 | static void get_file_type(struct fio_file *f) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1032 | { |
| 1033 | struct stat sb; |
| 1034 | |
Jens Axboe | 6615982 | 2007-04-16 21:54:24 +0200 | [diff] [blame] | 1035 | if (!strcmp(f->file_name, "-")) |
| 1036 | f->filetype = FIO_TYPE_PIPE; |
| 1037 | else |
| 1038 | f->filetype = FIO_TYPE_FILE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1039 | |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1040 | /* \\.\ is the device namespace in Windows, where every file is |
| 1041 | * a block device */ |
| 1042 | if (strncmp(f->file_name, "\\\\.\\", 4) == 0) |
| 1043 | f->filetype = FIO_TYPE_BD; |
| 1044 | |
Jens Axboe | b30d395 | 2010-02-06 23:36:26 +0100 | [diff] [blame] | 1045 | if (!stat(f->file_name, &sb)) { |
Bruce Cran | 3892182 | 2012-02-22 17:55:16 +0000 | [diff] [blame] | 1046 | if (S_ISBLK(sb.st_mode)) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1047 | f->filetype = FIO_TYPE_BD; |
| 1048 | else if (S_ISCHR(sb.st_mode)) |
| 1049 | f->filetype = FIO_TYPE_CHAR; |
Jens Axboe | b5605e9 | 2007-04-16 20:42:33 +0200 | [diff] [blame] | 1050 | else if (S_ISFIFO(sb.st_mode)) |
| 1051 | f->filetype = FIO_TYPE_PIPE; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1055 | int add_file(struct thread_data *td, const char *fname) |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1056 | { |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1057 | int cur_files = td->files_index; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1058 | char file_name[PATH_MAX]; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1059 | struct fio_file *f; |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1060 | int len = 0; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1061 | |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1062 | dprint(FD_FILE, "add file %s\n", fname); |
| 1063 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1064 | f = smalloc(sizeof(*f)); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1065 | if (!f) { |
| 1066 | log_err("fio: smalloc OOM\n"); |
| 1067 | assert(0); |
| 1068 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1069 | |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1070 | f->fd = -1; |
Jens Axboe | e6c4d73 | 2012-12-12 08:16:27 +0100 | [diff] [blame] | 1071 | f->shadow_fd = -1; |
Jens Axboe | 38dad62 | 2010-07-20 14:46:00 -0600 | [diff] [blame] | 1072 | fio_file_reset(f); |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1073 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1074 | if (td->files_size <= td->files_index) { |
Carl Henrik Lunde | 4b341fc | 2009-04-20 08:41:37 +0200 | [diff] [blame] | 1075 | int new_size = td->o.nr_files + 1; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1076 | |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1077 | dprint(FD_FILE, "resize file array to %d files\n", new_size); |
| 1078 | |
| 1079 | td->files = realloc(td->files, new_size * sizeof(f)); |
Jens Axboe | fc99bc0 | 2009-03-04 15:27:42 +0100 | [diff] [blame] | 1080 | td->files_size = new_size; |
| 1081 | } |
Jens Axboe | 8bb7679 | 2009-03-04 15:37:52 +0100 | [diff] [blame] | 1082 | td->files[cur_files] = f; |
Shaohua Li | 89ac1d4 | 2012-06-11 08:56:32 +0200 | [diff] [blame] | 1083 | f->fileno = cur_files; |
Jens Axboe | 126d65c | 2008-03-01 18:04:31 +0100 | [diff] [blame] | 1084 | |
Jens Axboe | 07eb79d | 2007-04-12 13:02:38 +0200 | [diff] [blame] | 1085 | /* |
| 1086 | * init function, io engine may not be loaded yet |
| 1087 | */ |
| 1088 | if (td->io_ops && (td->io_ops->flags & FIO_DISKLESSIO)) |
| 1089 | f->real_file_size = -1ULL; |
| 1090 | |
Jens Axboe | bd0ee74 | 2007-03-23 14:26:23 +0100 | [diff] [blame] | 1091 | if (td->o.directory) |
| 1092 | len = sprintf(file_name, "%s/", td->o.directory); |
| 1093 | |
| 1094 | sprintf(file_name + len, "%s", fname); |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1095 | f->file_name = smalloc_strdup(file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1096 | if (!f->file_name) { |
| 1097 | log_err("fio: smalloc OOM\n"); |
| 1098 | assert(0); |
| 1099 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1100 | |
Jens Axboe | e3bab46 | 2007-03-13 11:22:09 +0100 | [diff] [blame] | 1101 | get_file_type(f); |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1102 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1103 | switch (td->o.file_lock_mode) { |
| 1104 | case FILE_LOCK_NONE: |
| 1105 | break; |
| 1106 | case FILE_LOCK_READWRITE: |
| 1107 | f->lock = fio_mutex_rw_init(); |
| 1108 | break; |
| 1109 | case FILE_LOCK_EXCLUSIVE: |
Jens Axboe | 521da52 | 2012-08-02 11:21:36 +0200 | [diff] [blame] | 1110 | f->lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1111 | break; |
| 1112 | default: |
| 1113 | log_err("fio: unknown lock mode: %d\n", td->o.file_lock_mode); |
| 1114 | assert(0); |
| 1115 | } |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1116 | |
Jens Axboe | 7b4e4fe | 2007-03-13 12:51:40 +0100 | [diff] [blame] | 1117 | td->files_index++; |
Jens Axboe | 1549441 | 2007-03-13 11:52:47 +0100 | [diff] [blame] | 1118 | if (f->filetype == FIO_TYPE_FILE) |
| 1119 | td->nr_normal_files++; |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1120 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1121 | dprint(FD_FILE, "file %p \"%s\" added at %d\n", f, f->file_name, |
| 1122 | cur_files); |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1123 | |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1124 | return cur_files; |
Jens Axboe | af52b34 | 2007-03-13 10:07:47 +0100 | [diff] [blame] | 1125 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1126 | |
Jens Axboe | 49ffb4a | 2010-08-24 15:01:37 +0200 | [diff] [blame] | 1127 | int add_file_exclusive(struct thread_data *td, const char *fname) |
| 1128 | { |
| 1129 | struct fio_file *f; |
| 1130 | unsigned int i; |
| 1131 | |
| 1132 | for_each_file(td, f, i) { |
| 1133 | if (!strcmp(f->file_name, fname)) |
| 1134 | return i; |
| 1135 | } |
| 1136 | |
| 1137 | return add_file(td, fname); |
| 1138 | } |
| 1139 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1140 | void get_file(struct fio_file *f) |
| 1141 | { |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1142 | dprint(FD_FILE, "get file %s, ref=%d\n", f->file_name, f->references); |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1143 | assert(fio_file_open(f)); |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1144 | f->references++; |
| 1145 | } |
| 1146 | |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1147 | int put_file(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1148 | { |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1149 | int f_ret = 0, ret = 0; |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1150 | |
Jens Axboe | 8172fe9 | 2008-02-01 21:51:02 +0100 | [diff] [blame] | 1151 | dprint(FD_FILE, "put file %s, ref=%d\n", f->file_name, f->references); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 1152 | |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1153 | if (!fio_file_open(f)) { |
| 1154 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1155 | return 0; |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1156 | } |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1157 | |
| 1158 | assert(f->references); |
| 1159 | if (--f->references) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1160 | return 0; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1161 | |
Jens Axboe | d424d4d | 2007-04-17 09:05:10 +0200 | [diff] [blame] | 1162 | if (should_fsync(td) && td->o.fsync_on_close) |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1163 | f_ret = fsync(f->fd); |
Jens Axboe | ebb1415 | 2007-03-13 14:42:15 +0100 | [diff] [blame] | 1164 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1165 | if (td->io_ops->close_file) |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1166 | ret = td->io_ops->close_file(td, f); |
Jens Axboe | 1020a13 | 2007-03-29 11:15:06 +0200 | [diff] [blame] | 1167 | |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1168 | if (!ret) |
Jens Axboe | a5fb461 | 2008-05-28 10:54:01 +0200 | [diff] [blame] | 1169 | ret = f_ret; |
Jens Axboe | 98e1ac4 | 2008-03-06 11:56:48 +0100 | [diff] [blame] | 1170 | |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1171 | td->nr_open_files--; |
Jens Axboe | d6aed79 | 2009-06-03 08:41:15 +0200 | [diff] [blame] | 1172 | fio_file_clear_open(f); |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1173 | assert(f->fd == -1); |
Jens Axboe | 6977bcd | 2008-03-01 15:55:36 +0100 | [diff] [blame] | 1174 | return ret; |
Jens Axboe | 0ad920e | 2007-03-13 11:06:45 +0100 | [diff] [blame] | 1175 | } |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1176 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1177 | void lock_file(struct thread_data *td, struct fio_file *f, enum fio_ddir ddir) |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1178 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1179 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1180 | return; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1181 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1182 | if (f->lock_owner == td && f->lock_batch--) |
| 1183 | return; |
| 1184 | |
| 1185 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
| 1186 | if (ddir == DDIR_READ) |
| 1187 | fio_mutex_down_read(f->lock); |
| 1188 | else |
| 1189 | fio_mutex_down_write(f->lock); |
| 1190 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) |
| 1191 | fio_mutex_down(f->lock); |
| 1192 | |
| 1193 | f->lock_owner = td; |
| 1194 | f->lock_batch = td->o.lockfile_batch; |
| 1195 | f->lock_ddir = ddir; |
| 1196 | } |
| 1197 | |
| 1198 | void unlock_file(struct thread_data *td, struct fio_file *f) |
| 1199 | { |
| 1200 | if (!f->lock || td->o.file_lock_mode == FILE_LOCK_NONE) |
| 1201 | return; |
| 1202 | if (f->lock_batch) |
| 1203 | return; |
| 1204 | |
| 1205 | if (td->o.file_lock_mode == FILE_LOCK_READWRITE) { |
| 1206 | const int is_read = f->lock_ddir == DDIR_READ; |
| 1207 | int val = fio_mutex_getval(f->lock); |
| 1208 | |
| 1209 | if ((is_read && val == 1) || (!is_read && val == -1)) |
| 1210 | f->lock_owner = NULL; |
| 1211 | |
| 1212 | if (is_read) |
| 1213 | fio_mutex_up_read(f->lock); |
| 1214 | else |
| 1215 | fio_mutex_up_write(f->lock); |
| 1216 | } else if (td->o.file_lock_mode == FILE_LOCK_EXCLUSIVE) { |
| 1217 | int val = fio_mutex_getval(f->lock); |
| 1218 | |
| 1219 | if (val == 0) |
| 1220 | f->lock_owner = NULL; |
| 1221 | |
| 1222 | fio_mutex_up(f->lock); |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1223 | } |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1224 | } |
| 1225 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1226 | void unlock_file_all(struct thread_data *td, struct fio_file *f) |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1227 | { |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1228 | if (f->lock_owner != td) |
| 1229 | return; |
Jens Axboe | 29c1349 | 2008-03-01 19:25:20 +0100 | [diff] [blame] | 1230 | |
Jens Axboe | 4d4e80f | 2008-03-04 10:18:56 +0100 | [diff] [blame] | 1231 | f->lock_batch = 0; |
| 1232 | unlock_file(td, f); |
Jens Axboe | b2bd2bd | 2008-03-01 18:19:52 +0100 | [diff] [blame] | 1233 | } |
| 1234 | |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1235 | static int recurse_dir(struct thread_data *td, const char *dirname) |
| 1236 | { |
| 1237 | struct dirent *dir; |
| 1238 | int ret = 0; |
| 1239 | DIR *D; |
| 1240 | |
| 1241 | D = opendir(dirname); |
| 1242 | if (!D) { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1243 | char buf[FIO_VERROR_SIZE]; |
| 1244 | |
| 1245 | snprintf(buf, FIO_VERROR_SIZE - 1, "opendir(%s)", dirname); |
| 1246 | td_verror(td, errno, buf); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1247 | return 1; |
| 1248 | } |
| 1249 | |
| 1250 | while ((dir = readdir(D)) != NULL) { |
| 1251 | char full_path[PATH_MAX]; |
| 1252 | struct stat sb; |
| 1253 | |
Jens Axboe | e85b2b8 | 2007-03-14 09:39:06 +0100 | [diff] [blame] | 1254 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) |
| 1255 | continue; |
Jens Axboe | 96d32d5 | 2007-03-14 09:16:09 +0100 | [diff] [blame] | 1256 | |
Bruce Cran | b9fd788 | 2012-02-20 17:01:46 +0000 | [diff] [blame] | 1257 | sprintf(full_path, "%s%s%s", dirname, FIO_OS_PATH_SEPARATOR, dir->d_name); |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1258 | |
| 1259 | if (lstat(full_path, &sb) == -1) { |
| 1260 | if (errno != ENOENT) { |
| 1261 | td_verror(td, errno, "stat"); |
| 1262 | return 1; |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | if (S_ISREG(sb.st_mode)) { |
| 1267 | add_file(td, full_path); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 1268 | td->o.nr_files++; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1269 | continue; |
| 1270 | } |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1271 | if (!S_ISDIR(sb.st_mode)) |
| 1272 | continue; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1273 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 1274 | ret = recurse_dir(td, full_path); |
| 1275 | if (ret) |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | closedir(D); |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | int add_dir_files(struct thread_data *td, const char *path) |
| 1284 | { |
Jens Axboe | 0ddb270 | 2007-03-27 19:43:53 +0200 | [diff] [blame] | 1285 | int ret = recurse_dir(td, path); |
| 1286 | |
| 1287 | if (!ret) |
| 1288 | log_info("fio: opendir added %d files\n", td->o.nr_files); |
| 1289 | |
| 1290 | return ret; |
Jens Axboe | bbf6b54 | 2007-03-13 15:28:55 +0100 | [diff] [blame] | 1291 | } |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1292 | |
| 1293 | void dup_files(struct thread_data *td, struct thread_data *org) |
| 1294 | { |
| 1295 | struct fio_file *f; |
| 1296 | unsigned int i; |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1297 | |
| 1298 | dprint(FD_FILE, "dup files: %d\n", org->files_index); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1299 | |
| 1300 | if (!org->files) |
| 1301 | return; |
| 1302 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1303 | td->files = malloc(org->files_index * sizeof(f)); |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1304 | |
Jens Axboe | 9efef3c | 2008-03-06 10:26:25 +0100 | [diff] [blame] | 1305 | for_each_file(org, f, i) { |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1306 | struct fio_file *__f; |
| 1307 | |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1308 | __f = smalloc(sizeof(*__f)); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1309 | if (!__f) { |
| 1310 | log_err("fio: smalloc OOM\n"); |
| 1311 | assert(0); |
| 1312 | } |
Jens Axboe | 22a57ba | 2009-06-09 14:34:35 +0200 | [diff] [blame] | 1313 | __f->fd = -1; |
Jens Axboe | 38dad62 | 2010-07-20 14:46:00 -0600 | [diff] [blame] | 1314 | fio_file_reset(__f); |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1315 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1316 | if (f->file_name) { |
Jens Axboe | f17c439 | 2008-03-01 18:40:46 +0100 | [diff] [blame] | 1317 | __f->file_name = smalloc_strdup(f->file_name); |
Jens Axboe | c48c0be | 2008-07-31 19:55:02 +0200 | [diff] [blame] | 1318 | if (!__f->file_name) { |
| 1319 | log_err("fio: smalloc OOM\n"); |
| 1320 | assert(0); |
| 1321 | } |
Jens Axboe | 0b9d69e | 2009-09-11 22:29:54 +0200 | [diff] [blame] | 1322 | |
Aaron Carroll | bc3456f | 2008-06-25 09:20:37 +0200 | [diff] [blame] | 1323 | __f->filetype = f->filetype; |
| 1324 | } |
Jens Axboe | b0fe421 | 2008-03-01 18:22:27 +0100 | [diff] [blame] | 1325 | |
| 1326 | td->files[i] = __f; |
Jens Axboe | cade3ef | 2007-03-23 15:29:45 +0100 | [diff] [blame] | 1327 | } |
| 1328 | } |
Jens Axboe | f29b25a | 2007-07-23 08:56:43 +0200 | [diff] [blame] | 1329 | |
| 1330 | /* |
| 1331 | * Returns the index that matches the filename, or -1 if not there |
| 1332 | */ |
| 1333 | int get_fileno(struct thread_data *td, const char *fname) |
| 1334 | { |
| 1335 | struct fio_file *f; |
| 1336 | unsigned int i; |
| 1337 | |
| 1338 | for_each_file(td, f, i) |
| 1339 | if (!strcmp(f->file_name, fname)) |
| 1340 | return i; |
| 1341 | |
| 1342 | return -1; |
| 1343 | } |
| 1344 | |
| 1345 | /* |
| 1346 | * For log usage, where we add/open/close files automatically |
| 1347 | */ |
| 1348 | void free_release_files(struct thread_data *td) |
| 1349 | { |
| 1350 | close_files(td); |
| 1351 | td->files_index = 0; |
| 1352 | td->nr_normal_files = 0; |
| 1353 | } |