page.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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 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. pagevec_init(&pvec);
  259. repeat:
  260. n = pagevec_lookup(&pvec, smap, &index);
  261. if (!n)
  262. return;
  263. for (i = 0; i < pagevec_count(&pvec); i++) {
  264. struct page *page = pvec.pages[i], *dpage;
  265. pgoff_t offset = page->index;
  266. lock_page(page);
  267. dpage = find_lock_page(dmap, offset);
  268. if (dpage) {
  269. /* overwrite existing page in the destination cache */
  270. WARN_ON(PageDirty(dpage));
  271. nilfs_copy_page(dpage, page, 0);
  272. unlock_page(dpage);
  273. put_page(dpage);
  274. /* Do we not need to remove page from smap here? */
  275. } else {
  276. struct page *p;
  277. /* move the page to the destination cache */
  278. xa_lock_irq(&smap->i_pages);
  279. p = __xa_erase(&smap->i_pages, offset);
  280. WARN_ON(page != p);
  281. smap->nrpages--;
  282. xa_unlock_irq(&smap->i_pages);
  283. xa_lock_irq(&dmap->i_pages);
  284. p = __xa_store(&dmap->i_pages, offset, page, GFP_NOFS);
  285. if (unlikely(p)) {
  286. /* Probably -ENOMEM */
  287. page->mapping = NULL;
  288. put_page(page);
  289. } else {
  290. page->mapping = dmap;
  291. dmap->nrpages++;
  292. if (PageDirty(page))
  293. __xa_set_mark(&dmap->i_pages, offset,
  294. PAGECACHE_TAG_DIRTY);
  295. }
  296. xa_unlock_irq(&dmap->i_pages);
  297. }
  298. unlock_page(page);
  299. }
  300. pagevec_release(&pvec);
  301. cond_resched();
  302. goto repeat;
  303. }
  304. /**
  305. * nilfs_clear_dirty_pages - discard dirty pages in address space
  306. * @mapping: address space with dirty pages for discarding
  307. * @silent: suppress [true] or print [false] warning messages
  308. */
  309. void nilfs_clear_dirty_pages(struct address_space *mapping, bool silent)
  310. {
  311. struct pagevec pvec;
  312. unsigned int i;
  313. pgoff_t index = 0;
  314. pagevec_init(&pvec);
  315. while (pagevec_lookup_tag(&pvec, mapping, &index,
  316. PAGECACHE_TAG_DIRTY)) {
  317. for (i = 0; i < pagevec_count(&pvec); i++) {
  318. struct page *page = pvec.pages[i];
  319. lock_page(page);
  320. nilfs_clear_dirty_page(page, silent);
  321. unlock_page(page);
  322. }
  323. pagevec_release(&pvec);
  324. cond_resched();
  325. }
  326. }
  327. /**
  328. * nilfs_clear_dirty_page - discard dirty page
  329. * @page: dirty page that will be discarded
  330. * @silent: suppress [true] or print [false] warning messages
  331. */
  332. void nilfs_clear_dirty_page(struct page *page, bool silent)
  333. {
  334. struct inode *inode = page->mapping->host;
  335. struct super_block *sb = inode->i_sb;
  336. BUG_ON(!PageLocked(page));
  337. if (!silent)
  338. nilfs_msg(sb, KERN_WARNING,
  339. "discard dirty page: offset=%lld, ino=%lu",
  340. page_offset(page), inode->i_ino);
  341. ClearPageUptodate(page);
  342. ClearPageMappedToDisk(page);
  343. if (page_has_buffers(page)) {
  344. struct buffer_head *bh, *head;
  345. const unsigned long clear_bits =
  346. (BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
  347. BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
  348. BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected));
  349. bh = head = page_buffers(page);
  350. do {
  351. lock_buffer(bh);
  352. if (!silent)
  353. nilfs_msg(sb, KERN_WARNING,
  354. "discard dirty block: blocknr=%llu, size=%zu",
  355. (u64)bh->b_blocknr, bh->b_size);
  356. set_mask_bits(&bh->b_state, clear_bits, 0);
  357. unlock_buffer(bh);
  358. } while (bh = bh->b_this_page, bh != head);
  359. }
  360. __nilfs_clear_page_dirty(page);
  361. }
  362. unsigned int nilfs_page_count_clean_buffers(struct page *page,
  363. unsigned int from, unsigned int to)
  364. {
  365. unsigned int block_start, block_end;
  366. struct buffer_head *bh, *head;
  367. unsigned int nc = 0;
  368. for (bh = head = page_buffers(page), block_start = 0;
  369. bh != head || !block_start;
  370. block_start = block_end, bh = bh->b_this_page) {
  371. block_end = block_start + bh->b_size;
  372. if (block_end > from && block_start < to && !buffer_dirty(bh))
  373. nc++;
  374. }
  375. return nc;
  376. }
  377. void nilfs_mapping_init(struct address_space *mapping, struct inode *inode)
  378. {
  379. mapping->host = inode;
  380. mapping->flags = 0;
  381. mapping_set_gfp_mask(mapping, GFP_NOFS);
  382. mapping->private_data = NULL;
  383. mapping->a_ops = &empty_aops;
  384. }
  385. /*
  386. * NILFS2 needs clear_page_dirty() in the following two cases:
  387. *
  388. * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
  389. * page dirty flags when it copies back pages from the shadow cache
  390. * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
  391. * (dat->{i_mapping,i_btnode_cache}).
  392. *
  393. * 2) Some B-tree operations like insertion or deletion may dispose buffers
  394. * in dirty state, and this needs to cancel the dirty state of their pages.
  395. */
  396. int __nilfs_clear_page_dirty(struct page *page)
  397. {
  398. struct address_space *mapping = page->mapping;
  399. if (mapping) {
  400. xa_lock_irq(&mapping->i_pages);
  401. if (test_bit(PG_dirty, &page->flags)) {
  402. __xa_clear_mark(&mapping->i_pages, page_index(page),
  403. PAGECACHE_TAG_DIRTY);
  404. xa_unlock_irq(&mapping->i_pages);
  405. return clear_page_dirty_for_io(page);
  406. }
  407. xa_unlock_irq(&mapping->i_pages);
  408. return 0;
  409. }
  410. return TestClearPageDirty(page);
  411. }
  412. /**
  413. * nilfs_find_uncommitted_extent - find extent of uncommitted data
  414. * @inode: inode
  415. * @start_blk: start block offset (in)
  416. * @blkoff: start offset of the found extent (out)
  417. *
  418. * This function searches an extent of buffers marked "delayed" which
  419. * starts from a block offset equal to or larger than @start_blk. If
  420. * such an extent was found, this will store the start offset in
  421. * @blkoff and return its length in blocks. Otherwise, zero is
  422. * returned.
  423. */
  424. unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
  425. sector_t start_blk,
  426. sector_t *blkoff)
  427. {
  428. unsigned int i;
  429. pgoff_t index;
  430. unsigned int nblocks_in_page;
  431. unsigned long length = 0;
  432. sector_t b;
  433. struct pagevec pvec;
  434. struct page *page;
  435. if (inode->i_mapping->nrpages == 0)
  436. return 0;
  437. index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
  438. nblocks_in_page = 1U << (PAGE_SHIFT - inode->i_blkbits);
  439. pagevec_init(&pvec);
  440. repeat:
  441. pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
  442. pvec.pages);
  443. if (pvec.nr == 0)
  444. return length;
  445. if (length > 0 && pvec.pages[0]->index > index)
  446. goto out;
  447. b = pvec.pages[0]->index << (PAGE_SHIFT - inode->i_blkbits);
  448. i = 0;
  449. do {
  450. page = pvec.pages[i];
  451. lock_page(page);
  452. if (page_has_buffers(page)) {
  453. struct buffer_head *bh, *head;
  454. bh = head = page_buffers(page);
  455. do {
  456. if (b < start_blk)
  457. continue;
  458. if (buffer_delay(bh)) {
  459. if (length == 0)
  460. *blkoff = b;
  461. length++;
  462. } else if (length > 0) {
  463. goto out_locked;
  464. }
  465. } while (++b, bh = bh->b_this_page, bh != head);
  466. } else {
  467. if (length > 0)
  468. goto out_locked;
  469. b += nblocks_in_page;
  470. }
  471. unlock_page(page);
  472. } while (++i < pagevec_count(&pvec));
  473. index = page->index + 1;
  474. pagevec_release(&pvec);
  475. cond_resched();
  476. goto repeat;
  477. out_locked:
  478. unlock_page(page);
  479. out:
  480. pagevec_release(&pvec);
  481. return length;
  482. }