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