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