xref: /linux/mm/truncate.c (revision 977fbdcd5986c9ff700bf276644d2b1973a53348)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/truncate.c - code for taking down pages from address_spaces
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds
51da177e4SLinus Torvalds  *
6e1f8e874SFrancois Cami  * 10Sep2002	Andrew Morton
71da177e4SLinus Torvalds  *		Initial version.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/kernel.h>
114af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
12f9fe48beSRoss Zwisler #include <linux/dax.h>
135a0e3ad6STejun Heo #include <linux/gfp.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
150fd0e6b0SNick Piggin #include <linux/swap.h>
16b95f1b31SPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
1801f2705dSNate Diller #include <linux/highmem.h>
191da177e4SLinus Torvalds #include <linux/pagevec.h>
20e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h>
211da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* grr. try_to_release_page,
22aaa4059bSJan Kara 				   do_invalidatepage */
233a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
24c515e1fdSDan Magenheimer #include <linux/cleancache.h>
2590a80202SJan Kara #include <linux/rmap.h>
26ba470de4SRik van Riel #include "internal.h"
271da177e4SLinus Torvalds 
28f2187599SMel Gorman /*
29f2187599SMel Gorman  * Regular page slots are stabilized by the page lock even without the tree
30f2187599SMel Gorman  * itself locked.  These unlocked entries need verification under the tree
31f2187599SMel Gorman  * lock.
32f2187599SMel Gorman  */
33f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping,
34f2187599SMel Gorman 				pgoff_t index, void *entry)
350cd6144aSJohannes Weiner {
36449dd698SJohannes Weiner 	struct radix_tree_node *node;
37449dd698SJohannes Weiner 	void **slot;
38449dd698SJohannes Weiner 
3914b46879SJohannes Weiner 	if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
40f2187599SMel Gorman 		return;
41449dd698SJohannes Weiner 	if (*slot != entry)
42f2187599SMel Gorman 		return;
4314b46879SJohannes Weiner 	__radix_tree_replace(&mapping->page_tree, node, slot, NULL,
44c7df8ad2SMel Gorman 			     workingset_update_node);
45f9fe48beSRoss Zwisler 	mapping->nrexceptional--;
46f2187599SMel Gorman }
47f2187599SMel Gorman 
48f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
49f2187599SMel Gorman 			       void *entry)
50f2187599SMel Gorman {
51f2187599SMel Gorman 	spin_lock_irq(&mapping->tree_lock);
52f2187599SMel Gorman 	__clear_shadow_entry(mapping, index, entry);
530cd6144aSJohannes Weiner 	spin_unlock_irq(&mapping->tree_lock);
540cd6144aSJohannes Weiner }
551da177e4SLinus Torvalds 
56c6dcf52cSJan Kara /*
57f2187599SMel Gorman  * Unconditionally remove exceptional entries. Usually called from truncate
58f2187599SMel Gorman  * path. Note that the pagevec may be altered by this function by removing
59f2187599SMel Gorman  * exceptional entries similar to what pagevec_remove_exceptionals does.
60c6dcf52cSJan Kara  */
61f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping,
62f2187599SMel Gorman 				struct pagevec *pvec, pgoff_t *indices,
63f2187599SMel Gorman 				pgoff_t end)
64c6dcf52cSJan Kara {
65f2187599SMel Gorman 	int i, j;
66f2187599SMel Gorman 	bool dax, lock;
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++)
73f2187599SMel Gorman 		if (radix_tree_exceptional_entry(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);
80f2187599SMel Gorman 	lock = !dax && indices[j] < end;
81f2187599SMel Gorman 	if (lock)
82f2187599SMel Gorman 		spin_lock_irq(&mapping->tree_lock);
83f2187599SMel Gorman 
84f2187599SMel Gorman 	for (i = j; i < pagevec_count(pvec); i++) {
85f2187599SMel Gorman 		struct page *page = pvec->pages[i];
86f2187599SMel Gorman 		pgoff_t index = indices[i];
87f2187599SMel Gorman 
88f2187599SMel Gorman 		if (!radix_tree_exceptional_entry(page)) {
89f2187599SMel Gorman 			pvec->pages[j++] = page;
90f2187599SMel Gorman 			continue;
91c6dcf52cSJan Kara 		}
92f2187599SMel Gorman 
93f2187599SMel Gorman 		if (index >= end)
94f2187599SMel Gorman 			continue;
95f2187599SMel Gorman 
96f2187599SMel Gorman 		if (unlikely(dax)) {
97f2187599SMel Gorman 			dax_delete_mapping_entry(mapping, index);
98f2187599SMel Gorman 			continue;
99f2187599SMel Gorman 		}
100f2187599SMel Gorman 
101f2187599SMel Gorman 		__clear_shadow_entry(mapping, index, page);
102f2187599SMel Gorman 	}
103f2187599SMel Gorman 
104f2187599SMel Gorman 	if (lock)
105f2187599SMel Gorman 		spin_unlock_irq(&mapping->tree_lock);
106f2187599SMel Gorman 	pvec->nr = j;
107c6dcf52cSJan Kara }
108c6dcf52cSJan Kara 
109c6dcf52cSJan Kara /*
110c6dcf52cSJan Kara  * Invalidate exceptional entry if easily possible. This handles exceptional
1114636e70bSRoss Zwisler  * entries for invalidate_inode_pages().
112c6dcf52cSJan Kara  */
113c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping,
114c6dcf52cSJan Kara 					pgoff_t index, void *entry)
115c6dcf52cSJan Kara {
1164636e70bSRoss Zwisler 	/* Handled by shmem itself, or for DAX we do nothing. */
1174636e70bSRoss Zwisler 	if (shmem_mapping(mapping) || dax_mapping(mapping))
118c6dcf52cSJan Kara 		return 1;
119c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
120c6dcf52cSJan Kara 	return 1;
121c6dcf52cSJan Kara }
122c6dcf52cSJan Kara 
123c6dcf52cSJan Kara /*
124c6dcf52cSJan Kara  * Invalidate exceptional entry if clean. This handles exceptional entries for
125c6dcf52cSJan Kara  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
126c6dcf52cSJan Kara  */
127c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping,
128c6dcf52cSJan Kara 					 pgoff_t index, void *entry)
129c6dcf52cSJan Kara {
130c6dcf52cSJan Kara 	/* Handled by shmem itself */
131c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
132c6dcf52cSJan Kara 		return 1;
133c6dcf52cSJan Kara 	if (dax_mapping(mapping))
134c6dcf52cSJan Kara 		return dax_invalidate_mapping_entry_sync(mapping, index);
135c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
136c6dcf52cSJan Kara 	return 1;
137c6dcf52cSJan Kara }
138c6dcf52cSJan Kara 
139cf9a2ae8SDavid Howells /**
14028bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
141cf9a2ae8SDavid Howells  * @page: the page which is affected
142d47992f8SLukas Czerner  * @offset: start of the range to invalidate
143d47992f8SLukas Czerner  * @length: length of the range to invalidate
144cf9a2ae8SDavid Howells  *
145cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
146cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
147cf9a2ae8SDavid Howells  *
148cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
149cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
150cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
151cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
152cf9a2ae8SDavid Howells  * blocks on-disk.
153cf9a2ae8SDavid Howells  */
154d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
155d47992f8SLukas Czerner 		       unsigned int length)
156cf9a2ae8SDavid Howells {
157d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
158d47992f8SLukas Czerner 
159cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
1609361401eSDavid Howells #ifdef CONFIG_BLOCK
161cf9a2ae8SDavid Howells 	if (!invalidatepage)
162cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
1639361401eSDavid Howells #endif
164cf9a2ae8SDavid Howells 	if (invalidatepage)
165d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
166cf9a2ae8SDavid Howells }
167cf9a2ae8SDavid Howells 
168ecdfc978SLinus Torvalds /*
1691da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
17062e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
17154cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1721da177e4SLinus Torvalds  *
1731da177e4SLinus Torvalds  * We need to bale out if page->mapping is no longer equal to the original
1741da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
175fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1761da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1771da177e4SLinus Torvalds  */
1789f4e41f4SJan Kara static void
1799f4e41f4SJan Kara truncate_cleanup_page(struct address_space *mapping, struct page *page)
1801da177e4SLinus Torvalds {
1819f4e41f4SJan Kara 	if (page_mapped(page)) {
182*977fbdcdSMatthew Wilcox 		pgoff_t nr = PageTransHuge(page) ? HPAGE_PMD_NR : 1;
183*977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, page->index, nr, false);
1849f4e41f4SJan Kara 	}
1851da177e4SLinus Torvalds 
186266cf658SDavid Howells 	if (page_has_private(page))
18709cbfeafSKirill A. Shutemov 		do_invalidatepage(page, 0, PAGE_SIZE);
1881da177e4SLinus Torvalds 
189b9ea2515SKonstantin Khlebnikov 	/*
190b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
191b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
192b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
193b9ea2515SKonstantin Khlebnikov 	 */
19411f81becSTejun Heo 	cancel_dirty_page(page);
1951da177e4SLinus Torvalds 	ClearPageMappedToDisk(page);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds /*
199fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
2001da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
2010fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
2020fd0e6b0SNick Piggin  * discards clean, unused pages.
2031da177e4SLinus Torvalds  *
2041da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
2051da177e4SLinus Torvalds  */
2061da177e4SLinus Torvalds static int
2071da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
2081da177e4SLinus Torvalds {
2090fd0e6b0SNick Piggin 	int ret;
2100fd0e6b0SNick Piggin 
2111da177e4SLinus Torvalds 	if (page->mapping != mapping)
2121da177e4SLinus Torvalds 		return 0;
2131da177e4SLinus Torvalds 
214266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
2151da177e4SLinus Torvalds 		return 0;
2161da177e4SLinus Torvalds 
2170fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
2180fd0e6b0SNick Piggin 
2190fd0e6b0SNick Piggin 	return ret;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
222750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page)
223750b4987SNick Piggin {
224fc127da0SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageTail(page), page);
225fc127da0SKirill A. Shutemov 
2269f4e41f4SJan Kara 	if (page->mapping != mapping)
2279f4e41f4SJan Kara 		return -EIO;
2289f4e41f4SJan Kara 
2299f4e41f4SJan Kara 	truncate_cleanup_page(mapping, page);
2309f4e41f4SJan Kara 	delete_from_page_cache(page);
2319f4e41f4SJan Kara 	return 0;
232750b4987SNick Piggin }
233750b4987SNick Piggin 
23483f78668SWu Fengguang /*
23525718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
23625718736SAndi Kleen  */
23725718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
23825718736SAndi Kleen {
23925718736SAndi Kleen 	if (!mapping)
24025718736SAndi Kleen 		return -EINVAL;
24125718736SAndi Kleen 	/*
24225718736SAndi Kleen 	 * Only punch for normal data pages for now.
24325718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
24425718736SAndi Kleen 	 */
24525718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
24625718736SAndi Kleen 		return -EIO;
24725718736SAndi Kleen 	return truncate_inode_page(mapping, page);
24825718736SAndi Kleen }
24925718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
25025718736SAndi Kleen 
25125718736SAndi Kleen /*
25283f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
25383f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
25483f78668SWu Fengguang  *
25583f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
25683f78668SWu Fengguang  */
25783f78668SWu Fengguang int invalidate_inode_page(struct page *page)
25883f78668SWu Fengguang {
25983f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
26083f78668SWu Fengguang 	if (!mapping)
26183f78668SWu Fengguang 		return 0;
26283f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
26383f78668SWu Fengguang 		return 0;
26483f78668SWu Fengguang 	if (page_mapped(page))
26583f78668SWu Fengguang 		return 0;
26683f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
26783f78668SWu Fengguang }
26883f78668SWu Fengguang 
2691da177e4SLinus Torvalds /**
27073c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2711da177e4SLinus Torvalds  * @mapping: mapping to truncate
2721da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2735a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2741da177e4SLinus Torvalds  *
275d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2765a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2775a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2781da177e4SLinus Torvalds  *
2791da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2801da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2811da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2821da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2831da177e4SLinus Torvalds  * is low.
2841da177e4SLinus Torvalds  *
2851da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2861da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2871da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2885a720394SLukas Czerner  *
2895a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2905a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2915a720394SLukas Czerner  * page aligned properly.
2921da177e4SLinus Torvalds  */
293d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
294d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2951da177e4SLinus Torvalds {
2965a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2975a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2985a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2995a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
3001da177e4SLinus Torvalds 	struct pagevec	pvec;
3010cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
302b85e0effSHugh Dickins 	pgoff_t		index;
3031da177e4SLinus Torvalds 	int		i;
3041da177e4SLinus Torvalds 
305f9fe48beSRoss Zwisler 	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
30634ccb69eSAndrey Ryabinin 		goto out;
3071da177e4SLinus Torvalds 
3085a720394SLukas Czerner 	/* Offsets within partial pages */
30909cbfeafSKirill A. Shutemov 	partial_start = lstart & (PAGE_SIZE - 1);
31009cbfeafSKirill A. Shutemov 	partial_end = (lend + 1) & (PAGE_SIZE - 1);
3115a720394SLukas Czerner 
3125a720394SLukas Czerner 	/*
3135a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
3145a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
3155a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
3165a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
3175a720394SLukas Czerner 	 */
31809cbfeafSKirill A. Shutemov 	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
3195a720394SLukas Czerner 	if (lend == -1)
3205a720394SLukas Czerner 		/*
3215a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
3225a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
3235a720394SLukas Czerner 		 * unsigned we're using -1.
3245a720394SLukas Czerner 		 */
3255a720394SLukas Czerner 		end = -1;
3265a720394SLukas Czerner 	else
32709cbfeafSKirill A. Shutemov 		end = (lend + 1) >> PAGE_SHIFT;
328d7339071SHans Reiser 
32986679820SMel Gorman 	pagevec_init(&pvec);
330b85e0effSHugh Dickins 	index = start;
3310cd6144aSJohannes Weiner 	while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
3320cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
3330cd6144aSJohannes Weiner 			indices)) {
334aa65c29cSJan Kara 		/*
335aa65c29cSJan Kara 		 * Pagevec array has exceptional entries and we may also fail
336aa65c29cSJan Kara 		 * to lock some pages. So we store pages that can be deleted
337aa65c29cSJan Kara 		 * in a new pagevec.
338aa65c29cSJan Kara 		 */
339aa65c29cSJan Kara 		struct pagevec locked_pvec;
340aa65c29cSJan Kara 
34186679820SMel Gorman 		pagevec_init(&locked_pvec);
3421da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3431da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
3441da177e4SLinus Torvalds 
345b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
3460cd6144aSJohannes Weiner 			index = indices[i];
3475a720394SLukas Czerner 			if (index >= end)
348d7339071SHans Reiser 				break;
349d7339071SHans Reiser 
350f2187599SMel Gorman 			if (radix_tree_exceptional_entry(page))
3510cd6144aSJohannes Weiner 				continue;
3520cd6144aSJohannes Weiner 
353529ae9aaSNick Piggin 			if (!trylock_page(page))
3541da177e4SLinus Torvalds 				continue;
3555cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
3561da177e4SLinus Torvalds 			if (PageWriteback(page)) {
3571da177e4SLinus Torvalds 				unlock_page(page);
3581da177e4SLinus Torvalds 				continue;
3591da177e4SLinus Torvalds 			}
360aa65c29cSJan Kara 			if (page->mapping != mapping) {
3611da177e4SLinus Torvalds 				unlock_page(page);
362aa65c29cSJan Kara 				continue;
3631da177e4SLinus Torvalds 			}
364aa65c29cSJan Kara 			pagevec_add(&locked_pvec, page);
365aa65c29cSJan Kara 		}
366aa65c29cSJan Kara 		for (i = 0; i < pagevec_count(&locked_pvec); i++)
367aa65c29cSJan Kara 			truncate_cleanup_page(mapping, locked_pvec.pages[i]);
368aa65c29cSJan Kara 		delete_from_page_cache_batch(mapping, &locked_pvec);
369aa65c29cSJan Kara 		for (i = 0; i < pagevec_count(&locked_pvec); i++)
370aa65c29cSJan Kara 			unlock_page(locked_pvec.pages[i]);
371f2187599SMel Gorman 		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
3721da177e4SLinus Torvalds 		pagevec_release(&pvec);
3731da177e4SLinus Torvalds 		cond_resched();
374b85e0effSHugh Dickins 		index++;
3751da177e4SLinus Torvalds 	}
3765a720394SLukas Czerner 	if (partial_start) {
3771da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3781da177e4SLinus Torvalds 		if (page) {
37909cbfeafSKirill A. Shutemov 			unsigned int top = PAGE_SIZE;
3805a720394SLukas Czerner 			if (start > end) {
3815a720394SLukas Czerner 				/* Truncation within a single page */
3825a720394SLukas Czerner 				top = partial_end;
3835a720394SLukas Czerner 				partial_end = 0;
3845a720394SLukas Czerner 			}
3851da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3865a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3875a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3885a720394SLukas Czerner 			if (page_has_private(page))
3895a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3905a720394SLukas Czerner 						  top - partial_start);
3911da177e4SLinus Torvalds 			unlock_page(page);
39209cbfeafSKirill A. Shutemov 			put_page(page);
3931da177e4SLinus Torvalds 		}
3941da177e4SLinus Torvalds 	}
3955a720394SLukas Czerner 	if (partial_end) {
3965a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3975a720394SLukas Czerner 		if (page) {
3985a720394SLukas Czerner 			wait_on_page_writeback(page);
3995a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
4005a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
4015a720394SLukas Czerner 			if (page_has_private(page))
4025a720394SLukas Czerner 				do_invalidatepage(page, 0,
4035a720394SLukas Czerner 						  partial_end);
4045a720394SLukas Czerner 			unlock_page(page);
40509cbfeafSKirill A. Shutemov 			put_page(page);
4065a720394SLukas Czerner 		}
4075a720394SLukas Czerner 	}
4085a720394SLukas Czerner 	/*
4095a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
4105a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
4115a720394SLukas Czerner 	 */
4125a720394SLukas Czerner 	if (start >= end)
41334ccb69eSAndrey Ryabinin 		goto out;
4141da177e4SLinus Torvalds 
415b85e0effSHugh Dickins 	index = start;
4161da177e4SLinus Torvalds 	for ( ; ; ) {
4171da177e4SLinus Torvalds 		cond_resched();
4180cd6144aSJohannes Weiner 		if (!pagevec_lookup_entries(&pvec, mapping, index,
419792ceaefSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
420792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
421b85e0effSHugh Dickins 			if (index == start)
4221da177e4SLinus Torvalds 				break;
423792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
424b85e0effSHugh Dickins 			index = start;
4251da177e4SLinus Torvalds 			continue;
4261da177e4SLinus Torvalds 		}
4270cd6144aSJohannes Weiner 		if (index == start && indices[0] >= end) {
428792ceaefSHugh Dickins 			/* All gone out of hole to be punched, we're done */
4290cd6144aSJohannes Weiner 			pagevec_remove_exceptionals(&pvec);
430d7339071SHans Reiser 			pagevec_release(&pvec);
431d7339071SHans Reiser 			break;
432d7339071SHans Reiser 		}
433f2187599SMel Gorman 
4341da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
4351da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
4361da177e4SLinus Torvalds 
437b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4380cd6144aSJohannes Weiner 			index = indices[i];
439792ceaefSHugh Dickins 			if (index >= end) {
440792ceaefSHugh Dickins 				/* Restart punch to make sure all gone */
441792ceaefSHugh Dickins 				index = start - 1;
442d7339071SHans Reiser 				break;
443792ceaefSHugh Dickins 			}
444b85e0effSHugh Dickins 
445f2187599SMel Gorman 			if (radix_tree_exceptional_entry(page))
4460cd6144aSJohannes Weiner 				continue;
4470cd6144aSJohannes Weiner 
4481da177e4SLinus Torvalds 			lock_page(page);
4495cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
4501da177e4SLinus Torvalds 			wait_on_page_writeback(page);
451750b4987SNick Piggin 			truncate_inode_page(mapping, page);
4521da177e4SLinus Torvalds 			unlock_page(page);
4531da177e4SLinus Torvalds 		}
454f2187599SMel Gorman 		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
4551da177e4SLinus Torvalds 		pagevec_release(&pvec);
456b85e0effSHugh Dickins 		index++;
4571da177e4SLinus Torvalds 	}
45834ccb69eSAndrey Ryabinin 
45934ccb69eSAndrey Ryabinin out:
4603167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
4611da177e4SLinus Torvalds }
462d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
4631da177e4SLinus Torvalds 
464d7339071SHans Reiser /**
465d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
466d7339071SHans Reiser  * @mapping: mapping to truncate
467d7339071SHans Reiser  * @lstart: offset from which to truncate
468d7339071SHans Reiser  *
4691b1dcc1bSJes Sorensen  * Called under (and serialised by) inode->i_mutex.
47008142579SJan Kara  *
47108142579SJan Kara  * Note: When this function returns, there can be a page in the process of
47208142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
47308142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
47408142579SJan Kara  * truncation of the whole mapping.
475d7339071SHans Reiser  */
476d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
477d7339071SHans Reiser {
478d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
479d7339071SHans Reiser }
4801da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4811da177e4SLinus Torvalds 
48228697355SMike Waychison /**
48391b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
48491b0abe3SJohannes Weiner  * @mapping: mapping to truncate
48591b0abe3SJohannes Weiner  *
48691b0abe3SJohannes Weiner  * Called under (and serialized by) inode->i_mutex.
48791b0abe3SJohannes Weiner  *
48891b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
48991b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
49091b0abe3SJohannes Weiner  */
49191b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
49291b0abe3SJohannes Weiner {
493f9fe48beSRoss Zwisler 	unsigned long nrexceptional;
49491b0abe3SJohannes Weiner 	unsigned long nrpages;
49591b0abe3SJohannes Weiner 
49691b0abe3SJohannes Weiner 	/*
49791b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
49891b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
49991b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
50091b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
50191b0abe3SJohannes Weiner 	 * final truncate has begun.
50291b0abe3SJohannes Weiner 	 */
50391b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
50491b0abe3SJohannes Weiner 
50591b0abe3SJohannes Weiner 	/*
50691b0abe3SJohannes Weiner 	 * When reclaim installs eviction entries, it increases
507f9fe48beSRoss Zwisler 	 * nrexceptional first, then decreases nrpages.  Make sure we see
50891b0abe3SJohannes Weiner 	 * this in the right order or we might miss an entry.
50991b0abe3SJohannes Weiner 	 */
51091b0abe3SJohannes Weiner 	nrpages = mapping->nrpages;
51191b0abe3SJohannes Weiner 	smp_rmb();
512f9fe48beSRoss Zwisler 	nrexceptional = mapping->nrexceptional;
51391b0abe3SJohannes Weiner 
514f9fe48beSRoss Zwisler 	if (nrpages || nrexceptional) {
51591b0abe3SJohannes Weiner 		/*
51691b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
51791b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
51891b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
51991b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
52091b0abe3SJohannes Weiner 		 */
52191b0abe3SJohannes Weiner 		spin_lock_irq(&mapping->tree_lock);
52291b0abe3SJohannes Weiner 		spin_unlock_irq(&mapping->tree_lock);
52391b0abe3SJohannes Weiner 
52491b0abe3SJohannes Weiner 		truncate_inode_pages(mapping, 0);
52591b0abe3SJohannes Weiner 	}
52691b0abe3SJohannes Weiner }
52791b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
52891b0abe3SJohannes Weiner 
52991b0abe3SJohannes Weiner /**
53028697355SMike Waychison  * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
53128697355SMike Waychison  * @mapping: the address_space which holds the pages to invalidate
53228697355SMike Waychison  * @start: the offset 'from' which to invalidate
53328697355SMike Waychison  * @end: the offset 'to' which to invalidate (inclusive)
53428697355SMike Waychison  *
53528697355SMike Waychison  * This function only removes the unlocked pages, if you want to
53628697355SMike Waychison  * remove all the pages of one inode, you must call truncate_inode_pages.
53728697355SMike Waychison  *
53828697355SMike Waychison  * invalidate_mapping_pages() will not block on IO activity. It will not
53928697355SMike Waychison  * invalidate pages which are dirty, locked, under writeback or mapped into
54028697355SMike Waychison  * pagetables.
54128697355SMike Waychison  */
54228697355SMike Waychison unsigned long invalidate_mapping_pages(struct address_space *mapping,
54328697355SMike Waychison 		pgoff_t start, pgoff_t end)
5441da177e4SLinus Torvalds {
5450cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
5461da177e4SLinus Torvalds 	struct pagevec pvec;
547b85e0effSHugh Dickins 	pgoff_t index = start;
54831560180SMinchan Kim 	unsigned long ret;
54931560180SMinchan Kim 	unsigned long count = 0;
5501da177e4SLinus Torvalds 	int i;
5511da177e4SLinus Torvalds 
55286679820SMel Gorman 	pagevec_init(&pvec);
5530cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
5540cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
5550cd6144aSJohannes Weiner 			indices)) {
5561da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
5571da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
5581da177e4SLinus Torvalds 
559b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
5600cd6144aSJohannes Weiner 			index = indices[i];
561b85e0effSHugh Dickins 			if (index > end)
562b85e0effSHugh Dickins 				break;
563e0f23603SNeilBrown 
5640cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
565c6dcf52cSJan Kara 				invalidate_exceptional_entry(mapping, index,
566c6dcf52cSJan Kara 							     page);
5670cd6144aSJohannes Weiner 				continue;
5680cd6144aSJohannes Weiner 			}
5690cd6144aSJohannes Weiner 
570b85e0effSHugh Dickins 			if (!trylock_page(page))
571b85e0effSHugh Dickins 				continue;
572fc127da0SKirill A. Shutemov 
5735cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
574fc127da0SKirill A. Shutemov 
575fc127da0SKirill A. Shutemov 			/* Middle of THP: skip */
576fc127da0SKirill A. Shutemov 			if (PageTransTail(page)) {
577fc127da0SKirill A. Shutemov 				unlock_page(page);
578fc127da0SKirill A. Shutemov 				continue;
579fc127da0SKirill A. Shutemov 			} else if (PageTransHuge(page)) {
580fc127da0SKirill A. Shutemov 				index += HPAGE_PMD_NR - 1;
581fc127da0SKirill A. Shutemov 				i += HPAGE_PMD_NR - 1;
58276b6f9b7SJan Kara 				/*
58376b6f9b7SJan Kara 				 * 'end' is in the middle of THP. Don't
58476b6f9b7SJan Kara 				 * invalidate the page as the part outside of
58576b6f9b7SJan Kara 				 * 'end' could be still useful.
58676b6f9b7SJan Kara 				 */
58776b6f9b7SJan Kara 				if (index > end) {
58876b6f9b7SJan Kara 					unlock_page(page);
589fc127da0SKirill A. Shutemov 					continue;
590fc127da0SKirill A. Shutemov 				}
59176b6f9b7SJan Kara 			}
592fc127da0SKirill A. Shutemov 
59331560180SMinchan Kim 			ret = invalidate_inode_page(page);
5941da177e4SLinus Torvalds 			unlock_page(page);
59531560180SMinchan Kim 			/*
59631560180SMinchan Kim 			 * Invalidation is a hint that the page is no longer
59731560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
59831560180SMinchan Kim 			 */
59931560180SMinchan Kim 			if (!ret)
600cc5993bdSMinchan Kim 				deactivate_file_page(page);
60131560180SMinchan Kim 			count += ret;
6021da177e4SLinus Torvalds 		}
6030cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6041da177e4SLinus Torvalds 		pagevec_release(&pvec);
605fc9a07e7SAndrew Morton 		cond_resched();
606b85e0effSHugh Dickins 		index++;
6071da177e4SLinus Torvalds 	}
60831560180SMinchan Kim 	return count;
6091da177e4SLinus Torvalds }
61054bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
6111da177e4SLinus Torvalds 
612bd4c8ce4SAndrew Morton /*
613bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
614bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
615bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
6162706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
6172706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
618bd4c8ce4SAndrew Morton  */
619bd4c8ce4SAndrew Morton static int
620bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
621bd4c8ce4SAndrew Morton {
622c4843a75SGreg Thelen 	unsigned long flags;
623c4843a75SGreg Thelen 
624bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
625bd4c8ce4SAndrew Morton 		return 0;
626bd4c8ce4SAndrew Morton 
627266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
628bd4c8ce4SAndrew Morton 		return 0;
629bd4c8ce4SAndrew Morton 
630c4843a75SGreg Thelen 	spin_lock_irqsave(&mapping->tree_lock, flags);
631bd4c8ce4SAndrew Morton 	if (PageDirty(page))
632bd4c8ce4SAndrew Morton 		goto failed;
633bd4c8ce4SAndrew Morton 
634266cf658SDavid Howells 	BUG_ON(page_has_private(page));
63562cccb8cSJohannes Weiner 	__delete_from_page_cache(page, NULL);
636c4843a75SGreg Thelen 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
6376072d13cSLinus Torvalds 
6386072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
6396072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
6406072d13cSLinus Torvalds 
64109cbfeafSKirill A. Shutemov 	put_page(page);	/* pagecache ref */
642bd4c8ce4SAndrew Morton 	return 1;
643bd4c8ce4SAndrew Morton failed:
644c4843a75SGreg Thelen 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
645bd4c8ce4SAndrew Morton 	return 0;
646bd4c8ce4SAndrew Morton }
647bd4c8ce4SAndrew Morton 
648e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
649e3db7691STrond Myklebust {
650e3db7691STrond Myklebust 	if (!PageDirty(page))
651e3db7691STrond Myklebust 		return 0;
652e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
653e3db7691STrond Myklebust 		return 0;
654e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
655e3db7691STrond Myklebust }
656e3db7691STrond Myklebust 
6571da177e4SLinus Torvalds /**
6581da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
65967be2dd1SMartin Waitz  * @mapping: the address_space
6601da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
6611da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
6621da177e4SLinus Torvalds  *
6631da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6641da177e4SLinus Torvalds  * invalidation.
6651da177e4SLinus Torvalds  *
6666ccfa806SHisashi Hifumi  * Returns -EBUSY if any pages could not be invalidated.
6671da177e4SLinus Torvalds  */
6681da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
6691da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
6701da177e4SLinus Torvalds {
6710cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
6721da177e4SLinus Torvalds 	struct pagevec pvec;
673b85e0effSHugh Dickins 	pgoff_t index;
6741da177e4SLinus Torvalds 	int i;
6751da177e4SLinus Torvalds 	int ret = 0;
6760dd1334fSHisashi Hifumi 	int ret2 = 0;
6771da177e4SLinus Torvalds 	int did_range_unmap = 0;
6781da177e4SLinus Torvalds 
67932691f0fSAndrey Ryabinin 	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
68034ccb69eSAndrey Ryabinin 		goto out;
68132691f0fSAndrey Ryabinin 
68286679820SMel Gorman 	pagevec_init(&pvec);
683b85e0effSHugh Dickins 	index = start;
6840cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
6850cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
6860cd6144aSJohannes Weiner 			indices)) {
6877b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
6881da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
689b85e0effSHugh Dickins 
690b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
6910cd6144aSJohannes Weiner 			index = indices[i];
692b85e0effSHugh Dickins 			if (index > end)
693b85e0effSHugh Dickins 				break;
6941da177e4SLinus Torvalds 
6950cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
696c6dcf52cSJan Kara 				if (!invalidate_exceptional_entry2(mapping,
697c6dcf52cSJan Kara 								   index, page))
698c6dcf52cSJan Kara 					ret = -EBUSY;
6990cd6144aSJohannes Weiner 				continue;
7000cd6144aSJohannes Weiner 			}
7010cd6144aSJohannes Weiner 
7021da177e4SLinus Torvalds 			lock_page(page);
7035cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
7041da177e4SLinus Torvalds 			if (page->mapping != mapping) {
7051da177e4SLinus Torvalds 				unlock_page(page);
7061da177e4SLinus Torvalds 				continue;
7071da177e4SLinus Torvalds 			}
7081da177e4SLinus Torvalds 			wait_on_page_writeback(page);
709d00806b1SNick Piggin 			if (page_mapped(page)) {
7101da177e4SLinus Torvalds 				if (!did_range_unmap) {
7111da177e4SLinus Torvalds 					/*
7121da177e4SLinus Torvalds 					 * Zap the rest of the file in one hit.
7131da177e4SLinus Torvalds 					 */
714*977fbdcdSMatthew Wilcox 					unmap_mapping_pages(mapping, index,
715*977fbdcdSMatthew Wilcox 						(1 + end - index), false);
7161da177e4SLinus Torvalds 					did_range_unmap = 1;
7171da177e4SLinus Torvalds 				} else {
7181da177e4SLinus Torvalds 					/*
7191da177e4SLinus Torvalds 					 * Just zap this page
7201da177e4SLinus Torvalds 					 */
721*977fbdcdSMatthew Wilcox 					unmap_mapping_pages(mapping, index,
722*977fbdcdSMatthew Wilcox 								1, false);
7231da177e4SLinus Torvalds 				}
7241da177e4SLinus Torvalds 			}
725d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
7260dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
7270dd1334fSHisashi Hifumi 			if (ret2 == 0) {
7280dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
7296ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
7300dd1334fSHisashi Hifumi 			}
7310dd1334fSHisashi Hifumi 			if (ret2 < 0)
7320dd1334fSHisashi Hifumi 				ret = ret2;
7331da177e4SLinus Torvalds 			unlock_page(page);
7341da177e4SLinus Torvalds 		}
7350cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
7361da177e4SLinus Torvalds 		pagevec_release(&pvec);
7371da177e4SLinus Torvalds 		cond_resched();
738b85e0effSHugh Dickins 		index++;
7391da177e4SLinus Torvalds 	}
740cd656375SJan Kara 	/*
741cd656375SJan Kara 	 * For DAX we invalidate page tables after invalidating radix tree.  We
742cd656375SJan Kara 	 * could invalidate page tables while invalidating each entry however
743cd656375SJan Kara 	 * that would be expensive. And doing range unmapping before doesn't
744cd656375SJan Kara 	 * work as we have no cheap way to find whether radix tree entry didn't
745cd656375SJan Kara 	 * get remapped later.
746cd656375SJan Kara 	 */
747cd656375SJan Kara 	if (dax_mapping(mapping)) {
748*977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, start, end - start + 1, false);
749cd656375SJan Kara 	}
75034ccb69eSAndrey Ryabinin out:
7513167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
7521da177e4SLinus Torvalds 	return ret;
7531da177e4SLinus Torvalds }
7541da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
7551da177e4SLinus Torvalds 
7561da177e4SLinus Torvalds /**
7571da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
75867be2dd1SMartin Waitz  * @mapping: the address_space
7591da177e4SLinus Torvalds  *
7601da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
7611da177e4SLinus Torvalds  * invalidation.
7621da177e4SLinus Torvalds  *
763e9de25ddSPeng Tao  * Returns -EBUSY if any pages could not be invalidated.
7641da177e4SLinus Torvalds  */
7651da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
7661da177e4SLinus Torvalds {
7671da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
7681da177e4SLinus Torvalds }
7691da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
77025d9e2d1Snpiggin@suse.de 
77125d9e2d1Snpiggin@suse.de /**
77225d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
77325d9e2d1Snpiggin@suse.de  * @inode: inode
7748a549beaSHugh Dickins  * @newsize: new file size
77525d9e2d1Snpiggin@suse.de  *
77625d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
77725d9e2d1Snpiggin@suse.de  * is called.
77825d9e2d1Snpiggin@suse.de  *
77925d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
78025d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
78125d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
78225d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
78325d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
78425d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
78525d9e2d1Snpiggin@suse.de  */
7867caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
78725d9e2d1Snpiggin@suse.de {
78825d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
7898a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
79025d9e2d1Snpiggin@suse.de 
79125d9e2d1Snpiggin@suse.de 	/*
79225d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
79325d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
79425d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
79525d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
79625d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
79725d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
79825d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
79925d9e2d1Snpiggin@suse.de 	 */
8008a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
8018a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
8028a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
80325d9e2d1Snpiggin@suse.de }
80425d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
80525d9e2d1Snpiggin@suse.de 
80625d9e2d1Snpiggin@suse.de /**
8072c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
8082c27c65eSChristoph Hellwig  * @inode: inode
8092c27c65eSChristoph Hellwig  * @newsize: new file size
8102c27c65eSChristoph Hellwig  *
811382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
812382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
813382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
8142c27c65eSChristoph Hellwig  *
81577783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
81677783d06SJan Kara  * i_mutex but e.g. xfs uses a different lock) and before all filesystem
81777783d06SJan Kara  * specific block truncation has been performed.
8182c27c65eSChristoph Hellwig  */
8192c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
8202c27c65eSChristoph Hellwig {
82190a80202SJan Kara 	loff_t oldsize = inode->i_size;
82290a80202SJan Kara 
8232c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
82490a80202SJan Kara 	if (newsize > oldsize)
82590a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
8267caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
8272c27c65eSChristoph Hellwig }
8282c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
8292c27c65eSChristoph Hellwig 
8302c27c65eSChristoph Hellwig /**
83190a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
83290a80202SJan Kara  * @inode:	inode for which i_size was extended
83390a80202SJan Kara  * @from:	original inode size
83490a80202SJan Kara  * @to:		new inode size
83590a80202SJan Kara  *
83690a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
83790a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
83890a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
83990a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
84090a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
84190a80202SJan Kara  * changed.
84290a80202SJan Kara  *
84390a80202SJan Kara  * The function must be called after i_size is updated so that page fault
84490a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
84590a80202SJan Kara  * The function must be called while we still hold i_mutex - this not only
84690a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
84790a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
84890a80202SJan Kara  */
84990a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
85090a80202SJan Kara {
85193407472SFabian Frederick 	int bsize = i_blocksize(inode);
85290a80202SJan Kara 	loff_t rounded_from;
85390a80202SJan Kara 	struct page *page;
85490a80202SJan Kara 	pgoff_t index;
85590a80202SJan Kara 
85690a80202SJan Kara 	WARN_ON(to > inode->i_size);
85790a80202SJan Kara 
85809cbfeafSKirill A. Shutemov 	if (from >= to || bsize == PAGE_SIZE)
85990a80202SJan Kara 		return;
86090a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
86190a80202SJan Kara 	rounded_from = round_up(from, bsize);
86209cbfeafSKirill A. Shutemov 	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
86390a80202SJan Kara 		return;
86490a80202SJan Kara 
86509cbfeafSKirill A. Shutemov 	index = from >> PAGE_SHIFT;
86690a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
86790a80202SJan Kara 	/* Page not cached? Nothing to do */
86890a80202SJan Kara 	if (!page)
86990a80202SJan Kara 		return;
87090a80202SJan Kara 	/*
87190a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
87290a80202SJan Kara 	 * is needed.
87390a80202SJan Kara 	 */
87490a80202SJan Kara 	if (page_mkclean(page))
87590a80202SJan Kara 		set_page_dirty(page);
87690a80202SJan Kara 	unlock_page(page);
87709cbfeafSKirill A. Shutemov 	put_page(page);
87890a80202SJan Kara }
87990a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
88090a80202SJan Kara 
88190a80202SJan Kara /**
882623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
883623e3db9SHugh Dickins  * @inode: inode
884623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
885623e3db9SHugh Dickins  * @lend: offset of last byte of hole
886623e3db9SHugh Dickins  *
887623e3db9SHugh Dickins  * This function should typically be called before the filesystem
888623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
889623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
890623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
891623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
892623e3db9SHugh Dickins  * had its underlying blocks deallocated.
893623e3db9SHugh Dickins  */
894623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
895623e3db9SHugh Dickins {
896623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
897623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
898623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
899623e3db9SHugh Dickins 	/*
900623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
901623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
902623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
9035a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
9045a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
905623e3db9SHugh Dickins 	 */
906623e3db9SHugh Dickins 
907623e3db9SHugh Dickins 	/*
908623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
909623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
910623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
911623e3db9SHugh Dickins 	 */
912623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
913623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
914623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
915623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
916623e3db9SHugh Dickins }
917623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
918