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 update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc,
63 static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 u64 bytenr, u64 num_bytes, u64 parent,
66 u64 root_objectid, u64 ref_generation,
67 u64 owner_objectid, int pin,
70 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
71 struct btrfs_root *extent_root, u64 alloc_bytes,
72 u64 flags, int force);
74 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
76 return (cache->flags & bits) == bits;
80 * this adds the block group to the fs_info rb tree for the block group
83 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
84 struct btrfs_block_group_cache *block_group)
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
97 if (block_group->key.objectid < cache->key.objectid) {
99 } else if (block_group->key.objectid > cache->key.objectid) {
102 spin_unlock(&info->block_group_cache_lock);
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
119 static struct btrfs_block_group_cache *
120 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
123 struct btrfs_block_group_cache *cache, *ret = NULL;
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
131 cache = rb_entry(n, struct btrfs_block_group_cache,
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
152 atomic_inc(&ret->count);
153 spin_unlock(&info->block_group_cache_lock);
159 * this is only called by cache_block_group, since we could have freed extents
160 * we need to check the pinned_extents for any extents that can't be used yet
161 * since their free space will be released as soon as the transaction commits.
163 static int add_new_free_space(struct btrfs_block_group_cache *block_group,
164 struct btrfs_fs_info *info, u64 start, u64 end)
166 u64 extent_start, extent_end, size;
169 mutex_lock(&info->pinned_mutex);
170 while (start < end) {
171 ret = find_first_extent_bit(&info->pinned_extents, start,
172 &extent_start, &extent_end,
177 if (extent_start == start) {
178 start = extent_end + 1;
179 } else if (extent_start > start && extent_start < end) {
180 size = extent_start - start;
181 ret = btrfs_add_free_space(block_group, start,
184 start = extent_end + 1;
192 ret = btrfs_add_free_space(block_group, start, size);
195 mutex_unlock(&info->pinned_mutex);
200 static int remove_sb_from_cache(struct btrfs_root *root,
201 struct btrfs_block_group_cache *cache)
208 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
209 bytenr = btrfs_sb_offset(i);
210 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
211 cache->key.objectid, bytenr, 0,
212 &logical, &nr, &stripe_len);
215 btrfs_remove_free_space(cache, logical[nr],
223 static int cache_block_group(struct btrfs_root *root,
224 struct btrfs_block_group_cache *block_group)
226 struct btrfs_path *path;
228 struct btrfs_key key;
229 struct extent_buffer *leaf;
236 root = root->fs_info->extent_root;
238 if (block_group->cached)
241 path = btrfs_alloc_path();
247 * we get into deadlocks with paths held by callers of this function.
248 * since the alloc_mutex is protecting things right now, just
249 * skip the locking here
251 path->skip_locking = 1;
252 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
255 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
256 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
261 leaf = path->nodes[0];
262 slot = path->slots[0];
263 if (slot >= btrfs_header_nritems(leaf)) {
264 ret = btrfs_next_leaf(root, path);
272 btrfs_item_key_to_cpu(leaf, &key, slot);
273 if (key.objectid < block_group->key.objectid)
276 if (key.objectid >= block_group->key.objectid +
277 block_group->key.offset)
280 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
281 add_new_free_space(block_group, root->fs_info, last,
284 last = key.objectid + key.offset;
290 add_new_free_space(block_group, root->fs_info, last,
291 block_group->key.objectid +
292 block_group->key.offset);
294 remove_sb_from_cache(root, block_group);
295 block_group->cached = 1;
298 btrfs_free_path(path);
303 * return the block group that starts at or after bytenr
305 static struct btrfs_block_group_cache *
306 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
308 struct btrfs_block_group_cache *cache;
310 cache = block_group_cache_tree_search(info, bytenr, 0);
316 * return the block group that contains teh given bytenr
318 struct btrfs_block_group_cache *btrfs_lookup_block_group(
319 struct btrfs_fs_info *info,
322 struct btrfs_block_group_cache *cache;
324 cache = block_group_cache_tree_search(info, bytenr, 1);
329 static inline void put_block_group(struct btrfs_block_group_cache *cache)
331 if (atomic_dec_and_test(&cache->count))
335 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
338 struct list_head *head = &info->space_info;
339 struct btrfs_space_info *found;
342 list_for_each_entry_rcu(found, head, list) {
343 if (found->flags == flags) {
353 * after adding space to the filesystem, we need to clear the full flags
354 * on all the space infos.
356 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
358 struct list_head *head = &info->space_info;
359 struct btrfs_space_info *found;
362 list_for_each_entry_rcu(found, head, list)
367 static u64 div_factor(u64 num, int factor)
376 u64 btrfs_find_block_group(struct btrfs_root *root,
377 u64 search_start, u64 search_hint, int owner)
379 struct btrfs_block_group_cache *cache;
381 u64 last = max(search_hint, search_start);
388 cache = btrfs_lookup_first_block_group(root->fs_info, last);
392 spin_lock(&cache->lock);
393 last = cache->key.objectid + cache->key.offset;
394 used = btrfs_block_group_used(&cache->item);
396 if ((full_search || !cache->ro) &&
397 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
398 if (used + cache->pinned + cache->reserved <
399 div_factor(cache->key.offset, factor)) {
400 group_start = cache->key.objectid;
401 spin_unlock(&cache->lock);
402 put_block_group(cache);
406 spin_unlock(&cache->lock);
407 put_block_group(cache);
415 if (!full_search && factor < 10) {
425 /* simple helper to search for an existing extent at a given offset */
426 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
429 struct btrfs_key key;
430 struct btrfs_path *path;
432 path = btrfs_alloc_path();
434 key.objectid = start;
436 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
437 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
439 btrfs_free_path(path);
444 * Back reference rules. Back refs have three main goals:
446 * 1) differentiate between all holders of references to an extent so that
447 * when a reference is dropped we can make sure it was a valid reference
448 * before freeing the extent.
450 * 2) Provide enough information to quickly find the holders of an extent
451 * if we notice a given block is corrupted or bad.
453 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
454 * maintenance. This is actually the same as #2, but with a slightly
455 * different use case.
457 * File extents can be referenced by:
459 * - multiple snapshots, subvolumes, or different generations in one subvol
460 * - different files inside a single subvolume
461 * - different offsets inside a file (bookend extents in file.c)
463 * The extent ref structure has fields for:
465 * - Objectid of the subvolume root
466 * - Generation number of the tree holding the reference
467 * - objectid of the file holding the reference
468 * - number of references holding by parent node (alway 1 for tree blocks)
470 * Btree leaf may hold multiple references to a file extent. In most cases,
471 * these references are from same file and the corresponding offsets inside
472 * the file are close together.
474 * When a file extent is allocated the fields are filled in:
475 * (root_key.objectid, trans->transid, inode objectid, 1)
477 * When a leaf is cow'd new references are added for every file extent found
478 * in the leaf. It looks similar to the create case, but trans->transid will
479 * be different when the block is cow'd.
481 * (root_key.objectid, trans->transid, inode objectid,
482 * number of references in the leaf)
484 * When a file extent is removed either during snapshot deletion or
485 * file truncation, we find the corresponding back reference and check
486 * the following fields:
488 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
491 * Btree extents can be referenced by:
493 * - Different subvolumes
494 * - Different generations of the same subvolume
496 * When a tree block is created, back references are inserted:
498 * (root->root_key.objectid, trans->transid, level, 1)
500 * When a tree block is cow'd, new back references are added for all the
501 * blocks it points to. If the tree block isn't in reference counted root,
502 * the old back references are removed. These new back references are of
503 * the form (trans->transid will have increased since creation):
505 * (root->root_key.objectid, trans->transid, level, 1)
507 * When a backref is in deleting, the following fields are checked:
509 * if backref was for a tree root:
510 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
512 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
514 * Back Reference Key composing:
516 * The key objectid corresponds to the first byte in the extent, the key
517 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
518 * byte of parent extent. If a extent is tree root, the key offset is set
519 * to the key objectid.
522 static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
523 struct btrfs_root *root,
524 struct btrfs_path *path,
525 u64 bytenr, u64 parent,
526 u64 ref_root, u64 ref_generation,
527 u64 owner_objectid, int del)
529 struct btrfs_key key;
530 struct btrfs_extent_ref *ref;
531 struct extent_buffer *leaf;
535 key.objectid = bytenr;
536 key.type = BTRFS_EXTENT_REF_KEY;
539 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
547 leaf = path->nodes[0];
548 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
549 ref_objectid = btrfs_ref_objectid(leaf, ref);
550 if (btrfs_ref_root(leaf, ref) != ref_root ||
551 btrfs_ref_generation(leaf, ref) != ref_generation ||
552 (ref_objectid != owner_objectid &&
553 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
563 static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct btrfs_path *path,
566 u64 bytenr, u64 parent,
567 u64 ref_root, u64 ref_generation,
571 struct btrfs_key key;
572 struct extent_buffer *leaf;
573 struct btrfs_extent_ref *ref;
577 key.objectid = bytenr;
578 key.type = BTRFS_EXTENT_REF_KEY;
581 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
583 leaf = path->nodes[0];
584 ref = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_extent_ref);
586 btrfs_set_ref_root(leaf, ref, ref_root);
587 btrfs_set_ref_generation(leaf, ref, ref_generation);
588 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
589 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
590 } else if (ret == -EEXIST) {
593 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
594 leaf = path->nodes[0];
595 ref = btrfs_item_ptr(leaf, path->slots[0],
596 struct btrfs_extent_ref);
597 if (btrfs_ref_root(leaf, ref) != ref_root ||
598 btrfs_ref_generation(leaf, ref) != ref_generation) {
604 num_refs = btrfs_ref_num_refs(leaf, ref);
605 BUG_ON(num_refs == 0);
606 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
608 existing_owner = btrfs_ref_objectid(leaf, ref);
609 if (existing_owner != owner_objectid &&
610 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
611 btrfs_set_ref_objectid(leaf, ref,
612 BTRFS_MULTIPLE_OBJECTIDS);
618 btrfs_unlock_up_safe(path, 1);
619 btrfs_mark_buffer_dirty(path->nodes[0]);
621 btrfs_release_path(root, path);
625 static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
627 struct btrfs_path *path,
630 struct extent_buffer *leaf;
631 struct btrfs_extent_ref *ref;
635 leaf = path->nodes[0];
636 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
637 num_refs = btrfs_ref_num_refs(leaf, ref);
638 BUG_ON(num_refs < refs_to_drop);
639 num_refs -= refs_to_drop;
641 ret = btrfs_del_item(trans, root, path);
643 btrfs_set_ref_num_refs(leaf, ref, num_refs);
644 btrfs_mark_buffer_dirty(leaf);
646 btrfs_release_path(root, path);
650 #ifdef BIO_RW_DISCARD
651 static void btrfs_issue_discard(struct block_device *bdev,
654 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
658 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
661 #ifdef BIO_RW_DISCARD
663 u64 map_length = num_bytes;
664 struct btrfs_multi_bio *multi = NULL;
666 /* Tell the block device(s) that the sectors can be discarded */
667 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
668 bytenr, &map_length, &multi, 0);
670 struct btrfs_bio_stripe *stripe = multi->stripes;
673 if (map_length > num_bytes)
674 map_length = num_bytes;
676 for (i = 0; i < multi->num_stripes; i++, stripe++) {
677 btrfs_issue_discard(stripe->dev->bdev,
690 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
691 struct btrfs_root *root, u64 bytenr,
693 u64 orig_parent, u64 parent,
694 u64 orig_root, u64 ref_root,
695 u64 orig_generation, u64 ref_generation,
699 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
701 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
702 orig_parent, parent, orig_root,
703 ref_root, orig_generation,
704 ref_generation, owner_objectid, pin);
709 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
711 u64 num_bytes, u64 orig_parent, u64 parent,
712 u64 ref_root, u64 ref_generation,
716 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
717 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
720 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
721 orig_parent, parent, ref_root,
722 ref_root, ref_generation,
723 ref_generation, owner_objectid);
726 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
727 struct btrfs_root *root, u64 bytenr,
729 u64 orig_parent, u64 parent,
730 u64 orig_root, u64 ref_root,
731 u64 orig_generation, u64 ref_generation,
736 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
737 ref_generation, owner_objectid,
738 BTRFS_ADD_DELAYED_REF, 0);
743 static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
744 struct btrfs_root *root, u64 bytenr,
745 u64 num_bytes, u64 parent, u64 ref_root,
746 u64 ref_generation, u64 owner_objectid,
749 struct btrfs_path *path;
751 struct btrfs_key key;
752 struct extent_buffer *l;
753 struct btrfs_extent_item *item;
756 path = btrfs_alloc_path();
761 path->leave_spinning = 1;
762 key.objectid = bytenr;
763 key.type = BTRFS_EXTENT_ITEM_KEY;
764 key.offset = num_bytes;
766 /* first find the extent item and update its reference count */
767 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
770 btrfs_set_path_blocking(path);
776 btrfs_free_path(path);
781 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
782 if (key.objectid != bytenr) {
783 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
784 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
785 (unsigned long long)bytenr,
786 (unsigned long long)key.objectid);
789 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
791 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
793 refs = btrfs_extent_refs(l, item);
794 btrfs_set_extent_refs(l, item, refs + refs_to_add);
795 btrfs_unlock_up_safe(path, 1);
797 btrfs_mark_buffer_dirty(path->nodes[0]);
799 btrfs_release_path(root->fs_info->extent_root, path);
802 path->leave_spinning = 1;
804 /* now insert the actual backref */
805 ret = insert_extent_backref(trans, root->fs_info->extent_root,
806 path, bytenr, parent,
807 ref_root, ref_generation,
808 owner_objectid, refs_to_add);
810 btrfs_free_path(path);
814 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
815 struct btrfs_root *root,
816 u64 bytenr, u64 num_bytes, u64 parent,
817 u64 ref_root, u64 ref_generation,
821 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
822 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
825 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
826 0, ref_root, 0, ref_generation,
831 static int drop_delayed_ref(struct btrfs_trans_handle *trans,
832 struct btrfs_root *root,
833 struct btrfs_delayed_ref_node *node)
836 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
838 BUG_ON(node->ref_mod == 0);
839 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
840 node->parent, ref->root, ref->generation,
841 ref->owner_objectid, ref->pin, node->ref_mod);
846 /* helper function to actually process a single delayed ref entry */
847 static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root,
849 struct btrfs_delayed_ref_node *node,
853 struct btrfs_delayed_ref *ref;
855 if (node->parent == (u64)-1) {
856 struct btrfs_delayed_ref_head *head;
858 * we've hit the end of the chain and we were supposed
859 * to insert this extent into the tree. But, it got
860 * deleted before we ever needed to insert it, so all
861 * we have to do is clean up the accounting
863 if (insert_reserved) {
864 update_reserved_extents(root, node->bytenr,
867 head = btrfs_delayed_node_to_head(node);
868 mutex_unlock(&head->mutex);
872 ref = btrfs_delayed_node_to_ref(node);
873 if (ref->action == BTRFS_ADD_DELAYED_REF) {
874 if (insert_reserved) {
875 struct btrfs_key ins;
877 ins.objectid = node->bytenr;
878 ins.offset = node->num_bytes;
879 ins.type = BTRFS_EXTENT_ITEM_KEY;
881 /* record the full extent allocation */
882 ret = __btrfs_alloc_reserved_extent(trans, root,
883 node->parent, ref->root,
884 ref->generation, ref->owner_objectid,
885 &ins, node->ref_mod);
886 update_reserved_extents(root, node->bytenr,
889 /* just add one backref */
890 ret = add_extent_ref(trans, root, node->bytenr,
892 node->parent, ref->root, ref->generation,
893 ref->owner_objectid, node->ref_mod);
896 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
897 WARN_ON(insert_reserved);
898 ret = drop_delayed_ref(trans, root, node);
903 static noinline struct btrfs_delayed_ref_node *
904 select_delayed_ref(struct btrfs_delayed_ref_head *head)
906 struct rb_node *node;
907 struct btrfs_delayed_ref_node *ref;
908 int action = BTRFS_ADD_DELAYED_REF;
911 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
912 * this prevents ref count from going down to zero when
913 * there still are pending delayed ref.
915 node = rb_prev(&head->node.rb_node);
919 ref = rb_entry(node, struct btrfs_delayed_ref_node,
921 if (ref->bytenr != head->node.bytenr)
923 if (btrfs_delayed_node_to_ref(ref)->action == action)
925 node = rb_prev(node);
927 if (action == BTRFS_ADD_DELAYED_REF) {
928 action = BTRFS_DROP_DELAYED_REF;
934 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
935 struct btrfs_root *root,
936 struct list_head *cluster)
938 struct btrfs_delayed_ref_root *delayed_refs;
939 struct btrfs_delayed_ref_node *ref;
940 struct btrfs_delayed_ref_head *locked_ref = NULL;
943 int must_insert_reserved = 0;
945 delayed_refs = &trans->transaction->delayed_refs;
948 /* pick a new head ref from the cluster list */
949 if (list_empty(cluster))
952 locked_ref = list_entry(cluster->next,
953 struct btrfs_delayed_ref_head, cluster);
955 /* grab the lock that says we are going to process
956 * all the refs for this head */
957 ret = btrfs_delayed_ref_lock(trans, locked_ref);
960 * we may have dropped the spin lock to get the head
961 * mutex lock, and that might have given someone else
962 * time to free the head. If that's true, it has been
963 * removed from our list and we can move on.
965 if (ret == -EAGAIN) {
973 * record the must insert reserved flag before we
974 * drop the spin lock.
976 must_insert_reserved = locked_ref->must_insert_reserved;
977 locked_ref->must_insert_reserved = 0;
980 * locked_ref is the head node, so we have to go one
981 * node back for any delayed ref updates
983 ref = select_delayed_ref(locked_ref);
985 /* All delayed refs have been processed, Go ahead
986 * and send the head node to run_one_delayed_ref,
987 * so that any accounting fixes can happen
989 ref = &locked_ref->node;
990 list_del_init(&locked_ref->cluster);
995 rb_erase(&ref->rb_node, &delayed_refs->root);
996 delayed_refs->num_entries--;
997 spin_unlock(&delayed_refs->lock);
999 ret = run_one_delayed_ref(trans, root, ref,
1000 must_insert_reserved);
1002 btrfs_put_delayed_ref(ref);
1006 spin_lock(&delayed_refs->lock);
1012 * this starts processing the delayed reference count updates and
1013 * extent insertions we have queued up so far. count can be
1014 * 0, which means to process everything in the tree at the start
1015 * of the run (but not newly added entries), or it can be some target
1016 * number you'd like to process.
1018 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1019 struct btrfs_root *root, unsigned long count)
1021 struct rb_node *node;
1022 struct btrfs_delayed_ref_root *delayed_refs;
1023 struct btrfs_delayed_ref_node *ref;
1024 struct list_head cluster;
1026 int run_all = count == (unsigned long)-1;
1029 if (root == root->fs_info->extent_root)
1030 root = root->fs_info->tree_root;
1032 delayed_refs = &trans->transaction->delayed_refs;
1033 INIT_LIST_HEAD(&cluster);
1035 spin_lock(&delayed_refs->lock);
1037 count = delayed_refs->num_entries * 2;
1041 if (!(run_all || run_most) &&
1042 delayed_refs->num_heads_ready < 64)
1046 * go find something we can process in the rbtree. We start at
1047 * the beginning of the tree, and then build a cluster
1048 * of refs to process starting at the first one we are able to
1051 ret = btrfs_find_ref_cluster(trans, &cluster,
1052 delayed_refs->run_delayed_start);
1056 ret = run_clustered_refs(trans, root, &cluster);
1059 count -= min_t(unsigned long, ret, count);
1066 node = rb_first(&delayed_refs->root);
1069 count = (unsigned long)-1;
1072 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1074 if (btrfs_delayed_ref_is_head(ref)) {
1075 struct btrfs_delayed_ref_head *head;
1077 head = btrfs_delayed_node_to_head(ref);
1078 atomic_inc(&ref->refs);
1080 spin_unlock(&delayed_refs->lock);
1081 mutex_lock(&head->mutex);
1082 mutex_unlock(&head->mutex);
1084 btrfs_put_delayed_ref(ref);
1088 node = rb_next(node);
1090 spin_unlock(&delayed_refs->lock);
1091 schedule_timeout(1);
1095 spin_unlock(&delayed_refs->lock);
1099 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root, u64 objectid, u64 bytenr)
1102 struct btrfs_root *extent_root = root->fs_info->extent_root;
1103 struct btrfs_path *path;
1104 struct extent_buffer *leaf;
1105 struct btrfs_extent_ref *ref_item;
1106 struct btrfs_key key;
1107 struct btrfs_key found_key;
1113 key.objectid = bytenr;
1114 key.offset = (u64)-1;
1115 key.type = BTRFS_EXTENT_ITEM_KEY;
1117 path = btrfs_alloc_path();
1118 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1124 if (path->slots[0] == 0)
1128 leaf = path->nodes[0];
1129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1131 if (found_key.objectid != bytenr ||
1132 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1135 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1137 leaf = path->nodes[0];
1138 nritems = btrfs_header_nritems(leaf);
1139 if (path->slots[0] >= nritems) {
1140 ret = btrfs_next_leaf(extent_root, path);
1147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1148 if (found_key.objectid != bytenr)
1151 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1156 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1157 struct btrfs_extent_ref);
1158 ref_root = btrfs_ref_root(leaf, ref_item);
1159 if ((ref_root != root->root_key.objectid &&
1160 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1161 objectid != btrfs_ref_objectid(leaf, ref_item)) {
1165 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1174 btrfs_free_path(path);
1178 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1179 struct extent_buffer *buf, u32 nr_extents)
1181 struct btrfs_key key;
1182 struct btrfs_file_extent_item *fi;
1190 if (!root->ref_cows)
1193 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1195 root_gen = root->root_key.offset;
1198 root_gen = trans->transid - 1;
1201 level = btrfs_header_level(buf);
1202 nritems = btrfs_header_nritems(buf);
1205 struct btrfs_leaf_ref *ref;
1206 struct btrfs_extent_info *info;
1208 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1214 ref->root_gen = root_gen;
1215 ref->bytenr = buf->start;
1216 ref->owner = btrfs_header_owner(buf);
1217 ref->generation = btrfs_header_generation(buf);
1218 ref->nritems = nr_extents;
1219 info = ref->extents;
1221 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1223 btrfs_item_key_to_cpu(buf, &key, i);
1224 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1226 fi = btrfs_item_ptr(buf, i,
1227 struct btrfs_file_extent_item);
1228 if (btrfs_file_extent_type(buf, fi) ==
1229 BTRFS_FILE_EXTENT_INLINE)
1231 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1232 if (disk_bytenr == 0)
1235 info->bytenr = disk_bytenr;
1237 btrfs_file_extent_disk_num_bytes(buf, fi);
1238 info->objectid = key.objectid;
1239 info->offset = key.offset;
1243 ret = btrfs_add_leaf_ref(root, ref, shared);
1244 if (ret == -EEXIST && shared) {
1245 struct btrfs_leaf_ref *old;
1246 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1248 btrfs_remove_leaf_ref(root, old);
1249 btrfs_free_leaf_ref(root, old);
1250 ret = btrfs_add_leaf_ref(root, ref, shared);
1253 btrfs_free_leaf_ref(root, ref);
1259 /* when a block goes through cow, we update the reference counts of
1260 * everything that block points to. The internal pointers of the block
1261 * can be in just about any order, and it is likely to have clusters of
1262 * things that are close together and clusters of things that are not.
1264 * To help reduce the seeks that come with updating all of these reference
1265 * counts, sort them by byte number before actual updates are done.
1267 * struct refsort is used to match byte number to slot in the btree block.
1268 * we sort based on the byte number and then use the slot to actually
1271 * struct refsort is smaller than strcut btrfs_item and smaller than
1272 * struct btrfs_key_ptr. Since we're currently limited to the page size
1273 * for a btree block, there's no way for a kmalloc of refsorts for a
1274 * single node to be bigger than a page.
1282 * for passing into sort()
1284 static int refsort_cmp(const void *a_void, const void *b_void)
1286 const struct refsort *a = a_void;
1287 const struct refsort *b = b_void;
1289 if (a->bytenr < b->bytenr)
1291 if (a->bytenr > b->bytenr)
1297 noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root,
1299 struct extent_buffer *orig_buf,
1300 struct extent_buffer *buf, u32 *nr_extents)
1306 u64 orig_generation;
1307 struct refsort *sorted;
1309 u32 nr_file_extents = 0;
1310 struct btrfs_key key;
1311 struct btrfs_file_extent_item *fi;
1318 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1319 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1321 ref_root = btrfs_header_owner(buf);
1322 ref_generation = btrfs_header_generation(buf);
1323 orig_root = btrfs_header_owner(orig_buf);
1324 orig_generation = btrfs_header_generation(orig_buf);
1326 nritems = btrfs_header_nritems(buf);
1327 level = btrfs_header_level(buf);
1329 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1332 if (root->ref_cows) {
1333 process_func = __btrfs_inc_extent_ref;
1336 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1339 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1341 process_func = __btrfs_update_extent_ref;
1345 * we make two passes through the items. In the first pass we
1346 * only record the byte number and slot. Then we sort based on
1347 * byte number and do the actual work based on the sorted results
1349 for (i = 0; i < nritems; i++) {
1352 btrfs_item_key_to_cpu(buf, &key, i);
1353 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1355 fi = btrfs_item_ptr(buf, i,
1356 struct btrfs_file_extent_item);
1357 if (btrfs_file_extent_type(buf, fi) ==
1358 BTRFS_FILE_EXTENT_INLINE)
1360 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1365 sorted[refi].bytenr = bytenr;
1366 sorted[refi].slot = i;
1369 bytenr = btrfs_node_blockptr(buf, i);
1370 sorted[refi].bytenr = bytenr;
1371 sorted[refi].slot = i;
1376 * if refi == 0, we didn't actually put anything into the sorted
1377 * array and we're done
1382 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1384 for (i = 0; i < refi; i++) {
1386 slot = sorted[i].slot;
1387 bytenr = sorted[i].bytenr;
1390 btrfs_item_key_to_cpu(buf, &key, slot);
1391 fi = btrfs_item_ptr(buf, slot,
1392 struct btrfs_file_extent_item);
1394 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1398 ret = process_func(trans, root, bytenr,
1399 btrfs_file_extent_disk_num_bytes(buf, fi),
1400 orig_buf->start, buf->start,
1401 orig_root, ref_root,
1402 orig_generation, ref_generation,
1411 ret = process_func(trans, root, bytenr, buf->len,
1412 orig_buf->start, buf->start,
1413 orig_root, ref_root,
1414 orig_generation, ref_generation,
1427 *nr_extents = nr_file_extents;
1429 *nr_extents = nritems;
1438 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1439 struct btrfs_root *root, struct extent_buffer *orig_buf,
1440 struct extent_buffer *buf, int start_slot, int nr)
1447 u64 orig_generation;
1448 struct btrfs_key key;
1449 struct btrfs_file_extent_item *fi;
1455 BUG_ON(start_slot < 0);
1456 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1458 ref_root = btrfs_header_owner(buf);
1459 ref_generation = btrfs_header_generation(buf);
1460 orig_root = btrfs_header_owner(orig_buf);
1461 orig_generation = btrfs_header_generation(orig_buf);
1462 level = btrfs_header_level(buf);
1464 if (!root->ref_cows) {
1466 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1469 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1473 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1476 btrfs_item_key_to_cpu(buf, &key, slot);
1477 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1479 fi = btrfs_item_ptr(buf, slot,
1480 struct btrfs_file_extent_item);
1481 if (btrfs_file_extent_type(buf, fi) ==
1482 BTRFS_FILE_EXTENT_INLINE)
1484 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1487 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1488 btrfs_file_extent_disk_num_bytes(buf, fi),
1489 orig_buf->start, buf->start,
1490 orig_root, ref_root, orig_generation,
1491 ref_generation, key.objectid);
1495 bytenr = btrfs_node_blockptr(buf, slot);
1496 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1497 buf->len, orig_buf->start,
1498 buf->start, orig_root, ref_root,
1499 orig_generation, ref_generation,
1511 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1512 struct btrfs_root *root,
1513 struct btrfs_path *path,
1514 struct btrfs_block_group_cache *cache)
1517 struct btrfs_root *extent_root = root->fs_info->extent_root;
1519 struct extent_buffer *leaf;
1521 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1526 leaf = path->nodes[0];
1527 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1528 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1529 btrfs_mark_buffer_dirty(leaf);
1530 btrfs_release_path(extent_root, path);
1538 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1539 struct btrfs_root *root)
1541 struct btrfs_block_group_cache *cache, *entry;
1545 struct btrfs_path *path;
1548 path = btrfs_alloc_path();
1554 spin_lock(&root->fs_info->block_group_cache_lock);
1555 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1556 n; n = rb_next(n)) {
1557 entry = rb_entry(n, struct btrfs_block_group_cache,
1564 spin_unlock(&root->fs_info->block_group_cache_lock);
1570 last += cache->key.offset;
1572 err = write_one_cache_group(trans, root,
1575 * if we fail to write the cache group, we want
1576 * to keep it marked dirty in hopes that a later
1584 btrfs_free_path(path);
1588 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1590 struct btrfs_block_group_cache *block_group;
1593 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1594 if (!block_group || block_group->ro)
1597 put_block_group(block_group);
1601 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1602 u64 total_bytes, u64 bytes_used,
1603 struct btrfs_space_info **space_info)
1605 struct btrfs_space_info *found;
1607 found = __find_space_info(info, flags);
1609 spin_lock(&found->lock);
1610 found->total_bytes += total_bytes;
1611 found->bytes_used += bytes_used;
1613 spin_unlock(&found->lock);
1614 *space_info = found;
1617 found = kzalloc(sizeof(*found), GFP_NOFS);
1621 INIT_LIST_HEAD(&found->block_groups);
1622 init_rwsem(&found->groups_sem);
1623 spin_lock_init(&found->lock);
1624 found->flags = flags;
1625 found->total_bytes = total_bytes;
1626 found->bytes_used = bytes_used;
1627 found->bytes_pinned = 0;
1628 found->bytes_reserved = 0;
1629 found->bytes_readonly = 0;
1630 found->bytes_delalloc = 0;
1632 found->force_alloc = 0;
1633 *space_info = found;
1634 list_add_rcu(&found->list, &info->space_info);
1638 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1640 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1641 BTRFS_BLOCK_GROUP_RAID1 |
1642 BTRFS_BLOCK_GROUP_RAID10 |
1643 BTRFS_BLOCK_GROUP_DUP);
1645 if (flags & BTRFS_BLOCK_GROUP_DATA)
1646 fs_info->avail_data_alloc_bits |= extra_flags;
1647 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1648 fs_info->avail_metadata_alloc_bits |= extra_flags;
1649 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1650 fs_info->avail_system_alloc_bits |= extra_flags;
1654 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1656 spin_lock(&cache->space_info->lock);
1657 spin_lock(&cache->lock);
1659 cache->space_info->bytes_readonly += cache->key.offset -
1660 btrfs_block_group_used(&cache->item);
1663 spin_unlock(&cache->lock);
1664 spin_unlock(&cache->space_info->lock);
1667 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1669 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1671 if (num_devices == 1)
1672 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1673 if (num_devices < 4)
1674 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1676 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1677 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1678 BTRFS_BLOCK_GROUP_RAID10))) {
1679 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1682 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1683 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1684 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1687 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1688 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1689 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1690 (flags & BTRFS_BLOCK_GROUP_DUP)))
1691 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1695 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1697 struct btrfs_fs_info *info = root->fs_info;
1701 alloc_profile = info->avail_data_alloc_bits &
1702 info->data_alloc_profile;
1703 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1704 } else if (root == root->fs_info->chunk_root) {
1705 alloc_profile = info->avail_system_alloc_bits &
1706 info->system_alloc_profile;
1707 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1709 alloc_profile = info->avail_metadata_alloc_bits &
1710 info->metadata_alloc_profile;
1711 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1714 return btrfs_reduce_alloc_profile(root, data);
1717 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1721 alloc_target = btrfs_get_alloc_profile(root, 1);
1722 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1727 * for now this just makes sure we have at least 5% of our metadata space free
1730 int btrfs_check_metadata_free_space(struct btrfs_root *root)
1732 struct btrfs_fs_info *info = root->fs_info;
1733 struct btrfs_space_info *meta_sinfo;
1734 u64 alloc_target, thresh;
1735 int committed = 0, ret;
1737 /* get the space info for where the metadata will live */
1738 alloc_target = btrfs_get_alloc_profile(root, 0);
1739 meta_sinfo = __find_space_info(info, alloc_target);
1742 spin_lock(&meta_sinfo->lock);
1743 if (!meta_sinfo->full)
1744 thresh = meta_sinfo->total_bytes * 80;
1746 thresh = meta_sinfo->total_bytes * 95;
1748 do_div(thresh, 100);
1750 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1751 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
1752 struct btrfs_trans_handle *trans;
1753 if (!meta_sinfo->full) {
1754 meta_sinfo->force_alloc = 1;
1755 spin_unlock(&meta_sinfo->lock);
1757 trans = btrfs_start_transaction(root, 1);
1761 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1762 2 * 1024 * 1024, alloc_target, 0);
1763 btrfs_end_transaction(trans, root);
1766 spin_unlock(&meta_sinfo->lock);
1770 trans = btrfs_join_transaction(root, 1);
1773 ret = btrfs_commit_transaction(trans, root);
1780 spin_unlock(&meta_sinfo->lock);
1786 * This will check the space that the inode allocates from to make sure we have
1787 * enough space for bytes.
1789 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1792 struct btrfs_space_info *data_sinfo;
1793 int ret = 0, committed = 0;
1795 /* make sure bytes are sectorsize aligned */
1796 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1798 data_sinfo = BTRFS_I(inode)->space_info;
1800 /* make sure we have enough space to handle the data first */
1801 spin_lock(&data_sinfo->lock);
1802 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1803 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1804 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1805 data_sinfo->bytes_may_use < bytes) {
1806 struct btrfs_trans_handle *trans;
1809 * if we don't have enough free bytes in this space then we need
1810 * to alloc a new chunk.
1812 if (!data_sinfo->full) {
1815 data_sinfo->force_alloc = 1;
1816 spin_unlock(&data_sinfo->lock);
1818 alloc_target = btrfs_get_alloc_profile(root, 1);
1819 trans = btrfs_start_transaction(root, 1);
1823 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1824 bytes + 2 * 1024 * 1024,
1826 btrfs_end_transaction(trans, root);
1831 spin_unlock(&data_sinfo->lock);
1833 /* commit the current transaction and try again */
1836 trans = btrfs_join_transaction(root, 1);
1839 ret = btrfs_commit_transaction(trans, root);
1845 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1846 ", %llu bytes_used, %llu bytes_reserved, "
1847 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1848 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1849 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1850 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1851 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1854 data_sinfo->bytes_may_use += bytes;
1855 BTRFS_I(inode)->reserved_bytes += bytes;
1856 spin_unlock(&data_sinfo->lock);
1858 return btrfs_check_metadata_free_space(root);
1862 * if there was an error for whatever reason after calling
1863 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1865 void btrfs_free_reserved_data_space(struct btrfs_root *root,
1866 struct inode *inode, u64 bytes)
1868 struct btrfs_space_info *data_sinfo;
1870 /* make sure bytes are sectorsize aligned */
1871 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1873 data_sinfo = BTRFS_I(inode)->space_info;
1874 spin_lock(&data_sinfo->lock);
1875 data_sinfo->bytes_may_use -= bytes;
1876 BTRFS_I(inode)->reserved_bytes -= bytes;
1877 spin_unlock(&data_sinfo->lock);
1880 /* called when we are adding a delalloc extent to the inode's io_tree */
1881 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1884 struct btrfs_space_info *data_sinfo;
1886 /* get the space info for where this inode will be storing its data */
1887 data_sinfo = BTRFS_I(inode)->space_info;
1889 /* make sure we have enough space to handle the data first */
1890 spin_lock(&data_sinfo->lock);
1891 data_sinfo->bytes_delalloc += bytes;
1894 * we are adding a delalloc extent without calling
1895 * btrfs_check_data_free_space first. This happens on a weird
1896 * writepage condition, but shouldn't hurt our accounting
1898 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1899 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1900 BTRFS_I(inode)->reserved_bytes = 0;
1902 data_sinfo->bytes_may_use -= bytes;
1903 BTRFS_I(inode)->reserved_bytes -= bytes;
1906 spin_unlock(&data_sinfo->lock);
1909 /* called when we are clearing an delalloc extent from the inode's io_tree */
1910 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1913 struct btrfs_space_info *info;
1915 info = BTRFS_I(inode)->space_info;
1917 spin_lock(&info->lock);
1918 info->bytes_delalloc -= bytes;
1919 spin_unlock(&info->lock);
1922 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1923 struct btrfs_root *extent_root, u64 alloc_bytes,
1924 u64 flags, int force)
1926 struct btrfs_space_info *space_info;
1930 mutex_lock(&extent_root->fs_info->chunk_mutex);
1932 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1934 space_info = __find_space_info(extent_root->fs_info, flags);
1936 ret = update_space_info(extent_root->fs_info, flags,
1940 BUG_ON(!space_info);
1942 spin_lock(&space_info->lock);
1943 if (space_info->force_alloc) {
1945 space_info->force_alloc = 0;
1947 if (space_info->full) {
1948 spin_unlock(&space_info->lock);
1952 thresh = space_info->total_bytes - space_info->bytes_readonly;
1953 thresh = div_factor(thresh, 6);
1955 (space_info->bytes_used + space_info->bytes_pinned +
1956 space_info->bytes_reserved + alloc_bytes) < thresh) {
1957 spin_unlock(&space_info->lock);
1960 spin_unlock(&space_info->lock);
1962 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1964 space_info->full = 1;
1966 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1970 static int update_block_group(struct btrfs_trans_handle *trans,
1971 struct btrfs_root *root,
1972 u64 bytenr, u64 num_bytes, int alloc,
1975 struct btrfs_block_group_cache *cache;
1976 struct btrfs_fs_info *info = root->fs_info;
1977 u64 total = num_bytes;
1982 cache = btrfs_lookup_block_group(info, bytenr);
1985 byte_in_group = bytenr - cache->key.objectid;
1986 WARN_ON(byte_in_group > cache->key.offset);
1988 spin_lock(&cache->space_info->lock);
1989 spin_lock(&cache->lock);
1991 old_val = btrfs_block_group_used(&cache->item);
1992 num_bytes = min(total, cache->key.offset - byte_in_group);
1994 old_val += num_bytes;
1995 cache->space_info->bytes_used += num_bytes;
1997 cache->space_info->bytes_readonly -= num_bytes;
1998 btrfs_set_block_group_used(&cache->item, old_val);
1999 spin_unlock(&cache->lock);
2000 spin_unlock(&cache->space_info->lock);
2002 old_val -= num_bytes;
2003 cache->space_info->bytes_used -= num_bytes;
2005 cache->space_info->bytes_readonly += num_bytes;
2006 btrfs_set_block_group_used(&cache->item, old_val);
2007 spin_unlock(&cache->lock);
2008 spin_unlock(&cache->space_info->lock);
2012 ret = btrfs_discard_extent(root, bytenr,
2016 ret = btrfs_add_free_space(cache, bytenr,
2021 put_block_group(cache);
2023 bytenr += num_bytes;
2028 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2030 struct btrfs_block_group_cache *cache;
2033 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2037 bytenr = cache->key.objectid;
2038 put_block_group(cache);
2043 int btrfs_update_pinned_extents(struct btrfs_root *root,
2044 u64 bytenr, u64 num, int pin)
2047 struct btrfs_block_group_cache *cache;
2048 struct btrfs_fs_info *fs_info = root->fs_info;
2050 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
2052 set_extent_dirty(&fs_info->pinned_extents,
2053 bytenr, bytenr + num - 1, GFP_NOFS);
2055 clear_extent_dirty(&fs_info->pinned_extents,
2056 bytenr, bytenr + num - 1, GFP_NOFS);
2058 mutex_unlock(&root->fs_info->pinned_mutex);
2061 cache = btrfs_lookup_block_group(fs_info, bytenr);
2063 len = min(num, cache->key.offset -
2064 (bytenr - cache->key.objectid));
2066 spin_lock(&cache->space_info->lock);
2067 spin_lock(&cache->lock);
2068 cache->pinned += len;
2069 cache->space_info->bytes_pinned += len;
2070 spin_unlock(&cache->lock);
2071 spin_unlock(&cache->space_info->lock);
2072 fs_info->total_pinned += len;
2074 spin_lock(&cache->space_info->lock);
2075 spin_lock(&cache->lock);
2076 cache->pinned -= len;
2077 cache->space_info->bytes_pinned -= len;
2078 spin_unlock(&cache->lock);
2079 spin_unlock(&cache->space_info->lock);
2080 fs_info->total_pinned -= len;
2082 btrfs_add_free_space(cache, bytenr, len);
2084 put_block_group(cache);
2091 static int update_reserved_extents(struct btrfs_root *root,
2092 u64 bytenr, u64 num, int reserve)
2095 struct btrfs_block_group_cache *cache;
2096 struct btrfs_fs_info *fs_info = root->fs_info;
2099 cache = btrfs_lookup_block_group(fs_info, bytenr);
2101 len = min(num, cache->key.offset -
2102 (bytenr - cache->key.objectid));
2104 spin_lock(&cache->space_info->lock);
2105 spin_lock(&cache->lock);
2107 cache->reserved += len;
2108 cache->space_info->bytes_reserved += len;
2110 cache->reserved -= len;
2111 cache->space_info->bytes_reserved -= len;
2113 spin_unlock(&cache->lock);
2114 spin_unlock(&cache->space_info->lock);
2115 put_block_group(cache);
2122 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2127 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2130 mutex_lock(&root->fs_info->pinned_mutex);
2132 ret = find_first_extent_bit(pinned_extents, last,
2133 &start, &end, EXTENT_DIRTY);
2136 set_extent_dirty(copy, start, end, GFP_NOFS);
2139 mutex_unlock(&root->fs_info->pinned_mutex);
2143 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
2145 struct extent_io_tree *unpin)
2152 mutex_lock(&root->fs_info->pinned_mutex);
2153 ret = find_first_extent_bit(unpin, 0, &start, &end,
2158 ret = btrfs_discard_extent(root, start, end + 1 - start);
2160 /* unlocks the pinned mutex */
2161 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2162 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2166 mutex_unlock(&root->fs_info->pinned_mutex);
2170 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2171 struct btrfs_root *root,
2172 struct btrfs_path *path,
2173 u64 bytenr, u64 num_bytes, int is_data,
2174 struct extent_buffer **must_clean)
2177 struct extent_buffer *buf;
2182 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2186 /* we can reuse a block if it hasn't been written
2187 * and it is from this transaction. We can't
2188 * reuse anything from the tree log root because
2189 * it has tiny sub-transactions.
2191 if (btrfs_buffer_uptodate(buf, 0) &&
2192 btrfs_try_tree_lock(buf)) {
2193 u64 header_owner = btrfs_header_owner(buf);
2194 u64 header_transid = btrfs_header_generation(buf);
2195 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2196 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2197 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2198 header_transid == trans->transid &&
2199 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2203 btrfs_tree_unlock(buf);
2205 free_extent_buffer(buf);
2207 btrfs_set_path_blocking(path);
2208 mutex_lock(&root->fs_info->pinned_mutex);
2209 /* unlocks the pinned mutex */
2210 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2217 * remove an extent from the root, returns 0 on success
2219 static int __free_extent(struct btrfs_trans_handle *trans,
2220 struct btrfs_root *root,
2221 u64 bytenr, u64 num_bytes, u64 parent,
2222 u64 root_objectid, u64 ref_generation,
2223 u64 owner_objectid, int pin, int mark_free,
2226 struct btrfs_path *path;
2227 struct btrfs_key key;
2228 struct btrfs_fs_info *info = root->fs_info;
2229 struct btrfs_root *extent_root = info->extent_root;
2230 struct extent_buffer *leaf;
2232 int extent_slot = 0;
2233 int found_extent = 0;
2235 struct btrfs_extent_item *ei;
2238 key.objectid = bytenr;
2239 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2240 key.offset = num_bytes;
2241 path = btrfs_alloc_path();
2246 path->leave_spinning = 1;
2247 ret = lookup_extent_backref(trans, extent_root, path,
2248 bytenr, parent, root_objectid,
2249 ref_generation, owner_objectid, 1);
2251 struct btrfs_key found_key;
2252 extent_slot = path->slots[0];
2253 while (extent_slot > 0) {
2255 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2257 if (found_key.objectid != bytenr)
2259 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2260 found_key.offset == num_bytes) {
2264 if (path->slots[0] - extent_slot > 5)
2267 if (!found_extent) {
2268 ret = remove_extent_backref(trans, extent_root, path,
2271 btrfs_release_path(extent_root, path);
2272 path->leave_spinning = 1;
2273 ret = btrfs_search_slot(trans, extent_root,
2276 printk(KERN_ERR "umm, got %d back from search"
2277 ", was looking for %llu\n", ret,
2278 (unsigned long long)bytenr);
2279 btrfs_print_leaf(extent_root, path->nodes[0]);
2282 extent_slot = path->slots[0];
2285 btrfs_print_leaf(extent_root, path->nodes[0]);
2287 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2288 "parent %llu root %llu gen %llu owner %llu\n",
2289 (unsigned long long)bytenr,
2290 (unsigned long long)parent,
2291 (unsigned long long)root_objectid,
2292 (unsigned long long)ref_generation,
2293 (unsigned long long)owner_objectid);
2296 leaf = path->nodes[0];
2297 ei = btrfs_item_ptr(leaf, extent_slot,
2298 struct btrfs_extent_item);
2299 refs = btrfs_extent_refs(leaf, ei);
2302 * we're not allowed to delete the extent item if there
2303 * are other delayed ref updates pending
2306 BUG_ON(refs < refs_to_drop);
2307 refs -= refs_to_drop;
2308 btrfs_set_extent_refs(leaf, ei, refs);
2309 btrfs_mark_buffer_dirty(leaf);
2311 if (refs == 0 && found_extent &&
2312 path->slots[0] == extent_slot + 1) {
2313 struct btrfs_extent_ref *ref;
2314 ref = btrfs_item_ptr(leaf, path->slots[0],
2315 struct btrfs_extent_ref);
2316 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
2317 /* if the back ref and the extent are next to each other
2318 * they get deleted below in one shot
2320 path->slots[0] = extent_slot;
2322 } else if (found_extent) {
2323 /* otherwise delete the extent back ref */
2324 ret = remove_extent_backref(trans, extent_root, path,
2327 /* if refs are 0, we need to setup the path for deletion */
2329 btrfs_release_path(extent_root, path);
2330 path->leave_spinning = 1;
2331 ret = btrfs_search_slot(trans, extent_root, &key, path,
2340 struct extent_buffer *must_clean = NULL;
2343 ret = pin_down_bytes(trans, root, path,
2345 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2352 /* block accounting for super block */
2353 spin_lock(&info->delalloc_lock);
2354 super_used = btrfs_super_bytes_used(&info->super_copy);
2355 btrfs_set_super_bytes_used(&info->super_copy,
2356 super_used - num_bytes);
2358 /* block accounting for root item */
2359 root_used = btrfs_root_used(&root->root_item);
2360 btrfs_set_root_used(&root->root_item,
2361 root_used - num_bytes);
2362 spin_unlock(&info->delalloc_lock);
2365 * it is going to be very rare for someone to be waiting
2366 * on the block we're freeing. del_items might need to
2367 * schedule, so rather than get fancy, just force it
2371 btrfs_set_lock_blocking(must_clean);
2373 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2376 btrfs_release_path(extent_root, path);
2379 clean_tree_block(NULL, root, must_clean);
2380 btrfs_tree_unlock(must_clean);
2381 free_extent_buffer(must_clean);
2384 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2385 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2388 invalidate_mapping_pages(info->btree_inode->i_mapping,
2389 bytenr >> PAGE_CACHE_SHIFT,
2390 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
2393 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2397 btrfs_free_path(path);
2402 * remove an extent from the root, returns 0 on success
2404 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2405 struct btrfs_root *root,
2406 u64 bytenr, u64 num_bytes, u64 parent,
2407 u64 root_objectid, u64 ref_generation,
2408 u64 owner_objectid, int pin,
2411 WARN_ON(num_bytes < root->sectorsize);
2414 * if metadata always pin
2415 * if data pin when any transaction has committed this
2417 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2418 ref_generation != trans->transid)
2421 if (ref_generation != trans->transid)
2424 return __free_extent(trans, root, bytenr, num_bytes, parent,
2425 root_objectid, ref_generation,
2426 owner_objectid, pin, pin == 0, refs_to_drop);
2430 * when we free an extent, it is possible (and likely) that we free the last
2431 * delayed ref for that extent as well. This searches the delayed ref tree for
2432 * a given extent, and if there are no other delayed refs to be processed, it
2433 * removes it from the tree.
2435 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2436 struct btrfs_root *root, u64 bytenr)
2438 struct btrfs_delayed_ref_head *head;
2439 struct btrfs_delayed_ref_root *delayed_refs;
2440 struct btrfs_delayed_ref_node *ref;
2441 struct rb_node *node;
2444 delayed_refs = &trans->transaction->delayed_refs;
2445 spin_lock(&delayed_refs->lock);
2446 head = btrfs_find_delayed_ref_head(trans, bytenr);
2450 node = rb_prev(&head->node.rb_node);
2454 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2456 /* there are still entries for this ref, we can't drop it */
2457 if (ref->bytenr == bytenr)
2461 * waiting for the lock here would deadlock. If someone else has it
2462 * locked they are already in the process of dropping it anyway
2464 if (!mutex_trylock(&head->mutex))
2468 * at this point we have a head with no other entries. Go
2469 * ahead and process it.
2471 head->node.in_tree = 0;
2472 rb_erase(&head->node.rb_node, &delayed_refs->root);
2474 delayed_refs->num_entries--;
2477 * we don't take a ref on the node because we're removing it from the
2478 * tree, so we just steal the ref the tree was holding.
2480 delayed_refs->num_heads--;
2481 if (list_empty(&head->cluster))
2482 delayed_refs->num_heads_ready--;
2484 list_del_init(&head->cluster);
2485 spin_unlock(&delayed_refs->lock);
2487 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2488 &head->node, head->must_insert_reserved);
2490 btrfs_put_delayed_ref(&head->node);
2493 spin_unlock(&delayed_refs->lock);
2497 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2498 struct btrfs_root *root,
2499 u64 bytenr, u64 num_bytes, u64 parent,
2500 u64 root_objectid, u64 ref_generation,
2501 u64 owner_objectid, int pin)
2506 * tree log blocks never actually go into the extent allocation
2507 * tree, just update pinning info and exit early.
2509 * data extents referenced by the tree log do need to have
2510 * their reference counts bumped.
2512 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2513 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2514 mutex_lock(&root->fs_info->pinned_mutex);
2516 /* unlocks the pinned mutex */
2517 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2518 update_reserved_extents(root, bytenr, num_bytes, 0);
2521 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2522 root_objectid, ref_generation,
2524 BTRFS_DROP_DELAYED_REF, 1);
2526 ret = check_ref_cleanup(trans, root, bytenr);
2532 static u64 stripe_align(struct btrfs_root *root, u64 val)
2534 u64 mask = ((u64)root->stripesize - 1);
2535 u64 ret = (val + mask) & ~mask;
2540 * walks the btree of allocated extents and find a hole of a given size.
2541 * The key ins is changed to record the hole:
2542 * ins->objectid == block start
2543 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2544 * ins->offset == number of blocks
2545 * Any available blocks before search_start are skipped.
2547 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2548 struct btrfs_root *orig_root,
2549 u64 num_bytes, u64 empty_size,
2550 u64 search_start, u64 search_end,
2551 u64 hint_byte, struct btrfs_key *ins,
2552 u64 exclude_start, u64 exclude_nr,
2556 struct btrfs_root *root = orig_root->fs_info->extent_root;
2557 u64 total_needed = num_bytes;
2558 u64 *last_ptr = NULL;
2559 u64 last_wanted = 0;
2560 struct btrfs_block_group_cache *block_group = NULL;
2561 int chunk_alloc_done = 0;
2562 int empty_cluster = 2 * 1024 * 1024;
2563 int allowed_chunk_alloc = 0;
2564 struct list_head *head = NULL, *cur = NULL;
2567 struct btrfs_space_info *space_info;
2569 WARN_ON(num_bytes < root->sectorsize);
2570 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2574 if (orig_root->ref_cows || empty_size)
2575 allowed_chunk_alloc = 1;
2577 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2578 last_ptr = &root->fs_info->last_alloc;
2579 if (!btrfs_test_opt(root, SSD))
2580 empty_cluster = 64 * 1024;
2583 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2584 last_ptr = &root->fs_info->last_data_alloc;
2588 hint_byte = *last_ptr;
2589 last_wanted = *last_ptr;
2591 empty_size += empty_cluster;
2595 search_start = max(search_start, first_logical_byte(root, 0));
2596 search_start = max(search_start, hint_byte);
2598 if (last_wanted && search_start != last_wanted) {
2600 empty_size += empty_cluster;
2603 total_needed += empty_size;
2604 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
2606 block_group = btrfs_lookup_first_block_group(root->fs_info,
2608 space_info = __find_space_info(root->fs_info, data);
2610 down_read(&space_info->groups_sem);
2612 struct btrfs_free_space *free_space;
2614 * the only way this happens if our hint points to a block
2615 * group thats not of the proper type, while looping this
2616 * should never happen
2622 goto new_group_no_lock;
2624 if (unlikely(!block_group->cached)) {
2625 mutex_lock(&block_group->cache_mutex);
2626 ret = cache_block_group(root, block_group);
2627 mutex_unlock(&block_group->cache_mutex);
2632 mutex_lock(&block_group->alloc_mutex);
2633 if (unlikely(!block_group_bits(block_group, data)))
2636 if (unlikely(block_group->ro))
2639 free_space = btrfs_find_free_space(block_group, search_start,
2642 u64 start = block_group->key.objectid;
2643 u64 end = block_group->key.objectid +
2644 block_group->key.offset;
2646 search_start = stripe_align(root, free_space->offset);
2648 /* move on to the next group */
2649 if (search_start + num_bytes >= search_end)
2652 /* move on to the next group */
2653 if (search_start + num_bytes > end)
2656 if (last_wanted && search_start != last_wanted) {
2657 total_needed += empty_cluster;
2658 empty_size += empty_cluster;
2661 * if search_start is still in this block group
2662 * then we just re-search this block group
2664 if (search_start >= start &&
2665 search_start < end) {
2666 mutex_unlock(&block_group->alloc_mutex);
2670 /* else we go to the next block group */
2674 if (exclude_nr > 0 &&
2675 (search_start + num_bytes > exclude_start &&
2676 search_start < exclude_start + exclude_nr)) {
2677 search_start = exclude_start + exclude_nr;
2679 * if search_start is still in this block group
2680 * then we just re-search this block group
2682 if (search_start >= start &&
2683 search_start < end) {
2684 mutex_unlock(&block_group->alloc_mutex);
2689 /* else we go to the next block group */
2693 ins->objectid = search_start;
2694 ins->offset = num_bytes;
2696 btrfs_remove_free_space_lock(block_group, search_start,
2698 /* we are all good, lets return */
2699 mutex_unlock(&block_group->alloc_mutex);
2703 mutex_unlock(&block_group->alloc_mutex);
2704 put_block_group(block_group);
2707 /* don't try to compare new allocations against the
2708 * last allocation any more
2713 * Here's how this works.
2714 * loop == 0: we were searching a block group via a hint
2715 * and didn't find anything, so we start at
2716 * the head of the block groups and keep searching
2717 * loop == 1: we're searching through all of the block groups
2718 * if we hit the head again we have searched
2719 * all of the block groups for this space and we
2720 * need to try and allocate, if we cant error out.
2721 * loop == 2: we allocated more space and are looping through
2722 * all of the block groups again.
2725 head = &space_info->block_groups;
2728 } else if (loop == 1 && cur == head) {
2731 /* at this point we give up on the empty_size
2732 * allocations and just try to allocate the min
2735 * The extra_loop field was set if an empty_size
2736 * allocation was attempted above, and if this
2737 * is try we need to try the loop again without
2738 * the additional empty_size.
2740 total_needed -= empty_size;
2742 keep_going = extra_loop;
2745 if (allowed_chunk_alloc && !chunk_alloc_done) {
2746 up_read(&space_info->groups_sem);
2747 ret = do_chunk_alloc(trans, root, num_bytes +
2748 2 * 1024 * 1024, data, 1);
2749 down_read(&space_info->groups_sem);
2752 head = &space_info->block_groups;
2754 * we've allocated a new chunk, keep
2758 chunk_alloc_done = 1;
2759 } else if (!allowed_chunk_alloc) {
2760 space_info->force_alloc = 1;
2769 } else if (cur == head) {
2773 block_group = list_entry(cur, struct btrfs_block_group_cache,
2775 atomic_inc(&block_group->count);
2777 search_start = block_group->key.objectid;
2781 /* we found what we needed */
2782 if (ins->objectid) {
2783 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2784 trans->block_group = block_group->key.objectid;
2787 *last_ptr = ins->objectid + ins->offset;
2790 printk(KERN_ERR "btrfs searching for %llu bytes, "
2791 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2792 (unsigned long long)total_needed,
2793 (unsigned long long)num_bytes,
2794 loop, allowed_chunk_alloc);
2798 put_block_group(block_group);
2800 up_read(&space_info->groups_sem);
2804 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2806 struct btrfs_block_group_cache *cache;
2808 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2809 (unsigned long long)(info->total_bytes - info->bytes_used -
2810 info->bytes_pinned - info->bytes_reserved),
2811 (info->full) ? "" : "not ");
2812 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2813 " may_use=%llu, used=%llu\n", info->total_bytes,
2814 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2817 down_read(&info->groups_sem);
2818 list_for_each_entry(cache, &info->block_groups, list) {
2819 spin_lock(&cache->lock);
2820 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2821 "%llu pinned %llu reserved\n",
2822 (unsigned long long)cache->key.objectid,
2823 (unsigned long long)cache->key.offset,
2824 (unsigned long long)btrfs_block_group_used(&cache->item),
2825 (unsigned long long)cache->pinned,
2826 (unsigned long long)cache->reserved);
2827 btrfs_dump_free_space(cache, bytes);
2828 spin_unlock(&cache->lock);
2830 up_read(&info->groups_sem);
2833 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2834 struct btrfs_root *root,
2835 u64 num_bytes, u64 min_alloc_size,
2836 u64 empty_size, u64 hint_byte,
2837 u64 search_end, struct btrfs_key *ins,
2841 u64 search_start = 0;
2842 struct btrfs_fs_info *info = root->fs_info;
2844 data = btrfs_get_alloc_profile(root, data);
2847 * the only place that sets empty_size is btrfs_realloc_node, which
2848 * is not called recursively on allocations
2850 if (empty_size || root->ref_cows) {
2851 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2852 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2854 BTRFS_BLOCK_GROUP_METADATA |
2855 (info->metadata_alloc_profile &
2856 info->avail_metadata_alloc_bits), 0);
2858 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2859 num_bytes + 2 * 1024 * 1024, data, 0);
2862 WARN_ON(num_bytes < root->sectorsize);
2863 ret = find_free_extent(trans, root, num_bytes, empty_size,
2864 search_start, search_end, hint_byte, ins,
2865 trans->alloc_exclude_start,
2866 trans->alloc_exclude_nr, data);
2868 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2869 num_bytes = num_bytes >> 1;
2870 num_bytes = num_bytes & ~(root->sectorsize - 1);
2871 num_bytes = max(num_bytes, min_alloc_size);
2872 do_chunk_alloc(trans, root->fs_info->extent_root,
2873 num_bytes, data, 1);
2877 struct btrfs_space_info *sinfo;
2879 sinfo = __find_space_info(root->fs_info, data);
2880 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2881 "wanted %llu\n", (unsigned long long)data,
2882 (unsigned long long)num_bytes);
2883 dump_space_info(sinfo, num_bytes);
2890 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2892 struct btrfs_block_group_cache *cache;
2895 cache = btrfs_lookup_block_group(root->fs_info, start);
2897 printk(KERN_ERR "Unable to find block group for %llu\n",
2898 (unsigned long long)start);
2902 ret = btrfs_discard_extent(root, start, len);
2904 btrfs_add_free_space(cache, start, len);
2905 put_block_group(cache);
2906 update_reserved_extents(root, start, len, 0);
2911 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2912 struct btrfs_root *root,
2913 u64 num_bytes, u64 min_alloc_size,
2914 u64 empty_size, u64 hint_byte,
2915 u64 search_end, struct btrfs_key *ins,
2919 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2920 empty_size, hint_byte, search_end, ins,
2922 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2926 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2927 struct btrfs_root *root, u64 parent,
2928 u64 root_objectid, u64 ref_generation,
2929 u64 owner, struct btrfs_key *ins,
2935 u64 num_bytes = ins->offset;
2937 struct btrfs_fs_info *info = root->fs_info;
2938 struct btrfs_root *extent_root = info->extent_root;
2939 struct btrfs_extent_item *extent_item;
2940 struct btrfs_extent_ref *ref;
2941 struct btrfs_path *path;
2942 struct btrfs_key keys[2];
2945 parent = ins->objectid;
2947 /* block accounting for super block */
2948 spin_lock(&info->delalloc_lock);
2949 super_used = btrfs_super_bytes_used(&info->super_copy);
2950 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2952 /* block accounting for root item */
2953 root_used = btrfs_root_used(&root->root_item);
2954 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2955 spin_unlock(&info->delalloc_lock);
2957 memcpy(&keys[0], ins, sizeof(*ins));
2958 keys[1].objectid = ins->objectid;
2959 keys[1].type = BTRFS_EXTENT_REF_KEY;
2960 keys[1].offset = parent;
2961 sizes[0] = sizeof(*extent_item);
2962 sizes[1] = sizeof(*ref);
2964 path = btrfs_alloc_path();
2967 path->leave_spinning = 1;
2968 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2972 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2973 struct btrfs_extent_item);
2974 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
2975 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2976 struct btrfs_extent_ref);
2978 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2979 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2980 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2981 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
2983 btrfs_mark_buffer_dirty(path->nodes[0]);
2985 trans->alloc_exclude_start = 0;
2986 trans->alloc_exclude_nr = 0;
2987 btrfs_free_path(path);
2992 ret = update_block_group(trans, root, ins->objectid,
2995 printk(KERN_ERR "btrfs update block group failed for %llu "
2996 "%llu\n", (unsigned long long)ins->objectid,
2997 (unsigned long long)ins->offset);
3004 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
3005 struct btrfs_root *root, u64 parent,
3006 u64 root_objectid, u64 ref_generation,
3007 u64 owner, struct btrfs_key *ins)
3011 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3014 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3015 ins->offset, parent, root_objectid,
3016 ref_generation, owner,
3017 BTRFS_ADD_DELAYED_EXTENT, 0);
3023 * this is used by the tree logging recovery code. It records that
3024 * an extent has been allocated and makes sure to clear the free
3025 * space cache bits as well
3027 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
3028 struct btrfs_root *root, u64 parent,
3029 u64 root_objectid, u64 ref_generation,
3030 u64 owner, struct btrfs_key *ins)
3033 struct btrfs_block_group_cache *block_group;
3035 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
3036 mutex_lock(&block_group->cache_mutex);
3037 cache_block_group(root, block_group);
3038 mutex_unlock(&block_group->cache_mutex);
3040 ret = btrfs_remove_free_space(block_group, ins->objectid,
3043 put_block_group(block_group);
3044 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3045 ref_generation, owner, ins, 1);
3050 * finds a free extent and does all the dirty work required for allocation
3051 * returns the key for the extent through ins, and a tree buffer for
3052 * the first block of the extent through buf.
3054 * returns 0 if everything worked, non-zero otherwise.
3056 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3057 struct btrfs_root *root,
3058 u64 num_bytes, u64 parent, u64 min_alloc_size,
3059 u64 root_objectid, u64 ref_generation,
3060 u64 owner_objectid, u64 empty_size, u64 hint_byte,
3061 u64 search_end, struct btrfs_key *ins, u64 data)
3064 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3065 min_alloc_size, empty_size, hint_byte,
3066 search_end, ins, data);
3068 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
3069 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3070 ins->offset, parent, root_objectid,
3071 ref_generation, owner_objectid,
3072 BTRFS_ADD_DELAYED_EXTENT, 0);
3075 update_reserved_extents(root, ins->objectid, ins->offset, 1);
3079 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
3081 u64 bytenr, u32 blocksize,
3084 struct extent_buffer *buf;
3086 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3088 return ERR_PTR(-ENOMEM);
3089 btrfs_set_header_generation(buf, trans->transid);
3090 btrfs_set_buffer_lockdep_class(buf, level);
3091 btrfs_tree_lock(buf);
3092 clean_tree_block(trans, root, buf);
3094 btrfs_set_lock_blocking(buf);
3095 btrfs_set_buffer_uptodate(buf);
3097 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3098 set_extent_dirty(&root->dirty_log_pages, buf->start,
3099 buf->start + buf->len - 1, GFP_NOFS);
3101 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3102 buf->start + buf->len - 1, GFP_NOFS);
3104 trans->blocks_used++;
3105 /* this returns a buffer locked for blocking */
3110 * helper function to allocate a block for a given tree
3111 * returns the tree buffer or NULL.
3113 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3114 struct btrfs_root *root,
3115 u32 blocksize, u64 parent,
3122 struct btrfs_key ins;
3124 struct extent_buffer *buf;
3126 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3127 root_objectid, ref_generation, level,
3128 empty_size, hint, (u64)-1, &ins, 0);
3131 return ERR_PTR(ret);
3134 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3139 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root, struct extent_buffer *leaf)
3143 u64 leaf_generation;
3144 struct refsort *sorted;
3145 struct btrfs_key key;
3146 struct btrfs_file_extent_item *fi;
3153 BUG_ON(!btrfs_is_leaf(leaf));
3154 nritems = btrfs_header_nritems(leaf);
3155 leaf_owner = btrfs_header_owner(leaf);
3156 leaf_generation = btrfs_header_generation(leaf);
3158 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3159 /* we do this loop twice. The first time we build a list
3160 * of the extents we have a reference on, then we sort the list
3161 * by bytenr. The second time around we actually do the
3164 for (i = 0; i < nritems; i++) {
3168 btrfs_item_key_to_cpu(leaf, &key, i);
3170 /* only extents have references, skip everything else */
3171 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3174 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3176 /* inline extents live in the btree, they don't have refs */
3177 if (btrfs_file_extent_type(leaf, fi) ==
3178 BTRFS_FILE_EXTENT_INLINE)
3181 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3183 /* holes don't have refs */
3184 if (disk_bytenr == 0)
3187 sorted[refi].bytenr = disk_bytenr;
3188 sorted[refi].slot = i;
3195 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3197 for (i = 0; i < refi; i++) {
3200 disk_bytenr = sorted[i].bytenr;
3201 slot = sorted[i].slot;
3205 btrfs_item_key_to_cpu(leaf, &key, slot);
3206 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3209 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3211 ret = btrfs_free_extent(trans, root, disk_bytenr,
3212 btrfs_file_extent_disk_num_bytes(leaf, fi),
3213 leaf->start, leaf_owner, leaf_generation,
3217 atomic_inc(&root->fs_info->throttle_gen);
3218 wake_up(&root->fs_info->transaction_throttle);
3226 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3227 struct btrfs_root *root,
3228 struct btrfs_leaf_ref *ref)
3232 struct btrfs_extent_info *info;
3233 struct refsort *sorted;
3235 if (ref->nritems == 0)
3238 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
3239 for (i = 0; i < ref->nritems; i++) {
3240 sorted[i].bytenr = ref->extents[i].bytenr;
3243 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3246 * the items in the ref were sorted when the ref was inserted
3247 * into the ref cache, so this is already in order
3249 for (i = 0; i < ref->nritems; i++) {
3250 info = ref->extents + sorted[i].slot;
3251 ret = btrfs_free_extent(trans, root, info->bytenr,
3252 info->num_bytes, ref->bytenr,
3253 ref->owner, ref->generation,
3256 atomic_inc(&root->fs_info->throttle_gen);
3257 wake_up(&root->fs_info->transaction_throttle);
3268 static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3269 struct btrfs_root *root, u64 start,
3274 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
3277 #if 0 /* some debugging code in case we see problems here */
3278 /* if the refs count is one, it won't get increased again. But
3279 * if the ref count is > 1, someone may be decreasing it at
3280 * the same time we are.
3283 struct extent_buffer *eb = NULL;
3284 eb = btrfs_find_create_tree_block(root, start, len);
3286 btrfs_tree_lock(eb);
3288 mutex_lock(&root->fs_info->alloc_mutex);
3289 ret = lookup_extent_ref(NULL, root, start, len, refs);
3291 mutex_unlock(&root->fs_info->alloc_mutex);
3294 btrfs_tree_unlock(eb);
3295 free_extent_buffer(eb);
3298 printk(KERN_ERR "btrfs block %llu went down to one "
3299 "during drop_snap\n", (unsigned long long)start);
3310 * this is used while deleting old snapshots, and it drops the refs
3311 * on a whole subtree starting from a level 1 node.
3313 * The idea is to sort all the leaf pointers, and then drop the
3314 * ref on all the leaves in order. Most of the time the leaves
3315 * will have ref cache entries, so no leaf IOs will be required to
3316 * find the extents they have references on.
3318 * For each leaf, any references it has are also dropped in order
3320 * This ends up dropping the references in something close to optimal
3321 * order for reading and modifying the extent allocation tree.
3323 static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root,
3325 struct btrfs_path *path)
3330 struct extent_buffer *eb = path->nodes[1];
3331 struct extent_buffer *leaf;
3332 struct btrfs_leaf_ref *ref;
3333 struct refsort *sorted = NULL;
3334 int nritems = btrfs_header_nritems(eb);
3338 int slot = path->slots[1];
3339 u32 blocksize = btrfs_level_size(root, 0);
3345 root_owner = btrfs_header_owner(eb);
3346 root_gen = btrfs_header_generation(eb);
3347 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3350 * step one, sort all the leaf pointers so we don't scribble
3351 * randomly into the extent allocation tree
3353 for (i = slot; i < nritems; i++) {
3354 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3355 sorted[refi].slot = i;
3360 * nritems won't be zero, but if we're picking up drop_snapshot
3361 * after a crash, slot might be > 0, so double check things
3367 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3370 * the first loop frees everything the leaves point to
3372 for (i = 0; i < refi; i++) {
3375 bytenr = sorted[i].bytenr;
3378 * check the reference count on this leaf. If it is > 1
3379 * we just decrement it below and don't update any
3380 * of the refs the leaf points to.
3382 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3388 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3391 * the leaf only had one reference, which means the
3392 * only thing pointing to this leaf is the snapshot
3393 * we're deleting. It isn't possible for the reference
3394 * count to increase again later
3396 * The reference cache is checked for the leaf,
3397 * and if found we'll be able to drop any refs held by
3398 * the leaf without needing to read it in.
3400 ref = btrfs_lookup_leaf_ref(root, bytenr);
3401 if (ref && ref->generation != ptr_gen) {
3402 btrfs_free_leaf_ref(root, ref);
3406 ret = cache_drop_leaf_ref(trans, root, ref);
3408 btrfs_remove_leaf_ref(root, ref);
3409 btrfs_free_leaf_ref(root, ref);
3412 * the leaf wasn't in the reference cache, so
3413 * we have to read it.
3415 leaf = read_tree_block(root, bytenr, blocksize,
3417 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3419 free_extent_buffer(leaf);
3421 atomic_inc(&root->fs_info->throttle_gen);
3422 wake_up(&root->fs_info->transaction_throttle);
3427 * run through the loop again to free the refs on the leaves.
3428 * This is faster than doing it in the loop above because
3429 * the leaves are likely to be clustered together. We end up
3430 * working in nice chunks on the extent allocation tree.
3432 for (i = 0; i < refi; i++) {
3433 bytenr = sorted[i].bytenr;
3434 ret = btrfs_free_extent(trans, root, bytenr,
3435 blocksize, eb->start,
3436 root_owner, root_gen, 0, 1);
3439 atomic_inc(&root->fs_info->throttle_gen);
3440 wake_up(&root->fs_info->transaction_throttle);
3447 * update the path to show we've processed the entire level 1
3448 * node. This will get saved into the root's drop_snapshot_progress
3449 * field so these drops are not repeated again if this transaction
3452 path->slots[1] = nritems;
3457 * helper function for drop_snapshot, this walks down the tree dropping ref
3458 * counts as it goes.
3460 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3461 struct btrfs_root *root,
3462 struct btrfs_path *path, int *level)
3468 struct extent_buffer *next;
3469 struct extent_buffer *cur;
3470 struct extent_buffer *parent;
3475 WARN_ON(*level < 0);
3476 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3477 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
3478 path->nodes[*level]->len, &refs);
3484 * walk down to the last node level and free all the leaves
3486 while (*level >= 0) {
3487 WARN_ON(*level < 0);
3488 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3489 cur = path->nodes[*level];
3491 if (btrfs_header_level(cur) != *level)
3494 if (path->slots[*level] >=
3495 btrfs_header_nritems(cur))
3498 /* the new code goes down to level 1 and does all the
3499 * leaves pointed to that node in bulk. So, this check
3500 * for level 0 will always be false.
3502 * But, the disk format allows the drop_snapshot_progress
3503 * field in the root to leave things in a state where
3504 * a leaf will need cleaning up here. If someone crashes
3505 * with the old code and then boots with the new code,
3506 * we might find a leaf here.
3509 ret = btrfs_drop_leaf_ref(trans, root, cur);
3515 * once we get to level one, process the whole node
3516 * at once, including everything below it.
3519 ret = drop_level_one_refs(trans, root, path);
3524 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3525 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3526 blocksize = btrfs_level_size(root, *level - 1);
3528 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3533 * if there is more than one reference, we don't need
3534 * to read that node to drop any references it has. We
3535 * just drop the ref we hold on that node and move on to the
3536 * next slot in this level.
3539 parent = path->nodes[*level];
3540 root_owner = btrfs_header_owner(parent);
3541 root_gen = btrfs_header_generation(parent);
3542 path->slots[*level]++;
3544 ret = btrfs_free_extent(trans, root, bytenr,
3545 blocksize, parent->start,
3546 root_owner, root_gen,
3550 atomic_inc(&root->fs_info->throttle_gen);
3551 wake_up(&root->fs_info->transaction_throttle);
3558 * we need to keep freeing things in the next level down.
3559 * read the block and loop around to process it
3561 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3562 WARN_ON(*level <= 0);
3563 if (path->nodes[*level-1])
3564 free_extent_buffer(path->nodes[*level-1]);
3565 path->nodes[*level-1] = next;
3566 *level = btrfs_header_level(next);
3567 path->slots[*level] = 0;
3571 WARN_ON(*level < 0);
3572 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3574 if (path->nodes[*level] == root->node) {
3575 parent = path->nodes[*level];
3576 bytenr = path->nodes[*level]->start;
3578 parent = path->nodes[*level + 1];
3579 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3582 blocksize = btrfs_level_size(root, *level);
3583 root_owner = btrfs_header_owner(parent);
3584 root_gen = btrfs_header_generation(parent);
3587 * cleanup and free the reference on the last node
3590 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3591 parent->start, root_owner, root_gen,
3593 free_extent_buffer(path->nodes[*level]);
3594 path->nodes[*level] = NULL;
3604 * helper function for drop_subtree, this function is similar to
3605 * walk_down_tree. The main difference is that it checks reference
3606 * counts while tree blocks are locked.
3608 static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3609 struct btrfs_root *root,
3610 struct btrfs_path *path, int *level)
3612 struct extent_buffer *next;
3613 struct extent_buffer *cur;
3614 struct extent_buffer *parent;
3621 cur = path->nodes[*level];
3622 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3628 while (*level >= 0) {
3629 cur = path->nodes[*level];
3631 ret = btrfs_drop_leaf_ref(trans, root, cur);
3633 clean_tree_block(trans, root, cur);
3636 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3637 clean_tree_block(trans, root, cur);
3641 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3642 blocksize = btrfs_level_size(root, *level - 1);
3643 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3645 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3646 btrfs_tree_lock(next);
3647 btrfs_set_lock_blocking(next);
3649 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3653 parent = path->nodes[*level];
3654 ret = btrfs_free_extent(trans, root, bytenr,
3655 blocksize, parent->start,
3656 btrfs_header_owner(parent),
3657 btrfs_header_generation(parent),
3660 path->slots[*level]++;
3661 btrfs_tree_unlock(next);
3662 free_extent_buffer(next);
3666 *level = btrfs_header_level(next);
3667 path->nodes[*level] = next;
3668 path->slots[*level] = 0;
3669 path->locks[*level] = 1;
3673 parent = path->nodes[*level + 1];
3674 bytenr = path->nodes[*level]->start;
3675 blocksize = path->nodes[*level]->len;
3677 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3678 parent->start, btrfs_header_owner(parent),
3679 btrfs_header_generation(parent), *level, 1);
3682 if (path->locks[*level]) {
3683 btrfs_tree_unlock(path->nodes[*level]);
3684 path->locks[*level] = 0;
3686 free_extent_buffer(path->nodes[*level]);
3687 path->nodes[*level] = NULL;
3694 * helper for dropping snapshots. This walks back up the tree in the path
3695 * to find the first node higher up where we haven't yet gone through
3698 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
3699 struct btrfs_root *root,
3700 struct btrfs_path *path,
3701 int *level, int max_level)
3705 struct btrfs_root_item *root_item = &root->root_item;
3710 for (i = *level; i < max_level && path->nodes[i]; i++) {
3711 slot = path->slots[i];
3712 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3713 struct extent_buffer *node;
3714 struct btrfs_disk_key disk_key;
3717 * there is more work to do in this level.
3718 * Update the drop_progress marker to reflect
3719 * the work we've done so far, and then bump
3722 node = path->nodes[i];
3725 WARN_ON(*level == 0);
3726 btrfs_node_key(node, &disk_key, path->slots[i]);
3727 memcpy(&root_item->drop_progress,
3728 &disk_key, sizeof(disk_key));
3729 root_item->drop_level = i;
3732 struct extent_buffer *parent;
3735 * this whole node is done, free our reference
3736 * on it and go up one level
3738 if (path->nodes[*level] == root->node)
3739 parent = path->nodes[*level];
3741 parent = path->nodes[*level + 1];
3743 root_owner = btrfs_header_owner(parent);
3744 root_gen = btrfs_header_generation(parent);
3746 clean_tree_block(trans, root, path->nodes[*level]);
3747 ret = btrfs_free_extent(trans, root,
3748 path->nodes[*level]->start,
3749 path->nodes[*level]->len,
3750 parent->start, root_owner,
3751 root_gen, *level, 1);
3753 if (path->locks[*level]) {
3754 btrfs_tree_unlock(path->nodes[*level]);
3755 path->locks[*level] = 0;
3757 free_extent_buffer(path->nodes[*level]);
3758 path->nodes[*level] = NULL;
3766 * drop the reference count on the tree rooted at 'snap'. This traverses
3767 * the tree freeing any blocks that have a ref count of zero after being
3770 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3776 struct btrfs_path *path;
3780 struct btrfs_root_item *root_item = &root->root_item;
3782 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3783 path = btrfs_alloc_path();
3786 level = btrfs_header_level(root->node);
3788 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3789 path->nodes[level] = root->node;
3790 extent_buffer_get(root->node);
3791 path->slots[level] = 0;
3793 struct btrfs_key key;
3794 struct btrfs_disk_key found_key;
3795 struct extent_buffer *node;
3797 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3798 level = root_item->drop_level;
3799 path->lowest_level = level;
3800 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3805 node = path->nodes[level];
3806 btrfs_node_key(node, &found_key, path->slots[level]);
3807 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3808 sizeof(found_key)));
3810 * unlock our path, this is safe because only this
3811 * function is allowed to delete this snapshot
3813 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3814 if (path->nodes[i] && path->locks[i]) {
3816 btrfs_tree_unlock(path->nodes[i]);
3821 unsigned long update;
3822 wret = walk_down_tree(trans, root, path, &level);
3828 wret = walk_up_tree(trans, root, path, &level,
3834 if (trans->transaction->in_commit ||
3835 trans->transaction->delayed_refs.flushing) {
3839 atomic_inc(&root->fs_info->throttle_gen);
3840 wake_up(&root->fs_info->transaction_throttle);
3841 for (update_count = 0; update_count < 16; update_count++) {
3842 update = trans->delayed_ref_updates;
3843 trans->delayed_ref_updates = 0;
3845 btrfs_run_delayed_refs(trans, root, update);
3850 for (i = 0; i <= orig_level; i++) {
3851 if (path->nodes[i]) {
3852 free_extent_buffer(path->nodes[i]);
3853 path->nodes[i] = NULL;
3857 btrfs_free_path(path);
3861 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root,
3863 struct extent_buffer *node,
3864 struct extent_buffer *parent)
3866 struct btrfs_path *path;
3872 path = btrfs_alloc_path();
3875 btrfs_assert_tree_locked(parent);
3876 parent_level = btrfs_header_level(parent);
3877 extent_buffer_get(parent);
3878 path->nodes[parent_level] = parent;
3879 path->slots[parent_level] = btrfs_header_nritems(parent);
3881 btrfs_assert_tree_locked(node);
3882 level = btrfs_header_level(node);
3883 extent_buffer_get(node);
3884 path->nodes[level] = node;
3885 path->slots[level] = 0;
3888 wret = walk_down_subtree(trans, root, path, &level);
3894 wret = walk_up_tree(trans, root, path, &level, parent_level);
3901 btrfs_free_path(path);
3905 static unsigned long calc_ra(unsigned long start, unsigned long last,
3908 return min(last, start + nr - 1);
3911 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
3916 unsigned long first_index;
3917 unsigned long last_index;
3920 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3921 struct file_ra_state *ra;
3922 struct btrfs_ordered_extent *ordered;
3923 unsigned int total_read = 0;
3924 unsigned int total_dirty = 0;
3927 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3929 mutex_lock(&inode->i_mutex);
3930 first_index = start >> PAGE_CACHE_SHIFT;
3931 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3933 /* make sure the dirty trick played by the caller work */
3934 ret = invalidate_inode_pages2_range(inode->i_mapping,
3935 first_index, last_index);
3939 file_ra_state_init(ra, inode->i_mapping);
3941 for (i = first_index ; i <= last_index; i++) {
3942 if (total_read % ra->ra_pages == 0) {
3943 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3944 calc_ra(i, last_index, ra->ra_pages));
3948 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3950 page = grab_cache_page(inode->i_mapping, i);
3955 if (!PageUptodate(page)) {
3956 btrfs_readpage(NULL, page);
3958 if (!PageUptodate(page)) {
3960 page_cache_release(page);
3965 wait_on_page_writeback(page);
3967 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3968 page_end = page_start + PAGE_CACHE_SIZE - 1;
3969 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3971 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3973 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3975 page_cache_release(page);
3976 btrfs_start_ordered_extent(inode, ordered, 1);
3977 btrfs_put_ordered_extent(ordered);
3980 set_page_extent_mapped(page);
3982 if (i == first_index)
3983 set_extent_bits(io_tree, page_start, page_end,
3984 EXTENT_BOUNDARY, GFP_NOFS);
3985 btrfs_set_extent_delalloc(inode, page_start, page_end);
3987 set_page_dirty(page);
3990 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3992 page_cache_release(page);
3997 mutex_unlock(&inode->i_mutex);
3998 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
4002 static noinline int relocate_data_extent(struct inode *reloc_inode,
4003 struct btrfs_key *extent_key,
4006 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4007 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4008 struct extent_map *em;
4009 u64 start = extent_key->objectid - offset;
4010 u64 end = start + extent_key->offset - 1;
4012 em = alloc_extent_map(GFP_NOFS);
4013 BUG_ON(!em || IS_ERR(em));
4016 em->len = extent_key->offset;
4017 em->block_len = extent_key->offset;
4018 em->block_start = extent_key->objectid;
4019 em->bdev = root->fs_info->fs_devices->latest_bdev;
4020 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4022 /* setup extent map to cheat btrfs_readpage */
4023 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4026 spin_lock(&em_tree->lock);
4027 ret = add_extent_mapping(em_tree, em);
4028 spin_unlock(&em_tree->lock);
4029 if (ret != -EEXIST) {
4030 free_extent_map(em);
4033 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
4035 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
4037 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
4040 struct btrfs_ref_path {
4042 u64 nodes[BTRFS_MAX_LEVEL];
4044 u64 root_generation;
4051 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4052 u64 new_nodes[BTRFS_MAX_LEVEL];
4055 struct disk_extent {
4066 static int is_cowonly_root(u64 root_objectid)
4068 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4069 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4070 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4071 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
4072 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4073 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
4078 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
4079 struct btrfs_root *extent_root,
4080 struct btrfs_ref_path *ref_path,
4083 struct extent_buffer *leaf;
4084 struct btrfs_path *path;
4085 struct btrfs_extent_ref *ref;
4086 struct btrfs_key key;
4087 struct btrfs_key found_key;
4093 path = btrfs_alloc_path();
4098 ref_path->lowest_level = -1;
4099 ref_path->current_level = -1;
4100 ref_path->shared_level = -1;
4104 level = ref_path->current_level - 1;
4105 while (level >= -1) {
4107 if (level < ref_path->lowest_level)
4111 bytenr = ref_path->nodes[level];
4113 bytenr = ref_path->extent_start;
4114 BUG_ON(bytenr == 0);
4116 parent = ref_path->nodes[level + 1];
4117 ref_path->nodes[level + 1] = 0;
4118 ref_path->current_level = level;
4119 BUG_ON(parent == 0);
4121 key.objectid = bytenr;
4122 key.offset = parent + 1;
4123 key.type = BTRFS_EXTENT_REF_KEY;
4125 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4130 leaf = path->nodes[0];
4131 nritems = btrfs_header_nritems(leaf);
4132 if (path->slots[0] >= nritems) {
4133 ret = btrfs_next_leaf(extent_root, path);
4138 leaf = path->nodes[0];
4141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4142 if (found_key.objectid == bytenr &&
4143 found_key.type == BTRFS_EXTENT_REF_KEY) {
4144 if (level < ref_path->shared_level)
4145 ref_path->shared_level = level;
4150 btrfs_release_path(extent_root, path);
4153 /* reached lowest level */
4157 level = ref_path->current_level;
4158 while (level < BTRFS_MAX_LEVEL - 1) {
4162 bytenr = ref_path->nodes[level];
4164 bytenr = ref_path->extent_start;
4166 BUG_ON(bytenr == 0);
4168 key.objectid = bytenr;
4170 key.type = BTRFS_EXTENT_REF_KEY;
4172 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4176 leaf = path->nodes[0];
4177 nritems = btrfs_header_nritems(leaf);
4178 if (path->slots[0] >= nritems) {
4179 ret = btrfs_next_leaf(extent_root, path);
4183 /* the extent was freed by someone */
4184 if (ref_path->lowest_level == level)
4186 btrfs_release_path(extent_root, path);
4189 leaf = path->nodes[0];
4192 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4193 if (found_key.objectid != bytenr ||
4194 found_key.type != BTRFS_EXTENT_REF_KEY) {
4195 /* the extent was freed by someone */
4196 if (ref_path->lowest_level == level) {
4200 btrfs_release_path(extent_root, path);
4204 ref = btrfs_item_ptr(leaf, path->slots[0],
4205 struct btrfs_extent_ref);
4206 ref_objectid = btrfs_ref_objectid(leaf, ref);
4207 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4209 level = (int)ref_objectid;
4210 BUG_ON(level >= BTRFS_MAX_LEVEL);
4211 ref_path->lowest_level = level;
4212 ref_path->current_level = level;
4213 ref_path->nodes[level] = bytenr;
4215 WARN_ON(ref_objectid != level);
4218 WARN_ON(level != -1);
4222 if (ref_path->lowest_level == level) {
4223 ref_path->owner_objectid = ref_objectid;
4224 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4228 * the block is tree root or the block isn't in reference
4231 if (found_key.objectid == found_key.offset ||
4232 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4233 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4234 ref_path->root_generation =
4235 btrfs_ref_generation(leaf, ref);
4237 /* special reference from the tree log */
4238 ref_path->nodes[0] = found_key.offset;
4239 ref_path->current_level = 0;
4246 BUG_ON(ref_path->nodes[level] != 0);
4247 ref_path->nodes[level] = found_key.offset;
4248 ref_path->current_level = level;
4251 * the reference was created in the running transaction,
4252 * no need to continue walking up.
4254 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4255 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4256 ref_path->root_generation =
4257 btrfs_ref_generation(leaf, ref);
4262 btrfs_release_path(extent_root, path);
4265 /* reached max tree level, but no tree root found. */
4268 btrfs_free_path(path);
4272 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4273 struct btrfs_root *extent_root,
4274 struct btrfs_ref_path *ref_path,
4277 memset(ref_path, 0, sizeof(*ref_path));
4278 ref_path->extent_start = extent_start;
4280 return __next_ref_path(trans, extent_root, ref_path, 1);
4283 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4284 struct btrfs_root *extent_root,
4285 struct btrfs_ref_path *ref_path)
4287 return __next_ref_path(trans, extent_root, ref_path, 0);
4290 static noinline int get_new_locations(struct inode *reloc_inode,
4291 struct btrfs_key *extent_key,
4292 u64 offset, int no_fragment,
4293 struct disk_extent **extents,
4296 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4297 struct btrfs_path *path;
4298 struct btrfs_file_extent_item *fi;
4299 struct extent_buffer *leaf;
4300 struct disk_extent *exts = *extents;
4301 struct btrfs_key found_key;
4306 int max = *nr_extents;
4309 WARN_ON(!no_fragment && *extents);
4312 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4317 path = btrfs_alloc_path();
4320 cur_pos = extent_key->objectid - offset;
4321 last_byte = extent_key->objectid + extent_key->offset;
4322 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4332 leaf = path->nodes[0];
4333 nritems = btrfs_header_nritems(leaf);
4334 if (path->slots[0] >= nritems) {
4335 ret = btrfs_next_leaf(root, path);
4340 leaf = path->nodes[0];
4343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4344 if (found_key.offset != cur_pos ||
4345 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4346 found_key.objectid != reloc_inode->i_ino)
4349 fi = btrfs_item_ptr(leaf, path->slots[0],
4350 struct btrfs_file_extent_item);
4351 if (btrfs_file_extent_type(leaf, fi) !=
4352 BTRFS_FILE_EXTENT_REG ||
4353 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4357 struct disk_extent *old = exts;
4359 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4360 memcpy(exts, old, sizeof(*exts) * nr);
4361 if (old != *extents)
4365 exts[nr].disk_bytenr =
4366 btrfs_file_extent_disk_bytenr(leaf, fi);
4367 exts[nr].disk_num_bytes =
4368 btrfs_file_extent_disk_num_bytes(leaf, fi);
4369 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4370 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4371 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4372 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4373 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4374 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4376 BUG_ON(exts[nr].offset > 0);
4377 BUG_ON(exts[nr].compression || exts[nr].encryption);
4378 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4380 cur_pos += exts[nr].num_bytes;
4383 if (cur_pos + offset >= last_byte)
4393 BUG_ON(cur_pos + offset > last_byte);
4394 if (cur_pos + offset < last_byte) {
4400 btrfs_free_path(path);
4402 if (exts != *extents)
4411 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4412 struct btrfs_root *root,
4413 struct btrfs_path *path,
4414 struct btrfs_key *extent_key,
4415 struct btrfs_key *leaf_key,
4416 struct btrfs_ref_path *ref_path,
4417 struct disk_extent *new_extents,
4420 struct extent_buffer *leaf;
4421 struct btrfs_file_extent_item *fi;
4422 struct inode *inode = NULL;
4423 struct btrfs_key key;
4428 u64 search_end = (u64)-1;
4431 int extent_locked = 0;
4435 memcpy(&key, leaf_key, sizeof(key));
4436 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4437 if (key.objectid < ref_path->owner_objectid ||
4438 (key.objectid == ref_path->owner_objectid &&
4439 key.type < BTRFS_EXTENT_DATA_KEY)) {
4440 key.objectid = ref_path->owner_objectid;
4441 key.type = BTRFS_EXTENT_DATA_KEY;
4447 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4451 leaf = path->nodes[0];
4452 nritems = btrfs_header_nritems(leaf);
4454 if (extent_locked && ret > 0) {
4456 * the file extent item was modified by someone
4457 * before the extent got locked.
4459 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4460 lock_end, GFP_NOFS);
4464 if (path->slots[0] >= nritems) {
4465 if (++nr_scaned > 2)
4468 BUG_ON(extent_locked);
4469 ret = btrfs_next_leaf(root, path);
4474 leaf = path->nodes[0];
4475 nritems = btrfs_header_nritems(leaf);
4478 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4480 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4481 if ((key.objectid > ref_path->owner_objectid) ||
4482 (key.objectid == ref_path->owner_objectid &&
4483 key.type > BTRFS_EXTENT_DATA_KEY) ||
4484 key.offset >= search_end)
4488 if (inode && key.objectid != inode->i_ino) {
4489 BUG_ON(extent_locked);
4490 btrfs_release_path(root, path);
4491 mutex_unlock(&inode->i_mutex);
4497 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4502 fi = btrfs_item_ptr(leaf, path->slots[0],
4503 struct btrfs_file_extent_item);
4504 extent_type = btrfs_file_extent_type(leaf, fi);
4505 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4506 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4507 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4508 extent_key->objectid)) {
4514 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4515 ext_offset = btrfs_file_extent_offset(leaf, fi);
4517 if (search_end == (u64)-1) {
4518 search_end = key.offset - ext_offset +
4519 btrfs_file_extent_ram_bytes(leaf, fi);
4522 if (!extent_locked) {
4523 lock_start = key.offset;
4524 lock_end = lock_start + num_bytes - 1;
4526 if (lock_start > key.offset ||
4527 lock_end + 1 < key.offset + num_bytes) {
4528 unlock_extent(&BTRFS_I(inode)->io_tree,
4529 lock_start, lock_end, GFP_NOFS);
4535 btrfs_release_path(root, path);
4537 inode = btrfs_iget_locked(root->fs_info->sb,
4538 key.objectid, root);
4539 if (inode->i_state & I_NEW) {
4540 BTRFS_I(inode)->root = root;
4541 BTRFS_I(inode)->location.objectid =
4543 BTRFS_I(inode)->location.type =
4544 BTRFS_INODE_ITEM_KEY;
4545 BTRFS_I(inode)->location.offset = 0;
4546 btrfs_read_locked_inode(inode);
4547 unlock_new_inode(inode);
4550 * some code call btrfs_commit_transaction while
4551 * holding the i_mutex, so we can't use mutex_lock
4554 if (is_bad_inode(inode) ||
4555 !mutex_trylock(&inode->i_mutex)) {
4558 key.offset = (u64)-1;
4563 if (!extent_locked) {
4564 struct btrfs_ordered_extent *ordered;
4566 btrfs_release_path(root, path);
4568 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4569 lock_end, GFP_NOFS);
4570 ordered = btrfs_lookup_first_ordered_extent(inode,
4573 ordered->file_offset <= lock_end &&
4574 ordered->file_offset + ordered->len > lock_start) {
4575 unlock_extent(&BTRFS_I(inode)->io_tree,
4576 lock_start, lock_end, GFP_NOFS);
4577 btrfs_start_ordered_extent(inode, ordered, 1);
4578 btrfs_put_ordered_extent(ordered);
4579 key.offset += num_bytes;
4583 btrfs_put_ordered_extent(ordered);
4589 if (nr_extents == 1) {
4590 /* update extent pointer in place */
4591 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4592 new_extents[0].disk_bytenr);
4593 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4594 new_extents[0].disk_num_bytes);
4595 btrfs_mark_buffer_dirty(leaf);
4597 btrfs_drop_extent_cache(inode, key.offset,
4598 key.offset + num_bytes - 1, 0);
4600 ret = btrfs_inc_extent_ref(trans, root,
4601 new_extents[0].disk_bytenr,
4602 new_extents[0].disk_num_bytes,
4604 root->root_key.objectid,
4609 ret = btrfs_free_extent(trans, root,
4610 extent_key->objectid,
4613 btrfs_header_owner(leaf),
4614 btrfs_header_generation(leaf),
4618 btrfs_release_path(root, path);
4619 key.offset += num_bytes;
4627 * drop old extent pointer at first, then insert the
4628 * new pointers one bye one
4630 btrfs_release_path(root, path);
4631 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4632 key.offset + num_bytes,
4633 key.offset, &alloc_hint);
4636 for (i = 0; i < nr_extents; i++) {
4637 if (ext_offset >= new_extents[i].num_bytes) {
4638 ext_offset -= new_extents[i].num_bytes;
4641 extent_len = min(new_extents[i].num_bytes -
4642 ext_offset, num_bytes);
4644 ret = btrfs_insert_empty_item(trans, root,
4649 leaf = path->nodes[0];
4650 fi = btrfs_item_ptr(leaf, path->slots[0],
4651 struct btrfs_file_extent_item);
4652 btrfs_set_file_extent_generation(leaf, fi,
4654 btrfs_set_file_extent_type(leaf, fi,
4655 BTRFS_FILE_EXTENT_REG);
4656 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4657 new_extents[i].disk_bytenr);
4658 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4659 new_extents[i].disk_num_bytes);
4660 btrfs_set_file_extent_ram_bytes(leaf, fi,
4661 new_extents[i].ram_bytes);
4663 btrfs_set_file_extent_compression(leaf, fi,
4664 new_extents[i].compression);
4665 btrfs_set_file_extent_encryption(leaf, fi,
4666 new_extents[i].encryption);
4667 btrfs_set_file_extent_other_encoding(leaf, fi,
4668 new_extents[i].other_encoding);
4670 btrfs_set_file_extent_num_bytes(leaf, fi,
4672 ext_offset += new_extents[i].offset;
4673 btrfs_set_file_extent_offset(leaf, fi,
4675 btrfs_mark_buffer_dirty(leaf);
4677 btrfs_drop_extent_cache(inode, key.offset,
4678 key.offset + extent_len - 1, 0);
4680 ret = btrfs_inc_extent_ref(trans, root,
4681 new_extents[i].disk_bytenr,
4682 new_extents[i].disk_num_bytes,
4684 root->root_key.objectid,
4685 trans->transid, key.objectid);
4687 btrfs_release_path(root, path);
4689 inode_add_bytes(inode, extent_len);
4692 num_bytes -= extent_len;
4693 key.offset += extent_len;
4698 BUG_ON(i >= nr_extents);
4702 if (extent_locked) {
4703 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4704 lock_end, GFP_NOFS);
4708 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4709 key.offset >= search_end)
4716 btrfs_release_path(root, path);
4718 mutex_unlock(&inode->i_mutex);
4719 if (extent_locked) {
4720 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4721 lock_end, GFP_NOFS);
4728 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4729 struct btrfs_root *root,
4730 struct extent_buffer *buf, u64 orig_start)
4735 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4736 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4738 level = btrfs_header_level(buf);
4740 struct btrfs_leaf_ref *ref;
4741 struct btrfs_leaf_ref *orig_ref;
4743 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4747 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4749 btrfs_free_leaf_ref(root, orig_ref);
4753 ref->nritems = orig_ref->nritems;
4754 memcpy(ref->extents, orig_ref->extents,
4755 sizeof(ref->extents[0]) * ref->nritems);
4757 btrfs_free_leaf_ref(root, orig_ref);
4759 ref->root_gen = trans->transid;
4760 ref->bytenr = buf->start;
4761 ref->owner = btrfs_header_owner(buf);
4762 ref->generation = btrfs_header_generation(buf);
4764 ret = btrfs_add_leaf_ref(root, ref, 0);
4766 btrfs_free_leaf_ref(root, ref);
4771 static noinline int invalidate_extent_cache(struct btrfs_root *root,
4772 struct extent_buffer *leaf,
4773 struct btrfs_block_group_cache *group,
4774 struct btrfs_root *target_root)
4776 struct btrfs_key key;
4777 struct inode *inode = NULL;
4778 struct btrfs_file_extent_item *fi;
4780 u64 skip_objectid = 0;
4784 nritems = btrfs_header_nritems(leaf);
4785 for (i = 0; i < nritems; i++) {
4786 btrfs_item_key_to_cpu(leaf, &key, i);
4787 if (key.objectid == skip_objectid ||
4788 key.type != BTRFS_EXTENT_DATA_KEY)
4790 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4791 if (btrfs_file_extent_type(leaf, fi) ==
4792 BTRFS_FILE_EXTENT_INLINE)
4794 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4796 if (!inode || inode->i_ino != key.objectid) {
4798 inode = btrfs_ilookup(target_root->fs_info->sb,
4799 key.objectid, target_root, 1);
4802 skip_objectid = key.objectid;
4805 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4807 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4808 key.offset + num_bytes - 1, GFP_NOFS);
4809 btrfs_drop_extent_cache(inode, key.offset,
4810 key.offset + num_bytes - 1, 1);
4811 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4812 key.offset + num_bytes - 1, GFP_NOFS);
4819 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4820 struct btrfs_root *root,
4821 struct extent_buffer *leaf,
4822 struct btrfs_block_group_cache *group,
4823 struct inode *reloc_inode)
4825 struct btrfs_key key;
4826 struct btrfs_key extent_key;
4827 struct btrfs_file_extent_item *fi;
4828 struct btrfs_leaf_ref *ref;
4829 struct disk_extent *new_extent;
4838 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4839 BUG_ON(!new_extent);
4841 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4845 nritems = btrfs_header_nritems(leaf);
4846 for (i = 0; i < nritems; i++) {
4847 btrfs_item_key_to_cpu(leaf, &key, i);
4848 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4850 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4851 if (btrfs_file_extent_type(leaf, fi) ==
4852 BTRFS_FILE_EXTENT_INLINE)
4854 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4855 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4860 if (bytenr >= group->key.objectid + group->key.offset ||
4861 bytenr + num_bytes <= group->key.objectid)
4864 extent_key.objectid = bytenr;
4865 extent_key.offset = num_bytes;
4866 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4868 ret = get_new_locations(reloc_inode, &extent_key,
4869 group->key.objectid, 1,
4870 &new_extent, &nr_extent);
4875 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4876 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4877 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4878 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4880 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4881 new_extent->disk_bytenr);
4882 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4883 new_extent->disk_num_bytes);
4884 btrfs_mark_buffer_dirty(leaf);
4886 ret = btrfs_inc_extent_ref(trans, root,
4887 new_extent->disk_bytenr,
4888 new_extent->disk_num_bytes,
4890 root->root_key.objectid,
4891 trans->transid, key.objectid);
4894 ret = btrfs_free_extent(trans, root,
4895 bytenr, num_bytes, leaf->start,
4896 btrfs_header_owner(leaf),
4897 btrfs_header_generation(leaf),
4903 BUG_ON(ext_index + 1 != ref->nritems);
4904 btrfs_free_leaf_ref(root, ref);
4908 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4909 struct btrfs_root *root)
4911 struct btrfs_root *reloc_root;
4914 if (root->reloc_root) {
4915 reloc_root = root->reloc_root;
4916 root->reloc_root = NULL;
4917 list_add(&reloc_root->dead_list,
4918 &root->fs_info->dead_reloc_roots);
4920 btrfs_set_root_bytenr(&reloc_root->root_item,
4921 reloc_root->node->start);
4922 btrfs_set_root_level(&root->root_item,
4923 btrfs_header_level(reloc_root->node));
4924 memset(&reloc_root->root_item.drop_progress, 0,
4925 sizeof(struct btrfs_disk_key));
4926 reloc_root->root_item.drop_level = 0;
4928 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4929 &reloc_root->root_key,
4930 &reloc_root->root_item);
4936 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4938 struct btrfs_trans_handle *trans;
4939 struct btrfs_root *reloc_root;
4940 struct btrfs_root *prev_root = NULL;
4941 struct list_head dead_roots;
4945 INIT_LIST_HEAD(&dead_roots);
4946 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4948 while (!list_empty(&dead_roots)) {
4949 reloc_root = list_entry(dead_roots.prev,
4950 struct btrfs_root, dead_list);
4951 list_del_init(&reloc_root->dead_list);
4953 BUG_ON(reloc_root->commit_root != NULL);
4955 trans = btrfs_join_transaction(root, 1);
4958 mutex_lock(&root->fs_info->drop_mutex);
4959 ret = btrfs_drop_snapshot(trans, reloc_root);
4962 mutex_unlock(&root->fs_info->drop_mutex);
4964 nr = trans->blocks_used;
4965 ret = btrfs_end_transaction(trans, root);
4967 btrfs_btree_balance_dirty(root, nr);
4970 free_extent_buffer(reloc_root->node);
4972 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4973 &reloc_root->root_key);
4975 mutex_unlock(&root->fs_info->drop_mutex);
4977 nr = trans->blocks_used;
4978 ret = btrfs_end_transaction(trans, root);
4980 btrfs_btree_balance_dirty(root, nr);
4983 prev_root = reloc_root;
4986 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4992 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4994 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4998 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5000 struct btrfs_root *reloc_root;
5001 struct btrfs_trans_handle *trans;
5002 struct btrfs_key location;
5006 mutex_lock(&root->fs_info->tree_reloc_mutex);
5007 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5009 found = !list_empty(&root->fs_info->dead_reloc_roots);
5010 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5013 trans = btrfs_start_transaction(root, 1);
5015 ret = btrfs_commit_transaction(trans, root);
5019 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5020 location.offset = (u64)-1;
5021 location.type = BTRFS_ROOT_ITEM_KEY;
5023 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5024 BUG_ON(!reloc_root);
5025 btrfs_orphan_cleanup(reloc_root);
5029 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
5030 struct btrfs_root *root)
5032 struct btrfs_root *reloc_root;
5033 struct extent_buffer *eb;
5034 struct btrfs_root_item *root_item;
5035 struct btrfs_key root_key;
5038 BUG_ON(!root->ref_cows);
5039 if (root->reloc_root)
5042 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5045 ret = btrfs_copy_root(trans, root, root->commit_root,
5046 &eb, BTRFS_TREE_RELOC_OBJECTID);
5049 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5050 root_key.offset = root->root_key.objectid;
5051 root_key.type = BTRFS_ROOT_ITEM_KEY;
5053 memcpy(root_item, &root->root_item, sizeof(root_item));
5054 btrfs_set_root_refs(root_item, 0);
5055 btrfs_set_root_bytenr(root_item, eb->start);
5056 btrfs_set_root_level(root_item, btrfs_header_level(eb));
5057 btrfs_set_root_generation(root_item, trans->transid);
5059 btrfs_tree_unlock(eb);
5060 free_extent_buffer(eb);
5062 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5063 &root_key, root_item);
5067 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5069 BUG_ON(!reloc_root);
5070 reloc_root->last_trans = trans->transid;
5071 reloc_root->commit_root = NULL;
5072 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5074 root->reloc_root = reloc_root;
5079 * Core function of space balance.
5081 * The idea is using reloc trees to relocate tree blocks in reference
5082 * counted roots. There is one reloc tree for each subvol, and all
5083 * reloc trees share same root key objectid. Reloc trees are snapshots
5084 * of the latest committed roots of subvols (root->commit_root).
5086 * To relocate a tree block referenced by a subvol, there are two steps.
5087 * COW the block through subvol's reloc tree, then update block pointer
5088 * in the subvol to point to the new block. Since all reloc trees share
5089 * same root key objectid, doing special handing for tree blocks owned
5090 * by them is easy. Once a tree block has been COWed in one reloc tree,
5091 * we can use the resulting new block directly when the same block is
5092 * required to COW again through other reloc trees. By this way, relocated
5093 * tree blocks are shared between reloc trees, so they are also shared
5096 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5097 struct btrfs_root *root,
5098 struct btrfs_path *path,
5099 struct btrfs_key *first_key,
5100 struct btrfs_ref_path *ref_path,
5101 struct btrfs_block_group_cache *group,
5102 struct inode *reloc_inode)
5104 struct btrfs_root *reloc_root;
5105 struct extent_buffer *eb = NULL;
5106 struct btrfs_key *keys;
5110 int lowest_level = 0;
5113 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5114 lowest_level = ref_path->owner_objectid;
5116 if (!root->ref_cows) {
5117 path->lowest_level = lowest_level;
5118 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5120 path->lowest_level = 0;
5121 btrfs_release_path(root, path);
5125 mutex_lock(&root->fs_info->tree_reloc_mutex);
5126 ret = init_reloc_tree(trans, root);
5128 reloc_root = root->reloc_root;
5130 shared_level = ref_path->shared_level;
5131 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5133 keys = ref_path->node_keys;
5134 nodes = ref_path->new_nodes;
5135 memset(&keys[shared_level + 1], 0,
5136 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5137 memset(&nodes[shared_level + 1], 0,
5138 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5140 if (nodes[lowest_level] == 0) {
5141 path->lowest_level = lowest_level;
5142 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5145 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5146 eb = path->nodes[level];
5147 if (!eb || eb == reloc_root->node)
5149 nodes[level] = eb->start;
5151 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5153 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5156 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5157 eb = path->nodes[0];
5158 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5159 group, reloc_inode);
5162 btrfs_release_path(reloc_root, path);
5164 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5170 * replace tree blocks in the fs tree with tree blocks in
5173 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5176 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5177 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5180 extent_buffer_get(path->nodes[0]);
5181 eb = path->nodes[0];
5182 btrfs_release_path(reloc_root, path);
5183 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5185 free_extent_buffer(eb);
5188 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5189 path->lowest_level = 0;
5193 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5194 struct btrfs_root *root,
5195 struct btrfs_path *path,
5196 struct btrfs_key *first_key,
5197 struct btrfs_ref_path *ref_path)
5201 ret = relocate_one_path(trans, root, path, first_key,
5202 ref_path, NULL, NULL);
5208 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5209 struct btrfs_root *extent_root,
5210 struct btrfs_path *path,
5211 struct btrfs_key *extent_key)
5215 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5218 ret = btrfs_del_item(trans, extent_root, path);
5220 btrfs_release_path(extent_root, path);
5224 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5225 struct btrfs_ref_path *ref_path)
5227 struct btrfs_key root_key;
5229 root_key.objectid = ref_path->root_objectid;
5230 root_key.type = BTRFS_ROOT_ITEM_KEY;
5231 if (is_cowonly_root(ref_path->root_objectid))
5232 root_key.offset = 0;
5234 root_key.offset = (u64)-1;
5236 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5239 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5240 struct btrfs_path *path,
5241 struct btrfs_key *extent_key,
5242 struct btrfs_block_group_cache *group,
5243 struct inode *reloc_inode, int pass)
5245 struct btrfs_trans_handle *trans;
5246 struct btrfs_root *found_root;
5247 struct btrfs_ref_path *ref_path = NULL;
5248 struct disk_extent *new_extents = NULL;
5253 struct btrfs_key first_key;
5257 trans = btrfs_start_transaction(extent_root, 1);
5260 if (extent_key->objectid == 0) {
5261 ret = del_extent_zero(trans, extent_root, path, extent_key);
5265 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5271 for (loops = 0; ; loops++) {
5273 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5274 extent_key->objectid);
5276 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5283 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5284 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5287 found_root = read_ref_root(extent_root->fs_info, ref_path);
5288 BUG_ON(!found_root);
5290 * for reference counted tree, only process reference paths
5291 * rooted at the latest committed root.
5293 if (found_root->ref_cows &&
5294 ref_path->root_generation != found_root->root_key.offset)
5297 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5300 * copy data extents to new locations
5302 u64 group_start = group->key.objectid;
5303 ret = relocate_data_extent(reloc_inode,
5312 level = ref_path->owner_objectid;
5315 if (prev_block != ref_path->nodes[level]) {
5316 struct extent_buffer *eb;
5317 u64 block_start = ref_path->nodes[level];
5318 u64 block_size = btrfs_level_size(found_root, level);
5320 eb = read_tree_block(found_root, block_start,
5322 btrfs_tree_lock(eb);
5323 BUG_ON(level != btrfs_header_level(eb));
5326 btrfs_item_key_to_cpu(eb, &first_key, 0);
5328 btrfs_node_key_to_cpu(eb, &first_key, 0);
5330 btrfs_tree_unlock(eb);
5331 free_extent_buffer(eb);
5332 prev_block = block_start;
5335 mutex_lock(&extent_root->fs_info->trans_mutex);
5336 btrfs_record_root_in_trans(found_root);
5337 mutex_unlock(&extent_root->fs_info->trans_mutex);
5338 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5340 * try to update data extent references while
5341 * keeping metadata shared between snapshots.
5344 ret = relocate_one_path(trans, found_root,
5345 path, &first_key, ref_path,
5346 group, reloc_inode);
5352 * use fallback method to process the remaining
5356 u64 group_start = group->key.objectid;
5357 new_extents = kmalloc(sizeof(*new_extents),
5360 ret = get_new_locations(reloc_inode,
5368 ret = replace_one_extent(trans, found_root,
5370 &first_key, ref_path,
5371 new_extents, nr_extents);
5373 ret = relocate_tree_block(trans, found_root, path,
5374 &first_key, ref_path);
5381 btrfs_end_transaction(trans, extent_root);
5387 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5390 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5391 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5393 num_devices = root->fs_info->fs_devices->rw_devices;
5394 if (num_devices == 1) {
5395 stripped |= BTRFS_BLOCK_GROUP_DUP;
5396 stripped = flags & ~stripped;
5398 /* turn raid0 into single device chunks */
5399 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5402 /* turn mirroring into duplication */
5403 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5404 BTRFS_BLOCK_GROUP_RAID10))
5405 return stripped | BTRFS_BLOCK_GROUP_DUP;
5408 /* they already had raid on here, just return */
5409 if (flags & stripped)
5412 stripped |= BTRFS_BLOCK_GROUP_DUP;
5413 stripped = flags & ~stripped;
5415 /* switch duplicated blocks with raid1 */
5416 if (flags & BTRFS_BLOCK_GROUP_DUP)
5417 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5419 /* turn single device chunks into raid0 */
5420 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5425 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5426 struct btrfs_block_group_cache *shrink_block_group,
5429 struct btrfs_trans_handle *trans;
5430 u64 new_alloc_flags;
5433 spin_lock(&shrink_block_group->lock);
5434 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5435 spin_unlock(&shrink_block_group->lock);
5437 trans = btrfs_start_transaction(root, 1);
5438 spin_lock(&shrink_block_group->lock);
5440 new_alloc_flags = update_block_group_flags(root,
5441 shrink_block_group->flags);
5442 if (new_alloc_flags != shrink_block_group->flags) {
5444 btrfs_block_group_used(&shrink_block_group->item);
5446 calc = shrink_block_group->key.offset;
5448 spin_unlock(&shrink_block_group->lock);
5450 do_chunk_alloc(trans, root->fs_info->extent_root,
5451 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5453 btrfs_end_transaction(trans, root);
5455 spin_unlock(&shrink_block_group->lock);
5459 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5460 struct btrfs_root *root,
5461 u64 objectid, u64 size)
5463 struct btrfs_path *path;
5464 struct btrfs_inode_item *item;
5465 struct extent_buffer *leaf;
5468 path = btrfs_alloc_path();
5472 path->leave_spinning = 1;
5473 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5477 leaf = path->nodes[0];
5478 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5479 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5480 btrfs_set_inode_generation(leaf, item, 1);
5481 btrfs_set_inode_size(leaf, item, size);
5482 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5483 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5484 btrfs_mark_buffer_dirty(leaf);
5485 btrfs_release_path(root, path);
5487 btrfs_free_path(path);
5491 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5492 struct btrfs_block_group_cache *group)
5494 struct inode *inode = NULL;
5495 struct btrfs_trans_handle *trans;
5496 struct btrfs_root *root;
5497 struct btrfs_key root_key;
5498 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5501 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5502 root_key.type = BTRFS_ROOT_ITEM_KEY;
5503 root_key.offset = (u64)-1;
5504 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5506 return ERR_CAST(root);
5508 trans = btrfs_start_transaction(root, 1);
5511 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5515 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5518 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5519 group->key.offset, 0, group->key.offset,
5523 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5524 if (inode->i_state & I_NEW) {
5525 BTRFS_I(inode)->root = root;
5526 BTRFS_I(inode)->location.objectid = objectid;
5527 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5528 BTRFS_I(inode)->location.offset = 0;
5529 btrfs_read_locked_inode(inode);
5530 unlock_new_inode(inode);
5531 BUG_ON(is_bad_inode(inode));
5535 BTRFS_I(inode)->index_cnt = group->key.objectid;
5537 err = btrfs_orphan_add(trans, inode);
5539 btrfs_end_transaction(trans, root);
5543 inode = ERR_PTR(err);
5548 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5551 struct btrfs_ordered_sum *sums;
5552 struct btrfs_sector_sum *sector_sum;
5553 struct btrfs_ordered_extent *ordered;
5554 struct btrfs_root *root = BTRFS_I(inode)->root;
5555 struct list_head list;
5560 INIT_LIST_HEAD(&list);
5562 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5563 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5565 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5566 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
5567 disk_bytenr + len - 1, &list);
5569 while (!list_empty(&list)) {
5570 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5571 list_del_init(&sums->list);
5573 sector_sum = sums->sums;
5574 sums->bytenr = ordered->start;
5577 while (offset < sums->len) {
5578 sector_sum->bytenr += ordered->start - disk_bytenr;
5580 offset += root->sectorsize;
5583 btrfs_add_ordered_sum(inode, ordered, sums);
5585 btrfs_put_ordered_extent(ordered);
5589 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5591 struct btrfs_trans_handle *trans;
5592 struct btrfs_path *path;
5593 struct btrfs_fs_info *info = root->fs_info;
5594 struct extent_buffer *leaf;
5595 struct inode *reloc_inode;
5596 struct btrfs_block_group_cache *block_group;
5597 struct btrfs_key key;
5606 root = root->fs_info->extent_root;
5608 block_group = btrfs_lookup_block_group(info, group_start);
5609 BUG_ON(!block_group);
5611 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5612 (unsigned long long)block_group->key.objectid,
5613 (unsigned long long)block_group->flags);
5615 path = btrfs_alloc_path();
5618 reloc_inode = create_reloc_inode(info, block_group);
5619 BUG_ON(IS_ERR(reloc_inode));
5621 __alloc_chunk_for_shrink(root, block_group, 1);
5622 set_block_group_readonly(block_group);
5624 btrfs_start_delalloc_inodes(info->tree_root);
5625 btrfs_wait_ordered_extents(info->tree_root, 0);
5630 key.objectid = block_group->key.objectid;
5633 cur_byte = key.objectid;
5635 trans = btrfs_start_transaction(info->tree_root, 1);
5636 btrfs_commit_transaction(trans, info->tree_root);
5638 mutex_lock(&root->fs_info->cleaner_mutex);
5639 btrfs_clean_old_snapshots(info->tree_root);
5640 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5641 mutex_unlock(&root->fs_info->cleaner_mutex);
5643 trans = btrfs_start_transaction(info->tree_root, 1);
5644 btrfs_commit_transaction(trans, info->tree_root);
5647 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5651 leaf = path->nodes[0];
5652 nritems = btrfs_header_nritems(leaf);
5653 if (path->slots[0] >= nritems) {
5654 ret = btrfs_next_leaf(root, path);
5661 leaf = path->nodes[0];
5662 nritems = btrfs_header_nritems(leaf);
5665 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5667 if (key.objectid >= block_group->key.objectid +
5668 block_group->key.offset)
5671 if (progress && need_resched()) {
5672 btrfs_release_path(root, path);
5679 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5680 key.objectid + key.offset <= cur_byte) {
5686 cur_byte = key.objectid + key.offset;
5687 btrfs_release_path(root, path);
5689 __alloc_chunk_for_shrink(root, block_group, 0);
5690 ret = relocate_one_extent(root, path, &key, block_group,
5696 key.objectid = cur_byte;
5701 btrfs_release_path(root, path);
5704 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5705 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5708 if (total_found > 0) {
5709 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
5710 (unsigned long long)total_found, pass);
5712 if (total_found == skipped && pass > 2) {
5714 reloc_inode = create_reloc_inode(info, block_group);
5720 /* delete reloc_inode */
5723 /* unpin extents in this range */
5724 trans = btrfs_start_transaction(info->tree_root, 1);
5725 btrfs_commit_transaction(trans, info->tree_root);
5727 spin_lock(&block_group->lock);
5728 WARN_ON(block_group->pinned > 0);
5729 WARN_ON(block_group->reserved > 0);
5730 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5731 spin_unlock(&block_group->lock);
5732 put_block_group(block_group);
5735 btrfs_free_path(path);
5739 static int find_first_block_group(struct btrfs_root *root,
5740 struct btrfs_path *path, struct btrfs_key *key)
5743 struct btrfs_key found_key;
5744 struct extent_buffer *leaf;
5747 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5752 slot = path->slots[0];
5753 leaf = path->nodes[0];
5754 if (slot >= btrfs_header_nritems(leaf)) {
5755 ret = btrfs_next_leaf(root, path);
5762 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5764 if (found_key.objectid >= key->objectid &&
5765 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5776 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5778 struct btrfs_block_group_cache *block_group;
5779 struct btrfs_space_info *space_info;
5782 spin_lock(&info->block_group_cache_lock);
5783 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5784 block_group = rb_entry(n, struct btrfs_block_group_cache,
5786 rb_erase(&block_group->cache_node,
5787 &info->block_group_cache_tree);
5788 spin_unlock(&info->block_group_cache_lock);
5790 btrfs_remove_free_space_cache(block_group);
5791 down_write(&block_group->space_info->groups_sem);
5792 list_del(&block_group->list);
5793 up_write(&block_group->space_info->groups_sem);
5795 WARN_ON(atomic_read(&block_group->count) != 1);
5798 spin_lock(&info->block_group_cache_lock);
5800 spin_unlock(&info->block_group_cache_lock);
5802 /* now that all the block groups are freed, go through and
5803 * free all the space_info structs. This is only called during
5804 * the final stages of unmount, and so we know nobody is
5805 * using them. We call synchronize_rcu() once before we start,
5806 * just to be on the safe side.
5810 while(!list_empty(&info->space_info)) {
5811 space_info = list_entry(info->space_info.next,
5812 struct btrfs_space_info,
5815 list_del(&space_info->list);
5821 int btrfs_read_block_groups(struct btrfs_root *root)
5823 struct btrfs_path *path;
5825 struct btrfs_block_group_cache *cache;
5826 struct btrfs_fs_info *info = root->fs_info;
5827 struct btrfs_space_info *space_info;
5828 struct btrfs_key key;
5829 struct btrfs_key found_key;
5830 struct extent_buffer *leaf;
5832 root = info->extent_root;
5835 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5836 path = btrfs_alloc_path();
5841 ret = find_first_block_group(root, path, &key);
5849 leaf = path->nodes[0];
5850 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5851 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5857 atomic_set(&cache->count, 1);
5858 spin_lock_init(&cache->lock);
5859 mutex_init(&cache->alloc_mutex);
5860 mutex_init(&cache->cache_mutex);
5861 INIT_LIST_HEAD(&cache->list);
5862 read_extent_buffer(leaf, &cache->item,
5863 btrfs_item_ptr_offset(leaf, path->slots[0]),
5864 sizeof(cache->item));
5865 memcpy(&cache->key, &found_key, sizeof(found_key));
5867 key.objectid = found_key.objectid + found_key.offset;
5868 btrfs_release_path(root, path);
5869 cache->flags = btrfs_block_group_flags(&cache->item);
5871 ret = update_space_info(info, cache->flags, found_key.offset,
5872 btrfs_block_group_used(&cache->item),
5875 cache->space_info = space_info;
5876 down_write(&space_info->groups_sem);
5877 list_add_tail(&cache->list, &space_info->block_groups);
5878 up_write(&space_info->groups_sem);
5880 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5883 set_avail_alloc_bits(root->fs_info, cache->flags);
5884 if (btrfs_chunk_readonly(root, cache->key.objectid))
5885 set_block_group_readonly(cache);
5889 btrfs_free_path(path);
5893 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5894 struct btrfs_root *root, u64 bytes_used,
5895 u64 type, u64 chunk_objectid, u64 chunk_offset,
5899 struct btrfs_root *extent_root;
5900 struct btrfs_block_group_cache *cache;
5902 extent_root = root->fs_info->extent_root;
5904 root->fs_info->last_trans_log_full_commit = trans->transid;
5906 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5910 cache->key.objectid = chunk_offset;
5911 cache->key.offset = size;
5912 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5913 atomic_set(&cache->count, 1);
5914 spin_lock_init(&cache->lock);
5915 mutex_init(&cache->alloc_mutex);
5916 mutex_init(&cache->cache_mutex);
5917 INIT_LIST_HEAD(&cache->list);
5919 btrfs_set_block_group_used(&cache->item, bytes_used);
5920 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5921 cache->flags = type;
5922 btrfs_set_block_group_flags(&cache->item, type);
5924 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5925 &cache->space_info);
5927 down_write(&cache->space_info->groups_sem);
5928 list_add_tail(&cache->list, &cache->space_info->block_groups);
5929 up_write(&cache->space_info->groups_sem);
5931 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5934 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5935 sizeof(cache->item));
5938 set_avail_alloc_bits(extent_root->fs_info, type);
5943 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5944 struct btrfs_root *root, u64 group_start)
5946 struct btrfs_path *path;
5947 struct btrfs_block_group_cache *block_group;
5948 struct btrfs_key key;
5951 root = root->fs_info->extent_root;
5953 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5954 BUG_ON(!block_group);
5955 BUG_ON(!block_group->ro);
5957 memcpy(&key, &block_group->key, sizeof(key));
5959 path = btrfs_alloc_path();
5962 spin_lock(&root->fs_info->block_group_cache_lock);
5963 rb_erase(&block_group->cache_node,
5964 &root->fs_info->block_group_cache_tree);
5965 spin_unlock(&root->fs_info->block_group_cache_lock);
5966 btrfs_remove_free_space_cache(block_group);
5967 down_write(&block_group->space_info->groups_sem);
5968 list_del(&block_group->list);
5969 up_write(&block_group->space_info->groups_sem);
5971 spin_lock(&block_group->space_info->lock);
5972 block_group->space_info->total_bytes -= block_group->key.offset;
5973 block_group->space_info->bytes_readonly -= block_group->key.offset;
5974 spin_unlock(&block_group->space_info->lock);
5975 block_group->space_info->full = 0;
5977 put_block_group(block_group);
5978 put_block_group(block_group);
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5986 ret = btrfs_del_item(trans, root, path);
5988 btrfs_free_path(path);