subpage.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/slab.h>
  3. #include "messages.h"
  4. #include "ctree.h"
  5. #include "subpage.h"
  6. #include "btrfs_inode.h"
  7. /*
  8. * Subpage (sectorsize < PAGE_SIZE) support overview:
  9. *
  10. * Limitations:
  11. *
  12. * - Only support 64K page size for now
  13. * This is to make metadata handling easier, as 64K page would ensure
  14. * all nodesize would fit inside one page, thus we don't need to handle
  15. * cases where a tree block crosses several pages.
  16. *
  17. * - Only metadata read-write for now
  18. * The data read-write part is in development.
  19. *
  20. * - Metadata can't cross 64K page boundary
  21. * btrfs-progs and kernel have done that for a while, thus only ancient
  22. * filesystems could have such problem. For such case, do a graceful
  23. * rejection.
  24. *
  25. * Special behavior:
  26. *
  27. * - Metadata
  28. * Metadata read is fully supported.
  29. * Meaning when reading one tree block will only trigger the read for the
  30. * needed range, other unrelated range in the same page will not be touched.
  31. *
  32. * Metadata write support is partial.
  33. * The writeback is still for the full page, but we will only submit
  34. * the dirty extent buffers in the page.
  35. *
  36. * This means, if we have a metadata page like this:
  37. *
  38. * Page offset
  39. * 0 16K 32K 48K 64K
  40. * |/////////| |///////////|
  41. * \- Tree block A \- Tree block B
  42. *
  43. * Even if we just want to writeback tree block A, we will also writeback
  44. * tree block B if it's also dirty.
  45. *
  46. * This may cause extra metadata writeback which results more COW.
  47. *
  48. * Implementation:
  49. *
  50. * - Common
  51. * Both metadata and data will use a new structure, btrfs_subpage, to
  52. * record the status of each sector inside a page. This provides the extra
  53. * granularity needed.
  54. *
  55. * - Metadata
  56. * Since we have multiple tree blocks inside one page, we can't rely on page
  57. * locking anymore, or we will have greatly reduced concurrency or even
  58. * deadlocks (hold one tree lock while trying to lock another tree lock in
  59. * the same page).
  60. *
  61. * Thus for metadata locking, subpage support relies on io_tree locking only.
  62. * This means a slightly higher tree locking latency.
  63. */
  64. #if PAGE_SIZE > SZ_4K
  65. bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping)
  66. {
  67. if (fs_info->sectorsize >= PAGE_SIZE)
  68. return false;
  69. /*
  70. * Only data pages (either through DIO or compression) can have no
  71. * mapping. And if page->mapping->host is data inode, it's subpage.
  72. * As we have ruled our sectorsize >= PAGE_SIZE case already.
  73. */
  74. if (!mapping || !mapping->host || is_data_inode(BTRFS_I(mapping->host)))
  75. return true;
  76. /*
  77. * Now the only remaining case is metadata, which we only go subpage
  78. * routine if nodesize < PAGE_SIZE.
  79. */
  80. if (fs_info->nodesize < PAGE_SIZE)
  81. return true;
  82. return false;
  83. }
  84. #endif
  85. int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
  86. struct folio *folio, enum btrfs_subpage_type type)
  87. {
  88. struct btrfs_subpage *subpage;
  89. /*
  90. * We have cases like a dummy extent buffer page, which is not mapped
  91. * and doesn't need to be locked.
  92. */
  93. if (folio->mapping)
  94. ASSERT(folio_test_locked(folio));
  95. /* Either not subpage, or the folio already has private attached. */
  96. if (!btrfs_is_subpage(fs_info, folio->mapping) || folio_test_private(folio))
  97. return 0;
  98. subpage = btrfs_alloc_subpage(fs_info, type);
  99. if (IS_ERR(subpage))
  100. return PTR_ERR(subpage);
  101. folio_attach_private(folio, subpage);
  102. return 0;
  103. }
  104. void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio)
  105. {
  106. struct btrfs_subpage *subpage;
  107. /* Either not subpage, or the folio already has private attached. */
  108. if (!btrfs_is_subpage(fs_info, folio->mapping) || !folio_test_private(folio))
  109. return;
  110. subpage = folio_detach_private(folio);
  111. ASSERT(subpage);
  112. btrfs_free_subpage(subpage);
  113. }
  114. struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
  115. enum btrfs_subpage_type type)
  116. {
  117. struct btrfs_subpage *ret;
  118. unsigned int real_size;
  119. ASSERT(fs_info->sectorsize < PAGE_SIZE);
  120. real_size = struct_size(ret, bitmaps,
  121. BITS_TO_LONGS(btrfs_bitmap_nr_max * fs_info->sectors_per_page));
  122. ret = kzalloc(real_size, GFP_NOFS);
  123. if (!ret)
  124. return ERR_PTR(-ENOMEM);
  125. spin_lock_init(&ret->lock);
  126. if (type == BTRFS_SUBPAGE_METADATA)
  127. atomic_set(&ret->eb_refs, 0);
  128. else
  129. atomic_set(&ret->nr_locked, 0);
  130. return ret;
  131. }
  132. void btrfs_free_subpage(struct btrfs_subpage *subpage)
  133. {
  134. kfree(subpage);
  135. }
  136. /*
  137. * Increase the eb_refs of current subpage.
  138. *
  139. * This is important for eb allocation, to prevent race with last eb freeing
  140. * of the same page.
  141. * With the eb_refs increased before the eb inserted into radix tree,
  142. * detach_extent_buffer_page() won't detach the folio private while we're still
  143. * allocating the extent buffer.
  144. */
  145. void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
  146. {
  147. struct btrfs_subpage *subpage;
  148. if (!btrfs_is_subpage(fs_info, folio->mapping))
  149. return;
  150. ASSERT(folio_test_private(folio) && folio->mapping);
  151. lockdep_assert_held(&folio->mapping->i_private_lock);
  152. subpage = folio_get_private(folio);
  153. atomic_inc(&subpage->eb_refs);
  154. }
  155. void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio)
  156. {
  157. struct btrfs_subpage *subpage;
  158. if (!btrfs_is_subpage(fs_info, folio->mapping))
  159. return;
  160. ASSERT(folio_test_private(folio) && folio->mapping);
  161. lockdep_assert_held(&folio->mapping->i_private_lock);
  162. subpage = folio_get_private(folio);
  163. ASSERT(atomic_read(&subpage->eb_refs));
  164. atomic_dec(&subpage->eb_refs);
  165. }
  166. static void btrfs_subpage_assert(const struct btrfs_fs_info *fs_info,
  167. struct folio *folio, u64 start, u32 len)
  168. {
  169. /* For subpage support, the folio must be single page. */
  170. ASSERT(folio_order(folio) == 0);
  171. /* Basic checks */
  172. ASSERT(folio_test_private(folio) && folio_get_private(folio));
  173. ASSERT(IS_ALIGNED(start, fs_info->sectorsize) &&
  174. IS_ALIGNED(len, fs_info->sectorsize));
  175. /*
  176. * The range check only works for mapped page, we can still have
  177. * unmapped page like dummy extent buffer pages.
  178. */
  179. if (folio->mapping)
  180. ASSERT(folio_pos(folio) <= start &&
  181. start + len <= folio_pos(folio) + PAGE_SIZE);
  182. }
  183. #define subpage_calc_start_bit(fs_info, folio, name, start, len) \
  184. ({ \
  185. unsigned int __start_bit; \
  186. \
  187. btrfs_subpage_assert(fs_info, folio, start, len); \
  188. __start_bit = offset_in_page(start) >> fs_info->sectorsize_bits; \
  189. __start_bit += fs_info->sectors_per_page * btrfs_bitmap_nr_##name; \
  190. __start_bit; \
  191. })
  192. static void btrfs_subpage_clamp_range(struct folio *folio, u64 *start, u32 *len)
  193. {
  194. u64 orig_start = *start;
  195. u32 orig_len = *len;
  196. *start = max_t(u64, folio_pos(folio), orig_start);
  197. /*
  198. * For certain call sites like btrfs_drop_pages(), we may have pages
  199. * beyond the target range. In that case, just set @len to 0, subpage
  200. * helpers can handle @len == 0 without any problem.
  201. */
  202. if (folio_pos(folio) >= orig_start + orig_len)
  203. *len = 0;
  204. else
  205. *len = min_t(u64, folio_pos(folio) + PAGE_SIZE,
  206. orig_start + orig_len) - *start;
  207. }
  208. static bool btrfs_subpage_end_and_test_lock(const struct btrfs_fs_info *fs_info,
  209. struct folio *folio, u64 start, u32 len)
  210. {
  211. struct btrfs_subpage *subpage = folio_get_private(folio);
  212. const int start_bit = subpage_calc_start_bit(fs_info, folio, locked, start, len);
  213. const int nbits = (len >> fs_info->sectorsize_bits);
  214. unsigned long flags;
  215. unsigned int cleared = 0;
  216. int bit = start_bit;
  217. bool last;
  218. btrfs_subpage_assert(fs_info, folio, start, len);
  219. spin_lock_irqsave(&subpage->lock, flags);
  220. /*
  221. * We have call sites passing @lock_page into
  222. * extent_clear_unlock_delalloc() for compression path.
  223. *
  224. * This @locked_page is locked by plain lock_page(), thus its
  225. * subpage::locked is 0. Handle them in a special way.
  226. */
  227. if (atomic_read(&subpage->nr_locked) == 0) {
  228. spin_unlock_irqrestore(&subpage->lock, flags);
  229. return true;
  230. }
  231. for_each_set_bit_from(bit, subpage->bitmaps, start_bit + nbits) {
  232. clear_bit(bit, subpage->bitmaps);
  233. cleared++;
  234. }
  235. ASSERT(atomic_read(&subpage->nr_locked) >= cleared);
  236. last = atomic_sub_and_test(cleared, &subpage->nr_locked);
  237. spin_unlock_irqrestore(&subpage->lock, flags);
  238. return last;
  239. }
  240. /*
  241. * Handle different locked folios:
  242. *
  243. * - Non-subpage folio
  244. * Just unlock it.
  245. *
  246. * - folio locked but without any subpage locked
  247. * This happens either before writepage_delalloc() or the delalloc range is
  248. * already handled by previous folio.
  249. * We can simple unlock it.
  250. *
  251. * - folio locked with subpage range locked.
  252. * We go through the locked sectors inside the range and clear their locked
  253. * bitmap, reduce the writer lock number, and unlock the page if that's
  254. * the last locked range.
  255. */
  256. void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
  257. struct folio *folio, u64 start, u32 len)
  258. {
  259. struct btrfs_subpage *subpage = folio_get_private(folio);
  260. ASSERT(folio_test_locked(folio));
  261. if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
  262. folio_unlock(folio);
  263. return;
  264. }
  265. /*
  266. * For subpage case, there are two types of locked page. With or
  267. * without locked number.
  268. *
  269. * Since we own the page lock, no one else could touch subpage::locked
  270. * and we are safe to do several atomic operations without spinlock.
  271. */
  272. if (atomic_read(&subpage->nr_locked) == 0) {
  273. /* No subpage lock, locked by plain lock_page(). */
  274. folio_unlock(folio);
  275. return;
  276. }
  277. btrfs_subpage_clamp_range(folio, &start, &len);
  278. if (btrfs_subpage_end_and_test_lock(fs_info, folio, start, len))
  279. folio_unlock(folio);
  280. }
  281. void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
  282. struct folio *folio, unsigned long bitmap)
  283. {
  284. struct btrfs_subpage *subpage = folio_get_private(folio);
  285. const int start_bit = fs_info->sectors_per_page * btrfs_bitmap_nr_locked;
  286. unsigned long flags;
  287. bool last = false;
  288. int cleared = 0;
  289. int bit;
  290. if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping)) {
  291. folio_unlock(folio);
  292. return;
  293. }
  294. if (atomic_read(&subpage->nr_locked) == 0) {
  295. /* No subpage lock, locked by plain lock_page(). */
  296. folio_unlock(folio);
  297. return;
  298. }
  299. spin_lock_irqsave(&subpage->lock, flags);
  300. for_each_set_bit(bit, &bitmap, fs_info->sectors_per_page) {
  301. if (test_and_clear_bit(bit + start_bit, subpage->bitmaps))
  302. cleared++;
  303. }
  304. ASSERT(atomic_read(&subpage->nr_locked) >= cleared);
  305. last = atomic_sub_and_test(cleared, &subpage->nr_locked);
  306. spin_unlock_irqrestore(&subpage->lock, flags);
  307. if (last)
  308. folio_unlock(folio);
  309. }
  310. #define subpage_test_bitmap_all_set(fs_info, subpage, name) \
  311. bitmap_test_range_all_set(subpage->bitmaps, \
  312. fs_info->sectors_per_page * btrfs_bitmap_nr_##name, \
  313. fs_info->sectors_per_page)
  314. #define subpage_test_bitmap_all_zero(fs_info, subpage, name) \
  315. bitmap_test_range_all_zero(subpage->bitmaps, \
  316. fs_info->sectors_per_page * btrfs_bitmap_nr_##name, \
  317. fs_info->sectors_per_page)
  318. void btrfs_subpage_set_uptodate(const struct btrfs_fs_info *fs_info,
  319. struct folio *folio, u64 start, u32 len)
  320. {
  321. struct btrfs_subpage *subpage = folio_get_private(folio);
  322. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  323. uptodate, start, len);
  324. unsigned long flags;
  325. spin_lock_irqsave(&subpage->lock, flags);
  326. bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  327. if (subpage_test_bitmap_all_set(fs_info, subpage, uptodate))
  328. folio_mark_uptodate(folio);
  329. spin_unlock_irqrestore(&subpage->lock, flags);
  330. }
  331. void btrfs_subpage_clear_uptodate(const struct btrfs_fs_info *fs_info,
  332. struct folio *folio, u64 start, u32 len)
  333. {
  334. struct btrfs_subpage *subpage = folio_get_private(folio);
  335. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  336. uptodate, start, len);
  337. unsigned long flags;
  338. spin_lock_irqsave(&subpage->lock, flags);
  339. bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  340. folio_clear_uptodate(folio);
  341. spin_unlock_irqrestore(&subpage->lock, flags);
  342. }
  343. void btrfs_subpage_set_dirty(const struct btrfs_fs_info *fs_info,
  344. struct folio *folio, u64 start, u32 len)
  345. {
  346. struct btrfs_subpage *subpage = folio_get_private(folio);
  347. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  348. dirty, start, len);
  349. unsigned long flags;
  350. spin_lock_irqsave(&subpage->lock, flags);
  351. bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  352. spin_unlock_irqrestore(&subpage->lock, flags);
  353. folio_mark_dirty(folio);
  354. }
  355. /*
  356. * Extra clear_and_test function for subpage dirty bitmap.
  357. *
  358. * Return true if we're the last bits in the dirty_bitmap and clear the
  359. * dirty_bitmap.
  360. * Return false otherwise.
  361. *
  362. * NOTE: Callers should manually clear page dirty for true case, as we have
  363. * extra handling for tree blocks.
  364. */
  365. bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
  366. struct folio *folio, u64 start, u32 len)
  367. {
  368. struct btrfs_subpage *subpage = folio_get_private(folio);
  369. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  370. dirty, start, len);
  371. unsigned long flags;
  372. bool last = false;
  373. spin_lock_irqsave(&subpage->lock, flags);
  374. bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  375. if (subpage_test_bitmap_all_zero(fs_info, subpage, dirty))
  376. last = true;
  377. spin_unlock_irqrestore(&subpage->lock, flags);
  378. return last;
  379. }
  380. void btrfs_subpage_clear_dirty(const struct btrfs_fs_info *fs_info,
  381. struct folio *folio, u64 start, u32 len)
  382. {
  383. bool last;
  384. last = btrfs_subpage_clear_and_test_dirty(fs_info, folio, start, len);
  385. if (last)
  386. folio_clear_dirty_for_io(folio);
  387. }
  388. void btrfs_subpage_set_writeback(const struct btrfs_fs_info *fs_info,
  389. struct folio *folio, u64 start, u32 len)
  390. {
  391. struct btrfs_subpage *subpage = folio_get_private(folio);
  392. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  393. writeback, start, len);
  394. unsigned long flags;
  395. spin_lock_irqsave(&subpage->lock, flags);
  396. bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  397. /*
  398. * Don't clear the TOWRITE tag when starting writeback on a still-dirty
  399. * folio. Doing so can cause WB_SYNC_ALL writepages() to overlook it,
  400. * assume writeback is complete, and exit too early — violating sync
  401. * ordering guarantees.
  402. */
  403. if (!folio_test_writeback(folio))
  404. __folio_start_writeback(folio, true);
  405. if (!folio_test_dirty(folio)) {
  406. struct address_space *mapping = folio_mapping(folio);
  407. XA_STATE(xas, &mapping->i_pages, folio->index);
  408. unsigned long flags;
  409. xas_lock_irqsave(&xas, flags);
  410. xas_load(&xas);
  411. xas_clear_mark(&xas, PAGECACHE_TAG_TOWRITE);
  412. xas_unlock_irqrestore(&xas, flags);
  413. }
  414. spin_unlock_irqrestore(&subpage->lock, flags);
  415. }
  416. void btrfs_subpage_clear_writeback(const struct btrfs_fs_info *fs_info,
  417. struct folio *folio, u64 start, u32 len)
  418. {
  419. struct btrfs_subpage *subpage = folio_get_private(folio);
  420. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  421. writeback, start, len);
  422. unsigned long flags;
  423. spin_lock_irqsave(&subpage->lock, flags);
  424. bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  425. if (subpage_test_bitmap_all_zero(fs_info, subpage, writeback)) {
  426. ASSERT(folio_test_writeback(folio));
  427. folio_end_writeback(folio);
  428. }
  429. spin_unlock_irqrestore(&subpage->lock, flags);
  430. }
  431. void btrfs_subpage_set_ordered(const struct btrfs_fs_info *fs_info,
  432. struct folio *folio, u64 start, u32 len)
  433. {
  434. struct btrfs_subpage *subpage = folio_get_private(folio);
  435. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  436. ordered, start, len);
  437. unsigned long flags;
  438. spin_lock_irqsave(&subpage->lock, flags);
  439. bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  440. folio_set_ordered(folio);
  441. spin_unlock_irqrestore(&subpage->lock, flags);
  442. }
  443. void btrfs_subpage_clear_ordered(const struct btrfs_fs_info *fs_info,
  444. struct folio *folio, u64 start, u32 len)
  445. {
  446. struct btrfs_subpage *subpage = folio_get_private(folio);
  447. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  448. ordered, start, len);
  449. unsigned long flags;
  450. spin_lock_irqsave(&subpage->lock, flags);
  451. bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  452. if (subpage_test_bitmap_all_zero(fs_info, subpage, ordered))
  453. folio_clear_ordered(folio);
  454. spin_unlock_irqrestore(&subpage->lock, flags);
  455. }
  456. void btrfs_subpage_set_checked(const struct btrfs_fs_info *fs_info,
  457. struct folio *folio, u64 start, u32 len)
  458. {
  459. struct btrfs_subpage *subpage = folio_get_private(folio);
  460. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  461. checked, start, len);
  462. unsigned long flags;
  463. spin_lock_irqsave(&subpage->lock, flags);
  464. bitmap_set(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  465. if (subpage_test_bitmap_all_set(fs_info, subpage, checked))
  466. folio_set_checked(folio);
  467. spin_unlock_irqrestore(&subpage->lock, flags);
  468. }
  469. void btrfs_subpage_clear_checked(const struct btrfs_fs_info *fs_info,
  470. struct folio *folio, u64 start, u32 len)
  471. {
  472. struct btrfs_subpage *subpage = folio_get_private(folio);
  473. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio,
  474. checked, start, len);
  475. unsigned long flags;
  476. spin_lock_irqsave(&subpage->lock, flags);
  477. bitmap_clear(subpage->bitmaps, start_bit, len >> fs_info->sectorsize_bits);
  478. folio_clear_checked(folio);
  479. spin_unlock_irqrestore(&subpage->lock, flags);
  480. }
  481. /*
  482. * Unlike set/clear which is dependent on each page status, for test all bits
  483. * are tested in the same way.
  484. */
  485. #define IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(name) \
  486. bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
  487. struct folio *folio, u64 start, u32 len) \
  488. { \
  489. struct btrfs_subpage *subpage = folio_get_private(folio); \
  490. unsigned int start_bit = subpage_calc_start_bit(fs_info, folio, \
  491. name, start, len); \
  492. unsigned long flags; \
  493. bool ret; \
  494. \
  495. spin_lock_irqsave(&subpage->lock, flags); \
  496. ret = bitmap_test_range_all_set(subpage->bitmaps, start_bit, \
  497. len >> fs_info->sectorsize_bits); \
  498. spin_unlock_irqrestore(&subpage->lock, flags); \
  499. return ret; \
  500. }
  501. IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(uptodate);
  502. IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(dirty);
  503. IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(writeback);
  504. IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(ordered);
  505. IMPLEMENT_BTRFS_SUBPAGE_TEST_OP(checked);
  506. /*
  507. * Note that, in selftests (extent-io-tests), we can have empty fs_info passed
  508. * in. We only test sectorsize == PAGE_SIZE cases so far, thus we can fall
  509. * back to regular sectorsize branch.
  510. */
  511. #define IMPLEMENT_BTRFS_PAGE_OPS(name, folio_set_func, \
  512. folio_clear_func, folio_test_func) \
  513. void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
  514. struct folio *folio, u64 start, u32 len) \
  515. { \
  516. if (unlikely(!fs_info) || \
  517. !btrfs_is_subpage(fs_info, folio->mapping)) { \
  518. folio_set_func(folio); \
  519. return; \
  520. } \
  521. btrfs_subpage_set_##name(fs_info, folio, start, len); \
  522. } \
  523. void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
  524. struct folio *folio, u64 start, u32 len) \
  525. { \
  526. if (unlikely(!fs_info) || \
  527. !btrfs_is_subpage(fs_info, folio->mapping)) { \
  528. folio_clear_func(folio); \
  529. return; \
  530. } \
  531. btrfs_subpage_clear_##name(fs_info, folio, start, len); \
  532. } \
  533. bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
  534. struct folio *folio, u64 start, u32 len) \
  535. { \
  536. if (unlikely(!fs_info) || \
  537. !btrfs_is_subpage(fs_info, folio->mapping)) \
  538. return folio_test_func(folio); \
  539. return btrfs_subpage_test_##name(fs_info, folio, start, len); \
  540. } \
  541. void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
  542. struct folio *folio, u64 start, u32 len) \
  543. { \
  544. if (unlikely(!fs_info) || \
  545. !btrfs_is_subpage(fs_info, folio->mapping)) { \
  546. folio_set_func(folio); \
  547. return; \
  548. } \
  549. btrfs_subpage_clamp_range(folio, &start, &len); \
  550. btrfs_subpage_set_##name(fs_info, folio, start, len); \
  551. } \
  552. void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
  553. struct folio *folio, u64 start, u32 len) \
  554. { \
  555. if (unlikely(!fs_info) || \
  556. !btrfs_is_subpage(fs_info, folio->mapping)) { \
  557. folio_clear_func(folio); \
  558. return; \
  559. } \
  560. btrfs_subpage_clamp_range(folio, &start, &len); \
  561. btrfs_subpage_clear_##name(fs_info, folio, start, len); \
  562. } \
  563. bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
  564. struct folio *folio, u64 start, u32 len) \
  565. { \
  566. if (unlikely(!fs_info) || \
  567. !btrfs_is_subpage(fs_info, folio->mapping)) \
  568. return folio_test_func(folio); \
  569. btrfs_subpage_clamp_range(folio, &start, &len); \
  570. return btrfs_subpage_test_##name(fs_info, folio, start, len); \
  571. }
  572. IMPLEMENT_BTRFS_PAGE_OPS(uptodate, folio_mark_uptodate, folio_clear_uptodate,
  573. folio_test_uptodate);
  574. IMPLEMENT_BTRFS_PAGE_OPS(dirty, folio_mark_dirty, folio_clear_dirty_for_io,
  575. folio_test_dirty);
  576. IMPLEMENT_BTRFS_PAGE_OPS(writeback, folio_start_writeback, folio_end_writeback,
  577. folio_test_writeback);
  578. IMPLEMENT_BTRFS_PAGE_OPS(ordered, folio_set_ordered, folio_clear_ordered,
  579. folio_test_ordered);
  580. IMPLEMENT_BTRFS_PAGE_OPS(checked, folio_set_checked, folio_clear_checked,
  581. folio_test_checked);
  582. /*
  583. * Make sure not only the page dirty bit is cleared, but also subpage dirty bit
  584. * is cleared.
  585. */
  586. void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
  587. struct folio *folio, u64 start, u32 len)
  588. {
  589. struct btrfs_subpage *subpage;
  590. unsigned int start_bit;
  591. unsigned int nbits;
  592. unsigned long flags;
  593. if (!IS_ENABLED(CONFIG_BTRFS_ASSERT))
  594. return;
  595. if (!btrfs_is_subpage(fs_info, folio->mapping)) {
  596. ASSERT(!folio_test_dirty(folio));
  597. return;
  598. }
  599. start_bit = subpage_calc_start_bit(fs_info, folio, dirty, start, len);
  600. nbits = len >> fs_info->sectorsize_bits;
  601. subpage = folio_get_private(folio);
  602. ASSERT(subpage);
  603. spin_lock_irqsave(&subpage->lock, flags);
  604. ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits));
  605. spin_unlock_irqrestore(&subpage->lock, flags);
  606. }
  607. /*
  608. * This is for folio already locked by plain lock_page()/folio_lock(), which
  609. * doesn't have any subpage awareness.
  610. *
  611. * This populates the involved subpage ranges so that subpage helpers can
  612. * properly unlock them.
  613. */
  614. void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
  615. struct folio *folio, u64 start, u32 len)
  616. {
  617. struct btrfs_subpage *subpage;
  618. unsigned long flags;
  619. unsigned int start_bit;
  620. unsigned int nbits;
  621. int ret;
  622. ASSERT(folio_test_locked(folio));
  623. if (unlikely(!fs_info) || !btrfs_is_subpage(fs_info, folio->mapping))
  624. return;
  625. subpage = folio_get_private(folio);
  626. start_bit = subpage_calc_start_bit(fs_info, folio, locked, start, len);
  627. nbits = len >> fs_info->sectorsize_bits;
  628. spin_lock_irqsave(&subpage->lock, flags);
  629. /* Target range should not yet be locked. */
  630. ASSERT(bitmap_test_range_all_zero(subpage->bitmaps, start_bit, nbits));
  631. bitmap_set(subpage->bitmaps, start_bit, nbits);
  632. ret = atomic_add_return(nbits, &subpage->nr_locked);
  633. ASSERT(ret <= fs_info->sectors_per_page);
  634. spin_unlock_irqrestore(&subpage->lock, flags);
  635. }
  636. #define GET_SUBPAGE_BITMAP(subpage, fs_info, name, dst) \
  637. { \
  638. const int sectors_per_page = fs_info->sectors_per_page; \
  639. \
  640. ASSERT(sectors_per_page < BITS_PER_LONG); \
  641. *dst = bitmap_read(subpage->bitmaps, \
  642. sectors_per_page * btrfs_bitmap_nr_##name, \
  643. sectors_per_page); \
  644. }
  645. void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
  646. struct folio *folio, u64 start, u32 len)
  647. {
  648. struct btrfs_subpage *subpage;
  649. const u32 sectors_per_page = fs_info->sectors_per_page;
  650. unsigned long uptodate_bitmap;
  651. unsigned long dirty_bitmap;
  652. unsigned long writeback_bitmap;
  653. unsigned long ordered_bitmap;
  654. unsigned long checked_bitmap;
  655. unsigned long locked_bitmap;
  656. unsigned long flags;
  657. ASSERT(folio_test_private(folio) && folio_get_private(folio));
  658. ASSERT(sectors_per_page > 1);
  659. subpage = folio_get_private(folio);
  660. spin_lock_irqsave(&subpage->lock, flags);
  661. GET_SUBPAGE_BITMAP(subpage, fs_info, uptodate, &uptodate_bitmap);
  662. GET_SUBPAGE_BITMAP(subpage, fs_info, dirty, &dirty_bitmap);
  663. GET_SUBPAGE_BITMAP(subpage, fs_info, writeback, &writeback_bitmap);
  664. GET_SUBPAGE_BITMAP(subpage, fs_info, ordered, &ordered_bitmap);
  665. GET_SUBPAGE_BITMAP(subpage, fs_info, checked, &checked_bitmap);
  666. GET_SUBPAGE_BITMAP(subpage, fs_info, locked, &locked_bitmap);
  667. spin_unlock_irqrestore(&subpage->lock, flags);
  668. dump_page(folio_page(folio, 0), "btrfs subpage dump");
  669. btrfs_warn(fs_info,
  670. "start=%llu len=%u page=%llu, bitmaps uptodate=%*pbl dirty=%*pbl locked=%*pbl writeback=%*pbl ordered=%*pbl checked=%*pbl",
  671. start, len, folio_pos(folio),
  672. sectors_per_page, &uptodate_bitmap,
  673. sectors_per_page, &dirty_bitmap,
  674. sectors_per_page, &locked_bitmap,
  675. sectors_per_page, &writeback_bitmap,
  676. sectors_per_page, &ordered_bitmap,
  677. sectors_per_page, &checked_bitmap);
  678. }
  679. void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
  680. struct folio *folio,
  681. unsigned long *ret_bitmap)
  682. {
  683. struct btrfs_subpage *subpage;
  684. unsigned long flags;
  685. ASSERT(folio_test_private(folio) && folio_get_private(folio));
  686. ASSERT(fs_info->sectors_per_page > 1);
  687. subpage = folio_get_private(folio);
  688. spin_lock_irqsave(&subpage->lock, flags);
  689. GET_SUBPAGE_BITMAP(subpage, fs_info, dirty, ret_bitmap);
  690. spin_unlock_irqrestore(&subpage->lock, flags);
  691. }