truncate.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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. ClearPageUptodate(page);
  95. ClearPageMappedToDisk(page);
  96. remove_from_page_cache(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. /**
  232. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  233. * @mapping: the address_space which holds the pages to invalidate
  234. * @start: the offset 'from' which to invalidate
  235. * @end: the offset 'to' which to invalidate (inclusive)
  236. *
  237. * This function only removes the unlocked pages, if you want to
  238. * remove all the pages of one inode, you must call truncate_inode_pages.
  239. *
  240. * invalidate_mapping_pages() will not block on IO activity. It will not
  241. * invalidate pages which are dirty, locked, under writeback or mapped into
  242. * pagetables.
  243. */
  244. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  245. pgoff_t start, pgoff_t end)
  246. {
  247. struct pagevec pvec;
  248. pgoff_t next = start;
  249. unsigned long ret = 0;
  250. int i;
  251. pagevec_init(&pvec, 0);
  252. while (next <= end &&
  253. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  254. for (i = 0; i < pagevec_count(&pvec); i++) {
  255. struct page *page = pvec.pages[i];
  256. pgoff_t index;
  257. int lock_failed;
  258. lock_failed = TestSetPageLocked(page);
  259. /*
  260. * We really shouldn't be looking at the ->index of an
  261. * unlocked page. But we're not allowed to lock these
  262. * pages. So we rely upon nobody altering the ->index
  263. * of this (pinned-by-us) page.
  264. */
  265. index = page->index;
  266. if (index > next)
  267. next = index;
  268. next++;
  269. if (lock_failed)
  270. continue;
  271. if (PageDirty(page) || PageWriteback(page))
  272. goto unlock;
  273. if (page_mapped(page))
  274. goto unlock;
  275. ret += invalidate_complete_page(mapping, page);
  276. unlock:
  277. unlock_page(page);
  278. if (next > end)
  279. break;
  280. }
  281. pagevec_release(&pvec);
  282. }
  283. return ret;
  284. }
  285. EXPORT_SYMBOL(invalidate_mapping_pages);
  286. /*
  287. * This is like invalidate_complete_page(), except it ignores the page's
  288. * refcount. We do this because invalidate_inode_pages2() needs stronger
  289. * invalidation guarantees, and cannot afford to leave pages behind because
  290. * shrink_list() has a temp ref on them, or because they're transiently sitting
  291. * in the lru_cache_add() pagevecs.
  292. */
  293. static int
  294. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  295. {
  296. if (page->mapping != mapping)
  297. return 0;
  298. if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  299. return 0;
  300. write_lock_irq(&mapping->tree_lock);
  301. if (PageDirty(page))
  302. goto failed;
  303. BUG_ON(PagePrivate(page));
  304. __remove_from_page_cache(page);
  305. write_unlock_irq(&mapping->tree_lock);
  306. ClearPageUptodate(page);
  307. page_cache_release(page); /* pagecache ref */
  308. return 1;
  309. failed:
  310. write_unlock_irq(&mapping->tree_lock);
  311. return 0;
  312. }
  313. static int do_launder_page(struct address_space *mapping, struct page *page)
  314. {
  315. if (!PageDirty(page))
  316. return 0;
  317. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  318. return 0;
  319. return mapping->a_ops->launder_page(page);
  320. }
  321. /**
  322. * invalidate_inode_pages2_range - remove range of pages from an address_space
  323. * @mapping: the address_space
  324. * @start: the page offset 'from' which to invalidate
  325. * @end: the page offset 'to' which to invalidate (inclusive)
  326. *
  327. * Any pages which are found to be mapped into pagetables are unmapped prior to
  328. * invalidation.
  329. *
  330. * Returns -EIO if any pages could not be invalidated.
  331. */
  332. int invalidate_inode_pages2_range(struct address_space *mapping,
  333. pgoff_t start, pgoff_t end)
  334. {
  335. struct pagevec pvec;
  336. pgoff_t next;
  337. int i;
  338. int ret = 0;
  339. int did_range_unmap = 0;
  340. int wrapped = 0;
  341. pagevec_init(&pvec, 0);
  342. next = start;
  343. while (next <= end && !wrapped &&
  344. pagevec_lookup(&pvec, mapping, next,
  345. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  346. for (i = 0; i < pagevec_count(&pvec); i++) {
  347. struct page *page = pvec.pages[i];
  348. pgoff_t page_index;
  349. lock_page(page);
  350. if (page->mapping != mapping) {
  351. unlock_page(page);
  352. continue;
  353. }
  354. page_index = page->index;
  355. next = page_index + 1;
  356. if (next == 0)
  357. wrapped = 1;
  358. if (page_index > end) {
  359. unlock_page(page);
  360. break;
  361. }
  362. wait_on_page_writeback(page);
  363. while (page_mapped(page)) {
  364. if (!did_range_unmap) {
  365. /*
  366. * Zap the rest of the file in one hit.
  367. */
  368. unmap_mapping_range(mapping,
  369. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  370. (loff_t)(end - page_index + 1)
  371. << PAGE_CACHE_SHIFT,
  372. 0);
  373. did_range_unmap = 1;
  374. } else {
  375. /*
  376. * Just zap this page
  377. */
  378. unmap_mapping_range(mapping,
  379. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  380. PAGE_CACHE_SIZE, 0);
  381. }
  382. }
  383. ret = do_launder_page(mapping, page);
  384. if (ret == 0 && !invalidate_complete_page2(mapping, page))
  385. ret = -EIO;
  386. unlock_page(page);
  387. }
  388. pagevec_release(&pvec);
  389. cond_resched();
  390. }
  391. return ret;
  392. }
  393. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  394. /**
  395. * invalidate_inode_pages2 - remove all pages from an address_space
  396. * @mapping: the address_space
  397. *
  398. * Any pages which are found to be mapped into pagetables are unmapped prior to
  399. * invalidation.
  400. *
  401. * Returns -EIO if any pages could not be invalidated.
  402. */
  403. int invalidate_inode_pages2(struct address_space *mapping)
  404. {
  405. return invalidate_inode_pages2_range(mapping, 0, -1);
  406. }
  407. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);