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