truncate.c 8.7 KB

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