blob: e314daeb5072c4f9a206361741140d98f19a1e17 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Omar Sandovala5ed9182015-09-29 20:50:35 -07002/*
3 * Copyright (C) 2015 Facebook. All rights reserved.
Omar Sandovala5ed9182015-09-29 20:50:35 -07004 */
5
6#include <linux/kernel.h>
Omar Sandoval25ff17e2017-06-05 00:12:31 -07007#include <linux/sched/mm.h>
Omar Sandovala5ed9182015-09-29 20:50:35 -07008#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,
Omar Sandovala5ed9182015-09-29 20:50:35 -070015 struct btrfs_block_group_cache *block_group,
16 struct btrfs_path *path);
17
18void 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 */
Jeff Mahoneyda170662016-06-15 09:22:56 -040028 bitmap_range = cache->fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
Omar Sandovala5ed9182015-09-29 20:50:35 -070029 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
46static int add_new_free_space_info(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -070047 struct btrfs_block_group_cache *block_group,
48 struct btrfs_path *path)
49{
Nikolay Borisov66afee12018-05-10 15:44:44 +030050 struct btrfs_root *root = trans->fs_info->free_space_root;
Omar Sandovala5ed9182015-09-29 20:50:35 -070051 struct btrfs_free_space_info *info;
52 struct btrfs_key key;
53 struct extent_buffer *leaf;
54 int ret;
55
56 key.objectid = block_group->key.objectid;
57 key.type = BTRFS_FREE_SPACE_INFO_KEY;
58 key.offset = block_group->key.offset;
59
60 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*info));
61 if (ret)
62 goto out;
63
64 leaf = path->nodes[0];
65 info = btrfs_item_ptr(leaf, path->slots[0],
66 struct btrfs_free_space_info);
67 btrfs_set_free_space_extent_count(leaf, info, 0);
68 btrfs_set_free_space_flags(leaf, info, 0);
69 btrfs_mark_buffer_dirty(leaf);
70
71 ret = 0;
72out:
73 btrfs_release_path(path);
74 return ret;
75}
76
77struct btrfs_free_space_info *
78search_free_space_info(struct btrfs_trans_handle *trans,
79 struct btrfs_fs_info *fs_info,
80 struct btrfs_block_group_cache *block_group,
81 struct btrfs_path *path, int cow)
82{
83 struct btrfs_root *root = fs_info->free_space_root;
84 struct btrfs_key key;
85 int ret;
86
87 key.objectid = block_group->key.objectid;
88 key.type = BTRFS_FREE_SPACE_INFO_KEY;
89 key.offset = block_group->key.offset;
90
91 ret = btrfs_search_slot(trans, root, &key, path, 0, cow);
92 if (ret < 0)
93 return ERR_PTR(ret);
94 if (ret != 0) {
Jeff Mahoneyab8d0fc2016-09-20 10:05:02 -040095 btrfs_warn(fs_info, "missing free space info for %llu",
Omar Sandovala5ed9182015-09-29 20:50:35 -070096 block_group->key.objectid);
97 ASSERT(0);
98 return ERR_PTR(-ENOENT);
99 }
100
101 return btrfs_item_ptr(path->nodes[0], path->slots[0],
102 struct btrfs_free_space_info);
103}
104
105/*
106 * btrfs_search_slot() but we're looking for the greatest key less than the
107 * passed key.
108 */
109static int btrfs_search_prev_slot(struct btrfs_trans_handle *trans,
110 struct btrfs_root *root,
111 struct btrfs_key *key, struct btrfs_path *p,
112 int ins_len, int cow)
113{
114 int ret;
115
116 ret = btrfs_search_slot(trans, root, key, p, ins_len, cow);
117 if (ret < 0)
118 return ret;
119
120 if (ret == 0) {
121 ASSERT(0);
122 return -EIO;
123 }
124
125 if (p->slots[0] == 0) {
126 ASSERT(0);
127 return -EIO;
128 }
129 p->slots[0]--;
130
131 return 0;
132}
133
134static inline u32 free_space_bitmap_size(u64 size, u32 sectorsize)
135{
136 return DIV_ROUND_UP((u32)div_u64(size, sectorsize), BITS_PER_BYTE);
137}
138
Howard McLauchlana5659712018-04-18 18:02:36 -0700139static unsigned long *alloc_bitmap(u32 bitmap_size)
Omar Sandovala5ed9182015-09-29 20:50:35 -0700140{
Howard McLauchlana5659712018-04-18 18:02:36 -0700141 unsigned long *ret;
Omar Sandoval25ff17e2017-06-05 00:12:31 -0700142 unsigned int nofs_flag;
Howard McLauchlana5659712018-04-18 18:02:36 -0700143 u32 bitmap_rounded_size = round_up(bitmap_size, sizeof(unsigned long));
David Sterba79b134a2016-01-22 17:16:18 +0100144
145 /*
Omar Sandoval25ff17e2017-06-05 00:12:31 -0700146 * GFP_NOFS doesn't work with kvmalloc(), but we really can't recurse
147 * into the filesystem as the free space bitmap can be modified in the
148 * critical section of a transaction commit.
149 *
150 * TODO: push the memalloc_nofs_{save,restore}() to the caller where we
151 * know that recursion is unsafe.
David Sterba79b134a2016-01-22 17:16:18 +0100152 */
Omar Sandoval25ff17e2017-06-05 00:12:31 -0700153 nofs_flag = memalloc_nofs_save();
Howard McLauchlana5659712018-04-18 18:02:36 -0700154 ret = kvzalloc(bitmap_rounded_size, GFP_KERNEL);
Omar Sandoval25ff17e2017-06-05 00:12:31 -0700155 memalloc_nofs_restore(nofs_flag);
156 return ret;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700157}
158
Howard McLauchlana5659712018-04-18 18:02:36 -0700159static void le_bitmap_set(unsigned long *map, unsigned int start, int len)
Howard McLauchlan6faa8f42018-04-18 18:02:35 -0700160{
Howard McLauchlana5659712018-04-18 18:02:36 -0700161 u8 *p = ((u8 *)map) + BIT_BYTE(start);
Howard McLauchlan6faa8f42018-04-18 18:02:35 -0700162 const unsigned int size = start + len;
163 int bits_to_set = BITS_PER_BYTE - (start % BITS_PER_BYTE);
164 u8 mask_to_set = BITMAP_FIRST_BYTE_MASK(start);
165
166 while (len - bits_to_set >= 0) {
167 *p |= mask_to_set;
168 len -= bits_to_set;
169 bits_to_set = BITS_PER_BYTE;
170 mask_to_set = ~0;
171 p++;
172 }
173 if (len) {
174 mask_to_set &= BITMAP_LAST_BYTE_MASK(size);
175 *p |= mask_to_set;
176 }
177}
178
Omar Sandovala5ed9182015-09-29 20:50:35 -0700179int convert_free_space_to_bitmaps(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700180 struct btrfs_block_group_cache *block_group,
181 struct btrfs_path *path)
182{
Nikolay Borisov719fb4d2018-05-10 15:44:47 +0300183 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700184 struct btrfs_root *root = fs_info->free_space_root;
185 struct btrfs_free_space_info *info;
186 struct btrfs_key key, found_key;
187 struct extent_buffer *leaf;
Howard McLauchlana5659712018-04-18 18:02:36 -0700188 unsigned long *bitmap;
189 char *bitmap_cursor;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700190 u64 start, end;
191 u64 bitmap_range, i;
192 u32 bitmap_size, flags, expected_extent_count;
193 u32 extent_count = 0;
194 int done = 0, nr;
195 int ret;
196
197 bitmap_size = free_space_bitmap_size(block_group->key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400198 fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700199 bitmap = alloc_bitmap(bitmap_size);
200 if (!bitmap) {
201 ret = -ENOMEM;
202 goto out;
203 }
204
205 start = block_group->key.objectid;
206 end = block_group->key.objectid + block_group->key.offset;
207
208 key.objectid = end - 1;
209 key.type = (u8)-1;
210 key.offset = (u64)-1;
211
212 while (!done) {
213 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
214 if (ret)
215 goto out;
216
217 leaf = path->nodes[0];
218 nr = 0;
219 path->slots[0]++;
220 while (path->slots[0] > 0) {
221 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
222
223 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
224 ASSERT(found_key.objectid == block_group->key.objectid);
225 ASSERT(found_key.offset == block_group->key.offset);
226 done = 1;
227 break;
228 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY) {
229 u64 first, last;
230
231 ASSERT(found_key.objectid >= start);
232 ASSERT(found_key.objectid < end);
233 ASSERT(found_key.objectid + found_key.offset <= end);
234
235 first = div_u64(found_key.objectid - start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400236 fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700237 last = div_u64(found_key.objectid + found_key.offset - start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400238 fs_info->sectorsize);
Omar Sandoval2fe1d552016-09-22 17:24:20 -0700239 le_bitmap_set(bitmap, first, last - first);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700240
241 extent_count++;
242 nr++;
243 path->slots[0]--;
244 } else {
245 ASSERT(0);
246 }
247 }
248
249 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
250 if (ret)
251 goto out;
252 btrfs_release_path(path);
253 }
254
255 info = search_free_space_info(trans, fs_info, block_group, path, 1);
256 if (IS_ERR(info)) {
257 ret = PTR_ERR(info);
258 goto out;
259 }
260 leaf = path->nodes[0];
261 flags = btrfs_free_space_flags(leaf, info);
262 flags |= BTRFS_FREE_SPACE_USING_BITMAPS;
263 btrfs_set_free_space_flags(leaf, info, flags);
264 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
265 btrfs_mark_buffer_dirty(leaf);
266 btrfs_release_path(path);
267
268 if (extent_count != expected_extent_count) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400269 btrfs_err(fs_info,
270 "incorrect extent count for %llu; counted %u, expected %u",
Omar Sandovala5ed9182015-09-29 20:50:35 -0700271 block_group->key.objectid, extent_count,
272 expected_extent_count);
273 ASSERT(0);
274 ret = -EIO;
275 goto out;
276 }
277
Howard McLauchlana5659712018-04-18 18:02:36 -0700278 bitmap_cursor = (char *)bitmap;
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400279 bitmap_range = fs_info->sectorsize * BTRFS_FREE_SPACE_BITMAP_BITS;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700280 i = start;
281 while (i < end) {
282 unsigned long ptr;
283 u64 extent_size;
284 u32 data_size;
285
286 extent_size = min(end - i, bitmap_range);
287 data_size = free_space_bitmap_size(extent_size,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400288 fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700289
290 key.objectid = i;
291 key.type = BTRFS_FREE_SPACE_BITMAP_KEY;
292 key.offset = extent_size;
293
294 ret = btrfs_insert_empty_item(trans, root, path, &key,
295 data_size);
296 if (ret)
297 goto out;
298
299 leaf = path->nodes[0];
300 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
301 write_extent_buffer(leaf, bitmap_cursor, ptr,
302 data_size);
303 btrfs_mark_buffer_dirty(leaf);
304 btrfs_release_path(path);
305
306 i += extent_size;
307 bitmap_cursor += data_size;
308 }
309
310 ret = 0;
311out:
David Sterba79b134a2016-01-22 17:16:18 +0100312 kvfree(bitmap);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700313 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400314 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700315 return ret;
316}
317
318int convert_free_space_to_extents(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700319 struct btrfs_block_group_cache *block_group,
320 struct btrfs_path *path)
321{
Nikolay Borisov5296c2b2018-05-10 15:44:48 +0300322 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700323 struct btrfs_root *root = fs_info->free_space_root;
324 struct btrfs_free_space_info *info;
325 struct btrfs_key key, found_key;
326 struct extent_buffer *leaf;
Howard McLauchlana5659712018-04-18 18:02:36 -0700327 unsigned long *bitmap;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700328 u64 start, end;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700329 u32 bitmap_size, flags, expected_extent_count;
Howard McLauchlana5659712018-04-18 18:02:36 -0700330 unsigned long nrbits, start_bit, end_bit;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700331 u32 extent_count = 0;
332 int done = 0, nr;
333 int ret;
334
335 bitmap_size = free_space_bitmap_size(block_group->key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400336 fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700337 bitmap = alloc_bitmap(bitmap_size);
338 if (!bitmap) {
339 ret = -ENOMEM;
340 goto out;
341 }
342
343 start = block_group->key.objectid;
344 end = block_group->key.objectid + block_group->key.offset;
345
346 key.objectid = end - 1;
347 key.type = (u8)-1;
348 key.offset = (u64)-1;
349
350 while (!done) {
351 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
352 if (ret)
353 goto out;
354
355 leaf = path->nodes[0];
356 nr = 0;
357 path->slots[0]++;
358 while (path->slots[0] > 0) {
359 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
360
361 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
362 ASSERT(found_key.objectid == block_group->key.objectid);
363 ASSERT(found_key.offset == block_group->key.offset);
364 done = 1;
365 break;
366 } else if (found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
367 unsigned long ptr;
Howard McLauchlana5659712018-04-18 18:02:36 -0700368 char *bitmap_cursor;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700369 u32 bitmap_pos, data_size;
370
371 ASSERT(found_key.objectid >= start);
372 ASSERT(found_key.objectid < end);
373 ASSERT(found_key.objectid + found_key.offset <= end);
374
375 bitmap_pos = div_u64(found_key.objectid - start,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400376 fs_info->sectorsize *
Omar Sandovala5ed9182015-09-29 20:50:35 -0700377 BITS_PER_BYTE);
Howard McLauchlana5659712018-04-18 18:02:36 -0700378 bitmap_cursor = ((char *)bitmap) + bitmap_pos;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700379 data_size = free_space_bitmap_size(found_key.offset,
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400380 fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700381
382 ptr = btrfs_item_ptr_offset(leaf, path->slots[0] - 1);
383 read_extent_buffer(leaf, bitmap_cursor, ptr,
384 data_size);
385
386 nr++;
387 path->slots[0]--;
388 } else {
389 ASSERT(0);
390 }
391 }
392
393 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
394 if (ret)
395 goto out;
396 btrfs_release_path(path);
397 }
398
399 info = search_free_space_info(trans, fs_info, block_group, path, 1);
400 if (IS_ERR(info)) {
401 ret = PTR_ERR(info);
402 goto out;
403 }
404 leaf = path->nodes[0];
405 flags = btrfs_free_space_flags(leaf, info);
406 flags &= ~BTRFS_FREE_SPACE_USING_BITMAPS;
407 btrfs_set_free_space_flags(leaf, info, flags);
408 expected_extent_count = btrfs_free_space_extent_count(leaf, info);
409 btrfs_mark_buffer_dirty(leaf);
410 btrfs_release_path(path);
411
Howard McLauchlana5659712018-04-18 18:02:36 -0700412 nrbits = div_u64(block_group->key.offset, block_group->fs_info->sectorsize);
413 start_bit = find_next_bit_le(bitmap, nrbits, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700414
Howard McLauchlana5659712018-04-18 18:02:36 -0700415 while (start_bit < nrbits) {
416 end_bit = find_next_zero_bit_le(bitmap, nrbits, start_bit);
417 ASSERT(start_bit < end_bit);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700418
Howard McLauchlana5659712018-04-18 18:02:36 -0700419 key.objectid = start + start_bit * block_group->fs_info->sectorsize;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700420 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
Howard McLauchlana5659712018-04-18 18:02:36 -0700421 key.offset = (end_bit - start_bit) * block_group->fs_info->sectorsize;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700422
423 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
424 if (ret)
425 goto out;
426 btrfs_release_path(path);
427
428 extent_count++;
Howard McLauchlana5659712018-04-18 18:02:36 -0700429
430 start_bit = find_next_bit_le(bitmap, nrbits, end_bit);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700431 }
432
433 if (extent_count != expected_extent_count) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -0400434 btrfs_err(fs_info,
435 "incorrect extent count for %llu; counted %u, expected %u",
Omar Sandovala5ed9182015-09-29 20:50:35 -0700436 block_group->key.objectid, extent_count,
437 expected_extent_count);
438 ASSERT(0);
439 ret = -EIO;
440 goto out;
441 }
442
443 ret = 0;
444out:
David Sterba79b134a2016-01-22 17:16:18 +0100445 kvfree(bitmap);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700446 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400447 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700448 return ret;
449}
450
451static int update_free_space_extent_count(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700452 struct btrfs_block_group_cache *block_group,
453 struct btrfs_path *path,
454 int new_extents)
455{
456 struct btrfs_free_space_info *info;
457 u32 flags;
458 u32 extent_count;
459 int ret = 0;
460
461 if (new_extents == 0)
462 return 0;
463
Nikolay Borisov690d7682018-05-10 15:44:49 +0300464 info = search_free_space_info(trans, trans->fs_info, block_group, path,
465 1);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700466 if (IS_ERR(info)) {
467 ret = PTR_ERR(info);
468 goto out;
469 }
470 flags = btrfs_free_space_flags(path->nodes[0], info);
471 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
472
473 extent_count += new_extents;
474 btrfs_set_free_space_extent_count(path->nodes[0], info, extent_count);
475 btrfs_mark_buffer_dirty(path->nodes[0]);
476 btrfs_release_path(path);
477
478 if (!(flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
479 extent_count > block_group->bitmap_high_thresh) {
Nikolay Borisov719fb4d2018-05-10 15:44:47 +0300480 ret = convert_free_space_to_bitmaps(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700481 } else if ((flags & BTRFS_FREE_SPACE_USING_BITMAPS) &&
482 extent_count < block_group->bitmap_low_thresh) {
Nikolay Borisov5296c2b2018-05-10 15:44:48 +0300483 ret = convert_free_space_to_extents(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700484 }
485
486out:
487 return ret;
488}
489
490int free_space_test_bit(struct btrfs_block_group_cache *block_group,
491 struct btrfs_path *path, u64 offset)
492{
493 struct extent_buffer *leaf;
494 struct btrfs_key key;
495 u64 found_start, found_end;
496 unsigned long ptr, i;
497
498 leaf = path->nodes[0];
499 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
500 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
501
502 found_start = key.objectid;
503 found_end = key.objectid + key.offset;
504 ASSERT(offset >= found_start && offset < found_end);
505
506 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Jeff Mahoneyda170662016-06-15 09:22:56 -0400507 i = div_u64(offset - found_start,
508 block_group->fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700509 return !!extent_buffer_test_bit(leaf, ptr, i);
510}
511
512static void free_space_set_bits(struct btrfs_block_group_cache *block_group,
513 struct btrfs_path *path, u64 *start, u64 *size,
514 int bit)
515{
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400516 struct btrfs_fs_info *fs_info = block_group->fs_info;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700517 struct extent_buffer *leaf;
518 struct btrfs_key key;
519 u64 end = *start + *size;
520 u64 found_start, found_end;
521 unsigned long ptr, first, last;
522
523 leaf = path->nodes[0];
524 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
525 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
526
527 found_start = key.objectid;
528 found_end = key.objectid + key.offset;
529 ASSERT(*start >= found_start && *start < found_end);
530 ASSERT(end > found_start);
531
532 if (end > found_end)
533 end = found_end;
534
535 ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
Jeff Mahoney0b246af2016-06-22 18:54:23 -0400536 first = div_u64(*start - found_start, fs_info->sectorsize);
537 last = div_u64(end - found_start, fs_info->sectorsize);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700538 if (bit)
539 extent_buffer_bitmap_set(leaf, ptr, first, last - first);
540 else
541 extent_buffer_bitmap_clear(leaf, ptr, first, last - first);
542 btrfs_mark_buffer_dirty(leaf);
543
544 *size -= end - *start;
545 *start = end;
546}
547
548/*
549 * We can't use btrfs_next_item() in modify_free_space_bitmap() because
550 * btrfs_next_leaf() doesn't get the path for writing. We can forgo the fancy
551 * tree walking in btrfs_next_leaf() anyways because we know exactly what we're
552 * looking for.
553 */
554static int free_space_next_bitmap(struct btrfs_trans_handle *trans,
555 struct btrfs_root *root, struct btrfs_path *p)
556{
557 struct btrfs_key key;
558
559 if (p->slots[0] + 1 < btrfs_header_nritems(p->nodes[0])) {
560 p->slots[0]++;
561 return 0;
562 }
563
564 btrfs_item_key_to_cpu(p->nodes[0], &key, p->slots[0]);
565 btrfs_release_path(p);
566
567 key.objectid += key.offset;
568 key.type = (u8)-1;
569 key.offset = (u64)-1;
570
571 return btrfs_search_prev_slot(trans, root, &key, p, 0, 1);
572}
573
574/*
575 * If remove is 1, then we are removing free space, thus clearing bits in the
576 * bitmap. If remove is 0, then we are adding free space, thus setting bits in
577 * the bitmap.
578 */
579static int modify_free_space_bitmap(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700580 struct btrfs_block_group_cache *block_group,
581 struct btrfs_path *path,
582 u64 start, u64 size, int remove)
583{
Nikolay Borisov85a7ef12018-05-10 15:44:50 +0300584 struct btrfs_root *root = block_group->fs_info->free_space_root;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700585 struct btrfs_key key;
586 u64 end = start + size;
587 u64 cur_start, cur_size;
588 int prev_bit, next_bit;
589 int new_extents;
590 int ret;
591
592 /*
593 * Read the bit for the block immediately before the extent of space if
594 * that block is within the block group.
595 */
596 if (start > block_group->key.objectid) {
Jeff Mahoneyda170662016-06-15 09:22:56 -0400597 u64 prev_block = start - block_group->fs_info->sectorsize;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700598
599 key.objectid = prev_block;
600 key.type = (u8)-1;
601 key.offset = (u64)-1;
602
603 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
604 if (ret)
605 goto out;
606
607 prev_bit = free_space_test_bit(block_group, path, prev_block);
608
609 /* The previous block may have been in the previous bitmap. */
610 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
611 if (start >= key.objectid + key.offset) {
612 ret = free_space_next_bitmap(trans, root, path);
613 if (ret)
614 goto out;
615 }
616 } else {
617 key.objectid = start;
618 key.type = (u8)-1;
619 key.offset = (u64)-1;
620
621 ret = btrfs_search_prev_slot(trans, root, &key, path, 0, 1);
622 if (ret)
623 goto out;
624
625 prev_bit = -1;
626 }
627
628 /*
629 * Iterate over all of the bitmaps overlapped by the extent of space,
630 * clearing/setting bits as required.
631 */
632 cur_start = start;
633 cur_size = size;
634 while (1) {
635 free_space_set_bits(block_group, path, &cur_start, &cur_size,
636 !remove);
637 if (cur_size == 0)
638 break;
639 ret = free_space_next_bitmap(trans, root, path);
640 if (ret)
641 goto out;
642 }
643
644 /*
645 * Read the bit for the block immediately after the extent of space if
646 * that block is within the block group.
647 */
648 if (end < block_group->key.objectid + block_group->key.offset) {
649 /* The next block may be in the next bitmap. */
650 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
651 if (end >= key.objectid + key.offset) {
652 ret = free_space_next_bitmap(trans, root, path);
653 if (ret)
654 goto out;
655 }
656
657 next_bit = free_space_test_bit(block_group, path, end);
658 } else {
659 next_bit = -1;
660 }
661
662 if (remove) {
663 new_extents = -1;
664 if (prev_bit == 1) {
665 /* Leftover on the left. */
666 new_extents++;
667 }
668 if (next_bit == 1) {
669 /* Leftover on the right. */
670 new_extents++;
671 }
672 } else {
673 new_extents = 1;
674 if (prev_bit == 1) {
675 /* Merging with neighbor on the left. */
676 new_extents--;
677 }
678 if (next_bit == 1) {
679 /* Merging with neighbor on the right. */
680 new_extents--;
681 }
682 }
683
684 btrfs_release_path(path);
Nikolay Borisov690d7682018-05-10 15:44:49 +0300685 ret = update_free_space_extent_count(trans, block_group, path,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700686 new_extents);
687
688out:
689 return ret;
690}
691
692static int remove_free_space_extent(struct btrfs_trans_handle *trans,
693 struct btrfs_fs_info *fs_info,
694 struct btrfs_block_group_cache *block_group,
695 struct btrfs_path *path,
696 u64 start, u64 size)
697{
698 struct btrfs_root *root = fs_info->free_space_root;
699 struct btrfs_key key;
700 u64 found_start, found_end;
701 u64 end = start + size;
702 int new_extents = -1;
703 int ret;
704
705 key.objectid = start;
706 key.type = (u8)-1;
707 key.offset = (u64)-1;
708
709 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
710 if (ret)
711 goto out;
712
713 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
714
715 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
716
717 found_start = key.objectid;
718 found_end = key.objectid + key.offset;
719 ASSERT(start >= found_start && end <= found_end);
720
721 /*
722 * Okay, now that we've found the free space extent which contains the
723 * free space that we are removing, there are four cases:
724 *
725 * 1. We're using the whole extent: delete the key we found and
726 * decrement the free space extent count.
727 * 2. We are using part of the extent starting at the beginning: delete
728 * the key we found and insert a new key representing the leftover at
729 * the end. There is no net change in the number of extents.
730 * 3. We are using part of the extent ending at the end: delete the key
731 * we found and insert a new key representing the leftover at the
732 * beginning. There is no net change in the number of extents.
733 * 4. We are using part of the extent in the middle: delete the key we
734 * found and insert two new keys representing the leftovers on each
735 * side. Where we used to have one extent, we now have two, so increment
736 * the extent count. We may need to convert the block group to bitmaps
737 * as a result.
738 */
739
740 /* Delete the existing key (cases 1-4). */
741 ret = btrfs_del_item(trans, root, path);
742 if (ret)
743 goto out;
744
745 /* Add a key for leftovers at the beginning (cases 3 and 4). */
746 if (start > found_start) {
747 key.objectid = found_start;
748 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
749 key.offset = start - found_start;
750
751 btrfs_release_path(path);
752 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
753 if (ret)
754 goto out;
755 new_extents++;
756 }
757
758 /* Add a key for leftovers at the end (cases 2 and 4). */
759 if (end < found_end) {
760 key.objectid = end;
761 key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
762 key.offset = found_end - end;
763
764 btrfs_release_path(path);
765 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
766 if (ret)
767 goto out;
768 new_extents++;
769 }
770
771 btrfs_release_path(path);
Nikolay Borisov690d7682018-05-10 15:44:49 +0300772 ret = update_free_space_extent_count(trans, block_group, path,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700773 new_extents);
774
775out:
776 return ret;
777}
778
779int __remove_from_free_space_tree(struct btrfs_trans_handle *trans,
780 struct btrfs_fs_info *fs_info,
781 struct btrfs_block_group_cache *block_group,
782 struct btrfs_path *path, u64 start, u64 size)
783{
784 struct btrfs_free_space_info *info;
785 u32 flags;
786 int ret;
787
788 if (block_group->needs_free_space) {
Nikolay Borisov9a7e0f92018-05-10 15:44:42 +0300789 ret = __add_block_group_free_space(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700790 if (ret)
791 return ret;
792 }
793
794 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
795 if (IS_ERR(info))
796 return PTR_ERR(info);
797 flags = btrfs_free_space_flags(path->nodes[0], info);
798 btrfs_release_path(path);
799
800 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
Nikolay Borisov85a7ef12018-05-10 15:44:50 +0300801 return modify_free_space_bitmap(trans, block_group, path,
802 start, size, 1);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700803 } else {
804 return remove_free_space_extent(trans, fs_info, block_group,
805 path, start, size);
806 }
807}
808
809int remove_from_free_space_tree(struct btrfs_trans_handle *trans,
810 struct btrfs_fs_info *fs_info,
811 u64 start, u64 size)
812{
813 struct btrfs_block_group_cache *block_group;
814 struct btrfs_path *path;
815 int ret;
816
817 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
818 return 0;
819
820 path = btrfs_alloc_path();
821 if (!path) {
822 ret = -ENOMEM;
823 goto out;
824 }
825
826 block_group = btrfs_lookup_block_group(fs_info, start);
827 if (!block_group) {
828 ASSERT(0);
829 ret = -ENOENT;
830 goto out;
831 }
832
833 mutex_lock(&block_group->free_space_lock);
834 ret = __remove_from_free_space_tree(trans, fs_info, block_group, path,
835 start, size);
836 mutex_unlock(&block_group->free_space_lock);
837
838 btrfs_put_block_group(block_group);
839out:
840 btrfs_free_path(path);
841 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -0400842 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700843 return ret;
844}
845
846static int add_free_space_extent(struct btrfs_trans_handle *trans,
847 struct btrfs_fs_info *fs_info,
848 struct btrfs_block_group_cache *block_group,
849 struct btrfs_path *path,
850 u64 start, u64 size)
851{
852 struct btrfs_root *root = fs_info->free_space_root;
853 struct btrfs_key key, new_key;
854 u64 found_start, found_end;
855 u64 end = start + size;
856 int new_extents = 1;
857 int ret;
858
859 /*
860 * We are adding a new extent of free space, but we need to merge
861 * extents. There are four cases here:
862 *
863 * 1. The new extent does not have any immediate neighbors to merge
864 * with: add the new key and increment the free space extent count. We
865 * may need to convert the block group to bitmaps as a result.
866 * 2. The new extent has an immediate neighbor before it: remove the
867 * previous key and insert a new key combining both of them. There is no
868 * net change in the number of extents.
869 * 3. The new extent has an immediate neighbor after it: remove the next
870 * key and insert a new key combining both of them. There is no net
871 * change in the number of extents.
872 * 4. The new extent has immediate neighbors on both sides: remove both
873 * of the keys and insert a new key combining all of them. Where we used
874 * to have two extents, we now have one, so decrement the extent count.
875 */
876
877 new_key.objectid = start;
878 new_key.type = BTRFS_FREE_SPACE_EXTENT_KEY;
879 new_key.offset = size;
880
881 /* Search for a neighbor on the left. */
882 if (start == block_group->key.objectid)
883 goto right;
884 key.objectid = start - 1;
885 key.type = (u8)-1;
886 key.offset = (u64)-1;
887
888 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
889 if (ret)
890 goto out;
891
892 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
893
894 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
895 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
896 btrfs_release_path(path);
897 goto right;
898 }
899
900 found_start = key.objectid;
901 found_end = key.objectid + key.offset;
902 ASSERT(found_start >= block_group->key.objectid &&
903 found_end > block_group->key.objectid);
904 ASSERT(found_start < start && found_end <= start);
905
906 /*
907 * Delete the neighbor on the left and absorb it into the new key (cases
908 * 2 and 4).
909 */
910 if (found_end == start) {
911 ret = btrfs_del_item(trans, root, path);
912 if (ret)
913 goto out;
914 new_key.objectid = found_start;
915 new_key.offset += key.offset;
916 new_extents--;
917 }
918 btrfs_release_path(path);
919
920right:
921 /* Search for a neighbor on the right. */
922 if (end == block_group->key.objectid + block_group->key.offset)
923 goto insert;
924 key.objectid = end;
925 key.type = (u8)-1;
926 key.offset = (u64)-1;
927
928 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
929 if (ret)
930 goto out;
931
932 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
933
934 if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY) {
935 ASSERT(key.type == BTRFS_FREE_SPACE_INFO_KEY);
936 btrfs_release_path(path);
937 goto insert;
938 }
939
940 found_start = key.objectid;
941 found_end = key.objectid + key.offset;
942 ASSERT(found_start >= block_group->key.objectid &&
943 found_end > block_group->key.objectid);
944 ASSERT((found_start < start && found_end <= start) ||
945 (found_start >= end && found_end > end));
946
947 /*
948 * Delete the neighbor on the right and absorb it into the new key
949 * (cases 3 and 4).
950 */
951 if (found_start == end) {
952 ret = btrfs_del_item(trans, root, path);
953 if (ret)
954 goto out;
955 new_key.offset += key.offset;
956 new_extents--;
957 }
958 btrfs_release_path(path);
959
960insert:
961 /* Insert the new key (cases 1-4). */
962 ret = btrfs_insert_empty_item(trans, root, path, &new_key, 0);
963 if (ret)
964 goto out;
965
966 btrfs_release_path(path);
Nikolay Borisov690d7682018-05-10 15:44:49 +0300967 ret = update_free_space_extent_count(trans, block_group, path,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700968 new_extents);
969
970out:
971 return ret;
972}
973
974int __add_to_free_space_tree(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -0700975 struct btrfs_block_group_cache *block_group,
976 struct btrfs_path *path, u64 start, u64 size)
977{
Nikolay Borisov2d5cffa2018-05-10 15:44:43 +0300978 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovala5ed9182015-09-29 20:50:35 -0700979 struct btrfs_free_space_info *info;
980 u32 flags;
981 int ret;
982
983 if (block_group->needs_free_space) {
Nikolay Borisov9a7e0f92018-05-10 15:44:42 +0300984 ret = __add_block_group_free_space(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700985 if (ret)
986 return ret;
987 }
988
989 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
990 if (IS_ERR(info))
991 return PTR_ERR(info);
992 flags = btrfs_free_space_flags(path->nodes[0], info);
993 btrfs_release_path(path);
994
995 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
Nikolay Borisov85a7ef12018-05-10 15:44:50 +0300996 return modify_free_space_bitmap(trans, block_group, path,
997 start, size, 0);
Omar Sandovala5ed9182015-09-29 20:50:35 -0700998 } else {
999 return add_free_space_extent(trans, fs_info, block_group, path,
1000 start, size);
1001 }
1002}
1003
1004int add_to_free_space_tree(struct btrfs_trans_handle *trans,
1005 struct btrfs_fs_info *fs_info,
1006 u64 start, u64 size)
1007{
1008 struct btrfs_block_group_cache *block_group;
1009 struct btrfs_path *path;
1010 int ret;
1011
1012 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1013 return 0;
1014
1015 path = btrfs_alloc_path();
1016 if (!path) {
1017 ret = -ENOMEM;
1018 goto out;
1019 }
1020
1021 block_group = btrfs_lookup_block_group(fs_info, start);
1022 if (!block_group) {
1023 ASSERT(0);
1024 ret = -ENOENT;
1025 goto out;
1026 }
1027
1028 mutex_lock(&block_group->free_space_lock);
Nikolay Borisov2d5cffa2018-05-10 15:44:43 +03001029 ret = __add_to_free_space_tree(trans, block_group, path, start, size);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001030 mutex_unlock(&block_group->free_space_lock);
1031
1032 btrfs_put_block_group(block_group);
1033out:
1034 btrfs_free_path(path);
1035 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001036 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001037 return ret;
1038}
1039
1040/*
1041 * Populate the free space tree by walking the extent tree. Operations on the
1042 * extent tree that happen as a result of writes to the free space tree will go
1043 * through the normal add/remove hooks.
1044 */
1045static int populate_free_space_tree(struct btrfs_trans_handle *trans,
1046 struct btrfs_fs_info *fs_info,
1047 struct btrfs_block_group_cache *block_group)
1048{
1049 struct btrfs_root *extent_root = fs_info->extent_root;
1050 struct btrfs_path *path, *path2;
1051 struct btrfs_key key;
1052 u64 start, end;
1053 int ret;
1054
1055 path = btrfs_alloc_path();
1056 if (!path)
1057 return -ENOMEM;
Gu Jinxiang019599a2018-01-11 16:12:17 +08001058 path->reada = READA_FORWARD;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001059
1060 path2 = btrfs_alloc_path();
1061 if (!path2) {
1062 btrfs_free_path(path);
1063 return -ENOMEM;
1064 }
1065
Nikolay Borisov66afee12018-05-10 15:44:44 +03001066 ret = add_new_free_space_info(trans, block_group, path2);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001067 if (ret)
1068 goto out;
1069
Chris Mason511711a2015-12-30 07:52:35 -08001070 mutex_lock(&block_group->free_space_lock);
1071
Omar Sandovala5ed9182015-09-29 20:50:35 -07001072 /*
1073 * Iterate through all of the extent and metadata items in this block
1074 * group, adding the free space between them and the free space at the
1075 * end. Note that EXTENT_ITEM and METADATA_ITEM are less than
1076 * BLOCK_GROUP_ITEM, so an extent may precede the block group that it's
1077 * contained in.
1078 */
1079 key.objectid = block_group->key.objectid;
1080 key.type = BTRFS_EXTENT_ITEM_KEY;
1081 key.offset = 0;
1082
1083 ret = btrfs_search_slot_for_read(extent_root, &key, path, 1, 0);
1084 if (ret < 0)
Chris Mason511711a2015-12-30 07:52:35 -08001085 goto out_locked;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001086 ASSERT(ret == 0);
1087
1088 start = block_group->key.objectid;
1089 end = block_group->key.objectid + block_group->key.offset;
1090 while (1) {
1091 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1092
1093 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
1094 key.type == BTRFS_METADATA_ITEM_KEY) {
1095 if (key.objectid >= end)
1096 break;
1097
1098 if (start < key.objectid) {
Nikolay Borisov2d5cffa2018-05-10 15:44:43 +03001099 ret = __add_to_free_space_tree(trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001100 block_group,
1101 path2, start,
1102 key.objectid -
1103 start);
1104 if (ret)
Chris Mason511711a2015-12-30 07:52:35 -08001105 goto out_locked;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001106 }
1107 start = key.objectid;
1108 if (key.type == BTRFS_METADATA_ITEM_KEY)
Jeff Mahoneyda170662016-06-15 09:22:56 -04001109 start += fs_info->nodesize;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001110 else
1111 start += key.offset;
1112 } else if (key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
1113 if (key.objectid != block_group->key.objectid)
1114 break;
1115 }
1116
1117 ret = btrfs_next_item(extent_root, path);
1118 if (ret < 0)
Chris Mason511711a2015-12-30 07:52:35 -08001119 goto out_locked;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001120 if (ret)
1121 break;
1122 }
1123 if (start < end) {
Nikolay Borisov2d5cffa2018-05-10 15:44:43 +03001124 ret = __add_to_free_space_tree(trans, block_group, path2,
1125 start, end - start);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001126 if (ret)
Chris Mason511711a2015-12-30 07:52:35 -08001127 goto out_locked;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001128 }
1129
1130 ret = 0;
Chris Mason511711a2015-12-30 07:52:35 -08001131out_locked:
1132 mutex_unlock(&block_group->free_space_lock);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001133out:
1134 btrfs_free_path(path2);
1135 btrfs_free_path(path);
1136 return ret;
1137}
1138
1139int btrfs_create_free_space_tree(struct btrfs_fs_info *fs_info)
1140{
1141 struct btrfs_trans_handle *trans;
1142 struct btrfs_root *tree_root = fs_info->tree_root;
1143 struct btrfs_root *free_space_root;
1144 struct btrfs_block_group_cache *block_group;
1145 struct rb_node *node;
1146 int ret;
1147
1148 trans = btrfs_start_transaction(tree_root, 0);
1149 if (IS_ERR(trans))
1150 return PTR_ERR(trans);
1151
Josef Bacikafcdd122016-09-02 15:40:02 -04001152 set_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001153 free_space_root = btrfs_create_tree(trans, fs_info,
1154 BTRFS_FREE_SPACE_TREE_OBJECTID);
1155 if (IS_ERR(free_space_root)) {
1156 ret = PTR_ERR(free_space_root);
1157 goto abort;
1158 }
1159 fs_info->free_space_root = free_space_root;
1160
1161 node = rb_first(&fs_info->block_group_cache_tree);
1162 while (node) {
1163 block_group = rb_entry(node, struct btrfs_block_group_cache,
1164 cache_node);
1165 ret = populate_free_space_tree(trans, fs_info, block_group);
1166 if (ret)
1167 goto abort;
1168 node = rb_next(node);
1169 }
1170
1171 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE);
Omar Sandoval6675df32016-09-22 17:24:22 -07001172 btrfs_set_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
Josef Bacikafcdd122016-09-02 15:40:02 -04001173 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001174
Sahil Kang0bef7102017-05-17 03:33:45 -07001175 return btrfs_commit_transaction(trans);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001176
1177abort:
Josef Bacikafcdd122016-09-02 15:40:02 -04001178 clear_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags);
Jeff Mahoney66642832016-06-10 18:19:25 -04001179 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001180 btrfs_end_transaction(trans);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001181 return ret;
1182}
1183
1184static int clear_free_space_tree(struct btrfs_trans_handle *trans,
1185 struct btrfs_root *root)
1186{
1187 struct btrfs_path *path;
1188 struct btrfs_key key;
1189 int nr;
1190 int ret;
1191
1192 path = btrfs_alloc_path();
1193 if (!path)
1194 return -ENOMEM;
1195
1196 path->leave_spinning = 1;
1197
1198 key.objectid = 0;
1199 key.type = 0;
1200 key.offset = 0;
1201
1202 while (1) {
1203 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1204 if (ret < 0)
1205 goto out;
1206
1207 nr = btrfs_header_nritems(path->nodes[0]);
1208 if (!nr)
1209 break;
1210
1211 path->slots[0] = 0;
1212 ret = btrfs_del_items(trans, root, path, 0, nr);
1213 if (ret)
1214 goto out;
1215
1216 btrfs_release_path(path);
1217 }
1218
1219 ret = 0;
1220out:
1221 btrfs_free_path(path);
1222 return ret;
1223}
1224
1225int btrfs_clear_free_space_tree(struct btrfs_fs_info *fs_info)
1226{
1227 struct btrfs_trans_handle *trans;
1228 struct btrfs_root *tree_root = fs_info->tree_root;
1229 struct btrfs_root *free_space_root = fs_info->free_space_root;
1230 int ret;
1231
1232 trans = btrfs_start_transaction(tree_root, 0);
1233 if (IS_ERR(trans))
1234 return PTR_ERR(trans);
1235
1236 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE);
Omar Sandoval6675df32016-09-22 17:24:22 -07001237 btrfs_clear_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001238 fs_info->free_space_root = NULL;
1239
1240 ret = clear_free_space_tree(trans, free_space_root);
1241 if (ret)
1242 goto abort;
1243
Jeff Mahoney1cd54472017-08-17 10:25:11 -04001244 ret = btrfs_del_root(trans, fs_info, &free_space_root->root_key);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001245 if (ret)
1246 goto abort;
1247
1248 list_del(&free_space_root->dirty_list);
1249
1250 btrfs_tree_lock(free_space_root->node);
David Sterba7c302b42017-02-10 18:47:57 +01001251 clean_tree_block(fs_info, free_space_root->node);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001252 btrfs_tree_unlock(free_space_root->node);
1253 btrfs_free_tree_block(trans, free_space_root, free_space_root->node,
1254 0, 1);
1255
1256 free_extent_buffer(free_space_root->node);
1257 free_extent_buffer(free_space_root->commit_root);
1258 kfree(free_space_root);
1259
Sahil Kang0bef7102017-05-17 03:33:45 -07001260 return btrfs_commit_transaction(trans);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001261
1262abort:
Jeff Mahoney66642832016-06-10 18:19:25 -04001263 btrfs_abort_transaction(trans, ret);
Jeff Mahoney3a45bb22016-09-09 21:39:03 -04001264 btrfs_end_transaction(trans);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001265 return ret;
1266}
1267
1268static int __add_block_group_free_space(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001269 struct btrfs_block_group_cache *block_group,
1270 struct btrfs_path *path)
1271{
Omar Sandovala5ed9182015-09-29 20:50:35 -07001272 int ret;
1273
Omar Sandovala5ed9182015-09-29 20:50:35 -07001274 block_group->needs_free_space = 0;
1275
Nikolay Borisov66afee12018-05-10 15:44:44 +03001276 ret = add_new_free_space_info(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001277 if (ret)
1278 return ret;
1279
Nikolay Borisov2d5cffa2018-05-10 15:44:43 +03001280 return __add_to_free_space_tree(trans, block_group, path,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001281 block_group->key.objectid,
1282 block_group->key.offset);
1283}
1284
1285int add_block_group_free_space(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001286 struct btrfs_block_group_cache *block_group)
1287{
Nikolay Borisove4e07112018-05-10 15:44:41 +03001288 struct btrfs_fs_info *fs_info = trans->fs_info;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001289 struct btrfs_path *path = NULL;
1290 int ret = 0;
1291
1292 if (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE))
1293 return 0;
1294
1295 mutex_lock(&block_group->free_space_lock);
1296 if (!block_group->needs_free_space)
1297 goto out;
1298
1299 path = btrfs_alloc_path();
1300 if (!path) {
1301 ret = -ENOMEM;
1302 goto out;
1303 }
1304
Nikolay Borisov9a7e0f92018-05-10 15:44:42 +03001305 ret = __add_block_group_free_space(trans, block_group, path);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001306
1307out:
1308 btrfs_free_path(path);
1309 mutex_unlock(&block_group->free_space_lock);
1310 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001311 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001312 return ret;
1313}
1314
1315int remove_block_group_free_space(struct btrfs_trans_handle *trans,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001316 struct btrfs_block_group_cache *block_group)
1317{
Nikolay Borisovf3f72772018-05-10 15:44:46 +03001318 struct btrfs_root *root = trans->fs_info->free_space_root;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001319 struct btrfs_path *path;
1320 struct btrfs_key key, found_key;
1321 struct extent_buffer *leaf;
1322 u64 start, end;
1323 int done = 0, nr;
1324 int ret;
1325
Nikolay Borisovf3f72772018-05-10 15:44:46 +03001326 if (!btrfs_fs_compat_ro(trans->fs_info, FREE_SPACE_TREE))
Omar Sandovala5ed9182015-09-29 20:50:35 -07001327 return 0;
1328
1329 if (block_group->needs_free_space) {
1330 /* We never added this block group to the free space tree. */
1331 return 0;
1332 }
1333
1334 path = btrfs_alloc_path();
1335 if (!path) {
1336 ret = -ENOMEM;
1337 goto out;
1338 }
1339
1340 start = block_group->key.objectid;
1341 end = block_group->key.objectid + block_group->key.offset;
1342
1343 key.objectid = end - 1;
1344 key.type = (u8)-1;
1345 key.offset = (u64)-1;
1346
1347 while (!done) {
1348 ret = btrfs_search_prev_slot(trans, root, &key, path, -1, 1);
1349 if (ret)
1350 goto out;
1351
1352 leaf = path->nodes[0];
1353 nr = 0;
1354 path->slots[0]++;
1355 while (path->slots[0] > 0) {
1356 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0] - 1);
1357
1358 if (found_key.type == BTRFS_FREE_SPACE_INFO_KEY) {
1359 ASSERT(found_key.objectid == block_group->key.objectid);
1360 ASSERT(found_key.offset == block_group->key.offset);
1361 done = 1;
1362 nr++;
1363 path->slots[0]--;
1364 break;
1365 } else if (found_key.type == BTRFS_FREE_SPACE_EXTENT_KEY ||
1366 found_key.type == BTRFS_FREE_SPACE_BITMAP_KEY) {
1367 ASSERT(found_key.objectid >= start);
1368 ASSERT(found_key.objectid < end);
1369 ASSERT(found_key.objectid + found_key.offset <= end);
1370 nr++;
1371 path->slots[0]--;
1372 } else {
1373 ASSERT(0);
1374 }
1375 }
1376
1377 ret = btrfs_del_items(trans, root, path, path->slots[0], nr);
1378 if (ret)
1379 goto out;
1380 btrfs_release_path(path);
1381 }
1382
1383 ret = 0;
1384out:
1385 btrfs_free_path(path);
1386 if (ret)
Jeff Mahoney66642832016-06-10 18:19:25 -04001387 btrfs_abort_transaction(trans, ret);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001388 return ret;
1389}
1390
1391static int load_free_space_bitmaps(struct btrfs_caching_control *caching_ctl,
1392 struct btrfs_path *path,
1393 u32 expected_extent_count)
1394{
1395 struct btrfs_block_group_cache *block_group;
1396 struct btrfs_fs_info *fs_info;
1397 struct btrfs_root *root;
1398 struct btrfs_key key;
1399 int prev_bit = 0, bit;
1400 /* Initialize to silence GCC. */
1401 u64 extent_start = 0;
1402 u64 end, offset;
1403 u64 total_found = 0;
1404 u32 extent_count = 0;
1405 int ret;
1406
1407 block_group = caching_ctl->block_group;
1408 fs_info = block_group->fs_info;
1409 root = fs_info->free_space_root;
1410
1411 end = block_group->key.objectid + block_group->key.offset;
1412
1413 while (1) {
1414 ret = btrfs_next_item(root, path);
1415 if (ret < 0)
1416 goto out;
1417 if (ret)
1418 break;
1419
1420 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1421
1422 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1423 break;
1424
1425 ASSERT(key.type == BTRFS_FREE_SPACE_BITMAP_KEY);
1426 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1427
1428 caching_ctl->progress = key.objectid;
1429
1430 offset = key.objectid;
1431 while (offset < key.objectid + key.offset) {
1432 bit = free_space_test_bit(block_group, path, offset);
1433 if (prev_bit == 0 && bit == 1) {
1434 extent_start = offset;
1435 } else if (prev_bit == 1 && bit == 0) {
1436 total_found += add_new_free_space(block_group,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001437 extent_start,
1438 offset);
1439 if (total_found > CACHING_CTL_WAKE_UP) {
1440 total_found = 0;
1441 wake_up(&caching_ctl->wait);
1442 }
1443 extent_count++;
1444 }
1445 prev_bit = bit;
Jeff Mahoney0b246af2016-06-22 18:54:23 -04001446 offset += fs_info->sectorsize;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001447 }
1448 }
1449 if (prev_bit == 1) {
Nikolay Borisov4457c1c2018-05-10 15:44:45 +03001450 total_found += add_new_free_space(block_group, extent_start,
1451 end);
Omar Sandovala5ed9182015-09-29 20:50:35 -07001452 extent_count++;
1453 }
1454
1455 if (extent_count != expected_extent_count) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001456 btrfs_err(fs_info,
1457 "incorrect extent count for %llu; counted %u, expected %u",
Omar Sandovala5ed9182015-09-29 20:50:35 -07001458 block_group->key.objectid, extent_count,
1459 expected_extent_count);
1460 ASSERT(0);
1461 ret = -EIO;
1462 goto out;
1463 }
1464
1465 caching_ctl->progress = (u64)-1;
1466
1467 ret = 0;
1468out:
1469 return ret;
1470}
1471
1472static int load_free_space_extents(struct btrfs_caching_control *caching_ctl,
1473 struct btrfs_path *path,
1474 u32 expected_extent_count)
1475{
1476 struct btrfs_block_group_cache *block_group;
1477 struct btrfs_fs_info *fs_info;
1478 struct btrfs_root *root;
1479 struct btrfs_key key;
1480 u64 end;
1481 u64 total_found = 0;
1482 u32 extent_count = 0;
1483 int ret;
1484
1485 block_group = caching_ctl->block_group;
1486 fs_info = block_group->fs_info;
1487 root = fs_info->free_space_root;
1488
1489 end = block_group->key.objectid + block_group->key.offset;
1490
1491 while (1) {
1492 ret = btrfs_next_item(root, path);
1493 if (ret < 0)
1494 goto out;
1495 if (ret)
1496 break;
1497
1498 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
1499
1500 if (key.type == BTRFS_FREE_SPACE_INFO_KEY)
1501 break;
1502
1503 ASSERT(key.type == BTRFS_FREE_SPACE_EXTENT_KEY);
1504 ASSERT(key.objectid < end && key.objectid + key.offset <= end);
1505
1506 caching_ctl->progress = key.objectid;
1507
Nikolay Borisov4457c1c2018-05-10 15:44:45 +03001508 total_found += add_new_free_space(block_group, key.objectid,
Omar Sandovala5ed9182015-09-29 20:50:35 -07001509 key.objectid + key.offset);
1510 if (total_found > CACHING_CTL_WAKE_UP) {
1511 total_found = 0;
1512 wake_up(&caching_ctl->wait);
1513 }
1514 extent_count++;
1515 }
1516
1517 if (extent_count != expected_extent_count) {
Jeff Mahoney5d163e02016-09-20 10:05:00 -04001518 btrfs_err(fs_info,
1519 "incorrect extent count for %llu; counted %u, expected %u",
Omar Sandovala5ed9182015-09-29 20:50:35 -07001520 block_group->key.objectid, extent_count,
1521 expected_extent_count);
1522 ASSERT(0);
1523 ret = -EIO;
1524 goto out;
1525 }
1526
1527 caching_ctl->progress = (u64)-1;
1528
1529 ret = 0;
1530out:
1531 return ret;
1532}
1533
1534int load_free_space_tree(struct btrfs_caching_control *caching_ctl)
1535{
1536 struct btrfs_block_group_cache *block_group;
1537 struct btrfs_fs_info *fs_info;
1538 struct btrfs_free_space_info *info;
1539 struct btrfs_path *path;
1540 u32 extent_count, flags;
1541 int ret;
1542
1543 block_group = caching_ctl->block_group;
1544 fs_info = block_group->fs_info;
1545
1546 path = btrfs_alloc_path();
1547 if (!path)
1548 return -ENOMEM;
1549
1550 /*
1551 * Just like caching_thread() doesn't want to deadlock on the extent
1552 * tree, we don't want to deadlock on the free space tree.
1553 */
1554 path->skip_locking = 1;
1555 path->search_commit_root = 1;
Gu JinXiang7ce311d2018-01-11 16:12:18 +08001556 path->reada = READA_FORWARD;
Omar Sandovala5ed9182015-09-29 20:50:35 -07001557
1558 info = search_free_space_info(NULL, fs_info, block_group, path, 0);
1559 if (IS_ERR(info)) {
1560 ret = PTR_ERR(info);
1561 goto out;
1562 }
1563 extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
1564 flags = btrfs_free_space_flags(path->nodes[0], info);
1565
1566 /*
1567 * We left path pointing to the free space info item, so now
1568 * load_free_space_foo can just iterate through the free space tree from
1569 * there.
1570 */
1571 if (flags & BTRFS_FREE_SPACE_USING_BITMAPS)
1572 ret = load_free_space_bitmaps(caching_ctl, path, extent_count);
1573 else
1574 ret = load_free_space_extents(caching_ctl, path, extent_count);
1575
1576out:
1577 btrfs_free_path(path);
1578 return ret;
1579}