btrfs: Remove fs_info argument from __add_to_free_space_tree
[sfrench/cifs-2.6.git] / fs / btrfs / free-space-tree.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2015 Facebook.  All rights reserved.
4  */
5
6 #include <linux/kernel.h>
7 #include <linux/sched/mm.h>
8 #include "ctree.h"
9 #include "disk-io.h"
10 #include "locking.h"
11 #include "free-space-tree.h"
12 #include "transaction.h"
13
14 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
15                                         struct btrfs_block_group_cache *block_group,
16                                         struct btrfs_path *path);
17
18 void set_free_space_tree_thresholds(struct btrfs_block_group_cache *cache)
19 {
20         u32 bitmap_range;
21         size_t bitmap_size;
22         u64 num_bitmaps, total_bitmap_size;
23
24         /*
25          * We convert to bitmaps when the disk space required for using extents
26          * exceeds that required for using bitmaps.
27          */
28         bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
29         num_bitmaps = div_u64(cache->key.offset + bitmap_range - 1,
30                               bitmap_range);
31         bitmap_size = sizeof(struct btrfs_item) + BTRFS_FREE_SPACE_BITMAP_SIZE;
32         total_bitmap_size = num_bitmaps * bitmap_size;
33         cache->bitmap_high_thresh = div_u64(total_bitmap_size,
34                                             sizeof(struct btrfs_item));
35
36         /*
37          * We allow for a small buffer between the high threshold and low
38          * threshold to avoid thrashing back and forth between the two formats.
39          */
40         if (cache->bitmap_high_thresh > 100)
41                 cache->bitmap_low_thresh = cache->bitmap_high_thresh - 100;
42         else
43                 cache->bitmap_low_thresh = 0;
44 }
45
46 static int add_new_free_space_info(struct btrfs_trans_handle *trans,
47                                    struct btrfs_fs_info *fs_info,
48                                    struct btrfs_block_group_cache *block_group,
49                                    struct btrfs_path *path)
50 {
51         struct btrfs_root *root = fs_info->free_space_root;
52         struct btrfs_free_space_info *info;
53         struct btrfs_key key;
54         struct extent_buffer *leaf;
55         int ret;
56
57         key.objectid = block_group->key.objectid;
58         key.type = BTRFS_FREE_SPACE_INFO_KEY;
59         key.offset = block_group->key.offset;
60
61         ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
62         if (ret)
63                 goto out;
64
65         leaf = path->nodes[0];
66         info = btrfs_item_ptr(leaf, path->slots[0],
67                               struct btrfs_free_space_info);
68         btrfs_set_free_space_extent_count(leaf, info, 0);
69         btrfs_set_free_space_flags(leaf, info, 0);
70         btrfs_mark_buffer_dirty(leaf);
71
72         ret = 0;
73 out:
74         btrfs_release_path(path);
75         return ret;
76 }
77
78 struct btrfs_free_space_info *
79 search_free_space_info(struct btrfs_trans_handle *trans,
80                        struct btrfs_fs_info *fs_info,
81                        struct btrfs_block_group_cache *block_group,
82                        struct btrfs_path *path, int cow)
83 {
84         struct btrfs_root *root = fs_info->free_space_root;
85         struct btrfs_key key;
86         int ret;
87
88         key.objectid = block_group->key.objectid;
89         key.type = BTRFS_FREE_SPACE_INFO_KEY;
90         key.offset = block_group->key.offset;
91
92         ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
93         if (ret < 0)
94                 return ERR_PTR(ret);
95         if (ret != 0) {
96                 btrfs_warn(fs_info, "missing free space info for %llu",
97                            block_group->key.objectid);
98                 ASSERT(0);
99                 return ERR_PTR(-ENOENT);
100         }
101
102         return btrfs_item_ptr(path->nodes[0], path->slots[0],
103                               struct btrfs_free_space_info);
104 }
105
106 /*
107  * btrfs_search_slot() but we're looking for the greatest key less than the
108  * passed key.
109  */
110 static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
111                                   struct btrfs_root *root,
112                                   struct btrfs_key *key, struct btrfs_path *p,
113                                   int ins_len, int cow)
114 {
115         int ret;
116
117         ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
118         if (ret < 0)
119                 return ret;
120
121         if (ret == 0) {
122                 ASSERT(0);
123                 return -EIO;
124         }
125
126         if (p->slots[0] == 0) {
127                 ASSERT(0);
128                 return -EIO;
129         }
130         p->slots[0]--;
131
132         return 0;
133 }
134
135 static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
136 {
137         return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
138 }
139
140 static unsigned long *alloc_bitmap(u32 bitmap_size)
141 {
142         unsigned long *ret;
143         unsigned int nofs_flag;
144         u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
145
146         /*
147          * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
148          * into the filesystem as the free space bitmap can be modified in the
149          * critical section of a transaction commit.
150          *
151          * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
152          * know that recursion is unsafe.
153          */
154         nofs_flag = memalloc_nofs_save();
155         ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
156         memalloc_nofs_restore(nofs_flag);
157         return ret;
158 }
159
160 static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
161 {
162         u8 *p = ((u8 *)map) + BIT_BYTE(start);
163         const unsigned int size = start + len;
164         int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
165         u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
166
167         while (len - bits_to_set >= 0) {
168                 *p |= mask_to_set;
169                 len -= bits_to_set;
170                 bits_to_set = BITS_PER_BYTE;
171                 mask_to_set = ~0;
172                 p++;
173         }
174         if (len) {
175                 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
176                 *p |= mask_to_set;
177         }
178 }
179
180 int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
181                                   struct btrfs_fs_info *fs_info,
182                                   struct btrfs_block_group_cache *block_group,
183                                   struct btrfs_path *path)
184 {
185         struct btrfs_root *root = fs_info->free_space_root;
186         struct btrfs_free_space_info *info;
187         struct btrfs_key key, found_key;
188         struct extent_buffer *leaf;
189         unsigned long *bitmap;
190         char *bitmap_cursor;
191         u64 start, end;
192         u64 bitmap_range, i;
193         u32 bitmap_size, flags, expected_extent_count;
194         u32 extent_count = 0;
195         int done = 0, nr;
196         int ret;
197
198         bitmap_size = free_space_bitmap_size(block_group->key.offset,
199                                              fs_info->sectorsize);
200         bitmap = alloc_bitmap(bitmap_size);
201         if (!bitmap) {
202                 ret = -ENOMEM;
203                 goto out;
204         }
205
206         start = block_group->key.objectid;
207         end = block_group->key.objectid + block_group->key.offset;
208
209         key.objectid = end - 1;
210         key.type = (u8)-1;
211         key.offset = (u64)-1;
212
213         while (!done) {
214                 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
215                 if (ret)
216                         goto out;
217
218                 leaf = path->nodes[0];
219                 nr = 0;
220                 path->slots[0]++;
221                 while (path->slots[0] > 0) {
222                         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
223
224                         if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
225                                 ASSERT(found_key.objectid == block_group->key.objectid);
226                                 ASSERT(found_key.offset == block_group->key.offset);
227                                 done = 1;
228                                 break;
229                         } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
230                                 u64 first, last;
231
232                                 ASSERT(found_key.objectid >= start);
233                                 ASSERT(found_key.objectid < end);
234                                 ASSERT(found_key.objectid + found_key.offset <= end);
235
236                                 first = div_u64(found_key.objectid - start,
237                                                 fs_info->sectorsize);
238                                 last = div_u64(found_key.objectid + found_key.offset - start,
239                                                fs_info->sectorsize);
240                                 le_bitmap_set(bitmap, first, last - first);
241
242                                 extent_count++;
243                                 nr++;
244                                 path->slots[0]--;
245                         } else {
246                                 ASSERT(0);
247                         }
248                 }
249
250                 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
251                 if (ret)
252                         goto out;
253                 btrfs_release_path(path);
254         }
255
256         info = search_free_space_info(trans, fs_info, block_group, path, 1);
257         if (IS_ERR(info)) {
258                 ret = PTR_ERR(info);
259                 goto out;
260         }
261         leaf = path->nodes[0];
262         flags = btrfs_free_space_flags(leaf, info);
263         flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
264         btrfs_set_free_space_flags(leaf, info, flags);
265         expected_extent_count = btrfs_free_space_extent_count(leaf, info);
266         btrfs_mark_buffer_dirty(leaf);
267         btrfs_release_path(path);
268
269         if (extent_count != expected_extent_count) {
270                 btrfs_err(fs_info,
271                           "incorrect extent count for %llu; counted %u, expected %u",
272                           block_group->key.objectid, extent_count,
273                           expected_extent_count);
274                 ASSERT(0);
275                 ret = -EIO;
276                 goto out;
277         }
278
279         bitmap_cursor = (char *)bitmap;
280         bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
281         i = start;
282         while (i < end) {
283                 unsigned long ptr;
284                 u64 extent_size;
285                 u32 data_size;
286
287                 extent_size = min(end - i, bitmap_range);
288                 data_size = free_space_bitmap_size(extent_size,
289                                                    fs_info->sectorsize);
290
291                 key.objectid = i;
292                 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
293                 key.offset = extent_size;
294
295                 ret = btrfs_insert_empty_item(trans, root, path, &key,
296                                               data_size);
297                 if (ret)
298                         goto out;
299
300                 leaf = path->nodes[0];
301                 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
302                 write_extent_buffer(leaf, bitmap_cursor, ptr,
303                                     data_size);
304                 btrfs_mark_buffer_dirty(leaf);
305                 btrfs_release_path(path);
306
307                 i += extent_size;
308                 bitmap_cursor += data_size;
309         }
310
311         ret = 0;
312 out:
313         kvfree(bitmap);
314         if (ret)
315                 btrfs_abort_transaction(trans, ret);
316         return ret;
317 }
318
319 int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
320                                   struct btrfs_fs_info *fs_info,
321                                   struct btrfs_block_group_cache *block_group,
322                                   struct btrfs_path *path)
323 {
324         struct btrfs_root *root = fs_info->free_space_root;
325         struct btrfs_free_space_info *info;
326         struct btrfs_key key, found_key;
327         struct extent_buffer *leaf;
328         unsigned long *bitmap;
329         u64 start, end;
330         u32 bitmap_size, flags, expected_extent_count;
331         unsigned long nrbits, start_bit, end_bit;
332         u32 extent_count = 0;
333         int done = 0, nr;
334         int ret;
335
336         bitmap_size = free_space_bitmap_size(block_group->key.offset,
337                                              fs_info->sectorsize);
338         bitmap = alloc_bitmap(bitmap_size);
339         if (!bitmap) {
340                 ret = -ENOMEM;
341                 goto out;
342         }
343
344         start = block_group->key.objectid;
345         end = block_group->key.objectid + block_group->key.offset;
346
347         key.objectid = end - 1;
348         key.type = (u8)-1;
349         key.offset = (u64)-1;
350
351         while (!done) {
352                 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
353                 if (ret)
354                         goto out;
355
356                 leaf = path->nodes[0];
357                 nr = 0;
358                 path->slots[0]++;
359                 while (path->slots[0] > 0) {
360                         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
361
362                         if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
363                                 ASSERT(found_key.objectid == block_group->key.objectid);
364                                 ASSERT(found_key.offset == block_group->key.offset);
365                                 done = 1;
366                                 break;
367                         } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
368                                 unsigned long ptr;
369                                 char *bitmap_cursor;
370                                 u32 bitmap_pos, data_size;
371
372                                 ASSERT(found_key.objectid >= start);
373                                 ASSERT(found_key.objectid < end);
374                                 ASSERT(found_key.objectid + found_key.offset <= end);
375
376                                 bitmap_pos = div_u64(found_key.objectid - start,
377                                                      fs_info->sectorsize *
378                                                      BITS_PER_BYTE);
379                                 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
380                                 data_size = free_space_bitmap_size(found_key.offset,
381                                                                    fs_info->sectorsize);
382
383                                 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
384                                 read_extent_buffer(leaf, bitmap_cursor, ptr,
385                                                    data_size);
386
387                                 nr++;
388                                 path->slots[0]--;
389                         } else {
390                                 ASSERT(0);
391                         }
392                 }
393
394                 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
395                 if (ret)
396                         goto out;
397                 btrfs_release_path(path);
398         }
399
400         info = search_free_space_info(trans, fs_info, block_group, path, 1);
401         if (IS_ERR(info)) {
402                 ret = PTR_ERR(info);
403                 goto out;
404         }
405         leaf = path->nodes[0];
406         flags = btrfs_free_space_flags(leaf, info);
407         flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
408         btrfs_set_free_space_flags(leaf, info, flags);
409         expected_extent_count = btrfs_free_space_extent_count(leaf, info);
410         btrfs_mark_buffer_dirty(leaf);
411         btrfs_release_path(path);
412
413         nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
414         start_bit = find_next_bit_le(bitmap, nrbits, 0);
415
416         while (start_bit < nrbits) {
417                 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
418                 ASSERT(start_bit < end_bit);
419
420                 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
421                 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
422                 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
423
424                 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
425                 if (ret)
426                         goto out;
427                 btrfs_release_path(path);
428
429                 extent_count++;
430
431                 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
432         }
433
434         if (extent_count != expected_extent_count) {
435                 btrfs_err(fs_info,
436                           "incorrect extent count for %llu; counted %u, expected %u",
437                           block_group->key.objectid, extent_count,
438                           expected_extent_count);
439                 ASSERT(0);
440                 ret = -EIO;
441                 goto out;
442         }
443
444         ret = 0;
445 out:
446         kvfree(bitmap);
447         if (ret)
448                 btrfs_abort_transaction(trans, ret);
449         return ret;
450 }
451
452 static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
453                                           struct btrfs_fs_info *fs_info,
454                                           struct btrfs_block_group_cache *block_group,
455                                           struct btrfs_path *path,
456                                           int new_extents)
457 {
458         struct btrfs_free_space_info *info;
459         u32 flags;
460         u32 extent_count;
461         int ret = 0;
462
463         if (new_extents == 0)
464                 return 0;
465
466         info = search_free_space_info(trans, fs_info, block_group, path, 1);
467         if (IS_ERR(info)) {
468                 ret = PTR_ERR(info);
469                 goto out;
470         }
471         flags = btrfs_free_space_flags(path->nodes[0], info);
472         extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
473
474         extent_count += new_extents;
475         btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
476         btrfs_mark_buffer_dirty(path->nodes[0]);
477         btrfs_release_path(path);
478
479         if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
480             extent_count > block_group->bitmap_high_thresh) {
481                 ret = convert_free_space_to_bitmaps(trans, fs_info, block_group,
482                                                     path);
483         } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
484                    extent_count < block_group->bitmap_low_thresh) {
485                 ret = convert_free_space_to_extents(trans, fs_info, block_group,
486                                                     path);
487         }
488
489 out:
490         return ret;
491 }
492
493 int free_space_test_bit(struct btrfs_block_group_cache *block_group,
494                         struct btrfs_path *path, u64 offset)
495 {
496         struct extent_buffer *leaf;
497         struct btrfs_key key;
498         u64 found_start, found_end;
499         unsigned long ptr, i;
500
501         leaf = path->nodes[0];
502         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
503         ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
504
505         found_start = key.objectid;
506         found_end = key.objectid + key.offset;
507         ASSERT(offset >= found_start && offset < found_end);
508
509         ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
510         i = div_u64(offset - found_start,
511                     block_group->fs_info->sectorsize);
512         return !!extent_buffer_test_bit(leaf, ptr, i);
513 }
514
515 static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
516                                 struct btrfs_path *path, u64 *start, u64 *size,
517                                 int bit)
518 {
519         struct btrfs_fs_info *fs_info = block_group->fs_info;
520         struct extent_buffer *leaf;
521         struct btrfs_key key;
522         u64 end = *start + *size;
523         u64 found_start, found_end;
524         unsigned long ptr, first, last;
525
526         leaf = path->nodes[0];
527         btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
528         ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
529
530         found_start = key.objectid;
531         found_end = key.objectid + key.offset;
532         ASSERT(*start >= found_start && *start < found_end);
533         ASSERT(end > found_start);
534
535         if (end > found_end)
536                 end = found_end;
537
538         ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
539         first = div_u64(*start - found_start, fs_info->sectorsize);
540         last = div_u64(end - found_start, fs_info->sectorsize);
541         if (bit)
542                 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
543         else
544                 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
545         btrfs_mark_buffer_dirty(leaf);
546
547         *size -= end - *start;
548         *start = end;
549 }
550
551 /*
552  * We can't use btrfs_next_item() in modify_free_space_bitmap() because
553  * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
554  * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
555  * looking for.
556  */
557 static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
558                                   struct btrfs_root *root, struct btrfs_path *p)
559 {
560         struct btrfs_key key;
561
562         if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
563                 p->slots[0]++;
564                 return 0;
565         }
566
567         btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
568         btrfs_release_path(p);
569
570         key.objectid += key.offset;
571         key.type = (u8)-1;
572         key.offset = (u64)-1;
573
574         return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
575 }
576
577 /*
578  * If remove is 1, then we are removing free space, thus clearing bits in the
579  * bitmap. If remove is 0, then we are adding free space, thus setting bits in
580  * the bitmap.
581  */
582 static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
583                                     struct btrfs_fs_info *fs_info,
584                                     struct btrfs_block_group_cache *block_group,
585                                     struct btrfs_path *path,
586                                     u64 start, u64 size, int remove)
587 {
588         struct btrfs_root *root = fs_info->free_space_root;
589         struct btrfs_key key;
590         u64 end = start + size;
591         u64 cur_start, cur_size;
592         int prev_bit, next_bit;
593         int new_extents;
594         int ret;
595
596         /*
597          * Read the bit for the block immediately before the extent of space if
598          * that block is within the block group.
599          */
600         if (start > block_group->key.objectid) {
601                 u64 prev_block = start - block_group->fs_info->sectorsize;
602
603                 key.objectid = prev_block;
604                 key.type = (u8)-1;
605                 key.offset = (u64)-1;
606
607                 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
608                 if (ret)
609                         goto out;
610
611                 prev_bit = free_space_test_bit(block_group, path, prev_block);
612
613                 /* The previous block may have been in the previous bitmap. */
614                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
615                 if (start >= key.objectid + key.offset) {
616                         ret = free_space_next_bitmap(trans, root, path);
617                         if (ret)
618                                 goto out;
619                 }
620         } else {
621                 key.objectid = start;
622                 key.type = (u8)-1;
623                 key.offset = (u64)-1;
624
625                 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
626                 if (ret)
627                         goto out;
628
629                 prev_bit = -1;
630         }
631
632         /*
633          * Iterate over all of the bitmaps overlapped by the extent of space,
634          * clearing/setting bits as required.
635          */
636         cur_start = start;
637         cur_size = size;
638         while (1) {
639                 free_space_set_bits(block_group, path, &cur_start, &cur_size,
640                                     !remove);
641                 if (cur_size == 0)
642                         break;
643                 ret = free_space_next_bitmap(trans, root, path);
644                 if (ret)
645                         goto out;
646         }
647
648         /*
649          * Read the bit for the block immediately after the extent of space if
650          * that block is within the block group.
651          */
652         if (end < block_group->key.objectid + block_group->key.offset) {
653                 /* The next block may be in the next bitmap. */
654                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
655                 if (end >= key.objectid + key.offset) {
656                         ret = free_space_next_bitmap(trans, root, path);
657                         if (ret)
658                                 goto out;
659                 }
660
661                 next_bit = free_space_test_bit(block_group, path, end);
662         } else {
663                 next_bit = -1;
664         }
665
666         if (remove) {
667                 new_extents = -1;
668                 if (prev_bit == 1) {
669                         /* Leftover on the left. */
670                         new_extents++;
671                 }
672                 if (next_bit == 1) {
673                         /* Leftover on the right. */
674                         new_extents++;
675                 }
676         } else {
677                 new_extents = 1;
678                 if (prev_bit == 1) {
679                         /* Merging with neighbor on the left. */
680                         new_extents--;
681                 }
682                 if (next_bit == 1) {
683                         /* Merging with neighbor on the right. */
684                         new_extents--;
685                 }
686         }
687
688         btrfs_release_path(path);
689         ret = update_free_space_extent_count(trans, fs_info, block_group, path,
690                                              new_extents);
691
692 out:
693         return ret;
694 }
695
696 static int remove_free_space_extent(struct btrfs_trans_handle *trans,
697                                     struct btrfs_fs_info *fs_info,
698                                     struct btrfs_block_group_cache *block_group,
699                                     struct btrfs_path *path,
700                                     u64 start, u64 size)
701 {
702         struct btrfs_root *root = fs_info->free_space_root;
703         struct btrfs_key key;
704         u64 found_start, found_end;
705         u64 end = start + size;
706         int new_extents = -1;
707         int ret;
708
709         key.objectid = start;
710         key.type = (u8)-1;
711         key.offset = (u64)-1;
712
713         ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
714         if (ret)
715                 goto out;
716
717         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
718
719         ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
720
721         found_start = key.objectid;
722         found_end = key.objectid + key.offset;
723         ASSERT(start >= found_start && end <= found_end);
724
725         /*
726          * Okay, now that we've found the free space extent which contains the
727          * free space that we are removing, there are four cases:
728          *
729          * 1. We're using the whole extent: delete the key we found and
730          * decrement the free space extent count.
731          * 2. We are using part of the extent starting at the beginning: delete
732          * the key we found and insert a new key representing the leftover at
733          * the end. There is no net change in the number of extents.
734          * 3. We are using part of the extent ending at the end: delete the key
735          * we found and insert a new key representing the leftover at the
736          * beginning. There is no net change in the number of extents.
737          * 4. We are using part of the extent in the middle: delete the key we
738          * found and insert two new keys representing the leftovers on each
739          * side. Where we used to have one extent, we now have two, so increment
740          * the extent count. We may need to convert the block group to bitmaps
741          * as a result.
742          */
743
744         /* Delete the existing key (cases 1-4). */
745         ret = btrfs_del_item(trans, root, path);
746         if (ret)
747                 goto out;
748
749         /* Add a key for leftovers at the beginning (cases 3 and 4). */
750         if (start > found_start) {
751                 key.objectid = found_start;
752                 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
753                 key.offset = start - found_start;
754
755                 btrfs_release_path(path);
756                 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
757                 if (ret)
758                         goto out;
759                 new_extents++;
760         }
761
762         /* Add a key for leftovers at the end (cases 2 and 4). */
763         if (end < found_end) {
764                 key.objectid = end;
765                 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
766                 key.offset = found_end - end;
767
768                 btrfs_release_path(path);
769                 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
770                 if (ret)
771                         goto out;
772                 new_extents++;
773         }
774
775         btrfs_release_path(path);
776         ret = update_free_space_extent_count(trans, fs_info, block_group, path,
777                                              new_extents);
778
779 out:
780         return ret;
781 }
782
783 int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
784                                   struct btrfs_fs_info *fs_info,
785                                   struct btrfs_block_group_cache *block_group,
786                                   struct btrfs_path *path, u64 start, u64 size)
787 {
788         struct btrfs_free_space_info *info;
789         u32 flags;
790         int ret;
791
792         if (block_group->needs_free_space) {
793                 ret = __add_block_group_free_space(trans, block_group, path);
794                 if (ret)
795                         return ret;
796         }
797
798         info = search_free_space_info(NULL, fs_info, block_group, path, 0);
799         if (IS_ERR(info))
800                 return PTR_ERR(info);
801         flags = btrfs_free_space_flags(path->nodes[0], info);
802         btrfs_release_path(path);
803
804         if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
805                 return modify_free_space_bitmap(trans, fs_info, block_group,
806                                                 path, start, size, 1);
807         } else {
808                 return remove_free_space_extent(trans, fs_info, block_group,
809                                                 path, start, size);
810         }
811 }
812
813 int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
814                                 struct btrfs_fs_info *fs_info,
815                                 u64 start, u64 size)
816 {
817         struct btrfs_block_group_cache *block_group;
818         struct btrfs_path *path;
819         int ret;
820
821         if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
822                 return 0;
823
824         path = btrfs_alloc_path();
825         if (!path) {
826                 ret = -ENOMEM;
827                 goto out;
828         }
829
830         block_group = btrfs_lookup_block_group(fs_info, start);
831         if (!block_group) {
832                 ASSERT(0);
833                 ret = -ENOENT;
834                 goto out;
835         }
836
837         mutex_lock(&block_group->free_space_lock);
838         ret = __remove_from_free_space_tree(trans, fs_info, block_group, path,
839                                             start, size);
840         mutex_unlock(&block_group->free_space_lock);
841
842         btrfs_put_block_group(block_group);
843 out:
844         btrfs_free_path(path);
845         if (ret)
846                 btrfs_abort_transaction(trans, ret);
847         return ret;
848 }
849
850 static int add_free_space_extent(struct btrfs_trans_handle *trans,
851                                  struct btrfs_fs_info *fs_info,
852                                  struct btrfs_block_group_cache *block_group,
853                                  struct btrfs_path *path,
854                                  u64 start, u64 size)
855 {
856         struct btrfs_root *root = fs_info->free_space_root;
857         struct btrfs_key key, new_key;
858         u64 found_start, found_end;
859         u64 end = start + size;
860         int new_extents = 1;
861         int ret;
862
863         /*
864          * We are adding a new extent of free space, but we need to merge
865          * extents. There are four cases here:
866          *
867          * 1. The new extent does not have any immediate neighbors to merge
868          * with: add the new key and increment the free space extent count. We
869          * may need to convert the block group to bitmaps as a result.
870          * 2. The new extent has an immediate neighbor before it: remove the
871          * previous key and insert a new key combining both of them. There is no
872          * net change in the number of extents.
873          * 3. The new extent has an immediate neighbor after it: remove the next
874          * key and insert a new key combining both of them. There is no net
875          * change in the number of extents.
876          * 4. The new extent has immediate neighbors on both sides: remove both
877          * of the keys and insert a new key combining all of them. Where we used
878          * to have two extents, we now have one, so decrement the extent count.
879          */
880
881         new_key.objectid = start;
882         new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
883         new_key.offset = size;
884
885         /* Search for a neighbor on the left. */
886         if (start == block_group->key.objectid)
887                 goto right;
888         key.objectid = start - 1;
889         key.type = (u8)-1;
890         key.offset = (u64)-1;
891
892         ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
893         if (ret)
894                 goto out;
895
896         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
897
898         if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
899                 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
900                 btrfs_release_path(path);
901                 goto right;
902         }
903
904         found_start = key.objectid;
905         found_end = key.objectid + key.offset;
906         ASSERT(found_start >= block_group->key.objectid &&
907                found_end > block_group->key.objectid);
908         ASSERT(found_start < start && found_end <= start);
909
910         /*
911          * Delete the neighbor on the left and absorb it into the new key (cases
912          * 2 and 4).
913          */
914         if (found_end == start) {
915                 ret = btrfs_del_item(trans, root, path);
916                 if (ret)
917                         goto out;
918                 new_key.objectid = found_start;
919                 new_key.offset += key.offset;
920                 new_extents--;
921         }
922         btrfs_release_path(path);
923
924 right:
925         /* Search for a neighbor on the right. */
926         if (end == block_group->key.objectid + block_group->key.offset)
927                 goto insert;
928         key.objectid = end;
929         key.type = (u8)-1;
930         key.offset = (u64)-1;
931
932         ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
933         if (ret)
934                 goto out;
935
936         btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
937
938         if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
939                 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
940                 btrfs_release_path(path);
941                 goto insert;
942         }
943
944         found_start = key.objectid;
945         found_end = key.objectid + key.offset;
946         ASSERT(found_start >= block_group->key.objectid &&
947                found_end > block_group->key.objectid);
948         ASSERT((found_start < start && found_end <= start) ||
949                (found_start >= end && found_end > end));
950
951         /*
952          * Delete the neighbor on the right and absorb it into the new key
953          * (cases 3 and 4).
954          */
955         if (found_start == end) {
956                 ret = btrfs_del_item(trans, root, path);
957                 if (ret)
958                         goto out;
959                 new_key.offset += key.offset;
960                 new_extents--;
961         }
962         btrfs_release_path(path);
963
964 insert:
965         /* Insert the new key (cases 1-4). */
966         ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
967         if (ret)
968                 goto out;
969
970         btrfs_release_path(path);
971         ret = update_free_space_extent_count(trans, fs_info, block_group, path,
972                                              new_extents);
973
974 out:
975         return ret;
976 }
977
978 int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
979                              struct btrfs_block_group_cache *block_group,
980                              struct btrfs_path *path, u64 start, u64 size)
981 {
982         struct btrfs_fs_info *fs_info = trans->fs_info;
983         struct btrfs_free_space_info *info;
984         u32 flags;
985         int ret;
986
987         if (block_group->needs_free_space) {
988                 ret = __add_block_group_free_space(trans, block_group, path);
989                 if (ret)
990                         return ret;
991         }
992
993         info = search_free_space_info(NULL, fs_info, block_group, path, 0);
994         if (IS_ERR(info))
995                 return PTR_ERR(info);
996         flags = btrfs_free_space_flags(path->nodes[0], info);
997         btrfs_release_path(path);
998
999         if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
1000                 return modify_free_space_bitmap(trans, fs_info, block_group,
1001                                                 path, start, size, 0);
1002         } else {
1003                 return add_free_space_extent(trans, fs_info, block_group, path,
1004                                              start, size);
1005         }
1006 }
1007
1008 int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1009                            struct btrfs_fs_info *fs_info,
1010                            u64 start, u64 size)
1011 {
1012         struct btrfs_block_group_cache *block_group;
1013         struct btrfs_path *path;
1014         int ret;
1015
1016         if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1017                 return 0;
1018
1019         path = btrfs_alloc_path();
1020         if (!path) {
1021                 ret = -ENOMEM;
1022                 goto out;
1023         }
1024
1025         block_group = btrfs_lookup_block_group(fs_info, start);
1026         if (!block_group) {
1027                 ASSERT(0);
1028                 ret = -ENOENT;
1029                 goto out;
1030         }
1031
1032         mutex_lock(&block_group->free_space_lock);
1033         ret = __add_to_free_space_tree(trans, block_group, path, start, size);
1034         mutex_unlock(&block_group->free_space_lock);
1035
1036         btrfs_put_block_group(block_group);
1037 out:
1038         btrfs_free_path(path);
1039         if (ret)
1040                 btrfs_abort_transaction(trans, ret);
1041         return ret;
1042 }
1043
1044 /*
1045  * Populate the free space tree by walking the extent tree. Operations on the
1046  * extent tree that happen as a result of writes to the free space tree will go
1047  * through the normal add/remove hooks.
1048  */
1049 static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1050                                     struct btrfs_fs_info *fs_info,
1051                                     struct btrfs_block_group_cache *block_group)
1052 {
1053         struct btrfs_root *extent_root = fs_info->extent_root;
1054         struct btrfs_path *path, *path2;
1055         struct btrfs_key key;
1056         u64 start, end;
1057         int ret;
1058
1059         path = btrfs_alloc_path();
1060         if (!path)
1061                 return -ENOMEM;
1062         path->reada = READA_FORWARD;
1063
1064         path2 = btrfs_alloc_path();
1065         if (!path2) {
1066                 btrfs_free_path(path);
1067                 return -ENOMEM;
1068         }
1069
1070         ret = add_new_free_space_info(trans, fs_info, block_group, path2);
1071         if (ret)
1072                 goto out;
1073
1074         mutex_lock(&block_group->free_space_lock);
1075
1076         /*
1077          * Iterate through all of the extent and metadata items in this block
1078          * group, adding the free space between them and the free space at the
1079          * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1080          * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1081          * contained in.
1082          */
1083         key.objectid = block_group->key.objectid;
1084         key.type = BTRFS_EXTENT_ITEM_KEY;
1085         key.offset = 0;
1086
1087         ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1088         if (ret < 0)
1089                 goto out_locked;
1090         ASSERT(ret == 0);
1091
1092         start = block_group->key.objectid;
1093         end = block_group->key.objectid + block_group->key.offset;
1094         while (1) {
1095                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1096
1097                 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1098                     key.type == BTRFS_METADATA_ITEM_KEY) {
1099                         if (key.objectid >= end)
1100                                 break;
1101
1102                         if (start < key.objectid) {
1103                                 ret = __add_to_free_space_tree(trans,
1104                                                                block_group,
1105                                                                path2, start,
1106                                                                key.objectid -
1107                                                                start);
1108                                 if (ret)
1109                                         goto out_locked;
1110                         }
1111                         start = key.objectid;
1112                         if (key.type == BTRFS_METADATA_ITEM_KEY)
1113                                 start += fs_info->nodesize;
1114                         else
1115                                 start += key.offset;
1116                 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1117                         if (key.objectid != block_group->key.objectid)
1118                                 break;
1119                 }
1120
1121                 ret = btrfs_next_item(extent_root, path);
1122                 if (ret < 0)
1123                         goto out_locked;
1124                 if (ret)
1125                         break;
1126         }
1127         if (start < end) {
1128                 ret = __add_to_free_space_tree(trans, block_group, path2,
1129                                                start, end - start);
1130                 if (ret)
1131                         goto out_locked;
1132         }
1133
1134         ret = 0;
1135 out_locked:
1136         mutex_unlock(&block_group->free_space_lock);
1137 out:
1138         btrfs_free_path(path2);
1139         btrfs_free_path(path);
1140         return ret;
1141 }
1142
1143 int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1144 {
1145         struct btrfs_trans_handle *trans;
1146         struct btrfs_root *tree_root = fs_info->tree_root;
1147         struct btrfs_root *free_space_root;
1148         struct btrfs_block_group_cache *block_group;
1149         struct rb_node *node;
1150         int ret;
1151
1152         trans = btrfs_start_transaction(tree_root, 0);
1153         if (IS_ERR(trans))
1154                 return PTR_ERR(trans);
1155
1156         set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1157         free_space_root = btrfs_create_tree(trans, fs_info,
1158                                             BTRFS_FREE_SPACE_TREE_OBJECTID);
1159         if (IS_ERR(free_space_root)) {
1160                 ret = PTR_ERR(free_space_root);
1161                 goto abort;
1162         }
1163         fs_info->free_space_root = free_space_root;
1164
1165         node = rb_first(&fs_info->block_group_cache_tree);
1166         while (node) {
1167                 block_group = rb_entry(node, struct btrfs_block_group_cache,
1168                                        cache_node);
1169                 ret = populate_free_space_tree(trans, fs_info, block_group);
1170                 if (ret)
1171                         goto abort;
1172                 node = rb_next(node);
1173         }
1174
1175         btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1176         btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1177         clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1178
1179         return btrfs_commit_transaction(trans);
1180
1181 abort:
1182         clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
1183         btrfs_abort_transaction(trans, ret);
1184         btrfs_end_transaction(trans);
1185         return ret;
1186 }
1187
1188 static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1189                                  struct btrfs_root *root)
1190 {
1191         struct btrfs_path *path;
1192         struct btrfs_key key;
1193         int nr;
1194         int ret;
1195
1196         path = btrfs_alloc_path();
1197         if (!path)
1198                 return -ENOMEM;
1199
1200         path->leave_spinning = 1;
1201
1202         key.objectid = 0;
1203         key.type = 0;
1204         key.offset = 0;
1205
1206         while (1) {
1207                 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1208                 if (ret < 0)
1209                         goto out;
1210
1211                 nr = btrfs_header_nritems(path->nodes[0]);
1212                 if (!nr)
1213                         break;
1214
1215                 path->slots[0] = 0;
1216                 ret = btrfs_del_items(trans, root, path, 0, nr);
1217                 if (ret)
1218                         goto out;
1219
1220                 btrfs_release_path(path);
1221         }
1222
1223         ret = 0;
1224 out:
1225         btrfs_free_path(path);
1226         return ret;
1227 }
1228
1229 int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1230 {
1231         struct btrfs_trans_handle *trans;
1232         struct btrfs_root *tree_root = fs_info->tree_root;
1233         struct btrfs_root *free_space_root = fs_info->free_space_root;
1234         int ret;
1235
1236         trans = btrfs_start_transaction(tree_root, 0);
1237         if (IS_ERR(trans))
1238                 return PTR_ERR(trans);
1239
1240         btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
1241         btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
1242         fs_info->free_space_root = NULL;
1243
1244         ret = clear_free_space_tree(trans, free_space_root);
1245         if (ret)
1246                 goto abort;
1247
1248         ret = btrfs_del_root(trans, fs_info, &free_space_root->root_key);
1249         if (ret)
1250                 goto abort;
1251
1252         list_del(&free_space_root->dirty_list);
1253
1254         btrfs_tree_lock(free_space_root->node);
1255         clean_tree_block(fs_info, free_space_root->node);
1256         btrfs_tree_unlock(free_space_root->node);
1257         btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1258                               0, 1);
1259
1260         free_extent_buffer(free_space_root->node);
1261         free_extent_buffer(free_space_root->commit_root);
1262         kfree(free_space_root);
1263
1264         return btrfs_commit_transaction(trans);
1265
1266 abort:
1267         btrfs_abort_transaction(trans, ret);
1268         btrfs_end_transaction(trans);
1269         return ret;
1270 }
1271
1272 static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
1273                                         struct btrfs_block_group_cache *block_group,
1274                                         struct btrfs_path *path)
1275 {
1276         int ret;
1277
1278         block_group->needs_free_space = 0;
1279
1280         ret = add_new_free_space_info(trans, trans->fs_info, block_group, path);
1281         if (ret)
1282                 return ret;
1283
1284         return __add_to_free_space_tree(trans, block_group, path,
1285                                         block_group->key.objectid,
1286                                         block_group->key.offset);
1287 }
1288
1289 int add_block_group_free_space(struct btrfs_trans_handle *trans,
1290                                struct btrfs_block_group_cache *block_group)
1291 {
1292         struct btrfs_fs_info *fs_info = trans->fs_info;
1293         struct btrfs_path *path = NULL;
1294         int ret = 0;
1295
1296         if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1297                 return 0;
1298
1299         mutex_lock(&block_group->free_space_lock);
1300         if (!block_group->needs_free_space)
1301                 goto out;
1302
1303         path = btrfs_alloc_path();
1304         if (!path) {
1305                 ret = -ENOMEM;
1306                 goto out;
1307         }
1308
1309         ret = __add_block_group_free_space(trans, block_group, path);
1310
1311 out:
1312         btrfs_free_path(path);
1313         mutex_unlock(&block_group->free_space_lock);
1314         if (ret)
1315                 btrfs_abort_transaction(trans, ret);
1316         return ret;
1317 }
1318
1319 int remove_block_group_free_space(struct btrfs_trans_handle *trans,
1320                                   struct btrfs_fs_info *fs_info,
1321                                   struct btrfs_block_group_cache *block_group)
1322 {
1323         struct btrfs_root *root = fs_info->free_space_root;
1324         struct btrfs_path *path;
1325         struct btrfs_key key, found_key;
1326         struct extent_buffer *leaf;
1327         u64 start, end;
1328         int done = 0, nr;
1329         int ret;
1330
1331         if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1332                 return 0;
1333
1334         if (block_group->needs_free_space) {
1335                 /* We never added this block group to the free space tree. */
1336                 return 0;
1337         }
1338
1339         path = btrfs_alloc_path();
1340         if (!path) {
1341                 ret = -ENOMEM;
1342                 goto out;
1343         }
1344
1345         start = block_group->key.objectid;
1346         end = block_group->key.objectid + block_group->key.offset;
1347
1348         key.objectid = end - 1;
1349         key.type = (u8)-1;
1350         key.offset = (u64)-1;
1351
1352         while (!done) {
1353                 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1354                 if (ret)
1355                         goto out;
1356
1357                 leaf = path->nodes[0];
1358                 nr = 0;
1359                 path->slots[0]++;
1360                 while (path->slots[0] > 0) {
1361                         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1362
1363                         if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1364                                 ASSERT(found_key.objectid == block_group->key.objectid);
1365                                 ASSERT(found_key.offset == block_group->key.offset);
1366                                 done = 1;
1367                                 nr++;
1368                                 path->slots[0]--;
1369                                 break;
1370                         } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1371                                    found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1372                                 ASSERT(found_key.objectid >= start);
1373                                 ASSERT(found_key.objectid < end);
1374                                 ASSERT(found_key.objectid + found_key.offset <= end);
1375                                 nr++;
1376                                 path->slots[0]--;
1377                         } else {
1378                                 ASSERT(0);
1379                         }
1380                 }
1381
1382                 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1383                 if (ret)
1384                         goto out;
1385                 btrfs_release_path(path);
1386         }
1387
1388         ret = 0;
1389 out:
1390         btrfs_free_path(path);
1391         if (ret)
1392                 btrfs_abort_transaction(trans, ret);
1393         return ret;
1394 }
1395
1396 static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1397                                    struct btrfs_path *path,
1398                                    u32 expected_extent_count)
1399 {
1400         struct btrfs_block_group_cache *block_group;
1401         struct btrfs_fs_info *fs_info;
1402         struct btrfs_root *root;
1403         struct btrfs_key key;
1404         int prev_bit = 0, bit;
1405         /* Initialize to silence GCC. */
1406         u64 extent_start = 0;
1407         u64 end, offset;
1408         u64 total_found = 0;
1409         u32 extent_count = 0;
1410         int ret;
1411
1412         block_group = caching_ctl->block_group;
1413         fs_info = block_group->fs_info;
1414         root = fs_info->free_space_root;
1415
1416         end = block_group->key.objectid + block_group->key.offset;
1417
1418         while (1) {
1419                 ret = btrfs_next_item(root, path);
1420                 if (ret < 0)
1421                         goto out;
1422                 if (ret)
1423                         break;
1424
1425                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1426
1427                 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1428                         break;
1429
1430                 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1431                 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1432
1433                 caching_ctl->progress = key.objectid;
1434
1435                 offset = key.objectid;
1436                 while (offset < key.objectid + key.offset) {
1437                         bit = free_space_test_bit(block_group, path, offset);
1438                         if (prev_bit == 0 && bit == 1) {
1439                                 extent_start = offset;
1440                         } else if (prev_bit == 1 && bit == 0) {
1441                                 total_found += add_new_free_space(block_group,
1442                                                                   fs_info,
1443                                                                   extent_start,
1444                                                                   offset);
1445                                 if (total_found > CACHING_CTL_WAKE_UP) {
1446                                         total_found = 0;
1447                                         wake_up(&caching_ctl->wait);
1448                                 }
1449                                 extent_count++;
1450                         }
1451                         prev_bit = bit;
1452                         offset += fs_info->sectorsize;
1453                 }
1454         }
1455         if (prev_bit == 1) {
1456                 total_found += add_new_free_space(block_group, fs_info,
1457                                                   extent_start, end);
1458                 extent_count++;
1459         }
1460
1461         if (extent_count != expected_extent_count) {
1462                 btrfs_err(fs_info,
1463                           "incorrect extent count for %llu; counted %u, expected %u",
1464                           block_group->key.objectid, extent_count,
1465                           expected_extent_count);
1466                 ASSERT(0);
1467                 ret = -EIO;
1468                 goto out;
1469         }
1470
1471         caching_ctl->progress = (u64)-1;
1472
1473         ret = 0;
1474 out:
1475         return ret;
1476 }
1477
1478 static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1479                                    struct btrfs_path *path,
1480                                    u32 expected_extent_count)
1481 {
1482         struct btrfs_block_group_cache *block_group;
1483         struct btrfs_fs_info *fs_info;
1484         struct btrfs_root *root;
1485         struct btrfs_key key;
1486         u64 end;
1487         u64 total_found = 0;
1488         u32 extent_count = 0;
1489         int ret;
1490
1491         block_group = caching_ctl->block_group;
1492         fs_info = block_group->fs_info;
1493         root = fs_info->free_space_root;
1494
1495         end = block_group->key.objectid + block_group->key.offset;
1496
1497         while (1) {
1498                 ret = btrfs_next_item(root, path);
1499                 if (ret < 0)
1500                         goto out;
1501                 if (ret)
1502                         break;
1503
1504                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1505
1506                 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1507                         break;
1508
1509                 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1510                 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1511
1512                 caching_ctl->progress = key.objectid;
1513
1514                 total_found += add_new_free_space(block_group, fs_info,
1515                                                   key.objectid,
1516                                                   key.objectid + key.offset);
1517                 if (total_found > CACHING_CTL_WAKE_UP) {
1518                         total_found = 0;
1519                         wake_up(&caching_ctl->wait);
1520                 }
1521                 extent_count++;
1522         }
1523
1524         if (extent_count != expected_extent_count) {
1525                 btrfs_err(fs_info,
1526                           "incorrect extent count for %llu; counted %u, expected %u",
1527                           block_group->key.objectid, extent_count,
1528                           expected_extent_count);
1529                 ASSERT(0);
1530                 ret = -EIO;
1531                 goto out;
1532         }
1533
1534         caching_ctl->progress = (u64)-1;
1535
1536         ret = 0;
1537 out:
1538         return ret;
1539 }
1540
1541 int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1542 {
1543         struct btrfs_block_group_cache *block_group;
1544         struct btrfs_fs_info *fs_info;
1545         struct btrfs_free_space_info *info;
1546         struct btrfs_path *path;
1547         u32 extent_count, flags;
1548         int ret;
1549
1550         block_group = caching_ctl->block_group;
1551         fs_info = block_group->fs_info;
1552
1553         path = btrfs_alloc_path();
1554         if (!path)
1555                 return -ENOMEM;
1556
1557         /*
1558          * Just like caching_thread() doesn't want to deadlock on the extent
1559          * tree, we don't want to deadlock on the free space tree.
1560          */
1561         path->skip_locking = 1;
1562         path->search_commit_root = 1;
1563         path->reada = READA_FORWARD;
1564
1565         info = search_free_space_info(NULL, fs_info, block_group, path, 0);
1566         if (IS_ERR(info)) {
1567                 ret = PTR_ERR(info);
1568                 goto out;
1569         }
1570         extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1571         flags = btrfs_free_space_flags(path->nodes[0], info);
1572
1573         /*
1574          * We left path pointing to the free space info item, so now
1575          * load_free_space_foo can just iterate through the free space tree from
1576          * there.
1577          */
1578         if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1579                 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1580         else
1581                 ret = load_free_space_extents(caching_ctl, path, extent_count);
1582
1583 out:
1584         btrfs_free_path(path);
1585         return ret;
1586 }