Mention branches and keyring.
[releases.git] / btrfs / extent_map.c
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 "volumes.h"
8 #include "extent_map.h"
9 #include "compression.h"
10 #include "btrfs_inode.h"
11
12
13 static struct kmem_cache *extent_map_cache;
14
15 int __init extent_map_init(void)
16 {
17         extent_map_cache = kmem_cache_create("btrfs_extent_map",
18                         sizeof(struct extent_map), 0,
19                         SLAB_MEM_SPREAD, NULL);
20         if (!extent_map_cache)
21                 return -ENOMEM;
22         return 0;
23 }
24
25 void __cold extent_map_exit(void)
26 {
27         kmem_cache_destroy(extent_map_cache);
28 }
29
30 /**
31  * extent_map_tree_init - initialize extent map tree
32  * @tree:               tree to initialize
33  *
34  * Initialize the extent tree @tree.  Should be called for each new inode
35  * or other user of the extent_map interface.
36  */
37 void extent_map_tree_init(struct extent_map_tree *tree)
38 {
39         tree->map = RB_ROOT_CACHED;
40         INIT_LIST_HEAD(&tree->modified_extents);
41         rwlock_init(&tree->lock);
42 }
43
44 /**
45  * alloc_extent_map - allocate new extent map structure
46  *
47  * Allocate a new extent_map structure.  The new structure is
48  * returned with a reference count of one and needs to be
49  * freed using free_extent_map()
50  */
51 struct extent_map *alloc_extent_map(void)
52 {
53         struct extent_map *em;
54         em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
55         if (!em)
56                 return NULL;
57         RB_CLEAR_NODE(&em->rb_node);
58         em->compress_type = BTRFS_COMPRESS_NONE;
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  */
71 void free_extent_map(struct extent_map *em)
72 {
73         if (!em)
74                 return;
75         if (refcount_dec_and_test(&em->refs)) {
76                 WARN_ON(extent_map_in_tree(em));
77                 WARN_ON(!list_empty(&em->list));
78                 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
79                         kfree(em->map_lookup);
80                 kmem_cache_free(extent_map_cache, em);
81         }
82 }
83
84 /* simple helper to do math around the end of an extent, handling wrap */
85 static u64 range_end(u64 start, u64 len)
86 {
87         if (start + len < start)
88                 return (u64)-1;
89         return start + len;
90 }
91
92 static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
93 {
94         struct rb_node **p = &root->rb_root.rb_node;
95         struct rb_node *parent = NULL;
96         struct extent_map *entry = NULL;
97         struct rb_node *orig_parent = NULL;
98         u64 end = range_end(em->start, em->len);
99         bool leftmost = true;
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                         leftmost = false;
110                 } else {
111                         return -EEXIST;
112                 }
113         }
114
115         orig_parent = parent;
116         while (parent && em->start >= extent_map_end(entry)) {
117                 parent = rb_next(parent);
118                 entry = rb_entry(parent, struct extent_map, rb_node);
119         }
120         if (parent)
121                 if (end > entry->start && em->start < extent_map_end(entry))
122                         return -EEXIST;
123
124         parent = orig_parent;
125         entry = rb_entry(parent, struct extent_map, rb_node);
126         while (parent && em->start < entry->start) {
127                 parent = rb_prev(parent);
128                 entry = rb_entry(parent, struct extent_map, rb_node);
129         }
130         if (parent)
131                 if (end > entry->start && em->start < extent_map_end(entry))
132                         return -EEXIST;
133
134         rb_link_node(&em->rb_node, orig_parent, p);
135         rb_insert_color_cached(&em->rb_node, root, leftmost);
136         return 0;
137 }
138
139 /*
140  * search through the tree for an extent_map with a given offset.  If
141  * it can't be found, try to find some neighboring extents
142  */
143 static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
144                                      struct rb_node **prev_or_next_ret)
145 {
146         struct rb_node *n = root->rb_node;
147         struct rb_node *prev = NULL;
148         struct rb_node *orig_prev = NULL;
149         struct extent_map *entry;
150         struct extent_map *prev_entry = NULL;
151
152         ASSERT(prev_or_next_ret);
153
154         while (n) {
155                 entry = rb_entry(n, struct extent_map, rb_node);
156                 prev = n;
157                 prev_entry = entry;
158
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;
165         }
166
167         orig_prev = prev;
168         while (prev && offset >= extent_map_end(prev_entry)) {
169                 prev = rb_next(prev);
170                 prev_entry = rb_entry(prev, struct extent_map, rb_node);
171         }
172
173         /*
174          * Previous extent map found, return as in this case the caller does not
175          * care about the next one.
176          */
177         if (prev) {
178                 *prev_or_next_ret = prev;
179                 return NULL;
180         }
181
182         prev = orig_prev;
183         prev_entry = rb_entry(prev, struct extent_map, rb_node);
184         while (prev && offset < prev_entry->start) {
185                 prev = rb_prev(prev);
186                 prev_entry = rb_entry(prev, struct extent_map, rb_node);
187         }
188         *prev_or_next_ret = prev;
189
190         return NULL;
191 }
192
193 /* check to see if two extent_map structs are adjacent and safe to merge */
194 static int mergable_maps(struct extent_map *prev, struct extent_map *next)
195 {
196         if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
197                 return 0;
198
199         /*
200          * don't merge compressed extents, we need to know their
201          * actual size
202          */
203         if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
204                 return 0;
205
206         if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
207             test_bit(EXTENT_FLAG_LOGGING, &next->flags))
208                 return 0;
209
210         /*
211          * We don't want to merge stuff that hasn't been written to the log yet
212          * since it may not reflect exactly what is on disk, and that would be
213          * bad.
214          */
215         if (!list_empty(&prev->list) || !list_empty(&next->list))
216                 return 0;
217
218         ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
219                prev->block_start != EXTENT_MAP_DELALLOC);
220
221         if (prev->map_lookup || next->map_lookup)
222                 ASSERT(test_bit(EXTENT_FLAG_FS_MAPPING, &prev->flags) &&
223                        test_bit(EXTENT_FLAG_FS_MAPPING, &next->flags));
224
225         if (extent_map_end(prev) == next->start &&
226             prev->flags == next->flags &&
227             prev->map_lookup == next->map_lookup &&
228             ((next->block_start == EXTENT_MAP_HOLE &&
229               prev->block_start == EXTENT_MAP_HOLE) ||
230              (next->block_start == EXTENT_MAP_INLINE &&
231               prev->block_start == EXTENT_MAP_INLINE) ||
232              (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
233               next->block_start == extent_map_block_end(prev)))) {
234                 return 1;
235         }
236         return 0;
237 }
238
239 static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
240 {
241         struct extent_map *merge = NULL;
242         struct rb_node *rb;
243
244         /*
245          * We can't modify an extent map that is in the tree and that is being
246          * used by another task, as it can cause that other task to see it in
247          * inconsistent state during the merging. We always have 1 reference for
248          * the tree and 1 for this task (which is unpinning the extent map or
249          * clearing the logging flag), so anything > 2 means it's being used by
250          * other tasks too.
251          */
252         if (refcount_read(&em->refs) > 2)
253                 return;
254
255         if (em->start != 0) {
256                 rb = rb_prev(&em->rb_node);
257                 if (rb)
258                         merge = rb_entry(rb, struct extent_map, rb_node);
259                 if (rb && mergable_maps(merge, em)) {
260                         em->start = merge->start;
261                         em->orig_start = merge->orig_start;
262                         em->len += merge->len;
263                         em->block_len += merge->block_len;
264                         em->block_start = merge->block_start;
265                         em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
266                         em->mod_start = merge->mod_start;
267                         em->generation = max(em->generation, merge->generation);
268                         set_bit(EXTENT_FLAG_MERGED, &em->flags);
269
270                         rb_erase_cached(&merge->rb_node, &tree->map);
271                         RB_CLEAR_NODE(&merge->rb_node);
272                         free_extent_map(merge);
273                 }
274         }
275
276         rb = rb_next(&em->rb_node);
277         if (rb)
278                 merge = rb_entry(rb, struct extent_map, rb_node);
279         if (rb && mergable_maps(em, merge)) {
280                 em->len += merge->len;
281                 em->block_len += merge->block_len;
282                 rb_erase_cached(&merge->rb_node, &tree->map);
283                 RB_CLEAR_NODE(&merge->rb_node);
284                 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
285                 em->generation = max(em->generation, merge->generation);
286                 set_bit(EXTENT_FLAG_MERGED, &em->flags);
287                 free_extent_map(merge);
288         }
289 }
290
291 /**
292  * unpin_extent_cache - unpin an extent from the cache
293  * @tree:       tree to unpin the extent in
294  * @start:      logical offset in the file
295  * @len:        length of the extent
296  * @gen:        generation that this extent has been modified in
297  *
298  * Called after an extent has been written to disk properly.  Set the generation
299  * to the generation that actually added the file item to the inode so we know
300  * we need to sync this extent when we call fsync().
301  */
302 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
303                        u64 gen)
304 {
305         int ret = 0;
306         struct extent_map *em;
307         bool prealloc = false;
308
309         write_lock(&tree->lock);
310         em = lookup_extent_mapping(tree, start, len);
311
312         WARN_ON(!em || em->start != start);
313
314         if (!em)
315                 goto out;
316
317         em->generation = gen;
318         clear_bit(EXTENT_FLAG_PINNED, &em->flags);
319         em->mod_start = em->start;
320         em->mod_len = em->len;
321
322         if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
323                 prealloc = true;
324                 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
325         }
326
327         try_merge_map(tree, em);
328
329         if (prealloc) {
330                 em->mod_start = em->start;
331                 em->mod_len = em->len;
332         }
333
334         free_extent_map(em);
335 out:
336         write_unlock(&tree->lock);
337         return ret;
338
339 }
340
341 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
342 {
343         lockdep_assert_held_write(&tree->lock);
344
345         clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
346         if (extent_map_in_tree(em))
347                 try_merge_map(tree, em);
348 }
349
350 static inline void setup_extent_mapping(struct extent_map_tree *tree,
351                                         struct extent_map *em,
352                                         int modified)
353 {
354         refcount_inc(&em->refs);
355         em->mod_start = em->start;
356         em->mod_len = em->len;
357
358         if (modified)
359                 list_move(&em->list, &tree->modified_extents);
360         else
361                 try_merge_map(tree, em);
362 }
363
364 static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
365 {
366         struct map_lookup *map = em->map_lookup;
367         u64 stripe_size = em->orig_block_len;
368         int i;
369
370         for (i = 0; i < map->num_stripes; i++) {
371                 struct btrfs_io_stripe *stripe = &map->stripes[i];
372                 struct btrfs_device *device = stripe->dev;
373
374                 set_extent_bits_nowait(&device->alloc_state, stripe->physical,
375                                  stripe->physical + stripe_size - 1, bits);
376         }
377 }
378
379 static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
380 {
381         struct map_lookup *map = em->map_lookup;
382         u64 stripe_size = em->orig_block_len;
383         int i;
384
385         for (i = 0; i < map->num_stripes; i++) {
386                 struct btrfs_io_stripe *stripe = &map->stripes[i];
387                 struct btrfs_device *device = stripe->dev;
388
389                 __clear_extent_bit(&device->alloc_state, stripe->physical,
390                                    stripe->physical + stripe_size - 1, bits,
391                                    NULL, GFP_NOWAIT, NULL);
392         }
393 }
394
395 /**
396  * Add new extent map to the extent tree
397  *
398  * @tree:       tree to insert new map in
399  * @em:         map to insert
400  * @modified:   indicate whether the given @em should be added to the
401  *              modified list, which indicates the extent needs to be logged
402  *
403  * Insert @em into @tree or perform a simple forward/backward merge with
404  * existing mappings.  The extent_map struct passed in will be inserted
405  * into the tree directly, with an additional reference taken, or a
406  * reference dropped if the merge attempt was successful.
407  */
408 int add_extent_mapping(struct extent_map_tree *tree,
409                        struct extent_map *em, int modified)
410 {
411         int ret = 0;
412
413         lockdep_assert_held_write(&tree->lock);
414
415         ret = tree_insert(&tree->map, em);
416         if (ret)
417                 goto out;
418
419         setup_extent_mapping(tree, em, modified);
420         if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
421                 extent_map_device_set_bits(em, CHUNK_ALLOCATED);
422                 extent_map_device_clear_bits(em, CHUNK_TRIMMED);
423         }
424 out:
425         return ret;
426 }
427
428 static struct extent_map *
429 __lookup_extent_mapping(struct extent_map_tree *tree,
430                         u64 start, u64 len, int strict)
431 {
432         struct extent_map *em;
433         struct rb_node *rb_node;
434         struct rb_node *prev_or_next = NULL;
435         u64 end = range_end(start, len);
436
437         rb_node = __tree_search(&tree->map.rb_root, start, &prev_or_next);
438         if (!rb_node) {
439                 if (prev_or_next)
440                         rb_node = prev_or_next;
441                 else
442                         return NULL;
443         }
444
445         em = rb_entry(rb_node, struct extent_map, rb_node);
446
447         if (strict && !(end > em->start && start < extent_map_end(em)))
448                 return NULL;
449
450         refcount_inc(&em->refs);
451         return em;
452 }
453
454 /**
455  * lookup_extent_mapping - lookup extent_map
456  * @tree:       tree to lookup in
457  * @start:      byte offset to start the search
458  * @len:        length of the lookup range
459  *
460  * Find and return the first extent_map struct in @tree that intersects the
461  * [start, len] range.  There may be additional objects in the tree that
462  * intersect, so check the object returned carefully to make sure that no
463  * additional lookups are needed.
464  */
465 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
466                                          u64 start, u64 len)
467 {
468         return __lookup_extent_mapping(tree, start, len, 1);
469 }
470
471 /**
472  * search_extent_mapping - find a nearby extent map
473  * @tree:       tree to lookup in
474  * @start:      byte offset to start the search
475  * @len:        length of the lookup range
476  *
477  * Find and return the first extent_map struct in @tree that intersects the
478  * [start, len] range.
479  *
480  * If one can't be found, any nearby extent may be returned
481  */
482 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
483                                          u64 start, u64 len)
484 {
485         return __lookup_extent_mapping(tree, start, len, 0);
486 }
487
488 /**
489  * remove_extent_mapping - removes an extent_map from the extent tree
490  * @tree:       extent tree to remove from
491  * @em:         extent map being removed
492  *
493  * Removes @em from @tree.  No reference counts are dropped, and no checks
494  * are done to see if the range is in use
495  */
496 void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
497 {
498         lockdep_assert_held_write(&tree->lock);
499
500         WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
501         rb_erase_cached(&em->rb_node, &tree->map);
502         if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
503                 list_del_init(&em->list);
504         if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
505                 extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
506         RB_CLEAR_NODE(&em->rb_node);
507 }
508
509 void replace_extent_mapping(struct extent_map_tree *tree,
510                             struct extent_map *cur,
511                             struct extent_map *new,
512                             int modified)
513 {
514         lockdep_assert_held_write(&tree->lock);
515
516         WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
517         ASSERT(extent_map_in_tree(cur));
518         if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
519                 list_del_init(&cur->list);
520         rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
521         RB_CLEAR_NODE(&cur->rb_node);
522
523         setup_extent_mapping(tree, new, modified);
524 }
525
526 static struct extent_map *next_extent_map(const struct extent_map *em)
527 {
528         struct rb_node *next;
529
530         next = rb_next(&em->rb_node);
531         if (!next)
532                 return NULL;
533         return container_of(next, struct extent_map, rb_node);
534 }
535
536 /*
537  * Get the extent map that immediately follows another one.
538  *
539  * @tree:       The extent map tree that the extent map belong to.
540  *              Holding read or write access on the tree's lock is required.
541  * @em:         An extent map from the given tree. The caller must ensure that
542  *              between getting @em and between calling this function, the
543  *              extent map @em is not removed from the tree - for example, by
544  *              holding the tree's lock for the duration of those 2 operations.
545  *
546  * Returns the extent map that immediately follows @em, or NULL if @em is the
547  * last extent map in the tree.
548  */
549 struct extent_map *btrfs_next_extent_map(const struct extent_map_tree *tree,
550                                          const struct extent_map *em)
551 {
552         struct extent_map *next;
553
554         /* The lock must be acquired either in read mode or write mode. */
555         lockdep_assert_held(&tree->lock);
556         ASSERT(extent_map_in_tree(em));
557
558         next = next_extent_map(em);
559         if (next)
560                 refcount_inc(&next->refs);
561
562         return next;
563 }
564
565 static struct extent_map *prev_extent_map(struct extent_map *em)
566 {
567         struct rb_node *prev;
568
569         prev = rb_prev(&em->rb_node);
570         if (!prev)
571                 return NULL;
572         return container_of(prev, struct extent_map, rb_node);
573 }
574
575 /*
576  * Helper for btrfs_get_extent.  Given an existing extent in the tree,
577  * the existing extent is the nearest extent to map_start,
578  * and an extent that you want to insert, deal with overlap and insert
579  * the best fitted new extent into the tree.
580  */
581 static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
582                                          struct extent_map *existing,
583                                          struct extent_map *em,
584                                          u64 map_start)
585 {
586         struct extent_map *prev;
587         struct extent_map *next;
588         u64 start;
589         u64 end;
590         u64 start_diff;
591
592         BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
593
594         if (existing->start > map_start) {
595                 next = existing;
596                 prev = prev_extent_map(next);
597         } else {
598                 prev = existing;
599                 next = next_extent_map(prev);
600         }
601
602         start = prev ? extent_map_end(prev) : em->start;
603         start = max_t(u64, start, em->start);
604         end = next ? next->start : extent_map_end(em);
605         end = min_t(u64, end, extent_map_end(em));
606         start_diff = start - em->start;
607         em->start = start;
608         em->len = end - start;
609         if (em->block_start < EXTENT_MAP_LAST_BYTE &&
610             !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
611                 em->block_start += start_diff;
612                 em->block_len = em->len;
613         }
614         return add_extent_mapping(em_tree, em, 0);
615 }
616
617 /**
618  * Add extent mapping into em_tree
619  *
620  * @fs_info:  the filesystem
621  * @em_tree:  extent tree into which we want to insert the extent mapping
622  * @em_in:    extent we are inserting
623  * @start:    start of the logical range btrfs_get_extent() is requesting
624  * @len:      length of the logical range btrfs_get_extent() is requesting
625  *
626  * Note that @em_in's range may be different from [start, start+len),
627  * but they must be overlapped.
628  *
629  * Insert @em_in into @em_tree. In case there is an overlapping range, handle
630  * the -EEXIST by either:
631  * a) Returning the existing extent in @em_in if @start is within the
632  *    existing em.
633  * b) Merge the existing extent with @em_in passed in.
634  *
635  * Return 0 on success, otherwise -EEXIST.
636  *
637  */
638 int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
639                              struct extent_map_tree *em_tree,
640                              struct extent_map **em_in, u64 start, u64 len)
641 {
642         int ret;
643         struct extent_map *em = *em_in;
644
645         ret = add_extent_mapping(em_tree, em, 0);
646         /* it is possible that someone inserted the extent into the tree
647          * while we had the lock dropped.  It is also possible that
648          * an overlapping map exists in the tree
649          */
650         if (ret == -EEXIST) {
651                 struct extent_map *existing;
652
653                 ret = 0;
654
655                 existing = search_extent_mapping(em_tree, start, len);
656
657                 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
658
659                 /*
660                  * existing will always be non-NULL, since there must be
661                  * extent causing the -EEXIST.
662                  */
663                 if (start >= existing->start &&
664                     start < extent_map_end(existing)) {
665                         free_extent_map(em);
666                         *em_in = existing;
667                         ret = 0;
668                 } else {
669                         u64 orig_start = em->start;
670                         u64 orig_len = em->len;
671
672                         /*
673                          * The existing extent map is the one nearest to
674                          * the [start, start + len) range which overlaps
675                          */
676                         ret = merge_extent_mapping(em_tree, existing,
677                                                    em, start);
678                         if (ret) {
679                                 free_extent_map(em);
680                                 *em_in = NULL;
681                                 WARN_ONCE(ret,
682 "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
683                                           ret, existing->start, existing->len,
684                                           orig_start, orig_len);
685                         }
686                         free_extent_map(existing);
687                 }
688         }
689
690         ASSERT(ret == 0 || ret == -EEXIST);
691         return ret;
692 }
693
694 /*
695  * Drop all extent maps from a tree in the fastest possible way, rescheduling
696  * if needed. This avoids searching the tree, from the root down to the first
697  * extent map, before each deletion.
698  */
699 static void drop_all_extent_maps_fast(struct extent_map_tree *tree)
700 {
701         write_lock(&tree->lock);
702         while (!RB_EMPTY_ROOT(&tree->map.rb_root)) {
703                 struct extent_map *em;
704                 struct rb_node *node;
705
706                 node = rb_first_cached(&tree->map);
707                 em = rb_entry(node, struct extent_map, rb_node);
708                 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
709                 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
710                 remove_extent_mapping(tree, em);
711                 free_extent_map(em);
712                 cond_resched_rwlock_write(&tree->lock);
713         }
714         write_unlock(&tree->lock);
715 }
716
717 /*
718  * Drop all extent maps in a given range.
719  *
720  * @inode:       The target inode.
721  * @start:       Start offset of the range.
722  * @end:         End offset of the range (inclusive value).
723  * @skip_pinned: Indicate if pinned extent maps should be ignored or not.
724  *
725  * This drops all the extent maps that intersect the given range [@start, @end].
726  * Extent maps that partially overlap the range and extend behind or beyond it,
727  * are split.
728  * The caller should have locked an appropriate file range in the inode's io
729  * tree before calling this function.
730  */
731 void btrfs_drop_extent_map_range(struct btrfs_inode *inode, u64 start, u64 end,
732                                  bool skip_pinned)
733 {
734         struct extent_map *split;
735         struct extent_map *split2;
736         struct extent_map *em;
737         struct extent_map_tree *em_tree = &inode->extent_tree;
738         u64 len = end - start + 1;
739
740         WARN_ON(end < start);
741         if (end == (u64)-1) {
742                 if (start == 0 && !skip_pinned) {
743                         drop_all_extent_maps_fast(em_tree);
744                         return;
745                 }
746                 len = (u64)-1;
747         } else {
748                 /* Make end offset exclusive for use in the loop below. */
749                 end++;
750         }
751
752         /*
753          * It's ok if we fail to allocate the extent maps, see the comment near
754          * the bottom of the loop below. We only need two spare extent maps in
755          * the worst case, where the first extent map that intersects our range
756          * starts before the range and the last extent map that intersects our
757          * range ends after our range (and they might be the same extent map),
758          * because we need to split those two extent maps at the boundaries.
759          */
760         split = alloc_extent_map();
761         split2 = alloc_extent_map();
762
763         write_lock(&em_tree->lock);
764         em = lookup_extent_mapping(em_tree, start, len);
765
766         while (em) {
767                 /* extent_map_end() returns exclusive value (last byte + 1). */
768                 const u64 em_end = extent_map_end(em);
769                 struct extent_map *next_em = NULL;
770                 u64 gen;
771                 unsigned long flags;
772                 bool modified;
773                 bool compressed;
774
775                 if (em_end < end) {
776                         next_em = next_extent_map(em);
777                         if (next_em) {
778                                 if (next_em->start < end)
779                                         refcount_inc(&next_em->refs);
780                                 else
781                                         next_em = NULL;
782                         }
783                 }
784
785                 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
786                         start = em_end;
787                         goto next;
788                 }
789
790                 flags = em->flags;
791                 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
792                 /*
793                  * In case we split the extent map, we want to preserve the
794                  * EXTENT_FLAG_LOGGING flag on our extent map, but we don't want
795                  * it on the new extent maps.
796                  */
797                 clear_bit(EXTENT_FLAG_LOGGING, &flags);
798                 modified = !list_empty(&em->list);
799
800                 /*
801                  * The extent map does not cross our target range, so no need to
802                  * split it, we can remove it directly.
803                  */
804                 if (em->start >= start && em_end <= end)
805                         goto remove_em;
806
807                 gen = em->generation;
808                 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
809
810                 if (em->start < start) {
811                         if (!split) {
812                                 split = split2;
813                                 split2 = NULL;
814                                 if (!split)
815                                         goto remove_em;
816                         }
817                         split->start = em->start;
818                         split->len = start - em->start;
819
820                         if (em->block_start < EXTENT_MAP_LAST_BYTE) {
821                                 split->orig_start = em->orig_start;
822                                 split->block_start = em->block_start;
823
824                                 if (compressed)
825                                         split->block_len = em->block_len;
826                                 else
827                                         split->block_len = split->len;
828                                 split->orig_block_len = max(split->block_len,
829                                                 em->orig_block_len);
830                                 split->ram_bytes = em->ram_bytes;
831                         } else {
832                                 split->orig_start = split->start;
833                                 split->block_len = 0;
834                                 split->block_start = em->block_start;
835                                 split->orig_block_len = 0;
836                                 split->ram_bytes = split->len;
837                         }
838
839                         split->generation = gen;
840                         split->flags = flags;
841                         split->compress_type = em->compress_type;
842                         replace_extent_mapping(em_tree, em, split, modified);
843                         free_extent_map(split);
844                         split = split2;
845                         split2 = NULL;
846                 }
847                 if (em_end > end) {
848                         if (!split) {
849                                 split = split2;
850                                 split2 = NULL;
851                                 if (!split)
852                                         goto remove_em;
853                         }
854                         split->start = end;
855                         split->len = em_end - end;
856                         split->block_start = em->block_start;
857                         split->flags = flags;
858                         split->compress_type = em->compress_type;
859                         split->generation = gen;
860
861                         if (em->block_start < EXTENT_MAP_LAST_BYTE) {
862                                 split->orig_block_len = max(em->block_len,
863                                                     em->orig_block_len);
864
865                                 split->ram_bytes = em->ram_bytes;
866                                 if (compressed) {
867                                         split->block_len = em->block_len;
868                                         split->orig_start = em->orig_start;
869                                 } else {
870                                         const u64 diff = start + len - em->start;
871
872                                         split->block_len = split->len;
873                                         split->block_start += diff;
874                                         split->orig_start = em->orig_start;
875                                 }
876                         } else {
877                                 split->ram_bytes = split->len;
878                                 split->orig_start = split->start;
879                                 split->block_len = 0;
880                                 split->orig_block_len = 0;
881                         }
882
883                         if (extent_map_in_tree(em)) {
884                                 replace_extent_mapping(em_tree, em, split,
885                                                        modified);
886                         } else {
887                                 int ret;
888
889                                 ret = add_extent_mapping(em_tree, split,
890                                                          modified);
891                                 /* Logic error, shouldn't happen. */
892                                 ASSERT(ret == 0);
893                                 if (WARN_ON(ret != 0) && modified)
894                                         btrfs_set_inode_full_sync(inode);
895                         }
896                         free_extent_map(split);
897                         split = NULL;
898                 }
899 remove_em:
900                 if (extent_map_in_tree(em)) {
901                         /*
902                          * If the extent map is still in the tree it means that
903                          * either of the following is true:
904                          *
905                          * 1) It fits entirely in our range (doesn't end beyond
906                          *    it or starts before it);
907                          *
908                          * 2) It starts before our range and/or ends after our
909                          *    range, and we were not able to allocate the extent
910                          *    maps for split operations, @split and @split2.
911                          *
912                          * If we are at case 2) then we just remove the entire
913                          * extent map - this is fine since if anyone needs it to
914                          * access the subranges outside our range, will just
915                          * load it again from the subvolume tree's file extent
916                          * item. However if the extent map was in the list of
917                          * modified extents, then we must mark the inode for a
918                          * full fsync, otherwise a fast fsync will miss this
919                          * extent if it's new and needs to be logged.
920                          */
921                         if ((em->start < start || em_end > end) && modified) {
922                                 ASSERT(!split);
923                                 btrfs_set_inode_full_sync(inode);
924                         }
925                         remove_extent_mapping(em_tree, em);
926                 }
927
928                 /*
929                  * Once for the tree reference (we replaced or removed the
930                  * extent map from the tree).
931                  */
932                 free_extent_map(em);
933 next:
934                 /* Once for us (for our lookup reference). */
935                 free_extent_map(em);
936
937                 em = next_em;
938         }
939
940         write_unlock(&em_tree->lock);
941
942         free_extent_map(split);
943         free_extent_map(split2);
944 }
945
946 /*
947  * Replace a range in the inode's extent map tree with a new extent map.
948  *
949  * @inode:      The target inode.
950  * @new_em:     The new extent map to add to the inode's extent map tree.
951  * @modified:   Indicate if the new extent map should be added to the list of
952  *              modified extents (for fast fsync tracking).
953  *
954  * Drops all the extent maps in the inode's extent map tree that intersect the
955  * range of the new extent map and adds the new extent map to the tree.
956  * The caller should have locked an appropriate file range in the inode's io
957  * tree before calling this function.
958  */
959 int btrfs_replace_extent_map_range(struct btrfs_inode *inode,
960                                    struct extent_map *new_em,
961                                    bool modified)
962 {
963         const u64 end = new_em->start + new_em->len - 1;
964         struct extent_map_tree *tree = &inode->extent_tree;
965         int ret;
966
967         ASSERT(!extent_map_in_tree(new_em));
968
969         /*
970          * The caller has locked an appropriate file range in the inode's io
971          * tree, but getting -EEXIST when adding the new extent map can still
972          * happen in case there are extents that partially cover the range, and
973          * this is due to two tasks operating on different parts of the extent.
974          * See commit 18e83ac75bfe67 ("Btrfs: fix unexpected EEXIST from
975          * btrfs_get_extent") for an example and details.
976          */
977         do {
978                 btrfs_drop_extent_map_range(inode, new_em->start, end, false);
979                 write_lock(&tree->lock);
980                 ret = add_extent_mapping(tree, new_em, modified);
981                 write_unlock(&tree->lock);
982         } while (ret == -EEXIST);
983
984         return ret;
985 }