page.c 14 KB

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