xref: /linux/mm/truncate.c (revision a77eedbc871ee3b435bffc30b123b60eecca402c)
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);
43f9fe48beSRoss Zwisler 	mapping->nrexceptional--;
44f2187599SMel Gorman }
45f2187599SMel Gorman 
46f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
47f2187599SMel Gorman 			       void *entry)
48f2187599SMel Gorman {
49b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
50f2187599SMel Gorman 	__clear_shadow_entry(mapping, index, entry);
51b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
520cd6144aSJohannes Weiner }
531da177e4SLinus Torvalds 
54c6dcf52cSJan Kara /*
55f2187599SMel Gorman  * Unconditionally remove exceptional entries. Usually called from truncate
56f2187599SMel Gorman  * path. Note that the pagevec may be altered by this function by removing
57f2187599SMel Gorman  * exceptional entries similar to what pagevec_remove_exceptionals does.
58c6dcf52cSJan Kara  */
59f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping,
60f2187599SMel Gorman 				struct pagevec *pvec, pgoff_t *indices,
61f2187599SMel Gorman 				pgoff_t end)
62c6dcf52cSJan Kara {
63f2187599SMel Gorman 	int i, j;
64f2187599SMel Gorman 	bool dax, lock;
65f2187599SMel Gorman 
66c6dcf52cSJan Kara 	/* Handled by shmem itself */
67c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
68c6dcf52cSJan Kara 		return;
69c6dcf52cSJan Kara 
70f2187599SMel Gorman 	for (j = 0; j < pagevec_count(pvec); j++)
713159f943SMatthew Wilcox 		if (xa_is_value(pvec->pages[j]))
72f2187599SMel Gorman 			break;
73f2187599SMel Gorman 
74f2187599SMel Gorman 	if (j == pagevec_count(pvec))
75c6dcf52cSJan Kara 		return;
76f2187599SMel Gorman 
77f2187599SMel Gorman 	dax = dax_mapping(mapping);
78f2187599SMel Gorman 	lock = !dax && indices[j] < end;
79f2187599SMel Gorman 	if (lock)
80b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
81f2187599SMel Gorman 
82f2187599SMel Gorman 	for (i = j; i < pagevec_count(pvec); i++) {
83f2187599SMel Gorman 		struct page *page = pvec->pages[i];
84f2187599SMel Gorman 		pgoff_t index = indices[i];
85f2187599SMel Gorman 
863159f943SMatthew Wilcox 		if (!xa_is_value(page)) {
87f2187599SMel Gorman 			pvec->pages[j++] = page;
88f2187599SMel Gorman 			continue;
89c6dcf52cSJan Kara 		}
90f2187599SMel Gorman 
91f2187599SMel Gorman 		if (index >= end)
92f2187599SMel Gorman 			continue;
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 
102f2187599SMel Gorman 	if (lock)
103b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
104f2187599SMel Gorman 	pvec->nr = j;
105c6dcf52cSJan Kara }
106c6dcf52cSJan Kara 
107c6dcf52cSJan Kara /*
108c6dcf52cSJan Kara  * Invalidate exceptional entry if easily possible. This handles exceptional
1094636e70bSRoss Zwisler  * entries for invalidate_inode_pages().
110c6dcf52cSJan Kara  */
111c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping,
112c6dcf52cSJan Kara 					pgoff_t index, void *entry)
113c6dcf52cSJan Kara {
1144636e70bSRoss Zwisler 	/* Handled by shmem itself, or for DAX we do nothing. */
1154636e70bSRoss Zwisler 	if (shmem_mapping(mapping) || dax_mapping(mapping))
116c6dcf52cSJan Kara 		return 1;
117c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
118c6dcf52cSJan Kara 	return 1;
119c6dcf52cSJan Kara }
120c6dcf52cSJan Kara 
121c6dcf52cSJan Kara /*
122c6dcf52cSJan Kara  * Invalidate exceptional entry if clean. This handles exceptional entries for
123c6dcf52cSJan Kara  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
124c6dcf52cSJan Kara  */
125c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping,
126c6dcf52cSJan Kara 					 pgoff_t index, void *entry)
127c6dcf52cSJan Kara {
128c6dcf52cSJan Kara 	/* Handled by shmem itself */
129c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
130c6dcf52cSJan Kara 		return 1;
131c6dcf52cSJan Kara 	if (dax_mapping(mapping))
132c6dcf52cSJan Kara 		return dax_invalidate_mapping_entry_sync(mapping, index);
133c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
134c6dcf52cSJan Kara 	return 1;
135c6dcf52cSJan Kara }
136c6dcf52cSJan Kara 
137cf9a2ae8SDavid Howells /**
13828bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
139cf9a2ae8SDavid Howells  * @page: the page which is affected
140d47992f8SLukas Czerner  * @offset: start of the range to invalidate
141d47992f8SLukas Czerner  * @length: length of the range to invalidate
142cf9a2ae8SDavid Howells  *
143cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
144cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
145cf9a2ae8SDavid Howells  *
146cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
147cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
148cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
149cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
150cf9a2ae8SDavid Howells  * blocks on-disk.
151cf9a2ae8SDavid Howells  */
152d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
153d47992f8SLukas Czerner 		       unsigned int length)
154cf9a2ae8SDavid Howells {
155d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
156d47992f8SLukas Czerner 
157cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
1589361401eSDavid Howells #ifdef CONFIG_BLOCK
159cf9a2ae8SDavid Howells 	if (!invalidatepage)
160cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
1619361401eSDavid Howells #endif
162cf9a2ae8SDavid Howells 	if (invalidatepage)
163d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
164cf9a2ae8SDavid Howells }
165cf9a2ae8SDavid Howells 
166ecdfc978SLinus Torvalds /*
1671da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
16862e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
16954cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1701da177e4SLinus Torvalds  *
171fc3a5ac5SMatthew Wilcox (Oracle)  * We need to bail out if page->mapping is no longer equal to the original
1721da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
173fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1741da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1751da177e4SLinus Torvalds  */
1769f4e41f4SJan Kara static void
1779f4e41f4SJan Kara truncate_cleanup_page(struct address_space *mapping, struct page *page)
1781da177e4SLinus Torvalds {
1799f4e41f4SJan Kara 	if (page_mapped(page)) {
180fc3a5ac5SMatthew Wilcox (Oracle) 		unsigned int nr = thp_nr_pages(page);
181977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, page->index, nr, false);
1829f4e41f4SJan Kara 	}
1831da177e4SLinus Torvalds 
184266cf658SDavid Howells 	if (page_has_private(page))
185fc3a5ac5SMatthew Wilcox (Oracle) 		do_invalidatepage(page, 0, thp_size(page));
1861da177e4SLinus Torvalds 
187b9ea2515SKonstantin Khlebnikov 	/*
188b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
189b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
190b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
191b9ea2515SKonstantin Khlebnikov 	 */
19211f81becSTejun Heo 	cancel_dirty_page(page);
1931da177e4SLinus Torvalds 	ClearPageMappedToDisk(page);
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds /*
197fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
1981da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
1990fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
2000fd0e6b0SNick Piggin  * discards clean, unused pages.
2011da177e4SLinus Torvalds  *
2021da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
2031da177e4SLinus Torvalds  */
2041da177e4SLinus Torvalds static int
2051da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
2061da177e4SLinus Torvalds {
2070fd0e6b0SNick Piggin 	int ret;
2080fd0e6b0SNick Piggin 
2091da177e4SLinus Torvalds 	if (page->mapping != mapping)
2101da177e4SLinus Torvalds 		return 0;
2111da177e4SLinus Torvalds 
212266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
2131da177e4SLinus Torvalds 		return 0;
2141da177e4SLinus Torvalds 
2150fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
2160fd0e6b0SNick Piggin 
2170fd0e6b0SNick Piggin 	return ret;
2181da177e4SLinus Torvalds }
2191da177e4SLinus Torvalds 
220750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page)
221750b4987SNick Piggin {
222fc127da0SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageTail(page), page);
223fc127da0SKirill A. Shutemov 
2249f4e41f4SJan Kara 	if (page->mapping != mapping)
2259f4e41f4SJan Kara 		return -EIO;
2269f4e41f4SJan Kara 
2279f4e41f4SJan Kara 	truncate_cleanup_page(mapping, page);
2289f4e41f4SJan Kara 	delete_from_page_cache(page);
2299f4e41f4SJan Kara 	return 0;
230750b4987SNick Piggin }
231750b4987SNick Piggin 
23283f78668SWu Fengguang /*
23325718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
23425718736SAndi Kleen  */
23525718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
23625718736SAndi Kleen {
23725718736SAndi Kleen 	if (!mapping)
23825718736SAndi Kleen 		return -EINVAL;
23925718736SAndi Kleen 	/*
24025718736SAndi Kleen 	 * Only punch for normal data pages for now.
24125718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
24225718736SAndi Kleen 	 */
24325718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
24425718736SAndi Kleen 		return -EIO;
24525718736SAndi Kleen 	return truncate_inode_page(mapping, page);
24625718736SAndi Kleen }
24725718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
24825718736SAndi Kleen 
24925718736SAndi Kleen /*
25083f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
25183f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
25283f78668SWu Fengguang  *
25383f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
25483f78668SWu Fengguang  */
25583f78668SWu Fengguang int invalidate_inode_page(struct page *page)
25683f78668SWu Fengguang {
25783f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
25883f78668SWu Fengguang 	if (!mapping)
25983f78668SWu Fengguang 		return 0;
26083f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
26183f78668SWu Fengguang 		return 0;
26283f78668SWu Fengguang 	if (page_mapped(page))
26383f78668SWu Fengguang 		return 0;
26483f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
26583f78668SWu Fengguang }
26683f78668SWu Fengguang 
2671da177e4SLinus Torvalds /**
26873c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2691da177e4SLinus Torvalds  * @mapping: mapping to truncate
2701da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2715a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2721da177e4SLinus Torvalds  *
273d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2745a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2755a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2761da177e4SLinus Torvalds  *
2771da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2781da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2791da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2801da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2811da177e4SLinus Torvalds  * is low.
2821da177e4SLinus Torvalds  *
2831da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2841da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2851da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2865a720394SLukas Czerner  *
2875a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2885a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2895a720394SLukas Czerner  * page aligned properly.
2901da177e4SLinus Torvalds  */
291d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
292d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2931da177e4SLinus Torvalds {
2945a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2955a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2965a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2975a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
2981da177e4SLinus Torvalds 	struct pagevec	pvec;
2990cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
300b85e0effSHugh Dickins 	pgoff_t		index;
3011da177e4SLinus Torvalds 	int		i;
3021da177e4SLinus Torvalds 
303f9fe48beSRoss Zwisler 	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
30434ccb69eSAndrey Ryabinin 		goto out;
3051da177e4SLinus Torvalds 
3065a720394SLukas Czerner 	/* Offsets within partial pages */
30709cbfeafSKirill A. Shutemov 	partial_start = lstart & (PAGE_SIZE - 1);
30809cbfeafSKirill A. Shutemov 	partial_end = (lend + 1) & (PAGE_SIZE - 1);
3095a720394SLukas Czerner 
3105a720394SLukas Czerner 	/*
3115a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
3125a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
3135a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
3145a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
3155a720394SLukas Czerner 	 */
31609cbfeafSKirill A. Shutemov 	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
3175a720394SLukas Czerner 	if (lend == -1)
3185a720394SLukas Czerner 		/*
3195a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
3205a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
3215a720394SLukas Czerner 		 * unsigned we're using -1.
3225a720394SLukas Czerner 		 */
3235a720394SLukas Czerner 		end = -1;
3245a720394SLukas Czerner 	else
32509cbfeafSKirill A. Shutemov 		end = (lend + 1) >> PAGE_SHIFT;
326d7339071SHans Reiser 
32786679820SMel Gorman 	pagevec_init(&pvec);
328b85e0effSHugh Dickins 	index = start;
3290cd6144aSJohannes Weiner 	while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
3300cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
3310cd6144aSJohannes Weiner 			indices)) {
332aa65c29cSJan Kara 		/*
333aa65c29cSJan Kara 		 * Pagevec array has exceptional entries and we may also fail
334aa65c29cSJan Kara 		 * to lock some pages. So we store pages that can be deleted
335aa65c29cSJan Kara 		 * in a new pagevec.
336aa65c29cSJan Kara 		 */
337aa65c29cSJan Kara 		struct pagevec locked_pvec;
338aa65c29cSJan Kara 
33986679820SMel Gorman 		pagevec_init(&locked_pvec);
3401da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3411da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
3421da177e4SLinus Torvalds 
343b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
3440cd6144aSJohannes Weiner 			index = indices[i];
3455a720394SLukas Czerner 			if (index >= end)
346d7339071SHans Reiser 				break;
347d7339071SHans Reiser 
3483159f943SMatthew Wilcox 			if (xa_is_value(page))
3490cd6144aSJohannes Weiner 				continue;
3500cd6144aSJohannes Weiner 
351529ae9aaSNick Piggin 			if (!trylock_page(page))
3521da177e4SLinus Torvalds 				continue;
3535cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
3541da177e4SLinus Torvalds 			if (PageWriteback(page)) {
3551da177e4SLinus Torvalds 				unlock_page(page);
3561da177e4SLinus Torvalds 				continue;
3571da177e4SLinus Torvalds 			}
358aa65c29cSJan Kara 			if (page->mapping != mapping) {
3591da177e4SLinus Torvalds 				unlock_page(page);
360aa65c29cSJan Kara 				continue;
3611da177e4SLinus Torvalds 			}
362aa65c29cSJan Kara 			pagevec_add(&locked_pvec, page);
363aa65c29cSJan Kara 		}
364aa65c29cSJan Kara 		for (i = 0; i < pagevec_count(&locked_pvec); i++)
365aa65c29cSJan Kara 			truncate_cleanup_page(mapping, locked_pvec.pages[i]);
366aa65c29cSJan Kara 		delete_from_page_cache_batch(mapping, &locked_pvec);
367aa65c29cSJan Kara 		for (i = 0; i < pagevec_count(&locked_pvec); i++)
368aa65c29cSJan Kara 			unlock_page(locked_pvec.pages[i]);
369f2187599SMel Gorman 		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
3701da177e4SLinus Torvalds 		pagevec_release(&pvec);
3711da177e4SLinus Torvalds 		cond_resched();
372b85e0effSHugh Dickins 		index++;
3731da177e4SLinus Torvalds 	}
3745a720394SLukas Czerner 	if (partial_start) {
3751da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3761da177e4SLinus Torvalds 		if (page) {
37709cbfeafSKirill A. Shutemov 			unsigned int top = PAGE_SIZE;
3785a720394SLukas Czerner 			if (start > end) {
3795a720394SLukas Czerner 				/* Truncation within a single page */
3805a720394SLukas Czerner 				top = partial_end;
3815a720394SLukas Czerner 				partial_end = 0;
3825a720394SLukas Czerner 			}
3831da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3845a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3855a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3865a720394SLukas Czerner 			if (page_has_private(page))
3875a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3885a720394SLukas Czerner 						  top - partial_start);
3891da177e4SLinus Torvalds 			unlock_page(page);
39009cbfeafSKirill A. Shutemov 			put_page(page);
3911da177e4SLinus Torvalds 		}
3921da177e4SLinus Torvalds 	}
3935a720394SLukas Czerner 	if (partial_end) {
3945a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3955a720394SLukas Czerner 		if (page) {
3965a720394SLukas Czerner 			wait_on_page_writeback(page);
3975a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
3985a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3995a720394SLukas Czerner 			if (page_has_private(page))
4005a720394SLukas Czerner 				do_invalidatepage(page, 0,
4015a720394SLukas Czerner 						  partial_end);
4025a720394SLukas Czerner 			unlock_page(page);
40309cbfeafSKirill A. Shutemov 			put_page(page);
4045a720394SLukas Czerner 		}
4055a720394SLukas Czerner 	}
4065a720394SLukas Czerner 	/*
4075a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
4085a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
4095a720394SLukas Czerner 	 */
4105a720394SLukas Czerner 	if (start >= end)
41134ccb69eSAndrey Ryabinin 		goto out;
4121da177e4SLinus Torvalds 
413b85e0effSHugh Dickins 	index = start;
4141da177e4SLinus Torvalds 	for ( ; ; ) {
4151da177e4SLinus Torvalds 		cond_resched();
4160cd6144aSJohannes Weiner 		if (!pagevec_lookup_entries(&pvec, mapping, index,
417792ceaefSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
418792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
419b85e0effSHugh Dickins 			if (index == start)
4201da177e4SLinus Torvalds 				break;
421792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
422b85e0effSHugh Dickins 			index = start;
4231da177e4SLinus Torvalds 			continue;
4241da177e4SLinus Torvalds 		}
4250cd6144aSJohannes Weiner 		if (index == start && indices[0] >= end) {
426792ceaefSHugh Dickins 			/* All gone out of hole to be punched, we're done */
4270cd6144aSJohannes Weiner 			pagevec_remove_exceptionals(&pvec);
428d7339071SHans Reiser 			pagevec_release(&pvec);
429d7339071SHans Reiser 			break;
430d7339071SHans Reiser 		}
431f2187599SMel Gorman 
4321da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
4331da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
4341da177e4SLinus Torvalds 
435b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4360cd6144aSJohannes Weiner 			index = indices[i];
437792ceaefSHugh Dickins 			if (index >= end) {
438792ceaefSHugh Dickins 				/* Restart punch to make sure all gone */
439792ceaefSHugh Dickins 				index = start - 1;
440d7339071SHans Reiser 				break;
441792ceaefSHugh Dickins 			}
442b85e0effSHugh Dickins 
4433159f943SMatthew Wilcox 			if (xa_is_value(page))
4440cd6144aSJohannes Weiner 				continue;
4450cd6144aSJohannes Weiner 
4461da177e4SLinus Torvalds 			lock_page(page);
4475cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
4481da177e4SLinus Torvalds 			wait_on_page_writeback(page);
449750b4987SNick Piggin 			truncate_inode_page(mapping, page);
4501da177e4SLinus Torvalds 			unlock_page(page);
4511da177e4SLinus Torvalds 		}
452f2187599SMel Gorman 		truncate_exceptional_pvec_entries(mapping, &pvec, indices, end);
4531da177e4SLinus Torvalds 		pagevec_release(&pvec);
454b85e0effSHugh Dickins 		index++;
4551da177e4SLinus Torvalds 	}
45634ccb69eSAndrey Ryabinin 
45734ccb69eSAndrey Ryabinin out:
4583167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
4591da177e4SLinus Torvalds }
460d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
4611da177e4SLinus Torvalds 
462d7339071SHans Reiser /**
463d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
464d7339071SHans Reiser  * @mapping: mapping to truncate
465d7339071SHans Reiser  * @lstart: offset from which to truncate
466d7339071SHans Reiser  *
4671b1dcc1bSJes Sorensen  * Called under (and serialised by) inode->i_mutex.
46808142579SJan Kara  *
46908142579SJan Kara  * Note: When this function returns, there can be a page in the process of
47008142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
47108142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
47208142579SJan Kara  * truncation of the whole mapping.
473d7339071SHans Reiser  */
474d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
475d7339071SHans Reiser {
476d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
477d7339071SHans Reiser }
4781da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4791da177e4SLinus Torvalds 
48028697355SMike Waychison /**
48191b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
48291b0abe3SJohannes Weiner  * @mapping: mapping to truncate
48391b0abe3SJohannes Weiner  *
48491b0abe3SJohannes Weiner  * Called under (and serialized by) inode->i_mutex.
48591b0abe3SJohannes Weiner  *
48691b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
48791b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
48891b0abe3SJohannes Weiner  */
48991b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
49091b0abe3SJohannes Weiner {
491f9fe48beSRoss Zwisler 	unsigned long nrexceptional;
49291b0abe3SJohannes Weiner 	unsigned long nrpages;
49391b0abe3SJohannes Weiner 
49491b0abe3SJohannes Weiner 	/*
49591b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
49691b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
49791b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
49891b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
49991b0abe3SJohannes Weiner 	 * final truncate has begun.
50091b0abe3SJohannes Weiner 	 */
50191b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
50291b0abe3SJohannes Weiner 
50391b0abe3SJohannes Weiner 	/*
50491b0abe3SJohannes Weiner 	 * When reclaim installs eviction entries, it increases
505f9fe48beSRoss Zwisler 	 * nrexceptional first, then decreases nrpages.  Make sure we see
50691b0abe3SJohannes Weiner 	 * this in the right order or we might miss an entry.
50791b0abe3SJohannes Weiner 	 */
50891b0abe3SJohannes Weiner 	nrpages = mapping->nrpages;
50991b0abe3SJohannes Weiner 	smp_rmb();
510f9fe48beSRoss Zwisler 	nrexceptional = mapping->nrexceptional;
51191b0abe3SJohannes Weiner 
512f9fe48beSRoss Zwisler 	if (nrpages || nrexceptional) {
51391b0abe3SJohannes Weiner 		/*
51491b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
51591b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
51691b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
51791b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
51891b0abe3SJohannes Weiner 		 */
519b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
520b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
52191b0abe3SJohannes Weiner 	}
5226ff38bd4SPavel Tikhomirov 
5236ff38bd4SPavel Tikhomirov 	/*
5246ff38bd4SPavel Tikhomirov 	 * Cleancache needs notification even if there are no pages or shadow
5256ff38bd4SPavel Tikhomirov 	 * entries.
5266ff38bd4SPavel Tikhomirov 	 */
5276ff38bd4SPavel Tikhomirov 	truncate_inode_pages(mapping, 0);
52891b0abe3SJohannes Weiner }
52991b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
53091b0abe3SJohannes Weiner 
531*a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping,
532eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
5331da177e4SLinus Torvalds {
5340cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
5351da177e4SLinus Torvalds 	struct pagevec pvec;
536b85e0effSHugh Dickins 	pgoff_t index = start;
53731560180SMinchan Kim 	unsigned long ret;
53831560180SMinchan Kim 	unsigned long count = 0;
5391da177e4SLinus Torvalds 	int i;
5401da177e4SLinus Torvalds 
54186679820SMel Gorman 	pagevec_init(&pvec);
5420cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
5430cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
5440cd6144aSJohannes Weiner 			indices)) {
5451da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
5461da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
5471da177e4SLinus Torvalds 
548b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
5490cd6144aSJohannes Weiner 			index = indices[i];
550b85e0effSHugh Dickins 			if (index > end)
551b85e0effSHugh Dickins 				break;
552e0f23603SNeilBrown 
5533159f943SMatthew Wilcox 			if (xa_is_value(page)) {
554c6dcf52cSJan Kara 				invalidate_exceptional_entry(mapping, index,
555c6dcf52cSJan Kara 							     page);
5560cd6144aSJohannes Weiner 				continue;
5570cd6144aSJohannes Weiner 			}
5580cd6144aSJohannes Weiner 
559b85e0effSHugh Dickins 			if (!trylock_page(page))
560b85e0effSHugh Dickins 				continue;
561fc127da0SKirill A. Shutemov 
5625cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
563fc127da0SKirill A. Shutemov 
564fc127da0SKirill A. Shutemov 			/* Middle of THP: skip */
565fc127da0SKirill A. Shutemov 			if (PageTransTail(page)) {
566fc127da0SKirill A. Shutemov 				unlock_page(page);
567fc127da0SKirill A. Shutemov 				continue;
568fc127da0SKirill A. Shutemov 			} else if (PageTransHuge(page)) {
569fc127da0SKirill A. Shutemov 				index += HPAGE_PMD_NR - 1;
570fc127da0SKirill A. Shutemov 				i += HPAGE_PMD_NR - 1;
57176b6f9b7SJan Kara 				/*
57276b6f9b7SJan Kara 				 * 'end' is in the middle of THP. Don't
57376b6f9b7SJan Kara 				 * invalidate the page as the part outside of
57476b6f9b7SJan Kara 				 * 'end' could be still useful.
57576b6f9b7SJan Kara 				 */
57676b6f9b7SJan Kara 				if (index > end) {
57776b6f9b7SJan Kara 					unlock_page(page);
578fc127da0SKirill A. Shutemov 					continue;
579fc127da0SKirill A. Shutemov 				}
580ef18a1caSKirill A. Shutemov 
581ef18a1caSKirill A. Shutemov 				/* Take a pin outside pagevec */
582ef18a1caSKirill A. Shutemov 				get_page(page);
583ef18a1caSKirill A. Shutemov 
584ef18a1caSKirill A. Shutemov 				/*
585ef18a1caSKirill A. Shutemov 				 * Drop extra pins before trying to invalidate
586ef18a1caSKirill A. Shutemov 				 * the huge page.
587ef18a1caSKirill A. Shutemov 				 */
588ef18a1caSKirill A. Shutemov 				pagevec_remove_exceptionals(&pvec);
589ef18a1caSKirill A. Shutemov 				pagevec_release(&pvec);
59076b6f9b7SJan Kara 			}
591fc127da0SKirill A. Shutemov 
59231560180SMinchan Kim 			ret = invalidate_inode_page(page);
5931da177e4SLinus Torvalds 			unlock_page(page);
59431560180SMinchan Kim 			/*
59531560180SMinchan Kim 			 * Invalidation is a hint that the page is no longer
59631560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
59731560180SMinchan Kim 			 */
598eb1d7a65SYafang Shao 			if (!ret) {
599cc5993bdSMinchan Kim 				deactivate_file_page(page);
600eb1d7a65SYafang Shao 				/* It is likely on the pagevec of a remote CPU */
601eb1d7a65SYafang Shao 				if (nr_pagevec)
602eb1d7a65SYafang Shao 					(*nr_pagevec)++;
603eb1d7a65SYafang Shao 			}
604eb1d7a65SYafang Shao 
605ef18a1caSKirill A. Shutemov 			if (PageTransHuge(page))
606ef18a1caSKirill A. Shutemov 				put_page(page);
60731560180SMinchan Kim 			count += ret;
6081da177e4SLinus Torvalds 		}
6090cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6101da177e4SLinus Torvalds 		pagevec_release(&pvec);
611fc9a07e7SAndrew Morton 		cond_resched();
612b85e0effSHugh Dickins 		index++;
6131da177e4SLinus Torvalds 	}
61431560180SMinchan Kim 	return count;
6151da177e4SLinus Torvalds }
616eb1d7a65SYafang Shao 
617eb1d7a65SYafang Shao /**
618eb1d7a65SYafang Shao  * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
619eb1d7a65SYafang Shao  * @mapping: the address_space which holds the pages to invalidate
620eb1d7a65SYafang Shao  * @start: the offset 'from' which to invalidate
621eb1d7a65SYafang Shao  * @end: the offset 'to' which to invalidate (inclusive)
622eb1d7a65SYafang Shao  *
623eb1d7a65SYafang Shao  * This function only removes the unlocked pages, if you want to
624eb1d7a65SYafang Shao  * remove all the pages of one inode, you must call truncate_inode_pages.
625eb1d7a65SYafang Shao  *
626eb1d7a65SYafang Shao  * invalidate_mapping_pages() will not block on IO activity. It will not
627eb1d7a65SYafang Shao  * invalidate pages which are dirty, locked, under writeback or mapped into
628eb1d7a65SYafang Shao  * pagetables.
629eb1d7a65SYafang Shao  *
630eb1d7a65SYafang Shao  * Return: the number of the pages that were invalidated
631eb1d7a65SYafang Shao  */
632eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping,
633eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end)
634eb1d7a65SYafang Shao {
635eb1d7a65SYafang Shao 	return __invalidate_mapping_pages(mapping, start, end, NULL);
636eb1d7a65SYafang Shao }
63754bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
6381da177e4SLinus Torvalds 
639eb1d7a65SYafang Shao /**
640eb1d7a65SYafang Shao  * This helper is similar with the above one, except that it accounts for pages
641eb1d7a65SYafang Shao  * that are likely on a pagevec and count them in @nr_pagevec, which will used by
642eb1d7a65SYafang Shao  * the caller.
643eb1d7a65SYafang Shao  */
644eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping,
645eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
646eb1d7a65SYafang Shao {
647eb1d7a65SYafang Shao 	__invalidate_mapping_pages(mapping, start, end, nr_pagevec);
648eb1d7a65SYafang Shao }
649eb1d7a65SYafang Shao 
650bd4c8ce4SAndrew Morton /*
651bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
652bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
653bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
6542706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
6552706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
656bd4c8ce4SAndrew Morton  */
657bd4c8ce4SAndrew Morton static int
658bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
659bd4c8ce4SAndrew Morton {
660c4843a75SGreg Thelen 	unsigned long flags;
661c4843a75SGreg Thelen 
662bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
663bd4c8ce4SAndrew Morton 		return 0;
664bd4c8ce4SAndrew Morton 
665266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
666bd4c8ce4SAndrew Morton 		return 0;
667bd4c8ce4SAndrew Morton 
668b93b0163SMatthew Wilcox 	xa_lock_irqsave(&mapping->i_pages, flags);
669bd4c8ce4SAndrew Morton 	if (PageDirty(page))
670bd4c8ce4SAndrew Morton 		goto failed;
671bd4c8ce4SAndrew Morton 
672266cf658SDavid Howells 	BUG_ON(page_has_private(page));
67362cccb8cSJohannes Weiner 	__delete_from_page_cache(page, NULL);
674b93b0163SMatthew Wilcox 	xa_unlock_irqrestore(&mapping->i_pages, flags);
6756072d13cSLinus Torvalds 
6766072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
6776072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
6786072d13cSLinus Torvalds 
67909cbfeafSKirill A. Shutemov 	put_page(page);	/* pagecache ref */
680bd4c8ce4SAndrew Morton 	return 1;
681bd4c8ce4SAndrew Morton failed:
682b93b0163SMatthew Wilcox 	xa_unlock_irqrestore(&mapping->i_pages, flags);
683bd4c8ce4SAndrew Morton 	return 0;
684bd4c8ce4SAndrew Morton }
685bd4c8ce4SAndrew Morton 
686e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
687e3db7691STrond Myklebust {
688e3db7691STrond Myklebust 	if (!PageDirty(page))
689e3db7691STrond Myklebust 		return 0;
690e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
691e3db7691STrond Myklebust 		return 0;
692e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
693e3db7691STrond Myklebust }
694e3db7691STrond Myklebust 
6951da177e4SLinus Torvalds /**
6961da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
69767be2dd1SMartin Waitz  * @mapping: the address_space
6981da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
6991da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
7001da177e4SLinus Torvalds  *
7011da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
7021da177e4SLinus Torvalds  * invalidation.
7031da177e4SLinus Torvalds  *
704a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
7051da177e4SLinus Torvalds  */
7061da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
7071da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
7081da177e4SLinus Torvalds {
7090cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
7101da177e4SLinus Torvalds 	struct pagevec pvec;
711b85e0effSHugh Dickins 	pgoff_t index;
7121da177e4SLinus Torvalds 	int i;
7131da177e4SLinus Torvalds 	int ret = 0;
7140dd1334fSHisashi Hifumi 	int ret2 = 0;
7151da177e4SLinus Torvalds 	int did_range_unmap = 0;
7161da177e4SLinus Torvalds 
71732691f0fSAndrey Ryabinin 	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
71834ccb69eSAndrey Ryabinin 		goto out;
71932691f0fSAndrey Ryabinin 
72086679820SMel Gorman 	pagevec_init(&pvec);
721b85e0effSHugh Dickins 	index = start;
7220cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
7230cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
7240cd6144aSJohannes Weiner 			indices)) {
7257b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
7261da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
727b85e0effSHugh Dickins 
728b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
7290cd6144aSJohannes Weiner 			index = indices[i];
730b85e0effSHugh Dickins 			if (index > end)
731b85e0effSHugh Dickins 				break;
7321da177e4SLinus Torvalds 
7333159f943SMatthew Wilcox 			if (xa_is_value(page)) {
734c6dcf52cSJan Kara 				if (!invalidate_exceptional_entry2(mapping,
735c6dcf52cSJan Kara 								   index, page))
736c6dcf52cSJan Kara 					ret = -EBUSY;
7370cd6144aSJohannes Weiner 				continue;
7380cd6144aSJohannes Weiner 			}
7390cd6144aSJohannes Weiner 
7401da177e4SLinus Torvalds 			lock_page(page);
7415cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
7421da177e4SLinus Torvalds 			if (page->mapping != mapping) {
7431da177e4SLinus Torvalds 				unlock_page(page);
7441da177e4SLinus Torvalds 				continue;
7451da177e4SLinus Torvalds 			}
7461da177e4SLinus Torvalds 			wait_on_page_writeback(page);
747d00806b1SNick Piggin 			if (page_mapped(page)) {
7481da177e4SLinus Torvalds 				if (!did_range_unmap) {
7491da177e4SLinus Torvalds 					/*
7501da177e4SLinus Torvalds 					 * Zap the rest of the file in one hit.
7511da177e4SLinus Torvalds 					 */
752977fbdcdSMatthew Wilcox 					unmap_mapping_pages(mapping, index,
753977fbdcdSMatthew Wilcox 						(1 + end - index), false);
7541da177e4SLinus Torvalds 					did_range_unmap = 1;
7551da177e4SLinus Torvalds 				} else {
7561da177e4SLinus Torvalds 					/*
7571da177e4SLinus Torvalds 					 * Just zap this page
7581da177e4SLinus Torvalds 					 */
759977fbdcdSMatthew Wilcox 					unmap_mapping_pages(mapping, index,
760977fbdcdSMatthew Wilcox 								1, false);
7611da177e4SLinus Torvalds 				}
7621da177e4SLinus Torvalds 			}
763d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
7640dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
7650dd1334fSHisashi Hifumi 			if (ret2 == 0) {
7660dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
7676ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
7680dd1334fSHisashi Hifumi 			}
7690dd1334fSHisashi Hifumi 			if (ret2 < 0)
7700dd1334fSHisashi Hifumi 				ret = ret2;
7711da177e4SLinus Torvalds 			unlock_page(page);
7721da177e4SLinus Torvalds 		}
7730cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
7741da177e4SLinus Torvalds 		pagevec_release(&pvec);
7751da177e4SLinus Torvalds 		cond_resched();
776b85e0effSHugh Dickins 		index++;
7771da177e4SLinus Torvalds 	}
778cd656375SJan Kara 	/*
77969b6c131SMatthew Wilcox 	 * For DAX we invalidate page tables after invalidating page cache.  We
780cd656375SJan Kara 	 * could invalidate page tables while invalidating each entry however
781cd656375SJan Kara 	 * that would be expensive. And doing range unmapping before doesn't
78269b6c131SMatthew Wilcox 	 * work as we have no cheap way to find whether page cache entry didn't
783cd656375SJan Kara 	 * get remapped later.
784cd656375SJan Kara 	 */
785cd656375SJan Kara 	if (dax_mapping(mapping)) {
786977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, start, end - start + 1, false);
787cd656375SJan Kara 	}
78834ccb69eSAndrey Ryabinin out:
7893167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
7901da177e4SLinus Torvalds 	return ret;
7911da177e4SLinus Torvalds }
7921da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds /**
7951da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
79667be2dd1SMartin Waitz  * @mapping: the address_space
7971da177e4SLinus Torvalds  *
7981da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
7991da177e4SLinus Torvalds  * invalidation.
8001da177e4SLinus Torvalds  *
801a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
8021da177e4SLinus Torvalds  */
8031da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
8041da177e4SLinus Torvalds {
8051da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
8061da177e4SLinus Torvalds }
8071da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
80825d9e2d1Snpiggin@suse.de 
80925d9e2d1Snpiggin@suse.de /**
81025d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
81125d9e2d1Snpiggin@suse.de  * @inode: inode
8128a549beaSHugh Dickins  * @newsize: new file size
81325d9e2d1Snpiggin@suse.de  *
81425d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
81525d9e2d1Snpiggin@suse.de  * is called.
81625d9e2d1Snpiggin@suse.de  *
81725d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
81825d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
81925d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
82025d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
82125d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
82225d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
82325d9e2d1Snpiggin@suse.de  */
8247caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
82525d9e2d1Snpiggin@suse.de {
82625d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
8278a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
82825d9e2d1Snpiggin@suse.de 
82925d9e2d1Snpiggin@suse.de 	/*
83025d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
83125d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
83225d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
83325d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
83425d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
83525d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
83625d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
83725d9e2d1Snpiggin@suse.de 	 */
8388a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
8398a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
8408a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
84125d9e2d1Snpiggin@suse.de }
84225d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
84325d9e2d1Snpiggin@suse.de 
84425d9e2d1Snpiggin@suse.de /**
8452c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
8462c27c65eSChristoph Hellwig  * @inode: inode
8472c27c65eSChristoph Hellwig  * @newsize: new file size
8482c27c65eSChristoph Hellwig  *
849382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
850382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
851382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
8522c27c65eSChristoph Hellwig  *
85377783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
85477783d06SJan Kara  * i_mutex but e.g. xfs uses a different lock) and before all filesystem
85577783d06SJan Kara  * specific block truncation has been performed.
8562c27c65eSChristoph Hellwig  */
8572c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
8582c27c65eSChristoph Hellwig {
85990a80202SJan Kara 	loff_t oldsize = inode->i_size;
86090a80202SJan Kara 
8612c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
86290a80202SJan Kara 	if (newsize > oldsize)
86390a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
8647caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
8652c27c65eSChristoph Hellwig }
8662c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
8672c27c65eSChristoph Hellwig 
8682c27c65eSChristoph Hellwig /**
86990a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
87090a80202SJan Kara  * @inode:	inode for which i_size was extended
87190a80202SJan Kara  * @from:	original inode size
87290a80202SJan Kara  * @to:		new inode size
87390a80202SJan Kara  *
87490a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
87590a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
87690a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
87790a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
87890a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
87990a80202SJan Kara  * changed.
88090a80202SJan Kara  *
88190a80202SJan Kara  * The function must be called after i_size is updated so that page fault
88290a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
88390a80202SJan Kara  * The function must be called while we still hold i_mutex - this not only
88490a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
88590a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
88690a80202SJan Kara  */
88790a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
88890a80202SJan Kara {
88993407472SFabian Frederick 	int bsize = i_blocksize(inode);
89090a80202SJan Kara 	loff_t rounded_from;
89190a80202SJan Kara 	struct page *page;
89290a80202SJan Kara 	pgoff_t index;
89390a80202SJan Kara 
89490a80202SJan Kara 	WARN_ON(to > inode->i_size);
89590a80202SJan Kara 
89609cbfeafSKirill A. Shutemov 	if (from >= to || bsize == PAGE_SIZE)
89790a80202SJan Kara 		return;
89890a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
89990a80202SJan Kara 	rounded_from = round_up(from, bsize);
90009cbfeafSKirill A. Shutemov 	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
90190a80202SJan Kara 		return;
90290a80202SJan Kara 
90309cbfeafSKirill A. Shutemov 	index = from >> PAGE_SHIFT;
90490a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
90590a80202SJan Kara 	/* Page not cached? Nothing to do */
90690a80202SJan Kara 	if (!page)
90790a80202SJan Kara 		return;
90890a80202SJan Kara 	/*
90990a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
91090a80202SJan Kara 	 * is needed.
91190a80202SJan Kara 	 */
91290a80202SJan Kara 	if (page_mkclean(page))
91390a80202SJan Kara 		set_page_dirty(page);
91490a80202SJan Kara 	unlock_page(page);
91509cbfeafSKirill A. Shutemov 	put_page(page);
91690a80202SJan Kara }
91790a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
91890a80202SJan Kara 
91990a80202SJan Kara /**
920623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
921623e3db9SHugh Dickins  * @inode: inode
922623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
923623e3db9SHugh Dickins  * @lend: offset of last byte of hole
924623e3db9SHugh Dickins  *
925623e3db9SHugh Dickins  * This function should typically be called before the filesystem
926623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
927623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
928623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
929623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
930623e3db9SHugh Dickins  * had its underlying blocks deallocated.
931623e3db9SHugh Dickins  */
932623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
933623e3db9SHugh Dickins {
934623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
935623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
936623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
937623e3db9SHugh Dickins 	/*
938623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
939623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
940623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
9415a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
9425a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
943623e3db9SHugh Dickins 	 */
944623e3db9SHugh Dickins 
945623e3db9SHugh Dickins 	/*
946623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
947623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
948623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
949623e3db9SHugh Dickins 	 */
950623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
951623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
952623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
953623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
954623e3db9SHugh Dickins }
955623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
956