initial commit with v2.6.9
[linux-2.6.9-moxart.git] / mm / truncate.c
blob0ab3a157e985f47198183be8aded466db84f3388
1 /*
2 * mm/truncate.c - code for taking down pages from address_spaces
4 * Copyright (C) 2002, Linus Torvalds
6 * 10Sep2002 akpm@zip.com.au
7 * Initial version.
8 */
10 #include <linux/kernel.h>
11 #include <linux/mm.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.
45 static void
46 truncate_complete_page(struct address_space *mapping, struct page *page)
48 if (page->mapping != mapping)
49 return;
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
66 * functions.
68 static int
69 invalidate_complete_page(struct address_space *mapping, struct page *page)
71 if (page->mapping != mapping)
72 return 0;
74 if (PagePrivate(page) && !try_to_release_page(page, 0))
75 return 0;
77 spin_lock_irq(&mapping->tree_lock);
78 if (PageDirty(page)) {
79 spin_unlock_irq(&mapping->tree_lock);
80 return 0;
82 __remove_from_page_cache(page);
83 spin_unlock_irq(&mapping->tree_lock);
84 ClearPageUptodate(page);
85 page_cache_release(page); /* pagecache ref */
86 return 1;
89 /**
90 * truncate_inode_pages - truncate *all* the pages from an offset
91 * @mapping: mapping to truncate
92 * @lstart: offset from which to truncate
94 * Truncate the page cache at a set offset, removing the pages that are beyond
95 * that offset (and zeroing out partial pages).
97 * Truncate takes two passes - the first pass is nonblocking. It will not
98 * block on page locks and it will not block on writeback. The second pass
99 * will wait. This is to prevent as much IO as possible in the affected region.
100 * The first pass will remove most pages, so the search cost of the second pass
101 * is low.
103 * When looking at page->index outside the page lock we need to be careful to
104 * copy it into a local to avoid races (it could change at any time).
106 * We pass down the cache-hot hint to the page freeing code. Even if the
107 * mapping is large, it is probably the case that the final pages are the most
108 * recently touched, and freeing happens in ascending file offset order.
110 * Called under (and serialised by) inode->i_sem.
112 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
114 const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
115 const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
116 struct pagevec pvec;
117 pgoff_t next;
118 int i;
120 if (mapping->nrpages == 0)
121 return;
123 pagevec_init(&pvec, 0);
124 next = start;
125 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
126 for (i = 0; i < pagevec_count(&pvec); i++) {
127 struct page *page = pvec.pages[i];
128 pgoff_t page_index = page->index;
130 if (page_index > next)
131 next = page_index;
132 next++;
133 if (TestSetPageLocked(page))
134 continue;
135 if (PageWriteback(page)) {
136 unlock_page(page);
137 continue;
139 truncate_complete_page(mapping, page);
140 unlock_page(page);
142 pagevec_release(&pvec);
143 cond_resched();
146 if (partial) {
147 struct page *page = find_lock_page(mapping, start - 1);
148 if (page) {
149 wait_on_page_writeback(page);
150 truncate_partial_page(page, partial);
151 unlock_page(page);
152 page_cache_release(page);
156 next = start;
157 for ( ; ; ) {
158 cond_resched();
159 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
160 if (next == start)
161 break;
162 next = start;
163 continue;
165 for (i = 0; i < pagevec_count(&pvec); i++) {
166 struct page *page = pvec.pages[i];
168 lock_page(page);
169 wait_on_page_writeback(page);
170 if (page->index > next)
171 next = page->index;
172 next++;
173 truncate_complete_page(mapping, page);
174 unlock_page(page);
176 pagevec_release(&pvec);
180 EXPORT_SYMBOL(truncate_inode_pages);
183 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
184 * @mapping: the address_space which holds the pages to invalidate
185 * @start: the offset 'from' which to invalidate
186 * @end: the offset 'to' which to invalidate (inclusive)
188 * This function only removes the unlocked pages, if you want to
189 * remove all the pages of one inode, you must call truncate_inode_pages.
191 * invalidate_mapping_pages() will not block on IO activity. It will not
192 * invalidate pages which are dirty, locked, under writeback or mapped into
193 * pagetables.
195 unsigned long invalidate_mapping_pages(struct address_space *mapping,
196 pgoff_t start, pgoff_t end)
198 struct pagevec pvec;
199 pgoff_t next = start;
200 unsigned long ret = 0;
201 int i;
203 pagevec_init(&pvec, 0);
204 while (next <= end &&
205 pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
206 for (i = 0; i < pagevec_count(&pvec); i++) {
207 struct page *page = pvec.pages[i];
209 if (TestSetPageLocked(page)) {
210 next++;
211 continue;
213 if (page->index > next)
214 next = page->index;
215 next++;
216 if (PageDirty(page) || PageWriteback(page))
217 goto unlock;
218 if (page_mapped(page))
219 goto unlock;
220 ret += invalidate_complete_page(mapping, page);
221 unlock:
222 unlock_page(page);
223 if (next > end)
224 break;
226 pagevec_release(&pvec);
227 cond_resched();
229 return ret;
232 unsigned long invalidate_inode_pages(struct address_space *mapping)
234 return invalidate_mapping_pages(mapping, 0, ~0UL);
237 EXPORT_SYMBOL(invalidate_inode_pages);
240 * invalidate_inode_pages2 - remove all unmapped pages from an address_space
241 * @mapping - the address_space
243 * invalidate_inode_pages2() is like truncate_inode_pages(), except for the case
244 * where the page is seen to be mapped into process pagetables. In that case,
245 * the page is marked clean but is left attached to its address_space.
247 * The page is also marked not uptodate so that a subsequent pagefault will
248 * perform I/O to bringthe page's contents back into sync with its backing
249 * store.
251 * FIXME: invalidate_inode_pages2() is probably trivially livelockable.
253 void invalidate_inode_pages2(struct address_space *mapping)
255 struct pagevec pvec;
256 pgoff_t next = 0;
257 int i;
259 pagevec_init(&pvec, 0);
260 while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
261 for (i = 0; i < pagevec_count(&pvec); i++) {
262 struct page *page = pvec.pages[i];
264 lock_page(page);
265 if (page->mapping == mapping) { /* truncate race? */
266 wait_on_page_writeback(page);
267 next = page->index + 1;
268 if (page_mapped(page)) {
269 clear_page_dirty(page);
270 ClearPageUptodate(page);
271 } else {
272 invalidate_complete_page(mapping, page);
275 unlock_page(page);
277 pagevec_release(&pvec);
278 cond_resched();
282 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);