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