| 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 | /* |
| 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) |
| 15 | { |
| 16 | struct stat st; |
| 17 | |
| 18 | if (f->filetype != FIO_TYPE_FILE || |
| 19 | (td->io_ops->flags & FIO_DISKLESSIO)) |
| 20 | return 0; |
| 21 | |
| 22 | if (lstat(f->file_name, &st) == -1) |
| 23 | return 1; |
| 24 | |
| 25 | /* |
| 26 | * if it's a special file, size is always ok for now |
| 27 | */ |
| 28 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) |
| 29 | return 0; |
| 30 | if (st.st_size < (off_t) f->file_size) |
| 31 | return 1; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | static int create_file(struct thread_data *td, struct fio_file *f) |
| 37 | { |
| 38 | unsigned long long left; |
| 39 | unsigned int bs; |
| 40 | char *b; |
| 41 | int r; |
| 42 | |
| 43 | f->fd = open(f->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 44 | if (f->fd < 0) { |
| 45 | td_verror(td, errno, "open"); |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | if (ftruncate(f->fd, f->file_size) == -1) { |
| 50 | td_verror(td, errno, "ftruncate"); |
| 51 | goto err; |
| 52 | } |
| 53 | |
| 54 | if (posix_fallocate(f->fd, 0, f->file_size) < 0) { |
| 55 | td_verror(td, errno, "posix_fallocate"); |
| 56 | goto err; |
| 57 | } |
| 58 | |
| 59 | b = malloc(td->max_bs[DDIR_WRITE]); |
| 60 | memset(b, 0, td->max_bs[DDIR_WRITE]); |
| 61 | |
| 62 | left = f->file_size; |
| 63 | while (left && !td->terminate) { |
| 64 | bs = td->max_bs[DDIR_WRITE]; |
| 65 | if (bs > left) |
| 66 | bs = left; |
| 67 | |
| 68 | r = write(f->fd, b, bs); |
| 69 | |
| 70 | if (r == (int) bs) { |
| 71 | left -= bs; |
| 72 | continue; |
| 73 | } else { |
| 74 | if (r < 0) |
| 75 | td_verror(td, errno, "write"); |
| 76 | else |
| 77 | td_verror(td, EIO, "write"); |
| 78 | |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | if (td->terminate) |
| 84 | unlink(f->file_name); |
| 85 | else if (td->create_fsync) |
| 86 | fsync(f->fd); |
| 87 | |
| 88 | free(b); |
| 89 | close(f->fd); |
| 90 | f->fd = -1; |
| 91 | return 0; |
| 92 | err: |
| 93 | close(f->fd); |
| 94 | f->fd = -1; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | static int create_files(struct thread_data *td) |
| 99 | { |
| 100 | struct fio_file *f; |
| 101 | int err, need_create, can_extend; |
| 102 | unsigned int i; |
| 103 | |
| 104 | for_each_file(td, f, i) |
| 105 | f->file_size = td->total_file_size / td->nr_files; |
| 106 | |
| 107 | /* |
| 108 | * unless specifically asked for overwrite, let normal io extend it |
| 109 | */ |
| 110 | can_extend = !td->overwrite && !(td->io_ops->flags & FIO_NOEXTEND); |
| 111 | if (can_extend) |
| 112 | return 0; |
| 113 | |
| 114 | need_create = 0; |
| 115 | for_each_file(td, f, i) { |
| 116 | int file_there; |
| 117 | |
| 118 | if (f->filetype != FIO_TYPE_FILE) |
| 119 | continue; |
| 120 | |
| 121 | file_there = !file_ok(td, f); |
| 122 | |
| 123 | if (file_there && td_write(td) && !td->overwrite) { |
| 124 | unlink(f->file_name); |
| 125 | file_there = 0; |
| 126 | } |
| 127 | |
| 128 | need_create += !file_there; |
| 129 | } |
| 130 | |
| 131 | if (!need_create) |
| 132 | return 0; |
| 133 | |
| 134 | if (!td->total_file_size) { |
| 135 | log_err("Need size for create\n"); |
| 136 | td_verror(td, EINVAL, "file_size"); |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | temp_stall_ts = 1; |
| 141 | fprintf(f_out, "%s: Laying out IO file(s) (%u x %LuMiB == %LuMiB)\n", |
| 142 | td->name, td->nr_uniq_files, |
| 143 | (td->total_file_size >> 20) / td->nr_uniq_files, |
| 144 | td->total_file_size >> 20); |
| 145 | |
| 146 | err = 0; |
| 147 | for_each_file(td, f, i) { |
| 148 | /* |
| 149 | * Only unlink files that we created. |
| 150 | */ |
| 151 | f->flags &= ~FIO_FILE_UNLINK; |
| 152 | if (file_ok(td, f)) { |
| 153 | if (td->unlink) |
| 154 | f->flags |= FIO_FILE_UNLINK; |
| 155 | |
| 156 | err = create_file(td, f); |
| 157 | if (err) |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | temp_stall_ts = 0; |
| 163 | return err; |
| 164 | } |
| 165 | |
| 166 | static int file_size(struct thread_data *td, struct fio_file *f) |
| 167 | { |
| 168 | struct stat st; |
| 169 | |
| 170 | if (td->overwrite) { |
| 171 | if (fstat(f->fd, &st) == -1) { |
| 172 | td_verror(td, errno, "fstat"); |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | f->real_file_size = st.st_size; |
| 177 | |
| 178 | if (!f->file_size || f->file_size > f->real_file_size) |
| 179 | f->file_size = f->real_file_size; |
| 180 | } else |
| 181 | f->real_file_size = f->file_size; |
| 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | static int bdev_size(struct thread_data *td, struct fio_file *f) |
| 187 | { |
| 188 | unsigned long long bytes; |
| 189 | int r; |
| 190 | |
| 191 | r = blockdev_size(f->fd, &bytes); |
| 192 | if (r) { |
| 193 | td_verror(td, r, "blockdev_size"); |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | f->real_file_size = bytes; |
| 198 | |
| 199 | /* |
| 200 | * no extend possibilities, so limit size to device size if too large |
| 201 | */ |
| 202 | if (!f->file_size || f->file_size > f->real_file_size) |
| 203 | f->file_size = f->real_file_size; |
| 204 | |
| 205 | f->file_size -= f->file_offset; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int get_file_size(struct thread_data *td, struct fio_file *f) |
| 210 | { |
| 211 | int ret = 0; |
| 212 | |
| 213 | if (f->filetype == FIO_TYPE_FILE) |
| 214 | ret = file_size(td, f); |
| 215 | else if (f->filetype == FIO_TYPE_BD) |
| 216 | ret = bdev_size(td, f); |
| 217 | else |
| 218 | f->real_file_size = -1; |
| 219 | |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | if (f->file_offset > f->real_file_size) { |
| 224 | log_err("%s: offset extends end (%Lu > %Lu)\n", td->name, f->file_offset, f->real_file_size); |
| 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | int file_invalidate_cache(struct thread_data *td, struct fio_file *f) |
| 232 | { |
| 233 | int ret = 0; |
| 234 | |
| 235 | if (td->odirect) |
| 236 | return 0; |
| 237 | |
| 238 | /* |
| 239 | * FIXME: add blockdev flushing too |
| 240 | */ |
| 241 | if (f->mmap) |
| 242 | ret = madvise(f->mmap, f->file_size, MADV_DONTNEED); |
| 243 | else if (f->filetype == FIO_TYPE_FILE) { |
| 244 | ret = fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_DONTNEED); |
| 245 | } else if (f->filetype == FIO_TYPE_BD) { |
| 246 | ret = blockdev_invalidate_cache(f->fd); |
| 247 | } else if (f->filetype == FIO_TYPE_CHAR) |
| 248 | ret = 0; |
| 249 | |
| 250 | if (ret < 0) { |
| 251 | td_verror(td, errno, "invalidate_cache"); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | void generic_close_file(struct thread_data fio_unused *td, struct fio_file *f) |
| 259 | { |
| 260 | close(f->fd); |
| 261 | f->fd = -1; |
| 262 | } |
| 263 | |
| 264 | int generic_open_file(struct thread_data *td, struct fio_file *f) |
| 265 | { |
| 266 | int flags = 0; |
| 267 | |
| 268 | if (td->odirect) |
| 269 | flags |= OS_O_DIRECT; |
| 270 | if (td->sync_io) |
| 271 | flags |= O_SYNC; |
| 272 | |
| 273 | if (td_write(td) || td_rw(td)) { |
| 274 | flags |= O_RDWR; |
| 275 | |
| 276 | if (f->filetype == FIO_TYPE_FILE) |
| 277 | flags |= O_CREAT; |
| 278 | |
| 279 | f->fd = open(f->file_name, flags, 0600); |
| 280 | } else { |
| 281 | if (f->filetype == FIO_TYPE_CHAR) |
| 282 | flags |= O_RDWR; |
| 283 | else |
| 284 | flags |= O_RDONLY; |
| 285 | |
| 286 | f->fd = open(f->file_name, flags); |
| 287 | } |
| 288 | |
| 289 | if (f->fd == -1) { |
| 290 | int __e = errno; |
| 291 | |
| 292 | td_verror(td, __e, "open"); |
| 293 | if (__e == EINVAL && td->odirect) |
| 294 | log_err("fio: destination does not support O_DIRECT\n"); |
| 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | if (get_file_size(td, f)) |
| 299 | goto err; |
| 300 | |
| 301 | if (td->invalidate_cache && file_invalidate_cache(td, f)) |
| 302 | goto err; |
| 303 | |
| 304 | if (!td_random(td)) { |
| 305 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_SEQUENTIAL) < 0) { |
| 306 | td_verror(td, errno, "fadvise"); |
| 307 | goto err; |
| 308 | } |
| 309 | } else { |
| 310 | if (fadvise(f->fd, f->file_offset, f->file_size, POSIX_FADV_RANDOM) < 0) { |
| 311 | td_verror(td, errno, "fadvise"); |
| 312 | goto err; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return 0; |
| 317 | err: |
| 318 | close(f->fd); |
| 319 | return 1; |
| 320 | } |
| 321 | |
| 322 | int open_files(struct thread_data *td) |
| 323 | { |
| 324 | struct fio_file *f; |
| 325 | unsigned int i; |
| 326 | int err = 0; |
| 327 | |
| 328 | for_each_file(td, f, i) { |
| 329 | err = td_io_open_file(td, f); |
| 330 | if (err) |
| 331 | break; |
| 332 | |
| 333 | if (td->open_files == td->nr_open_files) |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | if (!err) |
| 338 | return 0; |
| 339 | |
| 340 | for_each_file(td, f, i) |
| 341 | td_io_close_file(td, f); |
| 342 | |
| 343 | return err; |
| 344 | } |
| 345 | |
| 346 | int setup_files(struct thread_data *td) |
| 347 | { |
| 348 | struct fio_file *f; |
| 349 | unsigned int i; |
| 350 | int err; |
| 351 | |
| 352 | /* |
| 353 | * if ioengine defines a setup() method, it's responsible for |
| 354 | * setting up everything in the td->files[] area. |
| 355 | */ |
| 356 | if (td->io_ops->setup) |
| 357 | return td->io_ops->setup(td); |
| 358 | |
| 359 | if (create_files(td)) |
| 360 | return 1; |
| 361 | |
| 362 | err = open_files(td); |
| 363 | if (err) |
| 364 | return err; |
| 365 | |
| 366 | /* |
| 367 | * Recalculate the total file size now that files are set up. |
| 368 | */ |
| 369 | td->total_file_size = 0; |
| 370 | for_each_file(td, f, i) |
| 371 | td->total_file_size += f->file_size; |
| 372 | |
| 373 | td->total_file_size = (td->total_file_size * td->nr_files) / td->open_files; |
| 374 | |
| 375 | td->io_size = td->total_file_size; |
| 376 | if (td->io_size == 0) { |
| 377 | log_err("%s: no io blocks\n", td->name); |
| 378 | td_verror(td, EINVAL, "total_file_size"); |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | if (!td->zone_size) |
| 383 | td->zone_size = td->io_size; |
| 384 | |
| 385 | td->total_io_size = td->io_size * td->loops; |
| 386 | |
| 387 | for_each_file(td, f, i) |
| 388 | td_io_close_file(td, f); |
| 389 | |
| 390 | return err; |
| 391 | } |
| 392 | |
| 393 | void close_files(struct thread_data *td) |
| 394 | { |
| 395 | struct fio_file *f; |
| 396 | unsigned int i; |
| 397 | |
| 398 | for_each_file(td, f, i) { |
| 399 | if (!f->file_name && (f->flags & FIO_FILE_UNLINK) && |
| 400 | f->filetype == FIO_TYPE_FILE) { |
| 401 | unlink(f->file_name); |
| 402 | free(f->file_name); |
| 403 | f->file_name = NULL; |
| 404 | } |
| 405 | |
| 406 | td_io_close_file(td, f); |
| 407 | |
| 408 | if (f->file_map) |
| 409 | free(f->file_map); |
| 410 | } |
| 411 | |
| 412 | td->filename = NULL; |
| 413 | free(td->files); |
| 414 | td->files = NULL; |
| 415 | td->nr_files = 0; |
| 416 | } |
| 417 | |
| 418 | static void get_file_type(struct fio_file *f) |
| 419 | { |
| 420 | struct stat sb; |
| 421 | |
| 422 | f->filetype = FIO_TYPE_FILE; |
| 423 | |
| 424 | if (!lstat(f->file_name, &sb)) { |
| 425 | if (S_ISBLK(sb.st_mode)) |
| 426 | f->filetype = FIO_TYPE_BD; |
| 427 | else if (S_ISCHR(sb.st_mode)) |
| 428 | f->filetype = FIO_TYPE_CHAR; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | void add_file(struct thread_data *td, const char *fname) |
| 433 | { |
| 434 | int cur_files = td->open_files; |
| 435 | struct fio_file *f; |
| 436 | |
| 437 | td->files = realloc(td->files, (cur_files + 1) * sizeof(*f)); |
| 438 | |
| 439 | f = &td->files[cur_files]; |
| 440 | memset(f, 0, sizeof(*f)); |
| 441 | f->fd = -1; |
| 442 | f->file_name = strdup(fname); |
| 443 | |
| 444 | get_file_type(f); |
| 445 | |
| 446 | td->open_files++; |
| 447 | td->nr_uniq_files = td->open_files; |
| 448 | } |
| 449 | |
| 450 | void get_file(struct fio_file *f) |
| 451 | { |
| 452 | f->references++; |
| 453 | } |
| 454 | |
| 455 | void put_file(struct thread_data *td, struct fio_file *f) |
| 456 | { |
| 457 | if (!(f->flags & FIO_FILE_OPEN)) |
| 458 | return; |
| 459 | |
| 460 | assert(f->references); |
| 461 | if (--f->references) |
| 462 | return; |
| 463 | |
| 464 | if (td->io_ops->close_file) |
| 465 | td->io_ops->close_file(td, f); |
| 466 | td->nr_open_files--; |
| 467 | f->flags &= ~FIO_FILE_OPEN; |
| 468 | } |