11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * mm/truncate.c - code for taking down pages from address_spaces 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds 51da177e4SLinus Torvalds * 6e1f8e874SFrancois Cami * 10Sep2002 Andrew Morton 71da177e4SLinus Torvalds * Initial version. 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/kernel.h> 114af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 12f9fe48beSRoss Zwisler #include <linux/dax.h> 135a0e3ad6STejun Heo #include <linux/gfp.h> 141da177e4SLinus Torvalds #include <linux/mm.h> 150fd0e6b0SNick Piggin #include <linux/swap.h> 16b95f1b31SPaul Gortmaker #include <linux/export.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 1801f2705dSNate Diller #include <linux/highmem.h> 191da177e4SLinus Torvalds #include <linux/pagevec.h> 20e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h> 211da177e4SLinus Torvalds #include <linux/buffer_head.h> /* grr. try_to_release_page, 22aaa4059bSJan Kara do_invalidatepage */ 233a4f8a0bSHugh Dickins #include <linux/shmem_fs.h> 24c515e1fdSDan Magenheimer #include <linux/cleancache.h> 2590a80202SJan Kara #include <linux/rmap.h> 26ba470de4SRik van Riel #include "internal.h" 271da177e4SLinus Torvalds 28*f2187599SMel Gorman /* 29*f2187599SMel Gorman * Regular page slots are stabilized by the page lock even without the tree 30*f2187599SMel Gorman * itself locked. These unlocked entries need verification under the tree 31*f2187599SMel Gorman * lock. 32*f2187599SMel Gorman */ 33*f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping, 34*f2187599SMel Gorman pgoff_t index, void *entry) 350cd6144aSJohannes Weiner { 36449dd698SJohannes Weiner struct radix_tree_node *node; 37449dd698SJohannes Weiner void **slot; 38449dd698SJohannes Weiner 3914b46879SJohannes Weiner if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot)) 40*f2187599SMel Gorman return; 41449dd698SJohannes Weiner if (*slot != entry) 42*f2187599SMel Gorman return; 4314b46879SJohannes Weiner __radix_tree_replace(&mapping->page_tree, node, slot, NULL, 44c7df8ad2SMel Gorman workingset_update_node); 45f9fe48beSRoss Zwisler mapping->nrexceptional--; 46*f2187599SMel Gorman } 47*f2187599SMel Gorman 48*f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index, 49*f2187599SMel Gorman void *entry) 50*f2187599SMel Gorman { 51*f2187599SMel Gorman spin_lock_irq(&mapping->tree_lock); 52*f2187599SMel Gorman __clear_shadow_entry(mapping, index, entry); 530cd6144aSJohannes Weiner spin_unlock_irq(&mapping->tree_lock); 540cd6144aSJohannes Weiner } 551da177e4SLinus Torvalds 56c6dcf52cSJan Kara /* 57*f2187599SMel Gorman * Unconditionally remove exceptional entries. Usually called from truncate 58*f2187599SMel Gorman * path. Note that the pagevec may be altered by this function by removing 59*f2187599SMel Gorman * exceptional entries similar to what pagevec_remove_exceptionals does. 60c6dcf52cSJan Kara */ 61*f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping, 62*f2187599SMel Gorman struct pagevec *pvec, pgoff_t *indices, 63*f2187599SMel Gorman pgoff_t end) 64c6dcf52cSJan Kara { 65*f2187599SMel Gorman int i, j; 66*f2187599SMel Gorman bool dax, lock; 67*f2187599SMel Gorman 68c6dcf52cSJan Kara /* Handled by shmem itself */ 69c6dcf52cSJan Kara if (shmem_mapping(mapping)) 70c6dcf52cSJan Kara return; 71c6dcf52cSJan Kara 72*f2187599SMel Gorman for (j = 0; j < pagevec_count(pvec); j++) 73*f2187599SMel Gorman if (radix_tree_exceptional_entry(pvec->pages[j])) 74*f2187599SMel Gorman break; 75*f2187599SMel Gorman 76*f2187599SMel Gorman if (j == pagevec_count(pvec)) 77c6dcf52cSJan Kara return; 78*f2187599SMel Gorman 79*f2187599SMel Gorman dax = dax_mapping(mapping); 80*f2187599SMel Gorman lock = !dax && indices[j] < end; 81*f2187599SMel Gorman if (lock) 82*f2187599SMel Gorman spin_lock_irq(&mapping->tree_lock); 83*f2187599SMel Gorman 84*f2187599SMel Gorman for (i = j; i < pagevec_count(pvec); i++) { 85*f2187599SMel Gorman struct page *page = pvec->pages[i]; 86*f2187599SMel Gorman pgoff_t index = indices[i]; 87*f2187599SMel Gorman 88*f2187599SMel Gorman if (!radix_tree_exceptional_entry(page)) { 89*f2187599SMel Gorman pvec->pages[j++] = page; 90*f2187599SMel Gorman continue; 91c6dcf52cSJan Kara } 92*f2187599SMel Gorman 93*f2187599SMel Gorman if (index >= end) 94*f2187599SMel Gorman continue; 95*f2187599SMel Gorman 96*f2187599SMel Gorman if (unlikely(dax)) { 97*f2187599SMel Gorman dax_delete_mapping_entry(mapping, index); 98*f2187599SMel Gorman continue; 99*f2187599SMel Gorman } 100*f2187599SMel Gorman 101*f2187599SMel Gorman __clear_shadow_entry(mapping, index, page); 102*f2187599SMel Gorman } 103*f2187599SMel Gorman 104*f2187599SMel Gorman if (lock) 105*f2187599SMel Gorman spin_unlock_irq(&mapping->tree_lock); 106*f2187599SMel Gorman pvec->nr = j; 107c6dcf52cSJan Kara } 108c6dcf52cSJan Kara 109c6dcf52cSJan Kara /* 110c6dcf52cSJan Kara * Invalidate exceptional entry if easily possible. This handles exceptional 1114636e70bSRoss Zwisler * entries for invalidate_inode_pages(). 112c6dcf52cSJan Kara */ 113c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping, 114c6dcf52cSJan Kara pgoff_t index, void *entry) 115c6dcf52cSJan Kara { 1164636e70bSRoss Zwisler /* Handled by shmem itself, or for DAX we do nothing. */ 1174636e70bSRoss Zwisler if (shmem_mapping(mapping) || dax_mapping(mapping)) 118c6dcf52cSJan Kara return 1; 119c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 120c6dcf52cSJan Kara return 1; 121c6dcf52cSJan Kara } 122c6dcf52cSJan Kara 123c6dcf52cSJan Kara /* 124c6dcf52cSJan Kara * Invalidate exceptional entry if clean. This handles exceptional entries for 125c6dcf52cSJan Kara * invalidate_inode_pages2() so for DAX it evicts only clean entries. 126c6dcf52cSJan Kara */ 127c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping, 128c6dcf52cSJan Kara pgoff_t index, void *entry) 129c6dcf52cSJan Kara { 130c6dcf52cSJan Kara /* Handled by shmem itself */ 131c6dcf52cSJan Kara if (shmem_mapping(mapping)) 132c6dcf52cSJan Kara return 1; 133c6dcf52cSJan Kara if (dax_mapping(mapping)) 134c6dcf52cSJan Kara return dax_invalidate_mapping_entry_sync(mapping, index); 135c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 136c6dcf52cSJan Kara return 1; 137c6dcf52cSJan Kara } 138c6dcf52cSJan Kara 139cf9a2ae8SDavid Howells /** 14028bc44d7SFengguang Wu * do_invalidatepage - invalidate part or all of a page 141cf9a2ae8SDavid Howells * @page: the page which is affected 142d47992f8SLukas Czerner * @offset: start of the range to invalidate 143d47992f8SLukas Czerner * @length: length of the range to invalidate 144cf9a2ae8SDavid Howells * 145cf9a2ae8SDavid Howells * do_invalidatepage() is called when all or part of the page has become 146cf9a2ae8SDavid Howells * invalidated by a truncate operation. 147cf9a2ae8SDavid Howells * 148cf9a2ae8SDavid Howells * do_invalidatepage() does not have to release all buffers, but it must 149cf9a2ae8SDavid Howells * ensure that no dirty buffer is left outside @offset and that no I/O 150cf9a2ae8SDavid Howells * is underway against any of the blocks which are outside the truncation 151cf9a2ae8SDavid Howells * point. Because the caller is about to free (and possibly reuse) those 152cf9a2ae8SDavid Howells * blocks on-disk. 153cf9a2ae8SDavid Howells */ 154d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset, 155d47992f8SLukas Czerner unsigned int length) 156cf9a2ae8SDavid Howells { 157d47992f8SLukas Czerner void (*invalidatepage)(struct page *, unsigned int, unsigned int); 158d47992f8SLukas Czerner 159cf9a2ae8SDavid Howells invalidatepage = page->mapping->a_ops->invalidatepage; 1609361401eSDavid Howells #ifdef CONFIG_BLOCK 161cf9a2ae8SDavid Howells if (!invalidatepage) 162cf9a2ae8SDavid Howells invalidatepage = block_invalidatepage; 1639361401eSDavid Howells #endif 164cf9a2ae8SDavid Howells if (invalidatepage) 165d47992f8SLukas Czerner (*invalidatepage)(page, offset, length); 166cf9a2ae8SDavid Howells } 167cf9a2ae8SDavid Howells 168ecdfc978SLinus Torvalds /* 1691da177e4SLinus Torvalds * If truncate cannot remove the fs-private metadata from the page, the page 17062e1c553SShaohua Li * becomes orphaned. It will be left on the LRU and may even be mapped into 17154cb8821SNick Piggin * user pagetables if we're racing with filemap_fault(). 1721da177e4SLinus Torvalds * 1731da177e4SLinus Torvalds * We need to bale out if page->mapping is no longer equal to the original 1741da177e4SLinus Torvalds * mapping. This happens a) when the VM reclaimed the page while we waited on 175fc0ecff6SAndrew Morton * its lock, b) when a concurrent invalidate_mapping_pages got there first and 1761da177e4SLinus Torvalds * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 1771da177e4SLinus Torvalds */ 1789f4e41f4SJan Kara static void 1799f4e41f4SJan Kara truncate_cleanup_page(struct address_space *mapping, struct page *page) 1801da177e4SLinus Torvalds { 1819f4e41f4SJan Kara if (page_mapped(page)) { 1829f4e41f4SJan Kara loff_t holelen; 1839f4e41f4SJan Kara 1849f4e41f4SJan Kara holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE; 1859f4e41f4SJan Kara unmap_mapping_range(mapping, 1869f4e41f4SJan Kara (loff_t)page->index << PAGE_SHIFT, 1879f4e41f4SJan Kara holelen, 0); 1889f4e41f4SJan Kara } 1891da177e4SLinus Torvalds 190266cf658SDavid Howells if (page_has_private(page)) 19109cbfeafSKirill A. Shutemov do_invalidatepage(page, 0, PAGE_SIZE); 1921da177e4SLinus Torvalds 193b9ea2515SKonstantin Khlebnikov /* 194b9ea2515SKonstantin Khlebnikov * Some filesystems seem to re-dirty the page even after 195b9ea2515SKonstantin Khlebnikov * the VM has canceled the dirty bit (eg ext3 journaling). 196b9ea2515SKonstantin Khlebnikov * Hence dirty accounting check is placed after invalidation. 197b9ea2515SKonstantin Khlebnikov */ 19811f81becSTejun Heo cancel_dirty_page(page); 1991da177e4SLinus Torvalds ClearPageMappedToDisk(page); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds /* 203fc0ecff6SAndrew Morton * This is for invalidate_mapping_pages(). That function can be called at 2041da177e4SLinus Torvalds * any time, and is not supposed to throw away dirty pages. But pages can 2050fd0e6b0SNick Piggin * be marked dirty at any time too, so use remove_mapping which safely 2060fd0e6b0SNick Piggin * discards clean, unused pages. 2071da177e4SLinus Torvalds * 2081da177e4SLinus Torvalds * Returns non-zero if the page was successfully invalidated. 2091da177e4SLinus Torvalds */ 2101da177e4SLinus Torvalds static int 2111da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page) 2121da177e4SLinus Torvalds { 2130fd0e6b0SNick Piggin int ret; 2140fd0e6b0SNick Piggin 2151da177e4SLinus Torvalds if (page->mapping != mapping) 2161da177e4SLinus Torvalds return 0; 2171da177e4SLinus Torvalds 218266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, 0)) 2191da177e4SLinus Torvalds return 0; 2201da177e4SLinus Torvalds 2210fd0e6b0SNick Piggin ret = remove_mapping(mapping, page); 2220fd0e6b0SNick Piggin 2230fd0e6b0SNick Piggin return ret; 2241da177e4SLinus Torvalds } 2251da177e4SLinus Torvalds 226750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page) 227750b4987SNick Piggin { 228fc127da0SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 229fc127da0SKirill A. Shutemov 2309f4e41f4SJan Kara if (page->mapping != mapping) 2319f4e41f4SJan Kara return -EIO; 2329f4e41f4SJan Kara 2339f4e41f4SJan Kara truncate_cleanup_page(mapping, page); 2349f4e41f4SJan Kara delete_from_page_cache(page); 2359f4e41f4SJan Kara return 0; 236750b4987SNick Piggin } 237750b4987SNick Piggin 23883f78668SWu Fengguang /* 23925718736SAndi Kleen * Used to get rid of pages on hardware memory corruption. 24025718736SAndi Kleen */ 24125718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page) 24225718736SAndi Kleen { 24325718736SAndi Kleen if (!mapping) 24425718736SAndi Kleen return -EINVAL; 24525718736SAndi Kleen /* 24625718736SAndi Kleen * Only punch for normal data pages for now. 24725718736SAndi Kleen * Handling other types like directories would need more auditing. 24825718736SAndi Kleen */ 24925718736SAndi Kleen if (!S_ISREG(mapping->host->i_mode)) 25025718736SAndi Kleen return -EIO; 25125718736SAndi Kleen return truncate_inode_page(mapping, page); 25225718736SAndi Kleen } 25325718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page); 25425718736SAndi Kleen 25525718736SAndi Kleen /* 25683f78668SWu Fengguang * Safely invalidate one page from its pagecache mapping. 25783f78668SWu Fengguang * It only drops clean, unused pages. The page must be locked. 25883f78668SWu Fengguang * 25983f78668SWu Fengguang * Returns 1 if the page is successfully invalidated, otherwise 0. 26083f78668SWu Fengguang */ 26183f78668SWu Fengguang int invalidate_inode_page(struct page *page) 26283f78668SWu Fengguang { 26383f78668SWu Fengguang struct address_space *mapping = page_mapping(page); 26483f78668SWu Fengguang if (!mapping) 26583f78668SWu Fengguang return 0; 26683f78668SWu Fengguang if (PageDirty(page) || PageWriteback(page)) 26783f78668SWu Fengguang return 0; 26883f78668SWu Fengguang if (page_mapped(page)) 26983f78668SWu Fengguang return 0; 27083f78668SWu Fengguang return invalidate_complete_page(mapping, page); 27183f78668SWu Fengguang } 27283f78668SWu Fengguang 2731da177e4SLinus Torvalds /** 27473c1e204SLiu Bo * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 2751da177e4SLinus Torvalds * @mapping: mapping to truncate 2761da177e4SLinus Torvalds * @lstart: offset from which to truncate 2775a720394SLukas Czerner * @lend: offset to which to truncate (inclusive) 2781da177e4SLinus Torvalds * 279d7339071SHans Reiser * Truncate the page cache, removing the pages that are between 2805a720394SLukas Czerner * specified offsets (and zeroing out partial pages 2815a720394SLukas Czerner * if lstart or lend + 1 is not page aligned). 2821da177e4SLinus Torvalds * 2831da177e4SLinus Torvalds * Truncate takes two passes - the first pass is nonblocking. It will not 2841da177e4SLinus Torvalds * block on page locks and it will not block on writeback. The second pass 2851da177e4SLinus Torvalds * will wait. This is to prevent as much IO as possible in the affected region. 2861da177e4SLinus Torvalds * The first pass will remove most pages, so the search cost of the second pass 2871da177e4SLinus Torvalds * is low. 2881da177e4SLinus Torvalds * 2891da177e4SLinus Torvalds * We pass down the cache-hot hint to the page freeing code. Even if the 2901da177e4SLinus Torvalds * mapping is large, it is probably the case that the final pages are the most 2911da177e4SLinus Torvalds * recently touched, and freeing happens in ascending file offset order. 2925a720394SLukas Czerner * 2935a720394SLukas Czerner * Note that since ->invalidatepage() accepts range to invalidate 2945a720394SLukas Czerner * truncate_inode_pages_range is able to handle cases where lend + 1 is not 2955a720394SLukas Czerner * page aligned properly. 2961da177e4SLinus Torvalds */ 297d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping, 298d7339071SHans Reiser loff_t lstart, loff_t lend) 2991da177e4SLinus Torvalds { 3005a720394SLukas Czerner pgoff_t start; /* inclusive */ 3015a720394SLukas Czerner pgoff_t end; /* exclusive */ 3025a720394SLukas Czerner unsigned int partial_start; /* inclusive */ 3035a720394SLukas Czerner unsigned int partial_end; /* exclusive */ 3041da177e4SLinus Torvalds struct pagevec pvec; 3050cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 306b85e0effSHugh Dickins pgoff_t index; 3071da177e4SLinus Torvalds int i; 3081da177e4SLinus Torvalds 309f9fe48beSRoss Zwisler if (mapping->nrpages == 0 && mapping->nrexceptional == 0) 31034ccb69eSAndrey Ryabinin goto out; 3111da177e4SLinus Torvalds 3125a720394SLukas Czerner /* Offsets within partial pages */ 31309cbfeafSKirill A. Shutemov partial_start = lstart & (PAGE_SIZE - 1); 31409cbfeafSKirill A. Shutemov partial_end = (lend + 1) & (PAGE_SIZE - 1); 3155a720394SLukas Czerner 3165a720394SLukas Czerner /* 3175a720394SLukas Czerner * 'start' and 'end' always covers the range of pages to be fully 3185a720394SLukas Czerner * truncated. Partial pages are covered with 'partial_start' at the 3195a720394SLukas Czerner * start of the range and 'partial_end' at the end of the range. 3205a720394SLukas Czerner * Note that 'end' is exclusive while 'lend' is inclusive. 3215a720394SLukas Czerner */ 32209cbfeafSKirill A. Shutemov start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 3235a720394SLukas Czerner if (lend == -1) 3245a720394SLukas Czerner /* 3255a720394SLukas Czerner * lend == -1 indicates end-of-file so we have to set 'end' 3265a720394SLukas Czerner * to the highest possible pgoff_t and since the type is 3275a720394SLukas Czerner * unsigned we're using -1. 3285a720394SLukas Czerner */ 3295a720394SLukas Czerner end = -1; 3305a720394SLukas Czerner else 33109cbfeafSKirill A. Shutemov end = (lend + 1) >> PAGE_SHIFT; 332d7339071SHans Reiser 3331da177e4SLinus Torvalds pagevec_init(&pvec, 0); 334b85e0effSHugh Dickins index = start; 3350cd6144aSJohannes Weiner while (index < end && pagevec_lookup_entries(&pvec, mapping, index, 3360cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE), 3370cd6144aSJohannes Weiner indices)) { 338aa65c29cSJan Kara /* 339aa65c29cSJan Kara * Pagevec array has exceptional entries and we may also fail 340aa65c29cSJan Kara * to lock some pages. So we store pages that can be deleted 341aa65c29cSJan Kara * in a new pagevec. 342aa65c29cSJan Kara */ 343aa65c29cSJan Kara struct pagevec locked_pvec; 344aa65c29cSJan Kara 345aa65c29cSJan Kara pagevec_init(&locked_pvec, 0); 3461da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 3471da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 3481da177e4SLinus Torvalds 349b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 3500cd6144aSJohannes Weiner index = indices[i]; 3515a720394SLukas Czerner if (index >= end) 352d7339071SHans Reiser break; 353d7339071SHans Reiser 354*f2187599SMel Gorman if (radix_tree_exceptional_entry(page)) 3550cd6144aSJohannes Weiner continue; 3560cd6144aSJohannes Weiner 357529ae9aaSNick Piggin if (!trylock_page(page)) 3581da177e4SLinus Torvalds continue; 3595cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 3601da177e4SLinus Torvalds if (PageWriteback(page)) { 3611da177e4SLinus Torvalds unlock_page(page); 3621da177e4SLinus Torvalds continue; 3631da177e4SLinus Torvalds } 364aa65c29cSJan Kara if (page->mapping != mapping) { 3651da177e4SLinus Torvalds unlock_page(page); 366aa65c29cSJan Kara continue; 3671da177e4SLinus Torvalds } 368aa65c29cSJan Kara pagevec_add(&locked_pvec, page); 369aa65c29cSJan Kara } 370aa65c29cSJan Kara for (i = 0; i < pagevec_count(&locked_pvec); i++) 371aa65c29cSJan Kara truncate_cleanup_page(mapping, locked_pvec.pages[i]); 372aa65c29cSJan Kara delete_from_page_cache_batch(mapping, &locked_pvec); 373aa65c29cSJan Kara for (i = 0; i < pagevec_count(&locked_pvec); i++) 374aa65c29cSJan Kara unlock_page(locked_pvec.pages[i]); 375*f2187599SMel Gorman truncate_exceptional_pvec_entries(mapping, &pvec, indices, end); 3761da177e4SLinus Torvalds pagevec_release(&pvec); 3771da177e4SLinus Torvalds cond_resched(); 378b85e0effSHugh Dickins index++; 3791da177e4SLinus Torvalds } 3805a720394SLukas Czerner if (partial_start) { 3811da177e4SLinus Torvalds struct page *page = find_lock_page(mapping, start - 1); 3821da177e4SLinus Torvalds if (page) { 38309cbfeafSKirill A. Shutemov unsigned int top = PAGE_SIZE; 3845a720394SLukas Czerner if (start > end) { 3855a720394SLukas Czerner /* Truncation within a single page */ 3865a720394SLukas Czerner top = partial_end; 3875a720394SLukas Czerner partial_end = 0; 3885a720394SLukas Czerner } 3891da177e4SLinus Torvalds wait_on_page_writeback(page); 3905a720394SLukas Czerner zero_user_segment(page, partial_start, top); 3915a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 3925a720394SLukas Czerner if (page_has_private(page)) 3935a720394SLukas Czerner do_invalidatepage(page, partial_start, 3945a720394SLukas Czerner top - partial_start); 3951da177e4SLinus Torvalds unlock_page(page); 39609cbfeafSKirill A. Shutemov put_page(page); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 3995a720394SLukas Czerner if (partial_end) { 4005a720394SLukas Czerner struct page *page = find_lock_page(mapping, end); 4015a720394SLukas Czerner if (page) { 4025a720394SLukas Czerner wait_on_page_writeback(page); 4035a720394SLukas Czerner zero_user_segment(page, 0, partial_end); 4045a720394SLukas Czerner cleancache_invalidate_page(mapping, page); 4055a720394SLukas Czerner if (page_has_private(page)) 4065a720394SLukas Czerner do_invalidatepage(page, 0, 4075a720394SLukas Czerner partial_end); 4085a720394SLukas Czerner unlock_page(page); 40909cbfeafSKirill A. Shutemov put_page(page); 4105a720394SLukas Czerner } 4115a720394SLukas Czerner } 4125a720394SLukas Czerner /* 4135a720394SLukas Czerner * If the truncation happened within a single page no pages 4145a720394SLukas Czerner * will be released, just zeroed, so we can bail out now. 4155a720394SLukas Czerner */ 4165a720394SLukas Czerner if (start >= end) 41734ccb69eSAndrey Ryabinin goto out; 4181da177e4SLinus Torvalds 419b85e0effSHugh Dickins index = start; 4201da177e4SLinus Torvalds for ( ; ; ) { 4211da177e4SLinus Torvalds cond_resched(); 4220cd6144aSJohannes Weiner if (!pagevec_lookup_entries(&pvec, mapping, index, 423792ceaefSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) { 424792ceaefSHugh Dickins /* If all gone from start onwards, we're done */ 425b85e0effSHugh Dickins if (index == start) 4261da177e4SLinus Torvalds break; 427792ceaefSHugh Dickins /* Otherwise restart to make sure all gone */ 428b85e0effSHugh Dickins index = start; 4291da177e4SLinus Torvalds continue; 4301da177e4SLinus Torvalds } 4310cd6144aSJohannes Weiner if (index == start && indices[0] >= end) { 432792ceaefSHugh Dickins /* All gone out of hole to be punched, we're done */ 4330cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 434d7339071SHans Reiser pagevec_release(&pvec); 435d7339071SHans Reiser break; 436d7339071SHans Reiser } 437*f2187599SMel Gorman 4381da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 4391da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 4401da177e4SLinus Torvalds 441b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4420cd6144aSJohannes Weiner index = indices[i]; 443792ceaefSHugh Dickins if (index >= end) { 444792ceaefSHugh Dickins /* Restart punch to make sure all gone */ 445792ceaefSHugh Dickins index = start - 1; 446d7339071SHans Reiser break; 447792ceaefSHugh Dickins } 448b85e0effSHugh Dickins 449*f2187599SMel Gorman if (radix_tree_exceptional_entry(page)) 4500cd6144aSJohannes Weiner continue; 4510cd6144aSJohannes Weiner 4521da177e4SLinus Torvalds lock_page(page); 4535cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 4541da177e4SLinus Torvalds wait_on_page_writeback(page); 455750b4987SNick Piggin truncate_inode_page(mapping, page); 4561da177e4SLinus Torvalds unlock_page(page); 4571da177e4SLinus Torvalds } 458*f2187599SMel Gorman truncate_exceptional_pvec_entries(mapping, &pvec, indices, end); 4591da177e4SLinus Torvalds pagevec_release(&pvec); 460b85e0effSHugh Dickins index++; 4611da177e4SLinus Torvalds } 46234ccb69eSAndrey Ryabinin 46334ccb69eSAndrey Ryabinin out: 4643167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 4651da177e4SLinus Torvalds } 466d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range); 4671da177e4SLinus Torvalds 468d7339071SHans Reiser /** 469d7339071SHans Reiser * truncate_inode_pages - truncate *all* the pages from an offset 470d7339071SHans Reiser * @mapping: mapping to truncate 471d7339071SHans Reiser * @lstart: offset from which to truncate 472d7339071SHans Reiser * 4731b1dcc1bSJes Sorensen * Called under (and serialised by) inode->i_mutex. 47408142579SJan Kara * 47508142579SJan Kara * Note: When this function returns, there can be a page in the process of 47608142579SJan Kara * deletion (inside __delete_from_page_cache()) in the specified range. Thus 47708142579SJan Kara * mapping->nrpages can be non-zero when this function returns even after 47808142579SJan Kara * truncation of the whole mapping. 479d7339071SHans Reiser */ 480d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 481d7339071SHans Reiser { 482d7339071SHans Reiser truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 483d7339071SHans Reiser } 4841da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages); 4851da177e4SLinus Torvalds 48628697355SMike Waychison /** 48791b0abe3SJohannes Weiner * truncate_inode_pages_final - truncate *all* pages before inode dies 48891b0abe3SJohannes Weiner * @mapping: mapping to truncate 48991b0abe3SJohannes Weiner * 49091b0abe3SJohannes Weiner * Called under (and serialized by) inode->i_mutex. 49191b0abe3SJohannes Weiner * 49291b0abe3SJohannes Weiner * Filesystems have to use this in the .evict_inode path to inform the 49391b0abe3SJohannes Weiner * VM that this is the final truncate and the inode is going away. 49491b0abe3SJohannes Weiner */ 49591b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping) 49691b0abe3SJohannes Weiner { 497f9fe48beSRoss Zwisler unsigned long nrexceptional; 49891b0abe3SJohannes Weiner unsigned long nrpages; 49991b0abe3SJohannes Weiner 50091b0abe3SJohannes Weiner /* 50191b0abe3SJohannes Weiner * Page reclaim can not participate in regular inode lifetime 50291b0abe3SJohannes Weiner * management (can't call iput()) and thus can race with the 50391b0abe3SJohannes Weiner * inode teardown. Tell it when the address space is exiting, 50491b0abe3SJohannes Weiner * so that it does not install eviction information after the 50591b0abe3SJohannes Weiner * final truncate has begun. 50691b0abe3SJohannes Weiner */ 50791b0abe3SJohannes Weiner mapping_set_exiting(mapping); 50891b0abe3SJohannes Weiner 50991b0abe3SJohannes Weiner /* 51091b0abe3SJohannes Weiner * When reclaim installs eviction entries, it increases 511f9fe48beSRoss Zwisler * nrexceptional first, then decreases nrpages. Make sure we see 51291b0abe3SJohannes Weiner * this in the right order or we might miss an entry. 51391b0abe3SJohannes Weiner */ 51491b0abe3SJohannes Weiner nrpages = mapping->nrpages; 51591b0abe3SJohannes Weiner smp_rmb(); 516f9fe48beSRoss Zwisler nrexceptional = mapping->nrexceptional; 51791b0abe3SJohannes Weiner 518f9fe48beSRoss Zwisler if (nrpages || nrexceptional) { 51991b0abe3SJohannes Weiner /* 52091b0abe3SJohannes Weiner * As truncation uses a lockless tree lookup, cycle 52191b0abe3SJohannes Weiner * the tree lock to make sure any ongoing tree 52291b0abe3SJohannes Weiner * modification that does not see AS_EXITING is 52391b0abe3SJohannes Weiner * completed before starting the final truncate. 52491b0abe3SJohannes Weiner */ 52591b0abe3SJohannes Weiner spin_lock_irq(&mapping->tree_lock); 52691b0abe3SJohannes Weiner spin_unlock_irq(&mapping->tree_lock); 52791b0abe3SJohannes Weiner 52891b0abe3SJohannes Weiner truncate_inode_pages(mapping, 0); 52991b0abe3SJohannes Weiner } 53091b0abe3SJohannes Weiner } 53191b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final); 53291b0abe3SJohannes Weiner 53391b0abe3SJohannes Weiner /** 53428697355SMike Waychison * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode 53528697355SMike Waychison * @mapping: the address_space which holds the pages to invalidate 53628697355SMike Waychison * @start: the offset 'from' which to invalidate 53728697355SMike Waychison * @end: the offset 'to' which to invalidate (inclusive) 53828697355SMike Waychison * 53928697355SMike Waychison * This function only removes the unlocked pages, if you want to 54028697355SMike Waychison * remove all the pages of one inode, you must call truncate_inode_pages. 54128697355SMike Waychison * 54228697355SMike Waychison * invalidate_mapping_pages() will not block on IO activity. It will not 54328697355SMike Waychison * invalidate pages which are dirty, locked, under writeback or mapped into 54428697355SMike Waychison * pagetables. 54528697355SMike Waychison */ 54628697355SMike Waychison unsigned long invalidate_mapping_pages(struct address_space *mapping, 54728697355SMike Waychison pgoff_t start, pgoff_t end) 5481da177e4SLinus Torvalds { 5490cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 5501da177e4SLinus Torvalds struct pagevec pvec; 551b85e0effSHugh Dickins pgoff_t index = start; 55231560180SMinchan Kim unsigned long ret; 55331560180SMinchan Kim unsigned long count = 0; 5541da177e4SLinus Torvalds int i; 5551da177e4SLinus Torvalds 5561da177e4SLinus Torvalds pagevec_init(&pvec, 0); 5570cd6144aSJohannes Weiner while (index <= end && pagevec_lookup_entries(&pvec, mapping, index, 5580cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 5590cd6144aSJohannes Weiner indices)) { 5601da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 5611da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 5621da177e4SLinus Torvalds 563b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 5640cd6144aSJohannes Weiner index = indices[i]; 565b85e0effSHugh Dickins if (index > end) 566b85e0effSHugh Dickins break; 567e0f23603SNeilBrown 5680cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 569c6dcf52cSJan Kara invalidate_exceptional_entry(mapping, index, 570c6dcf52cSJan Kara page); 5710cd6144aSJohannes Weiner continue; 5720cd6144aSJohannes Weiner } 5730cd6144aSJohannes Weiner 574b85e0effSHugh Dickins if (!trylock_page(page)) 575b85e0effSHugh Dickins continue; 576fc127da0SKirill A. Shutemov 5775cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 578fc127da0SKirill A. Shutemov 579fc127da0SKirill A. Shutemov /* Middle of THP: skip */ 580fc127da0SKirill A. Shutemov if (PageTransTail(page)) { 581fc127da0SKirill A. Shutemov unlock_page(page); 582fc127da0SKirill A. Shutemov continue; 583fc127da0SKirill A. Shutemov } else if (PageTransHuge(page)) { 584fc127da0SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 585fc127da0SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 58676b6f9b7SJan Kara /* 58776b6f9b7SJan Kara * 'end' is in the middle of THP. Don't 58876b6f9b7SJan Kara * invalidate the page as the part outside of 58976b6f9b7SJan Kara * 'end' could be still useful. 59076b6f9b7SJan Kara */ 59176b6f9b7SJan Kara if (index > end) { 59276b6f9b7SJan Kara unlock_page(page); 593fc127da0SKirill A. Shutemov continue; 594fc127da0SKirill A. Shutemov } 59576b6f9b7SJan Kara } 596fc127da0SKirill A. Shutemov 59731560180SMinchan Kim ret = invalidate_inode_page(page); 5981da177e4SLinus Torvalds unlock_page(page); 59931560180SMinchan Kim /* 60031560180SMinchan Kim * Invalidation is a hint that the page is no longer 60131560180SMinchan Kim * of interest and try to speed up its reclaim. 60231560180SMinchan Kim */ 60331560180SMinchan Kim if (!ret) 604cc5993bdSMinchan Kim deactivate_file_page(page); 60531560180SMinchan Kim count += ret; 6061da177e4SLinus Torvalds } 6070cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 6081da177e4SLinus Torvalds pagevec_release(&pvec); 609fc9a07e7SAndrew Morton cond_resched(); 610b85e0effSHugh Dickins index++; 6111da177e4SLinus Torvalds } 61231560180SMinchan Kim return count; 6131da177e4SLinus Torvalds } 61454bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages); 6151da177e4SLinus Torvalds 616bd4c8ce4SAndrew Morton /* 617bd4c8ce4SAndrew Morton * This is like invalidate_complete_page(), except it ignores the page's 618bd4c8ce4SAndrew Morton * refcount. We do this because invalidate_inode_pages2() needs stronger 619bd4c8ce4SAndrew Morton * invalidation guarantees, and cannot afford to leave pages behind because 6202706a1b8SAnderson Briglia * shrink_page_list() has a temp ref on them, or because they're transiently 6212706a1b8SAnderson Briglia * sitting in the lru_cache_add() pagevecs. 622bd4c8ce4SAndrew Morton */ 623bd4c8ce4SAndrew Morton static int 624bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page) 625bd4c8ce4SAndrew Morton { 626c4843a75SGreg Thelen unsigned long flags; 627c4843a75SGreg Thelen 628bd4c8ce4SAndrew Morton if (page->mapping != mapping) 629bd4c8ce4SAndrew Morton return 0; 630bd4c8ce4SAndrew Morton 631266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) 632bd4c8ce4SAndrew Morton return 0; 633bd4c8ce4SAndrew Morton 634c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 635bd4c8ce4SAndrew Morton if (PageDirty(page)) 636bd4c8ce4SAndrew Morton goto failed; 637bd4c8ce4SAndrew Morton 638266cf658SDavid Howells BUG_ON(page_has_private(page)); 63962cccb8cSJohannes Weiner __delete_from_page_cache(page, NULL); 640c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 6416072d13cSLinus Torvalds 6426072d13cSLinus Torvalds if (mapping->a_ops->freepage) 6436072d13cSLinus Torvalds mapping->a_ops->freepage(page); 6446072d13cSLinus Torvalds 64509cbfeafSKirill A. Shutemov put_page(page); /* pagecache ref */ 646bd4c8ce4SAndrew Morton return 1; 647bd4c8ce4SAndrew Morton failed: 648c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 649bd4c8ce4SAndrew Morton return 0; 650bd4c8ce4SAndrew Morton } 651bd4c8ce4SAndrew Morton 652e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page) 653e3db7691STrond Myklebust { 654e3db7691STrond Myklebust if (!PageDirty(page)) 655e3db7691STrond Myklebust return 0; 656e3db7691STrond Myklebust if (page->mapping != mapping || mapping->a_ops->launder_page == NULL) 657e3db7691STrond Myklebust return 0; 658e3db7691STrond Myklebust return mapping->a_ops->launder_page(page); 659e3db7691STrond Myklebust } 660e3db7691STrond Myklebust 6611da177e4SLinus Torvalds /** 6621da177e4SLinus Torvalds * invalidate_inode_pages2_range - remove range of pages from an address_space 66367be2dd1SMartin Waitz * @mapping: the address_space 6641da177e4SLinus Torvalds * @start: the page offset 'from' which to invalidate 6651da177e4SLinus Torvalds * @end: the page offset 'to' which to invalidate (inclusive) 6661da177e4SLinus Torvalds * 6671da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6681da177e4SLinus Torvalds * invalidation. 6691da177e4SLinus Torvalds * 6706ccfa806SHisashi Hifumi * Returns -EBUSY if any pages could not be invalidated. 6711da177e4SLinus Torvalds */ 6721da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping, 6731da177e4SLinus Torvalds pgoff_t start, pgoff_t end) 6741da177e4SLinus Torvalds { 6750cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 6761da177e4SLinus Torvalds struct pagevec pvec; 677b85e0effSHugh Dickins pgoff_t index; 6781da177e4SLinus Torvalds int i; 6791da177e4SLinus Torvalds int ret = 0; 6800dd1334fSHisashi Hifumi int ret2 = 0; 6811da177e4SLinus Torvalds int did_range_unmap = 0; 6821da177e4SLinus Torvalds 68332691f0fSAndrey Ryabinin if (mapping->nrpages == 0 && mapping->nrexceptional == 0) 68434ccb69eSAndrey Ryabinin goto out; 68532691f0fSAndrey Ryabinin 6861da177e4SLinus Torvalds pagevec_init(&pvec, 0); 687b85e0effSHugh Dickins index = start; 6880cd6144aSJohannes Weiner while (index <= end && pagevec_lookup_entries(&pvec, mapping, index, 6890cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 6900cd6144aSJohannes Weiner indices)) { 6917b965e08STrond Myklebust for (i = 0; i < pagevec_count(&pvec); i++) { 6921da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 693b85e0effSHugh Dickins 694b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 6950cd6144aSJohannes Weiner index = indices[i]; 696b85e0effSHugh Dickins if (index > end) 697b85e0effSHugh Dickins break; 6981da177e4SLinus Torvalds 6990cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 700c6dcf52cSJan Kara if (!invalidate_exceptional_entry2(mapping, 701c6dcf52cSJan Kara index, page)) 702c6dcf52cSJan Kara ret = -EBUSY; 7030cd6144aSJohannes Weiner continue; 7040cd6144aSJohannes Weiner } 7050cd6144aSJohannes Weiner 7061da177e4SLinus Torvalds lock_page(page); 7075cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 7081da177e4SLinus Torvalds if (page->mapping != mapping) { 7091da177e4SLinus Torvalds unlock_page(page); 7101da177e4SLinus Torvalds continue; 7111da177e4SLinus Torvalds } 7121da177e4SLinus Torvalds wait_on_page_writeback(page); 713d00806b1SNick Piggin if (page_mapped(page)) { 7141da177e4SLinus Torvalds if (!did_range_unmap) { 7151da177e4SLinus Torvalds /* 7161da177e4SLinus Torvalds * Zap the rest of the file in one hit. 7171da177e4SLinus Torvalds */ 7181da177e4SLinus Torvalds unmap_mapping_range(mapping, 71909cbfeafSKirill A. Shutemov (loff_t)index << PAGE_SHIFT, 720b85e0effSHugh Dickins (loff_t)(1 + end - index) 72109cbfeafSKirill A. Shutemov << PAGE_SHIFT, 7221da177e4SLinus Torvalds 0); 7231da177e4SLinus Torvalds did_range_unmap = 1; 7241da177e4SLinus Torvalds } else { 7251da177e4SLinus Torvalds /* 7261da177e4SLinus Torvalds * Just zap this page 7271da177e4SLinus Torvalds */ 7281da177e4SLinus Torvalds unmap_mapping_range(mapping, 72909cbfeafSKirill A. Shutemov (loff_t)index << PAGE_SHIFT, 73009cbfeafSKirill A. Shutemov PAGE_SIZE, 0); 7311da177e4SLinus Torvalds } 7321da177e4SLinus Torvalds } 733d00806b1SNick Piggin BUG_ON(page_mapped(page)); 7340dd1334fSHisashi Hifumi ret2 = do_launder_page(mapping, page); 7350dd1334fSHisashi Hifumi if (ret2 == 0) { 7360dd1334fSHisashi Hifumi if (!invalidate_complete_page2(mapping, page)) 7376ccfa806SHisashi Hifumi ret2 = -EBUSY; 7380dd1334fSHisashi Hifumi } 7390dd1334fSHisashi Hifumi if (ret2 < 0) 7400dd1334fSHisashi Hifumi ret = ret2; 7411da177e4SLinus Torvalds unlock_page(page); 7421da177e4SLinus Torvalds } 7430cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 7441da177e4SLinus Torvalds pagevec_release(&pvec); 7451da177e4SLinus Torvalds cond_resched(); 746b85e0effSHugh Dickins index++; 7471da177e4SLinus Torvalds } 748cd656375SJan Kara /* 749cd656375SJan Kara * For DAX we invalidate page tables after invalidating radix tree. We 750cd656375SJan Kara * could invalidate page tables while invalidating each entry however 751cd656375SJan Kara * that would be expensive. And doing range unmapping before doesn't 752cd656375SJan Kara * work as we have no cheap way to find whether radix tree entry didn't 753cd656375SJan Kara * get remapped later. 754cd656375SJan Kara */ 755cd656375SJan Kara if (dax_mapping(mapping)) { 756cd656375SJan Kara unmap_mapping_range(mapping, (loff_t)start << PAGE_SHIFT, 757cd656375SJan Kara (loff_t)(end - start + 1) << PAGE_SHIFT, 0); 758cd656375SJan Kara } 75934ccb69eSAndrey Ryabinin out: 7603167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 7611da177e4SLinus Torvalds return ret; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds /** 7661da177e4SLinus Torvalds * invalidate_inode_pages2 - remove all pages from an address_space 76767be2dd1SMartin Waitz * @mapping: the address_space 7681da177e4SLinus Torvalds * 7691da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 7701da177e4SLinus Torvalds * invalidation. 7711da177e4SLinus Torvalds * 772e9de25ddSPeng Tao * Returns -EBUSY if any pages could not be invalidated. 7731da177e4SLinus Torvalds */ 7741da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping) 7751da177e4SLinus Torvalds { 7761da177e4SLinus Torvalds return invalidate_inode_pages2_range(mapping, 0, -1); 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 77925d9e2d1Snpiggin@suse.de 78025d9e2d1Snpiggin@suse.de /** 78125d9e2d1Snpiggin@suse.de * truncate_pagecache - unmap and remove pagecache that has been truncated 78225d9e2d1Snpiggin@suse.de * @inode: inode 7838a549beaSHugh Dickins * @newsize: new file size 78425d9e2d1Snpiggin@suse.de * 78525d9e2d1Snpiggin@suse.de * inode's new i_size must already be written before truncate_pagecache 78625d9e2d1Snpiggin@suse.de * is called. 78725d9e2d1Snpiggin@suse.de * 78825d9e2d1Snpiggin@suse.de * This function should typically be called before the filesystem 78925d9e2d1Snpiggin@suse.de * releases resources associated with the freed range (eg. deallocates 79025d9e2d1Snpiggin@suse.de * blocks). This way, pagecache will always stay logically coherent 79125d9e2d1Snpiggin@suse.de * with on-disk format, and the filesystem would not have to deal with 79225d9e2d1Snpiggin@suse.de * situations such as writepage being called for a page that has already 79325d9e2d1Snpiggin@suse.de * had its underlying blocks deallocated. 79425d9e2d1Snpiggin@suse.de */ 7957caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize) 79625d9e2d1Snpiggin@suse.de { 79725d9e2d1Snpiggin@suse.de struct address_space *mapping = inode->i_mapping; 7988a549beaSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 79925d9e2d1Snpiggin@suse.de 80025d9e2d1Snpiggin@suse.de /* 80125d9e2d1Snpiggin@suse.de * unmap_mapping_range is called twice, first simply for 80225d9e2d1Snpiggin@suse.de * efficiency so that truncate_inode_pages does fewer 80325d9e2d1Snpiggin@suse.de * single-page unmaps. However after this first call, and 80425d9e2d1Snpiggin@suse.de * before truncate_inode_pages finishes, it is possible for 80525d9e2d1Snpiggin@suse.de * private pages to be COWed, which remain after 80625d9e2d1Snpiggin@suse.de * truncate_inode_pages finishes, hence the second 80725d9e2d1Snpiggin@suse.de * unmap_mapping_range call must be made for correctness. 80825d9e2d1Snpiggin@suse.de */ 8098a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 8108a549beaSHugh Dickins truncate_inode_pages(mapping, newsize); 8118a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 81225d9e2d1Snpiggin@suse.de } 81325d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache); 81425d9e2d1Snpiggin@suse.de 81525d9e2d1Snpiggin@suse.de /** 8162c27c65eSChristoph Hellwig * truncate_setsize - update inode and pagecache for a new file size 8172c27c65eSChristoph Hellwig * @inode: inode 8182c27c65eSChristoph Hellwig * @newsize: new file size 8192c27c65eSChristoph Hellwig * 820382e27daSJan Kara * truncate_setsize updates i_size and performs pagecache truncation (if 821382e27daSJan Kara * necessary) to @newsize. It will be typically be called from the filesystem's 822382e27daSJan Kara * setattr function when ATTR_SIZE is passed in. 8232c27c65eSChristoph Hellwig * 82477783d06SJan Kara * Must be called with a lock serializing truncates and writes (generally 82577783d06SJan Kara * i_mutex but e.g. xfs uses a different lock) and before all filesystem 82677783d06SJan Kara * specific block truncation has been performed. 8272c27c65eSChristoph Hellwig */ 8282c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize) 8292c27c65eSChristoph Hellwig { 83090a80202SJan Kara loff_t oldsize = inode->i_size; 83190a80202SJan Kara 8322c27c65eSChristoph Hellwig i_size_write(inode, newsize); 83390a80202SJan Kara if (newsize > oldsize) 83490a80202SJan Kara pagecache_isize_extended(inode, oldsize, newsize); 8357caef267SKirill A. Shutemov truncate_pagecache(inode, newsize); 8362c27c65eSChristoph Hellwig } 8372c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize); 8382c27c65eSChristoph Hellwig 8392c27c65eSChristoph Hellwig /** 84090a80202SJan Kara * pagecache_isize_extended - update pagecache after extension of i_size 84190a80202SJan Kara * @inode: inode for which i_size was extended 84290a80202SJan Kara * @from: original inode size 84390a80202SJan Kara * @to: new inode size 84490a80202SJan Kara * 84590a80202SJan Kara * Handle extension of inode size either caused by extending truncate or by 84690a80202SJan Kara * write starting after current i_size. We mark the page straddling current 84790a80202SJan Kara * i_size RO so that page_mkwrite() is called on the nearest write access to 84890a80202SJan Kara * the page. This way filesystem can be sure that page_mkwrite() is called on 84990a80202SJan Kara * the page before user writes to the page via mmap after the i_size has been 85090a80202SJan Kara * changed. 85190a80202SJan Kara * 85290a80202SJan Kara * The function must be called after i_size is updated so that page fault 85390a80202SJan Kara * coming after we unlock the page will already see the new i_size. 85490a80202SJan Kara * The function must be called while we still hold i_mutex - this not only 85590a80202SJan Kara * makes sure i_size is stable but also that userspace cannot observe new 85690a80202SJan Kara * i_size value before we are prepared to store mmap writes at new inode size. 85790a80202SJan Kara */ 85890a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 85990a80202SJan Kara { 86093407472SFabian Frederick int bsize = i_blocksize(inode); 86190a80202SJan Kara loff_t rounded_from; 86290a80202SJan Kara struct page *page; 86390a80202SJan Kara pgoff_t index; 86490a80202SJan Kara 86590a80202SJan Kara WARN_ON(to > inode->i_size); 86690a80202SJan Kara 86709cbfeafSKirill A. Shutemov if (from >= to || bsize == PAGE_SIZE) 86890a80202SJan Kara return; 86990a80202SJan Kara /* Page straddling @from will not have any hole block created? */ 87090a80202SJan Kara rounded_from = round_up(from, bsize); 87109cbfeafSKirill A. Shutemov if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 87290a80202SJan Kara return; 87390a80202SJan Kara 87409cbfeafSKirill A. Shutemov index = from >> PAGE_SHIFT; 87590a80202SJan Kara page = find_lock_page(inode->i_mapping, index); 87690a80202SJan Kara /* Page not cached? Nothing to do */ 87790a80202SJan Kara if (!page) 87890a80202SJan Kara return; 87990a80202SJan Kara /* 88090a80202SJan Kara * See clear_page_dirty_for_io() for details why set_page_dirty() 88190a80202SJan Kara * is needed. 88290a80202SJan Kara */ 88390a80202SJan Kara if (page_mkclean(page)) 88490a80202SJan Kara set_page_dirty(page); 88590a80202SJan Kara unlock_page(page); 88609cbfeafSKirill A. Shutemov put_page(page); 88790a80202SJan Kara } 88890a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended); 88990a80202SJan Kara 89090a80202SJan Kara /** 891623e3db9SHugh Dickins * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 892623e3db9SHugh Dickins * @inode: inode 893623e3db9SHugh Dickins * @lstart: offset of beginning of hole 894623e3db9SHugh Dickins * @lend: offset of last byte of hole 895623e3db9SHugh Dickins * 896623e3db9SHugh Dickins * This function should typically be called before the filesystem 897623e3db9SHugh Dickins * releases resources associated with the freed range (eg. deallocates 898623e3db9SHugh Dickins * blocks). This way, pagecache will always stay logically coherent 899623e3db9SHugh Dickins * with on-disk format, and the filesystem would not have to deal with 900623e3db9SHugh Dickins * situations such as writepage being called for a page that has already 901623e3db9SHugh Dickins * had its underlying blocks deallocated. 902623e3db9SHugh Dickins */ 903623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 904623e3db9SHugh Dickins { 905623e3db9SHugh Dickins struct address_space *mapping = inode->i_mapping; 906623e3db9SHugh Dickins loff_t unmap_start = round_up(lstart, PAGE_SIZE); 907623e3db9SHugh Dickins loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 908623e3db9SHugh Dickins /* 909623e3db9SHugh Dickins * This rounding is currently just for example: unmap_mapping_range 910623e3db9SHugh Dickins * expands its hole outwards, whereas we want it to contract the hole 911623e3db9SHugh Dickins * inwards. However, existing callers of truncate_pagecache_range are 9125a720394SLukas Czerner * doing their own page rounding first. Note that unmap_mapping_range 9135a720394SLukas Czerner * allows holelen 0 for all, and we allow lend -1 for end of file. 914623e3db9SHugh Dickins */ 915623e3db9SHugh Dickins 916623e3db9SHugh Dickins /* 917623e3db9SHugh Dickins * Unlike in truncate_pagecache, unmap_mapping_range is called only 918623e3db9SHugh Dickins * once (before truncating pagecache), and without "even_cows" flag: 919623e3db9SHugh Dickins * hole-punching should not remove private COWed pages from the hole. 920623e3db9SHugh Dickins */ 921623e3db9SHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 922623e3db9SHugh Dickins unmap_mapping_range(mapping, unmap_start, 923623e3db9SHugh Dickins 1 + unmap_end - unmap_start, 0); 924623e3db9SHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 925623e3db9SHugh Dickins } 926623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range); 927