1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2010 Red Hat, Inc. 4 * Copyright (c) 2016-2025 Christoph Hellwig. 5 */ 6 #include <linux/bio-integrity.h> 7 #include <linux/blk-crypto.h> 8 #include <linux/fscrypt.h> 9 #include <linux/pagemap.h> 10 #include <linux/iomap.h> 11 #include <linux/task_io_accounting_ops.h> 12 #include <linux/fserror.h> 13 #include "internal.h" 14 #include "trace.h" 15 16 #include "../internal.h" 17 18 /* 19 * Private flags for iomap_dio, must not overlap with the public ones in 20 * iomap.h: 21 */ 22 #define IOMAP_DIO_NO_INVALIDATE (1U << 26) 23 #define IOMAP_DIO_COMP_WORK (1U << 27) 24 #define IOMAP_DIO_WRITE_THROUGH (1U << 28) 25 #define IOMAP_DIO_NEED_SYNC (1U << 29) 26 #define IOMAP_DIO_WRITE (1U << 30) 27 #define IOMAP_DIO_USER_BACKED (1U << 31) 28 29 struct iomap_dio { 30 struct kiocb *iocb; 31 const struct iomap_dio_ops *dops; 32 loff_t i_size; 33 loff_t size; 34 atomic_t ref; 35 unsigned flags; 36 int error; 37 size_t done_before; 38 bool wait_for_completion; 39 40 union { 41 /* used during submission and for synchronous completion: */ 42 struct { 43 struct iov_iter *iter; 44 struct task_struct *waiter; 45 } submit; 46 47 /* used for aio completion: */ 48 struct { 49 struct work_struct work; 50 } aio; 51 }; 52 }; 53 54 static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter, 55 struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf) 56 { 57 if (dio->dops && dio->dops->bio_set) 58 return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf, 59 GFP_KERNEL, dio->dops->bio_set); 60 return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL); 61 } 62 63 static void iomap_dio_submit_bio(const struct iomap_iter *iter, 64 struct iomap_dio *dio, struct bio *bio, loff_t pos) 65 { 66 struct kiocb *iocb = dio->iocb; 67 68 atomic_inc(&dio->ref); 69 70 /* Sync dio can't be polled reliably */ 71 if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { 72 bio->bi_opf |= REQ_POLLED; 73 WRITE_ONCE(iocb->private, bio); 74 } 75 76 if (dio->dops && dio->dops->submit_io) { 77 dio->dops->submit_io(iter, bio, pos); 78 } else { 79 WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_ANON_WRITE); 80 blk_crypto_submit_bio(bio); 81 } 82 } 83 84 static inline enum fserror_type iomap_dio_err_type(const struct iomap_dio *dio) 85 { 86 if (dio->flags & IOMAP_DIO_WRITE) 87 return FSERR_DIRECTIO_WRITE; 88 return FSERR_DIRECTIO_READ; 89 } 90 91 static inline bool should_report_dio_fserror(const struct iomap_dio *dio) 92 { 93 switch (dio->error) { 94 case 0: 95 case -EAGAIN: 96 case -ENOTBLK: 97 /* don't send fsnotify for success or magic retry codes */ 98 return false; 99 default: 100 return true; 101 } 102 } 103 104 ssize_t iomap_dio_complete(struct iomap_dio *dio) 105 { 106 const struct iomap_dio_ops *dops = dio->dops; 107 struct kiocb *iocb = dio->iocb; 108 loff_t offset = iocb->ki_pos; 109 ssize_t ret = dio->error; 110 111 if (dops && dops->end_io) 112 ret = dops->end_io(iocb, dio->size, ret, dio->flags); 113 if (should_report_dio_fserror(dio)) 114 fserror_report_io(file_inode(iocb->ki_filp), 115 iomap_dio_err_type(dio), offset, dio->size, 116 dio->error, GFP_NOFS); 117 118 if (likely(!ret)) { 119 ret = dio->size; 120 /* check for short read */ 121 if (offset + ret > dio->i_size && 122 !(dio->flags & IOMAP_DIO_WRITE)) 123 ret = dio->i_size - offset; 124 } 125 126 /* 127 * Try again to invalidate clean pages which might have been cached by 128 * non-direct readahead, or faulted in by get_user_pages() if the source 129 * of the write was an mmap'ed region of the file we're writing. Either 130 * one is a pretty crazy thing to do, so we don't support it 100%. If 131 * this invalidation fails, tough, the write still worked... 132 * 133 * And this page cache invalidation has to be after ->end_io(), as some 134 * filesystems convert unwritten extents to real allocations in 135 * ->end_io() when necessary, otherwise a racing buffer read would cache 136 * zeros from unwritten extents. 137 */ 138 if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE) && 139 !(dio->flags & IOMAP_DIO_NO_INVALIDATE)) 140 kiocb_invalidate_post_direct_write(iocb, dio->size); 141 142 inode_dio_end(file_inode(iocb->ki_filp)); 143 144 if (ret > 0) { 145 iocb->ki_pos += ret; 146 147 /* 148 * If this is a DSYNC write, make sure we push it to stable 149 * storage now that we've written data. 150 */ 151 if (dio->flags & IOMAP_DIO_NEED_SYNC) 152 ret = generic_write_sync(iocb, ret); 153 if (ret > 0) 154 ret += dio->done_before; 155 } 156 trace_iomap_dio_complete(iocb, dio->error, ret); 157 kfree(dio); 158 return ret; 159 } 160 EXPORT_SYMBOL_GPL(iomap_dio_complete); 161 162 static void iomap_dio_complete_work(struct work_struct *work) 163 { 164 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); 165 struct kiocb *iocb = dio->iocb; 166 167 iocb->ki_complete(iocb, iomap_dio_complete(dio)); 168 } 169 170 /* 171 * Set an error in the dio if none is set yet. We have to use cmpxchg 172 * as the submission context and the completion context(s) can race to 173 * update the error. 174 */ 175 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) 176 { 177 cmpxchg(&dio->error, 0, ret); 178 } 179 180 /* 181 * Called when dio->ref reaches zero from an I/O completion. 182 */ 183 static void iomap_dio_done(struct iomap_dio *dio) 184 { 185 struct kiocb *iocb = dio->iocb; 186 187 if (dio->wait_for_completion) { 188 /* 189 * Synchronous I/O, task itself will handle any completion work 190 * that needs after IO. All we need to do is wake the task. 191 */ 192 struct task_struct *waiter = dio->submit.waiter; 193 194 WRITE_ONCE(dio->submit.waiter, NULL); 195 blk_wake_io_task(waiter); 196 return; 197 } 198 199 /* 200 * Always run error completions in user context. These are not 201 * performance critical and some code relies on taking sleeping locks 202 * for error handling. 203 */ 204 if (dio->error) 205 dio->flags |= IOMAP_DIO_COMP_WORK; 206 207 /* 208 * Never invalidate pages from this context to avoid deadlocks with 209 * buffered I/O completions when called from the ioend workqueue, 210 * or avoid sleeping when called directly from ->bi_end_io. 211 * Tough luck if you hit the tiny race with someone dirtying the range 212 * right between this check and the actual completion. 213 */ 214 if ((dio->flags & IOMAP_DIO_WRITE) && 215 !(dio->flags & IOMAP_DIO_COMP_WORK)) { 216 if (dio->iocb->ki_filp->f_mapping->nrpages) 217 dio->flags |= IOMAP_DIO_COMP_WORK; 218 else 219 dio->flags |= IOMAP_DIO_NO_INVALIDATE; 220 } 221 222 if (dio->flags & IOMAP_DIO_COMP_WORK) { 223 struct inode *inode = file_inode(iocb->ki_filp); 224 225 /* 226 * Async DIO completion that requires filesystem level 227 * completion work gets punted to a work queue to complete as 228 * the operation may require more IO to be issued to finalise 229 * filesystem metadata changes or guarantee data integrity. 230 */ 231 INIT_WORK(&dio->aio.work, iomap_dio_complete_work); 232 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); 233 return; 234 } 235 236 WRITE_ONCE(iocb->private, NULL); 237 iomap_dio_complete_work(&dio->aio.work); 238 } 239 240 static void __iomap_dio_bio_end_io(struct bio *bio, bool inline_completion) 241 { 242 struct iomap_dio *dio = bio->bi_private; 243 244 if (bio_integrity(bio)) 245 fs_bio_integrity_free(bio); 246 247 if (dio->flags & IOMAP_DIO_BOUNCE) { 248 bio_iov_iter_unbounce(bio, !!dio->error, 249 dio->flags & IOMAP_DIO_USER_BACKED); 250 bio_put(bio); 251 } else if (dio->flags & IOMAP_DIO_USER_BACKED) { 252 bio_check_pages_dirty(bio); 253 } else { 254 bio_release_pages(bio, false); 255 bio_put(bio); 256 } 257 258 /* Do not touch bio below, we just gave up our reference. */ 259 260 if (atomic_dec_and_test(&dio->ref)) { 261 /* 262 * Avoid another context switch for the completion when already 263 * called from the ioend completion workqueue. 264 */ 265 if (inline_completion) 266 dio->flags &= ~IOMAP_DIO_COMP_WORK; 267 iomap_dio_done(dio); 268 } 269 } 270 271 void iomap_dio_bio_end_io(struct bio *bio) 272 { 273 struct iomap_dio *dio = bio->bi_private; 274 275 if (bio->bi_status) 276 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); 277 __iomap_dio_bio_end_io(bio, false); 278 } 279 EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io); 280 281 u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend) 282 { 283 struct iomap_dio *dio = ioend->io_bio.bi_private; 284 u32 vec_count = ioend->io_bio.bi_vcnt; 285 286 if (ioend->io_error) 287 iomap_dio_set_error(dio, ioend->io_error); 288 __iomap_dio_bio_end_io(&ioend->io_bio, true); 289 290 /* 291 * Return the number of bvecs completed as even direct I/O completions 292 * do significant per-folio work and we'll still want to give up the 293 * CPU after a lot of completions. 294 */ 295 return vec_count; 296 } 297 298 static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio, 299 loff_t pos, unsigned len) 300 { 301 struct inode *inode = file_inode(dio->iocb->ki_filp); 302 struct bio *bio; 303 struct folio *zero_folio = largest_zero_folio(); 304 int nr_vecs = max(1, i_blocksize(inode) / folio_size(zero_folio)); 305 306 if (!len) 307 return 0; 308 309 /* 310 * This limit shall never be reached as most filesystems have a 311 * maximum blocksize of 64k. 312 */ 313 if (WARN_ON_ONCE(nr_vecs > BIO_MAX_VECS)) 314 return -EINVAL; 315 316 bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, 317 REQ_OP_WRITE | REQ_SYNC | REQ_IDLE); 318 fscrypt_set_bio_crypt_ctx(bio, inode, pos, GFP_KERNEL); 319 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); 320 bio->bi_private = dio; 321 bio->bi_end_io = iomap_dio_bio_end_io; 322 323 while (len > 0) { 324 unsigned int io_len = min(len, folio_size(zero_folio)); 325 326 bio_add_folio_nofail(bio, zero_folio, io_len, 0); 327 len -= io_len; 328 } 329 iomap_dio_submit_bio(iter, dio, bio, pos); 330 331 return 0; 332 } 333 334 static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, 335 struct iomap_dio *dio, loff_t pos, unsigned int alignment, 336 blk_opf_t op) 337 { 338 unsigned int nr_vecs; 339 struct bio *bio; 340 ssize_t ret; 341 342 if (dio->flags & IOMAP_DIO_BOUNCE) 343 nr_vecs = bio_iov_bounce_nr_vecs(dio->submit.iter, op); 344 else 345 nr_vecs = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS); 346 347 bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, op); 348 fscrypt_set_bio_crypt_ctx(bio, iter->inode, pos, GFP_KERNEL); 349 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); 350 bio->bi_write_hint = iter->inode->i_write_hint; 351 bio->bi_ioprio = dio->iocb->ki_ioprio; 352 bio->bi_private = dio; 353 bio->bi_end_io = iomap_dio_bio_end_io; 354 355 356 if (dio->flags & IOMAP_DIO_BOUNCE) 357 ret = bio_iov_iter_bounce(bio, dio->submit.iter, 358 iomap_max_bio_size(&iter->iomap), alignment); 359 else 360 ret = bio_iov_iter_get_pages(bio, dio->submit.iter, 361 alignment - 1); 362 if (unlikely(ret)) 363 goto out_put_bio; 364 ret = bio->bi_iter.bi_size; 365 366 /* 367 * An atomic write bio must cover the complete length. If it doesn't, 368 * error out. 369 */ 370 if ((op & REQ_ATOMIC) && WARN_ON_ONCE(ret != iomap_length(iter))) { 371 ret = -EINVAL; 372 goto out_put_bio; 373 } 374 375 if (iter->iomap.flags & IOMAP_F_INTEGRITY) { 376 if (dio->flags & IOMAP_DIO_WRITE) 377 fs_bio_integrity_generate(bio); 378 else 379 fs_bio_integrity_alloc(bio); 380 } 381 382 if (dio->flags & IOMAP_DIO_WRITE) 383 task_io_account_write(ret); 384 else if ((dio->flags & IOMAP_DIO_USER_BACKED) && 385 !(dio->flags & IOMAP_DIO_BOUNCE)) 386 bio_set_pages_dirty(bio); 387 388 /* 389 * We can only poll for single bio I/Os. 390 */ 391 if (iov_iter_count(dio->submit.iter)) 392 dio->iocb->ki_flags &= ~IOCB_HIPRI; 393 iomap_dio_submit_bio(iter, dio, bio, pos); 394 return ret; 395 396 out_put_bio: 397 bio_put(bio); 398 return ret; 399 } 400 401 static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio) 402 { 403 const struct iomap *iomap = &iter->iomap; 404 struct inode *inode = iter->inode; 405 unsigned int fs_block_size = i_blocksize(inode), pad; 406 const loff_t length = iomap_length(iter); 407 loff_t pos = iter->pos; 408 blk_opf_t bio_opf = REQ_SYNC | REQ_IDLE; 409 bool need_zeroout = false; 410 u64 copied = 0; 411 size_t orig_count; 412 unsigned int alignment; 413 ssize_t ret = 0; 414 415 /* 416 * File systems that write out of place and always allocate new blocks 417 * need each bio to be block aligned as that's the unit of allocation. 418 */ 419 if (dio->flags & IOMAP_DIO_FSBLOCK_ALIGNED) 420 alignment = fs_block_size; 421 else 422 alignment = bdev_logical_block_size(iomap->bdev); 423 424 if ((pos | length) & (alignment - 1)) 425 return -EINVAL; 426 427 if (dio->flags & IOMAP_DIO_WRITE) { 428 bool need_completion_work = true; 429 430 switch (iomap->type) { 431 case IOMAP_MAPPED: 432 /* 433 * Directly mapped I/O does not inherently need to do 434 * work at I/O completion time. But there are various 435 * cases below where this will get set again. 436 */ 437 need_completion_work = false; 438 break; 439 case IOMAP_UNWRITTEN: 440 dio->flags |= IOMAP_DIO_UNWRITTEN; 441 need_zeroout = true; 442 break; 443 default: 444 break; 445 } 446 447 if (iomap->flags & IOMAP_F_ATOMIC_BIO) { 448 /* 449 * Ensure that the mapping covers the full write 450 * length, otherwise it won't be submitted as a single 451 * bio, which is required to use hardware atomics. 452 */ 453 if (length != iter->len) 454 return -EINVAL; 455 bio_opf |= REQ_ATOMIC; 456 } 457 458 if (iomap->flags & IOMAP_F_SHARED) { 459 /* 460 * Unsharing of needs to update metadata at I/O 461 * completion time. 462 */ 463 need_completion_work = true; 464 dio->flags |= IOMAP_DIO_COW; 465 } 466 467 if (iomap->flags & IOMAP_F_NEW) { 468 /* 469 * Newly allocated blocks might need recording in 470 * metadata at I/O completion time. 471 */ 472 need_completion_work = true; 473 need_zeroout = true; 474 } 475 476 /* 477 * Use a FUA write if we need datasync semantics and this is a 478 * pure overwrite that doesn't require any metadata updates. 479 * 480 * This allows us to avoid cache flushes on I/O completion. 481 */ 482 if (dio->flags & IOMAP_DIO_WRITE_THROUGH) { 483 if (!need_completion_work && 484 !(iomap->flags & IOMAP_F_DIRTY) && 485 (!bdev_write_cache(iomap->bdev) || 486 bdev_fua(iomap->bdev))) 487 bio_opf |= REQ_FUA; 488 else 489 dio->flags &= ~IOMAP_DIO_WRITE_THROUGH; 490 } 491 492 /* 493 * We can only do inline completion for pure overwrites that 494 * don't require additional I/O at completion time. 495 * 496 * This rules out writes that need zeroing or metdata updates to 497 * convert unwritten or shared extents. 498 * 499 * Writes that extend i_size are also not supported, but this is 500 * handled in __iomap_dio_rw(). 501 */ 502 if (need_completion_work) 503 dio->flags |= IOMAP_DIO_COMP_WORK; 504 505 bio_opf |= REQ_OP_WRITE; 506 } else { 507 bio_opf |= REQ_OP_READ; 508 } 509 510 /* 511 * Save the original count and trim the iter to just the extent we 512 * are operating on right now. The iter will be re-expanded once 513 * we are done. 514 */ 515 orig_count = iov_iter_count(dio->submit.iter); 516 iov_iter_truncate(dio->submit.iter, length); 517 518 if (!iov_iter_count(dio->submit.iter)) 519 goto out; 520 521 /* 522 * The rules for polled IO completions follow the guidelines as the 523 * ones we set for inline and deferred completions. If none of those 524 * are available for this IO, clear the polled flag. 525 */ 526 if (dio->flags & IOMAP_DIO_COMP_WORK) 527 dio->iocb->ki_flags &= ~IOCB_HIPRI; 528 529 if (need_zeroout) { 530 /* zero out from the start of the block to the write offset */ 531 pad = pos & (fs_block_size - 1); 532 533 ret = iomap_dio_zero(iter, dio, pos - pad, pad); 534 if (ret) 535 goto out; 536 } 537 538 do { 539 /* 540 * If completions already occurred and reported errors, give up now and 541 * don't bother submitting more bios. 542 */ 543 if (unlikely(data_race(dio->error))) 544 goto out; 545 546 ret = iomap_dio_bio_iter_one(iter, dio, pos, alignment, bio_opf); 547 if (unlikely(ret < 0)) { 548 /* 549 * We have to stop part way through an IO. We must fall 550 * through to the sub-block tail zeroing here, otherwise 551 * this short IO may expose stale data in the tail of 552 * the block we haven't written data to. 553 */ 554 break; 555 } 556 dio->size += ret; 557 copied += ret; 558 pos += ret; 559 ret = 0; 560 } while (iov_iter_count(dio->submit.iter)); 561 562 /* 563 * We need to zeroout the tail of a sub-block write if the extent type 564 * requires zeroing or the write extends beyond EOF. If we don't zero 565 * the block tail in the latter case, we can expose stale data via mmap 566 * reads of the EOF block. 567 */ 568 if (need_zeroout || 569 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { 570 /* zero out from the end of the write to the end of the block */ 571 pad = pos & (fs_block_size - 1); 572 if (pad) 573 ret = iomap_dio_zero(iter, dio, pos, 574 fs_block_size - pad); 575 } 576 out: 577 /* Undo iter limitation to current extent */ 578 iov_iter_reexpand(dio->submit.iter, orig_count - copied); 579 if (copied) 580 return iomap_iter_advance(iter, copied); 581 return ret; 582 } 583 584 static int iomap_dio_hole_iter(struct iomap_iter *iter, struct iomap_dio *dio) 585 { 586 loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter); 587 588 dio->size += length; 589 if (!length) 590 return -EFAULT; 591 return iomap_iter_advance(iter, length); 592 } 593 594 static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio) 595 { 596 const struct iomap *iomap = &iomi->iomap; 597 struct iov_iter *iter = dio->submit.iter; 598 void *inline_data = iomap_inline_data(iomap, iomi->pos); 599 loff_t length = iomap_length(iomi); 600 loff_t pos = iomi->pos; 601 u64 copied; 602 603 if (WARN_ON_ONCE(!inline_data)) 604 return -EIO; 605 606 if (dio->flags & IOMAP_DIO_WRITE) { 607 loff_t size = iomi->inode->i_size; 608 609 if (pos > size) 610 memset(iomap_inline_data(iomap, size), 0, pos - size); 611 copied = copy_from_iter(inline_data, length, iter); 612 if (copied) { 613 if (pos + copied > size) 614 i_size_write(iomi->inode, pos + copied); 615 mark_inode_dirty(iomi->inode); 616 } 617 } else { 618 copied = copy_to_iter(inline_data, length, iter); 619 } 620 dio->size += copied; 621 if (!copied) 622 return -EFAULT; 623 return iomap_iter_advance(iomi, copied); 624 } 625 626 static int iomap_dio_iter(struct iomap_iter *iter, struct iomap_dio *dio) 627 { 628 switch (iter->iomap.type) { 629 case IOMAP_HOLE: 630 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) 631 return -EIO; 632 return iomap_dio_hole_iter(iter, dio); 633 case IOMAP_UNWRITTEN: 634 if (!(dio->flags & IOMAP_DIO_WRITE)) 635 return iomap_dio_hole_iter(iter, dio); 636 return iomap_dio_bio_iter(iter, dio); 637 case IOMAP_MAPPED: 638 return iomap_dio_bio_iter(iter, dio); 639 case IOMAP_INLINE: 640 return iomap_dio_inline_iter(iter, dio); 641 case IOMAP_DELALLOC: 642 /* 643 * DIO is not serialised against mmap() access at all, and so 644 * if the page_mkwrite occurs between the writeback and the 645 * iomap_iter() call in the DIO path, then it will see the 646 * DELALLOC block that the page-mkwrite allocated. 647 */ 648 pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n", 649 dio->iocb->ki_filp, current->comm); 650 return -EIO; 651 default: 652 WARN_ON_ONCE(1); 653 return -EIO; 654 } 655 } 656 657 /* 658 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO 659 * is being issued as AIO or not. This allows us to optimise pure data writes 660 * to use REQ_FUA rather than requiring generic_write_sync() to issue a 661 * REQ_FLUSH post write. This is slightly tricky because a single request here 662 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued 663 * may be pure data writes. In that case, we still need to do a full data sync 664 * completion. 665 * 666 * When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL, 667 * __iomap_dio_rw can return a partial result if it encounters a non-resident 668 * page in @iter after preparing a transfer. In that case, the non-resident 669 * pages can be faulted in and the request resumed with @done_before set to the 670 * number of bytes previously transferred. The request will then complete with 671 * the correct total number of bytes transferred; this is essential for 672 * completing partial requests asynchronously. 673 * 674 * Returns -ENOTBLK In case of a page invalidation invalidation failure for 675 * writes. The callers needs to fall back to buffered I/O in this case. 676 */ 677 struct iomap_dio * 678 __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, 679 const struct iomap_ops *ops, const struct iomap_dio_ops *dops, 680 unsigned int dio_flags, void *private, size_t done_before) 681 { 682 struct inode *inode = file_inode(iocb->ki_filp); 683 struct iomap_iter iomi = { 684 .inode = inode, 685 .pos = iocb->ki_pos, 686 .len = iov_iter_count(iter), 687 .flags = IOMAP_DIRECT, 688 .private = private, 689 }; 690 bool wait_for_completion = 691 is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT); 692 struct blk_plug plug; 693 struct iomap_dio *dio; 694 loff_t ret = 0; 695 696 trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before); 697 698 if (!iomi.len) 699 return NULL; 700 701 dio = kmalloc_obj(*dio); 702 if (!dio) 703 return ERR_PTR(-ENOMEM); 704 705 dio->iocb = iocb; 706 atomic_set(&dio->ref, 1); 707 dio->size = 0; 708 dio->i_size = i_size_read(inode); 709 dio->dops = dops; 710 dio->error = 0; 711 dio->flags = dio_flags & (IOMAP_DIO_FSBLOCK_ALIGNED | IOMAP_DIO_BOUNCE); 712 dio->done_before = done_before; 713 714 dio->submit.iter = iter; 715 dio->submit.waiter = current; 716 717 if (iocb->ki_flags & IOCB_NOWAIT) 718 iomi.flags |= IOMAP_NOWAIT; 719 720 if (iov_iter_rw(iter) == READ) { 721 if (iomi.pos >= dio->i_size) 722 goto out_free_dio; 723 724 if (user_backed_iter(iter)) 725 dio->flags |= IOMAP_DIO_USER_BACKED; 726 727 ret = kiocb_write_and_wait(iocb, iomi.len); 728 if (ret) 729 goto out_free_dio; 730 } else { 731 iomi.flags |= IOMAP_WRITE; 732 dio->flags |= IOMAP_DIO_WRITE; 733 734 if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) { 735 ret = -EAGAIN; 736 if (iomi.pos >= dio->i_size || 737 iomi.pos + iomi.len > dio->i_size) 738 goto out_free_dio; 739 iomi.flags |= IOMAP_OVERWRITE_ONLY; 740 } 741 742 if (iocb->ki_flags & IOCB_ATOMIC) 743 iomi.flags |= IOMAP_ATOMIC; 744 745 /* for data sync or sync, we need sync completion processing */ 746 if (iocb_is_dsync(iocb)) { 747 dio->flags |= IOMAP_DIO_NEED_SYNC; 748 749 /* 750 * For datasync only writes, we optimistically try using 751 * WRITE_THROUGH for this IO. This flag requires either 752 * FUA writes through the device's write cache, or a 753 * normal write to a device without a volatile write 754 * cache. For the former, Any non-FUA write that occurs 755 * will clear this flag, hence we know before completion 756 * whether a cache flush is necessary. 757 */ 758 if (!(iocb->ki_flags & IOCB_SYNC)) 759 dio->flags |= IOMAP_DIO_WRITE_THROUGH; 760 } 761 762 /* 763 * i_size updates must to happen from process context. 764 */ 765 if (iomi.pos + iomi.len > dio->i_size) 766 dio->flags |= IOMAP_DIO_COMP_WORK; 767 768 /* 769 * Try to invalidate cache pages for the range we are writing. 770 * If this invalidation fails, let the caller fall back to 771 * buffered I/O. 772 */ 773 ret = kiocb_invalidate_pages(iocb, iomi.len); 774 if (ret) { 775 if (ret != -EAGAIN) { 776 trace_iomap_dio_invalidate_fail(inode, iomi.pos, 777 iomi.len); 778 if (iocb->ki_flags & IOCB_ATOMIC) { 779 /* 780 * folio invalidation failed, maybe 781 * this is transient, unlock and see if 782 * the caller tries again. 783 */ 784 ret = -EAGAIN; 785 } else { 786 /* fall back to buffered write */ 787 ret = -ENOTBLK; 788 } 789 } 790 goto out_free_dio; 791 } 792 } 793 794 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) { 795 ret = sb_init_dio_done_wq(inode->i_sb); 796 if (ret < 0) 797 goto out_free_dio; 798 } 799 800 inode_dio_begin(inode); 801 802 blk_start_plug(&plug); 803 while ((ret = iomap_iter(&iomi, ops)) > 0) { 804 iomi.status = iomap_dio_iter(&iomi, dio); 805 806 /* 807 * We can only poll for single bio I/Os. 808 */ 809 iocb->ki_flags &= ~IOCB_HIPRI; 810 } 811 812 blk_finish_plug(&plug); 813 814 /* 815 * We only report that we've read data up to i_size. 816 * Revert iter to a state corresponding to that as some callers (such 817 * as the splice code) rely on it. 818 */ 819 if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size) 820 iov_iter_revert(iter, iomi.pos - dio->i_size); 821 822 if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) { 823 if (!(iocb->ki_flags & IOCB_NOWAIT)) 824 wait_for_completion = true; 825 ret = 0; 826 } 827 828 /* magic error code to fall back to buffered I/O */ 829 if (ret == -ENOTBLK) { 830 wait_for_completion = true; 831 ret = 0; 832 } 833 if (ret < 0) 834 iomap_dio_set_error(dio, ret); 835 836 /* 837 * If all the writes we issued were already written through to the 838 * media, we don't need to flush the cache on IO completion. Clear the 839 * sync flag for this case. 840 * 841 * Otherwise clear the inline completion flag if any sync work is 842 * needed, as that needs to be performed from process context. 843 */ 844 if (dio->flags & IOMAP_DIO_WRITE_THROUGH) 845 dio->flags &= ~IOMAP_DIO_NEED_SYNC; 846 else if (dio->flags & IOMAP_DIO_NEED_SYNC) 847 dio->flags |= IOMAP_DIO_COMP_WORK; 848 849 /* 850 * We are about to drop our additional submission reference, which 851 * might be the last reference to the dio. There are three different 852 * ways we can progress here: 853 * 854 * (a) If this is the last reference we will always complete and free 855 * the dio ourselves. 856 * (b) If this is not the last reference, and we serve an asynchronous 857 * iocb, we must never touch the dio after the decrement, the 858 * I/O completion handler will complete and free it. 859 * (c) If this is not the last reference, but we serve a synchronous 860 * iocb, the I/O completion handler will wake us up on the drop 861 * of the final reference, and we will complete and free it here 862 * after we got woken by the I/O completion handler. 863 */ 864 dio->wait_for_completion = wait_for_completion; 865 if (!atomic_dec_and_test(&dio->ref)) { 866 if (!wait_for_completion) { 867 trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len); 868 return ERR_PTR(-EIOCBQUEUED); 869 } 870 871 for (;;) { 872 set_current_state(TASK_UNINTERRUPTIBLE); 873 if (!READ_ONCE(dio->submit.waiter)) 874 break; 875 876 blk_io_schedule(); 877 } 878 __set_current_state(TASK_RUNNING); 879 } 880 881 return dio; 882 883 out_free_dio: 884 kfree(dio); 885 if (ret) 886 return ERR_PTR(ret); 887 return NULL; 888 } 889 EXPORT_SYMBOL_GPL(__iomap_dio_rw); 890 891 ssize_t 892 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, 893 const struct iomap_ops *ops, const struct iomap_dio_ops *dops, 894 unsigned int dio_flags, void *private, size_t done_before) 895 { 896 struct iomap_dio *dio; 897 898 dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private, 899 done_before); 900 if (IS_ERR_OR_NULL(dio)) 901 return PTR_ERR_OR_ZERO(dio); 902 return iomap_dio_complete(dio); 903 } 904 EXPORT_SYMBOL_GPL(iomap_dio_rw); 905