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