1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * mm/truncate.c - code for taking down pages from address_spaces 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * Copyright (C) 2002, Linus Torvalds 61da177e4SLinus Torvalds * 7e1f8e874SFrancois Cami * 10Sep2002 Andrew Morton 81da177e4SLinus Torvalds * Initial version. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds #include <linux/kernel.h> 124af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h> 13f9fe48beSRoss Zwisler #include <linux/dax.h> 145a0e3ad6STejun Heo #include <linux/gfp.h> 151da177e4SLinus Torvalds #include <linux/mm.h> 160fd0e6b0SNick Piggin #include <linux/swap.h> 17b95f1b31SPaul Gortmaker #include <linux/export.h> 181da177e4SLinus Torvalds #include <linux/pagemap.h> 1901f2705dSNate Diller #include <linux/highmem.h> 201da177e4SLinus Torvalds #include <linux/pagevec.h> 21e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h> 221da177e4SLinus Torvalds #include <linux/buffer_head.h> /* grr. try_to_release_page, 23aaa4059bSJan Kara do_invalidatepage */ 243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h> 2590a80202SJan Kara #include <linux/rmap.h> 26ba470de4SRik van Riel #include "internal.h" 271da177e4SLinus Torvalds 28f2187599SMel Gorman /* 29f2187599SMel Gorman * Regular page slots are stabilized by the page lock even without the tree 30f2187599SMel Gorman * itself locked. These unlocked entries need verification under the tree 31f2187599SMel Gorman * lock. 32f2187599SMel Gorman */ 33f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping, 34f2187599SMel Gorman pgoff_t index, void *entry) 350cd6144aSJohannes Weiner { 3669b6c131SMatthew Wilcox XA_STATE(xas, &mapping->i_pages, index); 37449dd698SJohannes Weiner 3869b6c131SMatthew Wilcox xas_set_update(&xas, workingset_update_node); 3969b6c131SMatthew Wilcox if (xas_load(&xas) != entry) 40f2187599SMel Gorman return; 4169b6c131SMatthew Wilcox xas_store(&xas, NULL); 42f2187599SMel Gorman } 43f2187599SMel Gorman 44f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index, 45f2187599SMel Gorman void *entry) 46f2187599SMel Gorman { 4751b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 48b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 49f2187599SMel Gorman __clear_shadow_entry(mapping, index, entry); 50b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 5151b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 5251b8c1feSJohannes Weiner inode_add_lru(mapping->host); 5351b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 540cd6144aSJohannes Weiner } 551da177e4SLinus Torvalds 56c6dcf52cSJan Kara /* 57f2187599SMel Gorman * Unconditionally remove exceptional entries. Usually called from truncate 5851dcbdacSMatthew Wilcox (Oracle) * path. Note that the folio_batch may be altered by this function by removing 591613fac9SMatthew Wilcox (Oracle) * exceptional entries similar to what folio_batch_remove_exceptionals() does. 60c6dcf52cSJan Kara */ 6151dcbdacSMatthew Wilcox (Oracle) static void truncate_folio_batch_exceptionals(struct address_space *mapping, 6251dcbdacSMatthew Wilcox (Oracle) struct folio_batch *fbatch, pgoff_t *indices) 63c6dcf52cSJan Kara { 64f2187599SMel Gorman int i, j; 6531d270fdSMatthew Wilcox (Oracle) bool dax; 66f2187599SMel Gorman 67c6dcf52cSJan Kara /* Handled by shmem itself */ 68c6dcf52cSJan Kara if (shmem_mapping(mapping)) 69c6dcf52cSJan Kara return; 70c6dcf52cSJan Kara 7151dcbdacSMatthew Wilcox (Oracle) for (j = 0; j < folio_batch_count(fbatch); j++) 7251dcbdacSMatthew Wilcox (Oracle) if (xa_is_value(fbatch->folios[j])) 73f2187599SMel Gorman break; 74f2187599SMel Gorman 7551dcbdacSMatthew Wilcox (Oracle) if (j == folio_batch_count(fbatch)) 76c6dcf52cSJan Kara return; 77f2187599SMel Gorman 78f2187599SMel Gorman dax = dax_mapping(mapping); 7951b8c1feSJohannes Weiner if (!dax) { 8051b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 81b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 8251b8c1feSJohannes Weiner } 83f2187599SMel Gorman 8451dcbdacSMatthew Wilcox (Oracle) for (i = j; i < folio_batch_count(fbatch); i++) { 8551dcbdacSMatthew Wilcox (Oracle) struct folio *folio = fbatch->folios[i]; 86f2187599SMel Gorman pgoff_t index = indices[i]; 87f2187599SMel Gorman 8851dcbdacSMatthew Wilcox (Oracle) if (!xa_is_value(folio)) { 8951dcbdacSMatthew Wilcox (Oracle) fbatch->folios[j++] = folio; 90f2187599SMel Gorman continue; 91c6dcf52cSJan Kara } 92f2187599SMel Gorman 93f2187599SMel Gorman if (unlikely(dax)) { 94f2187599SMel Gorman dax_delete_mapping_entry(mapping, index); 95f2187599SMel Gorman continue; 96f2187599SMel Gorman } 97f2187599SMel Gorman 9851dcbdacSMatthew Wilcox (Oracle) __clear_shadow_entry(mapping, index, folio); 99f2187599SMel Gorman } 100f2187599SMel Gorman 10151b8c1feSJohannes Weiner if (!dax) { 102b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 10351b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 10451b8c1feSJohannes Weiner inode_add_lru(mapping->host); 10551b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 10651b8c1feSJohannes Weiner } 10751dcbdacSMatthew Wilcox (Oracle) fbatch->nr = j; 1080e499ed3SMatthew Wilcox (Oracle) } 1090e499ed3SMatthew Wilcox (Oracle) 110c6dcf52cSJan Kara /* 111c6dcf52cSJan Kara * Invalidate exceptional entry if easily possible. This handles exceptional 1124636e70bSRoss Zwisler * entries for invalidate_inode_pages(). 113c6dcf52cSJan Kara */ 114c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping, 115c6dcf52cSJan Kara pgoff_t index, void *entry) 116c6dcf52cSJan Kara { 1174636e70bSRoss Zwisler /* Handled by shmem itself, or for DAX we do nothing. */ 1184636e70bSRoss Zwisler if (shmem_mapping(mapping) || dax_mapping(mapping)) 119c6dcf52cSJan Kara return 1; 120c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 121c6dcf52cSJan Kara return 1; 122c6dcf52cSJan Kara } 123c6dcf52cSJan Kara 124c6dcf52cSJan Kara /* 125c6dcf52cSJan Kara * Invalidate exceptional entry if clean. This handles exceptional entries for 126c6dcf52cSJan Kara * invalidate_inode_pages2() so for DAX it evicts only clean entries. 127c6dcf52cSJan Kara */ 128c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping, 129c6dcf52cSJan Kara pgoff_t index, void *entry) 130c6dcf52cSJan Kara { 131c6dcf52cSJan Kara /* Handled by shmem itself */ 132c6dcf52cSJan Kara if (shmem_mapping(mapping)) 133c6dcf52cSJan Kara return 1; 134c6dcf52cSJan Kara if (dax_mapping(mapping)) 135c6dcf52cSJan Kara return dax_invalidate_mapping_entry_sync(mapping, index); 136c6dcf52cSJan Kara clear_shadow_entry(mapping, index, entry); 137c6dcf52cSJan Kara return 1; 138c6dcf52cSJan Kara } 139c6dcf52cSJan Kara 140cf9a2ae8SDavid Howells /** 14128bc44d7SFengguang Wu * do_invalidatepage - invalidate part or all of a page 142cf9a2ae8SDavid Howells * @page: the page which is affected 143d47992f8SLukas Czerner * @offset: start of the range to invalidate 144d47992f8SLukas Czerner * @length: length of the range to invalidate 145cf9a2ae8SDavid Howells * 146cf9a2ae8SDavid Howells * do_invalidatepage() is called when all or part of the page has become 147cf9a2ae8SDavid Howells * invalidated by a truncate operation. 148cf9a2ae8SDavid Howells * 149cf9a2ae8SDavid Howells * do_invalidatepage() does not have to release all buffers, but it must 150cf9a2ae8SDavid Howells * ensure that no dirty buffer is left outside @offset and that no I/O 151cf9a2ae8SDavid Howells * is underway against any of the blocks which are outside the truncation 152cf9a2ae8SDavid Howells * point. Because the caller is about to free (and possibly reuse) those 153cf9a2ae8SDavid Howells * blocks on-disk. 154cf9a2ae8SDavid Howells */ 155d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset, 156d47992f8SLukas Czerner unsigned int length) 157cf9a2ae8SDavid Howells { 158d47992f8SLukas Czerner void (*invalidatepage)(struct page *, unsigned int, unsigned int); 159d47992f8SLukas Czerner 160cf9a2ae8SDavid Howells invalidatepage = page->mapping->a_ops->invalidatepage; 1619361401eSDavid Howells #ifdef CONFIG_BLOCK 162cf9a2ae8SDavid Howells if (!invalidatepage) 163cf9a2ae8SDavid Howells invalidatepage = block_invalidatepage; 1649361401eSDavid Howells #endif 165cf9a2ae8SDavid Howells if (invalidatepage) 166d47992f8SLukas Czerner (*invalidatepage)(page, offset, length); 167cf9a2ae8SDavid Howells } 168cf9a2ae8SDavid Howells 169ecdfc978SLinus Torvalds /* 1701da177e4SLinus Torvalds * If truncate cannot remove the fs-private metadata from the page, the page 17162e1c553SShaohua Li * becomes orphaned. It will be left on the LRU and may even be mapped into 17254cb8821SNick Piggin * user pagetables if we're racing with filemap_fault(). 1731da177e4SLinus Torvalds * 174fc3a5ac5SMatthew Wilcox (Oracle) * We need to bail out if page->mapping is no longer equal to the original 1751da177e4SLinus Torvalds * mapping. This happens a) when the VM reclaimed the page while we waited on 176fc0ecff6SAndrew Morton * its lock, b) when a concurrent invalidate_mapping_pages got there first and 1771da177e4SLinus Torvalds * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space. 1781da177e4SLinus Torvalds */ 179efe99bbaSMatthew Wilcox (Oracle) static void truncate_cleanup_folio(struct folio *folio) 1801da177e4SLinus Torvalds { 181efe99bbaSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 1823506659eSMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 1831da177e4SLinus Torvalds 184efe99bbaSMatthew Wilcox (Oracle) if (folio_has_private(folio)) 185efe99bbaSMatthew Wilcox (Oracle) do_invalidatepage(&folio->page, 0, folio_size(folio)); 1861da177e4SLinus Torvalds 187b9ea2515SKonstantin Khlebnikov /* 188b9ea2515SKonstantin Khlebnikov * Some filesystems seem to re-dirty the page even after 189b9ea2515SKonstantin Khlebnikov * the VM has canceled the dirty bit (eg ext3 journaling). 190b9ea2515SKonstantin Khlebnikov * Hence dirty accounting check is placed after invalidation. 191b9ea2515SKonstantin Khlebnikov */ 192efe99bbaSMatthew Wilcox (Oracle) folio_cancel_dirty(folio); 193efe99bbaSMatthew Wilcox (Oracle) folio_clear_mappedtodisk(folio); 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961e84a3d9SMatthew Wilcox (Oracle) int truncate_inode_folio(struct address_space *mapping, struct folio *folio) 197750b4987SNick Piggin { 1981e84a3d9SMatthew Wilcox (Oracle) if (folio->mapping != mapping) 1999f4e41f4SJan Kara return -EIO; 2009f4e41f4SJan Kara 201efe99bbaSMatthew Wilcox (Oracle) truncate_cleanup_folio(folio); 202efe99bbaSMatthew Wilcox (Oracle) filemap_remove_folio(folio); 2039f4e41f4SJan Kara return 0; 204750b4987SNick Piggin } 205750b4987SNick Piggin 20683f78668SWu Fengguang /* 207b9a8a419SMatthew Wilcox (Oracle) * Handle partial folios. The folio may be entirely within the 208b9a8a419SMatthew Wilcox (Oracle) * range if a split has raced with us. If not, we zero the part of the 209b9a8a419SMatthew Wilcox (Oracle) * folio that's within the [start, end] range, and then split the folio if 210b9a8a419SMatthew Wilcox (Oracle) * it's large. split_page_range() will discard pages which now lie beyond 211b9a8a419SMatthew Wilcox (Oracle) * i_size, and we rely on the caller to discard pages which lie within a 212b9a8a419SMatthew Wilcox (Oracle) * newly created hole. 213b9a8a419SMatthew Wilcox (Oracle) * 214b9a8a419SMatthew Wilcox (Oracle) * Returns false if splitting failed so the caller can avoid 215b9a8a419SMatthew Wilcox (Oracle) * discarding the entire folio which is stubbornly unsplit. 216b9a8a419SMatthew Wilcox (Oracle) */ 217b9a8a419SMatthew Wilcox (Oracle) bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) 218b9a8a419SMatthew Wilcox (Oracle) { 219b9a8a419SMatthew Wilcox (Oracle) loff_t pos = folio_pos(folio); 220b9a8a419SMatthew Wilcox (Oracle) unsigned int offset, length; 221b9a8a419SMatthew Wilcox (Oracle) 222b9a8a419SMatthew Wilcox (Oracle) if (pos < start) 223b9a8a419SMatthew Wilcox (Oracle) offset = start - pos; 224b9a8a419SMatthew Wilcox (Oracle) else 225b9a8a419SMatthew Wilcox (Oracle) offset = 0; 226b9a8a419SMatthew Wilcox (Oracle) length = folio_size(folio); 227b9a8a419SMatthew Wilcox (Oracle) if (pos + length <= (u64)end) 228b9a8a419SMatthew Wilcox (Oracle) length = length - offset; 229b9a8a419SMatthew Wilcox (Oracle) else 230b9a8a419SMatthew Wilcox (Oracle) length = end + 1 - pos - offset; 231b9a8a419SMatthew Wilcox (Oracle) 232b9a8a419SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 233b9a8a419SMatthew Wilcox (Oracle) if (length == folio_size(folio)) { 234b9a8a419SMatthew Wilcox (Oracle) truncate_inode_folio(folio->mapping, folio); 235b9a8a419SMatthew Wilcox (Oracle) return true; 236b9a8a419SMatthew Wilcox (Oracle) } 237b9a8a419SMatthew Wilcox (Oracle) 238b9a8a419SMatthew Wilcox (Oracle) /* 239b9a8a419SMatthew Wilcox (Oracle) * We may be zeroing pages we're about to discard, but it avoids 240b9a8a419SMatthew Wilcox (Oracle) * doing a complex calculation here, and then doing the zeroing 241b9a8a419SMatthew Wilcox (Oracle) * anyway if the page split fails. 242b9a8a419SMatthew Wilcox (Oracle) */ 243b9a8a419SMatthew Wilcox (Oracle) folio_zero_range(folio, offset, length); 244b9a8a419SMatthew Wilcox (Oracle) 245b9a8a419SMatthew Wilcox (Oracle) if (folio_has_private(folio)) 246b9a8a419SMatthew Wilcox (Oracle) do_invalidatepage(&folio->page, offset, length); 247b9a8a419SMatthew Wilcox (Oracle) if (!folio_test_large(folio)) 248b9a8a419SMatthew Wilcox (Oracle) return true; 249b9a8a419SMatthew Wilcox (Oracle) if (split_huge_page(&folio->page) == 0) 250b9a8a419SMatthew Wilcox (Oracle) return true; 251b9a8a419SMatthew Wilcox (Oracle) if (folio_test_dirty(folio)) 252b9a8a419SMatthew Wilcox (Oracle) return false; 253b9a8a419SMatthew Wilcox (Oracle) truncate_inode_folio(folio->mapping, folio); 254b9a8a419SMatthew Wilcox (Oracle) return true; 255b9a8a419SMatthew Wilcox (Oracle) } 256b9a8a419SMatthew Wilcox (Oracle) 257b9a8a419SMatthew Wilcox (Oracle) /* 25825718736SAndi Kleen * Used to get rid of pages on hardware memory corruption. 25925718736SAndi Kleen */ 26025718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page) 26125718736SAndi Kleen { 2621e84a3d9SMatthew Wilcox (Oracle) VM_BUG_ON_PAGE(PageTail(page), page); 2631e84a3d9SMatthew Wilcox (Oracle) 26425718736SAndi Kleen if (!mapping) 26525718736SAndi Kleen return -EINVAL; 26625718736SAndi Kleen /* 26725718736SAndi Kleen * Only punch for normal data pages for now. 26825718736SAndi Kleen * Handling other types like directories would need more auditing. 26925718736SAndi Kleen */ 27025718736SAndi Kleen if (!S_ISREG(mapping->host->i_mode)) 27125718736SAndi Kleen return -EIO; 2721e84a3d9SMatthew Wilcox (Oracle) return truncate_inode_folio(mapping, page_folio(page)); 27325718736SAndi Kleen } 27425718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page); 27525718736SAndi Kleen 276d6c75dc2SMatthew Wilcox (Oracle) static long mapping_evict_folio(struct address_space *mapping, 277d6c75dc2SMatthew Wilcox (Oracle) struct folio *folio) 27883f78668SWu Fengguang { 27944184813SMatthew Wilcox (Oracle) if (folio_test_dirty(folio) || folio_test_writeback(folio)) 28083f78668SWu Fengguang return 0; 281e41c81d0SMatthew Wilcox (Oracle) /* The refcount will be elevated if any page in the folio is mapped */ 282e41c81d0SMatthew Wilcox (Oracle) if (folio_ref_count(folio) > 283e41c81d0SMatthew Wilcox (Oracle) folio_nr_pages(folio) + folio_has_private(folio) + 1) 28483f78668SWu Fengguang return 0; 28544184813SMatthew Wilcox (Oracle) if (folio_has_private(folio) && !filemap_release_folio(folio, 0)) 2861b8ddbeeSMatthew Wilcox (Oracle) return 0; 2871b8ddbeeSMatthew Wilcox (Oracle) 2885100da38SMatthew Wilcox (Oracle) return remove_mapping(mapping, folio); 28983f78668SWu Fengguang } 29083f78668SWu Fengguang 2911da177e4SLinus Torvalds /** 292d6c75dc2SMatthew Wilcox (Oracle) * invalidate_inode_page() - Remove an unused page from the pagecache. 293d6c75dc2SMatthew Wilcox (Oracle) * @page: The page to remove. 294d6c75dc2SMatthew Wilcox (Oracle) * 295d6c75dc2SMatthew Wilcox (Oracle) * Safely invalidate one page from its pagecache mapping. 296d6c75dc2SMatthew Wilcox (Oracle) * It only drops clean, unused pages. 297d6c75dc2SMatthew Wilcox (Oracle) * 298d6c75dc2SMatthew Wilcox (Oracle) * Context: Page must be locked. 299d6c75dc2SMatthew Wilcox (Oracle) * Return: The number of pages successfully removed. 300d6c75dc2SMatthew Wilcox (Oracle) */ 301d6c75dc2SMatthew Wilcox (Oracle) long invalidate_inode_page(struct page *page) 302d6c75dc2SMatthew Wilcox (Oracle) { 303d6c75dc2SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 304d6c75dc2SMatthew Wilcox (Oracle) struct address_space *mapping = folio_mapping(folio); 305d6c75dc2SMatthew Wilcox (Oracle) 306d6c75dc2SMatthew Wilcox (Oracle) /* The page may have been truncated before it was locked */ 307d6c75dc2SMatthew Wilcox (Oracle) if (!mapping) 308d6c75dc2SMatthew Wilcox (Oracle) return 0; 309d6c75dc2SMatthew Wilcox (Oracle) return mapping_evict_folio(mapping, folio); 310d6c75dc2SMatthew Wilcox (Oracle) } 311d6c75dc2SMatthew Wilcox (Oracle) 312d6c75dc2SMatthew Wilcox (Oracle) /** 31373c1e204SLiu Bo * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets 3141da177e4SLinus Torvalds * @mapping: mapping to truncate 3151da177e4SLinus Torvalds * @lstart: offset from which to truncate 3165a720394SLukas Czerner * @lend: offset to which to truncate (inclusive) 3171da177e4SLinus Torvalds * 318d7339071SHans Reiser * Truncate the page cache, removing the pages that are between 3195a720394SLukas Czerner * specified offsets (and zeroing out partial pages 3205a720394SLukas Czerner * if lstart or lend + 1 is not page aligned). 3211da177e4SLinus Torvalds * 3221da177e4SLinus Torvalds * Truncate takes two passes - the first pass is nonblocking. It will not 3231da177e4SLinus Torvalds * block on page locks and it will not block on writeback. The second pass 3241da177e4SLinus Torvalds * will wait. This is to prevent as much IO as possible in the affected region. 3251da177e4SLinus Torvalds * The first pass will remove most pages, so the search cost of the second pass 3261da177e4SLinus Torvalds * is low. 3271da177e4SLinus Torvalds * 3281da177e4SLinus Torvalds * We pass down the cache-hot hint to the page freeing code. Even if the 3291da177e4SLinus Torvalds * mapping is large, it is probably the case that the final pages are the most 3301da177e4SLinus Torvalds * recently touched, and freeing happens in ascending file offset order. 3315a720394SLukas Czerner * 3325a720394SLukas Czerner * Note that since ->invalidatepage() accepts range to invalidate 3335a720394SLukas Czerner * truncate_inode_pages_range is able to handle cases where lend + 1 is not 3345a720394SLukas Czerner * page aligned properly. 3351da177e4SLinus Torvalds */ 336d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping, 337d7339071SHans Reiser loff_t lstart, loff_t lend) 3381da177e4SLinus Torvalds { 3395a720394SLukas Czerner pgoff_t start; /* inclusive */ 3405a720394SLukas Czerner pgoff_t end; /* exclusive */ 3410e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 3420cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 343b85e0effSHugh Dickins pgoff_t index; 3441da177e4SLinus Torvalds int i; 345b9a8a419SMatthew Wilcox (Oracle) struct folio *folio; 346b9a8a419SMatthew Wilcox (Oracle) bool same_folio; 3471da177e4SLinus Torvalds 3487716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 3490a4ee518SChristoph Hellwig return; 3501da177e4SLinus Torvalds 3515a720394SLukas Czerner /* 3525a720394SLukas Czerner * 'start' and 'end' always covers the range of pages to be fully 3535a720394SLukas Czerner * truncated. Partial pages are covered with 'partial_start' at the 3545a720394SLukas Czerner * start of the range and 'partial_end' at the end of the range. 3555a720394SLukas Czerner * Note that 'end' is exclusive while 'lend' is inclusive. 3565a720394SLukas Czerner */ 35709cbfeafSKirill A. Shutemov start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT; 3585a720394SLukas Czerner if (lend == -1) 3595a720394SLukas Czerner /* 3605a720394SLukas Czerner * lend == -1 indicates end-of-file so we have to set 'end' 3615a720394SLukas Czerner * to the highest possible pgoff_t and since the type is 3625a720394SLukas Czerner * unsigned we're using -1. 3635a720394SLukas Czerner */ 3645a720394SLukas Czerner end = -1; 3655a720394SLukas Czerner else 36609cbfeafSKirill A. Shutemov end = (lend + 1) >> PAGE_SHIFT; 367d7339071SHans Reiser 36851dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 369b85e0effSHugh Dickins index = start; 3705c211ba2SMatthew Wilcox (Oracle) while (index < end && find_lock_entries(mapping, index, end - 1, 37151dcbdacSMatthew Wilcox (Oracle) &fbatch, indices)) { 37251dcbdacSMatthew Wilcox (Oracle) index = indices[folio_batch_count(&fbatch) - 1] + 1; 37351dcbdacSMatthew Wilcox (Oracle) truncate_folio_batch_exceptionals(mapping, &fbatch, indices); 37451dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) 37551dcbdacSMatthew Wilcox (Oracle) truncate_cleanup_folio(fbatch.folios[i]); 37651dcbdacSMatthew Wilcox (Oracle) delete_from_page_cache_batch(mapping, &fbatch); 37751dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) 37851dcbdacSMatthew Wilcox (Oracle) folio_unlock(fbatch.folios[i]); 37951dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 3801da177e4SLinus Torvalds cond_resched(); 3811da177e4SLinus Torvalds } 3825c211ba2SMatthew Wilcox (Oracle) 383b9a8a419SMatthew Wilcox (Oracle) same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT); 384b9a8a419SMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0); 385b9a8a419SMatthew Wilcox (Oracle) if (folio) { 386b9a8a419SMatthew Wilcox (Oracle) same_folio = lend < folio_pos(folio) + folio_size(folio); 387b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) { 388b9a8a419SMatthew Wilcox (Oracle) start = folio->index + folio_nr_pages(folio); 389b9a8a419SMatthew Wilcox (Oracle) if (same_folio) 390b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 3915a720394SLukas Czerner } 392b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 393b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 394b9a8a419SMatthew Wilcox (Oracle) folio = NULL; 3951da177e4SLinus Torvalds } 396b9a8a419SMatthew Wilcox (Oracle) 397b9a8a419SMatthew Wilcox (Oracle) if (!same_folio) 398b9a8a419SMatthew Wilcox (Oracle) folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT, 399b9a8a419SMatthew Wilcox (Oracle) FGP_LOCK, 0); 400b9a8a419SMatthew Wilcox (Oracle) if (folio) { 401b9a8a419SMatthew Wilcox (Oracle) if (!truncate_inode_partial_folio(folio, lstart, lend)) 402b9a8a419SMatthew Wilcox (Oracle) end = folio->index; 403b9a8a419SMatthew Wilcox (Oracle) folio_unlock(folio); 404b9a8a419SMatthew Wilcox (Oracle) folio_put(folio); 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 407b85e0effSHugh Dickins index = start; 408b9a8a419SMatthew Wilcox (Oracle) while (index < end) { 4091da177e4SLinus Torvalds cond_resched(); 4100e499ed3SMatthew Wilcox (Oracle) if (!find_get_entries(mapping, index, end - 1, &fbatch, 41138cefeb3SMatthew Wilcox (Oracle) indices)) { 412792ceaefSHugh Dickins /* If all gone from start onwards, we're done */ 413b85e0effSHugh Dickins if (index == start) 4141da177e4SLinus Torvalds break; 415792ceaefSHugh Dickins /* Otherwise restart to make sure all gone */ 416b85e0effSHugh Dickins index = start; 4171da177e4SLinus Torvalds continue; 4181da177e4SLinus Torvalds } 419f2187599SMel Gorman 4200e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 4210e499ed3SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 4221da177e4SLinus Torvalds 423b85e0effSHugh Dickins /* We rely upon deletion not changing page->index */ 4240cd6144aSJohannes Weiner index = indices[i]; 425b85e0effSHugh Dickins 4260e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) 4270cd6144aSJohannes Weiner continue; 4280cd6144aSJohannes Weiner 4291e84a3d9SMatthew Wilcox (Oracle) folio_lock(folio); 4301e84a3d9SMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio); 4311e84a3d9SMatthew Wilcox (Oracle) folio_wait_writeback(folio); 4321e84a3d9SMatthew Wilcox (Oracle) truncate_inode_folio(mapping, folio); 4331e84a3d9SMatthew Wilcox (Oracle) folio_unlock(folio); 434ccbbf761SMatthew Wilcox (Oracle) index = folio_index(folio) + folio_nr_pages(folio) - 1; 4351da177e4SLinus Torvalds } 4360e499ed3SMatthew Wilcox (Oracle) truncate_folio_batch_exceptionals(mapping, &fbatch, indices); 4370e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 438b85e0effSHugh Dickins index++; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds } 441d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range); 4421da177e4SLinus Torvalds 443d7339071SHans Reiser /** 444d7339071SHans Reiser * truncate_inode_pages - truncate *all* the pages from an offset 445d7339071SHans Reiser * @mapping: mapping to truncate 446d7339071SHans Reiser * @lstart: offset from which to truncate 447d7339071SHans Reiser * 448730633f0SJan Kara * Called under (and serialised by) inode->i_rwsem and 449730633f0SJan Kara * mapping->invalidate_lock. 45008142579SJan Kara * 45108142579SJan Kara * Note: When this function returns, there can be a page in the process of 45208142579SJan Kara * deletion (inside __delete_from_page_cache()) in the specified range. Thus 45308142579SJan Kara * mapping->nrpages can be non-zero when this function returns even after 45408142579SJan Kara * truncation of the whole mapping. 455d7339071SHans Reiser */ 456d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart) 457d7339071SHans Reiser { 458d7339071SHans Reiser truncate_inode_pages_range(mapping, lstart, (loff_t)-1); 459d7339071SHans Reiser } 4601da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages); 4611da177e4SLinus Torvalds 46228697355SMike Waychison /** 46391b0abe3SJohannes Weiner * truncate_inode_pages_final - truncate *all* pages before inode dies 46491b0abe3SJohannes Weiner * @mapping: mapping to truncate 46591b0abe3SJohannes Weiner * 4669608703eSJan Kara * Called under (and serialized by) inode->i_rwsem. 46791b0abe3SJohannes Weiner * 46891b0abe3SJohannes Weiner * Filesystems have to use this in the .evict_inode path to inform the 46991b0abe3SJohannes Weiner * VM that this is the final truncate and the inode is going away. 47091b0abe3SJohannes Weiner */ 47191b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping) 47291b0abe3SJohannes Weiner { 47391b0abe3SJohannes Weiner /* 47491b0abe3SJohannes Weiner * Page reclaim can not participate in regular inode lifetime 47591b0abe3SJohannes Weiner * management (can't call iput()) and thus can race with the 47691b0abe3SJohannes Weiner * inode teardown. Tell it when the address space is exiting, 47791b0abe3SJohannes Weiner * so that it does not install eviction information after the 47891b0abe3SJohannes Weiner * final truncate has begun. 47991b0abe3SJohannes Weiner */ 48091b0abe3SJohannes Weiner mapping_set_exiting(mapping); 48191b0abe3SJohannes Weiner 4827716506aSMatthew Wilcox (Oracle) if (!mapping_empty(mapping)) { 48391b0abe3SJohannes Weiner /* 48491b0abe3SJohannes Weiner * As truncation uses a lockless tree lookup, cycle 48591b0abe3SJohannes Weiner * the tree lock to make sure any ongoing tree 48691b0abe3SJohannes Weiner * modification that does not see AS_EXITING is 48791b0abe3SJohannes Weiner * completed before starting the final truncate. 48891b0abe3SJohannes Weiner */ 489b93b0163SMatthew Wilcox xa_lock_irq(&mapping->i_pages); 490b93b0163SMatthew Wilcox xa_unlock_irq(&mapping->i_pages); 49191b0abe3SJohannes Weiner } 4926ff38bd4SPavel Tikhomirov 4936ff38bd4SPavel Tikhomirov truncate_inode_pages(mapping, 0); 49491b0abe3SJohannes Weiner } 49591b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final); 49691b0abe3SJohannes Weiner 497a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping, 498eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 4991da177e4SLinus Torvalds { 5000cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 50151dcbdacSMatthew Wilcox (Oracle) struct folio_batch fbatch; 502b85e0effSHugh Dickins pgoff_t index = start; 50331560180SMinchan Kim unsigned long ret; 50431560180SMinchan Kim unsigned long count = 0; 5051da177e4SLinus Torvalds int i; 5061da177e4SLinus Torvalds 50751dcbdacSMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 50851dcbdacSMatthew Wilcox (Oracle) while (find_lock_entries(mapping, index, end, &fbatch, indices)) { 50951dcbdacSMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 510*b4545f46SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 5111da177e4SLinus Torvalds 512*b4545f46SMatthew Wilcox (Oracle) /* We rely upon deletion not changing folio->index */ 5130cd6144aSJohannes Weiner index = indices[i]; 514e0f23603SNeilBrown 515*b4545f46SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 5167ae12c80SJohannes Weiner count += invalidate_exceptional_entry(mapping, 5177ae12c80SJohannes Weiner index, 518*b4545f46SMatthew Wilcox (Oracle) folio); 5190cd6144aSJohannes Weiner continue; 5200cd6144aSJohannes Weiner } 521*b4545f46SMatthew Wilcox (Oracle) index += folio_nr_pages(folio) - 1; 522fc127da0SKirill A. Shutemov 523*b4545f46SMatthew Wilcox (Oracle) ret = mapping_evict_folio(mapping, folio); 524*b4545f46SMatthew Wilcox (Oracle) folio_unlock(folio); 52531560180SMinchan Kim /* 526*b4545f46SMatthew Wilcox (Oracle) * Invalidation is a hint that the folio is no longer 52731560180SMinchan Kim * of interest and try to speed up its reclaim. 52831560180SMinchan Kim */ 529eb1d7a65SYafang Shao if (!ret) { 530*b4545f46SMatthew Wilcox (Oracle) deactivate_file_page(&folio->page); 531eb1d7a65SYafang Shao /* It is likely on the pagevec of a remote CPU */ 532eb1d7a65SYafang Shao if (nr_pagevec) 533eb1d7a65SYafang Shao (*nr_pagevec)++; 534eb1d7a65SYafang Shao } 53531560180SMinchan Kim count += ret; 5361da177e4SLinus Torvalds } 53751dcbdacSMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 53851dcbdacSMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 539fc9a07e7SAndrew Morton cond_resched(); 540b85e0effSHugh Dickins index++; 5411da177e4SLinus Torvalds } 54231560180SMinchan Kim return count; 5431da177e4SLinus Torvalds } 544eb1d7a65SYafang Shao 545eb1d7a65SYafang Shao /** 5467ae12c80SJohannes Weiner * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode 5477ae12c80SJohannes Weiner * @mapping: the address_space which holds the cache to invalidate 548eb1d7a65SYafang Shao * @start: the offset 'from' which to invalidate 549eb1d7a65SYafang Shao * @end: the offset 'to' which to invalidate (inclusive) 550eb1d7a65SYafang Shao * 5517ae12c80SJohannes Weiner * This function removes pages that are clean, unmapped and unlocked, 5527ae12c80SJohannes Weiner * as well as shadow entries. It will not block on IO activity. 553eb1d7a65SYafang Shao * 5547ae12c80SJohannes Weiner * If you want to remove all the pages of one inode, regardless of 5557ae12c80SJohannes Weiner * their use and writeback state, use truncate_inode_pages(). 556eb1d7a65SYafang Shao * 5577ae12c80SJohannes Weiner * Return: the number of the cache entries that were invalidated 558eb1d7a65SYafang Shao */ 559eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping, 560eb1d7a65SYafang Shao pgoff_t start, pgoff_t end) 561eb1d7a65SYafang Shao { 562eb1d7a65SYafang Shao return __invalidate_mapping_pages(mapping, start, end, NULL); 563eb1d7a65SYafang Shao } 56454bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages); 5651da177e4SLinus Torvalds 566eb1d7a65SYafang Shao /** 567649c6dfeSAlex Shi * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode 568649c6dfeSAlex Shi * @mapping: the address_space which holds the pages to invalidate 569649c6dfeSAlex Shi * @start: the offset 'from' which to invalidate 570649c6dfeSAlex Shi * @end: the offset 'to' which to invalidate (inclusive) 571649c6dfeSAlex Shi * @nr_pagevec: invalidate failed page number for caller 572649c6dfeSAlex Shi * 573a00cda3fSMauro Carvalho Chehab * This helper is similar to invalidate_mapping_pages(), except that it accounts 574a00cda3fSMauro Carvalho Chehab * for pages that are likely on a pagevec and counts them in @nr_pagevec, which 575a00cda3fSMauro Carvalho Chehab * will be used by the caller. 576eb1d7a65SYafang Shao */ 577eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping, 578eb1d7a65SYafang Shao pgoff_t start, pgoff_t end, unsigned long *nr_pagevec) 579eb1d7a65SYafang Shao { 580eb1d7a65SYafang Shao __invalidate_mapping_pages(mapping, start, end, nr_pagevec); 581eb1d7a65SYafang Shao } 582eb1d7a65SYafang Shao 583bd4c8ce4SAndrew Morton /* 5841b8ddbeeSMatthew Wilcox (Oracle) * This is like invalidate_inode_page(), except it ignores the page's 585bd4c8ce4SAndrew Morton * refcount. We do this because invalidate_inode_pages2() needs stronger 586bd4c8ce4SAndrew Morton * invalidation guarantees, and cannot afford to leave pages behind because 5872706a1b8SAnderson Briglia * shrink_page_list() has a temp ref on them, or because they're transiently 5882706a1b8SAnderson Briglia * sitting in the lru_cache_add() pagevecs. 589bd4c8ce4SAndrew Morton */ 59078f42660SMatthew Wilcox (Oracle) static int invalidate_complete_folio2(struct address_space *mapping, 59178f42660SMatthew Wilcox (Oracle) struct folio *folio) 592bd4c8ce4SAndrew Morton { 59378f42660SMatthew Wilcox (Oracle) if (folio->mapping != mapping) 594bd4c8ce4SAndrew Morton return 0; 595bd4c8ce4SAndrew Morton 59678f42660SMatthew Wilcox (Oracle) if (folio_has_private(folio) && 59778f42660SMatthew Wilcox (Oracle) !filemap_release_folio(folio, GFP_KERNEL)) 598bd4c8ce4SAndrew Morton return 0; 599bd4c8ce4SAndrew Morton 60051b8c1feSJohannes Weiner spin_lock(&mapping->host->i_lock); 60130472509SJohannes Weiner xa_lock_irq(&mapping->i_pages); 60278f42660SMatthew Wilcox (Oracle) if (folio_test_dirty(folio)) 603bd4c8ce4SAndrew Morton goto failed; 604bd4c8ce4SAndrew Morton 60578f42660SMatthew Wilcox (Oracle) BUG_ON(folio_has_private(folio)); 60678f42660SMatthew Wilcox (Oracle) __filemap_remove_folio(folio, NULL); 60730472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 60851b8c1feSJohannes Weiner if (mapping_shrinkable(mapping)) 60951b8c1feSJohannes Weiner inode_add_lru(mapping->host); 61051b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 6116072d13cSLinus Torvalds 61278f42660SMatthew Wilcox (Oracle) filemap_free_folio(mapping, folio); 613bd4c8ce4SAndrew Morton return 1; 614bd4c8ce4SAndrew Morton failed: 61530472509SJohannes Weiner xa_unlock_irq(&mapping->i_pages); 61651b8c1feSJohannes Weiner spin_unlock(&mapping->host->i_lock); 617bd4c8ce4SAndrew Morton return 0; 618bd4c8ce4SAndrew Morton } 619bd4c8ce4SAndrew Morton 620f6357c3aSMatthew Wilcox (Oracle) static int do_launder_folio(struct address_space *mapping, struct folio *folio) 621e3db7691STrond Myklebust { 622f6357c3aSMatthew Wilcox (Oracle) if (!folio_test_dirty(folio)) 623e3db7691STrond Myklebust return 0; 624f6357c3aSMatthew Wilcox (Oracle) if (folio->mapping != mapping || mapping->a_ops->launder_page == NULL) 625e3db7691STrond Myklebust return 0; 626f6357c3aSMatthew Wilcox (Oracle) return mapping->a_ops->launder_page(&folio->page); 627e3db7691STrond Myklebust } 628e3db7691STrond Myklebust 6291da177e4SLinus Torvalds /** 6301da177e4SLinus Torvalds * invalidate_inode_pages2_range - remove range of pages from an address_space 63167be2dd1SMartin Waitz * @mapping: the address_space 6321da177e4SLinus Torvalds * @start: the page offset 'from' which to invalidate 6331da177e4SLinus Torvalds * @end: the page offset 'to' which to invalidate (inclusive) 6341da177e4SLinus Torvalds * 6351da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 6361da177e4SLinus Torvalds * invalidation. 6371da177e4SLinus Torvalds * 638a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 6391da177e4SLinus Torvalds */ 6401da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping, 6411da177e4SLinus Torvalds pgoff_t start, pgoff_t end) 6421da177e4SLinus Torvalds { 6430cd6144aSJohannes Weiner pgoff_t indices[PAGEVEC_SIZE]; 6440e499ed3SMatthew Wilcox (Oracle) struct folio_batch fbatch; 645b85e0effSHugh Dickins pgoff_t index; 6461da177e4SLinus Torvalds int i; 6471da177e4SLinus Torvalds int ret = 0; 6480dd1334fSHisashi Hifumi int ret2 = 0; 6491da177e4SLinus Torvalds int did_range_unmap = 0; 6501da177e4SLinus Torvalds 6517716506aSMatthew Wilcox (Oracle) if (mapping_empty(mapping)) 6520a4ee518SChristoph Hellwig return 0; 65332691f0fSAndrey Ryabinin 6540e499ed3SMatthew Wilcox (Oracle) folio_batch_init(&fbatch); 655b85e0effSHugh Dickins index = start; 6560e499ed3SMatthew Wilcox (Oracle) while (find_get_entries(mapping, index, end, &fbatch, indices)) { 6570e499ed3SMatthew Wilcox (Oracle) for (i = 0; i < folio_batch_count(&fbatch); i++) { 6580e499ed3SMatthew Wilcox (Oracle) struct folio *folio = fbatch.folios[i]; 659b85e0effSHugh Dickins 660fae9bc4aSMatthew Wilcox (Oracle) /* We rely upon deletion not changing folio->index */ 6610cd6144aSJohannes Weiner index = indices[i]; 6621da177e4SLinus Torvalds 6630e499ed3SMatthew Wilcox (Oracle) if (xa_is_value(folio)) { 664c6dcf52cSJan Kara if (!invalidate_exceptional_entry2(mapping, 6650e499ed3SMatthew Wilcox (Oracle) index, folio)) 666c6dcf52cSJan Kara ret = -EBUSY; 6670cd6144aSJohannes Weiner continue; 6680cd6144aSJohannes Weiner } 6690cd6144aSJohannes Weiner 670fae9bc4aSMatthew Wilcox (Oracle) if (!did_range_unmap && folio_mapped(folio)) { 67122061a1fSHugh Dickins /* 672fae9bc4aSMatthew Wilcox (Oracle) * If folio is mapped, before taking its lock, 67322061a1fSHugh Dickins * zap the rest of the file in one hit. 67422061a1fSHugh Dickins */ 67522061a1fSHugh Dickins unmap_mapping_pages(mapping, index, 67622061a1fSHugh Dickins (1 + end - index), false); 67722061a1fSHugh Dickins did_range_unmap = 1; 67822061a1fSHugh Dickins } 67922061a1fSHugh Dickins 680fae9bc4aSMatthew Wilcox (Oracle) folio_lock(folio); 681fae9bc4aSMatthew Wilcox (Oracle) VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio); 682fae9bc4aSMatthew Wilcox (Oracle) if (folio->mapping != mapping) { 683fae9bc4aSMatthew Wilcox (Oracle) folio_unlock(folio); 6841da177e4SLinus Torvalds continue; 6851da177e4SLinus Torvalds } 686fae9bc4aSMatthew Wilcox (Oracle) folio_wait_writeback(folio); 68722061a1fSHugh Dickins 688fae9bc4aSMatthew Wilcox (Oracle) if (folio_mapped(folio)) 689fae9bc4aSMatthew Wilcox (Oracle) unmap_mapping_folio(folio); 690fae9bc4aSMatthew Wilcox (Oracle) BUG_ON(folio_mapped(folio)); 69122061a1fSHugh Dickins 692f6357c3aSMatthew Wilcox (Oracle) ret2 = do_launder_folio(mapping, folio); 6930dd1334fSHisashi Hifumi if (ret2 == 0) { 69478f42660SMatthew Wilcox (Oracle) if (!invalidate_complete_folio2(mapping, folio)) 6956ccfa806SHisashi Hifumi ret2 = -EBUSY; 6960dd1334fSHisashi Hifumi } 6970dd1334fSHisashi Hifumi if (ret2 < 0) 6980dd1334fSHisashi Hifumi ret = ret2; 699fae9bc4aSMatthew Wilcox (Oracle) folio_unlock(folio); 7001da177e4SLinus Torvalds } 7010e499ed3SMatthew Wilcox (Oracle) folio_batch_remove_exceptionals(&fbatch); 7020e499ed3SMatthew Wilcox (Oracle) folio_batch_release(&fbatch); 7031da177e4SLinus Torvalds cond_resched(); 704b85e0effSHugh Dickins index++; 7051da177e4SLinus Torvalds } 706cd656375SJan Kara /* 70769b6c131SMatthew Wilcox * For DAX we invalidate page tables after invalidating page cache. We 708cd656375SJan Kara * could invalidate page tables while invalidating each entry however 709cd656375SJan Kara * that would be expensive. And doing range unmapping before doesn't 71069b6c131SMatthew Wilcox * work as we have no cheap way to find whether page cache entry didn't 711cd656375SJan Kara * get remapped later. 712cd656375SJan Kara */ 713cd656375SJan Kara if (dax_mapping(mapping)) { 714977fbdcdSMatthew Wilcox unmap_mapping_pages(mapping, start, end - start + 1, false); 715cd656375SJan Kara } 7161da177e4SLinus Torvalds return ret; 7171da177e4SLinus Torvalds } 7181da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range); 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /** 7211da177e4SLinus Torvalds * invalidate_inode_pages2 - remove all pages from an address_space 72267be2dd1SMartin Waitz * @mapping: the address_space 7231da177e4SLinus Torvalds * 7241da177e4SLinus Torvalds * Any pages which are found to be mapped into pagetables are unmapped prior to 7251da177e4SLinus Torvalds * invalidation. 7261da177e4SLinus Torvalds * 727a862f68aSMike Rapoport * Return: -EBUSY if any pages could not be invalidated. 7281da177e4SLinus Torvalds */ 7291da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping) 7301da177e4SLinus Torvalds { 7311da177e4SLinus Torvalds return invalidate_inode_pages2_range(mapping, 0, -1); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2); 73425d9e2d1Snpiggin@suse.de 73525d9e2d1Snpiggin@suse.de /** 73625d9e2d1Snpiggin@suse.de * truncate_pagecache - unmap and remove pagecache that has been truncated 73725d9e2d1Snpiggin@suse.de * @inode: inode 7388a549beaSHugh Dickins * @newsize: new file size 73925d9e2d1Snpiggin@suse.de * 74025d9e2d1Snpiggin@suse.de * inode's new i_size must already be written before truncate_pagecache 74125d9e2d1Snpiggin@suse.de * is called. 74225d9e2d1Snpiggin@suse.de * 74325d9e2d1Snpiggin@suse.de * This function should typically be called before the filesystem 74425d9e2d1Snpiggin@suse.de * releases resources associated with the freed range (eg. deallocates 74525d9e2d1Snpiggin@suse.de * blocks). This way, pagecache will always stay logically coherent 74625d9e2d1Snpiggin@suse.de * with on-disk format, and the filesystem would not have to deal with 74725d9e2d1Snpiggin@suse.de * situations such as writepage being called for a page that has already 74825d9e2d1Snpiggin@suse.de * had its underlying blocks deallocated. 74925d9e2d1Snpiggin@suse.de */ 7507caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize) 75125d9e2d1Snpiggin@suse.de { 75225d9e2d1Snpiggin@suse.de struct address_space *mapping = inode->i_mapping; 7538a549beaSHugh Dickins loff_t holebegin = round_up(newsize, PAGE_SIZE); 75425d9e2d1Snpiggin@suse.de 75525d9e2d1Snpiggin@suse.de /* 75625d9e2d1Snpiggin@suse.de * unmap_mapping_range is called twice, first simply for 75725d9e2d1Snpiggin@suse.de * efficiency so that truncate_inode_pages does fewer 75825d9e2d1Snpiggin@suse.de * single-page unmaps. However after this first call, and 75925d9e2d1Snpiggin@suse.de * before truncate_inode_pages finishes, it is possible for 76025d9e2d1Snpiggin@suse.de * private pages to be COWed, which remain after 76125d9e2d1Snpiggin@suse.de * truncate_inode_pages finishes, hence the second 76225d9e2d1Snpiggin@suse.de * unmap_mapping_range call must be made for correctness. 76325d9e2d1Snpiggin@suse.de */ 7648a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 7658a549beaSHugh Dickins truncate_inode_pages(mapping, newsize); 7668a549beaSHugh Dickins unmap_mapping_range(mapping, holebegin, 0, 1); 76725d9e2d1Snpiggin@suse.de } 76825d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache); 76925d9e2d1Snpiggin@suse.de 77025d9e2d1Snpiggin@suse.de /** 7712c27c65eSChristoph Hellwig * truncate_setsize - update inode and pagecache for a new file size 7722c27c65eSChristoph Hellwig * @inode: inode 7732c27c65eSChristoph Hellwig * @newsize: new file size 7742c27c65eSChristoph Hellwig * 775382e27daSJan Kara * truncate_setsize updates i_size and performs pagecache truncation (if 776382e27daSJan Kara * necessary) to @newsize. It will be typically be called from the filesystem's 777382e27daSJan Kara * setattr function when ATTR_SIZE is passed in. 7782c27c65eSChristoph Hellwig * 77977783d06SJan Kara * Must be called with a lock serializing truncates and writes (generally 7809608703eSJan Kara * i_rwsem but e.g. xfs uses a different lock) and before all filesystem 78177783d06SJan Kara * specific block truncation has been performed. 7822c27c65eSChristoph Hellwig */ 7832c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize) 7842c27c65eSChristoph Hellwig { 78590a80202SJan Kara loff_t oldsize = inode->i_size; 78690a80202SJan Kara 7872c27c65eSChristoph Hellwig i_size_write(inode, newsize); 78890a80202SJan Kara if (newsize > oldsize) 78990a80202SJan Kara pagecache_isize_extended(inode, oldsize, newsize); 7907caef267SKirill A. Shutemov truncate_pagecache(inode, newsize); 7912c27c65eSChristoph Hellwig } 7922c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize); 7932c27c65eSChristoph Hellwig 7942c27c65eSChristoph Hellwig /** 79590a80202SJan Kara * pagecache_isize_extended - update pagecache after extension of i_size 79690a80202SJan Kara * @inode: inode for which i_size was extended 79790a80202SJan Kara * @from: original inode size 79890a80202SJan Kara * @to: new inode size 79990a80202SJan Kara * 80090a80202SJan Kara * Handle extension of inode size either caused by extending truncate or by 80190a80202SJan Kara * write starting after current i_size. We mark the page straddling current 80290a80202SJan Kara * i_size RO so that page_mkwrite() is called on the nearest write access to 80390a80202SJan Kara * the page. This way filesystem can be sure that page_mkwrite() is called on 80490a80202SJan Kara * the page before user writes to the page via mmap after the i_size has been 80590a80202SJan Kara * changed. 80690a80202SJan Kara * 80790a80202SJan Kara * The function must be called after i_size is updated so that page fault 80890a80202SJan Kara * coming after we unlock the page will already see the new i_size. 8099608703eSJan Kara * The function must be called while we still hold i_rwsem - this not only 81090a80202SJan Kara * makes sure i_size is stable but also that userspace cannot observe new 81190a80202SJan Kara * i_size value before we are prepared to store mmap writes at new inode size. 81290a80202SJan Kara */ 81390a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) 81490a80202SJan Kara { 81593407472SFabian Frederick int bsize = i_blocksize(inode); 81690a80202SJan Kara loff_t rounded_from; 81790a80202SJan Kara struct page *page; 81890a80202SJan Kara pgoff_t index; 81990a80202SJan Kara 82090a80202SJan Kara WARN_ON(to > inode->i_size); 82190a80202SJan Kara 82209cbfeafSKirill A. Shutemov if (from >= to || bsize == PAGE_SIZE) 82390a80202SJan Kara return; 82490a80202SJan Kara /* Page straddling @from will not have any hole block created? */ 82590a80202SJan Kara rounded_from = round_up(from, bsize); 82609cbfeafSKirill A. Shutemov if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1))) 82790a80202SJan Kara return; 82890a80202SJan Kara 82909cbfeafSKirill A. Shutemov index = from >> PAGE_SHIFT; 83090a80202SJan Kara page = find_lock_page(inode->i_mapping, index); 83190a80202SJan Kara /* Page not cached? Nothing to do */ 83290a80202SJan Kara if (!page) 83390a80202SJan Kara return; 83490a80202SJan Kara /* 83590a80202SJan Kara * See clear_page_dirty_for_io() for details why set_page_dirty() 83690a80202SJan Kara * is needed. 83790a80202SJan Kara */ 83890a80202SJan Kara if (page_mkclean(page)) 83990a80202SJan Kara set_page_dirty(page); 84090a80202SJan Kara unlock_page(page); 84109cbfeafSKirill A. Shutemov put_page(page); 84290a80202SJan Kara } 84390a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended); 84490a80202SJan Kara 84590a80202SJan Kara /** 846623e3db9SHugh Dickins * truncate_pagecache_range - unmap and remove pagecache that is hole-punched 847623e3db9SHugh Dickins * @inode: inode 848623e3db9SHugh Dickins * @lstart: offset of beginning of hole 849623e3db9SHugh Dickins * @lend: offset of last byte of hole 850623e3db9SHugh Dickins * 851623e3db9SHugh Dickins * This function should typically be called before the filesystem 852623e3db9SHugh Dickins * releases resources associated with the freed range (eg. deallocates 853623e3db9SHugh Dickins * blocks). This way, pagecache will always stay logically coherent 854623e3db9SHugh Dickins * with on-disk format, and the filesystem would not have to deal with 855623e3db9SHugh Dickins * situations such as writepage being called for a page that has already 856623e3db9SHugh Dickins * had its underlying blocks deallocated. 857623e3db9SHugh Dickins */ 858623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend) 859623e3db9SHugh Dickins { 860623e3db9SHugh Dickins struct address_space *mapping = inode->i_mapping; 861623e3db9SHugh Dickins loff_t unmap_start = round_up(lstart, PAGE_SIZE); 862623e3db9SHugh Dickins loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1; 863623e3db9SHugh Dickins /* 864623e3db9SHugh Dickins * This rounding is currently just for example: unmap_mapping_range 865623e3db9SHugh Dickins * expands its hole outwards, whereas we want it to contract the hole 866623e3db9SHugh Dickins * inwards. However, existing callers of truncate_pagecache_range are 8675a720394SLukas Czerner * doing their own page rounding first. Note that unmap_mapping_range 8685a720394SLukas Czerner * allows holelen 0 for all, and we allow lend -1 for end of file. 869623e3db9SHugh Dickins */ 870623e3db9SHugh Dickins 871623e3db9SHugh Dickins /* 872623e3db9SHugh Dickins * Unlike in truncate_pagecache, unmap_mapping_range is called only 873623e3db9SHugh Dickins * once (before truncating pagecache), and without "even_cows" flag: 874623e3db9SHugh Dickins * hole-punching should not remove private COWed pages from the hole. 875623e3db9SHugh Dickins */ 876623e3db9SHugh Dickins if ((u64)unmap_end > (u64)unmap_start) 877623e3db9SHugh Dickins unmap_mapping_range(mapping, unmap_start, 878623e3db9SHugh Dickins 1 + unmap_end - unmap_start, 0); 879623e3db9SHugh Dickins truncate_inode_pages_range(mapping, lstart, lend); 880623e3db9SHugh Dickins } 881623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range); 882