truncate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. void truncate_inode_pages(struct address_space *mapping, loff_t lstart)
  282. {
  283. truncate_inode_pages_range(mapping, lstart, (loff_t)-1);
  284. }
  285. EXPORT_SYMBOL(truncate_inode_pages);
  286. /**
  287. * invalidate_mapping_pages - Invalidate all the unlocked pages of one inode
  288. * @mapping: the address_space which holds the pages to invalidate
  289. * @start: the offset 'from' which to invalidate
  290. * @end: the offset 'to' which to invalidate (inclusive)
  291. *
  292. * This function only removes the unlocked pages, if you want to
  293. * remove all the pages of one inode, you must call truncate_inode_pages.
  294. *
  295. * invalidate_mapping_pages() will not block on IO activity. It will not
  296. * invalidate pages which are dirty, locked, under writeback or mapped into
  297. * pagetables.
  298. */
  299. unsigned long invalidate_mapping_pages(struct address_space *mapping,
  300. pgoff_t start, pgoff_t end)
  301. {
  302. struct pagevec pvec;
  303. pgoff_t next = start;
  304. unsigned long ret;
  305. unsigned long count = 0;
  306. int i;
  307. pagevec_init(&pvec, 0);
  308. while (next <= end &&
  309. pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  310. mem_cgroup_uncharge_start();
  311. for (i = 0; i < pagevec_count(&pvec); i++) {
  312. struct page *page = pvec.pages[i];
  313. pgoff_t index;
  314. int lock_failed;
  315. lock_failed = !trylock_page(page);
  316. /*
  317. * We really shouldn't be looking at the ->index of an
  318. * unlocked page. But we're not allowed to lock these
  319. * pages. So we rely upon nobody altering the ->index
  320. * of this (pinned-by-us) page.
  321. */
  322. index = page->index;
  323. if (index > next)
  324. next = index;
  325. next++;
  326. if (lock_failed)
  327. continue;
  328. ret = invalidate_inode_page(page);
  329. unlock_page(page);
  330. /*
  331. * Invalidation is a hint that the page is no longer
  332. * of interest and try to speed up its reclaim.
  333. */
  334. if (!ret)
  335. deactivate_page(page);
  336. count += ret;
  337. if (next > end)
  338. break;
  339. }
  340. pagevec_release(&pvec);
  341. mem_cgroup_uncharge_end();
  342. cond_resched();
  343. }
  344. return count;
  345. }
  346. EXPORT_SYMBOL(invalidate_mapping_pages);
  347. /*
  348. * This is like invalidate_complete_page(), except it ignores the page's
  349. * refcount. We do this because invalidate_inode_pages2() needs stronger
  350. * invalidation guarantees, and cannot afford to leave pages behind because
  351. * shrink_page_list() has a temp ref on them, or because they're transiently
  352. * sitting in the lru_cache_add() pagevecs.
  353. */
  354. static int
  355. invalidate_complete_page2(struct address_space *mapping, struct page *page)
  356. {
  357. if (page->mapping != mapping)
  358. return 0;
  359. if (page_has_private(page) && !try_to_release_page(page, GFP_KERNEL))
  360. return 0;
  361. spin_lock_irq(&mapping->tree_lock);
  362. if (PageDirty(page))
  363. goto failed;
  364. clear_page_mlock(page);
  365. BUG_ON(page_has_private(page));
  366. __delete_from_page_cache(page);
  367. spin_unlock_irq(&mapping->tree_lock);
  368. mem_cgroup_uncharge_cache_page(page);
  369. if (mapping->a_ops->freepage)
  370. mapping->a_ops->freepage(page);
  371. page_cache_release(page); /* pagecache ref */
  372. return 1;
  373. failed:
  374. spin_unlock_irq(&mapping->tree_lock);
  375. return 0;
  376. }
  377. static int do_launder_page(struct address_space *mapping, struct page *page)
  378. {
  379. if (!PageDirty(page))
  380. return 0;
  381. if (page->mapping != mapping || mapping->a_ops->launder_page == NULL)
  382. return 0;
  383. return mapping->a_ops->launder_page(page);
  384. }
  385. /**
  386. * invalidate_inode_pages2_range - remove range of pages from an address_space
  387. * @mapping: the address_space
  388. * @start: the page offset 'from' which to invalidate
  389. * @end: the page offset 'to' which to invalidate (inclusive)
  390. *
  391. * Any pages which are found to be mapped into pagetables are unmapped prior to
  392. * invalidation.
  393. *
  394. * Returns -EBUSY if any pages could not be invalidated.
  395. */
  396. int invalidate_inode_pages2_range(struct address_space *mapping,
  397. pgoff_t start, pgoff_t end)
  398. {
  399. struct pagevec pvec;
  400. pgoff_t next;
  401. int i;
  402. int ret = 0;
  403. int ret2 = 0;
  404. int did_range_unmap = 0;
  405. int wrapped = 0;
  406. cleancache_flush_inode(mapping);
  407. pagevec_init(&pvec, 0);
  408. next = start;
  409. while (next <= end && !wrapped &&
  410. pagevec_lookup(&pvec, mapping, next,
  411. min(end - next, (pgoff_t)PAGEVEC_SIZE - 1) + 1)) {
  412. mem_cgroup_uncharge_start();
  413. for (i = 0; i < pagevec_count(&pvec); i++) {
  414. struct page *page = pvec.pages[i];
  415. pgoff_t page_index;
  416. lock_page(page);
  417. if (page->mapping != mapping) {
  418. unlock_page(page);
  419. continue;
  420. }
  421. page_index = page->index;
  422. next = page_index + 1;
  423. if (next == 0)
  424. wrapped = 1;
  425. if (page_index > end) {
  426. unlock_page(page);
  427. break;
  428. }
  429. wait_on_page_writeback(page);
  430. if (page_mapped(page)) {
  431. if (!did_range_unmap) {
  432. /*
  433. * Zap the rest of the file in one hit.
  434. */
  435. unmap_mapping_range(mapping,
  436. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  437. (loff_t)(end - page_index + 1)
  438. << PAGE_CACHE_SHIFT,
  439. 0);
  440. did_range_unmap = 1;
  441. } else {
  442. /*
  443. * Just zap this page
  444. */
  445. unmap_mapping_range(mapping,
  446. (loff_t)page_index<<PAGE_CACHE_SHIFT,
  447. PAGE_CACHE_SIZE, 0);
  448. }
  449. }
  450. BUG_ON(page_mapped(page));
  451. ret2 = do_launder_page(mapping, page);
  452. if (ret2 == 0) {
  453. if (!invalidate_complete_page2(mapping, page))
  454. ret2 = -EBUSY;
  455. }
  456. if (ret2 < 0)
  457. ret = ret2;
  458. unlock_page(page);
  459. }
  460. pagevec_release(&pvec);
  461. mem_cgroup_uncharge_end();
  462. cond_resched();
  463. }
  464. cleancache_flush_inode(mapping);
  465. return ret;
  466. }
  467. EXPORT_SYMBOL_GPL(invalidate_inode_pages2_range);
  468. /**
  469. * invalidate_inode_pages2 - remove all pages from an address_space
  470. * @mapping: the address_space
  471. *
  472. * Any pages which are found to be mapped into pagetables are unmapped prior to
  473. * invalidation.
  474. *
  475. * Returns -EBUSY if any pages could not be invalidated.
  476. */
  477. int invalidate_inode_pages2(struct address_space *mapping)
  478. {
  479. return invalidate_inode_pages2_range(mapping, 0, -1);
  480. }
  481. EXPORT_SYMBOL_GPL(invalidate_inode_pages2);
  482. /**
  483. * truncate_pagecache - unmap and remove pagecache that has been truncated
  484. * @inode: inode
  485. * @old: old file offset
  486. * @new: new file offset
  487. *
  488. * inode's new i_size must already be written before truncate_pagecache
  489. * is called.
  490. *
  491. * This function should typically be called before the filesystem
  492. * releases resources associated with the freed range (eg. deallocates
  493. * blocks). This way, pagecache will always stay logically coherent
  494. * with on-disk format, and the filesystem would not have to deal with
  495. * situations such as writepage being called for a page that has already
  496. * had its underlying blocks deallocated.
  497. */
  498. void truncate_pagecache(struct inode *inode, loff_t old, loff_t new)
  499. {
  500. struct address_space *mapping = inode->i_mapping;
  501. /*
  502. * unmap_mapping_range is called twice, first simply for
  503. * efficiency so that truncate_inode_pages does fewer
  504. * single-page unmaps. However after this first call, and
  505. * before truncate_inode_pages finishes, it is possible for
  506. * private pages to be COWed, which remain after
  507. * truncate_inode_pages finishes, hence the second
  508. * unmap_mapping_range call must be made for correctness.
  509. */
  510. unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
  511. truncate_inode_pages(mapping, new);
  512. unmap_mapping_range(mapping, new + PAGE_SIZE - 1, 0, 1);
  513. }
  514. EXPORT_SYMBOL(truncate_pagecache);
  515. /**
  516. * truncate_setsize - update inode and pagecache for a new file size
  517. * @inode: inode
  518. * @newsize: new file size
  519. *
  520. * truncate_setsize updates i_size and performs pagecache truncation (if
  521. * necessary) to @newsize. It will be typically be called from the filesystem's
  522. * setattr function when ATTR_SIZE is passed in.
  523. *
  524. * Must be called with inode_mutex held and before all filesystem specific
  525. * block truncation has been performed.
  526. */
  527. void truncate_setsize(struct inode *inode, loff_t newsize)
  528. {
  529. loff_t oldsize;
  530. oldsize = inode->i_size;
  531. i_size_write(inode, newsize);
  532. truncate_pagecache(inode, oldsize, newsize);
  533. }
  534. EXPORT_SYMBOL(truncate_setsize);
  535. /**
  536. * vmtruncate - unmap mappings "freed" by truncate() syscall
  537. * @inode: inode of the file used
  538. * @offset: file offset to start truncating
  539. *
  540. * This function is deprecated and truncate_setsize or truncate_pagecache
  541. * should be used instead, together with filesystem specific block truncation.
  542. */
  543. int vmtruncate(struct inode *inode, loff_t offset)
  544. {
  545. int error;
  546. error = inode_newsize_ok(inode, offset);
  547. if (error)
  548. return error;
  549. truncate_setsize(inode, offset);
  550. if (inode->i_op->truncate)
  551. inode->i_op->truncate(inode);
  552. return 0;
  553. }
  554. EXPORT_SYMBOL(vmtruncate);