Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 
0003 #ifndef BTRFS_EXTENT_IO_TREE_H
0004 #define BTRFS_EXTENT_IO_TREE_H
0005 
0006 struct extent_changeset;
0007 struct io_failure_record;
0008 
0009 /* Bits for the extent state */
0010 #define EXTENT_DIRTY        (1U << 0)
0011 #define EXTENT_UPTODATE     (1U << 1)
0012 #define EXTENT_LOCKED       (1U << 2)
0013 #define EXTENT_NEW      (1U << 3)
0014 #define EXTENT_DELALLOC     (1U << 4)
0015 #define EXTENT_DEFRAG       (1U << 5)
0016 #define EXTENT_BOUNDARY     (1U << 6)
0017 #define EXTENT_NODATASUM    (1U << 7)
0018 #define EXTENT_CLEAR_META_RESV  (1U << 8)
0019 #define EXTENT_NEED_WAIT    (1U << 9)
0020 #define EXTENT_DAMAGED      (1U << 10)
0021 #define EXTENT_NORESERVE    (1U << 11)
0022 #define EXTENT_QGROUP_RESERVED  (1U << 12)
0023 #define EXTENT_CLEAR_DATA_RESV  (1U << 13)
0024 /*
0025  * Must be cleared only during ordered extent completion or on error paths if we
0026  * did not manage to submit bios and create the ordered extents for the range.
0027  * Should not be cleared during page release and page invalidation (if there is
0028  * an ordered extent in flight), that is left for the ordered extent completion.
0029  */
0030 #define EXTENT_DELALLOC_NEW (1U << 14)
0031 /*
0032  * When an ordered extent successfully completes for a region marked as a new
0033  * delalloc range, use this flag when clearing a new delalloc range to indicate
0034  * that the VFS' inode number of bytes should be incremented and the inode's new
0035  * delalloc bytes decremented, in an atomic way to prevent races with stat(2).
0036  */
0037 #define EXTENT_ADD_INODE_BYTES  (1U << 15)
0038 #define EXTENT_DO_ACCOUNTING    (EXTENT_CLEAR_META_RESV | \
0039                  EXTENT_CLEAR_DATA_RESV)
0040 #define EXTENT_CTLBITS      (EXTENT_DO_ACCOUNTING | \
0041                  EXTENT_ADD_INODE_BYTES)
0042 
0043 /*
0044  * Redefined bits above which are used only in the device allocation tree,
0045  * shouldn't be using EXTENT_LOCKED / EXTENT_BOUNDARY / EXTENT_CLEAR_META_RESV
0046  * / EXTENT_CLEAR_DATA_RESV because they have special meaning to the bit
0047  * manipulation functions
0048  */
0049 #define CHUNK_ALLOCATED             EXTENT_DIRTY
0050 #define CHUNK_TRIMMED               EXTENT_DEFRAG
0051 #define CHUNK_STATE_MASK            (CHUNK_ALLOCATED |      \
0052                          CHUNK_TRIMMED)
0053 
0054 enum {
0055     IO_TREE_FS_PINNED_EXTENTS,
0056     IO_TREE_FS_EXCLUDED_EXTENTS,
0057     IO_TREE_BTREE_INODE_IO,
0058     IO_TREE_INODE_IO,
0059     IO_TREE_INODE_IO_FAILURE,
0060     IO_TREE_RELOC_BLOCKS,
0061     IO_TREE_TRANS_DIRTY_PAGES,
0062     IO_TREE_ROOT_DIRTY_LOG_PAGES,
0063     IO_TREE_INODE_FILE_EXTENT,
0064     IO_TREE_LOG_CSUM_RANGE,
0065     IO_TREE_SELFTEST,
0066     IO_TREE_DEVICE_ALLOC_STATE,
0067 };
0068 
0069 struct extent_io_tree {
0070     struct rb_root state;
0071     struct btrfs_fs_info *fs_info;
0072     void *private_data;
0073     u64 dirty_bytes;
0074     bool track_uptodate;
0075 
0076     /* Who owns this io tree, should be one of IO_TREE_* */
0077     u8 owner;
0078 
0079     spinlock_t lock;
0080 };
0081 
0082 struct extent_state {
0083     u64 start;
0084     u64 end; /* inclusive */
0085     struct rb_node rb_node;
0086 
0087     /* ADD NEW ELEMENTS AFTER THIS */
0088     wait_queue_head_t wq;
0089     refcount_t refs;
0090     u32 state;
0091 
0092     struct io_failure_record *failrec;
0093 
0094 #ifdef CONFIG_BTRFS_DEBUG
0095     struct list_head leak_list;
0096 #endif
0097 };
0098 
0099 int __init extent_state_cache_init(void);
0100 void __cold extent_state_cache_exit(void);
0101 
0102 void extent_io_tree_init(struct btrfs_fs_info *fs_info,
0103              struct extent_io_tree *tree, unsigned int owner,
0104              void *private_data);
0105 void extent_io_tree_release(struct extent_io_tree *tree);
0106 
0107 int lock_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
0108              struct extent_state **cached);
0109 
0110 static inline int lock_extent(struct extent_io_tree *tree, u64 start, u64 end)
0111 {
0112     return lock_extent_bits(tree, start, end, NULL);
0113 }
0114 
0115 int try_lock_extent(struct extent_io_tree *tree, u64 start, u64 end);
0116 
0117 int __init extent_io_init(void);
0118 void __cold extent_io_exit(void);
0119 
0120 u64 count_range_bits(struct extent_io_tree *tree,
0121              u64 *start, u64 search_end,
0122              u64 max_bytes, u32 bits, int contig);
0123 
0124 void free_extent_state(struct extent_state *state);
0125 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
0126            u32 bits, int filled, struct extent_state *cached_state);
0127 int clear_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
0128                  u32 bits, struct extent_changeset *changeset);
0129 int clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
0130              u32 bits, int wake, int delete,
0131              struct extent_state **cached);
0132 int __clear_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
0133              u32 bits, int wake, int delete,
0134              struct extent_state **cached, gfp_t mask,
0135              struct extent_changeset *changeset);
0136 
0137 static inline int unlock_extent(struct extent_io_tree *tree, u64 start, u64 end)
0138 {
0139     return clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, NULL);
0140 }
0141 
0142 static inline int unlock_extent_cached(struct extent_io_tree *tree, u64 start,
0143         u64 end, struct extent_state **cached)
0144 {
0145     return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
0146                 GFP_NOFS, NULL);
0147 }
0148 
0149 static inline int unlock_extent_cached_atomic(struct extent_io_tree *tree,
0150         u64 start, u64 end, struct extent_state **cached)
0151 {
0152     return __clear_extent_bit(tree, start, end, EXTENT_LOCKED, 1, 0, cached,
0153                 GFP_ATOMIC, NULL);
0154 }
0155 
0156 static inline int clear_extent_bits(struct extent_io_tree *tree, u64 start,
0157                     u64 end, u32 bits)
0158 {
0159     int wake = 0;
0160 
0161     if (bits & EXTENT_LOCKED)
0162         wake = 1;
0163 
0164     return clear_extent_bit(tree, start, end, bits, wake, 0, NULL);
0165 }
0166 
0167 int set_record_extent_bits(struct extent_io_tree *tree, u64 start, u64 end,
0168                u32 bits, struct extent_changeset *changeset);
0169 int set_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
0170            u32 bits, unsigned exclusive_bits, u64 *failed_start,
0171            struct extent_state **cached_state, gfp_t mask,
0172            struct extent_changeset *changeset);
0173 int set_extent_bits_nowait(struct extent_io_tree *tree, u64 start, u64 end,
0174                u32 bits);
0175 
0176 static inline int set_extent_bits(struct extent_io_tree *tree, u64 start,
0177         u64 end, u32 bits)
0178 {
0179     return set_extent_bit(tree, start, end, bits, 0, NULL, NULL, GFP_NOFS,
0180                   NULL);
0181 }
0182 
0183 static inline int clear_extent_uptodate(struct extent_io_tree *tree, u64 start,
0184         u64 end, struct extent_state **cached_state)
0185 {
0186     return __clear_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, 0,
0187                 cached_state, GFP_NOFS, NULL);
0188 }
0189 
0190 static inline int set_extent_dirty(struct extent_io_tree *tree, u64 start,
0191         u64 end, gfp_t mask)
0192 {
0193     return set_extent_bit(tree, start, end, EXTENT_DIRTY, 0, NULL, NULL,
0194                   mask, NULL);
0195 }
0196 
0197 static inline int clear_extent_dirty(struct extent_io_tree *tree, u64 start,
0198                      u64 end, struct extent_state **cached)
0199 {
0200     return clear_extent_bit(tree, start, end,
0201                 EXTENT_DIRTY | EXTENT_DELALLOC |
0202                 EXTENT_DO_ACCOUNTING, 0, 0, cached);
0203 }
0204 
0205 int convert_extent_bit(struct extent_io_tree *tree, u64 start, u64 end,
0206                u32 bits, u32 clear_bits,
0207                struct extent_state **cached_state);
0208 
0209 static inline int set_extent_delalloc(struct extent_io_tree *tree, u64 start,
0210                       u64 end, u32 extra_bits,
0211                       struct extent_state **cached_state)
0212 {
0213     return set_extent_bit(tree, start, end,
0214                   EXTENT_DELALLOC | EXTENT_UPTODATE | extra_bits,
0215                   0, NULL, cached_state, GFP_NOFS, NULL);
0216 }
0217 
0218 static inline int set_extent_defrag(struct extent_io_tree *tree, u64 start,
0219         u64 end, struct extent_state **cached_state)
0220 {
0221     return set_extent_bit(tree, start, end,
0222                   EXTENT_DELALLOC | EXTENT_UPTODATE | EXTENT_DEFRAG,
0223                   0, NULL, cached_state, GFP_NOFS, NULL);
0224 }
0225 
0226 static inline int set_extent_new(struct extent_io_tree *tree, u64 start,
0227         u64 end)
0228 {
0229     return set_extent_bit(tree, start, end, EXTENT_NEW, 0, NULL, NULL,
0230                   GFP_NOFS, NULL);
0231 }
0232 
0233 static inline int set_extent_uptodate(struct extent_io_tree *tree, u64 start,
0234         u64 end, struct extent_state **cached_state, gfp_t mask)
0235 {
0236     return set_extent_bit(tree, start, end, EXTENT_UPTODATE, 0, NULL,
0237                   cached_state, mask, NULL);
0238 }
0239 
0240 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
0241               u64 *start_ret, u64 *end_ret, u32 bits,
0242               struct extent_state **cached_state);
0243 void find_first_clear_extent_bit(struct extent_io_tree *tree, u64 start,
0244                  u64 *start_ret, u64 *end_ret, u32 bits);
0245 int find_contiguous_extent_bit(struct extent_io_tree *tree, u64 start,
0246                    u64 *start_ret, u64 *end_ret, u32 bits);
0247 int extent_invalidate_folio(struct extent_io_tree *tree,
0248               struct folio *folio, size_t offset);
0249 bool btrfs_find_delalloc_range(struct extent_io_tree *tree, u64 *start,
0250                    u64 *end, u64 max_bytes,
0251                    struct extent_state **cached_state);
0252 
0253 /* This should be reworked in the future and put elsewhere. */
0254 struct io_failure_record *get_state_failrec(struct extent_io_tree *tree, u64 start);
0255 int set_state_failrec(struct extent_io_tree *tree, u64 start,
0256               struct io_failure_record *failrec);
0257 void btrfs_free_io_failure_record(struct btrfs_inode *inode, u64 start,
0258         u64 end);
0259 int free_io_failure(struct extent_io_tree *failure_tree,
0260             struct extent_io_tree *io_tree,
0261             struct io_failure_record *rec);
0262 int clean_io_failure(struct btrfs_fs_info *fs_info,
0263              struct extent_io_tree *failure_tree,
0264              struct extent_io_tree *io_tree, u64 start,
0265              struct page *page, u64 ino, unsigned int pg_offset);
0266 
0267 #endif /* BTRFS_EXTENT_IO_TREE_H */