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