subpage.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef BTRFS_SUBPAGE_H
  3. #define BTRFS_SUBPAGE_H
  4. #include <linux/spinlock.h>
  5. #include <linux/atomic.h>
  6. #include <linux/sizes.h>
  7. #include "btrfs_inode.h"
  8. struct address_space;
  9. struct folio;
  10. /*
  11. * Extra info for subpage bitmap.
  12. *
  13. * For subpage we pack all uptodate/dirty/writeback/ordered bitmaps into
  14. * one larger bitmap.
  15. *
  16. * This structure records how they are organized in the bitmap:
  17. *
  18. * /- uptodate /- dirty /- ordered
  19. * | | |
  20. * v v v
  21. * |u|u|u|u|........|u|u|d|d|.......|d|d|o|o|.......|o|o|
  22. * |< sectors_per_page >|
  23. *
  24. * Unlike regular macro-like enums, here we do not go upper-case names, as
  25. * these names will be utilized in various macros to define function names.
  26. */
  27. enum {
  28. btrfs_bitmap_nr_uptodate = 0,
  29. btrfs_bitmap_nr_dirty,
  30. /*
  31. * This can be changed to atomic eventually. But this change will rely
  32. * on the async delalloc range rework for locked bitmap. As async
  33. * delalloc can unlock its range and mark blocks writeback at random
  34. * timing.
  35. */
  36. btrfs_bitmap_nr_writeback,
  37. /*
  38. * The ordered and checked flags are for COW fixup, already marked
  39. * deprecated, and will be removed eventually.
  40. */
  41. btrfs_bitmap_nr_ordered,
  42. btrfs_bitmap_nr_checked,
  43. /*
  44. * The locked bit is for async delalloc range (compression), currently
  45. * async extent is queued with the range locked, until the compression
  46. * is done.
  47. * So an async extent can unlock the range at any random timing.
  48. *
  49. * This will need a rework on the async extent lifespan (mark writeback
  50. * and do compression) before deprecating this flag.
  51. */
  52. btrfs_bitmap_nr_locked,
  53. btrfs_bitmap_nr_max
  54. };
  55. /*
  56. * Structure to trace status of each sector inside a page, attached to
  57. * page::private for both data and metadata inodes.
  58. */
  59. struct btrfs_folio_state {
  60. /* Common members for both data and metadata pages */
  61. spinlock_t lock;
  62. union {
  63. /*
  64. * Structures only used by metadata
  65. *
  66. * @eb_refs should only be operated under private_lock, as it
  67. * manages whether the btrfs_folio_state can be detached.
  68. */
  69. atomic_t eb_refs;
  70. /*
  71. * Structures only used by data,
  72. *
  73. * How many sectors inside the page is locked.
  74. */
  75. atomic_t nr_locked;
  76. };
  77. unsigned long bitmaps[];
  78. };
  79. enum btrfs_folio_type {
  80. BTRFS_SUBPAGE_METADATA,
  81. BTRFS_SUBPAGE_DATA,
  82. };
  83. /*
  84. * Subpage support for metadata is more complex, as we can have dummy extent
  85. * buffers, where folios have no mapping to determine the owning inode.
  86. *
  87. * Thankfully we only need to check if node size is smaller than page size.
  88. * Even with larger folio support, we will only allocate a folio as large as
  89. * node size.
  90. * Thus if nodesize < PAGE_SIZE, we know metadata needs need to subpage routine.
  91. */
  92. static inline bool btrfs_meta_is_subpage(const struct btrfs_fs_info *fs_info)
  93. {
  94. return fs_info->nodesize < PAGE_SIZE;
  95. }
  96. static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
  97. struct folio *folio)
  98. {
  99. if (folio->mapping && folio->mapping->host)
  100. ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
  101. return fs_info->sectorsize < folio_size(folio);
  102. }
  103. int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
  104. struct folio *folio, enum btrfs_folio_type type);
  105. void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
  106. enum btrfs_folio_type type);
  107. /* Allocate additional data where page represents more than one sector */
  108. struct btrfs_folio_state *btrfs_alloc_folio_state(const struct btrfs_fs_info *fs_info,
  109. size_t fsize, enum btrfs_folio_type type);
  110. static inline void btrfs_free_folio_state(struct btrfs_folio_state *bfs)
  111. {
  112. kfree(bfs);
  113. }
  114. void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
  115. void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
  116. void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
  117. struct folio *folio, u64 start, u32 len);
  118. void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
  119. struct folio *folio, u64 start, u32 len);
  120. void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
  121. struct folio *folio, unsigned long bitmap);
  122. /*
  123. * Template for subpage related operations.
  124. *
  125. * btrfs_subpage_*() are for call sites where the folio has subpage attached and
  126. * the range is ensured to be inside the folio's single page.
  127. *
  128. * btrfs_folio_*() are for call sites where the page can either be subpage
  129. * specific or regular folios. The function will handle both cases.
  130. * But the range still needs to be inside one single page.
  131. *
  132. * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
  133. * need to be inside the page. Those functions will truncate the range
  134. * automatically.
  135. *
  136. * Both btrfs_folio_*() and btrfs_folio_clamp_*() are for data folios.
  137. *
  138. * For metadata, one should use btrfs_meta_folio_*() helpers instead, and there
  139. * is no clamp version for metadata helpers, as we either go subpage
  140. * (nodesize < PAGE_SIZE) or go regular folio helpers (nodesize >= PAGE_SIZE,
  141. * and our folio is never larger than nodesize).
  142. */
  143. #define DECLARE_BTRFS_SUBPAGE_OPS(name) \
  144. void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
  145. struct folio *folio, u64 start, u32 len); \
  146. void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
  147. struct folio *folio, u64 start, u32 len); \
  148. bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
  149. struct folio *folio, u64 start, u32 len); \
  150. void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
  151. struct folio *folio, u64 start, u32 len); \
  152. void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
  153. struct folio *folio, u64 start, u32 len); \
  154. bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
  155. struct folio *folio, u64 start, u32 len); \
  156. void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
  157. struct folio *folio, u64 start, u32 len); \
  158. void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
  159. struct folio *folio, u64 start, u32 len); \
  160. bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
  161. struct folio *folio, u64 start, u32 len); \
  162. void btrfs_meta_folio_set_##name(struct folio *folio, const struct extent_buffer *eb); \
  163. void btrfs_meta_folio_clear_##name(struct folio *folio, const struct extent_buffer *eb); \
  164. bool btrfs_meta_folio_test_##name(struct folio *folio, const struct extent_buffer *eb);
  165. DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
  166. DECLARE_BTRFS_SUBPAGE_OPS(dirty);
  167. DECLARE_BTRFS_SUBPAGE_OPS(writeback);
  168. DECLARE_BTRFS_SUBPAGE_OPS(ordered);
  169. DECLARE_BTRFS_SUBPAGE_OPS(checked);
  170. /*
  171. * Helper for error cleanup, where a folio will have its dirty flag cleared,
  172. * with writeback started and finished.
  173. */
  174. static inline void btrfs_folio_clamp_finish_io(struct btrfs_fs_info *fs_info,
  175. struct folio *locked_folio,
  176. u64 start, u32 len)
  177. {
  178. btrfs_folio_clamp_clear_dirty(fs_info, locked_folio, start, len);
  179. btrfs_folio_clamp_set_writeback(fs_info, locked_folio, start, len);
  180. btrfs_folio_clamp_clear_writeback(fs_info, locked_folio, start, len);
  181. }
  182. bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
  183. struct folio *folio, u64 start, u32 len);
  184. void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
  185. struct folio *folio, u64 start, u32 len);
  186. bool btrfs_meta_folio_clear_and_test_dirty(struct folio *folio, const struct extent_buffer *eb);
  187. void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
  188. struct folio *folio,
  189. unsigned long *ret_bitmap);
  190. void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
  191. struct folio *folio, u64 start, u32 len);
  192. #endif