xref: /linux/mm/truncate.c (revision 1e84a3d997b74c33491899e31d48774f252213ab)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * mm/truncate.c - code for taking down pages from address_spaces
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds
61da177e4SLinus Torvalds  *
7e1f8e874SFrancois Cami  * 10Sep2002	Andrew Morton
81da177e4SLinus Torvalds  *		Initial version.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
124af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
13f9fe48beSRoss Zwisler #include <linux/dax.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
160fd0e6b0SNick Piggin #include <linux/swap.h>
17b95f1b31SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
1901f2705dSNate Diller #include <linux/highmem.h>
201da177e4SLinus Torvalds #include <linux/pagevec.h>
21e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h>
221da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* grr. try_to_release_page,
23aaa4059bSJan Kara 				   do_invalidatepage */
243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
25c515e1fdSDan Magenheimer #include <linux/cleancache.h>
2690a80202SJan Kara #include <linux/rmap.h>
27ba470de4SRik van Riel #include "internal.h"
281da177e4SLinus Torvalds 
29f2187599SMel Gorman /*
30f2187599SMel Gorman  * Regular page slots are stabilized by the page lock even without the tree
31f2187599SMel Gorman  * itself locked.  These unlocked entries need verification under the tree
32f2187599SMel Gorman  * lock.
33f2187599SMel Gorman  */
34f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping,
35f2187599SMel Gorman 				pgoff_t index, void *entry)
360cd6144aSJohannes Weiner {
3769b6c131SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
38449dd698SJohannes Weiner 
3969b6c131SMatthew Wilcox 	xas_set_update(&xas, workingset_update_node);
4069b6c131SMatthew Wilcox 	if (xas_load(&xas) != entry)
41f2187599SMel Gorman 		return;
4269b6c131SMatthew Wilcox 	xas_store(&xas, NULL);
43f2187599SMel Gorman }
44f2187599SMel Gorman 
45f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
46f2187599SMel Gorman 			       void *entry)
47f2187599SMel Gorman {
4851b8c1feSJohannes Weiner 	spin_lock(&mapping->host->i_lock);
49b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
50f2187599SMel Gorman 	__clear_shadow_entry(mapping, index, entry);
51b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
5251b8c1feSJohannes Weiner 	if (mapping_shrinkable(mapping))
5351b8c1feSJohannes Weiner 		inode_add_lru(mapping->host);
5451b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
550cd6144aSJohannes Weiner }
561da177e4SLinus Torvalds 
57c6dcf52cSJan Kara /*
58f2187599SMel Gorman  * Unconditionally remove exceptional entries. Usually called from truncate
59f2187599SMel Gorman  * path. Note that the pagevec may be altered by this function by removing
60f2187599SMel Gorman  * exceptional entries similar to what pagevec_remove_exceptionals does.
61c6dcf52cSJan Kara  */
62f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping,
6331d270fdSMatthew Wilcox (Oracle) 				struct pagevec *pvec, pgoff_t *indices)
64c6dcf52cSJan Kara {
65f2187599SMel Gorman 	int i, j;
6631d270fdSMatthew Wilcox (Oracle) 	bool dax;
67f2187599SMel Gorman 
68c6dcf52cSJan Kara 	/* Handled by shmem itself */
69c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
70c6dcf52cSJan Kara 		return;
71c6dcf52cSJan Kara 
72f2187599SMel Gorman 	for (j = 0; j < pagevec_count(pvec); j++)
733159f943SMatthew Wilcox 		if (xa_is_value(pvec->pages[j]))
74f2187599SMel Gorman 			break;
75f2187599SMel Gorman 
76f2187599SMel Gorman 	if (j == pagevec_count(pvec))
77c6dcf52cSJan Kara 		return;
78f2187599SMel Gorman 
79f2187599SMel Gorman 	dax = dax_mapping(mapping);
8051b8c1feSJohannes Weiner 	if (!dax) {
8151b8c1feSJohannes Weiner 		spin_lock(&mapping->host->i_lock);
82b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
8351b8c1feSJohannes Weiner 	}
84f2187599SMel Gorman 
85f2187599SMel Gorman 	for (i = j; i < pagevec_count(pvec); i++) {
86f2187599SMel Gorman 		struct page *page = pvec->pages[i];
87f2187599SMel Gorman 		pgoff_t index = indices[i];
88f2187599SMel Gorman 
893159f943SMatthew Wilcox 		if (!xa_is_value(page)) {
90f2187599SMel Gorman 			pvec->pages[j++] = page;
91f2187599SMel Gorman 			continue;
92c6dcf52cSJan Kara 		}
93f2187599SMel Gorman 
94f2187599SMel Gorman 		if (unlikely(dax)) {
95f2187599SMel Gorman 			dax_delete_mapping_entry(mapping, index);
96f2187599SMel Gorman 			continue;
97f2187599SMel Gorman 		}
98f2187599SMel Gorman 
99f2187599SMel Gorman 		__clear_shadow_entry(mapping, index, page);
100f2187599SMel Gorman 	}
101f2187599SMel Gorman 
10251b8c1feSJohannes Weiner 	if (!dax) {
103b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
10451b8c1feSJohannes Weiner 		if (mapping_shrinkable(mapping))
10551b8c1feSJohannes Weiner 			inode_add_lru(mapping->host);
10651b8c1feSJohannes Weiner 		spin_unlock(&mapping->host->i_lock);
10751b8c1feSJohannes Weiner 	}
108f2187599SMel Gorman 	pvec->nr = j;
109c6dcf52cSJan Kara }
110c6dcf52cSJan Kara 
111c6dcf52cSJan Kara /*
112c6dcf52cSJan Kara  * Invalidate exceptional entry if easily possible. This handles exceptional
1134636e70bSRoss Zwisler  * entries for invalidate_inode_pages().
114c6dcf52cSJan Kara  */
115c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping,
116c6dcf52cSJan Kara 					pgoff_t index, void *entry)
117c6dcf52cSJan Kara {
1184636e70bSRoss Zwisler 	/* Handled by shmem itself, or for DAX we do nothing. */
1194636e70bSRoss Zwisler 	if (shmem_mapping(mapping) || dax_mapping(mapping))
120c6dcf52cSJan Kara 		return 1;
121c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
122c6dcf52cSJan Kara 	return 1;
123c6dcf52cSJan Kara }
124c6dcf52cSJan Kara 
125c6dcf52cSJan Kara /*
126c6dcf52cSJan Kara  * Invalidate exceptional entry if clean. This handles exceptional entries for
127c6dcf52cSJan Kara  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
128c6dcf52cSJan Kara  */
129c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping,
130c6dcf52cSJan Kara 					 pgoff_t index, void *entry)
131c6dcf52cSJan Kara {
132c6dcf52cSJan Kara 	/* Handled by shmem itself */
133c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
134c6dcf52cSJan Kara 		return 1;
135c6dcf52cSJan Kara 	if (dax_mapping(mapping))
136c6dcf52cSJan Kara 		return dax_invalidate_mapping_entry_sync(mapping, index);
137c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
138c6dcf52cSJan Kara 	return 1;
139c6dcf52cSJan Kara }
140c6dcf52cSJan Kara 
141cf9a2ae8SDavid Howells /**
14228bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
143cf9a2ae8SDavid Howells  * @page: the page which is affected
144d47992f8SLukas Czerner  * @offset: start of the range to invalidate
145d47992f8SLukas Czerner  * @length: length of the range to invalidate
146cf9a2ae8SDavid Howells  *
147cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
148cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
149cf9a2ae8SDavid Howells  *
150cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
151cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
152cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
153cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
154cf9a2ae8SDavid Howells  * blocks on-disk.
155cf9a2ae8SDavid Howells  */
156d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
157d47992f8SLukas Czerner 		       unsigned int length)
158cf9a2ae8SDavid Howells {
159d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
160d47992f8SLukas Czerner 
161cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
1629361401eSDavid Howells #ifdef CONFIG_BLOCK
163cf9a2ae8SDavid Howells 	if (!invalidatepage)
164cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
1659361401eSDavid Howells #endif
166cf9a2ae8SDavid Howells 	if (invalidatepage)
167d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
168cf9a2ae8SDavid Howells }
169cf9a2ae8SDavid Howells 
170ecdfc978SLinus Torvalds /*
1711da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
17262e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
17354cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1741da177e4SLinus Torvalds  *
175fc3a5ac5SMatthew Wilcox (Oracle)  * We need to bail out if page->mapping is no longer equal to the original
1761da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
177fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1781da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1791da177e4SLinus Torvalds  */
180efe99bbaSMatthew Wilcox (Oracle) static void truncate_cleanup_folio(struct folio *folio)
1811da177e4SLinus Torvalds {
182efe99bbaSMatthew Wilcox (Oracle) 	if (folio_mapped(folio))
1833506659eSMatthew Wilcox (Oracle) 		unmap_mapping_folio(folio);
1841da177e4SLinus Torvalds 
185efe99bbaSMatthew Wilcox (Oracle) 	if (folio_has_private(folio))
186efe99bbaSMatthew Wilcox (Oracle) 		do_invalidatepage(&folio->page, 0, folio_size(folio));
1871da177e4SLinus Torvalds 
188b9ea2515SKonstantin Khlebnikov 	/*
189b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
190b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
191b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
192b9ea2515SKonstantin Khlebnikov 	 */
193efe99bbaSMatthew Wilcox (Oracle) 	folio_cancel_dirty(folio);
194efe99bbaSMatthew Wilcox (Oracle) 	folio_clear_mappedtodisk(folio);
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /*
198fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
1991da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
2000fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
2010fd0e6b0SNick Piggin  * discards clean, unused pages.
2021da177e4SLinus Torvalds  *
2031da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
2041da177e4SLinus Torvalds  */
2051da177e4SLinus Torvalds static int
2061da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
2071da177e4SLinus Torvalds {
2080fd0e6b0SNick Piggin 	int ret;
2090fd0e6b0SNick Piggin 
2101da177e4SLinus Torvalds 	if (page->mapping != mapping)
2111da177e4SLinus Torvalds 		return 0;
2121da177e4SLinus Torvalds 
213266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
2141da177e4SLinus Torvalds 		return 0;
2151da177e4SLinus Torvalds 
2160fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
2170fd0e6b0SNick Piggin 
2180fd0e6b0SNick Piggin 	return ret;
2191da177e4SLinus Torvalds }
2201da177e4SLinus Torvalds 
221*1e84a3d9SMatthew Wilcox (Oracle) int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
222750b4987SNick Piggin {
223*1e84a3d9SMatthew Wilcox (Oracle) 	if (folio->mapping != mapping)
2249f4e41f4SJan Kara 		return -EIO;
2259f4e41f4SJan Kara 
226efe99bbaSMatthew Wilcox (Oracle) 	truncate_cleanup_folio(folio);
227efe99bbaSMatthew Wilcox (Oracle) 	filemap_remove_folio(folio);
2289f4e41f4SJan Kara 	return 0;
229750b4987SNick Piggin }
230750b4987SNick Piggin 
23183f78668SWu Fengguang /*
23225718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
23325718736SAndi Kleen  */
23425718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
23525718736SAndi Kleen {
236*1e84a3d9SMatthew Wilcox (Oracle) 	VM_BUG_ON_PAGE(PageTail(page), page);
237*1e84a3d9SMatthew Wilcox (Oracle) 
23825718736SAndi Kleen 	if (!mapping)
23925718736SAndi Kleen 		return -EINVAL;
24025718736SAndi Kleen 	/*
24125718736SAndi Kleen 	 * Only punch for normal data pages for now.
24225718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
24325718736SAndi Kleen 	 */
24425718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
24525718736SAndi Kleen 		return -EIO;
246*1e84a3d9SMatthew Wilcox (Oracle) 	return truncate_inode_folio(mapping, page_folio(page));
24725718736SAndi Kleen }
24825718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
24925718736SAndi Kleen 
25025718736SAndi Kleen /*
25183f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
25283f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
25383f78668SWu Fengguang  *
25483f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
25583f78668SWu Fengguang  */
25683f78668SWu Fengguang int invalidate_inode_page(struct page *page)
25783f78668SWu Fengguang {
25883f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
25983f78668SWu Fengguang 	if (!mapping)
26083f78668SWu Fengguang 		return 0;
26183f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
26283f78668SWu Fengguang 		return 0;
26383f78668SWu Fengguang 	if (page_mapped(page))
26483f78668SWu Fengguang 		return 0;
26583f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
26683f78668SWu Fengguang }
26783f78668SWu Fengguang 
2681da177e4SLinus Torvalds /**
26973c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2701da177e4SLinus Torvalds  * @mapping: mapping to truncate
2711da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2725a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2731da177e4SLinus Torvalds  *
274d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2755a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2765a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2771da177e4SLinus Torvalds  *
2781da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2791da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2801da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2811da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2821da177e4SLinus Torvalds  * is low.
2831da177e4SLinus Torvalds  *
2841da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2851da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2861da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2875a720394SLukas Czerner  *
2885a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2895a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2905a720394SLukas Czerner  * page aligned properly.
2911da177e4SLinus Torvalds  */
292d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
293d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2941da177e4SLinus Torvalds {
2955a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2965a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2975a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2985a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
2991da177e4SLinus Torvalds 	struct pagevec	pvec;
3000cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
301b85e0effSHugh Dickins 	pgoff_t		index;
3021da177e4SLinus Torvalds 	int		i;
3031da177e4SLinus Torvalds 
3047716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
30534ccb69eSAndrey Ryabinin 		goto out;
3061da177e4SLinus Torvalds 
3075a720394SLukas Czerner 	/* Offsets within partial pages */
30809cbfeafSKirill A. Shutemov 	partial_start = lstart & (PAGE_SIZE - 1);
30909cbfeafSKirill A. Shutemov 	partial_end = (lend + 1) & (PAGE_SIZE - 1);
3105a720394SLukas Czerner 
3115a720394SLukas Czerner 	/*
3125a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
3135a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
3145a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
3155a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
3165a720394SLukas Czerner 	 */
31709cbfeafSKirill A. Shutemov 	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
3185a720394SLukas Czerner 	if (lend == -1)
3195a720394SLukas Czerner 		/*
3205a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
3215a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
3225a720394SLukas Czerner 		 * unsigned we're using -1.
3235a720394SLukas Czerner 		 */
3245a720394SLukas Czerner 		end = -1;
3255a720394SLukas Czerner 	else
32609cbfeafSKirill A. Shutemov 		end = (lend + 1) >> PAGE_SHIFT;
327d7339071SHans Reiser 
32886679820SMel Gorman 	pagevec_init(&pvec);
329b85e0effSHugh Dickins 	index = start;
3305c211ba2SMatthew Wilcox (Oracle) 	while (index < end && find_lock_entries(mapping, index, end - 1,
3315c211ba2SMatthew Wilcox (Oracle) 			&pvec, indices)) {
3325c211ba2SMatthew Wilcox (Oracle) 		index = indices[pagevec_count(&pvec) - 1] + 1;
33331d270fdSMatthew Wilcox (Oracle) 		truncate_exceptional_pvec_entries(mapping, &pvec, indices);
3345c211ba2SMatthew Wilcox (Oracle) 		for (i = 0; i < pagevec_count(&pvec); i++)
335efe99bbaSMatthew Wilcox (Oracle) 			truncate_cleanup_folio(page_folio(pvec.pages[i]));
3365c211ba2SMatthew Wilcox (Oracle) 		delete_from_page_cache_batch(mapping, &pvec);
3375c211ba2SMatthew Wilcox (Oracle) 		for (i = 0; i < pagevec_count(&pvec); i++)
3385c211ba2SMatthew Wilcox (Oracle) 			unlock_page(pvec.pages[i]);
3391da177e4SLinus Torvalds 		pagevec_release(&pvec);
3401da177e4SLinus Torvalds 		cond_resched();
3411da177e4SLinus Torvalds 	}
3425c211ba2SMatthew Wilcox (Oracle) 
3435a720394SLukas Czerner 	if (partial_start) {
3441da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3451da177e4SLinus Torvalds 		if (page) {
34609cbfeafSKirill A. Shutemov 			unsigned int top = PAGE_SIZE;
3475a720394SLukas Czerner 			if (start > end) {
3485a720394SLukas Czerner 				/* Truncation within a single page */
3495a720394SLukas Czerner 				top = partial_end;
3505a720394SLukas Czerner 				partial_end = 0;
3515a720394SLukas Czerner 			}
3521da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3535a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3545a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3555a720394SLukas Czerner 			if (page_has_private(page))
3565a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3575a720394SLukas Czerner 						  top - partial_start);
3581da177e4SLinus Torvalds 			unlock_page(page);
35909cbfeafSKirill A. Shutemov 			put_page(page);
3601da177e4SLinus Torvalds 		}
3611da177e4SLinus Torvalds 	}
3625a720394SLukas Czerner 	if (partial_end) {
3635a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3645a720394SLukas Czerner 		if (page) {
3655a720394SLukas Czerner 			wait_on_page_writeback(page);
3665a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
3675a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3685a720394SLukas Czerner 			if (page_has_private(page))
3695a720394SLukas Czerner 				do_invalidatepage(page, 0,
3705a720394SLukas Czerner 						  partial_end);
3715a720394SLukas Czerner 			unlock_page(page);
37209cbfeafSKirill A. Shutemov 			put_page(page);
3735a720394SLukas Czerner 		}
3745a720394SLukas Czerner 	}
3755a720394SLukas Czerner 	/*
3765a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
3775a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
3785a720394SLukas Czerner 	 */
3795a720394SLukas Czerner 	if (start >= end)
38034ccb69eSAndrey Ryabinin 		goto out;
3811da177e4SLinus Torvalds 
382b85e0effSHugh Dickins 	index = start;
3831da177e4SLinus Torvalds 	for ( ; ; ) {
3841da177e4SLinus Torvalds 		cond_resched();
385a656a202SMatthew Wilcox (Oracle) 		if (!find_get_entries(mapping, index, end - 1, &pvec,
38638cefeb3SMatthew Wilcox (Oracle) 				indices)) {
387792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
388b85e0effSHugh Dickins 			if (index == start)
3891da177e4SLinus Torvalds 				break;
390792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
391b85e0effSHugh Dickins 			index = start;
3921da177e4SLinus Torvalds 			continue;
3931da177e4SLinus Torvalds 		}
394f2187599SMel Gorman 
3951da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3961da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
397*1e84a3d9SMatthew Wilcox (Oracle) 			struct folio *folio;
3981da177e4SLinus Torvalds 
399b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4000cd6144aSJohannes Weiner 			index = indices[i];
401b85e0effSHugh Dickins 
4023159f943SMatthew Wilcox 			if (xa_is_value(page))
4030cd6144aSJohannes Weiner 				continue;
404*1e84a3d9SMatthew Wilcox (Oracle) 			folio = page_folio(page);
4050cd6144aSJohannes Weiner 
406*1e84a3d9SMatthew Wilcox (Oracle) 			folio_lock(folio);
407*1e84a3d9SMatthew Wilcox (Oracle) 			VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
408*1e84a3d9SMatthew Wilcox (Oracle) 			folio_wait_writeback(folio);
409*1e84a3d9SMatthew Wilcox (Oracle) 			truncate_inode_folio(mapping, folio);
410*1e84a3d9SMatthew Wilcox (Oracle) 			folio_unlock(folio);
4111da177e4SLinus Torvalds 		}
41231d270fdSMatthew Wilcox (Oracle) 		truncate_exceptional_pvec_entries(mapping, &pvec, indices);
4131da177e4SLinus Torvalds 		pagevec_release(&pvec);
414b85e0effSHugh Dickins 		index++;
4151da177e4SLinus Torvalds 	}
41634ccb69eSAndrey Ryabinin 
41734ccb69eSAndrey Ryabinin out:
4183167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
4191da177e4SLinus Torvalds }
420d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
4211da177e4SLinus Torvalds 
422d7339071SHans Reiser /**
423d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
424d7339071SHans Reiser  * @mapping: mapping to truncate
425d7339071SHans Reiser  * @lstart: offset from which to truncate
426d7339071SHans Reiser  *
427730633f0SJan Kara  * Called under (and serialised by) inode->i_rwsem and
428730633f0SJan Kara  * mapping->invalidate_lock.
42908142579SJan Kara  *
43008142579SJan Kara  * Note: When this function returns, there can be a page in the process of
43108142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
43208142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
43308142579SJan Kara  * truncation of the whole mapping.
434d7339071SHans Reiser  */
435d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
436d7339071SHans Reiser {
437d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
438d7339071SHans Reiser }
4391da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4401da177e4SLinus Torvalds 
44128697355SMike Waychison /**
44291b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
44391b0abe3SJohannes Weiner  * @mapping: mapping to truncate
44491b0abe3SJohannes Weiner  *
4459608703eSJan Kara  * Called under (and serialized by) inode->i_rwsem.
44691b0abe3SJohannes Weiner  *
44791b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
44891b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
44991b0abe3SJohannes Weiner  */
45091b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
45191b0abe3SJohannes Weiner {
45291b0abe3SJohannes Weiner 	/*
45391b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
45491b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
45591b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
45691b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
45791b0abe3SJohannes Weiner 	 * final truncate has begun.
45891b0abe3SJohannes Weiner 	 */
45991b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
46091b0abe3SJohannes Weiner 
4617716506aSMatthew Wilcox (Oracle) 	if (!mapping_empty(mapping)) {
46291b0abe3SJohannes Weiner 		/*
46391b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
46491b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
46591b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
46691b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
46791b0abe3SJohannes Weiner 		 */
468b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
469b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
47091b0abe3SJohannes Weiner 	}
4716ff38bd4SPavel Tikhomirov 
4726ff38bd4SPavel Tikhomirov 	/*
4736ff38bd4SPavel Tikhomirov 	 * Cleancache needs notification even if there are no pages or shadow
4746ff38bd4SPavel Tikhomirov 	 * entries.
4756ff38bd4SPavel Tikhomirov 	 */
4766ff38bd4SPavel Tikhomirov 	truncate_inode_pages(mapping, 0);
47791b0abe3SJohannes Weiner }
47891b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
47991b0abe3SJohannes Weiner 
480a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping,
481eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
4821da177e4SLinus Torvalds {
4830cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
4841da177e4SLinus Torvalds 	struct pagevec pvec;
485b85e0effSHugh Dickins 	pgoff_t index = start;
48631560180SMinchan Kim 	unsigned long ret;
48731560180SMinchan Kim 	unsigned long count = 0;
4881da177e4SLinus Torvalds 	int i;
4891da177e4SLinus Torvalds 
49086679820SMel Gorman 	pagevec_init(&pvec);
4915c211ba2SMatthew Wilcox (Oracle) 	while (find_lock_entries(mapping, index, end, &pvec, indices)) {
4921da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
4931da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
4941da177e4SLinus Torvalds 
495b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4960cd6144aSJohannes Weiner 			index = indices[i];
497e0f23603SNeilBrown 
4983159f943SMatthew Wilcox 			if (xa_is_value(page)) {
4997ae12c80SJohannes Weiner 				count += invalidate_exceptional_entry(mapping,
5007ae12c80SJohannes Weiner 								      index,
501c6dcf52cSJan Kara 								      page);
5020cd6144aSJohannes Weiner 				continue;
5030cd6144aSJohannes Weiner 			}
5045c211ba2SMatthew Wilcox (Oracle) 			index += thp_nr_pages(page) - 1;
505fc127da0SKirill A. Shutemov 
50631560180SMinchan Kim 			ret = invalidate_inode_page(page);
5071da177e4SLinus Torvalds 			unlock_page(page);
50831560180SMinchan Kim 			/*
50931560180SMinchan Kim 			 * Invalidation is a hint that the page is no longer
51031560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
51131560180SMinchan Kim 			 */
512eb1d7a65SYafang Shao 			if (!ret) {
513cc5993bdSMinchan Kim 				deactivate_file_page(page);
514eb1d7a65SYafang Shao 				/* It is likely on the pagevec of a remote CPU */
515eb1d7a65SYafang Shao 				if (nr_pagevec)
516eb1d7a65SYafang Shao 					(*nr_pagevec)++;
517eb1d7a65SYafang Shao 			}
51831560180SMinchan Kim 			count += ret;
5191da177e4SLinus Torvalds 		}
5200cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
5211da177e4SLinus Torvalds 		pagevec_release(&pvec);
522fc9a07e7SAndrew Morton 		cond_resched();
523b85e0effSHugh Dickins 		index++;
5241da177e4SLinus Torvalds 	}
52531560180SMinchan Kim 	return count;
5261da177e4SLinus Torvalds }
527eb1d7a65SYafang Shao 
528eb1d7a65SYafang Shao /**
5297ae12c80SJohannes Weiner  * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode
5307ae12c80SJohannes Weiner  * @mapping: the address_space which holds the cache to invalidate
531eb1d7a65SYafang Shao  * @start: the offset 'from' which to invalidate
532eb1d7a65SYafang Shao  * @end: the offset 'to' which to invalidate (inclusive)
533eb1d7a65SYafang Shao  *
5347ae12c80SJohannes Weiner  * This function removes pages that are clean, unmapped and unlocked,
5357ae12c80SJohannes Weiner  * as well as shadow entries. It will not block on IO activity.
536eb1d7a65SYafang Shao  *
5377ae12c80SJohannes Weiner  * If you want to remove all the pages of one inode, regardless of
5387ae12c80SJohannes Weiner  * their use and writeback state, use truncate_inode_pages().
539eb1d7a65SYafang Shao  *
5407ae12c80SJohannes Weiner  * Return: the number of the cache entries that were invalidated
541eb1d7a65SYafang Shao  */
542eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping,
543eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end)
544eb1d7a65SYafang Shao {
545eb1d7a65SYafang Shao 	return __invalidate_mapping_pages(mapping, start, end, NULL);
546eb1d7a65SYafang Shao }
54754bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
5481da177e4SLinus Torvalds 
549eb1d7a65SYafang Shao /**
550649c6dfeSAlex Shi  * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode
551649c6dfeSAlex Shi  * @mapping: the address_space which holds the pages to invalidate
552649c6dfeSAlex Shi  * @start: the offset 'from' which to invalidate
553649c6dfeSAlex Shi  * @end: the offset 'to' which to invalidate (inclusive)
554649c6dfeSAlex Shi  * @nr_pagevec: invalidate failed page number for caller
555649c6dfeSAlex Shi  *
556a00cda3fSMauro Carvalho Chehab  * This helper is similar to invalidate_mapping_pages(), except that it accounts
557a00cda3fSMauro Carvalho Chehab  * for pages that are likely on a pagevec and counts them in @nr_pagevec, which
558a00cda3fSMauro Carvalho Chehab  * will be used by the caller.
559eb1d7a65SYafang Shao  */
560eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping,
561eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
562eb1d7a65SYafang Shao {
563eb1d7a65SYafang Shao 	__invalidate_mapping_pages(mapping, start, end, nr_pagevec);
564eb1d7a65SYafang Shao }
565eb1d7a65SYafang Shao 
566bd4c8ce4SAndrew Morton /*
567bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
568bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
569bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
5702706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
5712706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
572bd4c8ce4SAndrew Morton  */
573bd4c8ce4SAndrew Morton static int
574bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
575bd4c8ce4SAndrew Morton {
576bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
577bd4c8ce4SAndrew Morton 		return 0;
578bd4c8ce4SAndrew Morton 
579266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
580bd4c8ce4SAndrew Morton 		return 0;
581bd4c8ce4SAndrew Morton 
58251b8c1feSJohannes Weiner 	spin_lock(&mapping->host->i_lock);
58330472509SJohannes Weiner 	xa_lock_irq(&mapping->i_pages);
584bd4c8ce4SAndrew Morton 	if (PageDirty(page))
585bd4c8ce4SAndrew Morton 		goto failed;
586bd4c8ce4SAndrew Morton 
587266cf658SDavid Howells 	BUG_ON(page_has_private(page));
58862cccb8cSJohannes Weiner 	__delete_from_page_cache(page, NULL);
58930472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
59051b8c1feSJohannes Weiner 	if (mapping_shrinkable(mapping))
59151b8c1feSJohannes Weiner 		inode_add_lru(mapping->host);
59251b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
5936072d13cSLinus Torvalds 
5946072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
5956072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
5966072d13cSLinus Torvalds 
59709cbfeafSKirill A. Shutemov 	put_page(page);	/* pagecache ref */
598bd4c8ce4SAndrew Morton 	return 1;
599bd4c8ce4SAndrew Morton failed:
60030472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
60151b8c1feSJohannes Weiner 	spin_unlock(&mapping->host->i_lock);
602bd4c8ce4SAndrew Morton 	return 0;
603bd4c8ce4SAndrew Morton }
604bd4c8ce4SAndrew Morton 
605e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
606e3db7691STrond Myklebust {
607e3db7691STrond Myklebust 	if (!PageDirty(page))
608e3db7691STrond Myklebust 		return 0;
609e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
610e3db7691STrond Myklebust 		return 0;
611e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
612e3db7691STrond Myklebust }
613e3db7691STrond Myklebust 
6141da177e4SLinus Torvalds /**
6151da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
61667be2dd1SMartin Waitz  * @mapping: the address_space
6171da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
6181da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
6191da177e4SLinus Torvalds  *
6201da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6211da177e4SLinus Torvalds  * invalidation.
6221da177e4SLinus Torvalds  *
623a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
6241da177e4SLinus Torvalds  */
6251da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
6261da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
6271da177e4SLinus Torvalds {
6280cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
6291da177e4SLinus Torvalds 	struct pagevec pvec;
630b85e0effSHugh Dickins 	pgoff_t index;
6311da177e4SLinus Torvalds 	int i;
6321da177e4SLinus Torvalds 	int ret = 0;
6330dd1334fSHisashi Hifumi 	int ret2 = 0;
6341da177e4SLinus Torvalds 	int did_range_unmap = 0;
6351da177e4SLinus Torvalds 
6367716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
63734ccb69eSAndrey Ryabinin 		goto out;
63832691f0fSAndrey Ryabinin 
63986679820SMel Gorman 	pagevec_init(&pvec);
640b85e0effSHugh Dickins 	index = start;
641a656a202SMatthew Wilcox (Oracle) 	while (find_get_entries(mapping, index, end, &pvec, indices)) {
6427b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
6431da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
644b85e0effSHugh Dickins 
645b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
6460cd6144aSJohannes Weiner 			index = indices[i];
6471da177e4SLinus Torvalds 
6483159f943SMatthew Wilcox 			if (xa_is_value(page)) {
649c6dcf52cSJan Kara 				if (!invalidate_exceptional_entry2(mapping,
650c6dcf52cSJan Kara 								   index, page))
651c6dcf52cSJan Kara 					ret = -EBUSY;
6520cd6144aSJohannes Weiner 				continue;
6530cd6144aSJohannes Weiner 			}
6540cd6144aSJohannes Weiner 
65522061a1fSHugh Dickins 			if (!did_range_unmap && page_mapped(page)) {
65622061a1fSHugh Dickins 				/*
65722061a1fSHugh Dickins 				 * If page is mapped, before taking its lock,
65822061a1fSHugh Dickins 				 * zap the rest of the file in one hit.
65922061a1fSHugh Dickins 				 */
66022061a1fSHugh Dickins 				unmap_mapping_pages(mapping, index,
66122061a1fSHugh Dickins 						(1 + end - index), false);
66222061a1fSHugh Dickins 				did_range_unmap = 1;
66322061a1fSHugh Dickins 			}
66422061a1fSHugh Dickins 
6651da177e4SLinus Torvalds 			lock_page(page);
6665cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
6671da177e4SLinus Torvalds 			if (page->mapping != mapping) {
6681da177e4SLinus Torvalds 				unlock_page(page);
6691da177e4SLinus Torvalds 				continue;
6701da177e4SLinus Torvalds 			}
6711da177e4SLinus Torvalds 			wait_on_page_writeback(page);
67222061a1fSHugh Dickins 
67322061a1fSHugh Dickins 			if (page_mapped(page))
6743506659eSMatthew Wilcox (Oracle) 				unmap_mapping_folio(page_folio(page));
675d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
67622061a1fSHugh Dickins 
6770dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
6780dd1334fSHisashi Hifumi 			if (ret2 == 0) {
6790dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
6806ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
6810dd1334fSHisashi Hifumi 			}
6820dd1334fSHisashi Hifumi 			if (ret2 < 0)
6830dd1334fSHisashi Hifumi 				ret = ret2;
6841da177e4SLinus Torvalds 			unlock_page(page);
6851da177e4SLinus Torvalds 		}
6860cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6871da177e4SLinus Torvalds 		pagevec_release(&pvec);
6881da177e4SLinus Torvalds 		cond_resched();
689b85e0effSHugh Dickins 		index++;
6901da177e4SLinus Torvalds 	}
691cd656375SJan Kara 	/*
69269b6c131SMatthew Wilcox 	 * For DAX we invalidate page tables after invalidating page cache.  We
693cd656375SJan Kara 	 * could invalidate page tables while invalidating each entry however
694cd656375SJan Kara 	 * that would be expensive. And doing range unmapping before doesn't
69569b6c131SMatthew Wilcox 	 * work as we have no cheap way to find whether page cache entry didn't
696cd656375SJan Kara 	 * get remapped later.
697cd656375SJan Kara 	 */
698cd656375SJan Kara 	if (dax_mapping(mapping)) {
699977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, start, end - start + 1, false);
700cd656375SJan Kara 	}
70134ccb69eSAndrey Ryabinin out:
7023167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
7031da177e4SLinus Torvalds 	return ret;
7041da177e4SLinus Torvalds }
7051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds /**
7081da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
70967be2dd1SMartin Waitz  * @mapping: the address_space
7101da177e4SLinus Torvalds  *
7111da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
7121da177e4SLinus Torvalds  * invalidation.
7131da177e4SLinus Torvalds  *
714a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
7151da177e4SLinus Torvalds  */
7161da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
7171da177e4SLinus Torvalds {
7181da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
7191da177e4SLinus Torvalds }
7201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
72125d9e2d1Snpiggin@suse.de 
72225d9e2d1Snpiggin@suse.de /**
72325d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
72425d9e2d1Snpiggin@suse.de  * @inode: inode
7258a549beaSHugh Dickins  * @newsize: new file size
72625d9e2d1Snpiggin@suse.de  *
72725d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
72825d9e2d1Snpiggin@suse.de  * is called.
72925d9e2d1Snpiggin@suse.de  *
73025d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
73125d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
73225d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
73325d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
73425d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
73525d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
73625d9e2d1Snpiggin@suse.de  */
7377caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
73825d9e2d1Snpiggin@suse.de {
73925d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
7408a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
74125d9e2d1Snpiggin@suse.de 
74225d9e2d1Snpiggin@suse.de 	/*
74325d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
74425d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
74525d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
74625d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
74725d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
74825d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
74925d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
75025d9e2d1Snpiggin@suse.de 	 */
7518a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
7528a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
7538a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
75425d9e2d1Snpiggin@suse.de }
75525d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
75625d9e2d1Snpiggin@suse.de 
75725d9e2d1Snpiggin@suse.de /**
7582c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
7592c27c65eSChristoph Hellwig  * @inode: inode
7602c27c65eSChristoph Hellwig  * @newsize: new file size
7612c27c65eSChristoph Hellwig  *
762382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
763382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
764382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
7652c27c65eSChristoph Hellwig  *
76677783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
7679608703eSJan Kara  * i_rwsem but e.g. xfs uses a different lock) and before all filesystem
76877783d06SJan Kara  * specific block truncation has been performed.
7692c27c65eSChristoph Hellwig  */
7702c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
7712c27c65eSChristoph Hellwig {
77290a80202SJan Kara 	loff_t oldsize = inode->i_size;
77390a80202SJan Kara 
7742c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
77590a80202SJan Kara 	if (newsize > oldsize)
77690a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
7777caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
7782c27c65eSChristoph Hellwig }
7792c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
7802c27c65eSChristoph Hellwig 
7812c27c65eSChristoph Hellwig /**
78290a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
78390a80202SJan Kara  * @inode:	inode for which i_size was extended
78490a80202SJan Kara  * @from:	original inode size
78590a80202SJan Kara  * @to:		new inode size
78690a80202SJan Kara  *
78790a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
78890a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
78990a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
79090a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
79190a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
79290a80202SJan Kara  * changed.
79390a80202SJan Kara  *
79490a80202SJan Kara  * The function must be called after i_size is updated so that page fault
79590a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
7969608703eSJan Kara  * The function must be called while we still hold i_rwsem - this not only
79790a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
79890a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
79990a80202SJan Kara  */
80090a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
80190a80202SJan Kara {
80293407472SFabian Frederick 	int bsize = i_blocksize(inode);
80390a80202SJan Kara 	loff_t rounded_from;
80490a80202SJan Kara 	struct page *page;
80590a80202SJan Kara 	pgoff_t index;
80690a80202SJan Kara 
80790a80202SJan Kara 	WARN_ON(to > inode->i_size);
80890a80202SJan Kara 
80909cbfeafSKirill A. Shutemov 	if (from >= to || bsize == PAGE_SIZE)
81090a80202SJan Kara 		return;
81190a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
81290a80202SJan Kara 	rounded_from = round_up(from, bsize);
81309cbfeafSKirill A. Shutemov 	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
81490a80202SJan Kara 		return;
81590a80202SJan Kara 
81609cbfeafSKirill A. Shutemov 	index = from >> PAGE_SHIFT;
81790a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
81890a80202SJan Kara 	/* Page not cached? Nothing to do */
81990a80202SJan Kara 	if (!page)
82090a80202SJan Kara 		return;
82190a80202SJan Kara 	/*
82290a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
82390a80202SJan Kara 	 * is needed.
82490a80202SJan Kara 	 */
82590a80202SJan Kara 	if (page_mkclean(page))
82690a80202SJan Kara 		set_page_dirty(page);
82790a80202SJan Kara 	unlock_page(page);
82809cbfeafSKirill A. Shutemov 	put_page(page);
82990a80202SJan Kara }
83090a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
83190a80202SJan Kara 
83290a80202SJan Kara /**
833623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
834623e3db9SHugh Dickins  * @inode: inode
835623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
836623e3db9SHugh Dickins  * @lend: offset of last byte of hole
837623e3db9SHugh Dickins  *
838623e3db9SHugh Dickins  * This function should typically be called before the filesystem
839623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
840623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
841623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
842623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
843623e3db9SHugh Dickins  * had its underlying blocks deallocated.
844623e3db9SHugh Dickins  */
845623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
846623e3db9SHugh Dickins {
847623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
848623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
849623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
850623e3db9SHugh Dickins 	/*
851623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
852623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
853623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
8545a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
8555a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
856623e3db9SHugh Dickins 	 */
857623e3db9SHugh Dickins 
858623e3db9SHugh Dickins 	/*
859623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
860623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
861623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
862623e3db9SHugh Dickins 	 */
863623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
864623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
865623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
866623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
867623e3db9SHugh Dickins }
868623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
869