2 * mm/truncate.c - code for taking down pages from address_spaces
4 * Copyright (C) 2002, Linus Torvalds
6 * 10Sep2002 akpm@zip.com.au
10 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/pagemap.h>
14 #include <linux/pagevec.h>
15 #include <linux/buffer_head.h> /* grr. try_to_release_page,
16 block_invalidatepage */
19 static int do_invalidatepage(struct page *page, unsigned long offset)
21 int (*invalidatepage)(struct page *, unsigned long);
22 invalidatepage = page->mapping->a_ops->invalidatepage;
23 if (invalidatepage == NULL)
24 invalidatepage = block_invalidatepage;
25 return (*invalidatepage)(page, offset);
28 static inline void truncate_partial_page(struct page *page, unsigned partial)
30 memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial);
31 if (PagePrivate(page))
32 do_invalidatepage(page, partial);
36 * If truncate cannot remove the fs-private metadata from the page, the page
37 * becomes anonymous. It will be left on the LRU and may even be mapped into
38 * user pagetables if we're racing with filemap_nopage().
40 * We need to bale out if page->mapping is no longer equal to the original
41 * mapping. This happens a) when the VM reclaimed the page while we waited on
42 * its lock, b) when a concurrent invalidate_inode_pages got there first and
43 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
46 truncate_complete_page(struct address_space *mapping, struct page *page)
48 if (page->mapping != mapping)
51 if (PagePrivate(page))
52 do_invalidatepage(page, 0);
54 clear_page_dirty(page);
55 ClearPageUptodate(page);
56 ClearPageMappedToDisk(page);
57 remove_from_page_cache(page);
58 page_cache_release(page); /* pagecache ref */
62 * This is for invalidate_inode_pages(). That function can be called at
63 * any time, and is not supposed to throw away dirty pages. But pages can
64 * be marked dirty at any time too. So we re-check the dirtiness inside
65 * ->tree_lock. That provides exclusion against the __set_page_dirty
68 * Returns non-zero if the page was successfully invalidated.
71 invalidate_complete_page(struct address_space *mapping, struct page *page)
73 if (page->mapping != mapping)
76 if (PagePrivate(page) && !try_to_release_page(page, 0))
79 write_lock_irq(&mapping->tree_lock);
80 if (PageDirty(page)) {
81 write_unlock_irq(&mapping->tree_lock);
85 BUG_ON(PagePrivate(page));
86 __remove_from_page_cache(page);
87 write_unlock_irq(&mapping->tree_lock);
88 ClearPageUptodate(page);
89 page_cache_release(page); /* pagecache ref */
94 * truncate_inode_pages - truncate *all* the pages from an offset
95 * @mapping: mapping to truncate
96 * @lstart: offset from which to truncate
98 * Truncate the page cache at a set offset, removing the pages that are beyond
99 * that offset (and zeroing out partial pages).
101 * Truncate takes two passes - the first pass is nonblocking. It will not
102 * block on page locks and it will not block on writeback. The second pass
103 * will wait. This is to prevent as much IO as possible in the affected region.
104 * The first pass will remove most pages, so the search cost of the second pass
107 * When looking at page->index outside the page lock we need to be careful to
108 * copy it into a local to avoid races (it could change at any time).
110 * We pass down the cache-hot hint to the page freeing code. Even if the
111 * mapping is large, it is probably the case that the final pages are the most
112 * recently touched, and freeing happens in ascending file offset order.
114 * Called under (and serialised by) inode->i_sem.
116 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
118 const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
119 const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
124 if (mapping->nrpages == 0)
127 pagevec_init(&pvec, 0);
129 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
130 for (i = 0; i < pagevec_count(&pvec); i++) {
131 struct page *page = pvec.pages[i];
132 pgoff_t page_index = page->index;
134 if (page_index > next)
137 if (TestSetPageLocked(page))
139 if (PageWriteback(page)) {
143 truncate_complete_page(mapping, page);
146 pagevec_release(&pvec);
151 struct page *page = find_lock_page(mapping, start - 1);
153 wait_on_page_writeback(page);
154 truncate_partial_page(page, partial);
156 page_cache_release(page);
163 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
169 for (i = 0; i < pagevec_count(&pvec); i++) {
170 struct page *page = pvec.pages[i];
173 wait_on_page_writeback(page);
174 if (page->index > next)
177 truncate_complete_page(mapping, page);
180 pagevec_release(&pvec);
184 EXPORT_SYMBOL(truncate_inode_pages);
187 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
188 * @mapping: the address_space which holds the pages to invalidate
189 * @start: the offset 'from' which to invalidate
190 * @end: the offset 'to' which to invalidate (inclusive)
192 * This function only removes the unlocked pages, if you want to
193 * remove all the pages of one inode, you must call truncate_inode_pages.
195 * invalidate_mapping_pages() will not block on IO activity. It will not
196 * invalidate pages which are dirty, locked, under writeback or mapped into
199 unsigned long invalidate_mapping_pages(struct address_space *mapping,
200 pgoff_t start, pgoff_t end)
203 pgoff_t next = start;
204 unsigned long ret = 0;
207 pagevec_init(&pvec, 0);
208 while (next <= end &&
209 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
210 for (i = 0; i < pagevec_count(&pvec); i++) {
211 struct page *page = pvec.pages[i];
213 if (TestSetPageLocked(page)) {
217 if (page->index > next)
220 if (PageDirty(page) || PageWriteback(page))
222 if (page_mapped(page))
224 ret += invalidate_complete_page(mapping, page);
230 pagevec_release(&pvec);
236 unsigned long invalidate_inode_pages(struct address_space *mapping)
238 return invalidate_mapping_pages(mapping, 0, ~0UL);
241 EXPORT_SYMBOL(invalidate_inode_pages);
244 * invalidate_inode_pages2_range - remove range of pages from an address_space
245 * @mapping: the address_space
246 * @start: the page offset 'from' which to invalidate
247 * @end: the page offset 'to' which to invalidate (inclusive)
249 * Any pages which are found to be mapped into pagetables are unmapped prior to
252 * Returns -EIO if any pages could not be invalidated.
254 int invalidate_inode_pages2_range(struct address_space *mapping,
255 pgoff_t start, pgoff_t end)
261 int did_range_unmap = 0;
264 pagevec_init(&pvec, 0);
266 while (next <= end && !ret && !wrapped &&
267 pagevec_lookup(&pvec, mapping, next,
268 min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
269 for (i = 0; !ret && i < pagevec_count(&pvec); i++) {
270 struct page *page = pvec.pages[i];
275 if (page->mapping != mapping) {
279 page_index = page->index;
280 next = page_index + 1;
283 if (page_index > end) {
287 wait_on_page_writeback(page);
288 while (page_mapped(page)) {
289 if (!did_range_unmap) {
291 * Zap the rest of the file in one hit.
293 unmap_mapping_range(mapping,
294 page_index << PAGE_CACHE_SHIFT,
295 (end - page_index + 1)
303 unmap_mapping_range(mapping,
304 page_index << PAGE_CACHE_SHIFT,
308 was_dirty = test_clear_page_dirty(page);
309 if (!invalidate_complete_page(mapping, page)) {
311 set_page_dirty(page);
316 pagevec_release(&pvec);
321 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
324 * invalidate_inode_pages2 - remove all pages from an address_space
325 * @mapping: the address_space
327 * Any pages which are found to be mapped into pagetables are unmapped prior to
330 * Returns -EIO if any pages could not be invalidated.
332 int invalidate_inode_pages2(struct address_space *mapping)
334 return invalidate_inode_pages2_range(mapping, 0, -1);
336 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);