mm: factor out checks and accounting from __delete_from_page_cache()
[linux-2.6-block.git] / mm / truncate.c
CommitLineData
1da177e4
LT
1/*
2 * mm/truncate.c - code for taking down pages from address_spaces
3 *
4 * Copyright (C) 2002, Linus Torvalds
5 *
e1f8e874 6 * 10Sep2002 Andrew Morton
1da177e4
LT
7 * Initial version.
8 */
9
10#include <linux/kernel.h>
4af3c9cc 11#include <linux/backing-dev.h>
f9fe48be 12#include <linux/dax.h>
5a0e3ad6 13#include <linux/gfp.h>
1da177e4 14#include <linux/mm.h>
0fd0e6b0 15#include <linux/swap.h>
b95f1b31 16#include <linux/export.h>
1da177e4 17#include <linux/pagemap.h>
01f2705d 18#include <linux/highmem.h>
1da177e4 19#include <linux/pagevec.h>
e08748ce 20#include <linux/task_io_accounting_ops.h>
1da177e4 21#include <linux/buffer_head.h> /* grr. try_to_release_page,
aaa4059b 22 do_invalidatepage */
3a4f8a0b 23#include <linux/shmem_fs.h>
c515e1fd 24#include <linux/cleancache.h>
90a80202 25#include <linux/rmap.h>
ba470de4 26#include "internal.h"
1da177e4 27
c6dcf52c
JK
28static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
29 void *entry)
0cd6144a 30{
449dd698
JW
31 struct radix_tree_node *node;
32 void **slot;
33
ac401cc7
JK
34 spin_lock_irq(&mapping->tree_lock);
35 /*
36 * Regular page slots are stabilized by the page lock even
37 * without the tree itself locked. These unlocked entries
38 * need verification under the tree lock.
39 */
14b46879 40 if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
ac401cc7
JK
41 goto unlock;
42 if (*slot != entry)
43 goto unlock;
14b46879
JW
44 __radix_tree_replace(&mapping->page_tree, node, slot, NULL,
45 workingset_update_node, mapping);
ac401cc7 46 mapping->nrexceptional--;
449dd698 47unlock:
0cd6144a
JW
48 spin_unlock_irq(&mapping->tree_lock);
49}
1da177e4 50
c6dcf52c
JK
51/*
52 * Unconditionally remove exceptional entry. Usually called from truncate path.
53 */
54static void truncate_exceptional_entry(struct address_space *mapping,
55 pgoff_t index, void *entry)
56{
57 /* Handled by shmem itself */
58 if (shmem_mapping(mapping))
59 return;
60
61 if (dax_mapping(mapping)) {
62 dax_delete_mapping_entry(mapping, index);
63 return;
64 }
65 clear_shadow_entry(mapping, index, entry);
66}
67
68/*
69 * Invalidate exceptional entry if easily possible. This handles exceptional
4636e70b 70 * entries for invalidate_inode_pages().
c6dcf52c
JK
71 */
72static int invalidate_exceptional_entry(struct address_space *mapping,
73 pgoff_t index, void *entry)
74{
4636e70b
RZ
75 /* Handled by shmem itself, or for DAX we do nothing. */
76 if (shmem_mapping(mapping) || dax_mapping(mapping))
c6dcf52c 77 return 1;
c6dcf52c
JK
78 clear_shadow_entry(mapping, index, entry);
79 return 1;
80}
81
82/*
83 * Invalidate exceptional entry if clean. This handles exceptional entries for
84 * invalidate_inode_pages2() so for DAX it evicts only clean entries.
85 */
86static int invalidate_exceptional_entry2(struct address_space *mapping,
87 pgoff_t index, void *entry)
88{
89 /* Handled by shmem itself */
90 if (shmem_mapping(mapping))
91 return 1;
92 if (dax_mapping(mapping))
93 return dax_invalidate_mapping_entry_sync(mapping, index);
94 clear_shadow_entry(mapping, index, entry);
95 return 1;
96}
97
cf9a2ae8 98/**
28bc44d7 99 * do_invalidatepage - invalidate part or all of a page
cf9a2ae8 100 * @page: the page which is affected
d47992f8
LC
101 * @offset: start of the range to invalidate
102 * @length: length of the range to invalidate
cf9a2ae8
DH
103 *
104 * do_invalidatepage() is called when all or part of the page has become
105 * invalidated by a truncate operation.
106 *
107 * do_invalidatepage() does not have to release all buffers, but it must
108 * ensure that no dirty buffer is left outside @offset and that no I/O
109 * is underway against any of the blocks which are outside the truncation
110 * point. Because the caller is about to free (and possibly reuse) those
111 * blocks on-disk.
112 */
d47992f8
LC
113void do_invalidatepage(struct page *page, unsigned int offset,
114 unsigned int length)
cf9a2ae8 115{
d47992f8
LC
116 void (*invalidatepage)(struct page *, unsigned int, unsigned int);
117
cf9a2ae8 118 invalidatepage = page->mapping->a_ops->invalidatepage;
9361401e 119#ifdef CONFIG_BLOCK
cf9a2ae8
DH
120 if (!invalidatepage)
121 invalidatepage = block_invalidatepage;
9361401e 122#endif
cf9a2ae8 123 if (invalidatepage)
d47992f8 124 (*invalidatepage)(page, offset, length);
cf9a2ae8
DH
125}
126
1da177e4
LT
127/*
128 * If truncate cannot remove the fs-private metadata from the page, the page
62e1c553 129 * becomes orphaned. It will be left on the LRU and may even be mapped into
54cb8821 130 * user pagetables if we're racing with filemap_fault().
1da177e4
LT
131 *
132 * We need to bale out if page->mapping is no longer equal to the original
133 * mapping. This happens a) when the VM reclaimed the page while we waited on
fc0ecff6 134 * its lock, b) when a concurrent invalidate_mapping_pages got there first and
1da177e4
LT
135 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
136 */
9f4e41f4
JK
137static void
138truncate_cleanup_page(struct address_space *mapping, struct page *page)
1da177e4 139{
9f4e41f4
JK
140 if (page_mapped(page)) {
141 loff_t holelen;
142
143 holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE;
144 unmap_mapping_range(mapping,
145 (loff_t)page->index << PAGE_SHIFT,
146 holelen, 0);
147 }
1da177e4 148
266cf658 149 if (page_has_private(page))
09cbfeaf 150 do_invalidatepage(page, 0, PAGE_SIZE);
1da177e4 151
b9ea2515
KK
152 /*
153 * Some filesystems seem to re-dirty the page even after
154 * the VM has canceled the dirty bit (eg ext3 journaling).
155 * Hence dirty accounting check is placed after invalidation.
156 */
11f81bec 157 cancel_dirty_page(page);
1da177e4 158 ClearPageMappedToDisk(page);
1da177e4
LT
159}
160
161/*
fc0ecff6 162 * This is for invalidate_mapping_pages(). That function can be called at
1da177e4 163 * any time, and is not supposed to throw away dirty pages. But pages can
0fd0e6b0
NP
164 * be marked dirty at any time too, so use remove_mapping which safely
165 * discards clean, unused pages.
1da177e4
LT
166 *
167 * Returns non-zero if the page was successfully invalidated.
168 */
169static int
170invalidate_complete_page(struct address_space *mapping, struct page *page)
171{
0fd0e6b0
NP
172 int ret;
173
1da177e4
LT
174 if (page->mapping != mapping)
175 return 0;
176
266cf658 177 if (page_has_private(page) && !try_to_release_page(page, 0))
1da177e4
LT
178 return 0;
179
0fd0e6b0 180 ret = remove_mapping(mapping, page);
0fd0e6b0
NP
181
182 return ret;
1da177e4
LT
183}
184
750b4987
NP
185int truncate_inode_page(struct address_space *mapping, struct page *page)
186{
fc127da0
KS
187 VM_BUG_ON_PAGE(PageTail(page), page);
188
9f4e41f4
JK
189 if (page->mapping != mapping)
190 return -EIO;
191
192 truncate_cleanup_page(mapping, page);
193 delete_from_page_cache(page);
194 return 0;
750b4987
NP
195}
196
25718736
AK
197/*
198 * Used to get rid of pages on hardware memory corruption.
199 */
200int generic_error_remove_page(struct address_space *mapping, struct page *page)
201{
202 if (!mapping)
203 return -EINVAL;
204 /*
205 * Only punch for normal data pages for now.
206 * Handling other types like directories would need more auditing.
207 */
208 if (!S_ISREG(mapping->host->i_mode))
209 return -EIO;
210 return truncate_inode_page(mapping, page);
211}
212EXPORT_SYMBOL(generic_error_remove_page);
213
83f78668
WF
214/*
215 * Safely invalidate one page from its pagecache mapping.
216 * It only drops clean, unused pages. The page must be locked.
217 *
218 * Returns 1 if the page is successfully invalidated, otherwise 0.
219 */
220int invalidate_inode_page(struct page *page)
221{
222 struct address_space *mapping = page_mapping(page);
223 if (!mapping)
224 return 0;
225 if (PageDirty(page) || PageWriteback(page))
226 return 0;
227 if (page_mapped(page))
228 return 0;
229 return invalidate_complete_page(mapping, page);
230}
231
1da177e4 232/**
73c1e204 233 * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
1da177e4
LT
234 * @mapping: mapping to truncate
235 * @lstart: offset from which to truncate
5a720394 236 * @lend: offset to which to truncate (inclusive)
1da177e4 237 *
d7339071 238 * Truncate the page cache, removing the pages that are between
5a720394
LC
239 * specified offsets (and zeroing out partial pages
240 * if lstart or lend + 1 is not page aligned).
1da177e4
LT
241 *
242 * Truncate takes two passes - the first pass is nonblocking. It will not
243 * block on page locks and it will not block on writeback. The second pass
244 * will wait. This is to prevent as much IO as possible in the affected region.
245 * The first pass will remove most pages, so the search cost of the second pass
246 * is low.
247 *
1da177e4
LT
248 * We pass down the cache-hot hint to the page freeing code. Even if the
249 * mapping is large, it is probably the case that the final pages are the most
250 * recently touched, and freeing happens in ascending file offset order.
5a720394
LC
251 *
252 * Note that since ->invalidatepage() accepts range to invalidate
253 * truncate_inode_pages_range is able to handle cases where lend + 1 is not
254 * page aligned properly.
1da177e4 255 */
d7339071
HR
256void truncate_inode_pages_range(struct address_space *mapping,
257 loff_t lstart, loff_t lend)
1da177e4 258{
5a720394
LC
259 pgoff_t start; /* inclusive */
260 pgoff_t end; /* exclusive */
261 unsigned int partial_start; /* inclusive */
262 unsigned int partial_end; /* exclusive */
263 struct pagevec pvec;
0cd6144a 264 pgoff_t indices[PAGEVEC_SIZE];
5a720394
LC
265 pgoff_t index;
266 int i;
1da177e4 267
f9fe48be 268 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
34ccb69e 269 goto out;
1da177e4 270
5a720394 271 /* Offsets within partial pages */
09cbfeaf
KS
272 partial_start = lstart & (PAGE_SIZE - 1);
273 partial_end = (lend + 1) & (PAGE_SIZE - 1);
5a720394
LC
274
275 /*
276 * 'start' and 'end' always covers the range of pages to be fully
277 * truncated. Partial pages are covered with 'partial_start' at the
278 * start of the range and 'partial_end' at the end of the range.
279 * Note that 'end' is exclusive while 'lend' is inclusive.
280 */
09cbfeaf 281 start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
5a720394
LC
282 if (lend == -1)
283 /*
284 * lend == -1 indicates end-of-file so we have to set 'end'
285 * to the highest possible pgoff_t and since the type is
286 * unsigned we're using -1.
287 */
288 end = -1;
289 else
09cbfeaf 290 end = (lend + 1) >> PAGE_SHIFT;
d7339071 291
1da177e4 292 pagevec_init(&pvec, 0);
b85e0eff 293 index = start;
0cd6144a
JW
294 while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
295 min(end - index, (pgoff_t)PAGEVEC_SIZE),
296 indices)) {
1da177e4
LT
297 for (i = 0; i < pagevec_count(&pvec); i++) {
298 struct page *page = pvec.pages[i];
1da177e4 299
b85e0eff 300 /* We rely upon deletion not changing page->index */
0cd6144a 301 index = indices[i];
5a720394 302 if (index >= end)
d7339071 303 break;
d7339071 304
0cd6144a 305 if (radix_tree_exceptional_entry(page)) {
c6dcf52c
JK
306 truncate_exceptional_entry(mapping, index,
307 page);
0cd6144a
JW
308 continue;
309 }
310
529ae9aa 311 if (!trylock_page(page))
1da177e4 312 continue;
5cbc198a 313 WARN_ON(page_to_index(page) != index);
1da177e4
LT
314 if (PageWriteback(page)) {
315 unlock_page(page);
316 continue;
317 }
750b4987 318 truncate_inode_page(mapping, page);
1da177e4
LT
319 unlock_page(page);
320 }
0cd6144a 321 pagevec_remove_exceptionals(&pvec);
1da177e4
LT
322 pagevec_release(&pvec);
323 cond_resched();
b85e0eff 324 index++;
1da177e4
LT
325 }
326
5a720394 327 if (partial_start) {
1da177e4
LT
328 struct page *page = find_lock_page(mapping, start - 1);
329 if (page) {
09cbfeaf 330 unsigned int top = PAGE_SIZE;
5a720394
LC
331 if (start > end) {
332 /* Truncation within a single page */
333 top = partial_end;
334 partial_end = 0;
335 }
1da177e4 336 wait_on_page_writeback(page);
5a720394
LC
337 zero_user_segment(page, partial_start, top);
338 cleancache_invalidate_page(mapping, page);
339 if (page_has_private(page))
340 do_invalidatepage(page, partial_start,
341 top - partial_start);
1da177e4 342 unlock_page(page);
09cbfeaf 343 put_page(page);
1da177e4
LT
344 }
345 }
5a720394
LC
346 if (partial_end) {
347 struct page *page = find_lock_page(mapping, end);
348 if (page) {
349 wait_on_page_writeback(page);
350 zero_user_segment(page, 0, partial_end);
351 cleancache_invalidate_page(mapping, page);
352 if (page_has_private(page))
353 do_invalidatepage(page, 0,
354 partial_end);
355 unlock_page(page);
09cbfeaf 356 put_page(page);
5a720394
LC
357 }
358 }
359 /*
360 * If the truncation happened within a single page no pages
361 * will be released, just zeroed, so we can bail out now.
362 */
363 if (start >= end)
34ccb69e 364 goto out;
1da177e4 365
b85e0eff 366 index = start;
1da177e4
LT
367 for ( ; ; ) {
368 cond_resched();
0cd6144a 369 if (!pagevec_lookup_entries(&pvec, mapping, index,
792ceaef
HD
370 min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
371 /* If all gone from start onwards, we're done */
b85e0eff 372 if (index == start)
1da177e4 373 break;
792ceaef 374 /* Otherwise restart to make sure all gone */
b85e0eff 375 index = start;
1da177e4
LT
376 continue;
377 }
0cd6144a 378 if (index == start && indices[0] >= end) {
792ceaef 379 /* All gone out of hole to be punched, we're done */
0cd6144a 380 pagevec_remove_exceptionals(&pvec);
d7339071
HR
381 pagevec_release(&pvec);
382 break;
383 }
1da177e4
LT
384 for (i = 0; i < pagevec_count(&pvec); i++) {
385 struct page *page = pvec.pages[i];
386
b85e0eff 387 /* We rely upon deletion not changing page->index */
0cd6144a 388 index = indices[i];
792ceaef
HD
389 if (index >= end) {
390 /* Restart punch to make sure all gone */
391 index = start - 1;
d7339071 392 break;
792ceaef 393 }
b85e0eff 394
0cd6144a 395 if (radix_tree_exceptional_entry(page)) {
c6dcf52c
JK
396 truncate_exceptional_entry(mapping, index,
397 page);
0cd6144a
JW
398 continue;
399 }
400
1da177e4 401 lock_page(page);
5cbc198a 402 WARN_ON(page_to_index(page) != index);
1da177e4 403 wait_on_page_writeback(page);
750b4987 404 truncate_inode_page(mapping, page);
1da177e4
LT
405 unlock_page(page);
406 }
0cd6144a 407 pagevec_remove_exceptionals(&pvec);
1da177e4 408 pagevec_release(&pvec);
b85e0eff 409 index++;
1da177e4 410 }
34ccb69e
AR
411
412out:
3167760f 413 cleancache_invalidate_inode(mapping);
1da177e4 414}
d7339071 415EXPORT_SYMBOL(truncate_inode_pages_range);
1da177e4 416
d7339071
HR
417/**
418 * truncate_inode_pages - truncate *all* the pages from an offset
419 * @mapping: mapping to truncate
420 * @lstart: offset from which to truncate
421 *
1b1dcc1b 422 * Called under (and serialised by) inode->i_mutex.
08142579
JK
423 *
424 * Note: When this function returns, there can be a page in the process of
425 * deletion (inside __delete_from_page_cache()) in the specified range. Thus
426 * mapping->nrpages can be non-zero when this function returns even after
427 * truncation of the whole mapping.
d7339071
HR
428 */
429void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
430{
431 truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
432}
1da177e4
LT
433EXPORT_SYMBOL(truncate_inode_pages);
434
91b0abe3
JW
435/**
436 * truncate_inode_pages_final - truncate *all* pages before inode dies
437 * @mapping: mapping to truncate
438 *
439 * Called under (and serialized by) inode->i_mutex.
440 *
441 * Filesystems have to use this in the .evict_inode path to inform the
442 * VM that this is the final truncate and the inode is going away.
443 */
444void truncate_inode_pages_final(struct address_space *mapping)
445{
f9fe48be 446 unsigned long nrexceptional;
91b0abe3
JW
447 unsigned long nrpages;
448
449 /*
450 * Page reclaim can not participate in regular inode lifetime
451 * management (can't call iput()) and thus can race with the
452 * inode teardown. Tell it when the address space is exiting,
453 * so that it does not install eviction information after the
454 * final truncate has begun.
455 */
456 mapping_set_exiting(mapping);
457
458 /*
459 * When reclaim installs eviction entries, it increases
f9fe48be 460 * nrexceptional first, then decreases nrpages. Make sure we see
91b0abe3
JW
461 * this in the right order or we might miss an entry.
462 */
463 nrpages = mapping->nrpages;
464 smp_rmb();
f9fe48be 465 nrexceptional = mapping->nrexceptional;
91b0abe3 466
f9fe48be 467 if (nrpages || nrexceptional) {
91b0abe3
JW
468 /*
469 * As truncation uses a lockless tree lookup, cycle
470 * the tree lock to make sure any ongoing tree
471 * modification that does not see AS_EXITING is
472 * completed before starting the final truncate.
473 */
474 spin_lock_irq(&mapping->tree_lock);
475 spin_unlock_irq(&mapping->tree_lock);
476
477 truncate_inode_pages(mapping, 0);
478 }
479}
480EXPORT_SYMBOL(truncate_inode_pages_final);
481
28697355
MW
482/**
483 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
484 * @mapping: the address_space which holds the pages to invalidate
485 * @start: the offset 'from' which to invalidate
486 * @end: the offset 'to' which to invalidate (inclusive)
487 *
488 * This function only removes the unlocked pages, if you want to
489 * remove all the pages of one inode, you must call truncate_inode_pages.
490 *
491 * invalidate_mapping_pages() will not block on IO activity. It will not
492 * invalidate pages which are dirty, locked, under writeback or mapped into
493 * pagetables.
494 */
495unsigned long invalidate_mapping_pages(struct address_space *mapping,
31560180 496 pgoff_t start, pgoff_t end)
1da177e4 497{
0cd6144a 498 pgoff_t indices[PAGEVEC_SIZE];
1da177e4 499 struct pagevec pvec;
b85e0eff 500 pgoff_t index = start;
31560180
MK
501 unsigned long ret;
502 unsigned long count = 0;
1da177e4
LT
503 int i;
504
505 pagevec_init(&pvec, 0);
0cd6144a
JW
506 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
507 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
508 indices)) {
1da177e4
LT
509 for (i = 0; i < pagevec_count(&pvec); i++) {
510 struct page *page = pvec.pages[i];
e0f23603 511
b85e0eff 512 /* We rely upon deletion not changing page->index */
0cd6144a 513 index = indices[i];
b85e0eff
HD
514 if (index > end)
515 break;
e0f23603 516
0cd6144a 517 if (radix_tree_exceptional_entry(page)) {
c6dcf52c
JK
518 invalidate_exceptional_entry(mapping, index,
519 page);
0cd6144a
JW
520 continue;
521 }
522
b85e0eff
HD
523 if (!trylock_page(page))
524 continue;
fc127da0 525
5cbc198a 526 WARN_ON(page_to_index(page) != index);
fc127da0
KS
527
528 /* Middle of THP: skip */
529 if (PageTransTail(page)) {
530 unlock_page(page);
531 continue;
532 } else if (PageTransHuge(page)) {
533 index += HPAGE_PMD_NR - 1;
534 i += HPAGE_PMD_NR - 1;
76b6f9b7
JK
535 /*
536 * 'end' is in the middle of THP. Don't
537 * invalidate the page as the part outside of
538 * 'end' could be still useful.
539 */
540 if (index > end) {
541 unlock_page(page);
fc127da0 542 continue;
76b6f9b7 543 }
fc127da0
KS
544 }
545
31560180 546 ret = invalidate_inode_page(page);
1da177e4 547 unlock_page(page);
31560180
MK
548 /*
549 * Invalidation is a hint that the page is no longer
550 * of interest and try to speed up its reclaim.
551 */
552 if (!ret)
cc5993bd 553 deactivate_file_page(page);
31560180 554 count += ret;
1da177e4 555 }
0cd6144a 556 pagevec_remove_exceptionals(&pvec);
1da177e4 557 pagevec_release(&pvec);
28697355 558 cond_resched();
b85e0eff 559 index++;
1da177e4 560 }
31560180 561 return count;
1da177e4 562}
54bc4855 563EXPORT_SYMBOL(invalidate_mapping_pages);
1da177e4 564
bd4c8ce4
AM
565/*
566 * This is like invalidate_complete_page(), except it ignores the page's
567 * refcount. We do this because invalidate_inode_pages2() needs stronger
568 * invalidation guarantees, and cannot afford to leave pages behind because
2706a1b8
AB
569 * shrink_page_list() has a temp ref on them, or because they're transiently
570 * sitting in the lru_cache_add() pagevecs.
bd4c8ce4
AM
571 */
572static int
573invalidate_complete_page2(struct address_space *mapping, struct page *page)
574{
c4843a75
GT
575 unsigned long flags;
576
bd4c8ce4
AM
577 if (page->mapping != mapping)
578 return 0;
579
266cf658 580 if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
bd4c8ce4
AM
581 return 0;
582
c4843a75 583 spin_lock_irqsave(&mapping->tree_lock, flags);
bd4c8ce4
AM
584 if (PageDirty(page))
585 goto failed;
586
266cf658 587 BUG_ON(page_has_private(page));
62cccb8c 588 __delete_from_page_cache(page, NULL);
c4843a75 589 spin_unlock_irqrestore(&mapping->tree_lock, flags);
6072d13c
LT
590
591 if (mapping->a_ops->freepage)
592 mapping->a_ops->freepage(page);
593
09cbfeaf 594 put_page(page); /* pagecache ref */
bd4c8ce4
AM
595 return 1;
596failed:
c4843a75 597 spin_unlock_irqrestore(&mapping->tree_lock, flags);
bd4c8ce4
AM
598 return 0;
599}
600
e3db7691
TM
601static int do_launder_page(struct address_space *mapping, struct page *page)
602{
603 if (!PageDirty(page))
604 return 0;
605 if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
606 return 0;
607 return mapping->a_ops->launder_page(page);
608}
609
1da177e4
LT
610/**
611 * invalidate_inode_pages2_range - remove range of pages from an address_space
67be2dd1 612 * @mapping: the address_space
1da177e4
LT
613 * @start: the page offset 'from' which to invalidate
614 * @end: the page offset 'to' which to invalidate (inclusive)
615 *
616 * Any pages which are found to be mapped into pagetables are unmapped prior to
617 * invalidation.
618 *
6ccfa806 619 * Returns -EBUSY if any pages could not be invalidated.
1da177e4
LT
620 */
621int invalidate_inode_pages2_range(struct address_space *mapping,
622 pgoff_t start, pgoff_t end)
623{
0cd6144a 624 pgoff_t indices[PAGEVEC_SIZE];
1da177e4 625 struct pagevec pvec;
b85e0eff 626 pgoff_t index;
1da177e4
LT
627 int i;
628 int ret = 0;
0dd1334f 629 int ret2 = 0;
1da177e4 630 int did_range_unmap = 0;
1da177e4 631
32691f0f 632 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
34ccb69e 633 goto out;
32691f0f 634
1da177e4 635 pagevec_init(&pvec, 0);
b85e0eff 636 index = start;
0cd6144a
JW
637 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
638 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
639 indices)) {
7b965e08 640 for (i = 0; i < pagevec_count(&pvec); i++) {
1da177e4 641 struct page *page = pvec.pages[i];
b85e0eff
HD
642
643 /* We rely upon deletion not changing page->index */
0cd6144a 644 index = indices[i];
b85e0eff
HD
645 if (index > end)
646 break;
1da177e4 647
0cd6144a 648 if (radix_tree_exceptional_entry(page)) {
c6dcf52c
JK
649 if (!invalidate_exceptional_entry2(mapping,
650 index, page))
651 ret = -EBUSY;
0cd6144a
JW
652 continue;
653 }
654
1da177e4 655 lock_page(page);
5cbc198a 656 WARN_ON(page_to_index(page) != index);
1da177e4
LT
657 if (page->mapping != mapping) {
658 unlock_page(page);
659 continue;
660 }
1da177e4 661 wait_on_page_writeback(page);
d00806b1 662 if (page_mapped(page)) {
1da177e4
LT
663 if (!did_range_unmap) {
664 /*
665 * Zap the rest of the file in one hit.
666 */
667 unmap_mapping_range(mapping,
09cbfeaf 668 (loff_t)index << PAGE_SHIFT,
b85e0eff 669 (loff_t)(1 + end - index)
09cbfeaf
KS
670 << PAGE_SHIFT,
671 0);
1da177e4
LT
672 did_range_unmap = 1;
673 } else {
674 /*
675 * Just zap this page
676 */
677 unmap_mapping_range(mapping,
09cbfeaf
KS
678 (loff_t)index << PAGE_SHIFT,
679 PAGE_SIZE, 0);
1da177e4
LT
680 }
681 }
d00806b1 682 BUG_ON(page_mapped(page));
0dd1334f
HH
683 ret2 = do_launder_page(mapping, page);
684 if (ret2 == 0) {
685 if (!invalidate_complete_page2(mapping, page))
6ccfa806 686 ret2 = -EBUSY;
0dd1334f
HH
687 }
688 if (ret2 < 0)
689 ret = ret2;
1da177e4
LT
690 unlock_page(page);
691 }
0cd6144a 692 pagevec_remove_exceptionals(&pvec);
1da177e4
LT
693 pagevec_release(&pvec);
694 cond_resched();
b85e0eff 695 index++;
1da177e4 696 }
cd656375
JK
697 /*
698 * For DAX we invalidate page tables after invalidating radix tree. We
699 * could invalidate page tables while invalidating each entry however
700 * that would be expensive. And doing range unmapping before doesn't
701 * work as we have no cheap way to find whether radix tree entry didn't
702 * get remapped later.
703 */
704 if (dax_mapping(mapping)) {
705 unmap_mapping_range(mapping, (loff_t)start << PAGE_SHIFT,
706 (loff_t)(end - start + 1) << PAGE_SHIFT, 0);
707 }
34ccb69e 708out:
3167760f 709 cleancache_invalidate_inode(mapping);
1da177e4
LT
710 return ret;
711}
712EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
713
714/**
715 * invalidate_inode_pages2 - remove all pages from an address_space
67be2dd1 716 * @mapping: the address_space
1da177e4
LT
717 *
718 * Any pages which are found to be mapped into pagetables are unmapped prior to
719 * invalidation.
720 *
e9de25dd 721 * Returns -EBUSY if any pages could not be invalidated.
1da177e4
LT
722 */
723int invalidate_inode_pages2(struct address_space *mapping)
724{
725 return invalidate_inode_pages2_range(mapping, 0, -1);
726}
727EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
25d9e2d1 728
729/**
730 * truncate_pagecache - unmap and remove pagecache that has been truncated
731 * @inode: inode
8a549bea 732 * @newsize: new file size
25d9e2d1 733 *
734 * inode's new i_size must already be written before truncate_pagecache
735 * is called.
736 *
737 * This function should typically be called before the filesystem
738 * releases resources associated with the freed range (eg. deallocates
739 * blocks). This way, pagecache will always stay logically coherent
740 * with on-disk format, and the filesystem would not have to deal with
741 * situations such as writepage being called for a page that has already
742 * had its underlying blocks deallocated.
743 */
7caef267 744void truncate_pagecache(struct inode *inode, loff_t newsize)
25d9e2d1 745{
cedabed4 746 struct address_space *mapping = inode->i_mapping;
8a549bea 747 loff_t holebegin = round_up(newsize, PAGE_SIZE);
cedabed4
OH
748
749 /*
750 * unmap_mapping_range is called twice, first simply for
751 * efficiency so that truncate_inode_pages does fewer
752 * single-page unmaps. However after this first call, and
753 * before truncate_inode_pages finishes, it is possible for
754 * private pages to be COWed, which remain after
755 * truncate_inode_pages finishes, hence the second
756 * unmap_mapping_range call must be made for correctness.
757 */
8a549bea
HD
758 unmap_mapping_range(mapping, holebegin, 0, 1);
759 truncate_inode_pages(mapping, newsize);
760 unmap_mapping_range(mapping, holebegin, 0, 1);
25d9e2d1 761}
762EXPORT_SYMBOL(truncate_pagecache);
763
2c27c65e
CH
764/**
765 * truncate_setsize - update inode and pagecache for a new file size
766 * @inode: inode
767 * @newsize: new file size
768 *
382e27da
JK
769 * truncate_setsize updates i_size and performs pagecache truncation (if
770 * necessary) to @newsize. It will be typically be called from the filesystem's
771 * setattr function when ATTR_SIZE is passed in.
2c27c65e 772 *
77783d06
JK
773 * Must be called with a lock serializing truncates and writes (generally
774 * i_mutex but e.g. xfs uses a different lock) and before all filesystem
775 * specific block truncation has been performed.
2c27c65e
CH
776 */
777void truncate_setsize(struct inode *inode, loff_t newsize)
778{
90a80202
JK
779 loff_t oldsize = inode->i_size;
780
2c27c65e 781 i_size_write(inode, newsize);
90a80202
JK
782 if (newsize > oldsize)
783 pagecache_isize_extended(inode, oldsize, newsize);
7caef267 784 truncate_pagecache(inode, newsize);
2c27c65e
CH
785}
786EXPORT_SYMBOL(truncate_setsize);
787
90a80202
JK
788/**
789 * pagecache_isize_extended - update pagecache after extension of i_size
790 * @inode: inode for which i_size was extended
791 * @from: original inode size
792 * @to: new inode size
793 *
794 * Handle extension of inode size either caused by extending truncate or by
795 * write starting after current i_size. We mark the page straddling current
796 * i_size RO so that page_mkwrite() is called on the nearest write access to
797 * the page. This way filesystem can be sure that page_mkwrite() is called on
798 * the page before user writes to the page via mmap after the i_size has been
799 * changed.
800 *
801 * The function must be called after i_size is updated so that page fault
802 * coming after we unlock the page will already see the new i_size.
803 * The function must be called while we still hold i_mutex - this not only
804 * makes sure i_size is stable but also that userspace cannot observe new
805 * i_size value before we are prepared to store mmap writes at new inode size.
806 */
807void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
808{
93407472 809 int bsize = i_blocksize(inode);
90a80202
JK
810 loff_t rounded_from;
811 struct page *page;
812 pgoff_t index;
813
90a80202
JK
814 WARN_ON(to > inode->i_size);
815
09cbfeaf 816 if (from >= to || bsize == PAGE_SIZE)
90a80202
JK
817 return;
818 /* Page straddling @from will not have any hole block created? */
819 rounded_from = round_up(from, bsize);
09cbfeaf 820 if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
90a80202
JK
821 return;
822
09cbfeaf 823 index = from >> PAGE_SHIFT;
90a80202
JK
824 page = find_lock_page(inode->i_mapping, index);
825 /* Page not cached? Nothing to do */
826 if (!page)
827 return;
828 /*
829 * See clear_page_dirty_for_io() for details why set_page_dirty()
830 * is needed.
831 */
832 if (page_mkclean(page))
833 set_page_dirty(page);
834 unlock_page(page);
09cbfeaf 835 put_page(page);
90a80202
JK
836}
837EXPORT_SYMBOL(pagecache_isize_extended);
838
623e3db9
HD
839/**
840 * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
841 * @inode: inode
842 * @lstart: offset of beginning of hole
843 * @lend: offset of last byte of hole
844 *
845 * This function should typically be called before the filesystem
846 * releases resources associated with the freed range (eg. deallocates
847 * blocks). This way, pagecache will always stay logically coherent
848 * with on-disk format, and the filesystem would not have to deal with
849 * situations such as writepage being called for a page that has already
850 * had its underlying blocks deallocated.
851 */
852void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
853{
854 struct address_space *mapping = inode->i_mapping;
855 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
856 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
857 /*
858 * This rounding is currently just for example: unmap_mapping_range
859 * expands its hole outwards, whereas we want it to contract the hole
860 * inwards. However, existing callers of truncate_pagecache_range are
5a720394
LC
861 * doing their own page rounding first. Note that unmap_mapping_range
862 * allows holelen 0 for all, and we allow lend -1 for end of file.
623e3db9
HD
863 */
864
865 /*
866 * Unlike in truncate_pagecache, unmap_mapping_range is called only
867 * once (before truncating pagecache), and without "even_cows" flag:
868 * hole-punching should not remove private COWed pages from the hole.
869 */
870 if ((u64)unmap_end > (u64)unmap_start)
871 unmap_mapping_range(mapping, unmap_start,
872 1 + unmap_end - unmap_start, 0);
873 truncate_inode_pages_range(mapping, lstart, lend);
874}
875EXPORT_SYMBOL(truncate_pagecache_range);