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