xref: /linux/mm/truncate.c (revision f9fe48bece3af2d60e1bad65db4825f5a025dd36)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * mm/truncate.c - code for taking down pages from address_spaces
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Copyright (C) 2002, Linus Torvalds
51da177e4SLinus Torvalds  *
6e1f8e874SFrancois Cami  * 10Sep2002	Andrew Morton
71da177e4SLinus Torvalds  *		Initial version.
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <linux/kernel.h>
114af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
12*f9fe48beSRoss Zwisler #include <linux/dax.h>
135a0e3ad6STejun Heo #include <linux/gfp.h>
141da177e4SLinus Torvalds #include <linux/mm.h>
150fd0e6b0SNick Piggin #include <linux/swap.h>
16b95f1b31SPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/pagemap.h>
1801f2705dSNate Diller #include <linux/highmem.h>
191da177e4SLinus Torvalds #include <linux/pagevec.h>
20e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h>
211da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* grr. try_to_release_page,
22aaa4059bSJan Kara 				   do_invalidatepage */
23c515e1fdSDan Magenheimer #include <linux/cleancache.h>
2490a80202SJan Kara #include <linux/rmap.h>
25ba470de4SRik van Riel #include "internal.h"
261da177e4SLinus Torvalds 
270cd6144aSJohannes Weiner static void clear_exceptional_entry(struct address_space *mapping,
280cd6144aSJohannes Weiner 				    pgoff_t index, void *entry)
290cd6144aSJohannes Weiner {
30449dd698SJohannes Weiner 	struct radix_tree_node *node;
31449dd698SJohannes Weiner 	void **slot;
32449dd698SJohannes Weiner 
330cd6144aSJohannes Weiner 	/* Handled by shmem itself */
340cd6144aSJohannes Weiner 	if (shmem_mapping(mapping))
350cd6144aSJohannes Weiner 		return;
360cd6144aSJohannes Weiner 
370cd6144aSJohannes Weiner 	spin_lock_irq(&mapping->tree_lock);
38*f9fe48beSRoss Zwisler 
39*f9fe48beSRoss Zwisler 	if (dax_mapping(mapping)) {
40*f9fe48beSRoss Zwisler 		if (radix_tree_delete_item(&mapping->page_tree, index, entry))
41*f9fe48beSRoss Zwisler 			mapping->nrexceptional--;
42*f9fe48beSRoss Zwisler 	} else {
430cd6144aSJohannes Weiner 		/*
440cd6144aSJohannes Weiner 		 * Regular page slots are stabilized by the page lock even
450cd6144aSJohannes Weiner 		 * without the tree itself locked.  These unlocked entries
460cd6144aSJohannes Weiner 		 * need verification under the tree lock.
470cd6144aSJohannes Weiner 		 */
48*f9fe48beSRoss Zwisler 		if (!__radix_tree_lookup(&mapping->page_tree, index, &node,
49*f9fe48beSRoss Zwisler 					&slot))
50449dd698SJohannes Weiner 			goto unlock;
51449dd698SJohannes Weiner 		if (*slot != entry)
52449dd698SJohannes Weiner 			goto unlock;
53449dd698SJohannes Weiner 		radix_tree_replace_slot(slot, NULL);
54*f9fe48beSRoss Zwisler 		mapping->nrexceptional--;
55449dd698SJohannes Weiner 		if (!node)
56449dd698SJohannes Weiner 			goto unlock;
57449dd698SJohannes Weiner 		workingset_node_shadows_dec(node);
58449dd698SJohannes Weiner 		/*
59449dd698SJohannes Weiner 		 * Don't track node without shadow entries.
60449dd698SJohannes Weiner 		 *
61449dd698SJohannes Weiner 		 * Avoid acquiring the list_lru lock if already untracked.
62449dd698SJohannes Weiner 		 * The list_empty() test is safe as node->private_list is
63449dd698SJohannes Weiner 		 * protected by mapping->tree_lock.
64449dd698SJohannes Weiner 		 */
65449dd698SJohannes Weiner 		if (!workingset_node_shadows(node) &&
66449dd698SJohannes Weiner 		    !list_empty(&node->private_list))
67*f9fe48beSRoss Zwisler 			list_lru_del(&workingset_shadow_nodes,
68*f9fe48beSRoss Zwisler 					&node->private_list);
69449dd698SJohannes Weiner 		__radix_tree_delete_node(&mapping->page_tree, node);
70*f9fe48beSRoss Zwisler 	}
71449dd698SJohannes Weiner unlock:
720cd6144aSJohannes Weiner 	spin_unlock_irq(&mapping->tree_lock);
730cd6144aSJohannes Weiner }
741da177e4SLinus Torvalds 
75cf9a2ae8SDavid Howells /**
7628bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
77cf9a2ae8SDavid Howells  * @page: the page which is affected
78d47992f8SLukas Czerner  * @offset: start of the range to invalidate
79d47992f8SLukas Czerner  * @length: length of the range to invalidate
80cf9a2ae8SDavid Howells  *
81cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
82cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
83cf9a2ae8SDavid Howells  *
84cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
85cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
86cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
87cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
88cf9a2ae8SDavid Howells  * blocks on-disk.
89cf9a2ae8SDavid Howells  */
90d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
91d47992f8SLukas Czerner 		       unsigned int length)
92cf9a2ae8SDavid Howells {
93d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
94d47992f8SLukas Czerner 
95cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
969361401eSDavid Howells #ifdef CONFIG_BLOCK
97cf9a2ae8SDavid Howells 	if (!invalidatepage)
98cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
999361401eSDavid Howells #endif
100cf9a2ae8SDavid Howells 	if (invalidatepage)
101d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
102cf9a2ae8SDavid Howells }
103cf9a2ae8SDavid Howells 
104ecdfc978SLinus Torvalds /*
1051da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
10662e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
10754cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1081da177e4SLinus Torvalds  *
1091da177e4SLinus Torvalds  * We need to bale out if page->mapping is no longer equal to the original
1101da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
111fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1121da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1131da177e4SLinus Torvalds  */
114750b4987SNick Piggin static int
1151da177e4SLinus Torvalds truncate_complete_page(struct address_space *mapping, struct page *page)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	if (page->mapping != mapping)
118750b4987SNick Piggin 		return -EIO;
1191da177e4SLinus Torvalds 
120266cf658SDavid Howells 	if (page_has_private(page))
121d47992f8SLukas Czerner 		do_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1221da177e4SLinus Torvalds 
123b9ea2515SKonstantin Khlebnikov 	/*
124b9ea2515SKonstantin Khlebnikov 	 * Some filesystems seem to re-dirty the page even after
125b9ea2515SKonstantin Khlebnikov 	 * the VM has canceled the dirty bit (eg ext3 journaling).
126b9ea2515SKonstantin Khlebnikov 	 * Hence dirty accounting check is placed after invalidation.
127b9ea2515SKonstantin Khlebnikov 	 */
12811f81becSTejun Heo 	cancel_dirty_page(page);
1291da177e4SLinus Torvalds 	ClearPageMappedToDisk(page);
1305adc7b51SMinchan Kim 	delete_from_page_cache(page);
131750b4987SNick Piggin 	return 0;
1321da177e4SLinus Torvalds }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds /*
135fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
1361da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
1370fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
1380fd0e6b0SNick Piggin  * discards clean, unused pages.
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
1411da177e4SLinus Torvalds  */
1421da177e4SLinus Torvalds static int
1431da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
1441da177e4SLinus Torvalds {
1450fd0e6b0SNick Piggin 	int ret;
1460fd0e6b0SNick Piggin 
1471da177e4SLinus Torvalds 	if (page->mapping != mapping)
1481da177e4SLinus Torvalds 		return 0;
1491da177e4SLinus Torvalds 
150266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
1511da177e4SLinus Torvalds 		return 0;
1521da177e4SLinus Torvalds 
1530fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
1540fd0e6b0SNick Piggin 
1550fd0e6b0SNick Piggin 	return ret;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
158750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page)
159750b4987SNick Piggin {
160750b4987SNick Piggin 	if (page_mapped(page)) {
161750b4987SNick Piggin 		unmap_mapping_range(mapping,
162750b4987SNick Piggin 				   (loff_t)page->index << PAGE_CACHE_SHIFT,
163750b4987SNick Piggin 				   PAGE_CACHE_SIZE, 0);
164750b4987SNick Piggin 	}
165750b4987SNick Piggin 	return truncate_complete_page(mapping, page);
166750b4987SNick Piggin }
167750b4987SNick Piggin 
16883f78668SWu Fengguang /*
16925718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
17025718736SAndi Kleen  */
17125718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
17225718736SAndi Kleen {
17325718736SAndi Kleen 	if (!mapping)
17425718736SAndi Kleen 		return -EINVAL;
17525718736SAndi Kleen 	/*
17625718736SAndi Kleen 	 * Only punch for normal data pages for now.
17725718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
17825718736SAndi Kleen 	 */
17925718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
18025718736SAndi Kleen 		return -EIO;
18125718736SAndi Kleen 	return truncate_inode_page(mapping, page);
18225718736SAndi Kleen }
18325718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
18425718736SAndi Kleen 
18525718736SAndi Kleen /*
18683f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
18783f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
18883f78668SWu Fengguang  *
18983f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
19083f78668SWu Fengguang  */
19183f78668SWu Fengguang int invalidate_inode_page(struct page *page)
19283f78668SWu Fengguang {
19383f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
19483f78668SWu Fengguang 	if (!mapping)
19583f78668SWu Fengguang 		return 0;
19683f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
19783f78668SWu Fengguang 		return 0;
19883f78668SWu Fengguang 	if (page_mapped(page))
19983f78668SWu Fengguang 		return 0;
20083f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
20183f78668SWu Fengguang }
20283f78668SWu Fengguang 
2031da177e4SLinus Torvalds /**
20473c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2051da177e4SLinus Torvalds  * @mapping: mapping to truncate
2061da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2075a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2081da177e4SLinus Torvalds  *
209d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2105a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2115a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2121da177e4SLinus Torvalds  *
2131da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2141da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2151da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2161da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2171da177e4SLinus Torvalds  * is low.
2181da177e4SLinus Torvalds  *
2191da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2201da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2211da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2225a720394SLukas Czerner  *
2235a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2245a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2255a720394SLukas Czerner  * page aligned properly.
2261da177e4SLinus Torvalds  */
227d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
228d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2291da177e4SLinus Torvalds {
2305a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2315a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2325a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2335a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
2341da177e4SLinus Torvalds 	struct pagevec	pvec;
2350cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
236b85e0effSHugh Dickins 	pgoff_t		index;
2371da177e4SLinus Torvalds 	int		i;
2381da177e4SLinus Torvalds 
2393167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
240*f9fe48beSRoss Zwisler 	if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
2411da177e4SLinus Torvalds 		return;
2421da177e4SLinus Torvalds 
2435a720394SLukas Czerner 	/* Offsets within partial pages */
2445a720394SLukas Czerner 	partial_start = lstart & (PAGE_CACHE_SIZE - 1);
2455a720394SLukas Czerner 	partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
2465a720394SLukas Czerner 
2475a720394SLukas Czerner 	/*
2485a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
2495a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
2505a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
2515a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
2525a720394SLukas Czerner 	 */
2535a720394SLukas Czerner 	start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2545a720394SLukas Czerner 	if (lend == -1)
2555a720394SLukas Czerner 		/*
2565a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
2575a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
2585a720394SLukas Czerner 		 * unsigned we're using -1.
2595a720394SLukas Czerner 		 */
2605a720394SLukas Czerner 		end = -1;
2615a720394SLukas Czerner 	else
2625a720394SLukas Czerner 		end = (lend + 1) >> PAGE_CACHE_SHIFT;
263d7339071SHans Reiser 
2641da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
265b85e0effSHugh Dickins 	index = start;
2660cd6144aSJohannes Weiner 	while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
2670cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
2680cd6144aSJohannes Weiner 			indices)) {
2691da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
2701da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
2711da177e4SLinus Torvalds 
272b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
2730cd6144aSJohannes Weiner 			index = indices[i];
2745a720394SLukas Czerner 			if (index >= end)
275d7339071SHans Reiser 				break;
276d7339071SHans Reiser 
2770cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
2780cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
2790cd6144aSJohannes Weiner 				continue;
2800cd6144aSJohannes Weiner 			}
2810cd6144aSJohannes Weiner 
282529ae9aaSNick Piggin 			if (!trylock_page(page))
2831da177e4SLinus Torvalds 				continue;
284b85e0effSHugh Dickins 			WARN_ON(page->index != index);
2851da177e4SLinus Torvalds 			if (PageWriteback(page)) {
2861da177e4SLinus Torvalds 				unlock_page(page);
2871da177e4SLinus Torvalds 				continue;
2881da177e4SLinus Torvalds 			}
289750b4987SNick Piggin 			truncate_inode_page(mapping, page);
2901da177e4SLinus Torvalds 			unlock_page(page);
2911da177e4SLinus Torvalds 		}
2920cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
2931da177e4SLinus Torvalds 		pagevec_release(&pvec);
2941da177e4SLinus Torvalds 		cond_resched();
295b85e0effSHugh Dickins 		index++;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds 
2985a720394SLukas Czerner 	if (partial_start) {
2991da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3001da177e4SLinus Torvalds 		if (page) {
3015a720394SLukas Czerner 			unsigned int top = PAGE_CACHE_SIZE;
3025a720394SLukas Czerner 			if (start > end) {
3035a720394SLukas Czerner 				/* Truncation within a single page */
3045a720394SLukas Czerner 				top = partial_end;
3055a720394SLukas Czerner 				partial_end = 0;
3065a720394SLukas Czerner 			}
3071da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3085a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3095a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3105a720394SLukas Czerner 			if (page_has_private(page))
3115a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3125a720394SLukas Czerner 						  top - partial_start);
3131da177e4SLinus Torvalds 			unlock_page(page);
3141da177e4SLinus Torvalds 			page_cache_release(page);
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 	}
3175a720394SLukas Czerner 	if (partial_end) {
3185a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3195a720394SLukas Czerner 		if (page) {
3205a720394SLukas Czerner 			wait_on_page_writeback(page);
3215a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
3225a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3235a720394SLukas Czerner 			if (page_has_private(page))
3245a720394SLukas Czerner 				do_invalidatepage(page, 0,
3255a720394SLukas Czerner 						  partial_end);
3265a720394SLukas Czerner 			unlock_page(page);
3275a720394SLukas Czerner 			page_cache_release(page);
3285a720394SLukas Czerner 		}
3295a720394SLukas Czerner 	}
3305a720394SLukas Czerner 	/*
3315a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
3325a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
3335a720394SLukas Czerner 	 */
3345a720394SLukas Czerner 	if (start >= end)
3355a720394SLukas Czerner 		return;
3361da177e4SLinus Torvalds 
337b85e0effSHugh Dickins 	index = start;
3381da177e4SLinus Torvalds 	for ( ; ; ) {
3391da177e4SLinus Torvalds 		cond_resched();
3400cd6144aSJohannes Weiner 		if (!pagevec_lookup_entries(&pvec, mapping, index,
341792ceaefSHugh Dickins 			min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
342792ceaefSHugh Dickins 			/* If all gone from start onwards, we're done */
343b85e0effSHugh Dickins 			if (index == start)
3441da177e4SLinus Torvalds 				break;
345792ceaefSHugh Dickins 			/* Otherwise restart to make sure all gone */
346b85e0effSHugh Dickins 			index = start;
3471da177e4SLinus Torvalds 			continue;
3481da177e4SLinus Torvalds 		}
3490cd6144aSJohannes Weiner 		if (index == start && indices[0] >= end) {
350792ceaefSHugh Dickins 			/* All gone out of hole to be punched, we're done */
3510cd6144aSJohannes Weiner 			pagevec_remove_exceptionals(&pvec);
352d7339071SHans Reiser 			pagevec_release(&pvec);
353d7339071SHans Reiser 			break;
354d7339071SHans Reiser 		}
3551da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3561da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
3571da177e4SLinus Torvalds 
358b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
3590cd6144aSJohannes Weiner 			index = indices[i];
360792ceaefSHugh Dickins 			if (index >= end) {
361792ceaefSHugh Dickins 				/* Restart punch to make sure all gone */
362792ceaefSHugh Dickins 				index = start - 1;
363d7339071SHans Reiser 				break;
364792ceaefSHugh Dickins 			}
365b85e0effSHugh Dickins 
3660cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
3670cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
3680cd6144aSJohannes Weiner 				continue;
3690cd6144aSJohannes Weiner 			}
3700cd6144aSJohannes Weiner 
3711da177e4SLinus Torvalds 			lock_page(page);
372b85e0effSHugh Dickins 			WARN_ON(page->index != index);
3731da177e4SLinus Torvalds 			wait_on_page_writeback(page);
374750b4987SNick Piggin 			truncate_inode_page(mapping, page);
3751da177e4SLinus Torvalds 			unlock_page(page);
3761da177e4SLinus Torvalds 		}
3770cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
3781da177e4SLinus Torvalds 		pagevec_release(&pvec);
379b85e0effSHugh Dickins 		index++;
3801da177e4SLinus Torvalds 	}
3813167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
3821da177e4SLinus Torvalds }
383d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
3841da177e4SLinus Torvalds 
385d7339071SHans Reiser /**
386d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
387d7339071SHans Reiser  * @mapping: mapping to truncate
388d7339071SHans Reiser  * @lstart: offset from which to truncate
389d7339071SHans Reiser  *
3901b1dcc1bSJes Sorensen  * Called under (and serialised by) inode->i_mutex.
39108142579SJan Kara  *
39208142579SJan Kara  * Note: When this function returns, there can be a page in the process of
39308142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
39408142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
39508142579SJan Kara  * truncation of the whole mapping.
396d7339071SHans Reiser  */
397d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
398d7339071SHans Reiser {
399d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
400d7339071SHans Reiser }
4011da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4021da177e4SLinus Torvalds 
40328697355SMike Waychison /**
40491b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
40591b0abe3SJohannes Weiner  * @mapping: mapping to truncate
40691b0abe3SJohannes Weiner  *
40791b0abe3SJohannes Weiner  * Called under (and serialized by) inode->i_mutex.
40891b0abe3SJohannes Weiner  *
40991b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
41091b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
41191b0abe3SJohannes Weiner  */
41291b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
41391b0abe3SJohannes Weiner {
414*f9fe48beSRoss Zwisler 	unsigned long nrexceptional;
41591b0abe3SJohannes Weiner 	unsigned long nrpages;
41691b0abe3SJohannes Weiner 
41791b0abe3SJohannes Weiner 	/*
41891b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
41991b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
42091b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
42191b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
42291b0abe3SJohannes Weiner 	 * final truncate has begun.
42391b0abe3SJohannes Weiner 	 */
42491b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
42591b0abe3SJohannes Weiner 
42691b0abe3SJohannes Weiner 	/*
42791b0abe3SJohannes Weiner 	 * When reclaim installs eviction entries, it increases
428*f9fe48beSRoss Zwisler 	 * nrexceptional first, then decreases nrpages.  Make sure we see
42991b0abe3SJohannes Weiner 	 * this in the right order or we might miss an entry.
43091b0abe3SJohannes Weiner 	 */
43191b0abe3SJohannes Weiner 	nrpages = mapping->nrpages;
43291b0abe3SJohannes Weiner 	smp_rmb();
433*f9fe48beSRoss Zwisler 	nrexceptional = mapping->nrexceptional;
43491b0abe3SJohannes Weiner 
435*f9fe48beSRoss Zwisler 	if (nrpages || nrexceptional) {
43691b0abe3SJohannes Weiner 		/*
43791b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
43891b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
43991b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
44091b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
44191b0abe3SJohannes Weiner 		 */
44291b0abe3SJohannes Weiner 		spin_lock_irq(&mapping->tree_lock);
44391b0abe3SJohannes Weiner 		spin_unlock_irq(&mapping->tree_lock);
44491b0abe3SJohannes Weiner 
44591b0abe3SJohannes Weiner 		truncate_inode_pages(mapping, 0);
44691b0abe3SJohannes Weiner 	}
44791b0abe3SJohannes Weiner }
44891b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
44991b0abe3SJohannes Weiner 
45091b0abe3SJohannes Weiner /**
45128697355SMike Waychison  * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
45228697355SMike Waychison  * @mapping: the address_space which holds the pages to invalidate
45328697355SMike Waychison  * @start: the offset 'from' which to invalidate
45428697355SMike Waychison  * @end: the offset 'to' which to invalidate (inclusive)
45528697355SMike Waychison  *
45628697355SMike Waychison  * This function only removes the unlocked pages, if you want to
45728697355SMike Waychison  * remove all the pages of one inode, you must call truncate_inode_pages.
45828697355SMike Waychison  *
45928697355SMike Waychison  * invalidate_mapping_pages() will not block on IO activity. It will not
46028697355SMike Waychison  * invalidate pages which are dirty, locked, under writeback or mapped into
46128697355SMike Waychison  * pagetables.
46228697355SMike Waychison  */
46328697355SMike Waychison unsigned long invalidate_mapping_pages(struct address_space *mapping,
46428697355SMike Waychison 		pgoff_t start, pgoff_t end)
4651da177e4SLinus Torvalds {
4660cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
4671da177e4SLinus Torvalds 	struct pagevec pvec;
468b85e0effSHugh Dickins 	pgoff_t index = start;
46931560180SMinchan Kim 	unsigned long ret;
47031560180SMinchan Kim 	unsigned long count = 0;
4711da177e4SLinus Torvalds 	int i;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
4740cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
4750cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
4760cd6144aSJohannes Weiner 			indices)) {
4771da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
4781da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
4791da177e4SLinus Torvalds 
480b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
4810cd6144aSJohannes Weiner 			index = indices[i];
482b85e0effSHugh Dickins 			if (index > end)
483b85e0effSHugh Dickins 				break;
484e0f23603SNeilBrown 
4850cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
4860cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
4870cd6144aSJohannes Weiner 				continue;
4880cd6144aSJohannes Weiner 			}
4890cd6144aSJohannes Weiner 
490b85e0effSHugh Dickins 			if (!trylock_page(page))
491b85e0effSHugh Dickins 				continue;
492b85e0effSHugh Dickins 			WARN_ON(page->index != index);
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 			 */
49931560180SMinchan Kim 			if (!ret)
500cc5993bdSMinchan Kim 				deactivate_file_page(page);
50131560180SMinchan Kim 			count += ret;
5021da177e4SLinus Torvalds 		}
5030cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
5041da177e4SLinus Torvalds 		pagevec_release(&pvec);
505fc9a07e7SAndrew Morton 		cond_resched();
506b85e0effSHugh Dickins 		index++;
5071da177e4SLinus Torvalds 	}
50831560180SMinchan Kim 	return count;
5091da177e4SLinus Torvalds }
51054bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
5111da177e4SLinus Torvalds 
512bd4c8ce4SAndrew Morton /*
513bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
514bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
515bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
5162706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
5172706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
518bd4c8ce4SAndrew Morton  */
519bd4c8ce4SAndrew Morton static int
520bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
521bd4c8ce4SAndrew Morton {
522c4843a75SGreg Thelen 	struct mem_cgroup *memcg;
523c4843a75SGreg Thelen 	unsigned long flags;
524c4843a75SGreg Thelen 
525bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
526bd4c8ce4SAndrew Morton 		return 0;
527bd4c8ce4SAndrew Morton 
528266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
529bd4c8ce4SAndrew Morton 		return 0;
530bd4c8ce4SAndrew Morton 
531c4843a75SGreg Thelen 	memcg = mem_cgroup_begin_page_stat(page);
532c4843a75SGreg Thelen 	spin_lock_irqsave(&mapping->tree_lock, flags);
533bd4c8ce4SAndrew Morton 	if (PageDirty(page))
534bd4c8ce4SAndrew Morton 		goto failed;
535bd4c8ce4SAndrew Morton 
536266cf658SDavid Howells 	BUG_ON(page_has_private(page));
537c4843a75SGreg Thelen 	__delete_from_page_cache(page, NULL, memcg);
538c4843a75SGreg Thelen 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
539c4843a75SGreg Thelen 	mem_cgroup_end_page_stat(memcg);
5406072d13cSLinus Torvalds 
5416072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
5426072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
5436072d13cSLinus Torvalds 
544bd4c8ce4SAndrew Morton 	page_cache_release(page);	/* pagecache ref */
545bd4c8ce4SAndrew Morton 	return 1;
546bd4c8ce4SAndrew Morton failed:
547c4843a75SGreg Thelen 	spin_unlock_irqrestore(&mapping->tree_lock, flags);
548c4843a75SGreg Thelen 	mem_cgroup_end_page_stat(memcg);
549bd4c8ce4SAndrew Morton 	return 0;
550bd4c8ce4SAndrew Morton }
551bd4c8ce4SAndrew Morton 
552e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
553e3db7691STrond Myklebust {
554e3db7691STrond Myklebust 	if (!PageDirty(page))
555e3db7691STrond Myklebust 		return 0;
556e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
557e3db7691STrond Myklebust 		return 0;
558e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
559e3db7691STrond Myklebust }
560e3db7691STrond Myklebust 
5611da177e4SLinus Torvalds /**
5621da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
56367be2dd1SMartin Waitz  * @mapping: the address_space
5641da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
5651da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
5661da177e4SLinus Torvalds  *
5671da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
5681da177e4SLinus Torvalds  * invalidation.
5691da177e4SLinus Torvalds  *
5706ccfa806SHisashi Hifumi  * Returns -EBUSY if any pages could not be invalidated.
5711da177e4SLinus Torvalds  */
5721da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
5731da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
5741da177e4SLinus Torvalds {
5750cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
5761da177e4SLinus Torvalds 	struct pagevec pvec;
577b85e0effSHugh Dickins 	pgoff_t index;
5781da177e4SLinus Torvalds 	int i;
5791da177e4SLinus Torvalds 	int ret = 0;
5800dd1334fSHisashi Hifumi 	int ret2 = 0;
5811da177e4SLinus Torvalds 	int did_range_unmap = 0;
5821da177e4SLinus Torvalds 
5833167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
5841da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
585b85e0effSHugh Dickins 	index = start;
5860cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
5870cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
5880cd6144aSJohannes Weiner 			indices)) {
5897b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
5901da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
591b85e0effSHugh Dickins 
592b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
5930cd6144aSJohannes Weiner 			index = indices[i];
594b85e0effSHugh Dickins 			if (index > end)
595b85e0effSHugh Dickins 				break;
5961da177e4SLinus Torvalds 
5970cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
5980cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
5990cd6144aSJohannes Weiner 				continue;
6000cd6144aSJohannes Weiner 			}
6010cd6144aSJohannes Weiner 
6021da177e4SLinus Torvalds 			lock_page(page);
603b85e0effSHugh Dickins 			WARN_ON(page->index != index);
6041da177e4SLinus Torvalds 			if (page->mapping != mapping) {
6051da177e4SLinus Torvalds 				unlock_page(page);
6061da177e4SLinus Torvalds 				continue;
6071da177e4SLinus Torvalds 			}
6081da177e4SLinus Torvalds 			wait_on_page_writeback(page);
609d00806b1SNick Piggin 			if (page_mapped(page)) {
6101da177e4SLinus Torvalds 				if (!did_range_unmap) {
6111da177e4SLinus Torvalds 					/*
6121da177e4SLinus Torvalds 					 * Zap the rest of the file in one hit.
6131da177e4SLinus Torvalds 					 */
6141da177e4SLinus Torvalds 					unmap_mapping_range(mapping,
615b85e0effSHugh Dickins 					   (loff_t)index << PAGE_CACHE_SHIFT,
616b85e0effSHugh Dickins 					   (loff_t)(1 + end - index)
6171da177e4SLinus Torvalds 							 << PAGE_CACHE_SHIFT,
6181da177e4SLinus Torvalds 					    0);
6191da177e4SLinus Torvalds 					did_range_unmap = 1;
6201da177e4SLinus Torvalds 				} else {
6211da177e4SLinus Torvalds 					/*
6221da177e4SLinus Torvalds 					 * Just zap this page
6231da177e4SLinus Torvalds 					 */
6241da177e4SLinus Torvalds 					unmap_mapping_range(mapping,
625b85e0effSHugh Dickins 					   (loff_t)index << PAGE_CACHE_SHIFT,
6261da177e4SLinus Torvalds 					   PAGE_CACHE_SIZE, 0);
6271da177e4SLinus Torvalds 				}
6281da177e4SLinus Torvalds 			}
629d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
6300dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
6310dd1334fSHisashi Hifumi 			if (ret2 == 0) {
6320dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
6336ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
6340dd1334fSHisashi Hifumi 			}
6350dd1334fSHisashi Hifumi 			if (ret2 < 0)
6360dd1334fSHisashi Hifumi 				ret = ret2;
6371da177e4SLinus Torvalds 			unlock_page(page);
6381da177e4SLinus Torvalds 		}
6390cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6401da177e4SLinus Torvalds 		pagevec_release(&pvec);
6411da177e4SLinus Torvalds 		cond_resched();
642b85e0effSHugh Dickins 		index++;
6431da177e4SLinus Torvalds 	}
6443167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
6451da177e4SLinus Torvalds 	return ret;
6461da177e4SLinus Torvalds }
6471da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds /**
6501da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
65167be2dd1SMartin Waitz  * @mapping: the address_space
6521da177e4SLinus Torvalds  *
6531da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6541da177e4SLinus Torvalds  * invalidation.
6551da177e4SLinus Torvalds  *
656e9de25ddSPeng Tao  * Returns -EBUSY if any pages could not be invalidated.
6571da177e4SLinus Torvalds  */
6581da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
6591da177e4SLinus Torvalds {
6601da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
6611da177e4SLinus Torvalds }
6621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
66325d9e2d1Snpiggin@suse.de 
66425d9e2d1Snpiggin@suse.de /**
66525d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
66625d9e2d1Snpiggin@suse.de  * @inode: inode
6678a549beaSHugh Dickins  * @newsize: new file size
66825d9e2d1Snpiggin@suse.de  *
66925d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
67025d9e2d1Snpiggin@suse.de  * is called.
67125d9e2d1Snpiggin@suse.de  *
67225d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
67325d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
67425d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
67525d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
67625d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
67725d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
67825d9e2d1Snpiggin@suse.de  */
6797caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
68025d9e2d1Snpiggin@suse.de {
68125d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
6828a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
68325d9e2d1Snpiggin@suse.de 
68425d9e2d1Snpiggin@suse.de 	/*
68525d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
68625d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
68725d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
68825d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
68925d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
69025d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
69125d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
69225d9e2d1Snpiggin@suse.de 	 */
6938a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
6948a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
6958a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
69625d9e2d1Snpiggin@suse.de }
69725d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
69825d9e2d1Snpiggin@suse.de 
69925d9e2d1Snpiggin@suse.de /**
7002c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
7012c27c65eSChristoph Hellwig  * @inode: inode
7022c27c65eSChristoph Hellwig  * @newsize: new file size
7032c27c65eSChristoph Hellwig  *
704382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
705382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
706382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
7072c27c65eSChristoph Hellwig  *
70877783d06SJan Kara  * Must be called with a lock serializing truncates and writes (generally
70977783d06SJan Kara  * i_mutex but e.g. xfs uses a different lock) and before all filesystem
71077783d06SJan Kara  * specific block truncation has been performed.
7112c27c65eSChristoph Hellwig  */
7122c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
7132c27c65eSChristoph Hellwig {
71490a80202SJan Kara 	loff_t oldsize = inode->i_size;
71590a80202SJan Kara 
7162c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
71790a80202SJan Kara 	if (newsize > oldsize)
71890a80202SJan Kara 		pagecache_isize_extended(inode, oldsize, newsize);
7197caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
7202c27c65eSChristoph Hellwig }
7212c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
7222c27c65eSChristoph Hellwig 
7232c27c65eSChristoph Hellwig /**
72490a80202SJan Kara  * pagecache_isize_extended - update pagecache after extension of i_size
72590a80202SJan Kara  * @inode:	inode for which i_size was extended
72690a80202SJan Kara  * @from:	original inode size
72790a80202SJan Kara  * @to:		new inode size
72890a80202SJan Kara  *
72990a80202SJan Kara  * Handle extension of inode size either caused by extending truncate or by
73090a80202SJan Kara  * write starting after current i_size. We mark the page straddling current
73190a80202SJan Kara  * i_size RO so that page_mkwrite() is called on the nearest write access to
73290a80202SJan Kara  * the page.  This way filesystem can be sure that page_mkwrite() is called on
73390a80202SJan Kara  * the page before user writes to the page via mmap after the i_size has been
73490a80202SJan Kara  * changed.
73590a80202SJan Kara  *
73690a80202SJan Kara  * The function must be called after i_size is updated so that page fault
73790a80202SJan Kara  * coming after we unlock the page will already see the new i_size.
73890a80202SJan Kara  * The function must be called while we still hold i_mutex - this not only
73990a80202SJan Kara  * makes sure i_size is stable but also that userspace cannot observe new
74090a80202SJan Kara  * i_size value before we are prepared to store mmap writes at new inode size.
74190a80202SJan Kara  */
74290a80202SJan Kara void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
74390a80202SJan Kara {
74490a80202SJan Kara 	int bsize = 1 << inode->i_blkbits;
74590a80202SJan Kara 	loff_t rounded_from;
74690a80202SJan Kara 	struct page *page;
74790a80202SJan Kara 	pgoff_t index;
74890a80202SJan Kara 
74990a80202SJan Kara 	WARN_ON(to > inode->i_size);
75090a80202SJan Kara 
75190a80202SJan Kara 	if (from >= to || bsize == PAGE_CACHE_SIZE)
75290a80202SJan Kara 		return;
75390a80202SJan Kara 	/* Page straddling @from will not have any hole block created? */
75490a80202SJan Kara 	rounded_from = round_up(from, bsize);
75590a80202SJan Kara 	if (to <= rounded_from || !(rounded_from & (PAGE_CACHE_SIZE - 1)))
75690a80202SJan Kara 		return;
75790a80202SJan Kara 
75890a80202SJan Kara 	index = from >> PAGE_CACHE_SHIFT;
75990a80202SJan Kara 	page = find_lock_page(inode->i_mapping, index);
76090a80202SJan Kara 	/* Page not cached? Nothing to do */
76190a80202SJan Kara 	if (!page)
76290a80202SJan Kara 		return;
76390a80202SJan Kara 	/*
76490a80202SJan Kara 	 * See clear_page_dirty_for_io() for details why set_page_dirty()
76590a80202SJan Kara 	 * is needed.
76690a80202SJan Kara 	 */
76790a80202SJan Kara 	if (page_mkclean(page))
76890a80202SJan Kara 		set_page_dirty(page);
76990a80202SJan Kara 	unlock_page(page);
77090a80202SJan Kara 	page_cache_release(page);
77190a80202SJan Kara }
77290a80202SJan Kara EXPORT_SYMBOL(pagecache_isize_extended);
77390a80202SJan Kara 
77490a80202SJan Kara /**
775623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
776623e3db9SHugh Dickins  * @inode: inode
777623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
778623e3db9SHugh Dickins  * @lend: offset of last byte of hole
779623e3db9SHugh Dickins  *
780623e3db9SHugh Dickins  * This function should typically be called before the filesystem
781623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
782623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
783623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
784623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
785623e3db9SHugh Dickins  * had its underlying blocks deallocated.
786623e3db9SHugh Dickins  */
787623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
788623e3db9SHugh Dickins {
789623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
790623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
791623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
792623e3db9SHugh Dickins 	/*
793623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
794623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
795623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
7965a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
7975a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
798623e3db9SHugh Dickins 	 */
799623e3db9SHugh Dickins 
800623e3db9SHugh Dickins 	/*
801623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
802623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
803623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
804623e3db9SHugh Dickins 	 */
805623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
806623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
807623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
808623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
809623e3db9SHugh Dickins }
810623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
811