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/swap.h>
13 #include <linux/module.h>
14 #include <linux/pagemap.h>
15 #include <linux/pagevec.h>
16 #include <linux/buffer_head.h> /* grr. try_to_release_page,
21 * do_invalidatepage - invalidate part of all of a page
22 * @page: the page which is affected
23 * @offset: the index of the truncation point
25 * do_invalidatepage() is called when all or part of the page has become
26 * invalidated by a truncate operation.
28 * do_invalidatepage() does not have to release all buffers, but it must
29 * ensure that no dirty buffer is left outside @offset and that no I/O
30 * is underway against any of the blocks which are outside the truncation
31 * point. Because the caller is about to free (and possibly reuse) those
34 void do_invalidatepage(struct page *page, unsigned long offset)
36 void (*invalidatepage)(struct page *, unsigned long);
37 invalidatepage = page->mapping->a_ops->invalidatepage;
39 invalidatepage = block_invalidatepage;
41 (*invalidatepage)(page, offset);
44 static inline void truncate_partial_page(struct page *page, unsigned partial)
46 memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial);
47 if (PagePrivate(page))
48 do_invalidatepage(page, partial);
52 * If truncate cannot remove the fs-private metadata from the page, the page
53 * becomes anonymous. It will be left on the LRU and may even be mapped into
54 * user pagetables if we're racing with filemap_nopage().
56 * We need to bale out if page->mapping is no longer equal to the original
57 * mapping. This happens a) when the VM reclaimed the page while we waited on
58 * its lock, b) when a concurrent invalidate_inode_pages got there first and
59 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
62 truncate_complete_page(struct address_space *mapping, struct page *page)
64 if (page->mapping != mapping)
67 if (PagePrivate(page))
68 do_invalidatepage(page, 0);
70 clear_page_dirty(page);
71 ClearPageUptodate(page);
72 ClearPageMappedToDisk(page);
73 remove_from_page_cache(page);
74 page_cache_release(page); /* pagecache ref */
78 * This is for invalidate_inode_pages(). That function can be called at
79 * any time, and is not supposed to throw away dirty pages. But pages can
80 * be marked dirty at any time too, so use remove_mapping which safely
81 * discards clean, unused pages.
83 * Returns non-zero if the page was successfully invalidated.
86 invalidate_complete_page(struct address_space *mapping, struct page *page)
90 if (page->mapping != mapping)
93 if (PagePrivate(page) && !try_to_release_page(page, 0))
96 ret = remove_mapping(mapping, page);
97 ClearPageUptodate(page);
103 * truncate_inode_pages - truncate range of pages specified by start and
105 * @mapping: mapping to truncate
106 * @lstart: offset from which to truncate
107 * @lend: offset to which to truncate
109 * Truncate the page cache, removing the pages that are between
110 * specified offsets (and zeroing out partial page
111 * (if lstart is not page aligned)).
113 * Truncate takes two passes - the first pass is nonblocking. It will not
114 * block on page locks and it will not block on writeback. The second pass
115 * will wait. This is to prevent as much IO as possible in the affected region.
116 * The first pass will remove most pages, so the search cost of the second pass
119 * When looking at page->index outside the page lock we need to be careful to
120 * copy it into a local to avoid races (it could change at any time).
122 * We pass down the cache-hot hint to the page freeing code. Even if the
123 * mapping is large, it is probably the case that the final pages are the most
124 * recently touched, and freeing happens in ascending file offset order.
126 void truncate_inode_pages_range(struct address_space *mapping,
127 loff_t lstart, loff_t lend)
129 const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
131 const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
136 if (mapping->nrpages == 0)
139 BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
140 end = (lend >> PAGE_CACHE_SHIFT);
142 pagevec_init(&pvec, 0);
144 while (next <= end &&
145 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
146 for (i = 0; i < pagevec_count(&pvec); i++) {
147 struct page *page = pvec.pages[i];
148 pgoff_t page_index = page->index;
150 if (page_index > end) {
155 if (page_index > next)
158 if (TestSetPageLocked(page))
160 if (PageWriteback(page)) {
164 truncate_complete_page(mapping, page);
167 pagevec_release(&pvec);
172 struct page *page = find_lock_page(mapping, start - 1);
174 wait_on_page_writeback(page);
175 truncate_partial_page(page, partial);
177 page_cache_release(page);
184 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
190 if (pvec.pages[0]->index > end) {
191 pagevec_release(&pvec);
194 for (i = 0; i < pagevec_count(&pvec); i++) {
195 struct page *page = pvec.pages[i];
197 if (page->index > end)
200 wait_on_page_writeback(page);
201 if (page->index > next)
204 truncate_complete_page(mapping, page);
207 pagevec_release(&pvec);
210 EXPORT_SYMBOL(truncate_inode_pages_range);
213 * truncate_inode_pages - truncate *all* the pages from an offset
214 * @mapping: mapping to truncate
215 * @lstart: offset from which to truncate
217 * Called under (and serialised by) inode->i_mutex.
219 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
221 truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
223 EXPORT_SYMBOL(truncate_inode_pages);
226 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
227 * @mapping: the address_space which holds the pages to invalidate
228 * @start: the offset 'from' which to invalidate
229 * @end: the offset 'to' which to invalidate (inclusive)
231 * This function only removes the unlocked pages, if you want to
232 * remove all the pages of one inode, you must call truncate_inode_pages.
234 * invalidate_mapping_pages() will not block on IO activity. It will not
235 * invalidate pages which are dirty, locked, under writeback or mapped into
238 unsigned long invalidate_mapping_pages(struct address_space *mapping,
239 pgoff_t start, pgoff_t end)
242 pgoff_t next = start;
243 unsigned long ret = 0;
246 pagevec_init(&pvec, 0);
247 while (next <= end &&
248 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
249 for (i = 0; i < pagevec_count(&pvec); i++) {
250 struct page *page = pvec.pages[i];
254 lock_failed = TestSetPageLocked(page);
257 * We really shouldn't be looking at the ->index of an
258 * unlocked page. But we're not allowed to lock these
259 * pages. So we rely upon nobody altering the ->index
260 * of this (pinned-by-us) page.
269 if (PageDirty(page) || PageWriteback(page))
271 if (page_mapped(page))
273 ret += invalidate_complete_page(mapping, page);
279 pagevec_release(&pvec);
284 unsigned long invalidate_inode_pages(struct address_space *mapping)
286 return invalidate_mapping_pages(mapping, 0, ~0UL);
289 EXPORT_SYMBOL(invalidate_inode_pages);
292 * invalidate_inode_pages2_range - remove range of pages from an address_space
293 * @mapping: the address_space
294 * @start: the page offset 'from' which to invalidate
295 * @end: the page offset 'to' which to invalidate (inclusive)
297 * Any pages which are found to be mapped into pagetables are unmapped prior to
300 * Returns -EIO if any pages could not be invalidated.
302 int invalidate_inode_pages2_range(struct address_space *mapping,
303 pgoff_t start, pgoff_t end)
309 int did_range_unmap = 0;
312 pagevec_init(&pvec, 0);
314 while (next <= end && !ret && !wrapped &&
315 pagevec_lookup(&pvec, mapping, next,
316 min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
317 for (i = 0; !ret && i < pagevec_count(&pvec); i++) {
318 struct page *page = pvec.pages[i];
323 if (page->mapping != mapping) {
327 page_index = page->index;
328 next = page_index + 1;
331 if (page_index > end) {
335 wait_on_page_writeback(page);
336 while (page_mapped(page)) {
337 if (!did_range_unmap) {
339 * Zap the rest of the file in one hit.
341 unmap_mapping_range(mapping,
342 (loff_t)page_index<<PAGE_CACHE_SHIFT,
343 (loff_t)(end - page_index + 1)
351 unmap_mapping_range(mapping,
352 (loff_t)page_index<<PAGE_CACHE_SHIFT,
356 was_dirty = test_clear_page_dirty(page);
357 if (!invalidate_complete_page(mapping, page)) {
359 set_page_dirty(page);
364 pagevec_release(&pvec);
369 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
372 * invalidate_inode_pages2 - remove all pages from an address_space
373 * @mapping: the address_space
375 * Any pages which are found to be mapped into pagetables are unmapped prior to
378 * Returns -EIO if any pages could not be invalidated.
380 int invalidate_inode_pages2(struct address_space *mapping)
382 return invalidate_inode_pages2_range(mapping, 0, -1);
384 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);