This commit was generated by cvs2svn to compensate for changes in r655,
[linux-2.6.git] / mm / truncate.c
1 /*
2  * mm/truncate.c - code for taking down pages from address_spaces
3  *
4  * Copyright (C) 2002, Linus Torvalds
5  *
6  * 10Sep2002    akpm@zip.com.au
7  *              Initial version.
8  */
9
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 */
17
18
19 static int do_invalidatepage(struct page *page, unsigned long offset)
20 {
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);
26 }
27
28 static inline void truncate_partial_page(struct page *page, unsigned partial)
29 {
30         memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial);
31         if (PagePrivate(page))
32                 do_invalidatepage(page, partial);
33 }
34
35 /*
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().
39  *
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.
44  */
45 static void
46 truncate_complete_page(struct address_space *mapping, struct page *page)
47 {
48         if (page->mapping != mapping)
49                 return;
50
51         if (PagePrivate(page))
52                 do_invalidatepage(page, 0);
53
54         clear_page_dirty(page);
55         ClearPageUptodate(page);
56         ClearPageMappedToDisk(page);
57         remove_from_page_cache(page);
58         page_cache_release(page);       /* pagecache ref */
59 }
60
61 /*
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.
67  */
68 static int
69 invalidate_complete_page(struct address_space *mapping, struct page *page)
70 {
71         if (page->mapping != mapping)
72                 return 0;
73
74         if (PagePrivate(page) && !try_to_release_page(page, 0))
75                 return 0;
76
77         spin_lock_irq(&mapping->tree_lock);
78         if (PageDirty(page)) {
79                 spin_unlock_irq(&mapping->tree_lock);
80                 return 0;
81         }
82
83         BUG_ON(PagePrivate(page));
84         if (page_count(page) != 2) {
85                 spin_unlock_irq(&mapping->tree_lock);
86                 return 0;
87         }
88         __remove_from_page_cache(page);
89         spin_unlock_irq(&mapping->tree_lock);
90         ClearPageUptodate(page);
91         page_cache_release(page);       /* pagecache ref */
92         return 1;
93 }
94
95 /**
96  * truncate_inode_pages - truncate *all* the pages from an offset
97  * @mapping: mapping to truncate
98  * @lstart: offset from which to truncate
99  *
100  * Truncate the page cache at a set offset, removing the pages that are beyond
101  * that offset (and zeroing out partial pages).
102  *
103  * Truncate takes two passes - the first pass is nonblocking.  It will not
104  * block on page locks and it will not block on writeback.  The second pass
105  * will wait.  This is to prevent as much IO as possible in the affected region.
106  * The first pass will remove most pages, so the search cost of the second pass
107  * is low.
108  *
109  * When looking at page->index outside the page lock we need to be careful to
110  * copy it into a local to avoid races (it could change at any time).
111  *
112  * We pass down the cache-hot hint to the page freeing code.  Even if the
113  * mapping is large, it is probably the case that the final pages are the most
114  * recently touched, and freeing happens in ascending file offset order.
115  *
116  * Called under (and serialised by) inode->i_sem.
117  */
118 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
119 {
120         const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
121         const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
122         struct pagevec pvec;
123         pgoff_t next;
124         int i;
125
126         if (mapping->nrpages == 0)
127                 return;
128
129         pagevec_init(&pvec, 0);
130         next = start;
131         while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
132                 for (i = 0; i < pagevec_count(&pvec); i++) {
133                         struct page *page = pvec.pages[i];
134                         pgoff_t page_index = page->index;
135
136                         if (page_index > next)
137                                 next = page_index;
138                         next++;
139                         if (TestSetPageLocked(page))
140                                 continue;
141                         if (PageWriteback(page)) {
142                                 unlock_page(page);
143                                 continue;
144                         }
145                         truncate_complete_page(mapping, page);
146                         unlock_page(page);
147                 }
148                 pagevec_release(&pvec);
149                 cond_resched();
150         }
151
152         if (partial) {
153                 struct page *page = find_lock_page(mapping, start - 1);
154                 if (page) {
155                         wait_on_page_writeback(page);
156                         truncate_partial_page(page, partial);
157                         unlock_page(page);
158                         page_cache_release(page);
159                 }
160         }
161
162         next = start;
163         for ( ; ; ) {
164                 cond_resched();
165                 if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
166                         if (next == start)
167                                 break;
168                         next = start;
169                         continue;
170                 }
171                 for (i = 0; i < pagevec_count(&pvec); i++) {
172                         struct page *page = pvec.pages[i];
173
174                         lock_page(page);
175                         wait_on_page_writeback(page);
176                         if (page->index > next)
177                                 next = page->index;
178                         next++;
179                         truncate_complete_page(mapping, page);
180                         unlock_page(page);
181                 }
182                 pagevec_release(&pvec);
183         }
184 }
185
186 EXPORT_SYMBOL(truncate_inode_pages);
187
188 /**
189  * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
190  * @mapping: the address_space which holds the pages to invalidate
191  * @start: the offset 'from' which to invalidate
192  * @end: the offset 'to' which to invalidate (inclusive)
193  *
194  * This function only removes the unlocked pages, if you want to
195  * remove all the pages of one inode, you must call truncate_inode_pages.
196  *
197  * invalidate_mapping_pages() will not block on IO activity. It will not
198  * invalidate pages which are dirty, locked, under writeback or mapped into
199  * pagetables.
200  */
201 unsigned long invalidate_mapping_pages(struct address_space *mapping,
202                                 pgoff_t start, pgoff_t end)
203 {
204         struct pagevec pvec;
205         pgoff_t next = start;
206         unsigned long ret = 0;
207         int i;
208
209         pagevec_init(&pvec, 0);
210         while (next <= end &&
211                         pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
212                 for (i = 0; i < pagevec_count(&pvec); i++) {
213                         struct page *page = pvec.pages[i];
214
215                         if (TestSetPageLocked(page)) {
216                                 next++;
217                                 continue;
218                         }
219                         if (page->index > next)
220                                 next = page->index;
221                         next++;
222                         if (PageDirty(page) || PageWriteback(page))
223                                 goto unlock;
224                         if (page_mapped(page))
225                                 goto unlock;
226                         ret += invalidate_complete_page(mapping, page);
227 unlock:
228                         unlock_page(page);
229                         if (next > end)
230                                 break;
231                 }
232                 pagevec_release(&pvec);
233                 cond_resched();
234         }
235         return ret;
236 }
237
238 EXPORT_SYMBOL_GPL(invalidate_mapping_pages);
239
240 unsigned long invalidate_inode_pages(struct address_space *mapping)
241 {
242         return invalidate_mapping_pages(mapping, 0, ~0UL);
243 }
244
245 EXPORT_SYMBOL(invalidate_inode_pages);
246
247 /**
248  * invalidate_inode_pages2 - remove all unmapped pages from an address_space
249  * @mapping - the address_space
250  *
251  * invalidate_inode_pages2() is like truncate_inode_pages(), except for the case
252  * where the page is seen to be mapped into process pagetables.  In that case,
253  * the page is marked clean but is left attached to its address_space.
254  *
255  * The page is also marked not uptodate so that a subsequent pagefault will
256  * perform I/O to bringthe page's contents back into sync with its backing
257  * store.
258  *
259  * FIXME: invalidate_inode_pages2() is probably trivially livelockable.
260  */
261 void invalidate_inode_pages2(struct address_space *mapping)
262 {
263         struct pagevec pvec;
264         pgoff_t next = 0;
265         int i;
266
267         pagevec_init(&pvec, 0);
268         while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
269                 for (i = 0; i < pagevec_count(&pvec); i++) {
270                         struct page *page = pvec.pages[i];
271
272                         lock_page(page);
273                         if (page->mapping == mapping) { /* truncate race? */
274                                 wait_on_page_writeback(page);
275                                 next = page->index + 1;
276                                 if (page_mapped(page)) {
277                                         clear_page_dirty(page);
278                                         ClearPageUptodate(page);
279                                 } else {
280                                         if (!invalidate_complete_page(mapping,
281                                                                       page)) {
282                                                 clear_page_dirty(page);
283                                                 ClearPageUptodate(page);
284                                         }
285                                 }
286                         }
287                         unlock_page(page);
288                 }
289                 pagevec_release(&pvec);
290                 cond_resched();
291         }
292 }
293
294 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);