truncate.c 9.4 KB

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