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