truncate.c 12 KB

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