Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 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 | */ |
| 18 | struct btrfs_subpage { |
| 19 | /* Common members for both data and metadata pages */ |
| 20 | spinlock_t lock; |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 21 | u16 uptodate_bitmap; |
Qu Wenruo | 03a816b3 | 2021-01-26 16:33:53 +0800 | [diff] [blame] | 22 | u16 error_bitmap; |
Qu Wenruo | d8a5713 | 2021-03-25 15:14:37 +0800 | [diff] [blame] | 23 | u16 dirty_bitmap; |
Qu Wenruo | 3470da3 | 2021-03-25 15:14:38 +0800 | [diff] [blame] | 24 | u16 writeback_bitmap; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 25 | union { |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 26 | /* |
| 27 | * Structures only used by metadata |
| 28 | * |
| 29 | * @eb_refs should only be operated under private_lock, as it |
| 30 | * manages whether the subpage can be detached. |
| 31 | */ |
| 32 | atomic_t eb_refs; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 33 | /* Structures only used by data */ |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 34 | struct { |
| 35 | atomic_t readers; |
| 36 | }; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 37 | }; |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | enum btrfs_subpage_type { |
| 41 | BTRFS_SUBPAGE_METADATA, |
| 42 | BTRFS_SUBPAGE_DATA, |
| 43 | }; |
| 44 | |
| 45 | int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info, |
| 46 | struct page *page, enum btrfs_subpage_type type); |
| 47 | void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, |
| 48 | struct page *page); |
| 49 | |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 50 | /* Allocate additional data where page represents more than one sector */ |
| 51 | int btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, |
| 52 | struct btrfs_subpage **ret, |
| 53 | enum btrfs_subpage_type type); |
| 54 | void btrfs_free_subpage(struct btrfs_subpage *subpage); |
| 55 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 56 | void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info, |
| 57 | struct page *page); |
| 58 | void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info, |
| 59 | struct page *page); |
| 60 | |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 61 | void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info, |
| 62 | struct page *page, u64 start, u32 len); |
| 63 | void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info, |
| 64 | struct page *page, u64 start, u32 len); |
| 65 | |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 66 | /* |
| 67 | * Template for subpage related operations. |
| 68 | * |
| 69 | * btrfs_subpage_*() are for call sites where the page has subpage attached and |
| 70 | * the range is ensured to be inside the page. |
| 71 | * |
| 72 | * btrfs_page_*() are for call sites where the page can either be subpage |
| 73 | * specific or regular page. The function will handle both cases. |
| 74 | * But the range still needs to be inside the page. |
Qu Wenruo | 60e2d25 | 2021-05-31 16:50:39 +0800 | [diff] [blame^] | 75 | * |
| 76 | * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't |
| 77 | * need to be inside the page. Those functions will truncate the range |
| 78 | * automatically. |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 79 | */ |
| 80 | #define DECLARE_BTRFS_SUBPAGE_OPS(name) \ |
| 81 | void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 82 | struct page *page, u64 start, u32 len); \ |
| 83 | void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 84 | struct page *page, u64 start, u32 len); \ |
| 85 | bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \ |
| 86 | struct page *page, u64 start, u32 len); \ |
| 87 | void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 88 | struct page *page, u64 start, u32 len); \ |
| 89 | void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 90 | struct page *page, u64 start, u32 len); \ |
| 91 | bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \ |
Qu Wenruo | 60e2d25 | 2021-05-31 16:50:39 +0800 | [diff] [blame^] | 92 | struct page *page, u64 start, u32 len); \ |
| 93 | void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 94 | struct page *page, u64 start, u32 len); \ |
| 95 | void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 96 | struct page *page, u64 start, u32 len); \ |
| 97 | bool btrfs_page_clamp_test_##name(const struct btrfs_fs_info *fs_info, \ |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 98 | struct page *page, u64 start, u32 len); |
| 99 | |
| 100 | DECLARE_BTRFS_SUBPAGE_OPS(uptodate); |
Qu Wenruo | 03a816b3 | 2021-01-26 16:33:53 +0800 | [diff] [blame] | 101 | DECLARE_BTRFS_SUBPAGE_OPS(error); |
Qu Wenruo | d8a5713 | 2021-03-25 15:14:37 +0800 | [diff] [blame] | 102 | DECLARE_BTRFS_SUBPAGE_OPS(dirty); |
Qu Wenruo | 3470da3 | 2021-03-25 15:14:38 +0800 | [diff] [blame] | 103 | DECLARE_BTRFS_SUBPAGE_OPS(writeback); |
Qu Wenruo | d8a5713 | 2021-03-25 15:14:37 +0800 | [diff] [blame] | 104 | |
| 105 | bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info, |
| 106 | struct page *page, u64 start, u32 len); |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 107 | |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 108 | #endif |