1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * mm/truncate.c - code for taking down pages from address_spaces 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds 61da177e4SLinus Torvalds * 7e1f8e874SFrancois Cami * 10Sep2002 Andrew Morton 81da177e4SLinus Torvalds * Initial version. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/kernel.h> 124af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 13f9fe48beSRoss Zwisler #include <linux/dax.h> 145a0e3ad6STejun Heo #include <linux/gfp.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 160fd0e6b0SNick Piggin #include <linux/swap.h> 17b95f1b31SPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 1901f2705dSNate Diller #include <linux/highmem.h> 201da177e4SLinus Torvalds #include <linux/pagevec.h> 21e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h> 221da177e4SLinus Torvalds #include <linux/buffer_head.h> /* grr. try_to_release_page, 23aaa4059bSJan Kara do_invalidatepage */ 243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h> 25c515e1fdSDan Magenheimer #include <linux/cleancache.h> 2690a80202SJan Kara #include <linux/rmap.h> 27ba470de4SRik van Riel #include "internal.h" 281da177e4SLinus Torvalds 29f2187599SMel Gorman /* 30f2187599SMel Gorman * Regular page slots are stabilized by the page lock even without the tree 31f2187599SMel Gorman * itself locked. These unlocked entries need verification under the tree 32f2187599SMel Gorman * lock. 33f2187599SMel Gorman */ 34f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping, 35f2187599SMel Gorman pgoff_t index, void *entry) 360cd6144aSJohannes Weiner { 3769b6c131SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 38449dd698SJohannes Weiner 3969b6c131SMatthew Wilcox xas_set_update(&xas, workingset_update_node); 4069b6c131SMatthew Wilcox if (xas_load(&xas) != entry) 41f2187599SMel Gorman return; 4269b6c131SMatthew Wilcox xas_store(&xas, NULL); 43f2187599SMel Gorman } 44f2187599SMel Gorman 45f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index, 46f2187599SMel Gorman void *entry) 47f2187599SMel Gorman { 48b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 49f2187599SMel Gorman __clear_shadow_entry(mapping, index, entry); 50b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 510cd6144aSJohannes Weiner } 521da177e4SLinus Torvalds 53c6dcf52cSJan Kara /* 54f2187599SMel Gorman * Unconditionally remove exceptional entries. Usually called from truncate 55f2187599SMel Gorman * path. Note that the pagevec may be altered by this function by removing 56f2187599SMel Gorman * exceptional entries similar to what pagevec_remove_exceptionals does. 57c6dcf52cSJan Kara */ 58f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping, 5931d270fdSMatthew Wilcox (Oracle) struct pagevec *pvec, pgoff_t *indices) 60c6dcf52cSJan Kara { 61f2187599SMel Gorman int i, j; 6231d270fdSMatthew Wilcox (Oracle) bool dax; 63f2187599SMel Gorman 64c6dcf52cSJan Kara /* Handled by shmem itself */ 65c6dcf52cSJan Kara if (shmem_mapping(mapping)) 66c6dcf52cSJan Kara return; 67c6dcf52cSJan Kara 68f2187599SMel Gorman for (j = 0; j < pagevec_count(pvec); j++) 693159f943SMatthew Wilcox if (xa_is_value(pvec->pages[j])) 70f2187599SMel Gorman break; 71f2187599SMel Gorman 72f2187599SMel Gorman if (j == pagevec_count(pvec)) 73c6dcf52cSJan Kara return; 74f2187599SMel Gorman 75f2187599SMel Gorman dax = dax_mapping(mapping); 7631d270fdSMatthew Wilcox (Oracle) if (!dax) 77b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 78f2187599SMel Gorman 79f2187599SMel Gorman for (i = j; i < pagevec_count(pvec); i++) { 80f2187599SMel Gorman struct page *page = pvec->pages[i]; 81f2187599SMel Gorman pgoff_t index = indices[i]; 82f2187599SMel Gorman 833159f943SMatthew Wilcox if (!xa_is_value(page)) { 84f2187599SMel Gorman pvec->pages[j++] = page; 85f2187599SMel Gorman continue; 86c6dcf52cSJan Kara } 87f2187599SMel Gorman 88f2187599SMel Gorman if (unlikely(dax)) { 89f2187599SMel Gorman dax_delete_mapping_entry(mapping, index); 90f2187599SMel Gorman continue; 91f2187599SMel Gorman } 92f2187599SMel Gorman 93f2187599SMel Gorman __clear_shadow_entry(mapping, index, page); 94f2187599SMel Gorman } 95f2187599SMel Gorman 9631d270fdSMatthew Wilcox (Oracle) if (!dax) 97b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 98f2187599SMel Gorman pvec->nr = j; 99c6dcf52cSJan Kara } 100c6dcf52cSJan Kara 101c6dcf52cSJan Kara /* 102c6dcf52cSJan Kara * Invalidate exceptional entry if easily possible. This handles exceptional 1034636e70bSRoss Zwisler * entries for invalidate_inode_pages(). 104c6dcf52cSJan Kara */ 105c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping, 106c6dcf52cSJan Kara pgoff_t index, void *entry) 107c6dcf52cSJan Kara { 1084636e70bSRoss Zwisler /* Handled by shmem itself, or for DAX we do nothing. */ 1094636e70bSRoss Zwisler if (shmem_mapping(mapping) || dax_mapping(mapping)) 110c6dcf52cSJan Kara return 1; 111c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 112c6dcf52cSJan Kara return 1; 113c6dcf52cSJan Kara } 114c6dcf52cSJan Kara 115c6dcf52cSJan Kara /* 116c6dcf52cSJan Kara * Invalidate exceptional entry if clean. This handles exceptional entries for 117c6dcf52cSJan Kara * invalidate_inode_pages2() so for DAX it evicts only clean entries. 118c6dcf52cSJan Kara */ 119c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping, 120c6dcf52cSJan Kara pgoff_t index, void *entry) 121c6dcf52cSJan Kara { 122c6dcf52cSJan Kara /* Handled by shmem itself */ 123c6dcf52cSJan Kara if (shmem_mapping(mapping)) 124c6dcf52cSJan Kara return 1; 125c6dcf52cSJan Kara if (dax_mapping(mapping)) 126c6dcf52cSJan Kara return dax_invalidate_mapping_entry_sync(mapping, index); 127c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 128c6dcf52cSJan Kara return 1; 129c6dcf52cSJan Kara } 130c6dcf52cSJan Kara 131cf9a2ae8SDavid Howells /** 13228bc44d7SFengguang Wu * do_invalidatepage - invalidate part or all of a page 133cf9a2ae8SDavid Howells * @page: the page which is affected 134d47992f8SLukas Czerner * @offset: start of the range to invalidate 135d47992f8SLukas Czerner * @length: length of the range to invalidate 136cf9a2ae8SDavid Howells * 137cf9a2ae8SDavid Howells * do_invalidatepage() is called when all or part of the page has become 138cf9a2ae8SDavid Howells * invalidated by a truncate operation. 139cf9a2ae8SDavid Howells * 140cf9a2ae8SDavid Howells * do_invalidatepage() does not have to release all buffers, but it must 141cf9a2ae8SDavid Howells * ensure that no dirty buffer is left outside @offset and that no I/O 142cf9a2ae8SDavid Howells * is underway against any of the blocks which are outside the truncation 143cf9a2ae8SDavid Howells * point. Because the caller is about to free (and possibly reuse) those 144cf9a2ae8SDavid Howells * blocks on-disk. 145cf9a2ae8SDavid Howells */ 146d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset, 147d47992f8SLukas Czerner unsigned int length) 148cf9a2ae8SDavid Howells { 149d47992f8SLukas Czerner void (*invalidatepage)(struct page *, unsigned int, unsigned int); 150d47992f8SLukas Czerner 151cf9a2ae8SDavid Howells invalidatepage = page->mapping->a_ops->invalidatepage; 1529361401eSDavid Howells #ifdef CONFIG_BLOCK 153cf9a2ae8SDavid Howells if (!invalidatepage) 154cf9a2ae8SDavid Howells invalidatepage = block_invalidatepage; 1559361401eSDavid Howells #endif 156cf9a2ae8SDavid Howells if (invalidatepage) 157d47992f8SLukas Czerner (*invalidatepage)(page, offset, length); 158cf9a2ae8SDavid Howells } 159cf9a2ae8SDavid Howells 160ecdfc978SLinus Torvalds /* 1611da177e4SLinus Torvalds * If truncate cannot remove the fs-private metadata from the page, the page 16262e1c553SShaohua Li * becomes orphaned. It will be left on the LRU and may even be mapped into 16354cb8821SNick Piggin * user pagetables if we're racing with filemap_fault(). 1641da177e4SLinus Torvalds * 165fc3a5ac5SMatthew Wilcox (Oracle) * We need to bail out if page->mapping is no longer equal to the original 1661da177e4SLinus Torvalds * mapping. This happens a) when the VM reclaimed the page while we waited on 167fc0ecff6SAndrew Morton * its lock, b) when a concurrent invalidate_mapping_pages got there first and 1681da177e4SLinus Torvalds * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 1691da177e4SLinus Torvalds */ 17022061a1fSHugh Dickins static void truncate_cleanup_page(struct page *page) 1711da177e4SLinus Torvalds { 17222061a1fSHugh Dickins if (page_mapped(page)) 17322061a1fSHugh Dickins unmap_mapping_page(page); 1741da177e4SLinus Torvalds 175266cf658SDavid Howells if (page_has_private(page)) 176fc3a5ac5SMatthew Wilcox (Oracle) do_invalidatepage(page, 0, thp_size(page)); 1771da177e4SLinus Torvalds 178b9ea2515SKonstantin Khlebnikov /* 179b9ea2515SKonstantin Khlebnikov * Some filesystems seem to re-dirty the page even after 180b9ea2515SKonstantin Khlebnikov * the VM has canceled the dirty bit (eg ext3 journaling). 181b9ea2515SKonstantin Khlebnikov * Hence dirty accounting check is placed after invalidation. 182b9ea2515SKonstantin Khlebnikov */ 18311f81becSTejun Heo cancel_dirty_page(page); 1841da177e4SLinus Torvalds ClearPageMappedToDisk(page); 1851da177e4SLinus Torvalds } 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds /* 188fc0ecff6SAndrew Morton * This is for invalidate_mapping_pages(). That function can be called at 1891da177e4SLinus Torvalds * any time, and is not supposed to throw away dirty pages. But pages can 1900fd0e6b0SNick Piggin * be marked dirty at any time too, so use remove_mapping which safely 1910fd0e6b0SNick Piggin * discards clean, unused pages. 1921da177e4SLinus Torvalds * 1931da177e4SLinus Torvalds * Returns non-zero if the page was successfully invalidated. 1941da177e4SLinus Torvalds */ 1951da177e4SLinus Torvalds static int 1961da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page) 1971da177e4SLinus Torvalds { 1980fd0e6b0SNick Piggin int ret; 1990fd0e6b0SNick Piggin 2001da177e4SLinus Torvalds if (page->mapping != mapping) 2011da177e4SLinus Torvalds return 0; 2021da177e4SLinus Torvalds 203266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, 0)) 2041da177e4SLinus Torvalds return 0; 2051da177e4SLinus Torvalds 2060fd0e6b0SNick Piggin ret = remove_mapping(mapping, page); 2070fd0e6b0SNick Piggin 2080fd0e6b0SNick Piggin return ret; 2091da177e4SLinus Torvalds } 2101da177e4SLinus Torvalds 211750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page) 212750b4987SNick Piggin { 213fc127da0SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 214fc127da0SKirill A. Shutemov 2159f4e41f4SJan Kara if (page->mapping != mapping) 2169f4e41f4SJan Kara return -EIO; 2179f4e41f4SJan Kara 21822061a1fSHugh Dickins truncate_cleanup_page(page); 2199f4e41f4SJan Kara delete_from_page_cache(page); 2209f4e41f4SJan Kara return 0; 221750b4987SNick Piggin } 222750b4987SNick Piggin 22383f78668SWu Fengguang /* 22425718736SAndi Kleen * Used to get rid of pages on hardware memory corruption. 22525718736SAndi Kleen */ 22625718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page) 22725718736SAndi Kleen { 22825718736SAndi Kleen if (!mapping) 22925718736SAndi Kleen return -EINVAL; 23025718736SAndi Kleen /* 23125718736SAndi Kleen * Only punch for normal data pages for now. 23225718736SAndi Kleen * Handling other types like directories would need more auditing. 23325718736SAndi Kleen */ 23425718736SAndi Kleen if (!S_ISREG(mapping->host->i_mode)) 23525718736SAndi Kleen return -EIO; 23625718736SAndi Kleen return truncate_inode_page(mapping, page); 23725718736SAndi Kleen } 23825718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page); 23925718736SAndi Kleen 24025718736SAndi Kleen /* 24183f78668SWu Fengguang * Safely invalidate one page from its pagecache mapping. 24283f78668SWu Fengguang * It only drops clean, unused pages. The page must be locked. 24383f78668SWu Fengguang * 24483f78668SWu Fengguang * Returns 1 if the page is successfully invalidated, otherwise 0. 24583f78668SWu Fengguang */ 24683f78668SWu Fengguang int invalidate_inode_page(struct page *page) 24783f78668SWu Fengguang { 24883f78668SWu Fengguang struct address_space *mapping = page_mapping(page); 24983f78668SWu Fengguang if (!mapping) 25083f78668SWu Fengguang return 0; 25183f78668SWu Fengguang if (PageDirty(page) || PageWriteback(page)) 25283f78668SWu Fengguang return 0; 25383f78668SWu Fengguang if (page_mapped(page)) 25483f78668SWu Fengguang return 0; 25583f78668SWu Fengguang return invalidate_complete_page(mapping, page); 25683f78668SWu Fengguang } 25783f78668SWu Fengguang 2581da177e4SLinus Torvalds /** 25973c1e204SLiu Bo * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 2601da177e4SLinus Torvalds * @mapping: mapping to truncate 2611da177e4SLinus Torvalds * @lstart: offset from which to truncate 2625a720394SLukas Czerner * @lend: offset to which to truncate (inclusive) 2631da177e4SLinus Torvalds * 264d7339071SHans Reiser * Truncate the page cache, removing the pages that are between 2655a720394SLukas Czerner * specified offsets (and zeroing out partial pages 2665a720394SLukas Czerner * if lstart or lend + 1 is not page aligned). 2671da177e4SLinus Torvalds * 2681da177e4SLinus Torvalds * Truncate takes two passes - the first pass is nonblocking. It will not 2691da177e4SLinus Torvalds * block on page locks and it will not block on writeback. The second pass 2701da177e4SLinus Torvalds * will wait. This is to prevent as much IO as possible in the affected region. 2711da177e4SLinus Torvalds * The first pass will remove most pages, so the search cost of the second pass 2721da177e4SLinus Torvalds * is low. 2731da177e4SLinus Torvalds * 2741da177e4SLinus Torvalds * We pass down the cache-hot hint to the page freeing code. Even if the 2751da177e4SLinus Torvalds * mapping is large, it is probably the case that the final pages are the most 2761da177e4SLinus Torvalds * recently touched, and freeing happens in ascending file offset order. 2775a720394SLukas Czerner * 2785a720394SLukas Czerner * Note that since ->invalidatepage() accepts range to invalidate 2795a720394SLukas Czerner * truncate_inode_pages_range is able to handle cases where lend + 1 is not 2805a720394SLukas Czerner * page aligned properly. 2811da177e4SLinus Torvalds */ 282d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping, 283d7339071SHans Reiser loff_t lstart, loff_t lend) 2841da177e4SLinus Torvalds { 2855a720394SLukas Czerner pgoff_t start; /* inclusive */ 2865a720394SLukas Czerner pgoff_t end; /* exclusive */ 2875a720394SLukas Czerner unsigned int partial_start; /* inclusive */ 2885a720394SLukas Czerner unsigned int partial_end; /* exclusive */ 2891da177e4SLinus Torvalds struct pagevec pvec; 2900cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 291b85e0effSHugh Dickins pgoff_t index; 2921da177e4SLinus Torvalds int i; 2931da177e4SLinus Torvalds 2947716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 29534ccb69eSAndrey Ryabinin goto out; 2961da177e4SLinus Torvalds 2975a720394SLukas Czerner /* Offsets within partial pages */ 29809cbfeafSKirill A. Shutemov partial_start = lstart & (PAGE_SIZE - 1); 29909cbfeafSKirill A. Shutemov partial_end = (lend + 1) & (PAGE_SIZE - 1); 3005a720394SLukas Czerner 3015a720394SLukas Czerner /* 3025a720394SLukas Czerner * 'start' and 'end' always covers the range of pages to be fully 3035a720394SLukas Czerner * truncated. Partial pages are covered with 'partial_start' at the 3045a720394SLukas Czerner * start of the range and 'partial_end' at the end of the range. 3055a720394SLukas Czerner * Note that 'end' is exclusive while 'lend' is inclusive. 3065a720394SLukas Czerner */ 30709cbfeafSKirill A. Shutemov start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 3085a720394SLukas Czerner if (lend == -1) 3095a720394SLukas Czerner /* 3105a720394SLukas Czerner * lend == -1 indicates end-of-file so we have to set 'end' 3115a720394SLukas Czerner * to the highest possible pgoff_t and since the type is 3125a720394SLukas Czerner * unsigned we're using -1. 3135a720394SLukas Czerner */ 3145a720394SLukas Czerner end = -1; 3155a720394SLukas Czerner else 31609cbfeafSKirill A. Shutemov end = (lend + 1) >> PAGE_SHIFT; 317d7339071SHans Reiser 31886679820SMel Gorman pagevec_init(&pvec); 319b85e0effSHugh Dickins index = start; 3205c211ba2SMatthew Wilcox (Oracle) while (index < end && find_lock_entries(mapping, index, end - 1, 3215c211ba2SMatthew Wilcox (Oracle) &pvec, indices)) { 3225c211ba2SMatthew Wilcox (Oracle) index = indices[pagevec_count(&pvec) - 1] + 1; 32331d270fdSMatthew Wilcox (Oracle) truncate_exceptional_pvec_entries(mapping, &pvec, indices); 3245c211ba2SMatthew Wilcox (Oracle) for (i = 0; i < pagevec_count(&pvec); i++) 32522061a1fSHugh Dickins truncate_cleanup_page(pvec.pages[i]); 3265c211ba2SMatthew Wilcox (Oracle) delete_from_page_cache_batch(mapping, &pvec); 3275c211ba2SMatthew Wilcox (Oracle) for (i = 0; i < pagevec_count(&pvec); i++) 3285c211ba2SMatthew Wilcox (Oracle) unlock_page(pvec.pages[i]); 3291da177e4SLinus Torvalds pagevec_release(&pvec); 3301da177e4SLinus Torvalds cond_resched(); 3311da177e4SLinus Torvalds } 3325c211ba2SMatthew Wilcox (Oracle) 3335a720394SLukas Czerner if (partial_start) { 3341da177e4SLinus Torvalds struct page *page = find_lock_page(mapping, start - 1); 3351da177e4SLinus Torvalds if (page) { 33609cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 3375a720394SLukas Czerner if (start > end) { 3385a720394SLukas Czerner /* Truncation within a single page */ 3395a720394SLukas Czerner top = partial_end; 3405a720394SLukas Czerner partial_end = 0; 3415a720394SLukas Czerner } 3421da177e4SLinus Torvalds wait_on_page_writeback(page); 3435a720394SLukas Czerner zero_user_segment(page, partial_start, top); 3445a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 3455a720394SLukas Czerner if (page_has_private(page)) 3465a720394SLukas Czerner do_invalidatepage(page, partial_start, 3475a720394SLukas Czerner top - partial_start); 3481da177e4SLinus Torvalds unlock_page(page); 34909cbfeafSKirill A. Shutemov put_page(page); 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds } 3525a720394SLukas Czerner if (partial_end) { 3535a720394SLukas Czerner struct page *page = find_lock_page(mapping, end); 3545a720394SLukas Czerner if (page) { 3555a720394SLukas Czerner wait_on_page_writeback(page); 3565a720394SLukas Czerner zero_user_segment(page, 0, partial_end); 3575a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 3585a720394SLukas Czerner if (page_has_private(page)) 3595a720394SLukas Czerner do_invalidatepage(page, 0, 3605a720394SLukas Czerner partial_end); 3615a720394SLukas Czerner unlock_page(page); 36209cbfeafSKirill A. Shutemov put_page(page); 3635a720394SLukas Czerner } 3645a720394SLukas Czerner } 3655a720394SLukas Czerner /* 3665a720394SLukas Czerner * If the truncation happened within a single page no pages 3675a720394SLukas Czerner * will be released, just zeroed, so we can bail out now. 3685a720394SLukas Czerner */ 3695a720394SLukas Czerner if (start >= end) 37034ccb69eSAndrey Ryabinin goto out; 3711da177e4SLinus Torvalds 372b85e0effSHugh Dickins index = start; 3731da177e4SLinus Torvalds for ( ; ; ) { 3741da177e4SLinus Torvalds cond_resched(); 375a656a202SMatthew Wilcox (Oracle) if (!find_get_entries(mapping, index, end - 1, &pvec, 37638cefeb3SMatthew Wilcox (Oracle) indices)) { 377792ceaefSHugh Dickins /* If all gone from start onwards, we're done */ 378b85e0effSHugh Dickins if (index == start) 3791da177e4SLinus Torvalds break; 380792ceaefSHugh Dickins /* Otherwise restart to make sure all gone */ 381b85e0effSHugh Dickins index = start; 3821da177e4SLinus Torvalds continue; 3831da177e4SLinus Torvalds } 384f2187599SMel Gorman 3851da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 3861da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 3871da177e4SLinus Torvalds 388b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 3890cd6144aSJohannes Weiner index = indices[i]; 390b85e0effSHugh Dickins 3913159f943SMatthew Wilcox if (xa_is_value(page)) 3920cd6144aSJohannes Weiner continue; 3930cd6144aSJohannes Weiner 3941da177e4SLinus Torvalds lock_page(page); 3955cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 3961da177e4SLinus Torvalds wait_on_page_writeback(page); 397750b4987SNick Piggin truncate_inode_page(mapping, page); 3981da177e4SLinus Torvalds unlock_page(page); 3991da177e4SLinus Torvalds } 40031d270fdSMatthew Wilcox (Oracle) truncate_exceptional_pvec_entries(mapping, &pvec, indices); 4011da177e4SLinus Torvalds pagevec_release(&pvec); 402b85e0effSHugh Dickins index++; 4031da177e4SLinus Torvalds } 40434ccb69eSAndrey Ryabinin 40534ccb69eSAndrey Ryabinin out: 4063167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 4071da177e4SLinus Torvalds } 408d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range); 4091da177e4SLinus Torvalds 410d7339071SHans Reiser /** 411d7339071SHans Reiser * truncate_inode_pages - truncate *all* the pages from an offset 412d7339071SHans Reiser * @mapping: mapping to truncate 413d7339071SHans Reiser * @lstart: offset from which to truncate 414d7339071SHans Reiser * 415*730633f0SJan Kara * Called under (and serialised by) inode->i_rwsem and 416*730633f0SJan Kara * mapping->invalidate_lock. 41708142579SJan Kara * 41808142579SJan Kara * Note: When this function returns, there can be a page in the process of 41908142579SJan Kara * deletion (inside __delete_from_page_cache()) in the specified range. Thus 42008142579SJan Kara * mapping->nrpages can be non-zero when this function returns even after 42108142579SJan Kara * truncation of the whole mapping. 422d7339071SHans Reiser */ 423d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 424d7339071SHans Reiser { 425d7339071SHans Reiser truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 426d7339071SHans Reiser } 4271da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages); 4281da177e4SLinus Torvalds 42928697355SMike Waychison /** 43091b0abe3SJohannes Weiner * truncate_inode_pages_final - truncate *all* pages before inode dies 43191b0abe3SJohannes Weiner * @mapping: mapping to truncate 43291b0abe3SJohannes Weiner * 4339608703eSJan Kara * Called under (and serialized by) inode->i_rwsem. 43491b0abe3SJohannes Weiner * 43591b0abe3SJohannes Weiner * Filesystems have to use this in the .evict_inode path to inform the 43691b0abe3SJohannes Weiner * VM that this is the final truncate and the inode is going away. 43791b0abe3SJohannes Weiner */ 43891b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping) 43991b0abe3SJohannes Weiner { 44091b0abe3SJohannes Weiner /* 44191b0abe3SJohannes Weiner * Page reclaim can not participate in regular inode lifetime 44291b0abe3SJohannes Weiner * management (can't call iput()) and thus can race with the 44391b0abe3SJohannes Weiner * inode teardown. Tell it when the address space is exiting, 44491b0abe3SJohannes Weiner * so that it does not install eviction information after the 44591b0abe3SJohannes Weiner * final truncate has begun. 44691b0abe3SJohannes Weiner */ 44791b0abe3SJohannes Weiner mapping_set_exiting(mapping); 44891b0abe3SJohannes Weiner 4497716506aSMatthew Wilcox (Oracle) if (!mapping_empty(mapping)) { 45091b0abe3SJohannes Weiner /* 45191b0abe3SJohannes Weiner * As truncation uses a lockless tree lookup, cycle 45291b0abe3SJohannes Weiner * the tree lock to make sure any ongoing tree 45391b0abe3SJohannes Weiner * modification that does not see AS_EXITING is 45491b0abe3SJohannes Weiner * completed before starting the final truncate. 45591b0abe3SJohannes Weiner */ 456b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 457b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 45891b0abe3SJohannes Weiner } 4596ff38bd4SPavel Tikhomirov 4606ff38bd4SPavel Tikhomirov /* 4616ff38bd4SPavel Tikhomirov * Cleancache needs notification even if there are no pages or shadow 4626ff38bd4SPavel Tikhomirov * entries. 4636ff38bd4SPavel Tikhomirov */ 4646ff38bd4SPavel Tikhomirov truncate_inode_pages(mapping, 0); 46591b0abe3SJohannes Weiner } 46691b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final); 46791b0abe3SJohannes Weiner 468a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping, 469eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 4701da177e4SLinus Torvalds { 4710cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 4721da177e4SLinus Torvalds struct pagevec pvec; 473b85e0effSHugh Dickins pgoff_t index = start; 47431560180SMinchan Kim unsigned long ret; 47531560180SMinchan Kim unsigned long count = 0; 4761da177e4SLinus Torvalds int i; 4771da177e4SLinus Torvalds 47886679820SMel Gorman pagevec_init(&pvec); 4795c211ba2SMatthew Wilcox (Oracle) while (find_lock_entries(mapping, index, end, &pvec, indices)) { 4801da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 4811da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 4821da177e4SLinus Torvalds 483b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4840cd6144aSJohannes Weiner index = indices[i]; 485e0f23603SNeilBrown 4863159f943SMatthew Wilcox if (xa_is_value(page)) { 487c6dcf52cSJan Kara invalidate_exceptional_entry(mapping, index, 488c6dcf52cSJan Kara page); 4890cd6144aSJohannes Weiner continue; 4900cd6144aSJohannes Weiner } 4915c211ba2SMatthew Wilcox (Oracle) index += thp_nr_pages(page) - 1; 492fc127da0SKirill A. Shutemov 49331560180SMinchan Kim ret = invalidate_inode_page(page); 4941da177e4SLinus Torvalds unlock_page(page); 49531560180SMinchan Kim /* 49631560180SMinchan Kim * Invalidation is a hint that the page is no longer 49731560180SMinchan Kim * of interest and try to speed up its reclaim. 49831560180SMinchan Kim */ 499eb1d7a65SYafang Shao if (!ret) { 500cc5993bdSMinchan Kim deactivate_file_page(page); 501eb1d7a65SYafang Shao /* It is likely on the pagevec of a remote CPU */ 502eb1d7a65SYafang Shao if (nr_pagevec) 503eb1d7a65SYafang Shao (*nr_pagevec)++; 504eb1d7a65SYafang Shao } 50531560180SMinchan Kim count += ret; 5061da177e4SLinus Torvalds } 5070cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 5081da177e4SLinus Torvalds pagevec_release(&pvec); 509fc9a07e7SAndrew Morton cond_resched(); 510b85e0effSHugh Dickins index++; 5111da177e4SLinus Torvalds } 51231560180SMinchan Kim return count; 5131da177e4SLinus Torvalds } 514eb1d7a65SYafang Shao 515eb1d7a65SYafang Shao /** 516eb1d7a65SYafang Shao * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode 517eb1d7a65SYafang Shao * @mapping: the address_space which holds the pages to invalidate 518eb1d7a65SYafang Shao * @start: the offset 'from' which to invalidate 519eb1d7a65SYafang Shao * @end: the offset 'to' which to invalidate (inclusive) 520eb1d7a65SYafang Shao * 521eb1d7a65SYafang Shao * This function only removes the unlocked pages, if you want to 522eb1d7a65SYafang Shao * remove all the pages of one inode, you must call truncate_inode_pages. 523eb1d7a65SYafang Shao * 524eb1d7a65SYafang Shao * invalidate_mapping_pages() will not block on IO activity. It will not 525eb1d7a65SYafang Shao * invalidate pages which are dirty, locked, under writeback or mapped into 526eb1d7a65SYafang Shao * pagetables. 527eb1d7a65SYafang Shao * 528eb1d7a65SYafang Shao * Return: the number of the pages that were invalidated 529eb1d7a65SYafang Shao */ 530eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping, 531eb1d7a65SYafang Shao pgoff_t start, pgoff_t end) 532eb1d7a65SYafang Shao { 533eb1d7a65SYafang Shao return __invalidate_mapping_pages(mapping, start, end, NULL); 534eb1d7a65SYafang Shao } 53554bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages); 5361da177e4SLinus Torvalds 537eb1d7a65SYafang Shao /** 538649c6dfeSAlex Shi * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode 539649c6dfeSAlex Shi * @mapping: the address_space which holds the pages to invalidate 540649c6dfeSAlex Shi * @start: the offset 'from' which to invalidate 541649c6dfeSAlex Shi * @end: the offset 'to' which to invalidate (inclusive) 542649c6dfeSAlex Shi * @nr_pagevec: invalidate failed page number for caller 543649c6dfeSAlex Shi * 544a00cda3fSMauro Carvalho Chehab * This helper is similar to invalidate_mapping_pages(), except that it accounts 545a00cda3fSMauro Carvalho Chehab * for pages that are likely on a pagevec and counts them in @nr_pagevec, which 546a00cda3fSMauro Carvalho Chehab * will be used by the caller. 547eb1d7a65SYafang Shao */ 548eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping, 549eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 550eb1d7a65SYafang Shao { 551eb1d7a65SYafang Shao __invalidate_mapping_pages(mapping, start, end, nr_pagevec); 552eb1d7a65SYafang Shao } 553eb1d7a65SYafang Shao 554bd4c8ce4SAndrew Morton /* 555bd4c8ce4SAndrew Morton * This is like invalidate_complete_page(), except it ignores the page's 556bd4c8ce4SAndrew Morton * refcount. We do this because invalidate_inode_pages2() needs stronger 557bd4c8ce4SAndrew Morton * invalidation guarantees, and cannot afford to leave pages behind because 5582706a1b8SAnderson Briglia * shrink_page_list() has a temp ref on them, or because they're transiently 5592706a1b8SAnderson Briglia * sitting in the lru_cache_add() pagevecs. 560bd4c8ce4SAndrew Morton */ 561bd4c8ce4SAndrew Morton static int 562bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page) 563bd4c8ce4SAndrew Morton { 564c4843a75SGreg Thelen unsigned long flags; 565c4843a75SGreg Thelen 566bd4c8ce4SAndrew Morton if (page->mapping != mapping) 567bd4c8ce4SAndrew Morton return 0; 568bd4c8ce4SAndrew Morton 569266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) 570bd4c8ce4SAndrew Morton return 0; 571bd4c8ce4SAndrew Morton 572b93b0163SMatthew Wilcox xa_lock_irqsave(&mapping->i_pages, flags); 573bd4c8ce4SAndrew Morton if (PageDirty(page)) 574bd4c8ce4SAndrew Morton goto failed; 575bd4c8ce4SAndrew Morton 576266cf658SDavid Howells BUG_ON(page_has_private(page)); 57762cccb8cSJohannes Weiner __delete_from_page_cache(page, NULL); 578b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 5796072d13cSLinus Torvalds 5806072d13cSLinus Torvalds if (mapping->a_ops->freepage) 5816072d13cSLinus Torvalds mapping->a_ops->freepage(page); 5826072d13cSLinus Torvalds 58309cbfeafSKirill A. Shutemov put_page(page); /* pagecache ref */ 584bd4c8ce4SAndrew Morton return 1; 585bd4c8ce4SAndrew Morton failed: 586b93b0163SMatthew Wilcox xa_unlock_irqrestore(&mapping->i_pages, flags); 587bd4c8ce4SAndrew Morton return 0; 588bd4c8ce4SAndrew Morton } 589bd4c8ce4SAndrew Morton 590e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page) 591e3db7691STrond Myklebust { 592e3db7691STrond Myklebust if (!PageDirty(page)) 593e3db7691STrond Myklebust return 0; 594e3db7691STrond Myklebust if (page->mapping != mapping || mapping->a_ops->launder_page == NULL) 595e3db7691STrond Myklebust return 0; 596e3db7691STrond Myklebust return mapping->a_ops->launder_page(page); 597e3db7691STrond Myklebust } 598e3db7691STrond Myklebust 5991da177e4SLinus Torvalds /** 6001da177e4SLinus Torvalds * invalidate_inode_pages2_range - remove range of pages from an address_space 60167be2dd1SMartin Waitz * @mapping: the address_space 6021da177e4SLinus Torvalds * @start: the page offset 'from' which to invalidate 6031da177e4SLinus Torvalds * @end: the page offset 'to' which to invalidate (inclusive) 6041da177e4SLinus Torvalds * 6051da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6061da177e4SLinus Torvalds * invalidation. 6071da177e4SLinus Torvalds * 608a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 6091da177e4SLinus Torvalds */ 6101da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping, 6111da177e4SLinus Torvalds pgoff_t start, pgoff_t end) 6121da177e4SLinus Torvalds { 6130cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 6141da177e4SLinus Torvalds struct pagevec pvec; 615b85e0effSHugh Dickins pgoff_t index; 6161da177e4SLinus Torvalds int i; 6171da177e4SLinus Torvalds int ret = 0; 6180dd1334fSHisashi Hifumi int ret2 = 0; 6191da177e4SLinus Torvalds int did_range_unmap = 0; 6201da177e4SLinus Torvalds 6217716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 62234ccb69eSAndrey Ryabinin goto out; 62332691f0fSAndrey Ryabinin 62486679820SMel Gorman pagevec_init(&pvec); 625b85e0effSHugh Dickins index = start; 626a656a202SMatthew Wilcox (Oracle) while (find_get_entries(mapping, index, end, &pvec, indices)) { 6277b965e08STrond Myklebust for (i = 0; i < pagevec_count(&pvec); i++) { 6281da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 629b85e0effSHugh Dickins 630b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 6310cd6144aSJohannes Weiner index = indices[i]; 6321da177e4SLinus Torvalds 6333159f943SMatthew Wilcox if (xa_is_value(page)) { 634c6dcf52cSJan Kara if (!invalidate_exceptional_entry2(mapping, 635c6dcf52cSJan Kara index, page)) 636c6dcf52cSJan Kara ret = -EBUSY; 6370cd6144aSJohannes Weiner continue; 6380cd6144aSJohannes Weiner } 6390cd6144aSJohannes Weiner 64022061a1fSHugh Dickins if (!did_range_unmap && page_mapped(page)) { 64122061a1fSHugh Dickins /* 64222061a1fSHugh Dickins * If page is mapped, before taking its lock, 64322061a1fSHugh Dickins * zap the rest of the file in one hit. 64422061a1fSHugh Dickins */ 64522061a1fSHugh Dickins unmap_mapping_pages(mapping, index, 64622061a1fSHugh Dickins (1 + end - index), false); 64722061a1fSHugh Dickins did_range_unmap = 1; 64822061a1fSHugh Dickins } 64922061a1fSHugh Dickins 6501da177e4SLinus Torvalds lock_page(page); 6515cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 6521da177e4SLinus Torvalds if (page->mapping != mapping) { 6531da177e4SLinus Torvalds unlock_page(page); 6541da177e4SLinus Torvalds continue; 6551da177e4SLinus Torvalds } 6561da177e4SLinus Torvalds wait_on_page_writeback(page); 65722061a1fSHugh Dickins 65822061a1fSHugh Dickins if (page_mapped(page)) 65922061a1fSHugh Dickins unmap_mapping_page(page); 660d00806b1SNick Piggin BUG_ON(page_mapped(page)); 66122061a1fSHugh Dickins 6620dd1334fSHisashi Hifumi ret2 = do_launder_page(mapping, page); 6630dd1334fSHisashi Hifumi if (ret2 == 0) { 6640dd1334fSHisashi Hifumi if (!invalidate_complete_page2(mapping, page)) 6656ccfa806SHisashi Hifumi ret2 = -EBUSY; 6660dd1334fSHisashi Hifumi } 6670dd1334fSHisashi Hifumi if (ret2 < 0) 6680dd1334fSHisashi Hifumi ret = ret2; 6691da177e4SLinus Torvalds unlock_page(page); 6701da177e4SLinus Torvalds } 6710cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 6721da177e4SLinus Torvalds pagevec_release(&pvec); 6731da177e4SLinus Torvalds cond_resched(); 674b85e0effSHugh Dickins index++; 6751da177e4SLinus Torvalds } 676cd656375SJan Kara /* 67769b6c131SMatthew Wilcox * For DAX we invalidate page tables after invalidating page cache. We 678cd656375SJan Kara * could invalidate page tables while invalidating each entry however 679cd656375SJan Kara * that would be expensive. And doing range unmapping before doesn't 68069b6c131SMatthew Wilcox * work as we have no cheap way to find whether page cache entry didn't 681cd656375SJan Kara * get remapped later. 682cd656375SJan Kara */ 683cd656375SJan Kara if (dax_mapping(mapping)) { 684977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, start, end - start + 1, false); 685cd656375SJan Kara } 68634ccb69eSAndrey Ryabinin out: 6873167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 6881da177e4SLinus Torvalds return ret; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds /** 6931da177e4SLinus Torvalds * invalidate_inode_pages2 - remove all pages from an address_space 69467be2dd1SMartin Waitz * @mapping: the address_space 6951da177e4SLinus Torvalds * 6961da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6971da177e4SLinus Torvalds * invalidation. 6981da177e4SLinus Torvalds * 699a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds return invalidate_inode_pages2_range(mapping, 0, -1); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 70625d9e2d1Snpiggin@suse.de 70725d9e2d1Snpiggin@suse.de /** 70825d9e2d1Snpiggin@suse.de * truncate_pagecache - unmap and remove pagecache that has been truncated 70925d9e2d1Snpiggin@suse.de * @inode: inode 7108a549beaSHugh Dickins * @newsize: new file size 71125d9e2d1Snpiggin@suse.de * 71225d9e2d1Snpiggin@suse.de * inode's new i_size must already be written before truncate_pagecache 71325d9e2d1Snpiggin@suse.de * is called. 71425d9e2d1Snpiggin@suse.de * 71525d9e2d1Snpiggin@suse.de * This function should typically be called before the filesystem 71625d9e2d1Snpiggin@suse.de * releases resources associated with the freed range (eg. deallocates 71725d9e2d1Snpiggin@suse.de * blocks). This way, pagecache will always stay logically coherent 71825d9e2d1Snpiggin@suse.de * with on-disk format, and the filesystem would not have to deal with 71925d9e2d1Snpiggin@suse.de * situations such as writepage being called for a page that has already 72025d9e2d1Snpiggin@suse.de * had its underlying blocks deallocated. 72125d9e2d1Snpiggin@suse.de */ 7227caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize) 72325d9e2d1Snpiggin@suse.de { 72425d9e2d1Snpiggin@suse.de struct address_space *mapping = inode->i_mapping; 7258a549beaSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 72625d9e2d1Snpiggin@suse.de 72725d9e2d1Snpiggin@suse.de /* 72825d9e2d1Snpiggin@suse.de * unmap_mapping_range is called twice, first simply for 72925d9e2d1Snpiggin@suse.de * efficiency so that truncate_inode_pages does fewer 73025d9e2d1Snpiggin@suse.de * single-page unmaps. However after this first call, and 73125d9e2d1Snpiggin@suse.de * before truncate_inode_pages finishes, it is possible for 73225d9e2d1Snpiggin@suse.de * private pages to be COWed, which remain after 73325d9e2d1Snpiggin@suse.de * truncate_inode_pages finishes, hence the second 73425d9e2d1Snpiggin@suse.de * unmap_mapping_range call must be made for correctness. 73525d9e2d1Snpiggin@suse.de */ 7368a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 7378a549beaSHugh Dickins truncate_inode_pages(mapping, newsize); 7388a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 73925d9e2d1Snpiggin@suse.de } 74025d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache); 74125d9e2d1Snpiggin@suse.de 74225d9e2d1Snpiggin@suse.de /** 7432c27c65eSChristoph Hellwig * truncate_setsize - update inode and pagecache for a new file size 7442c27c65eSChristoph Hellwig * @inode: inode 7452c27c65eSChristoph Hellwig * @newsize: new file size 7462c27c65eSChristoph Hellwig * 747382e27daSJan Kara * truncate_setsize updates i_size and performs pagecache truncation (if 748382e27daSJan Kara * necessary) to @newsize. It will be typically be called from the filesystem's 749382e27daSJan Kara * setattr function when ATTR_SIZE is passed in. 7502c27c65eSChristoph Hellwig * 75177783d06SJan Kara * Must be called with a lock serializing truncates and writes (generally 7529608703eSJan Kara * i_rwsem but e.g. xfs uses a different lock) and before all filesystem 75377783d06SJan Kara * specific block truncation has been performed. 7542c27c65eSChristoph Hellwig */ 7552c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize) 7562c27c65eSChristoph Hellwig { 75790a80202SJan Kara loff_t oldsize = inode->i_size; 75890a80202SJan Kara 7592c27c65eSChristoph Hellwig i_size_write(inode, newsize); 76090a80202SJan Kara if (newsize > oldsize) 76190a80202SJan Kara pagecache_isize_extended(inode, oldsize, newsize); 7627caef267SKirill A. Shutemov truncate_pagecache(inode, newsize); 7632c27c65eSChristoph Hellwig } 7642c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize); 7652c27c65eSChristoph Hellwig 7662c27c65eSChristoph Hellwig /** 76790a80202SJan Kara * pagecache_isize_extended - update pagecache after extension of i_size 76890a80202SJan Kara * @inode: inode for which i_size was extended 76990a80202SJan Kara * @from: original inode size 77090a80202SJan Kara * @to: new inode size 77190a80202SJan Kara * 77290a80202SJan Kara * Handle extension of inode size either caused by extending truncate or by 77390a80202SJan Kara * write starting after current i_size. We mark the page straddling current 77490a80202SJan Kara * i_size RO so that page_mkwrite() is called on the nearest write access to 77590a80202SJan Kara * the page. This way filesystem can be sure that page_mkwrite() is called on 77690a80202SJan Kara * the page before user writes to the page via mmap after the i_size has been 77790a80202SJan Kara * changed. 77890a80202SJan Kara * 77990a80202SJan Kara * The function must be called after i_size is updated so that page fault 78090a80202SJan Kara * coming after we unlock the page will already see the new i_size. 7819608703eSJan Kara * The function must be called while we still hold i_rwsem - this not only 78290a80202SJan Kara * makes sure i_size is stable but also that userspace cannot observe new 78390a80202SJan Kara * i_size value before we are prepared to store mmap writes at new inode size. 78490a80202SJan Kara */ 78590a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 78690a80202SJan Kara { 78793407472SFabian Frederick int bsize = i_blocksize(inode); 78890a80202SJan Kara loff_t rounded_from; 78990a80202SJan Kara struct page *page; 79090a80202SJan Kara pgoff_t index; 79190a80202SJan Kara 79290a80202SJan Kara WARN_ON(to > inode->i_size); 79390a80202SJan Kara 79409cbfeafSKirill A. Shutemov if (from >= to || bsize == PAGE_SIZE) 79590a80202SJan Kara return; 79690a80202SJan Kara /* Page straddling @from will not have any hole block created? */ 79790a80202SJan Kara rounded_from = round_up(from, bsize); 79809cbfeafSKirill A. Shutemov if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 79990a80202SJan Kara return; 80090a80202SJan Kara 80109cbfeafSKirill A. Shutemov index = from >> PAGE_SHIFT; 80290a80202SJan Kara page = find_lock_page(inode->i_mapping, index); 80390a80202SJan Kara /* Page not cached? Nothing to do */ 80490a80202SJan Kara if (!page) 80590a80202SJan Kara return; 80690a80202SJan Kara /* 80790a80202SJan Kara * See clear_page_dirty_for_io() for details why set_page_dirty() 80890a80202SJan Kara * is needed. 80990a80202SJan Kara */ 81090a80202SJan Kara if (page_mkclean(page)) 81190a80202SJan Kara set_page_dirty(page); 81290a80202SJan Kara unlock_page(page); 81309cbfeafSKirill A. Shutemov put_page(page); 81490a80202SJan Kara } 81590a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended); 81690a80202SJan Kara 81790a80202SJan Kara /** 818623e3db9SHugh Dickins * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 819623e3db9SHugh Dickins * @inode: inode 820623e3db9SHugh Dickins * @lstart: offset of beginning of hole 821623e3db9SHugh Dickins * @lend: offset of last byte of hole 822623e3db9SHugh Dickins * 823623e3db9SHugh Dickins * This function should typically be called before the filesystem 824623e3db9SHugh Dickins * releases resources associated with the freed range (eg. deallocates 825623e3db9SHugh Dickins * blocks). This way, pagecache will always stay logically coherent 826623e3db9SHugh Dickins * with on-disk format, and the filesystem would not have to deal with 827623e3db9SHugh Dickins * situations such as writepage being called for a page that has already 828623e3db9SHugh Dickins * had its underlying blocks deallocated. 829623e3db9SHugh Dickins */ 830623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 831623e3db9SHugh Dickins { 832623e3db9SHugh Dickins struct address_space *mapping = inode->i_mapping; 833623e3db9SHugh Dickins loff_t unmap_start = round_up(lstart, PAGE_SIZE); 834623e3db9SHugh Dickins loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 835623e3db9SHugh Dickins /* 836623e3db9SHugh Dickins * This rounding is currently just for example: unmap_mapping_range 837623e3db9SHugh Dickins * expands its hole outwards, whereas we want it to contract the hole 838623e3db9SHugh Dickins * inwards. However, existing callers of truncate_pagecache_range are 8395a720394SLukas Czerner * doing their own page rounding first. Note that unmap_mapping_range 8405a720394SLukas Czerner * allows holelen 0 for all, and we allow lend -1 for end of file. 841623e3db9SHugh Dickins */ 842623e3db9SHugh Dickins 843623e3db9SHugh Dickins /* 844623e3db9SHugh Dickins * Unlike in truncate_pagecache, unmap_mapping_range is called only 845623e3db9SHugh Dickins * once (before truncating pagecache), and without "even_cows" flag: 846623e3db9SHugh Dickins * hole-punching should not remove private COWed pages from the hole. 847623e3db9SHugh Dickins */ 848623e3db9SHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 849623e3db9SHugh Dickins unmap_mapping_range(mapping, unmap_start, 850623e3db9SHugh Dickins 1 + unmap_end - unmap_start, 0); 851623e3db9SHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 852623e3db9SHugh Dickins } 853623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range); 854