2 * Copyright (C) 2015 Facebook. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
21 #include "free-space-cache.h"
22 #include "free-space-tree.h"
24 static struct btrfs_free_space_info *
25 search_free_space_info(struct btrfs_trans_handle *trans,
26 struct btrfs_fs_info *fs_info,
27 struct btrfs_block_group_cache *block_group,
28 struct btrfs_path *path, int cow)
30 struct btrfs_root *root = fs_info->free_space_root;
34 key.objectid = block_group->key.objectid;
35 key.type = BTRFS_FREE_SPACE_INFO_KEY;
36 key.offset = block_group->key.offset;
38 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
42 return ERR_PTR(-ENOENT);
44 return btrfs_item_ptr(path->nodes[0], path->slots[0],
45 struct btrfs_free_space_info);
48 static int free_space_test_bit(struct btrfs_block_group_cache *block_group,
49 struct btrfs_path *path, u64 offset,
52 struct extent_buffer *leaf;
54 u64 found_start, found_end;
57 leaf = path->nodes[0];
58 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
59 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
61 found_start = key.objectid;
62 found_end = key.objectid + key.offset;
63 ASSERT(offset >= found_start && offset < found_end);
65 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
66 i = (offset - found_start) / sectorsize;
67 return !!extent_buffer_test_bit(leaf, ptr, i);
70 static int load_free_space_bitmaps(struct btrfs_fs_info *fs_info,
71 struct btrfs_block_group_cache *block_group,
72 struct btrfs_path *path,
73 u32 expected_extent_count,
76 struct btrfs_root *root = fs_info->free_space_root;
78 int prev_bit = 0, bit;
80 u64 start, end, offset;
84 start = block_group->key.objectid;
85 end = block_group->key.objectid + block_group->key.offset;
88 ret = btrfs_next_item(root, path);
94 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
96 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
99 if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY) {
100 fprintf(stderr, "unexpected key of type %u\n", key.type);
104 if (key.objectid >= end) {
105 fprintf(stderr, "free space bitmap starts at %Lu, beyond end of block group %Lu-%Lu\n",
106 key.objectid, start, end);
110 if (key.objectid + key.offset > end) {
111 fprintf(stderr, "free space bitmap ends at %Lu, beyond end of block group %Lu-%Lu\n",
112 key.objectid, start, end);
117 offset = key.objectid;
118 while (offset < key.objectid + key.offset) {
119 bit = free_space_test_bit(block_group, path, offset,
121 if (prev_bit == 0 && bit == 1) {
122 extent_start = offset;
123 } else if (prev_bit == 1 && bit == 0) {
124 add_new_free_space(block_group, fs_info, extent_start, offset);
128 offset += root->sectorsize;
133 add_new_free_space(block_group, fs_info, extent_start, end);
137 if (extent_count != expected_extent_count) {
138 fprintf(stderr, "free space info recorded %u extents, counted %u\n",
139 expected_extent_count, extent_count);
148 static int load_free_space_extents(struct btrfs_fs_info *fs_info,
149 struct btrfs_block_group_cache *block_group,
150 struct btrfs_path *path,
151 u32 expected_extent_count,
154 struct btrfs_root *root = fs_info->free_space_root;
155 struct btrfs_key key, prev_key;
158 u32 extent_count = 0;
161 start = block_group->key.objectid;
162 end = block_group->key.objectid + block_group->key.offset;
165 ret = btrfs_next_item(root, path);
171 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
173 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
176 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
177 fprintf(stderr, "unexpected key of type %u\n", key.type);
181 if (key.objectid >= end) {
182 fprintf(stderr, "free space extent starts at %Lu, beyond end of block group %Lu-%Lu\n",
183 key.objectid, start, end);
187 if (key.objectid + key.offset > end) {
188 fprintf(stderr, "free space extent ends at %Lu, beyond end of block group %Lu-%Lu\n",
189 key.objectid, start, end);
195 u64 cur_start = key.objectid;
196 u64 cur_end = cur_start + key.offset;
197 u64 prev_start = prev_key.objectid;
198 u64 prev_end = prev_start + prev_key.offset;
200 if (cur_start < prev_end) {
201 fprintf(stderr, "free space extent %Lu-%Lu overlaps with previous %Lu-%Lu\n",
203 prev_start, prev_end);
205 } else if (cur_start == prev_end) {
206 fprintf(stderr, "free space extent %Lu-%Lu is unmerged with previous %Lu-%Lu\n",
208 prev_start, prev_end);
213 add_new_free_space(block_group, fs_info, key.objectid, key.objectid + key.offset);
220 if (extent_count != expected_extent_count) {
221 fprintf(stderr, "free space info recorded %u extents, counted %u\n",
222 expected_extent_count, extent_count);
231 int load_free_space_tree(struct btrfs_fs_info *fs_info,
232 struct btrfs_block_group_cache *block_group)
234 struct btrfs_free_space_info *info;
235 struct btrfs_path *path;
236 u32 extent_count, flags;
240 path = btrfs_alloc_path();
245 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
250 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
251 flags = btrfs_free_space_flags(path->nodes[0], info);
253 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
254 ret = load_free_space_bitmaps(fs_info, block_group, path,
255 extent_count, &errors);
257 ret = load_free_space_extents(fs_info, block_group, path,
258 extent_count, &errors);
265 btrfs_free_path(path);
266 return ret ? ret : errors;