page.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * 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 *__nilfs_get_folio_block(struct folio *folio,
  25. unsigned long block, pgoff_t index, int blkbits,
  26. unsigned long b_state)
  27. {
  28. unsigned long first_block;
  29. struct buffer_head *bh = folio_buffers(folio);
  30. if (!bh)
  31. bh = create_empty_buffers(folio, 1 << blkbits, b_state);
  32. first_block = (unsigned long)index << (PAGE_SHIFT - blkbits);
  33. bh = get_nth_bh(bh, block - first_block);
  34. wait_on_buffer(bh);
  35. return bh;
  36. }
  37. struct buffer_head *nilfs_grab_buffer(struct inode *inode,
  38. struct address_space *mapping,
  39. unsigned long blkoff,
  40. unsigned long b_state)
  41. {
  42. int blkbits = inode->i_blkbits;
  43. pgoff_t index = blkoff >> (PAGE_SHIFT - blkbits);
  44. struct folio *folio;
  45. struct buffer_head *bh;
  46. folio = filemap_grab_folio(mapping, index);
  47. if (IS_ERR(folio))
  48. return NULL;
  49. bh = __nilfs_get_folio_block(folio, blkoff, index, blkbits, b_state);
  50. if (unlikely(!bh)) {
  51. folio_unlock(folio);
  52. folio_put(folio);
  53. return NULL;
  54. }
  55. bh->b_bdev = inode->i_sb->s_bdev;
  56. return bh;
  57. }
  58. /**
  59. * nilfs_forget_buffer - discard dirty state
  60. * @bh: buffer head of the buffer to be discarded
  61. */
  62. void nilfs_forget_buffer(struct buffer_head *bh)
  63. {
  64. struct folio *folio = bh->b_folio;
  65. const unsigned long clear_bits =
  66. (BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
  67. BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
  68. BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected) |
  69. BIT(BH_Delay));
  70. lock_buffer(bh);
  71. set_mask_bits(&bh->b_state, clear_bits, 0);
  72. if (nilfs_folio_buffers_clean(folio))
  73. __nilfs_clear_folio_dirty(folio);
  74. bh->b_blocknr = -1;
  75. folio_clear_uptodate(folio);
  76. folio_clear_mappedtodisk(folio);
  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 *saddr, *daddr;
  88. unsigned long bits;
  89. struct folio *sfolio = sbh->b_folio, *dfolio = dbh->b_folio;
  90. struct buffer_head *bh;
  91. saddr = kmap_local_folio(sfolio, bh_offset(sbh));
  92. daddr = kmap_local_folio(dfolio, bh_offset(dbh));
  93. memcpy(daddr, saddr, sbh->b_size);
  94. kunmap_local(daddr);
  95. kunmap_local(saddr);
  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. folio_mark_uptodate(dfolio);
  108. else
  109. folio_clear_uptodate(dfolio);
  110. if (bits & BIT(BH_Mapped))
  111. folio_set_mappedtodisk(dfolio);
  112. else
  113. folio_clear_mappedtodisk(dfolio);
  114. }
  115. /**
  116. * nilfs_folio_buffers_clean - Check if a folio has dirty buffers or not.
  117. * @folio: Folio to be checked.
  118. *
  119. * Return: false if the folio has dirty buffers, true otherwise.
  120. */
  121. bool nilfs_folio_buffers_clean(struct folio *folio)
  122. {
  123. struct buffer_head *bh, *head;
  124. bh = head = folio_buffers(folio);
  125. do {
  126. if (buffer_dirty(bh))
  127. return false;
  128. bh = bh->b_this_page;
  129. } while (bh != head);
  130. return true;
  131. }
  132. void nilfs_folio_bug(struct folio *folio)
  133. {
  134. struct buffer_head *bh, *head;
  135. struct address_space *m;
  136. unsigned long ino;
  137. if (unlikely(!folio)) {
  138. printk(KERN_CRIT "NILFS_FOLIO_BUG(NULL)\n");
  139. return;
  140. }
  141. m = folio->mapping;
  142. ino = m ? m->host->i_ino : 0;
  143. printk(KERN_CRIT "NILFS_FOLIO_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
  144. "mapping=%p ino=%lu\n",
  145. folio, folio_ref_count(folio),
  146. (unsigned long long)folio->index, folio->flags.f, m, ino);
  147. head = folio_buffers(folio);
  148. if (head) {
  149. int i = 0;
  150. bh = head;
  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_folio -- copy the folio with buffers
  162. * @dst: destination folio
  163. * @src: source folio
  164. * @copy_dirty: flag whether to copy dirty states on the folio's buffer heads.
  165. *
  166. * This function is for both data folios and btnode folios. The dirty flag
  167. * should be treated by caller. The folio must not be under i/o.
  168. * Both src and dst folio must be locked
  169. */
  170. static void nilfs_copy_folio(struct folio *dst, struct folio *src,
  171. bool copy_dirty)
  172. {
  173. struct buffer_head *dbh, *dbufs, *sbh;
  174. unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
  175. BUG_ON(folio_test_writeback(dst));
  176. sbh = folio_buffers(src);
  177. dbh = folio_buffers(dst);
  178. if (!dbh)
  179. dbh = create_empty_buffers(dst, sbh->b_size, 0);
  180. if (copy_dirty)
  181. mask |= BIT(BH_Dirty);
  182. dbufs = dbh;
  183. do {
  184. lock_buffer(sbh);
  185. lock_buffer(dbh);
  186. dbh->b_state = sbh->b_state & mask;
  187. dbh->b_blocknr = sbh->b_blocknr;
  188. dbh->b_bdev = sbh->b_bdev;
  189. sbh = sbh->b_this_page;
  190. dbh = dbh->b_this_page;
  191. } while (dbh != dbufs);
  192. folio_copy(dst, src);
  193. if (folio_test_uptodate(src) && !folio_test_uptodate(dst))
  194. folio_mark_uptodate(dst);
  195. else if (!folio_test_uptodate(src) && folio_test_uptodate(dst))
  196. folio_clear_uptodate(dst);
  197. if (folio_test_mappedtodisk(src) && !folio_test_mappedtodisk(dst))
  198. folio_set_mappedtodisk(dst);
  199. else if (!folio_test_mappedtodisk(src) && folio_test_mappedtodisk(dst))
  200. folio_clear_mappedtodisk(dst);
  201. do {
  202. unlock_buffer(sbh);
  203. unlock_buffer(dbh);
  204. sbh = sbh->b_this_page;
  205. dbh = dbh->b_this_page;
  206. } while (dbh != dbufs);
  207. }
  208. int nilfs_copy_dirty_pages(struct address_space *dmap,
  209. struct address_space *smap)
  210. {
  211. struct folio_batch fbatch;
  212. unsigned int i;
  213. pgoff_t index = 0;
  214. int err = 0;
  215. folio_batch_init(&fbatch);
  216. repeat:
  217. if (!filemap_get_folios_tag(smap, &index, (pgoff_t)-1,
  218. PAGECACHE_TAG_DIRTY, &fbatch))
  219. return 0;
  220. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  221. struct folio *folio = fbatch.folios[i], *dfolio;
  222. folio_lock(folio);
  223. if (unlikely(!folio_test_dirty(folio)))
  224. NILFS_FOLIO_BUG(folio, "inconsistent dirty state");
  225. dfolio = filemap_grab_folio(dmap, folio->index);
  226. if (IS_ERR(dfolio)) {
  227. /* No empty page is added to the page cache */
  228. folio_unlock(folio);
  229. err = PTR_ERR(dfolio);
  230. break;
  231. }
  232. if (unlikely(!folio_buffers(folio)))
  233. NILFS_FOLIO_BUG(folio,
  234. "found empty page in dat page cache");
  235. nilfs_copy_folio(dfolio, folio, true);
  236. filemap_dirty_folio(folio_mapping(dfolio), dfolio);
  237. folio_unlock(dfolio);
  238. folio_put(dfolio);
  239. folio_unlock(folio);
  240. }
  241. folio_batch_release(&fbatch);
  242. cond_resched();
  243. if (likely(!err))
  244. goto repeat;
  245. return err;
  246. }
  247. /**
  248. * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
  249. * @dmap: destination page cache
  250. * @smap: source page cache
  251. *
  252. * No pages must be added to the cache during this process.
  253. * This must be ensured by the caller.
  254. */
  255. void nilfs_copy_back_pages(struct address_space *dmap,
  256. struct address_space *smap)
  257. {
  258. struct folio_batch fbatch;
  259. unsigned int i, n;
  260. pgoff_t start = 0;
  261. folio_batch_init(&fbatch);
  262. repeat:
  263. n = filemap_get_folios(smap, &start, ~0UL, &fbatch);
  264. if (!n)
  265. return;
  266. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  267. struct folio *folio = fbatch.folios[i], *dfolio;
  268. pgoff_t index = folio->index;
  269. folio_lock(folio);
  270. dfolio = filemap_lock_folio(dmap, index);
  271. if (!IS_ERR(dfolio)) {
  272. /* overwrite existing folio in the destination cache */
  273. WARN_ON(folio_test_dirty(dfolio));
  274. nilfs_copy_folio(dfolio, folio, false);
  275. folio_unlock(dfolio);
  276. folio_put(dfolio);
  277. /* Do we not need to remove folio from smap here? */
  278. } else {
  279. struct folio *f;
  280. /* move the folio to the destination cache */
  281. xa_lock_irq(&smap->i_pages);
  282. f = __xa_erase(&smap->i_pages, index);
  283. WARN_ON(folio != f);
  284. smap->nrpages--;
  285. xa_unlock_irq(&smap->i_pages);
  286. xa_lock_irq(&dmap->i_pages);
  287. f = __xa_store(&dmap->i_pages, index, folio, GFP_NOFS);
  288. if (unlikely(f)) {
  289. /* Probably -ENOMEM */
  290. folio->mapping = NULL;
  291. folio_put(folio);
  292. } else {
  293. folio->mapping = dmap;
  294. dmap->nrpages++;
  295. if (folio_test_dirty(folio))
  296. __xa_set_mark(&dmap->i_pages, index,
  297. PAGECACHE_TAG_DIRTY);
  298. }
  299. xa_unlock_irq(&dmap->i_pages);
  300. }
  301. folio_unlock(folio);
  302. }
  303. folio_batch_release(&fbatch);
  304. cond_resched();
  305. goto repeat;
  306. }
  307. /**
  308. * nilfs_clear_dirty_pages - discard dirty pages in address space
  309. * @mapping: address space with dirty pages for discarding
  310. */
  311. void nilfs_clear_dirty_pages(struct address_space *mapping)
  312. {
  313. struct folio_batch fbatch;
  314. unsigned int i;
  315. pgoff_t index = 0;
  316. folio_batch_init(&fbatch);
  317. while (filemap_get_folios_tag(mapping, &index, (pgoff_t)-1,
  318. PAGECACHE_TAG_DIRTY, &fbatch)) {
  319. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  320. struct folio *folio = fbatch.folios[i];
  321. folio_lock(folio);
  322. /*
  323. * This folio may have been removed from the address
  324. * space by truncation or invalidation when the lock
  325. * was acquired. Skip processing in that case.
  326. */
  327. if (likely(folio->mapping == mapping))
  328. nilfs_clear_folio_dirty(folio);
  329. folio_unlock(folio);
  330. }
  331. folio_batch_release(&fbatch);
  332. cond_resched();
  333. }
  334. }
  335. /**
  336. * nilfs_clear_folio_dirty - discard dirty folio
  337. * @folio: dirty folio that will be discarded
  338. *
  339. * nilfs_clear_folio_dirty() clears working states including dirty state for
  340. * the folio and its buffers. If the folio has buffers, clear only if it is
  341. * confirmed that none of the buffer heads are busy (none have valid
  342. * references and none are locked).
  343. */
  344. void nilfs_clear_folio_dirty(struct folio *folio)
  345. {
  346. struct buffer_head *bh, *head;
  347. BUG_ON(!folio_test_locked(folio));
  348. head = folio_buffers(folio);
  349. if (head) {
  350. const unsigned long clear_bits =
  351. (BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
  352. BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
  353. BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected) |
  354. BIT(BH_Delay));
  355. bool busy, invalidated = false;
  356. recheck_buffers:
  357. busy = false;
  358. bh = head;
  359. do {
  360. if (atomic_read(&bh->b_count) | buffer_locked(bh)) {
  361. busy = true;
  362. break;
  363. }
  364. } while (bh = bh->b_this_page, bh != head);
  365. if (busy) {
  366. if (invalidated)
  367. return;
  368. invalidate_bh_lrus();
  369. invalidated = true;
  370. goto recheck_buffers;
  371. }
  372. bh = head;
  373. do {
  374. lock_buffer(bh);
  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. folio_clear_uptodate(folio);
  380. folio_clear_mappedtodisk(folio);
  381. folio_clear_checked(folio);
  382. __nilfs_clear_folio_dirty(folio);
  383. }
  384. unsigned int nilfs_page_count_clean_buffers(struct folio *folio,
  385. unsigned int from, unsigned int to)
  386. {
  387. unsigned int block_start, block_end;
  388. struct buffer_head *bh, *head;
  389. unsigned int nc = 0;
  390. for (bh = head = folio_buffers(folio), block_start = 0;
  391. bh != head || !block_start;
  392. block_start = block_end, bh = bh->b_this_page) {
  393. block_end = block_start + bh->b_size;
  394. if (block_end > from && block_start < to && !buffer_dirty(bh))
  395. nc++;
  396. }
  397. return nc;
  398. }
  399. /*
  400. * NILFS2 needs clear_page_dirty() in the following two cases:
  401. *
  402. * 1) For B-tree node pages and data pages of DAT file, NILFS2 clears dirty
  403. * flag of pages when it copies back pages from shadow cache to the
  404. * original cache.
  405. *
  406. * 2) Some B-tree operations like insertion or deletion may dispose buffers
  407. * in dirty state, and this needs to cancel the dirty state of their pages.
  408. */
  409. void __nilfs_clear_folio_dirty(struct folio *folio)
  410. {
  411. struct address_space *mapping = folio->mapping;
  412. if (mapping) {
  413. xa_lock_irq(&mapping->i_pages);
  414. if (folio_test_dirty(folio)) {
  415. __xa_clear_mark(&mapping->i_pages, folio->index,
  416. PAGECACHE_TAG_DIRTY);
  417. xa_unlock_irq(&mapping->i_pages);
  418. folio_clear_dirty_for_io(folio);
  419. return;
  420. }
  421. xa_unlock_irq(&mapping->i_pages);
  422. return;
  423. }
  424. folio_clear_dirty(folio);
  425. }
  426. /**
  427. * nilfs_find_uncommitted_extent - find extent of uncommitted data
  428. * @inode: inode
  429. * @start_blk: start block offset (in)
  430. * @blkoff: start offset of the found extent (out)
  431. *
  432. * This function searches an extent of buffers marked "delayed" which
  433. * starts from a block offset equal to or larger than @start_blk. If
  434. * such an extent was found, this will store the start offset in
  435. * @blkoff and return its length in blocks.
  436. *
  437. * Return: Length in blocks of found extent, 0 otherwise.
  438. */
  439. unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
  440. sector_t start_blk,
  441. sector_t *blkoff)
  442. {
  443. unsigned int i, nr_folios;
  444. pgoff_t index;
  445. unsigned long length = 0;
  446. struct folio_batch fbatch;
  447. struct folio *folio;
  448. if (inode->i_mapping->nrpages == 0)
  449. return 0;
  450. index = start_blk >> (PAGE_SHIFT - inode->i_blkbits);
  451. folio_batch_init(&fbatch);
  452. repeat:
  453. nr_folios = filemap_get_folios_contig(inode->i_mapping, &index, ULONG_MAX,
  454. &fbatch);
  455. if (nr_folios == 0)
  456. return length;
  457. i = 0;
  458. do {
  459. folio = fbatch.folios[i];
  460. folio_lock(folio);
  461. if (folio_buffers(folio)) {
  462. struct buffer_head *bh, *head;
  463. sector_t b;
  464. b = folio->index << (PAGE_SHIFT - inode->i_blkbits);
  465. bh = head = folio_buffers(folio);
  466. do {
  467. if (b < start_blk)
  468. continue;
  469. if (buffer_delay(bh)) {
  470. if (length == 0)
  471. *blkoff = b;
  472. length++;
  473. } else if (length > 0) {
  474. goto out_locked;
  475. }
  476. } while (++b, bh = bh->b_this_page, bh != head);
  477. } else {
  478. if (length > 0)
  479. goto out_locked;
  480. }
  481. folio_unlock(folio);
  482. } while (++i < nr_folios);
  483. folio_batch_release(&fbatch);
  484. cond_resched();
  485. goto repeat;
  486. out_locked:
  487. folio_unlock(folio);
  488. folio_batch_release(&fbatch);
  489. return length;
  490. }