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 while (start < end) {
170 ret = find_first_extent_bit(&info->pinned_extents, start,
171 &extent_start, &extent_end,
176 if (extent_start == start) {
177 start = extent_end + 1;
178 } else if (extent_start > start && extent_start < end) {
179 size = extent_start - start;
180 ret = btrfs_add_free_space(block_group, start,
183 start = extent_end + 1;
191 ret = btrfs_add_free_space(block_group, start, size);
198 static int remove_sb_from_cache(struct btrfs_root *root,
199 struct btrfs_block_group_cache *cache)
206 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
207 bytenr = btrfs_sb_offset(i);
208 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
209 cache->key.objectid, bytenr, 0,
210 &logical, &nr, &stripe_len);
213 btrfs_remove_free_space(cache, logical[nr],
221 static int cache_block_group(struct btrfs_root *root,
222 struct btrfs_block_group_cache *block_group)
224 struct btrfs_path *path;
226 struct btrfs_key key;
227 struct extent_buffer *leaf;
234 root = root->fs_info->extent_root;
236 if (block_group->cached)
239 path = btrfs_alloc_path();
245 * we get into deadlocks with paths held by callers of this function.
246 * since the alloc_mutex is protecting things right now, just
247 * skip the locking here
249 path->skip_locking = 1;
250 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
253 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
254 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
259 leaf = path->nodes[0];
260 slot = path->slots[0];
261 if (slot >= btrfs_header_nritems(leaf)) {
262 ret = btrfs_next_leaf(root, path);
270 btrfs_item_key_to_cpu(leaf, &key, slot);
271 if (key.objectid < block_group->key.objectid)
274 if (key.objectid >= block_group->key.objectid +
275 block_group->key.offset)
278 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
279 add_new_free_space(block_group, root->fs_info, last,
282 last = key.objectid + key.offset;
288 add_new_free_space(block_group, root->fs_info, last,
289 block_group->key.objectid +
290 block_group->key.offset);
292 block_group->cached = 1;
293 remove_sb_from_cache(root, block_group);
296 btrfs_free_path(path);
301 * return the block group that starts at or after bytenr
303 static struct btrfs_block_group_cache *
304 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
306 struct btrfs_block_group_cache *cache;
308 cache = block_group_cache_tree_search(info, bytenr, 0);
314 * return the block group that contains teh given bytenr
316 struct btrfs_block_group_cache *btrfs_lookup_block_group(
317 struct btrfs_fs_info *info,
320 struct btrfs_block_group_cache *cache;
322 cache = block_group_cache_tree_search(info, bytenr, 1);
327 static inline void put_block_group(struct btrfs_block_group_cache *cache)
329 if (atomic_dec_and_test(&cache->count))
333 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
336 struct list_head *head = &info->space_info;
337 struct btrfs_space_info *found;
340 list_for_each_entry_rcu(found, head, list) {
341 if (found->flags == flags) {
351 * after adding space to the filesystem, we need to clear the full flags
352 * on all the space infos.
354 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
356 struct list_head *head = &info->space_info;
357 struct btrfs_space_info *found;
360 list_for_each_entry_rcu(found, head, list)
365 static u64 div_factor(u64 num, int factor)
374 u64 btrfs_find_block_group(struct btrfs_root *root,
375 u64 search_start, u64 search_hint, int owner)
377 struct btrfs_block_group_cache *cache;
379 u64 last = max(search_hint, search_start);
386 cache = btrfs_lookup_first_block_group(root->fs_info, last);
390 spin_lock(&cache->lock);
391 last = cache->key.objectid + cache->key.offset;
392 used = btrfs_block_group_used(&cache->item);
394 if ((full_search || !cache->ro) &&
395 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
396 if (used + cache->pinned + cache->reserved <
397 div_factor(cache->key.offset, factor)) {
398 group_start = cache->key.objectid;
399 spin_unlock(&cache->lock);
400 put_block_group(cache);
404 spin_unlock(&cache->lock);
405 put_block_group(cache);
413 if (!full_search && factor < 10) {
423 /* simple helper to search for an existing extent at a given offset */
424 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
427 struct btrfs_key key;
428 struct btrfs_path *path;
430 path = btrfs_alloc_path();
432 key.objectid = start;
434 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
435 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
437 btrfs_free_path(path);
442 * Back reference rules. Back refs have three main goals:
444 * 1) differentiate between all holders of references to an extent so that
445 * when a reference is dropped we can make sure it was a valid reference
446 * before freeing the extent.
448 * 2) Provide enough information to quickly find the holders of an extent
449 * if we notice a given block is corrupted or bad.
451 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
452 * maintenance. This is actually the same as #2, but with a slightly
453 * different use case.
455 * File extents can be referenced by:
457 * - multiple snapshots, subvolumes, or different generations in one subvol
458 * - different files inside a single subvolume
459 * - different offsets inside a file (bookend extents in file.c)
461 * The extent ref structure has fields for:
463 * - Objectid of the subvolume root
464 * - Generation number of the tree holding the reference
465 * - objectid of the file holding the reference
466 * - number of references holding by parent node (alway 1 for tree blocks)
468 * Btree leaf may hold multiple references to a file extent. In most cases,
469 * these references are from same file and the corresponding offsets inside
470 * the file are close together.
472 * When a file extent is allocated the fields are filled in:
473 * (root_key.objectid, trans->transid, inode objectid, 1)
475 * When a leaf is cow'd new references are added for every file extent found
476 * in the leaf. It looks similar to the create case, but trans->transid will
477 * be different when the block is cow'd.
479 * (root_key.objectid, trans->transid, inode objectid,
480 * number of references in the leaf)
482 * When a file extent is removed either during snapshot deletion or
483 * file truncation, we find the corresponding back reference and check
484 * the following fields:
486 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
489 * Btree extents can be referenced by:
491 * - Different subvolumes
492 * - Different generations of the same subvolume
494 * When a tree block is created, back references are inserted:
496 * (root->root_key.objectid, trans->transid, level, 1)
498 * When a tree block is cow'd, new back references are added for all the
499 * blocks it points to. If the tree block isn't in reference counted root,
500 * the old back references are removed. These new back references are of
501 * the form (trans->transid will have increased since creation):
503 * (root->root_key.objectid, trans->transid, level, 1)
505 * When a backref is in deleting, the following fields are checked:
507 * if backref was for a tree root:
508 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
510 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
512 * Back Reference Key composing:
514 * The key objectid corresponds to the first byte in the extent, the key
515 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
516 * byte of parent extent. If a extent is tree root, the key offset is set
517 * to the key objectid.
520 static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
521 struct btrfs_root *root,
522 struct btrfs_path *path,
523 u64 bytenr, u64 parent,
524 u64 ref_root, u64 ref_generation,
525 u64 owner_objectid, int del)
527 struct btrfs_key key;
528 struct btrfs_extent_ref *ref;
529 struct extent_buffer *leaf;
533 key.objectid = bytenr;
534 key.type = BTRFS_EXTENT_REF_KEY;
537 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
545 leaf = path->nodes[0];
546 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
547 ref_objectid = btrfs_ref_objectid(leaf, ref);
548 if (btrfs_ref_root(leaf, ref) != ref_root ||
549 btrfs_ref_generation(leaf, ref) != ref_generation ||
550 (ref_objectid != owner_objectid &&
551 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
561 static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
562 struct btrfs_root *root,
563 struct btrfs_path *path,
564 u64 bytenr, u64 parent,
565 u64 ref_root, u64 ref_generation,
569 struct btrfs_key key;
570 struct extent_buffer *leaf;
571 struct btrfs_extent_ref *ref;
575 key.objectid = bytenr;
576 key.type = BTRFS_EXTENT_REF_KEY;
579 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
581 leaf = path->nodes[0];
582 ref = btrfs_item_ptr(leaf, path->slots[0],
583 struct btrfs_extent_ref);
584 btrfs_set_ref_root(leaf, ref, ref_root);
585 btrfs_set_ref_generation(leaf, ref, ref_generation);
586 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
587 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
588 } else if (ret == -EEXIST) {
591 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
592 leaf = path->nodes[0];
593 ref = btrfs_item_ptr(leaf, path->slots[0],
594 struct btrfs_extent_ref);
595 if (btrfs_ref_root(leaf, ref) != ref_root ||
596 btrfs_ref_generation(leaf, ref) != ref_generation) {
602 num_refs = btrfs_ref_num_refs(leaf, ref);
603 BUG_ON(num_refs == 0);
604 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
606 existing_owner = btrfs_ref_objectid(leaf, ref);
607 if (existing_owner != owner_objectid &&
608 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
609 btrfs_set_ref_objectid(leaf, ref,
610 BTRFS_MULTIPLE_OBJECTIDS);
616 btrfs_unlock_up_safe(path, 1);
617 btrfs_mark_buffer_dirty(path->nodes[0]);
619 btrfs_release_path(root, path);
623 static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
624 struct btrfs_root *root,
625 struct btrfs_path *path,
628 struct extent_buffer *leaf;
629 struct btrfs_extent_ref *ref;
633 leaf = path->nodes[0];
634 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
635 num_refs = btrfs_ref_num_refs(leaf, ref);
636 BUG_ON(num_refs < refs_to_drop);
637 num_refs -= refs_to_drop;
639 ret = btrfs_del_item(trans, root, path);
641 btrfs_set_ref_num_refs(leaf, ref, num_refs);
642 btrfs_mark_buffer_dirty(leaf);
644 btrfs_release_path(root, path);
648 #ifdef BIO_RW_DISCARD
649 static void btrfs_issue_discard(struct block_device *bdev,
652 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
656 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
659 #ifdef BIO_RW_DISCARD
661 u64 map_length = num_bytes;
662 struct btrfs_multi_bio *multi = NULL;
664 /* Tell the block device(s) that the sectors can be discarded */
665 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
666 bytenr, &map_length, &multi, 0);
668 struct btrfs_bio_stripe *stripe = multi->stripes;
671 if (map_length > num_bytes)
672 map_length = num_bytes;
674 for (i = 0; i < multi->num_stripes; i++, stripe++) {
675 btrfs_issue_discard(stripe->dev->bdev,
688 static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
689 struct btrfs_root *root, u64 bytenr,
691 u64 orig_parent, u64 parent,
692 u64 orig_root, u64 ref_root,
693 u64 orig_generation, u64 ref_generation,
697 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
699 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
700 orig_parent, parent, orig_root,
701 ref_root, orig_generation,
702 ref_generation, owner_objectid, pin);
707 int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root, u64 bytenr,
709 u64 num_bytes, u64 orig_parent, u64 parent,
710 u64 ref_root, u64 ref_generation,
714 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
715 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
718 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
719 orig_parent, parent, ref_root,
720 ref_root, ref_generation,
721 ref_generation, owner_objectid);
724 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
725 struct btrfs_root *root, u64 bytenr,
727 u64 orig_parent, u64 parent,
728 u64 orig_root, u64 ref_root,
729 u64 orig_generation, u64 ref_generation,
734 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
735 ref_generation, owner_objectid,
736 BTRFS_ADD_DELAYED_REF, 0);
741 static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
742 struct btrfs_root *root, u64 bytenr,
743 u64 num_bytes, u64 parent, u64 ref_root,
744 u64 ref_generation, u64 owner_objectid,
747 struct btrfs_path *path;
749 struct btrfs_key key;
750 struct extent_buffer *l;
751 struct btrfs_extent_item *item;
754 path = btrfs_alloc_path();
759 path->leave_spinning = 1;
760 key.objectid = bytenr;
761 key.type = BTRFS_EXTENT_ITEM_KEY;
762 key.offset = num_bytes;
764 /* first find the extent item and update its reference count */
765 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
768 btrfs_set_path_blocking(path);
774 btrfs_free_path(path);
779 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
780 if (key.objectid != bytenr) {
781 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
782 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
783 (unsigned long long)bytenr,
784 (unsigned long long)key.objectid);
787 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
789 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
791 refs = btrfs_extent_refs(l, item);
792 btrfs_set_extent_refs(l, item, refs + refs_to_add);
793 btrfs_unlock_up_safe(path, 1);
795 btrfs_mark_buffer_dirty(path->nodes[0]);
797 btrfs_release_path(root->fs_info->extent_root, path);
800 path->leave_spinning = 1;
802 /* now insert the actual backref */
803 ret = insert_extent_backref(trans, root->fs_info->extent_root,
804 path, bytenr, parent,
805 ref_root, ref_generation,
806 owner_objectid, refs_to_add);
808 btrfs_free_path(path);
812 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
813 struct btrfs_root *root,
814 u64 bytenr, u64 num_bytes, u64 parent,
815 u64 ref_root, u64 ref_generation,
819 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
820 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
823 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
824 0, ref_root, 0, ref_generation,
829 static int drop_delayed_ref(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root,
831 struct btrfs_delayed_ref_node *node)
834 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
836 BUG_ON(node->ref_mod == 0);
837 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
838 node->parent, ref->root, ref->generation,
839 ref->owner_objectid, ref->pin, node->ref_mod);
844 /* helper function to actually process a single delayed ref entry */
845 static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
846 struct btrfs_root *root,
847 struct btrfs_delayed_ref_node *node,
851 struct btrfs_delayed_ref *ref;
853 if (node->parent == (u64)-1) {
854 struct btrfs_delayed_ref_head *head;
856 * we've hit the end of the chain and we were supposed
857 * to insert this extent into the tree. But, it got
858 * deleted before we ever needed to insert it, so all
859 * we have to do is clean up the accounting
861 if (insert_reserved) {
862 update_reserved_extents(root, node->bytenr,
865 head = btrfs_delayed_node_to_head(node);
866 mutex_unlock(&head->mutex);
870 ref = btrfs_delayed_node_to_ref(node);
871 if (ref->action == BTRFS_ADD_DELAYED_REF) {
872 if (insert_reserved) {
873 struct btrfs_key ins;
875 ins.objectid = node->bytenr;
876 ins.offset = node->num_bytes;
877 ins.type = BTRFS_EXTENT_ITEM_KEY;
879 /* record the full extent allocation */
880 ret = __btrfs_alloc_reserved_extent(trans, root,
881 node->parent, ref->root,
882 ref->generation, ref->owner_objectid,
883 &ins, node->ref_mod);
884 update_reserved_extents(root, node->bytenr,
887 /* just add one backref */
888 ret = add_extent_ref(trans, root, node->bytenr,
890 node->parent, ref->root, ref->generation,
891 ref->owner_objectid, node->ref_mod);
894 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
895 WARN_ON(insert_reserved);
896 ret = drop_delayed_ref(trans, root, node);
901 static noinline struct btrfs_delayed_ref_node *
902 select_delayed_ref(struct btrfs_delayed_ref_head *head)
904 struct rb_node *node;
905 struct btrfs_delayed_ref_node *ref;
906 int action = BTRFS_ADD_DELAYED_REF;
909 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
910 * this prevents ref count from going down to zero when
911 * there still are pending delayed ref.
913 node = rb_prev(&head->node.rb_node);
917 ref = rb_entry(node, struct btrfs_delayed_ref_node,
919 if (ref->bytenr != head->node.bytenr)
921 if (btrfs_delayed_node_to_ref(ref)->action == action)
923 node = rb_prev(node);
925 if (action == BTRFS_ADD_DELAYED_REF) {
926 action = BTRFS_DROP_DELAYED_REF;
932 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct list_head *cluster)
936 struct btrfs_delayed_ref_root *delayed_refs;
937 struct btrfs_delayed_ref_node *ref;
938 struct btrfs_delayed_ref_head *locked_ref = NULL;
941 int must_insert_reserved = 0;
943 delayed_refs = &trans->transaction->delayed_refs;
946 /* pick a new head ref from the cluster list */
947 if (list_empty(cluster))
950 locked_ref = list_entry(cluster->next,
951 struct btrfs_delayed_ref_head, cluster);
953 /* grab the lock that says we are going to process
954 * all the refs for this head */
955 ret = btrfs_delayed_ref_lock(trans, locked_ref);
958 * we may have dropped the spin lock to get the head
959 * mutex lock, and that might have given someone else
960 * time to free the head. If that's true, it has been
961 * removed from our list and we can move on.
963 if (ret == -EAGAIN) {
971 * record the must insert reserved flag before we
972 * drop the spin lock.
974 must_insert_reserved = locked_ref->must_insert_reserved;
975 locked_ref->must_insert_reserved = 0;
978 * locked_ref is the head node, so we have to go one
979 * node back for any delayed ref updates
981 ref = select_delayed_ref(locked_ref);
983 /* All delayed refs have been processed, Go ahead
984 * and send the head node to run_one_delayed_ref,
985 * so that any accounting fixes can happen
987 ref = &locked_ref->node;
988 list_del_init(&locked_ref->cluster);
993 rb_erase(&ref->rb_node, &delayed_refs->root);
994 delayed_refs->num_entries--;
995 spin_unlock(&delayed_refs->lock);
997 ret = run_one_delayed_ref(trans, root, ref,
998 must_insert_reserved);
1000 btrfs_put_delayed_ref(ref);
1004 spin_lock(&delayed_refs->lock);
1010 * this starts processing the delayed reference count updates and
1011 * extent insertions we have queued up so far. count can be
1012 * 0, which means to process everything in the tree at the start
1013 * of the run (but not newly added entries), or it can be some target
1014 * number you'd like to process.
1016 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root, unsigned long count)
1019 struct rb_node *node;
1020 struct btrfs_delayed_ref_root *delayed_refs;
1021 struct btrfs_delayed_ref_node *ref;
1022 struct list_head cluster;
1024 int run_all = count == (unsigned long)-1;
1027 if (root == root->fs_info->extent_root)
1028 root = root->fs_info->tree_root;
1030 delayed_refs = &trans->transaction->delayed_refs;
1031 INIT_LIST_HEAD(&cluster);
1033 spin_lock(&delayed_refs->lock);
1035 count = delayed_refs->num_entries * 2;
1039 if (!(run_all || run_most) &&
1040 delayed_refs->num_heads_ready < 64)
1044 * go find something we can process in the rbtree. We start at
1045 * the beginning of the tree, and then build a cluster
1046 * of refs to process starting at the first one we are able to
1049 ret = btrfs_find_ref_cluster(trans, &cluster,
1050 delayed_refs->run_delayed_start);
1054 ret = run_clustered_refs(trans, root, &cluster);
1057 count -= min_t(unsigned long, ret, count);
1064 node = rb_first(&delayed_refs->root);
1067 count = (unsigned long)-1;
1070 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1072 if (btrfs_delayed_ref_is_head(ref)) {
1073 struct btrfs_delayed_ref_head *head;
1075 head = btrfs_delayed_node_to_head(ref);
1076 atomic_inc(&ref->refs);
1078 spin_unlock(&delayed_refs->lock);
1079 mutex_lock(&head->mutex);
1080 mutex_unlock(&head->mutex);
1082 btrfs_put_delayed_ref(ref);
1086 node = rb_next(node);
1088 spin_unlock(&delayed_refs->lock);
1089 schedule_timeout(1);
1093 spin_unlock(&delayed_refs->lock);
1097 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1098 struct btrfs_root *root, u64 objectid, u64 bytenr)
1100 struct btrfs_root *extent_root = root->fs_info->extent_root;
1101 struct btrfs_path *path;
1102 struct extent_buffer *leaf;
1103 struct btrfs_extent_ref *ref_item;
1104 struct btrfs_key key;
1105 struct btrfs_key found_key;
1111 key.objectid = bytenr;
1112 key.offset = (u64)-1;
1113 key.type = BTRFS_EXTENT_ITEM_KEY;
1115 path = btrfs_alloc_path();
1116 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1122 if (path->slots[0] == 0)
1126 leaf = path->nodes[0];
1127 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1129 if (found_key.objectid != bytenr ||
1130 found_key.type != BTRFS_EXTENT_ITEM_KEY)
1133 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
1135 leaf = path->nodes[0];
1136 nritems = btrfs_header_nritems(leaf);
1137 if (path->slots[0] >= nritems) {
1138 ret = btrfs_next_leaf(extent_root, path);
1145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1146 if (found_key.objectid != bytenr)
1149 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1154 ref_item = btrfs_item_ptr(leaf, path->slots[0],
1155 struct btrfs_extent_ref);
1156 ref_root = btrfs_ref_root(leaf, ref_item);
1157 if ((ref_root != root->root_key.objectid &&
1158 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1159 objectid != btrfs_ref_objectid(leaf, ref_item)) {
1163 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1172 btrfs_free_path(path);
1176 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1177 struct extent_buffer *buf, u32 nr_extents)
1179 struct btrfs_key key;
1180 struct btrfs_file_extent_item *fi;
1188 if (!root->ref_cows)
1191 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1193 root_gen = root->root_key.offset;
1196 root_gen = trans->transid - 1;
1199 level = btrfs_header_level(buf);
1200 nritems = btrfs_header_nritems(buf);
1203 struct btrfs_leaf_ref *ref;
1204 struct btrfs_extent_info *info;
1206 ref = btrfs_alloc_leaf_ref(root, nr_extents);
1212 ref->root_gen = root_gen;
1213 ref->bytenr = buf->start;
1214 ref->owner = btrfs_header_owner(buf);
1215 ref->generation = btrfs_header_generation(buf);
1216 ref->nritems = nr_extents;
1217 info = ref->extents;
1219 for (i = 0; nr_extents > 0 && i < nritems; i++) {
1221 btrfs_item_key_to_cpu(buf, &key, i);
1222 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1224 fi = btrfs_item_ptr(buf, i,
1225 struct btrfs_file_extent_item);
1226 if (btrfs_file_extent_type(buf, fi) ==
1227 BTRFS_FILE_EXTENT_INLINE)
1229 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1230 if (disk_bytenr == 0)
1233 info->bytenr = disk_bytenr;
1235 btrfs_file_extent_disk_num_bytes(buf, fi);
1236 info->objectid = key.objectid;
1237 info->offset = key.offset;
1241 ret = btrfs_add_leaf_ref(root, ref, shared);
1242 if (ret == -EEXIST && shared) {
1243 struct btrfs_leaf_ref *old;
1244 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1246 btrfs_remove_leaf_ref(root, old);
1247 btrfs_free_leaf_ref(root, old);
1248 ret = btrfs_add_leaf_ref(root, ref, shared);
1251 btrfs_free_leaf_ref(root, ref);
1257 /* when a block goes through cow, we update the reference counts of
1258 * everything that block points to. The internal pointers of the block
1259 * can be in just about any order, and it is likely to have clusters of
1260 * things that are close together and clusters of things that are not.
1262 * To help reduce the seeks that come with updating all of these reference
1263 * counts, sort them by byte number before actual updates are done.
1265 * struct refsort is used to match byte number to slot in the btree block.
1266 * we sort based on the byte number and then use the slot to actually
1269 * struct refsort is smaller than strcut btrfs_item and smaller than
1270 * struct btrfs_key_ptr. Since we're currently limited to the page size
1271 * for a btree block, there's no way for a kmalloc of refsorts for a
1272 * single node to be bigger than a page.
1280 * for passing into sort()
1282 static int refsort_cmp(const void *a_void, const void *b_void)
1284 const struct refsort *a = a_void;
1285 const struct refsort *b = b_void;
1287 if (a->bytenr < b->bytenr)
1289 if (a->bytenr > b->bytenr)
1295 noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root,
1297 struct extent_buffer *orig_buf,
1298 struct extent_buffer *buf, u32 *nr_extents)
1304 u64 orig_generation;
1305 struct refsort *sorted;
1307 u32 nr_file_extents = 0;
1308 struct btrfs_key key;
1309 struct btrfs_file_extent_item *fi;
1316 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
1317 u64, u64, u64, u64, u64, u64, u64, u64, u64);
1319 ref_root = btrfs_header_owner(buf);
1320 ref_generation = btrfs_header_generation(buf);
1321 orig_root = btrfs_header_owner(orig_buf);
1322 orig_generation = btrfs_header_generation(orig_buf);
1324 nritems = btrfs_header_nritems(buf);
1325 level = btrfs_header_level(buf);
1327 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1330 if (root->ref_cows) {
1331 process_func = __btrfs_inc_extent_ref;
1334 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1337 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1339 process_func = __btrfs_update_extent_ref;
1343 * we make two passes through the items. In the first pass we
1344 * only record the byte number and slot. Then we sort based on
1345 * byte number and do the actual work based on the sorted results
1347 for (i = 0; i < nritems; i++) {
1350 btrfs_item_key_to_cpu(buf, &key, i);
1351 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1353 fi = btrfs_item_ptr(buf, i,
1354 struct btrfs_file_extent_item);
1355 if (btrfs_file_extent_type(buf, fi) ==
1356 BTRFS_FILE_EXTENT_INLINE)
1358 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1363 sorted[refi].bytenr = bytenr;
1364 sorted[refi].slot = i;
1367 bytenr = btrfs_node_blockptr(buf, i);
1368 sorted[refi].bytenr = bytenr;
1369 sorted[refi].slot = i;
1374 * if refi == 0, we didn't actually put anything into the sorted
1375 * array and we're done
1380 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1382 for (i = 0; i < refi; i++) {
1384 slot = sorted[i].slot;
1385 bytenr = sorted[i].bytenr;
1388 btrfs_item_key_to_cpu(buf, &key, slot);
1389 fi = btrfs_item_ptr(buf, slot,
1390 struct btrfs_file_extent_item);
1392 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1396 ret = process_func(trans, root, bytenr,
1397 btrfs_file_extent_disk_num_bytes(buf, fi),
1398 orig_buf->start, buf->start,
1399 orig_root, ref_root,
1400 orig_generation, ref_generation,
1409 ret = process_func(trans, root, bytenr, buf->len,
1410 orig_buf->start, buf->start,
1411 orig_root, ref_root,
1412 orig_generation, ref_generation,
1425 *nr_extents = nr_file_extents;
1427 *nr_extents = nritems;
1436 int btrfs_update_ref(struct btrfs_trans_handle *trans,
1437 struct btrfs_root *root, struct extent_buffer *orig_buf,
1438 struct extent_buffer *buf, int start_slot, int nr)
1445 u64 orig_generation;
1446 struct btrfs_key key;
1447 struct btrfs_file_extent_item *fi;
1453 BUG_ON(start_slot < 0);
1454 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1456 ref_root = btrfs_header_owner(buf);
1457 ref_generation = btrfs_header_generation(buf);
1458 orig_root = btrfs_header_owner(orig_buf);
1459 orig_generation = btrfs_header_generation(orig_buf);
1460 level = btrfs_header_level(buf);
1462 if (!root->ref_cows) {
1464 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1467 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1471 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1474 btrfs_item_key_to_cpu(buf, &key, slot);
1475 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1477 fi = btrfs_item_ptr(buf, slot,
1478 struct btrfs_file_extent_item);
1479 if (btrfs_file_extent_type(buf, fi) ==
1480 BTRFS_FILE_EXTENT_INLINE)
1482 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1485 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1486 btrfs_file_extent_disk_num_bytes(buf, fi),
1487 orig_buf->start, buf->start,
1488 orig_root, ref_root, orig_generation,
1489 ref_generation, key.objectid);
1493 bytenr = btrfs_node_blockptr(buf, slot);
1494 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1495 buf->len, orig_buf->start,
1496 buf->start, orig_root, ref_root,
1497 orig_generation, ref_generation,
1509 static int write_one_cache_group(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root,
1511 struct btrfs_path *path,
1512 struct btrfs_block_group_cache *cache)
1515 struct btrfs_root *extent_root = root->fs_info->extent_root;
1517 struct extent_buffer *leaf;
1519 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
1524 leaf = path->nodes[0];
1525 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1526 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1527 btrfs_mark_buffer_dirty(leaf);
1528 btrfs_release_path(extent_root, path);
1536 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root)
1539 struct btrfs_block_group_cache *cache, *entry;
1543 struct btrfs_path *path;
1546 path = btrfs_alloc_path();
1552 spin_lock(&root->fs_info->block_group_cache_lock);
1553 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1554 n; n = rb_next(n)) {
1555 entry = rb_entry(n, struct btrfs_block_group_cache,
1562 spin_unlock(&root->fs_info->block_group_cache_lock);
1568 last += cache->key.offset;
1570 err = write_one_cache_group(trans, root,
1573 * if we fail to write the cache group, we want
1574 * to keep it marked dirty in hopes that a later
1582 btrfs_free_path(path);
1586 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1588 struct btrfs_block_group_cache *block_group;
1591 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1592 if (!block_group || block_group->ro)
1595 put_block_group(block_group);
1599 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1600 u64 total_bytes, u64 bytes_used,
1601 struct btrfs_space_info **space_info)
1603 struct btrfs_space_info *found;
1605 found = __find_space_info(info, flags);
1607 spin_lock(&found->lock);
1608 found->total_bytes += total_bytes;
1609 found->bytes_used += bytes_used;
1611 spin_unlock(&found->lock);
1612 *space_info = found;
1615 found = kzalloc(sizeof(*found), GFP_NOFS);
1619 INIT_LIST_HEAD(&found->block_groups);
1620 init_rwsem(&found->groups_sem);
1621 spin_lock_init(&found->lock);
1622 found->flags = flags;
1623 found->total_bytes = total_bytes;
1624 found->bytes_used = bytes_used;
1625 found->bytes_pinned = 0;
1626 found->bytes_reserved = 0;
1627 found->bytes_readonly = 0;
1628 found->bytes_delalloc = 0;
1630 found->force_alloc = 0;
1631 *space_info = found;
1632 list_add_rcu(&found->list, &info->space_info);
1636 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1638 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
1639 BTRFS_BLOCK_GROUP_RAID1 |
1640 BTRFS_BLOCK_GROUP_RAID10 |
1641 BTRFS_BLOCK_GROUP_DUP);
1643 if (flags & BTRFS_BLOCK_GROUP_DATA)
1644 fs_info->avail_data_alloc_bits |= extra_flags;
1645 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1646 fs_info->avail_metadata_alloc_bits |= extra_flags;
1647 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1648 fs_info->avail_system_alloc_bits |= extra_flags;
1652 static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1654 spin_lock(&cache->space_info->lock);
1655 spin_lock(&cache->lock);
1657 cache->space_info->bytes_readonly += cache->key.offset -
1658 btrfs_block_group_used(&cache->item);
1661 spin_unlock(&cache->lock);
1662 spin_unlock(&cache->space_info->lock);
1665 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
1667 u64 num_devices = root->fs_info->fs_devices->rw_devices;
1669 if (num_devices == 1)
1670 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1671 if (num_devices < 4)
1672 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1674 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1675 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1676 BTRFS_BLOCK_GROUP_RAID10))) {
1677 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1680 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1681 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
1682 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1685 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1686 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1687 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1688 (flags & BTRFS_BLOCK_GROUP_DUP)))
1689 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1693 static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1695 struct btrfs_fs_info *info = root->fs_info;
1699 alloc_profile = info->avail_data_alloc_bits &
1700 info->data_alloc_profile;
1701 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1702 } else if (root == root->fs_info->chunk_root) {
1703 alloc_profile = info->avail_system_alloc_bits &
1704 info->system_alloc_profile;
1705 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1707 alloc_profile = info->avail_metadata_alloc_bits &
1708 info->metadata_alloc_profile;
1709 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1712 return btrfs_reduce_alloc_profile(root, data);
1715 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1719 alloc_target = btrfs_get_alloc_profile(root, 1);
1720 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1725 * for now this just makes sure we have at least 5% of our metadata space free
1728 int btrfs_check_metadata_free_space(struct btrfs_root *root)
1730 struct btrfs_fs_info *info = root->fs_info;
1731 struct btrfs_space_info *meta_sinfo;
1732 u64 alloc_target, thresh;
1733 int committed = 0, ret;
1735 /* get the space info for where the metadata will live */
1736 alloc_target = btrfs_get_alloc_profile(root, 0);
1737 meta_sinfo = __find_space_info(info, alloc_target);
1740 spin_lock(&meta_sinfo->lock);
1741 if (!meta_sinfo->full)
1742 thresh = meta_sinfo->total_bytes * 80;
1744 thresh = meta_sinfo->total_bytes * 95;
1746 do_div(thresh, 100);
1748 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1749 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
1750 struct btrfs_trans_handle *trans;
1751 if (!meta_sinfo->full) {
1752 meta_sinfo->force_alloc = 1;
1753 spin_unlock(&meta_sinfo->lock);
1755 trans = btrfs_start_transaction(root, 1);
1759 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1760 2 * 1024 * 1024, alloc_target, 0);
1761 btrfs_end_transaction(trans, root);
1764 spin_unlock(&meta_sinfo->lock);
1768 trans = btrfs_join_transaction(root, 1);
1771 ret = btrfs_commit_transaction(trans, root);
1778 spin_unlock(&meta_sinfo->lock);
1784 * This will check the space that the inode allocates from to make sure we have
1785 * enough space for bytes.
1787 int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1790 struct btrfs_space_info *data_sinfo;
1791 int ret = 0, committed = 0;
1793 /* make sure bytes are sectorsize aligned */
1794 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1796 data_sinfo = BTRFS_I(inode)->space_info;
1798 /* make sure we have enough space to handle the data first */
1799 spin_lock(&data_sinfo->lock);
1800 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1801 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1802 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1803 data_sinfo->bytes_may_use < bytes) {
1804 struct btrfs_trans_handle *trans;
1807 * if we don't have enough free bytes in this space then we need
1808 * to alloc a new chunk.
1810 if (!data_sinfo->full) {
1813 data_sinfo->force_alloc = 1;
1814 spin_unlock(&data_sinfo->lock);
1816 alloc_target = btrfs_get_alloc_profile(root, 1);
1817 trans = btrfs_start_transaction(root, 1);
1821 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1822 bytes + 2 * 1024 * 1024,
1824 btrfs_end_transaction(trans, root);
1829 spin_unlock(&data_sinfo->lock);
1831 /* commit the current transaction and try again */
1834 trans = btrfs_join_transaction(root, 1);
1837 ret = btrfs_commit_transaction(trans, root);
1843 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1844 ", %llu bytes_used, %llu bytes_reserved, "
1845 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1846 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1847 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1848 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1849 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1852 data_sinfo->bytes_may_use += bytes;
1853 BTRFS_I(inode)->reserved_bytes += bytes;
1854 spin_unlock(&data_sinfo->lock);
1856 return btrfs_check_metadata_free_space(root);
1860 * if there was an error for whatever reason after calling
1861 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1863 void btrfs_free_reserved_data_space(struct btrfs_root *root,
1864 struct inode *inode, u64 bytes)
1866 struct btrfs_space_info *data_sinfo;
1868 /* make sure bytes are sectorsize aligned */
1869 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1871 data_sinfo = BTRFS_I(inode)->space_info;
1872 spin_lock(&data_sinfo->lock);
1873 data_sinfo->bytes_may_use -= bytes;
1874 BTRFS_I(inode)->reserved_bytes -= bytes;
1875 spin_unlock(&data_sinfo->lock);
1878 /* called when we are adding a delalloc extent to the inode's io_tree */
1879 void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1882 struct btrfs_space_info *data_sinfo;
1884 /* get the space info for where this inode will be storing its data */
1885 data_sinfo = BTRFS_I(inode)->space_info;
1887 /* make sure we have enough space to handle the data first */
1888 spin_lock(&data_sinfo->lock);
1889 data_sinfo->bytes_delalloc += bytes;
1892 * we are adding a delalloc extent without calling
1893 * btrfs_check_data_free_space first. This happens on a weird
1894 * writepage condition, but shouldn't hurt our accounting
1896 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1897 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1898 BTRFS_I(inode)->reserved_bytes = 0;
1900 data_sinfo->bytes_may_use -= bytes;
1901 BTRFS_I(inode)->reserved_bytes -= bytes;
1904 spin_unlock(&data_sinfo->lock);
1907 /* called when we are clearing an delalloc extent from the inode's io_tree */
1908 void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1911 struct btrfs_space_info *info;
1913 info = BTRFS_I(inode)->space_info;
1915 spin_lock(&info->lock);
1916 info->bytes_delalloc -= bytes;
1917 spin_unlock(&info->lock);
1920 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1921 struct btrfs_root *extent_root, u64 alloc_bytes,
1922 u64 flags, int force)
1924 struct btrfs_space_info *space_info;
1928 mutex_lock(&extent_root->fs_info->chunk_mutex);
1930 flags = btrfs_reduce_alloc_profile(extent_root, flags);
1932 space_info = __find_space_info(extent_root->fs_info, flags);
1934 ret = update_space_info(extent_root->fs_info, flags,
1938 BUG_ON(!space_info);
1940 spin_lock(&space_info->lock);
1941 if (space_info->force_alloc) {
1943 space_info->force_alloc = 0;
1945 if (space_info->full) {
1946 spin_unlock(&space_info->lock);
1950 thresh = space_info->total_bytes - space_info->bytes_readonly;
1951 thresh = div_factor(thresh, 6);
1953 (space_info->bytes_used + space_info->bytes_pinned +
1954 space_info->bytes_reserved + alloc_bytes) < thresh) {
1955 spin_unlock(&space_info->lock);
1958 spin_unlock(&space_info->lock);
1960 ret = btrfs_alloc_chunk(trans, extent_root, flags);
1962 space_info->full = 1;
1964 mutex_unlock(&extent_root->fs_info->chunk_mutex);
1968 static int update_block_group(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
1970 u64 bytenr, u64 num_bytes, int alloc,
1973 struct btrfs_block_group_cache *cache;
1974 struct btrfs_fs_info *info = root->fs_info;
1975 u64 total = num_bytes;
1980 cache = btrfs_lookup_block_group(info, bytenr);
1983 byte_in_group = bytenr - cache->key.objectid;
1984 WARN_ON(byte_in_group > cache->key.offset);
1986 spin_lock(&cache->space_info->lock);
1987 spin_lock(&cache->lock);
1989 old_val = btrfs_block_group_used(&cache->item);
1990 num_bytes = min(total, cache->key.offset - byte_in_group);
1992 old_val += num_bytes;
1993 cache->space_info->bytes_used += num_bytes;
1995 cache->space_info->bytes_readonly -= num_bytes;
1996 btrfs_set_block_group_used(&cache->item, old_val);
1997 spin_unlock(&cache->lock);
1998 spin_unlock(&cache->space_info->lock);
2000 old_val -= num_bytes;
2001 cache->space_info->bytes_used -= num_bytes;
2003 cache->space_info->bytes_readonly += num_bytes;
2004 btrfs_set_block_group_used(&cache->item, old_val);
2005 spin_unlock(&cache->lock);
2006 spin_unlock(&cache->space_info->lock);
2010 ret = btrfs_discard_extent(root, bytenr,
2014 ret = btrfs_add_free_space(cache, bytenr,
2019 put_block_group(cache);
2021 bytenr += num_bytes;
2026 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2028 struct btrfs_block_group_cache *cache;
2031 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2035 bytenr = cache->key.objectid;
2036 put_block_group(cache);
2041 int btrfs_update_pinned_extents(struct btrfs_root *root,
2042 u64 bytenr, u64 num, int pin)
2045 struct btrfs_block_group_cache *cache;
2046 struct btrfs_fs_info *fs_info = root->fs_info;
2049 set_extent_dirty(&fs_info->pinned_extents,
2050 bytenr, bytenr + num - 1, GFP_NOFS);
2052 clear_extent_dirty(&fs_info->pinned_extents,
2053 bytenr, bytenr + num - 1, GFP_NOFS);
2057 cache = btrfs_lookup_block_group(fs_info, bytenr);
2059 len = min(num, cache->key.offset -
2060 (bytenr - cache->key.objectid));
2062 spin_lock(&cache->space_info->lock);
2063 spin_lock(&cache->lock);
2064 cache->pinned += len;
2065 cache->space_info->bytes_pinned += len;
2066 spin_unlock(&cache->lock);
2067 spin_unlock(&cache->space_info->lock);
2068 fs_info->total_pinned += len;
2070 spin_lock(&cache->space_info->lock);
2071 spin_lock(&cache->lock);
2072 cache->pinned -= len;
2073 cache->space_info->bytes_pinned -= len;
2074 spin_unlock(&cache->lock);
2075 spin_unlock(&cache->space_info->lock);
2076 fs_info->total_pinned -= len;
2078 btrfs_add_free_space(cache, bytenr, len);
2080 put_block_group(cache);
2087 static int update_reserved_extents(struct btrfs_root *root,
2088 u64 bytenr, u64 num, int reserve)
2091 struct btrfs_block_group_cache *cache;
2092 struct btrfs_fs_info *fs_info = root->fs_info;
2095 cache = btrfs_lookup_block_group(fs_info, bytenr);
2097 len = min(num, cache->key.offset -
2098 (bytenr - cache->key.objectid));
2100 spin_lock(&cache->space_info->lock);
2101 spin_lock(&cache->lock);
2103 cache->reserved += len;
2104 cache->space_info->bytes_reserved += len;
2106 cache->reserved -= len;
2107 cache->space_info->bytes_reserved -= len;
2109 spin_unlock(&cache->lock);
2110 spin_unlock(&cache->space_info->lock);
2111 put_block_group(cache);
2118 int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
2123 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
2127 ret = find_first_extent_bit(pinned_extents, last,
2128 &start, &end, EXTENT_DIRTY);
2131 set_extent_dirty(copy, start, end, GFP_NOFS);
2137 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root,
2139 struct extent_io_tree *unpin)
2146 ret = find_first_extent_bit(unpin, 0, &start, &end,
2151 ret = btrfs_discard_extent(root, start, end + 1 - start);
2153 /* unlocks the pinned mutex */
2154 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
2155 clear_extent_dirty(unpin, start, end, GFP_NOFS);
2162 static int pin_down_bytes(struct btrfs_trans_handle *trans,
2163 struct btrfs_root *root,
2164 struct btrfs_path *path,
2165 u64 bytenr, u64 num_bytes, int is_data,
2166 struct extent_buffer **must_clean)
2169 struct extent_buffer *buf;
2174 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2178 /* we can reuse a block if it hasn't been written
2179 * and it is from this transaction. We can't
2180 * reuse anything from the tree log root because
2181 * it has tiny sub-transactions.
2183 if (btrfs_buffer_uptodate(buf, 0) &&
2184 btrfs_try_tree_lock(buf)) {
2185 u64 header_owner = btrfs_header_owner(buf);
2186 u64 header_transid = btrfs_header_generation(buf);
2187 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
2188 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
2189 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
2190 header_transid == trans->transid &&
2191 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2195 btrfs_tree_unlock(buf);
2197 free_extent_buffer(buf);
2199 btrfs_set_path_blocking(path);
2200 /* unlocks the pinned mutex */
2201 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2208 * remove an extent from the root, returns 0 on success
2210 static int __free_extent(struct btrfs_trans_handle *trans,
2211 struct btrfs_root *root,
2212 u64 bytenr, u64 num_bytes, u64 parent,
2213 u64 root_objectid, u64 ref_generation,
2214 u64 owner_objectid, int pin, int mark_free,
2217 struct btrfs_path *path;
2218 struct btrfs_key key;
2219 struct btrfs_fs_info *info = root->fs_info;
2220 struct btrfs_root *extent_root = info->extent_root;
2221 struct extent_buffer *leaf;
2223 int extent_slot = 0;
2224 int found_extent = 0;
2226 struct btrfs_extent_item *ei;
2229 key.objectid = bytenr;
2230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
2231 key.offset = num_bytes;
2232 path = btrfs_alloc_path();
2237 path->leave_spinning = 1;
2238 ret = lookup_extent_backref(trans, extent_root, path,
2239 bytenr, parent, root_objectid,
2240 ref_generation, owner_objectid, 1);
2242 struct btrfs_key found_key;
2243 extent_slot = path->slots[0];
2244 while (extent_slot > 0) {
2246 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2248 if (found_key.objectid != bytenr)
2250 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2251 found_key.offset == num_bytes) {
2255 if (path->slots[0] - extent_slot > 5)
2258 if (!found_extent) {
2259 ret = remove_extent_backref(trans, extent_root, path,
2262 btrfs_release_path(extent_root, path);
2263 path->leave_spinning = 1;
2264 ret = btrfs_search_slot(trans, extent_root,
2267 printk(KERN_ERR "umm, got %d back from search"
2268 ", was looking for %llu\n", ret,
2269 (unsigned long long)bytenr);
2270 btrfs_print_leaf(extent_root, path->nodes[0]);
2273 extent_slot = path->slots[0];
2276 btrfs_print_leaf(extent_root, path->nodes[0]);
2278 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2279 "parent %llu root %llu gen %llu owner %llu\n",
2280 (unsigned long long)bytenr,
2281 (unsigned long long)parent,
2282 (unsigned long long)root_objectid,
2283 (unsigned long long)ref_generation,
2284 (unsigned long long)owner_objectid);
2287 leaf = path->nodes[0];
2288 ei = btrfs_item_ptr(leaf, extent_slot,
2289 struct btrfs_extent_item);
2290 refs = btrfs_extent_refs(leaf, ei);
2293 * we're not allowed to delete the extent item if there
2294 * are other delayed ref updates pending
2297 BUG_ON(refs < refs_to_drop);
2298 refs -= refs_to_drop;
2299 btrfs_set_extent_refs(leaf, ei, refs);
2300 btrfs_mark_buffer_dirty(leaf);
2302 if (refs == 0 && found_extent &&
2303 path->slots[0] == extent_slot + 1) {
2304 struct btrfs_extent_ref *ref;
2305 ref = btrfs_item_ptr(leaf, path->slots[0],
2306 struct btrfs_extent_ref);
2307 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
2308 /* if the back ref and the extent are next to each other
2309 * they get deleted below in one shot
2311 path->slots[0] = extent_slot;
2313 } else if (found_extent) {
2314 /* otherwise delete the extent back ref */
2315 ret = remove_extent_backref(trans, extent_root, path,
2318 /* if refs are 0, we need to setup the path for deletion */
2320 btrfs_release_path(extent_root, path);
2321 path->leave_spinning = 1;
2322 ret = btrfs_search_slot(trans, extent_root, &key, path,
2331 struct extent_buffer *must_clean = NULL;
2334 ret = pin_down_bytes(trans, root, path,
2336 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2343 /* block accounting for super block */
2344 spin_lock(&info->delalloc_lock);
2345 super_used = btrfs_super_bytes_used(&info->super_copy);
2346 btrfs_set_super_bytes_used(&info->super_copy,
2347 super_used - num_bytes);
2349 /* block accounting for root item */
2350 root_used = btrfs_root_used(&root->root_item);
2351 btrfs_set_root_used(&root->root_item,
2352 root_used - num_bytes);
2353 spin_unlock(&info->delalloc_lock);
2356 * it is going to be very rare for someone to be waiting
2357 * on the block we're freeing. del_items might need to
2358 * schedule, so rather than get fancy, just force it
2362 btrfs_set_lock_blocking(must_clean);
2364 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2367 btrfs_release_path(extent_root, path);
2370 clean_tree_block(NULL, root, must_clean);
2371 btrfs_tree_unlock(must_clean);
2372 free_extent_buffer(must_clean);
2375 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2376 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2379 invalidate_mapping_pages(info->btree_inode->i_mapping,
2380 bytenr >> PAGE_CACHE_SHIFT,
2381 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
2384 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2388 btrfs_free_path(path);
2393 * remove an extent from the root, returns 0 on success
2395 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
2396 struct btrfs_root *root,
2397 u64 bytenr, u64 num_bytes, u64 parent,
2398 u64 root_objectid, u64 ref_generation,
2399 u64 owner_objectid, int pin,
2402 WARN_ON(num_bytes < root->sectorsize);
2405 * if metadata always pin
2406 * if data pin when any transaction has committed this
2408 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2409 ref_generation != trans->transid)
2412 if (ref_generation != trans->transid)
2415 return __free_extent(trans, root, bytenr, num_bytes, parent,
2416 root_objectid, ref_generation,
2417 owner_objectid, pin, pin == 0, refs_to_drop);
2421 * when we free an extent, it is possible (and likely) that we free the last
2422 * delayed ref for that extent as well. This searches the delayed ref tree for
2423 * a given extent, and if there are no other delayed refs to be processed, it
2424 * removes it from the tree.
2426 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2427 struct btrfs_root *root, u64 bytenr)
2429 struct btrfs_delayed_ref_head *head;
2430 struct btrfs_delayed_ref_root *delayed_refs;
2431 struct btrfs_delayed_ref_node *ref;
2432 struct rb_node *node;
2435 delayed_refs = &trans->transaction->delayed_refs;
2436 spin_lock(&delayed_refs->lock);
2437 head = btrfs_find_delayed_ref_head(trans, bytenr);
2441 node = rb_prev(&head->node.rb_node);
2445 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2447 /* there are still entries for this ref, we can't drop it */
2448 if (ref->bytenr == bytenr)
2452 * waiting for the lock here would deadlock. If someone else has it
2453 * locked they are already in the process of dropping it anyway
2455 if (!mutex_trylock(&head->mutex))
2459 * at this point we have a head with no other entries. Go
2460 * ahead and process it.
2462 head->node.in_tree = 0;
2463 rb_erase(&head->node.rb_node, &delayed_refs->root);
2465 delayed_refs->num_entries--;
2468 * we don't take a ref on the node because we're removing it from the
2469 * tree, so we just steal the ref the tree was holding.
2471 delayed_refs->num_heads--;
2472 if (list_empty(&head->cluster))
2473 delayed_refs->num_heads_ready--;
2475 list_del_init(&head->cluster);
2476 spin_unlock(&delayed_refs->lock);
2478 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2479 &head->node, head->must_insert_reserved);
2481 btrfs_put_delayed_ref(&head->node);
2484 spin_unlock(&delayed_refs->lock);
2488 int btrfs_free_extent(struct btrfs_trans_handle *trans,
2489 struct btrfs_root *root,
2490 u64 bytenr, u64 num_bytes, u64 parent,
2491 u64 root_objectid, u64 ref_generation,
2492 u64 owner_objectid, int pin)
2497 * tree log blocks never actually go into the extent allocation
2498 * tree, just update pinning info and exit early.
2500 * data extents referenced by the tree log do need to have
2501 * their reference counts bumped.
2503 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2504 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2505 /* unlocks the pinned mutex */
2506 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2507 update_reserved_extents(root, bytenr, num_bytes, 0);
2510 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2511 root_objectid, ref_generation,
2513 BTRFS_DROP_DELAYED_REF, 1);
2515 ret = check_ref_cleanup(trans, root, bytenr);
2521 static u64 stripe_align(struct btrfs_root *root, u64 val)
2523 u64 mask = ((u64)root->stripesize - 1);
2524 u64 ret = (val + mask) & ~mask;
2529 * walks the btree of allocated extents and find a hole of a given size.
2530 * The key ins is changed to record the hole:
2531 * ins->objectid == block start
2532 * ins->flags = BTRFS_EXTENT_ITEM_KEY
2533 * ins->offset == number of blocks
2534 * Any available blocks before search_start are skipped.
2536 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
2537 struct btrfs_root *orig_root,
2538 u64 num_bytes, u64 empty_size,
2539 u64 search_start, u64 search_end,
2540 u64 hint_byte, struct btrfs_key *ins,
2541 u64 exclude_start, u64 exclude_nr,
2545 struct btrfs_root *root = orig_root->fs_info->extent_root;
2546 u64 *last_ptr = NULL;
2547 struct btrfs_block_group_cache *block_group = NULL;
2548 int empty_cluster = 2 * 1024 * 1024;
2549 int allowed_chunk_alloc = 0;
2551 struct btrfs_space_info *space_info;
2553 WARN_ON(num_bytes < root->sectorsize);
2554 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
2558 space_info = __find_space_info(root->fs_info, data);
2560 if (orig_root->ref_cows || empty_size)
2561 allowed_chunk_alloc = 1;
2563 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2564 last_ptr = &root->fs_info->last_alloc;
2565 if (!btrfs_test_opt(root, SSD))
2566 empty_cluster = 64 * 1024;
2569 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
2570 last_ptr = &root->fs_info->last_data_alloc;
2574 hint_byte = *last_ptr;
2576 empty_size += empty_cluster;
2580 search_start = max(search_start, first_logical_byte(root, 0));
2581 search_start = max(search_start, hint_byte);
2583 if (search_start == hint_byte) {
2585 block_group = btrfs_lookup_block_group(root->fs_info,
2587 if (block_group && block_group_bits(block_group, data)) {
2588 down_read(&space_info->groups_sem);
2589 goto have_block_group;
2590 } else if (block_group) {
2591 put_block_group(block_group);
2594 empty_size += empty_cluster;
2599 down_read(&space_info->groups_sem);
2600 list_for_each_entry(block_group, &space_info->block_groups, list) {
2603 atomic_inc(&block_group->count);
2604 search_start = block_group->key.objectid;
2607 if (unlikely(!block_group->cached)) {
2608 mutex_lock(&block_group->cache_mutex);
2609 ret = cache_block_group(root, block_group);
2610 mutex_unlock(&block_group->cache_mutex);
2612 put_block_group(block_group);
2617 if (unlikely(block_group->ro))
2620 offset = btrfs_find_space_for_alloc(block_group, search_start,
2621 num_bytes, empty_size);
2625 search_start = stripe_align(root, offset);
2627 /* move on to the next group */
2628 if (search_start + num_bytes >= search_end) {
2629 btrfs_add_free_space(block_group, offset, num_bytes);
2633 /* move on to the next group */
2634 if (search_start + num_bytes >
2635 block_group->key.objectid + block_group->key.offset) {
2636 btrfs_add_free_space(block_group, offset, num_bytes);
2640 if (using_hint && search_start > hint_byte) {
2641 btrfs_add_free_space(block_group, offset, num_bytes);
2645 if (exclude_nr > 0 &&
2646 (search_start + num_bytes > exclude_start &&
2647 search_start < exclude_start + exclude_nr)) {
2648 search_start = exclude_start + exclude_nr;
2650 btrfs_add_free_space(block_group, offset, num_bytes);
2652 * if search_start is still in this block group
2653 * then we just re-search this block group
2655 if (search_start >= block_group->key.objectid &&
2656 search_start < (block_group->key.objectid +
2657 block_group->key.offset))
2658 goto have_block_group;
2662 ins->objectid = search_start;
2663 ins->offset = num_bytes;
2665 if (offset < search_start)
2666 btrfs_add_free_space(block_group, offset,
2667 search_start - offset);
2668 BUG_ON(offset > search_start);
2670 /* we are all good, lets return */
2673 put_block_group(block_group);
2675 empty_size += empty_cluster;
2677 up_read(&space_info->groups_sem);
2681 up_read(&space_info->groups_sem);
2683 if (!ins->objectid && (empty_size || allowed_chunk_alloc)) {
2684 int try_again = empty_size;
2688 if (allowed_chunk_alloc) {
2689 ret = do_chunk_alloc(trans, root, num_bytes +
2690 2 * 1024 * 1024, data, 1);
2693 allowed_chunk_alloc = 0;
2695 space_info->force_alloc = 1;
2701 } else if (!ins->objectid) {
2705 /* we found what we needed */
2706 if (ins->objectid) {
2707 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2708 trans->block_group = block_group->key.objectid;
2711 *last_ptr = ins->objectid + ins->offset;
2712 put_block_group(block_group);
2719 static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2721 struct btrfs_block_group_cache *cache;
2723 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2724 (unsigned long long)(info->total_bytes - info->bytes_used -
2725 info->bytes_pinned - info->bytes_reserved),
2726 (info->full) ? "" : "not ");
2727 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2728 " may_use=%llu, used=%llu\n", info->total_bytes,
2729 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2732 down_read(&info->groups_sem);
2733 list_for_each_entry(cache, &info->block_groups, list) {
2734 spin_lock(&cache->lock);
2735 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2736 "%llu pinned %llu reserved\n",
2737 (unsigned long long)cache->key.objectid,
2738 (unsigned long long)cache->key.offset,
2739 (unsigned long long)btrfs_block_group_used(&cache->item),
2740 (unsigned long long)cache->pinned,
2741 (unsigned long long)cache->reserved);
2742 btrfs_dump_free_space(cache, bytes);
2743 spin_unlock(&cache->lock);
2745 up_read(&info->groups_sem);
2748 static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2749 struct btrfs_root *root,
2750 u64 num_bytes, u64 min_alloc_size,
2751 u64 empty_size, u64 hint_byte,
2752 u64 search_end, struct btrfs_key *ins,
2756 u64 search_start = 0;
2757 struct btrfs_fs_info *info = root->fs_info;
2759 data = btrfs_get_alloc_profile(root, data);
2762 * the only place that sets empty_size is btrfs_realloc_node, which
2763 * is not called recursively on allocations
2765 if (empty_size || root->ref_cows) {
2766 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
2767 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2769 BTRFS_BLOCK_GROUP_METADATA |
2770 (info->metadata_alloc_profile &
2771 info->avail_metadata_alloc_bits), 0);
2773 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2774 num_bytes + 2 * 1024 * 1024, data, 0);
2777 WARN_ON(num_bytes < root->sectorsize);
2778 ret = find_free_extent(trans, root, num_bytes, empty_size,
2779 search_start, search_end, hint_byte, ins,
2780 trans->alloc_exclude_start,
2781 trans->alloc_exclude_nr, data);
2783 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2784 num_bytes = num_bytes >> 1;
2785 num_bytes = num_bytes & ~(root->sectorsize - 1);
2786 num_bytes = max(num_bytes, min_alloc_size);
2787 do_chunk_alloc(trans, root->fs_info->extent_root,
2788 num_bytes, data, 1);
2792 struct btrfs_space_info *sinfo;
2794 sinfo = __find_space_info(root->fs_info, data);
2795 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2796 "wanted %llu\n", (unsigned long long)data,
2797 (unsigned long long)num_bytes);
2798 dump_space_info(sinfo, num_bytes);
2805 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2807 struct btrfs_block_group_cache *cache;
2810 cache = btrfs_lookup_block_group(root->fs_info, start);
2812 printk(KERN_ERR "Unable to find block group for %llu\n",
2813 (unsigned long long)start);
2817 ret = btrfs_discard_extent(root, start, len);
2819 btrfs_add_free_space(cache, start, len);
2820 put_block_group(cache);
2821 update_reserved_extents(root, start, len, 0);
2826 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root,
2828 u64 num_bytes, u64 min_alloc_size,
2829 u64 empty_size, u64 hint_byte,
2830 u64 search_end, struct btrfs_key *ins,
2834 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2835 empty_size, hint_byte, search_end, ins,
2837 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2841 static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2842 struct btrfs_root *root, u64 parent,
2843 u64 root_objectid, u64 ref_generation,
2844 u64 owner, struct btrfs_key *ins,
2850 u64 num_bytes = ins->offset;
2852 struct btrfs_fs_info *info = root->fs_info;
2853 struct btrfs_root *extent_root = info->extent_root;
2854 struct btrfs_extent_item *extent_item;
2855 struct btrfs_extent_ref *ref;
2856 struct btrfs_path *path;
2857 struct btrfs_key keys[2];
2860 parent = ins->objectid;
2862 /* block accounting for super block */
2863 spin_lock(&info->delalloc_lock);
2864 super_used = btrfs_super_bytes_used(&info->super_copy);
2865 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
2867 /* block accounting for root item */
2868 root_used = btrfs_root_used(&root->root_item);
2869 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
2870 spin_unlock(&info->delalloc_lock);
2872 memcpy(&keys[0], ins, sizeof(*ins));
2873 keys[1].objectid = ins->objectid;
2874 keys[1].type = BTRFS_EXTENT_REF_KEY;
2875 keys[1].offset = parent;
2876 sizes[0] = sizeof(*extent_item);
2877 sizes[1] = sizeof(*ref);
2879 path = btrfs_alloc_path();
2882 path->leave_spinning = 1;
2883 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2887 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2888 struct btrfs_extent_item);
2889 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
2890 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2891 struct btrfs_extent_ref);
2893 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2894 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2895 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2896 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
2898 btrfs_mark_buffer_dirty(path->nodes[0]);
2900 trans->alloc_exclude_start = 0;
2901 trans->alloc_exclude_nr = 0;
2902 btrfs_free_path(path);
2907 ret = update_block_group(trans, root, ins->objectid,
2910 printk(KERN_ERR "btrfs update block group failed for %llu "
2911 "%llu\n", (unsigned long long)ins->objectid,
2912 (unsigned long long)ins->offset);
2919 int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2920 struct btrfs_root *root, u64 parent,
2921 u64 root_objectid, u64 ref_generation,
2922 u64 owner, struct btrfs_key *ins)
2926 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2929 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2930 ins->offset, parent, root_objectid,
2931 ref_generation, owner,
2932 BTRFS_ADD_DELAYED_EXTENT, 0);
2938 * this is used by the tree logging recovery code. It records that
2939 * an extent has been allocated and makes sure to clear the free
2940 * space cache bits as well
2942 int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2943 struct btrfs_root *root, u64 parent,
2944 u64 root_objectid, u64 ref_generation,
2945 u64 owner, struct btrfs_key *ins)
2948 struct btrfs_block_group_cache *block_group;
2950 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2951 mutex_lock(&block_group->cache_mutex);
2952 cache_block_group(root, block_group);
2953 mutex_unlock(&block_group->cache_mutex);
2955 ret = btrfs_remove_free_space(block_group, ins->objectid,
2958 put_block_group(block_group);
2959 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2960 ref_generation, owner, ins, 1);
2965 * finds a free extent and does all the dirty work required for allocation
2966 * returns the key for the extent through ins, and a tree buffer for
2967 * the first block of the extent through buf.
2969 * returns 0 if everything worked, non-zero otherwise.
2971 int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *root,
2973 u64 num_bytes, u64 parent, u64 min_alloc_size,
2974 u64 root_objectid, u64 ref_generation,
2975 u64 owner_objectid, u64 empty_size, u64 hint_byte,
2976 u64 search_end, struct btrfs_key *ins, u64 data)
2979 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2980 min_alloc_size, empty_size, hint_byte,
2981 search_end, ins, data);
2983 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2984 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2985 ins->offset, parent, root_objectid,
2986 ref_generation, owner_objectid,
2987 BTRFS_ADD_DELAYED_EXTENT, 0);
2990 update_reserved_extents(root, ins->objectid, ins->offset, 1);
2994 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2995 struct btrfs_root *root,
2996 u64 bytenr, u32 blocksize,
2999 struct extent_buffer *buf;
3001 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3003 return ERR_PTR(-ENOMEM);
3004 btrfs_set_header_generation(buf, trans->transid);
3005 btrfs_set_buffer_lockdep_class(buf, level);
3006 btrfs_tree_lock(buf);
3007 clean_tree_block(trans, root, buf);
3009 btrfs_set_lock_blocking(buf);
3010 btrfs_set_buffer_uptodate(buf);
3012 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3013 set_extent_dirty(&root->dirty_log_pages, buf->start,
3014 buf->start + buf->len - 1, GFP_NOFS);
3016 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3017 buf->start + buf->len - 1, GFP_NOFS);
3019 trans->blocks_used++;
3020 /* this returns a buffer locked for blocking */
3025 * helper function to allocate a block for a given tree
3026 * returns the tree buffer or NULL.
3028 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
3029 struct btrfs_root *root,
3030 u32 blocksize, u64 parent,
3037 struct btrfs_key ins;
3039 struct extent_buffer *buf;
3041 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
3042 root_objectid, ref_generation, level,
3043 empty_size, hint, (u64)-1, &ins, 0);
3046 return ERR_PTR(ret);
3049 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3054 int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3055 struct btrfs_root *root, struct extent_buffer *leaf)
3058 u64 leaf_generation;
3059 struct refsort *sorted;
3060 struct btrfs_key key;
3061 struct btrfs_file_extent_item *fi;
3068 BUG_ON(!btrfs_is_leaf(leaf));
3069 nritems = btrfs_header_nritems(leaf);
3070 leaf_owner = btrfs_header_owner(leaf);
3071 leaf_generation = btrfs_header_generation(leaf);
3073 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3074 /* we do this loop twice. The first time we build a list
3075 * of the extents we have a reference on, then we sort the list
3076 * by bytenr. The second time around we actually do the
3079 for (i = 0; i < nritems; i++) {
3083 btrfs_item_key_to_cpu(leaf, &key, i);
3085 /* only extents have references, skip everything else */
3086 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3089 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
3091 /* inline extents live in the btree, they don't have refs */
3092 if (btrfs_file_extent_type(leaf, fi) ==
3093 BTRFS_FILE_EXTENT_INLINE)
3096 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3098 /* holes don't have refs */
3099 if (disk_bytenr == 0)
3102 sorted[refi].bytenr = disk_bytenr;
3103 sorted[refi].slot = i;
3110 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3112 for (i = 0; i < refi; i++) {
3115 disk_bytenr = sorted[i].bytenr;
3116 slot = sorted[i].slot;
3120 btrfs_item_key_to_cpu(leaf, &key, slot);
3121 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3124 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3126 ret = btrfs_free_extent(trans, root, disk_bytenr,
3127 btrfs_file_extent_disk_num_bytes(leaf, fi),
3128 leaf->start, leaf_owner, leaf_generation,
3132 atomic_inc(&root->fs_info->throttle_gen);
3133 wake_up(&root->fs_info->transaction_throttle);
3141 static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3142 struct btrfs_root *root,
3143 struct btrfs_leaf_ref *ref)
3147 struct btrfs_extent_info *info;
3148 struct refsort *sorted;
3150 if (ref->nritems == 0)
3153 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
3154 for (i = 0; i < ref->nritems; i++) {
3155 sorted[i].bytenr = ref->extents[i].bytenr;
3158 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3161 * the items in the ref were sorted when the ref was inserted
3162 * into the ref cache, so this is already in order
3164 for (i = 0; i < ref->nritems; i++) {
3165 info = ref->extents + sorted[i].slot;
3166 ret = btrfs_free_extent(trans, root, info->bytenr,
3167 info->num_bytes, ref->bytenr,
3168 ref->owner, ref->generation,
3171 atomic_inc(&root->fs_info->throttle_gen);
3172 wake_up(&root->fs_info->transaction_throttle);
3183 static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3184 struct btrfs_root *root, u64 start,
3189 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
3192 #if 0 /* some debugging code in case we see problems here */
3193 /* if the refs count is one, it won't get increased again. But
3194 * if the ref count is > 1, someone may be decreasing it at
3195 * the same time we are.
3198 struct extent_buffer *eb = NULL;
3199 eb = btrfs_find_create_tree_block(root, start, len);
3201 btrfs_tree_lock(eb);
3203 mutex_lock(&root->fs_info->alloc_mutex);
3204 ret = lookup_extent_ref(NULL, root, start, len, refs);
3206 mutex_unlock(&root->fs_info->alloc_mutex);
3209 btrfs_tree_unlock(eb);
3210 free_extent_buffer(eb);
3213 printk(KERN_ERR "btrfs block %llu went down to one "
3214 "during drop_snap\n", (unsigned long long)start);
3225 * this is used while deleting old snapshots, and it drops the refs
3226 * on a whole subtree starting from a level 1 node.
3228 * The idea is to sort all the leaf pointers, and then drop the
3229 * ref on all the leaves in order. Most of the time the leaves
3230 * will have ref cache entries, so no leaf IOs will be required to
3231 * find the extents they have references on.
3233 * For each leaf, any references it has are also dropped in order
3235 * This ends up dropping the references in something close to optimal
3236 * order for reading and modifying the extent allocation tree.
3238 static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3239 struct btrfs_root *root,
3240 struct btrfs_path *path)
3245 struct extent_buffer *eb = path->nodes[1];
3246 struct extent_buffer *leaf;
3247 struct btrfs_leaf_ref *ref;
3248 struct refsort *sorted = NULL;
3249 int nritems = btrfs_header_nritems(eb);
3253 int slot = path->slots[1];
3254 u32 blocksize = btrfs_level_size(root, 0);
3260 root_owner = btrfs_header_owner(eb);
3261 root_gen = btrfs_header_generation(eb);
3262 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3265 * step one, sort all the leaf pointers so we don't scribble
3266 * randomly into the extent allocation tree
3268 for (i = slot; i < nritems; i++) {
3269 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3270 sorted[refi].slot = i;
3275 * nritems won't be zero, but if we're picking up drop_snapshot
3276 * after a crash, slot might be > 0, so double check things
3282 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3285 * the first loop frees everything the leaves point to
3287 for (i = 0; i < refi; i++) {
3290 bytenr = sorted[i].bytenr;
3293 * check the reference count on this leaf. If it is > 1
3294 * we just decrement it below and don't update any
3295 * of the refs the leaf points to.
3297 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3303 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3306 * the leaf only had one reference, which means the
3307 * only thing pointing to this leaf is the snapshot
3308 * we're deleting. It isn't possible for the reference
3309 * count to increase again later
3311 * The reference cache is checked for the leaf,
3312 * and if found we'll be able to drop any refs held by
3313 * the leaf without needing to read it in.
3315 ref = btrfs_lookup_leaf_ref(root, bytenr);
3316 if (ref && ref->generation != ptr_gen) {
3317 btrfs_free_leaf_ref(root, ref);
3321 ret = cache_drop_leaf_ref(trans, root, ref);
3323 btrfs_remove_leaf_ref(root, ref);
3324 btrfs_free_leaf_ref(root, ref);
3327 * the leaf wasn't in the reference cache, so
3328 * we have to read it.
3330 leaf = read_tree_block(root, bytenr, blocksize,
3332 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3334 free_extent_buffer(leaf);
3336 atomic_inc(&root->fs_info->throttle_gen);
3337 wake_up(&root->fs_info->transaction_throttle);
3342 * run through the loop again to free the refs on the leaves.
3343 * This is faster than doing it in the loop above because
3344 * the leaves are likely to be clustered together. We end up
3345 * working in nice chunks on the extent allocation tree.
3347 for (i = 0; i < refi; i++) {
3348 bytenr = sorted[i].bytenr;
3349 ret = btrfs_free_extent(trans, root, bytenr,
3350 blocksize, eb->start,
3351 root_owner, root_gen, 0, 1);
3354 atomic_inc(&root->fs_info->throttle_gen);
3355 wake_up(&root->fs_info->transaction_throttle);
3362 * update the path to show we've processed the entire level 1
3363 * node. This will get saved into the root's drop_snapshot_progress
3364 * field so these drops are not repeated again if this transaction
3367 path->slots[1] = nritems;
3372 * helper function for drop_snapshot, this walks down the tree dropping ref
3373 * counts as it goes.
3375 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
3376 struct btrfs_root *root,
3377 struct btrfs_path *path, int *level)
3383 struct extent_buffer *next;
3384 struct extent_buffer *cur;
3385 struct extent_buffer *parent;
3390 WARN_ON(*level < 0);
3391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3392 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
3393 path->nodes[*level]->len, &refs);
3399 * walk down to the last node level and free all the leaves
3401 while (*level >= 0) {
3402 WARN_ON(*level < 0);
3403 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3404 cur = path->nodes[*level];
3406 if (btrfs_header_level(cur) != *level)
3409 if (path->slots[*level] >=
3410 btrfs_header_nritems(cur))
3413 /* the new code goes down to level 1 and does all the
3414 * leaves pointed to that node in bulk. So, this check
3415 * for level 0 will always be false.
3417 * But, the disk format allows the drop_snapshot_progress
3418 * field in the root to leave things in a state where
3419 * a leaf will need cleaning up here. If someone crashes
3420 * with the old code and then boots with the new code,
3421 * we might find a leaf here.
3424 ret = btrfs_drop_leaf_ref(trans, root, cur);
3430 * once we get to level one, process the whole node
3431 * at once, including everything below it.
3434 ret = drop_level_one_refs(trans, root, path);
3439 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3440 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3441 blocksize = btrfs_level_size(root, *level - 1);
3443 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3448 * if there is more than one reference, we don't need
3449 * to read that node to drop any references it has. We
3450 * just drop the ref we hold on that node and move on to the
3451 * next slot in this level.
3454 parent = path->nodes[*level];
3455 root_owner = btrfs_header_owner(parent);
3456 root_gen = btrfs_header_generation(parent);
3457 path->slots[*level]++;
3459 ret = btrfs_free_extent(trans, root, bytenr,
3460 blocksize, parent->start,
3461 root_owner, root_gen,
3465 atomic_inc(&root->fs_info->throttle_gen);
3466 wake_up(&root->fs_info->transaction_throttle);
3473 * we need to keep freeing things in the next level down.
3474 * read the block and loop around to process it
3476 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3477 WARN_ON(*level <= 0);
3478 if (path->nodes[*level-1])
3479 free_extent_buffer(path->nodes[*level-1]);
3480 path->nodes[*level-1] = next;
3481 *level = btrfs_header_level(next);
3482 path->slots[*level] = 0;
3486 WARN_ON(*level < 0);
3487 WARN_ON(*level >= BTRFS_MAX_LEVEL);
3489 if (path->nodes[*level] == root->node) {
3490 parent = path->nodes[*level];
3491 bytenr = path->nodes[*level]->start;
3493 parent = path->nodes[*level + 1];
3494 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
3497 blocksize = btrfs_level_size(root, *level);
3498 root_owner = btrfs_header_owner(parent);
3499 root_gen = btrfs_header_generation(parent);
3502 * cleanup and free the reference on the last node
3505 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3506 parent->start, root_owner, root_gen,
3508 free_extent_buffer(path->nodes[*level]);
3509 path->nodes[*level] = NULL;
3519 * helper function for drop_subtree, this function is similar to
3520 * walk_down_tree. The main difference is that it checks reference
3521 * counts while tree blocks are locked.
3523 static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
3524 struct btrfs_root *root,
3525 struct btrfs_path *path, int *level)
3527 struct extent_buffer *next;
3528 struct extent_buffer *cur;
3529 struct extent_buffer *parent;
3536 cur = path->nodes[*level];
3537 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3543 while (*level >= 0) {
3544 cur = path->nodes[*level];
3546 ret = btrfs_drop_leaf_ref(trans, root, cur);
3548 clean_tree_block(trans, root, cur);
3551 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3552 clean_tree_block(trans, root, cur);
3556 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3557 blocksize = btrfs_level_size(root, *level - 1);
3558 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3560 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3561 btrfs_tree_lock(next);
3562 btrfs_set_lock_blocking(next);
3564 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3568 parent = path->nodes[*level];
3569 ret = btrfs_free_extent(trans, root, bytenr,
3570 blocksize, parent->start,
3571 btrfs_header_owner(parent),
3572 btrfs_header_generation(parent),
3575 path->slots[*level]++;
3576 btrfs_tree_unlock(next);
3577 free_extent_buffer(next);
3581 *level = btrfs_header_level(next);
3582 path->nodes[*level] = next;
3583 path->slots[*level] = 0;
3584 path->locks[*level] = 1;
3588 parent = path->nodes[*level + 1];
3589 bytenr = path->nodes[*level]->start;
3590 blocksize = path->nodes[*level]->len;
3592 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3593 parent->start, btrfs_header_owner(parent),
3594 btrfs_header_generation(parent), *level, 1);
3597 if (path->locks[*level]) {
3598 btrfs_tree_unlock(path->nodes[*level]);
3599 path->locks[*level] = 0;
3601 free_extent_buffer(path->nodes[*level]);
3602 path->nodes[*level] = NULL;
3609 * helper for dropping snapshots. This walks back up the tree in the path
3610 * to find the first node higher up where we haven't yet gone through
3613 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
3614 struct btrfs_root *root,
3615 struct btrfs_path *path,
3616 int *level, int max_level)
3620 struct btrfs_root_item *root_item = &root->root_item;
3625 for (i = *level; i < max_level && path->nodes[i]; i++) {
3626 slot = path->slots[i];
3627 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3628 struct extent_buffer *node;
3629 struct btrfs_disk_key disk_key;
3632 * there is more work to do in this level.
3633 * Update the drop_progress marker to reflect
3634 * the work we've done so far, and then bump
3637 node = path->nodes[i];
3640 WARN_ON(*level == 0);
3641 btrfs_node_key(node, &disk_key, path->slots[i]);
3642 memcpy(&root_item->drop_progress,
3643 &disk_key, sizeof(disk_key));
3644 root_item->drop_level = i;
3647 struct extent_buffer *parent;
3650 * this whole node is done, free our reference
3651 * on it and go up one level
3653 if (path->nodes[*level] == root->node)
3654 parent = path->nodes[*level];
3656 parent = path->nodes[*level + 1];
3658 root_owner = btrfs_header_owner(parent);
3659 root_gen = btrfs_header_generation(parent);
3661 clean_tree_block(trans, root, path->nodes[*level]);
3662 ret = btrfs_free_extent(trans, root,
3663 path->nodes[*level]->start,
3664 path->nodes[*level]->len,
3665 parent->start, root_owner,
3666 root_gen, *level, 1);
3668 if (path->locks[*level]) {
3669 btrfs_tree_unlock(path->nodes[*level]);
3670 path->locks[*level] = 0;
3672 free_extent_buffer(path->nodes[*level]);
3673 path->nodes[*level] = NULL;
3681 * drop the reference count on the tree rooted at 'snap'. This traverses
3682 * the tree freeing any blocks that have a ref count of zero after being
3685 int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
3691 struct btrfs_path *path;
3695 struct btrfs_root_item *root_item = &root->root_item;
3697 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
3698 path = btrfs_alloc_path();
3701 level = btrfs_header_level(root->node);
3703 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3704 path->nodes[level] = root->node;
3705 extent_buffer_get(root->node);
3706 path->slots[level] = 0;
3708 struct btrfs_key key;
3709 struct btrfs_disk_key found_key;
3710 struct extent_buffer *node;
3712 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
3713 level = root_item->drop_level;
3714 path->lowest_level = level;
3715 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3720 node = path->nodes[level];
3721 btrfs_node_key(node, &found_key, path->slots[level]);
3722 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3723 sizeof(found_key)));
3725 * unlock our path, this is safe because only this
3726 * function is allowed to delete this snapshot
3728 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3729 if (path->nodes[i] && path->locks[i]) {
3731 btrfs_tree_unlock(path->nodes[i]);
3736 unsigned long update;
3737 wret = walk_down_tree(trans, root, path, &level);
3743 wret = walk_up_tree(trans, root, path, &level,
3749 if (trans->transaction->in_commit ||
3750 trans->transaction->delayed_refs.flushing) {
3754 atomic_inc(&root->fs_info->throttle_gen);
3755 wake_up(&root->fs_info->transaction_throttle);
3756 for (update_count = 0; update_count < 16; update_count++) {
3757 update = trans->delayed_ref_updates;
3758 trans->delayed_ref_updates = 0;
3760 btrfs_run_delayed_refs(trans, root, update);
3765 for (i = 0; i <= orig_level; i++) {
3766 if (path->nodes[i]) {
3767 free_extent_buffer(path->nodes[i]);
3768 path->nodes[i] = NULL;
3772 btrfs_free_path(path);
3776 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root,
3778 struct extent_buffer *node,
3779 struct extent_buffer *parent)
3781 struct btrfs_path *path;
3787 path = btrfs_alloc_path();
3790 btrfs_assert_tree_locked(parent);
3791 parent_level = btrfs_header_level(parent);
3792 extent_buffer_get(parent);
3793 path->nodes[parent_level] = parent;
3794 path->slots[parent_level] = btrfs_header_nritems(parent);
3796 btrfs_assert_tree_locked(node);
3797 level = btrfs_header_level(node);
3798 extent_buffer_get(node);
3799 path->nodes[level] = node;
3800 path->slots[level] = 0;
3803 wret = walk_down_subtree(trans, root, path, &level);
3809 wret = walk_up_tree(trans, root, path, &level, parent_level);
3816 btrfs_free_path(path);
3820 static unsigned long calc_ra(unsigned long start, unsigned long last,
3823 return min(last, start + nr - 1);
3826 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
3831 unsigned long first_index;
3832 unsigned long last_index;
3835 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3836 struct file_ra_state *ra;
3837 struct btrfs_ordered_extent *ordered;
3838 unsigned int total_read = 0;
3839 unsigned int total_dirty = 0;
3842 ra = kzalloc(sizeof(*ra), GFP_NOFS);
3844 mutex_lock(&inode->i_mutex);
3845 first_index = start >> PAGE_CACHE_SHIFT;
3846 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3848 /* make sure the dirty trick played by the caller work */
3849 ret = invalidate_inode_pages2_range(inode->i_mapping,
3850 first_index, last_index);
3854 file_ra_state_init(ra, inode->i_mapping);
3856 for (i = first_index ; i <= last_index; i++) {
3857 if (total_read % ra->ra_pages == 0) {
3858 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
3859 calc_ra(i, last_index, ra->ra_pages));
3863 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
3865 page = grab_cache_page(inode->i_mapping, i);
3870 if (!PageUptodate(page)) {
3871 btrfs_readpage(NULL, page);
3873 if (!PageUptodate(page)) {
3875 page_cache_release(page);
3880 wait_on_page_writeback(page);
3882 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3883 page_end = page_start + PAGE_CACHE_SIZE - 1;
3884 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3886 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3888 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3890 page_cache_release(page);
3891 btrfs_start_ordered_extent(inode, ordered, 1);
3892 btrfs_put_ordered_extent(ordered);
3895 set_page_extent_mapped(page);
3897 if (i == first_index)
3898 set_extent_bits(io_tree, page_start, page_end,
3899 EXTENT_BOUNDARY, GFP_NOFS);
3900 btrfs_set_extent_delalloc(inode, page_start, page_end);
3902 set_page_dirty(page);
3905 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3907 page_cache_release(page);
3912 mutex_unlock(&inode->i_mutex);
3913 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
3917 static noinline int relocate_data_extent(struct inode *reloc_inode,
3918 struct btrfs_key *extent_key,
3921 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3922 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3923 struct extent_map *em;
3924 u64 start = extent_key->objectid - offset;
3925 u64 end = start + extent_key->offset - 1;
3927 em = alloc_extent_map(GFP_NOFS);
3928 BUG_ON(!em || IS_ERR(em));
3931 em->len = extent_key->offset;
3932 em->block_len = extent_key->offset;
3933 em->block_start = extent_key->objectid;
3934 em->bdev = root->fs_info->fs_devices->latest_bdev;
3935 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3937 /* setup extent map to cheat btrfs_readpage */
3938 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
3941 spin_lock(&em_tree->lock);
3942 ret = add_extent_mapping(em_tree, em);
3943 spin_unlock(&em_tree->lock);
3944 if (ret != -EEXIST) {
3945 free_extent_map(em);
3948 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
3950 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
3952 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
3955 struct btrfs_ref_path {
3957 u64 nodes[BTRFS_MAX_LEVEL];
3959 u64 root_generation;
3966 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3967 u64 new_nodes[BTRFS_MAX_LEVEL];
3970 struct disk_extent {
3981 static int is_cowonly_root(u64 root_objectid)
3983 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3984 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3985 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3986 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3987 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
3988 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
3993 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
3994 struct btrfs_root *extent_root,
3995 struct btrfs_ref_path *ref_path,
3998 struct extent_buffer *leaf;
3999 struct btrfs_path *path;
4000 struct btrfs_extent_ref *ref;
4001 struct btrfs_key key;
4002 struct btrfs_key found_key;
4008 path = btrfs_alloc_path();
4013 ref_path->lowest_level = -1;
4014 ref_path->current_level = -1;
4015 ref_path->shared_level = -1;
4019 level = ref_path->current_level - 1;
4020 while (level >= -1) {
4022 if (level < ref_path->lowest_level)
4026 bytenr = ref_path->nodes[level];
4028 bytenr = ref_path->extent_start;
4029 BUG_ON(bytenr == 0);
4031 parent = ref_path->nodes[level + 1];
4032 ref_path->nodes[level + 1] = 0;
4033 ref_path->current_level = level;
4034 BUG_ON(parent == 0);
4036 key.objectid = bytenr;
4037 key.offset = parent + 1;
4038 key.type = BTRFS_EXTENT_REF_KEY;
4040 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4045 leaf = path->nodes[0];
4046 nritems = btrfs_header_nritems(leaf);
4047 if (path->slots[0] >= nritems) {
4048 ret = btrfs_next_leaf(extent_root, path);
4053 leaf = path->nodes[0];
4056 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4057 if (found_key.objectid == bytenr &&
4058 found_key.type == BTRFS_EXTENT_REF_KEY) {
4059 if (level < ref_path->shared_level)
4060 ref_path->shared_level = level;
4065 btrfs_release_path(extent_root, path);
4068 /* reached lowest level */
4072 level = ref_path->current_level;
4073 while (level < BTRFS_MAX_LEVEL - 1) {
4077 bytenr = ref_path->nodes[level];
4079 bytenr = ref_path->extent_start;
4081 BUG_ON(bytenr == 0);
4083 key.objectid = bytenr;
4085 key.type = BTRFS_EXTENT_REF_KEY;
4087 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4091 leaf = path->nodes[0];
4092 nritems = btrfs_header_nritems(leaf);
4093 if (path->slots[0] >= nritems) {
4094 ret = btrfs_next_leaf(extent_root, path);
4098 /* the extent was freed by someone */
4099 if (ref_path->lowest_level == level)
4101 btrfs_release_path(extent_root, path);
4104 leaf = path->nodes[0];
4107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4108 if (found_key.objectid != bytenr ||
4109 found_key.type != BTRFS_EXTENT_REF_KEY) {
4110 /* the extent was freed by someone */
4111 if (ref_path->lowest_level == level) {
4115 btrfs_release_path(extent_root, path);
4119 ref = btrfs_item_ptr(leaf, path->slots[0],
4120 struct btrfs_extent_ref);
4121 ref_objectid = btrfs_ref_objectid(leaf, ref);
4122 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4124 level = (int)ref_objectid;
4125 BUG_ON(level >= BTRFS_MAX_LEVEL);
4126 ref_path->lowest_level = level;
4127 ref_path->current_level = level;
4128 ref_path->nodes[level] = bytenr;
4130 WARN_ON(ref_objectid != level);
4133 WARN_ON(level != -1);
4137 if (ref_path->lowest_level == level) {
4138 ref_path->owner_objectid = ref_objectid;
4139 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4143 * the block is tree root or the block isn't in reference
4146 if (found_key.objectid == found_key.offset ||
4147 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4148 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4149 ref_path->root_generation =
4150 btrfs_ref_generation(leaf, ref);
4152 /* special reference from the tree log */
4153 ref_path->nodes[0] = found_key.offset;
4154 ref_path->current_level = 0;
4161 BUG_ON(ref_path->nodes[level] != 0);
4162 ref_path->nodes[level] = found_key.offset;
4163 ref_path->current_level = level;
4166 * the reference was created in the running transaction,
4167 * no need to continue walking up.
4169 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4170 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4171 ref_path->root_generation =
4172 btrfs_ref_generation(leaf, ref);
4177 btrfs_release_path(extent_root, path);
4180 /* reached max tree level, but no tree root found. */
4183 btrfs_free_path(path);
4187 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4188 struct btrfs_root *extent_root,
4189 struct btrfs_ref_path *ref_path,
4192 memset(ref_path, 0, sizeof(*ref_path));
4193 ref_path->extent_start = extent_start;
4195 return __next_ref_path(trans, extent_root, ref_path, 1);
4198 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4199 struct btrfs_root *extent_root,
4200 struct btrfs_ref_path *ref_path)
4202 return __next_ref_path(trans, extent_root, ref_path, 0);
4205 static noinline int get_new_locations(struct inode *reloc_inode,
4206 struct btrfs_key *extent_key,
4207 u64 offset, int no_fragment,
4208 struct disk_extent **extents,
4211 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4212 struct btrfs_path *path;
4213 struct btrfs_file_extent_item *fi;
4214 struct extent_buffer *leaf;
4215 struct disk_extent *exts = *extents;
4216 struct btrfs_key found_key;
4221 int max = *nr_extents;
4224 WARN_ON(!no_fragment && *extents);
4227 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4232 path = btrfs_alloc_path();
4235 cur_pos = extent_key->objectid - offset;
4236 last_byte = extent_key->objectid + extent_key->offset;
4237 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4247 leaf = path->nodes[0];
4248 nritems = btrfs_header_nritems(leaf);
4249 if (path->slots[0] >= nritems) {
4250 ret = btrfs_next_leaf(root, path);
4255 leaf = path->nodes[0];
4258 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4259 if (found_key.offset != cur_pos ||
4260 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4261 found_key.objectid != reloc_inode->i_ino)
4264 fi = btrfs_item_ptr(leaf, path->slots[0],
4265 struct btrfs_file_extent_item);
4266 if (btrfs_file_extent_type(leaf, fi) !=
4267 BTRFS_FILE_EXTENT_REG ||
4268 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4272 struct disk_extent *old = exts;
4274 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4275 memcpy(exts, old, sizeof(*exts) * nr);
4276 if (old != *extents)
4280 exts[nr].disk_bytenr =
4281 btrfs_file_extent_disk_bytenr(leaf, fi);
4282 exts[nr].disk_num_bytes =
4283 btrfs_file_extent_disk_num_bytes(leaf, fi);
4284 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4285 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4286 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4287 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4288 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4289 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4291 BUG_ON(exts[nr].offset > 0);
4292 BUG_ON(exts[nr].compression || exts[nr].encryption);
4293 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
4295 cur_pos += exts[nr].num_bytes;
4298 if (cur_pos + offset >= last_byte)
4308 BUG_ON(cur_pos + offset > last_byte);
4309 if (cur_pos + offset < last_byte) {
4315 btrfs_free_path(path);
4317 if (exts != *extents)
4326 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
4327 struct btrfs_root *root,
4328 struct btrfs_path *path,
4329 struct btrfs_key *extent_key,
4330 struct btrfs_key *leaf_key,
4331 struct btrfs_ref_path *ref_path,
4332 struct disk_extent *new_extents,
4335 struct extent_buffer *leaf;
4336 struct btrfs_file_extent_item *fi;
4337 struct inode *inode = NULL;
4338 struct btrfs_key key;
4343 u64 search_end = (u64)-1;
4346 int extent_locked = 0;
4350 memcpy(&key, leaf_key, sizeof(key));
4351 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4352 if (key.objectid < ref_path->owner_objectid ||
4353 (key.objectid == ref_path->owner_objectid &&
4354 key.type < BTRFS_EXTENT_DATA_KEY)) {
4355 key.objectid = ref_path->owner_objectid;
4356 key.type = BTRFS_EXTENT_DATA_KEY;
4362 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4366 leaf = path->nodes[0];
4367 nritems = btrfs_header_nritems(leaf);
4369 if (extent_locked && ret > 0) {
4371 * the file extent item was modified by someone
4372 * before the extent got locked.
4374 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4375 lock_end, GFP_NOFS);
4379 if (path->slots[0] >= nritems) {
4380 if (++nr_scaned > 2)
4383 BUG_ON(extent_locked);
4384 ret = btrfs_next_leaf(root, path);
4389 leaf = path->nodes[0];
4390 nritems = btrfs_header_nritems(leaf);
4393 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4395 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4396 if ((key.objectid > ref_path->owner_objectid) ||
4397 (key.objectid == ref_path->owner_objectid &&
4398 key.type > BTRFS_EXTENT_DATA_KEY) ||
4399 key.offset >= search_end)
4403 if (inode && key.objectid != inode->i_ino) {
4404 BUG_ON(extent_locked);
4405 btrfs_release_path(root, path);
4406 mutex_unlock(&inode->i_mutex);
4412 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4417 fi = btrfs_item_ptr(leaf, path->slots[0],
4418 struct btrfs_file_extent_item);
4419 extent_type = btrfs_file_extent_type(leaf, fi);
4420 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4421 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
4422 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4423 extent_key->objectid)) {
4429 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4430 ext_offset = btrfs_file_extent_offset(leaf, fi);
4432 if (search_end == (u64)-1) {
4433 search_end = key.offset - ext_offset +
4434 btrfs_file_extent_ram_bytes(leaf, fi);
4437 if (!extent_locked) {
4438 lock_start = key.offset;
4439 lock_end = lock_start + num_bytes - 1;
4441 if (lock_start > key.offset ||
4442 lock_end + 1 < key.offset + num_bytes) {
4443 unlock_extent(&BTRFS_I(inode)->io_tree,
4444 lock_start, lock_end, GFP_NOFS);
4450 btrfs_release_path(root, path);
4452 inode = btrfs_iget_locked(root->fs_info->sb,
4453 key.objectid, root);
4454 if (inode->i_state & I_NEW) {
4455 BTRFS_I(inode)->root = root;
4456 BTRFS_I(inode)->location.objectid =
4458 BTRFS_I(inode)->location.type =
4459 BTRFS_INODE_ITEM_KEY;
4460 BTRFS_I(inode)->location.offset = 0;
4461 btrfs_read_locked_inode(inode);
4462 unlock_new_inode(inode);
4465 * some code call btrfs_commit_transaction while
4466 * holding the i_mutex, so we can't use mutex_lock
4469 if (is_bad_inode(inode) ||
4470 !mutex_trylock(&inode->i_mutex)) {
4473 key.offset = (u64)-1;
4478 if (!extent_locked) {
4479 struct btrfs_ordered_extent *ordered;
4481 btrfs_release_path(root, path);
4483 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4484 lock_end, GFP_NOFS);
4485 ordered = btrfs_lookup_first_ordered_extent(inode,
4488 ordered->file_offset <= lock_end &&
4489 ordered->file_offset + ordered->len > lock_start) {
4490 unlock_extent(&BTRFS_I(inode)->io_tree,
4491 lock_start, lock_end, GFP_NOFS);
4492 btrfs_start_ordered_extent(inode, ordered, 1);
4493 btrfs_put_ordered_extent(ordered);
4494 key.offset += num_bytes;
4498 btrfs_put_ordered_extent(ordered);
4504 if (nr_extents == 1) {
4505 /* update extent pointer in place */
4506 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4507 new_extents[0].disk_bytenr);
4508 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4509 new_extents[0].disk_num_bytes);
4510 btrfs_mark_buffer_dirty(leaf);
4512 btrfs_drop_extent_cache(inode, key.offset,
4513 key.offset + num_bytes - 1, 0);
4515 ret = btrfs_inc_extent_ref(trans, root,
4516 new_extents[0].disk_bytenr,
4517 new_extents[0].disk_num_bytes,
4519 root->root_key.objectid,
4524 ret = btrfs_free_extent(trans, root,
4525 extent_key->objectid,
4528 btrfs_header_owner(leaf),
4529 btrfs_header_generation(leaf),
4533 btrfs_release_path(root, path);
4534 key.offset += num_bytes;
4542 * drop old extent pointer at first, then insert the
4543 * new pointers one bye one
4545 btrfs_release_path(root, path);
4546 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4547 key.offset + num_bytes,
4548 key.offset, &alloc_hint);
4551 for (i = 0; i < nr_extents; i++) {
4552 if (ext_offset >= new_extents[i].num_bytes) {
4553 ext_offset -= new_extents[i].num_bytes;
4556 extent_len = min(new_extents[i].num_bytes -
4557 ext_offset, num_bytes);
4559 ret = btrfs_insert_empty_item(trans, root,
4564 leaf = path->nodes[0];
4565 fi = btrfs_item_ptr(leaf, path->slots[0],
4566 struct btrfs_file_extent_item);
4567 btrfs_set_file_extent_generation(leaf, fi,
4569 btrfs_set_file_extent_type(leaf, fi,
4570 BTRFS_FILE_EXTENT_REG);
4571 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4572 new_extents[i].disk_bytenr);
4573 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4574 new_extents[i].disk_num_bytes);
4575 btrfs_set_file_extent_ram_bytes(leaf, fi,
4576 new_extents[i].ram_bytes);
4578 btrfs_set_file_extent_compression(leaf, fi,
4579 new_extents[i].compression);
4580 btrfs_set_file_extent_encryption(leaf, fi,
4581 new_extents[i].encryption);
4582 btrfs_set_file_extent_other_encoding(leaf, fi,
4583 new_extents[i].other_encoding);
4585 btrfs_set_file_extent_num_bytes(leaf, fi,
4587 ext_offset += new_extents[i].offset;
4588 btrfs_set_file_extent_offset(leaf, fi,
4590 btrfs_mark_buffer_dirty(leaf);
4592 btrfs_drop_extent_cache(inode, key.offset,
4593 key.offset + extent_len - 1, 0);
4595 ret = btrfs_inc_extent_ref(trans, root,
4596 new_extents[i].disk_bytenr,
4597 new_extents[i].disk_num_bytes,
4599 root->root_key.objectid,
4600 trans->transid, key.objectid);
4602 btrfs_release_path(root, path);
4604 inode_add_bytes(inode, extent_len);
4607 num_bytes -= extent_len;
4608 key.offset += extent_len;
4613 BUG_ON(i >= nr_extents);
4617 if (extent_locked) {
4618 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4619 lock_end, GFP_NOFS);
4623 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4624 key.offset >= search_end)
4631 btrfs_release_path(root, path);
4633 mutex_unlock(&inode->i_mutex);
4634 if (extent_locked) {
4635 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4636 lock_end, GFP_NOFS);
4643 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root,
4645 struct extent_buffer *buf, u64 orig_start)
4650 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4651 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4653 level = btrfs_header_level(buf);
4655 struct btrfs_leaf_ref *ref;
4656 struct btrfs_leaf_ref *orig_ref;
4658 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4662 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4664 btrfs_free_leaf_ref(root, orig_ref);
4668 ref->nritems = orig_ref->nritems;
4669 memcpy(ref->extents, orig_ref->extents,
4670 sizeof(ref->extents[0]) * ref->nritems);
4672 btrfs_free_leaf_ref(root, orig_ref);
4674 ref->root_gen = trans->transid;
4675 ref->bytenr = buf->start;
4676 ref->owner = btrfs_header_owner(buf);
4677 ref->generation = btrfs_header_generation(buf);
4679 ret = btrfs_add_leaf_ref(root, ref, 0);
4681 btrfs_free_leaf_ref(root, ref);
4686 static noinline int invalidate_extent_cache(struct btrfs_root *root,
4687 struct extent_buffer *leaf,
4688 struct btrfs_block_group_cache *group,
4689 struct btrfs_root *target_root)
4691 struct btrfs_key key;
4692 struct inode *inode = NULL;
4693 struct btrfs_file_extent_item *fi;
4695 u64 skip_objectid = 0;
4699 nritems = btrfs_header_nritems(leaf);
4700 for (i = 0; i < nritems; i++) {
4701 btrfs_item_key_to_cpu(leaf, &key, i);
4702 if (key.objectid == skip_objectid ||
4703 key.type != BTRFS_EXTENT_DATA_KEY)
4705 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4706 if (btrfs_file_extent_type(leaf, fi) ==
4707 BTRFS_FILE_EXTENT_INLINE)
4709 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4711 if (!inode || inode->i_ino != key.objectid) {
4713 inode = btrfs_ilookup(target_root->fs_info->sb,
4714 key.objectid, target_root, 1);
4717 skip_objectid = key.objectid;
4720 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4722 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4723 key.offset + num_bytes - 1, GFP_NOFS);
4724 btrfs_drop_extent_cache(inode, key.offset,
4725 key.offset + num_bytes - 1, 1);
4726 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4727 key.offset + num_bytes - 1, GFP_NOFS);
4734 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4735 struct btrfs_root *root,
4736 struct extent_buffer *leaf,
4737 struct btrfs_block_group_cache *group,
4738 struct inode *reloc_inode)
4740 struct btrfs_key key;
4741 struct btrfs_key extent_key;
4742 struct btrfs_file_extent_item *fi;
4743 struct btrfs_leaf_ref *ref;
4744 struct disk_extent *new_extent;
4753 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4754 BUG_ON(!new_extent);
4756 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4760 nritems = btrfs_header_nritems(leaf);
4761 for (i = 0; i < nritems; i++) {
4762 btrfs_item_key_to_cpu(leaf, &key, i);
4763 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4765 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4766 if (btrfs_file_extent_type(leaf, fi) ==
4767 BTRFS_FILE_EXTENT_INLINE)
4769 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4770 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4775 if (bytenr >= group->key.objectid + group->key.offset ||
4776 bytenr + num_bytes <= group->key.objectid)
4779 extent_key.objectid = bytenr;
4780 extent_key.offset = num_bytes;
4781 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4783 ret = get_new_locations(reloc_inode, &extent_key,
4784 group->key.objectid, 1,
4785 &new_extent, &nr_extent);
4790 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4791 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4792 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4793 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4795 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4796 new_extent->disk_bytenr);
4797 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4798 new_extent->disk_num_bytes);
4799 btrfs_mark_buffer_dirty(leaf);
4801 ret = btrfs_inc_extent_ref(trans, root,
4802 new_extent->disk_bytenr,
4803 new_extent->disk_num_bytes,
4805 root->root_key.objectid,
4806 trans->transid, key.objectid);
4809 ret = btrfs_free_extent(trans, root,
4810 bytenr, num_bytes, leaf->start,
4811 btrfs_header_owner(leaf),
4812 btrfs_header_generation(leaf),
4818 BUG_ON(ext_index + 1 != ref->nritems);
4819 btrfs_free_leaf_ref(root, ref);
4823 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4824 struct btrfs_root *root)
4826 struct btrfs_root *reloc_root;
4829 if (root->reloc_root) {
4830 reloc_root = root->reloc_root;
4831 root->reloc_root = NULL;
4832 list_add(&reloc_root->dead_list,
4833 &root->fs_info->dead_reloc_roots);
4835 btrfs_set_root_bytenr(&reloc_root->root_item,
4836 reloc_root->node->start);
4837 btrfs_set_root_level(&root->root_item,
4838 btrfs_header_level(reloc_root->node));
4839 memset(&reloc_root->root_item.drop_progress, 0,
4840 sizeof(struct btrfs_disk_key));
4841 reloc_root->root_item.drop_level = 0;
4843 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4844 &reloc_root->root_key,
4845 &reloc_root->root_item);
4851 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4853 struct btrfs_trans_handle *trans;
4854 struct btrfs_root *reloc_root;
4855 struct btrfs_root *prev_root = NULL;
4856 struct list_head dead_roots;
4860 INIT_LIST_HEAD(&dead_roots);
4861 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4863 while (!list_empty(&dead_roots)) {
4864 reloc_root = list_entry(dead_roots.prev,
4865 struct btrfs_root, dead_list);
4866 list_del_init(&reloc_root->dead_list);
4868 BUG_ON(reloc_root->commit_root != NULL);
4870 trans = btrfs_join_transaction(root, 1);
4873 mutex_lock(&root->fs_info->drop_mutex);
4874 ret = btrfs_drop_snapshot(trans, reloc_root);
4877 mutex_unlock(&root->fs_info->drop_mutex);
4879 nr = trans->blocks_used;
4880 ret = btrfs_end_transaction(trans, root);
4882 btrfs_btree_balance_dirty(root, nr);
4885 free_extent_buffer(reloc_root->node);
4887 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4888 &reloc_root->root_key);
4890 mutex_unlock(&root->fs_info->drop_mutex);
4892 nr = trans->blocks_used;
4893 ret = btrfs_end_transaction(trans, root);
4895 btrfs_btree_balance_dirty(root, nr);
4898 prev_root = reloc_root;
4901 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4907 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4909 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4913 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4915 struct btrfs_root *reloc_root;
4916 struct btrfs_trans_handle *trans;
4917 struct btrfs_key location;
4921 mutex_lock(&root->fs_info->tree_reloc_mutex);
4922 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4924 found = !list_empty(&root->fs_info->dead_reloc_roots);
4925 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4928 trans = btrfs_start_transaction(root, 1);
4930 ret = btrfs_commit_transaction(trans, root);
4934 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4935 location.offset = (u64)-1;
4936 location.type = BTRFS_ROOT_ITEM_KEY;
4938 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4939 BUG_ON(!reloc_root);
4940 btrfs_orphan_cleanup(reloc_root);
4944 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
4945 struct btrfs_root *root)
4947 struct btrfs_root *reloc_root;
4948 struct extent_buffer *eb;
4949 struct btrfs_root_item *root_item;
4950 struct btrfs_key root_key;
4953 BUG_ON(!root->ref_cows);
4954 if (root->reloc_root)
4957 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4960 ret = btrfs_copy_root(trans, root, root->commit_root,
4961 &eb, BTRFS_TREE_RELOC_OBJECTID);
4964 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4965 root_key.offset = root->root_key.objectid;
4966 root_key.type = BTRFS_ROOT_ITEM_KEY;
4968 memcpy(root_item, &root->root_item, sizeof(root_item));
4969 btrfs_set_root_refs(root_item, 0);
4970 btrfs_set_root_bytenr(root_item, eb->start);
4971 btrfs_set_root_level(root_item, btrfs_header_level(eb));
4972 btrfs_set_root_generation(root_item, trans->transid);
4974 btrfs_tree_unlock(eb);
4975 free_extent_buffer(eb);
4977 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4978 &root_key, root_item);
4982 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4984 BUG_ON(!reloc_root);
4985 reloc_root->last_trans = trans->transid;
4986 reloc_root->commit_root = NULL;
4987 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4989 root->reloc_root = reloc_root;
4994 * Core function of space balance.
4996 * The idea is using reloc trees to relocate tree blocks in reference
4997 * counted roots. There is one reloc tree for each subvol, and all
4998 * reloc trees share same root key objectid. Reloc trees are snapshots
4999 * of the latest committed roots of subvols (root->commit_root).
5001 * To relocate a tree block referenced by a subvol, there are two steps.
5002 * COW the block through subvol's reloc tree, then update block pointer
5003 * in the subvol to point to the new block. Since all reloc trees share
5004 * same root key objectid, doing special handing for tree blocks owned
5005 * by them is easy. Once a tree block has been COWed in one reloc tree,
5006 * we can use the resulting new block directly when the same block is
5007 * required to COW again through other reloc trees. By this way, relocated
5008 * tree blocks are shared between reloc trees, so they are also shared
5011 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
5012 struct btrfs_root *root,
5013 struct btrfs_path *path,
5014 struct btrfs_key *first_key,
5015 struct btrfs_ref_path *ref_path,
5016 struct btrfs_block_group_cache *group,
5017 struct inode *reloc_inode)
5019 struct btrfs_root *reloc_root;
5020 struct extent_buffer *eb = NULL;
5021 struct btrfs_key *keys;
5025 int lowest_level = 0;
5028 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5029 lowest_level = ref_path->owner_objectid;
5031 if (!root->ref_cows) {
5032 path->lowest_level = lowest_level;
5033 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5035 path->lowest_level = 0;
5036 btrfs_release_path(root, path);
5040 mutex_lock(&root->fs_info->tree_reloc_mutex);
5041 ret = init_reloc_tree(trans, root);
5043 reloc_root = root->reloc_root;
5045 shared_level = ref_path->shared_level;
5046 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
5048 keys = ref_path->node_keys;
5049 nodes = ref_path->new_nodes;
5050 memset(&keys[shared_level + 1], 0,
5051 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5052 memset(&nodes[shared_level + 1], 0,
5053 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
5055 if (nodes[lowest_level] == 0) {
5056 path->lowest_level = lowest_level;
5057 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5060 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5061 eb = path->nodes[level];
5062 if (!eb || eb == reloc_root->node)
5064 nodes[level] = eb->start;
5066 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5068 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5071 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5072 eb = path->nodes[0];
5073 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5074 group, reloc_inode);
5077 btrfs_release_path(reloc_root, path);
5079 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
5085 * replace tree blocks in the fs tree with tree blocks in
5088 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5091 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5092 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5095 extent_buffer_get(path->nodes[0]);
5096 eb = path->nodes[0];
5097 btrfs_release_path(reloc_root, path);
5098 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5100 free_extent_buffer(eb);
5103 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5104 path->lowest_level = 0;
5108 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
5109 struct btrfs_root *root,
5110 struct btrfs_path *path,
5111 struct btrfs_key *first_key,
5112 struct btrfs_ref_path *ref_path)
5116 ret = relocate_one_path(trans, root, path, first_key,
5117 ref_path, NULL, NULL);
5123 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
5124 struct btrfs_root *extent_root,
5125 struct btrfs_path *path,
5126 struct btrfs_key *extent_key)
5130 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5133 ret = btrfs_del_item(trans, extent_root, path);
5135 btrfs_release_path(extent_root, path);
5139 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
5140 struct btrfs_ref_path *ref_path)
5142 struct btrfs_key root_key;
5144 root_key.objectid = ref_path->root_objectid;
5145 root_key.type = BTRFS_ROOT_ITEM_KEY;
5146 if (is_cowonly_root(ref_path->root_objectid))
5147 root_key.offset = 0;
5149 root_key.offset = (u64)-1;
5151 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5154 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
5155 struct btrfs_path *path,
5156 struct btrfs_key *extent_key,
5157 struct btrfs_block_group_cache *group,
5158 struct inode *reloc_inode, int pass)
5160 struct btrfs_trans_handle *trans;
5161 struct btrfs_root *found_root;
5162 struct btrfs_ref_path *ref_path = NULL;
5163 struct disk_extent *new_extents = NULL;
5168 struct btrfs_key first_key;
5172 trans = btrfs_start_transaction(extent_root, 1);
5175 if (extent_key->objectid == 0) {
5176 ret = del_extent_zero(trans, extent_root, path, extent_key);
5180 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5186 for (loops = 0; ; loops++) {
5188 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5189 extent_key->objectid);
5191 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5198 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5199 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5202 found_root = read_ref_root(extent_root->fs_info, ref_path);
5203 BUG_ON(!found_root);
5205 * for reference counted tree, only process reference paths
5206 * rooted at the latest committed root.
5208 if (found_root->ref_cows &&
5209 ref_path->root_generation != found_root->root_key.offset)
5212 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5215 * copy data extents to new locations
5217 u64 group_start = group->key.objectid;
5218 ret = relocate_data_extent(reloc_inode,
5227 level = ref_path->owner_objectid;
5230 if (prev_block != ref_path->nodes[level]) {
5231 struct extent_buffer *eb;
5232 u64 block_start = ref_path->nodes[level];
5233 u64 block_size = btrfs_level_size(found_root, level);
5235 eb = read_tree_block(found_root, block_start,
5237 btrfs_tree_lock(eb);
5238 BUG_ON(level != btrfs_header_level(eb));
5241 btrfs_item_key_to_cpu(eb, &first_key, 0);
5243 btrfs_node_key_to_cpu(eb, &first_key, 0);
5245 btrfs_tree_unlock(eb);
5246 free_extent_buffer(eb);
5247 prev_block = block_start;
5250 mutex_lock(&extent_root->fs_info->trans_mutex);
5251 btrfs_record_root_in_trans(found_root);
5252 mutex_unlock(&extent_root->fs_info->trans_mutex);
5253 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5255 * try to update data extent references while
5256 * keeping metadata shared between snapshots.
5259 ret = relocate_one_path(trans, found_root,
5260 path, &first_key, ref_path,
5261 group, reloc_inode);
5267 * use fallback method to process the remaining
5271 u64 group_start = group->key.objectid;
5272 new_extents = kmalloc(sizeof(*new_extents),
5275 ret = get_new_locations(reloc_inode,
5283 ret = replace_one_extent(trans, found_root,
5285 &first_key, ref_path,
5286 new_extents, nr_extents);
5288 ret = relocate_tree_block(trans, found_root, path,
5289 &first_key, ref_path);
5296 btrfs_end_transaction(trans, extent_root);
5302 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5305 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5306 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5308 num_devices = root->fs_info->fs_devices->rw_devices;
5309 if (num_devices == 1) {
5310 stripped |= BTRFS_BLOCK_GROUP_DUP;
5311 stripped = flags & ~stripped;
5313 /* turn raid0 into single device chunks */
5314 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5317 /* turn mirroring into duplication */
5318 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5319 BTRFS_BLOCK_GROUP_RAID10))
5320 return stripped | BTRFS_BLOCK_GROUP_DUP;
5323 /* they already had raid on here, just return */
5324 if (flags & stripped)
5327 stripped |= BTRFS_BLOCK_GROUP_DUP;
5328 stripped = flags & ~stripped;
5330 /* switch duplicated blocks with raid1 */
5331 if (flags & BTRFS_BLOCK_GROUP_DUP)
5332 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5334 /* turn single device chunks into raid0 */
5335 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5340 static int __alloc_chunk_for_shrink(struct btrfs_root *root,
5341 struct btrfs_block_group_cache *shrink_block_group,
5344 struct btrfs_trans_handle *trans;
5345 u64 new_alloc_flags;
5348 spin_lock(&shrink_block_group->lock);
5349 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
5350 spin_unlock(&shrink_block_group->lock);
5352 trans = btrfs_start_transaction(root, 1);
5353 spin_lock(&shrink_block_group->lock);
5355 new_alloc_flags = update_block_group_flags(root,
5356 shrink_block_group->flags);
5357 if (new_alloc_flags != shrink_block_group->flags) {
5359 btrfs_block_group_used(&shrink_block_group->item);
5361 calc = shrink_block_group->key.offset;
5363 spin_unlock(&shrink_block_group->lock);
5365 do_chunk_alloc(trans, root->fs_info->extent_root,
5366 calc + 2 * 1024 * 1024, new_alloc_flags, force);
5368 btrfs_end_transaction(trans, root);
5370 spin_unlock(&shrink_block_group->lock);
5374 static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5375 struct btrfs_root *root,
5376 u64 objectid, u64 size)
5378 struct btrfs_path *path;
5379 struct btrfs_inode_item *item;
5380 struct extent_buffer *leaf;
5383 path = btrfs_alloc_path();
5387 path->leave_spinning = 1;
5388 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5392 leaf = path->nodes[0];
5393 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5394 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5395 btrfs_set_inode_generation(leaf, item, 1);
5396 btrfs_set_inode_size(leaf, item, size);
5397 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
5398 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
5399 btrfs_mark_buffer_dirty(leaf);
5400 btrfs_release_path(root, path);
5402 btrfs_free_path(path);
5406 static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
5407 struct btrfs_block_group_cache *group)
5409 struct inode *inode = NULL;
5410 struct btrfs_trans_handle *trans;
5411 struct btrfs_root *root;
5412 struct btrfs_key root_key;
5413 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5416 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5417 root_key.type = BTRFS_ROOT_ITEM_KEY;
5418 root_key.offset = (u64)-1;
5419 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5421 return ERR_CAST(root);
5423 trans = btrfs_start_transaction(root, 1);
5426 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5430 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5433 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
5434 group->key.offset, 0, group->key.offset,
5438 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5439 if (inode->i_state & I_NEW) {
5440 BTRFS_I(inode)->root = root;
5441 BTRFS_I(inode)->location.objectid = objectid;
5442 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5443 BTRFS_I(inode)->location.offset = 0;
5444 btrfs_read_locked_inode(inode);
5445 unlock_new_inode(inode);
5446 BUG_ON(is_bad_inode(inode));
5450 BTRFS_I(inode)->index_cnt = group->key.objectid;
5452 err = btrfs_orphan_add(trans, inode);
5454 btrfs_end_transaction(trans, root);
5458 inode = ERR_PTR(err);
5463 int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5466 struct btrfs_ordered_sum *sums;
5467 struct btrfs_sector_sum *sector_sum;
5468 struct btrfs_ordered_extent *ordered;
5469 struct btrfs_root *root = BTRFS_I(inode)->root;
5470 struct list_head list;
5475 INIT_LIST_HEAD(&list);
5477 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5478 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5480 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
5481 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
5482 disk_bytenr + len - 1, &list);
5484 while (!list_empty(&list)) {
5485 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5486 list_del_init(&sums->list);
5488 sector_sum = sums->sums;
5489 sums->bytenr = ordered->start;
5492 while (offset < sums->len) {
5493 sector_sum->bytenr += ordered->start - disk_bytenr;
5495 offset += root->sectorsize;
5498 btrfs_add_ordered_sum(inode, ordered, sums);
5500 btrfs_put_ordered_extent(ordered);
5504 int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
5506 struct btrfs_trans_handle *trans;
5507 struct btrfs_path *path;
5508 struct btrfs_fs_info *info = root->fs_info;
5509 struct extent_buffer *leaf;
5510 struct inode *reloc_inode;
5511 struct btrfs_block_group_cache *block_group;
5512 struct btrfs_key key;
5521 root = root->fs_info->extent_root;
5523 block_group = btrfs_lookup_block_group(info, group_start);
5524 BUG_ON(!block_group);
5526 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
5527 (unsigned long long)block_group->key.objectid,
5528 (unsigned long long)block_group->flags);
5530 path = btrfs_alloc_path();
5533 reloc_inode = create_reloc_inode(info, block_group);
5534 BUG_ON(IS_ERR(reloc_inode));
5536 __alloc_chunk_for_shrink(root, block_group, 1);
5537 set_block_group_readonly(block_group);
5539 btrfs_start_delalloc_inodes(info->tree_root);
5540 btrfs_wait_ordered_extents(info->tree_root, 0);
5545 key.objectid = block_group->key.objectid;
5548 cur_byte = key.objectid;
5550 trans = btrfs_start_transaction(info->tree_root, 1);
5551 btrfs_commit_transaction(trans, info->tree_root);
5553 mutex_lock(&root->fs_info->cleaner_mutex);
5554 btrfs_clean_old_snapshots(info->tree_root);
5555 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5556 mutex_unlock(&root->fs_info->cleaner_mutex);
5558 trans = btrfs_start_transaction(info->tree_root, 1);
5559 btrfs_commit_transaction(trans, info->tree_root);
5562 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5566 leaf = path->nodes[0];
5567 nritems = btrfs_header_nritems(leaf);
5568 if (path->slots[0] >= nritems) {
5569 ret = btrfs_next_leaf(root, path);
5576 leaf = path->nodes[0];
5577 nritems = btrfs_header_nritems(leaf);
5580 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5582 if (key.objectid >= block_group->key.objectid +
5583 block_group->key.offset)
5586 if (progress && need_resched()) {
5587 btrfs_release_path(root, path);
5594 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5595 key.objectid + key.offset <= cur_byte) {
5601 cur_byte = key.objectid + key.offset;
5602 btrfs_release_path(root, path);
5604 __alloc_chunk_for_shrink(root, block_group, 0);
5605 ret = relocate_one_extent(root, path, &key, block_group,
5611 key.objectid = cur_byte;
5616 btrfs_release_path(root, path);
5619 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5620 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5623 if (total_found > 0) {
5624 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
5625 (unsigned long long)total_found, pass);
5627 if (total_found == skipped && pass > 2) {
5629 reloc_inode = create_reloc_inode(info, block_group);
5635 /* delete reloc_inode */
5638 /* unpin extents in this range */
5639 trans = btrfs_start_transaction(info->tree_root, 1);
5640 btrfs_commit_transaction(trans, info->tree_root);
5642 spin_lock(&block_group->lock);
5643 WARN_ON(block_group->pinned > 0);
5644 WARN_ON(block_group->reserved > 0);
5645 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5646 spin_unlock(&block_group->lock);
5647 put_block_group(block_group);
5650 btrfs_free_path(path);
5654 static int find_first_block_group(struct btrfs_root *root,
5655 struct btrfs_path *path, struct btrfs_key *key)
5658 struct btrfs_key found_key;
5659 struct extent_buffer *leaf;
5662 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5667 slot = path->slots[0];
5668 leaf = path->nodes[0];
5669 if (slot >= btrfs_header_nritems(leaf)) {
5670 ret = btrfs_next_leaf(root, path);
5677 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5679 if (found_key.objectid >= key->objectid &&
5680 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5691 int btrfs_free_block_groups(struct btrfs_fs_info *info)
5693 struct btrfs_block_group_cache *block_group;
5694 struct btrfs_space_info *space_info;
5697 spin_lock(&info->block_group_cache_lock);
5698 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5699 block_group = rb_entry(n, struct btrfs_block_group_cache,
5701 rb_erase(&block_group->cache_node,
5702 &info->block_group_cache_tree);
5703 spin_unlock(&info->block_group_cache_lock);
5705 btrfs_remove_free_space_cache(block_group);
5706 down_write(&block_group->space_info->groups_sem);
5707 list_del(&block_group->list);
5708 up_write(&block_group->space_info->groups_sem);
5710 WARN_ON(atomic_read(&block_group->count) != 1);
5713 spin_lock(&info->block_group_cache_lock);
5715 spin_unlock(&info->block_group_cache_lock);
5717 /* now that all the block groups are freed, go through and
5718 * free all the space_info structs. This is only called during
5719 * the final stages of unmount, and so we know nobody is
5720 * using them. We call synchronize_rcu() once before we start,
5721 * just to be on the safe side.
5725 while(!list_empty(&info->space_info)) {
5726 space_info = list_entry(info->space_info.next,
5727 struct btrfs_space_info,
5730 list_del(&space_info->list);
5736 int btrfs_read_block_groups(struct btrfs_root *root)
5738 struct btrfs_path *path;
5740 struct btrfs_block_group_cache *cache;
5741 struct btrfs_fs_info *info = root->fs_info;
5742 struct btrfs_space_info *space_info;
5743 struct btrfs_key key;
5744 struct btrfs_key found_key;
5745 struct extent_buffer *leaf;
5747 root = info->extent_root;
5750 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
5751 path = btrfs_alloc_path();
5756 ret = find_first_block_group(root, path, &key);
5764 leaf = path->nodes[0];
5765 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5766 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5772 atomic_set(&cache->count, 1);
5773 spin_lock_init(&cache->lock);
5774 spin_lock_init(&cache->tree_lock);
5775 mutex_init(&cache->cache_mutex);
5776 INIT_LIST_HEAD(&cache->list);
5777 read_extent_buffer(leaf, &cache->item,
5778 btrfs_item_ptr_offset(leaf, path->slots[0]),
5779 sizeof(cache->item));
5780 memcpy(&cache->key, &found_key, sizeof(found_key));
5782 key.objectid = found_key.objectid + found_key.offset;
5783 btrfs_release_path(root, path);
5784 cache->flags = btrfs_block_group_flags(&cache->item);
5786 ret = update_space_info(info, cache->flags, found_key.offset,
5787 btrfs_block_group_used(&cache->item),
5790 cache->space_info = space_info;
5791 down_write(&space_info->groups_sem);
5792 list_add_tail(&cache->list, &space_info->block_groups);
5793 up_write(&space_info->groups_sem);
5795 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5798 set_avail_alloc_bits(root->fs_info, cache->flags);
5799 if (btrfs_chunk_readonly(root, cache->key.objectid))
5800 set_block_group_readonly(cache);
5804 btrfs_free_path(path);
5808 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5809 struct btrfs_root *root, u64 bytes_used,
5810 u64 type, u64 chunk_objectid, u64 chunk_offset,
5814 struct btrfs_root *extent_root;
5815 struct btrfs_block_group_cache *cache;
5817 extent_root = root->fs_info->extent_root;
5819 root->fs_info->last_trans_log_full_commit = trans->transid;
5821 cache = kzalloc(sizeof(*cache), GFP_NOFS);
5825 cache->key.objectid = chunk_offset;
5826 cache->key.offset = size;
5827 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5828 atomic_set(&cache->count, 1);
5829 spin_lock_init(&cache->lock);
5830 spin_lock_init(&cache->tree_lock);
5831 mutex_init(&cache->cache_mutex);
5832 INIT_LIST_HEAD(&cache->list);
5834 btrfs_set_block_group_used(&cache->item, bytes_used);
5835 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5836 cache->flags = type;
5837 btrfs_set_block_group_flags(&cache->item, type);
5839 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5840 &cache->space_info);
5842 down_write(&cache->space_info->groups_sem);
5843 list_add_tail(&cache->list, &cache->space_info->block_groups);
5844 up_write(&cache->space_info->groups_sem);
5846 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5849 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5850 sizeof(cache->item));
5853 set_avail_alloc_bits(extent_root->fs_info, type);
5858 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5859 struct btrfs_root *root, u64 group_start)
5861 struct btrfs_path *path;
5862 struct btrfs_block_group_cache *block_group;
5863 struct btrfs_key key;
5866 root = root->fs_info->extent_root;
5868 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5869 BUG_ON(!block_group);
5870 BUG_ON(!block_group->ro);
5872 memcpy(&key, &block_group->key, sizeof(key));
5874 path = btrfs_alloc_path();
5877 spin_lock(&root->fs_info->block_group_cache_lock);
5878 rb_erase(&block_group->cache_node,
5879 &root->fs_info->block_group_cache_tree);
5880 spin_unlock(&root->fs_info->block_group_cache_lock);
5881 btrfs_remove_free_space_cache(block_group);
5882 down_write(&block_group->space_info->groups_sem);
5883 list_del(&block_group->list);
5884 up_write(&block_group->space_info->groups_sem);
5886 spin_lock(&block_group->space_info->lock);
5887 block_group->space_info->total_bytes -= block_group->key.offset;
5888 block_group->space_info->bytes_readonly -= block_group->key.offset;
5889 spin_unlock(&block_group->space_info->lock);
5890 block_group->space_info->full = 0;
5892 put_block_group(block_group);
5893 put_block_group(block_group);
5895 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5901 ret = btrfs_del_item(trans, root, path);
5903 btrfs_free_path(path);