xref: /linux/mm/truncate.c (revision 2033c98cce666b0d125ae956613ab5111bb8d202)
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>
223a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
2390a80202SJan Kara #include <linux/rmap.h>
24ba470de4SRik van Riel #include "internal.h"
251da177e4SLinus Torvalds 
26f2187599SMel Gorman /*
27f2187599SMel Gorman  * Regular page slots are stabilized by the page lock even without the tree
28f2187599SMel Gorman  * itself locked.  These unlocked entries need verification under the tree
29f2187599SMel Gorman  * lock.
30f2187599SMel Gorman  */
31f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping,
32f2187599SMel Gorman 				pgoff_t index, void *entry)
330cd6144aSJohannes Weiner {
3469b6c131SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
35449dd698SJohannes Weiner 
3669b6c131SMatthew Wilcox 	xas_set_update(&xas, workingset_update_node);
3769b6c131SMatthew Wilcox 	if (xas_load(&xas) != entry)
38f2187599SMel Gorman 		return;
3969b6c131SMatthew Wilcox 	xas_store(&xas, NULL);
40f2187599SMel Gorman }
41f2187599SMel Gorman 
42f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
43f2187599SMel Gorman 			       void *entry)
44f2187599SMel Gorman {
4551b8c1feSJohannes Weiner 	spin_lock(&mapping->host->i_lock);
46b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
47f2187599SMel Gorman 	__clear_shadow_entry(mapping, index, entry);
48b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
4951b8c1feSJohannes Weiner 	if (mapping_shrinkable(mapping))
5051b8c1feSJohannes Weiner 		inode_add_lru(mapping->host);
5151b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
520cd6144aSJohannes Weiner }
531da177e4SLinus Torvalds 
54c6dcf52cSJan Kara /*
55f2187599SMel Gorman  * Unconditionally remove exceptional entries. Usually called from truncate
5651dcbdacSMatthew Wilcox (Oracle)  * path. Note that the folio_batch may be altered by this function by removing
571613fac9SMatthew Wilcox (Oracle)  * exceptional entries similar to what folio_batch_remove_exceptionals() does.
58c6dcf52cSJan Kara  */
5951dcbdacSMatthew Wilcox (Oracle) static void truncate_folio_batch_exceptionals(struct address_space *mapping,
6051dcbdacSMatthew Wilcox (Oracle) 				struct folio_batch *fbatch, pgoff_t *indices)
61c6dcf52cSJan Kara {
62f2187599SMel Gorman 	int i, j;
6331d270fdSMatthew Wilcox (Oracle) 	bool dax;
64f2187599SMel Gorman 
65c6dcf52cSJan Kara 	/* Handled by shmem itself */
66c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
67c6dcf52cSJan Kara 		return;
68c6dcf52cSJan Kara 
6951dcbdacSMatthew Wilcox (Oracle) 	for (j = 0; j < folio_batch_count(fbatch); j++)
7051dcbdacSMatthew Wilcox (Oracle) 		if (xa_is_value(fbatch->folios[j]))
71f2187599SMel Gorman 			break;
72f2187599SMel Gorman 
7351dcbdacSMatthew Wilcox (Oracle) 	if (j == folio_batch_count(fbatch))
74c6dcf52cSJan Kara 		return;
75f2187599SMel Gorman 
76f2187599SMel Gorman 	dax = dax_mapping(mapping);
7751b8c1feSJohannes Weiner 	if (!dax) {
7851b8c1feSJohannes Weiner 		spin_lock(&mapping->host->i_lock);
79b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
8051b8c1feSJohannes Weiner 	}
81f2187599SMel Gorman 
8251dcbdacSMatthew Wilcox (Oracle) 	for (i = j; i < folio_batch_count(fbatch); i++) {
8351dcbdacSMatthew Wilcox (Oracle) 		struct folio *folio = fbatch->folios[i];
84f2187599SMel Gorman 		pgoff_t index = indices[i];
85f2187599SMel Gorman 
8651dcbdacSMatthew Wilcox (Oracle) 		if (!xa_is_value(folio)) {
8751dcbdacSMatthew Wilcox (Oracle) 			fbatch->folios[j++] = folio;
88f2187599SMel Gorman 			continue;
89c6dcf52cSJan Kara 		}
90f2187599SMel Gorman 
91f2187599SMel Gorman 		if (unlikely(dax)) {
92f2187599SMel Gorman 			dax_delete_mapping_entry(mapping, index);
93f2187599SMel Gorman 			continue;
94f2187599SMel Gorman 		}
95f2187599SMel Gorman 
9651dcbdacSMatthew Wilcox (Oracle) 		__clear_shadow_entry(mapping, index, folio);
97f2187599SMel Gorman 	}
98f2187599SMel Gorman 
9951b8c1feSJohannes Weiner 	if (!dax) {
100b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
10151b8c1feSJohannes Weiner 		if (mapping_shrinkable(mapping))
10251b8c1feSJohannes Weiner 			inode_add_lru(mapping->host);
10351b8c1feSJohannes Weiner 		spin_unlock(&mapping->host->i_lock);
10451b8c1feSJohannes Weiner 	}
10551dcbdacSMatthew Wilcox (Oracle) 	fbatch->nr = j;
1060e499ed3SMatthew Wilcox (Oracle) }
1070e499ed3SMatthew Wilcox (Oracle) 
108c6dcf52cSJan Kara /*
109c6dcf52cSJan Kara  * Invalidate exceptional entry if easily possible. This handles exceptional
1104636e70bSRoss Zwisler  * entries for invalidate_inode_pages().
111c6dcf52cSJan Kara  */
112c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping,
113c6dcf52cSJan Kara 					pgoff_t index, void *entry)
114c6dcf52cSJan Kara {
1154636e70bSRoss Zwisler 	/* Handled by shmem itself, or for DAX we do nothing. */
1164636e70bSRoss Zwisler 	if (shmem_mapping(mapping) || dax_mapping(mapping))
117c6dcf52cSJan Kara 		return 1;
118c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
119c6dcf52cSJan Kara 	return 1;
120c6dcf52cSJan Kara }
121c6dcf52cSJan Kara 
122c6dcf52cSJan Kara /*
123c6dcf52cSJan Kara  * Invalidate exceptional entry if clean. This handles exceptional entries for
124c6dcf52cSJan Kara  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
125c6dcf52cSJan Kara  */
126c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping,
127c6dcf52cSJan Kara 					 pgoff_t index, void *entry)
128c6dcf52cSJan Kara {
129c6dcf52cSJan Kara 	/* Handled by shmem itself */
130c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
131c6dcf52cSJan Kara 		return 1;
132c6dcf52cSJan Kara 	if (dax_mapping(mapping))
133c6dcf52cSJan Kara 		return dax_invalidate_mapping_entry_sync(mapping, index);
134c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
135c6dcf52cSJan Kara 	return 1;
136c6dcf52cSJan Kara }
137c6dcf52cSJan Kara 
138cf9a2ae8SDavid Howells /**
1395ad6b2bdSMatthew Wilcox (Oracle)  * folio_invalidate - Invalidate part or all of a folio.
1405ad6b2bdSMatthew Wilcox (Oracle)  * @folio: The folio which is affected.
141d47992f8SLukas Czerner  * @offset: start of the range to invalidate
142d47992f8SLukas Czerner  * @length: length of the range to invalidate
143cf9a2ae8SDavid Howells  *
1445ad6b2bdSMatthew Wilcox (Oracle)  * folio_invalidate() is called when all or part of the folio has become
145cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
146cf9a2ae8SDavid Howells  *
1475ad6b2bdSMatthew Wilcox (Oracle)  * folio_invalidate() does not have to release all buffers, but it must
148cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
149cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
150cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
151cf9a2ae8SDavid Howells  * blocks on-disk.
152cf9a2ae8SDavid Howells  */
1535ad6b2bdSMatthew Wilcox (Oracle) void folio_invalidate(struct folio *folio, size_t offset, size_t length)
154cf9a2ae8SDavid Howells {
155128d1f82SMatthew Wilcox (Oracle) 	const struct address_space_operations *aops = folio->mapping->a_ops;
156d47992f8SLukas Czerner 
157f50015a5SMatthew Wilcox (Oracle) 	if (aops->invalidate_folio)
158128d1f82SMatthew Wilcox (Oracle) 		aops->invalidate_folio(folio, offset, length);
159cf9a2ae8SDavid Howells }
1605ad6b2bdSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(folio_invalidate);
161cf9a2ae8SDavid Howells 
162ecdfc978SLinus Torvalds /*
1631da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
16462e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
16554cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1661da177e4SLinus Torvalds  *
167fc3a5ac5SMatthew Wilcox (Oracle)  * We need to bail out if page->mapping is no longer equal to the original
1681da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
169fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1701da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1711da177e4SLinus Torvalds  */
172efe99bbaSMatthew Wilcox (Oracle) static void truncate_cleanup_folio(struct folio *folio)
1731da177e4SLinus Torvalds {
174efe99bbaSMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
1753506659eSMatthew Wilcox (Oracle) 		unmap_mapping_folio(folio);
1761da177e4SLinus Torvalds 
177efe99bbaSMatthew Wilcox (Oracle) 	if (folio_has_private(folio))
1785ad6b2bdSMatthew Wilcox (Oracle) 		folio_invalidate(folio, 0, folio_size(folio));
1791da177e4SLinus Torvalds 
180b9ea2515SKonstantin Khlebnikov 	/*
181b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
182b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
183b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
184b9ea2515SKonstantin Khlebnikov 	 */
185efe99bbaSMatthew Wilcox (Oracle) 	folio_cancel_dirty(folio);
186efe99bbaSMatthew Wilcox (Oracle) 	folio_clear_mappedtodisk(folio);
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds 
1891e84a3d9SMatthew Wilcox (Oracle) int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
190750b4987SNick Piggin {
1911e84a3d9SMatthew Wilcox (Oracle) 	if (folio->mapping != mapping)
1929f4e41f4SJan Kara 		return -EIO;
1939f4e41f4SJan Kara 
194efe99bbaSMatthew Wilcox (Oracle) 	truncate_cleanup_folio(folio);
195efe99bbaSMatthew Wilcox (Oracle) 	filemap_remove_folio(folio);
1969f4e41f4SJan Kara 	return 0;
197750b4987SNick Piggin }
198750b4987SNick Piggin 
19983f78668SWu Fengguang /*
200b9a8a419SMatthew Wilcox (Oracle)  * Handle partial folios.  The folio may be entirely within the
201b9a8a419SMatthew Wilcox (Oracle)  * range if a split has raced with us.  If not, we zero the part of the
202b9a8a419SMatthew Wilcox (Oracle)  * folio that's within the [start, end] range, and then split the folio if
203b9a8a419SMatthew Wilcox (Oracle)  * it's large.  split_page_range() will discard pages which now lie beyond
204b9a8a419SMatthew Wilcox (Oracle)  * i_size, and we rely on the caller to discard pages which lie within a
205b9a8a419SMatthew Wilcox (Oracle)  * newly created hole.
206b9a8a419SMatthew Wilcox (Oracle)  *
207b9a8a419SMatthew Wilcox (Oracle)  * Returns false if splitting failed so the caller can avoid
208b9a8a419SMatthew Wilcox (Oracle)  * discarding the entire folio which is stubbornly unsplit.
209b9a8a419SMatthew Wilcox (Oracle)  */
210b9a8a419SMatthew Wilcox (Oracle) bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
211b9a8a419SMatthew Wilcox (Oracle) {
212b9a8a419SMatthew Wilcox (Oracle) 	loff_t pos = folio_pos(folio);
213b9a8a419SMatthew Wilcox (Oracle) 	unsigned int offset, length;
214b9a8a419SMatthew Wilcox (Oracle) 
215b9a8a419SMatthew Wilcox (Oracle) 	if (pos < start)
216b9a8a419SMatthew Wilcox (Oracle) 		offset = start - pos;
217b9a8a419SMatthew Wilcox (Oracle) 	else
218b9a8a419SMatthew Wilcox (Oracle) 		offset = 0;
219b9a8a419SMatthew Wilcox (Oracle) 	length = folio_size(folio);
220b9a8a419SMatthew Wilcox (Oracle) 	if (pos + length <= (u64)end)
221b9a8a419SMatthew Wilcox (Oracle) 		length = length - offset;
222b9a8a419SMatthew Wilcox (Oracle) 	else
223b9a8a419SMatthew Wilcox (Oracle) 		length = end + 1 - pos - offset;
224b9a8a419SMatthew Wilcox (Oracle) 
225b9a8a419SMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
226b9a8a419SMatthew Wilcox (Oracle) 	if (length == folio_size(folio)) {
227b9a8a419SMatthew Wilcox (Oracle) 		truncate_inode_folio(folio->mapping, folio);
228b9a8a419SMatthew Wilcox (Oracle) 		return true;
229b9a8a419SMatthew Wilcox (Oracle) 	}
230b9a8a419SMatthew Wilcox (Oracle) 
231b9a8a419SMatthew Wilcox (Oracle) 	/*
232b9a8a419SMatthew Wilcox (Oracle) 	 * We may be zeroing pages we're about to discard, but it avoids
233b9a8a419SMatthew Wilcox (Oracle) 	 * doing a complex calculation here, and then doing the zeroing
234b9a8a419SMatthew Wilcox (Oracle) 	 * anyway if the page split fails.
235b9a8a419SMatthew Wilcox (Oracle) 	 */
236b9a8a419SMatthew Wilcox (Oracle) 	folio_zero_range(folio, offset, length);
237b9a8a419SMatthew Wilcox (Oracle) 
238b9a8a419SMatthew Wilcox (Oracle) 	if (folio_has_private(folio))
2395ad6b2bdSMatthew Wilcox (Oracle) 		folio_invalidate(folio, offset, length);
240b9a8a419SMatthew Wilcox (Oracle) 	if (!folio_test_large(folio))
241b9a8a419SMatthew Wilcox (Oracle) 		return true;
242d788f5b3SMatthew Wilcox (Oracle) 	if (split_folio(folio) == 0)
243b9a8a419SMatthew Wilcox (Oracle) 		return true;
244b9a8a419SMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio))
245b9a8a419SMatthew Wilcox (Oracle) 		return false;
246b9a8a419SMatthew Wilcox (Oracle) 	truncate_inode_folio(folio->mapping, folio);
247b9a8a419SMatthew Wilcox (Oracle) 	return true;
248b9a8a419SMatthew Wilcox (Oracle) }
249b9a8a419SMatthew Wilcox (Oracle) 
250b9a8a419SMatthew Wilcox (Oracle) /*
25125718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
25225718736SAndi Kleen  */
25325718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
25425718736SAndi Kleen {
2551e84a3d9SMatthew Wilcox (Oracle) 	VM_BUG_ON_PAGE(PageTail(page), page);
2561e84a3d9SMatthew Wilcox (Oracle) 
25725718736SAndi Kleen 	if (!mapping)
25825718736SAndi Kleen 		return -EINVAL;
25925718736SAndi Kleen 	/*
26025718736SAndi Kleen 	 * Only punch for normal data pages for now.
26125718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
26225718736SAndi Kleen 	 */
26325718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
26425718736SAndi Kleen 		return -EIO;
2651e84a3d9SMatthew Wilcox (Oracle) 	return truncate_inode_folio(mapping, page_folio(page));
26625718736SAndi Kleen }
26725718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
26825718736SAndi Kleen 
2691e12cbb9SMatthew Wilcox (Oracle) /**
2701e12cbb9SMatthew Wilcox (Oracle)  * mapping_evict_folio() - Remove an unused folio from the page-cache.
2711e12cbb9SMatthew Wilcox (Oracle)  * @mapping: The mapping this folio belongs to.
2721e12cbb9SMatthew Wilcox (Oracle)  * @folio: The folio to remove.
2731e12cbb9SMatthew Wilcox (Oracle)  *
2741e12cbb9SMatthew Wilcox (Oracle)  * Safely remove one folio from the page cache.
2751e12cbb9SMatthew Wilcox (Oracle)  * It only drops clean, unused folios.
2761e12cbb9SMatthew Wilcox (Oracle)  *
2771e12cbb9SMatthew Wilcox (Oracle)  * Context: Folio must be locked.
2781e12cbb9SMatthew Wilcox (Oracle)  * Return: The number of pages successfully removed.
2791e12cbb9SMatthew Wilcox (Oracle)  */
2801e12cbb9SMatthew Wilcox (Oracle) long mapping_evict_folio(struct address_space *mapping, struct folio *folio)
28183f78668SWu Fengguang {
2821e12cbb9SMatthew Wilcox (Oracle) 	/* The page may have been truncated before it was locked */
2831e12cbb9SMatthew Wilcox (Oracle) 	if (!mapping)
2841e12cbb9SMatthew Wilcox (Oracle) 		return 0;
28544184813SMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
28683f78668SWu Fengguang 		return 0;
287e41c81d0SMatthew Wilcox (Oracle) 	/* The refcount will be elevated if any page in the folio is mapped */
288e41c81d0SMatthew Wilcox (Oracle) 	if (folio_ref_count(folio) >
289e41c81d0SMatthew Wilcox (Oracle) 			folio_nr_pages(folio) + folio_has_private(folio) + 1)
29083f78668SWu Fengguang 		return 0;
2910201ebf2SDavid Howells 	if (!filemap_release_folio(folio, 0))
2921b8ddbeeSMatthew Wilcox (Oracle) 		return 0;
2931b8ddbeeSMatthew Wilcox (Oracle) 
2945100da38SMatthew Wilcox (Oracle) 	return remove_mapping(mapping, folio);
29583f78668SWu Fengguang }
29683f78668SWu Fengguang 
297d6c75dc2SMatthew Wilcox (Oracle) /**
29873c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2991da177e4SLinus Torvalds  * @mapping: mapping to truncate
3001da177e4SLinus Torvalds  * @lstart: offset from which to truncate
3015a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
3021da177e4SLinus Torvalds  *
303d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
3045a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
3055a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
3061da177e4SLinus Torvalds  *
3071da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
3081da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
3091da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
3101da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
3111da177e4SLinus Torvalds  * is low.
3121da177e4SLinus Torvalds  *
3131da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
3141da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
3151da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
3165a720394SLukas Czerner  *
317f50015a5SMatthew Wilcox (Oracle)  * Note that since ->invalidate_folio() accepts range to invalidate
3185a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
3195a720394SLukas Czerner  * page aligned properly.
3201da177e4SLinus Torvalds  */
321d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
322d7339071SHans Reiser 				loff_t lstart, loff_t lend)
3231da177e4SLinus Torvalds {
3245a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
3255a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
3260e499ed3SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
3270cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
328b85e0effSHugh Dickins 	pgoff_t		index;
3291da177e4SLinus Torvalds 	int		i;
330b9a8a419SMatthew Wilcox (Oracle) 	struct folio	*folio;
331b9a8a419SMatthew Wilcox (Oracle) 	bool		same_folio;
3321da177e4SLinus Torvalds 
3337716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
3340a4ee518SChristoph Hellwig 		return;
3351da177e4SLinus Torvalds 
3365a720394SLukas Czerner 	/*
3375a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
3385a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
3395a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
3405a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
3415a720394SLukas Czerner 	 */
34209cbfeafSKirill A. Shutemov 	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
3435a720394SLukas Czerner 	if (lend == -1)
3445a720394SLukas Czerner 		/*
3455a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
3465a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
3475a720394SLukas Czerner 		 * unsigned we're using -1.
3485a720394SLukas Czerner 		 */
3495a720394SLukas Czerner 		end = -1;
3505a720394SLukas Czerner 	else
35109cbfeafSKirill A. Shutemov 		end = (lend + 1) >> PAGE_SHIFT;
352d7339071SHans Reiser 
35351dcbdacSMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
354b85e0effSHugh Dickins 	index = start;
3553392ca12SVishal Moola (Oracle) 	while (index < end && find_lock_entries(mapping, &index, end - 1,
35651dcbdacSMatthew Wilcox (Oracle) 			&fbatch, indices)) {
35751dcbdacSMatthew Wilcox (Oracle) 		truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
35851dcbdacSMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++)
35951dcbdacSMatthew Wilcox (Oracle) 			truncate_cleanup_folio(fbatch.folios[i]);
36051dcbdacSMatthew Wilcox (Oracle) 		delete_from_page_cache_batch(mapping, &fbatch);
36151dcbdacSMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++)
36251dcbdacSMatthew Wilcox (Oracle) 			folio_unlock(fbatch.folios[i]);
36351dcbdacSMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
3641da177e4SLinus Torvalds 		cond_resched();
3651da177e4SLinus Torvalds 	}
3665c211ba2SMatthew Wilcox (Oracle) 
367b9a8a419SMatthew Wilcox (Oracle) 	same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
368b9a8a419SMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
36966dabbb6SChristoph Hellwig 	if (!IS_ERR(folio)) {
370b9a8a419SMatthew Wilcox (Oracle) 		same_folio = lend < folio_pos(folio) + folio_size(folio);
371b9a8a419SMatthew Wilcox (Oracle) 		if (!truncate_inode_partial_folio(folio, lstart, lend)) {
37287b11f86SSidhartha Kumar 			start = folio_next_index(folio);
373b9a8a419SMatthew Wilcox (Oracle) 			if (same_folio)
374b9a8a419SMatthew Wilcox (Oracle) 				end = folio->index;
3755a720394SLukas Czerner 		}
376b9a8a419SMatthew Wilcox (Oracle) 		folio_unlock(folio);
377b9a8a419SMatthew Wilcox (Oracle) 		folio_put(folio);
378b9a8a419SMatthew Wilcox (Oracle) 		folio = NULL;
3791da177e4SLinus Torvalds 	}
380b9a8a419SMatthew Wilcox (Oracle) 
38166dabbb6SChristoph Hellwig 	if (!same_folio) {
382b9a8a419SMatthew Wilcox (Oracle) 		folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
383b9a8a419SMatthew Wilcox (Oracle) 						FGP_LOCK, 0);
38466dabbb6SChristoph Hellwig 		if (!IS_ERR(folio)) {
385b9a8a419SMatthew Wilcox (Oracle) 			if (!truncate_inode_partial_folio(folio, lstart, lend))
386b9a8a419SMatthew Wilcox (Oracle) 				end = folio->index;
387b9a8a419SMatthew Wilcox (Oracle) 			folio_unlock(folio);
388b9a8a419SMatthew Wilcox (Oracle) 			folio_put(folio);
3891da177e4SLinus Torvalds 		}
39066dabbb6SChristoph Hellwig 	}
3911da177e4SLinus Torvalds 
392b85e0effSHugh Dickins 	index = start;
393b9a8a419SMatthew Wilcox (Oracle) 	while (index < end) {
3941da177e4SLinus Torvalds 		cond_resched();
3959fb6beeaSVishal Moola (Oracle) 		if (!find_get_entries(mapping, &index, end - 1, &fbatch,
39638cefeb3SMatthew Wilcox (Oracle) 				indices)) {
397792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
398b85e0effSHugh Dickins 			if (index == start)
3991da177e4SLinus Torvalds 				break;
400792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
401b85e0effSHugh Dickins 			index = start;
4021da177e4SLinus Torvalds 			continue;
4031da177e4SLinus Torvalds 		}
404f2187599SMel Gorman 
4050e499ed3SMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
4060e499ed3SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
4071da177e4SLinus Torvalds 
408b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
409b85e0effSHugh Dickins 
4100e499ed3SMatthew Wilcox (Oracle) 			if (xa_is_value(folio))
4110cd6144aSJohannes Weiner 				continue;
4120cd6144aSJohannes Weiner 
4131e84a3d9SMatthew Wilcox (Oracle) 			folio_lock(folio);
4149fb6beeaSVishal Moola (Oracle) 			VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
4151e84a3d9SMatthew Wilcox (Oracle) 			folio_wait_writeback(folio);
4161e84a3d9SMatthew Wilcox (Oracle) 			truncate_inode_folio(mapping, folio);
4171e84a3d9SMatthew Wilcox (Oracle) 			folio_unlock(folio);
4181da177e4SLinus Torvalds 		}
4190e499ed3SMatthew Wilcox (Oracle) 		truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
4200e499ed3SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
4211da177e4SLinus Torvalds 	}
4221da177e4SLinus Torvalds }
423d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
4241da177e4SLinus Torvalds 
425d7339071SHans Reiser /**
426d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
427d7339071SHans Reiser  * @mapping: mapping to truncate
428d7339071SHans Reiser  * @lstart: offset from which to truncate
429d7339071SHans Reiser  *
430730633f0SJan Kara  * Called under (and serialised by) inode->i_rwsem and
431730633f0SJan Kara  * mapping->invalidate_lock.
43208142579SJan Kara  *
43308142579SJan Kara  * Note: When this function returns, there can be a page in the process of
4346ffcd825SMatthew Wilcox (Oracle)  * deletion (inside __filemap_remove_folio()) in the specified range.  Thus
43508142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
43608142579SJan Kara  * truncation of the whole mapping.
437d7339071SHans Reiser  */
438d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
439d7339071SHans Reiser {
440d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
441d7339071SHans Reiser }
4421da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4431da177e4SLinus Torvalds 
44428697355SMike Waychison /**
44591b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
44691b0abe3SJohannes Weiner  * @mapping: mapping to truncate
44791b0abe3SJohannes Weiner  *
4489608703eSJan Kara  * Called under (and serialized by) inode->i_rwsem.
44991b0abe3SJohannes Weiner  *
45091b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
45191b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
45291b0abe3SJohannes Weiner  */
45391b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
45491b0abe3SJohannes Weiner {
45591b0abe3SJohannes Weiner 	/*
45691b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
45791b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
45891b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
45991b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
46091b0abe3SJohannes Weiner 	 * final truncate has begun.
46191b0abe3SJohannes Weiner 	 */
46291b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
46391b0abe3SJohannes Weiner 
4647716506aSMatthew Wilcox (Oracle) 	if (!mapping_empty(mapping)) {
46591b0abe3SJohannes Weiner 		/*
46691b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
46791b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
46891b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
46991b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
47091b0abe3SJohannes Weiner 		 */
471b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
472b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
47391b0abe3SJohannes Weiner 	}
4746ff38bd4SPavel Tikhomirov 
4756ff38bd4SPavel Tikhomirov 	truncate_inode_pages(mapping, 0);
47691b0abe3SJohannes Weiner }
47791b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
47891b0abe3SJohannes Weiner 
479c56109ddSMatthew Wilcox (Oracle) /**
4801a0fc811SMatthew Wilcox (Oracle)  * mapping_try_invalidate - Invalidate all the evictable folios of one inode
4811a0fc811SMatthew Wilcox (Oracle)  * @mapping: the address_space which holds the folios to invalidate
482c56109ddSMatthew Wilcox (Oracle)  * @start: the offset 'from' which to invalidate
483c56109ddSMatthew Wilcox (Oracle)  * @end: the offset 'to' which to invalidate (inclusive)
4841a0fc811SMatthew Wilcox (Oracle)  * @nr_failed: How many folio invalidations failed
485c56109ddSMatthew Wilcox (Oracle)  *
4861a0fc811SMatthew Wilcox (Oracle)  * This function is similar to invalidate_mapping_pages(), except that it
4871a0fc811SMatthew Wilcox (Oracle)  * returns the number of folios which could not be evicted in @nr_failed.
488c56109ddSMatthew Wilcox (Oracle)  */
4891a0fc811SMatthew Wilcox (Oracle) unsigned long mapping_try_invalidate(struct address_space *mapping,
4901a0fc811SMatthew Wilcox (Oracle) 		pgoff_t start, pgoff_t end, unsigned long *nr_failed)
4911da177e4SLinus Torvalds {
4920cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
49351dcbdacSMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
494b85e0effSHugh Dickins 	pgoff_t index = start;
49531560180SMinchan Kim 	unsigned long ret;
49631560180SMinchan Kim 	unsigned long count = 0;
4971da177e4SLinus Torvalds 	int i;
4981da177e4SLinus Torvalds 
49951dcbdacSMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
5003392ca12SVishal Moola (Oracle) 	while (find_lock_entries(mapping, &index, end, &fbatch, indices)) {
50151dcbdacSMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
502b4545f46SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
5031da177e4SLinus Torvalds 
504b4545f46SMatthew Wilcox (Oracle) 			/* We rely upon deletion not changing folio->index */
505e0f23603SNeilBrown 
506b4545f46SMatthew Wilcox (Oracle) 			if (xa_is_value(folio)) {
5077ae12c80SJohannes Weiner 				count += invalidate_exceptional_entry(mapping,
5083392ca12SVishal Moola (Oracle) 							     indices[i], folio);
5090cd6144aSJohannes Weiner 				continue;
5100cd6144aSJohannes Weiner 			}
511fc127da0SKirill A. Shutemov 
512b4545f46SMatthew Wilcox (Oracle) 			ret = mapping_evict_folio(mapping, folio);
513b4545f46SMatthew Wilcox (Oracle) 			folio_unlock(folio);
51431560180SMinchan Kim 			/*
515b4545f46SMatthew Wilcox (Oracle) 			 * Invalidation is a hint that the folio is no longer
51631560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
51731560180SMinchan Kim 			 */
518eb1d7a65SYafang Shao 			if (!ret) {
519261b6840SMatthew Wilcox (Oracle) 				deactivate_file_folio(folio);
5201a0fc811SMatthew Wilcox (Oracle) 				/* Likely in the lru cache of a remote CPU */
5211a0fc811SMatthew Wilcox (Oracle) 				if (nr_failed)
5221a0fc811SMatthew Wilcox (Oracle) 					(*nr_failed)++;
523eb1d7a65SYafang Shao 			}
52431560180SMinchan Kim 			count += ret;
5251da177e4SLinus Torvalds 		}
52651dcbdacSMatthew Wilcox (Oracle) 		folio_batch_remove_exceptionals(&fbatch);
52751dcbdacSMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
528fc9a07e7SAndrew Morton 		cond_resched();
5291da177e4SLinus Torvalds 	}
53031560180SMinchan Kim 	return count;
5311da177e4SLinus Torvalds }
532eb1d7a65SYafang Shao 
533eb1d7a65SYafang Shao /**
5347ae12c80SJohannes Weiner  * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode
5357ae12c80SJohannes Weiner  * @mapping: the address_space which holds the cache to invalidate
536eb1d7a65SYafang Shao  * @start: the offset 'from' which to invalidate
537eb1d7a65SYafang Shao  * @end: the offset 'to' which to invalidate (inclusive)
538eb1d7a65SYafang Shao  *
5397ae12c80SJohannes Weiner  * This function removes pages that are clean, unmapped and unlocked,
5407ae12c80SJohannes Weiner  * as well as shadow entries. It will not block on IO activity.
541eb1d7a65SYafang Shao  *
5427ae12c80SJohannes Weiner  * If you want to remove all the pages of one inode, regardless of
5437ae12c80SJohannes Weiner  * their use and writeback state, use truncate_inode_pages().
544eb1d7a65SYafang Shao  *
5451a0fc811SMatthew Wilcox (Oracle)  * Return: The number of indices that had their contents invalidated
546eb1d7a65SYafang Shao  */
547eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping,
548eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end)
549eb1d7a65SYafang Shao {
5501a0fc811SMatthew Wilcox (Oracle) 	return mapping_try_invalidate(mapping, start, end, NULL);
551eb1d7a65SYafang Shao }
55254bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
5531da177e4SLinus Torvalds 
554bd4c8ce4SAndrew Morton /*
555*2033c98cSMatthew Wilcox (Oracle)  * This is like mapping_evict_folio(), except it ignores the folio's
556bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
557*2033c98cSMatthew Wilcox (Oracle)  * invalidation guarantees, and cannot afford to leave folios behind because
5582706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
5591fec6890SMatthew Wilcox (Oracle)  * sitting in the folio_add_lru() caches.
560bd4c8ce4SAndrew Morton  */
56178f42660SMatthew Wilcox (Oracle) static int invalidate_complete_folio2(struct address_space *mapping,
56278f42660SMatthew Wilcox (Oracle) 					struct folio *folio)
563bd4c8ce4SAndrew Morton {
56478f42660SMatthew Wilcox (Oracle) 	if (folio->mapping != mapping)
565bd4c8ce4SAndrew Morton 		return 0;
566bd4c8ce4SAndrew Morton 
5670201ebf2SDavid Howells 	if (!filemap_release_folio(folio, GFP_KERNEL))
568bd4c8ce4SAndrew Morton 		return 0;
569bd4c8ce4SAndrew Morton 
57051b8c1feSJohannes Weiner 	spin_lock(&mapping->host->i_lock);
57130472509SJohannes Weiner 	xa_lock_irq(&mapping->i_pages);
57278f42660SMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio))
573bd4c8ce4SAndrew Morton 		goto failed;
574bd4c8ce4SAndrew Morton 
57578f42660SMatthew Wilcox (Oracle) 	BUG_ON(folio_has_private(folio));
57678f42660SMatthew Wilcox (Oracle) 	__filemap_remove_folio(folio, NULL);
57730472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
57851b8c1feSJohannes Weiner 	if (mapping_shrinkable(mapping))
57951b8c1feSJohannes Weiner 		inode_add_lru(mapping->host);
58051b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
5816072d13cSLinus Torvalds 
58278f42660SMatthew Wilcox (Oracle) 	filemap_free_folio(mapping, folio);
583bd4c8ce4SAndrew Morton 	return 1;
584bd4c8ce4SAndrew Morton failed:
58530472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
58651b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
587bd4c8ce4SAndrew Morton 	return 0;
588bd4c8ce4SAndrew Morton }
589bd4c8ce4SAndrew Morton 
590affa80e8SMatthew Wilcox (Oracle) static int folio_launder(struct address_space *mapping, struct folio *folio)
591e3db7691STrond Myklebust {
592f6357c3aSMatthew Wilcox (Oracle) 	if (!folio_test_dirty(folio))
593e3db7691STrond Myklebust 		return 0;
594affa80e8SMatthew Wilcox (Oracle) 	if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
595e3db7691STrond Myklebust 		return 0;
596affa80e8SMatthew Wilcox (Oracle) 	return mapping->a_ops->launder_folio(folio);
597e3db7691STrond Myklebust }
598e3db7691STrond Myklebust 
5991da177e4SLinus Torvalds /**
6001da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
60167be2dd1SMartin Waitz  * @mapping: the address_space
6021da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
6031da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
6041da177e4SLinus Torvalds  *
6051da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6061da177e4SLinus Torvalds  * invalidation.
6071da177e4SLinus Torvalds  *
608a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
6091da177e4SLinus Torvalds  */
6101da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
6111da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
6121da177e4SLinus Torvalds {
6130cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
6140e499ed3SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
615b85e0effSHugh Dickins 	pgoff_t index;
6161da177e4SLinus Torvalds 	int i;
6171da177e4SLinus Torvalds 	int ret = 0;
6180dd1334fSHisashi Hifumi 	int ret2 = 0;
6191da177e4SLinus Torvalds 	int did_range_unmap = 0;
6201da177e4SLinus Torvalds 
6217716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
6220a4ee518SChristoph Hellwig 		return 0;
62332691f0fSAndrey Ryabinin 
6240e499ed3SMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
625b85e0effSHugh Dickins 	index = start;
6269fb6beeaSVishal Moola (Oracle) 	while (find_get_entries(mapping, &index, end, &fbatch, indices)) {
6270e499ed3SMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
6280e499ed3SMatthew Wilcox (Oracle) 			struct folio *folio = fbatch.folios[i];
629b85e0effSHugh Dickins 
630fae9bc4aSMatthew Wilcox (Oracle) 			/* We rely upon deletion not changing folio->index */
6311da177e4SLinus Torvalds 
6320e499ed3SMatthew Wilcox (Oracle) 			if (xa_is_value(folio)) {
633c6dcf52cSJan Kara 				if (!invalidate_exceptional_entry2(mapping,
6349fb6beeaSVishal Moola (Oracle) 						indices[i], folio))
635c6dcf52cSJan Kara 					ret = -EBUSY;
6360cd6144aSJohannes Weiner 				continue;
6370cd6144aSJohannes Weiner 			}
6380cd6144aSJohannes Weiner 
639fae9bc4aSMatthew Wilcox (Oracle) 			if (!did_range_unmap && folio_mapped(folio)) {
64022061a1fSHugh Dickins 				/*
641fae9bc4aSMatthew Wilcox (Oracle) 				 * If folio is mapped, before taking its lock,
64222061a1fSHugh Dickins 				 * zap the rest of the file in one hit.
64322061a1fSHugh Dickins 				 */
6449fb6beeaSVishal Moola (Oracle) 				unmap_mapping_pages(mapping, indices[i],
6459fb6beeaSVishal Moola (Oracle) 						(1 + end - indices[i]), false);
64622061a1fSHugh Dickins 				did_range_unmap = 1;
64722061a1fSHugh Dickins 			}
64822061a1fSHugh Dickins 
649fae9bc4aSMatthew Wilcox (Oracle) 			folio_lock(folio);
650aa5b9178SHugh Dickins 			if (unlikely(folio->mapping != mapping)) {
651fae9bc4aSMatthew Wilcox (Oracle) 				folio_unlock(folio);
6521da177e4SLinus Torvalds 				continue;
6531da177e4SLinus Torvalds 			}
654aa5b9178SHugh Dickins 			VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
655fae9bc4aSMatthew Wilcox (Oracle) 			folio_wait_writeback(folio);
65622061a1fSHugh Dickins 
657fae9bc4aSMatthew Wilcox (Oracle) 			if (folio_mapped(folio))
658fae9bc4aSMatthew Wilcox (Oracle) 				unmap_mapping_folio(folio);
659fae9bc4aSMatthew Wilcox (Oracle) 			BUG_ON(folio_mapped(folio));
66022061a1fSHugh Dickins 
661affa80e8SMatthew Wilcox (Oracle) 			ret2 = folio_launder(mapping, folio);
6620dd1334fSHisashi Hifumi 			if (ret2 == 0) {
66378f42660SMatthew Wilcox (Oracle) 				if (!invalidate_complete_folio2(mapping, folio))
6646ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
6650dd1334fSHisashi Hifumi 			}
6660dd1334fSHisashi Hifumi 			if (ret2 < 0)
6670dd1334fSHisashi Hifumi 				ret = ret2;
668fae9bc4aSMatthew Wilcox (Oracle) 			folio_unlock(folio);
6691da177e4SLinus Torvalds 		}
6700e499ed3SMatthew Wilcox (Oracle) 		folio_batch_remove_exceptionals(&fbatch);
6710e499ed3SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
6721da177e4SLinus Torvalds 		cond_resched();
6731da177e4SLinus Torvalds 	}
674cd656375SJan Kara 	/*
67569b6c131SMatthew Wilcox 	 * For DAX we invalidate page tables after invalidating page cache.  We
676cd656375SJan Kara 	 * could invalidate page tables while invalidating each entry however
677cd656375SJan Kara 	 * that would be expensive. And doing range unmapping before doesn't
67869b6c131SMatthew Wilcox 	 * work as we have no cheap way to find whether page cache entry didn't
679cd656375SJan Kara 	 * get remapped later.
680cd656375SJan Kara 	 */
681cd656375SJan Kara 	if (dax_mapping(mapping)) {
682977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, start, end - start + 1, false);
683cd656375SJan Kara 	}
6841da177e4SLinus Torvalds 	return ret;
6851da177e4SLinus Torvalds }
6861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
6871da177e4SLinus Torvalds 
6881da177e4SLinus Torvalds /**
6891da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
69067be2dd1SMartin Waitz  * @mapping: the address_space
6911da177e4SLinus Torvalds  *
6921da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6931da177e4SLinus Torvalds  * invalidation.
6941da177e4SLinus Torvalds  *
695a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
6961da177e4SLinus Torvalds  */
6971da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
7001da177e4SLinus Torvalds }
7011da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
70225d9e2d1Snpiggin@suse.de 
70325d9e2d1Snpiggin@suse.de /**
70425d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
70525d9e2d1Snpiggin@suse.de  * @inode: inode
7068a549beaSHugh Dickins  * @newsize: new file size
70725d9e2d1Snpiggin@suse.de  *
70825d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
70925d9e2d1Snpiggin@suse.de  * is called.
71025d9e2d1Snpiggin@suse.de  *
71125d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
71225d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
71325d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
71425d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
71525d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
71625d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
71725d9e2d1Snpiggin@suse.de  */
7187caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
71925d9e2d1Snpiggin@suse.de {
72025d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
7218a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
72225d9e2d1Snpiggin@suse.de 
72325d9e2d1Snpiggin@suse.de 	/*
72425d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
72525d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
72625d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
72725d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
72825d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
72925d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
73025d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
73125d9e2d1Snpiggin@suse.de 	 */
7328a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
7338a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
7348a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
73525d9e2d1Snpiggin@suse.de }
73625d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
73725d9e2d1Snpiggin@suse.de 
73825d9e2d1Snpiggin@suse.de /**
7392c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
7402c27c65eSChristoph Hellwig  * @inode: inode
7412c27c65eSChristoph Hellwig  * @newsize: new file size
7422c27c65eSChristoph Hellwig  *
743382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
744382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
745382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
7462c27c65eSChristoph Hellwig  *
74777783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
7489608703eSJan Kara  * i_rwsem but e.g. xfs uses a different lock) and before all filesystem
74977783d06SJan Kara  * specific block truncation has been performed.
7502c27c65eSChristoph Hellwig  */
7512c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
7522c27c65eSChristoph Hellwig {
75390a80202SJan Kara 	loff_t oldsize = inode->i_size;
75490a80202SJan Kara 
7552c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
75690a80202SJan Kara 	if (newsize > oldsize)
75790a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
7587caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
7592c27c65eSChristoph Hellwig }
7602c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
7612c27c65eSChristoph Hellwig 
7622c27c65eSChristoph Hellwig /**
76390a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
76490a80202SJan Kara  * @inode:	inode for which i_size was extended
76590a80202SJan Kara  * @from:	original inode size
76690a80202SJan Kara  * @to:		new inode size
76790a80202SJan Kara  *
76890a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
76990a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
77090a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
77190a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
77290a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
77390a80202SJan Kara  * changed.
77490a80202SJan Kara  *
77590a80202SJan Kara  * The function must be called after i_size is updated so that page fault
77690a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
7779608703eSJan Kara  * The function must be called while we still hold i_rwsem - this not only
77890a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
77990a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
78090a80202SJan Kara  */
78190a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
78290a80202SJan Kara {
78393407472SFabian Frederick 	int bsize = i_blocksize(inode);
78490a80202SJan Kara 	loff_t rounded_from;
78590a80202SJan Kara 	struct page *page;
78690a80202SJan Kara 	pgoff_t index;
78790a80202SJan Kara 
78890a80202SJan Kara 	WARN_ON(to > inode->i_size);
78990a80202SJan Kara 
79009cbfeafSKirill A. Shutemov 	if (from >= to || bsize == PAGE_SIZE)
79190a80202SJan Kara 		return;
79290a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
79390a80202SJan Kara 	rounded_from = round_up(from, bsize);
79409cbfeafSKirill A. Shutemov 	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
79590a80202SJan Kara 		return;
79690a80202SJan Kara 
79709cbfeafSKirill A. Shutemov 	index = from >> PAGE_SHIFT;
79890a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
79990a80202SJan Kara 	/* Page not cached? Nothing to do */
80090a80202SJan Kara 	if (!page)
80190a80202SJan Kara 		return;
80290a80202SJan Kara 	/*
80390a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
80490a80202SJan Kara 	 * is needed.
80590a80202SJan Kara 	 */
80690a80202SJan Kara 	if (page_mkclean(page))
80790a80202SJan Kara 		set_page_dirty(page);
80890a80202SJan Kara 	unlock_page(page);
80909cbfeafSKirill A. Shutemov 	put_page(page);
81090a80202SJan Kara }
81190a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
81290a80202SJan Kara 
81390a80202SJan Kara /**
814623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
815623e3db9SHugh Dickins  * @inode: inode
816623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
817623e3db9SHugh Dickins  * @lend: offset of last byte of hole
818623e3db9SHugh Dickins  *
819623e3db9SHugh Dickins  * This function should typically be called before the filesystem
820623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
821623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
822623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
823623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
824623e3db9SHugh Dickins  * had its underlying blocks deallocated.
825623e3db9SHugh Dickins  */
826623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
827623e3db9SHugh Dickins {
828623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
829623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
830623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
831623e3db9SHugh Dickins 	/*
832623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
833623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
834623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
8355a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
8365a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
837623e3db9SHugh Dickins 	 */
838623e3db9SHugh Dickins 
839623e3db9SHugh Dickins 	/*
840623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
841623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
842623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
843623e3db9SHugh Dickins 	 */
844623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
845623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
846623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
847623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
848623e3db9SHugh Dickins }
849623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
850