truncate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * mm/truncate.c - code for taking down pages from address_spaces
  4. *
  5. * Copyright (C) 2002, Linus Torvalds
  6. *
  7. * 10Sep2002 Andrew Morton
  8. * Initial version.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/backing-dev.h>
  12. #include <linux/dax.h>
  13. #include <linux/gfp.h>
  14. #include <linux/mm.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/shmem_fs.h>
  22. #include <linux/rmap.h>
  23. #include "internal.h"
  24. /*
  25. * Regular page slots are stabilized by the page lock even without the tree
  26. * itself locked. These unlocked entries need verification under the tree
  27. * lock.
  28. */
  29. static inline void __clear_shadow_entry(struct address_space *mapping,
  30. pgoff_t index, void *entry)
  31. {
  32. XA_STATE(xas, &mapping->i_pages, index);
  33. xas_set_update(&xas, workingset_update_node);
  34. if (xas_load(&xas) != entry)
  35. return;
  36. xas_store(&xas, NULL);
  37. }
  38. static void clear_shadow_entries(struct address_space *mapping,
  39. struct folio_batch *fbatch, pgoff_t *indices)
  40. {
  41. int i;
  42. /* Handled by shmem itself, or for DAX we do nothing. */
  43. if (shmem_mapping(mapping) || dax_mapping(mapping))
  44. return;
  45. spin_lock(&mapping->host->i_lock);
  46. xa_lock_irq(&mapping->i_pages);
  47. for (i = 0; i < folio_batch_count(fbatch); i++) {
  48. struct folio *folio = fbatch->folios[i];
  49. if (xa_is_value(folio))
  50. __clear_shadow_entry(mapping, indices[i], folio);
  51. }
  52. xa_unlock_irq(&mapping->i_pages);
  53. if (mapping_shrinkable(mapping))
  54. inode_add_lru(mapping->host);
  55. spin_unlock(&mapping->host->i_lock);
  56. }
  57. /*
  58. * Unconditionally remove exceptional entries. Usually called from truncate
  59. * path. Note that the folio_batch may be altered by this function by removing
  60. * exceptional entries similar to what folio_batch_remove_exceptionals() does.
  61. */
  62. static void truncate_folio_batch_exceptionals(struct address_space *mapping,
  63. struct folio_batch *fbatch, pgoff_t *indices)
  64. {
  65. int i, j;
  66. bool dax;
  67. /* Handled by shmem itself */
  68. if (shmem_mapping(mapping))
  69. return;
  70. for (j = 0; j < folio_batch_count(fbatch); j++)
  71. if (xa_is_value(fbatch->folios[j]))
  72. break;
  73. if (j == folio_batch_count(fbatch))
  74. return;
  75. dax = dax_mapping(mapping);
  76. if (!dax) {
  77. spin_lock(&mapping->host->i_lock);
  78. xa_lock_irq(&mapping->i_pages);
  79. }
  80. for (i = j; i < folio_batch_count(fbatch); i++) {
  81. struct folio *folio = fbatch->folios[i];
  82. pgoff_t index = indices[i];
  83. if (!xa_is_value(folio)) {
  84. fbatch->folios[j++] = folio;
  85. continue;
  86. }
  87. if (unlikely(dax)) {
  88. dax_delete_mapping_entry(mapping, index);
  89. continue;
  90. }
  91. __clear_shadow_entry(mapping, index, folio);
  92. }
  93. if (!dax) {
  94. xa_unlock_irq(&mapping->i_pages);
  95. if (mapping_shrinkable(mapping))
  96. inode_add_lru(mapping->host);
  97. spin_unlock(&mapping->host->i_lock);
  98. }
  99. fbatch->nr = j;
  100. }
  101. /**
  102. * folio_invalidate - Invalidate part or all of a folio.
  103. * @folio: The folio which is affected.
  104. * @offset: start of the range to invalidate
  105. * @length: length of the range to invalidate
  106. *
  107. * folio_invalidate() is called when all or part of the folio has become
  108. * invalidated by a truncate operation.
  109. *
  110. * folio_invalidate() does not have to release all buffers, but it must
  111. * ensure that no dirty buffer is left outside @offset and that no I/O
  112. * is underway against any of the blocks which are outside the truncation
  113. * point. Because the caller is about to free (and possibly reuse) those
  114. * blocks on-disk.
  115. */
  116. void folio_invalidate(struct folio *folio, size_t offset, size_t length)
  117. {
  118. const struct address_space_operations *aops = folio->mapping->a_ops;
  119. if (aops->invalidate_folio)
  120. aops->invalidate_folio(folio, offset, length);
  121. }
  122. EXPORT_SYMBOL_GPL(folio_invalidate);
  123. /*
  124. * If truncate cannot remove the fs-private metadata from the page, the page
  125. * becomes orphaned. It will be left on the LRU and may even be mapped into
  126. * user pagetables if we're racing with filemap_fault().
  127. *
  128. * We need to bail out if page->mapping is no longer equal to the original
  129. * mapping. This happens a) when the VM reclaimed the page while we waited on
  130. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  131. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  132. */
  133. static void truncate_cleanup_folio(struct folio *folio)
  134. {
  135. if (folio_mapped(folio))
  136. unmap_mapping_folio(folio);
  137. if (folio_needs_release(folio))
  138. folio_invalidate(folio, 0, folio_size(folio));
  139. /*
  140. * Some filesystems seem to re-dirty the page even after
  141. * the VM has canceled the dirty bit (eg ext3 journaling).
  142. * Hence dirty accounting check is placed after invalidation.
  143. */
  144. folio_cancel_dirty(folio);
  145. folio_clear_mappedtodisk(folio);
  146. }
  147. int truncate_inode_folio(struct address_space *mapping, struct folio *folio)
  148. {
  149. if (folio->mapping != mapping)
  150. return -EIO;
  151. truncate_cleanup_folio(folio);
  152. filemap_remove_folio(folio);
  153. return 0;
  154. }
  155. /*
  156. * Handle partial folios. The folio may be entirely within the
  157. * range if a split has raced with us. If not, we zero the part of the
  158. * folio that's within the [start, end] range, and then split the folio if
  159. * it's large. split_page_range() will discard pages which now lie beyond
  160. * i_size, and we rely on the caller to discard pages which lie within a
  161. * newly created hole.
  162. *
  163. * Returns false if splitting failed so the caller can avoid
  164. * discarding the entire folio which is stubbornly unsplit.
  165. */
  166. bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end)
  167. {
  168. loff_t pos = folio_pos(folio);
  169. unsigned int offset, length;
  170. if (pos < start)
  171. offset = start - pos;
  172. else
  173. offset = 0;
  174. length = folio_size(folio);
  175. if (pos + length <= (u64)end)
  176. length = length - offset;
  177. else
  178. length = end + 1 - pos - offset;
  179. folio_wait_writeback(folio);
  180. if (length == folio_size(folio)) {
  181. truncate_inode_folio(folio->mapping, folio);
  182. return true;
  183. }
  184. /*
  185. * We may be zeroing pages we're about to discard, but it avoids
  186. * doing a complex calculation here, and then doing the zeroing
  187. * anyway if the page split fails.
  188. */
  189. if (!mapping_inaccessible(folio->mapping))
  190. folio_zero_range(folio, offset, length);
  191. if (folio_needs_release(folio))
  192. folio_invalidate(folio, offset, length);
  193. if (!folio_test_large(folio))
  194. return true;
  195. if (split_folio(folio) == 0)
  196. return true;
  197. if (folio_test_dirty(folio))
  198. return false;
  199. truncate_inode_folio(folio->mapping, folio);
  200. return true;
  201. }
  202. /*
  203. * Used to get rid of pages on hardware memory corruption.
  204. */
  205. int generic_error_remove_folio(struct address_space *mapping,
  206. struct folio *folio)
  207. {
  208. if (!mapping)
  209. return -EINVAL;
  210. /*
  211. * Only punch for normal data pages for now.
  212. * Handling other types like directories would need more auditing.
  213. */
  214. if (!S_ISREG(mapping->host->i_mode))
  215. return -EIO;
  216. return truncate_inode_folio(mapping, folio);
  217. }
  218. EXPORT_SYMBOL(generic_error_remove_folio);
  219. /**
  220. * mapping_evict_folio() - Remove an unused folio from the page-cache.
  221. * @mapping: The mapping this folio belongs to.
  222. * @folio: The folio to remove.
  223. *
  224. * Safely remove one folio from the page cache.
  225. * It only drops clean, unused folios.
  226. *
  227. * Context: Folio must be locked.
  228. * Return: The number of pages successfully removed.
  229. */
  230. long mapping_evict_folio(struct address_space *mapping, struct folio *folio)
  231. {
  232. /* The page may have been truncated before it was locked */
  233. if (!mapping)
  234. return 0;
  235. if (folio_test_dirty(folio) || folio_test_writeback(folio))
  236. return 0;
  237. /* The refcount will be elevated if any page in the folio is mapped */
  238. if (folio_ref_count(folio) >
  239. folio_nr_pages(folio) + folio_has_private(folio) + 1)
  240. return 0;
  241. if (!filemap_release_folio(folio, 0))
  242. return 0;
  243. return remove_mapping(mapping, folio);
  244. }
  245. /**
  246. * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
  247. * @mapping: mapping to truncate
  248. * @lstart: offset from which to truncate
  249. * @lend: offset to which to truncate (inclusive)
  250. *
  251. * Truncate the page cache, removing the pages that are between
  252. * specified offsets (and zeroing out partial pages
  253. * if lstart or lend + 1 is not page aligned).
  254. *
  255. * Truncate takes two passes - the first pass is nonblocking. It will not
  256. * block on page locks and it will not block on writeback. The second pass
  257. * will wait. This is to prevent as much IO as possible in the affected region.
  258. * The first pass will remove most pages, so the search cost of the second pass
  259. * is low.
  260. *
  261. * We pass down the cache-hot hint to the page freeing code. Even if the
  262. * mapping is large, it is probably the case that the final pages are the most
  263. * recently touched, and freeing happens in ascending file offset order.
  264. *
  265. * Note that since ->invalidate_folio() accepts range to invalidate
  266. * truncate_inode_pages_range is able to handle cases where lend + 1 is not
  267. * page aligned properly.
  268. */
  269. void truncate_inode_pages_range(struct address_space *mapping,
  270. loff_t lstart, loff_t lend)
  271. {
  272. pgoff_t start; /* inclusive */
  273. pgoff_t end; /* exclusive */
  274. struct folio_batch fbatch;
  275. pgoff_t indices[PAGEVEC_SIZE];
  276. pgoff_t index;
  277. int i;
  278. struct folio *folio;
  279. bool same_folio;
  280. if (mapping_empty(mapping))
  281. return;
  282. /*
  283. * 'start' and 'end' always covers the range of pages to be fully
  284. * truncated. Partial pages are covered with 'partial_start' at the
  285. * start of the range and 'partial_end' at the end of the range.
  286. * Note that 'end' is exclusive while 'lend' is inclusive.
  287. */
  288. start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
  289. if (lend == -1)
  290. /*
  291. * lend == -1 indicates end-of-file so we have to set 'end'
  292. * to the highest possible pgoff_t and since the type is
  293. * unsigned we're using -1.
  294. */
  295. end = -1;
  296. else
  297. end = (lend + 1) >> PAGE_SHIFT;
  298. folio_batch_init(&fbatch);
  299. index = start;
  300. while (index < end && find_lock_entries(mapping, &index, end - 1,
  301. &fbatch, indices)) {
  302. truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
  303. for (i = 0; i < folio_batch_count(&fbatch); i++)
  304. truncate_cleanup_folio(fbatch.folios[i]);
  305. delete_from_page_cache_batch(mapping, &fbatch);
  306. for (i = 0; i < folio_batch_count(&fbatch); i++)
  307. folio_unlock(fbatch.folios[i]);
  308. folio_batch_release(&fbatch);
  309. cond_resched();
  310. }
  311. same_folio = (lstart >> PAGE_SHIFT) == (lend >> PAGE_SHIFT);
  312. folio = __filemap_get_folio(mapping, lstart >> PAGE_SHIFT, FGP_LOCK, 0);
  313. if (!IS_ERR(folio)) {
  314. same_folio = lend < folio_pos(folio) + folio_size(folio);
  315. if (!truncate_inode_partial_folio(folio, lstart, lend)) {
  316. start = folio_next_index(folio);
  317. if (same_folio)
  318. end = folio->index;
  319. }
  320. folio_unlock(folio);
  321. folio_put(folio);
  322. folio = NULL;
  323. }
  324. if (!same_folio) {
  325. folio = __filemap_get_folio(mapping, lend >> PAGE_SHIFT,
  326. FGP_LOCK, 0);
  327. if (!IS_ERR(folio)) {
  328. if (!truncate_inode_partial_folio(folio, lstart, lend))
  329. end = folio->index;
  330. folio_unlock(folio);
  331. folio_put(folio);
  332. }
  333. }
  334. index = start;
  335. while (index < end) {
  336. cond_resched();
  337. if (!find_get_entries(mapping, &index, end - 1, &fbatch,
  338. indices)) {
  339. /* If all gone from start onwards, we're done */
  340. if (index == start)
  341. break;
  342. /* Otherwise restart to make sure all gone */
  343. index = start;
  344. continue;
  345. }
  346. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  347. struct folio *folio = fbatch.folios[i];
  348. /* We rely upon deletion not changing page->index */
  349. if (xa_is_value(folio))
  350. continue;
  351. folio_lock(folio);
  352. VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
  353. folio_wait_writeback(folio);
  354. truncate_inode_folio(mapping, folio);
  355. folio_unlock(folio);
  356. }
  357. truncate_folio_batch_exceptionals(mapping, &fbatch, indices);
  358. folio_batch_release(&fbatch);
  359. }
  360. }
  361. EXPORT_SYMBOL(truncate_inode_pages_range);
  362. /**
  363. * truncate_inode_pages - truncate *all* the pages from an offset
  364. * @mapping: mapping to truncate
  365. * @lstart: offset from which to truncate
  366. *
  367. * Called under (and serialised by) inode->i_rwsem and
  368. * mapping->invalidate_lock.
  369. *
  370. * Note: When this function returns, there can be a page in the process of
  371. * deletion (inside __filemap_remove_folio()) in the specified range. Thus
  372. * mapping->nrpages can be non-zero when this function returns even after
  373. * truncation of the whole mapping.
  374. */
  375. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  376. {
  377. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  378. }
  379. EXPORT_SYMBOL(truncate_inode_pages);
  380. /**
  381. * truncate_inode_pages_final - truncate *all* pages before inode dies
  382. * @mapping: mapping to truncate
  383. *
  384. * Called under (and serialized by) inode->i_rwsem.
  385. *
  386. * Filesystems have to use this in the .evict_inode path to inform the
  387. * VM that this is the final truncate and the inode is going away.
  388. */
  389. void truncate_inode_pages_final(struct address_space *mapping)
  390. {
  391. /*
  392. * Page reclaim can not participate in regular inode lifetime
  393. * management (can't call iput()) and thus can race with the
  394. * inode teardown. Tell it when the address space is exiting,
  395. * so that it does not install eviction information after the
  396. * final truncate has begun.
  397. */
  398. mapping_set_exiting(mapping);
  399. if (!mapping_empty(mapping)) {
  400. /*
  401. * As truncation uses a lockless tree lookup, cycle
  402. * the tree lock to make sure any ongoing tree
  403. * modification that does not see AS_EXITING is
  404. * completed before starting the final truncate.
  405. */
  406. xa_lock_irq(&mapping->i_pages);
  407. xa_unlock_irq(&mapping->i_pages);
  408. }
  409. truncate_inode_pages(mapping, 0);
  410. }
  411. EXPORT_SYMBOL(truncate_inode_pages_final);
  412. /**
  413. * mapping_try_invalidate - Invalidate all the evictable folios of one inode
  414. * @mapping: the address_space which holds the folios to invalidate
  415. * @start: the offset 'from' which to invalidate
  416. * @end: the offset 'to' which to invalidate (inclusive)
  417. * @nr_failed: How many folio invalidations failed
  418. *
  419. * This function is similar to invalidate_mapping_pages(), except that it
  420. * returns the number of folios which could not be evicted in @nr_failed.
  421. */
  422. unsigned long mapping_try_invalidate(struct address_space *mapping,
  423. pgoff_t start, pgoff_t end, unsigned long *nr_failed)
  424. {
  425. pgoff_t indices[PAGEVEC_SIZE];
  426. struct folio_batch fbatch;
  427. pgoff_t index = start;
  428. unsigned long ret;
  429. unsigned long count = 0;
  430. int i;
  431. bool xa_has_values = false;
  432. folio_batch_init(&fbatch);
  433. while (find_lock_entries(mapping, &index, end, &fbatch, indices)) {
  434. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  435. struct folio *folio = fbatch.folios[i];
  436. /* We rely upon deletion not changing folio->index */
  437. if (xa_is_value(folio)) {
  438. xa_has_values = true;
  439. count++;
  440. continue;
  441. }
  442. ret = mapping_evict_folio(mapping, folio);
  443. folio_unlock(folio);
  444. /*
  445. * Invalidation is a hint that the folio is no longer
  446. * of interest and try to speed up its reclaim.
  447. */
  448. if (!ret) {
  449. deactivate_file_folio(folio);
  450. /* Likely in the lru cache of a remote CPU */
  451. if (nr_failed)
  452. (*nr_failed)++;
  453. }
  454. count += ret;
  455. }
  456. if (xa_has_values)
  457. clear_shadow_entries(mapping, &fbatch, indices);
  458. folio_batch_remove_exceptionals(&fbatch);
  459. folio_batch_release(&fbatch);
  460. cond_resched();
  461. }
  462. return count;
  463. }
  464. /**
  465. * invalidate_mapping_pages - Invalidate all clean, unlocked cache of one inode
  466. * @mapping: the address_space which holds the cache to invalidate
  467. * @start: the offset 'from' which to invalidate
  468. * @end: the offset 'to' which to invalidate (inclusive)
  469. *
  470. * This function removes pages that are clean, unmapped and unlocked,
  471. * as well as shadow entries. It will not block on IO activity.
  472. *
  473. * If you want to remove all the pages of one inode, regardless of
  474. * their use and writeback state, use truncate_inode_pages().
  475. *
  476. * Return: The number of indices that had their contents invalidated
  477. */
  478. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  479. pgoff_t start, pgoff_t end)
  480. {
  481. return mapping_try_invalidate(mapping, start, end, NULL);
  482. }
  483. EXPORT_SYMBOL(invalidate_mapping_pages);
  484. /*
  485. * This is like mapping_evict_folio(), except it ignores the folio's
  486. * refcount. We do this because invalidate_inode_pages2() needs stronger
  487. * invalidation guarantees, and cannot afford to leave folios behind because
  488. * shrink_folio_list() has a temp ref on them, or because they're transiently
  489. * sitting in the folio_add_lru() caches.
  490. */
  491. static int invalidate_complete_folio2(struct address_space *mapping,
  492. struct folio *folio)
  493. {
  494. if (folio->mapping != mapping)
  495. return 0;
  496. if (!filemap_release_folio(folio, GFP_KERNEL))
  497. return 0;
  498. spin_lock(&mapping->host->i_lock);
  499. xa_lock_irq(&mapping->i_pages);
  500. if (folio_test_dirty(folio))
  501. goto failed;
  502. BUG_ON(folio_has_private(folio));
  503. __filemap_remove_folio(folio, NULL);
  504. xa_unlock_irq(&mapping->i_pages);
  505. if (mapping_shrinkable(mapping))
  506. inode_add_lru(mapping->host);
  507. spin_unlock(&mapping->host->i_lock);
  508. filemap_free_folio(mapping, folio);
  509. return 1;
  510. failed:
  511. xa_unlock_irq(&mapping->i_pages);
  512. spin_unlock(&mapping->host->i_lock);
  513. return 0;
  514. }
  515. static int folio_launder(struct address_space *mapping, struct folio *folio)
  516. {
  517. if (!folio_test_dirty(folio))
  518. return 0;
  519. if (folio->mapping != mapping || mapping->a_ops->launder_folio == NULL)
  520. return 0;
  521. return mapping->a_ops->launder_folio(folio);
  522. }
  523. /**
  524. * invalidate_inode_pages2_range - remove range of pages from an address_space
  525. * @mapping: the address_space
  526. * @start: the page offset 'from' which to invalidate
  527. * @end: the page offset 'to' which to invalidate (inclusive)
  528. *
  529. * Any pages which are found to be mapped into pagetables are unmapped prior to
  530. * invalidation.
  531. *
  532. * Return: -EBUSY if any pages could not be invalidated.
  533. */
  534. int invalidate_inode_pages2_range(struct address_space *mapping,
  535. pgoff_t start, pgoff_t end)
  536. {
  537. pgoff_t indices[PAGEVEC_SIZE];
  538. struct folio_batch fbatch;
  539. pgoff_t index;
  540. int i;
  541. int ret = 0;
  542. int ret2 = 0;
  543. int did_range_unmap = 0;
  544. bool xa_has_values = false;
  545. if (mapping_empty(mapping))
  546. return 0;
  547. folio_batch_init(&fbatch);
  548. index = start;
  549. while (find_get_entries(mapping, &index, end, &fbatch, indices)) {
  550. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  551. struct folio *folio = fbatch.folios[i];
  552. /* We rely upon deletion not changing folio->index */
  553. if (xa_is_value(folio)) {
  554. xa_has_values = true;
  555. if (dax_mapping(mapping) &&
  556. !dax_invalidate_mapping_entry_sync(mapping, indices[i]))
  557. ret = -EBUSY;
  558. continue;
  559. }
  560. if (!did_range_unmap && folio_mapped(folio)) {
  561. /*
  562. * If folio is mapped, before taking its lock,
  563. * zap the rest of the file in one hit.
  564. */
  565. unmap_mapping_pages(mapping, indices[i],
  566. (1 + end - indices[i]), false);
  567. did_range_unmap = 1;
  568. }
  569. folio_lock(folio);
  570. if (unlikely(folio->mapping != mapping)) {
  571. folio_unlock(folio);
  572. continue;
  573. }
  574. VM_BUG_ON_FOLIO(!folio_contains(folio, indices[i]), folio);
  575. folio_wait_writeback(folio);
  576. if (folio_mapped(folio))
  577. unmap_mapping_folio(folio);
  578. BUG_ON(folio_mapped(folio));
  579. ret2 = folio_launder(mapping, folio);
  580. if (ret2 == 0) {
  581. if (!invalidate_complete_folio2(mapping, folio))
  582. ret2 = -EBUSY;
  583. }
  584. if (ret2 < 0)
  585. ret = ret2;
  586. folio_unlock(folio);
  587. }
  588. if (xa_has_values)
  589. clear_shadow_entries(mapping, &fbatch, indices);
  590. folio_batch_remove_exceptionals(&fbatch);
  591. folio_batch_release(&fbatch);
  592. cond_resched();
  593. }
  594. /*
  595. * For DAX we invalidate page tables after invalidating page cache. We
  596. * could invalidate page tables while invalidating each entry however
  597. * that would be expensive. And doing range unmapping before doesn't
  598. * work as we have no cheap way to find whether page cache entry didn't
  599. * get remapped later.
  600. */
  601. if (dax_mapping(mapping)) {
  602. unmap_mapping_pages(mapping, start, end - start + 1, false);
  603. }
  604. return ret;
  605. }
  606. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  607. /**
  608. * invalidate_inode_pages2 - remove all pages from an address_space
  609. * @mapping: the address_space
  610. *
  611. * Any pages which are found to be mapped into pagetables are unmapped prior to
  612. * invalidation.
  613. *
  614. * Return: -EBUSY if any pages could not be invalidated.
  615. */
  616. int invalidate_inode_pages2(struct address_space *mapping)
  617. {
  618. return invalidate_inode_pages2_range(mapping, 0, -1);
  619. }
  620. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
  621. /**
  622. * truncate_pagecache - unmap and remove pagecache that has been truncated
  623. * @inode: inode
  624. * @newsize: new file size
  625. *
  626. * inode's new i_size must already be written before truncate_pagecache
  627. * is called.
  628. *
  629. * This function should typically be called before the filesystem
  630. * releases resources associated with the freed range (eg. deallocates
  631. * blocks). This way, pagecache will always stay logically coherent
  632. * with on-disk format, and the filesystem would not have to deal with
  633. * situations such as writepage being called for a page that has already
  634. * had its underlying blocks deallocated.
  635. */
  636. void truncate_pagecache(struct inode *inode, loff_t newsize)
  637. {
  638. struct address_space *mapping = inode->i_mapping;
  639. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  640. /*
  641. * unmap_mapping_range is called twice, first simply for
  642. * efficiency so that truncate_inode_pages does fewer
  643. * single-page unmaps. However after this first call, and
  644. * before truncate_inode_pages finishes, it is possible for
  645. * private pages to be COWed, which remain after
  646. * truncate_inode_pages finishes, hence the second
  647. * unmap_mapping_range call must be made for correctness.
  648. */
  649. unmap_mapping_range(mapping, holebegin, 0, 1);
  650. truncate_inode_pages(mapping, newsize);
  651. unmap_mapping_range(mapping, holebegin, 0, 1);
  652. }
  653. EXPORT_SYMBOL(truncate_pagecache);
  654. /**
  655. * truncate_setsize - update inode and pagecache for a new file size
  656. * @inode: inode
  657. * @newsize: new file size
  658. *
  659. * truncate_setsize updates i_size and performs pagecache truncation (if
  660. * necessary) to @newsize. It will be typically be called from the filesystem's
  661. * setattr function when ATTR_SIZE is passed in.
  662. *
  663. * Must be called with a lock serializing truncates and writes (generally
  664. * i_rwsem but e.g. xfs uses a different lock) and before all filesystem
  665. * specific block truncation has been performed.
  666. */
  667. void truncate_setsize(struct inode *inode, loff_t newsize)
  668. {
  669. loff_t oldsize = inode->i_size;
  670. i_size_write(inode, newsize);
  671. if (newsize > oldsize)
  672. pagecache_isize_extended(inode, oldsize, newsize);
  673. truncate_pagecache(inode, newsize);
  674. }
  675. EXPORT_SYMBOL(truncate_setsize);
  676. /**
  677. * pagecache_isize_extended - update pagecache after extension of i_size
  678. * @inode: inode for which i_size was extended
  679. * @from: original inode size
  680. * @to: new inode size
  681. *
  682. * Handle extension of inode size either caused by extending truncate or
  683. * by write starting after current i_size. We mark the page straddling
  684. * current i_size RO so that page_mkwrite() is called on the first
  685. * write access to the page. The filesystem will update its per-block
  686. * information before user writes to the page via mmap after the i_size
  687. * has been changed.
  688. *
  689. * The function must be called after i_size is updated so that page fault
  690. * coming after we unlock the folio will already see the new i_size.
  691. * The function must be called while we still hold i_rwsem - this not only
  692. * makes sure i_size is stable but also that userspace cannot observe new
  693. * i_size value before we are prepared to store mmap writes at new inode size.
  694. */
  695. void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
  696. {
  697. int bsize = i_blocksize(inode);
  698. loff_t rounded_from;
  699. struct folio *folio;
  700. WARN_ON(to > inode->i_size);
  701. if (from >= to || bsize >= PAGE_SIZE)
  702. return;
  703. /* Page straddling @from will not have any hole block created? */
  704. rounded_from = round_up(from, bsize);
  705. if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
  706. return;
  707. folio = filemap_lock_folio(inode->i_mapping, from / PAGE_SIZE);
  708. /* Folio not cached? Nothing to do */
  709. if (IS_ERR(folio))
  710. return;
  711. /*
  712. * See folio_clear_dirty_for_io() for details why folio_mark_dirty()
  713. * is needed.
  714. */
  715. if (folio_mkclean(folio))
  716. folio_mark_dirty(folio);
  717. folio_unlock(folio);
  718. folio_put(folio);
  719. }
  720. EXPORT_SYMBOL(pagecache_isize_extended);
  721. /**
  722. * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
  723. * @inode: inode
  724. * @lstart: offset of beginning of hole
  725. * @lend: offset of last byte of hole
  726. *
  727. * This function should typically be called before the filesystem
  728. * releases resources associated with the freed range (eg. deallocates
  729. * blocks). This way, pagecache will always stay logically coherent
  730. * with on-disk format, and the filesystem would not have to deal with
  731. * situations such as writepage being called for a page that has already
  732. * had its underlying blocks deallocated.
  733. */
  734. void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
  735. {
  736. struct address_space *mapping = inode->i_mapping;
  737. loff_t unmap_start = round_up(lstart, PAGE_SIZE);
  738. loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
  739. /*
  740. * This rounding is currently just for example: unmap_mapping_range
  741. * expands its hole outwards, whereas we want it to contract the hole
  742. * inwards. However, existing callers of truncate_pagecache_range are
  743. * doing their own page rounding first. Note that unmap_mapping_range
  744. * allows holelen 0 for all, and we allow lend -1 for end of file.
  745. */
  746. /*
  747. * Unlike in truncate_pagecache, unmap_mapping_range is called only
  748. * once (before truncating pagecache), and without "even_cows" flag:
  749. * hole-punching should not remove private COWed pages from the hole.
  750. */
  751. if ((u64)unmap_end > (u64)unmap_start)
  752. unmap_mapping_range(mapping, unmap_start,
  753. 1 + unmap_end - unmap_start, 0);
  754. truncate_inode_pages_range(mapping, lstart, lend);
  755. }
  756. EXPORT_SYMBOL(truncate_pagecache_range);