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