perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / fs / btrfs / extent_map.c
blob7eea8b6e2cd3f5aa01c47a96642364395504995f
1 // SPDX-License-Identifier: GPL-2.0
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"
11 static struct kmem_cache *extent_map_cache;
13 int __init extent_map_init(void)
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;
23 void __cold extent_map_exit(void)
25 kmem_cache_destroy(extent_map_cache);
28 /**
29 * extent_map_tree_init - initialize extent map tree
30 * @tree: tree to initialize
32 * Initialize the extent tree @tree. Should be called for each new inode
33 * or other user of the extent_map interface.
35 void extent_map_tree_init(struct extent_map_tree *tree)
37 tree->map = RB_ROOT_CACHED;
38 INIT_LIST_HEAD(&tree->modified_extents);
39 rwlock_init(&tree->lock);
42 /**
43 * alloc_extent_map - allocate new extent map structure
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()
49 struct extent_map *alloc_extent_map(void)
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;
64 /**
65 * free_extent_map - drop reference count of an extent_map
66 * @em: extent map being released
68 * Drops the reference out on @em by one and free the structure
69 * if the reference count hits zero.
71 void free_extent_map(struct extent_map *em)
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);
85 /* simple helper to do math around the end of an extent, handling wrap */
86 static u64 range_end(u64 start, u64 len)
88 if (start + len < start)
89 return (u64)-1;
90 return start + len;
93 static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
95 struct rb_node **p = &root->rb_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 bool leftmost = true;
102 while (*p) {
103 parent = *p;
104 entry = rb_entry(parent, struct extent_map, rb_node);
106 if (em->start < entry->start) {
107 p = &(*p)->rb_left;
108 } else if (em->start >= extent_map_end(entry)) {
109 p = &(*p)->rb_right;
110 leftmost = false;
111 } else {
112 return -EEXIST;
116 orig_parent = parent;
117 while (parent && em->start >= extent_map_end(entry)) {
118 parent = rb_next(parent);
119 entry = rb_entry(parent, struct extent_map, rb_node);
121 if (parent)
122 if (end > entry->start && em->start < extent_map_end(entry))
123 return -EEXIST;
125 parent = orig_parent;
126 entry = rb_entry(parent, struct extent_map, rb_node);
127 while (parent && em->start < entry->start) {
128 parent = rb_prev(parent);
129 entry = rb_entry(parent, struct extent_map, rb_node);
131 if (parent)
132 if (end > entry->start && em->start < extent_map_end(entry))
133 return -EEXIST;
135 rb_link_node(&em->rb_node, orig_parent, p);
136 rb_insert_color_cached(&em->rb_node, root, leftmost);
137 return 0;
141 * search through the tree for an extent_map with a given offset. If
142 * it can't be found, try to find some neighboring extents
144 static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
145 struct rb_node **prev_ret,
146 struct rb_node **next_ret)
148 struct rb_node *n = root->rb_node;
149 struct rb_node *prev = NULL;
150 struct rb_node *orig_prev = NULL;
151 struct extent_map *entry;
152 struct extent_map *prev_entry = NULL;
154 while (n) {
155 entry = rb_entry(n, struct extent_map, rb_node);
156 prev = n;
157 prev_entry = entry;
159 if (offset < entry->start)
160 n = n->rb_left;
161 else if (offset >= extent_map_end(entry))
162 n = n->rb_right;
163 else
164 return n;
167 if (prev_ret) {
168 orig_prev = prev;
169 while (prev && offset >= extent_map_end(prev_entry)) {
170 prev = rb_next(prev);
171 prev_entry = rb_entry(prev, struct extent_map, rb_node);
173 *prev_ret = prev;
174 prev = orig_prev;
177 if (next_ret) {
178 prev_entry = rb_entry(prev, struct extent_map, rb_node);
179 while (prev && offset < prev_entry->start) {
180 prev = rb_prev(prev);
181 prev_entry = rb_entry(prev, struct extent_map, rb_node);
183 *next_ret = prev;
185 return NULL;
188 /* check to see if two extent_map structs are adjacent and safe to merge */
189 static int mergable_maps(struct extent_map *prev, struct extent_map *next)
191 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
192 return 0;
195 * don't merge compressed extents, we need to know their
196 * actual size
198 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
199 return 0;
201 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
202 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
203 return 0;
206 * We don't want to merge stuff that hasn't been written to the log yet
207 * since it may not reflect exactly what is on disk, and that would be
208 * bad.
210 if (!list_empty(&prev->list) || !list_empty(&next->list))
211 return 0;
213 if (extent_map_end(prev) == next->start &&
214 prev->flags == next->flags &&
215 prev->bdev == next->bdev &&
216 ((next->block_start == EXTENT_MAP_HOLE &&
217 prev->block_start == EXTENT_MAP_HOLE) ||
218 (next->block_start == EXTENT_MAP_INLINE &&
219 prev->block_start == EXTENT_MAP_INLINE) ||
220 (next->block_start == EXTENT_MAP_DELALLOC &&
221 prev->block_start == EXTENT_MAP_DELALLOC) ||
222 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
223 next->block_start == extent_map_block_end(prev)))) {
224 return 1;
226 return 0;
229 static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
231 struct extent_map *merge = NULL;
232 struct rb_node *rb;
234 if (em->start != 0) {
235 rb = rb_prev(&em->rb_node);
236 if (rb)
237 merge = rb_entry(rb, struct extent_map, rb_node);
238 if (rb && mergable_maps(merge, em)) {
239 em->start = merge->start;
240 em->orig_start = merge->orig_start;
241 em->len += merge->len;
242 em->block_len += merge->block_len;
243 em->block_start = merge->block_start;
244 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
245 em->mod_start = merge->mod_start;
246 em->generation = max(em->generation, merge->generation);
248 rb_erase_cached(&merge->rb_node, &tree->map);
249 RB_CLEAR_NODE(&merge->rb_node);
250 free_extent_map(merge);
254 rb = rb_next(&em->rb_node);
255 if (rb)
256 merge = rb_entry(rb, struct extent_map, rb_node);
257 if (rb && mergable_maps(em, merge)) {
258 em->len += merge->len;
259 em->block_len += merge->block_len;
260 rb_erase_cached(&merge->rb_node, &tree->map);
261 RB_CLEAR_NODE(&merge->rb_node);
262 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
263 em->generation = max(em->generation, merge->generation);
264 free_extent_map(merge);
269 * unpin_extent_cache - unpin an extent from the cache
270 * @tree: tree to unpin the extent in
271 * @start: logical offset in the file
272 * @len: length of the extent
273 * @gen: generation that this extent has been modified in
275 * Called after an extent has been written to disk properly. Set the generation
276 * to the generation that actually added the file item to the inode so we know
277 * we need to sync this extent when we call fsync().
279 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
280 u64 gen)
282 int ret = 0;
283 struct extent_map *em;
284 bool prealloc = false;
286 write_lock(&tree->lock);
287 em = lookup_extent_mapping(tree, start, len);
289 WARN_ON(!em || em->start != start);
291 if (!em)
292 goto out;
294 em->generation = gen;
295 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
296 em->mod_start = em->start;
297 em->mod_len = em->len;
299 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
300 prealloc = true;
301 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
304 try_merge_map(tree, em);
306 if (prealloc) {
307 em->mod_start = em->start;
308 em->mod_len = em->len;
311 free_extent_map(em);
312 out:
313 write_unlock(&tree->lock);
314 return ret;
318 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
320 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
321 if (extent_map_in_tree(em))
322 try_merge_map(tree, em);
325 static inline void setup_extent_mapping(struct extent_map_tree *tree,
326 struct extent_map *em,
327 int modified)
329 refcount_inc(&em->refs);
330 em->mod_start = em->start;
331 em->mod_len = em->len;
333 if (modified)
334 list_move(&em->list, &tree->modified_extents);
335 else
336 try_merge_map(tree, em);
340 * add_extent_mapping - add new extent map to the extent tree
341 * @tree: tree to insert new map in
342 * @em: map to insert
344 * Insert @em into @tree or perform a simple forward/backward merge with
345 * existing mappings. The extent_map struct passed in will be inserted
346 * into the tree directly, with an additional reference taken, or a
347 * reference dropped if the merge attempt was successful.
349 int add_extent_mapping(struct extent_map_tree *tree,
350 struct extent_map *em, int modified)
352 int ret = 0;
354 ret = tree_insert(&tree->map, em);
355 if (ret)
356 goto out;
358 setup_extent_mapping(tree, em, modified);
359 out:
360 return ret;
363 static struct extent_map *
364 __lookup_extent_mapping(struct extent_map_tree *tree,
365 u64 start, u64 len, int strict)
367 struct extent_map *em;
368 struct rb_node *rb_node;
369 struct rb_node *prev = NULL;
370 struct rb_node *next = NULL;
371 u64 end = range_end(start, len);
373 rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
374 if (!rb_node) {
375 if (prev)
376 rb_node = prev;
377 else if (next)
378 rb_node = next;
379 else
380 return NULL;
383 em = rb_entry(rb_node, struct extent_map, rb_node);
385 if (strict && !(end > em->start && start < extent_map_end(em)))
386 return NULL;
388 refcount_inc(&em->refs);
389 return em;
393 * lookup_extent_mapping - lookup extent_map
394 * @tree: tree to lookup in
395 * @start: byte offset to start the search
396 * @len: length of the lookup range
398 * Find and return the first extent_map struct in @tree that intersects the
399 * [start, len] range. There may be additional objects in the tree that
400 * intersect, so check the object returned carefully to make sure that no
401 * additional lookups are needed.
403 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
404 u64 start, u64 len)
406 return __lookup_extent_mapping(tree, start, len, 1);
410 * search_extent_mapping - find a nearby extent map
411 * @tree: tree to lookup in
412 * @start: byte offset to start the search
413 * @len: length of the lookup range
415 * Find and return the first extent_map struct in @tree that intersects the
416 * [start, len] range.
418 * If one can't be found, any nearby extent may be returned
420 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
421 u64 start, u64 len)
423 return __lookup_extent_mapping(tree, start, len, 0);
427 * remove_extent_mapping - removes an extent_map from the extent tree
428 * @tree: extent tree to remove from
429 * @em: extent map being removed
431 * Removes @em from @tree. No reference counts are dropped, and no checks
432 * are done to see if the range is in use
434 void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
436 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
437 rb_erase_cached(&em->rb_node, &tree->map);
438 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
439 list_del_init(&em->list);
440 RB_CLEAR_NODE(&em->rb_node);
443 void replace_extent_mapping(struct extent_map_tree *tree,
444 struct extent_map *cur,
445 struct extent_map *new,
446 int modified)
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_cached(&cur->rb_node, &new->rb_node, &tree->map);
453 RB_CLEAR_NODE(&cur->rb_node);
455 setup_extent_mapping(tree, new, modified);
458 static struct extent_map *next_extent_map(struct extent_map *em)
460 struct rb_node *next;
462 next = rb_next(&em->rb_node);
463 if (!next)
464 return NULL;
465 return container_of(next, struct extent_map, rb_node);
468 static struct extent_map *prev_extent_map(struct extent_map *em)
470 struct rb_node *prev;
472 prev = rb_prev(&em->rb_node);
473 if (!prev)
474 return NULL;
475 return container_of(prev, struct extent_map, rb_node);
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.
483 static 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)
488 struct extent_map *prev;
489 struct extent_map *next;
490 u64 start;
491 u64 end;
492 u64 start_diff;
494 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
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);
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;
516 return add_extent_mapping(em_tree, em, 0);
520 * btrfs_add_extent_mapping - add extent mapping into em_tree
521 * @fs_info - used for tracepoint
522 * @em_tree - the extent tree into which we want to insert the extent mapping
523 * @em_in - extent we are inserting
524 * @start - start of the logical range btrfs_get_extent() is requesting
525 * @len - length of the logical range btrfs_get_extent() is requesting
527 * Note that @em_in's range may be different from [start, start+len),
528 * but they must be overlapped.
530 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
531 * the -EEXIST by either:
532 * a) Returning the existing extent in @em_in if @start is within the
533 * existing em.
534 * b) Merge the existing extent with @em_in passed in.
536 * Return 0 on success, otherwise -EEXIST.
539 int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
540 struct extent_map_tree *em_tree,
541 struct extent_map **em_in, u64 start, u64 len)
543 int ret;
544 struct extent_map *em = *em_in;
546 ret = add_extent_mapping(em_tree, em, 0);
547 /* it is possible that someone inserted the extent into the tree
548 * while we had the lock dropped. It is also possible that
549 * an overlapping map exists in the tree
551 if (ret == -EEXIST) {
552 struct extent_map *existing;
554 ret = 0;
556 existing = search_extent_mapping(em_tree, start, len);
558 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
561 * existing will always be non-NULL, since there must be
562 * extent causing the -EEXIST.
564 if (start >= existing->start &&
565 start < extent_map_end(existing)) {
566 free_extent_map(em);
567 *em_in = existing;
568 ret = 0;
569 } else {
570 u64 orig_start = em->start;
571 u64 orig_len = em->len;
574 * The existing extent map is the one nearest to
575 * the [start, start + len) range which overlaps
577 ret = merge_extent_mapping(em_tree, existing,
578 em, start);
579 if (ret) {
580 free_extent_map(em);
581 *em_in = NULL;
582 WARN_ONCE(ret,
583 "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
584 ret, existing->start, existing->len,
585 orig_start, orig_len);
587 free_extent_map(existing);
591 ASSERT(ret == 0 || ret == -EEXIST);
592 return ret;