truncate.c 24 KB

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