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 */
50 * Structures only used by metadata
52 * @eb_refs should only be operated under private_lock, as it
53 * manages whether the subpage can be detached.
58 * Structures only used by data,
60 * How many sectors inside the page is locked.
64 unsigned long bitmaps
[];
67 enum btrfs_subpage_type
{
68 BTRFS_SUBPAGE_METADATA
,
73 bool btrfs_is_subpage(const struct btrfs_fs_info
*fs_info
, struct address_space
*mapping
);
75 static inline bool btrfs_is_subpage(const struct btrfs_fs_info
*fs_info
,
76 struct address_space
*mapping
)
82 int btrfs_attach_subpage(const struct btrfs_fs_info
*fs_info
,
83 struct folio
*folio
, enum btrfs_subpage_type type
);
84 void btrfs_detach_subpage(const struct btrfs_fs_info
*fs_info
, struct folio
*folio
);
86 /* Allocate additional data where page represents more than one sector */
87 struct btrfs_subpage
*btrfs_alloc_subpage(const struct btrfs_fs_info
*fs_info
,
88 enum btrfs_subpage_type type
);
89 void btrfs_free_subpage(struct btrfs_subpage
*subpage
);
91 void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info
*fs_info
, struct folio
*folio
);
92 void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info
*fs_info
, struct folio
*folio
);
94 void btrfs_folio_end_lock(const struct btrfs_fs_info
*fs_info
,
95 struct folio
*folio
, u64 start
, u32 len
);
96 void btrfs_folio_set_lock(const struct btrfs_fs_info
*fs_info
,
97 struct folio
*folio
, u64 start
, u32 len
);
98 void btrfs_folio_end_lock_bitmap(const struct btrfs_fs_info
*fs_info
,
99 struct folio
*folio
, unsigned long bitmap
);
101 * Template for subpage related operations.
103 * btrfs_subpage_*() are for call sites where the folio has subpage attached and
104 * the range is ensured to be inside the folio's single page.
106 * btrfs_folio_*() are for call sites where the page can either be subpage
107 * specific or regular folios. The function will handle both cases.
108 * But the range still needs to be inside one single page.
110 * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't
111 * need to be inside the page. Those functions will truncate the range
114 #define DECLARE_BTRFS_SUBPAGE_OPS(name) \
115 void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
116 struct folio *folio, u64 start, u32 len); \
117 void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
118 struct folio *folio, u64 start, u32 len); \
119 bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
120 struct folio *folio, u64 start, u32 len); \
121 void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
122 struct folio *folio, u64 start, u32 len); \
123 void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
124 struct folio *folio, u64 start, u32 len); \
125 bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
126 struct folio *folio, u64 start, u32 len); \
127 void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
128 struct folio *folio, u64 start, u32 len); \
129 void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
130 struct folio *folio, u64 start, u32 len); \
131 bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
132 struct folio *folio, u64 start, u32 len);
134 DECLARE_BTRFS_SUBPAGE_OPS(uptodate
);
135 DECLARE_BTRFS_SUBPAGE_OPS(dirty
);
136 DECLARE_BTRFS_SUBPAGE_OPS(writeback
);
137 DECLARE_BTRFS_SUBPAGE_OPS(ordered
);
138 DECLARE_BTRFS_SUBPAGE_OPS(checked
);
140 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info
*fs_info
,
141 struct folio
*folio
, u64 start
, u32 len
);
143 void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info
*fs_info
,
144 struct folio
*folio
, u64 start
, u32 len
);
145 void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info
*fs_info
,
147 unsigned long *ret_bitmap
);
148 void __cold
btrfs_subpage_dump_bitmap(const struct btrfs_fs_info
*fs_info
,
149 struct folio
*folio
, u64 start
, u32 len
);