truncate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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_fault().
  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. if (page_mapped(page)) {
  176. unmap_mapping_range(mapping,
  177. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  178. PAGE_CACHE_SIZE, 0);
  179. }
  180. truncate_complete_page(mapping, page);
  181. unlock_page(page);
  182. }
  183. pagevec_release(&pvec);
  184. cond_resched();
  185. }
  186. if (partial) {
  187. struct page *page = find_lock_page(mapping, start - 1);
  188. if (page) {
  189. wait_on_page_writeback(page);
  190. truncate_partial_page(page, partial);
  191. unlock_page(page);
  192. page_cache_release(page);
  193. }
  194. }
  195. next = start;
  196. for ( ; ; ) {
  197. cond_resched();
  198. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  199. if (next == start)
  200. break;
  201. next = start;
  202. continue;
  203. }
  204. if (pvec.pages[0]->index > end) {
  205. pagevec_release(&pvec);
  206. break;
  207. }
  208. for (i = 0; i < pagevec_count(&pvec); i++) {
  209. struct page *page = pvec.pages[i];
  210. if (page->index > end)
  211. break;
  212. lock_page(page);
  213. wait_on_page_writeback(page);
  214. if (page_mapped(page)) {
  215. unmap_mapping_range(mapping,
  216. (loff_t)page->index<<PAGE_CACHE_SHIFT,
  217. PAGE_CACHE_SIZE, 0);
  218. }
  219. if (page->index > next)
  220. next = page->index;
  221. next++;
  222. truncate_complete_page(mapping, page);
  223. unlock_page(page);
  224. }
  225. pagevec_release(&pvec);
  226. }
  227. }
  228. EXPORT_SYMBOL(truncate_inode_pages_range);
  229. /**
  230. * truncate_inode_pages - truncate *all* the pages from an offset
  231. * @mapping: mapping to truncate
  232. * @lstart: offset from which to truncate
  233. *
  234. * Called under (and serialised by) inode->i_mutex.
  235. */
  236. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  237. {
  238. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  239. }
  240. EXPORT_SYMBOL(truncate_inode_pages);
  241. unsigned long __invalidate_mapping_pages(struct address_space *mapping,
  242. pgoff_t start, pgoff_t end, bool be_atomic)
  243. {
  244. struct pagevec pvec;
  245. pgoff_t next = start;
  246. unsigned long ret = 0;
  247. int i;
  248. pagevec_init(&pvec, 0);
  249. while (next <= end &&
  250. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  251. for (i = 0; i < pagevec_count(&pvec); i++) {
  252. struct page *page = pvec.pages[i];
  253. pgoff_t index;
  254. int lock_failed;
  255. lock_failed = TestSetPageLocked(page);
  256. /*
  257. * We really shouldn't be looking at the ->index of an
  258. * unlocked page. But we're not allowed to lock these
  259. * pages. So we rely upon nobody altering the ->index
  260. * of this (pinned-by-us) page.
  261. */
  262. index = page->index;
  263. if (index > next)
  264. next = index;
  265. next++;
  266. if (lock_failed)
  267. continue;
  268. if (PageDirty(page) || PageWriteback(page))
  269. goto unlock;
  270. if (page_mapped(page))
  271. goto unlock;
  272. ret += invalidate_complete_page(mapping, page);
  273. unlock:
  274. unlock_page(page);
  275. if (next > end)
  276. break;
  277. }
  278. pagevec_release(&pvec);
  279. if (likely(!be_atomic))
  280. cond_resched();
  281. }
  282. return ret;
  283. }
  284. /**
  285. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  286. * @mapping: the address_space which holds the pages to invalidate
  287. * @start: the offset 'from' which to invalidate
  288. * @end: the offset 'to' which to invalidate (inclusive)
  289. *
  290. * This function only removes the unlocked pages, if you want to
  291. * remove all the pages of one inode, you must call truncate_inode_pages.
  292. *
  293. * invalidate_mapping_pages() will not block on IO activity. It will not
  294. * invalidate pages which are dirty, locked, under writeback or mapped into
  295. * pagetables.
  296. */
  297. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  298. pgoff_t start, pgoff_t end)
  299. {
  300. return __invalidate_mapping_pages(mapping, start, end, false);
  301. }
  302. EXPORT_SYMBOL(invalidate_mapping_pages);
  303. /*
  304. * This is like invalidate_complete_page(), except it ignores the page's
  305. * refcount. We do this because invalidate_inode_pages2() needs stronger
  306. * invalidation guarantees, and cannot afford to leave pages behind because
  307. * shrink_page_list() has a temp ref on them, or because they're transiently
  308. * sitting in the lru_cache_add() pagevecs.
  309. */
  310. static int
  311. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  312. {
  313. if (page->mapping != mapping)
  314. return 0;
  315. if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  316. return 0;
  317. write_lock_irq(&mapping->tree_lock);
  318. if (PageDirty(page))
  319. goto failed;
  320. BUG_ON(PagePrivate(page));
  321. __remove_from_page_cache(page);
  322. write_unlock_irq(&mapping->tree_lock);
  323. ClearPageUptodate(page);
  324. page_cache_release(page); /* pagecache ref */
  325. return 1;
  326. failed:
  327. write_unlock_irq(&mapping->tree_lock);
  328. return 0;
  329. }
  330. static int do_launder_page(struct address_space *mapping, struct page *page)
  331. {
  332. if (!PageDirty(page))
  333. return 0;
  334. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  335. return 0;
  336. return mapping->a_ops->launder_page(page);
  337. }
  338. /**
  339. * invalidate_inode_pages2_range - remove range of pages from an address_space
  340. * @mapping: the address_space
  341. * @start: the page offset 'from' which to invalidate
  342. * @end: the page offset 'to' which to invalidate (inclusive)
  343. *
  344. * Any pages which are found to be mapped into pagetables are unmapped prior to
  345. * invalidation.
  346. *
  347. * Returns -EIO if any pages could not be invalidated.
  348. */
  349. int invalidate_inode_pages2_range(struct address_space *mapping,
  350. pgoff_t start, pgoff_t end)
  351. {
  352. struct pagevec pvec;
  353. pgoff_t next;
  354. int i;
  355. int ret = 0;
  356. int did_range_unmap = 0;
  357. int wrapped = 0;
  358. pagevec_init(&pvec, 0);
  359. next = start;
  360. while (next <= end && !wrapped &&
  361. pagevec_lookup(&pvec, mapping, next,
  362. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  363. for (i = 0; i < pagevec_count(&pvec); i++) {
  364. struct page *page = pvec.pages[i];
  365. pgoff_t page_index;
  366. lock_page(page);
  367. if (page->mapping != mapping) {
  368. unlock_page(page);
  369. continue;
  370. }
  371. page_index = page->index;
  372. next = page_index + 1;
  373. if (next == 0)
  374. wrapped = 1;
  375. if (page_index > end) {
  376. unlock_page(page);
  377. break;
  378. }
  379. wait_on_page_writeback(page);
  380. if (page_mapped(page)) {
  381. if (!did_range_unmap) {
  382. /*
  383. * Zap the rest of the file in one hit.
  384. */
  385. unmap_mapping_range(mapping,
  386. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  387. (loff_t)(end - page_index + 1)
  388. << PAGE_CACHE_SHIFT,
  389. 0);
  390. did_range_unmap = 1;
  391. } else {
  392. /*
  393. * Just zap this page
  394. */
  395. unmap_mapping_range(mapping,
  396. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  397. PAGE_CACHE_SIZE, 0);
  398. }
  399. }
  400. BUG_ON(page_mapped(page));
  401. ret = do_launder_page(mapping, page);
  402. if (ret == 0 && !invalidate_complete_page2(mapping, page))
  403. ret = -EIO;
  404. unlock_page(page);
  405. }
  406. pagevec_release(&pvec);
  407. cond_resched();
  408. }
  409. return ret;
  410. }
  411. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  412. /**
  413. * invalidate_inode_pages2 - remove all pages from an address_space
  414. * @mapping: the address_space
  415. *
  416. * Any pages which are found to be mapped into pagetables are unmapped prior to
  417. * invalidation.
  418. *
  419. * Returns -EIO if any pages could not be invalidated.
  420. */
  421. int invalidate_inode_pages2(struct address_space *mapping)
  422. {
  423. return invalidate_inode_pages2_range(mapping, 0, -1);
  424. }
  425. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);