1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mm/truncate.c - code for taking down pages from address_spaces 4 * 5 * Copyright (C) 2002, Linus Torvalds 6 * 7 * 10Sep2002 Andrew Morton 8 * Initial version. 9 */ 10 11 #include <linux/kernel.h> 12 #include <linux/backing-dev.h> 13 #include <linux/dax.h> 14 #include <linux/gfp.h> 15 #include <linux/mm.h> 16 #include <linux/swap.h> 17 #include <linux/export.h> 18 #include <linux/pagemap.h> 19 #include <linux/highmem.h> 20 #include <linux/pagevec.h> 21 #include <linux/task_io_accounting_ops.h> 22 #include <linux/shmem_fs.h> 23 #include <linux/rmap.h> 24 #include "internal.h" 25 26 static void clear_shadow_entries(struct address_space *mapping, 27 unsigned long start, unsigned long max) 28 { 29 XA_STATE(xas, &mapping->i_pages, start); 30 struct folio *folio; 31 32 /* Handled by shmem itself, or for DAX we do nothing. */ 33 if (shmem_mapping(mapping) || dax_mapping(mapping)) 34 return; 35 36 xas_set_update(&xas, workingset_update_node); 37 38 spin_lock(&mapping->host->i_lock); 39 xas_lock_irq(&xas); 40 41 /* Clear all shadow entries from start to max */ 42 xas_for_each(&xas, folio, max) { 43 if (xa_is_value(folio)) 44 xas_store(&xas, NULL); 45 } 46 47 xas_unlock_irq(&xas); 48 if (mapping_shrinkable(mapping)) 49 inode_add_lru(mapping->host); 50 spin_unlock(&mapping->host->i_lock); 51 } 52 53 /* 54 * Unconditionally remove exceptional entries. Usually called from truncate 55 * path. Note that the folio_batch may be altered by this function by removing 56 * exceptional entries similar to what folio_batch_remove_exceptionals() does. 57 * Please note that indices[] has entries in ascending order as guaranteed by 58 * either find_get_entries() or find_lock_entries(). 59 */ 60 static void truncate_folio_batch_exceptionals(struct address_space *mapping, 61 struct folio_batch *fbatch, pgoff_t *indices) 62 { 63 XA_STATE(xas, &mapping->i_pages, indices[0]); 64 int nr = folio_batch_count(fbatch); 65 struct folio *folio; 66 int i, j; 67 68 /* Handled by shmem itself */ 69 if (shmem_mapping(mapping)) 70 return; 71 72 for (j = 0; j < nr; j++) 73 if (xa_is_value(fbatch->folios[j])) 74 break; 75 76 if (j == nr) 77 return; 78 79 if (dax_mapping(mapping)) { 80 for (i = j; i < nr; i++) { 81 if (xa_is_value(fbatch->folios[i])) { 82 /* 83 * File systems should already have called 84 * dax_break_layout_entry() to remove all DAX 85 * entries while holding a lock to prevent 86 * establishing new entries. Therefore we 87 * shouldn't find any here. 88 */ 89 WARN_ON_ONCE(1); 90 91 /* 92 * Delete the mapping so truncate_pagecache() 93 * doesn't loop forever. 94 */ 95 dax_delete_mapping_entry(mapping, indices[i]); 96 } 97 } 98 goto out; 99 } 100 101 xas_set(&xas, indices[j]); 102 xas_set_update(&xas, workingset_update_node); 103 104 spin_lock(&mapping->host->i_lock); 105 xas_lock_irq(&xas); 106 107 xas_for_each(&xas, folio, indices[nr-1]) { 108 if (xa_is_value(folio)) 109 xas_store(&xas, NULL); 110 } 111 112 xas_unlock_irq(&xas); 113 if (mapping_shrinkable(mapping)) 114 inode_add_lru(mapping->host); 115 spin_unlock(&mapping->host->i_lock); 116 out: 117 folio_batch_remove_exceptionals(fbatch); 118 } 119 120 /** 121 * folio_invalidate - Invalidate part or all of a folio. 122 * @folio: The folio which is affected. 123 * @offset: start of the range to invalidate 124 * @length: length of the range to invalidate 125 * 126 * folio_invalidate() is called when all or part of the folio has become 127 * invalidated by a truncate operation. 128 * 129 * folio_invalidate() does not have to release all buffers, but it must 130 * ensure that no dirty buffer is left outside @offset and that no I/O 131 * is underway against any of the blocks which are outside the truncation 132 * point. Because the caller is about to free (and possibly reuse) those 133 * blocks on-disk. 134 */ 135 void folio_invalidate(struct folio *folio, size_t offset, size_t length) 136 { 137 const struct address_space_operations *aops = folio->mapping->a_ops; 138 139 if (aops->invalidate_folio) 140 aops->invalidate_folio(folio, offset, length); 141 } 142 EXPORT_SYMBOL_GPL(folio_invalidate); 143 144 /* 145 * If truncate cannot remove the fs-private metadata from the page, the page 146 * becomes orphaned. It will be left on the LRU and may even be mapped into 147 * user pagetables if we're racing with filemap_fault(). 148 * 149 * We need to bail out if page->mapping is no longer equal to the original 150 * mapping. This happens a) when the VM reclaimed the page while we waited on 151 * its lock, b) when a concurrent invalidate_mapping_pages got there first and 152 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 153 */ 154 static void truncate_cleanup_folio(struct folio *folio) 155 { 156 if (folio_mapped(folio)) 157 unmap_mapping_folio(folio); 158 159 if (folio_needs_release(folio)) 160 folio_invalidate(folio, 0, folio_size(folio)); 161 162 /* 163 * Some filesystems seem to re-dirty the page even after 164 * the VM has canceled the dirty bit (eg ext3 journaling). 165 * Hence dirty accounting check is placed after invalidation. 166 */ 167 folio_cancel_dirty(folio); 168 } 169 170 int truncate_inode_folio(struct address_space *mapping, struct folio *folio) 171 { 172 if (folio->mapping != mapping) 173 return -EIO; 174 175 truncate_cleanup_folio(folio); 176 filemap_remove_folio(folio); 177 return 0; 178 } 179 180 /* 181 * Handle partial folios. The folio may be entirely within the 182 * range if a split has raced with us. If not, we zero the part of the 183 * folio that's within the [start, end] range, and then split the folio if 184 * it's large. split_page_range() will discard pages which now lie beyond 185 * i_size, and we rely on the caller to discard pages which lie within a 186 * newly created hole. 187 * 188 * Returns false if splitting failed so the caller can avoid 189 * discarding the entire folio which is stubbornly unsplit. 190 */ 191 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) 192 { 193 loff_t pos = folio_pos(folio); 194 unsigned int offset, length; 195 struct page *split_at, *split_at2; 196 197 if (pos < start) 198 offset = start - pos; 199 else 200 offset = 0; 201 length = folio_size(folio); 202 if (pos + length <= (u64)end) 203 length = length - offset; 204 else 205 length = end + 1 - pos - offset; 206 207 folio_wait_writeback(folio); 208 if (length == folio_size(folio)) { 209 truncate_inode_folio(folio->mapping, folio); 210 return true; 211 } 212 213 /* 214 * We may be zeroing pages we're about to discard, but it avoids 215 * doing a complex calculation here, and then doing the zeroing 216 * anyway if the page split fails. 217 */ 218 if (!mapping_inaccessible(folio->mapping)) 219 folio_zero_range(folio, offset, length); 220 221 if (folio_needs_release(folio)) 222 folio_invalidate(folio, offset, length); 223 if (!folio_test_large(folio)) 224 return true; 225 226 split_at = folio_page(folio, PAGE_ALIGN_DOWN(offset) / PAGE_SIZE); 227 split_at2 = folio_page(folio, 228 PAGE_ALIGN_DOWN(offset + length) / PAGE_SIZE); 229 230 if (!try_folio_split(folio, split_at, NULL)) { 231 /* 232 * try to split at offset + length to make sure folios within 233 * the range can be dropped, especially to avoid memory waste 234 * for shmem truncate 235 */ 236 struct folio *folio2 = page_folio(split_at2); 237 238 if (!folio_try_get(folio2)) 239 goto no_split; 240 241 if (!folio_test_large(folio2)) 242 goto out; 243 244 if (!folio_trylock(folio2)) 245 goto out; 246 247 /* 248 * make sure folio2 is large and does not change its mapping. 249 * Its split result does not matter here. 250 */ 251 if (folio_test_large(folio2) && 252 folio2->mapping == folio->mapping) 253 try_folio_split(folio2, split_at2, NULL); 254 255 folio_unlock(folio2); 256 out: 257 folio_put(folio2); 258 no_split: 259 return true; 260 } 261 if (folio_test_dirty(folio)) 262 return false; 263 truncate_inode_folio(folio->mapping, folio); 264 return true; 265 } 266 267 /* 268 * Used to get rid of pages on hardware memory corruption. 269 */ 270 int generic_error_remove_folio(struct address_space *mapping, 271 struct folio *folio) 272 { 273 if (!mapping) 274 return -EINVAL; 275 /* 276 * Only punch for normal data pages for now. 277 * Handling other types like directories would need more auditing. 278 */ 279 if (!S_ISREG(mapping->host->i_mode)) 280 return -EIO; 281 return truncate_inode_folio(mapping, folio); 282 } 283 EXPORT_SYMBOL(generic_error_remove_folio); 284 285 /** 286 * mapping_evict_folio() - Remove an unused folio from the page-cache. 287 * @mapping: The mapping this folio belongs to. 288 * @folio: The folio to remove. 289 * 290 * Safely remove one folio from the page cache. 291 * It only drops clean, unused folios. 292 * 293 * Context: Folio must be locked. 294 * Return: The number of pages successfully removed. 295 */ 296 long mapping_evict_folio(struct address_space *mapping, struct folio *folio) 297 { 298 /* The page may have been truncated before it was locked */ 299 if (!mapping) 300 return 0; 301 if (folio_test_dirty(folio) || folio_test_writeback(folio)) 302 return 0; 303 /* The refcount will be elevated if any page in the folio is mapped */ 304 if (folio_ref_count(folio) > 305 folio_nr_pages(folio) + folio_has_private(folio) + 1) 306 return 0; 307 if (!filemap_release_folio(folio, 0)) 308 return 0; 309 310 return remove_mapping(mapping, folio); 311 } 312 313 /** 314 * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 315 * @mapping: mapping to truncate 316 * @lstart: offset from which to truncate 317 * @lend: offset to which to truncate (inclusive) 318 * 319 * Truncate the page cache, removing the pages that are between 320 * specified offsets (and zeroing out partial pages 321 * if lstart or lend + 1 is not page aligned). 322 * 323 * Truncate takes two passes - the first pass is nonblocking. It will not 324 * block on page locks and it will not block on writeback. The second pass 325 * will wait. This is to prevent as much IO as possible in the affected region. 326 * The first pass will remove most pages, so the search cost of the second pass 327 * is low. 328 * 329 * We pass down the cache-hot hint to the page freeing code. Even if the 330 * mapping is large, it is probably the case that the final pages are the most 331 * recently touched, and freeing happens in ascending file offset order. 332 * 333 * Note that since ->invalidate_folio() accepts range to invalidate 334 * truncate_inode_pages_range is able to handle cases where lend + 1 is not 335 * page aligned properly. 336 */ 337 void truncate_inode_pages_range(struct address_space *mapping, 338 loff_t lstart, loff_t lend) 339 { 340 pgoff_t start; /* inclusive */ 341 pgoff_t end; /* exclusive */ 342 struct folio_batch fbatch; 343 pgoff_t indices[PAGEVEC_SIZE]; 344 pgoff_t index; 345 int i; 346 struct folio *folio; 347 bool same_folio; 348 349 if (mapping_empty(mapping)) 350 return; 351 352 /* 353 * 'start' and 'end' always covers the range of pages to be fully 354 * truncated. Partial pages are covered with 'partial_start' at the 355 * start of the range and 'partial_end' at the end of the range. 356 * Note that 'end' is exclusive while 'lend' is inclusive. 357 */ 358 start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 359 if (lend == -1) 360 /* 361 * lend == -1 indicates end-of-file so we have to set 'end' 362 * to the highest possible pgoff_t and since the type is 363 * unsigned we're using -1. 364 */ 365 end = -1; 366 else 367 end = (lend + 1) >> PAGE_SHIFT; 368 369 folio_batch_init(&fbatch); 370 index = start; 371 while (index < end && find_lock_entries(mapping, &index, end - 1, 372 &fbatch, indices)) { 373 truncate_folio_batch_exceptionals(mapping, &fbatch, indices); 374 for (i = 0; i < folio_batch_count(&fbatch); i++) 375 truncate_cleanup_folio(fbatch.folios[i]); 376 delete_from_page_cache_batch(mapping, &fbatch); 377 for (i = 0; i < folio_batch_count(&fbatch); i++) 378 folio_unlock(fbatch.folios[i]); 379 folio_batch_release(&fbatch); 380 cond_resched(); 381 } 382 383 same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 384 folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0); 385 if (!IS_ERR(folio)) { 386 same_folio = lend < folio_pos(folio) + folio_size(folio); 387 if (!truncate_inode_partial_folio(folio, lstart, lend)) { 388 start = folio_next_index(folio); 389 if (same_folio) 390 end = folio->index; 391 } 392 folio_unlock(folio); 393 folio_put(folio); 394 folio = NULL; 395 } 396 397 if (!same_folio) { 398 folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT, 399 FGP_LOCK, 0); 400 if (!IS_ERR(folio)) { 401 if (!truncate_inode_partial_folio(folio, lstart, lend)) 402 end = folio->index; 403 folio_unlock(folio); 404 folio_put(folio); 405 } 406 } 407 408 index = start; 409 while (index < end) { 410 cond_resched(); 411 if (!find_get_entries(mapping, &index, end - 1, &fbatch, 412 indices)) { 413 /* If all gone from start onwards, we're done */ 414 if (index == start) 415 break; 416 /* Otherwise restart to make sure all gone */ 417 index = start; 418 continue; 419 } 420 421 for (i = 0; i < folio_batch_count(&fbatch); i++) { 422 struct folio *folio = fbatch.folios[i]; 423 424 /* We rely upon deletion not changing page->index */ 425 426 if (xa_is_value(folio)) 427 continue; 428 429 folio_lock(folio); 430 VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio); 431 folio_wait_writeback(folio); 432 truncate_inode_folio(mapping, folio); 433 folio_unlock(folio); 434 } 435 truncate_folio_batch_exceptionals(mapping, &fbatch, indices); 436 folio_batch_release(&fbatch); 437 } 438 } 439 EXPORT_SYMBOL(truncate_inode_pages_range); 440 441 /** 442 * truncate_inode_pages - truncate *all* the pages from an offset 443 * @mapping: mapping to truncate 444 * @lstart: offset from which to truncate 445 * 446 * Called under (and serialised by) inode->i_rwsem and 447 * mapping->invalidate_lock. 448 * 449 * Note: When this function returns, there can be a page in the process of 450 * deletion (inside __filemap_remove_folio()) in the specified range. Thus 451 * mapping->nrpages can be non-zero when this function returns even after 452 * truncation of the whole mapping. 453 */ 454 void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 455 { 456 truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 457 } 458 EXPORT_SYMBOL(truncate_inode_pages); 459 460 /** 461 * truncate_inode_pages_final - truncate *all* pages before inode dies 462 * @mapping: mapping to truncate 463 * 464 * Called under (and serialized by) inode->i_rwsem. 465 * 466 * Filesystems have to use this in the .evict_inode path to inform the 467 * VM that this is the final truncate and the inode is going away. 468 */ 469 void truncate_inode_pages_final(struct address_space *mapping) 470 { 471 /* 472 * Page reclaim can not participate in regular inode lifetime 473 * management (can't call iput()) and thus can race with the 474 * inode teardown. Tell it when the address space is exiting, 475 * so that it does not install eviction information after the 476 * final truncate has begun. 477 */ 478 mapping_set_exiting(mapping); 479 480 if (!mapping_empty(mapping)) { 481 /* 482 * As truncation uses a lockless tree lookup, cycle 483 * the tree lock to make sure any ongoing tree 484 * modification that does not see AS_EXITING is 485 * completed before starting the final truncate. 486 */ 487 xa_lock_irq(&mapping->i_pages); 488 xa_unlock_irq(&mapping->i_pages); 489 } 490 491 truncate_inode_pages(mapping, 0); 492 } 493 EXPORT_SYMBOL(truncate_inode_pages_final); 494 495 /** 496 * mapping_try_invalidate - Invalidate all the evictable folios of one inode 497 * @mapping: the address_space which holds the folios to invalidate 498 * @start: the offset 'from' which to invalidate 499 * @end: the offset 'to' which to invalidate (inclusive) 500 * @nr_failed: How many folio invalidations failed 501 * 502 * This function is similar to invalidate_mapping_pages(), except that it 503 * returns the number of folios which could not be evicted in @nr_failed. 504 */ 505 unsigned long mapping_try_invalidate(struct address_space *mapping, 506 pgoff_t start, pgoff_t end, unsigned long *nr_failed) 507 { 508 pgoff_t indices[PAGEVEC_SIZE]; 509 struct folio_batch fbatch; 510 pgoff_t index = start; 511 unsigned long ret; 512 unsigned long count = 0; 513 int i; 514 515 folio_batch_init(&fbatch); 516 while (find_lock_entries(mapping, &index, end, &fbatch, indices)) { 517 bool xa_has_values = false; 518 int nr = folio_batch_count(&fbatch); 519 520 for (i = 0; i < nr; i++) { 521 struct folio *folio = fbatch.folios[i]; 522 523 /* We rely upon deletion not changing folio->index */ 524 525 if (xa_is_value(folio)) { 526 xa_has_values = true; 527 count++; 528 continue; 529 } 530 531 ret = mapping_evict_folio(mapping, folio); 532 folio_unlock(folio); 533 /* 534 * Invalidation is a hint that the folio is no longer 535 * of interest and try to speed up its reclaim. 536 */ 537 if (!ret) { 538 deactivate_file_folio(folio); 539 /* Likely in the lru cache of a remote CPU */ 540 if (nr_failed) 541 (*nr_failed)++; 542 } 543 count += ret; 544 } 545 546 if (xa_has_values) 547 clear_shadow_entries(mapping, indices[0], indices[nr-1]); 548 549 folio_batch_remove_exceptionals(&fbatch); 550 folio_batch_release(&fbatch); 551 cond_resched(); 552 } 553 return count; 554 } 555 556 /** 557 * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode 558 * @mapping: the address_space which holds the cache to invalidate 559 * @start: the offset 'from' which to invalidate 560 * @end: the offset 'to' which to invalidate (inclusive) 561 * 562 * This function removes pages that are clean, unmapped and unlocked, 563 * as well as shadow entries. It will not block on IO activity. 564 * 565 * If you want to remove all the pages of one inode, regardless of 566 * their use and writeback state, use truncate_inode_pages(). 567 * 568 * Return: The number of indices that had their contents invalidated 569 */ 570 unsigned long invalidate_mapping_pages(struct address_space *mapping, 571 pgoff_t start, pgoff_t end) 572 { 573 return mapping_try_invalidate(mapping, start, end, NULL); 574 } 575 EXPORT_SYMBOL(invalidate_mapping_pages); 576 577 static int folio_launder(struct address_space *mapping, struct folio *folio) 578 { 579 if (!folio_test_dirty(folio)) 580 return 0; 581 if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL) 582 return 0; 583 return mapping->a_ops->launder_folio(folio); 584 } 585 586 /* 587 * This is like mapping_evict_folio(), except it ignores the folio's 588 * refcount. We do this because invalidate_inode_pages2() needs stronger 589 * invalidation guarantees, and cannot afford to leave folios behind because 590 * shrink_folio_list() has a temp ref on them, or because they're transiently 591 * sitting in the folio_add_lru() caches. 592 */ 593 int folio_unmap_invalidate(struct address_space *mapping, struct folio *folio, 594 gfp_t gfp) 595 { 596 int ret; 597 598 VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); 599 600 if (folio_mapped(folio)) 601 unmap_mapping_folio(folio); 602 BUG_ON(folio_mapped(folio)); 603 604 ret = folio_launder(mapping, folio); 605 if (ret) 606 return ret; 607 if (folio->mapping != mapping) 608 return -EBUSY; 609 if (!filemap_release_folio(folio, gfp)) 610 return -EBUSY; 611 612 spin_lock(&mapping->host->i_lock); 613 xa_lock_irq(&mapping->i_pages); 614 if (folio_test_dirty(folio)) 615 goto failed; 616 617 BUG_ON(folio_has_private(folio)); 618 __filemap_remove_folio(folio, NULL); 619 xa_unlock_irq(&mapping->i_pages); 620 if (mapping_shrinkable(mapping)) 621 inode_add_lru(mapping->host); 622 spin_unlock(&mapping->host->i_lock); 623 624 filemap_free_folio(mapping, folio); 625 return 1; 626 failed: 627 xa_unlock_irq(&mapping->i_pages); 628 spin_unlock(&mapping->host->i_lock); 629 return -EBUSY; 630 } 631 632 /** 633 * invalidate_inode_pages2_range - remove range of pages from an address_space 634 * @mapping: the address_space 635 * @start: the page offset 'from' which to invalidate 636 * @end: the page offset 'to' which to invalidate (inclusive) 637 * 638 * Any pages which are found to be mapped into pagetables are unmapped prior to 639 * invalidation. 640 * 641 * Return: -EBUSY if any pages could not be invalidated. 642 */ 643 int invalidate_inode_pages2_range(struct address_space *mapping, 644 pgoff_t start, pgoff_t end) 645 { 646 pgoff_t indices[PAGEVEC_SIZE]; 647 struct folio_batch fbatch; 648 pgoff_t index; 649 int i; 650 int ret = 0; 651 int ret2 = 0; 652 int did_range_unmap = 0; 653 654 if (mapping_empty(mapping)) 655 return 0; 656 657 folio_batch_init(&fbatch); 658 index = start; 659 while (find_get_entries(mapping, &index, end, &fbatch, indices)) { 660 bool xa_has_values = false; 661 int nr = folio_batch_count(&fbatch); 662 663 for (i = 0; i < nr; i++) { 664 struct folio *folio = fbatch.folios[i]; 665 666 /* We rely upon deletion not changing folio->index */ 667 668 if (xa_is_value(folio)) { 669 xa_has_values = true; 670 if (dax_mapping(mapping) && 671 !dax_invalidate_mapping_entry_sync(mapping, indices[i])) 672 ret = -EBUSY; 673 continue; 674 } 675 676 if (!did_range_unmap && folio_mapped(folio)) { 677 /* 678 * If folio is mapped, before taking its lock, 679 * zap the rest of the file in one hit. 680 */ 681 unmap_mapping_pages(mapping, indices[i], 682 (1 + end - indices[i]), false); 683 did_range_unmap = 1; 684 } 685 686 folio_lock(folio); 687 if (unlikely(folio->mapping != mapping)) { 688 folio_unlock(folio); 689 continue; 690 } 691 VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio); 692 folio_wait_writeback(folio); 693 ret2 = folio_unmap_invalidate(mapping, folio, GFP_KERNEL); 694 if (ret2 < 0) 695 ret = ret2; 696 folio_unlock(folio); 697 } 698 699 if (xa_has_values) 700 clear_shadow_entries(mapping, indices[0], indices[nr-1]); 701 702 folio_batch_remove_exceptionals(&fbatch); 703 folio_batch_release(&fbatch); 704 cond_resched(); 705 } 706 /* 707 * For DAX we invalidate page tables after invalidating page cache. We 708 * could invalidate page tables while invalidating each entry however 709 * that would be expensive. And doing range unmapping before doesn't 710 * work as we have no cheap way to find whether page cache entry didn't 711 * get remapped later. 712 */ 713 if (dax_mapping(mapping)) { 714 unmap_mapping_pages(mapping, start, end - start + 1, false); 715 } 716 return ret; 717 } 718 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 719 720 /** 721 * invalidate_inode_pages2 - remove all pages from an address_space 722 * @mapping: the address_space 723 * 724 * Any pages which are found to be mapped into pagetables are unmapped prior to 725 * invalidation. 726 * 727 * Return: -EBUSY if any pages could not be invalidated. 728 */ 729 int invalidate_inode_pages2(struct address_space *mapping) 730 { 731 return invalidate_inode_pages2_range(mapping, 0, -1); 732 } 733 EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 734 735 /** 736 * truncate_pagecache - unmap and remove pagecache that has been truncated 737 * @inode: inode 738 * @newsize: new file size 739 * 740 * inode's new i_size must already be written before truncate_pagecache 741 * is called. 742 * 743 * This function should typically be called before the filesystem 744 * releases resources associated with the freed range (eg. deallocates 745 * blocks). This way, pagecache will always stay logically coherent 746 * with on-disk format, and the filesystem would not have to deal with 747 * situations such as writepage being called for a page that has already 748 * had its underlying blocks deallocated. 749 */ 750 void truncate_pagecache(struct inode *inode, loff_t newsize) 751 { 752 struct address_space *mapping = inode->i_mapping; 753 loff_t holebegin = round_up(newsize, PAGE_SIZE); 754 755 /* 756 * unmap_mapping_range is called twice, first simply for 757 * efficiency so that truncate_inode_pages does fewer 758 * single-page unmaps. However after this first call, and 759 * before truncate_inode_pages finishes, it is possible for 760 * private pages to be COWed, which remain after 761 * truncate_inode_pages finishes, hence the second 762 * unmap_mapping_range call must be made for correctness. 763 */ 764 unmap_mapping_range(mapping, holebegin, 0, 1); 765 truncate_inode_pages(mapping, newsize); 766 unmap_mapping_range(mapping, holebegin, 0, 1); 767 } 768 EXPORT_SYMBOL(truncate_pagecache); 769 770 /** 771 * truncate_setsize - update inode and pagecache for a new file size 772 * @inode: inode 773 * @newsize: new file size 774 * 775 * truncate_setsize updates i_size and performs pagecache truncation (if 776 * necessary) to @newsize. It will be typically be called from the filesystem's 777 * setattr function when ATTR_SIZE is passed in. 778 * 779 * Must be called with a lock serializing truncates and writes (generally 780 * i_rwsem but e.g. xfs uses a different lock) and before all filesystem 781 * specific block truncation has been performed. 782 */ 783 void truncate_setsize(struct inode *inode, loff_t newsize) 784 { 785 loff_t oldsize = inode->i_size; 786 787 i_size_write(inode, newsize); 788 if (newsize > oldsize) 789 pagecache_isize_extended(inode, oldsize, newsize); 790 truncate_pagecache(inode, newsize); 791 } 792 EXPORT_SYMBOL(truncate_setsize); 793 794 /** 795 * pagecache_isize_extended - update pagecache after extension of i_size 796 * @inode: inode for which i_size was extended 797 * @from: original inode size 798 * @to: new inode size 799 * 800 * Handle extension of inode size either caused by extending truncate or 801 * by write starting after current i_size. We mark the page straddling 802 * current i_size RO so that page_mkwrite() is called on the first 803 * write access to the page. The filesystem will update its per-block 804 * information before user writes to the page via mmap after the i_size 805 * has been changed. 806 * 807 * The function must be called after i_size is updated so that page fault 808 * coming after we unlock the folio will already see the new i_size. 809 * The function must be called while we still hold i_rwsem - this not only 810 * makes sure i_size is stable but also that userspace cannot observe new 811 * i_size value before we are prepared to store mmap writes at new inode size. 812 */ 813 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 814 { 815 int bsize = i_blocksize(inode); 816 loff_t rounded_from; 817 struct folio *folio; 818 819 WARN_ON(to > inode->i_size); 820 821 if (from >= to || bsize >= PAGE_SIZE) 822 return; 823 /* Page straddling @from will not have any hole block created? */ 824 rounded_from = round_up(from, bsize); 825 if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 826 return; 827 828 folio = filemap_lock_folio(inode->i_mapping, from / PAGE_SIZE); 829 /* Folio not cached? Nothing to do */ 830 if (IS_ERR(folio)) 831 return; 832 /* 833 * See folio_clear_dirty_for_io() for details why folio_mark_dirty() 834 * is needed. 835 */ 836 if (folio_mkclean(folio)) 837 folio_mark_dirty(folio); 838 839 /* 840 * The post-eof range of the folio must be zeroed before it is exposed 841 * to the file. Writeback normally does this, but since i_size has been 842 * increased we handle it here. 843 */ 844 if (folio_test_dirty(folio)) { 845 unsigned int offset, end; 846 847 offset = from - folio_pos(folio); 848 end = min_t(unsigned int, to - folio_pos(folio), 849 folio_size(folio)); 850 folio_zero_segment(folio, offset, end); 851 } 852 853 folio_unlock(folio); 854 folio_put(folio); 855 } 856 EXPORT_SYMBOL(pagecache_isize_extended); 857 858 /** 859 * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 860 * @inode: inode 861 * @lstart: offset of beginning of hole 862 * @lend: offset of last byte of hole 863 * 864 * This function should typically be called before the filesystem 865 * releases resources associated with the freed range (eg. deallocates 866 * blocks). This way, pagecache will always stay logically coherent 867 * with on-disk format, and the filesystem would not have to deal with 868 * situations such as writepage being called for a page that has already 869 * had its underlying blocks deallocated. 870 */ 871 void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 872 { 873 struct address_space *mapping = inode->i_mapping; 874 loff_t unmap_start = round_up(lstart, PAGE_SIZE); 875 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 876 /* 877 * This rounding is currently just for example: unmap_mapping_range 878 * expands its hole outwards, whereas we want it to contract the hole 879 * inwards. However, existing callers of truncate_pagecache_range are 880 * doing their own page rounding first. Note that unmap_mapping_range 881 * allows holelen 0 for all, and we allow lend -1 for end of file. 882 */ 883 884 /* 885 * Unlike in truncate_pagecache, unmap_mapping_range is called only 886 * once (before truncating pagecache), and without "even_cows" flag: 887 * hole-punching should not remove private COWed pages from the hole. 888 */ 889 if ((u64)unmap_end > (u64)unmap_start) 890 unmap_mapping_range(mapping, unmap_start, 891 1 + unmap_end - unmap_start, 0); 892 truncate_inode_pages_range(mapping, lstart, lend); 893 } 894 EXPORT_SYMBOL(truncate_pagecache_range); 895