truncate.c 9.7 KB

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