xref: /linux/mm/truncate.c (revision 7ae12c809f6a31d3da7b96339dbefa141884c711)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * mm/truncate.c - code for taking down pages from address_spaces
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds
61da177e4SLinus Torvalds  *
7e1f8e874SFrancois Cami  * 10Sep2002	Andrew Morton
81da177e4SLinus Torvalds  *		Initial version.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
124af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
13f9fe48beSRoss Zwisler #include <linux/dax.h>
145a0e3ad6STejun Heo #include <linux/gfp.h>
151da177e4SLinus Torvalds #include <linux/mm.h>
160fd0e6b0SNick Piggin #include <linux/swap.h>
17b95f1b31SPaul Gortmaker #include <linux/export.h>
181da177e4SLinus Torvalds #include <linux/pagemap.h>
1901f2705dSNate Diller #include <linux/highmem.h>
201da177e4SLinus Torvalds #include <linux/pagevec.h>
21e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h>
221da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* grr. try_to_release_page,
23aaa4059bSJan Kara 				   do_invalidatepage */
243a4f8a0bSHugh Dickins #include <linux/shmem_fs.h>
25c515e1fdSDan Magenheimer #include <linux/cleancache.h>
2690a80202SJan Kara #include <linux/rmap.h>
27ba470de4SRik van Riel #include "internal.h"
281da177e4SLinus Torvalds 
29f2187599SMel Gorman /*
30f2187599SMel Gorman  * Regular page slots are stabilized by the page lock even without the tree
31f2187599SMel Gorman  * itself locked.  These unlocked entries need verification under the tree
32f2187599SMel Gorman  * lock.
33f2187599SMel Gorman  */
34f2187599SMel Gorman static inline void __clear_shadow_entry(struct address_space *mapping,
35f2187599SMel Gorman 				pgoff_t index, void *entry)
360cd6144aSJohannes Weiner {
3769b6c131SMatthew Wilcox 	XA_STATE(xas, &mapping->i_pages, index);
38449dd698SJohannes Weiner 
3969b6c131SMatthew Wilcox 	xas_set_update(&xas, workingset_update_node);
4069b6c131SMatthew Wilcox 	if (xas_load(&xas) != entry)
41f2187599SMel Gorman 		return;
4269b6c131SMatthew Wilcox 	xas_store(&xas, NULL);
43f2187599SMel Gorman }
44f2187599SMel Gorman 
45f2187599SMel Gorman static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
46f2187599SMel Gorman 			       void *entry)
47f2187599SMel Gorman {
48b93b0163SMatthew Wilcox 	xa_lock_irq(&mapping->i_pages);
49f2187599SMel Gorman 	__clear_shadow_entry(mapping, index, entry);
50b93b0163SMatthew Wilcox 	xa_unlock_irq(&mapping->i_pages);
510cd6144aSJohannes Weiner }
521da177e4SLinus Torvalds 
53c6dcf52cSJan Kara /*
54f2187599SMel Gorman  * Unconditionally remove exceptional entries. Usually called from truncate
55f2187599SMel Gorman  * path. Note that the pagevec may be altered by this function by removing
56f2187599SMel Gorman  * exceptional entries similar to what pagevec_remove_exceptionals does.
57c6dcf52cSJan Kara  */
58f2187599SMel Gorman static void truncate_exceptional_pvec_entries(struct address_space *mapping,
5931d270fdSMatthew Wilcox (Oracle) 				struct pagevec *pvec, pgoff_t *indices)
60c6dcf52cSJan Kara {
61f2187599SMel Gorman 	int i, j;
6231d270fdSMatthew Wilcox (Oracle) 	bool dax;
63f2187599SMel Gorman 
64c6dcf52cSJan Kara 	/* Handled by shmem itself */
65c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
66c6dcf52cSJan Kara 		return;
67c6dcf52cSJan Kara 
68f2187599SMel Gorman 	for (j = 0; j < pagevec_count(pvec); j++)
693159f943SMatthew Wilcox 		if (xa_is_value(pvec->pages[j]))
70f2187599SMel Gorman 			break;
71f2187599SMel Gorman 
72f2187599SMel Gorman 	if (j == pagevec_count(pvec))
73c6dcf52cSJan Kara 		return;
74f2187599SMel Gorman 
75f2187599SMel Gorman 	dax = dax_mapping(mapping);
7631d270fdSMatthew Wilcox (Oracle) 	if (!dax)
77b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
78f2187599SMel Gorman 
79f2187599SMel Gorman 	for (i = j; i < pagevec_count(pvec); i++) {
80f2187599SMel Gorman 		struct page *page = pvec->pages[i];
81f2187599SMel Gorman 		pgoff_t index = indices[i];
82f2187599SMel Gorman 
833159f943SMatthew Wilcox 		if (!xa_is_value(page)) {
84f2187599SMel Gorman 			pvec->pages[j++] = page;
85f2187599SMel Gorman 			continue;
86c6dcf52cSJan Kara 		}
87f2187599SMel Gorman 
88f2187599SMel Gorman 		if (unlikely(dax)) {
89f2187599SMel Gorman 			dax_delete_mapping_entry(mapping, index);
90f2187599SMel Gorman 			continue;
91f2187599SMel Gorman 		}
92f2187599SMel Gorman 
93f2187599SMel Gorman 		__clear_shadow_entry(mapping, index, page);
94f2187599SMel Gorman 	}
95f2187599SMel Gorman 
9631d270fdSMatthew Wilcox (Oracle) 	if (!dax)
97b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
98f2187599SMel Gorman 	pvec->nr = j;
99c6dcf52cSJan Kara }
100c6dcf52cSJan Kara 
101c6dcf52cSJan Kara /*
102c6dcf52cSJan Kara  * Invalidate exceptional entry if easily possible. This handles exceptional
1034636e70bSRoss Zwisler  * entries for invalidate_inode_pages().
104c6dcf52cSJan Kara  */
105c6dcf52cSJan Kara static int invalidate_exceptional_entry(struct address_space *mapping,
106c6dcf52cSJan Kara 					pgoff_t index, void *entry)
107c6dcf52cSJan Kara {
1084636e70bSRoss Zwisler 	/* Handled by shmem itself, or for DAX we do nothing. */
1094636e70bSRoss Zwisler 	if (shmem_mapping(mapping) || dax_mapping(mapping))
110c6dcf52cSJan Kara 		return 1;
111c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
112c6dcf52cSJan Kara 	return 1;
113c6dcf52cSJan Kara }
114c6dcf52cSJan Kara 
115c6dcf52cSJan Kara /*
116c6dcf52cSJan Kara  * Invalidate exceptional entry if clean. This handles exceptional entries for
117c6dcf52cSJan Kara  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
118c6dcf52cSJan Kara  */
119c6dcf52cSJan Kara static int invalidate_exceptional_entry2(struct address_space *mapping,
120c6dcf52cSJan Kara 					 pgoff_t index, void *entry)
121c6dcf52cSJan Kara {
122c6dcf52cSJan Kara 	/* Handled by shmem itself */
123c6dcf52cSJan Kara 	if (shmem_mapping(mapping))
124c6dcf52cSJan Kara 		return 1;
125c6dcf52cSJan Kara 	if (dax_mapping(mapping))
126c6dcf52cSJan Kara 		return dax_invalidate_mapping_entry_sync(mapping, index);
127c6dcf52cSJan Kara 	clear_shadow_entry(mapping, index, entry);
128c6dcf52cSJan Kara 	return 1;
129c6dcf52cSJan Kara }
130c6dcf52cSJan Kara 
131cf9a2ae8SDavid Howells /**
13228bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
133cf9a2ae8SDavid Howells  * @page: the page which is affected
134d47992f8SLukas Czerner  * @offset: start of the range to invalidate
135d47992f8SLukas Czerner  * @length: length of the range to invalidate
136cf9a2ae8SDavid Howells  *
137cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
138cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
139cf9a2ae8SDavid Howells  *
140cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
141cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
142cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
143cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
144cf9a2ae8SDavid Howells  * blocks on-disk.
145cf9a2ae8SDavid Howells  */
146d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
147d47992f8SLukas Czerner 		       unsigned int length)
148cf9a2ae8SDavid Howells {
149d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
150d47992f8SLukas Czerner 
151cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
1529361401eSDavid Howells #ifdef CONFIG_BLOCK
153cf9a2ae8SDavid Howells 	if (!invalidatepage)
154cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
1559361401eSDavid Howells #endif
156cf9a2ae8SDavid Howells 	if (invalidatepage)
157d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
158cf9a2ae8SDavid Howells }
159cf9a2ae8SDavid Howells 
160ecdfc978SLinus Torvalds /*
1611da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
16262e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
16354cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1641da177e4SLinus Torvalds  *
165fc3a5ac5SMatthew Wilcox (Oracle)  * We need to bail out if page->mapping is no longer equal to the original
1661da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
167fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1681da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1691da177e4SLinus Torvalds  */
17022061a1fSHugh Dickins static void truncate_cleanup_page(struct page *page)
1711da177e4SLinus Torvalds {
17222061a1fSHugh Dickins 	if (page_mapped(page))
17322061a1fSHugh Dickins 		unmap_mapping_page(page);
1741da177e4SLinus Torvalds 
175266cf658SDavid Howells 	if (page_has_private(page))
176fc3a5ac5SMatthew Wilcox (Oracle) 		do_invalidatepage(page, 0, thp_size(page));
1771da177e4SLinus Torvalds 
178b9ea2515SKonstantin Khlebnikov 	/*
179b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
180b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
181b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
182b9ea2515SKonstantin Khlebnikov 	 */
18311f81becSTejun Heo 	cancel_dirty_page(page);
1841da177e4SLinus Torvalds 	ClearPageMappedToDisk(page);
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
188fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
1891da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
1900fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
1910fd0e6b0SNick Piggin  * discards clean, unused pages.
1921da177e4SLinus Torvalds  *
1931da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds static int
1961da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
1971da177e4SLinus Torvalds {
1980fd0e6b0SNick Piggin 	int ret;
1990fd0e6b0SNick Piggin 
2001da177e4SLinus Torvalds 	if (page->mapping != mapping)
2011da177e4SLinus Torvalds 		return 0;
2021da177e4SLinus Torvalds 
203266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
2041da177e4SLinus Torvalds 		return 0;
2051da177e4SLinus Torvalds 
2060fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
2070fd0e6b0SNick Piggin 
2080fd0e6b0SNick Piggin 	return ret;
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
211750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page)
212750b4987SNick Piggin {
213fc127da0SKirill A. Shutemov 	VM_BUG_ON_PAGE(PageTail(page), page);
214fc127da0SKirill A. Shutemov 
2159f4e41f4SJan Kara 	if (page->mapping != mapping)
2169f4e41f4SJan Kara 		return -EIO;
2179f4e41f4SJan Kara 
21822061a1fSHugh Dickins 	truncate_cleanup_page(page);
2199f4e41f4SJan Kara 	delete_from_page_cache(page);
2209f4e41f4SJan Kara 	return 0;
221750b4987SNick Piggin }
222750b4987SNick Piggin 
22383f78668SWu Fengguang /*
22425718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
22525718736SAndi Kleen  */
22625718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
22725718736SAndi Kleen {
22825718736SAndi Kleen 	if (!mapping)
22925718736SAndi Kleen 		return -EINVAL;
23025718736SAndi Kleen 	/*
23125718736SAndi Kleen 	 * Only punch for normal data pages for now.
23225718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
23325718736SAndi Kleen 	 */
23425718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
23525718736SAndi Kleen 		return -EIO;
23625718736SAndi Kleen 	return truncate_inode_page(mapping, page);
23725718736SAndi Kleen }
23825718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
23925718736SAndi Kleen 
24025718736SAndi Kleen /*
24183f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
24283f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
24383f78668SWu Fengguang  *
24483f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
24583f78668SWu Fengguang  */
24683f78668SWu Fengguang int invalidate_inode_page(struct page *page)
24783f78668SWu Fengguang {
24883f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
24983f78668SWu Fengguang 	if (!mapping)
25083f78668SWu Fengguang 		return 0;
25183f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
25283f78668SWu Fengguang 		return 0;
25383f78668SWu Fengguang 	if (page_mapped(page))
25483f78668SWu Fengguang 		return 0;
25583f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
25683f78668SWu Fengguang }
25783f78668SWu Fengguang 
2581da177e4SLinus Torvalds /**
25973c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2601da177e4SLinus Torvalds  * @mapping: mapping to truncate
2611da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2625a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2631da177e4SLinus Torvalds  *
264d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2655a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2665a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2671da177e4SLinus Torvalds  *
2681da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2691da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2701da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2711da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2721da177e4SLinus Torvalds  * is low.
2731da177e4SLinus Torvalds  *
2741da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2751da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2761da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2775a720394SLukas Czerner  *
2785a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2795a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2805a720394SLukas Czerner  * page aligned properly.
2811da177e4SLinus Torvalds  */
282d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
283d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2841da177e4SLinus Torvalds {
2855a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2865a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2875a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2885a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
2891da177e4SLinus Torvalds 	struct pagevec	pvec;
2900cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
291b85e0effSHugh Dickins 	pgoff_t		index;
2921da177e4SLinus Torvalds 	int		i;
2931da177e4SLinus Torvalds 
2947716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
29534ccb69eSAndrey Ryabinin 		goto out;
2961da177e4SLinus Torvalds 
2975a720394SLukas Czerner 	/* Offsets within partial pages */
29809cbfeafSKirill A. Shutemov 	partial_start = lstart & (PAGE_SIZE - 1);
29909cbfeafSKirill A. Shutemov 	partial_end = (lend + 1) & (PAGE_SIZE - 1);
3005a720394SLukas Czerner 
3015a720394SLukas Czerner 	/*
3025a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
3035a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
3045a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
3055a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
3065a720394SLukas Czerner 	 */
30709cbfeafSKirill A. Shutemov 	start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
3085a720394SLukas Czerner 	if (lend == -1)
3095a720394SLukas Czerner 		/*
3105a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
3115a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
3125a720394SLukas Czerner 		 * unsigned we're using -1.
3135a720394SLukas Czerner 		 */
3145a720394SLukas Czerner 		end = -1;
3155a720394SLukas Czerner 	else
31609cbfeafSKirill A. Shutemov 		end = (lend + 1) >> PAGE_SHIFT;
317d7339071SHans Reiser 
31886679820SMel Gorman 	pagevec_init(&pvec);
319b85e0effSHugh Dickins 	index = start;
3205c211ba2SMatthew Wilcox (Oracle) 	while (index < end && find_lock_entries(mapping, index, end - 1,
3215c211ba2SMatthew Wilcox (Oracle) 			&pvec, indices)) {
3225c211ba2SMatthew Wilcox (Oracle) 		index = indices[pagevec_count(&pvec) - 1] + 1;
32331d270fdSMatthew Wilcox (Oracle) 		truncate_exceptional_pvec_entries(mapping, &pvec, indices);
3245c211ba2SMatthew Wilcox (Oracle) 		for (i = 0; i < pagevec_count(&pvec); i++)
32522061a1fSHugh Dickins 			truncate_cleanup_page(pvec.pages[i]);
3265c211ba2SMatthew Wilcox (Oracle) 		delete_from_page_cache_batch(mapping, &pvec);
3275c211ba2SMatthew Wilcox (Oracle) 		for (i = 0; i < pagevec_count(&pvec); i++)
3285c211ba2SMatthew Wilcox (Oracle) 			unlock_page(pvec.pages[i]);
3291da177e4SLinus Torvalds 		pagevec_release(&pvec);
3301da177e4SLinus Torvalds 		cond_resched();
3311da177e4SLinus Torvalds 	}
3325c211ba2SMatthew Wilcox (Oracle) 
3335a720394SLukas Czerner 	if (partial_start) {
3341da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3351da177e4SLinus Torvalds 		if (page) {
33609cbfeafSKirill A. Shutemov 			unsigned int top = PAGE_SIZE;
3375a720394SLukas Czerner 			if (start > end) {
3385a720394SLukas Czerner 				/* Truncation within a single page */
3395a720394SLukas Czerner 				top = partial_end;
3405a720394SLukas Czerner 				partial_end = 0;
3415a720394SLukas Czerner 			}
3421da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3435a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3445a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3455a720394SLukas Czerner 			if (page_has_private(page))
3465a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3475a720394SLukas Czerner 						  top - partial_start);
3481da177e4SLinus Torvalds 			unlock_page(page);
34909cbfeafSKirill A. Shutemov 			put_page(page);
3501da177e4SLinus Torvalds 		}
3511da177e4SLinus Torvalds 	}
3525a720394SLukas Czerner 	if (partial_end) {
3535a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3545a720394SLukas Czerner 		if (page) {
3555a720394SLukas Czerner 			wait_on_page_writeback(page);
3565a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
3575a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3585a720394SLukas Czerner 			if (page_has_private(page))
3595a720394SLukas Czerner 				do_invalidatepage(page, 0,
3605a720394SLukas Czerner 						  partial_end);
3615a720394SLukas Czerner 			unlock_page(page);
36209cbfeafSKirill A. Shutemov 			put_page(page);
3635a720394SLukas Czerner 		}
3645a720394SLukas Czerner 	}
3655a720394SLukas Czerner 	/*
3665a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
3675a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
3685a720394SLukas Czerner 	 */
3695a720394SLukas Czerner 	if (start >= end)
37034ccb69eSAndrey Ryabinin 		goto out;
3711da177e4SLinus Torvalds 
372b85e0effSHugh Dickins 	index = start;
3731da177e4SLinus Torvalds 	for ( ; ; ) {
3741da177e4SLinus Torvalds 		cond_resched();
375a656a202SMatthew Wilcox (Oracle) 		if (!find_get_entries(mapping, index, end - 1, &pvec,
37638cefeb3SMatthew Wilcox (Oracle) 				indices)) {
377792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
378b85e0effSHugh Dickins 			if (index == start)
3791da177e4SLinus Torvalds 				break;
380792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
381b85e0effSHugh Dickins 			index = start;
3821da177e4SLinus Torvalds 			continue;
3831da177e4SLinus Torvalds 		}
384f2187599SMel Gorman 
3851da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3861da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
3871da177e4SLinus Torvalds 
388b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
3890cd6144aSJohannes Weiner 			index = indices[i];
390b85e0effSHugh Dickins 
3913159f943SMatthew Wilcox 			if (xa_is_value(page))
3920cd6144aSJohannes Weiner 				continue;
3930cd6144aSJohannes Weiner 
3941da177e4SLinus Torvalds 			lock_page(page);
3955cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
3961da177e4SLinus Torvalds 			wait_on_page_writeback(page);
397750b4987SNick Piggin 			truncate_inode_page(mapping, page);
3981da177e4SLinus Torvalds 			unlock_page(page);
3991da177e4SLinus Torvalds 		}
40031d270fdSMatthew Wilcox (Oracle) 		truncate_exceptional_pvec_entries(mapping, &pvec, indices);
4011da177e4SLinus Torvalds 		pagevec_release(&pvec);
402b85e0effSHugh Dickins 		index++;
4031da177e4SLinus Torvalds 	}
40434ccb69eSAndrey Ryabinin 
40534ccb69eSAndrey Ryabinin out:
4063167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
4071da177e4SLinus Torvalds }
408d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
4091da177e4SLinus Torvalds 
410d7339071SHans Reiser /**
411d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
412d7339071SHans Reiser  * @mapping: mapping to truncate
413d7339071SHans Reiser  * @lstart: offset from which to truncate
414d7339071SHans Reiser  *
4151b1dcc1bSJes Sorensen  * Called under (and serialised by) inode->i_mutex.
41608142579SJan Kara  *
41708142579SJan Kara  * Note: When this function returns, there can be a page in the process of
41808142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
41908142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
42008142579SJan Kara  * truncation of the whole mapping.
421d7339071SHans Reiser  */
422d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
423d7339071SHans Reiser {
424d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
425d7339071SHans Reiser }
4261da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4271da177e4SLinus Torvalds 
42828697355SMike Waychison /**
42991b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
43091b0abe3SJohannes Weiner  * @mapping: mapping to truncate
43191b0abe3SJohannes Weiner  *
43291b0abe3SJohannes Weiner  * Called under (and serialized by) inode->i_mutex.
43391b0abe3SJohannes Weiner  *
43491b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
43591b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
43691b0abe3SJohannes Weiner  */
43791b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
43891b0abe3SJohannes Weiner {
43991b0abe3SJohannes Weiner 	/*
44091b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
44191b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
44291b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
44391b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
44491b0abe3SJohannes Weiner 	 * final truncate has begun.
44591b0abe3SJohannes Weiner 	 */
44691b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
44791b0abe3SJohannes Weiner 
4487716506aSMatthew Wilcox (Oracle) 	if (!mapping_empty(mapping)) {
44991b0abe3SJohannes Weiner 		/*
45091b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
45191b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
45291b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
45391b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
45491b0abe3SJohannes Weiner 		 */
455b93b0163SMatthew Wilcox 		xa_lock_irq(&mapping->i_pages);
456b93b0163SMatthew Wilcox 		xa_unlock_irq(&mapping->i_pages);
45791b0abe3SJohannes Weiner 	}
4586ff38bd4SPavel Tikhomirov 
4596ff38bd4SPavel Tikhomirov 	/*
4606ff38bd4SPavel Tikhomirov 	 * Cleancache needs notification even if there are no pages or shadow
4616ff38bd4SPavel Tikhomirov 	 * entries.
4626ff38bd4SPavel Tikhomirov 	 */
4636ff38bd4SPavel Tikhomirov 	truncate_inode_pages(mapping, 0);
46491b0abe3SJohannes Weiner }
46591b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
46691b0abe3SJohannes Weiner 
467a77eedbcSJason Yan static unsigned long __invalidate_mapping_pages(struct address_space *mapping,
468eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
4691da177e4SLinus Torvalds {
4700cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
4711da177e4SLinus Torvalds 	struct pagevec pvec;
472b85e0effSHugh Dickins 	pgoff_t index = start;
47331560180SMinchan Kim 	unsigned long ret;
47431560180SMinchan Kim 	unsigned long count = 0;
4751da177e4SLinus Torvalds 	int i;
4761da177e4SLinus Torvalds 
47786679820SMel Gorman 	pagevec_init(&pvec);
4785c211ba2SMatthew Wilcox (Oracle) 	while (find_lock_entries(mapping, index, end, &pvec, indices)) {
4791da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
4801da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
4811da177e4SLinus Torvalds 
482b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4830cd6144aSJohannes Weiner 			index = indices[i];
484e0f23603SNeilBrown 
4853159f943SMatthew Wilcox 			if (xa_is_value(page)) {
486*7ae12c80SJohannes Weiner 				count += invalidate_exceptional_entry(mapping,
487*7ae12c80SJohannes Weiner 								      index,
488c6dcf52cSJan Kara 								      page);
4890cd6144aSJohannes Weiner 				continue;
4900cd6144aSJohannes Weiner 			}
4915c211ba2SMatthew Wilcox (Oracle) 			index += thp_nr_pages(page) - 1;
492fc127da0SKirill A. Shutemov 
49331560180SMinchan Kim 			ret = invalidate_inode_page(page);
4941da177e4SLinus Torvalds 			unlock_page(page);
49531560180SMinchan Kim 			/*
49631560180SMinchan Kim 			 * Invalidation is a hint that the page is no longer
49731560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
49831560180SMinchan Kim 			 */
499eb1d7a65SYafang Shao 			if (!ret) {
500cc5993bdSMinchan Kim 				deactivate_file_page(page);
501eb1d7a65SYafang Shao 				/* It is likely on the pagevec of a remote CPU */
502eb1d7a65SYafang Shao 				if (nr_pagevec)
503eb1d7a65SYafang Shao 					(*nr_pagevec)++;
504eb1d7a65SYafang Shao 			}
50531560180SMinchan Kim 			count += ret;
5061da177e4SLinus Torvalds 		}
5070cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
5081da177e4SLinus Torvalds 		pagevec_release(&pvec);
509fc9a07e7SAndrew Morton 		cond_resched();
510b85e0effSHugh Dickins 		index++;
5111da177e4SLinus Torvalds 	}
51231560180SMinchan Kim 	return count;
5131da177e4SLinus Torvalds }
514eb1d7a65SYafang Shao 
515eb1d7a65SYafang Shao /**
516*7ae12c80SJohannes Weiner  * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode
517*7ae12c80SJohannes Weiner  * @mapping: the address_space which holds the cache to invalidate
518eb1d7a65SYafang Shao  * @start: the offset 'from' which to invalidate
519eb1d7a65SYafang Shao  * @end: the offset 'to' which to invalidate (inclusive)
520eb1d7a65SYafang Shao  *
521*7ae12c80SJohannes Weiner  * This function removes pages that are clean, unmapped and unlocked,
522*7ae12c80SJohannes Weiner  * as well as shadow entries. It will not block on IO activity.
523eb1d7a65SYafang Shao  *
524*7ae12c80SJohannes Weiner  * If you want to remove all the pages of one inode, regardless of
525*7ae12c80SJohannes Weiner  * their use and writeback state, use truncate_inode_pages().
526eb1d7a65SYafang Shao  *
527*7ae12c80SJohannes Weiner  * Return: the number of the cache entries that were invalidated
528eb1d7a65SYafang Shao  */
529eb1d7a65SYafang Shao unsigned long invalidate_mapping_pages(struct address_space *mapping,
530eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end)
531eb1d7a65SYafang Shao {
532eb1d7a65SYafang Shao 	return __invalidate_mapping_pages(mapping, start, end, NULL);
533eb1d7a65SYafang Shao }
53454bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
5351da177e4SLinus Torvalds 
536eb1d7a65SYafang Shao /**
537649c6dfeSAlex Shi  * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode
538649c6dfeSAlex Shi  * @mapping: the address_space which holds the pages to invalidate
539649c6dfeSAlex Shi  * @start: the offset 'from' which to invalidate
540649c6dfeSAlex Shi  * @end: the offset 'to' which to invalidate (inclusive)
541649c6dfeSAlex Shi  * @nr_pagevec: invalidate failed page number for caller
542649c6dfeSAlex Shi  *
543a00cda3fSMauro Carvalho Chehab  * This helper is similar to invalidate_mapping_pages(), except that it accounts
544a00cda3fSMauro Carvalho Chehab  * for pages that are likely on a pagevec and counts them in @nr_pagevec, which
545a00cda3fSMauro Carvalho Chehab  * will be used by the caller.
546eb1d7a65SYafang Shao  */
547eb1d7a65SYafang Shao void invalidate_mapping_pagevec(struct address_space *mapping,
548eb1d7a65SYafang Shao 		pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
549eb1d7a65SYafang Shao {
550eb1d7a65SYafang Shao 	__invalidate_mapping_pages(mapping, start, end, nr_pagevec);
551eb1d7a65SYafang Shao }
552eb1d7a65SYafang Shao 
553bd4c8ce4SAndrew Morton /*
554bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
555bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
556bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
5572706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
5582706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
559bd4c8ce4SAndrew Morton  */
560bd4c8ce4SAndrew Morton static int
561bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
562bd4c8ce4SAndrew Morton {
563bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
564bd4c8ce4SAndrew Morton 		return 0;
565bd4c8ce4SAndrew Morton 
566266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
567bd4c8ce4SAndrew Morton 		return 0;
568bd4c8ce4SAndrew Morton 
56930472509SJohannes Weiner 	xa_lock_irq(&mapping->i_pages);
570bd4c8ce4SAndrew Morton 	if (PageDirty(page))
571bd4c8ce4SAndrew Morton 		goto failed;
572bd4c8ce4SAndrew Morton 
573266cf658SDavid Howells 	BUG_ON(page_has_private(page));
57462cccb8cSJohannes Weiner 	__delete_from_page_cache(page, NULL);
57530472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
5766072d13cSLinus Torvalds 
5776072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
5786072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
5796072d13cSLinus Torvalds 
58009cbfeafSKirill A. Shutemov 	put_page(page);	/* pagecache ref */
581bd4c8ce4SAndrew Morton 	return 1;
582bd4c8ce4SAndrew Morton failed:
58330472509SJohannes Weiner 	xa_unlock_irq(&mapping->i_pages);
584bd4c8ce4SAndrew Morton 	return 0;
585bd4c8ce4SAndrew Morton }
586bd4c8ce4SAndrew Morton 
587e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
588e3db7691STrond Myklebust {
589e3db7691STrond Myklebust 	if (!PageDirty(page))
590e3db7691STrond Myklebust 		return 0;
591e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
592e3db7691STrond Myklebust 		return 0;
593e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
594e3db7691STrond Myklebust }
595e3db7691STrond Myklebust 
5961da177e4SLinus Torvalds /**
5971da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
59867be2dd1SMartin Waitz  * @mapping: the address_space
5991da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
6001da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
6011da177e4SLinus Torvalds  *
6021da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6031da177e4SLinus Torvalds  * invalidation.
6041da177e4SLinus Torvalds  *
605a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
6061da177e4SLinus Torvalds  */
6071da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
6081da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
6091da177e4SLinus Torvalds {
6100cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
6111da177e4SLinus Torvalds 	struct pagevec pvec;
612b85e0effSHugh Dickins 	pgoff_t index;
6131da177e4SLinus Torvalds 	int i;
6141da177e4SLinus Torvalds 	int ret = 0;
6150dd1334fSHisashi Hifumi 	int ret2 = 0;
6161da177e4SLinus Torvalds 	int did_range_unmap = 0;
6171da177e4SLinus Torvalds 
6187716506aSMatthew Wilcox (Oracle) 	if (mapping_empty(mapping))
61934ccb69eSAndrey Ryabinin 		goto out;
62032691f0fSAndrey Ryabinin 
62186679820SMel Gorman 	pagevec_init(&pvec);
622b85e0effSHugh Dickins 	index = start;
623a656a202SMatthew Wilcox (Oracle) 	while (find_get_entries(mapping, index, end, &pvec, indices)) {
6247b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
6251da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
626b85e0effSHugh Dickins 
627b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
6280cd6144aSJohannes Weiner 			index = indices[i];
6291da177e4SLinus Torvalds 
6303159f943SMatthew Wilcox 			if (xa_is_value(page)) {
631c6dcf52cSJan Kara 				if (!invalidate_exceptional_entry2(mapping,
632c6dcf52cSJan Kara 								   index, page))
633c6dcf52cSJan Kara 					ret = -EBUSY;
6340cd6144aSJohannes Weiner 				continue;
6350cd6144aSJohannes Weiner 			}
6360cd6144aSJohannes Weiner 
63722061a1fSHugh Dickins 			if (!did_range_unmap && page_mapped(page)) {
63822061a1fSHugh Dickins 				/*
63922061a1fSHugh Dickins 				 * If page is mapped, before taking its lock,
64022061a1fSHugh Dickins 				 * zap the rest of the file in one hit.
64122061a1fSHugh Dickins 				 */
64222061a1fSHugh Dickins 				unmap_mapping_pages(mapping, index,
64322061a1fSHugh Dickins 						(1 + end - index), false);
64422061a1fSHugh Dickins 				did_range_unmap = 1;
64522061a1fSHugh Dickins 			}
64622061a1fSHugh Dickins 
6471da177e4SLinus Torvalds 			lock_page(page);
6485cbc198aSKirill A. Shutemov 			WARN_ON(page_to_index(page) != index);
6491da177e4SLinus Torvalds 			if (page->mapping != mapping) {
6501da177e4SLinus Torvalds 				unlock_page(page);
6511da177e4SLinus Torvalds 				continue;
6521da177e4SLinus Torvalds 			}
6531da177e4SLinus Torvalds 			wait_on_page_writeback(page);
65422061a1fSHugh Dickins 
65522061a1fSHugh Dickins 			if (page_mapped(page))
65622061a1fSHugh Dickins 				unmap_mapping_page(page);
657d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
65822061a1fSHugh Dickins 
6590dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
6600dd1334fSHisashi Hifumi 			if (ret2 == 0) {
6610dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
6626ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
6630dd1334fSHisashi Hifumi 			}
6640dd1334fSHisashi Hifumi 			if (ret2 < 0)
6650dd1334fSHisashi Hifumi 				ret = ret2;
6661da177e4SLinus Torvalds 			unlock_page(page);
6671da177e4SLinus Torvalds 		}
6680cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6691da177e4SLinus Torvalds 		pagevec_release(&pvec);
6701da177e4SLinus Torvalds 		cond_resched();
671b85e0effSHugh Dickins 		index++;
6721da177e4SLinus Torvalds 	}
673cd656375SJan Kara 	/*
67469b6c131SMatthew Wilcox 	 * For DAX we invalidate page tables after invalidating page cache.  We
675cd656375SJan Kara 	 * could invalidate page tables while invalidating each entry however
676cd656375SJan Kara 	 * that would be expensive. And doing range unmapping before doesn't
67769b6c131SMatthew Wilcox 	 * work as we have no cheap way to find whether page cache entry didn't
678cd656375SJan Kara 	 * get remapped later.
679cd656375SJan Kara 	 */
680cd656375SJan Kara 	if (dax_mapping(mapping)) {
681977fbdcdSMatthew Wilcox 		unmap_mapping_pages(mapping, start, end - start + 1, false);
682cd656375SJan Kara 	}
68334ccb69eSAndrey Ryabinin out:
6843167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
6851da177e4SLinus Torvalds 	return ret;
6861da177e4SLinus Torvalds }
6871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
6881da177e4SLinus Torvalds 
6891da177e4SLinus Torvalds /**
6901da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
69167be2dd1SMartin Waitz  * @mapping: the address_space
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6941da177e4SLinus Torvalds  * invalidation.
6951da177e4SLinus Torvalds  *
696a862f68aSMike Rapoport  * Return: -EBUSY if any pages could not be invalidated.
6971da177e4SLinus Torvalds  */
6981da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
6991da177e4SLinus Torvalds {
7001da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
7011da177e4SLinus Torvalds }
7021da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
70325d9e2d1Snpiggin@suse.de 
70425d9e2d1Snpiggin@suse.de /**
70525d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
70625d9e2d1Snpiggin@suse.de  * @inode: inode
7078a549beaSHugh Dickins  * @newsize: new file size
70825d9e2d1Snpiggin@suse.de  *
70925d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
71025d9e2d1Snpiggin@suse.de  * is called.
71125d9e2d1Snpiggin@suse.de  *
71225d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
71325d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
71425d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
71525d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
71625d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
71725d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
71825d9e2d1Snpiggin@suse.de  */
7197caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
72025d9e2d1Snpiggin@suse.de {
72125d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
7228a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
72325d9e2d1Snpiggin@suse.de 
72425d9e2d1Snpiggin@suse.de 	/*
72525d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
72625d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
72725d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
72825d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
72925d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
73025d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
73125d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
73225d9e2d1Snpiggin@suse.de 	 */
7338a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
7348a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
7358a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
73625d9e2d1Snpiggin@suse.de }
73725d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
73825d9e2d1Snpiggin@suse.de 
73925d9e2d1Snpiggin@suse.de /**
7402c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
7412c27c65eSChristoph Hellwig  * @inode: inode
7422c27c65eSChristoph Hellwig  * @newsize: new file size
7432c27c65eSChristoph Hellwig  *
744382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
745382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
746382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
7472c27c65eSChristoph Hellwig  *
74877783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
74977783d06SJan Kara  * i_mutex but e.g. xfs uses a different lock) and before all filesystem
75077783d06SJan Kara  * specific block truncation has been performed.
7512c27c65eSChristoph Hellwig  */
7522c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
7532c27c65eSChristoph Hellwig {
75490a80202SJan Kara 	loff_t oldsize = inode->i_size;
75590a80202SJan Kara 
7562c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
75790a80202SJan Kara 	if (newsize > oldsize)
75890a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
7597caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
7602c27c65eSChristoph Hellwig }
7612c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
7622c27c65eSChristoph Hellwig 
7632c27c65eSChristoph Hellwig /**
76490a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
76590a80202SJan Kara  * @inode:	inode for which i_size was extended
76690a80202SJan Kara  * @from:	original inode size
76790a80202SJan Kara  * @to:		new inode size
76890a80202SJan Kara  *
76990a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
77090a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
77190a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
77290a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
77390a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
77490a80202SJan Kara  * changed.
77590a80202SJan Kara  *
77690a80202SJan Kara  * The function must be called after i_size is updated so that page fault
77790a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
77890a80202SJan Kara  * The function must be called while we still hold i_mutex - this not only
77990a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
78090a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
78190a80202SJan Kara  */
78290a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
78390a80202SJan Kara {
78493407472SFabian Frederick 	int bsize = i_blocksize(inode);
78590a80202SJan Kara 	loff_t rounded_from;
78690a80202SJan Kara 	struct page *page;
78790a80202SJan Kara 	pgoff_t index;
78890a80202SJan Kara 
78990a80202SJan Kara 	WARN_ON(to > inode->i_size);
79090a80202SJan Kara 
79109cbfeafSKirill A. Shutemov 	if (from >= to || bsize == PAGE_SIZE)
79290a80202SJan Kara 		return;
79390a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
79490a80202SJan Kara 	rounded_from = round_up(from, bsize);
79509cbfeafSKirill A. Shutemov 	if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
79690a80202SJan Kara 		return;
79790a80202SJan Kara 
79809cbfeafSKirill A. Shutemov 	index = from >> PAGE_SHIFT;
79990a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
80090a80202SJan Kara 	/* Page not cached? Nothing to do */
80190a80202SJan Kara 	if (!page)
80290a80202SJan Kara 		return;
80390a80202SJan Kara 	/*
80490a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
80590a80202SJan Kara 	 * is needed.
80690a80202SJan Kara 	 */
80790a80202SJan Kara 	if (page_mkclean(page))
80890a80202SJan Kara 		set_page_dirty(page);
80990a80202SJan Kara 	unlock_page(page);
81009cbfeafSKirill A. Shutemov 	put_page(page);
81190a80202SJan Kara }
81290a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
81390a80202SJan Kara 
81490a80202SJan Kara /**
815623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
816623e3db9SHugh Dickins  * @inode: inode
817623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
818623e3db9SHugh Dickins  * @lend: offset of last byte of hole
819623e3db9SHugh Dickins  *
820623e3db9SHugh Dickins  * This function should typically be called before the filesystem
821623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
822623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
823623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
824623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
825623e3db9SHugh Dickins  * had its underlying blocks deallocated.
826623e3db9SHugh Dickins  */
827623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
828623e3db9SHugh Dickins {
829623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
830623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
831623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
832623e3db9SHugh Dickins 	/*
833623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
834623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
835623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
8365a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
8375a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
838623e3db9SHugh Dickins 	 */
839623e3db9SHugh Dickins 
840623e3db9SHugh Dickins 	/*
841623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
842623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
843623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
844623e3db9SHugh Dickins 	 */
845623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
846623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
847623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
848623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
849623e3db9SHugh Dickins }
850623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
851