xref: /linux/fs/btrfs/extent_map.h (revision 6dfafbd0299a60bfb5d5e277fdf100037c7ded07)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 
3 #ifndef BTRFS_EXTENT_MAP_H
4 #define BTRFS_EXTENT_MAP_H
5 
6 #include <linux/compiler_types.h>
7 #include <linux/spinlock_types.h>
8 #include <linux/rbtree.h>
9 #include <linux/list.h>
10 #include <linux/refcount.h>
11 #include "fs.h"
12 
13 struct btrfs_inode;
14 struct btrfs_fs_info;
15 
16 #define EXTENT_MAP_LAST_BYTE ((u64)-4)
17 #define EXTENT_MAP_HOLE ((u64)-3)
18 #define EXTENT_MAP_INLINE ((u64)-2)
19 
20 /* bits for the extent_map::flags field */
21 enum {
22 	/* this entry not yet on disk, don't free it */
23 	ENUM_BIT(EXTENT_FLAG_PINNED),
24 	ENUM_BIT(EXTENT_FLAG_COMPRESS_ZLIB),
25 	ENUM_BIT(EXTENT_FLAG_COMPRESS_LZO),
26 	ENUM_BIT(EXTENT_FLAG_COMPRESS_ZSTD),
27 	/* pre-allocated extent */
28 	ENUM_BIT(EXTENT_FLAG_PREALLOC),
29 	/* Logging this extent */
30 	ENUM_BIT(EXTENT_FLAG_LOGGING),
31 	/* This em is merged from two or more physically adjacent ems */
32 	ENUM_BIT(EXTENT_FLAG_MERGED),
33 };
34 
35 /*
36  * This structure represents file extents and holes.
37  *
38  * Unlike on-disk file extent items, extent maps can be merged to save memory.
39  * This means members only match file extent items before any merging.
40  *
41  * Keep this structure as compact as possible, as we can have really large
42  * amounts of allocated extent maps at any time.
43  */
44 struct extent_map {
45 	struct rb_node rb_node;
46 
47 	/* All of these are in bytes. */
48 
49 	/* File offset matching the offset of a BTRFS_EXTENT_ITEM_KEY key. */
50 	u64 start;
51 
52 	/*
53 	 * Length of the file extent.
54 	 *
55 	 * For non-inlined file extents it's btrfs_file_extent_item::num_bytes.
56 	 * For inline extents it's sectorsize, since inline data starts at
57 	 * offsetof(struct btrfs_file_extent_item, disk_bytenr) thus
58 	 * btrfs_file_extent_item::num_bytes is not valid.
59 	 */
60 	u64 len;
61 
62 	/*
63 	 * The bytenr of the full on-disk extent.
64 	 *
65 	 * For regular extents it's btrfs_file_extent_item::disk_bytenr.
66 	 * For holes it's EXTENT_MAP_HOLE and for inline extents it's
67 	 * EXTENT_MAP_INLINE.
68 	 */
69 	u64 disk_bytenr;
70 
71 	/*
72 	 * The full on-disk extent length, matching
73 	 * btrfs_file_extent_item::disk_num_bytes.
74 	 */
75 	u64 disk_num_bytes;
76 
77 	/*
78 	 * Offset inside the decompressed extent.
79 	 *
80 	 * For regular extents it's btrfs_file_extent_item::offset.
81 	 * For holes and inline extents it's 0.
82 	 */
83 	u64 offset;
84 
85 	/*
86 	 * The decompressed size of the whole on-disk extent, matching
87 	 * btrfs_file_extent_item::ram_bytes.
88 	 */
89 	u64 ram_bytes;
90 
91 	/*
92 	 * Generation of the extent map, for merged em it's the highest
93 	 * generation of all merged ems.
94 	 * For non-merged extents, it's from btrfs_file_extent_item::generation.
95 	 */
96 	u64 generation;
97 	u32 flags;
98 	refcount_t refs;
99 	struct list_head list;
100 };
101 
102 struct extent_map_tree {
103 	struct rb_root root;
104 	struct list_head modified_extents;
105 	rwlock_t lock;
106 };
107 
108 struct btrfs_inode;
109 
110 static inline void btrfs_extent_map_set_compression(struct extent_map *em,
111 						    enum btrfs_compression_type type)
112 {
113 	if (type == BTRFS_COMPRESS_ZLIB)
114 		em->flags |= EXTENT_FLAG_COMPRESS_ZLIB;
115 	else if (type == BTRFS_COMPRESS_LZO)
116 		em->flags |= EXTENT_FLAG_COMPRESS_LZO;
117 	else if (type == BTRFS_COMPRESS_ZSTD)
118 		em->flags |= EXTENT_FLAG_COMPRESS_ZSTD;
119 }
120 
121 static inline enum btrfs_compression_type btrfs_extent_map_compression(
122 						       const struct extent_map *em)
123 {
124 	if (em->flags & EXTENT_FLAG_COMPRESS_ZLIB)
125 		return BTRFS_COMPRESS_ZLIB;
126 
127 	if (em->flags & EXTENT_FLAG_COMPRESS_LZO)
128 		return BTRFS_COMPRESS_LZO;
129 
130 	if (em->flags & EXTENT_FLAG_COMPRESS_ZSTD)
131 		return BTRFS_COMPRESS_ZSTD;
132 
133 	return BTRFS_COMPRESS_NONE;
134 }
135 
136 /*
137  * More efficient way to determine if extent is compressed, instead of using
138  * 'extent_map_compression() != BTRFS_COMPRESS_NONE'.
139  */
140 static inline bool btrfs_extent_map_is_compressed(const struct extent_map *em)
141 {
142 	return (em->flags & (EXTENT_FLAG_COMPRESS_ZLIB |
143 			     EXTENT_FLAG_COMPRESS_LZO |
144 			     EXTENT_FLAG_COMPRESS_ZSTD)) != 0;
145 }
146 
147 static inline int btrfs_extent_map_in_tree(const struct extent_map *em)
148 {
149 	return !RB_EMPTY_NODE(&em->rb_node);
150 }
151 
152 static inline u64 btrfs_extent_map_block_start(const struct extent_map *em)
153 {
154 	if (em->disk_bytenr < EXTENT_MAP_LAST_BYTE) {
155 		if (btrfs_extent_map_is_compressed(em))
156 			return em->disk_bytenr;
157 		return em->disk_bytenr + em->offset;
158 	}
159 	return em->disk_bytenr;
160 }
161 
162 static inline u64 btrfs_extent_map_end(const struct extent_map *em)
163 {
164 	if (em->start + em->len < em->start)
165 		return (u64)-1;
166 	return em->start + em->len;
167 }
168 
169 void btrfs_extent_map_tree_init(struct extent_map_tree *tree);
170 struct extent_map *btrfs_lookup_extent_mapping(struct extent_map_tree *tree,
171 					       u64 start, u64 len);
172 void btrfs_remove_extent_mapping(struct btrfs_inode *inode, struct extent_map *em);
173 int btrfs_split_extent_map(struct btrfs_inode *inode, u64 start, u64 len, u64 pre,
174 			   u64 new_logical);
175 
176 struct extent_map *btrfs_alloc_extent_map(void);
177 void btrfs_free_extent_map(struct extent_map *em);
178 int __init btrfs_extent_map_init(void);
179 void __cold btrfs_extent_map_exit(void);
180 int btrfs_unpin_extent_cache(struct btrfs_inode *inode, u64 start, u64 len, u64 gen);
181 void btrfs_clear_em_logging(struct btrfs_inode *inode, struct extent_map *em);
182 struct extent_map *btrfs_search_extent_mapping(struct extent_map_tree *tree,
183 					       u64 start, u64 len);
184 int btrfs_add_extent_mapping(struct btrfs_inode *inode,
185 			     struct extent_map **em_in, u64 start, u64 len);
186 void btrfs_drop_extent_map_range(struct btrfs_inode *inode,
187 				 u64 start, u64 end,
188 				 bool skip_pinned);
189 int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
190 				   struct extent_map *new_em,
191 				   bool modified);
192 void btrfs_free_extent_maps(struct btrfs_fs_info *fs_info, long nr_to_scan);
193 void btrfs_init_extent_map_shrinker_work(struct btrfs_fs_info *fs_info);
194 
195 #endif
196