page.c 14 KB

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