truncate.c 13 KB

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