truncate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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/backing-dev.h>
  11. #include <linux/mm.h>
  12. #include <linux/swap.h>
  13. #include <linux/module.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/highmem.h>
  16. #include <linux/pagevec.h>
  17. #include <linux/task_io_accounting_ops.h>
  18. #include <linux/buffer_head.h> /* grr. try_to_release_page,
  19. do_invalidatepage */
  20. /**
  21. * do_invalidatepage - invalidate part of all of a page
  22. * @page: the page which is affected
  23. * @offset: the index of the truncation point
  24. *
  25. * do_invalidatepage() is called when all or part of the page has become
  26. * invalidated by a truncate operation.
  27. *
  28. * do_invalidatepage() does not have to release all buffers, but it must
  29. * ensure that no dirty buffer is left outside @offset and that no I/O
  30. * is underway against any of the blocks which are outside the truncation
  31. * point. Because the caller is about to free (and possibly reuse) those
  32. * blocks on-disk.
  33. */
  34. void do_invalidatepage(struct page *page, unsigned long offset)
  35. {
  36. void (*invalidatepage)(struct page *, unsigned long);
  37. invalidatepage = page->mapping->a_ops->invalidatepage;
  38. #ifdef CONFIG_BLOCK
  39. if (!invalidatepage)
  40. invalidatepage = block_invalidatepage;
  41. #endif
  42. if (invalidatepage)
  43. (*invalidatepage)(page, offset);
  44. }
  45. static inline void truncate_partial_page(struct page *page, unsigned partial)
  46. {
  47. zero_user_page(page, partial, PAGE_CACHE_SIZE - partial, KM_USER0);
  48. if (PagePrivate(page))
  49. do_invalidatepage(page, partial);
  50. }
  51. /*
  52. * This cancels just the dirty bit on the kernel page itself, it
  53. * does NOT actually remove dirty bits on any mmap's that may be
  54. * around. It also leaves the page tagged dirty, so any sync
  55. * activity will still find it on the dirty lists, and in particular,
  56. * clear_page_dirty_for_io() will still look at the dirty bits in
  57. * the VM.
  58. *
  59. * Doing this should *normally* only ever be done when a page
  60. * is truncated, and is not actually mapped anywhere at all. However,
  61. * fs/buffer.c does this when it notices that somebody has cleaned
  62. * out all the buffers on a page without actually doing it through
  63. * the VM. Can you say "ext3 is horribly ugly"? Tought you could.
  64. */
  65. void cancel_dirty_page(struct page *page, unsigned int account_size)
  66. {
  67. if (TestClearPageDirty(page)) {
  68. struct address_space *mapping = page->mapping;
  69. if (mapping && mapping_cap_account_dirty(mapping)) {
  70. dec_zone_page_state(page, NR_FILE_DIRTY);
  71. dec_bdi_stat(mapping->backing_dev_info,
  72. BDI_RECLAIMABLE);
  73. if (account_size)
  74. task_io_account_cancelled_write(account_size);
  75. }
  76. }
  77. }
  78. EXPORT_SYMBOL(cancel_dirty_page);
  79. /*
  80. * If truncate cannot remove the fs-private metadata from the page, the page
  81. * becomes anonymous. It will be left on the LRU and may even be mapped into
  82. * user pagetables if we're racing with filemap_fault().
  83. *
  84. * We need to bale out if page->mapping is no longer equal to the original
  85. * mapping. This happens a) when the VM reclaimed the page while we waited on
  86. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  87. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  88. */
  89. static void
  90. truncate_complete_page(struct address_space *mapping, struct page *page)
  91. {
  92. if (page->mapping != mapping)
  93. return;
  94. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  95. if (PagePrivate(page))
  96. do_invalidatepage(page, 0);
  97. remove_from_page_cache(page);
  98. ClearPageUptodate(page);
  99. ClearPageMappedToDisk(page);
  100. page_cache_release(page); /* pagecache ref */
  101. }
  102. /*
  103. * This is for invalidate_mapping_pages(). That function can be called at
  104. * any time, and is not supposed to throw away dirty pages. But pages can
  105. * be marked dirty at any time too, so use remove_mapping which safely
  106. * discards clean, unused pages.
  107. *
  108. * Returns non-zero if the page was successfully invalidated.
  109. */
  110. static int
  111. invalidate_complete_page(struct address_space *mapping, struct page *page)
  112. {
  113. int ret;
  114. if (page->mapping != mapping)
  115. return 0;
  116. if (PagePrivate(page) && !try_to_release_page(page, 0))
  117. return 0;
  118. ret = remove_mapping(mapping, page);
  119. return ret;
  120. }
  121. /**
  122. * truncate_inode_pages - truncate range of pages specified by start and
  123. * end byte offsets
  124. * @mapping: mapping to truncate
  125. * @lstart: offset from which to truncate
  126. * @lend: offset to which to truncate
  127. *
  128. * Truncate the page cache, removing the pages that are between
  129. * specified offsets (and zeroing out partial page
  130. * (if lstart is not page aligned)).
  131. *
  132. * Truncate takes two passes - the first pass is nonblocking. It will not
  133. * block on page locks and it will not block on writeback. The second pass
  134. * will wait. This is to prevent as much IO as possible in the affected region.
  135. * The first pass will remove most pages, so the search cost of the second pass
  136. * is low.
  137. *
  138. * When looking at page->index outside the page lock we need to be careful to
  139. * copy it into a local to avoid races (it could change at any time).
  140. *
  141. * We pass down the cache-hot hint to the page freeing code. Even if the
  142. * mapping is large, it is probably the case that the final pages are the most
  143. * recently touched, and freeing happens in ascending file offset order.
  144. */
  145. void truncate_inode_pages_range(struct address_space *mapping,
  146. loff_t lstart, loff_t lend)
  147. {
  148. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  149. pgoff_t end;
  150. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  151. struct pagevec pvec;
  152. pgoff_t next;
  153. int i;
  154. if (mapping->nrpages == 0)
  155. return;
  156. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  157. end = (lend >> PAGE_CACHE_SHIFT);
  158. pagevec_init(&pvec, 0);
  159. next = start;
  160. while (next <= end &&
  161. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  162. for (i = 0; i < pagevec_count(&pvec); i++) {
  163. struct page *page = pvec.pages[i];
  164. pgoff_t page_index = page->index;
  165. if (page_index > end) {
  166. next = page_index;
  167. break;
  168. }
  169. if (page_index > next)
  170. next = page_index;
  171. next++;
  172. if (TestSetPageLocked(page))
  173. continue;
  174. if (PageWriteback(page)) {
  175. unlock_page(page);
  176. continue;
  177. }
  178. if (page_mapped(page)) {
  179. unmap_mapping_range(mapping,
  180. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  181. PAGE_CACHE_SIZE, 0);
  182. }
  183. truncate_complete_page(mapping, page);
  184. unlock_page(page);
  185. }
  186. pagevec_release(&pvec);
  187. cond_resched();
  188. }
  189. if (partial) {
  190. struct page *page = find_lock_page(mapping, start - 1);
  191. if (page) {
  192. wait_on_page_writeback(page);
  193. truncate_partial_page(page, partial);
  194. unlock_page(page);
  195. page_cache_release(page);
  196. }
  197. }
  198. next = start;
  199. for ( ; ; ) {
  200. cond_resched();
  201. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  202. if (next == start)
  203. break;
  204. next = start;
  205. continue;
  206. }
  207. if (pvec.pages[0]->index > end) {
  208. pagevec_release(&pvec);
  209. break;
  210. }
  211. for (i = 0; i < pagevec_count(&pvec); i++) {
  212. struct page *page = pvec.pages[i];
  213. if (page->index > end)
  214. break;
  215. lock_page(page);
  216. wait_on_page_writeback(page);
  217. if (page_mapped(page)) {
  218. unmap_mapping_range(mapping,
  219. (loff_t)page->index<<PAGE_CACHE_SHIFT,
  220. PAGE_CACHE_SIZE, 0);
  221. }
  222. if (page->index > next)
  223. next = page->index;
  224. next++;
  225. truncate_complete_page(mapping, page);
  226. unlock_page(page);
  227. }
  228. pagevec_release(&pvec);
  229. }
  230. }
  231. EXPORT_SYMBOL(truncate_inode_pages_range);
  232. /**
  233. * truncate_inode_pages - truncate *all* the pages from an offset
  234. * @mapping: mapping to truncate
  235. * @lstart: offset from which to truncate
  236. *
  237. * Called under (and serialised by) inode->i_mutex.
  238. */
  239. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  240. {
  241. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  242. }
  243. EXPORT_SYMBOL(truncate_inode_pages);
  244. unsigned long __invalidate_mapping_pages(struct address_space *mapping,
  245. pgoff_t start, pgoff_t end, bool be_atomic)
  246. {
  247. struct pagevec pvec;
  248. pgoff_t next = start;
  249. unsigned long ret = 0;
  250. int i;
  251. pagevec_init(&pvec, 0);
  252. while (next <= end &&
  253. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  254. for (i = 0; i < pagevec_count(&pvec); i++) {
  255. struct page *page = pvec.pages[i];
  256. pgoff_t index;
  257. int lock_failed;
  258. lock_failed = TestSetPageLocked(page);
  259. /*
  260. * We really shouldn't be looking at the ->index of an
  261. * unlocked page. But we're not allowed to lock these
  262. * pages. So we rely upon nobody altering the ->index
  263. * of this (pinned-by-us) page.
  264. */
  265. index = page->index;
  266. if (index > next)
  267. next = index;
  268. next++;
  269. if (lock_failed)
  270. continue;
  271. if (PageDirty(page) || PageWriteback(page))
  272. goto unlock;
  273. if (page_mapped(page))
  274. goto unlock;
  275. ret += invalidate_complete_page(mapping, page);
  276. unlock:
  277. unlock_page(page);
  278. if (next > end)
  279. break;
  280. }
  281. pagevec_release(&pvec);
  282. if (likely(!be_atomic))
  283. cond_resched();
  284. }
  285. return ret;
  286. }
  287. /**
  288. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  289. * @mapping: the address_space which holds the pages to invalidate
  290. * @start: the offset 'from' which to invalidate
  291. * @end: the offset 'to' which to invalidate (inclusive)
  292. *
  293. * This function only removes the unlocked pages, if you want to
  294. * remove all the pages of one inode, you must call truncate_inode_pages.
  295. *
  296. * invalidate_mapping_pages() will not block on IO activity. It will not
  297. * invalidate pages which are dirty, locked, under writeback or mapped into
  298. * pagetables.
  299. */
  300. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  301. pgoff_t start, pgoff_t end)
  302. {
  303. return __invalidate_mapping_pages(mapping, start, end, false);
  304. }
  305. EXPORT_SYMBOL(invalidate_mapping_pages);
  306. /*
  307. * This is like invalidate_complete_page(), except it ignores the page's
  308. * refcount. We do this because invalidate_inode_pages2() needs stronger
  309. * invalidation guarantees, and cannot afford to leave pages behind because
  310. * shrink_page_list() has a temp ref on them, or because they're transiently
  311. * sitting in the lru_cache_add() pagevecs.
  312. */
  313. static int
  314. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  315. {
  316. if (page->mapping != mapping)
  317. return 0;
  318. if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  319. return 0;
  320. write_lock_irq(&mapping->tree_lock);
  321. if (PageDirty(page))
  322. goto failed;
  323. BUG_ON(PagePrivate(page));
  324. __remove_from_page_cache(page);
  325. write_unlock_irq(&mapping->tree_lock);
  326. ClearPageUptodate(page);
  327. page_cache_release(page); /* pagecache ref */
  328. return 1;
  329. failed:
  330. write_unlock_irq(&mapping->tree_lock);
  331. return 0;
  332. }
  333. static int do_launder_page(struct address_space *mapping, struct page *page)
  334. {
  335. if (!PageDirty(page))
  336. return 0;
  337. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  338. return 0;
  339. return mapping->a_ops->launder_page(page);
  340. }
  341. /**
  342. * invalidate_inode_pages2_range - remove range of pages from an address_space
  343. * @mapping: the address_space
  344. * @start: the page offset 'from' which to invalidate
  345. * @end: the page offset 'to' which to invalidate (inclusive)
  346. *
  347. * Any pages which are found to be mapped into pagetables are unmapped prior to
  348. * invalidation.
  349. *
  350. * Returns -EIO if any pages could not be invalidated.
  351. */
  352. int invalidate_inode_pages2_range(struct address_space *mapping,
  353. pgoff_t start, pgoff_t end)
  354. {
  355. struct pagevec pvec;
  356. pgoff_t next;
  357. int i;
  358. int ret = 0;
  359. int did_range_unmap = 0;
  360. int wrapped = 0;
  361. pagevec_init(&pvec, 0);
  362. next = start;
  363. while (next <= end && !wrapped &&
  364. pagevec_lookup(&pvec, mapping, next,
  365. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  366. for (i = 0; i < pagevec_count(&pvec); i++) {
  367. struct page *page = pvec.pages[i];
  368. pgoff_t page_index;
  369. lock_page(page);
  370. if (page->mapping != mapping) {
  371. unlock_page(page);
  372. continue;
  373. }
  374. page_index = page->index;
  375. next = page_index + 1;
  376. if (next == 0)
  377. wrapped = 1;
  378. if (page_index > end) {
  379. unlock_page(page);
  380. break;
  381. }
  382. wait_on_page_writeback(page);
  383. if (page_mapped(page)) {
  384. if (!did_range_unmap) {
  385. /*
  386. * Zap the rest of the file in one hit.
  387. */
  388. unmap_mapping_range(mapping,
  389. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  390. (loff_t)(end - page_index + 1)
  391. << PAGE_CACHE_SHIFT,
  392. 0);
  393. did_range_unmap = 1;
  394. } else {
  395. /*
  396. * Just zap this page
  397. */
  398. unmap_mapping_range(mapping,
  399. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  400. PAGE_CACHE_SIZE, 0);
  401. }
  402. }
  403. BUG_ON(page_mapped(page));
  404. ret = do_launder_page(mapping, page);
  405. if (ret == 0 && !invalidate_complete_page2(mapping, page))
  406. ret = -EIO;
  407. unlock_page(page);
  408. }
  409. pagevec_release(&pvec);
  410. cond_resched();
  411. }
  412. return ret;
  413. }
  414. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  415. /**
  416. * invalidate_inode_pages2 - remove all pages from an address_space
  417. * @mapping: the address_space
  418. *
  419. * Any pages which are found to be mapped into pagetables are unmapped prior to
  420. * invalidation.
  421. *
  422. * Returns -EIO if any pages could not be invalidated.
  423. */
  424. int invalidate_inode_pages2(struct address_space *mapping)
  425. {
  426. return invalidate_inode_pages2_range(mapping, 0, -1);
  427. }
  428. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);