page.c 14 KB

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