truncate.c 24 KB

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