Loading...
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
8/*
9 * Extra info for subpapge bitmap.
10 *
11 * For subpage we pack all uptodate/error/dirty/writeback/ordered bitmaps into
12 * one larger bitmap.
13 *
14 * This structure records how they are organized in the bitmap:
15 *
16 * /- uptodate_offset /- error_offset /- dirty_offset
17 * | | |
18 * v v v
19 * |u|u|u|u|........|u|u|e|e|.......|e|e| ... |o|o|
20 * |<- bitmap_nr_bits ->|
21 * |<--------------- total_nr_bits ---------------->|
22 */
23struct btrfs_subpage_info {
24 /* Number of bits for each bitmap */
25 unsigned int bitmap_nr_bits;
26
27 /* Total number of bits for the whole bitmap */
28 unsigned int total_nr_bits;
29
30 /*
31 * *_start indicates where the bitmap starts, the length is always
32 * @bitmap_size, which is calculated from PAGE_SIZE / sectorsize.
33 */
34 unsigned int uptodate_offset;
35 unsigned int error_offset;
36 unsigned int dirty_offset;
37 unsigned int writeback_offset;
38 unsigned int ordered_offset;
39 unsigned int checked_offset;
40};
41
42/*
43 * Structure to trace status of each sector inside a page, attached to
44 * page::private for both data and metadata inodes.
45 */
46struct btrfs_subpage {
47 /* Common members for both data and metadata pages */
48 spinlock_t lock;
49 /*
50 * Both data and metadata needs to track how many readers are for the
51 * page.
52 * Data relies on @readers to unlock the page when last reader finished.
53 * While metadata doesn't need page unlock, it needs to prevent
54 * page::private get cleared before the last end_page_read().
55 */
56 atomic_t readers;
57 union {
58 /*
59 * Structures only used by metadata
60 *
61 * @eb_refs should only be operated under private_lock, as it
62 * manages whether the subpage can be detached.
63 */
64 atomic_t eb_refs;
65
66 /* Structures only used by data */
67 atomic_t writers;
68 };
69 unsigned long bitmaps[];
70};
71
72enum btrfs_subpage_type {
73 BTRFS_SUBPAGE_METADATA,
74 BTRFS_SUBPAGE_DATA,
75};
76
77bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct page *page);
78
79void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize);
80int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
81 struct page *page, enum btrfs_subpage_type type);
82void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
83 struct page *page);
84
85/* Allocate additional data where page represents more than one sector */
86struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
87 enum btrfs_subpage_type type);
88void btrfs_free_subpage(struct btrfs_subpage *subpage);
89
90void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
91 struct page *page);
92void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
93 struct page *page);
94
95void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
96 struct page *page, u64 start, u32 len);
97void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
98 struct page *page, u64 start, u32 len);
99
100void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
101 struct page *page, u64 start, u32 len);
102bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
103 struct page *page, u64 start, u32 len);
104int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
105 struct page *page, u64 start, u32 len);
106void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
107 struct page *page, u64 start, u32 len);
108
109/*
110 * Template for subpage related operations.
111 *
112 * btrfs_subpage_*() are for call sites where the page has subpage attached and
113 * the range is ensured to be inside the page.
114 *
115 * btrfs_page_*() are for call sites where the page can either be subpage
116 * specific or regular page. The function will handle both cases.
117 * But the range still needs to be inside the page.
118 *
119 * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't
120 * need to be inside the page. Those functions will truncate the range
121 * automatically.
122 */
123#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
124void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
125 struct page *page, u64 start, u32 len); \
126void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
127 struct page *page, u64 start, u32 len); \
128bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
129 struct page *page, u64 start, u32 len); \
130void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
131 struct page *page, u64 start, u32 len); \
132void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
133 struct page *page, u64 start, u32 len); \
134bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
135 struct page *page, u64 start, u32 len); \
136void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
137 struct page *page, u64 start, u32 len); \
138void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
139 struct page *page, u64 start, u32 len); \
140bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
141 struct page *page, u64 start, u32 len);
142
143DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
144DECLARE_BTRFS_SUBPAGE_OPS(error);
145DECLARE_BTRFS_SUBPAGE_OPS(dirty);
146DECLARE_BTRFS_SUBPAGE_OPS(writeback);
147DECLARE_BTRFS_SUBPAGE_OPS(ordered);
148DECLARE_BTRFS_SUBPAGE_OPS(checked);
149
150bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
151 struct page *page, u64 start, u32 len);
152
153void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,
154 struct page *page);
155void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page,
156 u64 start, u32 len);
157
158#endif
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
10struct address_space;
11struct folio;
12struct 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 */
31enum {
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 */
45struct 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
67enum btrfs_subpage_type {
68 BTRFS_SUBPAGE_METADATA,
69 BTRFS_SUBPAGE_DATA,
70};
71
72#if PAGE_SIZE > SZ_4K
73bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);
74#else
75static 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
82int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
83 struct folio *folio, enum btrfs_subpage_type type);
84void 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 */
87struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
88 enum btrfs_subpage_type type);
89void btrfs_free_subpage(struct btrfs_subpage *subpage);
90
91void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
92void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);
93
94void btrfs_folio_end_lock(const struct btrfs_fs_info *fs_info,
95 struct folio *folio, u64 start, u32 len);
96void btrfs_folio_set_lock(const struct btrfs_fs_info *fs_info,
97 struct folio *folio, u64 start, u32 len);
98void 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) \
115void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
116 struct folio *folio, u64 start, u32 len); \
117void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
118 struct folio *folio, u64 start, u32 len); \
119bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
120 struct folio *folio, u64 start, u32 len); \
121void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \
122 struct folio *folio, u64 start, u32 len); \
123void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \
124 struct folio *folio, u64 start, u32 len); \
125bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \
126 struct folio *folio, u64 start, u32 len); \
127void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
128 struct folio *folio, u64 start, u32 len); \
129void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
130 struct folio *folio, u64 start, u32 len); \
131bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
132 struct folio *folio, u64 start, u32 len);
133
134DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
135DECLARE_BTRFS_SUBPAGE_OPS(dirty);
136DECLARE_BTRFS_SUBPAGE_OPS(writeback);
137DECLARE_BTRFS_SUBPAGE_OPS(ordered);
138DECLARE_BTRFS_SUBPAGE_OPS(checked);
139
140/*
141 * Helper for error cleanup, where a folio will have its dirty flag cleared,
142 * with writeback started and finished.
143 */
144static inline void btrfs_folio_clamp_finish_io(struct btrfs_fs_info *fs_info,
145 struct folio *locked_folio,
146 u64 start, u32 len)
147{
148 btrfs_folio_clamp_clear_dirty(fs_info, locked_folio, start, len);
149 btrfs_folio_clamp_set_writeback(fs_info, locked_folio, start, len);
150 btrfs_folio_clamp_clear_writeback(fs_info, locked_folio, start, len);
151}
152
153bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
154 struct folio *folio, u64 start, u32 len);
155
156void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,
157 struct folio *folio, u64 start, u32 len);
158void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,
159 struct folio *folio,
160 unsigned long *ret_bitmap);
161void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,
162 struct folio *folio, u64 start, u32 len);
163
164#endif