xref: /linux/mm/truncate.c (revision 449dd6984d0e47643c04c807f609dd56d48d5bcc)
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>
125a0e3ad6STejun Heo #include <linux/gfp.h>
131da177e4SLinus Torvalds #include <linux/mm.h>
140fd0e6b0SNick Piggin #include <linux/swap.h>
15b95f1b31SPaul Gortmaker #include <linux/export.h>
161da177e4SLinus Torvalds #include <linux/pagemap.h>
1701f2705dSNate Diller #include <linux/highmem.h>
181da177e4SLinus Torvalds #include <linux/pagevec.h>
19e08748ceSAndrew Morton #include <linux/task_io_accounting_ops.h>
201da177e4SLinus Torvalds #include <linux/buffer_head.h>	/* grr. try_to_release_page,
21aaa4059bSJan Kara 				   do_invalidatepage */
22c515e1fdSDan Magenheimer #include <linux/cleancache.h>
23ba470de4SRik van Riel #include "internal.h"
241da177e4SLinus Torvalds 
250cd6144aSJohannes Weiner static void clear_exceptional_entry(struct address_space *mapping,
260cd6144aSJohannes Weiner 				    pgoff_t index, void *entry)
270cd6144aSJohannes Weiner {
28*449dd698SJohannes Weiner 	struct radix_tree_node *node;
29*449dd698SJohannes Weiner 	void **slot;
30*449dd698SJohannes Weiner 
310cd6144aSJohannes Weiner 	/* Handled by shmem itself */
320cd6144aSJohannes Weiner 	if (shmem_mapping(mapping))
330cd6144aSJohannes Weiner 		return;
340cd6144aSJohannes Weiner 
350cd6144aSJohannes Weiner 	spin_lock_irq(&mapping->tree_lock);
360cd6144aSJohannes Weiner 	/*
370cd6144aSJohannes Weiner 	 * Regular page slots are stabilized by the page lock even
380cd6144aSJohannes Weiner 	 * without the tree itself locked.  These unlocked entries
390cd6144aSJohannes Weiner 	 * need verification under the tree lock.
400cd6144aSJohannes Weiner 	 */
41*449dd698SJohannes Weiner 	if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
42*449dd698SJohannes Weiner 		goto unlock;
43*449dd698SJohannes Weiner 	if (*slot != entry)
44*449dd698SJohannes Weiner 		goto unlock;
45*449dd698SJohannes Weiner 	radix_tree_replace_slot(slot, NULL);
4691b0abe3SJohannes Weiner 	mapping->nrshadows--;
47*449dd698SJohannes Weiner 	if (!node)
48*449dd698SJohannes Weiner 		goto unlock;
49*449dd698SJohannes Weiner 	workingset_node_shadows_dec(node);
50*449dd698SJohannes Weiner 	/*
51*449dd698SJohannes Weiner 	 * Don't track node without shadow entries.
52*449dd698SJohannes Weiner 	 *
53*449dd698SJohannes Weiner 	 * Avoid acquiring the list_lru lock if already untracked.
54*449dd698SJohannes Weiner 	 * The list_empty() test is safe as node->private_list is
55*449dd698SJohannes Weiner 	 * protected by mapping->tree_lock.
56*449dd698SJohannes Weiner 	 */
57*449dd698SJohannes Weiner 	if (!workingset_node_shadows(node) &&
58*449dd698SJohannes Weiner 	    !list_empty(&node->private_list))
59*449dd698SJohannes Weiner 		list_lru_del(&workingset_shadow_nodes, &node->private_list);
60*449dd698SJohannes Weiner 	__radix_tree_delete_node(&mapping->page_tree, node);
61*449dd698SJohannes Weiner unlock:
620cd6144aSJohannes Weiner 	spin_unlock_irq(&mapping->tree_lock);
630cd6144aSJohannes Weiner }
641da177e4SLinus Torvalds 
65cf9a2ae8SDavid Howells /**
6628bc44d7SFengguang Wu  * do_invalidatepage - invalidate part or all of a page
67cf9a2ae8SDavid Howells  * @page: the page which is affected
68d47992f8SLukas Czerner  * @offset: start of the range to invalidate
69d47992f8SLukas Czerner  * @length: length of the range to invalidate
70cf9a2ae8SDavid Howells  *
71cf9a2ae8SDavid Howells  * do_invalidatepage() is called when all or part of the page has become
72cf9a2ae8SDavid Howells  * invalidated by a truncate operation.
73cf9a2ae8SDavid Howells  *
74cf9a2ae8SDavid Howells  * do_invalidatepage() does not have to release all buffers, but it must
75cf9a2ae8SDavid Howells  * ensure that no dirty buffer is left outside @offset and that no I/O
76cf9a2ae8SDavid Howells  * is underway against any of the blocks which are outside the truncation
77cf9a2ae8SDavid Howells  * point.  Because the caller is about to free (and possibly reuse) those
78cf9a2ae8SDavid Howells  * blocks on-disk.
79cf9a2ae8SDavid Howells  */
80d47992f8SLukas Czerner void do_invalidatepage(struct page *page, unsigned int offset,
81d47992f8SLukas Czerner 		       unsigned int length)
82cf9a2ae8SDavid Howells {
83d47992f8SLukas Czerner 	void (*invalidatepage)(struct page *, unsigned int, unsigned int);
84d47992f8SLukas Czerner 
85cf9a2ae8SDavid Howells 	invalidatepage = page->mapping->a_ops->invalidatepage;
869361401eSDavid Howells #ifdef CONFIG_BLOCK
87cf9a2ae8SDavid Howells 	if (!invalidatepage)
88cf9a2ae8SDavid Howells 		invalidatepage = block_invalidatepage;
899361401eSDavid Howells #endif
90cf9a2ae8SDavid Howells 	if (invalidatepage)
91d47992f8SLukas Czerner 		(*invalidatepage)(page, offset, length);
92cf9a2ae8SDavid Howells }
93cf9a2ae8SDavid Howells 
94ecdfc978SLinus Torvalds /*
95ecdfc978SLinus Torvalds  * This cancels just the dirty bit on the kernel page itself, it
96ecdfc978SLinus Torvalds  * does NOT actually remove dirty bits on any mmap's that may be
97ecdfc978SLinus Torvalds  * around. It also leaves the page tagged dirty, so any sync
98ecdfc978SLinus Torvalds  * activity will still find it on the dirty lists, and in particular,
99ecdfc978SLinus Torvalds  * clear_page_dirty_for_io() will still look at the dirty bits in
100ecdfc978SLinus Torvalds  * the VM.
101ecdfc978SLinus Torvalds  *
102ecdfc978SLinus Torvalds  * Doing this should *normally* only ever be done when a page
103ecdfc978SLinus Torvalds  * is truncated, and is not actually mapped anywhere at all. However,
104ecdfc978SLinus Torvalds  * fs/buffer.c does this when it notices that somebody has cleaned
105ecdfc978SLinus Torvalds  * out all the buffers on a page without actually doing it through
106ecdfc978SLinus Torvalds  * the VM. Can you say "ext3 is horribly ugly"? Tought you could.
107ecdfc978SLinus Torvalds  */
108fba2591bSLinus Torvalds void cancel_dirty_page(struct page *page, unsigned int account_size)
109fba2591bSLinus Torvalds {
1108368e328SLinus Torvalds 	if (TestClearPageDirty(page)) {
1118368e328SLinus Torvalds 		struct address_space *mapping = page->mapping;
1128368e328SLinus Torvalds 		if (mapping && mapping_cap_account_dirty(mapping)) {
1133e67c098SAndrew Morton 			dec_zone_page_state(page, NR_FILE_DIRTY);
114c9e51e41SPeter Zijlstra 			dec_bdi_stat(mapping->backing_dev_info,
115c9e51e41SPeter Zijlstra 					BDI_RECLAIMABLE);
1168368e328SLinus Torvalds 			if (account_size)
117fba2591bSLinus Torvalds 				task_io_account_cancelled_write(account_size);
118fba2591bSLinus Torvalds 		}
1193e67c098SAndrew Morton 	}
1208368e328SLinus Torvalds }
1218368e328SLinus Torvalds EXPORT_SYMBOL(cancel_dirty_page);
122fba2591bSLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * If truncate cannot remove the fs-private metadata from the page, the page
12562e1c553SShaohua Li  * becomes orphaned.  It will be left on the LRU and may even be mapped into
12654cb8821SNick Piggin  * user pagetables if we're racing with filemap_fault().
1271da177e4SLinus Torvalds  *
1281da177e4SLinus Torvalds  * We need to bale out if page->mapping is no longer equal to the original
1291da177e4SLinus Torvalds  * mapping.  This happens a) when the VM reclaimed the page while we waited on
130fc0ecff6SAndrew Morton  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1311da177e4SLinus Torvalds  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
1321da177e4SLinus Torvalds  */
133750b4987SNick Piggin static int
1341da177e4SLinus Torvalds truncate_complete_page(struct address_space *mapping, struct page *page)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	if (page->mapping != mapping)
137750b4987SNick Piggin 		return -EIO;
1381da177e4SLinus Torvalds 
139266cf658SDavid Howells 	if (page_has_private(page))
140d47992f8SLukas Czerner 		do_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1411da177e4SLinus Torvalds 
142a2b34564SBjorn Steinbrink 	cancel_dirty_page(page, PAGE_CACHE_SIZE);
143a2b34564SBjorn Steinbrink 
1441da177e4SLinus Torvalds 	ClearPageMappedToDisk(page);
1455adc7b51SMinchan Kim 	delete_from_page_cache(page);
146750b4987SNick Piggin 	return 0;
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds /*
150fc0ecff6SAndrew Morton  * This is for invalidate_mapping_pages().  That function can be called at
1511da177e4SLinus Torvalds  * any time, and is not supposed to throw away dirty pages.  But pages can
1520fd0e6b0SNick Piggin  * be marked dirty at any time too, so use remove_mapping which safely
1530fd0e6b0SNick Piggin  * discards clean, unused pages.
1541da177e4SLinus Torvalds  *
1551da177e4SLinus Torvalds  * Returns non-zero if the page was successfully invalidated.
1561da177e4SLinus Torvalds  */
1571da177e4SLinus Torvalds static int
1581da177e4SLinus Torvalds invalidate_complete_page(struct address_space *mapping, struct page *page)
1591da177e4SLinus Torvalds {
1600fd0e6b0SNick Piggin 	int ret;
1610fd0e6b0SNick Piggin 
1621da177e4SLinus Torvalds 	if (page->mapping != mapping)
1631da177e4SLinus Torvalds 		return 0;
1641da177e4SLinus Torvalds 
165266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, 0))
1661da177e4SLinus Torvalds 		return 0;
1671da177e4SLinus Torvalds 
1680fd0e6b0SNick Piggin 	ret = remove_mapping(mapping, page);
1690fd0e6b0SNick Piggin 
1700fd0e6b0SNick Piggin 	return ret;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
173750b4987SNick Piggin int truncate_inode_page(struct address_space *mapping, struct page *page)
174750b4987SNick Piggin {
175750b4987SNick Piggin 	if (page_mapped(page)) {
176750b4987SNick Piggin 		unmap_mapping_range(mapping,
177750b4987SNick Piggin 				   (loff_t)page->index << PAGE_CACHE_SHIFT,
178750b4987SNick Piggin 				   PAGE_CACHE_SIZE, 0);
179750b4987SNick Piggin 	}
180750b4987SNick Piggin 	return truncate_complete_page(mapping, page);
181750b4987SNick Piggin }
182750b4987SNick Piggin 
18383f78668SWu Fengguang /*
18425718736SAndi Kleen  * Used to get rid of pages on hardware memory corruption.
18525718736SAndi Kleen  */
18625718736SAndi Kleen int generic_error_remove_page(struct address_space *mapping, struct page *page)
18725718736SAndi Kleen {
18825718736SAndi Kleen 	if (!mapping)
18925718736SAndi Kleen 		return -EINVAL;
19025718736SAndi Kleen 	/*
19125718736SAndi Kleen 	 * Only punch for normal data pages for now.
19225718736SAndi Kleen 	 * Handling other types like directories would need more auditing.
19325718736SAndi Kleen 	 */
19425718736SAndi Kleen 	if (!S_ISREG(mapping->host->i_mode))
19525718736SAndi Kleen 		return -EIO;
19625718736SAndi Kleen 	return truncate_inode_page(mapping, page);
19725718736SAndi Kleen }
19825718736SAndi Kleen EXPORT_SYMBOL(generic_error_remove_page);
19925718736SAndi Kleen 
20025718736SAndi Kleen /*
20183f78668SWu Fengguang  * Safely invalidate one page from its pagecache mapping.
20283f78668SWu Fengguang  * It only drops clean, unused pages. The page must be locked.
20383f78668SWu Fengguang  *
20483f78668SWu Fengguang  * Returns 1 if the page is successfully invalidated, otherwise 0.
20583f78668SWu Fengguang  */
20683f78668SWu Fengguang int invalidate_inode_page(struct page *page)
20783f78668SWu Fengguang {
20883f78668SWu Fengguang 	struct address_space *mapping = page_mapping(page);
20983f78668SWu Fengguang 	if (!mapping)
21083f78668SWu Fengguang 		return 0;
21183f78668SWu Fengguang 	if (PageDirty(page) || PageWriteback(page))
21283f78668SWu Fengguang 		return 0;
21383f78668SWu Fengguang 	if (page_mapped(page))
21483f78668SWu Fengguang 		return 0;
21583f78668SWu Fengguang 	return invalidate_complete_page(mapping, page);
21683f78668SWu Fengguang }
21783f78668SWu Fengguang 
2181da177e4SLinus Torvalds /**
21973c1e204SLiu Bo  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
2201da177e4SLinus Torvalds  * @mapping: mapping to truncate
2211da177e4SLinus Torvalds  * @lstart: offset from which to truncate
2225a720394SLukas Czerner  * @lend: offset to which to truncate (inclusive)
2231da177e4SLinus Torvalds  *
224d7339071SHans Reiser  * Truncate the page cache, removing the pages that are between
2255a720394SLukas Czerner  * specified offsets (and zeroing out partial pages
2265a720394SLukas Czerner  * if lstart or lend + 1 is not page aligned).
2271da177e4SLinus Torvalds  *
2281da177e4SLinus Torvalds  * Truncate takes two passes - the first pass is nonblocking.  It will not
2291da177e4SLinus Torvalds  * block on page locks and it will not block on writeback.  The second pass
2301da177e4SLinus Torvalds  * will wait.  This is to prevent as much IO as possible in the affected region.
2311da177e4SLinus Torvalds  * The first pass will remove most pages, so the search cost of the second pass
2321da177e4SLinus Torvalds  * is low.
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  * We pass down the cache-hot hint to the page freeing code.  Even if the
2351da177e4SLinus Torvalds  * mapping is large, it is probably the case that the final pages are the most
2361da177e4SLinus Torvalds  * recently touched, and freeing happens in ascending file offset order.
2375a720394SLukas Czerner  *
2385a720394SLukas Czerner  * Note that since ->invalidatepage() accepts range to invalidate
2395a720394SLukas Czerner  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
2405a720394SLukas Czerner  * page aligned properly.
2411da177e4SLinus Torvalds  */
242d7339071SHans Reiser void truncate_inode_pages_range(struct address_space *mapping,
243d7339071SHans Reiser 				loff_t lstart, loff_t lend)
2441da177e4SLinus Torvalds {
2455a720394SLukas Czerner 	pgoff_t		start;		/* inclusive */
2465a720394SLukas Czerner 	pgoff_t		end;		/* exclusive */
2475a720394SLukas Czerner 	unsigned int	partial_start;	/* inclusive */
2485a720394SLukas Czerner 	unsigned int	partial_end;	/* exclusive */
2491da177e4SLinus Torvalds 	struct pagevec	pvec;
2500cd6144aSJohannes Weiner 	pgoff_t		indices[PAGEVEC_SIZE];
251b85e0effSHugh Dickins 	pgoff_t		index;
2521da177e4SLinus Torvalds 	int		i;
2531da177e4SLinus Torvalds 
2543167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
25591b0abe3SJohannes Weiner 	if (mapping->nrpages == 0 && mapping->nrshadows == 0)
2561da177e4SLinus Torvalds 		return;
2571da177e4SLinus Torvalds 
2585a720394SLukas Czerner 	/* Offsets within partial pages */
2595a720394SLukas Czerner 	partial_start = lstart & (PAGE_CACHE_SIZE - 1);
2605a720394SLukas Czerner 	partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
2615a720394SLukas Czerner 
2625a720394SLukas Czerner 	/*
2635a720394SLukas Czerner 	 * 'start' and 'end' always covers the range of pages to be fully
2645a720394SLukas Czerner 	 * truncated. Partial pages are covered with 'partial_start' at the
2655a720394SLukas Czerner 	 * start of the range and 'partial_end' at the end of the range.
2665a720394SLukas Czerner 	 * Note that 'end' is exclusive while 'lend' is inclusive.
2675a720394SLukas Czerner 	 */
2685a720394SLukas Czerner 	start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
2695a720394SLukas Czerner 	if (lend == -1)
2705a720394SLukas Czerner 		/*
2715a720394SLukas Czerner 		 * lend == -1 indicates end-of-file so we have to set 'end'
2725a720394SLukas Czerner 		 * to the highest possible pgoff_t and since the type is
2735a720394SLukas Czerner 		 * unsigned we're using -1.
2745a720394SLukas Czerner 		 */
2755a720394SLukas Czerner 		end = -1;
2765a720394SLukas Czerner 	else
2775a720394SLukas Czerner 		end = (lend + 1) >> PAGE_CACHE_SHIFT;
278d7339071SHans Reiser 
2791da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
280b85e0effSHugh Dickins 	index = start;
2810cd6144aSJohannes Weiner 	while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
2820cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
2830cd6144aSJohannes Weiner 			indices)) {
284e5598f8bSHugh Dickins 		mem_cgroup_uncharge_start();
2851da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
2861da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
2871da177e4SLinus Torvalds 
288b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
2890cd6144aSJohannes Weiner 			index = indices[i];
2905a720394SLukas Czerner 			if (index >= end)
291d7339071SHans Reiser 				break;
292d7339071SHans Reiser 
2930cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
2940cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
2950cd6144aSJohannes Weiner 				continue;
2960cd6144aSJohannes Weiner 			}
2970cd6144aSJohannes Weiner 
298529ae9aaSNick Piggin 			if (!trylock_page(page))
2991da177e4SLinus Torvalds 				continue;
300b85e0effSHugh Dickins 			WARN_ON(page->index != index);
3011da177e4SLinus Torvalds 			if (PageWriteback(page)) {
3021da177e4SLinus Torvalds 				unlock_page(page);
3031da177e4SLinus Torvalds 				continue;
3041da177e4SLinus Torvalds 			}
305750b4987SNick Piggin 			truncate_inode_page(mapping, page);
3061da177e4SLinus Torvalds 			unlock_page(page);
3071da177e4SLinus Torvalds 		}
3080cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
3091da177e4SLinus Torvalds 		pagevec_release(&pvec);
310e5598f8bSHugh Dickins 		mem_cgroup_uncharge_end();
3111da177e4SLinus Torvalds 		cond_resched();
312b85e0effSHugh Dickins 		index++;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3155a720394SLukas Czerner 	if (partial_start) {
3161da177e4SLinus Torvalds 		struct page *page = find_lock_page(mapping, start - 1);
3171da177e4SLinus Torvalds 		if (page) {
3185a720394SLukas Czerner 			unsigned int top = PAGE_CACHE_SIZE;
3195a720394SLukas Czerner 			if (start > end) {
3205a720394SLukas Czerner 				/* Truncation within a single page */
3215a720394SLukas Czerner 				top = partial_end;
3225a720394SLukas Czerner 				partial_end = 0;
3235a720394SLukas Czerner 			}
3241da177e4SLinus Torvalds 			wait_on_page_writeback(page);
3255a720394SLukas Czerner 			zero_user_segment(page, partial_start, top);
3265a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3275a720394SLukas Czerner 			if (page_has_private(page))
3285a720394SLukas Czerner 				do_invalidatepage(page, partial_start,
3295a720394SLukas Czerner 						  top - partial_start);
3301da177e4SLinus Torvalds 			unlock_page(page);
3311da177e4SLinus Torvalds 			page_cache_release(page);
3321da177e4SLinus Torvalds 		}
3331da177e4SLinus Torvalds 	}
3345a720394SLukas Czerner 	if (partial_end) {
3355a720394SLukas Czerner 		struct page *page = find_lock_page(mapping, end);
3365a720394SLukas Czerner 		if (page) {
3375a720394SLukas Czerner 			wait_on_page_writeback(page);
3385a720394SLukas Czerner 			zero_user_segment(page, 0, partial_end);
3395a720394SLukas Czerner 			cleancache_invalidate_page(mapping, page);
3405a720394SLukas Czerner 			if (page_has_private(page))
3415a720394SLukas Czerner 				do_invalidatepage(page, 0,
3425a720394SLukas Czerner 						  partial_end);
3435a720394SLukas Czerner 			unlock_page(page);
3445a720394SLukas Czerner 			page_cache_release(page);
3455a720394SLukas Czerner 		}
3465a720394SLukas Czerner 	}
3475a720394SLukas Czerner 	/*
3485a720394SLukas Czerner 	 * If the truncation happened within a single page no pages
3495a720394SLukas Czerner 	 * will be released, just zeroed, so we can bail out now.
3505a720394SLukas Czerner 	 */
3515a720394SLukas Czerner 	if (start >= end)
3525a720394SLukas Czerner 		return;
3531da177e4SLinus Torvalds 
354b85e0effSHugh Dickins 	index = start;
3551da177e4SLinus Torvalds 	for ( ; ; ) {
3561da177e4SLinus Torvalds 		cond_resched();
3570cd6144aSJohannes Weiner 		if (!pagevec_lookup_entries(&pvec, mapping, index,
3580cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE),
3590cd6144aSJohannes Weiner 			indices)) {
360b85e0effSHugh Dickins 			if (index == start)
3611da177e4SLinus Torvalds 				break;
362b85e0effSHugh Dickins 			index = start;
3631da177e4SLinus Torvalds 			continue;
3641da177e4SLinus Torvalds 		}
3650cd6144aSJohannes Weiner 		if (index == start && indices[0] >= end) {
3660cd6144aSJohannes Weiner 			pagevec_remove_exceptionals(&pvec);
367d7339071SHans Reiser 			pagevec_release(&pvec);
368d7339071SHans Reiser 			break;
369d7339071SHans Reiser 		}
370569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_start();
3711da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
3721da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
3731da177e4SLinus Torvalds 
374b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
3750cd6144aSJohannes Weiner 			index = indices[i];
3765a720394SLukas Czerner 			if (index >= end)
377d7339071SHans Reiser 				break;
378b85e0effSHugh Dickins 
3790cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
3800cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
3810cd6144aSJohannes Weiner 				continue;
3820cd6144aSJohannes Weiner 			}
3830cd6144aSJohannes Weiner 
3841da177e4SLinus Torvalds 			lock_page(page);
385b85e0effSHugh Dickins 			WARN_ON(page->index != index);
3861da177e4SLinus Torvalds 			wait_on_page_writeback(page);
387750b4987SNick Piggin 			truncate_inode_page(mapping, page);
3881da177e4SLinus Torvalds 			unlock_page(page);
3891da177e4SLinus Torvalds 		}
3900cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
3911da177e4SLinus Torvalds 		pagevec_release(&pvec);
392569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_end();
393b85e0effSHugh Dickins 		index++;
3941da177e4SLinus Torvalds 	}
3953167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
3961da177e4SLinus Torvalds }
397d7339071SHans Reiser EXPORT_SYMBOL(truncate_inode_pages_range);
3981da177e4SLinus Torvalds 
399d7339071SHans Reiser /**
400d7339071SHans Reiser  * truncate_inode_pages - truncate *all* the pages from an offset
401d7339071SHans Reiser  * @mapping: mapping to truncate
402d7339071SHans Reiser  * @lstart: offset from which to truncate
403d7339071SHans Reiser  *
4041b1dcc1bSJes Sorensen  * Called under (and serialised by) inode->i_mutex.
40508142579SJan Kara  *
40608142579SJan Kara  * Note: When this function returns, there can be a page in the process of
40708142579SJan Kara  * deletion (inside __delete_from_page_cache()) in the specified range.  Thus
40808142579SJan Kara  * mapping->nrpages can be non-zero when this function returns even after
40908142579SJan Kara  * truncation of the whole mapping.
410d7339071SHans Reiser  */
411d7339071SHans Reiser void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
412d7339071SHans Reiser {
413d7339071SHans Reiser 	truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
414d7339071SHans Reiser }
4151da177e4SLinus Torvalds EXPORT_SYMBOL(truncate_inode_pages);
4161da177e4SLinus Torvalds 
41728697355SMike Waychison /**
41891b0abe3SJohannes Weiner  * truncate_inode_pages_final - truncate *all* pages before inode dies
41991b0abe3SJohannes Weiner  * @mapping: mapping to truncate
42091b0abe3SJohannes Weiner  *
42191b0abe3SJohannes Weiner  * Called under (and serialized by) inode->i_mutex.
42291b0abe3SJohannes Weiner  *
42391b0abe3SJohannes Weiner  * Filesystems have to use this in the .evict_inode path to inform the
42491b0abe3SJohannes Weiner  * VM that this is the final truncate and the inode is going away.
42591b0abe3SJohannes Weiner  */
42691b0abe3SJohannes Weiner void truncate_inode_pages_final(struct address_space *mapping)
42791b0abe3SJohannes Weiner {
42891b0abe3SJohannes Weiner 	unsigned long nrshadows;
42991b0abe3SJohannes Weiner 	unsigned long nrpages;
43091b0abe3SJohannes Weiner 
43191b0abe3SJohannes Weiner 	/*
43291b0abe3SJohannes Weiner 	 * Page reclaim can not participate in regular inode lifetime
43391b0abe3SJohannes Weiner 	 * management (can't call iput()) and thus can race with the
43491b0abe3SJohannes Weiner 	 * inode teardown.  Tell it when the address space is exiting,
43591b0abe3SJohannes Weiner 	 * so that it does not install eviction information after the
43691b0abe3SJohannes Weiner 	 * final truncate has begun.
43791b0abe3SJohannes Weiner 	 */
43891b0abe3SJohannes Weiner 	mapping_set_exiting(mapping);
43991b0abe3SJohannes Weiner 
44091b0abe3SJohannes Weiner 	/*
44191b0abe3SJohannes Weiner 	 * When reclaim installs eviction entries, it increases
44291b0abe3SJohannes Weiner 	 * nrshadows first, then decreases nrpages.  Make sure we see
44391b0abe3SJohannes Weiner 	 * this in the right order or we might miss an entry.
44491b0abe3SJohannes Weiner 	 */
44591b0abe3SJohannes Weiner 	nrpages = mapping->nrpages;
44691b0abe3SJohannes Weiner 	smp_rmb();
44791b0abe3SJohannes Weiner 	nrshadows = mapping->nrshadows;
44891b0abe3SJohannes Weiner 
44991b0abe3SJohannes Weiner 	if (nrpages || nrshadows) {
45091b0abe3SJohannes Weiner 		/*
45191b0abe3SJohannes Weiner 		 * As truncation uses a lockless tree lookup, cycle
45291b0abe3SJohannes Weiner 		 * the tree lock to make sure any ongoing tree
45391b0abe3SJohannes Weiner 		 * modification that does not see AS_EXITING is
45491b0abe3SJohannes Weiner 		 * completed before starting the final truncate.
45591b0abe3SJohannes Weiner 		 */
45691b0abe3SJohannes Weiner 		spin_lock_irq(&mapping->tree_lock);
45791b0abe3SJohannes Weiner 		spin_unlock_irq(&mapping->tree_lock);
45891b0abe3SJohannes Weiner 
45991b0abe3SJohannes Weiner 		truncate_inode_pages(mapping, 0);
46091b0abe3SJohannes Weiner 	}
46191b0abe3SJohannes Weiner }
46291b0abe3SJohannes Weiner EXPORT_SYMBOL(truncate_inode_pages_final);
46391b0abe3SJohannes Weiner 
46491b0abe3SJohannes Weiner /**
46528697355SMike Waychison  * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
46628697355SMike Waychison  * @mapping: the address_space which holds the pages to invalidate
46728697355SMike Waychison  * @start: the offset 'from' which to invalidate
46828697355SMike Waychison  * @end: the offset 'to' which to invalidate (inclusive)
46928697355SMike Waychison  *
47028697355SMike Waychison  * This function only removes the unlocked pages, if you want to
47128697355SMike Waychison  * remove all the pages of one inode, you must call truncate_inode_pages.
47228697355SMike Waychison  *
47328697355SMike Waychison  * invalidate_mapping_pages() will not block on IO activity. It will not
47428697355SMike Waychison  * invalidate pages which are dirty, locked, under writeback or mapped into
47528697355SMike Waychison  * pagetables.
47628697355SMike Waychison  */
47728697355SMike Waychison unsigned long invalidate_mapping_pages(struct address_space *mapping,
47828697355SMike Waychison 		pgoff_t start, pgoff_t end)
4791da177e4SLinus Torvalds {
4800cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
4811da177e4SLinus Torvalds 	struct pagevec pvec;
482b85e0effSHugh Dickins 	pgoff_t index = start;
48331560180SMinchan Kim 	unsigned long ret;
48431560180SMinchan Kim 	unsigned long count = 0;
4851da177e4SLinus Torvalds 	int i;
4861da177e4SLinus Torvalds 
48731475dd6SHugh Dickins 	/*
48831475dd6SHugh Dickins 	 * Note: this function may get called on a shmem/tmpfs mapping:
48931475dd6SHugh Dickins 	 * pagevec_lookup() might then return 0 prematurely (because it
49031475dd6SHugh Dickins 	 * got a gangful of swap entries); but it's hardly worth worrying
49131475dd6SHugh Dickins 	 * about - it can rarely have anything to free from such a mapping
49231475dd6SHugh Dickins 	 * (most pages are dirty), and already skips over any difficulties.
49331475dd6SHugh Dickins 	 */
49431475dd6SHugh Dickins 
4951da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
4960cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
4970cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
4980cd6144aSJohannes Weiner 			indices)) {
499569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_start();
5001da177e4SLinus Torvalds 		for (i = 0; i < pagevec_count(&pvec); i++) {
5011da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
5021da177e4SLinus Torvalds 
503b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
5040cd6144aSJohannes Weiner 			index = indices[i];
505b85e0effSHugh Dickins 			if (index > end)
506b85e0effSHugh Dickins 				break;
507e0f23603SNeilBrown 
5080cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
5090cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
5100cd6144aSJohannes Weiner 				continue;
5110cd6144aSJohannes Weiner 			}
5120cd6144aSJohannes Weiner 
513b85e0effSHugh Dickins 			if (!trylock_page(page))
514b85e0effSHugh Dickins 				continue;
515b85e0effSHugh Dickins 			WARN_ON(page->index != index);
51631560180SMinchan Kim 			ret = invalidate_inode_page(page);
5171da177e4SLinus Torvalds 			unlock_page(page);
51831560180SMinchan Kim 			/*
51931560180SMinchan Kim 			 * Invalidation is a hint that the page is no longer
52031560180SMinchan Kim 			 * of interest and try to speed up its reclaim.
52131560180SMinchan Kim 			 */
52231560180SMinchan Kim 			if (!ret)
52331560180SMinchan Kim 				deactivate_page(page);
52431560180SMinchan Kim 			count += ret;
5251da177e4SLinus Torvalds 		}
5260cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
5271da177e4SLinus Torvalds 		pagevec_release(&pvec);
528569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_end();
529fc9a07e7SAndrew Morton 		cond_resched();
530b85e0effSHugh Dickins 		index++;
5311da177e4SLinus Torvalds 	}
53231560180SMinchan Kim 	return count;
5331da177e4SLinus Torvalds }
53454bc4855SAnton Altaparmakov EXPORT_SYMBOL(invalidate_mapping_pages);
5351da177e4SLinus Torvalds 
536bd4c8ce4SAndrew Morton /*
537bd4c8ce4SAndrew Morton  * This is like invalidate_complete_page(), except it ignores the page's
538bd4c8ce4SAndrew Morton  * refcount.  We do this because invalidate_inode_pages2() needs stronger
539bd4c8ce4SAndrew Morton  * invalidation guarantees, and cannot afford to leave pages behind because
5402706a1b8SAnderson Briglia  * shrink_page_list() has a temp ref on them, or because they're transiently
5412706a1b8SAnderson Briglia  * sitting in the lru_cache_add() pagevecs.
542bd4c8ce4SAndrew Morton  */
543bd4c8ce4SAndrew Morton static int
544bd4c8ce4SAndrew Morton invalidate_complete_page2(struct address_space *mapping, struct page *page)
545bd4c8ce4SAndrew Morton {
546bd4c8ce4SAndrew Morton 	if (page->mapping != mapping)
547bd4c8ce4SAndrew Morton 		return 0;
548bd4c8ce4SAndrew Morton 
549266cf658SDavid Howells 	if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
550bd4c8ce4SAndrew Morton 		return 0;
551bd4c8ce4SAndrew Morton 
55219fd6231SNick Piggin 	spin_lock_irq(&mapping->tree_lock);
553bd4c8ce4SAndrew Morton 	if (PageDirty(page))
554bd4c8ce4SAndrew Morton 		goto failed;
555bd4c8ce4SAndrew Morton 
556266cf658SDavid Howells 	BUG_ON(page_has_private(page));
55791b0abe3SJohannes Weiner 	__delete_from_page_cache(page, NULL);
55819fd6231SNick Piggin 	spin_unlock_irq(&mapping->tree_lock);
559e767e056SDaisuke Nishimura 	mem_cgroup_uncharge_cache_page(page);
5606072d13cSLinus Torvalds 
5616072d13cSLinus Torvalds 	if (mapping->a_ops->freepage)
5626072d13cSLinus Torvalds 		mapping->a_ops->freepage(page);
5636072d13cSLinus Torvalds 
564bd4c8ce4SAndrew Morton 	page_cache_release(page);	/* pagecache ref */
565bd4c8ce4SAndrew Morton 	return 1;
566bd4c8ce4SAndrew Morton failed:
56719fd6231SNick Piggin 	spin_unlock_irq(&mapping->tree_lock);
568bd4c8ce4SAndrew Morton 	return 0;
569bd4c8ce4SAndrew Morton }
570bd4c8ce4SAndrew Morton 
571e3db7691STrond Myklebust static int do_launder_page(struct address_space *mapping, struct page *page)
572e3db7691STrond Myklebust {
573e3db7691STrond Myklebust 	if (!PageDirty(page))
574e3db7691STrond Myklebust 		return 0;
575e3db7691STrond Myklebust 	if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
576e3db7691STrond Myklebust 		return 0;
577e3db7691STrond Myklebust 	return mapping->a_ops->launder_page(page);
578e3db7691STrond Myklebust }
579e3db7691STrond Myklebust 
5801da177e4SLinus Torvalds /**
5811da177e4SLinus Torvalds  * invalidate_inode_pages2_range - remove range of pages from an address_space
58267be2dd1SMartin Waitz  * @mapping: the address_space
5831da177e4SLinus Torvalds  * @start: the page offset 'from' which to invalidate
5841da177e4SLinus Torvalds  * @end: the page offset 'to' which to invalidate (inclusive)
5851da177e4SLinus Torvalds  *
5861da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
5871da177e4SLinus Torvalds  * invalidation.
5881da177e4SLinus Torvalds  *
5896ccfa806SHisashi Hifumi  * Returns -EBUSY if any pages could not be invalidated.
5901da177e4SLinus Torvalds  */
5911da177e4SLinus Torvalds int invalidate_inode_pages2_range(struct address_space *mapping,
5921da177e4SLinus Torvalds 				  pgoff_t start, pgoff_t end)
5931da177e4SLinus Torvalds {
5940cd6144aSJohannes Weiner 	pgoff_t indices[PAGEVEC_SIZE];
5951da177e4SLinus Torvalds 	struct pagevec pvec;
596b85e0effSHugh Dickins 	pgoff_t index;
5971da177e4SLinus Torvalds 	int i;
5981da177e4SLinus Torvalds 	int ret = 0;
5990dd1334fSHisashi Hifumi 	int ret2 = 0;
6001da177e4SLinus Torvalds 	int did_range_unmap = 0;
6011da177e4SLinus Torvalds 
6023167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
6031da177e4SLinus Torvalds 	pagevec_init(&pvec, 0);
604b85e0effSHugh Dickins 	index = start;
6050cd6144aSJohannes Weiner 	while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
6060cd6144aSJohannes Weiner 			min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
6070cd6144aSJohannes Weiner 			indices)) {
608569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_start();
6097b965e08STrond Myklebust 		for (i = 0; i < pagevec_count(&pvec); i++) {
6101da177e4SLinus Torvalds 			struct page *page = pvec.pages[i];
611b85e0effSHugh Dickins 
612b85e0effSHugh Dickins 			/* We rely upon deletion not changing page->index */
6130cd6144aSJohannes Weiner 			index = indices[i];
614b85e0effSHugh Dickins 			if (index > end)
615b85e0effSHugh Dickins 				break;
6161da177e4SLinus Torvalds 
6170cd6144aSJohannes Weiner 			if (radix_tree_exceptional_entry(page)) {
6180cd6144aSJohannes Weiner 				clear_exceptional_entry(mapping, index, page);
6190cd6144aSJohannes Weiner 				continue;
6200cd6144aSJohannes Weiner 			}
6210cd6144aSJohannes Weiner 
6221da177e4SLinus Torvalds 			lock_page(page);
623b85e0effSHugh Dickins 			WARN_ON(page->index != index);
6241da177e4SLinus Torvalds 			if (page->mapping != mapping) {
6251da177e4SLinus Torvalds 				unlock_page(page);
6261da177e4SLinus Torvalds 				continue;
6271da177e4SLinus Torvalds 			}
6281da177e4SLinus Torvalds 			wait_on_page_writeback(page);
629d00806b1SNick Piggin 			if (page_mapped(page)) {
6301da177e4SLinus Torvalds 				if (!did_range_unmap) {
6311da177e4SLinus Torvalds 					/*
6321da177e4SLinus Torvalds 					 * Zap the rest of the file in one hit.
6331da177e4SLinus Torvalds 					 */
6341da177e4SLinus Torvalds 					unmap_mapping_range(mapping,
635b85e0effSHugh Dickins 					   (loff_t)index << PAGE_CACHE_SHIFT,
636b85e0effSHugh Dickins 					   (loff_t)(1 + end - index)
6371da177e4SLinus Torvalds 							 << PAGE_CACHE_SHIFT,
6381da177e4SLinus Torvalds 					    0);
6391da177e4SLinus Torvalds 					did_range_unmap = 1;
6401da177e4SLinus Torvalds 				} else {
6411da177e4SLinus Torvalds 					/*
6421da177e4SLinus Torvalds 					 * Just zap this page
6431da177e4SLinus Torvalds 					 */
6441da177e4SLinus Torvalds 					unmap_mapping_range(mapping,
645b85e0effSHugh Dickins 					   (loff_t)index << PAGE_CACHE_SHIFT,
6461da177e4SLinus Torvalds 					   PAGE_CACHE_SIZE, 0);
6471da177e4SLinus Torvalds 				}
6481da177e4SLinus Torvalds 			}
649d00806b1SNick Piggin 			BUG_ON(page_mapped(page));
6500dd1334fSHisashi Hifumi 			ret2 = do_launder_page(mapping, page);
6510dd1334fSHisashi Hifumi 			if (ret2 == 0) {
6520dd1334fSHisashi Hifumi 				if (!invalidate_complete_page2(mapping, page))
6536ccfa806SHisashi Hifumi 					ret2 = -EBUSY;
6540dd1334fSHisashi Hifumi 			}
6550dd1334fSHisashi Hifumi 			if (ret2 < 0)
6560dd1334fSHisashi Hifumi 				ret = ret2;
6571da177e4SLinus Torvalds 			unlock_page(page);
6581da177e4SLinus Torvalds 		}
6590cd6144aSJohannes Weiner 		pagevec_remove_exceptionals(&pvec);
6601da177e4SLinus Torvalds 		pagevec_release(&pvec);
661569b846dSKAMEZAWA Hiroyuki 		mem_cgroup_uncharge_end();
6621da177e4SLinus Torvalds 		cond_resched();
663b85e0effSHugh Dickins 		index++;
6641da177e4SLinus Torvalds 	}
6653167760fSDan Magenheimer 	cleancache_invalidate_inode(mapping);
6661da177e4SLinus Torvalds 	return ret;
6671da177e4SLinus Torvalds }
6681da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds /**
6711da177e4SLinus Torvalds  * invalidate_inode_pages2 - remove all pages from an address_space
67267be2dd1SMartin Waitz  * @mapping: the address_space
6731da177e4SLinus Torvalds  *
6741da177e4SLinus Torvalds  * Any pages which are found to be mapped into pagetables are unmapped prior to
6751da177e4SLinus Torvalds  * invalidation.
6761da177e4SLinus Torvalds  *
677e9de25ddSPeng Tao  * Returns -EBUSY if any pages could not be invalidated.
6781da177e4SLinus Torvalds  */
6791da177e4SLinus Torvalds int invalidate_inode_pages2(struct address_space *mapping)
6801da177e4SLinus Torvalds {
6811da177e4SLinus Torvalds 	return invalidate_inode_pages2_range(mapping, 0, -1);
6821da177e4SLinus Torvalds }
6831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
68425d9e2d1Snpiggin@suse.de 
68525d9e2d1Snpiggin@suse.de /**
68625d9e2d1Snpiggin@suse.de  * truncate_pagecache - unmap and remove pagecache that has been truncated
68725d9e2d1Snpiggin@suse.de  * @inode: inode
6888a549beaSHugh Dickins  * @newsize: new file size
68925d9e2d1Snpiggin@suse.de  *
69025d9e2d1Snpiggin@suse.de  * inode's new i_size must already be written before truncate_pagecache
69125d9e2d1Snpiggin@suse.de  * is called.
69225d9e2d1Snpiggin@suse.de  *
69325d9e2d1Snpiggin@suse.de  * This function should typically be called before the filesystem
69425d9e2d1Snpiggin@suse.de  * releases resources associated with the freed range (eg. deallocates
69525d9e2d1Snpiggin@suse.de  * blocks). This way, pagecache will always stay logically coherent
69625d9e2d1Snpiggin@suse.de  * with on-disk format, and the filesystem would not have to deal with
69725d9e2d1Snpiggin@suse.de  * situations such as writepage being called for a page that has already
69825d9e2d1Snpiggin@suse.de  * had its underlying blocks deallocated.
69925d9e2d1Snpiggin@suse.de  */
7007caef267SKirill A. Shutemov void truncate_pagecache(struct inode *inode, loff_t newsize)
70125d9e2d1Snpiggin@suse.de {
70225d9e2d1Snpiggin@suse.de 	struct address_space *mapping = inode->i_mapping;
7038a549beaSHugh Dickins 	loff_t holebegin = round_up(newsize, PAGE_SIZE);
70425d9e2d1Snpiggin@suse.de 
70525d9e2d1Snpiggin@suse.de 	/*
70625d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range is called twice, first simply for
70725d9e2d1Snpiggin@suse.de 	 * efficiency so that truncate_inode_pages does fewer
70825d9e2d1Snpiggin@suse.de 	 * single-page unmaps.  However after this first call, and
70925d9e2d1Snpiggin@suse.de 	 * before truncate_inode_pages finishes, it is possible for
71025d9e2d1Snpiggin@suse.de 	 * private pages to be COWed, which remain after
71125d9e2d1Snpiggin@suse.de 	 * truncate_inode_pages finishes, hence the second
71225d9e2d1Snpiggin@suse.de 	 * unmap_mapping_range call must be made for correctness.
71325d9e2d1Snpiggin@suse.de 	 */
7148a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
7158a549beaSHugh Dickins 	truncate_inode_pages(mapping, newsize);
7168a549beaSHugh Dickins 	unmap_mapping_range(mapping, holebegin, 0, 1);
71725d9e2d1Snpiggin@suse.de }
71825d9e2d1Snpiggin@suse.de EXPORT_SYMBOL(truncate_pagecache);
71925d9e2d1Snpiggin@suse.de 
72025d9e2d1Snpiggin@suse.de /**
7212c27c65eSChristoph Hellwig  * truncate_setsize - update inode and pagecache for a new file size
7222c27c65eSChristoph Hellwig  * @inode: inode
7232c27c65eSChristoph Hellwig  * @newsize: new file size
7242c27c65eSChristoph Hellwig  *
725382e27daSJan Kara  * truncate_setsize updates i_size and performs pagecache truncation (if
726382e27daSJan Kara  * necessary) to @newsize. It will be typically be called from the filesystem's
727382e27daSJan Kara  * setattr function when ATTR_SIZE is passed in.
7282c27c65eSChristoph Hellwig  *
729382e27daSJan Kara  * Must be called with inode_mutex held and before all filesystem specific
730382e27daSJan Kara  * block truncation has been performed.
7312c27c65eSChristoph Hellwig  */
7322c27c65eSChristoph Hellwig void truncate_setsize(struct inode *inode, loff_t newsize)
7332c27c65eSChristoph Hellwig {
7342c27c65eSChristoph Hellwig 	i_size_write(inode, newsize);
7357caef267SKirill A. Shutemov 	truncate_pagecache(inode, newsize);
7362c27c65eSChristoph Hellwig }
7372c27c65eSChristoph Hellwig EXPORT_SYMBOL(truncate_setsize);
7382c27c65eSChristoph Hellwig 
7392c27c65eSChristoph Hellwig /**
740623e3db9SHugh Dickins  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
741623e3db9SHugh Dickins  * @inode: inode
742623e3db9SHugh Dickins  * @lstart: offset of beginning of hole
743623e3db9SHugh Dickins  * @lend: offset of last byte of hole
744623e3db9SHugh Dickins  *
745623e3db9SHugh Dickins  * This function should typically be called before the filesystem
746623e3db9SHugh Dickins  * releases resources associated with the freed range (eg. deallocates
747623e3db9SHugh Dickins  * blocks). This way, pagecache will always stay logically coherent
748623e3db9SHugh Dickins  * with on-disk format, and the filesystem would not have to deal with
749623e3db9SHugh Dickins  * situations such as writepage being called for a page that has already
750623e3db9SHugh Dickins  * had its underlying blocks deallocated.
751623e3db9SHugh Dickins  */
752623e3db9SHugh Dickins void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
753623e3db9SHugh Dickins {
754623e3db9SHugh Dickins 	struct address_space *mapping = inode->i_mapping;
755623e3db9SHugh Dickins 	loff_t unmap_start = round_up(lstart, PAGE_SIZE);
756623e3db9SHugh Dickins 	loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
757623e3db9SHugh Dickins 	/*
758623e3db9SHugh Dickins 	 * This rounding is currently just for example: unmap_mapping_range
759623e3db9SHugh Dickins 	 * expands its hole outwards, whereas we want it to contract the hole
760623e3db9SHugh Dickins 	 * inwards.  However, existing callers of truncate_pagecache_range are
7615a720394SLukas Czerner 	 * doing their own page rounding first.  Note that unmap_mapping_range
7625a720394SLukas Czerner 	 * allows holelen 0 for all, and we allow lend -1 for end of file.
763623e3db9SHugh Dickins 	 */
764623e3db9SHugh Dickins 
765623e3db9SHugh Dickins 	/*
766623e3db9SHugh Dickins 	 * Unlike in truncate_pagecache, unmap_mapping_range is called only
767623e3db9SHugh Dickins 	 * once (before truncating pagecache), and without "even_cows" flag:
768623e3db9SHugh Dickins 	 * hole-punching should not remove private COWed pages from the hole.
769623e3db9SHugh Dickins 	 */
770623e3db9SHugh Dickins 	if ((u64)unmap_end > (u64)unmap_start)
771623e3db9SHugh Dickins 		unmap_mapping_range(mapping, unmap_start,
772623e3db9SHugh Dickins 				    1 + unmap_end - unmap_start, 0);
773623e3db9SHugh Dickins 	truncate_inode_pages_range(mapping, lstart, lend);
774623e3db9SHugh Dickins }
775623e3db9SHugh Dickins EXPORT_SYMBOL(truncate_pagecache_range);
776