page.c 14 KB

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