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