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