]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
2 | * Copyright (C) 2007 Oracle. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program; if not, write to the | |
15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
16 | * Boston, MA 021110-1307, USA. | |
17 | */ | |
ec6b910f | 18 | #include <linux/sched.h> |
edbd8d4e | 19 | #include <linux/pagemap.h> |
ec44a35c | 20 | #include <linux/writeback.h> |
21af804c | 21 | #include <linux/blkdev.h> |
b7a9f29f | 22 | #include <linux/sort.h> |
4184ea7f | 23 | #include <linux/rcupdate.h> |
817d52f8 | 24 | #include <linux/kthread.h> |
5a0e3ad6 | 25 | #include <linux/slab.h> |
dff51cd1 | 26 | #include <linux/ratelimit.h> |
b150a4f1 | 27 | #include <linux/percpu_counter.h> |
74493f7a | 28 | #include "hash.h" |
995946dd | 29 | #include "tree-log.h" |
fec577fb CM |
30 | #include "disk-io.h" |
31 | #include "print-tree.h" | |
0b86a832 | 32 | #include "volumes.h" |
53b381b3 | 33 | #include "raid56.h" |
925baedd | 34 | #include "locking.h" |
fa9c0d79 | 35 | #include "free-space-cache.h" |
1e144fb8 | 36 | #include "free-space-tree.h" |
3fed40cc | 37 | #include "math.h" |
6ab0a202 | 38 | #include "sysfs.h" |
fcebe456 | 39 | #include "qgroup.h" |
fec577fb | 40 | |
709c0486 AJ |
41 | #undef SCRAMBLE_DELAYED_REFS |
42 | ||
9e622d6b MX |
43 | /* |
44 | * control flags for do_chunk_alloc's force field | |
0e4f8f88 CM |
45 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk |
46 | * if we really need one. | |
47 | * | |
0e4f8f88 CM |
48 | * CHUNK_ALLOC_LIMITED means to only try and allocate one |
49 | * if we have very few chunks already allocated. This is | |
50 | * used as part of the clustering code to help make sure | |
51 | * we have a good pool of storage to cluster in, without | |
52 | * filling the FS with empty chunks | |
53 | * | |
9e622d6b MX |
54 | * CHUNK_ALLOC_FORCE means it must try to allocate one |
55 | * | |
0e4f8f88 CM |
56 | */ |
57 | enum { | |
58 | CHUNK_ALLOC_NO_FORCE = 0, | |
9e622d6b MX |
59 | CHUNK_ALLOC_LIMITED = 1, |
60 | CHUNK_ALLOC_FORCE = 2, | |
0e4f8f88 CM |
61 | }; |
62 | ||
ce93ec54 | 63 | static int update_block_group(struct btrfs_trans_handle *trans, |
6202df69 | 64 | struct btrfs_fs_info *fs_info, u64 bytenr, |
ce93ec54 | 65 | u64 num_bytes, int alloc); |
5d4f98a2 YZ |
66 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
67 | struct btrfs_root *root, | |
c682f9b3 | 68 | struct btrfs_delayed_ref_node *node, u64 parent, |
5d4f98a2 YZ |
69 | u64 root_objectid, u64 owner_objectid, |
70 | u64 owner_offset, int refs_to_drop, | |
c682f9b3 | 71 | struct btrfs_delayed_extent_op *extra_op); |
5d4f98a2 YZ |
72 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, |
73 | struct extent_buffer *leaf, | |
74 | struct btrfs_extent_item *ei); | |
75 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
76 | struct btrfs_root *root, | |
77 | u64 parent, u64 root_objectid, | |
78 | u64 flags, u64 owner, u64 offset, | |
79 | struct btrfs_key *ins, int ref_mod); | |
80 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, | |
81 | struct btrfs_root *root, | |
82 | u64 parent, u64 root_objectid, | |
83 | u64 flags, struct btrfs_disk_key *key, | |
b06c4bf5 | 84 | int level, struct btrfs_key *ins); |
6a63209f | 85 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
698d0082 JB |
86 | struct btrfs_root *extent_root, u64 flags, |
87 | int force); | |
11833d66 YZ |
88 | static int find_next_key(struct btrfs_path *path, int level, |
89 | struct btrfs_key *key); | |
ab8d0fc4 JM |
90 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
91 | struct btrfs_space_info *info, u64 bytes, | |
9ed74f2d | 92 | int dump_block_groups); |
4824f1f4 | 93 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
18513091 | 94 | u64 ram_bytes, u64 num_bytes, int delalloc); |
4824f1f4 WX |
95 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, |
96 | u64 num_bytes, int delalloc); | |
5d80366e JB |
97 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, |
98 | u64 num_bytes); | |
48a3b636 ES |
99 | int btrfs_pin_extent(struct btrfs_root *root, |
100 | u64 bytenr, u64 num_bytes, int reserved); | |
957780eb JB |
101 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
102 | struct btrfs_space_info *space_info, | |
103 | u64 orig_bytes, | |
104 | enum btrfs_reserve_flush_enum flush); | |
105 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, | |
106 | struct btrfs_space_info *space_info, | |
107 | u64 num_bytes); | |
108 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, | |
109 | struct btrfs_space_info *space_info, | |
110 | u64 num_bytes); | |
6a63209f | 111 | |
817d52f8 JB |
112 | static noinline int |
113 | block_group_cache_done(struct btrfs_block_group_cache *cache) | |
114 | { | |
115 | smp_mb(); | |
36cce922 JB |
116 | return cache->cached == BTRFS_CACHE_FINISHED || |
117 | cache->cached == BTRFS_CACHE_ERROR; | |
817d52f8 JB |
118 | } |
119 | ||
0f9dd46c JB |
120 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
121 | { | |
122 | return (cache->flags & bits) == bits; | |
123 | } | |
124 | ||
758f2dfc | 125 | void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
11dfe35a JB |
126 | { |
127 | atomic_inc(&cache->count); | |
128 | } | |
129 | ||
130 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) | |
131 | { | |
f0486c68 YZ |
132 | if (atomic_dec_and_test(&cache->count)) { |
133 | WARN_ON(cache->pinned > 0); | |
134 | WARN_ON(cache->reserved > 0); | |
34d52cb6 | 135 | kfree(cache->free_space_ctl); |
11dfe35a | 136 | kfree(cache); |
f0486c68 | 137 | } |
11dfe35a JB |
138 | } |
139 | ||
0f9dd46c JB |
140 | /* |
141 | * this adds the block group to the fs_info rb tree for the block group | |
142 | * cache | |
143 | */ | |
b2950863 | 144 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
0f9dd46c JB |
145 | struct btrfs_block_group_cache *block_group) |
146 | { | |
147 | struct rb_node **p; | |
148 | struct rb_node *parent = NULL; | |
149 | struct btrfs_block_group_cache *cache; | |
150 | ||
151 | spin_lock(&info->block_group_cache_lock); | |
152 | p = &info->block_group_cache_tree.rb_node; | |
153 | ||
154 | while (*p) { | |
155 | parent = *p; | |
156 | cache = rb_entry(parent, struct btrfs_block_group_cache, | |
157 | cache_node); | |
158 | if (block_group->key.objectid < cache->key.objectid) { | |
159 | p = &(*p)->rb_left; | |
160 | } else if (block_group->key.objectid > cache->key.objectid) { | |
161 | p = &(*p)->rb_right; | |
162 | } else { | |
163 | spin_unlock(&info->block_group_cache_lock); | |
164 | return -EEXIST; | |
165 | } | |
166 | } | |
167 | ||
168 | rb_link_node(&block_group->cache_node, parent, p); | |
169 | rb_insert_color(&block_group->cache_node, | |
170 | &info->block_group_cache_tree); | |
a1897fdd LB |
171 | |
172 | if (info->first_logical_byte > block_group->key.objectid) | |
173 | info->first_logical_byte = block_group->key.objectid; | |
174 | ||
0f9dd46c JB |
175 | spin_unlock(&info->block_group_cache_lock); |
176 | ||
177 | return 0; | |
178 | } | |
179 | ||
180 | /* | |
181 | * This will return the block group at or after bytenr if contains is 0, else | |
182 | * it will return the block group that contains the bytenr | |
183 | */ | |
184 | static struct btrfs_block_group_cache * | |
185 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, | |
186 | int contains) | |
187 | { | |
188 | struct btrfs_block_group_cache *cache, *ret = NULL; | |
189 | struct rb_node *n; | |
190 | u64 end, start; | |
191 | ||
192 | spin_lock(&info->block_group_cache_lock); | |
193 | n = info->block_group_cache_tree.rb_node; | |
194 | ||
195 | while (n) { | |
196 | cache = rb_entry(n, struct btrfs_block_group_cache, | |
197 | cache_node); | |
198 | end = cache->key.objectid + cache->key.offset - 1; | |
199 | start = cache->key.objectid; | |
200 | ||
201 | if (bytenr < start) { | |
202 | if (!contains && (!ret || start < ret->key.objectid)) | |
203 | ret = cache; | |
204 | n = n->rb_left; | |
205 | } else if (bytenr > start) { | |
206 | if (contains && bytenr <= end) { | |
207 | ret = cache; | |
208 | break; | |
209 | } | |
210 | n = n->rb_right; | |
211 | } else { | |
212 | ret = cache; | |
213 | break; | |
214 | } | |
215 | } | |
a1897fdd | 216 | if (ret) { |
11dfe35a | 217 | btrfs_get_block_group(ret); |
a1897fdd LB |
218 | if (bytenr == 0 && info->first_logical_byte > ret->key.objectid) |
219 | info->first_logical_byte = ret->key.objectid; | |
220 | } | |
0f9dd46c JB |
221 | spin_unlock(&info->block_group_cache_lock); |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
11833d66 YZ |
226 | static int add_excluded_extent(struct btrfs_root *root, |
227 | u64 start, u64 num_bytes) | |
817d52f8 | 228 | { |
0b246afa | 229 | struct btrfs_fs_info *fs_info = root->fs_info; |
11833d66 | 230 | u64 end = start + num_bytes - 1; |
0b246afa | 231 | set_extent_bits(&fs_info->freed_extents[0], |
ceeb0ae7 | 232 | start, end, EXTENT_UPTODATE); |
0b246afa | 233 | set_extent_bits(&fs_info->freed_extents[1], |
ceeb0ae7 | 234 | start, end, EXTENT_UPTODATE); |
11833d66 YZ |
235 | return 0; |
236 | } | |
817d52f8 | 237 | |
11833d66 YZ |
238 | static void free_excluded_extents(struct btrfs_root *root, |
239 | struct btrfs_block_group_cache *cache) | |
240 | { | |
0b246afa | 241 | struct btrfs_fs_info *fs_info = root->fs_info; |
11833d66 | 242 | u64 start, end; |
817d52f8 | 243 | |
11833d66 YZ |
244 | start = cache->key.objectid; |
245 | end = start + cache->key.offset - 1; | |
246 | ||
0b246afa | 247 | clear_extent_bits(&fs_info->freed_extents[0], |
91166212 | 248 | start, end, EXTENT_UPTODATE); |
0b246afa | 249 | clear_extent_bits(&fs_info->freed_extents[1], |
91166212 | 250 | start, end, EXTENT_UPTODATE); |
817d52f8 JB |
251 | } |
252 | ||
11833d66 YZ |
253 | static int exclude_super_stripes(struct btrfs_root *root, |
254 | struct btrfs_block_group_cache *cache) | |
817d52f8 | 255 | { |
0b246afa | 256 | struct btrfs_fs_info *fs_info = root->fs_info; |
817d52f8 JB |
257 | u64 bytenr; |
258 | u64 *logical; | |
259 | int stripe_len; | |
260 | int i, nr, ret; | |
261 | ||
06b2331f YZ |
262 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
263 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; | |
264 | cache->bytes_super += stripe_len; | |
265 | ret = add_excluded_extent(root, cache->key.objectid, | |
266 | stripe_len); | |
835d974f JB |
267 | if (ret) |
268 | return ret; | |
06b2331f YZ |
269 | } |
270 | ||
817d52f8 JB |
271 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
272 | bytenr = btrfs_sb_offset(i); | |
0b246afa | 273 | ret = btrfs_rmap_block(fs_info, cache->key.objectid, |
ab8d0fc4 | 274 | bytenr, 0, &logical, &nr, &stripe_len); |
835d974f JB |
275 | if (ret) |
276 | return ret; | |
11833d66 | 277 | |
817d52f8 | 278 | while (nr--) { |
51bf5f0b JB |
279 | u64 start, len; |
280 | ||
281 | if (logical[nr] > cache->key.objectid + | |
282 | cache->key.offset) | |
283 | continue; | |
284 | ||
285 | if (logical[nr] + stripe_len <= cache->key.objectid) | |
286 | continue; | |
287 | ||
288 | start = logical[nr]; | |
289 | if (start < cache->key.objectid) { | |
290 | start = cache->key.objectid; | |
291 | len = (logical[nr] + stripe_len) - start; | |
292 | } else { | |
293 | len = min_t(u64, stripe_len, | |
294 | cache->key.objectid + | |
295 | cache->key.offset - start); | |
296 | } | |
297 | ||
298 | cache->bytes_super += len; | |
299 | ret = add_excluded_extent(root, start, len); | |
835d974f JB |
300 | if (ret) { |
301 | kfree(logical); | |
302 | return ret; | |
303 | } | |
817d52f8 | 304 | } |
11833d66 | 305 | |
817d52f8 JB |
306 | kfree(logical); |
307 | } | |
817d52f8 JB |
308 | return 0; |
309 | } | |
310 | ||
11833d66 YZ |
311 | static struct btrfs_caching_control * |
312 | get_caching_control(struct btrfs_block_group_cache *cache) | |
313 | { | |
314 | struct btrfs_caching_control *ctl; | |
315 | ||
316 | spin_lock(&cache->lock); | |
dde5abee JB |
317 | if (!cache->caching_ctl) { |
318 | spin_unlock(&cache->lock); | |
11833d66 YZ |
319 | return NULL; |
320 | } | |
321 | ||
322 | ctl = cache->caching_ctl; | |
323 | atomic_inc(&ctl->count); | |
324 | spin_unlock(&cache->lock); | |
325 | return ctl; | |
326 | } | |
327 | ||
328 | static void put_caching_control(struct btrfs_caching_control *ctl) | |
329 | { | |
330 | if (atomic_dec_and_test(&ctl->count)) | |
331 | kfree(ctl); | |
332 | } | |
333 | ||
d0bd4560 JB |
334 | #ifdef CONFIG_BTRFS_DEBUG |
335 | static void fragment_free_space(struct btrfs_root *root, | |
336 | struct btrfs_block_group_cache *block_group) | |
337 | { | |
0b246afa JM |
338 | struct btrfs_fs_info *fs_info = root->fs_info; |
339 | ||
d0bd4560 JB |
340 | u64 start = block_group->key.objectid; |
341 | u64 len = block_group->key.offset; | |
342 | u64 chunk = block_group->flags & BTRFS_BLOCK_GROUP_METADATA ? | |
0b246afa | 343 | fs_info->nodesize : fs_info->sectorsize; |
d0bd4560 JB |
344 | u64 step = chunk << 1; |
345 | ||
346 | while (len > chunk) { | |
347 | btrfs_remove_free_space(block_group, start, chunk); | |
348 | start += step; | |
349 | if (len < step) | |
350 | len = 0; | |
351 | else | |
352 | len -= step; | |
353 | } | |
354 | } | |
355 | #endif | |
356 | ||
0f9dd46c JB |
357 | /* |
358 | * this is only called by cache_block_group, since we could have freed extents | |
359 | * we need to check the pinned_extents for any extents that can't be used yet | |
360 | * since their free space will be released as soon as the transaction commits. | |
361 | */ | |
a5ed9182 OS |
362 | u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
363 | struct btrfs_fs_info *info, u64 start, u64 end) | |
0f9dd46c | 364 | { |
817d52f8 | 365 | u64 extent_start, extent_end, size, total_added = 0; |
0f9dd46c JB |
366 | int ret; |
367 | ||
368 | while (start < end) { | |
11833d66 | 369 | ret = find_first_extent_bit(info->pinned_extents, start, |
0f9dd46c | 370 | &extent_start, &extent_end, |
e6138876 JB |
371 | EXTENT_DIRTY | EXTENT_UPTODATE, |
372 | NULL); | |
0f9dd46c JB |
373 | if (ret) |
374 | break; | |
375 | ||
06b2331f | 376 | if (extent_start <= start) { |
0f9dd46c JB |
377 | start = extent_end + 1; |
378 | } else if (extent_start > start && extent_start < end) { | |
379 | size = extent_start - start; | |
817d52f8 | 380 | total_added += size; |
ea6a478e JB |
381 | ret = btrfs_add_free_space(block_group, start, |
382 | size); | |
79787eaa | 383 | BUG_ON(ret); /* -ENOMEM or logic error */ |
0f9dd46c JB |
384 | start = extent_end + 1; |
385 | } else { | |
386 | break; | |
387 | } | |
388 | } | |
389 | ||
390 | if (start < end) { | |
391 | size = end - start; | |
817d52f8 | 392 | total_added += size; |
ea6a478e | 393 | ret = btrfs_add_free_space(block_group, start, size); |
79787eaa | 394 | BUG_ON(ret); /* -ENOMEM or logic error */ |
0f9dd46c JB |
395 | } |
396 | ||
817d52f8 | 397 | return total_added; |
0f9dd46c JB |
398 | } |
399 | ||
73fa48b6 | 400 | static int load_extent_tree_free(struct btrfs_caching_control *caching_ctl) |
e37c9e69 | 401 | { |
0b246afa JM |
402 | struct btrfs_block_group_cache *block_group = caching_ctl->block_group; |
403 | struct btrfs_fs_info *fs_info = block_group->fs_info; | |
404 | struct btrfs_root *extent_root = fs_info->extent_root; | |
e37c9e69 | 405 | struct btrfs_path *path; |
5f39d397 | 406 | struct extent_buffer *leaf; |
11833d66 | 407 | struct btrfs_key key; |
817d52f8 | 408 | u64 total_found = 0; |
11833d66 YZ |
409 | u64 last = 0; |
410 | u32 nritems; | |
73fa48b6 | 411 | int ret; |
d0bd4560 | 412 | bool wakeup = true; |
f510cfec | 413 | |
e37c9e69 CM |
414 | path = btrfs_alloc_path(); |
415 | if (!path) | |
73fa48b6 | 416 | return -ENOMEM; |
7d7d6068 | 417 | |
817d52f8 | 418 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
11833d66 | 419 | |
d0bd4560 JB |
420 | #ifdef CONFIG_BTRFS_DEBUG |
421 | /* | |
422 | * If we're fragmenting we don't want to make anybody think we can | |
423 | * allocate from this block group until we've had a chance to fragment | |
424 | * the free space. | |
425 | */ | |
426 | if (btrfs_should_fragment_free_space(extent_root, block_group)) | |
427 | wakeup = false; | |
428 | #endif | |
5cd57b2c | 429 | /* |
817d52f8 JB |
430 | * We don't want to deadlock with somebody trying to allocate a new |
431 | * extent for the extent root while also trying to search the extent | |
432 | * root to add free space. So we skip locking and search the commit | |
433 | * root, since its read-only | |
5cd57b2c CM |
434 | */ |
435 | path->skip_locking = 1; | |
817d52f8 | 436 | path->search_commit_root = 1; |
e4058b54 | 437 | path->reada = READA_FORWARD; |
817d52f8 | 438 | |
e4404d6e | 439 | key.objectid = last; |
e37c9e69 | 440 | key.offset = 0; |
11833d66 | 441 | key.type = BTRFS_EXTENT_ITEM_KEY; |
013f1b12 | 442 | |
52ee28d2 | 443 | next: |
11833d66 | 444 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
e37c9e69 | 445 | if (ret < 0) |
73fa48b6 | 446 | goto out; |
a512bbf8 | 447 | |
11833d66 YZ |
448 | leaf = path->nodes[0]; |
449 | nritems = btrfs_header_nritems(leaf); | |
450 | ||
d397712b | 451 | while (1) { |
7841cb28 | 452 | if (btrfs_fs_closing(fs_info) > 1) { |
f25784b3 | 453 | last = (u64)-1; |
817d52f8 | 454 | break; |
f25784b3 | 455 | } |
817d52f8 | 456 | |
11833d66 YZ |
457 | if (path->slots[0] < nritems) { |
458 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
459 | } else { | |
460 | ret = find_next_key(path, 0, &key); | |
461 | if (ret) | |
e37c9e69 | 462 | break; |
817d52f8 | 463 | |
c9ea7b24 | 464 | if (need_resched() || |
9e351cc8 | 465 | rwsem_is_contended(&fs_info->commit_root_sem)) { |
d0bd4560 JB |
466 | if (wakeup) |
467 | caching_ctl->progress = last; | |
ff5714cc | 468 | btrfs_release_path(path); |
9e351cc8 | 469 | up_read(&fs_info->commit_root_sem); |
589d8ade | 470 | mutex_unlock(&caching_ctl->mutex); |
11833d66 | 471 | cond_resched(); |
73fa48b6 OS |
472 | mutex_lock(&caching_ctl->mutex); |
473 | down_read(&fs_info->commit_root_sem); | |
474 | goto next; | |
589d8ade | 475 | } |
0a3896d0 JB |
476 | |
477 | ret = btrfs_next_leaf(extent_root, path); | |
478 | if (ret < 0) | |
73fa48b6 | 479 | goto out; |
0a3896d0 JB |
480 | if (ret) |
481 | break; | |
589d8ade JB |
482 | leaf = path->nodes[0]; |
483 | nritems = btrfs_header_nritems(leaf); | |
484 | continue; | |
11833d66 | 485 | } |
817d52f8 | 486 | |
52ee28d2 LB |
487 | if (key.objectid < last) { |
488 | key.objectid = last; | |
489 | key.offset = 0; | |
490 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
491 | ||
d0bd4560 JB |
492 | if (wakeup) |
493 | caching_ctl->progress = last; | |
52ee28d2 LB |
494 | btrfs_release_path(path); |
495 | goto next; | |
496 | } | |
497 | ||
11833d66 YZ |
498 | if (key.objectid < block_group->key.objectid) { |
499 | path->slots[0]++; | |
817d52f8 | 500 | continue; |
e37c9e69 | 501 | } |
0f9dd46c | 502 | |
e37c9e69 | 503 | if (key.objectid >= block_group->key.objectid + |
0f9dd46c | 504 | block_group->key.offset) |
e37c9e69 | 505 | break; |
7d7d6068 | 506 | |
3173a18f JB |
507 | if (key.type == BTRFS_EXTENT_ITEM_KEY || |
508 | key.type == BTRFS_METADATA_ITEM_KEY) { | |
817d52f8 JB |
509 | total_found += add_new_free_space(block_group, |
510 | fs_info, last, | |
511 | key.objectid); | |
3173a18f JB |
512 | if (key.type == BTRFS_METADATA_ITEM_KEY) |
513 | last = key.objectid + | |
da17066c | 514 | fs_info->nodesize; |
3173a18f JB |
515 | else |
516 | last = key.objectid + key.offset; | |
817d52f8 | 517 | |
73fa48b6 | 518 | if (total_found > CACHING_CTL_WAKE_UP) { |
11833d66 | 519 | total_found = 0; |
d0bd4560 JB |
520 | if (wakeup) |
521 | wake_up(&caching_ctl->wait); | |
11833d66 | 522 | } |
817d52f8 | 523 | } |
e37c9e69 CM |
524 | path->slots[0]++; |
525 | } | |
817d52f8 | 526 | ret = 0; |
e37c9e69 | 527 | |
817d52f8 JB |
528 | total_found += add_new_free_space(block_group, fs_info, last, |
529 | block_group->key.objectid + | |
530 | block_group->key.offset); | |
11833d66 | 531 | caching_ctl->progress = (u64)-1; |
817d52f8 | 532 | |
73fa48b6 OS |
533 | out: |
534 | btrfs_free_path(path); | |
535 | return ret; | |
536 | } | |
537 | ||
538 | static noinline void caching_thread(struct btrfs_work *work) | |
539 | { | |
540 | struct btrfs_block_group_cache *block_group; | |
541 | struct btrfs_fs_info *fs_info; | |
542 | struct btrfs_caching_control *caching_ctl; | |
b4570aa9 | 543 | struct btrfs_root *extent_root; |
73fa48b6 OS |
544 | int ret; |
545 | ||
546 | caching_ctl = container_of(work, struct btrfs_caching_control, work); | |
547 | block_group = caching_ctl->block_group; | |
548 | fs_info = block_group->fs_info; | |
b4570aa9 | 549 | extent_root = fs_info->extent_root; |
73fa48b6 OS |
550 | |
551 | mutex_lock(&caching_ctl->mutex); | |
552 | down_read(&fs_info->commit_root_sem); | |
553 | ||
1e144fb8 OS |
554 | if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) |
555 | ret = load_free_space_tree(caching_ctl); | |
556 | else | |
557 | ret = load_extent_tree_free(caching_ctl); | |
73fa48b6 | 558 | |
817d52f8 | 559 | spin_lock(&block_group->lock); |
11833d66 | 560 | block_group->caching_ctl = NULL; |
73fa48b6 | 561 | block_group->cached = ret ? BTRFS_CACHE_ERROR : BTRFS_CACHE_FINISHED; |
817d52f8 | 562 | spin_unlock(&block_group->lock); |
0f9dd46c | 563 | |
d0bd4560 JB |
564 | #ifdef CONFIG_BTRFS_DEBUG |
565 | if (btrfs_should_fragment_free_space(extent_root, block_group)) { | |
566 | u64 bytes_used; | |
567 | ||
568 | spin_lock(&block_group->space_info->lock); | |
569 | spin_lock(&block_group->lock); | |
570 | bytes_used = block_group->key.offset - | |
571 | btrfs_block_group_used(&block_group->item); | |
572 | block_group->space_info->bytes_used += bytes_used >> 1; | |
573 | spin_unlock(&block_group->lock); | |
574 | spin_unlock(&block_group->space_info->lock); | |
575 | fragment_free_space(extent_root, block_group); | |
576 | } | |
577 | #endif | |
578 | ||
579 | caching_ctl->progress = (u64)-1; | |
11833d66 | 580 | |
9e351cc8 | 581 | up_read(&fs_info->commit_root_sem); |
73fa48b6 | 582 | free_excluded_extents(fs_info->extent_root, block_group); |
11833d66 | 583 | mutex_unlock(&caching_ctl->mutex); |
73fa48b6 | 584 | |
11833d66 YZ |
585 | wake_up(&caching_ctl->wait); |
586 | ||
587 | put_caching_control(caching_ctl); | |
11dfe35a | 588 | btrfs_put_block_group(block_group); |
817d52f8 JB |
589 | } |
590 | ||
9d66e233 | 591 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
9d66e233 | 592 | int load_cache_only) |
817d52f8 | 593 | { |
291c7d2f | 594 | DEFINE_WAIT(wait); |
11833d66 YZ |
595 | struct btrfs_fs_info *fs_info = cache->fs_info; |
596 | struct btrfs_caching_control *caching_ctl; | |
817d52f8 JB |
597 | int ret = 0; |
598 | ||
291c7d2f | 599 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
79787eaa JM |
600 | if (!caching_ctl) |
601 | return -ENOMEM; | |
291c7d2f JB |
602 | |
603 | INIT_LIST_HEAD(&caching_ctl->list); | |
604 | mutex_init(&caching_ctl->mutex); | |
605 | init_waitqueue_head(&caching_ctl->wait); | |
606 | caching_ctl->block_group = cache; | |
607 | caching_ctl->progress = cache->key.objectid; | |
608 | atomic_set(&caching_ctl->count, 1); | |
9e0af237 LB |
609 | btrfs_init_work(&caching_ctl->work, btrfs_cache_helper, |
610 | caching_thread, NULL, NULL); | |
291c7d2f JB |
611 | |
612 | spin_lock(&cache->lock); | |
613 | /* | |
614 | * This should be a rare occasion, but this could happen I think in the | |
615 | * case where one thread starts to load the space cache info, and then | |
616 | * some other thread starts a transaction commit which tries to do an | |
617 | * allocation while the other thread is still loading the space cache | |
618 | * info. The previous loop should have kept us from choosing this block | |
619 | * group, but if we've moved to the state where we will wait on caching | |
620 | * block groups we need to first check if we're doing a fast load here, | |
621 | * so we can wait for it to finish, otherwise we could end up allocating | |
622 | * from a block group who's cache gets evicted for one reason or | |
623 | * another. | |
624 | */ | |
625 | while (cache->cached == BTRFS_CACHE_FAST) { | |
626 | struct btrfs_caching_control *ctl; | |
627 | ||
628 | ctl = cache->caching_ctl; | |
629 | atomic_inc(&ctl->count); | |
630 | prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE); | |
631 | spin_unlock(&cache->lock); | |
632 | ||
633 | schedule(); | |
634 | ||
635 | finish_wait(&ctl->wait, &wait); | |
636 | put_caching_control(ctl); | |
637 | spin_lock(&cache->lock); | |
638 | } | |
639 | ||
640 | if (cache->cached != BTRFS_CACHE_NO) { | |
641 | spin_unlock(&cache->lock); | |
642 | kfree(caching_ctl); | |
11833d66 | 643 | return 0; |
291c7d2f JB |
644 | } |
645 | WARN_ON(cache->caching_ctl); | |
646 | cache->caching_ctl = caching_ctl; | |
647 | cache->cached = BTRFS_CACHE_FAST; | |
648 | spin_unlock(&cache->lock); | |
11833d66 | 649 | |
d53ba474 | 650 | if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) { |
cb83b7b8 | 651 | mutex_lock(&caching_ctl->mutex); |
9d66e233 JB |
652 | ret = load_free_space_cache(fs_info, cache); |
653 | ||
654 | spin_lock(&cache->lock); | |
655 | if (ret == 1) { | |
291c7d2f | 656 | cache->caching_ctl = NULL; |
9d66e233 JB |
657 | cache->cached = BTRFS_CACHE_FINISHED; |
658 | cache->last_byte_to_unpin = (u64)-1; | |
cb83b7b8 | 659 | caching_ctl->progress = (u64)-1; |
9d66e233 | 660 | } else { |
291c7d2f JB |
661 | if (load_cache_only) { |
662 | cache->caching_ctl = NULL; | |
663 | cache->cached = BTRFS_CACHE_NO; | |
664 | } else { | |
665 | cache->cached = BTRFS_CACHE_STARTED; | |
4f69cb98 | 666 | cache->has_caching_ctl = 1; |
291c7d2f | 667 | } |
9d66e233 JB |
668 | } |
669 | spin_unlock(&cache->lock); | |
d0bd4560 JB |
670 | #ifdef CONFIG_BTRFS_DEBUG |
671 | if (ret == 1 && | |
672 | btrfs_should_fragment_free_space(fs_info->extent_root, | |
673 | cache)) { | |
674 | u64 bytes_used; | |
675 | ||
676 | spin_lock(&cache->space_info->lock); | |
677 | spin_lock(&cache->lock); | |
678 | bytes_used = cache->key.offset - | |
679 | btrfs_block_group_used(&cache->item); | |
680 | cache->space_info->bytes_used += bytes_used >> 1; | |
681 | spin_unlock(&cache->lock); | |
682 | spin_unlock(&cache->space_info->lock); | |
683 | fragment_free_space(fs_info->extent_root, cache); | |
684 | } | |
685 | #endif | |
cb83b7b8 JB |
686 | mutex_unlock(&caching_ctl->mutex); |
687 | ||
291c7d2f | 688 | wake_up(&caching_ctl->wait); |
3c14874a | 689 | if (ret == 1) { |
291c7d2f | 690 | put_caching_control(caching_ctl); |
3c14874a | 691 | free_excluded_extents(fs_info->extent_root, cache); |
9d66e233 | 692 | return 0; |
3c14874a | 693 | } |
291c7d2f JB |
694 | } else { |
695 | /* | |
1e144fb8 OS |
696 | * We're either using the free space tree or no caching at all. |
697 | * Set cached to the appropriate value and wakeup any waiters. | |
291c7d2f JB |
698 | */ |
699 | spin_lock(&cache->lock); | |
700 | if (load_cache_only) { | |
701 | cache->caching_ctl = NULL; | |
702 | cache->cached = BTRFS_CACHE_NO; | |
703 | } else { | |
704 | cache->cached = BTRFS_CACHE_STARTED; | |
4f69cb98 | 705 | cache->has_caching_ctl = 1; |
291c7d2f JB |
706 | } |
707 | spin_unlock(&cache->lock); | |
708 | wake_up(&caching_ctl->wait); | |
9d66e233 JB |
709 | } |
710 | ||
291c7d2f JB |
711 | if (load_cache_only) { |
712 | put_caching_control(caching_ctl); | |
11833d66 | 713 | return 0; |
817d52f8 | 714 | } |
817d52f8 | 715 | |
9e351cc8 | 716 | down_write(&fs_info->commit_root_sem); |
291c7d2f | 717 | atomic_inc(&caching_ctl->count); |
11833d66 | 718 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); |
9e351cc8 | 719 | up_write(&fs_info->commit_root_sem); |
11833d66 | 720 | |
11dfe35a | 721 | btrfs_get_block_group(cache); |
11833d66 | 722 | |
e66f0bb1 | 723 | btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work); |
817d52f8 | 724 | |
ef8bbdfe | 725 | return ret; |
e37c9e69 CM |
726 | } |
727 | ||
0f9dd46c JB |
728 | /* |
729 | * return the block group that starts at or after bytenr | |
730 | */ | |
d397712b CM |
731 | static struct btrfs_block_group_cache * |
732 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) | |
0ef3e66b | 733 | { |
e2c89907 | 734 | return block_group_cache_tree_search(info, bytenr, 0); |
0ef3e66b CM |
735 | } |
736 | ||
0f9dd46c | 737 | /* |
9f55684c | 738 | * return the block group that contains the given bytenr |
0f9dd46c | 739 | */ |
d397712b CM |
740 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
741 | struct btrfs_fs_info *info, | |
742 | u64 bytenr) | |
be744175 | 743 | { |
e2c89907 | 744 | return block_group_cache_tree_search(info, bytenr, 1); |
be744175 | 745 | } |
0b86a832 | 746 | |
0f9dd46c JB |
747 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
748 | u64 flags) | |
6324fbf3 | 749 | { |
0f9dd46c | 750 | struct list_head *head = &info->space_info; |
0f9dd46c | 751 | struct btrfs_space_info *found; |
4184ea7f | 752 | |
52ba6929 | 753 | flags &= BTRFS_BLOCK_GROUP_TYPE_MASK; |
b742bb82 | 754 | |
4184ea7f CM |
755 | rcu_read_lock(); |
756 | list_for_each_entry_rcu(found, head, list) { | |
67377734 | 757 | if (found->flags & flags) { |
4184ea7f | 758 | rcu_read_unlock(); |
0f9dd46c | 759 | return found; |
4184ea7f | 760 | } |
0f9dd46c | 761 | } |
4184ea7f | 762 | rcu_read_unlock(); |
0f9dd46c | 763 | return NULL; |
6324fbf3 CM |
764 | } |
765 | ||
4184ea7f CM |
766 | /* |
767 | * after adding space to the filesystem, we need to clear the full flags | |
768 | * on all the space infos. | |
769 | */ | |
770 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) | |
771 | { | |
772 | struct list_head *head = &info->space_info; | |
773 | struct btrfs_space_info *found; | |
774 | ||
775 | rcu_read_lock(); | |
776 | list_for_each_entry_rcu(found, head, list) | |
777 | found->full = 0; | |
778 | rcu_read_unlock(); | |
779 | } | |
780 | ||
1a4ed8fd FM |
781 | /* simple helper to search for an existing data extent at a given offset */ |
782 | int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len) | |
e02119d5 | 783 | { |
0b246afa | 784 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 CM |
785 | int ret; |
786 | struct btrfs_key key; | |
31840ae1 | 787 | struct btrfs_path *path; |
e02119d5 | 788 | |
31840ae1 | 789 | path = btrfs_alloc_path(); |
d8926bb3 MF |
790 | if (!path) |
791 | return -ENOMEM; | |
792 | ||
e02119d5 CM |
793 | key.objectid = start; |
794 | key.offset = len; | |
3173a18f | 795 | key.type = BTRFS_EXTENT_ITEM_KEY; |
0b246afa | 796 | ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0); |
31840ae1 | 797 | btrfs_free_path(path); |
7bb86316 CM |
798 | return ret; |
799 | } | |
800 | ||
a22285a6 | 801 | /* |
3173a18f | 802 | * helper function to lookup reference count and flags of a tree block. |
a22285a6 YZ |
803 | * |
804 | * the head node for delayed ref is used to store the sum of all the | |
805 | * reference count modifications queued up in the rbtree. the head | |
806 | * node may also store the extent flags to set. This way you can check | |
807 | * to see what the reference count and extent flags would be if all of | |
808 | * the delayed refs are not processed. | |
809 | */ | |
810 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, | |
811 | struct btrfs_root *root, u64 bytenr, | |
3173a18f | 812 | u64 offset, int metadata, u64 *refs, u64 *flags) |
a22285a6 | 813 | { |
0b246afa | 814 | struct btrfs_fs_info *fs_info = root->fs_info; |
a22285a6 YZ |
815 | struct btrfs_delayed_ref_head *head; |
816 | struct btrfs_delayed_ref_root *delayed_refs; | |
817 | struct btrfs_path *path; | |
818 | struct btrfs_extent_item *ei; | |
819 | struct extent_buffer *leaf; | |
820 | struct btrfs_key key; | |
821 | u32 item_size; | |
822 | u64 num_refs; | |
823 | u64 extent_flags; | |
824 | int ret; | |
825 | ||
3173a18f JB |
826 | /* |
827 | * If we don't have skinny metadata, don't bother doing anything | |
828 | * different | |
829 | */ | |
0b246afa JM |
830 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { |
831 | offset = fs_info->nodesize; | |
3173a18f JB |
832 | metadata = 0; |
833 | } | |
834 | ||
a22285a6 YZ |
835 | path = btrfs_alloc_path(); |
836 | if (!path) | |
837 | return -ENOMEM; | |
838 | ||
a22285a6 YZ |
839 | if (!trans) { |
840 | path->skip_locking = 1; | |
841 | path->search_commit_root = 1; | |
842 | } | |
639eefc8 FDBM |
843 | |
844 | search_again: | |
845 | key.objectid = bytenr; | |
846 | key.offset = offset; | |
847 | if (metadata) | |
848 | key.type = BTRFS_METADATA_ITEM_KEY; | |
849 | else | |
850 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
851 | ||
0b246afa | 852 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0); |
a22285a6 YZ |
853 | if (ret < 0) |
854 | goto out_free; | |
855 | ||
3173a18f | 856 | if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) { |
74be9510 FDBM |
857 | if (path->slots[0]) { |
858 | path->slots[0]--; | |
859 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
860 | path->slots[0]); | |
861 | if (key.objectid == bytenr && | |
862 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
0b246afa | 863 | key.offset == fs_info->nodesize) |
74be9510 FDBM |
864 | ret = 0; |
865 | } | |
3173a18f JB |
866 | } |
867 | ||
a22285a6 YZ |
868 | if (ret == 0) { |
869 | leaf = path->nodes[0]; | |
870 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
871 | if (item_size >= sizeof(*ei)) { | |
872 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
873 | struct btrfs_extent_item); | |
874 | num_refs = btrfs_extent_refs(leaf, ei); | |
875 | extent_flags = btrfs_extent_flags(leaf, ei); | |
876 | } else { | |
877 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
878 | struct btrfs_extent_item_v0 *ei0; | |
879 | BUG_ON(item_size != sizeof(*ei0)); | |
880 | ei0 = btrfs_item_ptr(leaf, path->slots[0], | |
881 | struct btrfs_extent_item_v0); | |
882 | num_refs = btrfs_extent_refs_v0(leaf, ei0); | |
883 | /* FIXME: this isn't correct for data */ | |
884 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
885 | #else | |
886 | BUG(); | |
887 | #endif | |
888 | } | |
889 | BUG_ON(num_refs == 0); | |
890 | } else { | |
891 | num_refs = 0; | |
892 | extent_flags = 0; | |
893 | ret = 0; | |
894 | } | |
895 | ||
896 | if (!trans) | |
897 | goto out; | |
898 | ||
899 | delayed_refs = &trans->transaction->delayed_refs; | |
900 | spin_lock(&delayed_refs->lock); | |
901 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
902 | if (head) { | |
903 | if (!mutex_trylock(&head->mutex)) { | |
904 | atomic_inc(&head->node.refs); | |
905 | spin_unlock(&delayed_refs->lock); | |
906 | ||
b3b4aa74 | 907 | btrfs_release_path(path); |
a22285a6 | 908 | |
8cc33e5c DS |
909 | /* |
910 | * Mutex was contended, block until it's released and try | |
911 | * again | |
912 | */ | |
a22285a6 YZ |
913 | mutex_lock(&head->mutex); |
914 | mutex_unlock(&head->mutex); | |
915 | btrfs_put_delayed_ref(&head->node); | |
639eefc8 | 916 | goto search_again; |
a22285a6 | 917 | } |
d7df2c79 | 918 | spin_lock(&head->lock); |
a22285a6 YZ |
919 | if (head->extent_op && head->extent_op->update_flags) |
920 | extent_flags |= head->extent_op->flags_to_set; | |
921 | else | |
922 | BUG_ON(num_refs == 0); | |
923 | ||
924 | num_refs += head->node.ref_mod; | |
d7df2c79 | 925 | spin_unlock(&head->lock); |
a22285a6 YZ |
926 | mutex_unlock(&head->mutex); |
927 | } | |
928 | spin_unlock(&delayed_refs->lock); | |
929 | out: | |
930 | WARN_ON(num_refs == 0); | |
931 | if (refs) | |
932 | *refs = num_refs; | |
933 | if (flags) | |
934 | *flags = extent_flags; | |
935 | out_free: | |
936 | btrfs_free_path(path); | |
937 | return ret; | |
938 | } | |
939 | ||
d8d5f3e1 CM |
940 | /* |
941 | * Back reference rules. Back refs have three main goals: | |
942 | * | |
943 | * 1) differentiate between all holders of references to an extent so that | |
944 | * when a reference is dropped we can make sure it was a valid reference | |
945 | * before freeing the extent. | |
946 | * | |
947 | * 2) Provide enough information to quickly find the holders of an extent | |
948 | * if we notice a given block is corrupted or bad. | |
949 | * | |
950 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
951 | * maintenance. This is actually the same as #2, but with a slightly | |
952 | * different use case. | |
953 | * | |
5d4f98a2 YZ |
954 | * There are two kinds of back refs. The implicit back refs is optimized |
955 | * for pointers in non-shared tree blocks. For a given pointer in a block, | |
956 | * back refs of this kind provide information about the block's owner tree | |
957 | * and the pointer's key. These information allow us to find the block by | |
958 | * b-tree searching. The full back refs is for pointers in tree blocks not | |
959 | * referenced by their owner trees. The location of tree block is recorded | |
960 | * in the back refs. Actually the full back refs is generic, and can be | |
961 | * used in all cases the implicit back refs is used. The major shortcoming | |
962 | * of the full back refs is its overhead. Every time a tree block gets | |
963 | * COWed, we have to update back refs entry for all pointers in it. | |
964 | * | |
965 | * For a newly allocated tree block, we use implicit back refs for | |
966 | * pointers in it. This means most tree related operations only involve | |
967 | * implicit back refs. For a tree block created in old transaction, the | |
968 | * only way to drop a reference to it is COW it. So we can detect the | |
969 | * event that tree block loses its owner tree's reference and do the | |
970 | * back refs conversion. | |
971 | * | |
01327610 | 972 | * When a tree block is COWed through a tree, there are four cases: |
5d4f98a2 YZ |
973 | * |
974 | * The reference count of the block is one and the tree is the block's | |
975 | * owner tree. Nothing to do in this case. | |
976 | * | |
977 | * The reference count of the block is one and the tree is not the | |
978 | * block's owner tree. In this case, full back refs is used for pointers | |
979 | * in the block. Remove these full back refs, add implicit back refs for | |
980 | * every pointers in the new block. | |
981 | * | |
982 | * The reference count of the block is greater than one and the tree is | |
983 | * the block's owner tree. In this case, implicit back refs is used for | |
984 | * pointers in the block. Add full back refs for every pointers in the | |
985 | * block, increase lower level extents' reference counts. The original | |
986 | * implicit back refs are entailed to the new block. | |
987 | * | |
988 | * The reference count of the block is greater than one and the tree is | |
989 | * not the block's owner tree. Add implicit back refs for every pointer in | |
990 | * the new block, increase lower level extents' reference count. | |
991 | * | |
992 | * Back Reference Key composing: | |
993 | * | |
994 | * The key objectid corresponds to the first byte in the extent, | |
995 | * The key type is used to differentiate between types of back refs. | |
996 | * There are different meanings of the key offset for different types | |
997 | * of back refs. | |
998 | * | |
d8d5f3e1 CM |
999 | * File extents can be referenced by: |
1000 | * | |
1001 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
31840ae1 | 1002 | * - different files inside a single subvolume |
d8d5f3e1 CM |
1003 | * - different offsets inside a file (bookend extents in file.c) |
1004 | * | |
5d4f98a2 | 1005 | * The extent ref structure for the implicit back refs has fields for: |
d8d5f3e1 CM |
1006 | * |
1007 | * - Objectid of the subvolume root | |
d8d5f3e1 | 1008 | * - objectid of the file holding the reference |
5d4f98a2 YZ |
1009 | * - original offset in the file |
1010 | * - how many bookend extents | |
d8d5f3e1 | 1011 | * |
5d4f98a2 YZ |
1012 | * The key offset for the implicit back refs is hash of the first |
1013 | * three fields. | |
d8d5f3e1 | 1014 | * |
5d4f98a2 | 1015 | * The extent ref structure for the full back refs has field for: |
d8d5f3e1 | 1016 | * |
5d4f98a2 | 1017 | * - number of pointers in the tree leaf |
d8d5f3e1 | 1018 | * |
5d4f98a2 YZ |
1019 | * The key offset for the implicit back refs is the first byte of |
1020 | * the tree leaf | |
d8d5f3e1 | 1021 | * |
5d4f98a2 YZ |
1022 | * When a file extent is allocated, The implicit back refs is used. |
1023 | * the fields are filled in: | |
d8d5f3e1 | 1024 | * |
5d4f98a2 | 1025 | * (root_key.objectid, inode objectid, offset in file, 1) |
d8d5f3e1 | 1026 | * |
5d4f98a2 YZ |
1027 | * When a file extent is removed file truncation, we find the |
1028 | * corresponding implicit back refs and check the following fields: | |
d8d5f3e1 | 1029 | * |
5d4f98a2 | 1030 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
d8d5f3e1 | 1031 | * |
5d4f98a2 | 1032 | * Btree extents can be referenced by: |
d8d5f3e1 | 1033 | * |
5d4f98a2 | 1034 | * - Different subvolumes |
d8d5f3e1 | 1035 | * |
5d4f98a2 YZ |
1036 | * Both the implicit back refs and the full back refs for tree blocks |
1037 | * only consist of key. The key offset for the implicit back refs is | |
1038 | * objectid of block's owner tree. The key offset for the full back refs | |
1039 | * is the first byte of parent block. | |
d8d5f3e1 | 1040 | * |
5d4f98a2 YZ |
1041 | * When implicit back refs is used, information about the lowest key and |
1042 | * level of the tree block are required. These information are stored in | |
1043 | * tree block info structure. | |
d8d5f3e1 | 1044 | */ |
31840ae1 | 1045 | |
5d4f98a2 YZ |
1046 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
1047 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, | |
1048 | struct btrfs_root *root, | |
1049 | struct btrfs_path *path, | |
1050 | u64 owner, u32 extra_size) | |
7bb86316 | 1051 | { |
5d4f98a2 YZ |
1052 | struct btrfs_extent_item *item; |
1053 | struct btrfs_extent_item_v0 *ei0; | |
1054 | struct btrfs_extent_ref_v0 *ref0; | |
1055 | struct btrfs_tree_block_info *bi; | |
1056 | struct extent_buffer *leaf; | |
7bb86316 | 1057 | struct btrfs_key key; |
5d4f98a2 YZ |
1058 | struct btrfs_key found_key; |
1059 | u32 new_size = sizeof(*item); | |
1060 | u64 refs; | |
1061 | int ret; | |
1062 | ||
1063 | leaf = path->nodes[0]; | |
1064 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); | |
1065 | ||
1066 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1067 | ei0 = btrfs_item_ptr(leaf, path->slots[0], | |
1068 | struct btrfs_extent_item_v0); | |
1069 | refs = btrfs_extent_refs_v0(leaf, ei0); | |
1070 | ||
1071 | if (owner == (u64)-1) { | |
1072 | while (1) { | |
1073 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
1074 | ret = btrfs_next_leaf(root, path); | |
1075 | if (ret < 0) | |
1076 | return ret; | |
79787eaa | 1077 | BUG_ON(ret > 0); /* Corruption */ |
5d4f98a2 YZ |
1078 | leaf = path->nodes[0]; |
1079 | } | |
1080 | btrfs_item_key_to_cpu(leaf, &found_key, | |
1081 | path->slots[0]); | |
1082 | BUG_ON(key.objectid != found_key.objectid); | |
1083 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { | |
1084 | path->slots[0]++; | |
1085 | continue; | |
1086 | } | |
1087 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1088 | struct btrfs_extent_ref_v0); | |
1089 | owner = btrfs_ref_objectid_v0(leaf, ref0); | |
1090 | break; | |
1091 | } | |
1092 | } | |
b3b4aa74 | 1093 | btrfs_release_path(path); |
5d4f98a2 YZ |
1094 | |
1095 | if (owner < BTRFS_FIRST_FREE_OBJECTID) | |
1096 | new_size += sizeof(*bi); | |
1097 | ||
1098 | new_size -= sizeof(*ei0); | |
1099 | ret = btrfs_search_slot(trans, root, &key, path, | |
1100 | new_size + extra_size, 1); | |
1101 | if (ret < 0) | |
1102 | return ret; | |
79787eaa | 1103 | BUG_ON(ret); /* Corruption */ |
5d4f98a2 | 1104 | |
4b90c680 | 1105 | btrfs_extend_item(root, path, new_size); |
5d4f98a2 YZ |
1106 | |
1107 | leaf = path->nodes[0]; | |
1108 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1109 | btrfs_set_extent_refs(leaf, item, refs); | |
1110 | /* FIXME: get real generation */ | |
1111 | btrfs_set_extent_generation(leaf, item, 0); | |
1112 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1113 | btrfs_set_extent_flags(leaf, item, | |
1114 | BTRFS_EXTENT_FLAG_TREE_BLOCK | | |
1115 | BTRFS_BLOCK_FLAG_FULL_BACKREF); | |
1116 | bi = (struct btrfs_tree_block_info *)(item + 1); | |
1117 | /* FIXME: get first key of the block */ | |
b159fa28 | 1118 | memzero_extent_buffer(leaf, (unsigned long)bi, sizeof(*bi)); |
5d4f98a2 YZ |
1119 | btrfs_set_tree_block_level(leaf, bi, (int)owner); |
1120 | } else { | |
1121 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); | |
1122 | } | |
1123 | btrfs_mark_buffer_dirty(leaf); | |
1124 | return 0; | |
1125 | } | |
1126 | #endif | |
1127 | ||
1128 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) | |
1129 | { | |
1130 | u32 high_crc = ~(u32)0; | |
1131 | u32 low_crc = ~(u32)0; | |
1132 | __le64 lenum; | |
1133 | ||
1134 | lenum = cpu_to_le64(root_objectid); | |
14a958e6 | 1135 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 1136 | lenum = cpu_to_le64(owner); |
14a958e6 | 1137 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 1138 | lenum = cpu_to_le64(offset); |
14a958e6 | 1139 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 YZ |
1140 | |
1141 | return ((u64)high_crc << 31) ^ (u64)low_crc; | |
1142 | } | |
1143 | ||
1144 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, | |
1145 | struct btrfs_extent_data_ref *ref) | |
1146 | { | |
1147 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), | |
1148 | btrfs_extent_data_ref_objectid(leaf, ref), | |
1149 | btrfs_extent_data_ref_offset(leaf, ref)); | |
1150 | } | |
1151 | ||
1152 | static int match_extent_data_ref(struct extent_buffer *leaf, | |
1153 | struct btrfs_extent_data_ref *ref, | |
1154 | u64 root_objectid, u64 owner, u64 offset) | |
1155 | { | |
1156 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || | |
1157 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || | |
1158 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
1159 | return 0; | |
1160 | return 1; | |
1161 | } | |
1162 | ||
1163 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, | |
1164 | struct btrfs_root *root, | |
1165 | struct btrfs_path *path, | |
1166 | u64 bytenr, u64 parent, | |
1167 | u64 root_objectid, | |
1168 | u64 owner, u64 offset) | |
1169 | { | |
1170 | struct btrfs_key key; | |
1171 | struct btrfs_extent_data_ref *ref; | |
31840ae1 | 1172 | struct extent_buffer *leaf; |
5d4f98a2 | 1173 | u32 nritems; |
74493f7a | 1174 | int ret; |
5d4f98a2 YZ |
1175 | int recow; |
1176 | int err = -ENOENT; | |
74493f7a | 1177 | |
31840ae1 | 1178 | key.objectid = bytenr; |
5d4f98a2 YZ |
1179 | if (parent) { |
1180 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
1181 | key.offset = parent; | |
1182 | } else { | |
1183 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
1184 | key.offset = hash_extent_data_ref(root_objectid, | |
1185 | owner, offset); | |
1186 | } | |
1187 | again: | |
1188 | recow = 0; | |
1189 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1190 | if (ret < 0) { | |
1191 | err = ret; | |
1192 | goto fail; | |
1193 | } | |
31840ae1 | 1194 | |
5d4f98a2 YZ |
1195 | if (parent) { |
1196 | if (!ret) | |
1197 | return 0; | |
1198 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1199 | key.type = BTRFS_EXTENT_REF_V0_KEY; | |
b3b4aa74 | 1200 | btrfs_release_path(path); |
5d4f98a2 YZ |
1201 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1202 | if (ret < 0) { | |
1203 | err = ret; | |
1204 | goto fail; | |
1205 | } | |
1206 | if (!ret) | |
1207 | return 0; | |
1208 | #endif | |
1209 | goto fail; | |
31840ae1 ZY |
1210 | } |
1211 | ||
1212 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
1213 | nritems = btrfs_header_nritems(leaf); |
1214 | while (1) { | |
1215 | if (path->slots[0] >= nritems) { | |
1216 | ret = btrfs_next_leaf(root, path); | |
1217 | if (ret < 0) | |
1218 | err = ret; | |
1219 | if (ret) | |
1220 | goto fail; | |
1221 | ||
1222 | leaf = path->nodes[0]; | |
1223 | nritems = btrfs_header_nritems(leaf); | |
1224 | recow = 1; | |
1225 | } | |
1226 | ||
1227 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1228 | if (key.objectid != bytenr || | |
1229 | key.type != BTRFS_EXTENT_DATA_REF_KEY) | |
1230 | goto fail; | |
1231 | ||
1232 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1233 | struct btrfs_extent_data_ref); | |
1234 | ||
1235 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
1236 | owner, offset)) { | |
1237 | if (recow) { | |
b3b4aa74 | 1238 | btrfs_release_path(path); |
5d4f98a2 YZ |
1239 | goto again; |
1240 | } | |
1241 | err = 0; | |
1242 | break; | |
1243 | } | |
1244 | path->slots[0]++; | |
31840ae1 | 1245 | } |
5d4f98a2 YZ |
1246 | fail: |
1247 | return err; | |
31840ae1 ZY |
1248 | } |
1249 | ||
5d4f98a2 YZ |
1250 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
1251 | struct btrfs_root *root, | |
1252 | struct btrfs_path *path, | |
1253 | u64 bytenr, u64 parent, | |
1254 | u64 root_objectid, u64 owner, | |
1255 | u64 offset, int refs_to_add) | |
31840ae1 ZY |
1256 | { |
1257 | struct btrfs_key key; | |
1258 | struct extent_buffer *leaf; | |
5d4f98a2 | 1259 | u32 size; |
31840ae1 ZY |
1260 | u32 num_refs; |
1261 | int ret; | |
74493f7a | 1262 | |
74493f7a | 1263 | key.objectid = bytenr; |
5d4f98a2 YZ |
1264 | if (parent) { |
1265 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
1266 | key.offset = parent; | |
1267 | size = sizeof(struct btrfs_shared_data_ref); | |
1268 | } else { | |
1269 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
1270 | key.offset = hash_extent_data_ref(root_objectid, | |
1271 | owner, offset); | |
1272 | size = sizeof(struct btrfs_extent_data_ref); | |
1273 | } | |
74493f7a | 1274 | |
5d4f98a2 YZ |
1275 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
1276 | if (ret && ret != -EEXIST) | |
1277 | goto fail; | |
1278 | ||
1279 | leaf = path->nodes[0]; | |
1280 | if (parent) { | |
1281 | struct btrfs_shared_data_ref *ref; | |
31840ae1 | 1282 | ref = btrfs_item_ptr(leaf, path->slots[0], |
5d4f98a2 YZ |
1283 | struct btrfs_shared_data_ref); |
1284 | if (ret == 0) { | |
1285 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); | |
1286 | } else { | |
1287 | num_refs = btrfs_shared_data_ref_count(leaf, ref); | |
1288 | num_refs += refs_to_add; | |
1289 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 1290 | } |
5d4f98a2 YZ |
1291 | } else { |
1292 | struct btrfs_extent_data_ref *ref; | |
1293 | while (ret == -EEXIST) { | |
1294 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1295 | struct btrfs_extent_data_ref); | |
1296 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
1297 | owner, offset)) | |
1298 | break; | |
b3b4aa74 | 1299 | btrfs_release_path(path); |
5d4f98a2 YZ |
1300 | key.offset++; |
1301 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1302 | size); | |
1303 | if (ret && ret != -EEXIST) | |
1304 | goto fail; | |
31840ae1 | 1305 | |
5d4f98a2 YZ |
1306 | leaf = path->nodes[0]; |
1307 | } | |
1308 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1309 | struct btrfs_extent_data_ref); | |
1310 | if (ret == 0) { | |
1311 | btrfs_set_extent_data_ref_root(leaf, ref, | |
1312 | root_objectid); | |
1313 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
1314 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
1315 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); | |
1316 | } else { | |
1317 | num_refs = btrfs_extent_data_ref_count(leaf, ref); | |
1318 | num_refs += refs_to_add; | |
1319 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 1320 | } |
31840ae1 | 1321 | } |
5d4f98a2 YZ |
1322 | btrfs_mark_buffer_dirty(leaf); |
1323 | ret = 0; | |
1324 | fail: | |
b3b4aa74 | 1325 | btrfs_release_path(path); |
7bb86316 | 1326 | return ret; |
74493f7a CM |
1327 | } |
1328 | ||
5d4f98a2 YZ |
1329 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
1330 | struct btrfs_root *root, | |
1331 | struct btrfs_path *path, | |
fcebe456 | 1332 | int refs_to_drop, int *last_ref) |
31840ae1 | 1333 | { |
5d4f98a2 YZ |
1334 | struct btrfs_key key; |
1335 | struct btrfs_extent_data_ref *ref1 = NULL; | |
1336 | struct btrfs_shared_data_ref *ref2 = NULL; | |
31840ae1 | 1337 | struct extent_buffer *leaf; |
5d4f98a2 | 1338 | u32 num_refs = 0; |
31840ae1 ZY |
1339 | int ret = 0; |
1340 | ||
1341 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
1342 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
1343 | ||
1344 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1345 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
1346 | struct btrfs_extent_data_ref); | |
1347 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1348 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
1349 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
1350 | struct btrfs_shared_data_ref); | |
1351 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1352 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1353 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | |
1354 | struct btrfs_extent_ref_v0 *ref0; | |
1355 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1356 | struct btrfs_extent_ref_v0); | |
1357 | num_refs = btrfs_ref_count_v0(leaf, ref0); | |
1358 | #endif | |
1359 | } else { | |
1360 | BUG(); | |
1361 | } | |
1362 | ||
56bec294 CM |
1363 | BUG_ON(num_refs < refs_to_drop); |
1364 | num_refs -= refs_to_drop; | |
5d4f98a2 | 1365 | |
31840ae1 ZY |
1366 | if (num_refs == 0) { |
1367 | ret = btrfs_del_item(trans, root, path); | |
fcebe456 | 1368 | *last_ref = 1; |
31840ae1 | 1369 | } else { |
5d4f98a2 YZ |
1370 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
1371 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); | |
1372 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) | |
1373 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); | |
1374 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1375 | else { | |
1376 | struct btrfs_extent_ref_v0 *ref0; | |
1377 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1378 | struct btrfs_extent_ref_v0); | |
1379 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); | |
1380 | } | |
1381 | #endif | |
31840ae1 ZY |
1382 | btrfs_mark_buffer_dirty(leaf); |
1383 | } | |
31840ae1 ZY |
1384 | return ret; |
1385 | } | |
1386 | ||
9ed0dea0 | 1387 | static noinline u32 extent_data_ref_count(struct btrfs_path *path, |
5d4f98a2 | 1388 | struct btrfs_extent_inline_ref *iref) |
15916de8 | 1389 | { |
5d4f98a2 YZ |
1390 | struct btrfs_key key; |
1391 | struct extent_buffer *leaf; | |
1392 | struct btrfs_extent_data_ref *ref1; | |
1393 | struct btrfs_shared_data_ref *ref2; | |
1394 | u32 num_refs = 0; | |
1395 | ||
1396 | leaf = path->nodes[0]; | |
1397 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1398 | if (iref) { | |
1399 | if (btrfs_extent_inline_ref_type(leaf, iref) == | |
1400 | BTRFS_EXTENT_DATA_REF_KEY) { | |
1401 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1402 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1403 | } else { | |
1404 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); | |
1405 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1406 | } | |
1407 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1408 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
1409 | struct btrfs_extent_data_ref); | |
1410 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1411 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
1412 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
1413 | struct btrfs_shared_data_ref); | |
1414 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1415 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1416 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | |
1417 | struct btrfs_extent_ref_v0 *ref0; | |
1418 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1419 | struct btrfs_extent_ref_v0); | |
1420 | num_refs = btrfs_ref_count_v0(leaf, ref0); | |
4b4e25f2 | 1421 | #endif |
5d4f98a2 YZ |
1422 | } else { |
1423 | WARN_ON(1); | |
1424 | } | |
1425 | return num_refs; | |
1426 | } | |
15916de8 | 1427 | |
5d4f98a2 YZ |
1428 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
1429 | struct btrfs_root *root, | |
1430 | struct btrfs_path *path, | |
1431 | u64 bytenr, u64 parent, | |
1432 | u64 root_objectid) | |
1f3c79a2 | 1433 | { |
5d4f98a2 | 1434 | struct btrfs_key key; |
1f3c79a2 | 1435 | int ret; |
1f3c79a2 | 1436 | |
5d4f98a2 YZ |
1437 | key.objectid = bytenr; |
1438 | if (parent) { | |
1439 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1440 | key.offset = parent; | |
1441 | } else { | |
1442 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
1443 | key.offset = root_objectid; | |
1f3c79a2 LH |
1444 | } |
1445 | ||
5d4f98a2 YZ |
1446 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1447 | if (ret > 0) | |
1448 | ret = -ENOENT; | |
1449 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1450 | if (ret == -ENOENT && parent) { | |
b3b4aa74 | 1451 | btrfs_release_path(path); |
5d4f98a2 YZ |
1452 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
1453 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1454 | if (ret > 0) | |
1455 | ret = -ENOENT; | |
1456 | } | |
1f3c79a2 | 1457 | #endif |
5d4f98a2 | 1458 | return ret; |
1f3c79a2 LH |
1459 | } |
1460 | ||
5d4f98a2 YZ |
1461 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
1462 | struct btrfs_root *root, | |
1463 | struct btrfs_path *path, | |
1464 | u64 bytenr, u64 parent, | |
1465 | u64 root_objectid) | |
31840ae1 | 1466 | { |
5d4f98a2 | 1467 | struct btrfs_key key; |
31840ae1 | 1468 | int ret; |
31840ae1 | 1469 | |
5d4f98a2 YZ |
1470 | key.objectid = bytenr; |
1471 | if (parent) { | |
1472 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1473 | key.offset = parent; | |
1474 | } else { | |
1475 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
1476 | key.offset = root_objectid; | |
1477 | } | |
1478 | ||
1479 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
b3b4aa74 | 1480 | btrfs_release_path(path); |
31840ae1 ZY |
1481 | return ret; |
1482 | } | |
1483 | ||
5d4f98a2 | 1484 | static inline int extent_ref_type(u64 parent, u64 owner) |
31840ae1 | 1485 | { |
5d4f98a2 YZ |
1486 | int type; |
1487 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1488 | if (parent > 0) | |
1489 | type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1490 | else | |
1491 | type = BTRFS_TREE_BLOCK_REF_KEY; | |
1492 | } else { | |
1493 | if (parent > 0) | |
1494 | type = BTRFS_SHARED_DATA_REF_KEY; | |
1495 | else | |
1496 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
1497 | } | |
1498 | return type; | |
31840ae1 | 1499 | } |
56bec294 | 1500 | |
2c47e605 YZ |
1501 | static int find_next_key(struct btrfs_path *path, int level, |
1502 | struct btrfs_key *key) | |
56bec294 | 1503 | |
02217ed2 | 1504 | { |
2c47e605 | 1505 | for (; level < BTRFS_MAX_LEVEL; level++) { |
5d4f98a2 YZ |
1506 | if (!path->nodes[level]) |
1507 | break; | |
5d4f98a2 YZ |
1508 | if (path->slots[level] + 1 >= |
1509 | btrfs_header_nritems(path->nodes[level])) | |
1510 | continue; | |
1511 | if (level == 0) | |
1512 | btrfs_item_key_to_cpu(path->nodes[level], key, | |
1513 | path->slots[level] + 1); | |
1514 | else | |
1515 | btrfs_node_key_to_cpu(path->nodes[level], key, | |
1516 | path->slots[level] + 1); | |
1517 | return 0; | |
1518 | } | |
1519 | return 1; | |
1520 | } | |
037e6390 | 1521 | |
5d4f98a2 YZ |
1522 | /* |
1523 | * look for inline back ref. if back ref is found, *ref_ret is set | |
1524 | * to the address of inline back ref, and 0 is returned. | |
1525 | * | |
1526 | * if back ref isn't found, *ref_ret is set to the address where it | |
1527 | * should be inserted, and -ENOENT is returned. | |
1528 | * | |
1529 | * if insert is true and there are too many inline back refs, the path | |
1530 | * points to the extent item, and -EAGAIN is returned. | |
1531 | * | |
1532 | * NOTE: inline back refs are ordered in the same way that back ref | |
1533 | * items in the tree are ordered. | |
1534 | */ | |
1535 | static noinline_for_stack | |
1536 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1537 | struct btrfs_root *root, | |
1538 | struct btrfs_path *path, | |
1539 | struct btrfs_extent_inline_ref **ref_ret, | |
1540 | u64 bytenr, u64 num_bytes, | |
1541 | u64 parent, u64 root_objectid, | |
1542 | u64 owner, u64 offset, int insert) | |
1543 | { | |
0b246afa | 1544 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
1545 | struct btrfs_key key; |
1546 | struct extent_buffer *leaf; | |
1547 | struct btrfs_extent_item *ei; | |
1548 | struct btrfs_extent_inline_ref *iref; | |
1549 | u64 flags; | |
1550 | u64 item_size; | |
1551 | unsigned long ptr; | |
1552 | unsigned long end; | |
1553 | int extra_size; | |
1554 | int type; | |
1555 | int want; | |
1556 | int ret; | |
1557 | int err = 0; | |
0b246afa | 1558 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
26b8003f | 1559 | |
db94535d | 1560 | key.objectid = bytenr; |
31840ae1 | 1561 | key.type = BTRFS_EXTENT_ITEM_KEY; |
56bec294 | 1562 | key.offset = num_bytes; |
31840ae1 | 1563 | |
5d4f98a2 YZ |
1564 | want = extent_ref_type(parent, owner); |
1565 | if (insert) { | |
1566 | extra_size = btrfs_extent_inline_ref_size(want); | |
85d4198e | 1567 | path->keep_locks = 1; |
5d4f98a2 YZ |
1568 | } else |
1569 | extra_size = -1; | |
3173a18f JB |
1570 | |
1571 | /* | |
1572 | * Owner is our parent level, so we can just add one to get the level | |
1573 | * for the block we are interested in. | |
1574 | */ | |
1575 | if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1576 | key.type = BTRFS_METADATA_ITEM_KEY; | |
1577 | key.offset = owner; | |
1578 | } | |
1579 | ||
1580 | again: | |
5d4f98a2 | 1581 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); |
b9473439 | 1582 | if (ret < 0) { |
5d4f98a2 YZ |
1583 | err = ret; |
1584 | goto out; | |
1585 | } | |
3173a18f JB |
1586 | |
1587 | /* | |
1588 | * We may be a newly converted file system which still has the old fat | |
1589 | * extent entries for metadata, so try and see if we have one of those. | |
1590 | */ | |
1591 | if (ret > 0 && skinny_metadata) { | |
1592 | skinny_metadata = false; | |
1593 | if (path->slots[0]) { | |
1594 | path->slots[0]--; | |
1595 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
1596 | path->slots[0]); | |
1597 | if (key.objectid == bytenr && | |
1598 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
1599 | key.offset == num_bytes) | |
1600 | ret = 0; | |
1601 | } | |
1602 | if (ret) { | |
9ce49a0b | 1603 | key.objectid = bytenr; |
3173a18f JB |
1604 | key.type = BTRFS_EXTENT_ITEM_KEY; |
1605 | key.offset = num_bytes; | |
1606 | btrfs_release_path(path); | |
1607 | goto again; | |
1608 | } | |
1609 | } | |
1610 | ||
79787eaa JM |
1611 | if (ret && !insert) { |
1612 | err = -ENOENT; | |
1613 | goto out; | |
fae7f21c | 1614 | } else if (WARN_ON(ret)) { |
492104c8 | 1615 | err = -EIO; |
492104c8 | 1616 | goto out; |
79787eaa | 1617 | } |
5d4f98a2 YZ |
1618 | |
1619 | leaf = path->nodes[0]; | |
1620 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1621 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1622 | if (item_size < sizeof(*ei)) { | |
1623 | if (!insert) { | |
1624 | err = -ENOENT; | |
1625 | goto out; | |
1626 | } | |
1627 | ret = convert_extent_item_v0(trans, root, path, owner, | |
1628 | extra_size); | |
1629 | if (ret < 0) { | |
1630 | err = ret; | |
1631 | goto out; | |
1632 | } | |
1633 | leaf = path->nodes[0]; | |
1634 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1635 | } | |
1636 | #endif | |
1637 | BUG_ON(item_size < sizeof(*ei)); | |
1638 | ||
5d4f98a2 YZ |
1639 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1640 | flags = btrfs_extent_flags(leaf, ei); | |
1641 | ||
1642 | ptr = (unsigned long)(ei + 1); | |
1643 | end = (unsigned long)ei + item_size; | |
1644 | ||
3173a18f | 1645 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) { |
5d4f98a2 YZ |
1646 | ptr += sizeof(struct btrfs_tree_block_info); |
1647 | BUG_ON(ptr > end); | |
5d4f98a2 YZ |
1648 | } |
1649 | ||
1650 | err = -ENOENT; | |
1651 | while (1) { | |
1652 | if (ptr >= end) { | |
1653 | WARN_ON(ptr > end); | |
1654 | break; | |
1655 | } | |
1656 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
1657 | type = btrfs_extent_inline_ref_type(leaf, iref); | |
1658 | if (want < type) | |
1659 | break; | |
1660 | if (want > type) { | |
1661 | ptr += btrfs_extent_inline_ref_size(type); | |
1662 | continue; | |
1663 | } | |
1664 | ||
1665 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1666 | struct btrfs_extent_data_ref *dref; | |
1667 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1668 | if (match_extent_data_ref(leaf, dref, root_objectid, | |
1669 | owner, offset)) { | |
1670 | err = 0; | |
1671 | break; | |
1672 | } | |
1673 | if (hash_extent_data_ref_item(leaf, dref) < | |
1674 | hash_extent_data_ref(root_objectid, owner, offset)) | |
1675 | break; | |
1676 | } else { | |
1677 | u64 ref_offset; | |
1678 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); | |
1679 | if (parent > 0) { | |
1680 | if (parent == ref_offset) { | |
1681 | err = 0; | |
1682 | break; | |
1683 | } | |
1684 | if (ref_offset < parent) | |
1685 | break; | |
1686 | } else { | |
1687 | if (root_objectid == ref_offset) { | |
1688 | err = 0; | |
1689 | break; | |
1690 | } | |
1691 | if (ref_offset < root_objectid) | |
1692 | break; | |
1693 | } | |
1694 | } | |
1695 | ptr += btrfs_extent_inline_ref_size(type); | |
1696 | } | |
1697 | if (err == -ENOENT && insert) { | |
1698 | if (item_size + extra_size >= | |
1699 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { | |
1700 | err = -EAGAIN; | |
1701 | goto out; | |
1702 | } | |
1703 | /* | |
1704 | * To add new inline back ref, we have to make sure | |
1705 | * there is no corresponding back ref item. | |
1706 | * For simplicity, we just do not add new inline back | |
1707 | * ref if there is any kind of item for this block | |
1708 | */ | |
2c47e605 YZ |
1709 | if (find_next_key(path, 0, &key) == 0 && |
1710 | key.objectid == bytenr && | |
85d4198e | 1711 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
5d4f98a2 YZ |
1712 | err = -EAGAIN; |
1713 | goto out; | |
1714 | } | |
1715 | } | |
1716 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; | |
1717 | out: | |
85d4198e | 1718 | if (insert) { |
5d4f98a2 YZ |
1719 | path->keep_locks = 0; |
1720 | btrfs_unlock_up_safe(path, 1); | |
1721 | } | |
1722 | return err; | |
1723 | } | |
1724 | ||
1725 | /* | |
1726 | * helper to add new inline back ref | |
1727 | */ | |
1728 | static noinline_for_stack | |
fd279fae | 1729 | void setup_inline_extent_backref(struct btrfs_root *root, |
143bede5 JM |
1730 | struct btrfs_path *path, |
1731 | struct btrfs_extent_inline_ref *iref, | |
1732 | u64 parent, u64 root_objectid, | |
1733 | u64 owner, u64 offset, int refs_to_add, | |
1734 | struct btrfs_delayed_extent_op *extent_op) | |
5d4f98a2 YZ |
1735 | { |
1736 | struct extent_buffer *leaf; | |
1737 | struct btrfs_extent_item *ei; | |
1738 | unsigned long ptr; | |
1739 | unsigned long end; | |
1740 | unsigned long item_offset; | |
1741 | u64 refs; | |
1742 | int size; | |
1743 | int type; | |
5d4f98a2 YZ |
1744 | |
1745 | leaf = path->nodes[0]; | |
1746 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1747 | item_offset = (unsigned long)iref - (unsigned long)ei; | |
1748 | ||
1749 | type = extent_ref_type(parent, owner); | |
1750 | size = btrfs_extent_inline_ref_size(type); | |
1751 | ||
4b90c680 | 1752 | btrfs_extend_item(root, path, size); |
5d4f98a2 YZ |
1753 | |
1754 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1755 | refs = btrfs_extent_refs(leaf, ei); | |
1756 | refs += refs_to_add; | |
1757 | btrfs_set_extent_refs(leaf, ei, refs); | |
1758 | if (extent_op) | |
1759 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1760 | ||
1761 | ptr = (unsigned long)ei + item_offset; | |
1762 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); | |
1763 | if (ptr < end - size) | |
1764 | memmove_extent_buffer(leaf, ptr + size, ptr, | |
1765 | end - size - ptr); | |
1766 | ||
1767 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
1768 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
1769 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1770 | struct btrfs_extent_data_ref *dref; | |
1771 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1772 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); | |
1773 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); | |
1774 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); | |
1775 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); | |
1776 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1777 | struct btrfs_shared_data_ref *sref; | |
1778 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1779 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); | |
1780 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1781 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { | |
1782 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1783 | } else { | |
1784 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | |
1785 | } | |
1786 | btrfs_mark_buffer_dirty(leaf); | |
5d4f98a2 YZ |
1787 | } |
1788 | ||
1789 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, | |
1790 | struct btrfs_root *root, | |
1791 | struct btrfs_path *path, | |
1792 | struct btrfs_extent_inline_ref **ref_ret, | |
1793 | u64 bytenr, u64 num_bytes, u64 parent, | |
1794 | u64 root_objectid, u64 owner, u64 offset) | |
1795 | { | |
1796 | int ret; | |
1797 | ||
1798 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, | |
1799 | bytenr, num_bytes, parent, | |
1800 | root_objectid, owner, offset, 0); | |
1801 | if (ret != -ENOENT) | |
54aa1f4d | 1802 | return ret; |
5d4f98a2 | 1803 | |
b3b4aa74 | 1804 | btrfs_release_path(path); |
5d4f98a2 YZ |
1805 | *ref_ret = NULL; |
1806 | ||
1807 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1808 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, | |
1809 | root_objectid); | |
1810 | } else { | |
1811 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, | |
1812 | root_objectid, owner, offset); | |
b9473439 | 1813 | } |
5d4f98a2 YZ |
1814 | return ret; |
1815 | } | |
31840ae1 | 1816 | |
5d4f98a2 YZ |
1817 | /* |
1818 | * helper to update/remove inline back ref | |
1819 | */ | |
1820 | static noinline_for_stack | |
afe5fea7 | 1821 | void update_inline_extent_backref(struct btrfs_root *root, |
143bede5 JM |
1822 | struct btrfs_path *path, |
1823 | struct btrfs_extent_inline_ref *iref, | |
1824 | int refs_to_mod, | |
fcebe456 JB |
1825 | struct btrfs_delayed_extent_op *extent_op, |
1826 | int *last_ref) | |
5d4f98a2 YZ |
1827 | { |
1828 | struct extent_buffer *leaf; | |
1829 | struct btrfs_extent_item *ei; | |
1830 | struct btrfs_extent_data_ref *dref = NULL; | |
1831 | struct btrfs_shared_data_ref *sref = NULL; | |
1832 | unsigned long ptr; | |
1833 | unsigned long end; | |
1834 | u32 item_size; | |
1835 | int size; | |
1836 | int type; | |
5d4f98a2 YZ |
1837 | u64 refs; |
1838 | ||
1839 | leaf = path->nodes[0]; | |
1840 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1841 | refs = btrfs_extent_refs(leaf, ei); | |
1842 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); | |
1843 | refs += refs_to_mod; | |
1844 | btrfs_set_extent_refs(leaf, ei, refs); | |
1845 | if (extent_op) | |
1846 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1847 | ||
1848 | type = btrfs_extent_inline_ref_type(leaf, iref); | |
1849 | ||
1850 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1851 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1852 | refs = btrfs_extent_data_ref_count(leaf, dref); | |
1853 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1854 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1855 | refs = btrfs_shared_data_ref_count(leaf, sref); | |
1856 | } else { | |
1857 | refs = 1; | |
1858 | BUG_ON(refs_to_mod != -1); | |
56bec294 | 1859 | } |
31840ae1 | 1860 | |
5d4f98a2 YZ |
1861 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
1862 | refs += refs_to_mod; | |
1863 | ||
1864 | if (refs > 0) { | |
1865 | if (type == BTRFS_EXTENT_DATA_REF_KEY) | |
1866 | btrfs_set_extent_data_ref_count(leaf, dref, refs); | |
1867 | else | |
1868 | btrfs_set_shared_data_ref_count(leaf, sref, refs); | |
1869 | } else { | |
fcebe456 | 1870 | *last_ref = 1; |
5d4f98a2 YZ |
1871 | size = btrfs_extent_inline_ref_size(type); |
1872 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1873 | ptr = (unsigned long)iref; | |
1874 | end = (unsigned long)ei + item_size; | |
1875 | if (ptr + size < end) | |
1876 | memmove_extent_buffer(leaf, ptr, ptr + size, | |
1877 | end - ptr - size); | |
1878 | item_size -= size; | |
afe5fea7 | 1879 | btrfs_truncate_item(root, path, item_size, 1); |
5d4f98a2 YZ |
1880 | } |
1881 | btrfs_mark_buffer_dirty(leaf); | |
5d4f98a2 YZ |
1882 | } |
1883 | ||
1884 | static noinline_for_stack | |
1885 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1886 | struct btrfs_root *root, | |
1887 | struct btrfs_path *path, | |
1888 | u64 bytenr, u64 num_bytes, u64 parent, | |
1889 | u64 root_objectid, u64 owner, | |
1890 | u64 offset, int refs_to_add, | |
1891 | struct btrfs_delayed_extent_op *extent_op) | |
1892 | { | |
1893 | struct btrfs_extent_inline_ref *iref; | |
1894 | int ret; | |
1895 | ||
1896 | ret = lookup_inline_extent_backref(trans, root, path, &iref, | |
1897 | bytenr, num_bytes, parent, | |
1898 | root_objectid, owner, offset, 1); | |
1899 | if (ret == 0) { | |
1900 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); | |
afe5fea7 | 1901 | update_inline_extent_backref(root, path, iref, |
fcebe456 | 1902 | refs_to_add, extent_op, NULL); |
5d4f98a2 | 1903 | } else if (ret == -ENOENT) { |
fd279fae | 1904 | setup_inline_extent_backref(root, path, iref, parent, |
143bede5 JM |
1905 | root_objectid, owner, offset, |
1906 | refs_to_add, extent_op); | |
1907 | ret = 0; | |
771ed689 | 1908 | } |
5d4f98a2 YZ |
1909 | return ret; |
1910 | } | |
31840ae1 | 1911 | |
5d4f98a2 YZ |
1912 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
1913 | struct btrfs_root *root, | |
1914 | struct btrfs_path *path, | |
1915 | u64 bytenr, u64 parent, u64 root_objectid, | |
1916 | u64 owner, u64 offset, int refs_to_add) | |
1917 | { | |
1918 | int ret; | |
1919 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1920 | BUG_ON(refs_to_add != 1); | |
1921 | ret = insert_tree_block_ref(trans, root, path, bytenr, | |
1922 | parent, root_objectid); | |
1923 | } else { | |
1924 | ret = insert_extent_data_ref(trans, root, path, bytenr, | |
1925 | parent, root_objectid, | |
1926 | owner, offset, refs_to_add); | |
1927 | } | |
1928 | return ret; | |
1929 | } | |
56bec294 | 1930 | |
5d4f98a2 YZ |
1931 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
1932 | struct btrfs_root *root, | |
1933 | struct btrfs_path *path, | |
1934 | struct btrfs_extent_inline_ref *iref, | |
fcebe456 | 1935 | int refs_to_drop, int is_data, int *last_ref) |
5d4f98a2 | 1936 | { |
143bede5 | 1937 | int ret = 0; |
b9473439 | 1938 | |
5d4f98a2 YZ |
1939 | BUG_ON(!is_data && refs_to_drop != 1); |
1940 | if (iref) { | |
afe5fea7 | 1941 | update_inline_extent_backref(root, path, iref, |
fcebe456 | 1942 | -refs_to_drop, NULL, last_ref); |
5d4f98a2 | 1943 | } else if (is_data) { |
fcebe456 JB |
1944 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop, |
1945 | last_ref); | |
5d4f98a2 | 1946 | } else { |
fcebe456 | 1947 | *last_ref = 1; |
5d4f98a2 YZ |
1948 | ret = btrfs_del_item(trans, root, path); |
1949 | } | |
1950 | return ret; | |
1951 | } | |
1952 | ||
86557861 | 1953 | #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len)) |
d04c6b88 JM |
1954 | static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len, |
1955 | u64 *discarded_bytes) | |
5d4f98a2 | 1956 | { |
86557861 JM |
1957 | int j, ret = 0; |
1958 | u64 bytes_left, end; | |
4d89d377 | 1959 | u64 aligned_start = ALIGN(start, 1 << 9); |
d04c6b88 | 1960 | |
4d89d377 JM |
1961 | if (WARN_ON(start != aligned_start)) { |
1962 | len -= aligned_start - start; | |
1963 | len = round_down(len, 1 << 9); | |
1964 | start = aligned_start; | |
1965 | } | |
d04c6b88 | 1966 | |
4d89d377 | 1967 | *discarded_bytes = 0; |
86557861 JM |
1968 | |
1969 | if (!len) | |
1970 | return 0; | |
1971 | ||
1972 | end = start + len; | |
1973 | bytes_left = len; | |
1974 | ||
1975 | /* Skip any superblocks on this device. */ | |
1976 | for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) { | |
1977 | u64 sb_start = btrfs_sb_offset(j); | |
1978 | u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE; | |
1979 | u64 size = sb_start - start; | |
1980 | ||
1981 | if (!in_range(sb_start, start, bytes_left) && | |
1982 | !in_range(sb_end, start, bytes_left) && | |
1983 | !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE)) | |
1984 | continue; | |
1985 | ||
1986 | /* | |
1987 | * Superblock spans beginning of range. Adjust start and | |
1988 | * try again. | |
1989 | */ | |
1990 | if (sb_start <= start) { | |
1991 | start += sb_end - start; | |
1992 | if (start > end) { | |
1993 | bytes_left = 0; | |
1994 | break; | |
1995 | } | |
1996 | bytes_left = end - start; | |
1997 | continue; | |
1998 | } | |
1999 | ||
2000 | if (size) { | |
2001 | ret = blkdev_issue_discard(bdev, start >> 9, size >> 9, | |
2002 | GFP_NOFS, 0); | |
2003 | if (!ret) | |
2004 | *discarded_bytes += size; | |
2005 | else if (ret != -EOPNOTSUPP) | |
2006 | return ret; | |
2007 | } | |
2008 | ||
2009 | start = sb_end; | |
2010 | if (start > end) { | |
2011 | bytes_left = 0; | |
2012 | break; | |
2013 | } | |
2014 | bytes_left = end - start; | |
2015 | } | |
2016 | ||
2017 | if (bytes_left) { | |
2018 | ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9, | |
4d89d377 JM |
2019 | GFP_NOFS, 0); |
2020 | if (!ret) | |
86557861 | 2021 | *discarded_bytes += bytes_left; |
4d89d377 | 2022 | } |
d04c6b88 | 2023 | return ret; |
5d4f98a2 | 2024 | } |
5d4f98a2 | 2025 | |
1edb647b FM |
2026 | int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, |
2027 | u64 num_bytes, u64 *actual_bytes) | |
5d4f98a2 | 2028 | { |
0b246afa | 2029 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 | 2030 | int ret; |
5378e607 | 2031 | u64 discarded_bytes = 0; |
a1d3c478 | 2032 | struct btrfs_bio *bbio = NULL; |
5d4f98a2 | 2033 | |
e244a0ae | 2034 | |
2999241d FM |
2035 | /* |
2036 | * Avoid races with device replace and make sure our bbio has devices | |
2037 | * associated to its stripes that don't go away while we are discarding. | |
2038 | */ | |
0b246afa | 2039 | btrfs_bio_counter_inc_blocked(fs_info); |
5d4f98a2 | 2040 | /* Tell the block device(s) that the sectors can be discarded */ |
0b246afa JM |
2041 | ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, bytenr, &num_bytes, |
2042 | &bbio, 0); | |
79787eaa | 2043 | /* Error condition is -ENOMEM */ |
5d4f98a2 | 2044 | if (!ret) { |
a1d3c478 | 2045 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
5d4f98a2 YZ |
2046 | int i; |
2047 | ||
5d4f98a2 | 2048 | |
a1d3c478 | 2049 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
d04c6b88 | 2050 | u64 bytes; |
d5e2003c JB |
2051 | if (!stripe->dev->can_discard) |
2052 | continue; | |
2053 | ||
5378e607 LD |
2054 | ret = btrfs_issue_discard(stripe->dev->bdev, |
2055 | stripe->physical, | |
d04c6b88 JM |
2056 | stripe->length, |
2057 | &bytes); | |
5378e607 | 2058 | if (!ret) |
d04c6b88 | 2059 | discarded_bytes += bytes; |
5378e607 | 2060 | else if (ret != -EOPNOTSUPP) |
79787eaa | 2061 | break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */ |
d5e2003c JB |
2062 | |
2063 | /* | |
2064 | * Just in case we get back EOPNOTSUPP for some reason, | |
2065 | * just ignore the return value so we don't screw up | |
2066 | * people calling discard_extent. | |
2067 | */ | |
2068 | ret = 0; | |
5d4f98a2 | 2069 | } |
6e9606d2 | 2070 | btrfs_put_bbio(bbio); |
5d4f98a2 | 2071 | } |
0b246afa | 2072 | btrfs_bio_counter_dec(fs_info); |
5378e607 LD |
2073 | |
2074 | if (actual_bytes) | |
2075 | *actual_bytes = discarded_bytes; | |
2076 | ||
5d4f98a2 | 2077 | |
53b381b3 DW |
2078 | if (ret == -EOPNOTSUPP) |
2079 | ret = 0; | |
5d4f98a2 | 2080 | return ret; |
5d4f98a2 YZ |
2081 | } |
2082 | ||
79787eaa | 2083 | /* Can return -ENOMEM */ |
5d4f98a2 YZ |
2084 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
2085 | struct btrfs_root *root, | |
2086 | u64 bytenr, u64 num_bytes, u64 parent, | |
b06c4bf5 | 2087 | u64 root_objectid, u64 owner, u64 offset) |
5d4f98a2 YZ |
2088 | { |
2089 | int ret; | |
66d7e7f0 AJ |
2090 | struct btrfs_fs_info *fs_info = root->fs_info; |
2091 | ||
5d4f98a2 YZ |
2092 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && |
2093 | root_objectid == BTRFS_TREE_LOG_OBJECTID); | |
2094 | ||
2095 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
66d7e7f0 AJ |
2096 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
2097 | num_bytes, | |
5d4f98a2 | 2098 | parent, root_objectid, (int)owner, |
b06c4bf5 | 2099 | BTRFS_ADD_DELAYED_REF, NULL); |
5d4f98a2 | 2100 | } else { |
66d7e7f0 | 2101 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
5846a3c2 QW |
2102 | num_bytes, parent, root_objectid, |
2103 | owner, offset, 0, | |
b06c4bf5 | 2104 | BTRFS_ADD_DELAYED_REF, NULL); |
5d4f98a2 YZ |
2105 | } |
2106 | return ret; | |
2107 | } | |
2108 | ||
2109 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |
2110 | struct btrfs_root *root, | |
c682f9b3 | 2111 | struct btrfs_delayed_ref_node *node, |
5d4f98a2 YZ |
2112 | u64 parent, u64 root_objectid, |
2113 | u64 owner, u64 offset, int refs_to_add, | |
2114 | struct btrfs_delayed_extent_op *extent_op) | |
2115 | { | |
fcebe456 | 2116 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
2117 | struct btrfs_path *path; |
2118 | struct extent_buffer *leaf; | |
2119 | struct btrfs_extent_item *item; | |
fcebe456 | 2120 | struct btrfs_key key; |
c682f9b3 QW |
2121 | u64 bytenr = node->bytenr; |
2122 | u64 num_bytes = node->num_bytes; | |
5d4f98a2 YZ |
2123 | u64 refs; |
2124 | int ret; | |
5d4f98a2 YZ |
2125 | |
2126 | path = btrfs_alloc_path(); | |
2127 | if (!path) | |
2128 | return -ENOMEM; | |
2129 | ||
e4058b54 | 2130 | path->reada = READA_FORWARD; |
5d4f98a2 YZ |
2131 | path->leave_spinning = 1; |
2132 | /* this will setup the path even if it fails to insert the back ref */ | |
fcebe456 JB |
2133 | ret = insert_inline_extent_backref(trans, fs_info->extent_root, path, |
2134 | bytenr, num_bytes, parent, | |
5d4f98a2 YZ |
2135 | root_objectid, owner, offset, |
2136 | refs_to_add, extent_op); | |
0ed4792a | 2137 | if ((ret < 0 && ret != -EAGAIN) || !ret) |
5d4f98a2 | 2138 | goto out; |
fcebe456 JB |
2139 | |
2140 | /* | |
2141 | * Ok we had -EAGAIN which means we didn't have space to insert and | |
2142 | * inline extent ref, so just update the reference count and add a | |
2143 | * normal backref. | |
2144 | */ | |
5d4f98a2 | 2145 | leaf = path->nodes[0]; |
fcebe456 | 2146 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
5d4f98a2 YZ |
2147 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
2148 | refs = btrfs_extent_refs(leaf, item); | |
2149 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); | |
2150 | if (extent_op) | |
2151 | __run_delayed_extent_op(extent_op, leaf, item); | |
56bec294 | 2152 | |
5d4f98a2 | 2153 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 2154 | btrfs_release_path(path); |
56bec294 | 2155 | |
e4058b54 | 2156 | path->reada = READA_FORWARD; |
b9473439 | 2157 | path->leave_spinning = 1; |
56bec294 | 2158 | /* now insert the actual backref */ |
0b246afa | 2159 | ret = insert_extent_backref(trans, fs_info->extent_root, |
5d4f98a2 YZ |
2160 | path, bytenr, parent, root_objectid, |
2161 | owner, offset, refs_to_add); | |
79787eaa | 2162 | if (ret) |
66642832 | 2163 | btrfs_abort_transaction(trans, ret); |
5d4f98a2 | 2164 | out: |
56bec294 | 2165 | btrfs_free_path(path); |
30d133fc | 2166 | return ret; |
56bec294 CM |
2167 | } |
2168 | ||
5d4f98a2 YZ |
2169 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
2170 | struct btrfs_root *root, | |
2171 | struct btrfs_delayed_ref_node *node, | |
2172 | struct btrfs_delayed_extent_op *extent_op, | |
2173 | int insert_reserved) | |
56bec294 | 2174 | { |
0b246afa | 2175 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
2176 | int ret = 0; |
2177 | struct btrfs_delayed_data_ref *ref; | |
2178 | struct btrfs_key ins; | |
2179 | u64 parent = 0; | |
2180 | u64 ref_root = 0; | |
2181 | u64 flags = 0; | |
2182 | ||
2183 | ins.objectid = node->bytenr; | |
2184 | ins.offset = node->num_bytes; | |
2185 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
2186 | ||
2187 | ref = btrfs_delayed_node_to_data_ref(node); | |
0b246afa | 2188 | trace_run_delayed_data_ref(fs_info, node, ref, node->action); |
599c75ec | 2189 | |
5d4f98a2 YZ |
2190 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) |
2191 | parent = ref->parent; | |
fcebe456 | 2192 | ref_root = ref->root; |
5d4f98a2 YZ |
2193 | |
2194 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | |
3173a18f | 2195 | if (extent_op) |
5d4f98a2 | 2196 | flags |= extent_op->flags_to_set; |
5d4f98a2 YZ |
2197 | ret = alloc_reserved_file_extent(trans, root, |
2198 | parent, ref_root, flags, | |
2199 | ref->objectid, ref->offset, | |
2200 | &ins, node->ref_mod); | |
5d4f98a2 | 2201 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
c682f9b3 | 2202 | ret = __btrfs_inc_extent_ref(trans, root, node, parent, |
5d4f98a2 YZ |
2203 | ref_root, ref->objectid, |
2204 | ref->offset, node->ref_mod, | |
c682f9b3 | 2205 | extent_op); |
5d4f98a2 | 2206 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
c682f9b3 | 2207 | ret = __btrfs_free_extent(trans, root, node, parent, |
5d4f98a2 YZ |
2208 | ref_root, ref->objectid, |
2209 | ref->offset, node->ref_mod, | |
c682f9b3 | 2210 | extent_op); |
5d4f98a2 YZ |
2211 | } else { |
2212 | BUG(); | |
2213 | } | |
2214 | return ret; | |
2215 | } | |
2216 | ||
2217 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, | |
2218 | struct extent_buffer *leaf, | |
2219 | struct btrfs_extent_item *ei) | |
2220 | { | |
2221 | u64 flags = btrfs_extent_flags(leaf, ei); | |
2222 | if (extent_op->update_flags) { | |
2223 | flags |= extent_op->flags_to_set; | |
2224 | btrfs_set_extent_flags(leaf, ei, flags); | |
2225 | } | |
2226 | ||
2227 | if (extent_op->update_key) { | |
2228 | struct btrfs_tree_block_info *bi; | |
2229 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); | |
2230 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
2231 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); | |
2232 | } | |
2233 | } | |
2234 | ||
2235 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, | |
2236 | struct btrfs_root *root, | |
2237 | struct btrfs_delayed_ref_node *node, | |
2238 | struct btrfs_delayed_extent_op *extent_op) | |
2239 | { | |
0b246afa | 2240 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
2241 | struct btrfs_key key; |
2242 | struct btrfs_path *path; | |
2243 | struct btrfs_extent_item *ei; | |
2244 | struct extent_buffer *leaf; | |
2245 | u32 item_size; | |
56bec294 | 2246 | int ret; |
5d4f98a2 | 2247 | int err = 0; |
b1c79e09 | 2248 | int metadata = !extent_op->is_data; |
5d4f98a2 | 2249 | |
79787eaa JM |
2250 | if (trans->aborted) |
2251 | return 0; | |
2252 | ||
0b246afa | 2253 | if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
3173a18f JB |
2254 | metadata = 0; |
2255 | ||
5d4f98a2 YZ |
2256 | path = btrfs_alloc_path(); |
2257 | if (!path) | |
2258 | return -ENOMEM; | |
2259 | ||
2260 | key.objectid = node->bytenr; | |
5d4f98a2 | 2261 | |
3173a18f | 2262 | if (metadata) { |
3173a18f | 2263 | key.type = BTRFS_METADATA_ITEM_KEY; |
b1c79e09 | 2264 | key.offset = extent_op->level; |
3173a18f JB |
2265 | } else { |
2266 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
2267 | key.offset = node->num_bytes; | |
2268 | } | |
2269 | ||
2270 | again: | |
e4058b54 | 2271 | path->reada = READA_FORWARD; |
5d4f98a2 | 2272 | path->leave_spinning = 1; |
0b246afa | 2273 | ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1); |
5d4f98a2 YZ |
2274 | if (ret < 0) { |
2275 | err = ret; | |
2276 | goto out; | |
2277 | } | |
2278 | if (ret > 0) { | |
3173a18f | 2279 | if (metadata) { |
55994887 FDBM |
2280 | if (path->slots[0] > 0) { |
2281 | path->slots[0]--; | |
2282 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
2283 | path->slots[0]); | |
2284 | if (key.objectid == node->bytenr && | |
2285 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
2286 | key.offset == node->num_bytes) | |
2287 | ret = 0; | |
2288 | } | |
2289 | if (ret > 0) { | |
2290 | btrfs_release_path(path); | |
2291 | metadata = 0; | |
3173a18f | 2292 | |
55994887 FDBM |
2293 | key.objectid = node->bytenr; |
2294 | key.offset = node->num_bytes; | |
2295 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
2296 | goto again; | |
2297 | } | |
2298 | } else { | |
2299 | err = -EIO; | |
2300 | goto out; | |
3173a18f | 2301 | } |
5d4f98a2 YZ |
2302 | } |
2303 | ||
2304 | leaf = path->nodes[0]; | |
2305 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2306 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
2307 | if (item_size < sizeof(*ei)) { | |
0b246afa | 2308 | ret = convert_extent_item_v0(trans, fs_info->extent_root, |
5d4f98a2 YZ |
2309 | path, (u64)-1, 0); |
2310 | if (ret < 0) { | |
2311 | err = ret; | |
2312 | goto out; | |
2313 | } | |
2314 | leaf = path->nodes[0]; | |
2315 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2316 | } | |
2317 | #endif | |
2318 | BUG_ON(item_size < sizeof(*ei)); | |
2319 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
2320 | __run_delayed_extent_op(extent_op, leaf, ei); | |
56bec294 | 2321 | |
5d4f98a2 YZ |
2322 | btrfs_mark_buffer_dirty(leaf); |
2323 | out: | |
2324 | btrfs_free_path(path); | |
2325 | return err; | |
56bec294 CM |
2326 | } |
2327 | ||
5d4f98a2 YZ |
2328 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
2329 | struct btrfs_root *root, | |
2330 | struct btrfs_delayed_ref_node *node, | |
2331 | struct btrfs_delayed_extent_op *extent_op, | |
2332 | int insert_reserved) | |
56bec294 | 2333 | { |
0b246afa | 2334 | struct btrfs_fs_info *fs_info = root->fs_info; |
56bec294 | 2335 | int ret = 0; |
5d4f98a2 YZ |
2336 | struct btrfs_delayed_tree_ref *ref; |
2337 | struct btrfs_key ins; | |
2338 | u64 parent = 0; | |
2339 | u64 ref_root = 0; | |
0b246afa | 2340 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
56bec294 | 2341 | |
5d4f98a2 | 2342 | ref = btrfs_delayed_node_to_tree_ref(node); |
0b246afa | 2343 | trace_run_delayed_tree_ref(fs_info, node, ref, node->action); |
599c75ec | 2344 | |
5d4f98a2 YZ |
2345 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) |
2346 | parent = ref->parent; | |
fcebe456 | 2347 | ref_root = ref->root; |
5d4f98a2 | 2348 | |
3173a18f JB |
2349 | ins.objectid = node->bytenr; |
2350 | if (skinny_metadata) { | |
2351 | ins.offset = ref->level; | |
2352 | ins.type = BTRFS_METADATA_ITEM_KEY; | |
2353 | } else { | |
2354 | ins.offset = node->num_bytes; | |
2355 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
2356 | } | |
2357 | ||
02794222 LB |
2358 | if (node->ref_mod != 1) { |
2359 | btrfs_err(root->fs_info, | |
2360 | "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu", | |
2361 | node->bytenr, node->ref_mod, node->action, ref_root, | |
2362 | parent); | |
2363 | return -EIO; | |
2364 | } | |
5d4f98a2 | 2365 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { |
3173a18f | 2366 | BUG_ON(!extent_op || !extent_op->update_flags); |
5d4f98a2 YZ |
2367 | ret = alloc_reserved_tree_block(trans, root, |
2368 | parent, ref_root, | |
2369 | extent_op->flags_to_set, | |
2370 | &extent_op->key, | |
b06c4bf5 | 2371 | ref->level, &ins); |
5d4f98a2 | 2372 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
c682f9b3 QW |
2373 | ret = __btrfs_inc_extent_ref(trans, root, node, |
2374 | parent, ref_root, | |
2375 | ref->level, 0, 1, | |
fcebe456 | 2376 | extent_op); |
5d4f98a2 | 2377 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { |
c682f9b3 QW |
2378 | ret = __btrfs_free_extent(trans, root, node, |
2379 | parent, ref_root, | |
2380 | ref->level, 0, 1, extent_op); | |
5d4f98a2 YZ |
2381 | } else { |
2382 | BUG(); | |
2383 | } | |
56bec294 CM |
2384 | return ret; |
2385 | } | |
2386 | ||
2387 | /* helper function to actually process a single delayed ref entry */ | |
5d4f98a2 YZ |
2388 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
2389 | struct btrfs_root *root, | |
2390 | struct btrfs_delayed_ref_node *node, | |
2391 | struct btrfs_delayed_extent_op *extent_op, | |
2392 | int insert_reserved) | |
56bec294 | 2393 | { |
0b246afa | 2394 | struct btrfs_fs_info *fs_info = root->fs_info; |
79787eaa JM |
2395 | int ret = 0; |
2396 | ||
857cc2fc JB |
2397 | if (trans->aborted) { |
2398 | if (insert_reserved) | |
2399 | btrfs_pin_extent(root, node->bytenr, | |
2400 | node->num_bytes, 1); | |
79787eaa | 2401 | return 0; |
857cc2fc | 2402 | } |
79787eaa | 2403 | |
5d4f98a2 | 2404 | if (btrfs_delayed_ref_is_head(node)) { |
56bec294 CM |
2405 | struct btrfs_delayed_ref_head *head; |
2406 | /* | |
2407 | * we've hit the end of the chain and we were supposed | |
2408 | * to insert this extent into the tree. But, it got | |
2409 | * deleted before we ever needed to insert it, so all | |
2410 | * we have to do is clean up the accounting | |
2411 | */ | |
5d4f98a2 YZ |
2412 | BUG_ON(extent_op); |
2413 | head = btrfs_delayed_node_to_head(node); | |
0b246afa | 2414 | trace_run_delayed_ref_head(fs_info, node, head, node->action); |
599c75ec | 2415 | |
56bec294 | 2416 | if (insert_reserved) { |
f0486c68 YZ |
2417 | btrfs_pin_extent(root, node->bytenr, |
2418 | node->num_bytes, 1); | |
5d4f98a2 | 2419 | if (head->is_data) { |
0b246afa | 2420 | ret = btrfs_del_csums(trans, fs_info, |
5d4f98a2 YZ |
2421 | node->bytenr, |
2422 | node->num_bytes); | |
5d4f98a2 | 2423 | } |
56bec294 | 2424 | } |
297d750b QW |
2425 | |
2426 | /* Also free its reserved qgroup space */ | |
0b246afa | 2427 | btrfs_qgroup_free_delayed_ref(fs_info, head->qgroup_ref_root, |
297d750b | 2428 | head->qgroup_reserved); |
79787eaa | 2429 | return ret; |
56bec294 CM |
2430 | } |
2431 | ||
5d4f98a2 YZ |
2432 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
2433 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) | |
2434 | ret = run_delayed_tree_ref(trans, root, node, extent_op, | |
2435 | insert_reserved); | |
2436 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || | |
2437 | node->type == BTRFS_SHARED_DATA_REF_KEY) | |
2438 | ret = run_delayed_data_ref(trans, root, node, extent_op, | |
2439 | insert_reserved); | |
2440 | else | |
2441 | BUG(); | |
2442 | return ret; | |
56bec294 CM |
2443 | } |
2444 | ||
c6fc2454 | 2445 | static inline struct btrfs_delayed_ref_node * |
56bec294 CM |
2446 | select_delayed_ref(struct btrfs_delayed_ref_head *head) |
2447 | { | |
cffc3374 FM |
2448 | struct btrfs_delayed_ref_node *ref; |
2449 | ||
c6fc2454 QW |
2450 | if (list_empty(&head->ref_list)) |
2451 | return NULL; | |
d7df2c79 | 2452 | |
cffc3374 FM |
2453 | /* |
2454 | * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first. | |
2455 | * This is to prevent a ref count from going down to zero, which deletes | |
2456 | * the extent item from the extent tree, when there still are references | |
2457 | * to add, which would fail because they would not find the extent item. | |
2458 | */ | |
1d57ee94 WX |
2459 | if (!list_empty(&head->ref_add_list)) |
2460 | return list_first_entry(&head->ref_add_list, | |
2461 | struct btrfs_delayed_ref_node, add_list); | |
2462 | ||
2463 | ref = list_first_entry(&head->ref_list, struct btrfs_delayed_ref_node, | |
2464 | list); | |
2465 | ASSERT(list_empty(&ref->add_list)); | |
2466 | return ref; | |
56bec294 CM |
2467 | } |
2468 | ||
79787eaa JM |
2469 | /* |
2470 | * Returns 0 on success or if called with an already aborted transaction. | |
2471 | * Returns -ENOMEM or -EIO on failure and will abort the transaction. | |
2472 | */ | |
d7df2c79 JB |
2473 | static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, |
2474 | struct btrfs_root *root, | |
2475 | unsigned long nr) | |
56bec294 | 2476 | { |
56bec294 CM |
2477 | struct btrfs_delayed_ref_root *delayed_refs; |
2478 | struct btrfs_delayed_ref_node *ref; | |
2479 | struct btrfs_delayed_ref_head *locked_ref = NULL; | |
5d4f98a2 | 2480 | struct btrfs_delayed_extent_op *extent_op; |
097b8a7c | 2481 | struct btrfs_fs_info *fs_info = root->fs_info; |
0a2b2a84 | 2482 | ktime_t start = ktime_get(); |
56bec294 | 2483 | int ret; |
d7df2c79 | 2484 | unsigned long count = 0; |
0a2b2a84 | 2485 | unsigned long actual_count = 0; |
56bec294 | 2486 | int must_insert_reserved = 0; |
56bec294 CM |
2487 | |
2488 | delayed_refs = &trans->transaction->delayed_refs; | |
56bec294 CM |
2489 | while (1) { |
2490 | if (!locked_ref) { | |
d7df2c79 | 2491 | if (count >= nr) |
56bec294 | 2492 | break; |
56bec294 | 2493 | |
d7df2c79 JB |
2494 | spin_lock(&delayed_refs->lock); |
2495 | locked_ref = btrfs_select_ref_head(trans); | |
2496 | if (!locked_ref) { | |
2497 | spin_unlock(&delayed_refs->lock); | |
2498 | break; | |
2499 | } | |
c3e69d58 CM |
2500 | |
2501 | /* grab the lock that says we are going to process | |
2502 | * all the refs for this head */ | |
2503 | ret = btrfs_delayed_ref_lock(trans, locked_ref); | |
d7df2c79 | 2504 | spin_unlock(&delayed_refs->lock); |
c3e69d58 CM |
2505 | /* |
2506 | * we may have dropped the spin lock to get the head | |
2507 | * mutex lock, and that might have given someone else | |
2508 | * time to free the head. If that's true, it has been | |
2509 | * removed from our list and we can move on. | |
2510 | */ | |
2511 | if (ret == -EAGAIN) { | |
2512 | locked_ref = NULL; | |
2513 | count++; | |
2514 | continue; | |
56bec294 CM |
2515 | } |
2516 | } | |
a28ec197 | 2517 | |
2c3cf7d5 FM |
2518 | /* |
2519 | * We need to try and merge add/drops of the same ref since we | |
2520 | * can run into issues with relocate dropping the implicit ref | |
2521 | * and then it being added back again before the drop can | |
2522 | * finish. If we merged anything we need to re-loop so we can | |
2523 | * get a good ref. | |
2524 | * Or we can get node references of the same type that weren't | |
2525 | * merged when created due to bumps in the tree mod seq, and | |
2526 | * we need to merge them to prevent adding an inline extent | |
2527 | * backref before dropping it (triggering a BUG_ON at | |
2528 | * insert_inline_extent_backref()). | |
2529 | */ | |
d7df2c79 | 2530 | spin_lock(&locked_ref->lock); |
2c3cf7d5 FM |
2531 | btrfs_merge_delayed_refs(trans, fs_info, delayed_refs, |
2532 | locked_ref); | |
ae1e206b | 2533 | |
d1270cd9 AJ |
2534 | /* |
2535 | * locked_ref is the head node, so we have to go one | |
2536 | * node back for any delayed ref updates | |
2537 | */ | |
2538 | ref = select_delayed_ref(locked_ref); | |
2539 | ||
2540 | if (ref && ref->seq && | |
097b8a7c | 2541 | btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) { |
d7df2c79 | 2542 | spin_unlock(&locked_ref->lock); |
093486c4 | 2543 | btrfs_delayed_ref_unlock(locked_ref); |
d7df2c79 JB |
2544 | spin_lock(&delayed_refs->lock); |
2545 | locked_ref->processing = 0; | |
d1270cd9 AJ |
2546 | delayed_refs->num_heads_ready++; |
2547 | spin_unlock(&delayed_refs->lock); | |
d7df2c79 | 2548 | locked_ref = NULL; |
d1270cd9 | 2549 | cond_resched(); |
27a377db | 2550 | count++; |
d1270cd9 AJ |
2551 | continue; |
2552 | } | |
2553 | ||
56bec294 CM |
2554 | /* |
2555 | * record the must insert reserved flag before we | |
2556 | * drop the spin lock. | |
2557 | */ | |
2558 | must_insert_reserved = locked_ref->must_insert_reserved; | |
2559 | locked_ref->must_insert_reserved = 0; | |
7bb86316 | 2560 | |
5d4f98a2 YZ |
2561 | extent_op = locked_ref->extent_op; |
2562 | locked_ref->extent_op = NULL; | |
2563 | ||
56bec294 | 2564 | if (!ref) { |
d7df2c79 JB |
2565 | |
2566 | ||
56bec294 CM |
2567 | /* All delayed refs have been processed, Go ahead |
2568 | * and send the head node to run_one_delayed_ref, | |
2569 | * so that any accounting fixes can happen | |
2570 | */ | |
2571 | ref = &locked_ref->node; | |
5d4f98a2 YZ |
2572 | |
2573 | if (extent_op && must_insert_reserved) { | |
78a6184a | 2574 | btrfs_free_delayed_extent_op(extent_op); |
5d4f98a2 YZ |
2575 | extent_op = NULL; |
2576 | } | |
2577 | ||
2578 | if (extent_op) { | |
d7df2c79 | 2579 | spin_unlock(&locked_ref->lock); |
5d4f98a2 YZ |
2580 | ret = run_delayed_extent_op(trans, root, |
2581 | ref, extent_op); | |
78a6184a | 2582 | btrfs_free_delayed_extent_op(extent_op); |
5d4f98a2 | 2583 | |
79787eaa | 2584 | if (ret) { |
857cc2fc JB |
2585 | /* |
2586 | * Need to reset must_insert_reserved if | |
2587 | * there was an error so the abort stuff | |
2588 | * can cleanup the reserved space | |
2589 | * properly. | |
2590 | */ | |
2591 | if (must_insert_reserved) | |
2592 | locked_ref->must_insert_reserved = 1; | |
d7df2c79 | 2593 | locked_ref->processing = 0; |
5d163e0e JM |
2594 | btrfs_debug(fs_info, |
2595 | "run_delayed_extent_op returned %d", | |
2596 | ret); | |
093486c4 | 2597 | btrfs_delayed_ref_unlock(locked_ref); |
79787eaa JM |
2598 | return ret; |
2599 | } | |
d7df2c79 | 2600 | continue; |
5d4f98a2 | 2601 | } |
02217ed2 | 2602 | |
d7df2c79 | 2603 | /* |
01327610 | 2604 | * Need to drop our head ref lock and re-acquire the |
d7df2c79 JB |
2605 | * delayed ref lock and then re-check to make sure |
2606 | * nobody got added. | |
2607 | */ | |
2608 | spin_unlock(&locked_ref->lock); | |
2609 | spin_lock(&delayed_refs->lock); | |
2610 | spin_lock(&locked_ref->lock); | |
c6fc2454 | 2611 | if (!list_empty(&locked_ref->ref_list) || |
573a0755 | 2612 | locked_ref->extent_op) { |
d7df2c79 JB |
2613 | spin_unlock(&locked_ref->lock); |
2614 | spin_unlock(&delayed_refs->lock); | |
2615 | continue; | |
2616 | } | |
2617 | ref->in_tree = 0; | |
2618 | delayed_refs->num_heads--; | |
c46effa6 LB |
2619 | rb_erase(&locked_ref->href_node, |
2620 | &delayed_refs->href_root); | |
d7df2c79 JB |
2621 | spin_unlock(&delayed_refs->lock); |
2622 | } else { | |
0a2b2a84 | 2623 | actual_count++; |
d7df2c79 | 2624 | ref->in_tree = 0; |
c6fc2454 | 2625 | list_del(&ref->list); |
1d57ee94 WX |
2626 | if (!list_empty(&ref->add_list)) |
2627 | list_del(&ref->add_list); | |
c46effa6 | 2628 | } |
d7df2c79 JB |
2629 | atomic_dec(&delayed_refs->num_entries); |
2630 | ||
093486c4 | 2631 | if (!btrfs_delayed_ref_is_head(ref)) { |
22cd2e7d AJ |
2632 | /* |
2633 | * when we play the delayed ref, also correct the | |
2634 | * ref_mod on head | |
2635 | */ | |
2636 | switch (ref->action) { | |
2637 | case BTRFS_ADD_DELAYED_REF: | |
2638 | case BTRFS_ADD_DELAYED_EXTENT: | |
2639 | locked_ref->node.ref_mod -= ref->ref_mod; | |
2640 | break; | |
2641 | case BTRFS_DROP_DELAYED_REF: | |
2642 | locked_ref->node.ref_mod += ref->ref_mod; | |
2643 | break; | |
2644 | default: | |
2645 | WARN_ON(1); | |
2646 | } | |
2647 | } | |
d7df2c79 | 2648 | spin_unlock(&locked_ref->lock); |
925baedd | 2649 | |
5d4f98a2 | 2650 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
56bec294 | 2651 | must_insert_reserved); |
eb099670 | 2652 | |
78a6184a | 2653 | btrfs_free_delayed_extent_op(extent_op); |
79787eaa | 2654 | if (ret) { |
9d1032cc | 2655 | spin_lock(&delayed_refs->lock); |
d7df2c79 | 2656 | locked_ref->processing = 0; |
9d1032cc WX |
2657 | delayed_refs->num_heads_ready++; |
2658 | spin_unlock(&delayed_refs->lock); | |
093486c4 MX |
2659 | btrfs_delayed_ref_unlock(locked_ref); |
2660 | btrfs_put_delayed_ref(ref); | |
5d163e0e JM |
2661 | btrfs_debug(fs_info, "run_one_delayed_ref returned %d", |
2662 | ret); | |
79787eaa JM |
2663 | return ret; |
2664 | } | |
2665 | ||
093486c4 MX |
2666 | /* |
2667 | * If this node is a head, that means all the refs in this head | |
2668 | * have been dealt with, and we will pick the next head to deal | |
2669 | * with, so we must unlock the head and drop it from the cluster | |
2670 | * list before we release it. | |
2671 | */ | |
2672 | if (btrfs_delayed_ref_is_head(ref)) { | |
1262133b JB |
2673 | if (locked_ref->is_data && |
2674 | locked_ref->total_ref_mod < 0) { | |
2675 | spin_lock(&delayed_refs->lock); | |
2676 | delayed_refs->pending_csums -= ref->num_bytes; | |
2677 | spin_unlock(&delayed_refs->lock); | |
2678 | } | |
093486c4 MX |
2679 | btrfs_delayed_ref_unlock(locked_ref); |
2680 | locked_ref = NULL; | |
2681 | } | |
2682 | btrfs_put_delayed_ref(ref); | |
2683 | count++; | |
c3e69d58 | 2684 | cond_resched(); |
c3e69d58 | 2685 | } |
0a2b2a84 JB |
2686 | |
2687 | /* | |
2688 | * We don't want to include ref heads since we can have empty ref heads | |
2689 | * and those will drastically skew our runtime down since we just do | |
2690 | * accounting, no actual extent tree updates. | |
2691 | */ | |
2692 | if (actual_count > 0) { | |
2693 | u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start)); | |
2694 | u64 avg; | |
2695 | ||
2696 | /* | |
2697 | * We weigh the current average higher than our current runtime | |
2698 | * to avoid large swings in the average. | |
2699 | */ | |
2700 | spin_lock(&delayed_refs->lock); | |
2701 | avg = fs_info->avg_delayed_ref_runtime * 3 + runtime; | |
f8c269d7 | 2702 | fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */ |
0a2b2a84 JB |
2703 | spin_unlock(&delayed_refs->lock); |
2704 | } | |
d7df2c79 | 2705 | return 0; |
c3e69d58 CM |
2706 | } |
2707 | ||
709c0486 AJ |
2708 | #ifdef SCRAMBLE_DELAYED_REFS |
2709 | /* | |
2710 | * Normally delayed refs get processed in ascending bytenr order. This | |
2711 | * correlates in most cases to the order added. To expose dependencies on this | |
2712 | * order, we start to process the tree in the middle instead of the beginning | |
2713 | */ | |
2714 | static u64 find_middle(struct rb_root *root) | |
2715 | { | |
2716 | struct rb_node *n = root->rb_node; | |
2717 | struct btrfs_delayed_ref_node *entry; | |
2718 | int alt = 1; | |
2719 | u64 middle; | |
2720 | u64 first = 0, last = 0; | |
2721 | ||
2722 | n = rb_first(root); | |
2723 | if (n) { | |
2724 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2725 | first = entry->bytenr; | |
2726 | } | |
2727 | n = rb_last(root); | |
2728 | if (n) { | |
2729 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2730 | last = entry->bytenr; | |
2731 | } | |
2732 | n = root->rb_node; | |
2733 | ||
2734 | while (n) { | |
2735 | entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node); | |
2736 | WARN_ON(!entry->in_tree); | |
2737 | ||
2738 | middle = entry->bytenr; | |
2739 | ||
2740 | if (alt) | |
2741 | n = n->rb_left; | |
2742 | else | |
2743 | n = n->rb_right; | |
2744 | ||
2745 | alt = 1 - alt; | |
2746 | } | |
2747 | return middle; | |
2748 | } | |
2749 | #endif | |
2750 | ||
1be41b78 JB |
2751 | static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads) |
2752 | { | |
0b246afa | 2753 | struct btrfs_fs_info *fs_info = root->fs_info; |
1be41b78 JB |
2754 | u64 num_bytes; |
2755 | ||
2756 | num_bytes = heads * (sizeof(struct btrfs_extent_item) + | |
2757 | sizeof(struct btrfs_extent_inline_ref)); | |
0b246afa | 2758 | if (!btrfs_fs_incompat(fs_info, SKINNY_METADATA)) |
1be41b78 JB |
2759 | num_bytes += heads * sizeof(struct btrfs_tree_block_info); |
2760 | ||
2761 | /* | |
2762 | * We don't ever fill up leaves all the way so multiply by 2 just to be | |
01327610 | 2763 | * closer to what we're really going to want to use. |
1be41b78 | 2764 | */ |
0b246afa | 2765 | return div_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(fs_info)); |
1be41b78 JB |
2766 | } |
2767 | ||
1262133b JB |
2768 | /* |
2769 | * Takes the number of bytes to be csumm'ed and figures out how many leaves it | |
2770 | * would require to store the csums for that many bytes. | |
2771 | */ | |
28f75a0e | 2772 | u64 btrfs_csum_bytes_to_leaves(struct btrfs_root *root, u64 csum_bytes) |
1262133b | 2773 | { |
0b246afa | 2774 | struct btrfs_fs_info *fs_info = root->fs_info; |
1262133b JB |
2775 | u64 csum_size; |
2776 | u64 num_csums_per_leaf; | |
2777 | u64 num_csums; | |
2778 | ||
0b246afa | 2779 | csum_size = BTRFS_MAX_ITEM_SIZE(fs_info); |
1262133b | 2780 | num_csums_per_leaf = div64_u64(csum_size, |
0b246afa JM |
2781 | (u64)btrfs_super_csum_size(fs_info->super_copy)); |
2782 | num_csums = div64_u64(csum_bytes, fs_info->sectorsize); | |
1262133b JB |
2783 | num_csums += num_csums_per_leaf - 1; |
2784 | num_csums = div64_u64(num_csums, num_csums_per_leaf); | |
2785 | return num_csums; | |
2786 | } | |
2787 | ||
0a2b2a84 | 2788 | int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans, |
1be41b78 JB |
2789 | struct btrfs_root *root) |
2790 | { | |
0b246afa | 2791 | struct btrfs_fs_info *fs_info = root->fs_info; |
1be41b78 JB |
2792 | struct btrfs_block_rsv *global_rsv; |
2793 | u64 num_heads = trans->transaction->delayed_refs.num_heads_ready; | |
1262133b | 2794 | u64 csum_bytes = trans->transaction->delayed_refs.pending_csums; |
cb723e49 JB |
2795 | u64 num_dirty_bgs = trans->transaction->num_dirty_bgs; |
2796 | u64 num_bytes, num_dirty_bgs_bytes; | |
1be41b78 JB |
2797 | int ret = 0; |
2798 | ||
0b246afa | 2799 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
1be41b78 JB |
2800 | num_heads = heads_to_leaves(root, num_heads); |
2801 | if (num_heads > 1) | |
0b246afa | 2802 | num_bytes += (num_heads - 1) * fs_info->nodesize; |
1be41b78 | 2803 | num_bytes <<= 1; |
0b246afa JM |
2804 | num_bytes += btrfs_csum_bytes_to_leaves(root, csum_bytes) * fs_info->nodesize; |
2805 | num_dirty_bgs_bytes = btrfs_calc_trans_metadata_size(fs_info, | |
cb723e49 | 2806 | num_dirty_bgs); |
0b246afa | 2807 | global_rsv = &fs_info->global_block_rsv; |
1be41b78 JB |
2808 | |
2809 | /* | |
2810 | * If we can't allocate any more chunks lets make sure we have _lots_ of | |
2811 | * wiggle room since running delayed refs can create more delayed refs. | |
2812 | */ | |
cb723e49 JB |
2813 | if (global_rsv->space_info->full) { |
2814 | num_dirty_bgs_bytes <<= 1; | |
1be41b78 | 2815 | num_bytes <<= 1; |
cb723e49 | 2816 | } |
1be41b78 JB |
2817 | |
2818 | spin_lock(&global_rsv->lock); | |
cb723e49 | 2819 | if (global_rsv->reserved <= num_bytes + num_dirty_bgs_bytes) |
1be41b78 JB |
2820 | ret = 1; |
2821 | spin_unlock(&global_rsv->lock); | |
2822 | return ret; | |
2823 | } | |
2824 | ||
0a2b2a84 JB |
2825 | int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans, |
2826 | struct btrfs_root *root) | |
2827 | { | |
2828 | struct btrfs_fs_info *fs_info = root->fs_info; | |
2829 | u64 num_entries = | |
2830 | atomic_read(&trans->transaction->delayed_refs.num_entries); | |
2831 | u64 avg_runtime; | |
a79b7d4b | 2832 | u64 val; |
0a2b2a84 JB |
2833 | |
2834 | smp_mb(); | |
2835 | avg_runtime = fs_info->avg_delayed_ref_runtime; | |
a79b7d4b | 2836 | val = num_entries * avg_runtime; |
dc1a90c6 | 2837 | if (val >= NSEC_PER_SEC) |
0a2b2a84 | 2838 | return 1; |
a79b7d4b CM |
2839 | if (val >= NSEC_PER_SEC / 2) |
2840 | return 2; | |
0a2b2a84 JB |
2841 | |
2842 | return btrfs_check_space_for_delayed_refs(trans, root); | |
2843 | } | |
2844 | ||
a79b7d4b CM |
2845 | struct async_delayed_refs { |
2846 | struct btrfs_root *root; | |
31b9655f | 2847 | u64 transid; |
a79b7d4b CM |
2848 | int count; |
2849 | int error; | |
2850 | int sync; | |
2851 | struct completion wait; | |
2852 | struct btrfs_work work; | |
2853 | }; | |
2854 | ||
2855 | static void delayed_ref_async_start(struct btrfs_work *work) | |
2856 | { | |
2857 | struct async_delayed_refs *async; | |
2858 | struct btrfs_trans_handle *trans; | |
2859 | int ret; | |
2860 | ||
2861 | async = container_of(work, struct async_delayed_refs, work); | |
2862 | ||
0f873eca CM |
2863 | /* if the commit is already started, we don't need to wait here */ |
2864 | if (btrfs_transaction_blocked(async->root->fs_info)) | |
31b9655f | 2865 | goto done; |
31b9655f | 2866 | |
0f873eca CM |
2867 | trans = btrfs_join_transaction(async->root); |
2868 | if (IS_ERR(trans)) { | |
2869 | async->error = PTR_ERR(trans); | |
a79b7d4b CM |
2870 | goto done; |
2871 | } | |
2872 | ||
2873 | /* | |
01327610 | 2874 | * trans->sync means that when we call end_transaction, we won't |
a79b7d4b CM |
2875 | * wait on delayed refs |
2876 | */ | |
2877 | trans->sync = true; | |
0f873eca CM |
2878 | |
2879 | /* Don't bother flushing if we got into a different transaction */ | |
2880 | if (trans->transid > async->transid) | |
2881 | goto end; | |
2882 | ||
a79b7d4b CM |
2883 | ret = btrfs_run_delayed_refs(trans, async->root, async->count); |
2884 | if (ret) | |
2885 | async->error = ret; | |
0f873eca | 2886 | end: |
a79b7d4b CM |
2887 | ret = btrfs_end_transaction(trans, async->root); |
2888 | if (ret && !async->error) | |
2889 | async->error = ret; | |
2890 | done: | |
2891 | if (async->sync) | |
2892 | complete(&async->wait); | |
2893 | else | |
2894 | kfree(async); | |
2895 | } | |
2896 | ||
2897 | int btrfs_async_run_delayed_refs(struct btrfs_root *root, | |
31b9655f | 2898 | unsigned long count, u64 transid, int wait) |
a79b7d4b | 2899 | { |
0b246afa | 2900 | struct btrfs_fs_info *fs_info = root->fs_info; |
a79b7d4b CM |
2901 | struct async_delayed_refs *async; |
2902 | int ret; | |
2903 | ||
2904 | async = kmalloc(sizeof(*async), GFP_NOFS); | |
2905 | if (!async) | |
2906 | return -ENOMEM; | |
2907 | ||
0b246afa | 2908 | async->root = fs_info->tree_root; |
a79b7d4b CM |
2909 | async->count = count; |
2910 | async->error = 0; | |
31b9655f | 2911 | async->transid = transid; |
a79b7d4b CM |
2912 | if (wait) |
2913 | async->sync = 1; | |
2914 | else | |
2915 | async->sync = 0; | |
2916 | init_completion(&async->wait); | |
2917 | ||
9e0af237 LB |
2918 | btrfs_init_work(&async->work, btrfs_extent_refs_helper, |
2919 | delayed_ref_async_start, NULL, NULL); | |
a79b7d4b | 2920 | |
0b246afa | 2921 | btrfs_queue_work(fs_info->extent_workers, &async->work); |
a79b7d4b CM |
2922 | |
2923 | if (wait) { | |
2924 | wait_for_completion(&async->wait); | |
2925 | ret = async->error; | |
2926 | kfree(async); | |
2927 | return ret; | |
2928 | } | |
2929 | return 0; | |
2930 | } | |
2931 | ||
c3e69d58 CM |
2932 | /* |
2933 | * this starts processing the delayed reference count updates and | |
2934 | * extent insertions we have queued up so far. count can be | |
2935 | * 0, which means to process everything in the tree at the start | |
2936 | * of the run (but not newly added entries), or it can be some target | |
2937 | * number you'd like to process. | |
79787eaa JM |
2938 | * |
2939 | * Returns 0 on success or if called with an aborted transaction | |
2940 | * Returns <0 on error and aborts the transaction | |
c3e69d58 CM |
2941 | */ |
2942 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | |
2943 | struct btrfs_root *root, unsigned long count) | |
2944 | { | |
0b246afa | 2945 | struct btrfs_fs_info *fs_info = root->fs_info; |
c3e69d58 CM |
2946 | struct rb_node *node; |
2947 | struct btrfs_delayed_ref_root *delayed_refs; | |
c46effa6 | 2948 | struct btrfs_delayed_ref_head *head; |
c3e69d58 CM |
2949 | int ret; |
2950 | int run_all = count == (unsigned long)-1; | |
d9a0540a | 2951 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
c3e69d58 | 2952 | |
79787eaa JM |
2953 | /* We'll clean this up in btrfs_cleanup_transaction */ |
2954 | if (trans->aborted) | |
2955 | return 0; | |
2956 | ||
0b246afa | 2957 | if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags)) |
511711af CM |
2958 | return 0; |
2959 | ||
0b246afa JM |
2960 | if (root == fs_info->extent_root) |
2961 | root = fs_info->tree_root; | |
c3e69d58 CM |
2962 | |
2963 | delayed_refs = &trans->transaction->delayed_refs; | |
26455d33 | 2964 | if (count == 0) |
d7df2c79 | 2965 | count = atomic_read(&delayed_refs->num_entries) * 2; |
bb721703 | 2966 | |
c3e69d58 | 2967 | again: |
709c0486 AJ |
2968 | #ifdef SCRAMBLE_DELAYED_REFS |
2969 | delayed_refs->run_delayed_start = find_middle(&delayed_refs->root); | |
2970 | #endif | |
d9a0540a | 2971 | trans->can_flush_pending_bgs = false; |
d7df2c79 JB |
2972 | ret = __btrfs_run_delayed_refs(trans, root, count); |
2973 | if (ret < 0) { | |
66642832 | 2974 | btrfs_abort_transaction(trans, ret); |
d7df2c79 | 2975 | return ret; |
eb099670 | 2976 | } |
c3e69d58 | 2977 | |
56bec294 | 2978 | if (run_all) { |
d7df2c79 | 2979 | if (!list_empty(&trans->new_bgs)) |
ea658bad | 2980 | btrfs_create_pending_block_groups(trans, root); |
ea658bad | 2981 | |
d7df2c79 | 2982 | spin_lock(&delayed_refs->lock); |
c46effa6 | 2983 | node = rb_first(&delayed_refs->href_root); |
d7df2c79 JB |
2984 | if (!node) { |
2985 | spin_unlock(&delayed_refs->lock); | |
56bec294 | 2986 | goto out; |
d7df2c79 | 2987 | } |
e9d0b13b | 2988 | |
56bec294 | 2989 | while (node) { |
c46effa6 LB |
2990 | head = rb_entry(node, struct btrfs_delayed_ref_head, |
2991 | href_node); | |
2992 | if (btrfs_delayed_ref_is_head(&head->node)) { | |
2993 | struct btrfs_delayed_ref_node *ref; | |
5caf2a00 | 2994 | |
c46effa6 | 2995 | ref = &head->node; |
56bec294 CM |
2996 | atomic_inc(&ref->refs); |
2997 | ||
2998 | spin_unlock(&delayed_refs->lock); | |
8cc33e5c DS |
2999 | /* |
3000 | * Mutex was contended, block until it's | |
3001 | * released and try again | |
3002 | */ | |
56bec294 CM |
3003 | mutex_lock(&head->mutex); |
3004 | mutex_unlock(&head->mutex); | |
3005 | ||
3006 | btrfs_put_delayed_ref(ref); | |
1887be66 | 3007 | cond_resched(); |
56bec294 | 3008 | goto again; |
c46effa6 LB |
3009 | } else { |
3010 | WARN_ON(1); | |
56bec294 CM |
3011 | } |
3012 | node = rb_next(node); | |
3013 | } | |
3014 | spin_unlock(&delayed_refs->lock); | |
d7df2c79 | 3015 | cond_resched(); |
56bec294 | 3016 | goto again; |
5f39d397 | 3017 | } |
54aa1f4d | 3018 | out: |
edf39272 | 3019 | assert_qgroups_uptodate(trans); |
d9a0540a | 3020 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
a28ec197 CM |
3021 | return 0; |
3022 | } | |
3023 | ||
5d4f98a2 YZ |
3024 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
3025 | struct btrfs_root *root, | |
3026 | u64 bytenr, u64 num_bytes, u64 flags, | |
b1c79e09 | 3027 | int level, int is_data) |
5d4f98a2 | 3028 | { |
0b246afa | 3029 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
3030 | struct btrfs_delayed_extent_op *extent_op; |
3031 | int ret; | |
3032 | ||
78a6184a | 3033 | extent_op = btrfs_alloc_delayed_extent_op(); |
5d4f98a2 YZ |
3034 | if (!extent_op) |
3035 | return -ENOMEM; | |
3036 | ||
3037 | extent_op->flags_to_set = flags; | |
35b3ad50 DS |
3038 | extent_op->update_flags = true; |
3039 | extent_op->update_key = false; | |
3040 | extent_op->is_data = is_data ? true : false; | |
b1c79e09 | 3041 | extent_op->level = level; |
5d4f98a2 | 3042 | |
0b246afa | 3043 | ret = btrfs_add_delayed_extent_op(fs_info, trans, bytenr, |
66d7e7f0 | 3044 | num_bytes, extent_op); |
5d4f98a2 | 3045 | if (ret) |
78a6184a | 3046 | btrfs_free_delayed_extent_op(extent_op); |
5d4f98a2 YZ |
3047 | return ret; |
3048 | } | |
3049 | ||
3050 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, | |
3051 | struct btrfs_root *root, | |
3052 | struct btrfs_path *path, | |
3053 | u64 objectid, u64 offset, u64 bytenr) | |
3054 | { | |
3055 | struct btrfs_delayed_ref_head *head; | |
3056 | struct btrfs_delayed_ref_node *ref; | |
3057 | struct btrfs_delayed_data_ref *data_ref; | |
3058 | struct btrfs_delayed_ref_root *delayed_refs; | |
5d4f98a2 YZ |
3059 | int ret = 0; |
3060 | ||
5d4f98a2 YZ |
3061 | delayed_refs = &trans->transaction->delayed_refs; |
3062 | spin_lock(&delayed_refs->lock); | |
3063 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
d7df2c79 JB |
3064 | if (!head) { |
3065 | spin_unlock(&delayed_refs->lock); | |
3066 | return 0; | |
3067 | } | |
5d4f98a2 YZ |
3068 | |
3069 | if (!mutex_trylock(&head->mutex)) { | |
3070 | atomic_inc(&head->node.refs); | |
3071 | spin_unlock(&delayed_refs->lock); | |
3072 | ||
b3b4aa74 | 3073 | btrfs_release_path(path); |
5d4f98a2 | 3074 | |
8cc33e5c DS |
3075 | /* |
3076 | * Mutex was contended, block until it's released and let | |
3077 | * caller try again | |
3078 | */ | |
5d4f98a2 YZ |
3079 | mutex_lock(&head->mutex); |
3080 | mutex_unlock(&head->mutex); | |
3081 | btrfs_put_delayed_ref(&head->node); | |
3082 | return -EAGAIN; | |
3083 | } | |
d7df2c79 | 3084 | spin_unlock(&delayed_refs->lock); |
5d4f98a2 | 3085 | |
d7df2c79 | 3086 | spin_lock(&head->lock); |
c6fc2454 | 3087 | list_for_each_entry(ref, &head->ref_list, list) { |
d7df2c79 JB |
3088 | /* If it's a shared ref we know a cross reference exists */ |
3089 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) { | |
3090 | ret = 1; | |
3091 | break; | |
3092 | } | |
5d4f98a2 | 3093 | |
d7df2c79 | 3094 | data_ref = btrfs_delayed_node_to_data_ref(ref); |
5d4f98a2 | 3095 | |
d7df2c79 JB |
3096 | /* |
3097 | * If our ref doesn't match the one we're currently looking at | |
3098 | * then we have a cross reference. | |
3099 | */ | |
3100 | if (data_ref->root != root->root_key.objectid || | |
3101 | data_ref->objectid != objectid || | |
3102 | data_ref->offset != offset) { | |
3103 | ret = 1; | |
3104 | break; | |
3105 | } | |
5d4f98a2 | 3106 | } |
d7df2c79 | 3107 | spin_unlock(&head->lock); |
5d4f98a2 | 3108 | mutex_unlock(&head->mutex); |
5d4f98a2 YZ |
3109 | return ret; |
3110 | } | |
3111 | ||
3112 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, | |
3113 | struct btrfs_root *root, | |
3114 | struct btrfs_path *path, | |
3115 | u64 objectid, u64 offset, u64 bytenr) | |
be20aa9d | 3116 | { |
0b246afa JM |
3117 | struct btrfs_fs_info *fs_info = root->fs_info; |
3118 | struct btrfs_root *extent_root = fs_info->extent_root; | |
f321e491 | 3119 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
3120 | struct btrfs_extent_data_ref *ref; |
3121 | struct btrfs_extent_inline_ref *iref; | |
3122 | struct btrfs_extent_item *ei; | |
f321e491 | 3123 | struct btrfs_key key; |
5d4f98a2 | 3124 | u32 item_size; |
be20aa9d | 3125 | int ret; |
925baedd | 3126 | |
be20aa9d | 3127 | key.objectid = bytenr; |
31840ae1 | 3128 | key.offset = (u64)-1; |
f321e491 | 3129 | key.type = BTRFS_EXTENT_ITEM_KEY; |
be20aa9d | 3130 | |
be20aa9d CM |
3131 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
3132 | if (ret < 0) | |
3133 | goto out; | |
79787eaa | 3134 | BUG_ON(ret == 0); /* Corruption */ |
80ff3856 YZ |
3135 | |
3136 | ret = -ENOENT; | |
3137 | if (path->slots[0] == 0) | |
31840ae1 | 3138 | goto out; |
be20aa9d | 3139 | |
31840ae1 | 3140 | path->slots[0]--; |
f321e491 | 3141 | leaf = path->nodes[0]; |
5d4f98a2 | 3142 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
be20aa9d | 3143 | |
5d4f98a2 | 3144 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
be20aa9d | 3145 | goto out; |
f321e491 | 3146 | |
5d4f98a2 YZ |
3147 | ret = 1; |
3148 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
3149 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
3150 | if (item_size < sizeof(*ei)) { | |
3151 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); | |
3152 | goto out; | |
3153 | } | |
3154 | #endif | |
3155 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
bd09835d | 3156 | |
5d4f98a2 YZ |
3157 | if (item_size != sizeof(*ei) + |
3158 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) | |
3159 | goto out; | |
be20aa9d | 3160 | |
5d4f98a2 YZ |
3161 | if (btrfs_extent_generation(leaf, ei) <= |
3162 | btrfs_root_last_snapshot(&root->root_item)) | |
3163 | goto out; | |
3164 | ||
3165 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); | |
3166 | if (btrfs_extent_inline_ref_type(leaf, iref) != | |
3167 | BTRFS_EXTENT_DATA_REF_KEY) | |
3168 | goto out; | |
3169 | ||
3170 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
3171 | if (btrfs_extent_refs(leaf, ei) != | |
3172 | btrfs_extent_data_ref_count(leaf, ref) || | |
3173 | btrfs_extent_data_ref_root(leaf, ref) != | |
3174 | root->root_key.objectid || | |
3175 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || | |
3176 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
3177 | goto out; | |
3178 | ||
3179 | ret = 0; | |
3180 | out: | |
3181 | return ret; | |
3182 | } | |
3183 | ||
3184 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, | |
3185 | struct btrfs_root *root, | |
3186 | u64 objectid, u64 offset, u64 bytenr) | |
3187 | { | |
3188 | struct btrfs_path *path; | |
3189 | int ret; | |
3190 | int ret2; | |
3191 | ||
3192 | path = btrfs_alloc_path(); | |
3193 | if (!path) | |
3194 | return -ENOENT; | |
3195 | ||
3196 | do { | |
3197 | ret = check_committed_ref(trans, root, path, objectid, | |
3198 | offset, bytenr); | |
3199 | if (ret && ret != -ENOENT) | |
f321e491 | 3200 | goto out; |
80ff3856 | 3201 | |
5d4f98a2 YZ |
3202 | ret2 = check_delayed_ref(trans, root, path, objectid, |
3203 | offset, bytenr); | |
3204 | } while (ret2 == -EAGAIN); | |
3205 | ||
3206 | if (ret2 && ret2 != -ENOENT) { | |
3207 | ret = ret2; | |
3208 | goto out; | |
f321e491 | 3209 | } |
5d4f98a2 YZ |
3210 | |
3211 | if (ret != -ENOENT || ret2 != -ENOENT) | |
3212 | ret = 0; | |
be20aa9d | 3213 | out: |
80ff3856 | 3214 | btrfs_free_path(path); |
f0486c68 YZ |
3215 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
3216 | WARN_ON(ret > 0); | |
f321e491 | 3217 | return ret; |
be20aa9d | 3218 | } |
c5739bba | 3219 | |
5d4f98a2 | 3220 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
b7a9f29f | 3221 | struct btrfs_root *root, |
5d4f98a2 | 3222 | struct extent_buffer *buf, |
e339a6b0 | 3223 | int full_backref, int inc) |
31840ae1 | 3224 | { |
0b246afa | 3225 | struct btrfs_fs_info *fs_info = root->fs_info; |
31840ae1 | 3226 | u64 bytenr; |
5d4f98a2 YZ |
3227 | u64 num_bytes; |
3228 | u64 parent; | |
31840ae1 | 3229 | u64 ref_root; |
31840ae1 | 3230 | u32 nritems; |
31840ae1 ZY |
3231 | struct btrfs_key key; |
3232 | struct btrfs_file_extent_item *fi; | |
3233 | int i; | |
3234 | int level; | |
3235 | int ret = 0; | |
31840ae1 | 3236 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
b06c4bf5 | 3237 | u64, u64, u64, u64, u64, u64); |
31840ae1 | 3238 | |
fccb84c9 | 3239 | |
0b246afa | 3240 | if (btrfs_is_testing(fs_info)) |
faa2dbf0 | 3241 | return 0; |
fccb84c9 | 3242 | |
31840ae1 | 3243 | ref_root = btrfs_header_owner(buf); |
31840ae1 ZY |
3244 | nritems = btrfs_header_nritems(buf); |
3245 | level = btrfs_header_level(buf); | |
3246 | ||
27cdeb70 | 3247 | if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0) |
5d4f98a2 | 3248 | return 0; |
31840ae1 | 3249 | |
5d4f98a2 YZ |
3250 | if (inc) |
3251 | process_func = btrfs_inc_extent_ref; | |
3252 | else | |
3253 | process_func = btrfs_free_extent; | |
31840ae1 | 3254 | |
5d4f98a2 YZ |
3255 | if (full_backref) |
3256 | parent = buf->start; | |
3257 | else | |
3258 | parent = 0; | |
3259 | ||
3260 | for (i = 0; i < nritems; i++) { | |
31840ae1 | 3261 | if (level == 0) { |
5d4f98a2 | 3262 | btrfs_item_key_to_cpu(buf, &key, i); |
962a298f | 3263 | if (key.type != BTRFS_EXTENT_DATA_KEY) |
31840ae1 | 3264 | continue; |
5d4f98a2 | 3265 | fi = btrfs_item_ptr(buf, i, |
31840ae1 ZY |
3266 | struct btrfs_file_extent_item); |
3267 | if (btrfs_file_extent_type(buf, fi) == | |
3268 | BTRFS_FILE_EXTENT_INLINE) | |
3269 | continue; | |
3270 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
3271 | if (bytenr == 0) | |
3272 | continue; | |
5d4f98a2 YZ |
3273 | |
3274 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); | |
3275 | key.offset -= btrfs_file_extent_offset(buf, fi); | |
3276 | ret = process_func(trans, root, bytenr, num_bytes, | |
3277 | parent, ref_root, key.objectid, | |
b06c4bf5 | 3278 | key.offset); |
31840ae1 ZY |
3279 | if (ret) |
3280 | goto fail; | |
3281 | } else { | |
5d4f98a2 | 3282 | bytenr = btrfs_node_blockptr(buf, i); |
0b246afa | 3283 | num_bytes = fs_info->nodesize; |
5d4f98a2 | 3284 | ret = process_func(trans, root, bytenr, num_bytes, |
b06c4bf5 | 3285 | parent, ref_root, level - 1, 0); |
31840ae1 ZY |
3286 | if (ret) |
3287 | goto fail; | |
3288 | } | |
3289 | } | |
3290 | return 0; | |
3291 | fail: | |
5d4f98a2 YZ |
3292 | return ret; |
3293 | } | |
3294 | ||
3295 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
e339a6b0 | 3296 | struct extent_buffer *buf, int full_backref) |
5d4f98a2 | 3297 | { |
e339a6b0 | 3298 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); |
5d4f98a2 YZ |
3299 | } |
3300 | ||
3301 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
e339a6b0 | 3302 | struct extent_buffer *buf, int full_backref) |
5d4f98a2 | 3303 | { |
e339a6b0 | 3304 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); |
31840ae1 ZY |
3305 | } |
3306 | ||
9078a3e1 CM |
3307 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
3308 | struct btrfs_root *root, | |
3309 | struct btrfs_path *path, | |
3310 | struct btrfs_block_group_cache *cache) | |
3311 | { | |
3312 | int ret; | |
0b246afa JM |
3313 | struct btrfs_fs_info *fs_info = root->fs_info; |
3314 | struct btrfs_root *extent_root = fs_info->extent_root; | |
5f39d397 CM |
3315 | unsigned long bi; |
3316 | struct extent_buffer *leaf; | |
9078a3e1 | 3317 | |
9078a3e1 | 3318 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
df95e7f0 JB |
3319 | if (ret) { |
3320 | if (ret > 0) | |
3321 | ret = -ENOENT; | |
54aa1f4d | 3322 | goto fail; |
df95e7f0 | 3323 | } |
5f39d397 CM |
3324 | |
3325 | leaf = path->nodes[0]; | |
3326 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
3327 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
3328 | btrfs_mark_buffer_dirty(leaf); | |
54aa1f4d | 3329 | fail: |
24b89d08 | 3330 | btrfs_release_path(path); |
df95e7f0 | 3331 | return ret; |
9078a3e1 CM |
3332 | |
3333 | } | |
3334 | ||
4a8c9a62 YZ |
3335 | static struct btrfs_block_group_cache * |
3336 | next_block_group(struct btrfs_root *root, | |
3337 | struct btrfs_block_group_cache *cache) | |
3338 | { | |
0b246afa | 3339 | struct btrfs_fs_info *fs_info = root->fs_info; |
4a8c9a62 | 3340 | struct rb_node *node; |
292cbd51 | 3341 | |
0b246afa | 3342 | spin_lock(&fs_info->block_group_cache_lock); |
292cbd51 FM |
3343 | |
3344 | /* If our block group was removed, we need a full search. */ | |
3345 | if (RB_EMPTY_NODE(&cache->cache_node)) { | |
3346 | const u64 next_bytenr = cache->key.objectid + cache->key.offset; | |
3347 | ||
0b246afa | 3348 | spin_unlock(&fs_info->block_group_cache_lock); |
292cbd51 | 3349 | btrfs_put_block_group(cache); |
0b246afa | 3350 | cache = btrfs_lookup_first_block_group(fs_info, next_bytenr); return cache; |
292cbd51 | 3351 | } |
4a8c9a62 YZ |
3352 | node = rb_next(&cache->cache_node); |
3353 | btrfs_put_block_group(cache); | |
3354 | if (node) { | |
3355 | cache = rb_entry(node, struct btrfs_block_group_cache, | |
3356 | cache_node); | |
11dfe35a | 3357 | btrfs_get_block_group(cache); |
4a8c9a62 YZ |
3358 | } else |
3359 | cache = NULL; | |
0b246afa | 3360 | spin_unlock(&fs_info->block_group_cache_lock); |
4a8c9a62 YZ |
3361 | return cache; |
3362 | } | |
3363 | ||
0af3d00b JB |
3364 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
3365 | struct btrfs_trans_handle *trans, | |
3366 | struct btrfs_path *path) | |
3367 | { | |
0b246afa JM |
3368 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
3369 | struct btrfs_root *root = fs_info->tree_root; | |
0af3d00b JB |
3370 | struct inode *inode = NULL; |
3371 | u64 alloc_hint = 0; | |
2b20982e | 3372 | int dcs = BTRFS_DC_ERROR; |
f8c269d7 | 3373 | u64 num_pages = 0; |
0af3d00b JB |
3374 | int retries = 0; |
3375 | int ret = 0; | |
3376 | ||
3377 | /* | |
3378 | * If this block group is smaller than 100 megs don't bother caching the | |
3379 | * block group. | |
3380 | */ | |
ee22184b | 3381 | if (block_group->key.offset < (100 * SZ_1M)) { |
0af3d00b JB |
3382 | spin_lock(&block_group->lock); |
3383 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; | |
3384 | spin_unlock(&block_group->lock); | |
3385 | return 0; | |
3386 | } | |
3387 | ||
0c0ef4bc JB |
3388 | if (trans->aborted) |
3389 | return 0; | |
0af3d00b JB |
3390 | again: |
3391 | inode = lookup_free_space_inode(root, block_group, path); | |
3392 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { | |
3393 | ret = PTR_ERR(inode); | |
b3b4aa74 | 3394 | btrfs_release_path(path); |
0af3d00b JB |
3395 | goto out; |
3396 | } | |
3397 | ||
3398 | if (IS_ERR(inode)) { | |
3399 | BUG_ON(retries); | |
3400 | retries++; | |
3401 | ||
3402 | if (block_group->ro) | |
3403 | goto out_free; | |
3404 | ||
3405 | ret = create_free_space_inode(root, trans, block_group, path); | |
3406 | if (ret) | |
3407 | goto out_free; | |
3408 | goto again; | |
3409 | } | |
3410 | ||
5b0e95bf JB |
3411 | /* We've already setup this transaction, go ahead and exit */ |
3412 | if (block_group->cache_generation == trans->transid && | |
3413 | i_size_read(inode)) { | |
3414 | dcs = BTRFS_DC_SETUP; | |
3415 | goto out_put; | |
3416 | } | |
3417 | ||
0af3d00b JB |
3418 | /* |
3419 | * We want to set the generation to 0, that way if anything goes wrong | |
3420 | * from here on out we know not to trust this cache when we load up next | |
3421 | * time. | |
3422 | */ | |
3423 | BTRFS_I(inode)->generation = 0; | |
3424 | ret = btrfs_update_inode(trans, root, inode); | |
0c0ef4bc JB |
3425 | if (ret) { |
3426 | /* | |
3427 | * So theoretically we could recover from this, simply set the | |
3428 | * super cache generation to 0 so we know to invalidate the | |
3429 | * cache, but then we'd have to keep track of the block groups | |
3430 | * that fail this way so we know we _have_ to reset this cache | |
3431 | * before the next commit or risk reading stale cache. So to | |
3432 | * limit our exposure to horrible edge cases lets just abort the | |
3433 | * transaction, this only happens in really bad situations | |
3434 | * anyway. | |
3435 | */ | |
66642832 | 3436 | btrfs_abort_transaction(trans, ret); |
0c0ef4bc JB |
3437 | goto out_put; |
3438 | } | |
0af3d00b JB |
3439 | WARN_ON(ret); |
3440 | ||
3441 | if (i_size_read(inode) > 0) { | |
7b61cd92 | 3442 | ret = btrfs_check_trunc_cache_free_space(root, |
0b246afa | 3443 | &fs_info->global_block_rsv); |
7b61cd92 MX |
3444 | if (ret) |
3445 | goto out_put; | |
3446 | ||
1bbc621e | 3447 | ret = btrfs_truncate_free_space_cache(root, trans, NULL, inode); |
0af3d00b JB |
3448 | if (ret) |
3449 | goto out_put; | |
3450 | } | |
3451 | ||
3452 | spin_lock(&block_group->lock); | |
cf7c1ef6 | 3453 | if (block_group->cached != BTRFS_CACHE_FINISHED || |
0b246afa | 3454 | !btrfs_test_opt(fs_info, SPACE_CACHE)) { |
cf7c1ef6 LB |
3455 | /* |
3456 | * don't bother trying to write stuff out _if_ | |
3457 | * a) we're not cached, | |
3458 | * b) we're with nospace_cache mount option. | |
3459 | */ | |
2b20982e | 3460 | dcs = BTRFS_DC_WRITTEN; |
0af3d00b JB |
3461 | spin_unlock(&block_group->lock); |
3462 | goto out_put; | |
3463 | } | |
3464 | spin_unlock(&block_group->lock); | |
3465 | ||
2968b1f4 JB |
3466 | /* |
3467 | * We hit an ENOSPC when setting up the cache in this transaction, just | |
3468 | * skip doing the setup, we've already cleared the cache so we're safe. | |
3469 | */ | |
3470 | if (test_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags)) { | |
3471 | ret = -ENOSPC; | |
3472 | goto out_put; | |
3473 | } | |
3474 | ||
6fc823b1 JB |
3475 | /* |
3476 | * Try to preallocate enough space based on how big the block group is. | |
3477 | * Keep in mind this has to include any pinned space which could end up | |
3478 | * taking up quite a bit since it's not folded into the other space | |
3479 | * cache. | |
3480 | */ | |
ee22184b | 3481 | num_pages = div_u64(block_group->key.offset, SZ_256M); |
0af3d00b JB |
3482 | if (!num_pages) |
3483 | num_pages = 1; | |
3484 | ||
0af3d00b | 3485 | num_pages *= 16; |
09cbfeaf | 3486 | num_pages *= PAGE_SIZE; |
0af3d00b | 3487 | |
7cf5b976 | 3488 | ret = btrfs_check_data_free_space(inode, 0, num_pages); |
0af3d00b JB |
3489 | if (ret) |
3490 | goto out_put; | |
3491 | ||
3492 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, | |
3493 | num_pages, num_pages, | |
3494 | &alloc_hint); | |
2968b1f4 JB |
3495 | /* |
3496 | * Our cache requires contiguous chunks so that we don't modify a bunch | |
3497 | * of metadata or split extents when writing the cache out, which means | |
3498 | * we can enospc if we are heavily fragmented in addition to just normal | |
3499 | * out of space conditions. So if we hit this just skip setting up any | |
3500 | * other block groups for this transaction, maybe we'll unpin enough | |
3501 | * space the next time around. | |
3502 | */ | |
2b20982e JB |
3503 | if (!ret) |
3504 | dcs = BTRFS_DC_SETUP; | |
2968b1f4 JB |
3505 | else if (ret == -ENOSPC) |
3506 | set_bit(BTRFS_TRANS_CACHE_ENOSPC, &trans->transaction->flags); | |
c09544e0 | 3507 | |
0af3d00b JB |
3508 | out_put: |
3509 | iput(inode); | |
3510 | out_free: | |
b3b4aa74 | 3511 | btrfs_release_path(path); |
0af3d00b JB |
3512 | out: |
3513 | spin_lock(&block_group->lock); | |
e65cbb94 | 3514 | if (!ret && dcs == BTRFS_DC_SETUP) |
5b0e95bf | 3515 | block_group->cache_generation = trans->transid; |
2b20982e | 3516 | block_group->disk_cache_state = dcs; |
0af3d00b JB |
3517 | spin_unlock(&block_group->lock); |
3518 | ||
3519 | return ret; | |
3520 | } | |
3521 | ||
dcdf7f6d JB |
3522 | int btrfs_setup_space_cache(struct btrfs_trans_handle *trans, |
3523 | struct btrfs_root *root) | |
3524 | { | |
0b246afa | 3525 | struct btrfs_fs_info *fs_info = root->fs_info; |
dcdf7f6d JB |
3526 | struct btrfs_block_group_cache *cache, *tmp; |
3527 | struct btrfs_transaction *cur_trans = trans->transaction; | |
3528 | struct btrfs_path *path; | |
3529 | ||
3530 | if (list_empty(&cur_trans->dirty_bgs) || | |
0b246afa | 3531 | !btrfs_test_opt(fs_info, SPACE_CACHE)) |
dcdf7f6d JB |
3532 | return 0; |
3533 | ||
3534 | path = btrfs_alloc_path(); | |
3535 | if (!path) | |
3536 | return -ENOMEM; | |
3537 | ||
3538 | /* Could add new block groups, use _safe just in case */ | |
3539 | list_for_each_entry_safe(cache, tmp, &cur_trans->dirty_bgs, | |
3540 | dirty_list) { | |
3541 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) | |
3542 | cache_save_setup(cache, trans, path); | |
3543 | } | |
3544 | ||
3545 | btrfs_free_path(path); | |
3546 | return 0; | |
3547 | } | |
3548 | ||
1bbc621e CM |
3549 | /* |
3550 | * transaction commit does final block group cache writeback during a | |
3551 | * critical section where nothing is allowed to change the FS. This is | |
3552 | * required in order for the cache to actually match the block group, | |
3553 | * but can introduce a lot of latency into the commit. | |
3554 | * | |
3555 | * So, btrfs_start_dirty_block_groups is here to kick off block group | |
3556 | * cache IO. There's a chance we'll have to redo some of it if the | |
3557 | * block group changes again during the commit, but it greatly reduces | |
3558 | * the commit latency by getting rid of the easy block groups while | |
3559 | * we're still allowing others to join the commit. | |
3560 | */ | |
3561 | int btrfs_start_dirty_block_groups(struct btrfs_trans_handle *trans, | |
96b5179d | 3562 | struct btrfs_root *root) |
9078a3e1 | 3563 | { |
0b246afa | 3564 | struct btrfs_fs_info *fs_info = root->fs_info; |
4a8c9a62 | 3565 | struct btrfs_block_group_cache *cache; |
ce93ec54 JB |
3566 | struct btrfs_transaction *cur_trans = trans->transaction; |
3567 | int ret = 0; | |
c9dc4c65 | 3568 | int should_put; |
1bbc621e CM |
3569 | struct btrfs_path *path = NULL; |
3570 | LIST_HEAD(dirty); | |
3571 | struct list_head *io = &cur_trans->io_bgs; | |
c9dc4c65 | 3572 | int num_started = 0; |
1bbc621e CM |
3573 | int loops = 0; |
3574 | ||
3575 | spin_lock(&cur_trans->dirty_bgs_lock); | |
b58d1a9e FM |
3576 | if (list_empty(&cur_trans->dirty_bgs)) { |
3577 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
3578 | return 0; | |
1bbc621e | 3579 | } |
b58d1a9e | 3580 | list_splice_init(&cur_trans->dirty_bgs, &dirty); |
1bbc621e | 3581 | spin_unlock(&cur_trans->dirty_bgs_lock); |
ce93ec54 | 3582 | |
1bbc621e | 3583 | again: |
1bbc621e CM |
3584 | /* |
3585 | * make sure all the block groups on our dirty list actually | |
3586 | * exist | |
3587 | */ | |
3588 | btrfs_create_pending_block_groups(trans, root); | |
3589 | ||
3590 | if (!path) { | |
3591 | path = btrfs_alloc_path(); | |
3592 | if (!path) | |
3593 | return -ENOMEM; | |
3594 | } | |
3595 | ||
b58d1a9e FM |
3596 | /* |
3597 | * cache_write_mutex is here only to save us from balance or automatic | |
3598 | * removal of empty block groups deleting this block group while we are | |
3599 | * writing out the cache | |
3600 | */ | |
3601 | mutex_lock(&trans->transaction->cache_write_mutex); | |
1bbc621e CM |
3602 | while (!list_empty(&dirty)) { |
3603 | cache = list_first_entry(&dirty, | |
3604 | struct btrfs_block_group_cache, | |
3605 | dirty_list); | |
1bbc621e CM |
3606 | /* |
3607 | * this can happen if something re-dirties a block | |
3608 | * group that is already under IO. Just wait for it to | |
3609 | * finish and then do it all again | |
3610 | */ | |
3611 | if (!list_empty(&cache->io_list)) { | |
3612 | list_del_init(&cache->io_list); | |
afdb5718 | 3613 | btrfs_wait_cache_io(trans, cache, path); |
1bbc621e CM |
3614 | btrfs_put_block_group(cache); |
3615 | } | |
3616 | ||
3617 | ||
3618 | /* | |
3619 | * btrfs_wait_cache_io uses the cache->dirty_list to decide | |
3620 | * if it should update the cache_state. Don't delete | |
3621 | * until after we wait. | |
3622 | * | |
3623 | * Since we're not running in the commit critical section | |
3624 | * we need the dirty_bgs_lock to protect from update_block_group | |
3625 | */ | |
3626 | spin_lock(&cur_trans->dirty_bgs_lock); | |
3627 | list_del_init(&cache->dirty_list); | |
3628 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
3629 | ||
3630 | should_put = 1; | |
3631 | ||
3632 | cache_save_setup(cache, trans, path); | |
3633 | ||
3634 | if (cache->disk_cache_state == BTRFS_DC_SETUP) { | |
3635 | cache->io_ctl.inode = NULL; | |
0b246afa | 3636 | ret = btrfs_write_out_cache(fs_info, trans, |
5b4aacef | 3637 | cache, path); |
1bbc621e CM |
3638 | if (ret == 0 && cache->io_ctl.inode) { |
3639 | num_started++; | |
3640 | should_put = 0; | |
3641 | ||
3642 | /* | |
3643 | * the cache_write_mutex is protecting | |
3644 | * the io_list | |
3645 | */ | |
3646 | list_add_tail(&cache->io_list, io); | |
3647 | } else { | |
3648 | /* | |
3649 | * if we failed to write the cache, the | |
3650 | * generation will be bad and life goes on | |
3651 | */ | |
3652 | ret = 0; | |
3653 | } | |
3654 | } | |
ff1f8250 | 3655 | if (!ret) { |
1bbc621e | 3656 | ret = write_one_cache_group(trans, root, path, cache); |
ff1f8250 FM |
3657 | /* |
3658 | * Our block group might still be attached to the list | |
3659 | * of new block groups in the transaction handle of some | |
3660 | * other task (struct btrfs_trans_handle->new_bgs). This | |
3661 | * means its block group item isn't yet in the extent | |
3662 | * tree. If this happens ignore the error, as we will | |
3663 | * try again later in the critical section of the | |
3664 | * transaction commit. | |
3665 | */ | |
3666 | if (ret == -ENOENT) { | |
3667 | ret = 0; | |
3668 | spin_lock(&cur_trans->dirty_bgs_lock); | |
3669 | if (list_empty(&cache->dirty_list)) { | |
3670 | list_add_tail(&cache->dirty_list, | |
3671 | &cur_trans->dirty_bgs); | |
3672 | btrfs_get_block_group(cache); | |
3673 | } | |
3674 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
3675 | } else if (ret) { | |
66642832 | 3676 | btrfs_abort_transaction(trans, ret); |
ff1f8250 FM |
3677 | } |
3678 | } | |
1bbc621e CM |
3679 | |
3680 | /* if its not on the io list, we need to put the block group */ | |
3681 | if (should_put) | |
3682 | btrfs_put_block_group(cache); | |
3683 | ||
3684 | if (ret) | |
3685 | break; | |
b58d1a9e FM |
3686 | |
3687 | /* | |
3688 | * Avoid blocking other tasks for too long. It might even save | |
3689 | * us from writing caches for block groups that are going to be | |
3690 | * removed. | |
3691 | */ | |
3692 | mutex_unlock(&trans->transaction->cache_write_mutex); | |
3693 | mutex_lock(&trans->transaction->cache_write_mutex); | |
1bbc621e | 3694 | } |
b58d1a9e | 3695 | mutex_unlock(&trans->transaction->cache_write_mutex); |
1bbc621e CM |
3696 | |
3697 | /* | |
3698 | * go through delayed refs for all the stuff we've just kicked off | |
3699 | * and then loop back (just once) | |
3700 | */ | |
3701 | ret = btrfs_run_delayed_refs(trans, root, 0); | |
3702 | if (!ret && loops == 0) { | |
3703 | loops++; | |
3704 | spin_lock(&cur_trans->dirty_bgs_lock); | |
3705 | list_splice_init(&cur_trans->dirty_bgs, &dirty); | |
b58d1a9e FM |
3706 | /* |
3707 | * dirty_bgs_lock protects us from concurrent block group | |
3708 | * deletes too (not just cache_write_mutex). | |
3709 | */ | |
3710 | if (!list_empty(&dirty)) { | |
3711 | spin_unlock(&cur_trans->dirty_bgs_lock); | |
3712 | goto again; | |
3713 | } | |
1bbc621e | 3714 | spin_unlock(&cur_trans->dirty_bgs_lock); |
c79a1751 LB |
3715 | } else if (ret < 0) { |
3716 | btrfs_cleanup_dirty_bgs(cur_trans, root); | |
1bbc621e CM |
3717 | } |
3718 | ||
3719 | btrfs_free_path(path); | |
3720 | return ret; | |
3721 | } | |
3722 | ||
3723 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, | |
3724 | struct btrfs_root *root) | |
3725 | { | |
0b246afa | 3726 | struct btrfs_fs_info *fs_info = root->fs_info; |
1bbc621e CM |
3727 | struct btrfs_block_group_cache *cache; |
3728 | struct btrfs_transaction *cur_trans = trans->transaction; | |
3729 | int ret = 0; | |
3730 | int should_put; | |
3731 | struct btrfs_path *path; | |
3732 | struct list_head *io = &cur_trans->io_bgs; | |
3733 | int num_started = 0; | |
9078a3e1 CM |
3734 | |
3735 | path = btrfs_alloc_path(); | |
3736 | if (!path) | |
3737 | return -ENOMEM; | |
3738 | ||
ce93ec54 | 3739 | /* |
e44081ef FM |
3740 | * Even though we are in the critical section of the transaction commit, |
3741 | * we can still have concurrent tasks adding elements to this | |
3742 | * transaction's list of dirty block groups. These tasks correspond to | |
3743 | * endio free space workers started when writeback finishes for a | |
3744 | * space cache, which run inode.c:btrfs_finish_ordered_io(), and can | |
3745 | * allocate new block groups as a result of COWing nodes of the root | |
3746 | * tree when updating the free space inode. The writeback for the space | |
3747 | * caches is triggered by an earlier call to | |
3748 | * btrfs_start_dirty_block_groups() and iterations of the following | |
3749 | * loop. | |
3750 | * Also we want to do the cache_save_setup first and then run the | |
ce93ec54 JB |
3751 | * delayed refs to make sure we have the best chance at doing this all |
3752 | * in one shot. | |
3753 | */ | |
e44081ef | 3754 | spin_lock(&cur_trans->dirty_bgs_lock); |
ce93ec54 JB |
3755 | while (!list_empty(&cur_trans->dirty_bgs)) { |
3756 | cache = list_first_entry(&cur_trans->dirty_bgs, | |
3757 | struct btrfs_block_group_cache, | |
3758 | dirty_list); | |
c9dc4c65 CM |
3759 | |
3760 | /* | |
3761 | * this can happen if cache_save_setup re-dirties a block | |
3762 | * group that is already under IO. Just wait for it to | |
3763 | * finish and then do it all again | |
3764 | */ | |
3765 | if (!list_empty(&cache->io_list)) { | |
e44081ef | 3766 | spin_unlock(&cur_trans->dirty_bgs_lock); |
c9dc4c65 | 3767 | list_del_init(&cache->io_list); |
afdb5718 | 3768 | btrfs_wait_cache_io(trans, cache, path); |
c9dc4c65 | 3769 | btrfs_put_block_group(cache); |
e44081ef | 3770 | spin_lock(&cur_trans->dirty_bgs_lock); |
c9dc4c65 CM |
3771 | } |
3772 | ||
1bbc621e CM |
3773 | /* |
3774 | * don't remove from the dirty list until after we've waited | |
3775 | * on any pending IO | |
3776 | */ | |
ce93ec54 | 3777 | list_del_init(&cache->dirty_list); |
e44081ef | 3778 | spin_unlock(&cur_trans->dirty_bgs_lock); |
c9dc4c65 CM |
3779 | should_put = 1; |
3780 | ||
1bbc621e | 3781 | cache_save_setup(cache, trans, path); |
c9dc4c65 | 3782 | |
ce93ec54 | 3783 | if (!ret) |
c9dc4c65 CM |
3784 | ret = btrfs_run_delayed_refs(trans, root, (unsigned long) -1); |
3785 | ||
3786 | if (!ret && cache->disk_cache_state == BTRFS_DC_SETUP) { | |
3787 | cache->io_ctl.inode = NULL; | |
0b246afa | 3788 | ret = btrfs_write_out_cache(fs_info, trans, |
5b4aacef | 3789 | cache, path); |
c9dc4c65 CM |
3790 | if (ret == 0 && cache->io_ctl.inode) { |
3791 | num_started++; | |
3792 | should_put = 0; | |
1bbc621e | 3793 | list_add_tail(&cache->io_list, io); |
c9dc4c65 CM |
3794 | } else { |
3795 | /* | |
3796 | * if we failed to write the cache, the | |
3797 | * generation will be bad and life goes on | |
3798 | */ | |
3799 | ret = 0; | |
3800 | } | |
3801 | } | |
ff1f8250 | 3802 | if (!ret) { |
ce93ec54 | 3803 | ret = write_one_cache_group(trans, root, path, cache); |
2bc0bb5f FM |
3804 | /* |
3805 | * One of the free space endio workers might have | |
3806 | * created a new block group while updating a free space | |
3807 | * cache's inode (at inode.c:btrfs_finish_ordered_io()) | |
3808 | * and hasn't released its transaction handle yet, in | |
3809 | * which case the new block group is still attached to | |
3810 | * its transaction handle and its creation has not | |
3811 | * finished yet (no block group item in the extent tree | |
3812 | * yet, etc). If this is the case, wait for all free | |
3813 | * space endio workers to finish and retry. This is a | |
3814 | * a very rare case so no need for a more efficient and | |
3815 | * complex approach. | |
3816 | */ | |
3817 | if (ret == -ENOENT) { | |
3818 | wait_event(cur_trans->writer_wait, | |
3819 | atomic_read(&cur_trans->num_writers) == 1); | |
3820 | ret = write_one_cache_group(trans, root, path, | |
3821 | cache); | |
3822 | } | |
ff1f8250 | 3823 | if (ret) |
66642832 | 3824 | btrfs_abort_transaction(trans, ret); |
ff1f8250 | 3825 | } |
c9dc4c65 CM |
3826 | |
3827 | /* if its not on the io list, we need to put the block group */ | |
3828 | if (should_put) | |
3829 | btrfs_put_block_group(cache); | |
e44081ef | 3830 | spin_lock(&cur_trans->dirty_bgs_lock); |
c9dc4c65 | 3831 | } |
e44081ef | 3832 | spin_unlock(&cur_trans->dirty_bgs_lock); |
c9dc4c65 | 3833 | |
1bbc621e CM |
3834 | while (!list_empty(io)) { |
3835 | cache = list_first_entry(io, struct btrfs_block_group_cache, | |
c9dc4c65 CM |
3836 | io_list); |
3837 | list_del_init(&cache->io_list); | |
afdb5718 | 3838 | btrfs_wait_cache_io(trans, cache, path); |
0cb59c99 JB |
3839 | btrfs_put_block_group(cache); |
3840 | } | |
3841 | ||
9078a3e1 | 3842 | btrfs_free_path(path); |
ce93ec54 | 3843 | return ret; |
9078a3e1 CM |
3844 | } |
3845 | ||
d2fb3437 YZ |
3846 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
3847 | { | |
0b246afa | 3848 | struct btrfs_fs_info *fs_info = root->fs_info; |
d2fb3437 YZ |
3849 | struct btrfs_block_group_cache *block_group; |
3850 | int readonly = 0; | |
3851 | ||
0b246afa | 3852 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
d2fb3437 YZ |
3853 | if (!block_group || block_group->ro) |
3854 | readonly = 1; | |
3855 | if (block_group) | |
fa9c0d79 | 3856 | btrfs_put_block_group(block_group); |
d2fb3437 YZ |
3857 | return readonly; |
3858 | } | |
3859 | ||
f78c436c FM |
3860 | bool btrfs_inc_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) |
3861 | { | |
3862 | struct btrfs_block_group_cache *bg; | |
3863 | bool ret = true; | |
3864 | ||
3865 | bg = btrfs_lookup_block_group(fs_info, bytenr); | |
3866 | if (!bg) | |
3867 | return false; | |
3868 | ||
3869 | spin_lock(&bg->lock); | |
3870 | if (bg->ro) | |
3871 | ret = false; | |
3872 | else | |
3873 | atomic_inc(&bg->nocow_writers); | |
3874 | spin_unlock(&bg->lock); | |
3875 | ||
3876 | /* no put on block group, done by btrfs_dec_nocow_writers */ | |
3877 | if (!ret) | |
3878 | btrfs_put_block_group(bg); | |
3879 | ||
3880 | return ret; | |
3881 | ||
3882 | } | |
3883 | ||
3884 | void btrfs_dec_nocow_writers(struct btrfs_fs_info *fs_info, u64 bytenr) | |
3885 | { | |
3886 | struct btrfs_block_group_cache *bg; | |
3887 | ||
3888 | bg = btrfs_lookup_block_group(fs_info, bytenr); | |
3889 | ASSERT(bg); | |
3890 | if (atomic_dec_and_test(&bg->nocow_writers)) | |
3891 | wake_up_atomic_t(&bg->nocow_writers); | |
3892 | /* | |
3893 | * Once for our lookup and once for the lookup done by a previous call | |
3894 | * to btrfs_inc_nocow_writers() | |
3895 | */ | |
3896 | btrfs_put_block_group(bg); | |
3897 | btrfs_put_block_group(bg); | |
3898 | } | |
3899 | ||
3900 | static int btrfs_wait_nocow_writers_atomic_t(atomic_t *a) | |
3901 | { | |
3902 | schedule(); | |
3903 | return 0; | |
3904 | } | |
3905 | ||
3906 | void btrfs_wait_nocow_writers(struct btrfs_block_group_cache *bg) | |
3907 | { | |
3908 | wait_on_atomic_t(&bg->nocow_writers, | |
3909 | btrfs_wait_nocow_writers_atomic_t, | |
3910 | TASK_UNINTERRUPTIBLE); | |
3911 | } | |
3912 | ||
6ab0a202 JM |
3913 | static const char *alloc_name(u64 flags) |
3914 | { | |
3915 | switch (flags) { | |
3916 | case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA: | |
3917 | return "mixed"; | |
3918 | case BTRFS_BLOCK_GROUP_METADATA: | |
3919 | return "metadata"; | |
3920 | case BTRFS_BLOCK_GROUP_DATA: | |
3921 | return "data"; | |
3922 | case BTRFS_BLOCK_GROUP_SYSTEM: | |
3923 | return "system"; | |
3924 | default: | |
3925 | WARN_ON(1); | |
3926 | return "invalid-combination"; | |
3927 | }; | |
3928 | } | |
3929 | ||
593060d7 CM |
3930 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
3931 | u64 total_bytes, u64 bytes_used, | |
e40edf2d | 3932 | u64 bytes_readonly, |
593060d7 CM |
3933 | struct btrfs_space_info **space_info) |
3934 | { | |
3935 | struct btrfs_space_info *found; | |
b742bb82 YZ |
3936 | int i; |
3937 | int factor; | |
b150a4f1 | 3938 | int ret; |
b742bb82 YZ |
3939 | |
3940 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | | |
3941 | BTRFS_BLOCK_GROUP_RAID10)) | |
3942 | factor = 2; | |
3943 | else | |
3944 | factor = 1; | |
593060d7 CM |
3945 | |
3946 | found = __find_space_info(info, flags); | |
3947 | if (found) { | |
25179201 | 3948 | spin_lock(&found->lock); |
593060d7 | 3949 | found->total_bytes += total_bytes; |
89a55897 | 3950 | found->disk_total += total_bytes * factor; |
593060d7 | 3951 | found->bytes_used += bytes_used; |
b742bb82 | 3952 | found->disk_used += bytes_used * factor; |
e40edf2d | 3953 | found->bytes_readonly += bytes_readonly; |
2e6e5183 FM |
3954 | if (total_bytes > 0) |
3955 | found->full = 0; | |
957780eb JB |
3956 | space_info_add_new_bytes(info, found, total_bytes - |
3957 | bytes_used - bytes_readonly); | |
25179201 | 3958 | spin_unlock(&found->lock); |
593060d7 CM |
3959 | *space_info = found; |
3960 | return 0; | |
3961 | } | |
c146afad | 3962 | found = kzalloc(sizeof(*found), GFP_NOFS); |
593060d7 CM |
3963 | if (!found) |
3964 | return -ENOMEM; | |
3965 | ||
908c7f19 | 3966 | ret = percpu_counter_init(&found->total_bytes_pinned, 0, GFP_KERNEL); |
b150a4f1 JB |
3967 | if (ret) { |
3968 | kfree(found); | |
3969 | return ret; | |
3970 | } | |
3971 | ||
c1895442 | 3972 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
b742bb82 | 3973 | INIT_LIST_HEAD(&found->block_groups[i]); |
80eb234a | 3974 | init_rwsem(&found->groups_sem); |
0f9dd46c | 3975 | spin_lock_init(&found->lock); |
52ba6929 | 3976 | found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
593060d7 | 3977 | found->total_bytes = total_bytes; |
89a55897 | 3978 | found->disk_total = total_bytes * factor; |
593060d7 | 3979 | found->bytes_used = bytes_used; |
b742bb82 | 3980 | found->disk_used = bytes_used * factor; |
593060d7 | 3981 | found->bytes_pinned = 0; |
e8569813 | 3982 | found->bytes_reserved = 0; |
e40edf2d | 3983 | found->bytes_readonly = bytes_readonly; |
f0486c68 | 3984 | found->bytes_may_use = 0; |
6af3e3ad | 3985 | found->full = 0; |
4f4db217 | 3986 | found->max_extent_size = 0; |
0e4f8f88 | 3987 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
6d74119f | 3988 | found->chunk_alloc = 0; |
fdb5effd JB |
3989 | found->flush = 0; |
3990 | init_waitqueue_head(&found->wait); | |
633c0aad | 3991 | INIT_LIST_HEAD(&found->ro_bgs); |
957780eb JB |
3992 | INIT_LIST_HEAD(&found->tickets); |
3993 | INIT_LIST_HEAD(&found->priority_tickets); | |
6ab0a202 JM |
3994 | |
3995 | ret = kobject_init_and_add(&found->kobj, &space_info_ktype, | |
3996 | info->space_info_kobj, "%s", | |
3997 | alloc_name(found->flags)); | |
3998 | if (ret) { | |
3999 | kfree(found); | |
4000 | return ret; | |
4001 | } | |
4002 | ||
593060d7 | 4003 | *space_info = found; |
4184ea7f | 4004 | list_add_rcu(&found->list, &info->space_info); |
b4d7c3c9 LZ |
4005 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
4006 | info->data_sinfo = found; | |
6ab0a202 JM |
4007 | |
4008 | return ret; | |
593060d7 CM |
4009 | } |
4010 | ||
8790d502 CM |
4011 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
4012 | { | |
899c81ea ID |
4013 | u64 extra_flags = chunk_to_extended(flags) & |
4014 | BTRFS_EXTENDED_PROFILE_MASK; | |
a46d11a8 | 4015 | |
de98ced9 | 4016 | write_seqlock(&fs_info->profiles_lock); |
a46d11a8 ID |
4017 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
4018 | fs_info->avail_data_alloc_bits |= extra_flags; | |
4019 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
4020 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
4021 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
4022 | fs_info->avail_system_alloc_bits |= extra_flags; | |
de98ced9 | 4023 | write_sequnlock(&fs_info->profiles_lock); |
8790d502 | 4024 | } |
593060d7 | 4025 | |
fc67c450 ID |
4026 | /* |
4027 | * returns target flags in extended format or 0 if restripe for this | |
4028 | * chunk_type is not in progress | |
c6664b42 ID |
4029 | * |
4030 | * should be called with either volume_mutex or balance_lock held | |
fc67c450 ID |
4031 | */ |
4032 | static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags) | |
4033 | { | |
4034 | struct btrfs_balance_control *bctl = fs_info->balance_ctl; | |
4035 | u64 target = 0; | |
4036 | ||
fc67c450 ID |
4037 | if (!bctl) |
4038 | return 0; | |
4039 | ||
4040 | if (flags & BTRFS_BLOCK_GROUP_DATA && | |
4041 | bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
4042 | target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target; | |
4043 | } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM && | |
4044 | bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
4045 | target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target; | |
4046 | } else if (flags & BTRFS_BLOCK_GROUP_METADATA && | |
4047 | bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) { | |
4048 | target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target; | |
4049 | } | |
4050 | ||
4051 | return target; | |
4052 | } | |
4053 | ||
a46d11a8 ID |
4054 | /* |
4055 | * @flags: available profiles in extended format (see ctree.h) | |
4056 | * | |
e4d8ec0f ID |
4057 | * Returns reduced profile in chunk format. If profile changing is in |
4058 | * progress (either running or paused) picks the target profile (if it's | |
4059 | * already available), otherwise falls back to plain reducing. | |
a46d11a8 | 4060 | */ |
48a3b636 | 4061 | static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
ec44a35c | 4062 | { |
0b246afa JM |
4063 | struct btrfs_fs_info *fs_info = root->fs_info; |
4064 | u64 num_devices = fs_info->fs_devices->rw_devices; | |
fc67c450 | 4065 | u64 target; |
9c170b26 ZL |
4066 | u64 raid_type; |
4067 | u64 allowed = 0; | |
a061fc8d | 4068 | |
fc67c450 ID |
4069 | /* |
4070 | * see if restripe for this chunk_type is in progress, if so | |
4071 | * try to reduce to the target profile | |
4072 | */ | |
0b246afa JM |
4073 | spin_lock(&fs_info->balance_lock); |
4074 | target = get_restripe_target(fs_info, flags); | |
fc67c450 ID |
4075 | if (target) { |
4076 | /* pick target profile only if it's already available */ | |
4077 | if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) { | |
0b246afa | 4078 | spin_unlock(&fs_info->balance_lock); |
fc67c450 | 4079 | return extended_to_chunk(target); |
e4d8ec0f ID |
4080 | } |
4081 | } | |
0b246afa | 4082 | spin_unlock(&fs_info->balance_lock); |
e4d8ec0f | 4083 | |
53b381b3 | 4084 | /* First, mask out the RAID levels which aren't possible */ |
9c170b26 ZL |
4085 | for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) { |
4086 | if (num_devices >= btrfs_raid_array[raid_type].devs_min) | |
4087 | allowed |= btrfs_raid_group[raid_type]; | |
4088 | } | |
4089 | allowed &= flags; | |
4090 | ||
4091 | if (allowed & BTRFS_BLOCK_GROUP_RAID6) | |
4092 | allowed = BTRFS_BLOCK_GROUP_RAID6; | |
4093 | else if (allowed & BTRFS_BLOCK_GROUP_RAID5) | |
4094 | allowed = BTRFS_BLOCK_GROUP_RAID5; | |
4095 | else if (allowed & BTRFS_BLOCK_GROUP_RAID10) | |
4096 | allowed = BTRFS_BLOCK_GROUP_RAID10; | |
4097 | else if (allowed & BTRFS_BLOCK_GROUP_RAID1) | |
4098 | allowed = BTRFS_BLOCK_GROUP_RAID1; | |
4099 | else if (allowed & BTRFS_BLOCK_GROUP_RAID0) | |
4100 | allowed = BTRFS_BLOCK_GROUP_RAID0; | |
4101 | ||
4102 | flags &= ~BTRFS_BLOCK_GROUP_PROFILE_MASK; | |
4103 | ||
4104 | return extended_to_chunk(flags | allowed); | |
ec44a35c CM |
4105 | } |
4106 | ||
f8213bdc | 4107 | static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags) |
6a63209f | 4108 | { |
0b246afa | 4109 | struct btrfs_fs_info *fs_info = root->fs_info; |
de98ced9 | 4110 | unsigned seq; |
f8213bdc | 4111 | u64 flags; |
de98ced9 MX |
4112 | |
4113 | do { | |
f8213bdc | 4114 | flags = orig_flags; |
0b246afa | 4115 | seq = read_seqbegin(&fs_info->profiles_lock); |
de98ced9 MX |
4116 | |
4117 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
0b246afa | 4118 | flags |= fs_info->avail_data_alloc_bits; |
de98ced9 | 4119 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
0b246afa | 4120 | flags |= fs_info->avail_system_alloc_bits; |
de98ced9 | 4121 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) |
0b246afa JM |
4122 | flags |= fs_info->avail_metadata_alloc_bits; |
4123 | } while (read_seqretry(&fs_info->profiles_lock, seq)); | |
6fef8df1 | 4124 | |
b742bb82 | 4125 | return btrfs_reduce_alloc_profile(root, flags); |
6a63209f JB |
4126 | } |
4127 | ||
6d07bcec | 4128 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
9ed74f2d | 4129 | { |
0b246afa | 4130 | struct btrfs_fs_info *fs_info = root->fs_info; |
b742bb82 | 4131 | u64 flags; |
53b381b3 | 4132 | u64 ret; |
9ed74f2d | 4133 | |
b742bb82 YZ |
4134 | if (data) |
4135 | flags = BTRFS_BLOCK_GROUP_DATA; | |
0b246afa | 4136 | else if (root == fs_info->chunk_root) |
b742bb82 | 4137 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
9ed74f2d | 4138 | else |
b742bb82 | 4139 | flags = BTRFS_BLOCK_GROUP_METADATA; |
9ed74f2d | 4140 | |
53b381b3 DW |
4141 | ret = get_alloc_profile(root, flags); |
4142 | return ret; | |
6a63209f | 4143 | } |
9ed74f2d | 4144 | |
4ceff079 | 4145 | int btrfs_alloc_data_chunk_ondemand(struct inode *inode, u64 bytes) |
6a63209f | 4146 | { |
6a63209f | 4147 | struct btrfs_space_info *data_sinfo; |
0ca1f7ce | 4148 | struct btrfs_root *root = BTRFS_I(inode)->root; |
b4d7c3c9 | 4149 | struct btrfs_fs_info *fs_info = root->fs_info; |
ab6e2410 | 4150 | u64 used; |
94b947b2 | 4151 | int ret = 0; |
c99f1b0c ZL |
4152 | int need_commit = 2; |
4153 | int have_pinned_space; | |
6a63209f | 4154 | |
6a63209f | 4155 | /* make sure bytes are sectorsize aligned */ |
0b246afa | 4156 | bytes = ALIGN(bytes, fs_info->sectorsize); |
6a63209f | 4157 | |
9dced186 | 4158 | if (btrfs_is_free_space_inode(inode)) { |
c99f1b0c | 4159 | need_commit = 0; |
9dced186 | 4160 | ASSERT(current->journal_info); |
0af3d00b JB |
4161 | } |
4162 | ||
b4d7c3c9 | 4163 | data_sinfo = fs_info->data_sinfo; |
33b4d47f CM |
4164 | if (!data_sinfo) |
4165 | goto alloc; | |
9ed74f2d | 4166 | |
6a63209f JB |
4167 | again: |
4168 | /* make sure we have enough space to handle the data first */ | |
4169 | spin_lock(&data_sinfo->lock); | |
8929ecfa YZ |
4170 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
4171 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + | |
4172 | data_sinfo->bytes_may_use; | |
ab6e2410 JB |
4173 | |
4174 | if (used + bytes > data_sinfo->total_bytes) { | |
4e06bdd6 | 4175 | struct btrfs_trans_handle *trans; |
9ed74f2d | 4176 | |
6a63209f JB |
4177 | /* |
4178 | * if we don't have enough free bytes in this space then we need | |
4179 | * to alloc a new chunk. | |
4180 | */ | |
b9fd47cd | 4181 | if (!data_sinfo->full) { |
6a63209f | 4182 | u64 alloc_target; |
9ed74f2d | 4183 | |
0e4f8f88 | 4184 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
6a63209f | 4185 | spin_unlock(&data_sinfo->lock); |
33b4d47f | 4186 | alloc: |
6a63209f | 4187 | alloc_target = btrfs_get_alloc_profile(root, 1); |
9dced186 MX |
4188 | /* |
4189 | * It is ugly that we don't call nolock join | |
4190 | * transaction for the free space inode case here. | |
4191 | * But it is safe because we only do the data space | |
4192 | * reservation for the free space cache in the | |
4193 | * transaction context, the common join transaction | |
4194 | * just increase the counter of the current transaction | |
4195 | * handler, doesn't try to acquire the trans_lock of | |
4196 | * the fs. | |
4197 | */ | |
7a7eaa40 | 4198 | trans = btrfs_join_transaction(root); |
a22285a6 YZ |
4199 | if (IS_ERR(trans)) |
4200 | return PTR_ERR(trans); | |
9ed74f2d | 4201 | |
0b246afa | 4202 | ret = do_chunk_alloc(trans, fs_info->extent_root, |
0e4f8f88 CM |
4203 | alloc_target, |
4204 | CHUNK_ALLOC_NO_FORCE); | |
6a63209f | 4205 | btrfs_end_transaction(trans, root); |
d52a5b5f MX |
4206 | if (ret < 0) { |
4207 | if (ret != -ENOSPC) | |
4208 | return ret; | |
c99f1b0c ZL |
4209 | else { |
4210 | have_pinned_space = 1; | |
d52a5b5f | 4211 | goto commit_trans; |
c99f1b0c | 4212 | } |
d52a5b5f | 4213 | } |
9ed74f2d | 4214 | |
b4d7c3c9 LZ |
4215 | if (!data_sinfo) |
4216 | data_sinfo = fs_info->data_sinfo; | |
4217 | ||
6a63209f JB |
4218 | goto again; |
4219 | } | |
f2bb8f5c JB |
4220 | |
4221 | /* | |
b150a4f1 | 4222 | * If we don't have enough pinned space to deal with this |
94b947b2 ZL |
4223 | * allocation, and no removed chunk in current transaction, |
4224 | * don't bother committing the transaction. | |
f2bb8f5c | 4225 | */ |
c99f1b0c ZL |
4226 | have_pinned_space = percpu_counter_compare( |
4227 | &data_sinfo->total_bytes_pinned, | |
4228 | used + bytes - data_sinfo->total_bytes); | |
6a63209f | 4229 | spin_unlock(&data_sinfo->lock); |
6a63209f | 4230 | |
4e06bdd6 | 4231 | /* commit the current transaction and try again */ |
d52a5b5f | 4232 | commit_trans: |
c99f1b0c | 4233 | if (need_commit && |
0b246afa | 4234 | !atomic_read(&fs_info->open_ioctl_trans)) { |
c99f1b0c | 4235 | need_commit--; |
b150a4f1 | 4236 | |
e1746e83 ZL |
4237 | if (need_commit > 0) { |
4238 | btrfs_start_delalloc_roots(fs_info, 0, -1); | |
0b246afa JM |
4239 | btrfs_wait_ordered_roots(fs_info, -1, 0, |
4240 | (u64)-1); | |
e1746e83 | 4241 | } |
9a4e7276 | 4242 | |
7a7eaa40 | 4243 | trans = btrfs_join_transaction(root); |
a22285a6 YZ |
4244 | if (IS_ERR(trans)) |
4245 | return PTR_ERR(trans); | |
c99f1b0c | 4246 | if (have_pinned_space >= 0 || |
3204d33c JB |
4247 | test_bit(BTRFS_TRANS_HAVE_FREE_BGS, |
4248 | &trans->transaction->flags) || | |
c99f1b0c | 4249 | need_commit > 0) { |
94b947b2 ZL |
4250 | ret = btrfs_commit_transaction(trans, root); |
4251 | if (ret) | |
4252 | return ret; | |
d7c15171 | 4253 | /* |
c2d6cb16 FM |
4254 | * The cleaner kthread might still be doing iput |
4255 | * operations. Wait for it to finish so that | |
4256 | * more space is released. | |
d7c15171 | 4257 | */ |
0b246afa JM |
4258 | mutex_lock(&fs_info->cleaner_delayed_iput_mutex); |
4259 | mutex_unlock(&fs_info->cleaner_delayed_iput_mutex); | |
94b947b2 ZL |
4260 | goto again; |
4261 | } else { | |
4262 | btrfs_end_transaction(trans, root); | |
4263 | } | |
4e06bdd6 | 4264 | } |
9ed74f2d | 4265 | |
0b246afa | 4266 | trace_btrfs_space_reservation(fs_info, |
cab45e22 JM |
4267 | "space_info:enospc", |
4268 | data_sinfo->flags, bytes, 1); | |
6a63209f JB |
4269 | return -ENOSPC; |
4270 | } | |
4271 | data_sinfo->bytes_may_use += bytes; | |
0b246afa | 4272 | trace_btrfs_space_reservation(fs_info, "space_info", |
2bcc0328 | 4273 | data_sinfo->flags, bytes, 1); |
6a63209f | 4274 | spin_unlock(&data_sinfo->lock); |
6a63209f | 4275 | |
237c0e9f | 4276 | return ret; |
9ed74f2d | 4277 | } |
6a63209f | 4278 | |
4ceff079 QW |
4279 | /* |
4280 | * New check_data_free_space() with ability for precious data reservation | |
4281 | * Will replace old btrfs_check_data_free_space(), but for patch split, | |
4282 | * add a new function first and then replace it. | |
4283 | */ | |
7cf5b976 | 4284 | int btrfs_check_data_free_space(struct inode *inode, u64 start, u64 len) |
4ceff079 | 4285 | { |
0b246afa | 4286 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4ceff079 QW |
4287 | int ret; |
4288 | ||
4289 | /* align the range */ | |
0b246afa JM |
4290 | len = round_up(start + len, fs_info->sectorsize) - |
4291 | round_down(start, fs_info->sectorsize); | |
4292 | start = round_down(start, fs_info->sectorsize); | |
4ceff079 QW |
4293 | |
4294 | ret = btrfs_alloc_data_chunk_ondemand(inode, len); | |
4295 | if (ret < 0) | |
4296 | return ret; | |
4297 | ||
1e5ec2e7 | 4298 | /* Use new btrfs_qgroup_reserve_data to reserve precious data space. */ |
4ceff079 | 4299 | ret = btrfs_qgroup_reserve_data(inode, start, len); |
1e5ec2e7 JB |
4300 | if (ret) |
4301 | btrfs_free_reserved_data_space_noquota(inode, start, len); | |
4ceff079 QW |
4302 | return ret; |
4303 | } | |
4304 | ||
4ceff079 QW |
4305 | /* |
4306 | * Called if we need to clear a data reservation for this inode | |
4307 | * Normally in a error case. | |
4308 | * | |
51773bec QW |
4309 | * This one will *NOT* use accurate qgroup reserved space API, just for case |
4310 | * which we can't sleep and is sure it won't affect qgroup reserved space. | |
4311 | * Like clear_bit_hook(). | |
4ceff079 | 4312 | */ |
51773bec QW |
4313 | void btrfs_free_reserved_data_space_noquota(struct inode *inode, u64 start, |
4314 | u64 len) | |
4ceff079 | 4315 | { |
0b246afa | 4316 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
4ceff079 QW |
4317 | struct btrfs_space_info *data_sinfo; |
4318 | ||
4319 | /* Make sure the range is aligned to sectorsize */ | |
0b246afa JM |
4320 | len = round_up(start + len, fs_info->sectorsize) - |
4321 | round_down(start, fs_info->sectorsize); | |
4322 | start = round_down(start, fs_info->sectorsize); | |
4ceff079 | 4323 | |
0b246afa | 4324 | data_sinfo = fs_info->data_sinfo; |
4ceff079 QW |
4325 | spin_lock(&data_sinfo->lock); |
4326 | if (WARN_ON(data_sinfo->bytes_may_use < len)) | |
4327 | data_sinfo->bytes_may_use = 0; | |
4328 | else | |
4329 | data_sinfo->bytes_may_use -= len; | |
0b246afa | 4330 | trace_btrfs_space_reservation(fs_info, "space_info", |
4ceff079 QW |
4331 | data_sinfo->flags, len, 0); |
4332 | spin_unlock(&data_sinfo->lock); | |
4333 | } | |
4334 | ||
51773bec QW |
4335 | /* |
4336 | * Called if we need to clear a data reservation for this inode | |
4337 | * Normally in a error case. | |
4338 | * | |
01327610 | 4339 | * This one will handle the per-inode data rsv map for accurate reserved |
51773bec QW |
4340 | * space framework. |
4341 | */ | |
4342 | void btrfs_free_reserved_data_space(struct inode *inode, u64 start, u64 len) | |
4343 | { | |
0c476a5d JM |
4344 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4345 | ||
4346 | /* Make sure the range is aligned to sectorsize */ | |
da17066c JM |
4347 | len = round_up(start + len, root->fs_info->sectorsize) - |
4348 | round_down(start, root->fs_info->sectorsize); | |
4349 | start = round_down(start, root->fs_info->sectorsize); | |
0c476a5d | 4350 | |
51773bec QW |
4351 | btrfs_free_reserved_data_space_noquota(inode, start, len); |
4352 | btrfs_qgroup_free_data(inode, start, len); | |
4353 | } | |
4354 | ||
97e728d4 | 4355 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
e3ccfa98 | 4356 | { |
97e728d4 JB |
4357 | struct list_head *head = &info->space_info; |
4358 | struct btrfs_space_info *found; | |
e3ccfa98 | 4359 | |
97e728d4 JB |
4360 | rcu_read_lock(); |
4361 | list_for_each_entry_rcu(found, head, list) { | |
4362 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) | |
0e4f8f88 | 4363 | found->force_alloc = CHUNK_ALLOC_FORCE; |
e3ccfa98 | 4364 | } |
97e728d4 | 4365 | rcu_read_unlock(); |
e3ccfa98 JB |
4366 | } |
4367 | ||
3c76cd84 MX |
4368 | static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global) |
4369 | { | |
4370 | return (global->size << 1); | |
4371 | } | |
4372 | ||
e5bc2458 | 4373 | static int should_alloc_chunk(struct btrfs_root *root, |
698d0082 | 4374 | struct btrfs_space_info *sinfo, int force) |
32c00aff | 4375 | { |
0b246afa JM |
4376 | struct btrfs_fs_info *fs_info = root->fs_info; |
4377 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
424499db | 4378 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
0e4f8f88 | 4379 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
e5bc2458 | 4380 | u64 thresh; |
e3ccfa98 | 4381 | |
0e4f8f88 CM |
4382 | if (force == CHUNK_ALLOC_FORCE) |
4383 | return 1; | |
4384 | ||
fb25e914 JB |
4385 | /* |
4386 | * We need to take into account the global rsv because for all intents | |
4387 | * and purposes it's used space. Don't worry about locking the | |
4388 | * global_rsv, it doesn't change except when the transaction commits. | |
4389 | */ | |
54338b5c | 4390 | if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA) |
3c76cd84 | 4391 | num_allocated += calc_global_rsv_need_space(global_rsv); |
fb25e914 | 4392 | |
0e4f8f88 CM |
4393 | /* |
4394 | * in limited mode, we want to have some free space up to | |
4395 | * about 1% of the FS size. | |
4396 | */ | |
4397 | if (force == CHUNK_ALLOC_LIMITED) { | |
0b246afa | 4398 | thresh = btrfs_super_total_bytes(fs_info->super_copy); |
ee22184b | 4399 | thresh = max_t(u64, SZ_64M, div_factor_fine(thresh, 1)); |
0e4f8f88 CM |
4400 | |
4401 | if (num_bytes - num_allocated < thresh) | |
4402 | return 1; | |
4403 | } | |
0e4f8f88 | 4404 | |
ee22184b | 4405 | if (num_allocated + SZ_2M < div_factor(num_bytes, 8)) |
14ed0ca6 | 4406 | return 0; |
424499db | 4407 | return 1; |
32c00aff JB |
4408 | } |
4409 | ||
39c2d7fa | 4410 | static u64 get_profile_num_devs(struct btrfs_root *root, u64 type) |
15d1ff81 | 4411 | { |
0b246afa | 4412 | struct btrfs_fs_info *fs_info = root->fs_info; |
15d1ff81 LB |
4413 | u64 num_dev; |
4414 | ||
53b381b3 DW |
4415 | if (type & (BTRFS_BLOCK_GROUP_RAID10 | |
4416 | BTRFS_BLOCK_GROUP_RAID0 | | |
4417 | BTRFS_BLOCK_GROUP_RAID5 | | |
4418 | BTRFS_BLOCK_GROUP_RAID6)) | |
0b246afa | 4419 | num_dev = fs_info->fs_devices->rw_devices; |
15d1ff81 LB |
4420 | else if (type & BTRFS_BLOCK_GROUP_RAID1) |
4421 | num_dev = 2; | |
4422 | else | |
4423 | num_dev = 1; /* DUP or single */ | |
4424 | ||
39c2d7fa | 4425 | return num_dev; |
15d1ff81 LB |
4426 | } |
4427 | ||
39c2d7fa FM |
4428 | /* |
4429 | * If @is_allocation is true, reserve space in the system space info necessary | |
4430 | * for allocating a chunk, otherwise if it's false, reserve space necessary for | |
4431 | * removing a chunk. | |
4432 | */ | |
4433 | void check_system_chunk(struct btrfs_trans_handle *trans, | |
4434 | struct btrfs_root *root, | |
4617ea3a | 4435 | u64 type) |
15d1ff81 | 4436 | { |
0b246afa | 4437 | struct btrfs_fs_info *fs_info = root->fs_info; |
15d1ff81 LB |
4438 | struct btrfs_space_info *info; |
4439 | u64 left; | |
4440 | u64 thresh; | |
4fbcdf66 | 4441 | int ret = 0; |
39c2d7fa | 4442 | u64 num_devs; |
4fbcdf66 FM |
4443 | |
4444 | /* | |
4445 | * Needed because we can end up allocating a system chunk and for an | |
4446 | * atomic and race free space reservation in the chunk block reserve. | |
4447 | */ | |
0b246afa | 4448 | ASSERT(mutex_is_locked(&fs_info->chunk_mutex)); |
15d1ff81 | 4449 | |
0b246afa | 4450 | info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
15d1ff81 LB |
4451 | spin_lock(&info->lock); |
4452 | left = info->total_bytes - info->bytes_used - info->bytes_pinned - | |
4fbcdf66 FM |
4453 | info->bytes_reserved - info->bytes_readonly - |
4454 | info->bytes_may_use; | |
15d1ff81 LB |
4455 | spin_unlock(&info->lock); |
4456 | ||
39c2d7fa FM |
4457 | num_devs = get_profile_num_devs(root, type); |
4458 | ||
4459 | /* num_devs device items to update and 1 chunk item to add or remove */ | |
0b246afa JM |
4460 | thresh = btrfs_calc_trunc_metadata_size(fs_info, num_devs) + |
4461 | btrfs_calc_trans_metadata_size(fs_info, 1); | |
39c2d7fa | 4462 | |
0b246afa JM |
4463 | if (left < thresh && btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
4464 | btrfs_info(fs_info, "left=%llu, need=%llu, flags=%llu", | |
4465 | left, thresh, type); | |
4466 | dump_space_info(fs_info, info, 0, 0); | |
15d1ff81 LB |
4467 | } |
4468 | ||
4469 | if (left < thresh) { | |
4470 | u64 flags; | |
4471 | ||
0b246afa | 4472 | flags = btrfs_get_alloc_profile(fs_info->chunk_root, 0); |
4fbcdf66 FM |
4473 | /* |
4474 | * Ignore failure to create system chunk. We might end up not | |
4475 | * needing it, as we might not need to COW all nodes/leafs from | |
4476 | * the paths we visit in the chunk tree (they were already COWed | |
4477 | * or created in the current transaction for example). | |
4478 | */ | |
4479 | ret = btrfs_alloc_chunk(trans, root, flags); | |
4480 | } | |
4481 | ||
4482 | if (!ret) { | |
0b246afa JM |
4483 | ret = btrfs_block_rsv_add(fs_info->chunk_root, |
4484 | &fs_info->chunk_block_rsv, | |
4fbcdf66 FM |
4485 | thresh, BTRFS_RESERVE_NO_FLUSH); |
4486 | if (!ret) | |
4487 | trans->chunk_bytes_reserved += thresh; | |
15d1ff81 LB |
4488 | } |
4489 | } | |
4490 | ||
28b737f6 LB |
4491 | /* |
4492 | * If force is CHUNK_ALLOC_FORCE: | |
4493 | * - return 1 if it successfully allocates a chunk, | |
4494 | * - return errors including -ENOSPC otherwise. | |
4495 | * If force is NOT CHUNK_ALLOC_FORCE: | |
4496 | * - return 0 if it doesn't need to allocate a new chunk, | |
4497 | * - return 1 if it successfully allocates a chunk, | |
4498 | * - return errors including -ENOSPC otherwise. | |
4499 | */ | |
6324fbf3 | 4500 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
698d0082 | 4501 | struct btrfs_root *extent_root, u64 flags, int force) |
9ed74f2d | 4502 | { |
6324fbf3 | 4503 | struct btrfs_space_info *space_info; |
97e728d4 | 4504 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
6d74119f | 4505 | int wait_for_alloc = 0; |
9ed74f2d | 4506 | int ret = 0; |
9ed74f2d | 4507 | |
c6b305a8 JB |
4508 | /* Don't re-enter if we're already allocating a chunk */ |
4509 | if (trans->allocating_chunk) | |
4510 | return -ENOSPC; | |
4511 | ||
0b246afa | 4512 | space_info = __find_space_info(fs_info, flags); |
593060d7 | 4513 | if (!space_info) { |
0b246afa | 4514 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
79787eaa | 4515 | BUG_ON(ret); /* -ENOMEM */ |
9ed74f2d | 4516 | } |
79787eaa | 4517 | BUG_ON(!space_info); /* Logic error */ |
9ed74f2d | 4518 | |
6d74119f | 4519 | again: |
25179201 | 4520 | spin_lock(&space_info->lock); |
9e622d6b | 4521 | if (force < space_info->force_alloc) |
0e4f8f88 | 4522 | force = space_info->force_alloc; |
25179201 | 4523 | if (space_info->full) { |
09fb99a6 FDBM |
4524 | if (should_alloc_chunk(extent_root, space_info, force)) |
4525 | ret = -ENOSPC; | |
4526 | else | |
4527 | ret = 0; | |
25179201 | 4528 | spin_unlock(&space_info->lock); |
09fb99a6 | 4529 | return ret; |
9ed74f2d JB |
4530 | } |
4531 | ||
698d0082 | 4532 | if (!should_alloc_chunk(extent_root, space_info, force)) { |
25179201 | 4533 | spin_unlock(&space_info->lock); |
6d74119f JB |
4534 | return 0; |
4535 | } else if (space_info->chunk_alloc) { | |
4536 | wait_for_alloc = 1; | |
4537 | } else { | |
4538 | space_info->chunk_alloc = 1; | |
9ed74f2d | 4539 | } |
0e4f8f88 | 4540 | |
25179201 | 4541 | spin_unlock(&space_info->lock); |
9ed74f2d | 4542 | |
6d74119f JB |
4543 | mutex_lock(&fs_info->chunk_mutex); |
4544 | ||
4545 | /* | |
4546 | * The chunk_mutex is held throughout the entirety of a chunk | |
4547 | * allocation, so once we've acquired the chunk_mutex we know that the | |
4548 | * other guy is done and we need to recheck and see if we should | |
4549 | * allocate. | |
4550 | */ | |
4551 | if (wait_for_alloc) { | |
4552 | mutex_unlock(&fs_info->chunk_mutex); | |
4553 | wait_for_alloc = 0; | |
4554 | goto again; | |
4555 | } | |
4556 | ||
c6b305a8 JB |
4557 | trans->allocating_chunk = true; |
4558 | ||
67377734 JB |
4559 | /* |
4560 | * If we have mixed data/metadata chunks we want to make sure we keep | |
4561 | * allocating mixed chunks instead of individual chunks. | |
4562 | */ | |
4563 | if (btrfs_mixed_space_info(space_info)) | |
4564 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); | |
4565 | ||
97e728d4 JB |
4566 | /* |
4567 | * if we're doing a data chunk, go ahead and make sure that | |
4568 | * we keep a reasonable number of metadata chunks allocated in the | |
4569 | * FS as well. | |
4570 | */ | |
9ed74f2d | 4571 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
97e728d4 JB |
4572 | fs_info->data_chunk_allocations++; |
4573 | if (!(fs_info->data_chunk_allocations % | |
4574 | fs_info->metadata_ratio)) | |
4575 | force_metadata_allocation(fs_info); | |
9ed74f2d JB |
4576 | } |
4577 | ||
15d1ff81 LB |
4578 | /* |
4579 | * Check if we have enough space in SYSTEM chunk because we may need | |
4580 | * to update devices. | |
4581 | */ | |
4617ea3a | 4582 | check_system_chunk(trans, extent_root, flags); |
15d1ff81 | 4583 | |
2b82032c | 4584 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
c6b305a8 | 4585 | trans->allocating_chunk = false; |
92b8e897 | 4586 | |
9ed74f2d | 4587 | spin_lock(&space_info->lock); |
a81cb9a2 AO |
4588 | if (ret < 0 && ret != -ENOSPC) |
4589 | goto out; | |
9ed74f2d | 4590 | if (ret) |
6324fbf3 | 4591 | space_info->full = 1; |
424499db YZ |
4592 | else |
4593 | ret = 1; | |
6d74119f | 4594 | |
0e4f8f88 | 4595 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
a81cb9a2 | 4596 | out: |
6d74119f | 4597 | space_info->chunk_alloc = 0; |
9ed74f2d | 4598 | spin_unlock(&space_info->lock); |
a25c75d5 | 4599 | mutex_unlock(&fs_info->chunk_mutex); |
00d80e34 FM |
4600 | /* |
4601 | * When we allocate a new chunk we reserve space in the chunk block | |
4602 | * reserve to make sure we can COW nodes/leafs in the chunk tree or | |
4603 | * add new nodes/leafs to it if we end up needing to do it when | |
4604 | * inserting the chunk item and updating device items as part of the | |
4605 | * second phase of chunk allocation, performed by | |
4606 | * btrfs_finish_chunk_alloc(). So make sure we don't accumulate a | |
4607 | * large number of new block groups to create in our transaction | |
4608 | * handle's new_bgs list to avoid exhausting the chunk block reserve | |
4609 | * in extreme cases - like having a single transaction create many new | |
4610 | * block groups when starting to write out the free space caches of all | |
4611 | * the block groups that were made dirty during the lifetime of the | |
4612 | * transaction. | |
4613 | */ | |
d9a0540a | 4614 | if (trans->can_flush_pending_bgs && |
ee22184b | 4615 | trans->chunk_bytes_reserved >= (u64)SZ_2M) { |
64b63580 | 4616 | btrfs_create_pending_block_groups(trans, extent_root); |
00d80e34 FM |
4617 | btrfs_trans_release_chunk_metadata(trans); |
4618 | } | |
0f9dd46c | 4619 | return ret; |
6324fbf3 | 4620 | } |
9ed74f2d | 4621 | |
a80c8dcf JB |
4622 | static int can_overcommit(struct btrfs_root *root, |
4623 | struct btrfs_space_info *space_info, u64 bytes, | |
08e007d2 | 4624 | enum btrfs_reserve_flush_enum flush) |
a80c8dcf | 4625 | { |
0b246afa JM |
4626 | struct btrfs_fs_info *fs_info = root->fs_info; |
4627 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
957780eb | 4628 | u64 profile; |
3c76cd84 | 4629 | u64 space_size; |
a80c8dcf JB |
4630 | u64 avail; |
4631 | u64 used; | |
4632 | ||
957780eb JB |
4633 | /* Don't overcommit when in mixed mode. */ |
4634 | if (space_info->flags & BTRFS_BLOCK_GROUP_DATA) | |
4635 | return 0; | |
4636 | ||
957780eb | 4637 | profile = btrfs_get_alloc_profile(root, 0); |
a80c8dcf | 4638 | used = space_info->bytes_used + space_info->bytes_reserved + |
96f1bb57 JB |
4639 | space_info->bytes_pinned + space_info->bytes_readonly; |
4640 | ||
96f1bb57 JB |
4641 | /* |
4642 | * We only want to allow over committing if we have lots of actual space | |
4643 | * free, but if we don't have enough space to handle the global reserve | |
4644 | * space then we could end up having a real enospc problem when trying | |
4645 | * to allocate a chunk or some other such important allocation. | |
4646 | */ | |
3c76cd84 MX |
4647 | spin_lock(&global_rsv->lock); |
4648 | space_size = calc_global_rsv_need_space(global_rsv); | |
4649 | spin_unlock(&global_rsv->lock); | |
4650 | if (used + space_size >= space_info->total_bytes) | |
96f1bb57 JB |
4651 | return 0; |
4652 | ||
4653 | used += space_info->bytes_may_use; | |
a80c8dcf | 4654 | |
0b246afa JM |
4655 | spin_lock(&fs_info->free_chunk_lock); |
4656 | avail = fs_info->free_chunk_space; | |
4657 | spin_unlock(&fs_info->free_chunk_lock); | |
a80c8dcf JB |
4658 | |
4659 | /* | |
4660 | * If we have dup, raid1 or raid10 then only half of the free | |
53b381b3 DW |
4661 | * space is actually useable. For raid56, the space info used |
4662 | * doesn't include the parity drive, so we don't have to | |
4663 | * change the math | |
a80c8dcf JB |
4664 | */ |
4665 | if (profile & (BTRFS_BLOCK_GROUP_DUP | | |
4666 | BTRFS_BLOCK_GROUP_RAID1 | | |
4667 | BTRFS_BLOCK_GROUP_RAID10)) | |
4668 | avail >>= 1; | |
4669 | ||
4670 | /* | |
561c294d MX |
4671 | * If we aren't flushing all things, let us overcommit up to |
4672 | * 1/2th of the space. If we can flush, don't let us overcommit | |
4673 | * too much, let it overcommit up to 1/8 of the space. | |
a80c8dcf | 4674 | */ |
08e007d2 | 4675 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
14575aef | 4676 | avail >>= 3; |
a80c8dcf | 4677 | else |
14575aef | 4678 | avail >>= 1; |
a80c8dcf | 4679 | |
14575aef | 4680 | if (used + bytes < space_info->total_bytes + avail) |
a80c8dcf JB |
4681 | return 1; |
4682 | return 0; | |
4683 | } | |
4684 | ||
48a3b636 | 4685 | static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root, |
6c255e67 | 4686 | unsigned long nr_pages, int nr_items) |
da633a42 | 4687 | { |
0b246afa JM |
4688 | struct btrfs_fs_info *fs_info = root->fs_info; |
4689 | struct super_block *sb = fs_info->sb; | |
da633a42 | 4690 | |
925a6efb JB |
4691 | if (down_read_trylock(&sb->s_umount)) { |
4692 | writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE); | |
4693 | up_read(&sb->s_umount); | |
4694 | } else { | |
da633a42 MX |
4695 | /* |
4696 | * We needn't worry the filesystem going from r/w to r/o though | |
4697 | * we don't acquire ->s_umount mutex, because the filesystem | |
4698 | * should guarantee the delalloc inodes list be empty after | |
4699 | * the filesystem is readonly(all dirty pages are written to | |
4700 | * the disk). | |
4701 | */ | |
0b246afa | 4702 | btrfs_start_delalloc_roots(fs_info, 0, nr_items); |
98ad69cf | 4703 | if (!current->journal_info) |
0b246afa | 4704 | btrfs_wait_ordered_roots(fs_info, nr_items, 0, (u64)-1); |
da633a42 MX |
4705 | } |
4706 | } | |
4707 | ||
18cd8ea6 MX |
4708 | static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim) |
4709 | { | |
4710 | u64 bytes; | |
4711 | int nr; | |
4712 | ||
27965b6c | 4713 | bytes = btrfs_calc_trans_metadata_size(root->fs_info, 1); |
18cd8ea6 MX |
4714 | nr = (int)div64_u64(to_reclaim, bytes); |
4715 | if (!nr) | |
4716 | nr = 1; | |
4717 | return nr; | |
4718 | } | |
4719 | ||
ee22184b | 4720 | #define EXTENT_SIZE_PER_ITEM SZ_256K |
c61a16a7 | 4721 | |
9ed74f2d | 4722 | /* |
5da9d01b | 4723 | * shrink metadata reservation for delalloc |
9ed74f2d | 4724 | */ |
f4c738c2 JB |
4725 | static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig, |
4726 | bool wait_ordered) | |
5da9d01b | 4727 | { |
0b246afa | 4728 | struct btrfs_fs_info *fs_info = root->fs_info; |
0ca1f7ce | 4729 | struct btrfs_block_rsv *block_rsv; |
0019f10d | 4730 | struct btrfs_space_info *space_info; |
663350ac | 4731 | struct btrfs_trans_handle *trans; |
f4c738c2 | 4732 | u64 delalloc_bytes; |
5da9d01b | 4733 | u64 max_reclaim; |
b1953bce | 4734 | long time_left; |
d3ee29e3 MX |
4735 | unsigned long nr_pages; |
4736 | int loops; | |
b0244199 | 4737 | int items; |
08e007d2 | 4738 | enum btrfs_reserve_flush_enum flush; |
5da9d01b | 4739 | |
c61a16a7 | 4740 | /* Calc the number of the pages we need flush for space reservation */ |
b0244199 | 4741 | items = calc_reclaim_items_nr(root, to_reclaim); |
8eb0dfdb | 4742 | to_reclaim = (u64)items * EXTENT_SIZE_PER_ITEM; |
c61a16a7 | 4743 | |
663350ac | 4744 | trans = (struct btrfs_trans_handle *)current->journal_info; |
0b246afa | 4745 | block_rsv = &fs_info->delalloc_block_rsv; |
0019f10d | 4746 | space_info = block_rsv->space_info; |
bf9022e0 | 4747 | |
963d678b | 4748 | delalloc_bytes = percpu_counter_sum_positive( |
0b246afa | 4749 | &fs_info->delalloc_bytes); |
f4c738c2 | 4750 | if (delalloc_bytes == 0) { |
fdb5effd | 4751 | if (trans) |
f4c738c2 | 4752 | return; |
38c135af | 4753 | if (wait_ordered) |
0b246afa | 4754 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
f4c738c2 | 4755 | return; |
fdb5effd JB |
4756 | } |
4757 | ||
d3ee29e3 | 4758 | loops = 0; |
f4c738c2 JB |
4759 | while (delalloc_bytes && loops < 3) { |
4760 | max_reclaim = min(delalloc_bytes, to_reclaim); | |
09cbfeaf | 4761 | nr_pages = max_reclaim >> PAGE_SHIFT; |
6c255e67 | 4762 | btrfs_writeback_inodes_sb_nr(root, nr_pages, items); |
dea31f52 JB |
4763 | /* |
4764 | * We need to wait for the async pages to actually start before | |
4765 | * we do anything. | |
4766 | */ | |
0b246afa | 4767 | max_reclaim = atomic_read(&fs_info->async_delalloc_pages); |
9f3a074d MX |
4768 | if (!max_reclaim) |
4769 | goto skip_async; | |
4770 | ||
4771 | if (max_reclaim <= nr_pages) | |
4772 | max_reclaim = 0; | |
4773 | else | |
4774 | max_reclaim -= nr_pages; | |
dea31f52 | 4775 | |
0b246afa JM |
4776 | wait_event(fs_info->async_submit_wait, |
4777 | atomic_read(&fs_info->async_delalloc_pages) <= | |
9f3a074d MX |
4778 | (int)max_reclaim); |
4779 | skip_async: | |
08e007d2 MX |
4780 | if (!trans) |
4781 | flush = BTRFS_RESERVE_FLUSH_ALL; | |
4782 | else | |
4783 | flush = BTRFS_RESERVE_NO_FLUSH; | |
0019f10d | 4784 | spin_lock(&space_info->lock); |
08e007d2 | 4785 | if (can_overcommit(root, space_info, orig, flush)) { |
f4c738c2 JB |
4786 | spin_unlock(&space_info->lock); |
4787 | break; | |
4788 | } | |
957780eb JB |
4789 | if (list_empty(&space_info->tickets) && |
4790 | list_empty(&space_info->priority_tickets)) { | |
4791 | spin_unlock(&space_info->lock); | |
4792 | break; | |
4793 | } | |
0019f10d | 4794 | spin_unlock(&space_info->lock); |
5da9d01b | 4795 | |
36e39c40 | 4796 | loops++; |
f104d044 | 4797 | if (wait_ordered && !trans) { |
0b246afa | 4798 | btrfs_wait_ordered_roots(fs_info, items, 0, (u64)-1); |
f104d044 | 4799 | } else { |
f4c738c2 | 4800 | time_left = schedule_timeout_killable(1); |
f104d044 JB |
4801 | if (time_left) |
4802 | break; | |
4803 | } | |
963d678b | 4804 | delalloc_bytes = percpu_counter_sum_positive( |
0b246afa | 4805 | &fs_info->delalloc_bytes); |
5da9d01b | 4806 | } |
5da9d01b YZ |
4807 | } |
4808 | ||
663350ac JB |
4809 | /** |
4810 | * maybe_commit_transaction - possibly commit the transaction if its ok to | |
4811 | * @root - the root we're allocating for | |
4812 | * @bytes - the number of bytes we want to reserve | |
4813 | * @force - force the commit | |
8bb8ab2e | 4814 | * |
663350ac JB |
4815 | * This will check to make sure that committing the transaction will actually |
4816 | * get us somewhere and then commit the transaction if it does. Otherwise it | |
4817 | * will return -ENOSPC. | |
8bb8ab2e | 4818 | */ |
663350ac JB |
4819 | static int may_commit_transaction(struct btrfs_root *root, |
4820 | struct btrfs_space_info *space_info, | |
4821 | u64 bytes, int force) | |
4822 | { | |
0b246afa JM |
4823 | struct btrfs_fs_info *fs_info = root->fs_info; |
4824 | struct btrfs_block_rsv *delayed_rsv = &fs_info->delayed_block_rsv; | |
663350ac JB |
4825 | struct btrfs_trans_handle *trans; |
4826 | ||
4827 | trans = (struct btrfs_trans_handle *)current->journal_info; | |
4828 | if (trans) | |
4829 | return -EAGAIN; | |
4830 | ||
4831 | if (force) | |
4832 | goto commit; | |
4833 | ||
4834 | /* See if there is enough pinned space to make this reservation */ | |
b150a4f1 | 4835 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
0424c548 | 4836 | bytes) >= 0) |
663350ac | 4837 | goto commit; |
663350ac JB |
4838 | |
4839 | /* | |
4840 | * See if there is some space in the delayed insertion reservation for | |
4841 | * this reservation. | |
4842 | */ | |
4843 | if (space_info != delayed_rsv->space_info) | |
4844 | return -ENOSPC; | |
4845 | ||
4846 | spin_lock(&delayed_rsv->lock); | |
b150a4f1 JB |
4847 | if (percpu_counter_compare(&space_info->total_bytes_pinned, |
4848 | bytes - delayed_rsv->size) >= 0) { | |
663350ac JB |
4849 | spin_unlock(&delayed_rsv->lock); |
4850 | return -ENOSPC; | |
4851 | } | |
4852 | spin_unlock(&delayed_rsv->lock); | |
4853 | ||
4854 | commit: | |
4855 | trans = btrfs_join_transaction(root); | |
4856 | if (IS_ERR(trans)) | |
4857 | return -ENOSPC; | |
4858 | ||
4859 | return btrfs_commit_transaction(trans, root); | |
4860 | } | |
4861 | ||
957780eb JB |
4862 | struct reserve_ticket { |
4863 | u64 bytes; | |
4864 | int error; | |
4865 | struct list_head list; | |
4866 | wait_queue_head_t wait; | |
96c3f433 JB |
4867 | }; |
4868 | ||
4869 | static int flush_space(struct btrfs_root *root, | |
4870 | struct btrfs_space_info *space_info, u64 num_bytes, | |
4871 | u64 orig_bytes, int state) | |
4872 | { | |
0b246afa | 4873 | struct btrfs_fs_info *fs_info = root->fs_info; |
96c3f433 JB |
4874 | struct btrfs_trans_handle *trans; |
4875 | int nr; | |
f4c738c2 | 4876 | int ret = 0; |
96c3f433 JB |
4877 | |
4878 | switch (state) { | |
96c3f433 JB |
4879 | case FLUSH_DELAYED_ITEMS_NR: |
4880 | case FLUSH_DELAYED_ITEMS: | |
18cd8ea6 MX |
4881 | if (state == FLUSH_DELAYED_ITEMS_NR) |
4882 | nr = calc_reclaim_items_nr(root, num_bytes) * 2; | |
4883 | else | |
96c3f433 | 4884 | nr = -1; |
18cd8ea6 | 4885 | |
96c3f433 JB |
4886 | trans = btrfs_join_transaction(root); |
4887 | if (IS_ERR(trans)) { | |
4888 | ret = PTR_ERR(trans); | |
4889 | break; | |
4890 | } | |
4891 | ret = btrfs_run_delayed_items_nr(trans, root, nr); | |
4892 | btrfs_end_transaction(trans, root); | |
4893 | break; | |
67b0fd63 JB |
4894 | case FLUSH_DELALLOC: |
4895 | case FLUSH_DELALLOC_WAIT: | |
24af7dd1 | 4896 | shrink_delalloc(root, num_bytes * 2, orig_bytes, |
67b0fd63 JB |
4897 | state == FLUSH_DELALLOC_WAIT); |
4898 | break; | |
ea658bad JB |
4899 | case ALLOC_CHUNK: |
4900 | trans = btrfs_join_transaction(root); | |
4901 | if (IS_ERR(trans)) { | |
4902 | ret = PTR_ERR(trans); | |
4903 | break; | |
4904 | } | |
0b246afa | 4905 | ret = do_chunk_alloc(trans, fs_info->extent_root, |
ea658bad JB |
4906 | btrfs_get_alloc_profile(root, 0), |
4907 | CHUNK_ALLOC_NO_FORCE); | |
4908 | btrfs_end_transaction(trans, root); | |
eecba891 | 4909 | if (ret > 0 || ret == -ENOSPC) |
ea658bad JB |
4910 | ret = 0; |
4911 | break; | |
96c3f433 JB |
4912 | case COMMIT_TRANS: |
4913 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); | |
4914 | break; | |
4915 | default: | |
4916 | ret = -ENOSPC; | |
4917 | break; | |
4918 | } | |
4919 | ||
0b246afa | 4920 | trace_btrfs_flush_space(fs_info, space_info->flags, num_bytes, |
f376df2b | 4921 | orig_bytes, state, ret); |
96c3f433 JB |
4922 | return ret; |
4923 | } | |
21c7e756 MX |
4924 | |
4925 | static inline u64 | |
4926 | btrfs_calc_reclaim_metadata_size(struct btrfs_root *root, | |
4927 | struct btrfs_space_info *space_info) | |
4928 | { | |
957780eb | 4929 | struct reserve_ticket *ticket; |
21c7e756 MX |
4930 | u64 used; |
4931 | u64 expected; | |
957780eb | 4932 | u64 to_reclaim = 0; |
21c7e756 | 4933 | |
957780eb JB |
4934 | list_for_each_entry(ticket, &space_info->tickets, list) |
4935 | to_reclaim += ticket->bytes; | |
4936 | list_for_each_entry(ticket, &space_info->priority_tickets, list) | |
4937 | to_reclaim += ticket->bytes; | |
4938 | if (to_reclaim) | |
4939 | return to_reclaim; | |
21c7e756 | 4940 | |
e0af2484 WX |
4941 | to_reclaim = min_t(u64, num_online_cpus() * SZ_1M, SZ_16M); |
4942 | if (can_overcommit(root, space_info, to_reclaim, | |
4943 | BTRFS_RESERVE_FLUSH_ALL)) | |
4944 | return 0; | |
4945 | ||
21c7e756 MX |
4946 | used = space_info->bytes_used + space_info->bytes_reserved + |
4947 | space_info->bytes_pinned + space_info->bytes_readonly + | |
4948 | space_info->bytes_may_use; | |
ee22184b | 4949 | if (can_overcommit(root, space_info, SZ_1M, BTRFS_RESERVE_FLUSH_ALL)) |
21c7e756 MX |
4950 | expected = div_factor_fine(space_info->total_bytes, 95); |
4951 | else | |
4952 | expected = div_factor_fine(space_info->total_bytes, 90); | |
4953 | ||
4954 | if (used > expected) | |
4955 | to_reclaim = used - expected; | |
4956 | else | |
4957 | to_reclaim = 0; | |
4958 | to_reclaim = min(to_reclaim, space_info->bytes_may_use + | |
4959 | space_info->bytes_reserved); | |
21c7e756 MX |
4960 | return to_reclaim; |
4961 | } | |
4962 | ||
4963 | static inline int need_do_async_reclaim(struct btrfs_space_info *space_info, | |
87241c2e | 4964 | struct btrfs_root *root, u64 used) |
21c7e756 | 4965 | { |
0b246afa | 4966 | struct btrfs_fs_info *fs_info = root->fs_info; |
365c5313 JB |
4967 | u64 thresh = div_factor_fine(space_info->total_bytes, 98); |
4968 | ||
4969 | /* If we're just plain full then async reclaim just slows us down. */ | |
baee8790 | 4970 | if ((space_info->bytes_used + space_info->bytes_reserved) >= thresh) |
365c5313 JB |
4971 | return 0; |
4972 | ||
87241c2e | 4973 | if (!btrfs_calc_reclaim_metadata_size(root, space_info)) |
d38b349c JB |
4974 | return 0; |
4975 | ||
0b246afa JM |
4976 | return (used >= thresh && !btrfs_fs_closing(fs_info) && |
4977 | !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state)); | |
21c7e756 MX |
4978 | } |
4979 | ||
957780eb | 4980 | static void wake_all_tickets(struct list_head *head) |
21c7e756 | 4981 | { |
957780eb | 4982 | struct reserve_ticket *ticket; |
25ce459c | 4983 | |
957780eb JB |
4984 | while (!list_empty(head)) { |
4985 | ticket = list_first_entry(head, struct reserve_ticket, list); | |
4986 | list_del_init(&ticket->list); | |
4987 | ticket->error = -ENOSPC; | |
4988 | wake_up(&ticket->wait); | |
21c7e756 | 4989 | } |
21c7e756 MX |
4990 | } |
4991 | ||
957780eb JB |
4992 | /* |
4993 | * This is for normal flushers, we can wait all goddamned day if we want to. We | |
4994 | * will loop and continuously try to flush as long as we are making progress. | |
4995 | * We count progress as clearing off tickets each time we have to loop. | |
4996 | */ | |
21c7e756 MX |
4997 | static void btrfs_async_reclaim_metadata_space(struct work_struct *work) |
4998 | { | |
4999 | struct btrfs_fs_info *fs_info; | |
5000 | struct btrfs_space_info *space_info; | |
5001 | u64 to_reclaim; | |
5002 | int flush_state; | |
957780eb | 5003 | int commit_cycles = 0; |
ce129655 | 5004 | u64 last_tickets_id; |
21c7e756 MX |
5005 | |
5006 | fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work); | |
5007 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); | |
5008 | ||
957780eb | 5009 | spin_lock(&space_info->lock); |
21c7e756 MX |
5010 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, |
5011 | space_info); | |
957780eb JB |
5012 | if (!to_reclaim) { |
5013 | space_info->flush = 0; | |
5014 | spin_unlock(&space_info->lock); | |
21c7e756 | 5015 | return; |
957780eb | 5016 | } |
ce129655 | 5017 | last_tickets_id = space_info->tickets_id; |
957780eb | 5018 | spin_unlock(&space_info->lock); |
21c7e756 MX |
5019 | |
5020 | flush_state = FLUSH_DELAYED_ITEMS_NR; | |
957780eb JB |
5021 | do { |
5022 | struct reserve_ticket *ticket; | |
5023 | int ret; | |
5024 | ||
5025 | ret = flush_space(fs_info->fs_root, space_info, to_reclaim, | |
5026 | to_reclaim, flush_state); | |
5027 | spin_lock(&space_info->lock); | |
5028 | if (list_empty(&space_info->tickets)) { | |
5029 | space_info->flush = 0; | |
5030 | spin_unlock(&space_info->lock); | |
5031 | return; | |
5032 | } | |
5033 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, | |
5034 | space_info); | |
5035 | ticket = list_first_entry(&space_info->tickets, | |
5036 | struct reserve_ticket, list); | |
ce129655 | 5037 | if (last_tickets_id == space_info->tickets_id) { |
957780eb JB |
5038 | flush_state++; |
5039 | } else { | |
ce129655 | 5040 | last_tickets_id = space_info->tickets_id; |
957780eb JB |
5041 | flush_state = FLUSH_DELAYED_ITEMS_NR; |
5042 | if (commit_cycles) | |
5043 | commit_cycles--; | |
5044 | } | |
5045 | ||
5046 | if (flush_state > COMMIT_TRANS) { | |
5047 | commit_cycles++; | |
5048 | if (commit_cycles > 2) { | |
5049 | wake_all_tickets(&space_info->tickets); | |
5050 | space_info->flush = 0; | |
5051 | } else { | |
5052 | flush_state = FLUSH_DELAYED_ITEMS_NR; | |
5053 | } | |
5054 | } | |
5055 | spin_unlock(&space_info->lock); | |
5056 | } while (flush_state <= COMMIT_TRANS); | |
5057 | } | |
5058 | ||
5059 | void btrfs_init_async_reclaim_work(struct work_struct *work) | |
5060 | { | |
5061 | INIT_WORK(work, btrfs_async_reclaim_metadata_space); | |
5062 | } | |
5063 | ||
5064 | static void priority_reclaim_metadata_space(struct btrfs_fs_info *fs_info, | |
5065 | struct btrfs_space_info *space_info, | |
5066 | struct reserve_ticket *ticket) | |
5067 | { | |
5068 | u64 to_reclaim; | |
5069 | int flush_state = FLUSH_DELAYED_ITEMS_NR; | |
5070 | ||
5071 | spin_lock(&space_info->lock); | |
5072 | to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root, | |
5073 | space_info); | |
5074 | if (!to_reclaim) { | |
5075 | spin_unlock(&space_info->lock); | |
5076 | return; | |
5077 | } | |
5078 | spin_unlock(&space_info->lock); | |
5079 | ||
21c7e756 MX |
5080 | do { |
5081 | flush_space(fs_info->fs_root, space_info, to_reclaim, | |
5082 | to_reclaim, flush_state); | |
5083 | flush_state++; | |
957780eb JB |
5084 | spin_lock(&space_info->lock); |
5085 | if (ticket->bytes == 0) { | |
5086 | spin_unlock(&space_info->lock); | |
21c7e756 | 5087 | return; |
957780eb JB |
5088 | } |
5089 | spin_unlock(&space_info->lock); | |
5090 | ||
5091 | /* | |
5092 | * Priority flushers can't wait on delalloc without | |
5093 | * deadlocking. | |
5094 | */ | |
5095 | if (flush_state == FLUSH_DELALLOC || | |
5096 | flush_state == FLUSH_DELALLOC_WAIT) | |
5097 | flush_state = ALLOC_CHUNK; | |
365c5313 | 5098 | } while (flush_state < COMMIT_TRANS); |
21c7e756 MX |
5099 | } |
5100 | ||
957780eb JB |
5101 | static int wait_reserve_ticket(struct btrfs_fs_info *fs_info, |
5102 | struct btrfs_space_info *space_info, | |
5103 | struct reserve_ticket *ticket, u64 orig_bytes) | |
5104 | ||
21c7e756 | 5105 | { |
957780eb JB |
5106 | DEFINE_WAIT(wait); |
5107 | int ret = 0; | |
5108 | ||
5109 | spin_lock(&space_info->lock); | |
5110 | while (ticket->bytes > 0 && ticket->error == 0) { | |
5111 | ret = prepare_to_wait_event(&ticket->wait, &wait, TASK_KILLABLE); | |
5112 | if (ret) { | |
5113 | ret = -EINTR; | |
5114 | break; | |
5115 | } | |
5116 | spin_unlock(&space_info->lock); | |
5117 | ||
5118 | schedule(); | |
5119 | ||
5120 | finish_wait(&ticket->wait, &wait); | |
5121 | spin_lock(&space_info->lock); | |
5122 | } | |
5123 | if (!ret) | |
5124 | ret = ticket->error; | |
5125 | if (!list_empty(&ticket->list)) | |
5126 | list_del_init(&ticket->list); | |
5127 | if (ticket->bytes && ticket->bytes < orig_bytes) { | |
5128 | u64 num_bytes = orig_bytes - ticket->bytes; | |
5129 | space_info->bytes_may_use -= num_bytes; | |
5130 | trace_btrfs_space_reservation(fs_info, "space_info", | |
5131 | space_info->flags, num_bytes, 0); | |
5132 | } | |
5133 | spin_unlock(&space_info->lock); | |
5134 | ||
5135 | return ret; | |
21c7e756 MX |
5136 | } |
5137 | ||
4a92b1b8 JB |
5138 | /** |
5139 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space | |
5140 | * @root - the root we're allocating for | |
957780eb | 5141 | * @space_info - the space info we want to allocate from |
4a92b1b8 | 5142 | * @orig_bytes - the number of bytes we want |
48fc7f7e | 5143 | * @flush - whether or not we can flush to make our reservation |
8bb8ab2e | 5144 | * |
01327610 | 5145 | * This will reserve orig_bytes number of bytes from the space info associated |
4a92b1b8 JB |
5146 | * with the block_rsv. If there is not enough space it will make an attempt to |
5147 | * flush out space to make room. It will do this by flushing delalloc if | |
5148 | * possible or committing the transaction. If flush is 0 then no attempts to | |
5149 | * regain reservations will be made and this will fail if there is not enough | |
5150 | * space already. | |
8bb8ab2e | 5151 | */ |
957780eb JB |
5152 | static int __reserve_metadata_bytes(struct btrfs_root *root, |
5153 | struct btrfs_space_info *space_info, | |
5154 | u64 orig_bytes, | |
5155 | enum btrfs_reserve_flush_enum flush) | |
9ed74f2d | 5156 | { |
0b246afa | 5157 | struct btrfs_fs_info *fs_info = root->fs_info; |
957780eb | 5158 | struct reserve_ticket ticket; |
2bf64758 | 5159 | u64 used; |
8bb8ab2e | 5160 | int ret = 0; |
9ed74f2d | 5161 | |
957780eb | 5162 | ASSERT(orig_bytes); |
8ca17f0f | 5163 | ASSERT(!current->journal_info || flush != BTRFS_RESERVE_FLUSH_ALL); |
fdb5effd | 5164 | |
8bb8ab2e | 5165 | spin_lock(&space_info->lock); |
fdb5effd | 5166 | ret = -ENOSPC; |
2bf64758 JB |
5167 | used = space_info->bytes_used + space_info->bytes_reserved + |
5168 | space_info->bytes_pinned + space_info->bytes_readonly + | |
5169 | space_info->bytes_may_use; | |
9ed74f2d | 5170 | |
8bb8ab2e | 5171 | /* |
957780eb JB |
5172 | * If we have enough space then hooray, make our reservation and carry |
5173 | * on. If not see if we can overcommit, and if we can, hooray carry on. | |
5174 | * If not things get more complicated. | |
8bb8ab2e | 5175 | */ |
957780eb JB |
5176 | if (used + orig_bytes <= space_info->total_bytes) { |
5177 | space_info->bytes_may_use += orig_bytes; | |
0b246afa JM |
5178 | trace_btrfs_space_reservation(fs_info, "space_info", |
5179 | space_info->flags, orig_bytes, 1); | |
957780eb JB |
5180 | ret = 0; |
5181 | } else if (can_overcommit(root, space_info, orig_bytes, flush)) { | |
44734ed1 | 5182 | space_info->bytes_may_use += orig_bytes; |
0b246afa JM |
5183 | trace_btrfs_space_reservation(fs_info, "space_info", |
5184 | space_info->flags, orig_bytes, 1); | |
44734ed1 | 5185 | ret = 0; |
2bf64758 JB |
5186 | } |
5187 | ||
8bb8ab2e | 5188 | /* |
957780eb JB |
5189 | * If we couldn't make a reservation then setup our reservation ticket |
5190 | * and kick the async worker if it's not already running. | |
08e007d2 | 5191 | * |
957780eb JB |
5192 | * If we are a priority flusher then we just need to add our ticket to |
5193 | * the list and we will do our own flushing further down. | |
8bb8ab2e | 5194 | */ |
72bcd99d | 5195 | if (ret && flush != BTRFS_RESERVE_NO_FLUSH) { |
957780eb JB |
5196 | ticket.bytes = orig_bytes; |
5197 | ticket.error = 0; | |
5198 | init_waitqueue_head(&ticket.wait); | |
5199 | if (flush == BTRFS_RESERVE_FLUSH_ALL) { | |
5200 | list_add_tail(&ticket.list, &space_info->tickets); | |
5201 | if (!space_info->flush) { | |
5202 | space_info->flush = 1; | |
0b246afa | 5203 | trace_btrfs_trigger_flush(fs_info, |
f376df2b JB |
5204 | space_info->flags, |
5205 | orig_bytes, flush, | |
5206 | "enospc"); | |
957780eb JB |
5207 | queue_work(system_unbound_wq, |
5208 | &root->fs_info->async_reclaim_work); | |
5209 | } | |
5210 | } else { | |
5211 | list_add_tail(&ticket.list, | |
5212 | &space_info->priority_tickets); | |
5213 | } | |
21c7e756 MX |
5214 | } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
5215 | used += orig_bytes; | |
f6acfd50 JB |
5216 | /* |
5217 | * We will do the space reservation dance during log replay, | |
5218 | * which means we won't have fs_info->fs_root set, so don't do | |
5219 | * the async reclaim as we will panic. | |
5220 | */ | |
0b246afa | 5221 | if (!test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags) && |
87241c2e | 5222 | need_do_async_reclaim(space_info, root, used) && |
0b246afa JM |
5223 | !work_busy(&fs_info->async_reclaim_work)) { |
5224 | trace_btrfs_trigger_flush(fs_info, space_info->flags, | |
5225 | orig_bytes, flush, "preempt"); | |
21c7e756 | 5226 | queue_work(system_unbound_wq, |
0b246afa | 5227 | &fs_info->async_reclaim_work); |
f376df2b | 5228 | } |
8bb8ab2e | 5229 | } |
f0486c68 | 5230 | spin_unlock(&space_info->lock); |
08e007d2 | 5231 | if (!ret || flush == BTRFS_RESERVE_NO_FLUSH) |
957780eb | 5232 | return ret; |
f0486c68 | 5233 | |
957780eb | 5234 | if (flush == BTRFS_RESERVE_FLUSH_ALL) |
0b246afa | 5235 | return wait_reserve_ticket(fs_info, space_info, &ticket, |
957780eb | 5236 | orig_bytes); |
08e007d2 | 5237 | |
957780eb | 5238 | ret = 0; |
0b246afa | 5239 | priority_reclaim_metadata_space(fs_info, space_info, &ticket); |
957780eb JB |
5240 | spin_lock(&space_info->lock); |
5241 | if (ticket.bytes) { | |
5242 | if (ticket.bytes < orig_bytes) { | |
5243 | u64 num_bytes = orig_bytes - ticket.bytes; | |
5244 | space_info->bytes_may_use -= num_bytes; | |
0b246afa JM |
5245 | trace_btrfs_space_reservation(fs_info, "space_info", |
5246 | space_info->flags, | |
5247 | num_bytes, 0); | |
08e007d2 | 5248 | |
957780eb JB |
5249 | } |
5250 | list_del_init(&ticket.list); | |
5251 | ret = -ENOSPC; | |
5252 | } | |
5253 | spin_unlock(&space_info->lock); | |
5254 | ASSERT(list_empty(&ticket.list)); | |
5255 | return ret; | |
5256 | } | |
8bb8ab2e | 5257 | |
957780eb JB |
5258 | /** |
5259 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space | |
5260 | * @root - the root we're allocating for | |
5261 | * @block_rsv - the block_rsv we're allocating for | |
5262 | * @orig_bytes - the number of bytes we want | |
5263 | * @flush - whether or not we can flush to make our reservation | |
5264 | * | |
5265 | * This will reserve orgi_bytes number of bytes from the space info associated | |
5266 | * with the block_rsv. If there is not enough space it will make an attempt to | |
5267 | * flush out space to make room. It will do this by flushing delalloc if | |
5268 | * possible or committing the transaction. If flush is 0 then no attempts to | |
5269 | * regain reservations will be made and this will fail if there is not enough | |
5270 | * space already. | |
5271 | */ | |
5272 | static int reserve_metadata_bytes(struct btrfs_root *root, | |
5273 | struct btrfs_block_rsv *block_rsv, | |
5274 | u64 orig_bytes, | |
5275 | enum btrfs_reserve_flush_enum flush) | |
5276 | { | |
0b246afa JM |
5277 | struct btrfs_fs_info *fs_info = root->fs_info; |
5278 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
957780eb JB |
5279 | int ret; |
5280 | ||
5281 | ret = __reserve_metadata_bytes(root, block_rsv->space_info, orig_bytes, | |
5282 | flush); | |
5d80366e JB |
5283 | if (ret == -ENOSPC && |
5284 | unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) { | |
5d80366e JB |
5285 | if (block_rsv != global_rsv && |
5286 | !block_rsv_use_bytes(global_rsv, orig_bytes)) | |
5287 | ret = 0; | |
5288 | } | |
cab45e22 | 5289 | if (ret == -ENOSPC) |
0b246afa | 5290 | trace_btrfs_space_reservation(fs_info, "space_info:enospc", |
957780eb JB |
5291 | block_rsv->space_info->flags, |
5292 | orig_bytes, 1); | |
f0486c68 YZ |
5293 | return ret; |
5294 | } | |
5295 | ||
79787eaa JM |
5296 | static struct btrfs_block_rsv *get_block_rsv( |
5297 | const struct btrfs_trans_handle *trans, | |
5298 | const struct btrfs_root *root) | |
f0486c68 | 5299 | { |
0b246afa | 5300 | struct btrfs_fs_info *fs_info = root->fs_info; |
4c13d758 JB |
5301 | struct btrfs_block_rsv *block_rsv = NULL; |
5302 | ||
e9cf439f | 5303 | if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) || |
0b246afa JM |
5304 | (root == fs_info->csum_root && trans->adding_csums) || |
5305 | (root == fs_info->uuid_root)) | |
f7a81ea4 SB |
5306 | block_rsv = trans->block_rsv; |
5307 | ||
4c13d758 | 5308 | if (!block_rsv) |
f0486c68 YZ |
5309 | block_rsv = root->block_rsv; |
5310 | ||
5311 | if (!block_rsv) | |
0b246afa | 5312 | block_rsv = &fs_info->empty_block_rsv; |
f0486c68 YZ |
5313 | |
5314 | return block_rsv; | |
5315 | } | |
5316 | ||
5317 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, | |
5318 | u64 num_bytes) | |
5319 | { | |
5320 | int ret = -ENOSPC; | |
5321 | spin_lock(&block_rsv->lock); | |
5322 | if (block_rsv->reserved >= num_bytes) { | |
5323 | block_rsv->reserved -= num_bytes; | |
5324 | if (block_rsv->reserved < block_rsv->size) | |
5325 | block_rsv->full = 0; | |
5326 | ret = 0; | |
5327 | } | |
5328 | spin_unlock(&block_rsv->lock); | |
5329 | return ret; | |
5330 | } | |
5331 | ||
5332 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, | |
5333 | u64 num_bytes, int update_size) | |
5334 | { | |
5335 | spin_lock(&block_rsv->lock); | |
5336 | block_rsv->reserved += num_bytes; | |
5337 | if (update_size) | |
5338 | block_rsv->size += num_bytes; | |
5339 | else if (block_rsv->reserved >= block_rsv->size) | |
5340 | block_rsv->full = 1; | |
5341 | spin_unlock(&block_rsv->lock); | |
5342 | } | |
5343 | ||
d52be818 JB |
5344 | int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info, |
5345 | struct btrfs_block_rsv *dest, u64 num_bytes, | |
5346 | int min_factor) | |
5347 | { | |
5348 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
5349 | u64 min_bytes; | |
5350 | ||
5351 | if (global_rsv->space_info != dest->space_info) | |
5352 | return -ENOSPC; | |
5353 | ||
5354 | spin_lock(&global_rsv->lock); | |
5355 | min_bytes = div_factor(global_rsv->size, min_factor); | |
5356 | if (global_rsv->reserved < min_bytes + num_bytes) { | |
5357 | spin_unlock(&global_rsv->lock); | |
5358 | return -ENOSPC; | |
5359 | } | |
5360 | global_rsv->reserved -= num_bytes; | |
5361 | if (global_rsv->reserved < global_rsv->size) | |
5362 | global_rsv->full = 0; | |
5363 | spin_unlock(&global_rsv->lock); | |
5364 | ||
5365 | block_rsv_add_bytes(dest, num_bytes, 1); | |
5366 | return 0; | |
5367 | } | |
5368 | ||
957780eb JB |
5369 | /* |
5370 | * This is for space we already have accounted in space_info->bytes_may_use, so | |
5371 | * basically when we're returning space from block_rsv's. | |
5372 | */ | |
5373 | static void space_info_add_old_bytes(struct btrfs_fs_info *fs_info, | |
5374 | struct btrfs_space_info *space_info, | |
5375 | u64 num_bytes) | |
5376 | { | |
5377 | struct reserve_ticket *ticket; | |
5378 | struct list_head *head; | |
5379 | u64 used; | |
5380 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_NO_FLUSH; | |
5381 | bool check_overcommit = false; | |
5382 | ||
5383 | spin_lock(&space_info->lock); | |
5384 | head = &space_info->priority_tickets; | |
5385 | ||
5386 | /* | |
5387 | * If we are over our limit then we need to check and see if we can | |
5388 | * overcommit, and if we can't then we just need to free up our space | |
5389 | * and not satisfy any requests. | |
5390 | */ | |
5391 | used = space_info->bytes_used + space_info->bytes_reserved + | |
5392 | space_info->bytes_pinned + space_info->bytes_readonly + | |
5393 | space_info->bytes_may_use; | |
5394 | if (used - num_bytes >= space_info->total_bytes) | |
5395 | check_overcommit = true; | |
5396 | again: | |
5397 | while (!list_empty(head) && num_bytes) { | |
5398 | ticket = list_first_entry(head, struct reserve_ticket, | |
5399 | list); | |
5400 | /* | |
5401 | * We use 0 bytes because this space is already reserved, so | |
5402 | * adding the ticket space would be a double count. | |
5403 | */ | |
5404 | if (check_overcommit && | |
5405 | !can_overcommit(fs_info->extent_root, space_info, 0, | |
5406 | flush)) | |
5407 | break; | |
5408 | if (num_bytes >= ticket->bytes) { | |
5409 | list_del_init(&ticket->list); | |
5410 | num_bytes -= ticket->bytes; | |
5411 | ticket->bytes = 0; | |
ce129655 | 5412 | space_info->tickets_id++; |
957780eb JB |
5413 | wake_up(&ticket->wait); |
5414 | } else { | |
5415 | ticket->bytes -= num_bytes; | |
5416 | num_bytes = 0; | |
5417 | } | |
5418 | } | |
5419 | ||
5420 | if (num_bytes && head == &space_info->priority_tickets) { | |
5421 | head = &space_info->tickets; | |
5422 | flush = BTRFS_RESERVE_FLUSH_ALL; | |
5423 | goto again; | |
5424 | } | |
5425 | space_info->bytes_may_use -= num_bytes; | |
5426 | trace_btrfs_space_reservation(fs_info, "space_info", | |
5427 | space_info->flags, num_bytes, 0); | |
5428 | spin_unlock(&space_info->lock); | |
5429 | } | |
5430 | ||
5431 | /* | |
5432 | * This is for newly allocated space that isn't accounted in | |
5433 | * space_info->bytes_may_use yet. So if we allocate a chunk or unpin an extent | |
5434 | * we use this helper. | |
5435 | */ | |
5436 | static void space_info_add_new_bytes(struct btrfs_fs_info *fs_info, | |
5437 | struct btrfs_space_info *space_info, | |
5438 | u64 num_bytes) | |
5439 | { | |
5440 | struct reserve_ticket *ticket; | |
5441 | struct list_head *head = &space_info->priority_tickets; | |
5442 | ||
5443 | again: | |
5444 | while (!list_empty(head) && num_bytes) { | |
5445 | ticket = list_first_entry(head, struct reserve_ticket, | |
5446 | list); | |
5447 | if (num_bytes >= ticket->bytes) { | |
5448 | trace_btrfs_space_reservation(fs_info, "space_info", | |
5449 | space_info->flags, | |
5450 | ticket->bytes, 1); | |
5451 | list_del_init(&ticket->list); | |
5452 | num_bytes -= ticket->bytes; | |
5453 | space_info->bytes_may_use += ticket->bytes; | |
5454 | ticket->bytes = 0; | |
ce129655 | 5455 | space_info->tickets_id++; |
957780eb JB |
5456 | wake_up(&ticket->wait); |
5457 | } else { | |
5458 | trace_btrfs_space_reservation(fs_info, "space_info", | |
5459 | space_info->flags, | |
5460 | num_bytes, 1); | |
5461 | space_info->bytes_may_use += num_bytes; | |
5462 | ticket->bytes -= num_bytes; | |
5463 | num_bytes = 0; | |
5464 | } | |
5465 | } | |
5466 | ||
5467 | if (num_bytes && head == &space_info->priority_tickets) { | |
5468 | head = &space_info->tickets; | |
5469 | goto again; | |
5470 | } | |
5471 | } | |
5472 | ||
8c2a3ca2 JB |
5473 | static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info, |
5474 | struct btrfs_block_rsv *block_rsv, | |
62a45b60 | 5475 | struct btrfs_block_rsv *dest, u64 num_bytes) |
f0486c68 YZ |
5476 | { |
5477 | struct btrfs_space_info *space_info = block_rsv->space_info; | |
5478 | ||
5479 | spin_lock(&block_rsv->lock); | |
5480 | if (num_bytes == (u64)-1) | |
5481 | num_bytes = block_rsv->size; | |
5482 | block_rsv->size -= num_bytes; | |
5483 | if (block_rsv->reserved >= block_rsv->size) { | |
5484 | num_bytes = block_rsv->reserved - block_rsv->size; | |
5485 | block_rsv->reserved = block_rsv->size; | |
5486 | block_rsv->full = 1; | |
5487 | } else { | |
5488 | num_bytes = 0; | |
5489 | } | |
5490 | spin_unlock(&block_rsv->lock); | |
5491 | ||
5492 | if (num_bytes > 0) { | |
5493 | if (dest) { | |
e9e22899 JB |
5494 | spin_lock(&dest->lock); |
5495 | if (!dest->full) { | |
5496 | u64 bytes_to_add; | |
5497 | ||
5498 | bytes_to_add = dest->size - dest->reserved; | |
5499 | bytes_to_add = min(num_bytes, bytes_to_add); | |
5500 | dest->reserved += bytes_to_add; | |
5501 | if (dest->reserved >= dest->size) | |
5502 | dest->full = 1; | |
5503 | num_bytes -= bytes_to_add; | |
5504 | } | |
5505 | spin_unlock(&dest->lock); | |
5506 | } | |
957780eb JB |
5507 | if (num_bytes) |
5508 | space_info_add_old_bytes(fs_info, space_info, | |
5509 | num_bytes); | |
9ed74f2d | 5510 | } |
f0486c68 | 5511 | } |
4e06bdd6 | 5512 | |
25d609f8 JB |
5513 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src, |
5514 | struct btrfs_block_rsv *dst, u64 num_bytes, | |
5515 | int update_size) | |
f0486c68 YZ |
5516 | { |
5517 | int ret; | |
9ed74f2d | 5518 | |
f0486c68 YZ |
5519 | ret = block_rsv_use_bytes(src, num_bytes); |
5520 | if (ret) | |
5521 | return ret; | |
9ed74f2d | 5522 | |
25d609f8 | 5523 | block_rsv_add_bytes(dst, num_bytes, update_size); |
9ed74f2d JB |
5524 | return 0; |
5525 | } | |
5526 | ||
66d8f3dd | 5527 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type) |
9ed74f2d | 5528 | { |
f0486c68 YZ |
5529 | memset(rsv, 0, sizeof(*rsv)); |
5530 | spin_lock_init(&rsv->lock); | |
66d8f3dd | 5531 | rsv->type = type; |
f0486c68 YZ |
5532 | } |
5533 | ||
66d8f3dd MX |
5534 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root, |
5535 | unsigned short type) | |
f0486c68 YZ |
5536 | { |
5537 | struct btrfs_block_rsv *block_rsv; | |
5538 | struct btrfs_fs_info *fs_info = root->fs_info; | |
9ed74f2d | 5539 | |
f0486c68 YZ |
5540 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
5541 | if (!block_rsv) | |
5542 | return NULL; | |
9ed74f2d | 5543 | |
66d8f3dd | 5544 | btrfs_init_block_rsv(block_rsv, type); |
f0486c68 YZ |
5545 | block_rsv->space_info = __find_space_info(fs_info, |
5546 | BTRFS_BLOCK_GROUP_METADATA); | |
f0486c68 YZ |
5547 | return block_rsv; |
5548 | } | |
9ed74f2d | 5549 | |
f0486c68 YZ |
5550 | void btrfs_free_block_rsv(struct btrfs_root *root, |
5551 | struct btrfs_block_rsv *rsv) | |
5552 | { | |
2aaa6655 JB |
5553 | if (!rsv) |
5554 | return; | |
dabdb640 JB |
5555 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
5556 | kfree(rsv); | |
9ed74f2d JB |
5557 | } |
5558 | ||
cdfb080e CM |
5559 | void __btrfs_free_block_rsv(struct btrfs_block_rsv *rsv) |
5560 | { | |
5561 | kfree(rsv); | |
5562 | } | |
5563 | ||
08e007d2 MX |
5564 | int btrfs_block_rsv_add(struct btrfs_root *root, |
5565 | struct btrfs_block_rsv *block_rsv, u64 num_bytes, | |
5566 | enum btrfs_reserve_flush_enum flush) | |
9ed74f2d | 5567 | { |
f0486c68 | 5568 | int ret; |
9ed74f2d | 5569 | |
f0486c68 YZ |
5570 | if (num_bytes == 0) |
5571 | return 0; | |
8bb8ab2e | 5572 | |
61b520a9 | 5573 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
f0486c68 YZ |
5574 | if (!ret) { |
5575 | block_rsv_add_bytes(block_rsv, num_bytes, 1); | |
5576 | return 0; | |
5577 | } | |
9ed74f2d | 5578 | |
f0486c68 | 5579 | return ret; |
f0486c68 | 5580 | } |
9ed74f2d | 5581 | |
4a92b1b8 | 5582 | int btrfs_block_rsv_check(struct btrfs_root *root, |
36ba022a | 5583 | struct btrfs_block_rsv *block_rsv, int min_factor) |
f0486c68 YZ |
5584 | { |
5585 | u64 num_bytes = 0; | |
f0486c68 | 5586 | int ret = -ENOSPC; |
9ed74f2d | 5587 | |
f0486c68 YZ |
5588 | if (!block_rsv) |
5589 | return 0; | |
9ed74f2d | 5590 | |
f0486c68 | 5591 | spin_lock(&block_rsv->lock); |
36ba022a JB |
5592 | num_bytes = div_factor(block_rsv->size, min_factor); |
5593 | if (block_rsv->reserved >= num_bytes) | |
5594 | ret = 0; | |
5595 | spin_unlock(&block_rsv->lock); | |
9ed74f2d | 5596 | |
36ba022a JB |
5597 | return ret; |
5598 | } | |
5599 | ||
08e007d2 MX |
5600 | int btrfs_block_rsv_refill(struct btrfs_root *root, |
5601 | struct btrfs_block_rsv *block_rsv, u64 min_reserved, | |
5602 | enum btrfs_reserve_flush_enum flush) | |
36ba022a JB |
5603 | { |
5604 | u64 num_bytes = 0; | |
5605 | int ret = -ENOSPC; | |
5606 | ||
5607 | if (!block_rsv) | |
5608 | return 0; | |
5609 | ||
5610 | spin_lock(&block_rsv->lock); | |
5611 | num_bytes = min_reserved; | |
13553e52 | 5612 | if (block_rsv->reserved >= num_bytes) |
f0486c68 | 5613 | ret = 0; |
13553e52 | 5614 | else |
f0486c68 | 5615 | num_bytes -= block_rsv->reserved; |
f0486c68 | 5616 | spin_unlock(&block_rsv->lock); |
13553e52 | 5617 | |
f0486c68 YZ |
5618 | if (!ret) |
5619 | return 0; | |
5620 | ||
aa38a711 | 5621 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
dabdb640 JB |
5622 | if (!ret) { |
5623 | block_rsv_add_bytes(block_rsv, num_bytes, 0); | |
f0486c68 | 5624 | return 0; |
6a63209f | 5625 | } |
9ed74f2d | 5626 | |
13553e52 | 5627 | return ret; |
f0486c68 YZ |
5628 | } |
5629 | ||
f0486c68 YZ |
5630 | void btrfs_block_rsv_release(struct btrfs_root *root, |
5631 | struct btrfs_block_rsv *block_rsv, | |
5632 | u64 num_bytes) | |
5633 | { | |
0b246afa JM |
5634 | struct btrfs_fs_info *fs_info = root->fs_info; |
5635 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
5636 | ||
17504584 | 5637 | if (global_rsv == block_rsv || |
f0486c68 YZ |
5638 | block_rsv->space_info != global_rsv->space_info) |
5639 | global_rsv = NULL; | |
0b246afa | 5640 | block_rsv_release_bytes(fs_info, block_rsv, global_rsv, num_bytes); |
6a63209f JB |
5641 | } |
5642 | ||
8929ecfa YZ |
5643 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
5644 | { | |
5645 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; | |
5646 | struct btrfs_space_info *sinfo = block_rsv->space_info; | |
5647 | u64 num_bytes; | |
6a63209f | 5648 | |
ae2e4728 JB |
5649 | /* |
5650 | * The global block rsv is based on the size of the extent tree, the | |
5651 | * checksum tree and the root tree. If the fs is empty we want to set | |
5652 | * it to a minimal amount for safety. | |
5653 | */ | |
5654 | num_bytes = btrfs_root_used(&fs_info->extent_root->root_item) + | |
5655 | btrfs_root_used(&fs_info->csum_root->root_item) + | |
5656 | btrfs_root_used(&fs_info->tree_root->root_item); | |
5657 | num_bytes = max_t(u64, num_bytes, SZ_16M); | |
33b4d47f | 5658 | |
8929ecfa | 5659 | spin_lock(&sinfo->lock); |
1f699d38 | 5660 | spin_lock(&block_rsv->lock); |
4e06bdd6 | 5661 | |
ee22184b | 5662 | block_rsv->size = min_t(u64, num_bytes, SZ_512M); |
4e06bdd6 | 5663 | |
fb4b10e5 JB |
5664 | if (block_rsv->reserved < block_rsv->size) { |
5665 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + | |
5666 | sinfo->bytes_reserved + sinfo->bytes_readonly + | |
5667 | sinfo->bytes_may_use; | |
5668 | if (sinfo->total_bytes > num_bytes) { | |
5669 | num_bytes = sinfo->total_bytes - num_bytes; | |
5670 | num_bytes = min(num_bytes, | |
5671 | block_rsv->size - block_rsv->reserved); | |
5672 | block_rsv->reserved += num_bytes; | |
5673 | sinfo->bytes_may_use += num_bytes; | |
5674 | trace_btrfs_space_reservation(fs_info, "space_info", | |
5675 | sinfo->flags, num_bytes, | |
5676 | 1); | |
5677 | } | |
5678 | } else if (block_rsv->reserved > block_rsv->size) { | |
8929ecfa | 5679 | num_bytes = block_rsv->reserved - block_rsv->size; |
fb25e914 | 5680 | sinfo->bytes_may_use -= num_bytes; |
8c2a3ca2 | 5681 | trace_btrfs_space_reservation(fs_info, "space_info", |
2bcc0328 | 5682 | sinfo->flags, num_bytes, 0); |
8929ecfa | 5683 | block_rsv->reserved = block_rsv->size; |
8929ecfa | 5684 | } |
182608c8 | 5685 | |
fb4b10e5 JB |
5686 | if (block_rsv->reserved == block_rsv->size) |
5687 | block_rsv->full = 1; | |
5688 | else | |
5689 | block_rsv->full = 0; | |
5690 | ||
8929ecfa | 5691 | spin_unlock(&block_rsv->lock); |
1f699d38 | 5692 | spin_unlock(&sinfo->lock); |
6a63209f JB |
5693 | } |
5694 | ||
f0486c68 | 5695 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
6a63209f | 5696 | { |
f0486c68 | 5697 | struct btrfs_space_info *space_info; |
6a63209f | 5698 | |
f0486c68 YZ |
5699 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
5700 | fs_info->chunk_block_rsv.space_info = space_info; | |
6a63209f | 5701 | |
f0486c68 | 5702 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
8929ecfa | 5703 | fs_info->global_block_rsv.space_info = space_info; |
8929ecfa | 5704 | fs_info->delalloc_block_rsv.space_info = space_info; |
f0486c68 YZ |
5705 | fs_info->trans_block_rsv.space_info = space_info; |
5706 | fs_info->empty_block_rsv.space_info = space_info; | |
6d668dda | 5707 | fs_info->delayed_block_rsv.space_info = space_info; |
f0486c68 | 5708 | |
8929ecfa YZ |
5709 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
5710 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; | |
5711 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; | |
5712 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; | |
3a6cad90 SB |
5713 | if (fs_info->quota_root) |
5714 | fs_info->quota_root->block_rsv = &fs_info->global_block_rsv; | |
f0486c68 | 5715 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
8929ecfa | 5716 | |
8929ecfa | 5717 | update_global_block_rsv(fs_info); |
6a63209f JB |
5718 | } |
5719 | ||
8929ecfa | 5720 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
6a63209f | 5721 | { |
8c2a3ca2 JB |
5722 | block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL, |
5723 | (u64)-1); | |
8929ecfa YZ |
5724 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); |
5725 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); | |
5726 | WARN_ON(fs_info->trans_block_rsv.size > 0); | |
5727 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); | |
5728 | WARN_ON(fs_info->chunk_block_rsv.size > 0); | |
5729 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); | |
6d668dda JB |
5730 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
5731 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); | |
fcb80c2a JB |
5732 | } |
5733 | ||
a22285a6 YZ |
5734 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
5735 | struct btrfs_root *root) | |
6a63209f | 5736 | { |
0b246afa JM |
5737 | struct btrfs_fs_info *fs_info = root->fs_info; |
5738 | ||
0e721106 JB |
5739 | if (!trans->block_rsv) |
5740 | return; | |
5741 | ||
a22285a6 YZ |
5742 | if (!trans->bytes_reserved) |
5743 | return; | |
6a63209f | 5744 | |
0b246afa | 5745 | trace_btrfs_space_reservation(fs_info, "transaction", |
2bcc0328 | 5746 | trans->transid, trans->bytes_reserved, 0); |
b24e03db | 5747 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
a22285a6 YZ |
5748 | trans->bytes_reserved = 0; |
5749 | } | |
6a63209f | 5750 | |
4fbcdf66 FM |
5751 | /* |
5752 | * To be called after all the new block groups attached to the transaction | |
5753 | * handle have been created (btrfs_create_pending_block_groups()). | |
5754 | */ | |
5755 | void btrfs_trans_release_chunk_metadata(struct btrfs_trans_handle *trans) | |
5756 | { | |
64b63580 | 5757 | struct btrfs_fs_info *fs_info = trans->fs_info; |
4fbcdf66 FM |
5758 | |
5759 | if (!trans->chunk_bytes_reserved) | |
5760 | return; | |
5761 | ||
5762 | WARN_ON_ONCE(!list_empty(&trans->new_bgs)); | |
5763 | ||
5764 | block_rsv_release_bytes(fs_info, &fs_info->chunk_block_rsv, NULL, | |
5765 | trans->chunk_bytes_reserved); | |
5766 | trans->chunk_bytes_reserved = 0; | |
5767 | } | |
5768 | ||
79787eaa | 5769 | /* Can only return 0 or -ENOSPC */ |
d68fc57b YZ |
5770 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
5771 | struct inode *inode) | |
5772 | { | |
0b246afa | 5773 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
d68fc57b | 5774 | struct btrfs_root *root = BTRFS_I(inode)->root; |
40acc3ee JB |
5775 | /* |
5776 | * We always use trans->block_rsv here as we will have reserved space | |
5777 | * for our orphan when starting the transaction, using get_block_rsv() | |
5778 | * here will sometimes make us choose the wrong block rsv as we could be | |
5779 | * doing a reloc inode for a non refcounted root. | |
5780 | */ | |
5781 | struct btrfs_block_rsv *src_rsv = trans->block_rsv; | |
d68fc57b YZ |
5782 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; |
5783 | ||
5784 | /* | |
fcb80c2a JB |
5785 | * We need to hold space in order to delete our orphan item once we've |
5786 | * added it, so this takes the reservation so we can release it later | |
5787 | * when we are truly done with the orphan item. | |
d68fc57b | 5788 | */ |
0b246afa JM |
5789 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
5790 | ||
5791 | trace_btrfs_space_reservation(fs_info, "orphan", | |
8c2a3ca2 | 5792 | btrfs_ino(inode), num_bytes, 1); |
25d609f8 | 5793 | return btrfs_block_rsv_migrate(src_rsv, dst_rsv, num_bytes, 1); |
6a63209f JB |
5794 | } |
5795 | ||
d68fc57b | 5796 | void btrfs_orphan_release_metadata(struct inode *inode) |
97e728d4 | 5797 | { |
0b246afa | 5798 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
d68fc57b | 5799 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0b246afa JM |
5800 | u64 num_bytes = btrfs_calc_trans_metadata_size(fs_info, 1); |
5801 | ||
5802 | trace_btrfs_space_reservation(fs_info, "orphan", | |
8c2a3ca2 | 5803 | btrfs_ino(inode), num_bytes, 0); |
d68fc57b YZ |
5804 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
5805 | } | |
97e728d4 | 5806 | |
d5c12070 MX |
5807 | /* |
5808 | * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation | |
5809 | * root: the root of the parent directory | |
5810 | * rsv: block reservation | |
5811 | * items: the number of items that we need do reservation | |
5812 | * qgroup_reserved: used to return the reserved size in qgroup | |
5813 | * | |
5814 | * This function is used to reserve the space for snapshot/subvolume | |
5815 | * creation and deletion. Those operations are different with the | |
5816 | * common file/directory operations, they change two fs/file trees | |
5817 | * and root tree, the number of items that the qgroup reserves is | |
5818 | * different with the free space reservation. So we can not use | |
01327610 | 5819 | * the space reservation mechanism in start_transaction(). |
d5c12070 MX |
5820 | */ |
5821 | int btrfs_subvolume_reserve_metadata(struct btrfs_root *root, | |
5822 | struct btrfs_block_rsv *rsv, | |
5823 | int items, | |
ee3441b4 JM |
5824 | u64 *qgroup_reserved, |
5825 | bool use_global_rsv) | |
a22285a6 | 5826 | { |
d5c12070 MX |
5827 | u64 num_bytes; |
5828 | int ret; | |
0b246afa JM |
5829 | struct btrfs_fs_info *fs_info = root->fs_info; |
5830 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
d5c12070 | 5831 | |
0b246afa | 5832 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
d5c12070 | 5833 | /* One for parent inode, two for dir entries */ |
0b246afa | 5834 | num_bytes = 3 * fs_info->nodesize; |
7174109c | 5835 | ret = btrfs_qgroup_reserve_meta(root, num_bytes); |
d5c12070 MX |
5836 | if (ret) |
5837 | return ret; | |
5838 | } else { | |
5839 | num_bytes = 0; | |
5840 | } | |
5841 | ||
5842 | *qgroup_reserved = num_bytes; | |
5843 | ||
0b246afa JM |
5844 | num_bytes = btrfs_calc_trans_metadata_size(fs_info, items); |
5845 | rsv->space_info = __find_space_info(fs_info, | |
d5c12070 MX |
5846 | BTRFS_BLOCK_GROUP_METADATA); |
5847 | ret = btrfs_block_rsv_add(root, rsv, num_bytes, | |
5848 | BTRFS_RESERVE_FLUSH_ALL); | |
ee3441b4 JM |
5849 | |
5850 | if (ret == -ENOSPC && use_global_rsv) | |
25d609f8 | 5851 | ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes, 1); |
ee3441b4 | 5852 | |
7174109c QW |
5853 | if (ret && *qgroup_reserved) |
5854 | btrfs_qgroup_free_meta(root, *qgroup_reserved); | |
d5c12070 MX |
5855 | |
5856 | return ret; | |
5857 | } | |
5858 | ||
5859 | void btrfs_subvolume_release_metadata(struct btrfs_root *root, | |
5860 | struct btrfs_block_rsv *rsv, | |
5861 | u64 qgroup_reserved) | |
5862 | { | |
5863 | btrfs_block_rsv_release(root, rsv, (u64)-1); | |
97e728d4 JB |
5864 | } |
5865 | ||
7709cde3 JB |
5866 | /** |
5867 | * drop_outstanding_extent - drop an outstanding extent | |
5868 | * @inode: the inode we're dropping the extent for | |
01327610 | 5869 | * @num_bytes: the number of bytes we're releasing. |
7709cde3 JB |
5870 | * |
5871 | * This is called when we are freeing up an outstanding extent, either called | |
5872 | * after an error or after an extent is written. This will return the number of | |
5873 | * reserved extents that need to be freed. This must be called with | |
5874 | * BTRFS_I(inode)->lock held. | |
5875 | */ | |
dcab6a3b | 5876 | static unsigned drop_outstanding_extent(struct inode *inode, u64 num_bytes) |
9e0baf60 | 5877 | { |
7fd2ae21 | 5878 | unsigned drop_inode_space = 0; |
9e0baf60 | 5879 | unsigned dropped_extents = 0; |
dcab6a3b | 5880 | unsigned num_extents = 0; |
9e0baf60 | 5881 | |
dcab6a3b JB |
5882 | num_extents = (unsigned)div64_u64(num_bytes + |
5883 | BTRFS_MAX_EXTENT_SIZE - 1, | |
5884 | BTRFS_MAX_EXTENT_SIZE); | |
5885 | ASSERT(num_extents); | |
5886 | ASSERT(BTRFS_I(inode)->outstanding_extents >= num_extents); | |
5887 | BTRFS_I(inode)->outstanding_extents -= num_extents; | |
9e0baf60 | 5888 | |
7fd2ae21 | 5889 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
72ac3c0d JB |
5890 | test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
5891 | &BTRFS_I(inode)->runtime_flags)) | |
7fd2ae21 | 5892 | drop_inode_space = 1; |
7fd2ae21 | 5893 | |
9e0baf60 | 5894 | /* |
01327610 | 5895 | * If we have more or the same amount of outstanding extents than we have |
9e0baf60 JB |
5896 | * reserved then we need to leave the reserved extents count alone. |
5897 | */ | |
5898 | if (BTRFS_I(inode)->outstanding_extents >= | |
5899 | BTRFS_I(inode)->reserved_extents) | |
7fd2ae21 | 5900 | return drop_inode_space; |
9e0baf60 JB |
5901 | |
5902 | dropped_extents = BTRFS_I(inode)->reserved_extents - | |
5903 | BTRFS_I(inode)->outstanding_extents; | |
5904 | BTRFS_I(inode)->reserved_extents -= dropped_extents; | |
7fd2ae21 | 5905 | return dropped_extents + drop_inode_space; |
9e0baf60 JB |
5906 | } |
5907 | ||
7709cde3 | 5908 | /** |
01327610 NS |
5909 | * calc_csum_metadata_size - return the amount of metadata space that must be |
5910 | * reserved/freed for the given bytes. | |
7709cde3 JB |
5911 | * @inode: the inode we're manipulating |
5912 | * @num_bytes: the number of bytes in question | |
5913 | * @reserve: 1 if we are reserving space, 0 if we are freeing space | |
5914 | * | |
5915 | * This adjusts the number of csum_bytes in the inode and then returns the | |
5916 | * correct amount of metadata that must either be reserved or freed. We | |
5917 | * calculate how many checksums we can fit into one leaf and then divide the | |
5918 | * number of bytes that will need to be checksumed by this value to figure out | |
5919 | * how many checksums will be required. If we are adding bytes then the number | |
5920 | * may go up and we will return the number of additional bytes that must be | |
5921 | * reserved. If it is going down we will return the number of bytes that must | |
5922 | * be freed. | |
5923 | * | |
5924 | * This must be called with BTRFS_I(inode)->lock held. | |
5925 | */ | |
5926 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, | |
5927 | int reserve) | |
6324fbf3 | 5928 | { |
0b246afa | 5929 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
7709cde3 | 5930 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1262133b | 5931 | u64 old_csums, num_csums; |
7709cde3 JB |
5932 | |
5933 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && | |
5934 | BTRFS_I(inode)->csum_bytes == 0) | |
5935 | return 0; | |
5936 | ||
28f75a0e | 5937 | old_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); |
7709cde3 JB |
5938 | if (reserve) |
5939 | BTRFS_I(inode)->csum_bytes += num_bytes; | |
5940 | else | |
5941 | BTRFS_I(inode)->csum_bytes -= num_bytes; | |
28f75a0e | 5942 | num_csums = btrfs_csum_bytes_to_leaves(root, BTRFS_I(inode)->csum_bytes); |
7709cde3 JB |
5943 | |
5944 | /* No change, no need to reserve more */ | |
5945 | if (old_csums == num_csums) | |
5946 | return 0; | |
5947 | ||
5948 | if (reserve) | |
0b246afa | 5949 | return btrfs_calc_trans_metadata_size(fs_info, |
7709cde3 JB |
5950 | num_csums - old_csums); |
5951 | ||
0b246afa | 5952 | return btrfs_calc_trans_metadata_size(fs_info, old_csums - num_csums); |
0ca1f7ce | 5953 | } |
c146afad | 5954 | |
0ca1f7ce YZ |
5955 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
5956 | { | |
0b246afa | 5957 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0ca1f7ce | 5958 | struct btrfs_root *root = BTRFS_I(inode)->root; |
0b246afa | 5959 | struct btrfs_block_rsv *block_rsv = &fs_info->delalloc_block_rsv; |
9e0baf60 | 5960 | u64 to_reserve = 0; |
660d3f6c | 5961 | u64 csum_bytes; |
9e0baf60 | 5962 | unsigned nr_extents = 0; |
08e007d2 | 5963 | enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL; |
eb6b88d9 | 5964 | int ret = 0; |
c64c2bd8 | 5965 | bool delalloc_lock = true; |
88e081bf WS |
5966 | u64 to_free = 0; |
5967 | unsigned dropped; | |
48c3d480 | 5968 | bool release_extra = false; |
6324fbf3 | 5969 | |
c64c2bd8 JB |
5970 | /* If we are a free space inode we need to not flush since we will be in |
5971 | * the middle of a transaction commit. We also don't need the delalloc | |
5972 | * mutex since we won't race with anybody. We need this mostly to make | |
5973 | * lockdep shut its filthy mouth. | |
bac357dc JB |
5974 | * |
5975 | * If we have a transaction open (can happen if we call truncate_block | |
5976 | * from truncate), then we need FLUSH_LIMIT so we don't deadlock. | |
c64c2bd8 JB |
5977 | */ |
5978 | if (btrfs_is_free_space_inode(inode)) { | |
08e007d2 | 5979 | flush = BTRFS_RESERVE_NO_FLUSH; |
c64c2bd8 | 5980 | delalloc_lock = false; |
bac357dc JB |
5981 | } else if (current->journal_info) { |
5982 | flush = BTRFS_RESERVE_FLUSH_LIMIT; | |
c64c2bd8 | 5983 | } |
c09544e0 | 5984 | |
08e007d2 | 5985 | if (flush != BTRFS_RESERVE_NO_FLUSH && |
0b246afa | 5986 | btrfs_transaction_in_commit(fs_info)) |
0ca1f7ce | 5987 | schedule_timeout(1); |
ec44a35c | 5988 | |
c64c2bd8 JB |
5989 | if (delalloc_lock) |
5990 | mutex_lock(&BTRFS_I(inode)->delalloc_mutex); | |
5991 | ||
0b246afa | 5992 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
8bb8ab2e | 5993 | |
9e0baf60 | 5994 | spin_lock(&BTRFS_I(inode)->lock); |
6a41dd09 JB |
5995 | nr_extents = (unsigned)div64_u64(num_bytes + |
5996 | BTRFS_MAX_EXTENT_SIZE - 1, | |
5997 | BTRFS_MAX_EXTENT_SIZE); | |
5998 | BTRFS_I(inode)->outstanding_extents += nr_extents; | |
9e0baf60 | 5999 | |
48c3d480 | 6000 | nr_extents = 0; |
9e0baf60 | 6001 | if (BTRFS_I(inode)->outstanding_extents > |
660d3f6c | 6002 | BTRFS_I(inode)->reserved_extents) |
48c3d480 | 6003 | nr_extents += BTRFS_I(inode)->outstanding_extents - |
9e0baf60 | 6004 | BTRFS_I(inode)->reserved_extents; |
57a45ced | 6005 | |
48c3d480 | 6006 | /* We always want to reserve a slot for updating the inode. */ |
0b246afa | 6007 | to_reserve = btrfs_calc_trans_metadata_size(fs_info, nr_extents + 1); |
7709cde3 | 6008 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
660d3f6c | 6009 | csum_bytes = BTRFS_I(inode)->csum_bytes; |
9e0baf60 | 6010 | spin_unlock(&BTRFS_I(inode)->lock); |
57a45ced | 6011 | |
0b246afa | 6012 | if (test_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags)) { |
7174109c | 6013 | ret = btrfs_qgroup_reserve_meta(root, |
0b246afa | 6014 | nr_extents * fs_info->nodesize); |
88e081bf WS |
6015 | if (ret) |
6016 | goto out_fail; | |
6017 | } | |
c5567237 | 6018 | |
48c3d480 | 6019 | ret = btrfs_block_rsv_add(root, block_rsv, to_reserve, flush); |
88e081bf | 6020 | if (unlikely(ret)) { |
da17066c | 6021 | btrfs_qgroup_free_meta(root, |
0b246afa | 6022 | nr_extents * fs_info->nodesize); |
88e081bf | 6023 | goto out_fail; |
9e0baf60 | 6024 | } |
25179201 | 6025 | |
660d3f6c | 6026 | spin_lock(&BTRFS_I(inode)->lock); |
48c3d480 | 6027 | if (test_and_set_bit(BTRFS_INODE_DELALLOC_META_RESERVED, |
f485c9ee | 6028 | &BTRFS_I(inode)->runtime_flags)) { |
0b246afa | 6029 | to_reserve -= btrfs_calc_trans_metadata_size(fs_info, 1); |
48c3d480 | 6030 | release_extra = true; |
660d3f6c JB |
6031 | } |
6032 | BTRFS_I(inode)->reserved_extents += nr_extents; | |
6033 | spin_unlock(&BTRFS_I(inode)->lock); | |
c64c2bd8 JB |
6034 | |
6035 | if (delalloc_lock) | |
6036 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); | |
660d3f6c | 6037 | |
8c2a3ca2 | 6038 | if (to_reserve) |
0b246afa | 6039 | trace_btrfs_space_reservation(fs_info, "delalloc", |
8c2a3ca2 | 6040 | btrfs_ino(inode), to_reserve, 1); |
48c3d480 JB |
6041 | if (release_extra) |
6042 | btrfs_block_rsv_release(root, block_rsv, | |
0b246afa | 6043 | btrfs_calc_trans_metadata_size(fs_info, 1)); |
0ca1f7ce | 6044 | return 0; |
88e081bf WS |
6045 | |
6046 | out_fail: | |
6047 | spin_lock(&BTRFS_I(inode)->lock); | |
dcab6a3b | 6048 | dropped = drop_outstanding_extent(inode, num_bytes); |
88e081bf WS |
6049 | /* |
6050 | * If the inodes csum_bytes is the same as the original | |
6051 | * csum_bytes then we know we haven't raced with any free()ers | |
6052 | * so we can just reduce our inodes csum bytes and carry on. | |
88e081bf | 6053 | */ |
f4881bc7 | 6054 | if (BTRFS_I(inode)->csum_bytes == csum_bytes) { |
88e081bf | 6055 | calc_csum_metadata_size(inode, num_bytes, 0); |
f4881bc7 JB |
6056 | } else { |
6057 | u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes; | |
6058 | u64 bytes; | |
6059 | ||
6060 | /* | |
6061 | * This is tricky, but first we need to figure out how much we | |
01327610 | 6062 | * freed from any free-ers that occurred during this |
f4881bc7 JB |
6063 | * reservation, so we reset ->csum_bytes to the csum_bytes |
6064 | * before we dropped our lock, and then call the free for the | |
6065 | * number of bytes that were freed while we were trying our | |
6066 | * reservation. | |
6067 | */ | |
6068 | bytes = csum_bytes - BTRFS_I(inode)->csum_bytes; | |
6069 | BTRFS_I(inode)->csum_bytes = csum_bytes; | |
6070 | to_free = calc_csum_metadata_size(inode, bytes, 0); | |
6071 | ||
6072 | ||
6073 | /* | |
6074 | * Now we need to see how much we would have freed had we not | |
6075 | * been making this reservation and our ->csum_bytes were not | |
6076 | * artificially inflated. | |
6077 | */ | |
6078 | BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes; | |
6079 | bytes = csum_bytes - orig_csum_bytes; | |
6080 | bytes = calc_csum_metadata_size(inode, bytes, 0); | |
6081 | ||
6082 | /* | |
6083 | * Now reset ->csum_bytes to what it should be. If bytes is | |
01327610 | 6084 | * more than to_free then we would have freed more space had we |
f4881bc7 JB |
6085 | * not had an artificially high ->csum_bytes, so we need to free |
6086 | * the remainder. If bytes is the same or less then we don't | |
6087 | * need to do anything, the other free-ers did the correct | |
6088 | * thing. | |
6089 | */ | |
6090 | BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes; | |
6091 | if (bytes > to_free) | |
6092 | to_free = bytes - to_free; | |
6093 | else | |
6094 | to_free = 0; | |
6095 | } | |
88e081bf | 6096 | spin_unlock(&BTRFS_I(inode)->lock); |
e2d1f923 | 6097 | if (dropped) |
0b246afa | 6098 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
88e081bf WS |
6099 | |
6100 | if (to_free) { | |
6101 | btrfs_block_rsv_release(root, block_rsv, to_free); | |
0b246afa | 6102 | trace_btrfs_space_reservation(fs_info, "delalloc", |
88e081bf WS |
6103 | btrfs_ino(inode), to_free, 0); |
6104 | } | |
6105 | if (delalloc_lock) | |
6106 | mutex_unlock(&BTRFS_I(inode)->delalloc_mutex); | |
6107 | return ret; | |
0ca1f7ce YZ |
6108 | } |
6109 | ||
7709cde3 JB |
6110 | /** |
6111 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode | |
6112 | * @inode: the inode to release the reservation for | |
6113 | * @num_bytes: the number of bytes we're releasing | |
6114 | * | |
6115 | * This will release the metadata reservation for an inode. This can be called | |
6116 | * once we complete IO for a given set of bytes to release their metadata | |
6117 | * reservations. | |
6118 | */ | |
0ca1f7ce YZ |
6119 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
6120 | { | |
0b246afa | 6121 | struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); |
0ca1f7ce | 6122 | struct btrfs_root *root = BTRFS_I(inode)->root; |
9e0baf60 JB |
6123 | u64 to_free = 0; |
6124 | unsigned dropped; | |
0ca1f7ce | 6125 | |
0b246afa | 6126 | num_bytes = ALIGN(num_bytes, fs_info->sectorsize); |
7709cde3 | 6127 | spin_lock(&BTRFS_I(inode)->lock); |
dcab6a3b | 6128 | dropped = drop_outstanding_extent(inode, num_bytes); |
97e728d4 | 6129 | |
0934856d MX |
6130 | if (num_bytes) |
6131 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); | |
7709cde3 | 6132 | spin_unlock(&BTRFS_I(inode)->lock); |
9e0baf60 | 6133 | if (dropped > 0) |
0b246afa | 6134 | to_free += btrfs_calc_trans_metadata_size(fs_info, dropped); |
0ca1f7ce | 6135 | |
0b246afa | 6136 | if (btrfs_is_testing(fs_info)) |
6a3891c5 JB |
6137 | return; |
6138 | ||
0b246afa | 6139 | trace_btrfs_space_reservation(fs_info, "delalloc", |
8c2a3ca2 | 6140 | btrfs_ino(inode), to_free, 0); |
c5567237 | 6141 | |
0b246afa | 6142 | btrfs_block_rsv_release(root, &fs_info->delalloc_block_rsv, to_free); |
0ca1f7ce YZ |
6143 | } |
6144 | ||
1ada3a62 | 6145 | /** |
7cf5b976 | 6146 | * btrfs_delalloc_reserve_space - reserve data and metadata space for |
1ada3a62 QW |
6147 | * delalloc |
6148 | * @inode: inode we're writing to | |
6149 | * @start: start range we are writing to | |
6150 | * @len: how long the range we are writing to | |
6151 | * | |
1ada3a62 QW |
6152 | * This will do the following things |
6153 | * | |
6154 | * o reserve space in data space info for num bytes | |
6155 | * and reserve precious corresponding qgroup space | |
6156 | * (Done in check_data_free_space) | |
6157 | * | |
6158 | * o reserve space for metadata space, based on the number of outstanding | |
6159 | * extents and how much csums will be needed | |
6160 | * also reserve metadata space in a per root over-reserve method. | |
6161 | * o add to the inodes->delalloc_bytes | |
6162 | * o add it to the fs_info's delalloc inodes list. | |
6163 | * (Above 3 all done in delalloc_reserve_metadata) | |
6164 | * | |
6165 | * Return 0 for success | |
6166 | * Return <0 for error(-ENOSPC or -EQUOT) | |
6167 | */ | |
7cf5b976 | 6168 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 start, u64 len) |
1ada3a62 QW |
6169 | { |
6170 | int ret; | |
6171 | ||
7cf5b976 | 6172 | ret = btrfs_check_data_free_space(inode, start, len); |
1ada3a62 QW |
6173 | if (ret < 0) |
6174 | return ret; | |
6175 | ret = btrfs_delalloc_reserve_metadata(inode, len); | |
6176 | if (ret < 0) | |
7cf5b976 | 6177 | btrfs_free_reserved_data_space(inode, start, len); |
1ada3a62 QW |
6178 | return ret; |
6179 | } | |
6180 | ||
7709cde3 | 6181 | /** |
7cf5b976 | 6182 | * btrfs_delalloc_release_space - release data and metadata space for delalloc |
1ada3a62 QW |
6183 | * @inode: inode we're releasing space for |
6184 | * @start: start position of the space already reserved | |
6185 | * @len: the len of the space already reserved | |
6186 | * | |
6187 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is | |
6188 | * called in the case that we don't need the metadata AND data reservations | |
6189 | * anymore. So if there is an error or we insert an inline extent. | |
6190 | * | |
6191 | * This function will release the metadata space that was not used and will | |
6192 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes | |
6193 | * list if there are no delalloc bytes left. | |
6194 | * Also it will handle the qgroup reserved space. | |
6195 | */ | |
7cf5b976 | 6196 | void btrfs_delalloc_release_space(struct inode *inode, u64 start, u64 len) |
1ada3a62 QW |
6197 | { |
6198 | btrfs_delalloc_release_metadata(inode, len); | |
7cf5b976 | 6199 | btrfs_free_reserved_data_space(inode, start, len); |
6324fbf3 CM |
6200 | } |
6201 | ||
ce93ec54 | 6202 | static int update_block_group(struct btrfs_trans_handle *trans, |
6202df69 | 6203 | struct btrfs_fs_info *info, u64 bytenr, |
ce93ec54 | 6204 | u64 num_bytes, int alloc) |
9078a3e1 | 6205 | { |
0af3d00b | 6206 | struct btrfs_block_group_cache *cache = NULL; |
db94535d | 6207 | u64 total = num_bytes; |
9078a3e1 | 6208 | u64 old_val; |
db94535d | 6209 | u64 byte_in_group; |
0af3d00b | 6210 | int factor; |
3e1ad54f | 6211 | |
5d4f98a2 | 6212 | /* block accounting for super block */ |
eb73c1b7 | 6213 | spin_lock(&info->delalloc_root_lock); |
6c41761f | 6214 | old_val = btrfs_super_bytes_used(info->super_copy); |
5d4f98a2 YZ |
6215 | if (alloc) |
6216 | old_val += num_bytes; | |
6217 | else | |
6218 | old_val -= num_bytes; | |
6c41761f | 6219 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
eb73c1b7 | 6220 | spin_unlock(&info->delalloc_root_lock); |
5d4f98a2 | 6221 | |
d397712b | 6222 | while (total) { |
db94535d | 6223 | cache = btrfs_lookup_block_group(info, bytenr); |
f3465ca4 | 6224 | if (!cache) |
79787eaa | 6225 | return -ENOENT; |
b742bb82 YZ |
6226 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
6227 | BTRFS_BLOCK_GROUP_RAID1 | | |
6228 | BTRFS_BLOCK_GROUP_RAID10)) | |
6229 | factor = 2; | |
6230 | else | |
6231 | factor = 1; | |
9d66e233 JB |
6232 | /* |
6233 | * If this block group has free space cache written out, we | |
6234 | * need to make sure to load it if we are removing space. This | |
6235 | * is because we need the unpinning stage to actually add the | |
6236 | * space back to the block group, otherwise we will leak space. | |
6237 | */ | |
6238 | if (!alloc && cache->cached == BTRFS_CACHE_NO) | |
f6373bf3 | 6239 | cache_block_group(cache, 1); |
0af3d00b | 6240 | |
db94535d CM |
6241 | byte_in_group = bytenr - cache->key.objectid; |
6242 | WARN_ON(byte_in_group > cache->key.offset); | |
9078a3e1 | 6243 | |
25179201 | 6244 | spin_lock(&cache->space_info->lock); |
c286ac48 | 6245 | spin_lock(&cache->lock); |
0af3d00b | 6246 | |
6202df69 | 6247 | if (btrfs_test_opt(info, SPACE_CACHE) && |
0af3d00b JB |
6248 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
6249 | cache->disk_cache_state = BTRFS_DC_CLEAR; | |
6250 | ||
9078a3e1 | 6251 | old_val = btrfs_block_group_used(&cache->item); |
db94535d | 6252 | num_bytes = min(total, cache->key.offset - byte_in_group); |
cd1bc465 | 6253 | if (alloc) { |
db94535d | 6254 | old_val += num_bytes; |
11833d66 YZ |
6255 | btrfs_set_block_group_used(&cache->item, old_val); |
6256 | cache->reserved -= num_bytes; | |
11833d66 | 6257 | cache->space_info->bytes_reserved -= num_bytes; |
b742bb82 YZ |
6258 | cache->space_info->bytes_used += num_bytes; |
6259 | cache->space_info->disk_used += num_bytes * factor; | |
c286ac48 | 6260 | spin_unlock(&cache->lock); |
25179201 | 6261 | spin_unlock(&cache->space_info->lock); |
cd1bc465 | 6262 | } else { |
db94535d | 6263 | old_val -= num_bytes; |
ae0ab003 FM |
6264 | btrfs_set_block_group_used(&cache->item, old_val); |
6265 | cache->pinned += num_bytes; | |
6266 | cache->space_info->bytes_pinned += num_bytes; | |
6267 | cache->space_info->bytes_used -= num_bytes; | |
6268 | cache->space_info->disk_used -= num_bytes * factor; | |
6269 | spin_unlock(&cache->lock); | |
6270 | spin_unlock(&cache->space_info->lock); | |
47ab2a6c | 6271 | |
0b246afa | 6272 | trace_btrfs_space_reservation(info, "pinned", |
c51e7bb1 JB |
6273 | cache->space_info->flags, |
6274 | num_bytes, 1); | |
ae0ab003 FM |
6275 | set_extent_dirty(info->pinned_extents, |
6276 | bytenr, bytenr + num_bytes - 1, | |
6277 | GFP_NOFS | __GFP_NOFAIL); | |
cd1bc465 | 6278 | } |
1bbc621e CM |
6279 | |
6280 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
6281 | if (list_empty(&cache->dirty_list)) { | |
6282 | list_add_tail(&cache->dirty_list, | |
6283 | &trans->transaction->dirty_bgs); | |
6284 | trans->transaction->num_dirty_bgs++; | |
6285 | btrfs_get_block_group(cache); | |
6286 | } | |
6287 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
6288 | ||
036a9348 FM |
6289 | /* |
6290 | * No longer have used bytes in this block group, queue it for | |
6291 | * deletion. We do this after adding the block group to the | |
6292 | * dirty list to avoid races between cleaner kthread and space | |
6293 | * cache writeout. | |
6294 | */ | |
6295 | if (!alloc && old_val == 0) { | |
6296 | spin_lock(&info->unused_bgs_lock); | |
6297 | if (list_empty(&cache->bg_list)) { | |
6298 | btrfs_get_block_group(cache); | |
6299 | list_add_tail(&cache->bg_list, | |
6300 | &info->unused_bgs); | |
6301 | } | |
6302 | spin_unlock(&info->unused_bgs_lock); | |
6303 | } | |
6304 | ||
fa9c0d79 | 6305 | btrfs_put_block_group(cache); |
db94535d CM |
6306 | total -= num_bytes; |
6307 | bytenr += num_bytes; | |
9078a3e1 CM |
6308 | } |
6309 | return 0; | |
6310 | } | |
6324fbf3 | 6311 | |
a061fc8d CM |
6312 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
6313 | { | |
0b246afa | 6314 | struct btrfs_fs_info *fs_info = root->fs_info; |
0f9dd46c | 6315 | struct btrfs_block_group_cache *cache; |
d2fb3437 | 6316 | u64 bytenr; |
0f9dd46c | 6317 | |
0b246afa JM |
6318 | spin_lock(&fs_info->block_group_cache_lock); |
6319 | bytenr = fs_info->first_logical_byte; | |
6320 | spin_unlock(&fs_info->block_group_cache_lock); | |
a1897fdd LB |
6321 | |
6322 | if (bytenr < (u64)-1) | |
6323 | return bytenr; | |
6324 | ||
0b246afa | 6325 | cache = btrfs_lookup_first_block_group(fs_info, search_start); |
0f9dd46c | 6326 | if (!cache) |
a061fc8d | 6327 | return 0; |
0f9dd46c | 6328 | |
d2fb3437 | 6329 | bytenr = cache->key.objectid; |
fa9c0d79 | 6330 | btrfs_put_block_group(cache); |
d2fb3437 YZ |
6331 | |
6332 | return bytenr; | |
a061fc8d CM |
6333 | } |
6334 | ||
f0486c68 YZ |
6335 | static int pin_down_extent(struct btrfs_root *root, |
6336 | struct btrfs_block_group_cache *cache, | |
6337 | u64 bytenr, u64 num_bytes, int reserved) | |
324ae4df | 6338 | { |
0b246afa JM |
6339 | struct btrfs_fs_info *fs_info = cache->fs_info; |
6340 | ||
11833d66 YZ |
6341 | spin_lock(&cache->space_info->lock); |
6342 | spin_lock(&cache->lock); | |
6343 | cache->pinned += num_bytes; | |
6344 | cache->space_info->bytes_pinned += num_bytes; | |
6345 | if (reserved) { | |
6346 | cache->reserved -= num_bytes; | |
6347 | cache->space_info->bytes_reserved -= num_bytes; | |
6348 | } | |
6349 | spin_unlock(&cache->lock); | |
6350 | spin_unlock(&cache->space_info->lock); | |
68b38550 | 6351 | |
0b246afa | 6352 | trace_btrfs_space_reservation(fs_info, "pinned", |
c51e7bb1 | 6353 | cache->space_info->flags, num_bytes, 1); |
0b246afa | 6354 | set_extent_dirty(fs_info->pinned_extents, bytenr, |
f0486c68 YZ |
6355 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); |
6356 | return 0; | |
6357 | } | |
68b38550 | 6358 | |
f0486c68 YZ |
6359 | /* |
6360 | * this function must be called within transaction | |
6361 | */ | |
6362 | int btrfs_pin_extent(struct btrfs_root *root, | |
6363 | u64 bytenr, u64 num_bytes, int reserved) | |
6364 | { | |
0b246afa | 6365 | struct btrfs_fs_info *fs_info = root->fs_info; |
f0486c68 | 6366 | struct btrfs_block_group_cache *cache; |
68b38550 | 6367 | |
0b246afa | 6368 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
79787eaa | 6369 | BUG_ON(!cache); /* Logic error */ |
f0486c68 YZ |
6370 | |
6371 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); | |
6372 | ||
6373 | btrfs_put_block_group(cache); | |
11833d66 YZ |
6374 | return 0; |
6375 | } | |
6376 | ||
f0486c68 | 6377 | /* |
e688b725 CM |
6378 | * this function must be called within transaction |
6379 | */ | |
dcfac415 | 6380 | int btrfs_pin_extent_for_log_replay(struct btrfs_root *root, |
e688b725 CM |
6381 | u64 bytenr, u64 num_bytes) |
6382 | { | |
0b246afa | 6383 | struct btrfs_fs_info *fs_info = root->fs_info; |
e688b725 | 6384 | struct btrfs_block_group_cache *cache; |
b50c6e25 | 6385 | int ret; |
e688b725 | 6386 | |
0b246afa | 6387 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
b50c6e25 JB |
6388 | if (!cache) |
6389 | return -EINVAL; | |
e688b725 CM |
6390 | |
6391 | /* | |
6392 | * pull in the free space cache (if any) so that our pin | |
6393 | * removes the free space from the cache. We have load_only set | |
6394 | * to one because the slow code to read in the free extents does check | |
6395 | * the pinned extents. | |
6396 | */ | |
f6373bf3 | 6397 | cache_block_group(cache, 1); |
e688b725 CM |
6398 | |
6399 | pin_down_extent(root, cache, bytenr, num_bytes, 0); | |
6400 | ||
6401 | /* remove us from the free space cache (if we're there at all) */ | |
b50c6e25 | 6402 | ret = btrfs_remove_free_space(cache, bytenr, num_bytes); |
e688b725 | 6403 | btrfs_put_block_group(cache); |
b50c6e25 | 6404 | return ret; |
e688b725 CM |
6405 | } |
6406 | ||
8c2a1a30 JB |
6407 | static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes) |
6408 | { | |
0b246afa | 6409 | struct btrfs_fs_info *fs_info = root->fs_info; |
8c2a1a30 JB |
6410 | int ret; |
6411 | struct btrfs_block_group_cache *block_group; | |
6412 | struct btrfs_caching_control *caching_ctl; | |
6413 | ||
0b246afa | 6414 | block_group = btrfs_lookup_block_group(fs_info, start); |
8c2a1a30 JB |
6415 | if (!block_group) |
6416 | return -EINVAL; | |
6417 | ||
6418 | cache_block_group(block_group, 0); | |
6419 | caching_ctl = get_caching_control(block_group); | |
6420 | ||
6421 | if (!caching_ctl) { | |
6422 | /* Logic error */ | |
6423 | BUG_ON(!block_group_cache_done(block_group)); | |
6424 | ret = btrfs_remove_free_space(block_group, start, num_bytes); | |
6425 | } else { | |
6426 | mutex_lock(&caching_ctl->mutex); | |
6427 | ||
6428 | if (start >= caching_ctl->progress) { | |
6429 | ret = add_excluded_extent(root, start, num_bytes); | |
6430 | } else if (start + num_bytes <= caching_ctl->progress) { | |
6431 | ret = btrfs_remove_free_space(block_group, | |
6432 | start, num_bytes); | |
6433 | } else { | |
6434 | num_bytes = caching_ctl->progress - start; | |
6435 | ret = btrfs_remove_free_space(block_group, | |
6436 | start, num_bytes); | |
6437 | if (ret) | |
6438 | goto out_lock; | |
6439 | ||
6440 | num_bytes = (start + num_bytes) - | |
6441 | caching_ctl->progress; | |
6442 | start = caching_ctl->progress; | |
6443 | ret = add_excluded_extent(root, start, num_bytes); | |
6444 | } | |
6445 | out_lock: | |
6446 | mutex_unlock(&caching_ctl->mutex); | |
6447 | put_caching_control(caching_ctl); | |
6448 | } | |
6449 | btrfs_put_block_group(block_group); | |
6450 | return ret; | |
6451 | } | |
6452 | ||
6453 | int btrfs_exclude_logged_extents(struct btrfs_root *log, | |
6454 | struct extent_buffer *eb) | |
6455 | { | |
6456 | struct btrfs_file_extent_item *item; | |
6457 | struct btrfs_key key; | |
6458 | int found_type; | |
6459 | int i; | |
6460 | ||
6461 | if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS)) | |
6462 | return 0; | |
6463 | ||
6464 | for (i = 0; i < btrfs_header_nritems(eb); i++) { | |
6465 | btrfs_item_key_to_cpu(eb, &key, i); | |
6466 | if (key.type != BTRFS_EXTENT_DATA_KEY) | |
6467 | continue; | |
6468 | item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item); | |
6469 | found_type = btrfs_file_extent_type(eb, item); | |
6470 | if (found_type == BTRFS_FILE_EXTENT_INLINE) | |
6471 | continue; | |
6472 | if (btrfs_file_extent_disk_bytenr(eb, item) == 0) | |
6473 | continue; | |
6474 | key.objectid = btrfs_file_extent_disk_bytenr(eb, item); | |
6475 | key.offset = btrfs_file_extent_disk_num_bytes(eb, item); | |
6476 | __exclude_logged_extent(log, key.objectid, key.offset); | |
6477 | } | |
6478 | ||
6479 | return 0; | |
6480 | } | |
6481 | ||
9cfa3e34 FM |
6482 | static void |
6483 | btrfs_inc_block_group_reservations(struct btrfs_block_group_cache *bg) | |
6484 | { | |
6485 | atomic_inc(&bg->reservations); | |
6486 | } | |
6487 | ||
6488 | void btrfs_dec_block_group_reservations(struct btrfs_fs_info *fs_info, | |
6489 | const u64 start) | |
6490 | { | |
6491 | struct btrfs_block_group_cache *bg; | |
6492 | ||
6493 | bg = btrfs_lookup_block_group(fs_info, start); | |
6494 | ASSERT(bg); | |
6495 | if (atomic_dec_and_test(&bg->reservations)) | |
6496 | wake_up_atomic_t(&bg->reservations); | |
6497 | btrfs_put_block_group(bg); | |
6498 | } | |
6499 | ||
6500 | static int btrfs_wait_bg_reservations_atomic_t(atomic_t *a) | |
6501 | { | |
6502 | schedule(); | |
6503 | return 0; | |
6504 | } | |
6505 | ||
6506 | void btrfs_wait_block_group_reservations(struct btrfs_block_group_cache *bg) | |
6507 | { | |
6508 | struct btrfs_space_info *space_info = bg->space_info; | |
6509 | ||
6510 | ASSERT(bg->ro); | |
6511 | ||
6512 | if (!(bg->flags & BTRFS_BLOCK_GROUP_DATA)) | |
6513 | return; | |
6514 | ||
6515 | /* | |
6516 | * Our block group is read only but before we set it to read only, | |
6517 | * some task might have had allocated an extent from it already, but it | |
6518 | * has not yet created a respective ordered extent (and added it to a | |
6519 | * root's list of ordered extents). | |
6520 | * Therefore wait for any task currently allocating extents, since the | |
6521 | * block group's reservations counter is incremented while a read lock | |
6522 | * on the groups' semaphore is held and decremented after releasing | |
6523 | * the read access on that semaphore and creating the ordered extent. | |
6524 | */ | |
6525 | down_write(&space_info->groups_sem); | |
6526 | up_write(&space_info->groups_sem); | |
6527 | ||
6528 | wait_on_atomic_t(&bg->reservations, | |
6529 | btrfs_wait_bg_reservations_atomic_t, | |
6530 | TASK_UNINTERRUPTIBLE); | |
6531 | } | |
6532 | ||
fb25e914 | 6533 | /** |
4824f1f4 | 6534 | * btrfs_add_reserved_bytes - update the block_group and space info counters |
fb25e914 | 6535 | * @cache: The cache we are manipulating |
18513091 WX |
6536 | * @ram_bytes: The number of bytes of file content, and will be same to |
6537 | * @num_bytes except for the compress path. | |
fb25e914 | 6538 | * @num_bytes: The number of bytes in question |
e570fd27 | 6539 | * @delalloc: The blocks are allocated for the delalloc write |
fb25e914 | 6540 | * |
745699ef XW |
6541 | * This is called by the allocator when it reserves space. If this is a |
6542 | * reservation and the block group has become read only we cannot make the | |
6543 | * reservation and return -EAGAIN, otherwise this function always succeeds. | |
f0486c68 | 6544 | */ |
4824f1f4 | 6545 | static int btrfs_add_reserved_bytes(struct btrfs_block_group_cache *cache, |
18513091 | 6546 | u64 ram_bytes, u64 num_bytes, int delalloc) |
11833d66 | 6547 | { |
fb25e914 | 6548 | struct btrfs_space_info *space_info = cache->space_info; |
f0486c68 | 6549 | int ret = 0; |
79787eaa | 6550 | |
fb25e914 JB |
6551 | spin_lock(&space_info->lock); |
6552 | spin_lock(&cache->lock); | |
4824f1f4 WX |
6553 | if (cache->ro) { |
6554 | ret = -EAGAIN; | |
fb25e914 | 6555 | } else { |
4824f1f4 WX |
6556 | cache->reserved += num_bytes; |
6557 | space_info->bytes_reserved += num_bytes; | |
e570fd27 | 6558 | |
18513091 WX |
6559 | trace_btrfs_space_reservation(cache->fs_info, |
6560 | "space_info", space_info->flags, | |
6561 | ram_bytes, 0); | |
6562 | space_info->bytes_may_use -= ram_bytes; | |
e570fd27 | 6563 | if (delalloc) |
4824f1f4 | 6564 | cache->delalloc_bytes += num_bytes; |
324ae4df | 6565 | } |
fb25e914 JB |
6566 | spin_unlock(&cache->lock); |
6567 | spin_unlock(&space_info->lock); | |
f0486c68 | 6568 | return ret; |
324ae4df | 6569 | } |
9078a3e1 | 6570 | |
4824f1f4 WX |
6571 | /** |
6572 | * btrfs_free_reserved_bytes - update the block_group and space info counters | |
6573 | * @cache: The cache we are manipulating | |
6574 | * @num_bytes: The number of bytes in question | |
6575 | * @delalloc: The blocks are allocated for the delalloc write | |
6576 | * | |
6577 | * This is called by somebody who is freeing space that was never actually used | |
6578 | * on disk. For example if you reserve some space for a new leaf in transaction | |
6579 | * A and before transaction A commits you free that leaf, you call this with | |
6580 | * reserve set to 0 in order to clear the reservation. | |
6581 | */ | |
6582 | ||
6583 | static int btrfs_free_reserved_bytes(struct btrfs_block_group_cache *cache, | |
6584 | u64 num_bytes, int delalloc) | |
6585 | { | |
6586 | struct btrfs_space_info *space_info = cache->space_info; | |
6587 | int ret = 0; | |
6588 | ||
6589 | spin_lock(&space_info->lock); | |
6590 | spin_lock(&cache->lock); | |
6591 | if (cache->ro) | |
6592 | space_info->bytes_readonly += num_bytes; | |
6593 | cache->reserved -= num_bytes; | |
6594 | space_info->bytes_reserved -= num_bytes; | |
6595 | ||
6596 | if (delalloc) | |
6597 | cache->delalloc_bytes -= num_bytes; | |
6598 | spin_unlock(&cache->lock); | |
6599 | spin_unlock(&space_info->lock); | |
6600 | return ret; | |
6601 | } | |
143bede5 | 6602 | void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
11833d66 | 6603 | struct btrfs_root *root) |
e8569813 | 6604 | { |
e8569813 | 6605 | struct btrfs_fs_info *fs_info = root->fs_info; |
11833d66 YZ |
6606 | struct btrfs_caching_control *next; |
6607 | struct btrfs_caching_control *caching_ctl; | |
6608 | struct btrfs_block_group_cache *cache; | |
e8569813 | 6609 | |
9e351cc8 | 6610 | down_write(&fs_info->commit_root_sem); |
25179201 | 6611 | |
11833d66 YZ |
6612 | list_for_each_entry_safe(caching_ctl, next, |
6613 | &fs_info->caching_block_groups, list) { | |
6614 | cache = caching_ctl->block_group; | |
6615 | if (block_group_cache_done(cache)) { | |
6616 | cache->last_byte_to_unpin = (u64)-1; | |
6617 | list_del_init(&caching_ctl->list); | |
6618 | put_caching_control(caching_ctl); | |
e8569813 | 6619 | } else { |
11833d66 | 6620 | cache->last_byte_to_unpin = caching_ctl->progress; |
e8569813 | 6621 | } |
e8569813 | 6622 | } |
11833d66 YZ |
6623 | |
6624 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) | |
6625 | fs_info->pinned_extents = &fs_info->freed_extents[1]; | |
6626 | else | |
6627 | fs_info->pinned_extents = &fs_info->freed_extents[0]; | |
6628 | ||
9e351cc8 | 6629 | up_write(&fs_info->commit_root_sem); |
8929ecfa YZ |
6630 | |
6631 | update_global_block_rsv(fs_info); | |
e8569813 ZY |
6632 | } |
6633 | ||
c759c4e1 JB |
6634 | /* |
6635 | * Returns the free cluster for the given space info and sets empty_cluster to | |
6636 | * what it should be based on the mount options. | |
6637 | */ | |
6638 | static struct btrfs_free_cluster * | |
6639 | fetch_cluster_info(struct btrfs_root *root, struct btrfs_space_info *space_info, | |
6640 | u64 *empty_cluster) | |
6641 | { | |
0b246afa | 6642 | struct btrfs_fs_info *fs_info = root->fs_info; |
c759c4e1 | 6643 | struct btrfs_free_cluster *ret = NULL; |
0b246afa | 6644 | bool ssd = btrfs_test_opt(fs_info, SSD); |
c759c4e1 JB |
6645 | |
6646 | *empty_cluster = 0; | |
6647 | if (btrfs_mixed_space_info(space_info)) | |
6648 | return ret; | |
6649 | ||
6650 | if (ssd) | |
ee22184b | 6651 | *empty_cluster = SZ_2M; |
c759c4e1 | 6652 | if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) { |
0b246afa | 6653 | ret = &fs_info->meta_alloc_cluster; |
c759c4e1 | 6654 | if (!ssd) |
ee22184b | 6655 | *empty_cluster = SZ_64K; |
c759c4e1 | 6656 | } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) && ssd) { |
0b246afa | 6657 | ret = &fs_info->data_alloc_cluster; |
c759c4e1 JB |
6658 | } |
6659 | ||
6660 | return ret; | |
6661 | } | |
6662 | ||
678886bd FM |
6663 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end, |
6664 | const bool return_free_space) | |
ccd467d6 | 6665 | { |
11833d66 YZ |
6666 | struct btrfs_fs_info *fs_info = root->fs_info; |
6667 | struct btrfs_block_group_cache *cache = NULL; | |
7b398f8e JB |
6668 | struct btrfs_space_info *space_info; |
6669 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; | |
c759c4e1 | 6670 | struct btrfs_free_cluster *cluster = NULL; |
11833d66 | 6671 | u64 len; |
c759c4e1 JB |
6672 | u64 total_unpinned = 0; |
6673 | u64 empty_cluster = 0; | |
7b398f8e | 6674 | bool readonly; |
ccd467d6 | 6675 | |
11833d66 | 6676 | while (start <= end) { |
7b398f8e | 6677 | readonly = false; |
11833d66 YZ |
6678 | if (!cache || |
6679 | start >= cache->key.objectid + cache->key.offset) { | |
6680 | if (cache) | |
6681 | btrfs_put_block_group(cache); | |
c759c4e1 | 6682 | total_unpinned = 0; |
11833d66 | 6683 | cache = btrfs_lookup_block_group(fs_info, start); |
79787eaa | 6684 | BUG_ON(!cache); /* Logic error */ |
c759c4e1 JB |
6685 | |
6686 | cluster = fetch_cluster_info(root, | |
6687 | cache->space_info, | |
6688 | &empty_cluster); | |
6689 | empty_cluster <<= 1; | |
11833d66 YZ |
6690 | } |
6691 | ||
6692 | len = cache->key.objectid + cache->key.offset - start; | |
6693 | len = min(len, end + 1 - start); | |
6694 | ||
6695 | if (start < cache->last_byte_to_unpin) { | |
6696 | len = min(len, cache->last_byte_to_unpin - start); | |
678886bd FM |
6697 | if (return_free_space) |
6698 | btrfs_add_free_space(cache, start, len); | |
11833d66 YZ |
6699 | } |
6700 | ||
f0486c68 | 6701 | start += len; |
c759c4e1 | 6702 | total_unpinned += len; |
7b398f8e | 6703 | space_info = cache->space_info; |
f0486c68 | 6704 | |
c759c4e1 JB |
6705 | /* |
6706 | * If this space cluster has been marked as fragmented and we've | |
6707 | * unpinned enough in this block group to potentially allow a | |
6708 | * cluster to be created inside of it go ahead and clear the | |
6709 | * fragmented check. | |
6710 | */ | |
6711 | if (cluster && cluster->fragmented && | |
6712 | total_unpinned > empty_cluster) { | |
6713 | spin_lock(&cluster->lock); | |
6714 | cluster->fragmented = 0; | |
6715 | spin_unlock(&cluster->lock); | |
6716 | } | |
6717 | ||
7b398f8e | 6718 | spin_lock(&space_info->lock); |
11833d66 YZ |
6719 | spin_lock(&cache->lock); |
6720 | cache->pinned -= len; | |
7b398f8e | 6721 | space_info->bytes_pinned -= len; |
c51e7bb1 JB |
6722 | |
6723 | trace_btrfs_space_reservation(fs_info, "pinned", | |
6724 | space_info->flags, len, 0); | |
4f4db217 | 6725 | space_info->max_extent_size = 0; |
d288db5d | 6726 | percpu_counter_add(&space_info->total_bytes_pinned, -len); |
7b398f8e JB |
6727 | if (cache->ro) { |
6728 | space_info->bytes_readonly += len; | |
6729 | readonly = true; | |
6730 | } | |
11833d66 | 6731 | spin_unlock(&cache->lock); |
957780eb JB |
6732 | if (!readonly && return_free_space && |
6733 | global_rsv->space_info == space_info) { | |
6734 | u64 to_add = len; | |
6735 | WARN_ON(!return_free_space); | |
7b398f8e JB |
6736 | spin_lock(&global_rsv->lock); |
6737 | if (!global_rsv->full) { | |
957780eb JB |
6738 | to_add = min(len, global_rsv->size - |
6739 | global_rsv->reserved); | |
6740 | global_rsv->reserved += to_add; | |
6741 | space_info->bytes_may_use += to_add; | |
7b398f8e JB |
6742 | if (global_rsv->reserved >= global_rsv->size) |
6743 | global_rsv->full = 1; | |
957780eb JB |
6744 | trace_btrfs_space_reservation(fs_info, |
6745 | "space_info", | |
6746 | space_info->flags, | |
6747 | to_add, 1); | |
6748 | len -= to_add; | |
7b398f8e JB |
6749 | } |
6750 | spin_unlock(&global_rsv->lock); | |
957780eb JB |
6751 | /* Add to any tickets we may have */ |
6752 | if (len) | |
6753 | space_info_add_new_bytes(fs_info, space_info, | |
6754 | len); | |
7b398f8e JB |
6755 | } |
6756 | spin_unlock(&space_info->lock); | |
ccd467d6 | 6757 | } |
11833d66 YZ |
6758 | |
6759 | if (cache) | |
6760 | btrfs_put_block_group(cache); | |
ccd467d6 CM |
6761 | return 0; |
6762 | } | |
6763 | ||
6764 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
11833d66 | 6765 | struct btrfs_root *root) |
a28ec197 | 6766 | { |
11833d66 | 6767 | struct btrfs_fs_info *fs_info = root->fs_info; |
e33e17ee JM |
6768 | struct btrfs_block_group_cache *block_group, *tmp; |
6769 | struct list_head *deleted_bgs; | |
11833d66 | 6770 | struct extent_io_tree *unpin; |
1a5bc167 CM |
6771 | u64 start; |
6772 | u64 end; | |
a28ec197 | 6773 | int ret; |
a28ec197 | 6774 | |
11833d66 YZ |
6775 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
6776 | unpin = &fs_info->freed_extents[1]; | |
6777 | else | |
6778 | unpin = &fs_info->freed_extents[0]; | |
6779 | ||
e33e17ee | 6780 | while (!trans->aborted) { |
d4b450cd | 6781 | mutex_lock(&fs_info->unused_bg_unpin_mutex); |
1a5bc167 | 6782 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
e6138876 | 6783 | EXTENT_DIRTY, NULL); |
d4b450cd FM |
6784 | if (ret) { |
6785 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); | |
a28ec197 | 6786 | break; |
d4b450cd | 6787 | } |
1f3c79a2 | 6788 | |
0b246afa | 6789 | if (btrfs_test_opt(fs_info, DISCARD)) |
5378e607 LD |
6790 | ret = btrfs_discard_extent(root, start, |
6791 | end + 1 - start, NULL); | |
1f3c79a2 | 6792 | |
af6f8f60 | 6793 | clear_extent_dirty(unpin, start, end); |
678886bd | 6794 | unpin_extent_range(root, start, end, true); |
d4b450cd | 6795 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
b9473439 | 6796 | cond_resched(); |
a28ec197 | 6797 | } |
817d52f8 | 6798 | |
e33e17ee JM |
6799 | /* |
6800 | * Transaction is finished. We don't need the lock anymore. We | |
6801 | * do need to clean up the block groups in case of a transaction | |
6802 | * abort. | |
6803 | */ | |
6804 | deleted_bgs = &trans->transaction->deleted_bgs; | |
6805 | list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) { | |
6806 | u64 trimmed = 0; | |
6807 | ||
6808 | ret = -EROFS; | |
6809 | if (!trans->aborted) | |
6810 | ret = btrfs_discard_extent(root, | |
6811 | block_group->key.objectid, | |
6812 | block_group->key.offset, | |
6813 | &trimmed); | |
6814 | ||
6815 | list_del_init(&block_group->bg_list); | |
6816 | btrfs_put_block_group_trimming(block_group); | |
6817 | btrfs_put_block_group(block_group); | |
6818 | ||
6819 | if (ret) { | |
6820 | const char *errstr = btrfs_decode_error(ret); | |
6821 | btrfs_warn(fs_info, | |
6822 | "Discard failed while removing blockgroup: errno=%d %s\n", | |
6823 | ret, errstr); | |
6824 | } | |
6825 | } | |
6826 | ||
e20d96d6 CM |
6827 | return 0; |
6828 | } | |
6829 | ||
b150a4f1 JB |
6830 | static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes, |
6831 | u64 owner, u64 root_objectid) | |
6832 | { | |
6833 | struct btrfs_space_info *space_info; | |
6834 | u64 flags; | |
6835 | ||
6836 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
6837 | if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID) | |
6838 | flags = BTRFS_BLOCK_GROUP_SYSTEM; | |
6839 | else | |
6840 | flags = BTRFS_BLOCK_GROUP_METADATA; | |
6841 | } else { | |
6842 | flags = BTRFS_BLOCK_GROUP_DATA; | |
6843 | } | |
6844 | ||
6845 | space_info = __find_space_info(fs_info, flags); | |
6846 | BUG_ON(!space_info); /* Logic bug */ | |
6847 | percpu_counter_add(&space_info->total_bytes_pinned, num_bytes); | |
6848 | } | |
6849 | ||
6850 | ||
5d4f98a2 YZ |
6851 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
6852 | struct btrfs_root *root, | |
c682f9b3 | 6853 | struct btrfs_delayed_ref_node *node, u64 parent, |
5d4f98a2 YZ |
6854 | u64 root_objectid, u64 owner_objectid, |
6855 | u64 owner_offset, int refs_to_drop, | |
c682f9b3 | 6856 | struct btrfs_delayed_extent_op *extent_op) |
a28ec197 | 6857 | { |
e2fa7227 | 6858 | struct btrfs_key key; |
5d4f98a2 | 6859 | struct btrfs_path *path; |
1261ec42 CM |
6860 | struct btrfs_fs_info *info = root->fs_info; |
6861 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 6862 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
6863 | struct btrfs_extent_item *ei; |
6864 | struct btrfs_extent_inline_ref *iref; | |
a28ec197 | 6865 | int ret; |
5d4f98a2 | 6866 | int is_data; |
952fccac CM |
6867 | int extent_slot = 0; |
6868 | int found_extent = 0; | |
6869 | int num_to_del = 1; | |
5d4f98a2 YZ |
6870 | u32 item_size; |
6871 | u64 refs; | |
c682f9b3 QW |
6872 | u64 bytenr = node->bytenr; |
6873 | u64 num_bytes = node->num_bytes; | |
fcebe456 | 6874 | int last_ref = 0; |
0b246afa | 6875 | bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA); |
037e6390 | 6876 | |
5caf2a00 | 6877 | path = btrfs_alloc_path(); |
54aa1f4d CM |
6878 | if (!path) |
6879 | return -ENOMEM; | |
5f26f772 | 6880 | |
e4058b54 | 6881 | path->reada = READA_FORWARD; |
b9473439 | 6882 | path->leave_spinning = 1; |
5d4f98a2 YZ |
6883 | |
6884 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; | |
6885 | BUG_ON(!is_data && refs_to_drop != 1); | |
6886 | ||
3173a18f JB |
6887 | if (is_data) |
6888 | skinny_metadata = 0; | |
6889 | ||
5d4f98a2 YZ |
6890 | ret = lookup_extent_backref(trans, extent_root, path, &iref, |
6891 | bytenr, num_bytes, parent, | |
6892 | root_objectid, owner_objectid, | |
6893 | owner_offset); | |
7bb86316 | 6894 | if (ret == 0) { |
952fccac | 6895 | extent_slot = path->slots[0]; |
5d4f98a2 YZ |
6896 | while (extent_slot >= 0) { |
6897 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
952fccac | 6898 | extent_slot); |
5d4f98a2 | 6899 | if (key.objectid != bytenr) |
952fccac | 6900 | break; |
5d4f98a2 YZ |
6901 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
6902 | key.offset == num_bytes) { | |
952fccac CM |
6903 | found_extent = 1; |
6904 | break; | |
6905 | } | |
3173a18f JB |
6906 | if (key.type == BTRFS_METADATA_ITEM_KEY && |
6907 | key.offset == owner_objectid) { | |
6908 | found_extent = 1; | |
6909 | break; | |
6910 | } | |
952fccac CM |
6911 | if (path->slots[0] - extent_slot > 5) |
6912 | break; | |
5d4f98a2 | 6913 | extent_slot--; |
952fccac | 6914 | } |
5d4f98a2 YZ |
6915 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
6916 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); | |
6917 | if (found_extent && item_size < sizeof(*ei)) | |
6918 | found_extent = 0; | |
6919 | #endif | |
31840ae1 | 6920 | if (!found_extent) { |
5d4f98a2 | 6921 | BUG_ON(iref); |
56bec294 | 6922 | ret = remove_extent_backref(trans, extent_root, path, |
5d4f98a2 | 6923 | NULL, refs_to_drop, |
fcebe456 | 6924 | is_data, &last_ref); |
005d6427 | 6925 | if (ret) { |
66642832 | 6926 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
6927 | goto out; |
6928 | } | |
b3b4aa74 | 6929 | btrfs_release_path(path); |
b9473439 | 6930 | path->leave_spinning = 1; |
5d4f98a2 YZ |
6931 | |
6932 | key.objectid = bytenr; | |
6933 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
6934 | key.offset = num_bytes; | |
6935 | ||
3173a18f JB |
6936 | if (!is_data && skinny_metadata) { |
6937 | key.type = BTRFS_METADATA_ITEM_KEY; | |
6938 | key.offset = owner_objectid; | |
6939 | } | |
6940 | ||
31840ae1 ZY |
6941 | ret = btrfs_search_slot(trans, extent_root, |
6942 | &key, path, -1, 1); | |
3173a18f JB |
6943 | if (ret > 0 && skinny_metadata && path->slots[0]) { |
6944 | /* | |
6945 | * Couldn't find our skinny metadata item, | |
6946 | * see if we have ye olde extent item. | |
6947 | */ | |
6948 | path->slots[0]--; | |
6949 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
6950 | path->slots[0]); | |
6951 | if (key.objectid == bytenr && | |
6952 | key.type == BTRFS_EXTENT_ITEM_KEY && | |
6953 | key.offset == num_bytes) | |
6954 | ret = 0; | |
6955 | } | |
6956 | ||
6957 | if (ret > 0 && skinny_metadata) { | |
6958 | skinny_metadata = false; | |
9ce49a0b | 6959 | key.objectid = bytenr; |
3173a18f JB |
6960 | key.type = BTRFS_EXTENT_ITEM_KEY; |
6961 | key.offset = num_bytes; | |
6962 | btrfs_release_path(path); | |
6963 | ret = btrfs_search_slot(trans, extent_root, | |
6964 | &key, path, -1, 1); | |
6965 | } | |
6966 | ||
f3465ca4 | 6967 | if (ret) { |
5d163e0e JM |
6968 | btrfs_err(info, |
6969 | "umm, got %d back from search, was looking for %llu", | |
6970 | ret, bytenr); | |
b783e62d JB |
6971 | if (ret > 0) |
6972 | btrfs_print_leaf(extent_root, | |
6973 | path->nodes[0]); | |
f3465ca4 | 6974 | } |
005d6427 | 6975 | if (ret < 0) { |
66642832 | 6976 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
6977 | goto out; |
6978 | } | |
31840ae1 ZY |
6979 | extent_slot = path->slots[0]; |
6980 | } | |
fae7f21c | 6981 | } else if (WARN_ON(ret == -ENOENT)) { |
7bb86316 | 6982 | btrfs_print_leaf(extent_root, path->nodes[0]); |
c2cf52eb SK |
6983 | btrfs_err(info, |
6984 | "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu", | |
c1c9ff7c GU |
6985 | bytenr, parent, root_objectid, owner_objectid, |
6986 | owner_offset); | |
66642832 | 6987 | btrfs_abort_transaction(trans, ret); |
c4a050bb | 6988 | goto out; |
79787eaa | 6989 | } else { |
66642832 | 6990 | btrfs_abort_transaction(trans, ret); |
005d6427 | 6991 | goto out; |
7bb86316 | 6992 | } |
5f39d397 CM |
6993 | |
6994 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
6995 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
6996 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
6997 | if (item_size < sizeof(*ei)) { | |
6998 | BUG_ON(found_extent || extent_slot != path->slots[0]); | |
6999 | ret = convert_extent_item_v0(trans, extent_root, path, | |
7000 | owner_objectid, 0); | |
005d6427 | 7001 | if (ret < 0) { |
66642832 | 7002 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7003 | goto out; |
7004 | } | |
5d4f98a2 | 7005 | |
b3b4aa74 | 7006 | btrfs_release_path(path); |
5d4f98a2 YZ |
7007 | path->leave_spinning = 1; |
7008 | ||
7009 | key.objectid = bytenr; | |
7010 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
7011 | key.offset = num_bytes; | |
7012 | ||
7013 | ret = btrfs_search_slot(trans, extent_root, &key, path, | |
7014 | -1, 1); | |
7015 | if (ret) { | |
5d163e0e JM |
7016 | btrfs_err(info, |
7017 | "umm, got %d back from search, was looking for %llu", | |
c1c9ff7c | 7018 | ret, bytenr); |
5d4f98a2 YZ |
7019 | btrfs_print_leaf(extent_root, path->nodes[0]); |
7020 | } | |
005d6427 | 7021 | if (ret < 0) { |
66642832 | 7022 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7023 | goto out; |
7024 | } | |
7025 | ||
5d4f98a2 YZ |
7026 | extent_slot = path->slots[0]; |
7027 | leaf = path->nodes[0]; | |
7028 | item_size = btrfs_item_size_nr(leaf, extent_slot); | |
7029 | } | |
7030 | #endif | |
7031 | BUG_ON(item_size < sizeof(*ei)); | |
952fccac | 7032 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 7033 | struct btrfs_extent_item); |
3173a18f JB |
7034 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID && |
7035 | key.type == BTRFS_EXTENT_ITEM_KEY) { | |
5d4f98a2 YZ |
7036 | struct btrfs_tree_block_info *bi; |
7037 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); | |
7038 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
7039 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); | |
7040 | } | |
56bec294 | 7041 | |
5d4f98a2 | 7042 | refs = btrfs_extent_refs(leaf, ei); |
32b02538 | 7043 | if (refs < refs_to_drop) { |
5d163e0e JM |
7044 | btrfs_err(info, |
7045 | "trying to drop %d refs but we only have %Lu for bytenr %Lu", | |
7046 | refs_to_drop, refs, bytenr); | |
32b02538 | 7047 | ret = -EINVAL; |
66642832 | 7048 | btrfs_abort_transaction(trans, ret); |
32b02538 JB |
7049 | goto out; |
7050 | } | |
56bec294 | 7051 | refs -= refs_to_drop; |
5f39d397 | 7052 | |
5d4f98a2 YZ |
7053 | if (refs > 0) { |
7054 | if (extent_op) | |
7055 | __run_delayed_extent_op(extent_op, leaf, ei); | |
7056 | /* | |
7057 | * In the case of inline back ref, reference count will | |
7058 | * be updated by remove_extent_backref | |
952fccac | 7059 | */ |
5d4f98a2 YZ |
7060 | if (iref) { |
7061 | BUG_ON(!found_extent); | |
7062 | } else { | |
7063 | btrfs_set_extent_refs(leaf, ei, refs); | |
7064 | btrfs_mark_buffer_dirty(leaf); | |
7065 | } | |
7066 | if (found_extent) { | |
7067 | ret = remove_extent_backref(trans, extent_root, path, | |
7068 | iref, refs_to_drop, | |
fcebe456 | 7069 | is_data, &last_ref); |
005d6427 | 7070 | if (ret) { |
66642832 | 7071 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7072 | goto out; |
7073 | } | |
952fccac | 7074 | } |
0b246afa | 7075 | add_pinned_bytes(info, -num_bytes, owner_objectid, |
b150a4f1 | 7076 | root_objectid); |
5d4f98a2 | 7077 | } else { |
5d4f98a2 YZ |
7078 | if (found_extent) { |
7079 | BUG_ON(is_data && refs_to_drop != | |
9ed0dea0 | 7080 | extent_data_ref_count(path, iref)); |
5d4f98a2 YZ |
7081 | if (iref) { |
7082 | BUG_ON(path->slots[0] != extent_slot); | |
7083 | } else { | |
7084 | BUG_ON(path->slots[0] != extent_slot + 1); | |
7085 | path->slots[0] = extent_slot; | |
7086 | num_to_del = 2; | |
7087 | } | |
78fae27e | 7088 | } |
b9473439 | 7089 | |
fcebe456 | 7090 | last_ref = 1; |
952fccac CM |
7091 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
7092 | num_to_del); | |
005d6427 | 7093 | if (ret) { |
66642832 | 7094 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7095 | goto out; |
7096 | } | |
b3b4aa74 | 7097 | btrfs_release_path(path); |
21af804c | 7098 | |
5d4f98a2 | 7099 | if (is_data) { |
5b4aacef | 7100 | ret = btrfs_del_csums(trans, info, bytenr, num_bytes); |
005d6427 | 7101 | if (ret) { |
66642832 | 7102 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7103 | goto out; |
7104 | } | |
459931ec CM |
7105 | } |
7106 | ||
0b246afa | 7107 | ret = add_to_free_space_tree(trans, info, bytenr, num_bytes); |
1e144fb8 | 7108 | if (ret) { |
66642832 | 7109 | btrfs_abort_transaction(trans, ret); |
1e144fb8 OS |
7110 | goto out; |
7111 | } | |
7112 | ||
0b246afa | 7113 | ret = update_block_group(trans, info, bytenr, num_bytes, 0); |
005d6427 | 7114 | if (ret) { |
66642832 | 7115 | btrfs_abort_transaction(trans, ret); |
005d6427 DS |
7116 | goto out; |
7117 | } | |
a28ec197 | 7118 | } |
fcebe456 JB |
7119 | btrfs_release_path(path); |
7120 | ||
79787eaa | 7121 | out: |
5caf2a00 | 7122 | btrfs_free_path(path); |
a28ec197 CM |
7123 | return ret; |
7124 | } | |
7125 | ||
1887be66 | 7126 | /* |
f0486c68 | 7127 | * when we free an block, it is possible (and likely) that we free the last |
1887be66 CM |
7128 | * delayed ref for that extent as well. This searches the delayed ref tree for |
7129 | * a given extent, and if there are no other delayed refs to be processed, it | |
7130 | * removes it from the tree. | |
7131 | */ | |
7132 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, | |
7133 | struct btrfs_root *root, u64 bytenr) | |
7134 | { | |
7135 | struct btrfs_delayed_ref_head *head; | |
7136 | struct btrfs_delayed_ref_root *delayed_refs; | |
f0486c68 | 7137 | int ret = 0; |
1887be66 CM |
7138 | |
7139 | delayed_refs = &trans->transaction->delayed_refs; | |
7140 | spin_lock(&delayed_refs->lock); | |
7141 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
7142 | if (!head) | |
cf93da7b | 7143 | goto out_delayed_unlock; |
1887be66 | 7144 | |
d7df2c79 | 7145 | spin_lock(&head->lock); |
c6fc2454 | 7146 | if (!list_empty(&head->ref_list)) |
1887be66 CM |
7147 | goto out; |
7148 | ||
5d4f98a2 YZ |
7149 | if (head->extent_op) { |
7150 | if (!head->must_insert_reserved) | |
7151 | goto out; | |
78a6184a | 7152 | btrfs_free_delayed_extent_op(head->extent_op); |
5d4f98a2 YZ |
7153 | head->extent_op = NULL; |
7154 | } | |
7155 | ||
1887be66 CM |
7156 | /* |
7157 | * waiting for the lock here would deadlock. If someone else has it | |
7158 | * locked they are already in the process of dropping it anyway | |
7159 | */ | |
7160 | if (!mutex_trylock(&head->mutex)) | |
7161 | goto out; | |
7162 | ||
7163 | /* | |
7164 | * at this point we have a head with no other entries. Go | |
7165 | * ahead and process it. | |
7166 | */ | |
7167 | head->node.in_tree = 0; | |
c46effa6 | 7168 | rb_erase(&head->href_node, &delayed_refs->href_root); |
c3e69d58 | 7169 | |
d7df2c79 | 7170 | atomic_dec(&delayed_refs->num_entries); |
1887be66 CM |
7171 | |
7172 | /* | |
7173 | * we don't take a ref on the node because we're removing it from the | |
7174 | * tree, so we just steal the ref the tree was holding. | |
7175 | */ | |
c3e69d58 | 7176 | delayed_refs->num_heads--; |
d7df2c79 | 7177 | if (head->processing == 0) |
c3e69d58 | 7178 | delayed_refs->num_heads_ready--; |
d7df2c79 JB |
7179 | head->processing = 0; |
7180 | spin_unlock(&head->lock); | |
1887be66 CM |
7181 | spin_unlock(&delayed_refs->lock); |
7182 | ||
f0486c68 YZ |
7183 | BUG_ON(head->extent_op); |
7184 | if (head->must_insert_reserved) | |
7185 | ret = 1; | |
7186 | ||
7187 | mutex_unlock(&head->mutex); | |
1887be66 | 7188 | btrfs_put_delayed_ref(&head->node); |
f0486c68 | 7189 | return ret; |
1887be66 | 7190 | out: |
d7df2c79 | 7191 | spin_unlock(&head->lock); |
cf93da7b CM |
7192 | |
7193 | out_delayed_unlock: | |
1887be66 CM |
7194 | spin_unlock(&delayed_refs->lock); |
7195 | return 0; | |
7196 | } | |
7197 | ||
f0486c68 YZ |
7198 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
7199 | struct btrfs_root *root, | |
7200 | struct extent_buffer *buf, | |
5581a51a | 7201 | u64 parent, int last_ref) |
f0486c68 | 7202 | { |
0b246afa | 7203 | struct btrfs_fs_info *fs_info = root->fs_info; |
b150a4f1 | 7204 | int pin = 1; |
f0486c68 YZ |
7205 | int ret; |
7206 | ||
7207 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | |
0b246afa JM |
7208 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
7209 | buf->start, buf->len, | |
7210 | parent, | |
7211 | root->root_key.objectid, | |
7212 | btrfs_header_level(buf), | |
7213 | BTRFS_DROP_DELAYED_REF, NULL); | |
79787eaa | 7214 | BUG_ON(ret); /* -ENOMEM */ |
f0486c68 YZ |
7215 | } |
7216 | ||
7217 | if (!last_ref) | |
7218 | return; | |
7219 | ||
f0486c68 | 7220 | if (btrfs_header_generation(buf) == trans->transid) { |
6219872d FM |
7221 | struct btrfs_block_group_cache *cache; |
7222 | ||
f0486c68 YZ |
7223 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { |
7224 | ret = check_ref_cleanup(trans, root, buf->start); | |
7225 | if (!ret) | |
37be25bc | 7226 | goto out; |
f0486c68 YZ |
7227 | } |
7228 | ||
0b246afa | 7229 | cache = btrfs_lookup_block_group(fs_info, buf->start); |
6219872d | 7230 | |
f0486c68 YZ |
7231 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { |
7232 | pin_down_extent(root, cache, buf->start, buf->len, 1); | |
6219872d | 7233 | btrfs_put_block_group(cache); |
37be25bc | 7234 | goto out; |
f0486c68 YZ |
7235 | } |
7236 | ||
7237 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); | |
7238 | ||
7239 | btrfs_add_free_space(cache, buf->start, buf->len); | |
4824f1f4 | 7240 | btrfs_free_reserved_bytes(cache, buf->len, 0); |
6219872d | 7241 | btrfs_put_block_group(cache); |
71ff6437 | 7242 | trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len); |
b150a4f1 | 7243 | pin = 0; |
f0486c68 YZ |
7244 | } |
7245 | out: | |
b150a4f1 | 7246 | if (pin) |
0b246afa | 7247 | add_pinned_bytes(fs_info, buf->len, btrfs_header_level(buf), |
b150a4f1 JB |
7248 | root->root_key.objectid); |
7249 | ||
a826d6dc JB |
7250 | /* |
7251 | * Deleting the buffer, clear the corrupt flag since it doesn't matter | |
7252 | * anymore. | |
7253 | */ | |
7254 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); | |
f0486c68 YZ |
7255 | } |
7256 | ||
79787eaa | 7257 | /* Can return -ENOMEM */ |
66d7e7f0 AJ |
7258 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
7259 | u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid, | |
b06c4bf5 | 7260 | u64 owner, u64 offset) |
925baedd CM |
7261 | { |
7262 | int ret; | |
66d7e7f0 | 7263 | struct btrfs_fs_info *fs_info = root->fs_info; |
925baedd | 7264 | |
f5ee5c9a | 7265 | if (btrfs_is_testing(fs_info)) |
faa2dbf0 | 7266 | return 0; |
fccb84c9 | 7267 | |
0b246afa | 7268 | add_pinned_bytes(fs_info, num_bytes, owner, root_objectid); |
b150a4f1 | 7269 | |
56bec294 CM |
7270 | /* |
7271 | * tree log blocks never actually go into the extent allocation | |
7272 | * tree, just update pinning info and exit early. | |
56bec294 | 7273 | */ |
5d4f98a2 YZ |
7274 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
7275 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); | |
b9473439 | 7276 | /* unlocks the pinned mutex */ |
11833d66 | 7277 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
56bec294 | 7278 | ret = 0; |
5d4f98a2 | 7279 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
66d7e7f0 AJ |
7280 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr, |
7281 | num_bytes, | |
5d4f98a2 | 7282 | parent, root_objectid, (int)owner, |
b06c4bf5 | 7283 | BTRFS_DROP_DELAYED_REF, NULL); |
5d4f98a2 | 7284 | } else { |
66d7e7f0 AJ |
7285 | ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr, |
7286 | num_bytes, | |
7287 | parent, root_objectid, owner, | |
5846a3c2 QW |
7288 | offset, 0, |
7289 | BTRFS_DROP_DELAYED_REF, NULL); | |
56bec294 | 7290 | } |
925baedd CM |
7291 | return ret; |
7292 | } | |
7293 | ||
817d52f8 JB |
7294 | /* |
7295 | * when we wait for progress in the block group caching, its because | |
7296 | * our allocation attempt failed at least once. So, we must sleep | |
7297 | * and let some progress happen before we try again. | |
7298 | * | |
7299 | * This function will sleep at least once waiting for new free space to | |
7300 | * show up, and then it will check the block group free space numbers | |
7301 | * for our min num_bytes. Another option is to have it go ahead | |
7302 | * and look in the rbtree for a free extent of a given size, but this | |
7303 | * is a good start. | |
36cce922 JB |
7304 | * |
7305 | * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using | |
7306 | * any of the information in this block group. | |
817d52f8 | 7307 | */ |
36cce922 | 7308 | static noinline void |
817d52f8 JB |
7309 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, |
7310 | u64 num_bytes) | |
7311 | { | |
11833d66 | 7312 | struct btrfs_caching_control *caching_ctl; |
817d52f8 | 7313 | |
11833d66 YZ |
7314 | caching_ctl = get_caching_control(cache); |
7315 | if (!caching_ctl) | |
36cce922 | 7316 | return; |
817d52f8 | 7317 | |
11833d66 | 7318 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
34d52cb6 | 7319 | (cache->free_space_ctl->free_space >= num_bytes)); |
11833d66 YZ |
7320 | |
7321 | put_caching_control(caching_ctl); | |
11833d66 YZ |
7322 | } |
7323 | ||
7324 | static noinline int | |
7325 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) | |
7326 | { | |
7327 | struct btrfs_caching_control *caching_ctl; | |
36cce922 | 7328 | int ret = 0; |
11833d66 YZ |
7329 | |
7330 | caching_ctl = get_caching_control(cache); | |
7331 | if (!caching_ctl) | |
36cce922 | 7332 | return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0; |
11833d66 YZ |
7333 | |
7334 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); | |
36cce922 JB |
7335 | if (cache->cached == BTRFS_CACHE_ERROR) |
7336 | ret = -EIO; | |
11833d66 | 7337 | put_caching_control(caching_ctl); |
36cce922 | 7338 | return ret; |
817d52f8 JB |
7339 | } |
7340 | ||
31e50229 | 7341 | int __get_raid_index(u64 flags) |
b742bb82 | 7342 | { |
7738a53a | 7343 | if (flags & BTRFS_BLOCK_GROUP_RAID10) |
e6ec716f | 7344 | return BTRFS_RAID_RAID10; |
7738a53a | 7345 | else if (flags & BTRFS_BLOCK_GROUP_RAID1) |
e6ec716f | 7346 | return BTRFS_RAID_RAID1; |
7738a53a | 7347 | else if (flags & BTRFS_BLOCK_GROUP_DUP) |
e6ec716f | 7348 | return BTRFS_RAID_DUP; |
7738a53a | 7349 | else if (flags & BTRFS_BLOCK_GROUP_RAID0) |
e6ec716f | 7350 | return BTRFS_RAID_RAID0; |
53b381b3 | 7351 | else if (flags & BTRFS_BLOCK_GROUP_RAID5) |
e942f883 | 7352 | return BTRFS_RAID_RAID5; |
53b381b3 | 7353 | else if (flags & BTRFS_BLOCK_GROUP_RAID6) |
e942f883 | 7354 | return BTRFS_RAID_RAID6; |
7738a53a | 7355 | |
e942f883 | 7356 | return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */ |
b742bb82 YZ |
7357 | } |
7358 | ||
6ab0a202 | 7359 | int get_block_group_index(struct btrfs_block_group_cache *cache) |
7738a53a | 7360 | { |
31e50229 | 7361 | return __get_raid_index(cache->flags); |
7738a53a ID |
7362 | } |
7363 | ||
6ab0a202 JM |
7364 | static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = { |
7365 | [BTRFS_RAID_RAID10] = "raid10", | |
7366 | [BTRFS_RAID_RAID1] = "raid1", | |
7367 | [BTRFS_RAID_DUP] = "dup", | |
7368 | [BTRFS_RAID_RAID0] = "raid0", | |
7369 | [BTRFS_RAID_SINGLE] = "single", | |
7370 | [BTRFS_RAID_RAID5] = "raid5", | |
7371 | [BTRFS_RAID_RAID6] = "raid6", | |
7372 | }; | |
7373 | ||
1b8e5df6 | 7374 | static const char *get_raid_name(enum btrfs_raid_types type) |
6ab0a202 JM |
7375 | { |
7376 | if (type >= BTRFS_NR_RAID_TYPES) | |
7377 | return NULL; | |
7378 | ||
7379 | return btrfs_raid_type_names[type]; | |
7380 | } | |
7381 | ||
817d52f8 | 7382 | enum btrfs_loop_type { |
285ff5af JB |
7383 | LOOP_CACHING_NOWAIT = 0, |
7384 | LOOP_CACHING_WAIT = 1, | |
7385 | LOOP_ALLOC_CHUNK = 2, | |
7386 | LOOP_NO_EMPTY_SIZE = 3, | |
817d52f8 JB |
7387 | }; |
7388 | ||
e570fd27 MX |
7389 | static inline void |
7390 | btrfs_lock_block_group(struct btrfs_block_group_cache *cache, | |
7391 | int delalloc) | |
7392 | { | |
7393 | if (delalloc) | |
7394 | down_read(&cache->data_rwsem); | |
7395 | } | |
7396 | ||
7397 | static inline void | |
7398 | btrfs_grab_block_group(struct btrfs_block_group_cache *cache, | |
7399 | int delalloc) | |
7400 | { | |
7401 | btrfs_get_block_group(cache); | |
7402 | if (delalloc) | |
7403 | down_read(&cache->data_rwsem); | |
7404 | } | |
7405 | ||
7406 | static struct btrfs_block_group_cache * | |
7407 | btrfs_lock_cluster(struct btrfs_block_group_cache *block_group, | |
7408 | struct btrfs_free_cluster *cluster, | |
7409 | int delalloc) | |
7410 | { | |
89771cc9 | 7411 | struct btrfs_block_group_cache *used_bg = NULL; |
6719afdc | 7412 | |
e570fd27 | 7413 | spin_lock(&cluster->refill_lock); |
6719afdc GU |
7414 | while (1) { |
7415 | used_bg = cluster->block_group; | |
7416 | if (!used_bg) | |
7417 | return NULL; | |
7418 | ||
7419 | if (used_bg == block_group) | |
e570fd27 MX |
7420 | return used_bg; |
7421 | ||
6719afdc | 7422 | btrfs_get_block_group(used_bg); |
e570fd27 | 7423 | |
6719afdc GU |
7424 | if (!delalloc) |
7425 | return used_bg; | |
e570fd27 | 7426 | |
6719afdc GU |
7427 | if (down_read_trylock(&used_bg->data_rwsem)) |
7428 | return used_bg; | |
e570fd27 | 7429 | |
6719afdc | 7430 | spin_unlock(&cluster->refill_lock); |
e570fd27 | 7431 | |
6719afdc | 7432 | down_read(&used_bg->data_rwsem); |
e570fd27 | 7433 | |
6719afdc GU |
7434 | spin_lock(&cluster->refill_lock); |
7435 | if (used_bg == cluster->block_group) | |
7436 | return used_bg; | |
e570fd27 | 7437 | |
6719afdc GU |
7438 | up_read(&used_bg->data_rwsem); |
7439 | btrfs_put_block_group(used_bg); | |
7440 | } | |
e570fd27 MX |
7441 | } |
7442 | ||
7443 | static inline void | |
7444 | btrfs_release_block_group(struct btrfs_block_group_cache *cache, | |
7445 | int delalloc) | |
7446 | { | |
7447 | if (delalloc) | |
7448 | up_read(&cache->data_rwsem); | |
7449 | btrfs_put_block_group(cache); | |
7450 | } | |
7451 | ||
fec577fb CM |
7452 | /* |
7453 | * walks the btree of allocated extents and find a hole of a given size. | |
7454 | * The key ins is changed to record the hole: | |
a4820398 | 7455 | * ins->objectid == start position |
62e2749e | 7456 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
a4820398 | 7457 | * ins->offset == the size of the hole. |
fec577fb | 7458 | * Any available blocks before search_start are skipped. |
a4820398 MX |
7459 | * |
7460 | * If there is no suitable free space, we will record the max size of | |
7461 | * the free space extent currently. | |
fec577fb | 7462 | */ |
00361589 | 7463 | static noinline int find_free_extent(struct btrfs_root *orig_root, |
18513091 WX |
7464 | u64 ram_bytes, u64 num_bytes, u64 empty_size, |
7465 | u64 hint_byte, struct btrfs_key *ins, | |
7466 | u64 flags, int delalloc) | |
fec577fb | 7467 | { |
0b246afa | 7468 | struct btrfs_fs_info *fs_info = orig_root->fs_info; |
80eb234a | 7469 | int ret = 0; |
0b246afa | 7470 | struct btrfs_root *root = fs_info->extent_root; |
fa9c0d79 | 7471 | struct btrfs_free_cluster *last_ptr = NULL; |
80eb234a | 7472 | struct btrfs_block_group_cache *block_group = NULL; |
81c9ad23 | 7473 | u64 search_start = 0; |
a4820398 | 7474 | u64 max_extent_size = 0; |
c759c4e1 | 7475 | u64 empty_cluster = 0; |
80eb234a | 7476 | struct btrfs_space_info *space_info; |
fa9c0d79 | 7477 | int loop = 0; |
b6919a58 | 7478 | int index = __get_raid_index(flags); |
0a24325e | 7479 | bool failed_cluster_refill = false; |
1cdda9b8 | 7480 | bool failed_alloc = false; |
67377734 | 7481 | bool use_cluster = true; |
60d2adbb | 7482 | bool have_caching_bg = false; |
13a0db5a | 7483 | bool orig_have_caching_bg = false; |
a5e681d9 | 7484 | bool full_search = false; |
fec577fb | 7485 | |
0b246afa | 7486 | WARN_ON(num_bytes < fs_info->sectorsize); |
962a298f | 7487 | ins->type = BTRFS_EXTENT_ITEM_KEY; |
80eb234a JB |
7488 | ins->objectid = 0; |
7489 | ins->offset = 0; | |
b1a4d965 | 7490 | |
71ff6437 | 7491 | trace_find_free_extent(fs_info, num_bytes, empty_size, flags); |
3f7de037 | 7492 | |
0b246afa | 7493 | space_info = __find_space_info(fs_info, flags); |
1b1d1f66 | 7494 | if (!space_info) { |
0b246afa | 7495 | btrfs_err(fs_info, "No space info for %llu", flags); |
1b1d1f66 JB |
7496 | return -ENOSPC; |
7497 | } | |
2552d17e | 7498 | |
67377734 | 7499 | /* |
4f4db217 JB |
7500 | * If our free space is heavily fragmented we may not be able to make |
7501 | * big contiguous allocations, so instead of doing the expensive search | |
7502 | * for free space, simply return ENOSPC with our max_extent_size so we | |
7503 | * can go ahead and search for a more manageable chunk. | |
7504 | * | |
7505 | * If our max_extent_size is large enough for our allocation simply | |
7506 | * disable clustering since we will likely not be able to find enough | |
7507 | * space to create a cluster and induce latency trying. | |
67377734 | 7508 | */ |
4f4db217 JB |
7509 | if (unlikely(space_info->max_extent_size)) { |
7510 | spin_lock(&space_info->lock); | |
7511 | if (space_info->max_extent_size && | |
7512 | num_bytes > space_info->max_extent_size) { | |
7513 | ins->offset = space_info->max_extent_size; | |
7514 | spin_unlock(&space_info->lock); | |
7515 | return -ENOSPC; | |
7516 | } else if (space_info->max_extent_size) { | |
7517 | use_cluster = false; | |
7518 | } | |
7519 | spin_unlock(&space_info->lock); | |
fa9c0d79 | 7520 | } |
0f9dd46c | 7521 | |
c759c4e1 | 7522 | last_ptr = fetch_cluster_info(orig_root, space_info, &empty_cluster); |
239b14b3 | 7523 | if (last_ptr) { |
fa9c0d79 CM |
7524 | spin_lock(&last_ptr->lock); |
7525 | if (last_ptr->block_group) | |
7526 | hint_byte = last_ptr->window_start; | |
c759c4e1 JB |
7527 | if (last_ptr->fragmented) { |
7528 | /* | |
7529 | * We still set window_start so we can keep track of the | |
7530 | * last place we found an allocation to try and save | |
7531 | * some time. | |
7532 | */ | |
7533 | hint_byte = last_ptr->window_start; | |
7534 | use_cluster = false; | |
7535 | } | |
fa9c0d79 | 7536 | spin_unlock(&last_ptr->lock); |
239b14b3 | 7537 | } |
fa9c0d79 | 7538 | |
a061fc8d | 7539 | search_start = max(search_start, first_logical_byte(root, 0)); |
239b14b3 | 7540 | search_start = max(search_start, hint_byte); |
2552d17e | 7541 | if (search_start == hint_byte) { |
0b246afa | 7542 | block_group = btrfs_lookup_block_group(fs_info, search_start); |
817d52f8 JB |
7543 | /* |
7544 | * we don't want to use the block group if it doesn't match our | |
7545 | * allocation bits, or if its not cached. | |
ccf0e725 JB |
7546 | * |
7547 | * However if we are re-searching with an ideal block group | |
7548 | * picked out then we don't care that the block group is cached. | |
817d52f8 | 7549 | */ |
b6919a58 | 7550 | if (block_group && block_group_bits(block_group, flags) && |
285ff5af | 7551 | block_group->cached != BTRFS_CACHE_NO) { |
2552d17e | 7552 | down_read(&space_info->groups_sem); |
44fb5511 CM |
7553 | if (list_empty(&block_group->list) || |
7554 | block_group->ro) { | |
7555 | /* | |
7556 | * someone is removing this block group, | |
7557 | * we can't jump into the have_block_group | |
7558 | * target because our list pointers are not | |
7559 | * valid | |
7560 | */ | |
7561 | btrfs_put_block_group(block_group); | |
7562 | up_read(&space_info->groups_sem); | |
ccf0e725 | 7563 | } else { |
b742bb82 | 7564 | index = get_block_group_index(block_group); |
e570fd27 | 7565 | btrfs_lock_block_group(block_group, delalloc); |
44fb5511 | 7566 | goto have_block_group; |
ccf0e725 | 7567 | } |
2552d17e | 7568 | } else if (block_group) { |
fa9c0d79 | 7569 | btrfs_put_block_group(block_group); |
2552d17e | 7570 | } |
42e70e7a | 7571 | } |
2552d17e | 7572 | search: |
60d2adbb | 7573 | have_caching_bg = false; |
a5e681d9 JB |
7574 | if (index == 0 || index == __get_raid_index(flags)) |
7575 | full_search = true; | |
80eb234a | 7576 | down_read(&space_info->groups_sem); |
b742bb82 YZ |
7577 | list_for_each_entry(block_group, &space_info->block_groups[index], |
7578 | list) { | |
6226cb0a | 7579 | u64 offset; |
817d52f8 | 7580 | int cached; |
8a1413a2 | 7581 | |
e570fd27 | 7582 | btrfs_grab_block_group(block_group, delalloc); |
2552d17e | 7583 | search_start = block_group->key.objectid; |
42e70e7a | 7584 | |
83a50de9 CM |
7585 | /* |
7586 | * this can happen if we end up cycling through all the | |
7587 | * raid types, but we want to make sure we only allocate | |
7588 | * for the proper type. | |
7589 | */ | |
b6919a58 | 7590 | if (!block_group_bits(block_group, flags)) { |
83a50de9 CM |
7591 | u64 extra = BTRFS_BLOCK_GROUP_DUP | |
7592 | BTRFS_BLOCK_GROUP_RAID1 | | |
53b381b3 DW |
7593 | BTRFS_BLOCK_GROUP_RAID5 | |
7594 | BTRFS_BLOCK_GROUP_RAID6 | | |
83a50de9 CM |
7595 | BTRFS_BLOCK_GROUP_RAID10; |
7596 | ||
7597 | /* | |
7598 | * if they asked for extra copies and this block group | |
7599 | * doesn't provide them, bail. This does allow us to | |
7600 | * fill raid0 from raid1. | |
7601 | */ | |
b6919a58 | 7602 | if ((flags & extra) && !(block_group->flags & extra)) |
83a50de9 CM |
7603 | goto loop; |
7604 | } | |
7605 | ||
2552d17e | 7606 | have_block_group: |
291c7d2f JB |
7607 | cached = block_group_cache_done(block_group); |
7608 | if (unlikely(!cached)) { | |
a5e681d9 | 7609 | have_caching_bg = true; |
f6373bf3 | 7610 | ret = cache_block_group(block_group, 0); |
1d4284bd CM |
7611 | BUG_ON(ret < 0); |
7612 | ret = 0; | |
817d52f8 JB |
7613 | } |
7614 | ||
36cce922 JB |
7615 | if (unlikely(block_group->cached == BTRFS_CACHE_ERROR)) |
7616 | goto loop; | |
ea6a478e | 7617 | if (unlikely(block_group->ro)) |
2552d17e | 7618 | goto loop; |
0f9dd46c | 7619 | |
0a24325e | 7620 | /* |
062c05c4 AO |
7621 | * Ok we want to try and use the cluster allocator, so |
7622 | * lets look there | |
0a24325e | 7623 | */ |
c759c4e1 | 7624 | if (last_ptr && use_cluster) { |
215a63d1 | 7625 | struct btrfs_block_group_cache *used_block_group; |
8de972b4 | 7626 | unsigned long aligned_cluster; |
fa9c0d79 CM |
7627 | /* |
7628 | * the refill lock keeps out other | |
7629 | * people trying to start a new cluster | |
7630 | */ | |
e570fd27 MX |
7631 | used_block_group = btrfs_lock_cluster(block_group, |
7632 | last_ptr, | |
7633 | delalloc); | |
7634 | if (!used_block_group) | |
44fb5511 | 7635 | goto refill_cluster; |
274bd4fb | 7636 | |
e570fd27 MX |
7637 | if (used_block_group != block_group && |
7638 | (used_block_group->ro || | |
7639 | !block_group_bits(used_block_group, flags))) | |
7640 | goto release_cluster; | |
44fb5511 | 7641 | |
274bd4fb | 7642 | offset = btrfs_alloc_from_cluster(used_block_group, |
a4820398 MX |
7643 | last_ptr, |
7644 | num_bytes, | |
7645 | used_block_group->key.objectid, | |
7646 | &max_extent_size); | |
fa9c0d79 CM |
7647 | if (offset) { |
7648 | /* we have a block, we're done */ | |
7649 | spin_unlock(&last_ptr->refill_lock); | |
71ff6437 | 7650 | trace_btrfs_reserve_extent_cluster(fs_info, |
89d4346a MX |
7651 | used_block_group, |
7652 | search_start, num_bytes); | |
215a63d1 | 7653 | if (used_block_group != block_group) { |
e570fd27 MX |
7654 | btrfs_release_block_group(block_group, |
7655 | delalloc); | |
215a63d1 MX |
7656 | block_group = used_block_group; |
7657 | } | |
fa9c0d79 CM |
7658 | goto checks; |
7659 | } | |
7660 | ||
274bd4fb | 7661 | WARN_ON(last_ptr->block_group != used_block_group); |
e570fd27 | 7662 | release_cluster: |
062c05c4 AO |
7663 | /* If we are on LOOP_NO_EMPTY_SIZE, we can't |
7664 | * set up a new clusters, so lets just skip it | |
7665 | * and let the allocator find whatever block | |
7666 | * it can find. If we reach this point, we | |
7667 | * will have tried the cluster allocator | |
7668 | * plenty of times and not have found | |
7669 | * anything, so we are likely way too | |
7670 | * fragmented for the clustering stuff to find | |
a5f6f719 AO |
7671 | * anything. |
7672 | * | |
7673 | * However, if the cluster is taken from the | |
7674 | * current block group, release the cluster | |
7675 | * first, so that we stand a better chance of | |
7676 | * succeeding in the unclustered | |
7677 | * allocation. */ | |
7678 | if (loop >= LOOP_NO_EMPTY_SIZE && | |
e570fd27 | 7679 | used_block_group != block_group) { |
062c05c4 | 7680 | spin_unlock(&last_ptr->refill_lock); |
e570fd27 MX |
7681 | btrfs_release_block_group(used_block_group, |
7682 | delalloc); | |
062c05c4 AO |
7683 | goto unclustered_alloc; |
7684 | } | |
7685 | ||
fa9c0d79 CM |
7686 | /* |
7687 | * this cluster didn't work out, free it and | |
7688 | * start over | |
7689 | */ | |
7690 | btrfs_return_cluster_to_free_space(NULL, last_ptr); | |
7691 | ||
e570fd27 MX |
7692 | if (used_block_group != block_group) |
7693 | btrfs_release_block_group(used_block_group, | |
7694 | delalloc); | |
7695 | refill_cluster: | |
a5f6f719 AO |
7696 | if (loop >= LOOP_NO_EMPTY_SIZE) { |
7697 | spin_unlock(&last_ptr->refill_lock); | |
7698 | goto unclustered_alloc; | |
7699 | } | |
7700 | ||
8de972b4 CM |
7701 | aligned_cluster = max_t(unsigned long, |
7702 | empty_cluster + empty_size, | |
7703 | block_group->full_stripe_len); | |
7704 | ||
fa9c0d79 | 7705 | /* allocate a cluster in this block group */ |
00361589 JB |
7706 | ret = btrfs_find_space_cluster(root, block_group, |
7707 | last_ptr, search_start, | |
7708 | num_bytes, | |
7709 | aligned_cluster); | |
fa9c0d79 CM |
7710 | if (ret == 0) { |
7711 | /* | |
7712 | * now pull our allocation out of this | |
7713 | * cluster | |
7714 | */ | |
7715 | offset = btrfs_alloc_from_cluster(block_group, | |
a4820398 MX |
7716 | last_ptr, |
7717 | num_bytes, | |
7718 | search_start, | |
7719 | &max_extent_size); | |
fa9c0d79 CM |
7720 | if (offset) { |
7721 | /* we found one, proceed */ | |
7722 | spin_unlock(&last_ptr->refill_lock); | |
71ff6437 | 7723 | trace_btrfs_reserve_extent_cluster(fs_info, |
3f7de037 JB |
7724 | block_group, search_start, |
7725 | num_bytes); | |
fa9c0d79 CM |
7726 | goto checks; |
7727 | } | |
0a24325e JB |
7728 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
7729 | && !failed_cluster_refill) { | |
817d52f8 JB |
7730 | spin_unlock(&last_ptr->refill_lock); |
7731 | ||
0a24325e | 7732 | failed_cluster_refill = true; |
817d52f8 JB |
7733 | wait_block_group_cache_progress(block_group, |
7734 | num_bytes + empty_cluster + empty_size); | |
7735 | goto have_block_group; | |
fa9c0d79 | 7736 | } |
817d52f8 | 7737 | |
fa9c0d79 CM |
7738 | /* |
7739 | * at this point we either didn't find a cluster | |
7740 | * or we weren't able to allocate a block from our | |
7741 | * cluster. Free the cluster we've been trying | |
7742 | * to use, and go to the next block group | |
7743 | */ | |
0a24325e | 7744 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
fa9c0d79 | 7745 | spin_unlock(&last_ptr->refill_lock); |
0a24325e | 7746 | goto loop; |
fa9c0d79 CM |
7747 | } |
7748 | ||
062c05c4 | 7749 | unclustered_alloc: |
c759c4e1 JB |
7750 | /* |
7751 | * We are doing an unclustered alloc, set the fragmented flag so | |
7752 | * we don't bother trying to setup a cluster again until we get | |
7753 | * more space. | |
7754 | */ | |
7755 | if (unlikely(last_ptr)) { | |
7756 | spin_lock(&last_ptr->lock); | |
7757 | last_ptr->fragmented = 1; | |
7758 | spin_unlock(&last_ptr->lock); | |
7759 | } | |
a5f6f719 AO |
7760 | spin_lock(&block_group->free_space_ctl->tree_lock); |
7761 | if (cached && | |
7762 | block_group->free_space_ctl->free_space < | |
7763 | num_bytes + empty_cluster + empty_size) { | |
a4820398 MX |
7764 | if (block_group->free_space_ctl->free_space > |
7765 | max_extent_size) | |
7766 | max_extent_size = | |
7767 | block_group->free_space_ctl->free_space; | |
a5f6f719 AO |
7768 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
7769 | goto loop; | |
7770 | } | |
7771 | spin_unlock(&block_group->free_space_ctl->tree_lock); | |
7772 | ||
6226cb0a | 7773 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
a4820398 MX |
7774 | num_bytes, empty_size, |
7775 | &max_extent_size); | |
1cdda9b8 JB |
7776 | /* |
7777 | * If we didn't find a chunk, and we haven't failed on this | |
7778 | * block group before, and this block group is in the middle of | |
7779 | * caching and we are ok with waiting, then go ahead and wait | |
7780 | * for progress to be made, and set failed_alloc to true. | |
7781 | * | |
7782 | * If failed_alloc is true then we've already waited on this | |
7783 | * block group once and should move on to the next block group. | |
7784 | */ | |
7785 | if (!offset && !failed_alloc && !cached && | |
7786 | loop > LOOP_CACHING_NOWAIT) { | |
817d52f8 | 7787 | wait_block_group_cache_progress(block_group, |
1cdda9b8 JB |
7788 | num_bytes + empty_size); |
7789 | failed_alloc = true; | |
817d52f8 | 7790 | goto have_block_group; |
1cdda9b8 JB |
7791 | } else if (!offset) { |
7792 | goto loop; | |
817d52f8 | 7793 | } |
fa9c0d79 | 7794 | checks: |
0b246afa | 7795 | search_start = ALIGN(offset, fs_info->stripesize); |
25179201 | 7796 | |
2552d17e JB |
7797 | /* move on to the next group */ |
7798 | if (search_start + num_bytes > | |
215a63d1 MX |
7799 | block_group->key.objectid + block_group->key.offset) { |
7800 | btrfs_add_free_space(block_group, offset, num_bytes); | |
2552d17e | 7801 | goto loop; |
6226cb0a | 7802 | } |
f5a31e16 | 7803 | |
f0486c68 | 7804 | if (offset < search_start) |
215a63d1 | 7805 | btrfs_add_free_space(block_group, offset, |
f0486c68 YZ |
7806 | search_start - offset); |
7807 | BUG_ON(offset > search_start); | |
2552d17e | 7808 | |
18513091 WX |
7809 | ret = btrfs_add_reserved_bytes(block_group, ram_bytes, |
7810 | num_bytes, delalloc); | |
f0486c68 | 7811 | if (ret == -EAGAIN) { |
215a63d1 | 7812 | btrfs_add_free_space(block_group, offset, num_bytes); |
2552d17e | 7813 | goto loop; |
0f9dd46c | 7814 | } |
9cfa3e34 | 7815 | btrfs_inc_block_group_reservations(block_group); |
0b86a832 | 7816 | |
f0486c68 | 7817 | /* we are all good, lets return */ |
2552d17e JB |
7818 | ins->objectid = search_start; |
7819 | ins->offset = num_bytes; | |
d2fb3437 | 7820 | |
71ff6437 | 7821 | trace_btrfs_reserve_extent(fs_info, block_group, |
3f7de037 | 7822 | search_start, num_bytes); |
e570fd27 | 7823 | btrfs_release_block_group(block_group, delalloc); |
2552d17e JB |
7824 | break; |
7825 | loop: | |
0a24325e | 7826 | failed_cluster_refill = false; |
1cdda9b8 | 7827 | failed_alloc = false; |
b742bb82 | 7828 | BUG_ON(index != get_block_group_index(block_group)); |
e570fd27 | 7829 | btrfs_release_block_group(block_group, delalloc); |
2552d17e JB |
7830 | } |
7831 | up_read(&space_info->groups_sem); | |
7832 | ||
13a0db5a | 7833 | if ((loop == LOOP_CACHING_NOWAIT) && have_caching_bg |
7834 | && !orig_have_caching_bg) | |
7835 | orig_have_caching_bg = true; | |
7836 | ||
60d2adbb MX |
7837 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
7838 | goto search; | |
7839 | ||
b742bb82 YZ |
7840 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
7841 | goto search; | |
7842 | ||
285ff5af | 7843 | /* |
ccf0e725 JB |
7844 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking |
7845 | * caching kthreads as we move along | |
817d52f8 JB |
7846 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
7847 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again | |
7848 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try | |
7849 | * again | |
fa9c0d79 | 7850 | */ |
723bda20 | 7851 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
b742bb82 | 7852 | index = 0; |
a5e681d9 JB |
7853 | if (loop == LOOP_CACHING_NOWAIT) { |
7854 | /* | |
7855 | * We want to skip the LOOP_CACHING_WAIT step if we | |
01327610 | 7856 | * don't have any uncached bgs and we've already done a |
a5e681d9 JB |
7857 | * full search through. |
7858 | */ | |
13a0db5a | 7859 | if (orig_have_caching_bg || !full_search) |
a5e681d9 JB |
7860 | loop = LOOP_CACHING_WAIT; |
7861 | else | |
7862 | loop = LOOP_ALLOC_CHUNK; | |
7863 | } else { | |
7864 | loop++; | |
7865 | } | |
7866 | ||
817d52f8 | 7867 | if (loop == LOOP_ALLOC_CHUNK) { |
00361589 | 7868 | struct btrfs_trans_handle *trans; |
f017f15f WS |
7869 | int exist = 0; |
7870 | ||
7871 | trans = current->journal_info; | |
7872 | if (trans) | |
7873 | exist = 1; | |
7874 | else | |
7875 | trans = btrfs_join_transaction(root); | |
00361589 | 7876 | |
00361589 JB |
7877 | if (IS_ERR(trans)) { |
7878 | ret = PTR_ERR(trans); | |
7879 | goto out; | |
7880 | } | |
7881 | ||
b6919a58 | 7882 | ret = do_chunk_alloc(trans, root, flags, |
ea658bad | 7883 | CHUNK_ALLOC_FORCE); |
a5e681d9 JB |
7884 | |
7885 | /* | |
7886 | * If we can't allocate a new chunk we've already looped | |
7887 | * through at least once, move on to the NO_EMPTY_SIZE | |
7888 | * case. | |
7889 | */ | |
7890 | if (ret == -ENOSPC) | |
7891 | loop = LOOP_NO_EMPTY_SIZE; | |
7892 | ||
ea658bad JB |
7893 | /* |
7894 | * Do not bail out on ENOSPC since we | |
7895 | * can do more things. | |
7896 | */ | |
00361589 | 7897 | if (ret < 0 && ret != -ENOSPC) |
66642832 | 7898 | btrfs_abort_transaction(trans, ret); |
00361589 JB |
7899 | else |
7900 | ret = 0; | |
f017f15f WS |
7901 | if (!exist) |
7902 | btrfs_end_transaction(trans, root); | |
00361589 | 7903 | if (ret) |
ea658bad | 7904 | goto out; |
2552d17e JB |
7905 | } |
7906 | ||
723bda20 | 7907 | if (loop == LOOP_NO_EMPTY_SIZE) { |
a5e681d9 JB |
7908 | /* |
7909 | * Don't loop again if we already have no empty_size and | |
7910 | * no empty_cluster. | |
7911 | */ | |
7912 | if (empty_size == 0 && | |
7913 | empty_cluster == 0) { | |
7914 | ret = -ENOSPC; | |
7915 | goto out; | |
7916 | } | |
723bda20 JB |
7917 | empty_size = 0; |
7918 | empty_cluster = 0; | |
fa9c0d79 | 7919 | } |
723bda20 JB |
7920 | |
7921 | goto search; | |
2552d17e JB |
7922 | } else if (!ins->objectid) { |
7923 | ret = -ENOSPC; | |
d82a6f1d | 7924 | } else if (ins->objectid) { |
c759c4e1 JB |
7925 | if (!use_cluster && last_ptr) { |
7926 | spin_lock(&last_ptr->lock); | |
7927 | last_ptr->window_start = ins->objectid; | |
7928 | spin_unlock(&last_ptr->lock); | |
7929 | } | |
80eb234a | 7930 | ret = 0; |
be744175 | 7931 | } |
79787eaa | 7932 | out: |
4f4db217 JB |
7933 | if (ret == -ENOSPC) { |
7934 | spin_lock(&space_info->lock); | |
7935 | space_info->max_extent_size = max_extent_size; | |
7936 | spin_unlock(&space_info->lock); | |
a4820398 | 7937 | ins->offset = max_extent_size; |
4f4db217 | 7938 | } |
0f70abe2 | 7939 | return ret; |
fec577fb | 7940 | } |
ec44a35c | 7941 | |
ab8d0fc4 JM |
7942 | static void dump_space_info(struct btrfs_fs_info *fs_info, |
7943 | struct btrfs_space_info *info, u64 bytes, | |
9ed74f2d | 7944 | int dump_block_groups) |
0f9dd46c JB |
7945 | { |
7946 | struct btrfs_block_group_cache *cache; | |
b742bb82 | 7947 | int index = 0; |
0f9dd46c | 7948 | |
9ed74f2d | 7949 | spin_lock(&info->lock); |
ab8d0fc4 JM |
7950 | btrfs_info(fs_info, "space_info %llu has %llu free, is %sfull", |
7951 | info->flags, | |
7952 | info->total_bytes - info->bytes_used - info->bytes_pinned - | |
7953 | info->bytes_reserved - info->bytes_readonly - | |
7954 | info->bytes_may_use, (info->full) ? "" : "not "); | |
7955 | btrfs_info(fs_info, | |
7956 | "space_info total=%llu, used=%llu, pinned=%llu, reserved=%llu, may_use=%llu, readonly=%llu", | |
7957 | info->total_bytes, info->bytes_used, info->bytes_pinned, | |
7958 | info->bytes_reserved, info->bytes_may_use, | |
7959 | info->bytes_readonly); | |
9ed74f2d JB |
7960 | spin_unlock(&info->lock); |
7961 | ||
7962 | if (!dump_block_groups) | |
7963 | return; | |
0f9dd46c | 7964 | |
80eb234a | 7965 | down_read(&info->groups_sem); |
b742bb82 YZ |
7966 | again: |
7967 | list_for_each_entry(cache, &info->block_groups[index], list) { | |
0f9dd46c | 7968 | spin_lock(&cache->lock); |
ab8d0fc4 JM |
7969 | btrfs_info(fs_info, |
7970 | "block group %llu has %llu bytes, %llu used %llu pinned %llu reserved %s", | |
7971 | cache->key.objectid, cache->key.offset, | |
7972 | btrfs_block_group_used(&cache->item), cache->pinned, | |
7973 | cache->reserved, cache->ro ? "[readonly]" : ""); | |
0f9dd46c JB |
7974 | btrfs_dump_free_space(cache, bytes); |
7975 | spin_unlock(&cache->lock); | |
7976 | } | |
b742bb82 YZ |
7977 | if (++index < BTRFS_NR_RAID_TYPES) |
7978 | goto again; | |
80eb234a | 7979 | up_read(&info->groups_sem); |
0f9dd46c | 7980 | } |
e8569813 | 7981 | |
18513091 | 7982 | int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, |
11833d66 YZ |
7983 | u64 num_bytes, u64 min_alloc_size, |
7984 | u64 empty_size, u64 hint_byte, | |
e570fd27 | 7985 | struct btrfs_key *ins, int is_data, int delalloc) |
fec577fb | 7986 | { |
ab8d0fc4 | 7987 | struct btrfs_fs_info *fs_info = root->fs_info; |
36af4e07 | 7988 | bool final_tried = num_bytes == min_alloc_size; |
b6919a58 | 7989 | u64 flags; |
fec577fb | 7990 | int ret; |
925baedd | 7991 | |
b6919a58 | 7992 | flags = btrfs_get_alloc_profile(root, is_data); |
98d20f67 | 7993 | again: |
0b246afa | 7994 | WARN_ON(num_bytes < fs_info->sectorsize); |
18513091 WX |
7995 | ret = find_free_extent(root, ram_bytes, num_bytes, empty_size, |
7996 | hint_byte, ins, flags, delalloc); | |
9cfa3e34 | 7997 | if (!ret && !is_data) { |
ab8d0fc4 | 7998 | btrfs_dec_block_group_reservations(fs_info, ins->objectid); |
9cfa3e34 | 7999 | } else if (ret == -ENOSPC) { |
a4820398 MX |
8000 | if (!final_tried && ins->offset) { |
8001 | num_bytes = min(num_bytes >> 1, ins->offset); | |
da17066c | 8002 | num_bytes = round_down(num_bytes, |
0b246afa | 8003 | fs_info->sectorsize); |
9e622d6b | 8004 | num_bytes = max(num_bytes, min_alloc_size); |
18513091 | 8005 | ram_bytes = num_bytes; |
9e622d6b MX |
8006 | if (num_bytes == min_alloc_size) |
8007 | final_tried = true; | |
8008 | goto again; | |
ab8d0fc4 | 8009 | } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
9e622d6b MX |
8010 | struct btrfs_space_info *sinfo; |
8011 | ||
ab8d0fc4 | 8012 | sinfo = __find_space_info(fs_info, flags); |
0b246afa | 8013 | btrfs_err(fs_info, |
5d163e0e JM |
8014 | "allocation failed flags %llu, wanted %llu", |
8015 | flags, num_bytes); | |
53804280 | 8016 | if (sinfo) |
ab8d0fc4 | 8017 | dump_space_info(fs_info, sinfo, num_bytes, 1); |
9e622d6b | 8018 | } |
925baedd | 8019 | } |
0f9dd46c JB |
8020 | |
8021 | return ret; | |
e6dcd2dc CM |
8022 | } |
8023 | ||
e688b725 | 8024 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
e570fd27 MX |
8025 | u64 start, u64 len, |
8026 | int pin, int delalloc) | |
65b51a00 | 8027 | { |
0b246afa | 8028 | struct btrfs_fs_info *fs_info = root->fs_info; |
0f9dd46c | 8029 | struct btrfs_block_group_cache *cache; |
1f3c79a2 | 8030 | int ret = 0; |
0f9dd46c | 8031 | |
0b246afa | 8032 | cache = btrfs_lookup_block_group(fs_info, start); |
0f9dd46c | 8033 | if (!cache) { |
0b246afa JM |
8034 | btrfs_err(fs_info, "Unable to find block group for %llu", |
8035 | start); | |
0f9dd46c JB |
8036 | return -ENOSPC; |
8037 | } | |
1f3c79a2 | 8038 | |
e688b725 CM |
8039 | if (pin) |
8040 | pin_down_extent(root, cache, start, len, 1); | |
8041 | else { | |
0b246afa | 8042 | if (btrfs_test_opt(fs_info, DISCARD)) |
dcc82f47 | 8043 | ret = btrfs_discard_extent(root, start, len, NULL); |
e688b725 | 8044 | btrfs_add_free_space(cache, start, len); |
4824f1f4 | 8045 | btrfs_free_reserved_bytes(cache, len, delalloc); |
71ff6437 | 8046 | trace_btrfs_reserved_extent_free(fs_info, start, len); |
e688b725 | 8047 | } |
31193213 | 8048 | |
fa9c0d79 | 8049 | btrfs_put_block_group(cache); |
e6dcd2dc CM |
8050 | return ret; |
8051 | } | |
8052 | ||
e688b725 | 8053 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
e570fd27 | 8054 | u64 start, u64 len, int delalloc) |
e688b725 | 8055 | { |
e570fd27 | 8056 | return __btrfs_free_reserved_extent(root, start, len, 0, delalloc); |
e688b725 CM |
8057 | } |
8058 | ||
8059 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, | |
8060 | u64 start, u64 len) | |
8061 | { | |
e570fd27 | 8062 | return __btrfs_free_reserved_extent(root, start, len, 1, 0); |
e688b725 CM |
8063 | } |
8064 | ||
5d4f98a2 YZ |
8065 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
8066 | struct btrfs_root *root, | |
8067 | u64 parent, u64 root_objectid, | |
8068 | u64 flags, u64 owner, u64 offset, | |
8069 | struct btrfs_key *ins, int ref_mod) | |
e6dcd2dc CM |
8070 | { |
8071 | int ret; | |
5d4f98a2 | 8072 | struct btrfs_fs_info *fs_info = root->fs_info; |
e6dcd2dc | 8073 | struct btrfs_extent_item *extent_item; |
5d4f98a2 | 8074 | struct btrfs_extent_inline_ref *iref; |
e6dcd2dc | 8075 | struct btrfs_path *path; |
5d4f98a2 YZ |
8076 | struct extent_buffer *leaf; |
8077 | int type; | |
8078 | u32 size; | |
26b8003f | 8079 | |
5d4f98a2 YZ |
8080 | if (parent > 0) |
8081 | type = BTRFS_SHARED_DATA_REF_KEY; | |
8082 | else | |
8083 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
58176a96 | 8084 | |
5d4f98a2 | 8085 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
7bb86316 CM |
8086 | |
8087 | path = btrfs_alloc_path(); | |
db5b493a TI |
8088 | if (!path) |
8089 | return -ENOMEM; | |
47e4bb98 | 8090 | |
b9473439 | 8091 | path->leave_spinning = 1; |
5d4f98a2 YZ |
8092 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
8093 | ins, size); | |
79787eaa JM |
8094 | if (ret) { |
8095 | btrfs_free_path(path); | |
8096 | return ret; | |
8097 | } | |
0f9dd46c | 8098 | |
5d4f98a2 YZ |
8099 | leaf = path->nodes[0]; |
8100 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
47e4bb98 | 8101 | struct btrfs_extent_item); |
5d4f98a2 YZ |
8102 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
8103 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
8104 | btrfs_set_extent_flags(leaf, extent_item, | |
8105 | flags | BTRFS_EXTENT_FLAG_DATA); | |
8106 | ||
8107 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); | |
8108 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
8109 | if (parent > 0) { | |
8110 | struct btrfs_shared_data_ref *ref; | |
8111 | ref = (struct btrfs_shared_data_ref *)(iref + 1); | |
8112 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
8113 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); | |
8114 | } else { | |
8115 | struct btrfs_extent_data_ref *ref; | |
8116 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
8117 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); | |
8118 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
8119 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
8120 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); | |
8121 | } | |
47e4bb98 CM |
8122 | |
8123 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
7bb86316 | 8124 | btrfs_free_path(path); |
f510cfec | 8125 | |
1e144fb8 OS |
8126 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
8127 | ins->offset); | |
8128 | if (ret) | |
8129 | return ret; | |
8130 | ||
6202df69 | 8131 | ret = update_block_group(trans, fs_info, ins->objectid, ins->offset, 1); |
79787eaa | 8132 | if (ret) { /* -ENOENT, logic error */ |
c2cf52eb | 8133 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
c1c9ff7c | 8134 | ins->objectid, ins->offset); |
f5947066 CM |
8135 | BUG(); |
8136 | } | |
71ff6437 | 8137 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset); |
e6dcd2dc CM |
8138 | return ret; |
8139 | } | |
8140 | ||
5d4f98a2 YZ |
8141 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
8142 | struct btrfs_root *root, | |
8143 | u64 parent, u64 root_objectid, | |
8144 | u64 flags, struct btrfs_disk_key *key, | |
b06c4bf5 | 8145 | int level, struct btrfs_key *ins) |
e6dcd2dc CM |
8146 | { |
8147 | int ret; | |
5d4f98a2 YZ |
8148 | struct btrfs_fs_info *fs_info = root->fs_info; |
8149 | struct btrfs_extent_item *extent_item; | |
8150 | struct btrfs_tree_block_info *block_info; | |
8151 | struct btrfs_extent_inline_ref *iref; | |
8152 | struct btrfs_path *path; | |
8153 | struct extent_buffer *leaf; | |
3173a18f | 8154 | u32 size = sizeof(*extent_item) + sizeof(*iref); |
fcebe456 | 8155 | u64 num_bytes = ins->offset; |
0b246afa | 8156 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); |
3173a18f JB |
8157 | |
8158 | if (!skinny_metadata) | |
8159 | size += sizeof(*block_info); | |
1c2308f8 | 8160 | |
5d4f98a2 | 8161 | path = btrfs_alloc_path(); |
857cc2fc JB |
8162 | if (!path) { |
8163 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, | |
0b246afa | 8164 | fs_info->nodesize); |
d8926bb3 | 8165 | return -ENOMEM; |
857cc2fc | 8166 | } |
56bec294 | 8167 | |
5d4f98a2 YZ |
8168 | path->leave_spinning = 1; |
8169 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, | |
8170 | ins, size); | |
79787eaa | 8171 | if (ret) { |
dd825259 | 8172 | btrfs_free_path(path); |
857cc2fc | 8173 | btrfs_free_and_pin_reserved_extent(root, ins->objectid, |
0b246afa | 8174 | fs_info->nodesize); |
79787eaa JM |
8175 | return ret; |
8176 | } | |
5d4f98a2 YZ |
8177 | |
8178 | leaf = path->nodes[0]; | |
8179 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
8180 | struct btrfs_extent_item); | |
8181 | btrfs_set_extent_refs(leaf, extent_item, 1); | |
8182 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
8183 | btrfs_set_extent_flags(leaf, extent_item, | |
8184 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); | |
5d4f98a2 | 8185 | |
3173a18f JB |
8186 | if (skinny_metadata) { |
8187 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); | |
0b246afa | 8188 | num_bytes = fs_info->nodesize; |
3173a18f JB |
8189 | } else { |
8190 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); | |
8191 | btrfs_set_tree_block_key(leaf, block_info, key); | |
8192 | btrfs_set_tree_block_level(leaf, block_info, level); | |
8193 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); | |
8194 | } | |
5d4f98a2 | 8195 | |
5d4f98a2 YZ |
8196 | if (parent > 0) { |
8197 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | |
8198 | btrfs_set_extent_inline_ref_type(leaf, iref, | |
8199 | BTRFS_SHARED_BLOCK_REF_KEY); | |
8200 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
8201 | } else { | |
8202 | btrfs_set_extent_inline_ref_type(leaf, iref, | |
8203 | BTRFS_TREE_BLOCK_REF_KEY); | |
8204 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | |
8205 | } | |
8206 | ||
8207 | btrfs_mark_buffer_dirty(leaf); | |
8208 | btrfs_free_path(path); | |
8209 | ||
1e144fb8 OS |
8210 | ret = remove_from_free_space_tree(trans, fs_info, ins->objectid, |
8211 | num_bytes); | |
8212 | if (ret) | |
8213 | return ret; | |
8214 | ||
6202df69 JM |
8215 | ret = update_block_group(trans, fs_info, ins->objectid, |
8216 | fs_info->nodesize, 1); | |
79787eaa | 8217 | if (ret) { /* -ENOENT, logic error */ |
c2cf52eb | 8218 | btrfs_err(fs_info, "update block group failed for %llu %llu", |
c1c9ff7c | 8219 | ins->objectid, ins->offset); |
5d4f98a2 YZ |
8220 | BUG(); |
8221 | } | |
0be5dc67 | 8222 | |
71ff6437 | 8223 | trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, |
0b246afa | 8224 | fs_info->nodesize); |
5d4f98a2 YZ |
8225 | return ret; |
8226 | } | |
8227 | ||
8228 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
8229 | struct btrfs_root *root, | |
8230 | u64 root_objectid, u64 owner, | |
5846a3c2 QW |
8231 | u64 offset, u64 ram_bytes, |
8232 | struct btrfs_key *ins) | |
5d4f98a2 | 8233 | { |
0b246afa | 8234 | struct btrfs_fs_info *fs_info = root->fs_info; |
5d4f98a2 YZ |
8235 | int ret; |
8236 | ||
8237 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); | |
8238 | ||
0b246afa | 8239 | ret = btrfs_add_delayed_data_ref(fs_info, trans, ins->objectid, |
66d7e7f0 AJ |
8240 | ins->offset, 0, |
8241 | root_objectid, owner, offset, | |
5846a3c2 QW |
8242 | ram_bytes, BTRFS_ADD_DELAYED_EXTENT, |
8243 | NULL); | |
e6dcd2dc CM |
8244 | return ret; |
8245 | } | |
e02119d5 CM |
8246 | |
8247 | /* | |
8248 | * this is used by the tree logging recovery code. It records that | |
8249 | * an extent has been allocated and makes sure to clear the free | |
8250 | * space cache bits as well | |
8251 | */ | |
5d4f98a2 YZ |
8252 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
8253 | struct btrfs_root *root, | |
8254 | u64 root_objectid, u64 owner, u64 offset, | |
8255 | struct btrfs_key *ins) | |
e02119d5 | 8256 | { |
0b246afa | 8257 | struct btrfs_fs_info *fs_info = root->fs_info; |
e02119d5 CM |
8258 | int ret; |
8259 | struct btrfs_block_group_cache *block_group; | |
ed7a6948 | 8260 | struct btrfs_space_info *space_info; |
11833d66 | 8261 | |
8c2a1a30 JB |
8262 | /* |
8263 | * Mixed block groups will exclude before processing the log so we only | |
01327610 | 8264 | * need to do the exclude dance if this fs isn't mixed. |
8c2a1a30 | 8265 | */ |
0b246afa | 8266 | if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) { |
8c2a1a30 | 8267 | ret = __exclude_logged_extent(root, ins->objectid, ins->offset); |
b50c6e25 | 8268 | if (ret) |
8c2a1a30 | 8269 | return ret; |
11833d66 YZ |
8270 | } |
8271 | ||
0b246afa | 8272 | block_group = btrfs_lookup_block_group(fs_info, ins->objectid); |
8c2a1a30 JB |
8273 | if (!block_group) |
8274 | return -EINVAL; | |
8275 | ||
ed7a6948 WX |
8276 | space_info = block_group->space_info; |
8277 | spin_lock(&space_info->lock); | |
8278 | spin_lock(&block_group->lock); | |
8279 | space_info->bytes_reserved += ins->offset; | |
8280 | block_group->reserved += ins->offset; | |
8281 | spin_unlock(&block_group->lock); | |
8282 | spin_unlock(&space_info->lock); | |
8283 | ||
5d4f98a2 YZ |
8284 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
8285 | 0, owner, offset, ins, 1); | |
b50c6e25 | 8286 | btrfs_put_block_group(block_group); |
e02119d5 CM |
8287 | return ret; |
8288 | } | |
8289 | ||
48a3b636 ES |
8290 | static struct extent_buffer * |
8291 | btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
fe864576 | 8292 | u64 bytenr, int level) |
65b51a00 | 8293 | { |
0b246afa | 8294 | struct btrfs_fs_info *fs_info = root->fs_info; |
65b51a00 CM |
8295 | struct extent_buffer *buf; |
8296 | ||
a83fffb7 | 8297 | buf = btrfs_find_create_tree_block(root, bytenr); |
c871b0f2 LB |
8298 | if (IS_ERR(buf)) |
8299 | return buf; | |
8300 | ||
65b51a00 | 8301 | btrfs_set_header_generation(buf, trans->transid); |
85d4e461 | 8302 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
65b51a00 | 8303 | btrfs_tree_lock(buf); |
0b246afa | 8304 | clean_tree_block(trans, fs_info, buf); |
3083ee2e | 8305 | clear_bit(EXTENT_BUFFER_STALE, &buf->bflags); |
b4ce94de CM |
8306 | |
8307 | btrfs_set_lock_blocking(buf); | |
4db8c528 | 8308 | set_extent_buffer_uptodate(buf); |
b4ce94de | 8309 | |
d0c803c4 | 8310 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
656f30db | 8311 | buf->log_index = root->log_transid % 2; |
8cef4e16 YZ |
8312 | /* |
8313 | * we allow two log transactions at a time, use different | |
8314 | * EXENT bit to differentiate dirty pages. | |
8315 | */ | |
656f30db | 8316 | if (buf->log_index == 0) |
8cef4e16 YZ |
8317 | set_extent_dirty(&root->dirty_log_pages, buf->start, |
8318 | buf->start + buf->len - 1, GFP_NOFS); | |
8319 | else | |
8320 | set_extent_new(&root->dirty_log_pages, buf->start, | |
3744dbeb | 8321 | buf->start + buf->len - 1); |
d0c803c4 | 8322 | } else { |
656f30db | 8323 | buf->log_index = -1; |
d0c803c4 | 8324 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
65b51a00 | 8325 | buf->start + buf->len - 1, GFP_NOFS); |
d0c803c4 | 8326 | } |
64c12921 | 8327 | trans->dirty = true; |
b4ce94de | 8328 | /* this returns a buffer locked for blocking */ |
65b51a00 CM |
8329 | return buf; |
8330 | } | |
8331 | ||
f0486c68 YZ |
8332 | static struct btrfs_block_rsv * |
8333 | use_block_rsv(struct btrfs_trans_handle *trans, | |
8334 | struct btrfs_root *root, u32 blocksize) | |
8335 | { | |
0b246afa | 8336 | struct btrfs_fs_info *fs_info = root->fs_info; |
f0486c68 | 8337 | struct btrfs_block_rsv *block_rsv; |
0b246afa | 8338 | struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv; |
f0486c68 | 8339 | int ret; |
d88033db | 8340 | bool global_updated = false; |
f0486c68 YZ |
8341 | |
8342 | block_rsv = get_block_rsv(trans, root); | |
8343 | ||
b586b323 MX |
8344 | if (unlikely(block_rsv->size == 0)) |
8345 | goto try_reserve; | |
d88033db | 8346 | again: |
f0486c68 YZ |
8347 | ret = block_rsv_use_bytes(block_rsv, blocksize); |
8348 | if (!ret) | |
8349 | return block_rsv; | |
8350 | ||
b586b323 MX |
8351 | if (block_rsv->failfast) |
8352 | return ERR_PTR(ret); | |
8353 | ||
d88033db MX |
8354 | if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) { |
8355 | global_updated = true; | |
0b246afa | 8356 | update_global_block_rsv(fs_info); |
d88033db MX |
8357 | goto again; |
8358 | } | |
8359 | ||
0b246afa | 8360 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
b586b323 MX |
8361 | static DEFINE_RATELIMIT_STATE(_rs, |
8362 | DEFAULT_RATELIMIT_INTERVAL * 10, | |
8363 | /*DEFAULT_RATELIMIT_BURST*/ 1); | |
8364 | if (__ratelimit(&_rs)) | |
8365 | WARN(1, KERN_DEBUG | |
efe120a0 | 8366 | "BTRFS: block rsv returned %d\n", ret); |
b586b323 MX |
8367 | } |
8368 | try_reserve: | |
8369 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, | |
8370 | BTRFS_RESERVE_NO_FLUSH); | |
8371 | if (!ret) | |
8372 | return block_rsv; | |
8373 | /* | |
8374 | * If we couldn't reserve metadata bytes try and use some from | |
5881cfc9 MX |
8375 | * the global reserve if its space type is the same as the global |
8376 | * reservation. | |
b586b323 | 8377 | */ |
5881cfc9 MX |
8378 | if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL && |
8379 | block_rsv->space_info == global_rsv->space_info) { | |
b586b323 MX |
8380 | ret = block_rsv_use_bytes(global_rsv, blocksize); |
8381 | if (!ret) | |
8382 | return global_rsv; | |
8383 | } | |
8384 | return ERR_PTR(ret); | |
f0486c68 YZ |
8385 | } |
8386 | ||
8c2a3ca2 JB |
8387 | static void unuse_block_rsv(struct btrfs_fs_info *fs_info, |
8388 | struct btrfs_block_rsv *block_rsv, u32 blocksize) | |
f0486c68 YZ |
8389 | { |
8390 | block_rsv_add_bytes(block_rsv, blocksize, 0); | |
8c2a3ca2 | 8391 | block_rsv_release_bytes(fs_info, block_rsv, NULL, 0); |
f0486c68 YZ |
8392 | } |
8393 | ||
fec577fb | 8394 | /* |
f0486c68 | 8395 | * finds a free extent and does all the dirty work required for allocation |
67b7859e | 8396 | * returns the tree buffer or an ERR_PTR on error. |
fec577fb | 8397 | */ |
4d75f8a9 DS |
8398 | struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans, |
8399 | struct btrfs_root *root, | |
5d4f98a2 YZ |
8400 | u64 parent, u64 root_objectid, |
8401 | struct btrfs_disk_key *key, int level, | |
5581a51a | 8402 | u64 hint, u64 empty_size) |
fec577fb | 8403 | { |
0b246afa | 8404 | struct btrfs_fs_info *fs_info = root->fs_info; |
e2fa7227 | 8405 | struct btrfs_key ins; |
f0486c68 | 8406 | struct btrfs_block_rsv *block_rsv; |
5f39d397 | 8407 | struct extent_buffer *buf; |
67b7859e | 8408 | struct btrfs_delayed_extent_op *extent_op; |
f0486c68 YZ |
8409 | u64 flags = 0; |
8410 | int ret; | |
0b246afa JM |
8411 | u32 blocksize = fs_info->nodesize; |
8412 | bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA); | |
fec577fb | 8413 | |
05653ef3 | 8414 | #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS |
0b246afa | 8415 | if (btrfs_is_testing(fs_info)) { |
faa2dbf0 | 8416 | buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr, |
fe864576 | 8417 | level); |
faa2dbf0 JB |
8418 | if (!IS_ERR(buf)) |
8419 | root->alloc_bytenr += blocksize; | |
8420 | return buf; | |
8421 | } | |
05653ef3 | 8422 | #endif |
fccb84c9 | 8423 | |
f0486c68 YZ |
8424 | block_rsv = use_block_rsv(trans, root, blocksize); |
8425 | if (IS_ERR(block_rsv)) | |
8426 | return ERR_CAST(block_rsv); | |
8427 | ||
18513091 | 8428 | ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize, |
e570fd27 | 8429 | empty_size, hint, &ins, 0, 0); |
67b7859e OS |
8430 | if (ret) |
8431 | goto out_unuse; | |
55c69072 | 8432 | |
fe864576 | 8433 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, level); |
67b7859e OS |
8434 | if (IS_ERR(buf)) { |
8435 | ret = PTR_ERR(buf); | |
8436 | goto out_free_reserved; | |
8437 | } | |
f0486c68 YZ |
8438 | |
8439 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { | |
8440 | if (parent == 0) | |
8441 | parent = ins.objectid; | |
8442 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
8443 | } else | |
8444 | BUG_ON(parent > 0); | |
8445 | ||
8446 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { | |
78a6184a | 8447 | extent_op = btrfs_alloc_delayed_extent_op(); |
67b7859e OS |
8448 | if (!extent_op) { |
8449 | ret = -ENOMEM; | |
8450 | goto out_free_buf; | |
8451 | } | |
f0486c68 YZ |
8452 | if (key) |
8453 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); | |
8454 | else | |
8455 | memset(&extent_op->key, 0, sizeof(extent_op->key)); | |
8456 | extent_op->flags_to_set = flags; | |
35b3ad50 DS |
8457 | extent_op->update_key = skinny_metadata ? false : true; |
8458 | extent_op->update_flags = true; | |
8459 | extent_op->is_data = false; | |
b1c79e09 | 8460 | extent_op->level = level; |
f0486c68 | 8461 | |
0b246afa | 8462 | ret = btrfs_add_delayed_tree_ref(fs_info, trans, |
67b7859e OS |
8463 | ins.objectid, ins.offset, |
8464 | parent, root_objectid, level, | |
8465 | BTRFS_ADD_DELAYED_EXTENT, | |
b06c4bf5 | 8466 | extent_op); |
67b7859e OS |
8467 | if (ret) |
8468 | goto out_free_delayed; | |
f0486c68 | 8469 | } |
fec577fb | 8470 | return buf; |
67b7859e OS |
8471 | |
8472 | out_free_delayed: | |
8473 | btrfs_free_delayed_extent_op(extent_op); | |
8474 | out_free_buf: | |
8475 | free_extent_buffer(buf); | |
8476 | out_free_reserved: | |
8477 | btrfs_free_reserved_extent(root, ins.objectid, ins.offset, 0); | |
8478 | out_unuse: | |
0b246afa | 8479 | unuse_block_rsv(fs_info, block_rsv, blocksize); |
67b7859e | 8480 | return ERR_PTR(ret); |
fec577fb | 8481 | } |
a28ec197 | 8482 | |
2c47e605 YZ |
8483 | struct walk_control { |
8484 | u64 refs[BTRFS_MAX_LEVEL]; | |
8485 | u64 flags[BTRFS_MAX_LEVEL]; | |
8486 | struct btrfs_key update_progress; | |
8487 | int stage; | |
8488 | int level; | |
8489 | int shared_level; | |
8490 | int update_ref; | |
8491 | int keep_locks; | |
1c4850e2 YZ |
8492 | int reada_slot; |
8493 | int reada_count; | |
66d7e7f0 | 8494 | int for_reloc; |
2c47e605 YZ |
8495 | }; |
8496 | ||
8497 | #define DROP_REFERENCE 1 | |
8498 | #define UPDATE_BACKREF 2 | |
8499 | ||
1c4850e2 YZ |
8500 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
8501 | struct btrfs_root *root, | |
8502 | struct walk_control *wc, | |
8503 | struct btrfs_path *path) | |
6407bf6d | 8504 | { |
0b246afa | 8505 | struct btrfs_fs_info *fs_info = root->fs_info; |
1c4850e2 YZ |
8506 | u64 bytenr; |
8507 | u64 generation; | |
8508 | u64 refs; | |
94fcca9f | 8509 | u64 flags; |
5d4f98a2 | 8510 | u32 nritems; |
1c4850e2 YZ |
8511 | struct btrfs_key key; |
8512 | struct extent_buffer *eb; | |
6407bf6d | 8513 | int ret; |
1c4850e2 YZ |
8514 | int slot; |
8515 | int nread = 0; | |
6407bf6d | 8516 | |
1c4850e2 YZ |
8517 | if (path->slots[wc->level] < wc->reada_slot) { |
8518 | wc->reada_count = wc->reada_count * 2 / 3; | |
8519 | wc->reada_count = max(wc->reada_count, 2); | |
8520 | } else { | |
8521 | wc->reada_count = wc->reada_count * 3 / 2; | |
8522 | wc->reada_count = min_t(int, wc->reada_count, | |
0b246afa | 8523 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
1c4850e2 | 8524 | } |
7bb86316 | 8525 | |
1c4850e2 YZ |
8526 | eb = path->nodes[wc->level]; |
8527 | nritems = btrfs_header_nritems(eb); | |
bd56b302 | 8528 | |
1c4850e2 YZ |
8529 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
8530 | if (nread >= wc->reada_count) | |
8531 | break; | |
bd56b302 | 8532 | |
2dd3e67b | 8533 | cond_resched(); |
1c4850e2 YZ |
8534 | bytenr = btrfs_node_blockptr(eb, slot); |
8535 | generation = btrfs_node_ptr_generation(eb, slot); | |
2dd3e67b | 8536 | |
1c4850e2 YZ |
8537 | if (slot == path->slots[wc->level]) |
8538 | goto reada; | |
5d4f98a2 | 8539 | |
1c4850e2 YZ |
8540 | if (wc->stage == UPDATE_BACKREF && |
8541 | generation <= root->root_key.offset) | |
bd56b302 CM |
8542 | continue; |
8543 | ||
94fcca9f | 8544 | /* We don't lock the tree block, it's OK to be racy here */ |
3173a18f JB |
8545 | ret = btrfs_lookup_extent_info(trans, root, bytenr, |
8546 | wc->level - 1, 1, &refs, | |
8547 | &flags); | |
79787eaa JM |
8548 | /* We don't care about errors in readahead. */ |
8549 | if (ret < 0) | |
8550 | continue; | |
94fcca9f YZ |
8551 | BUG_ON(refs == 0); |
8552 | ||
1c4850e2 | 8553 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 YZ |
8554 | if (refs == 1) |
8555 | goto reada; | |
bd56b302 | 8556 | |
94fcca9f YZ |
8557 | if (wc->level == 1 && |
8558 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
8559 | continue; | |
1c4850e2 YZ |
8560 | if (!wc->update_ref || |
8561 | generation <= root->root_key.offset) | |
8562 | continue; | |
8563 | btrfs_node_key_to_cpu(eb, &key, slot); | |
8564 | ret = btrfs_comp_cpu_keys(&key, | |
8565 | &wc->update_progress); | |
8566 | if (ret < 0) | |
8567 | continue; | |
94fcca9f YZ |
8568 | } else { |
8569 | if (wc->level == 1 && | |
8570 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
8571 | continue; | |
6407bf6d | 8572 | } |
1c4850e2 | 8573 | reada: |
d3e46fea | 8574 | readahead_tree_block(root, bytenr); |
1c4850e2 | 8575 | nread++; |
20524f02 | 8576 | } |
1c4850e2 | 8577 | wc->reada_slot = slot; |
20524f02 | 8578 | } |
2c47e605 | 8579 | |
f82d02d9 | 8580 | /* |
2c016dc2 | 8581 | * helper to process tree block while walking down the tree. |
2c47e605 | 8582 | * |
2c47e605 YZ |
8583 | * when wc->stage == UPDATE_BACKREF, this function updates |
8584 | * back refs for pointers in the block. | |
8585 | * | |
8586 | * NOTE: return value 1 means we should stop walking down. | |
f82d02d9 | 8587 | */ |
2c47e605 | 8588 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
5d4f98a2 | 8589 | struct btrfs_root *root, |
2c47e605 | 8590 | struct btrfs_path *path, |
94fcca9f | 8591 | struct walk_control *wc, int lookup_info) |
f82d02d9 | 8592 | { |
2c47e605 YZ |
8593 | int level = wc->level; |
8594 | struct extent_buffer *eb = path->nodes[level]; | |
2c47e605 | 8595 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
f82d02d9 YZ |
8596 | int ret; |
8597 | ||
2c47e605 YZ |
8598 | if (wc->stage == UPDATE_BACKREF && |
8599 | btrfs_header_owner(eb) != root->root_key.objectid) | |
8600 | return 1; | |
f82d02d9 | 8601 | |
2c47e605 YZ |
8602 | /* |
8603 | * when reference count of tree block is 1, it won't increase | |
8604 | * again. once full backref flag is set, we never clear it. | |
8605 | */ | |
94fcca9f YZ |
8606 | if (lookup_info && |
8607 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || | |
8608 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { | |
2c47e605 YZ |
8609 | BUG_ON(!path->locks[level]); |
8610 | ret = btrfs_lookup_extent_info(trans, root, | |
3173a18f | 8611 | eb->start, level, 1, |
2c47e605 YZ |
8612 | &wc->refs[level], |
8613 | &wc->flags[level]); | |
79787eaa JM |
8614 | BUG_ON(ret == -ENOMEM); |
8615 | if (ret) | |
8616 | return ret; | |
2c47e605 YZ |
8617 | BUG_ON(wc->refs[level] == 0); |
8618 | } | |
5d4f98a2 | 8619 | |
2c47e605 YZ |
8620 | if (wc->stage == DROP_REFERENCE) { |
8621 | if (wc->refs[level] > 1) | |
8622 | return 1; | |
f82d02d9 | 8623 | |
2c47e605 | 8624 | if (path->locks[level] && !wc->keep_locks) { |
bd681513 | 8625 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
8626 | path->locks[level] = 0; |
8627 | } | |
8628 | return 0; | |
8629 | } | |
f82d02d9 | 8630 | |
2c47e605 YZ |
8631 | /* wc->stage == UPDATE_BACKREF */ |
8632 | if (!(wc->flags[level] & flag)) { | |
8633 | BUG_ON(!path->locks[level]); | |
e339a6b0 | 8634 | ret = btrfs_inc_ref(trans, root, eb, 1); |
79787eaa | 8635 | BUG_ON(ret); /* -ENOMEM */ |
e339a6b0 | 8636 | ret = btrfs_dec_ref(trans, root, eb, 0); |
79787eaa | 8637 | BUG_ON(ret); /* -ENOMEM */ |
2c47e605 | 8638 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, |
b1c79e09 JB |
8639 | eb->len, flag, |
8640 | btrfs_header_level(eb), 0); | |
79787eaa | 8641 | BUG_ON(ret); /* -ENOMEM */ |
2c47e605 YZ |
8642 | wc->flags[level] |= flag; |
8643 | } | |
8644 | ||
8645 | /* | |
8646 | * the block is shared by multiple trees, so it's not good to | |
8647 | * keep the tree lock | |
8648 | */ | |
8649 | if (path->locks[level] && level > 0) { | |
bd681513 | 8650 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
8651 | path->locks[level] = 0; |
8652 | } | |
8653 | return 0; | |
8654 | } | |
8655 | ||
1c4850e2 | 8656 | /* |
2c016dc2 | 8657 | * helper to process tree block pointer. |
1c4850e2 YZ |
8658 | * |
8659 | * when wc->stage == DROP_REFERENCE, this function checks | |
8660 | * reference count of the block pointed to. if the block | |
8661 | * is shared and we need update back refs for the subtree | |
8662 | * rooted at the block, this function changes wc->stage to | |
8663 | * UPDATE_BACKREF. if the block is shared and there is no | |
8664 | * need to update back, this function drops the reference | |
8665 | * to the block. | |
8666 | * | |
8667 | * NOTE: return value 1 means we should stop walking down. | |
8668 | */ | |
8669 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, | |
8670 | struct btrfs_root *root, | |
8671 | struct btrfs_path *path, | |
94fcca9f | 8672 | struct walk_control *wc, int *lookup_info) |
1c4850e2 | 8673 | { |
0b246afa | 8674 | struct btrfs_fs_info *fs_info = root->fs_info; |
1c4850e2 YZ |
8675 | u64 bytenr; |
8676 | u64 generation; | |
8677 | u64 parent; | |
8678 | u32 blocksize; | |
8679 | struct btrfs_key key; | |
8680 | struct extent_buffer *next; | |
8681 | int level = wc->level; | |
8682 | int reada = 0; | |
8683 | int ret = 0; | |
1152651a | 8684 | bool need_account = false; |
1c4850e2 YZ |
8685 | |
8686 | generation = btrfs_node_ptr_generation(path->nodes[level], | |
8687 | path->slots[level]); | |
8688 | /* | |
8689 | * if the lower level block was created before the snapshot | |
8690 | * was created, we know there is no need to update back refs | |
8691 | * for the subtree | |
8692 | */ | |
8693 | if (wc->stage == UPDATE_BACKREF && | |
94fcca9f YZ |
8694 | generation <= root->root_key.offset) { |
8695 | *lookup_info = 1; | |
1c4850e2 | 8696 | return 1; |
94fcca9f | 8697 | } |
1c4850e2 YZ |
8698 | |
8699 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); | |
0b246afa | 8700 | blocksize = fs_info->nodesize; |
1c4850e2 | 8701 | |
0b246afa | 8702 | next = find_extent_buffer(fs_info, bytenr); |
1c4850e2 | 8703 | if (!next) { |
a83fffb7 | 8704 | next = btrfs_find_create_tree_block(root, bytenr); |
c871b0f2 LB |
8705 | if (IS_ERR(next)) |
8706 | return PTR_ERR(next); | |
8707 | ||
b2aaaa3b JB |
8708 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, next, |
8709 | level - 1); | |
1c4850e2 YZ |
8710 | reada = 1; |
8711 | } | |
8712 | btrfs_tree_lock(next); | |
8713 | btrfs_set_lock_blocking(next); | |
8714 | ||
3173a18f | 8715 | ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1, |
94fcca9f YZ |
8716 | &wc->refs[level - 1], |
8717 | &wc->flags[level - 1]); | |
4867268c JB |
8718 | if (ret < 0) |
8719 | goto out_unlock; | |
79787eaa | 8720 | |
c2cf52eb | 8721 | if (unlikely(wc->refs[level - 1] == 0)) { |
0b246afa | 8722 | btrfs_err(fs_info, "Missing references."); |
4867268c JB |
8723 | ret = -EIO; |
8724 | goto out_unlock; | |
c2cf52eb | 8725 | } |
94fcca9f | 8726 | *lookup_info = 0; |
1c4850e2 | 8727 | |
94fcca9f | 8728 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 | 8729 | if (wc->refs[level - 1] > 1) { |
1152651a | 8730 | need_account = true; |
94fcca9f YZ |
8731 | if (level == 1 && |
8732 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
8733 | goto skip; | |
8734 | ||
1c4850e2 YZ |
8735 | if (!wc->update_ref || |
8736 | generation <= root->root_key.offset) | |
8737 | goto skip; | |
8738 | ||
8739 | btrfs_node_key_to_cpu(path->nodes[level], &key, | |
8740 | path->slots[level]); | |
8741 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); | |
8742 | if (ret < 0) | |
8743 | goto skip; | |
8744 | ||
8745 | wc->stage = UPDATE_BACKREF; | |
8746 | wc->shared_level = level - 1; | |
8747 | } | |
94fcca9f YZ |
8748 | } else { |
8749 | if (level == 1 && | |
8750 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
8751 | goto skip; | |
1c4850e2 YZ |
8752 | } |
8753 | ||
b9fab919 | 8754 | if (!btrfs_buffer_uptodate(next, generation, 0)) { |
1c4850e2 YZ |
8755 | btrfs_tree_unlock(next); |
8756 | free_extent_buffer(next); | |
8757 | next = NULL; | |
94fcca9f | 8758 | *lookup_info = 1; |
1c4850e2 YZ |
8759 | } |
8760 | ||
8761 | if (!next) { | |
8762 | if (reada && level == 1) | |
8763 | reada_walk_down(trans, root, wc, path); | |
ce86cd59 | 8764 | next = read_tree_block(root, bytenr, generation); |
64c043de LB |
8765 | if (IS_ERR(next)) { |
8766 | return PTR_ERR(next); | |
8767 | } else if (!extent_buffer_uptodate(next)) { | |
416bc658 | 8768 | free_extent_buffer(next); |
97d9a8a4 | 8769 | return -EIO; |
416bc658 | 8770 | } |
1c4850e2 YZ |
8771 | btrfs_tree_lock(next); |
8772 | btrfs_set_lock_blocking(next); | |
8773 | } | |
8774 | ||
8775 | level--; | |
4867268c JB |
8776 | ASSERT(level == btrfs_header_level(next)); |
8777 | if (level != btrfs_header_level(next)) { | |
8778 | btrfs_err(root->fs_info, "mismatched level"); | |
8779 | ret = -EIO; | |
8780 | goto out_unlock; | |
8781 | } | |
1c4850e2 YZ |
8782 | path->nodes[level] = next; |
8783 | path->slots[level] = 0; | |
bd681513 | 8784 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
1c4850e2 YZ |
8785 | wc->level = level; |
8786 | if (wc->level == 1) | |
8787 | wc->reada_slot = 0; | |
8788 | return 0; | |
8789 | skip: | |
8790 | wc->refs[level - 1] = 0; | |
8791 | wc->flags[level - 1] = 0; | |
94fcca9f YZ |
8792 | if (wc->stage == DROP_REFERENCE) { |
8793 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { | |
8794 | parent = path->nodes[level]->start; | |
8795 | } else { | |
4867268c | 8796 | ASSERT(root->root_key.objectid == |
94fcca9f | 8797 | btrfs_header_owner(path->nodes[level])); |
4867268c JB |
8798 | if (root->root_key.objectid != |
8799 | btrfs_header_owner(path->nodes[level])) { | |
8800 | btrfs_err(root->fs_info, | |
8801 | "mismatched block owner"); | |
8802 | ret = -EIO; | |
8803 | goto out_unlock; | |
8804 | } | |
94fcca9f YZ |
8805 | parent = 0; |
8806 | } | |
1c4850e2 | 8807 | |
1152651a | 8808 | if (need_account) { |
33d1f05c QW |
8809 | ret = btrfs_qgroup_trace_subtree(trans, root, next, |
8810 | generation, level - 1); | |
1152651a | 8811 | if (ret) { |
0b246afa | 8812 | btrfs_err_rl(fs_info, |
5d163e0e JM |
8813 | "Error %d accounting shared subtree. Quota is out of sync, rescan required.", |
8814 | ret); | |
1152651a MF |
8815 | } |
8816 | } | |
94fcca9f | 8817 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
b06c4bf5 | 8818 | root->root_key.objectid, level - 1, 0); |
4867268c JB |
8819 | if (ret) |
8820 | goto out_unlock; | |
1c4850e2 | 8821 | } |
4867268c JB |
8822 | |
8823 | *lookup_info = 1; | |
8824 | ret = 1; | |
8825 | ||
8826 | out_unlock: | |
1c4850e2 YZ |
8827 | btrfs_tree_unlock(next); |
8828 | free_extent_buffer(next); | |
4867268c JB |
8829 | |
8830 | return ret; | |
1c4850e2 YZ |
8831 | } |
8832 | ||
2c47e605 | 8833 | /* |
2c016dc2 | 8834 | * helper to process tree block while walking up the tree. |
2c47e605 YZ |
8835 | * |
8836 | * when wc->stage == DROP_REFERENCE, this function drops | |
8837 | * reference count on the block. | |
8838 | * | |
8839 | * when wc->stage == UPDATE_BACKREF, this function changes | |
8840 | * wc->stage back to DROP_REFERENCE if we changed wc->stage | |
8841 | * to UPDATE_BACKREF previously while processing the block. | |
8842 | * | |
8843 | * NOTE: return value 1 means we should stop walking up. | |
8844 | */ | |
8845 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, | |
8846 | struct btrfs_root *root, | |
8847 | struct btrfs_path *path, | |
8848 | struct walk_control *wc) | |
8849 | { | |
0b246afa | 8850 | struct btrfs_fs_info *fs_info = root->fs_info; |
f0486c68 | 8851 | int ret; |
2c47e605 YZ |
8852 | int level = wc->level; |
8853 | struct extent_buffer *eb = path->nodes[level]; | |
8854 | u64 parent = 0; | |
8855 | ||
8856 | if (wc->stage == UPDATE_BACKREF) { | |
8857 | BUG_ON(wc->shared_level < level); | |
8858 | if (level < wc->shared_level) | |
8859 | goto out; | |
8860 | ||
2c47e605 YZ |
8861 | ret = find_next_key(path, level + 1, &wc->update_progress); |
8862 | if (ret > 0) | |
8863 | wc->update_ref = 0; | |
8864 | ||
8865 | wc->stage = DROP_REFERENCE; | |
8866 | wc->shared_level = -1; | |
8867 | path->slots[level] = 0; | |
8868 | ||
8869 | /* | |
8870 | * check reference count again if the block isn't locked. | |
8871 | * we should start walking down the tree again if reference | |
8872 | * count is one. | |
8873 | */ | |
8874 | if (!path->locks[level]) { | |
8875 | BUG_ON(level == 0); | |
8876 | btrfs_tree_lock(eb); | |
8877 | btrfs_set_lock_blocking(eb); | |
bd681513 | 8878 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
8879 | |
8880 | ret = btrfs_lookup_extent_info(trans, root, | |
3173a18f | 8881 | eb->start, level, 1, |
2c47e605 YZ |
8882 | &wc->refs[level], |
8883 | &wc->flags[level]); | |
79787eaa JM |
8884 | if (ret < 0) { |
8885 | btrfs_tree_unlock_rw(eb, path->locks[level]); | |
3268a246 | 8886 | path->locks[level] = 0; |
79787eaa JM |
8887 | return ret; |
8888 | } | |
2c47e605 YZ |
8889 | BUG_ON(wc->refs[level] == 0); |
8890 | if (wc->refs[level] == 1) { | |
bd681513 | 8891 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
3268a246 | 8892 | path->locks[level] = 0; |
2c47e605 YZ |
8893 | return 1; |
8894 | } | |
f82d02d9 | 8895 | } |
2c47e605 | 8896 | } |
f82d02d9 | 8897 | |
2c47e605 YZ |
8898 | /* wc->stage == DROP_REFERENCE */ |
8899 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); | |
5d4f98a2 | 8900 | |
2c47e605 YZ |
8901 | if (wc->refs[level] == 1) { |
8902 | if (level == 0) { | |
8903 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
e339a6b0 | 8904 | ret = btrfs_dec_ref(trans, root, eb, 1); |
2c47e605 | 8905 | else |
e339a6b0 | 8906 | ret = btrfs_dec_ref(trans, root, eb, 0); |
79787eaa | 8907 | BUG_ON(ret); /* -ENOMEM */ |
33d1f05c | 8908 | ret = btrfs_qgroup_trace_leaf_items(trans, root, eb); |
1152651a | 8909 | if (ret) { |
0b246afa | 8910 | btrfs_err_rl(fs_info, |
5d163e0e JM |
8911 | "error %d accounting leaf items. Quota is out of sync, rescan required.", |
8912 | ret); | |
1152651a | 8913 | } |
2c47e605 YZ |
8914 | } |
8915 | /* make block locked assertion in clean_tree_block happy */ | |
8916 | if (!path->locks[level] && | |
8917 | btrfs_header_generation(eb) == trans->transid) { | |
8918 | btrfs_tree_lock(eb); | |
8919 | btrfs_set_lock_blocking(eb); | |
bd681513 | 8920 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 | 8921 | } |
0b246afa | 8922 | clean_tree_block(trans, fs_info, eb); |
2c47e605 YZ |
8923 | } |
8924 | ||
8925 | if (eb == root->node) { | |
8926 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
8927 | parent = eb->start; | |
8928 | else | |
8929 | BUG_ON(root->root_key.objectid != | |
8930 | btrfs_header_owner(eb)); | |
8931 | } else { | |
8932 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
8933 | parent = path->nodes[level + 1]->start; | |
8934 | else | |
8935 | BUG_ON(root->root_key.objectid != | |
8936 | btrfs_header_owner(path->nodes[level + 1])); | |
f82d02d9 | 8937 | } |
f82d02d9 | 8938 | |
5581a51a | 8939 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
2c47e605 YZ |
8940 | out: |
8941 | wc->refs[level] = 0; | |
8942 | wc->flags[level] = 0; | |
f0486c68 | 8943 | return 0; |
2c47e605 YZ |
8944 | } |
8945 | ||
8946 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |
8947 | struct btrfs_root *root, | |
8948 | struct btrfs_path *path, | |
8949 | struct walk_control *wc) | |
8950 | { | |
2c47e605 | 8951 | int level = wc->level; |
94fcca9f | 8952 | int lookup_info = 1; |
2c47e605 YZ |
8953 | int ret; |
8954 | ||
8955 | while (level >= 0) { | |
94fcca9f | 8956 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
2c47e605 YZ |
8957 | if (ret > 0) |
8958 | break; | |
8959 | ||
8960 | if (level == 0) | |
8961 | break; | |
8962 | ||
7a7965f8 YZ |
8963 | if (path->slots[level] >= |
8964 | btrfs_header_nritems(path->nodes[level])) | |
8965 | break; | |
8966 | ||
94fcca9f | 8967 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
1c4850e2 YZ |
8968 | if (ret > 0) { |
8969 | path->slots[level]++; | |
8970 | continue; | |
90d2c51d MX |
8971 | } else if (ret < 0) |
8972 | return ret; | |
1c4850e2 | 8973 | level = wc->level; |
f82d02d9 | 8974 | } |
f82d02d9 YZ |
8975 | return 0; |
8976 | } | |
8977 | ||
d397712b | 8978 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
98ed5174 | 8979 | struct btrfs_root *root, |
f82d02d9 | 8980 | struct btrfs_path *path, |
2c47e605 | 8981 | struct walk_control *wc, int max_level) |
20524f02 | 8982 | { |
2c47e605 | 8983 | int level = wc->level; |
20524f02 | 8984 | int ret; |
9f3a7427 | 8985 | |
2c47e605 YZ |
8986 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
8987 | while (level < max_level && path->nodes[level]) { | |
8988 | wc->level = level; | |
8989 | if (path->slots[level] + 1 < | |
8990 | btrfs_header_nritems(path->nodes[level])) { | |
8991 | path->slots[level]++; | |
20524f02 CM |
8992 | return 0; |
8993 | } else { | |
2c47e605 YZ |
8994 | ret = walk_up_proc(trans, root, path, wc); |
8995 | if (ret > 0) | |
8996 | return 0; | |
bd56b302 | 8997 | |
2c47e605 | 8998 | if (path->locks[level]) { |
bd681513 CM |
8999 | btrfs_tree_unlock_rw(path->nodes[level], |
9000 | path->locks[level]); | |
2c47e605 | 9001 | path->locks[level] = 0; |
f82d02d9 | 9002 | } |
2c47e605 YZ |
9003 | free_extent_buffer(path->nodes[level]); |
9004 | path->nodes[level] = NULL; | |
9005 | level++; | |
20524f02 CM |
9006 | } |
9007 | } | |
9008 | return 1; | |
9009 | } | |
9010 | ||
9aca1d51 | 9011 | /* |
2c47e605 YZ |
9012 | * drop a subvolume tree. |
9013 | * | |
9014 | * this function traverses the tree freeing any blocks that only | |
9015 | * referenced by the tree. | |
9016 | * | |
9017 | * when a shared tree block is found. this function decreases its | |
9018 | * reference count by one. if update_ref is true, this function | |
9019 | * also make sure backrefs for the shared block and all lower level | |
9020 | * blocks are properly updated. | |
9d1a2a3a DS |
9021 | * |
9022 | * If called with for_reloc == 0, may exit early with -EAGAIN | |
9aca1d51 | 9023 | */ |
2c536799 | 9024 | int btrfs_drop_snapshot(struct btrfs_root *root, |
66d7e7f0 AJ |
9025 | struct btrfs_block_rsv *block_rsv, int update_ref, |
9026 | int for_reloc) | |
20524f02 | 9027 | { |
ab8d0fc4 | 9028 | struct btrfs_fs_info *fs_info = root->fs_info; |
5caf2a00 | 9029 | struct btrfs_path *path; |
2c47e605 | 9030 | struct btrfs_trans_handle *trans; |
ab8d0fc4 | 9031 | struct btrfs_root *tree_root = fs_info->tree_root; |
9f3a7427 | 9032 | struct btrfs_root_item *root_item = &root->root_item; |
2c47e605 YZ |
9033 | struct walk_control *wc; |
9034 | struct btrfs_key key; | |
9035 | int err = 0; | |
9036 | int ret; | |
9037 | int level; | |
d29a9f62 | 9038 | bool root_dropped = false; |
20524f02 | 9039 | |
ab8d0fc4 | 9040 | btrfs_debug(fs_info, "Drop subvolume %llu", root->objectid); |
1152651a | 9041 | |
5caf2a00 | 9042 | path = btrfs_alloc_path(); |
cb1b69f4 TI |
9043 | if (!path) { |
9044 | err = -ENOMEM; | |
9045 | goto out; | |
9046 | } | |
20524f02 | 9047 | |
2c47e605 | 9048 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
38a1a919 MF |
9049 | if (!wc) { |
9050 | btrfs_free_path(path); | |
cb1b69f4 TI |
9051 | err = -ENOMEM; |
9052 | goto out; | |
38a1a919 | 9053 | } |
2c47e605 | 9054 | |
a22285a6 | 9055 | trans = btrfs_start_transaction(tree_root, 0); |
79787eaa JM |
9056 | if (IS_ERR(trans)) { |
9057 | err = PTR_ERR(trans); | |
9058 | goto out_free; | |
9059 | } | |
98d5dc13 | 9060 | |
3fd0a558 YZ |
9061 | if (block_rsv) |
9062 | trans->block_rsv = block_rsv; | |
2c47e605 | 9063 | |
9f3a7427 | 9064 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
2c47e605 | 9065 | level = btrfs_header_level(root->node); |
5d4f98a2 YZ |
9066 | path->nodes[level] = btrfs_lock_root_node(root); |
9067 | btrfs_set_lock_blocking(path->nodes[level]); | |
9f3a7427 | 9068 | path->slots[level] = 0; |
bd681513 | 9069 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
9070 | memset(&wc->update_progress, 0, |
9071 | sizeof(wc->update_progress)); | |
9f3a7427 | 9072 | } else { |
9f3a7427 | 9073 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
2c47e605 YZ |
9074 | memcpy(&wc->update_progress, &key, |
9075 | sizeof(wc->update_progress)); | |
9076 | ||
6702ed49 | 9077 | level = root_item->drop_level; |
2c47e605 | 9078 | BUG_ON(level == 0); |
6702ed49 | 9079 | path->lowest_level = level; |
2c47e605 YZ |
9080 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
9081 | path->lowest_level = 0; | |
9082 | if (ret < 0) { | |
9083 | err = ret; | |
79787eaa | 9084 | goto out_end_trans; |
9f3a7427 | 9085 | } |
1c4850e2 | 9086 | WARN_ON(ret > 0); |
2c47e605 | 9087 | |
7d9eb12c CM |
9088 | /* |
9089 | * unlock our path, this is safe because only this | |
9090 | * function is allowed to delete this snapshot | |
9091 | */ | |
5d4f98a2 | 9092 | btrfs_unlock_up_safe(path, 0); |
2c47e605 YZ |
9093 | |
9094 | level = btrfs_header_level(root->node); | |
9095 | while (1) { | |
9096 | btrfs_tree_lock(path->nodes[level]); | |
9097 | btrfs_set_lock_blocking(path->nodes[level]); | |
fec386ac | 9098 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
9099 | |
9100 | ret = btrfs_lookup_extent_info(trans, root, | |
9101 | path->nodes[level]->start, | |
3173a18f | 9102 | level, 1, &wc->refs[level], |
2c47e605 | 9103 | &wc->flags[level]); |
79787eaa JM |
9104 | if (ret < 0) { |
9105 | err = ret; | |
9106 | goto out_end_trans; | |
9107 | } | |
2c47e605 YZ |
9108 | BUG_ON(wc->refs[level] == 0); |
9109 | ||
9110 | if (level == root_item->drop_level) | |
9111 | break; | |
9112 | ||
9113 | btrfs_tree_unlock(path->nodes[level]); | |
fec386ac | 9114 | path->locks[level] = 0; |
2c47e605 YZ |
9115 | WARN_ON(wc->refs[level] != 1); |
9116 | level--; | |
9117 | } | |
9f3a7427 | 9118 | } |
2c47e605 YZ |
9119 | |
9120 | wc->level = level; | |
9121 | wc->shared_level = -1; | |
9122 | wc->stage = DROP_REFERENCE; | |
9123 | wc->update_ref = update_ref; | |
9124 | wc->keep_locks = 0; | |
66d7e7f0 | 9125 | wc->for_reloc = for_reloc; |
0b246afa | 9126 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
2c47e605 | 9127 | |
d397712b | 9128 | while (1) { |
9d1a2a3a | 9129 | |
2c47e605 YZ |
9130 | ret = walk_down_tree(trans, root, path, wc); |
9131 | if (ret < 0) { | |
9132 | err = ret; | |
20524f02 | 9133 | break; |
2c47e605 | 9134 | } |
9aca1d51 | 9135 | |
2c47e605 YZ |
9136 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
9137 | if (ret < 0) { | |
9138 | err = ret; | |
20524f02 | 9139 | break; |
2c47e605 YZ |
9140 | } |
9141 | ||
9142 | if (ret > 0) { | |
9143 | BUG_ON(wc->stage != DROP_REFERENCE); | |
e7a84565 CM |
9144 | break; |
9145 | } | |
2c47e605 YZ |
9146 | |
9147 | if (wc->stage == DROP_REFERENCE) { | |
9148 | level = wc->level; | |
9149 | btrfs_node_key(path->nodes[level], | |
9150 | &root_item->drop_progress, | |
9151 | path->slots[level]); | |
9152 | root_item->drop_level = level; | |
9153 | } | |
9154 | ||
9155 | BUG_ON(wc->level == 0); | |
3c8f2422 JB |
9156 | if (btrfs_should_end_transaction(trans, tree_root) || |
9157 | (!for_reloc && btrfs_need_cleaner_sleep(root))) { | |
2c47e605 YZ |
9158 | ret = btrfs_update_root(trans, tree_root, |
9159 | &root->root_key, | |
9160 | root_item); | |
79787eaa | 9161 | if (ret) { |
66642832 | 9162 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9163 | err = ret; |
9164 | goto out_end_trans; | |
9165 | } | |
2c47e605 | 9166 | |
3fd0a558 | 9167 | btrfs_end_transaction_throttle(trans, tree_root); |
3c8f2422 | 9168 | if (!for_reloc && btrfs_need_cleaner_sleep(root)) { |
ab8d0fc4 JM |
9169 | btrfs_debug(fs_info, |
9170 | "drop snapshot early exit"); | |
3c8f2422 JB |
9171 | err = -EAGAIN; |
9172 | goto out_free; | |
9173 | } | |
9174 | ||
a22285a6 | 9175 | trans = btrfs_start_transaction(tree_root, 0); |
79787eaa JM |
9176 | if (IS_ERR(trans)) { |
9177 | err = PTR_ERR(trans); | |
9178 | goto out_free; | |
9179 | } | |
3fd0a558 YZ |
9180 | if (block_rsv) |
9181 | trans->block_rsv = block_rsv; | |
c3e69d58 | 9182 | } |
20524f02 | 9183 | } |
b3b4aa74 | 9184 | btrfs_release_path(path); |
79787eaa JM |
9185 | if (err) |
9186 | goto out_end_trans; | |
2c47e605 YZ |
9187 | |
9188 | ret = btrfs_del_root(trans, tree_root, &root->root_key); | |
79787eaa | 9189 | if (ret) { |
66642832 | 9190 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9191 | goto out_end_trans; |
9192 | } | |
2c47e605 | 9193 | |
76dda93c | 9194 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
cb517eab MX |
9195 | ret = btrfs_find_root(tree_root, &root->root_key, path, |
9196 | NULL, NULL); | |
79787eaa | 9197 | if (ret < 0) { |
66642832 | 9198 | btrfs_abort_transaction(trans, ret); |
79787eaa JM |
9199 | err = ret; |
9200 | goto out_end_trans; | |
9201 | } else if (ret > 0) { | |
84cd948c JB |
9202 | /* if we fail to delete the orphan item this time |
9203 | * around, it'll get picked up the next time. | |
9204 | * | |
9205 | * The most common failure here is just -ENOENT. | |
9206 | */ | |
9207 | btrfs_del_orphan_item(trans, tree_root, | |
9208 | root->root_key.objectid); | |
76dda93c YZ |
9209 | } |
9210 | } | |
9211 | ||
27cdeb70 | 9212 | if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) { |
2b9dbef2 | 9213 | btrfs_add_dropped_root(trans, root); |
76dda93c YZ |
9214 | } else { |
9215 | free_extent_buffer(root->node); | |
9216 | free_extent_buffer(root->commit_root); | |
b0feb9d9 | 9217 | btrfs_put_fs_root(root); |
76dda93c | 9218 | } |
d29a9f62 | 9219 | root_dropped = true; |
79787eaa | 9220 | out_end_trans: |
3fd0a558 | 9221 | btrfs_end_transaction_throttle(trans, tree_root); |
79787eaa | 9222 | out_free: |
2c47e605 | 9223 | kfree(wc); |
5caf2a00 | 9224 | btrfs_free_path(path); |
cb1b69f4 | 9225 | out: |
d29a9f62 JB |
9226 | /* |
9227 | * So if we need to stop dropping the snapshot for whatever reason we | |
9228 | * need to make sure to add it back to the dead root list so that we | |
9229 | * keep trying to do the work later. This also cleans up roots if we | |
9230 | * don't have it in the radix (like when we recover after a power fail | |
9231 | * or unmount) so we don't leak memory. | |
9232 | */ | |
b37b39cd | 9233 | if (!for_reloc && root_dropped == false) |
d29a9f62 | 9234 | btrfs_add_dead_root(root); |
90515e7f | 9235 | if (err && err != -EAGAIN) |
ab8d0fc4 | 9236 | btrfs_handle_fs_error(fs_info, err, NULL); |
2c536799 | 9237 | return err; |
20524f02 | 9238 | } |
9078a3e1 | 9239 | |
2c47e605 YZ |
9240 | /* |
9241 | * drop subtree rooted at tree block 'node'. | |
9242 | * | |
9243 | * NOTE: this function will unlock and release tree block 'node' | |
66d7e7f0 | 9244 | * only used by relocation code |
2c47e605 | 9245 | */ |
f82d02d9 YZ |
9246 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
9247 | struct btrfs_root *root, | |
9248 | struct extent_buffer *node, | |
9249 | struct extent_buffer *parent) | |
9250 | { | |
0b246afa | 9251 | struct btrfs_fs_info *fs_info = root->fs_info; |
f82d02d9 | 9252 | struct btrfs_path *path; |
2c47e605 | 9253 | struct walk_control *wc; |
f82d02d9 YZ |
9254 | int level; |
9255 | int parent_level; | |
9256 | int ret = 0; | |
9257 | int wret; | |
9258 | ||
2c47e605 YZ |
9259 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
9260 | ||
f82d02d9 | 9261 | path = btrfs_alloc_path(); |
db5b493a TI |
9262 | if (!path) |
9263 | return -ENOMEM; | |
f82d02d9 | 9264 | |
2c47e605 | 9265 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
db5b493a TI |
9266 | if (!wc) { |
9267 | btrfs_free_path(path); | |
9268 | return -ENOMEM; | |
9269 | } | |
2c47e605 | 9270 | |
b9447ef8 | 9271 | btrfs_assert_tree_locked(parent); |
f82d02d9 YZ |
9272 | parent_level = btrfs_header_level(parent); |
9273 | extent_buffer_get(parent); | |
9274 | path->nodes[parent_level] = parent; | |
9275 | path->slots[parent_level] = btrfs_header_nritems(parent); | |
9276 | ||
b9447ef8 | 9277 | btrfs_assert_tree_locked(node); |
f82d02d9 | 9278 | level = btrfs_header_level(node); |
f82d02d9 YZ |
9279 | path->nodes[level] = node; |
9280 | path->slots[level] = 0; | |
bd681513 | 9281 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
9282 | |
9283 | wc->refs[parent_level] = 1; | |
9284 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
9285 | wc->level = level; | |
9286 | wc->shared_level = -1; | |
9287 | wc->stage = DROP_REFERENCE; | |
9288 | wc->update_ref = 0; | |
9289 | wc->keep_locks = 1; | |
66d7e7f0 | 9290 | wc->for_reloc = 1; |
0b246afa | 9291 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info); |
f82d02d9 YZ |
9292 | |
9293 | while (1) { | |
2c47e605 YZ |
9294 | wret = walk_down_tree(trans, root, path, wc); |
9295 | if (wret < 0) { | |
f82d02d9 | 9296 | ret = wret; |
f82d02d9 | 9297 | break; |
2c47e605 | 9298 | } |
f82d02d9 | 9299 | |
2c47e605 | 9300 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
f82d02d9 YZ |
9301 | if (wret < 0) |
9302 | ret = wret; | |
9303 | if (wret != 0) | |
9304 | break; | |
9305 | } | |
9306 | ||
2c47e605 | 9307 | kfree(wc); |
f82d02d9 YZ |
9308 | btrfs_free_path(path); |
9309 | return ret; | |
9310 | } | |
9311 | ||
6202df69 | 9312 | static u64 update_block_group_flags(struct btrfs_fs_info *fs_info, u64 flags) |
ec44a35c CM |
9313 | { |
9314 | u64 num_devices; | |
fc67c450 | 9315 | u64 stripped; |
e4d8ec0f | 9316 | |
fc67c450 ID |
9317 | /* |
9318 | * if restripe for this chunk_type is on pick target profile and | |
9319 | * return, otherwise do the usual balance | |
9320 | */ | |
6202df69 | 9321 | stripped = get_restripe_target(fs_info, flags); |
fc67c450 ID |
9322 | if (stripped) |
9323 | return extended_to_chunk(stripped); | |
e4d8ec0f | 9324 | |
6202df69 | 9325 | num_devices = fs_info->fs_devices->rw_devices; |
cd02dca5 | 9326 | |
fc67c450 | 9327 | stripped = BTRFS_BLOCK_GROUP_RAID0 | |
53b381b3 | 9328 | BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 | |
fc67c450 ID |
9329 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; |
9330 | ||
ec44a35c CM |
9331 | if (num_devices == 1) { |
9332 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
9333 | stripped = flags & ~stripped; | |
9334 | ||
9335 | /* turn raid0 into single device chunks */ | |
9336 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
9337 | return stripped; | |
9338 | ||
9339 | /* turn mirroring into duplication */ | |
9340 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
9341 | BTRFS_BLOCK_GROUP_RAID10)) | |
9342 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
ec44a35c CM |
9343 | } else { |
9344 | /* they already had raid on here, just return */ | |
ec44a35c CM |
9345 | if (flags & stripped) |
9346 | return flags; | |
9347 | ||
9348 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
9349 | stripped = flags & ~stripped; | |
9350 | ||
9351 | /* switch duplicated blocks with raid1 */ | |
9352 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
9353 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
9354 | ||
e3176ca2 | 9355 | /* this is drive concat, leave it alone */ |
ec44a35c | 9356 | } |
e3176ca2 | 9357 | |
ec44a35c CM |
9358 | return flags; |
9359 | } | |
9360 | ||
868f401a | 9361 | static int inc_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
0ef3e66b | 9362 | { |
f0486c68 YZ |
9363 | struct btrfs_space_info *sinfo = cache->space_info; |
9364 | u64 num_bytes; | |
199c36ea | 9365 | u64 min_allocable_bytes; |
f0486c68 | 9366 | int ret = -ENOSPC; |
0ef3e66b | 9367 | |
199c36ea MX |
9368 | /* |
9369 | * We need some metadata space and system metadata space for | |
9370 | * allocating chunks in some corner cases until we force to set | |
9371 | * it to be readonly. | |
9372 | */ | |
9373 | if ((sinfo->flags & | |
9374 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && | |
9375 | !force) | |
ee22184b | 9376 | min_allocable_bytes = SZ_1M; |
199c36ea MX |
9377 | else |
9378 | min_allocable_bytes = 0; | |
9379 | ||
f0486c68 YZ |
9380 | spin_lock(&sinfo->lock); |
9381 | spin_lock(&cache->lock); | |
61cfea9b W |
9382 | |
9383 | if (cache->ro) { | |
868f401a | 9384 | cache->ro++; |
61cfea9b W |
9385 | ret = 0; |
9386 | goto out; | |
9387 | } | |
9388 | ||
f0486c68 YZ |
9389 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
9390 | cache->bytes_super - btrfs_block_group_used(&cache->item); | |
9391 | ||
9392 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + | |
37be25bc JB |
9393 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
9394 | min_allocable_bytes <= sinfo->total_bytes) { | |
f0486c68 | 9395 | sinfo->bytes_readonly += num_bytes; |
868f401a | 9396 | cache->ro++; |
633c0aad | 9397 | list_add_tail(&cache->ro_list, &sinfo->ro_bgs); |
f0486c68 YZ |
9398 | ret = 0; |
9399 | } | |
61cfea9b | 9400 | out: |
f0486c68 YZ |
9401 | spin_unlock(&cache->lock); |
9402 | spin_unlock(&sinfo->lock); | |
9403 | return ret; | |
9404 | } | |
7d9eb12c | 9405 | |
868f401a | 9406 | int btrfs_inc_block_group_ro(struct btrfs_root *root, |
f0486c68 | 9407 | struct btrfs_block_group_cache *cache) |
c286ac48 | 9408 | |
f0486c68 | 9409 | { |
0b246afa | 9410 | struct btrfs_fs_info *fs_info = root->fs_info; |
f0486c68 YZ |
9411 | struct btrfs_trans_handle *trans; |
9412 | u64 alloc_flags; | |
9413 | int ret; | |
7d9eb12c | 9414 | |
1bbc621e | 9415 | again: |
ff5714cc | 9416 | trans = btrfs_join_transaction(root); |
79787eaa JM |
9417 | if (IS_ERR(trans)) |
9418 | return PTR_ERR(trans); | |
5d4f98a2 | 9419 | |
1bbc621e CM |
9420 | /* |
9421 | * we're not allowed to set block groups readonly after the dirty | |
9422 | * block groups cache has started writing. If it already started, | |
9423 | * back off and let this transaction commit | |
9424 | */ | |
0b246afa | 9425 | mutex_lock(&fs_info->ro_block_group_mutex); |
3204d33c | 9426 | if (test_bit(BTRFS_TRANS_DIRTY_BG_RUN, &trans->transaction->flags)) { |
1bbc621e CM |
9427 | u64 transid = trans->transid; |
9428 | ||
0b246afa | 9429 | mutex_unlock(&fs_info->ro_block_group_mutex); |
1bbc621e CM |
9430 | btrfs_end_transaction(trans, root); |
9431 | ||
9432 | ret = btrfs_wait_for_commit(root, transid); | |
9433 | if (ret) | |
9434 | return ret; | |
9435 | goto again; | |
9436 | } | |
9437 | ||
153c35b6 CM |
9438 | /* |
9439 | * if we are changing raid levels, try to allocate a corresponding | |
9440 | * block group with the new raid level. | |
9441 | */ | |
0b246afa | 9442 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
153c35b6 CM |
9443 | if (alloc_flags != cache->flags) { |
9444 | ret = do_chunk_alloc(trans, root, alloc_flags, | |
9445 | CHUNK_ALLOC_FORCE); | |
9446 | /* | |
9447 | * ENOSPC is allowed here, we may have enough space | |
9448 | * already allocated at the new raid level to | |
9449 | * carry on | |
9450 | */ | |
9451 | if (ret == -ENOSPC) | |
9452 | ret = 0; | |
9453 | if (ret < 0) | |
9454 | goto out; | |
9455 | } | |
1bbc621e | 9456 | |
868f401a | 9457 | ret = inc_block_group_ro(cache, 0); |
f0486c68 YZ |
9458 | if (!ret) |
9459 | goto out; | |
9460 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); | |
698d0082 | 9461 | ret = do_chunk_alloc(trans, root, alloc_flags, |
0e4f8f88 | 9462 | CHUNK_ALLOC_FORCE); |
f0486c68 YZ |
9463 | if (ret < 0) |
9464 | goto out; | |
868f401a | 9465 | ret = inc_block_group_ro(cache, 0); |
f0486c68 | 9466 | out: |
2f081088 | 9467 | if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
0b246afa JM |
9468 | alloc_flags = update_block_group_flags(fs_info, cache->flags); |
9469 | lock_chunks(fs_info); | |
4617ea3a | 9470 | check_system_chunk(trans, root, alloc_flags); |
0b246afa | 9471 | unlock_chunks(fs_info); |
2f081088 | 9472 | } |
0b246afa | 9473 | mutex_unlock(&fs_info->ro_block_group_mutex); |
2f081088 | 9474 | |
f0486c68 YZ |
9475 | btrfs_end_transaction(trans, root); |
9476 | return ret; | |
9477 | } | |
5d4f98a2 | 9478 | |
c87f08ca CM |
9479 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
9480 | struct btrfs_root *root, u64 type) | |
9481 | { | |
9482 | u64 alloc_flags = get_alloc_profile(root, type); | |
698d0082 | 9483 | return do_chunk_alloc(trans, root, alloc_flags, |
0e4f8f88 | 9484 | CHUNK_ALLOC_FORCE); |
c87f08ca CM |
9485 | } |
9486 | ||
6d07bcec MX |
9487 | /* |
9488 | * helper to account the unused space of all the readonly block group in the | |
633c0aad | 9489 | * space_info. takes mirrors into account. |
6d07bcec | 9490 | */ |
633c0aad | 9491 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) |
6d07bcec MX |
9492 | { |
9493 | struct btrfs_block_group_cache *block_group; | |
9494 | u64 free_bytes = 0; | |
9495 | int factor; | |
9496 | ||
01327610 | 9497 | /* It's df, we don't care if it's racy */ |
633c0aad JB |
9498 | if (list_empty(&sinfo->ro_bgs)) |
9499 | return 0; | |
9500 | ||
9501 | spin_lock(&sinfo->lock); | |
9502 | list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) { | |
6d07bcec MX |
9503 | spin_lock(&block_group->lock); |
9504 | ||
9505 | if (!block_group->ro) { | |
9506 | spin_unlock(&block_group->lock); | |
9507 | continue; | |
9508 | } | |
9509 | ||
9510 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
9511 | BTRFS_BLOCK_GROUP_RAID10 | | |
9512 | BTRFS_BLOCK_GROUP_DUP)) | |
9513 | factor = 2; | |
9514 | else | |
9515 | factor = 1; | |
9516 | ||
9517 | free_bytes += (block_group->key.offset - | |
9518 | btrfs_block_group_used(&block_group->item)) * | |
9519 | factor; | |
9520 | ||
9521 | spin_unlock(&block_group->lock); | |
9522 | } | |
6d07bcec MX |
9523 | spin_unlock(&sinfo->lock); |
9524 | ||
9525 | return free_bytes; | |
9526 | } | |
9527 | ||
868f401a | 9528 | void btrfs_dec_block_group_ro(struct btrfs_root *root, |
f0486c68 | 9529 | struct btrfs_block_group_cache *cache) |
5d4f98a2 | 9530 | { |
f0486c68 YZ |
9531 | struct btrfs_space_info *sinfo = cache->space_info; |
9532 | u64 num_bytes; | |
9533 | ||
9534 | BUG_ON(!cache->ro); | |
9535 | ||
9536 | spin_lock(&sinfo->lock); | |
9537 | spin_lock(&cache->lock); | |
868f401a Z |
9538 | if (!--cache->ro) { |
9539 | num_bytes = cache->key.offset - cache->reserved - | |
9540 | cache->pinned - cache->bytes_super - | |
9541 | btrfs_block_group_used(&cache->item); | |
9542 | sinfo->bytes_readonly -= num_bytes; | |
9543 | list_del_init(&cache->ro_list); | |
9544 | } | |
f0486c68 YZ |
9545 | spin_unlock(&cache->lock); |
9546 | spin_unlock(&sinfo->lock); | |
5d4f98a2 YZ |
9547 | } |
9548 | ||
ba1bf481 JB |
9549 | /* |
9550 | * checks to see if its even possible to relocate this block group. | |
9551 | * | |
9552 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its | |
9553 | * ok to go ahead and try. | |
9554 | */ | |
6bccf3ab | 9555 | int btrfs_can_relocate(struct btrfs_fs_info *fs_info, u64 bytenr) |
1a40e23b | 9556 | { |
6bccf3ab | 9557 | struct btrfs_root *root = fs_info->extent_root; |
ba1bf481 JB |
9558 | struct btrfs_block_group_cache *block_group; |
9559 | struct btrfs_space_info *space_info; | |
0b246afa | 9560 | struct btrfs_fs_devices *fs_devices = fs_info->fs_devices; |
ba1bf481 | 9561 | struct btrfs_device *device; |
6df9a95e | 9562 | struct btrfs_trans_handle *trans; |
cdcb725c | 9563 | u64 min_free; |
6719db6a JB |
9564 | u64 dev_min = 1; |
9565 | u64 dev_nr = 0; | |
4a5e98f5 | 9566 | u64 target; |
0305bc27 | 9567 | int debug; |
cdcb725c | 9568 | int index; |
ba1bf481 JB |
9569 | int full = 0; |
9570 | int ret = 0; | |
1a40e23b | 9571 | |
0b246afa | 9572 | debug = btrfs_test_opt(fs_info, ENOSPC_DEBUG); |
0305bc27 | 9573 | |
0b246afa | 9574 | block_group = btrfs_lookup_block_group(fs_info, bytenr); |
1a40e23b | 9575 | |
ba1bf481 | 9576 | /* odd, couldn't find the block group, leave it alone */ |
0305bc27 QW |
9577 | if (!block_group) { |
9578 | if (debug) | |
0b246afa | 9579 | btrfs_warn(fs_info, |
0305bc27 QW |
9580 | "can't find block group for bytenr %llu", |
9581 | bytenr); | |
ba1bf481 | 9582 | return -1; |
0305bc27 | 9583 | } |
1a40e23b | 9584 | |
cdcb725c | 9585 | min_free = btrfs_block_group_used(&block_group->item); |
9586 | ||
ba1bf481 | 9587 | /* no bytes used, we're good */ |
cdcb725c | 9588 | if (!min_free) |
1a40e23b ZY |
9589 | goto out; |
9590 | ||
ba1bf481 JB |
9591 | space_info = block_group->space_info; |
9592 | spin_lock(&space_info->lock); | |
17d217fe | 9593 | |
ba1bf481 | 9594 | full = space_info->full; |
17d217fe | 9595 | |
ba1bf481 JB |
9596 | /* |
9597 | * if this is the last block group we have in this space, we can't | |
7ce618db CM |
9598 | * relocate it unless we're able to allocate a new chunk below. |
9599 | * | |
9600 | * Otherwise, we need to make sure we have room in the space to handle | |
9601 | * all of the extents from this block group. If we can, we're good | |
ba1bf481 | 9602 | */ |
7ce618db | 9603 | if ((space_info->total_bytes != block_group->key.offset) && |
cdcb725c | 9604 | (space_info->bytes_used + space_info->bytes_reserved + |
9605 | space_info->bytes_pinned + space_info->bytes_readonly + | |
9606 | min_free < space_info->total_bytes)) { | |
ba1bf481 JB |
9607 | spin_unlock(&space_info->lock); |
9608 | goto out; | |
17d217fe | 9609 | } |
ba1bf481 | 9610 | spin_unlock(&space_info->lock); |
ea8c2819 | 9611 | |
ba1bf481 JB |
9612 | /* |
9613 | * ok we don't have enough space, but maybe we have free space on our | |
9614 | * devices to allocate new chunks for relocation, so loop through our | |
4a5e98f5 ID |
9615 | * alloc devices and guess if we have enough space. if this block |
9616 | * group is going to be restriped, run checks against the target | |
9617 | * profile instead of the current one. | |
ba1bf481 JB |
9618 | */ |
9619 | ret = -1; | |
ea8c2819 | 9620 | |
cdcb725c | 9621 | /* |
9622 | * index: | |
9623 | * 0: raid10 | |
9624 | * 1: raid1 | |
9625 | * 2: dup | |
9626 | * 3: raid0 | |
9627 | * 4: single | |
9628 | */ | |
0b246afa | 9629 | target = get_restripe_target(fs_info, block_group->flags); |
4a5e98f5 | 9630 | if (target) { |
31e50229 | 9631 | index = __get_raid_index(extended_to_chunk(target)); |
4a5e98f5 ID |
9632 | } else { |
9633 | /* | |
9634 | * this is just a balance, so if we were marked as full | |
9635 | * we know there is no space for a new chunk | |
9636 | */ | |
0305bc27 QW |
9637 | if (full) { |
9638 | if (debug) | |
0b246afa JM |
9639 | btrfs_warn(fs_info, |
9640 | "no space to alloc new chunk for block group %llu", | |
9641 | block_group->key.objectid); | |
4a5e98f5 | 9642 | goto out; |
0305bc27 | 9643 | } |
4a5e98f5 ID |
9644 | |
9645 | index = get_block_group_index(block_group); | |
9646 | } | |
9647 | ||
e6ec716f | 9648 | if (index == BTRFS_RAID_RAID10) { |
cdcb725c | 9649 | dev_min = 4; |
6719db6a JB |
9650 | /* Divide by 2 */ |
9651 | min_free >>= 1; | |
e6ec716f | 9652 | } else if (index == BTRFS_RAID_RAID1) { |
cdcb725c | 9653 | dev_min = 2; |
e6ec716f | 9654 | } else if (index == BTRFS_RAID_DUP) { |
6719db6a JB |
9655 | /* Multiply by 2 */ |
9656 | min_free <<= 1; | |
e6ec716f | 9657 | } else if (index == BTRFS_RAID_RAID0) { |
cdcb725c | 9658 | dev_min = fs_devices->rw_devices; |
47c5713f | 9659 | min_free = div64_u64(min_free, dev_min); |
cdcb725c | 9660 | } |
9661 | ||
6df9a95e JB |
9662 | /* We need to do this so that we can look at pending chunks */ |
9663 | trans = btrfs_join_transaction(root); | |
9664 | if (IS_ERR(trans)) { | |
9665 | ret = PTR_ERR(trans); | |
9666 | goto out; | |
9667 | } | |
9668 | ||
0b246afa | 9669 | mutex_lock(&fs_info->chunk_mutex); |
ba1bf481 | 9670 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { |
7bfc837d | 9671 | u64 dev_offset; |
56bec294 | 9672 | |
ba1bf481 JB |
9673 | /* |
9674 | * check to make sure we can actually find a chunk with enough | |
9675 | * space to fit our block group in. | |
9676 | */ | |
63a212ab SB |
9677 | if (device->total_bytes > device->bytes_used + min_free && |
9678 | !device->is_tgtdev_for_dev_replace) { | |
6df9a95e | 9679 | ret = find_free_dev_extent(trans, device, min_free, |
7bfc837d | 9680 | &dev_offset, NULL); |
ba1bf481 | 9681 | if (!ret) |
cdcb725c | 9682 | dev_nr++; |
9683 | ||
9684 | if (dev_nr >= dev_min) | |
73e48b27 | 9685 | break; |
cdcb725c | 9686 | |
ba1bf481 | 9687 | ret = -1; |
725c8463 | 9688 | } |
edbd8d4e | 9689 | } |
0305bc27 | 9690 | if (debug && ret == -1) |
0b246afa JM |
9691 | btrfs_warn(fs_info, |
9692 | "no space to allocate a new chunk for block group %llu", | |
9693 | block_group->key.objectid); | |
9694 | mutex_unlock(&fs_info->chunk_mutex); | |
6df9a95e | 9695 | btrfs_end_transaction(trans, root); |
edbd8d4e | 9696 | out: |
ba1bf481 | 9697 | btrfs_put_block_group(block_group); |
edbd8d4e CM |
9698 | return ret; |
9699 | } | |
9700 | ||
6bccf3ab JM |
9701 | static int find_first_block_group(struct btrfs_fs_info *fs_info, |
9702 | struct btrfs_path *path, | |
9703 | struct btrfs_key *key) | |
0b86a832 | 9704 | { |
6bccf3ab | 9705 | struct btrfs_root *root = fs_info->extent_root; |
925baedd | 9706 | int ret = 0; |
0b86a832 CM |
9707 | struct btrfs_key found_key; |
9708 | struct extent_buffer *leaf; | |
9709 | int slot; | |
edbd8d4e | 9710 | |
0b86a832 CM |
9711 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
9712 | if (ret < 0) | |
925baedd CM |
9713 | goto out; |
9714 | ||
d397712b | 9715 | while (1) { |
0b86a832 | 9716 | slot = path->slots[0]; |
edbd8d4e | 9717 | leaf = path->nodes[0]; |
0b86a832 CM |
9718 | if (slot >= btrfs_header_nritems(leaf)) { |
9719 | ret = btrfs_next_leaf(root, path); | |
9720 | if (ret == 0) | |
9721 | continue; | |
9722 | if (ret < 0) | |
925baedd | 9723 | goto out; |
0b86a832 | 9724 | break; |
edbd8d4e | 9725 | } |
0b86a832 | 9726 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
edbd8d4e | 9727 | |
0b86a832 | 9728 | if (found_key.objectid >= key->objectid && |
925baedd | 9729 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
6fb37b75 LB |
9730 | struct extent_map_tree *em_tree; |
9731 | struct extent_map *em; | |
9732 | ||
9733 | em_tree = &root->fs_info->mapping_tree.map_tree; | |
9734 | read_lock(&em_tree->lock); | |
9735 | em = lookup_extent_mapping(em_tree, found_key.objectid, | |
9736 | found_key.offset); | |
9737 | read_unlock(&em_tree->lock); | |
9738 | if (!em) { | |
0b246afa | 9739 | btrfs_err(fs_info, |
6fb37b75 LB |
9740 | "logical %llu len %llu found bg but no related chunk", |
9741 | found_key.objectid, found_key.offset); | |
9742 | ret = -ENOENT; | |
9743 | } else { | |
9744 | ret = 0; | |
9745 | } | |
187ee58c | 9746 | free_extent_map(em); |
925baedd CM |
9747 | goto out; |
9748 | } | |
0b86a832 | 9749 | path->slots[0]++; |
edbd8d4e | 9750 | } |
925baedd | 9751 | out: |
0b86a832 | 9752 | return ret; |
edbd8d4e CM |
9753 | } |
9754 | ||
0af3d00b JB |
9755 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
9756 | { | |
9757 | struct btrfs_block_group_cache *block_group; | |
9758 | u64 last = 0; | |
9759 | ||
9760 | while (1) { | |
9761 | struct inode *inode; | |
9762 | ||
9763 | block_group = btrfs_lookup_first_block_group(info, last); | |
9764 | while (block_group) { | |
9765 | spin_lock(&block_group->lock); | |
9766 | if (block_group->iref) | |
9767 | break; | |
9768 | spin_unlock(&block_group->lock); | |
9769 | block_group = next_block_group(info->tree_root, | |
9770 | block_group); | |
9771 | } | |
9772 | if (!block_group) { | |
9773 | if (last == 0) | |
9774 | break; | |
9775 | last = 0; | |
9776 | continue; | |
9777 | } | |
9778 | ||
9779 | inode = block_group->inode; | |
9780 | block_group->iref = 0; | |
9781 | block_group->inode = NULL; | |
9782 | spin_unlock(&block_group->lock); | |
f3bca802 | 9783 | ASSERT(block_group->io_ctl.inode == NULL); |
0af3d00b JB |
9784 | iput(inode); |
9785 | last = block_group->key.objectid + block_group->key.offset; | |
9786 | btrfs_put_block_group(block_group); | |
9787 | } | |
9788 | } | |
9789 | ||
1a40e23b ZY |
9790 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
9791 | { | |
9792 | struct btrfs_block_group_cache *block_group; | |
4184ea7f | 9793 | struct btrfs_space_info *space_info; |
11833d66 | 9794 | struct btrfs_caching_control *caching_ctl; |
1a40e23b ZY |
9795 | struct rb_node *n; |
9796 | ||
9e351cc8 | 9797 | down_write(&info->commit_root_sem); |
11833d66 YZ |
9798 | while (!list_empty(&info->caching_block_groups)) { |
9799 | caching_ctl = list_entry(info->caching_block_groups.next, | |
9800 | struct btrfs_caching_control, list); | |
9801 | list_del(&caching_ctl->list); | |
9802 | put_caching_control(caching_ctl); | |
9803 | } | |
9e351cc8 | 9804 | up_write(&info->commit_root_sem); |
11833d66 | 9805 | |
47ab2a6c JB |
9806 | spin_lock(&info->unused_bgs_lock); |
9807 | while (!list_empty(&info->unused_bgs)) { | |
9808 | block_group = list_first_entry(&info->unused_bgs, | |
9809 | struct btrfs_block_group_cache, | |
9810 | bg_list); | |
9811 | list_del_init(&block_group->bg_list); | |
9812 | btrfs_put_block_group(block_group); | |
9813 | } | |
9814 | spin_unlock(&info->unused_bgs_lock); | |
9815 | ||
1a40e23b ZY |
9816 | spin_lock(&info->block_group_cache_lock); |
9817 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { | |
9818 | block_group = rb_entry(n, struct btrfs_block_group_cache, | |
9819 | cache_node); | |
1a40e23b ZY |
9820 | rb_erase(&block_group->cache_node, |
9821 | &info->block_group_cache_tree); | |
01eacb27 | 9822 | RB_CLEAR_NODE(&block_group->cache_node); |
d899e052 YZ |
9823 | spin_unlock(&info->block_group_cache_lock); |
9824 | ||
80eb234a | 9825 | down_write(&block_group->space_info->groups_sem); |
1a40e23b | 9826 | list_del(&block_group->list); |
80eb234a | 9827 | up_write(&block_group->space_info->groups_sem); |
d2fb3437 | 9828 | |
817d52f8 | 9829 | if (block_group->cached == BTRFS_CACHE_STARTED) |
11833d66 | 9830 | wait_block_group_cache_done(block_group); |
817d52f8 | 9831 | |
3c14874a JB |
9832 | /* |
9833 | * We haven't cached this block group, which means we could | |
9834 | * possibly have excluded extents on this block group. | |
9835 | */ | |
36cce922 JB |
9836 | if (block_group->cached == BTRFS_CACHE_NO || |
9837 | block_group->cached == BTRFS_CACHE_ERROR) | |
3c14874a JB |
9838 | free_excluded_extents(info->extent_root, block_group); |
9839 | ||
817d52f8 | 9840 | btrfs_remove_free_space_cache(block_group); |
f3bca802 LB |
9841 | ASSERT(list_empty(&block_group->dirty_list)); |
9842 | ASSERT(list_empty(&block_group->io_list)); | |
9843 | ASSERT(list_empty(&block_group->bg_list)); | |
9844 | ASSERT(atomic_read(&block_group->count) == 1); | |
11dfe35a | 9845 | btrfs_put_block_group(block_group); |
d899e052 YZ |
9846 | |
9847 | spin_lock(&info->block_group_cache_lock); | |
1a40e23b ZY |
9848 | } |
9849 | spin_unlock(&info->block_group_cache_lock); | |
4184ea7f CM |
9850 | |
9851 | /* now that all the block groups are freed, go through and | |
9852 | * free all the space_info structs. This is only called during | |
9853 | * the final stages of unmount, and so we know nobody is | |
9854 | * using them. We call synchronize_rcu() once before we start, | |
9855 | * just to be on the safe side. | |
9856 | */ | |
9857 | synchronize_rcu(); | |
9858 | ||
8929ecfa YZ |
9859 | release_global_block_rsv(info); |
9860 | ||
67871254 | 9861 | while (!list_empty(&info->space_info)) { |
6ab0a202 JM |
9862 | int i; |
9863 | ||
4184ea7f CM |
9864 | space_info = list_entry(info->space_info.next, |
9865 | struct btrfs_space_info, | |
9866 | list); | |
d555b6c3 JB |
9867 | |
9868 | /* | |
9869 | * Do not hide this behind enospc_debug, this is actually | |
9870 | * important and indicates a real bug if this happens. | |
9871 | */ | |
9872 | if (WARN_ON(space_info->bytes_pinned > 0 || | |
b069e0c3 | 9873 | space_info->bytes_reserved > 0 || |
d555b6c3 | 9874 | space_info->bytes_may_use > 0)) |
ab8d0fc4 | 9875 | dump_space_info(info, space_info, 0, 0); |
4184ea7f | 9876 | list_del(&space_info->list); |
6ab0a202 JM |
9877 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) { |
9878 | struct kobject *kobj; | |
c1895442 JM |
9879 | kobj = space_info->block_group_kobjs[i]; |
9880 | space_info->block_group_kobjs[i] = NULL; | |
9881 | if (kobj) { | |
6ab0a202 JM |
9882 | kobject_del(kobj); |
9883 | kobject_put(kobj); | |
9884 | } | |
9885 | } | |
9886 | kobject_del(&space_info->kobj); | |
9887 | kobject_put(&space_info->kobj); | |
4184ea7f | 9888 | } |
1a40e23b ZY |
9889 | return 0; |
9890 | } | |
9891 | ||
b742bb82 YZ |
9892 | static void __link_block_group(struct btrfs_space_info *space_info, |
9893 | struct btrfs_block_group_cache *cache) | |
9894 | { | |
9895 | int index = get_block_group_index(cache); | |
ed55b6ac | 9896 | bool first = false; |
b742bb82 YZ |
9897 | |
9898 | down_write(&space_info->groups_sem); | |
ed55b6ac JM |
9899 | if (list_empty(&space_info->block_groups[index])) |
9900 | first = true; | |
9901 | list_add_tail(&cache->list, &space_info->block_groups[index]); | |
9902 | up_write(&space_info->groups_sem); | |
9903 | ||
9904 | if (first) { | |
c1895442 | 9905 | struct raid_kobject *rkobj; |
6ab0a202 JM |
9906 | int ret; |
9907 | ||
c1895442 JM |
9908 | rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS); |
9909 | if (!rkobj) | |
9910 | goto out_err; | |
9911 | rkobj->raid_type = index; | |
9912 | kobject_init(&rkobj->kobj, &btrfs_raid_ktype); | |
9913 | ret = kobject_add(&rkobj->kobj, &space_info->kobj, | |
9914 | "%s", get_raid_name(index)); | |
6ab0a202 | 9915 | if (ret) { |
c1895442 JM |
9916 | kobject_put(&rkobj->kobj); |
9917 | goto out_err; | |
6ab0a202 | 9918 | } |
c1895442 | 9919 | space_info->block_group_kobjs[index] = &rkobj->kobj; |
6ab0a202 | 9920 | } |
c1895442 JM |
9921 | |
9922 | return; | |
9923 | out_err: | |
ab8d0fc4 JM |
9924 | btrfs_warn(cache->fs_info, |
9925 | "failed to add kobject for block cache, ignoring"); | |
b742bb82 YZ |
9926 | } |
9927 | ||
920e4a58 MX |
9928 | static struct btrfs_block_group_cache * |
9929 | btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size) | |
9930 | { | |
0b246afa | 9931 | struct btrfs_fs_info *fs_info = root->fs_info; |
920e4a58 MX |
9932 | struct btrfs_block_group_cache *cache; |
9933 | ||
9934 | cache = kzalloc(sizeof(*cache), GFP_NOFS); | |
9935 | if (!cache) | |
9936 | return NULL; | |
9937 | ||
9938 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), | |
9939 | GFP_NOFS); | |
9940 | if (!cache->free_space_ctl) { | |
9941 | kfree(cache); | |
9942 | return NULL; | |
9943 | } | |
9944 | ||
9945 | cache->key.objectid = start; | |
9946 | cache->key.offset = size; | |
9947 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; | |
9948 | ||
0b246afa JM |
9949 | cache->sectorsize = fs_info->sectorsize; |
9950 | cache->fs_info = fs_info; | |
920e4a58 | 9951 | cache->full_stripe_len = btrfs_full_stripe_len(root, |
0b246afa | 9952 | &fs_info->mapping_tree, start); |
1e144fb8 OS |
9953 | set_free_space_tree_thresholds(cache); |
9954 | ||
920e4a58 MX |
9955 | atomic_set(&cache->count, 1); |
9956 | spin_lock_init(&cache->lock); | |
e570fd27 | 9957 | init_rwsem(&cache->data_rwsem); |
920e4a58 MX |
9958 | INIT_LIST_HEAD(&cache->list); |
9959 | INIT_LIST_HEAD(&cache->cluster_list); | |
47ab2a6c | 9960 | INIT_LIST_HEAD(&cache->bg_list); |
633c0aad | 9961 | INIT_LIST_HEAD(&cache->ro_list); |
ce93ec54 | 9962 | INIT_LIST_HEAD(&cache->dirty_list); |
c9dc4c65 | 9963 | INIT_LIST_HEAD(&cache->io_list); |
920e4a58 | 9964 | btrfs_init_free_space_ctl(cache); |
04216820 | 9965 | atomic_set(&cache->trimming, 0); |
a5ed9182 | 9966 | mutex_init(&cache->free_space_lock); |
920e4a58 MX |
9967 | |
9968 | return cache; | |
9969 | } | |
9970 | ||
5b4aacef | 9971 | int btrfs_read_block_groups(struct btrfs_fs_info *info) |
9078a3e1 | 9972 | { |
5b4aacef | 9973 | struct btrfs_root *root = info->extent_root; |
9078a3e1 CM |
9974 | struct btrfs_path *path; |
9975 | int ret; | |
9078a3e1 | 9976 | struct btrfs_block_group_cache *cache; |
6324fbf3 | 9977 | struct btrfs_space_info *space_info; |
9078a3e1 CM |
9978 | struct btrfs_key key; |
9979 | struct btrfs_key found_key; | |
5f39d397 | 9980 | struct extent_buffer *leaf; |
0af3d00b JB |
9981 | int need_clear = 0; |
9982 | u64 cache_gen; | |
49303381 LB |
9983 | u64 feature; |
9984 | int mixed; | |
9985 | ||
9986 | feature = btrfs_super_incompat_flags(info->super_copy); | |
9987 | mixed = !!(feature & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS); | |
96b5179d | 9988 | |
9078a3e1 | 9989 | key.objectid = 0; |
0b86a832 | 9990 | key.offset = 0; |
962a298f | 9991 | key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
9078a3e1 CM |
9992 | path = btrfs_alloc_path(); |
9993 | if (!path) | |
9994 | return -ENOMEM; | |
e4058b54 | 9995 | path->reada = READA_FORWARD; |
9078a3e1 | 9996 | |
0b246afa JM |
9997 | cache_gen = btrfs_super_cache_generation(info->super_copy); |
9998 | if (btrfs_test_opt(info, SPACE_CACHE) && | |
9999 | btrfs_super_generation(info->super_copy) != cache_gen) | |
0af3d00b | 10000 | need_clear = 1; |
0b246afa | 10001 | if (btrfs_test_opt(info, CLEAR_CACHE)) |
88c2ba3b | 10002 | need_clear = 1; |
0af3d00b | 10003 | |
d397712b | 10004 | while (1) { |
6bccf3ab | 10005 | ret = find_first_block_group(info, path, &key); |
b742bb82 YZ |
10006 | if (ret > 0) |
10007 | break; | |
0b86a832 CM |
10008 | if (ret != 0) |
10009 | goto error; | |
920e4a58 | 10010 | |
5f39d397 CM |
10011 | leaf = path->nodes[0]; |
10012 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
920e4a58 MX |
10013 | |
10014 | cache = btrfs_create_block_group_cache(root, found_key.objectid, | |
10015 | found_key.offset); | |
9078a3e1 | 10016 | if (!cache) { |
0b86a832 | 10017 | ret = -ENOMEM; |
f0486c68 | 10018 | goto error; |
9078a3e1 | 10019 | } |
96303081 | 10020 | |
cf7c1ef6 LB |
10021 | if (need_clear) { |
10022 | /* | |
10023 | * When we mount with old space cache, we need to | |
10024 | * set BTRFS_DC_CLEAR and set dirty flag. | |
10025 | * | |
10026 | * a) Setting 'BTRFS_DC_CLEAR' makes sure that we | |
10027 | * truncate the old free space cache inode and | |
10028 | * setup a new one. | |
10029 | * b) Setting 'dirty flag' makes sure that we flush | |
10030 | * the new space cache info onto disk. | |
10031 | */ | |
0b246afa | 10032 | if (btrfs_test_opt(info, SPACE_CACHE)) |
ce93ec54 | 10033 | cache->disk_cache_state = BTRFS_DC_CLEAR; |
cf7c1ef6 | 10034 | } |
0af3d00b | 10035 | |
5f39d397 CM |
10036 | read_extent_buffer(leaf, &cache->item, |
10037 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
10038 | sizeof(cache->item)); | |
920e4a58 | 10039 | cache->flags = btrfs_block_group_flags(&cache->item); |
49303381 LB |
10040 | if (!mixed && |
10041 | ((cache->flags & BTRFS_BLOCK_GROUP_METADATA) && | |
10042 | (cache->flags & BTRFS_BLOCK_GROUP_DATA))) { | |
10043 | btrfs_err(info, | |
10044 | "bg %llu is a mixed block group but filesystem hasn't enabled mixed block groups", | |
10045 | cache->key.objectid); | |
10046 | ret = -EINVAL; | |
10047 | goto error; | |
10048 | } | |
0b86a832 | 10049 | |
9078a3e1 | 10050 | key.objectid = found_key.objectid + found_key.offset; |
b3b4aa74 | 10051 | btrfs_release_path(path); |
34d52cb6 | 10052 | |
3c14874a JB |
10053 | /* |
10054 | * We need to exclude the super stripes now so that the space | |
10055 | * info has super bytes accounted for, otherwise we'll think | |
10056 | * we have more space than we actually do. | |
10057 | */ | |
835d974f JB |
10058 | ret = exclude_super_stripes(root, cache); |
10059 | if (ret) { | |
10060 | /* | |
10061 | * We may have excluded something, so call this just in | |
10062 | * case. | |
10063 | */ | |
10064 | free_excluded_extents(root, cache); | |
920e4a58 | 10065 | btrfs_put_block_group(cache); |
835d974f JB |
10066 | goto error; |
10067 | } | |
3c14874a | 10068 | |
817d52f8 JB |
10069 | /* |
10070 | * check for two cases, either we are full, and therefore | |
10071 | * don't need to bother with the caching work since we won't | |
10072 | * find any space, or we are empty, and we can just add all | |
10073 | * the space in and be done with it. This saves us _alot_ of | |
10074 | * time, particularly in the full case. | |
10075 | */ | |
10076 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { | |
11833d66 | 10077 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 | 10078 | cache->cached = BTRFS_CACHE_FINISHED; |
1b2da372 | 10079 | free_excluded_extents(root, cache); |
817d52f8 | 10080 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
11833d66 | 10081 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 | 10082 | cache->cached = BTRFS_CACHE_FINISHED; |
0b246afa | 10083 | add_new_free_space(cache, info, |
817d52f8 JB |
10084 | found_key.objectid, |
10085 | found_key.objectid + | |
10086 | found_key.offset); | |
11833d66 | 10087 | free_excluded_extents(root, cache); |
817d52f8 | 10088 | } |
96b5179d | 10089 | |
0b246afa | 10090 | ret = btrfs_add_block_group_cache(info, cache); |
8c579fe7 JB |
10091 | if (ret) { |
10092 | btrfs_remove_free_space_cache(cache); | |
10093 | btrfs_put_block_group(cache); | |
10094 | goto error; | |
10095 | } | |
10096 | ||
0b246afa | 10097 | trace_btrfs_add_block_group(info, cache, 0); |
6324fbf3 CM |
10098 | ret = update_space_info(info, cache->flags, found_key.offset, |
10099 | btrfs_block_group_used(&cache->item), | |
e40edf2d | 10100 | cache->bytes_super, &space_info); |
8c579fe7 JB |
10101 | if (ret) { |
10102 | btrfs_remove_free_space_cache(cache); | |
10103 | spin_lock(&info->block_group_cache_lock); | |
10104 | rb_erase(&cache->cache_node, | |
10105 | &info->block_group_cache_tree); | |
01eacb27 | 10106 | RB_CLEAR_NODE(&cache->cache_node); |
8c579fe7 JB |
10107 | spin_unlock(&info->block_group_cache_lock); |
10108 | btrfs_put_block_group(cache); | |
10109 | goto error; | |
10110 | } | |
10111 | ||
6324fbf3 | 10112 | cache->space_info = space_info; |
1b2da372 | 10113 | |
b742bb82 | 10114 | __link_block_group(space_info, cache); |
0f9dd46c | 10115 | |
0b246afa | 10116 | set_avail_alloc_bits(info, cache->flags); |
47ab2a6c | 10117 | if (btrfs_chunk_readonly(root, cache->key.objectid)) { |
868f401a | 10118 | inc_block_group_ro(cache, 1); |
47ab2a6c JB |
10119 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
10120 | spin_lock(&info->unused_bgs_lock); | |
10121 | /* Should always be true but just in case. */ | |
10122 | if (list_empty(&cache->bg_list)) { | |
10123 | btrfs_get_block_group(cache); | |
10124 | list_add_tail(&cache->bg_list, | |
10125 | &info->unused_bgs); | |
10126 | } | |
10127 | spin_unlock(&info->unused_bgs_lock); | |
10128 | } | |
9078a3e1 | 10129 | } |
b742bb82 | 10130 | |
0b246afa | 10131 | list_for_each_entry_rcu(space_info, &info->space_info, list) { |
b742bb82 YZ |
10132 | if (!(get_alloc_profile(root, space_info->flags) & |
10133 | (BTRFS_BLOCK_GROUP_RAID10 | | |
10134 | BTRFS_BLOCK_GROUP_RAID1 | | |
53b381b3 DW |
10135 | BTRFS_BLOCK_GROUP_RAID5 | |
10136 | BTRFS_BLOCK_GROUP_RAID6 | | |
b742bb82 YZ |
10137 | BTRFS_BLOCK_GROUP_DUP))) |
10138 | continue; | |
10139 | /* | |
10140 | * avoid allocating from un-mirrored block group if there are | |
10141 | * mirrored block groups. | |
10142 | */ | |
1095cc0d | 10143 | list_for_each_entry(cache, |
10144 | &space_info->block_groups[BTRFS_RAID_RAID0], | |
10145 | list) | |
868f401a | 10146 | inc_block_group_ro(cache, 1); |
1095cc0d | 10147 | list_for_each_entry(cache, |
10148 | &space_info->block_groups[BTRFS_RAID_SINGLE], | |
10149 | list) | |
868f401a | 10150 | inc_block_group_ro(cache, 1); |
9078a3e1 | 10151 | } |
f0486c68 YZ |
10152 | |
10153 | init_global_block_rsv(info); | |
0b86a832 CM |
10154 | ret = 0; |
10155 | error: | |
9078a3e1 | 10156 | btrfs_free_path(path); |
0b86a832 | 10157 | return ret; |
9078a3e1 | 10158 | } |
6324fbf3 | 10159 | |
ea658bad JB |
10160 | void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans, |
10161 | struct btrfs_root *root) | |
10162 | { | |
0b246afa | 10163 | struct btrfs_fs_info *fs_info = root->fs_info; |
ea658bad | 10164 | struct btrfs_block_group_cache *block_group, *tmp; |
0b246afa | 10165 | struct btrfs_root *extent_root = fs_info->extent_root; |
ea658bad JB |
10166 | struct btrfs_block_group_item item; |
10167 | struct btrfs_key key; | |
10168 | int ret = 0; | |
d9a0540a | 10169 | bool can_flush_pending_bgs = trans->can_flush_pending_bgs; |
ea658bad | 10170 | |
d9a0540a | 10171 | trans->can_flush_pending_bgs = false; |
47ab2a6c | 10172 | list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) { |
ea658bad | 10173 | if (ret) |
c92f6be3 | 10174 | goto next; |
ea658bad JB |
10175 | |
10176 | spin_lock(&block_group->lock); | |
10177 | memcpy(&item, &block_group->item, sizeof(item)); | |
10178 | memcpy(&key, &block_group->key, sizeof(key)); | |
10179 | spin_unlock(&block_group->lock); | |
10180 | ||
10181 | ret = btrfs_insert_item(trans, extent_root, &key, &item, | |
10182 | sizeof(item)); | |
10183 | if (ret) | |
66642832 | 10184 | btrfs_abort_transaction(trans, ret); |
0b246afa JM |
10185 | ret = btrfs_finish_chunk_alloc(trans, fs_info, key.objectid, |
10186 | key.offset); | |
6df9a95e | 10187 | if (ret) |
66642832 | 10188 | btrfs_abort_transaction(trans, ret); |
0b246afa | 10189 | add_block_group_free_space(trans, fs_info, block_group); |
1e144fb8 | 10190 | /* already aborted the transaction if it failed. */ |
c92f6be3 FM |
10191 | next: |
10192 | list_del_init(&block_group->bg_list); | |
ea658bad | 10193 | } |
d9a0540a | 10194 | trans->can_flush_pending_bgs = can_flush_pending_bgs; |
ea658bad JB |
10195 | } |
10196 | ||
6324fbf3 CM |
10197 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
10198 | struct btrfs_root *root, u64 bytes_used, | |
e17cade2 | 10199 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
6324fbf3 CM |
10200 | u64 size) |
10201 | { | |
0b246afa | 10202 | struct btrfs_fs_info *fs_info = root->fs_info; |
6324fbf3 | 10203 | struct btrfs_block_group_cache *cache; |
0b246afa | 10204 | int ret; |
6324fbf3 | 10205 | |
0b246afa | 10206 | btrfs_set_log_full_commit(fs_info, trans); |
e02119d5 | 10207 | |
920e4a58 | 10208 | cache = btrfs_create_block_group_cache(root, chunk_offset, size); |
0f9dd46c JB |
10209 | if (!cache) |
10210 | return -ENOMEM; | |
34d52cb6 | 10211 | |
6324fbf3 | 10212 | btrfs_set_block_group_used(&cache->item, bytes_used); |
6324fbf3 | 10213 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
6324fbf3 CM |
10214 | btrfs_set_block_group_flags(&cache->item, type); |
10215 | ||
920e4a58 | 10216 | cache->flags = type; |
11833d66 | 10217 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 | 10218 | cache->cached = BTRFS_CACHE_FINISHED; |
1e144fb8 | 10219 | cache->needs_free_space = 1; |
835d974f JB |
10220 | ret = exclude_super_stripes(root, cache); |
10221 | if (ret) { | |
10222 | /* | |
10223 | * We may have excluded something, so call this just in | |
10224 | * case. | |
10225 | */ | |
10226 | free_excluded_extents(root, cache); | |
920e4a58 | 10227 | btrfs_put_block_group(cache); |
835d974f JB |
10228 | return ret; |
10229 | } | |
96303081 | 10230 | |
0b246afa | 10231 | add_new_free_space(cache, fs_info, chunk_offset, chunk_offset + size); |
817d52f8 | 10232 | |
11833d66 YZ |
10233 | free_excluded_extents(root, cache); |
10234 | ||
d0bd4560 JB |
10235 | #ifdef CONFIG_BTRFS_DEBUG |
10236 | if (btrfs_should_fragment_free_space(root, cache)) { | |
10237 | u64 new_bytes_used = size - bytes_used; | |
10238 | ||
10239 | bytes_used += new_bytes_used >> 1; | |
10240 | fragment_free_space(root, cache); | |
10241 | } | |
10242 | #endif | |
2e6e5183 FM |
10243 | /* |
10244 | * Call to ensure the corresponding space_info object is created and | |
10245 | * assigned to our block group, but don't update its counters just yet. | |
10246 | * We want our bg to be added to the rbtree with its ->space_info set. | |
10247 | */ | |
0b246afa | 10248 | ret = update_space_info(fs_info, cache->flags, 0, 0, 0, |
2e6e5183 FM |
10249 | &cache->space_info); |
10250 | if (ret) { | |
10251 | btrfs_remove_free_space_cache(cache); | |
10252 | btrfs_put_block_group(cache); | |
10253 | return ret; | |
10254 | } | |
10255 | ||
0b246afa | 10256 | ret = btrfs_add_block_group_cache(fs_info, cache); |
8c579fe7 JB |
10257 | if (ret) { |
10258 | btrfs_remove_free_space_cache(cache); | |
10259 | btrfs_put_block_group(cache); | |
10260 | return ret; | |
10261 | } | |
10262 | ||
2e6e5183 FM |
10263 | /* |
10264 | * Now that our block group has its ->space_info set and is inserted in | |
10265 | * the rbtree, update the space info's counters. | |
10266 | */ | |
0b246afa JM |
10267 | trace_btrfs_add_block_group(fs_info, cache, 1); |
10268 | ret = update_space_info(fs_info, cache->flags, size, bytes_used, | |
e40edf2d | 10269 | cache->bytes_super, &cache->space_info); |
8c579fe7 JB |
10270 | if (ret) { |
10271 | btrfs_remove_free_space_cache(cache); | |
0b246afa | 10272 | spin_lock(&fs_info->block_group_cache_lock); |
8c579fe7 | 10273 | rb_erase(&cache->cache_node, |
0b246afa | 10274 | &fs_info->block_group_cache_tree); |
01eacb27 | 10275 | RB_CLEAR_NODE(&cache->cache_node); |
0b246afa | 10276 | spin_unlock(&fs_info->block_group_cache_lock); |
8c579fe7 JB |
10277 | btrfs_put_block_group(cache); |
10278 | return ret; | |
10279 | } | |
0b246afa | 10280 | update_global_block_rsv(fs_info); |
1b2da372 | 10281 | |
b742bb82 | 10282 | __link_block_group(cache->space_info, cache); |
6324fbf3 | 10283 | |
47ab2a6c | 10284 | list_add_tail(&cache->bg_list, &trans->new_bgs); |
6324fbf3 | 10285 | |
0b246afa | 10286 | set_avail_alloc_bits(fs_info, type); |
6324fbf3 CM |
10287 | return 0; |
10288 | } | |
1a40e23b | 10289 | |
10ea00f5 ID |
10290 | static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
10291 | { | |
899c81ea ID |
10292 | u64 extra_flags = chunk_to_extended(flags) & |
10293 | BTRFS_EXTENDED_PROFILE_MASK; | |
10ea00f5 | 10294 | |
de98ced9 | 10295 | write_seqlock(&fs_info->profiles_lock); |
10ea00f5 ID |
10296 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
10297 | fs_info->avail_data_alloc_bits &= ~extra_flags; | |
10298 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
10299 | fs_info->avail_metadata_alloc_bits &= ~extra_flags; | |
10300 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
10301 | fs_info->avail_system_alloc_bits &= ~extra_flags; | |
de98ced9 | 10302 | write_sequnlock(&fs_info->profiles_lock); |
10ea00f5 ID |
10303 | } |
10304 | ||
1a40e23b | 10305 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, |
6bccf3ab | 10306 | struct btrfs_fs_info *fs_info, u64 group_start, |
04216820 | 10307 | struct extent_map *em) |
1a40e23b | 10308 | { |
6bccf3ab | 10309 | struct btrfs_root *root = fs_info->extent_root; |
1a40e23b ZY |
10310 | struct btrfs_path *path; |
10311 | struct btrfs_block_group_cache *block_group; | |
44fb5511 | 10312 | struct btrfs_free_cluster *cluster; |
0b246afa | 10313 | struct btrfs_root *tree_root = fs_info->tree_root; |
1a40e23b | 10314 | struct btrfs_key key; |
0af3d00b | 10315 | struct inode *inode; |
c1895442 | 10316 | struct kobject *kobj = NULL; |
1a40e23b | 10317 | int ret; |
10ea00f5 | 10318 | int index; |
89a55897 | 10319 | int factor; |
4f69cb98 | 10320 | struct btrfs_caching_control *caching_ctl = NULL; |
04216820 | 10321 | bool remove_em; |
1a40e23b | 10322 | |
6bccf3ab | 10323 | block_group = btrfs_lookup_block_group(fs_info, group_start); |
1a40e23b | 10324 | BUG_ON(!block_group); |
c146afad | 10325 | BUG_ON(!block_group->ro); |
1a40e23b | 10326 | |
9f7c43c9 | 10327 | /* |
10328 | * Free the reserved super bytes from this block group before | |
10329 | * remove it. | |
10330 | */ | |
10331 | free_excluded_extents(root, block_group); | |
10332 | ||
1a40e23b | 10333 | memcpy(&key, &block_group->key, sizeof(key)); |
10ea00f5 | 10334 | index = get_block_group_index(block_group); |
89a55897 JB |
10335 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
10336 | BTRFS_BLOCK_GROUP_RAID1 | | |
10337 | BTRFS_BLOCK_GROUP_RAID10)) | |
10338 | factor = 2; | |
10339 | else | |
10340 | factor = 1; | |
1a40e23b | 10341 | |
44fb5511 | 10342 | /* make sure this block group isn't part of an allocation cluster */ |
0b246afa | 10343 | cluster = &fs_info->data_alloc_cluster; |
44fb5511 CM |
10344 | spin_lock(&cluster->refill_lock); |
10345 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
10346 | spin_unlock(&cluster->refill_lock); | |
10347 | ||
10348 | /* | |
10349 | * make sure this block group isn't part of a metadata | |
10350 | * allocation cluster | |
10351 | */ | |
0b246afa | 10352 | cluster = &fs_info->meta_alloc_cluster; |
44fb5511 CM |
10353 | spin_lock(&cluster->refill_lock); |
10354 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
10355 | spin_unlock(&cluster->refill_lock); | |
10356 | ||
1a40e23b | 10357 | path = btrfs_alloc_path(); |
d8926bb3 MF |
10358 | if (!path) { |
10359 | ret = -ENOMEM; | |
10360 | goto out; | |
10361 | } | |
1a40e23b | 10362 | |
1bbc621e CM |
10363 | /* |
10364 | * get the inode first so any iput calls done for the io_list | |
10365 | * aren't the final iput (no unlinks allowed now) | |
10366 | */ | |
10b2f34d | 10367 | inode = lookup_free_space_inode(tree_root, block_group, path); |
1bbc621e CM |
10368 | |
10369 | mutex_lock(&trans->transaction->cache_write_mutex); | |
10370 | /* | |
10371 | * make sure our free spache cache IO is done before remove the | |
10372 | * free space inode | |
10373 | */ | |
10374 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
10375 | if (!list_empty(&block_group->io_list)) { | |
10376 | list_del_init(&block_group->io_list); | |
10377 | ||
10378 | WARN_ON(!IS_ERR(inode) && inode != block_group->io_ctl.inode); | |
10379 | ||
10380 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
afdb5718 | 10381 | btrfs_wait_cache_io(trans, block_group, path); |
1bbc621e CM |
10382 | btrfs_put_block_group(block_group); |
10383 | spin_lock(&trans->transaction->dirty_bgs_lock); | |
10384 | } | |
10385 | ||
10386 | if (!list_empty(&block_group->dirty_list)) { | |
10387 | list_del_init(&block_group->dirty_list); | |
10388 | btrfs_put_block_group(block_group); | |
10389 | } | |
10390 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
10391 | mutex_unlock(&trans->transaction->cache_write_mutex); | |
10392 | ||
0af3d00b | 10393 | if (!IS_ERR(inode)) { |
b532402e | 10394 | ret = btrfs_orphan_add(trans, inode); |
79787eaa JM |
10395 | if (ret) { |
10396 | btrfs_add_delayed_iput(inode); | |
10397 | goto out; | |
10398 | } | |
0af3d00b JB |
10399 | clear_nlink(inode); |
10400 | /* One for the block groups ref */ | |
10401 | spin_lock(&block_group->lock); | |
10402 | if (block_group->iref) { | |
10403 | block_group->iref = 0; | |
10404 | block_group->inode = NULL; | |
10405 | spin_unlock(&block_group->lock); | |
10406 | iput(inode); | |
10407 | } else { | |
10408 | spin_unlock(&block_group->lock); | |
10409 | } | |
10410 | /* One for our lookup ref */ | |
455757c3 | 10411 | btrfs_add_delayed_iput(inode); |
0af3d00b JB |
10412 | } |
10413 | ||
10414 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
10415 | key.offset = block_group->key.objectid; | |
10416 | key.type = 0; | |
10417 | ||
10418 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); | |
10419 | if (ret < 0) | |
10420 | goto out; | |
10421 | if (ret > 0) | |
b3b4aa74 | 10422 | btrfs_release_path(path); |
0af3d00b JB |
10423 | if (ret == 0) { |
10424 | ret = btrfs_del_item(trans, tree_root, path); | |
10425 | if (ret) | |
10426 | goto out; | |
b3b4aa74 | 10427 | btrfs_release_path(path); |
0af3d00b JB |
10428 | } |
10429 | ||
0b246afa | 10430 | spin_lock(&fs_info->block_group_cache_lock); |
1a40e23b | 10431 | rb_erase(&block_group->cache_node, |
0b246afa | 10432 | &fs_info->block_group_cache_tree); |
292cbd51 | 10433 | RB_CLEAR_NODE(&block_group->cache_node); |
a1897fdd | 10434 | |
0b246afa JM |
10435 | if (fs_info->first_logical_byte == block_group->key.objectid) |
10436 | fs_info->first_logical_byte = (u64)-1; | |
10437 | spin_unlock(&fs_info->block_group_cache_lock); | |
817d52f8 | 10438 | |
80eb234a | 10439 | down_write(&block_group->space_info->groups_sem); |
44fb5511 CM |
10440 | /* |
10441 | * we must use list_del_init so people can check to see if they | |
10442 | * are still on the list after taking the semaphore | |
10443 | */ | |
10444 | list_del_init(&block_group->list); | |
6ab0a202 | 10445 | if (list_empty(&block_group->space_info->block_groups[index])) { |
c1895442 JM |
10446 | kobj = block_group->space_info->block_group_kobjs[index]; |
10447 | block_group->space_info->block_group_kobjs[index] = NULL; | |
0b246afa | 10448 | clear_avail_alloc_bits(fs_info, block_group->flags); |
6ab0a202 | 10449 | } |
80eb234a | 10450 | up_write(&block_group->space_info->groups_sem); |
c1895442 JM |
10451 | if (kobj) { |
10452 | kobject_del(kobj); | |
10453 | kobject_put(kobj); | |
10454 | } | |
1a40e23b | 10455 | |
4f69cb98 FM |
10456 | if (block_group->has_caching_ctl) |
10457 | caching_ctl = get_caching_control(block_group); | |
817d52f8 | 10458 | if (block_group->cached == BTRFS_CACHE_STARTED) |
11833d66 | 10459 | wait_block_group_cache_done(block_group); |
4f69cb98 | 10460 | if (block_group->has_caching_ctl) { |
0b246afa | 10461 | down_write(&fs_info->commit_root_sem); |
4f69cb98 FM |
10462 | if (!caching_ctl) { |
10463 | struct btrfs_caching_control *ctl; | |
10464 | ||
10465 | list_for_each_entry(ctl, | |
0b246afa | 10466 | &fs_info->caching_block_groups, list) |
4f69cb98 FM |
10467 | if (ctl->block_group == block_group) { |
10468 | caching_ctl = ctl; | |
10469 | atomic_inc(&caching_ctl->count); | |
10470 | break; | |
10471 | } | |
10472 | } | |
10473 | if (caching_ctl) | |
10474 | list_del_init(&caching_ctl->list); | |
0b246afa | 10475 | up_write(&fs_info->commit_root_sem); |
4f69cb98 FM |
10476 | if (caching_ctl) { |
10477 | /* Once for the caching bgs list and once for us. */ | |
10478 | put_caching_control(caching_ctl); | |
10479 | put_caching_control(caching_ctl); | |
10480 | } | |
10481 | } | |
817d52f8 | 10482 | |
ce93ec54 JB |
10483 | spin_lock(&trans->transaction->dirty_bgs_lock); |
10484 | if (!list_empty(&block_group->dirty_list)) { | |
1bbc621e CM |
10485 | WARN_ON(1); |
10486 | } | |
10487 | if (!list_empty(&block_group->io_list)) { | |
10488 | WARN_ON(1); | |
ce93ec54 JB |
10489 | } |
10490 | spin_unlock(&trans->transaction->dirty_bgs_lock); | |
817d52f8 JB |
10491 | btrfs_remove_free_space_cache(block_group); |
10492 | ||
c146afad | 10493 | spin_lock(&block_group->space_info->lock); |
75c68e9f | 10494 | list_del_init(&block_group->ro_list); |
18d018ad | 10495 | |
0b246afa | 10496 | if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) { |
18d018ad ZL |
10497 | WARN_ON(block_group->space_info->total_bytes |
10498 | < block_group->key.offset); | |
10499 | WARN_ON(block_group->space_info->bytes_readonly | |
10500 | < block_group->key.offset); | |
10501 | WARN_ON(block_group->space_info->disk_total | |
10502 | < block_group->key.offset * factor); | |
10503 | } | |
c146afad YZ |
10504 | block_group->space_info->total_bytes -= block_group->key.offset; |
10505 | block_group->space_info->bytes_readonly -= block_group->key.offset; | |
89a55897 | 10506 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
18d018ad | 10507 | |
c146afad | 10508 | spin_unlock(&block_group->space_info->lock); |
283bb197 | 10509 | |
0af3d00b JB |
10510 | memcpy(&key, &block_group->key, sizeof(key)); |
10511 | ||
0b246afa | 10512 | lock_chunks(fs_info); |
495e64f4 FM |
10513 | if (!list_empty(&em->list)) { |
10514 | /* We're in the transaction->pending_chunks list. */ | |
10515 | free_extent_map(em); | |
10516 | } | |
04216820 FM |
10517 | spin_lock(&block_group->lock); |
10518 | block_group->removed = 1; | |
10519 | /* | |
10520 | * At this point trimming can't start on this block group, because we | |
10521 | * removed the block group from the tree fs_info->block_group_cache_tree | |
10522 | * so no one can't find it anymore and even if someone already got this | |
10523 | * block group before we removed it from the rbtree, they have already | |
10524 | * incremented block_group->trimming - if they didn't, they won't find | |
10525 | * any free space entries because we already removed them all when we | |
10526 | * called btrfs_remove_free_space_cache(). | |
10527 | * | |
10528 | * And we must not remove the extent map from the fs_info->mapping_tree | |
10529 | * to prevent the same logical address range and physical device space | |
10530 | * ranges from being reused for a new block group. This is because our | |
10531 | * fs trim operation (btrfs_trim_fs() / btrfs_ioctl_fitrim()) is | |
10532 | * completely transactionless, so while it is trimming a range the | |
10533 | * currently running transaction might finish and a new one start, | |
10534 | * allowing for new block groups to be created that can reuse the same | |
10535 | * physical device locations unless we take this special care. | |
e33e17ee JM |
10536 | * |
10537 | * There may also be an implicit trim operation if the file system | |
10538 | * is mounted with -odiscard. The same protections must remain | |
10539 | * in place until the extents have been discarded completely when | |
10540 | * the transaction commit has completed. | |
04216820 FM |
10541 | */ |
10542 | remove_em = (atomic_read(&block_group->trimming) == 0); | |
10543 | /* | |
10544 | * Make sure a trimmer task always sees the em in the pinned_chunks list | |
10545 | * if it sees block_group->removed == 1 (needs to lock block_group->lock | |
10546 | * before checking block_group->removed). | |
10547 | */ | |
10548 | if (!remove_em) { | |
10549 | /* | |
10550 | * Our em might be in trans->transaction->pending_chunks which | |
10551 | * is protected by fs_info->chunk_mutex ([lock|unlock]_chunks), | |
10552 | * and so is the fs_info->pinned_chunks list. | |
10553 | * | |
10554 | * So at this point we must be holding the chunk_mutex to avoid | |
10555 | * any races with chunk allocation (more specifically at | |
10556 | * volumes.c:contains_pending_extent()), to ensure it always | |
10557 | * sees the em, either in the pending_chunks list or in the | |
10558 | * pinned_chunks list. | |
10559 | */ | |
0b246afa | 10560 | list_move_tail(&em->list, &fs_info->pinned_chunks); |
04216820 FM |
10561 | } |
10562 | spin_unlock(&block_group->lock); | |
04216820 FM |
10563 | |
10564 | if (remove_em) { | |
10565 | struct extent_map_tree *em_tree; | |
10566 | ||
0b246afa | 10567 | em_tree = &fs_info->mapping_tree.map_tree; |
04216820 | 10568 | write_lock(&em_tree->lock); |
8dbcd10f FM |
10569 | /* |
10570 | * The em might be in the pending_chunks list, so make sure the | |
10571 | * chunk mutex is locked, since remove_extent_mapping() will | |
10572 | * delete us from that list. | |
10573 | */ | |
04216820 FM |
10574 | remove_extent_mapping(em_tree, em); |
10575 | write_unlock(&em_tree->lock); | |
10576 | /* once for the tree */ | |
10577 | free_extent_map(em); | |
10578 | } | |
10579 | ||
0b246afa | 10580 | unlock_chunks(fs_info); |
8dbcd10f | 10581 | |
0b246afa | 10582 | ret = remove_block_group_free_space(trans, fs_info, block_group); |
1e144fb8 OS |
10583 | if (ret) |
10584 | goto out; | |
10585 | ||
fa9c0d79 CM |
10586 | btrfs_put_block_group(block_group); |
10587 | btrfs_put_block_group(block_group); | |
1a40e23b ZY |
10588 | |
10589 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
10590 | if (ret > 0) | |
10591 | ret = -EIO; | |
10592 | if (ret < 0) | |
10593 | goto out; | |
10594 | ||
10595 | ret = btrfs_del_item(trans, root, path); | |
10596 | out: | |
10597 | btrfs_free_path(path); | |
10598 | return ret; | |
10599 | } | |
acce952b | 10600 | |
8eab77ff | 10601 | struct btrfs_trans_handle * |
7fd01182 FM |
10602 | btrfs_start_trans_remove_block_group(struct btrfs_fs_info *fs_info, |
10603 | const u64 chunk_offset) | |
8eab77ff | 10604 | { |
7fd01182 FM |
10605 | struct extent_map_tree *em_tree = &fs_info->mapping_tree.map_tree; |
10606 | struct extent_map *em; | |
10607 | struct map_lookup *map; | |
10608 | unsigned int num_items; | |
10609 | ||
10610 | read_lock(&em_tree->lock); | |
10611 | em = lookup_extent_mapping(em_tree, chunk_offset, 1); | |
10612 | read_unlock(&em_tree->lock); | |
10613 | ASSERT(em && em->start == chunk_offset); | |
10614 | ||
8eab77ff | 10615 | /* |
7fd01182 FM |
10616 | * We need to reserve 3 + N units from the metadata space info in order |
10617 | * to remove a block group (done at btrfs_remove_chunk() and at | |
10618 | * btrfs_remove_block_group()), which are used for: | |
10619 | * | |
8eab77ff FM |
10620 | * 1 unit for adding the free space inode's orphan (located in the tree |
10621 | * of tree roots). | |
7fd01182 FM |
10622 | * 1 unit for deleting the block group item (located in the extent |
10623 | * tree). | |
10624 | * 1 unit for deleting the free space item (located in tree of tree | |
10625 | * roots). | |
10626 | * N units for deleting N device extent items corresponding to each | |
10627 | * stripe (located in the device tree). | |
10628 | * | |
10629 | * In order to remove a block group we also need to reserve units in the | |
10630 | * system space info in order to update the chunk tree (update one or | |
10631 | * more device items and remove one chunk item), but this is done at | |
10632 | * btrfs_remove_chunk() through a call to check_system_chunk(). | |
8eab77ff | 10633 | */ |
95617d69 | 10634 | map = em->map_lookup; |
7fd01182 FM |
10635 | num_items = 3 + map->num_stripes; |
10636 | free_extent_map(em); | |
10637 | ||
8eab77ff | 10638 | return btrfs_start_transaction_fallback_global_rsv(fs_info->extent_root, |
7fd01182 | 10639 | num_items, 1); |
8eab77ff FM |
10640 | } |
10641 | ||
47ab2a6c JB |
10642 | /* |
10643 | * Process the unused_bgs list and remove any that don't have any allocated | |
10644 | * space inside of them. | |
10645 | */ | |
10646 | void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info) | |
10647 | { | |
10648 | struct btrfs_block_group_cache *block_group; | |
10649 | struct btrfs_space_info *space_info; | |
10650 | struct btrfs_root *root = fs_info->extent_root; | |
10651 | struct btrfs_trans_handle *trans; | |
10652 | int ret = 0; | |
10653 | ||
afcdd129 | 10654 | if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags)) |
47ab2a6c JB |
10655 | return; |
10656 | ||
10657 | spin_lock(&fs_info->unused_bgs_lock); | |
10658 | while (!list_empty(&fs_info->unused_bgs)) { | |
10659 | u64 start, end; | |
e33e17ee | 10660 | int trimming; |
47ab2a6c JB |
10661 | |
10662 | block_group = list_first_entry(&fs_info->unused_bgs, | |
10663 | struct btrfs_block_group_cache, | |
10664 | bg_list); | |
47ab2a6c | 10665 | list_del_init(&block_group->bg_list); |
aefbe9a6 ZL |
10666 | |
10667 | space_info = block_group->space_info; | |
10668 | ||
47ab2a6c JB |
10669 | if (ret || btrfs_mixed_space_info(space_info)) { |
10670 | btrfs_put_block_group(block_group); | |
10671 | continue; | |
10672 | } | |
10673 | spin_unlock(&fs_info->unused_bgs_lock); | |
10674 | ||
d5f2e33b | 10675 | mutex_lock(&fs_info->delete_unused_bgs_mutex); |
67c5e7d4 | 10676 | |
47ab2a6c JB |
10677 | /* Don't want to race with allocators so take the groups_sem */ |
10678 | down_write(&space_info->groups_sem); | |
10679 | spin_lock(&block_group->lock); | |
10680 | if (block_group->reserved || | |
10681 | btrfs_block_group_used(&block_group->item) || | |
19c4d2f9 | 10682 | block_group->ro || |
aefbe9a6 | 10683 | list_is_singular(&block_group->list)) { |
47ab2a6c JB |
10684 | /* |
10685 | * We want to bail if we made new allocations or have | |
10686 | * outstanding allocations in this block group. We do | |
10687 | * the ro check in case balance is currently acting on | |
10688 | * this block group. | |
10689 | */ | |
10690 | spin_unlock(&block_group->lock); | |
10691 | up_write(&space_info->groups_sem); | |
10692 | goto next; | |
10693 | } | |
10694 | spin_unlock(&block_group->lock); | |
10695 | ||
10696 | /* We don't want to force the issue, only flip if it's ok. */ | |
868f401a | 10697 | ret = inc_block_group_ro(block_group, 0); |
47ab2a6c JB |
10698 | up_write(&space_info->groups_sem); |
10699 | if (ret < 0) { | |
10700 | ret = 0; | |
10701 | goto next; | |
10702 | } | |
10703 | ||
10704 | /* | |
10705 | * Want to do this before we do anything else so we can recover | |
10706 | * properly if we fail to join the transaction. | |
10707 | */ | |
7fd01182 FM |
10708 | trans = btrfs_start_trans_remove_block_group(fs_info, |
10709 | block_group->key.objectid); | |
47ab2a6c | 10710 | if (IS_ERR(trans)) { |
868f401a | 10711 | btrfs_dec_block_group_ro(root, block_group); |
47ab2a6c JB |
10712 | ret = PTR_ERR(trans); |
10713 | goto next; | |
10714 | } | |
10715 | ||
10716 | /* | |
10717 | * We could have pending pinned extents for this block group, | |
10718 | * just delete them, we don't care about them anymore. | |
10719 | */ | |
10720 | start = block_group->key.objectid; | |
10721 | end = start + block_group->key.offset - 1; | |
d4b450cd FM |
10722 | /* |
10723 | * Hold the unused_bg_unpin_mutex lock to avoid racing with | |
10724 | * btrfs_finish_extent_commit(). If we are at transaction N, | |
10725 | * another task might be running finish_extent_commit() for the | |
10726 | * previous transaction N - 1, and have seen a range belonging | |
10727 | * to the block group in freed_extents[] before we were able to | |
10728 | * clear the whole block group range from freed_extents[]. This | |
10729 | * means that task can lookup for the block group after we | |
10730 | * unpinned it from freed_extents[] and removed it, leading to | |
10731 | * a BUG_ON() at btrfs_unpin_extent_range(). | |
10732 | */ | |
10733 | mutex_lock(&fs_info->unused_bg_unpin_mutex); | |
758eb51e | 10734 | ret = clear_extent_bits(&fs_info->freed_extents[0], start, end, |
91166212 | 10735 | EXTENT_DIRTY); |
758eb51e | 10736 | if (ret) { |
d4b450cd | 10737 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
868f401a | 10738 | btrfs_dec_block_group_ro(root, block_group); |
758eb51e FM |
10739 | goto end_trans; |
10740 | } | |
10741 | ret = clear_extent_bits(&fs_info->freed_extents[1], start, end, | |
91166212 | 10742 | EXTENT_DIRTY); |
758eb51e | 10743 | if (ret) { |
d4b450cd | 10744 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
868f401a | 10745 | btrfs_dec_block_group_ro(root, block_group); |
758eb51e FM |
10746 | goto end_trans; |
10747 | } | |
d4b450cd | 10748 | mutex_unlock(&fs_info->unused_bg_unpin_mutex); |
47ab2a6c JB |
10749 | |
10750 | /* Reset pinned so btrfs_put_block_group doesn't complain */ | |
c30666d4 ZL |
10751 | spin_lock(&space_info->lock); |
10752 | spin_lock(&block_group->lock); | |
10753 | ||
10754 | space_info->bytes_pinned -= block_group->pinned; | |
10755 | space_info->bytes_readonly += block_group->pinned; | |
10756 | percpu_counter_add(&space_info->total_bytes_pinned, | |
10757 | -block_group->pinned); | |
47ab2a6c JB |
10758 | block_group->pinned = 0; |
10759 | ||
c30666d4 ZL |
10760 | spin_unlock(&block_group->lock); |
10761 | spin_unlock(&space_info->lock); | |
10762 | ||
e33e17ee | 10763 | /* DISCARD can flip during remount */ |
0b246afa | 10764 | trimming = btrfs_test_opt(fs_info, DISCARD); |
e33e17ee JM |
10765 | |
10766 | /* Implicit trim during transaction commit. */ | |
10767 | if (trimming) | |
10768 | btrfs_get_block_group_trimming(block_group); | |
10769 | ||
47ab2a6c JB |
10770 | /* |
10771 | * Btrfs_remove_chunk will abort the transaction if things go | |
10772 | * horribly wrong. | |
10773 | */ | |
5b4aacef | 10774 | ret = btrfs_remove_chunk(trans, fs_info, |
47ab2a6c | 10775 | block_group->key.objectid); |
e33e17ee JM |
10776 | |
10777 | if (ret) { | |
10778 | if (trimming) | |
10779 | btrfs_put_block_group_trimming(block_group); | |
10780 | goto end_trans; | |
10781 | } | |
10782 | ||
10783 | /* | |
10784 | * If we're not mounted with -odiscard, we can just forget | |
10785 | * about this block group. Otherwise we'll need to wait | |
10786 | * until transaction commit to do the actual discard. | |
10787 | */ | |
10788 | if (trimming) { | |
348a0013 FM |
10789 | spin_lock(&fs_info->unused_bgs_lock); |
10790 | /* | |
10791 | * A concurrent scrub might have added us to the list | |
10792 | * fs_info->unused_bgs, so use a list_move operation | |
10793 | * to add the block group to the deleted_bgs list. | |
10794 | */ | |
e33e17ee JM |
10795 | list_move(&block_group->bg_list, |
10796 | &trans->transaction->deleted_bgs); | |
348a0013 | 10797 | spin_unlock(&fs_info->unused_bgs_lock); |
e33e17ee JM |
10798 | btrfs_get_block_group(block_group); |
10799 | } | |
758eb51e | 10800 | end_trans: |
47ab2a6c JB |
10801 | btrfs_end_transaction(trans, root); |
10802 | next: | |
d5f2e33b | 10803 | mutex_unlock(&fs_info->delete_unused_bgs_mutex); |
47ab2a6c JB |
10804 | btrfs_put_block_group(block_group); |
10805 | spin_lock(&fs_info->unused_bgs_lock); | |
10806 | } | |
10807 | spin_unlock(&fs_info->unused_bgs_lock); | |
10808 | } | |
10809 | ||
c59021f8 | 10810 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
10811 | { | |
10812 | struct btrfs_space_info *space_info; | |
1aba86d6 | 10813 | struct btrfs_super_block *disk_super; |
10814 | u64 features; | |
10815 | u64 flags; | |
10816 | int mixed = 0; | |
c59021f8 | 10817 | int ret; |
10818 | ||
6c41761f | 10819 | disk_super = fs_info->super_copy; |
1aba86d6 | 10820 | if (!btrfs_super_root(disk_super)) |
0dc924c5 | 10821 | return -EINVAL; |
c59021f8 | 10822 | |
1aba86d6 | 10823 | features = btrfs_super_incompat_flags(disk_super); |
10824 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
10825 | mixed = 1; | |
c59021f8 | 10826 | |
1aba86d6 | 10827 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
e40edf2d | 10828 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
c59021f8 | 10829 | if (ret) |
1aba86d6 | 10830 | goto out; |
c59021f8 | 10831 | |
1aba86d6 | 10832 | if (mixed) { |
10833 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; | |
e40edf2d | 10834 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
1aba86d6 | 10835 | } else { |
10836 | flags = BTRFS_BLOCK_GROUP_METADATA; | |
e40edf2d | 10837 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
1aba86d6 | 10838 | if (ret) |
10839 | goto out; | |
10840 | ||
10841 | flags = BTRFS_BLOCK_GROUP_DATA; | |
e40edf2d | 10842 | ret = update_space_info(fs_info, flags, 0, 0, 0, &space_info); |
1aba86d6 | 10843 | } |
10844 | out: | |
c59021f8 | 10845 | return ret; |
10846 | } | |
10847 | ||
acce952b | 10848 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
10849 | { | |
678886bd | 10850 | return unpin_extent_range(root, start, end, false); |
acce952b | 10851 | } |
10852 | ||
499f377f JM |
10853 | /* |
10854 | * It used to be that old block groups would be left around forever. | |
10855 | * Iterating over them would be enough to trim unused space. Since we | |
10856 | * now automatically remove them, we also need to iterate over unallocated | |
10857 | * space. | |
10858 | * | |
10859 | * We don't want a transaction for this since the discard may take a | |
10860 | * substantial amount of time. We don't require that a transaction be | |
10861 | * running, but we do need to take a running transaction into account | |
10862 | * to ensure that we're not discarding chunks that were released in | |
10863 | * the current transaction. | |
10864 | * | |
10865 | * Holding the chunks lock will prevent other threads from allocating | |
10866 | * or releasing chunks, but it won't prevent a running transaction | |
10867 | * from committing and releasing the memory that the pending chunks | |
10868 | * list head uses. For that, we need to take a reference to the | |
10869 | * transaction. | |
10870 | */ | |
10871 | static int btrfs_trim_free_extents(struct btrfs_device *device, | |
10872 | u64 minlen, u64 *trimmed) | |
10873 | { | |
10874 | u64 start = 0, len = 0; | |
10875 | int ret; | |
10876 | ||
10877 | *trimmed = 0; | |
10878 | ||
10879 | /* Not writeable = nothing to do. */ | |
10880 | if (!device->writeable) | |
10881 | return 0; | |
10882 | ||
10883 | /* No free space = nothing to do. */ | |
10884 | if (device->total_bytes <= device->bytes_used) | |
10885 | return 0; | |
10886 | ||
10887 | ret = 0; | |
10888 | ||
10889 | while (1) { | |
fb456252 | 10890 | struct btrfs_fs_info *fs_info = device->fs_info; |
499f377f JM |
10891 | struct btrfs_transaction *trans; |
10892 | u64 bytes; | |
10893 | ||
10894 | ret = mutex_lock_interruptible(&fs_info->chunk_mutex); | |
10895 | if (ret) | |
10896 | return ret; | |
10897 | ||
10898 | down_read(&fs_info->commit_root_sem); | |
10899 | ||
10900 | spin_lock(&fs_info->trans_lock); | |
10901 | trans = fs_info->running_transaction; | |
10902 | if (trans) | |
10903 | atomic_inc(&trans->use_count); | |
10904 | spin_unlock(&fs_info->trans_lock); | |
10905 | ||
10906 | ret = find_free_dev_extent_start(trans, device, minlen, start, | |
10907 | &start, &len); | |
10908 | if (trans) | |
10909 | btrfs_put_transaction(trans); | |
10910 | ||
10911 | if (ret) { | |
10912 | up_read(&fs_info->commit_root_sem); | |
10913 | mutex_unlock(&fs_info->chunk_mutex); | |
10914 | if (ret == -ENOSPC) | |
10915 | ret = 0; | |
10916 | break; | |
10917 | } | |
10918 | ||
10919 | ret = btrfs_issue_discard(device->bdev, start, len, &bytes); | |
10920 | up_read(&fs_info->commit_root_sem); | |
10921 | mutex_unlock(&fs_info->chunk_mutex); | |
10922 | ||
10923 | if (ret) | |
10924 | break; | |
10925 | ||
10926 | start += len; | |
10927 | *trimmed += bytes; | |
10928 | ||
10929 | if (fatal_signal_pending(current)) { | |
10930 | ret = -ERESTARTSYS; | |
10931 | break; | |
10932 | } | |
10933 | ||
10934 | cond_resched(); | |
10935 | } | |
10936 | ||
10937 | return ret; | |
10938 | } | |
10939 | ||
f7039b1d LD |
10940 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) |
10941 | { | |
10942 | struct btrfs_fs_info *fs_info = root->fs_info; | |
10943 | struct btrfs_block_group_cache *cache = NULL; | |
499f377f JM |
10944 | struct btrfs_device *device; |
10945 | struct list_head *devices; | |
f7039b1d LD |
10946 | u64 group_trimmed; |
10947 | u64 start; | |
10948 | u64 end; | |
10949 | u64 trimmed = 0; | |
2cac13e4 | 10950 | u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy); |
f7039b1d LD |
10951 | int ret = 0; |
10952 | ||
2cac13e4 LB |
10953 | /* |
10954 | * try to trim all FS space, our block group may start from non-zero. | |
10955 | */ | |
10956 | if (range->len == total_bytes) | |
10957 | cache = btrfs_lookup_first_block_group(fs_info, range->start); | |
10958 | else | |
10959 | cache = btrfs_lookup_block_group(fs_info, range->start); | |
f7039b1d LD |
10960 | |
10961 | while (cache) { | |
10962 | if (cache->key.objectid >= (range->start + range->len)) { | |
10963 | btrfs_put_block_group(cache); | |
10964 | break; | |
10965 | } | |
10966 | ||
10967 | start = max(range->start, cache->key.objectid); | |
10968 | end = min(range->start + range->len, | |
10969 | cache->key.objectid + cache->key.offset); | |
10970 | ||
10971 | if (end - start >= range->minlen) { | |
10972 | if (!block_group_cache_done(cache)) { | |
f6373bf3 | 10973 | ret = cache_block_group(cache, 0); |
1be41b78 JB |
10974 | if (ret) { |
10975 | btrfs_put_block_group(cache); | |
10976 | break; | |
10977 | } | |
10978 | ret = wait_block_group_cache_done(cache); | |
10979 | if (ret) { | |
10980 | btrfs_put_block_group(cache); | |
10981 | break; | |
10982 | } | |
f7039b1d LD |
10983 | } |
10984 | ret = btrfs_trim_block_group(cache, | |
10985 | &group_trimmed, | |
10986 | start, | |
10987 | end, | |
10988 | range->minlen); | |
10989 | ||
10990 | trimmed += group_trimmed; | |
10991 | if (ret) { | |
10992 | btrfs_put_block_group(cache); | |
10993 | break; | |
10994 | } | |
10995 | } | |
10996 | ||
10997 | cache = next_block_group(fs_info->tree_root, cache); | |
10998 | } | |
10999 | ||
0b246afa JM |
11000 | mutex_lock(&fs_info->fs_devices->device_list_mutex); |
11001 | devices = &fs_info->fs_devices->alloc_list; | |
499f377f JM |
11002 | list_for_each_entry(device, devices, dev_alloc_list) { |
11003 | ret = btrfs_trim_free_extents(device, range->minlen, | |
11004 | &group_trimmed); | |
11005 | if (ret) | |
11006 | break; | |
11007 | ||
11008 | trimmed += group_trimmed; | |
11009 | } | |
0b246afa | 11010 | mutex_unlock(&fs_info->fs_devices->device_list_mutex); |
499f377f | 11011 | |
f7039b1d LD |
11012 | range->len = trimmed; |
11013 | return ret; | |
11014 | } | |
8257b2dc MX |
11015 | |
11016 | /* | |
9ea24bbe FM |
11017 | * btrfs_{start,end}_write_no_snapshoting() are similar to |
11018 | * mnt_{want,drop}_write(), they are used to prevent some tasks from writing | |
11019 | * data into the page cache through nocow before the subvolume is snapshoted, | |
11020 | * but flush the data into disk after the snapshot creation, or to prevent | |
11021 | * operations while snapshoting is ongoing and that cause the snapshot to be | |
11022 | * inconsistent (writes followed by expanding truncates for example). | |
8257b2dc | 11023 | */ |
9ea24bbe | 11024 | void btrfs_end_write_no_snapshoting(struct btrfs_root *root) |
8257b2dc MX |
11025 | { |
11026 | percpu_counter_dec(&root->subv_writers->counter); | |
11027 | /* | |
a83342aa | 11028 | * Make sure counter is updated before we wake up waiters. |
8257b2dc MX |
11029 | */ |
11030 | smp_mb(); | |
11031 | if (waitqueue_active(&root->subv_writers->wait)) | |
11032 | wake_up(&root->subv_writers->wait); | |
11033 | } | |
11034 | ||
9ea24bbe | 11035 | int btrfs_start_write_no_snapshoting(struct btrfs_root *root) |
8257b2dc | 11036 | { |
ee39b432 | 11037 | if (atomic_read(&root->will_be_snapshoted)) |
8257b2dc MX |
11038 | return 0; |
11039 | ||
11040 | percpu_counter_inc(&root->subv_writers->counter); | |
11041 | /* | |
11042 | * Make sure counter is updated before we check for snapshot creation. | |
11043 | */ | |
11044 | smp_mb(); | |
ee39b432 | 11045 | if (atomic_read(&root->will_be_snapshoted)) { |
9ea24bbe | 11046 | btrfs_end_write_no_snapshoting(root); |
8257b2dc MX |
11047 | return 0; |
11048 | } | |
11049 | return 1; | |
11050 | } | |
0bc19f90 ZL |
11051 | |
11052 | static int wait_snapshoting_atomic_t(atomic_t *a) | |
11053 | { | |
11054 | schedule(); | |
11055 | return 0; | |
11056 | } | |
11057 | ||
11058 | void btrfs_wait_for_snapshot_creation(struct btrfs_root *root) | |
11059 | { | |
11060 | while (true) { | |
11061 | int ret; | |
11062 | ||
11063 | ret = btrfs_start_write_no_snapshoting(root); | |
11064 | if (ret) | |
11065 | break; | |
11066 | wait_on_atomic_t(&root->will_be_snapshoted, | |
11067 | wait_snapshoting_atomic_t, | |
11068 | TASK_UNINTERRUPTIBLE); | |
11069 | } | |
11070 | } |