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