1 /* SPDX-License-Identifier: GPL-2.0 */
3 #ifndef BTRFS_SUBPAGE_H
4 #define BTRFS_SUBPAGE_H
6 #include <linux/spinlock.h>
7 #include <linux/atomic.h>
8 #include <linux/sizes.h>
15 * Extra info for subpapge bitmap.
17 * For subpage we pack all uptodate/dirty/writeback/ordered bitmaps into
20 * This structure records how they are organized in the bitmap:
22 * /- uptodate /- dirty /- ordered
25 * |u|u|u|u|........|u|u|d|d|.......|d|d|o|o|.......|o|o|
26 * |< sectors_per_page >|
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.
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
,
42 * Structure to trace status of each sector inside a page, attached to
43 * page::private for both data and metadata inodes.
45 struct btrfs_subpage
{
46 /* Common members for both data and metadata pages */
49 * Both data and metadata needs to track how many readers are for the
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().
58 * Structures only used by metadata
60 * @eb_refs should only be operated under private_lock, as it
61 * manages whether the subpage can be detached.
65 /* Structures only used by data */
68 unsigned long bitmaps
[];
71 enum btrfs_subpage_type
{
72 BTRFS_SUBPAGE_METADATA
,
77 bool 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
)
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
);
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
);
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
);
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
);
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
);
116 * Template for subpage related operations.
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.
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.
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
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);
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
);
155 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info
*fs_info
,
156 struct folio
*folio
, u64 start
, u32 len
);
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
,
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
);