truncate.c 13 KB

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