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