truncate.c 25 KB

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