truncate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 or 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_segment(page, partial, PAGE_CACHE_SIZE);
  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 orphaned. 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. if (PagePrivate(page))
  95. do_invalidatepage(page, 0);
  96. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  97. remove_from_page_cache(page);
  98. ClearPageMappedToDisk(page);
  99. page_cache_release(page); /* pagecache ref */
  100. }
  101. /*
  102. * This is for invalidate_mapping_pages(). That function can be called at
  103. * any time, and is not supposed to throw away dirty pages. But pages can
  104. * be marked dirty at any time too, so use remove_mapping which safely
  105. * discards clean, unused pages.
  106. *
  107. * Returns non-zero if the page was successfully invalidated.
  108. */
  109. static int
  110. invalidate_complete_page(struct address_space *mapping, struct page *page)
  111. {
  112. int ret;
  113. if (page->mapping != mapping)
  114. return 0;
  115. if (PagePrivate(page) && !try_to_release_page(page, 0))
  116. return 0;
  117. ret = remove_mapping(mapping, page);
  118. return ret;
  119. }
  120. /**
  121. * truncate_inode_pages - truncate range of pages specified by start & end byte offsets
  122. * @mapping: mapping to truncate
  123. * @lstart: offset from which to truncate
  124. * @lend: offset to which to truncate
  125. *
  126. * Truncate the page cache, removing the pages that are between
  127. * specified offsets (and zeroing out partial page
  128. * (if lstart is not page aligned)).
  129. *
  130. * Truncate takes two passes - the first pass is nonblocking. It will not
  131. * block on page locks and it will not block on writeback. The second pass
  132. * will wait. This is to prevent as much IO as possible in the affected region.
  133. * The first pass will remove most pages, so the search cost of the second pass
  134. * is low.
  135. *
  136. * When looking at page->index outside the page lock we need to be careful to
  137. * copy it into a local to avoid races (it could change at any time).
  138. *
  139. * We pass down the cache-hot hint to the page freeing code. Even if the
  140. * mapping is large, it is probably the case that the final pages are the most
  141. * recently touched, and freeing happens in ascending file offset order.
  142. */
  143. void truncate_inode_pages_range(struct address_space *mapping,
  144. loff_t lstart, loff_t lend)
  145. {
  146. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  147. pgoff_t end;
  148. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  149. struct pagevec pvec;
  150. pgoff_t next;
  151. int i;
  152. if (mapping->nrpages == 0)
  153. return;
  154. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  155. end = (lend >> PAGE_CACHE_SHIFT);
  156. pagevec_init(&pvec, 0);
  157. next = start;
  158. while (next <= end &&
  159. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  160. for (i = 0; i < pagevec_count(&pvec); i++) {
  161. struct page *page = pvec.pages[i];
  162. pgoff_t page_index = page->index;
  163. if (page_index > end) {
  164. next = page_index;
  165. break;
  166. }
  167. if (page_index > next)
  168. next = page_index;
  169. next++;
  170. if (!trylock_page(page))
  171. continue;
  172. if (PageWriteback(page)) {
  173. unlock_page(page);
  174. continue;
  175. }
  176. if (page_mapped(page)) {
  177. unmap_mapping_range(mapping,
  178. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  179. PAGE_CACHE_SIZE, 0);
  180. }
  181. truncate_complete_page(mapping, page);
  182. unlock_page(page);
  183. }
  184. pagevec_release(&pvec);
  185. cond_resched();
  186. }
  187. if (partial) {
  188. struct page *page = find_lock_page(mapping, start - 1);
  189. if (page) {
  190. wait_on_page_writeback(page);
  191. truncate_partial_page(page, partial);
  192. unlock_page(page);
  193. page_cache_release(page);
  194. }
  195. }
  196. next = start;
  197. for ( ; ; ) {
  198. cond_resched();
  199. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  200. if (next == start)
  201. break;
  202. next = start;
  203. continue;
  204. }
  205. if (pvec.pages[0]->index > end) {
  206. pagevec_release(&pvec);
  207. break;
  208. }
  209. for (i = 0; i < pagevec_count(&pvec); i++) {
  210. struct page *page = pvec.pages[i];
  211. if (page->index > end)
  212. break;
  213. lock_page(page);
  214. wait_on_page_writeback(page);
  215. if (page_mapped(page)) {
  216. unmap_mapping_range(mapping,
  217. (loff_t)page->index<<PAGE_CACHE_SHIFT,
  218. PAGE_CACHE_SIZE, 0);
  219. }
  220. if (page->index > next)
  221. next = page->index;
  222. next++;
  223. truncate_complete_page(mapping, page);
  224. unlock_page(page);
  225. }
  226. pagevec_release(&pvec);
  227. }
  228. }
  229. EXPORT_SYMBOL(truncate_inode_pages_range);
  230. /**
  231. * truncate_inode_pages - truncate *all* the pages from an offset
  232. * @mapping: mapping to truncate
  233. * @lstart: offset from which to truncate
  234. *
  235. * Called under (and serialised by) inode->i_mutex.
  236. */
  237. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  238. {
  239. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  240. }
  241. EXPORT_SYMBOL(truncate_inode_pages);
  242. unsigned long __invalidate_mapping_pages(struct address_space *mapping,
  243. pgoff_t start, pgoff_t end, bool be_atomic)
  244. {
  245. struct pagevec pvec;
  246. pgoff_t next = start;
  247. unsigned long ret = 0;
  248. int i;
  249. pagevec_init(&pvec, 0);
  250. while (next <= end &&
  251. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  252. for (i = 0; i < pagevec_count(&pvec); i++) {
  253. struct page *page = pvec.pages[i];
  254. pgoff_t index;
  255. int lock_failed;
  256. lock_failed = !trylock_page(page);
  257. /*
  258. * We really shouldn't be looking at the ->index of an
  259. * unlocked page. But we're not allowed to lock these
  260. * pages. So we rely upon nobody altering the ->index
  261. * of this (pinned-by-us) page.
  262. */
  263. index = page->index;
  264. if (index > next)
  265. next = index;
  266. next++;
  267. if (lock_failed)
  268. continue;
  269. if (PageDirty(page) || PageWriteback(page))
  270. goto unlock;
  271. if (page_mapped(page))
  272. goto unlock;
  273. ret += invalidate_complete_page(mapping, page);
  274. unlock:
  275. unlock_page(page);
  276. if (next > end)
  277. break;
  278. }
  279. pagevec_release(&pvec);
  280. if (likely(!be_atomic))
  281. cond_resched();
  282. }
  283. return ret;
  284. }
  285. /**
  286. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  287. * @mapping: the address_space which holds the pages to invalidate
  288. * @start: the offset 'from' which to invalidate
  289. * @end: the offset 'to' which to invalidate (inclusive)
  290. *
  291. * This function only removes the unlocked pages, if you want to
  292. * remove all the pages of one inode, you must call truncate_inode_pages.
  293. *
  294. * invalidate_mapping_pages() will not block on IO activity. It will not
  295. * invalidate pages which are dirty, locked, under writeback or mapped into
  296. * pagetables.
  297. */
  298. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  299. pgoff_t start, pgoff_t end)
  300. {
  301. return __invalidate_mapping_pages(mapping, start, end, false);
  302. }
  303. EXPORT_SYMBOL(invalidate_mapping_pages);
  304. /*
  305. * This is like invalidate_complete_page(), except it ignores the page's
  306. * refcount. We do this because invalidate_inode_pages2() needs stronger
  307. * invalidation guarantees, and cannot afford to leave pages behind because
  308. * shrink_page_list() has a temp ref on them, or because they're transiently
  309. * sitting in the lru_cache_add() pagevecs.
  310. */
  311. static int
  312. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  313. {
  314. if (page->mapping != mapping)
  315. return 0;
  316. if (PagePrivate(page) && !try_to_release_page(page, GFP_KERNEL))
  317. return 0;
  318. spin_lock_irq(&mapping->tree_lock);
  319. if (PageDirty(page))
  320. goto failed;
  321. BUG_ON(PagePrivate(page));
  322. __remove_from_page_cache(page);
  323. spin_unlock_irq(&mapping->tree_lock);
  324. page_cache_release(page); /* pagecache ref */
  325. return 1;
  326. failed:
  327. spin_unlock_irq(&mapping->tree_lock);
  328. return 0;
  329. }
  330. static int do_launder_page(struct address_space *mapping, struct page *page)
  331. {
  332. if (!PageDirty(page))
  333. return 0;
  334. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  335. return 0;
  336. return mapping->a_ops->launder_page(page);
  337. }
  338. /**
  339. * invalidate_inode_pages2_range - remove range of pages from an address_space
  340. * @mapping: the address_space
  341. * @start: the page offset 'from' which to invalidate
  342. * @end: the page offset 'to' which to invalidate (inclusive)
  343. *
  344. * Any pages which are found to be mapped into pagetables are unmapped prior to
  345. * invalidation.
  346. *
  347. * Returns -EBUSY if any pages could not be invalidated.
  348. */
  349. int invalidate_inode_pages2_range(struct address_space *mapping,
  350. pgoff_t start, pgoff_t end)
  351. {
  352. struct pagevec pvec;
  353. pgoff_t next;
  354. int i;
  355. int ret = 0;
  356. int ret2 = 0;
  357. int did_range_unmap = 0;
  358. int wrapped = 0;
  359. pagevec_init(&pvec, 0);
  360. next = start;
  361. while (next <= end && !wrapped &&
  362. pagevec_lookup(&pvec, mapping, next,
  363. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  364. for (i = 0; i < pagevec_count(&pvec); i++) {
  365. struct page *page = pvec.pages[i];
  366. pgoff_t page_index;
  367. lock_page(page);
  368. if (page->mapping != mapping) {
  369. unlock_page(page);
  370. continue;
  371. }
  372. page_index = page->index;
  373. next = page_index + 1;
  374. if (next == 0)
  375. wrapped = 1;
  376. if (page_index > end) {
  377. unlock_page(page);
  378. break;
  379. }
  380. wait_on_page_writeback(page);
  381. if (page_mapped(page)) {
  382. if (!did_range_unmap) {
  383. /*
  384. * Zap the rest of the file in one hit.
  385. */
  386. unmap_mapping_range(mapping,
  387. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  388. (loff_t)(end - page_index + 1)
  389. << PAGE_CACHE_SHIFT,
  390. 0);
  391. did_range_unmap = 1;
  392. } else {
  393. /*
  394. * Just zap this page
  395. */
  396. unmap_mapping_range(mapping,
  397. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  398. PAGE_CACHE_SIZE, 0);
  399. }
  400. }
  401. BUG_ON(page_mapped(page));
  402. ret2 = do_launder_page(mapping, page);
  403. if (ret2 == 0) {
  404. if (!invalidate_complete_page2(mapping, page))
  405. ret2 = -EBUSY;
  406. }
  407. if (ret2 < 0)
  408. ret = ret2;
  409. unlock_page(page);
  410. }
  411. pagevec_release(&pvec);
  412. cond_resched();
  413. }
  414. return ret;
  415. }
  416. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  417. /**
  418. * invalidate_inode_pages2 - remove all pages from an address_space
  419. * @mapping: the address_space
  420. *
  421. * Any pages which are found to be mapped into pagetables are unmapped prior to
  422. * invalidation.
  423. *
  424. * Returns -EIO if any pages could not be invalidated.
  425. */
  426. int invalidate_inode_pages2(struct address_space *mapping)
  427. {
  428. return invalidate_inode_pages2_range(mapping, 0, -1);
  429. }
  430. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);