1 /* SPDX-License-Identifier: GPL-2.0 */
2
3 #ifndef BTRFS_SUBPAGE_H
4 #define BTRFS_SUBPAGE_H
5
6 #include <linux/spinlock.h>
7 #include <linux/atomic.h>
8 #include <linux/sizes.h>
9
10 struct address_space;
11 struct folio;
12 struct btrfs_fs_info;
13
14 /*
15 * Extra info for subpapge bitmap.
16 *
17 * For subpage we pack all uptodate/dirty/writeback/ordered bitmaps into
18 * one larger bitmap.
19 *
20 * This structure records how they are organized in the bitmap:
21 *
22 * /- uptodate /- dirty /- ordered
23 * | | |
24 * v v v
25 * |u|u|u|u|........|u|u|d|d|.......|d|d|o|o|.......|o|o|
26 * |< sectors_per_page >|
27 *
28 * Unlike regular macro-like enums, here we do not go upper-case names, as
29 * these names will be utilized in various macros to define function names.
30 */
31 enum {
32 btrfs_bitmap_nr_uptodate = 0,
33 btrfs_bitmap_nr_dirty,
34 btrfs_bitmap_nr_writeback,
35 btrfs_bitmap_nr_ordered,
36 btrfs_bitmap_nr_checked,
37 btrfs_bitmap_nr_locked,
38 btrfs_bitmap_nr_max
39 };
40
41 /*
42 * Structure to trace status of each sector inside a page, attached to
43 * page::private for both data and metadata inodes.
44 */
45 struct btrfs_subpage {
46 /* Common members for both data and metadata pages */
47 spinlock_t lock;
48 /*
49 * Both data and metadata needs to track how many readers are for the
50 * page.
51 * Data relies on @readers to unlock the page when last reader finished.
52 * While metadata doesn't need page unlock, it needs to prevent
53 * page::private get cleared before the last end_page_read().
54 */
55 atomic_t readers;
56 union {
57 /*
58 * Structures only used by metadata
59 *
60 * @eb_refs should only be operated under private_lock, as it
61 * manages whether the subpage can be detached.
62 */
63 atomic_t eb_refs;
64
65 /* Structures only used by data */
66 atomic_t writers;
67 };
68 unsigned long bitmaps[];
69 };
70
71 enum btrfs_subpage_type {
72 BTRFS_SUBPAGE_METADATA,
73 BTRFS_SUBPAGE_DATA,
74 };
75
76 #if PAGE_SIZE > SZ_4K
77 bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
78 #else
btrfs_is_subpage(const struct btrfs_fs_info * fs_info,struct address_space * mapping)79 static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
80 struct address_space *mapping)
81 {
82 return false;
83 }
84 #endif
85
86 int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
87 struct folio *folio, enum btrfs_subpage_type type);
88 void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio);
89
90 /* Allocate additional data where page represents more than one sector */
91 struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
92 enum btrfs_subpage_type type);
93 void btrfs_free_subpage(struct btrfs_subpage *subpage);
94
95 void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
96 void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
97
98 void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
99 struct folio *folio, u64 start, u32 len);
100 void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
101 struct folio *folio, u64 start, u32 len);
102
103 int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,
104 struct folio *folio, u64 start, u32 len);
105 void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,
106 struct folio *folio, u64 start, u32 len);
107 void btrfs_folio_set_writer_lock(const struct btrfs_fs_info *fs_info,
108 struct folio *folio, u64 start, u32 len);
109 void btrfs_folio_end_writer_lock_bitmap(const struct btrfs_fs_info *fs_info,
110 struct folio *folio, unsigned long bitmap);
111 bool btrfs_subpage_find_writer_locked(const struct btrfs_fs_info *fs_info,
112 struct folio *folio, u64 search_start,
113 u64 *found_start_ret, u32 *found_len_ret);
114
115 /*
116 * Template for subpage related operations.
117 *
118 * btrfs_subpage_*() are for call sites where the folio has subpage attached and
119 * the range is ensured to be inside the folio's single page.
120 *
121 * btrfs_folio_*() are for call sites where the page can either be subpage
122 * specific or regular folios. The function will handle both cases.
123 * But the range still needs to be inside one single page.
124 *
125 * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
126 * need to be inside the page. Those functions will truncate the range
127 * automatically.
128 */
129 #define DECLARE_BTRFS_SUBPAGE_OPS(name) \
130 void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
131 struct folio *folio, u64 start, u32 len); \
132 void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
133 struct folio *folio, u64 start, u32 len); \
134 bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
135 struct folio *folio, u64 start, u32 len); \
136 void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
137 struct folio *folio, u64 start, u32 len); \
138 void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
139 struct folio *folio, u64 start, u32 len); \
140 bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
141 struct folio *folio, u64 start, u32 len); \
142 void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
143 struct folio *folio, u64 start, u32 len); \
144 void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
145 struct folio *folio, u64 start, u32 len); \
146 bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
147 struct folio *folio, u64 start, u32 len);
148
149 DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
150 DECLARE_BTRFS_SUBPAGE_OPS(dirty);
151 DECLARE_BTRFS_SUBPAGE_OPS(writeback);
152 DECLARE_BTRFS_SUBPAGE_OPS(ordered);
153 DECLARE_BTRFS_SUBPAGE_OPS(checked);
154
155 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
156 struct folio *folio, u64 start, u32 len);
157
158 void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
159 struct folio *folio, u64 start, u32 len);
160 void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
161 struct folio *folio,
162 unsigned long *ret_bitmap);
163 void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
164 struct folio *folio, u64 start, u32 len);
165
166 #endif
167