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 | |
| 11 | static int create_file(struct thread_data *td, struct fio_file *f) |
| 12 | { |
| 13 | unsigned long long left; |
| 14 | unsigned int bs; |
| 15 | char *b; |
| 16 | int r; |
| 17 | |
| 18 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 19 | if (f->fd < 0) { |
| 20 | td_verror(td, errno); |
| 21 | return 1; |
| 22 | } |
| 23 | |
| 24 | if (ftruncate(f->fd, f->file_size) == -1) { |
| 25 | td_verror(td, errno); |
| 26 | goto err; |
| 27 | } |
| 28 | |
| 29 | b = malloc(td->max_bs); |
| 30 | memset(b, 0, td->max_bs); |
| 31 | |
| 32 | left = f->file_size; |
| 33 | while (left && !td->terminate) { |
| 34 | bs = td->max_bs; |
| 35 | if (bs > left) |
| 36 | bs = left; |
| 37 | |
| 38 | r = write(f->fd, b, bs); |
| 39 | |
| 40 | if (r == (int) bs) { |
| 41 | left -= bs; |
| 42 | continue; |
| 43 | } else { |
| 44 | if (r < 0) |
| 45 | td_verror(td, errno); |
| 46 | else |
| 47 | td_verror(td, EIO); |
| 48 | |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if (td->terminate) |
| 54 | unlink(f->file_name); |
| 55 | else if (td->create_fsync) |
| 56 | fsync(f->fd); |
| 57 | |
| 58 | free(b); |
| 59 | close(f->fd); |
| 60 | f->fd = -1; |
| 61 | return 0; |
| 62 | err: |
| 63 | close(f->fd); |
| 64 | f->fd = -1; |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | static int create_files(struct thread_data *td) |
| 69 | { |
| 70 | struct fio_file *f; |
| 71 | int i, err; |
| 72 | |
| 73 | /* |
| 74 | * unless specifically asked for overwrite, let normal io extend it |
| 75 | */ |
| 76 | if (!td->overwrite) { |
| 77 | td->io_size = td->total_file_size; |
| 78 | for_each_file(td, f, i) |
| 79 | f->file_size = td->total_file_size / td->nr_files; |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | if (!td->total_file_size) { |
| 85 | log_err("Need size for create\n"); |
| 86 | td_verror(td, EINVAL); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | temp_stall_ts = 1; |
| 91 | fprintf(f_out, "%s: Laying out IO file(s) (%LuMiB)\n", |
| 92 | td->name, td->total_file_size >> 20); |
| 93 | |
| 94 | err = 0; |
| 95 | for_each_file(td, f, i) { |
| 96 | f->file_size = td->total_file_size / td->nr_files; |
| 97 | err = create_file(td, f); |
| 98 | break; |
| 99 | |
| 100 | td->io_size += f->file_size; |
| 101 | } |
| 102 | |
| 103 | temp_stall_ts = 0; |
| 104 | return err; |
| 105 | } |
| 106 | |
| 107 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 108 | { |
| 109 | struct stat st; |
| 110 | |
| 111 | if (td->overwrite) { |
| 112 | if (fstat(f->fd, &st) == -1) { |
| 113 | td_verror(td, errno); |
| 114 | return 1; |
| 115 | } |
| 116 | |
| 117 | f->real_file_size = st.st_size; |
| 118 | |
| 119 | if (!f->file_size || f->file_size > f->real_file_size) |
| 120 | f->file_size = f->real_file_size; |
| 121 | } |
| 122 | |
| 123 | f->file_size -= f->file_offset; |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 128 | { |
| 129 | unsigned long long bytes; |
| 130 | int r; |
| 131 | |
| 132 | r = blockdev_size(f->fd, &bytes); |
| 133 | if (r) { |
| 134 | td_verror(td, r); |
| 135 | return 1; |
| 136 | } |
| 137 | |
| 138 | f->real_file_size = bytes; |
| 139 | |
| 140 | /* |
| 141 | * no extend possibilities, so limit size to device size if too large |
| 142 | */ |
| 143 | if (!f->file_size || f->file_size > f->real_file_size) |
| 144 | f->file_size = f->real_file_size; |
| 145 | |
| 146 | f->file_size -= f->file_offset; |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 151 | { |
| 152 | int ret = 0; |
| 153 | |
| 154 | if (td->filetype == FIO_TYPE_FILE) |
| 155 | ret = file_size(td, f); |
| 156 | else if (td->filetype == FIO_TYPE_BD) |
| 157 | ret = bdev_size(td, f); |
| 158 | else |
| 159 | f->real_file_size = -1; |
| 160 | |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | |
| 164 | if (f->file_offset > f->real_file_size) { |
| 165 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | td->io_size += f->file_size; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int __setup_file_mmap(struct thread_data *td, struct fio_file *f) |
| 174 | { |
| 175 | int flags; |
| 176 | |
| 177 | if (td_rw(td)) |
| 178 | flags = PROT_READ | PROT_WRITE; |
| 179 | else if (td_write(td)) { |
| 180 | flags = PROT_WRITE; |
| 181 | |
| 182 | if (td->verify != VERIFY_NONE) |
| 183 | flags |= PROT_READ; |
| 184 | } else |
| 185 | flags = PROT_READ; |
| 186 | |
| 187 | f->mmap = mmap(NULL, f->file_size, flags, MAP_SHARED, f->fd, f->file_offset); |
| 188 | if (f->mmap == MAP_FAILED) { |
| 189 | f->mmap = NULL; |
| 190 | td_verror(td, errno); |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | if (td->invalidate_cache) { |
| 195 | if (madvise(f->mmap, f->file_size, MADV_DONTNEED) < 0) { |
| 196 | td_verror(td, errno); |
| 197 | return 1; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (td->sequential) { |
| 202 | if (madvise(f->mmap, f->file_size, MADV_SEQUENTIAL) < 0) { |
| 203 | td_verror(td, errno); |
| 204 | return 1; |
| 205 | } |
| 206 | } else { |
| 207 | if (madvise(f->mmap, f->file_size, MADV_RANDOM) < 0) { |
| 208 | td_verror(td, errno); |
| 209 | return 1; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int setup_files_mmap(struct thread_data *td) |
| 217 | { |
| 218 | struct fio_file *f; |
| 219 | int i, err = 0; |
| 220 | |
| 221 | for_each_file(td, f, i) { |
| 222 | err = __setup_file_mmap(td, f); |
| 223 | if (err) |
| 224 | break; |
| 225 | } |
| 226 | |
| 227 | return err; |
| 228 | } |
| 229 | |
| 230 | static int __setup_file_plain(struct thread_data *td, struct fio_file *f) |
| 231 | { |
| 232 | if (td->invalidate_cache) { |
| 233 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED) < 0) { |
| 234 | td_verror(td, errno); |
| 235 | return 1; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | if (td->sequential) { |
| 240 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 241 | td_verror(td, errno); |
| 242 | return 1; |
| 243 | } |
| 244 | } else { |
| 245 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { |
| 246 | td_verror(td, errno); |
| 247 | return 1; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static int setup_files_plain(struct thread_data *td) |
| 255 | { |
| 256 | struct fio_file *f; |
| 257 | int i, err = 0; |
| 258 | |
| 259 | for_each_file(td, f, i) { |
| 260 | err = __setup_file_plain(td, f); |
| 261 | if (err) |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | return err; |
| 266 | } |
| 267 | |
| 268 | static int setup_file(struct thread_data *td, struct fio_file *f) |
| 269 | { |
| 270 | struct stat st; |
| 271 | int flags = 0; |
| 272 | |
| 273 | if (stat(f->file_name, &st) == -1) { |
| 274 | if (errno != ENOENT) { |
| 275 | td_verror(td, errno); |
| 276 | return 1; |
| 277 | } |
| 278 | if (!td->create_file) { |
| 279 | td_verror(td, ENOENT); |
| 280 | return 1; |
| 281 | } |
| 282 | if (create_file(td, f)) |
| 283 | return 1; |
| 284 | } else if (td->filetype == FIO_TYPE_FILE && |
| 285 | st.st_size < (off_t) f->file_size) { |
| 286 | if (create_file(td, f)) |
| 287 | return 1; |
| 288 | } |
| 289 | |
| 290 | if (td->odirect) |
| 291 | flags |= OS_O_DIRECT; |
| 292 | |
| 293 | if (td_write(td) || td_rw(td)) { |
| 294 | if (td->filetype == FIO_TYPE_FILE) { |
| 295 | if (!td->overwrite) |
| 296 | flags |= O_TRUNC; |
| 297 | |
| 298 | flags |= O_CREAT; |
| 299 | } |
| 300 | if (td->sync_io) |
| 301 | flags |= O_SYNC; |
| 302 | |
| 303 | flags |= O_RDWR; |
| 304 | |
| 305 | f->fd = open(f->file_name, flags, 0600); |
| 306 | } else { |
| 307 | if (td->filetype == FIO_TYPE_CHAR) |
| 308 | flags |= O_RDWR; |
| 309 | else |
| 310 | flags |= O_RDONLY; |
| 311 | |
| 312 | f->fd = open(f->file_name, flags); |
| 313 | } |
| 314 | |
| 315 | if (f->fd == -1) { |
| 316 | td_verror(td, errno); |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | if (get_file_size(td, f)) |
| 321 | return 1; |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int setup_files(struct thread_data *td) |
| 327 | { |
| 328 | struct fio_file *f; |
| 329 | int i, err; |
| 330 | |
| 331 | /* |
| 332 | * if ioengine defines a setup() method, it's responsible for |
| 333 | * setting up everything in the td->files[] area. |
| 334 | */ |
| 335 | if (td->io_ops->setup) |
| 336 | return td->io_ops->setup(td); |
| 337 | |
| 338 | if (create_files(td)) |
| 339 | return 1; |
| 340 | |
| 341 | for_each_file(td, f, i) { |
| 342 | err = setup_file(td, f); |
| 343 | if (err) |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | if (td->io_size == 0) { |
| 348 | log_err("%s: no io blocks\n", td->name); |
| 349 | td_verror(td, EINVAL); |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | if (!td->zone_size) |
| 354 | td->zone_size = td->io_size; |
| 355 | |
| 356 | td->total_io_size = td->io_size * td->loops; |
| 357 | |
| 358 | if (td->io_ops->flags & FIO_MMAPIO) |
| 359 | return setup_files_mmap(td); |
| 360 | else |
| 361 | return setup_files_plain(td); |
| 362 | } |
| 363 | |
| 364 | void close_files(struct thread_data *td) |
| 365 | { |
| 366 | int i; |
| 367 | |
| 368 | for (i = 0; i < td->nr_files; i++) { |
| 369 | struct fio_file *f = &td->files[i]; |
| 370 | |
| 371 | if (f->fd != -1) { |
| 372 | close(f->fd); |
| 373 | f->fd = -1; |
| 374 | } |
| 375 | if (f->mmap) { |
| 376 | munmap(f->mmap, f->file_size); |
| 377 | f->mmap = NULL; |
| 378 | } |
| 379 | } |
| 380 | } |