1 // SPDX-License-Identifier: GPL-2.0+
2
3 /*
4 * Crossported from btrfs-progs/extent_io.h
5 *
6 * Modification includes:
7 * - extent_buffer:data
8 * Use pointer to provide better alignment.
9 * - Remove max_cache_size related interfaces
10 * Includes free_extent_buffer_nocache()
11 * As we don't cache eb in U-boot.
12 * - Include headers
13 *
14 * Write related functions are kept as we still need to modify dummy extent
15 * buffers even in RO environment.
16 */
17 #ifndef __BTRFS_EXTENT_IO_H__
18 #define __BTRFS_EXTENT_IO_H__
19
20 #include <linux/types.h>
21 #include <linux/list.h>
22 #include <linux/err.h>
23 #include <linux/bitops.h>
24 #include <fs_internal.h>
25 #include "extent-cache.h"
26
27 #define EXTENT_DIRTY (1U << 0)
28 #define EXTENT_WRITEBACK (1U << 1)
29 #define EXTENT_UPTODATE (1U << 2)
30 #define EXTENT_LOCKED (1U << 3)
31 #define EXTENT_NEW (1U << 4)
32 #define EXTENT_DELALLOC (1U << 5)
33 #define EXTENT_DEFRAG (1U << 6)
34 #define EXTENT_DEFRAG_DONE (1U << 7)
35 #define EXTENT_BUFFER_FILLED (1U << 8)
36 #define EXTENT_CSUM (1U << 9)
37 #define EXTENT_BAD_TRANSID (1U << 10)
38 #define EXTENT_BUFFER_DUMMY (1U << 11)
39 #define EXTENT_IOBITS (EXTENT_LOCKED | EXTENT_WRITEBACK)
40
41 #define BLOCK_GROUP_DATA (1U << 1)
42 #define BLOCK_GROUP_METADATA (1U << 2)
43 #define BLOCK_GROUP_SYSTEM (1U << 4)
44
45 /*
46 * The extent buffer bitmap operations are done with byte granularity instead of
47 * word granularity for two reasons:
48 * 1. The bitmaps must be little-endian on disk.
49 * 2. Bitmap items are not guaranteed to be aligned to a word and therefore a
50 * single word in a bitmap may straddle two pages in the extent buffer.
51 */
52 #define BIT_BYTE(nr) ((nr) / BITS_PER_BYTE)
53 #define BYTE_MASK ((1 << BITS_PER_BYTE) - 1)
54 #define BITMAP_FIRST_BYTE_MASK(start) \
55 ((BYTE_MASK << ((start) & (BITS_PER_BYTE - 1))) & BYTE_MASK)
56 #define BITMAP_LAST_BYTE_MASK(nbits) \
57 (BYTE_MASK >> (-(nbits) & (BITS_PER_BYTE - 1)))
58
le_test_bit(int nr,const u8 * addr)59 static inline int le_test_bit(int nr, const u8 *addr)
60 {
61 return 1U & (addr[BIT_BYTE(nr)] >> (nr & (BITS_PER_BYTE-1)));
62 }
63
64 struct btrfs_fs_info;
65
66 struct extent_io_tree {
67 struct cache_tree state;
68 struct cache_tree cache;
69 u64 cache_size;
70 };
71
72 struct extent_state {
73 struct cache_extent cache_node;
74 u64 start;
75 u64 end;
76 int refs;
77 unsigned long state;
78 u64 xprivate;
79 };
80
81 struct extent_buffer {
82 struct cache_extent cache_node;
83 u64 start;
84 u32 len;
85 int refs;
86 u32 flags;
87 struct btrfs_fs_info *fs_info;
88 char *data;
89 };
90
extent_buffer_get(struct extent_buffer * eb)91 static inline void extent_buffer_get(struct extent_buffer *eb)
92 {
93 eb->refs++;
94 }
95
96 void extent_io_tree_init(struct extent_io_tree *tree);
97 void extent_io_tree_cleanup(struct extent_io_tree *tree);
98 int set_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, int bits);
99 int clear_extent_bits(struct extent_io_tree *tree, u64 start, u64 end, int bits);
100 int find_first_extent_bit(struct extent_io_tree *tree, u64 start,
101 u64 *start_ret, u64 *end_ret, int bits);
102 int test_range_bit(struct extent_io_tree *tree, u64 start, u64 end,
103 int bits, int filled);
104 int set_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end);
105 int clear_extent_dirty(struct extent_io_tree *tree, u64 start, u64 end);
set_extent_buffer_uptodate(struct extent_buffer * eb)106 static inline int set_extent_buffer_uptodate(struct extent_buffer *eb)
107 {
108 eb->flags |= EXTENT_UPTODATE;
109 return 0;
110 }
111
clear_extent_buffer_uptodate(struct extent_buffer * eb)112 static inline int clear_extent_buffer_uptodate(struct extent_buffer *eb)
113 {
114 eb->flags &= ~EXTENT_UPTODATE;
115 return 0;
116 }
117
extent_buffer_uptodate(struct extent_buffer * eb)118 static inline int extent_buffer_uptodate(struct extent_buffer *eb)
119 {
120 if (!eb || IS_ERR(eb))
121 return 0;
122 if (eb->flags & EXTENT_UPTODATE)
123 return 1;
124 return 0;
125 }
126
127 int set_state_private(struct extent_io_tree *tree, u64 start, u64 xprivate);
128 int get_state_private(struct extent_io_tree *tree, u64 start, u64 *xprivate);
129 struct extent_buffer *find_extent_buffer(struct extent_io_tree *tree,
130 u64 bytenr, u32 blocksize);
131 struct extent_buffer *find_first_extent_buffer(struct extent_io_tree *tree,
132 u64 start);
133 struct extent_buffer *alloc_extent_buffer(struct btrfs_fs_info *fs_info,
134 u64 bytenr, u32 blocksize);
135 struct extent_buffer *btrfs_clone_extent_buffer(struct extent_buffer *src);
136 struct extent_buffer *alloc_dummy_extent_buffer(struct btrfs_fs_info *fs_info,
137 u64 bytenr, u32 blocksize);
138 void free_extent_buffer(struct extent_buffer *eb);
139 int read_extent_from_disk(struct blk_desc *desc, struct disk_partition *part,
140 u64 physical, struct extent_buffer *eb,
141 unsigned long offset, unsigned long len);
142 int memcmp_extent_buffer(const struct extent_buffer *eb, const void *ptrv,
143 unsigned long start, unsigned long len);
144 void read_extent_buffer(const struct extent_buffer *eb, void *dst,
145 unsigned long start, unsigned long len);
146 void write_extent_buffer(struct extent_buffer *eb, const void *src,
147 unsigned long start, unsigned long len);
148 void copy_extent_buffer(struct extent_buffer *dst, struct extent_buffer *src,
149 unsigned long dst_offset, unsigned long src_offset,
150 unsigned long len);
151 void memmove_extent_buffer(struct extent_buffer *dst, unsigned long dst_offset,
152 unsigned long src_offset, unsigned long len);
153 void memset_extent_buffer(struct extent_buffer *eb, char c,
154 unsigned long start, unsigned long len);
155 int extent_buffer_test_bit(struct extent_buffer *eb, unsigned long start,
156 unsigned long nr);
157 int set_extent_buffer_dirty(struct extent_buffer *eb);
158 int clear_extent_buffer_dirty(struct extent_buffer *eb);
159 void extent_buffer_bitmap_clear(struct extent_buffer *eb, unsigned long start,
160 unsigned long pos, unsigned long len);
161 void extent_buffer_bitmap_set(struct extent_buffer *eb, unsigned long start,
162 unsigned long pos, unsigned long len);
163
164 #endif
165