2 * mm/truncate.c - code for taking down pages from address_spaces
4 * Copyright (C) 2002, Linus Torvalds
6 * 10Sep2002 Andrew Morton
10 #include <linux/kernel.h>
11 #include <linux/backing-dev.h>
12 #include <linux/gfp.h>
14 #include <linux/swap.h>
15 #include <linux/export.h>
16 #include <linux/pagemap.h>
17 #include <linux/highmem.h>
18 #include <linux/pagevec.h>
19 #include <linux/task_io_accounting_ops.h>
20 #include <linux/buffer_head.h> /* grr. try_to_release_page,
22 #include <linux/cleancache.h>
23 #include <linux/rmap.h>
26 static void clear_exceptional_entry(struct address_space *mapping,
27 pgoff_t index, void *entry)
29 struct radix_tree_node *node;
32 /* Handled by shmem itself */
33 if (shmem_mapping(mapping))
36 spin_lock_irq(&mapping->tree_lock);
38 * Regular page slots are stabilized by the page lock even
39 * without the tree itself locked. These unlocked entries
40 * need verification under the tree lock.
42 if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
46 radix_tree_replace_slot(slot, NULL);
50 workingset_node_shadows_dec(node);
52 * Don't track node without shadow entries.
54 * Avoid acquiring the list_lru lock if already untracked.
55 * The list_empty() test is safe as node->private_list is
56 * protected by mapping->tree_lock.
58 if (!workingset_node_shadows(node) &&
59 !list_empty(&node->private_list)) {
60 local_lock(workingset_shadow_lock);
61 list_lru_del(&__workingset_shadow_nodes, &node->private_list);
62 local_unlock(workingset_shadow_lock);
64 __radix_tree_delete_node(&mapping->page_tree, node);
66 spin_unlock_irq(&mapping->tree_lock);
70 * do_invalidatepage - invalidate part or all of a page
71 * @page: the page which is affected
72 * @offset: start of the range to invalidate
73 * @length: length of the range to invalidate
75 * do_invalidatepage() is called when all or part of the page has become
76 * invalidated by a truncate operation.
78 * do_invalidatepage() does not have to release all buffers, but it must
79 * ensure that no dirty buffer is left outside @offset and that no I/O
80 * is underway against any of the blocks which are outside the truncation
81 * point. Because the caller is about to free (and possibly reuse) those
84 void do_invalidatepage(struct page *page, unsigned int offset,
87 void (*invalidatepage)(struct page *, unsigned int, unsigned int);
89 invalidatepage = page->mapping->a_ops->invalidatepage;
92 invalidatepage = block_invalidatepage;
95 (*invalidatepage)(page, offset, length);
99 * This cancels just the dirty bit on the kernel page itself, it
100 * does NOT actually remove dirty bits on any mmap's that may be
101 * around. It also leaves the page tagged dirty, so any sync
102 * activity will still find it on the dirty lists, and in particular,
103 * clear_page_dirty_for_io() will still look at the dirty bits in
106 * Doing this should *normally* only ever be done when a page
107 * is truncated, and is not actually mapped anywhere at all. However,
108 * fs/buffer.c does this when it notices that somebody has cleaned
109 * out all the buffers on a page without actually doing it through
110 * the VM. Can you say "ext3 is horribly ugly"? Tought you could.
112 void cancel_dirty_page(struct page *page, unsigned int account_size)
114 if (TestClearPageDirty(page)) {
115 struct address_space *mapping = page->mapping;
116 if (mapping && mapping_cap_account_dirty(mapping)) {
117 dec_zone_page_state(page, NR_FILE_DIRTY);
118 dec_bdi_stat(inode_to_bdi(mapping->host),
121 task_io_account_cancelled_write(account_size);
125 EXPORT_SYMBOL(cancel_dirty_page);
128 * If truncate cannot remove the fs-private metadata from the page, the page
129 * becomes orphaned. It will be left on the LRU and may even be mapped into
130 * user pagetables if we're racing with filemap_fault().
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
134 * its lock, b) when a concurrent invalidate_mapping_pages got there first and
135 * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
138 truncate_complete_page(struct address_space *mapping, struct page *page)
140 if (page->mapping != mapping)
143 if (page_has_private(page))
144 do_invalidatepage(page, 0, PAGE_CACHE_SIZE);
146 cancel_dirty_page(page, PAGE_CACHE_SIZE);
148 ClearPageMappedToDisk(page);
149 delete_from_page_cache(page);
154 * This is for invalidate_mapping_pages(). That function can be called at
155 * any time, and is not supposed to throw away dirty pages. But pages can
156 * be marked dirty at any time too, so use remove_mapping which safely
157 * discards clean, unused pages.
159 * Returns non-zero if the page was successfully invalidated.
162 invalidate_complete_page(struct address_space *mapping, struct page *page)
166 if (page->mapping != mapping)
169 if (page_has_private(page) && !try_to_release_page(page, 0))
172 ret = remove_mapping(mapping, page);
177 int truncate_inode_page(struct address_space *mapping, struct page *page)
179 if (page_mapped(page)) {
180 unmap_mapping_range(mapping,
181 (loff_t)page->index << PAGE_CACHE_SHIFT,
184 return truncate_complete_page(mapping, page);
188 * Used to get rid of pages on hardware memory corruption.
190 int generic_error_remove_page(struct address_space *mapping, struct page *page)
195 * Only punch for normal data pages for now.
196 * Handling other types like directories would need more auditing.
198 if (!S_ISREG(mapping->host->i_mode))
200 return truncate_inode_page(mapping, page);
202 EXPORT_SYMBOL(generic_error_remove_page);
205 * Safely invalidate one page from its pagecache mapping.
206 * It only drops clean, unused pages. The page must be locked.
208 * Returns 1 if the page is successfully invalidated, otherwise 0.
210 int invalidate_inode_page(struct page *page)
212 struct address_space *mapping = page_mapping(page);
215 if (PageDirty(page) || PageWriteback(page))
217 if (page_mapped(page))
219 return invalidate_complete_page(mapping, page);
223 * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
224 * @mapping: mapping to truncate
225 * @lstart: offset from which to truncate
226 * @lend: offset to which to truncate (inclusive)
228 * Truncate the page cache, removing the pages that are between
229 * specified offsets (and zeroing out partial pages
230 * if lstart or lend + 1 is not page aligned).
232 * Truncate takes two passes - the first pass is nonblocking. It will not
233 * block on page locks and it will not block on writeback. The second pass
234 * will wait. This is to prevent as much IO as possible in the affected region.
235 * The first pass will remove most pages, so the search cost of the second pass
238 * We pass down the cache-hot hint to the page freeing code. Even if the
239 * mapping is large, it is probably the case that the final pages are the most
240 * recently touched, and freeing happens in ascending file offset order.
242 * Note that since ->invalidatepage() accepts range to invalidate
243 * truncate_inode_pages_range is able to handle cases where lend + 1 is not
244 * page aligned properly.
246 void truncate_inode_pages_range(struct address_space *mapping,
247 loff_t lstart, loff_t lend)
249 pgoff_t start; /* inclusive */
250 pgoff_t end; /* exclusive */
251 unsigned int partial_start; /* inclusive */
252 unsigned int partial_end; /* exclusive */
254 pgoff_t indices[PAGEVEC_SIZE];
258 cleancache_invalidate_inode(mapping);
259 if (mapping->nrpages == 0 && mapping->nrshadows == 0)
262 /* Offsets within partial pages */
263 partial_start = lstart & (PAGE_CACHE_SIZE - 1);
264 partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
267 * 'start' and 'end' always covers the range of pages to be fully
268 * truncated. Partial pages are covered with 'partial_start' at the
269 * start of the range and 'partial_end' at the end of the range.
270 * Note that 'end' is exclusive while 'lend' is inclusive.
272 start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
275 * lend == -1 indicates end-of-file so we have to set 'end'
276 * to the highest possible pgoff_t and since the type is
277 * unsigned we're using -1.
281 end = (lend + 1) >> PAGE_CACHE_SHIFT;
283 pagevec_init(&pvec, 0);
285 while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
286 min(end - index, (pgoff_t)PAGEVEC_SIZE),
288 for (i = 0; i < pagevec_count(&pvec); i++) {
289 struct page *page = pvec.pages[i];
291 /* We rely upon deletion not changing page->index */
296 if (radix_tree_exceptional_entry(page)) {
297 clear_exceptional_entry(mapping, index, page);
301 if (!trylock_page(page))
303 WARN_ON(page->index != index);
304 if (PageWriteback(page)) {
308 truncate_inode_page(mapping, page);
311 pagevec_remove_exceptionals(&pvec);
312 pagevec_release(&pvec);
318 struct page *page = find_lock_page(mapping, start - 1);
320 unsigned int top = PAGE_CACHE_SIZE;
322 /* Truncation within a single page */
326 wait_on_page_writeback(page);
327 zero_user_segment(page, partial_start, top);
328 cleancache_invalidate_page(mapping, page);
329 if (page_has_private(page))
330 do_invalidatepage(page, partial_start,
331 top - partial_start);
333 page_cache_release(page);
337 struct page *page = find_lock_page(mapping, end);
339 wait_on_page_writeback(page);
340 zero_user_segment(page, 0, partial_end);
341 cleancache_invalidate_page(mapping, page);
342 if (page_has_private(page))
343 do_invalidatepage(page, 0,
346 page_cache_release(page);
350 * If the truncation happened within a single page no pages
351 * will be released, just zeroed, so we can bail out now.
359 if (!pagevec_lookup_entries(&pvec, mapping, index,
360 min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
361 /* If all gone from start onwards, we're done */
364 /* Otherwise restart to make sure all gone */
368 if (index == start && indices[0] >= end) {
369 /* All gone out of hole to be punched, we're done */
370 pagevec_remove_exceptionals(&pvec);
371 pagevec_release(&pvec);
374 for (i = 0; i < pagevec_count(&pvec); i++) {
375 struct page *page = pvec.pages[i];
377 /* We rely upon deletion not changing page->index */
380 /* Restart punch to make sure all gone */
385 if (radix_tree_exceptional_entry(page)) {
386 clear_exceptional_entry(mapping, index, page);
391 WARN_ON(page->index != index);
392 wait_on_page_writeback(page);
393 truncate_inode_page(mapping, page);
396 pagevec_remove_exceptionals(&pvec);
397 pagevec_release(&pvec);
400 cleancache_invalidate_inode(mapping);
402 EXPORT_SYMBOL(truncate_inode_pages_range);
405 * truncate_inode_pages - truncate *all* the pages from an offset
406 * @mapping: mapping to truncate
407 * @lstart: offset from which to truncate
409 * Called under (and serialised by) inode->i_mutex.
411 * Note: When this function returns, there can be a page in the process of
412 * deletion (inside __delete_from_page_cache()) in the specified range. Thus
413 * mapping->nrpages can be non-zero when this function returns even after
414 * truncation of the whole mapping.
416 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
418 truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
420 EXPORT_SYMBOL(truncate_inode_pages);
423 * truncate_inode_pages_final - truncate *all* pages before inode dies
424 * @mapping: mapping to truncate
426 * Called under (and serialized by) inode->i_mutex.
428 * Filesystems have to use this in the .evict_inode path to inform the
429 * VM that this is the final truncate and the inode is going away.
431 void truncate_inode_pages_final(struct address_space *mapping)
433 unsigned long nrshadows;
434 unsigned long nrpages;
437 * Page reclaim can not participate in regular inode lifetime
438 * management (can't call iput()) and thus can race with the
439 * inode teardown. Tell it when the address space is exiting,
440 * so that it does not install eviction information after the
441 * final truncate has begun.
443 mapping_set_exiting(mapping);
446 * When reclaim installs eviction entries, it increases
447 * nrshadows first, then decreases nrpages. Make sure we see
448 * this in the right order or we might miss an entry.
450 nrpages = mapping->nrpages;
452 nrshadows = mapping->nrshadows;
454 if (nrpages || nrshadows) {
456 * As truncation uses a lockless tree lookup, cycle
457 * the tree lock to make sure any ongoing tree
458 * modification that does not see AS_EXITING is
459 * completed before starting the final truncate.
461 spin_lock_irq(&mapping->tree_lock);
462 spin_unlock_irq(&mapping->tree_lock);
464 truncate_inode_pages(mapping, 0);
467 EXPORT_SYMBOL(truncate_inode_pages_final);
470 * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
471 * @mapping: the address_space which holds the pages to invalidate
472 * @start: the offset 'from' which to invalidate
473 * @end: the offset 'to' which to invalidate (inclusive)
475 * This function only removes the unlocked pages, if you want to
476 * remove all the pages of one inode, you must call truncate_inode_pages.
478 * invalidate_mapping_pages() will not block on IO activity. It will not
479 * invalidate pages which are dirty, locked, under writeback or mapped into
482 unsigned long invalidate_mapping_pages(struct address_space *mapping,
483 pgoff_t start, pgoff_t end)
485 pgoff_t indices[PAGEVEC_SIZE];
487 pgoff_t index = start;
489 unsigned long count = 0;
492 pagevec_init(&pvec, 0);
493 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
494 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
496 for (i = 0; i < pagevec_count(&pvec); i++) {
497 struct page *page = pvec.pages[i];
499 /* We rely upon deletion not changing page->index */
504 if (radix_tree_exceptional_entry(page)) {
505 clear_exceptional_entry(mapping, index, page);
509 if (!trylock_page(page))
511 WARN_ON(page->index != index);
512 ret = invalidate_inode_page(page);
515 * Invalidation is a hint that the page is no longer
516 * of interest and try to speed up its reclaim.
519 deactivate_page(page);
522 pagevec_remove_exceptionals(&pvec);
523 pagevec_release(&pvec);
529 EXPORT_SYMBOL(invalidate_mapping_pages);
532 * This is like invalidate_complete_page(), except it ignores the page's
533 * refcount. We do this because invalidate_inode_pages2() needs stronger
534 * invalidation guarantees, and cannot afford to leave pages behind because
535 * shrink_page_list() has a temp ref on them, or because they're transiently
536 * sitting in the lru_cache_add() pagevecs.
539 invalidate_complete_page2(struct address_space *mapping, struct page *page)
541 if (page->mapping != mapping)
544 if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
547 spin_lock_irq(&mapping->tree_lock);
551 BUG_ON(page_has_private(page));
552 __delete_from_page_cache(page, NULL);
553 spin_unlock_irq(&mapping->tree_lock);
555 if (mapping->a_ops->freepage)
556 mapping->a_ops->freepage(page);
558 page_cache_release(page); /* pagecache ref */
561 spin_unlock_irq(&mapping->tree_lock);
565 static int do_launder_page(struct address_space *mapping, struct page *page)
567 if (!PageDirty(page))
569 if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
571 return mapping->a_ops->launder_page(page);
575 * invalidate_inode_pages2_range - remove range of pages from an address_space
576 * @mapping: the address_space
577 * @start: the page offset 'from' which to invalidate
578 * @end: the page offset 'to' which to invalidate (inclusive)
580 * Any pages which are found to be mapped into pagetables are unmapped prior to
583 * Returns -EBUSY if any pages could not be invalidated.
585 int invalidate_inode_pages2_range(struct address_space *mapping,
586 pgoff_t start, pgoff_t end)
588 pgoff_t indices[PAGEVEC_SIZE];
594 int did_range_unmap = 0;
596 cleancache_invalidate_inode(mapping);
597 pagevec_init(&pvec, 0);
599 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
600 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
602 for (i = 0; i < pagevec_count(&pvec); i++) {
603 struct page *page = pvec.pages[i];
605 /* We rely upon deletion not changing page->index */
610 if (radix_tree_exceptional_entry(page)) {
611 clear_exceptional_entry(mapping, index, page);
616 WARN_ON(page->index != index);
617 if (page->mapping != mapping) {
621 wait_on_page_writeback(page);
622 if (page_mapped(page)) {
623 if (!did_range_unmap) {
625 * Zap the rest of the file in one hit.
627 unmap_mapping_range(mapping,
628 (loff_t)index << PAGE_CACHE_SHIFT,
629 (loff_t)(1 + end - index)
637 unmap_mapping_range(mapping,
638 (loff_t)index << PAGE_CACHE_SHIFT,
642 BUG_ON(page_mapped(page));
643 ret2 = do_launder_page(mapping, page);
645 if (!invalidate_complete_page2(mapping, page))
652 pagevec_remove_exceptionals(&pvec);
653 pagevec_release(&pvec);
657 cleancache_invalidate_inode(mapping);
660 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
663 * invalidate_inode_pages2 - remove all pages from an address_space
664 * @mapping: the address_space
666 * Any pages which are found to be mapped into pagetables are unmapped prior to
669 * Returns -EBUSY if any pages could not be invalidated.
671 int invalidate_inode_pages2(struct address_space *mapping)
673 return invalidate_inode_pages2_range(mapping, 0, -1);
675 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
678 * truncate_pagecache - unmap and remove pagecache that has been truncated
680 * @newsize: new file size
682 * inode's new i_size must already be written before truncate_pagecache
685 * This function should typically be called before the filesystem
686 * releases resources associated with the freed range (eg. deallocates
687 * blocks). This way, pagecache will always stay logically coherent
688 * with on-disk format, and the filesystem would not have to deal with
689 * situations such as writepage being called for a page that has already
690 * had its underlying blocks deallocated.
692 void truncate_pagecache(struct inode *inode, loff_t newsize)
694 struct address_space *mapping = inode->i_mapping;
695 loff_t holebegin = round_up(newsize, PAGE_SIZE);
698 * unmap_mapping_range is called twice, first simply for
699 * efficiency so that truncate_inode_pages does fewer
700 * single-page unmaps. However after this first call, and
701 * before truncate_inode_pages finishes, it is possible for
702 * private pages to be COWed, which remain after
703 * truncate_inode_pages finishes, hence the second
704 * unmap_mapping_range call must be made for correctness.
706 unmap_mapping_range(mapping, holebegin, 0, 1);
707 truncate_inode_pages(mapping, newsize);
708 unmap_mapping_range(mapping, holebegin, 0, 1);
710 EXPORT_SYMBOL(truncate_pagecache);
713 * truncate_setsize - update inode and pagecache for a new file size
715 * @newsize: new file size
717 * truncate_setsize updates i_size and performs pagecache truncation (if
718 * necessary) to @newsize. It will be typically be called from the filesystem's
719 * setattr function when ATTR_SIZE is passed in.
721 * Must be called with a lock serializing truncates and writes (generally
722 * i_mutex but e.g. xfs uses a different lock) and before all filesystem
723 * specific block truncation has been performed.
725 void truncate_setsize(struct inode *inode, loff_t newsize)
727 loff_t oldsize = inode->i_size;
729 i_size_write(inode, newsize);
730 if (newsize > oldsize)
731 pagecache_isize_extended(inode, oldsize, newsize);
732 truncate_pagecache(inode, newsize);
734 EXPORT_SYMBOL(truncate_setsize);
737 * pagecache_isize_extended - update pagecache after extension of i_size
738 * @inode: inode for which i_size was extended
739 * @from: original inode size
740 * @to: new inode size
742 * Handle extension of inode size either caused by extending truncate or by
743 * write starting after current i_size. We mark the page straddling current
744 * i_size RO so that page_mkwrite() is called on the nearest write access to
745 * the page. This way filesystem can be sure that page_mkwrite() is called on
746 * the page before user writes to the page via mmap after the i_size has been
749 * The function must be called after i_size is updated so that page fault
750 * coming after we unlock the page will already see the new i_size.
751 * The function must be called while we still hold i_mutex - this not only
752 * makes sure i_size is stable but also that userspace cannot observe new
753 * i_size value before we are prepared to store mmap writes at new inode size.
755 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
757 int bsize = 1 << inode->i_blkbits;
762 WARN_ON(to > inode->i_size);
764 if (from >= to || bsize == PAGE_CACHE_SIZE)
766 /* Page straddling @from will not have any hole block created? */
767 rounded_from = round_up(from, bsize);
768 if (to <= rounded_from || !(rounded_from & (PAGE_CACHE_SIZE - 1)))
771 index = from >> PAGE_CACHE_SHIFT;
772 page = find_lock_page(inode->i_mapping, index);
773 /* Page not cached? Nothing to do */
777 * See clear_page_dirty_for_io() for details why set_page_dirty()
780 if (page_mkclean(page))
781 set_page_dirty(page);
783 page_cache_release(page);
785 EXPORT_SYMBOL(pagecache_isize_extended);
788 * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
790 * @lstart: offset of beginning of hole
791 * @lend: offset of last byte of hole
793 * This function should typically be called before the filesystem
794 * releases resources associated with the freed range (eg. deallocates
795 * blocks). This way, pagecache will always stay logically coherent
796 * with on-disk format, and the filesystem would not have to deal with
797 * situations such as writepage being called for a page that has already
798 * had its underlying blocks deallocated.
800 void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
802 struct address_space *mapping = inode->i_mapping;
803 loff_t unmap_start = round_up(lstart, PAGE_SIZE);
804 loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
806 * This rounding is currently just for example: unmap_mapping_range
807 * expands its hole outwards, whereas we want it to contract the hole
808 * inwards. However, existing callers of truncate_pagecache_range are
809 * doing their own page rounding first. Note that unmap_mapping_range
810 * allows holelen 0 for all, and we allow lend -1 for end of file.
814 * Unlike in truncate_pagecache, unmap_mapping_range is called only
815 * once (before truncating pagecache), and without "even_cows" flag:
816 * hole-punching should not remove private COWed pages from the hole.
818 if ((u64)unmap_end > (u64)unmap_start)
819 unmap_mapping_range(mapping, unmap_start,
820 1 + unmap_end - unmap_start, 0);
821 truncate_inode_pages_range(mapping, lstart, lend);
823 EXPORT_SYMBOL(truncate_pagecache_range);