truncate.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 range of pages specified by start and
  75. * end byte offsets
  76. * @mapping: mapping to truncate
  77. * @lstart: offset from which to truncate
  78. * @lend: offset to which to truncate
  79. *
  80. * Truncate the page cache, removing the pages that are between
  81. * specified offsets (and zeroing out partial page
  82. * (if lstart is not page aligned)).
  83. *
  84. * Truncate takes two passes - the first pass is nonblocking. It will not
  85. * block on page locks and it will not block on writeback. The second pass
  86. * will wait. This is to prevent as much IO as possible in the affected region.
  87. * The first pass will remove most pages, so the search cost of the second pass
  88. * is low.
  89. *
  90. * When looking at page->index outside the page lock we need to be careful to
  91. * copy it into a local to avoid races (it could change at any time).
  92. *
  93. * We pass down the cache-hot hint to the page freeing code. Even if the
  94. * mapping is large, it is probably the case that the final pages are the most
  95. * recently touched, and freeing happens in ascending file offset order.
  96. */
  97. void truncate_inode_pages_range(struct address_space *mapping,
  98. loff_t lstart, loff_t lend)
  99. {
  100. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  101. pgoff_t end;
  102. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  103. struct pagevec pvec;
  104. pgoff_t next;
  105. int i;
  106. if (mapping->nrpages == 0)
  107. return;
  108. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  109. end = (lend >> PAGE_CACHE_SHIFT);
  110. pagevec_init(&pvec, 0);
  111. next = start;
  112. while (next <= end &&
  113. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  114. for (i = 0; i < pagevec_count(&pvec); i++) {
  115. struct page *page = pvec.pages[i];
  116. pgoff_t page_index = page->index;
  117. if (page_index > end) {
  118. next = page_index;
  119. break;
  120. }
  121. if (page_index > next)
  122. next = page_index;
  123. next++;
  124. if (TestSetPageLocked(page))
  125. continue;
  126. if (PageWriteback(page)) {
  127. unlock_page(page);
  128. continue;
  129. }
  130. truncate_complete_page(mapping, page);
  131. unlock_page(page);
  132. }
  133. pagevec_release(&pvec);
  134. cond_resched();
  135. }
  136. if (partial) {
  137. struct page *page = find_lock_page(mapping, start - 1);
  138. if (page) {
  139. wait_on_page_writeback(page);
  140. truncate_partial_page(page, partial);
  141. unlock_page(page);
  142. page_cache_release(page);
  143. }
  144. }
  145. next = start;
  146. for ( ; ; ) {
  147. cond_resched();
  148. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  149. if (next == start)
  150. break;
  151. next = start;
  152. continue;
  153. }
  154. if (pvec.pages[0]->index > end) {
  155. pagevec_release(&pvec);
  156. break;
  157. }
  158. for (i = 0; i < pagevec_count(&pvec); i++) {
  159. struct page *page = pvec.pages[i];
  160. if (page->index > end)
  161. break;
  162. lock_page(page);
  163. wait_on_page_writeback(page);
  164. if (page->index > next)
  165. next = page->index;
  166. next++;
  167. truncate_complete_page(mapping, page);
  168. unlock_page(page);
  169. }
  170. pagevec_release(&pvec);
  171. }
  172. }
  173. EXPORT_SYMBOL(truncate_inode_pages_range);
  174. /**
  175. * truncate_inode_pages - truncate *all* the pages from an offset
  176. * @mapping: mapping to truncate
  177. * @lstart: offset from which to truncate
  178. *
  179. * Called under (and serialised by) inode->i_mutex.
  180. */
  181. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  182. {
  183. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  184. }
  185. EXPORT_SYMBOL(truncate_inode_pages);
  186. /**
  187. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  188. * @mapping: the address_space which holds the pages to invalidate
  189. * @start: the offset 'from' which to invalidate
  190. * @end: the offset 'to' which to invalidate (inclusive)
  191. *
  192. * This function only removes the unlocked pages, if you want to
  193. * remove all the pages of one inode, you must call truncate_inode_pages.
  194. *
  195. * invalidate_mapping_pages() will not block on IO activity. It will not
  196. * invalidate pages which are dirty, locked, under writeback or mapped into
  197. * pagetables.
  198. */
  199. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  200. pgoff_t start, pgoff_t end)
  201. {
  202. struct pagevec pvec;
  203. pgoff_t next = start;
  204. unsigned long ret = 0;
  205. int i;
  206. pagevec_init(&pvec, 0);
  207. while (next <= end &&
  208. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  209. for (i = 0; i < pagevec_count(&pvec); i++) {
  210. struct page *page = pvec.pages[i];
  211. if (TestSetPageLocked(page)) {
  212. next++;
  213. continue;
  214. }
  215. if (page->index > next)
  216. next = page->index;
  217. next++;
  218. if (PageDirty(page) || PageWriteback(page))
  219. goto unlock;
  220. if (page_mapped(page))
  221. goto unlock;
  222. ret += invalidate_complete_page(mapping, page);
  223. unlock:
  224. unlock_page(page);
  225. if (next > end)
  226. break;
  227. }
  228. pagevec_release(&pvec);
  229. }
  230. return ret;
  231. }
  232. unsigned long invalidate_inode_pages(struct address_space *mapping)
  233. {
  234. return invalidate_mapping_pages(mapping, 0, ~0UL);
  235. }
  236. EXPORT_SYMBOL(invalidate_inode_pages);
  237. /**
  238. * invalidate_inode_pages2_range - remove range of pages from an address_space
  239. * @mapping: the address_space
  240. * @start: the page offset 'from' which to invalidate
  241. * @end: the page offset 'to' which to invalidate (inclusive)
  242. *
  243. * Any pages which are found to be mapped into pagetables are unmapped prior to
  244. * invalidation.
  245. *
  246. * Returns -EIO if any pages could not be invalidated.
  247. */
  248. int invalidate_inode_pages2_range(struct address_space *mapping,
  249. pgoff_t start, pgoff_t end)
  250. {
  251. struct pagevec pvec;
  252. pgoff_t next;
  253. int i;
  254. int ret = 0;
  255. int did_range_unmap = 0;
  256. int wrapped = 0;
  257. pagevec_init(&pvec, 0);
  258. next = start;
  259. while (next <= end && !ret && !wrapped &&
  260. pagevec_lookup(&pvec, mapping, next,
  261. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  262. for (i = 0; !ret && i < pagevec_count(&pvec); i++) {
  263. struct page *page = pvec.pages[i];
  264. pgoff_t page_index;
  265. int was_dirty;
  266. lock_page(page);
  267. if (page->mapping != mapping) {
  268. unlock_page(page);
  269. continue;
  270. }
  271. page_index = page->index;
  272. next = page_index + 1;
  273. if (next == 0)
  274. wrapped = 1;
  275. if (page_index > end) {
  276. unlock_page(page);
  277. break;
  278. }
  279. wait_on_page_writeback(page);
  280. while (page_mapped(page)) {
  281. if (!did_range_unmap) {
  282. /*
  283. * Zap the rest of the file in one hit.
  284. */
  285. unmap_mapping_range(mapping,
  286. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  287. (loff_t)(end - page_index + 1)
  288. << PAGE_CACHE_SHIFT,
  289. 0);
  290. did_range_unmap = 1;
  291. } else {
  292. /*
  293. * Just zap this page
  294. */
  295. unmap_mapping_range(mapping,
  296. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  297. PAGE_CACHE_SIZE, 0);
  298. }
  299. }
  300. was_dirty = test_clear_page_dirty(page);
  301. if (!invalidate_complete_page(mapping, page)) {
  302. if (was_dirty)
  303. set_page_dirty(page);
  304. ret = -EIO;
  305. }
  306. unlock_page(page);
  307. }
  308. pagevec_release(&pvec);
  309. cond_resched();
  310. }
  311. return ret;
  312. }
  313. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  314. /**
  315. * invalidate_inode_pages2 - remove all pages from an address_space
  316. * @mapping: the address_space
  317. *
  318. * Any pages which are found to be mapped into pagetables are unmapped prior to
  319. * invalidation.
  320. *
  321. * Returns -EIO if any pages could not be invalidated.
  322. */
  323. int invalidate_inode_pages2(struct address_space *mapping)
  324. {
  325. return invalidate_inode_pages2_range(mapping, 0, -1);
  326. }
  327. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);