Loading...
1#include <linux/err.h>
2#include <linux/slab.h>
3#include <linux/spinlock.h>
4#include <linux/hardirq.h>
5#include "ctree.h"
6#include "extent_map.h"
7
8
9static struct kmem_cache *extent_map_cache;
10
11int __init extent_map_init(void)
12{
13 extent_map_cache = kmem_cache_create("btrfs_extent_map",
14 sizeof(struct extent_map), 0,
15 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
16 if (!extent_map_cache)
17 return -ENOMEM;
18 return 0;
19}
20
21void extent_map_exit(void)
22{
23 if (extent_map_cache)
24 kmem_cache_destroy(extent_map_cache);
25}
26
27/**
28 * extent_map_tree_init - initialize extent map tree
29 * @tree: tree to initialize
30 *
31 * Initialize the extent tree @tree. Should be called for each new inode
32 * or other user of the extent_map interface.
33 */
34void extent_map_tree_init(struct extent_map_tree *tree)
35{
36 tree->map = RB_ROOT;
37 INIT_LIST_HEAD(&tree->modified_extents);
38 rwlock_init(&tree->lock);
39}
40
41/**
42 * alloc_extent_map - allocate new extent map structure
43 *
44 * Allocate a new extent_map structure. The new structure is
45 * returned with a reference count of one and needs to be
46 * freed using free_extent_map()
47 */
48struct extent_map *alloc_extent_map(void)
49{
50 struct extent_map *em;
51 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
52 if (!em)
53 return NULL;
54 RB_CLEAR_NODE(&em->rb_node);
55 em->flags = 0;
56 em->compress_type = BTRFS_COMPRESS_NONE;
57 em->generation = 0;
58 atomic_set(&em->refs, 1);
59 INIT_LIST_HEAD(&em->list);
60 return em;
61}
62
63/**
64 * free_extent_map - drop reference count of an extent_map
65 * @em: extent map beeing releasead
66 *
67 * Drops the reference out on @em by one and free the structure
68 * if the reference count hits zero.
69 */
70void free_extent_map(struct extent_map *em)
71{
72 if (!em)
73 return;
74 WARN_ON(atomic_read(&em->refs) == 0);
75 if (atomic_dec_and_test(&em->refs)) {
76 WARN_ON(extent_map_in_tree(em));
77 WARN_ON(!list_empty(&em->list));
78 kmem_cache_free(extent_map_cache, em);
79 }
80}
81
82/* simple helper to do math around the end of an extent, handling wrap */
83static u64 range_end(u64 start, u64 len)
84{
85 if (start + len < start)
86 return (u64)-1;
87 return start + len;
88}
89
90static int tree_insert(struct rb_root *root, struct extent_map *em)
91{
92 struct rb_node **p = &root->rb_node;
93 struct rb_node *parent = NULL;
94 struct extent_map *entry = NULL;
95 struct rb_node *orig_parent = NULL;
96 u64 end = range_end(em->start, em->len);
97
98 while (*p) {
99 parent = *p;
100 entry = rb_entry(parent, struct extent_map, rb_node);
101
102 if (em->start < entry->start)
103 p = &(*p)->rb_left;
104 else if (em->start >= extent_map_end(entry))
105 p = &(*p)->rb_right;
106 else
107 return -EEXIST;
108 }
109
110 orig_parent = parent;
111 while (parent && em->start >= extent_map_end(entry)) {
112 parent = rb_next(parent);
113 entry = rb_entry(parent, struct extent_map, rb_node);
114 }
115 if (parent)
116 if (end > entry->start && em->start < extent_map_end(entry))
117 return -EEXIST;
118
119 parent = orig_parent;
120 entry = rb_entry(parent, struct extent_map, rb_node);
121 while (parent && em->start < entry->start) {
122 parent = rb_prev(parent);
123 entry = rb_entry(parent, struct extent_map, rb_node);
124 }
125 if (parent)
126 if (end > entry->start && em->start < extent_map_end(entry))
127 return -EEXIST;
128
129 rb_link_node(&em->rb_node, orig_parent, p);
130 rb_insert_color(&em->rb_node, root);
131 return 0;
132}
133
134/*
135 * search through the tree for an extent_map with a given offset. If
136 * it can't be found, try to find some neighboring extents
137 */
138static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
139 struct rb_node **prev_ret,
140 struct rb_node **next_ret)
141{
142 struct rb_node *n = root->rb_node;
143 struct rb_node *prev = NULL;
144 struct rb_node *orig_prev = NULL;
145 struct extent_map *entry;
146 struct extent_map *prev_entry = NULL;
147
148 while (n) {
149 entry = rb_entry(n, struct extent_map, rb_node);
150 prev = n;
151 prev_entry = entry;
152
153 if (offset < entry->start)
154 n = n->rb_left;
155 else if (offset >= extent_map_end(entry))
156 n = n->rb_right;
157 else
158 return n;
159 }
160
161 if (prev_ret) {
162 orig_prev = prev;
163 while (prev && offset >= extent_map_end(prev_entry)) {
164 prev = rb_next(prev);
165 prev_entry = rb_entry(prev, struct extent_map, rb_node);
166 }
167 *prev_ret = prev;
168 prev = orig_prev;
169 }
170
171 if (next_ret) {
172 prev_entry = rb_entry(prev, struct extent_map, rb_node);
173 while (prev && offset < prev_entry->start) {
174 prev = rb_prev(prev);
175 prev_entry = rb_entry(prev, struct extent_map, rb_node);
176 }
177 *next_ret = prev;
178 }
179 return NULL;
180}
181
182/* check to see if two extent_map structs are adjacent and safe to merge */
183static int mergable_maps(struct extent_map *prev, struct extent_map *next)
184{
185 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
186 return 0;
187
188 /*
189 * don't merge compressed extents, we need to know their
190 * actual size
191 */
192 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
193 return 0;
194
195 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
196 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
197 return 0;
198
199 /*
200 * We don't want to merge stuff that hasn't been written to the log yet
201 * since it may not reflect exactly what is on disk, and that would be
202 * bad.
203 */
204 if (!list_empty(&prev->list) || !list_empty(&next->list))
205 return 0;
206
207 if (extent_map_end(prev) == next->start &&
208 prev->flags == next->flags &&
209 prev->bdev == next->bdev &&
210 ((next->block_start == EXTENT_MAP_HOLE &&
211 prev->block_start == EXTENT_MAP_HOLE) ||
212 (next->block_start == EXTENT_MAP_INLINE &&
213 prev->block_start == EXTENT_MAP_INLINE) ||
214 (next->block_start == EXTENT_MAP_DELALLOC &&
215 prev->block_start == EXTENT_MAP_DELALLOC) ||
216 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
217 next->block_start == extent_map_block_end(prev)))) {
218 return 1;
219 }
220 return 0;
221}
222
223static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
224{
225 struct extent_map *merge = NULL;
226 struct rb_node *rb;
227
228 if (em->start != 0) {
229 rb = rb_prev(&em->rb_node);
230 if (rb)
231 merge = rb_entry(rb, struct extent_map, rb_node);
232 if (rb && mergable_maps(merge, em)) {
233 em->start = merge->start;
234 em->orig_start = merge->orig_start;
235 em->len += merge->len;
236 em->block_len += merge->block_len;
237 em->block_start = merge->block_start;
238 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
239 em->mod_start = merge->mod_start;
240 em->generation = max(em->generation, merge->generation);
241
242 rb_erase(&merge->rb_node, &tree->map);
243 RB_CLEAR_NODE(&merge->rb_node);
244 free_extent_map(merge);
245 }
246 }
247
248 rb = rb_next(&em->rb_node);
249 if (rb)
250 merge = rb_entry(rb, struct extent_map, rb_node);
251 if (rb && mergable_maps(em, merge)) {
252 em->len += merge->len;
253 em->block_len += merge->block_len;
254 rb_erase(&merge->rb_node, &tree->map);
255 RB_CLEAR_NODE(&merge->rb_node);
256 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
257 em->generation = max(em->generation, merge->generation);
258 free_extent_map(merge);
259 }
260}
261
262/**
263 * unpin_extent_cache - unpin an extent from the cache
264 * @tree: tree to unpin the extent in
265 * @start: logical offset in the file
266 * @len: length of the extent
267 * @gen: generation that this extent has been modified in
268 *
269 * Called after an extent has been written to disk properly. Set the generation
270 * to the generation that actually added the file item to the inode so we know
271 * we need to sync this extent when we call fsync().
272 */
273int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
274 u64 gen)
275{
276 int ret = 0;
277 struct extent_map *em;
278 bool prealloc = false;
279
280 write_lock(&tree->lock);
281 em = lookup_extent_mapping(tree, start, len);
282
283 WARN_ON(!em || em->start != start);
284
285 if (!em)
286 goto out;
287
288 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
289 list_move(&em->list, &tree->modified_extents);
290 em->generation = gen;
291 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
292 em->mod_start = em->start;
293 em->mod_len = em->len;
294
295 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
296 prealloc = true;
297 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
298 }
299
300 try_merge_map(tree, em);
301
302 if (prealloc) {
303 em->mod_start = em->start;
304 em->mod_len = em->len;
305 }
306
307 free_extent_map(em);
308out:
309 write_unlock(&tree->lock);
310 return ret;
311
312}
313
314void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
315{
316 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
317 if (extent_map_in_tree(em))
318 try_merge_map(tree, em);
319}
320
321static inline void setup_extent_mapping(struct extent_map_tree *tree,
322 struct extent_map *em,
323 int modified)
324{
325 atomic_inc(&em->refs);
326 em->mod_start = em->start;
327 em->mod_len = em->len;
328
329 if (modified)
330 list_move(&em->list, &tree->modified_extents);
331 else
332 try_merge_map(tree, em);
333}
334
335/**
336 * add_extent_mapping - add new extent map to the extent tree
337 * @tree: tree to insert new map in
338 * @em: map to insert
339 *
340 * Insert @em into @tree or perform a simple forward/backward merge with
341 * existing mappings. The extent_map struct passed in will be inserted
342 * into the tree directly, with an additional reference taken, or a
343 * reference dropped if the merge attempt was successful.
344 */
345int add_extent_mapping(struct extent_map_tree *tree,
346 struct extent_map *em, int modified)
347{
348 int ret = 0;
349
350 ret = tree_insert(&tree->map, em);
351 if (ret)
352 goto out;
353
354 setup_extent_mapping(tree, em, modified);
355out:
356 return ret;
357}
358
359static struct extent_map *
360__lookup_extent_mapping(struct extent_map_tree *tree,
361 u64 start, u64 len, int strict)
362{
363 struct extent_map *em;
364 struct rb_node *rb_node;
365 struct rb_node *prev = NULL;
366 struct rb_node *next = NULL;
367 u64 end = range_end(start, len);
368
369 rb_node = __tree_search(&tree->map, start, &prev, &next);
370 if (!rb_node) {
371 if (prev)
372 rb_node = prev;
373 else if (next)
374 rb_node = next;
375 else
376 return NULL;
377 }
378
379 em = rb_entry(rb_node, struct extent_map, rb_node);
380
381 if (strict && !(end > em->start && start < extent_map_end(em)))
382 return NULL;
383
384 atomic_inc(&em->refs);
385 return em;
386}
387
388/**
389 * lookup_extent_mapping - lookup extent_map
390 * @tree: tree to lookup in
391 * @start: byte offset to start the search
392 * @len: length of the lookup range
393 *
394 * Find and return the first extent_map struct in @tree that intersects the
395 * [start, len] range. There may be additional objects in the tree that
396 * intersect, so check the object returned carefully to make sure that no
397 * additional lookups are needed.
398 */
399struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
400 u64 start, u64 len)
401{
402 return __lookup_extent_mapping(tree, start, len, 1);
403}
404
405/**
406 * search_extent_mapping - find a nearby extent map
407 * @tree: tree to lookup in
408 * @start: byte offset to start the search
409 * @len: length of the lookup range
410 *
411 * Find and return the first extent_map struct in @tree that intersects the
412 * [start, len] range.
413 *
414 * If one can't be found, any nearby extent may be returned
415 */
416struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
417 u64 start, u64 len)
418{
419 return __lookup_extent_mapping(tree, start, len, 0);
420}
421
422/**
423 * remove_extent_mapping - removes an extent_map from the extent tree
424 * @tree: extent tree to remove from
425 * @em: extent map beeing removed
426 *
427 * Removes @em from @tree. No reference counts are dropped, and no checks
428 * are done to see if the range is in use
429 */
430int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
431{
432 int ret = 0;
433
434 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
435 rb_erase(&em->rb_node, &tree->map);
436 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
437 list_del_init(&em->list);
438 RB_CLEAR_NODE(&em->rb_node);
439 return ret;
440}
441
442void replace_extent_mapping(struct extent_map_tree *tree,
443 struct extent_map *cur,
444 struct extent_map *new,
445 int modified)
446{
447 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
448 ASSERT(extent_map_in_tree(cur));
449 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
450 list_del_init(&cur->list);
451 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
452 RB_CLEAR_NODE(&cur->rb_node);
453
454 setup_extent_mapping(tree, new, modified);
455}
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/err.h>
4#include <linux/slab.h>
5#include <linux/spinlock.h>
6#include "ctree.h"
7#include "extent_map.h"
8#include "compression.h"
9
10
11static struct kmem_cache *extent_map_cache;
12
13int __init extent_map_init(void)
14{
15 extent_map_cache = kmem_cache_create("btrfs_extent_map",
16 sizeof(struct extent_map), 0,
17 SLAB_MEM_SPREAD, NULL);
18 if (!extent_map_cache)
19 return -ENOMEM;
20 return 0;
21}
22
23void __cold extent_map_exit(void)
24{
25 kmem_cache_destroy(extent_map_cache);
26}
27
28/**
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
31 *
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
34 */
35void extent_map_tree_init(struct extent_map_tree *tree)
36{
37 tree->map = RB_ROOT;
38 INIT_LIST_HEAD(&tree->modified_extents);
39 rwlock_init(&tree->lock);
40}
41
42/**
43 * alloc_extent_map - allocate new extent map structure
44 *
45 * Allocate a new extent_map structure. The new structure is
46 * returned with a reference count of one and needs to be
47 * freed using free_extent_map()
48 */
49struct extent_map *alloc_extent_map(void)
50{
51 struct extent_map *em;
52 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
53 if (!em)
54 return NULL;
55 RB_CLEAR_NODE(&em->rb_node);
56 em->flags = 0;
57 em->compress_type = BTRFS_COMPRESS_NONE;
58 em->generation = 0;
59 refcount_set(&em->refs, 1);
60 INIT_LIST_HEAD(&em->list);
61 return em;
62}
63
64/**
65 * free_extent_map - drop reference count of an extent_map
66 * @em: extent map being released
67 *
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
70 */
71void free_extent_map(struct extent_map *em)
72{
73 if (!em)
74 return;
75 WARN_ON(refcount_read(&em->refs) == 0);
76 if (refcount_dec_and_test(&em->refs)) {
77 WARN_ON(extent_map_in_tree(em));
78 WARN_ON(!list_empty(&em->list));
79 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
80 kfree(em->map_lookup);
81 kmem_cache_free(extent_map_cache, em);
82 }
83}
84
85/* simple helper to do math around the end of an extent, handling wrap */
86static u64 range_end(u64 start, u64 len)
87{
88 if (start + len < start)
89 return (u64)-1;
90 return start + len;
91}
92
93static int tree_insert(struct rb_root *root, struct extent_map *em)
94{
95 struct rb_node **p = &root->rb_node;
96 struct rb_node *parent = NULL;
97 struct extent_map *entry = NULL;
98 struct rb_node *orig_parent = NULL;
99 u64 end = range_end(em->start, em->len);
100
101 while (*p) {
102 parent = *p;
103 entry = rb_entry(parent, struct extent_map, rb_node);
104
105 if (em->start < entry->start)
106 p = &(*p)->rb_left;
107 else if (em->start >= extent_map_end(entry))
108 p = &(*p)->rb_right;
109 else
110 return -EEXIST;
111 }
112
113 orig_parent = parent;
114 while (parent && em->start >= extent_map_end(entry)) {
115 parent = rb_next(parent);
116 entry = rb_entry(parent, struct extent_map, rb_node);
117 }
118 if (parent)
119 if (end > entry->start && em->start < extent_map_end(entry))
120 return -EEXIST;
121
122 parent = orig_parent;
123 entry = rb_entry(parent, struct extent_map, rb_node);
124 while (parent && em->start < entry->start) {
125 parent = rb_prev(parent);
126 entry = rb_entry(parent, struct extent_map, rb_node);
127 }
128 if (parent)
129 if (end > entry->start && em->start < extent_map_end(entry))
130 return -EEXIST;
131
132 rb_link_node(&em->rb_node, orig_parent, p);
133 rb_insert_color(&em->rb_node, root);
134 return 0;
135}
136
137/*
138 * search through the tree for an extent_map with a given offset. If
139 * it can't be found, try to find some neighboring extents
140 */
141static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
142 struct rb_node **prev_ret,
143 struct rb_node **next_ret)
144{
145 struct rb_node *n = root->rb_node;
146 struct rb_node *prev = NULL;
147 struct rb_node *orig_prev = NULL;
148 struct extent_map *entry;
149 struct extent_map *prev_entry = NULL;
150
151 while (n) {
152 entry = rb_entry(n, struct extent_map, rb_node);
153 prev = n;
154 prev_entry = entry;
155
156 if (offset < entry->start)
157 n = n->rb_left;
158 else if (offset >= extent_map_end(entry))
159 n = n->rb_right;
160 else
161 return n;
162 }
163
164 if (prev_ret) {
165 orig_prev = prev;
166 while (prev && offset >= extent_map_end(prev_entry)) {
167 prev = rb_next(prev);
168 prev_entry = rb_entry(prev, struct extent_map, rb_node);
169 }
170 *prev_ret = prev;
171 prev = orig_prev;
172 }
173
174 if (next_ret) {
175 prev_entry = rb_entry(prev, struct extent_map, rb_node);
176 while (prev && offset < prev_entry->start) {
177 prev = rb_prev(prev);
178 prev_entry = rb_entry(prev, struct extent_map, rb_node);
179 }
180 *next_ret = prev;
181 }
182 return NULL;
183}
184
185/* check to see if two extent_map structs are adjacent and safe to merge */
186static int mergable_maps(struct extent_map *prev, struct extent_map *next)
187{
188 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
189 return 0;
190
191 /*
192 * don't merge compressed extents, we need to know their
193 * actual size
194 */
195 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
196 return 0;
197
198 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
199 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
200 return 0;
201
202 /*
203 * We don't want to merge stuff that hasn't been written to the log yet
204 * since it may not reflect exactly what is on disk, and that would be
205 * bad.
206 */
207 if (!list_empty(&prev->list) || !list_empty(&next->list))
208 return 0;
209
210 if (extent_map_end(prev) == next->start &&
211 prev->flags == next->flags &&
212 prev->bdev == next->bdev &&
213 ((next->block_start == EXTENT_MAP_HOLE &&
214 prev->block_start == EXTENT_MAP_HOLE) ||
215 (next->block_start == EXTENT_MAP_INLINE &&
216 prev->block_start == EXTENT_MAP_INLINE) ||
217 (next->block_start == EXTENT_MAP_DELALLOC &&
218 prev->block_start == EXTENT_MAP_DELALLOC) ||
219 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
220 next->block_start == extent_map_block_end(prev)))) {
221 return 1;
222 }
223 return 0;
224}
225
226static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
227{
228 struct extent_map *merge = NULL;
229 struct rb_node *rb;
230
231 if (em->start != 0) {
232 rb = rb_prev(&em->rb_node);
233 if (rb)
234 merge = rb_entry(rb, struct extent_map, rb_node);
235 if (rb && mergable_maps(merge, em)) {
236 em->start = merge->start;
237 em->orig_start = merge->orig_start;
238 em->len += merge->len;
239 em->block_len += merge->block_len;
240 em->block_start = merge->block_start;
241 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
242 em->mod_start = merge->mod_start;
243 em->generation = max(em->generation, merge->generation);
244
245 rb_erase(&merge->rb_node, &tree->map);
246 RB_CLEAR_NODE(&merge->rb_node);
247 free_extent_map(merge);
248 }
249 }
250
251 rb = rb_next(&em->rb_node);
252 if (rb)
253 merge = rb_entry(rb, struct extent_map, rb_node);
254 if (rb && mergable_maps(em, merge)) {
255 em->len += merge->len;
256 em->block_len += merge->block_len;
257 rb_erase(&merge->rb_node, &tree->map);
258 RB_CLEAR_NODE(&merge->rb_node);
259 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
260 em->generation = max(em->generation, merge->generation);
261 free_extent_map(merge);
262 }
263}
264
265/**
266 * unpin_extent_cache - unpin an extent from the cache
267 * @tree: tree to unpin the extent in
268 * @start: logical offset in the file
269 * @len: length of the extent
270 * @gen: generation that this extent has been modified in
271 *
272 * Called after an extent has been written to disk properly. Set the generation
273 * to the generation that actually added the file item to the inode so we know
274 * we need to sync this extent when we call fsync().
275 */
276int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
277 u64 gen)
278{
279 int ret = 0;
280 struct extent_map *em;
281 bool prealloc = false;
282
283 write_lock(&tree->lock);
284 em = lookup_extent_mapping(tree, start, len);
285
286 WARN_ON(!em || em->start != start);
287
288 if (!em)
289 goto out;
290
291 em->generation = gen;
292 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
293 em->mod_start = em->start;
294 em->mod_len = em->len;
295
296 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
297 prealloc = true;
298 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
299 }
300
301 try_merge_map(tree, em);
302
303 if (prealloc) {
304 em->mod_start = em->start;
305 em->mod_len = em->len;
306 }
307
308 free_extent_map(em);
309out:
310 write_unlock(&tree->lock);
311 return ret;
312
313}
314
315void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
316{
317 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
318 if (extent_map_in_tree(em))
319 try_merge_map(tree, em);
320}
321
322static inline void setup_extent_mapping(struct extent_map_tree *tree,
323 struct extent_map *em,
324 int modified)
325{
326 refcount_inc(&em->refs);
327 em->mod_start = em->start;
328 em->mod_len = em->len;
329
330 if (modified)
331 list_move(&em->list, &tree->modified_extents);
332 else
333 try_merge_map(tree, em);
334}
335
336/**
337 * add_extent_mapping - add new extent map to the extent tree
338 * @tree: tree to insert new map in
339 * @em: map to insert
340 *
341 * Insert @em into @tree or perform a simple forward/backward merge with
342 * existing mappings. The extent_map struct passed in will be inserted
343 * into the tree directly, with an additional reference taken, or a
344 * reference dropped if the merge attempt was successful.
345 */
346int add_extent_mapping(struct extent_map_tree *tree,
347 struct extent_map *em, int modified)
348{
349 int ret = 0;
350
351 ret = tree_insert(&tree->map, em);
352 if (ret)
353 goto out;
354
355 setup_extent_mapping(tree, em, modified);
356out:
357 return ret;
358}
359
360static struct extent_map *
361__lookup_extent_mapping(struct extent_map_tree *tree,
362 u64 start, u64 len, int strict)
363{
364 struct extent_map *em;
365 struct rb_node *rb_node;
366 struct rb_node *prev = NULL;
367 struct rb_node *next = NULL;
368 u64 end = range_end(start, len);
369
370 rb_node = __tree_search(&tree->map, start, &prev, &next);
371 if (!rb_node) {
372 if (prev)
373 rb_node = prev;
374 else if (next)
375 rb_node = next;
376 else
377 return NULL;
378 }
379
380 em = rb_entry(rb_node, struct extent_map, rb_node);
381
382 if (strict && !(end > em->start && start < extent_map_end(em)))
383 return NULL;
384
385 refcount_inc(&em->refs);
386 return em;
387}
388
389/**
390 * lookup_extent_mapping - lookup extent_map
391 * @tree: tree to lookup in
392 * @start: byte offset to start the search
393 * @len: length of the lookup range
394 *
395 * Find and return the first extent_map struct in @tree that intersects the
396 * [start, len] range. There may be additional objects in the tree that
397 * intersect, so check the object returned carefully to make sure that no
398 * additional lookups are needed.
399 */
400struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
401 u64 start, u64 len)
402{
403 return __lookup_extent_mapping(tree, start, len, 1);
404}
405
406/**
407 * search_extent_mapping - find a nearby extent map
408 * @tree: tree to lookup in
409 * @start: byte offset to start the search
410 * @len: length of the lookup range
411 *
412 * Find and return the first extent_map struct in @tree that intersects the
413 * [start, len] range.
414 *
415 * If one can't be found, any nearby extent may be returned
416 */
417struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
418 u64 start, u64 len)
419{
420 return __lookup_extent_mapping(tree, start, len, 0);
421}
422
423/**
424 * remove_extent_mapping - removes an extent_map from the extent tree
425 * @tree: extent tree to remove from
426 * @em: extent map being removed
427 *
428 * Removes @em from @tree. No reference counts are dropped, and no checks
429 * are done to see if the range is in use
430 */
431int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
432{
433 int ret = 0;
434
435 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
436 rb_erase(&em->rb_node, &tree->map);
437 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
438 list_del_init(&em->list);
439 RB_CLEAR_NODE(&em->rb_node);
440 return ret;
441}
442
443void replace_extent_mapping(struct extent_map_tree *tree,
444 struct extent_map *cur,
445 struct extent_map *new,
446 int modified)
447{
448 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
449 ASSERT(extent_map_in_tree(cur));
450 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
451 list_del_init(&cur->list);
452 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
453 RB_CLEAR_NODE(&cur->rb_node);
454
455 setup_extent_mapping(tree, new, modified);
456}
457
458static struct extent_map *next_extent_map(struct extent_map *em)
459{
460 struct rb_node *next;
461
462 next = rb_next(&em->rb_node);
463 if (!next)
464 return NULL;
465 return container_of(next, struct extent_map, rb_node);
466}
467
468static struct extent_map *prev_extent_map(struct extent_map *em)
469{
470 struct rb_node *prev;
471
472 prev = rb_prev(&em->rb_node);
473 if (!prev)
474 return NULL;
475 return container_of(prev, struct extent_map, rb_node);
476}
477
478/* helper for btfs_get_extent. Given an existing extent in the tree,
479 * the existing extent is the nearest extent to map_start,
480 * and an extent that you want to insert, deal with overlap and insert
481 * the best fitted new extent into the tree.
482 */
483static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
484 struct extent_map *existing,
485 struct extent_map *em,
486 u64 map_start)
487{
488 struct extent_map *prev;
489 struct extent_map *next;
490 u64 start;
491 u64 end;
492 u64 start_diff;
493
494 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
495
496 if (existing->start > map_start) {
497 next = existing;
498 prev = prev_extent_map(next);
499 } else {
500 prev = existing;
501 next = next_extent_map(prev);
502 }
503
504 start = prev ? extent_map_end(prev) : em->start;
505 start = max_t(u64, start, em->start);
506 end = next ? next->start : extent_map_end(em);
507 end = min_t(u64, end, extent_map_end(em));
508 start_diff = start - em->start;
509 em->start = start;
510 em->len = end - start;
511 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
512 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
513 em->block_start += start_diff;
514 em->block_len = em->len;
515 }
516 return add_extent_mapping(em_tree, em, 0);
517}
518
519/**
520 * btrfs_add_extent_mapping - add extent mapping into em_tree
521 * @em_tree - the extent tree into which we want to insert the extent mapping
522 * @em_in - extent we are inserting
523 * @start - start of the logical range btrfs_get_extent() is requesting
524 * @len - length of the logical range btrfs_get_extent() is requesting
525 *
526 * Note that @em_in's range may be different from [start, start+len),
527 * but they must be overlapped.
528 *
529 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
530 * the -EEXIST by either:
531 * a) Returning the existing extent in @em_in if @start is within the
532 * existing em.
533 * b) Merge the existing extent with @em_in passed in.
534 *
535 * Return 0 on success, otherwise -EEXIST.
536 *
537 */
538int btrfs_add_extent_mapping(struct extent_map_tree *em_tree,
539 struct extent_map **em_in, u64 start, u64 len)
540{
541 int ret;
542 struct extent_map *em = *em_in;
543
544 ret = add_extent_mapping(em_tree, em, 0);
545 /* it is possible that someone inserted the extent into the tree
546 * while we had the lock dropped. It is also possible that
547 * an overlapping map exists in the tree
548 */
549 if (ret == -EEXIST) {
550 struct extent_map *existing;
551
552 ret = 0;
553
554 existing = search_extent_mapping(em_tree, start, len);
555
556 trace_btrfs_handle_em_exist(existing, em, start, len);
557
558 /*
559 * existing will always be non-NULL, since there must be
560 * extent causing the -EEXIST.
561 */
562 if (start >= existing->start &&
563 start < extent_map_end(existing)) {
564 free_extent_map(em);
565 *em_in = existing;
566 ret = 0;
567 } else {
568 u64 orig_start = em->start;
569 u64 orig_len = em->len;
570
571 /*
572 * The existing extent map is the one nearest to
573 * the [start, start + len) range which overlaps
574 */
575 ret = merge_extent_mapping(em_tree, existing,
576 em, start);
577 if (ret) {
578 free_extent_map(em);
579 *em_in = NULL;
580 WARN_ONCE(ret,
581"unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
582 ret, existing->start, existing->len,
583 orig_start, orig_len);
584 }
585 free_extent_map(existing);
586 }
587 }
588
589 ASSERT(ret == 0 || ret == -EEXIST);
590 return ret;
591}