blob: fdcadc5193c1d9fa703f6f55335a8322900dad58 [file] [log] [blame]
Qu Wenruocac06d82021-01-26 16:33:47 +08001/* 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;
Qu Wenruoa1d767c2021-01-26 16:33:52 +080021 u16 uptodate_bitmap;
Qu Wenruo03a816b32021-01-26 16:33:53 +080022 u16 error_bitmap;
Qu Wenruod8a57132021-03-25 15:14:37 +080023 u16 dirty_bitmap;
Qu Wenruo3470da32021-03-25 15:14:38 +080024 u16 writeback_bitmap;
Qu Wenruo3d078ef2021-06-07 17:02:58 +080025 /*
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;
Qu Wenruo760f9912021-01-26 16:33:48 +080033 union {
Qu Wenruo8ff84662021-01-26 16:33:50 +080034 /*
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;
Qu Wenruo760f9912021-01-26 16:33:48 +080041 /* Structures only used by data */
Qu Wenruo92082d42021-02-02 10:28:36 +080042 struct {
Qu Wenruo1e1de382021-05-31 16:50:44 +080043 atomic_t writers;
Qu Wenruo6f174002021-05-31 16:50:45 +080044
45 /* Tracke pending ordered extent in this sector */
46 u16 ordered_bitmap;
Qu Wenruo92082d42021-02-02 10:28:36 +080047 };
Qu Wenruo760f9912021-01-26 16:33:48 +080048 };
Qu Wenruocac06d82021-01-26 16:33:47 +080049};
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
Qu Wenruo760f9912021-01-26 16:33:48 +080061/* Allocate additional data where page represents more than one sector */
Qu Wenruo651fb412021-08-17 17:38:50 +080062struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,
63 enum btrfs_subpage_type type);
Qu Wenruo760f9912021-01-26 16:33:48 +080064void btrfs_free_subpage(struct btrfs_subpage *subpage);
65
Qu Wenruo8ff84662021-01-26 16:33:50 +080066void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info,
67 struct page *page);
68void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info,
69 struct page *page);
70
Qu Wenruo92082d42021-02-02 10:28:36 +080071void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,
72 struct page *page, u64 start, u32 len);
73void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,
74 struct page *page, u64 start, u32 len);
75
Qu Wenruo1e1de382021-05-31 16:50:44 +080076void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info,
77 struct page *page, u64 start, u32 len);
78bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info,
79 struct page *page, u64 start, u32 len);
80int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info,
81 struct page *page, u64 start, u32 len);
82void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info,
83 struct page *page, u64 start, u32 len);
84
Qu Wenruoa1d767c2021-01-26 16:33:52 +080085/*
86 * Template for subpage related operations.
87 *
88 * btrfs_subpage_*() are for call sites where the page has subpage attached and
89 * the range is ensured to be inside the page.
90 *
91 * btrfs_page_*() are for call sites where the page can either be subpage
92 * specific or regular page. The function will handle both cases.
93 * But the range still needs to be inside the page.
Qu Wenruo60e2d252021-05-31 16:50:39 +080094 *
95 * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't
96 * need to be inside the page. Those functions will truncate the range
97 * automatically.
Qu Wenruoa1d767c2021-01-26 16:33:52 +080098 */
99#define DECLARE_BTRFS_SUBPAGE_OPS(name) \
100void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \
101 struct page *page, u64 start, u32 len); \
102void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \
103 struct page *page, u64 start, u32 len); \
104bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \
105 struct page *page, u64 start, u32 len); \
106void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \
107 struct page *page, u64 start, u32 len); \
108void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \
109 struct page *page, u64 start, u32 len); \
110bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \
Qu Wenruo60e2d252021-05-31 16:50:39 +0800111 struct page *page, u64 start, u32 len); \
112void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \
113 struct page *page, u64 start, u32 len); \
114void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \
115 struct page *page, u64 start, u32 len); \
116bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \
Qu Wenruoa1d767c2021-01-26 16:33:52 +0800117 struct page *page, u64 start, u32 len);
118
119DECLARE_BTRFS_SUBPAGE_OPS(uptodate);
Qu Wenruo03a816b32021-01-26 16:33:53 +0800120DECLARE_BTRFS_SUBPAGE_OPS(error);
Qu Wenruod8a57132021-03-25 15:14:37 +0800121DECLARE_BTRFS_SUBPAGE_OPS(dirty);
Qu Wenruo3470da32021-03-25 15:14:38 +0800122DECLARE_BTRFS_SUBPAGE_OPS(writeback);
Qu Wenruo6f174002021-05-31 16:50:45 +0800123DECLARE_BTRFS_SUBPAGE_OPS(ordered);
Qu Wenruod8a57132021-03-25 15:14:37 +0800124
125bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,
126 struct page *page, u64 start, u32 len);
Qu Wenruoa1d767c2021-01-26 16:33:52 +0800127
Qu Wenruocc1d0d92021-07-26 14:34:58 +0800128void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info,
129 struct page *page);
130
Qu Wenruocac06d82021-01-26 16:33:47 +0800131#endif