Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * mm/truncate.c - code for taking down pages from address_spaces
0004  *
0005  * Copyright (C) 2002, Linus Torvalds
0006  *
0007  * 10Sep2002    Andrew Morton
0008  *      Initial version.
0009  */
0010 
0011 #include <linux/kernel.h>
0012 #include <linux/backing-dev.h>
0013 #include <linux/dax.h>
0014 #include <linux/gfp.h>
0015 #include <linux/mm.h>
0016 #include <linux/swap.h>
0017 #include <linux/export.h>
0018 #include <linux/pagemap.h>
0019 #include <linux/highmem.h>
0020 #include <linux/pagevec.h>
0021 #include <linux/task_io_accounting_ops.h>
0022 #include <linux/buffer_head.h>  /* grr. try_to_release_page */
0023 #include <linux/shmem_fs.h>
0024 #include <linux/rmap.h>
0025 #include "internal.h"
0026 
0027 /*
0028  * Regular page slots are stabilized by the page lock even without the tree
0029  * itself locked.  These unlocked entries need verification under the tree
0030  * lock.
0031  */
0032 static inline void __clear_shadow_entry(struct address_space *mapping,
0033                 pgoff_t index, void *entry)
0034 {
0035     XA_STATE(xas, &mapping->i_pages, index);
0036 
0037     xas_set_update(&xas, workingset_update_node);
0038     if (xas_load(&xas) != entry)
0039         return;
0040     xas_store(&xas, NULL);
0041 }
0042 
0043 static void clear_shadow_entry(struct address_space *mapping, pgoff_t index,
0044                    void *entry)
0045 {
0046     spin_lock(&mapping->host->i_lock);
0047     xa_lock_irq(&mapping->i_pages);
0048     __clear_shadow_entry(mapping, index, entry);
0049     xa_unlock_irq(&mapping->i_pages);
0050     if (mapping_shrinkable(mapping))
0051         inode_add_lru(mapping->host);
0052     spin_unlock(&mapping->host->i_lock);
0053 }
0054 
0055 /*
0056  * Unconditionally remove exceptional entries. Usually called from truncate
0057  * path. Note that the folio_batch may be altered by this function by removing
0058  * exceptional entries similar to what folio_batch_remove_exceptionals() does.
0059  */
0060 static void truncate_folio_batch_exceptionals(struct address_space *mapping,
0061                 struct folio_batch *fbatch, pgoff_t *indices)
0062 {
0063     int i, j;
0064     bool dax;
0065 
0066     /* Handled by shmem itself */
0067     if (shmem_mapping(mapping))
0068         return;
0069 
0070     for (j = 0; j < folio_batch_count(fbatch); j++)
0071         if (xa_is_value(fbatch->folios[j]))
0072             break;
0073 
0074     if (j == folio_batch_count(fbatch))
0075         return;
0076 
0077     dax = dax_mapping(mapping);
0078     if (!dax) {
0079         spin_lock(&mapping->host->i_lock);
0080         xa_lock_irq(&mapping->i_pages);
0081     }
0082 
0083     for (i = j; i < folio_batch_count(fbatch); i++) {
0084         struct folio *folio = fbatch->folios[i];
0085         pgoff_t index = indices[i];
0086 
0087         if (!xa_is_value(folio)) {
0088             fbatch->folios[j++] = folio;
0089             continue;
0090         }
0091 
0092         if (unlikely(dax)) {
0093             dax_delete_mapping_entry(mapping, index);
0094             continue;
0095         }
0096 
0097         __clear_shadow_entry(mapping, index, folio);
0098     }
0099 
0100     if (!dax) {
0101         xa_unlock_irq(&mapping->i_pages);
0102         if (mapping_shrinkable(mapping))
0103             inode_add_lru(mapping->host);
0104         spin_unlock(&mapping->host->i_lock);
0105     }
0106     fbatch->nr = j;
0107 }
0108 
0109 /*
0110  * Invalidate exceptional entry if easily possible. This handles exceptional
0111  * entries for invalidate_inode_pages().
0112  */
0113 static int invalidate_exceptional_entry(struct address_space *mapping,
0114                     pgoff_t index, void *entry)
0115 {
0116     /* Handled by shmem itself, or for DAX we do nothing. */
0117     if (shmem_mapping(mapping) || dax_mapping(mapping))
0118         return 1;
0119     clear_shadow_entry(mapping, index, entry);
0120     return 1;
0121 }
0122 
0123 /*
0124  * Invalidate exceptional entry if clean. This handles exceptional entries for
0125  * invalidate_inode_pages2() so for DAX it evicts only clean entries.
0126  */
0127 static int invalidate_exceptional_entry2(struct address_space *mapping,
0128                      pgoff_t index, void *entry)
0129 {
0130     /* Handled by shmem itself */
0131     if (shmem_mapping(mapping))
0132         return 1;
0133     if (dax_mapping(mapping))
0134         return dax_invalidate_mapping_entry_sync(mapping, index);
0135     clear_shadow_entry(mapping, index, entry);
0136     return 1;
0137 }
0138 
0139 /**
0140  * folio_invalidate - Invalidate part or all of a folio.
0141  * @folio: The folio which is affected.
0142  * @offset: start of the range to invalidate
0143  * @length: length of the range to invalidate
0144  *
0145  * folio_invalidate() is called when all or part of the folio has become
0146  * invalidated by a truncate operation.
0147  *
0148  * folio_invalidate() does not have to release all buffers, but it must
0149  * ensure that no dirty buffer is left outside @offset and that no I/O
0150  * is underway against any of the blocks which are outside the truncation
0151  * point.  Because the caller is about to free (and possibly reuse) those
0152  * blocks on-disk.
0153  */
0154 void folio_invalidate(struct folio *folio, size_t offset, size_t length)
0155 {
0156     const struct address_space_operations *aops = folio->mapping->a_ops;
0157 
0158     if (aops->invalidate_folio)
0159         aops->invalidate_folio(folio, offset, length);
0160 }
0161 EXPORT_SYMBOL_GPL(folio_invalidate);
0162 
0163 /*
0164  * If truncate cannot remove the fs-private metadata from the page, the page
0165  * becomes orphaned.  It will be left on the LRU and may even be mapped into
0166  * user pagetables if we're racing with filemap_fault().
0167  *
0168  * We need to bail out if page->mapping is no longer equal to the original
0169  * mapping.  This happens a) when the VM reclaimed the page while we waited on
0170  * its lock, b) when a concurrent invalidate_mapping_pages got there first and
0171  * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
0172  */
0173 static void truncate_cleanup_folio(struct folio *folio)
0174 {
0175     if (folio_mapped(folio))
0176         unmap_mapping_folio(folio);
0177 
0178     if (folio_has_private(folio))
0179         folio_invalidate(folio, 0, folio_size(folio));
0180 
0181     /*
0182      * Some filesystems seem to re-dirty the page even after
0183      * the VM has canceled the dirty bit (eg ext3 journaling).
0184      * Hence dirty accounting check is placed after invalidation.
0185      */
0186     folio_cancel_dirty(folio);
0187     folio_clear_mappedtodisk(folio);
0188 }
0189 
0190 int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
0191 {
0192     if (folio->mapping != mapping)
0193         return -EIO;
0194 
0195     truncate_cleanup_folio(folio);
0196     filemap_remove_folio(folio);
0197     return 0;
0198 }
0199 
0200 /*
0201  * Handle partial folios.  The folio may be entirely within the
0202  * range if a split has raced with us.  If not, we zero the part of the
0203  * folio that's within the [start, end] range, and then split the folio if
0204  * it's large.  split_page_range() will discard pages which now lie beyond
0205  * i_size, and we rely on the caller to discard pages which lie within a
0206  * newly created hole.
0207  *
0208  * Returns false if splitting failed so the caller can avoid
0209  * discarding the entire folio which is stubbornly unsplit.
0210  */
0211 bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
0212 {
0213     loff_t pos = folio_pos(folio);
0214     unsigned int offset, length;
0215 
0216     if (pos < start)
0217         offset = start - pos;
0218     else
0219         offset = 0;
0220     length = folio_size(folio);
0221     if (pos + length <= (u64)end)
0222         length = length - offset;
0223     else
0224         length = end + 1 - pos - offset;
0225 
0226     folio_wait_writeback(folio);
0227     if (length == folio_size(folio)) {
0228         truncate_inode_folio(folio->mapping, folio);
0229         return true;
0230     }
0231 
0232     /*
0233      * We may be zeroing pages we're about to discard, but it avoids
0234      * doing a complex calculation here, and then doing the zeroing
0235      * anyway if the page split fails.
0236      */
0237     folio_zero_range(folio, offset, length);
0238 
0239     if (folio_has_private(folio))
0240         folio_invalidate(folio, offset, length);
0241     if (!folio_test_large(folio))
0242         return true;
0243     if (split_huge_page(&folio->page) == 0)
0244         return true;
0245     if (folio_test_dirty(folio))
0246         return false;
0247     truncate_inode_folio(folio->mapping, folio);
0248     return true;
0249 }
0250 
0251 /*
0252  * Used to get rid of pages on hardware memory corruption.
0253  */
0254 int generic_error_remove_page(struct address_space *mapping, struct page *page)
0255 {
0256     VM_BUG_ON_PAGE(PageTail(page), page);
0257 
0258     if (!mapping)
0259         return -EINVAL;
0260     /*
0261      * Only punch for normal data pages for now.
0262      * Handling other types like directories would need more auditing.
0263      */
0264     if (!S_ISREG(mapping->host->i_mode))
0265         return -EIO;
0266     return truncate_inode_folio(mapping, page_folio(page));
0267 }
0268 EXPORT_SYMBOL(generic_error_remove_page);
0269 
0270 static long mapping_evict_folio(struct address_space *mapping,
0271         struct folio *folio)
0272 {
0273     if (folio_test_dirty(folio) || folio_test_writeback(folio))
0274         return 0;
0275     /* The refcount will be elevated if any page in the folio is mapped */
0276     if (folio_ref_count(folio) >
0277             folio_nr_pages(folio) + folio_has_private(folio) + 1)
0278         return 0;
0279     if (folio_has_private(folio) && !filemap_release_folio(folio, 0))
0280         return 0;
0281 
0282     return remove_mapping(mapping, folio);
0283 }
0284 
0285 /**
0286  * invalidate_inode_page() - Remove an unused page from the pagecache.
0287  * @page: The page to remove.
0288  *
0289  * Safely invalidate one page from its pagecache mapping.
0290  * It only drops clean, unused pages.
0291  *
0292  * Context: Page must be locked.
0293  * Return: The number of pages successfully removed.
0294  */
0295 long invalidate_inode_page(struct page *page)
0296 {
0297     struct folio *folio = page_folio(page);
0298     struct address_space *mapping = folio_mapping(folio);
0299 
0300     /* The page may have been truncated before it was locked */
0301     if (!mapping)
0302         return 0;
0303     return mapping_evict_folio(mapping, folio);
0304 }
0305 
0306 /**
0307  * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
0308  * @mapping: mapping to truncate
0309  * @lstart: offset from which to truncate
0310  * @lend: offset to which to truncate (inclusive)
0311  *
0312  * Truncate the page cache, removing the pages that are between
0313  * specified offsets (and zeroing out partial pages
0314  * if lstart or lend + 1 is not page aligned).
0315  *
0316  * Truncate takes two passes - the first pass is nonblocking.  It will not
0317  * block on page locks and it will not block on writeback.  The second pass
0318  * will wait.  This is to prevent as much IO as possible in the affected region.
0319  * The first pass will remove most pages, so the search cost of the second pass
0320  * is low.
0321  *
0322  * We pass down the cache-hot hint to the page freeing code.  Even if the
0323  * mapping is large, it is probably the case that the final pages are the most
0324  * recently touched, and freeing happens in ascending file offset order.
0325  *
0326  * Note that since ->invalidate_folio() accepts range to invalidate
0327  * truncate_inode_pages_range is able to handle cases where lend + 1 is not
0328  * page aligned properly.
0329  */
0330 void truncate_inode_pages_range(struct address_space *mapping,
0331                 loff_t lstart, loff_t lend)
0332 {
0333     pgoff_t     start;      /* inclusive */
0334     pgoff_t     end;        /* exclusive */
0335     struct folio_batch fbatch;
0336     pgoff_t     indices[PAGEVEC_SIZE];
0337     pgoff_t     index;
0338     int     i;
0339     struct folio    *folio;
0340     bool        same_folio;
0341 
0342     if (mapping_empty(mapping))
0343         return;
0344 
0345     /*
0346      * 'start' and 'end' always covers the range of pages to be fully
0347      * truncated. Partial pages are covered with 'partial_start' at the
0348      * start of the range and 'partial_end' at the end of the range.
0349      * Note that 'end' is exclusive while 'lend' is inclusive.
0350      */
0351     start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
0352     if (lend == -1)
0353         /*
0354          * lend == -1 indicates end-of-file so we have to set 'end'
0355          * to the highest possible pgoff_t and since the type is
0356          * unsigned we're using -1.
0357          */
0358         end = -1;
0359     else
0360         end = (lend + 1) >> PAGE_SHIFT;
0361 
0362     folio_batch_init(&fbatch);
0363     index = start;
0364     while (index < end && find_lock_entries(mapping, index, end - 1,
0365             &fbatch, indices)) {
0366         index = indices[folio_batch_count(&fbatch) - 1] + 1;
0367         truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
0368         for (i = 0; i < folio_batch_count(&fbatch); i++)
0369             truncate_cleanup_folio(fbatch.folios[i]);
0370         delete_from_page_cache_batch(mapping, &fbatch);
0371         for (i = 0; i < folio_batch_count(&fbatch); i++)
0372             folio_unlock(fbatch.folios[i]);
0373         folio_batch_release(&fbatch);
0374         cond_resched();
0375     }
0376 
0377     same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
0378     folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
0379     if (folio) {
0380         same_folio = lend < folio_pos(folio) + folio_size(folio);
0381         if (!truncate_inode_partial_folio(folio, lstart, lend)) {
0382             start = folio->index + folio_nr_pages(folio);
0383             if (same_folio)
0384                 end = folio->index;
0385         }
0386         folio_unlock(folio);
0387         folio_put(folio);
0388         folio = NULL;
0389     }
0390 
0391     if (!same_folio)
0392         folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
0393                         FGP_LOCK, 0);
0394     if (folio) {
0395         if (!truncate_inode_partial_folio(folio, lstart, lend))
0396             end = folio->index;
0397         folio_unlock(folio);
0398         folio_put(folio);
0399     }
0400 
0401     index = start;
0402     while (index < end) {
0403         cond_resched();
0404         if (!find_get_entries(mapping, index, end - 1, &fbatch,
0405                 indices)) {
0406             /* If all gone from start onwards, we're done */
0407             if (index == start)
0408                 break;
0409             /* Otherwise restart to make sure all gone */
0410             index = start;
0411             continue;
0412         }
0413 
0414         for (i = 0; i < folio_batch_count(&fbatch); i++) {
0415             struct folio *folio = fbatch.folios[i];
0416 
0417             /* We rely upon deletion not changing page->index */
0418             index = indices[i];
0419 
0420             if (xa_is_value(folio))
0421                 continue;
0422 
0423             folio_lock(folio);
0424             VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
0425             folio_wait_writeback(folio);
0426             truncate_inode_folio(mapping, folio);
0427             folio_unlock(folio);
0428             index = folio_index(folio) + folio_nr_pages(folio) - 1;
0429         }
0430         truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
0431         folio_batch_release(&fbatch);
0432         index++;
0433     }
0434 }
0435 EXPORT_SYMBOL(truncate_inode_pages_range);
0436 
0437 /**
0438  * truncate_inode_pages - truncate *all* the pages from an offset
0439  * @mapping: mapping to truncate
0440  * @lstart: offset from which to truncate
0441  *
0442  * Called under (and serialised by) inode->i_rwsem and
0443  * mapping->invalidate_lock.
0444  *
0445  * Note: When this function returns, there can be a page in the process of
0446  * deletion (inside __filemap_remove_folio()) in the specified range.  Thus
0447  * mapping->nrpages can be non-zero when this function returns even after
0448  * truncation of the whole mapping.
0449  */
0450 void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
0451 {
0452     truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
0453 }
0454 EXPORT_SYMBOL(truncate_inode_pages);
0455 
0456 /**
0457  * truncate_inode_pages_final - truncate *all* pages before inode dies
0458  * @mapping: mapping to truncate
0459  *
0460  * Called under (and serialized by) inode->i_rwsem.
0461  *
0462  * Filesystems have to use this in the .evict_inode path to inform the
0463  * VM that this is the final truncate and the inode is going away.
0464  */
0465 void truncate_inode_pages_final(struct address_space *mapping)
0466 {
0467     /*
0468      * Page reclaim can not participate in regular inode lifetime
0469      * management (can't call iput()) and thus can race with the
0470      * inode teardown.  Tell it when the address space is exiting,
0471      * so that it does not install eviction information after the
0472      * final truncate has begun.
0473      */
0474     mapping_set_exiting(mapping);
0475 
0476     if (!mapping_empty(mapping)) {
0477         /*
0478          * As truncation uses a lockless tree lookup, cycle
0479          * the tree lock to make sure any ongoing tree
0480          * modification that does not see AS_EXITING is
0481          * completed before starting the final truncate.
0482          */
0483         xa_lock_irq(&mapping->i_pages);
0484         xa_unlock_irq(&mapping->i_pages);
0485     }
0486 
0487     truncate_inode_pages(mapping, 0);
0488 }
0489 EXPORT_SYMBOL(truncate_inode_pages_final);
0490 
0491 /**
0492  * invalidate_mapping_pagevec - Invalidate all the unlocked pages of one inode
0493  * @mapping: the address_space which holds the pages to invalidate
0494  * @start: the offset 'from' which to invalidate
0495  * @end: the offset 'to' which to invalidate (inclusive)
0496  * @nr_pagevec: invalidate failed page number for caller
0497  *
0498  * This helper is similar to invalidate_mapping_pages(), except that it accounts
0499  * for pages that are likely on a pagevec and counts them in @nr_pagevec, which
0500  * will be used by the caller.
0501  */
0502 unsigned long invalidate_mapping_pagevec(struct address_space *mapping,
0503         pgoff_t start, pgoff_t end, unsigned long *nr_pagevec)
0504 {
0505     pgoff_t indices[PAGEVEC_SIZE];
0506     struct folio_batch fbatch;
0507     pgoff_t index = start;
0508     unsigned long ret;
0509     unsigned long count = 0;
0510     int i;
0511 
0512     folio_batch_init(&fbatch);
0513     while (find_lock_entries(mapping, index, end, &fbatch, indices)) {
0514         for (i = 0; i < folio_batch_count(&fbatch); i++) {
0515             struct folio *folio = fbatch.folios[i];
0516 
0517             /* We rely upon deletion not changing folio->index */
0518             index = indices[i];
0519 
0520             if (xa_is_value(folio)) {
0521                 count += invalidate_exceptional_entry(mapping,
0522                                       index,
0523                                       folio);
0524                 continue;
0525             }
0526             index += folio_nr_pages(folio) - 1;
0527 
0528             ret = mapping_evict_folio(mapping, folio);
0529             folio_unlock(folio);
0530             /*
0531              * Invalidation is a hint that the folio is no longer
0532              * of interest and try to speed up its reclaim.
0533              */
0534             if (!ret) {
0535                 deactivate_file_folio(folio);
0536                 /* It is likely on the pagevec of a remote CPU */
0537                 if (nr_pagevec)
0538                     (*nr_pagevec)++;
0539             }
0540             count += ret;
0541         }
0542         folio_batch_remove_exceptionals(&fbatch);
0543         folio_batch_release(&fbatch);
0544         cond_resched();
0545         index++;
0546     }
0547     return count;
0548 }
0549 
0550 /**
0551  * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode
0552  * @mapping: the address_space which holds the cache to invalidate
0553  * @start: the offset 'from' which to invalidate
0554  * @end: the offset 'to' which to invalidate (inclusive)
0555  *
0556  * This function removes pages that are clean, unmapped and unlocked,
0557  * as well as shadow entries. It will not block on IO activity.
0558  *
0559  * If you want to remove all the pages of one inode, regardless of
0560  * their use and writeback state, use truncate_inode_pages().
0561  *
0562  * Return: the number of the cache entries that were invalidated
0563  */
0564 unsigned long invalidate_mapping_pages(struct address_space *mapping,
0565         pgoff_t start, pgoff_t end)
0566 {
0567     return invalidate_mapping_pagevec(mapping, start, end, NULL);
0568 }
0569 EXPORT_SYMBOL(invalidate_mapping_pages);
0570 
0571 /*
0572  * This is like invalidate_inode_page(), except it ignores the page's
0573  * refcount.  We do this because invalidate_inode_pages2() needs stronger
0574  * invalidation guarantees, and cannot afford to leave pages behind because
0575  * shrink_page_list() has a temp ref on them, or because they're transiently
0576  * sitting in the lru_cache_add() pagevecs.
0577  */
0578 static int invalidate_complete_folio2(struct address_space *mapping,
0579                     struct folio *folio)
0580 {
0581     if (folio->mapping != mapping)
0582         return 0;
0583 
0584     if (folio_has_private(folio) &&
0585         !filemap_release_folio(folio, GFP_KERNEL))
0586         return 0;
0587 
0588     spin_lock(&mapping->host->i_lock);
0589     xa_lock_irq(&mapping->i_pages);
0590     if (folio_test_dirty(folio))
0591         goto failed;
0592 
0593     BUG_ON(folio_has_private(folio));
0594     __filemap_remove_folio(folio, NULL);
0595     xa_unlock_irq(&mapping->i_pages);
0596     if (mapping_shrinkable(mapping))
0597         inode_add_lru(mapping->host);
0598     spin_unlock(&mapping->host->i_lock);
0599 
0600     filemap_free_folio(mapping, folio);
0601     return 1;
0602 failed:
0603     xa_unlock_irq(&mapping->i_pages);
0604     spin_unlock(&mapping->host->i_lock);
0605     return 0;
0606 }
0607 
0608 static int folio_launder(struct address_space *mapping, struct folio *folio)
0609 {
0610     if (!folio_test_dirty(folio))
0611         return 0;
0612     if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
0613         return 0;
0614     return mapping->a_ops->launder_folio(folio);
0615 }
0616 
0617 /**
0618  * invalidate_inode_pages2_range - remove range of pages from an address_space
0619  * @mapping: the address_space
0620  * @start: the page offset 'from' which to invalidate
0621  * @end: the page offset 'to' which to invalidate (inclusive)
0622  *
0623  * Any pages which are found to be mapped into pagetables are unmapped prior to
0624  * invalidation.
0625  *
0626  * Return: -EBUSY if any pages could not be invalidated.
0627  */
0628 int invalidate_inode_pages2_range(struct address_space *mapping,
0629                   pgoff_t start, pgoff_t end)
0630 {
0631     pgoff_t indices[PAGEVEC_SIZE];
0632     struct folio_batch fbatch;
0633     pgoff_t index;
0634     int i;
0635     int ret = 0;
0636     int ret2 = 0;
0637     int did_range_unmap = 0;
0638 
0639     if (mapping_empty(mapping))
0640         return 0;
0641 
0642     folio_batch_init(&fbatch);
0643     index = start;
0644     while (find_get_entries(mapping, index, end, &fbatch, indices)) {
0645         for (i = 0; i < folio_batch_count(&fbatch); i++) {
0646             struct folio *folio = fbatch.folios[i];
0647 
0648             /* We rely upon deletion not changing folio->index */
0649             index = indices[i];
0650 
0651             if (xa_is_value(folio)) {
0652                 if (!invalidate_exceptional_entry2(mapping,
0653                         index, folio))
0654                     ret = -EBUSY;
0655                 continue;
0656             }
0657 
0658             if (!did_range_unmap && folio_mapped(folio)) {
0659                 /*
0660                  * If folio is mapped, before taking its lock,
0661                  * zap the rest of the file in one hit.
0662                  */
0663                 unmap_mapping_pages(mapping, index,
0664                         (1 + end - index), false);
0665                 did_range_unmap = 1;
0666             }
0667 
0668             folio_lock(folio);
0669             VM_BUG_ON_FOLIO(!folio_contains(folio, index), folio);
0670             if (folio->mapping != mapping) {
0671                 folio_unlock(folio);
0672                 continue;
0673             }
0674             folio_wait_writeback(folio);
0675 
0676             if (folio_mapped(folio))
0677                 unmap_mapping_folio(folio);
0678             BUG_ON(folio_mapped(folio));
0679 
0680             ret2 = folio_launder(mapping, folio);
0681             if (ret2 == 0) {
0682                 if (!invalidate_complete_folio2(mapping, folio))
0683                     ret2 = -EBUSY;
0684             }
0685             if (ret2 < 0)
0686                 ret = ret2;
0687             folio_unlock(folio);
0688         }
0689         folio_batch_remove_exceptionals(&fbatch);
0690         folio_batch_release(&fbatch);
0691         cond_resched();
0692         index++;
0693     }
0694     /*
0695      * For DAX we invalidate page tables after invalidating page cache.  We
0696      * could invalidate page tables while invalidating each entry however
0697      * that would be expensive. And doing range unmapping before doesn't
0698      * work as we have no cheap way to find whether page cache entry didn't
0699      * get remapped later.
0700      */
0701     if (dax_mapping(mapping)) {
0702         unmap_mapping_pages(mapping, start, end - start + 1, false);
0703     }
0704     return ret;
0705 }
0706 EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
0707 
0708 /**
0709  * invalidate_inode_pages2 - remove all pages from an address_space
0710  * @mapping: the address_space
0711  *
0712  * Any pages which are found to be mapped into pagetables are unmapped prior to
0713  * invalidation.
0714  *
0715  * Return: -EBUSY if any pages could not be invalidated.
0716  */
0717 int invalidate_inode_pages2(struct address_space *mapping)
0718 {
0719     return invalidate_inode_pages2_range(mapping, 0, -1);
0720 }
0721 EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
0722 
0723 /**
0724  * truncate_pagecache - unmap and remove pagecache that has been truncated
0725  * @inode: inode
0726  * @newsize: new file size
0727  *
0728  * inode's new i_size must already be written before truncate_pagecache
0729  * is called.
0730  *
0731  * This function should typically be called before the filesystem
0732  * releases resources associated with the freed range (eg. deallocates
0733  * blocks). This way, pagecache will always stay logically coherent
0734  * with on-disk format, and the filesystem would not have to deal with
0735  * situations such as writepage being called for a page that has already
0736  * had its underlying blocks deallocated.
0737  */
0738 void truncate_pagecache(struct inode *inode, loff_t newsize)
0739 {
0740     struct address_space *mapping = inode->i_mapping;
0741     loff_t holebegin = round_up(newsize, PAGE_SIZE);
0742 
0743     /*
0744      * unmap_mapping_range is called twice, first simply for
0745      * efficiency so that truncate_inode_pages does fewer
0746      * single-page unmaps.  However after this first call, and
0747      * before truncate_inode_pages finishes, it is possible for
0748      * private pages to be COWed, which remain after
0749      * truncate_inode_pages finishes, hence the second
0750      * unmap_mapping_range call must be made for correctness.
0751      */
0752     unmap_mapping_range(mapping, holebegin, 0, 1);
0753     truncate_inode_pages(mapping, newsize);
0754     unmap_mapping_range(mapping, holebegin, 0, 1);
0755 }
0756 EXPORT_SYMBOL(truncate_pagecache);
0757 
0758 /**
0759  * truncate_setsize - update inode and pagecache for a new file size
0760  * @inode: inode
0761  * @newsize: new file size
0762  *
0763  * truncate_setsize updates i_size and performs pagecache truncation (if
0764  * necessary) to @newsize. It will be typically be called from the filesystem's
0765  * setattr function when ATTR_SIZE is passed in.
0766  *
0767  * Must be called with a lock serializing truncates and writes (generally
0768  * i_rwsem but e.g. xfs uses a different lock) and before all filesystem
0769  * specific block truncation has been performed.
0770  */
0771 void truncate_setsize(struct inode *inode, loff_t newsize)
0772 {
0773     loff_t oldsize = inode->i_size;
0774 
0775     i_size_write(inode, newsize);
0776     if (newsize > oldsize)
0777         pagecache_isize_extended(inode, oldsize, newsize);
0778     truncate_pagecache(inode, newsize);
0779 }
0780 EXPORT_SYMBOL(truncate_setsize);
0781 
0782 /**
0783  * pagecache_isize_extended - update pagecache after extension of i_size
0784  * @inode:  inode for which i_size was extended
0785  * @from:   original inode size
0786  * @to:     new inode size
0787  *
0788  * Handle extension of inode size either caused by extending truncate or by
0789  * write starting after current i_size. We mark the page straddling current
0790  * i_size RO so that page_mkwrite() is called on the nearest write access to
0791  * the page.  This way filesystem can be sure that page_mkwrite() is called on
0792  * the page before user writes to the page via mmap after the i_size has been
0793  * changed.
0794  *
0795  * The function must be called after i_size is updated so that page fault
0796  * coming after we unlock the page will already see the new i_size.
0797  * The function must be called while we still hold i_rwsem - this not only
0798  * makes sure i_size is stable but also that userspace cannot observe new
0799  * i_size value before we are prepared to store mmap writes at new inode size.
0800  */
0801 void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
0802 {
0803     int bsize = i_blocksize(inode);
0804     loff_t rounded_from;
0805     struct page *page;
0806     pgoff_t index;
0807 
0808     WARN_ON(to > inode->i_size);
0809 
0810     if (from >= to || bsize == PAGE_SIZE)
0811         return;
0812     /* Page straddling @from will not have any hole block created? */
0813     rounded_from = round_up(from, bsize);
0814     if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
0815         return;
0816 
0817     index = from >> PAGE_SHIFT;
0818     page = find_lock_page(inode->i_mapping, index);
0819     /* Page not cached? Nothing to do */
0820     if (!page)
0821         return;
0822     /*
0823      * See clear_page_dirty_for_io() for details why set_page_dirty()
0824      * is needed.
0825      */
0826     if (page_mkclean(page))
0827         set_page_dirty(page);
0828     unlock_page(page);
0829     put_page(page);
0830 }
0831 EXPORT_SYMBOL(pagecache_isize_extended);
0832 
0833 /**
0834  * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
0835  * @inode: inode
0836  * @lstart: offset of beginning of hole
0837  * @lend: offset of last byte of hole
0838  *
0839  * This function should typically be called before the filesystem
0840  * releases resources associated with the freed range (eg. deallocates
0841  * blocks). This way, pagecache will always stay logically coherent
0842  * with on-disk format, and the filesystem would not have to deal with
0843  * situations such as writepage being called for a page that has already
0844  * had its underlying blocks deallocated.
0845  */
0846 void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
0847 {
0848     struct address_space *mapping = inode->i_mapping;
0849     loff_t unmap_start = round_up(lstart, PAGE_SIZE);
0850     loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
0851     /*
0852      * This rounding is currently just for example: unmap_mapping_range
0853      * expands its hole outwards, whereas we want it to contract the hole
0854      * inwards.  However, existing callers of truncate_pagecache_range are
0855      * doing their own page rounding first.  Note that unmap_mapping_range
0856      * allows holelen 0 for all, and we allow lend -1 for end of file.
0857      */
0858 
0859     /*
0860      * Unlike in truncate_pagecache, unmap_mapping_range is called only
0861      * once (before truncating pagecache), and without "even_cows" flag:
0862      * hole-punching should not remove private COWed pages from the hole.
0863      */
0864     if ((u64)unmap_end > (u64)unmap_start)
0865         unmap_mapping_range(mapping, unmap_start,
0866                     1 + unmap_end - unmap_start, 0);
0867     truncate_inode_pages_range(mapping, lstart, lend);
0868 }
0869 EXPORT_SYMBOL(truncate_pagecache_range);