truncate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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().
  65. */
  66. static int invalidate_exceptional_entry(struct address_space *mapping,
  67. pgoff_t index, void *entry)
  68. {
  69. /* Handled by shmem itself, or for DAX we do nothing. */
  70. if (shmem_mapping(mapping) || dax_mapping(mapping))
  71. return 1;
  72. clear_shadow_entry(mapping, index, entry);
  73. return 1;
  74. }
  75. /*
  76. * Invalidate exceptional entry if clean. This handles exceptional entries for
  77. * invalidate_inode_pages2() so for DAX it evicts only clean entries.
  78. */
  79. static int invalidate_exceptional_entry2(struct address_space *mapping,
  80. pgoff_t index, void *entry)
  81. {
  82. /* Handled by shmem itself */
  83. if (shmem_mapping(mapping))
  84. return 1;
  85. if (dax_mapping(mapping))
  86. return dax_invalidate_mapping_entry_sync(mapping, index);
  87. clear_shadow_entry(mapping, index, entry);
  88. return 1;
  89. }
  90. /**
  91. * do_invalidatepage - invalidate part or all of a page
  92. * @page: the page which is affected
  93. * @offset: start of the range to invalidate
  94. * @length: length of the range to invalidate
  95. *
  96. * do_invalidatepage() is called when all or part of the page has become
  97. * invalidated by a truncate operation.
  98. *
  99. * do_invalidatepage() does not have to release all buffers, but it must
  100. * ensure that no dirty buffer is left outside @offset and that no I/O
  101. * is underway against any of the blocks which are outside the truncation
  102. * point. Because the caller is about to free (and possibly reuse) those
  103. * blocks on-disk.
  104. */
  105. void do_invalidatepage(struct page *page, unsigned int offset,
  106. unsigned int length)
  107. {
  108. void (*invalidatepage)(struct page *, unsigned int, unsigned int);
  109. invalidatepage = page->mapping->a_ops->invalidatepage;
  110. #ifdef CONFIG_BLOCK
  111. if (!invalidatepage)
  112. invalidatepage = block_invalidatepage;
  113. #endif
  114. if (invalidatepage)
  115. (*invalidatepage)(page, offset, length);
  116. }
  117. /*
  118. * If truncate cannot remove the fs-private metadata from the page, the page
  119. * becomes orphaned. It will be left on the LRU and may even be mapped into
  120. * user pagetables if we're racing with filemap_fault().
  121. *
  122. * We need to bale out if page->mapping is no longer equal to the original
  123. * mapping. This happens a) when the VM reclaimed the page while we waited on
  124. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  125. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  126. */
  127. static int
  128. truncate_complete_page(struct address_space *mapping, struct page *page)
  129. {
  130. if (page->mapping != mapping)
  131. return -EIO;
  132. if (page_has_private(page))
  133. do_invalidatepage(page, 0, PAGE_SIZE);
  134. /*
  135. * Some filesystems seem to re-dirty the page even after
  136. * the VM has canceled the dirty bit (eg ext3 journaling).
  137. * Hence dirty accounting check is placed after invalidation.
  138. */
  139. cancel_dirty_page(page);
  140. ClearPageMappedToDisk(page);
  141. delete_from_page_cache(page);
  142. return 0;
  143. }
  144. /*
  145. * This is for invalidate_mapping_pages(). That function can be called at
  146. * any time, and is not supposed to throw away dirty pages. But pages can
  147. * be marked dirty at any time too, so use remove_mapping which safely
  148. * discards clean, unused pages.
  149. *
  150. * Returns non-zero if the page was successfully invalidated.
  151. */
  152. static int
  153. invalidate_complete_page(struct address_space *mapping, struct page *page)
  154. {
  155. int ret;
  156. if (page->mapping != mapping)
  157. return 0;
  158. if (page_has_private(page) && !try_to_release_page(page, 0))
  159. return 0;
  160. ret = remove_mapping(mapping, page);
  161. return ret;
  162. }
  163. int truncate_inode_page(struct address_space *mapping, struct page *page)
  164. {
  165. loff_t holelen;
  166. VM_BUG_ON_PAGE(PageTail(page), page);
  167. holelen = PageTransHuge(page) ? HPAGE_PMD_SIZE : PAGE_SIZE;
  168. if (page_mapped(page)) {
  169. unmap_mapping_range(mapping,
  170. (loff_t)page->index << PAGE_SHIFT,
  171. holelen, 0);
  172. }
  173. return truncate_complete_page(mapping, page);
  174. }
  175. /*
  176. * Used to get rid of pages on hardware memory corruption.
  177. */
  178. int generic_error_remove_page(struct address_space *mapping, struct page *page)
  179. {
  180. if (!mapping)
  181. return -EINVAL;
  182. /*
  183. * Only punch for normal data pages for now.
  184. * Handling other types like directories would need more auditing.
  185. */
  186. if (!S_ISREG(mapping->host->i_mode))
  187. return -EIO;
  188. return truncate_inode_page(mapping, page);
  189. }
  190. EXPORT_SYMBOL(generic_error_remove_page);
  191. /*
  192. * Safely invalidate one page from its pagecache mapping.
  193. * It only drops clean, unused pages. The page must be locked.
  194. *
  195. * Returns 1 if the page is successfully invalidated, otherwise 0.
  196. */
  197. int invalidate_inode_page(struct page *page)
  198. {
  199. struct address_space *mapping = page_mapping(page);
  200. if (!mapping)
  201. return 0;
  202. if (PageDirty(page) || PageWriteback(page))
  203. return 0;
  204. if (page_mapped(page))
  205. return 0;
  206. return invalidate_complete_page(mapping, page);
  207. }
  208. /**
  209. * truncate_inode_pages_range - truncate range of pages specified by start & end byte offsets
  210. * @mapping: mapping to truncate
  211. * @lstart: offset from which to truncate
  212. * @lend: offset to which to truncate (inclusive)
  213. *
  214. * Truncate the page cache, removing the pages that are between
  215. * specified offsets (and zeroing out partial pages
  216. * if lstart or lend + 1 is not page aligned).
  217. *
  218. * Truncate takes two passes - the first pass is nonblocking. It will not
  219. * block on page locks and it will not block on writeback. The second pass
  220. * will wait. This is to prevent as much IO as possible in the affected region.
  221. * The first pass will remove most pages, so the search cost of the second pass
  222. * is low.
  223. *
  224. * We pass down the cache-hot hint to the page freeing code. Even if the
  225. * mapping is large, it is probably the case that the final pages are the most
  226. * recently touched, and freeing happens in ascending file offset order.
  227. *
  228. * Note that since ->invalidatepage() accepts range to invalidate
  229. * truncate_inode_pages_range is able to handle cases where lend + 1 is not
  230. * page aligned properly.
  231. */
  232. void truncate_inode_pages_range(struct address_space *mapping,
  233. loff_t lstart, loff_t lend)
  234. {
  235. pgoff_t start; /* inclusive */
  236. pgoff_t end; /* exclusive */
  237. unsigned int partial_start; /* inclusive */
  238. unsigned int partial_end; /* exclusive */
  239. struct pagevec pvec;
  240. pgoff_t indices[PAGEVEC_SIZE];
  241. pgoff_t index;
  242. int i;
  243. if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
  244. goto out;
  245. /* Offsets within partial pages */
  246. partial_start = lstart & (PAGE_SIZE - 1);
  247. partial_end = (lend + 1) & (PAGE_SIZE - 1);
  248. /*
  249. * 'start' and 'end' always covers the range of pages to be fully
  250. * truncated. Partial pages are covered with 'partial_start' at the
  251. * start of the range and 'partial_end' at the end of the range.
  252. * Note that 'end' is exclusive while 'lend' is inclusive.
  253. */
  254. start = (lstart + PAGE_SIZE - 1) >> PAGE_SHIFT;
  255. if (lend == -1)
  256. /*
  257. * lend == -1 indicates end-of-file so we have to set 'end'
  258. * to the highest possible pgoff_t and since the type is
  259. * unsigned we're using -1.
  260. */
  261. end = -1;
  262. else
  263. end = (lend + 1) >> PAGE_SHIFT;
  264. pagevec_init(&pvec, 0);
  265. index = start;
  266. while (index < end && pagevec_lookup_entries(&pvec, mapping, index,
  267. min(end - index, (pgoff_t)PAGEVEC_SIZE),
  268. indices)) {
  269. for (i = 0; i < pagevec_count(&pvec); i++) {
  270. struct page *page = pvec.pages[i];
  271. /* We rely upon deletion not changing page->index */
  272. index = indices[i];
  273. if (index >= end)
  274. break;
  275. if (radix_tree_exceptional_entry(page)) {
  276. truncate_exceptional_entry(mapping, index,
  277. page);
  278. continue;
  279. }
  280. if (!trylock_page(page))
  281. continue;
  282. WARN_ON(page_to_index(page) != index);
  283. if (PageWriteback(page)) {
  284. unlock_page(page);
  285. continue;
  286. }
  287. truncate_inode_page(mapping, page);
  288. unlock_page(page);
  289. }
  290. pagevec_remove_exceptionals(&pvec);
  291. pagevec_release(&pvec);
  292. cond_resched();
  293. index++;
  294. }
  295. if (partial_start) {
  296. struct page *page = find_lock_page(mapping, start - 1);
  297. if (page) {
  298. unsigned int top = PAGE_SIZE;
  299. if (start > end) {
  300. /* Truncation within a single page */
  301. top = partial_end;
  302. partial_end = 0;
  303. }
  304. wait_on_page_writeback(page);
  305. zero_user_segment(page, partial_start, top);
  306. cleancache_invalidate_page(mapping, page);
  307. if (page_has_private(page))
  308. do_invalidatepage(page, partial_start,
  309. top - partial_start);
  310. unlock_page(page);
  311. put_page(page);
  312. }
  313. }
  314. if (partial_end) {
  315. struct page *page = find_lock_page(mapping, end);
  316. if (page) {
  317. wait_on_page_writeback(page);
  318. zero_user_segment(page, 0, partial_end);
  319. cleancache_invalidate_page(mapping, page);
  320. if (page_has_private(page))
  321. do_invalidatepage(page, 0,
  322. partial_end);
  323. unlock_page(page);
  324. put_page(page);
  325. }
  326. }
  327. /*
  328. * If the truncation happened within a single page no pages
  329. * will be released, just zeroed, so we can bail out now.
  330. */
  331. if (start >= end)
  332. goto out;
  333. index = start;
  334. for ( ; ; ) {
  335. cond_resched();
  336. if (!pagevec_lookup_entries(&pvec, mapping, index,
  337. min(end - index, (pgoff_t)PAGEVEC_SIZE), indices)) {
  338. /* If all gone from start onwards, we're done */
  339. if (index == start)
  340. break;
  341. /* Otherwise restart to make sure all gone */
  342. index = start;
  343. continue;
  344. }
  345. if (index == start && indices[0] >= end) {
  346. /* All gone out of hole to be punched, we're done */
  347. pagevec_remove_exceptionals(&pvec);
  348. pagevec_release(&pvec);
  349. break;
  350. }
  351. for (i = 0; i < pagevec_count(&pvec); i++) {
  352. struct page *page = pvec.pages[i];
  353. /* We rely upon deletion not changing page->index */
  354. index = indices[i];
  355. if (index >= end) {
  356. /* Restart punch to make sure all gone */
  357. index = start - 1;
  358. break;
  359. }
  360. if (radix_tree_exceptional_entry(page)) {
  361. truncate_exceptional_entry(mapping, index,
  362. page);
  363. continue;
  364. }
  365. lock_page(page);
  366. WARN_ON(page_to_index(page) != index);
  367. wait_on_page_writeback(page);
  368. truncate_inode_page(mapping, page);
  369. unlock_page(page);
  370. }
  371. pagevec_remove_exceptionals(&pvec);
  372. pagevec_release(&pvec);
  373. index++;
  374. }
  375. out:
  376. cleancache_invalidate_inode(mapping);
  377. }
  378. EXPORT_SYMBOL(truncate_inode_pages_range);
  379. /**
  380. * truncate_inode_pages - truncate *all* the pages from an offset
  381. * @mapping: mapping to truncate
  382. * @lstart: offset from which to truncate
  383. *
  384. * Called under (and serialised by) inode->i_mutex.
  385. *
  386. * Note: When this function returns, there can be a page in the process of
  387. * deletion (inside __delete_from_page_cache()) in the specified range. Thus
  388. * mapping->nrpages can be non-zero when this function returns even after
  389. * truncation of the whole mapping.
  390. */
  391. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  392. {
  393. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  394. }
  395. EXPORT_SYMBOL(truncate_inode_pages);
  396. /**
  397. * truncate_inode_pages_final - truncate *all* pages before inode dies
  398. * @mapping: mapping to truncate
  399. *
  400. * Called under (and serialized by) inode->i_mutex.
  401. *
  402. * Filesystems have to use this in the .evict_inode path to inform the
  403. * VM that this is the final truncate and the inode is going away.
  404. */
  405. void truncate_inode_pages_final(struct address_space *mapping)
  406. {
  407. unsigned long nrexceptional;
  408. unsigned long nrpages;
  409. /*
  410. * Page reclaim can not participate in regular inode lifetime
  411. * management (can't call iput()) and thus can race with the
  412. * inode teardown. Tell it when the address space is exiting,
  413. * so that it does not install eviction information after the
  414. * final truncate has begun.
  415. */
  416. mapping_set_exiting(mapping);
  417. /*
  418. * When reclaim installs eviction entries, it increases
  419. * nrexceptional first, then decreases nrpages. Make sure we see
  420. * this in the right order or we might miss an entry.
  421. */
  422. nrpages = mapping->nrpages;
  423. smp_rmb();
  424. nrexceptional = mapping->nrexceptional;
  425. if (nrpages || nrexceptional) {
  426. /*
  427. * As truncation uses a lockless tree lookup, cycle
  428. * the tree lock to make sure any ongoing tree
  429. * modification that does not see AS_EXITING is
  430. * completed before starting the final truncate.
  431. */
  432. spin_lock_irq(&mapping->tree_lock);
  433. spin_unlock_irq(&mapping->tree_lock);
  434. truncate_inode_pages(mapping, 0);
  435. }
  436. }
  437. EXPORT_SYMBOL(truncate_inode_pages_final);
  438. /**
  439. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  440. * @mapping: the address_space which holds the pages to invalidate
  441. * @start: the offset 'from' which to invalidate
  442. * @end: the offset 'to' which to invalidate (inclusive)
  443. *
  444. * This function only removes the unlocked pages, if you want to
  445. * remove all the pages of one inode, you must call truncate_inode_pages.
  446. *
  447. * invalidate_mapping_pages() will not block on IO activity. It will not
  448. * invalidate pages which are dirty, locked, under writeback or mapped into
  449. * pagetables.
  450. */
  451. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  452. pgoff_t start, pgoff_t end)
  453. {
  454. pgoff_t indices[PAGEVEC_SIZE];
  455. struct pagevec pvec;
  456. pgoff_t index = start;
  457. unsigned long ret;
  458. unsigned long count = 0;
  459. int i;
  460. pagevec_init(&pvec, 0);
  461. while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
  462. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  463. indices)) {
  464. for (i = 0; i < pagevec_count(&pvec); i++) {
  465. struct page *page = pvec.pages[i];
  466. /* We rely upon deletion not changing page->index */
  467. index = indices[i];
  468. if (index > end)
  469. break;
  470. if (radix_tree_exceptional_entry(page)) {
  471. invalidate_exceptional_entry(mapping, index,
  472. page);
  473. continue;
  474. }
  475. if (!trylock_page(page))
  476. continue;
  477. WARN_ON(page_to_index(page) != index);
  478. /* Middle of THP: skip */
  479. if (PageTransTail(page)) {
  480. unlock_page(page);
  481. continue;
  482. } else if (PageTransHuge(page)) {
  483. index += HPAGE_PMD_NR - 1;
  484. i += HPAGE_PMD_NR - 1;
  485. /* 'end' is in the middle of THP */
  486. if (index == round_down(end, HPAGE_PMD_NR))
  487. continue;
  488. }
  489. ret = invalidate_inode_page(page);
  490. unlock_page(page);
  491. /*
  492. * Invalidation is a hint that the page is no longer
  493. * of interest and try to speed up its reclaim.
  494. */
  495. if (!ret)
  496. deactivate_file_page(page);
  497. count += ret;
  498. }
  499. pagevec_remove_exceptionals(&pvec);
  500. pagevec_release(&pvec);
  501. cond_resched();
  502. index++;
  503. }
  504. return count;
  505. }
  506. EXPORT_SYMBOL(invalidate_mapping_pages);
  507. /*
  508. * This is like invalidate_complete_page(), except it ignores the page's
  509. * refcount. We do this because invalidate_inode_pages2() needs stronger
  510. * invalidation guarantees, and cannot afford to leave pages behind because
  511. * shrink_page_list() has a temp ref on them, or because they're transiently
  512. * sitting in the lru_cache_add() pagevecs.
  513. */
  514. static int
  515. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  516. {
  517. unsigned long flags;
  518. if (page->mapping != mapping)
  519. return 0;
  520. if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
  521. return 0;
  522. spin_lock_irqsave(&mapping->tree_lock, flags);
  523. if (PageDirty(page))
  524. goto failed;
  525. BUG_ON(page_has_private(page));
  526. __delete_from_page_cache(page, NULL);
  527. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  528. if (mapping->a_ops->freepage)
  529. mapping->a_ops->freepage(page);
  530. put_page(page); /* pagecache ref */
  531. return 1;
  532. failed:
  533. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  534. return 0;
  535. }
  536. static int do_launder_page(struct address_space *mapping, struct page *page)
  537. {
  538. if (!PageDirty(page))
  539. return 0;
  540. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  541. return 0;
  542. return mapping->a_ops->launder_page(page);
  543. }
  544. /**
  545. * invalidate_inode_pages2_range - remove range of pages from an address_space
  546. * @mapping: the address_space
  547. * @start: the page offset 'from' which to invalidate
  548. * @end: the page offset 'to' which to invalidate (inclusive)
  549. *
  550. * Any pages which are found to be mapped into pagetables are unmapped prior to
  551. * invalidation.
  552. *
  553. * Returns -EBUSY if any pages could not be invalidated.
  554. */
  555. int invalidate_inode_pages2_range(struct address_space *mapping,
  556. pgoff_t start, pgoff_t end)
  557. {
  558. pgoff_t indices[PAGEVEC_SIZE];
  559. struct pagevec pvec;
  560. pgoff_t index;
  561. int i;
  562. int ret = 0;
  563. int ret2 = 0;
  564. int did_range_unmap = 0;
  565. if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
  566. goto out;
  567. pagevec_init(&pvec, 0);
  568. index = start;
  569. while (index <= end && pagevec_lookup_entries(&pvec, mapping, index,
  570. min(end - index, (pgoff_t)PAGEVEC_SIZE - 1) + 1,
  571. indices)) {
  572. for (i = 0; i < pagevec_count(&pvec); i++) {
  573. struct page *page = pvec.pages[i];
  574. /* We rely upon deletion not changing page->index */
  575. index = indices[i];
  576. if (index > end)
  577. break;
  578. if (radix_tree_exceptional_entry(page)) {
  579. if (!invalidate_exceptional_entry2(mapping,
  580. index, page))
  581. ret = -EBUSY;
  582. continue;
  583. }
  584. lock_page(page);
  585. WARN_ON(page_to_index(page) != index);
  586. if (page->mapping != mapping) {
  587. unlock_page(page);
  588. continue;
  589. }
  590. wait_on_page_writeback(page);
  591. if (page_mapped(page)) {
  592. if (!did_range_unmap) {
  593. /*
  594. * Zap the rest of the file in one hit.
  595. */
  596. unmap_mapping_range(mapping,
  597. (loff_t)index << PAGE_SHIFT,
  598. (loff_t)(1 + end - index)
  599. << PAGE_SHIFT,
  600. 0);
  601. did_range_unmap = 1;
  602. } else {
  603. /*
  604. * Just zap this page
  605. */
  606. unmap_mapping_range(mapping,
  607. (loff_t)index << PAGE_SHIFT,
  608. PAGE_SIZE, 0);
  609. }
  610. }
  611. BUG_ON(page_mapped(page));
  612. ret2 = do_launder_page(mapping, page);
  613. if (ret2 == 0) {
  614. if (!invalidate_complete_page2(mapping, page))
  615. ret2 = -EBUSY;
  616. }
  617. if (ret2 < 0)
  618. ret = ret2;
  619. unlock_page(page);
  620. }
  621. pagevec_remove_exceptionals(&pvec);
  622. pagevec_release(&pvec);
  623. cond_resched();
  624. index++;
  625. }
  626. out:
  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 = i_blocksize(inode);
  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);