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>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
29 #include "print-tree.h"
30 #include "transaction.h"
33 #include "ref-cache.h"
35 #define PENDING_EXTENT_INSERT 0
36 #define PENDING_EXTENT_DELETE 1
37 #define PENDING_BACKREF_UPDATE 2
39 struct pending_extent_op {
48 struct list_head list;
52 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root, u64 parent,
54 u64 root_objectid, u64 ref_generation,
55 u64 owner, struct btrfs_key *ins,
57 static int update_reserved_extents(struct btrfs_root *root,
58 u64 bytenr, u64 num, int reserve);
59 static int pin_down_bytes(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int is_data);
62 static int update_block_group(struct btrfs_trans_handle *trans,
63 struct btrfs_root *root,
64 u64 bytenr, u64 num_bytes, int alloc,
66 static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
67 struct btrfs_root *root,
68 u64 bytenr, u64 num_bytes, u64 parent,
69 u64 root_objectid, u64 ref_generation,
70 u64 owner_objectid, int pin,
73 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
74 struct btrfs_root *extent_root, u64 alloc_bytes,
75 u64 flags, int force);
77 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
79 return (cache->flags & bits) == bits;
83 * this adds the block group to the fs_info rb tree for the block group
86 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
87 struct btrfs_block_group_cache *block_group)
90 struct rb_node *parent = NULL;
91 struct btrfs_block_group_cache *cache;
93 spin_lock(&info->block_group_cache_lock);
94 p = &info->block_group_cache_tree.rb_node;
98 cache = rb_entry(parent, struct btrfs_block_group_cache,
100 if (block_group->key.objectid < cache->key.objectid) {
102 } else if (block_group->key.objectid > cache->key.objectid) {
105 spin_unlock(&info->block_group_cache_lock);
110 rb_link_node(&block_group->cache_node, parent, p);
111 rb_insert_color(&block_group->cache_node,
112 &info->block_group_cache_tree);
113 spin_unlock(&info->block_group_cache_lock);
119 * This will return the block group at or after bytenr if contains is 0, else
120 * it will return the block group that contains the bytenr
122 static struct btrfs_block_group_cache *
123 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
126 struct btrfs_block_group_cache *cache, *ret = NULL;
130 spin_lock(&info->block_group_cache_lock);
131 n = info->block_group_cache_tree.rb_node;
134 cache = rb_entry(n, struct btrfs_block_group_cache,
136 end = cache->key.objectid + cache->key.offset - 1;
137 start = cache->key.objectid;
139 if (bytenr < start) {
140 if (!contains && (!ret || start < ret->key.objectid))
143 } else if (bytenr > start) {
144 if (contains && bytenr <= end) {
155 atomic_inc(&ret->count);
156 spin_unlock(&info->block_group_cache_lock);
162 * this is only called by cache_block_group, since we could have freed extents
163 * we need to check the pinned_extents for any extents that can't be used yet
164 * since their free space will be released as soon as the transaction commits.
166 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
167 struct btrfs_fs_info *info, u64 start, u64 end)
169 u64 extent_start, extent_end, size;
172 mutex_lock(&info->pinned_mutex);
173 while (start < end) {
174 ret = find_first_extent_bit(&info->pinned_extents, start,
175 &extent_start, &extent_end,
180 if (extent_start == start) {
181 start = extent_end + 1;
182 } else if (extent_start > start && extent_start < end) {
183 size = extent_start - start;
184 ret = btrfs_add_free_space(block_group, start,
187 start = extent_end + 1;
195 ret = btrfs_add_free_space(block_group, start, size);
198 mutex_unlock(&info->pinned_mutex);
203 static int remove_sb_from_cache(struct btrfs_root *root,
204 struct btrfs_block_group_cache *cache)
211 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
212 bytenr = btrfs_sb_offset(i);
213 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
214 cache->key.objectid, bytenr, 0,
215 &logical, &nr, &stripe_len);
218 btrfs_remove_free_space(cache, logical[nr],
226 static int cache_block_group(struct btrfs_root *root,
227 struct btrfs_block_group_cache *block_group)
229 struct btrfs_path *path;
231 struct btrfs_key key;
232 struct extent_buffer *leaf;
239 root = root->fs_info->extent_root;
241 if (block_group->cached)
244 path = btrfs_alloc_path();
250 * we get into deadlocks with paths held by callers of this function.
251 * since the alloc_mutex is protecting things right now, just
252 * skip the locking here
254 path->skip_locking = 1;
255 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
258 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
264 leaf = path->nodes[0];
265 slot = path->slots[0];
266 if (slot >= btrfs_header_nritems(leaf)) {
267 ret = btrfs_next_leaf(root, path);
275 btrfs_item_key_to_cpu(leaf, &key, slot);
276 if (key.objectid < block_group->key.objectid)
279 if (key.objectid >= block_group->key.objectid +
280 block_group->key.offset)
283 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
284 add_new_free_space(block_group, root->fs_info, last,
287 last = key.objectid + key.offset;
293 add_new_free_space(block_group, root->fs_info, last,
294 block_group->key.objectid +
295 block_group->key.offset);
297 remove_sb_from_cache(root, block_group);
298 block_group->cached = 1;
301 btrfs_free_path(path);
306 * return the block group that starts at or after bytenr
308 static struct btrfs_block_group_cache *
309 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
311 struct btrfs_block_group_cache *cache;
313 cache = block_group_cache_tree_search(info, bytenr, 0);
319 * return the block group that contains teh given bytenr
321 struct btrfs_block_group_cache *btrfs_lookup_block_group(
322 struct btrfs_fs_info *info,
325 struct btrfs_block_group_cache *cache;
327 cache = block_group_cache_tree_search(info, bytenr, 1);
332 static inline void put_block_group(struct btrfs_block_group_cache *cache)
334 if (atomic_dec_and_test(&cache->count))
338 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
341 struct list_head *head = &info->space_info;
342 struct btrfs_space_info *found;
345 list_for_each_entry_rcu(found, head, list) {
346 if (found->flags == flags) {
356 * after adding space to the filesystem, we need to clear the full flags
357 * on all the space infos.
359 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
361 struct list_head *head = &info->space_info;
362 struct btrfs_space_info *found;
365 list_for_each_entry_rcu(found, head, list)
370 static u64 div_factor(u64 num, int factor)
379 u64 btrfs_find_block_group(struct btrfs_root *root,
380 u64 search_start, u64 search_hint, int owner)
382 struct btrfs_block_group_cache *cache;
384 u64 last = max(search_hint, search_start);
391 cache = btrfs_lookup_first_block_group(root->fs_info, last);
395 spin_lock(&cache->lock);
396 last = cache->key.objectid + cache->key.offset;
397 used = btrfs_block_group_used(&cache->item);
399 if ((full_search || !cache->ro) &&
400 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
401 if (used + cache->pinned + cache->reserved <
402 div_factor(cache->key.offset, factor)) {
403 group_start = cache->key.objectid;
404 spin_unlock(&cache->lock);
405 put_block_group(cache);
409 spin_unlock(&cache->lock);
410 put_block_group(cache);
418 if (!full_search && factor < 10) {
428 /* simple helper to search for an existing extent at a given offset */
429 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
432 struct btrfs_key key;
433 struct btrfs_path *path;
435 path = btrfs_alloc_path();
437 key.objectid = start;
439 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
440 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
442 btrfs_free_path(path);
447 * Back reference rules. Back refs have three main goals:
449 * 1) differentiate between all holders of references to an extent so that
450 * when a reference is dropped we can make sure it was a valid reference
451 * before freeing the extent.
453 * 2) Provide enough information to quickly find the holders of an extent
454 * if we notice a given block is corrupted or bad.
456 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
457 * maintenance. This is actually the same as #2, but with a slightly
458 * different use case.
460 * File extents can be referenced by:
462 * - multiple snapshots, subvolumes, or different generations in one subvol
463 * - different files inside a single subvolume
464 * - different offsets inside a file (bookend extents in file.c)
466 * The extent ref structure has fields for:
468 * - Objectid of the subvolume root
469 * - Generation number of the tree holding the reference
470 * - objectid of the file holding the reference
471 * - number of references holding by parent node (alway 1 for tree blocks)
473 * Btree leaf may hold multiple references to a file extent. In most cases,
474 * these references are from same file and the corresponding offsets inside
475 * the file are close together.
477 * When a file extent is allocated the fields are filled in:
478 * (root_key.objectid, trans->transid, inode objectid, 1)
480 * When a leaf is cow'd new references are added for every file extent found
481 * in the leaf. It looks similar to the create case, but trans->transid will
482 * be different when the block is cow'd.
484 * (root_key.objectid, trans->transid, inode objectid,
485 * number of references in the leaf)
487 * When a file extent is removed either during snapshot deletion or
488 * file truncation, we find the corresponding back reference and check
489 * the following fields:
491 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
494 * Btree extents can be referenced by:
496 * - Different subvolumes
497 * - Different generations of the same subvolume
499 * When a tree block is created, back references are inserted:
501 * (root->root_key.objectid, trans->transid, level, 1)
503 * When a tree block is cow'd, new back references are added for all the
504 * blocks it points to. If the tree block isn't in reference counted root,
505 * the old back references are removed. These new back references are of
506 * the form (trans->transid will have increased since creation):
508 * (root->root_key.objectid, trans->transid, level, 1)
510 * When a backref is in deleting, the following fields are checked:
512 * if backref was for a tree root:
513 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
515 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
517 * Back Reference Key composing:
519 * The key objectid corresponds to the first byte in the extent, the key
520 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
521 * byte of parent extent. If a extent is tree root, the key offset is set
522 * to the key objectid.
525 static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
526 struct btrfs_root *root,
527 struct btrfs_path *path,
528 u64 bytenr, u64 parent,
529 u64 ref_root, u64 ref_generation,
530 u64 owner_objectid, int del)
532 struct btrfs_key key;
533 struct btrfs_extent_ref *ref;
534 struct extent_buffer *leaf;
538 key.objectid = bytenr;
539 key.type = BTRFS_EXTENT_REF_KEY;
542 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
550 leaf = path->nodes[0];
551 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
552 ref_objectid = btrfs_ref_objectid(leaf, ref);
553 if (btrfs_ref_root(leaf, ref) != ref_root ||
554 btrfs_ref_generation(leaf, ref) != ref_generation ||
555 (ref_objectid != owner_objectid &&
556 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
566 static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
567 struct btrfs_root *root,
568 struct btrfs_path *path,
569 u64 bytenr, u64 parent,
570 u64 ref_root, u64 ref_generation,
574 struct btrfs_key key;
575 struct extent_buffer *leaf;
576 struct btrfs_extent_ref *ref;
580 key.objectid = bytenr;
581 key.type = BTRFS_EXTENT_REF_KEY;
584 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
586 leaf = path->nodes[0];
587 ref = btrfs_item_ptr(leaf, path->slots[0],
588 struct btrfs_extent_ref);
589 btrfs_set_ref_root(leaf, ref, ref_root);
590 btrfs_set_ref_generation(leaf, ref, ref_generation);
591 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
592 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
593 } else if (ret == -EEXIST) {
596 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
597 leaf = path->nodes[0];
598 ref = btrfs_item_ptr(leaf, path->slots[0],
599 struct btrfs_extent_ref);
600 if (btrfs_ref_root(leaf, ref) != ref_root ||
601 btrfs_ref_generation(leaf, ref) != ref_generation) {
607 num_refs = btrfs_ref_num_refs(leaf, ref);
608 BUG_ON(num_refs == 0);
609 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
611 existing_owner = btrfs_ref_objectid(leaf, ref);
612 if (existing_owner != owner_objectid &&
613 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
614 btrfs_set_ref_objectid(leaf, ref,
615 BTRFS_MULTIPLE_OBJECTIDS);
621 btrfs_mark_buffer_dirty(path->nodes[0]);
623 btrfs_release_path(root, path);
627 static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
628 struct btrfs_root *root,
629 struct btrfs_path *path,
632 struct extent_buffer *leaf;
633 struct btrfs_extent_ref *ref;
637 leaf = path->nodes[0];
638 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
639 num_refs = btrfs_ref_num_refs(leaf, ref);
640 BUG_ON(num_refs < refs_to_drop);
641 num_refs -= refs_to_drop;
643 ret = btrfs_del_item(trans, root, path);
645 btrfs_set_ref_num_refs(leaf, ref, num_refs);
646 btrfs_mark_buffer_dirty(leaf);
648 btrfs_release_path(root, path);
652 #ifdef BIO_RW_DISCARD
653 static void btrfs_issue_discard(struct block_device *bdev,
656 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
660 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
663 #ifdef BIO_RW_DISCARD
665 u64 map_length = num_bytes;
666 struct btrfs_multi_bio *multi = NULL;
668 /* Tell the block device(s) that the sectors can be discarded */
669 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
670 bytenr, &map_length, &multi, 0);
672 struct btrfs_bio_stripe *stripe = multi->stripes;
675 if (map_length > num_bytes)
676 map_length = num_bytes;
678 for (i = 0; i < multi->num_stripes; i++, stripe++) {
679 btrfs_issue_discard(stripe->dev->bdev,
692 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
693 struct btrfs_root *root, u64 bytenr,
695 u64 orig_parent, u64 parent,
696 u64 orig_root, u64 ref_root,
697 u64 orig_generation, u64 ref_generation,
701 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
703 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
704 orig_parent, parent, orig_root,
705 ref_root, orig_generation,
706 ref_generation, owner_objectid, pin);
711 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
712 struct btrfs_root *root, u64 bytenr,
713 u64 num_bytes, u64 orig_parent, u64 parent,
714 u64 ref_root, u64 ref_generation,
718 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
719 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
722 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
723 orig_parent, parent, ref_root,
724 ref_root, ref_generation,
725 ref_generation, owner_objectid);
728 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
729 struct btrfs_root *root, u64 bytenr,
731 u64 orig_parent, u64 parent,
732 u64 orig_root, u64 ref_root,
733 u64 orig_generation, u64 ref_generation,
738 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
739 ref_generation, owner_objectid,
740 BTRFS_ADD_DELAYED_REF, 0);
745 static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
746 struct btrfs_root *root, u64 bytenr,
747 u64 num_bytes, u64 parent, u64 ref_root,
748 u64 ref_generation, u64 owner_objectid,
751 struct btrfs_path *path;
753 struct btrfs_key key;
754 struct extent_buffer *l;
755 struct btrfs_extent_item *item;
758 path = btrfs_alloc_path();
763 key.objectid = bytenr;
764 key.type = BTRFS_EXTENT_ITEM_KEY;
765 key.offset = num_bytes;
767 /* first find the extent item and update its reference count */
768 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
775 btrfs_free_path(path);
780 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
781 if (key.objectid != bytenr) {
782 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
783 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
784 (unsigned long long)bytenr,
785 (unsigned long long)key.objectid);
788 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
790 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
792 refs = btrfs_extent_refs(l, item);
793 btrfs_set_extent_refs(l, item, refs + refs_to_add);
794 btrfs_mark_buffer_dirty(path->nodes[0]);
796 btrfs_release_path(root->fs_info->extent_root, path);
799 /* now insert the actual backref */
800 ret = insert_extent_backref(trans, root->fs_info->extent_root,
801 path, bytenr, parent,
802 ref_root, ref_generation,
803 owner_objectid, refs_to_add);
805 btrfs_free_path(path);
809 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
810 struct btrfs_root *root,
811 u64 bytenr, u64 num_bytes, u64 parent,
812 u64 ref_root, u64 ref_generation,
816 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
817 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
820 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
821 0, ref_root, 0, ref_generation,
826 static int drop_delayed_ref(struct btrfs_trans_handle *trans,
827 struct btrfs_root *root,
828 struct btrfs_delayed_ref_node *node)
831 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
833 BUG_ON(node->ref_mod == 0);
834 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
835 node->parent, ref->root, ref->generation,
836 ref->owner_objectid, ref->pin, node->ref_mod);
841 /* helper function to actually process a single delayed ref entry */
842 static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct btrfs_delayed_ref_node *node,
848 struct btrfs_delayed_ref *ref;
850 if (node->parent == (u64)-1) {
851 struct btrfs_delayed_ref_head *head;
853 * we've hit the end of the chain and we were supposed
854 * to insert this extent into the tree. But, it got
855 * deleted before we ever needed to insert it, so all
856 * we have to do is clean up the accounting
858 if (insert_reserved) {
859 update_reserved_extents(root, node->bytenr,
862 head = btrfs_delayed_node_to_head(node);
863 mutex_unlock(&head->mutex);
867 ref = btrfs_delayed_node_to_ref(node);
868 if (ref->action == BTRFS_ADD_DELAYED_REF) {
869 if (insert_reserved) {
870 struct btrfs_key ins;
872 ins.objectid = node->bytenr;
873 ins.offset = node->num_bytes;
874 ins.type = BTRFS_EXTENT_ITEM_KEY;
876 /* record the full extent allocation */
877 ret = __btrfs_alloc_reserved_extent(trans, root,
878 node->parent, ref->root,
879 ref->generation, ref->owner_objectid,
880 &ins, node->ref_mod);
881 update_reserved_extents(root, node->bytenr,
884 /* just add one backref */
885 ret = add_extent_ref(trans, root, node->bytenr,
887 node->parent, ref->root, ref->generation,
888 ref->owner_objectid, node->ref_mod);
891 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
892 WARN_ON(insert_reserved);
893 ret = drop_delayed_ref(trans, root, node);
898 static noinline struct btrfs_delayed_ref_node *
899 select_delayed_ref(struct btrfs_delayed_ref_head *head)
901 struct rb_node *node;
902 struct btrfs_delayed_ref_node *ref;
903 int action = BTRFS_ADD_DELAYED_REF;
906 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
907 * this prevents ref count from going down to zero when
908 * there still are pending delayed ref.
910 node = rb_prev(&head->node.rb_node);
914 ref = rb_entry(node, struct btrfs_delayed_ref_node,
916 if (ref->bytenr != head->node.bytenr)
918 if (btrfs_delayed_node_to_ref(ref)->action == action)
920 node = rb_prev(node);
922 if (action == BTRFS_ADD_DELAYED_REF) {
923 action = BTRFS_DROP_DELAYED_REF;
929 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
930 struct btrfs_root *root,
931 struct list_head *cluster)
933 struct btrfs_delayed_ref_root *delayed_refs;
934 struct btrfs_delayed_ref_node *ref;
935 struct btrfs_delayed_ref_head *locked_ref = NULL;
938 int must_insert_reserved = 0;
940 delayed_refs = &trans->transaction->delayed_refs;
943 /* pick a new head ref from the cluster list */
944 if (list_empty(cluster))
947 locked_ref = list_entry(cluster->next,
948 struct btrfs_delayed_ref_head, cluster);
950 /* grab the lock that says we are going to process
951 * all the refs for this head */
952 ret = btrfs_delayed_ref_lock(trans, locked_ref);
955 * we may have dropped the spin lock to get the head
956 * mutex lock, and that might have given someone else
957 * time to free the head. If that's true, it has been
958 * removed from our list and we can move on.
960 if (ret == -EAGAIN) {
968 * record the must insert reserved flag before we
969 * drop the spin lock.
971 must_insert_reserved = locked_ref->must_insert_reserved;
972 locked_ref->must_insert_reserved = 0;
975 * locked_ref is the head node, so we have to go one
976 * node back for any delayed ref updates
978 ref = select_delayed_ref(locked_ref);
980 /* All delayed refs have been processed, Go ahead
981 * and send the head node to run_one_delayed_ref,
982 * so that any accounting fixes can happen
984 ref = &locked_ref->node;
985 list_del_init(&locked_ref->cluster);
990 rb_erase(&ref->rb_node, &delayed_refs->root);
991 delayed_refs->num_entries--;
992 spin_unlock(&delayed_refs->lock);
994 ret = run_one_delayed_ref(trans, root, ref,
995 must_insert_reserved);
997 btrfs_put_delayed_ref(ref);
1001 spin_lock(&delayed_refs->lock);
1007 * this starts processing the delayed reference count updates and
1008 * extent insertions we have queued up so far. count can be
1009 * 0, which means to process everything in the tree at the start
1010 * of the run (but not newly added entries), or it can be some target
1011 * number you'd like to process.
1013 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1014 struct btrfs_root *root, unsigned long count)
1016 struct rb_node *node;
1017 struct btrfs_delayed_ref_root *delayed_refs;
1018 struct btrfs_delayed_ref_node *ref;
1019 struct list_head cluster;
1021 int run_all = count == (unsigned long)-1;
1024 if (root == root->fs_info->extent_root)
1025 root = root->fs_info->tree_root;
1027 delayed_refs = &trans->transaction->delayed_refs;
1028 INIT_LIST_HEAD(&cluster);
1030 spin_lock(&delayed_refs->lock);
1032 count = delayed_refs->num_entries * 2;
1036 if (!(run_all || run_most) &&
1037 delayed_refs->num_heads_ready < 64)
1041 * go find something we can process in the rbtree. We start at
1042 * the beginning of the tree, and then build a cluster
1043 * of refs to process starting at the first one we are able to
1046 ret = btrfs_find_ref_cluster(trans, &cluster,
1047 delayed_refs->run_delayed_start);
1051 ret = run_clustered_refs(trans, root, &cluster);
1054 count -= min_t(unsigned long, ret, count);
1061 node = rb_first(&delayed_refs->root);
1064 count = (unsigned long)-1;
1067 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1069 if (btrfs_delayed_ref_is_head(ref)) {
1070 struct btrfs_delayed_ref_head *head;
1072 head = btrfs_delayed_node_to_head(ref);
1073 atomic_inc(&ref->refs);
1075 spin_unlock(&delayed_refs->lock);
1076 mutex_lock(&head->mutex);
1077 mutex_unlock(&head->mutex);
1079 btrfs_put_delayed_ref(ref);
1083 node = rb_next(node);
1085 spin_unlock(&delayed_refs->lock);
1086 schedule_timeout(1);
1090 spin_unlock(&delayed_refs->lock);
1094 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1095 struct btrfs_root *root, u64 objectid, u64 bytenr)
1097 struct btrfs_root *extent_root = root->fs_info->extent_root;
1098 struct btrfs_path *path;
1099 struct extent_buffer *leaf;
1100 struct btrfs_extent_ref *ref_item;
1101 struct btrfs_key key;
1102 struct btrfs_key found_key;
1108 key.objectid = bytenr;
1109 key.offset = (u64)-1;
1110 key.type = BTRFS_EXTENT_ITEM_KEY;
1112 path = btrfs_alloc_path();
1113 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1119 if (path->slots[0] == 0)
1123 leaf = path->nodes[0];
1124 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1126 if (found_key.objectid != bytenr ||
1127 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1130 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1132 leaf = path->nodes[0];
1133 nritems = btrfs_header_nritems(leaf);
1134 if (path->slots[0] >= nritems) {
1135 ret = btrfs_next_leaf(extent_root, path);
1142 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1143 if (found_key.objectid != bytenr)
1146 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1151 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1152 struct btrfs_extent_ref);
1153 ref_root = btrfs_ref_root(leaf, ref_item);
1154 if ((ref_root != root->root_key.objectid &&
1155 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1156 objectid != btrfs_ref_objectid(leaf, ref_item)) {
1160 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1169 btrfs_free_path(path);
1173 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1174 struct extent_buffer *buf, u32 nr_extents)
1176 struct btrfs_key key;
1177 struct btrfs_file_extent_item *fi;
1185 if (!root->ref_cows)
1188 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1190 root_gen = root->root_key.offset;
1193 root_gen = trans->transid - 1;
1196 level = btrfs_header_level(buf);
1197 nritems = btrfs_header_nritems(buf);
1200 struct btrfs_leaf_ref *ref;
1201 struct btrfs_extent_info *info;
1203 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1209 ref->root_gen = root_gen;
1210 ref->bytenr = buf->start;
1211 ref->owner = btrfs_header_owner(buf);
1212 ref->generation = btrfs_header_generation(buf);
1213 ref->nritems = nr_extents;
1214 info = ref->extents;
1216 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1218 btrfs_item_key_to_cpu(buf, &key, i);
1219 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1221 fi = btrfs_item_ptr(buf, i,
1222 struct btrfs_file_extent_item);
1223 if (btrfs_file_extent_type(buf, fi) ==
1224 BTRFS_FILE_EXTENT_INLINE)
1226 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1227 if (disk_bytenr == 0)
1230 info->bytenr = disk_bytenr;
1232 btrfs_file_extent_disk_num_bytes(buf, fi);
1233 info->objectid = key.objectid;
1234 info->offset = key.offset;
1238 ret = btrfs_add_leaf_ref(root, ref, shared);
1239 if (ret == -EEXIST && shared) {
1240 struct btrfs_leaf_ref *old;
1241 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1243 btrfs_remove_leaf_ref(root, old);
1244 btrfs_free_leaf_ref(root, old);
1245 ret = btrfs_add_leaf_ref(root, ref, shared);
1248 btrfs_free_leaf_ref(root, ref);
1254 /* when a block goes through cow, we update the reference counts of
1255 * everything that block points to. The internal pointers of the block
1256 * can be in just about any order, and it is likely to have clusters of
1257 * things that are close together and clusters of things that are not.
1259 * To help reduce the seeks that come with updating all of these reference
1260 * counts, sort them by byte number before actual updates are done.
1262 * struct refsort is used to match byte number to slot in the btree block.
1263 * we sort based on the byte number and then use the slot to actually
1266 * struct refsort is smaller than strcut btrfs_item and smaller than
1267 * struct btrfs_key_ptr. Since we're currently limited to the page size
1268 * for a btree block, there's no way for a kmalloc of refsorts for a
1269 * single node to be bigger than a page.
1277 * for passing into sort()
1279 static int refsort_cmp(const void *a_void, const void *b_void)
1281 const struct refsort *a = a_void;
1282 const struct refsort *b = b_void;
1284 if (a->bytenr < b->bytenr)
1286 if (a->bytenr > b->bytenr)
1292 noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1293 struct btrfs_root *root,
1294 struct extent_buffer *orig_buf,
1295 struct extent_buffer *buf, u32 *nr_extents)
1301 u64 orig_generation;
1302 struct refsort *sorted;
1304 u32 nr_file_extents = 0;
1305 struct btrfs_key key;
1306 struct btrfs_file_extent_item *fi;
1313 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1314 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1316 ref_root = btrfs_header_owner(buf);
1317 ref_generation = btrfs_header_generation(buf);
1318 orig_root = btrfs_header_owner(orig_buf);
1319 orig_generation = btrfs_header_generation(orig_buf);
1321 nritems = btrfs_header_nritems(buf);
1322 level = btrfs_header_level(buf);
1324 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1327 if (root->ref_cows) {
1328 process_func = __btrfs_inc_extent_ref;
1331 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1334 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1336 process_func = __btrfs_update_extent_ref;
1340 * we make two passes through the items. In the first pass we
1341 * only record the byte number and slot. Then we sort based on
1342 * byte number and do the actual work based on the sorted results
1344 for (i = 0; i < nritems; i++) {
1347 btrfs_item_key_to_cpu(buf, &key, i);
1348 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1350 fi = btrfs_item_ptr(buf, i,
1351 struct btrfs_file_extent_item);
1352 if (btrfs_file_extent_type(buf, fi) ==
1353 BTRFS_FILE_EXTENT_INLINE)
1355 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1360 sorted[refi].bytenr = bytenr;
1361 sorted[refi].slot = i;
1364 bytenr = btrfs_node_blockptr(buf, i);
1365 sorted[refi].bytenr = bytenr;
1366 sorted[refi].slot = i;
1371 * if refi == 0, we didn't actually put anything into the sorted
1372 * array and we're done
1377 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1379 for (i = 0; i < refi; i++) {
1381 slot = sorted[i].slot;
1382 bytenr = sorted[i].bytenr;
1385 btrfs_item_key_to_cpu(buf, &key, slot);
1386 fi = btrfs_item_ptr(buf, slot,
1387 struct btrfs_file_extent_item);
1389 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1393 ret = process_func(trans, root, bytenr,
1394 btrfs_file_extent_disk_num_bytes(buf, fi),
1395 orig_buf->start, buf->start,
1396 orig_root, ref_root,
1397 orig_generation, ref_generation,
1406 ret = process_func(trans, root, bytenr, buf->len,
1407 orig_buf->start, buf->start,
1408 orig_root, ref_root,
1409 orig_generation, ref_generation,
1422 *nr_extents = nr_file_extents;
1424 *nr_extents = nritems;
1433 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1434 struct btrfs_root *root, struct extent_buffer *orig_buf,
1435 struct extent_buffer *buf, int start_slot, int nr)
1442 u64 orig_generation;
1443 struct btrfs_key key;
1444 struct btrfs_file_extent_item *fi;
1450 BUG_ON(start_slot < 0);
1451 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1453 ref_root = btrfs_header_owner(buf);
1454 ref_generation = btrfs_header_generation(buf);
1455 orig_root = btrfs_header_owner(orig_buf);
1456 orig_generation = btrfs_header_generation(orig_buf);
1457 level = btrfs_header_level(buf);
1459 if (!root->ref_cows) {
1461 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1464 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1468 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1471 btrfs_item_key_to_cpu(buf, &key, slot);
1472 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1474 fi = btrfs_item_ptr(buf, slot,
1475 struct btrfs_file_extent_item);
1476 if (btrfs_file_extent_type(buf, fi) ==
1477 BTRFS_FILE_EXTENT_INLINE)
1479 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1482 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1483 btrfs_file_extent_disk_num_bytes(buf, fi),
1484 orig_buf->start, buf->start,
1485 orig_root, ref_root, orig_generation,
1486 ref_generation, key.objectid);
1490 bytenr = btrfs_node_blockptr(buf, slot);
1491 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1492 buf->len, orig_buf->start,
1493 buf->start, orig_root, ref_root,
1494 orig_generation, ref_generation,
1506 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1507 struct btrfs_root *root,
1508 struct btrfs_path *path,
1509 struct btrfs_block_group_cache *cache)
1512 struct btrfs_root *extent_root = root->fs_info->extent_root;
1514 struct extent_buffer *leaf;
1516 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1521 leaf = path->nodes[0];
1522 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1523 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1524 btrfs_mark_buffer_dirty(leaf);
1525 btrfs_release_path(extent_root, path);
1533 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1534 struct btrfs_root *root)
1536 struct btrfs_block_group_cache *cache, *entry;
1540 struct btrfs_path *path;
1543 path = btrfs_alloc_path();
1549 spin_lock(&root->fs_info->block_group_cache_lock);
1550 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1551 n; n = rb_next(n)) {
1552 entry = rb_entry(n, struct btrfs_block_group_cache,
1559 spin_unlock(&root->fs_info->block_group_cache_lock);
1565 last += cache->key.offset;
1567 err = write_one_cache_group(trans, root,
1570 * if we fail to write the cache group, we want
1571 * to keep it marked dirty in hopes that a later
1579 btrfs_free_path(path);
1583 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1585 struct btrfs_block_group_cache *block_group;
1588 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1589 if (!block_group || block_group->ro)
1592 put_block_group(block_group);
1596 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1597 u64 total_bytes, u64 bytes_used,
1598 struct btrfs_space_info **space_info)
1600 struct btrfs_space_info *found;
1602 found = __find_space_info(info, flags);
1604 spin_lock(&found->lock);
1605 found->total_bytes += total_bytes;
1606 found->bytes_used += bytes_used;
1608 spin_unlock(&found->lock);
1609 *space_info = found;
1612 found = kzalloc(sizeof(*found), GFP_NOFS);
1616 INIT_LIST_HEAD(&found->block_groups);
1617 init_rwsem(&found->groups_sem);
1618 spin_lock_init(&found->lock);
1619 found->flags = flags;
1620 found->total_bytes = total_bytes;
1621 found->bytes_used = bytes_used;
1622 found->bytes_pinned = 0;
1623 found->bytes_reserved = 0;
1624 found->bytes_readonly = 0;
1625 found->bytes_delalloc = 0;
1627 found->force_alloc = 0;
1628 *space_info = found;
1629 list_add_rcu(&found->list, &info->space_info);
1633 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1635 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1636 BTRFS_BLOCK_GROUP_RAID1 |
1637 BTRFS_BLOCK_GROUP_RAID10 |
1638 BTRFS_BLOCK_GROUP_DUP);
1640 if (flags & BTRFS_BLOCK_GROUP_DATA)
1641 fs_info->avail_data_alloc_bits |= extra_flags;
1642 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1643 fs_info->avail_metadata_alloc_bits |= extra_flags;
1644 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1645 fs_info->avail_system_alloc_bits |= extra_flags;
1649 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1651 spin_lock(&cache->space_info->lock);
1652 spin_lock(&cache->lock);
1654 cache->space_info->bytes_readonly += cache->key.offset -
1655 btrfs_block_group_used(&cache->item);
1658 spin_unlock(&cache->lock);
1659 spin_unlock(&cache->space_info->lock);
1662 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1664 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1666 if (num_devices == 1)
1667 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1668 if (num_devices < 4)
1669 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1671 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1672 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1673 BTRFS_BLOCK_GROUP_RAID10))) {
1674 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1677 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1678 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1679 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1682 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1683 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1684 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1685 (flags & BTRFS_BLOCK_GROUP_DUP)))
1686 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1690 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1692 struct btrfs_fs_info *info = root->fs_info;
1696 alloc_profile = info->avail_data_alloc_bits &
1697 info->data_alloc_profile;
1698 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1699 } else if (root == root->fs_info->chunk_root) {
1700 alloc_profile = info->avail_system_alloc_bits &
1701 info->system_alloc_profile;
1702 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1704 alloc_profile = info->avail_metadata_alloc_bits &
1705 info->metadata_alloc_profile;
1706 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1709 return btrfs_reduce_alloc_profile(root, data);
1712 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1716 alloc_target = btrfs_get_alloc_profile(root, 1);
1717 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1722 * for now this just makes sure we have at least 5% of our metadata space free
1725 int btrfs_check_metadata_free_space(struct btrfs_root *root)
1727 struct btrfs_fs_info *info = root->fs_info;
1728 struct btrfs_space_info *meta_sinfo;
1729 u64 alloc_target, thresh;
1730 int committed = 0, ret;
1732 /* get the space info for where the metadata will live */
1733 alloc_target = btrfs_get_alloc_profile(root, 0);
1734 meta_sinfo = __find_space_info(info, alloc_target);
1737 spin_lock(&meta_sinfo->lock);
1738 if (!meta_sinfo->full)
1739 thresh = meta_sinfo->total_bytes * 80;
1741 thresh = meta_sinfo->total_bytes * 95;
1743 do_div(thresh, 100);
1745 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1746 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
1747 struct btrfs_trans_handle *trans;
1748 if (!meta_sinfo->full) {
1749 meta_sinfo->force_alloc = 1;
1750 spin_unlock(&meta_sinfo->lock);
1752 trans = btrfs_start_transaction(root, 1);
1756 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1757 2 * 1024 * 1024, alloc_target, 0);
1758 btrfs_end_transaction(trans, root);
1761 spin_unlock(&meta_sinfo->lock);
1765 trans = btrfs_join_transaction(root, 1);
1768 ret = btrfs_commit_transaction(trans, root);
1775 spin_unlock(&meta_sinfo->lock);
1781 * This will check the space that the inode allocates from to make sure we have
1782 * enough space for bytes.
1784 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1787 struct btrfs_space_info *data_sinfo;
1788 int ret = 0, committed = 0;
1790 /* make sure bytes are sectorsize aligned */
1791 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1793 data_sinfo = BTRFS_I(inode)->space_info;
1795 /* make sure we have enough space to handle the data first */
1796 spin_lock(&data_sinfo->lock);
1797 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1798 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1799 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1800 data_sinfo->bytes_may_use < bytes) {
1801 struct btrfs_trans_handle *trans;
1804 * if we don't have enough free bytes in this space then we need
1805 * to alloc a new chunk.
1807 if (!data_sinfo->full) {
1810 data_sinfo->force_alloc = 1;
1811 spin_unlock(&data_sinfo->lock);
1813 alloc_target = btrfs_get_alloc_profile(root, 1);
1814 trans = btrfs_start_transaction(root, 1);
1818 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1819 bytes + 2 * 1024 * 1024,
1821 btrfs_end_transaction(trans, root);
1826 spin_unlock(&data_sinfo->lock);
1828 /* commit the current transaction and try again */
1831 trans = btrfs_join_transaction(root, 1);
1834 ret = btrfs_commit_transaction(trans, root);
1840 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1841 ", %llu bytes_used, %llu bytes_reserved, "
1842 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1843 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1844 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1845 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1846 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1849 data_sinfo->bytes_may_use += bytes;
1850 BTRFS_I(inode)->reserved_bytes += bytes;
1851 spin_unlock(&data_sinfo->lock);
1853 return btrfs_check_metadata_free_space(root);
1857 * if there was an error for whatever reason after calling
1858 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1860 void btrfs_free_reserved_data_space(struct btrfs_root *root,
1861 struct inode *inode, u64 bytes)
1863 struct btrfs_space_info *data_sinfo;
1865 /* make sure bytes are sectorsize aligned */
1866 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1868 data_sinfo = BTRFS_I(inode)->space_info;
1869 spin_lock(&data_sinfo->lock);
1870 data_sinfo->bytes_may_use -= bytes;
1871 BTRFS_I(inode)->reserved_bytes -= bytes;
1872 spin_unlock(&data_sinfo->lock);
1875 /* called when we are adding a delalloc extent to the inode's io_tree */
1876 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1879 struct btrfs_space_info *data_sinfo;
1881 /* get the space info for where this inode will be storing its data */
1882 data_sinfo = BTRFS_I(inode)->space_info;
1884 /* make sure we have enough space to handle the data first */
1885 spin_lock(&data_sinfo->lock);
1886 data_sinfo->bytes_delalloc += bytes;
1889 * we are adding a delalloc extent without calling
1890 * btrfs_check_data_free_space first. This happens on a weird
1891 * writepage condition, but shouldn't hurt our accounting
1893 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1894 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1895 BTRFS_I(inode)->reserved_bytes = 0;
1897 data_sinfo->bytes_may_use -= bytes;
1898 BTRFS_I(inode)->reserved_bytes -= bytes;
1901 spin_unlock(&data_sinfo->lock);
1904 /* called when we are clearing an delalloc extent from the inode's io_tree */
1905 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1908 struct btrfs_space_info *info;
1910 info = BTRFS_I(inode)->space_info;
1912 spin_lock(&info->lock);
1913 info->bytes_delalloc -= bytes;
1914 spin_unlock(&info->lock);
1917 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1918 struct btrfs_root *extent_root, u64 alloc_bytes,
1919 u64 flags, int force)
1921 struct btrfs_space_info *space_info;
1925 mutex_lock(&extent_root->fs_info->chunk_mutex);
1927 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1929 space_info = __find_space_info(extent_root->fs_info, flags);
1931 ret = update_space_info(extent_root->fs_info, flags,
1935 BUG_ON(!space_info);
1937 spin_lock(&space_info->lock);
1938 if (space_info->force_alloc) {
1940 space_info->force_alloc = 0;
1942 if (space_info->full) {
1943 spin_unlock(&space_info->lock);
1947 thresh = space_info->total_bytes - space_info->bytes_readonly;
1948 thresh = div_factor(thresh, 6);
1950 (space_info->bytes_used + space_info->bytes_pinned +
1951 space_info->bytes_reserved + alloc_bytes) < thresh) {
1952 spin_unlock(&space_info->lock);
1955 spin_unlock(&space_info->lock);
1957 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1959 space_info->full = 1;
1961 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1965 static int update_block_group(struct btrfs_trans_handle *trans,
1966 struct btrfs_root *root,
1967 u64 bytenr, u64 num_bytes, int alloc,
1970 struct btrfs_block_group_cache *cache;
1971 struct btrfs_fs_info *info = root->fs_info;
1972 u64 total = num_bytes;
1977 cache = btrfs_lookup_block_group(info, bytenr);
1980 byte_in_group = bytenr - cache->key.objectid;
1981 WARN_ON(byte_in_group > cache->key.offset);
1983 spin_lock(&cache->space_info->lock);
1984 spin_lock(&cache->lock);
1986 old_val = btrfs_block_group_used(&cache->item);
1987 num_bytes = min(total, cache->key.offset - byte_in_group);
1989 old_val += num_bytes;
1990 cache->space_info->bytes_used += num_bytes;
1992 cache->space_info->bytes_readonly -= num_bytes;
1993 btrfs_set_block_group_used(&cache->item, old_val);
1994 spin_unlock(&cache->lock);
1995 spin_unlock(&cache->space_info->lock);
1997 old_val -= num_bytes;
1998 cache->space_info->bytes_used -= num_bytes;
2000 cache->space_info->bytes_readonly += num_bytes;
2001 btrfs_set_block_group_used(&cache->item, old_val);
2002 spin_unlock(&cache->lock);
2003 spin_unlock(&cache->space_info->lock);
2007 ret = btrfs_discard_extent(root, bytenr,
2011 ret = btrfs_add_free_space(cache, bytenr,
2016 put_block_group(cache);
2018 bytenr += num_bytes;
2023 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2025 struct btrfs_block_group_cache *cache;
2028 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2032 bytenr = cache->key.objectid;
2033 put_block_group(cache);
2038 int btrfs_update_pinned_extents(struct btrfs_root *root,
2039 u64 bytenr, u64 num, int pin)
2042 struct btrfs_block_group_cache *cache;
2043 struct btrfs_fs_info *fs_info = root->fs_info;
2045 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2047 set_extent_dirty(&fs_info->pinned_extents,
2048 bytenr, bytenr + num - 1, GFP_NOFS);
2050 clear_extent_dirty(&fs_info->pinned_extents,
2051 bytenr, bytenr + num - 1, GFP_NOFS);
2054 cache = btrfs_lookup_block_group(fs_info, bytenr);
2056 len = min(num, cache->key.offset -
2057 (bytenr - cache->key.objectid));
2059 spin_lock(&cache->space_info->lock);
2060 spin_lock(&cache->lock);
2061 cache->pinned += len;
2062 cache->space_info->bytes_pinned += len;
2063 spin_unlock(&cache->lock);
2064 spin_unlock(&cache->space_info->lock);
2065 fs_info->total_pinned += len;
2067 spin_lock(&cache->space_info->lock);
2068 spin_lock(&cache->lock);
2069 cache->pinned -= len;
2070 cache->space_info->bytes_pinned -= len;
2071 spin_unlock(&cache->lock);
2072 spin_unlock(&cache->space_info->lock);
2073 fs_info->total_pinned -= len;
2075 btrfs_add_free_space(cache, bytenr, len);
2077 put_block_group(cache);
2084 static int update_reserved_extents(struct btrfs_root *root,
2085 u64 bytenr, u64 num, int reserve)
2088 struct btrfs_block_group_cache *cache;
2089 struct btrfs_fs_info *fs_info = root->fs_info;
2092 cache = btrfs_lookup_block_group(fs_info, bytenr);
2094 len = min(num, cache->key.offset -
2095 (bytenr - cache->key.objectid));
2097 spin_lock(&cache->space_info->lock);
2098 spin_lock(&cache->lock);
2100 cache->reserved += len;
2101 cache->space_info->bytes_reserved += len;
2103 cache->reserved -= len;
2104 cache->space_info->bytes_reserved -= len;
2106 spin_unlock(&cache->lock);
2107 spin_unlock(&cache->space_info->lock);
2108 put_block_group(cache);
2115 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2120 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2123 mutex_lock(&root->fs_info->pinned_mutex);
2125 ret = find_first_extent_bit(pinned_extents, last,
2126 &start, &end, EXTENT_DIRTY);
2129 set_extent_dirty(copy, start, end, GFP_NOFS);
2132 mutex_unlock(&root->fs_info->pinned_mutex);
2136 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2137 struct btrfs_root *root,
2138 struct extent_io_tree *unpin)
2144 mutex_lock(&root->fs_info->pinned_mutex);
2146 ret = find_first_extent_bit(unpin, 0, &start, &end,
2151 ret = btrfs_discard_extent(root, start, end + 1 - start);
2153 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2154 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2156 if (need_resched()) {
2157 mutex_unlock(&root->fs_info->pinned_mutex);
2159 mutex_lock(&root->fs_info->pinned_mutex);
2162 mutex_unlock(&root->fs_info->pinned_mutex);
2166 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2167 struct btrfs_root *root,
2168 u64 bytenr, u64 num_bytes, int is_data)
2171 struct extent_buffer *buf;
2176 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2180 /* we can reuse a block if it hasn't been written
2181 * and it is from this transaction. We can't
2182 * reuse anything from the tree log root because
2183 * it has tiny sub-transactions.
2185 if (btrfs_buffer_uptodate(buf, 0) &&
2186 btrfs_try_tree_lock(buf)) {
2187 u64 header_owner = btrfs_header_owner(buf);
2188 u64 header_transid = btrfs_header_generation(buf);
2189 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2190 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2191 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2192 header_transid == trans->transid &&
2193 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2194 clean_tree_block(NULL, root, buf);
2195 btrfs_tree_unlock(buf);
2196 free_extent_buffer(buf);
2199 btrfs_tree_unlock(buf);
2201 free_extent_buffer(buf);
2203 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2210 * remove an extent from the root, returns 0 on success
2212 static int __free_extent(struct btrfs_trans_handle *trans,
2213 struct btrfs_root *root,
2214 u64 bytenr, u64 num_bytes, u64 parent,
2215 u64 root_objectid, u64 ref_generation,
2216 u64 owner_objectid, int pin, int mark_free,
2219 struct btrfs_path *path;
2220 struct btrfs_key key;
2221 struct btrfs_fs_info *info = root->fs_info;
2222 struct btrfs_root *extent_root = info->extent_root;
2223 struct extent_buffer *leaf;
2225 int extent_slot = 0;
2226 int found_extent = 0;
2228 struct btrfs_extent_item *ei;
2231 key.objectid = bytenr;
2232 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2233 key.offset = num_bytes;
2234 path = btrfs_alloc_path();
2239 ret = lookup_extent_backref(trans, extent_root, path,
2240 bytenr, parent, root_objectid,
2241 ref_generation, owner_objectid, 1);
2243 struct btrfs_key found_key;
2244 extent_slot = path->slots[0];
2245 while (extent_slot > 0) {
2247 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2249 if (found_key.objectid != bytenr)
2251 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2252 found_key.offset == num_bytes) {
2256 if (path->slots[0] - extent_slot > 5)
2259 if (!found_extent) {
2260 ret = remove_extent_backref(trans, extent_root, path,
2263 btrfs_release_path(extent_root, path);
2264 ret = btrfs_search_slot(trans, extent_root,
2267 printk(KERN_ERR "umm, got %d back from search"
2268 ", was looking for %llu\n", ret,
2269 (unsigned long long)bytenr);
2270 btrfs_print_leaf(extent_root, path->nodes[0]);
2273 extent_slot = path->slots[0];
2276 btrfs_print_leaf(extent_root, path->nodes[0]);
2278 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2279 "parent %llu root %llu gen %llu owner %llu\n",
2280 (unsigned long long)bytenr,
2281 (unsigned long long)parent,
2282 (unsigned long long)root_objectid,
2283 (unsigned long long)ref_generation,
2284 (unsigned long long)owner_objectid);
2287 leaf = path->nodes[0];
2288 ei = btrfs_item_ptr(leaf, extent_slot,
2289 struct btrfs_extent_item);
2290 refs = btrfs_extent_refs(leaf, ei);
2293 * we're not allowed to delete the extent item if there
2294 * are other delayed ref updates pending
2297 BUG_ON(refs < refs_to_drop);
2298 refs -= refs_to_drop;
2299 btrfs_set_extent_refs(leaf, ei, refs);
2300 btrfs_mark_buffer_dirty(leaf);
2302 if (refs == 0 && found_extent &&
2303 path->slots[0] == extent_slot + 1) {
2304 struct btrfs_extent_ref *ref;
2305 ref = btrfs_item_ptr(leaf, path->slots[0],
2306 struct btrfs_extent_ref);
2307 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
2308 /* if the back ref and the extent are next to each other
2309 * they get deleted below in one shot
2311 path->slots[0] = extent_slot;
2313 } else if (found_extent) {
2314 /* otherwise delete the extent back ref */
2315 ret = remove_extent_backref(trans, extent_root, path,
2318 /* if refs are 0, we need to setup the path for deletion */
2320 btrfs_release_path(extent_root, path);
2321 ret = btrfs_search_slot(trans, extent_root, &key, path,
2332 mutex_lock(&root->fs_info->pinned_mutex);
2333 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2334 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
2335 mutex_unlock(&root->fs_info->pinned_mutex);
2340 /* block accounting for super block */
2341 spin_lock(&info->delalloc_lock);
2342 super_used = btrfs_super_bytes_used(&info->super_copy);
2343 btrfs_set_super_bytes_used(&info->super_copy,
2344 super_used - num_bytes);
2346 /* block accounting for root item */
2347 root_used = btrfs_root_used(&root->root_item);
2348 btrfs_set_root_used(&root->root_item,
2349 root_used - num_bytes);
2350 spin_unlock(&info->delalloc_lock);
2351 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2354 btrfs_release_path(extent_root, path);
2356 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2357 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2361 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2365 btrfs_free_path(path);
2370 * remove an extent from the root, returns 0 on success
2372 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2373 struct btrfs_root *root,
2374 u64 bytenr, u64 num_bytes, u64 parent,
2375 u64 root_objectid, u64 ref_generation,
2376 u64 owner_objectid, int pin,
2379 WARN_ON(num_bytes < root->sectorsize);
2382 * if metadata always pin
2383 * if data pin when any transaction has committed this
2385 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2386 ref_generation != trans->transid)
2389 if (ref_generation != trans->transid)
2392 return __free_extent(trans, root, bytenr, num_bytes, parent,
2393 root_objectid, ref_generation,
2394 owner_objectid, pin, pin == 0, refs_to_drop);
2398 * when we free an extent, it is possible (and likely) that we free the last
2399 * delayed ref for that extent as well. This searches the delayed ref tree for
2400 * a given extent, and if there are no other delayed refs to be processed, it
2401 * removes it from the tree.
2403 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2404 struct btrfs_root *root, u64 bytenr)
2406 struct btrfs_delayed_ref_head *head;
2407 struct btrfs_delayed_ref_root *delayed_refs;
2408 struct btrfs_delayed_ref_node *ref;
2409 struct rb_node *node;
2412 delayed_refs = &trans->transaction->delayed_refs;
2413 spin_lock(&delayed_refs->lock);
2414 head = btrfs_find_delayed_ref_head(trans, bytenr);
2418 node = rb_prev(&head->node.rb_node);
2422 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2424 /* there are still entries for this ref, we can't drop it */
2425 if (ref->bytenr == bytenr)
2429 * waiting for the lock here would deadlock. If someone else has it
2430 * locked they are already in the process of dropping it anyway
2432 if (!mutex_trylock(&head->mutex))
2436 * at this point we have a head with no other entries. Go
2437 * ahead and process it.
2439 head->node.in_tree = 0;
2440 rb_erase(&head->node.rb_node, &delayed_refs->root);
2442 delayed_refs->num_entries--;
2445 * we don't take a ref on the node because we're removing it from the
2446 * tree, so we just steal the ref the tree was holding.
2448 delayed_refs->num_heads--;
2449 if (list_empty(&head->cluster))
2450 delayed_refs->num_heads_ready--;
2452 list_del_init(&head->cluster);
2453 spin_unlock(&delayed_refs->lock);
2455 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2456 &head->node, head->must_insert_reserved);
2458 btrfs_put_delayed_ref(&head->node);
2461 spin_unlock(&delayed_refs->lock);
2465 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2466 struct btrfs_root *root,
2467 u64 bytenr, u64 num_bytes, u64 parent,
2468 u64 root_objectid, u64 ref_generation,
2469 u64 owner_objectid, int pin)
2474 * tree log blocks never actually go into the extent allocation
2475 * tree, just update pinning info and exit early.
2477 * data extents referenced by the tree log do need to have
2478 * their reference counts bumped.
2480 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2481 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2482 mutex_lock(&root->fs_info->pinned_mutex);
2483 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2484 mutex_unlock(&root->fs_info->pinned_mutex);
2485 update_reserved_extents(root, bytenr, num_bytes, 0);
2488 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2489 root_objectid, ref_generation,
2491 BTRFS_DROP_DELAYED_REF, 1);
2493 ret = check_ref_cleanup(trans, root, bytenr);
2499 static u64 stripe_align(struct btrfs_root *root, u64 val)
2501 u64 mask = ((u64)root->stripesize - 1);
2502 u64 ret = (val + mask) & ~mask;
2507 * walks the btree of allocated extents and find a hole of a given size.
2508 * The key ins is changed to record the hole:
2509 * ins->objectid == block start
2510 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2511 * ins->offset == number of blocks
2512 * Any available blocks before search_start are skipped.
2514 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2515 struct btrfs_root *orig_root,
2516 u64 num_bytes, u64 empty_size,
2517 u64 search_start, u64 search_end,
2518 u64 hint_byte, struct btrfs_key *ins,
2519 u64 exclude_start, u64 exclude_nr,
2523 struct btrfs_root *root = orig_root->fs_info->extent_root;
2524 u64 total_needed = num_bytes;
2525 u64 *last_ptr = NULL;
2526 u64 last_wanted = 0;
2527 struct btrfs_block_group_cache *block_group = NULL;
2528 int chunk_alloc_done = 0;
2529 int empty_cluster = 2 * 1024 * 1024;
2530 int allowed_chunk_alloc = 0;
2531 struct list_head *head = NULL, *cur = NULL;
2534 struct btrfs_space_info *space_info;
2536 WARN_ON(num_bytes < root->sectorsize);
2537 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2541 if (orig_root->ref_cows || empty_size)
2542 allowed_chunk_alloc = 1;
2544 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2545 last_ptr = &root->fs_info->last_alloc;
2546 if (!btrfs_test_opt(root, SSD))
2547 empty_cluster = 64 * 1024;
2550 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2551 last_ptr = &root->fs_info->last_data_alloc;
2555 hint_byte = *last_ptr;
2556 last_wanted = *last_ptr;
2558 empty_size += empty_cluster;
2562 search_start = max(search_start, first_logical_byte(root, 0));
2563 search_start = max(search_start, hint_byte);
2565 if (last_wanted && search_start != last_wanted) {
2567 empty_size += empty_cluster;
2570 total_needed += empty_size;
2571 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2573 block_group = btrfs_lookup_first_block_group(root->fs_info,
2575 space_info = __find_space_info(root->fs_info, data);
2577 down_read(&space_info->groups_sem);
2579 struct btrfs_free_space *free_space;
2581 * the only way this happens if our hint points to a block
2582 * group thats not of the proper type, while looping this
2583 * should never happen
2589 goto new_group_no_lock;
2591 if (unlikely(!block_group->cached)) {
2592 mutex_lock(&block_group->cache_mutex);
2593 ret = cache_block_group(root, block_group);
2594 mutex_unlock(&block_group->cache_mutex);
2599 mutex_lock(&block_group->alloc_mutex);
2600 if (unlikely(!block_group_bits(block_group, data)))
2603 if (unlikely(block_group->ro))
2606 free_space = btrfs_find_free_space(block_group, search_start,
2609 u64 start = block_group->key.objectid;
2610 u64 end = block_group->key.objectid +
2611 block_group->key.offset;
2613 search_start = stripe_align(root, free_space->offset);
2615 /* move on to the next group */
2616 if (search_start + num_bytes >= search_end)
2619 /* move on to the next group */
2620 if (search_start + num_bytes > end)
2623 if (last_wanted && search_start != last_wanted) {
2624 total_needed += empty_cluster;
2625 empty_size += empty_cluster;
2628 * if search_start is still in this block group
2629 * then we just re-search this block group
2631 if (search_start >= start &&
2632 search_start < end) {
2633 mutex_unlock(&block_group->alloc_mutex);
2637 /* else we go to the next block group */
2641 if (exclude_nr > 0 &&
2642 (search_start + num_bytes > exclude_start &&
2643 search_start < exclude_start + exclude_nr)) {
2644 search_start = exclude_start + exclude_nr;
2646 * if search_start is still in this block group
2647 * then we just re-search this block group
2649 if (search_start >= start &&
2650 search_start < end) {
2651 mutex_unlock(&block_group->alloc_mutex);
2656 /* else we go to the next block group */
2660 ins->objectid = search_start;
2661 ins->offset = num_bytes;
2663 btrfs_remove_free_space_lock(block_group, search_start,
2665 /* we are all good, lets return */
2666 mutex_unlock(&block_group->alloc_mutex);
2670 mutex_unlock(&block_group->alloc_mutex);
2671 put_block_group(block_group);
2674 /* don't try to compare new allocations against the
2675 * last allocation any more
2680 * Here's how this works.
2681 * loop == 0: we were searching a block group via a hint
2682 * and didn't find anything, so we start at
2683 * the head of the block groups and keep searching
2684 * loop == 1: we're searching through all of the block groups
2685 * if we hit the head again we have searched
2686 * all of the block groups for this space and we
2687 * need to try and allocate, if we cant error out.
2688 * loop == 2: we allocated more space and are looping through
2689 * all of the block groups again.
2692 head = &space_info->block_groups;
2695 } else if (loop == 1 && cur == head) {
2698 /* at this point we give up on the empty_size
2699 * allocations and just try to allocate the min
2702 * The extra_loop field was set if an empty_size
2703 * allocation was attempted above, and if this
2704 * is try we need to try the loop again without
2705 * the additional empty_size.
2707 total_needed -= empty_size;
2709 keep_going = extra_loop;
2712 if (allowed_chunk_alloc && !chunk_alloc_done) {
2713 up_read(&space_info->groups_sem);
2714 ret = do_chunk_alloc(trans, root, num_bytes +
2715 2 * 1024 * 1024, data, 1);
2716 down_read(&space_info->groups_sem);
2719 head = &space_info->block_groups;
2721 * we've allocated a new chunk, keep
2725 chunk_alloc_done = 1;
2726 } else if (!allowed_chunk_alloc) {
2727 space_info->force_alloc = 1;
2736 } else if (cur == head) {
2740 block_group = list_entry(cur, struct btrfs_block_group_cache,
2742 atomic_inc(&block_group->count);
2744 search_start = block_group->key.objectid;
2748 /* we found what we needed */
2749 if (ins->objectid) {
2750 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2751 trans->block_group = block_group->key.objectid;
2754 *last_ptr = ins->objectid + ins->offset;
2757 printk(KERN_ERR "btrfs searching for %llu bytes, "
2758 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2759 (unsigned long long)total_needed,
2760 (unsigned long long)num_bytes,
2761 loop, allowed_chunk_alloc);
2765 put_block_group(block_group);
2767 up_read(&space_info->groups_sem);
2771 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2773 struct btrfs_block_group_cache *cache;
2775 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2776 (unsigned long long)(info->total_bytes - info->bytes_used -
2777 info->bytes_pinned - info->bytes_reserved),
2778 (info->full) ? "" : "not ");
2779 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2780 " may_use=%llu, used=%llu\n", info->total_bytes,
2781 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2784 down_read(&info->groups_sem);
2785 list_for_each_entry(cache, &info->block_groups, list) {
2786 spin_lock(&cache->lock);
2787 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2788 "%llu pinned %llu reserved\n",
2789 (unsigned long long)cache->key.objectid,
2790 (unsigned long long)cache->key.offset,
2791 (unsigned long long)btrfs_block_group_used(&cache->item),
2792 (unsigned long long)cache->pinned,
2793 (unsigned long long)cache->reserved);
2794 btrfs_dump_free_space(cache, bytes);
2795 spin_unlock(&cache->lock);
2797 up_read(&info->groups_sem);
2800 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2801 struct btrfs_root *root,
2802 u64 num_bytes, u64 min_alloc_size,
2803 u64 empty_size, u64 hint_byte,
2804 u64 search_end, struct btrfs_key *ins,
2808 u64 search_start = 0;
2809 struct btrfs_fs_info *info = root->fs_info;
2811 data = btrfs_get_alloc_profile(root, data);
2814 * the only place that sets empty_size is btrfs_realloc_node, which
2815 * is not called recursively on allocations
2817 if (empty_size || root->ref_cows) {
2818 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2819 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2821 BTRFS_BLOCK_GROUP_METADATA |
2822 (info->metadata_alloc_profile &
2823 info->avail_metadata_alloc_bits), 0);
2825 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2826 num_bytes + 2 * 1024 * 1024, data, 0);
2829 WARN_ON(num_bytes < root->sectorsize);
2830 ret = find_free_extent(trans, root, num_bytes, empty_size,
2831 search_start, search_end, hint_byte, ins,
2832 trans->alloc_exclude_start,
2833 trans->alloc_exclude_nr, data);
2835 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2836 num_bytes = num_bytes >> 1;
2837 num_bytes = num_bytes & ~(root->sectorsize - 1);
2838 num_bytes = max(num_bytes, min_alloc_size);
2839 do_chunk_alloc(trans, root->fs_info->extent_root,
2840 num_bytes, data, 1);
2844 struct btrfs_space_info *sinfo;
2846 sinfo = __find_space_info(root->fs_info, data);
2847 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2848 "wanted %llu\n", (unsigned long long)data,
2849 (unsigned long long)num_bytes);
2850 dump_space_info(sinfo, num_bytes);
2857 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2859 struct btrfs_block_group_cache *cache;
2862 cache = btrfs_lookup_block_group(root->fs_info, start);
2864 printk(KERN_ERR "Unable to find block group for %llu\n",
2865 (unsigned long long)start);
2869 ret = btrfs_discard_extent(root, start, len);
2871 btrfs_add_free_space(cache, start, len);
2872 put_block_group(cache);
2873 update_reserved_extents(root, start, len, 0);
2878 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root,
2880 u64 num_bytes, u64 min_alloc_size,
2881 u64 empty_size, u64 hint_byte,
2882 u64 search_end, struct btrfs_key *ins,
2886 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2887 empty_size, hint_byte, search_end, ins,
2889 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2893 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2894 struct btrfs_root *root, u64 parent,
2895 u64 root_objectid, u64 ref_generation,
2896 u64 owner, struct btrfs_key *ins,
2902 u64 num_bytes = ins->offset;
2904 struct btrfs_fs_info *info = root->fs_info;
2905 struct btrfs_root *extent_root = info->extent_root;
2906 struct btrfs_extent_item *extent_item;
2907 struct btrfs_extent_ref *ref;
2908 struct btrfs_path *path;
2909 struct btrfs_key keys[2];
2912 parent = ins->objectid;
2914 /* block accounting for super block */
2915 spin_lock(&info->delalloc_lock);
2916 super_used = btrfs_super_bytes_used(&info->super_copy);
2917 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2919 /* block accounting for root item */
2920 root_used = btrfs_root_used(&root->root_item);
2921 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2922 spin_unlock(&info->delalloc_lock);
2924 memcpy(&keys[0], ins, sizeof(*ins));
2925 keys[1].objectid = ins->objectid;
2926 keys[1].type = BTRFS_EXTENT_REF_KEY;
2927 keys[1].offset = parent;
2928 sizes[0] = sizeof(*extent_item);
2929 sizes[1] = sizeof(*ref);
2931 path = btrfs_alloc_path();
2934 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2938 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2939 struct btrfs_extent_item);
2940 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
2941 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2942 struct btrfs_extent_ref);
2944 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2945 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2946 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2947 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
2949 btrfs_mark_buffer_dirty(path->nodes[0]);
2951 trans->alloc_exclude_start = 0;
2952 trans->alloc_exclude_nr = 0;
2953 btrfs_free_path(path);
2958 ret = update_block_group(trans, root, ins->objectid,
2961 printk(KERN_ERR "btrfs update block group failed for %llu "
2962 "%llu\n", (unsigned long long)ins->objectid,
2963 (unsigned long long)ins->offset);
2970 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2971 struct btrfs_root *root, u64 parent,
2972 u64 root_objectid, u64 ref_generation,
2973 u64 owner, struct btrfs_key *ins)
2977 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2980 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2981 ins->offset, parent, root_objectid,
2982 ref_generation, owner,
2983 BTRFS_ADD_DELAYED_EXTENT, 0);
2989 * this is used by the tree logging recovery code. It records that
2990 * an extent has been allocated and makes sure to clear the free
2991 * space cache bits as well
2993 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2994 struct btrfs_root *root, u64 parent,
2995 u64 root_objectid, u64 ref_generation,
2996 u64 owner, struct btrfs_key *ins)
2999 struct btrfs_block_group_cache *block_group;
3001 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3002 mutex_lock(&block_group->cache_mutex);
3003 cache_block_group(root, block_group);
3004 mutex_unlock(&block_group->cache_mutex);
3006 ret = btrfs_remove_free_space(block_group, ins->objectid,
3009 put_block_group(block_group);
3010 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3011 ref_generation, owner, ins, 1);
3016 * finds a free extent and does all the dirty work required for allocation
3017 * returns the key for the extent through ins, and a tree buffer for
3018 * the first block of the extent through buf.
3020 * returns 0 if everything worked, non-zero otherwise.
3022 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3023 struct btrfs_root *root,
3024 u64 num_bytes, u64 parent, u64 min_alloc_size,
3025 u64 root_objectid, u64 ref_generation,
3026 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3027 u64 search_end, struct btrfs_key *ins, u64 data)
3030 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3031 min_alloc_size, empty_size, hint_byte,
3032 search_end, ins, data);
3034 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3035 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3036 ins->offset, parent, root_objectid,
3037 ref_generation, owner_objectid,
3038 BTRFS_ADD_DELAYED_EXTENT, 0);
3041 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3045 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3046 struct btrfs_root *root,
3047 u64 bytenr, u32 blocksize,
3050 struct extent_buffer *buf;
3052 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3054 return ERR_PTR(-ENOMEM);
3055 btrfs_set_header_generation(buf, trans->transid);
3056 btrfs_set_buffer_lockdep_class(buf, level);
3057 btrfs_tree_lock(buf);
3058 clean_tree_block(trans, root, buf);
3060 btrfs_set_lock_blocking(buf);
3061 btrfs_set_buffer_uptodate(buf);
3063 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3064 set_extent_dirty(&root->dirty_log_pages, buf->start,
3065 buf->start + buf->len - 1, GFP_NOFS);
3067 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3068 buf->start + buf->len - 1, GFP_NOFS);
3070 trans->blocks_used++;
3071 /* this returns a buffer locked for blocking */
3076 * helper function to allocate a block for a given tree
3077 * returns the tree buffer or NULL.
3079 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 u32 blocksize, u64 parent,
3088 struct btrfs_key ins;
3090 struct extent_buffer *buf;
3092 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3093 root_objectid, ref_generation, level,
3094 empty_size, hint, (u64)-1, &ins, 0);
3097 return ERR_PTR(ret);
3100 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3105 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3106 struct btrfs_root *root, struct extent_buffer *leaf)
3109 u64 leaf_generation;
3110 struct refsort *sorted;
3111 struct btrfs_key key;
3112 struct btrfs_file_extent_item *fi;
3119 BUG_ON(!btrfs_is_leaf(leaf));
3120 nritems = btrfs_header_nritems(leaf);
3121 leaf_owner = btrfs_header_owner(leaf);
3122 leaf_generation = btrfs_header_generation(leaf);
3124 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3125 /* we do this loop twice. The first time we build a list
3126 * of the extents we have a reference on, then we sort the list
3127 * by bytenr. The second time around we actually do the
3130 for (i = 0; i < nritems; i++) {
3134 btrfs_item_key_to_cpu(leaf, &key, i);
3136 /* only extents have references, skip everything else */
3137 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3140 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3142 /* inline extents live in the btree, they don't have refs */
3143 if (btrfs_file_extent_type(leaf, fi) ==
3144 BTRFS_FILE_EXTENT_INLINE)
3147 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3149 /* holes don't have refs */
3150 if (disk_bytenr == 0)
3153 sorted[refi].bytenr = disk_bytenr;
3154 sorted[refi].slot = i;
3161 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3163 for (i = 0; i < refi; i++) {
3166 disk_bytenr = sorted[i].bytenr;
3167 slot = sorted[i].slot;
3171 btrfs_item_key_to_cpu(leaf, &key, slot);
3172 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3175 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3177 ret = btrfs_free_extent(trans, root, disk_bytenr,
3178 btrfs_file_extent_disk_num_bytes(leaf, fi),
3179 leaf->start, leaf_owner, leaf_generation,
3183 atomic_inc(&root->fs_info->throttle_gen);
3184 wake_up(&root->fs_info->transaction_throttle);
3192 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3193 struct btrfs_root *root,
3194 struct btrfs_leaf_ref *ref)
3198 struct btrfs_extent_info *info;
3199 struct refsort *sorted;
3201 if (ref->nritems == 0)
3204 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
3205 for (i = 0; i < ref->nritems; i++) {
3206 sorted[i].bytenr = ref->extents[i].bytenr;
3209 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3212 * the items in the ref were sorted when the ref was inserted
3213 * into the ref cache, so this is already in order
3215 for (i = 0; i < ref->nritems; i++) {
3216 info = ref->extents + sorted[i].slot;
3217 ret = btrfs_free_extent(trans, root, info->bytenr,
3218 info->num_bytes, ref->bytenr,
3219 ref->owner, ref->generation,
3222 atomic_inc(&root->fs_info->throttle_gen);
3223 wake_up(&root->fs_info->transaction_throttle);
3234 static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3235 struct btrfs_root *root, u64 start,
3240 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
3243 #if 0 /* some debugging code in case we see problems here */
3244 /* if the refs count is one, it won't get increased again. But
3245 * if the ref count is > 1, someone may be decreasing it at
3246 * the same time we are.
3249 struct extent_buffer *eb = NULL;
3250 eb = btrfs_find_create_tree_block(root, start, len);
3252 btrfs_tree_lock(eb);
3254 mutex_lock(&root->fs_info->alloc_mutex);
3255 ret = lookup_extent_ref(NULL, root, start, len, refs);
3257 mutex_unlock(&root->fs_info->alloc_mutex);
3260 btrfs_tree_unlock(eb);
3261 free_extent_buffer(eb);
3264 printk(KERN_ERR "btrfs block %llu went down to one "
3265 "during drop_snap\n", (unsigned long long)start);
3276 * this is used while deleting old snapshots, and it drops the refs
3277 * on a whole subtree starting from a level 1 node.
3279 * The idea is to sort all the leaf pointers, and then drop the
3280 * ref on all the leaves in order. Most of the time the leaves
3281 * will have ref cache entries, so no leaf IOs will be required to
3282 * find the extents they have references on.
3284 * For each leaf, any references it has are also dropped in order
3286 * This ends up dropping the references in something close to optimal
3287 * order for reading and modifying the extent allocation tree.
3289 static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3290 struct btrfs_root *root,
3291 struct btrfs_path *path)
3296 struct extent_buffer *eb = path->nodes[1];
3297 struct extent_buffer *leaf;
3298 struct btrfs_leaf_ref *ref;
3299 struct refsort *sorted = NULL;
3300 int nritems = btrfs_header_nritems(eb);
3304 int slot = path->slots[1];
3305 u32 blocksize = btrfs_level_size(root, 0);
3311 root_owner = btrfs_header_owner(eb);
3312 root_gen = btrfs_header_generation(eb);
3313 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3316 * step one, sort all the leaf pointers so we don't scribble
3317 * randomly into the extent allocation tree
3319 for (i = slot; i < nritems; i++) {
3320 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3321 sorted[refi].slot = i;
3326 * nritems won't be zero, but if we're picking up drop_snapshot
3327 * after a crash, slot might be > 0, so double check things
3333 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3336 * the first loop frees everything the leaves point to
3338 for (i = 0; i < refi; i++) {
3341 bytenr = sorted[i].bytenr;
3344 * check the reference count on this leaf. If it is > 1
3345 * we just decrement it below and don't update any
3346 * of the refs the leaf points to.
3348 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3354 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3357 * the leaf only had one reference, which means the
3358 * only thing pointing to this leaf is the snapshot
3359 * we're deleting. It isn't possible for the reference
3360 * count to increase again later
3362 * The reference cache is checked for the leaf,
3363 * and if found we'll be able to drop any refs held by
3364 * the leaf without needing to read it in.
3366 ref = btrfs_lookup_leaf_ref(root, bytenr);
3367 if (ref && ref->generation != ptr_gen) {
3368 btrfs_free_leaf_ref(root, ref);
3372 ret = cache_drop_leaf_ref(trans, root, ref);
3374 btrfs_remove_leaf_ref(root, ref);
3375 btrfs_free_leaf_ref(root, ref);
3378 * the leaf wasn't in the reference cache, so
3379 * we have to read it.
3381 leaf = read_tree_block(root, bytenr, blocksize,
3383 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3385 free_extent_buffer(leaf);
3387 atomic_inc(&root->fs_info->throttle_gen);
3388 wake_up(&root->fs_info->transaction_throttle);
3393 * run through the loop again to free the refs on the leaves.
3394 * This is faster than doing it in the loop above because
3395 * the leaves are likely to be clustered together. We end up
3396 * working in nice chunks on the extent allocation tree.
3398 for (i = 0; i < refi; i++) {
3399 bytenr = sorted[i].bytenr;
3400 ret = btrfs_free_extent(trans, root, bytenr,
3401 blocksize, eb->start,
3402 root_owner, root_gen, 0, 1);
3405 atomic_inc(&root->fs_info->throttle_gen);
3406 wake_up(&root->fs_info->transaction_throttle);
3413 * update the path to show we've processed the entire level 1
3414 * node. This will get saved into the root's drop_snapshot_progress
3415 * field so these drops are not repeated again if this transaction
3418 path->slots[1] = nritems;
3423 * helper function for drop_snapshot, this walks down the tree dropping ref
3424 * counts as it goes.
3426 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3427 struct btrfs_root *root,
3428 struct btrfs_path *path, int *level)
3434 struct extent_buffer *next;
3435 struct extent_buffer *cur;
3436 struct extent_buffer *parent;
3441 WARN_ON(*level < 0);
3442 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3443 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
3444 path->nodes[*level]->len, &refs);
3450 * walk down to the last node level and free all the leaves
3452 while (*level >= 0) {
3453 WARN_ON(*level < 0);
3454 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3455 cur = path->nodes[*level];
3457 if (btrfs_header_level(cur) != *level)
3460 if (path->slots[*level] >=
3461 btrfs_header_nritems(cur))
3464 /* the new code goes down to level 1 and does all the
3465 * leaves pointed to that node in bulk. So, this check
3466 * for level 0 will always be false.
3468 * But, the disk format allows the drop_snapshot_progress
3469 * field in the root to leave things in a state where
3470 * a leaf will need cleaning up here. If someone crashes
3471 * with the old code and then boots with the new code,
3472 * we might find a leaf here.
3475 ret = btrfs_drop_leaf_ref(trans, root, cur);
3481 * once we get to level one, process the whole node
3482 * at once, including everything below it.
3485 ret = drop_level_one_refs(trans, root, path);
3490 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3491 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3492 blocksize = btrfs_level_size(root, *level - 1);
3494 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3499 * if there is more than one reference, we don't need
3500 * to read that node to drop any references it has. We
3501 * just drop the ref we hold on that node and move on to the
3502 * next slot in this level.
3505 parent = path->nodes[*level];
3506 root_owner = btrfs_header_owner(parent);
3507 root_gen = btrfs_header_generation(parent);
3508 path->slots[*level]++;
3510 ret = btrfs_free_extent(trans, root, bytenr,
3511 blocksize, parent->start,
3512 root_owner, root_gen,
3516 atomic_inc(&root->fs_info->throttle_gen);
3517 wake_up(&root->fs_info->transaction_throttle);
3524 * we need to keep freeing things in the next level down.
3525 * read the block and loop around to process it
3527 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3528 WARN_ON(*level <= 0);
3529 if (path->nodes[*level-1])
3530 free_extent_buffer(path->nodes[*level-1]);
3531 path->nodes[*level-1] = next;
3532 *level = btrfs_header_level(next);
3533 path->slots[*level] = 0;
3537 WARN_ON(*level < 0);
3538 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3540 if (path->nodes[*level] == root->node) {
3541 parent = path->nodes[*level];
3542 bytenr = path->nodes[*level]->start;
3544 parent = path->nodes[*level + 1];
3545 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3548 blocksize = btrfs_level_size(root, *level);
3549 root_owner = btrfs_header_owner(parent);
3550 root_gen = btrfs_header_generation(parent);
3553 * cleanup and free the reference on the last node
3556 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3557 parent->start, root_owner, root_gen,
3559 free_extent_buffer(path->nodes[*level]);
3560 path->nodes[*level] = NULL;
3570 * helper function for drop_subtree, this function is similar to
3571 * walk_down_tree. The main difference is that it checks reference
3572 * counts while tree blocks are locked.
3574 static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3575 struct btrfs_root *root,
3576 struct btrfs_path *path, int *level)
3578 struct extent_buffer *next;
3579 struct extent_buffer *cur;
3580 struct extent_buffer *parent;
3587 cur = path->nodes[*level];
3588 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3594 while (*level >= 0) {
3595 cur = path->nodes[*level];
3597 ret = btrfs_drop_leaf_ref(trans, root, cur);
3599 clean_tree_block(trans, root, cur);
3602 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3603 clean_tree_block(trans, root, cur);
3607 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3608 blocksize = btrfs_level_size(root, *level - 1);
3609 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3611 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3612 btrfs_tree_lock(next);
3613 btrfs_set_lock_blocking(next);
3615 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3619 parent = path->nodes[*level];
3620 ret = btrfs_free_extent(trans, root, bytenr,
3621 blocksize, parent->start,
3622 btrfs_header_owner(parent),
3623 btrfs_header_generation(parent),
3626 path->slots[*level]++;
3627 btrfs_tree_unlock(next);
3628 free_extent_buffer(next);
3632 *level = btrfs_header_level(next);
3633 path->nodes[*level] = next;
3634 path->slots[*level] = 0;
3635 path->locks[*level] = 1;
3639 parent = path->nodes[*level + 1];
3640 bytenr = path->nodes[*level]->start;
3641 blocksize = path->nodes[*level]->len;
3643 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3644 parent->start, btrfs_header_owner(parent),
3645 btrfs_header_generation(parent), *level, 1);
3648 if (path->locks[*level]) {
3649 btrfs_tree_unlock(path->nodes[*level]);
3650 path->locks[*level] = 0;
3652 free_extent_buffer(path->nodes[*level]);
3653 path->nodes[*level] = NULL;
3660 * helper for dropping snapshots. This walks back up the tree in the path
3661 * to find the first node higher up where we haven't yet gone through
3664 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
3665 struct btrfs_root *root,
3666 struct btrfs_path *path,
3667 int *level, int max_level)
3671 struct btrfs_root_item *root_item = &root->root_item;
3676 for (i = *level; i < max_level && path->nodes[i]; i++) {
3677 slot = path->slots[i];
3678 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3679 struct extent_buffer *node;
3680 struct btrfs_disk_key disk_key;
3683 * there is more work to do in this level.
3684 * Update the drop_progress marker to reflect
3685 * the work we've done so far, and then bump
3688 node = path->nodes[i];
3691 WARN_ON(*level == 0);
3692 btrfs_node_key(node, &disk_key, path->slots[i]);
3693 memcpy(&root_item->drop_progress,
3694 &disk_key, sizeof(disk_key));
3695 root_item->drop_level = i;
3698 struct extent_buffer *parent;
3701 * this whole node is done, free our reference
3702 * on it and go up one level
3704 if (path->nodes[*level] == root->node)
3705 parent = path->nodes[*level];
3707 parent = path->nodes[*level + 1];
3709 root_owner = btrfs_header_owner(parent);
3710 root_gen = btrfs_header_generation(parent);
3712 clean_tree_block(trans, root, path->nodes[*level]);
3713 ret = btrfs_free_extent(trans, root,
3714 path->nodes[*level]->start,
3715 path->nodes[*level]->len,
3716 parent->start, root_owner,
3717 root_gen, *level, 1);
3719 if (path->locks[*level]) {
3720 btrfs_tree_unlock(path->nodes[*level]);
3721 path->locks[*level] = 0;
3723 free_extent_buffer(path->nodes[*level]);
3724 path->nodes[*level] = NULL;
3732 * drop the reference count on the tree rooted at 'snap'. This traverses
3733 * the tree freeing any blocks that have a ref count of zero after being
3736 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3742 struct btrfs_path *path;
3746 struct btrfs_root_item *root_item = &root->root_item;
3748 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3749 path = btrfs_alloc_path();
3752 level = btrfs_header_level(root->node);
3754 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3755 path->nodes[level] = root->node;
3756 extent_buffer_get(root->node);
3757 path->slots[level] = 0;
3759 struct btrfs_key key;
3760 struct btrfs_disk_key found_key;
3761 struct extent_buffer *node;
3763 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3764 level = root_item->drop_level;
3765 path->lowest_level = level;
3766 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3771 node = path->nodes[level];
3772 btrfs_node_key(node, &found_key, path->slots[level]);
3773 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3774 sizeof(found_key)));
3776 * unlock our path, this is safe because only this
3777 * function is allowed to delete this snapshot
3779 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3780 if (path->nodes[i] && path->locks[i]) {
3782 btrfs_tree_unlock(path->nodes[i]);
3787 unsigned long update;
3788 wret = walk_down_tree(trans, root, path, &level);
3794 wret = walk_up_tree(trans, root, path, &level,
3800 if (trans->transaction->in_commit ||
3801 trans->transaction->delayed_refs.flushing) {
3805 atomic_inc(&root->fs_info->throttle_gen);
3806 wake_up(&root->fs_info->transaction_throttle);
3807 for (update_count = 0; update_count < 16; update_count++) {
3808 update = trans->delayed_ref_updates;
3809 trans->delayed_ref_updates = 0;
3811 btrfs_run_delayed_refs(trans, root, update);
3816 for (i = 0; i <= orig_level; i++) {
3817 if (path->nodes[i]) {
3818 free_extent_buffer(path->nodes[i]);
3819 path->nodes[i] = NULL;
3823 btrfs_free_path(path);
3827 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3828 struct btrfs_root *root,
3829 struct extent_buffer *node,
3830 struct extent_buffer *parent)
3832 struct btrfs_path *path;
3838 path = btrfs_alloc_path();
3841 btrfs_assert_tree_locked(parent);
3842 parent_level = btrfs_header_level(parent);
3843 extent_buffer_get(parent);
3844 path->nodes[parent_level] = parent;
3845 path->slots[parent_level] = btrfs_header_nritems(parent);
3847 btrfs_assert_tree_locked(node);
3848 level = btrfs_header_level(node);
3849 extent_buffer_get(node);
3850 path->nodes[level] = node;
3851 path->slots[level] = 0;
3854 wret = walk_down_subtree(trans, root, path, &level);
3860 wret = walk_up_tree(trans, root, path, &level, parent_level);
3867 btrfs_free_path(path);
3871 static unsigned long calc_ra(unsigned long start, unsigned long last,
3874 return min(last, start + nr - 1);
3877 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
3882 unsigned long first_index;
3883 unsigned long last_index;
3886 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3887 struct file_ra_state *ra;
3888 struct btrfs_ordered_extent *ordered;
3889 unsigned int total_read = 0;
3890 unsigned int total_dirty = 0;
3893 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3895 mutex_lock(&inode->i_mutex);
3896 first_index = start >> PAGE_CACHE_SHIFT;
3897 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3899 /* make sure the dirty trick played by the caller work */
3900 ret = invalidate_inode_pages2_range(inode->i_mapping,
3901 first_index, last_index);
3905 file_ra_state_init(ra, inode->i_mapping);
3907 for (i = first_index ; i <= last_index; i++) {
3908 if (total_read % ra->ra_pages == 0) {
3909 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3910 calc_ra(i, last_index, ra->ra_pages));
3914 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3916 page = grab_cache_page(inode->i_mapping, i);
3921 if (!PageUptodate(page)) {
3922 btrfs_readpage(NULL, page);
3924 if (!PageUptodate(page)) {
3926 page_cache_release(page);
3931 wait_on_page_writeback(page);
3933 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3934 page_end = page_start + PAGE_CACHE_SIZE - 1;
3935 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3937 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3939 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3941 page_cache_release(page);
3942 btrfs_start_ordered_extent(inode, ordered, 1);
3943 btrfs_put_ordered_extent(ordered);
3946 set_page_extent_mapped(page);
3948 if (i == first_index)
3949 set_extent_bits(io_tree, page_start, page_end,
3950 EXTENT_BOUNDARY, GFP_NOFS);
3951 btrfs_set_extent_delalloc(inode, page_start, page_end);
3953 set_page_dirty(page);
3956 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3958 page_cache_release(page);
3963 mutex_unlock(&inode->i_mutex);
3964 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3968 static noinline int relocate_data_extent(struct inode *reloc_inode,
3969 struct btrfs_key *extent_key,
3972 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3973 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3974 struct extent_map *em;
3975 u64 start = extent_key->objectid - offset;
3976 u64 end = start + extent_key->offset - 1;
3978 em = alloc_extent_map(GFP_NOFS);
3979 BUG_ON(!em || IS_ERR(em));
3982 em->len = extent_key->offset;
3983 em->block_len = extent_key->offset;
3984 em->block_start = extent_key->objectid;
3985 em->bdev = root->fs_info->fs_devices->latest_bdev;
3986 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3988 /* setup extent map to cheat btrfs_readpage */
3989 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
3992 spin_lock(&em_tree->lock);
3993 ret = add_extent_mapping(em_tree, em);
3994 spin_unlock(&em_tree->lock);
3995 if (ret != -EEXIST) {
3996 free_extent_map(em);
3999 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4001 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4003 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4006 struct btrfs_ref_path {
4008 u64 nodes[BTRFS_MAX_LEVEL];
4010 u64 root_generation;
4017 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4018 u64 new_nodes[BTRFS_MAX_LEVEL];
4021 struct disk_extent {
4032 static int is_cowonly_root(u64 root_objectid)
4034 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4035 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4036 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4037 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4038 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4039 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
4044 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
4045 struct btrfs_root *extent_root,
4046 struct btrfs_ref_path *ref_path,
4049 struct extent_buffer *leaf;
4050 struct btrfs_path *path;
4051 struct btrfs_extent_ref *ref;
4052 struct btrfs_key key;
4053 struct btrfs_key found_key;
4059 path = btrfs_alloc_path();
4064 ref_path->lowest_level = -1;
4065 ref_path->current_level = -1;
4066 ref_path->shared_level = -1;
4070 level = ref_path->current_level - 1;
4071 while (level >= -1) {
4073 if (level < ref_path->lowest_level)
4077 bytenr = ref_path->nodes[level];
4079 bytenr = ref_path->extent_start;
4080 BUG_ON(bytenr == 0);
4082 parent = ref_path->nodes[level + 1];
4083 ref_path->nodes[level + 1] = 0;
4084 ref_path->current_level = level;
4085 BUG_ON(parent == 0);
4087 key.objectid = bytenr;
4088 key.offset = parent + 1;
4089 key.type = BTRFS_EXTENT_REF_KEY;
4091 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4096 leaf = path->nodes[0];
4097 nritems = btrfs_header_nritems(leaf);
4098 if (path->slots[0] >= nritems) {
4099 ret = btrfs_next_leaf(extent_root, path);
4104 leaf = path->nodes[0];
4107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4108 if (found_key.objectid == bytenr &&
4109 found_key.type == BTRFS_EXTENT_REF_KEY) {
4110 if (level < ref_path->shared_level)
4111 ref_path->shared_level = level;
4116 btrfs_release_path(extent_root, path);
4119 /* reached lowest level */
4123 level = ref_path->current_level;
4124 while (level < BTRFS_MAX_LEVEL - 1) {
4128 bytenr = ref_path->nodes[level];
4130 bytenr = ref_path->extent_start;
4132 BUG_ON(bytenr == 0);
4134 key.objectid = bytenr;
4136 key.type = BTRFS_EXTENT_REF_KEY;
4138 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4142 leaf = path->nodes[0];
4143 nritems = btrfs_header_nritems(leaf);
4144 if (path->slots[0] >= nritems) {
4145 ret = btrfs_next_leaf(extent_root, path);
4149 /* the extent was freed by someone */
4150 if (ref_path->lowest_level == level)
4152 btrfs_release_path(extent_root, path);
4155 leaf = path->nodes[0];
4158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4159 if (found_key.objectid != bytenr ||
4160 found_key.type != BTRFS_EXTENT_REF_KEY) {
4161 /* the extent was freed by someone */
4162 if (ref_path->lowest_level == level) {
4166 btrfs_release_path(extent_root, path);
4170 ref = btrfs_item_ptr(leaf, path->slots[0],
4171 struct btrfs_extent_ref);
4172 ref_objectid = btrfs_ref_objectid(leaf, ref);
4173 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4175 level = (int)ref_objectid;
4176 BUG_ON(level >= BTRFS_MAX_LEVEL);
4177 ref_path->lowest_level = level;
4178 ref_path->current_level = level;
4179 ref_path->nodes[level] = bytenr;
4181 WARN_ON(ref_objectid != level);
4184 WARN_ON(level != -1);
4188 if (ref_path->lowest_level == level) {
4189 ref_path->owner_objectid = ref_objectid;
4190 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4194 * the block is tree root or the block isn't in reference
4197 if (found_key.objectid == found_key.offset ||
4198 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4199 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4200 ref_path->root_generation =
4201 btrfs_ref_generation(leaf, ref);
4203 /* special reference from the tree log */
4204 ref_path->nodes[0] = found_key.offset;
4205 ref_path->current_level = 0;
4212 BUG_ON(ref_path->nodes[level] != 0);
4213 ref_path->nodes[level] = found_key.offset;
4214 ref_path->current_level = level;
4217 * the reference was created in the running transaction,
4218 * no need to continue walking up.
4220 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4221 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4222 ref_path->root_generation =
4223 btrfs_ref_generation(leaf, ref);
4228 btrfs_release_path(extent_root, path);
4231 /* reached max tree level, but no tree root found. */
4234 btrfs_free_path(path);
4238 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4239 struct btrfs_root *extent_root,
4240 struct btrfs_ref_path *ref_path,
4243 memset(ref_path, 0, sizeof(*ref_path));
4244 ref_path->extent_start = extent_start;
4246 return __next_ref_path(trans, extent_root, ref_path, 1);
4249 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4250 struct btrfs_root *extent_root,
4251 struct btrfs_ref_path *ref_path)
4253 return __next_ref_path(trans, extent_root, ref_path, 0);
4256 static noinline int get_new_locations(struct inode *reloc_inode,
4257 struct btrfs_key *extent_key,
4258 u64 offset, int no_fragment,
4259 struct disk_extent **extents,
4262 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4263 struct btrfs_path *path;
4264 struct btrfs_file_extent_item *fi;
4265 struct extent_buffer *leaf;
4266 struct disk_extent *exts = *extents;
4267 struct btrfs_key found_key;
4272 int max = *nr_extents;
4275 WARN_ON(!no_fragment && *extents);
4278 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4283 path = btrfs_alloc_path();
4286 cur_pos = extent_key->objectid - offset;
4287 last_byte = extent_key->objectid + extent_key->offset;
4288 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4298 leaf = path->nodes[0];
4299 nritems = btrfs_header_nritems(leaf);
4300 if (path->slots[0] >= nritems) {
4301 ret = btrfs_next_leaf(root, path);
4306 leaf = path->nodes[0];
4309 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4310 if (found_key.offset != cur_pos ||
4311 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4312 found_key.objectid != reloc_inode->i_ino)
4315 fi = btrfs_item_ptr(leaf, path->slots[0],
4316 struct btrfs_file_extent_item);
4317 if (btrfs_file_extent_type(leaf, fi) !=
4318 BTRFS_FILE_EXTENT_REG ||
4319 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4323 struct disk_extent *old = exts;
4325 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4326 memcpy(exts, old, sizeof(*exts) * nr);
4327 if (old != *extents)
4331 exts[nr].disk_bytenr =
4332 btrfs_file_extent_disk_bytenr(leaf, fi);
4333 exts[nr].disk_num_bytes =
4334 btrfs_file_extent_disk_num_bytes(leaf, fi);
4335 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4336 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4337 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4338 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4339 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4340 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4342 BUG_ON(exts[nr].offset > 0);
4343 BUG_ON(exts[nr].compression || exts[nr].encryption);
4344 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4346 cur_pos += exts[nr].num_bytes;
4349 if (cur_pos + offset >= last_byte)
4359 BUG_ON(cur_pos + offset > last_byte);
4360 if (cur_pos + offset < last_byte) {
4366 btrfs_free_path(path);
4368 if (exts != *extents)
4377 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4378 struct btrfs_root *root,
4379 struct btrfs_path *path,
4380 struct btrfs_key *extent_key,
4381 struct btrfs_key *leaf_key,
4382 struct btrfs_ref_path *ref_path,
4383 struct disk_extent *new_extents,
4386 struct extent_buffer *leaf;
4387 struct btrfs_file_extent_item *fi;
4388 struct inode *inode = NULL;
4389 struct btrfs_key key;
4394 u64 search_end = (u64)-1;
4397 int extent_locked = 0;
4401 memcpy(&key, leaf_key, sizeof(key));
4402 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4403 if (key.objectid < ref_path->owner_objectid ||
4404 (key.objectid == ref_path->owner_objectid &&
4405 key.type < BTRFS_EXTENT_DATA_KEY)) {
4406 key.objectid = ref_path->owner_objectid;
4407 key.type = BTRFS_EXTENT_DATA_KEY;
4413 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4417 leaf = path->nodes[0];
4418 nritems = btrfs_header_nritems(leaf);
4420 if (extent_locked && ret > 0) {
4422 * the file extent item was modified by someone
4423 * before the extent got locked.
4425 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4426 lock_end, GFP_NOFS);
4430 if (path->slots[0] >= nritems) {
4431 if (++nr_scaned > 2)
4434 BUG_ON(extent_locked);
4435 ret = btrfs_next_leaf(root, path);
4440 leaf = path->nodes[0];
4441 nritems = btrfs_header_nritems(leaf);
4444 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4446 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4447 if ((key.objectid > ref_path->owner_objectid) ||
4448 (key.objectid == ref_path->owner_objectid &&
4449 key.type > BTRFS_EXTENT_DATA_KEY) ||
4450 key.offset >= search_end)
4454 if (inode && key.objectid != inode->i_ino) {
4455 BUG_ON(extent_locked);
4456 btrfs_release_path(root, path);
4457 mutex_unlock(&inode->i_mutex);
4463 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4468 fi = btrfs_item_ptr(leaf, path->slots[0],
4469 struct btrfs_file_extent_item);
4470 extent_type = btrfs_file_extent_type(leaf, fi);
4471 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4472 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4473 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4474 extent_key->objectid)) {
4480 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4481 ext_offset = btrfs_file_extent_offset(leaf, fi);
4483 if (search_end == (u64)-1) {
4484 search_end = key.offset - ext_offset +
4485 btrfs_file_extent_ram_bytes(leaf, fi);
4488 if (!extent_locked) {
4489 lock_start = key.offset;
4490 lock_end = lock_start + num_bytes - 1;
4492 if (lock_start > key.offset ||
4493 lock_end + 1 < key.offset + num_bytes) {
4494 unlock_extent(&BTRFS_I(inode)->io_tree,
4495 lock_start, lock_end, GFP_NOFS);
4501 btrfs_release_path(root, path);
4503 inode = btrfs_iget_locked(root->fs_info->sb,
4504 key.objectid, root);
4505 if (inode->i_state & I_NEW) {
4506 BTRFS_I(inode)->root = root;
4507 BTRFS_I(inode)->location.objectid =
4509 BTRFS_I(inode)->location.type =
4510 BTRFS_INODE_ITEM_KEY;
4511 BTRFS_I(inode)->location.offset = 0;
4512 btrfs_read_locked_inode(inode);
4513 unlock_new_inode(inode);
4516 * some code call btrfs_commit_transaction while
4517 * holding the i_mutex, so we can't use mutex_lock
4520 if (is_bad_inode(inode) ||
4521 !mutex_trylock(&inode->i_mutex)) {
4524 key.offset = (u64)-1;
4529 if (!extent_locked) {
4530 struct btrfs_ordered_extent *ordered;
4532 btrfs_release_path(root, path);
4534 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4535 lock_end, GFP_NOFS);
4536 ordered = btrfs_lookup_first_ordered_extent(inode,
4539 ordered->file_offset <= lock_end &&
4540 ordered->file_offset + ordered->len > lock_start) {
4541 unlock_extent(&BTRFS_I(inode)->io_tree,
4542 lock_start, lock_end, GFP_NOFS);
4543 btrfs_start_ordered_extent(inode, ordered, 1);
4544 btrfs_put_ordered_extent(ordered);
4545 key.offset += num_bytes;
4549 btrfs_put_ordered_extent(ordered);
4555 if (nr_extents == 1) {
4556 /* update extent pointer in place */
4557 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4558 new_extents[0].disk_bytenr);
4559 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4560 new_extents[0].disk_num_bytes);
4561 btrfs_mark_buffer_dirty(leaf);
4563 btrfs_drop_extent_cache(inode, key.offset,
4564 key.offset + num_bytes - 1, 0);
4566 ret = btrfs_inc_extent_ref(trans, root,
4567 new_extents[0].disk_bytenr,
4568 new_extents[0].disk_num_bytes,
4570 root->root_key.objectid,
4575 ret = btrfs_free_extent(trans, root,
4576 extent_key->objectid,
4579 btrfs_header_owner(leaf),
4580 btrfs_header_generation(leaf),
4584 btrfs_release_path(root, path);
4585 key.offset += num_bytes;
4593 * drop old extent pointer at first, then insert the
4594 * new pointers one bye one
4596 btrfs_release_path(root, path);
4597 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4598 key.offset + num_bytes,
4599 key.offset, &alloc_hint);
4602 for (i = 0; i < nr_extents; i++) {
4603 if (ext_offset >= new_extents[i].num_bytes) {
4604 ext_offset -= new_extents[i].num_bytes;
4607 extent_len = min(new_extents[i].num_bytes -
4608 ext_offset, num_bytes);
4610 ret = btrfs_insert_empty_item(trans, root,
4615 leaf = path->nodes[0];
4616 fi = btrfs_item_ptr(leaf, path->slots[0],
4617 struct btrfs_file_extent_item);
4618 btrfs_set_file_extent_generation(leaf, fi,
4620 btrfs_set_file_extent_type(leaf, fi,
4621 BTRFS_FILE_EXTENT_REG);
4622 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4623 new_extents[i].disk_bytenr);
4624 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4625 new_extents[i].disk_num_bytes);
4626 btrfs_set_file_extent_ram_bytes(leaf, fi,
4627 new_extents[i].ram_bytes);
4629 btrfs_set_file_extent_compression(leaf, fi,
4630 new_extents[i].compression);
4631 btrfs_set_file_extent_encryption(leaf, fi,
4632 new_extents[i].encryption);
4633 btrfs_set_file_extent_other_encoding(leaf, fi,
4634 new_extents[i].other_encoding);
4636 btrfs_set_file_extent_num_bytes(leaf, fi,
4638 ext_offset += new_extents[i].offset;
4639 btrfs_set_file_extent_offset(leaf, fi,
4641 btrfs_mark_buffer_dirty(leaf);
4643 btrfs_drop_extent_cache(inode, key.offset,
4644 key.offset + extent_len - 1, 0);
4646 ret = btrfs_inc_extent_ref(trans, root,
4647 new_extents[i].disk_bytenr,
4648 new_extents[i].disk_num_bytes,
4650 root->root_key.objectid,
4651 trans->transid, key.objectid);
4653 btrfs_release_path(root, path);
4655 inode_add_bytes(inode, extent_len);
4658 num_bytes -= extent_len;
4659 key.offset += extent_len;
4664 BUG_ON(i >= nr_extents);
4668 if (extent_locked) {
4669 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4670 lock_end, GFP_NOFS);
4674 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4675 key.offset >= search_end)
4682 btrfs_release_path(root, path);
4684 mutex_unlock(&inode->i_mutex);
4685 if (extent_locked) {
4686 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4687 lock_end, GFP_NOFS);
4694 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4695 struct btrfs_root *root,
4696 struct extent_buffer *buf, u64 orig_start)
4701 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4702 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4704 level = btrfs_header_level(buf);
4706 struct btrfs_leaf_ref *ref;
4707 struct btrfs_leaf_ref *orig_ref;
4709 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4713 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4715 btrfs_free_leaf_ref(root, orig_ref);
4719 ref->nritems = orig_ref->nritems;
4720 memcpy(ref->extents, orig_ref->extents,
4721 sizeof(ref->extents[0]) * ref->nritems);
4723 btrfs_free_leaf_ref(root, orig_ref);
4725 ref->root_gen = trans->transid;
4726 ref->bytenr = buf->start;
4727 ref->owner = btrfs_header_owner(buf);
4728 ref->generation = btrfs_header_generation(buf);
4730 ret = btrfs_add_leaf_ref(root, ref, 0);
4732 btrfs_free_leaf_ref(root, ref);
4737 static noinline int invalidate_extent_cache(struct btrfs_root *root,
4738 struct extent_buffer *leaf,
4739 struct btrfs_block_group_cache *group,
4740 struct btrfs_root *target_root)
4742 struct btrfs_key key;
4743 struct inode *inode = NULL;
4744 struct btrfs_file_extent_item *fi;
4746 u64 skip_objectid = 0;
4750 nritems = btrfs_header_nritems(leaf);
4751 for (i = 0; i < nritems; i++) {
4752 btrfs_item_key_to_cpu(leaf, &key, i);
4753 if (key.objectid == skip_objectid ||
4754 key.type != BTRFS_EXTENT_DATA_KEY)
4756 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4757 if (btrfs_file_extent_type(leaf, fi) ==
4758 BTRFS_FILE_EXTENT_INLINE)
4760 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4762 if (!inode || inode->i_ino != key.objectid) {
4764 inode = btrfs_ilookup(target_root->fs_info->sb,
4765 key.objectid, target_root, 1);
4768 skip_objectid = key.objectid;
4771 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4773 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4774 key.offset + num_bytes - 1, GFP_NOFS);
4775 btrfs_drop_extent_cache(inode, key.offset,
4776 key.offset + num_bytes - 1, 1);
4777 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4778 key.offset + num_bytes - 1, GFP_NOFS);
4785 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4786 struct btrfs_root *root,
4787 struct extent_buffer *leaf,
4788 struct btrfs_block_group_cache *group,
4789 struct inode *reloc_inode)
4791 struct btrfs_key key;
4792 struct btrfs_key extent_key;
4793 struct btrfs_file_extent_item *fi;
4794 struct btrfs_leaf_ref *ref;
4795 struct disk_extent *new_extent;
4804 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4805 BUG_ON(!new_extent);
4807 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4811 nritems = btrfs_header_nritems(leaf);
4812 for (i = 0; i < nritems; i++) {
4813 btrfs_item_key_to_cpu(leaf, &key, i);
4814 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4816 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4817 if (btrfs_file_extent_type(leaf, fi) ==
4818 BTRFS_FILE_EXTENT_INLINE)
4820 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4821 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4826 if (bytenr >= group->key.objectid + group->key.offset ||
4827 bytenr + num_bytes <= group->key.objectid)
4830 extent_key.objectid = bytenr;
4831 extent_key.offset = num_bytes;
4832 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4834 ret = get_new_locations(reloc_inode, &extent_key,
4835 group->key.objectid, 1,
4836 &new_extent, &nr_extent);
4841 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4842 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4843 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4844 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4846 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4847 new_extent->disk_bytenr);
4848 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4849 new_extent->disk_num_bytes);
4850 btrfs_mark_buffer_dirty(leaf);
4852 ret = btrfs_inc_extent_ref(trans, root,
4853 new_extent->disk_bytenr,
4854 new_extent->disk_num_bytes,
4856 root->root_key.objectid,
4857 trans->transid, key.objectid);
4860 ret = btrfs_free_extent(trans, root,
4861 bytenr, num_bytes, leaf->start,
4862 btrfs_header_owner(leaf),
4863 btrfs_header_generation(leaf),
4869 BUG_ON(ext_index + 1 != ref->nritems);
4870 btrfs_free_leaf_ref(root, ref);
4874 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4875 struct btrfs_root *root)
4877 struct btrfs_root *reloc_root;
4880 if (root->reloc_root) {
4881 reloc_root = root->reloc_root;
4882 root->reloc_root = NULL;
4883 list_add(&reloc_root->dead_list,
4884 &root->fs_info->dead_reloc_roots);
4886 btrfs_set_root_bytenr(&reloc_root->root_item,
4887 reloc_root->node->start);
4888 btrfs_set_root_level(&root->root_item,
4889 btrfs_header_level(reloc_root->node));
4890 memset(&reloc_root->root_item.drop_progress, 0,
4891 sizeof(struct btrfs_disk_key));
4892 reloc_root->root_item.drop_level = 0;
4894 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4895 &reloc_root->root_key,
4896 &reloc_root->root_item);
4902 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4904 struct btrfs_trans_handle *trans;
4905 struct btrfs_root *reloc_root;
4906 struct btrfs_root *prev_root = NULL;
4907 struct list_head dead_roots;
4911 INIT_LIST_HEAD(&dead_roots);
4912 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4914 while (!list_empty(&dead_roots)) {
4915 reloc_root = list_entry(dead_roots.prev,
4916 struct btrfs_root, dead_list);
4917 list_del_init(&reloc_root->dead_list);
4919 BUG_ON(reloc_root->commit_root != NULL);
4921 trans = btrfs_join_transaction(root, 1);
4924 mutex_lock(&root->fs_info->drop_mutex);
4925 ret = btrfs_drop_snapshot(trans, reloc_root);
4928 mutex_unlock(&root->fs_info->drop_mutex);
4930 nr = trans->blocks_used;
4931 ret = btrfs_end_transaction(trans, root);
4933 btrfs_btree_balance_dirty(root, nr);
4936 free_extent_buffer(reloc_root->node);
4938 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4939 &reloc_root->root_key);
4941 mutex_unlock(&root->fs_info->drop_mutex);
4943 nr = trans->blocks_used;
4944 ret = btrfs_end_transaction(trans, root);
4946 btrfs_btree_balance_dirty(root, nr);
4949 prev_root = reloc_root;
4952 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4958 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4960 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4964 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4966 struct btrfs_root *reloc_root;
4967 struct btrfs_trans_handle *trans;
4968 struct btrfs_key location;
4972 mutex_lock(&root->fs_info->tree_reloc_mutex);
4973 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4975 found = !list_empty(&root->fs_info->dead_reloc_roots);
4976 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4979 trans = btrfs_start_transaction(root, 1);
4981 ret = btrfs_commit_transaction(trans, root);
4985 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4986 location.offset = (u64)-1;
4987 location.type = BTRFS_ROOT_ITEM_KEY;
4989 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4990 BUG_ON(!reloc_root);
4991 btrfs_orphan_cleanup(reloc_root);
4995 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
4996 struct btrfs_root *root)
4998 struct btrfs_root *reloc_root;
4999 struct extent_buffer *eb;
5000 struct btrfs_root_item *root_item;
5001 struct btrfs_key root_key;
5004 BUG_ON(!root->ref_cows);
5005 if (root->reloc_root)
5008 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5011 ret = btrfs_copy_root(trans, root, root->commit_root,
5012 &eb, BTRFS_TREE_RELOC_OBJECTID);
5015 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5016 root_key.offset = root->root_key.objectid;
5017 root_key.type = BTRFS_ROOT_ITEM_KEY;
5019 memcpy(root_item, &root->root_item, sizeof(root_item));
5020 btrfs_set_root_refs(root_item, 0);
5021 btrfs_set_root_bytenr(root_item, eb->start);
5022 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5023 btrfs_set_root_generation(root_item, trans->transid);
5025 btrfs_tree_unlock(eb);
5026 free_extent_buffer(eb);
5028 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5029 &root_key, root_item);
5033 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5035 BUG_ON(!reloc_root);
5036 reloc_root->last_trans = trans->transid;
5037 reloc_root->commit_root = NULL;
5038 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5040 root->reloc_root = reloc_root;
5045 * Core function of space balance.
5047 * The idea is using reloc trees to relocate tree blocks in reference
5048 * counted roots. There is one reloc tree for each subvol, and all
5049 * reloc trees share same root key objectid. Reloc trees are snapshots
5050 * of the latest committed roots of subvols (root->commit_root).
5052 * To relocate a tree block referenced by a subvol, there are two steps.
5053 * COW the block through subvol's reloc tree, then update block pointer
5054 * in the subvol to point to the new block. Since all reloc trees share
5055 * same root key objectid, doing special handing for tree blocks owned
5056 * by them is easy. Once a tree block has been COWed in one reloc tree,
5057 * we can use the resulting new block directly when the same block is
5058 * required to COW again through other reloc trees. By this way, relocated
5059 * tree blocks are shared between reloc trees, so they are also shared
5062 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5063 struct btrfs_root *root,
5064 struct btrfs_path *path,
5065 struct btrfs_key *first_key,
5066 struct btrfs_ref_path *ref_path,
5067 struct btrfs_block_group_cache *group,
5068 struct inode *reloc_inode)
5070 struct btrfs_root *reloc_root;
5071 struct extent_buffer *eb = NULL;
5072 struct btrfs_key *keys;
5076 int lowest_level = 0;
5079 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5080 lowest_level = ref_path->owner_objectid;
5082 if (!root->ref_cows) {
5083 path->lowest_level = lowest_level;
5084 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5086 path->lowest_level = 0;
5087 btrfs_release_path(root, path);
5091 mutex_lock(&root->fs_info->tree_reloc_mutex);
5092 ret = init_reloc_tree(trans, root);
5094 reloc_root = root->reloc_root;
5096 shared_level = ref_path->shared_level;
5097 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5099 keys = ref_path->node_keys;
5100 nodes = ref_path->new_nodes;
5101 memset(&keys[shared_level + 1], 0,
5102 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5103 memset(&nodes[shared_level + 1], 0,
5104 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5106 if (nodes[lowest_level] == 0) {
5107 path->lowest_level = lowest_level;
5108 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5111 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5112 eb = path->nodes[level];
5113 if (!eb || eb == reloc_root->node)
5115 nodes[level] = eb->start;
5117 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5119 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5122 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5123 eb = path->nodes[0];
5124 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5125 group, reloc_inode);
5128 btrfs_release_path(reloc_root, path);
5130 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5136 * replace tree blocks in the fs tree with tree blocks in
5139 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5142 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5143 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5146 extent_buffer_get(path->nodes[0]);
5147 eb = path->nodes[0];
5148 btrfs_release_path(reloc_root, path);
5149 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5151 free_extent_buffer(eb);
5154 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5155 path->lowest_level = 0;
5159 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5160 struct btrfs_root *root,
5161 struct btrfs_path *path,
5162 struct btrfs_key *first_key,
5163 struct btrfs_ref_path *ref_path)
5167 ret = relocate_one_path(trans, root, path, first_key,
5168 ref_path, NULL, NULL);
5174 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5175 struct btrfs_root *extent_root,
5176 struct btrfs_path *path,
5177 struct btrfs_key *extent_key)
5181 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5184 ret = btrfs_del_item(trans, extent_root, path);
5186 btrfs_release_path(extent_root, path);
5190 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5191 struct btrfs_ref_path *ref_path)
5193 struct btrfs_key root_key;
5195 root_key.objectid = ref_path->root_objectid;
5196 root_key.type = BTRFS_ROOT_ITEM_KEY;
5197 if (is_cowonly_root(ref_path->root_objectid))
5198 root_key.offset = 0;
5200 root_key.offset = (u64)-1;
5202 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5205 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5206 struct btrfs_path *path,
5207 struct btrfs_key *extent_key,
5208 struct btrfs_block_group_cache *group,
5209 struct inode *reloc_inode, int pass)
5211 struct btrfs_trans_handle *trans;
5212 struct btrfs_root *found_root;
5213 struct btrfs_ref_path *ref_path = NULL;
5214 struct disk_extent *new_extents = NULL;
5219 struct btrfs_key first_key;
5223 trans = btrfs_start_transaction(extent_root, 1);
5226 if (extent_key->objectid == 0) {
5227 ret = del_extent_zero(trans, extent_root, path, extent_key);
5231 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5237 for (loops = 0; ; loops++) {
5239 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5240 extent_key->objectid);
5242 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5249 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5250 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5253 found_root = read_ref_root(extent_root->fs_info, ref_path);
5254 BUG_ON(!found_root);
5256 * for reference counted tree, only process reference paths
5257 * rooted at the latest committed root.
5259 if (found_root->ref_cows &&
5260 ref_path->root_generation != found_root->root_key.offset)
5263 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5266 * copy data extents to new locations
5268 u64 group_start = group->key.objectid;
5269 ret = relocate_data_extent(reloc_inode,
5278 level = ref_path->owner_objectid;
5281 if (prev_block != ref_path->nodes[level]) {
5282 struct extent_buffer *eb;
5283 u64 block_start = ref_path->nodes[level];
5284 u64 block_size = btrfs_level_size(found_root, level);
5286 eb = read_tree_block(found_root, block_start,
5288 btrfs_tree_lock(eb);
5289 BUG_ON(level != btrfs_header_level(eb));
5292 btrfs_item_key_to_cpu(eb, &first_key, 0);
5294 btrfs_node_key_to_cpu(eb, &first_key, 0);
5296 btrfs_tree_unlock(eb);
5297 free_extent_buffer(eb);
5298 prev_block = block_start;
5301 mutex_lock(&extent_root->fs_info->trans_mutex);
5302 btrfs_record_root_in_trans(found_root);
5303 mutex_unlock(&extent_root->fs_info->trans_mutex);
5304 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5306 * try to update data extent references while
5307 * keeping metadata shared between snapshots.
5310 ret = relocate_one_path(trans, found_root,
5311 path, &first_key, ref_path,
5312 group, reloc_inode);
5318 * use fallback method to process the remaining
5322 u64 group_start = group->key.objectid;
5323 new_extents = kmalloc(sizeof(*new_extents),
5326 ret = get_new_locations(reloc_inode,
5334 ret = replace_one_extent(trans, found_root,
5336 &first_key, ref_path,
5337 new_extents, nr_extents);
5339 ret = relocate_tree_block(trans, found_root, path,
5340 &first_key, ref_path);
5347 btrfs_end_transaction(trans, extent_root);
5353 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5356 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5357 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5359 num_devices = root->fs_info->fs_devices->rw_devices;
5360 if (num_devices == 1) {
5361 stripped |= BTRFS_BLOCK_GROUP_DUP;
5362 stripped = flags & ~stripped;
5364 /* turn raid0 into single device chunks */
5365 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5368 /* turn mirroring into duplication */
5369 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5370 BTRFS_BLOCK_GROUP_RAID10))
5371 return stripped | BTRFS_BLOCK_GROUP_DUP;
5374 /* they already had raid on here, just return */
5375 if (flags & stripped)
5378 stripped |= BTRFS_BLOCK_GROUP_DUP;
5379 stripped = flags & ~stripped;
5381 /* switch duplicated blocks with raid1 */
5382 if (flags & BTRFS_BLOCK_GROUP_DUP)
5383 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5385 /* turn single device chunks into raid0 */
5386 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5391 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5392 struct btrfs_block_group_cache *shrink_block_group,
5395 struct btrfs_trans_handle *trans;
5396 u64 new_alloc_flags;
5399 spin_lock(&shrink_block_group->lock);
5400 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5401 spin_unlock(&shrink_block_group->lock);
5403 trans = btrfs_start_transaction(root, 1);
5404 spin_lock(&shrink_block_group->lock);
5406 new_alloc_flags = update_block_group_flags(root,
5407 shrink_block_group->flags);
5408 if (new_alloc_flags != shrink_block_group->flags) {
5410 btrfs_block_group_used(&shrink_block_group->item);
5412 calc = shrink_block_group->key.offset;
5414 spin_unlock(&shrink_block_group->lock);
5416 do_chunk_alloc(trans, root->fs_info->extent_root,
5417 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5419 btrfs_end_transaction(trans, root);
5421 spin_unlock(&shrink_block_group->lock);
5425 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5426 struct btrfs_root *root,
5427 u64 objectid, u64 size)
5429 struct btrfs_path *path;
5430 struct btrfs_inode_item *item;
5431 struct extent_buffer *leaf;
5434 path = btrfs_alloc_path();
5438 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5442 leaf = path->nodes[0];
5443 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5444 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5445 btrfs_set_inode_generation(leaf, item, 1);
5446 btrfs_set_inode_size(leaf, item, size);
5447 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5448 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5449 btrfs_mark_buffer_dirty(leaf);
5450 btrfs_release_path(root, path);
5452 btrfs_free_path(path);
5456 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5457 struct btrfs_block_group_cache *group)
5459 struct inode *inode = NULL;
5460 struct btrfs_trans_handle *trans;
5461 struct btrfs_root *root;
5462 struct btrfs_key root_key;
5463 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5466 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5467 root_key.type = BTRFS_ROOT_ITEM_KEY;
5468 root_key.offset = (u64)-1;
5469 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5471 return ERR_CAST(root);
5473 trans = btrfs_start_transaction(root, 1);
5476 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5480 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5483 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5484 group->key.offset, 0, group->key.offset,
5488 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5489 if (inode->i_state & I_NEW) {
5490 BTRFS_I(inode)->root = root;
5491 BTRFS_I(inode)->location.objectid = objectid;
5492 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5493 BTRFS_I(inode)->location.offset = 0;
5494 btrfs_read_locked_inode(inode);
5495 unlock_new_inode(inode);
5496 BUG_ON(is_bad_inode(inode));
5500 BTRFS_I(inode)->index_cnt = group->key.objectid;
5502 err = btrfs_orphan_add(trans, inode);
5504 btrfs_end_transaction(trans, root);
5508 inode = ERR_PTR(err);
5513 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5516 struct btrfs_ordered_sum *sums;
5517 struct btrfs_sector_sum *sector_sum;
5518 struct btrfs_ordered_extent *ordered;
5519 struct btrfs_root *root = BTRFS_I(inode)->root;
5520 struct list_head list;
5525 INIT_LIST_HEAD(&list);
5527 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5528 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5530 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5531 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
5532 disk_bytenr + len - 1, &list);
5534 while (!list_empty(&list)) {
5535 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5536 list_del_init(&sums->list);
5538 sector_sum = sums->sums;
5539 sums->bytenr = ordered->start;
5542 while (offset < sums->len) {
5543 sector_sum->bytenr += ordered->start - disk_bytenr;
5545 offset += root->sectorsize;
5548 btrfs_add_ordered_sum(inode, ordered, sums);
5550 btrfs_put_ordered_extent(ordered);
5554 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5556 struct btrfs_trans_handle *trans;
5557 struct btrfs_path *path;
5558 struct btrfs_fs_info *info = root->fs_info;
5559 struct extent_buffer *leaf;
5560 struct inode *reloc_inode;
5561 struct btrfs_block_group_cache *block_group;
5562 struct btrfs_key key;
5571 root = root->fs_info->extent_root;
5573 block_group = btrfs_lookup_block_group(info, group_start);
5574 BUG_ON(!block_group);
5576 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5577 (unsigned long long)block_group->key.objectid,
5578 (unsigned long long)block_group->flags);
5580 path = btrfs_alloc_path();
5583 reloc_inode = create_reloc_inode(info, block_group);
5584 BUG_ON(IS_ERR(reloc_inode));
5586 __alloc_chunk_for_shrink(root, block_group, 1);
5587 set_block_group_readonly(block_group);
5589 btrfs_start_delalloc_inodes(info->tree_root);
5590 btrfs_wait_ordered_extents(info->tree_root, 0);
5595 key.objectid = block_group->key.objectid;
5598 cur_byte = key.objectid;
5600 trans = btrfs_start_transaction(info->tree_root, 1);
5601 btrfs_commit_transaction(trans, info->tree_root);
5603 mutex_lock(&root->fs_info->cleaner_mutex);
5604 btrfs_clean_old_snapshots(info->tree_root);
5605 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5606 mutex_unlock(&root->fs_info->cleaner_mutex);
5608 trans = btrfs_start_transaction(info->tree_root, 1);
5609 btrfs_commit_transaction(trans, info->tree_root);
5612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5616 leaf = path->nodes[0];
5617 nritems = btrfs_header_nritems(leaf);
5618 if (path->slots[0] >= nritems) {
5619 ret = btrfs_next_leaf(root, path);
5626 leaf = path->nodes[0];
5627 nritems = btrfs_header_nritems(leaf);
5630 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5632 if (key.objectid >= block_group->key.objectid +
5633 block_group->key.offset)
5636 if (progress && need_resched()) {
5637 btrfs_release_path(root, path);
5644 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5645 key.objectid + key.offset <= cur_byte) {
5651 cur_byte = key.objectid + key.offset;
5652 btrfs_release_path(root, path);
5654 __alloc_chunk_for_shrink(root, block_group, 0);
5655 ret = relocate_one_extent(root, path, &key, block_group,
5661 key.objectid = cur_byte;
5666 btrfs_release_path(root, path);
5669 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5670 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5673 if (total_found > 0) {
5674 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
5675 (unsigned long long)total_found, pass);
5677 if (total_found == skipped && pass > 2) {
5679 reloc_inode = create_reloc_inode(info, block_group);
5685 /* delete reloc_inode */
5688 /* unpin extents in this range */
5689 trans = btrfs_start_transaction(info->tree_root, 1);
5690 btrfs_commit_transaction(trans, info->tree_root);
5692 spin_lock(&block_group->lock);
5693 WARN_ON(block_group->pinned > 0);
5694 WARN_ON(block_group->reserved > 0);
5695 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5696 spin_unlock(&block_group->lock);
5697 put_block_group(block_group);
5700 btrfs_free_path(path);
5704 static int find_first_block_group(struct btrfs_root *root,
5705 struct btrfs_path *path, struct btrfs_key *key)
5708 struct btrfs_key found_key;
5709 struct extent_buffer *leaf;
5712 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5717 slot = path->slots[0];
5718 leaf = path->nodes[0];
5719 if (slot >= btrfs_header_nritems(leaf)) {
5720 ret = btrfs_next_leaf(root, path);
5727 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5729 if (found_key.objectid >= key->objectid &&
5730 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5741 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5743 struct btrfs_block_group_cache *block_group;
5744 struct btrfs_space_info *space_info;
5747 spin_lock(&info->block_group_cache_lock);
5748 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5749 block_group = rb_entry(n, struct btrfs_block_group_cache,
5751 rb_erase(&block_group->cache_node,
5752 &info->block_group_cache_tree);
5753 spin_unlock(&info->block_group_cache_lock);
5755 btrfs_remove_free_space_cache(block_group);
5756 down_write(&block_group->space_info->groups_sem);
5757 list_del(&block_group->list);
5758 up_write(&block_group->space_info->groups_sem);
5760 WARN_ON(atomic_read(&block_group->count) != 1);
5763 spin_lock(&info->block_group_cache_lock);
5765 spin_unlock(&info->block_group_cache_lock);
5767 /* now that all the block groups are freed, go through and
5768 * free all the space_info structs. This is only called during
5769 * the final stages of unmount, and so we know nobody is
5770 * using them. We call synchronize_rcu() once before we start,
5771 * just to be on the safe side.
5775 while(!list_empty(&info->space_info)) {
5776 space_info = list_entry(info->space_info.next,
5777 struct btrfs_space_info,
5780 list_del(&space_info->list);
5786 int btrfs_read_block_groups(struct btrfs_root *root)
5788 struct btrfs_path *path;
5790 struct btrfs_block_group_cache *cache;
5791 struct btrfs_fs_info *info = root->fs_info;
5792 struct btrfs_space_info *space_info;
5793 struct btrfs_key key;
5794 struct btrfs_key found_key;
5795 struct extent_buffer *leaf;
5797 root = info->extent_root;
5800 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5801 path = btrfs_alloc_path();
5806 ret = find_first_block_group(root, path, &key);
5814 leaf = path->nodes[0];
5815 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5816 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5822 atomic_set(&cache->count, 1);
5823 spin_lock_init(&cache->lock);
5824 mutex_init(&cache->alloc_mutex);
5825 mutex_init(&cache->cache_mutex);
5826 INIT_LIST_HEAD(&cache->list);
5827 read_extent_buffer(leaf, &cache->item,
5828 btrfs_item_ptr_offset(leaf, path->slots[0]),
5829 sizeof(cache->item));
5830 memcpy(&cache->key, &found_key, sizeof(found_key));
5832 key.objectid = found_key.objectid + found_key.offset;
5833 btrfs_release_path(root, path);
5834 cache->flags = btrfs_block_group_flags(&cache->item);
5836 ret = update_space_info(info, cache->flags, found_key.offset,
5837 btrfs_block_group_used(&cache->item),
5840 cache->space_info = space_info;
5841 down_write(&space_info->groups_sem);
5842 list_add_tail(&cache->list, &space_info->block_groups);
5843 up_write(&space_info->groups_sem);
5845 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5848 set_avail_alloc_bits(root->fs_info, cache->flags);
5849 if (btrfs_chunk_readonly(root, cache->key.objectid))
5850 set_block_group_readonly(cache);
5854 btrfs_free_path(path);
5858 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5859 struct btrfs_root *root, u64 bytes_used,
5860 u64 type, u64 chunk_objectid, u64 chunk_offset,
5864 struct btrfs_root *extent_root;
5865 struct btrfs_block_group_cache *cache;
5867 extent_root = root->fs_info->extent_root;
5869 root->fs_info->last_trans_new_blockgroup = trans->transid;
5871 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5875 cache->key.objectid = chunk_offset;
5876 cache->key.offset = size;
5877 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5878 atomic_set(&cache->count, 1);
5879 spin_lock_init(&cache->lock);
5880 mutex_init(&cache->alloc_mutex);
5881 mutex_init(&cache->cache_mutex);
5882 INIT_LIST_HEAD(&cache->list);
5884 btrfs_set_block_group_used(&cache->item, bytes_used);
5885 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5886 cache->flags = type;
5887 btrfs_set_block_group_flags(&cache->item, type);
5889 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5890 &cache->space_info);
5892 down_write(&cache->space_info->groups_sem);
5893 list_add_tail(&cache->list, &cache->space_info->block_groups);
5894 up_write(&cache->space_info->groups_sem);
5896 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5899 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5900 sizeof(cache->item));
5903 set_avail_alloc_bits(extent_root->fs_info, type);
5908 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5909 struct btrfs_root *root, u64 group_start)
5911 struct btrfs_path *path;
5912 struct btrfs_block_group_cache *block_group;
5913 struct btrfs_key key;
5916 root = root->fs_info->extent_root;
5918 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5919 BUG_ON(!block_group);
5920 BUG_ON(!block_group->ro);
5922 memcpy(&key, &block_group->key, sizeof(key));
5924 path = btrfs_alloc_path();
5927 spin_lock(&root->fs_info->block_group_cache_lock);
5928 rb_erase(&block_group->cache_node,
5929 &root->fs_info->block_group_cache_tree);
5930 spin_unlock(&root->fs_info->block_group_cache_lock);
5931 btrfs_remove_free_space_cache(block_group);
5932 down_write(&block_group->space_info->groups_sem);
5933 list_del(&block_group->list);
5934 up_write(&block_group->space_info->groups_sem);
5936 spin_lock(&block_group->space_info->lock);
5937 block_group->space_info->total_bytes -= block_group->key.offset;
5938 block_group->space_info->bytes_readonly -= block_group->key.offset;
5939 spin_unlock(&block_group->space_info->lock);
5940 block_group->space_info->full = 0;
5942 put_block_group(block_group);
5943 put_block_group(block_group);
5945 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5951 ret = btrfs_del_item(trans, root, path);
5953 btrfs_free_path(path);