page.c 14 KB

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