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 #include "btrfs_inode.h"
10
11 struct address_space;
12 struct folio;
13
14 /*
15 * Extra info for subpage bitmap.
16 *
17 * For subpage we pack all uptodate/dirty/writeback/fixup bitmaps into
18 * one larger bitmap.
19 *
20 * This structure records how they are organized in the bitmap:
21 *
22 * /- uptodate /- dirty /- writeback /- fixup
23 * | | | |
24 * v v v v
25 * |u|u|u|u|........|u|u|d|d|.......|d|d|w|w|.....|w|w|f|f|.....|f|f|
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
35 /*
36 * This can be changed to atomic eventually. But this change will rely
37 * on the async delalloc range rework for locked bitmap. As async
38 * delalloc can unlock its range and mark blocks writeback at random
39 * timing.
40 */
41 btrfs_bitmap_nr_writeback,
42
43 /*
44 * Blocks dirtied by the dirty_folio callback instead of a reserving
45 * write path (e.g. set_page_dirty_lock() on a GUP pin). They have
46 * no space reservation and need the writepage fixup before they can
47 * be submitted.
48 */
49 btrfs_bitmap_nr_fixup,
50
51 btrfs_bitmap_nr_max
52 };
53
54 /*
55 * Structure to trace status of each sector inside a page, attached to
56 * page::private for both data and metadata inodes.
57 */
58 struct btrfs_folio_state {
59 /* Common members for both data and metadata pages */
60 spinlock_t lock;
61 union {
62 /*
63 * Structures only used by metadata
64 *
65 * @eb_refs should only be operated under private_lock, as it
66 * manages whether the btrfs_folio_state can be detached.
67 */
68 atomic_t eb_refs;
69
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
80 enum btrfs_folio_type {
81 BTRFS_SUBPAGE_METADATA,
82 BTRFS_SUBPAGE_DATA,
83 };
84
85 /*
86 * Subpage support for metadata is more complex, as we can have dummy extent
87 * buffers, where folios have no mapping to determine the owning inode.
88 *
89 * Thankfully we only need to check if node size is smaller than page size.
90 * Even with larger folio support, we will only allocate a folio as large as
91 * node size.
92 * Thus if nodesize < PAGE_SIZE, we know metadata needs need to subpage routine.
93 */
btrfs_meta_is_subpage(const struct btrfs_fs_info * fs_info)94 static inline bool btrfs_meta_is_subpage(const struct btrfs_fs_info *fs_info)
95 {
96 return fs_info->nodesize < PAGE_SIZE;
97 }
btrfs_is_subpage(const struct btrfs_fs_info * fs_info,struct folio * folio)98 static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,
99 struct folio *folio)
100 {
101 if (folio->mapping && folio->mapping->host)
102 ASSERT(is_data_inode(BTRFS_I(folio->mapping->host)));
103 return fs_info->sectorsize < folio_size(folio);
104 }
105
106 int btrfs_attach_folio_state(const struct btrfs_fs_info *fs_info,
107 struct folio *folio, enum btrfs_folio_type type);
108 void btrfs_detach_folio_state(const struct btrfs_fs_info *fs_info, struct folio *folio,
109 enum btrfs_folio_type type);
110
111 /* Allocate additional data where page represents more than one sector */
112 struct btrfs_folio_state *btrfs_alloc_folio_state(const struct btrfs_fs_info *fs_info,
113 size_t fsize, enum btrfs_folio_type type,
114 gfp_t gfp);
btrfs_free_folio_state(struct btrfs_folio_state * bfs)115 static inline void btrfs_free_folio_state(struct btrfs_folio_state *bfs)
116 {
117 kfree(bfs);
118 }
119
120 void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
121 void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
122
123 void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
124 struct folio *folio, u64 start, u32 len);
125 void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
126 struct folio *folio, u64 start, u32 len);
127 void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info *fs_info,
128 struct folio *folio, unsigned long *bitmap);
129 /*
130 * Template for subpage related operations.
131 *
132 * btrfs_subpage_*() are for call sites where the folio has subpage attached and
133 * the range is ensured to be inside the folio's single page.
134 *
135 * btrfs_folio_*() are for call sites where the page can either be subpage
136 * specific or regular folios. The function will handle both cases.
137 * But the range still needs to be inside one single page.
138 *
139 * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
140 * need to be inside the page. Those functions will truncate the range
141 * automatically.
142 *
143 * Both btrfs_folio_*() and btrfs_folio_clamp_*() are for data folios.
144 *
145 * For metadata, one should use btrfs_meta_folio_*() helpers instead, and there
146 * is no clamp version for metadata helpers, as we either go subpage
147 * (nodesize < PAGE_SIZE) or go regular folio helpers (nodesize >= PAGE_SIZE,
148 * and our folio is never larger than nodesize).
149 */
150 #define DECLARE_BTRFS_SUBPAGE_OPS(name) \
151 void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
152 struct folio *folio, u64 start, u32 len); \
153 void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
154 struct folio *folio, u64 start, u32 len); \
155 bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
156 struct folio *folio, u64 start, u32 len); \
157 void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
158 struct folio *folio, u64 start, u32 len); \
159 void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
160 struct folio *folio, u64 start, u32 len); \
161 bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
162 struct folio *folio, u64 start, u32 len); \
163 void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
164 struct folio *folio, u64 start, u32 len); \
165 void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
166 struct folio *folio, u64 start, u32 len); \
167 bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
168 struct folio *folio, u64 start, u32 len); \
169 void btrfs_meta_folio_set_##name(struct folio *folio, const struct extent_buffer *eb); \
170 void btrfs_meta_folio_clear_##name(struct folio *folio, const struct extent_buffer *eb); \
171 bool btrfs_meta_folio_test_##name(struct folio *folio, const struct extent_buffer *eb);
172
173 DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
174 DECLARE_BTRFS_SUBPAGE_OPS(dirty);
175 DECLARE_BTRFS_SUBPAGE_OPS(writeback);
176
177 /*
178 * Fixup bit helpers.
179 *
180 * The fixup bit is data-only and has no plain set helper (setting happens
181 * together with dirtying in btrfs_subpage_set_fixup_dirty()), so it does not
182 * go through DECLARE_BTRFS_SUBPAGE_OPS(). For single-block folios the
183 * folio_*_fixup_pending() flag takes the place of the bitmap.
184 */
185 void btrfs_subpage_clear_fixup(const struct btrfs_fs_info *fs_info,
186 struct folio *folio, u64 start, u32 len);
187 bool btrfs_subpage_test_fixup(const struct btrfs_fs_info *fs_info,
188 struct folio *folio, u64 start, u32 len);
189 bool btrfs_folio_test_fixup(const struct btrfs_fs_info *fs_info,
190 struct folio *folio, u64 start, u32 len);
191 void btrfs_folio_set_fixup_dirty(const struct btrfs_fs_info *fs_info,
192 struct folio *folio, u64 start, u32 len);
193 /* For a block that just got its space reserved; it stays dirty. */
194 void btrfs_folio_clear_fixup(const struct btrfs_fs_info *fs_info,
195 struct folio *folio, u64 start, u32 len);
196 /* For callers discarding the data; clears the dirty bits too. */
197 void btrfs_folio_clear_fixup_dirty(const struct btrfs_fs_info *fs_info,
198 struct folio *folio, u64 start, u32 len);
199
200 /*
201 * Helper for error cleanup, where a folio will have its dirty flag cleared,
202 * with writeback started and finished.
203 */
btrfs_folio_clamp_finish_io(struct btrfs_fs_info * fs_info,struct folio * locked_folio,u64 start,u32 len)204 static inline void btrfs_folio_clamp_finish_io(struct btrfs_fs_info *fs_info,
205 struct folio *locked_folio,
206 u64 start, u32 len)
207 {
208 btrfs_folio_clamp_clear_dirty(fs_info, locked_folio, start, len);
209 btrfs_folio_clamp_set_writeback(fs_info, locked_folio, start, len);
210 btrfs_folio_clamp_clear_writeback(fs_info, locked_folio, start, len);
211 }
212
213 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
214 struct folio *folio, u64 start, u32 len);
215
216 void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
217 struct folio *folio, u64 start, u32 len);
218 bool btrfs_meta_folio_clear_and_test_dirty(struct folio *folio, const struct extent_buffer *eb);
219 void btrfs_copy_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
220 struct folio *folio,
221 unsigned long *dst);
222 void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
223 struct folio *folio, u64 start, u32 len);
224
225 #endif
226