page.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /*
  2. * page.c - buffer/page management specific to NILFS
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * Written by Ryusuke Konishi <ryusuke@osrg.net>,
  21. * Seiji Kihara <kihara@osrg.net>.
  22. */
  23. #include <linux/pagemap.h>
  24. #include <linux/writeback.h>
  25. #include <linux/swap.h>
  26. #include <linux/bitops.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/list.h>
  29. #include <linux/highmem.h>
  30. #include <linux/pagevec.h>
  31. #include <linux/gfp.h>
  32. #include "nilfs.h"
  33. #include "page.h"
  34. #include "mdt.h"
  35. #define NILFS_BUFFER_INHERENT_BITS \
  36. ((1UL << BH_Uptodate) | (1UL << BH_Mapped) | (1UL << BH_NILFS_Node) | \
  37. (1UL << BH_NILFS_Volatile) | (1UL << BH_NILFS_Allocated) | \
  38. (1UL << BH_NILFS_Checked))
  39. static struct buffer_head *
  40. __nilfs_get_page_block(struct page *page, unsigned long block, pgoff_t index,
  41. int blkbits, unsigned long b_state)
  42. {
  43. unsigned long first_block;
  44. struct buffer_head *bh;
  45. if (!page_has_buffers(page))
  46. create_empty_buffers(page, 1 << blkbits, b_state);
  47. first_block = (unsigned long)index << (PAGE_CACHE_SHIFT - blkbits);
  48. bh = nilfs_page_get_nth_block(page, block - first_block);
  49. touch_buffer(bh);
  50. wait_on_buffer(bh);
  51. return bh;
  52. }
  53. /*
  54. * Since the page cache of B-tree node pages or data page cache of pseudo
  55. * inodes does not have a valid mapping->host pointer, calling
  56. * mark_buffer_dirty() for their buffers causes a NULL pointer dereference;
  57. * it calls __mark_inode_dirty(NULL) through __set_page_dirty().
  58. * To avoid this problem, the old style mark_buffer_dirty() is used instead.
  59. */
  60. void nilfs_mark_buffer_dirty(struct buffer_head *bh)
  61. {
  62. if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh))
  63. __set_page_dirty_nobuffers(bh->b_page);
  64. }
  65. struct buffer_head *nilfs_grab_buffer(struct inode *inode,
  66. struct address_space *mapping,
  67. unsigned long blkoff,
  68. unsigned long b_state)
  69. {
  70. int blkbits = inode->i_blkbits;
  71. pgoff_t index = blkoff >> (PAGE_CACHE_SHIFT - blkbits);
  72. struct page *page, *opage;
  73. struct buffer_head *bh, *obh;
  74. page = grab_cache_page(mapping, index);
  75. if (unlikely(!page))
  76. return NULL;
  77. bh = __nilfs_get_page_block(page, blkoff, index, blkbits, b_state);
  78. if (unlikely(!bh)) {
  79. unlock_page(page);
  80. page_cache_release(page);
  81. return NULL;
  82. }
  83. if (!buffer_uptodate(bh) && mapping->assoc_mapping != NULL) {
  84. /*
  85. * Shadow page cache uses assoc_mapping to point its original
  86. * page cache. The following code tries the original cache
  87. * if the given cache is a shadow and it didn't hit.
  88. */
  89. opage = find_lock_page(mapping->assoc_mapping, index);
  90. if (!opage)
  91. return bh;
  92. obh = __nilfs_get_page_block(opage, blkoff, index, blkbits,
  93. b_state);
  94. if (buffer_uptodate(obh)) {
  95. nilfs_copy_buffer(bh, obh);
  96. if (buffer_dirty(obh)) {
  97. nilfs_mark_buffer_dirty(bh);
  98. if (!buffer_nilfs_node(bh) && NILFS_MDT(inode))
  99. nilfs_mdt_mark_dirty(inode);
  100. }
  101. }
  102. brelse(obh);
  103. unlock_page(opage);
  104. page_cache_release(opage);
  105. }
  106. return bh;
  107. }
  108. /**
  109. * nilfs_forget_buffer - discard dirty state
  110. * @inode: owner inode of the buffer
  111. * @bh: buffer head of the buffer to be discarded
  112. */
  113. void nilfs_forget_buffer(struct buffer_head *bh)
  114. {
  115. struct page *page = bh->b_page;
  116. lock_buffer(bh);
  117. clear_buffer_nilfs_volatile(bh);
  118. clear_buffer_nilfs_checked(bh);
  119. clear_buffer_dirty(bh);
  120. if (nilfs_page_buffers_clean(page))
  121. __nilfs_clear_page_dirty(page);
  122. clear_buffer_uptodate(bh);
  123. clear_buffer_mapped(bh);
  124. bh->b_blocknr = -1;
  125. ClearPageUptodate(page);
  126. ClearPageMappedToDisk(page);
  127. unlock_buffer(bh);
  128. brelse(bh);
  129. }
  130. /**
  131. * nilfs_copy_buffer -- copy buffer data and flags
  132. * @dbh: destination buffer
  133. * @sbh: source buffer
  134. */
  135. void nilfs_copy_buffer(struct buffer_head *dbh, struct buffer_head *sbh)
  136. {
  137. void *kaddr0, *kaddr1;
  138. unsigned long bits;
  139. struct page *spage = sbh->b_page, *dpage = dbh->b_page;
  140. struct buffer_head *bh;
  141. kaddr0 = kmap_atomic(spage, KM_USER0);
  142. kaddr1 = kmap_atomic(dpage, KM_USER1);
  143. memcpy(kaddr1 + bh_offset(dbh), kaddr0 + bh_offset(sbh), sbh->b_size);
  144. kunmap_atomic(kaddr1, KM_USER1);
  145. kunmap_atomic(kaddr0, KM_USER0);
  146. dbh->b_state = sbh->b_state & NILFS_BUFFER_INHERENT_BITS;
  147. dbh->b_blocknr = sbh->b_blocknr;
  148. dbh->b_bdev = sbh->b_bdev;
  149. bh = dbh;
  150. bits = sbh->b_state & ((1UL << BH_Uptodate) | (1UL << BH_Mapped));
  151. while ((bh = bh->b_this_page) != dbh) {
  152. lock_buffer(bh);
  153. bits &= bh->b_state;
  154. unlock_buffer(bh);
  155. }
  156. if (bits & (1UL << BH_Uptodate))
  157. SetPageUptodate(dpage);
  158. else
  159. ClearPageUptodate(dpage);
  160. if (bits & (1UL << BH_Mapped))
  161. SetPageMappedToDisk(dpage);
  162. else
  163. ClearPageMappedToDisk(dpage);
  164. }
  165. /**
  166. * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
  167. * @page: page to be checked
  168. *
  169. * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
  170. * Otherwise, it returns non-zero value.
  171. */
  172. int nilfs_page_buffers_clean(struct page *page)
  173. {
  174. struct buffer_head *bh, *head;
  175. bh = head = page_buffers(page);
  176. do {
  177. if (buffer_dirty(bh))
  178. return 0;
  179. bh = bh->b_this_page;
  180. } while (bh != head);
  181. return 1;
  182. }
  183. void nilfs_page_bug(struct page *page)
  184. {
  185. struct address_space *m;
  186. unsigned long ino = 0;
  187. if (unlikely(!page)) {
  188. printk(KERN_CRIT "NILFS_PAGE_BUG(NULL)\n");
  189. return;
  190. }
  191. m = page->mapping;
  192. if (m) {
  193. struct inode *inode = NILFS_AS_I(m);
  194. if (inode != NULL)
  195. ino = inode->i_ino;
  196. }
  197. printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
  198. "mapping=%p ino=%lu\n",
  199. page, atomic_read(&page->_count),
  200. (unsigned long long)page->index, page->flags, m, ino);
  201. if (page_has_buffers(page)) {
  202. struct buffer_head *bh, *head;
  203. int i = 0;
  204. bh = head = page_buffers(page);
  205. do {
  206. printk(KERN_CRIT
  207. " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
  208. i++, bh, atomic_read(&bh->b_count),
  209. (unsigned long long)bh->b_blocknr, bh->b_state);
  210. bh = bh->b_this_page;
  211. } while (bh != head);
  212. }
  213. }
  214. /**
  215. * nilfs_alloc_private_page - allocate a private page with buffer heads
  216. *
  217. * Return Value: On success, a pointer to the allocated page is returned.
  218. * On error, NULL is returned.
  219. */
  220. struct page *nilfs_alloc_private_page(struct block_device *bdev, int size,
  221. unsigned long state)
  222. {
  223. struct buffer_head *bh, *head, *tail;
  224. struct page *page;
  225. page = alloc_page(GFP_NOFS); /* page_count of the returned page is 1 */
  226. if (unlikely(!page))
  227. return NULL;
  228. lock_page(page);
  229. head = alloc_page_buffers(page, size, 0);
  230. if (unlikely(!head)) {
  231. unlock_page(page);
  232. __free_page(page);
  233. return NULL;
  234. }
  235. bh = head;
  236. do {
  237. bh->b_state = (1UL << BH_NILFS_Allocated) | state;
  238. tail = bh;
  239. bh->b_bdev = bdev;
  240. bh = bh->b_this_page;
  241. } while (bh);
  242. tail->b_this_page = head;
  243. attach_page_buffers(page, head);
  244. return page;
  245. }
  246. void nilfs_free_private_page(struct page *page)
  247. {
  248. BUG_ON(!PageLocked(page));
  249. BUG_ON(page->mapping);
  250. if (page_has_buffers(page) && !try_to_free_buffers(page))
  251. NILFS_PAGE_BUG(page, "failed to free page");
  252. unlock_page(page);
  253. __free_page(page);
  254. }
  255. /**
  256. * nilfs_copy_page -- copy the page with buffers
  257. * @dst: destination page
  258. * @src: source page
  259. * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
  260. *
  261. * This function is for both data pages and btnode pages. The dirty flag
  262. * should be treated by caller. The page must not be under i/o.
  263. * Both src and dst page must be locked
  264. */
  265. static void nilfs_copy_page(struct page *dst, struct page *src, int copy_dirty)
  266. {
  267. struct buffer_head *dbh, *dbufs, *sbh, *sbufs;
  268. unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
  269. BUG_ON(PageWriteback(dst));
  270. sbh = sbufs = page_buffers(src);
  271. if (!page_has_buffers(dst))
  272. create_empty_buffers(dst, sbh->b_size, 0);
  273. if (copy_dirty)
  274. mask |= (1UL << BH_Dirty);
  275. dbh = dbufs = page_buffers(dst);
  276. do {
  277. lock_buffer(sbh);
  278. lock_buffer(dbh);
  279. dbh->b_state = sbh->b_state & mask;
  280. dbh->b_blocknr = sbh->b_blocknr;
  281. dbh->b_bdev = sbh->b_bdev;
  282. sbh = sbh->b_this_page;
  283. dbh = dbh->b_this_page;
  284. } while (dbh != dbufs);
  285. copy_highpage(dst, src);
  286. if (PageUptodate(src) && !PageUptodate(dst))
  287. SetPageUptodate(dst);
  288. else if (!PageUptodate(src) && PageUptodate(dst))
  289. ClearPageUptodate(dst);
  290. if (PageMappedToDisk(src) && !PageMappedToDisk(dst))
  291. SetPageMappedToDisk(dst);
  292. else if (!PageMappedToDisk(src) && PageMappedToDisk(dst))
  293. ClearPageMappedToDisk(dst);
  294. do {
  295. unlock_buffer(sbh);
  296. unlock_buffer(dbh);
  297. sbh = sbh->b_this_page;
  298. dbh = dbh->b_this_page;
  299. } while (dbh != dbufs);
  300. }
  301. int nilfs_copy_dirty_pages(struct address_space *dmap,
  302. struct address_space *smap)
  303. {
  304. struct pagevec pvec;
  305. unsigned int i;
  306. pgoff_t index = 0;
  307. int err = 0;
  308. pagevec_init(&pvec, 0);
  309. repeat:
  310. if (!pagevec_lookup_tag(&pvec, smap, &index, PAGECACHE_TAG_DIRTY,
  311. PAGEVEC_SIZE))
  312. return 0;
  313. for (i = 0; i < pagevec_count(&pvec); i++) {
  314. struct page *page = pvec.pages[i], *dpage;
  315. lock_page(page);
  316. if (unlikely(!PageDirty(page)))
  317. NILFS_PAGE_BUG(page, "inconsistent dirty state");
  318. dpage = grab_cache_page(dmap, page->index);
  319. if (unlikely(!dpage)) {
  320. /* No empty page is added to the page cache */
  321. err = -ENOMEM;
  322. unlock_page(page);
  323. break;
  324. }
  325. if (unlikely(!page_has_buffers(page)))
  326. NILFS_PAGE_BUG(page,
  327. "found empty page in dat page cache");
  328. nilfs_copy_page(dpage, page, 1);
  329. __set_page_dirty_nobuffers(dpage);
  330. unlock_page(dpage);
  331. page_cache_release(dpage);
  332. unlock_page(page);
  333. }
  334. pagevec_release(&pvec);
  335. cond_resched();
  336. if (likely(!err))
  337. goto repeat;
  338. return err;
  339. }
  340. /**
  341. * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
  342. * @dmap: destination page cache
  343. * @smap: source page cache
  344. *
  345. * No pages must no be added to the cache during this process.
  346. * This must be ensured by the caller.
  347. */
  348. void nilfs_copy_back_pages(struct address_space *dmap,
  349. struct address_space *smap)
  350. {
  351. struct pagevec pvec;
  352. unsigned int i, n;
  353. pgoff_t index = 0;
  354. int err;
  355. pagevec_init(&pvec, 0);
  356. repeat:
  357. n = pagevec_lookup(&pvec, smap, index, PAGEVEC_SIZE);
  358. if (!n)
  359. return;
  360. index = pvec.pages[n - 1]->index + 1;
  361. for (i = 0; i < pagevec_count(&pvec); i++) {
  362. struct page *page = pvec.pages[i], *dpage;
  363. pgoff_t offset = page->index;
  364. lock_page(page);
  365. dpage = find_lock_page(dmap, offset);
  366. if (dpage) {
  367. /* override existing page on the destination cache */
  368. WARN_ON(PageDirty(dpage));
  369. nilfs_copy_page(dpage, page, 0);
  370. unlock_page(dpage);
  371. page_cache_release(dpage);
  372. } else {
  373. struct page *page2;
  374. /* move the page to the destination cache */
  375. spin_lock_irq(&smap->tree_lock);
  376. page2 = radix_tree_delete(&smap->page_tree, offset);
  377. WARN_ON(page2 != page);
  378. smap->nrpages--;
  379. spin_unlock_irq(&smap->tree_lock);
  380. spin_lock_irq(&dmap->tree_lock);
  381. err = radix_tree_insert(&dmap->page_tree, offset, page);
  382. if (unlikely(err < 0)) {
  383. WARN_ON(err == -EEXIST);
  384. page->mapping = NULL;
  385. page_cache_release(page); /* for cache */
  386. } else {
  387. page->mapping = dmap;
  388. dmap->nrpages++;
  389. if (PageDirty(page))
  390. radix_tree_tag_set(&dmap->page_tree,
  391. offset,
  392. PAGECACHE_TAG_DIRTY);
  393. }
  394. spin_unlock_irq(&dmap->tree_lock);
  395. }
  396. unlock_page(page);
  397. }
  398. pagevec_release(&pvec);
  399. cond_resched();
  400. goto repeat;
  401. }
  402. void nilfs_clear_dirty_pages(struct address_space *mapping)
  403. {
  404. struct pagevec pvec;
  405. unsigned int i;
  406. pgoff_t index = 0;
  407. pagevec_init(&pvec, 0);
  408. while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
  409. PAGEVEC_SIZE)) {
  410. for (i = 0; i < pagevec_count(&pvec); i++) {
  411. struct page *page = pvec.pages[i];
  412. struct buffer_head *bh, *head;
  413. lock_page(page);
  414. ClearPageUptodate(page);
  415. ClearPageMappedToDisk(page);
  416. bh = head = page_buffers(page);
  417. do {
  418. lock_buffer(bh);
  419. clear_buffer_dirty(bh);
  420. clear_buffer_nilfs_volatile(bh);
  421. clear_buffer_nilfs_checked(bh);
  422. clear_buffer_uptodate(bh);
  423. clear_buffer_mapped(bh);
  424. unlock_buffer(bh);
  425. bh = bh->b_this_page;
  426. } while (bh != head);
  427. __nilfs_clear_page_dirty(page);
  428. unlock_page(page);
  429. }
  430. pagevec_release(&pvec);
  431. cond_resched();
  432. }
  433. }
  434. unsigned nilfs_page_count_clean_buffers(struct page *page,
  435. unsigned from, unsigned to)
  436. {
  437. unsigned block_start, block_end;
  438. struct buffer_head *bh, *head;
  439. unsigned nc = 0;
  440. for (bh = head = page_buffers(page), block_start = 0;
  441. bh != head || !block_start;
  442. block_start = block_end, bh = bh->b_this_page) {
  443. block_end = block_start + bh->b_size;
  444. if (block_end > from && block_start < to && !buffer_dirty(bh))
  445. nc++;
  446. }
  447. return nc;
  448. }
  449. /*
  450. * NILFS2 needs clear_page_dirty() in the following two cases:
  451. *
  452. * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
  453. * page dirty flags when it copies back pages from the shadow cache
  454. * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
  455. * (dat->{i_mapping,i_btnode_cache}).
  456. *
  457. * 2) Some B-tree operations like insertion or deletion may dispose buffers
  458. * in dirty state, and this needs to cancel the dirty state of their pages.
  459. */
  460. int __nilfs_clear_page_dirty(struct page *page)
  461. {
  462. struct address_space *mapping = page->mapping;
  463. if (mapping) {
  464. spin_lock_irq(&mapping->tree_lock);
  465. if (test_bit(PG_dirty, &page->flags)) {
  466. radix_tree_tag_clear(&mapping->page_tree,
  467. page_index(page),
  468. PAGECACHE_TAG_DIRTY);
  469. spin_unlock_irq(&mapping->tree_lock);
  470. return clear_page_dirty_for_io(page);
  471. }
  472. spin_unlock_irq(&mapping->tree_lock);
  473. return 0;
  474. }
  475. return TestClearPageDirty(page);
  476. }