truncate.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/module.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/pagevec.h>
  14. #include <linux/buffer_head.h> /* grr. try_to_release_page,
  15. block_invalidatepage */
  16. static int do_invalidatepage(struct page *page, unsigned long offset)
  17. {
  18. int (*invalidatepage)(struct page *, unsigned long);
  19. invalidatepage = page->mapping->a_ops->invalidatepage;
  20. if (invalidatepage == NULL)
  21. invalidatepage = block_invalidatepage;
  22. return (*invalidatepage)(page, offset);
  23. }
  24. static inline void truncate_partial_page(struct page *page, unsigned partial)
  25. {
  26. memclear_highpage_flush(page, partial, PAGE_CACHE_SIZE-partial);
  27. if (PagePrivate(page))
  28. do_invalidatepage(page, partial);
  29. }
  30. /*
  31. * If truncate cannot remove the fs-private metadata from the page, the page
  32. * becomes anonymous. It will be left on the LRU and may even be mapped into
  33. * user pagetables if we're racing with filemap_nopage().
  34. *
  35. * We need to bale out if page->mapping is no longer equal to the original
  36. * mapping. This happens a) when the VM reclaimed the page while we waited on
  37. * its lock, b) when a concurrent invalidate_inode_pages got there first and
  38. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  39. */
  40. static void
  41. truncate_complete_page(struct address_space *mapping, struct page *page)
  42. {
  43. if (page->mapping != mapping)
  44. return;
  45. if (PagePrivate(page))
  46. do_invalidatepage(page, 0);
  47. clear_page_dirty(page);
  48. ClearPageUptodate(page);
  49. ClearPageMappedToDisk(page);
  50. remove_from_page_cache(page);
  51. page_cache_release(page); /* pagecache ref */
  52. }
  53. /*
  54. * This is for invalidate_inode_pages(). That function can be called at
  55. * any time, and is not supposed to throw away dirty pages. But pages can
  56. * be marked dirty at any time too. So we re-check the dirtiness inside
  57. * ->tree_lock. That provides exclusion against the __set_page_dirty
  58. * functions.
  59. *
  60. * Returns non-zero if the page was successfully invalidated.
  61. */
  62. static int
  63. invalidate_complete_page(struct address_space *mapping, struct page *page)
  64. {
  65. if (page->mapping != mapping)
  66. return 0;
  67. if (PagePrivate(page) && !try_to_release_page(page, 0))
  68. return 0;
  69. write_lock_irq(&mapping->tree_lock);
  70. if (PageDirty(page)) {
  71. write_unlock_irq(&mapping->tree_lock);
  72. return 0;
  73. }
  74. BUG_ON(PagePrivate(page));
  75. __remove_from_page_cache(page);
  76. write_unlock_irq(&mapping->tree_lock);
  77. ClearPageUptodate(page);
  78. page_cache_release(page); /* pagecache ref */
  79. return 1;
  80. }
  81. /**
  82. * truncate_inode_pages - truncate *all* the pages from an offset
  83. * @mapping: mapping to truncate
  84. * @lstart: offset from which to truncate
  85. *
  86. * Truncate the page cache at a set offset, removing the pages that are beyond
  87. * that offset (and zeroing out partial pages).
  88. *
  89. * Truncate takes two passes - the first pass is nonblocking. It will not
  90. * block on page locks and it will not block on writeback. The second pass
  91. * will wait. This is to prevent as much IO as possible in the affected region.
  92. * The first pass will remove most pages, so the search cost of the second pass
  93. * is low.
  94. *
  95. * When looking at page->index outside the page lock we need to be careful to
  96. * copy it into a local to avoid races (it could change at any time).
  97. *
  98. * We pass down the cache-hot hint to the page freeing code. Even if the
  99. * mapping is large, it is probably the case that the final pages are the most
  100. * recently touched, and freeing happens in ascending file offset order.
  101. *
  102. * Called under (and serialised by) inode->i_sem.
  103. */
  104. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  105. {
  106. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  107. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  108. struct pagevec pvec;
  109. pgoff_t next;
  110. int i;
  111. if (mapping->nrpages == 0)
  112. return;
  113. pagevec_init(&pvec, 0);
  114. next = start;
  115. while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  116. for (i = 0; i < pagevec_count(&pvec); i++) {
  117. struct page *page = pvec.pages[i];
  118. pgoff_t page_index = page->index;
  119. if (page_index > next)
  120. next = page_index;
  121. next++;
  122. if (TestSetPageLocked(page))
  123. continue;
  124. if (PageWriteback(page)) {
  125. unlock_page(page);
  126. continue;
  127. }
  128. truncate_complete_page(mapping, page);
  129. unlock_page(page);
  130. }
  131. pagevec_release(&pvec);
  132. cond_resched();
  133. }
  134. if (partial) {
  135. struct page *page = find_lock_page(mapping, start - 1);
  136. if (page) {
  137. wait_on_page_writeback(page);
  138. truncate_partial_page(page, partial);
  139. unlock_page(page);
  140. page_cache_release(page);
  141. }
  142. }
  143. next = start;
  144. for ( ; ; ) {
  145. cond_resched();
  146. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  147. if (next == start)
  148. break;
  149. next = start;
  150. continue;
  151. }
  152. for (i = 0; i < pagevec_count(&pvec); i++) {
  153. struct page *page = pvec.pages[i];
  154. lock_page(page);
  155. wait_on_page_writeback(page);
  156. if (page->index > next)
  157. next = page->index;
  158. next++;
  159. truncate_complete_page(mapping, page);
  160. unlock_page(page);
  161. }
  162. pagevec_release(&pvec);
  163. }
  164. }
  165. EXPORT_SYMBOL(truncate_inode_pages);
  166. /**
  167. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  168. * @mapping: the address_space which holds the pages to invalidate
  169. * @start: the offset 'from' which to invalidate
  170. * @end: the offset 'to' which to invalidate (inclusive)
  171. *
  172. * This function only removes the unlocked pages, if you want to
  173. * remove all the pages of one inode, you must call truncate_inode_pages.
  174. *
  175. * invalidate_mapping_pages() will not block on IO activity. It will not
  176. * invalidate pages which are dirty, locked, under writeback or mapped into
  177. * pagetables.
  178. */
  179. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  180. pgoff_t start, pgoff_t end)
  181. {
  182. struct pagevec pvec;
  183. pgoff_t next = start;
  184. unsigned long ret = 0;
  185. int i;
  186. pagevec_init(&pvec, 0);
  187. while (next <= end &&
  188. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  189. for (i = 0; i < pagevec_count(&pvec); i++) {
  190. struct page *page = pvec.pages[i];
  191. if (TestSetPageLocked(page)) {
  192. next++;
  193. continue;
  194. }
  195. if (page->index > next)
  196. next = page->index;
  197. next++;
  198. if (PageDirty(page) || PageWriteback(page))
  199. goto unlock;
  200. if (page_mapped(page))
  201. goto unlock;
  202. ret += invalidate_complete_page(mapping, page);
  203. unlock:
  204. unlock_page(page);
  205. if (next > end)
  206. break;
  207. }
  208. pagevec_release(&pvec);
  209. cond_resched();
  210. }
  211. return ret;
  212. }
  213. unsigned long invalidate_inode_pages(struct address_space *mapping)
  214. {
  215. return invalidate_mapping_pages(mapping, 0, ~0UL);
  216. }
  217. EXPORT_SYMBOL(invalidate_inode_pages);
  218. /**
  219. * invalidate_inode_pages2_range - remove range of pages from an address_space
  220. * @mapping: the address_space
  221. * @start: the page offset 'from' which to invalidate
  222. * @end: the page offset 'to' which to invalidate (inclusive)
  223. *
  224. * Any pages which are found to be mapped into pagetables are unmapped prior to
  225. * invalidation.
  226. *
  227. * Returns -EIO if any pages could not be invalidated.
  228. */
  229. int invalidate_inode_pages2_range(struct address_space *mapping,
  230. pgoff_t start, pgoff_t end)
  231. {
  232. struct pagevec pvec;
  233. pgoff_t next;
  234. int i;
  235. int ret = 0;
  236. int did_range_unmap = 0;
  237. int wrapped = 0;
  238. pagevec_init(&pvec, 0);
  239. next = start;
  240. while (next <= end && !ret && !wrapped &&
  241. pagevec_lookup(&pvec, mapping, next,
  242. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  243. for (i = 0; !ret && i < pagevec_count(&pvec); i++) {
  244. struct page *page = pvec.pages[i];
  245. pgoff_t page_index;
  246. int was_dirty;
  247. lock_page(page);
  248. if (page->mapping != mapping) {
  249. unlock_page(page);
  250. continue;
  251. }
  252. page_index = page->index;
  253. next = page_index + 1;
  254. if (next == 0)
  255. wrapped = 1;
  256. if (page_index > end) {
  257. unlock_page(page);
  258. break;
  259. }
  260. wait_on_page_writeback(page);
  261. while (page_mapped(page)) {
  262. if (!did_range_unmap) {
  263. /*
  264. * Zap the rest of the file in one hit.
  265. */
  266. unmap_mapping_range(mapping,
  267. page_index << PAGE_CACHE_SHIFT,
  268. (end - page_index + 1)
  269. << PAGE_CACHE_SHIFT,
  270. 0);
  271. did_range_unmap = 1;
  272. } else {
  273. /*
  274. * Just zap this page
  275. */
  276. unmap_mapping_range(mapping,
  277. page_index << PAGE_CACHE_SHIFT,
  278. PAGE_CACHE_SIZE, 0);
  279. }
  280. }
  281. was_dirty = test_clear_page_dirty(page);
  282. if (!invalidate_complete_page(mapping, page)) {
  283. if (was_dirty)
  284. set_page_dirty(page);
  285. ret = -EIO;
  286. }
  287. unlock_page(page);
  288. }
  289. pagevec_release(&pvec);
  290. cond_resched();
  291. }
  292. return ret;
  293. }
  294. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  295. /**
  296. * invalidate_inode_pages2 - remove all pages from an address_space
  297. * @mapping: the address_space
  298. *
  299. * Any pages which are found to be mapped into pagetables are unmapped prior to
  300. * invalidation.
  301. *
  302. * Returns -EIO if any pages could not be invalidated.
  303. */
  304. int invalidate_inode_pages2(struct address_space *mapping)
  305. {
  306. return invalidate_inode_pages2_range(mapping, 0, -1);
  307. }
  308. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);