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 28c6dcf52cSJan Kara static void clear_shadow_entry(struct address_space *mapping, pgoff_t index, 29c6dcf52cSJan Kara void *entry) 300cd6144aSJohannes Weiner { 31449dd698SJohannes Weiner struct radix_tree_node *node; 32449dd698SJohannes Weiner void **slot; 33449dd698SJohannes Weiner 34ac401cc7SJan Kara spin_lock_irq(&mapping->tree_lock); 350cd6144aSJohannes Weiner /* 360cd6144aSJohannes Weiner * Regular page slots are stabilized by the page lock even 370cd6144aSJohannes Weiner * without the tree itself locked. These unlocked entries 380cd6144aSJohannes Weiner * need verification under the tree lock. 390cd6144aSJohannes Weiner */ 4014b46879SJohannes Weiner if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot)) 41449dd698SJohannes Weiner goto unlock; 42449dd698SJohannes Weiner if (*slot != entry) 43449dd698SJohannes Weiner goto unlock; 4414b46879SJohannes Weiner __radix_tree_replace(&mapping->page_tree, node, slot, NULL, 4514b46879SJohannes Weiner workingset_update_node, mapping); 46f9fe48beSRoss Zwisler mapping->nrexceptional--; 47449dd698SJohannes Weiner unlock: 480cd6144aSJohannes Weiner spin_unlock_irq(&mapping->tree_lock); 490cd6144aSJohannes Weiner } 501da177e4SLinus Torvalds 51c6dcf52cSJan Kara /* 52c6dcf52cSJan Kara * Unconditionally remove exceptional entry. Usually called from truncate path. 53c6dcf52cSJan Kara */ 54c6dcf52cSJan Kara static void truncate_exceptional_entry(struct address_space *mapping, 55c6dcf52cSJan Kara pgoff_t index, void *entry) 56c6dcf52cSJan Kara { 57c6dcf52cSJan Kara /* Handled by shmem itself */ 58c6dcf52cSJan Kara if (shmem_mapping(mapping)) 59c6dcf52cSJan Kara return; 60c6dcf52cSJan Kara 61c6dcf52cSJan Kara if (dax_mapping(mapping)) { 62c6dcf52cSJan Kara dax_delete_mapping_entry(mapping, index); 63c6dcf52cSJan Kara return; 64c6dcf52cSJan Kara } 65c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 66c6dcf52cSJan Kara } 67c6dcf52cSJan Kara 68c6dcf52cSJan Kara /* 69c6dcf52cSJan Kara * Invalidate exceptional entry if easily possible. This handles exceptional 704636e70bSRoss Zwisler * entries for invalidate_inode_pages(). 71c6dcf52cSJan Kara */ 72c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping, 73c6dcf52cSJan Kara pgoff_t index, void *entry) 74c6dcf52cSJan Kara { 754636e70bSRoss Zwisler /* Handled by shmem itself, or for DAX we do nothing. */ 764636e70bSRoss Zwisler if (shmem_mapping(mapping) || dax_mapping(mapping)) 77c6dcf52cSJan Kara return 1; 78c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 79c6dcf52cSJan Kara return 1; 80c6dcf52cSJan Kara } 81c6dcf52cSJan Kara 82c6dcf52cSJan Kara /* 83c6dcf52cSJan Kara * Invalidate exceptional entry if clean. This handles exceptional entries for 84c6dcf52cSJan Kara * invalidate_inode_pages2() so for DAX it evicts only clean entries. 85c6dcf52cSJan Kara */ 86c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping, 87c6dcf52cSJan Kara pgoff_t index, void *entry) 88c6dcf52cSJan Kara { 89c6dcf52cSJan Kara /* Handled by shmem itself */ 90c6dcf52cSJan Kara if (shmem_mapping(mapping)) 91c6dcf52cSJan Kara return 1; 92c6dcf52cSJan Kara if (dax_mapping(mapping)) 93c6dcf52cSJan Kara return dax_invalidate_mapping_entry_sync(mapping, index); 94c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 95c6dcf52cSJan Kara return 1; 96c6dcf52cSJan Kara } 97c6dcf52cSJan Kara 98cf9a2ae8SDavid Howells /** 9928bc44d7SFengguang Wu * do_invalidatepage - invalidate part or all of a page 100cf9a2ae8SDavid Howells * @page: the page which is affected 101d47992f8SLukas Czerner * @offset: start of the range to invalidate 102d47992f8SLukas Czerner * @length: length of the range to invalidate 103cf9a2ae8SDavid Howells * 104cf9a2ae8SDavid Howells * do_invalidatepage() is called when all or part of the page has become 105cf9a2ae8SDavid Howells * invalidated by a truncate operation. 106cf9a2ae8SDavid Howells * 107cf9a2ae8SDavid Howells * do_invalidatepage() does not have to release all buffers, but it must 108cf9a2ae8SDavid Howells * ensure that no dirty buffer is left outside @offset and that no I/O 109cf9a2ae8SDavid Howells * is underway against any of the blocks which are outside the truncation 110cf9a2ae8SDavid Howells * point. Because the caller is about to free (and possibly reuse) those 111cf9a2ae8SDavid Howells * blocks on-disk. 112cf9a2ae8SDavid Howells */ 113d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset, 114d47992f8SLukas Czerner unsigned int length) 115cf9a2ae8SDavid Howells { 116d47992f8SLukas Czerner void (*invalidatepage)(struct page *, unsigned int, unsigned int); 117d47992f8SLukas Czerner 118cf9a2ae8SDavid Howells invalidatepage = page->mapping->a_ops->invalidatepage; 1199361401eSDavid Howells #ifdef CONFIG_BLOCK 120cf9a2ae8SDavid Howells if (!invalidatepage) 121cf9a2ae8SDavid Howells invalidatepage = block_invalidatepage; 1229361401eSDavid Howells #endif 123cf9a2ae8SDavid Howells if (invalidatepage) 124d47992f8SLukas Czerner (*invalidatepage)(page, offset, length); 125cf9a2ae8SDavid Howells } 126cf9a2ae8SDavid Howells 127ecdfc978SLinus Torvalds /* 1281da177e4SLinus Torvalds * If truncate cannot remove the fs-private metadata from the page, the page 12962e1c553SShaohua Li * becomes orphaned. It will be left on the LRU and may even be mapped into 13054cb8821SNick Piggin * user pagetables if we're racing with filemap_fault(). 1311da177e4SLinus Torvalds * 1321da177e4SLinus Torvalds * We need to bale out if page->mapping is no longer equal to the original 1331da177e4SLinus Torvalds * mapping. This happens a) when the VM reclaimed the page while we waited on 134fc0ecff6SAndrew Morton * its lock, b) when a concurrent invalidate_mapping_pages got there first and 1351da177e4SLinus Torvalds * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 1361da177e4SLinus Torvalds */ 1379f4e41f4SJan Kara static void 1389f4e41f4SJan Kara truncate_cleanup_page(struct address_space *mapping, struct page *page) 1391da177e4SLinus Torvalds { 1409f4e41f4SJan Kara if (page_mapped(page)) { 1419f4e41f4SJan Kara loff_t holelen; 1429f4e41f4SJan Kara 1439f4e41f4SJan Kara holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE; 1449f4e41f4SJan Kara unmap_mapping_range(mapping, 1459f4e41f4SJan Kara (loff_t)page->index << PAGE_SHIFT, 1469f4e41f4SJan Kara holelen, 0); 1479f4e41f4SJan Kara } 1481da177e4SLinus Torvalds 149266cf658SDavid Howells if (page_has_private(page)) 15009cbfeafSKirill A. Shutemov do_invalidatepage(page, 0, PAGE_SIZE); 1511da177e4SLinus Torvalds 152b9ea2515SKonstantin Khlebnikov /* 153b9ea2515SKonstantin Khlebnikov * Some filesystems seem to re-dirty the page even after 154b9ea2515SKonstantin Khlebnikov * the VM has canceled the dirty bit (eg ext3 journaling). 155b9ea2515SKonstantin Khlebnikov * Hence dirty accounting check is placed after invalidation. 156b9ea2515SKonstantin Khlebnikov */ 15711f81becSTejun Heo cancel_dirty_page(page); 1581da177e4SLinus Torvalds ClearPageMappedToDisk(page); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds /* 162fc0ecff6SAndrew Morton * This is for invalidate_mapping_pages(). That function can be called at 1631da177e4SLinus Torvalds * any time, and is not supposed to throw away dirty pages. But pages can 1640fd0e6b0SNick Piggin * be marked dirty at any time too, so use remove_mapping which safely 1650fd0e6b0SNick Piggin * discards clean, unused pages. 1661da177e4SLinus Torvalds * 1671da177e4SLinus Torvalds * Returns non-zero if the page was successfully invalidated. 1681da177e4SLinus Torvalds */ 1691da177e4SLinus Torvalds static int 1701da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page) 1711da177e4SLinus Torvalds { 1720fd0e6b0SNick Piggin int ret; 1730fd0e6b0SNick Piggin 1741da177e4SLinus Torvalds if (page->mapping != mapping) 1751da177e4SLinus Torvalds return 0; 1761da177e4SLinus Torvalds 177266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, 0)) 1781da177e4SLinus Torvalds return 0; 1791da177e4SLinus Torvalds 1800fd0e6b0SNick Piggin ret = remove_mapping(mapping, page); 1810fd0e6b0SNick Piggin 1820fd0e6b0SNick Piggin return ret; 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 185750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page) 186750b4987SNick Piggin { 187fc127da0SKirill A. Shutemov VM_BUG_ON_PAGE(PageTail(page), page); 188fc127da0SKirill A. Shutemov 1899f4e41f4SJan Kara if (page->mapping != mapping) 1909f4e41f4SJan Kara return -EIO; 1919f4e41f4SJan Kara 1929f4e41f4SJan Kara truncate_cleanup_page(mapping, page); 1939f4e41f4SJan Kara delete_from_page_cache(page); 1949f4e41f4SJan Kara return 0; 195750b4987SNick Piggin } 196750b4987SNick Piggin 19783f78668SWu Fengguang /* 19825718736SAndi Kleen * Used to get rid of pages on hardware memory corruption. 19925718736SAndi Kleen */ 20025718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page) 20125718736SAndi Kleen { 20225718736SAndi Kleen if (!mapping) 20325718736SAndi Kleen return -EINVAL; 20425718736SAndi Kleen /* 20525718736SAndi Kleen * Only punch for normal data pages for now. 20625718736SAndi Kleen * Handling other types like directories would need more auditing. 20725718736SAndi Kleen */ 20825718736SAndi Kleen if (!S_ISREG(mapping->host->i_mode)) 20925718736SAndi Kleen return -EIO; 21025718736SAndi Kleen return truncate_inode_page(mapping, page); 21125718736SAndi Kleen } 21225718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page); 21325718736SAndi Kleen 21425718736SAndi Kleen /* 21583f78668SWu Fengguang * Safely invalidate one page from its pagecache mapping. 21683f78668SWu Fengguang * It only drops clean, unused pages. The page must be locked. 21783f78668SWu Fengguang * 21883f78668SWu Fengguang * Returns 1 if the page is successfully invalidated, otherwise 0. 21983f78668SWu Fengguang */ 22083f78668SWu Fengguang int invalidate_inode_page(struct page *page) 22183f78668SWu Fengguang { 22283f78668SWu Fengguang struct address_space *mapping = page_mapping(page); 22383f78668SWu Fengguang if (!mapping) 22483f78668SWu Fengguang return 0; 22583f78668SWu Fengguang if (PageDirty(page) || PageWriteback(page)) 22683f78668SWu Fengguang return 0; 22783f78668SWu Fengguang if (page_mapped(page)) 22883f78668SWu Fengguang return 0; 22983f78668SWu Fengguang return invalidate_complete_page(mapping, page); 23083f78668SWu Fengguang } 23183f78668SWu Fengguang 2321da177e4SLinus Torvalds /** 23373c1e204SLiu Bo * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 2341da177e4SLinus Torvalds * @mapping: mapping to truncate 2351da177e4SLinus Torvalds * @lstart: offset from which to truncate 2365a720394SLukas Czerner * @lend: offset to which to truncate (inclusive) 2371da177e4SLinus Torvalds * 238d7339071SHans Reiser * Truncate the page cache, removing the pages that are between 2395a720394SLukas Czerner * specified offsets (and zeroing out partial pages 2405a720394SLukas Czerner * if lstart or lend + 1 is not page aligned). 2411da177e4SLinus Torvalds * 2421da177e4SLinus Torvalds * Truncate takes two passes - the first pass is nonblocking. It will not 2431da177e4SLinus Torvalds * block on page locks and it will not block on writeback. The second pass 2441da177e4SLinus Torvalds * will wait. This is to prevent as much IO as possible in the affected region. 2451da177e4SLinus Torvalds * The first pass will remove most pages, so the search cost of the second pass 2461da177e4SLinus Torvalds * is low. 2471da177e4SLinus Torvalds * 2481da177e4SLinus Torvalds * We pass down the cache-hot hint to the page freeing code. Even if the 2491da177e4SLinus Torvalds * mapping is large, it is probably the case that the final pages are the most 2501da177e4SLinus Torvalds * recently touched, and freeing happens in ascending file offset order. 2515a720394SLukas Czerner * 2525a720394SLukas Czerner * Note that since ->invalidatepage() accepts range to invalidate 2535a720394SLukas Czerner * truncate_inode_pages_range is able to handle cases where lend + 1 is not 2545a720394SLukas Czerner * page aligned properly. 2551da177e4SLinus Torvalds */ 256d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping, 257d7339071SHans Reiser loff_t lstart, loff_t lend) 2581da177e4SLinus Torvalds { 2595a720394SLukas Czerner pgoff_t start; /* inclusive */ 2605a720394SLukas Czerner pgoff_t end; /* exclusive */ 2615a720394SLukas Czerner unsigned int partial_start; /* inclusive */ 2625a720394SLukas Czerner unsigned int partial_end; /* exclusive */ 2631da177e4SLinus Torvalds struct pagevec pvec; 2640cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 265b85e0effSHugh Dickins pgoff_t index; 2661da177e4SLinus Torvalds int i; 2671da177e4SLinus Torvalds 268f9fe48beSRoss Zwisler if (mapping->nrpages == 0 && mapping->nrexceptional == 0) 26934ccb69eSAndrey Ryabinin goto out; 2701da177e4SLinus Torvalds 2715a720394SLukas Czerner /* Offsets within partial pages */ 27209cbfeafSKirill A. Shutemov partial_start = lstart & (PAGE_SIZE - 1); 27309cbfeafSKirill A. Shutemov partial_end = (lend + 1) & (PAGE_SIZE - 1); 2745a720394SLukas Czerner 2755a720394SLukas Czerner /* 2765a720394SLukas Czerner * 'start' and 'end' always covers the range of pages to be fully 2775a720394SLukas Czerner * truncated. Partial pages are covered with 'partial_start' at the 2785a720394SLukas Czerner * start of the range and 'partial_end' at the end of the range. 2795a720394SLukas Czerner * Note that 'end' is exclusive while 'lend' is inclusive. 2805a720394SLukas Czerner */ 28109cbfeafSKirill A. Shutemov start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 2825a720394SLukas Czerner if (lend == -1) 2835a720394SLukas Czerner /* 2845a720394SLukas Czerner * lend == -1 indicates end-of-file so we have to set 'end' 2855a720394SLukas Czerner * to the highest possible pgoff_t and since the type is 2865a720394SLukas Czerner * unsigned we're using -1. 2875a720394SLukas Czerner */ 2885a720394SLukas Czerner end = -1; 2895a720394SLukas Czerner else 29009cbfeafSKirill A. Shutemov end = (lend + 1) >> PAGE_SHIFT; 291d7339071SHans Reiser 2921da177e4SLinus Torvalds pagevec_init(&pvec, 0); 293b85e0effSHugh Dickins index = start; 2940cd6144aSJohannes Weiner while (index < end && pagevec_lookup_entries(&pvec, mapping, index, 2950cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE), 2960cd6144aSJohannes Weiner indices)) { 297*aa65c29cSJan Kara /* 298*aa65c29cSJan Kara * Pagevec array has exceptional entries and we may also fail 299*aa65c29cSJan Kara * to lock some pages. So we store pages that can be deleted 300*aa65c29cSJan Kara * in a new pagevec. 301*aa65c29cSJan Kara */ 302*aa65c29cSJan Kara struct pagevec locked_pvec; 303*aa65c29cSJan Kara 304*aa65c29cSJan Kara pagevec_init(&locked_pvec, 0); 3051da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 3061da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 3071da177e4SLinus Torvalds 308b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 3090cd6144aSJohannes Weiner index = indices[i]; 3105a720394SLukas Czerner if (index >= end) 311d7339071SHans Reiser break; 312d7339071SHans Reiser 3130cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 314c6dcf52cSJan Kara truncate_exceptional_entry(mapping, index, 315c6dcf52cSJan Kara page); 3160cd6144aSJohannes Weiner continue; 3170cd6144aSJohannes Weiner } 3180cd6144aSJohannes Weiner 319529ae9aaSNick Piggin if (!trylock_page(page)) 3201da177e4SLinus Torvalds continue; 3215cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 3221da177e4SLinus Torvalds if (PageWriteback(page)) { 3231da177e4SLinus Torvalds unlock_page(page); 3241da177e4SLinus Torvalds continue; 3251da177e4SLinus Torvalds } 326*aa65c29cSJan Kara if (page->mapping != mapping) { 3271da177e4SLinus Torvalds unlock_page(page); 328*aa65c29cSJan Kara continue; 3291da177e4SLinus Torvalds } 330*aa65c29cSJan Kara pagevec_add(&locked_pvec, page); 331*aa65c29cSJan Kara } 332*aa65c29cSJan Kara for (i = 0; i < pagevec_count(&locked_pvec); i++) 333*aa65c29cSJan Kara truncate_cleanup_page(mapping, locked_pvec.pages[i]); 334*aa65c29cSJan Kara delete_from_page_cache_batch(mapping, &locked_pvec); 335*aa65c29cSJan Kara for (i = 0; i < pagevec_count(&locked_pvec); i++) 336*aa65c29cSJan Kara unlock_page(locked_pvec.pages[i]); 3370cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 3381da177e4SLinus Torvalds pagevec_release(&pvec); 3391da177e4SLinus Torvalds cond_resched(); 340b85e0effSHugh Dickins index++; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 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(); 3850cd6144aSJohannes Weiner if (!pagevec_lookup_entries(&pvec, mapping, index, 386792ceaefSHugh Dickins min(end - index, (pgoff_t)PAGEVEC_SIZE), 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 } 3940cd6144aSJohannes Weiner if (index == start && indices[0] >= end) { 395792ceaefSHugh Dickins /* All gone out of hole to be punched, we're done */ 3960cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 397d7339071SHans Reiser pagevec_release(&pvec); 398d7339071SHans Reiser break; 399d7339071SHans Reiser } 4001da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 4011da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 4021da177e4SLinus Torvalds 403b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4040cd6144aSJohannes Weiner index = indices[i]; 405792ceaefSHugh Dickins if (index >= end) { 406792ceaefSHugh Dickins /* Restart punch to make sure all gone */ 407792ceaefSHugh Dickins index = start - 1; 408d7339071SHans Reiser break; 409792ceaefSHugh Dickins } 410b85e0effSHugh Dickins 4110cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 412c6dcf52cSJan Kara truncate_exceptional_entry(mapping, index, 413c6dcf52cSJan Kara page); 4140cd6144aSJohannes Weiner continue; 4150cd6144aSJohannes Weiner } 4160cd6144aSJohannes Weiner 4171da177e4SLinus Torvalds lock_page(page); 4185cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 4191da177e4SLinus Torvalds wait_on_page_writeback(page); 420750b4987SNick Piggin truncate_inode_page(mapping, page); 4211da177e4SLinus Torvalds unlock_page(page); 4221da177e4SLinus Torvalds } 4230cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 4241da177e4SLinus Torvalds pagevec_release(&pvec); 425b85e0effSHugh Dickins index++; 4261da177e4SLinus Torvalds } 42734ccb69eSAndrey Ryabinin 42834ccb69eSAndrey Ryabinin out: 4293167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 4301da177e4SLinus Torvalds } 431d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range); 4321da177e4SLinus Torvalds 433d7339071SHans Reiser /** 434d7339071SHans Reiser * truncate_inode_pages - truncate *all* the pages from an offset 435d7339071SHans Reiser * @mapping: mapping to truncate 436d7339071SHans Reiser * @lstart: offset from which to truncate 437d7339071SHans Reiser * 4381b1dcc1bSJes Sorensen * Called under (and serialised by) inode->i_mutex. 43908142579SJan Kara * 44008142579SJan Kara * Note: When this function returns, there can be a page in the process of 44108142579SJan Kara * deletion (inside __delete_from_page_cache()) in the specified range. Thus 44208142579SJan Kara * mapping->nrpages can be non-zero when this function returns even after 44308142579SJan Kara * truncation of the whole mapping. 444d7339071SHans Reiser */ 445d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 446d7339071SHans Reiser { 447d7339071SHans Reiser truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 448d7339071SHans Reiser } 4491da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages); 4501da177e4SLinus Torvalds 45128697355SMike Waychison /** 45291b0abe3SJohannes Weiner * truncate_inode_pages_final - truncate *all* pages before inode dies 45391b0abe3SJohannes Weiner * @mapping: mapping to truncate 45491b0abe3SJohannes Weiner * 45591b0abe3SJohannes Weiner * Called under (and serialized by) inode->i_mutex. 45691b0abe3SJohannes Weiner * 45791b0abe3SJohannes Weiner * Filesystems have to use this in the .evict_inode path to inform the 45891b0abe3SJohannes Weiner * VM that this is the final truncate and the inode is going away. 45991b0abe3SJohannes Weiner */ 46091b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping) 46191b0abe3SJohannes Weiner { 462f9fe48beSRoss Zwisler unsigned long nrexceptional; 46391b0abe3SJohannes Weiner unsigned long nrpages; 46491b0abe3SJohannes Weiner 46591b0abe3SJohannes Weiner /* 46691b0abe3SJohannes Weiner * Page reclaim can not participate in regular inode lifetime 46791b0abe3SJohannes Weiner * management (can't call iput()) and thus can race with the 46891b0abe3SJohannes Weiner * inode teardown. Tell it when the address space is exiting, 46991b0abe3SJohannes Weiner * so that it does not install eviction information after the 47091b0abe3SJohannes Weiner * final truncate has begun. 47191b0abe3SJohannes Weiner */ 47291b0abe3SJohannes Weiner mapping_set_exiting(mapping); 47391b0abe3SJohannes Weiner 47491b0abe3SJohannes Weiner /* 47591b0abe3SJohannes Weiner * When reclaim installs eviction entries, it increases 476f9fe48beSRoss Zwisler * nrexceptional first, then decreases nrpages. Make sure we see 47791b0abe3SJohannes Weiner * this in the right order or we might miss an entry. 47891b0abe3SJohannes Weiner */ 47991b0abe3SJohannes Weiner nrpages = mapping->nrpages; 48091b0abe3SJohannes Weiner smp_rmb(); 481f9fe48beSRoss Zwisler nrexceptional = mapping->nrexceptional; 48291b0abe3SJohannes Weiner 483f9fe48beSRoss Zwisler if (nrpages || nrexceptional) { 48491b0abe3SJohannes Weiner /* 48591b0abe3SJohannes Weiner * As truncation uses a lockless tree lookup, cycle 48691b0abe3SJohannes Weiner * the tree lock to make sure any ongoing tree 48791b0abe3SJohannes Weiner * modification that does not see AS_EXITING is 48891b0abe3SJohannes Weiner * completed before starting the final truncate. 48991b0abe3SJohannes Weiner */ 49091b0abe3SJohannes Weiner spin_lock_irq(&mapping->tree_lock); 49191b0abe3SJohannes Weiner spin_unlock_irq(&mapping->tree_lock); 49291b0abe3SJohannes Weiner 49391b0abe3SJohannes Weiner truncate_inode_pages(mapping, 0); 49491b0abe3SJohannes Weiner } 49591b0abe3SJohannes Weiner } 49691b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final); 49791b0abe3SJohannes Weiner 49891b0abe3SJohannes Weiner /** 49928697355SMike Waychison * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode 50028697355SMike Waychison * @mapping: the address_space which holds the pages to invalidate 50128697355SMike Waychison * @start: the offset 'from' which to invalidate 50228697355SMike Waychison * @end: the offset 'to' which to invalidate (inclusive) 50328697355SMike Waychison * 50428697355SMike Waychison * This function only removes the unlocked pages, if you want to 50528697355SMike Waychison * remove all the pages of one inode, you must call truncate_inode_pages. 50628697355SMike Waychison * 50728697355SMike Waychison * invalidate_mapping_pages() will not block on IO activity. It will not 50828697355SMike Waychison * invalidate pages which are dirty, locked, under writeback or mapped into 50928697355SMike Waychison * pagetables. 51028697355SMike Waychison */ 51128697355SMike Waychison unsigned long invalidate_mapping_pages(struct address_space *mapping, 51228697355SMike Waychison pgoff_t start, pgoff_t end) 5131da177e4SLinus Torvalds { 5140cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 5151da177e4SLinus Torvalds struct pagevec pvec; 516b85e0effSHugh Dickins pgoff_t index = start; 51731560180SMinchan Kim unsigned long ret; 51831560180SMinchan Kim unsigned long count = 0; 5191da177e4SLinus Torvalds int i; 5201da177e4SLinus Torvalds 5211da177e4SLinus Torvalds pagevec_init(&pvec, 0); 5220cd6144aSJohannes Weiner while (index <= end && pagevec_lookup_entries(&pvec, mapping, index, 5230cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 5240cd6144aSJohannes Weiner indices)) { 5251da177e4SLinus Torvalds for (i = 0; i < pagevec_count(&pvec); i++) { 5261da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 5271da177e4SLinus Torvalds 528b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 5290cd6144aSJohannes Weiner index = indices[i]; 530b85e0effSHugh Dickins if (index > end) 531b85e0effSHugh Dickins break; 532e0f23603SNeilBrown 5330cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 534c6dcf52cSJan Kara invalidate_exceptional_entry(mapping, index, 535c6dcf52cSJan Kara page); 5360cd6144aSJohannes Weiner continue; 5370cd6144aSJohannes Weiner } 5380cd6144aSJohannes Weiner 539b85e0effSHugh Dickins if (!trylock_page(page)) 540b85e0effSHugh Dickins continue; 541fc127da0SKirill A. Shutemov 5425cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 543fc127da0SKirill A. Shutemov 544fc127da0SKirill A. Shutemov /* Middle of THP: skip */ 545fc127da0SKirill A. Shutemov if (PageTransTail(page)) { 546fc127da0SKirill A. Shutemov unlock_page(page); 547fc127da0SKirill A. Shutemov continue; 548fc127da0SKirill A. Shutemov } else if (PageTransHuge(page)) { 549fc127da0SKirill A. Shutemov index += HPAGE_PMD_NR - 1; 550fc127da0SKirill A. Shutemov i += HPAGE_PMD_NR - 1; 55176b6f9b7SJan Kara /* 55276b6f9b7SJan Kara * 'end' is in the middle of THP. Don't 55376b6f9b7SJan Kara * invalidate the page as the part outside of 55476b6f9b7SJan Kara * 'end' could be still useful. 55576b6f9b7SJan Kara */ 55676b6f9b7SJan Kara if (index > end) { 55776b6f9b7SJan Kara unlock_page(page); 558fc127da0SKirill A. Shutemov continue; 559fc127da0SKirill A. Shutemov } 56076b6f9b7SJan Kara } 561fc127da0SKirill A. Shutemov 56231560180SMinchan Kim ret = invalidate_inode_page(page); 5631da177e4SLinus Torvalds unlock_page(page); 56431560180SMinchan Kim /* 56531560180SMinchan Kim * Invalidation is a hint that the page is no longer 56631560180SMinchan Kim * of interest and try to speed up its reclaim. 56731560180SMinchan Kim */ 56831560180SMinchan Kim if (!ret) 569cc5993bdSMinchan Kim deactivate_file_page(page); 57031560180SMinchan Kim count += ret; 5711da177e4SLinus Torvalds } 5720cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 5731da177e4SLinus Torvalds pagevec_release(&pvec); 574fc9a07e7SAndrew Morton cond_resched(); 575b85e0effSHugh Dickins index++; 5761da177e4SLinus Torvalds } 57731560180SMinchan Kim return count; 5781da177e4SLinus Torvalds } 57954bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages); 5801da177e4SLinus Torvalds 581bd4c8ce4SAndrew Morton /* 582bd4c8ce4SAndrew Morton * This is like invalidate_complete_page(), except it ignores the page's 583bd4c8ce4SAndrew Morton * refcount. We do this because invalidate_inode_pages2() needs stronger 584bd4c8ce4SAndrew Morton * invalidation guarantees, and cannot afford to leave pages behind because 5852706a1b8SAnderson Briglia * shrink_page_list() has a temp ref on them, or because they're transiently 5862706a1b8SAnderson Briglia * sitting in the lru_cache_add() pagevecs. 587bd4c8ce4SAndrew Morton */ 588bd4c8ce4SAndrew Morton static int 589bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page) 590bd4c8ce4SAndrew Morton { 591c4843a75SGreg Thelen unsigned long flags; 592c4843a75SGreg Thelen 593bd4c8ce4SAndrew Morton if (page->mapping != mapping) 594bd4c8ce4SAndrew Morton return 0; 595bd4c8ce4SAndrew Morton 596266cf658SDavid Howells if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL)) 597bd4c8ce4SAndrew Morton return 0; 598bd4c8ce4SAndrew Morton 599c4843a75SGreg Thelen spin_lock_irqsave(&mapping->tree_lock, flags); 600bd4c8ce4SAndrew Morton if (PageDirty(page)) 601bd4c8ce4SAndrew Morton goto failed; 602bd4c8ce4SAndrew Morton 603266cf658SDavid Howells BUG_ON(page_has_private(page)); 60462cccb8cSJohannes Weiner __delete_from_page_cache(page, NULL); 605c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 6066072d13cSLinus Torvalds 6076072d13cSLinus Torvalds if (mapping->a_ops->freepage) 6086072d13cSLinus Torvalds mapping->a_ops->freepage(page); 6096072d13cSLinus Torvalds 61009cbfeafSKirill A. Shutemov put_page(page); /* pagecache ref */ 611bd4c8ce4SAndrew Morton return 1; 612bd4c8ce4SAndrew Morton failed: 613c4843a75SGreg Thelen spin_unlock_irqrestore(&mapping->tree_lock, flags); 614bd4c8ce4SAndrew Morton return 0; 615bd4c8ce4SAndrew Morton } 616bd4c8ce4SAndrew Morton 617e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page) 618e3db7691STrond Myklebust { 619e3db7691STrond Myklebust if (!PageDirty(page)) 620e3db7691STrond Myklebust return 0; 621e3db7691STrond Myklebust if (page->mapping != mapping || mapping->a_ops->launder_page == NULL) 622e3db7691STrond Myklebust return 0; 623e3db7691STrond Myklebust return mapping->a_ops->launder_page(page); 624e3db7691STrond Myklebust } 625e3db7691STrond Myklebust 6261da177e4SLinus Torvalds /** 6271da177e4SLinus Torvalds * invalidate_inode_pages2_range - remove range of pages from an address_space 62867be2dd1SMartin Waitz * @mapping: the address_space 6291da177e4SLinus Torvalds * @start: the page offset 'from' which to invalidate 6301da177e4SLinus Torvalds * @end: the page offset 'to' which to invalidate (inclusive) 6311da177e4SLinus Torvalds * 6321da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6331da177e4SLinus Torvalds * invalidation. 6341da177e4SLinus Torvalds * 6356ccfa806SHisashi Hifumi * Returns -EBUSY if any pages could not be invalidated. 6361da177e4SLinus Torvalds */ 6371da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping, 6381da177e4SLinus Torvalds pgoff_t start, pgoff_t end) 6391da177e4SLinus Torvalds { 6400cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 6411da177e4SLinus Torvalds struct pagevec pvec; 642b85e0effSHugh Dickins pgoff_t index; 6431da177e4SLinus Torvalds int i; 6441da177e4SLinus Torvalds int ret = 0; 6450dd1334fSHisashi Hifumi int ret2 = 0; 6461da177e4SLinus Torvalds int did_range_unmap = 0; 6471da177e4SLinus Torvalds 64832691f0fSAndrey Ryabinin if (mapping->nrpages == 0 && mapping->nrexceptional == 0) 64934ccb69eSAndrey Ryabinin goto out; 65032691f0fSAndrey Ryabinin 6511da177e4SLinus Torvalds pagevec_init(&pvec, 0); 652b85e0effSHugh Dickins index = start; 6530cd6144aSJohannes Weiner while (index <= end && pagevec_lookup_entries(&pvec, mapping, index, 6540cd6144aSJohannes Weiner min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1, 6550cd6144aSJohannes Weiner indices)) { 6567b965e08STrond Myklebust for (i = 0; i < pagevec_count(&pvec); i++) { 6571da177e4SLinus Torvalds struct page *page = pvec.pages[i]; 658b85e0effSHugh Dickins 659b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 6600cd6144aSJohannes Weiner index = indices[i]; 661b85e0effSHugh Dickins if (index > end) 662b85e0effSHugh Dickins break; 6631da177e4SLinus Torvalds 6640cd6144aSJohannes Weiner if (radix_tree_exceptional_entry(page)) { 665c6dcf52cSJan Kara if (!invalidate_exceptional_entry2(mapping, 666c6dcf52cSJan Kara index, page)) 667c6dcf52cSJan Kara ret = -EBUSY; 6680cd6144aSJohannes Weiner continue; 6690cd6144aSJohannes Weiner } 6700cd6144aSJohannes Weiner 6711da177e4SLinus Torvalds lock_page(page); 6725cbc198aSKirill A. Shutemov WARN_ON(page_to_index(page) != index); 6731da177e4SLinus Torvalds if (page->mapping != mapping) { 6741da177e4SLinus Torvalds unlock_page(page); 6751da177e4SLinus Torvalds continue; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds wait_on_page_writeback(page); 678d00806b1SNick Piggin if (page_mapped(page)) { 6791da177e4SLinus Torvalds if (!did_range_unmap) { 6801da177e4SLinus Torvalds /* 6811da177e4SLinus Torvalds * Zap the rest of the file in one hit. 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds unmap_mapping_range(mapping, 68409cbfeafSKirill A. Shutemov (loff_t)index << PAGE_SHIFT, 685b85e0effSHugh Dickins (loff_t)(1 + end - index) 68609cbfeafSKirill A. Shutemov << PAGE_SHIFT, 6871da177e4SLinus Torvalds 0); 6881da177e4SLinus Torvalds did_range_unmap = 1; 6891da177e4SLinus Torvalds } else { 6901da177e4SLinus Torvalds /* 6911da177e4SLinus Torvalds * Just zap this page 6921da177e4SLinus Torvalds */ 6931da177e4SLinus Torvalds unmap_mapping_range(mapping, 69409cbfeafSKirill A. Shutemov (loff_t)index << PAGE_SHIFT, 69509cbfeafSKirill A. Shutemov PAGE_SIZE, 0); 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds } 698d00806b1SNick Piggin BUG_ON(page_mapped(page)); 6990dd1334fSHisashi Hifumi ret2 = do_launder_page(mapping, page); 7000dd1334fSHisashi Hifumi if (ret2 == 0) { 7010dd1334fSHisashi Hifumi if (!invalidate_complete_page2(mapping, page)) 7026ccfa806SHisashi Hifumi ret2 = -EBUSY; 7030dd1334fSHisashi Hifumi } 7040dd1334fSHisashi Hifumi if (ret2 < 0) 7050dd1334fSHisashi Hifumi ret = ret2; 7061da177e4SLinus Torvalds unlock_page(page); 7071da177e4SLinus Torvalds } 7080cd6144aSJohannes Weiner pagevec_remove_exceptionals(&pvec); 7091da177e4SLinus Torvalds pagevec_release(&pvec); 7101da177e4SLinus Torvalds cond_resched(); 711b85e0effSHugh Dickins index++; 7121da177e4SLinus Torvalds } 713cd656375SJan Kara /* 714cd656375SJan Kara * For DAX we invalidate page tables after invalidating radix tree. We 715cd656375SJan Kara * could invalidate page tables while invalidating each entry however 716cd656375SJan Kara * that would be expensive. And doing range unmapping before doesn't 717cd656375SJan Kara * work as we have no cheap way to find whether radix tree entry didn't 718cd656375SJan Kara * get remapped later. 719cd656375SJan Kara */ 720cd656375SJan Kara if (dax_mapping(mapping)) { 721cd656375SJan Kara unmap_mapping_range(mapping, (loff_t)start << PAGE_SHIFT, 722cd656375SJan Kara (loff_t)(end - start + 1) << PAGE_SHIFT, 0); 723cd656375SJan Kara } 72434ccb69eSAndrey Ryabinin out: 7253167760fSDan Magenheimer cleancache_invalidate_inode(mapping); 7261da177e4SLinus Torvalds return ret; 7271da177e4SLinus Torvalds } 7281da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds /** 7311da177e4SLinus Torvalds * invalidate_inode_pages2 - remove all pages from an address_space 73267be2dd1SMartin Waitz * @mapping: the address_space 7331da177e4SLinus Torvalds * 7341da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 7351da177e4SLinus Torvalds * invalidation. 7361da177e4SLinus Torvalds * 737e9de25ddSPeng Tao * Returns -EBUSY if any pages could not be invalidated. 7381da177e4SLinus Torvalds */ 7391da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping) 7401da177e4SLinus Torvalds { 7411da177e4SLinus Torvalds return invalidate_inode_pages2_range(mapping, 0, -1); 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 74425d9e2d1Snpiggin@suse.de 74525d9e2d1Snpiggin@suse.de /** 74625d9e2d1Snpiggin@suse.de * truncate_pagecache - unmap and remove pagecache that has been truncated 74725d9e2d1Snpiggin@suse.de * @inode: inode 7488a549beaSHugh Dickins * @newsize: new file size 74925d9e2d1Snpiggin@suse.de * 75025d9e2d1Snpiggin@suse.de * inode's new i_size must already be written before truncate_pagecache 75125d9e2d1Snpiggin@suse.de * is called. 75225d9e2d1Snpiggin@suse.de * 75325d9e2d1Snpiggin@suse.de * This function should typically be called before the filesystem 75425d9e2d1Snpiggin@suse.de * releases resources associated with the freed range (eg. deallocates 75525d9e2d1Snpiggin@suse.de * blocks). This way, pagecache will always stay logically coherent 75625d9e2d1Snpiggin@suse.de * with on-disk format, and the filesystem would not have to deal with 75725d9e2d1Snpiggin@suse.de * situations such as writepage being called for a page that has already 75825d9e2d1Snpiggin@suse.de * had its underlying blocks deallocated. 75925d9e2d1Snpiggin@suse.de */ 7607caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize) 76125d9e2d1Snpiggin@suse.de { 76225d9e2d1Snpiggin@suse.de struct address_space *mapping = inode->i_mapping; 7638a549beaSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 76425d9e2d1Snpiggin@suse.de 76525d9e2d1Snpiggin@suse.de /* 76625d9e2d1Snpiggin@suse.de * unmap_mapping_range is called twice, first simply for 76725d9e2d1Snpiggin@suse.de * efficiency so that truncate_inode_pages does fewer 76825d9e2d1Snpiggin@suse.de * single-page unmaps. However after this first call, and 76925d9e2d1Snpiggin@suse.de * before truncate_inode_pages finishes, it is possible for 77025d9e2d1Snpiggin@suse.de * private pages to be COWed, which remain after 77125d9e2d1Snpiggin@suse.de * truncate_inode_pages finishes, hence the second 77225d9e2d1Snpiggin@suse.de * unmap_mapping_range call must be made for correctness. 77325d9e2d1Snpiggin@suse.de */ 7748a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 7758a549beaSHugh Dickins truncate_inode_pages(mapping, newsize); 7768a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 77725d9e2d1Snpiggin@suse.de } 77825d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache); 77925d9e2d1Snpiggin@suse.de 78025d9e2d1Snpiggin@suse.de /** 7812c27c65eSChristoph Hellwig * truncate_setsize - update inode and pagecache for a new file size 7822c27c65eSChristoph Hellwig * @inode: inode 7832c27c65eSChristoph Hellwig * @newsize: new file size 7842c27c65eSChristoph Hellwig * 785382e27daSJan Kara * truncate_setsize updates i_size and performs pagecache truncation (if 786382e27daSJan Kara * necessary) to @newsize. It will be typically be called from the filesystem's 787382e27daSJan Kara * setattr function when ATTR_SIZE is passed in. 7882c27c65eSChristoph Hellwig * 78977783d06SJan Kara * Must be called with a lock serializing truncates and writes (generally 79077783d06SJan Kara * i_mutex but e.g. xfs uses a different lock) and before all filesystem 79177783d06SJan Kara * specific block truncation has been performed. 7922c27c65eSChristoph Hellwig */ 7932c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize) 7942c27c65eSChristoph Hellwig { 79590a80202SJan Kara loff_t oldsize = inode->i_size; 79690a80202SJan Kara 7972c27c65eSChristoph Hellwig i_size_write(inode, newsize); 79890a80202SJan Kara if (newsize > oldsize) 79990a80202SJan Kara pagecache_isize_extended(inode, oldsize, newsize); 8007caef267SKirill A. Shutemov truncate_pagecache(inode, newsize); 8012c27c65eSChristoph Hellwig } 8022c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize); 8032c27c65eSChristoph Hellwig 8042c27c65eSChristoph Hellwig /** 80590a80202SJan Kara * pagecache_isize_extended - update pagecache after extension of i_size 80690a80202SJan Kara * @inode: inode for which i_size was extended 80790a80202SJan Kara * @from: original inode size 80890a80202SJan Kara * @to: new inode size 80990a80202SJan Kara * 81090a80202SJan Kara * Handle extension of inode size either caused by extending truncate or by 81190a80202SJan Kara * write starting after current i_size. We mark the page straddling current 81290a80202SJan Kara * i_size RO so that page_mkwrite() is called on the nearest write access to 81390a80202SJan Kara * the page. This way filesystem can be sure that page_mkwrite() is called on 81490a80202SJan Kara * the page before user writes to the page via mmap after the i_size has been 81590a80202SJan Kara * changed. 81690a80202SJan Kara * 81790a80202SJan Kara * The function must be called after i_size is updated so that page fault 81890a80202SJan Kara * coming after we unlock the page will already see the new i_size. 81990a80202SJan Kara * The function must be called while we still hold i_mutex - this not only 82090a80202SJan Kara * makes sure i_size is stable but also that userspace cannot observe new 82190a80202SJan Kara * i_size value before we are prepared to store mmap writes at new inode size. 82290a80202SJan Kara */ 82390a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 82490a80202SJan Kara { 82593407472SFabian Frederick int bsize = i_blocksize(inode); 82690a80202SJan Kara loff_t rounded_from; 82790a80202SJan Kara struct page *page; 82890a80202SJan Kara pgoff_t index; 82990a80202SJan Kara 83090a80202SJan Kara WARN_ON(to > inode->i_size); 83190a80202SJan Kara 83209cbfeafSKirill A. Shutemov if (from >= to || bsize == PAGE_SIZE) 83390a80202SJan Kara return; 83490a80202SJan Kara /* Page straddling @from will not have any hole block created? */ 83590a80202SJan Kara rounded_from = round_up(from, bsize); 83609cbfeafSKirill A. Shutemov if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 83790a80202SJan Kara return; 83890a80202SJan Kara 83909cbfeafSKirill A. Shutemov index = from >> PAGE_SHIFT; 84090a80202SJan Kara page = find_lock_page(inode->i_mapping, index); 84190a80202SJan Kara /* Page not cached? Nothing to do */ 84290a80202SJan Kara if (!page) 84390a80202SJan Kara return; 84490a80202SJan Kara /* 84590a80202SJan Kara * See clear_page_dirty_for_io() for details why set_page_dirty() 84690a80202SJan Kara * is needed. 84790a80202SJan Kara */ 84890a80202SJan Kara if (page_mkclean(page)) 84990a80202SJan Kara set_page_dirty(page); 85090a80202SJan Kara unlock_page(page); 85109cbfeafSKirill A. Shutemov put_page(page); 85290a80202SJan Kara } 85390a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended); 85490a80202SJan Kara 85590a80202SJan Kara /** 856623e3db9SHugh Dickins * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 857623e3db9SHugh Dickins * @inode: inode 858623e3db9SHugh Dickins * @lstart: offset of beginning of hole 859623e3db9SHugh Dickins * @lend: offset of last byte of hole 860623e3db9SHugh Dickins * 861623e3db9SHugh Dickins * This function should typically be called before the filesystem 862623e3db9SHugh Dickins * releases resources associated with the freed range (eg. deallocates 863623e3db9SHugh Dickins * blocks). This way, pagecache will always stay logically coherent 864623e3db9SHugh Dickins * with on-disk format, and the filesystem would not have to deal with 865623e3db9SHugh Dickins * situations such as writepage being called for a page that has already 866623e3db9SHugh Dickins * had its underlying blocks deallocated. 867623e3db9SHugh Dickins */ 868623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 869623e3db9SHugh Dickins { 870623e3db9SHugh Dickins struct address_space *mapping = inode->i_mapping; 871623e3db9SHugh Dickins loff_t unmap_start = round_up(lstart, PAGE_SIZE); 872623e3db9SHugh Dickins loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 873623e3db9SHugh Dickins /* 874623e3db9SHugh Dickins * This rounding is currently just for example: unmap_mapping_range 875623e3db9SHugh Dickins * expands its hole outwards, whereas we want it to contract the hole 876623e3db9SHugh Dickins * inwards. However, existing callers of truncate_pagecache_range are 8775a720394SLukas Czerner * doing their own page rounding first. Note that unmap_mapping_range 8785a720394SLukas Czerner * allows holelen 0 for all, and we allow lend -1 for end of file. 879623e3db9SHugh Dickins */ 880623e3db9SHugh Dickins 881623e3db9SHugh Dickins /* 882623e3db9SHugh Dickins * Unlike in truncate_pagecache, unmap_mapping_range is called only 883623e3db9SHugh Dickins * once (before truncating pagecache), and without "even_cows" flag: 884623e3db9SHugh Dickins * hole-punching should not remove private COWed pages from the hole. 885623e3db9SHugh Dickins */ 886623e3db9SHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 887623e3db9SHugh Dickins unmap_mapping_range(mapping, unmap_start, 888623e3db9SHugh Dickins 1 + unmap_end - unmap_start, 0); 889623e3db9SHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 890623e3db9SHugh Dickins } 891623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range); 892