truncate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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/gfp.h>
  12. #include <linux/mm.h>
  13. #include <linux/swap.h>
  14. #include <linux/module.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/highmem.h>
  17. #include <linux/pagevec.h>
  18. #include <linux/task_io_accounting_ops.h>
  19. #include <linux/buffer_head.h> /* grr. try_to_release_page,
  20. do_invalidatepage */
  21. #include "internal.h"
  22. /**
  23. * do_invalidatepage - invalidate part or all of a page
  24. * @page: the page which is affected
  25. * @offset: the index of the truncation point
  26. *
  27. * do_invalidatepage() is called when all or part of the page has become
  28. * invalidated by a truncate operation.
  29. *
  30. * do_invalidatepage() does not have to release all buffers, but it must
  31. * ensure that no dirty buffer is left outside @offset and that no I/O
  32. * is underway against any of the blocks which are outside the truncation
  33. * point. Because the caller is about to free (and possibly reuse) those
  34. * blocks on-disk.
  35. */
  36. void do_invalidatepage(struct page *page, unsigned long offset)
  37. {
  38. void (*invalidatepage)(struct page *, unsigned long);
  39. invalidatepage = page->mapping->a_ops->invalidatepage;
  40. #ifdef CONFIG_BLOCK
  41. if (!invalidatepage)
  42. invalidatepage = block_invalidatepage;
  43. #endif
  44. if (invalidatepage)
  45. (*invalidatepage)(page, offset);
  46. }
  47. static inline void truncate_partial_page(struct page *page, unsigned partial)
  48. {
  49. zero_user_segment(page, partial, PAGE_CACHE_SIZE);
  50. if (page_has_private(page))
  51. do_invalidatepage(page, partial);
  52. }
  53. /*
  54. * This cancels just the dirty bit on the kernel page itself, it
  55. * does NOT actually remove dirty bits on any mmap's that may be
  56. * around. It also leaves the page tagged dirty, so any sync
  57. * activity will still find it on the dirty lists, and in particular,
  58. * clear_page_dirty_for_io() will still look at the dirty bits in
  59. * the VM.
  60. *
  61. * Doing this should *normally* only ever be done when a page
  62. * is truncated, and is not actually mapped anywhere at all. However,
  63. * fs/buffer.c does this when it notices that somebody has cleaned
  64. * out all the buffers on a page without actually doing it through
  65. * the VM. Can you say "ext3 is horribly ugly"? Tought you could.
  66. */
  67. void cancel_dirty_page(struct page *page, unsigned int account_size)
  68. {
  69. if (TestClearPageDirty(page)) {
  70. struct address_space *mapping = page->mapping;
  71. if (mapping && mapping_cap_account_dirty(mapping)) {
  72. dec_zone_page_state(page, NR_FILE_DIRTY);
  73. dec_bdi_stat(mapping->backing_dev_info,
  74. BDI_RECLAIMABLE);
  75. if (account_size)
  76. task_io_account_cancelled_write(account_size);
  77. }
  78. }
  79. }
  80. EXPORT_SYMBOL(cancel_dirty_page);
  81. /*
  82. * If truncate cannot remove the fs-private metadata from the page, the page
  83. * becomes orphaned. It will be left on the LRU and may even be mapped into
  84. * user pagetables if we're racing with filemap_fault().
  85. *
  86. * We need to bale out if page->mapping is no longer equal to the original
  87. * mapping. This happens a) when the VM reclaimed the page while we waited on
  88. * its lock, b) when a concurrent invalidate_mapping_pages got there first and
  89. * c) when tmpfs swizzles a page between a tmpfs inode and swapper_space.
  90. */
  91. static int
  92. truncate_complete_page(struct address_space *mapping, struct page *page)
  93. {
  94. if (page->mapping != mapping)
  95. return -EIO;
  96. if (page_has_private(page))
  97. do_invalidatepage(page, 0);
  98. cancel_dirty_page(page, PAGE_CACHE_SIZE);
  99. clear_page_mlock(page);
  100. ClearPageMappedToDisk(page);
  101. delete_from_page_cache(page);
  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. * Used to get rid of pages on hardware memory corruption.
  135. */
  136. int generic_error_remove_page(struct address_space *mapping, struct page *page)
  137. {
  138. if (!mapping)
  139. return -EINVAL;
  140. /*
  141. * Only punch for normal data pages for now.
  142. * Handling other types like directories would need more auditing.
  143. */
  144. if (!S_ISREG(mapping->host->i_mode))
  145. return -EIO;
  146. return truncate_inode_page(mapping, page);
  147. }
  148. EXPORT_SYMBOL(generic_error_remove_page);
  149. /*
  150. * Safely invalidate one page from its pagecache mapping.
  151. * It only drops clean, unused pages. The page must be locked.
  152. *
  153. * Returns 1 if the page is successfully invalidated, otherwise 0.
  154. */
  155. int invalidate_inode_page(struct page *page)
  156. {
  157. struct address_space *mapping = page_mapping(page);
  158. if (!mapping)
  159. return 0;
  160. if (PageDirty(page) || PageWriteback(page))
  161. return 0;
  162. if (page_mapped(page))
  163. return 0;
  164. return invalidate_complete_page(mapping, page);
  165. }
  166. /**
  167. * truncate_inode_pages - truncate range of pages specified by start & end byte offsets
  168. * @mapping: mapping to truncate
  169. * @lstart: offset from which to truncate
  170. * @lend: offset to which to truncate
  171. *
  172. * Truncate the page cache, removing the pages that are between
  173. * specified offsets (and zeroing out partial page
  174. * (if lstart is not page aligned)).
  175. *
  176. * Truncate takes two passes - the first pass is nonblocking. It will not
  177. * block on page locks and it will not block on writeback. The second pass
  178. * will wait. This is to prevent as much IO as possible in the affected region.
  179. * The first pass will remove most pages, so the search cost of the second pass
  180. * is low.
  181. *
  182. * When looking at page->index outside the page lock we need to be careful to
  183. * copy it into a local to avoid races (it could change at any time).
  184. *
  185. * We pass down the cache-hot hint to the page freeing code. Even if the
  186. * mapping is large, it is probably the case that the final pages are the most
  187. * recently touched, and freeing happens in ascending file offset order.
  188. */
  189. void truncate_inode_pages_range(struct address_space *mapping,
  190. loff_t lstart, loff_t lend)
  191. {
  192. const pgoff_t start = (lstart + PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT;
  193. pgoff_t end;
  194. const unsigned partial = lstart & (PAGE_CACHE_SIZE - 1);
  195. struct pagevec pvec;
  196. pgoff_t next;
  197. int i;
  198. if (mapping->nrpages == 0)
  199. return;
  200. BUG_ON((lend & (PAGE_CACHE_SIZE - 1)) != (PAGE_CACHE_SIZE - 1));
  201. end = (lend >> PAGE_CACHE_SHIFT);
  202. pagevec_init(&pvec, 0);
  203. next = start;
  204. while (next <= end &&
  205. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  206. mem_cgroup_uncharge_start();
  207. for (i = 0; i < pagevec_count(&pvec); i++) {
  208. struct page *page = pvec.pages[i];
  209. pgoff_t page_index = page->index;
  210. if (page_index > end) {
  211. next = page_index;
  212. break;
  213. }
  214. if (page_index > next)
  215. next = page_index;
  216. next++;
  217. if (!trylock_page(page))
  218. continue;
  219. if (PageWriteback(page)) {
  220. unlock_page(page);
  221. continue;
  222. }
  223. truncate_inode_page(mapping, page);
  224. unlock_page(page);
  225. }
  226. pagevec_release(&pvec);
  227. mem_cgroup_uncharge_end();
  228. cond_resched();
  229. }
  230. if (partial) {
  231. struct page *page = find_lock_page(mapping, start - 1);
  232. if (page) {
  233. wait_on_page_writeback(page);
  234. truncate_partial_page(page, partial);
  235. unlock_page(page);
  236. page_cache_release(page);
  237. }
  238. }
  239. next = start;
  240. for ( ; ; ) {
  241. cond_resched();
  242. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  243. if (next == start)
  244. break;
  245. next = start;
  246. continue;
  247. }
  248. if (pvec.pages[0]->index > end) {
  249. pagevec_release(&pvec);
  250. break;
  251. }
  252. mem_cgroup_uncharge_start();
  253. for (i = 0; i < pagevec_count(&pvec); i++) {
  254. struct page *page = pvec.pages[i];
  255. if (page->index > end)
  256. break;
  257. lock_page(page);
  258. wait_on_page_writeback(page);
  259. truncate_inode_page(mapping, page);
  260. if (page->index > next)
  261. next = page->index;
  262. next++;
  263. unlock_page(page);
  264. }
  265. pagevec_release(&pvec);
  266. mem_cgroup_uncharge_end();
  267. }
  268. }
  269. EXPORT_SYMBOL(truncate_inode_pages_range);
  270. /**
  271. * truncate_inode_pages - truncate *all* the pages from an offset
  272. * @mapping: mapping to truncate
  273. * @lstart: offset from which to truncate
  274. *
  275. * Called under (and serialised by) inode->i_mutex.
  276. */
  277. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  278. {
  279. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  280. }
  281. EXPORT_SYMBOL(truncate_inode_pages);
  282. /**
  283. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  284. * @mapping: the address_space which holds the pages to invalidate
  285. * @start: the offset 'from' which to invalidate
  286. * @end: the offset 'to' which to invalidate (inclusive)
  287. *
  288. * This function only removes the unlocked pages, if you want to
  289. * remove all the pages of one inode, you must call truncate_inode_pages.
  290. *
  291. * invalidate_mapping_pages() will not block on IO activity. It will not
  292. * invalidate pages which are dirty, locked, under writeback or mapped into
  293. * pagetables.
  294. */
  295. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  296. pgoff_t start, pgoff_t end)
  297. {
  298. struct pagevec pvec;
  299. pgoff_t next = start;
  300. unsigned long ret;
  301. unsigned long count = 0;
  302. int i;
  303. pagevec_init(&pvec, 0);
  304. while (next <= end &&
  305. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  306. mem_cgroup_uncharge_start();
  307. for (i = 0; i < pagevec_count(&pvec); i++) {
  308. struct page *page = pvec.pages[i];
  309. pgoff_t index;
  310. int lock_failed;
  311. lock_failed = !trylock_page(page);
  312. /*
  313. * We really shouldn't be looking at the ->index of an
  314. * unlocked page. But we're not allowed to lock these
  315. * pages. So we rely upon nobody altering the ->index
  316. * of this (pinned-by-us) page.
  317. */
  318. index = page->index;
  319. if (index > next)
  320. next = index;
  321. next++;
  322. if (lock_failed)
  323. continue;
  324. ret = invalidate_inode_page(page);
  325. unlock_page(page);
  326. /*
  327. * Invalidation is a hint that the page is no longer
  328. * of interest and try to speed up its reclaim.
  329. */
  330. if (!ret)
  331. deactivate_page(page);
  332. count += ret;
  333. if (next > end)
  334. break;
  335. }
  336. pagevec_release(&pvec);
  337. mem_cgroup_uncharge_end();
  338. cond_resched();
  339. }
  340. return count;
  341. }
  342. EXPORT_SYMBOL(invalidate_mapping_pages);
  343. /*
  344. * This is like invalidate_complete_page(), except it ignores the page's
  345. * refcount. We do this because invalidate_inode_pages2() needs stronger
  346. * invalidation guarantees, and cannot afford to leave pages behind because
  347. * shrink_page_list() has a temp ref on them, or because they're transiently
  348. * sitting in the lru_cache_add() pagevecs.
  349. */
  350. static int
  351. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  352. {
  353. if (page->mapping != mapping)
  354. return 0;
  355. if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
  356. return 0;
  357. spin_lock_irq(&mapping->tree_lock);
  358. if (PageDirty(page))
  359. goto failed;
  360. clear_page_mlock(page);
  361. BUG_ON(page_has_private(page));
  362. __delete_from_page_cache(page);
  363. spin_unlock_irq(&mapping->tree_lock);
  364. mem_cgroup_uncharge_cache_page(page);
  365. if (mapping->a_ops->freepage)
  366. mapping->a_ops->freepage(page);
  367. page_cache_release(page); /* pagecache ref */
  368. return 1;
  369. failed:
  370. spin_unlock_irq(&mapping->tree_lock);
  371. return 0;
  372. }
  373. static int do_launder_page(struct address_space *mapping, struct page *page)
  374. {
  375. if (!PageDirty(page))
  376. return 0;
  377. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  378. return 0;
  379. return mapping->a_ops->launder_page(page);
  380. }
  381. /**
  382. * invalidate_inode_pages2_range - remove range of pages from an address_space
  383. * @mapping: the address_space
  384. * @start: the page offset 'from' which to invalidate
  385. * @end: the page offset 'to' which to invalidate (inclusive)
  386. *
  387. * Any pages which are found to be mapped into pagetables are unmapped prior to
  388. * invalidation.
  389. *
  390. * Returns -EBUSY if any pages could not be invalidated.
  391. */
  392. int invalidate_inode_pages2_range(struct address_space *mapping,
  393. pgoff_t start, pgoff_t end)
  394. {
  395. struct pagevec pvec;
  396. pgoff_t next;
  397. int i;
  398. int ret = 0;
  399. int ret2 = 0;
  400. int did_range_unmap = 0;
  401. int wrapped = 0;
  402. pagevec_init(&pvec, 0);
  403. next = start;
  404. while (next <= end && !wrapped &&
  405. pagevec_lookup(&pvec, mapping, next,
  406. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  407. mem_cgroup_uncharge_start();
  408. for (i = 0; i < pagevec_count(&pvec); i++) {
  409. struct page *page = pvec.pages[i];
  410. pgoff_t page_index;
  411. lock_page(page);
  412. if (page->mapping != mapping) {
  413. unlock_page(page);
  414. continue;
  415. }
  416. page_index = page->index;
  417. next = page_index + 1;
  418. if (next == 0)
  419. wrapped = 1;
  420. if (page_index > end) {
  421. unlock_page(page);
  422. break;
  423. }
  424. wait_on_page_writeback(page);
  425. if (page_mapped(page)) {
  426. if (!did_range_unmap) {
  427. /*
  428. * Zap the rest of the file in one hit.
  429. */
  430. unmap_mapping_range(mapping,
  431. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  432. (loff_t)(end - page_index + 1)
  433. << PAGE_CACHE_SHIFT,
  434. 0);
  435. did_range_unmap = 1;
  436. } else {
  437. /*
  438. * Just zap this page
  439. */
  440. unmap_mapping_range(mapping,
  441. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  442. PAGE_CACHE_SIZE, 0);
  443. }
  444. }
  445. BUG_ON(page_mapped(page));
  446. ret2 = do_launder_page(mapping, page);
  447. if (ret2 == 0) {
  448. if (!invalidate_complete_page2(mapping, page))
  449. ret2 = -EBUSY;
  450. }
  451. if (ret2 < 0)
  452. ret = ret2;
  453. unlock_page(page);
  454. }
  455. pagevec_release(&pvec);
  456. mem_cgroup_uncharge_end();
  457. cond_resched();
  458. }
  459. return ret;
  460. }
  461. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  462. /**
  463. * invalidate_inode_pages2 - remove all pages from an address_space
  464. * @mapping: the address_space
  465. *
  466. * Any pages which are found to be mapped into pagetables are unmapped prior to
  467. * invalidation.
  468. *
  469. * Returns -EBUSY if any pages could not be invalidated.
  470. */
  471. int invalidate_inode_pages2(struct address_space *mapping)
  472. {
  473. return invalidate_inode_pages2_range(mapping, 0, -1);
  474. }
  475. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
  476. /**
  477. * truncate_pagecache - unmap and remove pagecache that has been truncated
  478. * @inode: inode
  479. * @old: old file offset
  480. * @new: new file offset
  481. *
  482. * inode's new i_size must already be written before truncate_pagecache
  483. * is called.
  484. *
  485. * This function should typically be called before the filesystem
  486. * releases resources associated with the freed range (eg. deallocates
  487. * blocks). This way, pagecache will always stay logically coherent
  488. * with on-disk format, and the filesystem would not have to deal with
  489. * situations such as writepage being called for a page that has already
  490. * had its underlying blocks deallocated.
  491. */
  492. void truncate_pagecache(struct inode *inode, loff_t old, loff_t new)
  493. {
  494. struct address_space *mapping = inode->i_mapping;
  495. /*
  496. * unmap_mapping_range is called twice, first simply for
  497. * efficiency so that truncate_inode_pages does fewer
  498. * single-page unmaps. However after this first call, and
  499. * before truncate_inode_pages finishes, it is possible for
  500. * private pages to be COWed, which remain after
  501. * truncate_inode_pages finishes, hence the second
  502. * unmap_mapping_range call must be made for correctness.
  503. */
  504. unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
  505. truncate_inode_pages(mapping, new);
  506. unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
  507. }
  508. EXPORT_SYMBOL(truncate_pagecache);
  509. /**
  510. * truncate_setsize - update inode and pagecache for a new file size
  511. * @inode: inode
  512. * @newsize: new file size
  513. *
  514. * truncate_setsize updates i_size and performs pagecache truncation (if
  515. * necessary) to @newsize. It will be typically be called from the filesystem's
  516. * setattr function when ATTR_SIZE is passed in.
  517. *
  518. * Must be called with inode_mutex held and before all filesystem specific
  519. * block truncation has been performed.
  520. */
  521. void truncate_setsize(struct inode *inode, loff_t newsize)
  522. {
  523. loff_t oldsize;
  524. oldsize = inode->i_size;
  525. i_size_write(inode, newsize);
  526. truncate_pagecache(inode, oldsize, newsize);
  527. }
  528. EXPORT_SYMBOL(truncate_setsize);
  529. /**
  530. * vmtruncate - unmap mappings "freed" by truncate() syscall
  531. * @inode: inode of the file used
  532. * @offset: file offset to start truncating
  533. *
  534. * This function is deprecated and truncate_setsize or truncate_pagecache
  535. * should be used instead, together with filesystem specific block truncation.
  536. */
  537. int vmtruncate(struct inode *inode, loff_t offset)
  538. {
  539. int error;
  540. error = inode_newsize_ok(inode, offset);
  541. if (error)
  542. return error;
  543. truncate_setsize(inode, offset);
  544. if (inode->i_op->truncate)
  545. inode->i_op->truncate(inode);
  546. return 0;
  547. }
  548. EXPORT_SYMBOL(vmtruncate);