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
11 static struct kmem_cache *extent_map_cache;
12
extent_map_init(void)13 int __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
extent_map_exit(void)23 void __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 */
extent_map_tree_init(struct extent_map_tree * tree)35 void 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 */
alloc_extent_map(void)49 struct 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 */
free_extent_map(struct extent_map * em)71 void 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 */
range_end(u64 start,u64 len)86 static u64 range_end(u64 start, u64 len)
87 {
88 if (start + len < start)
89 return (u64)-1;
90 return start + len;
91 }
92
tree_insert(struct rb_root * root,struct extent_map * em)93 static 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 */
__tree_search(struct rb_root * root,u64 offset,struct rb_node ** prev_ret,struct rb_node ** next_ret)141 static 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 */
mergable_maps(struct extent_map * prev,struct extent_map * next)186 static 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
try_merge_map(struct extent_map_tree * tree,struct extent_map * em)226 static 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 /*
232 * We can't modify an extent map that is in the tree and that is being
233 * used by another task, as it can cause that other task to see it in
234 * inconsistent state during the merging. We always have 1 reference for
235 * the tree and 1 for this task (which is unpinning the extent map or
236 * clearing the logging flag), so anything > 2 means it's being used by
237 * other tasks too.
238 */
239 if (refcount_read(&em->refs) > 2)
240 return;
241
242 if (em->start != 0) {
243 rb = rb_prev(&em->rb_node);
244 if (rb)
245 merge = rb_entry(rb, struct extent_map, rb_node);
246 if (rb && mergable_maps(merge, em)) {
247 em->start = merge->start;
248 em->orig_start = merge->orig_start;
249 em->len += merge->len;
250 em->block_len += merge->block_len;
251 em->block_start = merge->block_start;
252 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
253 em->mod_start = merge->mod_start;
254 em->generation = max(em->generation, merge->generation);
255
256 rb_erase(&merge->rb_node, &tree->map);
257 RB_CLEAR_NODE(&merge->rb_node);
258 free_extent_map(merge);
259 }
260 }
261
262 rb = rb_next(&em->rb_node);
263 if (rb)
264 merge = rb_entry(rb, struct extent_map, rb_node);
265 if (rb && mergable_maps(em, merge)) {
266 em->len += merge->len;
267 em->block_len += merge->block_len;
268 rb_erase(&merge->rb_node, &tree->map);
269 RB_CLEAR_NODE(&merge->rb_node);
270 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
271 em->generation = max(em->generation, merge->generation);
272 free_extent_map(merge);
273 }
274 }
275
276 /**
277 * unpin_extent_cache - unpin an extent from the cache
278 * @tree: tree to unpin the extent in
279 * @start: logical offset in the file
280 * @len: length of the extent
281 * @gen: generation that this extent has been modified in
282 *
283 * Called after an extent has been written to disk properly. Set the generation
284 * to the generation that actually added the file item to the inode so we know
285 * we need to sync this extent when we call fsync().
286 */
unpin_extent_cache(struct extent_map_tree * tree,u64 start,u64 len,u64 gen)287 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
288 u64 gen)
289 {
290 int ret = 0;
291 struct extent_map *em;
292 bool prealloc = false;
293
294 write_lock(&tree->lock);
295 em = lookup_extent_mapping(tree, start, len);
296
297 WARN_ON(!em || em->start != start);
298
299 if (!em)
300 goto out;
301
302 em->generation = gen;
303 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
304 em->mod_start = em->start;
305 em->mod_len = em->len;
306
307 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
308 prealloc = true;
309 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
310 }
311
312 try_merge_map(tree, em);
313
314 if (prealloc) {
315 em->mod_start = em->start;
316 em->mod_len = em->len;
317 }
318
319 free_extent_map(em);
320 out:
321 write_unlock(&tree->lock);
322 return ret;
323
324 }
325
clear_em_logging(struct extent_map_tree * tree,struct extent_map * em)326 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
327 {
328 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
329 if (extent_map_in_tree(em))
330 try_merge_map(tree, em);
331 }
332
setup_extent_mapping(struct extent_map_tree * tree,struct extent_map * em,int modified)333 static inline void setup_extent_mapping(struct extent_map_tree *tree,
334 struct extent_map *em,
335 int modified)
336 {
337 refcount_inc(&em->refs);
338 em->mod_start = em->start;
339 em->mod_len = em->len;
340
341 if (modified)
342 list_move(&em->list, &tree->modified_extents);
343 else
344 try_merge_map(tree, em);
345 }
346
347 /**
348 * add_extent_mapping - add new extent map to the extent tree
349 * @tree: tree to insert new map in
350 * @em: map to insert
351 *
352 * Insert @em into @tree or perform a simple forward/backward merge with
353 * existing mappings. The extent_map struct passed in will be inserted
354 * into the tree directly, with an additional reference taken, or a
355 * reference dropped if the merge attempt was successful.
356 */
add_extent_mapping(struct extent_map_tree * tree,struct extent_map * em,int modified)357 int add_extent_mapping(struct extent_map_tree *tree,
358 struct extent_map *em, int modified)
359 {
360 int ret = 0;
361
362 ret = tree_insert(&tree->map, em);
363 if (ret)
364 goto out;
365
366 setup_extent_mapping(tree, em, modified);
367 out:
368 return ret;
369 }
370
371 static struct extent_map *
__lookup_extent_mapping(struct extent_map_tree * tree,u64 start,u64 len,int strict)372 __lookup_extent_mapping(struct extent_map_tree *tree,
373 u64 start, u64 len, int strict)
374 {
375 struct extent_map *em;
376 struct rb_node *rb_node;
377 struct rb_node *prev = NULL;
378 struct rb_node *next = NULL;
379 u64 end = range_end(start, len);
380
381 rb_node = __tree_search(&tree->map, start, &prev, &next);
382 if (!rb_node) {
383 if (prev)
384 rb_node = prev;
385 else if (next)
386 rb_node = next;
387 else
388 return NULL;
389 }
390
391 em = rb_entry(rb_node, struct extent_map, rb_node);
392
393 if (strict && !(end > em->start && start < extent_map_end(em)))
394 return NULL;
395
396 refcount_inc(&em->refs);
397 return em;
398 }
399
400 /**
401 * lookup_extent_mapping - lookup extent_map
402 * @tree: tree to lookup in
403 * @start: byte offset to start the search
404 * @len: length of the lookup range
405 *
406 * Find and return the first extent_map struct in @tree that intersects the
407 * [start, len] range. There may be additional objects in the tree that
408 * intersect, so check the object returned carefully to make sure that no
409 * additional lookups are needed.
410 */
lookup_extent_mapping(struct extent_map_tree * tree,u64 start,u64 len)411 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
412 u64 start, u64 len)
413 {
414 return __lookup_extent_mapping(tree, start, len, 1);
415 }
416
417 /**
418 * search_extent_mapping - find a nearby extent map
419 * @tree: tree to lookup in
420 * @start: byte offset to start the search
421 * @len: length of the lookup range
422 *
423 * Find and return the first extent_map struct in @tree that intersects the
424 * [start, len] range.
425 *
426 * If one can't be found, any nearby extent may be returned
427 */
search_extent_mapping(struct extent_map_tree * tree,u64 start,u64 len)428 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
429 u64 start, u64 len)
430 {
431 return __lookup_extent_mapping(tree, start, len, 0);
432 }
433
434 /**
435 * remove_extent_mapping - removes an extent_map from the extent tree
436 * @tree: extent tree to remove from
437 * @em: extent map being removed
438 *
439 * Removes @em from @tree. No reference counts are dropped, and no checks
440 * are done to see if the range is in use
441 */
remove_extent_mapping(struct extent_map_tree * tree,struct extent_map * em)442 int remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
443 {
444 int ret = 0;
445
446 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
447 rb_erase(&em->rb_node, &tree->map);
448 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
449 list_del_init(&em->list);
450 RB_CLEAR_NODE(&em->rb_node);
451 return ret;
452 }
453
replace_extent_mapping(struct extent_map_tree * tree,struct extent_map * cur,struct extent_map * new,int modified)454 void replace_extent_mapping(struct extent_map_tree *tree,
455 struct extent_map *cur,
456 struct extent_map *new,
457 int modified)
458 {
459 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
460 ASSERT(extent_map_in_tree(cur));
461 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
462 list_del_init(&cur->list);
463 rb_replace_node(&cur->rb_node, &new->rb_node, &tree->map);
464 RB_CLEAR_NODE(&cur->rb_node);
465
466 setup_extent_mapping(tree, new, modified);
467 }
468
next_extent_map(struct extent_map * em)469 static struct extent_map *next_extent_map(struct extent_map *em)
470 {
471 struct rb_node *next;
472
473 next = rb_next(&em->rb_node);
474 if (!next)
475 return NULL;
476 return container_of(next, struct extent_map, rb_node);
477 }
478
prev_extent_map(struct extent_map * em)479 static struct extent_map *prev_extent_map(struct extent_map *em)
480 {
481 struct rb_node *prev;
482
483 prev = rb_prev(&em->rb_node);
484 if (!prev)
485 return NULL;
486 return container_of(prev, struct extent_map, rb_node);
487 }
488
489 /* helper for btfs_get_extent. Given an existing extent in the tree,
490 * the existing extent is the nearest extent to map_start,
491 * and an extent that you want to insert, deal with overlap and insert
492 * the best fitted new extent into the tree.
493 */
merge_extent_mapping(struct extent_map_tree * em_tree,struct extent_map * existing,struct extent_map * em,u64 map_start)494 static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
495 struct extent_map *existing,
496 struct extent_map *em,
497 u64 map_start)
498 {
499 struct extent_map *prev;
500 struct extent_map *next;
501 u64 start;
502 u64 end;
503 u64 start_diff;
504
505 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
506
507 if (existing->start > map_start) {
508 next = existing;
509 prev = prev_extent_map(next);
510 } else {
511 prev = existing;
512 next = next_extent_map(prev);
513 }
514
515 start = prev ? extent_map_end(prev) : em->start;
516 start = max_t(u64, start, em->start);
517 end = next ? next->start : extent_map_end(em);
518 end = min_t(u64, end, extent_map_end(em));
519 start_diff = start - em->start;
520 em->start = start;
521 em->len = end - start;
522 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
523 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
524 em->block_start += start_diff;
525 em->block_len = em->len;
526 }
527 return add_extent_mapping(em_tree, em, 0);
528 }
529
530 /**
531 * btrfs_add_extent_mapping - add extent mapping into em_tree
532 * @fs_info - used for tracepoint
533 * @em_tree - the extent tree into which we want to insert the extent mapping
534 * @em_in - extent we are inserting
535 * @start - start of the logical range btrfs_get_extent() is requesting
536 * @len - length of the logical range btrfs_get_extent() is requesting
537 *
538 * Note that @em_in's range may be different from [start, start+len),
539 * but they must be overlapped.
540 *
541 * Insert @em_in into @em_tree. In case there is an overlapping range, handle
542 * the -EEXIST by either:
543 * a) Returning the existing extent in @em_in if @start is within the
544 * existing em.
545 * b) Merge the existing extent with @em_in passed in.
546 *
547 * Return 0 on success, otherwise -EEXIST.
548 *
549 */
btrfs_add_extent_mapping(struct btrfs_fs_info * fs_info,struct extent_map_tree * em_tree,struct extent_map ** em_in,u64 start,u64 len)550 int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
551 struct extent_map_tree *em_tree,
552 struct extent_map **em_in, u64 start, u64 len)
553 {
554 int ret;
555 struct extent_map *em = *em_in;
556
557 ret = add_extent_mapping(em_tree, em, 0);
558 /* it is possible that someone inserted the extent into the tree
559 * while we had the lock dropped. It is also possible that
560 * an overlapping map exists in the tree
561 */
562 if (ret == -EEXIST) {
563 struct extent_map *existing;
564
565 ret = 0;
566
567 existing = search_extent_mapping(em_tree, start, len);
568
569 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
570
571 /*
572 * existing will always be non-NULL, since there must be
573 * extent causing the -EEXIST.
574 */
575 if (start >= existing->start &&
576 start < extent_map_end(existing)) {
577 free_extent_map(em);
578 *em_in = existing;
579 ret = 0;
580 } else {
581 u64 orig_start = em->start;
582 u64 orig_len = em->len;
583
584 /*
585 * The existing extent map is the one nearest to
586 * the [start, start + len) range which overlaps
587 */
588 ret = merge_extent_mapping(em_tree, existing,
589 em, start);
590 if (ret) {
591 free_extent_map(em);
592 *em_in = NULL;
593 WARN_ONCE(ret,
594 "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
595 ret, existing->start, existing->len,
596 orig_start, orig_len);
597 }
598 free_extent_map(existing);
599 }
600 }
601
602 ASSERT(ret == 0 || ret == -EEXIST);
603 return ret;
604 }
605