truncate.c 26 KB

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