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