]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/btrfs/extent-tree.c
Btrfs: stop trying to shrink delalloc if there are no inodes to reclaim
[mirror_ubuntu-artful-kernel.git] / fs / btrfs / extent-tree.c
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 */
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
26 #include "compat.h"
27 #include "hash.h"
28 #include "ctree.h"
29 #include "disk-io.h"
30 #include "print-tree.h"
31 #include "transaction.h"
32 #include "volumes.h"
33 #include "locking.h"
34 #include "free-space-cache.h"
35
36 static int update_block_group(struct btrfs_trans_handle *trans,
37 struct btrfs_root *root,
38 u64 bytenr, u64 num_bytes, int alloc);
39 static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
40 u64 num_bytes, int reserve, int sinfo);
41 static 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);
47 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
48 struct extent_buffer *leaf,
49 struct btrfs_extent_item *ei);
50 static 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);
55 static 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);
60 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
61 struct btrfs_root *extent_root, u64 alloc_bytes,
62 u64 flags, int force);
63 static int find_next_key(struct btrfs_path *path, int level,
64 struct btrfs_key *key);
65 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
66 int dump_block_groups);
67
68 static noinline int
69 block_group_cache_done(struct btrfs_block_group_cache *cache)
70 {
71 smp_mb();
72 return cache->cached == BTRFS_CACHE_FINISHED;
73 }
74
75 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76 {
77 return (cache->flags & bits) == bits;
78 }
79
80 void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
81 {
82 atomic_inc(&cache->count);
83 }
84
85 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
86 {
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);
91 kfree(cache);
92 }
93 }
94
95 /*
96 * this adds the block group to the fs_info rb tree for the block group
97 * cache
98 */
99 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
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 */
135 static struct btrfs_block_group_cache *
136 block_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 }
167 if (ret)
168 btrfs_get_block_group(ret);
169 spin_unlock(&info->block_group_cache_lock);
170
171 return ret;
172 }
173
174 static int add_excluded_extent(struct btrfs_root *root,
175 u64 start, u64 num_bytes)
176 {
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 }
184
185 static void free_excluded_extents(struct btrfs_root *root,
186 struct btrfs_block_group_cache *cache)
187 {
188 u64 start, end;
189
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);
197 }
198
199 static int exclude_super_stripes(struct btrfs_root *root,
200 struct btrfs_block_group_cache *cache)
201 {
202 u64 bytenr;
203 u64 *logical;
204 int stripe_len;
205 int i, nr, ret;
206
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
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);
221
222 while (nr--) {
223 cache->bytes_super += stripe_len;
224 ret = add_excluded_extent(root, logical[nr],
225 stripe_len);
226 BUG_ON(ret);
227 }
228
229 kfree(logical);
230 }
231 return 0;
232 }
233
234 static struct btrfs_caching_control *
235 get_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
251 static void put_caching_control(struct btrfs_caching_control *ctl)
252 {
253 if (atomic_dec_and_test(&ctl->count))
254 kfree(ctl);
255 }
256
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 */
262 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
263 struct btrfs_fs_info *info, u64 start, u64 end)
264 {
265 u64 extent_start, extent_end, size, total_added = 0;
266 int ret;
267
268 while (start < end) {
269 ret = find_first_extent_bit(info->pinned_extents, start,
270 &extent_start, &extent_end,
271 EXTENT_DIRTY | EXTENT_UPTODATE);
272 if (ret)
273 break;
274
275 if (extent_start <= start) {
276 start = extent_end + 1;
277 } else if (extent_start > start && extent_start < end) {
278 size = extent_start - start;
279 total_added += size;
280 ret = btrfs_add_free_space(block_group, start,
281 size);
282 BUG_ON(ret);
283 start = extent_end + 1;
284 } else {
285 break;
286 }
287 }
288
289 if (start < end) {
290 size = end - start;
291 total_added += size;
292 ret = btrfs_add_free_space(block_group, start, size);
293 BUG_ON(ret);
294 }
295
296 return total_added;
297 }
298
299 static int caching_kthread(void *data)
300 {
301 struct btrfs_block_group_cache *block_group = data;
302 struct btrfs_fs_info *fs_info = block_group->fs_info;
303 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
304 struct btrfs_root *extent_root = fs_info->extent_root;
305 struct btrfs_path *path;
306 struct extent_buffer *leaf;
307 struct btrfs_key key;
308 u64 total_found = 0;
309 u64 last = 0;
310 u32 nritems;
311 int ret = 0;
312
313 path = btrfs_alloc_path();
314 if (!path)
315 return -ENOMEM;
316
317 exclude_super_stripes(extent_root, block_group);
318 spin_lock(&block_group->space_info->lock);
319 block_group->space_info->bytes_readonly += block_group->bytes_super;
320 spin_unlock(&block_group->space_info->lock);
321
322 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
323
324 /*
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
329 */
330 path->skip_locking = 1;
331 path->search_commit_root = 1;
332 path->reada = 2;
333
334 key.objectid = last;
335 key.offset = 0;
336 key.type = BTRFS_EXTENT_ITEM_KEY;
337 again:
338 mutex_lock(&caching_ctl->mutex);
339 /* need to make sure the commit_root doesn't disappear */
340 down_read(&fs_info->extent_commit_sem);
341
342 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
343 if (ret < 0)
344 goto err;
345
346 leaf = path->nodes[0];
347 nritems = btrfs_header_nritems(leaf);
348
349 while (1) {
350 smp_mb();
351 if (fs_info->closing > 1) {
352 last = (u64)-1;
353 break;
354 }
355
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)
361 break;
362
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))
368 schedule_timeout(1);
369 else
370 cond_resched();
371 goto again;
372 }
373
374 if (key.objectid < block_group->key.objectid) {
375 path->slots[0]++;
376 continue;
377 }
378
379 if (key.objectid >= block_group->key.objectid +
380 block_group->key.offset)
381 break;
382
383 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
384 total_found += add_new_free_space(block_group,
385 fs_info, last,
386 key.objectid);
387 last = key.objectid + key.offset;
388
389 if (total_found > (1024 * 1024 * 2)) {
390 total_found = 0;
391 wake_up(&caching_ctl->wait);
392 }
393 }
394 path->slots[0]++;
395 }
396 ret = 0;
397
398 total_found += add_new_free_space(block_group, fs_info, last,
399 block_group->key.objectid +
400 block_group->key.offset);
401 caching_ctl->progress = (u64)-1;
402
403 spin_lock(&block_group->lock);
404 block_group->caching_ctl = NULL;
405 block_group->cached = BTRFS_CACHE_FINISHED;
406 spin_unlock(&block_group->lock);
407
408 err:
409 btrfs_free_path(path);
410 up_read(&fs_info->extent_commit_sem);
411
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);
419 btrfs_put_block_group(block_group);
420
421 return 0;
422 }
423
424 static int cache_block_group(struct btrfs_block_group_cache *cache)
425 {
426 struct btrfs_fs_info *fs_info = cache->fs_info;
427 struct btrfs_caching_control *caching_ctl;
428 struct task_struct *tsk;
429 int ret = 0;
430
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
446 spin_lock(&cache->lock);
447 if (cache->cached != BTRFS_CACHE_NO) {
448 spin_unlock(&cache->lock);
449 kfree(caching_ctl);
450 return 0;
451 }
452 cache->caching_ctl = caching_ctl;
453 cache->cached = BTRFS_CACHE_STARTED;
454 spin_unlock(&cache->lock);
455
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);
461 btrfs_get_block_group(cache);
462
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
471 return ret;
472 }
473
474 /*
475 * return the block group that starts at or after bytenr
476 */
477 static struct btrfs_block_group_cache *
478 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
479 {
480 struct btrfs_block_group_cache *cache;
481
482 cache = block_group_cache_tree_search(info, bytenr, 0);
483
484 return cache;
485 }
486
487 /*
488 * return the block group that contains the given bytenr
489 */
490 struct btrfs_block_group_cache *btrfs_lookup_block_group(
491 struct btrfs_fs_info *info,
492 u64 bytenr)
493 {
494 struct btrfs_block_group_cache *cache;
495
496 cache = block_group_cache_tree_search(info, bytenr, 1);
497
498 return cache;
499 }
500
501 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
502 u64 flags)
503 {
504 struct list_head *head = &info->space_info;
505 struct btrfs_space_info *found;
506
507 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
508 BTRFS_BLOCK_GROUP_METADATA;
509
510 rcu_read_lock();
511 list_for_each_entry_rcu(found, head, list) {
512 if (found->flags == flags) {
513 rcu_read_unlock();
514 return found;
515 }
516 }
517 rcu_read_unlock();
518 return NULL;
519 }
520
521 /*
522 * after adding space to the filesystem, we need to clear the full flags
523 * on all the space infos.
524 */
525 void 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
536 static 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
545 u64 btrfs_find_block_group(struct btrfs_root *root,
546 u64 search_start, u64 search_hint, int owner)
547 {
548 struct btrfs_block_group_cache *cache;
549 u64 used;
550 u64 last = max(search_hint, search_start);
551 u64 group_start = 0;
552 int full_search = 0;
553 int factor = 9;
554 int wrapped = 0;
555 again:
556 while (1) {
557 cache = btrfs_lookup_first_block_group(root->fs_info, last);
558 if (!cache)
559 break;
560
561 spin_lock(&cache->lock);
562 last = cache->key.objectid + cache->key.offset;
563 used = btrfs_block_group_used(&cache->item);
564
565 if ((full_search || !cache->ro) &&
566 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
567 if (used + cache->pinned + cache->reserved <
568 div_factor(cache->key.offset, factor)) {
569 group_start = cache->key.objectid;
570 spin_unlock(&cache->lock);
571 btrfs_put_block_group(cache);
572 goto found;
573 }
574 }
575 spin_unlock(&cache->lock);
576 btrfs_put_block_group(cache);
577 cond_resched();
578 }
579 if (!wrapped) {
580 last = search_start;
581 wrapped = 1;
582 goto again;
583 }
584 if (!full_search && factor < 10) {
585 last = search_start;
586 full_search = 1;
587 factor = 10;
588 goto again;
589 }
590 found:
591 return group_start;
592 }
593
594 /* simple helper to search for an existing extent at a given offset */
595 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
596 {
597 int ret;
598 struct btrfs_key key;
599 struct btrfs_path *path;
600
601 path = btrfs_alloc_path();
602 BUG_ON(!path);
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);
608 btrfs_free_path(path);
609 return ret;
610 }
611
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 */
621 int 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 }
647 again:
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);
708 out:
709 WARN_ON(num_refs == 0);
710 if (refs)
711 *refs = num_refs;
712 if (flags)
713 *flags = extent_flags;
714 out_free:
715 btrfs_free_path(path);
716 return ret;
717 }
718
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 *
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 *
778 * File extents can be referenced by:
779 *
780 * - multiple snapshots, subvolumes, or different generations in one subvol
781 * - different files inside a single subvolume
782 * - different offsets inside a file (bookend extents in file.c)
783 *
784 * The extent ref structure for the implicit back refs has fields for:
785 *
786 * - Objectid of the subvolume root
787 * - objectid of the file holding the reference
788 * - original offset in the file
789 * - how many bookend extents
790 *
791 * The key offset for the implicit back refs is hash of the first
792 * three fields.
793 *
794 * The extent ref structure for the full back refs has field for:
795 *
796 * - number of pointers in the tree leaf
797 *
798 * The key offset for the implicit back refs is the first byte of
799 * the tree leaf
800 *
801 * When a file extent is allocated, The implicit back refs is used.
802 * the fields are filled in:
803 *
804 * (root_key.objectid, inode objectid, offset in file, 1)
805 *
806 * When a file extent is removed file truncation, we find the
807 * corresponding implicit back refs and check the following fields:
808 *
809 * (btrfs_header_owner(leaf), inode objectid, offset in file)
810 *
811 * Btree extents can be referenced by:
812 *
813 * - Different subvolumes
814 *
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.
819 *
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.
823 */
824
825 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
826 static 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)
830 {
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;
836 struct btrfs_key key;
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
908 static 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);
915 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
916 lenum = cpu_to_le64(owner);
917 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
918 lenum = cpu_to_le64(offset);
919 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
920
921 return ((u64)high_crc << 31) ^ (u64)low_crc;
922 }
923
924 static 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
932 static 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
943 static 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;
952 struct extent_buffer *leaf;
953 u32 nritems;
954 int ret;
955 int recow;
956 int err = -ENOENT;
957
958 key.objectid = bytenr;
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 }
967 again:
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 }
974
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;
990 }
991
992 leaf = path->nodes[0];
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]++;
1025 }
1026 fail:
1027 return err;
1028 }
1029
1030 static 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)
1036 {
1037 struct btrfs_key key;
1038 struct extent_buffer *leaf;
1039 u32 size;
1040 u32 num_refs;
1041 int ret;
1042
1043 key.objectid = bytenr;
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 }
1054
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;
1062 ref = btrfs_item_ptr(leaf, path->slots[0],
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);
1070 }
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;
1085
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);
1100 }
1101 }
1102 btrfs_mark_buffer_dirty(leaf);
1103 ret = 0;
1104 fail:
1105 btrfs_release_path(root, path);
1106 return ret;
1107 }
1108
1109 static 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)
1113 {
1114 struct btrfs_key key;
1115 struct btrfs_extent_data_ref *ref1 = NULL;
1116 struct btrfs_shared_data_ref *ref2 = NULL;
1117 struct extent_buffer *leaf;
1118 u32 num_refs = 0;
1119 int ret = 0;
1120
1121 leaf = path->nodes[0];
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
1143 BUG_ON(num_refs < refs_to_drop);
1144 num_refs -= refs_to_drop;
1145
1146 if (num_refs == 0) {
1147 ret = btrfs_del_item(trans, root, path);
1148 } else {
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
1161 btrfs_mark_buffer_dirty(leaf);
1162 }
1163 return ret;
1164 }
1165
1166 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1167 struct btrfs_path *path,
1168 struct btrfs_extent_inline_ref *iref)
1169 {
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);
1201 #endif
1202 } else {
1203 WARN_ON(1);
1204 }
1205 return num_refs;
1206 }
1207
1208 static 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)
1213 {
1214 struct btrfs_key key;
1215 int ret;
1216
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;
1224 }
1225
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 }
1237 #endif
1238 return ret;
1239 }
1240
1241 static 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)
1246 {
1247 struct btrfs_key key;
1248 int ret;
1249
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);
1261 return ret;
1262 }
1263
1264 static inline int extent_ref_type(u64 parent, u64 owner)
1265 {
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;
1279 }
1280
1281 static int find_next_key(struct btrfs_path *path, int level,
1282 struct btrfs_key *key)
1283
1284 {
1285 for (; level < BTRFS_MAX_LEVEL; level++) {
1286 if (!path->nodes[level])
1287 break;
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 }
1301
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 */
1315 static noinline_for_stack
1316 int 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;
1337
1338 key.objectid = bytenr;
1339 key.type = BTRFS_EXTENT_ITEM_KEY;
1340 key.offset = num_bytes;
1341
1342 want = extent_ref_type(parent, owner);
1343 if (insert) {
1344 extra_size = btrfs_extent_inline_ref_size(want);
1345 path->keep_locks = 1;
1346 } else
1347 extra_size = -1;
1348 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1349 if (ret < 0) {
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
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 */
1447 if (find_next_key(path, 0, &key) == 0 &&
1448 key.objectid == bytenr &&
1449 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1450 err = -EAGAIN;
1451 goto out;
1452 }
1453 }
1454 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1455 out:
1456 if (insert) {
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 */
1466 static noinline_for_stack
1467 int 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
1531 static 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)
1544 return ret;
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);
1555 }
1556 return ret;
1557 }
1558
1559 /*
1560 * helper to update/remove inline back ref
1561 */
1562 static noinline_for_stack
1563 int 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);
1602 }
1603
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
1628 static noinline_for_stack
1629 int 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);
1652 }
1653 return ret;
1654 }
1655
1656 static 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 }
1674
1675 static 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;
1682
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
1695 static void btrfs_issue_discard(struct block_device *bdev,
1696 u64 start, u64 len)
1697 {
1698 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL,
1699 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
1700 }
1701
1702 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1703 u64 num_bytes)
1704 {
1705 int ret;
1706 u64 map_length = num_bytes;
1707 struct btrfs_multi_bio *multi = NULL;
1708
1709 if (!btrfs_test_opt(root, DISCARD))
1710 return 0;
1711
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;
1731 }
1732
1733 int 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
1754 static 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);
1793
1794 btrfs_mark_buffer_dirty(leaf);
1795 btrfs_release_path(root->fs_info->extent_root, path);
1796
1797 path->reada = 1;
1798 path->leave_spinning = 1;
1799
1800 /* now insert the actual backref */
1801 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1802 path, bytenr, parent, root_objectid,
1803 owner, offset, refs_to_add);
1804 BUG_ON(ret);
1805 out:
1806 btrfs_free_path(path);
1807 return err;
1808 }
1809
1810 static 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)
1815 {
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);
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
1860 static 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
1878 static 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;
1888 int ret;
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);
1929
1930 btrfs_mark_buffer_dirty(leaf);
1931 out:
1932 btrfs_free_path(path);
1933 return err;
1934 }
1935
1936 static 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)
1941 {
1942 int ret = 0;
1943 struct btrfs_delayed_tree_ref *ref;
1944 struct btrfs_key ins;
1945 u64 parent = 0;
1946 u64 ref_root = 0;
1947
1948 ins.objectid = node->bytenr;
1949 ins.offset = node->num_bytes;
1950 ins.type = BTRFS_EXTENT_ITEM_KEY;
1951
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);
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 }
1978 return ret;
1979 }
1980
1981 /* helper function to actually process a single delayed ref entry */
1982 static 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)
1987 {
1988 int ret;
1989 if (btrfs_delayed_ref_is_head(node)) {
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 */
1997 BUG_ON(extent_op);
1998 head = btrfs_delayed_node_to_head(node);
1999 if (insert_reserved) {
2000 btrfs_pin_extent(root, node->bytenr,
2001 node->num_bytes, 1);
2002 if (head->is_data) {
2003 ret = btrfs_del_csums(trans, root,
2004 node->bytenr,
2005 node->num_bytes);
2006 BUG_ON(ret);
2007 }
2008 }
2009 mutex_unlock(&head->mutex);
2010 return 0;
2011 }
2012
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;
2024 }
2025
2026 static noinline struct btrfs_delayed_ref_node *
2027 select_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;
2032 again:
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;
2046 if (ref->action == action)
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
2057 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 struct list_head *cluster)
2060 {
2061 struct btrfs_delayed_ref_root *delayed_refs;
2062 struct btrfs_delayed_ref_node *ref;
2063 struct btrfs_delayed_ref_head *locked_ref = NULL;
2064 struct btrfs_delayed_extent_op *extent_op;
2065 int ret;
2066 int count = 0;
2067 int must_insert_reserved = 0;
2068
2069 delayed_refs = &trans->transaction->delayed_refs;
2070 while (1) {
2071 if (!locked_ref) {
2072 /* pick a new head ref from the cluster list */
2073 if (list_empty(cluster))
2074 break;
2075
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;
2093 }
2094 }
2095
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;
2102
2103 extent_op = locked_ref->extent_op;
2104 locked_ref->extent_op = NULL;
2105
2106 /*
2107 * locked_ref is the head node, so we have to go one
2108 * node back for any delayed ref updates
2109 */
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;
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
2136 list_del_init(&locked_ref->cluster);
2137 locked_ref = NULL;
2138 }
2139
2140 ref->in_tree = 0;
2141 rb_erase(&ref->rb_node, &delayed_refs->root);
2142 delayed_refs->num_entries--;
2143
2144 spin_unlock(&delayed_refs->lock);
2145
2146 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2147 must_insert_reserved);
2148 BUG_ON(ret);
2149
2150 btrfs_put_delayed_ref(ref);
2151 kfree(extent_op);
2152 count++;
2153
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 */
2167 int 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);
2183 again:
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;
2193
2194 /*
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
2199 */
2200 ret = btrfs_find_ref_cluster(trans, &cluster,
2201 delayed_refs->run_delayed_start);
2202 if (ret)
2203 break;
2204
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;
2212 }
2213
2214 if (run_all) {
2215 node = rb_first(&delayed_refs->root);
2216 if (!node)
2217 goto out;
2218 count = (unsigned long)-1;
2219
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;
2225
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);
2234 cond_resched();
2235 goto again;
2236 }
2237 node = rb_next(node);
2238 }
2239 spin_unlock(&delayed_refs->lock);
2240 schedule_timeout(1);
2241 goto again;
2242 }
2243 out:
2244 spin_unlock(&delayed_refs->lock);
2245 return 0;
2246 }
2247
2248 int 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
2271 static 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;
2329 out_unlock:
2330 mutex_unlock(&head->mutex);
2331 out:
2332 spin_unlock(&delayed_refs->lock);
2333 return ret;
2334 }
2335
2336 static 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)
2340 {
2341 struct btrfs_root *extent_root = root->fs_info->extent_root;
2342 struct extent_buffer *leaf;
2343 struct btrfs_extent_data_ref *ref;
2344 struct btrfs_extent_inline_ref *iref;
2345 struct btrfs_extent_item *ei;
2346 struct btrfs_key key;
2347 u32 item_size;
2348 int ret;
2349
2350 key.objectid = bytenr;
2351 key.offset = (u64)-1;
2352 key.type = BTRFS_EXTENT_ITEM_KEY;
2353
2354 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2355 if (ret < 0)
2356 goto out;
2357 BUG_ON(ret == 0);
2358
2359 ret = -ENOENT;
2360 if (path->slots[0] == 0)
2361 goto out;
2362
2363 path->slots[0]--;
2364 leaf = path->nodes[0];
2365 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2366
2367 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2368 goto out;
2369
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);
2379
2380 if (item_size != sizeof(*ei) +
2381 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2382 goto out;
2383
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;
2403 out:
2404 return ret;
2405 }
2406
2407 int 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)
2423 goto out;
2424
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;
2432 }
2433
2434 if (ret != -ENOENT || ret2 != -ENOENT)
2435 ret = 0;
2436 out:
2437 btrfs_free_path(path);
2438 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2439 WARN_ON(ret > 0);
2440 return ret;
2441 }
2442
2443 #if 0
2444 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2445 struct extent_buffer *buf, u32 nr_extents)
2446 {
2447 struct btrfs_key key;
2448 struct btrfs_file_extent_item *fi;
2449 u64 root_gen;
2450 u32 nritems;
2451 int i;
2452 int level;
2453 int ret = 0;
2454 int shared = 0;
2455
2456 if (!root->ref_cows)
2457 return 0;
2458
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
2467 level = btrfs_header_level(buf);
2468 nritems = btrfs_header_nritems(buf);
2469
2470 if (level == 0) {
2471 struct btrfs_leaf_ref *ref;
2472 struct btrfs_extent_info *info;
2473
2474 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2475 if (!ref) {
2476 ret = -ENOMEM;
2477 goto out;
2478 }
2479
2480 ref->root_gen = root_gen;
2481 ref->bytenr = buf->start;
2482 ref->owner = btrfs_header_owner(buf);
2483 ref->generation = btrfs_header_generation(buf);
2484 ref->nritems = nr_extents;
2485 info = ref->extents;
2486
2487 for (i = 0; nr_extents > 0 && i < nritems; i++) {
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
2509 ret = btrfs_add_leaf_ref(root, ref, shared);
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 }
2518 WARN_ON(ret);
2519 btrfs_free_leaf_ref(root, ref);
2520 }
2521 out:
2522 return ret;
2523 }
2524
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.
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.
2541 */
2542 struct refsort {
2543 u64 bytenr;
2544 u32 slot;
2545 };
2546
2547 /*
2548 * for passing into sort()
2549 */
2550 static 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 }
2561 #endif
2562
2563 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2564 struct btrfs_root *root,
2565 struct extent_buffer *buf,
2566 int full_backref, int inc)
2567 {
2568 u64 bytenr;
2569 u64 num_bytes;
2570 u64 parent;
2571 u64 ref_root;
2572 u32 nritems;
2573 struct btrfs_key key;
2574 struct btrfs_file_extent_item *fi;
2575 int i;
2576 int level;
2577 int ret = 0;
2578 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2579 u64, u64, u64, u64, u64, u64);
2580
2581 ref_root = btrfs_header_owner(buf);
2582 nritems = btrfs_header_nritems(buf);
2583 level = btrfs_header_level(buf);
2584
2585 if (!root->ref_cows && level == 0)
2586 return 0;
2587
2588 if (inc)
2589 process_func = btrfs_inc_extent_ref;
2590 else
2591 process_func = btrfs_free_extent;
2592
2593 if (full_backref)
2594 parent = buf->start;
2595 else
2596 parent = 0;
2597
2598 for (i = 0; i < nritems; i++) {
2599 if (level == 0) {
2600 btrfs_item_key_to_cpu(buf, &key, i);
2601 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2602 continue;
2603 fi = btrfs_item_ptr(buf, i,
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;
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);
2617 if (ret)
2618 goto fail;
2619 } else {
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);
2624 if (ret)
2625 goto fail;
2626 }
2627 }
2628 return 0;
2629 fail:
2630 BUG();
2631 return ret;
2632 }
2633
2634 int 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
2640 int 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);
2644 }
2645
2646 static 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;
2652 struct btrfs_root *extent_root = root->fs_info->extent_root;
2653 unsigned long bi;
2654 struct extent_buffer *leaf;
2655
2656 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2657 if (ret < 0)
2658 goto fail;
2659 BUG_ON(ret);
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);
2665 btrfs_release_path(extent_root, path);
2666 fail:
2667 if (ret)
2668 return ret;
2669 return 0;
2670
2671 }
2672
2673 static struct btrfs_block_group_cache *
2674 next_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);
2684 btrfs_get_block_group(cache);
2685 } else
2686 cache = NULL;
2687 spin_unlock(&root->fs_info->block_group_cache_lock);
2688 return cache;
2689 }
2690
2691 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2692 struct btrfs_root *root)
2693 {
2694 struct btrfs_block_group_cache *cache;
2695 int err = 0;
2696 struct btrfs_path *path;
2697 u64 last = 0;
2698
2699 path = btrfs_alloc_path();
2700 if (!path)
2701 return -ENOMEM;
2702
2703 while (1) {
2704 if (last == 0) {
2705 err = btrfs_run_delayed_refs(trans, root,
2706 (unsigned long)-1);
2707 BUG_ON(err);
2708 }
2709
2710 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2711 while (cache) {
2712 if (cache->dirty)
2713 break;
2714 cache = next_block_group(root, cache);
2715 }
2716 if (!cache) {
2717 if (last == 0)
2718 break;
2719 last = 0;
2720 continue;
2721 }
2722
2723 cache->dirty = 0;
2724 last = cache->key.objectid + cache->key.offset;
2725
2726 err = write_one_cache_group(trans, root, path, cache);
2727 BUG_ON(err);
2728 btrfs_put_block_group(cache);
2729 }
2730
2731 btrfs_free_path(path);
2732 return 0;
2733 }
2734
2735 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2736 {
2737 struct btrfs_block_group_cache *block_group;
2738 int readonly = 0;
2739
2740 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2741 if (!block_group || block_group->ro)
2742 readonly = 1;
2743 if (block_group)
2744 btrfs_put_block_group(block_group);
2745 return readonly;
2746 }
2747
2748 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2749 u64 total_bytes, u64 bytes_used,
2750 struct btrfs_space_info **space_info)
2751 {
2752 struct btrfs_space_info *found;
2753 int i;
2754 int factor;
2755
2756 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2757 BTRFS_BLOCK_GROUP_RAID10))
2758 factor = 2;
2759 else
2760 factor = 1;
2761
2762 found = __find_space_info(info, flags);
2763 if (found) {
2764 spin_lock(&found->lock);
2765 found->total_bytes += total_bytes;
2766 found->bytes_used += bytes_used;
2767 found->disk_used += bytes_used * factor;
2768 found->full = 0;
2769 spin_unlock(&found->lock);
2770 *space_info = found;
2771 return 0;
2772 }
2773 found = kzalloc(sizeof(*found), GFP_NOFS);
2774 if (!found)
2775 return -ENOMEM;
2776
2777 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2778 INIT_LIST_HEAD(&found->block_groups[i]);
2779 init_rwsem(&found->groups_sem);
2780 spin_lock_init(&found->lock);
2781 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2782 BTRFS_BLOCK_GROUP_SYSTEM |
2783 BTRFS_BLOCK_GROUP_METADATA);
2784 found->total_bytes = total_bytes;
2785 found->bytes_used = bytes_used;
2786 found->disk_used = bytes_used * factor;
2787 found->bytes_pinned = 0;
2788 found->bytes_reserved = 0;
2789 found->bytes_readonly = 0;
2790 found->bytes_may_use = 0;
2791 found->full = 0;
2792 found->force_alloc = 0;
2793 *space_info = found;
2794 list_add_rcu(&found->list, &info->space_info);
2795 atomic_set(&found->caching_threads, 0);
2796 return 0;
2797 }
2798
2799 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2800 {
2801 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2802 BTRFS_BLOCK_GROUP_RAID1 |
2803 BTRFS_BLOCK_GROUP_RAID10 |
2804 BTRFS_BLOCK_GROUP_DUP);
2805 if (extra_flags) {
2806 if (flags & BTRFS_BLOCK_GROUP_DATA)
2807 fs_info->avail_data_alloc_bits |= extra_flags;
2808 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2809 fs_info->avail_metadata_alloc_bits |= extra_flags;
2810 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2811 fs_info->avail_system_alloc_bits |= extra_flags;
2812 }
2813 }
2814
2815 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2816 {
2817 u64 num_devices = root->fs_info->fs_devices->rw_devices;
2818
2819 if (num_devices == 1)
2820 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2821 if (num_devices < 4)
2822 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2823
2824 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2825 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2826 BTRFS_BLOCK_GROUP_RAID10))) {
2827 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2828 }
2829
2830 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2831 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2832 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2833 }
2834
2835 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2836 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2837 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2838 (flags & BTRFS_BLOCK_GROUP_DUP)))
2839 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2840 return flags;
2841 }
2842
2843 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
2844 {
2845 if (flags & BTRFS_BLOCK_GROUP_DATA)
2846 flags |= root->fs_info->avail_data_alloc_bits &
2847 root->fs_info->data_alloc_profile;
2848 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2849 flags |= root->fs_info->avail_system_alloc_bits &
2850 root->fs_info->system_alloc_profile;
2851 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
2852 flags |= root->fs_info->avail_metadata_alloc_bits &
2853 root->fs_info->metadata_alloc_profile;
2854 return btrfs_reduce_alloc_profile(root, flags);
2855 }
2856
2857 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
2858 {
2859 u64 flags;
2860
2861 if (data)
2862 flags = BTRFS_BLOCK_GROUP_DATA;
2863 else if (root == root->fs_info->chunk_root)
2864 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2865 else
2866 flags = BTRFS_BLOCK_GROUP_METADATA;
2867
2868 return get_alloc_profile(root, flags);
2869 }
2870
2871 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2872 {
2873 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2874 BTRFS_BLOCK_GROUP_DATA);
2875 }
2876
2877 /*
2878 * This will check the space that the inode allocates from to make sure we have
2879 * enough space for bytes.
2880 */
2881 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
2882 {
2883 struct btrfs_space_info *data_sinfo;
2884 struct btrfs_root *root = BTRFS_I(inode)->root;
2885 u64 used;
2886 int ret = 0, committed = 0;
2887
2888 /* make sure bytes are sectorsize aligned */
2889 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2890
2891 data_sinfo = BTRFS_I(inode)->space_info;
2892 if (!data_sinfo)
2893 goto alloc;
2894
2895 again:
2896 /* make sure we have enough space to handle the data first */
2897 spin_lock(&data_sinfo->lock);
2898 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
2899 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
2900 data_sinfo->bytes_may_use;
2901
2902 if (used + bytes > data_sinfo->total_bytes) {
2903 struct btrfs_trans_handle *trans;
2904
2905 /*
2906 * if we don't have enough free bytes in this space then we need
2907 * to alloc a new chunk.
2908 */
2909 if (!data_sinfo->full) {
2910 u64 alloc_target;
2911
2912 data_sinfo->force_alloc = 1;
2913 spin_unlock(&data_sinfo->lock);
2914 alloc:
2915 alloc_target = btrfs_get_alloc_profile(root, 1);
2916 trans = btrfs_join_transaction(root, 1);
2917 if (IS_ERR(trans))
2918 return PTR_ERR(trans);
2919
2920 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2921 bytes + 2 * 1024 * 1024,
2922 alloc_target, 0);
2923 btrfs_end_transaction(trans, root);
2924 if (ret < 0)
2925 return ret;
2926
2927 if (!data_sinfo) {
2928 btrfs_set_inode_space_info(root, inode);
2929 data_sinfo = BTRFS_I(inode)->space_info;
2930 }
2931 goto again;
2932 }
2933 spin_unlock(&data_sinfo->lock);
2934
2935 /* commit the current transaction and try again */
2936 if (!committed && !root->fs_info->open_ioctl_trans) {
2937 committed = 1;
2938 trans = btrfs_join_transaction(root, 1);
2939 if (IS_ERR(trans))
2940 return PTR_ERR(trans);
2941 ret = btrfs_commit_transaction(trans, root);
2942 if (ret)
2943 return ret;
2944 goto again;
2945 }
2946
2947 #if 0 /* I hope we never need this code again, just in case */
2948 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
2949 "%llu bytes_reserved, " "%llu bytes_pinned, "
2950 "%llu bytes_readonly, %llu may use %llu total\n",
2951 (unsigned long long)bytes,
2952 (unsigned long long)data_sinfo->bytes_used,
2953 (unsigned long long)data_sinfo->bytes_reserved,
2954 (unsigned long long)data_sinfo->bytes_pinned,
2955 (unsigned long long)data_sinfo->bytes_readonly,
2956 (unsigned long long)data_sinfo->bytes_may_use,
2957 (unsigned long long)data_sinfo->total_bytes);
2958 #endif
2959 return -ENOSPC;
2960 }
2961 data_sinfo->bytes_may_use += bytes;
2962 BTRFS_I(inode)->reserved_bytes += bytes;
2963 spin_unlock(&data_sinfo->lock);
2964
2965 return 0;
2966 }
2967
2968 /*
2969 * called when we are clearing an delalloc extent from the
2970 * inode's io_tree or there was an error for whatever reason
2971 * after calling btrfs_check_data_free_space
2972 */
2973 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
2974 {
2975 struct btrfs_root *root = BTRFS_I(inode)->root;
2976 struct btrfs_space_info *data_sinfo;
2977
2978 /* make sure bytes are sectorsize aligned */
2979 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2980
2981 data_sinfo = BTRFS_I(inode)->space_info;
2982 spin_lock(&data_sinfo->lock);
2983 data_sinfo->bytes_may_use -= bytes;
2984 BTRFS_I(inode)->reserved_bytes -= bytes;
2985 spin_unlock(&data_sinfo->lock);
2986 }
2987
2988 static void force_metadata_allocation(struct btrfs_fs_info *info)
2989 {
2990 struct list_head *head = &info->space_info;
2991 struct btrfs_space_info *found;
2992
2993 rcu_read_lock();
2994 list_for_each_entry_rcu(found, head, list) {
2995 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
2996 found->force_alloc = 1;
2997 }
2998 rcu_read_unlock();
2999 }
3000
3001 static int should_alloc_chunk(struct btrfs_space_info *sinfo,
3002 u64 alloc_bytes)
3003 {
3004 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3005
3006 if (sinfo->bytes_used + sinfo->bytes_reserved +
3007 alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3008 return 0;
3009
3010 if (sinfo->bytes_used + sinfo->bytes_reserved +
3011 alloc_bytes < div_factor(num_bytes, 8))
3012 return 0;
3013
3014 return 1;
3015 }
3016
3017 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3018 struct btrfs_root *extent_root, u64 alloc_bytes,
3019 u64 flags, int force)
3020 {
3021 struct btrfs_space_info *space_info;
3022 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3023 int ret = 0;
3024
3025 mutex_lock(&fs_info->chunk_mutex);
3026
3027 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3028
3029 space_info = __find_space_info(extent_root->fs_info, flags);
3030 if (!space_info) {
3031 ret = update_space_info(extent_root->fs_info, flags,
3032 0, 0, &space_info);
3033 BUG_ON(ret);
3034 }
3035 BUG_ON(!space_info);
3036
3037 spin_lock(&space_info->lock);
3038 if (space_info->force_alloc)
3039 force = 1;
3040 if (space_info->full) {
3041 spin_unlock(&space_info->lock);
3042 goto out;
3043 }
3044
3045 if (!force && !should_alloc_chunk(space_info, alloc_bytes)) {
3046 spin_unlock(&space_info->lock);
3047 goto out;
3048 }
3049 spin_unlock(&space_info->lock);
3050
3051 /*
3052 * if we're doing a data chunk, go ahead and make sure that
3053 * we keep a reasonable number of metadata chunks allocated in the
3054 * FS as well.
3055 */
3056 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3057 fs_info->data_chunk_allocations++;
3058 if (!(fs_info->data_chunk_allocations %
3059 fs_info->metadata_ratio))
3060 force_metadata_allocation(fs_info);
3061 }
3062
3063 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3064 spin_lock(&space_info->lock);
3065 if (ret)
3066 space_info->full = 1;
3067 else
3068 ret = 1;
3069 space_info->force_alloc = 0;
3070 spin_unlock(&space_info->lock);
3071 out:
3072 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3073 return ret;
3074 }
3075
3076 static int maybe_allocate_chunk(struct btrfs_trans_handle *trans,
3077 struct btrfs_root *root,
3078 struct btrfs_space_info *sinfo, u64 num_bytes)
3079 {
3080 int ret;
3081 int end_trans = 0;
3082
3083 if (sinfo->full)
3084 return 0;
3085
3086 spin_lock(&sinfo->lock);
3087 ret = should_alloc_chunk(sinfo, num_bytes + 2 * 1024 * 1024);
3088 spin_unlock(&sinfo->lock);
3089 if (!ret)
3090 return 0;
3091
3092 if (!trans) {
3093 trans = btrfs_join_transaction(root, 1);
3094 BUG_ON(IS_ERR(trans));
3095 end_trans = 1;
3096 }
3097
3098 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3099 num_bytes + 2 * 1024 * 1024,
3100 get_alloc_profile(root, sinfo->flags), 0);
3101
3102 if (end_trans)
3103 btrfs_end_transaction(trans, root);
3104
3105 return ret == 1 ? 1 : 0;
3106 }
3107
3108 /*
3109 * shrink metadata reservation for delalloc
3110 */
3111 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3112 struct btrfs_root *root, u64 to_reclaim)
3113 {
3114 struct btrfs_block_rsv *block_rsv;
3115 u64 reserved;
3116 u64 max_reclaim;
3117 u64 reclaimed = 0;
3118 int no_reclaim = 0;
3119 int pause = 1;
3120 int ret;
3121
3122 block_rsv = &root->fs_info->delalloc_block_rsv;
3123 spin_lock(&block_rsv->lock);
3124 reserved = block_rsv->reserved;
3125 spin_unlock(&block_rsv->lock);
3126
3127 if (reserved == 0)
3128 return 0;
3129
3130 max_reclaim = min(reserved, to_reclaim);
3131
3132 while (1) {
3133 ret = btrfs_start_one_delalloc_inode(root, trans ? 1 : 0);
3134 if (!ret) {
3135 if (no_reclaim > 2)
3136 break;
3137 no_reclaim++;
3138 __set_current_state(TASK_INTERRUPTIBLE);
3139 schedule_timeout(pause);
3140 pause <<= 1;
3141 if (pause > HZ / 10)
3142 pause = HZ / 10;
3143 } else {
3144 no_reclaim = 0;
3145 pause = 1;
3146 }
3147
3148 spin_lock(&block_rsv->lock);
3149 if (reserved > block_rsv->reserved)
3150 reclaimed = reserved - block_rsv->reserved;
3151 reserved = block_rsv->reserved;
3152 spin_unlock(&block_rsv->lock);
3153
3154 if (reserved == 0 || reclaimed >= max_reclaim)
3155 break;
3156
3157 if (trans && trans->transaction->blocked)
3158 return -EAGAIN;
3159 }
3160 return reclaimed >= to_reclaim;
3161 }
3162
3163 static int should_retry_reserve(struct btrfs_trans_handle *trans,
3164 struct btrfs_root *root,
3165 struct btrfs_block_rsv *block_rsv,
3166 u64 num_bytes, int *retries)
3167 {
3168 struct btrfs_space_info *space_info = block_rsv->space_info;
3169 int ret;
3170
3171 if ((*retries) > 2)
3172 return -ENOSPC;
3173
3174 ret = maybe_allocate_chunk(trans, root, space_info, num_bytes);
3175 if (ret)
3176 return 1;
3177
3178 if (trans && trans->transaction->in_commit)
3179 return -ENOSPC;
3180
3181 ret = shrink_delalloc(trans, root, num_bytes);
3182 if (ret)
3183 return ret;
3184
3185 spin_lock(&space_info->lock);
3186 if (space_info->bytes_pinned < num_bytes)
3187 ret = 1;
3188 spin_unlock(&space_info->lock);
3189 if (ret)
3190 return -ENOSPC;
3191
3192 (*retries)++;
3193
3194 if (trans)
3195 return -EAGAIN;
3196
3197 trans = btrfs_join_transaction(root, 1);
3198 BUG_ON(IS_ERR(trans));
3199 ret = btrfs_commit_transaction(trans, root);
3200 BUG_ON(ret);
3201
3202 return 1;
3203 }
3204
3205 static int reserve_metadata_bytes(struct btrfs_block_rsv *block_rsv,
3206 u64 num_bytes)
3207 {
3208 struct btrfs_space_info *space_info = block_rsv->space_info;
3209 u64 unused;
3210 int ret = -ENOSPC;
3211
3212 spin_lock(&space_info->lock);
3213 unused = space_info->bytes_used + space_info->bytes_reserved +
3214 space_info->bytes_pinned + space_info->bytes_readonly;
3215
3216 if (unused < space_info->total_bytes)
3217 unused = space_info->total_bytes - unused;
3218 else
3219 unused = 0;
3220
3221 if (unused >= num_bytes) {
3222 if (block_rsv->priority >= 10) {
3223 space_info->bytes_reserved += num_bytes;
3224 ret = 0;
3225 } else {
3226 if ((unused + block_rsv->reserved) *
3227 block_rsv->priority >=
3228 (num_bytes + block_rsv->reserved) * 10) {
3229 space_info->bytes_reserved += num_bytes;
3230 ret = 0;
3231 }
3232 }
3233 }
3234 spin_unlock(&space_info->lock);
3235
3236 return ret;
3237 }
3238
3239 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3240 struct btrfs_root *root)
3241 {
3242 struct btrfs_block_rsv *block_rsv;
3243 if (root->ref_cows)
3244 block_rsv = trans->block_rsv;
3245 else
3246 block_rsv = root->block_rsv;
3247
3248 if (!block_rsv)
3249 block_rsv = &root->fs_info->empty_block_rsv;
3250
3251 return block_rsv;
3252 }
3253
3254 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3255 u64 num_bytes)
3256 {
3257 int ret = -ENOSPC;
3258 spin_lock(&block_rsv->lock);
3259 if (block_rsv->reserved >= num_bytes) {
3260 block_rsv->reserved -= num_bytes;
3261 if (block_rsv->reserved < block_rsv->size)
3262 block_rsv->full = 0;
3263 ret = 0;
3264 }
3265 spin_unlock(&block_rsv->lock);
3266 return ret;
3267 }
3268
3269 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3270 u64 num_bytes, int update_size)
3271 {
3272 spin_lock(&block_rsv->lock);
3273 block_rsv->reserved += num_bytes;
3274 if (update_size)
3275 block_rsv->size += num_bytes;
3276 else if (block_rsv->reserved >= block_rsv->size)
3277 block_rsv->full = 1;
3278 spin_unlock(&block_rsv->lock);
3279 }
3280
3281 void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3282 struct btrfs_block_rsv *dest, u64 num_bytes)
3283 {
3284 struct btrfs_space_info *space_info = block_rsv->space_info;
3285
3286 spin_lock(&block_rsv->lock);
3287 if (num_bytes == (u64)-1)
3288 num_bytes = block_rsv->size;
3289 block_rsv->size -= num_bytes;
3290 if (block_rsv->reserved >= block_rsv->size) {
3291 num_bytes = block_rsv->reserved - block_rsv->size;
3292 block_rsv->reserved = block_rsv->size;
3293 block_rsv->full = 1;
3294 } else {
3295 num_bytes = 0;
3296 }
3297 spin_unlock(&block_rsv->lock);
3298
3299 if (num_bytes > 0) {
3300 if (dest) {
3301 block_rsv_add_bytes(dest, num_bytes, 0);
3302 } else {
3303 spin_lock(&space_info->lock);
3304 space_info->bytes_reserved -= num_bytes;
3305 spin_unlock(&space_info->lock);
3306 }
3307 }
3308 }
3309
3310 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3311 struct btrfs_block_rsv *dst, u64 num_bytes)
3312 {
3313 int ret;
3314
3315 ret = block_rsv_use_bytes(src, num_bytes);
3316 if (ret)
3317 return ret;
3318
3319 block_rsv_add_bytes(dst, num_bytes, 1);
3320 return 0;
3321 }
3322
3323 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3324 {
3325 memset(rsv, 0, sizeof(*rsv));
3326 spin_lock_init(&rsv->lock);
3327 atomic_set(&rsv->usage, 1);
3328 rsv->priority = 6;
3329 INIT_LIST_HEAD(&rsv->list);
3330 }
3331
3332 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3333 {
3334 struct btrfs_block_rsv *block_rsv;
3335 struct btrfs_fs_info *fs_info = root->fs_info;
3336 u64 alloc_target;
3337
3338 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3339 if (!block_rsv)
3340 return NULL;
3341
3342 btrfs_init_block_rsv(block_rsv);
3343
3344 alloc_target = btrfs_get_alloc_profile(root, 0);
3345 block_rsv->space_info = __find_space_info(fs_info,
3346 BTRFS_BLOCK_GROUP_METADATA);
3347
3348 return block_rsv;
3349 }
3350
3351 void btrfs_free_block_rsv(struct btrfs_root *root,
3352 struct btrfs_block_rsv *rsv)
3353 {
3354 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3355 btrfs_block_rsv_release(root, rsv, (u64)-1);
3356 if (!rsv->durable)
3357 kfree(rsv);
3358 }
3359 }
3360
3361 /*
3362 * make the block_rsv struct be able to capture freed space.
3363 * the captured space will re-add to the the block_rsv struct
3364 * after transaction commit
3365 */
3366 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3367 struct btrfs_block_rsv *block_rsv)
3368 {
3369 block_rsv->durable = 1;
3370 mutex_lock(&fs_info->durable_block_rsv_mutex);
3371 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3372 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3373 }
3374
3375 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3376 struct btrfs_root *root,
3377 struct btrfs_block_rsv *block_rsv,
3378 u64 num_bytes, int *retries)
3379 {
3380 int ret;
3381
3382 if (num_bytes == 0)
3383 return 0;
3384 again:
3385 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3386 if (!ret) {
3387 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3388 return 0;
3389 }
3390
3391 ret = should_retry_reserve(trans, root, block_rsv, num_bytes, retries);
3392 if (ret > 0)
3393 goto again;
3394
3395 return ret;
3396 }
3397
3398 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3399 struct btrfs_root *root,
3400 struct btrfs_block_rsv *block_rsv,
3401 u64 min_reserved, int min_factor)
3402 {
3403 u64 num_bytes = 0;
3404 int commit_trans = 0;
3405 int ret = -ENOSPC;
3406
3407 if (!block_rsv)
3408 return 0;
3409
3410 spin_lock(&block_rsv->lock);
3411 if (min_factor > 0)
3412 num_bytes = div_factor(block_rsv->size, min_factor);
3413 if (min_reserved > num_bytes)
3414 num_bytes = min_reserved;
3415
3416 if (block_rsv->reserved >= num_bytes) {
3417 ret = 0;
3418 } else {
3419 num_bytes -= block_rsv->reserved;
3420 if (block_rsv->durable &&
3421 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3422 commit_trans = 1;
3423 }
3424 spin_unlock(&block_rsv->lock);
3425 if (!ret)
3426 return 0;
3427
3428 if (block_rsv->refill_used) {
3429 ret = reserve_metadata_bytes(block_rsv, num_bytes);
3430 if (!ret) {
3431 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3432 return 0;
3433 }
3434 }
3435
3436 if (commit_trans) {
3437 if (trans)
3438 return -EAGAIN;
3439
3440 trans = btrfs_join_transaction(root, 1);
3441 BUG_ON(IS_ERR(trans));
3442 ret = btrfs_commit_transaction(trans, root);
3443 return 0;
3444 }
3445
3446 WARN_ON(1);
3447 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
3448 block_rsv->size, block_rsv->reserved,
3449 block_rsv->freed[0], block_rsv->freed[1]);
3450
3451 return -ENOSPC;
3452 }
3453
3454 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3455 struct btrfs_block_rsv *dst_rsv,
3456 u64 num_bytes)
3457 {
3458 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3459 }
3460
3461 void btrfs_block_rsv_release(struct btrfs_root *root,
3462 struct btrfs_block_rsv *block_rsv,
3463 u64 num_bytes)
3464 {
3465 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3466 if (global_rsv->full || global_rsv == block_rsv ||
3467 block_rsv->space_info != global_rsv->space_info)
3468 global_rsv = NULL;
3469 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3470 }
3471
3472 /*
3473 * helper to calculate size of global block reservation.
3474 * the desired value is sum of space used by extent tree,
3475 * checksum tree and root tree
3476 */
3477 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3478 {
3479 struct btrfs_space_info *sinfo;
3480 u64 num_bytes;
3481 u64 meta_used;
3482 u64 data_used;
3483 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3484 #if 0
3485 /*
3486 * per tree used space accounting can be inaccuracy, so we
3487 * can't rely on it.
3488 */
3489 spin_lock(&fs_info->extent_root->accounting_lock);
3490 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3491 spin_unlock(&fs_info->extent_root->accounting_lock);
3492
3493 spin_lock(&fs_info->csum_root->accounting_lock);
3494 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3495 spin_unlock(&fs_info->csum_root->accounting_lock);
3496
3497 spin_lock(&fs_info->tree_root->accounting_lock);
3498 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3499 spin_unlock(&fs_info->tree_root->accounting_lock);
3500 #endif
3501 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3502 spin_lock(&sinfo->lock);
3503 data_used = sinfo->bytes_used;
3504 spin_unlock(&sinfo->lock);
3505
3506 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3507 spin_lock(&sinfo->lock);
3508 meta_used = sinfo->bytes_used;
3509 spin_unlock(&sinfo->lock);
3510
3511 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3512 csum_size * 2;
3513 num_bytes += div64_u64(data_used + meta_used, 50);
3514
3515 if (num_bytes * 3 > meta_used)
3516 num_bytes = div64_u64(meta_used, 3);
3517
3518 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3519 }
3520
3521 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3522 {
3523 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3524 struct btrfs_space_info *sinfo = block_rsv->space_info;
3525 u64 num_bytes;
3526
3527 num_bytes = calc_global_metadata_size(fs_info);
3528
3529 spin_lock(&block_rsv->lock);
3530 spin_lock(&sinfo->lock);
3531
3532 block_rsv->size = num_bytes;
3533
3534 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3535 sinfo->bytes_reserved + sinfo->bytes_readonly;
3536
3537 if (sinfo->total_bytes > num_bytes) {
3538 num_bytes = sinfo->total_bytes - num_bytes;
3539 block_rsv->reserved += num_bytes;
3540 sinfo->bytes_reserved += num_bytes;
3541 }
3542
3543 if (block_rsv->reserved >= block_rsv->size) {
3544 num_bytes = block_rsv->reserved - block_rsv->size;
3545 sinfo->bytes_reserved -= num_bytes;
3546 block_rsv->reserved = block_rsv->size;
3547 block_rsv->full = 1;
3548 }
3549 #if 0
3550 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3551 block_rsv->size, block_rsv->reserved);
3552 #endif
3553 spin_unlock(&sinfo->lock);
3554 spin_unlock(&block_rsv->lock);
3555 }
3556
3557 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3558 {
3559 struct btrfs_space_info *space_info;
3560
3561 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3562 fs_info->chunk_block_rsv.space_info = space_info;
3563 fs_info->chunk_block_rsv.priority = 10;
3564
3565 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3566 fs_info->global_block_rsv.space_info = space_info;
3567 fs_info->global_block_rsv.priority = 10;
3568 fs_info->global_block_rsv.refill_used = 1;
3569 fs_info->delalloc_block_rsv.space_info = space_info;
3570 fs_info->trans_block_rsv.space_info = space_info;
3571 fs_info->empty_block_rsv.space_info = space_info;
3572 fs_info->empty_block_rsv.priority = 10;
3573
3574 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3575 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3576 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3577 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3578 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3579
3580 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3581
3582 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3583
3584 update_global_block_rsv(fs_info);
3585 }
3586
3587 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3588 {
3589 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3590 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3591 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3592 WARN_ON(fs_info->trans_block_rsv.size > 0);
3593 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3594 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3595 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3596 }
3597
3598 static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3599 {
3600 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3601 3 * num_items;
3602 }
3603
3604 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3605 struct btrfs_root *root,
3606 int num_items, int *retries)
3607 {
3608 u64 num_bytes;
3609 int ret;
3610
3611 if (num_items == 0 || root->fs_info->chunk_root == root)
3612 return 0;
3613
3614 num_bytes = calc_trans_metadata_size(root, num_items);
3615 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3616 num_bytes, retries);
3617 if (!ret) {
3618 trans->bytes_reserved += num_bytes;
3619 trans->block_rsv = &root->fs_info->trans_block_rsv;
3620 }
3621 return ret;
3622 }
3623
3624 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3625 struct btrfs_root *root)
3626 {
3627 if (!trans->bytes_reserved)
3628 return;
3629
3630 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3631 btrfs_block_rsv_release(root, trans->block_rsv,
3632 trans->bytes_reserved);
3633 trans->bytes_reserved = 0;
3634 }
3635
3636 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3637 struct inode *inode)
3638 {
3639 struct btrfs_root *root = BTRFS_I(inode)->root;
3640 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3641 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3642
3643 /*
3644 * one for deleting orphan item, one for updating inode and
3645 * two for calling btrfs_truncate_inode_items.
3646 *
3647 * btrfs_truncate_inode_items is a delete operation, it frees
3648 * more space than it uses in most cases. So two units of
3649 * metadata space should be enough for calling it many times.
3650 * If all of the metadata space is used, we can commit
3651 * transaction and use space it freed.
3652 */
3653 u64 num_bytes = calc_trans_metadata_size(root, 4);
3654 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3655 }
3656
3657 void btrfs_orphan_release_metadata(struct inode *inode)
3658 {
3659 struct btrfs_root *root = BTRFS_I(inode)->root;
3660 u64 num_bytes = calc_trans_metadata_size(root, 4);
3661 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3662 }
3663
3664 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3665 struct btrfs_pending_snapshot *pending)
3666 {
3667 struct btrfs_root *root = pending->root;
3668 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3669 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3670 /*
3671 * two for root back/forward refs, two for directory entries
3672 * and one for root of the snapshot.
3673 */
3674 u64 num_bytes = calc_trans_metadata_size(root, 5);
3675 dst_rsv->space_info = src_rsv->space_info;
3676 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3677 }
3678
3679 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3680 {
3681 return num_bytes >>= 3;
3682 }
3683
3684 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3685 {
3686 struct btrfs_root *root = BTRFS_I(inode)->root;
3687 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3688 u64 to_reserve;
3689 int nr_extents;
3690 int retries = 0;
3691 int ret;
3692
3693 if (btrfs_transaction_in_commit(root->fs_info))
3694 schedule_timeout(1);
3695
3696 num_bytes = ALIGN(num_bytes, root->sectorsize);
3697 again:
3698 spin_lock(&BTRFS_I(inode)->accounting_lock);
3699 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3700 if (nr_extents > BTRFS_I(inode)->reserved_extents) {
3701 nr_extents -= BTRFS_I(inode)->reserved_extents;
3702 to_reserve = calc_trans_metadata_size(root, nr_extents);
3703 } else {
3704 nr_extents = 0;
3705 to_reserve = 0;
3706 }
3707
3708 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3709 ret = reserve_metadata_bytes(block_rsv, to_reserve);
3710 if (ret) {
3711 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3712 ret = should_retry_reserve(NULL, root, block_rsv, to_reserve,
3713 &retries);
3714 if (ret > 0)
3715 goto again;
3716 return ret;
3717 }
3718
3719 BTRFS_I(inode)->reserved_extents += nr_extents;
3720 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3721 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3722
3723 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3724
3725 if (block_rsv->size > 512 * 1024 * 1024)
3726 shrink_delalloc(NULL, root, to_reserve);
3727
3728 return 0;
3729 }
3730
3731 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3732 {
3733 struct btrfs_root *root = BTRFS_I(inode)->root;
3734 u64 to_free;
3735 int nr_extents;
3736
3737 num_bytes = ALIGN(num_bytes, root->sectorsize);
3738 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
3739
3740 spin_lock(&BTRFS_I(inode)->accounting_lock);
3741 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3742 if (nr_extents < BTRFS_I(inode)->reserved_extents) {
3743 nr_extents = BTRFS_I(inode)->reserved_extents - nr_extents;
3744 BTRFS_I(inode)->reserved_extents -= nr_extents;
3745 } else {
3746 nr_extents = 0;
3747 }
3748 spin_unlock(&BTRFS_I(inode)->accounting_lock);
3749
3750 to_free = calc_csum_metadata_size(inode, num_bytes);
3751 if (nr_extents > 0)
3752 to_free += calc_trans_metadata_size(root, nr_extents);
3753
3754 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3755 to_free);
3756 }
3757
3758 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
3759 {
3760 int ret;
3761
3762 ret = btrfs_check_data_free_space(inode, num_bytes);
3763 if (ret)
3764 return ret;
3765
3766 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
3767 if (ret) {
3768 btrfs_free_reserved_data_space(inode, num_bytes);
3769 return ret;
3770 }
3771
3772 return 0;
3773 }
3774
3775 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
3776 {
3777 btrfs_delalloc_release_metadata(inode, num_bytes);
3778 btrfs_free_reserved_data_space(inode, num_bytes);
3779 }
3780
3781 static int update_block_group(struct btrfs_trans_handle *trans,
3782 struct btrfs_root *root,
3783 u64 bytenr, u64 num_bytes, int alloc)
3784 {
3785 struct btrfs_block_group_cache *cache;
3786 struct btrfs_fs_info *info = root->fs_info;
3787 int factor;
3788 u64 total = num_bytes;
3789 u64 old_val;
3790 u64 byte_in_group;
3791
3792 /* block accounting for super block */
3793 spin_lock(&info->delalloc_lock);
3794 old_val = btrfs_super_bytes_used(&info->super_copy);
3795 if (alloc)
3796 old_val += num_bytes;
3797 else
3798 old_val -= num_bytes;
3799 btrfs_set_super_bytes_used(&info->super_copy, old_val);
3800 spin_unlock(&info->delalloc_lock);
3801
3802 while (total) {
3803 cache = btrfs_lookup_block_group(info, bytenr);
3804 if (!cache)
3805 return -1;
3806 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
3807 BTRFS_BLOCK_GROUP_RAID1 |
3808 BTRFS_BLOCK_GROUP_RAID10))
3809 factor = 2;
3810 else
3811 factor = 1;
3812 byte_in_group = bytenr - cache->key.objectid;
3813 WARN_ON(byte_in_group > cache->key.offset);
3814
3815 spin_lock(&cache->space_info->lock);
3816 spin_lock(&cache->lock);
3817 cache->dirty = 1;
3818 old_val = btrfs_block_group_used(&cache->item);
3819 num_bytes = min(total, cache->key.offset - byte_in_group);
3820 if (alloc) {
3821 old_val += num_bytes;
3822 btrfs_set_block_group_used(&cache->item, old_val);
3823 cache->reserved -= num_bytes;
3824 cache->space_info->bytes_reserved -= num_bytes;
3825 cache->space_info->bytes_used += num_bytes;
3826 cache->space_info->disk_used += num_bytes * factor;
3827 spin_unlock(&cache->lock);
3828 spin_unlock(&cache->space_info->lock);
3829 } else {
3830 old_val -= num_bytes;
3831 btrfs_set_block_group_used(&cache->item, old_val);
3832 cache->pinned += num_bytes;
3833 cache->space_info->bytes_pinned += num_bytes;
3834 cache->space_info->bytes_used -= num_bytes;
3835 cache->space_info->disk_used -= num_bytes * factor;
3836 spin_unlock(&cache->lock);
3837 spin_unlock(&cache->space_info->lock);
3838
3839 set_extent_dirty(info->pinned_extents,
3840 bytenr, bytenr + num_bytes - 1,
3841 GFP_NOFS | __GFP_NOFAIL);
3842 }
3843 btrfs_put_block_group(cache);
3844 total -= num_bytes;
3845 bytenr += num_bytes;
3846 }
3847 return 0;
3848 }
3849
3850 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
3851 {
3852 struct btrfs_block_group_cache *cache;
3853 u64 bytenr;
3854
3855 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
3856 if (!cache)
3857 return 0;
3858
3859 bytenr = cache->key.objectid;
3860 btrfs_put_block_group(cache);
3861
3862 return bytenr;
3863 }
3864
3865 static int pin_down_extent(struct btrfs_root *root,
3866 struct btrfs_block_group_cache *cache,
3867 u64 bytenr, u64 num_bytes, int reserved)
3868 {
3869 spin_lock(&cache->space_info->lock);
3870 spin_lock(&cache->lock);
3871 cache->pinned += num_bytes;
3872 cache->space_info->bytes_pinned += num_bytes;
3873 if (reserved) {
3874 cache->reserved -= num_bytes;
3875 cache->space_info->bytes_reserved -= num_bytes;
3876 }
3877 spin_unlock(&cache->lock);
3878 spin_unlock(&cache->space_info->lock);
3879
3880 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
3881 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
3882 return 0;
3883 }
3884
3885 /*
3886 * this function must be called within transaction
3887 */
3888 int btrfs_pin_extent(struct btrfs_root *root,
3889 u64 bytenr, u64 num_bytes, int reserved)
3890 {
3891 struct btrfs_block_group_cache *cache;
3892
3893 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
3894 BUG_ON(!cache);
3895
3896 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
3897
3898 btrfs_put_block_group(cache);
3899 return 0;
3900 }
3901
3902 /*
3903 * update size of reserved extents. this function may return -EAGAIN
3904 * if 'reserve' is true or 'sinfo' is false.
3905 */
3906 static int update_reserved_bytes(struct btrfs_block_group_cache *cache,
3907 u64 num_bytes, int reserve, int sinfo)
3908 {
3909 int ret = 0;
3910 if (sinfo) {
3911 struct btrfs_space_info *space_info = cache->space_info;
3912 spin_lock(&space_info->lock);
3913 spin_lock(&cache->lock);
3914 if (reserve) {
3915 if (cache->ro) {
3916 ret = -EAGAIN;
3917 } else {
3918 cache->reserved += num_bytes;
3919 space_info->bytes_reserved += num_bytes;
3920 }
3921 } else {
3922 if (cache->ro)
3923 space_info->bytes_readonly += num_bytes;
3924 cache->reserved -= num_bytes;
3925 space_info->bytes_reserved -= num_bytes;
3926 }
3927 spin_unlock(&cache->lock);
3928 spin_unlock(&space_info->lock);
3929 } else {
3930 spin_lock(&cache->lock);
3931 if (cache->ro) {
3932 ret = -EAGAIN;
3933 } else {
3934 if (reserve)
3935 cache->reserved += num_bytes;
3936 else
3937 cache->reserved -= num_bytes;
3938 }
3939 spin_unlock(&cache->lock);
3940 }
3941 return ret;
3942 }
3943
3944 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
3945 struct btrfs_root *root)
3946 {
3947 struct btrfs_fs_info *fs_info = root->fs_info;
3948 struct btrfs_caching_control *next;
3949 struct btrfs_caching_control *caching_ctl;
3950 struct btrfs_block_group_cache *cache;
3951
3952 down_write(&fs_info->extent_commit_sem);
3953
3954 list_for_each_entry_safe(caching_ctl, next,
3955 &fs_info->caching_block_groups, list) {
3956 cache = caching_ctl->block_group;
3957 if (block_group_cache_done(cache)) {
3958 cache->last_byte_to_unpin = (u64)-1;
3959 list_del_init(&caching_ctl->list);
3960 put_caching_control(caching_ctl);
3961 } else {
3962 cache->last_byte_to_unpin = caching_ctl->progress;
3963 }
3964 }
3965
3966 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
3967 fs_info->pinned_extents = &fs_info->freed_extents[1];
3968 else
3969 fs_info->pinned_extents = &fs_info->freed_extents[0];
3970
3971 up_write(&fs_info->extent_commit_sem);
3972
3973 update_global_block_rsv(fs_info);
3974 return 0;
3975 }
3976
3977 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
3978 {
3979 struct btrfs_fs_info *fs_info = root->fs_info;
3980 struct btrfs_block_group_cache *cache = NULL;
3981 u64 len;
3982
3983 while (start <= end) {
3984 if (!cache ||
3985 start >= cache->key.objectid + cache->key.offset) {
3986 if (cache)
3987 btrfs_put_block_group(cache);
3988 cache = btrfs_lookup_block_group(fs_info, start);
3989 BUG_ON(!cache);
3990 }
3991
3992 len = cache->key.objectid + cache->key.offset - start;
3993 len = min(len, end + 1 - start);
3994
3995 if (start < cache->last_byte_to_unpin) {
3996 len = min(len, cache->last_byte_to_unpin - start);
3997 btrfs_add_free_space(cache, start, len);
3998 }
3999
4000 start += len;
4001
4002 spin_lock(&cache->space_info->lock);
4003 spin_lock(&cache->lock);
4004 cache->pinned -= len;
4005 cache->space_info->bytes_pinned -= len;
4006 if (cache->ro) {
4007 cache->space_info->bytes_readonly += len;
4008 } else if (cache->reserved_pinned > 0) {
4009 len = min(len, cache->reserved_pinned);
4010 cache->reserved_pinned -= len;
4011 cache->space_info->bytes_reserved += len;
4012 }
4013 spin_unlock(&cache->lock);
4014 spin_unlock(&cache->space_info->lock);
4015 }
4016
4017 if (cache)
4018 btrfs_put_block_group(cache);
4019 return 0;
4020 }
4021
4022 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4023 struct btrfs_root *root)
4024 {
4025 struct btrfs_fs_info *fs_info = root->fs_info;
4026 struct extent_io_tree *unpin;
4027 struct btrfs_block_rsv *block_rsv;
4028 struct btrfs_block_rsv *next_rsv;
4029 u64 start;
4030 u64 end;
4031 int idx;
4032 int ret;
4033
4034 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4035 unpin = &fs_info->freed_extents[1];
4036 else
4037 unpin = &fs_info->freed_extents[0];
4038
4039 while (1) {
4040 ret = find_first_extent_bit(unpin, 0, &start, &end,
4041 EXTENT_DIRTY);
4042 if (ret)
4043 break;
4044
4045 ret = btrfs_discard_extent(root, start, end + 1 - start);
4046
4047 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4048 unpin_extent_range(root, start, end);
4049 cond_resched();
4050 }
4051
4052 mutex_lock(&fs_info->durable_block_rsv_mutex);
4053 list_for_each_entry_safe(block_rsv, next_rsv,
4054 &fs_info->durable_block_rsv_list, list) {
4055
4056 idx = trans->transid & 0x1;
4057 if (block_rsv->freed[idx] > 0) {
4058 block_rsv_add_bytes(block_rsv,
4059 block_rsv->freed[idx], 0);
4060 block_rsv->freed[idx] = 0;
4061 }
4062 if (atomic_read(&block_rsv->usage) == 0) {
4063 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4064
4065 if (block_rsv->freed[0] == 0 &&
4066 block_rsv->freed[1] == 0) {
4067 list_del_init(&block_rsv->list);
4068 kfree(block_rsv);
4069 }
4070 } else {
4071 btrfs_block_rsv_release(root, block_rsv, 0);
4072 }
4073 }
4074 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4075
4076 return 0;
4077 }
4078
4079 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4080 struct btrfs_root *root,
4081 u64 bytenr, u64 num_bytes, u64 parent,
4082 u64 root_objectid, u64 owner_objectid,
4083 u64 owner_offset, int refs_to_drop,
4084 struct btrfs_delayed_extent_op *extent_op)
4085 {
4086 struct btrfs_key key;
4087 struct btrfs_path *path;
4088 struct btrfs_fs_info *info = root->fs_info;
4089 struct btrfs_root *extent_root = info->extent_root;
4090 struct extent_buffer *leaf;
4091 struct btrfs_extent_item *ei;
4092 struct btrfs_extent_inline_ref *iref;
4093 int ret;
4094 int is_data;
4095 int extent_slot = 0;
4096 int found_extent = 0;
4097 int num_to_del = 1;
4098 u32 item_size;
4099 u64 refs;
4100
4101 path = btrfs_alloc_path();
4102 if (!path)
4103 return -ENOMEM;
4104
4105 path->reada = 1;
4106 path->leave_spinning = 1;
4107
4108 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4109 BUG_ON(!is_data && refs_to_drop != 1);
4110
4111 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4112 bytenr, num_bytes, parent,
4113 root_objectid, owner_objectid,
4114 owner_offset);
4115 if (ret == 0) {
4116 extent_slot = path->slots[0];
4117 while (extent_slot >= 0) {
4118 btrfs_item_key_to_cpu(path->nodes[0], &key,
4119 extent_slot);
4120 if (key.objectid != bytenr)
4121 break;
4122 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4123 key.offset == num_bytes) {
4124 found_extent = 1;
4125 break;
4126 }
4127 if (path->slots[0] - extent_slot > 5)
4128 break;
4129 extent_slot--;
4130 }
4131 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4132 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4133 if (found_extent && item_size < sizeof(*ei))
4134 found_extent = 0;
4135 #endif
4136 if (!found_extent) {
4137 BUG_ON(iref);
4138 ret = remove_extent_backref(trans, extent_root, path,
4139 NULL, refs_to_drop,
4140 is_data);
4141 BUG_ON(ret);
4142 btrfs_release_path(extent_root, path);
4143 path->leave_spinning = 1;
4144
4145 key.objectid = bytenr;
4146 key.type = BTRFS_EXTENT_ITEM_KEY;
4147 key.offset = num_bytes;
4148
4149 ret = btrfs_search_slot(trans, extent_root,
4150 &key, path, -1, 1);
4151 if (ret) {
4152 printk(KERN_ERR "umm, got %d back from search"
4153 ", was looking for %llu\n", ret,
4154 (unsigned long long)bytenr);
4155 btrfs_print_leaf(extent_root, path->nodes[0]);
4156 }
4157 BUG_ON(ret);
4158 extent_slot = path->slots[0];
4159 }
4160 } else {
4161 btrfs_print_leaf(extent_root, path->nodes[0]);
4162 WARN_ON(1);
4163 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4164 "parent %llu root %llu owner %llu offset %llu\n",
4165 (unsigned long long)bytenr,
4166 (unsigned long long)parent,
4167 (unsigned long long)root_objectid,
4168 (unsigned long long)owner_objectid,
4169 (unsigned long long)owner_offset);
4170 }
4171
4172 leaf = path->nodes[0];
4173 item_size = btrfs_item_size_nr(leaf, extent_slot);
4174 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4175 if (item_size < sizeof(*ei)) {
4176 BUG_ON(found_extent || extent_slot != path->slots[0]);
4177 ret = convert_extent_item_v0(trans, extent_root, path,
4178 owner_objectid, 0);
4179 BUG_ON(ret < 0);
4180
4181 btrfs_release_path(extent_root, path);
4182 path->leave_spinning = 1;
4183
4184 key.objectid = bytenr;
4185 key.type = BTRFS_EXTENT_ITEM_KEY;
4186 key.offset = num_bytes;
4187
4188 ret = btrfs_search_slot(trans, extent_root, &key, path,
4189 -1, 1);
4190 if (ret) {
4191 printk(KERN_ERR "umm, got %d back from search"
4192 ", was looking for %llu\n", ret,
4193 (unsigned long long)bytenr);
4194 btrfs_print_leaf(extent_root, path->nodes[0]);
4195 }
4196 BUG_ON(ret);
4197 extent_slot = path->slots[0];
4198 leaf = path->nodes[0];
4199 item_size = btrfs_item_size_nr(leaf, extent_slot);
4200 }
4201 #endif
4202 BUG_ON(item_size < sizeof(*ei));
4203 ei = btrfs_item_ptr(leaf, extent_slot,
4204 struct btrfs_extent_item);
4205 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4206 struct btrfs_tree_block_info *bi;
4207 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4208 bi = (struct btrfs_tree_block_info *)(ei + 1);
4209 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4210 }
4211
4212 refs = btrfs_extent_refs(leaf, ei);
4213 BUG_ON(refs < refs_to_drop);
4214 refs -= refs_to_drop;
4215
4216 if (refs > 0) {
4217 if (extent_op)
4218 __run_delayed_extent_op(extent_op, leaf, ei);
4219 /*
4220 * In the case of inline back ref, reference count will
4221 * be updated by remove_extent_backref
4222 */
4223 if (iref) {
4224 BUG_ON(!found_extent);
4225 } else {
4226 btrfs_set_extent_refs(leaf, ei, refs);
4227 btrfs_mark_buffer_dirty(leaf);
4228 }
4229 if (found_extent) {
4230 ret = remove_extent_backref(trans, extent_root, path,
4231 iref, refs_to_drop,
4232 is_data);
4233 BUG_ON(ret);
4234 }
4235 } else {
4236 if (found_extent) {
4237 BUG_ON(is_data && refs_to_drop !=
4238 extent_data_ref_count(root, path, iref));
4239 if (iref) {
4240 BUG_ON(path->slots[0] != extent_slot);
4241 } else {
4242 BUG_ON(path->slots[0] != extent_slot + 1);
4243 path->slots[0] = extent_slot;
4244 num_to_del = 2;
4245 }
4246 }
4247
4248 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4249 num_to_del);
4250 BUG_ON(ret);
4251 btrfs_release_path(extent_root, path);
4252
4253 if (is_data) {
4254 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4255 BUG_ON(ret);
4256 } else {
4257 invalidate_mapping_pages(info->btree_inode->i_mapping,
4258 bytenr >> PAGE_CACHE_SHIFT,
4259 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4260 }
4261
4262 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4263 BUG_ON(ret);
4264 }
4265 btrfs_free_path(path);
4266 return ret;
4267 }
4268
4269 /*
4270 * when we free an block, it is possible (and likely) that we free the last
4271 * delayed ref for that extent as well. This searches the delayed ref tree for
4272 * a given extent, and if there are no other delayed refs to be processed, it
4273 * removes it from the tree.
4274 */
4275 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *root, u64 bytenr)
4277 {
4278 struct btrfs_delayed_ref_head *head;
4279 struct btrfs_delayed_ref_root *delayed_refs;
4280 struct btrfs_delayed_ref_node *ref;
4281 struct rb_node *node;
4282 int ret = 0;
4283
4284 delayed_refs = &trans->transaction->delayed_refs;
4285 spin_lock(&delayed_refs->lock);
4286 head = btrfs_find_delayed_ref_head(trans, bytenr);
4287 if (!head)
4288 goto out;
4289
4290 node = rb_prev(&head->node.rb_node);
4291 if (!node)
4292 goto out;
4293
4294 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4295
4296 /* there are still entries for this ref, we can't drop it */
4297 if (ref->bytenr == bytenr)
4298 goto out;
4299
4300 if (head->extent_op) {
4301 if (!head->must_insert_reserved)
4302 goto out;
4303 kfree(head->extent_op);
4304 head->extent_op = NULL;
4305 }
4306
4307 /*
4308 * waiting for the lock here would deadlock. If someone else has it
4309 * locked they are already in the process of dropping it anyway
4310 */
4311 if (!mutex_trylock(&head->mutex))
4312 goto out;
4313
4314 /*
4315 * at this point we have a head with no other entries. Go
4316 * ahead and process it.
4317 */
4318 head->node.in_tree = 0;
4319 rb_erase(&head->node.rb_node, &delayed_refs->root);
4320
4321 delayed_refs->num_entries--;
4322
4323 /*
4324 * we don't take a ref on the node because we're removing it from the
4325 * tree, so we just steal the ref the tree was holding.
4326 */
4327 delayed_refs->num_heads--;
4328 if (list_empty(&head->cluster))
4329 delayed_refs->num_heads_ready--;
4330
4331 list_del_init(&head->cluster);
4332 spin_unlock(&delayed_refs->lock);
4333
4334 BUG_ON(head->extent_op);
4335 if (head->must_insert_reserved)
4336 ret = 1;
4337
4338 mutex_unlock(&head->mutex);
4339 btrfs_put_delayed_ref(&head->node);
4340 return ret;
4341 out:
4342 spin_unlock(&delayed_refs->lock);
4343 return 0;
4344 }
4345
4346 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4347 struct btrfs_root *root,
4348 struct extent_buffer *buf,
4349 u64 parent, int last_ref)
4350 {
4351 struct btrfs_block_rsv *block_rsv;
4352 struct btrfs_block_group_cache *cache = NULL;
4353 int ret;
4354
4355 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4356 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4357 parent, root->root_key.objectid,
4358 btrfs_header_level(buf),
4359 BTRFS_DROP_DELAYED_REF, NULL);
4360 BUG_ON(ret);
4361 }
4362
4363 if (!last_ref)
4364 return;
4365
4366 block_rsv = get_block_rsv(trans, root);
4367 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4368 if (block_rsv->space_info != cache->space_info)
4369 goto out;
4370
4371 if (btrfs_header_generation(buf) == trans->transid) {
4372 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4373 ret = check_ref_cleanup(trans, root, buf->start);
4374 if (!ret)
4375 goto pin;
4376 }
4377
4378 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4379 pin_down_extent(root, cache, buf->start, buf->len, 1);
4380 goto pin;
4381 }
4382
4383 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4384
4385 btrfs_add_free_space(cache, buf->start, buf->len);
4386 ret = update_reserved_bytes(cache, buf->len, 0, 0);
4387 if (ret == -EAGAIN) {
4388 /* block group became read-only */
4389 update_reserved_bytes(cache, buf->len, 0, 1);
4390 goto out;
4391 }
4392
4393 ret = 1;
4394 spin_lock(&block_rsv->lock);
4395 if (block_rsv->reserved < block_rsv->size) {
4396 block_rsv->reserved += buf->len;
4397 ret = 0;
4398 }
4399 spin_unlock(&block_rsv->lock);
4400
4401 if (ret) {
4402 spin_lock(&cache->space_info->lock);
4403 cache->space_info->bytes_reserved -= buf->len;
4404 spin_unlock(&cache->space_info->lock);
4405 }
4406 goto out;
4407 }
4408 pin:
4409 if (block_rsv->durable && !cache->ro) {
4410 ret = 0;
4411 spin_lock(&cache->lock);
4412 if (!cache->ro) {
4413 cache->reserved_pinned += buf->len;
4414 ret = 1;
4415 }
4416 spin_unlock(&cache->lock);
4417
4418 if (ret) {
4419 spin_lock(&block_rsv->lock);
4420 block_rsv->freed[trans->transid & 0x1] += buf->len;
4421 spin_unlock(&block_rsv->lock);
4422 }
4423 }
4424 out:
4425 btrfs_put_block_group(cache);
4426 }
4427
4428 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4429 struct btrfs_root *root,
4430 u64 bytenr, u64 num_bytes, u64 parent,
4431 u64 root_objectid, u64 owner, u64 offset)
4432 {
4433 int ret;
4434
4435 /*
4436 * tree log blocks never actually go into the extent allocation
4437 * tree, just update pinning info and exit early.
4438 */
4439 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4440 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4441 /* unlocks the pinned mutex */
4442 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4443 ret = 0;
4444 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4445 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4446 parent, root_objectid, (int)owner,
4447 BTRFS_DROP_DELAYED_REF, NULL);
4448 BUG_ON(ret);
4449 } else {
4450 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4451 parent, root_objectid, owner,
4452 offset, BTRFS_DROP_DELAYED_REF, NULL);
4453 BUG_ON(ret);
4454 }
4455 return ret;
4456 }
4457
4458 static u64 stripe_align(struct btrfs_root *root, u64 val)
4459 {
4460 u64 mask = ((u64)root->stripesize - 1);
4461 u64 ret = (val + mask) & ~mask;
4462 return ret;
4463 }
4464
4465 /*
4466 * when we wait for progress in the block group caching, its because
4467 * our allocation attempt failed at least once. So, we must sleep
4468 * and let some progress happen before we try again.
4469 *
4470 * This function will sleep at least once waiting for new free space to
4471 * show up, and then it will check the block group free space numbers
4472 * for our min num_bytes. Another option is to have it go ahead
4473 * and look in the rbtree for a free extent of a given size, but this
4474 * is a good start.
4475 */
4476 static noinline int
4477 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4478 u64 num_bytes)
4479 {
4480 struct btrfs_caching_control *caching_ctl;
4481 DEFINE_WAIT(wait);
4482
4483 caching_ctl = get_caching_control(cache);
4484 if (!caching_ctl)
4485 return 0;
4486
4487 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4488 (cache->free_space >= num_bytes));
4489
4490 put_caching_control(caching_ctl);
4491 return 0;
4492 }
4493
4494 static noinline int
4495 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4496 {
4497 struct btrfs_caching_control *caching_ctl;
4498 DEFINE_WAIT(wait);
4499
4500 caching_ctl = get_caching_control(cache);
4501 if (!caching_ctl)
4502 return 0;
4503
4504 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4505
4506 put_caching_control(caching_ctl);
4507 return 0;
4508 }
4509
4510 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4511 {
4512 int index;
4513 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4514 index = 0;
4515 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4516 index = 1;
4517 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4518 index = 2;
4519 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4520 index = 3;
4521 else
4522 index = 4;
4523 return index;
4524 }
4525
4526 enum btrfs_loop_type {
4527 LOOP_FIND_IDEAL = 0,
4528 LOOP_CACHING_NOWAIT = 1,
4529 LOOP_CACHING_WAIT = 2,
4530 LOOP_ALLOC_CHUNK = 3,
4531 LOOP_NO_EMPTY_SIZE = 4,
4532 };
4533
4534 /*
4535 * walks the btree of allocated extents and find a hole of a given size.
4536 * The key ins is changed to record the hole:
4537 * ins->objectid == block start
4538 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4539 * ins->offset == number of blocks
4540 * Any available blocks before search_start are skipped.
4541 */
4542 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4543 struct btrfs_root *orig_root,
4544 u64 num_bytes, u64 empty_size,
4545 u64 search_start, u64 search_end,
4546 u64 hint_byte, struct btrfs_key *ins,
4547 int data)
4548 {
4549 int ret = 0;
4550 struct btrfs_root *root = orig_root->fs_info->extent_root;
4551 struct btrfs_free_cluster *last_ptr = NULL;
4552 struct btrfs_block_group_cache *block_group = NULL;
4553 int empty_cluster = 2 * 1024 * 1024;
4554 int allowed_chunk_alloc = 0;
4555 int done_chunk_alloc = 0;
4556 struct btrfs_space_info *space_info;
4557 int last_ptr_loop = 0;
4558 int loop = 0;
4559 int index = 0;
4560 bool found_uncached_bg = false;
4561 bool failed_cluster_refill = false;
4562 bool failed_alloc = false;
4563 u64 ideal_cache_percent = 0;
4564 u64 ideal_cache_offset = 0;
4565
4566 WARN_ON(num_bytes < root->sectorsize);
4567 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4568 ins->objectid = 0;
4569 ins->offset = 0;
4570
4571 space_info = __find_space_info(root->fs_info, data);
4572 if (!space_info) {
4573 printk(KERN_ERR "No space info for %d\n", data);
4574 return -ENOSPC;
4575 }
4576
4577 if (orig_root->ref_cows || empty_size)
4578 allowed_chunk_alloc = 1;
4579
4580 if (data & BTRFS_BLOCK_GROUP_METADATA) {
4581 last_ptr = &root->fs_info->meta_alloc_cluster;
4582 if (!btrfs_test_opt(root, SSD))
4583 empty_cluster = 64 * 1024;
4584 }
4585
4586 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
4587 last_ptr = &root->fs_info->data_alloc_cluster;
4588 }
4589
4590 if (last_ptr) {
4591 spin_lock(&last_ptr->lock);
4592 if (last_ptr->block_group)
4593 hint_byte = last_ptr->window_start;
4594 spin_unlock(&last_ptr->lock);
4595 }
4596
4597 search_start = max(search_start, first_logical_byte(root, 0));
4598 search_start = max(search_start, hint_byte);
4599
4600 if (!last_ptr)
4601 empty_cluster = 0;
4602
4603 if (search_start == hint_byte) {
4604 ideal_cache:
4605 block_group = btrfs_lookup_block_group(root->fs_info,
4606 search_start);
4607 /*
4608 * we don't want to use the block group if it doesn't match our
4609 * allocation bits, or if its not cached.
4610 *
4611 * However if we are re-searching with an ideal block group
4612 * picked out then we don't care that the block group is cached.
4613 */
4614 if (block_group && block_group_bits(block_group, data) &&
4615 (block_group->cached != BTRFS_CACHE_NO ||
4616 search_start == ideal_cache_offset)) {
4617 down_read(&space_info->groups_sem);
4618 if (list_empty(&block_group->list) ||
4619 block_group->ro) {
4620 /*
4621 * someone is removing this block group,
4622 * we can't jump into the have_block_group
4623 * target because our list pointers are not
4624 * valid
4625 */
4626 btrfs_put_block_group(block_group);
4627 up_read(&space_info->groups_sem);
4628 } else {
4629 index = get_block_group_index(block_group);
4630 goto have_block_group;
4631 }
4632 } else if (block_group) {
4633 btrfs_put_block_group(block_group);
4634 }
4635 }
4636 search:
4637 down_read(&space_info->groups_sem);
4638 list_for_each_entry(block_group, &space_info->block_groups[index],
4639 list) {
4640 u64 offset;
4641 int cached;
4642
4643 btrfs_get_block_group(block_group);
4644 search_start = block_group->key.objectid;
4645
4646 have_block_group:
4647 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4648 u64 free_percent;
4649
4650 free_percent = btrfs_block_group_used(&block_group->item);
4651 free_percent *= 100;
4652 free_percent = div64_u64(free_percent,
4653 block_group->key.offset);
4654 free_percent = 100 - free_percent;
4655 if (free_percent > ideal_cache_percent &&
4656 likely(!block_group->ro)) {
4657 ideal_cache_offset = block_group->key.objectid;
4658 ideal_cache_percent = free_percent;
4659 }
4660
4661 /*
4662 * We only want to start kthread caching if we are at
4663 * the point where we will wait for caching to make
4664 * progress, or if our ideal search is over and we've
4665 * found somebody to start caching.
4666 */
4667 if (loop > LOOP_CACHING_NOWAIT ||
4668 (loop > LOOP_FIND_IDEAL &&
4669 atomic_read(&space_info->caching_threads) < 2)) {
4670 ret = cache_block_group(block_group);
4671 BUG_ON(ret);
4672 }
4673 found_uncached_bg = true;
4674
4675 /*
4676 * If loop is set for cached only, try the next block
4677 * group.
4678 */
4679 if (loop == LOOP_FIND_IDEAL)
4680 goto loop;
4681 }
4682
4683 cached = block_group_cache_done(block_group);
4684 if (unlikely(!cached))
4685 found_uncached_bg = true;
4686
4687 if (unlikely(block_group->ro))
4688 goto loop;
4689
4690 /*
4691 * Ok we want to try and use the cluster allocator, so lets look
4692 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4693 * have tried the cluster allocator plenty of times at this
4694 * point and not have found anything, so we are likely way too
4695 * fragmented for the clustering stuff to find anything, so lets
4696 * just skip it and let the allocator find whatever block it can
4697 * find
4698 */
4699 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
4700 /*
4701 * the refill lock keeps out other
4702 * people trying to start a new cluster
4703 */
4704 spin_lock(&last_ptr->refill_lock);
4705 if (last_ptr->block_group &&
4706 (last_ptr->block_group->ro ||
4707 !block_group_bits(last_ptr->block_group, data))) {
4708 offset = 0;
4709 goto refill_cluster;
4710 }
4711
4712 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
4713 num_bytes, search_start);
4714 if (offset) {
4715 /* we have a block, we're done */
4716 spin_unlock(&last_ptr->refill_lock);
4717 goto checks;
4718 }
4719
4720 spin_lock(&last_ptr->lock);
4721 /*
4722 * whoops, this cluster doesn't actually point to
4723 * this block group. Get a ref on the block
4724 * group is does point to and try again
4725 */
4726 if (!last_ptr_loop && last_ptr->block_group &&
4727 last_ptr->block_group != block_group) {
4728
4729 btrfs_put_block_group(block_group);
4730 block_group = last_ptr->block_group;
4731 btrfs_get_block_group(block_group);
4732 spin_unlock(&last_ptr->lock);
4733 spin_unlock(&last_ptr->refill_lock);
4734
4735 last_ptr_loop = 1;
4736 search_start = block_group->key.objectid;
4737 /*
4738 * we know this block group is properly
4739 * in the list because
4740 * btrfs_remove_block_group, drops the
4741 * cluster before it removes the block
4742 * group from the list
4743 */
4744 goto have_block_group;
4745 }
4746 spin_unlock(&last_ptr->lock);
4747 refill_cluster:
4748 /*
4749 * this cluster didn't work out, free it and
4750 * start over
4751 */
4752 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4753
4754 last_ptr_loop = 0;
4755
4756 /* allocate a cluster in this block group */
4757 ret = btrfs_find_space_cluster(trans, root,
4758 block_group, last_ptr,
4759 offset, num_bytes,
4760 empty_cluster + empty_size);
4761 if (ret == 0) {
4762 /*
4763 * now pull our allocation out of this
4764 * cluster
4765 */
4766 offset = btrfs_alloc_from_cluster(block_group,
4767 last_ptr, num_bytes,
4768 search_start);
4769 if (offset) {
4770 /* we found one, proceed */
4771 spin_unlock(&last_ptr->refill_lock);
4772 goto checks;
4773 }
4774 } else if (!cached && loop > LOOP_CACHING_NOWAIT
4775 && !failed_cluster_refill) {
4776 spin_unlock(&last_ptr->refill_lock);
4777
4778 failed_cluster_refill = true;
4779 wait_block_group_cache_progress(block_group,
4780 num_bytes + empty_cluster + empty_size);
4781 goto have_block_group;
4782 }
4783
4784 /*
4785 * at this point we either didn't find a cluster
4786 * or we weren't able to allocate a block from our
4787 * cluster. Free the cluster we've been trying
4788 * to use, and go to the next block group
4789 */
4790 btrfs_return_cluster_to_free_space(NULL, last_ptr);
4791 spin_unlock(&last_ptr->refill_lock);
4792 goto loop;
4793 }
4794
4795 offset = btrfs_find_space_for_alloc(block_group, search_start,
4796 num_bytes, empty_size);
4797 /*
4798 * If we didn't find a chunk, and we haven't failed on this
4799 * block group before, and this block group is in the middle of
4800 * caching and we are ok with waiting, then go ahead and wait
4801 * for progress to be made, and set failed_alloc to true.
4802 *
4803 * If failed_alloc is true then we've already waited on this
4804 * block group once and should move on to the next block group.
4805 */
4806 if (!offset && !failed_alloc && !cached &&
4807 loop > LOOP_CACHING_NOWAIT) {
4808 wait_block_group_cache_progress(block_group,
4809 num_bytes + empty_size);
4810 failed_alloc = true;
4811 goto have_block_group;
4812 } else if (!offset) {
4813 goto loop;
4814 }
4815 checks:
4816 search_start = stripe_align(root, offset);
4817 /* move on to the next group */
4818 if (search_start + num_bytes >= search_end) {
4819 btrfs_add_free_space(block_group, offset, num_bytes);
4820 goto loop;
4821 }
4822
4823 /* move on to the next group */
4824 if (search_start + num_bytes >
4825 block_group->key.objectid + block_group->key.offset) {
4826 btrfs_add_free_space(block_group, offset, num_bytes);
4827 goto loop;
4828 }
4829
4830 ins->objectid = search_start;
4831 ins->offset = num_bytes;
4832
4833 if (offset < search_start)
4834 btrfs_add_free_space(block_group, offset,
4835 search_start - offset);
4836 BUG_ON(offset > search_start);
4837
4838 ret = update_reserved_bytes(block_group, num_bytes, 1,
4839 (data & BTRFS_BLOCK_GROUP_DATA));
4840 if (ret == -EAGAIN) {
4841 btrfs_add_free_space(block_group, offset, num_bytes);
4842 goto loop;
4843 }
4844
4845 /* we are all good, lets return */
4846 ins->objectid = search_start;
4847 ins->offset = num_bytes;
4848
4849 if (offset < search_start)
4850 btrfs_add_free_space(block_group, offset,
4851 search_start - offset);
4852 BUG_ON(offset > search_start);
4853 break;
4854 loop:
4855 failed_cluster_refill = false;
4856 failed_alloc = false;
4857 BUG_ON(index != get_block_group_index(block_group));
4858 btrfs_put_block_group(block_group);
4859 }
4860 up_read(&space_info->groups_sem);
4861
4862 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
4863 goto search;
4864
4865 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
4866 * for them to make caching progress. Also
4867 * determine the best possible bg to cache
4868 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
4869 * caching kthreads as we move along
4870 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
4871 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
4872 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
4873 * again
4874 */
4875 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
4876 (found_uncached_bg || empty_size || empty_cluster ||
4877 allowed_chunk_alloc)) {
4878 index = 0;
4879 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
4880 found_uncached_bg = false;
4881 loop++;
4882 if (!ideal_cache_percent &&
4883 atomic_read(&space_info->caching_threads))
4884 goto search;
4885
4886 /*
4887 * 1 of the following 2 things have happened so far
4888 *
4889 * 1) We found an ideal block group for caching that
4890 * is mostly full and will cache quickly, so we might
4891 * as well wait for it.
4892 *
4893 * 2) We searched for cached only and we didn't find
4894 * anything, and we didn't start any caching kthreads
4895 * either, so chances are we will loop through and
4896 * start a couple caching kthreads, and then come back
4897 * around and just wait for them. This will be slower
4898 * because we will have 2 caching kthreads reading at
4899 * the same time when we could have just started one
4900 * and waited for it to get far enough to give us an
4901 * allocation, so go ahead and go to the wait caching
4902 * loop.
4903 */
4904 loop = LOOP_CACHING_WAIT;
4905 search_start = ideal_cache_offset;
4906 ideal_cache_percent = 0;
4907 goto ideal_cache;
4908 } else if (loop == LOOP_FIND_IDEAL) {
4909 /*
4910 * Didn't find a uncached bg, wait on anything we find
4911 * next.
4912 */
4913 loop = LOOP_CACHING_WAIT;
4914 goto search;
4915 }
4916
4917 if (loop < LOOP_CACHING_WAIT) {
4918 loop++;
4919 goto search;
4920 }
4921
4922 if (loop == LOOP_ALLOC_CHUNK) {
4923 empty_size = 0;
4924 empty_cluster = 0;
4925 }
4926
4927 if (allowed_chunk_alloc) {
4928 ret = do_chunk_alloc(trans, root, num_bytes +
4929 2 * 1024 * 1024, data, 1);
4930 allowed_chunk_alloc = 0;
4931 done_chunk_alloc = 1;
4932 } else if (!done_chunk_alloc) {
4933 space_info->force_alloc = 1;
4934 }
4935
4936 if (loop < LOOP_NO_EMPTY_SIZE) {
4937 loop++;
4938 goto search;
4939 }
4940 ret = -ENOSPC;
4941 } else if (!ins->objectid) {
4942 ret = -ENOSPC;
4943 }
4944
4945 /* we found what we needed */
4946 if (ins->objectid) {
4947 if (!(data & BTRFS_BLOCK_GROUP_DATA))
4948 trans->block_group = block_group->key.objectid;
4949
4950 btrfs_put_block_group(block_group);
4951 ret = 0;
4952 }
4953
4954 return ret;
4955 }
4956
4957 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
4958 int dump_block_groups)
4959 {
4960 struct btrfs_block_group_cache *cache;
4961 int index = 0;
4962
4963 spin_lock(&info->lock);
4964 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
4965 (unsigned long long)(info->total_bytes - info->bytes_used -
4966 info->bytes_pinned - info->bytes_reserved -
4967 info->bytes_readonly),
4968 (info->full) ? "" : "not ");
4969 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
4970 "reserved=%llu, may_use=%llu, readonly=%llu\n",
4971 (unsigned long long)info->total_bytes,
4972 (unsigned long long)info->bytes_used,
4973 (unsigned long long)info->bytes_pinned,
4974 (unsigned long long)info->bytes_reserved,
4975 (unsigned long long)info->bytes_may_use,
4976 (unsigned long long)info->bytes_readonly);
4977 spin_unlock(&info->lock);
4978
4979 if (!dump_block_groups)
4980 return;
4981
4982 down_read(&info->groups_sem);
4983 again:
4984 list_for_each_entry(cache, &info->block_groups[index], list) {
4985 spin_lock(&cache->lock);
4986 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
4987 "%llu pinned %llu reserved\n",
4988 (unsigned long long)cache->key.objectid,
4989 (unsigned long long)cache->key.offset,
4990 (unsigned long long)btrfs_block_group_used(&cache->item),
4991 (unsigned long long)cache->pinned,
4992 (unsigned long long)cache->reserved);
4993 btrfs_dump_free_space(cache, bytes);
4994 spin_unlock(&cache->lock);
4995 }
4996 if (++index < BTRFS_NR_RAID_TYPES)
4997 goto again;
4998 up_read(&info->groups_sem);
4999 }
5000
5001 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5002 struct btrfs_root *root,
5003 u64 num_bytes, u64 min_alloc_size,
5004 u64 empty_size, u64 hint_byte,
5005 u64 search_end, struct btrfs_key *ins,
5006 u64 data)
5007 {
5008 int ret;
5009 u64 search_start = 0;
5010
5011 data = btrfs_get_alloc_profile(root, data);
5012 again:
5013 /*
5014 * the only place that sets empty_size is btrfs_realloc_node, which
5015 * is not called recursively on allocations
5016 */
5017 if (empty_size || root->ref_cows)
5018 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5019 num_bytes + 2 * 1024 * 1024, data, 0);
5020
5021 WARN_ON(num_bytes < root->sectorsize);
5022 ret = find_free_extent(trans, root, num_bytes, empty_size,
5023 search_start, search_end, hint_byte,
5024 ins, data);
5025
5026 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5027 num_bytes = num_bytes >> 1;
5028 num_bytes = num_bytes & ~(root->sectorsize - 1);
5029 num_bytes = max(num_bytes, min_alloc_size);
5030 do_chunk_alloc(trans, root->fs_info->extent_root,
5031 num_bytes, data, 1);
5032 goto again;
5033 }
5034 if (ret == -ENOSPC) {
5035 struct btrfs_space_info *sinfo;
5036
5037 sinfo = __find_space_info(root->fs_info, data);
5038 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5039 "wanted %llu\n", (unsigned long long)data,
5040 (unsigned long long)num_bytes);
5041 dump_space_info(sinfo, num_bytes, 1);
5042 }
5043
5044 return ret;
5045 }
5046
5047 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5048 {
5049 struct btrfs_block_group_cache *cache;
5050 int ret = 0;
5051
5052 cache = btrfs_lookup_block_group(root->fs_info, start);
5053 if (!cache) {
5054 printk(KERN_ERR "Unable to find block group for %llu\n",
5055 (unsigned long long)start);
5056 return -ENOSPC;
5057 }
5058
5059 ret = btrfs_discard_extent(root, start, len);
5060
5061 btrfs_add_free_space(cache, start, len);
5062 update_reserved_bytes(cache, len, 0, 1);
5063 btrfs_put_block_group(cache);
5064
5065 return ret;
5066 }
5067
5068 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5069 struct btrfs_root *root,
5070 u64 parent, u64 root_objectid,
5071 u64 flags, u64 owner, u64 offset,
5072 struct btrfs_key *ins, int ref_mod)
5073 {
5074 int ret;
5075 struct btrfs_fs_info *fs_info = root->fs_info;
5076 struct btrfs_extent_item *extent_item;
5077 struct btrfs_extent_inline_ref *iref;
5078 struct btrfs_path *path;
5079 struct extent_buffer *leaf;
5080 int type;
5081 u32 size;
5082
5083 if (parent > 0)
5084 type = BTRFS_SHARED_DATA_REF_KEY;
5085 else
5086 type = BTRFS_EXTENT_DATA_REF_KEY;
5087
5088 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5089
5090 path = btrfs_alloc_path();
5091 BUG_ON(!path);
5092
5093 path->leave_spinning = 1;
5094 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5095 ins, size);
5096 BUG_ON(ret);
5097
5098 leaf = path->nodes[0];
5099 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5100 struct btrfs_extent_item);
5101 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5102 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5103 btrfs_set_extent_flags(leaf, extent_item,
5104 flags | BTRFS_EXTENT_FLAG_DATA);
5105
5106 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5107 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5108 if (parent > 0) {
5109 struct btrfs_shared_data_ref *ref;
5110 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5111 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5112 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5113 } else {
5114 struct btrfs_extent_data_ref *ref;
5115 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5116 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5117 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5118 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5119 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5120 }
5121
5122 btrfs_mark_buffer_dirty(path->nodes[0]);
5123 btrfs_free_path(path);
5124
5125 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5126 if (ret) {
5127 printk(KERN_ERR "btrfs update block group failed for %llu "
5128 "%llu\n", (unsigned long long)ins->objectid,
5129 (unsigned long long)ins->offset);
5130 BUG();
5131 }
5132 return ret;
5133 }
5134
5135 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5136 struct btrfs_root *root,
5137 u64 parent, u64 root_objectid,
5138 u64 flags, struct btrfs_disk_key *key,
5139 int level, struct btrfs_key *ins)
5140 {
5141 int ret;
5142 struct btrfs_fs_info *fs_info = root->fs_info;
5143 struct btrfs_extent_item *extent_item;
5144 struct btrfs_tree_block_info *block_info;
5145 struct btrfs_extent_inline_ref *iref;
5146 struct btrfs_path *path;
5147 struct extent_buffer *leaf;
5148 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5149
5150 path = btrfs_alloc_path();
5151 BUG_ON(!path);
5152
5153 path->leave_spinning = 1;
5154 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5155 ins, size);
5156 BUG_ON(ret);
5157
5158 leaf = path->nodes[0];
5159 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5160 struct btrfs_extent_item);
5161 btrfs_set_extent_refs(leaf, extent_item, 1);
5162 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5163 btrfs_set_extent_flags(leaf, extent_item,
5164 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5165 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5166
5167 btrfs_set_tree_block_key(leaf, block_info, key);
5168 btrfs_set_tree_block_level(leaf, block_info, level);
5169
5170 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5171 if (parent > 0) {
5172 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5173 btrfs_set_extent_inline_ref_type(leaf, iref,
5174 BTRFS_SHARED_BLOCK_REF_KEY);
5175 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5176 } else {
5177 btrfs_set_extent_inline_ref_type(leaf, iref,
5178 BTRFS_TREE_BLOCK_REF_KEY);
5179 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5180 }
5181
5182 btrfs_mark_buffer_dirty(leaf);
5183 btrfs_free_path(path);
5184
5185 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5186 if (ret) {
5187 printk(KERN_ERR "btrfs update block group failed for %llu "
5188 "%llu\n", (unsigned long long)ins->objectid,
5189 (unsigned long long)ins->offset);
5190 BUG();
5191 }
5192 return ret;
5193 }
5194
5195 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5196 struct btrfs_root *root,
5197 u64 root_objectid, u64 owner,
5198 u64 offset, struct btrfs_key *ins)
5199 {
5200 int ret;
5201
5202 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5203
5204 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5205 0, root_objectid, owner, offset,
5206 BTRFS_ADD_DELAYED_EXTENT, NULL);
5207 return ret;
5208 }
5209
5210 /*
5211 * this is used by the tree logging recovery code. It records that
5212 * an extent has been allocated and makes sure to clear the free
5213 * space cache bits as well
5214 */
5215 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5216 struct btrfs_root *root,
5217 u64 root_objectid, u64 owner, u64 offset,
5218 struct btrfs_key *ins)
5219 {
5220 int ret;
5221 struct btrfs_block_group_cache *block_group;
5222 struct btrfs_caching_control *caching_ctl;
5223 u64 start = ins->objectid;
5224 u64 num_bytes = ins->offset;
5225
5226 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5227 cache_block_group(block_group);
5228 caching_ctl = get_caching_control(block_group);
5229
5230 if (!caching_ctl) {
5231 BUG_ON(!block_group_cache_done(block_group));
5232 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5233 BUG_ON(ret);
5234 } else {
5235 mutex_lock(&caching_ctl->mutex);
5236
5237 if (start >= caching_ctl->progress) {
5238 ret = add_excluded_extent(root, start, num_bytes);
5239 BUG_ON(ret);
5240 } else if (start + num_bytes <= caching_ctl->progress) {
5241 ret = btrfs_remove_free_space(block_group,
5242 start, num_bytes);
5243 BUG_ON(ret);
5244 } else {
5245 num_bytes = caching_ctl->progress - start;
5246 ret = btrfs_remove_free_space(block_group,
5247 start, num_bytes);
5248 BUG_ON(ret);
5249
5250 start = caching_ctl->progress;
5251 num_bytes = ins->objectid + ins->offset -
5252 caching_ctl->progress;
5253 ret = add_excluded_extent(root, start, num_bytes);
5254 BUG_ON(ret);
5255 }
5256
5257 mutex_unlock(&caching_ctl->mutex);
5258 put_caching_control(caching_ctl);
5259 }
5260
5261 ret = update_reserved_bytes(block_group, ins->offset, 1, 1);
5262 BUG_ON(ret);
5263 btrfs_put_block_group(block_group);
5264 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5265 0, owner, offset, ins, 1);
5266 return ret;
5267 }
5268
5269 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5270 struct btrfs_root *root,
5271 u64 bytenr, u32 blocksize,
5272 int level)
5273 {
5274 struct extent_buffer *buf;
5275
5276 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5277 if (!buf)
5278 return ERR_PTR(-ENOMEM);
5279 btrfs_set_header_generation(buf, trans->transid);
5280 btrfs_set_buffer_lockdep_class(buf, level);
5281 btrfs_tree_lock(buf);
5282 clean_tree_block(trans, root, buf);
5283
5284 btrfs_set_lock_blocking(buf);
5285 btrfs_set_buffer_uptodate(buf);
5286
5287 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5288 /*
5289 * we allow two log transactions at a time, use different
5290 * EXENT bit to differentiate dirty pages.
5291 */
5292 if (root->log_transid % 2 == 0)
5293 set_extent_dirty(&root->dirty_log_pages, buf->start,
5294 buf->start + buf->len - 1, GFP_NOFS);
5295 else
5296 set_extent_new(&root->dirty_log_pages, buf->start,
5297 buf->start + buf->len - 1, GFP_NOFS);
5298 } else {
5299 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5300 buf->start + buf->len - 1, GFP_NOFS);
5301 }
5302 trans->blocks_used++;
5303 /* this returns a buffer locked for blocking */
5304 return buf;
5305 }
5306
5307 static struct btrfs_block_rsv *
5308 use_block_rsv(struct btrfs_trans_handle *trans,
5309 struct btrfs_root *root, u32 blocksize)
5310 {
5311 struct btrfs_block_rsv *block_rsv;
5312 int ret;
5313
5314 block_rsv = get_block_rsv(trans, root);
5315
5316 if (block_rsv->size == 0) {
5317 ret = reserve_metadata_bytes(block_rsv, blocksize);
5318 if (ret)
5319 return ERR_PTR(ret);
5320 return block_rsv;
5321 }
5322
5323 ret = block_rsv_use_bytes(block_rsv, blocksize);
5324 if (!ret)
5325 return block_rsv;
5326
5327 WARN_ON(1);
5328 printk(KERN_INFO"block_rsv size %llu reserved %llu freed %llu %llu\n",
5329 block_rsv->size, block_rsv->reserved,
5330 block_rsv->freed[0], block_rsv->freed[1]);
5331
5332 return ERR_PTR(-ENOSPC);
5333 }
5334
5335 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5336 {
5337 block_rsv_add_bytes(block_rsv, blocksize, 0);
5338 block_rsv_release_bytes(block_rsv, NULL, 0);
5339 }
5340
5341 /*
5342 * finds a free extent and does all the dirty work required for allocation
5343 * returns the key for the extent through ins, and a tree buffer for
5344 * the first block of the extent through buf.
5345 *
5346 * returns the tree buffer or NULL.
5347 */
5348 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5349 struct btrfs_root *root, u32 blocksize,
5350 u64 parent, u64 root_objectid,
5351 struct btrfs_disk_key *key, int level,
5352 u64 hint, u64 empty_size)
5353 {
5354 struct btrfs_key ins;
5355 struct btrfs_block_rsv *block_rsv;
5356 struct extent_buffer *buf;
5357 u64 flags = 0;
5358 int ret;
5359
5360
5361 block_rsv = use_block_rsv(trans, root, blocksize);
5362 if (IS_ERR(block_rsv))
5363 return ERR_CAST(block_rsv);
5364
5365 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5366 empty_size, hint, (u64)-1, &ins, 0);
5367 if (ret) {
5368 unuse_block_rsv(block_rsv, blocksize);
5369 return ERR_PTR(ret);
5370 }
5371
5372 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5373 blocksize, level);
5374 BUG_ON(IS_ERR(buf));
5375
5376 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5377 if (parent == 0)
5378 parent = ins.objectid;
5379 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5380 } else
5381 BUG_ON(parent > 0);
5382
5383 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5384 struct btrfs_delayed_extent_op *extent_op;
5385 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5386 BUG_ON(!extent_op);
5387 if (key)
5388 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5389 else
5390 memset(&extent_op->key, 0, sizeof(extent_op->key));
5391 extent_op->flags_to_set = flags;
5392 extent_op->update_key = 1;
5393 extent_op->update_flags = 1;
5394 extent_op->is_data = 0;
5395
5396 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5397 ins.offset, parent, root_objectid,
5398 level, BTRFS_ADD_DELAYED_EXTENT,
5399 extent_op);
5400 BUG_ON(ret);
5401 }
5402 return buf;
5403 }
5404
5405 struct walk_control {
5406 u64 refs[BTRFS_MAX_LEVEL];
5407 u64 flags[BTRFS_MAX_LEVEL];
5408 struct btrfs_key update_progress;
5409 int stage;
5410 int level;
5411 int shared_level;
5412 int update_ref;
5413 int keep_locks;
5414 int reada_slot;
5415 int reada_count;
5416 };
5417
5418 #define DROP_REFERENCE 1
5419 #define UPDATE_BACKREF 2
5420
5421 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5422 struct btrfs_root *root,
5423 struct walk_control *wc,
5424 struct btrfs_path *path)
5425 {
5426 u64 bytenr;
5427 u64 generation;
5428 u64 refs;
5429 u64 flags;
5430 u64 last = 0;
5431 u32 nritems;
5432 u32 blocksize;
5433 struct btrfs_key key;
5434 struct extent_buffer *eb;
5435 int ret;
5436 int slot;
5437 int nread = 0;
5438
5439 if (path->slots[wc->level] < wc->reada_slot) {
5440 wc->reada_count = wc->reada_count * 2 / 3;
5441 wc->reada_count = max(wc->reada_count, 2);
5442 } else {
5443 wc->reada_count = wc->reada_count * 3 / 2;
5444 wc->reada_count = min_t(int, wc->reada_count,
5445 BTRFS_NODEPTRS_PER_BLOCK(root));
5446 }
5447
5448 eb = path->nodes[wc->level];
5449 nritems = btrfs_header_nritems(eb);
5450 blocksize = btrfs_level_size(root, wc->level - 1);
5451
5452 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5453 if (nread >= wc->reada_count)
5454 break;
5455
5456 cond_resched();
5457 bytenr = btrfs_node_blockptr(eb, slot);
5458 generation = btrfs_node_ptr_generation(eb, slot);
5459
5460 if (slot == path->slots[wc->level])
5461 goto reada;
5462
5463 if (wc->stage == UPDATE_BACKREF &&
5464 generation <= root->root_key.offset)
5465 continue;
5466
5467 /* We don't lock the tree block, it's OK to be racy here */
5468 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5469 &refs, &flags);
5470 BUG_ON(ret);
5471 BUG_ON(refs == 0);
5472
5473 if (wc->stage == DROP_REFERENCE) {
5474 if (refs == 1)
5475 goto reada;
5476
5477 if (wc->level == 1 &&
5478 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5479 continue;
5480 if (!wc->update_ref ||
5481 generation <= root->root_key.offset)
5482 continue;
5483 btrfs_node_key_to_cpu(eb, &key, slot);
5484 ret = btrfs_comp_cpu_keys(&key,
5485 &wc->update_progress);
5486 if (ret < 0)
5487 continue;
5488 } else {
5489 if (wc->level == 1 &&
5490 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5491 continue;
5492 }
5493 reada:
5494 ret = readahead_tree_block(root, bytenr, blocksize,
5495 generation);
5496 if (ret)
5497 break;
5498 last = bytenr + blocksize;
5499 nread++;
5500 }
5501 wc->reada_slot = slot;
5502 }
5503
5504 /*
5505 * hepler to process tree block while walking down the tree.
5506 *
5507 * when wc->stage == UPDATE_BACKREF, this function updates
5508 * back refs for pointers in the block.
5509 *
5510 * NOTE: return value 1 means we should stop walking down.
5511 */
5512 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5513 struct btrfs_root *root,
5514 struct btrfs_path *path,
5515 struct walk_control *wc, int lookup_info)
5516 {
5517 int level = wc->level;
5518 struct extent_buffer *eb = path->nodes[level];
5519 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5520 int ret;
5521
5522 if (wc->stage == UPDATE_BACKREF &&
5523 btrfs_header_owner(eb) != root->root_key.objectid)
5524 return 1;
5525
5526 /*
5527 * when reference count of tree block is 1, it won't increase
5528 * again. once full backref flag is set, we never clear it.
5529 */
5530 if (lookup_info &&
5531 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5532 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5533 BUG_ON(!path->locks[level]);
5534 ret = btrfs_lookup_extent_info(trans, root,
5535 eb->start, eb->len,
5536 &wc->refs[level],
5537 &wc->flags[level]);
5538 BUG_ON(ret);
5539 BUG_ON(wc->refs[level] == 0);
5540 }
5541
5542 if (wc->stage == DROP_REFERENCE) {
5543 if (wc->refs[level] > 1)
5544 return 1;
5545
5546 if (path->locks[level] && !wc->keep_locks) {
5547 btrfs_tree_unlock(eb);
5548 path->locks[level] = 0;
5549 }
5550 return 0;
5551 }
5552
5553 /* wc->stage == UPDATE_BACKREF */
5554 if (!(wc->flags[level] & flag)) {
5555 BUG_ON(!path->locks[level]);
5556 ret = btrfs_inc_ref(trans, root, eb, 1);
5557 BUG_ON(ret);
5558 ret = btrfs_dec_ref(trans, root, eb, 0);
5559 BUG_ON(ret);
5560 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5561 eb->len, flag, 0);
5562 BUG_ON(ret);
5563 wc->flags[level] |= flag;
5564 }
5565
5566 /*
5567 * the block is shared by multiple trees, so it's not good to
5568 * keep the tree lock
5569 */
5570 if (path->locks[level] && level > 0) {
5571 btrfs_tree_unlock(eb);
5572 path->locks[level] = 0;
5573 }
5574 return 0;
5575 }
5576
5577 /*
5578 * hepler to process tree block pointer.
5579 *
5580 * when wc->stage == DROP_REFERENCE, this function checks
5581 * reference count of the block pointed to. if the block
5582 * is shared and we need update back refs for the subtree
5583 * rooted at the block, this function changes wc->stage to
5584 * UPDATE_BACKREF. if the block is shared and there is no
5585 * need to update back, this function drops the reference
5586 * to the block.
5587 *
5588 * NOTE: return value 1 means we should stop walking down.
5589 */
5590 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5591 struct btrfs_root *root,
5592 struct btrfs_path *path,
5593 struct walk_control *wc, int *lookup_info)
5594 {
5595 u64 bytenr;
5596 u64 generation;
5597 u64 parent;
5598 u32 blocksize;
5599 struct btrfs_key key;
5600 struct extent_buffer *next;
5601 int level = wc->level;
5602 int reada = 0;
5603 int ret = 0;
5604
5605 generation = btrfs_node_ptr_generation(path->nodes[level],
5606 path->slots[level]);
5607 /*
5608 * if the lower level block was created before the snapshot
5609 * was created, we know there is no need to update back refs
5610 * for the subtree
5611 */
5612 if (wc->stage == UPDATE_BACKREF &&
5613 generation <= root->root_key.offset) {
5614 *lookup_info = 1;
5615 return 1;
5616 }
5617
5618 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5619 blocksize = btrfs_level_size(root, level - 1);
5620
5621 next = btrfs_find_tree_block(root, bytenr, blocksize);
5622 if (!next) {
5623 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5624 if (!next)
5625 return -ENOMEM;
5626 reada = 1;
5627 }
5628 btrfs_tree_lock(next);
5629 btrfs_set_lock_blocking(next);
5630
5631 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5632 &wc->refs[level - 1],
5633 &wc->flags[level - 1]);
5634 BUG_ON(ret);
5635 BUG_ON(wc->refs[level - 1] == 0);
5636 *lookup_info = 0;
5637
5638 if (wc->stage == DROP_REFERENCE) {
5639 if (wc->refs[level - 1] > 1) {
5640 if (level == 1 &&
5641 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5642 goto skip;
5643
5644 if (!wc->update_ref ||
5645 generation <= root->root_key.offset)
5646 goto skip;
5647
5648 btrfs_node_key_to_cpu(path->nodes[level], &key,
5649 path->slots[level]);
5650 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5651 if (ret < 0)
5652 goto skip;
5653
5654 wc->stage = UPDATE_BACKREF;
5655 wc->shared_level = level - 1;
5656 }
5657 } else {
5658 if (level == 1 &&
5659 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5660 goto skip;
5661 }
5662
5663 if (!btrfs_buffer_uptodate(next, generation)) {
5664 btrfs_tree_unlock(next);
5665 free_extent_buffer(next);
5666 next = NULL;
5667 *lookup_info = 1;
5668 }
5669
5670 if (!next) {
5671 if (reada && level == 1)
5672 reada_walk_down(trans, root, wc, path);
5673 next = read_tree_block(root, bytenr, blocksize, generation);
5674 btrfs_tree_lock(next);
5675 btrfs_set_lock_blocking(next);
5676 }
5677
5678 level--;
5679 BUG_ON(level != btrfs_header_level(next));
5680 path->nodes[level] = next;
5681 path->slots[level] = 0;
5682 path->locks[level] = 1;
5683 wc->level = level;
5684 if (wc->level == 1)
5685 wc->reada_slot = 0;
5686 return 0;
5687 skip:
5688 wc->refs[level - 1] = 0;
5689 wc->flags[level - 1] = 0;
5690 if (wc->stage == DROP_REFERENCE) {
5691 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5692 parent = path->nodes[level]->start;
5693 } else {
5694 BUG_ON(root->root_key.objectid !=
5695 btrfs_header_owner(path->nodes[level]));
5696 parent = 0;
5697 }
5698
5699 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
5700 root->root_key.objectid, level - 1, 0);
5701 BUG_ON(ret);
5702 }
5703 btrfs_tree_unlock(next);
5704 free_extent_buffer(next);
5705 *lookup_info = 1;
5706 return 1;
5707 }
5708
5709 /*
5710 * hepler to process tree block while walking up the tree.
5711 *
5712 * when wc->stage == DROP_REFERENCE, this function drops
5713 * reference count on the block.
5714 *
5715 * when wc->stage == UPDATE_BACKREF, this function changes
5716 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5717 * to UPDATE_BACKREF previously while processing the block.
5718 *
5719 * NOTE: return value 1 means we should stop walking up.
5720 */
5721 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5722 struct btrfs_root *root,
5723 struct btrfs_path *path,
5724 struct walk_control *wc)
5725 {
5726 int ret;
5727 int level = wc->level;
5728 struct extent_buffer *eb = path->nodes[level];
5729 u64 parent = 0;
5730
5731 if (wc->stage == UPDATE_BACKREF) {
5732 BUG_ON(wc->shared_level < level);
5733 if (level < wc->shared_level)
5734 goto out;
5735
5736 ret = find_next_key(path, level + 1, &wc->update_progress);
5737 if (ret > 0)
5738 wc->update_ref = 0;
5739
5740 wc->stage = DROP_REFERENCE;
5741 wc->shared_level = -1;
5742 path->slots[level] = 0;
5743
5744 /*
5745 * check reference count again if the block isn't locked.
5746 * we should start walking down the tree again if reference
5747 * count is one.
5748 */
5749 if (!path->locks[level]) {
5750 BUG_ON(level == 0);
5751 btrfs_tree_lock(eb);
5752 btrfs_set_lock_blocking(eb);
5753 path->locks[level] = 1;
5754
5755 ret = btrfs_lookup_extent_info(trans, root,
5756 eb->start, eb->len,
5757 &wc->refs[level],
5758 &wc->flags[level]);
5759 BUG_ON(ret);
5760 BUG_ON(wc->refs[level] == 0);
5761 if (wc->refs[level] == 1) {
5762 btrfs_tree_unlock(eb);
5763 path->locks[level] = 0;
5764 return 1;
5765 }
5766 }
5767 }
5768
5769 /* wc->stage == DROP_REFERENCE */
5770 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5771
5772 if (wc->refs[level] == 1) {
5773 if (level == 0) {
5774 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5775 ret = btrfs_dec_ref(trans, root, eb, 1);
5776 else
5777 ret = btrfs_dec_ref(trans, root, eb, 0);
5778 BUG_ON(ret);
5779 }
5780 /* make block locked assertion in clean_tree_block happy */
5781 if (!path->locks[level] &&
5782 btrfs_header_generation(eb) == trans->transid) {
5783 btrfs_tree_lock(eb);
5784 btrfs_set_lock_blocking(eb);
5785 path->locks[level] = 1;
5786 }
5787 clean_tree_block(trans, root, eb);
5788 }
5789
5790 if (eb == root->node) {
5791 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5792 parent = eb->start;
5793 else
5794 BUG_ON(root->root_key.objectid !=
5795 btrfs_header_owner(eb));
5796 } else {
5797 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5798 parent = path->nodes[level + 1]->start;
5799 else
5800 BUG_ON(root->root_key.objectid !=
5801 btrfs_header_owner(path->nodes[level + 1]));
5802 }
5803
5804 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
5805 out:
5806 wc->refs[level] = 0;
5807 wc->flags[level] = 0;
5808 return 0;
5809 }
5810
5811 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5812 struct btrfs_root *root,
5813 struct btrfs_path *path,
5814 struct walk_control *wc)
5815 {
5816 int level = wc->level;
5817 int lookup_info = 1;
5818 int ret;
5819
5820 while (level >= 0) {
5821 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5822 if (ret > 0)
5823 break;
5824
5825 if (level == 0)
5826 break;
5827
5828 if (path->slots[level] >=
5829 btrfs_header_nritems(path->nodes[level]))
5830 break;
5831
5832 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5833 if (ret > 0) {
5834 path->slots[level]++;
5835 continue;
5836 } else if (ret < 0)
5837 return ret;
5838 level = wc->level;
5839 }
5840 return 0;
5841 }
5842
5843 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5844 struct btrfs_root *root,
5845 struct btrfs_path *path,
5846 struct walk_control *wc, int max_level)
5847 {
5848 int level = wc->level;
5849 int ret;
5850
5851 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5852 while (level < max_level && path->nodes[level]) {
5853 wc->level = level;
5854 if (path->slots[level] + 1 <
5855 btrfs_header_nritems(path->nodes[level])) {
5856 path->slots[level]++;
5857 return 0;
5858 } else {
5859 ret = walk_up_proc(trans, root, path, wc);
5860 if (ret > 0)
5861 return 0;
5862
5863 if (path->locks[level]) {
5864 btrfs_tree_unlock(path->nodes[level]);
5865 path->locks[level] = 0;
5866 }
5867 free_extent_buffer(path->nodes[level]);
5868 path->nodes[level] = NULL;
5869 level++;
5870 }
5871 }
5872 return 1;
5873 }
5874
5875 /*
5876 * drop a subvolume tree.
5877 *
5878 * this function traverses the tree freeing any blocks that only
5879 * referenced by the tree.
5880 *
5881 * when a shared tree block is found. this function decreases its
5882 * reference count by one. if update_ref is true, this function
5883 * also make sure backrefs for the shared block and all lower level
5884 * blocks are properly updated.
5885 */
5886 int btrfs_drop_snapshot(struct btrfs_root *root,
5887 struct btrfs_block_rsv *block_rsv, int update_ref)
5888 {
5889 struct btrfs_path *path;
5890 struct btrfs_trans_handle *trans;
5891 struct btrfs_root *tree_root = root->fs_info->tree_root;
5892 struct btrfs_root_item *root_item = &root->root_item;
5893 struct walk_control *wc;
5894 struct btrfs_key key;
5895 int err = 0;
5896 int ret;
5897 int level;
5898
5899 path = btrfs_alloc_path();
5900 BUG_ON(!path);
5901
5902 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5903 BUG_ON(!wc);
5904
5905 trans = btrfs_start_transaction(tree_root, 0);
5906 if (block_rsv)
5907 trans->block_rsv = block_rsv;
5908
5909 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5910 level = btrfs_header_level(root->node);
5911 path->nodes[level] = btrfs_lock_root_node(root);
5912 btrfs_set_lock_blocking(path->nodes[level]);
5913 path->slots[level] = 0;
5914 path->locks[level] = 1;
5915 memset(&wc->update_progress, 0,
5916 sizeof(wc->update_progress));
5917 } else {
5918 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5919 memcpy(&wc->update_progress, &key,
5920 sizeof(wc->update_progress));
5921
5922 level = root_item->drop_level;
5923 BUG_ON(level == 0);
5924 path->lowest_level = level;
5925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5926 path->lowest_level = 0;
5927 if (ret < 0) {
5928 err = ret;
5929 goto out;
5930 }
5931 WARN_ON(ret > 0);
5932
5933 /*
5934 * unlock our path, this is safe because only this
5935 * function is allowed to delete this snapshot
5936 */
5937 btrfs_unlock_up_safe(path, 0);
5938
5939 level = btrfs_header_level(root->node);
5940 while (1) {
5941 btrfs_tree_lock(path->nodes[level]);
5942 btrfs_set_lock_blocking(path->nodes[level]);
5943
5944 ret = btrfs_lookup_extent_info(trans, root,
5945 path->nodes[level]->start,
5946 path->nodes[level]->len,
5947 &wc->refs[level],
5948 &wc->flags[level]);
5949 BUG_ON(ret);
5950 BUG_ON(wc->refs[level] == 0);
5951
5952 if (level == root_item->drop_level)
5953 break;
5954
5955 btrfs_tree_unlock(path->nodes[level]);
5956 WARN_ON(wc->refs[level] != 1);
5957 level--;
5958 }
5959 }
5960
5961 wc->level = level;
5962 wc->shared_level = -1;
5963 wc->stage = DROP_REFERENCE;
5964 wc->update_ref = update_ref;
5965 wc->keep_locks = 0;
5966 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
5967
5968 while (1) {
5969 ret = walk_down_tree(trans, root, path, wc);
5970 if (ret < 0) {
5971 err = ret;
5972 break;
5973 }
5974
5975 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5976 if (ret < 0) {
5977 err = ret;
5978 break;
5979 }
5980
5981 if (ret > 0) {
5982 BUG_ON(wc->stage != DROP_REFERENCE);
5983 break;
5984 }
5985
5986 if (wc->stage == DROP_REFERENCE) {
5987 level = wc->level;
5988 btrfs_node_key(path->nodes[level],
5989 &root_item->drop_progress,
5990 path->slots[level]);
5991 root_item->drop_level = level;
5992 }
5993
5994 BUG_ON(wc->level == 0);
5995 if (btrfs_should_end_transaction(trans, tree_root)) {
5996 ret = btrfs_update_root(trans, tree_root,
5997 &root->root_key,
5998 root_item);
5999 BUG_ON(ret);
6000
6001 btrfs_end_transaction_throttle(trans, tree_root);
6002 trans = btrfs_start_transaction(tree_root, 0);
6003 if (block_rsv)
6004 trans->block_rsv = block_rsv;
6005 }
6006 }
6007 btrfs_release_path(root, path);
6008 BUG_ON(err);
6009
6010 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6011 BUG_ON(ret);
6012
6013 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6014 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6015 NULL, NULL);
6016 BUG_ON(ret < 0);
6017 if (ret > 0) {
6018 ret = btrfs_del_orphan_item(trans, tree_root,
6019 root->root_key.objectid);
6020 BUG_ON(ret);
6021 }
6022 }
6023
6024 if (root->in_radix) {
6025 btrfs_free_fs_root(tree_root->fs_info, root);
6026 } else {
6027 free_extent_buffer(root->node);
6028 free_extent_buffer(root->commit_root);
6029 kfree(root);
6030 }
6031 out:
6032 btrfs_end_transaction_throttle(trans, tree_root);
6033 kfree(wc);
6034 btrfs_free_path(path);
6035 return err;
6036 }
6037
6038 /*
6039 * drop subtree rooted at tree block 'node'.
6040 *
6041 * NOTE: this function will unlock and release tree block 'node'
6042 */
6043 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6044 struct btrfs_root *root,
6045 struct extent_buffer *node,
6046 struct extent_buffer *parent)
6047 {
6048 struct btrfs_path *path;
6049 struct walk_control *wc;
6050 int level;
6051 int parent_level;
6052 int ret = 0;
6053 int wret;
6054
6055 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6056
6057 path = btrfs_alloc_path();
6058 BUG_ON(!path);
6059
6060 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6061 BUG_ON(!wc);
6062
6063 btrfs_assert_tree_locked(parent);
6064 parent_level = btrfs_header_level(parent);
6065 extent_buffer_get(parent);
6066 path->nodes[parent_level] = parent;
6067 path->slots[parent_level] = btrfs_header_nritems(parent);
6068
6069 btrfs_assert_tree_locked(node);
6070 level = btrfs_header_level(node);
6071 path->nodes[level] = node;
6072 path->slots[level] = 0;
6073 path->locks[level] = 1;
6074
6075 wc->refs[parent_level] = 1;
6076 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6077 wc->level = level;
6078 wc->shared_level = -1;
6079 wc->stage = DROP_REFERENCE;
6080 wc->update_ref = 0;
6081 wc->keep_locks = 1;
6082 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6083
6084 while (1) {
6085 wret = walk_down_tree(trans, root, path, wc);
6086 if (wret < 0) {
6087 ret = wret;
6088 break;
6089 }
6090
6091 wret = walk_up_tree(trans, root, path, wc, parent_level);
6092 if (wret < 0)
6093 ret = wret;
6094 if (wret != 0)
6095 break;
6096 }
6097
6098 kfree(wc);
6099 btrfs_free_path(path);
6100 return ret;
6101 }
6102
6103 #if 0
6104 static unsigned long calc_ra(unsigned long start, unsigned long last,
6105 unsigned long nr)
6106 {
6107 return min(last, start + nr - 1);
6108 }
6109
6110 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
6111 u64 len)
6112 {
6113 u64 page_start;
6114 u64 page_end;
6115 unsigned long first_index;
6116 unsigned long last_index;
6117 unsigned long i;
6118 struct page *page;
6119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6120 struct file_ra_state *ra;
6121 struct btrfs_ordered_extent *ordered;
6122 unsigned int total_read = 0;
6123 unsigned int total_dirty = 0;
6124 int ret = 0;
6125
6126 ra = kzalloc(sizeof(*ra), GFP_NOFS);
6127
6128 mutex_lock(&inode->i_mutex);
6129 first_index = start >> PAGE_CACHE_SHIFT;
6130 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6131
6132 /* make sure the dirty trick played by the caller work */
6133 ret = invalidate_inode_pages2_range(inode->i_mapping,
6134 first_index, last_index);
6135 if (ret)
6136 goto out_unlock;
6137
6138 file_ra_state_init(ra, inode->i_mapping);
6139
6140 for (i = first_index ; i <= last_index; i++) {
6141 if (total_read % ra->ra_pages == 0) {
6142 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
6143 calc_ra(i, last_index, ra->ra_pages));
6144 }
6145 total_read++;
6146 again:
6147 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
6148 BUG_ON(1);
6149 page = grab_cache_page(inode->i_mapping, i);
6150 if (!page) {
6151 ret = -ENOMEM;
6152 goto out_unlock;
6153 }
6154 if (!PageUptodate(page)) {
6155 btrfs_readpage(NULL, page);
6156 lock_page(page);
6157 if (!PageUptodate(page)) {
6158 unlock_page(page);
6159 page_cache_release(page);
6160 ret = -EIO;
6161 goto out_unlock;
6162 }
6163 }
6164 wait_on_page_writeback(page);
6165
6166 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6167 page_end = page_start + PAGE_CACHE_SIZE - 1;
6168 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
6169
6170 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6171 if (ordered) {
6172 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6173 unlock_page(page);
6174 page_cache_release(page);
6175 btrfs_start_ordered_extent(inode, ordered, 1);
6176 btrfs_put_ordered_extent(ordered);
6177 goto again;
6178 }
6179 set_page_extent_mapped(page);
6180
6181 if (i == first_index)
6182 set_extent_bits(io_tree, page_start, page_end,
6183 EXTENT_BOUNDARY, GFP_NOFS);
6184 btrfs_set_extent_delalloc(inode, page_start, page_end);
6185
6186 set_page_dirty(page);
6187 total_dirty++;
6188
6189 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6190 unlock_page(page);
6191 page_cache_release(page);
6192 }
6193
6194 out_unlock:
6195 kfree(ra);
6196 mutex_unlock(&inode->i_mutex);
6197 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
6198 return ret;
6199 }
6200
6201 static noinline int relocate_data_extent(struct inode *reloc_inode,
6202 struct btrfs_key *extent_key,
6203 u64 offset)
6204 {
6205 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6206 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6207 struct extent_map *em;
6208 u64 start = extent_key->objectid - offset;
6209 u64 end = start + extent_key->offset - 1;
6210
6211 em = alloc_extent_map(GFP_NOFS);
6212 BUG_ON(!em || IS_ERR(em));
6213
6214 em->start = start;
6215 em->len = extent_key->offset;
6216 em->block_len = extent_key->offset;
6217 em->block_start = extent_key->objectid;
6218 em->bdev = root->fs_info->fs_devices->latest_bdev;
6219 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6220
6221 /* setup extent map to cheat btrfs_readpage */
6222 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6223 while (1) {
6224 int ret;
6225 write_lock(&em_tree->lock);
6226 ret = add_extent_mapping(em_tree, em);
6227 write_unlock(&em_tree->lock);
6228 if (ret != -EEXIST) {
6229 free_extent_map(em);
6230 break;
6231 }
6232 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
6233 }
6234 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6235
6236 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
6237 }
6238
6239 struct btrfs_ref_path {
6240 u64 extent_start;
6241 u64 nodes[BTRFS_MAX_LEVEL];
6242 u64 root_objectid;
6243 u64 root_generation;
6244 u64 owner_objectid;
6245 u32 num_refs;
6246 int lowest_level;
6247 int current_level;
6248 int shared_level;
6249
6250 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6251 u64 new_nodes[BTRFS_MAX_LEVEL];
6252 };
6253
6254 struct disk_extent {
6255 u64 ram_bytes;
6256 u64 disk_bytenr;
6257 u64 disk_num_bytes;
6258 u64 offset;
6259 u64 num_bytes;
6260 u8 compression;
6261 u8 encryption;
6262 u16 other_encoding;
6263 };
6264
6265 static int is_cowonly_root(u64 root_objectid)
6266 {
6267 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6268 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6269 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6270 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
6271 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6272 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
6273 return 1;
6274 return 0;
6275 }
6276
6277 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
6278 struct btrfs_root *extent_root,
6279 struct btrfs_ref_path *ref_path,
6280 int first_time)
6281 {
6282 struct extent_buffer *leaf;
6283 struct btrfs_path *path;
6284 struct btrfs_extent_ref *ref;
6285 struct btrfs_key key;
6286 struct btrfs_key found_key;
6287 u64 bytenr;
6288 u32 nritems;
6289 int level;
6290 int ret = 1;
6291
6292 path = btrfs_alloc_path();
6293 if (!path)
6294 return -ENOMEM;
6295
6296 if (first_time) {
6297 ref_path->lowest_level = -1;
6298 ref_path->current_level = -1;
6299 ref_path->shared_level = -1;
6300 goto walk_up;
6301 }
6302 walk_down:
6303 level = ref_path->current_level - 1;
6304 while (level >= -1) {
6305 u64 parent;
6306 if (level < ref_path->lowest_level)
6307 break;
6308
6309 if (level >= 0)
6310 bytenr = ref_path->nodes[level];
6311 else
6312 bytenr = ref_path->extent_start;
6313 BUG_ON(bytenr == 0);
6314
6315 parent = ref_path->nodes[level + 1];
6316 ref_path->nodes[level + 1] = 0;
6317 ref_path->current_level = level;
6318 BUG_ON(parent == 0);
6319
6320 key.objectid = bytenr;
6321 key.offset = parent + 1;
6322 key.type = BTRFS_EXTENT_REF_KEY;
6323
6324 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6325 if (ret < 0)
6326 goto out;
6327 BUG_ON(ret == 0);
6328
6329 leaf = path->nodes[0];
6330 nritems = btrfs_header_nritems(leaf);
6331 if (path->slots[0] >= nritems) {
6332 ret = btrfs_next_leaf(extent_root, path);
6333 if (ret < 0)
6334 goto out;
6335 if (ret > 0)
6336 goto next;
6337 leaf = path->nodes[0];
6338 }
6339
6340 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6341 if (found_key.objectid == bytenr &&
6342 found_key.type == BTRFS_EXTENT_REF_KEY) {
6343 if (level < ref_path->shared_level)
6344 ref_path->shared_level = level;
6345 goto found;
6346 }
6347 next:
6348 level--;
6349 btrfs_release_path(extent_root, path);
6350 cond_resched();
6351 }
6352 /* reached lowest level */
6353 ret = 1;
6354 goto out;
6355 walk_up:
6356 level = ref_path->current_level;
6357 while (level < BTRFS_MAX_LEVEL - 1) {
6358 u64 ref_objectid;
6359
6360 if (level >= 0)
6361 bytenr = ref_path->nodes[level];
6362 else
6363 bytenr = ref_path->extent_start;
6364
6365 BUG_ON(bytenr == 0);
6366
6367 key.objectid = bytenr;
6368 key.offset = 0;
6369 key.type = BTRFS_EXTENT_REF_KEY;
6370
6371 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6372 if (ret < 0)
6373 goto out;
6374
6375 leaf = path->nodes[0];
6376 nritems = btrfs_header_nritems(leaf);
6377 if (path->slots[0] >= nritems) {
6378 ret = btrfs_next_leaf(extent_root, path);
6379 if (ret < 0)
6380 goto out;
6381 if (ret > 0) {
6382 /* the extent was freed by someone */
6383 if (ref_path->lowest_level == level)
6384 goto out;
6385 btrfs_release_path(extent_root, path);
6386 goto walk_down;
6387 }
6388 leaf = path->nodes[0];
6389 }
6390
6391 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6392 if (found_key.objectid != bytenr ||
6393 found_key.type != BTRFS_EXTENT_REF_KEY) {
6394 /* the extent was freed by someone */
6395 if (ref_path->lowest_level == level) {
6396 ret = 1;
6397 goto out;
6398 }
6399 btrfs_release_path(extent_root, path);
6400 goto walk_down;
6401 }
6402 found:
6403 ref = btrfs_item_ptr(leaf, path->slots[0],
6404 struct btrfs_extent_ref);
6405 ref_objectid = btrfs_ref_objectid(leaf, ref);
6406 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6407 if (first_time) {
6408 level = (int)ref_objectid;
6409 BUG_ON(level >= BTRFS_MAX_LEVEL);
6410 ref_path->lowest_level = level;
6411 ref_path->current_level = level;
6412 ref_path->nodes[level] = bytenr;
6413 } else {
6414 WARN_ON(ref_objectid != level);
6415 }
6416 } else {
6417 WARN_ON(level != -1);
6418 }
6419 first_time = 0;
6420
6421 if (ref_path->lowest_level == level) {
6422 ref_path->owner_objectid = ref_objectid;
6423 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6424 }
6425
6426 /*
6427 * the block is tree root or the block isn't in reference
6428 * counted tree.
6429 */
6430 if (found_key.objectid == found_key.offset ||
6431 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6432 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6433 ref_path->root_generation =
6434 btrfs_ref_generation(leaf, ref);
6435 if (level < 0) {
6436 /* special reference from the tree log */
6437 ref_path->nodes[0] = found_key.offset;
6438 ref_path->current_level = 0;
6439 }
6440 ret = 0;
6441 goto out;
6442 }
6443
6444 level++;
6445 BUG_ON(ref_path->nodes[level] != 0);
6446 ref_path->nodes[level] = found_key.offset;
6447 ref_path->current_level = level;
6448
6449 /*
6450 * the reference was created in the running transaction,
6451 * no need to continue walking up.
6452 */
6453 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6454 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6455 ref_path->root_generation =
6456 btrfs_ref_generation(leaf, ref);
6457 ret = 0;
6458 goto out;
6459 }
6460
6461 btrfs_release_path(extent_root, path);
6462 cond_resched();
6463 }
6464 /* reached max tree level, but no tree root found. */
6465 BUG();
6466 out:
6467 btrfs_free_path(path);
6468 return ret;
6469 }
6470
6471 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6472 struct btrfs_root *extent_root,
6473 struct btrfs_ref_path *ref_path,
6474 u64 extent_start)
6475 {
6476 memset(ref_path, 0, sizeof(*ref_path));
6477 ref_path->extent_start = extent_start;
6478
6479 return __next_ref_path(trans, extent_root, ref_path, 1);
6480 }
6481
6482 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6483 struct btrfs_root *extent_root,
6484 struct btrfs_ref_path *ref_path)
6485 {
6486 return __next_ref_path(trans, extent_root, ref_path, 0);
6487 }
6488
6489 static noinline int get_new_locations(struct inode *reloc_inode,
6490 struct btrfs_key *extent_key,
6491 u64 offset, int no_fragment,
6492 struct disk_extent **extents,
6493 int *nr_extents)
6494 {
6495 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6496 struct btrfs_path *path;
6497 struct btrfs_file_extent_item *fi;
6498 struct extent_buffer *leaf;
6499 struct disk_extent *exts = *extents;
6500 struct btrfs_key found_key;
6501 u64 cur_pos;
6502 u64 last_byte;
6503 u32 nritems;
6504 int nr = 0;
6505 int max = *nr_extents;
6506 int ret;
6507
6508 WARN_ON(!no_fragment && *extents);
6509 if (!exts) {
6510 max = 1;
6511 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
6512 if (!exts)
6513 return -ENOMEM;
6514 }
6515
6516 path = btrfs_alloc_path();
6517 BUG_ON(!path);
6518
6519 cur_pos = extent_key->objectid - offset;
6520 last_byte = extent_key->objectid + extent_key->offset;
6521 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
6522 cur_pos, 0);
6523 if (ret < 0)
6524 goto out;
6525 if (ret > 0) {
6526 ret = -ENOENT;
6527 goto out;
6528 }
6529
6530 while (1) {
6531 leaf = path->nodes[0];
6532 nritems = btrfs_header_nritems(leaf);
6533 if (path->slots[0] >= nritems) {
6534 ret = btrfs_next_leaf(root, path);
6535 if (ret < 0)
6536 goto out;
6537 if (ret > 0)
6538 break;
6539 leaf = path->nodes[0];
6540 }
6541
6542 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6543 if (found_key.offset != cur_pos ||
6544 found_key.type != BTRFS_EXTENT_DATA_KEY ||
6545 found_key.objectid != reloc_inode->i_ino)
6546 break;
6547
6548 fi = btrfs_item_ptr(leaf, path->slots[0],
6549 struct btrfs_file_extent_item);
6550 if (btrfs_file_extent_type(leaf, fi) !=
6551 BTRFS_FILE_EXTENT_REG ||
6552 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6553 break;
6554
6555 if (nr == max) {
6556 struct disk_extent *old = exts;
6557 max *= 2;
6558 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
6559 memcpy(exts, old, sizeof(*exts) * nr);
6560 if (old != *extents)
6561 kfree(old);
6562 }
6563
6564 exts[nr].disk_bytenr =
6565 btrfs_file_extent_disk_bytenr(leaf, fi);
6566 exts[nr].disk_num_bytes =
6567 btrfs_file_extent_disk_num_bytes(leaf, fi);
6568 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
6569 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6570 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
6571 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
6572 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
6573 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
6574 fi);
6575 BUG_ON(exts[nr].offset > 0);
6576 BUG_ON(exts[nr].compression || exts[nr].encryption);
6577 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
6578
6579 cur_pos += exts[nr].num_bytes;
6580 nr++;
6581
6582 if (cur_pos + offset >= last_byte)
6583 break;
6584
6585 if (no_fragment) {
6586 ret = 1;
6587 goto out;
6588 }
6589 path->slots[0]++;
6590 }
6591
6592 BUG_ON(cur_pos + offset > last_byte);
6593 if (cur_pos + offset < last_byte) {
6594 ret = -ENOENT;
6595 goto out;
6596 }
6597 ret = 0;
6598 out:
6599 btrfs_free_path(path);
6600 if (ret) {
6601 if (exts != *extents)
6602 kfree(exts);
6603 } else {
6604 *extents = exts;
6605 *nr_extents = nr;
6606 }
6607 return ret;
6608 }
6609
6610 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
6611 struct btrfs_root *root,
6612 struct btrfs_path *path,
6613 struct btrfs_key *extent_key,
6614 struct btrfs_key *leaf_key,
6615 struct btrfs_ref_path *ref_path,
6616 struct disk_extent *new_extents,
6617 int nr_extents)
6618 {
6619 struct extent_buffer *leaf;
6620 struct btrfs_file_extent_item *fi;
6621 struct inode *inode = NULL;
6622 struct btrfs_key key;
6623 u64 lock_start = 0;
6624 u64 lock_end = 0;
6625 u64 num_bytes;
6626 u64 ext_offset;
6627 u64 search_end = (u64)-1;
6628 u32 nritems;
6629 int nr_scaned = 0;
6630 int extent_locked = 0;
6631 int extent_type;
6632 int ret;
6633
6634 memcpy(&key, leaf_key, sizeof(key));
6635 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6636 if (key.objectid < ref_path->owner_objectid ||
6637 (key.objectid == ref_path->owner_objectid &&
6638 key.type < BTRFS_EXTENT_DATA_KEY)) {
6639 key.objectid = ref_path->owner_objectid;
6640 key.type = BTRFS_EXTENT_DATA_KEY;
6641 key.offset = 0;
6642 }
6643 }
6644
6645 while (1) {
6646 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
6647 if (ret < 0)
6648 goto out;
6649
6650 leaf = path->nodes[0];
6651 nritems = btrfs_header_nritems(leaf);
6652 next:
6653 if (extent_locked && ret > 0) {
6654 /*
6655 * the file extent item was modified by someone
6656 * before the extent got locked.
6657 */
6658 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6659 lock_end, GFP_NOFS);
6660 extent_locked = 0;
6661 }
6662
6663 if (path->slots[0] >= nritems) {
6664 if (++nr_scaned > 2)
6665 break;
6666
6667 BUG_ON(extent_locked);
6668 ret = btrfs_next_leaf(root, path);
6669 if (ret < 0)
6670 goto out;
6671 if (ret > 0)
6672 break;
6673 leaf = path->nodes[0];
6674 nritems = btrfs_header_nritems(leaf);
6675 }
6676
6677 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
6678
6679 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
6680 if ((key.objectid > ref_path->owner_objectid) ||
6681 (key.objectid == ref_path->owner_objectid &&
6682 key.type > BTRFS_EXTENT_DATA_KEY) ||
6683 key.offset >= search_end)
6684 break;
6685 }
6686
6687 if (inode && key.objectid != inode->i_ino) {
6688 BUG_ON(extent_locked);
6689 btrfs_release_path(root, path);
6690 mutex_unlock(&inode->i_mutex);
6691 iput(inode);
6692 inode = NULL;
6693 continue;
6694 }
6695
6696 if (key.type != BTRFS_EXTENT_DATA_KEY) {
6697 path->slots[0]++;
6698 ret = 1;
6699 goto next;
6700 }
6701 fi = btrfs_item_ptr(leaf, path->slots[0],
6702 struct btrfs_file_extent_item);
6703 extent_type = btrfs_file_extent_type(leaf, fi);
6704 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
6705 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
6706 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
6707 extent_key->objectid)) {
6708 path->slots[0]++;
6709 ret = 1;
6710 goto next;
6711 }
6712
6713 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
6714 ext_offset = btrfs_file_extent_offset(leaf, fi);
6715
6716 if (search_end == (u64)-1) {
6717 search_end = key.offset - ext_offset +
6718 btrfs_file_extent_ram_bytes(leaf, fi);
6719 }
6720
6721 if (!extent_locked) {
6722 lock_start = key.offset;
6723 lock_end = lock_start + num_bytes - 1;
6724 } else {
6725 if (lock_start > key.offset ||
6726 lock_end + 1 < key.offset + num_bytes) {
6727 unlock_extent(&BTRFS_I(inode)->io_tree,
6728 lock_start, lock_end, GFP_NOFS);
6729 extent_locked = 0;
6730 }
6731 }
6732
6733 if (!inode) {
6734 btrfs_release_path(root, path);
6735
6736 inode = btrfs_iget_locked(root->fs_info->sb,
6737 key.objectid, root);
6738 if (inode->i_state & I_NEW) {
6739 BTRFS_I(inode)->root = root;
6740 BTRFS_I(inode)->location.objectid =
6741 key.objectid;
6742 BTRFS_I(inode)->location.type =
6743 BTRFS_INODE_ITEM_KEY;
6744 BTRFS_I(inode)->location.offset = 0;
6745 btrfs_read_locked_inode(inode);
6746 unlock_new_inode(inode);
6747 }
6748 /*
6749 * some code call btrfs_commit_transaction while
6750 * holding the i_mutex, so we can't use mutex_lock
6751 * here.
6752 */
6753 if (is_bad_inode(inode) ||
6754 !mutex_trylock(&inode->i_mutex)) {
6755 iput(inode);
6756 inode = NULL;
6757 key.offset = (u64)-1;
6758 goto skip;
6759 }
6760 }
6761
6762 if (!extent_locked) {
6763 struct btrfs_ordered_extent *ordered;
6764
6765 btrfs_release_path(root, path);
6766
6767 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6768 lock_end, GFP_NOFS);
6769 ordered = btrfs_lookup_first_ordered_extent(inode,
6770 lock_end);
6771 if (ordered &&
6772 ordered->file_offset <= lock_end &&
6773 ordered->file_offset + ordered->len > lock_start) {
6774 unlock_extent(&BTRFS_I(inode)->io_tree,
6775 lock_start, lock_end, GFP_NOFS);
6776 btrfs_start_ordered_extent(inode, ordered, 1);
6777 btrfs_put_ordered_extent(ordered);
6778 key.offset += num_bytes;
6779 goto skip;
6780 }
6781 if (ordered)
6782 btrfs_put_ordered_extent(ordered);
6783
6784 extent_locked = 1;
6785 continue;
6786 }
6787
6788 if (nr_extents == 1) {
6789 /* update extent pointer in place */
6790 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6791 new_extents[0].disk_bytenr);
6792 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6793 new_extents[0].disk_num_bytes);
6794 btrfs_mark_buffer_dirty(leaf);
6795
6796 btrfs_drop_extent_cache(inode, key.offset,
6797 key.offset + num_bytes - 1, 0);
6798
6799 ret = btrfs_inc_extent_ref(trans, root,
6800 new_extents[0].disk_bytenr,
6801 new_extents[0].disk_num_bytes,
6802 leaf->start,
6803 root->root_key.objectid,
6804 trans->transid,
6805 key.objectid);
6806 BUG_ON(ret);
6807
6808 ret = btrfs_free_extent(trans, root,
6809 extent_key->objectid,
6810 extent_key->offset,
6811 leaf->start,
6812 btrfs_header_owner(leaf),
6813 btrfs_header_generation(leaf),
6814 key.objectid, 0);
6815 BUG_ON(ret);
6816
6817 btrfs_release_path(root, path);
6818 key.offset += num_bytes;
6819 } else {
6820 BUG_ON(1);
6821 #if 0
6822 u64 alloc_hint;
6823 u64 extent_len;
6824 int i;
6825 /*
6826 * drop old extent pointer at first, then insert the
6827 * new pointers one bye one
6828 */
6829 btrfs_release_path(root, path);
6830 ret = btrfs_drop_extents(trans, root, inode, key.offset,
6831 key.offset + num_bytes,
6832 key.offset, &alloc_hint);
6833 BUG_ON(ret);
6834
6835 for (i = 0; i < nr_extents; i++) {
6836 if (ext_offset >= new_extents[i].num_bytes) {
6837 ext_offset -= new_extents[i].num_bytes;
6838 continue;
6839 }
6840 extent_len = min(new_extents[i].num_bytes -
6841 ext_offset, num_bytes);
6842
6843 ret = btrfs_insert_empty_item(trans, root,
6844 path, &key,
6845 sizeof(*fi));
6846 BUG_ON(ret);
6847
6848 leaf = path->nodes[0];
6849 fi = btrfs_item_ptr(leaf, path->slots[0],
6850 struct btrfs_file_extent_item);
6851 btrfs_set_file_extent_generation(leaf, fi,
6852 trans->transid);
6853 btrfs_set_file_extent_type(leaf, fi,
6854 BTRFS_FILE_EXTENT_REG);
6855 btrfs_set_file_extent_disk_bytenr(leaf, fi,
6856 new_extents[i].disk_bytenr);
6857 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
6858 new_extents[i].disk_num_bytes);
6859 btrfs_set_file_extent_ram_bytes(leaf, fi,
6860 new_extents[i].ram_bytes);
6861
6862 btrfs_set_file_extent_compression(leaf, fi,
6863 new_extents[i].compression);
6864 btrfs_set_file_extent_encryption(leaf, fi,
6865 new_extents[i].encryption);
6866 btrfs_set_file_extent_other_encoding(leaf, fi,
6867 new_extents[i].other_encoding);
6868
6869 btrfs_set_file_extent_num_bytes(leaf, fi,
6870 extent_len);
6871 ext_offset += new_extents[i].offset;
6872 btrfs_set_file_extent_offset(leaf, fi,
6873 ext_offset);
6874 btrfs_mark_buffer_dirty(leaf);
6875
6876 btrfs_drop_extent_cache(inode, key.offset,
6877 key.offset + extent_len - 1, 0);
6878
6879 ret = btrfs_inc_extent_ref(trans, root,
6880 new_extents[i].disk_bytenr,
6881 new_extents[i].disk_num_bytes,
6882 leaf->start,
6883 root->root_key.objectid,
6884 trans->transid, key.objectid);
6885 BUG_ON(ret);
6886 btrfs_release_path(root, path);
6887
6888 inode_add_bytes(inode, extent_len);
6889
6890 ext_offset = 0;
6891 num_bytes -= extent_len;
6892 key.offset += extent_len;
6893
6894 if (num_bytes == 0)
6895 break;
6896 }
6897 BUG_ON(i >= nr_extents);
6898 #endif
6899 }
6900
6901 if (extent_locked) {
6902 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6903 lock_end, GFP_NOFS);
6904 extent_locked = 0;
6905 }
6906 skip:
6907 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
6908 key.offset >= search_end)
6909 break;
6910
6911 cond_resched();
6912 }
6913 ret = 0;
6914 out:
6915 btrfs_release_path(root, path);
6916 if (inode) {
6917 mutex_unlock(&inode->i_mutex);
6918 if (extent_locked) {
6919 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
6920 lock_end, GFP_NOFS);
6921 }
6922 iput(inode);
6923 }
6924 return ret;
6925 }
6926
6927 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
6928 struct btrfs_root *root,
6929 struct extent_buffer *buf, u64 orig_start)
6930 {
6931 int level;
6932 int ret;
6933
6934 BUG_ON(btrfs_header_generation(buf) != trans->transid);
6935 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6936
6937 level = btrfs_header_level(buf);
6938 if (level == 0) {
6939 struct btrfs_leaf_ref *ref;
6940 struct btrfs_leaf_ref *orig_ref;
6941
6942 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
6943 if (!orig_ref)
6944 return -ENOENT;
6945
6946 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
6947 if (!ref) {
6948 btrfs_free_leaf_ref(root, orig_ref);
6949 return -ENOMEM;
6950 }
6951
6952 ref->nritems = orig_ref->nritems;
6953 memcpy(ref->extents, orig_ref->extents,
6954 sizeof(ref->extents[0]) * ref->nritems);
6955
6956 btrfs_free_leaf_ref(root, orig_ref);
6957
6958 ref->root_gen = trans->transid;
6959 ref->bytenr = buf->start;
6960 ref->owner = btrfs_header_owner(buf);
6961 ref->generation = btrfs_header_generation(buf);
6962
6963 ret = btrfs_add_leaf_ref(root, ref, 0);
6964 WARN_ON(ret);
6965 btrfs_free_leaf_ref(root, ref);
6966 }
6967 return 0;
6968 }
6969
6970 static noinline int invalidate_extent_cache(struct btrfs_root *root,
6971 struct extent_buffer *leaf,
6972 struct btrfs_block_group_cache *group,
6973 struct btrfs_root *target_root)
6974 {
6975 struct btrfs_key key;
6976 struct inode *inode = NULL;
6977 struct btrfs_file_extent_item *fi;
6978 struct extent_state *cached_state = NULL;
6979 u64 num_bytes;
6980 u64 skip_objectid = 0;
6981 u32 nritems;
6982 u32 i;
6983
6984 nritems = btrfs_header_nritems(leaf);
6985 for (i = 0; i < nritems; i++) {
6986 btrfs_item_key_to_cpu(leaf, &key, i);
6987 if (key.objectid == skip_objectid ||
6988 key.type != BTRFS_EXTENT_DATA_KEY)
6989 continue;
6990 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
6991 if (btrfs_file_extent_type(leaf, fi) ==
6992 BTRFS_FILE_EXTENT_INLINE)
6993 continue;
6994 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
6995 continue;
6996 if (!inode || inode->i_ino != key.objectid) {
6997 iput(inode);
6998 inode = btrfs_ilookup(target_root->fs_info->sb,
6999 key.objectid, target_root, 1);
7000 }
7001 if (!inode) {
7002 skip_objectid = key.objectid;
7003 continue;
7004 }
7005 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7006
7007 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7008 key.offset + num_bytes - 1, 0, &cached_state,
7009 GFP_NOFS);
7010 btrfs_drop_extent_cache(inode, key.offset,
7011 key.offset + num_bytes - 1, 1);
7012 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7013 key.offset + num_bytes - 1, &cached_state,
7014 GFP_NOFS);
7015 cond_resched();
7016 }
7017 iput(inode);
7018 return 0;
7019 }
7020
7021 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
7022 struct btrfs_root *root,
7023 struct extent_buffer *leaf,
7024 struct btrfs_block_group_cache *group,
7025 struct inode *reloc_inode)
7026 {
7027 struct btrfs_key key;
7028 struct btrfs_key extent_key;
7029 struct btrfs_file_extent_item *fi;
7030 struct btrfs_leaf_ref *ref;
7031 struct disk_extent *new_extent;
7032 u64 bytenr;
7033 u64 num_bytes;
7034 u32 nritems;
7035 u32 i;
7036 int ext_index;
7037 int nr_extent;
7038 int ret;
7039
7040 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7041 BUG_ON(!new_extent);
7042
7043 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7044 BUG_ON(!ref);
7045
7046 ext_index = -1;
7047 nritems = btrfs_header_nritems(leaf);
7048 for (i = 0; i < nritems; i++) {
7049 btrfs_item_key_to_cpu(leaf, &key, i);
7050 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7051 continue;
7052 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7053 if (btrfs_file_extent_type(leaf, fi) ==
7054 BTRFS_FILE_EXTENT_INLINE)
7055 continue;
7056 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7057 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7058 if (bytenr == 0)
7059 continue;
7060
7061 ext_index++;
7062 if (bytenr >= group->key.objectid + group->key.offset ||
7063 bytenr + num_bytes <= group->key.objectid)
7064 continue;
7065
7066 extent_key.objectid = bytenr;
7067 extent_key.offset = num_bytes;
7068 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7069 nr_extent = 1;
7070 ret = get_new_locations(reloc_inode, &extent_key,
7071 group->key.objectid, 1,
7072 &new_extent, &nr_extent);
7073 if (ret > 0)
7074 continue;
7075 BUG_ON(ret < 0);
7076
7077 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7078 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7079 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7080 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7081
7082 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7083 new_extent->disk_bytenr);
7084 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7085 new_extent->disk_num_bytes);
7086 btrfs_mark_buffer_dirty(leaf);
7087
7088 ret = btrfs_inc_extent_ref(trans, root,
7089 new_extent->disk_bytenr,
7090 new_extent->disk_num_bytes,
7091 leaf->start,
7092 root->root_key.objectid,
7093 trans->transid, key.objectid);
7094 BUG_ON(ret);
7095
7096 ret = btrfs_free_extent(trans, root,
7097 bytenr, num_bytes, leaf->start,
7098 btrfs_header_owner(leaf),
7099 btrfs_header_generation(leaf),
7100 key.objectid, 0);
7101 BUG_ON(ret);
7102 cond_resched();
7103 }
7104 kfree(new_extent);
7105 BUG_ON(ext_index + 1 != ref->nritems);
7106 btrfs_free_leaf_ref(root, ref);
7107 return 0;
7108 }
7109
7110 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7111 struct btrfs_root *root)
7112 {
7113 struct btrfs_root *reloc_root;
7114 int ret;
7115
7116 if (root->reloc_root) {
7117 reloc_root = root->reloc_root;
7118 root->reloc_root = NULL;
7119 list_add(&reloc_root->dead_list,
7120 &root->fs_info->dead_reloc_roots);
7121
7122 btrfs_set_root_bytenr(&reloc_root->root_item,
7123 reloc_root->node->start);
7124 btrfs_set_root_level(&root->root_item,
7125 btrfs_header_level(reloc_root->node));
7126 memset(&reloc_root->root_item.drop_progress, 0,
7127 sizeof(struct btrfs_disk_key));
7128 reloc_root->root_item.drop_level = 0;
7129
7130 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7131 &reloc_root->root_key,
7132 &reloc_root->root_item);
7133 BUG_ON(ret);
7134 }
7135 return 0;
7136 }
7137
7138 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7139 {
7140 struct btrfs_trans_handle *trans;
7141 struct btrfs_root *reloc_root;
7142 struct btrfs_root *prev_root = NULL;
7143 struct list_head dead_roots;
7144 int ret;
7145 unsigned long nr;
7146
7147 INIT_LIST_HEAD(&dead_roots);
7148 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7149
7150 while (!list_empty(&dead_roots)) {
7151 reloc_root = list_entry(dead_roots.prev,
7152 struct btrfs_root, dead_list);
7153 list_del_init(&reloc_root->dead_list);
7154
7155 BUG_ON(reloc_root->commit_root != NULL);
7156 while (1) {
7157 trans = btrfs_join_transaction(root, 1);
7158 BUG_ON(!trans);
7159
7160 mutex_lock(&root->fs_info->drop_mutex);
7161 ret = btrfs_drop_snapshot(trans, reloc_root);
7162 if (ret != -EAGAIN)
7163 break;
7164 mutex_unlock(&root->fs_info->drop_mutex);
7165
7166 nr = trans->blocks_used;
7167 ret = btrfs_end_transaction(trans, root);
7168 BUG_ON(ret);
7169 btrfs_btree_balance_dirty(root, nr);
7170 }
7171
7172 free_extent_buffer(reloc_root->node);
7173
7174 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7175 &reloc_root->root_key);
7176 BUG_ON(ret);
7177 mutex_unlock(&root->fs_info->drop_mutex);
7178
7179 nr = trans->blocks_used;
7180 ret = btrfs_end_transaction(trans, root);
7181 BUG_ON(ret);
7182 btrfs_btree_balance_dirty(root, nr);
7183
7184 kfree(prev_root);
7185 prev_root = reloc_root;
7186 }
7187 if (prev_root) {
7188 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7189 kfree(prev_root);
7190 }
7191 return 0;
7192 }
7193
7194 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7195 {
7196 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7197 return 0;
7198 }
7199
7200 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7201 {
7202 struct btrfs_root *reloc_root;
7203 struct btrfs_trans_handle *trans;
7204 struct btrfs_key location;
7205 int found;
7206 int ret;
7207
7208 mutex_lock(&root->fs_info->tree_reloc_mutex);
7209 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7210 BUG_ON(ret);
7211 found = !list_empty(&root->fs_info->dead_reloc_roots);
7212 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7213
7214 if (found) {
7215 trans = btrfs_start_transaction(root, 1);
7216 BUG_ON(!trans);
7217 ret = btrfs_commit_transaction(trans, root);
7218 BUG_ON(ret);
7219 }
7220
7221 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7222 location.offset = (u64)-1;
7223 location.type = BTRFS_ROOT_ITEM_KEY;
7224
7225 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7226 BUG_ON(!reloc_root);
7227 btrfs_orphan_cleanup(reloc_root);
7228 return 0;
7229 }
7230
7231 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
7232 struct btrfs_root *root)
7233 {
7234 struct btrfs_root *reloc_root;
7235 struct extent_buffer *eb;
7236 struct btrfs_root_item *root_item;
7237 struct btrfs_key root_key;
7238 int ret;
7239
7240 BUG_ON(!root->ref_cows);
7241 if (root->reloc_root)
7242 return 0;
7243
7244 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7245 BUG_ON(!root_item);
7246
7247 ret = btrfs_copy_root(trans, root, root->commit_root,
7248 &eb, BTRFS_TREE_RELOC_OBJECTID);
7249 BUG_ON(ret);
7250
7251 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7252 root_key.offset = root->root_key.objectid;
7253 root_key.type = BTRFS_ROOT_ITEM_KEY;
7254
7255 memcpy(root_item, &root->root_item, sizeof(root_item));
7256 btrfs_set_root_refs(root_item, 0);
7257 btrfs_set_root_bytenr(root_item, eb->start);
7258 btrfs_set_root_level(root_item, btrfs_header_level(eb));
7259 btrfs_set_root_generation(root_item, trans->transid);
7260
7261 btrfs_tree_unlock(eb);
7262 free_extent_buffer(eb);
7263
7264 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7265 &root_key, root_item);
7266 BUG_ON(ret);
7267 kfree(root_item);
7268
7269 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7270 &root_key);
7271 BUG_ON(!reloc_root);
7272 reloc_root->last_trans = trans->transid;
7273 reloc_root->commit_root = NULL;
7274 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7275
7276 root->reloc_root = reloc_root;
7277 return 0;
7278 }
7279
7280 /*
7281 * Core function of space balance.
7282 *
7283 * The idea is using reloc trees to relocate tree blocks in reference
7284 * counted roots. There is one reloc tree for each subvol, and all
7285 * reloc trees share same root key objectid. Reloc trees are snapshots
7286 * of the latest committed roots of subvols (root->commit_root).
7287 *
7288 * To relocate a tree block referenced by a subvol, there are two steps.
7289 * COW the block through subvol's reloc tree, then update block pointer
7290 * in the subvol to point to the new block. Since all reloc trees share
7291 * same root key objectid, doing special handing for tree blocks owned
7292 * by them is easy. Once a tree block has been COWed in one reloc tree,
7293 * we can use the resulting new block directly when the same block is
7294 * required to COW again through other reloc trees. By this way, relocated
7295 * tree blocks are shared between reloc trees, so they are also shared
7296 * between subvols.
7297 */
7298 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
7299 struct btrfs_root *root,
7300 struct btrfs_path *path,
7301 struct btrfs_key *first_key,
7302 struct btrfs_ref_path *ref_path,
7303 struct btrfs_block_group_cache *group,
7304 struct inode *reloc_inode)
7305 {
7306 struct btrfs_root *reloc_root;
7307 struct extent_buffer *eb = NULL;
7308 struct btrfs_key *keys;
7309 u64 *nodes;
7310 int level;
7311 int shared_level;
7312 int lowest_level = 0;
7313 int ret;
7314
7315 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7316 lowest_level = ref_path->owner_objectid;
7317
7318 if (!root->ref_cows) {
7319 path->lowest_level = lowest_level;
7320 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7321 BUG_ON(ret < 0);
7322 path->lowest_level = 0;
7323 btrfs_release_path(root, path);
7324 return 0;
7325 }
7326
7327 mutex_lock(&root->fs_info->tree_reloc_mutex);
7328 ret = init_reloc_tree(trans, root);
7329 BUG_ON(ret);
7330 reloc_root = root->reloc_root;
7331
7332 shared_level = ref_path->shared_level;
7333 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
7334
7335 keys = ref_path->node_keys;
7336 nodes = ref_path->new_nodes;
7337 memset(&keys[shared_level + 1], 0,
7338 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7339 memset(&nodes[shared_level + 1], 0,
7340 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
7341
7342 if (nodes[lowest_level] == 0) {
7343 path->lowest_level = lowest_level;
7344 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7345 0, 1);
7346 BUG_ON(ret);
7347 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7348 eb = path->nodes[level];
7349 if (!eb || eb == reloc_root->node)
7350 break;
7351 nodes[level] = eb->start;
7352 if (level == 0)
7353 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7354 else
7355 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7356 }
7357 if (nodes[0] &&
7358 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7359 eb = path->nodes[0];
7360 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7361 group, reloc_inode);
7362 BUG_ON(ret);
7363 }
7364 btrfs_release_path(reloc_root, path);
7365 } else {
7366 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
7367 lowest_level);
7368 BUG_ON(ret);
7369 }
7370
7371 /*
7372 * replace tree blocks in the fs tree with tree blocks in
7373 * the reloc tree.
7374 */
7375 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7376 BUG_ON(ret < 0);
7377
7378 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7379 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7380 0, 0);
7381 BUG_ON(ret);
7382 extent_buffer_get(path->nodes[0]);
7383 eb = path->nodes[0];
7384 btrfs_release_path(reloc_root, path);
7385 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7386 BUG_ON(ret);
7387 free_extent_buffer(eb);
7388 }
7389
7390 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7391 path->lowest_level = 0;
7392 return 0;
7393 }
7394
7395 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
7396 struct btrfs_root *root,
7397 struct btrfs_path *path,
7398 struct btrfs_key *first_key,
7399 struct btrfs_ref_path *ref_path)
7400 {
7401 int ret;
7402
7403 ret = relocate_one_path(trans, root, path, first_key,
7404 ref_path, NULL, NULL);
7405 BUG_ON(ret);
7406
7407 return 0;
7408 }
7409
7410 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7411 struct btrfs_root *extent_root,
7412 struct btrfs_path *path,
7413 struct btrfs_key *extent_key)
7414 {
7415 int ret;
7416
7417 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7418 if (ret)
7419 goto out;
7420 ret = btrfs_del_item(trans, extent_root, path);
7421 out:
7422 btrfs_release_path(extent_root, path);
7423 return ret;
7424 }
7425
7426 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7427 struct btrfs_ref_path *ref_path)
7428 {
7429 struct btrfs_key root_key;
7430
7431 root_key.objectid = ref_path->root_objectid;
7432 root_key.type = BTRFS_ROOT_ITEM_KEY;
7433 if (is_cowonly_root(ref_path->root_objectid))
7434 root_key.offset = 0;
7435 else
7436 root_key.offset = (u64)-1;
7437
7438 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7439 }
7440
7441 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7442 struct btrfs_path *path,
7443 struct btrfs_key *extent_key,
7444 struct btrfs_block_group_cache *group,
7445 struct inode *reloc_inode, int pass)
7446 {
7447 struct btrfs_trans_handle *trans;
7448 struct btrfs_root *found_root;
7449 struct btrfs_ref_path *ref_path = NULL;
7450 struct disk_extent *new_extents = NULL;
7451 int nr_extents = 0;
7452 int loops;
7453 int ret;
7454 int level;
7455 struct btrfs_key first_key;
7456 u64 prev_block = 0;
7457
7458
7459 trans = btrfs_start_transaction(extent_root, 1);
7460 BUG_ON(!trans);
7461
7462 if (extent_key->objectid == 0) {
7463 ret = del_extent_zero(trans, extent_root, path, extent_key);
7464 goto out;
7465 }
7466
7467 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7468 if (!ref_path) {
7469 ret = -ENOMEM;
7470 goto out;
7471 }
7472
7473 for (loops = 0; ; loops++) {
7474 if (loops == 0) {
7475 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7476 extent_key->objectid);
7477 } else {
7478 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7479 }
7480 if (ret < 0)
7481 goto out;
7482 if (ret > 0)
7483 break;
7484
7485 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7486 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7487 continue;
7488
7489 found_root = read_ref_root(extent_root->fs_info, ref_path);
7490 BUG_ON(!found_root);
7491 /*
7492 * for reference counted tree, only process reference paths
7493 * rooted at the latest committed root.
7494 */
7495 if (found_root->ref_cows &&
7496 ref_path->root_generation != found_root->root_key.offset)
7497 continue;
7498
7499 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7500 if (pass == 0) {
7501 /*
7502 * copy data extents to new locations
7503 */
7504 u64 group_start = group->key.objectid;
7505 ret = relocate_data_extent(reloc_inode,
7506 extent_key,
7507 group_start);
7508 if (ret < 0)
7509 goto out;
7510 break;
7511 }
7512 level = 0;
7513 } else {
7514 level = ref_path->owner_objectid;
7515 }
7516
7517 if (prev_block != ref_path->nodes[level]) {
7518 struct extent_buffer *eb;
7519 u64 block_start = ref_path->nodes[level];
7520 u64 block_size = btrfs_level_size(found_root, level);
7521
7522 eb = read_tree_block(found_root, block_start,
7523 block_size, 0);
7524 btrfs_tree_lock(eb);
7525 BUG_ON(level != btrfs_header_level(eb));
7526
7527 if (level == 0)
7528 btrfs_item_key_to_cpu(eb, &first_key, 0);
7529 else
7530 btrfs_node_key_to_cpu(eb, &first_key, 0);
7531
7532 btrfs_tree_unlock(eb);
7533 free_extent_buffer(eb);
7534 prev_block = block_start;
7535 }
7536
7537 mutex_lock(&extent_root->fs_info->trans_mutex);
7538 btrfs_record_root_in_trans(found_root);
7539 mutex_unlock(&extent_root->fs_info->trans_mutex);
7540 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7541 /*
7542 * try to update data extent references while
7543 * keeping metadata shared between snapshots.
7544 */
7545 if (pass == 1) {
7546 ret = relocate_one_path(trans, found_root,
7547 path, &first_key, ref_path,
7548 group, reloc_inode);
7549 if (ret < 0)
7550 goto out;
7551 continue;
7552 }
7553 /*
7554 * use fallback method to process the remaining
7555 * references.
7556 */
7557 if (!new_extents) {
7558 u64 group_start = group->key.objectid;
7559 new_extents = kmalloc(sizeof(*new_extents),
7560 GFP_NOFS);
7561 nr_extents = 1;
7562 ret = get_new_locations(reloc_inode,
7563 extent_key,
7564 group_start, 1,
7565 &new_extents,
7566 &nr_extents);
7567 if (ret)
7568 goto out;
7569 }
7570 ret = replace_one_extent(trans, found_root,
7571 path, extent_key,
7572 &first_key, ref_path,
7573 new_extents, nr_extents);
7574 } else {
7575 ret = relocate_tree_block(trans, found_root, path,
7576 &first_key, ref_path);
7577 }
7578 if (ret < 0)
7579 goto out;
7580 }
7581 ret = 0;
7582 out:
7583 btrfs_end_transaction(trans, extent_root);
7584 kfree(new_extents);
7585 kfree(ref_path);
7586 return ret;
7587 }
7588 #endif
7589
7590 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
7591 {
7592 u64 num_devices;
7593 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
7594 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
7595
7596 num_devices = root->fs_info->fs_devices->rw_devices;
7597 if (num_devices == 1) {
7598 stripped |= BTRFS_BLOCK_GROUP_DUP;
7599 stripped = flags & ~stripped;
7600
7601 /* turn raid0 into single device chunks */
7602 if (flags & BTRFS_BLOCK_GROUP_RAID0)
7603 return stripped;
7604
7605 /* turn mirroring into duplication */
7606 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
7607 BTRFS_BLOCK_GROUP_RAID10))
7608 return stripped | BTRFS_BLOCK_GROUP_DUP;
7609 return flags;
7610 } else {
7611 /* they already had raid on here, just return */
7612 if (flags & stripped)
7613 return flags;
7614
7615 stripped |= BTRFS_BLOCK_GROUP_DUP;
7616 stripped = flags & ~stripped;
7617
7618 /* switch duplicated blocks with raid1 */
7619 if (flags & BTRFS_BLOCK_GROUP_DUP)
7620 return stripped | BTRFS_BLOCK_GROUP_RAID1;
7621
7622 /* turn single device chunks into raid0 */
7623 return stripped | BTRFS_BLOCK_GROUP_RAID0;
7624 }
7625 return flags;
7626 }
7627
7628 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
7629 {
7630 struct btrfs_space_info *sinfo = cache->space_info;
7631 u64 num_bytes;
7632 int ret = -ENOSPC;
7633
7634 if (cache->ro)
7635 return 0;
7636
7637 spin_lock(&sinfo->lock);
7638 spin_lock(&cache->lock);
7639 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7640 cache->bytes_super - btrfs_block_group_used(&cache->item);
7641
7642 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
7643 sinfo->bytes_may_use + sinfo->bytes_readonly +
7644 cache->reserved_pinned + num_bytes < sinfo->total_bytes) {
7645 sinfo->bytes_readonly += num_bytes;
7646 sinfo->bytes_reserved += cache->reserved_pinned;
7647 cache->reserved_pinned = 0;
7648 cache->ro = 1;
7649 ret = 0;
7650 }
7651 spin_unlock(&cache->lock);
7652 spin_unlock(&sinfo->lock);
7653 return ret;
7654 }
7655
7656 int btrfs_set_block_group_ro(struct btrfs_root *root,
7657 struct btrfs_block_group_cache *cache)
7658
7659 {
7660 struct btrfs_trans_handle *trans;
7661 u64 alloc_flags;
7662 int ret;
7663
7664 BUG_ON(cache->ro);
7665
7666 trans = btrfs_join_transaction(root, 1);
7667 BUG_ON(IS_ERR(trans));
7668
7669 alloc_flags = update_block_group_flags(root, cache->flags);
7670 if (alloc_flags != cache->flags)
7671 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7672
7673 ret = set_block_group_ro(cache);
7674 if (!ret)
7675 goto out;
7676 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
7677 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags, 1);
7678 if (ret < 0)
7679 goto out;
7680 ret = set_block_group_ro(cache);
7681 out:
7682 btrfs_end_transaction(trans, root);
7683 return ret;
7684 }
7685
7686 int btrfs_set_block_group_rw(struct btrfs_root *root,
7687 struct btrfs_block_group_cache *cache)
7688 {
7689 struct btrfs_space_info *sinfo = cache->space_info;
7690 u64 num_bytes;
7691
7692 BUG_ON(!cache->ro);
7693
7694 spin_lock(&sinfo->lock);
7695 spin_lock(&cache->lock);
7696 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
7697 cache->bytes_super - btrfs_block_group_used(&cache->item);
7698 sinfo->bytes_readonly -= num_bytes;
7699 cache->ro = 0;
7700 spin_unlock(&cache->lock);
7701 spin_unlock(&sinfo->lock);
7702 return 0;
7703 }
7704
7705 /*
7706 * checks to see if its even possible to relocate this block group.
7707 *
7708 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
7709 * ok to go ahead and try.
7710 */
7711 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
7712 {
7713 struct btrfs_block_group_cache *block_group;
7714 struct btrfs_space_info *space_info;
7715 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
7716 struct btrfs_device *device;
7717 int full = 0;
7718 int ret = 0;
7719
7720 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
7721
7722 /* odd, couldn't find the block group, leave it alone */
7723 if (!block_group)
7724 return -1;
7725
7726 /* no bytes used, we're good */
7727 if (!btrfs_block_group_used(&block_group->item))
7728 goto out;
7729
7730 space_info = block_group->space_info;
7731 spin_lock(&space_info->lock);
7732
7733 full = space_info->full;
7734
7735 /*
7736 * if this is the last block group we have in this space, we can't
7737 * relocate it unless we're able to allocate a new chunk below.
7738 *
7739 * Otherwise, we need to make sure we have room in the space to handle
7740 * all of the extents from this block group. If we can, we're good
7741 */
7742 if ((space_info->total_bytes != block_group->key.offset) &&
7743 (space_info->bytes_used + space_info->bytes_reserved +
7744 space_info->bytes_pinned + space_info->bytes_readonly +
7745 btrfs_block_group_used(&block_group->item) <
7746 space_info->total_bytes)) {
7747 spin_unlock(&space_info->lock);
7748 goto out;
7749 }
7750 spin_unlock(&space_info->lock);
7751
7752 /*
7753 * ok we don't have enough space, but maybe we have free space on our
7754 * devices to allocate new chunks for relocation, so loop through our
7755 * alloc devices and guess if we have enough space. However, if we
7756 * were marked as full, then we know there aren't enough chunks, and we
7757 * can just return.
7758 */
7759 ret = -1;
7760 if (full)
7761 goto out;
7762
7763 mutex_lock(&root->fs_info->chunk_mutex);
7764 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
7765 u64 min_free = btrfs_block_group_used(&block_group->item);
7766 u64 dev_offset, max_avail;
7767
7768 /*
7769 * check to make sure we can actually find a chunk with enough
7770 * space to fit our block group in.
7771 */
7772 if (device->total_bytes > device->bytes_used + min_free) {
7773 ret = find_free_dev_extent(NULL, device, min_free,
7774 &dev_offset, &max_avail);
7775 if (!ret)
7776 break;
7777 ret = -1;
7778 }
7779 }
7780 mutex_unlock(&root->fs_info->chunk_mutex);
7781 out:
7782 btrfs_put_block_group(block_group);
7783 return ret;
7784 }
7785
7786 static int find_first_block_group(struct btrfs_root *root,
7787 struct btrfs_path *path, struct btrfs_key *key)
7788 {
7789 int ret = 0;
7790 struct btrfs_key found_key;
7791 struct extent_buffer *leaf;
7792 int slot;
7793
7794 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
7795 if (ret < 0)
7796 goto out;
7797
7798 while (1) {
7799 slot = path->slots[0];
7800 leaf = path->nodes[0];
7801 if (slot >= btrfs_header_nritems(leaf)) {
7802 ret = btrfs_next_leaf(root, path);
7803 if (ret == 0)
7804 continue;
7805 if (ret < 0)
7806 goto out;
7807 break;
7808 }
7809 btrfs_item_key_to_cpu(leaf, &found_key, slot);
7810
7811 if (found_key.objectid >= key->objectid &&
7812 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
7813 ret = 0;
7814 goto out;
7815 }
7816 path->slots[0]++;
7817 }
7818 out:
7819 return ret;
7820 }
7821
7822 int btrfs_free_block_groups(struct btrfs_fs_info *info)
7823 {
7824 struct btrfs_block_group_cache *block_group;
7825 struct btrfs_space_info *space_info;
7826 struct btrfs_caching_control *caching_ctl;
7827 struct rb_node *n;
7828
7829 down_write(&info->extent_commit_sem);
7830 while (!list_empty(&info->caching_block_groups)) {
7831 caching_ctl = list_entry(info->caching_block_groups.next,
7832 struct btrfs_caching_control, list);
7833 list_del(&caching_ctl->list);
7834 put_caching_control(caching_ctl);
7835 }
7836 up_write(&info->extent_commit_sem);
7837
7838 spin_lock(&info->block_group_cache_lock);
7839 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
7840 block_group = rb_entry(n, struct btrfs_block_group_cache,
7841 cache_node);
7842 rb_erase(&block_group->cache_node,
7843 &info->block_group_cache_tree);
7844 spin_unlock(&info->block_group_cache_lock);
7845
7846 down_write(&block_group->space_info->groups_sem);
7847 list_del(&block_group->list);
7848 up_write(&block_group->space_info->groups_sem);
7849
7850 if (block_group->cached == BTRFS_CACHE_STARTED)
7851 wait_block_group_cache_done(block_group);
7852
7853 btrfs_remove_free_space_cache(block_group);
7854 btrfs_put_block_group(block_group);
7855
7856 spin_lock(&info->block_group_cache_lock);
7857 }
7858 spin_unlock(&info->block_group_cache_lock);
7859
7860 /* now that all the block groups are freed, go through and
7861 * free all the space_info structs. This is only called during
7862 * the final stages of unmount, and so we know nobody is
7863 * using them. We call synchronize_rcu() once before we start,
7864 * just to be on the safe side.
7865 */
7866 synchronize_rcu();
7867
7868 release_global_block_rsv(info);
7869
7870 while(!list_empty(&info->space_info)) {
7871 space_info = list_entry(info->space_info.next,
7872 struct btrfs_space_info,
7873 list);
7874 if (space_info->bytes_pinned > 0 ||
7875 space_info->bytes_reserved > 0) {
7876 WARN_ON(1);
7877 dump_space_info(space_info, 0, 0);
7878 }
7879 list_del(&space_info->list);
7880 kfree(space_info);
7881 }
7882 return 0;
7883 }
7884
7885 static void __link_block_group(struct btrfs_space_info *space_info,
7886 struct btrfs_block_group_cache *cache)
7887 {
7888 int index = get_block_group_index(cache);
7889
7890 down_write(&space_info->groups_sem);
7891 list_add_tail(&cache->list, &space_info->block_groups[index]);
7892 up_write(&space_info->groups_sem);
7893 }
7894
7895 int btrfs_read_block_groups(struct btrfs_root *root)
7896 {
7897 struct btrfs_path *path;
7898 int ret;
7899 struct btrfs_block_group_cache *cache;
7900 struct btrfs_fs_info *info = root->fs_info;
7901 struct btrfs_space_info *space_info;
7902 struct btrfs_key key;
7903 struct btrfs_key found_key;
7904 struct extent_buffer *leaf;
7905
7906 root = info->extent_root;
7907 key.objectid = 0;
7908 key.offset = 0;
7909 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
7910 path = btrfs_alloc_path();
7911 if (!path)
7912 return -ENOMEM;
7913
7914 while (1) {
7915 ret = find_first_block_group(root, path, &key);
7916 if (ret > 0)
7917 break;
7918 if (ret != 0)
7919 goto error;
7920
7921 leaf = path->nodes[0];
7922 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7923 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7924 if (!cache) {
7925 ret = -ENOMEM;
7926 goto error;
7927 }
7928
7929 atomic_set(&cache->count, 1);
7930 spin_lock_init(&cache->lock);
7931 spin_lock_init(&cache->tree_lock);
7932 cache->fs_info = info;
7933 INIT_LIST_HEAD(&cache->list);
7934 INIT_LIST_HEAD(&cache->cluster_list);
7935
7936 /*
7937 * we only want to have 32k of ram per block group for keeping
7938 * track of free space, and if we pass 1/2 of that we want to
7939 * start converting things over to using bitmaps
7940 */
7941 cache->extents_thresh = ((1024 * 32) / 2) /
7942 sizeof(struct btrfs_free_space);
7943
7944 read_extent_buffer(leaf, &cache->item,
7945 btrfs_item_ptr_offset(leaf, path->slots[0]),
7946 sizeof(cache->item));
7947 memcpy(&cache->key, &found_key, sizeof(found_key));
7948
7949 key.objectid = found_key.objectid + found_key.offset;
7950 btrfs_release_path(root, path);
7951 cache->flags = btrfs_block_group_flags(&cache->item);
7952 cache->sectorsize = root->sectorsize;
7953
7954 /*
7955 * check for two cases, either we are full, and therefore
7956 * don't need to bother with the caching work since we won't
7957 * find any space, or we are empty, and we can just add all
7958 * the space in and be done with it. This saves us _alot_ of
7959 * time, particularly in the full case.
7960 */
7961 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
7962 exclude_super_stripes(root, cache);
7963 cache->last_byte_to_unpin = (u64)-1;
7964 cache->cached = BTRFS_CACHE_FINISHED;
7965 free_excluded_extents(root, cache);
7966 } else if (btrfs_block_group_used(&cache->item) == 0) {
7967 exclude_super_stripes(root, cache);
7968 cache->last_byte_to_unpin = (u64)-1;
7969 cache->cached = BTRFS_CACHE_FINISHED;
7970 add_new_free_space(cache, root->fs_info,
7971 found_key.objectid,
7972 found_key.objectid +
7973 found_key.offset);
7974 free_excluded_extents(root, cache);
7975 }
7976
7977 ret = update_space_info(info, cache->flags, found_key.offset,
7978 btrfs_block_group_used(&cache->item),
7979 &space_info);
7980 BUG_ON(ret);
7981 cache->space_info = space_info;
7982 spin_lock(&cache->space_info->lock);
7983 cache->space_info->bytes_readonly += cache->bytes_super;
7984 spin_unlock(&cache->space_info->lock);
7985
7986 __link_block_group(space_info, cache);
7987
7988 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7989 BUG_ON(ret);
7990
7991 set_avail_alloc_bits(root->fs_info, cache->flags);
7992 if (btrfs_chunk_readonly(root, cache->key.objectid))
7993 set_block_group_ro(cache);
7994 }
7995
7996 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
7997 if (!(get_alloc_profile(root, space_info->flags) &
7998 (BTRFS_BLOCK_GROUP_RAID10 |
7999 BTRFS_BLOCK_GROUP_RAID1 |
8000 BTRFS_BLOCK_GROUP_DUP)))
8001 continue;
8002 /*
8003 * avoid allocating from un-mirrored block group if there are
8004 * mirrored block groups.
8005 */
8006 list_for_each_entry(cache, &space_info->block_groups[3], list)
8007 set_block_group_ro(cache);
8008 list_for_each_entry(cache, &space_info->block_groups[4], list)
8009 set_block_group_ro(cache);
8010 }
8011
8012 init_global_block_rsv(info);
8013 ret = 0;
8014 error:
8015 btrfs_free_path(path);
8016 return ret;
8017 }
8018
8019 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8020 struct btrfs_root *root, u64 bytes_used,
8021 u64 type, u64 chunk_objectid, u64 chunk_offset,
8022 u64 size)
8023 {
8024 int ret;
8025 struct btrfs_root *extent_root;
8026 struct btrfs_block_group_cache *cache;
8027
8028 extent_root = root->fs_info->extent_root;
8029
8030 root->fs_info->last_trans_log_full_commit = trans->transid;
8031
8032 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8033 if (!cache)
8034 return -ENOMEM;
8035
8036 cache->key.objectid = chunk_offset;
8037 cache->key.offset = size;
8038 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8039 cache->sectorsize = root->sectorsize;
8040
8041 /*
8042 * we only want to have 32k of ram per block group for keeping track
8043 * of free space, and if we pass 1/2 of that we want to start
8044 * converting things over to using bitmaps
8045 */
8046 cache->extents_thresh = ((1024 * 32) / 2) /
8047 sizeof(struct btrfs_free_space);
8048 atomic_set(&cache->count, 1);
8049 spin_lock_init(&cache->lock);
8050 spin_lock_init(&cache->tree_lock);
8051 INIT_LIST_HEAD(&cache->list);
8052 INIT_LIST_HEAD(&cache->cluster_list);
8053
8054 btrfs_set_block_group_used(&cache->item, bytes_used);
8055 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8056 cache->flags = type;
8057 btrfs_set_block_group_flags(&cache->item, type);
8058
8059 cache->last_byte_to_unpin = (u64)-1;
8060 cache->cached = BTRFS_CACHE_FINISHED;
8061 exclude_super_stripes(root, cache);
8062
8063 add_new_free_space(cache, root->fs_info, chunk_offset,
8064 chunk_offset + size);
8065
8066 free_excluded_extents(root, cache);
8067
8068 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8069 &cache->space_info);
8070 BUG_ON(ret);
8071
8072 spin_lock(&cache->space_info->lock);
8073 cache->space_info->bytes_readonly += cache->bytes_super;
8074 spin_unlock(&cache->space_info->lock);
8075
8076 __link_block_group(cache->space_info, cache);
8077
8078 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8079 BUG_ON(ret);
8080
8081 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8082 sizeof(cache->item));
8083 BUG_ON(ret);
8084
8085 set_avail_alloc_bits(extent_root->fs_info, type);
8086
8087 return 0;
8088 }
8089
8090 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8091 struct btrfs_root *root, u64 group_start)
8092 {
8093 struct btrfs_path *path;
8094 struct btrfs_block_group_cache *block_group;
8095 struct btrfs_free_cluster *cluster;
8096 struct btrfs_key key;
8097 int ret;
8098
8099 root = root->fs_info->extent_root;
8100
8101 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8102 BUG_ON(!block_group);
8103 BUG_ON(!block_group->ro);
8104
8105 memcpy(&key, &block_group->key, sizeof(key));
8106
8107 /* make sure this block group isn't part of an allocation cluster */
8108 cluster = &root->fs_info->data_alloc_cluster;
8109 spin_lock(&cluster->refill_lock);
8110 btrfs_return_cluster_to_free_space(block_group, cluster);
8111 spin_unlock(&cluster->refill_lock);
8112
8113 /*
8114 * make sure this block group isn't part of a metadata
8115 * allocation cluster
8116 */
8117 cluster = &root->fs_info->meta_alloc_cluster;
8118 spin_lock(&cluster->refill_lock);
8119 btrfs_return_cluster_to_free_space(block_group, cluster);
8120 spin_unlock(&cluster->refill_lock);
8121
8122 path = btrfs_alloc_path();
8123 BUG_ON(!path);
8124
8125 spin_lock(&root->fs_info->block_group_cache_lock);
8126 rb_erase(&block_group->cache_node,
8127 &root->fs_info->block_group_cache_tree);
8128 spin_unlock(&root->fs_info->block_group_cache_lock);
8129
8130 down_write(&block_group->space_info->groups_sem);
8131 /*
8132 * we must use list_del_init so people can check to see if they
8133 * are still on the list after taking the semaphore
8134 */
8135 list_del_init(&block_group->list);
8136 up_write(&block_group->space_info->groups_sem);
8137
8138 if (block_group->cached == BTRFS_CACHE_STARTED)
8139 wait_block_group_cache_done(block_group);
8140
8141 btrfs_remove_free_space_cache(block_group);
8142
8143 spin_lock(&block_group->space_info->lock);
8144 block_group->space_info->total_bytes -= block_group->key.offset;
8145 block_group->space_info->bytes_readonly -= block_group->key.offset;
8146 spin_unlock(&block_group->space_info->lock);
8147
8148 btrfs_clear_space_info_full(root->fs_info);
8149
8150 btrfs_put_block_group(block_group);
8151 btrfs_put_block_group(block_group);
8152
8153 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8154 if (ret > 0)
8155 ret = -EIO;
8156 if (ret < 0)
8157 goto out;
8158
8159 ret = btrfs_del_item(trans, root, path);
8160 out:
8161 btrfs_free_path(path);
8162 return ret;
8163 }