]> rtime.felk.cvut.cz Git - zynq/linux.git/blob - mm/truncate.c
Merge branch '4.0.8-rt6'
[zynq/linux.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    Andrew Morton
7  *              Initial version.
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/backing-dev.h>
12 #include <linux/gfp.h>
13 #include <linux/mm.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,
21                                    do_invalidatepage */
22 #include <linux/cleancache.h>
23 #include <linux/rmap.h>
24 #include "internal.h"
25
26 static void clear_exceptional_entry(struct address_space *mapping,
27                                     pgoff_t index, void *entry)
28 {
29         struct radix_tree_node *node;
30         void **slot;
31
32         /* Handled by shmem itself */
33         if (shmem_mapping(mapping))
34                 return;
35
36         spin_lock_irq(&mapping->tree_lock);
37         /*
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.
41          */
42         if (!__radix_tree_lookup(&mapping->page_tree, index, &node, &slot))
43                 goto unlock;
44         if (*slot != entry)
45                 goto unlock;
46         radix_tree_replace_slot(slot, NULL);
47         mapping->nrshadows--;
48         if (!node)
49                 goto unlock;
50         workingset_node_shadows_dec(node);
51         /*
52          * Don't track node without shadow entries.
53          *
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.
57          */
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);
63         }
64         __radix_tree_delete_node(&mapping->page_tree, node);
65 unlock:
66         spin_unlock_irq(&mapping->tree_lock);
67 }
68
69 /**
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
74  *
75  * do_invalidatepage() is called when all or part of the page has become
76  * invalidated by a truncate operation.
77  *
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
82  * blocks on-disk.
83  */
84 void do_invalidatepage(struct page *page, unsigned int offset,
85                        unsigned int length)
86 {
87         void (*invalidatepage)(struct page *, unsigned int, unsigned int);
88
89         invalidatepage = page->mapping->a_ops->invalidatepage;
90 #ifdef CONFIG_BLOCK
91         if (!invalidatepage)
92                 invalidatepage = block_invalidatepage;
93 #endif
94         if (invalidatepage)
95                 (*invalidatepage)(page, offset, length);
96 }
97
98 /*
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
104  * the VM.
105  *
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.
111  */
112 void cancel_dirty_page(struct page *page, unsigned int account_size)
113 {
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),
119                                         BDI_RECLAIMABLE);
120                         if (account_size)
121                                 task_io_account_cancelled_write(account_size);
122                 }
123         }
124 }
125 EXPORT_SYMBOL(cancel_dirty_page);
126
127 /*
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().
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
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.
136  */
137 static int
138 truncate_complete_page(struct address_space *mapping, struct page *page)
139 {
140         if (page->mapping != mapping)
141                 return -EIO;
142
143         if (page_has_private(page))
144                 do_invalidatepage(page, 0, PAGE_CACHE_SIZE);
145
146         cancel_dirty_page(page, PAGE_CACHE_SIZE);
147
148         ClearPageMappedToDisk(page);
149         delete_from_page_cache(page);
150         return 0;
151 }
152
153 /*
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.
158  *
159  * Returns non-zero if the page was successfully invalidated.
160  */
161 static int
162 invalidate_complete_page(struct address_space *mapping, struct page *page)
163 {
164         int ret;
165
166         if (page->mapping != mapping)
167                 return 0;
168
169         if (page_has_private(page) && !try_to_release_page(page, 0))
170                 return 0;
171
172         ret = remove_mapping(mapping, page);
173
174         return ret;
175 }
176
177 int truncate_inode_page(struct address_space *mapping, struct page *page)
178 {
179         if (page_mapped(page)) {
180                 unmap_mapping_range(mapping,
181                                    (loff_t)page->index << PAGE_CACHE_SHIFT,
182                                    PAGE_CACHE_SIZE, 0);
183         }
184         return truncate_complete_page(mapping, page);
185 }
186
187 /*
188  * Used to get rid of pages on hardware memory corruption.
189  */
190 int generic_error_remove_page(struct address_space *mapping, struct page *page)
191 {
192         if (!mapping)
193                 return -EINVAL;
194         /*
195          * Only punch for normal data pages for now.
196          * Handling other types like directories would need more auditing.
197          */
198         if (!S_ISREG(mapping->host->i_mode))
199                 return -EIO;
200         return truncate_inode_page(mapping, page);
201 }
202 EXPORT_SYMBOL(generic_error_remove_page);
203
204 /*
205  * Safely invalidate one page from its pagecache mapping.
206  * It only drops clean, unused pages. The page must be locked.
207  *
208  * Returns 1 if the page is successfully invalidated, otherwise 0.
209  */
210 int invalidate_inode_page(struct page *page)
211 {
212         struct address_space *mapping = page_mapping(page);
213         if (!mapping)
214                 return 0;
215         if (PageDirty(page) || PageWriteback(page))
216                 return 0;
217         if (page_mapped(page))
218                 return 0;
219         return invalidate_complete_page(mapping, page);
220 }
221
222 /**
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)
227  *
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).
231  *
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
236  * is low.
237  *
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.
241  *
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.
245  */
246 void truncate_inode_pages_range(struct address_space *mapping,
247                                 loff_t lstart, loff_t lend)
248 {
249         pgoff_t         start;          /* inclusive */
250         pgoff_t         end;            /* exclusive */
251         unsigned int    partial_start;  /* inclusive */
252         unsigned int    partial_end;    /* exclusive */
253         struct pagevec  pvec;
254         pgoff_t         indices[PAGEVEC_SIZE];
255         pgoff_t         index;
256         int             i;
257
258         cleancache_invalidate_inode(mapping);
259         if (mapping->nrpages == 0 && mapping->nrshadows == 0)
260                 return;
261
262         /* Offsets within partial pages */
263         partial_start = lstart & (PAGE_CACHE_SIZE - 1);
264         partial_end = (lend + 1) & (PAGE_CACHE_SIZE - 1);
265
266         /*
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.
271          */
272         start = (lstart + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
273         if (lend == -1)
274                 /*
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.
278                  */
279                 end = -1;
280         else
281                 end = (lend + 1) >> PAGE_CACHE_SHIFT;
282
283         pagevec_init(&pvec, 0);
284         index = start;
285         while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
286                         min(end - index, (pgoff_t)PAGEVEC_SIZE),
287                         indices)) {
288                 for (i = 0; i < pagevec_count(&pvec); i++) {
289                         struct page *page = pvec.pages[i];
290
291                         /* We rely upon deletion not changing page->index */
292                         index = indices[i];
293                         if (index >= end)
294                                 break;
295
296                         if (radix_tree_exceptional_entry(page)) {
297                                 clear_exceptional_entry(mapping, index, page);
298                                 continue;
299                         }
300
301                         if (!trylock_page(page))
302                                 continue;
303                         WARN_ON(page->index != index);
304                         if (PageWriteback(page)) {
305                                 unlock_page(page);
306                                 continue;
307                         }
308                         truncate_inode_page(mapping, page);
309                         unlock_page(page);
310                 }
311                 pagevec_remove_exceptionals(&pvec);
312                 pagevec_release(&pvec);
313                 cond_resched();
314                 index++;
315         }
316
317         if (partial_start) {
318                 struct page *page = find_lock_page(mapping, start - 1);
319                 if (page) {
320                         unsigned int top = PAGE_CACHE_SIZE;
321                         if (start > end) {
322                                 /* Truncation within a single page */
323                                 top = partial_end;
324                                 partial_end = 0;
325                         }
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);
332                         unlock_page(page);
333                         page_cache_release(page);
334                 }
335         }
336         if (partial_end) {
337                 struct page *page = find_lock_page(mapping, end);
338                 if (page) {
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,
344                                                   partial_end);
345                         unlock_page(page);
346                         page_cache_release(page);
347                 }
348         }
349         /*
350          * If the truncation happened within a single page no pages
351          * will be released, just zeroed, so we can bail out now.
352          */
353         if (start >= end)
354                 return;
355
356         index = start;
357         for ( ; ; ) {
358                 cond_resched();
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 */
362                         if (index == start)
363                                 break;
364                         /* Otherwise restart to make sure all gone */
365                         index = start;
366                         continue;
367                 }
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);
372                         break;
373                 }
374                 for (i = 0; i < pagevec_count(&pvec); i++) {
375                         struct page *page = pvec.pages[i];
376
377                         /* We rely upon deletion not changing page->index */
378                         index = indices[i];
379                         if (index >= end) {
380                                 /* Restart punch to make sure all gone */
381                                 index = start - 1;
382                                 break;
383                         }
384
385                         if (radix_tree_exceptional_entry(page)) {
386                                 clear_exceptional_entry(mapping, index, page);
387                                 continue;
388                         }
389
390                         lock_page(page);
391                         WARN_ON(page->index != index);
392                         wait_on_page_writeback(page);
393                         truncate_inode_page(mapping, page);
394                         unlock_page(page);
395                 }
396                 pagevec_remove_exceptionals(&pvec);
397                 pagevec_release(&pvec);
398                 index++;
399         }
400         cleancache_invalidate_inode(mapping);
401 }
402 EXPORT_SYMBOL(truncate_inode_pages_range);
403
404 /**
405  * truncate_inode_pages - truncate *all* the pages from an offset
406  * @mapping: mapping to truncate
407  * @lstart: offset from which to truncate
408  *
409  * Called under (and serialised by) inode->i_mutex.
410  *
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.
415  */
416 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
417 {
418         truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
419 }
420 EXPORT_SYMBOL(truncate_inode_pages);
421
422 /**
423  * truncate_inode_pages_final - truncate *all* pages before inode dies
424  * @mapping: mapping to truncate
425  *
426  * Called under (and serialized by) inode->i_mutex.
427  *
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.
430  */
431 void truncate_inode_pages_final(struct address_space *mapping)
432 {
433         unsigned long nrshadows;
434         unsigned long nrpages;
435
436         /*
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.
442          */
443         mapping_set_exiting(mapping);
444
445         /*
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.
449          */
450         nrpages = mapping->nrpages;
451         smp_rmb();
452         nrshadows = mapping->nrshadows;
453
454         if (nrpages || nrshadows) {
455                 /*
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.
460                  */
461                 spin_lock_irq(&mapping->tree_lock);
462                 spin_unlock_irq(&mapping->tree_lock);
463
464                 truncate_inode_pages(mapping, 0);
465         }
466 }
467 EXPORT_SYMBOL(truncate_inode_pages_final);
468
469 /**
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)
474  *
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.
477  *
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
480  * pagetables.
481  */
482 unsigned long invalidate_mapping_pages(struct address_space *mapping,
483                 pgoff_t start, pgoff_t end)
484 {
485         pgoff_t indices[PAGEVEC_SIZE];
486         struct pagevec pvec;
487         pgoff_t index = start;
488         unsigned long ret;
489         unsigned long count = 0;
490         int i;
491
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,
495                         indices)) {
496                 for (i = 0; i < pagevec_count(&pvec); i++) {
497                         struct page *page = pvec.pages[i];
498
499                         /* We rely upon deletion not changing page->index */
500                         index = indices[i];
501                         if (index > end)
502                                 break;
503
504                         if (radix_tree_exceptional_entry(page)) {
505                                 clear_exceptional_entry(mapping, index, page);
506                                 continue;
507                         }
508
509                         if (!trylock_page(page))
510                                 continue;
511                         WARN_ON(page->index != index);
512                         ret = invalidate_inode_page(page);
513                         unlock_page(page);
514                         /*
515                          * Invalidation is a hint that the page is no longer
516                          * of interest and try to speed up its reclaim.
517                          */
518                         if (!ret)
519                                 deactivate_page(page);
520                         count += ret;
521                 }
522                 pagevec_remove_exceptionals(&pvec);
523                 pagevec_release(&pvec);
524                 cond_resched();
525                 index++;
526         }
527         return count;
528 }
529 EXPORT_SYMBOL(invalidate_mapping_pages);
530
531 /*
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.
537  */
538 static int
539 invalidate_complete_page2(struct address_space *mapping, struct page *page)
540 {
541         if (page->mapping != mapping)
542                 return 0;
543
544         if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
545                 return 0;
546
547         spin_lock_irq(&mapping->tree_lock);
548         if (PageDirty(page))
549                 goto failed;
550
551         BUG_ON(page_has_private(page));
552         __delete_from_page_cache(page, NULL);
553         spin_unlock_irq(&mapping->tree_lock);
554
555         if (mapping->a_ops->freepage)
556                 mapping->a_ops->freepage(page);
557
558         page_cache_release(page);       /* pagecache ref */
559         return 1;
560 failed:
561         spin_unlock_irq(&mapping->tree_lock);
562         return 0;
563 }
564
565 static int do_launder_page(struct address_space *mapping, struct page *page)
566 {
567         if (!PageDirty(page))
568                 return 0;
569         if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
570                 return 0;
571         return mapping->a_ops->launder_page(page);
572 }
573
574 /**
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)
579  *
580  * Any pages which are found to be mapped into pagetables are unmapped prior to
581  * invalidation.
582  *
583  * Returns -EBUSY if any pages could not be invalidated.
584  */
585 int invalidate_inode_pages2_range(struct address_space *mapping,
586                                   pgoff_t start, pgoff_t end)
587 {
588         pgoff_t indices[PAGEVEC_SIZE];
589         struct pagevec pvec;
590         pgoff_t index;
591         int i;
592         int ret = 0;
593         int ret2 = 0;
594         int did_range_unmap = 0;
595
596         cleancache_invalidate_inode(mapping);
597         pagevec_init(&pvec, 0);
598         index = start;
599         while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
600                         min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
601                         indices)) {
602                 for (i = 0; i < pagevec_count(&pvec); i++) {
603                         struct page *page = pvec.pages[i];
604
605                         /* We rely upon deletion not changing page->index */
606                         index = indices[i];
607                         if (index > end)
608                                 break;
609
610                         if (radix_tree_exceptional_entry(page)) {
611                                 clear_exceptional_entry(mapping, index, page);
612                                 continue;
613                         }
614
615                         lock_page(page);
616                         WARN_ON(page->index != index);
617                         if (page->mapping != mapping) {
618                                 unlock_page(page);
619                                 continue;
620                         }
621                         wait_on_page_writeback(page);
622                         if (page_mapped(page)) {
623                                 if (!did_range_unmap) {
624                                         /*
625                                          * Zap the rest of the file in one hit.
626                                          */
627                                         unmap_mapping_range(mapping,
628                                            (loff_t)index << PAGE_CACHE_SHIFT,
629                                            (loff_t)(1 + end - index)
630                                                          << PAGE_CACHE_SHIFT,
631                                             0);
632                                         did_range_unmap = 1;
633                                 } else {
634                                         /*
635                                          * Just zap this page
636                                          */
637                                         unmap_mapping_range(mapping,
638                                            (loff_t)index << PAGE_CACHE_SHIFT,
639                                            PAGE_CACHE_SIZE, 0);
640                                 }
641                         }
642                         BUG_ON(page_mapped(page));
643                         ret2 = do_launder_page(mapping, page);
644                         if (ret2 == 0) {
645                                 if (!invalidate_complete_page2(mapping, page))
646                                         ret2 = -EBUSY;
647                         }
648                         if (ret2 < 0)
649                                 ret = ret2;
650                         unlock_page(page);
651                 }
652                 pagevec_remove_exceptionals(&pvec);
653                 pagevec_release(&pvec);
654                 cond_resched();
655                 index++;
656         }
657         cleancache_invalidate_inode(mapping);
658         return ret;
659 }
660 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
661
662 /**
663  * invalidate_inode_pages2 - remove all pages from an address_space
664  * @mapping: the address_space
665  *
666  * Any pages which are found to be mapped into pagetables are unmapped prior to
667  * invalidation.
668  *
669  * Returns -EBUSY if any pages could not be invalidated.
670  */
671 int invalidate_inode_pages2(struct address_space *mapping)
672 {
673         return invalidate_inode_pages2_range(mapping, 0, -1);
674 }
675 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
676
677 /**
678  * truncate_pagecache - unmap and remove pagecache that has been truncated
679  * @inode: inode
680  * @newsize: new file size
681  *
682  * inode's new i_size must already be written before truncate_pagecache
683  * is called.
684  *
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.
691  */
692 void truncate_pagecache(struct inode *inode, loff_t newsize)
693 {
694         struct address_space *mapping = inode->i_mapping;
695         loff_t holebegin = round_up(newsize, PAGE_SIZE);
696
697         /*
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.
705          */
706         unmap_mapping_range(mapping, holebegin, 0, 1);
707         truncate_inode_pages(mapping, newsize);
708         unmap_mapping_range(mapping, holebegin, 0, 1);
709 }
710 EXPORT_SYMBOL(truncate_pagecache);
711
712 /**
713  * truncate_setsize - update inode and pagecache for a new file size
714  * @inode: inode
715  * @newsize: new file size
716  *
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.
720  *
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.
724  */
725 void truncate_setsize(struct inode *inode, loff_t newsize)
726 {
727         loff_t oldsize = inode->i_size;
728
729         i_size_write(inode, newsize);
730         if (newsize > oldsize)
731                 pagecache_isize_extended(inode, oldsize, newsize);
732         truncate_pagecache(inode, newsize);
733 }
734 EXPORT_SYMBOL(truncate_setsize);
735
736 /**
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
741  *
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
747  * changed.
748  *
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.
754  */
755 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
756 {
757         int bsize = 1 << inode->i_blkbits;
758         loff_t rounded_from;
759         struct page *page;
760         pgoff_t index;
761
762         WARN_ON(to > inode->i_size);
763
764         if (from >= to || bsize == PAGE_CACHE_SIZE)
765                 return;
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)))
769                 return;
770
771         index = from >> PAGE_CACHE_SHIFT;
772         page = find_lock_page(inode->i_mapping, index);
773         /* Page not cached? Nothing to do */
774         if (!page)
775                 return;
776         /*
777          * See clear_page_dirty_for_io() for details why set_page_dirty()
778          * is needed.
779          */
780         if (page_mkclean(page))
781                 set_page_dirty(page);
782         unlock_page(page);
783         page_cache_release(page);
784 }
785 EXPORT_SYMBOL(pagecache_isize_extended);
786
787 /**
788  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
789  * @inode: inode
790  * @lstart: offset of beginning of hole
791  * @lend: offset of last byte of hole
792  *
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.
799  */
800 void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
801 {
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;
805         /*
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.
811          */
812
813         /*
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.
817          */
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);
822 }
823 EXPORT_SYMBOL(truncate_pagecache_range);