This source file includes following definitions.
- extent_map_init
- extent_map_exit
- extent_map_tree_init
- alloc_extent_map
- free_extent_map
- range_end
- tree_insert
- __tree_search
- mergable_maps
- try_merge_map
- unpin_extent_cache
- clear_em_logging
- setup_extent_mapping
- extent_map_device_set_bits
- extent_map_device_clear_bits
- add_extent_mapping
- __lookup_extent_mapping
- lookup_extent_mapping
- search_extent_mapping
- remove_extent_mapping
- replace_extent_mapping
- next_extent_map
- prev_extent_map
- merge_extent_mapping
- btrfs_add_extent_mapping
1
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
11
12 static struct kmem_cache *extent_map_cache;
13
14 int __init extent_map_init(void)
15 {
16 extent_map_cache = kmem_cache_create("btrfs_extent_map",
17 sizeof(struct extent_map), 0,
18 SLAB_MEM_SPREAD, NULL);
19 if (!extent_map_cache)
20 return -ENOMEM;
21 return 0;
22 }
23
24 void __cold extent_map_exit(void)
25 {
26 kmem_cache_destroy(extent_map_cache);
27 }
28
29
30
31
32
33
34
35
36 void extent_map_tree_init(struct extent_map_tree *tree)
37 {
38 tree->map = RB_ROOT_CACHED;
39 INIT_LIST_HEAD(&tree->modified_extents);
40 rwlock_init(&tree->lock);
41 }
42
43
44
45
46
47
48
49
50 struct extent_map *alloc_extent_map(void)
51 {
52 struct extent_map *em;
53 em = kmem_cache_zalloc(extent_map_cache, GFP_NOFS);
54 if (!em)
55 return NULL;
56 RB_CLEAR_NODE(&em->rb_node);
57 em->flags = 0;
58 em->compress_type = BTRFS_COMPRESS_NONE;
59 em->generation = 0;
60 refcount_set(&em->refs, 1);
61 INIT_LIST_HEAD(&em->list);
62 return em;
63 }
64
65
66
67
68
69
70
71
72 void free_extent_map(struct extent_map *em)
73 {
74 if (!em)
75 return;
76 WARN_ON(refcount_read(&em->refs) == 0);
77 if (refcount_dec_and_test(&em->refs)) {
78 WARN_ON(extent_map_in_tree(em));
79 WARN_ON(!list_empty(&em->list));
80 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
81 kfree(em->map_lookup);
82 kmem_cache_free(extent_map_cache, em);
83 }
84 }
85
86
87 static u64 range_end(u64 start, u64 len)
88 {
89 if (start + len < start)
90 return (u64)-1;
91 return start + len;
92 }
93
94 static int tree_insert(struct rb_root_cached *root, struct extent_map *em)
95 {
96 struct rb_node **p = &root->rb_root.rb_node;
97 struct rb_node *parent = NULL;
98 struct extent_map *entry = NULL;
99 struct rb_node *orig_parent = NULL;
100 u64 end = range_end(em->start, em->len);
101 bool leftmost = true;
102
103 while (*p) {
104 parent = *p;
105 entry = rb_entry(parent, struct extent_map, rb_node);
106
107 if (em->start < entry->start) {
108 p = &(*p)->rb_left;
109 } else if (em->start >= extent_map_end(entry)) {
110 p = &(*p)->rb_right;
111 leftmost = false;
112 } else {
113 return -EEXIST;
114 }
115 }
116
117 orig_parent = parent;
118 while (parent && em->start >= extent_map_end(entry)) {
119 parent = rb_next(parent);
120 entry = rb_entry(parent, struct extent_map, rb_node);
121 }
122 if (parent)
123 if (end > entry->start && em->start < extent_map_end(entry))
124 return -EEXIST;
125
126 parent = orig_parent;
127 entry = rb_entry(parent, struct extent_map, rb_node);
128 while (parent && em->start < entry->start) {
129 parent = rb_prev(parent);
130 entry = rb_entry(parent, struct extent_map, rb_node);
131 }
132 if (parent)
133 if (end > entry->start && em->start < extent_map_end(entry))
134 return -EEXIST;
135
136 rb_link_node(&em->rb_node, orig_parent, p);
137 rb_insert_color_cached(&em->rb_node, root, leftmost);
138 return 0;
139 }
140
141
142
143
144
145 static struct rb_node *__tree_search(struct rb_root *root, u64 offset,
146 struct rb_node **prev_ret,
147 struct rb_node **next_ret)
148 {
149 struct rb_node *n = root->rb_node;
150 struct rb_node *prev = NULL;
151 struct rb_node *orig_prev = NULL;
152 struct extent_map *entry;
153 struct extent_map *prev_entry = NULL;
154
155 while (n) {
156 entry = rb_entry(n, struct extent_map, rb_node);
157 prev = n;
158 prev_entry = entry;
159
160 if (offset < entry->start)
161 n = n->rb_left;
162 else if (offset >= extent_map_end(entry))
163 n = n->rb_right;
164 else
165 return n;
166 }
167
168 if (prev_ret) {
169 orig_prev = prev;
170 while (prev && offset >= extent_map_end(prev_entry)) {
171 prev = rb_next(prev);
172 prev_entry = rb_entry(prev, struct extent_map, rb_node);
173 }
174 *prev_ret = prev;
175 prev = orig_prev;
176 }
177
178 if (next_ret) {
179 prev_entry = rb_entry(prev, struct extent_map, rb_node);
180 while (prev && offset < prev_entry->start) {
181 prev = rb_prev(prev);
182 prev_entry = rb_entry(prev, struct extent_map, rb_node);
183 }
184 *next_ret = prev;
185 }
186 return NULL;
187 }
188
189
190 static int mergable_maps(struct extent_map *prev, struct extent_map *next)
191 {
192 if (test_bit(EXTENT_FLAG_PINNED, &prev->flags))
193 return 0;
194
195
196
197
198
199 if (test_bit(EXTENT_FLAG_COMPRESSED, &prev->flags))
200 return 0;
201
202 if (test_bit(EXTENT_FLAG_LOGGING, &prev->flags) ||
203 test_bit(EXTENT_FLAG_LOGGING, &next->flags))
204 return 0;
205
206
207
208
209
210
211 if (!list_empty(&prev->list) || !list_empty(&next->list))
212 return 0;
213
214 ASSERT(next->block_start != EXTENT_MAP_DELALLOC &&
215 prev->block_start != EXTENT_MAP_DELALLOC);
216
217 if (extent_map_end(prev) == next->start &&
218 prev->flags == next->flags &&
219 prev->bdev == next->bdev &&
220 ((next->block_start == EXTENT_MAP_HOLE &&
221 prev->block_start == EXTENT_MAP_HOLE) ||
222 (next->block_start == EXTENT_MAP_INLINE &&
223 prev->block_start == EXTENT_MAP_INLINE) ||
224 (next->block_start < EXTENT_MAP_LAST_BYTE - 1 &&
225 next->block_start == extent_map_block_end(prev)))) {
226 return 1;
227 }
228 return 0;
229 }
230
231 static void try_merge_map(struct extent_map_tree *tree, struct extent_map *em)
232 {
233 struct extent_map *merge = NULL;
234 struct rb_node *rb;
235
236
237
238
239
240
241
242
243
244 if (refcount_read(&em->refs) > 2)
245 return;
246
247 if (em->start != 0) {
248 rb = rb_prev(&em->rb_node);
249 if (rb)
250 merge = rb_entry(rb, struct extent_map, rb_node);
251 if (rb && mergable_maps(merge, em)) {
252 em->start = merge->start;
253 em->orig_start = merge->orig_start;
254 em->len += merge->len;
255 em->block_len += merge->block_len;
256 em->block_start = merge->block_start;
257 em->mod_len = (em->mod_len + em->mod_start) - merge->mod_start;
258 em->mod_start = merge->mod_start;
259 em->generation = max(em->generation, merge->generation);
260
261 rb_erase_cached(&merge->rb_node, &tree->map);
262 RB_CLEAR_NODE(&merge->rb_node);
263 free_extent_map(merge);
264 }
265 }
266
267 rb = rb_next(&em->rb_node);
268 if (rb)
269 merge = rb_entry(rb, struct extent_map, rb_node);
270 if (rb && mergable_maps(em, merge)) {
271 em->len += merge->len;
272 em->block_len += merge->block_len;
273 rb_erase_cached(&merge->rb_node, &tree->map);
274 RB_CLEAR_NODE(&merge->rb_node);
275 em->mod_len = (merge->mod_start + merge->mod_len) - em->mod_start;
276 em->generation = max(em->generation, merge->generation);
277 free_extent_map(merge);
278 }
279 }
280
281
282
283
284
285
286
287
288
289
290
291
292 int unpin_extent_cache(struct extent_map_tree *tree, u64 start, u64 len,
293 u64 gen)
294 {
295 int ret = 0;
296 struct extent_map *em;
297 bool prealloc = false;
298
299 write_lock(&tree->lock);
300 em = lookup_extent_mapping(tree, start, len);
301
302 WARN_ON(!em || em->start != start);
303
304 if (!em)
305 goto out;
306
307 em->generation = gen;
308 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
309 em->mod_start = em->start;
310 em->mod_len = em->len;
311
312 if (test_bit(EXTENT_FLAG_FILLING, &em->flags)) {
313 prealloc = true;
314 clear_bit(EXTENT_FLAG_FILLING, &em->flags);
315 }
316
317 try_merge_map(tree, em);
318
319 if (prealloc) {
320 em->mod_start = em->start;
321 em->mod_len = em->len;
322 }
323
324 free_extent_map(em);
325 out:
326 write_unlock(&tree->lock);
327 return ret;
328
329 }
330
331 void clear_em_logging(struct extent_map_tree *tree, struct extent_map *em)
332 {
333 clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
334 if (extent_map_in_tree(em))
335 try_merge_map(tree, em);
336 }
337
338 static inline void setup_extent_mapping(struct extent_map_tree *tree,
339 struct extent_map *em,
340 int modified)
341 {
342 refcount_inc(&em->refs);
343 em->mod_start = em->start;
344 em->mod_len = em->len;
345
346 if (modified)
347 list_move(&em->list, &tree->modified_extents);
348 else
349 try_merge_map(tree, em);
350 }
351
352 static void extent_map_device_set_bits(struct extent_map *em, unsigned bits)
353 {
354 struct map_lookup *map = em->map_lookup;
355 u64 stripe_size = em->orig_block_len;
356 int i;
357
358 for (i = 0; i < map->num_stripes; i++) {
359 struct btrfs_bio_stripe *stripe = &map->stripes[i];
360 struct btrfs_device *device = stripe->dev;
361
362 set_extent_bits_nowait(&device->alloc_state, stripe->physical,
363 stripe->physical + stripe_size - 1, bits);
364 }
365 }
366
367 static void extent_map_device_clear_bits(struct extent_map *em, unsigned bits)
368 {
369 struct map_lookup *map = em->map_lookup;
370 u64 stripe_size = em->orig_block_len;
371 int i;
372
373 for (i = 0; i < map->num_stripes; i++) {
374 struct btrfs_bio_stripe *stripe = &map->stripes[i];
375 struct btrfs_device *device = stripe->dev;
376
377 __clear_extent_bit(&device->alloc_state, stripe->physical,
378 stripe->physical + stripe_size - 1, bits,
379 0, 0, NULL, GFP_NOWAIT, NULL);
380 }
381 }
382
383
384
385
386
387
388
389
390
391
392
393 int add_extent_mapping(struct extent_map_tree *tree,
394 struct extent_map *em, int modified)
395 {
396 int ret = 0;
397
398 lockdep_assert_held_write(&tree->lock);
399
400 ret = tree_insert(&tree->map, em);
401 if (ret)
402 goto out;
403
404 setup_extent_mapping(tree, em, modified);
405 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags)) {
406 extent_map_device_set_bits(em, CHUNK_ALLOCATED);
407 extent_map_device_clear_bits(em, CHUNK_TRIMMED);
408 }
409 out:
410 return ret;
411 }
412
413 static struct extent_map *
414 __lookup_extent_mapping(struct extent_map_tree *tree,
415 u64 start, u64 len, int strict)
416 {
417 struct extent_map *em;
418 struct rb_node *rb_node;
419 struct rb_node *prev = NULL;
420 struct rb_node *next = NULL;
421 u64 end = range_end(start, len);
422
423 rb_node = __tree_search(&tree->map.rb_root, start, &prev, &next);
424 if (!rb_node) {
425 if (prev)
426 rb_node = prev;
427 else if (next)
428 rb_node = next;
429 else
430 return NULL;
431 }
432
433 em = rb_entry(rb_node, struct extent_map, rb_node);
434
435 if (strict && !(end > em->start && start < extent_map_end(em)))
436 return NULL;
437
438 refcount_inc(&em->refs);
439 return em;
440 }
441
442
443
444
445
446
447
448
449
450
451
452
453 struct extent_map *lookup_extent_mapping(struct extent_map_tree *tree,
454 u64 start, u64 len)
455 {
456 return __lookup_extent_mapping(tree, start, len, 1);
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470 struct extent_map *search_extent_mapping(struct extent_map_tree *tree,
471 u64 start, u64 len)
472 {
473 return __lookup_extent_mapping(tree, start, len, 0);
474 }
475
476
477
478
479
480
481
482
483
484 void remove_extent_mapping(struct extent_map_tree *tree, struct extent_map *em)
485 {
486 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &em->flags));
487 rb_erase_cached(&em->rb_node, &tree->map);
488 if (!test_bit(EXTENT_FLAG_LOGGING, &em->flags))
489 list_del_init(&em->list);
490 if (test_bit(EXTENT_FLAG_FS_MAPPING, &em->flags))
491 extent_map_device_clear_bits(em, CHUNK_ALLOCATED);
492 RB_CLEAR_NODE(&em->rb_node);
493 }
494
495 void replace_extent_mapping(struct extent_map_tree *tree,
496 struct extent_map *cur,
497 struct extent_map *new,
498 int modified)
499 {
500 WARN_ON(test_bit(EXTENT_FLAG_PINNED, &cur->flags));
501 ASSERT(extent_map_in_tree(cur));
502 if (!test_bit(EXTENT_FLAG_LOGGING, &cur->flags))
503 list_del_init(&cur->list);
504 rb_replace_node_cached(&cur->rb_node, &new->rb_node, &tree->map);
505 RB_CLEAR_NODE(&cur->rb_node);
506
507 setup_extent_mapping(tree, new, modified);
508 }
509
510 static struct extent_map *next_extent_map(struct extent_map *em)
511 {
512 struct rb_node *next;
513
514 next = rb_next(&em->rb_node);
515 if (!next)
516 return NULL;
517 return container_of(next, struct extent_map, rb_node);
518 }
519
520 static struct extent_map *prev_extent_map(struct extent_map *em)
521 {
522 struct rb_node *prev;
523
524 prev = rb_prev(&em->rb_node);
525 if (!prev)
526 return NULL;
527 return container_of(prev, struct extent_map, rb_node);
528 }
529
530
531
532
533
534
535
536 static noinline int merge_extent_mapping(struct extent_map_tree *em_tree,
537 struct extent_map *existing,
538 struct extent_map *em,
539 u64 map_start)
540 {
541 struct extent_map *prev;
542 struct extent_map *next;
543 u64 start;
544 u64 end;
545 u64 start_diff;
546
547 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
548
549 if (existing->start > map_start) {
550 next = existing;
551 prev = prev_extent_map(next);
552 } else {
553 prev = existing;
554 next = next_extent_map(prev);
555 }
556
557 start = prev ? extent_map_end(prev) : em->start;
558 start = max_t(u64, start, em->start);
559 end = next ? next->start : extent_map_end(em);
560 end = min_t(u64, end, extent_map_end(em));
561 start_diff = start - em->start;
562 em->start = start;
563 em->len = end - start;
564 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
565 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
566 em->block_start += start_diff;
567 em->block_len = em->len;
568 }
569 return add_extent_mapping(em_tree, em, 0);
570 }
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592 int btrfs_add_extent_mapping(struct btrfs_fs_info *fs_info,
593 struct extent_map_tree *em_tree,
594 struct extent_map **em_in, u64 start, u64 len)
595 {
596 int ret;
597 struct extent_map *em = *em_in;
598
599 ret = add_extent_mapping(em_tree, em, 0);
600
601
602
603
604 if (ret == -EEXIST) {
605 struct extent_map *existing;
606
607 ret = 0;
608
609 existing = search_extent_mapping(em_tree, start, len);
610
611 trace_btrfs_handle_em_exist(fs_info, existing, em, start, len);
612
613
614
615
616
617 if (start >= existing->start &&
618 start < extent_map_end(existing)) {
619 free_extent_map(em);
620 *em_in = existing;
621 ret = 0;
622 } else {
623 u64 orig_start = em->start;
624 u64 orig_len = em->len;
625
626
627
628
629
630 ret = merge_extent_mapping(em_tree, existing,
631 em, start);
632 if (ret) {
633 free_extent_map(em);
634 *em_in = NULL;
635 WARN_ONCE(ret,
636 "unexpected error %d: merge existing(start %llu len %llu) with em(start %llu len %llu)\n",
637 ret, existing->start, existing->len,
638 orig_start, orig_len);
639 }
640 free_extent_map(existing);
641 }
642 }
643
644 ASSERT(ret == 0 || ret == -EEXIST);
645 return ret;
646 }