truncate.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. #include <linux/kernel.h>
  10. #include <linux/backing-dev.h>
  11. #include <linux/dax.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. static void clear_exceptional_entry(struct address_space *mapping,
  26. pgoff_t index, void *entry)
  27. {
  28. struct radix_tree_node *node;
  29. void **slot;
  30. /* Handled by shmem itself */
  31. if (shmem_mapping(mapping))
  32. return;
  33. if (dax_mapping(mapping)) {
  34. dax_delete_mapping_entry(mapping, index);
  35. return;
  36. }
  37. spin_lock_irq(&mapping->tree_lock);
  38. /*
  39. * Regular page slots are stabilized by the page lock even
  40. * without the tree itself locked. These unlocked entries
  41. * need verification under the tree lock.
  42. */
  43. if (!__radix_tree_lookup(&mapping->page_tree, index, &node,
  44. &slot))
  45. goto unlock;
  46. if (*slot != entry)
  47. goto unlock;
  48. radix_tree_replace_slot(slot, NULL);
  49. mapping->nrexceptional--;
  50. if (!node)
  51. goto unlock;
  52. workingset_node_shadows_dec(node);
  53. /*
  54. * Don't track node without shadow entries.
  55. *
  56. * Avoid acquiring the list_lru lock if already untracked.
  57. * The list_empty() test is safe as node->private_list is
  58. * protected by mapping->tree_lock.
  59. */
  60. if (!workingset_node_shadows(node) &&
  61. !list_empty(&node->private_list))
  62. list_lru_del(&workingset_shadow_nodes,
  63. &node->private_list);
  64. __radix_tree_delete_node(&mapping->page_tree, node);
  65. unlock:
  66. spin_unlock_irq(&mapping->tree_lock);
  67. }
  68. /**
  69. * do_invalidatepage - invalidate part or all of a page
  70. * @page: the page which is affected
  71. * @offset: start of the range to invalidate
  72. * @length: length of the range to invalidate
  73. *
  74. * do_invalidatepage() is called when all or part of the page has become
  75. * invalidated by a truncate operation.
  76. *
  77. * do_invalidatepage() does not have to release all buffers, but it must
  78. * ensure that no dirty buffer is left outside @offset and that no I/O
  79. * is underway against any of the blocks which are outside the truncation
  80. * point. Because the caller is about to free (and possibly reuse) those
  81. * blocks on-disk.
  82. */
  83. void do_invalidatepage(struct page *page, unsigned int offset,
  84. unsigned int length)
  85. {
  86. void (*invalidatepage)(struct page *, unsigned int, unsigned int);
  87. invalidatepage = page->mapping->a_ops->invalidatepage;
  88. #ifdef CONFIG_BLOCK
  89. if (!invalidatepage)
  90. invalidatepage = block_invalidatepage;
  91. #endif
  92. if (invalidatepage)
  93. (*invalidatepage)(page, offset, length);
  94. }
  95. /*
  96. * If truncate cannot remove the fs-private metadata from the page, the page
  97. * becomes orphaned. It will be left on the LRU and may even be mapped into
  98. * user pagetables if we're racing with filemap_fault().
  99. *
  100. * We need to bale out if page->mapping is no longer equal to the original
  101. * mapping. This happens a) when the VM reclaimed the page while we waited on
  102. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  103. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  104. */
  105. static int
  106. truncate_complete_page(struct address_space *mapping, struct page *page)
  107. {
  108. if (page->mapping != mapping)
  109. return -EIO;
  110. if (page_has_private(page))
  111. do_invalidatepage(page, 0, PAGE_SIZE);
  112. /*
  113. * Some filesystems seem to re-dirty the page even after
  114. * the VM has canceled the dirty bit (eg ext3 journaling).
  115. * Hence dirty accounting check is placed after invalidation.
  116. */
  117. cancel_dirty_page(page);
  118. ClearPageMappedToDisk(page);
  119. delete_from_page_cache(page);
  120. return 0;
  121. }
  122. /*
  123. * This is for invalidate_mapping_pages(). That function can be called at
  124. * any time, and is not supposed to throw away dirty pages. But pages can
  125. * be marked dirty at any time too, so use remove_mapping which safely
  126. * discards clean, unused pages.
  127. *
  128. * Returns non-zero if the page was successfully invalidated.
  129. */
  130. static int
  131. invalidate_complete_page(struct address_space *mapping, struct page *page)
  132. {
  133. int ret;
  134. if (page->mapping != mapping)
  135. return 0;
  136. if (page_has_private(page) && !try_to_release_page(page, 0))
  137. return 0;
  138. ret = remove_mapping(mapping, page);
  139. return ret;
  140. }
  141. int truncate_inode_page(struct address_space *mapping, struct page *page)
  142. {
  143. if (page_mapped(page)) {
  144. unmap_mapping_range(mapping,
  145. (loff_t)page->index << PAGE_SHIFT,
  146. PAGE_SIZE, 0);
  147. }
  148. return truncate_complete_page(mapping, page);
  149. }
  150. /*
  151. * Used to get rid of pages on hardware memory corruption.
  152. */
  153. int generic_error_remove_page(struct address_space *mapping, struct page *page)
  154. {
  155. if (!mapping)
  156. return -EINVAL;
  157. /*
  158. * Only punch for normal data pages for now.
  159. * Handling other types like directories would need more auditing.
  160. */
  161. if (!S_ISREG(mapping->host->i_mode))
  162. return -EIO;
  163. return truncate_inode_page(mapping, page);
  164. }
  165. EXPORT_SYMBOL(generic_error_remove_page);
  166. /*
  167. * Safely invalidate one page from its pagecache mapping.
  168. * It only drops clean, unused pages. The page must be locked.
  169. *
  170. * Returns 1 if the page is successfully invalidated, otherwise 0.
  171. */
  172. int invalidate_inode_page(struct page *page)
  173. {
  174. struct address_space *mapping = page_mapping(page);
  175. if (!mapping)
  176. return 0;
  177. if (PageDirty(page) || PageWriteback(page))
  178. return 0;
  179. if (page_mapped(page))
  180. return 0;
  181. return invalidate_complete_page(mapping, page);
  182. }
  183. /**
  184. * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
  185. * @mapping: mapping to truncate
  186. * @lstart: offset from which to truncate
  187. * @lend: offset to which to truncate (inclusive)
  188. *
  189. * Truncate the page cache, removing the pages that are between
  190. * specified offsets (and zeroing out partial pages
  191. * if lstart or lend + 1 is not page aligned).
  192. *
  193. * Truncate takes two passes - the first pass is nonblocking. It will not
  194. * block on page locks and it will not block on writeback. The second pass
  195. * will wait. This is to prevent as much IO as possible in the affected region.
  196. * The first pass will remove most pages, so the search cost of the second pass
  197. * is low.
  198. *
  199. * We pass down the cache-hot hint to the page freeing code. Even if the
  200. * mapping is large, it is probably the case that the final pages are the most
  201. * recently touched, and freeing happens in ascending file offset order.
  202. *
  203. * Note that since ->invalidatepage() accepts range to invalidate
  204. * truncate_inode_pages_range is able to handle cases where lend + 1 is not
  205. * page aligned properly.
  206. */
  207. void truncate_inode_pages_range(struct address_space *mapping,
  208. loff_t lstart, loff_t lend)
  209. {
  210. pgoff_t start; /* inclusive */
  211. pgoff_t end; /* exclusive */
  212. unsigned int partial_start; /* inclusive */
  213. unsigned int partial_end; /* exclusive */
  214. struct pagevec pvec;
  215. pgoff_t indices[PAGEVEC_SIZE];
  216. pgoff_t index;
  217. int i;
  218. cleancache_invalidate_inode(mapping);
  219. if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
  220. return;
  221. /* Offsets within partial pages */
  222. partial_start = lstart & (PAGE_SIZE - 1);
  223. partial_end = (lend + 1) & (PAGE_SIZE - 1);
  224. /*
  225. * 'start' and 'end' always covers the range of pages to be fully
  226. * truncated. Partial pages are covered with 'partial_start' at the
  227. * start of the range and 'partial_end' at the end of the range.
  228. * Note that 'end' is exclusive while 'lend' is inclusive.
  229. */
  230. start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
  231. if (lend == -1)
  232. /*
  233. * lend == -1 indicates end-of-file so we have to set 'end'
  234. * to the highest possible pgoff_t and since the type is
  235. * unsigned we're using -1.
  236. */
  237. end = -1;
  238. else
  239. end = (lend + 1) >> PAGE_SHIFT;
  240. pagevec_init(&pvec, 0);
  241. index = start;
  242. while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
  243. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  244. indices)) {
  245. for (i = 0; i < pagevec_count(&pvec); i++) {
  246. struct page *page = pvec.pages[i];
  247. /* We rely upon deletion not changing page->index */
  248. index = indices[i];
  249. if (index >= end)
  250. break;
  251. if (radix_tree_exceptional_entry(page)) {
  252. clear_exceptional_entry(mapping, index, page);
  253. continue;
  254. }
  255. if (!trylock_page(page))
  256. continue;
  257. WARN_ON(page->index != index);
  258. if (PageWriteback(page)) {
  259. unlock_page(page);
  260. continue;
  261. }
  262. truncate_inode_page(mapping, page);
  263. unlock_page(page);
  264. }
  265. pagevec_remove_exceptionals(&pvec);
  266. pagevec_release(&pvec);
  267. cond_resched();
  268. index++;
  269. }
  270. if (partial_start) {
  271. struct page *page = find_lock_page(mapping, start - 1);
  272. if (page) {
  273. unsigned int top = PAGE_SIZE;
  274. if (start > end) {
  275. /* Truncation within a single page */
  276. top = partial_end;
  277. partial_end = 0;
  278. }
  279. wait_on_page_writeback(page);
  280. zero_user_segment(page, partial_start, top);
  281. cleancache_invalidate_page(mapping, page);
  282. if (page_has_private(page))
  283. do_invalidatepage(page, partial_start,
  284. top - partial_start);
  285. unlock_page(page);
  286. put_page(page);
  287. }
  288. }
  289. if (partial_end) {
  290. struct page *page = find_lock_page(mapping, end);
  291. if (page) {
  292. wait_on_page_writeback(page);
  293. zero_user_segment(page, 0, partial_end);
  294. cleancache_invalidate_page(mapping, page);
  295. if (page_has_private(page))
  296. do_invalidatepage(page, 0,
  297. partial_end);
  298. unlock_page(page);
  299. put_page(page);
  300. }
  301. }
  302. /*
  303. * If the truncation happened within a single page no pages
  304. * will be released, just zeroed, so we can bail out now.
  305. */
  306. if (start >= end)
  307. return;
  308. index = start;
  309. for ( ; ; ) {
  310. cond_resched();
  311. if (!pagevec_lookup_entries(&pvec, mapping, index,
  312. min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
  313. /* If all gone from start onwards, we're done */
  314. if (index == start)
  315. break;
  316. /* Otherwise restart to make sure all gone */
  317. index = start;
  318. continue;
  319. }
  320. if (index == start && indices[0] >= end) {
  321. /* All gone out of hole to be punched, we're done */
  322. pagevec_remove_exceptionals(&pvec);
  323. pagevec_release(&pvec);
  324. break;
  325. }
  326. for (i = 0; i < pagevec_count(&pvec); i++) {
  327. struct page *page = pvec.pages[i];
  328. /* We rely upon deletion not changing page->index */
  329. index = indices[i];
  330. if (index >= end) {
  331. /* Restart punch to make sure all gone */
  332. index = start - 1;
  333. break;
  334. }
  335. if (radix_tree_exceptional_entry(page)) {
  336. clear_exceptional_entry(mapping, index, page);
  337. continue;
  338. }
  339. lock_page(page);
  340. WARN_ON(page->index != index);
  341. wait_on_page_writeback(page);
  342. truncate_inode_page(mapping, page);
  343. unlock_page(page);
  344. }
  345. pagevec_remove_exceptionals(&pvec);
  346. pagevec_release(&pvec);
  347. index++;
  348. }
  349. cleancache_invalidate_inode(mapping);
  350. }
  351. EXPORT_SYMBOL(truncate_inode_pages_range);
  352. /**
  353. * truncate_inode_pages - truncate *all* the pages from an offset
  354. * @mapping: mapping to truncate
  355. * @lstart: offset from which to truncate
  356. *
  357. * Called under (and serialised by) inode->i_mutex.
  358. *
  359. * Note: When this function returns, there can be a page in the process of
  360. * deletion (inside __delete_from_page_cache()) in the specified range. Thus
  361. * mapping->nrpages can be non-zero when this function returns even after
  362. * truncation of the whole mapping.
  363. */
  364. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  365. {
  366. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  367. }
  368. EXPORT_SYMBOL(truncate_inode_pages);
  369. /**
  370. * truncate_inode_pages_final - truncate *all* pages before inode dies
  371. * @mapping: mapping to truncate
  372. *
  373. * Called under (and serialized by) inode->i_mutex.
  374. *
  375. * Filesystems have to use this in the .evict_inode path to inform the
  376. * VM that this is the final truncate and the inode is going away.
  377. */
  378. void truncate_inode_pages_final(struct address_space *mapping)
  379. {
  380. unsigned long nrexceptional;
  381. unsigned long nrpages;
  382. /*
  383. * Page reclaim can not participate in regular inode lifetime
  384. * management (can't call iput()) and thus can race with the
  385. * inode teardown. Tell it when the address space is exiting,
  386. * so that it does not install eviction information after the
  387. * final truncate has begun.
  388. */
  389. mapping_set_exiting(mapping);
  390. /*
  391. * When reclaim installs eviction entries, it increases
  392. * nrexceptional first, then decreases nrpages. Make sure we see
  393. * this in the right order or we might miss an entry.
  394. */
  395. nrpages = mapping->nrpages;
  396. smp_rmb();
  397. nrexceptional = mapping->nrexceptional;
  398. if (nrpages || nrexceptional) {
  399. /*
  400. * As truncation uses a lockless tree lookup, cycle
  401. * the tree lock to make sure any ongoing tree
  402. * modification that does not see AS_EXITING is
  403. * completed before starting the final truncate.
  404. */
  405. spin_lock_irq(&mapping->tree_lock);
  406. spin_unlock_irq(&mapping->tree_lock);
  407. truncate_inode_pages(mapping, 0);
  408. }
  409. }
  410. EXPORT_SYMBOL(truncate_inode_pages_final);
  411. /**
  412. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  413. * @mapping: the address_space which holds the pages to invalidate
  414. * @start: the offset 'from' which to invalidate
  415. * @end: the offset 'to' which to invalidate (inclusive)
  416. *
  417. * This function only removes the unlocked pages, if you want to
  418. * remove all the pages of one inode, you must call truncate_inode_pages.
  419. *
  420. * invalidate_mapping_pages() will not block on IO activity. It will not
  421. * invalidate pages which are dirty, locked, under writeback or mapped into
  422. * pagetables.
  423. */
  424. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  425. pgoff_t start, pgoff_t end)
  426. {
  427. pgoff_t indices[PAGEVEC_SIZE];
  428. struct pagevec pvec;
  429. pgoff_t index = start;
  430. unsigned long ret;
  431. unsigned long count = 0;
  432. int i;
  433. pagevec_init(&pvec, 0);
  434. while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
  435. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  436. indices)) {
  437. for (i = 0; i < pagevec_count(&pvec); i++) {
  438. struct page *page = pvec.pages[i];
  439. /* We rely upon deletion not changing page->index */
  440. index = indices[i];
  441. if (index > end)
  442. break;
  443. if (radix_tree_exceptional_entry(page)) {
  444. clear_exceptional_entry(mapping, index, page);
  445. continue;
  446. }
  447. if (!trylock_page(page))
  448. continue;
  449. WARN_ON(page->index != index);
  450. ret = invalidate_inode_page(page);
  451. unlock_page(page);
  452. /*
  453. * Invalidation is a hint that the page is no longer
  454. * of interest and try to speed up its reclaim.
  455. */
  456. if (!ret)
  457. deactivate_file_page(page);
  458. count += ret;
  459. }
  460. pagevec_remove_exceptionals(&pvec);
  461. pagevec_release(&pvec);
  462. cond_resched();
  463. index++;
  464. }
  465. return count;
  466. }
  467. EXPORT_SYMBOL(invalidate_mapping_pages);
  468. /*
  469. * This is like invalidate_complete_page(), except it ignores the page's
  470. * refcount. We do this because invalidate_inode_pages2() needs stronger
  471. * invalidation guarantees, and cannot afford to leave pages behind because
  472. * shrink_page_list() has a temp ref on them, or because they're transiently
  473. * sitting in the lru_cache_add() pagevecs.
  474. */
  475. static int
  476. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  477. {
  478. unsigned long flags;
  479. if (page->mapping != mapping)
  480. return 0;
  481. if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
  482. return 0;
  483. spin_lock_irqsave(&mapping->tree_lock, flags);
  484. if (PageDirty(page))
  485. goto failed;
  486. BUG_ON(page_has_private(page));
  487. __delete_from_page_cache(page, NULL);
  488. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  489. if (mapping->a_ops->freepage)
  490. mapping->a_ops->freepage(page);
  491. put_page(page); /* pagecache ref */
  492. return 1;
  493. failed:
  494. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  495. return 0;
  496. }
  497. static int do_launder_page(struct address_space *mapping, struct page *page)
  498. {
  499. if (!PageDirty(page))
  500. return 0;
  501. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  502. return 0;
  503. return mapping->a_ops->launder_page(page);
  504. }
  505. /**
  506. * invalidate_inode_pages2_range - remove range of pages from an address_space
  507. * @mapping: the address_space
  508. * @start: the page offset 'from' which to invalidate
  509. * @end: the page offset 'to' which to invalidate (inclusive)
  510. *
  511. * Any pages which are found to be mapped into pagetables are unmapped prior to
  512. * invalidation.
  513. *
  514. * Returns -EBUSY if any pages could not be invalidated.
  515. */
  516. int invalidate_inode_pages2_range(struct address_space *mapping,
  517. pgoff_t start, pgoff_t end)
  518. {
  519. pgoff_t indices[PAGEVEC_SIZE];
  520. struct pagevec pvec;
  521. pgoff_t index;
  522. int i;
  523. int ret = 0;
  524. int ret2 = 0;
  525. int did_range_unmap = 0;
  526. cleancache_invalidate_inode(mapping);
  527. pagevec_init(&pvec, 0);
  528. index = start;
  529. while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
  530. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  531. indices)) {
  532. for (i = 0; i < pagevec_count(&pvec); i++) {
  533. struct page *page = pvec.pages[i];
  534. /* We rely upon deletion not changing page->index */
  535. index = indices[i];
  536. if (index > end)
  537. break;
  538. if (radix_tree_exceptional_entry(page)) {
  539. clear_exceptional_entry(mapping, index, page);
  540. continue;
  541. }
  542. lock_page(page);
  543. WARN_ON(page->index != index);
  544. if (page->mapping != mapping) {
  545. unlock_page(page);
  546. continue;
  547. }
  548. wait_on_page_writeback(page);
  549. if (page_mapped(page)) {
  550. if (!did_range_unmap) {
  551. /*
  552. * Zap the rest of the file in one hit.
  553. */
  554. unmap_mapping_range(mapping,
  555. (loff_t)index << PAGE_SHIFT,
  556. (loff_t)(1 + end - index)
  557. << PAGE_SHIFT,
  558. 0);
  559. did_range_unmap = 1;
  560. } else {
  561. /*
  562. * Just zap this page
  563. */
  564. unmap_mapping_range(mapping,
  565. (loff_t)index << PAGE_SHIFT,
  566. PAGE_SIZE, 0);
  567. }
  568. }
  569. BUG_ON(page_mapped(page));
  570. ret2 = do_launder_page(mapping, page);
  571. if (ret2 == 0) {
  572. if (!invalidate_complete_page2(mapping, page))
  573. ret2 = -EBUSY;
  574. }
  575. if (ret2 < 0)
  576. ret = ret2;
  577. unlock_page(page);
  578. }
  579. pagevec_remove_exceptionals(&pvec);
  580. pagevec_release(&pvec);
  581. cond_resched();
  582. index++;
  583. }
  584. cleancache_invalidate_inode(mapping);
  585. return ret;
  586. }
  587. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  588. /**
  589. * invalidate_inode_pages2 - remove all pages from an address_space
  590. * @mapping: the address_space
  591. *
  592. * Any pages which are found to be mapped into pagetables are unmapped prior to
  593. * invalidation.
  594. *
  595. * Returns -EBUSY if any pages could not be invalidated.
  596. */
  597. int invalidate_inode_pages2(struct address_space *mapping)
  598. {
  599. return invalidate_inode_pages2_range(mapping, 0, -1);
  600. }
  601. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
  602. /**
  603. * truncate_pagecache - unmap and remove pagecache that has been truncated
  604. * @inode: inode
  605. * @newsize: new file size
  606. *
  607. * inode's new i_size must already be written before truncate_pagecache
  608. * is called.
  609. *
  610. * This function should typically be called before the filesystem
  611. * releases resources associated with the freed range (eg. deallocates
  612. * blocks). This way, pagecache will always stay logically coherent
  613. * with on-disk format, and the filesystem would not have to deal with
  614. * situations such as writepage being called for a page that has already
  615. * had its underlying blocks deallocated.
  616. */
  617. void truncate_pagecache(struct inode *inode, loff_t newsize)
  618. {
  619. struct address_space *mapping = inode->i_mapping;
  620. loff_t holebegin = round_up(newsize, PAGE_SIZE);
  621. /*
  622. * unmap_mapping_range is called twice, first simply for
  623. * efficiency so that truncate_inode_pages does fewer
  624. * single-page unmaps. However after this first call, and
  625. * before truncate_inode_pages finishes, it is possible for
  626. * private pages to be COWed, which remain after
  627. * truncate_inode_pages finishes, hence the second
  628. * unmap_mapping_range call must be made for correctness.
  629. */
  630. unmap_mapping_range(mapping, holebegin, 0, 1);
  631. truncate_inode_pages(mapping, newsize);
  632. unmap_mapping_range(mapping, holebegin, 0, 1);
  633. }
  634. EXPORT_SYMBOL(truncate_pagecache);
  635. /**
  636. * truncate_setsize - update inode and pagecache for a new file size
  637. * @inode: inode
  638. * @newsize: new file size
  639. *
  640. * truncate_setsize updates i_size and performs pagecache truncation (if
  641. * necessary) to @newsize. It will be typically be called from the filesystem's
  642. * setattr function when ATTR_SIZE is passed in.
  643. *
  644. * Must be called with a lock serializing truncates and writes (generally
  645. * i_mutex but e.g. xfs uses a different lock) and before all filesystem
  646. * specific block truncation has been performed.
  647. */
  648. void truncate_setsize(struct inode *inode, loff_t newsize)
  649. {
  650. loff_t oldsize = inode->i_size;
  651. i_size_write(inode, newsize);
  652. if (newsize > oldsize)
  653. pagecache_isize_extended(inode, oldsize, newsize);
  654. truncate_pagecache(inode, newsize);
  655. }
  656. EXPORT_SYMBOL(truncate_setsize);
  657. /**
  658. * pagecache_isize_extended - update pagecache after extension of i_size
  659. * @inode: inode for which i_size was extended
  660. * @from: original inode size
  661. * @to: new inode size
  662. *
  663. * Handle extension of inode size either caused by extending truncate or by
  664. * write starting after current i_size. We mark the page straddling current
  665. * i_size RO so that page_mkwrite() is called on the nearest write access to
  666. * the page. This way filesystem can be sure that page_mkwrite() is called on
  667. * the page before user writes to the page via mmap after the i_size has been
  668. * changed.
  669. *
  670. * The function must be called after i_size is updated so that page fault
  671. * coming after we unlock the page will already see the new i_size.
  672. * The function must be called while we still hold i_mutex - this not only
  673. * makes sure i_size is stable but also that userspace cannot observe new
  674. * i_size value before we are prepared to store mmap writes at new inode size.
  675. */
  676. void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to)
  677. {
  678. int bsize = 1 << inode->i_blkbits;
  679. loff_t rounded_from;
  680. struct page *page;
  681. pgoff_t index;
  682. WARN_ON(to > inode->i_size);
  683. if (from >= to || bsize == PAGE_SIZE)
  684. return;
  685. /* Page straddling @from will not have any hole block created? */
  686. rounded_from = round_up(from, bsize);
  687. if (to <= rounded_from || !(rounded_from & (PAGE_SIZE - 1)))
  688. return;
  689. index = from >> PAGE_SHIFT;
  690. page = find_lock_page(inode->i_mapping, index);
  691. /* Page not cached? Nothing to do */
  692. if (!page)
  693. return;
  694. /*
  695. * See clear_page_dirty_for_io() for details why set_page_dirty()
  696. * is needed.
  697. */
  698. if (page_mkclean(page))
  699. set_page_dirty(page);
  700. unlock_page(page);
  701. put_page(page);
  702. }
  703. EXPORT_SYMBOL(pagecache_isize_extended);
  704. /**
  705. * truncate_pagecache_range - unmap and remove pagecache that is hole-punched
  706. * @inode: inode
  707. * @lstart: offset of beginning of hole
  708. * @lend: offset of last byte of hole
  709. *
  710. * This function should typically be called before the filesystem
  711. * releases resources associated with the freed range (eg. deallocates
  712. * blocks). This way, pagecache will always stay logically coherent
  713. * with on-disk format, and the filesystem would not have to deal with
  714. * situations such as writepage being called for a page that has already
  715. * had its underlying blocks deallocated.
  716. */
  717. void truncate_pagecache_range(struct inode *inode, loff_t lstart, loff_t lend)
  718. {
  719. struct address_space *mapping = inode->i_mapping;
  720. loff_t unmap_start = round_up(lstart, PAGE_SIZE);
  721. loff_t unmap_end = round_down(1 + lend, PAGE_SIZE) - 1;
  722. /*
  723. * This rounding is currently just for example: unmap_mapping_range
  724. * expands its hole outwards, whereas we want it to contract the hole
  725. * inwards. However, existing callers of truncate_pagecache_range are
  726. * doing their own page rounding first. Note that unmap_mapping_range
  727. * allows holelen 0 for all, and we allow lend -1 for end of file.
  728. */
  729. /*
  730. * Unlike in truncate_pagecache, unmap_mapping_range is called only
  731. * once (before truncating pagecache), and without "even_cows" flag:
  732. * hole-punching should not remove private COWed pages from the hole.
  733. */
  734. if ((u64)unmap_end > (u64)unmap_start)
  735. unmap_mapping_range(mapping, unmap_start,
  736. 1 + unmap_end - unmap_start, 0);
  737. truncate_inode_pages_range(mapping, lstart, lend);
  738. }
  739. EXPORT_SYMBOL(truncate_pagecache_range);