truncate.c 13 KB

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