]>
Commit | Line | Data |
---|---|---|
2e405ad8 JB |
1 | // SPDX-License-Identifier: GPL-2.0 |
2 | ||
784352fe | 3 | #include "misc.h" |
2e405ad8 JB |
4 | #include "ctree.h" |
5 | #include "block-group.h" | |
3eeb3226 | 6 | #include "space-info.h" |
9f21246d JB |
7 | #include "disk-io.h" |
8 | #include "free-space-cache.h" | |
9 | #include "free-space-tree.h" | |
e3e0520b JB |
10 | #include "disk-io.h" |
11 | #include "volumes.h" | |
12 | #include "transaction.h" | |
13 | #include "ref-verify.h" | |
4358d963 JB |
14 | #include "sysfs.h" |
15 | #include "tree-log.h" | |
77745c05 | 16 | #include "delalloc-space.h" |
b0643e59 | 17 | #include "discard.h" |
2e405ad8 | 18 | |
878d7b67 JB |
19 | /* |
20 | * Return target flags in extended format or 0 if restripe for this chunk_type | |
21 | * is not in progress | |
22 | * | |
23 | * Should be called with balance_lock held | |
24 | */ | |
e11c0406 | 25 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) |
878d7b67 JB |
26 | { |
27 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
28 | u64 target = 0; | |
29 | ||
30 | if (!bctl) | |
31 | return 0; | |
32 | ||
33 | if (flags & BTRFS_BLOCK_GROUP_DATA && | |
34 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
35 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; | |
36 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && | |
37 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
38 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; | |
39 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && | |
40 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
41 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; | |
42 | } | |
43 | ||
44 | return target; | |
45 | } | |
46 | ||
47 | /* | |
48 | * @flags: available profiles in extended format (see ctree.h) | |
49 | * | |
50 | * Return reduced profile in chunk format. If profile changing is in progress | |
51 | * (either running or paused) picks the target profile (if it's already | |
52 | * available), otherwise falls back to plain reducing. | |
53 | */ | |
54 | static u64 btrfs_reduce_alloc_profile(struct btrfs_fs_info *fs_info, u64 flags) | |
55 | { | |
56 | u64 num_devices = fs_info->fs_devices->rw_devices; | |
57 | u64 target; | |
58 | u64 raid_type; | |
59 | u64 allowed = 0; | |
60 | ||
61 | /* | |
62 | * See if restripe for this chunk_type is in progress, if so try to | |
63 | * reduce to the target profile | |
64 | */ | |
65 | spin_lock(&fs_info->balance_lock); | |
e11c0406 | 66 | target = get_restripe_target(fs_info, flags); |
878d7b67 JB |
67 | if (target) { |
68 | /* Pick target profile only if it's already available */ | |
69 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { | |
70 | spin_unlock(&fs_info->balance_lock); | |
71 | return extended_to_chunk(target); | |
72 | } | |
73 | } | |
74 | spin_unlock(&fs_info->balance_lock); | |
75 | ||
76 | /* First, mask out the RAID levels which aren't possible */ | |
77 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { | |
78 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) | |
79 | allowed |= btrfs_raid_array[raid_type].bg_flag; | |
80 | } | |
81 | allowed &= flags; | |
82 | ||
83 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) | |
84 | allowed = BTRFS_BLOCK_GROUP_RAID6; | |
85 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) | |
86 | allowed = BTRFS_BLOCK_GROUP_RAID5; | |
87 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) | |
88 | allowed = BTRFS_BLOCK_GROUP_RAID10; | |
89 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) | |
90 | allowed = BTRFS_BLOCK_GROUP_RAID1; | |
91 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) | |
92 | allowed = BTRFS_BLOCK_GROUP_RAID0; | |
93 | ||
94 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; | |
95 | ||
96 | return extended_to_chunk(flags | allowed); | |
97 | } | |
98 | ||
99 | static u64 get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) | |
100 | { | |
101 | unsigned seq; | |
102 | u64 flags; | |
103 | ||
104 | do { | |
105 | flags = orig_flags; | |
106 | seq = read_seqbegin(&fs_info->profiles_lock); | |
107 | ||
108 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
109 | flags |= fs_info->avail_data_alloc_bits; | |
110 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
111 | flags |= fs_info->avail_system_alloc_bits; | |
112 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
113 | flags |= fs_info->avail_metadata_alloc_bits; | |
114 | } while (read_seqretry(&fs_info->profiles_lock, seq)); | |
115 | ||
116 | return btrfs_reduce_alloc_profile(fs_info, flags); | |
117 | } | |
118 | ||
119 | u64 btrfs_get_alloc_profile(struct btrfs_fs_info *fs_info, u64 orig_flags) | |
120 | { | |
121 | return get_alloc_profile(fs_info, orig_flags); | |
122 | } | |
123 | ||
32da5386 | 124 | void btrfs_get_block_group(struct btrfs_block_group *cache) |
3cad1284 JB |
125 | { |
126 | atomic_inc(&cache->count); | |
127 | } | |
128 | ||
32da5386 | 129 | void btrfs_put_block_group(struct btrfs_block_group *cache) |
3cad1284 JB |
130 | { |
131 | if (atomic_dec_and_test(&cache->count)) { | |
132 | WARN_ON(cache->pinned > 0); | |
133 | WARN_ON(cache->reserved > 0); | |
134 | ||
b0643e59 DZ |
135 | /* |
136 | * A block_group shouldn't be on the discard_list anymore. | |
137 | * Remove the block_group from the discard_list to prevent us | |
138 | * from causing a panic due to NULL pointer dereference. | |
139 | */ | |
140 | if (WARN_ON(!list_empty(&cache->discard_list))) | |
141 | btrfs_discard_cancel_work(&cache->fs_info->discard_ctl, | |
142 | cache); | |
143 | ||
3cad1284 JB |
144 | /* |
145 | * If not empty, someone is still holding mutex of | |
146 | * full_stripe_lock, which can only be released by caller. | |
147 | * And it will definitely cause use-after-free when caller | |
148 | * tries to release full stripe lock. | |
149 | * | |
150 | * No better way to resolve, but only to warn. | |
151 | */ | |
152 | WARN_ON(!RB_EMPTY_ROOT(&cache->full_stripe_locks_root.root)); | |
153 | kfree(cache->free_space_ctl); | |
154 | kfree(cache); | |
155 | } | |
156 | } | |
157 | ||
4358d963 JB |
158 | /* |
159 | * This adds the block group to the fs_info rb tree for the block group cache | |
160 | */ | |
161 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, | |
32da5386 | 162 | struct btrfs_block_group *block_group) |
4358d963 JB |
163 | { |
164 | struct rb_node **p; | |
165 | struct rb_node *parent = NULL; | |
32da5386 | 166 | struct btrfs_block_group *cache; |
4358d963 JB |
167 | |
168 | spin_lock(&info->block_group_cache_lock); | |
169 | p = &info->block_group_cache_tree.rb_node; | |
170 | ||
171 | while (*p) { | |
172 | parent = *p; | |
32da5386 | 173 | cache = rb_entry(parent, struct btrfs_block_group, cache_node); |
b3470b5d | 174 | if (block_group->start < cache->start) { |
4358d963 | 175 | p = &(*p)->rb_left; |
b3470b5d | 176 | } else if (block_group->start > cache->start) { |
4358d963 JB |
177 | p = &(*p)->rb_right; |
178 | } else { | |
179 | spin_unlock(&info->block_group_cache_lock); | |
180 | return -EEXIST; | |
181 | } | |
182 | } | |
183 | ||
184 | rb_link_node(&block_group->cache_node, parent, p); | |
185 | rb_insert_color(&block_group->cache_node, | |
186 | &info->block_group_cache_tree); | |
187 | ||
b3470b5d DS |
188 | if (info->first_logical_byte > block_group->start) |
189 | info->first_logical_byte = block_group->start; | |
4358d963 JB |
190 | |
191 | spin_unlock(&info->block_group_cache_lock); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
2e405ad8 JB |
196 | /* |
197 | * This will return the block group at or after bytenr if contains is 0, else | |
198 | * it will return the block group that contains the bytenr | |
199 | */ | |
32da5386 | 200 | static struct btrfs_block_group *block_group_cache_tree_search( |
2e405ad8 JB |
201 | struct btrfs_fs_info *info, u64 bytenr, int contains) |
202 | { | |
32da5386 | 203 | struct btrfs_block_group *cache, *ret = NULL; |
2e405ad8 JB |
204 | struct rb_node *n; |
205 | u64 end, start; | |
206 | ||
207 | spin_lock(&info->block_group_cache_lock); | |
208 | n = info->block_group_cache_tree.rb_node; | |
209 | ||
210 | while (n) { | |
32da5386 | 211 | cache = rb_entry(n, struct btrfs_block_group, cache_node); |
b3470b5d DS |
212 | end = cache->start + cache->length - 1; |
213 | start = cache->start; | |
2e405ad8 JB |
214 | |
215 | if (bytenr < start) { | |
b3470b5d | 216 | if (!contains && (!ret || start < ret->start)) |
2e405ad8 JB |
217 | ret = cache; |
218 | n = n->rb_left; | |
219 | } else if (bytenr > start) { | |
220 | if (contains && bytenr <= end) { | |
221 | ret = cache; | |
222 | break; | |
223 | } | |
224 | n = n->rb_right; | |
225 | } else { | |
226 | ret = cache; | |
227 | break; | |
228 | } | |
229 | } | |
230 | if (ret) { | |
231 | btrfs_get_block_group(ret); | |
b3470b5d DS |
232 | if (bytenr == 0 && info->first_logical_byte > ret->start) |
233 | info->first_logical_byte = ret->start; | |
2e405ad8 JB |
234 | } |
235 | spin_unlock(&info->block_group_cache_lock); | |
236 | ||
237 | return ret; | |
238 | } | |
239 | ||
240 | /* | |
241 | * Return the block group that starts at or after bytenr | |
242 | */ | |
32da5386 | 243 | struct btrfs_block_group *btrfs_lookup_first_block_group( |
2e405ad8 JB |
244 | struct btrfs_fs_info *info, u64 bytenr) |
245 | { | |
246 | return block_group_cache_tree_search(info, bytenr, 0); | |
247 | } | |
248 | ||
249 | /* | |
250 | * Return the block group that contains the given bytenr | |
251 | */ | |
32da5386 | 252 | struct btrfs_block_group *btrfs_lookup_block_group( |
2e405ad8 JB |
253 | struct btrfs_fs_info *info, u64 bytenr) |
254 | { | |
255 | return block_group_cache_tree_search(info, bytenr, 1); | |
256 | } | |
257 | ||
32da5386 DS |
258 | struct btrfs_block_group *btrfs_next_block_group( |
259 | struct btrfs_block_group *cache) | |
2e405ad8 JB |
260 | { |
261 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
262 | struct rb_node *node; | |
263 | ||
264 | spin_lock(&fs_info->block_group_cache_lock); | |
265 | ||
266 | /* If our block group was removed, we need a full search. */ | |
267 | if (RB_EMPTY_NODE(&cache->cache_node)) { | |
b3470b5d | 268 | const u64 next_bytenr = cache->start + cache->length; |
2e405ad8 JB |
269 | |
270 | spin_unlock(&fs_info->block_group_cache_lock); | |
271 | btrfs_put_block_group(cache); | |
272 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; | |
273 | } | |
274 | node = rb_next(&cache->cache_node); | |
275 | btrfs_put_block_group(cache); | |
276 | if (node) { | |
32da5386 | 277 | cache = rb_entry(node, struct btrfs_block_group, cache_node); |
2e405ad8 JB |
278 | btrfs_get_block_group(cache); |
279 | } else | |
280 | cache = NULL; | |
281 | spin_unlock(&fs_info->block_group_cache_lock); | |
282 | return cache; | |
283 | } | |
3eeb3226 JB |
284 | |
285 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) | |
286 | { | |
32da5386 | 287 | struct btrfs_block_group *bg; |
3eeb3226 JB |
288 | bool ret = true; |
289 | ||
290 | bg = btrfs_lookup_block_group(fs_info, bytenr); | |
291 | if (!bg) | |
292 | return false; | |
293 | ||
294 | spin_lock(&bg->lock); | |
295 | if (bg->ro) | |
296 | ret = false; | |
297 | else | |
298 | atomic_inc(&bg->nocow_writers); | |
299 | spin_unlock(&bg->lock); | |
300 | ||
301 | /* No put on block group, done by btrfs_dec_nocow_writers */ | |
302 | if (!ret) | |
303 | btrfs_put_block_group(bg); | |
304 | ||
305 | return ret; | |
306 | } | |
307 | ||
308 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) | |
309 | { | |
32da5386 | 310 | struct btrfs_block_group *bg; |
3eeb3226 JB |
311 | |
312 | bg = btrfs_lookup_block_group(fs_info, bytenr); | |
313 | ASSERT(bg); | |
314 | if (atomic_dec_and_test(&bg->nocow_writers)) | |
315 | wake_up_var(&bg->nocow_writers); | |
316 | /* | |
317 | * Once for our lookup and once for the lookup done by a previous call | |
318 | * to btrfs_inc_nocow_writers() | |
319 | */ | |
320 | btrfs_put_block_group(bg); | |
321 | btrfs_put_block_group(bg); | |
322 | } | |
323 | ||
32da5386 | 324 | void btrfs_wait_nocow_writers(struct btrfs_block_group *bg) |
3eeb3226 JB |
325 | { |
326 | wait_var_event(&bg->nocow_writers, !atomic_read(&bg->nocow_writers)); | |
327 | } | |
328 | ||
329 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, | |
330 | const u64 start) | |
331 | { | |
32da5386 | 332 | struct btrfs_block_group *bg; |
3eeb3226 JB |
333 | |
334 | bg = btrfs_lookup_block_group(fs_info, start); | |
335 | ASSERT(bg); | |
336 | if (atomic_dec_and_test(&bg->reservations)) | |
337 | wake_up_var(&bg->reservations); | |
338 | btrfs_put_block_group(bg); | |
339 | } | |
340 | ||
32da5386 | 341 | void btrfs_wait_block_group_reservations(struct btrfs_block_group *bg) |
3eeb3226 JB |
342 | { |
343 | struct btrfs_space_info *space_info = bg->space_info; | |
344 | ||
345 | ASSERT(bg->ro); | |
346 | ||
347 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) | |
348 | return; | |
349 | ||
350 | /* | |
351 | * Our block group is read only but before we set it to read only, | |
352 | * some task might have had allocated an extent from it already, but it | |
353 | * has not yet created a respective ordered extent (and added it to a | |
354 | * root's list of ordered extents). | |
355 | * Therefore wait for any task currently allocating extents, since the | |
356 | * block group's reservations counter is incremented while a read lock | |
357 | * on the groups' semaphore is held and decremented after releasing | |
358 | * the read access on that semaphore and creating the ordered extent. | |
359 | */ | |
360 | down_write(&space_info->groups_sem); | |
361 | up_write(&space_info->groups_sem); | |
362 | ||
363 | wait_var_event(&bg->reservations, !atomic_read(&bg->reservations)); | |
364 | } | |
9f21246d JB |
365 | |
366 | struct btrfs_caching_control *btrfs_get_caching_control( | |
32da5386 | 367 | struct btrfs_block_group *cache) |
9f21246d JB |
368 | { |
369 | struct btrfs_caching_control *ctl; | |
370 | ||
371 | spin_lock(&cache->lock); | |
372 | if (!cache->caching_ctl) { | |
373 | spin_unlock(&cache->lock); | |
374 | return NULL; | |
375 | } | |
376 | ||
377 | ctl = cache->caching_ctl; | |
378 | refcount_inc(&ctl->count); | |
379 | spin_unlock(&cache->lock); | |
380 | return ctl; | |
381 | } | |
382 | ||
383 | void btrfs_put_caching_control(struct btrfs_caching_control *ctl) | |
384 | { | |
385 | if (refcount_dec_and_test(&ctl->count)) | |
386 | kfree(ctl); | |
387 | } | |
388 | ||
389 | /* | |
390 | * When we wait for progress in the block group caching, its because our | |
391 | * allocation attempt failed at least once. So, we must sleep and let some | |
392 | * progress happen before we try again. | |
393 | * | |
394 | * This function will sleep at least once waiting for new free space to show | |
395 | * up, and then it will check the block group free space numbers for our min | |
396 | * num_bytes. Another option is to have it go ahead and look in the rbtree for | |
397 | * a free extent of a given size, but this is a good start. | |
398 | * | |
399 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using | |
400 | * any of the information in this block group. | |
401 | */ | |
32da5386 | 402 | void btrfs_wait_block_group_cache_progress(struct btrfs_block_group *cache, |
9f21246d JB |
403 | u64 num_bytes) |
404 | { | |
405 | struct btrfs_caching_control *caching_ctl; | |
406 | ||
407 | caching_ctl = btrfs_get_caching_control(cache); | |
408 | if (!caching_ctl) | |
409 | return; | |
410 | ||
32da5386 | 411 | wait_event(caching_ctl->wait, btrfs_block_group_done(cache) || |
9f21246d JB |
412 | (cache->free_space_ctl->free_space >= num_bytes)); |
413 | ||
414 | btrfs_put_caching_control(caching_ctl); | |
415 | } | |
416 | ||
32da5386 | 417 | int btrfs_wait_block_group_cache_done(struct btrfs_block_group *cache) |
9f21246d JB |
418 | { |
419 | struct btrfs_caching_control *caching_ctl; | |
420 | int ret = 0; | |
421 | ||
422 | caching_ctl = btrfs_get_caching_control(cache); | |
423 | if (!caching_ctl) | |
424 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; | |
425 | ||
32da5386 | 426 | wait_event(caching_ctl->wait, btrfs_block_group_done(cache)); |
9f21246d JB |
427 | if (cache->cached == BTRFS_CACHE_ERROR) |
428 | ret = -EIO; | |
429 | btrfs_put_caching_control(caching_ctl); | |
430 | return ret; | |
431 | } | |
432 | ||
433 | #ifdef CONFIG_BTRFS_DEBUG | |
32da5386 | 434 | static void fragment_free_space(struct btrfs_block_group *block_group) |
9f21246d JB |
435 | { |
436 | struct btrfs_fs_info *fs_info = block_group->fs_info; | |
b3470b5d DS |
437 | u64 start = block_group->start; |
438 | u64 len = block_group->length; | |
9f21246d JB |
439 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? |
440 | fs_info->nodesize : fs_info->sectorsize; | |
441 | u64 step = chunk << 1; | |
442 | ||
443 | while (len > chunk) { | |
444 | btrfs_remove_free_space(block_group, start, chunk); | |
445 | start += step; | |
446 | if (len < step) | |
447 | len = 0; | |
448 | else | |
449 | len -= step; | |
450 | } | |
451 | } | |
452 | #endif | |
453 | ||
454 | /* | |
455 | * This is only called by btrfs_cache_block_group, since we could have freed | |
456 | * extents we need to check the pinned_extents for any extents that can't be | |
457 | * used yet since their free space will be released as soon as the transaction | |
458 | * commits. | |
459 | */ | |
32da5386 | 460 | u64 add_new_free_space(struct btrfs_block_group *block_group, u64 start, u64 end) |
9f21246d JB |
461 | { |
462 | struct btrfs_fs_info *info = block_group->fs_info; | |
463 | u64 extent_start, extent_end, size, total_added = 0; | |
464 | int ret; | |
465 | ||
466 | while (start < end) { | |
467 | ret = find_first_extent_bit(info->pinned_extents, start, | |
468 | &extent_start, &extent_end, | |
469 | EXTENT_DIRTY | EXTENT_UPTODATE, | |
470 | NULL); | |
471 | if (ret) | |
472 | break; | |
473 | ||
474 | if (extent_start <= start) { | |
475 | start = extent_end + 1; | |
476 | } else if (extent_start > start && extent_start < end) { | |
477 | size = extent_start - start; | |
478 | total_added += size; | |
b0643e59 DZ |
479 | ret = btrfs_add_free_space_async_trimmed(block_group, |
480 | start, size); | |
9f21246d JB |
481 | BUG_ON(ret); /* -ENOMEM or logic error */ |
482 | start = extent_end + 1; | |
483 | } else { | |
484 | break; | |
485 | } | |
486 | } | |
487 | ||
488 | if (start < end) { | |
489 | size = end - start; | |
490 | total_added += size; | |
b0643e59 DZ |
491 | ret = btrfs_add_free_space_async_trimmed(block_group, start, |
492 | size); | |
9f21246d JB |
493 | BUG_ON(ret); /* -ENOMEM or logic error */ |
494 | } | |
495 | ||
496 | return total_added; | |
497 | } | |
498 | ||
499 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) | |
500 | { | |
32da5386 | 501 | struct btrfs_block_group *block_group = caching_ctl->block_group; |
9f21246d JB |
502 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
503 | struct btrfs_root *extent_root = fs_info->extent_root; | |
504 | struct btrfs_path *path; | |
505 | struct extent_buffer *leaf; | |
506 | struct btrfs_key key; | |
507 | u64 total_found = 0; | |
508 | u64 last = 0; | |
509 | u32 nritems; | |
510 | int ret; | |
511 | bool wakeup = true; | |
512 | ||
513 | path = btrfs_alloc_path(); | |
514 | if (!path) | |
515 | return -ENOMEM; | |
516 | ||
b3470b5d | 517 | last = max_t(u64, block_group->start, BTRFS_SUPER_INFO_OFFSET); |
9f21246d JB |
518 | |
519 | #ifdef CONFIG_BTRFS_DEBUG | |
520 | /* | |
521 | * If we're fragmenting we don't want to make anybody think we can | |
522 | * allocate from this block group until we've had a chance to fragment | |
523 | * the free space. | |
524 | */ | |
525 | if (btrfs_should_fragment_free_space(block_group)) | |
526 | wakeup = false; | |
527 | #endif | |
528 | /* | |
529 | * We don't want to deadlock with somebody trying to allocate a new | |
530 | * extent for the extent root while also trying to search the extent | |
531 | * root to add free space. So we skip locking and search the commit | |
532 | * root, since its read-only | |
533 | */ | |
534 | path->skip_locking = 1; | |
535 | path->search_commit_root = 1; | |
536 | path->reada = READA_FORWARD; | |
537 | ||
538 | key.objectid = last; | |
539 | key.offset = 0; | |
540 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
541 | ||
542 | next: | |
543 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | |
544 | if (ret < 0) | |
545 | goto out; | |
546 | ||
547 | leaf = path->nodes[0]; | |
548 | nritems = btrfs_header_nritems(leaf); | |
549 | ||
550 | while (1) { | |
551 | if (btrfs_fs_closing(fs_info) > 1) { | |
552 | last = (u64)-1; | |
553 | break; | |
554 | } | |
555 | ||
556 | if (path->slots[0] < nritems) { | |
557 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
558 | } else { | |
559 | ret = btrfs_find_next_key(extent_root, path, &key, 0, 0); | |
560 | if (ret) | |
561 | break; | |
562 | ||
563 | if (need_resched() || | |
564 | rwsem_is_contended(&fs_info->commit_root_sem)) { | |
565 | if (wakeup) | |
566 | caching_ctl->progress = last; | |
567 | btrfs_release_path(path); | |
568 | up_read(&fs_info->commit_root_sem); | |
569 | mutex_unlock(&caching_ctl->mutex); | |
570 | cond_resched(); | |
571 | mutex_lock(&caching_ctl->mutex); | |
572 | down_read(&fs_info->commit_root_sem); | |
573 | goto next; | |
574 | } | |
575 | ||
576 | ret = btrfs_next_leaf(extent_root, path); | |
577 | if (ret < 0) | |
578 | goto out; | |
579 | if (ret) | |
580 | break; | |
581 | leaf = path->nodes[0]; | |
582 | nritems = btrfs_header_nritems(leaf); | |
583 | continue; | |
584 | } | |
585 | ||
586 | if (key.objectid < last) { | |
587 | key.objectid = last; | |
588 | key.offset = 0; | |
589 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
590 | ||
591 | if (wakeup) | |
592 | caching_ctl->progress = last; | |
593 | btrfs_release_path(path); | |
594 | goto next; | |
595 | } | |
596 | ||
b3470b5d | 597 | if (key.objectid < block_group->start) { |
9f21246d JB |
598 | path->slots[0]++; |
599 | continue; | |
600 | } | |
601 | ||
b3470b5d | 602 | if (key.objectid >= block_group->start + block_group->length) |
9f21246d JB |
603 | break; |
604 | ||
605 | if (key.type == BTRFS_EXTENT_ITEM_KEY || | |
606 | key.type == BTRFS_METADATA_ITEM_KEY) { | |
607 | total_found += add_new_free_space(block_group, last, | |
608 | key.objectid); | |
609 | if (key.type == BTRFS_METADATA_ITEM_KEY) | |
610 | last = key.objectid + | |
611 | fs_info->nodesize; | |
612 | else | |
613 | last = key.objectid + key.offset; | |
614 | ||
615 | if (total_found > CACHING_CTL_WAKE_UP) { | |
616 | total_found = 0; | |
617 | if (wakeup) | |
618 | wake_up(&caching_ctl->wait); | |
619 | } | |
620 | } | |
621 | path->slots[0]++; | |
622 | } | |
623 | ret = 0; | |
624 | ||
625 | total_found += add_new_free_space(block_group, last, | |
b3470b5d | 626 | block_group->start + block_group->length); |
9f21246d JB |
627 | caching_ctl->progress = (u64)-1; |
628 | ||
629 | out: | |
630 | btrfs_free_path(path); | |
631 | return ret; | |
632 | } | |
633 | ||
634 | static noinline void caching_thread(struct btrfs_work *work) | |
635 | { | |
32da5386 | 636 | struct btrfs_block_group *block_group; |
9f21246d JB |
637 | struct btrfs_fs_info *fs_info; |
638 | struct btrfs_caching_control *caching_ctl; | |
639 | int ret; | |
640 | ||
641 | caching_ctl = container_of(work, struct btrfs_caching_control, work); | |
642 | block_group = caching_ctl->block_group; | |
643 | fs_info = block_group->fs_info; | |
644 | ||
645 | mutex_lock(&caching_ctl->mutex); | |
646 | down_read(&fs_info->commit_root_sem); | |
647 | ||
648 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) | |
649 | ret = load_free_space_tree(caching_ctl); | |
650 | else | |
651 | ret = load_extent_tree_free(caching_ctl); | |
652 | ||
653 | spin_lock(&block_group->lock); | |
654 | block_group->caching_ctl = NULL; | |
655 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; | |
656 | spin_unlock(&block_group->lock); | |
657 | ||
658 | #ifdef CONFIG_BTRFS_DEBUG | |
659 | if (btrfs_should_fragment_free_space(block_group)) { | |
660 | u64 bytes_used; | |
661 | ||
662 | spin_lock(&block_group->space_info->lock); | |
663 | spin_lock(&block_group->lock); | |
b3470b5d | 664 | bytes_used = block_group->length - block_group->used; |
9f21246d JB |
665 | block_group->space_info->bytes_used += bytes_used >> 1; |
666 | spin_unlock(&block_group->lock); | |
667 | spin_unlock(&block_group->space_info->lock); | |
e11c0406 | 668 | fragment_free_space(block_group); |
9f21246d JB |
669 | } |
670 | #endif | |
671 | ||
672 | caching_ctl->progress = (u64)-1; | |
673 | ||
674 | up_read(&fs_info->commit_root_sem); | |
675 | btrfs_free_excluded_extents(block_group); | |
676 | mutex_unlock(&caching_ctl->mutex); | |
677 | ||
678 | wake_up(&caching_ctl->wait); | |
679 | ||
680 | btrfs_put_caching_control(caching_ctl); | |
681 | btrfs_put_block_group(block_group); | |
682 | } | |
683 | ||
32da5386 | 684 | int btrfs_cache_block_group(struct btrfs_block_group *cache, int load_cache_only) |
9f21246d JB |
685 | { |
686 | DEFINE_WAIT(wait); | |
687 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
688 | struct btrfs_caching_control *caching_ctl; | |
689 | int ret = 0; | |
690 | ||
691 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); | |
692 | if (!caching_ctl) | |
693 | return -ENOMEM; | |
694 | ||
695 | INIT_LIST_HEAD(&caching_ctl->list); | |
696 | mutex_init(&caching_ctl->mutex); | |
697 | init_waitqueue_head(&caching_ctl->wait); | |
698 | caching_ctl->block_group = cache; | |
b3470b5d | 699 | caching_ctl->progress = cache->start; |
9f21246d | 700 | refcount_set(&caching_ctl->count, 1); |
a0cac0ec | 701 | btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL); |
9f21246d JB |
702 | |
703 | spin_lock(&cache->lock); | |
704 | /* | |
705 | * This should be a rare occasion, but this could happen I think in the | |
706 | * case where one thread starts to load the space cache info, and then | |
707 | * some other thread starts a transaction commit which tries to do an | |
708 | * allocation while the other thread is still loading the space cache | |
709 | * info. The previous loop should have kept us from choosing this block | |
710 | * group, but if we've moved to the state where we will wait on caching | |
711 | * block groups we need to first check if we're doing a fast load here, | |
712 | * so we can wait for it to finish, otherwise we could end up allocating | |
713 | * from a block group who's cache gets evicted for one reason or | |
714 | * another. | |
715 | */ | |
716 | while (cache->cached == BTRFS_CACHE_FAST) { | |
717 | struct btrfs_caching_control *ctl; | |
718 | ||
719 | ctl = cache->caching_ctl; | |
720 | refcount_inc(&ctl->count); | |
721 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); | |
722 | spin_unlock(&cache->lock); | |
723 | ||
724 | schedule(); | |
725 | ||
726 | finish_wait(&ctl->wait, &wait); | |
727 | btrfs_put_caching_control(ctl); | |
728 | spin_lock(&cache->lock); | |
729 | } | |
730 | ||
731 | if (cache->cached != BTRFS_CACHE_NO) { | |
732 | spin_unlock(&cache->lock); | |
733 | kfree(caching_ctl); | |
734 | return 0; | |
735 | } | |
736 | WARN_ON(cache->caching_ctl); | |
737 | cache->caching_ctl = caching_ctl; | |
738 | cache->cached = BTRFS_CACHE_FAST; | |
739 | spin_unlock(&cache->lock); | |
740 | ||
741 | if (btrfs_test_opt(fs_info, SPACE_CACHE)) { | |
742 | mutex_lock(&caching_ctl->mutex); | |
743 | ret = load_free_space_cache(cache); | |
744 | ||
745 | spin_lock(&cache->lock); | |
746 | if (ret == 1) { | |
747 | cache->caching_ctl = NULL; | |
748 | cache->cached = BTRFS_CACHE_FINISHED; | |
749 | cache->last_byte_to_unpin = (u64)-1; | |
750 | caching_ctl->progress = (u64)-1; | |
751 | } else { | |
752 | if (load_cache_only) { | |
753 | cache->caching_ctl = NULL; | |
754 | cache->cached = BTRFS_CACHE_NO; | |
755 | } else { | |
756 | cache->cached = BTRFS_CACHE_STARTED; | |
757 | cache->has_caching_ctl = 1; | |
758 | } | |
759 | } | |
760 | spin_unlock(&cache->lock); | |
761 | #ifdef CONFIG_BTRFS_DEBUG | |
762 | if (ret == 1 && | |
763 | btrfs_should_fragment_free_space(cache)) { | |
764 | u64 bytes_used; | |
765 | ||
766 | spin_lock(&cache->space_info->lock); | |
767 | spin_lock(&cache->lock); | |
b3470b5d | 768 | bytes_used = cache->length - cache->used; |
9f21246d JB |
769 | cache->space_info->bytes_used += bytes_used >> 1; |
770 | spin_unlock(&cache->lock); | |
771 | spin_unlock(&cache->space_info->lock); | |
e11c0406 | 772 | fragment_free_space(cache); |
9f21246d JB |
773 | } |
774 | #endif | |
775 | mutex_unlock(&caching_ctl->mutex); | |
776 | ||
777 | wake_up(&caching_ctl->wait); | |
778 | if (ret == 1) { | |
779 | btrfs_put_caching_control(caching_ctl); | |
780 | btrfs_free_excluded_extents(cache); | |
781 | return 0; | |
782 | } | |
783 | } else { | |
784 | /* | |
785 | * We're either using the free space tree or no caching at all. | |
786 | * Set cached to the appropriate value and wakeup any waiters. | |
787 | */ | |
788 | spin_lock(&cache->lock); | |
789 | if (load_cache_only) { | |
790 | cache->caching_ctl = NULL; | |
791 | cache->cached = BTRFS_CACHE_NO; | |
792 | } else { | |
793 | cache->cached = BTRFS_CACHE_STARTED; | |
794 | cache->has_caching_ctl = 1; | |
795 | } | |
796 | spin_unlock(&cache->lock); | |
797 | wake_up(&caching_ctl->wait); | |
798 | } | |
799 | ||
800 | if (load_cache_only) { | |
801 | btrfs_put_caching_control(caching_ctl); | |
802 | return 0; | |
803 | } | |
804 | ||
805 | down_write(&fs_info->commit_root_sem); | |
806 | refcount_inc(&caching_ctl->count); | |
807 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); | |
808 | up_write(&fs_info->commit_root_sem); | |
809 | ||
810 | btrfs_get_block_group(cache); | |
811 | ||
812 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); | |
813 | ||
814 | return ret; | |
815 | } | |
e3e0520b JB |
816 | |
817 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) | |
818 | { | |
819 | u64 extra_flags = chunk_to_extended(flags) & | |
820 | BTRFS_EXTENDED_PROFILE_MASK; | |
821 | ||
822 | write_seqlock(&fs_info->profiles_lock); | |
823 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
824 | fs_info->avail_data_alloc_bits &= ~extra_flags; | |
825 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
826 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; | |
827 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
828 | fs_info->avail_system_alloc_bits &= ~extra_flags; | |
829 | write_sequnlock(&fs_info->profiles_lock); | |
830 | } | |
831 | ||
832 | /* | |
833 | * Clear incompat bits for the following feature(s): | |
834 | * | |
835 | * - RAID56 - in case there's neither RAID5 nor RAID6 profile block group | |
836 | * in the whole filesystem | |
9c907446 DS |
837 | * |
838 | * - RAID1C34 - same as above for RAID1C3 and RAID1C4 block groups | |
e3e0520b JB |
839 | */ |
840 | static void clear_incompat_bg_bits(struct btrfs_fs_info *fs_info, u64 flags) | |
841 | { | |
9c907446 DS |
842 | bool found_raid56 = false; |
843 | bool found_raid1c34 = false; | |
844 | ||
845 | if ((flags & BTRFS_BLOCK_GROUP_RAID56_MASK) || | |
846 | (flags & BTRFS_BLOCK_GROUP_RAID1C3) || | |
847 | (flags & BTRFS_BLOCK_GROUP_RAID1C4)) { | |
e3e0520b JB |
848 | struct list_head *head = &fs_info->space_info; |
849 | struct btrfs_space_info *sinfo; | |
850 | ||
851 | list_for_each_entry_rcu(sinfo, head, list) { | |
e3e0520b JB |
852 | down_read(&sinfo->groups_sem); |
853 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID5])) | |
9c907446 | 854 | found_raid56 = true; |
e3e0520b | 855 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID6])) |
9c907446 DS |
856 | found_raid56 = true; |
857 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C3])) | |
858 | found_raid1c34 = true; | |
859 | if (!list_empty(&sinfo->block_groups[BTRFS_RAID_RAID1C4])) | |
860 | found_raid1c34 = true; | |
e3e0520b | 861 | up_read(&sinfo->groups_sem); |
e3e0520b | 862 | } |
9c907446 DS |
863 | if (found_raid56) |
864 | btrfs_clear_fs_incompat(fs_info, RAID56); | |
865 | if (found_raid1c34) | |
866 | btrfs_clear_fs_incompat(fs_info, RAID1C34); | |
e3e0520b JB |
867 | } |
868 | } | |
869 | ||
870 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, | |
871 | u64 group_start, struct extent_map *em) | |
872 | { | |
873 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
874 | struct btrfs_root *root = fs_info->extent_root; | |
875 | struct btrfs_path *path; | |
32da5386 | 876 | struct btrfs_block_group *block_group; |
e3e0520b JB |
877 | struct btrfs_free_cluster *cluster; |
878 | struct btrfs_root *tree_root = fs_info->tree_root; | |
879 | struct btrfs_key key; | |
880 | struct inode *inode; | |
881 | struct kobject *kobj = NULL; | |
882 | int ret; | |
883 | int index; | |
884 | int factor; | |
885 | struct btrfs_caching_control *caching_ctl = NULL; | |
886 | bool remove_em; | |
887 | bool remove_rsv = false; | |
888 | ||
889 | block_group = btrfs_lookup_block_group(fs_info, group_start); | |
890 | BUG_ON(!block_group); | |
891 | BUG_ON(!block_group->ro); | |
892 | ||
893 | trace_btrfs_remove_block_group(block_group); | |
894 | /* | |
895 | * Free the reserved super bytes from this block group before | |
896 | * remove it. | |
897 | */ | |
898 | btrfs_free_excluded_extents(block_group); | |
b3470b5d DS |
899 | btrfs_free_ref_tree_range(fs_info, block_group->start, |
900 | block_group->length); | |
e3e0520b | 901 | |
e3e0520b JB |
902 | index = btrfs_bg_flags_to_raid_index(block_group->flags); |
903 | factor = btrfs_bg_type_to_factor(block_group->flags); | |
904 | ||
905 | /* make sure this block group isn't part of an allocation cluster */ | |
906 | cluster = &fs_info->data_alloc_cluster; | |
907 | spin_lock(&cluster->refill_lock); | |
908 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
909 | spin_unlock(&cluster->refill_lock); | |
910 | ||
911 | /* | |
912 | * make sure this block group isn't part of a metadata | |
913 | * allocation cluster | |
914 | */ | |
915 | cluster = &fs_info->meta_alloc_cluster; | |
916 | spin_lock(&cluster->refill_lock); | |
917 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
918 | spin_unlock(&cluster->refill_lock); | |
919 | ||
920 | path = btrfs_alloc_path(); | |
921 | if (!path) { | |
922 | ret = -ENOMEM; | |
923 | goto out; | |
924 | } | |
925 | ||
926 | /* | |
927 | * get the inode first so any iput calls done for the io_list | |
928 | * aren't the final iput (no unlinks allowed now) | |
929 | */ | |
930 | inode = lookup_free_space_inode(block_group, path); | |
931 | ||
932 | mutex_lock(&trans->transaction->cache_write_mutex); | |
933 | /* | |
934 | * Make sure our free space cache IO is done before removing the | |
935 | * free space inode | |
936 | */ | |
937 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
938 | if (!list_empty(&block_group->io_list)) { | |
939 | list_del_init(&block_group->io_list); | |
940 | ||
941 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); | |
942 | ||
943 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
944 | btrfs_wait_cache_io(trans, block_group, path); | |
945 | btrfs_put_block_group(block_group); | |
946 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
947 | } | |
948 | ||
949 | if (!list_empty(&block_group->dirty_list)) { | |
950 | list_del_init(&block_group->dirty_list); | |
951 | remove_rsv = true; | |
952 | btrfs_put_block_group(block_group); | |
953 | } | |
954 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
955 | mutex_unlock(&trans->transaction->cache_write_mutex); | |
956 | ||
957 | if (!IS_ERR(inode)) { | |
958 | ret = btrfs_orphan_add(trans, BTRFS_I(inode)); | |
959 | if (ret) { | |
960 | btrfs_add_delayed_iput(inode); | |
961 | goto out; | |
962 | } | |
963 | clear_nlink(inode); | |
964 | /* One for the block groups ref */ | |
965 | spin_lock(&block_group->lock); | |
966 | if (block_group->iref) { | |
967 | block_group->iref = 0; | |
968 | block_group->inode = NULL; | |
969 | spin_unlock(&block_group->lock); | |
970 | iput(inode); | |
971 | } else { | |
972 | spin_unlock(&block_group->lock); | |
973 | } | |
974 | /* One for our lookup ref */ | |
975 | btrfs_add_delayed_iput(inode); | |
976 | } | |
977 | ||
978 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
e3e0520b | 979 | key.type = 0; |
b3470b5d | 980 | key.offset = block_group->start; |
e3e0520b JB |
981 | |
982 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); | |
983 | if (ret < 0) | |
984 | goto out; | |
985 | if (ret > 0) | |
986 | btrfs_release_path(path); | |
987 | if (ret == 0) { | |
988 | ret = btrfs_del_item(trans, tree_root, path); | |
989 | if (ret) | |
990 | goto out; | |
991 | btrfs_release_path(path); | |
992 | } | |
993 | ||
994 | spin_lock(&fs_info->block_group_cache_lock); | |
995 | rb_erase(&block_group->cache_node, | |
996 | &fs_info->block_group_cache_tree); | |
997 | RB_CLEAR_NODE(&block_group->cache_node); | |
998 | ||
b3470b5d | 999 | if (fs_info->first_logical_byte == block_group->start) |
e3e0520b JB |
1000 | fs_info->first_logical_byte = (u64)-1; |
1001 | spin_unlock(&fs_info->block_group_cache_lock); | |
1002 | ||
1003 | down_write(&block_group->space_info->groups_sem); | |
1004 | /* | |
1005 | * we must use list_del_init so people can check to see if they | |
1006 | * are still on the list after taking the semaphore | |
1007 | */ | |
1008 | list_del_init(&block_group->list); | |
1009 | if (list_empty(&block_group->space_info->block_groups[index])) { | |
1010 | kobj = block_group->space_info->block_group_kobjs[index]; | |
1011 | block_group->space_info->block_group_kobjs[index] = NULL; | |
1012 | clear_avail_alloc_bits(fs_info, block_group->flags); | |
1013 | } | |
1014 | up_write(&block_group->space_info->groups_sem); | |
1015 | clear_incompat_bg_bits(fs_info, block_group->flags); | |
1016 | if (kobj) { | |
1017 | kobject_del(kobj); | |
1018 | kobject_put(kobj); | |
1019 | } | |
1020 | ||
1021 | if (block_group->has_caching_ctl) | |
1022 | caching_ctl = btrfs_get_caching_control(block_group); | |
1023 | if (block_group->cached == BTRFS_CACHE_STARTED) | |
1024 | btrfs_wait_block_group_cache_done(block_group); | |
1025 | if (block_group->has_caching_ctl) { | |
1026 | down_write(&fs_info->commit_root_sem); | |
1027 | if (!caching_ctl) { | |
1028 | struct btrfs_caching_control *ctl; | |
1029 | ||
1030 | list_for_each_entry(ctl, | |
1031 | &fs_info->caching_block_groups, list) | |
1032 | if (ctl->block_group == block_group) { | |
1033 | caching_ctl = ctl; | |
1034 | refcount_inc(&caching_ctl->count); | |
1035 | break; | |
1036 | } | |
1037 | } | |
1038 | if (caching_ctl) | |
1039 | list_del_init(&caching_ctl->list); | |
1040 | up_write(&fs_info->commit_root_sem); | |
1041 | if (caching_ctl) { | |
1042 | /* Once for the caching bgs list and once for us. */ | |
1043 | btrfs_put_caching_control(caching_ctl); | |
1044 | btrfs_put_caching_control(caching_ctl); | |
1045 | } | |
1046 | } | |
1047 | ||
1048 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
1049 | WARN_ON(!list_empty(&block_group->dirty_list)); | |
1050 | WARN_ON(!list_empty(&block_group->io_list)); | |
1051 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
1052 | ||
1053 | btrfs_remove_free_space_cache(block_group); | |
1054 | ||
1055 | spin_lock(&block_group->space_info->lock); | |
1056 | list_del_init(&block_group->ro_list); | |
1057 | ||
1058 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { | |
1059 | WARN_ON(block_group->space_info->total_bytes | |
b3470b5d | 1060 | < block_group->length); |
e3e0520b | 1061 | WARN_ON(block_group->space_info->bytes_readonly |
b3470b5d | 1062 | < block_group->length); |
e3e0520b | 1063 | WARN_ON(block_group->space_info->disk_total |
b3470b5d | 1064 | < block_group->length * factor); |
e3e0520b | 1065 | } |
b3470b5d DS |
1066 | block_group->space_info->total_bytes -= block_group->length; |
1067 | block_group->space_info->bytes_readonly -= block_group->length; | |
1068 | block_group->space_info->disk_total -= block_group->length * factor; | |
e3e0520b JB |
1069 | |
1070 | spin_unlock(&block_group->space_info->lock); | |
1071 | ||
b3470b5d DS |
1072 | key.objectid = block_group->start; |
1073 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | |
1074 | key.offset = block_group->length; | |
e3e0520b JB |
1075 | |
1076 | mutex_lock(&fs_info->chunk_mutex); | |
1077 | spin_lock(&block_group->lock); | |
1078 | block_group->removed = 1; | |
1079 | /* | |
1080 | * At this point trimming can't start on this block group, because we | |
1081 | * removed the block group from the tree fs_info->block_group_cache_tree | |
1082 | * so no one can't find it anymore and even if someone already got this | |
1083 | * block group before we removed it from the rbtree, they have already | |
1084 | * incremented block_group->trimming - if they didn't, they won't find | |
1085 | * any free space entries because we already removed them all when we | |
1086 | * called btrfs_remove_free_space_cache(). | |
1087 | * | |
1088 | * And we must not remove the extent map from the fs_info->mapping_tree | |
1089 | * to prevent the same logical address range and physical device space | |
1090 | * ranges from being reused for a new block group. This is because our | |
1091 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is | |
1092 | * completely transactionless, so while it is trimming a range the | |
1093 | * currently running transaction might finish and a new one start, | |
1094 | * allowing for new block groups to be created that can reuse the same | |
1095 | * physical device locations unless we take this special care. | |
1096 | * | |
1097 | * There may also be an implicit trim operation if the file system | |
1098 | * is mounted with -odiscard. The same protections must remain | |
1099 | * in place until the extents have been discarded completely when | |
1100 | * the transaction commit has completed. | |
1101 | */ | |
1102 | remove_em = (atomic_read(&block_group->trimming) == 0); | |
1103 | spin_unlock(&block_group->lock); | |
1104 | ||
1105 | mutex_unlock(&fs_info->chunk_mutex); | |
1106 | ||
1107 | ret = remove_block_group_free_space(trans, block_group); | |
1108 | if (ret) | |
1109 | goto out; | |
1110 | ||
1111 | btrfs_put_block_group(block_group); | |
1112 | btrfs_put_block_group(block_group); | |
1113 | ||
1114 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1115 | if (ret > 0) | |
1116 | ret = -EIO; | |
1117 | if (ret < 0) | |
1118 | goto out; | |
1119 | ||
1120 | ret = btrfs_del_item(trans, root, path); | |
1121 | if (ret) | |
1122 | goto out; | |
1123 | ||
1124 | if (remove_em) { | |
1125 | struct extent_map_tree *em_tree; | |
1126 | ||
1127 | em_tree = &fs_info->mapping_tree; | |
1128 | write_lock(&em_tree->lock); | |
1129 | remove_extent_mapping(em_tree, em); | |
1130 | write_unlock(&em_tree->lock); | |
1131 | /* once for the tree */ | |
1132 | free_extent_map(em); | |
1133 | } | |
1134 | out: | |
1135 | if (remove_rsv) | |
1136 | btrfs_delayed_refs_rsv_release(fs_info, 1); | |
1137 | btrfs_free_path(path); | |
1138 | return ret; | |
1139 | } | |
1140 | ||
1141 | struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( | |
1142 | struct btrfs_fs_info *fs_info, const u64 chunk_offset) | |
1143 | { | |
1144 | struct extent_map_tree *em_tree = &fs_info->mapping_tree; | |
1145 | struct extent_map *em; | |
1146 | struct map_lookup *map; | |
1147 | unsigned int num_items; | |
1148 | ||
1149 | read_lock(&em_tree->lock); | |
1150 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); | |
1151 | read_unlock(&em_tree->lock); | |
1152 | ASSERT(em && em->start == chunk_offset); | |
1153 | ||
1154 | /* | |
1155 | * We need to reserve 3 + N units from the metadata space info in order | |
1156 | * to remove a block group (done at btrfs_remove_chunk() and at | |
1157 | * btrfs_remove_block_group()), which are used for: | |
1158 | * | |
1159 | * 1 unit for adding the free space inode's orphan (located in the tree | |
1160 | * of tree roots). | |
1161 | * 1 unit for deleting the block group item (located in the extent | |
1162 | * tree). | |
1163 | * 1 unit for deleting the free space item (located in tree of tree | |
1164 | * roots). | |
1165 | * N units for deleting N device extent items corresponding to each | |
1166 | * stripe (located in the device tree). | |
1167 | * | |
1168 | * In order to remove a block group we also need to reserve units in the | |
1169 | * system space info in order to update the chunk tree (update one or | |
1170 | * more device items and remove one chunk item), but this is done at | |
1171 | * btrfs_remove_chunk() through a call to check_system_chunk(). | |
1172 | */ | |
1173 | map = em->map_lookup; | |
1174 | num_items = 3 + map->num_stripes; | |
1175 | free_extent_map(em); | |
1176 | ||
1177 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, | |
1178 | num_items, 1); | |
1179 | } | |
1180 | ||
26ce2095 JB |
1181 | /* |
1182 | * Mark block group @cache read-only, so later write won't happen to block | |
1183 | * group @cache. | |
1184 | * | |
1185 | * If @force is not set, this function will only mark the block group readonly | |
1186 | * if we have enough free space (1M) in other metadata/system block groups. | |
1187 | * If @force is not set, this function will mark the block group readonly | |
1188 | * without checking free space. | |
1189 | * | |
1190 | * NOTE: This function doesn't care if other block groups can contain all the | |
1191 | * data in this block group. That check should be done by relocation routine, | |
1192 | * not this function. | |
1193 | */ | |
32da5386 | 1194 | static int inc_block_group_ro(struct btrfs_block_group *cache, int force) |
26ce2095 JB |
1195 | { |
1196 | struct btrfs_space_info *sinfo = cache->space_info; | |
1197 | u64 num_bytes; | |
1198 | u64 sinfo_used; | |
26ce2095 JB |
1199 | int ret = -ENOSPC; |
1200 | ||
26ce2095 JB |
1201 | spin_lock(&sinfo->lock); |
1202 | spin_lock(&cache->lock); | |
1203 | ||
1204 | if (cache->ro) { | |
1205 | cache->ro++; | |
1206 | ret = 0; | |
1207 | goto out; | |
1208 | } | |
1209 | ||
b3470b5d | 1210 | num_bytes = cache->length - cache->reserved - cache->pinned - |
bf38be65 | 1211 | cache->bytes_super - cache->used; |
26ce2095 JB |
1212 | sinfo_used = btrfs_space_info_used(sinfo, true); |
1213 | ||
1214 | /* | |
1215 | * sinfo_used + num_bytes should always <= sinfo->total_bytes. | |
1216 | * | |
1217 | * Here we make sure if we mark this bg RO, we still have enough | |
f8935566 | 1218 | * free space as buffer. |
26ce2095 | 1219 | */ |
f8935566 | 1220 | if (sinfo_used + num_bytes <= sinfo->total_bytes) { |
26ce2095 JB |
1221 | sinfo->bytes_readonly += num_bytes; |
1222 | cache->ro++; | |
1223 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); | |
1224 | ret = 0; | |
1225 | } | |
1226 | out: | |
1227 | spin_unlock(&cache->lock); | |
1228 | spin_unlock(&sinfo->lock); | |
1229 | if (ret == -ENOSPC && btrfs_test_opt(cache->fs_info, ENOSPC_DEBUG)) { | |
1230 | btrfs_info(cache->fs_info, | |
b3470b5d | 1231 | "unable to make block group %llu ro", cache->start); |
26ce2095 | 1232 | btrfs_info(cache->fs_info, |
f8935566 JB |
1233 | "sinfo_used=%llu bg_num_bytes=%llu", |
1234 | sinfo_used, num_bytes); | |
26ce2095 JB |
1235 | btrfs_dump_space_info(cache->fs_info, cache->space_info, 0, 0); |
1236 | } | |
1237 | return ret; | |
1238 | } | |
1239 | ||
e3e0520b JB |
1240 | /* |
1241 | * Process the unused_bgs list and remove any that don't have any allocated | |
1242 | * space inside of them. | |
1243 | */ | |
1244 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) | |
1245 | { | |
32da5386 | 1246 | struct btrfs_block_group *block_group; |
e3e0520b JB |
1247 | struct btrfs_space_info *space_info; |
1248 | struct btrfs_trans_handle *trans; | |
1249 | int ret = 0; | |
1250 | ||
1251 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) | |
1252 | return; | |
1253 | ||
1254 | spin_lock(&fs_info->unused_bgs_lock); | |
1255 | while (!list_empty(&fs_info->unused_bgs)) { | |
1256 | u64 start, end; | |
1257 | int trimming; | |
1258 | ||
1259 | block_group = list_first_entry(&fs_info->unused_bgs, | |
32da5386 | 1260 | struct btrfs_block_group, |
e3e0520b JB |
1261 | bg_list); |
1262 | list_del_init(&block_group->bg_list); | |
1263 | ||
1264 | space_info = block_group->space_info; | |
1265 | ||
1266 | if (ret || btrfs_mixed_space_info(space_info)) { | |
1267 | btrfs_put_block_group(block_group); | |
1268 | continue; | |
1269 | } | |
1270 | spin_unlock(&fs_info->unused_bgs_lock); | |
1271 | ||
b0643e59 DZ |
1272 | btrfs_discard_cancel_work(&fs_info->discard_ctl, block_group); |
1273 | ||
e3e0520b JB |
1274 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
1275 | ||
1276 | /* Don't want to race with allocators so take the groups_sem */ | |
1277 | down_write(&space_info->groups_sem); | |
1278 | spin_lock(&block_group->lock); | |
1279 | if (block_group->reserved || block_group->pinned || | |
bf38be65 | 1280 | block_group->used || block_group->ro || |
e3e0520b JB |
1281 | list_is_singular(&block_group->list)) { |
1282 | /* | |
1283 | * We want to bail if we made new allocations or have | |
1284 | * outstanding allocations in this block group. We do | |
1285 | * the ro check in case balance is currently acting on | |
1286 | * this block group. | |
1287 | */ | |
1288 | trace_btrfs_skip_unused_block_group(block_group); | |
1289 | spin_unlock(&block_group->lock); | |
1290 | up_write(&space_info->groups_sem); | |
1291 | goto next; | |
1292 | } | |
1293 | spin_unlock(&block_group->lock); | |
1294 | ||
1295 | /* We don't want to force the issue, only flip if it's ok. */ | |
e11c0406 | 1296 | ret = inc_block_group_ro(block_group, 0); |
e3e0520b JB |
1297 | up_write(&space_info->groups_sem); |
1298 | if (ret < 0) { | |
1299 | ret = 0; | |
1300 | goto next; | |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * Want to do this before we do anything else so we can recover | |
1305 | * properly if we fail to join the transaction. | |
1306 | */ | |
1307 | trans = btrfs_start_trans_remove_block_group(fs_info, | |
b3470b5d | 1308 | block_group->start); |
e3e0520b JB |
1309 | if (IS_ERR(trans)) { |
1310 | btrfs_dec_block_group_ro(block_group); | |
1311 | ret = PTR_ERR(trans); | |
1312 | goto next; | |
1313 | } | |
1314 | ||
1315 | /* | |
1316 | * We could have pending pinned extents for this block group, | |
1317 | * just delete them, we don't care about them anymore. | |
1318 | */ | |
b3470b5d DS |
1319 | start = block_group->start; |
1320 | end = start + block_group->length - 1; | |
e3e0520b JB |
1321 | /* |
1322 | * Hold the unused_bg_unpin_mutex lock to avoid racing with | |
1323 | * btrfs_finish_extent_commit(). If we are at transaction N, | |
1324 | * another task might be running finish_extent_commit() for the | |
1325 | * previous transaction N - 1, and have seen a range belonging | |
1326 | * to the block group in freed_extents[] before we were able to | |
1327 | * clear the whole block group range from freed_extents[]. This | |
1328 | * means that task can lookup for the block group after we | |
1329 | * unpinned it from freed_extents[] and removed it, leading to | |
1330 | * a BUG_ON() at btrfs_unpin_extent_range(). | |
1331 | */ | |
1332 | mutex_lock(&fs_info->unused_bg_unpin_mutex); | |
1333 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, | |
1334 | EXTENT_DIRTY); | |
1335 | if (ret) { | |
1336 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
1337 | btrfs_dec_block_group_ro(block_group); | |
1338 | goto end_trans; | |
1339 | } | |
1340 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, | |
1341 | EXTENT_DIRTY); | |
1342 | if (ret) { | |
1343 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
1344 | btrfs_dec_block_group_ro(block_group); | |
1345 | goto end_trans; | |
1346 | } | |
1347 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
1348 | ||
b0643e59 DZ |
1349 | /* |
1350 | * At this point, the block_group is read only and should fail | |
1351 | * new allocations. However, btrfs_finish_extent_commit() can | |
1352 | * cause this block_group to be placed back on the discard | |
1353 | * lists because now the block_group isn't fully discarded. | |
1354 | * Bail here and try again later after discarding everything. | |
1355 | */ | |
1356 | spin_lock(&fs_info->discard_ctl.lock); | |
1357 | if (!list_empty(&block_group->discard_list)) { | |
1358 | spin_unlock(&fs_info->discard_ctl.lock); | |
1359 | btrfs_dec_block_group_ro(block_group); | |
1360 | btrfs_discard_queue_work(&fs_info->discard_ctl, | |
1361 | block_group); | |
1362 | goto end_trans; | |
1363 | } | |
1364 | spin_unlock(&fs_info->discard_ctl.lock); | |
1365 | ||
e3e0520b JB |
1366 | /* Reset pinned so btrfs_put_block_group doesn't complain */ |
1367 | spin_lock(&space_info->lock); | |
1368 | spin_lock(&block_group->lock); | |
1369 | ||
1370 | btrfs_space_info_update_bytes_pinned(fs_info, space_info, | |
1371 | -block_group->pinned); | |
1372 | space_info->bytes_readonly += block_group->pinned; | |
1373 | percpu_counter_add_batch(&space_info->total_bytes_pinned, | |
1374 | -block_group->pinned, | |
1375 | BTRFS_TOTAL_BYTES_PINNED_BATCH); | |
1376 | block_group->pinned = 0; | |
1377 | ||
1378 | spin_unlock(&block_group->lock); | |
1379 | spin_unlock(&space_info->lock); | |
1380 | ||
1381 | /* DISCARD can flip during remount */ | |
46b27f50 | 1382 | trimming = btrfs_test_opt(fs_info, DISCARD_SYNC); |
e3e0520b JB |
1383 | |
1384 | /* Implicit trim during transaction commit. */ | |
1385 | if (trimming) | |
1386 | btrfs_get_block_group_trimming(block_group); | |
1387 | ||
1388 | /* | |
1389 | * Btrfs_remove_chunk will abort the transaction if things go | |
1390 | * horribly wrong. | |
1391 | */ | |
b3470b5d | 1392 | ret = btrfs_remove_chunk(trans, block_group->start); |
e3e0520b JB |
1393 | |
1394 | if (ret) { | |
1395 | if (trimming) | |
1396 | btrfs_put_block_group_trimming(block_group); | |
1397 | goto end_trans; | |
1398 | } | |
1399 | ||
1400 | /* | |
1401 | * If we're not mounted with -odiscard, we can just forget | |
1402 | * about this block group. Otherwise we'll need to wait | |
1403 | * until transaction commit to do the actual discard. | |
1404 | */ | |
1405 | if (trimming) { | |
1406 | spin_lock(&fs_info->unused_bgs_lock); | |
1407 | /* | |
1408 | * A concurrent scrub might have added us to the list | |
1409 | * fs_info->unused_bgs, so use a list_move operation | |
1410 | * to add the block group to the deleted_bgs list. | |
1411 | */ | |
1412 | list_move(&block_group->bg_list, | |
1413 | &trans->transaction->deleted_bgs); | |
1414 | spin_unlock(&fs_info->unused_bgs_lock); | |
1415 | btrfs_get_block_group(block_group); | |
1416 | } | |
1417 | end_trans: | |
1418 | btrfs_end_transaction(trans); | |
1419 | next: | |
1420 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); | |
1421 | btrfs_put_block_group(block_group); | |
1422 | spin_lock(&fs_info->unused_bgs_lock); | |
1423 | } | |
1424 | spin_unlock(&fs_info->unused_bgs_lock); | |
1425 | } | |
1426 | ||
32da5386 | 1427 | void btrfs_mark_bg_unused(struct btrfs_block_group *bg) |
e3e0520b JB |
1428 | { |
1429 | struct btrfs_fs_info *fs_info = bg->fs_info; | |
1430 | ||
1431 | spin_lock(&fs_info->unused_bgs_lock); | |
1432 | if (list_empty(&bg->bg_list)) { | |
1433 | btrfs_get_block_group(bg); | |
1434 | trace_btrfs_add_unused_block_group(bg); | |
1435 | list_add_tail(&bg->bg_list, &fs_info->unused_bgs); | |
1436 | } | |
1437 | spin_unlock(&fs_info->unused_bgs_lock); | |
1438 | } | |
4358d963 JB |
1439 | |
1440 | static int find_first_block_group(struct btrfs_fs_info *fs_info, | |
1441 | struct btrfs_path *path, | |
1442 | struct btrfs_key *key) | |
1443 | { | |
1444 | struct btrfs_root *root = fs_info->extent_root; | |
1445 | int ret = 0; | |
1446 | struct btrfs_key found_key; | |
1447 | struct extent_buffer *leaf; | |
1448 | struct btrfs_block_group_item bg; | |
1449 | u64 flags; | |
1450 | int slot; | |
1451 | ||
1452 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); | |
1453 | if (ret < 0) | |
1454 | goto out; | |
1455 | ||
1456 | while (1) { | |
1457 | slot = path->slots[0]; | |
1458 | leaf = path->nodes[0]; | |
1459 | if (slot >= btrfs_header_nritems(leaf)) { | |
1460 | ret = btrfs_next_leaf(root, path); | |
1461 | if (ret == 0) | |
1462 | continue; | |
1463 | if (ret < 0) | |
1464 | goto out; | |
1465 | break; | |
1466 | } | |
1467 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1468 | ||
1469 | if (found_key.objectid >= key->objectid && | |
1470 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { | |
1471 | struct extent_map_tree *em_tree; | |
1472 | struct extent_map *em; | |
1473 | ||
1474 | em_tree = &root->fs_info->mapping_tree; | |
1475 | read_lock(&em_tree->lock); | |
1476 | em = lookup_extent_mapping(em_tree, found_key.objectid, | |
1477 | found_key.offset); | |
1478 | read_unlock(&em_tree->lock); | |
1479 | if (!em) { | |
1480 | btrfs_err(fs_info, | |
1481 | "logical %llu len %llu found bg but no related chunk", | |
1482 | found_key.objectid, found_key.offset); | |
1483 | ret = -ENOENT; | |
1484 | } else if (em->start != found_key.objectid || | |
1485 | em->len != found_key.offset) { | |
1486 | btrfs_err(fs_info, | |
1487 | "block group %llu len %llu mismatch with chunk %llu len %llu", | |
1488 | found_key.objectid, found_key.offset, | |
1489 | em->start, em->len); | |
1490 | ret = -EUCLEAN; | |
1491 | } else { | |
1492 | read_extent_buffer(leaf, &bg, | |
1493 | btrfs_item_ptr_offset(leaf, slot), | |
1494 | sizeof(bg)); | |
de0dc456 | 1495 | flags = btrfs_stack_block_group_flags(&bg) & |
4358d963 JB |
1496 | BTRFS_BLOCK_GROUP_TYPE_MASK; |
1497 | ||
1498 | if (flags != (em->map_lookup->type & | |
1499 | BTRFS_BLOCK_GROUP_TYPE_MASK)) { | |
1500 | btrfs_err(fs_info, | |
1501 | "block group %llu len %llu type flags 0x%llx mismatch with chunk type flags 0x%llx", | |
1502 | found_key.objectid, | |
1503 | found_key.offset, flags, | |
1504 | (BTRFS_BLOCK_GROUP_TYPE_MASK & | |
1505 | em->map_lookup->type)); | |
1506 | ret = -EUCLEAN; | |
1507 | } else { | |
1508 | ret = 0; | |
1509 | } | |
1510 | } | |
1511 | free_extent_map(em); | |
1512 | goto out; | |
1513 | } | |
1514 | path->slots[0]++; | |
1515 | } | |
1516 | out: | |
1517 | return ret; | |
1518 | } | |
1519 | ||
1520 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) | |
1521 | { | |
1522 | u64 extra_flags = chunk_to_extended(flags) & | |
1523 | BTRFS_EXTENDED_PROFILE_MASK; | |
1524 | ||
1525 | write_seqlock(&fs_info->profiles_lock); | |
1526 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
1527 | fs_info->avail_data_alloc_bits |= extra_flags; | |
1528 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
1529 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
1530 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
1531 | fs_info->avail_system_alloc_bits |= extra_flags; | |
1532 | write_sequnlock(&fs_info->profiles_lock); | |
1533 | } | |
1534 | ||
32da5386 | 1535 | static int exclude_super_stripes(struct btrfs_block_group *cache) |
4358d963 JB |
1536 | { |
1537 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
1538 | u64 bytenr; | |
1539 | u64 *logical; | |
1540 | int stripe_len; | |
1541 | int i, nr, ret; | |
1542 | ||
b3470b5d DS |
1543 | if (cache->start < BTRFS_SUPER_INFO_OFFSET) { |
1544 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->start; | |
4358d963 | 1545 | cache->bytes_super += stripe_len; |
b3470b5d | 1546 | ret = btrfs_add_excluded_extent(fs_info, cache->start, |
4358d963 JB |
1547 | stripe_len); |
1548 | if (ret) | |
1549 | return ret; | |
1550 | } | |
1551 | ||
1552 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { | |
1553 | bytenr = btrfs_sb_offset(i); | |
b3470b5d | 1554 | ret = btrfs_rmap_block(fs_info, cache->start, |
4358d963 JB |
1555 | bytenr, &logical, &nr, &stripe_len); |
1556 | if (ret) | |
1557 | return ret; | |
1558 | ||
1559 | while (nr--) { | |
1560 | u64 start, len; | |
1561 | ||
b3470b5d | 1562 | if (logical[nr] > cache->start + cache->length) |
4358d963 JB |
1563 | continue; |
1564 | ||
b3470b5d | 1565 | if (logical[nr] + stripe_len <= cache->start) |
4358d963 JB |
1566 | continue; |
1567 | ||
1568 | start = logical[nr]; | |
b3470b5d DS |
1569 | if (start < cache->start) { |
1570 | start = cache->start; | |
4358d963 JB |
1571 | len = (logical[nr] + stripe_len) - start; |
1572 | } else { | |
1573 | len = min_t(u64, stripe_len, | |
b3470b5d | 1574 | cache->start + cache->length - start); |
4358d963 JB |
1575 | } |
1576 | ||
1577 | cache->bytes_super += len; | |
1578 | ret = btrfs_add_excluded_extent(fs_info, start, len); | |
1579 | if (ret) { | |
1580 | kfree(logical); | |
1581 | return ret; | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | kfree(logical); | |
1586 | } | |
1587 | return 0; | |
1588 | } | |
1589 | ||
32da5386 | 1590 | static void link_block_group(struct btrfs_block_group *cache) |
4358d963 JB |
1591 | { |
1592 | struct btrfs_space_info *space_info = cache->space_info; | |
1593 | int index = btrfs_bg_flags_to_raid_index(cache->flags); | |
1594 | bool first = false; | |
1595 | ||
1596 | down_write(&space_info->groups_sem); | |
1597 | if (list_empty(&space_info->block_groups[index])) | |
1598 | first = true; | |
1599 | list_add_tail(&cache->list, &space_info->block_groups[index]); | |
1600 | up_write(&space_info->groups_sem); | |
1601 | ||
1602 | if (first) | |
1603 | btrfs_sysfs_add_block_group_type(cache); | |
1604 | } | |
1605 | ||
32da5386 | 1606 | static struct btrfs_block_group *btrfs_create_block_group_cache( |
4358d963 JB |
1607 | struct btrfs_fs_info *fs_info, u64 start, u64 size) |
1608 | { | |
32da5386 | 1609 | struct btrfs_block_group *cache; |
4358d963 JB |
1610 | |
1611 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | |
1612 | if (!cache) | |
1613 | return NULL; | |
1614 | ||
1615 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), | |
1616 | GFP_NOFS); | |
1617 | if (!cache->free_space_ctl) { | |
1618 | kfree(cache); | |
1619 | return NULL; | |
1620 | } | |
1621 | ||
b3470b5d DS |
1622 | cache->start = start; |
1623 | cache->length = size; | |
4358d963 JB |
1624 | |
1625 | cache->fs_info = fs_info; | |
1626 | cache->full_stripe_len = btrfs_full_stripe_len(fs_info, start); | |
1627 | set_free_space_tree_thresholds(cache); | |
1628 | ||
1629 | atomic_set(&cache->count, 1); | |
1630 | spin_lock_init(&cache->lock); | |
1631 | init_rwsem(&cache->data_rwsem); | |
1632 | INIT_LIST_HEAD(&cache->list); | |
1633 | INIT_LIST_HEAD(&cache->cluster_list); | |
1634 | INIT_LIST_HEAD(&cache->bg_list); | |
1635 | INIT_LIST_HEAD(&cache->ro_list); | |
b0643e59 | 1636 | INIT_LIST_HEAD(&cache->discard_list); |
4358d963 JB |
1637 | INIT_LIST_HEAD(&cache->dirty_list); |
1638 | INIT_LIST_HEAD(&cache->io_list); | |
1639 | btrfs_init_free_space_ctl(cache); | |
1640 | atomic_set(&cache->trimming, 0); | |
1641 | mutex_init(&cache->free_space_lock); | |
1642 | btrfs_init_full_stripe_locks_tree(&cache->full_stripe_locks_root); | |
1643 | ||
1644 | return cache; | |
1645 | } | |
1646 | ||
1647 | /* | |
1648 | * Iterate all chunks and verify that each of them has the corresponding block | |
1649 | * group | |
1650 | */ | |
1651 | static int check_chunk_block_group_mappings(struct btrfs_fs_info *fs_info) | |
1652 | { | |
1653 | struct extent_map_tree *map_tree = &fs_info->mapping_tree; | |
1654 | struct extent_map *em; | |
32da5386 | 1655 | struct btrfs_block_group *bg; |
4358d963 JB |
1656 | u64 start = 0; |
1657 | int ret = 0; | |
1658 | ||
1659 | while (1) { | |
1660 | read_lock(&map_tree->lock); | |
1661 | /* | |
1662 | * lookup_extent_mapping will return the first extent map | |
1663 | * intersecting the range, so setting @len to 1 is enough to | |
1664 | * get the first chunk. | |
1665 | */ | |
1666 | em = lookup_extent_mapping(map_tree, start, 1); | |
1667 | read_unlock(&map_tree->lock); | |
1668 | if (!em) | |
1669 | break; | |
1670 | ||
1671 | bg = btrfs_lookup_block_group(fs_info, em->start); | |
1672 | if (!bg) { | |
1673 | btrfs_err(fs_info, | |
1674 | "chunk start=%llu len=%llu doesn't have corresponding block group", | |
1675 | em->start, em->len); | |
1676 | ret = -EUCLEAN; | |
1677 | free_extent_map(em); | |
1678 | break; | |
1679 | } | |
b3470b5d | 1680 | if (bg->start != em->start || bg->length != em->len || |
4358d963 JB |
1681 | (bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK) != |
1682 | (em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK)) { | |
1683 | btrfs_err(fs_info, | |
1684 | "chunk start=%llu len=%llu flags=0x%llx doesn't match block group start=%llu len=%llu flags=0x%llx", | |
1685 | em->start, em->len, | |
1686 | em->map_lookup->type & BTRFS_BLOCK_GROUP_TYPE_MASK, | |
b3470b5d | 1687 | bg->start, bg->length, |
4358d963 JB |
1688 | bg->flags & BTRFS_BLOCK_GROUP_TYPE_MASK); |
1689 | ret = -EUCLEAN; | |
1690 | free_extent_map(em); | |
1691 | btrfs_put_block_group(bg); | |
1692 | break; | |
1693 | } | |
1694 | start = em->start + em->len; | |
1695 | free_extent_map(em); | |
1696 | btrfs_put_block_group(bg); | |
1697 | } | |
1698 | return ret; | |
1699 | } | |
1700 | ||
ffb9e0f0 QW |
1701 | static int read_one_block_group(struct btrfs_fs_info *info, |
1702 | struct btrfs_path *path, | |
d49a2ddb | 1703 | const struct btrfs_key *key, |
ffb9e0f0 QW |
1704 | int need_clear) |
1705 | { | |
1706 | struct extent_buffer *leaf = path->nodes[0]; | |
32da5386 | 1707 | struct btrfs_block_group *cache; |
ffb9e0f0 | 1708 | struct btrfs_space_info *space_info; |
ffb9e0f0 QW |
1709 | struct btrfs_block_group_item bgi; |
1710 | const bool mixed = btrfs_fs_incompat(info, MIXED_GROUPS); | |
1711 | int slot = path->slots[0]; | |
1712 | int ret; | |
1713 | ||
d49a2ddb | 1714 | ASSERT(key->type == BTRFS_BLOCK_GROUP_ITEM_KEY); |
ffb9e0f0 | 1715 | |
d49a2ddb | 1716 | cache = btrfs_create_block_group_cache(info, key->objectid, key->offset); |
ffb9e0f0 QW |
1717 | if (!cache) |
1718 | return -ENOMEM; | |
1719 | ||
1720 | if (need_clear) { | |
1721 | /* | |
1722 | * When we mount with old space cache, we need to | |
1723 | * set BTRFS_DC_CLEAR and set dirty flag. | |
1724 | * | |
1725 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we | |
1726 | * truncate the old free space cache inode and | |
1727 | * setup a new one. | |
1728 | * b) Setting 'dirty flag' makes sure that we flush | |
1729 | * the new space cache info onto disk. | |
1730 | */ | |
1731 | if (btrfs_test_opt(info, SPACE_CACHE)) | |
1732 | cache->disk_cache_state = BTRFS_DC_CLEAR; | |
1733 | } | |
1734 | read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), | |
1735 | sizeof(bgi)); | |
1736 | cache->used = btrfs_stack_block_group_used(&bgi); | |
1737 | cache->flags = btrfs_stack_block_group_flags(&bgi); | |
1738 | if (!mixed && ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && | |
1739 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { | |
1740 | btrfs_err(info, | |
1741 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", | |
1742 | cache->start); | |
1743 | ret = -EINVAL; | |
1744 | goto error; | |
1745 | } | |
1746 | ||
1747 | /* | |
1748 | * We need to exclude the super stripes now so that the space info has | |
1749 | * super bytes accounted for, otherwise we'll think we have more space | |
1750 | * than we actually do. | |
1751 | */ | |
1752 | ret = exclude_super_stripes(cache); | |
1753 | if (ret) { | |
1754 | /* We may have excluded something, so call this just in case. */ | |
1755 | btrfs_free_excluded_extents(cache); | |
1756 | goto error; | |
1757 | } | |
1758 | ||
1759 | /* | |
1760 | * Check for two cases, either we are full, and therefore don't need | |
1761 | * to bother with the caching work since we won't find any space, or we | |
1762 | * are empty, and we can just add all the space in and be done with it. | |
1763 | * This saves us _a_lot_ of time, particularly in the full case. | |
1764 | */ | |
d49a2ddb | 1765 | if (key->offset == cache->used) { |
ffb9e0f0 QW |
1766 | cache->last_byte_to_unpin = (u64)-1; |
1767 | cache->cached = BTRFS_CACHE_FINISHED; | |
1768 | btrfs_free_excluded_extents(cache); | |
1769 | } else if (cache->used == 0) { | |
1770 | cache->last_byte_to_unpin = (u64)-1; | |
1771 | cache->cached = BTRFS_CACHE_FINISHED; | |
d49a2ddb QW |
1772 | add_new_free_space(cache, key->objectid, |
1773 | key->objectid + key->offset); | |
ffb9e0f0 QW |
1774 | btrfs_free_excluded_extents(cache); |
1775 | } | |
1776 | ||
1777 | ret = btrfs_add_block_group_cache(info, cache); | |
1778 | if (ret) { | |
1779 | btrfs_remove_free_space_cache(cache); | |
1780 | goto error; | |
1781 | } | |
1782 | trace_btrfs_add_block_group(info, cache, 0); | |
d49a2ddb | 1783 | btrfs_update_space_info(info, cache->flags, key->offset, |
ffb9e0f0 QW |
1784 | cache->used, cache->bytes_super, &space_info); |
1785 | ||
1786 | cache->space_info = space_info; | |
1787 | ||
1788 | link_block_group(cache); | |
1789 | ||
1790 | set_avail_alloc_bits(info, cache->flags); | |
1791 | if (btrfs_chunk_readonly(info, cache->start)) { | |
1792 | inc_block_group_ro(cache, 1); | |
1793 | } else if (cache->used == 0) { | |
1794 | ASSERT(list_empty(&cache->bg_list)); | |
1795 | btrfs_mark_bg_unused(cache); | |
1796 | } | |
1797 | return 0; | |
1798 | error: | |
1799 | btrfs_put_block_group(cache); | |
1800 | return ret; | |
1801 | } | |
1802 | ||
4358d963 JB |
1803 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
1804 | { | |
1805 | struct btrfs_path *path; | |
1806 | int ret; | |
32da5386 | 1807 | struct btrfs_block_group *cache; |
4358d963 JB |
1808 | struct btrfs_space_info *space_info; |
1809 | struct btrfs_key key; | |
4358d963 JB |
1810 | int need_clear = 0; |
1811 | u64 cache_gen; | |
4358d963 JB |
1812 | |
1813 | key.objectid = 0; | |
1814 | key.offset = 0; | |
1815 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | |
1816 | path = btrfs_alloc_path(); | |
1817 | if (!path) | |
1818 | return -ENOMEM; | |
1819 | path->reada = READA_FORWARD; | |
1820 | ||
1821 | cache_gen = btrfs_super_cache_generation(info->super_copy); | |
1822 | if (btrfs_test_opt(info, SPACE_CACHE) && | |
1823 | btrfs_super_generation(info->super_copy) != cache_gen) | |
1824 | need_clear = 1; | |
1825 | if (btrfs_test_opt(info, CLEAR_CACHE)) | |
1826 | need_clear = 1; | |
1827 | ||
1828 | while (1) { | |
1829 | ret = find_first_block_group(info, path, &key); | |
1830 | if (ret > 0) | |
1831 | break; | |
1832 | if (ret != 0) | |
1833 | goto error; | |
1834 | ||
ffb9e0f0 | 1835 | btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); |
d49a2ddb | 1836 | ret = read_one_block_group(info, path, &key, need_clear); |
ffb9e0f0 | 1837 | if (ret < 0) |
4358d963 | 1838 | goto error; |
ffb9e0f0 QW |
1839 | key.objectid += key.offset; |
1840 | key.offset = 0; | |
4358d963 | 1841 | btrfs_release_path(path); |
4358d963 JB |
1842 | } |
1843 | ||
1844 | list_for_each_entry_rcu(space_info, &info->space_info, list) { | |
1845 | if (!(btrfs_get_alloc_profile(info, space_info->flags) & | |
1846 | (BTRFS_BLOCK_GROUP_RAID10 | | |
1847 | BTRFS_BLOCK_GROUP_RAID1_MASK | | |
1848 | BTRFS_BLOCK_GROUP_RAID56_MASK | | |
1849 | BTRFS_BLOCK_GROUP_DUP))) | |
1850 | continue; | |
1851 | /* | |
1852 | * Avoid allocating from un-mirrored block group if there are | |
1853 | * mirrored block groups. | |
1854 | */ | |
1855 | list_for_each_entry(cache, | |
1856 | &space_info->block_groups[BTRFS_RAID_RAID0], | |
1857 | list) | |
e11c0406 | 1858 | inc_block_group_ro(cache, 1); |
4358d963 JB |
1859 | list_for_each_entry(cache, |
1860 | &space_info->block_groups[BTRFS_RAID_SINGLE], | |
1861 | list) | |
e11c0406 | 1862 | inc_block_group_ro(cache, 1); |
4358d963 JB |
1863 | } |
1864 | ||
1865 | btrfs_init_global_block_rsv(info); | |
1866 | ret = check_chunk_block_group_mappings(info); | |
1867 | error: | |
1868 | btrfs_free_path(path); | |
1869 | return ret; | |
1870 | } | |
1871 | ||
1872 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans) | |
1873 | { | |
1874 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
32da5386 | 1875 | struct btrfs_block_group *block_group; |
4358d963 JB |
1876 | struct btrfs_root *extent_root = fs_info->extent_root; |
1877 | struct btrfs_block_group_item item; | |
1878 | struct btrfs_key key; | |
1879 | int ret = 0; | |
1880 | ||
1881 | if (!trans->can_flush_pending_bgs) | |
1882 | return; | |
1883 | ||
1884 | while (!list_empty(&trans->new_bgs)) { | |
1885 | block_group = list_first_entry(&trans->new_bgs, | |
32da5386 | 1886 | struct btrfs_block_group, |
4358d963 JB |
1887 | bg_list); |
1888 | if (ret) | |
1889 | goto next; | |
1890 | ||
1891 | spin_lock(&block_group->lock); | |
de0dc456 DS |
1892 | btrfs_set_stack_block_group_used(&item, block_group->used); |
1893 | btrfs_set_stack_block_group_chunk_objectid(&item, | |
3d976388 | 1894 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
de0dc456 | 1895 | btrfs_set_stack_block_group_flags(&item, block_group->flags); |
b3470b5d DS |
1896 | key.objectid = block_group->start; |
1897 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | |
1898 | key.offset = block_group->length; | |
4358d963 JB |
1899 | spin_unlock(&block_group->lock); |
1900 | ||
1901 | ret = btrfs_insert_item(trans, extent_root, &key, &item, | |
1902 | sizeof(item)); | |
1903 | if (ret) | |
1904 | btrfs_abort_transaction(trans, ret); | |
1905 | ret = btrfs_finish_chunk_alloc(trans, key.objectid, key.offset); | |
1906 | if (ret) | |
1907 | btrfs_abort_transaction(trans, ret); | |
1908 | add_block_group_free_space(trans, block_group); | |
1909 | /* Already aborted the transaction if it failed. */ | |
1910 | next: | |
1911 | btrfs_delayed_refs_rsv_release(fs_info, 1); | |
1912 | list_del_init(&block_group->bg_list); | |
1913 | } | |
1914 | btrfs_trans_release_chunk_metadata(trans); | |
1915 | } | |
1916 | ||
1917 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, u64 bytes_used, | |
1918 | u64 type, u64 chunk_offset, u64 size) | |
1919 | { | |
1920 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
32da5386 | 1921 | struct btrfs_block_group *cache; |
4358d963 JB |
1922 | int ret; |
1923 | ||
1924 | btrfs_set_log_full_commit(trans); | |
1925 | ||
1926 | cache = btrfs_create_block_group_cache(fs_info, chunk_offset, size); | |
1927 | if (!cache) | |
1928 | return -ENOMEM; | |
1929 | ||
bf38be65 | 1930 | cache->used = bytes_used; |
4358d963 JB |
1931 | cache->flags = type; |
1932 | cache->last_byte_to_unpin = (u64)-1; | |
1933 | cache->cached = BTRFS_CACHE_FINISHED; | |
1934 | cache->needs_free_space = 1; | |
1935 | ret = exclude_super_stripes(cache); | |
1936 | if (ret) { | |
1937 | /* We may have excluded something, so call this just in case */ | |
1938 | btrfs_free_excluded_extents(cache); | |
1939 | btrfs_put_block_group(cache); | |
1940 | return ret; | |
1941 | } | |
1942 | ||
1943 | add_new_free_space(cache, chunk_offset, chunk_offset + size); | |
1944 | ||
1945 | btrfs_free_excluded_extents(cache); | |
1946 | ||
1947 | #ifdef CONFIG_BTRFS_DEBUG | |
1948 | if (btrfs_should_fragment_free_space(cache)) { | |
1949 | u64 new_bytes_used = size - bytes_used; | |
1950 | ||
1951 | bytes_used += new_bytes_used >> 1; | |
e11c0406 | 1952 | fragment_free_space(cache); |
4358d963 JB |
1953 | } |
1954 | #endif | |
1955 | /* | |
1956 | * Ensure the corresponding space_info object is created and | |
1957 | * assigned to our block group. We want our bg to be added to the rbtree | |
1958 | * with its ->space_info set. | |
1959 | */ | |
1960 | cache->space_info = btrfs_find_space_info(fs_info, cache->flags); | |
1961 | ASSERT(cache->space_info); | |
1962 | ||
1963 | ret = btrfs_add_block_group_cache(fs_info, cache); | |
1964 | if (ret) { | |
1965 | btrfs_remove_free_space_cache(cache); | |
1966 | btrfs_put_block_group(cache); | |
1967 | return ret; | |
1968 | } | |
1969 | ||
1970 | /* | |
1971 | * Now that our block group has its ->space_info set and is inserted in | |
1972 | * the rbtree, update the space info's counters. | |
1973 | */ | |
1974 | trace_btrfs_add_block_group(fs_info, cache, 1); | |
1975 | btrfs_update_space_info(fs_info, cache->flags, size, bytes_used, | |
1976 | cache->bytes_super, &cache->space_info); | |
1977 | btrfs_update_global_block_rsv(fs_info); | |
1978 | ||
1979 | link_block_group(cache); | |
1980 | ||
1981 | list_add_tail(&cache->bg_list, &trans->new_bgs); | |
1982 | trans->delayed_ref_updates++; | |
1983 | btrfs_update_delayed_refs_rsv(trans); | |
1984 | ||
1985 | set_avail_alloc_bits(fs_info, type); | |
1986 | return 0; | |
1987 | } | |
26ce2095 JB |
1988 | |
1989 | static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags) | |
1990 | { | |
1991 | u64 num_devices; | |
1992 | u64 stripped; | |
1993 | ||
1994 | /* | |
1995 | * if restripe for this chunk_type is on pick target profile and | |
1996 | * return, otherwise do the usual balance | |
1997 | */ | |
e11c0406 | 1998 | stripped = get_restripe_target(fs_info, flags); |
26ce2095 JB |
1999 | if (stripped) |
2000 | return extended_to_chunk(stripped); | |
2001 | ||
2002 | num_devices = fs_info->fs_devices->rw_devices; | |
2003 | ||
2004 | stripped = BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID56_MASK | | |
2005 | BTRFS_BLOCK_GROUP_RAID1_MASK | BTRFS_BLOCK_GROUP_RAID10; | |
2006 | ||
2007 | if (num_devices == 1) { | |
2008 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
2009 | stripped = flags & ~stripped; | |
2010 | ||
2011 | /* turn raid0 into single device chunks */ | |
2012 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
2013 | return stripped; | |
2014 | ||
2015 | /* turn mirroring into duplication */ | |
2016 | if (flags & (BTRFS_BLOCK_GROUP_RAID1_MASK | | |
2017 | BTRFS_BLOCK_GROUP_RAID10)) | |
2018 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
2019 | } else { | |
2020 | /* they already had raid on here, just return */ | |
2021 | if (flags & stripped) | |
2022 | return flags; | |
2023 | ||
2024 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
2025 | stripped = flags & ~stripped; | |
2026 | ||
2027 | /* switch duplicated blocks with raid1 */ | |
2028 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
2029 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
2030 | ||
2031 | /* this is drive concat, leave it alone */ | |
2032 | } | |
2033 | ||
2034 | return flags; | |
2035 | } | |
2036 | ||
b12de528 QW |
2037 | /* |
2038 | * Mark one block group RO, can be called several times for the same block | |
2039 | * group. | |
2040 | * | |
2041 | * @cache: the destination block group | |
2042 | * @do_chunk_alloc: whether need to do chunk pre-allocation, this is to | |
2043 | * ensure we still have some free space after marking this | |
2044 | * block group RO. | |
2045 | */ | |
2046 | int btrfs_inc_block_group_ro(struct btrfs_block_group *cache, | |
2047 | bool do_chunk_alloc) | |
26ce2095 JB |
2048 | { |
2049 | struct btrfs_fs_info *fs_info = cache->fs_info; | |
2050 | struct btrfs_trans_handle *trans; | |
2051 | u64 alloc_flags; | |
2052 | int ret; | |
2053 | ||
2054 | again: | |
2055 | trans = btrfs_join_transaction(fs_info->extent_root); | |
2056 | if (IS_ERR(trans)) | |
2057 | return PTR_ERR(trans); | |
2058 | ||
2059 | /* | |
2060 | * we're not allowed to set block groups readonly after the dirty | |
2061 | * block groups cache has started writing. If it already started, | |
2062 | * back off and let this transaction commit | |
2063 | */ | |
2064 | mutex_lock(&fs_info->ro_block_group_mutex); | |
2065 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { | |
2066 | u64 transid = trans->transid; | |
2067 | ||
2068 | mutex_unlock(&fs_info->ro_block_group_mutex); | |
2069 | btrfs_end_transaction(trans); | |
2070 | ||
2071 | ret = btrfs_wait_for_commit(fs_info, transid); | |
2072 | if (ret) | |
2073 | return ret; | |
2074 | goto again; | |
2075 | } | |
2076 | ||
b12de528 | 2077 | if (do_chunk_alloc) { |
26ce2095 | 2078 | /* |
b12de528 QW |
2079 | * If we are changing raid levels, try to allocate a |
2080 | * corresponding block group with the new raid level. | |
26ce2095 | 2081 | */ |
b12de528 QW |
2082 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
2083 | if (alloc_flags != cache->flags) { | |
2084 | ret = btrfs_chunk_alloc(trans, alloc_flags, | |
2085 | CHUNK_ALLOC_FORCE); | |
2086 | /* | |
2087 | * ENOSPC is allowed here, we may have enough space | |
2088 | * already allocated at the new raid level to carry on | |
2089 | */ | |
2090 | if (ret == -ENOSPC) | |
2091 | ret = 0; | |
2092 | if (ret < 0) | |
2093 | goto out; | |
2094 | } | |
26ce2095 JB |
2095 | } |
2096 | ||
b12de528 QW |
2097 | ret = inc_block_group_ro(cache, !do_chunk_alloc); |
2098 | if (!do_chunk_alloc) | |
2099 | goto unlock_out; | |
26ce2095 JB |
2100 | if (!ret) |
2101 | goto out; | |
2102 | alloc_flags = btrfs_get_alloc_profile(fs_info, cache->space_info->flags); | |
2103 | ret = btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); | |
2104 | if (ret < 0) | |
2105 | goto out; | |
e11c0406 | 2106 | ret = inc_block_group_ro(cache, 0); |
26ce2095 JB |
2107 | out: |
2108 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { | |
2109 | alloc_flags = update_block_group_flags(fs_info, cache->flags); | |
2110 | mutex_lock(&fs_info->chunk_mutex); | |
2111 | check_system_chunk(trans, alloc_flags); | |
2112 | mutex_unlock(&fs_info->chunk_mutex); | |
2113 | } | |
b12de528 | 2114 | unlock_out: |
26ce2095 JB |
2115 | mutex_unlock(&fs_info->ro_block_group_mutex); |
2116 | ||
2117 | btrfs_end_transaction(trans); | |
2118 | return ret; | |
2119 | } | |
2120 | ||
32da5386 | 2121 | void btrfs_dec_block_group_ro(struct btrfs_block_group *cache) |
26ce2095 JB |
2122 | { |
2123 | struct btrfs_space_info *sinfo = cache->space_info; | |
2124 | u64 num_bytes; | |
2125 | ||
2126 | BUG_ON(!cache->ro); | |
2127 | ||
2128 | spin_lock(&sinfo->lock); | |
2129 | spin_lock(&cache->lock); | |
2130 | if (!--cache->ro) { | |
b3470b5d | 2131 | num_bytes = cache->length - cache->reserved - |
bf38be65 | 2132 | cache->pinned - cache->bytes_super - cache->used; |
26ce2095 JB |
2133 | sinfo->bytes_readonly -= num_bytes; |
2134 | list_del_init(&cache->ro_list); | |
2135 | } | |
2136 | spin_unlock(&cache->lock); | |
2137 | spin_unlock(&sinfo->lock); | |
2138 | } | |
77745c05 JB |
2139 | |
2140 | static int write_one_cache_group(struct btrfs_trans_handle *trans, | |
2141 | struct btrfs_path *path, | |
32da5386 | 2142 | struct btrfs_block_group *cache) |
77745c05 JB |
2143 | { |
2144 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
2145 | int ret; | |
2146 | struct btrfs_root *extent_root = fs_info->extent_root; | |
2147 | unsigned long bi; | |
2148 | struct extent_buffer *leaf; | |
bf38be65 | 2149 | struct btrfs_block_group_item bgi; |
b3470b5d DS |
2150 | struct btrfs_key key; |
2151 | ||
2152 | key.objectid = cache->start; | |
2153 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | |
2154 | key.offset = cache->length; | |
77745c05 | 2155 | |
b3470b5d | 2156 | ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1); |
77745c05 JB |
2157 | if (ret) { |
2158 | if (ret > 0) | |
2159 | ret = -ENOENT; | |
2160 | goto fail; | |
2161 | } | |
2162 | ||
2163 | leaf = path->nodes[0]; | |
2164 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
de0dc456 DS |
2165 | btrfs_set_stack_block_group_used(&bgi, cache->used); |
2166 | btrfs_set_stack_block_group_chunk_objectid(&bgi, | |
3d976388 | 2167 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
de0dc456 | 2168 | btrfs_set_stack_block_group_flags(&bgi, cache->flags); |
bf38be65 | 2169 | write_extent_buffer(leaf, &bgi, bi, sizeof(bgi)); |
77745c05 JB |
2170 | btrfs_mark_buffer_dirty(leaf); |
2171 | fail: | |
2172 | btrfs_release_path(path); | |
2173 | return ret; | |
2174 | ||
2175 | } | |
2176 | ||
32da5386 | 2177 | static int cache_save_setup(struct btrfs_block_group *block_group, |
77745c05 JB |
2178 | struct btrfs_trans_handle *trans, |
2179 | struct btrfs_path *path) | |
2180 | { | |
2181 | struct btrfs_fs_info *fs_info = block_group->fs_info; | |
2182 | struct btrfs_root *root = fs_info->tree_root; | |
2183 | struct inode *inode = NULL; | |
2184 | struct extent_changeset *data_reserved = NULL; | |
2185 | u64 alloc_hint = 0; | |
2186 | int dcs = BTRFS_DC_ERROR; | |
2187 | u64 num_pages = 0; | |
2188 | int retries = 0; | |
2189 | int ret = 0; | |
2190 | ||
2191 | /* | |
2192 | * If this block group is smaller than 100 megs don't bother caching the | |
2193 | * block group. | |
2194 | */ | |
b3470b5d | 2195 | if (block_group->length < (100 * SZ_1M)) { |
77745c05 JB |
2196 | spin_lock(&block_group->lock); |
2197 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; | |
2198 | spin_unlock(&block_group->lock); | |
2199 | return 0; | |
2200 | } | |
2201 | ||
2202 | if (trans->aborted) | |
2203 | return 0; | |
2204 | again: | |
2205 | inode = lookup_free_space_inode(block_group, path); | |
2206 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { | |
2207 | ret = PTR_ERR(inode); | |
2208 | btrfs_release_path(path); | |
2209 | goto out; | |
2210 | } | |
2211 | ||
2212 | if (IS_ERR(inode)) { | |
2213 | BUG_ON(retries); | |
2214 | retries++; | |
2215 | ||
2216 | if (block_group->ro) | |
2217 | goto out_free; | |
2218 | ||
2219 | ret = create_free_space_inode(trans, block_group, path); | |
2220 | if (ret) | |
2221 | goto out_free; | |
2222 | goto again; | |
2223 | } | |
2224 | ||
2225 | /* | |
2226 | * We want to set the generation to 0, that way if anything goes wrong | |
2227 | * from here on out we know not to trust this cache when we load up next | |
2228 | * time. | |
2229 | */ | |
2230 | BTRFS_I(inode)->generation = 0; | |
2231 | ret = btrfs_update_inode(trans, root, inode); | |
2232 | if (ret) { | |
2233 | /* | |
2234 | * So theoretically we could recover from this, simply set the | |
2235 | * super cache generation to 0 so we know to invalidate the | |
2236 | * cache, but then we'd have to keep track of the block groups | |
2237 | * that fail this way so we know we _have_ to reset this cache | |
2238 | * before the next commit or risk reading stale cache. So to | |
2239 | * limit our exposure to horrible edge cases lets just abort the | |
2240 | * transaction, this only happens in really bad situations | |
2241 | * anyway. | |
2242 | */ | |
2243 | btrfs_abort_transaction(trans, ret); | |
2244 | goto out_put; | |
2245 | } | |
2246 | WARN_ON(ret); | |
2247 | ||
2248 | /* We've already setup this transaction, go ahead and exit */ | |
2249 | if (block_group->cache_generation == trans->transid && | |
2250 | i_size_read(inode)) { | |
2251 | dcs = BTRFS_DC_SETUP; | |
2252 | goto out_put; | |
2253 | } | |
2254 | ||
2255 | if (i_size_read(inode) > 0) { | |
2256 | ret = btrfs_check_trunc_cache_free_space(fs_info, | |
2257 | &fs_info->global_block_rsv); | |
2258 | if (ret) | |
2259 | goto out_put; | |
2260 | ||
2261 | ret = btrfs_truncate_free_space_cache(trans, NULL, inode); | |
2262 | if (ret) | |
2263 | goto out_put; | |
2264 | } | |
2265 | ||
2266 | spin_lock(&block_group->lock); | |
2267 | if (block_group->cached != BTRFS_CACHE_FINISHED || | |
2268 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { | |
2269 | /* | |
2270 | * don't bother trying to write stuff out _if_ | |
2271 | * a) we're not cached, | |
2272 | * b) we're with nospace_cache mount option, | |
2273 | * c) we're with v2 space_cache (FREE_SPACE_TREE). | |
2274 | */ | |
2275 | dcs = BTRFS_DC_WRITTEN; | |
2276 | spin_unlock(&block_group->lock); | |
2277 | goto out_put; | |
2278 | } | |
2279 | spin_unlock(&block_group->lock); | |
2280 | ||
2281 | /* | |
2282 | * We hit an ENOSPC when setting up the cache in this transaction, just | |
2283 | * skip doing the setup, we've already cleared the cache so we're safe. | |
2284 | */ | |
2285 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { | |
2286 | ret = -ENOSPC; | |
2287 | goto out_put; | |
2288 | } | |
2289 | ||
2290 | /* | |
2291 | * Try to preallocate enough space based on how big the block group is. | |
2292 | * Keep in mind this has to include any pinned space which could end up | |
2293 | * taking up quite a bit since it's not folded into the other space | |
2294 | * cache. | |
2295 | */ | |
b3470b5d | 2296 | num_pages = div_u64(block_group->length, SZ_256M); |
77745c05 JB |
2297 | if (!num_pages) |
2298 | num_pages = 1; | |
2299 | ||
2300 | num_pages *= 16; | |
2301 | num_pages *= PAGE_SIZE; | |
2302 | ||
2303 | ret = btrfs_check_data_free_space(inode, &data_reserved, 0, num_pages); | |
2304 | if (ret) | |
2305 | goto out_put; | |
2306 | ||
2307 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, | |
2308 | num_pages, num_pages, | |
2309 | &alloc_hint); | |
2310 | /* | |
2311 | * Our cache requires contiguous chunks so that we don't modify a bunch | |
2312 | * of metadata or split extents when writing the cache out, which means | |
2313 | * we can enospc if we are heavily fragmented in addition to just normal | |
2314 | * out of space conditions. So if we hit this just skip setting up any | |
2315 | * other block groups for this transaction, maybe we'll unpin enough | |
2316 | * space the next time around. | |
2317 | */ | |
2318 | if (!ret) | |
2319 | dcs = BTRFS_DC_SETUP; | |
2320 | else if (ret == -ENOSPC) | |
2321 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); | |
2322 | ||
2323 | out_put: | |
2324 | iput(inode); | |
2325 | out_free: | |
2326 | btrfs_release_path(path); | |
2327 | out: | |
2328 | spin_lock(&block_group->lock); | |
2329 | if (!ret && dcs == BTRFS_DC_SETUP) | |
2330 | block_group->cache_generation = trans->transid; | |
2331 | block_group->disk_cache_state = dcs; | |
2332 | spin_unlock(&block_group->lock); | |
2333 | ||
2334 | extent_changeset_free(data_reserved); | |
2335 | return ret; | |
2336 | } | |
2337 | ||
2338 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans) | |
2339 | { | |
2340 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
32da5386 | 2341 | struct btrfs_block_group *cache, *tmp; |
77745c05 JB |
2342 | struct btrfs_transaction *cur_trans = trans->transaction; |
2343 | struct btrfs_path *path; | |
2344 | ||
2345 | if (list_empty(&cur_trans->dirty_bgs) || | |
2346 | !btrfs_test_opt(fs_info, SPACE_CACHE)) | |
2347 | return 0; | |
2348 | ||
2349 | path = btrfs_alloc_path(); | |
2350 | if (!path) | |
2351 | return -ENOMEM; | |
2352 | ||
2353 | /* Could add new block groups, use _safe just in case */ | |
2354 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, | |
2355 | dirty_list) { | |
2356 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) | |
2357 | cache_save_setup(cache, trans, path); | |
2358 | } | |
2359 | ||
2360 | btrfs_free_path(path); | |
2361 | return 0; | |
2362 | } | |
2363 | ||
2364 | /* | |
2365 | * Transaction commit does final block group cache writeback during a critical | |
2366 | * section where nothing is allowed to change the FS. This is required in | |
2367 | * order for the cache to actually match the block group, but can introduce a | |
2368 | * lot of latency into the commit. | |
2369 | * | |
2370 | * So, btrfs_start_dirty_block_groups is here to kick off block group cache IO. | |
2371 | * There's a chance we'll have to redo some of it if the block group changes | |
2372 | * again during the commit, but it greatly reduces the commit latency by | |
2373 | * getting rid of the easy block groups while we're still allowing others to | |
2374 | * join the commit. | |
2375 | */ | |
2376 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans) | |
2377 | { | |
2378 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
32da5386 | 2379 | struct btrfs_block_group *cache; |
77745c05 JB |
2380 | struct btrfs_transaction *cur_trans = trans->transaction; |
2381 | int ret = 0; | |
2382 | int should_put; | |
2383 | struct btrfs_path *path = NULL; | |
2384 | LIST_HEAD(dirty); | |
2385 | struct list_head *io = &cur_trans->io_bgs; | |
2386 | int num_started = 0; | |
2387 | int loops = 0; | |
2388 | ||
2389 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2390 | if (list_empty(&cur_trans->dirty_bgs)) { | |
2391 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2392 | return 0; | |
2393 | } | |
2394 | list_splice_init(&cur_trans->dirty_bgs, &dirty); | |
2395 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2396 | ||
2397 | again: | |
2398 | /* Make sure all the block groups on our dirty list actually exist */ | |
2399 | btrfs_create_pending_block_groups(trans); | |
2400 | ||
2401 | if (!path) { | |
2402 | path = btrfs_alloc_path(); | |
2403 | if (!path) | |
2404 | return -ENOMEM; | |
2405 | } | |
2406 | ||
2407 | /* | |
2408 | * cache_write_mutex is here only to save us from balance or automatic | |
2409 | * removal of empty block groups deleting this block group while we are | |
2410 | * writing out the cache | |
2411 | */ | |
2412 | mutex_lock(&trans->transaction->cache_write_mutex); | |
2413 | while (!list_empty(&dirty)) { | |
2414 | bool drop_reserve = true; | |
2415 | ||
32da5386 | 2416 | cache = list_first_entry(&dirty, struct btrfs_block_group, |
77745c05 JB |
2417 | dirty_list); |
2418 | /* | |
2419 | * This can happen if something re-dirties a block group that | |
2420 | * is already under IO. Just wait for it to finish and then do | |
2421 | * it all again | |
2422 | */ | |
2423 | if (!list_empty(&cache->io_list)) { | |
2424 | list_del_init(&cache->io_list); | |
2425 | btrfs_wait_cache_io(trans, cache, path); | |
2426 | btrfs_put_block_group(cache); | |
2427 | } | |
2428 | ||
2429 | ||
2430 | /* | |
2431 | * btrfs_wait_cache_io uses the cache->dirty_list to decide if | |
2432 | * it should update the cache_state. Don't delete until after | |
2433 | * we wait. | |
2434 | * | |
2435 | * Since we're not running in the commit critical section | |
2436 | * we need the dirty_bgs_lock to protect from update_block_group | |
2437 | */ | |
2438 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2439 | list_del_init(&cache->dirty_list); | |
2440 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2441 | ||
2442 | should_put = 1; | |
2443 | ||
2444 | cache_save_setup(cache, trans, path); | |
2445 | ||
2446 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { | |
2447 | cache->io_ctl.inode = NULL; | |
2448 | ret = btrfs_write_out_cache(trans, cache, path); | |
2449 | if (ret == 0 && cache->io_ctl.inode) { | |
2450 | num_started++; | |
2451 | should_put = 0; | |
2452 | ||
2453 | /* | |
2454 | * The cache_write_mutex is protecting the | |
2455 | * io_list, also refer to the definition of | |
2456 | * btrfs_transaction::io_bgs for more details | |
2457 | */ | |
2458 | list_add_tail(&cache->io_list, io); | |
2459 | } else { | |
2460 | /* | |
2461 | * If we failed to write the cache, the | |
2462 | * generation will be bad and life goes on | |
2463 | */ | |
2464 | ret = 0; | |
2465 | } | |
2466 | } | |
2467 | if (!ret) { | |
2468 | ret = write_one_cache_group(trans, path, cache); | |
2469 | /* | |
2470 | * Our block group might still be attached to the list | |
2471 | * of new block groups in the transaction handle of some | |
2472 | * other task (struct btrfs_trans_handle->new_bgs). This | |
2473 | * means its block group item isn't yet in the extent | |
2474 | * tree. If this happens ignore the error, as we will | |
2475 | * try again later in the critical section of the | |
2476 | * transaction commit. | |
2477 | */ | |
2478 | if (ret == -ENOENT) { | |
2479 | ret = 0; | |
2480 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2481 | if (list_empty(&cache->dirty_list)) { | |
2482 | list_add_tail(&cache->dirty_list, | |
2483 | &cur_trans->dirty_bgs); | |
2484 | btrfs_get_block_group(cache); | |
2485 | drop_reserve = false; | |
2486 | } | |
2487 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2488 | } else if (ret) { | |
2489 | btrfs_abort_transaction(trans, ret); | |
2490 | } | |
2491 | } | |
2492 | ||
2493 | /* If it's not on the io list, we need to put the block group */ | |
2494 | if (should_put) | |
2495 | btrfs_put_block_group(cache); | |
2496 | if (drop_reserve) | |
2497 | btrfs_delayed_refs_rsv_release(fs_info, 1); | |
2498 | ||
2499 | if (ret) | |
2500 | break; | |
2501 | ||
2502 | /* | |
2503 | * Avoid blocking other tasks for too long. It might even save | |
2504 | * us from writing caches for block groups that are going to be | |
2505 | * removed. | |
2506 | */ | |
2507 | mutex_unlock(&trans->transaction->cache_write_mutex); | |
2508 | mutex_lock(&trans->transaction->cache_write_mutex); | |
2509 | } | |
2510 | mutex_unlock(&trans->transaction->cache_write_mutex); | |
2511 | ||
2512 | /* | |
2513 | * Go through delayed refs for all the stuff we've just kicked off | |
2514 | * and then loop back (just once) | |
2515 | */ | |
2516 | ret = btrfs_run_delayed_refs(trans, 0); | |
2517 | if (!ret && loops == 0) { | |
2518 | loops++; | |
2519 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2520 | list_splice_init(&cur_trans->dirty_bgs, &dirty); | |
2521 | /* | |
2522 | * dirty_bgs_lock protects us from concurrent block group | |
2523 | * deletes too (not just cache_write_mutex). | |
2524 | */ | |
2525 | if (!list_empty(&dirty)) { | |
2526 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2527 | goto again; | |
2528 | } | |
2529 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2530 | } else if (ret < 0) { | |
2531 | btrfs_cleanup_dirty_bgs(cur_trans, fs_info); | |
2532 | } | |
2533 | ||
2534 | btrfs_free_path(path); | |
2535 | return ret; | |
2536 | } | |
2537 | ||
2538 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans) | |
2539 | { | |
2540 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
32da5386 | 2541 | struct btrfs_block_group *cache; |
77745c05 JB |
2542 | struct btrfs_transaction *cur_trans = trans->transaction; |
2543 | int ret = 0; | |
2544 | int should_put; | |
2545 | struct btrfs_path *path; | |
2546 | struct list_head *io = &cur_trans->io_bgs; | |
2547 | int num_started = 0; | |
2548 | ||
2549 | path = btrfs_alloc_path(); | |
2550 | if (!path) | |
2551 | return -ENOMEM; | |
2552 | ||
2553 | /* | |
2554 | * Even though we are in the critical section of the transaction commit, | |
2555 | * we can still have concurrent tasks adding elements to this | |
2556 | * transaction's list of dirty block groups. These tasks correspond to | |
2557 | * endio free space workers started when writeback finishes for a | |
2558 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can | |
2559 | * allocate new block groups as a result of COWing nodes of the root | |
2560 | * tree when updating the free space inode. The writeback for the space | |
2561 | * caches is triggered by an earlier call to | |
2562 | * btrfs_start_dirty_block_groups() and iterations of the following | |
2563 | * loop. | |
2564 | * Also we want to do the cache_save_setup first and then run the | |
2565 | * delayed refs to make sure we have the best chance at doing this all | |
2566 | * in one shot. | |
2567 | */ | |
2568 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2569 | while (!list_empty(&cur_trans->dirty_bgs)) { | |
2570 | cache = list_first_entry(&cur_trans->dirty_bgs, | |
32da5386 | 2571 | struct btrfs_block_group, |
77745c05 JB |
2572 | dirty_list); |
2573 | ||
2574 | /* | |
2575 | * This can happen if cache_save_setup re-dirties a block group | |
2576 | * that is already under IO. Just wait for it to finish and | |
2577 | * then do it all again | |
2578 | */ | |
2579 | if (!list_empty(&cache->io_list)) { | |
2580 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2581 | list_del_init(&cache->io_list); | |
2582 | btrfs_wait_cache_io(trans, cache, path); | |
2583 | btrfs_put_block_group(cache); | |
2584 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2585 | } | |
2586 | ||
2587 | /* | |
2588 | * Don't remove from the dirty list until after we've waited on | |
2589 | * any pending IO | |
2590 | */ | |
2591 | list_del_init(&cache->dirty_list); | |
2592 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2593 | should_put = 1; | |
2594 | ||
2595 | cache_save_setup(cache, trans, path); | |
2596 | ||
2597 | if (!ret) | |
2598 | ret = btrfs_run_delayed_refs(trans, | |
2599 | (unsigned long) -1); | |
2600 | ||
2601 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { | |
2602 | cache->io_ctl.inode = NULL; | |
2603 | ret = btrfs_write_out_cache(trans, cache, path); | |
2604 | if (ret == 0 && cache->io_ctl.inode) { | |
2605 | num_started++; | |
2606 | should_put = 0; | |
2607 | list_add_tail(&cache->io_list, io); | |
2608 | } else { | |
2609 | /* | |
2610 | * If we failed to write the cache, the | |
2611 | * generation will be bad and life goes on | |
2612 | */ | |
2613 | ret = 0; | |
2614 | } | |
2615 | } | |
2616 | if (!ret) { | |
2617 | ret = write_one_cache_group(trans, path, cache); | |
2618 | /* | |
2619 | * One of the free space endio workers might have | |
2620 | * created a new block group while updating a free space | |
2621 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) | |
2622 | * and hasn't released its transaction handle yet, in | |
2623 | * which case the new block group is still attached to | |
2624 | * its transaction handle and its creation has not | |
2625 | * finished yet (no block group item in the extent tree | |
2626 | * yet, etc). If this is the case, wait for all free | |
2627 | * space endio workers to finish and retry. This is a | |
2628 | * a very rare case so no need for a more efficient and | |
2629 | * complex approach. | |
2630 | */ | |
2631 | if (ret == -ENOENT) { | |
2632 | wait_event(cur_trans->writer_wait, | |
2633 | atomic_read(&cur_trans->num_writers) == 1); | |
2634 | ret = write_one_cache_group(trans, path, cache); | |
2635 | } | |
2636 | if (ret) | |
2637 | btrfs_abort_transaction(trans, ret); | |
2638 | } | |
2639 | ||
2640 | /* If its not on the io list, we need to put the block group */ | |
2641 | if (should_put) | |
2642 | btrfs_put_block_group(cache); | |
2643 | btrfs_delayed_refs_rsv_release(fs_info, 1); | |
2644 | spin_lock(&cur_trans->dirty_bgs_lock); | |
2645 | } | |
2646 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
2647 | ||
2648 | /* | |
2649 | * Refer to the definition of io_bgs member for details why it's safe | |
2650 | * to use it without any locking | |
2651 | */ | |
2652 | while (!list_empty(io)) { | |
32da5386 | 2653 | cache = list_first_entry(io, struct btrfs_block_group, |
77745c05 JB |
2654 | io_list); |
2655 | list_del_init(&cache->io_list); | |
2656 | btrfs_wait_cache_io(trans, cache, path); | |
2657 | btrfs_put_block_group(cache); | |
2658 | } | |
2659 | ||
2660 | btrfs_free_path(path); | |
2661 | return ret; | |
2662 | } | |
606d1bf1 JB |
2663 | |
2664 | int btrfs_update_block_group(struct btrfs_trans_handle *trans, | |
2665 | u64 bytenr, u64 num_bytes, int alloc) | |
2666 | { | |
2667 | struct btrfs_fs_info *info = trans->fs_info; | |
32da5386 | 2668 | struct btrfs_block_group *cache = NULL; |
606d1bf1 JB |
2669 | u64 total = num_bytes; |
2670 | u64 old_val; | |
2671 | u64 byte_in_group; | |
2672 | int factor; | |
2673 | int ret = 0; | |
2674 | ||
2675 | /* Block accounting for super block */ | |
2676 | spin_lock(&info->delalloc_root_lock); | |
2677 | old_val = btrfs_super_bytes_used(info->super_copy); | |
2678 | if (alloc) | |
2679 | old_val += num_bytes; | |
2680 | else | |
2681 | old_val -= num_bytes; | |
2682 | btrfs_set_super_bytes_used(info->super_copy, old_val); | |
2683 | spin_unlock(&info->delalloc_root_lock); | |
2684 | ||
2685 | while (total) { | |
2686 | cache = btrfs_lookup_block_group(info, bytenr); | |
2687 | if (!cache) { | |
2688 | ret = -ENOENT; | |
2689 | break; | |
2690 | } | |
2691 | factor = btrfs_bg_type_to_factor(cache->flags); | |
2692 | ||
2693 | /* | |
2694 | * If this block group has free space cache written out, we | |
2695 | * need to make sure to load it if we are removing space. This | |
2696 | * is because we need the unpinning stage to actually add the | |
2697 | * space back to the block group, otherwise we will leak space. | |
2698 | */ | |
32da5386 | 2699 | if (!alloc && !btrfs_block_group_done(cache)) |
606d1bf1 JB |
2700 | btrfs_cache_block_group(cache, 1); |
2701 | ||
b3470b5d DS |
2702 | byte_in_group = bytenr - cache->start; |
2703 | WARN_ON(byte_in_group > cache->length); | |
606d1bf1 JB |
2704 | |
2705 | spin_lock(&cache->space_info->lock); | |
2706 | spin_lock(&cache->lock); | |
2707 | ||
2708 | if (btrfs_test_opt(info, SPACE_CACHE) && | |
2709 | cache->disk_cache_state < BTRFS_DC_CLEAR) | |
2710 | cache->disk_cache_state = BTRFS_DC_CLEAR; | |
2711 | ||
bf38be65 | 2712 | old_val = cache->used; |
b3470b5d | 2713 | num_bytes = min(total, cache->length - byte_in_group); |
606d1bf1 JB |
2714 | if (alloc) { |
2715 | old_val += num_bytes; | |
bf38be65 | 2716 | cache->used = old_val; |
606d1bf1 JB |
2717 | cache->reserved -= num_bytes; |
2718 | cache->space_info->bytes_reserved -= num_bytes; | |
2719 | cache->space_info->bytes_used += num_bytes; | |
2720 | cache->space_info->disk_used += num_bytes * factor; | |
2721 | spin_unlock(&cache->lock); | |
2722 | spin_unlock(&cache->space_info->lock); | |
2723 | } else { | |
2724 | old_val -= num_bytes; | |
bf38be65 | 2725 | cache->used = old_val; |
606d1bf1 JB |
2726 | cache->pinned += num_bytes; |
2727 | btrfs_space_info_update_bytes_pinned(info, | |
2728 | cache->space_info, num_bytes); | |
2729 | cache->space_info->bytes_used -= num_bytes; | |
2730 | cache->space_info->disk_used -= num_bytes * factor; | |
2731 | spin_unlock(&cache->lock); | |
2732 | spin_unlock(&cache->space_info->lock); | |
2733 | ||
606d1bf1 JB |
2734 | percpu_counter_add_batch( |
2735 | &cache->space_info->total_bytes_pinned, | |
2736 | num_bytes, | |
2737 | BTRFS_TOTAL_BYTES_PINNED_BATCH); | |
2738 | set_extent_dirty(info->pinned_extents, | |
2739 | bytenr, bytenr + num_bytes - 1, | |
2740 | GFP_NOFS | __GFP_NOFAIL); | |
2741 | } | |
2742 | ||
2743 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
2744 | if (list_empty(&cache->dirty_list)) { | |
2745 | list_add_tail(&cache->dirty_list, | |
2746 | &trans->transaction->dirty_bgs); | |
2747 | trans->delayed_ref_updates++; | |
2748 | btrfs_get_block_group(cache); | |
2749 | } | |
2750 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
2751 | ||
2752 | /* | |
2753 | * No longer have used bytes in this block group, queue it for | |
2754 | * deletion. We do this after adding the block group to the | |
2755 | * dirty list to avoid races between cleaner kthread and space | |
2756 | * cache writeout. | |
2757 | */ | |
2758 | if (!alloc && old_val == 0) | |
2759 | btrfs_mark_bg_unused(cache); | |
2760 | ||
2761 | btrfs_put_block_group(cache); | |
2762 | total -= num_bytes; | |
2763 | bytenr += num_bytes; | |
2764 | } | |
2765 | ||
2766 | /* Modified block groups are accounted for in the delayed_refs_rsv. */ | |
2767 | btrfs_update_delayed_refs_rsv(trans); | |
2768 | return ret; | |
2769 | } | |
2770 | ||
2771 | /** | |
2772 | * btrfs_add_reserved_bytes - update the block_group and space info counters | |
2773 | * @cache: The cache we are manipulating | |
2774 | * @ram_bytes: The number of bytes of file content, and will be same to | |
2775 | * @num_bytes except for the compress path. | |
2776 | * @num_bytes: The number of bytes in question | |
2777 | * @delalloc: The blocks are allocated for the delalloc write | |
2778 | * | |
2779 | * This is called by the allocator when it reserves space. If this is a | |
2780 | * reservation and the block group has become read only we cannot make the | |
2781 | * reservation and return -EAGAIN, otherwise this function always succeeds. | |
2782 | */ | |
32da5386 | 2783 | int btrfs_add_reserved_bytes(struct btrfs_block_group *cache, |
606d1bf1 JB |
2784 | u64 ram_bytes, u64 num_bytes, int delalloc) |
2785 | { | |
2786 | struct btrfs_space_info *space_info = cache->space_info; | |
2787 | int ret = 0; | |
2788 | ||
2789 | spin_lock(&space_info->lock); | |
2790 | spin_lock(&cache->lock); | |
2791 | if (cache->ro) { | |
2792 | ret = -EAGAIN; | |
2793 | } else { | |
2794 | cache->reserved += num_bytes; | |
2795 | space_info->bytes_reserved += num_bytes; | |
a43c3835 JB |
2796 | trace_btrfs_space_reservation(cache->fs_info, "space_info", |
2797 | space_info->flags, num_bytes, 1); | |
606d1bf1 JB |
2798 | btrfs_space_info_update_bytes_may_use(cache->fs_info, |
2799 | space_info, -ram_bytes); | |
2800 | if (delalloc) | |
2801 | cache->delalloc_bytes += num_bytes; | |
2802 | } | |
2803 | spin_unlock(&cache->lock); | |
2804 | spin_unlock(&space_info->lock); | |
2805 | return ret; | |
2806 | } | |
2807 | ||
2808 | /** | |
2809 | * btrfs_free_reserved_bytes - update the block_group and space info counters | |
2810 | * @cache: The cache we are manipulating | |
2811 | * @num_bytes: The number of bytes in question | |
2812 | * @delalloc: The blocks are allocated for the delalloc write | |
2813 | * | |
2814 | * This is called by somebody who is freeing space that was never actually used | |
2815 | * on disk. For example if you reserve some space for a new leaf in transaction | |
2816 | * A and before transaction A commits you free that leaf, you call this with | |
2817 | * reserve set to 0 in order to clear the reservation. | |
2818 | */ | |
32da5386 | 2819 | void btrfs_free_reserved_bytes(struct btrfs_block_group *cache, |
606d1bf1 JB |
2820 | u64 num_bytes, int delalloc) |
2821 | { | |
2822 | struct btrfs_space_info *space_info = cache->space_info; | |
2823 | ||
2824 | spin_lock(&space_info->lock); | |
2825 | spin_lock(&cache->lock); | |
2826 | if (cache->ro) | |
2827 | space_info->bytes_readonly += num_bytes; | |
2828 | cache->reserved -= num_bytes; | |
2829 | space_info->bytes_reserved -= num_bytes; | |
2830 | space_info->max_extent_size = 0; | |
2831 | ||
2832 | if (delalloc) | |
2833 | cache->delalloc_bytes -= num_bytes; | |
2834 | spin_unlock(&cache->lock); | |
2835 | spin_unlock(&space_info->lock); | |
2836 | } | |
07730d87 JB |
2837 | |
2838 | static void force_metadata_allocation(struct btrfs_fs_info *info) | |
2839 | { | |
2840 | struct list_head *head = &info->space_info; | |
2841 | struct btrfs_space_info *found; | |
2842 | ||
2843 | rcu_read_lock(); | |
2844 | list_for_each_entry_rcu(found, head, list) { | |
2845 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) | |
2846 | found->force_alloc = CHUNK_ALLOC_FORCE; | |
2847 | } | |
2848 | rcu_read_unlock(); | |
2849 | } | |
2850 | ||
2851 | static int should_alloc_chunk(struct btrfs_fs_info *fs_info, | |
2852 | struct btrfs_space_info *sinfo, int force) | |
2853 | { | |
2854 | u64 bytes_used = btrfs_space_info_used(sinfo, false); | |
2855 | u64 thresh; | |
2856 | ||
2857 | if (force == CHUNK_ALLOC_FORCE) | |
2858 | return 1; | |
2859 | ||
2860 | /* | |
2861 | * in limited mode, we want to have some free space up to | |
2862 | * about 1% of the FS size. | |
2863 | */ | |
2864 | if (force == CHUNK_ALLOC_LIMITED) { | |
2865 | thresh = btrfs_super_total_bytes(fs_info->super_copy); | |
2866 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); | |
2867 | ||
2868 | if (sinfo->total_bytes - bytes_used < thresh) | |
2869 | return 1; | |
2870 | } | |
2871 | ||
2872 | if (bytes_used + SZ_2M < div_factor(sinfo->total_bytes, 8)) | |
2873 | return 0; | |
2874 | return 1; | |
2875 | } | |
2876 | ||
2877 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, u64 type) | |
2878 | { | |
2879 | u64 alloc_flags = btrfs_get_alloc_profile(trans->fs_info, type); | |
2880 | ||
2881 | return btrfs_chunk_alloc(trans, alloc_flags, CHUNK_ALLOC_FORCE); | |
2882 | } | |
2883 | ||
2884 | /* | |
2885 | * If force is CHUNK_ALLOC_FORCE: | |
2886 | * - return 1 if it successfully allocates a chunk, | |
2887 | * - return errors including -ENOSPC otherwise. | |
2888 | * If force is NOT CHUNK_ALLOC_FORCE: | |
2889 | * - return 0 if it doesn't need to allocate a new chunk, | |
2890 | * - return 1 if it successfully allocates a chunk, | |
2891 | * - return errors including -ENOSPC otherwise. | |
2892 | */ | |
2893 | int btrfs_chunk_alloc(struct btrfs_trans_handle *trans, u64 flags, | |
2894 | enum btrfs_chunk_alloc_enum force) | |
2895 | { | |
2896 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
2897 | struct btrfs_space_info *space_info; | |
2898 | bool wait_for_alloc = false; | |
2899 | bool should_alloc = false; | |
2900 | int ret = 0; | |
2901 | ||
2902 | /* Don't re-enter if we're already allocating a chunk */ | |
2903 | if (trans->allocating_chunk) | |
2904 | return -ENOSPC; | |
2905 | ||
2906 | space_info = btrfs_find_space_info(fs_info, flags); | |
2907 | ASSERT(space_info); | |
2908 | ||
2909 | do { | |
2910 | spin_lock(&space_info->lock); | |
2911 | if (force < space_info->force_alloc) | |
2912 | force = space_info->force_alloc; | |
2913 | should_alloc = should_alloc_chunk(fs_info, space_info, force); | |
2914 | if (space_info->full) { | |
2915 | /* No more free physical space */ | |
2916 | if (should_alloc) | |
2917 | ret = -ENOSPC; | |
2918 | else | |
2919 | ret = 0; | |
2920 | spin_unlock(&space_info->lock); | |
2921 | return ret; | |
2922 | } else if (!should_alloc) { | |
2923 | spin_unlock(&space_info->lock); | |
2924 | return 0; | |
2925 | } else if (space_info->chunk_alloc) { | |
2926 | /* | |
2927 | * Someone is already allocating, so we need to block | |
2928 | * until this someone is finished and then loop to | |
2929 | * recheck if we should continue with our allocation | |
2930 | * attempt. | |
2931 | */ | |
2932 | wait_for_alloc = true; | |
2933 | spin_unlock(&space_info->lock); | |
2934 | mutex_lock(&fs_info->chunk_mutex); | |
2935 | mutex_unlock(&fs_info->chunk_mutex); | |
2936 | } else { | |
2937 | /* Proceed with allocation */ | |
2938 | space_info->chunk_alloc = 1; | |
2939 | wait_for_alloc = false; | |
2940 | spin_unlock(&space_info->lock); | |
2941 | } | |
2942 | ||
2943 | cond_resched(); | |
2944 | } while (wait_for_alloc); | |
2945 | ||
2946 | mutex_lock(&fs_info->chunk_mutex); | |
2947 | trans->allocating_chunk = true; | |
2948 | ||
2949 | /* | |
2950 | * If we have mixed data/metadata chunks we want to make sure we keep | |
2951 | * allocating mixed chunks instead of individual chunks. | |
2952 | */ | |
2953 | if (btrfs_mixed_space_info(space_info)) | |
2954 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); | |
2955 | ||
2956 | /* | |
2957 | * if we're doing a data chunk, go ahead and make sure that | |
2958 | * we keep a reasonable number of metadata chunks allocated in the | |
2959 | * FS as well. | |
2960 | */ | |
2961 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { | |
2962 | fs_info->data_chunk_allocations++; | |
2963 | if (!(fs_info->data_chunk_allocations % | |
2964 | fs_info->metadata_ratio)) | |
2965 | force_metadata_allocation(fs_info); | |
2966 | } | |
2967 | ||
2968 | /* | |
2969 | * Check if we have enough space in SYSTEM chunk because we may need | |
2970 | * to update devices. | |
2971 | */ | |
2972 | check_system_chunk(trans, flags); | |
2973 | ||
2974 | ret = btrfs_alloc_chunk(trans, flags); | |
2975 | trans->allocating_chunk = false; | |
2976 | ||
2977 | spin_lock(&space_info->lock); | |
2978 | if (ret < 0) { | |
2979 | if (ret == -ENOSPC) | |
2980 | space_info->full = 1; | |
2981 | else | |
2982 | goto out; | |
2983 | } else { | |
2984 | ret = 1; | |
2985 | space_info->max_extent_size = 0; | |
2986 | } | |
2987 | ||
2988 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; | |
2989 | out: | |
2990 | space_info->chunk_alloc = 0; | |
2991 | spin_unlock(&space_info->lock); | |
2992 | mutex_unlock(&fs_info->chunk_mutex); | |
2993 | /* | |
2994 | * When we allocate a new chunk we reserve space in the chunk block | |
2995 | * reserve to make sure we can COW nodes/leafs in the chunk tree or | |
2996 | * add new nodes/leafs to it if we end up needing to do it when | |
2997 | * inserting the chunk item and updating device items as part of the | |
2998 | * second phase of chunk allocation, performed by | |
2999 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a | |
3000 | * large number of new block groups to create in our transaction | |
3001 | * handle's new_bgs list to avoid exhausting the chunk block reserve | |
3002 | * in extreme cases - like having a single transaction create many new | |
3003 | * block groups when starting to write out the free space caches of all | |
3004 | * the block groups that were made dirty during the lifetime of the | |
3005 | * transaction. | |
3006 | */ | |
3007 | if (trans->chunk_bytes_reserved >= (u64)SZ_2M) | |
3008 | btrfs_create_pending_block_groups(trans); | |
3009 | ||
3010 | return ret; | |
3011 | } | |
3012 | ||
3013 | static u64 get_profile_num_devs(struct btrfs_fs_info *fs_info, u64 type) | |
3014 | { | |
3015 | u64 num_dev; | |
3016 | ||
3017 | num_dev = btrfs_raid_array[btrfs_bg_flags_to_raid_index(type)].devs_max; | |
3018 | if (!num_dev) | |
3019 | num_dev = fs_info->fs_devices->rw_devices; | |
3020 | ||
3021 | return num_dev; | |
3022 | } | |
3023 | ||
3024 | /* | |
a9143bd3 | 3025 | * Reserve space in the system space for allocating or removing a chunk |
07730d87 JB |
3026 | */ |
3027 | void check_system_chunk(struct btrfs_trans_handle *trans, u64 type) | |
3028 | { | |
3029 | struct btrfs_fs_info *fs_info = trans->fs_info; | |
3030 | struct btrfs_space_info *info; | |
3031 | u64 left; | |
3032 | u64 thresh; | |
3033 | int ret = 0; | |
3034 | u64 num_devs; | |
3035 | ||
3036 | /* | |
3037 | * Needed because we can end up allocating a system chunk and for an | |
3038 | * atomic and race free space reservation in the chunk block reserve. | |
3039 | */ | |
3040 | lockdep_assert_held(&fs_info->chunk_mutex); | |
3041 | ||
3042 | info = btrfs_find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); | |
3043 | spin_lock(&info->lock); | |
3044 | left = info->total_bytes - btrfs_space_info_used(info, true); | |
3045 | spin_unlock(&info->lock); | |
3046 | ||
3047 | num_devs = get_profile_num_devs(fs_info, type); | |
3048 | ||
3049 | /* num_devs device items to update and 1 chunk item to add or remove */ | |
2bd36e7b JB |
3050 | thresh = btrfs_calc_metadata_size(fs_info, num_devs) + |
3051 | btrfs_calc_insert_metadata_size(fs_info, 1); | |
07730d87 JB |
3052 | |
3053 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { | |
3054 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", | |
3055 | left, thresh, type); | |
3056 | btrfs_dump_space_info(fs_info, info, 0, 0); | |
3057 | } | |
3058 | ||
3059 | if (left < thresh) { | |
3060 | u64 flags = btrfs_system_alloc_profile(fs_info); | |
3061 | ||
3062 | /* | |
3063 | * Ignore failure to create system chunk. We might end up not | |
3064 | * needing it, as we might not need to COW all nodes/leafs from | |
3065 | * the paths we visit in the chunk tree (they were already COWed | |
3066 | * or created in the current transaction for example). | |
3067 | */ | |
3068 | ret = btrfs_alloc_chunk(trans, flags); | |
3069 | } | |
3070 | ||
3071 | if (!ret) { | |
3072 | ret = btrfs_block_rsv_add(fs_info->chunk_root, | |
3073 | &fs_info->chunk_block_rsv, | |
3074 | thresh, BTRFS_RESERVE_NO_FLUSH); | |
3075 | if (!ret) | |
3076 | trans->chunk_bytes_reserved += thresh; | |
3077 | } | |
3078 | } | |
3079 | ||
3e43c279 JB |
3080 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
3081 | { | |
32da5386 | 3082 | struct btrfs_block_group *block_group; |
3e43c279 JB |
3083 | u64 last = 0; |
3084 | ||
3085 | while (1) { | |
3086 | struct inode *inode; | |
3087 | ||
3088 | block_group = btrfs_lookup_first_block_group(info, last); | |
3089 | while (block_group) { | |
3090 | btrfs_wait_block_group_cache_done(block_group); | |
3091 | spin_lock(&block_group->lock); | |
3092 | if (block_group->iref) | |
3093 | break; | |
3094 | spin_unlock(&block_group->lock); | |
3095 | block_group = btrfs_next_block_group(block_group); | |
3096 | } | |
3097 | if (!block_group) { | |
3098 | if (last == 0) | |
3099 | break; | |
3100 | last = 0; | |
3101 | continue; | |
3102 | } | |
3103 | ||
3104 | inode = block_group->inode; | |
3105 | block_group->iref = 0; | |
3106 | block_group->inode = NULL; | |
3107 | spin_unlock(&block_group->lock); | |
3108 | ASSERT(block_group->io_ctl.inode == NULL); | |
3109 | iput(inode); | |
b3470b5d | 3110 | last = block_group->start + block_group->length; |
3e43c279 JB |
3111 | btrfs_put_block_group(block_group); |
3112 | } | |
3113 | } | |
3114 | ||
3115 | /* | |
3116 | * Must be called only after stopping all workers, since we could have block | |
3117 | * group caching kthreads running, and therefore they could race with us if we | |
3118 | * freed the block groups before stopping them. | |
3119 | */ | |
3120 | int btrfs_free_block_groups(struct btrfs_fs_info *info) | |
3121 | { | |
32da5386 | 3122 | struct btrfs_block_group *block_group; |
3e43c279 JB |
3123 | struct btrfs_space_info *space_info; |
3124 | struct btrfs_caching_control *caching_ctl; | |
3125 | struct rb_node *n; | |
3126 | ||
3127 | down_write(&info->commit_root_sem); | |
3128 | while (!list_empty(&info->caching_block_groups)) { | |
3129 | caching_ctl = list_entry(info->caching_block_groups.next, | |
3130 | struct btrfs_caching_control, list); | |
3131 | list_del(&caching_ctl->list); | |
3132 | btrfs_put_caching_control(caching_ctl); | |
3133 | } | |
3134 | up_write(&info->commit_root_sem); | |
3135 | ||
3136 | spin_lock(&info->unused_bgs_lock); | |
3137 | while (!list_empty(&info->unused_bgs)) { | |
3138 | block_group = list_first_entry(&info->unused_bgs, | |
32da5386 | 3139 | struct btrfs_block_group, |
3e43c279 JB |
3140 | bg_list); |
3141 | list_del_init(&block_group->bg_list); | |
3142 | btrfs_put_block_group(block_group); | |
3143 | } | |
3144 | spin_unlock(&info->unused_bgs_lock); | |
3145 | ||
3146 | spin_lock(&info->block_group_cache_lock); | |
3147 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { | |
32da5386 | 3148 | block_group = rb_entry(n, struct btrfs_block_group, |
3e43c279 JB |
3149 | cache_node); |
3150 | rb_erase(&block_group->cache_node, | |
3151 | &info->block_group_cache_tree); | |
3152 | RB_CLEAR_NODE(&block_group->cache_node); | |
3153 | spin_unlock(&info->block_group_cache_lock); | |
3154 | ||
3155 | down_write(&block_group->space_info->groups_sem); | |
3156 | list_del(&block_group->list); | |
3157 | up_write(&block_group->space_info->groups_sem); | |
3158 | ||
3159 | /* | |
3160 | * We haven't cached this block group, which means we could | |
3161 | * possibly have excluded extents on this block group. | |
3162 | */ | |
3163 | if (block_group->cached == BTRFS_CACHE_NO || | |
3164 | block_group->cached == BTRFS_CACHE_ERROR) | |
3165 | btrfs_free_excluded_extents(block_group); | |
3166 | ||
3167 | btrfs_remove_free_space_cache(block_group); | |
3168 | ASSERT(block_group->cached != BTRFS_CACHE_STARTED); | |
3169 | ASSERT(list_empty(&block_group->dirty_list)); | |
3170 | ASSERT(list_empty(&block_group->io_list)); | |
3171 | ASSERT(list_empty(&block_group->bg_list)); | |
3172 | ASSERT(atomic_read(&block_group->count) == 1); | |
3173 | btrfs_put_block_group(block_group); | |
3174 | ||
3175 | spin_lock(&info->block_group_cache_lock); | |
3176 | } | |
3177 | spin_unlock(&info->block_group_cache_lock); | |
3178 | ||
3179 | /* | |
3180 | * Now that all the block groups are freed, go through and free all the | |
3181 | * space_info structs. This is only called during the final stages of | |
3182 | * unmount, and so we know nobody is using them. We call | |
3183 | * synchronize_rcu() once before we start, just to be on the safe side. | |
3184 | */ | |
3185 | synchronize_rcu(); | |
3186 | ||
3187 | btrfs_release_global_block_rsv(info); | |
3188 | ||
3189 | while (!list_empty(&info->space_info)) { | |
3190 | space_info = list_entry(info->space_info.next, | |
3191 | struct btrfs_space_info, | |
3192 | list); | |
3193 | ||
3194 | /* | |
3195 | * Do not hide this behind enospc_debug, this is actually | |
3196 | * important and indicates a real bug if this happens. | |
3197 | */ | |
3198 | if (WARN_ON(space_info->bytes_pinned > 0 || | |
3199 | space_info->bytes_reserved > 0 || | |
3200 | space_info->bytes_may_use > 0)) | |
3201 | btrfs_dump_space_info(info, space_info, 0, 0); | |
3202 | list_del(&space_info->list); | |
3203 | btrfs_sysfs_remove_space_info(space_info); | |
3204 | } | |
3205 | return 0; | |
3206 | } |