truncate.c 25 KB

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