truncate.c 24 KB

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