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
8/*
9 * Maximum page size we support is 64K, minimum sector size is 4K, u16 bitmap
10 * is sufficient. Regular bitmap_* is not used due to size reasons.
11 */
12#define BTRFS_SUBPAGE_BITMAP_SIZE 16
13
14/*
15 * Structure to trace status of each sector inside a page, attached to
16 * page::private for both data and metadata inodes.
17 */
18struct btrfs_subpage {
19 /* Common members for both data and metadata pages */
20 spinlock_t lock;
21 u16 uptodate_bitmap;
22 u16 error_bitmap;
23 u16 dirty_bitmap;
24 u16 writeback_bitmap;
25 /*
26 * Both data and metadata needs to track how many readers are for the
27 * page.
28 * Data relies on @readers to unlock the page when last reader finished.
29 * While metadata doesn't need page unlock, it needs to prevent
30 * page::private get cleared before the last end_page_read().
31 */
32 atomic_t readers;
33 union {
34 /*
35 * Structures only used by metadata
36 *
37 * @eb_refs should only be operated under private_lock, as it
38 * manages whether the subpage can be detached.
39 */
40 atomic_t eb_refs;
41 /* Structures only used by data */
42 struct {
43 atomic_t writers;
44
45 /* Tracke pending ordered extent in this sector */
46 u16 ordered_bitmap;
47 };
48 };
49};
50
51enum btrfs_subpage_type {
52 BTRFS_SUBPAGE_METADATA,
53 BTRFS_SUBPAGE_DATA,
54};
55
56int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,
57 struct page *page, enum btrfs_subpage_type type);
58void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info,
59 struct page *page);
60
61/* Allocate additional data where page represents more than one sector */
62int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
63 struct btrfs_subpage **ret,
64 enum btrfs_subpage_type type);
65void btrfs_free_subpage(struct btrfs_subpage *subpage);
66
67void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
68 struct page *page);
69void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
70 struct page *page);
71
72void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
73 struct page *page, u64 start, u32 len);
74void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
75 struct page *page, u64 start, u32 len);
76
77void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
78 struct page *page, u64 start, u32 len);
79bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
80 struct page *page, u64 start, u32 len);
81int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
82 struct page *page, u64 start, u32 len);
83void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
84 struct page *page, u64 start, u32 len);
85
86/*
87 * Template for subpage related operations.
88 *
89 * btrfs_subpage_*() are for call sites where the page has subpage attached and
90 * the range is ensured to be inside the page.
91 *
92 * btrfs_page_*() are for call sites where the page can either be subpage
93 * specific or regular page. The function will handle both cases.
94 * But the range still needs to be inside the page.
95 *
96 * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't
97 * need to be inside the page. Those functions will truncate the range
98 * automatically.
99 */
100#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
101void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
102 struct page *page, u64 start, u32 len); \
103void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
104 struct page *page, u64 start, u32 len); \
105bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
106 struct page *page, u64 start, u32 len); \
107void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
108 struct page *page, u64 start, u32 len); \
109void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
110 struct page *page, u64 start, u32 len); \
111bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
112 struct page *page, u64 start, u32 len); \
113void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
114 struct page *page, u64 start, u32 len); \
115void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
116 struct page *page, u64 start, u32 len); \
117bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
118 struct page *page, u64 start, u32 len);
119
120DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
121DECLARE_BTRFS_SUBPAGE_OPS(error);
122DECLARE_BTRFS_SUBPAGE_OPS(dirty);
123DECLARE_BTRFS_SUBPAGE_OPS(writeback);
124DECLARE_BTRFS_SUBPAGE_OPS(ordered);
125
126bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
127 struct page *page, u64 start, u32 len);
128
129#endif