page.c 15 KB

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