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> |
4b4e25f2 | 27 | #include "compat.h" |
74493f7a | 28 | #include "hash.h" |
fec577fb CM |
29 | #include "ctree.h" |
30 | #include "disk-io.h" | |
31 | #include "print-tree.h" | |
e089f05c | 32 | #include "transaction.h" |
0b86a832 | 33 | #include "volumes.h" |
925baedd | 34 | #include "locking.h" |
fa9c0d79 | 35 | #include "free-space-cache.h" |
fec577fb | 36 | |
0e4f8f88 CM |
37 | /* control flags for do_chunk_alloc's force field |
38 | * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk | |
39 | * if we really need one. | |
40 | * | |
41 | * CHUNK_ALLOC_FORCE means it must try to allocate one | |
42 | * | |
43 | * CHUNK_ALLOC_LIMITED means to only try and allocate one | |
44 | * if we have very few chunks already allocated. This is | |
45 | * used as part of the clustering code to help make sure | |
46 | * we have a good pool of storage to cluster in, without | |
47 | * filling the FS with empty chunks | |
48 | * | |
49 | */ | |
50 | enum { | |
51 | CHUNK_ALLOC_NO_FORCE = 0, | |
52 | CHUNK_ALLOC_FORCE = 1, | |
53 | CHUNK_ALLOC_LIMITED = 2, | |
54 | }; | |
55 | ||
fb25e914 JB |
56 | /* |
57 | * Control how reservations are dealt with. | |
58 | * | |
59 | * RESERVE_FREE - freeing a reservation. | |
60 | * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for | |
61 | * ENOSPC accounting | |
62 | * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update | |
63 | * bytes_may_use as the ENOSPC accounting is done elsewhere | |
64 | */ | |
65 | enum { | |
66 | RESERVE_FREE = 0, | |
67 | RESERVE_ALLOC = 1, | |
68 | RESERVE_ALLOC_NO_ACCOUNT = 2, | |
69 | }; | |
70 | ||
f3465ca4 JB |
71 | static int update_block_group(struct btrfs_trans_handle *trans, |
72 | struct btrfs_root *root, | |
f0486c68 | 73 | u64 bytenr, u64 num_bytes, int alloc); |
5d4f98a2 YZ |
74 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
75 | struct btrfs_root *root, | |
76 | u64 bytenr, u64 num_bytes, u64 parent, | |
77 | u64 root_objectid, u64 owner_objectid, | |
78 | u64 owner_offset, int refs_to_drop, | |
79 | struct btrfs_delayed_extent_op *extra_op); | |
80 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, | |
81 | struct extent_buffer *leaf, | |
82 | struct btrfs_extent_item *ei); | |
83 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
84 | struct btrfs_root *root, | |
85 | u64 parent, u64 root_objectid, | |
86 | u64 flags, u64 owner, u64 offset, | |
87 | struct btrfs_key *ins, int ref_mod); | |
88 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, | |
89 | struct btrfs_root *root, | |
90 | u64 parent, u64 root_objectid, | |
91 | u64 flags, struct btrfs_disk_key *key, | |
92 | int level, struct btrfs_key *ins); | |
6a63209f JB |
93 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
94 | struct btrfs_root *extent_root, u64 alloc_bytes, | |
95 | u64 flags, int force); | |
11833d66 YZ |
96 | static int find_next_key(struct btrfs_path *path, int level, |
97 | struct btrfs_key *key); | |
9ed74f2d JB |
98 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
99 | int dump_block_groups); | |
fb25e914 JB |
100 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
101 | u64 num_bytes, int reserve); | |
6a63209f | 102 | |
817d52f8 JB |
103 | static noinline int |
104 | block_group_cache_done(struct btrfs_block_group_cache *cache) | |
105 | { | |
106 | smp_mb(); | |
107 | return cache->cached == BTRFS_CACHE_FINISHED; | |
108 | } | |
109 | ||
0f9dd46c JB |
110 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
111 | { | |
112 | return (cache->flags & bits) == bits; | |
113 | } | |
114 | ||
62a45b60 | 115 | static void btrfs_get_block_group(struct btrfs_block_group_cache *cache) |
11dfe35a JB |
116 | { |
117 | atomic_inc(&cache->count); | |
118 | } | |
119 | ||
120 | void btrfs_put_block_group(struct btrfs_block_group_cache *cache) | |
121 | { | |
f0486c68 YZ |
122 | if (atomic_dec_and_test(&cache->count)) { |
123 | WARN_ON(cache->pinned > 0); | |
124 | WARN_ON(cache->reserved > 0); | |
34d52cb6 | 125 | kfree(cache->free_space_ctl); |
11dfe35a | 126 | kfree(cache); |
f0486c68 | 127 | } |
11dfe35a JB |
128 | } |
129 | ||
0f9dd46c JB |
130 | /* |
131 | * this adds the block group to the fs_info rb tree for the block group | |
132 | * cache | |
133 | */ | |
b2950863 | 134 | static int btrfs_add_block_group_cache(struct btrfs_fs_info *info, |
0f9dd46c JB |
135 | struct btrfs_block_group_cache *block_group) |
136 | { | |
137 | struct rb_node **p; | |
138 | struct rb_node *parent = NULL; | |
139 | struct btrfs_block_group_cache *cache; | |
140 | ||
141 | spin_lock(&info->block_group_cache_lock); | |
142 | p = &info->block_group_cache_tree.rb_node; | |
143 | ||
144 | while (*p) { | |
145 | parent = *p; | |
146 | cache = rb_entry(parent, struct btrfs_block_group_cache, | |
147 | cache_node); | |
148 | if (block_group->key.objectid < cache->key.objectid) { | |
149 | p = &(*p)->rb_left; | |
150 | } else if (block_group->key.objectid > cache->key.objectid) { | |
151 | p = &(*p)->rb_right; | |
152 | } else { | |
153 | spin_unlock(&info->block_group_cache_lock); | |
154 | return -EEXIST; | |
155 | } | |
156 | } | |
157 | ||
158 | rb_link_node(&block_group->cache_node, parent, p); | |
159 | rb_insert_color(&block_group->cache_node, | |
160 | &info->block_group_cache_tree); | |
161 | spin_unlock(&info->block_group_cache_lock); | |
162 | ||
163 | return 0; | |
164 | } | |
165 | ||
166 | /* | |
167 | * This will return the block group at or after bytenr if contains is 0, else | |
168 | * it will return the block group that contains the bytenr | |
169 | */ | |
170 | static struct btrfs_block_group_cache * | |
171 | block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr, | |
172 | int contains) | |
173 | { | |
174 | struct btrfs_block_group_cache *cache, *ret = NULL; | |
175 | struct rb_node *n; | |
176 | u64 end, start; | |
177 | ||
178 | spin_lock(&info->block_group_cache_lock); | |
179 | n = info->block_group_cache_tree.rb_node; | |
180 | ||
181 | while (n) { | |
182 | cache = rb_entry(n, struct btrfs_block_group_cache, | |
183 | cache_node); | |
184 | end = cache->key.objectid + cache->key.offset - 1; | |
185 | start = cache->key.objectid; | |
186 | ||
187 | if (bytenr < start) { | |
188 | if (!contains && (!ret || start < ret->key.objectid)) | |
189 | ret = cache; | |
190 | n = n->rb_left; | |
191 | } else if (bytenr > start) { | |
192 | if (contains && bytenr <= end) { | |
193 | ret = cache; | |
194 | break; | |
195 | } | |
196 | n = n->rb_right; | |
197 | } else { | |
198 | ret = cache; | |
199 | break; | |
200 | } | |
201 | } | |
d2fb3437 | 202 | if (ret) |
11dfe35a | 203 | btrfs_get_block_group(ret); |
0f9dd46c JB |
204 | spin_unlock(&info->block_group_cache_lock); |
205 | ||
206 | return ret; | |
207 | } | |
208 | ||
11833d66 YZ |
209 | static int add_excluded_extent(struct btrfs_root *root, |
210 | u64 start, u64 num_bytes) | |
817d52f8 | 211 | { |
11833d66 YZ |
212 | u64 end = start + num_bytes - 1; |
213 | set_extent_bits(&root->fs_info->freed_extents[0], | |
214 | start, end, EXTENT_UPTODATE, GFP_NOFS); | |
215 | set_extent_bits(&root->fs_info->freed_extents[1], | |
216 | start, end, EXTENT_UPTODATE, GFP_NOFS); | |
217 | return 0; | |
218 | } | |
817d52f8 | 219 | |
11833d66 YZ |
220 | static void free_excluded_extents(struct btrfs_root *root, |
221 | struct btrfs_block_group_cache *cache) | |
222 | { | |
223 | u64 start, end; | |
817d52f8 | 224 | |
11833d66 YZ |
225 | start = cache->key.objectid; |
226 | end = start + cache->key.offset - 1; | |
227 | ||
228 | clear_extent_bits(&root->fs_info->freed_extents[0], | |
229 | start, end, EXTENT_UPTODATE, GFP_NOFS); | |
230 | clear_extent_bits(&root->fs_info->freed_extents[1], | |
231 | start, end, EXTENT_UPTODATE, GFP_NOFS); | |
817d52f8 JB |
232 | } |
233 | ||
11833d66 YZ |
234 | static int exclude_super_stripes(struct btrfs_root *root, |
235 | struct btrfs_block_group_cache *cache) | |
817d52f8 | 236 | { |
817d52f8 JB |
237 | u64 bytenr; |
238 | u64 *logical; | |
239 | int stripe_len; | |
240 | int i, nr, ret; | |
241 | ||
06b2331f YZ |
242 | if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) { |
243 | stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid; | |
244 | cache->bytes_super += stripe_len; | |
245 | ret = add_excluded_extent(root, cache->key.objectid, | |
246 | stripe_len); | |
247 | BUG_ON(ret); | |
248 | } | |
249 | ||
817d52f8 JB |
250 | for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) { |
251 | bytenr = btrfs_sb_offset(i); | |
252 | ret = btrfs_rmap_block(&root->fs_info->mapping_tree, | |
253 | cache->key.objectid, bytenr, | |
254 | 0, &logical, &nr, &stripe_len); | |
255 | BUG_ON(ret); | |
11833d66 | 256 | |
817d52f8 | 257 | while (nr--) { |
1b2da372 | 258 | cache->bytes_super += stripe_len; |
11833d66 YZ |
259 | ret = add_excluded_extent(root, logical[nr], |
260 | stripe_len); | |
261 | BUG_ON(ret); | |
817d52f8 | 262 | } |
11833d66 | 263 | |
817d52f8 JB |
264 | kfree(logical); |
265 | } | |
817d52f8 JB |
266 | return 0; |
267 | } | |
268 | ||
11833d66 YZ |
269 | static struct btrfs_caching_control * |
270 | get_caching_control(struct btrfs_block_group_cache *cache) | |
271 | { | |
272 | struct btrfs_caching_control *ctl; | |
273 | ||
274 | spin_lock(&cache->lock); | |
275 | if (cache->cached != BTRFS_CACHE_STARTED) { | |
276 | spin_unlock(&cache->lock); | |
277 | return NULL; | |
278 | } | |
279 | ||
dde5abee JB |
280 | /* We're loading it the fast way, so we don't have a caching_ctl. */ |
281 | if (!cache->caching_ctl) { | |
282 | spin_unlock(&cache->lock); | |
11833d66 YZ |
283 | return NULL; |
284 | } | |
285 | ||
286 | ctl = cache->caching_ctl; | |
287 | atomic_inc(&ctl->count); | |
288 | spin_unlock(&cache->lock); | |
289 | return ctl; | |
290 | } | |
291 | ||
292 | static void put_caching_control(struct btrfs_caching_control *ctl) | |
293 | { | |
294 | if (atomic_dec_and_test(&ctl->count)) | |
295 | kfree(ctl); | |
296 | } | |
297 | ||
0f9dd46c JB |
298 | /* |
299 | * this is only called by cache_block_group, since we could have freed extents | |
300 | * we need to check the pinned_extents for any extents that can't be used yet | |
301 | * since their free space will be released as soon as the transaction commits. | |
302 | */ | |
817d52f8 | 303 | static u64 add_new_free_space(struct btrfs_block_group_cache *block_group, |
0f9dd46c JB |
304 | struct btrfs_fs_info *info, u64 start, u64 end) |
305 | { | |
817d52f8 | 306 | u64 extent_start, extent_end, size, total_added = 0; |
0f9dd46c JB |
307 | int ret; |
308 | ||
309 | while (start < end) { | |
11833d66 | 310 | ret = find_first_extent_bit(info->pinned_extents, start, |
0f9dd46c | 311 | &extent_start, &extent_end, |
11833d66 | 312 | EXTENT_DIRTY | EXTENT_UPTODATE); |
0f9dd46c JB |
313 | if (ret) |
314 | break; | |
315 | ||
06b2331f | 316 | if (extent_start <= start) { |
0f9dd46c JB |
317 | start = extent_end + 1; |
318 | } else if (extent_start > start && extent_start < end) { | |
319 | size = extent_start - start; | |
817d52f8 | 320 | total_added += size; |
ea6a478e JB |
321 | ret = btrfs_add_free_space(block_group, start, |
322 | size); | |
0f9dd46c JB |
323 | BUG_ON(ret); |
324 | start = extent_end + 1; | |
325 | } else { | |
326 | break; | |
327 | } | |
328 | } | |
329 | ||
330 | if (start < end) { | |
331 | size = end - start; | |
817d52f8 | 332 | total_added += size; |
ea6a478e | 333 | ret = btrfs_add_free_space(block_group, start, size); |
0f9dd46c JB |
334 | BUG_ON(ret); |
335 | } | |
336 | ||
817d52f8 | 337 | return total_added; |
0f9dd46c JB |
338 | } |
339 | ||
bab39bf9 | 340 | static noinline void caching_thread(struct btrfs_work *work) |
e37c9e69 | 341 | { |
bab39bf9 JB |
342 | struct btrfs_block_group_cache *block_group; |
343 | struct btrfs_fs_info *fs_info; | |
344 | struct btrfs_caching_control *caching_ctl; | |
345 | struct btrfs_root *extent_root; | |
e37c9e69 | 346 | struct btrfs_path *path; |
5f39d397 | 347 | struct extent_buffer *leaf; |
11833d66 | 348 | struct btrfs_key key; |
817d52f8 | 349 | u64 total_found = 0; |
11833d66 YZ |
350 | u64 last = 0; |
351 | u32 nritems; | |
352 | int ret = 0; | |
f510cfec | 353 | |
bab39bf9 JB |
354 | caching_ctl = container_of(work, struct btrfs_caching_control, work); |
355 | block_group = caching_ctl->block_group; | |
356 | fs_info = block_group->fs_info; | |
357 | extent_root = fs_info->extent_root; | |
358 | ||
e37c9e69 CM |
359 | path = btrfs_alloc_path(); |
360 | if (!path) | |
bab39bf9 | 361 | goto out; |
7d7d6068 | 362 | |
817d52f8 | 363 | last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET); |
11833d66 | 364 | |
5cd57b2c | 365 | /* |
817d52f8 JB |
366 | * We don't want to deadlock with somebody trying to allocate a new |
367 | * extent for the extent root while also trying to search the extent | |
368 | * root to add free space. So we skip locking and search the commit | |
369 | * root, since its read-only | |
5cd57b2c CM |
370 | */ |
371 | path->skip_locking = 1; | |
817d52f8 | 372 | path->search_commit_root = 1; |
026fd317 | 373 | path->reada = 1; |
817d52f8 | 374 | |
e4404d6e | 375 | key.objectid = last; |
e37c9e69 | 376 | key.offset = 0; |
11833d66 | 377 | key.type = BTRFS_EXTENT_ITEM_KEY; |
013f1b12 | 378 | again: |
11833d66 | 379 | mutex_lock(&caching_ctl->mutex); |
013f1b12 CM |
380 | /* need to make sure the commit_root doesn't disappear */ |
381 | down_read(&fs_info->extent_commit_sem); | |
382 | ||
11833d66 | 383 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
e37c9e69 | 384 | if (ret < 0) |
ef8bbdfe | 385 | goto err; |
a512bbf8 | 386 | |
11833d66 YZ |
387 | leaf = path->nodes[0]; |
388 | nritems = btrfs_header_nritems(leaf); | |
389 | ||
d397712b | 390 | while (1) { |
7841cb28 | 391 | if (btrfs_fs_closing(fs_info) > 1) { |
f25784b3 | 392 | last = (u64)-1; |
817d52f8 | 393 | break; |
f25784b3 | 394 | } |
817d52f8 | 395 | |
11833d66 YZ |
396 | if (path->slots[0] < nritems) { |
397 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
398 | } else { | |
399 | ret = find_next_key(path, 0, &key); | |
400 | if (ret) | |
e37c9e69 | 401 | break; |
817d52f8 | 402 | |
589d8ade JB |
403 | if (need_resched() || |
404 | btrfs_next_leaf(extent_root, path)) { | |
405 | caching_ctl->progress = last; | |
ff5714cc | 406 | btrfs_release_path(path); |
589d8ade JB |
407 | up_read(&fs_info->extent_commit_sem); |
408 | mutex_unlock(&caching_ctl->mutex); | |
11833d66 | 409 | cond_resched(); |
589d8ade JB |
410 | goto again; |
411 | } | |
412 | leaf = path->nodes[0]; | |
413 | nritems = btrfs_header_nritems(leaf); | |
414 | continue; | |
11833d66 | 415 | } |
817d52f8 | 416 | |
11833d66 YZ |
417 | if (key.objectid < block_group->key.objectid) { |
418 | path->slots[0]++; | |
817d52f8 | 419 | continue; |
e37c9e69 | 420 | } |
0f9dd46c | 421 | |
e37c9e69 | 422 | if (key.objectid >= block_group->key.objectid + |
0f9dd46c | 423 | block_group->key.offset) |
e37c9e69 | 424 | break; |
7d7d6068 | 425 | |
11833d66 | 426 | if (key.type == BTRFS_EXTENT_ITEM_KEY) { |
817d52f8 JB |
427 | total_found += add_new_free_space(block_group, |
428 | fs_info, last, | |
429 | key.objectid); | |
7d7d6068 | 430 | last = key.objectid + key.offset; |
817d52f8 | 431 | |
11833d66 YZ |
432 | if (total_found > (1024 * 1024 * 2)) { |
433 | total_found = 0; | |
434 | wake_up(&caching_ctl->wait); | |
435 | } | |
817d52f8 | 436 | } |
e37c9e69 CM |
437 | path->slots[0]++; |
438 | } | |
817d52f8 | 439 | ret = 0; |
e37c9e69 | 440 | |
817d52f8 JB |
441 | total_found += add_new_free_space(block_group, fs_info, last, |
442 | block_group->key.objectid + | |
443 | block_group->key.offset); | |
11833d66 | 444 | caching_ctl->progress = (u64)-1; |
817d52f8 JB |
445 | |
446 | spin_lock(&block_group->lock); | |
11833d66 | 447 | block_group->caching_ctl = NULL; |
817d52f8 JB |
448 | block_group->cached = BTRFS_CACHE_FINISHED; |
449 | spin_unlock(&block_group->lock); | |
0f9dd46c | 450 | |
54aa1f4d | 451 | err: |
e37c9e69 | 452 | btrfs_free_path(path); |
276e680d | 453 | up_read(&fs_info->extent_commit_sem); |
817d52f8 | 454 | |
11833d66 YZ |
455 | free_excluded_extents(extent_root, block_group); |
456 | ||
457 | mutex_unlock(&caching_ctl->mutex); | |
bab39bf9 | 458 | out: |
11833d66 YZ |
459 | wake_up(&caching_ctl->wait); |
460 | ||
461 | put_caching_control(caching_ctl); | |
11dfe35a | 462 | btrfs_put_block_group(block_group); |
817d52f8 JB |
463 | } |
464 | ||
9d66e233 JB |
465 | static int cache_block_group(struct btrfs_block_group_cache *cache, |
466 | struct btrfs_trans_handle *trans, | |
b8399dee | 467 | struct btrfs_root *root, |
9d66e233 | 468 | int load_cache_only) |
817d52f8 | 469 | { |
11833d66 YZ |
470 | struct btrfs_fs_info *fs_info = cache->fs_info; |
471 | struct btrfs_caching_control *caching_ctl; | |
817d52f8 JB |
472 | int ret = 0; |
473 | ||
11833d66 YZ |
474 | smp_mb(); |
475 | if (cache->cached != BTRFS_CACHE_NO) | |
476 | return 0; | |
477 | ||
9d66e233 JB |
478 | /* |
479 | * We can't do the read from on-disk cache during a commit since we need | |
b8399dee JB |
480 | * to have the normal tree locking. Also if we are currently trying to |
481 | * allocate blocks for the tree root we can't do the fast caching since | |
482 | * we likely hold important locks. | |
9d66e233 | 483 | */ |
f7039b1d | 484 | if (trans && (!trans->transaction->in_commit) && |
73bc1876 JB |
485 | (root && root != root->fs_info->tree_root) && |
486 | btrfs_test_opt(root, SPACE_CACHE)) { | |
9d66e233 JB |
487 | spin_lock(&cache->lock); |
488 | if (cache->cached != BTRFS_CACHE_NO) { | |
489 | spin_unlock(&cache->lock); | |
490 | return 0; | |
491 | } | |
492 | cache->cached = BTRFS_CACHE_STARTED; | |
493 | spin_unlock(&cache->lock); | |
494 | ||
495 | ret = load_free_space_cache(fs_info, cache); | |
496 | ||
497 | spin_lock(&cache->lock); | |
498 | if (ret == 1) { | |
499 | cache->cached = BTRFS_CACHE_FINISHED; | |
500 | cache->last_byte_to_unpin = (u64)-1; | |
501 | } else { | |
502 | cache->cached = BTRFS_CACHE_NO; | |
503 | } | |
504 | spin_unlock(&cache->lock); | |
3c14874a JB |
505 | if (ret == 1) { |
506 | free_excluded_extents(fs_info->extent_root, cache); | |
9d66e233 | 507 | return 0; |
3c14874a | 508 | } |
9d66e233 JB |
509 | } |
510 | ||
511 | if (load_cache_only) | |
512 | return 0; | |
513 | ||
fc0e4a31 | 514 | caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS); |
11833d66 YZ |
515 | BUG_ON(!caching_ctl); |
516 | ||
517 | INIT_LIST_HEAD(&caching_ctl->list); | |
518 | mutex_init(&caching_ctl->mutex); | |
519 | init_waitqueue_head(&caching_ctl->wait); | |
520 | caching_ctl->block_group = cache; | |
521 | caching_ctl->progress = cache->key.objectid; | |
522 | /* one for caching kthread, one for caching block group list */ | |
523 | atomic_set(&caching_ctl->count, 2); | |
bab39bf9 | 524 | caching_ctl->work.func = caching_thread; |
11833d66 | 525 | |
817d52f8 JB |
526 | spin_lock(&cache->lock); |
527 | if (cache->cached != BTRFS_CACHE_NO) { | |
528 | spin_unlock(&cache->lock); | |
11833d66 YZ |
529 | kfree(caching_ctl); |
530 | return 0; | |
817d52f8 | 531 | } |
11833d66 | 532 | cache->caching_ctl = caching_ctl; |
817d52f8 JB |
533 | cache->cached = BTRFS_CACHE_STARTED; |
534 | spin_unlock(&cache->lock); | |
535 | ||
11833d66 YZ |
536 | down_write(&fs_info->extent_commit_sem); |
537 | list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups); | |
538 | up_write(&fs_info->extent_commit_sem); | |
539 | ||
11dfe35a | 540 | btrfs_get_block_group(cache); |
11833d66 | 541 | |
bab39bf9 | 542 | btrfs_queue_worker(&fs_info->caching_workers, &caching_ctl->work); |
817d52f8 | 543 | |
ef8bbdfe | 544 | return ret; |
e37c9e69 CM |
545 | } |
546 | ||
0f9dd46c JB |
547 | /* |
548 | * return the block group that starts at or after bytenr | |
549 | */ | |
d397712b CM |
550 | static struct btrfs_block_group_cache * |
551 | btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr) | |
0ef3e66b | 552 | { |
0f9dd46c | 553 | struct btrfs_block_group_cache *cache; |
0ef3e66b | 554 | |
0f9dd46c | 555 | cache = block_group_cache_tree_search(info, bytenr, 0); |
0ef3e66b | 556 | |
0f9dd46c | 557 | return cache; |
0ef3e66b CM |
558 | } |
559 | ||
0f9dd46c | 560 | /* |
9f55684c | 561 | * return the block group that contains the given bytenr |
0f9dd46c | 562 | */ |
d397712b CM |
563 | struct btrfs_block_group_cache *btrfs_lookup_block_group( |
564 | struct btrfs_fs_info *info, | |
565 | u64 bytenr) | |
be744175 | 566 | { |
0f9dd46c | 567 | struct btrfs_block_group_cache *cache; |
be744175 | 568 | |
0f9dd46c | 569 | cache = block_group_cache_tree_search(info, bytenr, 1); |
96b5179d | 570 | |
0f9dd46c | 571 | return cache; |
be744175 | 572 | } |
0b86a832 | 573 | |
0f9dd46c JB |
574 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
575 | u64 flags) | |
6324fbf3 | 576 | { |
0f9dd46c | 577 | struct list_head *head = &info->space_info; |
0f9dd46c | 578 | struct btrfs_space_info *found; |
4184ea7f | 579 | |
b742bb82 YZ |
580 | flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM | |
581 | BTRFS_BLOCK_GROUP_METADATA; | |
582 | ||
4184ea7f CM |
583 | rcu_read_lock(); |
584 | list_for_each_entry_rcu(found, head, list) { | |
67377734 | 585 | if (found->flags & flags) { |
4184ea7f | 586 | rcu_read_unlock(); |
0f9dd46c | 587 | return found; |
4184ea7f | 588 | } |
0f9dd46c | 589 | } |
4184ea7f | 590 | rcu_read_unlock(); |
0f9dd46c | 591 | return NULL; |
6324fbf3 CM |
592 | } |
593 | ||
4184ea7f CM |
594 | /* |
595 | * after adding space to the filesystem, we need to clear the full flags | |
596 | * on all the space infos. | |
597 | */ | |
598 | void btrfs_clear_space_info_full(struct btrfs_fs_info *info) | |
599 | { | |
600 | struct list_head *head = &info->space_info; | |
601 | struct btrfs_space_info *found; | |
602 | ||
603 | rcu_read_lock(); | |
604 | list_for_each_entry_rcu(found, head, list) | |
605 | found->full = 0; | |
606 | rcu_read_unlock(); | |
607 | } | |
608 | ||
80eb234a JB |
609 | static u64 div_factor(u64 num, int factor) |
610 | { | |
611 | if (factor == 10) | |
612 | return num; | |
613 | num *= factor; | |
614 | do_div(num, 10); | |
615 | return num; | |
616 | } | |
617 | ||
e5bc2458 CM |
618 | static u64 div_factor_fine(u64 num, int factor) |
619 | { | |
620 | if (factor == 100) | |
621 | return num; | |
622 | num *= factor; | |
623 | do_div(num, 100); | |
624 | return num; | |
625 | } | |
626 | ||
d2fb3437 YZ |
627 | u64 btrfs_find_block_group(struct btrfs_root *root, |
628 | u64 search_start, u64 search_hint, int owner) | |
cd1bc465 | 629 | { |
96b5179d | 630 | struct btrfs_block_group_cache *cache; |
cd1bc465 | 631 | u64 used; |
d2fb3437 YZ |
632 | u64 last = max(search_hint, search_start); |
633 | u64 group_start = 0; | |
31f3c99b | 634 | int full_search = 0; |
d2fb3437 | 635 | int factor = 9; |
0ef3e66b | 636 | int wrapped = 0; |
31f3c99b | 637 | again: |
e8569813 ZY |
638 | while (1) { |
639 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | |
0f9dd46c JB |
640 | if (!cache) |
641 | break; | |
96b5179d | 642 | |
c286ac48 | 643 | spin_lock(&cache->lock); |
96b5179d CM |
644 | last = cache->key.objectid + cache->key.offset; |
645 | used = btrfs_block_group_used(&cache->item); | |
646 | ||
d2fb3437 YZ |
647 | if ((full_search || !cache->ro) && |
648 | block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) { | |
e8569813 | 649 | if (used + cache->pinned + cache->reserved < |
d2fb3437 YZ |
650 | div_factor(cache->key.offset, factor)) { |
651 | group_start = cache->key.objectid; | |
c286ac48 | 652 | spin_unlock(&cache->lock); |
fa9c0d79 | 653 | btrfs_put_block_group(cache); |
8790d502 CM |
654 | goto found; |
655 | } | |
6324fbf3 | 656 | } |
c286ac48 | 657 | spin_unlock(&cache->lock); |
fa9c0d79 | 658 | btrfs_put_block_group(cache); |
de428b63 | 659 | cond_resched(); |
cd1bc465 | 660 | } |
0ef3e66b CM |
661 | if (!wrapped) { |
662 | last = search_start; | |
663 | wrapped = 1; | |
664 | goto again; | |
665 | } | |
666 | if (!full_search && factor < 10) { | |
be744175 | 667 | last = search_start; |
31f3c99b | 668 | full_search = 1; |
0ef3e66b | 669 | factor = 10; |
31f3c99b CM |
670 | goto again; |
671 | } | |
be744175 | 672 | found: |
d2fb3437 | 673 | return group_start; |
925baedd | 674 | } |
0f9dd46c | 675 | |
e02119d5 | 676 | /* simple helper to search for an existing extent at a given offset */ |
31840ae1 | 677 | int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len) |
e02119d5 CM |
678 | { |
679 | int ret; | |
680 | struct btrfs_key key; | |
31840ae1 | 681 | struct btrfs_path *path; |
e02119d5 | 682 | |
31840ae1 | 683 | path = btrfs_alloc_path(); |
d8926bb3 MF |
684 | if (!path) |
685 | return -ENOMEM; | |
686 | ||
e02119d5 CM |
687 | key.objectid = start; |
688 | key.offset = len; | |
689 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
690 | ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path, | |
691 | 0, 0); | |
31840ae1 | 692 | btrfs_free_path(path); |
7bb86316 CM |
693 | return ret; |
694 | } | |
695 | ||
a22285a6 YZ |
696 | /* |
697 | * helper function to lookup reference count and flags of extent. | |
698 | * | |
699 | * the head node for delayed ref is used to store the sum of all the | |
700 | * reference count modifications queued up in the rbtree. the head | |
701 | * node may also store the extent flags to set. This way you can check | |
702 | * to see what the reference count and extent flags would be if all of | |
703 | * the delayed refs are not processed. | |
704 | */ | |
705 | int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans, | |
706 | struct btrfs_root *root, u64 bytenr, | |
707 | u64 num_bytes, u64 *refs, u64 *flags) | |
708 | { | |
709 | struct btrfs_delayed_ref_head *head; | |
710 | struct btrfs_delayed_ref_root *delayed_refs; | |
711 | struct btrfs_path *path; | |
712 | struct btrfs_extent_item *ei; | |
713 | struct extent_buffer *leaf; | |
714 | struct btrfs_key key; | |
715 | u32 item_size; | |
716 | u64 num_refs; | |
717 | u64 extent_flags; | |
718 | int ret; | |
719 | ||
720 | path = btrfs_alloc_path(); | |
721 | if (!path) | |
722 | return -ENOMEM; | |
723 | ||
724 | key.objectid = bytenr; | |
725 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
726 | key.offset = num_bytes; | |
727 | if (!trans) { | |
728 | path->skip_locking = 1; | |
729 | path->search_commit_root = 1; | |
730 | } | |
731 | again: | |
732 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, | |
733 | &key, path, 0, 0); | |
734 | if (ret < 0) | |
735 | goto out_free; | |
736 | ||
737 | if (ret == 0) { | |
738 | leaf = path->nodes[0]; | |
739 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
740 | if (item_size >= sizeof(*ei)) { | |
741 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
742 | struct btrfs_extent_item); | |
743 | num_refs = btrfs_extent_refs(leaf, ei); | |
744 | extent_flags = btrfs_extent_flags(leaf, ei); | |
745 | } else { | |
746 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
747 | struct btrfs_extent_item_v0 *ei0; | |
748 | BUG_ON(item_size != sizeof(*ei0)); | |
749 | ei0 = btrfs_item_ptr(leaf, path->slots[0], | |
750 | struct btrfs_extent_item_v0); | |
751 | num_refs = btrfs_extent_refs_v0(leaf, ei0); | |
752 | /* FIXME: this isn't correct for data */ | |
753 | extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
754 | #else | |
755 | BUG(); | |
756 | #endif | |
757 | } | |
758 | BUG_ON(num_refs == 0); | |
759 | } else { | |
760 | num_refs = 0; | |
761 | extent_flags = 0; | |
762 | ret = 0; | |
763 | } | |
764 | ||
765 | if (!trans) | |
766 | goto out; | |
767 | ||
768 | delayed_refs = &trans->transaction->delayed_refs; | |
769 | spin_lock(&delayed_refs->lock); | |
770 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
771 | if (head) { | |
772 | if (!mutex_trylock(&head->mutex)) { | |
773 | atomic_inc(&head->node.refs); | |
774 | spin_unlock(&delayed_refs->lock); | |
775 | ||
b3b4aa74 | 776 | btrfs_release_path(path); |
a22285a6 | 777 | |
8cc33e5c DS |
778 | /* |
779 | * Mutex was contended, block until it's released and try | |
780 | * again | |
781 | */ | |
a22285a6 YZ |
782 | mutex_lock(&head->mutex); |
783 | mutex_unlock(&head->mutex); | |
784 | btrfs_put_delayed_ref(&head->node); | |
785 | goto again; | |
786 | } | |
787 | if (head->extent_op && head->extent_op->update_flags) | |
788 | extent_flags |= head->extent_op->flags_to_set; | |
789 | else | |
790 | BUG_ON(num_refs == 0); | |
791 | ||
792 | num_refs += head->node.ref_mod; | |
793 | mutex_unlock(&head->mutex); | |
794 | } | |
795 | spin_unlock(&delayed_refs->lock); | |
796 | out: | |
797 | WARN_ON(num_refs == 0); | |
798 | if (refs) | |
799 | *refs = num_refs; | |
800 | if (flags) | |
801 | *flags = extent_flags; | |
802 | out_free: | |
803 | btrfs_free_path(path); | |
804 | return ret; | |
805 | } | |
806 | ||
d8d5f3e1 CM |
807 | /* |
808 | * Back reference rules. Back refs have three main goals: | |
809 | * | |
810 | * 1) differentiate between all holders of references to an extent so that | |
811 | * when a reference is dropped we can make sure it was a valid reference | |
812 | * before freeing the extent. | |
813 | * | |
814 | * 2) Provide enough information to quickly find the holders of an extent | |
815 | * if we notice a given block is corrupted or bad. | |
816 | * | |
817 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
818 | * maintenance. This is actually the same as #2, but with a slightly | |
819 | * different use case. | |
820 | * | |
5d4f98a2 YZ |
821 | * There are two kinds of back refs. The implicit back refs is optimized |
822 | * for pointers in non-shared tree blocks. For a given pointer in a block, | |
823 | * back refs of this kind provide information about the block's owner tree | |
824 | * and the pointer's key. These information allow us to find the block by | |
825 | * b-tree searching. The full back refs is for pointers in tree blocks not | |
826 | * referenced by their owner trees. The location of tree block is recorded | |
827 | * in the back refs. Actually the full back refs is generic, and can be | |
828 | * used in all cases the implicit back refs is used. The major shortcoming | |
829 | * of the full back refs is its overhead. Every time a tree block gets | |
830 | * COWed, we have to update back refs entry for all pointers in it. | |
831 | * | |
832 | * For a newly allocated tree block, we use implicit back refs for | |
833 | * pointers in it. This means most tree related operations only involve | |
834 | * implicit back refs. For a tree block created in old transaction, the | |
835 | * only way to drop a reference to it is COW it. So we can detect the | |
836 | * event that tree block loses its owner tree's reference and do the | |
837 | * back refs conversion. | |
838 | * | |
839 | * When a tree block is COW'd through a tree, there are four cases: | |
840 | * | |
841 | * The reference count of the block is one and the tree is the block's | |
842 | * owner tree. Nothing to do in this case. | |
843 | * | |
844 | * The reference count of the block is one and the tree is not the | |
845 | * block's owner tree. In this case, full back refs is used for pointers | |
846 | * in the block. Remove these full back refs, add implicit back refs for | |
847 | * every pointers in the new block. | |
848 | * | |
849 | * The reference count of the block is greater than one and the tree is | |
850 | * the block's owner tree. In this case, implicit back refs is used for | |
851 | * pointers in the block. Add full back refs for every pointers in the | |
852 | * block, increase lower level extents' reference counts. The original | |
853 | * implicit back refs are entailed to the new block. | |
854 | * | |
855 | * The reference count of the block is greater than one and the tree is | |
856 | * not the block's owner tree. Add implicit back refs for every pointer in | |
857 | * the new block, increase lower level extents' reference count. | |
858 | * | |
859 | * Back Reference Key composing: | |
860 | * | |
861 | * The key objectid corresponds to the first byte in the extent, | |
862 | * The key type is used to differentiate between types of back refs. | |
863 | * There are different meanings of the key offset for different types | |
864 | * of back refs. | |
865 | * | |
d8d5f3e1 CM |
866 | * File extents can be referenced by: |
867 | * | |
868 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
31840ae1 | 869 | * - different files inside a single subvolume |
d8d5f3e1 CM |
870 | * - different offsets inside a file (bookend extents in file.c) |
871 | * | |
5d4f98a2 | 872 | * The extent ref structure for the implicit back refs has fields for: |
d8d5f3e1 CM |
873 | * |
874 | * - Objectid of the subvolume root | |
d8d5f3e1 | 875 | * - objectid of the file holding the reference |
5d4f98a2 YZ |
876 | * - original offset in the file |
877 | * - how many bookend extents | |
d8d5f3e1 | 878 | * |
5d4f98a2 YZ |
879 | * The key offset for the implicit back refs is hash of the first |
880 | * three fields. | |
d8d5f3e1 | 881 | * |
5d4f98a2 | 882 | * The extent ref structure for the full back refs has field for: |
d8d5f3e1 | 883 | * |
5d4f98a2 | 884 | * - number of pointers in the tree leaf |
d8d5f3e1 | 885 | * |
5d4f98a2 YZ |
886 | * The key offset for the implicit back refs is the first byte of |
887 | * the tree leaf | |
d8d5f3e1 | 888 | * |
5d4f98a2 YZ |
889 | * When a file extent is allocated, The implicit back refs is used. |
890 | * the fields are filled in: | |
d8d5f3e1 | 891 | * |
5d4f98a2 | 892 | * (root_key.objectid, inode objectid, offset in file, 1) |
d8d5f3e1 | 893 | * |
5d4f98a2 YZ |
894 | * When a file extent is removed file truncation, we find the |
895 | * corresponding implicit back refs and check the following fields: | |
d8d5f3e1 | 896 | * |
5d4f98a2 | 897 | * (btrfs_header_owner(leaf), inode objectid, offset in file) |
d8d5f3e1 | 898 | * |
5d4f98a2 | 899 | * Btree extents can be referenced by: |
d8d5f3e1 | 900 | * |
5d4f98a2 | 901 | * - Different subvolumes |
d8d5f3e1 | 902 | * |
5d4f98a2 YZ |
903 | * Both the implicit back refs and the full back refs for tree blocks |
904 | * only consist of key. The key offset for the implicit back refs is | |
905 | * objectid of block's owner tree. The key offset for the full back refs | |
906 | * is the first byte of parent block. | |
d8d5f3e1 | 907 | * |
5d4f98a2 YZ |
908 | * When implicit back refs is used, information about the lowest key and |
909 | * level of the tree block are required. These information are stored in | |
910 | * tree block info structure. | |
d8d5f3e1 | 911 | */ |
31840ae1 | 912 | |
5d4f98a2 YZ |
913 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
914 | static int convert_extent_item_v0(struct btrfs_trans_handle *trans, | |
915 | struct btrfs_root *root, | |
916 | struct btrfs_path *path, | |
917 | u64 owner, u32 extra_size) | |
7bb86316 | 918 | { |
5d4f98a2 YZ |
919 | struct btrfs_extent_item *item; |
920 | struct btrfs_extent_item_v0 *ei0; | |
921 | struct btrfs_extent_ref_v0 *ref0; | |
922 | struct btrfs_tree_block_info *bi; | |
923 | struct extent_buffer *leaf; | |
7bb86316 | 924 | struct btrfs_key key; |
5d4f98a2 YZ |
925 | struct btrfs_key found_key; |
926 | u32 new_size = sizeof(*item); | |
927 | u64 refs; | |
928 | int ret; | |
929 | ||
930 | leaf = path->nodes[0]; | |
931 | BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0)); | |
932 | ||
933 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
934 | ei0 = btrfs_item_ptr(leaf, path->slots[0], | |
935 | struct btrfs_extent_item_v0); | |
936 | refs = btrfs_extent_refs_v0(leaf, ei0); | |
937 | ||
938 | if (owner == (u64)-1) { | |
939 | while (1) { | |
940 | if (path->slots[0] >= btrfs_header_nritems(leaf)) { | |
941 | ret = btrfs_next_leaf(root, path); | |
942 | if (ret < 0) | |
943 | return ret; | |
944 | BUG_ON(ret > 0); | |
945 | leaf = path->nodes[0]; | |
946 | } | |
947 | btrfs_item_key_to_cpu(leaf, &found_key, | |
948 | path->slots[0]); | |
949 | BUG_ON(key.objectid != found_key.objectid); | |
950 | if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) { | |
951 | path->slots[0]++; | |
952 | continue; | |
953 | } | |
954 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
955 | struct btrfs_extent_ref_v0); | |
956 | owner = btrfs_ref_objectid_v0(leaf, ref0); | |
957 | break; | |
958 | } | |
959 | } | |
b3b4aa74 | 960 | btrfs_release_path(path); |
5d4f98a2 YZ |
961 | |
962 | if (owner < BTRFS_FIRST_FREE_OBJECTID) | |
963 | new_size += sizeof(*bi); | |
964 | ||
965 | new_size -= sizeof(*ei0); | |
966 | ret = btrfs_search_slot(trans, root, &key, path, | |
967 | new_size + extra_size, 1); | |
968 | if (ret < 0) | |
969 | return ret; | |
970 | BUG_ON(ret); | |
971 | ||
972 | ret = btrfs_extend_item(trans, root, path, new_size); | |
5d4f98a2 YZ |
973 | |
974 | leaf = path->nodes[0]; | |
975 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
976 | btrfs_set_extent_refs(leaf, item, refs); | |
977 | /* FIXME: get real generation */ | |
978 | btrfs_set_extent_generation(leaf, item, 0); | |
979 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
980 | btrfs_set_extent_flags(leaf, item, | |
981 | BTRFS_EXTENT_FLAG_TREE_BLOCK | | |
982 | BTRFS_BLOCK_FLAG_FULL_BACKREF); | |
983 | bi = (struct btrfs_tree_block_info *)(item + 1); | |
984 | /* FIXME: get first key of the block */ | |
985 | memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi)); | |
986 | btrfs_set_tree_block_level(leaf, bi, (int)owner); | |
987 | } else { | |
988 | btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA); | |
989 | } | |
990 | btrfs_mark_buffer_dirty(leaf); | |
991 | return 0; | |
992 | } | |
993 | #endif | |
994 | ||
995 | static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset) | |
996 | { | |
997 | u32 high_crc = ~(u32)0; | |
998 | u32 low_crc = ~(u32)0; | |
999 | __le64 lenum; | |
1000 | ||
1001 | lenum = cpu_to_le64(root_objectid); | |
163e783e | 1002 | high_crc = crc32c(high_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 1003 | lenum = cpu_to_le64(owner); |
163e783e | 1004 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 | 1005 | lenum = cpu_to_le64(offset); |
163e783e | 1006 | low_crc = crc32c(low_crc, &lenum, sizeof(lenum)); |
5d4f98a2 YZ |
1007 | |
1008 | return ((u64)high_crc << 31) ^ (u64)low_crc; | |
1009 | } | |
1010 | ||
1011 | static u64 hash_extent_data_ref_item(struct extent_buffer *leaf, | |
1012 | struct btrfs_extent_data_ref *ref) | |
1013 | { | |
1014 | return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref), | |
1015 | btrfs_extent_data_ref_objectid(leaf, ref), | |
1016 | btrfs_extent_data_ref_offset(leaf, ref)); | |
1017 | } | |
1018 | ||
1019 | static int match_extent_data_ref(struct extent_buffer *leaf, | |
1020 | struct btrfs_extent_data_ref *ref, | |
1021 | u64 root_objectid, u64 owner, u64 offset) | |
1022 | { | |
1023 | if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid || | |
1024 | btrfs_extent_data_ref_objectid(leaf, ref) != owner || | |
1025 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
1026 | return 0; | |
1027 | return 1; | |
1028 | } | |
1029 | ||
1030 | static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans, | |
1031 | struct btrfs_root *root, | |
1032 | struct btrfs_path *path, | |
1033 | u64 bytenr, u64 parent, | |
1034 | u64 root_objectid, | |
1035 | u64 owner, u64 offset) | |
1036 | { | |
1037 | struct btrfs_key key; | |
1038 | struct btrfs_extent_data_ref *ref; | |
31840ae1 | 1039 | struct extent_buffer *leaf; |
5d4f98a2 | 1040 | u32 nritems; |
74493f7a | 1041 | int ret; |
5d4f98a2 YZ |
1042 | int recow; |
1043 | int err = -ENOENT; | |
74493f7a | 1044 | |
31840ae1 | 1045 | key.objectid = bytenr; |
5d4f98a2 YZ |
1046 | if (parent) { |
1047 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
1048 | key.offset = parent; | |
1049 | } else { | |
1050 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
1051 | key.offset = hash_extent_data_ref(root_objectid, | |
1052 | owner, offset); | |
1053 | } | |
1054 | again: | |
1055 | recow = 0; | |
1056 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1057 | if (ret < 0) { | |
1058 | err = ret; | |
1059 | goto fail; | |
1060 | } | |
31840ae1 | 1061 | |
5d4f98a2 YZ |
1062 | if (parent) { |
1063 | if (!ret) | |
1064 | return 0; | |
1065 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1066 | key.type = BTRFS_EXTENT_REF_V0_KEY; | |
b3b4aa74 | 1067 | btrfs_release_path(path); |
5d4f98a2 YZ |
1068 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1069 | if (ret < 0) { | |
1070 | err = ret; | |
1071 | goto fail; | |
1072 | } | |
1073 | if (!ret) | |
1074 | return 0; | |
1075 | #endif | |
1076 | goto fail; | |
31840ae1 ZY |
1077 | } |
1078 | ||
1079 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
1080 | nritems = btrfs_header_nritems(leaf); |
1081 | while (1) { | |
1082 | if (path->slots[0] >= nritems) { | |
1083 | ret = btrfs_next_leaf(root, path); | |
1084 | if (ret < 0) | |
1085 | err = ret; | |
1086 | if (ret) | |
1087 | goto fail; | |
1088 | ||
1089 | leaf = path->nodes[0]; | |
1090 | nritems = btrfs_header_nritems(leaf); | |
1091 | recow = 1; | |
1092 | } | |
1093 | ||
1094 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1095 | if (key.objectid != bytenr || | |
1096 | key.type != BTRFS_EXTENT_DATA_REF_KEY) | |
1097 | goto fail; | |
1098 | ||
1099 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1100 | struct btrfs_extent_data_ref); | |
1101 | ||
1102 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
1103 | owner, offset)) { | |
1104 | if (recow) { | |
b3b4aa74 | 1105 | btrfs_release_path(path); |
5d4f98a2 YZ |
1106 | goto again; |
1107 | } | |
1108 | err = 0; | |
1109 | break; | |
1110 | } | |
1111 | path->slots[0]++; | |
31840ae1 | 1112 | } |
5d4f98a2 YZ |
1113 | fail: |
1114 | return err; | |
31840ae1 ZY |
1115 | } |
1116 | ||
5d4f98a2 YZ |
1117 | static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans, |
1118 | struct btrfs_root *root, | |
1119 | struct btrfs_path *path, | |
1120 | u64 bytenr, u64 parent, | |
1121 | u64 root_objectid, u64 owner, | |
1122 | u64 offset, int refs_to_add) | |
31840ae1 ZY |
1123 | { |
1124 | struct btrfs_key key; | |
1125 | struct extent_buffer *leaf; | |
5d4f98a2 | 1126 | u32 size; |
31840ae1 ZY |
1127 | u32 num_refs; |
1128 | int ret; | |
74493f7a | 1129 | |
74493f7a | 1130 | key.objectid = bytenr; |
5d4f98a2 YZ |
1131 | if (parent) { |
1132 | key.type = BTRFS_SHARED_DATA_REF_KEY; | |
1133 | key.offset = parent; | |
1134 | size = sizeof(struct btrfs_shared_data_ref); | |
1135 | } else { | |
1136 | key.type = BTRFS_EXTENT_DATA_REF_KEY; | |
1137 | key.offset = hash_extent_data_ref(root_objectid, | |
1138 | owner, offset); | |
1139 | size = sizeof(struct btrfs_extent_data_ref); | |
1140 | } | |
74493f7a | 1141 | |
5d4f98a2 YZ |
1142 | ret = btrfs_insert_empty_item(trans, root, path, &key, size); |
1143 | if (ret && ret != -EEXIST) | |
1144 | goto fail; | |
1145 | ||
1146 | leaf = path->nodes[0]; | |
1147 | if (parent) { | |
1148 | struct btrfs_shared_data_ref *ref; | |
31840ae1 | 1149 | ref = btrfs_item_ptr(leaf, path->slots[0], |
5d4f98a2 YZ |
1150 | struct btrfs_shared_data_ref); |
1151 | if (ret == 0) { | |
1152 | btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add); | |
1153 | } else { | |
1154 | num_refs = btrfs_shared_data_ref_count(leaf, ref); | |
1155 | num_refs += refs_to_add; | |
1156 | btrfs_set_shared_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 1157 | } |
5d4f98a2 YZ |
1158 | } else { |
1159 | struct btrfs_extent_data_ref *ref; | |
1160 | while (ret == -EEXIST) { | |
1161 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1162 | struct btrfs_extent_data_ref); | |
1163 | if (match_extent_data_ref(leaf, ref, root_objectid, | |
1164 | owner, offset)) | |
1165 | break; | |
b3b4aa74 | 1166 | btrfs_release_path(path); |
5d4f98a2 YZ |
1167 | key.offset++; |
1168 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
1169 | size); | |
1170 | if (ret && ret != -EEXIST) | |
1171 | goto fail; | |
31840ae1 | 1172 | |
5d4f98a2 YZ |
1173 | leaf = path->nodes[0]; |
1174 | } | |
1175 | ref = btrfs_item_ptr(leaf, path->slots[0], | |
1176 | struct btrfs_extent_data_ref); | |
1177 | if (ret == 0) { | |
1178 | btrfs_set_extent_data_ref_root(leaf, ref, | |
1179 | root_objectid); | |
1180 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
1181 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
1182 | btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add); | |
1183 | } else { | |
1184 | num_refs = btrfs_extent_data_ref_count(leaf, ref); | |
1185 | num_refs += refs_to_add; | |
1186 | btrfs_set_extent_data_ref_count(leaf, ref, num_refs); | |
31840ae1 | 1187 | } |
31840ae1 | 1188 | } |
5d4f98a2 YZ |
1189 | btrfs_mark_buffer_dirty(leaf); |
1190 | ret = 0; | |
1191 | fail: | |
b3b4aa74 | 1192 | btrfs_release_path(path); |
7bb86316 | 1193 | return ret; |
74493f7a CM |
1194 | } |
1195 | ||
5d4f98a2 YZ |
1196 | static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans, |
1197 | struct btrfs_root *root, | |
1198 | struct btrfs_path *path, | |
1199 | int refs_to_drop) | |
31840ae1 | 1200 | { |
5d4f98a2 YZ |
1201 | struct btrfs_key key; |
1202 | struct btrfs_extent_data_ref *ref1 = NULL; | |
1203 | struct btrfs_shared_data_ref *ref2 = NULL; | |
31840ae1 | 1204 | struct extent_buffer *leaf; |
5d4f98a2 | 1205 | u32 num_refs = 0; |
31840ae1 ZY |
1206 | int ret = 0; |
1207 | ||
1208 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
1209 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
1210 | ||
1211 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1212 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
1213 | struct btrfs_extent_data_ref); | |
1214 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1215 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
1216 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
1217 | struct btrfs_shared_data_ref); | |
1218 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1219 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1220 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | |
1221 | struct btrfs_extent_ref_v0 *ref0; | |
1222 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1223 | struct btrfs_extent_ref_v0); | |
1224 | num_refs = btrfs_ref_count_v0(leaf, ref0); | |
1225 | #endif | |
1226 | } else { | |
1227 | BUG(); | |
1228 | } | |
1229 | ||
56bec294 CM |
1230 | BUG_ON(num_refs < refs_to_drop); |
1231 | num_refs -= refs_to_drop; | |
5d4f98a2 | 1232 | |
31840ae1 ZY |
1233 | if (num_refs == 0) { |
1234 | ret = btrfs_del_item(trans, root, path); | |
1235 | } else { | |
5d4f98a2 YZ |
1236 | if (key.type == BTRFS_EXTENT_DATA_REF_KEY) |
1237 | btrfs_set_extent_data_ref_count(leaf, ref1, num_refs); | |
1238 | else if (key.type == BTRFS_SHARED_DATA_REF_KEY) | |
1239 | btrfs_set_shared_data_ref_count(leaf, ref2, num_refs); | |
1240 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1241 | else { | |
1242 | struct btrfs_extent_ref_v0 *ref0; | |
1243 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1244 | struct btrfs_extent_ref_v0); | |
1245 | btrfs_set_ref_count_v0(leaf, ref0, num_refs); | |
1246 | } | |
1247 | #endif | |
31840ae1 ZY |
1248 | btrfs_mark_buffer_dirty(leaf); |
1249 | } | |
31840ae1 ZY |
1250 | return ret; |
1251 | } | |
1252 | ||
5d4f98a2 YZ |
1253 | static noinline u32 extent_data_ref_count(struct btrfs_root *root, |
1254 | struct btrfs_path *path, | |
1255 | struct btrfs_extent_inline_ref *iref) | |
15916de8 | 1256 | { |
5d4f98a2 YZ |
1257 | struct btrfs_key key; |
1258 | struct extent_buffer *leaf; | |
1259 | struct btrfs_extent_data_ref *ref1; | |
1260 | struct btrfs_shared_data_ref *ref2; | |
1261 | u32 num_refs = 0; | |
1262 | ||
1263 | leaf = path->nodes[0]; | |
1264 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
1265 | if (iref) { | |
1266 | if (btrfs_extent_inline_ref_type(leaf, iref) == | |
1267 | BTRFS_EXTENT_DATA_REF_KEY) { | |
1268 | ref1 = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1269 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1270 | } else { | |
1271 | ref2 = (struct btrfs_shared_data_ref *)(iref + 1); | |
1272 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1273 | } | |
1274 | } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1275 | ref1 = btrfs_item_ptr(leaf, path->slots[0], | |
1276 | struct btrfs_extent_data_ref); | |
1277 | num_refs = btrfs_extent_data_ref_count(leaf, ref1); | |
1278 | } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) { | |
1279 | ref2 = btrfs_item_ptr(leaf, path->slots[0], | |
1280 | struct btrfs_shared_data_ref); | |
1281 | num_refs = btrfs_shared_data_ref_count(leaf, ref2); | |
1282 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1283 | } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) { | |
1284 | struct btrfs_extent_ref_v0 *ref0; | |
1285 | ref0 = btrfs_item_ptr(leaf, path->slots[0], | |
1286 | struct btrfs_extent_ref_v0); | |
1287 | num_refs = btrfs_ref_count_v0(leaf, ref0); | |
4b4e25f2 | 1288 | #endif |
5d4f98a2 YZ |
1289 | } else { |
1290 | WARN_ON(1); | |
1291 | } | |
1292 | return num_refs; | |
1293 | } | |
15916de8 | 1294 | |
5d4f98a2 YZ |
1295 | static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans, |
1296 | struct btrfs_root *root, | |
1297 | struct btrfs_path *path, | |
1298 | u64 bytenr, u64 parent, | |
1299 | u64 root_objectid) | |
1f3c79a2 | 1300 | { |
5d4f98a2 | 1301 | struct btrfs_key key; |
1f3c79a2 | 1302 | int ret; |
1f3c79a2 | 1303 | |
5d4f98a2 YZ |
1304 | key.objectid = bytenr; |
1305 | if (parent) { | |
1306 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1307 | key.offset = parent; | |
1308 | } else { | |
1309 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
1310 | key.offset = root_objectid; | |
1f3c79a2 LH |
1311 | } |
1312 | ||
5d4f98a2 YZ |
1313 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
1314 | if (ret > 0) | |
1315 | ret = -ENOENT; | |
1316 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1317 | if (ret == -ENOENT && parent) { | |
b3b4aa74 | 1318 | btrfs_release_path(path); |
5d4f98a2 YZ |
1319 | key.type = BTRFS_EXTENT_REF_V0_KEY; |
1320 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1321 | if (ret > 0) | |
1322 | ret = -ENOENT; | |
1323 | } | |
1f3c79a2 | 1324 | #endif |
5d4f98a2 | 1325 | return ret; |
1f3c79a2 LH |
1326 | } |
1327 | ||
5d4f98a2 YZ |
1328 | static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans, |
1329 | struct btrfs_root *root, | |
1330 | struct btrfs_path *path, | |
1331 | u64 bytenr, u64 parent, | |
1332 | u64 root_objectid) | |
31840ae1 | 1333 | { |
5d4f98a2 | 1334 | struct btrfs_key key; |
31840ae1 | 1335 | int ret; |
31840ae1 | 1336 | |
5d4f98a2 YZ |
1337 | key.objectid = bytenr; |
1338 | if (parent) { | |
1339 | key.type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1340 | key.offset = parent; | |
1341 | } else { | |
1342 | key.type = BTRFS_TREE_BLOCK_REF_KEY; | |
1343 | key.offset = root_objectid; | |
1344 | } | |
1345 | ||
1346 | ret = btrfs_insert_empty_item(trans, root, path, &key, 0); | |
b3b4aa74 | 1347 | btrfs_release_path(path); |
31840ae1 ZY |
1348 | return ret; |
1349 | } | |
1350 | ||
5d4f98a2 | 1351 | static inline int extent_ref_type(u64 parent, u64 owner) |
31840ae1 | 1352 | { |
5d4f98a2 YZ |
1353 | int type; |
1354 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1355 | if (parent > 0) | |
1356 | type = BTRFS_SHARED_BLOCK_REF_KEY; | |
1357 | else | |
1358 | type = BTRFS_TREE_BLOCK_REF_KEY; | |
1359 | } else { | |
1360 | if (parent > 0) | |
1361 | type = BTRFS_SHARED_DATA_REF_KEY; | |
1362 | else | |
1363 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
1364 | } | |
1365 | return type; | |
31840ae1 | 1366 | } |
56bec294 | 1367 | |
2c47e605 YZ |
1368 | static int find_next_key(struct btrfs_path *path, int level, |
1369 | struct btrfs_key *key) | |
56bec294 | 1370 | |
02217ed2 | 1371 | { |
2c47e605 | 1372 | for (; level < BTRFS_MAX_LEVEL; level++) { |
5d4f98a2 YZ |
1373 | if (!path->nodes[level]) |
1374 | break; | |
5d4f98a2 YZ |
1375 | if (path->slots[level] + 1 >= |
1376 | btrfs_header_nritems(path->nodes[level])) | |
1377 | continue; | |
1378 | if (level == 0) | |
1379 | btrfs_item_key_to_cpu(path->nodes[level], key, | |
1380 | path->slots[level] + 1); | |
1381 | else | |
1382 | btrfs_node_key_to_cpu(path->nodes[level], key, | |
1383 | path->slots[level] + 1); | |
1384 | return 0; | |
1385 | } | |
1386 | return 1; | |
1387 | } | |
037e6390 | 1388 | |
5d4f98a2 YZ |
1389 | /* |
1390 | * look for inline back ref. if back ref is found, *ref_ret is set | |
1391 | * to the address of inline back ref, and 0 is returned. | |
1392 | * | |
1393 | * if back ref isn't found, *ref_ret is set to the address where it | |
1394 | * should be inserted, and -ENOENT is returned. | |
1395 | * | |
1396 | * if insert is true and there are too many inline back refs, the path | |
1397 | * points to the extent item, and -EAGAIN is returned. | |
1398 | * | |
1399 | * NOTE: inline back refs are ordered in the same way that back ref | |
1400 | * items in the tree are ordered. | |
1401 | */ | |
1402 | static noinline_for_stack | |
1403 | int lookup_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1404 | struct btrfs_root *root, | |
1405 | struct btrfs_path *path, | |
1406 | struct btrfs_extent_inline_ref **ref_ret, | |
1407 | u64 bytenr, u64 num_bytes, | |
1408 | u64 parent, u64 root_objectid, | |
1409 | u64 owner, u64 offset, int insert) | |
1410 | { | |
1411 | struct btrfs_key key; | |
1412 | struct extent_buffer *leaf; | |
1413 | struct btrfs_extent_item *ei; | |
1414 | struct btrfs_extent_inline_ref *iref; | |
1415 | u64 flags; | |
1416 | u64 item_size; | |
1417 | unsigned long ptr; | |
1418 | unsigned long end; | |
1419 | int extra_size; | |
1420 | int type; | |
1421 | int want; | |
1422 | int ret; | |
1423 | int err = 0; | |
26b8003f | 1424 | |
db94535d | 1425 | key.objectid = bytenr; |
31840ae1 | 1426 | key.type = BTRFS_EXTENT_ITEM_KEY; |
56bec294 | 1427 | key.offset = num_bytes; |
31840ae1 | 1428 | |
5d4f98a2 YZ |
1429 | want = extent_ref_type(parent, owner); |
1430 | if (insert) { | |
1431 | extra_size = btrfs_extent_inline_ref_size(want); | |
85d4198e | 1432 | path->keep_locks = 1; |
5d4f98a2 YZ |
1433 | } else |
1434 | extra_size = -1; | |
1435 | ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1); | |
b9473439 | 1436 | if (ret < 0) { |
5d4f98a2 YZ |
1437 | err = ret; |
1438 | goto out; | |
1439 | } | |
1440 | BUG_ON(ret); | |
1441 | ||
1442 | leaf = path->nodes[0]; | |
1443 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1444 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
1445 | if (item_size < sizeof(*ei)) { | |
1446 | if (!insert) { | |
1447 | err = -ENOENT; | |
1448 | goto out; | |
1449 | } | |
1450 | ret = convert_extent_item_v0(trans, root, path, owner, | |
1451 | extra_size); | |
1452 | if (ret < 0) { | |
1453 | err = ret; | |
1454 | goto out; | |
1455 | } | |
1456 | leaf = path->nodes[0]; | |
1457 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1458 | } | |
1459 | #endif | |
1460 | BUG_ON(item_size < sizeof(*ei)); | |
1461 | ||
5d4f98a2 YZ |
1462 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); |
1463 | flags = btrfs_extent_flags(leaf, ei); | |
1464 | ||
1465 | ptr = (unsigned long)(ei + 1); | |
1466 | end = (unsigned long)ei + item_size; | |
1467 | ||
1468 | if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) { | |
1469 | ptr += sizeof(struct btrfs_tree_block_info); | |
1470 | BUG_ON(ptr > end); | |
1471 | } else { | |
1472 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA)); | |
1473 | } | |
1474 | ||
1475 | err = -ENOENT; | |
1476 | while (1) { | |
1477 | if (ptr >= end) { | |
1478 | WARN_ON(ptr > end); | |
1479 | break; | |
1480 | } | |
1481 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
1482 | type = btrfs_extent_inline_ref_type(leaf, iref); | |
1483 | if (want < type) | |
1484 | break; | |
1485 | if (want > type) { | |
1486 | ptr += btrfs_extent_inline_ref_size(type); | |
1487 | continue; | |
1488 | } | |
1489 | ||
1490 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1491 | struct btrfs_extent_data_ref *dref; | |
1492 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1493 | if (match_extent_data_ref(leaf, dref, root_objectid, | |
1494 | owner, offset)) { | |
1495 | err = 0; | |
1496 | break; | |
1497 | } | |
1498 | if (hash_extent_data_ref_item(leaf, dref) < | |
1499 | hash_extent_data_ref(root_objectid, owner, offset)) | |
1500 | break; | |
1501 | } else { | |
1502 | u64 ref_offset; | |
1503 | ref_offset = btrfs_extent_inline_ref_offset(leaf, iref); | |
1504 | if (parent > 0) { | |
1505 | if (parent == ref_offset) { | |
1506 | err = 0; | |
1507 | break; | |
1508 | } | |
1509 | if (ref_offset < parent) | |
1510 | break; | |
1511 | } else { | |
1512 | if (root_objectid == ref_offset) { | |
1513 | err = 0; | |
1514 | break; | |
1515 | } | |
1516 | if (ref_offset < root_objectid) | |
1517 | break; | |
1518 | } | |
1519 | } | |
1520 | ptr += btrfs_extent_inline_ref_size(type); | |
1521 | } | |
1522 | if (err == -ENOENT && insert) { | |
1523 | if (item_size + extra_size >= | |
1524 | BTRFS_MAX_EXTENT_ITEM_SIZE(root)) { | |
1525 | err = -EAGAIN; | |
1526 | goto out; | |
1527 | } | |
1528 | /* | |
1529 | * To add new inline back ref, we have to make sure | |
1530 | * there is no corresponding back ref item. | |
1531 | * For simplicity, we just do not add new inline back | |
1532 | * ref if there is any kind of item for this block | |
1533 | */ | |
2c47e605 YZ |
1534 | if (find_next_key(path, 0, &key) == 0 && |
1535 | key.objectid == bytenr && | |
85d4198e | 1536 | key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) { |
5d4f98a2 YZ |
1537 | err = -EAGAIN; |
1538 | goto out; | |
1539 | } | |
1540 | } | |
1541 | *ref_ret = (struct btrfs_extent_inline_ref *)ptr; | |
1542 | out: | |
85d4198e | 1543 | if (insert) { |
5d4f98a2 YZ |
1544 | path->keep_locks = 0; |
1545 | btrfs_unlock_up_safe(path, 1); | |
1546 | } | |
1547 | return err; | |
1548 | } | |
1549 | ||
1550 | /* | |
1551 | * helper to add new inline back ref | |
1552 | */ | |
1553 | static noinline_for_stack | |
1554 | int setup_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1555 | struct btrfs_root *root, | |
1556 | struct btrfs_path *path, | |
1557 | struct btrfs_extent_inline_ref *iref, | |
1558 | u64 parent, u64 root_objectid, | |
1559 | u64 owner, u64 offset, int refs_to_add, | |
1560 | struct btrfs_delayed_extent_op *extent_op) | |
1561 | { | |
1562 | struct extent_buffer *leaf; | |
1563 | struct btrfs_extent_item *ei; | |
1564 | unsigned long ptr; | |
1565 | unsigned long end; | |
1566 | unsigned long item_offset; | |
1567 | u64 refs; | |
1568 | int size; | |
1569 | int type; | |
1570 | int ret; | |
1571 | ||
1572 | leaf = path->nodes[0]; | |
1573 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1574 | item_offset = (unsigned long)iref - (unsigned long)ei; | |
1575 | ||
1576 | type = extent_ref_type(parent, owner); | |
1577 | size = btrfs_extent_inline_ref_size(type); | |
1578 | ||
1579 | ret = btrfs_extend_item(trans, root, path, size); | |
5d4f98a2 YZ |
1580 | |
1581 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1582 | refs = btrfs_extent_refs(leaf, ei); | |
1583 | refs += refs_to_add; | |
1584 | btrfs_set_extent_refs(leaf, ei, refs); | |
1585 | if (extent_op) | |
1586 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1587 | ||
1588 | ptr = (unsigned long)ei + item_offset; | |
1589 | end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]); | |
1590 | if (ptr < end - size) | |
1591 | memmove_extent_buffer(leaf, ptr + size, ptr, | |
1592 | end - size - ptr); | |
1593 | ||
1594 | iref = (struct btrfs_extent_inline_ref *)ptr; | |
1595 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
1596 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1597 | struct btrfs_extent_data_ref *dref; | |
1598 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1599 | btrfs_set_extent_data_ref_root(leaf, dref, root_objectid); | |
1600 | btrfs_set_extent_data_ref_objectid(leaf, dref, owner); | |
1601 | btrfs_set_extent_data_ref_offset(leaf, dref, offset); | |
1602 | btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add); | |
1603 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1604 | struct btrfs_shared_data_ref *sref; | |
1605 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1606 | btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add); | |
1607 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1608 | } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) { | |
1609 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
1610 | } else { | |
1611 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | |
1612 | } | |
1613 | btrfs_mark_buffer_dirty(leaf); | |
1614 | return 0; | |
1615 | } | |
1616 | ||
1617 | static int lookup_extent_backref(struct btrfs_trans_handle *trans, | |
1618 | struct btrfs_root *root, | |
1619 | struct btrfs_path *path, | |
1620 | struct btrfs_extent_inline_ref **ref_ret, | |
1621 | u64 bytenr, u64 num_bytes, u64 parent, | |
1622 | u64 root_objectid, u64 owner, u64 offset) | |
1623 | { | |
1624 | int ret; | |
1625 | ||
1626 | ret = lookup_inline_extent_backref(trans, root, path, ref_ret, | |
1627 | bytenr, num_bytes, parent, | |
1628 | root_objectid, owner, offset, 0); | |
1629 | if (ret != -ENOENT) | |
54aa1f4d | 1630 | return ret; |
5d4f98a2 | 1631 | |
b3b4aa74 | 1632 | btrfs_release_path(path); |
5d4f98a2 YZ |
1633 | *ref_ret = NULL; |
1634 | ||
1635 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1636 | ret = lookup_tree_block_ref(trans, root, path, bytenr, parent, | |
1637 | root_objectid); | |
1638 | } else { | |
1639 | ret = lookup_extent_data_ref(trans, root, path, bytenr, parent, | |
1640 | root_objectid, owner, offset); | |
b9473439 | 1641 | } |
5d4f98a2 YZ |
1642 | return ret; |
1643 | } | |
31840ae1 | 1644 | |
5d4f98a2 YZ |
1645 | /* |
1646 | * helper to update/remove inline back ref | |
1647 | */ | |
1648 | static noinline_for_stack | |
1649 | int update_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1650 | struct btrfs_root *root, | |
1651 | struct btrfs_path *path, | |
1652 | struct btrfs_extent_inline_ref *iref, | |
1653 | int refs_to_mod, | |
1654 | struct btrfs_delayed_extent_op *extent_op) | |
1655 | { | |
1656 | struct extent_buffer *leaf; | |
1657 | struct btrfs_extent_item *ei; | |
1658 | struct btrfs_extent_data_ref *dref = NULL; | |
1659 | struct btrfs_shared_data_ref *sref = NULL; | |
1660 | unsigned long ptr; | |
1661 | unsigned long end; | |
1662 | u32 item_size; | |
1663 | int size; | |
1664 | int type; | |
1665 | int ret; | |
1666 | u64 refs; | |
1667 | ||
1668 | leaf = path->nodes[0]; | |
1669 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1670 | refs = btrfs_extent_refs(leaf, ei); | |
1671 | WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0); | |
1672 | refs += refs_to_mod; | |
1673 | btrfs_set_extent_refs(leaf, ei, refs); | |
1674 | if (extent_op) | |
1675 | __run_delayed_extent_op(extent_op, leaf, ei); | |
1676 | ||
1677 | type = btrfs_extent_inline_ref_type(leaf, iref); | |
1678 | ||
1679 | if (type == BTRFS_EXTENT_DATA_REF_KEY) { | |
1680 | dref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
1681 | refs = btrfs_extent_data_ref_count(leaf, dref); | |
1682 | } else if (type == BTRFS_SHARED_DATA_REF_KEY) { | |
1683 | sref = (struct btrfs_shared_data_ref *)(iref + 1); | |
1684 | refs = btrfs_shared_data_ref_count(leaf, sref); | |
1685 | } else { | |
1686 | refs = 1; | |
1687 | BUG_ON(refs_to_mod != -1); | |
56bec294 | 1688 | } |
31840ae1 | 1689 | |
5d4f98a2 YZ |
1690 | BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod); |
1691 | refs += refs_to_mod; | |
1692 | ||
1693 | if (refs > 0) { | |
1694 | if (type == BTRFS_EXTENT_DATA_REF_KEY) | |
1695 | btrfs_set_extent_data_ref_count(leaf, dref, refs); | |
1696 | else | |
1697 | btrfs_set_shared_data_ref_count(leaf, sref, refs); | |
1698 | } else { | |
1699 | size = btrfs_extent_inline_ref_size(type); | |
1700 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
1701 | ptr = (unsigned long)iref; | |
1702 | end = (unsigned long)ei + item_size; | |
1703 | if (ptr + size < end) | |
1704 | memmove_extent_buffer(leaf, ptr, ptr + size, | |
1705 | end - ptr - size); | |
1706 | item_size -= size; | |
1707 | ret = btrfs_truncate_item(trans, root, path, item_size, 1); | |
5d4f98a2 YZ |
1708 | } |
1709 | btrfs_mark_buffer_dirty(leaf); | |
1710 | return 0; | |
1711 | } | |
1712 | ||
1713 | static noinline_for_stack | |
1714 | int insert_inline_extent_backref(struct btrfs_trans_handle *trans, | |
1715 | struct btrfs_root *root, | |
1716 | struct btrfs_path *path, | |
1717 | u64 bytenr, u64 num_bytes, u64 parent, | |
1718 | u64 root_objectid, u64 owner, | |
1719 | u64 offset, int refs_to_add, | |
1720 | struct btrfs_delayed_extent_op *extent_op) | |
1721 | { | |
1722 | struct btrfs_extent_inline_ref *iref; | |
1723 | int ret; | |
1724 | ||
1725 | ret = lookup_inline_extent_backref(trans, root, path, &iref, | |
1726 | bytenr, num_bytes, parent, | |
1727 | root_objectid, owner, offset, 1); | |
1728 | if (ret == 0) { | |
1729 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID); | |
1730 | ret = update_inline_extent_backref(trans, root, path, iref, | |
1731 | refs_to_add, extent_op); | |
1732 | } else if (ret == -ENOENT) { | |
1733 | ret = setup_inline_extent_backref(trans, root, path, iref, | |
1734 | parent, root_objectid, | |
1735 | owner, offset, refs_to_add, | |
1736 | extent_op); | |
771ed689 | 1737 | } |
5d4f98a2 YZ |
1738 | return ret; |
1739 | } | |
31840ae1 | 1740 | |
5d4f98a2 YZ |
1741 | static int insert_extent_backref(struct btrfs_trans_handle *trans, |
1742 | struct btrfs_root *root, | |
1743 | struct btrfs_path *path, | |
1744 | u64 bytenr, u64 parent, u64 root_objectid, | |
1745 | u64 owner, u64 offset, int refs_to_add) | |
1746 | { | |
1747 | int ret; | |
1748 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1749 | BUG_ON(refs_to_add != 1); | |
1750 | ret = insert_tree_block_ref(trans, root, path, bytenr, | |
1751 | parent, root_objectid); | |
1752 | } else { | |
1753 | ret = insert_extent_data_ref(trans, root, path, bytenr, | |
1754 | parent, root_objectid, | |
1755 | owner, offset, refs_to_add); | |
1756 | } | |
1757 | return ret; | |
1758 | } | |
56bec294 | 1759 | |
5d4f98a2 YZ |
1760 | static int remove_extent_backref(struct btrfs_trans_handle *trans, |
1761 | struct btrfs_root *root, | |
1762 | struct btrfs_path *path, | |
1763 | struct btrfs_extent_inline_ref *iref, | |
1764 | int refs_to_drop, int is_data) | |
1765 | { | |
1766 | int ret; | |
b9473439 | 1767 | |
5d4f98a2 YZ |
1768 | BUG_ON(!is_data && refs_to_drop != 1); |
1769 | if (iref) { | |
1770 | ret = update_inline_extent_backref(trans, root, path, iref, | |
1771 | -refs_to_drop, NULL); | |
1772 | } else if (is_data) { | |
1773 | ret = remove_extent_data_ref(trans, root, path, refs_to_drop); | |
1774 | } else { | |
1775 | ret = btrfs_del_item(trans, root, path); | |
1776 | } | |
1777 | return ret; | |
1778 | } | |
1779 | ||
5378e607 | 1780 | static int btrfs_issue_discard(struct block_device *bdev, |
5d4f98a2 YZ |
1781 | u64 start, u64 len) |
1782 | { | |
5378e607 | 1783 | return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0); |
5d4f98a2 | 1784 | } |
5d4f98a2 YZ |
1785 | |
1786 | static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr, | |
5378e607 | 1787 | u64 num_bytes, u64 *actual_bytes) |
5d4f98a2 | 1788 | { |
5d4f98a2 | 1789 | int ret; |
5378e607 | 1790 | u64 discarded_bytes = 0; |
a1d3c478 | 1791 | struct btrfs_bio *bbio = NULL; |
5d4f98a2 | 1792 | |
e244a0ae | 1793 | |
5d4f98a2 | 1794 | /* Tell the block device(s) that the sectors can be discarded */ |
5378e607 | 1795 | ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD, |
a1d3c478 | 1796 | bytenr, &num_bytes, &bbio, 0); |
5d4f98a2 | 1797 | if (!ret) { |
a1d3c478 | 1798 | struct btrfs_bio_stripe *stripe = bbio->stripes; |
5d4f98a2 YZ |
1799 | int i; |
1800 | ||
5d4f98a2 | 1801 | |
a1d3c478 | 1802 | for (i = 0; i < bbio->num_stripes; i++, stripe++) { |
d5e2003c JB |
1803 | if (!stripe->dev->can_discard) |
1804 | continue; | |
1805 | ||
5378e607 LD |
1806 | ret = btrfs_issue_discard(stripe->dev->bdev, |
1807 | stripe->physical, | |
1808 | stripe->length); | |
1809 | if (!ret) | |
1810 | discarded_bytes += stripe->length; | |
1811 | else if (ret != -EOPNOTSUPP) | |
1812 | break; | |
d5e2003c JB |
1813 | |
1814 | /* | |
1815 | * Just in case we get back EOPNOTSUPP for some reason, | |
1816 | * just ignore the return value so we don't screw up | |
1817 | * people calling discard_extent. | |
1818 | */ | |
1819 | ret = 0; | |
5d4f98a2 | 1820 | } |
a1d3c478 | 1821 | kfree(bbio); |
5d4f98a2 | 1822 | } |
5378e607 LD |
1823 | |
1824 | if (actual_bytes) | |
1825 | *actual_bytes = discarded_bytes; | |
1826 | ||
5d4f98a2 YZ |
1827 | |
1828 | return ret; | |
5d4f98a2 YZ |
1829 | } |
1830 | ||
1831 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |
1832 | struct btrfs_root *root, | |
1833 | u64 bytenr, u64 num_bytes, u64 parent, | |
1834 | u64 root_objectid, u64 owner, u64 offset) | |
1835 | { | |
1836 | int ret; | |
1837 | BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID && | |
1838 | root_objectid == BTRFS_TREE_LOG_OBJECTID); | |
1839 | ||
1840 | if (owner < BTRFS_FIRST_FREE_OBJECTID) { | |
1841 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, | |
1842 | parent, root_objectid, (int)owner, | |
1843 | BTRFS_ADD_DELAYED_REF, NULL); | |
1844 | } else { | |
1845 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, | |
1846 | parent, root_objectid, owner, offset, | |
1847 | BTRFS_ADD_DELAYED_REF, NULL); | |
1848 | } | |
1849 | return ret; | |
1850 | } | |
1851 | ||
1852 | static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, | |
1853 | struct btrfs_root *root, | |
1854 | u64 bytenr, u64 num_bytes, | |
1855 | u64 parent, u64 root_objectid, | |
1856 | u64 owner, u64 offset, int refs_to_add, | |
1857 | struct btrfs_delayed_extent_op *extent_op) | |
1858 | { | |
1859 | struct btrfs_path *path; | |
1860 | struct extent_buffer *leaf; | |
1861 | struct btrfs_extent_item *item; | |
1862 | u64 refs; | |
1863 | int ret; | |
1864 | int err = 0; | |
1865 | ||
1866 | path = btrfs_alloc_path(); | |
1867 | if (!path) | |
1868 | return -ENOMEM; | |
1869 | ||
1870 | path->reada = 1; | |
1871 | path->leave_spinning = 1; | |
1872 | /* this will setup the path even if it fails to insert the back ref */ | |
1873 | ret = insert_inline_extent_backref(trans, root->fs_info->extent_root, | |
1874 | path, bytenr, num_bytes, parent, | |
1875 | root_objectid, owner, offset, | |
1876 | refs_to_add, extent_op); | |
1877 | if (ret == 0) | |
1878 | goto out; | |
1879 | ||
1880 | if (ret != -EAGAIN) { | |
1881 | err = ret; | |
1882 | goto out; | |
1883 | } | |
1884 | ||
1885 | leaf = path->nodes[0]; | |
1886 | item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
1887 | refs = btrfs_extent_refs(leaf, item); | |
1888 | btrfs_set_extent_refs(leaf, item, refs + refs_to_add); | |
1889 | if (extent_op) | |
1890 | __run_delayed_extent_op(extent_op, leaf, item); | |
56bec294 | 1891 | |
5d4f98a2 | 1892 | btrfs_mark_buffer_dirty(leaf); |
b3b4aa74 | 1893 | btrfs_release_path(path); |
56bec294 CM |
1894 | |
1895 | path->reada = 1; | |
b9473439 CM |
1896 | path->leave_spinning = 1; |
1897 | ||
56bec294 CM |
1898 | /* now insert the actual backref */ |
1899 | ret = insert_extent_backref(trans, root->fs_info->extent_root, | |
5d4f98a2 YZ |
1900 | path, bytenr, parent, root_objectid, |
1901 | owner, offset, refs_to_add); | |
56bec294 | 1902 | BUG_ON(ret); |
5d4f98a2 | 1903 | out: |
56bec294 | 1904 | btrfs_free_path(path); |
5d4f98a2 | 1905 | return err; |
56bec294 CM |
1906 | } |
1907 | ||
5d4f98a2 YZ |
1908 | static int run_delayed_data_ref(struct btrfs_trans_handle *trans, |
1909 | struct btrfs_root *root, | |
1910 | struct btrfs_delayed_ref_node *node, | |
1911 | struct btrfs_delayed_extent_op *extent_op, | |
1912 | int insert_reserved) | |
56bec294 | 1913 | { |
5d4f98a2 YZ |
1914 | int ret = 0; |
1915 | struct btrfs_delayed_data_ref *ref; | |
1916 | struct btrfs_key ins; | |
1917 | u64 parent = 0; | |
1918 | u64 ref_root = 0; | |
1919 | u64 flags = 0; | |
1920 | ||
1921 | ins.objectid = node->bytenr; | |
1922 | ins.offset = node->num_bytes; | |
1923 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
1924 | ||
1925 | ref = btrfs_delayed_node_to_data_ref(node); | |
1926 | if (node->type == BTRFS_SHARED_DATA_REF_KEY) | |
1927 | parent = ref->parent; | |
1928 | else | |
1929 | ref_root = ref->root; | |
1930 | ||
1931 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | |
1932 | if (extent_op) { | |
1933 | BUG_ON(extent_op->update_key); | |
1934 | flags |= extent_op->flags_to_set; | |
1935 | } | |
1936 | ret = alloc_reserved_file_extent(trans, root, | |
1937 | parent, ref_root, flags, | |
1938 | ref->objectid, ref->offset, | |
1939 | &ins, node->ref_mod); | |
5d4f98a2 YZ |
1940 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
1941 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, | |
1942 | node->num_bytes, parent, | |
1943 | ref_root, ref->objectid, | |
1944 | ref->offset, node->ref_mod, | |
1945 | extent_op); | |
1946 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { | |
1947 | ret = __btrfs_free_extent(trans, root, node->bytenr, | |
1948 | node->num_bytes, parent, | |
1949 | ref_root, ref->objectid, | |
1950 | ref->offset, node->ref_mod, | |
1951 | extent_op); | |
1952 | } else { | |
1953 | BUG(); | |
1954 | } | |
1955 | return ret; | |
1956 | } | |
1957 | ||
1958 | static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op, | |
1959 | struct extent_buffer *leaf, | |
1960 | struct btrfs_extent_item *ei) | |
1961 | { | |
1962 | u64 flags = btrfs_extent_flags(leaf, ei); | |
1963 | if (extent_op->update_flags) { | |
1964 | flags |= extent_op->flags_to_set; | |
1965 | btrfs_set_extent_flags(leaf, ei, flags); | |
1966 | } | |
1967 | ||
1968 | if (extent_op->update_key) { | |
1969 | struct btrfs_tree_block_info *bi; | |
1970 | BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK)); | |
1971 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
1972 | btrfs_set_tree_block_key(leaf, bi, &extent_op->key); | |
1973 | } | |
1974 | } | |
1975 | ||
1976 | static int run_delayed_extent_op(struct btrfs_trans_handle *trans, | |
1977 | struct btrfs_root *root, | |
1978 | struct btrfs_delayed_ref_node *node, | |
1979 | struct btrfs_delayed_extent_op *extent_op) | |
1980 | { | |
1981 | struct btrfs_key key; | |
1982 | struct btrfs_path *path; | |
1983 | struct btrfs_extent_item *ei; | |
1984 | struct extent_buffer *leaf; | |
1985 | u32 item_size; | |
56bec294 | 1986 | int ret; |
5d4f98a2 YZ |
1987 | int err = 0; |
1988 | ||
1989 | path = btrfs_alloc_path(); | |
1990 | if (!path) | |
1991 | return -ENOMEM; | |
1992 | ||
1993 | key.objectid = node->bytenr; | |
1994 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
1995 | key.offset = node->num_bytes; | |
1996 | ||
1997 | path->reada = 1; | |
1998 | path->leave_spinning = 1; | |
1999 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, | |
2000 | path, 0, 1); | |
2001 | if (ret < 0) { | |
2002 | err = ret; | |
2003 | goto out; | |
2004 | } | |
2005 | if (ret > 0) { | |
2006 | err = -EIO; | |
2007 | goto out; | |
2008 | } | |
2009 | ||
2010 | leaf = path->nodes[0]; | |
2011 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2012 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
2013 | if (item_size < sizeof(*ei)) { | |
2014 | ret = convert_extent_item_v0(trans, root->fs_info->extent_root, | |
2015 | path, (u64)-1, 0); | |
2016 | if (ret < 0) { | |
2017 | err = ret; | |
2018 | goto out; | |
2019 | } | |
2020 | leaf = path->nodes[0]; | |
2021 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2022 | } | |
2023 | #endif | |
2024 | BUG_ON(item_size < sizeof(*ei)); | |
2025 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
2026 | __run_delayed_extent_op(extent_op, leaf, ei); | |
56bec294 | 2027 | |
5d4f98a2 YZ |
2028 | btrfs_mark_buffer_dirty(leaf); |
2029 | out: | |
2030 | btrfs_free_path(path); | |
2031 | return err; | |
56bec294 CM |
2032 | } |
2033 | ||
5d4f98a2 YZ |
2034 | static int run_delayed_tree_ref(struct btrfs_trans_handle *trans, |
2035 | struct btrfs_root *root, | |
2036 | struct btrfs_delayed_ref_node *node, | |
2037 | struct btrfs_delayed_extent_op *extent_op, | |
2038 | int insert_reserved) | |
56bec294 CM |
2039 | { |
2040 | int ret = 0; | |
5d4f98a2 YZ |
2041 | struct btrfs_delayed_tree_ref *ref; |
2042 | struct btrfs_key ins; | |
2043 | u64 parent = 0; | |
2044 | u64 ref_root = 0; | |
56bec294 | 2045 | |
5d4f98a2 YZ |
2046 | ins.objectid = node->bytenr; |
2047 | ins.offset = node->num_bytes; | |
2048 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
56bec294 | 2049 | |
5d4f98a2 YZ |
2050 | ref = btrfs_delayed_node_to_tree_ref(node); |
2051 | if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) | |
2052 | parent = ref->parent; | |
2053 | else | |
2054 | ref_root = ref->root; | |
2055 | ||
2056 | BUG_ON(node->ref_mod != 1); | |
2057 | if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) { | |
2058 | BUG_ON(!extent_op || !extent_op->update_flags || | |
2059 | !extent_op->update_key); | |
2060 | ret = alloc_reserved_tree_block(trans, root, | |
2061 | parent, ref_root, | |
2062 | extent_op->flags_to_set, | |
2063 | &extent_op->key, | |
2064 | ref->level, &ins); | |
5d4f98a2 YZ |
2065 | } else if (node->action == BTRFS_ADD_DELAYED_REF) { |
2066 | ret = __btrfs_inc_extent_ref(trans, root, node->bytenr, | |
2067 | node->num_bytes, parent, ref_root, | |
2068 | ref->level, 0, 1, extent_op); | |
2069 | } else if (node->action == BTRFS_DROP_DELAYED_REF) { | |
2070 | ret = __btrfs_free_extent(trans, root, node->bytenr, | |
2071 | node->num_bytes, parent, ref_root, | |
2072 | ref->level, 0, 1, extent_op); | |
2073 | } else { | |
2074 | BUG(); | |
2075 | } | |
56bec294 CM |
2076 | return ret; |
2077 | } | |
2078 | ||
2079 | /* helper function to actually process a single delayed ref entry */ | |
5d4f98a2 YZ |
2080 | static int run_one_delayed_ref(struct btrfs_trans_handle *trans, |
2081 | struct btrfs_root *root, | |
2082 | struct btrfs_delayed_ref_node *node, | |
2083 | struct btrfs_delayed_extent_op *extent_op, | |
2084 | int insert_reserved) | |
56bec294 CM |
2085 | { |
2086 | int ret; | |
5d4f98a2 | 2087 | if (btrfs_delayed_ref_is_head(node)) { |
56bec294 CM |
2088 | struct btrfs_delayed_ref_head *head; |
2089 | /* | |
2090 | * we've hit the end of the chain and we were supposed | |
2091 | * to insert this extent into the tree. But, it got | |
2092 | * deleted before we ever needed to insert it, so all | |
2093 | * we have to do is clean up the accounting | |
2094 | */ | |
5d4f98a2 YZ |
2095 | BUG_ON(extent_op); |
2096 | head = btrfs_delayed_node_to_head(node); | |
56bec294 | 2097 | if (insert_reserved) { |
f0486c68 YZ |
2098 | btrfs_pin_extent(root, node->bytenr, |
2099 | node->num_bytes, 1); | |
5d4f98a2 YZ |
2100 | if (head->is_data) { |
2101 | ret = btrfs_del_csums(trans, root, | |
2102 | node->bytenr, | |
2103 | node->num_bytes); | |
2104 | BUG_ON(ret); | |
2105 | } | |
56bec294 | 2106 | } |
56bec294 CM |
2107 | mutex_unlock(&head->mutex); |
2108 | return 0; | |
2109 | } | |
2110 | ||
5d4f98a2 YZ |
2111 | if (node->type == BTRFS_TREE_BLOCK_REF_KEY || |
2112 | node->type == BTRFS_SHARED_BLOCK_REF_KEY) | |
2113 | ret = run_delayed_tree_ref(trans, root, node, extent_op, | |
2114 | insert_reserved); | |
2115 | else if (node->type == BTRFS_EXTENT_DATA_REF_KEY || | |
2116 | node->type == BTRFS_SHARED_DATA_REF_KEY) | |
2117 | ret = run_delayed_data_ref(trans, root, node, extent_op, | |
2118 | insert_reserved); | |
2119 | else | |
2120 | BUG(); | |
2121 | return ret; | |
56bec294 CM |
2122 | } |
2123 | ||
2124 | static noinline struct btrfs_delayed_ref_node * | |
2125 | select_delayed_ref(struct btrfs_delayed_ref_head *head) | |
2126 | { | |
2127 | struct rb_node *node; | |
2128 | struct btrfs_delayed_ref_node *ref; | |
2129 | int action = BTRFS_ADD_DELAYED_REF; | |
2130 | again: | |
2131 | /* | |
2132 | * select delayed ref of type BTRFS_ADD_DELAYED_REF first. | |
2133 | * this prevents ref count from going down to zero when | |
2134 | * there still are pending delayed ref. | |
2135 | */ | |
2136 | node = rb_prev(&head->node.rb_node); | |
2137 | while (1) { | |
2138 | if (!node) | |
2139 | break; | |
2140 | ref = rb_entry(node, struct btrfs_delayed_ref_node, | |
2141 | rb_node); | |
2142 | if (ref->bytenr != head->node.bytenr) | |
2143 | break; | |
5d4f98a2 | 2144 | if (ref->action == action) |
56bec294 CM |
2145 | return ref; |
2146 | node = rb_prev(node); | |
2147 | } | |
2148 | if (action == BTRFS_ADD_DELAYED_REF) { | |
2149 | action = BTRFS_DROP_DELAYED_REF; | |
2150 | goto again; | |
2151 | } | |
2152 | return NULL; | |
2153 | } | |
2154 | ||
c3e69d58 CM |
2155 | static noinline int run_clustered_refs(struct btrfs_trans_handle *trans, |
2156 | struct btrfs_root *root, | |
2157 | struct list_head *cluster) | |
56bec294 | 2158 | { |
56bec294 CM |
2159 | struct btrfs_delayed_ref_root *delayed_refs; |
2160 | struct btrfs_delayed_ref_node *ref; | |
2161 | struct btrfs_delayed_ref_head *locked_ref = NULL; | |
5d4f98a2 | 2162 | struct btrfs_delayed_extent_op *extent_op; |
56bec294 | 2163 | int ret; |
c3e69d58 | 2164 | int count = 0; |
56bec294 | 2165 | int must_insert_reserved = 0; |
56bec294 CM |
2166 | |
2167 | delayed_refs = &trans->transaction->delayed_refs; | |
56bec294 CM |
2168 | while (1) { |
2169 | if (!locked_ref) { | |
c3e69d58 CM |
2170 | /* pick a new head ref from the cluster list */ |
2171 | if (list_empty(cluster)) | |
56bec294 | 2172 | break; |
56bec294 | 2173 | |
c3e69d58 CM |
2174 | locked_ref = list_entry(cluster->next, |
2175 | struct btrfs_delayed_ref_head, cluster); | |
2176 | ||
2177 | /* grab the lock that says we are going to process | |
2178 | * all the refs for this head */ | |
2179 | ret = btrfs_delayed_ref_lock(trans, locked_ref); | |
2180 | ||
2181 | /* | |
2182 | * we may have dropped the spin lock to get the head | |
2183 | * mutex lock, and that might have given someone else | |
2184 | * time to free the head. If that's true, it has been | |
2185 | * removed from our list and we can move on. | |
2186 | */ | |
2187 | if (ret == -EAGAIN) { | |
2188 | locked_ref = NULL; | |
2189 | count++; | |
2190 | continue; | |
56bec294 CM |
2191 | } |
2192 | } | |
a28ec197 | 2193 | |
56bec294 CM |
2194 | /* |
2195 | * record the must insert reserved flag before we | |
2196 | * drop the spin lock. | |
2197 | */ | |
2198 | must_insert_reserved = locked_ref->must_insert_reserved; | |
2199 | locked_ref->must_insert_reserved = 0; | |
7bb86316 | 2200 | |
5d4f98a2 YZ |
2201 | extent_op = locked_ref->extent_op; |
2202 | locked_ref->extent_op = NULL; | |
2203 | ||
56bec294 CM |
2204 | /* |
2205 | * locked_ref is the head node, so we have to go one | |
2206 | * node back for any delayed ref updates | |
2207 | */ | |
56bec294 CM |
2208 | ref = select_delayed_ref(locked_ref); |
2209 | if (!ref) { | |
2210 | /* All delayed refs have been processed, Go ahead | |
2211 | * and send the head node to run_one_delayed_ref, | |
2212 | * so that any accounting fixes can happen | |
2213 | */ | |
2214 | ref = &locked_ref->node; | |
5d4f98a2 YZ |
2215 | |
2216 | if (extent_op && must_insert_reserved) { | |
2217 | kfree(extent_op); | |
2218 | extent_op = NULL; | |
2219 | } | |
2220 | ||
2221 | if (extent_op) { | |
2222 | spin_unlock(&delayed_refs->lock); | |
2223 | ||
2224 | ret = run_delayed_extent_op(trans, root, | |
2225 | ref, extent_op); | |
2226 | BUG_ON(ret); | |
2227 | kfree(extent_op); | |
2228 | ||
2229 | cond_resched(); | |
2230 | spin_lock(&delayed_refs->lock); | |
2231 | continue; | |
2232 | } | |
2233 | ||
c3e69d58 | 2234 | list_del_init(&locked_ref->cluster); |
56bec294 CM |
2235 | locked_ref = NULL; |
2236 | } | |
02217ed2 | 2237 | |
56bec294 CM |
2238 | ref->in_tree = 0; |
2239 | rb_erase(&ref->rb_node, &delayed_refs->root); | |
2240 | delayed_refs->num_entries--; | |
5d4f98a2 | 2241 | |
56bec294 | 2242 | spin_unlock(&delayed_refs->lock); |
925baedd | 2243 | |
5d4f98a2 | 2244 | ret = run_one_delayed_ref(trans, root, ref, extent_op, |
56bec294 CM |
2245 | must_insert_reserved); |
2246 | BUG_ON(ret); | |
eb099670 | 2247 | |
5d4f98a2 YZ |
2248 | btrfs_put_delayed_ref(ref); |
2249 | kfree(extent_op); | |
c3e69d58 | 2250 | count++; |
5d4f98a2 | 2251 | |
c3e69d58 CM |
2252 | cond_resched(); |
2253 | spin_lock(&delayed_refs->lock); | |
2254 | } | |
2255 | return count; | |
2256 | } | |
2257 | ||
2258 | /* | |
2259 | * this starts processing the delayed reference count updates and | |
2260 | * extent insertions we have queued up so far. count can be | |
2261 | * 0, which means to process everything in the tree at the start | |
2262 | * of the run (but not newly added entries), or it can be some target | |
2263 | * number you'd like to process. | |
2264 | */ | |
2265 | int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans, | |
2266 | struct btrfs_root *root, unsigned long count) | |
2267 | { | |
2268 | struct rb_node *node; | |
2269 | struct btrfs_delayed_ref_root *delayed_refs; | |
2270 | struct btrfs_delayed_ref_node *ref; | |
2271 | struct list_head cluster; | |
2272 | int ret; | |
2273 | int run_all = count == (unsigned long)-1; | |
2274 | int run_most = 0; | |
2275 | ||
2276 | if (root == root->fs_info->extent_root) | |
2277 | root = root->fs_info->tree_root; | |
2278 | ||
2279 | delayed_refs = &trans->transaction->delayed_refs; | |
2280 | INIT_LIST_HEAD(&cluster); | |
2281 | again: | |
2282 | spin_lock(&delayed_refs->lock); | |
2283 | if (count == 0) { | |
2284 | count = delayed_refs->num_entries * 2; | |
2285 | run_most = 1; | |
2286 | } | |
2287 | while (1) { | |
2288 | if (!(run_all || run_most) && | |
2289 | delayed_refs->num_heads_ready < 64) | |
2290 | break; | |
eb099670 | 2291 | |
56bec294 | 2292 | /* |
c3e69d58 CM |
2293 | * go find something we can process in the rbtree. We start at |
2294 | * the beginning of the tree, and then build a cluster | |
2295 | * of refs to process starting at the first one we are able to | |
2296 | * lock | |
56bec294 | 2297 | */ |
c3e69d58 CM |
2298 | ret = btrfs_find_ref_cluster(trans, &cluster, |
2299 | delayed_refs->run_delayed_start); | |
2300 | if (ret) | |
56bec294 CM |
2301 | break; |
2302 | ||
c3e69d58 CM |
2303 | ret = run_clustered_refs(trans, root, &cluster); |
2304 | BUG_ON(ret < 0); | |
2305 | ||
2306 | count -= min_t(unsigned long, ret, count); | |
2307 | ||
2308 | if (count == 0) | |
2309 | break; | |
eb099670 | 2310 | } |
c3e69d58 | 2311 | |
56bec294 | 2312 | if (run_all) { |
56bec294 | 2313 | node = rb_first(&delayed_refs->root); |
c3e69d58 | 2314 | if (!node) |
56bec294 | 2315 | goto out; |
c3e69d58 | 2316 | count = (unsigned long)-1; |
e9d0b13b | 2317 | |
56bec294 CM |
2318 | while (node) { |
2319 | ref = rb_entry(node, struct btrfs_delayed_ref_node, | |
2320 | rb_node); | |
2321 | if (btrfs_delayed_ref_is_head(ref)) { | |
2322 | struct btrfs_delayed_ref_head *head; | |
5caf2a00 | 2323 | |
56bec294 CM |
2324 | head = btrfs_delayed_node_to_head(ref); |
2325 | atomic_inc(&ref->refs); | |
2326 | ||
2327 | spin_unlock(&delayed_refs->lock); | |
8cc33e5c DS |
2328 | /* |
2329 | * Mutex was contended, block until it's | |
2330 | * released and try again | |
2331 | */ | |
56bec294 CM |
2332 | mutex_lock(&head->mutex); |
2333 | mutex_unlock(&head->mutex); | |
2334 | ||
2335 | btrfs_put_delayed_ref(ref); | |
1887be66 | 2336 | cond_resched(); |
56bec294 CM |
2337 | goto again; |
2338 | } | |
2339 | node = rb_next(node); | |
2340 | } | |
2341 | spin_unlock(&delayed_refs->lock); | |
56bec294 CM |
2342 | schedule_timeout(1); |
2343 | goto again; | |
5f39d397 | 2344 | } |
54aa1f4d | 2345 | out: |
c3e69d58 | 2346 | spin_unlock(&delayed_refs->lock); |
a28ec197 CM |
2347 | return 0; |
2348 | } | |
2349 | ||
5d4f98a2 YZ |
2350 | int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans, |
2351 | struct btrfs_root *root, | |
2352 | u64 bytenr, u64 num_bytes, u64 flags, | |
2353 | int is_data) | |
2354 | { | |
2355 | struct btrfs_delayed_extent_op *extent_op; | |
2356 | int ret; | |
2357 | ||
2358 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
2359 | if (!extent_op) | |
2360 | return -ENOMEM; | |
2361 | ||
2362 | extent_op->flags_to_set = flags; | |
2363 | extent_op->update_flags = 1; | |
2364 | extent_op->update_key = 0; | |
2365 | extent_op->is_data = is_data ? 1 : 0; | |
2366 | ||
2367 | ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op); | |
2368 | if (ret) | |
2369 | kfree(extent_op); | |
2370 | return ret; | |
2371 | } | |
2372 | ||
2373 | static noinline int check_delayed_ref(struct btrfs_trans_handle *trans, | |
2374 | struct btrfs_root *root, | |
2375 | struct btrfs_path *path, | |
2376 | u64 objectid, u64 offset, u64 bytenr) | |
2377 | { | |
2378 | struct btrfs_delayed_ref_head *head; | |
2379 | struct btrfs_delayed_ref_node *ref; | |
2380 | struct btrfs_delayed_data_ref *data_ref; | |
2381 | struct btrfs_delayed_ref_root *delayed_refs; | |
2382 | struct rb_node *node; | |
2383 | int ret = 0; | |
2384 | ||
2385 | ret = -ENOENT; | |
2386 | delayed_refs = &trans->transaction->delayed_refs; | |
2387 | spin_lock(&delayed_refs->lock); | |
2388 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
2389 | if (!head) | |
2390 | goto out; | |
2391 | ||
2392 | if (!mutex_trylock(&head->mutex)) { | |
2393 | atomic_inc(&head->node.refs); | |
2394 | spin_unlock(&delayed_refs->lock); | |
2395 | ||
b3b4aa74 | 2396 | btrfs_release_path(path); |
5d4f98a2 | 2397 | |
8cc33e5c DS |
2398 | /* |
2399 | * Mutex was contended, block until it's released and let | |
2400 | * caller try again | |
2401 | */ | |
5d4f98a2 YZ |
2402 | mutex_lock(&head->mutex); |
2403 | mutex_unlock(&head->mutex); | |
2404 | btrfs_put_delayed_ref(&head->node); | |
2405 | return -EAGAIN; | |
2406 | } | |
2407 | ||
2408 | node = rb_prev(&head->node.rb_node); | |
2409 | if (!node) | |
2410 | goto out_unlock; | |
2411 | ||
2412 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | |
2413 | ||
2414 | if (ref->bytenr != bytenr) | |
2415 | goto out_unlock; | |
2416 | ||
2417 | ret = 1; | |
2418 | if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) | |
2419 | goto out_unlock; | |
2420 | ||
2421 | data_ref = btrfs_delayed_node_to_data_ref(ref); | |
2422 | ||
2423 | node = rb_prev(node); | |
2424 | if (node) { | |
2425 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | |
2426 | if (ref->bytenr == bytenr) | |
2427 | goto out_unlock; | |
2428 | } | |
2429 | ||
2430 | if (data_ref->root != root->root_key.objectid || | |
2431 | data_ref->objectid != objectid || data_ref->offset != offset) | |
2432 | goto out_unlock; | |
2433 | ||
2434 | ret = 0; | |
2435 | out_unlock: | |
2436 | mutex_unlock(&head->mutex); | |
2437 | out: | |
2438 | spin_unlock(&delayed_refs->lock); | |
2439 | return ret; | |
2440 | } | |
2441 | ||
2442 | static noinline int check_committed_ref(struct btrfs_trans_handle *trans, | |
2443 | struct btrfs_root *root, | |
2444 | struct btrfs_path *path, | |
2445 | u64 objectid, u64 offset, u64 bytenr) | |
be20aa9d CM |
2446 | { |
2447 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
f321e491 | 2448 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
2449 | struct btrfs_extent_data_ref *ref; |
2450 | struct btrfs_extent_inline_ref *iref; | |
2451 | struct btrfs_extent_item *ei; | |
f321e491 | 2452 | struct btrfs_key key; |
5d4f98a2 | 2453 | u32 item_size; |
be20aa9d | 2454 | int ret; |
925baedd | 2455 | |
be20aa9d | 2456 | key.objectid = bytenr; |
31840ae1 | 2457 | key.offset = (u64)-1; |
f321e491 | 2458 | key.type = BTRFS_EXTENT_ITEM_KEY; |
be20aa9d | 2459 | |
be20aa9d CM |
2460 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
2461 | if (ret < 0) | |
2462 | goto out; | |
2463 | BUG_ON(ret == 0); | |
80ff3856 YZ |
2464 | |
2465 | ret = -ENOENT; | |
2466 | if (path->slots[0] == 0) | |
31840ae1 | 2467 | goto out; |
be20aa9d | 2468 | |
31840ae1 | 2469 | path->slots[0]--; |
f321e491 | 2470 | leaf = path->nodes[0]; |
5d4f98a2 | 2471 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
be20aa9d | 2472 | |
5d4f98a2 | 2473 | if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY) |
be20aa9d | 2474 | goto out; |
f321e491 | 2475 | |
5d4f98a2 YZ |
2476 | ret = 1; |
2477 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2478 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
2479 | if (item_size < sizeof(*ei)) { | |
2480 | WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0)); | |
2481 | goto out; | |
2482 | } | |
2483 | #endif | |
2484 | ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item); | |
bd09835d | 2485 | |
5d4f98a2 YZ |
2486 | if (item_size != sizeof(*ei) + |
2487 | btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY)) | |
2488 | goto out; | |
be20aa9d | 2489 | |
5d4f98a2 YZ |
2490 | if (btrfs_extent_generation(leaf, ei) <= |
2491 | btrfs_root_last_snapshot(&root->root_item)) | |
2492 | goto out; | |
2493 | ||
2494 | iref = (struct btrfs_extent_inline_ref *)(ei + 1); | |
2495 | if (btrfs_extent_inline_ref_type(leaf, iref) != | |
2496 | BTRFS_EXTENT_DATA_REF_KEY) | |
2497 | goto out; | |
2498 | ||
2499 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
2500 | if (btrfs_extent_refs(leaf, ei) != | |
2501 | btrfs_extent_data_ref_count(leaf, ref) || | |
2502 | btrfs_extent_data_ref_root(leaf, ref) != | |
2503 | root->root_key.objectid || | |
2504 | btrfs_extent_data_ref_objectid(leaf, ref) != objectid || | |
2505 | btrfs_extent_data_ref_offset(leaf, ref) != offset) | |
2506 | goto out; | |
2507 | ||
2508 | ret = 0; | |
2509 | out: | |
2510 | return ret; | |
2511 | } | |
2512 | ||
2513 | int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans, | |
2514 | struct btrfs_root *root, | |
2515 | u64 objectid, u64 offset, u64 bytenr) | |
2516 | { | |
2517 | struct btrfs_path *path; | |
2518 | int ret; | |
2519 | int ret2; | |
2520 | ||
2521 | path = btrfs_alloc_path(); | |
2522 | if (!path) | |
2523 | return -ENOENT; | |
2524 | ||
2525 | do { | |
2526 | ret = check_committed_ref(trans, root, path, objectid, | |
2527 | offset, bytenr); | |
2528 | if (ret && ret != -ENOENT) | |
f321e491 | 2529 | goto out; |
80ff3856 | 2530 | |
5d4f98a2 YZ |
2531 | ret2 = check_delayed_ref(trans, root, path, objectid, |
2532 | offset, bytenr); | |
2533 | } while (ret2 == -EAGAIN); | |
2534 | ||
2535 | if (ret2 && ret2 != -ENOENT) { | |
2536 | ret = ret2; | |
2537 | goto out; | |
f321e491 | 2538 | } |
5d4f98a2 YZ |
2539 | |
2540 | if (ret != -ENOENT || ret2 != -ENOENT) | |
2541 | ret = 0; | |
be20aa9d | 2542 | out: |
80ff3856 | 2543 | btrfs_free_path(path); |
f0486c68 YZ |
2544 | if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID) |
2545 | WARN_ON(ret > 0); | |
f321e491 | 2546 | return ret; |
be20aa9d | 2547 | } |
c5739bba | 2548 | |
5d4f98a2 | 2549 | static int __btrfs_mod_ref(struct btrfs_trans_handle *trans, |
b7a9f29f | 2550 | struct btrfs_root *root, |
5d4f98a2 YZ |
2551 | struct extent_buffer *buf, |
2552 | int full_backref, int inc) | |
31840ae1 ZY |
2553 | { |
2554 | u64 bytenr; | |
5d4f98a2 YZ |
2555 | u64 num_bytes; |
2556 | u64 parent; | |
31840ae1 | 2557 | u64 ref_root; |
31840ae1 | 2558 | u32 nritems; |
31840ae1 ZY |
2559 | struct btrfs_key key; |
2560 | struct btrfs_file_extent_item *fi; | |
2561 | int i; | |
2562 | int level; | |
2563 | int ret = 0; | |
31840ae1 | 2564 | int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *, |
5d4f98a2 | 2565 | u64, u64, u64, u64, u64, u64); |
31840ae1 ZY |
2566 | |
2567 | ref_root = btrfs_header_owner(buf); | |
31840ae1 ZY |
2568 | nritems = btrfs_header_nritems(buf); |
2569 | level = btrfs_header_level(buf); | |
2570 | ||
5d4f98a2 YZ |
2571 | if (!root->ref_cows && level == 0) |
2572 | return 0; | |
31840ae1 | 2573 | |
5d4f98a2 YZ |
2574 | if (inc) |
2575 | process_func = btrfs_inc_extent_ref; | |
2576 | else | |
2577 | process_func = btrfs_free_extent; | |
31840ae1 | 2578 | |
5d4f98a2 YZ |
2579 | if (full_backref) |
2580 | parent = buf->start; | |
2581 | else | |
2582 | parent = 0; | |
2583 | ||
2584 | for (i = 0; i < nritems; i++) { | |
31840ae1 | 2585 | if (level == 0) { |
5d4f98a2 | 2586 | btrfs_item_key_to_cpu(buf, &key, i); |
31840ae1 ZY |
2587 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
2588 | continue; | |
5d4f98a2 | 2589 | fi = btrfs_item_ptr(buf, i, |
31840ae1 ZY |
2590 | struct btrfs_file_extent_item); |
2591 | if (btrfs_file_extent_type(buf, fi) == | |
2592 | BTRFS_FILE_EXTENT_INLINE) | |
2593 | continue; | |
2594 | bytenr = btrfs_file_extent_disk_bytenr(buf, fi); | |
2595 | if (bytenr == 0) | |
2596 | continue; | |
5d4f98a2 YZ |
2597 | |
2598 | num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi); | |
2599 | key.offset -= btrfs_file_extent_offset(buf, fi); | |
2600 | ret = process_func(trans, root, bytenr, num_bytes, | |
2601 | parent, ref_root, key.objectid, | |
2602 | key.offset); | |
31840ae1 ZY |
2603 | if (ret) |
2604 | goto fail; | |
2605 | } else { | |
5d4f98a2 YZ |
2606 | bytenr = btrfs_node_blockptr(buf, i); |
2607 | num_bytes = btrfs_level_size(root, level - 1); | |
2608 | ret = process_func(trans, root, bytenr, num_bytes, | |
2609 | parent, ref_root, level - 1, 0); | |
31840ae1 ZY |
2610 | if (ret) |
2611 | goto fail; | |
2612 | } | |
2613 | } | |
2614 | return 0; | |
2615 | fail: | |
5d4f98a2 YZ |
2616 | BUG(); |
2617 | return ret; | |
2618 | } | |
2619 | ||
2620 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
2621 | struct extent_buffer *buf, int full_backref) | |
2622 | { | |
2623 | return __btrfs_mod_ref(trans, root, buf, full_backref, 1); | |
2624 | } | |
2625 | ||
2626 | int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, | |
2627 | struct extent_buffer *buf, int full_backref) | |
2628 | { | |
2629 | return __btrfs_mod_ref(trans, root, buf, full_backref, 0); | |
31840ae1 ZY |
2630 | } |
2631 | ||
9078a3e1 CM |
2632 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
2633 | struct btrfs_root *root, | |
2634 | struct btrfs_path *path, | |
2635 | struct btrfs_block_group_cache *cache) | |
2636 | { | |
2637 | int ret; | |
9078a3e1 | 2638 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
5f39d397 CM |
2639 | unsigned long bi; |
2640 | struct extent_buffer *leaf; | |
9078a3e1 | 2641 | |
9078a3e1 | 2642 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
54aa1f4d CM |
2643 | if (ret < 0) |
2644 | goto fail; | |
9078a3e1 | 2645 | BUG_ON(ret); |
5f39d397 CM |
2646 | |
2647 | leaf = path->nodes[0]; | |
2648 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
2649 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
2650 | btrfs_mark_buffer_dirty(leaf); | |
b3b4aa74 | 2651 | btrfs_release_path(path); |
54aa1f4d | 2652 | fail: |
9078a3e1 CM |
2653 | if (ret) |
2654 | return ret; | |
9078a3e1 CM |
2655 | return 0; |
2656 | ||
2657 | } | |
2658 | ||
4a8c9a62 YZ |
2659 | static struct btrfs_block_group_cache * |
2660 | next_block_group(struct btrfs_root *root, | |
2661 | struct btrfs_block_group_cache *cache) | |
2662 | { | |
2663 | struct rb_node *node; | |
2664 | spin_lock(&root->fs_info->block_group_cache_lock); | |
2665 | node = rb_next(&cache->cache_node); | |
2666 | btrfs_put_block_group(cache); | |
2667 | if (node) { | |
2668 | cache = rb_entry(node, struct btrfs_block_group_cache, | |
2669 | cache_node); | |
11dfe35a | 2670 | btrfs_get_block_group(cache); |
4a8c9a62 YZ |
2671 | } else |
2672 | cache = NULL; | |
2673 | spin_unlock(&root->fs_info->block_group_cache_lock); | |
2674 | return cache; | |
2675 | } | |
2676 | ||
0af3d00b JB |
2677 | static int cache_save_setup(struct btrfs_block_group_cache *block_group, |
2678 | struct btrfs_trans_handle *trans, | |
2679 | struct btrfs_path *path) | |
2680 | { | |
2681 | struct btrfs_root *root = block_group->fs_info->tree_root; | |
2682 | struct inode *inode = NULL; | |
2683 | u64 alloc_hint = 0; | |
2b20982e | 2684 | int dcs = BTRFS_DC_ERROR; |
0af3d00b JB |
2685 | int num_pages = 0; |
2686 | int retries = 0; | |
2687 | int ret = 0; | |
2688 | ||
2689 | /* | |
2690 | * If this block group is smaller than 100 megs don't bother caching the | |
2691 | * block group. | |
2692 | */ | |
2693 | if (block_group->key.offset < (100 * 1024 * 1024)) { | |
2694 | spin_lock(&block_group->lock); | |
2695 | block_group->disk_cache_state = BTRFS_DC_WRITTEN; | |
2696 | spin_unlock(&block_group->lock); | |
2697 | return 0; | |
2698 | } | |
2699 | ||
2700 | again: | |
2701 | inode = lookup_free_space_inode(root, block_group, path); | |
2702 | if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) { | |
2703 | ret = PTR_ERR(inode); | |
b3b4aa74 | 2704 | btrfs_release_path(path); |
0af3d00b JB |
2705 | goto out; |
2706 | } | |
2707 | ||
2708 | if (IS_ERR(inode)) { | |
2709 | BUG_ON(retries); | |
2710 | retries++; | |
2711 | ||
2712 | if (block_group->ro) | |
2713 | goto out_free; | |
2714 | ||
2715 | ret = create_free_space_inode(root, trans, block_group, path); | |
2716 | if (ret) | |
2717 | goto out_free; | |
2718 | goto again; | |
2719 | } | |
2720 | ||
5b0e95bf JB |
2721 | /* We've already setup this transaction, go ahead and exit */ |
2722 | if (block_group->cache_generation == trans->transid && | |
2723 | i_size_read(inode)) { | |
2724 | dcs = BTRFS_DC_SETUP; | |
2725 | goto out_put; | |
2726 | } | |
2727 | ||
0af3d00b JB |
2728 | /* |
2729 | * We want to set the generation to 0, that way if anything goes wrong | |
2730 | * from here on out we know not to trust this cache when we load up next | |
2731 | * time. | |
2732 | */ | |
2733 | BTRFS_I(inode)->generation = 0; | |
2734 | ret = btrfs_update_inode(trans, root, inode); | |
2735 | WARN_ON(ret); | |
2736 | ||
2737 | if (i_size_read(inode) > 0) { | |
2738 | ret = btrfs_truncate_free_space_cache(root, trans, path, | |
2739 | inode); | |
2740 | if (ret) | |
2741 | goto out_put; | |
2742 | } | |
2743 | ||
2744 | spin_lock(&block_group->lock); | |
2745 | if (block_group->cached != BTRFS_CACHE_FINISHED) { | |
2b20982e JB |
2746 | /* We're not cached, don't bother trying to write stuff out */ |
2747 | dcs = BTRFS_DC_WRITTEN; | |
0af3d00b JB |
2748 | spin_unlock(&block_group->lock); |
2749 | goto out_put; | |
2750 | } | |
2751 | spin_unlock(&block_group->lock); | |
2752 | ||
2753 | num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024); | |
2754 | if (!num_pages) | |
2755 | num_pages = 1; | |
2756 | ||
2757 | /* | |
2758 | * Just to make absolutely sure we have enough space, we're going to | |
2759 | * preallocate 12 pages worth of space for each block group. In | |
2760 | * practice we ought to use at most 8, but we need extra space so we can | |
2761 | * add our header and have a terminator between the extents and the | |
2762 | * bitmaps. | |
2763 | */ | |
2764 | num_pages *= 16; | |
2765 | num_pages *= PAGE_CACHE_SIZE; | |
2766 | ||
5b0e95bf | 2767 | ret = btrfs_check_data_free_space(inode, num_pages); |
0af3d00b JB |
2768 | if (ret) |
2769 | goto out_put; | |
2770 | ||
2771 | ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages, | |
2772 | num_pages, num_pages, | |
2773 | &alloc_hint); | |
5b0e95bf | 2774 | if (!ret) |
2b20982e | 2775 | dcs = BTRFS_DC_SETUP; |
5b0e95bf | 2776 | btrfs_free_reserved_data_space(inode, num_pages); |
c09544e0 | 2777 | |
0af3d00b JB |
2778 | out_put: |
2779 | iput(inode); | |
2780 | out_free: | |
b3b4aa74 | 2781 | btrfs_release_path(path); |
0af3d00b JB |
2782 | out: |
2783 | spin_lock(&block_group->lock); | |
5b0e95bf JB |
2784 | if (!ret) |
2785 | block_group->cache_generation = trans->transid; | |
2b20982e | 2786 | block_group->disk_cache_state = dcs; |
0af3d00b JB |
2787 | spin_unlock(&block_group->lock); |
2788 | ||
2789 | return ret; | |
2790 | } | |
2791 | ||
96b5179d CM |
2792 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
2793 | struct btrfs_root *root) | |
9078a3e1 | 2794 | { |
4a8c9a62 | 2795 | struct btrfs_block_group_cache *cache; |
9078a3e1 | 2796 | int err = 0; |
9078a3e1 | 2797 | struct btrfs_path *path; |
96b5179d | 2798 | u64 last = 0; |
9078a3e1 CM |
2799 | |
2800 | path = btrfs_alloc_path(); | |
2801 | if (!path) | |
2802 | return -ENOMEM; | |
2803 | ||
0af3d00b JB |
2804 | again: |
2805 | while (1) { | |
2806 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | |
2807 | while (cache) { | |
2808 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) | |
2809 | break; | |
2810 | cache = next_block_group(root, cache); | |
2811 | } | |
2812 | if (!cache) { | |
2813 | if (last == 0) | |
2814 | break; | |
2815 | last = 0; | |
2816 | continue; | |
2817 | } | |
2818 | err = cache_save_setup(cache, trans, path); | |
2819 | last = cache->key.objectid + cache->key.offset; | |
2820 | btrfs_put_block_group(cache); | |
2821 | } | |
2822 | ||
d397712b | 2823 | while (1) { |
4a8c9a62 YZ |
2824 | if (last == 0) { |
2825 | err = btrfs_run_delayed_refs(trans, root, | |
2826 | (unsigned long)-1); | |
2827 | BUG_ON(err); | |
0f9dd46c | 2828 | } |
54aa1f4d | 2829 | |
4a8c9a62 YZ |
2830 | cache = btrfs_lookup_first_block_group(root->fs_info, last); |
2831 | while (cache) { | |
0af3d00b JB |
2832 | if (cache->disk_cache_state == BTRFS_DC_CLEAR) { |
2833 | btrfs_put_block_group(cache); | |
2834 | goto again; | |
2835 | } | |
2836 | ||
4a8c9a62 YZ |
2837 | if (cache->dirty) |
2838 | break; | |
2839 | cache = next_block_group(root, cache); | |
2840 | } | |
2841 | if (!cache) { | |
2842 | if (last == 0) | |
2843 | break; | |
2844 | last = 0; | |
2845 | continue; | |
2846 | } | |
0f9dd46c | 2847 | |
0cb59c99 JB |
2848 | if (cache->disk_cache_state == BTRFS_DC_SETUP) |
2849 | cache->disk_cache_state = BTRFS_DC_NEED_WRITE; | |
e8569813 | 2850 | cache->dirty = 0; |
4a8c9a62 | 2851 | last = cache->key.objectid + cache->key.offset; |
0f9dd46c | 2852 | |
4a8c9a62 YZ |
2853 | err = write_one_cache_group(trans, root, path, cache); |
2854 | BUG_ON(err); | |
2855 | btrfs_put_block_group(cache); | |
9078a3e1 | 2856 | } |
4a8c9a62 | 2857 | |
0cb59c99 JB |
2858 | while (1) { |
2859 | /* | |
2860 | * I don't think this is needed since we're just marking our | |
2861 | * preallocated extent as written, but just in case it can't | |
2862 | * hurt. | |
2863 | */ | |
2864 | if (last == 0) { | |
2865 | err = btrfs_run_delayed_refs(trans, root, | |
2866 | (unsigned long)-1); | |
2867 | BUG_ON(err); | |
2868 | } | |
2869 | ||
2870 | cache = btrfs_lookup_first_block_group(root->fs_info, last); | |
2871 | while (cache) { | |
2872 | /* | |
2873 | * Really this shouldn't happen, but it could if we | |
2874 | * couldn't write the entire preallocated extent and | |
2875 | * splitting the extent resulted in a new block. | |
2876 | */ | |
2877 | if (cache->dirty) { | |
2878 | btrfs_put_block_group(cache); | |
2879 | goto again; | |
2880 | } | |
2881 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) | |
2882 | break; | |
2883 | cache = next_block_group(root, cache); | |
2884 | } | |
2885 | if (!cache) { | |
2886 | if (last == 0) | |
2887 | break; | |
2888 | last = 0; | |
2889 | continue; | |
2890 | } | |
2891 | ||
2892 | btrfs_write_out_cache(root, trans, cache, path); | |
2893 | ||
2894 | /* | |
2895 | * If we didn't have an error then the cache state is still | |
2896 | * NEED_WRITE, so we can set it to WRITTEN. | |
2897 | */ | |
2898 | if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE) | |
2899 | cache->disk_cache_state = BTRFS_DC_WRITTEN; | |
2900 | last = cache->key.objectid + cache->key.offset; | |
2901 | btrfs_put_block_group(cache); | |
2902 | } | |
2903 | ||
9078a3e1 | 2904 | btrfs_free_path(path); |
4a8c9a62 | 2905 | return 0; |
9078a3e1 CM |
2906 | } |
2907 | ||
d2fb3437 YZ |
2908 | int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr) |
2909 | { | |
2910 | struct btrfs_block_group_cache *block_group; | |
2911 | int readonly = 0; | |
2912 | ||
2913 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); | |
2914 | if (!block_group || block_group->ro) | |
2915 | readonly = 1; | |
2916 | if (block_group) | |
fa9c0d79 | 2917 | btrfs_put_block_group(block_group); |
d2fb3437 YZ |
2918 | return readonly; |
2919 | } | |
2920 | ||
593060d7 CM |
2921 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
2922 | u64 total_bytes, u64 bytes_used, | |
2923 | struct btrfs_space_info **space_info) | |
2924 | { | |
2925 | struct btrfs_space_info *found; | |
b742bb82 YZ |
2926 | int i; |
2927 | int factor; | |
2928 | ||
2929 | if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 | | |
2930 | BTRFS_BLOCK_GROUP_RAID10)) | |
2931 | factor = 2; | |
2932 | else | |
2933 | factor = 1; | |
593060d7 CM |
2934 | |
2935 | found = __find_space_info(info, flags); | |
2936 | if (found) { | |
25179201 | 2937 | spin_lock(&found->lock); |
593060d7 | 2938 | found->total_bytes += total_bytes; |
89a55897 | 2939 | found->disk_total += total_bytes * factor; |
593060d7 | 2940 | found->bytes_used += bytes_used; |
b742bb82 | 2941 | found->disk_used += bytes_used * factor; |
8f18cf13 | 2942 | found->full = 0; |
25179201 | 2943 | spin_unlock(&found->lock); |
593060d7 CM |
2944 | *space_info = found; |
2945 | return 0; | |
2946 | } | |
c146afad | 2947 | found = kzalloc(sizeof(*found), GFP_NOFS); |
593060d7 CM |
2948 | if (!found) |
2949 | return -ENOMEM; | |
2950 | ||
b742bb82 YZ |
2951 | for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) |
2952 | INIT_LIST_HEAD(&found->block_groups[i]); | |
80eb234a | 2953 | init_rwsem(&found->groups_sem); |
0f9dd46c | 2954 | spin_lock_init(&found->lock); |
b742bb82 YZ |
2955 | found->flags = flags & (BTRFS_BLOCK_GROUP_DATA | |
2956 | BTRFS_BLOCK_GROUP_SYSTEM | | |
2957 | BTRFS_BLOCK_GROUP_METADATA); | |
593060d7 | 2958 | found->total_bytes = total_bytes; |
89a55897 | 2959 | found->disk_total = total_bytes * factor; |
593060d7 | 2960 | found->bytes_used = bytes_used; |
b742bb82 | 2961 | found->disk_used = bytes_used * factor; |
593060d7 | 2962 | found->bytes_pinned = 0; |
e8569813 | 2963 | found->bytes_reserved = 0; |
c146afad | 2964 | found->bytes_readonly = 0; |
f0486c68 | 2965 | found->bytes_may_use = 0; |
593060d7 | 2966 | found->full = 0; |
0e4f8f88 | 2967 | found->force_alloc = CHUNK_ALLOC_NO_FORCE; |
6d74119f | 2968 | found->chunk_alloc = 0; |
fdb5effd JB |
2969 | found->flush = 0; |
2970 | init_waitqueue_head(&found->wait); | |
593060d7 | 2971 | *space_info = found; |
4184ea7f | 2972 | list_add_rcu(&found->list, &info->space_info); |
593060d7 CM |
2973 | return 0; |
2974 | } | |
2975 | ||
8790d502 CM |
2976 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
2977 | { | |
2978 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | | |
611f0e00 | 2979 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 2980 | BTRFS_BLOCK_GROUP_RAID10 | |
611f0e00 | 2981 | BTRFS_BLOCK_GROUP_DUP); |
8790d502 CM |
2982 | if (extra_flags) { |
2983 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
2984 | fs_info->avail_data_alloc_bits |= extra_flags; | |
2985 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
2986 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
2987 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
2988 | fs_info->avail_system_alloc_bits |= extra_flags; | |
2989 | } | |
2990 | } | |
593060d7 | 2991 | |
2b82032c | 2992 | u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
ec44a35c | 2993 | { |
cd02dca5 CM |
2994 | /* |
2995 | * we add in the count of missing devices because we want | |
2996 | * to make sure that any RAID levels on a degraded FS | |
2997 | * continue to be honored. | |
2998 | */ | |
2999 | u64 num_devices = root->fs_info->fs_devices->rw_devices + | |
3000 | root->fs_info->fs_devices->missing_devices; | |
a061fc8d CM |
3001 | |
3002 | if (num_devices == 1) | |
3003 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); | |
3004 | if (num_devices < 4) | |
3005 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; | |
3006 | ||
ec44a35c CM |
3007 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
3008 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
a061fc8d | 3009 | BTRFS_BLOCK_GROUP_RAID10))) { |
ec44a35c | 3010 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
a061fc8d | 3011 | } |
ec44a35c CM |
3012 | |
3013 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && | |
a061fc8d | 3014 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
ec44a35c | 3015 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
a061fc8d | 3016 | } |
ec44a35c CM |
3017 | |
3018 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && | |
3019 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | | |
3020 | (flags & BTRFS_BLOCK_GROUP_RAID10) | | |
3021 | (flags & BTRFS_BLOCK_GROUP_DUP))) | |
3022 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; | |
3023 | return flags; | |
3024 | } | |
3025 | ||
b742bb82 | 3026 | static u64 get_alloc_profile(struct btrfs_root *root, u64 flags) |
6a63209f | 3027 | { |
b742bb82 YZ |
3028 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
3029 | flags |= root->fs_info->avail_data_alloc_bits & | |
3030 | root->fs_info->data_alloc_profile; | |
3031 | else if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
3032 | flags |= root->fs_info->avail_system_alloc_bits & | |
3033 | root->fs_info->system_alloc_profile; | |
3034 | else if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
3035 | flags |= root->fs_info->avail_metadata_alloc_bits & | |
3036 | root->fs_info->metadata_alloc_profile; | |
3037 | return btrfs_reduce_alloc_profile(root, flags); | |
6a63209f JB |
3038 | } |
3039 | ||
6d07bcec | 3040 | u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data) |
9ed74f2d | 3041 | { |
b742bb82 | 3042 | u64 flags; |
9ed74f2d | 3043 | |
b742bb82 YZ |
3044 | if (data) |
3045 | flags = BTRFS_BLOCK_GROUP_DATA; | |
3046 | else if (root == root->fs_info->chunk_root) | |
3047 | flags = BTRFS_BLOCK_GROUP_SYSTEM; | |
9ed74f2d | 3048 | else |
b742bb82 | 3049 | flags = BTRFS_BLOCK_GROUP_METADATA; |
9ed74f2d | 3050 | |
b742bb82 | 3051 | return get_alloc_profile(root, flags); |
6a63209f | 3052 | } |
9ed74f2d | 3053 | |
6a63209f JB |
3054 | void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode) |
3055 | { | |
6a63209f | 3056 | BTRFS_I(inode)->space_info = __find_space_info(root->fs_info, |
f0486c68 | 3057 | BTRFS_BLOCK_GROUP_DATA); |
9ed74f2d JB |
3058 | } |
3059 | ||
6a63209f | 3060 | /* |
6a63209f JB |
3061 | * This will check the space that the inode allocates from to make sure we have |
3062 | * enough space for bytes. | |
6a63209f | 3063 | */ |
0ca1f7ce | 3064 | int btrfs_check_data_free_space(struct inode *inode, u64 bytes) |
6a63209f | 3065 | { |
6a63209f | 3066 | struct btrfs_space_info *data_sinfo; |
0ca1f7ce | 3067 | struct btrfs_root *root = BTRFS_I(inode)->root; |
ab6e2410 | 3068 | u64 used; |
0af3d00b | 3069 | int ret = 0, committed = 0, alloc_chunk = 1; |
6a63209f | 3070 | |
6a63209f JB |
3071 | /* make sure bytes are sectorsize aligned */ |
3072 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); | |
6a63209f | 3073 | |
82d5902d LZ |
3074 | if (root == root->fs_info->tree_root || |
3075 | BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) { | |
0af3d00b JB |
3076 | alloc_chunk = 0; |
3077 | committed = 1; | |
3078 | } | |
3079 | ||
6a63209f | 3080 | data_sinfo = BTRFS_I(inode)->space_info; |
33b4d47f CM |
3081 | if (!data_sinfo) |
3082 | goto alloc; | |
9ed74f2d | 3083 | |
6a63209f JB |
3084 | again: |
3085 | /* make sure we have enough space to handle the data first */ | |
3086 | spin_lock(&data_sinfo->lock); | |
8929ecfa YZ |
3087 | used = data_sinfo->bytes_used + data_sinfo->bytes_reserved + |
3088 | data_sinfo->bytes_pinned + data_sinfo->bytes_readonly + | |
3089 | data_sinfo->bytes_may_use; | |
ab6e2410 JB |
3090 | |
3091 | if (used + bytes > data_sinfo->total_bytes) { | |
4e06bdd6 | 3092 | struct btrfs_trans_handle *trans; |
9ed74f2d | 3093 | |
6a63209f JB |
3094 | /* |
3095 | * if we don't have enough free bytes in this space then we need | |
3096 | * to alloc a new chunk. | |
3097 | */ | |
0af3d00b | 3098 | if (!data_sinfo->full && alloc_chunk) { |
6a63209f | 3099 | u64 alloc_target; |
9ed74f2d | 3100 | |
0e4f8f88 | 3101 | data_sinfo->force_alloc = CHUNK_ALLOC_FORCE; |
6a63209f | 3102 | spin_unlock(&data_sinfo->lock); |
33b4d47f | 3103 | alloc: |
6a63209f | 3104 | alloc_target = btrfs_get_alloc_profile(root, 1); |
7a7eaa40 | 3105 | trans = btrfs_join_transaction(root); |
a22285a6 YZ |
3106 | if (IS_ERR(trans)) |
3107 | return PTR_ERR(trans); | |
9ed74f2d | 3108 | |
6a63209f JB |
3109 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
3110 | bytes + 2 * 1024 * 1024, | |
0e4f8f88 CM |
3111 | alloc_target, |
3112 | CHUNK_ALLOC_NO_FORCE); | |
6a63209f | 3113 | btrfs_end_transaction(trans, root); |
d52a5b5f MX |
3114 | if (ret < 0) { |
3115 | if (ret != -ENOSPC) | |
3116 | return ret; | |
3117 | else | |
3118 | goto commit_trans; | |
3119 | } | |
9ed74f2d | 3120 | |
33b4d47f CM |
3121 | if (!data_sinfo) { |
3122 | btrfs_set_inode_space_info(root, inode); | |
3123 | data_sinfo = BTRFS_I(inode)->space_info; | |
3124 | } | |
6a63209f JB |
3125 | goto again; |
3126 | } | |
f2bb8f5c JB |
3127 | |
3128 | /* | |
3129 | * If we have less pinned bytes than we want to allocate then | |
3130 | * don't bother committing the transaction, it won't help us. | |
3131 | */ | |
3132 | if (data_sinfo->bytes_pinned < bytes) | |
3133 | committed = 1; | |
6a63209f | 3134 | spin_unlock(&data_sinfo->lock); |
6a63209f | 3135 | |
4e06bdd6 | 3136 | /* commit the current transaction and try again */ |
d52a5b5f | 3137 | commit_trans: |
a4abeea4 JB |
3138 | if (!committed && |
3139 | !atomic_read(&root->fs_info->open_ioctl_trans)) { | |
4e06bdd6 | 3140 | committed = 1; |
7a7eaa40 | 3141 | trans = btrfs_join_transaction(root); |
a22285a6 YZ |
3142 | if (IS_ERR(trans)) |
3143 | return PTR_ERR(trans); | |
4e06bdd6 JB |
3144 | ret = btrfs_commit_transaction(trans, root); |
3145 | if (ret) | |
3146 | return ret; | |
3147 | goto again; | |
3148 | } | |
9ed74f2d | 3149 | |
6a63209f JB |
3150 | return -ENOSPC; |
3151 | } | |
3152 | data_sinfo->bytes_may_use += bytes; | |
6a63209f | 3153 | spin_unlock(&data_sinfo->lock); |
6a63209f | 3154 | |
9ed74f2d | 3155 | return 0; |
9ed74f2d | 3156 | } |
6a63209f | 3157 | |
6a63209f | 3158 | /* |
fb25e914 | 3159 | * Called if we need to clear a data reservation for this inode. |
6a63209f | 3160 | */ |
0ca1f7ce | 3161 | void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes) |
e3ccfa98 | 3162 | { |
0ca1f7ce | 3163 | struct btrfs_root *root = BTRFS_I(inode)->root; |
6a63209f | 3164 | struct btrfs_space_info *data_sinfo; |
e3ccfa98 | 3165 | |
6a63209f JB |
3166 | /* make sure bytes are sectorsize aligned */ |
3167 | bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1); | |
e3ccfa98 | 3168 | |
6a63209f JB |
3169 | data_sinfo = BTRFS_I(inode)->space_info; |
3170 | spin_lock(&data_sinfo->lock); | |
3171 | data_sinfo->bytes_may_use -= bytes; | |
6a63209f | 3172 | spin_unlock(&data_sinfo->lock); |
e3ccfa98 JB |
3173 | } |
3174 | ||
97e728d4 | 3175 | static void force_metadata_allocation(struct btrfs_fs_info *info) |
e3ccfa98 | 3176 | { |
97e728d4 JB |
3177 | struct list_head *head = &info->space_info; |
3178 | struct btrfs_space_info *found; | |
e3ccfa98 | 3179 | |
97e728d4 JB |
3180 | rcu_read_lock(); |
3181 | list_for_each_entry_rcu(found, head, list) { | |
3182 | if (found->flags & BTRFS_BLOCK_GROUP_METADATA) | |
0e4f8f88 | 3183 | found->force_alloc = CHUNK_ALLOC_FORCE; |
e3ccfa98 | 3184 | } |
97e728d4 | 3185 | rcu_read_unlock(); |
e3ccfa98 JB |
3186 | } |
3187 | ||
e5bc2458 | 3188 | static int should_alloc_chunk(struct btrfs_root *root, |
0e4f8f88 CM |
3189 | struct btrfs_space_info *sinfo, u64 alloc_bytes, |
3190 | int force) | |
32c00aff | 3191 | { |
fb25e914 | 3192 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
424499db | 3193 | u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly; |
0e4f8f88 | 3194 | u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved; |
e5bc2458 | 3195 | u64 thresh; |
e3ccfa98 | 3196 | |
0e4f8f88 CM |
3197 | if (force == CHUNK_ALLOC_FORCE) |
3198 | return 1; | |
3199 | ||
fb25e914 JB |
3200 | /* |
3201 | * We need to take into account the global rsv because for all intents | |
3202 | * and purposes it's used space. Don't worry about locking the | |
3203 | * global_rsv, it doesn't change except when the transaction commits. | |
3204 | */ | |
3205 | num_allocated += global_rsv->size; | |
3206 | ||
0e4f8f88 CM |
3207 | /* |
3208 | * in limited mode, we want to have some free space up to | |
3209 | * about 1% of the FS size. | |
3210 | */ | |
3211 | if (force == CHUNK_ALLOC_LIMITED) { | |
6c41761f | 3212 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
0e4f8f88 CM |
3213 | thresh = max_t(u64, 64 * 1024 * 1024, |
3214 | div_factor_fine(thresh, 1)); | |
3215 | ||
3216 | if (num_bytes - num_allocated < thresh) | |
3217 | return 1; | |
3218 | } | |
3219 | ||
3220 | /* | |
3221 | * we have two similar checks here, one based on percentage | |
3222 | * and once based on a hard number of 256MB. The idea | |
3223 | * is that if we have a good amount of free | |
3224 | * room, don't allocate a chunk. A good mount is | |
3225 | * less than 80% utilized of the chunks we have allocated, | |
3226 | * or more than 256MB free | |
3227 | */ | |
3228 | if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes) | |
424499db | 3229 | return 0; |
e3ccfa98 | 3230 | |
0e4f8f88 | 3231 | if (num_allocated + alloc_bytes < div_factor(num_bytes, 8)) |
424499db | 3232 | return 0; |
32c00aff | 3233 | |
6c41761f | 3234 | thresh = btrfs_super_total_bytes(root->fs_info->super_copy); |
0e4f8f88 CM |
3235 | |
3236 | /* 256MB or 5% of the FS */ | |
e5bc2458 CM |
3237 | thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5)); |
3238 | ||
3239 | if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3)) | |
14ed0ca6 | 3240 | return 0; |
424499db | 3241 | return 1; |
32c00aff JB |
3242 | } |
3243 | ||
6324fbf3 CM |
3244 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
3245 | struct btrfs_root *extent_root, u64 alloc_bytes, | |
0ef3e66b | 3246 | u64 flags, int force) |
9ed74f2d | 3247 | { |
6324fbf3 | 3248 | struct btrfs_space_info *space_info; |
97e728d4 | 3249 | struct btrfs_fs_info *fs_info = extent_root->fs_info; |
6d74119f | 3250 | int wait_for_alloc = 0; |
9ed74f2d | 3251 | int ret = 0; |
9ed74f2d | 3252 | |
2b82032c | 3253 | flags = btrfs_reduce_alloc_profile(extent_root, flags); |
ec44a35c | 3254 | |
6324fbf3 | 3255 | space_info = __find_space_info(extent_root->fs_info, flags); |
593060d7 CM |
3256 | if (!space_info) { |
3257 | ret = update_space_info(extent_root->fs_info, flags, | |
3258 | 0, 0, &space_info); | |
3259 | BUG_ON(ret); | |
9ed74f2d | 3260 | } |
6324fbf3 | 3261 | BUG_ON(!space_info); |
9ed74f2d | 3262 | |
6d74119f | 3263 | again: |
25179201 | 3264 | spin_lock(&space_info->lock); |
9ed74f2d | 3265 | if (space_info->force_alloc) |
0e4f8f88 | 3266 | force = space_info->force_alloc; |
25179201 JB |
3267 | if (space_info->full) { |
3268 | spin_unlock(&space_info->lock); | |
6d74119f | 3269 | return 0; |
9ed74f2d JB |
3270 | } |
3271 | ||
0e4f8f88 | 3272 | if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) { |
25179201 | 3273 | spin_unlock(&space_info->lock); |
6d74119f JB |
3274 | return 0; |
3275 | } else if (space_info->chunk_alloc) { | |
3276 | wait_for_alloc = 1; | |
3277 | } else { | |
3278 | space_info->chunk_alloc = 1; | |
9ed74f2d | 3279 | } |
0e4f8f88 | 3280 | |
25179201 | 3281 | spin_unlock(&space_info->lock); |
9ed74f2d | 3282 | |
6d74119f JB |
3283 | mutex_lock(&fs_info->chunk_mutex); |
3284 | ||
3285 | /* | |
3286 | * The chunk_mutex is held throughout the entirety of a chunk | |
3287 | * allocation, so once we've acquired the chunk_mutex we know that the | |
3288 | * other guy is done and we need to recheck and see if we should | |
3289 | * allocate. | |
3290 | */ | |
3291 | if (wait_for_alloc) { | |
3292 | mutex_unlock(&fs_info->chunk_mutex); | |
3293 | wait_for_alloc = 0; | |
3294 | goto again; | |
3295 | } | |
3296 | ||
67377734 JB |
3297 | /* |
3298 | * If we have mixed data/metadata chunks we want to make sure we keep | |
3299 | * allocating mixed chunks instead of individual chunks. | |
3300 | */ | |
3301 | if (btrfs_mixed_space_info(space_info)) | |
3302 | flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA); | |
3303 | ||
97e728d4 JB |
3304 | /* |
3305 | * if we're doing a data chunk, go ahead and make sure that | |
3306 | * we keep a reasonable number of metadata chunks allocated in the | |
3307 | * FS as well. | |
3308 | */ | |
9ed74f2d | 3309 | if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) { |
97e728d4 JB |
3310 | fs_info->data_chunk_allocations++; |
3311 | if (!(fs_info->data_chunk_allocations % | |
3312 | fs_info->metadata_ratio)) | |
3313 | force_metadata_allocation(fs_info); | |
9ed74f2d JB |
3314 | } |
3315 | ||
2b82032c | 3316 | ret = btrfs_alloc_chunk(trans, extent_root, flags); |
92b8e897 MF |
3317 | if (ret < 0 && ret != -ENOSPC) |
3318 | goto out; | |
3319 | ||
9ed74f2d | 3320 | spin_lock(&space_info->lock); |
9ed74f2d | 3321 | if (ret) |
6324fbf3 | 3322 | space_info->full = 1; |
424499db YZ |
3323 | else |
3324 | ret = 1; | |
6d74119f | 3325 | |
0e4f8f88 | 3326 | space_info->force_alloc = CHUNK_ALLOC_NO_FORCE; |
6d74119f | 3327 | space_info->chunk_alloc = 0; |
9ed74f2d | 3328 | spin_unlock(&space_info->lock); |
92b8e897 | 3329 | out: |
c146afad | 3330 | mutex_unlock(&extent_root->fs_info->chunk_mutex); |
0f9dd46c | 3331 | return ret; |
6324fbf3 | 3332 | } |
9ed74f2d | 3333 | |
9ed74f2d | 3334 | /* |
5da9d01b | 3335 | * shrink metadata reservation for delalloc |
9ed74f2d | 3336 | */ |
663350ac | 3337 | static int shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, |
f104d044 | 3338 | bool wait_ordered) |
5da9d01b | 3339 | { |
0ca1f7ce | 3340 | struct btrfs_block_rsv *block_rsv; |
0019f10d | 3341 | struct btrfs_space_info *space_info; |
663350ac | 3342 | struct btrfs_trans_handle *trans; |
5da9d01b YZ |
3343 | u64 reserved; |
3344 | u64 max_reclaim; | |
3345 | u64 reclaimed = 0; | |
b1953bce | 3346 | long time_left; |
877da174 | 3347 | unsigned long nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT; |
b1953bce | 3348 | int loops = 0; |
36e39c40 | 3349 | unsigned long progress; |
5da9d01b | 3350 | |
663350ac | 3351 | trans = (struct btrfs_trans_handle *)current->journal_info; |
0ca1f7ce | 3352 | block_rsv = &root->fs_info->delalloc_block_rsv; |
0019f10d | 3353 | space_info = block_rsv->space_info; |
bf9022e0 CM |
3354 | |
3355 | smp_mb(); | |
fb25e914 | 3356 | reserved = space_info->bytes_may_use; |
36e39c40 | 3357 | progress = space_info->reservation_progress; |
5da9d01b YZ |
3358 | |
3359 | if (reserved == 0) | |
3360 | return 0; | |
c4f675cd | 3361 | |
fdb5effd JB |
3362 | smp_mb(); |
3363 | if (root->fs_info->delalloc_bytes == 0) { | |
3364 | if (trans) | |
3365 | return 0; | |
3366 | btrfs_wait_ordered_extents(root, 0, 0); | |
3367 | return 0; | |
3368 | } | |
3369 | ||
5da9d01b | 3370 | max_reclaim = min(reserved, to_reclaim); |
877da174 JB |
3371 | nr_pages = max_t(unsigned long, nr_pages, |
3372 | max_reclaim >> PAGE_CACHE_SHIFT); | |
b1953bce | 3373 | while (loops < 1024) { |
bf9022e0 CM |
3374 | /* have the flusher threads jump in and do some IO */ |
3375 | smp_mb(); | |
3376 | nr_pages = min_t(unsigned long, nr_pages, | |
3377 | root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT); | |
3378 | writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages); | |
5da9d01b | 3379 | |
0019f10d | 3380 | spin_lock(&space_info->lock); |
fb25e914 JB |
3381 | if (reserved > space_info->bytes_may_use) |
3382 | reclaimed += reserved - space_info->bytes_may_use; | |
3383 | reserved = space_info->bytes_may_use; | |
0019f10d | 3384 | spin_unlock(&space_info->lock); |
5da9d01b | 3385 | |
36e39c40 CM |
3386 | loops++; |
3387 | ||
5da9d01b YZ |
3388 | if (reserved == 0 || reclaimed >= max_reclaim) |
3389 | break; | |
3390 | ||
3391 | if (trans && trans->transaction->blocked) | |
3392 | return -EAGAIN; | |
bf9022e0 | 3393 | |
f104d044 JB |
3394 | if (wait_ordered && !trans) { |
3395 | btrfs_wait_ordered_extents(root, 0, 0); | |
3396 | } else { | |
3397 | time_left = schedule_timeout_interruptible(1); | |
b1953bce | 3398 | |
f104d044 JB |
3399 | /* We were interrupted, exit */ |
3400 | if (time_left) | |
3401 | break; | |
3402 | } | |
b1953bce | 3403 | |
36e39c40 CM |
3404 | /* we've kicked the IO a few times, if anything has been freed, |
3405 | * exit. There is no sense in looping here for a long time | |
3406 | * when we really need to commit the transaction, or there are | |
3407 | * just too many writers without enough free space | |
3408 | */ | |
3409 | ||
3410 | if (loops > 3) { | |
3411 | smp_mb(); | |
3412 | if (progress != space_info->reservation_progress) | |
3413 | break; | |
3414 | } | |
bf9022e0 | 3415 | |
5da9d01b | 3416 | } |
f104d044 | 3417 | |
5da9d01b YZ |
3418 | return reclaimed >= to_reclaim; |
3419 | } | |
3420 | ||
663350ac JB |
3421 | /** |
3422 | * maybe_commit_transaction - possibly commit the transaction if its ok to | |
3423 | * @root - the root we're allocating for | |
3424 | * @bytes - the number of bytes we want to reserve | |
3425 | * @force - force the commit | |
3426 | * | |
3427 | * This will check to make sure that committing the transaction will actually | |
3428 | * get us somewhere and then commit the transaction if it does. Otherwise it | |
3429 | * will return -ENOSPC. | |
3430 | */ | |
3431 | static int may_commit_transaction(struct btrfs_root *root, | |
3432 | struct btrfs_space_info *space_info, | |
3433 | u64 bytes, int force) | |
3434 | { | |
3435 | struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv; | |
3436 | struct btrfs_trans_handle *trans; | |
3437 | ||
3438 | trans = (struct btrfs_trans_handle *)current->journal_info; | |
3439 | if (trans) | |
3440 | return -EAGAIN; | |
3441 | ||
3442 | if (force) | |
3443 | goto commit; | |
3444 | ||
3445 | /* See if there is enough pinned space to make this reservation */ | |
3446 | spin_lock(&space_info->lock); | |
3447 | if (space_info->bytes_pinned >= bytes) { | |
3448 | spin_unlock(&space_info->lock); | |
3449 | goto commit; | |
3450 | } | |
3451 | spin_unlock(&space_info->lock); | |
3452 | ||
3453 | /* | |
3454 | * See if there is some space in the delayed insertion reservation for | |
3455 | * this reservation. | |
3456 | */ | |
3457 | if (space_info != delayed_rsv->space_info) | |
3458 | return -ENOSPC; | |
3459 | ||
3460 | spin_lock(&delayed_rsv->lock); | |
3461 | if (delayed_rsv->size < bytes) { | |
3462 | spin_unlock(&delayed_rsv->lock); | |
3463 | return -ENOSPC; | |
3464 | } | |
3465 | spin_unlock(&delayed_rsv->lock); | |
3466 | ||
3467 | commit: | |
3468 | trans = btrfs_join_transaction(root); | |
3469 | if (IS_ERR(trans)) | |
3470 | return -ENOSPC; | |
3471 | ||
3472 | return btrfs_commit_transaction(trans, root); | |
3473 | } | |
3474 | ||
4a92b1b8 JB |
3475 | /** |
3476 | * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space | |
3477 | * @root - the root we're allocating for | |
3478 | * @block_rsv - the block_rsv we're allocating for | |
3479 | * @orig_bytes - the number of bytes we want | |
3480 | * @flush - wether or not we can flush to make our reservation | |
8bb8ab2e | 3481 | * |
4a92b1b8 JB |
3482 | * This will reserve orgi_bytes number of bytes from the space info associated |
3483 | * with the block_rsv. If there is not enough space it will make an attempt to | |
3484 | * flush out space to make room. It will do this by flushing delalloc if | |
3485 | * possible or committing the transaction. If flush is 0 then no attempts to | |
3486 | * regain reservations will be made and this will fail if there is not enough | |
3487 | * space already. | |
8bb8ab2e | 3488 | */ |
4a92b1b8 | 3489 | static int reserve_metadata_bytes(struct btrfs_root *root, |
8bb8ab2e | 3490 | struct btrfs_block_rsv *block_rsv, |
36ba022a | 3491 | u64 orig_bytes, int flush) |
9ed74f2d | 3492 | { |
f0486c68 | 3493 | struct btrfs_space_info *space_info = block_rsv->space_info; |
2bf64758 | 3494 | u64 used; |
8bb8ab2e JB |
3495 | u64 num_bytes = orig_bytes; |
3496 | int retries = 0; | |
3497 | int ret = 0; | |
38227933 | 3498 | bool committed = false; |
fdb5effd | 3499 | bool flushing = false; |
f104d044 | 3500 | bool wait_ordered = false; |
4a92b1b8 | 3501 | |
8bb8ab2e | 3502 | again: |
fdb5effd | 3503 | ret = 0; |
8bb8ab2e | 3504 | spin_lock(&space_info->lock); |
fdb5effd JB |
3505 | /* |
3506 | * We only want to wait if somebody other than us is flushing and we are | |
3507 | * actually alloed to flush. | |
3508 | */ | |
3509 | while (flush && !flushing && space_info->flush) { | |
3510 | spin_unlock(&space_info->lock); | |
3511 | /* | |
3512 | * If we have a trans handle we can't wait because the flusher | |
3513 | * may have to commit the transaction, which would mean we would | |
3514 | * deadlock since we are waiting for the flusher to finish, but | |
3515 | * hold the current transaction open. | |
3516 | */ | |
663350ac | 3517 | if (current->journal_info) |
fdb5effd JB |
3518 | return -EAGAIN; |
3519 | ret = wait_event_interruptible(space_info->wait, | |
3520 | !space_info->flush); | |
3521 | /* Must have been interrupted, return */ | |
3522 | if (ret) | |
3523 | return -EINTR; | |
3524 | ||
3525 | spin_lock(&space_info->lock); | |
3526 | } | |
3527 | ||
3528 | ret = -ENOSPC; | |
2bf64758 JB |
3529 | used = space_info->bytes_used + space_info->bytes_reserved + |
3530 | space_info->bytes_pinned + space_info->bytes_readonly + | |
3531 | space_info->bytes_may_use; | |
9ed74f2d | 3532 | |
8bb8ab2e JB |
3533 | /* |
3534 | * The idea here is that we've not already over-reserved the block group | |
3535 | * then we can go ahead and save our reservation first and then start | |
3536 | * flushing if we need to. Otherwise if we've already overcommitted | |
3537 | * lets start flushing stuff first and then come back and try to make | |
3538 | * our reservation. | |
3539 | */ | |
2bf64758 JB |
3540 | if (used <= space_info->total_bytes) { |
3541 | if (used + orig_bytes <= space_info->total_bytes) { | |
fb25e914 | 3542 | space_info->bytes_may_use += orig_bytes; |
8bb8ab2e JB |
3543 | ret = 0; |
3544 | } else { | |
3545 | /* | |
3546 | * Ok set num_bytes to orig_bytes since we aren't | |
3547 | * overocmmitted, this way we only try and reclaim what | |
3548 | * we need. | |
3549 | */ | |
3550 | num_bytes = orig_bytes; | |
3551 | } | |
3552 | } else { | |
3553 | /* | |
3554 | * Ok we're over committed, set num_bytes to the overcommitted | |
3555 | * amount plus the amount of bytes that we need for this | |
3556 | * reservation. | |
3557 | */ | |
f104d044 | 3558 | wait_ordered = true; |
2bf64758 | 3559 | num_bytes = used - space_info->total_bytes + |
8bb8ab2e JB |
3560 | (orig_bytes * (retries + 1)); |
3561 | } | |
9ed74f2d | 3562 | |
36ba022a | 3563 | if (ret) { |
2bf64758 JB |
3564 | u64 profile = btrfs_get_alloc_profile(root, 0); |
3565 | u64 avail; | |
3566 | ||
7e355b83 JB |
3567 | /* |
3568 | * If we have a lot of space that's pinned, don't bother doing | |
3569 | * the overcommit dance yet and just commit the transaction. | |
3570 | */ | |
3571 | avail = (space_info->total_bytes - space_info->bytes_used) * 8; | |
3572 | do_div(avail, 10); | |
663350ac | 3573 | if (space_info->bytes_pinned >= avail && flush && !committed) { |
7e355b83 JB |
3574 | space_info->flush = 1; |
3575 | flushing = true; | |
3576 | spin_unlock(&space_info->lock); | |
663350ac JB |
3577 | ret = may_commit_transaction(root, space_info, |
3578 | orig_bytes, 1); | |
3579 | if (ret) | |
3580 | goto out; | |
3581 | committed = true; | |
3582 | goto again; | |
7e355b83 JB |
3583 | } |
3584 | ||
2bf64758 JB |
3585 | spin_lock(&root->fs_info->free_chunk_lock); |
3586 | avail = root->fs_info->free_chunk_space; | |
3587 | ||
3588 | /* | |
3589 | * If we have dup, raid1 or raid10 then only half of the free | |
3590 | * space is actually useable. | |
3591 | */ | |
3592 | if (profile & (BTRFS_BLOCK_GROUP_DUP | | |
3593 | BTRFS_BLOCK_GROUP_RAID1 | | |
3594 | BTRFS_BLOCK_GROUP_RAID10)) | |
3595 | avail >>= 1; | |
3596 | ||
3597 | /* | |
3598 | * If we aren't flushing don't let us overcommit too much, say | |
3599 | * 1/8th of the space. If we can flush, let it overcommit up to | |
3600 | * 1/2 of the space. | |
3601 | */ | |
3602 | if (flush) | |
3603 | avail >>= 3; | |
3604 | else | |
3605 | avail >>= 1; | |
3606 | spin_unlock(&root->fs_info->free_chunk_lock); | |
3607 | ||
9a82ca65 | 3608 | if (used + num_bytes < space_info->total_bytes + avail) { |
2bf64758 JB |
3609 | space_info->bytes_may_use += orig_bytes; |
3610 | ret = 0; | |
f104d044 JB |
3611 | } else { |
3612 | wait_ordered = true; | |
2bf64758 JB |
3613 | } |
3614 | } | |
3615 | ||
8bb8ab2e JB |
3616 | /* |
3617 | * Couldn't make our reservation, save our place so while we're trying | |
3618 | * to reclaim space we can actually use it instead of somebody else | |
3619 | * stealing it from us. | |
3620 | */ | |
fdb5effd JB |
3621 | if (ret && flush) { |
3622 | flushing = true; | |
3623 | space_info->flush = 1; | |
8bb8ab2e | 3624 | } |
9ed74f2d | 3625 | |
f0486c68 | 3626 | spin_unlock(&space_info->lock); |
9ed74f2d | 3627 | |
fdb5effd | 3628 | if (!ret || !flush) |
8bb8ab2e | 3629 | goto out; |
f0486c68 | 3630 | |
8bb8ab2e JB |
3631 | /* |
3632 | * We do synchronous shrinking since we don't actually unreserve | |
3633 | * metadata until after the IO is completed. | |
3634 | */ | |
663350ac | 3635 | ret = shrink_delalloc(root, num_bytes, wait_ordered); |
fdb5effd | 3636 | if (ret < 0) |
8bb8ab2e | 3637 | goto out; |
f0486c68 | 3638 | |
75c195a2 CM |
3639 | ret = 0; |
3640 | ||
8bb8ab2e JB |
3641 | /* |
3642 | * So if we were overcommitted it's possible that somebody else flushed | |
3643 | * out enough space and we simply didn't have enough space to reclaim, | |
3644 | * so go back around and try again. | |
3645 | */ | |
3646 | if (retries < 2) { | |
f104d044 | 3647 | wait_ordered = true; |
8bb8ab2e JB |
3648 | retries++; |
3649 | goto again; | |
3650 | } | |
f0486c68 | 3651 | |
8bb8ab2e | 3652 | ret = -ENOSPC; |
75c195a2 CM |
3653 | if (committed) |
3654 | goto out; | |
3655 | ||
663350ac | 3656 | ret = may_commit_transaction(root, space_info, orig_bytes, 0); |
38227933 | 3657 | if (!ret) { |
38227933 | 3658 | committed = true; |
8bb8ab2e | 3659 | goto again; |
38227933 | 3660 | } |
8bb8ab2e JB |
3661 | |
3662 | out: | |
fdb5effd | 3663 | if (flushing) { |
8bb8ab2e | 3664 | spin_lock(&space_info->lock); |
fdb5effd JB |
3665 | space_info->flush = 0; |
3666 | wake_up_all(&space_info->wait); | |
8bb8ab2e | 3667 | spin_unlock(&space_info->lock); |
f0486c68 | 3668 | } |
f0486c68 YZ |
3669 | return ret; |
3670 | } | |
3671 | ||
3672 | static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans, | |
3673 | struct btrfs_root *root) | |
3674 | { | |
4c13d758 JB |
3675 | struct btrfs_block_rsv *block_rsv = NULL; |
3676 | ||
3677 | if (root->ref_cows || root == root->fs_info->csum_root) | |
f0486c68 | 3678 | block_rsv = trans->block_rsv; |
4c13d758 JB |
3679 | |
3680 | if (!block_rsv) | |
f0486c68 YZ |
3681 | block_rsv = root->block_rsv; |
3682 | ||
3683 | if (!block_rsv) | |
3684 | block_rsv = &root->fs_info->empty_block_rsv; | |
3685 | ||
3686 | return block_rsv; | |
3687 | } | |
3688 | ||
3689 | static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv, | |
3690 | u64 num_bytes) | |
3691 | { | |
3692 | int ret = -ENOSPC; | |
3693 | spin_lock(&block_rsv->lock); | |
3694 | if (block_rsv->reserved >= num_bytes) { | |
3695 | block_rsv->reserved -= num_bytes; | |
3696 | if (block_rsv->reserved < block_rsv->size) | |
3697 | block_rsv->full = 0; | |
3698 | ret = 0; | |
3699 | } | |
3700 | spin_unlock(&block_rsv->lock); | |
3701 | return ret; | |
3702 | } | |
3703 | ||
3704 | static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv, | |
3705 | u64 num_bytes, int update_size) | |
3706 | { | |
3707 | spin_lock(&block_rsv->lock); | |
3708 | block_rsv->reserved += num_bytes; | |
3709 | if (update_size) | |
3710 | block_rsv->size += num_bytes; | |
3711 | else if (block_rsv->reserved >= block_rsv->size) | |
3712 | block_rsv->full = 1; | |
3713 | spin_unlock(&block_rsv->lock); | |
3714 | } | |
3715 | ||
62a45b60 DS |
3716 | static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv, |
3717 | struct btrfs_block_rsv *dest, u64 num_bytes) | |
f0486c68 YZ |
3718 | { |
3719 | struct btrfs_space_info *space_info = block_rsv->space_info; | |
3720 | ||
3721 | spin_lock(&block_rsv->lock); | |
3722 | if (num_bytes == (u64)-1) | |
3723 | num_bytes = block_rsv->size; | |
3724 | block_rsv->size -= num_bytes; | |
3725 | if (block_rsv->reserved >= block_rsv->size) { | |
3726 | num_bytes = block_rsv->reserved - block_rsv->size; | |
3727 | block_rsv->reserved = block_rsv->size; | |
3728 | block_rsv->full = 1; | |
3729 | } else { | |
3730 | num_bytes = 0; | |
3731 | } | |
3732 | spin_unlock(&block_rsv->lock); | |
3733 | ||
3734 | if (num_bytes > 0) { | |
3735 | if (dest) { | |
e9e22899 JB |
3736 | spin_lock(&dest->lock); |
3737 | if (!dest->full) { | |
3738 | u64 bytes_to_add; | |
3739 | ||
3740 | bytes_to_add = dest->size - dest->reserved; | |
3741 | bytes_to_add = min(num_bytes, bytes_to_add); | |
3742 | dest->reserved += bytes_to_add; | |
3743 | if (dest->reserved >= dest->size) | |
3744 | dest->full = 1; | |
3745 | num_bytes -= bytes_to_add; | |
3746 | } | |
3747 | spin_unlock(&dest->lock); | |
3748 | } | |
3749 | if (num_bytes) { | |
f0486c68 | 3750 | spin_lock(&space_info->lock); |
fb25e914 | 3751 | space_info->bytes_may_use -= num_bytes; |
36e39c40 | 3752 | space_info->reservation_progress++; |
f0486c68 | 3753 | spin_unlock(&space_info->lock); |
4e06bdd6 | 3754 | } |
9ed74f2d | 3755 | } |
f0486c68 | 3756 | } |
4e06bdd6 | 3757 | |
f0486c68 YZ |
3758 | static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src, |
3759 | struct btrfs_block_rsv *dst, u64 num_bytes) | |
3760 | { | |
3761 | int ret; | |
9ed74f2d | 3762 | |
f0486c68 YZ |
3763 | ret = block_rsv_use_bytes(src, num_bytes); |
3764 | if (ret) | |
3765 | return ret; | |
9ed74f2d | 3766 | |
f0486c68 | 3767 | block_rsv_add_bytes(dst, num_bytes, 1); |
9ed74f2d JB |
3768 | return 0; |
3769 | } | |
3770 | ||
f0486c68 | 3771 | void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv) |
9ed74f2d | 3772 | { |
f0486c68 YZ |
3773 | memset(rsv, 0, sizeof(*rsv)); |
3774 | spin_lock_init(&rsv->lock); | |
f0486c68 YZ |
3775 | } |
3776 | ||
3777 | struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root) | |
3778 | { | |
3779 | struct btrfs_block_rsv *block_rsv; | |
3780 | struct btrfs_fs_info *fs_info = root->fs_info; | |
9ed74f2d | 3781 | |
f0486c68 YZ |
3782 | block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS); |
3783 | if (!block_rsv) | |
3784 | return NULL; | |
9ed74f2d | 3785 | |
f0486c68 | 3786 | btrfs_init_block_rsv(block_rsv); |
f0486c68 YZ |
3787 | block_rsv->space_info = __find_space_info(fs_info, |
3788 | BTRFS_BLOCK_GROUP_METADATA); | |
f0486c68 YZ |
3789 | return block_rsv; |
3790 | } | |
9ed74f2d | 3791 | |
f0486c68 YZ |
3792 | void btrfs_free_block_rsv(struct btrfs_root *root, |
3793 | struct btrfs_block_rsv *rsv) | |
3794 | { | |
dabdb640 JB |
3795 | btrfs_block_rsv_release(root, rsv, (u64)-1); |
3796 | kfree(rsv); | |
9ed74f2d JB |
3797 | } |
3798 | ||
61b520a9 MX |
3799 | static inline int __block_rsv_add(struct btrfs_root *root, |
3800 | struct btrfs_block_rsv *block_rsv, | |
3801 | u64 num_bytes, int flush) | |
f0486c68 YZ |
3802 | { |
3803 | int ret; | |
9ed74f2d | 3804 | |
f0486c68 YZ |
3805 | if (num_bytes == 0) |
3806 | return 0; | |
8bb8ab2e | 3807 | |
61b520a9 | 3808 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush); |
f0486c68 YZ |
3809 | if (!ret) { |
3810 | block_rsv_add_bytes(block_rsv, num_bytes, 1); | |
3811 | return 0; | |
3812 | } | |
9ed74f2d | 3813 | |
f0486c68 YZ |
3814 | return ret; |
3815 | } | |
9ed74f2d | 3816 | |
61b520a9 MX |
3817 | int btrfs_block_rsv_add(struct btrfs_root *root, |
3818 | struct btrfs_block_rsv *block_rsv, | |
3819 | u64 num_bytes) | |
3820 | { | |
3821 | return __block_rsv_add(root, block_rsv, num_bytes, 1); | |
3822 | } | |
3823 | ||
c06a0e12 JB |
3824 | int btrfs_block_rsv_add_noflush(struct btrfs_root *root, |
3825 | struct btrfs_block_rsv *block_rsv, | |
3826 | u64 num_bytes) | |
3827 | { | |
61b520a9 | 3828 | return __block_rsv_add(root, block_rsv, num_bytes, 0); |
c06a0e12 JB |
3829 | } |
3830 | ||
4a92b1b8 | 3831 | int btrfs_block_rsv_check(struct btrfs_root *root, |
36ba022a | 3832 | struct btrfs_block_rsv *block_rsv, int min_factor) |
f0486c68 YZ |
3833 | { |
3834 | u64 num_bytes = 0; | |
f0486c68 | 3835 | int ret = -ENOSPC; |
9ed74f2d | 3836 | |
f0486c68 YZ |
3837 | if (!block_rsv) |
3838 | return 0; | |
9ed74f2d | 3839 | |
f0486c68 | 3840 | spin_lock(&block_rsv->lock); |
36ba022a JB |
3841 | num_bytes = div_factor(block_rsv->size, min_factor); |
3842 | if (block_rsv->reserved >= num_bytes) | |
3843 | ret = 0; | |
3844 | spin_unlock(&block_rsv->lock); | |
9ed74f2d | 3845 | |
36ba022a JB |
3846 | return ret; |
3847 | } | |
3848 | ||
3849 | int btrfs_block_rsv_refill(struct btrfs_root *root, | |
3850 | struct btrfs_block_rsv *block_rsv, | |
3851 | u64 min_reserved) | |
3852 | { | |
3853 | u64 num_bytes = 0; | |
3854 | int ret = -ENOSPC; | |
3855 | ||
3856 | if (!block_rsv) | |
3857 | return 0; | |
3858 | ||
3859 | spin_lock(&block_rsv->lock); | |
3860 | num_bytes = min_reserved; | |
13553e52 | 3861 | if (block_rsv->reserved >= num_bytes) |
f0486c68 | 3862 | ret = 0; |
13553e52 | 3863 | else |
f0486c68 | 3864 | num_bytes -= block_rsv->reserved; |
f0486c68 | 3865 | spin_unlock(&block_rsv->lock); |
13553e52 | 3866 | |
f0486c68 YZ |
3867 | if (!ret) |
3868 | return 0; | |
3869 | ||
36ba022a | 3870 | ret = reserve_metadata_bytes(root, block_rsv, num_bytes, 1); |
dabdb640 JB |
3871 | if (!ret) { |
3872 | block_rsv_add_bytes(block_rsv, num_bytes, 0); | |
3873 | return 0; | |
f0486c68 | 3874 | } |
9ed74f2d | 3875 | |
13553e52 | 3876 | return ret; |
f0486c68 YZ |
3877 | } |
3878 | ||
3879 | int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv, | |
3880 | struct btrfs_block_rsv *dst_rsv, | |
3881 | u64 num_bytes) | |
3882 | { | |
3883 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); | |
3884 | } | |
3885 | ||
3886 | void btrfs_block_rsv_release(struct btrfs_root *root, | |
3887 | struct btrfs_block_rsv *block_rsv, | |
3888 | u64 num_bytes) | |
3889 | { | |
3890 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; | |
3891 | if (global_rsv->full || global_rsv == block_rsv || | |
3892 | block_rsv->space_info != global_rsv->space_info) | |
3893 | global_rsv = NULL; | |
3894 | block_rsv_release_bytes(block_rsv, global_rsv, num_bytes); | |
6a63209f JB |
3895 | } |
3896 | ||
3897 | /* | |
8929ecfa YZ |
3898 | * helper to calculate size of global block reservation. |
3899 | * the desired value is sum of space used by extent tree, | |
3900 | * checksum tree and root tree | |
6a63209f | 3901 | */ |
8929ecfa | 3902 | static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info) |
6a63209f | 3903 | { |
8929ecfa YZ |
3904 | struct btrfs_space_info *sinfo; |
3905 | u64 num_bytes; | |
3906 | u64 meta_used; | |
3907 | u64 data_used; | |
6c41761f | 3908 | int csum_size = btrfs_super_csum_size(fs_info->super_copy); |
6a63209f | 3909 | |
8929ecfa YZ |
3910 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA); |
3911 | spin_lock(&sinfo->lock); | |
3912 | data_used = sinfo->bytes_used; | |
3913 | spin_unlock(&sinfo->lock); | |
33b4d47f | 3914 | |
8929ecfa YZ |
3915 | sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
3916 | spin_lock(&sinfo->lock); | |
6d48755d JB |
3917 | if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA) |
3918 | data_used = 0; | |
8929ecfa YZ |
3919 | meta_used = sinfo->bytes_used; |
3920 | spin_unlock(&sinfo->lock); | |
ab6e2410 | 3921 | |
8929ecfa YZ |
3922 | num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) * |
3923 | csum_size * 2; | |
3924 | num_bytes += div64_u64(data_used + meta_used, 50); | |
4e06bdd6 | 3925 | |
8929ecfa YZ |
3926 | if (num_bytes * 3 > meta_used) |
3927 | num_bytes = div64_u64(meta_used, 3); | |
ab6e2410 | 3928 | |
8929ecfa YZ |
3929 | return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10); |
3930 | } | |
6a63209f | 3931 | |
8929ecfa YZ |
3932 | static void update_global_block_rsv(struct btrfs_fs_info *fs_info) |
3933 | { | |
3934 | struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv; | |
3935 | struct btrfs_space_info *sinfo = block_rsv->space_info; | |
3936 | u64 num_bytes; | |
6a63209f | 3937 | |
8929ecfa | 3938 | num_bytes = calc_global_metadata_size(fs_info); |
33b4d47f | 3939 | |
8929ecfa YZ |
3940 | spin_lock(&block_rsv->lock); |
3941 | spin_lock(&sinfo->lock); | |
4e06bdd6 | 3942 | |
8929ecfa | 3943 | block_rsv->size = num_bytes; |
4e06bdd6 | 3944 | |
8929ecfa | 3945 | num_bytes = sinfo->bytes_used + sinfo->bytes_pinned + |
6d48755d JB |
3946 | sinfo->bytes_reserved + sinfo->bytes_readonly + |
3947 | sinfo->bytes_may_use; | |
8929ecfa YZ |
3948 | |
3949 | if (sinfo->total_bytes > num_bytes) { | |
3950 | num_bytes = sinfo->total_bytes - num_bytes; | |
3951 | block_rsv->reserved += num_bytes; | |
fb25e914 | 3952 | sinfo->bytes_may_use += num_bytes; |
6a63209f | 3953 | } |
6a63209f | 3954 | |
8929ecfa YZ |
3955 | if (block_rsv->reserved >= block_rsv->size) { |
3956 | num_bytes = block_rsv->reserved - block_rsv->size; | |
fb25e914 | 3957 | sinfo->bytes_may_use -= num_bytes; |
36e39c40 | 3958 | sinfo->reservation_progress++; |
8929ecfa YZ |
3959 | block_rsv->reserved = block_rsv->size; |
3960 | block_rsv->full = 1; | |
3961 | } | |
182608c8 | 3962 | |
8929ecfa YZ |
3963 | spin_unlock(&sinfo->lock); |
3964 | spin_unlock(&block_rsv->lock); | |
6a63209f JB |
3965 | } |
3966 | ||
f0486c68 | 3967 | static void init_global_block_rsv(struct btrfs_fs_info *fs_info) |
6a63209f | 3968 | { |
f0486c68 | 3969 | struct btrfs_space_info *space_info; |
6a63209f | 3970 | |
f0486c68 YZ |
3971 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM); |
3972 | fs_info->chunk_block_rsv.space_info = space_info; | |
6a63209f | 3973 | |
f0486c68 | 3974 | space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA); |
8929ecfa | 3975 | fs_info->global_block_rsv.space_info = space_info; |
8929ecfa | 3976 | fs_info->delalloc_block_rsv.space_info = space_info; |
f0486c68 YZ |
3977 | fs_info->trans_block_rsv.space_info = space_info; |
3978 | fs_info->empty_block_rsv.space_info = space_info; | |
6d668dda | 3979 | fs_info->delayed_block_rsv.space_info = space_info; |
f0486c68 | 3980 | |
8929ecfa YZ |
3981 | fs_info->extent_root->block_rsv = &fs_info->global_block_rsv; |
3982 | fs_info->csum_root->block_rsv = &fs_info->global_block_rsv; | |
3983 | fs_info->dev_root->block_rsv = &fs_info->global_block_rsv; | |
3984 | fs_info->tree_root->block_rsv = &fs_info->global_block_rsv; | |
f0486c68 | 3985 | fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv; |
8929ecfa | 3986 | |
8929ecfa | 3987 | update_global_block_rsv(fs_info); |
6a63209f JB |
3988 | } |
3989 | ||
8929ecfa | 3990 | static void release_global_block_rsv(struct btrfs_fs_info *fs_info) |
6a63209f | 3991 | { |
8929ecfa YZ |
3992 | block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1); |
3993 | WARN_ON(fs_info->delalloc_block_rsv.size > 0); | |
3994 | WARN_ON(fs_info->delalloc_block_rsv.reserved > 0); | |
3995 | WARN_ON(fs_info->trans_block_rsv.size > 0); | |
3996 | WARN_ON(fs_info->trans_block_rsv.reserved > 0); | |
3997 | WARN_ON(fs_info->chunk_block_rsv.size > 0); | |
3998 | WARN_ON(fs_info->chunk_block_rsv.reserved > 0); | |
6d668dda JB |
3999 | WARN_ON(fs_info->delayed_block_rsv.size > 0); |
4000 | WARN_ON(fs_info->delayed_block_rsv.reserved > 0); | |
f0486c68 | 4001 | } |
6a63209f | 4002 | |
a22285a6 YZ |
4003 | void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans, |
4004 | struct btrfs_root *root) | |
6a63209f | 4005 | { |
a22285a6 YZ |
4006 | if (!trans->bytes_reserved) |
4007 | return; | |
6a63209f | 4008 | |
b24e03db | 4009 | btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved); |
a22285a6 YZ |
4010 | trans->bytes_reserved = 0; |
4011 | } | |
6a63209f | 4012 | |
d68fc57b YZ |
4013 | int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans, |
4014 | struct inode *inode) | |
4015 | { | |
4016 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4017 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); | |
4018 | struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv; | |
4019 | ||
4020 | /* | |
fcb80c2a JB |
4021 | * We need to hold space in order to delete our orphan item once we've |
4022 | * added it, so this takes the reservation so we can release it later | |
4023 | * when we are truly done with the orphan item. | |
d68fc57b | 4024 | */ |
ff5714cc | 4025 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
d68fc57b | 4026 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); |
6a63209f JB |
4027 | } |
4028 | ||
d68fc57b | 4029 | void btrfs_orphan_release_metadata(struct inode *inode) |
97e728d4 | 4030 | { |
d68fc57b | 4031 | struct btrfs_root *root = BTRFS_I(inode)->root; |
ff5714cc | 4032 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1); |
d68fc57b YZ |
4033 | btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes); |
4034 | } | |
97e728d4 | 4035 | |
a22285a6 YZ |
4036 | int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans, |
4037 | struct btrfs_pending_snapshot *pending) | |
4038 | { | |
4039 | struct btrfs_root *root = pending->root; | |
4040 | struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root); | |
4041 | struct btrfs_block_rsv *dst_rsv = &pending->block_rsv; | |
4042 | /* | |
4043 | * two for root back/forward refs, two for directory entries | |
4044 | * and one for root of the snapshot. | |
4045 | */ | |
16cdcec7 | 4046 | u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5); |
a22285a6 YZ |
4047 | dst_rsv->space_info = src_rsv->space_info; |
4048 | return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes); | |
97e728d4 JB |
4049 | } |
4050 | ||
7709cde3 JB |
4051 | /** |
4052 | * drop_outstanding_extent - drop an outstanding extent | |
4053 | * @inode: the inode we're dropping the extent for | |
4054 | * | |
4055 | * This is called when we are freeing up an outstanding extent, either called | |
4056 | * after an error or after an extent is written. This will return the number of | |
4057 | * reserved extents that need to be freed. This must be called with | |
4058 | * BTRFS_I(inode)->lock held. | |
4059 | */ | |
9e0baf60 JB |
4060 | static unsigned drop_outstanding_extent(struct inode *inode) |
4061 | { | |
7fd2ae21 | 4062 | unsigned drop_inode_space = 0; |
9e0baf60 JB |
4063 | unsigned dropped_extents = 0; |
4064 | ||
9e0baf60 JB |
4065 | BUG_ON(!BTRFS_I(inode)->outstanding_extents); |
4066 | BTRFS_I(inode)->outstanding_extents--; | |
4067 | ||
7fd2ae21 JB |
4068 | if (BTRFS_I(inode)->outstanding_extents == 0 && |
4069 | BTRFS_I(inode)->delalloc_meta_reserved) { | |
4070 | drop_inode_space = 1; | |
4071 | BTRFS_I(inode)->delalloc_meta_reserved = 0; | |
4072 | } | |
4073 | ||
9e0baf60 JB |
4074 | /* |
4075 | * If we have more or the same amount of outsanding extents than we have | |
4076 | * reserved then we need to leave the reserved extents count alone. | |
4077 | */ | |
4078 | if (BTRFS_I(inode)->outstanding_extents >= | |
4079 | BTRFS_I(inode)->reserved_extents) | |
7fd2ae21 | 4080 | return drop_inode_space; |
9e0baf60 JB |
4081 | |
4082 | dropped_extents = BTRFS_I(inode)->reserved_extents - | |
4083 | BTRFS_I(inode)->outstanding_extents; | |
4084 | BTRFS_I(inode)->reserved_extents -= dropped_extents; | |
7fd2ae21 | 4085 | return dropped_extents + drop_inode_space; |
9e0baf60 JB |
4086 | } |
4087 | ||
7709cde3 JB |
4088 | /** |
4089 | * calc_csum_metadata_size - return the amount of metada space that must be | |
4090 | * reserved/free'd for the given bytes. | |
4091 | * @inode: the inode we're manipulating | |
4092 | * @num_bytes: the number of bytes in question | |
4093 | * @reserve: 1 if we are reserving space, 0 if we are freeing space | |
4094 | * | |
4095 | * This adjusts the number of csum_bytes in the inode and then returns the | |
4096 | * correct amount of metadata that must either be reserved or freed. We | |
4097 | * calculate how many checksums we can fit into one leaf and then divide the | |
4098 | * number of bytes that will need to be checksumed by this value to figure out | |
4099 | * how many checksums will be required. If we are adding bytes then the number | |
4100 | * may go up and we will return the number of additional bytes that must be | |
4101 | * reserved. If it is going down we will return the number of bytes that must | |
4102 | * be freed. | |
4103 | * | |
4104 | * This must be called with BTRFS_I(inode)->lock held. | |
4105 | */ | |
4106 | static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes, | |
4107 | int reserve) | |
6324fbf3 | 4108 | { |
7709cde3 JB |
4109 | struct btrfs_root *root = BTRFS_I(inode)->root; |
4110 | u64 csum_size; | |
4111 | int num_csums_per_leaf; | |
4112 | int num_csums; | |
4113 | int old_csums; | |
4114 | ||
4115 | if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM && | |
4116 | BTRFS_I(inode)->csum_bytes == 0) | |
4117 | return 0; | |
4118 | ||
4119 | old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); | |
4120 | if (reserve) | |
4121 | BTRFS_I(inode)->csum_bytes += num_bytes; | |
4122 | else | |
4123 | BTRFS_I(inode)->csum_bytes -= num_bytes; | |
4124 | csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item); | |
4125 | num_csums_per_leaf = (int)div64_u64(csum_size, | |
4126 | sizeof(struct btrfs_csum_item) + | |
4127 | sizeof(struct btrfs_disk_key)); | |
4128 | num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize); | |
4129 | num_csums = num_csums + num_csums_per_leaf - 1; | |
4130 | num_csums = num_csums / num_csums_per_leaf; | |
4131 | ||
4132 | old_csums = old_csums + num_csums_per_leaf - 1; | |
4133 | old_csums = old_csums / num_csums_per_leaf; | |
4134 | ||
4135 | /* No change, no need to reserve more */ | |
4136 | if (old_csums == num_csums) | |
4137 | return 0; | |
4138 | ||
4139 | if (reserve) | |
4140 | return btrfs_calc_trans_metadata_size(root, | |
4141 | num_csums - old_csums); | |
4142 | ||
4143 | return btrfs_calc_trans_metadata_size(root, old_csums - num_csums); | |
0ca1f7ce | 4144 | } |
c146afad | 4145 | |
0ca1f7ce YZ |
4146 | int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes) |
4147 | { | |
4148 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
4149 | struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv; | |
9e0baf60 JB |
4150 | u64 to_reserve = 0; |
4151 | unsigned nr_extents = 0; | |
c09544e0 | 4152 | int flush = 1; |
0ca1f7ce | 4153 | int ret; |
6324fbf3 | 4154 | |
c09544e0 JB |
4155 | if (btrfs_is_free_space_inode(root, inode)) |
4156 | flush = 0; | |
4157 | ||
4158 | if (flush && btrfs_transaction_in_commit(root->fs_info)) | |
0ca1f7ce | 4159 | schedule_timeout(1); |
ec44a35c | 4160 | |
0ca1f7ce | 4161 | num_bytes = ALIGN(num_bytes, root->sectorsize); |
8bb8ab2e | 4162 | |
9e0baf60 JB |
4163 | spin_lock(&BTRFS_I(inode)->lock); |
4164 | BTRFS_I(inode)->outstanding_extents++; | |
4165 | ||
4166 | if (BTRFS_I(inode)->outstanding_extents > | |
4167 | BTRFS_I(inode)->reserved_extents) { | |
4168 | nr_extents = BTRFS_I(inode)->outstanding_extents - | |
4169 | BTRFS_I(inode)->reserved_extents; | |
4170 | BTRFS_I(inode)->reserved_extents += nr_extents; | |
7fd2ae21 | 4171 | } |
57a45ced | 4172 | |
7fd2ae21 JB |
4173 | /* |
4174 | * Add an item to reserve for updating the inode when we complete the | |
4175 | * delalloc io. | |
4176 | */ | |
4177 | if (!BTRFS_I(inode)->delalloc_meta_reserved) { | |
4178 | nr_extents++; | |
4179 | BTRFS_I(inode)->delalloc_meta_reserved = 1; | |
593060d7 | 4180 | } |
7fd2ae21 JB |
4181 | |
4182 | to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents); | |
7709cde3 | 4183 | to_reserve += calc_csum_metadata_size(inode, num_bytes, 1); |
9e0baf60 | 4184 | spin_unlock(&BTRFS_I(inode)->lock); |
57a45ced | 4185 | |
36ba022a | 4186 | ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush); |
9e0baf60 | 4187 | if (ret) { |
7ed49f18 | 4188 | u64 to_free = 0; |
9e0baf60 | 4189 | unsigned dropped; |
7ed49f18 | 4190 | |
7709cde3 | 4191 | spin_lock(&BTRFS_I(inode)->lock); |
9e0baf60 | 4192 | dropped = drop_outstanding_extent(inode); |
7ed49f18 | 4193 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
7709cde3 | 4194 | spin_unlock(&BTRFS_I(inode)->lock); |
7ed49f18 JB |
4195 | to_free += btrfs_calc_trans_metadata_size(root, dropped); |
4196 | ||
4197 | /* | |
4198 | * Somebody could have come in and twiddled with the | |
4199 | * reservation, so if we have to free more than we would have | |
4200 | * reserved from this reservation go ahead and release those | |
4201 | * bytes. | |
4202 | */ | |
4203 | to_free -= to_reserve; | |
4204 | if (to_free) | |
4205 | btrfs_block_rsv_release(root, block_rsv, to_free); | |
0ca1f7ce | 4206 | return ret; |
9e0baf60 | 4207 | } |
25179201 | 4208 | |
0ca1f7ce YZ |
4209 | block_rsv_add_bytes(block_rsv, to_reserve, 1); |
4210 | ||
0ca1f7ce YZ |
4211 | return 0; |
4212 | } | |
4213 | ||
7709cde3 JB |
4214 | /** |
4215 | * btrfs_delalloc_release_metadata - release a metadata reservation for an inode | |
4216 | * @inode: the inode to release the reservation for | |
4217 | * @num_bytes: the number of bytes we're releasing | |
4218 | * | |
4219 | * This will release the metadata reservation for an inode. This can be called | |
4220 | * once we complete IO for a given set of bytes to release their metadata | |
4221 | * reservations. | |
4222 | */ | |
0ca1f7ce YZ |
4223 | void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes) |
4224 | { | |
4225 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
9e0baf60 JB |
4226 | u64 to_free = 0; |
4227 | unsigned dropped; | |
0ca1f7ce YZ |
4228 | |
4229 | num_bytes = ALIGN(num_bytes, root->sectorsize); | |
7709cde3 | 4230 | spin_lock(&BTRFS_I(inode)->lock); |
9e0baf60 | 4231 | dropped = drop_outstanding_extent(inode); |
97e728d4 | 4232 | |
7709cde3 JB |
4233 | to_free = calc_csum_metadata_size(inode, num_bytes, 0); |
4234 | spin_unlock(&BTRFS_I(inode)->lock); | |
9e0baf60 JB |
4235 | if (dropped > 0) |
4236 | to_free += btrfs_calc_trans_metadata_size(root, dropped); | |
0ca1f7ce YZ |
4237 | |
4238 | btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv, | |
4239 | to_free); | |
4240 | } | |
4241 | ||
7709cde3 JB |
4242 | /** |
4243 | * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc | |
4244 | * @inode: inode we're writing to | |
4245 | * @num_bytes: the number of bytes we want to allocate | |
4246 | * | |
4247 | * This will do the following things | |
4248 | * | |
4249 | * o reserve space in the data space info for num_bytes | |
4250 | * o reserve space in the metadata space info based on number of outstanding | |
4251 | * extents and how much csums will be needed | |
4252 | * o add to the inodes ->delalloc_bytes | |
4253 | * o add it to the fs_info's delalloc inodes list. | |
4254 | * | |
4255 | * This will return 0 for success and -ENOSPC if there is no space left. | |
4256 | */ | |
0ca1f7ce YZ |
4257 | int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes) |
4258 | { | |
4259 | int ret; | |
4260 | ||
4261 | ret = btrfs_check_data_free_space(inode, num_bytes); | |
d397712b | 4262 | if (ret) |
0ca1f7ce YZ |
4263 | return ret; |
4264 | ||
4265 | ret = btrfs_delalloc_reserve_metadata(inode, num_bytes); | |
4266 | if (ret) { | |
4267 | btrfs_free_reserved_data_space(inode, num_bytes); | |
4268 | return ret; | |
4269 | } | |
4270 | ||
4271 | return 0; | |
4272 | } | |
4273 | ||
7709cde3 JB |
4274 | /** |
4275 | * btrfs_delalloc_release_space - release data and metadata space for delalloc | |
4276 | * @inode: inode we're releasing space for | |
4277 | * @num_bytes: the number of bytes we want to free up | |
4278 | * | |
4279 | * This must be matched with a call to btrfs_delalloc_reserve_space. This is | |
4280 | * called in the case that we don't need the metadata AND data reservations | |
4281 | * anymore. So if there is an error or we insert an inline extent. | |
4282 | * | |
4283 | * This function will release the metadata space that was not used and will | |
4284 | * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes | |
4285 | * list if there are no delalloc bytes left. | |
4286 | */ | |
0ca1f7ce YZ |
4287 | void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes) |
4288 | { | |
4289 | btrfs_delalloc_release_metadata(inode, num_bytes); | |
4290 | btrfs_free_reserved_data_space(inode, num_bytes); | |
6324fbf3 CM |
4291 | } |
4292 | ||
9078a3e1 CM |
4293 | static int update_block_group(struct btrfs_trans_handle *trans, |
4294 | struct btrfs_root *root, | |
f0486c68 | 4295 | u64 bytenr, u64 num_bytes, int alloc) |
9078a3e1 | 4296 | { |
0af3d00b | 4297 | struct btrfs_block_group_cache *cache = NULL; |
9078a3e1 | 4298 | struct btrfs_fs_info *info = root->fs_info; |
db94535d | 4299 | u64 total = num_bytes; |
9078a3e1 | 4300 | u64 old_val; |
db94535d | 4301 | u64 byte_in_group; |
0af3d00b | 4302 | int factor; |
3e1ad54f | 4303 | |
5d4f98a2 YZ |
4304 | /* block accounting for super block */ |
4305 | spin_lock(&info->delalloc_lock); | |
6c41761f | 4306 | old_val = btrfs_super_bytes_used(info->super_copy); |
5d4f98a2 YZ |
4307 | if (alloc) |
4308 | old_val += num_bytes; | |
4309 | else | |
4310 | old_val -= num_bytes; | |
6c41761f | 4311 | btrfs_set_super_bytes_used(info->super_copy, old_val); |
5d4f98a2 YZ |
4312 | spin_unlock(&info->delalloc_lock); |
4313 | ||
d397712b | 4314 | while (total) { |
db94535d | 4315 | cache = btrfs_lookup_block_group(info, bytenr); |
f3465ca4 | 4316 | if (!cache) |
9078a3e1 | 4317 | return -1; |
b742bb82 YZ |
4318 | if (cache->flags & (BTRFS_BLOCK_GROUP_DUP | |
4319 | BTRFS_BLOCK_GROUP_RAID1 | | |
4320 | BTRFS_BLOCK_GROUP_RAID10)) | |
4321 | factor = 2; | |
4322 | else | |
4323 | factor = 1; | |
9d66e233 JB |
4324 | /* |
4325 | * If this block group has free space cache written out, we | |
4326 | * need to make sure to load it if we are removing space. This | |
4327 | * is because we need the unpinning stage to actually add the | |
4328 | * space back to the block group, otherwise we will leak space. | |
4329 | */ | |
4330 | if (!alloc && cache->cached == BTRFS_CACHE_NO) | |
b8399dee | 4331 | cache_block_group(cache, trans, NULL, 1); |
0af3d00b | 4332 | |
db94535d CM |
4333 | byte_in_group = bytenr - cache->key.objectid; |
4334 | WARN_ON(byte_in_group > cache->key.offset); | |
9078a3e1 | 4335 | |
25179201 | 4336 | spin_lock(&cache->space_info->lock); |
c286ac48 | 4337 | spin_lock(&cache->lock); |
0af3d00b | 4338 | |
73bc1876 | 4339 | if (btrfs_test_opt(root, SPACE_CACHE) && |
0af3d00b JB |
4340 | cache->disk_cache_state < BTRFS_DC_CLEAR) |
4341 | cache->disk_cache_state = BTRFS_DC_CLEAR; | |
4342 | ||
0f9dd46c | 4343 | cache->dirty = 1; |
9078a3e1 | 4344 | old_val = btrfs_block_group_used(&cache->item); |
db94535d | 4345 | num_bytes = min(total, cache->key.offset - byte_in_group); |
cd1bc465 | 4346 | if (alloc) { |
db94535d | 4347 | old_val += num_bytes; |
11833d66 YZ |
4348 | btrfs_set_block_group_used(&cache->item, old_val); |
4349 | cache->reserved -= num_bytes; | |
11833d66 | 4350 | cache->space_info->bytes_reserved -= num_bytes; |
b742bb82 YZ |
4351 | cache->space_info->bytes_used += num_bytes; |
4352 | cache->space_info->disk_used += num_bytes * factor; | |
c286ac48 | 4353 | spin_unlock(&cache->lock); |
25179201 | 4354 | spin_unlock(&cache->space_info->lock); |
cd1bc465 | 4355 | } else { |
db94535d | 4356 | old_val -= num_bytes; |
c286ac48 | 4357 | btrfs_set_block_group_used(&cache->item, old_val); |
f0486c68 YZ |
4358 | cache->pinned += num_bytes; |
4359 | cache->space_info->bytes_pinned += num_bytes; | |
6324fbf3 | 4360 | cache->space_info->bytes_used -= num_bytes; |
b742bb82 | 4361 | cache->space_info->disk_used -= num_bytes * factor; |
c286ac48 | 4362 | spin_unlock(&cache->lock); |
25179201 | 4363 | spin_unlock(&cache->space_info->lock); |
1f3c79a2 | 4364 | |
f0486c68 YZ |
4365 | set_extent_dirty(info->pinned_extents, |
4366 | bytenr, bytenr + num_bytes - 1, | |
4367 | GFP_NOFS | __GFP_NOFAIL); | |
cd1bc465 | 4368 | } |
fa9c0d79 | 4369 | btrfs_put_block_group(cache); |
db94535d CM |
4370 | total -= num_bytes; |
4371 | bytenr += num_bytes; | |
9078a3e1 CM |
4372 | } |
4373 | return 0; | |
4374 | } | |
6324fbf3 | 4375 | |
a061fc8d CM |
4376 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
4377 | { | |
0f9dd46c | 4378 | struct btrfs_block_group_cache *cache; |
d2fb3437 | 4379 | u64 bytenr; |
0f9dd46c JB |
4380 | |
4381 | cache = btrfs_lookup_first_block_group(root->fs_info, search_start); | |
4382 | if (!cache) | |
a061fc8d | 4383 | return 0; |
0f9dd46c | 4384 | |
d2fb3437 | 4385 | bytenr = cache->key.objectid; |
fa9c0d79 | 4386 | btrfs_put_block_group(cache); |
d2fb3437 YZ |
4387 | |
4388 | return bytenr; | |
a061fc8d CM |
4389 | } |
4390 | ||
f0486c68 YZ |
4391 | static int pin_down_extent(struct btrfs_root *root, |
4392 | struct btrfs_block_group_cache *cache, | |
4393 | u64 bytenr, u64 num_bytes, int reserved) | |
324ae4df | 4394 | { |
11833d66 YZ |
4395 | spin_lock(&cache->space_info->lock); |
4396 | spin_lock(&cache->lock); | |
4397 | cache->pinned += num_bytes; | |
4398 | cache->space_info->bytes_pinned += num_bytes; | |
4399 | if (reserved) { | |
4400 | cache->reserved -= num_bytes; | |
4401 | cache->space_info->bytes_reserved -= num_bytes; | |
4402 | } | |
4403 | spin_unlock(&cache->lock); | |
4404 | spin_unlock(&cache->space_info->lock); | |
68b38550 | 4405 | |
f0486c68 YZ |
4406 | set_extent_dirty(root->fs_info->pinned_extents, bytenr, |
4407 | bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL); | |
4408 | return 0; | |
4409 | } | |
68b38550 | 4410 | |
f0486c68 YZ |
4411 | /* |
4412 | * this function must be called within transaction | |
4413 | */ | |
4414 | int btrfs_pin_extent(struct btrfs_root *root, | |
4415 | u64 bytenr, u64 num_bytes, int reserved) | |
4416 | { | |
4417 | struct btrfs_block_group_cache *cache; | |
68b38550 | 4418 | |
f0486c68 YZ |
4419 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); |
4420 | BUG_ON(!cache); | |
4421 | ||
4422 | pin_down_extent(root, cache, bytenr, num_bytes, reserved); | |
4423 | ||
4424 | btrfs_put_block_group(cache); | |
11833d66 YZ |
4425 | return 0; |
4426 | } | |
4427 | ||
e688b725 CM |
4428 | /* |
4429 | * this function must be called within transaction | |
4430 | */ | |
4431 | int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans, | |
4432 | struct btrfs_root *root, | |
4433 | u64 bytenr, u64 num_bytes) | |
4434 | { | |
4435 | struct btrfs_block_group_cache *cache; | |
4436 | ||
4437 | cache = btrfs_lookup_block_group(root->fs_info, bytenr); | |
4438 | BUG_ON(!cache); | |
4439 | ||
4440 | /* | |
4441 | * pull in the free space cache (if any) so that our pin | |
4442 | * removes the free space from the cache. We have load_only set | |
4443 | * to one because the slow code to read in the free extents does check | |
4444 | * the pinned extents. | |
4445 | */ | |
4446 | cache_block_group(cache, trans, root, 1); | |
4447 | ||
4448 | pin_down_extent(root, cache, bytenr, num_bytes, 0); | |
4449 | ||
4450 | /* remove us from the free space cache (if we're there at all) */ | |
4451 | btrfs_remove_free_space(cache, bytenr, num_bytes); | |
4452 | btrfs_put_block_group(cache); | |
4453 | return 0; | |
4454 | } | |
4455 | ||
fb25e914 JB |
4456 | /** |
4457 | * btrfs_update_reserved_bytes - update the block_group and space info counters | |
4458 | * @cache: The cache we are manipulating | |
4459 | * @num_bytes: The number of bytes in question | |
4460 | * @reserve: One of the reservation enums | |
4461 | * | |
4462 | * This is called by the allocator when it reserves space, or by somebody who is | |
4463 | * freeing space that was never actually used on disk. For example if you | |
4464 | * reserve some space for a new leaf in transaction A and before transaction A | |
4465 | * commits you free that leaf, you call this with reserve set to 0 in order to | |
4466 | * clear the reservation. | |
4467 | * | |
4468 | * Metadata reservations should be called with RESERVE_ALLOC so we do the proper | |
4469 | * ENOSPC accounting. For data we handle the reservation through clearing the | |
4470 | * delalloc bits in the io_tree. We have to do this since we could end up | |
4471 | * allocating less disk space for the amount of data we have reserved in the | |
4472 | * case of compression. | |
4473 | * | |
4474 | * If this is a reservation and the block group has become read only we cannot | |
4475 | * make the reservation and return -EAGAIN, otherwise this function always | |
4476 | * succeeds. | |
f0486c68 | 4477 | */ |
fb25e914 JB |
4478 | static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache, |
4479 | u64 num_bytes, int reserve) | |
11833d66 | 4480 | { |
fb25e914 | 4481 | struct btrfs_space_info *space_info = cache->space_info; |
f0486c68 | 4482 | int ret = 0; |
fb25e914 JB |
4483 | spin_lock(&space_info->lock); |
4484 | spin_lock(&cache->lock); | |
4485 | if (reserve != RESERVE_FREE) { | |
f0486c68 YZ |
4486 | if (cache->ro) { |
4487 | ret = -EAGAIN; | |
4488 | } else { | |
fb25e914 JB |
4489 | cache->reserved += num_bytes; |
4490 | space_info->bytes_reserved += num_bytes; | |
4491 | if (reserve == RESERVE_ALLOC) { | |
4492 | BUG_ON(space_info->bytes_may_use < num_bytes); | |
4493 | space_info->bytes_may_use -= num_bytes; | |
4494 | } | |
f0486c68 | 4495 | } |
fb25e914 JB |
4496 | } else { |
4497 | if (cache->ro) | |
4498 | space_info->bytes_readonly += num_bytes; | |
4499 | cache->reserved -= num_bytes; | |
4500 | space_info->bytes_reserved -= num_bytes; | |
4501 | space_info->reservation_progress++; | |
324ae4df | 4502 | } |
fb25e914 JB |
4503 | spin_unlock(&cache->lock); |
4504 | spin_unlock(&space_info->lock); | |
f0486c68 | 4505 | return ret; |
324ae4df | 4506 | } |
9078a3e1 | 4507 | |
11833d66 YZ |
4508 | int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans, |
4509 | struct btrfs_root *root) | |
e8569813 | 4510 | { |
e8569813 | 4511 | struct btrfs_fs_info *fs_info = root->fs_info; |
11833d66 YZ |
4512 | struct btrfs_caching_control *next; |
4513 | struct btrfs_caching_control *caching_ctl; | |
4514 | struct btrfs_block_group_cache *cache; | |
e8569813 | 4515 | |
11833d66 | 4516 | down_write(&fs_info->extent_commit_sem); |
25179201 | 4517 | |
11833d66 YZ |
4518 | list_for_each_entry_safe(caching_ctl, next, |
4519 | &fs_info->caching_block_groups, list) { | |
4520 | cache = caching_ctl->block_group; | |
4521 | if (block_group_cache_done(cache)) { | |
4522 | cache->last_byte_to_unpin = (u64)-1; | |
4523 | list_del_init(&caching_ctl->list); | |
4524 | put_caching_control(caching_ctl); | |
e8569813 | 4525 | } else { |
11833d66 | 4526 | cache->last_byte_to_unpin = caching_ctl->progress; |
e8569813 | 4527 | } |
e8569813 | 4528 | } |
11833d66 YZ |
4529 | |
4530 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) | |
4531 | fs_info->pinned_extents = &fs_info->freed_extents[1]; | |
4532 | else | |
4533 | fs_info->pinned_extents = &fs_info->freed_extents[0]; | |
4534 | ||
4535 | up_write(&fs_info->extent_commit_sem); | |
8929ecfa YZ |
4536 | |
4537 | update_global_block_rsv(fs_info); | |
e8569813 ZY |
4538 | return 0; |
4539 | } | |
4540 | ||
11833d66 | 4541 | static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
ccd467d6 | 4542 | { |
11833d66 YZ |
4543 | struct btrfs_fs_info *fs_info = root->fs_info; |
4544 | struct btrfs_block_group_cache *cache = NULL; | |
4545 | u64 len; | |
ccd467d6 | 4546 | |
11833d66 YZ |
4547 | while (start <= end) { |
4548 | if (!cache || | |
4549 | start >= cache->key.objectid + cache->key.offset) { | |
4550 | if (cache) | |
4551 | btrfs_put_block_group(cache); | |
4552 | cache = btrfs_lookup_block_group(fs_info, start); | |
4553 | BUG_ON(!cache); | |
4554 | } | |
4555 | ||
4556 | len = cache->key.objectid + cache->key.offset - start; | |
4557 | len = min(len, end + 1 - start); | |
4558 | ||
4559 | if (start < cache->last_byte_to_unpin) { | |
4560 | len = min(len, cache->last_byte_to_unpin - start); | |
4561 | btrfs_add_free_space(cache, start, len); | |
4562 | } | |
4563 | ||
f0486c68 YZ |
4564 | start += len; |
4565 | ||
11833d66 YZ |
4566 | spin_lock(&cache->space_info->lock); |
4567 | spin_lock(&cache->lock); | |
4568 | cache->pinned -= len; | |
4569 | cache->space_info->bytes_pinned -= len; | |
37be25bc | 4570 | if (cache->ro) |
f0486c68 | 4571 | cache->space_info->bytes_readonly += len; |
11833d66 YZ |
4572 | spin_unlock(&cache->lock); |
4573 | spin_unlock(&cache->space_info->lock); | |
ccd467d6 | 4574 | } |
11833d66 YZ |
4575 | |
4576 | if (cache) | |
4577 | btrfs_put_block_group(cache); | |
ccd467d6 CM |
4578 | return 0; |
4579 | } | |
4580 | ||
4581 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
11833d66 | 4582 | struct btrfs_root *root) |
a28ec197 | 4583 | { |
11833d66 YZ |
4584 | struct btrfs_fs_info *fs_info = root->fs_info; |
4585 | struct extent_io_tree *unpin; | |
1a5bc167 CM |
4586 | u64 start; |
4587 | u64 end; | |
a28ec197 | 4588 | int ret; |
a28ec197 | 4589 | |
11833d66 YZ |
4590 | if (fs_info->pinned_extents == &fs_info->freed_extents[0]) |
4591 | unpin = &fs_info->freed_extents[1]; | |
4592 | else | |
4593 | unpin = &fs_info->freed_extents[0]; | |
4594 | ||
d397712b | 4595 | while (1) { |
1a5bc167 CM |
4596 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
4597 | EXTENT_DIRTY); | |
4598 | if (ret) | |
a28ec197 | 4599 | break; |
1f3c79a2 | 4600 | |
5378e607 LD |
4601 | if (btrfs_test_opt(root, DISCARD)) |
4602 | ret = btrfs_discard_extent(root, start, | |
4603 | end + 1 - start, NULL); | |
1f3c79a2 | 4604 | |
1a5bc167 | 4605 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
11833d66 | 4606 | unpin_extent_range(root, start, end); |
b9473439 | 4607 | cond_resched(); |
a28ec197 | 4608 | } |
817d52f8 | 4609 | |
e20d96d6 CM |
4610 | return 0; |
4611 | } | |
4612 | ||
5d4f98a2 YZ |
4613 | static int __btrfs_free_extent(struct btrfs_trans_handle *trans, |
4614 | struct btrfs_root *root, | |
4615 | u64 bytenr, u64 num_bytes, u64 parent, | |
4616 | u64 root_objectid, u64 owner_objectid, | |
4617 | u64 owner_offset, int refs_to_drop, | |
4618 | struct btrfs_delayed_extent_op *extent_op) | |
a28ec197 | 4619 | { |
e2fa7227 | 4620 | struct btrfs_key key; |
5d4f98a2 | 4621 | struct btrfs_path *path; |
1261ec42 CM |
4622 | struct btrfs_fs_info *info = root->fs_info; |
4623 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 4624 | struct extent_buffer *leaf; |
5d4f98a2 YZ |
4625 | struct btrfs_extent_item *ei; |
4626 | struct btrfs_extent_inline_ref *iref; | |
a28ec197 | 4627 | int ret; |
5d4f98a2 | 4628 | int is_data; |
952fccac CM |
4629 | int extent_slot = 0; |
4630 | int found_extent = 0; | |
4631 | int num_to_del = 1; | |
5d4f98a2 YZ |
4632 | u32 item_size; |
4633 | u64 refs; | |
037e6390 | 4634 | |
5caf2a00 | 4635 | path = btrfs_alloc_path(); |
54aa1f4d CM |
4636 | if (!path) |
4637 | return -ENOMEM; | |
5f26f772 | 4638 | |
3c12ac72 | 4639 | path->reada = 1; |
b9473439 | 4640 | path->leave_spinning = 1; |
5d4f98a2 YZ |
4641 | |
4642 | is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID; | |
4643 | BUG_ON(!is_data && refs_to_drop != 1); | |
4644 | ||
4645 | ret = lookup_extent_backref(trans, extent_root, path, &iref, | |
4646 | bytenr, num_bytes, parent, | |
4647 | root_objectid, owner_objectid, | |
4648 | owner_offset); | |
7bb86316 | 4649 | if (ret == 0) { |
952fccac | 4650 | extent_slot = path->slots[0]; |
5d4f98a2 YZ |
4651 | while (extent_slot >= 0) { |
4652 | btrfs_item_key_to_cpu(path->nodes[0], &key, | |
952fccac | 4653 | extent_slot); |
5d4f98a2 | 4654 | if (key.objectid != bytenr) |
952fccac | 4655 | break; |
5d4f98a2 YZ |
4656 | if (key.type == BTRFS_EXTENT_ITEM_KEY && |
4657 | key.offset == num_bytes) { | |
952fccac CM |
4658 | found_extent = 1; |
4659 | break; | |
4660 | } | |
4661 | if (path->slots[0] - extent_slot > 5) | |
4662 | break; | |
5d4f98a2 | 4663 | extent_slot--; |
952fccac | 4664 | } |
5d4f98a2 YZ |
4665 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 |
4666 | item_size = btrfs_item_size_nr(path->nodes[0], extent_slot); | |
4667 | if (found_extent && item_size < sizeof(*ei)) | |
4668 | found_extent = 0; | |
4669 | #endif | |
31840ae1 | 4670 | if (!found_extent) { |
5d4f98a2 | 4671 | BUG_ON(iref); |
56bec294 | 4672 | ret = remove_extent_backref(trans, extent_root, path, |
5d4f98a2 YZ |
4673 | NULL, refs_to_drop, |
4674 | is_data); | |
31840ae1 | 4675 | BUG_ON(ret); |
b3b4aa74 | 4676 | btrfs_release_path(path); |
b9473439 | 4677 | path->leave_spinning = 1; |
5d4f98a2 YZ |
4678 | |
4679 | key.objectid = bytenr; | |
4680 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
4681 | key.offset = num_bytes; | |
4682 | ||
31840ae1 ZY |
4683 | ret = btrfs_search_slot(trans, extent_root, |
4684 | &key, path, -1, 1); | |
f3465ca4 JB |
4685 | if (ret) { |
4686 | printk(KERN_ERR "umm, got %d back from search" | |
d397712b CM |
4687 | ", was looking for %llu\n", ret, |
4688 | (unsigned long long)bytenr); | |
b783e62d JB |
4689 | if (ret > 0) |
4690 | btrfs_print_leaf(extent_root, | |
4691 | path->nodes[0]); | |
f3465ca4 | 4692 | } |
31840ae1 ZY |
4693 | BUG_ON(ret); |
4694 | extent_slot = path->slots[0]; | |
4695 | } | |
7bb86316 CM |
4696 | } else { |
4697 | btrfs_print_leaf(extent_root, path->nodes[0]); | |
4698 | WARN_ON(1); | |
d397712b | 4699 | printk(KERN_ERR "btrfs unable to find ref byte nr %llu " |
5d4f98a2 | 4700 | "parent %llu root %llu owner %llu offset %llu\n", |
d397712b | 4701 | (unsigned long long)bytenr, |
56bec294 | 4702 | (unsigned long long)parent, |
d397712b | 4703 | (unsigned long long)root_objectid, |
5d4f98a2 YZ |
4704 | (unsigned long long)owner_objectid, |
4705 | (unsigned long long)owner_offset); | |
7bb86316 | 4706 | } |
5f39d397 CM |
4707 | |
4708 | leaf = path->nodes[0]; | |
5d4f98a2 YZ |
4709 | item_size = btrfs_item_size_nr(leaf, extent_slot); |
4710 | #ifdef BTRFS_COMPAT_EXTENT_TREE_V0 | |
4711 | if (item_size < sizeof(*ei)) { | |
4712 | BUG_ON(found_extent || extent_slot != path->slots[0]); | |
4713 | ret = convert_extent_item_v0(trans, extent_root, path, | |
4714 | owner_objectid, 0); | |
4715 | BUG_ON(ret < 0); | |
4716 | ||
b3b4aa74 | 4717 | btrfs_release_path(path); |
5d4f98a2 YZ |
4718 | path->leave_spinning = 1; |
4719 | ||
4720 | key.objectid = bytenr; | |
4721 | key.type = BTRFS_EXTENT_ITEM_KEY; | |
4722 | key.offset = num_bytes; | |
4723 | ||
4724 | ret = btrfs_search_slot(trans, extent_root, &key, path, | |
4725 | -1, 1); | |
4726 | if (ret) { | |
4727 | printk(KERN_ERR "umm, got %d back from search" | |
4728 | ", was looking for %llu\n", ret, | |
4729 | (unsigned long long)bytenr); | |
4730 | btrfs_print_leaf(extent_root, path->nodes[0]); | |
4731 | } | |
4732 | BUG_ON(ret); | |
4733 | extent_slot = path->slots[0]; | |
4734 | leaf = path->nodes[0]; | |
4735 | item_size = btrfs_item_size_nr(leaf, extent_slot); | |
4736 | } | |
4737 | #endif | |
4738 | BUG_ON(item_size < sizeof(*ei)); | |
952fccac | 4739 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 4740 | struct btrfs_extent_item); |
5d4f98a2 YZ |
4741 | if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) { |
4742 | struct btrfs_tree_block_info *bi; | |
4743 | BUG_ON(item_size < sizeof(*ei) + sizeof(*bi)); | |
4744 | bi = (struct btrfs_tree_block_info *)(ei + 1); | |
4745 | WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi)); | |
4746 | } | |
56bec294 | 4747 | |
5d4f98a2 | 4748 | refs = btrfs_extent_refs(leaf, ei); |
56bec294 CM |
4749 | BUG_ON(refs < refs_to_drop); |
4750 | refs -= refs_to_drop; | |
5f39d397 | 4751 | |
5d4f98a2 YZ |
4752 | if (refs > 0) { |
4753 | if (extent_op) | |
4754 | __run_delayed_extent_op(extent_op, leaf, ei); | |
4755 | /* | |
4756 | * In the case of inline back ref, reference count will | |
4757 | * be updated by remove_extent_backref | |
952fccac | 4758 | */ |
5d4f98a2 YZ |
4759 | if (iref) { |
4760 | BUG_ON(!found_extent); | |
4761 | } else { | |
4762 | btrfs_set_extent_refs(leaf, ei, refs); | |
4763 | btrfs_mark_buffer_dirty(leaf); | |
4764 | } | |
4765 | if (found_extent) { | |
4766 | ret = remove_extent_backref(trans, extent_root, path, | |
4767 | iref, refs_to_drop, | |
4768 | is_data); | |
952fccac CM |
4769 | BUG_ON(ret); |
4770 | } | |
5d4f98a2 | 4771 | } else { |
5d4f98a2 YZ |
4772 | if (found_extent) { |
4773 | BUG_ON(is_data && refs_to_drop != | |
4774 | extent_data_ref_count(root, path, iref)); | |
4775 | if (iref) { | |
4776 | BUG_ON(path->slots[0] != extent_slot); | |
4777 | } else { | |
4778 | BUG_ON(path->slots[0] != extent_slot + 1); | |
4779 | path->slots[0] = extent_slot; | |
4780 | num_to_del = 2; | |
4781 | } | |
78fae27e | 4782 | } |
b9473439 | 4783 | |
952fccac CM |
4784 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
4785 | num_to_del); | |
31840ae1 | 4786 | BUG_ON(ret); |
b3b4aa74 | 4787 | btrfs_release_path(path); |
21af804c | 4788 | |
5d4f98a2 | 4789 | if (is_data) { |
459931ec CM |
4790 | ret = btrfs_del_csums(trans, root, bytenr, num_bytes); |
4791 | BUG_ON(ret); | |
d57e62b8 CM |
4792 | } else { |
4793 | invalidate_mapping_pages(info->btree_inode->i_mapping, | |
4794 | bytenr >> PAGE_CACHE_SHIFT, | |
4795 | (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT); | |
459931ec CM |
4796 | } |
4797 | ||
f0486c68 | 4798 | ret = update_block_group(trans, root, bytenr, num_bytes, 0); |
dcbdd4dc | 4799 | BUG_ON(ret); |
a28ec197 | 4800 | } |
5caf2a00 | 4801 | btrfs_free_path(path); |
a28ec197 CM |
4802 | return ret; |
4803 | } | |
4804 | ||
1887be66 | 4805 | /* |
f0486c68 | 4806 | * when we free an block, it is possible (and likely) that we free the last |
1887be66 CM |
4807 | * delayed ref for that extent as well. This searches the delayed ref tree for |
4808 | * a given extent, and if there are no other delayed refs to be processed, it | |
4809 | * removes it from the tree. | |
4810 | */ | |
4811 | static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans, | |
4812 | struct btrfs_root *root, u64 bytenr) | |
4813 | { | |
4814 | struct btrfs_delayed_ref_head *head; | |
4815 | struct btrfs_delayed_ref_root *delayed_refs; | |
4816 | struct btrfs_delayed_ref_node *ref; | |
4817 | struct rb_node *node; | |
f0486c68 | 4818 | int ret = 0; |
1887be66 CM |
4819 | |
4820 | delayed_refs = &trans->transaction->delayed_refs; | |
4821 | spin_lock(&delayed_refs->lock); | |
4822 | head = btrfs_find_delayed_ref_head(trans, bytenr); | |
4823 | if (!head) | |
4824 | goto out; | |
4825 | ||
4826 | node = rb_prev(&head->node.rb_node); | |
4827 | if (!node) | |
4828 | goto out; | |
4829 | ||
4830 | ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node); | |
4831 | ||
4832 | /* there are still entries for this ref, we can't drop it */ | |
4833 | if (ref->bytenr == bytenr) | |
4834 | goto out; | |
4835 | ||
5d4f98a2 YZ |
4836 | if (head->extent_op) { |
4837 | if (!head->must_insert_reserved) | |
4838 | goto out; | |
4839 | kfree(head->extent_op); | |
4840 | head->extent_op = NULL; | |
4841 | } | |
4842 | ||
1887be66 CM |
4843 | /* |
4844 | * waiting for the lock here would deadlock. If someone else has it | |
4845 | * locked they are already in the process of dropping it anyway | |
4846 | */ | |
4847 | if (!mutex_trylock(&head->mutex)) | |
4848 | goto out; | |
4849 | ||
4850 | /* | |
4851 | * at this point we have a head with no other entries. Go | |
4852 | * ahead and process it. | |
4853 | */ | |
4854 | head->node.in_tree = 0; | |
4855 | rb_erase(&head->node.rb_node, &delayed_refs->root); | |
c3e69d58 | 4856 | |
1887be66 CM |
4857 | delayed_refs->num_entries--; |
4858 | ||
4859 | /* | |
4860 | * we don't take a ref on the node because we're removing it from the | |
4861 | * tree, so we just steal the ref the tree was holding. | |
4862 | */ | |
c3e69d58 CM |
4863 | delayed_refs->num_heads--; |
4864 | if (list_empty(&head->cluster)) | |
4865 | delayed_refs->num_heads_ready--; | |
4866 | ||
4867 | list_del_init(&head->cluster); | |
1887be66 CM |
4868 | spin_unlock(&delayed_refs->lock); |
4869 | ||
f0486c68 YZ |
4870 | BUG_ON(head->extent_op); |
4871 | if (head->must_insert_reserved) | |
4872 | ret = 1; | |
4873 | ||
4874 | mutex_unlock(&head->mutex); | |
1887be66 | 4875 | btrfs_put_delayed_ref(&head->node); |
f0486c68 | 4876 | return ret; |
1887be66 CM |
4877 | out: |
4878 | spin_unlock(&delayed_refs->lock); | |
4879 | return 0; | |
4880 | } | |
4881 | ||
f0486c68 YZ |
4882 | void btrfs_free_tree_block(struct btrfs_trans_handle *trans, |
4883 | struct btrfs_root *root, | |
4884 | struct extent_buffer *buf, | |
4885 | u64 parent, int last_ref) | |
4886 | { | |
f0486c68 YZ |
4887 | struct btrfs_block_group_cache *cache = NULL; |
4888 | int ret; | |
4889 | ||
4890 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | |
4891 | ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len, | |
4892 | parent, root->root_key.objectid, | |
4893 | btrfs_header_level(buf), | |
4894 | BTRFS_DROP_DELAYED_REF, NULL); | |
4895 | BUG_ON(ret); | |
4896 | } | |
4897 | ||
4898 | if (!last_ref) | |
4899 | return; | |
4900 | ||
f0486c68 | 4901 | cache = btrfs_lookup_block_group(root->fs_info, buf->start); |
f0486c68 YZ |
4902 | |
4903 | if (btrfs_header_generation(buf) == trans->transid) { | |
4904 | if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) { | |
4905 | ret = check_ref_cleanup(trans, root, buf->start); | |
4906 | if (!ret) | |
37be25bc | 4907 | goto out; |
f0486c68 YZ |
4908 | } |
4909 | ||
4910 | if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) { | |
4911 | pin_down_extent(root, cache, buf->start, buf->len, 1); | |
37be25bc | 4912 | goto out; |
f0486c68 YZ |
4913 | } |
4914 | ||
4915 | WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)); | |
4916 | ||
4917 | btrfs_add_free_space(cache, buf->start, buf->len); | |
fb25e914 | 4918 | btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE); |
f0486c68 YZ |
4919 | } |
4920 | out: | |
a826d6dc JB |
4921 | /* |
4922 | * Deleting the buffer, clear the corrupt flag since it doesn't matter | |
4923 | * anymore. | |
4924 | */ | |
4925 | clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags); | |
f0486c68 YZ |
4926 | btrfs_put_block_group(cache); |
4927 | } | |
4928 | ||
925baedd | 4929 | int btrfs_free_extent(struct btrfs_trans_handle *trans, |
31840ae1 ZY |
4930 | struct btrfs_root *root, |
4931 | u64 bytenr, u64 num_bytes, u64 parent, | |
5d4f98a2 | 4932 | u64 root_objectid, u64 owner, u64 offset) |
925baedd CM |
4933 | { |
4934 | int ret; | |
4935 | ||
56bec294 CM |
4936 | /* |
4937 | * tree log blocks never actually go into the extent allocation | |
4938 | * tree, just update pinning info and exit early. | |
56bec294 | 4939 | */ |
5d4f98a2 YZ |
4940 | if (root_objectid == BTRFS_TREE_LOG_OBJECTID) { |
4941 | WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID); | |
b9473439 | 4942 | /* unlocks the pinned mutex */ |
11833d66 | 4943 | btrfs_pin_extent(root, bytenr, num_bytes, 1); |
56bec294 | 4944 | ret = 0; |
5d4f98a2 YZ |
4945 | } else if (owner < BTRFS_FIRST_FREE_OBJECTID) { |
4946 | ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes, | |
4947 | parent, root_objectid, (int)owner, | |
4948 | BTRFS_DROP_DELAYED_REF, NULL); | |
1887be66 | 4949 | BUG_ON(ret); |
5d4f98a2 YZ |
4950 | } else { |
4951 | ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes, | |
4952 | parent, root_objectid, owner, | |
4953 | offset, BTRFS_DROP_DELAYED_REF, NULL); | |
4954 | BUG_ON(ret); | |
56bec294 | 4955 | } |
925baedd CM |
4956 | return ret; |
4957 | } | |
4958 | ||
87ee04eb CM |
4959 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
4960 | { | |
4961 | u64 mask = ((u64)root->stripesize - 1); | |
4962 | u64 ret = (val + mask) & ~mask; | |
4963 | return ret; | |
4964 | } | |
4965 | ||
817d52f8 JB |
4966 | /* |
4967 | * when we wait for progress in the block group caching, its because | |
4968 | * our allocation attempt failed at least once. So, we must sleep | |
4969 | * and let some progress happen before we try again. | |
4970 | * | |
4971 | * This function will sleep at least once waiting for new free space to | |
4972 | * show up, and then it will check the block group free space numbers | |
4973 | * for our min num_bytes. Another option is to have it go ahead | |
4974 | * and look in the rbtree for a free extent of a given size, but this | |
4975 | * is a good start. | |
4976 | */ | |
4977 | static noinline int | |
4978 | wait_block_group_cache_progress(struct btrfs_block_group_cache *cache, | |
4979 | u64 num_bytes) | |
4980 | { | |
11833d66 | 4981 | struct btrfs_caching_control *caching_ctl; |
817d52f8 JB |
4982 | DEFINE_WAIT(wait); |
4983 | ||
11833d66 YZ |
4984 | caching_ctl = get_caching_control(cache); |
4985 | if (!caching_ctl) | |
817d52f8 | 4986 | return 0; |
817d52f8 | 4987 | |
11833d66 | 4988 | wait_event(caching_ctl->wait, block_group_cache_done(cache) || |
34d52cb6 | 4989 | (cache->free_space_ctl->free_space >= num_bytes)); |
11833d66 YZ |
4990 | |
4991 | put_caching_control(caching_ctl); | |
4992 | return 0; | |
4993 | } | |
4994 | ||
4995 | static noinline int | |
4996 | wait_block_group_cache_done(struct btrfs_block_group_cache *cache) | |
4997 | { | |
4998 | struct btrfs_caching_control *caching_ctl; | |
4999 | DEFINE_WAIT(wait); | |
5000 | ||
5001 | caching_ctl = get_caching_control(cache); | |
5002 | if (!caching_ctl) | |
5003 | return 0; | |
5004 | ||
5005 | wait_event(caching_ctl->wait, block_group_cache_done(cache)); | |
5006 | ||
5007 | put_caching_control(caching_ctl); | |
817d52f8 JB |
5008 | return 0; |
5009 | } | |
5010 | ||
b742bb82 YZ |
5011 | static int get_block_group_index(struct btrfs_block_group_cache *cache) |
5012 | { | |
5013 | int index; | |
5014 | if (cache->flags & BTRFS_BLOCK_GROUP_RAID10) | |
5015 | index = 0; | |
5016 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1) | |
5017 | index = 1; | |
5018 | else if (cache->flags & BTRFS_BLOCK_GROUP_DUP) | |
5019 | index = 2; | |
5020 | else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0) | |
5021 | index = 3; | |
5022 | else | |
5023 | index = 4; | |
5024 | return index; | |
5025 | } | |
5026 | ||
817d52f8 | 5027 | enum btrfs_loop_type { |
ccf0e725 | 5028 | LOOP_FIND_IDEAL = 0, |
817d52f8 JB |
5029 | LOOP_CACHING_NOWAIT = 1, |
5030 | LOOP_CACHING_WAIT = 2, | |
5031 | LOOP_ALLOC_CHUNK = 3, | |
5032 | LOOP_NO_EMPTY_SIZE = 4, | |
5033 | }; | |
5034 | ||
fec577fb CM |
5035 | /* |
5036 | * walks the btree of allocated extents and find a hole of a given size. | |
5037 | * The key ins is changed to record the hole: | |
5038 | * ins->objectid == block start | |
62e2749e | 5039 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
5040 | * ins->offset == number of blocks |
5041 | * Any available blocks before search_start are skipped. | |
5042 | */ | |
d397712b | 5043 | static noinline int find_free_extent(struct btrfs_trans_handle *trans, |
98ed5174 CM |
5044 | struct btrfs_root *orig_root, |
5045 | u64 num_bytes, u64 empty_size, | |
5046 | u64 search_start, u64 search_end, | |
5047 | u64 hint_byte, struct btrfs_key *ins, | |
e0f54067 | 5048 | u64 data) |
fec577fb | 5049 | { |
80eb234a | 5050 | int ret = 0; |
d397712b | 5051 | struct btrfs_root *root = orig_root->fs_info->extent_root; |
fa9c0d79 | 5052 | struct btrfs_free_cluster *last_ptr = NULL; |
80eb234a | 5053 | struct btrfs_block_group_cache *block_group = NULL; |
239b14b3 | 5054 | int empty_cluster = 2 * 1024 * 1024; |
0ef3e66b | 5055 | int allowed_chunk_alloc = 0; |
ccf0e725 | 5056 | int done_chunk_alloc = 0; |
80eb234a | 5057 | struct btrfs_space_info *space_info; |
fa9c0d79 CM |
5058 | int last_ptr_loop = 0; |
5059 | int loop = 0; | |
f0486c68 | 5060 | int index = 0; |
fb25e914 JB |
5061 | int alloc_type = (data & BTRFS_BLOCK_GROUP_DATA) ? |
5062 | RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC; | |
817d52f8 | 5063 | bool found_uncached_bg = false; |
0a24325e | 5064 | bool failed_cluster_refill = false; |
1cdda9b8 | 5065 | bool failed_alloc = false; |
67377734 | 5066 | bool use_cluster = true; |
60d2adbb | 5067 | bool have_caching_bg = false; |
ccf0e725 JB |
5068 | u64 ideal_cache_percent = 0; |
5069 | u64 ideal_cache_offset = 0; | |
fec577fb | 5070 | |
db94535d | 5071 | WARN_ON(num_bytes < root->sectorsize); |
b1a4d965 | 5072 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
80eb234a JB |
5073 | ins->objectid = 0; |
5074 | ins->offset = 0; | |
b1a4d965 | 5075 | |
2552d17e | 5076 | space_info = __find_space_info(root->fs_info, data); |
1b1d1f66 | 5077 | if (!space_info) { |
e0f54067 | 5078 | printk(KERN_ERR "No space info for %llu\n", data); |
1b1d1f66 JB |
5079 | return -ENOSPC; |
5080 | } | |
2552d17e | 5081 | |
67377734 JB |
5082 | /* |
5083 | * If the space info is for both data and metadata it means we have a | |
5084 | * small filesystem and we can't use the clustering stuff. | |
5085 | */ | |
5086 | if (btrfs_mixed_space_info(space_info)) | |
5087 | use_cluster = false; | |
5088 | ||
0ef3e66b CM |
5089 | if (orig_root->ref_cows || empty_size) |
5090 | allowed_chunk_alloc = 1; | |
5091 | ||
67377734 | 5092 | if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) { |
fa9c0d79 | 5093 | last_ptr = &root->fs_info->meta_alloc_cluster; |
536ac8ae CM |
5094 | if (!btrfs_test_opt(root, SSD)) |
5095 | empty_cluster = 64 * 1024; | |
239b14b3 CM |
5096 | } |
5097 | ||
67377734 JB |
5098 | if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster && |
5099 | btrfs_test_opt(root, SSD)) { | |
fa9c0d79 CM |
5100 | last_ptr = &root->fs_info->data_alloc_cluster; |
5101 | } | |
0f9dd46c | 5102 | |
239b14b3 | 5103 | if (last_ptr) { |
fa9c0d79 CM |
5104 | spin_lock(&last_ptr->lock); |
5105 | if (last_ptr->block_group) | |
5106 | hint_byte = last_ptr->window_start; | |
5107 | spin_unlock(&last_ptr->lock); | |
239b14b3 | 5108 | } |
fa9c0d79 | 5109 | |
a061fc8d | 5110 | search_start = max(search_start, first_logical_byte(root, 0)); |
239b14b3 | 5111 | search_start = max(search_start, hint_byte); |
0b86a832 | 5112 | |
817d52f8 | 5113 | if (!last_ptr) |
fa9c0d79 | 5114 | empty_cluster = 0; |
fa9c0d79 | 5115 | |
2552d17e | 5116 | if (search_start == hint_byte) { |
ccf0e725 | 5117 | ideal_cache: |
2552d17e JB |
5118 | block_group = btrfs_lookup_block_group(root->fs_info, |
5119 | search_start); | |
817d52f8 JB |
5120 | /* |
5121 | * we don't want to use the block group if it doesn't match our | |
5122 | * allocation bits, or if its not cached. | |
ccf0e725 JB |
5123 | * |
5124 | * However if we are re-searching with an ideal block group | |
5125 | * picked out then we don't care that the block group is cached. | |
817d52f8 JB |
5126 | */ |
5127 | if (block_group && block_group_bits(block_group, data) && | |
ccf0e725 JB |
5128 | (block_group->cached != BTRFS_CACHE_NO || |
5129 | search_start == ideal_cache_offset)) { | |
2552d17e | 5130 | down_read(&space_info->groups_sem); |
44fb5511 CM |
5131 | if (list_empty(&block_group->list) || |
5132 | block_group->ro) { | |
5133 | /* | |
5134 | * someone is removing this block group, | |
5135 | * we can't jump into the have_block_group | |
5136 | * target because our list pointers are not | |
5137 | * valid | |
5138 | */ | |
5139 | btrfs_put_block_group(block_group); | |
5140 | up_read(&space_info->groups_sem); | |
ccf0e725 | 5141 | } else { |
b742bb82 | 5142 | index = get_block_group_index(block_group); |
44fb5511 | 5143 | goto have_block_group; |
ccf0e725 | 5144 | } |
2552d17e | 5145 | } else if (block_group) { |
fa9c0d79 | 5146 | btrfs_put_block_group(block_group); |
2552d17e | 5147 | } |
42e70e7a | 5148 | } |
2552d17e | 5149 | search: |
60d2adbb | 5150 | have_caching_bg = false; |
80eb234a | 5151 | down_read(&space_info->groups_sem); |
b742bb82 YZ |
5152 | list_for_each_entry(block_group, &space_info->block_groups[index], |
5153 | list) { | |
6226cb0a | 5154 | u64 offset; |
817d52f8 | 5155 | int cached; |
8a1413a2 | 5156 | |
11dfe35a | 5157 | btrfs_get_block_group(block_group); |
2552d17e | 5158 | search_start = block_group->key.objectid; |
42e70e7a | 5159 | |
83a50de9 CM |
5160 | /* |
5161 | * this can happen if we end up cycling through all the | |
5162 | * raid types, but we want to make sure we only allocate | |
5163 | * for the proper type. | |
5164 | */ | |
5165 | if (!block_group_bits(block_group, data)) { | |
5166 | u64 extra = BTRFS_BLOCK_GROUP_DUP | | |
5167 | BTRFS_BLOCK_GROUP_RAID1 | | |
5168 | BTRFS_BLOCK_GROUP_RAID10; | |
5169 | ||
5170 | /* | |
5171 | * if they asked for extra copies and this block group | |
5172 | * doesn't provide them, bail. This does allow us to | |
5173 | * fill raid0 from raid1. | |
5174 | */ | |
5175 | if ((data & extra) && !(block_group->flags & extra)) | |
5176 | goto loop; | |
5177 | } | |
5178 | ||
2552d17e | 5179 | have_block_group: |
817d52f8 | 5180 | if (unlikely(block_group->cached == BTRFS_CACHE_NO)) { |
ccf0e725 JB |
5181 | u64 free_percent; |
5182 | ||
b8399dee JB |
5183 | ret = cache_block_group(block_group, trans, |
5184 | orig_root, 1); | |
9d66e233 JB |
5185 | if (block_group->cached == BTRFS_CACHE_FINISHED) |
5186 | goto have_block_group; | |
5187 | ||
ccf0e725 JB |
5188 | free_percent = btrfs_block_group_used(&block_group->item); |
5189 | free_percent *= 100; | |
5190 | free_percent = div64_u64(free_percent, | |
5191 | block_group->key.offset); | |
5192 | free_percent = 100 - free_percent; | |
5193 | if (free_percent > ideal_cache_percent && | |
5194 | likely(!block_group->ro)) { | |
5195 | ideal_cache_offset = block_group->key.objectid; | |
5196 | ideal_cache_percent = free_percent; | |
5197 | } | |
5198 | ||
817d52f8 | 5199 | /* |
bab39bf9 JB |
5200 | * The caching workers are limited to 2 threads, so we |
5201 | * can queue as much work as we care to. | |
817d52f8 | 5202 | */ |
bab39bf9 | 5203 | if (loop > LOOP_FIND_IDEAL) { |
b8399dee JB |
5204 | ret = cache_block_group(block_group, trans, |
5205 | orig_root, 0); | |
817d52f8 | 5206 | BUG_ON(ret); |
2552d17e | 5207 | } |
817d52f8 JB |
5208 | found_uncached_bg = true; |
5209 | ||
ccf0e725 JB |
5210 | /* |
5211 | * If loop is set for cached only, try the next block | |
5212 | * group. | |
5213 | */ | |
5214 | if (loop == LOOP_FIND_IDEAL) | |
817d52f8 JB |
5215 | goto loop; |
5216 | } | |
5217 | ||
ccf0e725 JB |
5218 | cached = block_group_cache_done(block_group); |
5219 | if (unlikely(!cached)) | |
5220 | found_uncached_bg = true; | |
5221 | ||
ea6a478e | 5222 | if (unlikely(block_group->ro)) |
2552d17e | 5223 | goto loop; |
0f9dd46c | 5224 | |
ff5714cc | 5225 | spin_lock(&block_group->free_space_ctl->tree_lock); |
cca1c81f | 5226 | if (cached && |
ff5714cc CM |
5227 | block_group->free_space_ctl->free_space < |
5228 | num_bytes + empty_size) { | |
5229 | spin_unlock(&block_group->free_space_ctl->tree_lock); | |
cca1c81f JB |
5230 | goto loop; |
5231 | } | |
ff5714cc | 5232 | spin_unlock(&block_group->free_space_ctl->tree_lock); |
cca1c81f | 5233 | |
0a24325e JB |
5234 | /* |
5235 | * Ok we want to try and use the cluster allocator, so lets look | |
5236 | * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will | |
5237 | * have tried the cluster allocator plenty of times at this | |
5238 | * point and not have found anything, so we are likely way too | |
5239 | * fragmented for the clustering stuff to find anything, so lets | |
5240 | * just skip it and let the allocator find whatever block it can | |
5241 | * find | |
5242 | */ | |
5243 | if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) { | |
fa9c0d79 CM |
5244 | /* |
5245 | * the refill lock keeps out other | |
5246 | * people trying to start a new cluster | |
5247 | */ | |
5248 | spin_lock(&last_ptr->refill_lock); | |
44fb5511 CM |
5249 | if (last_ptr->block_group && |
5250 | (last_ptr->block_group->ro || | |
5251 | !block_group_bits(last_ptr->block_group, data))) { | |
5252 | offset = 0; | |
5253 | goto refill_cluster; | |
5254 | } | |
5255 | ||
fa9c0d79 CM |
5256 | offset = btrfs_alloc_from_cluster(block_group, last_ptr, |
5257 | num_bytes, search_start); | |
5258 | if (offset) { | |
5259 | /* we have a block, we're done */ | |
5260 | spin_unlock(&last_ptr->refill_lock); | |
5261 | goto checks; | |
5262 | } | |
5263 | ||
5264 | spin_lock(&last_ptr->lock); | |
5265 | /* | |
5266 | * whoops, this cluster doesn't actually point to | |
5267 | * this block group. Get a ref on the block | |
5268 | * group is does point to and try again | |
5269 | */ | |
5270 | if (!last_ptr_loop && last_ptr->block_group && | |
ff1f2b44 | 5271 | last_ptr->block_group != block_group && |
5272 | index <= | |
5273 | get_block_group_index(last_ptr->block_group)) { | |
fa9c0d79 CM |
5274 | |
5275 | btrfs_put_block_group(block_group); | |
5276 | block_group = last_ptr->block_group; | |
11dfe35a | 5277 | btrfs_get_block_group(block_group); |
fa9c0d79 CM |
5278 | spin_unlock(&last_ptr->lock); |
5279 | spin_unlock(&last_ptr->refill_lock); | |
5280 | ||
5281 | last_ptr_loop = 1; | |
5282 | search_start = block_group->key.objectid; | |
44fb5511 CM |
5283 | /* |
5284 | * we know this block group is properly | |
5285 | * in the list because | |
5286 | * btrfs_remove_block_group, drops the | |
5287 | * cluster before it removes the block | |
5288 | * group from the list | |
5289 | */ | |
fa9c0d79 CM |
5290 | goto have_block_group; |
5291 | } | |
5292 | spin_unlock(&last_ptr->lock); | |
44fb5511 | 5293 | refill_cluster: |
fa9c0d79 CM |
5294 | /* |
5295 | * this cluster didn't work out, free it and | |
5296 | * start over | |
5297 | */ | |
5298 | btrfs_return_cluster_to_free_space(NULL, last_ptr); | |
5299 | ||
5300 | last_ptr_loop = 0; | |
5301 | ||
5302 | /* allocate a cluster in this block group */ | |
451d7585 | 5303 | ret = btrfs_find_space_cluster(trans, root, |
fa9c0d79 CM |
5304 | block_group, last_ptr, |
5305 | offset, num_bytes, | |
5306 | empty_cluster + empty_size); | |
5307 | if (ret == 0) { | |
5308 | /* | |
5309 | * now pull our allocation out of this | |
5310 | * cluster | |
5311 | */ | |
5312 | offset = btrfs_alloc_from_cluster(block_group, | |
5313 | last_ptr, num_bytes, | |
5314 | search_start); | |
5315 | if (offset) { | |
5316 | /* we found one, proceed */ | |
5317 | spin_unlock(&last_ptr->refill_lock); | |
5318 | goto checks; | |
5319 | } | |
0a24325e JB |
5320 | } else if (!cached && loop > LOOP_CACHING_NOWAIT |
5321 | && !failed_cluster_refill) { | |
817d52f8 JB |
5322 | spin_unlock(&last_ptr->refill_lock); |
5323 | ||
0a24325e | 5324 | failed_cluster_refill = true; |
817d52f8 JB |
5325 | wait_block_group_cache_progress(block_group, |
5326 | num_bytes + empty_cluster + empty_size); | |
5327 | goto have_block_group; | |
fa9c0d79 | 5328 | } |
817d52f8 | 5329 | |
fa9c0d79 CM |
5330 | /* |
5331 | * at this point we either didn't find a cluster | |
5332 | * or we weren't able to allocate a block from our | |
5333 | * cluster. Free the cluster we've been trying | |
5334 | * to use, and go to the next block group | |
5335 | */ | |
0a24325e | 5336 | btrfs_return_cluster_to_free_space(NULL, last_ptr); |
fa9c0d79 | 5337 | spin_unlock(&last_ptr->refill_lock); |
0a24325e | 5338 | goto loop; |
fa9c0d79 CM |
5339 | } |
5340 | ||
6226cb0a JB |
5341 | offset = btrfs_find_space_for_alloc(block_group, search_start, |
5342 | num_bytes, empty_size); | |
1cdda9b8 JB |
5343 | /* |
5344 | * If we didn't find a chunk, and we haven't failed on this | |
5345 | * block group before, and this block group is in the middle of | |
5346 | * caching and we are ok with waiting, then go ahead and wait | |
5347 | * for progress to be made, and set failed_alloc to true. | |
5348 | * | |
5349 | * If failed_alloc is true then we've already waited on this | |
5350 | * block group once and should move on to the next block group. | |
5351 | */ | |
5352 | if (!offset && !failed_alloc && !cached && | |
5353 | loop > LOOP_CACHING_NOWAIT) { | |
817d52f8 | 5354 | wait_block_group_cache_progress(block_group, |
1cdda9b8 JB |
5355 | num_bytes + empty_size); |
5356 | failed_alloc = true; | |
817d52f8 | 5357 | goto have_block_group; |
1cdda9b8 | 5358 | } else if (!offset) { |
60d2adbb MX |
5359 | if (!cached) |
5360 | have_caching_bg = true; | |
1cdda9b8 | 5361 | goto loop; |
817d52f8 | 5362 | } |
fa9c0d79 | 5363 | checks: |
6226cb0a | 5364 | search_start = stripe_align(root, offset); |
2552d17e | 5365 | /* move on to the next group */ |
6226cb0a JB |
5366 | if (search_start + num_bytes >= search_end) { |
5367 | btrfs_add_free_space(block_group, offset, num_bytes); | |
2552d17e | 5368 | goto loop; |
6226cb0a | 5369 | } |
25179201 | 5370 | |
2552d17e JB |
5371 | /* move on to the next group */ |
5372 | if (search_start + num_bytes > | |
6226cb0a JB |
5373 | block_group->key.objectid + block_group->key.offset) { |
5374 | btrfs_add_free_space(block_group, offset, num_bytes); | |
2552d17e | 5375 | goto loop; |
6226cb0a | 5376 | } |
f5a31e16 | 5377 | |
f0486c68 YZ |
5378 | ins->objectid = search_start; |
5379 | ins->offset = num_bytes; | |
2552d17e | 5380 | |
f0486c68 YZ |
5381 | if (offset < search_start) |
5382 | btrfs_add_free_space(block_group, offset, | |
5383 | search_start - offset); | |
5384 | BUG_ON(offset > search_start); | |
2552d17e | 5385 | |
fb25e914 JB |
5386 | ret = btrfs_update_reserved_bytes(block_group, num_bytes, |
5387 | alloc_type); | |
f0486c68 | 5388 | if (ret == -EAGAIN) { |
6226cb0a | 5389 | btrfs_add_free_space(block_group, offset, num_bytes); |
2552d17e | 5390 | goto loop; |
0f9dd46c | 5391 | } |
0b86a832 | 5392 | |
f0486c68 | 5393 | /* we are all good, lets return */ |
2552d17e JB |
5394 | ins->objectid = search_start; |
5395 | ins->offset = num_bytes; | |
d2fb3437 | 5396 | |
6226cb0a JB |
5397 | if (offset < search_start) |
5398 | btrfs_add_free_space(block_group, offset, | |
5399 | search_start - offset); | |
5400 | BUG_ON(offset > search_start); | |
d82a6f1d | 5401 | btrfs_put_block_group(block_group); |
2552d17e JB |
5402 | break; |
5403 | loop: | |
0a24325e | 5404 | failed_cluster_refill = false; |
1cdda9b8 | 5405 | failed_alloc = false; |
b742bb82 | 5406 | BUG_ON(index != get_block_group_index(block_group)); |
fa9c0d79 | 5407 | btrfs_put_block_group(block_group); |
2552d17e JB |
5408 | } |
5409 | up_read(&space_info->groups_sem); | |
5410 | ||
60d2adbb MX |
5411 | if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg) |
5412 | goto search; | |
5413 | ||
b742bb82 YZ |
5414 | if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES) |
5415 | goto search; | |
5416 | ||
ccf0e725 JB |
5417 | /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for |
5418 | * for them to make caching progress. Also | |
5419 | * determine the best possible bg to cache | |
5420 | * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking | |
5421 | * caching kthreads as we move along | |
817d52f8 JB |
5422 | * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching |
5423 | * LOOP_ALLOC_CHUNK, force a chunk allocation and try again | |
5424 | * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try | |
5425 | * again | |
fa9c0d79 | 5426 | */ |
723bda20 | 5427 | if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) { |
b742bb82 | 5428 | index = 0; |
ccf0e725 | 5429 | if (loop == LOOP_FIND_IDEAL && found_uncached_bg) { |
817d52f8 | 5430 | found_uncached_bg = false; |
ccf0e725 | 5431 | loop++; |
bab39bf9 | 5432 | if (!ideal_cache_percent) |
817d52f8 | 5433 | goto search; |
ccf0e725 JB |
5434 | |
5435 | /* | |
5436 | * 1 of the following 2 things have happened so far | |
5437 | * | |
5438 | * 1) We found an ideal block group for caching that | |
5439 | * is mostly full and will cache quickly, so we might | |
5440 | * as well wait for it. | |
5441 | * | |
5442 | * 2) We searched for cached only and we didn't find | |
5443 | * anything, and we didn't start any caching kthreads | |
5444 | * either, so chances are we will loop through and | |
5445 | * start a couple caching kthreads, and then come back | |
5446 | * around and just wait for them. This will be slower | |
5447 | * because we will have 2 caching kthreads reading at | |
5448 | * the same time when we could have just started one | |
5449 | * and waited for it to get far enough to give us an | |
5450 | * allocation, so go ahead and go to the wait caching | |
5451 | * loop. | |
5452 | */ | |
5453 | loop = LOOP_CACHING_WAIT; | |
5454 | search_start = ideal_cache_offset; | |
5455 | ideal_cache_percent = 0; | |
5456 | goto ideal_cache; | |
5457 | } else if (loop == LOOP_FIND_IDEAL) { | |
5458 | /* | |
5459 | * Didn't find a uncached bg, wait on anything we find | |
5460 | * next. | |
5461 | */ | |
5462 | loop = LOOP_CACHING_WAIT; | |
5463 | goto search; | |
5464 | } | |
5465 | ||
723bda20 | 5466 | loop++; |
817d52f8 JB |
5467 | |
5468 | if (loop == LOOP_ALLOC_CHUNK) { | |
723bda20 JB |
5469 | if (allowed_chunk_alloc) { |
5470 | ret = do_chunk_alloc(trans, root, num_bytes + | |
5471 | 2 * 1024 * 1024, data, | |
5472 | CHUNK_ALLOC_LIMITED); | |
5473 | allowed_chunk_alloc = 0; | |
5474 | if (ret == 1) | |
5475 | done_chunk_alloc = 1; | |
5476 | } else if (!done_chunk_alloc && | |
5477 | space_info->force_alloc == | |
5478 | CHUNK_ALLOC_NO_FORCE) { | |
5479 | space_info->force_alloc = CHUNK_ALLOC_LIMITED; | |
5480 | } | |
2552d17e | 5481 | |
723bda20 JB |
5482 | /* |
5483 | * We didn't allocate a chunk, go ahead and drop the | |
5484 | * empty size and loop again. | |
5485 | */ | |
5486 | if (!done_chunk_alloc) | |
5487 | loop = LOOP_NO_EMPTY_SIZE; | |
2552d17e JB |
5488 | } |
5489 | ||
723bda20 JB |
5490 | if (loop == LOOP_NO_EMPTY_SIZE) { |
5491 | empty_size = 0; | |
5492 | empty_cluster = 0; | |
fa9c0d79 | 5493 | } |
723bda20 JB |
5494 | |
5495 | goto search; | |
2552d17e JB |
5496 | } else if (!ins->objectid) { |
5497 | ret = -ENOSPC; | |
d82a6f1d | 5498 | } else if (ins->objectid) { |
80eb234a | 5499 | ret = 0; |
be744175 | 5500 | } |
be744175 | 5501 | |
0f70abe2 | 5502 | return ret; |
fec577fb | 5503 | } |
ec44a35c | 5504 | |
9ed74f2d JB |
5505 | static void dump_space_info(struct btrfs_space_info *info, u64 bytes, |
5506 | int dump_block_groups) | |
0f9dd46c JB |
5507 | { |
5508 | struct btrfs_block_group_cache *cache; | |
b742bb82 | 5509 | int index = 0; |
0f9dd46c | 5510 | |
9ed74f2d | 5511 | spin_lock(&info->lock); |
fb25e914 JB |
5512 | printk(KERN_INFO "space_info %llu has %llu free, is %sfull\n", |
5513 | (unsigned long long)info->flags, | |
d397712b | 5514 | (unsigned long long)(info->total_bytes - info->bytes_used - |
9ed74f2d | 5515 | info->bytes_pinned - info->bytes_reserved - |
8929ecfa | 5516 | info->bytes_readonly), |
d397712b | 5517 | (info->full) ? "" : "not "); |
8929ecfa YZ |
5518 | printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, " |
5519 | "reserved=%llu, may_use=%llu, readonly=%llu\n", | |
21380931 | 5520 | (unsigned long long)info->total_bytes, |
8929ecfa | 5521 | (unsigned long long)info->bytes_used, |
21380931 | 5522 | (unsigned long long)info->bytes_pinned, |
8929ecfa | 5523 | (unsigned long long)info->bytes_reserved, |
21380931 | 5524 | (unsigned long long)info->bytes_may_use, |
8929ecfa | 5525 | (unsigned long long)info->bytes_readonly); |
9ed74f2d JB |
5526 | spin_unlock(&info->lock); |
5527 | ||
5528 | if (!dump_block_groups) | |
5529 | return; | |
0f9dd46c | 5530 | |
80eb234a | 5531 | down_read(&info->groups_sem); |
b742bb82 YZ |
5532 | again: |
5533 | list_for_each_entry(cache, &info->block_groups[index], list) { | |
0f9dd46c | 5534 | spin_lock(&cache->lock); |
d397712b CM |
5535 | printk(KERN_INFO "block group %llu has %llu bytes, %llu used " |
5536 | "%llu pinned %llu reserved\n", | |
5537 | (unsigned long long)cache->key.objectid, | |
5538 | (unsigned long long)cache->key.offset, | |
5539 | (unsigned long long)btrfs_block_group_used(&cache->item), | |
5540 | (unsigned long long)cache->pinned, | |
5541 | (unsigned long long)cache->reserved); | |
0f9dd46c JB |
5542 | btrfs_dump_free_space(cache, bytes); |
5543 | spin_unlock(&cache->lock); | |
5544 | } | |
b742bb82 YZ |
5545 | if (++index < BTRFS_NR_RAID_TYPES) |
5546 | goto again; | |
80eb234a | 5547 | up_read(&info->groups_sem); |
0f9dd46c | 5548 | } |
e8569813 | 5549 | |
11833d66 YZ |
5550 | int btrfs_reserve_extent(struct btrfs_trans_handle *trans, |
5551 | struct btrfs_root *root, | |
5552 | u64 num_bytes, u64 min_alloc_size, | |
5553 | u64 empty_size, u64 hint_byte, | |
5554 | u64 search_end, struct btrfs_key *ins, | |
5555 | u64 data) | |
fec577fb CM |
5556 | { |
5557 | int ret; | |
fbdc762b | 5558 | u64 search_start = 0; |
925baedd | 5559 | |
6a63209f | 5560 | data = btrfs_get_alloc_profile(root, data); |
98d20f67 | 5561 | again: |
0ef3e66b CM |
5562 | /* |
5563 | * the only place that sets empty_size is btrfs_realloc_node, which | |
5564 | * is not called recursively on allocations | |
5565 | */ | |
83d3c969 | 5566 | if (empty_size || root->ref_cows) |
6324fbf3 | 5567 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
0e4f8f88 CM |
5568 | num_bytes + 2 * 1024 * 1024, data, |
5569 | CHUNK_ALLOC_NO_FORCE); | |
0b86a832 | 5570 | |
db94535d CM |
5571 | WARN_ON(num_bytes < root->sectorsize); |
5572 | ret = find_free_extent(trans, root, num_bytes, empty_size, | |
f0486c68 YZ |
5573 | search_start, search_end, hint_byte, |
5574 | ins, data); | |
3b951516 | 5575 | |
98d20f67 CM |
5576 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
5577 | num_bytes = num_bytes >> 1; | |
0f9dd46c | 5578 | num_bytes = num_bytes & ~(root->sectorsize - 1); |
98d20f67 | 5579 | num_bytes = max(num_bytes, min_alloc_size); |
0ef3e66b | 5580 | do_chunk_alloc(trans, root->fs_info->extent_root, |
0e4f8f88 | 5581 | num_bytes, data, CHUNK_ALLOC_FORCE); |
98d20f67 CM |
5582 | goto again; |
5583 | } | |
91435650 | 5584 | if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) { |
0f9dd46c JB |
5585 | struct btrfs_space_info *sinfo; |
5586 | ||
5587 | sinfo = __find_space_info(root->fs_info, data); | |
d397712b CM |
5588 | printk(KERN_ERR "btrfs allocation failed flags %llu, " |
5589 | "wanted %llu\n", (unsigned long long)data, | |
5590 | (unsigned long long)num_bytes); | |
9ed74f2d | 5591 | dump_space_info(sinfo, num_bytes, 1); |
925baedd | 5592 | } |
0f9dd46c | 5593 | |
1abe9b8a | 5594 | trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset); |
5595 | ||
0f9dd46c | 5596 | return ret; |
e6dcd2dc CM |
5597 | } |
5598 | ||
e688b725 CM |
5599 | static int __btrfs_free_reserved_extent(struct btrfs_root *root, |
5600 | u64 start, u64 len, int pin) | |
65b51a00 | 5601 | { |
0f9dd46c | 5602 | struct btrfs_block_group_cache *cache; |
1f3c79a2 | 5603 | int ret = 0; |
0f9dd46c | 5604 | |
0f9dd46c JB |
5605 | cache = btrfs_lookup_block_group(root->fs_info, start); |
5606 | if (!cache) { | |
d397712b CM |
5607 | printk(KERN_ERR "Unable to find block group for %llu\n", |
5608 | (unsigned long long)start); | |
0f9dd46c JB |
5609 | return -ENOSPC; |
5610 | } | |
1f3c79a2 | 5611 | |
5378e607 LD |
5612 | if (btrfs_test_opt(root, DISCARD)) |
5613 | ret = btrfs_discard_extent(root, start, len, NULL); | |
1f3c79a2 | 5614 | |
e688b725 CM |
5615 | if (pin) |
5616 | pin_down_extent(root, cache, start, len, 1); | |
5617 | else { | |
5618 | btrfs_add_free_space(cache, start, len); | |
5619 | btrfs_update_reserved_bytes(cache, len, RESERVE_FREE); | |
5620 | } | |
fa9c0d79 | 5621 | btrfs_put_block_group(cache); |
817d52f8 | 5622 | |
1abe9b8a | 5623 | trace_btrfs_reserved_extent_free(root, start, len); |
5624 | ||
e6dcd2dc CM |
5625 | return ret; |
5626 | } | |
5627 | ||
e688b725 CM |
5628 | int btrfs_free_reserved_extent(struct btrfs_root *root, |
5629 | u64 start, u64 len) | |
5630 | { | |
5631 | return __btrfs_free_reserved_extent(root, start, len, 0); | |
5632 | } | |
5633 | ||
5634 | int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root, | |
5635 | u64 start, u64 len) | |
5636 | { | |
5637 | return __btrfs_free_reserved_extent(root, start, len, 1); | |
5638 | } | |
5639 | ||
5d4f98a2 YZ |
5640 | static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans, |
5641 | struct btrfs_root *root, | |
5642 | u64 parent, u64 root_objectid, | |
5643 | u64 flags, u64 owner, u64 offset, | |
5644 | struct btrfs_key *ins, int ref_mod) | |
e6dcd2dc CM |
5645 | { |
5646 | int ret; | |
5d4f98a2 | 5647 | struct btrfs_fs_info *fs_info = root->fs_info; |
e6dcd2dc | 5648 | struct btrfs_extent_item *extent_item; |
5d4f98a2 | 5649 | struct btrfs_extent_inline_ref *iref; |
e6dcd2dc | 5650 | struct btrfs_path *path; |
5d4f98a2 YZ |
5651 | struct extent_buffer *leaf; |
5652 | int type; | |
5653 | u32 size; | |
26b8003f | 5654 | |
5d4f98a2 YZ |
5655 | if (parent > 0) |
5656 | type = BTRFS_SHARED_DATA_REF_KEY; | |
5657 | else | |
5658 | type = BTRFS_EXTENT_DATA_REF_KEY; | |
58176a96 | 5659 | |
5d4f98a2 | 5660 | size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type); |
7bb86316 CM |
5661 | |
5662 | path = btrfs_alloc_path(); | |
db5b493a TI |
5663 | if (!path) |
5664 | return -ENOMEM; | |
47e4bb98 | 5665 | |
b9473439 | 5666 | path->leave_spinning = 1; |
5d4f98a2 YZ |
5667 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, |
5668 | ins, size); | |
ccd467d6 | 5669 | BUG_ON(ret); |
0f9dd46c | 5670 | |
5d4f98a2 YZ |
5671 | leaf = path->nodes[0]; |
5672 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
47e4bb98 | 5673 | struct btrfs_extent_item); |
5d4f98a2 YZ |
5674 | btrfs_set_extent_refs(leaf, extent_item, ref_mod); |
5675 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
5676 | btrfs_set_extent_flags(leaf, extent_item, | |
5677 | flags | BTRFS_EXTENT_FLAG_DATA); | |
5678 | ||
5679 | iref = (struct btrfs_extent_inline_ref *)(extent_item + 1); | |
5680 | btrfs_set_extent_inline_ref_type(leaf, iref, type); | |
5681 | if (parent > 0) { | |
5682 | struct btrfs_shared_data_ref *ref; | |
5683 | ref = (struct btrfs_shared_data_ref *)(iref + 1); | |
5684 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
5685 | btrfs_set_shared_data_ref_count(leaf, ref, ref_mod); | |
5686 | } else { | |
5687 | struct btrfs_extent_data_ref *ref; | |
5688 | ref = (struct btrfs_extent_data_ref *)(&iref->offset); | |
5689 | btrfs_set_extent_data_ref_root(leaf, ref, root_objectid); | |
5690 | btrfs_set_extent_data_ref_objectid(leaf, ref, owner); | |
5691 | btrfs_set_extent_data_ref_offset(leaf, ref, offset); | |
5692 | btrfs_set_extent_data_ref_count(leaf, ref, ref_mod); | |
5693 | } | |
47e4bb98 CM |
5694 | |
5695 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
7bb86316 | 5696 | btrfs_free_path(path); |
f510cfec | 5697 | |
f0486c68 | 5698 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
f5947066 | 5699 | if (ret) { |
d397712b CM |
5700 | printk(KERN_ERR "btrfs update block group failed for %llu " |
5701 | "%llu\n", (unsigned long long)ins->objectid, | |
5702 | (unsigned long long)ins->offset); | |
f5947066 CM |
5703 | BUG(); |
5704 | } | |
e6dcd2dc CM |
5705 | return ret; |
5706 | } | |
5707 | ||
5d4f98a2 YZ |
5708 | static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans, |
5709 | struct btrfs_root *root, | |
5710 | u64 parent, u64 root_objectid, | |
5711 | u64 flags, struct btrfs_disk_key *key, | |
5712 | int level, struct btrfs_key *ins) | |
e6dcd2dc CM |
5713 | { |
5714 | int ret; | |
5d4f98a2 YZ |
5715 | struct btrfs_fs_info *fs_info = root->fs_info; |
5716 | struct btrfs_extent_item *extent_item; | |
5717 | struct btrfs_tree_block_info *block_info; | |
5718 | struct btrfs_extent_inline_ref *iref; | |
5719 | struct btrfs_path *path; | |
5720 | struct extent_buffer *leaf; | |
5721 | u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref); | |
1c2308f8 | 5722 | |
5d4f98a2 | 5723 | path = btrfs_alloc_path(); |
d8926bb3 MF |
5724 | if (!path) |
5725 | return -ENOMEM; | |
56bec294 | 5726 | |
5d4f98a2 YZ |
5727 | path->leave_spinning = 1; |
5728 | ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path, | |
5729 | ins, size); | |
56bec294 | 5730 | BUG_ON(ret); |
5d4f98a2 YZ |
5731 | |
5732 | leaf = path->nodes[0]; | |
5733 | extent_item = btrfs_item_ptr(leaf, path->slots[0], | |
5734 | struct btrfs_extent_item); | |
5735 | btrfs_set_extent_refs(leaf, extent_item, 1); | |
5736 | btrfs_set_extent_generation(leaf, extent_item, trans->transid); | |
5737 | btrfs_set_extent_flags(leaf, extent_item, | |
5738 | flags | BTRFS_EXTENT_FLAG_TREE_BLOCK); | |
5739 | block_info = (struct btrfs_tree_block_info *)(extent_item + 1); | |
5740 | ||
5741 | btrfs_set_tree_block_key(leaf, block_info, key); | |
5742 | btrfs_set_tree_block_level(leaf, block_info, level); | |
5743 | ||
5744 | iref = (struct btrfs_extent_inline_ref *)(block_info + 1); | |
5745 | if (parent > 0) { | |
5746 | BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)); | |
5747 | btrfs_set_extent_inline_ref_type(leaf, iref, | |
5748 | BTRFS_SHARED_BLOCK_REF_KEY); | |
5749 | btrfs_set_extent_inline_ref_offset(leaf, iref, parent); | |
5750 | } else { | |
5751 | btrfs_set_extent_inline_ref_type(leaf, iref, | |
5752 | BTRFS_TREE_BLOCK_REF_KEY); | |
5753 | btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid); | |
5754 | } | |
5755 | ||
5756 | btrfs_mark_buffer_dirty(leaf); | |
5757 | btrfs_free_path(path); | |
5758 | ||
f0486c68 | 5759 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1); |
5d4f98a2 YZ |
5760 | if (ret) { |
5761 | printk(KERN_ERR "btrfs update block group failed for %llu " | |
5762 | "%llu\n", (unsigned long long)ins->objectid, | |
5763 | (unsigned long long)ins->offset); | |
5764 | BUG(); | |
5765 | } | |
5766 | return ret; | |
5767 | } | |
5768 | ||
5769 | int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans, | |
5770 | struct btrfs_root *root, | |
5771 | u64 root_objectid, u64 owner, | |
5772 | u64 offset, struct btrfs_key *ins) | |
5773 | { | |
5774 | int ret; | |
5775 | ||
5776 | BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID); | |
5777 | ||
5778 | ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset, | |
5779 | 0, root_objectid, owner, offset, | |
5780 | BTRFS_ADD_DELAYED_EXTENT, NULL); | |
e6dcd2dc CM |
5781 | return ret; |
5782 | } | |
e02119d5 CM |
5783 | |
5784 | /* | |
5785 | * this is used by the tree logging recovery code. It records that | |
5786 | * an extent has been allocated and makes sure to clear the free | |
5787 | * space cache bits as well | |
5788 | */ | |
5d4f98a2 YZ |
5789 | int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans, |
5790 | struct btrfs_root *root, | |
5791 | u64 root_objectid, u64 owner, u64 offset, | |
5792 | struct btrfs_key *ins) | |
e02119d5 CM |
5793 | { |
5794 | int ret; | |
5795 | struct btrfs_block_group_cache *block_group; | |
11833d66 YZ |
5796 | struct btrfs_caching_control *caching_ctl; |
5797 | u64 start = ins->objectid; | |
5798 | u64 num_bytes = ins->offset; | |
e02119d5 | 5799 | |
e02119d5 | 5800 | block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid); |
b8399dee | 5801 | cache_block_group(block_group, trans, NULL, 0); |
11833d66 | 5802 | caching_ctl = get_caching_control(block_group); |
e02119d5 | 5803 | |
11833d66 YZ |
5804 | if (!caching_ctl) { |
5805 | BUG_ON(!block_group_cache_done(block_group)); | |
5806 | ret = btrfs_remove_free_space(block_group, start, num_bytes); | |
5807 | BUG_ON(ret); | |
5808 | } else { | |
5809 | mutex_lock(&caching_ctl->mutex); | |
5810 | ||
5811 | if (start >= caching_ctl->progress) { | |
5812 | ret = add_excluded_extent(root, start, num_bytes); | |
5813 | BUG_ON(ret); | |
5814 | } else if (start + num_bytes <= caching_ctl->progress) { | |
5815 | ret = btrfs_remove_free_space(block_group, | |
5816 | start, num_bytes); | |
5817 | BUG_ON(ret); | |
5818 | } else { | |
5819 | num_bytes = caching_ctl->progress - start; | |
5820 | ret = btrfs_remove_free_space(block_group, | |
5821 | start, num_bytes); | |
5822 | BUG_ON(ret); | |
5823 | ||
5824 | start = caching_ctl->progress; | |
5825 | num_bytes = ins->objectid + ins->offset - | |
5826 | caching_ctl->progress; | |
5827 | ret = add_excluded_extent(root, start, num_bytes); | |
5828 | BUG_ON(ret); | |
5829 | } | |
5830 | ||
5831 | mutex_unlock(&caching_ctl->mutex); | |
5832 | put_caching_control(caching_ctl); | |
5833 | } | |
5834 | ||
fb25e914 JB |
5835 | ret = btrfs_update_reserved_bytes(block_group, ins->offset, |
5836 | RESERVE_ALLOC_NO_ACCOUNT); | |
f0486c68 | 5837 | BUG_ON(ret); |
fa9c0d79 | 5838 | btrfs_put_block_group(block_group); |
5d4f98a2 YZ |
5839 | ret = alloc_reserved_file_extent(trans, root, 0, root_objectid, |
5840 | 0, owner, offset, ins, 1); | |
e02119d5 CM |
5841 | return ret; |
5842 | } | |
5843 | ||
65b51a00 CM |
5844 | struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans, |
5845 | struct btrfs_root *root, | |
4008c04a CM |
5846 | u64 bytenr, u32 blocksize, |
5847 | int level) | |
65b51a00 CM |
5848 | { |
5849 | struct extent_buffer *buf; | |
5850 | ||
5851 | buf = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
5852 | if (!buf) | |
5853 | return ERR_PTR(-ENOMEM); | |
5854 | btrfs_set_header_generation(buf, trans->transid); | |
85d4e461 | 5855 | btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level); |
65b51a00 CM |
5856 | btrfs_tree_lock(buf); |
5857 | clean_tree_block(trans, root, buf); | |
b4ce94de CM |
5858 | |
5859 | btrfs_set_lock_blocking(buf); | |
65b51a00 | 5860 | btrfs_set_buffer_uptodate(buf); |
b4ce94de | 5861 | |
d0c803c4 | 5862 | if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) { |
8cef4e16 YZ |
5863 | /* |
5864 | * we allow two log transactions at a time, use different | |
5865 | * EXENT bit to differentiate dirty pages. | |
5866 | */ | |
5867 | if (root->log_transid % 2 == 0) | |
5868 | set_extent_dirty(&root->dirty_log_pages, buf->start, | |
5869 | buf->start + buf->len - 1, GFP_NOFS); | |
5870 | else | |
5871 | set_extent_new(&root->dirty_log_pages, buf->start, | |
5872 | buf->start + buf->len - 1, GFP_NOFS); | |
d0c803c4 CM |
5873 | } else { |
5874 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, | |
65b51a00 | 5875 | buf->start + buf->len - 1, GFP_NOFS); |
d0c803c4 | 5876 | } |
65b51a00 | 5877 | trans->blocks_used++; |
b4ce94de | 5878 | /* this returns a buffer locked for blocking */ |
65b51a00 CM |
5879 | return buf; |
5880 | } | |
5881 | ||
f0486c68 YZ |
5882 | static struct btrfs_block_rsv * |
5883 | use_block_rsv(struct btrfs_trans_handle *trans, | |
5884 | struct btrfs_root *root, u32 blocksize) | |
5885 | { | |
5886 | struct btrfs_block_rsv *block_rsv; | |
68a82277 | 5887 | struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv; |
f0486c68 YZ |
5888 | int ret; |
5889 | ||
5890 | block_rsv = get_block_rsv(trans, root); | |
5891 | ||
5892 | if (block_rsv->size == 0) { | |
36ba022a | 5893 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
68a82277 JB |
5894 | /* |
5895 | * If we couldn't reserve metadata bytes try and use some from | |
5896 | * the global reserve. | |
5897 | */ | |
5898 | if (ret && block_rsv != global_rsv) { | |
5899 | ret = block_rsv_use_bytes(global_rsv, blocksize); | |
5900 | if (!ret) | |
5901 | return global_rsv; | |
f0486c68 | 5902 | return ERR_PTR(ret); |
68a82277 | 5903 | } else if (ret) { |
f0486c68 | 5904 | return ERR_PTR(ret); |
68a82277 | 5905 | } |
f0486c68 YZ |
5906 | return block_rsv; |
5907 | } | |
5908 | ||
5909 | ret = block_rsv_use_bytes(block_rsv, blocksize); | |
5910 | if (!ret) | |
5911 | return block_rsv; | |
68a82277 | 5912 | if (ret) { |
dff51cd1 DS |
5913 | static DEFINE_RATELIMIT_STATE(_rs, |
5914 | DEFAULT_RATELIMIT_INTERVAL, | |
5915 | /*DEFAULT_RATELIMIT_BURST*/ 2); | |
5916 | if (__ratelimit(&_rs)) { | |
5917 | printk(KERN_DEBUG "btrfs: block rsv returned %d\n", ret); | |
5918 | WARN_ON(1); | |
5919 | } | |
36ba022a | 5920 | ret = reserve_metadata_bytes(root, block_rsv, blocksize, 0); |
68a82277 | 5921 | if (!ret) { |
68a82277 JB |
5922 | return block_rsv; |
5923 | } else if (ret && block_rsv != global_rsv) { | |
5924 | ret = block_rsv_use_bytes(global_rsv, blocksize); | |
5925 | if (!ret) | |
5926 | return global_rsv; | |
5927 | } | |
5928 | } | |
f0486c68 | 5929 | |
f0486c68 YZ |
5930 | return ERR_PTR(-ENOSPC); |
5931 | } | |
5932 | ||
5933 | static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize) | |
5934 | { | |
5935 | block_rsv_add_bytes(block_rsv, blocksize, 0); | |
5936 | block_rsv_release_bytes(block_rsv, NULL, 0); | |
5937 | } | |
5938 | ||
fec577fb | 5939 | /* |
f0486c68 YZ |
5940 | * finds a free extent and does all the dirty work required for allocation |
5941 | * returns the key for the extent through ins, and a tree buffer for | |
5942 | * the first block of the extent through buf. | |
5943 | * | |
fec577fb CM |
5944 | * returns the tree buffer or NULL. |
5945 | */ | |
5f39d397 | 5946 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
5d4f98a2 YZ |
5947 | struct btrfs_root *root, u32 blocksize, |
5948 | u64 parent, u64 root_objectid, | |
5949 | struct btrfs_disk_key *key, int level, | |
5950 | u64 hint, u64 empty_size) | |
fec577fb | 5951 | { |
e2fa7227 | 5952 | struct btrfs_key ins; |
f0486c68 | 5953 | struct btrfs_block_rsv *block_rsv; |
5f39d397 | 5954 | struct extent_buffer *buf; |
f0486c68 YZ |
5955 | u64 flags = 0; |
5956 | int ret; | |
5957 | ||
fec577fb | 5958 | |
f0486c68 YZ |
5959 | block_rsv = use_block_rsv(trans, root, blocksize); |
5960 | if (IS_ERR(block_rsv)) | |
5961 | return ERR_CAST(block_rsv); | |
5962 | ||
5963 | ret = btrfs_reserve_extent(trans, root, blocksize, blocksize, | |
5964 | empty_size, hint, (u64)-1, &ins, 0); | |
fec577fb | 5965 | if (ret) { |
f0486c68 | 5966 | unuse_block_rsv(block_rsv, blocksize); |
54aa1f4d | 5967 | return ERR_PTR(ret); |
fec577fb | 5968 | } |
55c69072 | 5969 | |
4008c04a CM |
5970 | buf = btrfs_init_new_buffer(trans, root, ins.objectid, |
5971 | blocksize, level); | |
f0486c68 YZ |
5972 | BUG_ON(IS_ERR(buf)); |
5973 | ||
5974 | if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) { | |
5975 | if (parent == 0) | |
5976 | parent = ins.objectid; | |
5977 | flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
5978 | } else | |
5979 | BUG_ON(parent > 0); | |
5980 | ||
5981 | if (root_objectid != BTRFS_TREE_LOG_OBJECTID) { | |
5982 | struct btrfs_delayed_extent_op *extent_op; | |
5983 | extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS); | |
5984 | BUG_ON(!extent_op); | |
5985 | if (key) | |
5986 | memcpy(&extent_op->key, key, sizeof(extent_op->key)); | |
5987 | else | |
5988 | memset(&extent_op->key, 0, sizeof(extent_op->key)); | |
5989 | extent_op->flags_to_set = flags; | |
5990 | extent_op->update_key = 1; | |
5991 | extent_op->update_flags = 1; | |
5992 | extent_op->is_data = 0; | |
5993 | ||
5994 | ret = btrfs_add_delayed_tree_ref(trans, ins.objectid, | |
5995 | ins.offset, parent, root_objectid, | |
5996 | level, BTRFS_ADD_DELAYED_EXTENT, | |
5997 | extent_op); | |
5998 | BUG_ON(ret); | |
5999 | } | |
fec577fb CM |
6000 | return buf; |
6001 | } | |
a28ec197 | 6002 | |
2c47e605 YZ |
6003 | struct walk_control { |
6004 | u64 refs[BTRFS_MAX_LEVEL]; | |
6005 | u64 flags[BTRFS_MAX_LEVEL]; | |
6006 | struct btrfs_key update_progress; | |
6007 | int stage; | |
6008 | int level; | |
6009 | int shared_level; | |
6010 | int update_ref; | |
6011 | int keep_locks; | |
1c4850e2 YZ |
6012 | int reada_slot; |
6013 | int reada_count; | |
2c47e605 YZ |
6014 | }; |
6015 | ||
6016 | #define DROP_REFERENCE 1 | |
6017 | #define UPDATE_BACKREF 2 | |
6018 | ||
1c4850e2 YZ |
6019 | static noinline void reada_walk_down(struct btrfs_trans_handle *trans, |
6020 | struct btrfs_root *root, | |
6021 | struct walk_control *wc, | |
6022 | struct btrfs_path *path) | |
6407bf6d | 6023 | { |
1c4850e2 YZ |
6024 | u64 bytenr; |
6025 | u64 generation; | |
6026 | u64 refs; | |
94fcca9f | 6027 | u64 flags; |
5d4f98a2 | 6028 | u32 nritems; |
1c4850e2 YZ |
6029 | u32 blocksize; |
6030 | struct btrfs_key key; | |
6031 | struct extent_buffer *eb; | |
6407bf6d | 6032 | int ret; |
1c4850e2 YZ |
6033 | int slot; |
6034 | int nread = 0; | |
6407bf6d | 6035 | |
1c4850e2 YZ |
6036 | if (path->slots[wc->level] < wc->reada_slot) { |
6037 | wc->reada_count = wc->reada_count * 2 / 3; | |
6038 | wc->reada_count = max(wc->reada_count, 2); | |
6039 | } else { | |
6040 | wc->reada_count = wc->reada_count * 3 / 2; | |
6041 | wc->reada_count = min_t(int, wc->reada_count, | |
6042 | BTRFS_NODEPTRS_PER_BLOCK(root)); | |
6043 | } | |
7bb86316 | 6044 | |
1c4850e2 YZ |
6045 | eb = path->nodes[wc->level]; |
6046 | nritems = btrfs_header_nritems(eb); | |
6047 | blocksize = btrfs_level_size(root, wc->level - 1); | |
bd56b302 | 6048 | |
1c4850e2 YZ |
6049 | for (slot = path->slots[wc->level]; slot < nritems; slot++) { |
6050 | if (nread >= wc->reada_count) | |
6051 | break; | |
bd56b302 | 6052 | |
2dd3e67b | 6053 | cond_resched(); |
1c4850e2 YZ |
6054 | bytenr = btrfs_node_blockptr(eb, slot); |
6055 | generation = btrfs_node_ptr_generation(eb, slot); | |
2dd3e67b | 6056 | |
1c4850e2 YZ |
6057 | if (slot == path->slots[wc->level]) |
6058 | goto reada; | |
5d4f98a2 | 6059 | |
1c4850e2 YZ |
6060 | if (wc->stage == UPDATE_BACKREF && |
6061 | generation <= root->root_key.offset) | |
bd56b302 CM |
6062 | continue; |
6063 | ||
94fcca9f YZ |
6064 | /* We don't lock the tree block, it's OK to be racy here */ |
6065 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, | |
6066 | &refs, &flags); | |
6067 | BUG_ON(ret); | |
6068 | BUG_ON(refs == 0); | |
6069 | ||
1c4850e2 | 6070 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 YZ |
6071 | if (refs == 1) |
6072 | goto reada; | |
bd56b302 | 6073 | |
94fcca9f YZ |
6074 | if (wc->level == 1 && |
6075 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
6076 | continue; | |
1c4850e2 YZ |
6077 | if (!wc->update_ref || |
6078 | generation <= root->root_key.offset) | |
6079 | continue; | |
6080 | btrfs_node_key_to_cpu(eb, &key, slot); | |
6081 | ret = btrfs_comp_cpu_keys(&key, | |
6082 | &wc->update_progress); | |
6083 | if (ret < 0) | |
6084 | continue; | |
94fcca9f YZ |
6085 | } else { |
6086 | if (wc->level == 1 && | |
6087 | (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
6088 | continue; | |
6407bf6d | 6089 | } |
1c4850e2 YZ |
6090 | reada: |
6091 | ret = readahead_tree_block(root, bytenr, blocksize, | |
6092 | generation); | |
6093 | if (ret) | |
bd56b302 | 6094 | break; |
1c4850e2 | 6095 | nread++; |
20524f02 | 6096 | } |
1c4850e2 | 6097 | wc->reada_slot = slot; |
20524f02 | 6098 | } |
2c47e605 | 6099 | |
f82d02d9 | 6100 | /* |
2c47e605 YZ |
6101 | * hepler to process tree block while walking down the tree. |
6102 | * | |
2c47e605 YZ |
6103 | * when wc->stage == UPDATE_BACKREF, this function updates |
6104 | * back refs for pointers in the block. | |
6105 | * | |
6106 | * NOTE: return value 1 means we should stop walking down. | |
f82d02d9 | 6107 | */ |
2c47e605 | 6108 | static noinline int walk_down_proc(struct btrfs_trans_handle *trans, |
5d4f98a2 | 6109 | struct btrfs_root *root, |
2c47e605 | 6110 | struct btrfs_path *path, |
94fcca9f | 6111 | struct walk_control *wc, int lookup_info) |
f82d02d9 | 6112 | { |
2c47e605 YZ |
6113 | int level = wc->level; |
6114 | struct extent_buffer *eb = path->nodes[level]; | |
2c47e605 | 6115 | u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF; |
f82d02d9 YZ |
6116 | int ret; |
6117 | ||
2c47e605 YZ |
6118 | if (wc->stage == UPDATE_BACKREF && |
6119 | btrfs_header_owner(eb) != root->root_key.objectid) | |
6120 | return 1; | |
f82d02d9 | 6121 | |
2c47e605 YZ |
6122 | /* |
6123 | * when reference count of tree block is 1, it won't increase | |
6124 | * again. once full backref flag is set, we never clear it. | |
6125 | */ | |
94fcca9f YZ |
6126 | if (lookup_info && |
6127 | ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) || | |
6128 | (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) { | |
2c47e605 YZ |
6129 | BUG_ON(!path->locks[level]); |
6130 | ret = btrfs_lookup_extent_info(trans, root, | |
6131 | eb->start, eb->len, | |
6132 | &wc->refs[level], | |
6133 | &wc->flags[level]); | |
6134 | BUG_ON(ret); | |
6135 | BUG_ON(wc->refs[level] == 0); | |
6136 | } | |
5d4f98a2 | 6137 | |
2c47e605 YZ |
6138 | if (wc->stage == DROP_REFERENCE) { |
6139 | if (wc->refs[level] > 1) | |
6140 | return 1; | |
f82d02d9 | 6141 | |
2c47e605 | 6142 | if (path->locks[level] && !wc->keep_locks) { |
bd681513 | 6143 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
6144 | path->locks[level] = 0; |
6145 | } | |
6146 | return 0; | |
6147 | } | |
f82d02d9 | 6148 | |
2c47e605 YZ |
6149 | /* wc->stage == UPDATE_BACKREF */ |
6150 | if (!(wc->flags[level] & flag)) { | |
6151 | BUG_ON(!path->locks[level]); | |
6152 | ret = btrfs_inc_ref(trans, root, eb, 1); | |
f82d02d9 | 6153 | BUG_ON(ret); |
2c47e605 YZ |
6154 | ret = btrfs_dec_ref(trans, root, eb, 0); |
6155 | BUG_ON(ret); | |
6156 | ret = btrfs_set_disk_extent_flags(trans, root, eb->start, | |
6157 | eb->len, flag, 0); | |
6158 | BUG_ON(ret); | |
6159 | wc->flags[level] |= flag; | |
6160 | } | |
6161 | ||
6162 | /* | |
6163 | * the block is shared by multiple trees, so it's not good to | |
6164 | * keep the tree lock | |
6165 | */ | |
6166 | if (path->locks[level] && level > 0) { | |
bd681513 | 6167 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
6168 | path->locks[level] = 0; |
6169 | } | |
6170 | return 0; | |
6171 | } | |
6172 | ||
1c4850e2 YZ |
6173 | /* |
6174 | * hepler to process tree block pointer. | |
6175 | * | |
6176 | * when wc->stage == DROP_REFERENCE, this function checks | |
6177 | * reference count of the block pointed to. if the block | |
6178 | * is shared and we need update back refs for the subtree | |
6179 | * rooted at the block, this function changes wc->stage to | |
6180 | * UPDATE_BACKREF. if the block is shared and there is no | |
6181 | * need to update back, this function drops the reference | |
6182 | * to the block. | |
6183 | * | |
6184 | * NOTE: return value 1 means we should stop walking down. | |
6185 | */ | |
6186 | static noinline int do_walk_down(struct btrfs_trans_handle *trans, | |
6187 | struct btrfs_root *root, | |
6188 | struct btrfs_path *path, | |
94fcca9f | 6189 | struct walk_control *wc, int *lookup_info) |
1c4850e2 YZ |
6190 | { |
6191 | u64 bytenr; | |
6192 | u64 generation; | |
6193 | u64 parent; | |
6194 | u32 blocksize; | |
6195 | struct btrfs_key key; | |
6196 | struct extent_buffer *next; | |
6197 | int level = wc->level; | |
6198 | int reada = 0; | |
6199 | int ret = 0; | |
6200 | ||
6201 | generation = btrfs_node_ptr_generation(path->nodes[level], | |
6202 | path->slots[level]); | |
6203 | /* | |
6204 | * if the lower level block was created before the snapshot | |
6205 | * was created, we know there is no need to update back refs | |
6206 | * for the subtree | |
6207 | */ | |
6208 | if (wc->stage == UPDATE_BACKREF && | |
94fcca9f YZ |
6209 | generation <= root->root_key.offset) { |
6210 | *lookup_info = 1; | |
1c4850e2 | 6211 | return 1; |
94fcca9f | 6212 | } |
1c4850e2 YZ |
6213 | |
6214 | bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]); | |
6215 | blocksize = btrfs_level_size(root, level - 1); | |
6216 | ||
6217 | next = btrfs_find_tree_block(root, bytenr, blocksize); | |
6218 | if (!next) { | |
6219 | next = btrfs_find_create_tree_block(root, bytenr, blocksize); | |
90d2c51d MX |
6220 | if (!next) |
6221 | return -ENOMEM; | |
1c4850e2 YZ |
6222 | reada = 1; |
6223 | } | |
6224 | btrfs_tree_lock(next); | |
6225 | btrfs_set_lock_blocking(next); | |
6226 | ||
94fcca9f YZ |
6227 | ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize, |
6228 | &wc->refs[level - 1], | |
6229 | &wc->flags[level - 1]); | |
6230 | BUG_ON(ret); | |
6231 | BUG_ON(wc->refs[level - 1] == 0); | |
6232 | *lookup_info = 0; | |
1c4850e2 | 6233 | |
94fcca9f | 6234 | if (wc->stage == DROP_REFERENCE) { |
1c4850e2 | 6235 | if (wc->refs[level - 1] > 1) { |
94fcca9f YZ |
6236 | if (level == 1 && |
6237 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
6238 | goto skip; | |
6239 | ||
1c4850e2 YZ |
6240 | if (!wc->update_ref || |
6241 | generation <= root->root_key.offset) | |
6242 | goto skip; | |
6243 | ||
6244 | btrfs_node_key_to_cpu(path->nodes[level], &key, | |
6245 | path->slots[level]); | |
6246 | ret = btrfs_comp_cpu_keys(&key, &wc->update_progress); | |
6247 | if (ret < 0) | |
6248 | goto skip; | |
6249 | ||
6250 | wc->stage = UPDATE_BACKREF; | |
6251 | wc->shared_level = level - 1; | |
6252 | } | |
94fcca9f YZ |
6253 | } else { |
6254 | if (level == 1 && | |
6255 | (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF)) | |
6256 | goto skip; | |
1c4850e2 YZ |
6257 | } |
6258 | ||
6259 | if (!btrfs_buffer_uptodate(next, generation)) { | |
6260 | btrfs_tree_unlock(next); | |
6261 | free_extent_buffer(next); | |
6262 | next = NULL; | |
94fcca9f | 6263 | *lookup_info = 1; |
1c4850e2 YZ |
6264 | } |
6265 | ||
6266 | if (!next) { | |
6267 | if (reada && level == 1) | |
6268 | reada_walk_down(trans, root, wc, path); | |
6269 | next = read_tree_block(root, bytenr, blocksize, generation); | |
97d9a8a4 TI |
6270 | if (!next) |
6271 | return -EIO; | |
1c4850e2 YZ |
6272 | btrfs_tree_lock(next); |
6273 | btrfs_set_lock_blocking(next); | |
6274 | } | |
6275 | ||
6276 | level--; | |
6277 | BUG_ON(level != btrfs_header_level(next)); | |
6278 | path->nodes[level] = next; | |
6279 | path->slots[level] = 0; | |
bd681513 | 6280 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
1c4850e2 YZ |
6281 | wc->level = level; |
6282 | if (wc->level == 1) | |
6283 | wc->reada_slot = 0; | |
6284 | return 0; | |
6285 | skip: | |
6286 | wc->refs[level - 1] = 0; | |
6287 | wc->flags[level - 1] = 0; | |
94fcca9f YZ |
6288 | if (wc->stage == DROP_REFERENCE) { |
6289 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) { | |
6290 | parent = path->nodes[level]->start; | |
6291 | } else { | |
6292 | BUG_ON(root->root_key.objectid != | |
6293 | btrfs_header_owner(path->nodes[level])); | |
6294 | parent = 0; | |
6295 | } | |
1c4850e2 | 6296 | |
94fcca9f YZ |
6297 | ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent, |
6298 | root->root_key.objectid, level - 1, 0); | |
6299 | BUG_ON(ret); | |
1c4850e2 | 6300 | } |
1c4850e2 YZ |
6301 | btrfs_tree_unlock(next); |
6302 | free_extent_buffer(next); | |
94fcca9f | 6303 | *lookup_info = 1; |
1c4850e2 YZ |
6304 | return 1; |
6305 | } | |
6306 | ||
2c47e605 YZ |
6307 | /* |
6308 | * hepler to process tree block while walking up the tree. | |
6309 | * | |
6310 | * when wc->stage == DROP_REFERENCE, this function drops | |
6311 | * reference count on the block. | |
6312 | * | |
6313 | * when wc->stage == UPDATE_BACKREF, this function changes | |
6314 | * wc->stage back to DROP_REFERENCE if we changed wc->stage | |
6315 | * to UPDATE_BACKREF previously while processing the block. | |
6316 | * | |
6317 | * NOTE: return value 1 means we should stop walking up. | |
6318 | */ | |
6319 | static noinline int walk_up_proc(struct btrfs_trans_handle *trans, | |
6320 | struct btrfs_root *root, | |
6321 | struct btrfs_path *path, | |
6322 | struct walk_control *wc) | |
6323 | { | |
f0486c68 | 6324 | int ret; |
2c47e605 YZ |
6325 | int level = wc->level; |
6326 | struct extent_buffer *eb = path->nodes[level]; | |
6327 | u64 parent = 0; | |
6328 | ||
6329 | if (wc->stage == UPDATE_BACKREF) { | |
6330 | BUG_ON(wc->shared_level < level); | |
6331 | if (level < wc->shared_level) | |
6332 | goto out; | |
6333 | ||
2c47e605 YZ |
6334 | ret = find_next_key(path, level + 1, &wc->update_progress); |
6335 | if (ret > 0) | |
6336 | wc->update_ref = 0; | |
6337 | ||
6338 | wc->stage = DROP_REFERENCE; | |
6339 | wc->shared_level = -1; | |
6340 | path->slots[level] = 0; | |
6341 | ||
6342 | /* | |
6343 | * check reference count again if the block isn't locked. | |
6344 | * we should start walking down the tree again if reference | |
6345 | * count is one. | |
6346 | */ | |
6347 | if (!path->locks[level]) { | |
6348 | BUG_ON(level == 0); | |
6349 | btrfs_tree_lock(eb); | |
6350 | btrfs_set_lock_blocking(eb); | |
bd681513 | 6351 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
6352 | |
6353 | ret = btrfs_lookup_extent_info(trans, root, | |
6354 | eb->start, eb->len, | |
6355 | &wc->refs[level], | |
6356 | &wc->flags[level]); | |
f82d02d9 | 6357 | BUG_ON(ret); |
2c47e605 YZ |
6358 | BUG_ON(wc->refs[level] == 0); |
6359 | if (wc->refs[level] == 1) { | |
bd681513 | 6360 | btrfs_tree_unlock_rw(eb, path->locks[level]); |
2c47e605 YZ |
6361 | return 1; |
6362 | } | |
f82d02d9 | 6363 | } |
2c47e605 | 6364 | } |
f82d02d9 | 6365 | |
2c47e605 YZ |
6366 | /* wc->stage == DROP_REFERENCE */ |
6367 | BUG_ON(wc->refs[level] > 1 && !path->locks[level]); | |
5d4f98a2 | 6368 | |
2c47e605 YZ |
6369 | if (wc->refs[level] == 1) { |
6370 | if (level == 0) { | |
6371 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
6372 | ret = btrfs_dec_ref(trans, root, eb, 1); | |
6373 | else | |
6374 | ret = btrfs_dec_ref(trans, root, eb, 0); | |
6375 | BUG_ON(ret); | |
6376 | } | |
6377 | /* make block locked assertion in clean_tree_block happy */ | |
6378 | if (!path->locks[level] && | |
6379 | btrfs_header_generation(eb) == trans->transid) { | |
6380 | btrfs_tree_lock(eb); | |
6381 | btrfs_set_lock_blocking(eb); | |
bd681513 | 6382 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
6383 | } |
6384 | clean_tree_block(trans, root, eb); | |
6385 | } | |
6386 | ||
6387 | if (eb == root->node) { | |
6388 | if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
6389 | parent = eb->start; | |
6390 | else | |
6391 | BUG_ON(root->root_key.objectid != | |
6392 | btrfs_header_owner(eb)); | |
6393 | } else { | |
6394 | if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF) | |
6395 | parent = path->nodes[level + 1]->start; | |
6396 | else | |
6397 | BUG_ON(root->root_key.objectid != | |
6398 | btrfs_header_owner(path->nodes[level + 1])); | |
f82d02d9 | 6399 | } |
f82d02d9 | 6400 | |
f0486c68 | 6401 | btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1); |
2c47e605 YZ |
6402 | out: |
6403 | wc->refs[level] = 0; | |
6404 | wc->flags[level] = 0; | |
f0486c68 | 6405 | return 0; |
2c47e605 YZ |
6406 | } |
6407 | ||
6408 | static noinline int walk_down_tree(struct btrfs_trans_handle *trans, | |
6409 | struct btrfs_root *root, | |
6410 | struct btrfs_path *path, | |
6411 | struct walk_control *wc) | |
6412 | { | |
2c47e605 | 6413 | int level = wc->level; |
94fcca9f | 6414 | int lookup_info = 1; |
2c47e605 YZ |
6415 | int ret; |
6416 | ||
6417 | while (level >= 0) { | |
94fcca9f | 6418 | ret = walk_down_proc(trans, root, path, wc, lookup_info); |
2c47e605 YZ |
6419 | if (ret > 0) |
6420 | break; | |
6421 | ||
6422 | if (level == 0) | |
6423 | break; | |
6424 | ||
7a7965f8 YZ |
6425 | if (path->slots[level] >= |
6426 | btrfs_header_nritems(path->nodes[level])) | |
6427 | break; | |
6428 | ||
94fcca9f | 6429 | ret = do_walk_down(trans, root, path, wc, &lookup_info); |
1c4850e2 YZ |
6430 | if (ret > 0) { |
6431 | path->slots[level]++; | |
6432 | continue; | |
90d2c51d MX |
6433 | } else if (ret < 0) |
6434 | return ret; | |
1c4850e2 | 6435 | level = wc->level; |
f82d02d9 | 6436 | } |
f82d02d9 YZ |
6437 | return 0; |
6438 | } | |
6439 | ||
d397712b | 6440 | static noinline int walk_up_tree(struct btrfs_trans_handle *trans, |
98ed5174 | 6441 | struct btrfs_root *root, |
f82d02d9 | 6442 | struct btrfs_path *path, |
2c47e605 | 6443 | struct walk_control *wc, int max_level) |
20524f02 | 6444 | { |
2c47e605 | 6445 | int level = wc->level; |
20524f02 | 6446 | int ret; |
9f3a7427 | 6447 | |
2c47e605 YZ |
6448 | path->slots[level] = btrfs_header_nritems(path->nodes[level]); |
6449 | while (level < max_level && path->nodes[level]) { | |
6450 | wc->level = level; | |
6451 | if (path->slots[level] + 1 < | |
6452 | btrfs_header_nritems(path->nodes[level])) { | |
6453 | path->slots[level]++; | |
20524f02 CM |
6454 | return 0; |
6455 | } else { | |
2c47e605 YZ |
6456 | ret = walk_up_proc(trans, root, path, wc); |
6457 | if (ret > 0) | |
6458 | return 0; | |
bd56b302 | 6459 | |
2c47e605 | 6460 | if (path->locks[level]) { |
bd681513 CM |
6461 | btrfs_tree_unlock_rw(path->nodes[level], |
6462 | path->locks[level]); | |
2c47e605 | 6463 | path->locks[level] = 0; |
f82d02d9 | 6464 | } |
2c47e605 YZ |
6465 | free_extent_buffer(path->nodes[level]); |
6466 | path->nodes[level] = NULL; | |
6467 | level++; | |
20524f02 CM |
6468 | } |
6469 | } | |
6470 | return 1; | |
6471 | } | |
6472 | ||
9aca1d51 | 6473 | /* |
2c47e605 YZ |
6474 | * drop a subvolume tree. |
6475 | * | |
6476 | * this function traverses the tree freeing any blocks that only | |
6477 | * referenced by the tree. | |
6478 | * | |
6479 | * when a shared tree block is found. this function decreases its | |
6480 | * reference count by one. if update_ref is true, this function | |
6481 | * also make sure backrefs for the shared block and all lower level | |
6482 | * blocks are properly updated. | |
9aca1d51 | 6483 | */ |
cb1b69f4 TI |
6484 | void btrfs_drop_snapshot(struct btrfs_root *root, |
6485 | struct btrfs_block_rsv *block_rsv, int update_ref) | |
20524f02 | 6486 | { |
5caf2a00 | 6487 | struct btrfs_path *path; |
2c47e605 YZ |
6488 | struct btrfs_trans_handle *trans; |
6489 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
9f3a7427 | 6490 | struct btrfs_root_item *root_item = &root->root_item; |
2c47e605 YZ |
6491 | struct walk_control *wc; |
6492 | struct btrfs_key key; | |
6493 | int err = 0; | |
6494 | int ret; | |
6495 | int level; | |
20524f02 | 6496 | |
5caf2a00 | 6497 | path = btrfs_alloc_path(); |
cb1b69f4 TI |
6498 | if (!path) { |
6499 | err = -ENOMEM; | |
6500 | goto out; | |
6501 | } | |
20524f02 | 6502 | |
2c47e605 | 6503 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
38a1a919 MF |
6504 | if (!wc) { |
6505 | btrfs_free_path(path); | |
cb1b69f4 TI |
6506 | err = -ENOMEM; |
6507 | goto out; | |
38a1a919 | 6508 | } |
2c47e605 | 6509 | |
a22285a6 | 6510 | trans = btrfs_start_transaction(tree_root, 0); |
98d5dc13 TI |
6511 | BUG_ON(IS_ERR(trans)); |
6512 | ||
3fd0a558 YZ |
6513 | if (block_rsv) |
6514 | trans->block_rsv = block_rsv; | |
2c47e605 | 6515 | |
9f3a7427 | 6516 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
2c47e605 | 6517 | level = btrfs_header_level(root->node); |
5d4f98a2 YZ |
6518 | path->nodes[level] = btrfs_lock_root_node(root); |
6519 | btrfs_set_lock_blocking(path->nodes[level]); | |
9f3a7427 | 6520 | path->slots[level] = 0; |
bd681513 | 6521 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
6522 | memset(&wc->update_progress, 0, |
6523 | sizeof(wc->update_progress)); | |
9f3a7427 | 6524 | } else { |
9f3a7427 | 6525 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
2c47e605 YZ |
6526 | memcpy(&wc->update_progress, &key, |
6527 | sizeof(wc->update_progress)); | |
6528 | ||
6702ed49 | 6529 | level = root_item->drop_level; |
2c47e605 | 6530 | BUG_ON(level == 0); |
6702ed49 | 6531 | path->lowest_level = level; |
2c47e605 YZ |
6532 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6533 | path->lowest_level = 0; | |
6534 | if (ret < 0) { | |
6535 | err = ret; | |
cb1b69f4 | 6536 | goto out_free; |
9f3a7427 | 6537 | } |
1c4850e2 | 6538 | WARN_ON(ret > 0); |
2c47e605 | 6539 | |
7d9eb12c CM |
6540 | /* |
6541 | * unlock our path, this is safe because only this | |
6542 | * function is allowed to delete this snapshot | |
6543 | */ | |
5d4f98a2 | 6544 | btrfs_unlock_up_safe(path, 0); |
2c47e605 YZ |
6545 | |
6546 | level = btrfs_header_level(root->node); | |
6547 | while (1) { | |
6548 | btrfs_tree_lock(path->nodes[level]); | |
6549 | btrfs_set_lock_blocking(path->nodes[level]); | |
6550 | ||
6551 | ret = btrfs_lookup_extent_info(trans, root, | |
6552 | path->nodes[level]->start, | |
6553 | path->nodes[level]->len, | |
6554 | &wc->refs[level], | |
6555 | &wc->flags[level]); | |
6556 | BUG_ON(ret); | |
6557 | BUG_ON(wc->refs[level] == 0); | |
6558 | ||
6559 | if (level == root_item->drop_level) | |
6560 | break; | |
6561 | ||
6562 | btrfs_tree_unlock(path->nodes[level]); | |
6563 | WARN_ON(wc->refs[level] != 1); | |
6564 | level--; | |
6565 | } | |
9f3a7427 | 6566 | } |
2c47e605 YZ |
6567 | |
6568 | wc->level = level; | |
6569 | wc->shared_level = -1; | |
6570 | wc->stage = DROP_REFERENCE; | |
6571 | wc->update_ref = update_ref; | |
6572 | wc->keep_locks = 0; | |
1c4850e2 | 6573 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
2c47e605 | 6574 | |
d397712b | 6575 | while (1) { |
2c47e605 YZ |
6576 | ret = walk_down_tree(trans, root, path, wc); |
6577 | if (ret < 0) { | |
6578 | err = ret; | |
20524f02 | 6579 | break; |
2c47e605 | 6580 | } |
9aca1d51 | 6581 | |
2c47e605 YZ |
6582 | ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL); |
6583 | if (ret < 0) { | |
6584 | err = ret; | |
20524f02 | 6585 | break; |
2c47e605 YZ |
6586 | } |
6587 | ||
6588 | if (ret > 0) { | |
6589 | BUG_ON(wc->stage != DROP_REFERENCE); | |
e7a84565 CM |
6590 | break; |
6591 | } | |
2c47e605 YZ |
6592 | |
6593 | if (wc->stage == DROP_REFERENCE) { | |
6594 | level = wc->level; | |
6595 | btrfs_node_key(path->nodes[level], | |
6596 | &root_item->drop_progress, | |
6597 | path->slots[level]); | |
6598 | root_item->drop_level = level; | |
6599 | } | |
6600 | ||
6601 | BUG_ON(wc->level == 0); | |
3fd0a558 | 6602 | if (btrfs_should_end_transaction(trans, tree_root)) { |
2c47e605 YZ |
6603 | ret = btrfs_update_root(trans, tree_root, |
6604 | &root->root_key, | |
6605 | root_item); | |
6606 | BUG_ON(ret); | |
6607 | ||
3fd0a558 | 6608 | btrfs_end_transaction_throttle(trans, tree_root); |
a22285a6 | 6609 | trans = btrfs_start_transaction(tree_root, 0); |
98d5dc13 | 6610 | BUG_ON(IS_ERR(trans)); |
3fd0a558 YZ |
6611 | if (block_rsv) |
6612 | trans->block_rsv = block_rsv; | |
c3e69d58 | 6613 | } |
20524f02 | 6614 | } |
b3b4aa74 | 6615 | btrfs_release_path(path); |
2c47e605 YZ |
6616 | BUG_ON(err); |
6617 | ||
6618 | ret = btrfs_del_root(trans, tree_root, &root->root_key); | |
6619 | BUG_ON(ret); | |
6620 | ||
76dda93c YZ |
6621 | if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) { |
6622 | ret = btrfs_find_last_root(tree_root, root->root_key.objectid, | |
6623 | NULL, NULL); | |
6624 | BUG_ON(ret < 0); | |
6625 | if (ret > 0) { | |
84cd948c JB |
6626 | /* if we fail to delete the orphan item this time |
6627 | * around, it'll get picked up the next time. | |
6628 | * | |
6629 | * The most common failure here is just -ENOENT. | |
6630 | */ | |
6631 | btrfs_del_orphan_item(trans, tree_root, | |
6632 | root->root_key.objectid); | |
76dda93c YZ |
6633 | } |
6634 | } | |
6635 | ||
6636 | if (root->in_radix) { | |
6637 | btrfs_free_fs_root(tree_root->fs_info, root); | |
6638 | } else { | |
6639 | free_extent_buffer(root->node); | |
6640 | free_extent_buffer(root->commit_root); | |
6641 | kfree(root); | |
6642 | } | |
cb1b69f4 | 6643 | out_free: |
3fd0a558 | 6644 | btrfs_end_transaction_throttle(trans, tree_root); |
2c47e605 | 6645 | kfree(wc); |
5caf2a00 | 6646 | btrfs_free_path(path); |
cb1b69f4 TI |
6647 | out: |
6648 | if (err) | |
6649 | btrfs_std_error(root->fs_info, err); | |
6650 | return; | |
20524f02 | 6651 | } |
9078a3e1 | 6652 | |
2c47e605 YZ |
6653 | /* |
6654 | * drop subtree rooted at tree block 'node'. | |
6655 | * | |
6656 | * NOTE: this function will unlock and release tree block 'node' | |
6657 | */ | |
f82d02d9 YZ |
6658 | int btrfs_drop_subtree(struct btrfs_trans_handle *trans, |
6659 | struct btrfs_root *root, | |
6660 | struct extent_buffer *node, | |
6661 | struct extent_buffer *parent) | |
6662 | { | |
6663 | struct btrfs_path *path; | |
2c47e605 | 6664 | struct walk_control *wc; |
f82d02d9 YZ |
6665 | int level; |
6666 | int parent_level; | |
6667 | int ret = 0; | |
6668 | int wret; | |
6669 | ||
2c47e605 YZ |
6670 | BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID); |
6671 | ||
f82d02d9 | 6672 | path = btrfs_alloc_path(); |
db5b493a TI |
6673 | if (!path) |
6674 | return -ENOMEM; | |
f82d02d9 | 6675 | |
2c47e605 | 6676 | wc = kzalloc(sizeof(*wc), GFP_NOFS); |
db5b493a TI |
6677 | if (!wc) { |
6678 | btrfs_free_path(path); | |
6679 | return -ENOMEM; | |
6680 | } | |
2c47e605 | 6681 | |
b9447ef8 | 6682 | btrfs_assert_tree_locked(parent); |
f82d02d9 YZ |
6683 | parent_level = btrfs_header_level(parent); |
6684 | extent_buffer_get(parent); | |
6685 | path->nodes[parent_level] = parent; | |
6686 | path->slots[parent_level] = btrfs_header_nritems(parent); | |
6687 | ||
b9447ef8 | 6688 | btrfs_assert_tree_locked(node); |
f82d02d9 | 6689 | level = btrfs_header_level(node); |
f82d02d9 YZ |
6690 | path->nodes[level] = node; |
6691 | path->slots[level] = 0; | |
bd681513 | 6692 | path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING; |
2c47e605 YZ |
6693 | |
6694 | wc->refs[parent_level] = 1; | |
6695 | wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF; | |
6696 | wc->level = level; | |
6697 | wc->shared_level = -1; | |
6698 | wc->stage = DROP_REFERENCE; | |
6699 | wc->update_ref = 0; | |
6700 | wc->keep_locks = 1; | |
1c4850e2 | 6701 | wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root); |
f82d02d9 YZ |
6702 | |
6703 | while (1) { | |
2c47e605 YZ |
6704 | wret = walk_down_tree(trans, root, path, wc); |
6705 | if (wret < 0) { | |
f82d02d9 | 6706 | ret = wret; |
f82d02d9 | 6707 | break; |
2c47e605 | 6708 | } |
f82d02d9 | 6709 | |
2c47e605 | 6710 | wret = walk_up_tree(trans, root, path, wc, parent_level); |
f82d02d9 YZ |
6711 | if (wret < 0) |
6712 | ret = wret; | |
6713 | if (wret != 0) | |
6714 | break; | |
6715 | } | |
6716 | ||
2c47e605 | 6717 | kfree(wc); |
f82d02d9 YZ |
6718 | btrfs_free_path(path); |
6719 | return ret; | |
6720 | } | |
6721 | ||
ec44a35c CM |
6722 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
6723 | { | |
6724 | u64 num_devices; | |
6725 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | | |
6726 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; | |
6727 | ||
cd02dca5 CM |
6728 | /* |
6729 | * we add in the count of missing devices because we want | |
6730 | * to make sure that any RAID levels on a degraded FS | |
6731 | * continue to be honored. | |
6732 | */ | |
6733 | num_devices = root->fs_info->fs_devices->rw_devices + | |
6734 | root->fs_info->fs_devices->missing_devices; | |
6735 | ||
ec44a35c CM |
6736 | if (num_devices == 1) { |
6737 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
6738 | stripped = flags & ~stripped; | |
6739 | ||
6740 | /* turn raid0 into single device chunks */ | |
6741 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
6742 | return stripped; | |
6743 | ||
6744 | /* turn mirroring into duplication */ | |
6745 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
6746 | BTRFS_BLOCK_GROUP_RAID10)) | |
6747 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
6748 | return flags; | |
6749 | } else { | |
6750 | /* they already had raid on here, just return */ | |
ec44a35c CM |
6751 | if (flags & stripped) |
6752 | return flags; | |
6753 | ||
6754 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
6755 | stripped = flags & ~stripped; | |
6756 | ||
6757 | /* switch duplicated blocks with raid1 */ | |
6758 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
6759 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
6760 | ||
6761 | /* turn single device chunks into raid0 */ | |
6762 | return stripped | BTRFS_BLOCK_GROUP_RAID0; | |
6763 | } | |
6764 | return flags; | |
6765 | } | |
6766 | ||
199c36ea | 6767 | static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force) |
0ef3e66b | 6768 | { |
f0486c68 YZ |
6769 | struct btrfs_space_info *sinfo = cache->space_info; |
6770 | u64 num_bytes; | |
199c36ea | 6771 | u64 min_allocable_bytes; |
f0486c68 | 6772 | int ret = -ENOSPC; |
0ef3e66b | 6773 | |
c286ac48 | 6774 | |
199c36ea MX |
6775 | /* |
6776 | * We need some metadata space and system metadata space for | |
6777 | * allocating chunks in some corner cases until we force to set | |
6778 | * it to be readonly. | |
6779 | */ | |
6780 | if ((sinfo->flags & | |
6781 | (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) && | |
6782 | !force) | |
6783 | min_allocable_bytes = 1 * 1024 * 1024; | |
6784 | else | |
6785 | min_allocable_bytes = 0; | |
6786 | ||
f0486c68 YZ |
6787 | spin_lock(&sinfo->lock); |
6788 | spin_lock(&cache->lock); | |
61cfea9b W |
6789 | |
6790 | if (cache->ro) { | |
6791 | ret = 0; | |
6792 | goto out; | |
6793 | } | |
6794 | ||
f0486c68 YZ |
6795 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - |
6796 | cache->bytes_super - btrfs_block_group_used(&cache->item); | |
6797 | ||
6798 | if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned + | |
37be25bc JB |
6799 | sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes + |
6800 | min_allocable_bytes <= sinfo->total_bytes) { | |
f0486c68 | 6801 | sinfo->bytes_readonly += num_bytes; |
f0486c68 YZ |
6802 | cache->ro = 1; |
6803 | ret = 0; | |
6804 | } | |
61cfea9b | 6805 | out: |
f0486c68 YZ |
6806 | spin_unlock(&cache->lock); |
6807 | spin_unlock(&sinfo->lock); | |
6808 | return ret; | |
6809 | } | |
7d9eb12c | 6810 | |
f0486c68 YZ |
6811 | int btrfs_set_block_group_ro(struct btrfs_root *root, |
6812 | struct btrfs_block_group_cache *cache) | |
c286ac48 | 6813 | |
f0486c68 YZ |
6814 | { |
6815 | struct btrfs_trans_handle *trans; | |
6816 | u64 alloc_flags; | |
6817 | int ret; | |
7d9eb12c | 6818 | |
f0486c68 | 6819 | BUG_ON(cache->ro); |
0ef3e66b | 6820 | |
ff5714cc | 6821 | trans = btrfs_join_transaction(root); |
f0486c68 | 6822 | BUG_ON(IS_ERR(trans)); |
5d4f98a2 | 6823 | |
f0486c68 YZ |
6824 | alloc_flags = update_block_group_flags(root, cache->flags); |
6825 | if (alloc_flags != cache->flags) | |
0e4f8f88 CM |
6826 | do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
6827 | CHUNK_ALLOC_FORCE); | |
5d4f98a2 | 6828 | |
199c36ea | 6829 | ret = set_block_group_ro(cache, 0); |
f0486c68 YZ |
6830 | if (!ret) |
6831 | goto out; | |
6832 | alloc_flags = get_alloc_profile(root, cache->space_info->flags); | |
0e4f8f88 CM |
6833 | ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
6834 | CHUNK_ALLOC_FORCE); | |
f0486c68 YZ |
6835 | if (ret < 0) |
6836 | goto out; | |
199c36ea | 6837 | ret = set_block_group_ro(cache, 0); |
f0486c68 YZ |
6838 | out: |
6839 | btrfs_end_transaction(trans, root); | |
6840 | return ret; | |
6841 | } | |
5d4f98a2 | 6842 | |
c87f08ca CM |
6843 | int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans, |
6844 | struct btrfs_root *root, u64 type) | |
6845 | { | |
6846 | u64 alloc_flags = get_alloc_profile(root, type); | |
0e4f8f88 CM |
6847 | return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, |
6848 | CHUNK_ALLOC_FORCE); | |
c87f08ca CM |
6849 | } |
6850 | ||
6d07bcec MX |
6851 | /* |
6852 | * helper to account the unused space of all the readonly block group in the | |
6853 | * list. takes mirrors into account. | |
6854 | */ | |
6855 | static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list) | |
6856 | { | |
6857 | struct btrfs_block_group_cache *block_group; | |
6858 | u64 free_bytes = 0; | |
6859 | int factor; | |
6860 | ||
6861 | list_for_each_entry(block_group, groups_list, list) { | |
6862 | spin_lock(&block_group->lock); | |
6863 | ||
6864 | if (!block_group->ro) { | |
6865 | spin_unlock(&block_group->lock); | |
6866 | continue; | |
6867 | } | |
6868 | ||
6869 | if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
6870 | BTRFS_BLOCK_GROUP_RAID10 | | |
6871 | BTRFS_BLOCK_GROUP_DUP)) | |
6872 | factor = 2; | |
6873 | else | |
6874 | factor = 1; | |
6875 | ||
6876 | free_bytes += (block_group->key.offset - | |
6877 | btrfs_block_group_used(&block_group->item)) * | |
6878 | factor; | |
6879 | ||
6880 | spin_unlock(&block_group->lock); | |
6881 | } | |
6882 | ||
6883 | return free_bytes; | |
6884 | } | |
6885 | ||
6886 | /* | |
6887 | * helper to account the unused space of all the readonly block group in the | |
6888 | * space_info. takes mirrors into account. | |
6889 | */ | |
6890 | u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo) | |
6891 | { | |
6892 | int i; | |
6893 | u64 free_bytes = 0; | |
6894 | ||
6895 | spin_lock(&sinfo->lock); | |
6896 | ||
6897 | for(i = 0; i < BTRFS_NR_RAID_TYPES; i++) | |
6898 | if (!list_empty(&sinfo->block_groups[i])) | |
6899 | free_bytes += __btrfs_get_ro_block_group_free_space( | |
6900 | &sinfo->block_groups[i]); | |
6901 | ||
6902 | spin_unlock(&sinfo->lock); | |
6903 | ||
6904 | return free_bytes; | |
6905 | } | |
6906 | ||
f0486c68 YZ |
6907 | int btrfs_set_block_group_rw(struct btrfs_root *root, |
6908 | struct btrfs_block_group_cache *cache) | |
5d4f98a2 | 6909 | { |
f0486c68 YZ |
6910 | struct btrfs_space_info *sinfo = cache->space_info; |
6911 | u64 num_bytes; | |
6912 | ||
6913 | BUG_ON(!cache->ro); | |
6914 | ||
6915 | spin_lock(&sinfo->lock); | |
6916 | spin_lock(&cache->lock); | |
6917 | num_bytes = cache->key.offset - cache->reserved - cache->pinned - | |
6918 | cache->bytes_super - btrfs_block_group_used(&cache->item); | |
6919 | sinfo->bytes_readonly -= num_bytes; | |
6920 | cache->ro = 0; | |
6921 | spin_unlock(&cache->lock); | |
6922 | spin_unlock(&sinfo->lock); | |
5d4f98a2 YZ |
6923 | return 0; |
6924 | } | |
6925 | ||
ba1bf481 JB |
6926 | /* |
6927 | * checks to see if its even possible to relocate this block group. | |
6928 | * | |
6929 | * @return - -1 if it's not a good idea to relocate this block group, 0 if its | |
6930 | * ok to go ahead and try. | |
6931 | */ | |
6932 | int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr) | |
1a40e23b | 6933 | { |
ba1bf481 JB |
6934 | struct btrfs_block_group_cache *block_group; |
6935 | struct btrfs_space_info *space_info; | |
6936 | struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices; | |
6937 | struct btrfs_device *device; | |
cdcb725c | 6938 | u64 min_free; |
6719db6a JB |
6939 | u64 dev_min = 1; |
6940 | u64 dev_nr = 0; | |
cdcb725c | 6941 | int index; |
ba1bf481 JB |
6942 | int full = 0; |
6943 | int ret = 0; | |
1a40e23b | 6944 | |
ba1bf481 | 6945 | block_group = btrfs_lookup_block_group(root->fs_info, bytenr); |
1a40e23b | 6946 | |
ba1bf481 JB |
6947 | /* odd, couldn't find the block group, leave it alone */ |
6948 | if (!block_group) | |
6949 | return -1; | |
1a40e23b | 6950 | |
cdcb725c | 6951 | min_free = btrfs_block_group_used(&block_group->item); |
6952 | ||
ba1bf481 | 6953 | /* no bytes used, we're good */ |
cdcb725c | 6954 | if (!min_free) |
1a40e23b ZY |
6955 | goto out; |
6956 | ||
ba1bf481 JB |
6957 | space_info = block_group->space_info; |
6958 | spin_lock(&space_info->lock); | |
17d217fe | 6959 | |
ba1bf481 | 6960 | full = space_info->full; |
17d217fe | 6961 | |
ba1bf481 JB |
6962 | /* |
6963 | * if this is the last block group we have in this space, we can't | |
7ce618db CM |
6964 | * relocate it unless we're able to allocate a new chunk below. |
6965 | * | |
6966 | * Otherwise, we need to make sure we have room in the space to handle | |
6967 | * all of the extents from this block group. If we can, we're good | |
ba1bf481 | 6968 | */ |
7ce618db | 6969 | if ((space_info->total_bytes != block_group->key.offset) && |
cdcb725c | 6970 | (space_info->bytes_used + space_info->bytes_reserved + |
6971 | space_info->bytes_pinned + space_info->bytes_readonly + | |
6972 | min_free < space_info->total_bytes)) { | |
ba1bf481 JB |
6973 | spin_unlock(&space_info->lock); |
6974 | goto out; | |
17d217fe | 6975 | } |
ba1bf481 | 6976 | spin_unlock(&space_info->lock); |
ea8c2819 | 6977 | |
ba1bf481 JB |
6978 | /* |
6979 | * ok we don't have enough space, but maybe we have free space on our | |
6980 | * devices to allocate new chunks for relocation, so loop through our | |
6981 | * alloc devices and guess if we have enough space. However, if we | |
6982 | * were marked as full, then we know there aren't enough chunks, and we | |
6983 | * can just return. | |
6984 | */ | |
6985 | ret = -1; | |
6986 | if (full) | |
6987 | goto out; | |
ea8c2819 | 6988 | |
cdcb725c | 6989 | /* |
6990 | * index: | |
6991 | * 0: raid10 | |
6992 | * 1: raid1 | |
6993 | * 2: dup | |
6994 | * 3: raid0 | |
6995 | * 4: single | |
6996 | */ | |
6997 | index = get_block_group_index(block_group); | |
6998 | if (index == 0) { | |
6999 | dev_min = 4; | |
6719db6a JB |
7000 | /* Divide by 2 */ |
7001 | min_free >>= 1; | |
cdcb725c | 7002 | } else if (index == 1) { |
7003 | dev_min = 2; | |
7004 | } else if (index == 2) { | |
6719db6a JB |
7005 | /* Multiply by 2 */ |
7006 | min_free <<= 1; | |
cdcb725c | 7007 | } else if (index == 3) { |
7008 | dev_min = fs_devices->rw_devices; | |
6719db6a | 7009 | do_div(min_free, dev_min); |
cdcb725c | 7010 | } |
7011 | ||
ba1bf481 JB |
7012 | mutex_lock(&root->fs_info->chunk_mutex); |
7013 | list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) { | |
7bfc837d | 7014 | u64 dev_offset; |
56bec294 | 7015 | |
ba1bf481 JB |
7016 | /* |
7017 | * check to make sure we can actually find a chunk with enough | |
7018 | * space to fit our block group in. | |
7019 | */ | |
7020 | if (device->total_bytes > device->bytes_used + min_free) { | |
7021 | ret = find_free_dev_extent(NULL, device, min_free, | |
7bfc837d | 7022 | &dev_offset, NULL); |
ba1bf481 | 7023 | if (!ret) |
cdcb725c | 7024 | dev_nr++; |
7025 | ||
7026 | if (dev_nr >= dev_min) | |
73e48b27 | 7027 | break; |
cdcb725c | 7028 | |
ba1bf481 | 7029 | ret = -1; |
725c8463 | 7030 | } |
edbd8d4e | 7031 | } |
ba1bf481 | 7032 | mutex_unlock(&root->fs_info->chunk_mutex); |
edbd8d4e | 7033 | out: |
ba1bf481 | 7034 | btrfs_put_block_group(block_group); |
edbd8d4e CM |
7035 | return ret; |
7036 | } | |
7037 | ||
b2950863 CH |
7038 | static int find_first_block_group(struct btrfs_root *root, |
7039 | struct btrfs_path *path, struct btrfs_key *key) | |
0b86a832 | 7040 | { |
925baedd | 7041 | int ret = 0; |
0b86a832 CM |
7042 | struct btrfs_key found_key; |
7043 | struct extent_buffer *leaf; | |
7044 | int slot; | |
edbd8d4e | 7045 | |
0b86a832 CM |
7046 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
7047 | if (ret < 0) | |
925baedd CM |
7048 | goto out; |
7049 | ||
d397712b | 7050 | while (1) { |
0b86a832 | 7051 | slot = path->slots[0]; |
edbd8d4e | 7052 | leaf = path->nodes[0]; |
0b86a832 CM |
7053 | if (slot >= btrfs_header_nritems(leaf)) { |
7054 | ret = btrfs_next_leaf(root, path); | |
7055 | if (ret == 0) | |
7056 | continue; | |
7057 | if (ret < 0) | |
925baedd | 7058 | goto out; |
0b86a832 | 7059 | break; |
edbd8d4e | 7060 | } |
0b86a832 | 7061 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
edbd8d4e | 7062 | |
0b86a832 | 7063 | if (found_key.objectid >= key->objectid && |
925baedd CM |
7064 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) { |
7065 | ret = 0; | |
7066 | goto out; | |
7067 | } | |
0b86a832 | 7068 | path->slots[0]++; |
edbd8d4e | 7069 | } |
925baedd | 7070 | out: |
0b86a832 | 7071 | return ret; |
edbd8d4e CM |
7072 | } |
7073 | ||
0af3d00b JB |
7074 | void btrfs_put_block_group_cache(struct btrfs_fs_info *info) |
7075 | { | |
7076 | struct btrfs_block_group_cache *block_group; | |
7077 | u64 last = 0; | |
7078 | ||
7079 | while (1) { | |
7080 | struct inode *inode; | |
7081 | ||
7082 | block_group = btrfs_lookup_first_block_group(info, last); | |
7083 | while (block_group) { | |
7084 | spin_lock(&block_group->lock); | |
7085 | if (block_group->iref) | |
7086 | break; | |
7087 | spin_unlock(&block_group->lock); | |
7088 | block_group = next_block_group(info->tree_root, | |
7089 | block_group); | |
7090 | } | |
7091 | if (!block_group) { | |
7092 | if (last == 0) | |
7093 | break; | |
7094 | last = 0; | |
7095 | continue; | |
7096 | } | |
7097 | ||
7098 | inode = block_group->inode; | |
7099 | block_group->iref = 0; | |
7100 | block_group->inode = NULL; | |
7101 | spin_unlock(&block_group->lock); | |
7102 | iput(inode); | |
7103 | last = block_group->key.objectid + block_group->key.offset; | |
7104 | btrfs_put_block_group(block_group); | |
7105 | } | |
7106 | } | |
7107 | ||
1a40e23b ZY |
7108 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
7109 | { | |
7110 | struct btrfs_block_group_cache *block_group; | |
4184ea7f | 7111 | struct btrfs_space_info *space_info; |
11833d66 | 7112 | struct btrfs_caching_control *caching_ctl; |
1a40e23b ZY |
7113 | struct rb_node *n; |
7114 | ||
11833d66 YZ |
7115 | down_write(&info->extent_commit_sem); |
7116 | while (!list_empty(&info->caching_block_groups)) { | |
7117 | caching_ctl = list_entry(info->caching_block_groups.next, | |
7118 | struct btrfs_caching_control, list); | |
7119 | list_del(&caching_ctl->list); | |
7120 | put_caching_control(caching_ctl); | |
7121 | } | |
7122 | up_write(&info->extent_commit_sem); | |
7123 | ||
1a40e23b ZY |
7124 | spin_lock(&info->block_group_cache_lock); |
7125 | while ((n = rb_last(&info->block_group_cache_tree)) != NULL) { | |
7126 | block_group = rb_entry(n, struct btrfs_block_group_cache, | |
7127 | cache_node); | |
1a40e23b ZY |
7128 | rb_erase(&block_group->cache_node, |
7129 | &info->block_group_cache_tree); | |
d899e052 YZ |
7130 | spin_unlock(&info->block_group_cache_lock); |
7131 | ||
80eb234a | 7132 | down_write(&block_group->space_info->groups_sem); |
1a40e23b | 7133 | list_del(&block_group->list); |
80eb234a | 7134 | up_write(&block_group->space_info->groups_sem); |
d2fb3437 | 7135 | |
817d52f8 | 7136 | if (block_group->cached == BTRFS_CACHE_STARTED) |
11833d66 | 7137 | wait_block_group_cache_done(block_group); |
817d52f8 | 7138 | |
3c14874a JB |
7139 | /* |
7140 | * We haven't cached this block group, which means we could | |
7141 | * possibly have excluded extents on this block group. | |
7142 | */ | |
7143 | if (block_group->cached == BTRFS_CACHE_NO) | |
7144 | free_excluded_extents(info->extent_root, block_group); | |
7145 | ||
817d52f8 | 7146 | btrfs_remove_free_space_cache(block_group); |
11dfe35a | 7147 | btrfs_put_block_group(block_group); |
d899e052 YZ |
7148 | |
7149 | spin_lock(&info->block_group_cache_lock); | |
1a40e23b ZY |
7150 | } |
7151 | spin_unlock(&info->block_group_cache_lock); | |
4184ea7f CM |
7152 | |
7153 | /* now that all the block groups are freed, go through and | |
7154 | * free all the space_info structs. This is only called during | |
7155 | * the final stages of unmount, and so we know nobody is | |
7156 | * using them. We call synchronize_rcu() once before we start, | |
7157 | * just to be on the safe side. | |
7158 | */ | |
7159 | synchronize_rcu(); | |
7160 | ||
8929ecfa YZ |
7161 | release_global_block_rsv(info); |
7162 | ||
4184ea7f CM |
7163 | while(!list_empty(&info->space_info)) { |
7164 | space_info = list_entry(info->space_info.next, | |
7165 | struct btrfs_space_info, | |
7166 | list); | |
f0486c68 | 7167 | if (space_info->bytes_pinned > 0 || |
fb25e914 JB |
7168 | space_info->bytes_reserved > 0 || |
7169 | space_info->bytes_may_use > 0) { | |
f0486c68 YZ |
7170 | WARN_ON(1); |
7171 | dump_space_info(space_info, 0, 0); | |
7172 | } | |
4184ea7f CM |
7173 | list_del(&space_info->list); |
7174 | kfree(space_info); | |
7175 | } | |
1a40e23b ZY |
7176 | return 0; |
7177 | } | |
7178 | ||
b742bb82 YZ |
7179 | static void __link_block_group(struct btrfs_space_info *space_info, |
7180 | struct btrfs_block_group_cache *cache) | |
7181 | { | |
7182 | int index = get_block_group_index(cache); | |
7183 | ||
7184 | down_write(&space_info->groups_sem); | |
7185 | list_add_tail(&cache->list, &space_info->block_groups[index]); | |
7186 | up_write(&space_info->groups_sem); | |
7187 | } | |
7188 | ||
9078a3e1 CM |
7189 | int btrfs_read_block_groups(struct btrfs_root *root) |
7190 | { | |
7191 | struct btrfs_path *path; | |
7192 | int ret; | |
9078a3e1 | 7193 | struct btrfs_block_group_cache *cache; |
be744175 | 7194 | struct btrfs_fs_info *info = root->fs_info; |
6324fbf3 | 7195 | struct btrfs_space_info *space_info; |
9078a3e1 CM |
7196 | struct btrfs_key key; |
7197 | struct btrfs_key found_key; | |
5f39d397 | 7198 | struct extent_buffer *leaf; |
0af3d00b JB |
7199 | int need_clear = 0; |
7200 | u64 cache_gen; | |
96b5179d | 7201 | |
be744175 | 7202 | root = info->extent_root; |
9078a3e1 | 7203 | key.objectid = 0; |
0b86a832 | 7204 | key.offset = 0; |
9078a3e1 | 7205 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
9078a3e1 CM |
7206 | path = btrfs_alloc_path(); |
7207 | if (!path) | |
7208 | return -ENOMEM; | |
026fd317 | 7209 | path->reada = 1; |
9078a3e1 | 7210 | |
6c41761f | 7211 | cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy); |
73bc1876 | 7212 | if (btrfs_test_opt(root, SPACE_CACHE) && |
6c41761f | 7213 | btrfs_super_generation(root->fs_info->super_copy) != cache_gen) |
0af3d00b | 7214 | need_clear = 1; |
88c2ba3b JB |
7215 | if (btrfs_test_opt(root, CLEAR_CACHE)) |
7216 | need_clear = 1; | |
0af3d00b | 7217 | |
d397712b | 7218 | while (1) { |
0b86a832 | 7219 | ret = find_first_block_group(root, path, &key); |
b742bb82 YZ |
7220 | if (ret > 0) |
7221 | break; | |
0b86a832 CM |
7222 | if (ret != 0) |
7223 | goto error; | |
5f39d397 CM |
7224 | leaf = path->nodes[0]; |
7225 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 | 7226 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
9078a3e1 | 7227 | if (!cache) { |
0b86a832 | 7228 | ret = -ENOMEM; |
f0486c68 | 7229 | goto error; |
9078a3e1 | 7230 | } |
34d52cb6 LZ |
7231 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
7232 | GFP_NOFS); | |
7233 | if (!cache->free_space_ctl) { | |
7234 | kfree(cache); | |
7235 | ret = -ENOMEM; | |
7236 | goto error; | |
7237 | } | |
3e1ad54f | 7238 | |
d2fb3437 | 7239 | atomic_set(&cache->count, 1); |
c286ac48 | 7240 | spin_lock_init(&cache->lock); |
817d52f8 | 7241 | cache->fs_info = info; |
0f9dd46c | 7242 | INIT_LIST_HEAD(&cache->list); |
fa9c0d79 | 7243 | INIT_LIST_HEAD(&cache->cluster_list); |
96303081 | 7244 | |
0af3d00b JB |
7245 | if (need_clear) |
7246 | cache->disk_cache_state = BTRFS_DC_CLEAR; | |
7247 | ||
5f39d397 CM |
7248 | read_extent_buffer(leaf, &cache->item, |
7249 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
7250 | sizeof(cache->item)); | |
9078a3e1 | 7251 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
0b86a832 | 7252 | |
9078a3e1 | 7253 | key.objectid = found_key.objectid + found_key.offset; |
b3b4aa74 | 7254 | btrfs_release_path(path); |
0b86a832 | 7255 | cache->flags = btrfs_block_group_flags(&cache->item); |
817d52f8 JB |
7256 | cache->sectorsize = root->sectorsize; |
7257 | ||
34d52cb6 LZ |
7258 | btrfs_init_free_space_ctl(cache); |
7259 | ||
3c14874a JB |
7260 | /* |
7261 | * We need to exclude the super stripes now so that the space | |
7262 | * info has super bytes accounted for, otherwise we'll think | |
7263 | * we have more space than we actually do. | |
7264 | */ | |
7265 | exclude_super_stripes(root, cache); | |
7266 | ||
817d52f8 JB |
7267 | /* |
7268 | * check for two cases, either we are full, and therefore | |
7269 | * don't need to bother with the caching work since we won't | |
7270 | * find any space, or we are empty, and we can just add all | |
7271 | * the space in and be done with it. This saves us _alot_ of | |
7272 | * time, particularly in the full case. | |
7273 | */ | |
7274 | if (found_key.offset == btrfs_block_group_used(&cache->item)) { | |
11833d66 | 7275 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 | 7276 | cache->cached = BTRFS_CACHE_FINISHED; |
1b2da372 | 7277 | free_excluded_extents(root, cache); |
817d52f8 | 7278 | } else if (btrfs_block_group_used(&cache->item) == 0) { |
11833d66 | 7279 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 JB |
7280 | cache->cached = BTRFS_CACHE_FINISHED; |
7281 | add_new_free_space(cache, root->fs_info, | |
7282 | found_key.objectid, | |
7283 | found_key.objectid + | |
7284 | found_key.offset); | |
11833d66 | 7285 | free_excluded_extents(root, cache); |
817d52f8 | 7286 | } |
96b5179d | 7287 | |
6324fbf3 CM |
7288 | ret = update_space_info(info, cache->flags, found_key.offset, |
7289 | btrfs_block_group_used(&cache->item), | |
7290 | &space_info); | |
7291 | BUG_ON(ret); | |
7292 | cache->space_info = space_info; | |
1b2da372 | 7293 | spin_lock(&cache->space_info->lock); |
f0486c68 | 7294 | cache->space_info->bytes_readonly += cache->bytes_super; |
1b2da372 JB |
7295 | spin_unlock(&cache->space_info->lock); |
7296 | ||
b742bb82 | 7297 | __link_block_group(space_info, cache); |
0f9dd46c JB |
7298 | |
7299 | ret = btrfs_add_block_group_cache(root->fs_info, cache); | |
7300 | BUG_ON(ret); | |
75ccf47d CM |
7301 | |
7302 | set_avail_alloc_bits(root->fs_info, cache->flags); | |
2b82032c | 7303 | if (btrfs_chunk_readonly(root, cache->key.objectid)) |
199c36ea | 7304 | set_block_group_ro(cache, 1); |
9078a3e1 | 7305 | } |
b742bb82 YZ |
7306 | |
7307 | list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) { | |
7308 | if (!(get_alloc_profile(root, space_info->flags) & | |
7309 | (BTRFS_BLOCK_GROUP_RAID10 | | |
7310 | BTRFS_BLOCK_GROUP_RAID1 | | |
7311 | BTRFS_BLOCK_GROUP_DUP))) | |
7312 | continue; | |
7313 | /* | |
7314 | * avoid allocating from un-mirrored block group if there are | |
7315 | * mirrored block groups. | |
7316 | */ | |
7317 | list_for_each_entry(cache, &space_info->block_groups[3], list) | |
199c36ea | 7318 | set_block_group_ro(cache, 1); |
b742bb82 | 7319 | list_for_each_entry(cache, &space_info->block_groups[4], list) |
199c36ea | 7320 | set_block_group_ro(cache, 1); |
9078a3e1 | 7321 | } |
f0486c68 YZ |
7322 | |
7323 | init_global_block_rsv(info); | |
0b86a832 CM |
7324 | ret = 0; |
7325 | error: | |
9078a3e1 | 7326 | btrfs_free_path(path); |
0b86a832 | 7327 | return ret; |
9078a3e1 | 7328 | } |
6324fbf3 CM |
7329 | |
7330 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |
7331 | struct btrfs_root *root, u64 bytes_used, | |
e17cade2 | 7332 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
6324fbf3 CM |
7333 | u64 size) |
7334 | { | |
7335 | int ret; | |
6324fbf3 CM |
7336 | struct btrfs_root *extent_root; |
7337 | struct btrfs_block_group_cache *cache; | |
6324fbf3 CM |
7338 | |
7339 | extent_root = root->fs_info->extent_root; | |
6324fbf3 | 7340 | |
12fcfd22 | 7341 | root->fs_info->last_trans_log_full_commit = trans->transid; |
e02119d5 | 7342 | |
8f18cf13 | 7343 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
0f9dd46c JB |
7344 | if (!cache) |
7345 | return -ENOMEM; | |
34d52cb6 LZ |
7346 | cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl), |
7347 | GFP_NOFS); | |
7348 | if (!cache->free_space_ctl) { | |
7349 | kfree(cache); | |
7350 | return -ENOMEM; | |
7351 | } | |
0f9dd46c | 7352 | |
e17cade2 | 7353 | cache->key.objectid = chunk_offset; |
6324fbf3 | 7354 | cache->key.offset = size; |
d2fb3437 | 7355 | cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY; |
96303081 | 7356 | cache->sectorsize = root->sectorsize; |
0af3d00b | 7357 | cache->fs_info = root->fs_info; |
96303081 | 7358 | |
d2fb3437 | 7359 | atomic_set(&cache->count, 1); |
c286ac48 | 7360 | spin_lock_init(&cache->lock); |
0f9dd46c | 7361 | INIT_LIST_HEAD(&cache->list); |
fa9c0d79 | 7362 | INIT_LIST_HEAD(&cache->cluster_list); |
0ef3e66b | 7363 | |
34d52cb6 LZ |
7364 | btrfs_init_free_space_ctl(cache); |
7365 | ||
6324fbf3 | 7366 | btrfs_set_block_group_used(&cache->item, bytes_used); |
6324fbf3 CM |
7367 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
7368 | cache->flags = type; | |
7369 | btrfs_set_block_group_flags(&cache->item, type); | |
7370 | ||
11833d66 | 7371 | cache->last_byte_to_unpin = (u64)-1; |
817d52f8 | 7372 | cache->cached = BTRFS_CACHE_FINISHED; |
11833d66 | 7373 | exclude_super_stripes(root, cache); |
96303081 | 7374 | |
817d52f8 JB |
7375 | add_new_free_space(cache, root->fs_info, chunk_offset, |
7376 | chunk_offset + size); | |
7377 | ||
11833d66 YZ |
7378 | free_excluded_extents(root, cache); |
7379 | ||
6324fbf3 CM |
7380 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
7381 | &cache->space_info); | |
7382 | BUG_ON(ret); | |
1b2da372 JB |
7383 | |
7384 | spin_lock(&cache->space_info->lock); | |
f0486c68 | 7385 | cache->space_info->bytes_readonly += cache->bytes_super; |
1b2da372 JB |
7386 | spin_unlock(&cache->space_info->lock); |
7387 | ||
b742bb82 | 7388 | __link_block_group(cache->space_info, cache); |
6324fbf3 | 7389 | |
0f9dd46c JB |
7390 | ret = btrfs_add_block_group_cache(root->fs_info, cache); |
7391 | BUG_ON(ret); | |
c286ac48 | 7392 | |
6324fbf3 CM |
7393 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
7394 | sizeof(cache->item)); | |
7395 | BUG_ON(ret); | |
7396 | ||
d18a2c44 | 7397 | set_avail_alloc_bits(extent_root->fs_info, type); |
925baedd | 7398 | |
6324fbf3 CM |
7399 | return 0; |
7400 | } | |
1a40e23b ZY |
7401 | |
7402 | int btrfs_remove_block_group(struct btrfs_trans_handle *trans, | |
7403 | struct btrfs_root *root, u64 group_start) | |
7404 | { | |
7405 | struct btrfs_path *path; | |
7406 | struct btrfs_block_group_cache *block_group; | |
44fb5511 | 7407 | struct btrfs_free_cluster *cluster; |
0af3d00b | 7408 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
1a40e23b | 7409 | struct btrfs_key key; |
0af3d00b | 7410 | struct inode *inode; |
1a40e23b | 7411 | int ret; |
89a55897 | 7412 | int factor; |
1a40e23b | 7413 | |
1a40e23b ZY |
7414 | root = root->fs_info->extent_root; |
7415 | ||
7416 | block_group = btrfs_lookup_block_group(root->fs_info, group_start); | |
7417 | BUG_ON(!block_group); | |
c146afad | 7418 | BUG_ON(!block_group->ro); |
1a40e23b | 7419 | |
9f7c43c9 | 7420 | /* |
7421 | * Free the reserved super bytes from this block group before | |
7422 | * remove it. | |
7423 | */ | |
7424 | free_excluded_extents(root, block_group); | |
7425 | ||
1a40e23b | 7426 | memcpy(&key, &block_group->key, sizeof(key)); |
89a55897 JB |
7427 | if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP | |
7428 | BTRFS_BLOCK_GROUP_RAID1 | | |
7429 | BTRFS_BLOCK_GROUP_RAID10)) | |
7430 | factor = 2; | |
7431 | else | |
7432 | factor = 1; | |
1a40e23b | 7433 | |
44fb5511 CM |
7434 | /* make sure this block group isn't part of an allocation cluster */ |
7435 | cluster = &root->fs_info->data_alloc_cluster; | |
7436 | spin_lock(&cluster->refill_lock); | |
7437 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
7438 | spin_unlock(&cluster->refill_lock); | |
7439 | ||
7440 | /* | |
7441 | * make sure this block group isn't part of a metadata | |
7442 | * allocation cluster | |
7443 | */ | |
7444 | cluster = &root->fs_info->meta_alloc_cluster; | |
7445 | spin_lock(&cluster->refill_lock); | |
7446 | btrfs_return_cluster_to_free_space(block_group, cluster); | |
7447 | spin_unlock(&cluster->refill_lock); | |
7448 | ||
1a40e23b | 7449 | path = btrfs_alloc_path(); |
d8926bb3 MF |
7450 | if (!path) { |
7451 | ret = -ENOMEM; | |
7452 | goto out; | |
7453 | } | |
1a40e23b | 7454 | |
10b2f34d | 7455 | inode = lookup_free_space_inode(tree_root, block_group, path); |
0af3d00b | 7456 | if (!IS_ERR(inode)) { |
b532402e TI |
7457 | ret = btrfs_orphan_add(trans, inode); |
7458 | BUG_ON(ret); | |
0af3d00b JB |
7459 | clear_nlink(inode); |
7460 | /* One for the block groups ref */ | |
7461 | spin_lock(&block_group->lock); | |
7462 | if (block_group->iref) { | |
7463 | block_group->iref = 0; | |
7464 | block_group->inode = NULL; | |
7465 | spin_unlock(&block_group->lock); | |
7466 | iput(inode); | |
7467 | } else { | |
7468 | spin_unlock(&block_group->lock); | |
7469 | } | |
7470 | /* One for our lookup ref */ | |
455757c3 | 7471 | btrfs_add_delayed_iput(inode); |
0af3d00b JB |
7472 | } |
7473 | ||
7474 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; | |
7475 | key.offset = block_group->key.objectid; | |
7476 | key.type = 0; | |
7477 | ||
7478 | ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1); | |
7479 | if (ret < 0) | |
7480 | goto out; | |
7481 | if (ret > 0) | |
b3b4aa74 | 7482 | btrfs_release_path(path); |
0af3d00b JB |
7483 | if (ret == 0) { |
7484 | ret = btrfs_del_item(trans, tree_root, path); | |
7485 | if (ret) | |
7486 | goto out; | |
b3b4aa74 | 7487 | btrfs_release_path(path); |
0af3d00b JB |
7488 | } |
7489 | ||
3dfdb934 | 7490 | spin_lock(&root->fs_info->block_group_cache_lock); |
1a40e23b ZY |
7491 | rb_erase(&block_group->cache_node, |
7492 | &root->fs_info->block_group_cache_tree); | |
3dfdb934 | 7493 | spin_unlock(&root->fs_info->block_group_cache_lock); |
817d52f8 | 7494 | |
80eb234a | 7495 | down_write(&block_group->space_info->groups_sem); |
44fb5511 CM |
7496 | /* |
7497 | * we must use list_del_init so people can check to see if they | |
7498 | * are still on the list after taking the semaphore | |
7499 | */ | |
7500 | list_del_init(&block_group->list); | |
80eb234a | 7501 | up_write(&block_group->space_info->groups_sem); |
1a40e23b | 7502 | |
817d52f8 | 7503 | if (block_group->cached == BTRFS_CACHE_STARTED) |
11833d66 | 7504 | wait_block_group_cache_done(block_group); |
817d52f8 JB |
7505 | |
7506 | btrfs_remove_free_space_cache(block_group); | |
7507 | ||
c146afad YZ |
7508 | spin_lock(&block_group->space_info->lock); |
7509 | block_group->space_info->total_bytes -= block_group->key.offset; | |
7510 | block_group->space_info->bytes_readonly -= block_group->key.offset; | |
89a55897 | 7511 | block_group->space_info->disk_total -= block_group->key.offset * factor; |
c146afad | 7512 | spin_unlock(&block_group->space_info->lock); |
283bb197 | 7513 | |
0af3d00b JB |
7514 | memcpy(&key, &block_group->key, sizeof(key)); |
7515 | ||
283bb197 | 7516 | btrfs_clear_space_info_full(root->fs_info); |
c146afad | 7517 | |
fa9c0d79 CM |
7518 | btrfs_put_block_group(block_group); |
7519 | btrfs_put_block_group(block_group); | |
1a40e23b ZY |
7520 | |
7521 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
7522 | if (ret > 0) | |
7523 | ret = -EIO; | |
7524 | if (ret < 0) | |
7525 | goto out; | |
7526 | ||
7527 | ret = btrfs_del_item(trans, root, path); | |
7528 | out: | |
7529 | btrfs_free_path(path); | |
7530 | return ret; | |
7531 | } | |
acce952b | 7532 | |
c59021f8 | 7533 | int btrfs_init_space_info(struct btrfs_fs_info *fs_info) |
7534 | { | |
7535 | struct btrfs_space_info *space_info; | |
1aba86d6 | 7536 | struct btrfs_super_block *disk_super; |
7537 | u64 features; | |
7538 | u64 flags; | |
7539 | int mixed = 0; | |
c59021f8 | 7540 | int ret; |
7541 | ||
6c41761f | 7542 | disk_super = fs_info->super_copy; |
1aba86d6 | 7543 | if (!btrfs_super_root(disk_super)) |
7544 | return 1; | |
c59021f8 | 7545 | |
1aba86d6 | 7546 | features = btrfs_super_incompat_flags(disk_super); |
7547 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) | |
7548 | mixed = 1; | |
c59021f8 | 7549 | |
1aba86d6 | 7550 | flags = BTRFS_BLOCK_GROUP_SYSTEM; |
7551 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); | |
c59021f8 | 7552 | if (ret) |
1aba86d6 | 7553 | goto out; |
c59021f8 | 7554 | |
1aba86d6 | 7555 | if (mixed) { |
7556 | flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA; | |
7557 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); | |
7558 | } else { | |
7559 | flags = BTRFS_BLOCK_GROUP_METADATA; | |
7560 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); | |
7561 | if (ret) | |
7562 | goto out; | |
7563 | ||
7564 | flags = BTRFS_BLOCK_GROUP_DATA; | |
7565 | ret = update_space_info(fs_info, flags, 0, 0, &space_info); | |
7566 | } | |
7567 | out: | |
c59021f8 | 7568 | return ret; |
7569 | } | |
7570 | ||
acce952b | 7571 | int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end) |
7572 | { | |
7573 | return unpin_extent_range(root, start, end); | |
7574 | } | |
7575 | ||
7576 | int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, | |
5378e607 | 7577 | u64 num_bytes, u64 *actual_bytes) |
acce952b | 7578 | { |
5378e607 | 7579 | return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes); |
acce952b | 7580 | } |
f7039b1d LD |
7581 | |
7582 | int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range) | |
7583 | { | |
7584 | struct btrfs_fs_info *fs_info = root->fs_info; | |
7585 | struct btrfs_block_group_cache *cache = NULL; | |
7586 | u64 group_trimmed; | |
7587 | u64 start; | |
7588 | u64 end; | |
7589 | u64 trimmed = 0; | |
7590 | int ret = 0; | |
7591 | ||
7592 | cache = btrfs_lookup_block_group(fs_info, range->start); | |
7593 | ||
7594 | while (cache) { | |
7595 | if (cache->key.objectid >= (range->start + range->len)) { | |
7596 | btrfs_put_block_group(cache); | |
7597 | break; | |
7598 | } | |
7599 | ||
7600 | start = max(range->start, cache->key.objectid); | |
7601 | end = min(range->start + range->len, | |
7602 | cache->key.objectid + cache->key.offset); | |
7603 | ||
7604 | if (end - start >= range->minlen) { | |
7605 | if (!block_group_cache_done(cache)) { | |
7606 | ret = cache_block_group(cache, NULL, root, 0); | |
7607 | if (!ret) | |
7608 | wait_block_group_cache_done(cache); | |
7609 | } | |
7610 | ret = btrfs_trim_block_group(cache, | |
7611 | &group_trimmed, | |
7612 | start, | |
7613 | end, | |
7614 | range->minlen); | |
7615 | ||
7616 | trimmed += group_trimmed; | |
7617 | if (ret) { | |
7618 | btrfs_put_block_group(cache); | |
7619 | break; | |
7620 | } | |
7621 | } | |
7622 | ||
7623 | cache = next_block_group(fs_info->tree_root, cache); | |
7624 | } | |
7625 | ||
7626 | range->len = trimmed; | |
7627 | return ret; | |
7628 | } |