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