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