btrfs-progs: check: introduce function to check shared data backref
[platform/upstream/btrfs-progs.git] / free-space-tree.c
1 /*
2  * Copyright (C) 2015 Facebook.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include "ctree.h"
20 #include "disk-io.h"
21 #include "free-space-cache.h"
22 #include "free-space-tree.h"
23
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)
29 {
30         struct btrfs_root *root = fs_info->free_space_root;
31         struct btrfs_key key;
32         int ret;
33
34         key.objectid = block_group->key.objectid;
35         key.type = BTRFS_FREE_SPACE_INFO_KEY;
36         key.offset = block_group->key.offset;
37
38         ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
39         if (ret < 0)
40                 return ERR_PTR(ret);
41         if (ret != 0)
42                 return ERR_PTR(-ENOENT);
43
44         return btrfs_item_ptr(path->nodes[0], path->slots[0],
45                               struct btrfs_free_space_info);
46 }
47
48 static int free_space_test_bit(struct btrfs_block_group_cache *block_group,
49                                struct btrfs_path *path, u64 offset,
50                                u64 sectorsize)
51 {
52         struct extent_buffer *leaf;
53         struct btrfs_key key;
54         u64 found_start, found_end;
55         unsigned long ptr, i;
56
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);
60
61         found_start = key.objectid;
62         found_end = key.objectid + key.offset;
63         ASSERT(offset >= found_start && offset < found_end);
64
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);
68 }
69
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,
74                                    int *errors)
75 {
76         struct btrfs_root *root = fs_info->free_space_root;
77         struct btrfs_key key;
78         int prev_bit = 0, bit;
79         u64 extent_start = 0;
80         u64 start, end, offset;
81         u32 extent_count = 0;
82         int ret;
83
84         start = block_group->key.objectid;
85         end = block_group->key.objectid + block_group->key.offset;
86
87         while (1) {
88                 ret = btrfs_next_item(root, path);
89                 if (ret < 0)
90                         goto out;
91                 if (ret)
92                         break;
93
94                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
95
96                 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
97                         break;
98
99                 if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY) {
100                         fprintf(stderr, "unexpected key of type %u\n", key.type);
101                         (*errors)++;
102                         break;
103                 }
104                 if (key.objectid >= end) {
105                         fprintf(stderr,
106         "free space bitmap starts at %llu, beyond end of block group %llu-%llu\n",
107                                 key.objectid, start, end);
108                         (*errors)++;
109                         break;
110                 }
111                 if (key.objectid + key.offset > end) {
112                         fprintf(stderr,
113         "free space bitmap ends at %llu, beyond end of block group %llu-%llu\n",
114                                 key.objectid, start, end);
115                         (*errors)++;
116                         break;
117                 }
118
119                 offset = key.objectid;
120                 while (offset < key.objectid + key.offset) {
121                         bit = free_space_test_bit(block_group, path, offset,
122                                                   root->sectorsize);
123                         if (prev_bit == 0 && bit == 1) {
124                                 extent_start = offset;
125                         } else if (prev_bit == 1 && bit == 0) {
126                                 add_new_free_space(block_group, fs_info, extent_start, offset);
127                                 extent_count++;
128                         }
129                         prev_bit = bit;
130                         offset += root->sectorsize;
131                 }
132         }
133
134         if (prev_bit == 1) {
135                 add_new_free_space(block_group, fs_info, extent_start, end);
136                 extent_count++;
137         }
138
139         if (extent_count != expected_extent_count) {
140                 fprintf(stderr, "free space info recorded %u extents, counted %u\n",
141                         expected_extent_count, extent_count);
142                 (*errors)++;
143         }
144
145         ret = 0;
146 out:
147         return ret;
148 }
149
150 static int load_free_space_extents(struct btrfs_fs_info *fs_info,
151                                    struct btrfs_block_group_cache *block_group,
152                                    struct btrfs_path *path,
153                                    u32 expected_extent_count,
154                                    int *errors)
155 {
156         struct btrfs_root *root = fs_info->free_space_root;
157         struct btrfs_key key, prev_key;
158         int have_prev = 0;
159         u64 start, end;
160         u32 extent_count = 0;
161         int ret;
162
163         start = block_group->key.objectid;
164         end = block_group->key.objectid + block_group->key.offset;
165
166         while (1) {
167                 ret = btrfs_next_item(root, path);
168                 if (ret < 0)
169                         goto out;
170                 if (ret)
171                         break;
172
173                 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
174
175                 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
176                         break;
177
178                 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
179                         fprintf(stderr, "unexpected key of type %u\n", key.type);
180                         (*errors)++;
181                         break;
182                 }
183                 if (key.objectid >= end) {
184                         fprintf(stderr,
185         "free space extent starts at %llu, beyond end of block group %llu-%llu\n",
186                                 key.objectid, start, end);
187                         (*errors)++;
188                         break;
189                 }
190                 if (key.objectid + key.offset > end) {
191                         fprintf(stderr,
192         "free space extent ends at %llu, beyond end of block group %llu-%llu\n",
193                                 key.objectid, start, end);
194                         (*errors)++;
195                         break;
196                 }
197
198                 if (have_prev) {
199                         u64 cur_start = key.objectid;
200                         u64 cur_end = cur_start + key.offset;
201                         u64 prev_start = prev_key.objectid;
202                         u64 prev_end = prev_start + prev_key.offset;
203
204                         if (cur_start < prev_end) {
205                                 fprintf(stderr,
206         "free space extent %llu-%llu overlaps with previous %llu-%llu\n",
207                                         cur_start, cur_end,
208                                         prev_start, prev_end);
209                                 (*errors)++;
210                         } else if (cur_start == prev_end) {
211                                 fprintf(stderr,
212         "free space extent %llu-%llu is unmerged with previous %llu-%llu\n",
213                                         cur_start, cur_end,
214                                         prev_start, prev_end);
215                                 (*errors)++;
216                         }
217                 }
218
219                 add_new_free_space(block_group, fs_info, key.objectid, key.objectid + key.offset);
220                 extent_count++;
221
222                 prev_key = key;
223                 have_prev = 1;
224         }
225
226         if (extent_count != expected_extent_count) {
227                 fprintf(stderr, "free space info recorded %u extents, counted %u\n",
228                         expected_extent_count, extent_count);
229                 (*errors)++;
230         }
231
232         ret = 0;
233 out:
234         return ret;
235 }
236
237 int load_free_space_tree(struct btrfs_fs_info *fs_info,
238                          struct btrfs_block_group_cache *block_group)
239 {
240         struct btrfs_free_space_info *info;
241         struct btrfs_path *path;
242         u32 extent_count, flags;
243         int errors = 0;
244         int ret;
245
246         path = btrfs_alloc_path();
247         if (!path)
248                 return -ENOMEM;
249         path->reada = 1;
250
251         info = search_free_space_info(NULL, fs_info, block_group, path, 0);
252         if (IS_ERR(info)) {
253                 ret = PTR_ERR(info);
254                 goto out;
255         }
256         extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
257         flags = btrfs_free_space_flags(path->nodes[0], info);
258
259         if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
260                 ret = load_free_space_bitmaps(fs_info, block_group, path,
261                                               extent_count, &errors);
262         } else {
263                 ret = load_free_space_extents(fs_info, block_group, path,
264                                               extent_count, &errors);
265         }
266         if (ret)
267                 goto out;
268
269         ret = 0;
270 out:
271         btrfs_free_path(path);
272         return ret ? ret : errors;
273 }