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 | /* |
Qu Wenruo | 8481dd8 | 2021-08-17 17:38:51 +0800 | [diff] [blame] | 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 | */ |
| 23 | struct 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; |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 39 | unsigned int checked_offset; |
Qu Wenruo | 8481dd8 | 2021-08-17 17:38:51 +0800 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | /* |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 43 | * Structure to trace status of each sector inside a page, attached to |
| 44 | * page::private for both data and metadata inodes. |
| 45 | */ |
| 46 | struct btrfs_subpage { |
| 47 | /* Common members for both data and metadata pages */ |
| 48 | spinlock_t lock; |
Qu Wenruo | 3d078ef | 2021-06-07 17:02:58 +0800 | [diff] [blame] | 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; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 57 | union { |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 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; |
Qu Wenruo | 6f17400 | 2021-05-31 16:50:45 +0800 | [diff] [blame] | 65 | |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 66 | /* Structures only used by data */ |
| 67 | atomic_t writers; |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 68 | }; |
Qu Wenruo | 72a69cd | 2021-08-17 17:38:52 +0800 | [diff] [blame] | 69 | unsigned long bitmaps[]; |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | enum btrfs_subpage_type { |
| 73 | BTRFS_SUBPAGE_METADATA, |
| 74 | BTRFS_SUBPAGE_DATA, |
| 75 | }; |
| 76 | |
Qu Wenruo | 8481dd8 | 2021-08-17 17:38:51 +0800 | [diff] [blame] | 77 | void btrfs_init_subpage_info(struct btrfs_subpage_info *subpage_info, u32 sectorsize); |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 78 | int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info, |
| 79 | struct page *page, enum btrfs_subpage_type type); |
| 80 | void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, |
| 81 | struct page *page); |
| 82 | |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 83 | /* Allocate additional data where page represents more than one sector */ |
Qu Wenruo | 651fb41 | 2021-08-17 17:38:50 +0800 | [diff] [blame] | 84 | struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info, |
| 85 | enum btrfs_subpage_type type); |
Qu Wenruo | 760f991 | 2021-01-26 16:33:48 +0800 | [diff] [blame] | 86 | void btrfs_free_subpage(struct btrfs_subpage *subpage); |
| 87 | |
Qu Wenruo | 8ff8466 | 2021-01-26 16:33:50 +0800 | [diff] [blame] | 88 | void btrfs_page_inc_eb_refs(const struct btrfs_fs_info *fs_info, |
| 89 | struct page *page); |
| 90 | void btrfs_page_dec_eb_refs(const struct btrfs_fs_info *fs_info, |
| 91 | struct page *page); |
| 92 | |
Qu Wenruo | 92082d4 | 2021-02-02 10:28:36 +0800 | [diff] [blame] | 93 | void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info, |
| 94 | struct page *page, u64 start, u32 len); |
| 95 | void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info, |
| 96 | struct page *page, u64 start, u32 len); |
| 97 | |
Qu Wenruo | 1e1de38 | 2021-05-31 16:50:44 +0800 | [diff] [blame] | 98 | void btrfs_subpage_start_writer(const struct btrfs_fs_info *fs_info, |
| 99 | struct page *page, u64 start, u32 len); |
| 100 | bool btrfs_subpage_end_and_test_writer(const struct btrfs_fs_info *fs_info, |
| 101 | struct page *page, u64 start, u32 len); |
| 102 | int btrfs_page_start_writer_lock(const struct btrfs_fs_info *fs_info, |
| 103 | struct page *page, u64 start, u32 len); |
| 104 | void btrfs_page_end_writer_lock(const struct btrfs_fs_info *fs_info, |
| 105 | struct page *page, u64 start, u32 len); |
| 106 | |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 107 | /* |
| 108 | * Template for subpage related operations. |
| 109 | * |
| 110 | * btrfs_subpage_*() are for call sites where the page has subpage attached and |
| 111 | * the range is ensured to be inside the page. |
| 112 | * |
| 113 | * btrfs_page_*() are for call sites where the page can either be subpage |
| 114 | * specific or regular page. The function will handle both cases. |
| 115 | * But the range still needs to be inside the page. |
Qu Wenruo | 60e2d25 | 2021-05-31 16:50:39 +0800 | [diff] [blame] | 116 | * |
| 117 | * btrfs_page_clamp_*() are similar to btrfs_page_*(), except the range doesn't |
| 118 | * need to be inside the page. Those functions will truncate the range |
| 119 | * automatically. |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 120 | */ |
| 121 | #define DECLARE_BTRFS_SUBPAGE_OPS(name) \ |
| 122 | void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 123 | struct page *page, u64 start, u32 len); \ |
| 124 | void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 125 | struct page *page, u64 start, u32 len); \ |
| 126 | bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \ |
| 127 | struct page *page, u64 start, u32 len); \ |
| 128 | void btrfs_page_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 129 | struct page *page, u64 start, u32 len); \ |
| 130 | void btrfs_page_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 131 | struct page *page, u64 start, u32 len); \ |
| 132 | bool btrfs_page_test_##name(const struct btrfs_fs_info *fs_info, \ |
Qu Wenruo | 60e2d25 | 2021-05-31 16:50:39 +0800 | [diff] [blame] | 133 | struct page *page, u64 start, u32 len); \ |
| 134 | void btrfs_page_clamp_set_##name(const struct btrfs_fs_info *fs_info, \ |
| 135 | struct page *page, u64 start, u32 len); \ |
| 136 | void btrfs_page_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \ |
| 137 | struct page *page, u64 start, u32 len); \ |
| 138 | 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] | 139 | struct page *page, u64 start, u32 len); |
| 140 | |
| 141 | DECLARE_BTRFS_SUBPAGE_OPS(uptodate); |
Qu Wenruo | 03a816b3 | 2021-01-26 16:33:53 +0800 | [diff] [blame] | 142 | DECLARE_BTRFS_SUBPAGE_OPS(error); |
Qu Wenruo | d8a5713 | 2021-03-25 15:14:37 +0800 | [diff] [blame] | 143 | DECLARE_BTRFS_SUBPAGE_OPS(dirty); |
Qu Wenruo | 3470da3 | 2021-03-25 15:14:38 +0800 | [diff] [blame] | 144 | DECLARE_BTRFS_SUBPAGE_OPS(writeback); |
Qu Wenruo | 6f17400 | 2021-05-31 16:50:45 +0800 | [diff] [blame] | 145 | DECLARE_BTRFS_SUBPAGE_OPS(ordered); |
Qu Wenruo | e4f9434 | 2021-09-27 15:21:49 +0800 | [diff] [blame] | 146 | DECLARE_BTRFS_SUBPAGE_OPS(checked); |
Qu Wenruo | d8a5713 | 2021-03-25 15:14:37 +0800 | [diff] [blame] | 147 | |
| 148 | bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info, |
| 149 | struct page *page, u64 start, u32 len); |
Qu Wenruo | a1d767c | 2021-01-26 16:33:52 +0800 | [diff] [blame] | 150 | |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 151 | void btrfs_page_assert_not_dirty(const struct btrfs_fs_info *fs_info, |
| 152 | struct page *page); |
Qu Wenruo | e55a0de | 2021-09-27 15:22:05 +0800 | [diff] [blame] | 153 | void btrfs_page_unlock_writer(struct btrfs_fs_info *fs_info, struct page *page, |
| 154 | u64 start, u32 len); |
Qu Wenruo | cc1d0d9 | 2021-07-26 14:34:58 +0800 | [diff] [blame] | 155 | |
Qu Wenruo | cac06d8 | 2021-01-26 16:33:47 +0800 | [diff] [blame] | 156 | #endif |