page.c 14 KB

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