truncate.c 24 KB

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