xref: /linux/fs/btrfs/subpage.h (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 	union {
49 		/*
50 		 * Structures only used by metadata
51 		 *
52 		 * @eb_refs should only be operated under private_lock, as it
53 		 * manages whether the subpage can be detached.
54 		 */
55 		atomic_t eb_refs;
56 
57 		/*
58 		 * Structures only used by data,
59 		 *
60 		 * How many sectors inside the page is locked.
61 		 */
62 		atomic_t nr_locked;
63 	};
64 	unsigned long bitmaps[];
65 };
66 
67 enum btrfs_subpage_type {
68 	BTRFS_SUBPAGE_METADATA,
69 	BTRFS_SUBPAGE_DATA,
70 };
71 
72 #if PAGE_SIZE > SZ_4K
73 bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
74 #else
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)
77 {
78 	return false;
79 }
80 #endif
81 
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);
85 
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);
90 
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);
93 
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);
100 /*
101  * Template for subpage related operations.
102  *
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.
105  *
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.
109  *
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
112  * automatically.
113  */
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);
133 
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);
139 
140 bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
141 					struct folio *folio, u64 start, u32 len);
142 
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,
146 				    struct folio *folio,
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);
150 
151 #endif
152