truncate.c 23 KB

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