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 { 4851b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 49b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 50f2187599SMel Gorman __clear_shadow_entry(mapping, index, entry); 51b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 5251b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 5351b8c1feSJohannes Weiner inode_add_lru(mapping->host); 5451b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 550cd6144aSJohannes Weiner } 561da177e4SLinus Torvalds 57c6dcf52cSJan Kara /* 58f2187599SMel Gorman * Unconditionally remove exceptional entries. Usually called from truncate 59f2187599SMel Gorman * path. Note that the pagevec may be altered by this function by removing 60f2187599SMel Gorman * exceptional entries similar to what pagevec_remove_exceptionals does. 61c6dcf52cSJan Kara */ 62f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping, 6331d270fdSMatthew Wilcox (Oracle) struct pagevec *pvec, pgoff_t *indices) 64c6dcf52cSJan Kara { 65f2187599SMel Gorman int i, j; 6631d270fdSMatthew Wilcox (Oracle) bool dax; 67f2187599SMel Gorman 68c6dcf52cSJan Kara /* Handled by shmem itself */ 69c6dcf52cSJan Kara if (shmem_mapping(mapping)) 70c6dcf52cSJan Kara return; 71c6dcf52cSJan Kara 72f2187599SMel Gorman for (j = 0; j < pagevec_count(pvec); j++) 733159f943SMatthew Wilcox if (xa_is_value(pvec->pages[j])) 74f2187599SMel Gorman break; 75f2187599SMel Gorman 76f2187599SMel Gorman if (j == pagevec_count(pvec)) 77c6dcf52cSJan Kara return; 78f2187599SMel Gorman 79f2187599SMel Gorman dax = dax_mapping(mapping); 8051b8c1feSJohannes Weiner if (!dax) { 8151b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 82b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 8351b8c1feSJohannes Weiner } 84f2187599SMel Gorman 85f2187599SMel Gorman for (i = j; i < pagevec_count(pvec); i++) { 86f2187599SMel Gorman struct page *page = pvec->pages[i]; 87f2187599SMel Gorman pgoff_t index = indices[i]; 88f2187599SMel Gorman 893159f943SMatthew Wilcox if (!xa_is_value(page)) { 90f2187599SMel Gorman pvec->pages[j++] = page; 91f2187599SMel Gorman continue; 92c6dcf52cSJan Kara } 93f2187599SMel Gorman 94f2187599SMel Gorman if (unlikely(dax)) { 95f2187599SMel Gorman dax_delete_mapping_entry(mapping, index); 96f2187599SMel Gorman continue; 97f2187599SMel Gorman } 98f2187599SMel Gorman 99f2187599SMel Gorman __clear_shadow_entry(mapping, index, page); 100f2187599SMel Gorman } 101f2187599SMel Gorman 10251b8c1feSJohannes Weiner if (!dax) { 103b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 10451b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 10551b8c1feSJohannes Weiner inode_add_lru(mapping->host); 10651b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 10751b8c1feSJohannes Weiner } 108f2187599SMel Gorman pvec->nr = j; 109c6dcf52cSJan Kara } 110c6dcf52cSJan Kara 111c6dcf52cSJan Kara /* 112c6dcf52cSJan Kara * Invalidate exceptional entry if easily possible. This handles exceptional 1134636e70bSRoss Zwisler * entries for invalidate_inode_pages(). 114c6dcf52cSJan Kara */ 115c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping, 116c6dcf52cSJan Kara pgoff_t index, void *entry) 117c6dcf52cSJan Kara { 1184636e70bSRoss Zwisler /* Handled by shmem itself, or for DAX we do nothing. */ 1194636e70bSRoss Zwisler if (shmem_mapping(mapping) || dax_mapping(mapping)) 120c6dcf52cSJan Kara return 1; 121c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 122c6dcf52cSJan Kara return 1; 123c6dcf52cSJan Kara } 124c6dcf52cSJan Kara 125c6dcf52cSJan Kara /* 126c6dcf52cSJan Kara * Invalidate exceptional entry if clean. This handles exceptional entries for 127c6dcf52cSJan Kara * invalidate_inode_pages2() so for DAX it evicts only clean entries. 128c6dcf52cSJan Kara */ 129c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping, 130c6dcf52cSJan Kara pgoff_t index, void *entry) 131c6dcf52cSJan Kara { 132c6dcf52cSJan Kara /* Handled by shmem itself */ 133c6dcf52cSJan Kara if (shmem_mapping(mapping)) 134c6dcf52cSJan Kara return 1; 135c6dcf52cSJan Kara if (dax_mapping(mapping)) 136c6dcf52cSJan Kara return dax_invalidate_mapping_entry_sync(mapping, index); 137c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 138c6dcf52cSJan Kara return 1; 139c6dcf52cSJan Kara } 140c6dcf52cSJan Kara 141cf9a2ae8SDavid Howells /** 14228bc44d7SFengguang Wu * do_invalidatepage - invalidate part or all of a page 143cf9a2ae8SDavid Howells * @page: the page which is affected 144d47992f8SLukas Czerner * @offset: start of the range to invalidate 145d47992f8SLukas Czerner * @length: length of the range to invalidate 146cf9a2ae8SDavid Howells * 147cf9a2ae8SDavid Howells * do_invalidatepage() is called when all or part of the page has become 148cf9a2ae8SDavid Howells * invalidated by a truncate operation. 149cf9a2ae8SDavid Howells * 150cf9a2ae8SDavid Howells * do_invalidatepage() does not have to release all buffers, but it must 151cf9a2ae8SDavid Howells * ensure that no dirty buffer is left outside @offset and that no I/O 152cf9a2ae8SDavid Howells * is underway against any of the blocks which are outside the truncation 153cf9a2ae8SDavid Howells * point. Because the caller is about to free (and possibly reuse) those 154cf9a2ae8SDavid Howells * blocks on-disk. 155cf9a2ae8SDavid Howells */ 156d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset, 157d47992f8SLukas Czerner unsigned int length) 158cf9a2ae8SDavid Howells { 159d47992f8SLukas Czerner void (*invalidatepage)(struct page *, unsigned int, unsigned int); 160d47992f8SLukas Czerner 161cf9a2ae8SDavid Howells invalidatepage = page->mapping->a_ops->invalidatepage; 1629361401eSDavid Howells #ifdef CONFIG_BLOCK 163cf9a2ae8SDavid Howells if (!invalidatepage) 164cf9a2ae8SDavid Howells invalidatepage = block_invalidatepage; 1659361401eSDavid Howells #endif 166cf9a2ae8SDavid Howells if (invalidatepage) 167d47992f8SLukas Czerner (*invalidatepage)(page, offset, length); 168cf9a2ae8SDavid Howells } 169cf9a2ae8SDavid Howells 170ecdfc978SLinus Torvalds /* 1711da177e4SLinus Torvalds * If truncate cannot remove the fs-private metadata from the page, the page 17262e1c553SShaohua Li * becomes orphaned. It will be left on the LRU and may even be mapped into 17354cb8821SNick Piggin * user pagetables if we're racing with filemap_fault(). 1741da177e4SLinus Torvalds * 175fc3a5ac5SMatthew Wilcox (Oracle) * We need to bail out if page->mapping is no longer equal to the original 1761da177e4SLinus Torvalds * mapping. This happens a) when the VM reclaimed the page while we waited on 177fc0ecff6SAndrew Morton * its lock, b) when a concurrent invalidate_mapping_pages got there first and 1781da177e4SLinus Torvalds * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 1791da177e4SLinus Torvalds */ 180efe99bbaSMatthew Wilcox (Oracle) static void truncate_cleanup_folio(struct folio *folio) 1811da177e4SLinus Torvalds { 182efe99bbaSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 1833506659eSMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 1841da177e4SLinus Torvalds 185efe99bbaSMatthew Wilcox (Oracle) if (folio_has_private(folio)) 186efe99bbaSMatthew Wilcox (Oracle) do_invalidatepage(&folio->page, 0, folio_size(folio)); 1871da177e4SLinus Torvalds 188b9ea2515SKonstantin Khlebnikov /* 189b9ea2515SKonstantin Khlebnikov * Some filesystems seem to re-dirty the page even after 190b9ea2515SKonstantin Khlebnikov * the VM has canceled the dirty bit (eg ext3 journaling). 191b9ea2515SKonstantin Khlebnikov * Hence dirty accounting check is placed after invalidation. 192b9ea2515SKonstantin Khlebnikov */ 193efe99bbaSMatthew Wilcox (Oracle) folio_cancel_dirty(folio); 194efe99bbaSMatthew Wilcox (Oracle) folio_clear_mappedtodisk(folio); 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* 198fc0ecff6SAndrew Morton * This is for invalidate_mapping_pages(). That function can be called at 1991da177e4SLinus Torvalds * any time, and is not supposed to throw away dirty pages. But pages can 2000fd0e6b0SNick Piggin * be marked dirty at any time too, so use remove_mapping which safely 2010fd0e6b0SNick Piggin * discards clean, unused pages. 2021da177e4SLinus Torvalds * 2031da177e4SLinus Torvalds * Returns non-zero if the page was successfully invalidated. 2041da177e4SLinus Torvalds */ 2051da177e4SLinus Torvalds static int 2061da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page) 2071da177e4SLinus Torvalds { 2080fd0e6b0SNick Piggin int ret; 2090fd0e6b0SNick Piggin 2101da177e4SLinus Torvalds if (page->mapping != mapping) 2111da177e4SLinus Torvalds return 0; 2121da177e4SLinus Torvalds 213266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, 0)) 2141da177e4SLinus Torvalds return 0; 2151da177e4SLinus Torvalds 2160fd0e6b0SNick Piggin ret = remove_mapping(mapping, page); 2170fd0e6b0SNick Piggin 2180fd0e6b0SNick Piggin return ret; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211e84a3d9SMatthew Wilcox (Oracle) int truncate_inode_folio(struct address_space *mapping, struct folio *folio) 222750b4987SNick Piggin { 2231e84a3d9SMatthew Wilcox (Oracle) if (folio->mapping != mapping) 2249f4e41f4SJan Kara return -EIO; 2259f4e41f4SJan Kara 226efe99bbaSMatthew Wilcox (Oracle) truncate_cleanup_folio(folio); 227efe99bbaSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 2289f4e41f4SJan Kara return 0; 229750b4987SNick Piggin } 230750b4987SNick Piggin 23183f78668SWu Fengguang /* 23225718736SAndi Kleen * Used to get rid of pages on hardware memory corruption. 23325718736SAndi Kleen */ 23425718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page) 23525718736SAndi Kleen { 2361e84a3d9SMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(PageTail(page), page); 2371e84a3d9SMatthew Wilcox (Oracle) 23825718736SAndi Kleen if (!mapping) 23925718736SAndi Kleen return -EINVAL; 24025718736SAndi Kleen /* 24125718736SAndi Kleen * Only punch for normal data pages for now. 24225718736SAndi Kleen * Handling other types like directories would need more auditing. 24325718736SAndi Kleen */ 24425718736SAndi Kleen if (!S_ISREG(mapping->host->i_mode)) 24525718736SAndi Kleen return -EIO; 2461e84a3d9SMatthew Wilcox (Oracle) return truncate_inode_folio(mapping, page_folio(page)); 24725718736SAndi Kleen } 24825718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page); 24925718736SAndi Kleen 25025718736SAndi Kleen /* 25183f78668SWu Fengguang * Safely invalidate one page from its pagecache mapping. 25283f78668SWu Fengguang * It only drops clean, unused pages. The page must be locked. 25383f78668SWu Fengguang * 25483f78668SWu Fengguang * Returns 1 if the page is successfully invalidated, otherwise 0. 25583f78668SWu Fengguang */ 25683f78668SWu Fengguang int invalidate_inode_page(struct page *page) 25783f78668SWu Fengguang { 25883f78668SWu Fengguang struct address_space *mapping = page_mapping(page); 25983f78668SWu Fengguang if (!mapping) 26083f78668SWu Fengguang return 0; 26183f78668SWu Fengguang if (PageDirty(page) || PageWriteback(page)) 26283f78668SWu Fengguang return 0; 26383f78668SWu Fengguang if (page_mapped(page)) 26483f78668SWu Fengguang return 0; 26583f78668SWu Fengguang return invalidate_complete_page(mapping, page); 26683f78668SWu Fengguang } 26783f78668SWu Fengguang 2681da177e4SLinus Torvalds /** 26973c1e204SLiu Bo * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 2701da177e4SLinus Torvalds * @mapping: mapping to truncate 2711da177e4SLinus Torvalds * @lstart: offset from which to truncate 2725a720394SLukas Czerner * @lend: offset to which to truncate (inclusive) 2731da177e4SLinus Torvalds * 274d7339071SHans Reiser * Truncate the page cache, removing the pages that are between 2755a720394SLukas Czerner * specified offsets (and zeroing out partial pages 2765a720394SLukas Czerner * if lstart or lend + 1 is not page aligned). 2771da177e4SLinus Torvalds * 2781da177e4SLinus Torvalds * Truncate takes two passes - the first pass is nonblocking. It will not 2791da177e4SLinus Torvalds * block on page locks and it will not block on writeback. The second pass 2801da177e4SLinus Torvalds * will wait. This is to prevent as much IO as possible in the affected region. 2811da177e4SLinus Torvalds * The first pass will remove most pages, so the search cost of the second pass 2821da177e4SLinus Torvalds * is low. 2831da177e4SLinus Torvalds * 2841da177e4SLinus Torvalds * We pass down the cache-hot hint to the page freeing code. Even if the 2851da177e4SLinus Torvalds * mapping is large, it is probably the case that the final pages are the most 2861da177e4SLinus Torvalds * recently touched, and freeing happens in ascending file offset order. 2875a720394SLukas Czerner * 2885a720394SLukas Czerner * Note that since ->invalidatepage() accepts range to invalidate 2895a720394SLukas Czerner * truncate_inode_pages_range is able to handle cases where lend + 1 is not 2905a720394SLukas Czerner * page aligned properly. 2911da177e4SLinus Torvalds */ 292d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping, 293d7339071SHans Reiser loff_t lstart, loff_t lend) 2941da177e4SLinus Torvalds { 2955a720394SLukas Czerner pgoff_t start; /* inclusive */ 2965a720394SLukas Czerner pgoff_t end; /* exclusive */ 2975a720394SLukas Czerner unsigned int partial_start; /* inclusive */ 2985a720394SLukas Czerner unsigned int partial_end; /* exclusive */ 2991da177e4SLinus Torvalds struct pagevec pvec; 3000cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 301b85e0effSHugh Dickins pgoff_t index; 3021da177e4SLinus Torvalds int i; 3031da177e4SLinus Torvalds 3047716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 30534ccb69eSAndrey Ryabinin goto out; 3061da177e4SLinus Torvalds 3075a720394SLukas Czerner /* Offsets within partial pages */ 30809cbfeafSKirill A. Shutemov partial_start = lstart & (PAGE_SIZE - 1); 30909cbfeafSKirill A. Shutemov partial_end = (lend + 1) & (PAGE_SIZE - 1); 3105a720394SLukas Czerner 3115a720394SLukas Czerner /* 3125a720394SLukas Czerner * 'start' and 'end' always covers the range of pages to be fully 3135a720394SLukas Czerner * truncated. Partial pages are covered with 'partial_start' at the 3145a720394SLukas Czerner * start of the range and 'partial_end' at the end of the range. 3155a720394SLukas Czerner * Note that 'end' is exclusive while 'lend' is inclusive. 3165a720394SLukas Czerner */ 31709cbfeafSKirill A. Shutemov start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 3185a720394SLukas Czerner if (lend == -1) 3195a720394SLukas Czerner /* 3205a720394SLukas Czerner * lend == -1 indicates end-of-file so we have to set 'end' 3215a720394SLukas Czerner * to the highest possible pgoff_t and since the type is 3225a720394SLukas Czerner * unsigned we're using -1. 3235a720394SLukas Czerner */ 3245a720394SLukas Czerner end = -1; 3255a720394SLukas Czerner else 32609cbfeafSKirill A. Shutemov end = (lend + 1) >> PAGE_SHIFT; 327d7339071SHans Reiser 32886679820SMel Gorman pagevec_init(&pvec); 329b85e0effSHugh Dickins index = start; 3305c211ba2SMatthew Wilcox (Oracle) while (index < end && find_lock_entries(mapping, index, end - 1, 3315c211ba2SMatthew Wilcox (Oracle) &pvec, indices)) { 3325c211ba2SMatthew Wilcox (Oracle) index = indices[pagevec_count(&pvec) - 1] + 1; 33331d270fdSMatthew Wilcox (Oracle) truncate_exceptional_pvec_entries(mapping, &pvec, indices); 3345c211ba2SMatthew Wilcox (Oracle) for (i = 0; i < pagevec_count(&pvec); i++) 335efe99bbaSMatthew Wilcox (Oracle) truncate_cleanup_folio(page_folio(pvec.pages[i])); 3365c211ba2SMatthew Wilcox (Oracle) delete_from_page_cache_batch(mapping, &pvec); 3375c211ba2SMatthew Wilcox (Oracle) for (i = 0; i < pagevec_count(&pvec); i++) 3385c211ba2SMatthew Wilcox (Oracle) unlock_page(pvec.pages[i]); 3391da177e4SLinus Torvalds pagevec_release(&pvec); 3401da177e4SLinus Torvalds cond_resched(); 3411da177e4SLinus Torvalds } 3425c211ba2SMatthew Wilcox (Oracle) 3435a720394SLukas Czerner if (partial_start) { 3441da177e4SLinus Torvalds struct page *page = find_lock_page(mapping, start - 1); 3451da177e4SLinus Torvalds if (page) { 34609cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 3475a720394SLukas Czerner if (start > end) { 3485a720394SLukas Czerner /* Truncation within a single page */ 3495a720394SLukas Czerner top = partial_end; 3505a720394SLukas Czerner partial_end = 0; 3515a720394SLukas Czerner } 3521da177e4SLinus Torvalds wait_on_page_writeback(page); 3535a720394SLukas Czerner zero_user_segment(page, partial_start, top); 3545a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 3555a720394SLukas Czerner if (page_has_private(page)) 3565a720394SLukas Czerner do_invalidatepage(page, partial_start, 3575a720394SLukas Czerner top - partial_start); 3581da177e4SLinus Torvalds unlock_page(page); 35909cbfeafSKirill A. Shutemov put_page(page); 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds } 3625a720394SLukas Czerner if (partial_end) { 3635a720394SLukas Czerner struct page *page = find_lock_page(mapping, end); 3645a720394SLukas Czerner if (page) { 3655a720394SLukas Czerner wait_on_page_writeback(page); 3665a720394SLukas Czerner zero_user_segment(page, 0, partial_end); 3675a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 3685a720394SLukas Czerner if (page_has_private(page)) 3695a720394SLukas Czerner do_invalidatepage(page, 0, 3705a720394SLukas Czerner partial_end); 3715a720394SLukas Czerner unlock_page(page); 37209cbfeafSKirill A. Shutemov put_page(page); 3735a720394SLukas Czerner } 3745a720394SLukas Czerner } 3755a720394SLukas Czerner /* 3765a720394SLukas Czerner * If the truncation happened within a single page no pages 3775a720394SLukas Czerner * will be released, just zeroed, so we can bail out now. 3785a720394SLukas Czerner */ 3795a720394SLukas Czerner if (start >= end) 38034ccb69eSAndrey Ryabinin goto out; 3811da177e4SLinus Torvalds 382b85e0effSHugh Dickins index = start; 3831da177e4SLinus Torvalds for ( ; ; ) { 3841da177e4SLinus Torvalds cond_resched(); 385a656a202SMatthew Wilcox (Oracle) if (!find_get_entries(mapping, index, end - 1, &pvec, 38638cefeb3SMatthew Wilcox (Oracle) indices)) { 387792ceaefSHugh Dickins /* If all gone from start onwards, we're done */ 388b85e0effSHugh Dickins if (index == start) 3891da177e4SLinus Torvalds break; 390792ceaefSHugh Dickins /* Otherwise restart to make sure all gone */ 391b85e0effSHugh Dickins index = start; 3921da177e4SLinus Torvalds continue; 3931da177e4SLinus Torvalds } 394f2187599SMel Gorman 3951da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 3961da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 3971e84a3d9SMatthew Wilcox (Oracle) struct folio *folio; 3981da177e4SLinus Torvalds 399b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4000cd6144aSJohannes Weiner index = indices[i]; 401b85e0effSHugh Dickins 4023159f943SMatthew Wilcox if (xa_is_value(page)) 4030cd6144aSJohannes Weiner continue; 4041e84a3d9SMatthew Wilcox (Oracle) folio = page_folio(page); 4050cd6144aSJohannes Weiner 4061e84a3d9SMatthew Wilcox (Oracle) folio_lock(folio); 4071e84a3d9SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio); 4081e84a3d9SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 4091e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 4101e84a3d9SMatthew Wilcox (Oracle) folio_unlock(folio); 411ccbbf761SMatthew Wilcox (Oracle) index = folio_index(folio) + folio_nr_pages(folio) - 1; 4121da177e4SLinus Torvalds } 41331d270fdSMatthew Wilcox (Oracle) truncate_exceptional_pvec_entries(mapping, &pvec, indices); 4141da177e4SLinus Torvalds pagevec_release(&pvec); 415b85e0effSHugh Dickins index++; 4161da177e4SLinus Torvalds } 41734ccb69eSAndrey Ryabinin 41834ccb69eSAndrey Ryabinin out: 4193167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 4201da177e4SLinus Torvalds } 421d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range); 4221da177e4SLinus Torvalds 423d7339071SHans Reiser /** 424d7339071SHans Reiser * truncate_inode_pages - truncate *all* the pages from an offset 425d7339071SHans Reiser * @mapping: mapping to truncate 426d7339071SHans Reiser * @lstart: offset from which to truncate 427d7339071SHans Reiser * 428730633f0SJan Kara * Called under (and serialised by) inode->i_rwsem and 429730633f0SJan Kara * mapping->invalidate_lock. 43008142579SJan Kara * 43108142579SJan Kara * Note: When this function returns, there can be a page in the process of 43208142579SJan Kara * deletion (inside __delete_from_page_cache()) in the specified range. Thus 43308142579SJan Kara * mapping->nrpages can be non-zero when this function returns even after 43408142579SJan Kara * truncation of the whole mapping. 435d7339071SHans Reiser */ 436d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 437d7339071SHans Reiser { 438d7339071SHans Reiser truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 439d7339071SHans Reiser } 4401da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages); 4411da177e4SLinus Torvalds 44228697355SMike Waychison /** 44391b0abe3SJohannes Weiner * truncate_inode_pages_final - truncate *all* pages before inode dies 44491b0abe3SJohannes Weiner * @mapping: mapping to truncate 44591b0abe3SJohannes Weiner * 4469608703eSJan Kara * Called under (and serialized by) inode->i_rwsem. 44791b0abe3SJohannes Weiner * 44891b0abe3SJohannes Weiner * Filesystems have to use this in the .evict_inode path to inform the 44991b0abe3SJohannes Weiner * VM that this is the final truncate and the inode is going away. 45091b0abe3SJohannes Weiner */ 45191b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping) 45291b0abe3SJohannes Weiner { 45391b0abe3SJohannes Weiner /* 45491b0abe3SJohannes Weiner * Page reclaim can not participate in regular inode lifetime 45591b0abe3SJohannes Weiner * management (can't call iput()) and thus can race with the 45691b0abe3SJohannes Weiner * inode teardown. Tell it when the address space is exiting, 45791b0abe3SJohannes Weiner * so that it does not install eviction information after the 45891b0abe3SJohannes Weiner * final truncate has begun. 45991b0abe3SJohannes Weiner */ 46091b0abe3SJohannes Weiner mapping_set_exiting(mapping); 46191b0abe3SJohannes Weiner 4627716506aSMatthew Wilcox (Oracle) if (!mapping_empty(mapping)) { 46391b0abe3SJohannes Weiner /* 46491b0abe3SJohannes Weiner * As truncation uses a lockless tree lookup, cycle 46591b0abe3SJohannes Weiner * the tree lock to make sure any ongoing tree 46691b0abe3SJohannes Weiner * modification that does not see AS_EXITING is 46791b0abe3SJohannes Weiner * completed before starting the final truncate. 46891b0abe3SJohannes Weiner */ 469b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 470b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 47191b0abe3SJohannes Weiner } 4726ff38bd4SPavel Tikhomirov 4736ff38bd4SPavel Tikhomirov /* 4746ff38bd4SPavel Tikhomirov * Cleancache needs notification even if there are no pages or shadow 4756ff38bd4SPavel Tikhomirov * entries. 4766ff38bd4SPavel Tikhomirov */ 4776ff38bd4SPavel Tikhomirov truncate_inode_pages(mapping, 0); 47891b0abe3SJohannes Weiner } 47991b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final); 48091b0abe3SJohannes Weiner 481a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping, 482eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 4831da177e4SLinus Torvalds { 4840cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 4851da177e4SLinus Torvalds struct pagevec pvec; 486b85e0effSHugh Dickins pgoff_t index = start; 48731560180SMinchan Kim unsigned long ret; 48831560180SMinchan Kim unsigned long count = 0; 4891da177e4SLinus Torvalds int i; 4901da177e4SLinus Torvalds 49186679820SMel Gorman pagevec_init(&pvec); 4925c211ba2SMatthew Wilcox (Oracle) while (find_lock_entries(mapping, index, end, &pvec, indices)) { 4931da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 4941da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 4951da177e4SLinus Torvalds 496b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4970cd6144aSJohannes Weiner index = indices[i]; 498e0f23603SNeilBrown 4993159f943SMatthew Wilcox if (xa_is_value(page)) { 5007ae12c80SJohannes Weiner count += invalidate_exceptional_entry(mapping, 5017ae12c80SJohannes Weiner index, 502c6dcf52cSJan Kara page); 5030cd6144aSJohannes Weiner continue; 5040cd6144aSJohannes Weiner } 5055c211ba2SMatthew Wilcox (Oracle) index += thp_nr_pages(page) - 1; 506fc127da0SKirill A. Shutemov 50731560180SMinchan Kim ret = invalidate_inode_page(page); 5081da177e4SLinus Torvalds unlock_page(page); 50931560180SMinchan Kim /* 51031560180SMinchan Kim * Invalidation is a hint that the page is no longer 51131560180SMinchan Kim * of interest and try to speed up its reclaim. 51231560180SMinchan Kim */ 513eb1d7a65SYafang Shao if (!ret) { 514cc5993bdSMinchan Kim deactivate_file_page(page); 515eb1d7a65SYafang Shao /* It is likely on the pagevec of a remote CPU */ 516eb1d7a65SYafang Shao if (nr_pagevec) 517eb1d7a65SYafang Shao (*nr_pagevec)++; 518eb1d7a65SYafang Shao } 51931560180SMinchan Kim count += ret; 5201da177e4SLinus Torvalds } 5210cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 5221da177e4SLinus Torvalds pagevec_release(&pvec); 523fc9a07e7SAndrew Morton cond_resched(); 524b85e0effSHugh Dickins index++; 5251da177e4SLinus Torvalds } 52631560180SMinchan Kim return count; 5271da177e4SLinus Torvalds } 528eb1d7a65SYafang Shao 529eb1d7a65SYafang Shao /** 5307ae12c80SJohannes Weiner * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode 5317ae12c80SJohannes Weiner * @mapping: the address_space which holds the cache to invalidate 532eb1d7a65SYafang Shao * @start: the offset 'from' which to invalidate 533eb1d7a65SYafang Shao * @end: the offset 'to' which to invalidate (inclusive) 534eb1d7a65SYafang Shao * 5357ae12c80SJohannes Weiner * This function removes pages that are clean, unmapped and unlocked, 5367ae12c80SJohannes Weiner * as well as shadow entries. It will not block on IO activity. 537eb1d7a65SYafang Shao * 5387ae12c80SJohannes Weiner * If you want to remove all the pages of one inode, regardless of 5397ae12c80SJohannes Weiner * their use and writeback state, use truncate_inode_pages(). 540eb1d7a65SYafang Shao * 5417ae12c80SJohannes Weiner * Return: the number of the cache entries that were invalidated 542eb1d7a65SYafang Shao */ 543eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping, 544eb1d7a65SYafang Shao pgoff_t start, pgoff_t end) 545eb1d7a65SYafang Shao { 546eb1d7a65SYafang Shao return __invalidate_mapping_pages(mapping, start, end, NULL); 547eb1d7a65SYafang Shao } 54854bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages); 5491da177e4SLinus Torvalds 550eb1d7a65SYafang Shao /** 551649c6dfeSAlex Shi * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode 552649c6dfeSAlex Shi * @mapping: the address_space which holds the pages to invalidate 553649c6dfeSAlex Shi * @start: the offset 'from' which to invalidate 554649c6dfeSAlex Shi * @end: the offset 'to' which to invalidate (inclusive) 555649c6dfeSAlex Shi * @nr_pagevec: invalidate failed page number for caller 556649c6dfeSAlex Shi * 557a00cda3fSMauro Carvalho Chehab * This helper is similar to invalidate_mapping_pages(), except that it accounts 558a00cda3fSMauro Carvalho Chehab * for pages that are likely on a pagevec and counts them in @nr_pagevec, which 559a00cda3fSMauro Carvalho Chehab * will be used by the caller. 560eb1d7a65SYafang Shao */ 561eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping, 562eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 563eb1d7a65SYafang Shao { 564eb1d7a65SYafang Shao __invalidate_mapping_pages(mapping, start, end, nr_pagevec); 565eb1d7a65SYafang Shao } 566eb1d7a65SYafang Shao 567bd4c8ce4SAndrew Morton /* 568bd4c8ce4SAndrew Morton * This is like invalidate_complete_page(), except it ignores the page's 569bd4c8ce4SAndrew Morton * refcount. We do this because invalidate_inode_pages2() needs stronger 570bd4c8ce4SAndrew Morton * invalidation guarantees, and cannot afford to leave pages behind because 5712706a1b8SAnderson Briglia * shrink_page_list() has a temp ref on them, or because they're transiently 5722706a1b8SAnderson Briglia * sitting in the lru_cache_add() pagevecs. 573bd4c8ce4SAndrew Morton */ 574bd4c8ce4SAndrew Morton static int 575bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page) 576bd4c8ce4SAndrew Morton { 577bd4c8ce4SAndrew Morton if (page->mapping != mapping) 578bd4c8ce4SAndrew Morton return 0; 579bd4c8ce4SAndrew Morton 580266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) 581bd4c8ce4SAndrew Morton return 0; 582bd4c8ce4SAndrew Morton 58351b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 58430472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 585bd4c8ce4SAndrew Morton if (PageDirty(page)) 586bd4c8ce4SAndrew Morton goto failed; 587bd4c8ce4SAndrew Morton 588266cf658SDavid Howells BUG_ON(page_has_private(page)); 58962cccb8cSJohannes Weiner __delete_from_page_cache(page, NULL); 59030472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 59151b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 59251b8c1feSJohannes Weiner inode_add_lru(mapping->host); 59351b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 5946072d13cSLinus Torvalds 5956072d13cSLinus Torvalds if (mapping->a_ops->freepage) 5966072d13cSLinus Torvalds mapping->a_ops->freepage(page); 5976072d13cSLinus Torvalds 59809cbfeafSKirill A. Shutemov put_page(page); /* pagecache ref */ 599bd4c8ce4SAndrew Morton return 1; 600bd4c8ce4SAndrew Morton failed: 60130472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 60251b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 603bd4c8ce4SAndrew Morton return 0; 604bd4c8ce4SAndrew Morton } 605bd4c8ce4SAndrew Morton 606e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page) 607e3db7691STrond Myklebust { 608e3db7691STrond Myklebust if (!PageDirty(page)) 609e3db7691STrond Myklebust return 0; 610e3db7691STrond Myklebust if (page->mapping != mapping || mapping->a_ops->launder_page == NULL) 611e3db7691STrond Myklebust return 0; 612e3db7691STrond Myklebust return mapping->a_ops->launder_page(page); 613e3db7691STrond Myklebust } 614e3db7691STrond Myklebust 6151da177e4SLinus Torvalds /** 6161da177e4SLinus Torvalds * invalidate_inode_pages2_range - remove range of pages from an address_space 61767be2dd1SMartin Waitz * @mapping: the address_space 6181da177e4SLinus Torvalds * @start: the page offset 'from' which to invalidate 6191da177e4SLinus Torvalds * @end: the page offset 'to' which to invalidate (inclusive) 6201da177e4SLinus Torvalds * 6211da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6221da177e4SLinus Torvalds * invalidation. 6231da177e4SLinus Torvalds * 624a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 6251da177e4SLinus Torvalds */ 6261da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping, 6271da177e4SLinus Torvalds pgoff_t start, pgoff_t end) 6281da177e4SLinus Torvalds { 6290cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 6301da177e4SLinus Torvalds struct pagevec pvec; 631b85e0effSHugh Dickins pgoff_t index; 6321da177e4SLinus Torvalds int i; 6331da177e4SLinus Torvalds int ret = 0; 6340dd1334fSHisashi Hifumi int ret2 = 0; 6351da177e4SLinus Torvalds int did_range_unmap = 0; 6361da177e4SLinus Torvalds 6377716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 63834ccb69eSAndrey Ryabinin goto out; 63932691f0fSAndrey Ryabinin 64086679820SMel Gorman pagevec_init(&pvec); 641b85e0effSHugh Dickins index = start; 642a656a202SMatthew Wilcox (Oracle) while (find_get_entries(mapping, index, end, &pvec, indices)) { 6437b965e08STrond Myklebust for (i = 0; i < pagevec_count(&pvec); i++) { 6441da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 645*fae9bc4aSMatthew Wilcox (Oracle) struct folio *folio; 646b85e0effSHugh Dickins 647*fae9bc4aSMatthew Wilcox (Oracle) /* We rely upon deletion not changing folio->index */ 6480cd6144aSJohannes Weiner index = indices[i]; 6491da177e4SLinus Torvalds 6503159f943SMatthew Wilcox if (xa_is_value(page)) { 651c6dcf52cSJan Kara if (!invalidate_exceptional_entry2(mapping, 652c6dcf52cSJan Kara index, page)) 653c6dcf52cSJan Kara ret = -EBUSY; 6540cd6144aSJohannes Weiner continue; 6550cd6144aSJohannes Weiner } 656*fae9bc4aSMatthew Wilcox (Oracle) folio = page_folio(page); 6570cd6144aSJohannes Weiner 658*fae9bc4aSMatthew Wilcox (Oracle) if (!did_range_unmap && folio_mapped(folio)) { 65922061a1fSHugh Dickins /* 660*fae9bc4aSMatthew Wilcox (Oracle) * If folio is mapped, before taking its lock, 66122061a1fSHugh Dickins * zap the rest of the file in one hit. 66222061a1fSHugh Dickins */ 66322061a1fSHugh Dickins unmap_mapping_pages(mapping, index, 66422061a1fSHugh Dickins (1 + end - index), false); 66522061a1fSHugh Dickins did_range_unmap = 1; 66622061a1fSHugh Dickins } 66722061a1fSHugh Dickins 668*fae9bc4aSMatthew Wilcox (Oracle) folio_lock(folio); 669*fae9bc4aSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio); 670*fae9bc4aSMatthew Wilcox (Oracle) if (folio->mapping != mapping) { 671*fae9bc4aSMatthew Wilcox (Oracle) folio_unlock(folio); 6721da177e4SLinus Torvalds continue; 6731da177e4SLinus Torvalds } 674*fae9bc4aSMatthew Wilcox (Oracle) folio_wait_writeback(folio); 67522061a1fSHugh Dickins 676*fae9bc4aSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 677*fae9bc4aSMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 678*fae9bc4aSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 67922061a1fSHugh Dickins 680*fae9bc4aSMatthew Wilcox (Oracle) ret2 = do_launder_page(mapping, &folio->page); 6810dd1334fSHisashi Hifumi if (ret2 == 0) { 682*fae9bc4aSMatthew Wilcox (Oracle) if (!invalidate_complete_page2(mapping, 683*fae9bc4aSMatthew Wilcox (Oracle) &folio->page)) 6846ccfa806SHisashi Hifumi ret2 = -EBUSY; 6850dd1334fSHisashi Hifumi } 6860dd1334fSHisashi Hifumi if (ret2 < 0) 6870dd1334fSHisashi Hifumi ret = ret2; 688*fae9bc4aSMatthew Wilcox (Oracle) folio_unlock(folio); 6891da177e4SLinus Torvalds } 6900cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 6911da177e4SLinus Torvalds pagevec_release(&pvec); 6921da177e4SLinus Torvalds cond_resched(); 693b85e0effSHugh Dickins index++; 6941da177e4SLinus Torvalds } 695cd656375SJan Kara /* 69669b6c131SMatthew Wilcox * For DAX we invalidate page tables after invalidating page cache. We 697cd656375SJan Kara * could invalidate page tables while invalidating each entry however 698cd656375SJan Kara * that would be expensive. And doing range unmapping before doesn't 69969b6c131SMatthew Wilcox * work as we have no cheap way to find whether page cache entry didn't 700cd656375SJan Kara * get remapped later. 701cd656375SJan Kara */ 702cd656375SJan Kara if (dax_mapping(mapping)) { 703977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, start, end - start + 1, false); 704cd656375SJan Kara } 70534ccb69eSAndrey Ryabinin out: 7063167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 7071da177e4SLinus Torvalds return ret; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds /** 7121da177e4SLinus Torvalds * invalidate_inode_pages2 - remove all pages from an address_space 71367be2dd1SMartin Waitz * @mapping: the address_space 7141da177e4SLinus Torvalds * 7151da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 7161da177e4SLinus Torvalds * invalidation. 7171da177e4SLinus Torvalds * 718a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 7191da177e4SLinus Torvalds */ 7201da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping) 7211da177e4SLinus Torvalds { 7221da177e4SLinus Torvalds return invalidate_inode_pages2_range(mapping, 0, -1); 7231da177e4SLinus Torvalds } 7241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 72525d9e2d1Snpiggin@suse.de 72625d9e2d1Snpiggin@suse.de /** 72725d9e2d1Snpiggin@suse.de * truncate_pagecache - unmap and remove pagecache that has been truncated 72825d9e2d1Snpiggin@suse.de * @inode: inode 7298a549beaSHugh Dickins * @newsize: new file size 73025d9e2d1Snpiggin@suse.de * 73125d9e2d1Snpiggin@suse.de * inode's new i_size must already be written before truncate_pagecache 73225d9e2d1Snpiggin@suse.de * is called. 73325d9e2d1Snpiggin@suse.de * 73425d9e2d1Snpiggin@suse.de * This function should typically be called before the filesystem 73525d9e2d1Snpiggin@suse.de * releases resources associated with the freed range (eg. deallocates 73625d9e2d1Snpiggin@suse.de * blocks). This way, pagecache will always stay logically coherent 73725d9e2d1Snpiggin@suse.de * with on-disk format, and the filesystem would not have to deal with 73825d9e2d1Snpiggin@suse.de * situations such as writepage being called for a page that has already 73925d9e2d1Snpiggin@suse.de * had its underlying blocks deallocated. 74025d9e2d1Snpiggin@suse.de */ 7417caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize) 74225d9e2d1Snpiggin@suse.de { 74325d9e2d1Snpiggin@suse.de struct address_space *mapping = inode->i_mapping; 7448a549beaSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 74525d9e2d1Snpiggin@suse.de 74625d9e2d1Snpiggin@suse.de /* 74725d9e2d1Snpiggin@suse.de * unmap_mapping_range is called twice, first simply for 74825d9e2d1Snpiggin@suse.de * efficiency so that truncate_inode_pages does fewer 74925d9e2d1Snpiggin@suse.de * single-page unmaps. However after this first call, and 75025d9e2d1Snpiggin@suse.de * before truncate_inode_pages finishes, it is possible for 75125d9e2d1Snpiggin@suse.de * private pages to be COWed, which remain after 75225d9e2d1Snpiggin@suse.de * truncate_inode_pages finishes, hence the second 75325d9e2d1Snpiggin@suse.de * unmap_mapping_range call must be made for correctness. 75425d9e2d1Snpiggin@suse.de */ 7558a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 7568a549beaSHugh Dickins truncate_inode_pages(mapping, newsize); 7578a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 75825d9e2d1Snpiggin@suse.de } 75925d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache); 76025d9e2d1Snpiggin@suse.de 76125d9e2d1Snpiggin@suse.de /** 7622c27c65eSChristoph Hellwig * truncate_setsize - update inode and pagecache for a new file size 7632c27c65eSChristoph Hellwig * @inode: inode 7642c27c65eSChristoph Hellwig * @newsize: new file size 7652c27c65eSChristoph Hellwig * 766382e27daSJan Kara * truncate_setsize updates i_size and performs pagecache truncation (if 767382e27daSJan Kara * necessary) to @newsize. It will be typically be called from the filesystem's 768382e27daSJan Kara * setattr function when ATTR_SIZE is passed in. 7692c27c65eSChristoph Hellwig * 77077783d06SJan Kara * Must be called with a lock serializing truncates and writes (generally 7719608703eSJan Kara * i_rwsem but e.g. xfs uses a different lock) and before all filesystem 77277783d06SJan Kara * specific block truncation has been performed. 7732c27c65eSChristoph Hellwig */ 7742c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize) 7752c27c65eSChristoph Hellwig { 77690a80202SJan Kara loff_t oldsize = inode->i_size; 77790a80202SJan Kara 7782c27c65eSChristoph Hellwig i_size_write(inode, newsize); 77990a80202SJan Kara if (newsize > oldsize) 78090a80202SJan Kara pagecache_isize_extended(inode, oldsize, newsize); 7817caef267SKirill A. Shutemov truncate_pagecache(inode, newsize); 7822c27c65eSChristoph Hellwig } 7832c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize); 7842c27c65eSChristoph Hellwig 7852c27c65eSChristoph Hellwig /** 78690a80202SJan Kara * pagecache_isize_extended - update pagecache after extension of i_size 78790a80202SJan Kara * @inode: inode for which i_size was extended 78890a80202SJan Kara * @from: original inode size 78990a80202SJan Kara * @to: new inode size 79090a80202SJan Kara * 79190a80202SJan Kara * Handle extension of inode size either caused by extending truncate or by 79290a80202SJan Kara * write starting after current i_size. We mark the page straddling current 79390a80202SJan Kara * i_size RO so that page_mkwrite() is called on the nearest write access to 79490a80202SJan Kara * the page. This way filesystem can be sure that page_mkwrite() is called on 79590a80202SJan Kara * the page before user writes to the page via mmap after the i_size has been 79690a80202SJan Kara * changed. 79790a80202SJan Kara * 79890a80202SJan Kara * The function must be called after i_size is updated so that page fault 79990a80202SJan Kara * coming after we unlock the page will already see the new i_size. 8009608703eSJan Kara * The function must be called while we still hold i_rwsem - this not only 80190a80202SJan Kara * makes sure i_size is stable but also that userspace cannot observe new 80290a80202SJan Kara * i_size value before we are prepared to store mmap writes at new inode size. 80390a80202SJan Kara */ 80490a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 80590a80202SJan Kara { 80693407472SFabian Frederick int bsize = i_blocksize(inode); 80790a80202SJan Kara loff_t rounded_from; 80890a80202SJan Kara struct page *page; 80990a80202SJan Kara pgoff_t index; 81090a80202SJan Kara 81190a80202SJan Kara WARN_ON(to > inode->i_size); 81290a80202SJan Kara 81309cbfeafSKirill A. Shutemov if (from >= to || bsize == PAGE_SIZE) 81490a80202SJan Kara return; 81590a80202SJan Kara /* Page straddling @from will not have any hole block created? */ 81690a80202SJan Kara rounded_from = round_up(from, bsize); 81709cbfeafSKirill A. Shutemov if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 81890a80202SJan Kara return; 81990a80202SJan Kara 82009cbfeafSKirill A. Shutemov index = from >> PAGE_SHIFT; 82190a80202SJan Kara page = find_lock_page(inode->i_mapping, index); 82290a80202SJan Kara /* Page not cached? Nothing to do */ 82390a80202SJan Kara if (!page) 82490a80202SJan Kara return; 82590a80202SJan Kara /* 82690a80202SJan Kara * See clear_page_dirty_for_io() for details why set_page_dirty() 82790a80202SJan Kara * is needed. 82890a80202SJan Kara */ 82990a80202SJan Kara if (page_mkclean(page)) 83090a80202SJan Kara set_page_dirty(page); 83190a80202SJan Kara unlock_page(page); 83209cbfeafSKirill A. Shutemov put_page(page); 83390a80202SJan Kara } 83490a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended); 83590a80202SJan Kara 83690a80202SJan Kara /** 837623e3db9SHugh Dickins * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 838623e3db9SHugh Dickins * @inode: inode 839623e3db9SHugh Dickins * @lstart: offset of beginning of hole 840623e3db9SHugh Dickins * @lend: offset of last byte of hole 841623e3db9SHugh Dickins * 842623e3db9SHugh Dickins * This function should typically be called before the filesystem 843623e3db9SHugh Dickins * releases resources associated with the freed range (eg. deallocates 844623e3db9SHugh Dickins * blocks). This way, pagecache will always stay logically coherent 845623e3db9SHugh Dickins * with on-disk format, and the filesystem would not have to deal with 846623e3db9SHugh Dickins * situations such as writepage being called for a page that has already 847623e3db9SHugh Dickins * had its underlying blocks deallocated. 848623e3db9SHugh Dickins */ 849623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 850623e3db9SHugh Dickins { 851623e3db9SHugh Dickins struct address_space *mapping = inode->i_mapping; 852623e3db9SHugh Dickins loff_t unmap_start = round_up(lstart, PAGE_SIZE); 853623e3db9SHugh Dickins loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 854623e3db9SHugh Dickins /* 855623e3db9SHugh Dickins * This rounding is currently just for example: unmap_mapping_range 856623e3db9SHugh Dickins * expands its hole outwards, whereas we want it to contract the hole 857623e3db9SHugh Dickins * inwards. However, existing callers of truncate_pagecache_range are 8585a720394SLukas Czerner * doing their own page rounding first. Note that unmap_mapping_range 8595a720394SLukas Czerner * allows holelen 0 for all, and we allow lend -1 for end of file. 860623e3db9SHugh Dickins */ 861623e3db9SHugh Dickins 862623e3db9SHugh Dickins /* 863623e3db9SHugh Dickins * Unlike in truncate_pagecache, unmap_mapping_range is called only 864623e3db9SHugh Dickins * once (before truncating pagecache), and without "even_cows" flag: 865623e3db9SHugh Dickins * hole-punching should not remove private COWed pages from the hole. 866623e3db9SHugh Dickins */ 867623e3db9SHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 868623e3db9SHugh Dickins unmap_mapping_range(mapping, unmap_start, 869623e3db9SHugh Dickins 1 + unmap_end - unmap_start, 0); 870623e3db9SHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 871623e3db9SHugh Dickins } 872623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range); 873