truncate.c 12 KB

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