1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2010 Red Hat, Inc. 4 * Copyright (c) 2016-2021 Christoph Hellwig. 5 */ 6 #include <linux/module.h> 7 #include <linux/compiler.h> 8 #include <linux/fs.h> 9 #include <linux/iomap.h> 10 #include <linux/backing-dev.h> 11 #include <linux/uio.h> 12 #include <linux/task_io_accounting_ops.h> 13 #include "trace.h" 14 15 #include "../internal.h" 16 17 /* 18 * Private flags for iomap_dio, must not overlap with the public ones in 19 * iomap.h: 20 */ 21 #define IOMAP_DIO_WRITE_FUA (1 << 28) 22 #define IOMAP_DIO_NEED_SYNC (1 << 29) 23 #define IOMAP_DIO_WRITE (1 << 30) 24 #define IOMAP_DIO_DIRTY (1 << 31) 25 26 struct iomap_dio { 27 struct kiocb *iocb; 28 const struct iomap_dio_ops *dops; 29 loff_t i_size; 30 loff_t size; 31 atomic_t ref; 32 unsigned flags; 33 int error; 34 bool wait_for_completion; 35 36 union { 37 /* used during submission and for synchronous completion: */ 38 struct { 39 struct iov_iter *iter; 40 struct task_struct *waiter; 41 struct request_queue *last_queue; 42 blk_qc_t cookie; 43 } submit; 44 45 /* used for aio completion: */ 46 struct { 47 struct work_struct work; 48 } aio; 49 }; 50 }; 51 52 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin) 53 { 54 struct request_queue *q = READ_ONCE(kiocb->private); 55 56 if (!q) 57 return 0; 58 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin); 59 } 60 EXPORT_SYMBOL_GPL(iomap_dio_iopoll); 61 62 static void iomap_dio_submit_bio(const struct iomap_iter *iter, 63 struct iomap_dio *dio, struct bio *bio, loff_t pos) 64 { 65 atomic_inc(&dio->ref); 66 67 if (dio->iocb->ki_flags & IOCB_HIPRI) 68 bio_set_polled(bio, dio->iocb); 69 70 dio->submit.last_queue = bdev_get_queue(iter->iomap.bdev); 71 if (dio->dops && dio->dops->submit_io) 72 dio->submit.cookie = dio->dops->submit_io(iter, bio, pos); 73 else 74 dio->submit.cookie = submit_bio(bio); 75 } 76 77 ssize_t iomap_dio_complete(struct iomap_dio *dio) 78 { 79 const struct iomap_dio_ops *dops = dio->dops; 80 struct kiocb *iocb = dio->iocb; 81 struct inode *inode = file_inode(iocb->ki_filp); 82 loff_t offset = iocb->ki_pos; 83 ssize_t ret = dio->error; 84 85 if (dops && dops->end_io) 86 ret = dops->end_io(iocb, dio->size, ret, dio->flags); 87 88 if (likely(!ret)) { 89 ret = dio->size; 90 /* check for short read */ 91 if (offset + ret > dio->i_size && 92 !(dio->flags & IOMAP_DIO_WRITE)) 93 ret = dio->i_size - offset; 94 iocb->ki_pos += ret; 95 } 96 97 /* 98 * Try again to invalidate clean pages which might have been cached by 99 * non-direct readahead, or faulted in by get_user_pages() if the source 100 * of the write was an mmap'ed region of the file we're writing. Either 101 * one is a pretty crazy thing to do, so we don't support it 100%. If 102 * this invalidation fails, tough, the write still worked... 103 * 104 * And this page cache invalidation has to be after ->end_io(), as some 105 * filesystems convert unwritten extents to real allocations in 106 * ->end_io() when necessary, otherwise a racing buffer read would cache 107 * zeros from unwritten extents. 108 */ 109 if (!dio->error && dio->size && 110 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) { 111 int err; 112 err = invalidate_inode_pages2_range(inode->i_mapping, 113 offset >> PAGE_SHIFT, 114 (offset + dio->size - 1) >> PAGE_SHIFT); 115 if (err) 116 dio_warn_stale_pagecache(iocb->ki_filp); 117 } 118 119 inode_dio_end(file_inode(iocb->ki_filp)); 120 /* 121 * If this is a DSYNC write, make sure we push it to stable storage now 122 * that we've written data. 123 */ 124 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC)) 125 ret = generic_write_sync(iocb, ret); 126 127 kfree(dio); 128 129 return ret; 130 } 131 EXPORT_SYMBOL_GPL(iomap_dio_complete); 132 133 static void iomap_dio_complete_work(struct work_struct *work) 134 { 135 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); 136 struct kiocb *iocb = dio->iocb; 137 138 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0); 139 } 140 141 /* 142 * Set an error in the dio if none is set yet. We have to use cmpxchg 143 * as the submission context and the completion context(s) can race to 144 * update the error. 145 */ 146 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) 147 { 148 cmpxchg(&dio->error, 0, ret); 149 } 150 151 static void iomap_dio_bio_end_io(struct bio *bio) 152 { 153 struct iomap_dio *dio = bio->bi_private; 154 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY); 155 156 if (bio->bi_status) 157 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); 158 159 if (atomic_dec_and_test(&dio->ref)) { 160 if (dio->wait_for_completion) { 161 struct task_struct *waiter = dio->submit.waiter; 162 WRITE_ONCE(dio->submit.waiter, NULL); 163 blk_wake_io_task(waiter); 164 } else if (dio->flags & IOMAP_DIO_WRITE) { 165 struct inode *inode = file_inode(dio->iocb->ki_filp); 166 167 INIT_WORK(&dio->aio.work, iomap_dio_complete_work); 168 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); 169 } else { 170 iomap_dio_complete_work(&dio->aio.work); 171 } 172 } 173 174 if (should_dirty) { 175 bio_check_pages_dirty(bio); 176 } else { 177 bio_release_pages(bio, false); 178 bio_put(bio); 179 } 180 } 181 182 static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio, 183 loff_t pos, unsigned len) 184 { 185 struct page *page = ZERO_PAGE(0); 186 int flags = REQ_SYNC | REQ_IDLE; 187 struct bio *bio; 188 189 bio = bio_alloc(GFP_KERNEL, 1); 190 bio_set_dev(bio, iter->iomap.bdev); 191 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); 192 bio->bi_private = dio; 193 bio->bi_end_io = iomap_dio_bio_end_io; 194 195 get_page(page); 196 __bio_add_page(bio, page, len, 0); 197 bio_set_op_attrs(bio, REQ_OP_WRITE, flags); 198 iomap_dio_submit_bio(iter, dio, bio, pos); 199 } 200 201 /* 202 * Figure out the bio's operation flags from the dio request, the 203 * mapping, and whether or not we want FUA. Note that we can end up 204 * clearing the WRITE_FUA flag in the dio request. 205 */ 206 static inline unsigned int iomap_dio_bio_opflags(struct iomap_dio *dio, 207 const struct iomap *iomap, bool use_fua) 208 { 209 unsigned int opflags = REQ_SYNC | REQ_IDLE; 210 211 if (!(dio->flags & IOMAP_DIO_WRITE)) { 212 WARN_ON_ONCE(iomap->flags & IOMAP_F_ZONE_APPEND); 213 return REQ_OP_READ; 214 } 215 216 if (iomap->flags & IOMAP_F_ZONE_APPEND) 217 opflags |= REQ_OP_ZONE_APPEND; 218 else 219 opflags |= REQ_OP_WRITE; 220 221 if (use_fua) 222 opflags |= REQ_FUA; 223 else 224 dio->flags &= ~IOMAP_DIO_WRITE_FUA; 225 226 return opflags; 227 } 228 229 static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter, 230 struct iomap_dio *dio) 231 { 232 const struct iomap *iomap = &iter->iomap; 233 struct inode *inode = iter->inode; 234 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev)); 235 unsigned int fs_block_size = i_blocksize(inode), pad; 236 unsigned int align = iov_iter_alignment(dio->submit.iter); 237 loff_t length = iomap_length(iter); 238 loff_t pos = iter->pos; 239 unsigned int bio_opf; 240 struct bio *bio; 241 bool need_zeroout = false; 242 bool use_fua = false; 243 int nr_pages, ret = 0; 244 size_t copied = 0; 245 size_t orig_count; 246 247 if ((pos | length | align) & ((1 << blkbits) - 1)) 248 return -EINVAL; 249 250 if (iomap->type == IOMAP_UNWRITTEN) { 251 dio->flags |= IOMAP_DIO_UNWRITTEN; 252 need_zeroout = true; 253 } 254 255 if (iomap->flags & IOMAP_F_SHARED) 256 dio->flags |= IOMAP_DIO_COW; 257 258 if (iomap->flags & IOMAP_F_NEW) { 259 need_zeroout = true; 260 } else if (iomap->type == IOMAP_MAPPED) { 261 /* 262 * Use a FUA write if we need datasync semantics, this is a pure 263 * data IO that doesn't require any metadata updates (including 264 * after IO completion such as unwritten extent conversion) and 265 * the underlying device supports FUA. This allows us to avoid 266 * cache flushes on IO completion. 267 */ 268 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) && 269 (dio->flags & IOMAP_DIO_WRITE_FUA) && 270 blk_queue_fua(bdev_get_queue(iomap->bdev))) 271 use_fua = true; 272 } 273 274 /* 275 * Save the original count and trim the iter to just the extent we 276 * are operating on right now. The iter will be re-expanded once 277 * we are done. 278 */ 279 orig_count = iov_iter_count(dio->submit.iter); 280 iov_iter_truncate(dio->submit.iter, length); 281 282 if (!iov_iter_count(dio->submit.iter)) 283 goto out; 284 285 if (need_zeroout) { 286 /* zero out from the start of the block to the write offset */ 287 pad = pos & (fs_block_size - 1); 288 if (pad) 289 iomap_dio_zero(iter, dio, pos - pad, pad); 290 } 291 292 /* 293 * Set the operation flags early so that bio_iov_iter_get_pages 294 * can set up the page vector appropriately for a ZONE_APPEND 295 * operation. 296 */ 297 bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua); 298 299 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS); 300 do { 301 size_t n; 302 if (dio->error) { 303 iov_iter_revert(dio->submit.iter, copied); 304 copied = ret = 0; 305 goto out; 306 } 307 308 bio = bio_alloc(GFP_KERNEL, nr_pages); 309 bio_set_dev(bio, iomap->bdev); 310 bio->bi_iter.bi_sector = iomap_sector(iomap, pos); 311 bio->bi_write_hint = dio->iocb->ki_hint; 312 bio->bi_ioprio = dio->iocb->ki_ioprio; 313 bio->bi_private = dio; 314 bio->bi_end_io = iomap_dio_bio_end_io; 315 bio->bi_opf = bio_opf; 316 317 ret = bio_iov_iter_get_pages(bio, dio->submit.iter); 318 if (unlikely(ret)) { 319 /* 320 * We have to stop part way through an IO. We must fall 321 * through to the sub-block tail zeroing here, otherwise 322 * this short IO may expose stale data in the tail of 323 * the block we haven't written data to. 324 */ 325 bio_put(bio); 326 goto zero_tail; 327 } 328 329 n = bio->bi_iter.bi_size; 330 if (dio->flags & IOMAP_DIO_WRITE) { 331 task_io_account_write(n); 332 } else { 333 if (dio->flags & IOMAP_DIO_DIRTY) 334 bio_set_pages_dirty(bio); 335 } 336 337 dio->size += n; 338 copied += n; 339 340 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, 341 BIO_MAX_VECS); 342 iomap_dio_submit_bio(iter, dio, bio, pos); 343 pos += n; 344 } while (nr_pages); 345 346 /* 347 * We need to zeroout the tail of a sub-block write if the extent type 348 * requires zeroing or the write extends beyond EOF. If we don't zero 349 * the block tail in the latter case, we can expose stale data via mmap 350 * reads of the EOF block. 351 */ 352 zero_tail: 353 if (need_zeroout || 354 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { 355 /* zero out from the end of the write to the end of the block */ 356 pad = pos & (fs_block_size - 1); 357 if (pad) 358 iomap_dio_zero(iter, dio, pos, fs_block_size - pad); 359 } 360 out: 361 /* Undo iter limitation to current extent */ 362 iov_iter_reexpand(dio->submit.iter, orig_count - copied); 363 if (copied) 364 return copied; 365 return ret; 366 } 367 368 static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter, 369 struct iomap_dio *dio) 370 { 371 loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter); 372 373 dio->size += length; 374 return length; 375 } 376 377 static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi, 378 struct iomap_dio *dio) 379 { 380 const struct iomap *iomap = &iomi->iomap; 381 struct iov_iter *iter = dio->submit.iter; 382 void *inline_data = iomap_inline_data(iomap, iomi->pos); 383 loff_t length = iomap_length(iomi); 384 loff_t pos = iomi->pos; 385 size_t copied; 386 387 if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap))) 388 return -EIO; 389 390 if (dio->flags & IOMAP_DIO_WRITE) { 391 loff_t size = iomi->inode->i_size; 392 393 if (pos > size) 394 memset(iomap_inline_data(iomap, size), 0, pos - size); 395 copied = copy_from_iter(inline_data, length, iter); 396 if (copied) { 397 if (pos + copied > size) 398 i_size_write(iomi->inode, pos + copied); 399 mark_inode_dirty(iomi->inode); 400 } 401 } else { 402 copied = copy_to_iter(inline_data, length, iter); 403 } 404 dio->size += copied; 405 return copied; 406 } 407 408 static loff_t iomap_dio_iter(const struct iomap_iter *iter, 409 struct iomap_dio *dio) 410 { 411 switch (iter->iomap.type) { 412 case IOMAP_HOLE: 413 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) 414 return -EIO; 415 return iomap_dio_hole_iter(iter, dio); 416 case IOMAP_UNWRITTEN: 417 if (!(dio->flags & IOMAP_DIO_WRITE)) 418 return iomap_dio_hole_iter(iter, dio); 419 return iomap_dio_bio_iter(iter, dio); 420 case IOMAP_MAPPED: 421 return iomap_dio_bio_iter(iter, dio); 422 case IOMAP_INLINE: 423 return iomap_dio_inline_iter(iter, dio); 424 case IOMAP_DELALLOC: 425 /* 426 * DIO is not serialised against mmap() access at all, and so 427 * if the page_mkwrite occurs between the writeback and the 428 * iomap_iter() call in the DIO path, then it will see the 429 * DELALLOC block that the page-mkwrite allocated. 430 */ 431 pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n", 432 dio->iocb->ki_filp, current->comm); 433 return -EIO; 434 default: 435 WARN_ON_ONCE(1); 436 return -EIO; 437 } 438 } 439 440 /* 441 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO 442 * is being issued as AIO or not. This allows us to optimise pure data writes 443 * to use REQ_FUA rather than requiring generic_write_sync() to issue a 444 * REQ_FLUSH post write. This is slightly tricky because a single request here 445 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued 446 * may be pure data writes. In that case, we still need to do a full data sync 447 * completion. 448 * 449 * Returns -ENOTBLK In case of a page invalidation invalidation failure for 450 * writes. The callers needs to fall back to buffered I/O in this case. 451 */ 452 struct iomap_dio * 453 __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, 454 const struct iomap_ops *ops, const struct iomap_dio_ops *dops, 455 unsigned int dio_flags) 456 { 457 struct address_space *mapping = iocb->ki_filp->f_mapping; 458 struct inode *inode = file_inode(iocb->ki_filp); 459 struct iomap_iter iomi = { 460 .inode = inode, 461 .pos = iocb->ki_pos, 462 .len = iov_iter_count(iter), 463 .flags = IOMAP_DIRECT, 464 }; 465 loff_t end = iomi.pos + iomi.len - 1, ret = 0; 466 bool wait_for_completion = 467 is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT); 468 struct blk_plug plug; 469 struct iomap_dio *dio; 470 471 if (!iomi.len) 472 return NULL; 473 474 dio = kmalloc(sizeof(*dio), GFP_KERNEL); 475 if (!dio) 476 return ERR_PTR(-ENOMEM); 477 478 dio->iocb = iocb; 479 atomic_set(&dio->ref, 1); 480 dio->size = 0; 481 dio->i_size = i_size_read(inode); 482 dio->dops = dops; 483 dio->error = 0; 484 dio->flags = 0; 485 486 dio->submit.iter = iter; 487 dio->submit.waiter = current; 488 dio->submit.cookie = BLK_QC_T_NONE; 489 dio->submit.last_queue = NULL; 490 491 if (iov_iter_rw(iter) == READ) { 492 if (iomi.pos >= dio->i_size) 493 goto out_free_dio; 494 495 if (iocb->ki_flags & IOCB_NOWAIT) { 496 if (filemap_range_needs_writeback(mapping, iomi.pos, 497 end)) { 498 ret = -EAGAIN; 499 goto out_free_dio; 500 } 501 iomi.flags |= IOMAP_NOWAIT; 502 } 503 504 if (iter_is_iovec(iter)) 505 dio->flags |= IOMAP_DIO_DIRTY; 506 } else { 507 iomi.flags |= IOMAP_WRITE; 508 dio->flags |= IOMAP_DIO_WRITE; 509 510 if (iocb->ki_flags & IOCB_NOWAIT) { 511 if (filemap_range_has_page(mapping, iomi.pos, end)) { 512 ret = -EAGAIN; 513 goto out_free_dio; 514 } 515 iomi.flags |= IOMAP_NOWAIT; 516 } 517 518 /* for data sync or sync, we need sync completion processing */ 519 if (iocb->ki_flags & IOCB_DSYNC) 520 dio->flags |= IOMAP_DIO_NEED_SYNC; 521 522 /* 523 * For datasync only writes, we optimistically try using FUA for 524 * this IO. Any non-FUA write that occurs will clear this flag, 525 * hence we know before completion whether a cache flush is 526 * necessary. 527 */ 528 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC) 529 dio->flags |= IOMAP_DIO_WRITE_FUA; 530 } 531 532 if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) { 533 ret = -EAGAIN; 534 if (iomi.pos >= dio->i_size || 535 iomi.pos + iomi.len > dio->i_size) 536 goto out_free_dio; 537 iomi.flags |= IOMAP_OVERWRITE_ONLY; 538 } 539 540 ret = filemap_write_and_wait_range(mapping, iomi.pos, end); 541 if (ret) 542 goto out_free_dio; 543 544 if (iov_iter_rw(iter) == WRITE) { 545 /* 546 * Try to invalidate cache pages for the range we are writing. 547 * If this invalidation fails, let the caller fall back to 548 * buffered I/O. 549 */ 550 if (invalidate_inode_pages2_range(mapping, 551 iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) { 552 trace_iomap_dio_invalidate_fail(inode, iomi.pos, 553 iomi.len); 554 ret = -ENOTBLK; 555 goto out_free_dio; 556 } 557 558 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) { 559 ret = sb_init_dio_done_wq(inode->i_sb); 560 if (ret < 0) 561 goto out_free_dio; 562 } 563 } 564 565 inode_dio_begin(inode); 566 567 blk_start_plug(&plug); 568 while ((ret = iomap_iter(&iomi, ops)) > 0) 569 iomi.processed = iomap_dio_iter(&iomi, dio); 570 blk_finish_plug(&plug); 571 572 /* 573 * We only report that we've read data up to i_size. 574 * Revert iter to a state corresponding to that as some callers (such 575 * as the splice code) rely on it. 576 */ 577 if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size) 578 iov_iter_revert(iter, iomi.pos - dio->i_size); 579 580 /* magic error code to fall back to buffered I/O */ 581 if (ret == -ENOTBLK) { 582 wait_for_completion = true; 583 ret = 0; 584 } 585 if (ret < 0) 586 iomap_dio_set_error(dio, ret); 587 588 /* 589 * If all the writes we issued were FUA, we don't need to flush the 590 * cache on IO completion. Clear the sync flag for this case. 591 */ 592 if (dio->flags & IOMAP_DIO_WRITE_FUA) 593 dio->flags &= ~IOMAP_DIO_NEED_SYNC; 594 595 WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie); 596 WRITE_ONCE(iocb->private, dio->submit.last_queue); 597 598 /* 599 * We are about to drop our additional submission reference, which 600 * might be the last reference to the dio. There are three different 601 * ways we can progress here: 602 * 603 * (a) If this is the last reference we will always complete and free 604 * the dio ourselves. 605 * (b) If this is not the last reference, and we serve an asynchronous 606 * iocb, we must never touch the dio after the decrement, the 607 * I/O completion handler will complete and free it. 608 * (c) If this is not the last reference, but we serve a synchronous 609 * iocb, the I/O completion handler will wake us up on the drop 610 * of the final reference, and we will complete and free it here 611 * after we got woken by the I/O completion handler. 612 */ 613 dio->wait_for_completion = wait_for_completion; 614 if (!atomic_dec_and_test(&dio->ref)) { 615 if (!wait_for_completion) 616 return ERR_PTR(-EIOCBQUEUED); 617 618 for (;;) { 619 set_current_state(TASK_UNINTERRUPTIBLE); 620 if (!READ_ONCE(dio->submit.waiter)) 621 break; 622 623 if (!(iocb->ki_flags & IOCB_HIPRI) || 624 !dio->submit.last_queue || 625 !blk_poll(dio->submit.last_queue, 626 dio->submit.cookie, true)) 627 blk_io_schedule(); 628 } 629 __set_current_state(TASK_RUNNING); 630 } 631 632 return dio; 633 634 out_free_dio: 635 kfree(dio); 636 if (ret) 637 return ERR_PTR(ret); 638 return NULL; 639 } 640 EXPORT_SYMBOL_GPL(__iomap_dio_rw); 641 642 ssize_t 643 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, 644 const struct iomap_ops *ops, const struct iomap_dio_ops *dops, 645 unsigned int dio_flags) 646 { 647 struct iomap_dio *dio; 648 649 dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags); 650 if (IS_ERR_OR_NULL(dio)) 651 return PTR_ERR_OR_ZERO(dio); 652 return iomap_dio_complete(dio); 653 } 654 EXPORT_SYMBOL_GPL(iomap_dio_rw); 655