page.c 15 KB

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