2 * Copyright (C) 2007 Oracle. All rights reserved.
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.
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.
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.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
26 #include "print-tree.h"
27 #include "transaction.h"
30 #include "ref-cache.h"
32 #define PENDING_EXTENT_INSERT 0
33 #define PENDING_EXTENT_DELETE 1
34 #define PENDING_BACKREF_UPDATE 2
36 struct pending_extent_op {
47 static int finish_current_insert(struct btrfs_trans_handle *trans, struct
48 btrfs_root *extent_root, int all);
49 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
50 btrfs_root *extent_root, int all);
51 static struct btrfs_block_group_cache *
52 __btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
56 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
58 return (cache->flags & bits) == bits;
62 * this adds the block group to the fs_info rb tree for the block group
65 int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
66 struct btrfs_block_group_cache *block_group)
69 struct rb_node *parent = NULL;
70 struct btrfs_block_group_cache *cache;
72 spin_lock(&info->block_group_cache_lock);
73 p = &info->block_group_cache_tree.rb_node;
77 cache = rb_entry(parent, struct btrfs_block_group_cache,
79 if (block_group->key.objectid < cache->key.objectid) {
81 } else if (block_group->key.objectid > cache->key.objectid) {
84 spin_unlock(&info->block_group_cache_lock);
89 rb_link_node(&block_group->cache_node, parent, p);
90 rb_insert_color(&block_group->cache_node,
91 &info->block_group_cache_tree);
92 spin_unlock(&info->block_group_cache_lock);
98 * This will return the block group at or after bytenr if contains is 0, else
99 * it will return the block group that contains the bytenr
101 static struct btrfs_block_group_cache *
102 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
105 struct btrfs_block_group_cache *cache, *ret = NULL;
109 spin_lock(&info->block_group_cache_lock);
110 n = info->block_group_cache_tree.rb_node;
113 cache = rb_entry(n, struct btrfs_block_group_cache,
115 end = cache->key.objectid + cache->key.offset - 1;
116 start = cache->key.objectid;
118 if (bytenr < start) {
119 if (!contains && (!ret || start < ret->key.objectid))
122 } else if (bytenr > start) {
123 if (contains && bytenr <= end) {
133 spin_unlock(&info->block_group_cache_lock);
139 * this is only called by cache_block_group, since we could have freed extents
140 * we need to check the pinned_extents for any extents that can't be used yet
141 * since their free space will be released as soon as the transaction commits.
143 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
144 struct btrfs_fs_info *info, u64 start, u64 end)
146 u64 extent_start, extent_end, size;
149 mutex_lock(&info->pinned_mutex);
150 while (start < end) {
151 ret = find_first_extent_bit(&info->pinned_extents, start,
152 &extent_start, &extent_end,
157 if (extent_start == start) {
158 start = extent_end + 1;
159 } else if (extent_start > start && extent_start < end) {
160 size = extent_start - start;
161 ret = btrfs_add_free_space_lock(block_group, start,
164 start = extent_end + 1;
172 ret = btrfs_add_free_space_lock(block_group, start, size);
175 mutex_unlock(&info->pinned_mutex);
180 static int cache_block_group(struct btrfs_root *root,
181 struct btrfs_block_group_cache *block_group)
183 struct btrfs_path *path;
185 struct btrfs_key key;
186 struct extent_buffer *leaf;
195 root = root->fs_info->extent_root;
197 if (block_group->cached)
200 path = btrfs_alloc_path();
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
210 path->skip_locking = 1;
211 first_free = max_t(u64, block_group->key.objectid,
212 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
213 key.objectid = block_group->key.objectid;
215 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
219 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
223 leaf = path->nodes[0];
224 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
225 if (key.objectid + key.offset > first_free)
226 first_free = key.objectid + key.offset;
229 leaf = path->nodes[0];
230 slot = path->slots[0];
231 if (slot >= btrfs_header_nritems(leaf)) {
232 ret = btrfs_next_leaf(root, path);
240 btrfs_item_key_to_cpu(leaf, &key, slot);
241 if (key.objectid < block_group->key.objectid)
244 if (key.objectid >= block_group->key.objectid +
245 block_group->key.offset)
248 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
254 add_new_free_space(block_group, root->fs_info, last,
257 last = key.objectid + key.offset;
266 add_new_free_space(block_group, root->fs_info, last,
267 block_group->key.objectid +
268 block_group->key.offset);
270 block_group->cached = 1;
273 btrfs_free_path(path);
278 * return the block group that starts at or after bytenr
280 struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
284 struct btrfs_block_group_cache *cache;
286 cache = block_group_cache_tree_search(info, bytenr, 0);
292 * return the block group that contains teh given bytenr
294 struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
298 struct btrfs_block_group_cache *cache;
300 cache = block_group_cache_tree_search(info, bytenr, 1);
305 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
308 struct list_head *head = &info->space_info;
309 struct list_head *cur;
310 struct btrfs_space_info *found;
311 list_for_each(cur, head) {
312 found = list_entry(cur, struct btrfs_space_info, list);
313 if (found->flags == flags)
319 static u64 div_factor(u64 num, int factor)
328 static struct btrfs_block_group_cache *
329 __btrfs_find_block_group(struct btrfs_root *root,
330 struct btrfs_block_group_cache *hint,
331 u64 search_start, int data, int owner)
333 struct btrfs_block_group_cache *cache;
334 struct btrfs_block_group_cache *found_group = NULL;
335 struct btrfs_fs_info *info = root->fs_info;
343 if (data & BTRFS_BLOCK_GROUP_METADATA)
347 struct btrfs_block_group_cache *shint;
348 shint = btrfs_lookup_first_block_group(info, search_start);
349 if (shint && block_group_bits(shint, data) && !shint->ro) {
350 spin_lock(&shint->lock);
351 used = btrfs_block_group_used(&shint->item);
352 if (used + shint->pinned + shint->reserved <
353 div_factor(shint->key.offset, factor)) {
354 spin_unlock(&shint->lock);
357 spin_unlock(&shint->lock);
360 if (hint && !hint->ro && block_group_bits(hint, data)) {
361 spin_lock(&hint->lock);
362 used = btrfs_block_group_used(&hint->item);
363 if (used + hint->pinned + hint->reserved <
364 div_factor(hint->key.offset, factor)) {
365 spin_unlock(&hint->lock);
368 spin_unlock(&hint->lock);
369 last = hint->key.objectid + hint->key.offset;
372 last = max(hint->key.objectid, search_start);
378 cache = btrfs_lookup_first_block_group(root->fs_info, last);
382 spin_lock(&cache->lock);
383 last = cache->key.objectid + cache->key.offset;
384 used = btrfs_block_group_used(&cache->item);
386 if (!cache->ro && block_group_bits(cache, data)) {
387 free_check = div_factor(cache->key.offset, factor);
388 if (used + cache->pinned + cache->reserved <
391 spin_unlock(&cache->lock);
395 spin_unlock(&cache->lock);
403 if (!full_search && factor < 10) {
413 struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
414 struct btrfs_block_group_cache
415 *hint, u64 search_start,
419 struct btrfs_block_group_cache *ret;
420 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
424 /* simple helper to search for an existing extent at a given offset */
425 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
428 struct btrfs_key key;
429 struct btrfs_path *path;
431 path = btrfs_alloc_path();
433 key.objectid = start;
435 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
436 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
438 btrfs_free_path(path);
443 * Back reference rules. Back refs have three main goals:
445 * 1) differentiate between all holders of references to an extent so that
446 * when a reference is dropped we can make sure it was a valid reference
447 * before freeing the extent.
449 * 2) Provide enough information to quickly find the holders of an extent
450 * if we notice a given block is corrupted or bad.
452 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
453 * maintenance. This is actually the same as #2, but with a slightly
454 * different use case.
456 * File extents can be referenced by:
458 * - multiple snapshots, subvolumes, or different generations in one subvol
459 * - different files inside a single subvolume
460 * - different offsets inside a file (bookend extents in file.c)
462 * The extent ref structure has fields for:
464 * - Objectid of the subvolume root
465 * - Generation number of the tree holding the reference
466 * - objectid of the file holding the reference
467 * - number of references holding by parent node (alway 1 for tree blocks)
469 * Btree leaf may hold multiple references to a file extent. In most cases,
470 * these references are from same file and the corresponding offsets inside
471 * the file are close together.
473 * When a file extent is allocated the fields are filled in:
474 * (root_key.objectid, trans->transid, inode objectid, 1)
476 * When a leaf is cow'd new references are added for every file extent found
477 * in the leaf. It looks similar to the create case, but trans->transid will
478 * be different when the block is cow'd.
480 * (root_key.objectid, trans->transid, inode objectid,
481 * number of references in the leaf)
483 * When a file extent is removed either during snapshot deletion or
484 * file truncation, we find the corresponding back reference and check
485 * the following fields:
487 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
490 * Btree extents can be referenced by:
492 * - Different subvolumes
493 * - Different generations of the same subvolume
495 * When a tree block is created, back references are inserted:
497 * (root->root_key.objectid, trans->transid, level, 1)
499 * When a tree block is cow'd, new back references are added for all the
500 * blocks it points to. If the tree block isn't in reference counted root,
501 * the old back references are removed. These new back references are of
502 * the form (trans->transid will have increased since creation):
504 * (root->root_key.objectid, trans->transid, level, 1)
506 * When a backref is in deleting, the following fields are checked:
508 * if backref was for a tree root:
509 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
511 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
513 * Back Reference Key composing:
515 * The key objectid corresponds to the first byte in the extent, the key
516 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
517 * byte of parent extent. If a extent is tree root, the key offset is set
518 * to the key objectid.
521 static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
523 struct btrfs_path *path,
524 u64 bytenr, u64 parent,
525 u64 ref_root, u64 ref_generation,
526 u64 owner_objectid, int del)
528 struct btrfs_key key;
529 struct btrfs_extent_ref *ref;
530 struct extent_buffer *leaf;
534 key.objectid = bytenr;
535 key.type = BTRFS_EXTENT_REF_KEY;
538 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
546 leaf = path->nodes[0];
547 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
548 ref_objectid = btrfs_ref_objectid(leaf, ref);
549 if (btrfs_ref_root(leaf, ref) != ref_root ||
550 btrfs_ref_generation(leaf, ref) != ref_generation ||
551 (ref_objectid != owner_objectid &&
552 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
562 static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
563 struct btrfs_root *root,
564 struct btrfs_path *path,
565 u64 bytenr, u64 parent,
566 u64 ref_root, u64 ref_generation,
569 struct btrfs_key key;
570 struct extent_buffer *leaf;
571 struct btrfs_extent_ref *ref;
575 key.objectid = bytenr;
576 key.type = BTRFS_EXTENT_REF_KEY;
579 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
581 leaf = path->nodes[0];
582 ref = btrfs_item_ptr(leaf, path->slots[0],
583 struct btrfs_extent_ref);
584 btrfs_set_ref_root(leaf, ref, ref_root);
585 btrfs_set_ref_generation(leaf, ref, ref_generation);
586 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
587 btrfs_set_ref_num_refs(leaf, ref, 1);
588 } else if (ret == -EEXIST) {
590 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
591 leaf = path->nodes[0];
592 ref = btrfs_item_ptr(leaf, path->slots[0],
593 struct btrfs_extent_ref);
594 if (btrfs_ref_root(leaf, ref) != ref_root ||
595 btrfs_ref_generation(leaf, ref) != ref_generation) {
601 num_refs = btrfs_ref_num_refs(leaf, ref);
602 BUG_ON(num_refs == 0);
603 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
605 existing_owner = btrfs_ref_objectid(leaf, ref);
606 if (existing_owner != owner_objectid &&
607 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
608 btrfs_set_ref_objectid(leaf, ref,
609 BTRFS_MULTIPLE_OBJECTIDS);
615 btrfs_mark_buffer_dirty(path->nodes[0]);
617 btrfs_release_path(root, path);
621 static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path)
625 struct extent_buffer *leaf;
626 struct btrfs_extent_ref *ref;
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 num_refs = btrfs_ref_num_refs(leaf, ref);
633 BUG_ON(num_refs == 0);
636 ret = btrfs_del_item(trans, root, path);
638 btrfs_set_ref_num_refs(leaf, ref, num_refs);
639 btrfs_mark_buffer_dirty(leaf);
641 btrfs_release_path(root, path);
645 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root, u64 bytenr,
647 u64 orig_parent, u64 parent,
648 u64 orig_root, u64 ref_root,
649 u64 orig_generation, u64 ref_generation,
653 struct btrfs_root *extent_root = root->fs_info->extent_root;
654 struct btrfs_path *path;
656 if (root == root->fs_info->extent_root) {
657 struct pending_extent_op *extent_op;
660 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
661 num_bytes = btrfs_level_size(root, (int)owner_objectid);
662 mutex_lock(&root->fs_info->extent_ins_mutex);
663 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
664 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
666 ret = get_state_private(&root->fs_info->extent_ins,
669 extent_op = (struct pending_extent_op *)
671 BUG_ON(extent_op->parent != orig_parent);
672 BUG_ON(extent_op->generation != orig_generation);
674 extent_op->parent = parent;
675 extent_op->generation = ref_generation;
677 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
680 extent_op->type = PENDING_BACKREF_UPDATE;
681 extent_op->bytenr = bytenr;
682 extent_op->num_bytes = num_bytes;
683 extent_op->parent = parent;
684 extent_op->orig_parent = orig_parent;
685 extent_op->generation = ref_generation;
686 extent_op->orig_generation = orig_generation;
687 extent_op->level = (int)owner_objectid;
689 set_extent_bits(&root->fs_info->extent_ins,
690 bytenr, bytenr + num_bytes - 1,
691 EXTENT_WRITEBACK, GFP_NOFS);
692 set_state_private(&root->fs_info->extent_ins,
693 bytenr, (unsigned long)extent_op);
695 mutex_unlock(&root->fs_info->extent_ins_mutex);
699 path = btrfs_alloc_path();
702 ret = lookup_extent_backref(trans, extent_root, path,
703 bytenr, orig_parent, orig_root,
704 orig_generation, owner_objectid, 1);
707 ret = remove_extent_backref(trans, extent_root, path);
710 ret = insert_extent_backref(trans, extent_root, path, bytenr,
711 parent, ref_root, ref_generation,
714 finish_current_insert(trans, extent_root, 0);
715 del_pending_extents(trans, extent_root, 0);
717 btrfs_free_path(path);
721 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root, u64 bytenr,
723 u64 orig_parent, u64 parent,
724 u64 ref_root, u64 ref_generation,
728 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
729 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
731 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
732 parent, ref_root, ref_root,
733 ref_generation, ref_generation,
738 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
739 struct btrfs_root *root, u64 bytenr,
740 u64 orig_parent, u64 parent,
741 u64 orig_root, u64 ref_root,
742 u64 orig_generation, u64 ref_generation,
745 struct btrfs_path *path;
747 struct btrfs_key key;
748 struct extent_buffer *l;
749 struct btrfs_extent_item *item;
752 path = btrfs_alloc_path();
757 key.objectid = bytenr;
758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = (u64)-1;
761 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
765 BUG_ON(ret == 0 || path->slots[0] == 0);
770 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
771 BUG_ON(key.objectid != bytenr);
772 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
774 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
775 refs = btrfs_extent_refs(l, item);
776 btrfs_set_extent_refs(l, item, refs + 1);
777 btrfs_mark_buffer_dirty(path->nodes[0]);
779 btrfs_release_path(root->fs_info->extent_root, path);
782 ret = insert_extent_backref(trans, root->fs_info->extent_root,
783 path, bytenr, parent,
784 ref_root, ref_generation,
787 finish_current_insert(trans, root->fs_info->extent_root, 0);
788 del_pending_extents(trans, root->fs_info->extent_root, 0);
790 btrfs_free_path(path);
794 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
795 struct btrfs_root *root,
796 u64 bytenr, u64 num_bytes, u64 parent,
797 u64 ref_root, u64 ref_generation,
801 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
802 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
804 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
805 0, ref_root, 0, ref_generation,
810 int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
811 struct btrfs_root *root)
813 finish_current_insert(trans, root->fs_info->extent_root, 1);
814 del_pending_extents(trans, root->fs_info->extent_root, 1);
818 int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
819 struct btrfs_root *root, u64 bytenr,
820 u64 num_bytes, u32 *refs)
822 struct btrfs_path *path;
824 struct btrfs_key key;
825 struct extent_buffer *l;
826 struct btrfs_extent_item *item;
828 WARN_ON(num_bytes < root->sectorsize);
829 path = btrfs_alloc_path();
831 key.objectid = bytenr;
832 key.offset = num_bytes;
833 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
834 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
839 btrfs_print_leaf(root, path->nodes[0]);
840 printk("failed to find block number %Lu\n", bytenr);
844 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
845 *refs = btrfs_extent_refs(l, item);
847 btrfs_free_path(path);
851 static int get_reference_status(struct btrfs_root *root, u64 bytenr,
852 u64 parent_gen, u64 ref_objectid,
853 u64 *min_generation, u32 *ref_count)
855 struct btrfs_root *extent_root = root->fs_info->extent_root;
856 struct btrfs_path *path;
857 struct extent_buffer *leaf;
858 struct btrfs_extent_ref *ref_item;
859 struct btrfs_key key;
860 struct btrfs_key found_key;
861 u64 root_objectid = root->root_key.objectid;
866 key.objectid = bytenr;
867 key.offset = (u64)-1;
868 key.type = BTRFS_EXTENT_ITEM_KEY;
870 path = btrfs_alloc_path();
871 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
875 if (ret < 0 || path->slots[0] == 0)
879 leaf = path->nodes[0];
880 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
882 if (found_key.objectid != bytenr ||
883 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
889 *min_generation = (u64)-1;
892 leaf = path->nodes[0];
893 nritems = btrfs_header_nritems(leaf);
894 if (path->slots[0] >= nritems) {
895 ret = btrfs_next_leaf(extent_root, path);
902 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
903 if (found_key.objectid != bytenr)
906 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
911 ref_item = btrfs_item_ptr(leaf, path->slots[0],
912 struct btrfs_extent_ref);
913 ref_generation = btrfs_ref_generation(leaf, ref_item);
915 * For (parent_gen > 0 && parent_gen > ref_generation):
917 * we reach here through the oldest root, therefore
918 * all other reference from same snapshot should have
919 * a larger generation.
921 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
922 (parent_gen > 0 && parent_gen > ref_generation) ||
923 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
924 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
930 if (*min_generation > ref_generation)
931 *min_generation = ref_generation;
937 btrfs_free_path(path);
941 int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
942 struct btrfs_root *root,
943 struct btrfs_key *key, u64 bytenr)
945 struct btrfs_root *old_root;
946 struct btrfs_path *path = NULL;
947 struct extent_buffer *eb;
948 struct btrfs_file_extent_item *item;
956 BUG_ON(trans == NULL);
957 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
958 ret = get_reference_status(root, bytenr, 0, key->objectid,
959 &min_generation, &ref_count);
966 old_root = root->dirty_root->root;
967 ref_generation = old_root->root_key.offset;
969 /* all references are created in running transaction */
970 if (min_generation > ref_generation) {
975 path = btrfs_alloc_path();
981 path->skip_locking = 1;
982 /* if no item found, the extent is referenced by other snapshot */
983 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
988 item = btrfs_item_ptr(eb, path->slots[0],
989 struct btrfs_file_extent_item);
990 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
991 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
996 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
998 eb = path->nodes[level];
1001 extent_start = eb->start;
1003 extent_start = bytenr;
1005 ret = get_reference_status(root, extent_start, ref_generation,
1006 0, &min_generation, &ref_count);
1010 if (ref_count != 1) {
1015 ref_generation = btrfs_header_generation(eb);
1020 btrfs_free_path(path);
1024 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1025 struct extent_buffer *buf, u32 nr_extents)
1027 struct btrfs_key key;
1028 struct btrfs_file_extent_item *fi;
1036 if (!root->ref_cows)
1039 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1041 root_gen = root->root_key.offset;
1044 root_gen = trans->transid - 1;
1047 level = btrfs_header_level(buf);
1048 nritems = btrfs_header_nritems(buf);
1051 struct btrfs_leaf_ref *ref;
1052 struct btrfs_extent_info *info;
1054 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1060 ref->root_gen = root_gen;
1061 ref->bytenr = buf->start;
1062 ref->owner = btrfs_header_owner(buf);
1063 ref->generation = btrfs_header_generation(buf);
1064 ref->nritems = nr_extents;
1065 info = ref->extents;
1067 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1069 btrfs_item_key_to_cpu(buf, &key, i);
1070 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1072 fi = btrfs_item_ptr(buf, i,
1073 struct btrfs_file_extent_item);
1074 if (btrfs_file_extent_type(buf, fi) ==
1075 BTRFS_FILE_EXTENT_INLINE)
1077 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1078 if (disk_bytenr == 0)
1081 info->bytenr = disk_bytenr;
1083 btrfs_file_extent_disk_num_bytes(buf, fi);
1084 info->objectid = key.objectid;
1085 info->offset = key.offset;
1089 ret = btrfs_add_leaf_ref(root, ref, shared);
1090 if (ret == -EEXIST && shared) {
1091 struct btrfs_leaf_ref *old;
1092 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1094 btrfs_remove_leaf_ref(root, old);
1095 btrfs_free_leaf_ref(root, old);
1096 ret = btrfs_add_leaf_ref(root, ref, shared);
1099 btrfs_free_leaf_ref(root, ref);
1105 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1106 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1113 u64 orig_generation;
1115 u32 nr_file_extents = 0;
1116 struct btrfs_key key;
1117 struct btrfs_file_extent_item *fi;
1122 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1123 u64, u64, u64, u64, u64, u64, u64, u64);
1125 ref_root = btrfs_header_owner(buf);
1126 ref_generation = btrfs_header_generation(buf);
1127 orig_root = btrfs_header_owner(orig_buf);
1128 orig_generation = btrfs_header_generation(orig_buf);
1130 nritems = btrfs_header_nritems(buf);
1131 level = btrfs_header_level(buf);
1133 if (root->ref_cows) {
1134 process_func = __btrfs_inc_extent_ref;
1137 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1140 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1142 process_func = __btrfs_update_extent_ref;
1145 for (i = 0; i < nritems; i++) {
1148 btrfs_item_key_to_cpu(buf, &key, i);
1149 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1151 fi = btrfs_item_ptr(buf, i,
1152 struct btrfs_file_extent_item);
1153 if (btrfs_file_extent_type(buf, fi) ==
1154 BTRFS_FILE_EXTENT_INLINE)
1156 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1162 ret = process_func(trans, root, bytenr,
1163 orig_buf->start, buf->start,
1164 orig_root, ref_root,
1165 orig_generation, ref_generation,
1174 bytenr = btrfs_node_blockptr(buf, i);
1175 ret = process_func(trans, root, bytenr,
1176 orig_buf->start, buf->start,
1177 orig_root, ref_root,
1178 orig_generation, ref_generation,
1190 *nr_extents = nr_file_extents;
1192 *nr_extents = nritems;
1200 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1201 struct btrfs_root *root, struct extent_buffer *orig_buf,
1202 struct extent_buffer *buf, int start_slot, int nr)
1209 u64 orig_generation;
1210 struct btrfs_key key;
1211 struct btrfs_file_extent_item *fi;
1217 BUG_ON(start_slot < 0);
1218 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1220 ref_root = btrfs_header_owner(buf);
1221 ref_generation = btrfs_header_generation(buf);
1222 orig_root = btrfs_header_owner(orig_buf);
1223 orig_generation = btrfs_header_generation(orig_buf);
1224 level = btrfs_header_level(buf);
1226 if (!root->ref_cows) {
1228 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1231 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1235 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1238 btrfs_item_key_to_cpu(buf, &key, slot);
1239 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1241 fi = btrfs_item_ptr(buf, slot,
1242 struct btrfs_file_extent_item);
1243 if (btrfs_file_extent_type(buf, fi) ==
1244 BTRFS_FILE_EXTENT_INLINE)
1246 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1249 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1250 orig_buf->start, buf->start,
1251 orig_root, ref_root,
1252 orig_generation, ref_generation,
1257 bytenr = btrfs_node_blockptr(buf, slot);
1258 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1259 orig_buf->start, buf->start,
1260 orig_root, ref_root,
1261 orig_generation, ref_generation,
1273 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1274 struct btrfs_root *root,
1275 struct btrfs_path *path,
1276 struct btrfs_block_group_cache *cache)
1280 struct btrfs_root *extent_root = root->fs_info->extent_root;
1282 struct extent_buffer *leaf;
1284 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1289 leaf = path->nodes[0];
1290 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1291 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1292 btrfs_mark_buffer_dirty(leaf);
1293 btrfs_release_path(extent_root, path);
1295 finish_current_insert(trans, extent_root, 0);
1296 pending_ret = del_pending_extents(trans, extent_root, 0);
1305 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1306 struct btrfs_root *root)
1308 struct btrfs_block_group_cache *cache, *entry;
1312 struct btrfs_path *path;
1315 path = btrfs_alloc_path();
1321 spin_lock(&root->fs_info->block_group_cache_lock);
1322 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1323 n; n = rb_next(n)) {
1324 entry = rb_entry(n, struct btrfs_block_group_cache,
1331 spin_unlock(&root->fs_info->block_group_cache_lock);
1337 last += cache->key.offset;
1339 err = write_one_cache_group(trans, root,
1342 * if we fail to write the cache group, we want
1343 * to keep it marked dirty in hopes that a later
1351 btrfs_free_path(path);
1355 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1356 u64 total_bytes, u64 bytes_used,
1357 struct btrfs_space_info **space_info)
1359 struct btrfs_space_info *found;
1361 found = __find_space_info(info, flags);
1363 spin_lock(&found->lock);
1364 found->total_bytes += total_bytes;
1365 found->bytes_used += bytes_used;
1367 spin_unlock(&found->lock);
1368 *space_info = found;
1371 found = kmalloc(sizeof(*found), GFP_NOFS);
1375 list_add(&found->list, &info->space_info);
1376 INIT_LIST_HEAD(&found->block_groups);
1377 init_rwsem(&found->groups_sem);
1378 spin_lock_init(&found->lock);
1379 found->flags = flags;
1380 found->total_bytes = total_bytes;
1381 found->bytes_used = bytes_used;
1382 found->bytes_pinned = 0;
1383 found->bytes_reserved = 0;
1385 found->force_alloc = 0;
1386 *space_info = found;
1390 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1392 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1393 BTRFS_BLOCK_GROUP_RAID1 |
1394 BTRFS_BLOCK_GROUP_RAID10 |
1395 BTRFS_BLOCK_GROUP_DUP);
1397 if (flags & BTRFS_BLOCK_GROUP_DATA)
1398 fs_info->avail_data_alloc_bits |= extra_flags;
1399 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1400 fs_info->avail_metadata_alloc_bits |= extra_flags;
1401 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1402 fs_info->avail_system_alloc_bits |= extra_flags;
1406 static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1408 u64 num_devices = root->fs_info->fs_devices->num_devices;
1410 if (num_devices == 1)
1411 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1412 if (num_devices < 4)
1413 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1415 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1416 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1417 BTRFS_BLOCK_GROUP_RAID10))) {
1418 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1421 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1422 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1423 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1426 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1427 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1428 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1429 (flags & BTRFS_BLOCK_GROUP_DUP)))
1430 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1434 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1435 struct btrfs_root *extent_root, u64 alloc_bytes,
1436 u64 flags, int force)
1438 struct btrfs_space_info *space_info;
1442 int ret = 0, waited = 0;
1444 flags = reduce_alloc_profile(extent_root, flags);
1446 space_info = __find_space_info(extent_root->fs_info, flags);
1448 ret = update_space_info(extent_root->fs_info, flags,
1452 BUG_ON(!space_info);
1454 spin_lock(&space_info->lock);
1455 if (space_info->force_alloc) {
1457 space_info->force_alloc = 0;
1459 if (space_info->full) {
1460 spin_unlock(&space_info->lock);
1464 thresh = div_factor(space_info->total_bytes, 6);
1466 (space_info->bytes_used + space_info->bytes_pinned +
1467 space_info->bytes_reserved + alloc_bytes) < thresh) {
1468 spin_unlock(&space_info->lock);
1472 spin_unlock(&space_info->lock);
1474 ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
1475 if (!ret && !force) {
1478 mutex_lock(&extent_root->fs_info->chunk_mutex);
1483 spin_lock(&space_info->lock);
1484 if (space_info->full) {
1485 spin_unlock(&space_info->lock);
1488 spin_unlock(&space_info->lock);
1491 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1493 printk("space info full %Lu\n", flags);
1494 space_info->full = 1;
1498 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
1499 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
1502 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1507 static int update_block_group(struct btrfs_trans_handle *trans,
1508 struct btrfs_root *root,
1509 u64 bytenr, u64 num_bytes, int alloc,
1512 struct btrfs_block_group_cache *cache;
1513 struct btrfs_fs_info *info = root->fs_info;
1514 u64 total = num_bytes;
1519 cache = btrfs_lookup_block_group(info, bytenr);
1523 byte_in_group = bytenr - cache->key.objectid;
1524 WARN_ON(byte_in_group > cache->key.offset);
1526 spin_lock(&cache->space_info->lock);
1527 spin_lock(&cache->lock);
1529 old_val = btrfs_block_group_used(&cache->item);
1530 num_bytes = min(total, cache->key.offset - byte_in_group);
1532 old_val += num_bytes;
1533 cache->space_info->bytes_used += num_bytes;
1534 btrfs_set_block_group_used(&cache->item, old_val);
1535 spin_unlock(&cache->lock);
1536 spin_unlock(&cache->space_info->lock);
1538 old_val -= num_bytes;
1539 cache->space_info->bytes_used -= num_bytes;
1540 btrfs_set_block_group_used(&cache->item, old_val);
1541 spin_unlock(&cache->lock);
1542 spin_unlock(&cache->space_info->lock);
1545 ret = btrfs_add_free_space(cache, bytenr,
1552 bytenr += num_bytes;
1557 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1559 struct btrfs_block_group_cache *cache;
1561 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1565 return cache->key.objectid;
1568 int btrfs_update_pinned_extents(struct btrfs_root *root,
1569 u64 bytenr, u64 num, int pin)
1572 struct btrfs_block_group_cache *cache;
1573 struct btrfs_fs_info *fs_info = root->fs_info;
1575 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
1577 set_extent_dirty(&fs_info->pinned_extents,
1578 bytenr, bytenr + num - 1, GFP_NOFS);
1580 clear_extent_dirty(&fs_info->pinned_extents,
1581 bytenr, bytenr + num - 1, GFP_NOFS);
1584 cache = btrfs_lookup_block_group(fs_info, bytenr);
1586 len = min(num, cache->key.offset -
1587 (bytenr - cache->key.objectid));
1589 spin_lock(&cache->space_info->lock);
1590 spin_lock(&cache->lock);
1591 cache->pinned += len;
1592 cache->space_info->bytes_pinned += len;
1593 spin_unlock(&cache->lock);
1594 spin_unlock(&cache->space_info->lock);
1595 fs_info->total_pinned += len;
1597 spin_lock(&cache->space_info->lock);
1598 spin_lock(&cache->lock);
1599 cache->pinned -= len;
1600 cache->space_info->bytes_pinned -= len;
1601 spin_unlock(&cache->lock);
1602 spin_unlock(&cache->space_info->lock);
1603 fs_info->total_pinned -= len;
1611 static int update_reserved_extents(struct btrfs_root *root,
1612 u64 bytenr, u64 num, int reserve)
1615 struct btrfs_block_group_cache *cache;
1616 struct btrfs_fs_info *fs_info = root->fs_info;
1619 cache = btrfs_lookup_block_group(fs_info, bytenr);
1621 len = min(num, cache->key.offset -
1622 (bytenr - cache->key.objectid));
1624 spin_lock(&cache->space_info->lock);
1625 spin_lock(&cache->lock);
1627 cache->reserved += len;
1628 cache->space_info->bytes_reserved += len;
1630 cache->reserved -= len;
1631 cache->space_info->bytes_reserved -= len;
1633 spin_unlock(&cache->lock);
1634 spin_unlock(&cache->space_info->lock);
1641 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
1646 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
1649 mutex_lock(&root->fs_info->pinned_mutex);
1651 ret = find_first_extent_bit(pinned_extents, last,
1652 &start, &end, EXTENT_DIRTY);
1655 set_extent_dirty(copy, start, end, GFP_NOFS);
1658 mutex_unlock(&root->fs_info->pinned_mutex);
1662 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1663 struct btrfs_root *root,
1664 struct extent_io_tree *unpin)
1669 struct btrfs_block_group_cache *cache;
1671 mutex_lock(&root->fs_info->pinned_mutex);
1673 ret = find_first_extent_bit(unpin, 0, &start, &end,
1677 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
1678 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1679 cache = btrfs_lookup_block_group(root->fs_info, start);
1681 btrfs_add_free_space(cache, start, end - start + 1);
1682 if (need_resched()) {
1683 mutex_unlock(&root->fs_info->pinned_mutex);
1685 mutex_lock(&root->fs_info->pinned_mutex);
1688 mutex_unlock(&root->fs_info->pinned_mutex);
1692 static int finish_current_insert(struct btrfs_trans_handle *trans,
1693 struct btrfs_root *extent_root, int all)
1699 struct btrfs_fs_info *info = extent_root->fs_info;
1700 struct btrfs_path *path;
1701 struct btrfs_extent_ref *ref;
1702 struct pending_extent_op *extent_op;
1703 struct btrfs_key key;
1704 struct btrfs_extent_item extent_item;
1708 btrfs_set_stack_extent_refs(&extent_item, 1);
1709 path = btrfs_alloc_path();
1712 mutex_lock(&info->extent_ins_mutex);
1713 ret = find_first_extent_bit(&info->extent_ins, search, &start,
1714 &end, EXTENT_WRITEBACK);
1716 mutex_unlock(&info->extent_ins_mutex);
1717 if (search && all) {
1724 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
1727 mutex_unlock(&info->extent_ins_mutex);
1733 ret = get_state_private(&info->extent_ins, start, &priv);
1735 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1737 mutex_unlock(&info->extent_ins_mutex);
1739 if (extent_op->type == PENDING_EXTENT_INSERT) {
1740 key.objectid = start;
1741 key.offset = end + 1 - start;
1742 key.type = BTRFS_EXTENT_ITEM_KEY;
1743 err = btrfs_insert_item(trans, extent_root, &key,
1744 &extent_item, sizeof(extent_item));
1747 mutex_lock(&info->extent_ins_mutex);
1748 clear_extent_bits(&info->extent_ins, start, end,
1749 EXTENT_WRITEBACK, GFP_NOFS);
1750 mutex_unlock(&info->extent_ins_mutex);
1752 err = insert_extent_backref(trans, extent_root, path,
1753 start, extent_op->parent,
1754 extent_root->root_key.objectid,
1755 extent_op->generation,
1758 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1759 err = lookup_extent_backref(trans, extent_root, path,
1760 start, extent_op->orig_parent,
1761 extent_root->root_key.objectid,
1762 extent_op->orig_generation,
1763 extent_op->level, 0);
1766 mutex_lock(&info->extent_ins_mutex);
1767 clear_extent_bits(&info->extent_ins, start, end,
1768 EXTENT_WRITEBACK, GFP_NOFS);
1769 mutex_unlock(&info->extent_ins_mutex);
1771 key.objectid = start;
1772 key.offset = extent_op->parent;
1773 key.type = BTRFS_EXTENT_REF_KEY;
1774 err = btrfs_set_item_key_safe(trans, extent_root, path,
1777 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1778 struct btrfs_extent_ref);
1779 btrfs_set_ref_generation(path->nodes[0], ref,
1780 extent_op->generation);
1781 btrfs_mark_buffer_dirty(path->nodes[0]);
1782 btrfs_release_path(extent_root, path);
1787 unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
1795 btrfs_free_path(path);
1799 static int pin_down_bytes(struct btrfs_trans_handle *trans,
1800 struct btrfs_root *root,
1801 u64 bytenr, u64 num_bytes, int is_data)
1804 struct extent_buffer *buf;
1809 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1813 /* we can reuse a block if it hasn't been written
1814 * and it is from this transaction. We can't
1815 * reuse anything from the tree log root because
1816 * it has tiny sub-transactions.
1818 if (btrfs_buffer_uptodate(buf, 0) &&
1819 btrfs_try_tree_lock(buf)) {
1820 u64 header_owner = btrfs_header_owner(buf);
1821 u64 header_transid = btrfs_header_generation(buf);
1822 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
1823 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
1824 header_transid == trans->transid &&
1825 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1826 clean_tree_block(NULL, root, buf);
1827 btrfs_tree_unlock(buf);
1828 free_extent_buffer(buf);
1831 btrfs_tree_unlock(buf);
1833 free_extent_buffer(buf);
1835 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1842 * remove an extent from the root, returns 0 on success
1844 static int __free_extent(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *root,
1846 u64 bytenr, u64 num_bytes, u64 parent,
1847 u64 root_objectid, u64 ref_generation,
1848 u64 owner_objectid, int pin, int mark_free)
1850 struct btrfs_path *path;
1851 struct btrfs_key key;
1852 struct btrfs_fs_info *info = root->fs_info;
1853 struct btrfs_root *extent_root = info->extent_root;
1854 struct extent_buffer *leaf;
1856 int extent_slot = 0;
1857 int found_extent = 0;
1859 struct btrfs_extent_item *ei;
1862 key.objectid = bytenr;
1863 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
1864 key.offset = num_bytes;
1865 path = btrfs_alloc_path();
1870 ret = lookup_extent_backref(trans, extent_root, path,
1871 bytenr, parent, root_objectid,
1872 ref_generation, owner_objectid, 1);
1874 struct btrfs_key found_key;
1875 extent_slot = path->slots[0];
1876 while(extent_slot > 0) {
1878 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1880 if (found_key.objectid != bytenr)
1882 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1883 found_key.offset == num_bytes) {
1887 if (path->slots[0] - extent_slot > 5)
1890 if (!found_extent) {
1891 ret = remove_extent_backref(trans, extent_root, path);
1893 btrfs_release_path(extent_root, path);
1894 ret = btrfs_search_slot(trans, extent_root,
1897 extent_slot = path->slots[0];
1900 btrfs_print_leaf(extent_root, path->nodes[0]);
1902 printk("Unable to find ref byte nr %Lu root %Lu "
1903 "gen %Lu owner %Lu\n", bytenr,
1904 root_objectid, ref_generation, owner_objectid);
1907 leaf = path->nodes[0];
1908 ei = btrfs_item_ptr(leaf, extent_slot,
1909 struct btrfs_extent_item);
1910 refs = btrfs_extent_refs(leaf, ei);
1913 btrfs_set_extent_refs(leaf, ei, refs);
1915 btrfs_mark_buffer_dirty(leaf);
1917 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1918 struct btrfs_extent_ref *ref;
1919 ref = btrfs_item_ptr(leaf, path->slots[0],
1920 struct btrfs_extent_ref);
1921 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
1922 /* if the back ref and the extent are next to each other
1923 * they get deleted below in one shot
1925 path->slots[0] = extent_slot;
1927 } else if (found_extent) {
1928 /* otherwise delete the extent back ref */
1929 ret = remove_extent_backref(trans, extent_root, path);
1931 /* if refs are 0, we need to setup the path for deletion */
1933 btrfs_release_path(extent_root, path);
1934 ret = btrfs_search_slot(trans, extent_root, &key, path,
1943 #ifdef BIO_RW_DISCARD
1944 u64 map_length = num_bytes;
1945 struct btrfs_multi_bio *multi = NULL;
1949 mutex_lock(&root->fs_info->pinned_mutex);
1950 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1951 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
1952 mutex_unlock(&root->fs_info->pinned_mutex);
1958 /* block accounting for super block */
1959 spin_lock_irq(&info->delalloc_lock);
1960 super_used = btrfs_super_bytes_used(&info->super_copy);
1961 btrfs_set_super_bytes_used(&info->super_copy,
1962 super_used - num_bytes);
1963 spin_unlock_irq(&info->delalloc_lock);
1965 /* block accounting for root item */
1966 root_used = btrfs_root_used(&root->root_item);
1967 btrfs_set_root_used(&root->root_item,
1968 root_used - num_bytes);
1969 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1972 btrfs_release_path(extent_root, path);
1973 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
1977 #ifdef BIO_RW_DISCARD
1978 /* Tell the block device(s) that the sectors can be discarded */
1979 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1980 bytenr, &map_length, &multi, 0);
1982 struct btrfs_bio_stripe *stripe = multi->stripes;
1985 if (map_length > num_bytes)
1986 map_length = num_bytes;
1988 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1989 blkdev_issue_discard(stripe->dev->bdev,
1990 stripe->physical >> 9,
1997 btrfs_free_path(path);
1998 finish_current_insert(trans, extent_root, 0);
2003 * find all the blocks marked as pending in the radix tree and remove
2004 * them from the extent map
2006 static int del_pending_extents(struct btrfs_trans_handle *trans, struct
2007 btrfs_root *extent_root, int all)
2015 struct extent_io_tree *pending_del;
2016 struct extent_io_tree *extent_ins;
2017 struct pending_extent_op *extent_op;
2018 struct btrfs_fs_info *info = extent_root->fs_info;
2020 extent_ins = &extent_root->fs_info->extent_ins;
2021 pending_del = &extent_root->fs_info->pending_del;
2024 mutex_lock(&info->extent_ins_mutex);
2025 ret = find_first_extent_bit(pending_del, search, &start, &end,
2028 mutex_unlock(&info->extent_ins_mutex);
2029 if (all && search) {
2036 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2039 mutex_unlock(&info->extent_ins_mutex);
2045 ret = get_state_private(pending_del, start, &priv);
2047 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2049 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
2051 if (!test_range_bit(extent_ins, start, end,
2052 EXTENT_WRITEBACK, 0)) {
2053 mutex_unlock(&info->extent_ins_mutex);
2055 ret = __free_extent(trans, extent_root,
2056 start, end + 1 - start,
2057 extent_op->orig_parent,
2058 extent_root->root_key.objectid,
2059 extent_op->orig_generation,
2060 extent_op->level, 1, 0);
2065 ret = get_state_private(&info->extent_ins, start,
2068 extent_op = (struct pending_extent_op *)
2069 (unsigned long)priv;
2071 clear_extent_bits(&info->extent_ins, start, end,
2072 EXTENT_WRITEBACK, GFP_NOFS);
2074 mutex_unlock(&info->extent_ins_mutex);
2076 if (extent_op->type == PENDING_BACKREF_UPDATE)
2079 mutex_lock(&extent_root->fs_info->pinned_mutex);
2080 ret = pin_down_bytes(trans, extent_root, start,
2081 end + 1 - start, 0);
2082 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2084 ret = update_block_group(trans, extent_root, start,
2085 end + 1 - start, 0, ret > 0);
2092 unlock_extent(extent_ins, start, end, GFP_NOFS);
2104 * remove an extent from the root, returns 0 on success
2106 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2107 struct btrfs_root *root,
2108 u64 bytenr, u64 num_bytes, u64 parent,
2109 u64 root_objectid, u64 ref_generation,
2110 u64 owner_objectid, int pin)
2112 struct btrfs_root *extent_root = root->fs_info->extent_root;
2116 WARN_ON(num_bytes < root->sectorsize);
2117 if (root == extent_root) {
2118 struct pending_extent_op *extent_op;
2120 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2123 extent_op->type = PENDING_EXTENT_DELETE;
2124 extent_op->bytenr = bytenr;
2125 extent_op->num_bytes = num_bytes;
2126 extent_op->parent = parent;
2127 extent_op->orig_parent = parent;
2128 extent_op->generation = ref_generation;
2129 extent_op->orig_generation = ref_generation;
2130 extent_op->level = (int)owner_objectid;
2132 mutex_lock(&root->fs_info->extent_ins_mutex);
2133 set_extent_bits(&root->fs_info->pending_del,
2134 bytenr, bytenr + num_bytes - 1,
2135 EXTENT_WRITEBACK, GFP_NOFS);
2136 set_state_private(&root->fs_info->pending_del,
2137 bytenr, (unsigned long)extent_op);
2138 mutex_unlock(&root->fs_info->extent_ins_mutex);
2141 /* if metadata always pin */
2142 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2143 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2144 struct btrfs_block_group_cache *cache;
2146 /* btrfs_free_reserved_extent */
2147 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2149 btrfs_add_free_space(cache, bytenr, num_bytes);
2150 update_reserved_extents(root, bytenr, num_bytes, 0);
2156 /* if data pin when any transaction has committed this */
2157 if (ref_generation != trans->transid)
2160 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
2161 root_objectid, ref_generation,
2162 owner_objectid, pin, pin == 0);
2164 finish_current_insert(trans, root->fs_info->extent_root, 0);
2165 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
2166 return ret ? ret : pending_ret;
2169 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2170 struct btrfs_root *root,
2171 u64 bytenr, u64 num_bytes, u64 parent,
2172 u64 root_objectid, u64 ref_generation,
2173 u64 owner_objectid, int pin)
2177 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
2178 root_objectid, ref_generation,
2179 owner_objectid, pin);
2183 static u64 stripe_align(struct btrfs_root *root, u64 val)
2185 u64 mask = ((u64)root->stripesize - 1);
2186 u64 ret = (val + mask) & ~mask;
2191 * walks the btree of allocated extents and find a hole of a given size.
2192 * The key ins is changed to record the hole:
2193 * ins->objectid == block start
2194 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2195 * ins->offset == number of blocks
2196 * Any available blocks before search_start are skipped.
2198 static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2199 struct btrfs_root *orig_root,
2200 u64 num_bytes, u64 empty_size,
2201 u64 search_start, u64 search_end,
2202 u64 hint_byte, struct btrfs_key *ins,
2203 u64 exclude_start, u64 exclude_nr,
2207 struct btrfs_root * root = orig_root->fs_info->extent_root;
2208 u64 total_needed = num_bytes;
2209 u64 *last_ptr = NULL;
2210 struct btrfs_block_group_cache *block_group = NULL;
2211 int chunk_alloc_done = 0;
2212 int empty_cluster = 2 * 1024 * 1024;
2213 int allowed_chunk_alloc = 0;
2214 struct list_head *head = NULL, *cur = NULL;
2216 struct btrfs_space_info *space_info;
2218 WARN_ON(num_bytes < root->sectorsize);
2219 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2223 if (orig_root->ref_cows || empty_size)
2224 allowed_chunk_alloc = 1;
2226 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2227 last_ptr = &root->fs_info->last_alloc;
2228 empty_cluster = 256 * 1024;
2231 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2232 last_ptr = &root->fs_info->last_data_alloc;
2236 hint_byte = *last_ptr;
2238 empty_size += empty_cluster;
2240 search_start = max(search_start, first_logical_byte(root, 0));
2241 search_start = max(search_start, hint_byte);
2242 total_needed += empty_size;
2244 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2245 space_info = __find_space_info(root->fs_info, data);
2247 down_read(&space_info->groups_sem);
2249 struct btrfs_free_space *free_space;
2251 * the only way this happens if our hint points to a block
2252 * group thats not of the proper type, while looping this
2253 * should never happen
2255 WARN_ON(!block_group);
2256 mutex_lock(&block_group->alloc_mutex);
2257 if (unlikely(!block_group_bits(block_group, data)))
2260 ret = cache_block_group(root, block_group);
2262 mutex_unlock(&block_group->alloc_mutex);
2266 if (block_group->ro)
2269 free_space = btrfs_find_free_space(block_group, search_start,
2272 u64 start = block_group->key.objectid;
2273 u64 end = block_group->key.objectid +
2274 block_group->key.offset;
2276 search_start = stripe_align(root, free_space->offset);
2278 /* move on to the next group */
2279 if (search_start + num_bytes >= search_end)
2282 /* move on to the next group */
2283 if (search_start + num_bytes > end)
2286 if (exclude_nr > 0 &&
2287 (search_start + num_bytes > exclude_start &&
2288 search_start < exclude_start + exclude_nr)) {
2289 search_start = exclude_start + exclude_nr;
2291 * if search_start is still in this block group
2292 * then we just re-search this block group
2294 if (search_start >= start &&
2295 search_start < end) {
2296 mutex_unlock(&block_group->alloc_mutex);
2300 /* else we go to the next block group */
2304 ins->objectid = search_start;
2305 ins->offset = num_bytes;
2307 btrfs_remove_free_space_lock(block_group, search_start,
2309 /* we are all good, lets return */
2310 mutex_unlock(&block_group->alloc_mutex);
2314 mutex_unlock(&block_group->alloc_mutex);
2316 * Here's how this works.
2317 * loop == 0: we were searching a block group via a hint
2318 * and didn't find anything, so we start at
2319 * the head of the block groups and keep searching
2320 * loop == 1: we're searching through all of the block groups
2321 * if we hit the head again we have searched
2322 * all of the block groups for this space and we
2323 * need to try and allocate, if we cant error out.
2324 * loop == 2: we allocated more space and are looping through
2325 * all of the block groups again.
2328 head = &space_info->block_groups;
2331 if (last_ptr && *last_ptr) {
2332 total_needed += empty_cluster;
2336 } else if (loop == 1 && cur == head) {
2337 if (allowed_chunk_alloc && !chunk_alloc_done) {
2338 up_read(&space_info->groups_sem);
2339 ret = do_chunk_alloc(trans, root, num_bytes +
2340 2 * 1024 * 1024, data, 1);
2343 down_read(&space_info->groups_sem);
2345 head = &space_info->block_groups;
2347 chunk_alloc_done = 1;
2348 } else if (!allowed_chunk_alloc) {
2349 space_info->force_alloc = 1;
2354 } else if (cur == head) {
2358 block_group = list_entry(cur, struct btrfs_block_group_cache,
2360 search_start = block_group->key.objectid;
2364 /* we found what we needed */
2365 if (ins->objectid) {
2366 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2367 trans->block_group = block_group;
2370 *last_ptr = ins->objectid + ins->offset;
2376 up_read(&space_info->groups_sem);
2380 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2382 struct btrfs_block_group_cache *cache;
2383 struct list_head *l;
2385 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
2386 info->total_bytes - info->bytes_used - info->bytes_pinned -
2387 info->bytes_reserved, (info->full) ? "" : "not ");
2389 down_read(&info->groups_sem);
2390 list_for_each(l, &info->block_groups) {
2391 cache = list_entry(l, struct btrfs_block_group_cache, list);
2392 spin_lock(&cache->lock);
2393 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
2394 "%Lu pinned %Lu reserved\n",
2395 cache->key.objectid, cache->key.offset,
2396 btrfs_block_group_used(&cache->item),
2397 cache->pinned, cache->reserved);
2398 btrfs_dump_free_space(cache, bytes);
2399 spin_unlock(&cache->lock);
2401 up_read(&info->groups_sem);
2404 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2405 struct btrfs_root *root,
2406 u64 num_bytes, u64 min_alloc_size,
2407 u64 empty_size, u64 hint_byte,
2408 u64 search_end, struct btrfs_key *ins,
2412 u64 search_start = 0;
2414 struct btrfs_fs_info *info = root->fs_info;
2417 alloc_profile = info->avail_data_alloc_bits &
2418 info->data_alloc_profile;
2419 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2420 } else if (root == root->fs_info->chunk_root) {
2421 alloc_profile = info->avail_system_alloc_bits &
2422 info->system_alloc_profile;
2423 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2425 alloc_profile = info->avail_metadata_alloc_bits &
2426 info->metadata_alloc_profile;
2427 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2430 data = reduce_alloc_profile(root, data);
2432 * the only place that sets empty_size is btrfs_realloc_node, which
2433 * is not called recursively on allocations
2435 if (empty_size || root->ref_cows) {
2436 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2437 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2439 BTRFS_BLOCK_GROUP_METADATA |
2440 (info->metadata_alloc_profile &
2441 info->avail_metadata_alloc_bits), 0);
2443 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2444 num_bytes + 2 * 1024 * 1024, data, 0);
2447 WARN_ON(num_bytes < root->sectorsize);
2448 ret = find_free_extent(trans, root, num_bytes, empty_size,
2449 search_start, search_end, hint_byte, ins,
2450 trans->alloc_exclude_start,
2451 trans->alloc_exclude_nr, data);
2453 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2454 num_bytes = num_bytes >> 1;
2455 num_bytes = num_bytes & ~(root->sectorsize - 1);
2456 num_bytes = max(num_bytes, min_alloc_size);
2457 do_chunk_alloc(trans, root->fs_info->extent_root,
2458 num_bytes, data, 1);
2462 struct btrfs_space_info *sinfo;
2464 sinfo = __find_space_info(root->fs_info, data);
2465 printk("allocation failed flags %Lu, wanted %Lu\n",
2467 dump_space_info(sinfo, num_bytes);
2474 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2476 struct btrfs_block_group_cache *cache;
2478 cache = btrfs_lookup_block_group(root->fs_info, start);
2480 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
2483 btrfs_add_free_space(cache, start, len);
2484 update_reserved_extents(root, start, len, 0);
2488 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2489 struct btrfs_root *root,
2490 u64 num_bytes, u64 min_alloc_size,
2491 u64 empty_size, u64 hint_byte,
2492 u64 search_end, struct btrfs_key *ins,
2496 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2497 empty_size, hint_byte, search_end, ins,
2499 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2503 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2504 struct btrfs_root *root, u64 parent,
2505 u64 root_objectid, u64 ref_generation,
2506 u64 owner, struct btrfs_key *ins)
2512 u64 num_bytes = ins->offset;
2514 struct btrfs_fs_info *info = root->fs_info;
2515 struct btrfs_root *extent_root = info->extent_root;
2516 struct btrfs_extent_item *extent_item;
2517 struct btrfs_extent_ref *ref;
2518 struct btrfs_path *path;
2519 struct btrfs_key keys[2];
2522 parent = ins->objectid;
2524 /* block accounting for super block */
2525 spin_lock_irq(&info->delalloc_lock);
2526 super_used = btrfs_super_bytes_used(&info->super_copy);
2527 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2528 spin_unlock_irq(&info->delalloc_lock);
2530 /* block accounting for root item */
2531 root_used = btrfs_root_used(&root->root_item);
2532 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2534 if (root == extent_root) {
2535 struct pending_extent_op *extent_op;
2537 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2540 extent_op->type = PENDING_EXTENT_INSERT;
2541 extent_op->bytenr = ins->objectid;
2542 extent_op->num_bytes = ins->offset;
2543 extent_op->parent = parent;
2544 extent_op->orig_parent = 0;
2545 extent_op->generation = ref_generation;
2546 extent_op->orig_generation = 0;
2547 extent_op->level = (int)owner;
2549 mutex_lock(&root->fs_info->extent_ins_mutex);
2550 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2551 ins->objectid + ins->offset - 1,
2552 EXTENT_WRITEBACK, GFP_NOFS);
2553 set_state_private(&root->fs_info->extent_ins,
2554 ins->objectid, (unsigned long)extent_op);
2555 mutex_unlock(&root->fs_info->extent_ins_mutex);
2559 memcpy(&keys[0], ins, sizeof(*ins));
2560 keys[1].objectid = ins->objectid;
2561 keys[1].type = BTRFS_EXTENT_REF_KEY;
2562 keys[1].offset = parent;
2563 sizes[0] = sizeof(*extent_item);
2564 sizes[1] = sizeof(*ref);
2566 path = btrfs_alloc_path();
2569 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2573 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2574 struct btrfs_extent_item);
2575 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2576 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2577 struct btrfs_extent_ref);
2579 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2580 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2581 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2582 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
2584 btrfs_mark_buffer_dirty(path->nodes[0]);
2586 trans->alloc_exclude_start = 0;
2587 trans->alloc_exclude_nr = 0;
2588 btrfs_free_path(path);
2589 finish_current_insert(trans, extent_root, 0);
2590 pending_ret = del_pending_extents(trans, extent_root, 0);
2600 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
2602 printk("update block group failed for %Lu %Lu\n",
2603 ins->objectid, ins->offset);
2610 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2611 struct btrfs_root *root, u64 parent,
2612 u64 root_objectid, u64 ref_generation,
2613 u64 owner, struct btrfs_key *ins)
2617 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2619 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2620 ref_generation, owner, ins);
2621 update_reserved_extents(root, ins->objectid, ins->offset, 0);
2626 * this is used by the tree logging recovery code. It records that
2627 * an extent has been allocated and makes sure to clear the free
2628 * space cache bits as well
2630 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2631 struct btrfs_root *root, u64 parent,
2632 u64 root_objectid, u64 ref_generation,
2633 u64 owner, struct btrfs_key *ins)
2636 struct btrfs_block_group_cache *block_group;
2638 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2639 mutex_lock(&block_group->alloc_mutex);
2640 cache_block_group(root, block_group);
2642 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
2644 mutex_unlock(&block_group->alloc_mutex);
2646 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2647 ref_generation, owner, ins);
2652 * finds a free extent and does all the dirty work required for allocation
2653 * returns the key for the extent through ins, and a tree buffer for
2654 * the first block of the extent through buf.
2656 * returns 0 if everything worked, non-zero otherwise.
2658 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2659 struct btrfs_root *root,
2660 u64 num_bytes, u64 parent, u64 min_alloc_size,
2661 u64 root_objectid, u64 ref_generation,
2662 u64 owner_objectid, u64 empty_size, u64 hint_byte,
2663 u64 search_end, struct btrfs_key *ins, u64 data)
2667 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2668 min_alloc_size, empty_size, hint_byte,
2669 search_end, ins, data);
2671 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2672 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2673 root_objectid, ref_generation,
2674 owner_objectid, ins);
2678 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2683 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2684 struct btrfs_root *root,
2685 u64 bytenr, u32 blocksize)
2687 struct extent_buffer *buf;
2689 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2691 return ERR_PTR(-ENOMEM);
2692 btrfs_set_header_generation(buf, trans->transid);
2693 btrfs_tree_lock(buf);
2694 clean_tree_block(trans, root, buf);
2695 btrfs_set_buffer_uptodate(buf);
2696 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2697 set_extent_dirty(&root->dirty_log_pages, buf->start,
2698 buf->start + buf->len - 1, GFP_NOFS);
2700 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2701 buf->start + buf->len - 1, GFP_NOFS);
2703 trans->blocks_used++;
2708 * helper function to allocate a block for a given tree
2709 * returns the tree buffer or NULL.
2711 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
2712 struct btrfs_root *root,
2713 u32 blocksize, u64 parent,
2720 struct btrfs_key ins;
2722 struct extent_buffer *buf;
2724 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
2725 root_objectid, ref_generation, level,
2726 empty_size, hint, (u64)-1, &ins, 0);
2729 return ERR_PTR(ret);
2732 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
2736 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2737 struct btrfs_root *root, struct extent_buffer *leaf)
2740 u64 leaf_generation;
2741 struct btrfs_key key;
2742 struct btrfs_file_extent_item *fi;
2747 BUG_ON(!btrfs_is_leaf(leaf));
2748 nritems = btrfs_header_nritems(leaf);
2749 leaf_owner = btrfs_header_owner(leaf);
2750 leaf_generation = btrfs_header_generation(leaf);
2752 for (i = 0; i < nritems; i++) {
2756 btrfs_item_key_to_cpu(leaf, &key, i);
2757 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2759 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
2760 if (btrfs_file_extent_type(leaf, fi) ==
2761 BTRFS_FILE_EXTENT_INLINE)
2764 * FIXME make sure to insert a trans record that
2765 * repeats the snapshot del on crash
2767 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2768 if (disk_bytenr == 0)
2771 ret = __btrfs_free_extent(trans, root, disk_bytenr,
2772 btrfs_file_extent_disk_num_bytes(leaf, fi),
2773 leaf->start, leaf_owner, leaf_generation,
2777 atomic_inc(&root->fs_info->throttle_gen);
2778 wake_up(&root->fs_info->transaction_throttle);
2784 static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2785 struct btrfs_root *root,
2786 struct btrfs_leaf_ref *ref)
2790 struct btrfs_extent_info *info = ref->extents;
2792 for (i = 0; i < ref->nritems; i++) {
2793 ret = __btrfs_free_extent(trans, root, info->bytenr,
2794 info->num_bytes, ref->bytenr,
2795 ref->owner, ref->generation,
2798 atomic_inc(&root->fs_info->throttle_gen);
2799 wake_up(&root->fs_info->transaction_throttle);
2809 int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2814 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
2817 #if 0 // some debugging code in case we see problems here
2818 /* if the refs count is one, it won't get increased again. But
2819 * if the ref count is > 1, someone may be decreasing it at
2820 * the same time we are.
2823 struct extent_buffer *eb = NULL;
2824 eb = btrfs_find_create_tree_block(root, start, len);
2826 btrfs_tree_lock(eb);
2828 mutex_lock(&root->fs_info->alloc_mutex);
2829 ret = lookup_extent_ref(NULL, root, start, len, refs);
2831 mutex_unlock(&root->fs_info->alloc_mutex);
2834 btrfs_tree_unlock(eb);
2835 free_extent_buffer(eb);
2838 printk("block %llu went down to one during drop_snap\n",
2839 (unsigned long long)start);
2850 * helper function for drop_snapshot, this walks down the tree dropping ref
2851 * counts as it goes.
2853 static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2854 struct btrfs_root *root,
2855 struct btrfs_path *path, int *level)
2861 struct extent_buffer *next;
2862 struct extent_buffer *cur;
2863 struct extent_buffer *parent;
2864 struct btrfs_leaf_ref *ref;
2869 WARN_ON(*level < 0);
2870 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2871 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
2872 path->nodes[*level]->len, &refs);
2878 * walk down to the last node level and free all the leaves
2880 while(*level >= 0) {
2881 WARN_ON(*level < 0);
2882 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2883 cur = path->nodes[*level];
2885 if (btrfs_header_level(cur) != *level)
2888 if (path->slots[*level] >=
2889 btrfs_header_nritems(cur))
2892 ret = btrfs_drop_leaf_ref(trans, root, cur);
2896 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2897 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
2898 blocksize = btrfs_level_size(root, *level - 1);
2900 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
2903 parent = path->nodes[*level];
2904 root_owner = btrfs_header_owner(parent);
2905 root_gen = btrfs_header_generation(parent);
2906 path->slots[*level]++;
2908 ret = __btrfs_free_extent(trans, root, bytenr,
2909 blocksize, parent->start,
2910 root_owner, root_gen,
2914 atomic_inc(&root->fs_info->throttle_gen);
2915 wake_up(&root->fs_info->transaction_throttle);
2921 * at this point, we have a single ref, and since the
2922 * only place referencing this extent is a dead root
2923 * the reference count should never go higher.
2924 * So, we don't need to check it again
2927 ref = btrfs_lookup_leaf_ref(root, bytenr);
2928 if (ref && ref->generation != ptr_gen) {
2929 btrfs_free_leaf_ref(root, ref);
2933 ret = cache_drop_leaf_ref(trans, root, ref);
2935 btrfs_remove_leaf_ref(root, ref);
2936 btrfs_free_leaf_ref(root, ref);
2940 if (printk_ratelimit()) {
2941 printk("leaf ref miss for bytenr %llu\n",
2942 (unsigned long long)bytenr);
2945 next = btrfs_find_tree_block(root, bytenr, blocksize);
2946 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
2947 free_extent_buffer(next);
2949 next = read_tree_block(root, bytenr, blocksize,
2954 * this is a debugging check and can go away
2955 * the ref should never go all the way down to 1
2958 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2964 WARN_ON(*level <= 0);
2965 if (path->nodes[*level-1])
2966 free_extent_buffer(path->nodes[*level-1]);
2967 path->nodes[*level-1] = next;
2968 *level = btrfs_header_level(next);
2969 path->slots[*level] = 0;
2973 WARN_ON(*level < 0);
2974 WARN_ON(*level >= BTRFS_MAX_LEVEL);
2976 if (path->nodes[*level] == root->node) {
2977 parent = path->nodes[*level];
2978 bytenr = path->nodes[*level]->start;
2980 parent = path->nodes[*level + 1];
2981 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
2984 blocksize = btrfs_level_size(root, *level);
2985 root_owner = btrfs_header_owner(parent);
2986 root_gen = btrfs_header_generation(parent);
2988 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
2989 parent->start, root_owner, root_gen,
2991 free_extent_buffer(path->nodes[*level]);
2992 path->nodes[*level] = NULL;
3001 * helper function for drop_subtree, this function is similar to
3002 * walk_down_tree. The main difference is that it checks reference
3003 * counts while tree blocks are locked.
3005 static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3006 struct btrfs_root *root,
3007 struct btrfs_path *path, int *level)
3009 struct extent_buffer *next;
3010 struct extent_buffer *cur;
3011 struct extent_buffer *parent;
3018 cur = path->nodes[*level];
3019 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3025 while (*level >= 0) {
3026 cur = path->nodes[*level];
3028 ret = btrfs_drop_leaf_ref(trans, root, cur);
3030 clean_tree_block(trans, root, cur);
3033 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3034 clean_tree_block(trans, root, cur);
3038 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3039 blocksize = btrfs_level_size(root, *level - 1);
3040 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3042 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3043 btrfs_tree_lock(next);
3045 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3049 parent = path->nodes[*level];
3050 ret = btrfs_free_extent(trans, root, bytenr,
3051 blocksize, parent->start,
3052 btrfs_header_owner(parent),
3053 btrfs_header_generation(parent),
3056 path->slots[*level]++;
3057 btrfs_tree_unlock(next);
3058 free_extent_buffer(next);
3062 *level = btrfs_header_level(next);
3063 path->nodes[*level] = next;
3064 path->slots[*level] = 0;
3065 path->locks[*level] = 1;
3069 parent = path->nodes[*level + 1];
3070 bytenr = path->nodes[*level]->start;
3071 blocksize = path->nodes[*level]->len;
3073 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3074 parent->start, btrfs_header_owner(parent),
3075 btrfs_header_generation(parent), *level, 1);
3078 if (path->locks[*level]) {
3079 btrfs_tree_unlock(path->nodes[*level]);
3080 path->locks[*level] = 0;
3082 free_extent_buffer(path->nodes[*level]);
3083 path->nodes[*level] = NULL;
3090 * helper for dropping snapshots. This walks back up the tree in the path
3091 * to find the first node higher up where we haven't yet gone through
3094 static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3095 struct btrfs_root *root,
3096 struct btrfs_path *path,
3097 int *level, int max_level)
3101 struct btrfs_root_item *root_item = &root->root_item;
3106 for (i = *level; i < max_level && path->nodes[i]; i++) {
3107 slot = path->slots[i];
3108 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3109 struct extent_buffer *node;
3110 struct btrfs_disk_key disk_key;
3111 node = path->nodes[i];
3114 WARN_ON(*level == 0);
3115 btrfs_node_key(node, &disk_key, path->slots[i]);
3116 memcpy(&root_item->drop_progress,
3117 &disk_key, sizeof(disk_key));
3118 root_item->drop_level = i;
3121 struct extent_buffer *parent;
3122 if (path->nodes[*level] == root->node)
3123 parent = path->nodes[*level];
3125 parent = path->nodes[*level + 1];
3127 root_owner = btrfs_header_owner(parent);
3128 root_gen = btrfs_header_generation(parent);
3130 clean_tree_block(trans, root, path->nodes[*level]);
3131 ret = btrfs_free_extent(trans, root,
3132 path->nodes[*level]->start,
3133 path->nodes[*level]->len,
3134 parent->start, root_owner,
3135 root_gen, *level, 1);
3137 if (path->locks[*level]) {
3138 btrfs_tree_unlock(path->nodes[*level]);
3139 path->locks[*level] = 0;
3141 free_extent_buffer(path->nodes[*level]);
3142 path->nodes[*level] = NULL;
3150 * drop the reference count on the tree rooted at 'snap'. This traverses
3151 * the tree freeing any blocks that have a ref count of zero after being
3154 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3160 struct btrfs_path *path;
3163 struct btrfs_root_item *root_item = &root->root_item;
3165 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3166 path = btrfs_alloc_path();
3169 level = btrfs_header_level(root->node);
3171 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3172 path->nodes[level] = root->node;
3173 extent_buffer_get(root->node);
3174 path->slots[level] = 0;
3176 struct btrfs_key key;
3177 struct btrfs_disk_key found_key;
3178 struct extent_buffer *node;
3180 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3181 level = root_item->drop_level;
3182 path->lowest_level = level;
3183 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3188 node = path->nodes[level];
3189 btrfs_node_key(node, &found_key, path->slots[level]);
3190 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3191 sizeof(found_key)));
3193 * unlock our path, this is safe because only this
3194 * function is allowed to delete this snapshot
3196 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3197 if (path->nodes[i] && path->locks[i]) {
3199 btrfs_tree_unlock(path->nodes[i]);
3204 wret = walk_down_tree(trans, root, path, &level);
3210 wret = walk_up_tree(trans, root, path, &level,
3216 if (trans->transaction->in_commit) {
3220 atomic_inc(&root->fs_info->throttle_gen);
3221 wake_up(&root->fs_info->transaction_throttle);
3223 for (i = 0; i <= orig_level; i++) {
3224 if (path->nodes[i]) {
3225 free_extent_buffer(path->nodes[i]);
3226 path->nodes[i] = NULL;
3230 btrfs_free_path(path);
3234 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root,
3236 struct extent_buffer *node,
3237 struct extent_buffer *parent)
3239 struct btrfs_path *path;
3245 path = btrfs_alloc_path();
3248 BUG_ON(!btrfs_tree_locked(parent));
3249 parent_level = btrfs_header_level(parent);
3250 extent_buffer_get(parent);
3251 path->nodes[parent_level] = parent;
3252 path->slots[parent_level] = btrfs_header_nritems(parent);
3254 BUG_ON(!btrfs_tree_locked(node));
3255 level = btrfs_header_level(node);
3256 extent_buffer_get(node);
3257 path->nodes[level] = node;
3258 path->slots[level] = 0;
3261 wret = walk_down_subtree(trans, root, path, &level);
3267 wret = walk_up_tree(trans, root, path, &level, parent_level);
3274 btrfs_free_path(path);
3278 static unsigned long calc_ra(unsigned long start, unsigned long last,
3281 return min(last, start + nr - 1);
3284 static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3289 unsigned long first_index;
3290 unsigned long last_index;
3293 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3294 struct file_ra_state *ra;
3295 struct btrfs_ordered_extent *ordered;
3296 unsigned int total_read = 0;
3297 unsigned int total_dirty = 0;
3300 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3302 mutex_lock(&inode->i_mutex);
3303 first_index = start >> PAGE_CACHE_SHIFT;
3304 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3306 /* make sure the dirty trick played by the caller work */
3307 ret = invalidate_inode_pages2_range(inode->i_mapping,
3308 first_index, last_index);
3312 file_ra_state_init(ra, inode->i_mapping);
3314 for (i = first_index ; i <= last_index; i++) {
3315 if (total_read % ra->ra_pages == 0) {
3316 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3317 calc_ra(i, last_index, ra->ra_pages));
3321 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3323 page = grab_cache_page(inode->i_mapping, i);
3328 if (!PageUptodate(page)) {
3329 btrfs_readpage(NULL, page);
3331 if (!PageUptodate(page)) {
3333 page_cache_release(page);
3338 wait_on_page_writeback(page);
3340 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3341 page_end = page_start + PAGE_CACHE_SIZE - 1;
3342 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3344 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3346 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3348 page_cache_release(page);
3349 btrfs_start_ordered_extent(inode, ordered, 1);
3350 btrfs_put_ordered_extent(ordered);
3353 set_page_extent_mapped(page);
3355 btrfs_set_extent_delalloc(inode, page_start, page_end);
3356 if (i == first_index)
3357 set_extent_bits(io_tree, page_start, page_end,
3358 EXTENT_BOUNDARY, GFP_NOFS);
3360 set_page_dirty(page);
3363 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3365 page_cache_release(page);
3370 mutex_unlock(&inode->i_mutex);
3371 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3375 static int noinline relocate_data_extent(struct inode *reloc_inode,
3376 struct btrfs_key *extent_key,
3379 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3380 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3381 struct extent_map *em;
3383 em = alloc_extent_map(GFP_NOFS);
3384 BUG_ON(!em || IS_ERR(em));
3386 em->start = extent_key->objectid - offset;
3387 em->len = extent_key->offset;
3388 em->block_len = extent_key->offset;
3389 em->block_start = extent_key->objectid;
3390 em->bdev = root->fs_info->fs_devices->latest_bdev;
3391 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3393 /* setup extent map to cheat btrfs_readpage */
3394 mutex_lock(&BTRFS_I(reloc_inode)->extent_mutex);
3397 spin_lock(&em_tree->lock);
3398 ret = add_extent_mapping(em_tree, em);
3399 spin_unlock(&em_tree->lock);
3400 if (ret != -EEXIST) {
3401 free_extent_map(em);
3404 btrfs_drop_extent_cache(reloc_inode, em->start,
3405 em->start + em->len - 1, 0);
3407 mutex_unlock(&BTRFS_I(reloc_inode)->extent_mutex);
3409 return relocate_inode_pages(reloc_inode, extent_key->objectid - offset,
3410 extent_key->offset);
3413 struct btrfs_ref_path {
3415 u64 nodes[BTRFS_MAX_LEVEL];
3417 u64 root_generation;
3424 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3425 u64 new_nodes[BTRFS_MAX_LEVEL];
3428 struct disk_extent {
3439 static int is_cowonly_root(u64 root_objectid)
3441 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3442 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3443 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3444 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3445 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3450 static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3451 struct btrfs_root *extent_root,
3452 struct btrfs_ref_path *ref_path,
3455 struct extent_buffer *leaf;
3456 struct btrfs_path *path;
3457 struct btrfs_extent_ref *ref;
3458 struct btrfs_key key;
3459 struct btrfs_key found_key;
3465 path = btrfs_alloc_path();
3470 ref_path->lowest_level = -1;
3471 ref_path->current_level = -1;
3472 ref_path->shared_level = -1;
3476 level = ref_path->current_level - 1;
3477 while (level >= -1) {
3479 if (level < ref_path->lowest_level)
3483 bytenr = ref_path->nodes[level];
3485 bytenr = ref_path->extent_start;
3487 BUG_ON(bytenr == 0);
3489 parent = ref_path->nodes[level + 1];
3490 ref_path->nodes[level + 1] = 0;
3491 ref_path->current_level = level;
3492 BUG_ON(parent == 0);
3494 key.objectid = bytenr;
3495 key.offset = parent + 1;
3496 key.type = BTRFS_EXTENT_REF_KEY;
3498 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3503 leaf = path->nodes[0];
3504 nritems = btrfs_header_nritems(leaf);
3505 if (path->slots[0] >= nritems) {
3506 ret = btrfs_next_leaf(extent_root, path);
3511 leaf = path->nodes[0];
3514 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3515 if (found_key.objectid == bytenr &&
3516 found_key.type == BTRFS_EXTENT_REF_KEY) {
3517 if (level < ref_path->shared_level)
3518 ref_path->shared_level = level;
3523 btrfs_release_path(extent_root, path);
3524 if (need_resched()) {
3528 /* reached lowest level */
3532 level = ref_path->current_level;
3533 while (level < BTRFS_MAX_LEVEL - 1) {
3536 bytenr = ref_path->nodes[level];
3538 bytenr = ref_path->extent_start;
3540 BUG_ON(bytenr == 0);
3542 key.objectid = bytenr;
3544 key.type = BTRFS_EXTENT_REF_KEY;
3546 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3550 leaf = path->nodes[0];
3551 nritems = btrfs_header_nritems(leaf);
3552 if (path->slots[0] >= nritems) {
3553 ret = btrfs_next_leaf(extent_root, path);
3557 /* the extent was freed by someone */
3558 if (ref_path->lowest_level == level)
3560 btrfs_release_path(extent_root, path);
3563 leaf = path->nodes[0];
3566 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3567 if (found_key.objectid != bytenr ||
3568 found_key.type != BTRFS_EXTENT_REF_KEY) {
3569 /* the extent was freed by someone */
3570 if (ref_path->lowest_level == level) {
3574 btrfs_release_path(extent_root, path);
3578 ref = btrfs_item_ptr(leaf, path->slots[0],
3579 struct btrfs_extent_ref);
3580 ref_objectid = btrfs_ref_objectid(leaf, ref);
3581 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3583 level = (int)ref_objectid;
3584 BUG_ON(level >= BTRFS_MAX_LEVEL);
3585 ref_path->lowest_level = level;
3586 ref_path->current_level = level;
3587 ref_path->nodes[level] = bytenr;
3589 WARN_ON(ref_objectid != level);
3592 WARN_ON(level != -1);
3596 if (ref_path->lowest_level == level) {
3597 ref_path->owner_objectid = ref_objectid;
3598 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3602 * the block is tree root or the block isn't in reference
3605 if (found_key.objectid == found_key.offset ||
3606 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3607 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3608 ref_path->root_generation =
3609 btrfs_ref_generation(leaf, ref);
3611 /* special reference from the tree log */
3612 ref_path->nodes[0] = found_key.offset;
3613 ref_path->current_level = 0;
3620 BUG_ON(ref_path->nodes[level] != 0);
3621 ref_path->nodes[level] = found_key.offset;
3622 ref_path->current_level = level;
3625 * the reference was created in the running transaction,
3626 * no need to continue walking up.
3628 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3629 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3630 ref_path->root_generation =
3631 btrfs_ref_generation(leaf, ref);
3636 btrfs_release_path(extent_root, path);
3637 if (need_resched()) {
3641 /* reached max tree level, but no tree root found. */
3644 btrfs_free_path(path);
3648 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3649 struct btrfs_root *extent_root,
3650 struct btrfs_ref_path *ref_path,
3653 memset(ref_path, 0, sizeof(*ref_path));
3654 ref_path->extent_start = extent_start;
3656 return __next_ref_path(trans, extent_root, ref_path, 1);
3659 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3660 struct btrfs_root *extent_root,
3661 struct btrfs_ref_path *ref_path)
3663 return __next_ref_path(trans, extent_root, ref_path, 0);
3666 static int noinline get_new_locations(struct inode *reloc_inode,
3667 struct btrfs_key *extent_key,
3668 u64 offset, int no_fragment,
3669 struct disk_extent **extents,
3672 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3673 struct btrfs_path *path;
3674 struct btrfs_file_extent_item *fi;
3675 struct extent_buffer *leaf;
3676 struct disk_extent *exts = *extents;
3677 struct btrfs_key found_key;
3682 int max = *nr_extents;
3685 WARN_ON(!no_fragment && *extents);
3688 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3693 path = btrfs_alloc_path();
3696 cur_pos = extent_key->objectid - offset;
3697 last_byte = extent_key->objectid + extent_key->offset;
3698 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3708 leaf = path->nodes[0];
3709 nritems = btrfs_header_nritems(leaf);
3710 if (path->slots[0] >= nritems) {
3711 ret = btrfs_next_leaf(root, path);
3716 leaf = path->nodes[0];
3719 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3720 if (found_key.offset != cur_pos ||
3721 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3722 found_key.objectid != reloc_inode->i_ino)
3725 fi = btrfs_item_ptr(leaf, path->slots[0],
3726 struct btrfs_file_extent_item);
3727 if (btrfs_file_extent_type(leaf, fi) !=
3728 BTRFS_FILE_EXTENT_REG ||
3729 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3733 struct disk_extent *old = exts;
3735 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3736 memcpy(exts, old, sizeof(*exts) * nr);
3737 if (old != *extents)
3741 exts[nr].disk_bytenr =
3742 btrfs_file_extent_disk_bytenr(leaf, fi);
3743 exts[nr].disk_num_bytes =
3744 btrfs_file_extent_disk_num_bytes(leaf, fi);
3745 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3746 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3747 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
3748 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
3749 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
3750 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
3752 WARN_ON(exts[nr].offset > 0);
3753 WARN_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
3755 cur_pos += exts[nr].num_bytes;
3758 if (cur_pos + offset >= last_byte)
3768 WARN_ON(cur_pos + offset > last_byte);
3769 if (cur_pos + offset < last_byte) {
3775 btrfs_free_path(path);
3777 if (exts != *extents)
3786 static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3787 struct btrfs_root *root,
3788 struct btrfs_path *path,
3789 struct btrfs_key *extent_key,
3790 struct btrfs_key *leaf_key,
3791 struct btrfs_ref_path *ref_path,
3792 struct disk_extent *new_extents,
3795 struct extent_buffer *leaf;
3796 struct btrfs_file_extent_item *fi;
3797 struct inode *inode = NULL;
3798 struct btrfs_key key;
3806 int extent_locked = 0;
3809 memcpy(&key, leaf_key, sizeof(key));
3810 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
3811 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3812 if (key.objectid < ref_path->owner_objectid ||
3813 (key.objectid == ref_path->owner_objectid &&
3814 key.type < BTRFS_EXTENT_DATA_KEY)) {
3815 key.objectid = ref_path->owner_objectid;
3816 key.type = BTRFS_EXTENT_DATA_KEY;
3822 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3826 leaf = path->nodes[0];
3827 nritems = btrfs_header_nritems(leaf);
3829 if (extent_locked && ret > 0) {
3831 * the file extent item was modified by someone
3832 * before the extent got locked.
3834 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
3835 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3836 lock_end, GFP_NOFS);
3840 if (path->slots[0] >= nritems) {
3841 if (++nr_scaned > 2)
3844 BUG_ON(extent_locked);
3845 ret = btrfs_next_leaf(root, path);
3850 leaf = path->nodes[0];
3851 nritems = btrfs_header_nritems(leaf);
3854 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3856 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3857 if ((key.objectid > ref_path->owner_objectid) ||
3858 (key.objectid == ref_path->owner_objectid &&
3859 key.type > BTRFS_EXTENT_DATA_KEY) ||
3860 (key.offset >= first_pos + extent_key->offset))
3864 if (inode && key.objectid != inode->i_ino) {
3865 BUG_ON(extent_locked);
3866 btrfs_release_path(root, path);
3867 mutex_unlock(&inode->i_mutex);
3873 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3878 fi = btrfs_item_ptr(leaf, path->slots[0],
3879 struct btrfs_file_extent_item);
3880 if ((btrfs_file_extent_type(leaf, fi) !=
3881 BTRFS_FILE_EXTENT_REG) ||
3882 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3883 extent_key->objectid)) {
3889 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3890 ext_offset = btrfs_file_extent_offset(leaf, fi);
3892 if (first_pos > key.offset - ext_offset)
3893 first_pos = key.offset - ext_offset;
3895 if (!extent_locked) {
3896 lock_start = key.offset;
3897 lock_end = lock_start + num_bytes - 1;
3899 BUG_ON(lock_start != key.offset);
3900 BUG_ON(lock_end - lock_start + 1 < num_bytes);
3904 btrfs_release_path(root, path);
3906 inode = btrfs_iget_locked(root->fs_info->sb,
3907 key.objectid, root);
3908 if (inode->i_state & I_NEW) {
3909 BTRFS_I(inode)->root = root;
3910 BTRFS_I(inode)->location.objectid =
3912 BTRFS_I(inode)->location.type =
3913 BTRFS_INODE_ITEM_KEY;
3914 BTRFS_I(inode)->location.offset = 0;
3915 btrfs_read_locked_inode(inode);
3916 unlock_new_inode(inode);
3919 * some code call btrfs_commit_transaction while
3920 * holding the i_mutex, so we can't use mutex_lock
3923 if (is_bad_inode(inode) ||
3924 !mutex_trylock(&inode->i_mutex)) {
3927 key.offset = (u64)-1;
3932 if (!extent_locked) {
3933 struct btrfs_ordered_extent *ordered;
3935 btrfs_release_path(root, path);
3937 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3938 lock_end, GFP_NOFS);
3939 ordered = btrfs_lookup_first_ordered_extent(inode,
3942 ordered->file_offset <= lock_end &&
3943 ordered->file_offset + ordered->len > lock_start) {
3944 unlock_extent(&BTRFS_I(inode)->io_tree,
3945 lock_start, lock_end, GFP_NOFS);
3946 btrfs_start_ordered_extent(inode, ordered, 1);
3947 btrfs_put_ordered_extent(ordered);
3948 key.offset += num_bytes;
3952 btrfs_put_ordered_extent(ordered);
3954 mutex_lock(&BTRFS_I(inode)->extent_mutex);
3959 if (nr_extents == 1) {
3960 /* update extent pointer in place */
3961 btrfs_set_file_extent_generation(leaf, fi,
3963 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3964 new_extents[0].disk_bytenr);
3965 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3966 new_extents[0].disk_num_bytes);
3967 btrfs_set_file_extent_ram_bytes(leaf, fi,
3968 new_extents[0].ram_bytes);
3969 ext_offset += new_extents[0].offset;
3970 btrfs_set_file_extent_offset(leaf, fi, ext_offset);
3971 btrfs_mark_buffer_dirty(leaf);
3973 btrfs_drop_extent_cache(inode, key.offset,
3974 key.offset + num_bytes - 1, 0);
3976 ret = btrfs_inc_extent_ref(trans, root,
3977 new_extents[0].disk_bytenr,
3978 new_extents[0].disk_num_bytes,
3980 root->root_key.objectid,
3985 ret = btrfs_free_extent(trans, root,
3986 extent_key->objectid,
3989 btrfs_header_owner(leaf),
3990 btrfs_header_generation(leaf),
3994 btrfs_release_path(root, path);
3995 key.offset += num_bytes;
4001 * drop old extent pointer at first, then insert the
4002 * new pointers one bye one
4004 btrfs_release_path(root, path);
4005 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4006 key.offset + num_bytes,
4007 key.offset, &alloc_hint);
4010 for (i = 0; i < nr_extents; i++) {
4011 if (ext_offset >= new_extents[i].num_bytes) {
4012 ext_offset -= new_extents[i].num_bytes;
4015 extent_len = min(new_extents[i].num_bytes -
4016 ext_offset, num_bytes);
4018 ret = btrfs_insert_empty_item(trans, root,
4023 leaf = path->nodes[0];
4024 fi = btrfs_item_ptr(leaf, path->slots[0],
4025 struct btrfs_file_extent_item);
4026 btrfs_set_file_extent_generation(leaf, fi,
4028 btrfs_set_file_extent_type(leaf, fi,
4029 BTRFS_FILE_EXTENT_REG);
4030 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4031 new_extents[i].disk_bytenr);
4032 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4033 new_extents[i].disk_num_bytes);
4034 btrfs_set_file_extent_ram_bytes(leaf, fi,
4035 new_extents[i].ram_bytes);
4037 btrfs_set_file_extent_compression(leaf, fi,
4038 new_extents[i].compression);
4039 btrfs_set_file_extent_encryption(leaf, fi,
4040 new_extents[i].encryption);
4041 btrfs_set_file_extent_other_encoding(leaf, fi,
4042 new_extents[i].other_encoding);
4044 btrfs_set_file_extent_num_bytes(leaf, fi,
4046 ext_offset += new_extents[i].offset;
4047 btrfs_set_file_extent_offset(leaf, fi,
4049 btrfs_mark_buffer_dirty(leaf);
4051 btrfs_drop_extent_cache(inode, key.offset,
4052 key.offset + extent_len - 1, 0);
4054 ret = btrfs_inc_extent_ref(trans, root,
4055 new_extents[i].disk_bytenr,
4056 new_extents[i].disk_num_bytes,
4058 root->root_key.objectid,
4059 trans->transid, key.objectid);
4061 btrfs_release_path(root, path);
4063 inode_add_bytes(inode, extent_len);
4066 num_bytes -= extent_len;
4067 key.offset += extent_len;
4072 BUG_ON(i >= nr_extents);
4075 if (extent_locked) {
4076 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4077 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4078 lock_end, GFP_NOFS);
4082 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4083 key.offset >= first_pos + extent_key->offset)
4090 btrfs_release_path(root, path);
4092 mutex_unlock(&inode->i_mutex);
4093 if (extent_locked) {
4094 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4095 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4096 lock_end, GFP_NOFS);
4103 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4104 struct btrfs_root *root,
4105 struct extent_buffer *buf, u64 orig_start)
4110 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4111 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4113 level = btrfs_header_level(buf);
4115 struct btrfs_leaf_ref *ref;
4116 struct btrfs_leaf_ref *orig_ref;
4118 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4122 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4124 btrfs_free_leaf_ref(root, orig_ref);
4128 ref->nritems = orig_ref->nritems;
4129 memcpy(ref->extents, orig_ref->extents,
4130 sizeof(ref->extents[0]) * ref->nritems);
4132 btrfs_free_leaf_ref(root, orig_ref);
4134 ref->root_gen = trans->transid;
4135 ref->bytenr = buf->start;
4136 ref->owner = btrfs_header_owner(buf);
4137 ref->generation = btrfs_header_generation(buf);
4138 ret = btrfs_add_leaf_ref(root, ref, 0);
4140 btrfs_free_leaf_ref(root, ref);
4145 static int noinline invalidate_extent_cache(struct btrfs_root *root,
4146 struct extent_buffer *leaf,
4147 struct btrfs_block_group_cache *group,
4148 struct btrfs_root *target_root)
4150 struct btrfs_key key;
4151 struct inode *inode = NULL;
4152 struct btrfs_file_extent_item *fi;
4154 u64 skip_objectid = 0;
4158 nritems = btrfs_header_nritems(leaf);
4159 for (i = 0; i < nritems; i++) {
4160 btrfs_item_key_to_cpu(leaf, &key, i);
4161 if (key.objectid == skip_objectid ||
4162 key.type != BTRFS_EXTENT_DATA_KEY)
4164 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4165 if (btrfs_file_extent_type(leaf, fi) ==
4166 BTRFS_FILE_EXTENT_INLINE)
4168 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4170 if (!inode || inode->i_ino != key.objectid) {
4172 inode = btrfs_ilookup(target_root->fs_info->sb,
4173 key.objectid, target_root, 1);
4176 skip_objectid = key.objectid;
4179 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4181 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4182 key.offset + num_bytes - 1, GFP_NOFS);
4183 mutex_lock(&BTRFS_I(inode)->extent_mutex);
4184 btrfs_drop_extent_cache(inode, key.offset,
4185 key.offset + num_bytes - 1, 1);
4186 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
4187 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4188 key.offset + num_bytes - 1, GFP_NOFS);
4195 static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4196 struct btrfs_root *root,
4197 struct extent_buffer *leaf,
4198 struct btrfs_block_group_cache *group,
4199 struct inode *reloc_inode)
4201 struct btrfs_key key;
4202 struct btrfs_key extent_key;
4203 struct btrfs_file_extent_item *fi;
4204 struct btrfs_leaf_ref *ref;
4205 struct disk_extent *new_extent;
4214 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4215 BUG_ON(!new_extent);
4217 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4221 nritems = btrfs_header_nritems(leaf);
4222 for (i = 0; i < nritems; i++) {
4223 btrfs_item_key_to_cpu(leaf, &key, i);
4224 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4226 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4227 if (btrfs_file_extent_type(leaf, fi) ==
4228 BTRFS_FILE_EXTENT_INLINE)
4230 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4231 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4236 if (bytenr >= group->key.objectid + group->key.offset ||
4237 bytenr + num_bytes <= group->key.objectid)
4240 extent_key.objectid = bytenr;
4241 extent_key.offset = num_bytes;
4242 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4244 ret = get_new_locations(reloc_inode, &extent_key,
4245 group->key.objectid, 1,
4246 &new_extent, &nr_extent);
4251 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4252 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4253 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4254 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4256 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
4257 btrfs_set_file_extent_ram_bytes(leaf, fi,
4258 new_extent->ram_bytes);
4259 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4260 new_extent->disk_bytenr);
4261 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4262 new_extent->disk_num_bytes);
4263 new_extent->offset += btrfs_file_extent_offset(leaf, fi);
4264 btrfs_set_file_extent_offset(leaf, fi, new_extent->offset);
4265 btrfs_mark_buffer_dirty(leaf);
4267 ret = btrfs_inc_extent_ref(trans, root,
4268 new_extent->disk_bytenr,
4269 new_extent->disk_num_bytes,
4271 root->root_key.objectid,
4272 trans->transid, key.objectid);
4274 ret = btrfs_free_extent(trans, root,
4275 bytenr, num_bytes, leaf->start,
4276 btrfs_header_owner(leaf),
4277 btrfs_header_generation(leaf),
4283 BUG_ON(ext_index + 1 != ref->nritems);
4284 btrfs_free_leaf_ref(root, ref);
4288 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4289 struct btrfs_root *root)
4291 struct btrfs_root *reloc_root;
4294 if (root->reloc_root) {
4295 reloc_root = root->reloc_root;
4296 root->reloc_root = NULL;
4297 list_add(&reloc_root->dead_list,
4298 &root->fs_info->dead_reloc_roots);
4300 btrfs_set_root_bytenr(&reloc_root->root_item,
4301 reloc_root->node->start);
4302 btrfs_set_root_level(&root->root_item,
4303 btrfs_header_level(reloc_root->node));
4304 memset(&reloc_root->root_item.drop_progress, 0,
4305 sizeof(struct btrfs_disk_key));
4306 reloc_root->root_item.drop_level = 0;
4308 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4309 &reloc_root->root_key,
4310 &reloc_root->root_item);
4316 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4318 struct btrfs_trans_handle *trans;
4319 struct btrfs_root *reloc_root;
4320 struct btrfs_root *prev_root = NULL;
4321 struct list_head dead_roots;
4325 INIT_LIST_HEAD(&dead_roots);
4326 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4328 while (!list_empty(&dead_roots)) {
4329 reloc_root = list_entry(dead_roots.prev,
4330 struct btrfs_root, dead_list);
4331 list_del_init(&reloc_root->dead_list);
4333 BUG_ON(reloc_root->commit_root != NULL);
4335 trans = btrfs_join_transaction(root, 1);
4338 mutex_lock(&root->fs_info->drop_mutex);
4339 ret = btrfs_drop_snapshot(trans, reloc_root);
4342 mutex_unlock(&root->fs_info->drop_mutex);
4344 nr = trans->blocks_used;
4345 ret = btrfs_end_transaction(trans, root);
4347 btrfs_btree_balance_dirty(root, nr);
4350 free_extent_buffer(reloc_root->node);
4352 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4353 &reloc_root->root_key);
4355 mutex_unlock(&root->fs_info->drop_mutex);
4357 nr = trans->blocks_used;
4358 ret = btrfs_end_transaction(trans, root);
4360 btrfs_btree_balance_dirty(root, nr);
4363 prev_root = reloc_root;
4366 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4372 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4374 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4378 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4380 struct btrfs_root *reloc_root;
4381 struct btrfs_trans_handle *trans;
4382 struct btrfs_key location;
4386 mutex_lock(&root->fs_info->tree_reloc_mutex);
4387 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4389 found = !list_empty(&root->fs_info->dead_reloc_roots);
4390 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4393 trans = btrfs_start_transaction(root, 1);
4395 ret = btrfs_commit_transaction(trans, root);
4399 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4400 location.offset = (u64)-1;
4401 location.type = BTRFS_ROOT_ITEM_KEY;
4403 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4404 BUG_ON(!reloc_root);
4405 btrfs_orphan_cleanup(reloc_root);
4409 static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4410 struct btrfs_root *root)
4412 struct btrfs_root *reloc_root;
4413 struct extent_buffer *eb;
4414 struct btrfs_root_item *root_item;
4415 struct btrfs_key root_key;
4418 BUG_ON(!root->ref_cows);
4419 if (root->reloc_root)
4422 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4425 ret = btrfs_copy_root(trans, root, root->commit_root,
4426 &eb, BTRFS_TREE_RELOC_OBJECTID);
4429 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4430 root_key.offset = root->root_key.objectid;
4431 root_key.type = BTRFS_ROOT_ITEM_KEY;
4433 memcpy(root_item, &root->root_item, sizeof(root_item));
4434 btrfs_set_root_refs(root_item, 0);
4435 btrfs_set_root_bytenr(root_item, eb->start);
4436 btrfs_set_root_level(root_item, btrfs_header_level(eb));
4437 btrfs_set_root_generation(root_item, trans->transid);
4439 btrfs_tree_unlock(eb);
4440 free_extent_buffer(eb);
4442 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4443 &root_key, root_item);
4447 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4449 BUG_ON(!reloc_root);
4450 reloc_root->last_trans = trans->transid;
4451 reloc_root->commit_root = NULL;
4452 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4454 root->reloc_root = reloc_root;
4459 * Core function of space balance.
4461 * The idea is using reloc trees to relocate tree blocks in reference
4462 * counted roots. There is one reloc tree for each subvol, and all
4463 * reloc trees share same root key objectid. Reloc trees are snapshots
4464 * of the latest committed roots of subvols (root->commit_root).
4466 * To relocate a tree block referenced by a subvol, there are two steps.
4467 * COW the block through subvol's reloc tree, then update block pointer
4468 * in the subvol to point to the new block. Since all reloc trees share
4469 * same root key objectid, doing special handing for tree blocks owned
4470 * by them is easy. Once a tree block has been COWed in one reloc tree,
4471 * we can use the resulting new block directly when the same block is
4472 * required to COW again through other reloc trees. By this way, relocated
4473 * tree blocks are shared between reloc trees, so they are also shared
4476 static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4477 struct btrfs_root *root,
4478 struct btrfs_path *path,
4479 struct btrfs_key *first_key,
4480 struct btrfs_ref_path *ref_path,
4481 struct btrfs_block_group_cache *group,
4482 struct inode *reloc_inode)
4484 struct btrfs_root *reloc_root;
4485 struct extent_buffer *eb = NULL;
4486 struct btrfs_key *keys;
4490 int lowest_level = 0;
4493 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4494 lowest_level = ref_path->owner_objectid;
4496 if (!root->ref_cows) {
4497 path->lowest_level = lowest_level;
4498 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4500 path->lowest_level = 0;
4501 btrfs_release_path(root, path);
4505 mutex_lock(&root->fs_info->tree_reloc_mutex);
4506 ret = init_reloc_tree(trans, root);
4508 reloc_root = root->reloc_root;
4510 shared_level = ref_path->shared_level;
4511 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
4513 keys = ref_path->node_keys;
4514 nodes = ref_path->new_nodes;
4515 memset(&keys[shared_level + 1], 0,
4516 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
4517 memset(&nodes[shared_level + 1], 0,
4518 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
4520 if (nodes[lowest_level] == 0) {
4521 path->lowest_level = lowest_level;
4522 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4525 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
4526 eb = path->nodes[level];
4527 if (!eb || eb == reloc_root->node)
4529 nodes[level] = eb->start;
4531 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4533 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4535 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4536 eb = path->nodes[0];
4537 ret = replace_extents_in_leaf(trans, reloc_root, eb,
4538 group, reloc_inode);
4541 btrfs_release_path(reloc_root, path);
4543 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
4549 * replace tree blocks in the fs tree with tree blocks in
4552 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4555 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4556 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4559 extent_buffer_get(path->nodes[0]);
4560 eb = path->nodes[0];
4561 btrfs_release_path(reloc_root, path);
4562 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4564 free_extent_buffer(eb);
4567 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4568 path->lowest_level = 0;
4572 static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4573 struct btrfs_root *root,
4574 struct btrfs_path *path,
4575 struct btrfs_key *first_key,
4576 struct btrfs_ref_path *ref_path)
4580 ret = relocate_one_path(trans, root, path, first_key,
4581 ref_path, NULL, NULL);
4584 if (root == root->fs_info->extent_root)
4585 btrfs_extent_post_op(trans, root);
4590 static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4591 struct btrfs_root *extent_root,
4592 struct btrfs_path *path,
4593 struct btrfs_key *extent_key)
4597 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4600 ret = btrfs_del_item(trans, extent_root, path);
4602 btrfs_release_path(extent_root, path);
4606 static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4607 struct btrfs_ref_path *ref_path)
4609 struct btrfs_key root_key;
4611 root_key.objectid = ref_path->root_objectid;
4612 root_key.type = BTRFS_ROOT_ITEM_KEY;
4613 if (is_cowonly_root(ref_path->root_objectid))
4614 root_key.offset = 0;
4616 root_key.offset = (u64)-1;
4618 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4621 static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4622 struct btrfs_path *path,
4623 struct btrfs_key *extent_key,
4624 struct btrfs_block_group_cache *group,
4625 struct inode *reloc_inode, int pass)
4627 struct btrfs_trans_handle *trans;
4628 struct btrfs_root *found_root;
4629 struct btrfs_ref_path *ref_path = NULL;
4630 struct disk_extent *new_extents = NULL;
4635 struct btrfs_key first_key;
4639 trans = btrfs_start_transaction(extent_root, 1);
4642 if (extent_key->objectid == 0) {
4643 ret = del_extent_zero(trans, extent_root, path, extent_key);
4647 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4653 for (loops = 0; ; loops++) {
4655 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4656 extent_key->objectid);
4658 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4665 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4666 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4669 found_root = read_ref_root(extent_root->fs_info, ref_path);
4670 BUG_ON(!found_root);
4672 * for reference counted tree, only process reference paths
4673 * rooted at the latest committed root.
4675 if (found_root->ref_cows &&
4676 ref_path->root_generation != found_root->root_key.offset)
4679 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4682 * copy data extents to new locations
4684 u64 group_start = group->key.objectid;
4685 ret = relocate_data_extent(reloc_inode,
4694 level = ref_path->owner_objectid;
4697 if (prev_block != ref_path->nodes[level]) {
4698 struct extent_buffer *eb;
4699 u64 block_start = ref_path->nodes[level];
4700 u64 block_size = btrfs_level_size(found_root, level);
4702 eb = read_tree_block(found_root, block_start,
4704 btrfs_tree_lock(eb);
4705 BUG_ON(level != btrfs_header_level(eb));
4708 btrfs_item_key_to_cpu(eb, &first_key, 0);
4710 btrfs_node_key_to_cpu(eb, &first_key, 0);
4712 btrfs_tree_unlock(eb);
4713 free_extent_buffer(eb);
4714 prev_block = block_start;
4717 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4720 * use fallback method to process the remaining
4724 u64 group_start = group->key.objectid;
4725 ret = get_new_locations(reloc_inode,
4733 btrfs_record_root_in_trans(found_root);
4734 ret = replace_one_extent(trans, found_root,
4736 &first_key, ref_path,
4737 new_extents, nr_extents);
4743 btrfs_record_root_in_trans(found_root);
4744 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4745 ret = relocate_tree_block(trans, found_root, path,
4746 &first_key, ref_path);
4749 * try to update data extent references while
4750 * keeping metadata shared between snapshots.
4752 ret = relocate_one_path(trans, found_root, path,
4753 &first_key, ref_path,
4754 group, reloc_inode);
4761 btrfs_end_transaction(trans, extent_root);
4767 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4770 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4771 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4773 num_devices = root->fs_info->fs_devices->num_devices;
4774 if (num_devices == 1) {
4775 stripped |= BTRFS_BLOCK_GROUP_DUP;
4776 stripped = flags & ~stripped;
4778 /* turn raid0 into single device chunks */
4779 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4782 /* turn mirroring into duplication */
4783 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4784 BTRFS_BLOCK_GROUP_RAID10))
4785 return stripped | BTRFS_BLOCK_GROUP_DUP;
4788 /* they already had raid on here, just return */
4789 if (flags & stripped)
4792 stripped |= BTRFS_BLOCK_GROUP_DUP;
4793 stripped = flags & ~stripped;
4795 /* switch duplicated blocks with raid1 */
4796 if (flags & BTRFS_BLOCK_GROUP_DUP)
4797 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4799 /* turn single device chunks into raid0 */
4800 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4805 int __alloc_chunk_for_shrink(struct btrfs_root *root,
4806 struct btrfs_block_group_cache *shrink_block_group,
4809 struct btrfs_trans_handle *trans;
4810 u64 new_alloc_flags;
4813 spin_lock(&shrink_block_group->lock);
4814 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
4815 spin_unlock(&shrink_block_group->lock);
4817 trans = btrfs_start_transaction(root, 1);
4818 spin_lock(&shrink_block_group->lock);
4820 new_alloc_flags = update_block_group_flags(root,
4821 shrink_block_group->flags);
4822 if (new_alloc_flags != shrink_block_group->flags) {
4824 btrfs_block_group_used(&shrink_block_group->item);
4826 calc = shrink_block_group->key.offset;
4828 spin_unlock(&shrink_block_group->lock);
4830 do_chunk_alloc(trans, root->fs_info->extent_root,
4831 calc + 2 * 1024 * 1024, new_alloc_flags, force);
4833 btrfs_end_transaction(trans, root);
4835 spin_unlock(&shrink_block_group->lock);
4839 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4840 struct btrfs_root *root,
4841 u64 objectid, u64 size)
4843 struct btrfs_path *path;
4844 struct btrfs_inode_item *item;
4845 struct extent_buffer *leaf;
4848 path = btrfs_alloc_path();
4852 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4856 leaf = path->nodes[0];
4857 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4858 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4859 btrfs_set_inode_generation(leaf, item, 1);
4860 btrfs_set_inode_size(leaf, item, size);
4861 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
4862 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM);
4863 btrfs_mark_buffer_dirty(leaf);
4864 btrfs_release_path(root, path);
4866 btrfs_free_path(path);
4870 static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4871 struct btrfs_block_group_cache *group)
4873 struct inode *inode = NULL;
4874 struct btrfs_trans_handle *trans;
4875 struct btrfs_root *root;
4876 struct btrfs_key root_key;
4877 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4880 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4881 root_key.type = BTRFS_ROOT_ITEM_KEY;
4882 root_key.offset = (u64)-1;
4883 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4885 return ERR_CAST(root);
4887 trans = btrfs_start_transaction(root, 1);
4890 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4894 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4897 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
4898 group->key.offset, 0, group->key.offset,
4902 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4903 if (inode->i_state & I_NEW) {
4904 BTRFS_I(inode)->root = root;
4905 BTRFS_I(inode)->location.objectid = objectid;
4906 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4907 BTRFS_I(inode)->location.offset = 0;
4908 btrfs_read_locked_inode(inode);
4909 unlock_new_inode(inode);
4910 BUG_ON(is_bad_inode(inode));
4915 err = btrfs_orphan_add(trans, inode);
4917 btrfs_end_transaction(trans, root);
4921 inode = ERR_PTR(err);
4926 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
4928 struct btrfs_trans_handle *trans;
4929 struct btrfs_path *path;
4930 struct btrfs_fs_info *info = root->fs_info;
4931 struct extent_buffer *leaf;
4932 struct inode *reloc_inode;
4933 struct btrfs_block_group_cache *block_group;
4934 struct btrfs_key key;
4942 root = root->fs_info->extent_root;
4944 block_group = btrfs_lookup_block_group(info, group_start);
4945 BUG_ON(!block_group);
4947 printk("btrfs relocating block group %llu flags %llu\n",
4948 (unsigned long long)block_group->key.objectid,
4949 (unsigned long long)block_group->flags);
4951 path = btrfs_alloc_path();
4954 reloc_inode = create_reloc_inode(info, block_group);
4955 BUG_ON(IS_ERR(reloc_inode));
4957 __alloc_chunk_for_shrink(root, block_group, 1);
4958 block_group->ro = 1;
4959 block_group->space_info->total_bytes -= block_group->key.offset;
4961 btrfs_start_delalloc_inodes(info->tree_root);
4962 btrfs_wait_ordered_extents(info->tree_root, 0);
4966 key.objectid = block_group->key.objectid;
4969 cur_byte = key.objectid;
4971 trans = btrfs_start_transaction(info->tree_root, 1);
4972 btrfs_commit_transaction(trans, info->tree_root);
4974 mutex_lock(&root->fs_info->cleaner_mutex);
4975 btrfs_clean_old_snapshots(info->tree_root);
4976 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4977 mutex_unlock(&root->fs_info->cleaner_mutex);
4980 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4984 leaf = path->nodes[0];
4985 nritems = btrfs_header_nritems(leaf);
4986 if (path->slots[0] >= nritems) {
4987 ret = btrfs_next_leaf(root, path);
4994 leaf = path->nodes[0];
4995 nritems = btrfs_header_nritems(leaf);
4998 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5000 if (key.objectid >= block_group->key.objectid +
5001 block_group->key.offset)
5004 if (progress && need_resched()) {
5005 btrfs_release_path(root, path);
5012 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5013 key.objectid + key.offset <= cur_byte) {
5019 cur_byte = key.objectid + key.offset;
5020 btrfs_release_path(root, path);
5022 __alloc_chunk_for_shrink(root, block_group, 0);
5023 ret = relocate_one_extent(root, path, &key, block_group,
5027 key.objectid = cur_byte;
5032 btrfs_release_path(root, path);
5035 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5036 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5037 WARN_ON(reloc_inode->i_mapping->nrpages);
5040 if (total_found > 0) {
5041 printk("btrfs found %llu extents in pass %d\n",
5042 (unsigned long long)total_found, pass);
5047 /* delete reloc_inode */
5050 /* unpin extents in this range */
5051 trans = btrfs_start_transaction(info->tree_root, 1);
5052 btrfs_commit_transaction(trans, info->tree_root);
5054 spin_lock(&block_group->lock);
5055 WARN_ON(block_group->pinned > 0);
5056 WARN_ON(block_group->reserved > 0);
5057 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5058 spin_unlock(&block_group->lock);
5061 btrfs_free_path(path);
5065 int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5066 struct btrfs_key *key)
5069 struct btrfs_key found_key;
5070 struct extent_buffer *leaf;
5073 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5078 slot = path->slots[0];
5079 leaf = path->nodes[0];
5080 if (slot >= btrfs_header_nritems(leaf)) {
5081 ret = btrfs_next_leaf(root, path);
5088 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5090 if (found_key.objectid >= key->objectid &&
5091 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5102 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5104 struct btrfs_block_group_cache *block_group;
5107 spin_lock(&info->block_group_cache_lock);
5108 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5109 block_group = rb_entry(n, struct btrfs_block_group_cache,
5112 spin_unlock(&info->block_group_cache_lock);
5113 btrfs_remove_free_space_cache(block_group);
5114 spin_lock(&info->block_group_cache_lock);
5116 rb_erase(&block_group->cache_node,
5117 &info->block_group_cache_tree);
5118 down_write(&block_group->space_info->groups_sem);
5119 list_del(&block_group->list);
5120 up_write(&block_group->space_info->groups_sem);
5123 spin_unlock(&info->block_group_cache_lock);
5127 int btrfs_read_block_groups(struct btrfs_root *root)
5129 struct btrfs_path *path;
5131 struct btrfs_block_group_cache *cache;
5132 struct btrfs_fs_info *info = root->fs_info;
5133 struct btrfs_space_info *space_info;
5134 struct btrfs_key key;
5135 struct btrfs_key found_key;
5136 struct extent_buffer *leaf;
5138 root = info->extent_root;
5141 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5142 path = btrfs_alloc_path();
5147 ret = find_first_block_group(root, path, &key);
5155 leaf = path->nodes[0];
5156 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5157 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5163 spin_lock_init(&cache->lock);
5164 mutex_init(&cache->alloc_mutex);
5165 INIT_LIST_HEAD(&cache->list);
5166 read_extent_buffer(leaf, &cache->item,
5167 btrfs_item_ptr_offset(leaf, path->slots[0]),
5168 sizeof(cache->item));
5169 memcpy(&cache->key, &found_key, sizeof(found_key));
5171 key.objectid = found_key.objectid + found_key.offset;
5172 btrfs_release_path(root, path);
5173 cache->flags = btrfs_block_group_flags(&cache->item);
5175 ret = update_space_info(info, cache->flags, found_key.offset,
5176 btrfs_block_group_used(&cache->item),
5179 cache->space_info = space_info;
5180 down_write(&space_info->groups_sem);
5181 list_add_tail(&cache->list, &space_info->block_groups);
5182 up_write(&space_info->groups_sem);
5184 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5187 set_avail_alloc_bits(root->fs_info, cache->flags);
5191 btrfs_free_path(path);
5195 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5196 struct btrfs_root *root, u64 bytes_used,
5197 u64 type, u64 chunk_objectid, u64 chunk_offset,
5201 struct btrfs_root *extent_root;
5202 struct btrfs_block_group_cache *cache;
5204 extent_root = root->fs_info->extent_root;
5206 root->fs_info->last_trans_new_blockgroup = trans->transid;
5208 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5212 cache->key.objectid = chunk_offset;
5213 cache->key.offset = size;
5214 spin_lock_init(&cache->lock);
5215 mutex_init(&cache->alloc_mutex);
5216 INIT_LIST_HEAD(&cache->list);
5217 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5219 btrfs_set_block_group_used(&cache->item, bytes_used);
5220 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5221 cache->flags = type;
5222 btrfs_set_block_group_flags(&cache->item, type);
5224 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5225 &cache->space_info);
5227 down_write(&cache->space_info->groups_sem);
5228 list_add_tail(&cache->list, &cache->space_info->block_groups);
5229 up_write(&cache->space_info->groups_sem);
5231 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5234 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5235 sizeof(cache->item));
5238 finish_current_insert(trans, extent_root, 0);
5239 ret = del_pending_extents(trans, extent_root, 0);
5241 set_avail_alloc_bits(extent_root->fs_info, type);
5246 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5247 struct btrfs_root *root, u64 group_start)
5249 struct btrfs_path *path;
5250 struct btrfs_block_group_cache *block_group;
5251 struct btrfs_key key;
5254 root = root->fs_info->extent_root;
5256 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5257 BUG_ON(!block_group);
5259 memcpy(&key, &block_group->key, sizeof(key));
5261 path = btrfs_alloc_path();
5264 btrfs_remove_free_space_cache(block_group);
5265 rb_erase(&block_group->cache_node,
5266 &root->fs_info->block_group_cache_tree);
5267 down_write(&block_group->space_info->groups_sem);
5268 list_del(&block_group->list);
5269 up_write(&block_group->space_info->groups_sem);
5272 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5273 kfree(shrink_block_group);
5276 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5282 ret = btrfs_del_item(trans, root, path);
5284 btrfs_free_path(path);