page.c 14 KB

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