page.c 14 KB

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