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> |
| 5 | #include <sys/stat.h> |
| 6 | #include <sys/mman.h> |
| 7 | |
| 8 | #include "fio.h" |
| 9 | #include "os.h" |
| 10 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 11 | /* |
| 12 | * Check if the file exists and it's large enough. |
| 13 | */ |
| 14 | static int file_ok(struct thread_data *td, struct fio_file *f) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 15 | { |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 16 | struct stat st; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 17 | |
Jens Axboe | 4d9345a | 2007-02-07 13:14:57 +0100 | [diff] [blame] | 18 | if (td->filetype != FIO_TYPE_FILE || (td->io_ops->flags & FIO_NULLIO)) |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 19 | return 0; |
| 20 | |
Jens Axboe | fa01d13 | 2007-02-22 14:07:39 +0100 | [diff] [blame] | 21 | if (lstat(f->file_name, &st) == -1) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 22 | return 1; |
Jens Axboe | fa01d13 | 2007-02-22 14:07:39 +0100 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * if it's a special file, size is always ok for now |
| 26 | */ |
| 27 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) |
| 28 | return 0; |
| 29 | if (st.st_size < (off_t) f->file_size) |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 30 | return 1; |
| 31 | |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static int create_file(struct thread_data *td, struct fio_file *f) |
| 36 | { |
| 37 | unsigned long long left; |
| 38 | unsigned int bs; |
| 39 | char *b; |
| 40 | int r; |
Jens Axboe | b2a1519 | 2006-10-18 14:10:42 +0200 | [diff] [blame] | 41 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 42 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 43 | if (f->fd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 44 | td_verror(td, errno, "open"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 45 | return 1; |
| 46 | } |
| 47 | |
| 48 | if (ftruncate(f->fd, f->file_size) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 49 | td_verror(td, errno, "ftruncate"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 50 | goto err; |
| 51 | } |
| 52 | |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 53 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 54 | td_verror(td, errno, "posix_fallocate"); |
Jens Axboe | 40f8298 | 2006-10-25 11:21:05 +0200 | [diff] [blame] | 55 | goto err; |
| 56 | } |
| 57 | |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 58 | b = malloc(td->max_bs[DDIR_WRITE]); |
| 59 | memset(b, 0, td->max_bs[DDIR_WRITE]); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 60 | |
| 61 | left = f->file_size; |
| 62 | while (left && !td->terminate) { |
Jens Axboe | a00735e | 2006-11-03 08:58:08 +0100 | [diff] [blame] | 63 | bs = td->max_bs[DDIR_WRITE]; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 64 | if (bs > left) |
| 65 | bs = left; |
| 66 | |
| 67 | r = write(f->fd, b, bs); |
| 68 | |
| 69 | if (r == (int) bs) { |
| 70 | left -= bs; |
| 71 | continue; |
| 72 | } else { |
| 73 | if (r < 0) |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 74 | td_verror(td, errno, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 75 | else |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 76 | td_verror(td, EIO, "write"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 77 | |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (td->terminate) |
| 83 | unlink(f->file_name); |
| 84 | else if (td->create_fsync) |
| 85 | fsync(f->fd); |
| 86 | |
| 87 | free(b); |
| 88 | close(f->fd); |
| 89 | f->fd = -1; |
| 90 | return 0; |
| 91 | err: |
| 92 | close(f->fd); |
| 93 | f->fd = -1; |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | static int create_files(struct thread_data *td) |
| 98 | { |
| 99 | struct fio_file *f; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 100 | int i, err, need_create; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 101 | |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 102 | for_each_file(td, f, i) |
| 103 | f->file_size = td->total_file_size / td->nr_files; |
| 104 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 105 | /* |
| 106 | * unless specifically asked for overwrite, let normal io extend it |
| 107 | */ |
Jens Axboe | f697125 | 2006-11-15 10:00:31 +0100 | [diff] [blame] | 108 | if (!td->overwrite) |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 109 | return 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 110 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 111 | need_create = 0; |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 112 | if (td->filetype == FIO_TYPE_FILE) { |
| 113 | for_each_file(td, f, i) { |
| 114 | int file_there = !file_ok(td, f); |
| 115 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 116 | if (file_there && td_write(td) && !td->overwrite) { |
Jens Axboe | eff6861 | 2007-01-14 05:56:44 +0100 | [diff] [blame] | 117 | unlink(f->file_name); |
| 118 | file_there = 0; |
| 119 | } |
| 120 | |
| 121 | need_create += !file_there; |
| 122 | } |
| 123 | } |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 124 | |
| 125 | if (!need_create) |
| 126 | return 0; |
| 127 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 128 | if (!td->total_file_size) { |
| 129 | log_err("Need size for create\n"); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 130 | td_verror(td, EINVAL, "file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 131 | return 1; |
| 132 | } |
| 133 | |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 134 | temp_stall_ts = 1; |
Jens Axboe | 1e97cce | 2006-12-05 11:44:16 +0100 | [diff] [blame] | 135 | fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n", |
Jens Axboe | 13f8e2d | 2006-10-24 09:29:45 +0200 | [diff] [blame] | 136 | td->name, td->nr_uniq_files, |
| 137 | (td->total_file_size >> 20) / td->nr_uniq_files, |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 138 | td->total_file_size >> 20); |
| 139 | |
| 140 | err = 0; |
| 141 | for_each_file(td, f, i) { |
Jens Axboe | 9b031dc | 2006-12-15 16:01:54 +0100 | [diff] [blame] | 142 | /* |
| 143 | * Only unlink files that we created. |
| 144 | */ |
| 145 | f->unlink = 0; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 146 | if (file_ok(td, f)) { |
Jens Axboe | 9b031dc | 2006-12-15 16:01:54 +0100 | [diff] [blame] | 147 | f->unlink = td->unlink; |
Jens Axboe | 25205e9 | 2006-10-19 20:50:14 +0200 | [diff] [blame] | 148 | err = create_file(td, f); |
| 149 | if (err) |
| 150 | break; |
| 151 | } |
| 152 | } |
| 153 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 154 | temp_stall_ts = 0; |
| 155 | return err; |
| 156 | } |
| 157 | |
| 158 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 159 | { |
| 160 | struct stat st; |
| 161 | |
Jens Axboe | f4ee22c | 2007-02-10 18:57:18 +0100 | [diff] [blame] | 162 | /* |
| 163 | * if we are not doing real io, just pretend the file is as large |
| 164 | * as the size= given. this works fine with nrfiles > 1 as well, |
| 165 | * we only really care about it being at least as big as size= |
| 166 | */ |
| 167 | if (td->io_ops->flags & FIO_NULLIO) { |
| 168 | f->real_file_size = f->file_size = td->total_file_size; |
| 169 | return 0; |
| 170 | } |
| 171 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 172 | if (td->overwrite) { |
| 173 | if (fstat(f->fd, &st) == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 174 | td_verror(td, errno, "fstat"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 175 | return 1; |
| 176 | } |
| 177 | |
| 178 | f->real_file_size = st.st_size; |
| 179 | |
| 180 | if (!f->file_size || f->file_size > f->real_file_size) |
| 181 | f->file_size = f->real_file_size; |
Jens Axboe | 745508d | 2007-01-08 10:25:51 +0100 | [diff] [blame] | 182 | } else |
| 183 | f->real_file_size = f->file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 184 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 189 | { |
| 190 | unsigned long long bytes; |
| 191 | int r; |
| 192 | |
| 193 | r = blockdev_size(f->fd, &bytes); |
| 194 | if (r) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 195 | td_verror(td, r, "blockdev_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | f->real_file_size = bytes; |
| 200 | |
| 201 | /* |
| 202 | * no extend possibilities, so limit size to device size if too large |
| 203 | */ |
| 204 | if (!f->file_size || f->file_size > f->real_file_size) |
| 205 | f->file_size = f->real_file_size; |
| 206 | |
| 207 | f->file_size -= f->file_offset; |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 212 | { |
| 213 | int ret = 0; |
| 214 | |
| 215 | if (td->filetype == FIO_TYPE_FILE) |
| 216 | ret = file_size(td, f); |
| 217 | else if (td->filetype == FIO_TYPE_BD) |
| 218 | ret = bdev_size(td, f); |
| 219 | else |
| 220 | f->real_file_size = -1; |
| 221 | |
| 222 | if (ret) |
| 223 | return ret; |
| 224 | |
| 225 | if (f->file_offset > f->real_file_size) { |
| 226 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); |
| 227 | return 1; |
| 228 | } |
| 229 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 233 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 234 | { |
| 235 | int ret = 0; |
| 236 | |
| 237 | /* |
| 238 | * FIXME: add blockdev flushing too |
| 239 | */ |
| 240 | if (td->io_ops->flags & FIO_MMAPIO) |
| 241 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 242 | else if (td->filetype == FIO_TYPE_FILE) { |
| 243 | if (!td->odirect) |
| 244 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
| 245 | } else if (td->filetype == FIO_TYPE_BD) { |
| 246 | if (!td->odirect) |
| 247 | ret = blockdev_invalidate_cache(f->fd); |
| 248 | } else if (td->filetype == FIO_TYPE_CHAR) |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 249 | ret = 0; |
| 250 | |
| 251 | if (ret < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 252 | td_verror(td, errno, "invalidate_cache"); |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 253 | return 1; |
| 254 | } |
| 255 | |
Jens Axboe | ad2da60 | 2007-02-26 12:33:27 +0100 | [diff] [blame] | 256 | return ret; |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 257 | } |
| 258 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 259 | static int __setup_file_mmap(struct thread_data *td, struct fio_file *f) |
| 260 | { |
| 261 | int flags; |
| 262 | |
| 263 | if (td_rw(td)) |
| 264 | flags = PROT_READ | PROT_WRITE; |
| 265 | else if (td_write(td)) { |
| 266 | flags = PROT_WRITE; |
| 267 | |
| 268 | if (td->verify != VERIFY_NONE) |
| 269 | flags |= PROT_READ; |
| 270 | } else |
| 271 | flags = PROT_READ; |
| 272 | |
| 273 | f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset); |
| 274 | if (f->mmap == MAP_FAILED) { |
| 275 | f->mmap = NULL; |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 276 | td_verror(td, errno, "mmap"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 277 | return 1; |
| 278 | } |
| 279 | |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 280 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
| 281 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 282 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 283 | if (!td_random(td)) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 284 | if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 285 | td_verror(td, errno, "madvise"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | } else { |
| 289 | if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 290 | td_verror(td, errno, "madvise"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 291 | return 1; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static int setup_files_mmap(struct thread_data *td) |
| 299 | { |
| 300 | struct fio_file *f; |
| 301 | int i, err = 0; |
| 302 | |
| 303 | for_each_file(td, f, i) { |
| 304 | err = __setup_file_mmap(td, f); |
| 305 | if (err) |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | return err; |
| 310 | } |
| 311 | |
| 312 | static int __setup_file_plain(struct thread_data *td, struct fio_file *f) |
| 313 | { |
Jens Axboe | e5b401d | 2006-10-18 16:03:40 +0200 | [diff] [blame] | 314 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
| 315 | return 1; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 316 | |
Jens Axboe | 413dd45 | 2007-02-23 09:26:09 +0100 | [diff] [blame] | 317 | if (!td_random(td)) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 318 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 319 | td_verror(td, errno, "fadvise"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 320 | return 1; |
| 321 | } |
| 322 | } else { |
| 323 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 324 | td_verror(td, errno, "fadvise"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 325 | return 1; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int setup_files_plain(struct thread_data *td) |
| 333 | { |
| 334 | struct fio_file *f; |
| 335 | int i, err = 0; |
| 336 | |
| 337 | for_each_file(td, f, i) { |
| 338 | err = __setup_file_plain(td, f); |
| 339 | if (err) |
| 340 | break; |
| 341 | } |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | static int setup_file(struct thread_data *td, struct fio_file *f) |
| 347 | { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 348 | int flags = 0; |
| 349 | |
Jens Axboe | ed92ac0 | 2007-02-06 14:43:52 +0100 | [diff] [blame] | 350 | if (td->io_ops->flags & FIO_NETIO) |
| 351 | return 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 352 | |
Jens Axboe | 4d9345a | 2007-02-07 13:14:57 +0100 | [diff] [blame] | 353 | /* |
| 354 | * we need a valid file descriptor, but don't create a real file. |
| 355 | * lets just dup stdout, seems like a sensible approach. |
| 356 | */ |
| 357 | if (td->io_ops->flags & FIO_NULLIO) |
| 358 | f->fd = dup(STDOUT_FILENO); |
| 359 | else { |
| 360 | if (td->odirect) |
| 361 | flags |= OS_O_DIRECT; |
| 362 | if (td->sync_io) |
| 363 | flags |= O_SYNC; |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 364 | |
Jens Axboe | 4d9345a | 2007-02-07 13:14:57 +0100 | [diff] [blame] | 365 | if (td_write(td) || td_rw(td)) { |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 366 | flags |= O_RDWR; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 367 | |
Jens Axboe | 4d9345a | 2007-02-07 13:14:57 +0100 | [diff] [blame] | 368 | if (td->filetype == FIO_TYPE_FILE) { |
| 369 | if (!td->overwrite) |
| 370 | flags |= O_TRUNC; |
| 371 | |
| 372 | flags |= O_CREAT; |
| 373 | } |
| 374 | |
| 375 | f->fd = open(f->file_name, flags, 0600); |
| 376 | } else { |
| 377 | if (td->filetype == FIO_TYPE_CHAR) |
| 378 | flags |= O_RDWR; |
| 379 | else |
| 380 | flags |= O_RDONLY; |
| 381 | |
| 382 | f->fd = open(f->file_name, flags); |
| 383 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | if (f->fd == -1) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 387 | int __e = errno; |
| 388 | |
| 389 | td_verror(td, __e, "open"); |
| 390 | if (__e == EINVAL && td->odirect) |
Jens Axboe | 9d80e11 | 2007-02-22 19:37:53 +0100 | [diff] [blame] | 391 | log_err("fio: destination does not support O_DIRECT\n"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 392 | return 1; |
| 393 | } |
| 394 | |
| 395 | if (get_file_size(td, f)) |
| 396 | return 1; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 401 | int open_files(struct thread_data *td) |
| 402 | { |
| 403 | struct fio_file *f; |
| 404 | int i, err = 0; |
| 405 | |
| 406 | for_each_file(td, f, i) { |
| 407 | err = setup_file(td, f); |
| 408 | if (err) |
| 409 | break; |
| 410 | } |
| 411 | |
Jens Axboe | 7abf833 | 2006-11-23 15:01:19 +0100 | [diff] [blame] | 412 | if (!err) |
| 413 | return 0; |
| 414 | |
| 415 | for_each_file(td, f, i) { |
| 416 | if (f->fd != -1) { |
| 417 | close(f->fd); |
| 418 | f->fd = -1; |
| 419 | } |
| 420 | } |
| 421 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 422 | return err; |
| 423 | } |
| 424 | |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 425 | int setup_files(struct thread_data *td) |
| 426 | { |
| 427 | struct fio_file *f; |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 428 | int err, i; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * if ioengine defines a setup() method, it's responsible for |
| 432 | * setting up everything in the td->files[] area. |
| 433 | */ |
| 434 | if (td->io_ops->setup) |
| 435 | return td->io_ops->setup(td); |
| 436 | |
| 437 | if (create_files(td)) |
| 438 | return 1; |
| 439 | |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 440 | err = open_files(td); |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 441 | if (err) |
| 442 | return err; |
| 443 | |
Jens Axboe | 0a7eb12 | 2006-10-20 10:36:42 +0200 | [diff] [blame] | 444 | /* |
| 445 | * Recalculate the total file size now that files are set up. |
| 446 | */ |
| 447 | td->total_file_size = 0; |
| 448 | for_each_file(td, f, i) |
| 449 | td->total_file_size += f->file_size; |
| 450 | |
Jens Axboe | f102706 | 2006-10-20 10:14:01 +0200 | [diff] [blame] | 451 | td->io_size = td->total_file_size; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 452 | if (td->io_size == 0) { |
| 453 | log_err("%s: no io blocks\n", td->name); |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 454 | td_verror(td, EINVAL, "total_file_size"); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 455 | return 1; |
| 456 | } |
| 457 | |
| 458 | if (!td->zone_size) |
| 459 | td->zone_size = td->io_size; |
| 460 | |
| 461 | td->total_io_size = td->io_size * td->loops; |
| 462 | |
| 463 | if (td->io_ops->flags & FIO_MMAPIO) |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 464 | err = setup_files_mmap(td); |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 465 | else |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 466 | err = setup_files_plain(td); |
| 467 | |
| 468 | for_each_file(td, f, i) { |
| 469 | if (f->fd != -1) { |
| 470 | close(f->fd); |
| 471 | f->fd = -1; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | return err; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void close_files(struct thread_data *td) |
| 479 | { |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 480 | struct fio_file *f; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 481 | int i; |
| 482 | |
Jens Axboe | 0ab8db8 | 2006-10-18 17:16:23 +0200 | [diff] [blame] | 483 | for_each_file(td, f, i) { |
Jens Axboe | 9b031dc | 2006-12-15 16:01:54 +0100 | [diff] [blame] | 484 | if (!td->filename && f->unlink && |
Jens Axboe | 02bcaa8 | 2006-11-24 10:42:00 +0100 | [diff] [blame] | 485 | td->filetype == FIO_TYPE_FILE) { |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 486 | unlink(f->file_name); |
| 487 | free(f->file_name); |
| 488 | f->file_name = NULL; |
| 489 | } |
Jens Axboe | 21972cd | 2006-11-23 12:39:16 +0100 | [diff] [blame] | 490 | if (f->fd != -1) { |
| 491 | close(f->fd); |
| 492 | f->fd = -1; |
| 493 | } |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 494 | if (f->mmap) { |
| 495 | munmap(f->mmap, f->file_size); |
| 496 | f->mmap = NULL; |
| 497 | } |
| 498 | } |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 499 | |
Jens Axboe | 132ad46 | 2006-11-23 12:23:12 +0100 | [diff] [blame] | 500 | td->filename = NULL; |
Jens Axboe | b4a6a59 | 2006-10-20 13:54:47 +0200 | [diff] [blame] | 501 | free(td->files); |
| 502 | td->files = NULL; |
| 503 | td->nr_files = 0; |
Jens Axboe | 53cdc68 | 2006-10-18 11:50:58 +0200 | [diff] [blame] | 504 | } |