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 *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_local_page(spage);
  92. kaddr1 = kmap_local_page(dpage);
  93. memcpy(kaddr1 + bh_offset(dbh), kaddr0 + bh_offset(sbh), sbh->b_size);
  94. kunmap_local(kaddr1);
  95. kunmap_local(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_folio_buffers_clean - Check if a folio has dirty buffers or not.
  117. * @folio: Folio to be checked.
  118. *
  119. * nilfs_folio_buffers_clean() returns false if the folio has dirty buffers.
  120. * Otherwise, it returns true.
  121. */
  122. bool nilfs_folio_buffers_clean(struct folio *folio)
  123. {
  124. struct buffer_head *bh, *head;
  125. bh = head = folio_buffers(folio);
  126. do {
  127. if (buffer_dirty(bh))
  128. return false;
  129. bh = bh->b_this_page;
  130. } while (bh != head);
  131. return true;
  132. }
  133. void nilfs_folio_bug(struct folio *folio)
  134. {
  135. struct buffer_head *bh, *head;
  136. struct address_space *m;
  137. unsigned long ino;
  138. if (unlikely(!folio)) {
  139. printk(KERN_CRIT "NILFS_FOLIO_BUG(NULL)\n");
  140. return;
  141. }
  142. m = folio->mapping;
  143. ino = m ? m->host->i_ino : 0;
  144. printk(KERN_CRIT "NILFS_FOLIO_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
  145. "mapping=%p ino=%lu\n",
  146. folio, folio_ref_count(folio),
  147. (unsigned long long)folio->index, folio->flags, m, ino);
  148. head = folio_buffers(folio);
  149. if (head) {
  150. int i = 0;
  151. bh = head;
  152. do {
  153. printk(KERN_CRIT
  154. " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
  155. i++, bh, atomic_read(&bh->b_count),
  156. (unsigned long long)bh->b_blocknr, bh->b_state);
  157. bh = bh->b_this_page;
  158. } while (bh != head);
  159. }
  160. }
  161. /**
  162. * nilfs_copy_folio -- copy the folio with buffers
  163. * @dst: destination folio
  164. * @src: source folio
  165. * @copy_dirty: flag whether to copy dirty states on the folio's buffer heads.
  166. *
  167. * This function is for both data folios and btnode folios. The dirty flag
  168. * should be treated by caller. The folio must not be under i/o.
  169. * Both src and dst folio must be locked
  170. */
  171. static void nilfs_copy_folio(struct folio *dst, struct folio *src,
  172. bool copy_dirty)
  173. {
  174. struct buffer_head *dbh, *dbufs, *sbh;
  175. unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
  176. BUG_ON(folio_test_writeback(dst));
  177. sbh = folio_buffers(src);
  178. dbh = folio_buffers(dst);
  179. if (!dbh)
  180. dbh = create_empty_buffers(dst, sbh->b_size, 0);
  181. if (copy_dirty)
  182. mask |= BIT(BH_Dirty);
  183. dbufs = dbh;
  184. do {
  185. lock_buffer(sbh);
  186. lock_buffer(dbh);
  187. dbh->b_state = sbh->b_state & mask;
  188. dbh->b_blocknr = sbh->b_blocknr;
  189. dbh->b_bdev = sbh->b_bdev;
  190. sbh = sbh->b_this_page;
  191. dbh = dbh->b_this_page;
  192. } while (dbh != dbufs);
  193. folio_copy(dst, src);
  194. if (folio_test_uptodate(src) && !folio_test_uptodate(dst))
  195. folio_mark_uptodate(dst);
  196. else if (!folio_test_uptodate(src) && folio_test_uptodate(dst))
  197. folio_clear_uptodate(dst);
  198. if (folio_test_mappedtodisk(src) && !folio_test_mappedtodisk(dst))
  199. folio_set_mappedtodisk(dst);
  200. else if (!folio_test_mappedtodisk(src) && folio_test_mappedtodisk(dst))
  201. folio_clear_mappedtodisk(dst);
  202. do {
  203. unlock_buffer(sbh);
  204. unlock_buffer(dbh);
  205. sbh = sbh->b_this_page;
  206. dbh = dbh->b_this_page;
  207. } while (dbh != dbufs);
  208. }
  209. int nilfs_copy_dirty_pages(struct address_space *dmap,
  210. struct address_space *smap)
  211. {
  212. struct folio_batch fbatch;
  213. unsigned int i;
  214. pgoff_t index = 0;
  215. int err = 0;
  216. folio_batch_init(&fbatch);
  217. repeat:
  218. if (!filemap_get_folios_tag(smap, &index, (pgoff_t)-1,
  219. PAGECACHE_TAG_DIRTY, &fbatch))
  220. return 0;
  221. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  222. struct folio *folio = fbatch.folios[i], *dfolio;
  223. folio_lock(folio);
  224. if (unlikely(!folio_test_dirty(folio)))
  225. NILFS_FOLIO_BUG(folio, "inconsistent dirty state");
  226. dfolio = filemap_grab_folio(dmap, folio->index);
  227. if (IS_ERR(dfolio)) {
  228. /* No empty page is added to the page cache */
  229. folio_unlock(folio);
  230. err = PTR_ERR(dfolio);
  231. break;
  232. }
  233. if (unlikely(!folio_buffers(folio)))
  234. NILFS_FOLIO_BUG(folio,
  235. "found empty page in dat page cache");
  236. nilfs_copy_folio(dfolio, folio, true);
  237. filemap_dirty_folio(folio_mapping(dfolio), dfolio);
  238. folio_unlock(dfolio);
  239. folio_put(dfolio);
  240. folio_unlock(folio);
  241. }
  242. folio_batch_release(&fbatch);
  243. cond_resched();
  244. if (likely(!err))
  245. goto repeat;
  246. return err;
  247. }
  248. /**
  249. * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
  250. * @dmap: destination page cache
  251. * @smap: source page cache
  252. *
  253. * No pages must be added to the cache during this process.
  254. * This must be ensured by the caller.
  255. */
  256. void nilfs_copy_back_pages(struct address_space *dmap,
  257. struct address_space *smap)
  258. {
  259. struct folio_batch fbatch;
  260. unsigned int i, n;
  261. pgoff_t start = 0;
  262. folio_batch_init(&fbatch);
  263. repeat:
  264. n = filemap_get_folios(smap, &start, ~0UL, &fbatch);
  265. if (!n)
  266. return;
  267. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  268. struct folio *folio = fbatch.folios[i], *dfolio;
  269. pgoff_t index = folio->index;
  270. folio_lock(folio);
  271. dfolio = filemap_lock_folio(dmap, index);
  272. if (!IS_ERR(dfolio)) {
  273. /* overwrite existing folio in the destination cache */
  274. WARN_ON(folio_test_dirty(dfolio));
  275. nilfs_copy_folio(dfolio, folio, false);
  276. folio_unlock(dfolio);
  277. folio_put(dfolio);
  278. /* Do we not need to remove folio from smap here? */
  279. } else {
  280. struct folio *f;
  281. /* move the folio to the destination cache */
  282. xa_lock_irq(&smap->i_pages);
  283. f = __xa_erase(&smap->i_pages, index);
  284. WARN_ON(folio != f);
  285. smap->nrpages--;
  286. xa_unlock_irq(&smap->i_pages);
  287. xa_lock_irq(&dmap->i_pages);
  288. f = __xa_store(&dmap->i_pages, index, folio, GFP_NOFS);
  289. if (unlikely(f)) {
  290. /* Probably -ENOMEM */
  291. folio->mapping = NULL;
  292. folio_put(folio);
  293. } else {
  294. folio->mapping = dmap;
  295. dmap->nrpages++;
  296. if (folio_test_dirty(folio))
  297. __xa_set_mark(&dmap->i_pages, index,
  298. PAGECACHE_TAG_DIRTY);
  299. }
  300. xa_unlock_irq(&dmap->i_pages);
  301. }
  302. folio_unlock(folio);
  303. }
  304. folio_batch_release(&fbatch);
  305. cond_resched();
  306. goto repeat;
  307. }
  308. /**
  309. * nilfs_clear_dirty_pages - discard dirty pages in address space
  310. * @mapping: address space with dirty pages for discarding
  311. */
  312. void nilfs_clear_dirty_pages(struct address_space *mapping)
  313. {
  314. struct folio_batch fbatch;
  315. unsigned int i;
  316. pgoff_t index = 0;
  317. folio_batch_init(&fbatch);
  318. while (filemap_get_folios_tag(mapping, &index, (pgoff_t)-1,
  319. PAGECACHE_TAG_DIRTY, &fbatch)) {
  320. for (i = 0; i < folio_batch_count(&fbatch); i++) {
  321. struct folio *folio = fbatch.folios[i];
  322. folio_lock(folio);
  323. /*
  324. * This folio may have been removed from the address
  325. * space by truncation or invalidation when the lock
  326. * was acquired. Skip processing in that case.
  327. */
  328. if (likely(folio->mapping == mapping))
  329. nilfs_clear_folio_dirty(folio);
  330. folio_unlock(folio);
  331. }
  332. folio_batch_release(&fbatch);
  333. cond_resched();
  334. }
  335. }
  336. /**
  337. * nilfs_clear_folio_dirty - discard dirty folio
  338. * @folio: dirty folio that will be discarded
  339. *
  340. * nilfs_clear_folio_dirty() clears working states including dirty state for
  341. * the folio and its buffers. If the folio has buffers, clear only if it is
  342. * confirmed that none of the buffer heads are busy (none have valid
  343. * references and none are locked).
  344. */
  345. void nilfs_clear_folio_dirty(struct folio *folio)
  346. {
  347. struct buffer_head *bh, *head;
  348. BUG_ON(!folio_test_locked(folio));
  349. head = folio_buffers(folio);
  350. if (head) {
  351. const unsigned long clear_bits =
  352. (BIT(BH_Uptodate) | BIT(BH_Dirty) | BIT(BH_Mapped) |
  353. BIT(BH_Async_Write) | BIT(BH_NILFS_Volatile) |
  354. BIT(BH_NILFS_Checked) | BIT(BH_NILFS_Redirected) |
  355. BIT(BH_Delay));
  356. bool busy, invalidated = false;
  357. recheck_buffers:
  358. busy = false;
  359. bh = head;
  360. do {
  361. if (atomic_read(&bh->b_count) | buffer_locked(bh)) {
  362. busy = true;
  363. break;
  364. }
  365. } while (bh = bh->b_this_page, bh != head);
  366. if (busy) {
  367. if (invalidated)
  368. return;
  369. invalidate_bh_lrus();
  370. invalidated = true;
  371. goto recheck_buffers;
  372. }
  373. bh = head;
  374. do {
  375. lock_buffer(bh);
  376. set_mask_bits(&bh->b_state, clear_bits, 0);
  377. unlock_buffer(bh);
  378. } while (bh = bh->b_this_page, bh != head);
  379. }
  380. folio_clear_uptodate(folio);
  381. folio_clear_mappedtodisk(folio);
  382. folio_clear_checked(folio);
  383. __nilfs_clear_folio_dirty(folio);
  384. }
  385. unsigned int nilfs_page_count_clean_buffers(struct page *page,
  386. unsigned int from, unsigned int to)
  387. {
  388. unsigned int block_start, block_end;
  389. struct buffer_head *bh, *head;
  390. unsigned int nc = 0;
  391. for (bh = head = page_buffers(page), block_start = 0;
  392. bh != head || !block_start;
  393. block_start = block_end, bh = bh->b_this_page) {
  394. block_end = block_start + bh->b_size;
  395. if (block_end > from && block_start < to && !buffer_dirty(bh))
  396. nc++;
  397. }
  398. return nc;
  399. }
  400. /*
  401. * NILFS2 needs clear_page_dirty() in the following two cases:
  402. *
  403. * 1) For B-tree node pages and data pages of DAT file, NILFS2 clears dirty
  404. * flag of pages when it copies back pages from shadow cache to the
  405. * original cache.
  406. *
  407. * 2) Some B-tree operations like insertion or deletion may dispose buffers
  408. * in dirty state, and this needs to cancel the dirty state of their pages.
  409. */
  410. void __nilfs_clear_folio_dirty(struct folio *folio)
  411. {
  412. struct address_space *mapping = folio->mapping;
  413. if (mapping) {
  414. xa_lock_irq(&mapping->i_pages);
  415. if (folio_test_dirty(folio)) {
  416. __xa_clear_mark(&mapping->i_pages, folio->index,
  417. PAGECACHE_TAG_DIRTY);
  418. xa_unlock_irq(&mapping->i_pages);
  419. folio_clear_dirty_for_io(folio);
  420. return;
  421. }
  422. xa_unlock_irq(&mapping->i_pages);
  423. return;
  424. }
  425. folio_clear_dirty(folio);
  426. }
  427. /**
  428. * nilfs_find_uncommitted_extent - find extent of uncommitted data
  429. * @inode: inode
  430. * @start_blk: start block offset (in)
  431. * @blkoff: start offset of the found extent (out)
  432. *
  433. * This function searches an extent of buffers marked "delayed" which
  434. * starts from a block offset equal to or larger than @start_blk. If
  435. * such an extent was found, this will store the start offset in
  436. * @blkoff and return its length in blocks. Otherwise, zero is
  437. * returned.
  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. }