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