167 lines · c
1/* SPDX-License-Identifier: GPL-2.0 */2 3#ifndef BTRFS_SUBPAGE_H4#define BTRFS_SUBPAGE_H5 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 into18 * one larger bitmap.19 *20 * This structure records how they are organized in the bitmap:21 *22 * /- uptodate /- dirty /- ordered23 * | | |24 * v v v25 * |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, as29 * 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_max39};40 41/*42 * Structure to trace status of each sector inside a page, attached to43 * 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 /*49 * Both data and metadata needs to track how many readers are for the50 * page.51 * Data relies on @readers to unlock the page when last reader finished.52 * While metadata doesn't need page unlock, it needs to prevent53 * page::private get cleared before the last end_page_read().54 */55 atomic_t readers;56 union {57 /*58 * Structures only used by metadata59 *60 * @eb_refs should only be operated under private_lock, as it61 * manages whether the subpage can be detached.62 */63 atomic_t eb_refs;64 65 /* Structures only used by data */66 atomic_t writers;67 };68 unsigned long bitmaps[];69};70 71enum btrfs_subpage_type {72 BTRFS_SUBPAGE_METADATA,73 BTRFS_SUBPAGE_DATA,74};75 76#if PAGE_SIZE > SZ_4K77bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info, struct address_space *mapping);78#else79static inline bool btrfs_is_subpage(const struct btrfs_fs_info *fs_info,80 struct address_space *mapping)81{82 return false;83}84#endif85 86int btrfs_attach_subpage(const struct btrfs_fs_info *fs_info,87 struct folio *folio, enum btrfs_subpage_type type);88void btrfs_detach_subpage(const struct btrfs_fs_info *fs_info, struct folio *folio);89 90/* Allocate additional data where page represents more than one sector */91struct btrfs_subpage *btrfs_alloc_subpage(const struct btrfs_fs_info *fs_info,92 enum btrfs_subpage_type type);93void btrfs_free_subpage(struct btrfs_subpage *subpage);94 95void btrfs_folio_inc_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);96void btrfs_folio_dec_eb_refs(const struct btrfs_fs_info *fs_info, struct folio *folio);97 98void btrfs_subpage_start_reader(const struct btrfs_fs_info *fs_info,99 struct folio *folio, u64 start, u32 len);100void btrfs_subpage_end_reader(const struct btrfs_fs_info *fs_info,101 struct folio *folio, u64 start, u32 len);102 103int btrfs_folio_start_writer_lock(const struct btrfs_fs_info *fs_info,104 struct folio *folio, u64 start, u32 len);105void btrfs_folio_end_writer_lock(const struct btrfs_fs_info *fs_info,106 struct folio *folio, u64 start, u32 len);107void btrfs_folio_set_writer_lock(const struct btrfs_fs_info *fs_info,108 struct folio *folio, u64 start, u32 len);109void btrfs_folio_end_writer_lock_bitmap(const struct btrfs_fs_info *fs_info,110 struct folio *folio, unsigned long bitmap);111bool btrfs_subpage_find_writer_locked(const struct btrfs_fs_info *fs_info,112 struct folio *folio, u64 search_start,113 u64 *found_start_ret, u32 *found_len_ret);114 115/*116 * Template for subpage related operations.117 *118 * btrfs_subpage_*() are for call sites where the folio has subpage attached and119 * the range is ensured to be inside the folio's single page.120 *121 * btrfs_folio_*() are for call sites where the page can either be subpage122 * specific or regular folios. The function will handle both cases.123 * But the range still needs to be inside one single page.124 *125 * btrfs_folio_clamp_*() are similar to btrfs_folio_*(), except the range doesn't126 * need to be inside the page. Those functions will truncate the range127 * automatically.128 */129#define DECLARE_BTRFS_SUBPAGE_OPS(name) \130void btrfs_subpage_set_##name(const struct btrfs_fs_info *fs_info, \131 struct folio *folio, u64 start, u32 len); \132void btrfs_subpage_clear_##name(const struct btrfs_fs_info *fs_info, \133 struct folio *folio, u64 start, u32 len); \134bool btrfs_subpage_test_##name(const struct btrfs_fs_info *fs_info, \135 struct folio *folio, u64 start, u32 len); \136void btrfs_folio_set_##name(const struct btrfs_fs_info *fs_info, \137 struct folio *folio, u64 start, u32 len); \138void btrfs_folio_clear_##name(const struct btrfs_fs_info *fs_info, \139 struct folio *folio, u64 start, u32 len); \140bool btrfs_folio_test_##name(const struct btrfs_fs_info *fs_info, \141 struct folio *folio, u64 start, u32 len); \142void btrfs_folio_clamp_set_##name(const struct btrfs_fs_info *fs_info, \143 struct folio *folio, u64 start, u32 len); \144void btrfs_folio_clamp_clear_##name(const struct btrfs_fs_info *fs_info, \145 struct folio *folio, u64 start, u32 len); \146bool btrfs_folio_clamp_test_##name(const struct btrfs_fs_info *fs_info, \147 struct folio *folio, u64 start, u32 len);148 149DECLARE_BTRFS_SUBPAGE_OPS(uptodate);150DECLARE_BTRFS_SUBPAGE_OPS(dirty);151DECLARE_BTRFS_SUBPAGE_OPS(writeback);152DECLARE_BTRFS_SUBPAGE_OPS(ordered);153DECLARE_BTRFS_SUBPAGE_OPS(checked);154 155bool btrfs_subpage_clear_and_test_dirty(const struct btrfs_fs_info *fs_info,156 struct folio *folio, u64 start, u32 len);157 158void btrfs_folio_assert_not_dirty(const struct btrfs_fs_info *fs_info,159 struct folio *folio, u64 start, u32 len);160void btrfs_get_subpage_dirty_bitmap(struct btrfs_fs_info *fs_info,161 struct folio *folio,162 unsigned long *ret_bitmap);163void __cold btrfs_subpage_dump_bitmap(const struct btrfs_fs_info *fs_info,164 struct folio *folio, u64 start, u32 len);165 166#endif167