truncate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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/mm.h>
  12. #include <linux/swap.h>
  13. #include <linux/module.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/highmem.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/task_io_accounting_ops.h>
  18. #include <linux/buffer_head.h> /* grr. try_to_release_page,
  19. do_invalidatepage */
  20. #include "internal.h"
  21. /**
  22. * do_invalidatepage - invalidate part or all of a page
  23. * @page: the page which is affected
  24. * @offset: the index of the truncation point
  25. *
  26. * do_invalidatepage() is called when all or part of the page has become
  27. * invalidated by a truncate operation.
  28. *
  29. * do_invalidatepage() does not have to release all buffers, but it must
  30. * ensure that no dirty buffer is left outside @offset and that no I/O
  31. * is underway against any of the blocks which are outside the truncation
  32. * point. Because the caller is about to free (and possibly reuse) those
  33. * blocks on-disk.
  34. */
  35. void do_invalidatepage(struct page *page, unsigned long offset)
  36. {
  37. void (*invalidatepage)(struct page *, unsigned long);
  38. invalidatepage = page->mapping->a_ops->invalidatepage;
  39. #ifdef CONFIG_BLOCK
  40. if (!invalidatepage)
  41. invalidatepage = block_invalidatepage;
  42. #endif
  43. if (invalidatepage)
  44. (*invalidatepage)(page, offset);
  45. }
  46. static inline void truncate_partial_page(struct page *page, unsigned partial)
  47. {
  48. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  49. if (page_has_private(page))
  50. do_invalidatepage(page, partial);
  51. }
  52. /*
  53. * This cancels just the dirty bit on the kernel page itself, it
  54. * does NOT actually remove dirty bits on any mmap's that may be
  55. * around. It also leaves the page tagged dirty, so any sync
  56. * activity will still find it on the dirty lists, and in particular,
  57. * clear_page_dirty_for_io() will still look at the dirty bits in
  58. * the VM.
  59. *
  60. * Doing this should *normally* only ever be done when a page
  61. * is truncated, and is not actually mapped anywhere at all. However,
  62. * fs/buffer.c does this when it notices that somebody has cleaned
  63. * out all the buffers on a page without actually doing it through
  64. * the VM. Can you say "ext3 is horribly ugly"? Tought you could.
  65. */
  66. void cancel_dirty_page(struct page *page, unsigned int account_size)
  67. {
  68. if (TestClearPageDirty(page)) {
  69. struct address_space *mapping = page->mapping;
  70. if (mapping && mapping_cap_account_dirty(mapping)) {
  71. dec_zone_page_state(page, NR_FILE_DIRTY);
  72. dec_bdi_stat(mapping->backing_dev_info,
  73. BDI_RECLAIMABLE);
  74. if (account_size)
  75. task_io_account_cancelled_write(account_size);
  76. }
  77. }
  78. }
  79. EXPORT_SYMBOL(cancel_dirty_page);
  80. /*
  81. * If truncate cannot remove the fs-private metadata from the page, the page
  82. * becomes orphaned. It will be left on the LRU and may even be mapped into
  83. * user pagetables if we're racing with filemap_fault().
  84. *
  85. * We need to bale out if page->mapping is no longer equal to the original
  86. * mapping. This happens a) when the VM reclaimed the page while we waited on
  87. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  88. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  89. */
  90. static void
  91. truncate_complete_page(struct address_space *mapping, struct page *page)
  92. {
  93. if (page->mapping != mapping)
  94. return;
  95. if (page_has_private(page))
  96. do_invalidatepage(page, 0);
  97. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  98. clear_page_mlock(page);
  99. remove_from_page_cache(page);
  100. ClearPageMappedToDisk(page);
  101. page_cache_release(page); /* pagecache ref */
  102. }
  103. /*
  104. * This is for invalidate_mapping_pages(). That function can be called at
  105. * any time, and is not supposed to throw away dirty pages. But pages can
  106. * be marked dirty at any time too, so use remove_mapping which safely
  107. * discards clean, unused pages.
  108. *
  109. * Returns non-zero if the page was successfully invalidated.
  110. */
  111. static int
  112. invalidate_complete_page(struct address_space *mapping, struct page *page)
  113. {
  114. int ret;
  115. if (page->mapping != mapping)
  116. return 0;
  117. if (page_has_private(page) && !try_to_release_page(page, 0))
  118. return 0;
  119. clear_page_mlock(page);
  120. ret = remove_mapping(mapping, page);
  121. return ret;
  122. }
  123. /**
  124. * truncate_inode_pages - truncate range of pages specified by start & end byte offsets
  125. * @mapping: mapping to truncate
  126. * @lstart: offset from which to truncate
  127. * @lend: offset to which to truncate
  128. *
  129. * Truncate the page cache, removing the pages that are between
  130. * specified offsets (and zeroing out partial page
  131. * (if lstart is not page aligned)).
  132. *
  133. * Truncate takes two passes - the first pass is nonblocking. It will not
  134. * block on page locks and it will not block on writeback. The second pass
  135. * will wait. This is to prevent as much IO as possible in the affected region.
  136. * The first pass will remove most pages, so the search cost of the second pass
  137. * is low.
  138. *
  139. * When looking at page->index outside the page lock we need to be careful to
  140. * copy it into a local to avoid races (it could change at any time).
  141. *
  142. * We pass down the cache-hot hint to the page freeing code. Even if the
  143. * mapping is large, it is probably the case that the final pages are the most
  144. * recently touched, and freeing happens in ascending file offset order.
  145. */
  146. void truncate_inode_pages_range(struct address_space *mapping,
  147. loff_t lstart, loff_t lend)
  148. {
  149. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  150. pgoff_t end;
  151. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  152. struct pagevec pvec;
  153. pgoff_t next;
  154. int i;
  155. if (mapping->nrpages == 0)
  156. return;
  157. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  158. end = (lend >> PAGE_CACHE_SHIFT);
  159. pagevec_init(&pvec, 0);
  160. next = start;
  161. while (next <= end &&
  162. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  163. for (i = 0; i < pagevec_count(&pvec); i++) {
  164. struct page *page = pvec.pages[i];
  165. pgoff_t page_index = page->index;
  166. if (page_index > end) {
  167. next = page_index;
  168. break;
  169. }
  170. if (page_index > next)
  171. next = page_index;
  172. next++;
  173. if (!trylock_page(page))
  174. continue;
  175. if (PageWriteback(page)) {
  176. unlock_page(page);
  177. continue;
  178. }
  179. if (page_mapped(page)) {
  180. unmap_mapping_range(mapping,
  181. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  182. PAGE_CACHE_SIZE, 0);
  183. }
  184. truncate_complete_page(mapping, page);
  185. unlock_page(page);
  186. }
  187. pagevec_release(&pvec);
  188. cond_resched();
  189. }
  190. if (partial) {
  191. struct page *page = find_lock_page(mapping, start - 1);
  192. if (page) {
  193. wait_on_page_writeback(page);
  194. truncate_partial_page(page, partial);
  195. unlock_page(page);
  196. page_cache_release(page);
  197. }
  198. }
  199. next = start;
  200. for ( ; ; ) {
  201. cond_resched();
  202. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  203. if (next == start)
  204. break;
  205. next = start;
  206. continue;
  207. }
  208. if (pvec.pages[0]->index > end) {
  209. pagevec_release(&pvec);
  210. break;
  211. }
  212. for (i = 0; i < pagevec_count(&pvec); i++) {
  213. struct page *page = pvec.pages[i];
  214. if (page->index > end)
  215. break;
  216. lock_page(page);
  217. wait_on_page_writeback(page);
  218. if (page_mapped(page)) {
  219. unmap_mapping_range(mapping,
  220. (loff_t)page->index<<PAGE_CACHE_SHIFT,
  221. PAGE_CACHE_SIZE, 0);
  222. }
  223. if (page->index > next)
  224. next = page->index;
  225. next++;
  226. truncate_complete_page(mapping, page);
  227. unlock_page(page);
  228. }
  229. pagevec_release(&pvec);
  230. }
  231. }
  232. EXPORT_SYMBOL(truncate_inode_pages_range);
  233. /**
  234. * truncate_inode_pages - truncate *all* the pages from an offset
  235. * @mapping: mapping to truncate
  236. * @lstart: offset from which to truncate
  237. *
  238. * Called under (and serialised by) inode->i_mutex.
  239. */
  240. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  241. {
  242. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  243. }
  244. EXPORT_SYMBOL(truncate_inode_pages);
  245. unsigned long __invalidate_mapping_pages(struct address_space *mapping,
  246. pgoff_t start, pgoff_t end, bool be_atomic)
  247. {
  248. struct pagevec pvec;
  249. pgoff_t next = start;
  250. unsigned long ret = 0;
  251. int i;
  252. pagevec_init(&pvec, 0);
  253. while (next <= end &&
  254. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  255. for (i = 0; i < pagevec_count(&pvec); i++) {
  256. struct page *page = pvec.pages[i];
  257. pgoff_t index;
  258. int lock_failed;
  259. lock_failed = !trylock_page(page);
  260. /*
  261. * We really shouldn't be looking at the ->index of an
  262. * unlocked page. But we're not allowed to lock these
  263. * pages. So we rely upon nobody altering the ->index
  264. * of this (pinned-by-us) page.
  265. */
  266. index = page->index;
  267. if (index > next)
  268. next = index;
  269. next++;
  270. if (lock_failed)
  271. continue;
  272. if (PageDirty(page) || PageWriteback(page))
  273. goto unlock;
  274. if (page_mapped(page))
  275. goto unlock;
  276. ret += invalidate_complete_page(mapping, page);
  277. unlock:
  278. unlock_page(page);
  279. if (next > end)
  280. break;
  281. }
  282. pagevec_release(&pvec);
  283. if (likely(!be_atomic))
  284. cond_resched();
  285. }
  286. return ret;
  287. }
  288. /**
  289. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  290. * @mapping: the address_space which holds the pages to invalidate
  291. * @start: the offset 'from' which to invalidate
  292. * @end: the offset 'to' which to invalidate (inclusive)
  293. *
  294. * This function only removes the unlocked pages, if you want to
  295. * remove all the pages of one inode, you must call truncate_inode_pages.
  296. *
  297. * invalidate_mapping_pages() will not block on IO activity. It will not
  298. * invalidate pages which are dirty, locked, under writeback or mapped into
  299. * pagetables.
  300. */
  301. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  302. pgoff_t start, pgoff_t end)
  303. {
  304. return __invalidate_mapping_pages(mapping, start, end, false);
  305. }
  306. EXPORT_SYMBOL(invalidate_mapping_pages);
  307. /*
  308. * This is like invalidate_complete_page(), except it ignores the page's
  309. * refcount. We do this because invalidate_inode_pages2() needs stronger
  310. * invalidation guarantees, and cannot afford to leave pages behind because
  311. * shrink_page_list() has a temp ref on them, or because they're transiently
  312. * sitting in the lru_cache_add() pagevecs.
  313. */
  314. static int
  315. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  316. {
  317. if (page->mapping != mapping)
  318. return 0;
  319. if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
  320. return 0;
  321. spin_lock_irq(&mapping->tree_lock);
  322. if (PageDirty(page))
  323. goto failed;
  324. clear_page_mlock(page);
  325. BUG_ON(page_has_private(page));
  326. __remove_from_page_cache(page);
  327. spin_unlock_irq(&mapping->tree_lock);
  328. page_cache_release(page); /* pagecache ref */
  329. return 1;
  330. failed:
  331. spin_unlock_irq(&mapping->tree_lock);
  332. return 0;
  333. }
  334. static int do_launder_page(struct address_space *mapping, struct page *page)
  335. {
  336. if (!PageDirty(page))
  337. return 0;
  338. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  339. return 0;
  340. return mapping->a_ops->launder_page(page);
  341. }
  342. /**
  343. * invalidate_inode_pages2_range - remove range of pages from an address_space
  344. * @mapping: the address_space
  345. * @start: the page offset 'from' which to invalidate
  346. * @end: the page offset 'to' which to invalidate (inclusive)
  347. *
  348. * Any pages which are found to be mapped into pagetables are unmapped prior to
  349. * invalidation.
  350. *
  351. * Returns -EBUSY if any pages could not be invalidated.
  352. */
  353. int invalidate_inode_pages2_range(struct address_space *mapping,
  354. pgoff_t start, pgoff_t end)
  355. {
  356. struct pagevec pvec;
  357. pgoff_t next;
  358. int i;
  359. int ret = 0;
  360. int ret2 = 0;
  361. int did_range_unmap = 0;
  362. int wrapped = 0;
  363. pagevec_init(&pvec, 0);
  364. next = start;
  365. while (next <= end && !wrapped &&
  366. pagevec_lookup(&pvec, mapping, next,
  367. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  368. for (i = 0; i < pagevec_count(&pvec); i++) {
  369. struct page *page = pvec.pages[i];
  370. pgoff_t page_index;
  371. lock_page(page);
  372. if (page->mapping != mapping) {
  373. unlock_page(page);
  374. continue;
  375. }
  376. page_index = page->index;
  377. next = page_index + 1;
  378. if (next == 0)
  379. wrapped = 1;
  380. if (page_index > end) {
  381. unlock_page(page);
  382. break;
  383. }
  384. wait_on_page_writeback(page);
  385. if (page_mapped(page)) {
  386. if (!did_range_unmap) {
  387. /*
  388. * Zap the rest of the file in one hit.
  389. */
  390. unmap_mapping_range(mapping,
  391. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  392. (loff_t)(end - page_index + 1)
  393. << PAGE_CACHE_SHIFT,
  394. 0);
  395. did_range_unmap = 1;
  396. } else {
  397. /*
  398. * Just zap this page
  399. */
  400. unmap_mapping_range(mapping,
  401. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  402. PAGE_CACHE_SIZE, 0);
  403. }
  404. }
  405. BUG_ON(page_mapped(page));
  406. ret2 = do_launder_page(mapping, page);
  407. if (ret2 == 0) {
  408. if (!invalidate_complete_page2(mapping, page))
  409. ret2 = -EBUSY;
  410. }
  411. if (ret2 < 0)
  412. ret = ret2;
  413. unlock_page(page);
  414. }
  415. pagevec_release(&pvec);
  416. cond_resched();
  417. }
  418. return ret;
  419. }
  420. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  421. /**
  422. * invalidate_inode_pages2 - remove all pages from an address_space
  423. * @mapping: the address_space
  424. *
  425. * Any pages which are found to be mapped into pagetables are unmapped prior to
  426. * invalidation.
  427. *
  428. * Returns -EIO if any pages could not be invalidated.
  429. */
  430. int invalidate_inode_pages2(struct address_space *mapping)
  431. {
  432. return invalidate_inode_pages2_range(mapping, 0, -1);
  433. }
  434. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);