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