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/dax.h>
13 #include <linux/gfp.h>
15 #include <linux/swap.h>
16 #include <linux/export.h>
17 #include <linux/pagemap.h>
18 #include <linux/highmem.h>
19 #include <linux/pagevec.h>
20 #include <linux/task_io_accounting_ops.h>
21 #include <linux/buffer_head.h> /* grr. try_to_release_page,
23 #include <linux/shmem_fs.h>
24 #include <linux/cleancache.h>
25 #include <linux/rmap.h>
28 static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
31 struct radix_tree_node *node;
34 spin_lock_irq(&mapping->tree_lock);
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.
40 if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
44 __radix_tree_replace(&mapping->page_tree, node, slot, NULL,
45 workingset_update_node, mapping);
46 mapping->nrexceptional--;
48 spin_unlock_irq(&mapping->tree_lock);
52 * Unconditionally remove exceptional entry. Usually called from truncate path.
54 static void truncate_exceptional_entry(struct address_space *mapping,
55 pgoff_t index, void *entry)
57 /* Handled by shmem itself */
58 if (shmem_mapping(mapping))
61 if (dax_mapping(mapping)) {
62 dax_delete_mapping_entry(mapping, index);
65 clear_shadow_entry(mapping, index, entry);
69 * Invalidate exceptional entry if easily possible. This handles exceptional
70 * entries for invalidate_inode_pages().
72 static int invalidate_exceptional_entry(struct address_space *mapping,
73 pgoff_t index, void *entry)
75 /* Handled by shmem itself, or for DAX we do nothing. */
76 if (shmem_mapping(mapping) || dax_mapping(mapping))
78 clear_shadow_entry(mapping, index, entry);
83 * Invalidate exceptional entry if clean. This handles exceptional entries for
84 * invalidate_inode_pages2() so for DAX it evicts only clean entries.
86 static int invalidate_exceptional_entry2(struct address_space *mapping,
87 pgoff_t index, void *entry)
89 /* Handled by shmem itself */
90 if (shmem_mapping(mapping))
92 if (dax_mapping(mapping))
93 return dax_invalidate_mapping_entry_sync(mapping, index);
94 clear_shadow_entry(mapping, index, entry);
99 * do_invalidatepage - invalidate part or all of a page
100 * @page: the page which is affected
101 * @offset: start of the range to invalidate
102 * @length: length of the range to invalidate
104 * do_invalidatepage() is called when all or part of the page has become
105 * invalidated by a truncate operation.
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
113 void do_invalidatepage(struct page *page, unsigned int offset,
116 void (*invalidatepage)(struct page *, unsigned int, unsigned int);
118 invalidatepage = page->mapping->a_ops->invalidatepage;
121 invalidatepage = block_invalidatepage;
124 (*invalidatepage)(page, offset, length);
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_cleanup_page(struct address_space *mapping, struct page *page)
140 if (page_mapped(page)) {
143 holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE;
144 unmap_mapping_range(mapping,
145 (loff_t)page->index << PAGE_SHIFT,
149 if (page_has_private(page))
150 do_invalidatepage(page, 0, PAGE_SIZE);
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.
157 cancel_dirty_page(page);
158 ClearPageMappedToDisk(page);
162 * This is for invalidate_mapping_pages(). That function can be called at
163 * any time, and is not supposed to throw away dirty pages. But pages can
164 * be marked dirty at any time too, so use remove_mapping which safely
165 * discards clean, unused pages.
167 * Returns non-zero if the page was successfully invalidated.
170 invalidate_complete_page(struct address_space *mapping, struct page *page)
174 if (page->mapping != mapping)
177 if (page_has_private(page) && !try_to_release_page(page, 0))
180 ret = remove_mapping(mapping, page);
185 int truncate_inode_page(struct address_space *mapping, struct page *page)
187 VM_BUG_ON_PAGE(PageTail(page), page);
189 if (page->mapping != mapping)
192 truncate_cleanup_page(mapping, page);
193 delete_from_page_cache(page);
198 * Used to get rid of pages on hardware memory corruption.
200 int generic_error_remove_page(struct address_space *mapping, struct page *page)
205 * Only punch for normal data pages for now.
206 * Handling other types like directories would need more auditing.
208 if (!S_ISREG(mapping->host->i_mode))
210 return truncate_inode_page(mapping, page);
212 EXPORT_SYMBOL(generic_error_remove_page);
215 * Safely invalidate one page from its pagecache mapping.
216 * It only drops clean, unused pages. The page must be locked.
218 * Returns 1 if the page is successfully invalidated, otherwise 0.
220 int invalidate_inode_page(struct page *page)
222 struct address_space *mapping = page_mapping(page);
225 if (PageDirty(page) || PageWriteback(page))
227 if (page_mapped(page))
229 return invalidate_complete_page(mapping, page);
233 * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
234 * @mapping: mapping to truncate
235 * @lstart: offset from which to truncate
236 * @lend: offset to which to truncate (inclusive)
238 * Truncate the page cache, removing the pages that are between
239 * specified offsets (and zeroing out partial pages
240 * if lstart or lend + 1 is not page aligned).
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
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.
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.
256 void truncate_inode_pages_range(struct address_space *mapping,
257 loff_t lstart, loff_t lend)
259 pgoff_t start; /* inclusive */
260 pgoff_t end; /* exclusive */
261 unsigned int partial_start; /* inclusive */
262 unsigned int partial_end; /* exclusive */
264 pgoff_t indices[PAGEVEC_SIZE];
268 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
271 /* Offsets within partial pages */
272 partial_start = lstart & (PAGE_SIZE - 1);
273 partial_end = (lend + 1) & (PAGE_SIZE - 1);
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.
281 start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
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.
290 end = (lend + 1) >> PAGE_SHIFT;
292 pagevec_init(&pvec, 0);
294 while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
295 min(end - index, (pgoff_t)PAGEVEC_SIZE),
297 for (i = 0; i < pagevec_count(&pvec); i++) {
298 struct page *page = pvec.pages[i];
300 /* We rely upon deletion not changing page->index */
305 if (radix_tree_exceptional_entry(page)) {
306 truncate_exceptional_entry(mapping, index,
311 if (!trylock_page(page))
313 WARN_ON(page_to_index(page) != index);
314 if (PageWriteback(page)) {
318 truncate_inode_page(mapping, page);
321 pagevec_remove_exceptionals(&pvec);
322 pagevec_release(&pvec);
328 struct page *page = find_lock_page(mapping, start - 1);
330 unsigned int top = PAGE_SIZE;
332 /* Truncation within a single page */
336 wait_on_page_writeback(page);
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);
347 struct page *page = find_lock_page(mapping, end);
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,
360 * If the truncation happened within a single page no pages
361 * will be released, just zeroed, so we can bail out now.
369 if (!pagevec_lookup_entries(&pvec, mapping, index,
370 min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
371 /* If all gone from start onwards, we're done */
374 /* Otherwise restart to make sure all gone */
378 if (index == start && indices[0] >= end) {
379 /* All gone out of hole to be punched, we're done */
380 pagevec_remove_exceptionals(&pvec);
381 pagevec_release(&pvec);
384 for (i = 0; i < pagevec_count(&pvec); i++) {
385 struct page *page = pvec.pages[i];
387 /* We rely upon deletion not changing page->index */
390 /* Restart punch to make sure all gone */
395 if (radix_tree_exceptional_entry(page)) {
396 truncate_exceptional_entry(mapping, index,
402 WARN_ON(page_to_index(page) != index);
403 wait_on_page_writeback(page);
404 truncate_inode_page(mapping, page);
407 pagevec_remove_exceptionals(&pvec);
408 pagevec_release(&pvec);
413 cleancache_invalidate_inode(mapping);
415 EXPORT_SYMBOL(truncate_inode_pages_range);
418 * truncate_inode_pages - truncate *all* the pages from an offset
419 * @mapping: mapping to truncate
420 * @lstart: offset from which to truncate
422 * Called under (and serialised by) inode->i_mutex.
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.
429 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
431 truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
433 EXPORT_SYMBOL(truncate_inode_pages);
436 * truncate_inode_pages_final - truncate *all* pages before inode dies
437 * @mapping: mapping to truncate
439 * Called under (and serialized by) inode->i_mutex.
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.
444 void truncate_inode_pages_final(struct address_space *mapping)
446 unsigned long nrexceptional;
447 unsigned long nrpages;
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.
456 mapping_set_exiting(mapping);
459 * When reclaim installs eviction entries, it increases
460 * nrexceptional first, then decreases nrpages. Make sure we see
461 * this in the right order or we might miss an entry.
463 nrpages = mapping->nrpages;
465 nrexceptional = mapping->nrexceptional;
467 if (nrpages || nrexceptional) {
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.
474 spin_lock_irq(&mapping->tree_lock);
475 spin_unlock_irq(&mapping->tree_lock);
477 truncate_inode_pages(mapping, 0);
480 EXPORT_SYMBOL(truncate_inode_pages_final);
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)
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.
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
495 unsigned long invalidate_mapping_pages(struct address_space *mapping,
496 pgoff_t start, pgoff_t end)
498 pgoff_t indices[PAGEVEC_SIZE];
500 pgoff_t index = start;
502 unsigned long count = 0;
505 pagevec_init(&pvec, 0);
506 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
507 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
509 for (i = 0; i < pagevec_count(&pvec); i++) {
510 struct page *page = pvec.pages[i];
512 /* We rely upon deletion not changing page->index */
517 if (radix_tree_exceptional_entry(page)) {
518 invalidate_exceptional_entry(mapping, index,
523 if (!trylock_page(page))
526 WARN_ON(page_to_index(page) != index);
528 /* Middle of THP: skip */
529 if (PageTransTail(page)) {
532 } else if (PageTransHuge(page)) {
533 index += HPAGE_PMD_NR - 1;
534 i += HPAGE_PMD_NR - 1;
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.
546 ret = invalidate_inode_page(page);
549 * Invalidation is a hint that the page is no longer
550 * of interest and try to speed up its reclaim.
553 deactivate_file_page(page);
556 pagevec_remove_exceptionals(&pvec);
557 pagevec_release(&pvec);
563 EXPORT_SYMBOL(invalidate_mapping_pages);
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
569 * shrink_page_list() has a temp ref on them, or because they're transiently
570 * sitting in the lru_cache_add() pagevecs.
573 invalidate_complete_page2(struct address_space *mapping, struct page *page)
577 if (page->mapping != mapping)
580 if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
583 spin_lock_irqsave(&mapping->tree_lock, flags);
587 BUG_ON(page_has_private(page));
588 __delete_from_page_cache(page, NULL);
589 spin_unlock_irqrestore(&mapping->tree_lock, flags);
591 if (mapping->a_ops->freepage)
592 mapping->a_ops->freepage(page);
594 put_page(page); /* pagecache ref */
597 spin_unlock_irqrestore(&mapping->tree_lock, flags);
601 static int do_launder_page(struct address_space *mapping, struct page *page)
603 if (!PageDirty(page))
605 if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
607 return mapping->a_ops->launder_page(page);
611 * invalidate_inode_pages2_range - remove range of pages from an address_space
612 * @mapping: the address_space
613 * @start: the page offset 'from' which to invalidate
614 * @end: the page offset 'to' which to invalidate (inclusive)
616 * Any pages which are found to be mapped into pagetables are unmapped prior to
619 * Returns -EBUSY if any pages could not be invalidated.
621 int invalidate_inode_pages2_range(struct address_space *mapping,
622 pgoff_t start, pgoff_t end)
624 pgoff_t indices[PAGEVEC_SIZE];
630 int did_range_unmap = 0;
632 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
635 pagevec_init(&pvec, 0);
637 while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
638 min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
640 for (i = 0; i < pagevec_count(&pvec); i++) {
641 struct page *page = pvec.pages[i];
643 /* We rely upon deletion not changing page->index */
648 if (radix_tree_exceptional_entry(page)) {
649 if (!invalidate_exceptional_entry2(mapping,
656 WARN_ON(page_to_index(page) != index);
657 if (page->mapping != mapping) {
661 wait_on_page_writeback(page);
662 if (page_mapped(page)) {
663 if (!did_range_unmap) {
665 * Zap the rest of the file in one hit.
667 unmap_mapping_range(mapping,
668 (loff_t)index << PAGE_SHIFT,
669 (loff_t)(1 + end - index)
677 unmap_mapping_range(mapping,
678 (loff_t)index << PAGE_SHIFT,
682 BUG_ON(page_mapped(page));
683 ret2 = do_launder_page(mapping, page);
685 if (!invalidate_complete_page2(mapping, page))
692 pagevec_remove_exceptionals(&pvec);
693 pagevec_release(&pvec);
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.
704 if (dax_mapping(mapping)) {
705 unmap_mapping_range(mapping, (loff_t)start << PAGE_SHIFT,
706 (loff_t)(end - start + 1) << PAGE_SHIFT, 0);
709 cleancache_invalidate_inode(mapping);
712 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
715 * invalidate_inode_pages2 - remove all pages from an address_space
716 * @mapping: the address_space
718 * Any pages which are found to be mapped into pagetables are unmapped prior to
721 * Returns -EBUSY if any pages could not be invalidated.
723 int invalidate_inode_pages2(struct address_space *mapping)
725 return invalidate_inode_pages2_range(mapping, 0, -1);
727 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
730 * truncate_pagecache - unmap and remove pagecache that has been truncated
732 * @newsize: new file size
734 * inode's new i_size must already be written before truncate_pagecache
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.
744 void truncate_pagecache(struct inode *inode, loff_t newsize)
746 struct address_space *mapping = inode->i_mapping;
747 loff_t holebegin = round_up(newsize, PAGE_SIZE);
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.
758 unmap_mapping_range(mapping, holebegin, 0, 1);
759 truncate_inode_pages(mapping, newsize);
760 unmap_mapping_range(mapping, holebegin, 0, 1);
762 EXPORT_SYMBOL(truncate_pagecache);
765 * truncate_setsize - update inode and pagecache for a new file size
767 * @newsize: new file size
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.
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.
777 void truncate_setsize(struct inode *inode, loff_t newsize)
779 loff_t oldsize = inode->i_size;
781 i_size_write(inode, newsize);
782 if (newsize > oldsize)
783 pagecache_isize_extended(inode, oldsize, newsize);
784 truncate_pagecache(inode, newsize);
786 EXPORT_SYMBOL(truncate_setsize);
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
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
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.
807 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
809 int bsize = i_blocksize(inode);
814 WARN_ON(to > inode->i_size);
816 if (from >= to || bsize == PAGE_SIZE)
818 /* Page straddling @from will not have any hole block created? */
819 rounded_from = round_up(from, bsize);
820 if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
823 index = from >> PAGE_SHIFT;
824 page = find_lock_page(inode->i_mapping, index);
825 /* Page not cached? Nothing to do */
829 * See clear_page_dirty_for_io() for details why set_page_dirty()
832 if (page_mkclean(page))
833 set_page_dirty(page);
837 EXPORT_SYMBOL(pagecache_isize_extended);
840 * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
842 * @lstart: offset of beginning of hole
843 * @lend: offset of last byte of hole
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.
852 void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
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;
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
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.
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.
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);
875 EXPORT_SYMBOL(truncate_pagecache_range);