truncate.c 18 KB

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