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