1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2010 Red Hat, Inc. 4 * Copyright (C) 2016-2023 Christoph Hellwig. 5 */ 6 #include <linux/iomap.h> 7 #include <linux/buffer_head.h> 8 #include <linux/writeback.h> 9 #include <linux/swap.h> 10 #include <linux/migrate.h> 11 #include "trace.h" 12 13 #include "../internal.h" 14 15 /* 16 * Structure allocated for each folio to track per-block uptodate, dirty state 17 * and I/O completions. 18 */ 19 struct iomap_folio_state { 20 spinlock_t state_lock; 21 unsigned int read_bytes_pending; 22 atomic_t write_bytes_pending; 23 24 /* 25 * Each block has two bits in this bitmap: 26 * Bits [0..blocks_per_folio) has the uptodate status. 27 * Bits [b_p_f...(2*b_p_f)) has the dirty status. 28 */ 29 unsigned long state[]; 30 }; 31 32 static inline bool ifs_is_fully_uptodate(struct folio *folio, 33 struct iomap_folio_state *ifs) 34 { 35 struct inode *inode = folio->mapping->host; 36 37 return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio)); 38 } 39 40 static inline bool ifs_block_is_uptodate(struct iomap_folio_state *ifs, 41 unsigned int block) 42 { 43 return test_bit(block, ifs->state); 44 } 45 46 static bool ifs_set_range_uptodate(struct folio *folio, 47 struct iomap_folio_state *ifs, size_t off, size_t len) 48 { 49 struct inode *inode = folio->mapping->host; 50 unsigned int first_blk = off >> inode->i_blkbits; 51 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits; 52 unsigned int nr_blks = last_blk - first_blk + 1; 53 54 bitmap_set(ifs->state, first_blk, nr_blks); 55 return ifs_is_fully_uptodate(folio, ifs); 56 } 57 58 static void iomap_set_range_uptodate(struct folio *folio, size_t off, 59 size_t len) 60 { 61 struct iomap_folio_state *ifs = folio->private; 62 unsigned long flags; 63 bool uptodate = true; 64 65 if (folio_test_uptodate(folio)) 66 return; 67 68 if (ifs) { 69 spin_lock_irqsave(&ifs->state_lock, flags); 70 uptodate = ifs_set_range_uptodate(folio, ifs, off, len); 71 spin_unlock_irqrestore(&ifs->state_lock, flags); 72 } 73 74 if (uptodate) 75 folio_mark_uptodate(folio); 76 } 77 78 static inline bool ifs_block_is_dirty(struct folio *folio, 79 struct iomap_folio_state *ifs, int block) 80 { 81 struct inode *inode = folio->mapping->host; 82 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio); 83 84 return test_bit(block + blks_per_folio, ifs->state); 85 } 86 87 static unsigned ifs_find_dirty_range(struct folio *folio, 88 struct iomap_folio_state *ifs, u64 *range_start, u64 range_end) 89 { 90 struct inode *inode = folio->mapping->host; 91 unsigned start_blk = 92 offset_in_folio(folio, *range_start) >> inode->i_blkbits; 93 unsigned end_blk = min_not_zero( 94 offset_in_folio(folio, range_end) >> inode->i_blkbits, 95 i_blocks_per_folio(inode, folio)); 96 unsigned nblks = 1; 97 98 while (!ifs_block_is_dirty(folio, ifs, start_blk)) 99 if (++start_blk == end_blk) 100 return 0; 101 102 while (start_blk + nblks < end_blk) { 103 if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks)) 104 break; 105 nblks++; 106 } 107 108 *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits); 109 return nblks << inode->i_blkbits; 110 } 111 112 static unsigned iomap_find_dirty_range(struct folio *folio, u64 *range_start, 113 u64 range_end) 114 { 115 struct iomap_folio_state *ifs = folio->private; 116 117 if (*range_start >= range_end) 118 return 0; 119 120 if (ifs) 121 return ifs_find_dirty_range(folio, ifs, range_start, range_end); 122 return range_end - *range_start; 123 } 124 125 static void ifs_clear_range_dirty(struct folio *folio, 126 struct iomap_folio_state *ifs, size_t off, size_t len) 127 { 128 struct inode *inode = folio->mapping->host; 129 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio); 130 unsigned int first_blk = (off >> inode->i_blkbits); 131 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits; 132 unsigned int nr_blks = last_blk - first_blk + 1; 133 unsigned long flags; 134 135 spin_lock_irqsave(&ifs->state_lock, flags); 136 bitmap_clear(ifs->state, first_blk + blks_per_folio, nr_blks); 137 spin_unlock_irqrestore(&ifs->state_lock, flags); 138 } 139 140 static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len) 141 { 142 struct iomap_folio_state *ifs = folio->private; 143 144 if (ifs) 145 ifs_clear_range_dirty(folio, ifs, off, len); 146 } 147 148 static void ifs_set_range_dirty(struct folio *folio, 149 struct iomap_folio_state *ifs, size_t off, size_t len) 150 { 151 struct inode *inode = folio->mapping->host; 152 unsigned int blks_per_folio = i_blocks_per_folio(inode, folio); 153 unsigned int first_blk = (off >> inode->i_blkbits); 154 unsigned int last_blk = (off + len - 1) >> inode->i_blkbits; 155 unsigned int nr_blks = last_blk - first_blk + 1; 156 unsigned long flags; 157 158 spin_lock_irqsave(&ifs->state_lock, flags); 159 bitmap_set(ifs->state, first_blk + blks_per_folio, nr_blks); 160 spin_unlock_irqrestore(&ifs->state_lock, flags); 161 } 162 163 static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len) 164 { 165 struct iomap_folio_state *ifs = folio->private; 166 167 if (ifs) 168 ifs_set_range_dirty(folio, ifs, off, len); 169 } 170 171 static struct iomap_folio_state *ifs_alloc(struct inode *inode, 172 struct folio *folio, unsigned int flags) 173 { 174 struct iomap_folio_state *ifs = folio->private; 175 unsigned int nr_blocks = i_blocks_per_folio(inode, folio); 176 gfp_t gfp; 177 178 if (ifs || nr_blocks <= 1) 179 return ifs; 180 181 if (flags & IOMAP_NOWAIT) 182 gfp = GFP_NOWAIT; 183 else 184 gfp = GFP_NOFS | __GFP_NOFAIL; 185 186 /* 187 * ifs->state tracks two sets of state flags when the 188 * filesystem block size is smaller than the folio size. 189 * The first state tracks per-block uptodate and the 190 * second tracks per-block dirty state. 191 */ 192 ifs = kzalloc(struct_size(ifs, state, 193 BITS_TO_LONGS(2 * nr_blocks)), gfp); 194 if (!ifs) 195 return ifs; 196 197 spin_lock_init(&ifs->state_lock); 198 if (folio_test_uptodate(folio)) 199 bitmap_set(ifs->state, 0, nr_blocks); 200 if (folio_test_dirty(folio)) 201 bitmap_set(ifs->state, nr_blocks, nr_blocks); 202 folio_attach_private(folio, ifs); 203 204 return ifs; 205 } 206 207 static void ifs_free(struct folio *folio) 208 { 209 struct iomap_folio_state *ifs = folio_detach_private(folio); 210 211 if (!ifs) 212 return; 213 WARN_ON_ONCE(ifs->read_bytes_pending != 0); 214 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending)); 215 WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) != 216 folio_test_uptodate(folio)); 217 kfree(ifs); 218 } 219 220 /* 221 * Calculate the range inside the folio that we actually need to read. 222 */ 223 static void iomap_adjust_read_range(struct inode *inode, struct folio *folio, 224 loff_t *pos, loff_t length, size_t *offp, size_t *lenp) 225 { 226 struct iomap_folio_state *ifs = folio->private; 227 loff_t orig_pos = *pos; 228 loff_t isize = i_size_read(inode); 229 unsigned block_bits = inode->i_blkbits; 230 unsigned block_size = (1 << block_bits); 231 size_t poff = offset_in_folio(folio, *pos); 232 size_t plen = min_t(loff_t, folio_size(folio) - poff, length); 233 size_t orig_plen = plen; 234 unsigned first = poff >> block_bits; 235 unsigned last = (poff + plen - 1) >> block_bits; 236 237 /* 238 * If the block size is smaller than the page size, we need to check the 239 * per-block uptodate status and adjust the offset and length if needed 240 * to avoid reading in already uptodate ranges. 241 */ 242 if (ifs) { 243 unsigned int i; 244 245 /* move forward for each leading block marked uptodate */ 246 for (i = first; i <= last; i++) { 247 if (!ifs_block_is_uptodate(ifs, i)) 248 break; 249 *pos += block_size; 250 poff += block_size; 251 plen -= block_size; 252 first++; 253 } 254 255 /* truncate len if we find any trailing uptodate block(s) */ 256 while (++i <= last) { 257 if (ifs_block_is_uptodate(ifs, i)) { 258 plen -= (last - i + 1) * block_size; 259 last = i - 1; 260 break; 261 } 262 } 263 } 264 265 /* 266 * If the extent spans the block that contains the i_size, we need to 267 * handle both halves separately so that we properly zero data in the 268 * page cache for blocks that are entirely outside of i_size. 269 */ 270 if (orig_pos <= isize && orig_pos + orig_plen > isize) { 271 unsigned end = offset_in_folio(folio, isize - 1) >> block_bits; 272 273 if (first <= end && last > end) 274 plen -= (last - end) * block_size; 275 } 276 277 *offp = poff; 278 *lenp = plen; 279 } 280 281 static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter, 282 loff_t pos) 283 { 284 const struct iomap *srcmap = iomap_iter_srcmap(iter); 285 286 return srcmap->type != IOMAP_MAPPED || 287 (srcmap->flags & IOMAP_F_NEW) || 288 pos >= i_size_read(iter->inode); 289 } 290 291 /** 292 * iomap_read_inline_data - copy inline data into the page cache 293 * @iter: iteration structure 294 * @folio: folio to copy to 295 * 296 * Copy the inline data in @iter into @folio and zero out the rest of the folio. 297 * Only a single IOMAP_INLINE extent is allowed at the end of each file. 298 * Returns zero for success to complete the read, or the usual negative errno. 299 */ 300 static int iomap_read_inline_data(const struct iomap_iter *iter, 301 struct folio *folio) 302 { 303 const struct iomap *iomap = iomap_iter_srcmap(iter); 304 size_t size = i_size_read(iter->inode) - iomap->offset; 305 size_t offset = offset_in_folio(folio, iomap->offset); 306 307 if (folio_test_uptodate(folio)) 308 return 0; 309 310 if (WARN_ON_ONCE(size > iomap->length)) 311 return -EIO; 312 if (offset > 0) 313 ifs_alloc(iter->inode, folio, iter->flags); 314 315 folio_fill_tail(folio, offset, iomap->inline_data, size); 316 iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset); 317 return 0; 318 } 319 320 #ifdef CONFIG_BLOCK 321 static void iomap_finish_folio_read(struct folio *folio, size_t off, 322 size_t len, int error) 323 { 324 struct iomap_folio_state *ifs = folio->private; 325 bool uptodate = !error; 326 bool finished = true; 327 328 if (ifs) { 329 unsigned long flags; 330 331 spin_lock_irqsave(&ifs->state_lock, flags); 332 if (!error) 333 uptodate = ifs_set_range_uptodate(folio, ifs, off, len); 334 ifs->read_bytes_pending -= len; 335 finished = !ifs->read_bytes_pending; 336 spin_unlock_irqrestore(&ifs->state_lock, flags); 337 } 338 339 if (finished) 340 folio_end_read(folio, uptodate); 341 } 342 343 static void iomap_read_end_io(struct bio *bio) 344 { 345 int error = blk_status_to_errno(bio->bi_status); 346 struct folio_iter fi; 347 348 bio_for_each_folio_all(fi, bio) 349 iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error); 350 bio_put(bio); 351 } 352 353 struct iomap_readpage_ctx { 354 struct folio *cur_folio; 355 bool cur_folio_in_bio; 356 struct bio *bio; 357 struct readahead_control *rac; 358 }; 359 360 static int iomap_readpage_iter(struct iomap_iter *iter, 361 struct iomap_readpage_ctx *ctx) 362 { 363 const struct iomap *iomap = &iter->iomap; 364 loff_t pos = iter->pos; 365 loff_t length = iomap_length(iter); 366 struct folio *folio = ctx->cur_folio; 367 struct iomap_folio_state *ifs; 368 size_t poff, plen; 369 sector_t sector; 370 int ret; 371 372 if (iomap->type == IOMAP_INLINE) { 373 ret = iomap_read_inline_data(iter, folio); 374 if (ret) 375 return ret; 376 return iomap_iter_advance(iter, &length); 377 } 378 379 /* zero post-eof blocks as the page may be mapped */ 380 ifs = ifs_alloc(iter->inode, folio, iter->flags); 381 iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen); 382 if (plen == 0) 383 goto done; 384 385 if (iomap_block_needs_zeroing(iter, pos)) { 386 folio_zero_range(folio, poff, plen); 387 iomap_set_range_uptodate(folio, poff, plen); 388 goto done; 389 } 390 391 ctx->cur_folio_in_bio = true; 392 if (ifs) { 393 spin_lock_irq(&ifs->state_lock); 394 ifs->read_bytes_pending += plen; 395 spin_unlock_irq(&ifs->state_lock); 396 } 397 398 sector = iomap_sector(iomap, pos); 399 if (!ctx->bio || 400 bio_end_sector(ctx->bio) != sector || 401 !bio_add_folio(ctx->bio, folio, plen, poff)) { 402 gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL); 403 gfp_t orig_gfp = gfp; 404 unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE); 405 406 if (ctx->bio) 407 submit_bio(ctx->bio); 408 409 if (ctx->rac) /* same as readahead_gfp_mask */ 410 gfp |= __GFP_NORETRY | __GFP_NOWARN; 411 ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs), 412 REQ_OP_READ, gfp); 413 /* 414 * If the bio_alloc fails, try it again for a single page to 415 * avoid having to deal with partial page reads. This emulates 416 * what do_mpage_read_folio does. 417 */ 418 if (!ctx->bio) { 419 ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ, 420 orig_gfp); 421 } 422 if (ctx->rac) 423 ctx->bio->bi_opf |= REQ_RAHEAD; 424 ctx->bio->bi_iter.bi_sector = sector; 425 ctx->bio->bi_end_io = iomap_read_end_io; 426 bio_add_folio_nofail(ctx->bio, folio, plen, poff); 427 } 428 429 done: 430 /* 431 * Move the caller beyond our range so that it keeps making progress. 432 * For that, we have to include any leading non-uptodate ranges, but 433 * we can skip trailing ones as they will be handled in the next 434 * iteration. 435 */ 436 length = pos - iter->pos + plen; 437 return iomap_iter_advance(iter, &length); 438 } 439 440 static int iomap_read_folio_iter(struct iomap_iter *iter, 441 struct iomap_readpage_ctx *ctx) 442 { 443 int ret; 444 445 while (iomap_length(iter)) { 446 ret = iomap_readpage_iter(iter, ctx); 447 if (ret) 448 return ret; 449 } 450 451 return 0; 452 } 453 454 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops) 455 { 456 struct iomap_iter iter = { 457 .inode = folio->mapping->host, 458 .pos = folio_pos(folio), 459 .len = folio_size(folio), 460 }; 461 struct iomap_readpage_ctx ctx = { 462 .cur_folio = folio, 463 }; 464 int ret; 465 466 trace_iomap_readpage(iter.inode, 1); 467 468 while ((ret = iomap_iter(&iter, ops)) > 0) 469 iter.status = iomap_read_folio_iter(&iter, &ctx); 470 471 if (ctx.bio) { 472 submit_bio(ctx.bio); 473 WARN_ON_ONCE(!ctx.cur_folio_in_bio); 474 } else { 475 WARN_ON_ONCE(ctx.cur_folio_in_bio); 476 folio_unlock(folio); 477 } 478 479 /* 480 * Just like mpage_readahead and block_read_full_folio, we always 481 * return 0 and just set the folio error flag on errors. This 482 * should be cleaned up throughout the stack eventually. 483 */ 484 return 0; 485 } 486 EXPORT_SYMBOL_GPL(iomap_read_folio); 487 488 static int iomap_readahead_iter(struct iomap_iter *iter, 489 struct iomap_readpage_ctx *ctx) 490 { 491 int ret; 492 493 while (iomap_length(iter)) { 494 if (ctx->cur_folio && 495 offset_in_folio(ctx->cur_folio, iter->pos) == 0) { 496 if (!ctx->cur_folio_in_bio) 497 folio_unlock(ctx->cur_folio); 498 ctx->cur_folio = NULL; 499 } 500 if (!ctx->cur_folio) { 501 ctx->cur_folio = readahead_folio(ctx->rac); 502 ctx->cur_folio_in_bio = false; 503 } 504 ret = iomap_readpage_iter(iter, ctx); 505 if (ret) 506 return ret; 507 } 508 509 return 0; 510 } 511 512 /** 513 * iomap_readahead - Attempt to read pages from a file. 514 * @rac: Describes the pages to be read. 515 * @ops: The operations vector for the filesystem. 516 * 517 * This function is for filesystems to call to implement their readahead 518 * address_space operation. 519 * 520 * Context: The @ops callbacks may submit I/O (eg to read the addresses of 521 * blocks from disc), and may wait for it. The caller may be trying to 522 * access a different page, and so sleeping excessively should be avoided. 523 * It may allocate memory, but should avoid costly allocations. This 524 * function is called with memalloc_nofs set, so allocations will not cause 525 * the filesystem to be reentered. 526 */ 527 void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops) 528 { 529 struct iomap_iter iter = { 530 .inode = rac->mapping->host, 531 .pos = readahead_pos(rac), 532 .len = readahead_length(rac), 533 }; 534 struct iomap_readpage_ctx ctx = { 535 .rac = rac, 536 }; 537 538 trace_iomap_readahead(rac->mapping->host, readahead_count(rac)); 539 540 while (iomap_iter(&iter, ops) > 0) 541 iter.status = iomap_readahead_iter(&iter, &ctx); 542 543 if (ctx.bio) 544 submit_bio(ctx.bio); 545 if (ctx.cur_folio) { 546 if (!ctx.cur_folio_in_bio) 547 folio_unlock(ctx.cur_folio); 548 } 549 } 550 EXPORT_SYMBOL_GPL(iomap_readahead); 551 552 static int iomap_read_folio_range(const struct iomap_iter *iter, 553 struct folio *folio, loff_t pos, size_t len) 554 { 555 const struct iomap *srcmap = iomap_iter_srcmap(iter); 556 struct bio_vec bvec; 557 struct bio bio; 558 559 bio_init(&bio, srcmap->bdev, &bvec, 1, REQ_OP_READ); 560 bio.bi_iter.bi_sector = iomap_sector(srcmap, pos); 561 bio_add_folio_nofail(&bio, folio, len, offset_in_folio(folio, pos)); 562 return submit_bio_wait(&bio); 563 } 564 #else 565 static int iomap_read_folio_range(const struct iomap_iter *iter, 566 struct folio *folio, loff_t pos, size_t len) 567 { 568 WARN_ON_ONCE(1); 569 return -EIO; 570 } 571 #endif /* CONFIG_BLOCK */ 572 573 /* 574 * iomap_is_partially_uptodate checks whether blocks within a folio are 575 * uptodate or not. 576 * 577 * Returns true if all blocks which correspond to the specified part 578 * of the folio are uptodate. 579 */ 580 bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count) 581 { 582 struct iomap_folio_state *ifs = folio->private; 583 struct inode *inode = folio->mapping->host; 584 unsigned first, last, i; 585 586 if (!ifs) 587 return false; 588 589 /* Caller's range may extend past the end of this folio */ 590 count = min(folio_size(folio) - from, count); 591 592 /* First and last blocks in range within folio */ 593 first = from >> inode->i_blkbits; 594 last = (from + count - 1) >> inode->i_blkbits; 595 596 for (i = first; i <= last; i++) 597 if (!ifs_block_is_uptodate(ifs, i)) 598 return false; 599 return true; 600 } 601 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate); 602 603 /** 604 * iomap_get_folio - get a folio reference for writing 605 * @iter: iteration structure 606 * @pos: start offset of write 607 * @len: Suggested size of folio to create. 608 * 609 * Returns a locked reference to the folio at @pos, or an error pointer if the 610 * folio could not be obtained. 611 */ 612 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len) 613 { 614 fgf_t fgp = FGP_WRITEBEGIN | FGP_NOFS; 615 616 if (iter->flags & IOMAP_NOWAIT) 617 fgp |= FGP_NOWAIT; 618 if (iter->flags & IOMAP_DONTCACHE) 619 fgp |= FGP_DONTCACHE; 620 fgp |= fgf_set_order(len); 621 622 return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT, 623 fgp, mapping_gfp_mask(iter->inode->i_mapping)); 624 } 625 EXPORT_SYMBOL_GPL(iomap_get_folio); 626 627 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags) 628 { 629 trace_iomap_release_folio(folio->mapping->host, folio_pos(folio), 630 folio_size(folio)); 631 632 /* 633 * If the folio is dirty, we refuse to release our metadata because 634 * it may be partially dirty. Once we track per-block dirty state, 635 * we can release the metadata if every block is dirty. 636 */ 637 if (folio_test_dirty(folio)) 638 return false; 639 ifs_free(folio); 640 return true; 641 } 642 EXPORT_SYMBOL_GPL(iomap_release_folio); 643 644 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len) 645 { 646 trace_iomap_invalidate_folio(folio->mapping->host, 647 folio_pos(folio) + offset, len); 648 649 /* 650 * If we're invalidating the entire folio, clear the dirty state 651 * from it and release it to avoid unnecessary buildup of the LRU. 652 */ 653 if (offset == 0 && len == folio_size(folio)) { 654 WARN_ON_ONCE(folio_test_writeback(folio)); 655 folio_cancel_dirty(folio); 656 ifs_free(folio); 657 } 658 } 659 EXPORT_SYMBOL_GPL(iomap_invalidate_folio); 660 661 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio) 662 { 663 struct inode *inode = mapping->host; 664 size_t len = folio_size(folio); 665 666 ifs_alloc(inode, folio, 0); 667 iomap_set_range_dirty(folio, 0, len); 668 return filemap_dirty_folio(mapping, folio); 669 } 670 EXPORT_SYMBOL_GPL(iomap_dirty_folio); 671 672 static void 673 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len) 674 { 675 loff_t i_size = i_size_read(inode); 676 677 /* 678 * Only truncate newly allocated pages beyoned EOF, even if the 679 * write started inside the existing inode size. 680 */ 681 if (pos + len > i_size) 682 truncate_pagecache_range(inode, max(pos, i_size), 683 pos + len - 1); 684 } 685 686 static int __iomap_write_begin(const struct iomap_iter *iter, 687 const struct iomap_write_ops *write_ops, size_t len, 688 struct folio *folio) 689 { 690 struct iomap_folio_state *ifs; 691 loff_t pos = iter->pos; 692 loff_t block_size = i_blocksize(iter->inode); 693 loff_t block_start = round_down(pos, block_size); 694 loff_t block_end = round_up(pos + len, block_size); 695 unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio); 696 size_t from = offset_in_folio(folio, pos), to = from + len; 697 size_t poff, plen; 698 699 /* 700 * If the write or zeroing completely overlaps the current folio, then 701 * entire folio will be dirtied so there is no need for 702 * per-block state tracking structures to be attached to this folio. 703 * For the unshare case, we must read in the ondisk contents because we 704 * are not changing pagecache contents. 705 */ 706 if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) && 707 pos + len >= folio_pos(folio) + folio_size(folio)) 708 return 0; 709 710 ifs = ifs_alloc(iter->inode, folio, iter->flags); 711 if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1) 712 return -EAGAIN; 713 714 if (folio_test_uptodate(folio)) 715 return 0; 716 717 do { 718 iomap_adjust_read_range(iter->inode, folio, &block_start, 719 block_end - block_start, &poff, &plen); 720 if (plen == 0) 721 break; 722 723 if (!(iter->flags & IOMAP_UNSHARE) && 724 (from <= poff || from >= poff + plen) && 725 (to <= poff || to >= poff + plen)) 726 continue; 727 728 if (iomap_block_needs_zeroing(iter, block_start)) { 729 if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE)) 730 return -EIO; 731 folio_zero_segments(folio, poff, from, to, poff + plen); 732 } else { 733 int status; 734 735 if (iter->flags & IOMAP_NOWAIT) 736 return -EAGAIN; 737 738 if (write_ops && write_ops->read_folio_range) 739 status = write_ops->read_folio_range(iter, 740 folio, block_start, plen); 741 else 742 status = iomap_read_folio_range(iter, 743 folio, block_start, plen); 744 if (status) 745 return status; 746 } 747 iomap_set_range_uptodate(folio, poff, plen); 748 } while ((block_start += plen) < block_end); 749 750 return 0; 751 } 752 753 static struct folio *__iomap_get_folio(struct iomap_iter *iter, 754 const struct iomap_write_ops *write_ops, size_t len) 755 { 756 loff_t pos = iter->pos; 757 758 if (!mapping_large_folio_support(iter->inode->i_mapping)) 759 len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos)); 760 761 if (write_ops && write_ops->get_folio) 762 return write_ops->get_folio(iter, pos, len); 763 return iomap_get_folio(iter, pos, len); 764 } 765 766 static void __iomap_put_folio(struct iomap_iter *iter, 767 const struct iomap_write_ops *write_ops, size_t ret, 768 struct folio *folio) 769 { 770 loff_t pos = iter->pos; 771 772 if (write_ops && write_ops->put_folio) { 773 write_ops->put_folio(iter->inode, pos, ret, folio); 774 } else { 775 folio_unlock(folio); 776 folio_put(folio); 777 } 778 } 779 780 /* trim pos and bytes to within a given folio */ 781 static loff_t iomap_trim_folio_range(struct iomap_iter *iter, 782 struct folio *folio, size_t *offset, u64 *bytes) 783 { 784 loff_t pos = iter->pos; 785 size_t fsize = folio_size(folio); 786 787 WARN_ON_ONCE(pos < folio_pos(folio)); 788 WARN_ON_ONCE(pos >= folio_pos(folio) + fsize); 789 790 *offset = offset_in_folio(folio, pos); 791 *bytes = min(*bytes, fsize - *offset); 792 793 return pos; 794 } 795 796 static int iomap_write_begin_inline(const struct iomap_iter *iter, 797 struct folio *folio) 798 { 799 /* needs more work for the tailpacking case; disable for now */ 800 if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0)) 801 return -EIO; 802 return iomap_read_inline_data(iter, folio); 803 } 804 805 /* 806 * Grab and prepare a folio for write based on iter state. Returns the folio, 807 * offset, and length. Callers can optionally pass a max length *plen, 808 * otherwise init to zero. 809 */ 810 static int iomap_write_begin(struct iomap_iter *iter, 811 const struct iomap_write_ops *write_ops, struct folio **foliop, 812 size_t *poffset, u64 *plen) 813 { 814 const struct iomap *srcmap = iomap_iter_srcmap(iter); 815 loff_t pos = iter->pos; 816 u64 len = min_t(u64, SIZE_MAX, iomap_length(iter)); 817 struct folio *folio; 818 int status = 0; 819 820 len = min_not_zero(len, *plen); 821 BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length); 822 if (srcmap != &iter->iomap) 823 BUG_ON(pos + len > srcmap->offset + srcmap->length); 824 825 if (fatal_signal_pending(current)) 826 return -EINTR; 827 828 folio = __iomap_get_folio(iter, write_ops, len); 829 if (IS_ERR(folio)) 830 return PTR_ERR(folio); 831 832 /* 833 * Now we have a locked folio, before we do anything with it we need to 834 * check that the iomap we have cached is not stale. The inode extent 835 * mapping can change due to concurrent IO in flight (e.g. 836 * IOMAP_UNWRITTEN state can change and memory reclaim could have 837 * reclaimed a previously partially written page at this index after IO 838 * completion before this write reaches this file offset) and hence we 839 * could do the wrong thing here (zero a page range incorrectly or fail 840 * to zero) and corrupt data. 841 */ 842 if (write_ops && write_ops->iomap_valid) { 843 bool iomap_valid = write_ops->iomap_valid(iter->inode, 844 &iter->iomap); 845 if (!iomap_valid) { 846 iter->iomap.flags |= IOMAP_F_STALE; 847 status = 0; 848 goto out_unlock; 849 } 850 } 851 852 pos = iomap_trim_folio_range(iter, folio, poffset, &len); 853 854 if (srcmap->type == IOMAP_INLINE) 855 status = iomap_write_begin_inline(iter, folio); 856 else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) 857 status = __block_write_begin_int(folio, pos, len, NULL, srcmap); 858 else 859 status = __iomap_write_begin(iter, write_ops, len, folio); 860 861 if (unlikely(status)) 862 goto out_unlock; 863 864 *foliop = folio; 865 *plen = len; 866 return 0; 867 868 out_unlock: 869 __iomap_put_folio(iter, write_ops, 0, folio); 870 return status; 871 } 872 873 static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len, 874 size_t copied, struct folio *folio) 875 { 876 flush_dcache_folio(folio); 877 878 /* 879 * The blocks that were entirely written will now be uptodate, so we 880 * don't have to worry about a read_folio reading them and overwriting a 881 * partial write. However, if we've encountered a short write and only 882 * partially written into a block, it will not be marked uptodate, so a 883 * read_folio might come in and destroy our partial write. 884 * 885 * Do the simplest thing and just treat any short write to a 886 * non-uptodate page as a zero-length write, and force the caller to 887 * redo the whole thing. 888 */ 889 if (unlikely(copied < len && !folio_test_uptodate(folio))) 890 return false; 891 iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len); 892 iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied); 893 filemap_dirty_folio(inode->i_mapping, folio); 894 return true; 895 } 896 897 static void iomap_write_end_inline(const struct iomap_iter *iter, 898 struct folio *folio, loff_t pos, size_t copied) 899 { 900 const struct iomap *iomap = &iter->iomap; 901 void *addr; 902 903 WARN_ON_ONCE(!folio_test_uptodate(folio)); 904 BUG_ON(!iomap_inline_data_valid(iomap)); 905 906 flush_dcache_folio(folio); 907 addr = kmap_local_folio(folio, pos); 908 memcpy(iomap_inline_data(iomap, pos), addr, copied); 909 kunmap_local(addr); 910 911 mark_inode_dirty(iter->inode); 912 } 913 914 /* 915 * Returns true if all copied bytes have been written to the pagecache, 916 * otherwise return false. 917 */ 918 static bool iomap_write_end(struct iomap_iter *iter, size_t len, size_t copied, 919 struct folio *folio) 920 { 921 const struct iomap *srcmap = iomap_iter_srcmap(iter); 922 loff_t pos = iter->pos; 923 924 if (srcmap->type == IOMAP_INLINE) { 925 iomap_write_end_inline(iter, folio, pos, copied); 926 return true; 927 } 928 929 if (srcmap->flags & IOMAP_F_BUFFER_HEAD) { 930 size_t bh_written; 931 932 bh_written = block_write_end(pos, len, copied, folio); 933 WARN_ON_ONCE(bh_written != copied && bh_written != 0); 934 return bh_written == copied; 935 } 936 937 return __iomap_write_end(iter->inode, pos, len, copied, folio); 938 } 939 940 static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i, 941 const struct iomap_write_ops *write_ops) 942 { 943 ssize_t total_written = 0; 944 int status = 0; 945 struct address_space *mapping = iter->inode->i_mapping; 946 size_t chunk = mapping_max_folio_size(mapping); 947 unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0; 948 949 do { 950 struct folio *folio; 951 loff_t old_size; 952 size_t offset; /* Offset into folio */ 953 u64 bytes; /* Bytes to write to folio */ 954 size_t copied; /* Bytes copied from user */ 955 u64 written; /* Bytes have been written */ 956 loff_t pos; 957 958 bytes = iov_iter_count(i); 959 retry: 960 offset = iter->pos & (chunk - 1); 961 bytes = min(chunk - offset, bytes); 962 status = balance_dirty_pages_ratelimited_flags(mapping, 963 bdp_flags); 964 if (unlikely(status)) 965 break; 966 967 if (bytes > iomap_length(iter)) 968 bytes = iomap_length(iter); 969 970 /* 971 * Bring in the user page that we'll copy from _first_. 972 * Otherwise there's a nasty deadlock on copying from the 973 * same page as we're writing to, without it being marked 974 * up-to-date. 975 * 976 * For async buffered writes the assumption is that the user 977 * page has already been faulted in. This can be optimized by 978 * faulting the user page. 979 */ 980 if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) { 981 status = -EFAULT; 982 break; 983 } 984 985 status = iomap_write_begin(iter, write_ops, &folio, &offset, 986 &bytes); 987 if (unlikely(status)) { 988 iomap_write_failed(iter->inode, iter->pos, bytes); 989 break; 990 } 991 if (iter->iomap.flags & IOMAP_F_STALE) 992 break; 993 994 pos = iter->pos; 995 996 if (mapping_writably_mapped(mapping)) 997 flush_dcache_folio(folio); 998 999 copied = copy_folio_from_iter_atomic(folio, offset, bytes, i); 1000 written = iomap_write_end(iter, bytes, copied, folio) ? 1001 copied : 0; 1002 1003 /* 1004 * Update the in-memory inode size after copying the data into 1005 * the page cache. It's up to the file system to write the 1006 * updated size to disk, preferably after I/O completion so that 1007 * no stale data is exposed. Only once that's done can we 1008 * unlock and release the folio. 1009 */ 1010 old_size = iter->inode->i_size; 1011 if (pos + written > old_size) { 1012 i_size_write(iter->inode, pos + written); 1013 iter->iomap.flags |= IOMAP_F_SIZE_CHANGED; 1014 } 1015 __iomap_put_folio(iter, write_ops, written, folio); 1016 1017 if (old_size < pos) 1018 pagecache_isize_extended(iter->inode, old_size, pos); 1019 1020 cond_resched(); 1021 if (unlikely(written == 0)) { 1022 /* 1023 * A short copy made iomap_write_end() reject the 1024 * thing entirely. Might be memory poisoning 1025 * halfway through, might be a race with munmap, 1026 * might be severe memory pressure. 1027 */ 1028 iomap_write_failed(iter->inode, pos, bytes); 1029 iov_iter_revert(i, copied); 1030 1031 if (chunk > PAGE_SIZE) 1032 chunk /= 2; 1033 if (copied) { 1034 bytes = copied; 1035 goto retry; 1036 } 1037 } else { 1038 total_written += written; 1039 iomap_iter_advance(iter, &written); 1040 } 1041 } while (iov_iter_count(i) && iomap_length(iter)); 1042 1043 return total_written ? 0 : status; 1044 } 1045 1046 ssize_t 1047 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i, 1048 const struct iomap_ops *ops, 1049 const struct iomap_write_ops *write_ops, void *private) 1050 { 1051 struct iomap_iter iter = { 1052 .inode = iocb->ki_filp->f_mapping->host, 1053 .pos = iocb->ki_pos, 1054 .len = iov_iter_count(i), 1055 .flags = IOMAP_WRITE, 1056 .private = private, 1057 }; 1058 ssize_t ret; 1059 1060 if (iocb->ki_flags & IOCB_NOWAIT) 1061 iter.flags |= IOMAP_NOWAIT; 1062 if (iocb->ki_flags & IOCB_DONTCACHE) 1063 iter.flags |= IOMAP_DONTCACHE; 1064 1065 while ((ret = iomap_iter(&iter, ops)) > 0) 1066 iter.status = iomap_write_iter(&iter, i, write_ops); 1067 1068 if (unlikely(iter.pos == iocb->ki_pos)) 1069 return ret; 1070 ret = iter.pos - iocb->ki_pos; 1071 iocb->ki_pos = iter.pos; 1072 return ret; 1073 } 1074 EXPORT_SYMBOL_GPL(iomap_file_buffered_write); 1075 1076 static void iomap_write_delalloc_ifs_punch(struct inode *inode, 1077 struct folio *folio, loff_t start_byte, loff_t end_byte, 1078 struct iomap *iomap, iomap_punch_t punch) 1079 { 1080 unsigned int first_blk, last_blk, i; 1081 loff_t last_byte; 1082 u8 blkbits = inode->i_blkbits; 1083 struct iomap_folio_state *ifs; 1084 1085 /* 1086 * When we have per-block dirty tracking, there can be 1087 * blocks within a folio which are marked uptodate 1088 * but not dirty. In that case it is necessary to punch 1089 * out such blocks to avoid leaking any delalloc blocks. 1090 */ 1091 ifs = folio->private; 1092 if (!ifs) 1093 return; 1094 1095 last_byte = min_t(loff_t, end_byte - 1, 1096 folio_pos(folio) + folio_size(folio) - 1); 1097 first_blk = offset_in_folio(folio, start_byte) >> blkbits; 1098 last_blk = offset_in_folio(folio, last_byte) >> blkbits; 1099 for (i = first_blk; i <= last_blk; i++) { 1100 if (!ifs_block_is_dirty(folio, ifs, i)) 1101 punch(inode, folio_pos(folio) + (i << blkbits), 1102 1 << blkbits, iomap); 1103 } 1104 } 1105 1106 static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio, 1107 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte, 1108 struct iomap *iomap, iomap_punch_t punch) 1109 { 1110 if (!folio_test_dirty(folio)) 1111 return; 1112 1113 /* if dirty, punch up to offset */ 1114 if (start_byte > *punch_start_byte) { 1115 punch(inode, *punch_start_byte, start_byte - *punch_start_byte, 1116 iomap); 1117 } 1118 1119 /* Punch non-dirty blocks within folio */ 1120 iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte, 1121 iomap, punch); 1122 1123 /* 1124 * Make sure the next punch start is correctly bound to 1125 * the end of this data range, not the end of the folio. 1126 */ 1127 *punch_start_byte = min_t(loff_t, end_byte, 1128 folio_pos(folio) + folio_size(folio)); 1129 } 1130 1131 /* 1132 * Scan the data range passed to us for dirty page cache folios. If we find a 1133 * dirty folio, punch out the preceding range and update the offset from which 1134 * the next punch will start from. 1135 * 1136 * We can punch out storage reservations under clean pages because they either 1137 * contain data that has been written back - in which case the delalloc punch 1138 * over that range is a no-op - or they have been read faults in which case they 1139 * contain zeroes and we can remove the delalloc backing range and any new 1140 * writes to those pages will do the normal hole filling operation... 1141 * 1142 * This makes the logic simple: we only need to keep the delalloc extents only 1143 * over the dirty ranges of the page cache. 1144 * 1145 * This function uses [start_byte, end_byte) intervals (i.e. open ended) to 1146 * simplify range iterations. 1147 */ 1148 static void iomap_write_delalloc_scan(struct inode *inode, 1149 loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte, 1150 struct iomap *iomap, iomap_punch_t punch) 1151 { 1152 while (start_byte < end_byte) { 1153 struct folio *folio; 1154 1155 /* grab locked page */ 1156 folio = filemap_lock_folio(inode->i_mapping, 1157 start_byte >> PAGE_SHIFT); 1158 if (IS_ERR(folio)) { 1159 start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) + 1160 PAGE_SIZE; 1161 continue; 1162 } 1163 1164 iomap_write_delalloc_punch(inode, folio, punch_start_byte, 1165 start_byte, end_byte, iomap, punch); 1166 1167 /* move offset to start of next folio in range */ 1168 start_byte = folio_pos(folio) + folio_size(folio); 1169 folio_unlock(folio); 1170 folio_put(folio); 1171 } 1172 } 1173 1174 /* 1175 * When a short write occurs, the filesystem might need to use ->iomap_end 1176 * to remove space reservations created in ->iomap_begin. 1177 * 1178 * For filesystems that use delayed allocation, there can be dirty pages over 1179 * the delalloc extent outside the range of a short write but still within the 1180 * delalloc extent allocated for this iomap if the write raced with page 1181 * faults. 1182 * 1183 * Punch out all the delalloc blocks in the range given except for those that 1184 * have dirty data still pending in the page cache - those are going to be 1185 * written and so must still retain the delalloc backing for writeback. 1186 * 1187 * The punch() callback *must* only punch delalloc extents in the range passed 1188 * to it. It must skip over all other types of extents in the range and leave 1189 * them completely unchanged. It must do this punch atomically with respect to 1190 * other extent modifications. 1191 * 1192 * The punch() callback may be called with a folio locked to prevent writeback 1193 * extent allocation racing at the edge of the range we are currently punching. 1194 * The locked folio may or may not cover the range being punched, so it is not 1195 * safe for the punch() callback to lock folios itself. 1196 * 1197 * Lock order is: 1198 * 1199 * inode->i_rwsem (shared or exclusive) 1200 * inode->i_mapping->invalidate_lock (exclusive) 1201 * folio_lock() 1202 * ->punch 1203 * internal filesystem allocation lock 1204 * 1205 * As we are scanning the page cache for data, we don't need to reimplement the 1206 * wheel - mapping_seek_hole_data() does exactly what we need to identify the 1207 * start and end of data ranges correctly even for sub-folio block sizes. This 1208 * byte range based iteration is especially convenient because it means we 1209 * don't have to care about variable size folios, nor where the start or end of 1210 * the data range lies within a folio, if they lie within the same folio or even 1211 * if there are multiple discontiguous data ranges within the folio. 1212 * 1213 * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so 1214 * can return data ranges that exist in the cache beyond EOF. e.g. a page fault 1215 * spanning EOF will initialise the post-EOF data to zeroes and mark it up to 1216 * date. A write page fault can then mark it dirty. If we then fail a write() 1217 * beyond EOF into that up to date cached range, we allocate a delalloc block 1218 * beyond EOF and then have to punch it out. Because the range is up to date, 1219 * mapping_seek_hole_data() will return it, and we will skip the punch because 1220 * the folio is dirty. THis is incorrect - we always need to punch out delalloc 1221 * beyond EOF in this case as writeback will never write back and covert that 1222 * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF, 1223 * resulting in always punching out the range from the EOF to the end of the 1224 * range the iomap spans. 1225 * 1226 * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it 1227 * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA 1228 * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte) 1229 * returns the end of the data range (data_end). Using closed intervals would 1230 * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose 1231 * the code to subtle off-by-one bugs.... 1232 */ 1233 void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte, 1234 loff_t end_byte, unsigned flags, struct iomap *iomap, 1235 iomap_punch_t punch) 1236 { 1237 loff_t punch_start_byte = start_byte; 1238 loff_t scan_end_byte = min(i_size_read(inode), end_byte); 1239 1240 /* 1241 * The caller must hold invalidate_lock to avoid races with page faults 1242 * re-instantiating folios and dirtying them via ->page_mkwrite whilst 1243 * we walk the cache and perform delalloc extent removal. Failing to do 1244 * this can leave dirty pages with no space reservation in the cache. 1245 */ 1246 lockdep_assert_held_write(&inode->i_mapping->invalidate_lock); 1247 1248 while (start_byte < scan_end_byte) { 1249 loff_t data_end; 1250 1251 start_byte = mapping_seek_hole_data(inode->i_mapping, 1252 start_byte, scan_end_byte, SEEK_DATA); 1253 /* 1254 * If there is no more data to scan, all that is left is to 1255 * punch out the remaining range. 1256 * 1257 * Note that mapping_seek_hole_data is only supposed to return 1258 * either an offset or -ENXIO, so WARN on any other error as 1259 * that would be an API change without updating the callers. 1260 */ 1261 if (start_byte == -ENXIO || start_byte == scan_end_byte) 1262 break; 1263 if (WARN_ON_ONCE(start_byte < 0)) 1264 return; 1265 WARN_ON_ONCE(start_byte < punch_start_byte); 1266 WARN_ON_ONCE(start_byte > scan_end_byte); 1267 1268 /* 1269 * We find the end of this contiguous cached data range by 1270 * seeking from start_byte to the beginning of the next hole. 1271 */ 1272 data_end = mapping_seek_hole_data(inode->i_mapping, start_byte, 1273 scan_end_byte, SEEK_HOLE); 1274 if (WARN_ON_ONCE(data_end < 0)) 1275 return; 1276 1277 /* 1278 * If we race with post-direct I/O invalidation of the page cache, 1279 * there might be no data left at start_byte. 1280 */ 1281 if (data_end == start_byte) 1282 continue; 1283 1284 WARN_ON_ONCE(data_end < start_byte); 1285 WARN_ON_ONCE(data_end > scan_end_byte); 1286 1287 iomap_write_delalloc_scan(inode, &punch_start_byte, start_byte, 1288 data_end, iomap, punch); 1289 1290 /* The next data search starts at the end of this one. */ 1291 start_byte = data_end; 1292 } 1293 1294 if (punch_start_byte < end_byte) 1295 punch(inode, punch_start_byte, end_byte - punch_start_byte, 1296 iomap); 1297 } 1298 EXPORT_SYMBOL_GPL(iomap_write_delalloc_release); 1299 1300 static int iomap_unshare_iter(struct iomap_iter *iter, 1301 const struct iomap_write_ops *write_ops) 1302 { 1303 struct iomap *iomap = &iter->iomap; 1304 u64 bytes = iomap_length(iter); 1305 int status; 1306 1307 if (!iomap_want_unshare_iter(iter)) 1308 return iomap_iter_advance(iter, &bytes); 1309 1310 do { 1311 struct folio *folio; 1312 size_t offset; 1313 bool ret; 1314 1315 bytes = min_t(u64, SIZE_MAX, bytes); 1316 status = iomap_write_begin(iter, write_ops, &folio, &offset, 1317 &bytes); 1318 if (unlikely(status)) 1319 return status; 1320 if (iomap->flags & IOMAP_F_STALE) 1321 break; 1322 1323 ret = iomap_write_end(iter, bytes, bytes, folio); 1324 __iomap_put_folio(iter, write_ops, bytes, folio); 1325 if (WARN_ON_ONCE(!ret)) 1326 return -EIO; 1327 1328 cond_resched(); 1329 1330 balance_dirty_pages_ratelimited(iter->inode->i_mapping); 1331 1332 status = iomap_iter_advance(iter, &bytes); 1333 if (status) 1334 break; 1335 } while (bytes > 0); 1336 1337 return status; 1338 } 1339 1340 int 1341 iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len, 1342 const struct iomap_ops *ops, 1343 const struct iomap_write_ops *write_ops) 1344 { 1345 struct iomap_iter iter = { 1346 .inode = inode, 1347 .pos = pos, 1348 .flags = IOMAP_WRITE | IOMAP_UNSHARE, 1349 }; 1350 loff_t size = i_size_read(inode); 1351 int ret; 1352 1353 if (pos < 0 || pos >= size) 1354 return 0; 1355 1356 iter.len = min(len, size - pos); 1357 while ((ret = iomap_iter(&iter, ops)) > 0) 1358 iter.status = iomap_unshare_iter(&iter, write_ops); 1359 return ret; 1360 } 1361 EXPORT_SYMBOL_GPL(iomap_file_unshare); 1362 1363 /* 1364 * Flush the remaining range of the iter and mark the current mapping stale. 1365 * This is used when zero range sees an unwritten mapping that may have had 1366 * dirty pagecache over it. 1367 */ 1368 static inline int iomap_zero_iter_flush_and_stale(struct iomap_iter *i) 1369 { 1370 struct address_space *mapping = i->inode->i_mapping; 1371 loff_t end = i->pos + i->len - 1; 1372 1373 i->iomap.flags |= IOMAP_F_STALE; 1374 return filemap_write_and_wait_range(mapping, i->pos, end); 1375 } 1376 1377 static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero, 1378 const struct iomap_write_ops *write_ops) 1379 { 1380 u64 bytes = iomap_length(iter); 1381 int status; 1382 1383 do { 1384 struct folio *folio; 1385 size_t offset; 1386 bool ret; 1387 1388 bytes = min_t(u64, SIZE_MAX, bytes); 1389 status = iomap_write_begin(iter, write_ops, &folio, &offset, 1390 &bytes); 1391 if (status) 1392 return status; 1393 if (iter->iomap.flags & IOMAP_F_STALE) 1394 break; 1395 1396 /* warn about zeroing folios beyond eof that won't write back */ 1397 WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size); 1398 1399 folio_zero_range(folio, offset, bytes); 1400 folio_mark_accessed(folio); 1401 1402 ret = iomap_write_end(iter, bytes, bytes, folio); 1403 __iomap_put_folio(iter, write_ops, bytes, folio); 1404 if (WARN_ON_ONCE(!ret)) 1405 return -EIO; 1406 1407 status = iomap_iter_advance(iter, &bytes); 1408 if (status) 1409 break; 1410 } while (bytes > 0); 1411 1412 if (did_zero) 1413 *did_zero = true; 1414 return status; 1415 } 1416 1417 int 1418 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, 1419 const struct iomap_ops *ops, 1420 const struct iomap_write_ops *write_ops, void *private) 1421 { 1422 struct iomap_iter iter = { 1423 .inode = inode, 1424 .pos = pos, 1425 .len = len, 1426 .flags = IOMAP_ZERO, 1427 .private = private, 1428 }; 1429 struct address_space *mapping = inode->i_mapping; 1430 unsigned int blocksize = i_blocksize(inode); 1431 unsigned int off = pos & (blocksize - 1); 1432 loff_t plen = min_t(loff_t, len, blocksize - off); 1433 int ret; 1434 bool range_dirty; 1435 1436 /* 1437 * Zero range can skip mappings that are zero on disk so long as 1438 * pagecache is clean. If pagecache was dirty prior to zero range, the 1439 * mapping converts on writeback completion and so must be zeroed. 1440 * 1441 * The simplest way to deal with this across a range is to flush 1442 * pagecache and process the updated mappings. To avoid excessive 1443 * flushing on partial eof zeroing, special case it to zero the 1444 * unaligned start portion if already dirty in pagecache. 1445 */ 1446 if (off && 1447 filemap_range_needs_writeback(mapping, pos, pos + plen - 1)) { 1448 iter.len = plen; 1449 while ((ret = iomap_iter(&iter, ops)) > 0) 1450 iter.status = iomap_zero_iter(&iter, did_zero, 1451 write_ops); 1452 1453 iter.len = len - (iter.pos - pos); 1454 if (ret || !iter.len) 1455 return ret; 1456 } 1457 1458 /* 1459 * To avoid an unconditional flush, check pagecache state and only flush 1460 * if dirty and the fs returns a mapping that might convert on 1461 * writeback. 1462 */ 1463 range_dirty = filemap_range_needs_writeback(inode->i_mapping, 1464 iter.pos, iter.pos + iter.len - 1); 1465 while ((ret = iomap_iter(&iter, ops)) > 0) { 1466 const struct iomap *srcmap = iomap_iter_srcmap(&iter); 1467 1468 if (srcmap->type == IOMAP_HOLE || 1469 srcmap->type == IOMAP_UNWRITTEN) { 1470 s64 status; 1471 1472 if (range_dirty) { 1473 range_dirty = false; 1474 status = iomap_zero_iter_flush_and_stale(&iter); 1475 } else { 1476 status = iomap_iter_advance_full(&iter); 1477 } 1478 iter.status = status; 1479 continue; 1480 } 1481 1482 iter.status = iomap_zero_iter(&iter, did_zero, write_ops); 1483 } 1484 return ret; 1485 } 1486 EXPORT_SYMBOL_GPL(iomap_zero_range); 1487 1488 int 1489 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, 1490 const struct iomap_ops *ops, 1491 const struct iomap_write_ops *write_ops, void *private) 1492 { 1493 unsigned int blocksize = i_blocksize(inode); 1494 unsigned int off = pos & (blocksize - 1); 1495 1496 /* Block boundary? Nothing to do */ 1497 if (!off) 1498 return 0; 1499 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops, 1500 write_ops, private); 1501 } 1502 EXPORT_SYMBOL_GPL(iomap_truncate_page); 1503 1504 static int iomap_folio_mkwrite_iter(struct iomap_iter *iter, 1505 struct folio *folio) 1506 { 1507 loff_t length = iomap_length(iter); 1508 int ret; 1509 1510 if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) { 1511 ret = __block_write_begin_int(folio, iter->pos, length, NULL, 1512 &iter->iomap); 1513 if (ret) 1514 return ret; 1515 block_commit_write(folio, 0, length); 1516 } else { 1517 WARN_ON_ONCE(!folio_test_uptodate(folio)); 1518 folio_mark_dirty(folio); 1519 } 1520 1521 return iomap_iter_advance(iter, &length); 1522 } 1523 1524 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops, 1525 void *private) 1526 { 1527 struct iomap_iter iter = { 1528 .inode = file_inode(vmf->vma->vm_file), 1529 .flags = IOMAP_WRITE | IOMAP_FAULT, 1530 .private = private, 1531 }; 1532 struct folio *folio = page_folio(vmf->page); 1533 ssize_t ret; 1534 1535 folio_lock(folio); 1536 ret = folio_mkwrite_check_truncate(folio, iter.inode); 1537 if (ret < 0) 1538 goto out_unlock; 1539 iter.pos = folio_pos(folio); 1540 iter.len = ret; 1541 while ((ret = iomap_iter(&iter, ops)) > 0) 1542 iter.status = iomap_folio_mkwrite_iter(&iter, folio); 1543 1544 if (ret < 0) 1545 goto out_unlock; 1546 folio_wait_stable(folio); 1547 return VM_FAULT_LOCKED; 1548 out_unlock: 1549 folio_unlock(folio); 1550 return vmf_fs_error(ret); 1551 } 1552 EXPORT_SYMBOL_GPL(iomap_page_mkwrite); 1553 1554 void iomap_start_folio_write(struct inode *inode, struct folio *folio, 1555 size_t len) 1556 { 1557 struct iomap_folio_state *ifs = folio->private; 1558 1559 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs); 1560 if (ifs) 1561 atomic_add(len, &ifs->write_bytes_pending); 1562 } 1563 EXPORT_SYMBOL_GPL(iomap_start_folio_write); 1564 1565 void iomap_finish_folio_write(struct inode *inode, struct folio *folio, 1566 size_t len) 1567 { 1568 struct iomap_folio_state *ifs = folio->private; 1569 1570 WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs); 1571 WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0); 1572 1573 if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending)) 1574 folio_end_writeback(folio); 1575 } 1576 EXPORT_SYMBOL_GPL(iomap_finish_folio_write); 1577 1578 static int iomap_writeback_range(struct iomap_writepage_ctx *wpc, 1579 struct folio *folio, u64 pos, u32 rlen, u64 end_pos, 1580 bool *wb_pending) 1581 { 1582 do { 1583 ssize_t ret; 1584 1585 ret = wpc->ops->writeback_range(wpc, folio, pos, rlen, end_pos); 1586 if (WARN_ON_ONCE(ret == 0 || ret > rlen)) 1587 return -EIO; 1588 if (ret < 0) 1589 return ret; 1590 rlen -= ret; 1591 pos += ret; 1592 1593 /* 1594 * Holes are not be written back by ->writeback_range, so track 1595 * if we did handle anything that is not a hole here. 1596 */ 1597 if (wpc->iomap.type != IOMAP_HOLE) 1598 *wb_pending = true; 1599 } while (rlen); 1600 1601 return 0; 1602 } 1603 1604 /* 1605 * Check interaction of the folio with the file end. 1606 * 1607 * If the folio is entirely beyond i_size, return false. If it straddles 1608 * i_size, adjust end_pos and zero all data beyond i_size. 1609 */ 1610 static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode, 1611 u64 *end_pos) 1612 { 1613 u64 isize = i_size_read(inode); 1614 1615 if (*end_pos > isize) { 1616 size_t poff = offset_in_folio(folio, isize); 1617 pgoff_t end_index = isize >> PAGE_SHIFT; 1618 1619 /* 1620 * If the folio is entirely ouside of i_size, skip it. 1621 * 1622 * This can happen due to a truncate operation that is in 1623 * progress and in that case truncate will finish it off once 1624 * we've dropped the folio lock. 1625 * 1626 * Note that the pgoff_t used for end_index is an unsigned long. 1627 * If the given offset is greater than 16TB on a 32-bit system, 1628 * then if we checked if the folio is fully outside i_size with 1629 * "if (folio->index >= end_index + 1)", "end_index + 1" would 1630 * overflow and evaluate to 0. Hence this folio would be 1631 * redirtied and written out repeatedly, which would result in 1632 * an infinite loop; the user program performing this operation 1633 * would hang. Instead, we can detect this situation by 1634 * checking if the folio is totally beyond i_size or if its 1635 * offset is just equal to the EOF. 1636 */ 1637 if (folio->index > end_index || 1638 (folio->index == end_index && poff == 0)) 1639 return false; 1640 1641 /* 1642 * The folio straddles i_size. 1643 * 1644 * It must be zeroed out on each and every writepage invocation 1645 * because it may be mmapped: 1646 * 1647 * A file is mapped in multiples of the page size. For a 1648 * file that is not a multiple of the page size, the 1649 * remaining memory is zeroed when mapped, and writes to that 1650 * region are not written out to the file. 1651 * 1652 * Also adjust the end_pos to the end of file and skip writeback 1653 * for all blocks entirely beyond i_size. 1654 */ 1655 folio_zero_segment(folio, poff, folio_size(folio)); 1656 *end_pos = isize; 1657 } 1658 1659 return true; 1660 } 1661 1662 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio) 1663 { 1664 struct iomap_folio_state *ifs = folio->private; 1665 struct inode *inode = wpc->inode; 1666 u64 pos = folio_pos(folio); 1667 u64 end_pos = pos + folio_size(folio); 1668 u64 end_aligned = 0; 1669 bool wb_pending = false; 1670 int error = 0; 1671 u32 rlen; 1672 1673 WARN_ON_ONCE(!folio_test_locked(folio)); 1674 WARN_ON_ONCE(folio_test_dirty(folio)); 1675 WARN_ON_ONCE(folio_test_writeback(folio)); 1676 1677 trace_iomap_writeback_folio(inode, pos, folio_size(folio)); 1678 1679 if (!iomap_writeback_handle_eof(folio, inode, &end_pos)) 1680 return 0; 1681 WARN_ON_ONCE(end_pos <= pos); 1682 1683 if (i_blocks_per_folio(inode, folio) > 1) { 1684 if (!ifs) { 1685 ifs = ifs_alloc(inode, folio, 0); 1686 iomap_set_range_dirty(folio, 0, end_pos - pos); 1687 } 1688 1689 /* 1690 * Keep the I/O completion handler from clearing the writeback 1691 * bit until we have submitted all blocks by adding a bias to 1692 * ifs->write_bytes_pending, which is dropped after submitting 1693 * all blocks. 1694 */ 1695 WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending) != 0); 1696 iomap_start_folio_write(inode, folio, 1); 1697 } 1698 1699 /* 1700 * Set the writeback bit ASAP, as the I/O completion for the single 1701 * block per folio case happen hit as soon as we're submitting the bio. 1702 */ 1703 folio_start_writeback(folio); 1704 1705 /* 1706 * Walk through the folio to find dirty areas to write back. 1707 */ 1708 end_aligned = round_up(end_pos, i_blocksize(inode)); 1709 while ((rlen = iomap_find_dirty_range(folio, &pos, end_aligned))) { 1710 error = iomap_writeback_range(wpc, folio, pos, rlen, end_pos, 1711 &wb_pending); 1712 if (error) 1713 break; 1714 pos += rlen; 1715 } 1716 1717 if (wb_pending) 1718 wpc->nr_folios++; 1719 1720 /* 1721 * We can have dirty bits set past end of file in page_mkwrite path 1722 * while mapping the last partial folio. Hence it's better to clear 1723 * all the dirty bits in the folio here. 1724 */ 1725 iomap_clear_range_dirty(folio, 0, folio_size(folio)); 1726 1727 /* 1728 * Usually the writeback bit is cleared by the I/O completion handler. 1729 * But we may end up either not actually writing any blocks, or (when 1730 * there are multiple blocks in a folio) all I/O might have finished 1731 * already at this point. In that case we need to clear the writeback 1732 * bit ourselves right after unlocking the page. 1733 */ 1734 if (ifs) { 1735 if (atomic_dec_and_test(&ifs->write_bytes_pending)) 1736 folio_end_writeback(folio); 1737 } else { 1738 if (!wb_pending) 1739 folio_end_writeback(folio); 1740 } 1741 mapping_set_error(inode->i_mapping, error); 1742 return error; 1743 } 1744 EXPORT_SYMBOL_GPL(iomap_writeback_folio); 1745 1746 int 1747 iomap_writepages(struct iomap_writepage_ctx *wpc) 1748 { 1749 struct address_space *mapping = wpc->inode->i_mapping; 1750 struct folio *folio = NULL; 1751 int error; 1752 1753 /* 1754 * Writeback from reclaim context should never happen except in the case 1755 * of a VM regression so warn about it and refuse to write the data. 1756 */ 1757 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC | PF_KSWAPD)) == 1758 PF_MEMALLOC)) 1759 return -EIO; 1760 1761 while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) { 1762 error = iomap_writeback_folio(wpc, folio); 1763 folio_unlock(folio); 1764 } 1765 1766 /* 1767 * If @error is non-zero, it means that we have a situation where some 1768 * part of the submission process has failed after we've marked pages 1769 * for writeback. 1770 * 1771 * We cannot cancel the writeback directly in that case, so always call 1772 * ->writeback_submit to run the I/O completion handler to clear the 1773 * writeback bit and let the file system proess the errors. 1774 */ 1775 if (wpc->wb_ctx) 1776 return wpc->ops->writeback_submit(wpc, error); 1777 return error; 1778 } 1779 EXPORT_SYMBOL_GPL(iomap_writepages); 1780