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>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 /* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
55 static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 bytenr, u64 num_bytes, int alloc);
58 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
77 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
80 static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
82 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
86 block_group_cache_done(struct btrfs_block_group_cache *cache)
89 return cache->cached == BTRFS_CACHE_FINISHED;
92 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
94 return (cache->flags & bits) == bits;
97 static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
99 atomic_inc(&cache->count);
102 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
108 kfree(cache->free_space_ctl);
114 * this adds the block group to the fs_info rb tree for the block group
117 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
118 struct btrfs_block_group_cache *block_group)
121 struct rb_node *parent = NULL;
122 struct btrfs_block_group_cache *cache;
124 spin_lock(&info->block_group_cache_lock);
125 p = &info->block_group_cache_tree.rb_node;
129 cache = rb_entry(parent, struct btrfs_block_group_cache,
131 if (block_group->key.objectid < cache->key.objectid) {
133 } else if (block_group->key.objectid > cache->key.objectid) {
136 spin_unlock(&info->block_group_cache_lock);
141 rb_link_node(&block_group->cache_node, parent, p);
142 rb_insert_color(&block_group->cache_node,
143 &info->block_group_cache_tree);
144 spin_unlock(&info->block_group_cache_lock);
150 * This will return the block group at or after bytenr if contains is 0, else
151 * it will return the block group that contains the bytenr
153 static struct btrfs_block_group_cache *
154 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
157 struct btrfs_block_group_cache *cache, *ret = NULL;
161 spin_lock(&info->block_group_cache_lock);
162 n = info->block_group_cache_tree.rb_node;
165 cache = rb_entry(n, struct btrfs_block_group_cache,
167 end = cache->key.objectid + cache->key.offset - 1;
168 start = cache->key.objectid;
170 if (bytenr < start) {
171 if (!contains && (!ret || start < ret->key.objectid))
174 } else if (bytenr > start) {
175 if (contains && bytenr <= end) {
186 btrfs_get_block_group(ret);
187 spin_unlock(&info->block_group_cache_lock);
192 static int add_excluded_extent(struct btrfs_root *root,
193 u64 start, u64 num_bytes)
195 u64 end = start + num_bytes - 1;
196 set_extent_bits(&root->fs_info->freed_extents[0],
197 start, end, EXTENT_UPTODATE, GFP_NOFS);
198 set_extent_bits(&root->fs_info->freed_extents[1],
199 start, end, EXTENT_UPTODATE, GFP_NOFS);
203 static void free_excluded_extents(struct btrfs_root *root,
204 struct btrfs_block_group_cache *cache)
208 start = cache->key.objectid;
209 end = start + cache->key.offset - 1;
211 clear_extent_bits(&root->fs_info->freed_extents[0],
212 start, end, EXTENT_UPTODATE, GFP_NOFS);
213 clear_extent_bits(&root->fs_info->freed_extents[1],
214 start, end, EXTENT_UPTODATE, GFP_NOFS);
217 static int exclude_super_stripes(struct btrfs_root *root,
218 struct btrfs_block_group_cache *cache)
225 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
226 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
227 cache->bytes_super += stripe_len;
228 ret = add_excluded_extent(root, cache->key.objectid,
233 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
234 bytenr = btrfs_sb_offset(i);
235 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
236 cache->key.objectid, bytenr,
237 0, &logical, &nr, &stripe_len);
241 cache->bytes_super += stripe_len;
242 ret = add_excluded_extent(root, logical[nr],
252 static struct btrfs_caching_control *
253 get_caching_control(struct btrfs_block_group_cache *cache)
255 struct btrfs_caching_control *ctl;
257 spin_lock(&cache->lock);
258 if (cache->cached != BTRFS_CACHE_STARTED) {
259 spin_unlock(&cache->lock);
263 /* We're loading it the fast way, so we don't have a caching_ctl. */
264 if (!cache->caching_ctl) {
265 spin_unlock(&cache->lock);
269 ctl = cache->caching_ctl;
270 atomic_inc(&ctl->count);
271 spin_unlock(&cache->lock);
275 static void put_caching_control(struct btrfs_caching_control *ctl)
277 if (atomic_dec_and_test(&ctl->count))
282 * this is only called by cache_block_group, since we could have freed extents
283 * we need to check the pinned_extents for any extents that can't be used yet
284 * since their free space will be released as soon as the transaction commits.
286 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
287 struct btrfs_fs_info *info, u64 start, u64 end)
289 u64 extent_start, extent_end, size, total_added = 0;
292 while (start < end) {
293 ret = find_first_extent_bit(info->pinned_extents, start,
294 &extent_start, &extent_end,
295 EXTENT_DIRTY | EXTENT_UPTODATE);
299 if (extent_start <= start) {
300 start = extent_end + 1;
301 } else if (extent_start > start && extent_start < end) {
302 size = extent_start - start;
304 ret = btrfs_add_free_space(block_group, start,
307 start = extent_end + 1;
316 ret = btrfs_add_free_space(block_group, start, size);
323 static int caching_kthread(void *data)
325 struct btrfs_block_group_cache *block_group = data;
326 struct btrfs_fs_info *fs_info = block_group->fs_info;
327 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
328 struct btrfs_root *extent_root = fs_info->extent_root;
329 struct btrfs_path *path;
330 struct extent_buffer *leaf;
331 struct btrfs_key key;
337 path = btrfs_alloc_path();
341 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
344 * We don't want to deadlock with somebody trying to allocate a new
345 * extent for the extent root while also trying to search the extent
346 * root to add free space. So we skip locking and search the commit
347 * root, since its read-only
349 path->skip_locking = 1;
350 path->search_commit_root = 1;
355 key.type = BTRFS_EXTENT_ITEM_KEY;
357 mutex_lock(&caching_ctl->mutex);
358 /* need to make sure the commit_root doesn't disappear */
359 down_read(&fs_info->extent_commit_sem);
361 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
365 leaf = path->nodes[0];
366 nritems = btrfs_header_nritems(leaf);
370 if (fs_info->closing > 1) {
375 if (path->slots[0] < nritems) {
376 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
378 ret = find_next_key(path, 0, &key);
382 caching_ctl->progress = last;
383 btrfs_release_path(path);
384 up_read(&fs_info->extent_commit_sem);
385 mutex_unlock(&caching_ctl->mutex);
386 if (btrfs_transaction_in_commit(fs_info))
393 if (key.objectid < block_group->key.objectid) {
398 if (key.objectid >= block_group->key.objectid +
399 block_group->key.offset)
402 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
403 total_found += add_new_free_space(block_group,
406 last = key.objectid + key.offset;
408 if (total_found > (1024 * 1024 * 2)) {
410 wake_up(&caching_ctl->wait);
417 total_found += add_new_free_space(block_group, fs_info, last,
418 block_group->key.objectid +
419 block_group->key.offset);
420 caching_ctl->progress = (u64)-1;
422 spin_lock(&block_group->lock);
423 block_group->caching_ctl = NULL;
424 block_group->cached = BTRFS_CACHE_FINISHED;
425 spin_unlock(&block_group->lock);
428 btrfs_free_path(path);
429 up_read(&fs_info->extent_commit_sem);
431 free_excluded_extents(extent_root, block_group);
433 mutex_unlock(&caching_ctl->mutex);
434 wake_up(&caching_ctl->wait);
436 put_caching_control(caching_ctl);
437 atomic_dec(&block_group->space_info->caching_threads);
438 btrfs_put_block_group(block_group);
443 static int cache_block_group(struct btrfs_block_group_cache *cache,
444 struct btrfs_trans_handle *trans,
445 struct btrfs_root *root,
448 struct btrfs_fs_info *fs_info = cache->fs_info;
449 struct btrfs_caching_control *caching_ctl;
450 struct task_struct *tsk;
454 if (cache->cached != BTRFS_CACHE_NO)
458 * We can't do the read from on-disk cache during a commit since we need
459 * to have the normal tree locking. Also if we are currently trying to
460 * allocate blocks for the tree root we can't do the fast caching since
461 * we likely hold important locks.
463 if (trans && (!trans->transaction->in_commit) &&
464 (root && root != root->fs_info->tree_root)) {
465 spin_lock(&cache->lock);
466 if (cache->cached != BTRFS_CACHE_NO) {
467 spin_unlock(&cache->lock);
470 cache->cached = BTRFS_CACHE_STARTED;
471 spin_unlock(&cache->lock);
473 ret = load_free_space_cache(fs_info, cache);
475 spin_lock(&cache->lock);
477 cache->cached = BTRFS_CACHE_FINISHED;
478 cache->last_byte_to_unpin = (u64)-1;
480 cache->cached = BTRFS_CACHE_NO;
482 spin_unlock(&cache->lock);
484 free_excluded_extents(fs_info->extent_root, cache);
492 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
493 BUG_ON(!caching_ctl);
495 INIT_LIST_HEAD(&caching_ctl->list);
496 mutex_init(&caching_ctl->mutex);
497 init_waitqueue_head(&caching_ctl->wait);
498 caching_ctl->block_group = cache;
499 caching_ctl->progress = cache->key.objectid;
500 /* one for caching kthread, one for caching block group list */
501 atomic_set(&caching_ctl->count, 2);
503 spin_lock(&cache->lock);
504 if (cache->cached != BTRFS_CACHE_NO) {
505 spin_unlock(&cache->lock);
509 cache->caching_ctl = caching_ctl;
510 cache->cached = BTRFS_CACHE_STARTED;
511 spin_unlock(&cache->lock);
513 down_write(&fs_info->extent_commit_sem);
514 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
515 up_write(&fs_info->extent_commit_sem);
517 atomic_inc(&cache->space_info->caching_threads);
518 btrfs_get_block_group(cache);
520 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
521 cache->key.objectid);
524 printk(KERN_ERR "error running thread %d\n", ret);
532 * return the block group that starts at or after bytenr
534 static struct btrfs_block_group_cache *
535 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
537 struct btrfs_block_group_cache *cache;
539 cache = block_group_cache_tree_search(info, bytenr, 0);
545 * return the block group that contains the given bytenr
547 struct btrfs_block_group_cache *btrfs_lookup_block_group(
548 struct btrfs_fs_info *info,
551 struct btrfs_block_group_cache *cache;
553 cache = block_group_cache_tree_search(info, bytenr, 1);
558 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
561 struct list_head *head = &info->space_info;
562 struct btrfs_space_info *found;
564 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
565 BTRFS_BLOCK_GROUP_METADATA;
568 list_for_each_entry_rcu(found, head, list) {
569 if (found->flags & flags) {
579 * after adding space to the filesystem, we need to clear the full flags
580 * on all the space infos.
582 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
584 struct list_head *head = &info->space_info;
585 struct btrfs_space_info *found;
588 list_for_each_entry_rcu(found, head, list)
593 static u64 div_factor(u64 num, int factor)
602 static u64 div_factor_fine(u64 num, int factor)
611 u64 btrfs_find_block_group(struct btrfs_root *root,
612 u64 search_start, u64 search_hint, int owner)
614 struct btrfs_block_group_cache *cache;
616 u64 last = max(search_hint, search_start);
623 cache = btrfs_lookup_first_block_group(root->fs_info, last);
627 spin_lock(&cache->lock);
628 last = cache->key.objectid + cache->key.offset;
629 used = btrfs_block_group_used(&cache->item);
631 if ((full_search || !cache->ro) &&
632 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
633 if (used + cache->pinned + cache->reserved <
634 div_factor(cache->key.offset, factor)) {
635 group_start = cache->key.objectid;
636 spin_unlock(&cache->lock);
637 btrfs_put_block_group(cache);
641 spin_unlock(&cache->lock);
642 btrfs_put_block_group(cache);
650 if (!full_search && factor < 10) {
660 /* simple helper to search for an existing extent at a given offset */
661 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
664 struct btrfs_key key;
665 struct btrfs_path *path;
667 path = btrfs_alloc_path();
669 key.objectid = start;
671 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
672 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
674 btrfs_free_path(path);
679 * helper function to lookup reference count and flags of extent.
681 * the head node for delayed ref is used to store the sum of all the
682 * reference count modifications queued up in the rbtree. the head
683 * node may also store the extent flags to set. This way you can check
684 * to see what the reference count and extent flags would be if all of
685 * the delayed refs are not processed.
687 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
688 struct btrfs_root *root, u64 bytenr,
689 u64 num_bytes, u64 *refs, u64 *flags)
691 struct btrfs_delayed_ref_head *head;
692 struct btrfs_delayed_ref_root *delayed_refs;
693 struct btrfs_path *path;
694 struct btrfs_extent_item *ei;
695 struct extent_buffer *leaf;
696 struct btrfs_key key;
702 path = btrfs_alloc_path();
706 key.objectid = bytenr;
707 key.type = BTRFS_EXTENT_ITEM_KEY;
708 key.offset = num_bytes;
710 path->skip_locking = 1;
711 path->search_commit_root = 1;
714 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
720 leaf = path->nodes[0];
721 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
722 if (item_size >= sizeof(*ei)) {
723 ei = btrfs_item_ptr(leaf, path->slots[0],
724 struct btrfs_extent_item);
725 num_refs = btrfs_extent_refs(leaf, ei);
726 extent_flags = btrfs_extent_flags(leaf, ei);
728 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
729 struct btrfs_extent_item_v0 *ei0;
730 BUG_ON(item_size != sizeof(*ei0));
731 ei0 = btrfs_item_ptr(leaf, path->slots[0],
732 struct btrfs_extent_item_v0);
733 num_refs = btrfs_extent_refs_v0(leaf, ei0);
734 /* FIXME: this isn't correct for data */
735 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
740 BUG_ON(num_refs == 0);
750 delayed_refs = &trans->transaction->delayed_refs;
751 spin_lock(&delayed_refs->lock);
752 head = btrfs_find_delayed_ref_head(trans, bytenr);
754 if (!mutex_trylock(&head->mutex)) {
755 atomic_inc(&head->node.refs);
756 spin_unlock(&delayed_refs->lock);
758 btrfs_release_path(path);
761 * Mutex was contended, block until it's released and try
764 mutex_lock(&head->mutex);
765 mutex_unlock(&head->mutex);
766 btrfs_put_delayed_ref(&head->node);
769 if (head->extent_op && head->extent_op->update_flags)
770 extent_flags |= head->extent_op->flags_to_set;
772 BUG_ON(num_refs == 0);
774 num_refs += head->node.ref_mod;
775 mutex_unlock(&head->mutex);
777 spin_unlock(&delayed_refs->lock);
779 WARN_ON(num_refs == 0);
783 *flags = extent_flags;
785 btrfs_free_path(path);
790 * Back reference rules. Back refs have three main goals:
792 * 1) differentiate between all holders of references to an extent so that
793 * when a reference is dropped we can make sure it was a valid reference
794 * before freeing the extent.
796 * 2) Provide enough information to quickly find the holders of an extent
797 * if we notice a given block is corrupted or bad.
799 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
800 * maintenance. This is actually the same as #2, but with a slightly
801 * different use case.
803 * There are two kinds of back refs. The implicit back refs is optimized
804 * for pointers in non-shared tree blocks. For a given pointer in a block,
805 * back refs of this kind provide information about the block's owner tree
806 * and the pointer's key. These information allow us to find the block by
807 * b-tree searching. The full back refs is for pointers in tree blocks not
808 * referenced by their owner trees. The location of tree block is recorded
809 * in the back refs. Actually the full back refs is generic, and can be
810 * used in all cases the implicit back refs is used. The major shortcoming
811 * of the full back refs is its overhead. Every time a tree block gets
812 * COWed, we have to update back refs entry for all pointers in it.
814 * For a newly allocated tree block, we use implicit back refs for
815 * pointers in it. This means most tree related operations only involve
816 * implicit back refs. For a tree block created in old transaction, the
817 * only way to drop a reference to it is COW it. So we can detect the
818 * event that tree block loses its owner tree's reference and do the
819 * back refs conversion.
821 * When a tree block is COW'd through a tree, there are four cases:
823 * The reference count of the block is one and the tree is the block's
824 * owner tree. Nothing to do in this case.
826 * The reference count of the block is one and the tree is not the
827 * block's owner tree. In this case, full back refs is used for pointers
828 * in the block. Remove these full back refs, add implicit back refs for
829 * every pointers in the new block.
831 * The reference count of the block is greater than one and the tree is
832 * the block's owner tree. In this case, implicit back refs is used for
833 * pointers in the block. Add full back refs for every pointers in the
834 * block, increase lower level extents' reference counts. The original
835 * implicit back refs are entailed to the new block.
837 * The reference count of the block is greater than one and the tree is
838 * not the block's owner tree. Add implicit back refs for every pointer in
839 * the new block, increase lower level extents' reference count.
841 * Back Reference Key composing:
843 * The key objectid corresponds to the first byte in the extent,
844 * The key type is used to differentiate between types of back refs.
845 * There are different meanings of the key offset for different types
848 * File extents can be referenced by:
850 * - multiple snapshots, subvolumes, or different generations in one subvol
851 * - different files inside a single subvolume
852 * - different offsets inside a file (bookend extents in file.c)
854 * The extent ref structure for the implicit back refs has fields for:
856 * - Objectid of the subvolume root
857 * - objectid of the file holding the reference
858 * - original offset in the file
859 * - how many bookend extents
861 * The key offset for the implicit back refs is hash of the first
864 * The extent ref structure for the full back refs has field for:
866 * - number of pointers in the tree leaf
868 * The key offset for the implicit back refs is the first byte of
871 * When a file extent is allocated, The implicit back refs is used.
872 * the fields are filled in:
874 * (root_key.objectid, inode objectid, offset in file, 1)
876 * When a file extent is removed file truncation, we find the
877 * corresponding implicit back refs and check the following fields:
879 * (btrfs_header_owner(leaf), inode objectid, offset in file)
881 * Btree extents can be referenced by:
883 * - Different subvolumes
885 * Both the implicit back refs and the full back refs for tree blocks
886 * only consist of key. The key offset for the implicit back refs is
887 * objectid of block's owner tree. The key offset for the full back refs
888 * is the first byte of parent block.
890 * When implicit back refs is used, information about the lowest key and
891 * level of the tree block are required. These information are stored in
892 * tree block info structure.
895 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
896 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
897 struct btrfs_root *root,
898 struct btrfs_path *path,
899 u64 owner, u32 extra_size)
901 struct btrfs_extent_item *item;
902 struct btrfs_extent_item_v0 *ei0;
903 struct btrfs_extent_ref_v0 *ref0;
904 struct btrfs_tree_block_info *bi;
905 struct extent_buffer *leaf;
906 struct btrfs_key key;
907 struct btrfs_key found_key;
908 u32 new_size = sizeof(*item);
912 leaf = path->nodes[0];
913 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
915 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
916 ei0 = btrfs_item_ptr(leaf, path->slots[0],
917 struct btrfs_extent_item_v0);
918 refs = btrfs_extent_refs_v0(leaf, ei0);
920 if (owner == (u64)-1) {
922 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
923 ret = btrfs_next_leaf(root, path);
927 leaf = path->nodes[0];
929 btrfs_item_key_to_cpu(leaf, &found_key,
931 BUG_ON(key.objectid != found_key.objectid);
932 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
936 ref0 = btrfs_item_ptr(leaf, path->slots[0],
937 struct btrfs_extent_ref_v0);
938 owner = btrfs_ref_objectid_v0(leaf, ref0);
942 btrfs_release_path(path);
944 if (owner < BTRFS_FIRST_FREE_OBJECTID)
945 new_size += sizeof(*bi);
947 new_size -= sizeof(*ei0);
948 ret = btrfs_search_slot(trans, root, &key, path,
949 new_size + extra_size, 1);
954 ret = btrfs_extend_item(trans, root, path, new_size);
957 leaf = path->nodes[0];
958 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
959 btrfs_set_extent_refs(leaf, item, refs);
960 /* FIXME: get real generation */
961 btrfs_set_extent_generation(leaf, item, 0);
962 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
963 btrfs_set_extent_flags(leaf, item,
964 BTRFS_EXTENT_FLAG_TREE_BLOCK |
965 BTRFS_BLOCK_FLAG_FULL_BACKREF);
966 bi = (struct btrfs_tree_block_info *)(item + 1);
967 /* FIXME: get first key of the block */
968 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
969 btrfs_set_tree_block_level(leaf, bi, (int)owner);
971 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
973 btrfs_mark_buffer_dirty(leaf);
978 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
980 u32 high_crc = ~(u32)0;
981 u32 low_crc = ~(u32)0;
984 lenum = cpu_to_le64(root_objectid);
985 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
986 lenum = cpu_to_le64(owner);
987 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
988 lenum = cpu_to_le64(offset);
989 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
991 return ((u64)high_crc << 31) ^ (u64)low_crc;
994 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
995 struct btrfs_extent_data_ref *ref)
997 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
998 btrfs_extent_data_ref_objectid(leaf, ref),
999 btrfs_extent_data_ref_offset(leaf, ref));
1002 static int match_extent_data_ref(struct extent_buffer *leaf,
1003 struct btrfs_extent_data_ref *ref,
1004 u64 root_objectid, u64 owner, u64 offset)
1006 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1007 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1008 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1013 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1014 struct btrfs_root *root,
1015 struct btrfs_path *path,
1016 u64 bytenr, u64 parent,
1018 u64 owner, u64 offset)
1020 struct btrfs_key key;
1021 struct btrfs_extent_data_ref *ref;
1022 struct extent_buffer *leaf;
1028 key.objectid = bytenr;
1030 key.type = BTRFS_SHARED_DATA_REF_KEY;
1031 key.offset = parent;
1033 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1034 key.offset = hash_extent_data_ref(root_objectid,
1039 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1048 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1049 key.type = BTRFS_EXTENT_REF_V0_KEY;
1050 btrfs_release_path(path);
1051 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1062 leaf = path->nodes[0];
1063 nritems = btrfs_header_nritems(leaf);
1065 if (path->slots[0] >= nritems) {
1066 ret = btrfs_next_leaf(root, path);
1072 leaf = path->nodes[0];
1073 nritems = btrfs_header_nritems(leaf);
1077 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1078 if (key.objectid != bytenr ||
1079 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1082 ref = btrfs_item_ptr(leaf, path->slots[0],
1083 struct btrfs_extent_data_ref);
1085 if (match_extent_data_ref(leaf, ref, root_objectid,
1088 btrfs_release_path(path);
1100 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1101 struct btrfs_root *root,
1102 struct btrfs_path *path,
1103 u64 bytenr, u64 parent,
1104 u64 root_objectid, u64 owner,
1105 u64 offset, int refs_to_add)
1107 struct btrfs_key key;
1108 struct extent_buffer *leaf;
1113 key.objectid = bytenr;
1115 key.type = BTRFS_SHARED_DATA_REF_KEY;
1116 key.offset = parent;
1117 size = sizeof(struct btrfs_shared_data_ref);
1119 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1120 key.offset = hash_extent_data_ref(root_objectid,
1122 size = sizeof(struct btrfs_extent_data_ref);
1125 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1126 if (ret && ret != -EEXIST)
1129 leaf = path->nodes[0];
1131 struct btrfs_shared_data_ref *ref;
1132 ref = btrfs_item_ptr(leaf, path->slots[0],
1133 struct btrfs_shared_data_ref);
1135 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1137 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1138 num_refs += refs_to_add;
1139 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1142 struct btrfs_extent_data_ref *ref;
1143 while (ret == -EEXIST) {
1144 ref = btrfs_item_ptr(leaf, path->slots[0],
1145 struct btrfs_extent_data_ref);
1146 if (match_extent_data_ref(leaf, ref, root_objectid,
1149 btrfs_release_path(path);
1151 ret = btrfs_insert_empty_item(trans, root, path, &key,
1153 if (ret && ret != -EEXIST)
1156 leaf = path->nodes[0];
1158 ref = btrfs_item_ptr(leaf, path->slots[0],
1159 struct btrfs_extent_data_ref);
1161 btrfs_set_extent_data_ref_root(leaf, ref,
1163 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1164 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1165 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1167 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1168 num_refs += refs_to_add;
1169 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1172 btrfs_mark_buffer_dirty(leaf);
1175 btrfs_release_path(path);
1179 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1180 struct btrfs_root *root,
1181 struct btrfs_path *path,
1184 struct btrfs_key key;
1185 struct btrfs_extent_data_ref *ref1 = NULL;
1186 struct btrfs_shared_data_ref *ref2 = NULL;
1187 struct extent_buffer *leaf;
1191 leaf = path->nodes[0];
1192 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1194 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1195 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1196 struct btrfs_extent_data_ref);
1197 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1198 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1199 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1200 struct btrfs_shared_data_ref);
1201 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1202 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1203 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1204 struct btrfs_extent_ref_v0 *ref0;
1205 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1206 struct btrfs_extent_ref_v0);
1207 num_refs = btrfs_ref_count_v0(leaf, ref0);
1213 BUG_ON(num_refs < refs_to_drop);
1214 num_refs -= refs_to_drop;
1216 if (num_refs == 0) {
1217 ret = btrfs_del_item(trans, root, path);
1219 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1220 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1221 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1222 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1223 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1225 struct btrfs_extent_ref_v0 *ref0;
1226 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1227 struct btrfs_extent_ref_v0);
1228 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1231 btrfs_mark_buffer_dirty(leaf);
1236 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1237 struct btrfs_path *path,
1238 struct btrfs_extent_inline_ref *iref)
1240 struct btrfs_key key;
1241 struct extent_buffer *leaf;
1242 struct btrfs_extent_data_ref *ref1;
1243 struct btrfs_shared_data_ref *ref2;
1246 leaf = path->nodes[0];
1247 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1249 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1250 BTRFS_EXTENT_DATA_REF_KEY) {
1251 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1252 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1254 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1255 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1257 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1258 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_extent_data_ref);
1260 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1261 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1262 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1263 struct btrfs_shared_data_ref);
1264 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1265 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1266 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1267 struct btrfs_extent_ref_v0 *ref0;
1268 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1269 struct btrfs_extent_ref_v0);
1270 num_refs = btrfs_ref_count_v0(leaf, ref0);
1278 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1279 struct btrfs_root *root,
1280 struct btrfs_path *path,
1281 u64 bytenr, u64 parent,
1284 struct btrfs_key key;
1287 key.objectid = bytenr;
1289 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1290 key.offset = parent;
1292 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1293 key.offset = root_objectid;
1296 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1299 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1300 if (ret == -ENOENT && parent) {
1301 btrfs_release_path(path);
1302 key.type = BTRFS_EXTENT_REF_V0_KEY;
1303 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1311 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1312 struct btrfs_root *root,
1313 struct btrfs_path *path,
1314 u64 bytenr, u64 parent,
1317 struct btrfs_key key;
1320 key.objectid = bytenr;
1322 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1323 key.offset = parent;
1325 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1326 key.offset = root_objectid;
1329 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1330 btrfs_release_path(path);
1334 static inline int extent_ref_type(u64 parent, u64 owner)
1337 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1339 type = BTRFS_SHARED_BLOCK_REF_KEY;
1341 type = BTRFS_TREE_BLOCK_REF_KEY;
1344 type = BTRFS_SHARED_DATA_REF_KEY;
1346 type = BTRFS_EXTENT_DATA_REF_KEY;
1351 static int find_next_key(struct btrfs_path *path, int level,
1352 struct btrfs_key *key)
1355 for (; level < BTRFS_MAX_LEVEL; level++) {
1356 if (!path->nodes[level])
1358 if (path->slots[level] + 1 >=
1359 btrfs_header_nritems(path->nodes[level]))
1362 btrfs_item_key_to_cpu(path->nodes[level], key,
1363 path->slots[level] + 1);
1365 btrfs_node_key_to_cpu(path->nodes[level], key,
1366 path->slots[level] + 1);
1373 * look for inline back ref. if back ref is found, *ref_ret is set
1374 * to the address of inline back ref, and 0 is returned.
1376 * if back ref isn't found, *ref_ret is set to the address where it
1377 * should be inserted, and -ENOENT is returned.
1379 * if insert is true and there are too many inline back refs, the path
1380 * points to the extent item, and -EAGAIN is returned.
1382 * NOTE: inline back refs are ordered in the same way that back ref
1383 * items in the tree are ordered.
1385 static noinline_for_stack
1386 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1387 struct btrfs_root *root,
1388 struct btrfs_path *path,
1389 struct btrfs_extent_inline_ref **ref_ret,
1390 u64 bytenr, u64 num_bytes,
1391 u64 parent, u64 root_objectid,
1392 u64 owner, u64 offset, int insert)
1394 struct btrfs_key key;
1395 struct extent_buffer *leaf;
1396 struct btrfs_extent_item *ei;
1397 struct btrfs_extent_inline_ref *iref;
1408 key.objectid = bytenr;
1409 key.type = BTRFS_EXTENT_ITEM_KEY;
1410 key.offset = num_bytes;
1412 want = extent_ref_type(parent, owner);
1414 extra_size = btrfs_extent_inline_ref_size(want);
1415 path->keep_locks = 1;
1418 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1425 leaf = path->nodes[0];
1426 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1427 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1428 if (item_size < sizeof(*ei)) {
1433 ret = convert_extent_item_v0(trans, root, path, owner,
1439 leaf = path->nodes[0];
1440 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1443 BUG_ON(item_size < sizeof(*ei));
1445 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1446 flags = btrfs_extent_flags(leaf, ei);
1448 ptr = (unsigned long)(ei + 1);
1449 end = (unsigned long)ei + item_size;
1451 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1452 ptr += sizeof(struct btrfs_tree_block_info);
1455 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1464 iref = (struct btrfs_extent_inline_ref *)ptr;
1465 type = btrfs_extent_inline_ref_type(leaf, iref);
1469 ptr += btrfs_extent_inline_ref_size(type);
1473 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1474 struct btrfs_extent_data_ref *dref;
1475 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1476 if (match_extent_data_ref(leaf, dref, root_objectid,
1481 if (hash_extent_data_ref_item(leaf, dref) <
1482 hash_extent_data_ref(root_objectid, owner, offset))
1486 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1488 if (parent == ref_offset) {
1492 if (ref_offset < parent)
1495 if (root_objectid == ref_offset) {
1499 if (ref_offset < root_objectid)
1503 ptr += btrfs_extent_inline_ref_size(type);
1505 if (err == -ENOENT && insert) {
1506 if (item_size + extra_size >=
1507 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1512 * To add new inline back ref, we have to make sure
1513 * there is no corresponding back ref item.
1514 * For simplicity, we just do not add new inline back
1515 * ref if there is any kind of item for this block
1517 if (find_next_key(path, 0, &key) == 0 &&
1518 key.objectid == bytenr &&
1519 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1524 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1527 path->keep_locks = 0;
1528 btrfs_unlock_up_safe(path, 1);
1534 * helper to add new inline back ref
1536 static noinline_for_stack
1537 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1538 struct btrfs_root *root,
1539 struct btrfs_path *path,
1540 struct btrfs_extent_inline_ref *iref,
1541 u64 parent, u64 root_objectid,
1542 u64 owner, u64 offset, int refs_to_add,
1543 struct btrfs_delayed_extent_op *extent_op)
1545 struct extent_buffer *leaf;
1546 struct btrfs_extent_item *ei;
1549 unsigned long item_offset;
1555 leaf = path->nodes[0];
1556 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1557 item_offset = (unsigned long)iref - (unsigned long)ei;
1559 type = extent_ref_type(parent, owner);
1560 size = btrfs_extent_inline_ref_size(type);
1562 ret = btrfs_extend_item(trans, root, path, size);
1565 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1566 refs = btrfs_extent_refs(leaf, ei);
1567 refs += refs_to_add;
1568 btrfs_set_extent_refs(leaf, ei, refs);
1570 __run_delayed_extent_op(extent_op, leaf, ei);
1572 ptr = (unsigned long)ei + item_offset;
1573 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1574 if (ptr < end - size)
1575 memmove_extent_buffer(leaf, ptr + size, ptr,
1578 iref = (struct btrfs_extent_inline_ref *)ptr;
1579 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1580 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1581 struct btrfs_extent_data_ref *dref;
1582 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1583 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1584 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1585 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1586 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1587 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1588 struct btrfs_shared_data_ref *sref;
1589 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1590 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1592 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1593 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1595 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1597 btrfs_mark_buffer_dirty(leaf);
1601 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1602 struct btrfs_root *root,
1603 struct btrfs_path *path,
1604 struct btrfs_extent_inline_ref **ref_ret,
1605 u64 bytenr, u64 num_bytes, u64 parent,
1606 u64 root_objectid, u64 owner, u64 offset)
1610 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1611 bytenr, num_bytes, parent,
1612 root_objectid, owner, offset, 0);
1616 btrfs_release_path(path);
1619 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1620 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1623 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1624 root_objectid, owner, offset);
1630 * helper to update/remove inline back ref
1632 static noinline_for_stack
1633 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1634 struct btrfs_root *root,
1635 struct btrfs_path *path,
1636 struct btrfs_extent_inline_ref *iref,
1638 struct btrfs_delayed_extent_op *extent_op)
1640 struct extent_buffer *leaf;
1641 struct btrfs_extent_item *ei;
1642 struct btrfs_extent_data_ref *dref = NULL;
1643 struct btrfs_shared_data_ref *sref = NULL;
1652 leaf = path->nodes[0];
1653 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1654 refs = btrfs_extent_refs(leaf, ei);
1655 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1656 refs += refs_to_mod;
1657 btrfs_set_extent_refs(leaf, ei, refs);
1659 __run_delayed_extent_op(extent_op, leaf, ei);
1661 type = btrfs_extent_inline_ref_type(leaf, iref);
1663 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1664 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1665 refs = btrfs_extent_data_ref_count(leaf, dref);
1666 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1667 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1668 refs = btrfs_shared_data_ref_count(leaf, sref);
1671 BUG_ON(refs_to_mod != -1);
1674 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1675 refs += refs_to_mod;
1678 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1679 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1681 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1683 size = btrfs_extent_inline_ref_size(type);
1684 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1685 ptr = (unsigned long)iref;
1686 end = (unsigned long)ei + item_size;
1687 if (ptr + size < end)
1688 memmove_extent_buffer(leaf, ptr, ptr + size,
1691 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1694 btrfs_mark_buffer_dirty(leaf);
1698 static noinline_for_stack
1699 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
1701 struct btrfs_path *path,
1702 u64 bytenr, u64 num_bytes, u64 parent,
1703 u64 root_objectid, u64 owner,
1704 u64 offset, int refs_to_add,
1705 struct btrfs_delayed_extent_op *extent_op)
1707 struct btrfs_extent_inline_ref *iref;
1710 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1711 bytenr, num_bytes, parent,
1712 root_objectid, owner, offset, 1);
1714 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1715 ret = update_inline_extent_backref(trans, root, path, iref,
1716 refs_to_add, extent_op);
1717 } else if (ret == -ENOENT) {
1718 ret = setup_inline_extent_backref(trans, root, path, iref,
1719 parent, root_objectid,
1720 owner, offset, refs_to_add,
1726 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1727 struct btrfs_root *root,
1728 struct btrfs_path *path,
1729 u64 bytenr, u64 parent, u64 root_objectid,
1730 u64 owner, u64 offset, int refs_to_add)
1733 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1734 BUG_ON(refs_to_add != 1);
1735 ret = insert_tree_block_ref(trans, root, path, bytenr,
1736 parent, root_objectid);
1738 ret = insert_extent_data_ref(trans, root, path, bytenr,
1739 parent, root_objectid,
1740 owner, offset, refs_to_add);
1745 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1746 struct btrfs_root *root,
1747 struct btrfs_path *path,
1748 struct btrfs_extent_inline_ref *iref,
1749 int refs_to_drop, int is_data)
1753 BUG_ON(!is_data && refs_to_drop != 1);
1755 ret = update_inline_extent_backref(trans, root, path, iref,
1756 -refs_to_drop, NULL);
1757 } else if (is_data) {
1758 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1760 ret = btrfs_del_item(trans, root, path);
1765 static int btrfs_issue_discard(struct block_device *bdev,
1768 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
1771 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1772 u64 num_bytes, u64 *actual_bytes)
1775 u64 discarded_bytes = 0;
1776 struct btrfs_multi_bio *multi = NULL;
1779 /* Tell the block device(s) that the sectors can be discarded */
1780 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1781 bytenr, &num_bytes, &multi, 0);
1783 struct btrfs_bio_stripe *stripe = multi->stripes;
1787 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1788 ret = btrfs_issue_discard(stripe->dev->bdev,
1792 discarded_bytes += stripe->length;
1793 else if (ret != -EOPNOTSUPP)
1798 if (discarded_bytes && ret == -EOPNOTSUPP)
1802 *actual_bytes = discarded_bytes;
1808 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1809 struct btrfs_root *root,
1810 u64 bytenr, u64 num_bytes, u64 parent,
1811 u64 root_objectid, u64 owner, u64 offset)
1814 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1815 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1817 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1818 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1819 parent, root_objectid, (int)owner,
1820 BTRFS_ADD_DELAYED_REF, NULL);
1822 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1823 parent, root_objectid, owner, offset,
1824 BTRFS_ADD_DELAYED_REF, NULL);
1829 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1830 struct btrfs_root *root,
1831 u64 bytenr, u64 num_bytes,
1832 u64 parent, u64 root_objectid,
1833 u64 owner, u64 offset, int refs_to_add,
1834 struct btrfs_delayed_extent_op *extent_op)
1836 struct btrfs_path *path;
1837 struct extent_buffer *leaf;
1838 struct btrfs_extent_item *item;
1843 path = btrfs_alloc_path();
1848 path->leave_spinning = 1;
1849 /* this will setup the path even if it fails to insert the back ref */
1850 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1851 path, bytenr, num_bytes, parent,
1852 root_objectid, owner, offset,
1853 refs_to_add, extent_op);
1857 if (ret != -EAGAIN) {
1862 leaf = path->nodes[0];
1863 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1864 refs = btrfs_extent_refs(leaf, item);
1865 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1867 __run_delayed_extent_op(extent_op, leaf, item);
1869 btrfs_mark_buffer_dirty(leaf);
1870 btrfs_release_path(path);
1873 path->leave_spinning = 1;
1875 /* now insert the actual backref */
1876 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1877 path, bytenr, parent, root_objectid,
1878 owner, offset, refs_to_add);
1881 btrfs_free_path(path);
1885 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1886 struct btrfs_root *root,
1887 struct btrfs_delayed_ref_node *node,
1888 struct btrfs_delayed_extent_op *extent_op,
1889 int insert_reserved)
1892 struct btrfs_delayed_data_ref *ref;
1893 struct btrfs_key ins;
1898 ins.objectid = node->bytenr;
1899 ins.offset = node->num_bytes;
1900 ins.type = BTRFS_EXTENT_ITEM_KEY;
1902 ref = btrfs_delayed_node_to_data_ref(node);
1903 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1904 parent = ref->parent;
1906 ref_root = ref->root;
1908 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1910 BUG_ON(extent_op->update_key);
1911 flags |= extent_op->flags_to_set;
1913 ret = alloc_reserved_file_extent(trans, root,
1914 parent, ref_root, flags,
1915 ref->objectid, ref->offset,
1916 &ins, node->ref_mod);
1917 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1918 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1919 node->num_bytes, parent,
1920 ref_root, ref->objectid,
1921 ref->offset, node->ref_mod,
1923 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1924 ret = __btrfs_free_extent(trans, root, node->bytenr,
1925 node->num_bytes, parent,
1926 ref_root, ref->objectid,
1927 ref->offset, node->ref_mod,
1935 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1936 struct extent_buffer *leaf,
1937 struct btrfs_extent_item *ei)
1939 u64 flags = btrfs_extent_flags(leaf, ei);
1940 if (extent_op->update_flags) {
1941 flags |= extent_op->flags_to_set;
1942 btrfs_set_extent_flags(leaf, ei, flags);
1945 if (extent_op->update_key) {
1946 struct btrfs_tree_block_info *bi;
1947 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1948 bi = (struct btrfs_tree_block_info *)(ei + 1);
1949 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1953 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1954 struct btrfs_root *root,
1955 struct btrfs_delayed_ref_node *node,
1956 struct btrfs_delayed_extent_op *extent_op)
1958 struct btrfs_key key;
1959 struct btrfs_path *path;
1960 struct btrfs_extent_item *ei;
1961 struct extent_buffer *leaf;
1966 path = btrfs_alloc_path();
1970 key.objectid = node->bytenr;
1971 key.type = BTRFS_EXTENT_ITEM_KEY;
1972 key.offset = node->num_bytes;
1975 path->leave_spinning = 1;
1976 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1987 leaf = path->nodes[0];
1988 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1989 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1990 if (item_size < sizeof(*ei)) {
1991 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1997 leaf = path->nodes[0];
1998 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2001 BUG_ON(item_size < sizeof(*ei));
2002 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2003 __run_delayed_extent_op(extent_op, leaf, ei);
2005 btrfs_mark_buffer_dirty(leaf);
2007 btrfs_free_path(path);
2011 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2012 struct btrfs_root *root,
2013 struct btrfs_delayed_ref_node *node,
2014 struct btrfs_delayed_extent_op *extent_op,
2015 int insert_reserved)
2018 struct btrfs_delayed_tree_ref *ref;
2019 struct btrfs_key ins;
2023 ins.objectid = node->bytenr;
2024 ins.offset = node->num_bytes;
2025 ins.type = BTRFS_EXTENT_ITEM_KEY;
2027 ref = btrfs_delayed_node_to_tree_ref(node);
2028 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2029 parent = ref->parent;
2031 ref_root = ref->root;
2033 BUG_ON(node->ref_mod != 1);
2034 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2035 BUG_ON(!extent_op || !extent_op->update_flags ||
2036 !extent_op->update_key);
2037 ret = alloc_reserved_tree_block(trans, root,
2039 extent_op->flags_to_set,
2042 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2043 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2044 node->num_bytes, parent, ref_root,
2045 ref->level, 0, 1, extent_op);
2046 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2047 ret = __btrfs_free_extent(trans, root, node->bytenr,
2048 node->num_bytes, parent, ref_root,
2049 ref->level, 0, 1, extent_op);
2056 /* helper function to actually process a single delayed ref entry */
2057 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2058 struct btrfs_root *root,
2059 struct btrfs_delayed_ref_node *node,
2060 struct btrfs_delayed_extent_op *extent_op,
2061 int insert_reserved)
2064 if (btrfs_delayed_ref_is_head(node)) {
2065 struct btrfs_delayed_ref_head *head;
2067 * we've hit the end of the chain and we were supposed
2068 * to insert this extent into the tree. But, it got
2069 * deleted before we ever needed to insert it, so all
2070 * we have to do is clean up the accounting
2073 head = btrfs_delayed_node_to_head(node);
2074 if (insert_reserved) {
2075 btrfs_pin_extent(root, node->bytenr,
2076 node->num_bytes, 1);
2077 if (head->is_data) {
2078 ret = btrfs_del_csums(trans, root,
2084 mutex_unlock(&head->mutex);
2088 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2089 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2090 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2092 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2093 node->type == BTRFS_SHARED_DATA_REF_KEY)
2094 ret = run_delayed_data_ref(trans, root, node, extent_op,
2101 static noinline struct btrfs_delayed_ref_node *
2102 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2104 struct rb_node *node;
2105 struct btrfs_delayed_ref_node *ref;
2106 int action = BTRFS_ADD_DELAYED_REF;
2109 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2110 * this prevents ref count from going down to zero when
2111 * there still are pending delayed ref.
2113 node = rb_prev(&head->node.rb_node);
2117 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2119 if (ref->bytenr != head->node.bytenr)
2121 if (ref->action == action)
2123 node = rb_prev(node);
2125 if (action == BTRFS_ADD_DELAYED_REF) {
2126 action = BTRFS_DROP_DELAYED_REF;
2132 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root,
2134 struct list_head *cluster)
2136 struct btrfs_delayed_ref_root *delayed_refs;
2137 struct btrfs_delayed_ref_node *ref;
2138 struct btrfs_delayed_ref_head *locked_ref = NULL;
2139 struct btrfs_delayed_extent_op *extent_op;
2142 int must_insert_reserved = 0;
2144 delayed_refs = &trans->transaction->delayed_refs;
2147 /* pick a new head ref from the cluster list */
2148 if (list_empty(cluster))
2151 locked_ref = list_entry(cluster->next,
2152 struct btrfs_delayed_ref_head, cluster);
2154 /* grab the lock that says we are going to process
2155 * all the refs for this head */
2156 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2159 * we may have dropped the spin lock to get the head
2160 * mutex lock, and that might have given someone else
2161 * time to free the head. If that's true, it has been
2162 * removed from our list and we can move on.
2164 if (ret == -EAGAIN) {
2172 * record the must insert reserved flag before we
2173 * drop the spin lock.
2175 must_insert_reserved = locked_ref->must_insert_reserved;
2176 locked_ref->must_insert_reserved = 0;
2178 extent_op = locked_ref->extent_op;
2179 locked_ref->extent_op = NULL;
2182 * locked_ref is the head node, so we have to go one
2183 * node back for any delayed ref updates
2185 ref = select_delayed_ref(locked_ref);
2187 /* All delayed refs have been processed, Go ahead
2188 * and send the head node to run_one_delayed_ref,
2189 * so that any accounting fixes can happen
2191 ref = &locked_ref->node;
2193 if (extent_op && must_insert_reserved) {
2199 spin_unlock(&delayed_refs->lock);
2201 ret = run_delayed_extent_op(trans, root,
2207 spin_lock(&delayed_refs->lock);
2211 list_del_init(&locked_ref->cluster);
2216 rb_erase(&ref->rb_node, &delayed_refs->root);
2217 delayed_refs->num_entries--;
2219 spin_unlock(&delayed_refs->lock);
2221 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2222 must_insert_reserved);
2225 btrfs_put_delayed_ref(ref);
2230 spin_lock(&delayed_refs->lock);
2236 * this starts processing the delayed reference count updates and
2237 * extent insertions we have queued up so far. count can be
2238 * 0, which means to process everything in the tree at the start
2239 * of the run (but not newly added entries), or it can be some target
2240 * number you'd like to process.
2242 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2243 struct btrfs_root *root, unsigned long count)
2245 struct rb_node *node;
2246 struct btrfs_delayed_ref_root *delayed_refs;
2247 struct btrfs_delayed_ref_node *ref;
2248 struct list_head cluster;
2250 int run_all = count == (unsigned long)-1;
2253 if (root == root->fs_info->extent_root)
2254 root = root->fs_info->tree_root;
2256 delayed_refs = &trans->transaction->delayed_refs;
2257 INIT_LIST_HEAD(&cluster);
2259 spin_lock(&delayed_refs->lock);
2261 count = delayed_refs->num_entries * 2;
2265 if (!(run_all || run_most) &&
2266 delayed_refs->num_heads_ready < 64)
2270 * go find something we can process in the rbtree. We start at
2271 * the beginning of the tree, and then build a cluster
2272 * of refs to process starting at the first one we are able to
2275 ret = btrfs_find_ref_cluster(trans, &cluster,
2276 delayed_refs->run_delayed_start);
2280 ret = run_clustered_refs(trans, root, &cluster);
2283 count -= min_t(unsigned long, ret, count);
2290 node = rb_first(&delayed_refs->root);
2293 count = (unsigned long)-1;
2296 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2298 if (btrfs_delayed_ref_is_head(ref)) {
2299 struct btrfs_delayed_ref_head *head;
2301 head = btrfs_delayed_node_to_head(ref);
2302 atomic_inc(&ref->refs);
2304 spin_unlock(&delayed_refs->lock);
2306 * Mutex was contended, block until it's
2307 * released and try again
2309 mutex_lock(&head->mutex);
2310 mutex_unlock(&head->mutex);
2312 btrfs_put_delayed_ref(ref);
2316 node = rb_next(node);
2318 spin_unlock(&delayed_refs->lock);
2319 schedule_timeout(1);
2323 spin_unlock(&delayed_refs->lock);
2327 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2328 struct btrfs_root *root,
2329 u64 bytenr, u64 num_bytes, u64 flags,
2332 struct btrfs_delayed_extent_op *extent_op;
2335 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2339 extent_op->flags_to_set = flags;
2340 extent_op->update_flags = 1;
2341 extent_op->update_key = 0;
2342 extent_op->is_data = is_data ? 1 : 0;
2344 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2350 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2351 struct btrfs_root *root,
2352 struct btrfs_path *path,
2353 u64 objectid, u64 offset, u64 bytenr)
2355 struct btrfs_delayed_ref_head *head;
2356 struct btrfs_delayed_ref_node *ref;
2357 struct btrfs_delayed_data_ref *data_ref;
2358 struct btrfs_delayed_ref_root *delayed_refs;
2359 struct rb_node *node;
2363 delayed_refs = &trans->transaction->delayed_refs;
2364 spin_lock(&delayed_refs->lock);
2365 head = btrfs_find_delayed_ref_head(trans, bytenr);
2369 if (!mutex_trylock(&head->mutex)) {
2370 atomic_inc(&head->node.refs);
2371 spin_unlock(&delayed_refs->lock);
2373 btrfs_release_path(path);
2376 * Mutex was contended, block until it's released and let
2379 mutex_lock(&head->mutex);
2380 mutex_unlock(&head->mutex);
2381 btrfs_put_delayed_ref(&head->node);
2385 node = rb_prev(&head->node.rb_node);
2389 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2391 if (ref->bytenr != bytenr)
2395 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2398 data_ref = btrfs_delayed_node_to_data_ref(ref);
2400 node = rb_prev(node);
2402 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2403 if (ref->bytenr == bytenr)
2407 if (data_ref->root != root->root_key.objectid ||
2408 data_ref->objectid != objectid || data_ref->offset != offset)
2413 mutex_unlock(&head->mutex);
2415 spin_unlock(&delayed_refs->lock);
2419 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2420 struct btrfs_root *root,
2421 struct btrfs_path *path,
2422 u64 objectid, u64 offset, u64 bytenr)
2424 struct btrfs_root *extent_root = root->fs_info->extent_root;
2425 struct extent_buffer *leaf;
2426 struct btrfs_extent_data_ref *ref;
2427 struct btrfs_extent_inline_ref *iref;
2428 struct btrfs_extent_item *ei;
2429 struct btrfs_key key;
2433 key.objectid = bytenr;
2434 key.offset = (u64)-1;
2435 key.type = BTRFS_EXTENT_ITEM_KEY;
2437 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2443 if (path->slots[0] == 0)
2447 leaf = path->nodes[0];
2448 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2450 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2454 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2455 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2456 if (item_size < sizeof(*ei)) {
2457 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2461 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2463 if (item_size != sizeof(*ei) +
2464 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2467 if (btrfs_extent_generation(leaf, ei) <=
2468 btrfs_root_last_snapshot(&root->root_item))
2471 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2472 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2473 BTRFS_EXTENT_DATA_REF_KEY)
2476 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2477 if (btrfs_extent_refs(leaf, ei) !=
2478 btrfs_extent_data_ref_count(leaf, ref) ||
2479 btrfs_extent_data_ref_root(leaf, ref) !=
2480 root->root_key.objectid ||
2481 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2482 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2490 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2491 struct btrfs_root *root,
2492 u64 objectid, u64 offset, u64 bytenr)
2494 struct btrfs_path *path;
2498 path = btrfs_alloc_path();
2503 ret = check_committed_ref(trans, root, path, objectid,
2505 if (ret && ret != -ENOENT)
2508 ret2 = check_delayed_ref(trans, root, path, objectid,
2510 } while (ret2 == -EAGAIN);
2512 if (ret2 && ret2 != -ENOENT) {
2517 if (ret != -ENOENT || ret2 != -ENOENT)
2520 btrfs_free_path(path);
2521 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2526 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2527 struct btrfs_root *root,
2528 struct extent_buffer *buf,
2529 int full_backref, int inc)
2536 struct btrfs_key key;
2537 struct btrfs_file_extent_item *fi;
2541 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2542 u64, u64, u64, u64, u64, u64);
2544 ref_root = btrfs_header_owner(buf);
2545 nritems = btrfs_header_nritems(buf);
2546 level = btrfs_header_level(buf);
2548 if (!root->ref_cows && level == 0)
2552 process_func = btrfs_inc_extent_ref;
2554 process_func = btrfs_free_extent;
2557 parent = buf->start;
2561 for (i = 0; i < nritems; i++) {
2563 btrfs_item_key_to_cpu(buf, &key, i);
2564 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2566 fi = btrfs_item_ptr(buf, i,
2567 struct btrfs_file_extent_item);
2568 if (btrfs_file_extent_type(buf, fi) ==
2569 BTRFS_FILE_EXTENT_INLINE)
2571 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2575 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2576 key.offset -= btrfs_file_extent_offset(buf, fi);
2577 ret = process_func(trans, root, bytenr, num_bytes,
2578 parent, ref_root, key.objectid,
2583 bytenr = btrfs_node_blockptr(buf, i);
2584 num_bytes = btrfs_level_size(root, level - 1);
2585 ret = process_func(trans, root, bytenr, num_bytes,
2586 parent, ref_root, level - 1, 0);
2597 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2598 struct extent_buffer *buf, int full_backref)
2600 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2603 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2604 struct extent_buffer *buf, int full_backref)
2606 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2609 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2610 struct btrfs_root *root,
2611 struct btrfs_path *path,
2612 struct btrfs_block_group_cache *cache)
2615 struct btrfs_root *extent_root = root->fs_info->extent_root;
2617 struct extent_buffer *leaf;
2619 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2624 leaf = path->nodes[0];
2625 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2626 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2627 btrfs_mark_buffer_dirty(leaf);
2628 btrfs_release_path(path);
2636 static struct btrfs_block_group_cache *
2637 next_block_group(struct btrfs_root *root,
2638 struct btrfs_block_group_cache *cache)
2640 struct rb_node *node;
2641 spin_lock(&root->fs_info->block_group_cache_lock);
2642 node = rb_next(&cache->cache_node);
2643 btrfs_put_block_group(cache);
2645 cache = rb_entry(node, struct btrfs_block_group_cache,
2647 btrfs_get_block_group(cache);
2650 spin_unlock(&root->fs_info->block_group_cache_lock);
2654 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2655 struct btrfs_trans_handle *trans,
2656 struct btrfs_path *path)
2658 struct btrfs_root *root = block_group->fs_info->tree_root;
2659 struct inode *inode = NULL;
2661 int dcs = BTRFS_DC_ERROR;
2667 * If this block group is smaller than 100 megs don't bother caching the
2670 if (block_group->key.offset < (100 * 1024 * 1024)) {
2671 spin_lock(&block_group->lock);
2672 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2673 spin_unlock(&block_group->lock);
2678 inode = lookup_free_space_inode(root, block_group, path);
2679 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2680 ret = PTR_ERR(inode);
2681 btrfs_release_path(path);
2685 if (IS_ERR(inode)) {
2689 if (block_group->ro)
2692 ret = create_free_space_inode(root, trans, block_group, path);
2699 * We want to set the generation to 0, that way if anything goes wrong
2700 * from here on out we know not to trust this cache when we load up next
2703 BTRFS_I(inode)->generation = 0;
2704 ret = btrfs_update_inode(trans, root, inode);
2707 if (i_size_read(inode) > 0) {
2708 ret = btrfs_truncate_free_space_cache(root, trans, path,
2714 spin_lock(&block_group->lock);
2715 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2716 /* We're not cached, don't bother trying to write stuff out */
2717 dcs = BTRFS_DC_WRITTEN;
2718 spin_unlock(&block_group->lock);
2721 spin_unlock(&block_group->lock);
2723 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2728 * Just to make absolutely sure we have enough space, we're going to
2729 * preallocate 12 pages worth of space for each block group. In
2730 * practice we ought to use at most 8, but we need extra space so we can
2731 * add our header and have a terminator between the extents and the
2735 num_pages *= PAGE_CACHE_SIZE;
2737 ret = btrfs_check_data_free_space(inode, num_pages);
2741 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2742 num_pages, num_pages,
2745 dcs = BTRFS_DC_SETUP;
2746 btrfs_free_reserved_data_space(inode, num_pages);
2750 btrfs_release_path(path);
2752 spin_lock(&block_group->lock);
2753 block_group->disk_cache_state = dcs;
2754 spin_unlock(&block_group->lock);
2759 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2760 struct btrfs_root *root)
2762 struct btrfs_block_group_cache *cache;
2764 struct btrfs_path *path;
2767 path = btrfs_alloc_path();
2773 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2775 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2777 cache = next_block_group(root, cache);
2785 err = cache_save_setup(cache, trans, path);
2786 last = cache->key.objectid + cache->key.offset;
2787 btrfs_put_block_group(cache);
2792 err = btrfs_run_delayed_refs(trans, root,
2797 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2799 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2800 btrfs_put_block_group(cache);
2806 cache = next_block_group(root, cache);
2815 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2816 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
2818 last = cache->key.objectid + cache->key.offset;
2820 err = write_one_cache_group(trans, root, path, cache);
2822 btrfs_put_block_group(cache);
2827 * I don't think this is needed since we're just marking our
2828 * preallocated extent as written, but just in case it can't
2832 err = btrfs_run_delayed_refs(trans, root,
2837 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2840 * Really this shouldn't happen, but it could if we
2841 * couldn't write the entire preallocated extent and
2842 * splitting the extent resulted in a new block.
2845 btrfs_put_block_group(cache);
2848 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2850 cache = next_block_group(root, cache);
2859 btrfs_write_out_cache(root, trans, cache, path);
2862 * If we didn't have an error then the cache state is still
2863 * NEED_WRITE, so we can set it to WRITTEN.
2865 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2866 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2867 last = cache->key.objectid + cache->key.offset;
2868 btrfs_put_block_group(cache);
2871 btrfs_free_path(path);
2875 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2877 struct btrfs_block_group_cache *block_group;
2880 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2881 if (!block_group || block_group->ro)
2884 btrfs_put_block_group(block_group);
2888 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2889 u64 total_bytes, u64 bytes_used,
2890 struct btrfs_space_info **space_info)
2892 struct btrfs_space_info *found;
2896 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
2897 BTRFS_BLOCK_GROUP_RAID10))
2902 found = __find_space_info(info, flags);
2904 spin_lock(&found->lock);
2905 found->total_bytes += total_bytes;
2906 found->disk_total += total_bytes * factor;
2907 found->bytes_used += bytes_used;
2908 found->disk_used += bytes_used * factor;
2910 spin_unlock(&found->lock);
2911 *space_info = found;
2914 found = kzalloc(sizeof(*found), GFP_NOFS);
2918 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
2919 INIT_LIST_HEAD(&found->block_groups[i]);
2920 init_rwsem(&found->groups_sem);
2921 spin_lock_init(&found->lock);
2922 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
2923 BTRFS_BLOCK_GROUP_SYSTEM |
2924 BTRFS_BLOCK_GROUP_METADATA);
2925 found->total_bytes = total_bytes;
2926 found->disk_total = total_bytes * factor;
2927 found->bytes_used = bytes_used;
2928 found->disk_used = bytes_used * factor;
2929 found->bytes_pinned = 0;
2930 found->bytes_reserved = 0;
2931 found->bytes_readonly = 0;
2932 found->bytes_may_use = 0;
2934 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
2935 found->chunk_alloc = 0;
2936 *space_info = found;
2937 list_add_rcu(&found->list, &info->space_info);
2938 atomic_set(&found->caching_threads, 0);
2942 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
2944 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
2945 BTRFS_BLOCK_GROUP_RAID1 |
2946 BTRFS_BLOCK_GROUP_RAID10 |
2947 BTRFS_BLOCK_GROUP_DUP);
2949 if (flags & BTRFS_BLOCK_GROUP_DATA)
2950 fs_info->avail_data_alloc_bits |= extra_flags;
2951 if (flags & BTRFS_BLOCK_GROUP_METADATA)
2952 fs_info->avail_metadata_alloc_bits |= extra_flags;
2953 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2954 fs_info->avail_system_alloc_bits |= extra_flags;
2958 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
2961 * we add in the count of missing devices because we want
2962 * to make sure that any RAID levels on a degraded FS
2963 * continue to be honored.
2965 u64 num_devices = root->fs_info->fs_devices->rw_devices +
2966 root->fs_info->fs_devices->missing_devices;
2968 if (num_devices == 1)
2969 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
2970 if (num_devices < 4)
2971 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
2973 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2974 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2975 BTRFS_BLOCK_GROUP_RAID10))) {
2976 flags &= ~BTRFS_BLOCK_GROUP_DUP;
2979 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
2980 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
2981 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
2984 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
2985 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
2986 (flags & BTRFS_BLOCK_GROUP_RAID10) |
2987 (flags & BTRFS_BLOCK_GROUP_DUP)))
2988 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
2992 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
2994 if (flags & BTRFS_BLOCK_GROUP_DATA)
2995 flags |= root->fs_info->avail_data_alloc_bits &
2996 root->fs_info->data_alloc_profile;
2997 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
2998 flags |= root->fs_info->avail_system_alloc_bits &
2999 root->fs_info->system_alloc_profile;
3000 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3001 flags |= root->fs_info->avail_metadata_alloc_bits &
3002 root->fs_info->metadata_alloc_profile;
3003 return btrfs_reduce_alloc_profile(root, flags);
3006 u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3011 flags = BTRFS_BLOCK_GROUP_DATA;
3012 else if (root == root->fs_info->chunk_root)
3013 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3015 flags = BTRFS_BLOCK_GROUP_METADATA;
3017 return get_alloc_profile(root, flags);
3020 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3022 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
3023 BTRFS_BLOCK_GROUP_DATA);
3027 * This will check the space that the inode allocates from to make sure we have
3028 * enough space for bytes.
3030 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3032 struct btrfs_space_info *data_sinfo;
3033 struct btrfs_root *root = BTRFS_I(inode)->root;
3035 int ret = 0, committed = 0, alloc_chunk = 1;
3037 /* make sure bytes are sectorsize aligned */
3038 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3040 if (root == root->fs_info->tree_root ||
3041 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
3046 data_sinfo = BTRFS_I(inode)->space_info;
3051 /* make sure we have enough space to handle the data first */
3052 spin_lock(&data_sinfo->lock);
3053 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3054 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3055 data_sinfo->bytes_may_use;
3057 if (used + bytes > data_sinfo->total_bytes) {
3058 struct btrfs_trans_handle *trans;
3061 * if we don't have enough free bytes in this space then we need
3062 * to alloc a new chunk.
3064 if (!data_sinfo->full && alloc_chunk) {
3067 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
3068 spin_unlock(&data_sinfo->lock);
3070 alloc_target = btrfs_get_alloc_profile(root, 1);
3071 trans = btrfs_join_transaction(root, 1);
3073 return PTR_ERR(trans);
3075 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3076 bytes + 2 * 1024 * 1024,
3078 CHUNK_ALLOC_NO_FORCE);
3079 btrfs_end_transaction(trans, root);
3088 btrfs_set_inode_space_info(root, inode);
3089 data_sinfo = BTRFS_I(inode)->space_info;
3093 spin_unlock(&data_sinfo->lock);
3095 /* commit the current transaction and try again */
3097 if (!committed && !root->fs_info->open_ioctl_trans) {
3099 trans = btrfs_join_transaction(root, 1);
3101 return PTR_ERR(trans);
3102 ret = btrfs_commit_transaction(trans, root);
3110 data_sinfo->bytes_may_use += bytes;
3111 BTRFS_I(inode)->reserved_bytes += bytes;
3112 spin_unlock(&data_sinfo->lock);
3118 * called when we are clearing an delalloc extent from the
3119 * inode's io_tree or there was an error for whatever reason
3120 * after calling btrfs_check_data_free_space
3122 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3124 struct btrfs_root *root = BTRFS_I(inode)->root;
3125 struct btrfs_space_info *data_sinfo;
3127 /* make sure bytes are sectorsize aligned */
3128 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3130 data_sinfo = BTRFS_I(inode)->space_info;
3131 spin_lock(&data_sinfo->lock);
3132 data_sinfo->bytes_may_use -= bytes;
3133 BTRFS_I(inode)->reserved_bytes -= bytes;
3134 spin_unlock(&data_sinfo->lock);
3137 static void force_metadata_allocation(struct btrfs_fs_info *info)
3139 struct list_head *head = &info->space_info;
3140 struct btrfs_space_info *found;
3143 list_for_each_entry_rcu(found, head, list) {
3144 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3145 found->force_alloc = CHUNK_ALLOC_FORCE;
3150 static int should_alloc_chunk(struct btrfs_root *root,
3151 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3154 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3155 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3158 if (force == CHUNK_ALLOC_FORCE)
3162 * in limited mode, we want to have some free space up to
3163 * about 1% of the FS size.
3165 if (force == CHUNK_ALLOC_LIMITED) {
3166 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3167 thresh = max_t(u64, 64 * 1024 * 1024,
3168 div_factor_fine(thresh, 1));
3170 if (num_bytes - num_allocated < thresh)
3175 * we have two similar checks here, one based on percentage
3176 * and once based on a hard number of 256MB. The idea
3177 * is that if we have a good amount of free
3178 * room, don't allocate a chunk. A good mount is
3179 * less than 80% utilized of the chunks we have allocated,
3180 * or more than 256MB free
3182 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3185 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
3188 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3190 /* 256MB or 5% of the FS */
3191 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3193 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
3198 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3199 struct btrfs_root *extent_root, u64 alloc_bytes,
3200 u64 flags, int force)
3202 struct btrfs_space_info *space_info;
3203 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3204 int wait_for_alloc = 0;
3207 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3209 space_info = __find_space_info(extent_root->fs_info, flags);
3211 ret = update_space_info(extent_root->fs_info, flags,
3215 BUG_ON(!space_info);
3218 spin_lock(&space_info->lock);
3219 if (space_info->force_alloc)
3220 force = space_info->force_alloc;
3221 if (space_info->full) {
3222 spin_unlock(&space_info->lock);
3226 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
3227 spin_unlock(&space_info->lock);
3229 } else if (space_info->chunk_alloc) {
3232 space_info->chunk_alloc = 1;
3235 spin_unlock(&space_info->lock);
3237 mutex_lock(&fs_info->chunk_mutex);
3240 * The chunk_mutex is held throughout the entirety of a chunk
3241 * allocation, so once we've acquired the chunk_mutex we know that the
3242 * other guy is done and we need to recheck and see if we should
3245 if (wait_for_alloc) {
3246 mutex_unlock(&fs_info->chunk_mutex);
3252 * If we have mixed data/metadata chunks we want to make sure we keep
3253 * allocating mixed chunks instead of individual chunks.
3255 if (btrfs_mixed_space_info(space_info))
3256 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3259 * if we're doing a data chunk, go ahead and make sure that
3260 * we keep a reasonable number of metadata chunks allocated in the
3263 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3264 fs_info->data_chunk_allocations++;
3265 if (!(fs_info->data_chunk_allocations %
3266 fs_info->metadata_ratio))
3267 force_metadata_allocation(fs_info);
3270 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3271 spin_lock(&space_info->lock);
3273 space_info->full = 1;
3277 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3278 space_info->chunk_alloc = 0;
3279 spin_unlock(&space_info->lock);
3280 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3285 * shrink metadata reservation for delalloc
3287 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3288 struct btrfs_root *root, u64 to_reclaim, int sync)
3290 struct btrfs_block_rsv *block_rsv;
3291 struct btrfs_space_info *space_info;
3296 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
3298 unsigned long progress;
3300 block_rsv = &root->fs_info->delalloc_block_rsv;
3301 space_info = block_rsv->space_info;
3304 reserved = space_info->bytes_reserved;
3305 progress = space_info->reservation_progress;
3310 max_reclaim = min(reserved, to_reclaim);
3312 while (loops < 1024) {
3313 /* have the flusher threads jump in and do some IO */
3315 nr_pages = min_t(unsigned long, nr_pages,
3316 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3317 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
3319 spin_lock(&space_info->lock);
3320 if (reserved > space_info->bytes_reserved)
3321 reclaimed += reserved - space_info->bytes_reserved;
3322 reserved = space_info->bytes_reserved;
3323 spin_unlock(&space_info->lock);
3327 if (reserved == 0 || reclaimed >= max_reclaim)
3330 if (trans && trans->transaction->blocked)
3333 time_left = schedule_timeout_interruptible(1);
3335 /* We were interrupted, exit */
3339 /* we've kicked the IO a few times, if anything has been freed,
3340 * exit. There is no sense in looping here for a long time
3341 * when we really need to commit the transaction, or there are
3342 * just too many writers without enough free space
3347 if (progress != space_info->reservation_progress)
3352 return reclaimed >= to_reclaim;
3356 * Retries tells us how many times we've called reserve_metadata_bytes. The
3357 * idea is if this is the first call (retries == 0) then we will add to our
3358 * reserved count if we can't make the allocation in order to hold our place
3359 * while we go and try and free up space. That way for retries > 1 we don't try
3360 * and add space, we just check to see if the amount of unused space is >= the
3361 * total space, meaning that our reservation is valid.
3363 * However if we don't intend to retry this reservation, pass -1 as retries so
3364 * that it short circuits this logic.
3366 static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3367 struct btrfs_root *root,
3368 struct btrfs_block_rsv *block_rsv,
3369 u64 orig_bytes, int flush)
3371 struct btrfs_space_info *space_info = block_rsv->space_info;
3373 u64 num_bytes = orig_bytes;
3376 bool reserved = false;
3377 bool committed = false;
3384 spin_lock(&space_info->lock);
3385 unused = space_info->bytes_used + space_info->bytes_reserved +
3386 space_info->bytes_pinned + space_info->bytes_readonly +
3387 space_info->bytes_may_use;
3390 * The idea here is that we've not already over-reserved the block group
3391 * then we can go ahead and save our reservation first and then start
3392 * flushing if we need to. Otherwise if we've already overcommitted
3393 * lets start flushing stuff first and then come back and try to make
3396 if (unused <= space_info->total_bytes) {
3397 unused = space_info->total_bytes - unused;
3398 if (unused >= num_bytes) {
3400 space_info->bytes_reserved += orig_bytes;
3404 * Ok set num_bytes to orig_bytes since we aren't
3405 * overocmmitted, this way we only try and reclaim what
3408 num_bytes = orig_bytes;
3412 * Ok we're over committed, set num_bytes to the overcommitted
3413 * amount plus the amount of bytes that we need for this
3416 num_bytes = unused - space_info->total_bytes +
3417 (orig_bytes * (retries + 1));
3421 * Couldn't make our reservation, save our place so while we're trying
3422 * to reclaim space we can actually use it instead of somebody else
3423 * stealing it from us.
3425 if (ret && !reserved) {
3426 space_info->bytes_reserved += orig_bytes;
3430 spin_unlock(&space_info->lock);
3439 * We do synchronous shrinking since we don't actually unreserve
3440 * metadata until after the IO is completed.
3442 ret = shrink_delalloc(trans, root, num_bytes, 1);
3449 * So if we were overcommitted it's possible that somebody else flushed
3450 * out enough space and we simply didn't have enough space to reclaim,
3451 * so go back around and try again.
3458 spin_lock(&space_info->lock);
3460 * Not enough space to be reclaimed, don't bother committing the
3463 if (space_info->bytes_pinned < orig_bytes)
3465 spin_unlock(&space_info->lock);
3470 if (trans || committed)
3474 trans = btrfs_join_transaction(root, 1);
3477 ret = btrfs_commit_transaction(trans, root);
3486 spin_lock(&space_info->lock);
3487 space_info->bytes_reserved -= orig_bytes;
3488 spin_unlock(&space_info->lock);
3494 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3495 struct btrfs_root *root)
3497 struct btrfs_block_rsv *block_rsv;
3499 block_rsv = trans->block_rsv;
3501 block_rsv = root->block_rsv;
3504 block_rsv = &root->fs_info->empty_block_rsv;
3509 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3513 spin_lock(&block_rsv->lock);
3514 if (block_rsv->reserved >= num_bytes) {
3515 block_rsv->reserved -= num_bytes;
3516 if (block_rsv->reserved < block_rsv->size)
3517 block_rsv->full = 0;
3520 spin_unlock(&block_rsv->lock);
3524 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3525 u64 num_bytes, int update_size)
3527 spin_lock(&block_rsv->lock);
3528 block_rsv->reserved += num_bytes;
3530 block_rsv->size += num_bytes;
3531 else if (block_rsv->reserved >= block_rsv->size)
3532 block_rsv->full = 1;
3533 spin_unlock(&block_rsv->lock);
3536 static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3537 struct btrfs_block_rsv *dest, u64 num_bytes)
3539 struct btrfs_space_info *space_info = block_rsv->space_info;
3541 spin_lock(&block_rsv->lock);
3542 if (num_bytes == (u64)-1)
3543 num_bytes = block_rsv->size;
3544 block_rsv->size -= num_bytes;
3545 if (block_rsv->reserved >= block_rsv->size) {
3546 num_bytes = block_rsv->reserved - block_rsv->size;
3547 block_rsv->reserved = block_rsv->size;
3548 block_rsv->full = 1;
3552 spin_unlock(&block_rsv->lock);
3554 if (num_bytes > 0) {
3556 spin_lock(&dest->lock);
3560 bytes_to_add = dest->size - dest->reserved;
3561 bytes_to_add = min(num_bytes, bytes_to_add);
3562 dest->reserved += bytes_to_add;
3563 if (dest->reserved >= dest->size)
3565 num_bytes -= bytes_to_add;
3567 spin_unlock(&dest->lock);
3570 spin_lock(&space_info->lock);
3571 space_info->bytes_reserved -= num_bytes;
3572 space_info->reservation_progress++;
3573 spin_unlock(&space_info->lock);
3578 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3579 struct btrfs_block_rsv *dst, u64 num_bytes)
3583 ret = block_rsv_use_bytes(src, num_bytes);
3587 block_rsv_add_bytes(dst, num_bytes, 1);
3591 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3593 memset(rsv, 0, sizeof(*rsv));
3594 spin_lock_init(&rsv->lock);
3595 atomic_set(&rsv->usage, 1);
3597 INIT_LIST_HEAD(&rsv->list);
3600 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3602 struct btrfs_block_rsv *block_rsv;
3603 struct btrfs_fs_info *fs_info = root->fs_info;
3605 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3609 btrfs_init_block_rsv(block_rsv);
3610 block_rsv->space_info = __find_space_info(fs_info,
3611 BTRFS_BLOCK_GROUP_METADATA);
3615 void btrfs_free_block_rsv(struct btrfs_root *root,
3616 struct btrfs_block_rsv *rsv)
3618 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3619 btrfs_block_rsv_release(root, rsv, (u64)-1);
3626 * make the block_rsv struct be able to capture freed space.
3627 * the captured space will re-add to the the block_rsv struct
3628 * after transaction commit
3630 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3631 struct btrfs_block_rsv *block_rsv)
3633 block_rsv->durable = 1;
3634 mutex_lock(&fs_info->durable_block_rsv_mutex);
3635 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3636 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3639 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3640 struct btrfs_root *root,
3641 struct btrfs_block_rsv *block_rsv,
3649 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
3651 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3658 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3659 struct btrfs_root *root,
3660 struct btrfs_block_rsv *block_rsv,
3661 u64 min_reserved, int min_factor)
3664 int commit_trans = 0;
3670 spin_lock(&block_rsv->lock);
3672 num_bytes = div_factor(block_rsv->size, min_factor);
3673 if (min_reserved > num_bytes)
3674 num_bytes = min_reserved;
3676 if (block_rsv->reserved >= num_bytes) {
3679 num_bytes -= block_rsv->reserved;
3680 if (block_rsv->durable &&
3681 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3684 spin_unlock(&block_rsv->lock);
3688 if (block_rsv->refill_used) {
3689 ret = reserve_metadata_bytes(trans, root, block_rsv,
3692 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3701 trans = btrfs_join_transaction(root, 1);
3702 BUG_ON(IS_ERR(trans));
3703 ret = btrfs_commit_transaction(trans, root);
3710 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3711 struct btrfs_block_rsv *dst_rsv,
3714 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3717 void btrfs_block_rsv_release(struct btrfs_root *root,
3718 struct btrfs_block_rsv *block_rsv,
3721 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3722 if (global_rsv->full || global_rsv == block_rsv ||
3723 block_rsv->space_info != global_rsv->space_info)
3725 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3729 * helper to calculate size of global block reservation.
3730 * the desired value is sum of space used by extent tree,
3731 * checksum tree and root tree
3733 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3735 struct btrfs_space_info *sinfo;
3739 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3741 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3742 spin_lock(&sinfo->lock);
3743 data_used = sinfo->bytes_used;
3744 spin_unlock(&sinfo->lock);
3746 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3747 spin_lock(&sinfo->lock);
3748 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3750 meta_used = sinfo->bytes_used;
3751 spin_unlock(&sinfo->lock);
3753 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3755 num_bytes += div64_u64(data_used + meta_used, 50);
3757 if (num_bytes * 3 > meta_used)
3758 num_bytes = div64_u64(meta_used, 3);
3760 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3763 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3765 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3766 struct btrfs_space_info *sinfo = block_rsv->space_info;
3769 num_bytes = calc_global_metadata_size(fs_info);
3771 spin_lock(&block_rsv->lock);
3772 spin_lock(&sinfo->lock);
3774 block_rsv->size = num_bytes;
3776 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3777 sinfo->bytes_reserved + sinfo->bytes_readonly +
3778 sinfo->bytes_may_use;
3780 if (sinfo->total_bytes > num_bytes) {
3781 num_bytes = sinfo->total_bytes - num_bytes;
3782 block_rsv->reserved += num_bytes;
3783 sinfo->bytes_reserved += num_bytes;
3786 if (block_rsv->reserved >= block_rsv->size) {
3787 num_bytes = block_rsv->reserved - block_rsv->size;
3788 sinfo->bytes_reserved -= num_bytes;
3789 sinfo->reservation_progress++;
3790 block_rsv->reserved = block_rsv->size;
3791 block_rsv->full = 1;
3794 spin_unlock(&sinfo->lock);
3795 spin_unlock(&block_rsv->lock);
3798 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3800 struct btrfs_space_info *space_info;
3802 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3803 fs_info->chunk_block_rsv.space_info = space_info;
3804 fs_info->chunk_block_rsv.priority = 10;
3806 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3807 fs_info->global_block_rsv.space_info = space_info;
3808 fs_info->global_block_rsv.priority = 10;
3809 fs_info->global_block_rsv.refill_used = 1;
3810 fs_info->delalloc_block_rsv.space_info = space_info;
3811 fs_info->trans_block_rsv.space_info = space_info;
3812 fs_info->empty_block_rsv.space_info = space_info;
3813 fs_info->empty_block_rsv.priority = 10;
3815 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3816 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3817 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3818 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3819 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3821 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3823 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3825 update_global_block_rsv(fs_info);
3828 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3830 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3831 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3832 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3833 WARN_ON(fs_info->trans_block_rsv.size > 0);
3834 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3835 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3836 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3839 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3840 struct btrfs_root *root,
3846 if (num_items == 0 || root->fs_info->chunk_root == root)
3849 num_bytes = btrfs_calc_trans_metadata_size(root, num_items);
3850 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3853 trans->bytes_reserved += num_bytes;
3854 trans->block_rsv = &root->fs_info->trans_block_rsv;
3859 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
3860 struct btrfs_root *root)
3862 if (!trans->bytes_reserved)
3865 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
3866 btrfs_block_rsv_release(root, trans->block_rsv,
3867 trans->bytes_reserved);
3868 trans->bytes_reserved = 0;
3871 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
3872 struct inode *inode)
3874 struct btrfs_root *root = BTRFS_I(inode)->root;
3875 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3876 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
3879 * one for deleting orphan item, one for updating inode and
3880 * two for calling btrfs_truncate_inode_items.
3882 * btrfs_truncate_inode_items is a delete operation, it frees
3883 * more space than it uses in most cases. So two units of
3884 * metadata space should be enough for calling it many times.
3885 * If all of the metadata space is used, we can commit
3886 * transaction and use space it freed.
3888 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4);
3889 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3892 void btrfs_orphan_release_metadata(struct inode *inode)
3894 struct btrfs_root *root = BTRFS_I(inode)->root;
3895 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 4);
3896 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
3899 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
3900 struct btrfs_pending_snapshot *pending)
3902 struct btrfs_root *root = pending->root;
3903 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
3904 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
3906 * two for root back/forward refs, two for directory entries
3907 * and one for root of the snapshot.
3909 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 5);
3910 dst_rsv->space_info = src_rsv->space_info;
3911 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3914 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
3916 return num_bytes >>= 3;
3919 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
3921 struct btrfs_root *root = BTRFS_I(inode)->root;
3922 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
3925 int reserved_extents;
3928 if (btrfs_transaction_in_commit(root->fs_info))
3929 schedule_timeout(1);
3931 num_bytes = ALIGN(num_bytes, root->sectorsize);
3933 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
3934 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3936 if (nr_extents > reserved_extents) {
3937 nr_extents -= reserved_extents;
3938 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
3944 to_reserve += calc_csum_metadata_size(inode, num_bytes);
3945 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
3949 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
3950 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
3952 block_rsv_add_bytes(block_rsv, to_reserve, 1);
3954 if (block_rsv->size > 512 * 1024 * 1024)
3955 shrink_delalloc(NULL, root, to_reserve, 0);
3960 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
3962 struct btrfs_root *root = BTRFS_I(inode)->root;
3965 int reserved_extents;
3967 num_bytes = ALIGN(num_bytes, root->sectorsize);
3968 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
3969 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
3971 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
3975 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
3976 if (nr_extents >= reserved_extents) {
3980 old = reserved_extents;
3981 nr_extents = reserved_extents - nr_extents;
3982 new = reserved_extents - nr_extents;
3983 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
3984 reserved_extents, new);
3985 if (likely(old == reserved_extents))
3987 reserved_extents = old;
3990 to_free = calc_csum_metadata_size(inode, num_bytes);
3992 to_free += btrfs_calc_trans_metadata_size(root, nr_extents);
3994 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
3998 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4002 ret = btrfs_check_data_free_space(inode, num_bytes);
4006 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4008 btrfs_free_reserved_data_space(inode, num_bytes);
4015 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4017 btrfs_delalloc_release_metadata(inode, num_bytes);
4018 btrfs_free_reserved_data_space(inode, num_bytes);
4021 static int update_block_group(struct btrfs_trans_handle *trans,
4022 struct btrfs_root *root,
4023 u64 bytenr, u64 num_bytes, int alloc)
4025 struct btrfs_block_group_cache *cache = NULL;
4026 struct btrfs_fs_info *info = root->fs_info;
4027 u64 total = num_bytes;
4032 /* block accounting for super block */
4033 spin_lock(&info->delalloc_lock);
4034 old_val = btrfs_super_bytes_used(&info->super_copy);
4036 old_val += num_bytes;
4038 old_val -= num_bytes;
4039 btrfs_set_super_bytes_used(&info->super_copy, old_val);
4040 spin_unlock(&info->delalloc_lock);
4043 cache = btrfs_lookup_block_group(info, bytenr);
4046 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4047 BTRFS_BLOCK_GROUP_RAID1 |
4048 BTRFS_BLOCK_GROUP_RAID10))
4053 * If this block group has free space cache written out, we
4054 * need to make sure to load it if we are removing space. This
4055 * is because we need the unpinning stage to actually add the
4056 * space back to the block group, otherwise we will leak space.
4058 if (!alloc && cache->cached == BTRFS_CACHE_NO)
4059 cache_block_group(cache, trans, NULL, 1);
4061 byte_in_group = bytenr - cache->key.objectid;
4062 WARN_ON(byte_in_group > cache->key.offset);
4064 spin_lock(&cache->space_info->lock);
4065 spin_lock(&cache->lock);
4067 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4068 cache->disk_cache_state < BTRFS_DC_CLEAR)
4069 cache->disk_cache_state = BTRFS_DC_CLEAR;
4072 old_val = btrfs_block_group_used(&cache->item);
4073 num_bytes = min(total, cache->key.offset - byte_in_group);
4075 old_val += num_bytes;
4076 btrfs_set_block_group_used(&cache->item, old_val);
4077 cache->reserved -= num_bytes;
4078 cache->space_info->bytes_reserved -= num_bytes;
4079 cache->space_info->reservation_progress++;
4080 cache->space_info->bytes_used += num_bytes;
4081 cache->space_info->disk_used += num_bytes * factor;
4082 spin_unlock(&cache->lock);
4083 spin_unlock(&cache->space_info->lock);
4085 old_val -= num_bytes;
4086 btrfs_set_block_group_used(&cache->item, old_val);
4087 cache->pinned += num_bytes;
4088 cache->space_info->bytes_pinned += num_bytes;
4089 cache->space_info->bytes_used -= num_bytes;
4090 cache->space_info->disk_used -= num_bytes * factor;
4091 spin_unlock(&cache->lock);
4092 spin_unlock(&cache->space_info->lock);
4094 set_extent_dirty(info->pinned_extents,
4095 bytenr, bytenr + num_bytes - 1,
4096 GFP_NOFS | __GFP_NOFAIL);
4098 btrfs_put_block_group(cache);
4100 bytenr += num_bytes;
4105 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4107 struct btrfs_block_group_cache *cache;
4110 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4114 bytenr = cache->key.objectid;
4115 btrfs_put_block_group(cache);
4120 static int pin_down_extent(struct btrfs_root *root,
4121 struct btrfs_block_group_cache *cache,
4122 u64 bytenr, u64 num_bytes, int reserved)
4124 spin_lock(&cache->space_info->lock);
4125 spin_lock(&cache->lock);
4126 cache->pinned += num_bytes;
4127 cache->space_info->bytes_pinned += num_bytes;
4129 cache->reserved -= num_bytes;
4130 cache->space_info->bytes_reserved -= num_bytes;
4131 cache->space_info->reservation_progress++;
4133 spin_unlock(&cache->lock);
4134 spin_unlock(&cache->space_info->lock);
4136 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4137 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
4142 * this function must be called within transaction
4144 int btrfs_pin_extent(struct btrfs_root *root,
4145 u64 bytenr, u64 num_bytes, int reserved)
4147 struct btrfs_block_group_cache *cache;
4149 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4152 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4154 btrfs_put_block_group(cache);
4159 * update size of reserved extents. this function may return -EAGAIN
4160 * if 'reserve' is true or 'sinfo' is false.
4162 int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4163 u64 num_bytes, int reserve, int sinfo)
4167 struct btrfs_space_info *space_info = cache->space_info;
4168 spin_lock(&space_info->lock);
4169 spin_lock(&cache->lock);
4174 cache->reserved += num_bytes;
4175 space_info->bytes_reserved += num_bytes;
4179 space_info->bytes_readonly += num_bytes;
4180 cache->reserved -= num_bytes;
4181 space_info->bytes_reserved -= num_bytes;
4182 space_info->reservation_progress++;
4184 spin_unlock(&cache->lock);
4185 spin_unlock(&space_info->lock);
4187 spin_lock(&cache->lock);
4192 cache->reserved += num_bytes;
4194 cache->reserved -= num_bytes;
4196 spin_unlock(&cache->lock);
4201 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4202 struct btrfs_root *root)
4204 struct btrfs_fs_info *fs_info = root->fs_info;
4205 struct btrfs_caching_control *next;
4206 struct btrfs_caching_control *caching_ctl;
4207 struct btrfs_block_group_cache *cache;
4209 down_write(&fs_info->extent_commit_sem);
4211 list_for_each_entry_safe(caching_ctl, next,
4212 &fs_info->caching_block_groups, list) {
4213 cache = caching_ctl->block_group;
4214 if (block_group_cache_done(cache)) {
4215 cache->last_byte_to_unpin = (u64)-1;
4216 list_del_init(&caching_ctl->list);
4217 put_caching_control(caching_ctl);
4219 cache->last_byte_to_unpin = caching_ctl->progress;
4223 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4224 fs_info->pinned_extents = &fs_info->freed_extents[1];
4226 fs_info->pinned_extents = &fs_info->freed_extents[0];
4228 up_write(&fs_info->extent_commit_sem);
4230 update_global_block_rsv(fs_info);
4234 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4236 struct btrfs_fs_info *fs_info = root->fs_info;
4237 struct btrfs_block_group_cache *cache = NULL;
4240 while (start <= end) {
4242 start >= cache->key.objectid + cache->key.offset) {
4244 btrfs_put_block_group(cache);
4245 cache = btrfs_lookup_block_group(fs_info, start);
4249 len = cache->key.objectid + cache->key.offset - start;
4250 len = min(len, end + 1 - start);
4252 if (start < cache->last_byte_to_unpin) {
4253 len = min(len, cache->last_byte_to_unpin - start);
4254 btrfs_add_free_space(cache, start, len);
4259 spin_lock(&cache->space_info->lock);
4260 spin_lock(&cache->lock);
4261 cache->pinned -= len;
4262 cache->space_info->bytes_pinned -= len;
4264 cache->space_info->bytes_readonly += len;
4265 } else if (cache->reserved_pinned > 0) {
4266 len = min(len, cache->reserved_pinned);
4267 cache->reserved_pinned -= len;
4268 cache->space_info->bytes_reserved += len;
4270 spin_unlock(&cache->lock);
4271 spin_unlock(&cache->space_info->lock);
4275 btrfs_put_block_group(cache);
4279 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4280 struct btrfs_root *root)
4282 struct btrfs_fs_info *fs_info = root->fs_info;
4283 struct extent_io_tree *unpin;
4284 struct btrfs_block_rsv *block_rsv;
4285 struct btrfs_block_rsv *next_rsv;
4291 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4292 unpin = &fs_info->freed_extents[1];
4294 unpin = &fs_info->freed_extents[0];
4297 ret = find_first_extent_bit(unpin, 0, &start, &end,
4302 if (btrfs_test_opt(root, DISCARD))
4303 ret = btrfs_discard_extent(root, start,
4304 end + 1 - start, NULL);
4306 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4307 unpin_extent_range(root, start, end);
4311 mutex_lock(&fs_info->durable_block_rsv_mutex);
4312 list_for_each_entry_safe(block_rsv, next_rsv,
4313 &fs_info->durable_block_rsv_list, list) {
4315 idx = trans->transid & 0x1;
4316 if (block_rsv->freed[idx] > 0) {
4317 block_rsv_add_bytes(block_rsv,
4318 block_rsv->freed[idx], 0);
4319 block_rsv->freed[idx] = 0;
4321 if (atomic_read(&block_rsv->usage) == 0) {
4322 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4324 if (block_rsv->freed[0] == 0 &&
4325 block_rsv->freed[1] == 0) {
4326 list_del_init(&block_rsv->list);
4330 btrfs_block_rsv_release(root, block_rsv, 0);
4333 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4338 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4339 struct btrfs_root *root,
4340 u64 bytenr, u64 num_bytes, u64 parent,
4341 u64 root_objectid, u64 owner_objectid,
4342 u64 owner_offset, int refs_to_drop,
4343 struct btrfs_delayed_extent_op *extent_op)
4345 struct btrfs_key key;
4346 struct btrfs_path *path;
4347 struct btrfs_fs_info *info = root->fs_info;
4348 struct btrfs_root *extent_root = info->extent_root;
4349 struct extent_buffer *leaf;
4350 struct btrfs_extent_item *ei;
4351 struct btrfs_extent_inline_ref *iref;
4354 int extent_slot = 0;
4355 int found_extent = 0;
4360 path = btrfs_alloc_path();
4365 path->leave_spinning = 1;
4367 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4368 BUG_ON(!is_data && refs_to_drop != 1);
4370 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4371 bytenr, num_bytes, parent,
4372 root_objectid, owner_objectid,
4375 extent_slot = path->slots[0];
4376 while (extent_slot >= 0) {
4377 btrfs_item_key_to_cpu(path->nodes[0], &key,
4379 if (key.objectid != bytenr)
4381 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4382 key.offset == num_bytes) {
4386 if (path->slots[0] - extent_slot > 5)
4390 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4391 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4392 if (found_extent && item_size < sizeof(*ei))
4395 if (!found_extent) {
4397 ret = remove_extent_backref(trans, extent_root, path,
4401 btrfs_release_path(path);
4402 path->leave_spinning = 1;
4404 key.objectid = bytenr;
4405 key.type = BTRFS_EXTENT_ITEM_KEY;
4406 key.offset = num_bytes;
4408 ret = btrfs_search_slot(trans, extent_root,
4411 printk(KERN_ERR "umm, got %d back from search"
4412 ", was looking for %llu\n", ret,
4413 (unsigned long long)bytenr);
4414 btrfs_print_leaf(extent_root, path->nodes[0]);
4417 extent_slot = path->slots[0];
4420 btrfs_print_leaf(extent_root, path->nodes[0]);
4422 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4423 "parent %llu root %llu owner %llu offset %llu\n",
4424 (unsigned long long)bytenr,
4425 (unsigned long long)parent,
4426 (unsigned long long)root_objectid,
4427 (unsigned long long)owner_objectid,
4428 (unsigned long long)owner_offset);
4431 leaf = path->nodes[0];
4432 item_size = btrfs_item_size_nr(leaf, extent_slot);
4433 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4434 if (item_size < sizeof(*ei)) {
4435 BUG_ON(found_extent || extent_slot != path->slots[0]);
4436 ret = convert_extent_item_v0(trans, extent_root, path,
4440 btrfs_release_path(path);
4441 path->leave_spinning = 1;
4443 key.objectid = bytenr;
4444 key.type = BTRFS_EXTENT_ITEM_KEY;
4445 key.offset = num_bytes;
4447 ret = btrfs_search_slot(trans, extent_root, &key, path,
4450 printk(KERN_ERR "umm, got %d back from search"
4451 ", was looking for %llu\n", ret,
4452 (unsigned long long)bytenr);
4453 btrfs_print_leaf(extent_root, path->nodes[0]);
4456 extent_slot = path->slots[0];
4457 leaf = path->nodes[0];
4458 item_size = btrfs_item_size_nr(leaf, extent_slot);
4461 BUG_ON(item_size < sizeof(*ei));
4462 ei = btrfs_item_ptr(leaf, extent_slot,
4463 struct btrfs_extent_item);
4464 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4465 struct btrfs_tree_block_info *bi;
4466 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4467 bi = (struct btrfs_tree_block_info *)(ei + 1);
4468 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4471 refs = btrfs_extent_refs(leaf, ei);
4472 BUG_ON(refs < refs_to_drop);
4473 refs -= refs_to_drop;
4477 __run_delayed_extent_op(extent_op, leaf, ei);
4479 * In the case of inline back ref, reference count will
4480 * be updated by remove_extent_backref
4483 BUG_ON(!found_extent);
4485 btrfs_set_extent_refs(leaf, ei, refs);
4486 btrfs_mark_buffer_dirty(leaf);
4489 ret = remove_extent_backref(trans, extent_root, path,
4496 BUG_ON(is_data && refs_to_drop !=
4497 extent_data_ref_count(root, path, iref));
4499 BUG_ON(path->slots[0] != extent_slot);
4501 BUG_ON(path->slots[0] != extent_slot + 1);
4502 path->slots[0] = extent_slot;
4507 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4510 btrfs_release_path(path);
4513 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4516 invalidate_mapping_pages(info->btree_inode->i_mapping,
4517 bytenr >> PAGE_CACHE_SHIFT,
4518 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4521 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4524 btrfs_free_path(path);
4529 * when we free an block, it is possible (and likely) that we free the last
4530 * delayed ref for that extent as well. This searches the delayed ref tree for
4531 * a given extent, and if there are no other delayed refs to be processed, it
4532 * removes it from the tree.
4534 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4535 struct btrfs_root *root, u64 bytenr)
4537 struct btrfs_delayed_ref_head *head;
4538 struct btrfs_delayed_ref_root *delayed_refs;
4539 struct btrfs_delayed_ref_node *ref;
4540 struct rb_node *node;
4543 delayed_refs = &trans->transaction->delayed_refs;
4544 spin_lock(&delayed_refs->lock);
4545 head = btrfs_find_delayed_ref_head(trans, bytenr);
4549 node = rb_prev(&head->node.rb_node);
4553 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4555 /* there are still entries for this ref, we can't drop it */
4556 if (ref->bytenr == bytenr)
4559 if (head->extent_op) {
4560 if (!head->must_insert_reserved)
4562 kfree(head->extent_op);
4563 head->extent_op = NULL;
4567 * waiting for the lock here would deadlock. If someone else has it
4568 * locked they are already in the process of dropping it anyway
4570 if (!mutex_trylock(&head->mutex))
4574 * at this point we have a head with no other entries. Go
4575 * ahead and process it.
4577 head->node.in_tree = 0;
4578 rb_erase(&head->node.rb_node, &delayed_refs->root);
4580 delayed_refs->num_entries--;
4583 * we don't take a ref on the node because we're removing it from the
4584 * tree, so we just steal the ref the tree was holding.
4586 delayed_refs->num_heads--;
4587 if (list_empty(&head->cluster))
4588 delayed_refs->num_heads_ready--;
4590 list_del_init(&head->cluster);
4591 spin_unlock(&delayed_refs->lock);
4593 BUG_ON(head->extent_op);
4594 if (head->must_insert_reserved)
4597 mutex_unlock(&head->mutex);
4598 btrfs_put_delayed_ref(&head->node);
4601 spin_unlock(&delayed_refs->lock);
4605 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4606 struct btrfs_root *root,
4607 struct extent_buffer *buf,
4608 u64 parent, int last_ref)
4610 struct btrfs_block_rsv *block_rsv;
4611 struct btrfs_block_group_cache *cache = NULL;
4614 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4615 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4616 parent, root->root_key.objectid,
4617 btrfs_header_level(buf),
4618 BTRFS_DROP_DELAYED_REF, NULL);
4625 block_rsv = get_block_rsv(trans, root);
4626 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4627 if (block_rsv->space_info != cache->space_info)
4630 if (btrfs_header_generation(buf) == trans->transid) {
4631 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4632 ret = check_ref_cleanup(trans, root, buf->start);
4637 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4638 pin_down_extent(root, cache, buf->start, buf->len, 1);
4642 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4644 btrfs_add_free_space(cache, buf->start, buf->len);
4645 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
4646 if (ret == -EAGAIN) {
4647 /* block group became read-only */
4648 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
4653 spin_lock(&block_rsv->lock);
4654 if (block_rsv->reserved < block_rsv->size) {
4655 block_rsv->reserved += buf->len;
4658 spin_unlock(&block_rsv->lock);
4661 spin_lock(&cache->space_info->lock);
4662 cache->space_info->bytes_reserved -= buf->len;
4663 cache->space_info->reservation_progress++;
4664 spin_unlock(&cache->space_info->lock);
4669 if (block_rsv->durable && !cache->ro) {
4671 spin_lock(&cache->lock);
4673 cache->reserved_pinned += buf->len;
4676 spin_unlock(&cache->lock);
4679 spin_lock(&block_rsv->lock);
4680 block_rsv->freed[trans->transid & 0x1] += buf->len;
4681 spin_unlock(&block_rsv->lock);
4686 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4689 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
4690 btrfs_put_block_group(cache);
4693 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4694 struct btrfs_root *root,
4695 u64 bytenr, u64 num_bytes, u64 parent,
4696 u64 root_objectid, u64 owner, u64 offset)
4701 * tree log blocks never actually go into the extent allocation
4702 * tree, just update pinning info and exit early.
4704 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4705 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4706 /* unlocks the pinned mutex */
4707 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4709 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4710 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4711 parent, root_objectid, (int)owner,
4712 BTRFS_DROP_DELAYED_REF, NULL);
4715 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4716 parent, root_objectid, owner,
4717 offset, BTRFS_DROP_DELAYED_REF, NULL);
4723 static u64 stripe_align(struct btrfs_root *root, u64 val)
4725 u64 mask = ((u64)root->stripesize - 1);
4726 u64 ret = (val + mask) & ~mask;
4731 * when we wait for progress in the block group caching, its because
4732 * our allocation attempt failed at least once. So, we must sleep
4733 * and let some progress happen before we try again.
4735 * This function will sleep at least once waiting for new free space to
4736 * show up, and then it will check the block group free space numbers
4737 * for our min num_bytes. Another option is to have it go ahead
4738 * and look in the rbtree for a free extent of a given size, but this
4742 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4745 struct btrfs_caching_control *caching_ctl;
4748 caching_ctl = get_caching_control(cache);
4752 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4753 (cache->free_space_ctl->free_space >= num_bytes));
4755 put_caching_control(caching_ctl);
4760 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4762 struct btrfs_caching_control *caching_ctl;
4765 caching_ctl = get_caching_control(cache);
4769 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4771 put_caching_control(caching_ctl);
4775 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4778 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4780 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4782 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4784 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4791 enum btrfs_loop_type {
4792 LOOP_FIND_IDEAL = 0,
4793 LOOP_CACHING_NOWAIT = 1,
4794 LOOP_CACHING_WAIT = 2,
4795 LOOP_ALLOC_CHUNK = 3,
4796 LOOP_NO_EMPTY_SIZE = 4,
4800 * walks the btree of allocated extents and find a hole of a given size.
4801 * The key ins is changed to record the hole:
4802 * ins->objectid == block start
4803 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4804 * ins->offset == number of blocks
4805 * Any available blocks before search_start are skipped.
4807 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4808 struct btrfs_root *orig_root,
4809 u64 num_bytes, u64 empty_size,
4810 u64 search_start, u64 search_end,
4811 u64 hint_byte, struct btrfs_key *ins,
4815 struct btrfs_root *root = orig_root->fs_info->extent_root;
4816 struct btrfs_free_cluster *last_ptr = NULL;
4817 struct btrfs_block_group_cache *block_group = NULL;
4818 int empty_cluster = 2 * 1024 * 1024;
4819 int allowed_chunk_alloc = 0;
4820 int done_chunk_alloc = 0;
4821 struct btrfs_space_info *space_info;
4822 int last_ptr_loop = 0;
4825 bool found_uncached_bg = false;
4826 bool failed_cluster_refill = false;
4827 bool failed_alloc = false;
4828 bool use_cluster = true;
4829 u64 ideal_cache_percent = 0;
4830 u64 ideal_cache_offset = 0;
4832 WARN_ON(num_bytes < root->sectorsize);
4833 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4837 space_info = __find_space_info(root->fs_info, data);
4839 printk(KERN_ERR "No space info for %d\n", data);
4844 * If the space info is for both data and metadata it means we have a
4845 * small filesystem and we can't use the clustering stuff.
4847 if (btrfs_mixed_space_info(space_info))
4848 use_cluster = false;
4850 if (orig_root->ref_cows || empty_size)
4851 allowed_chunk_alloc = 1;
4853 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
4854 last_ptr = &root->fs_info->meta_alloc_cluster;
4855 if (!btrfs_test_opt(root, SSD))
4856 empty_cluster = 64 * 1024;
4859 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
4860 btrfs_test_opt(root, SSD)) {
4861 last_ptr = &root->fs_info->data_alloc_cluster;
4865 spin_lock(&last_ptr->lock);
4866 if (last_ptr->block_group)
4867 hint_byte = last_ptr->window_start;
4868 spin_unlock(&last_ptr->lock);
4871 search_start = max(search_start, first_logical_byte(root, 0));
4872 search_start = max(search_start, hint_byte);
4877 if (search_start == hint_byte) {
4879 block_group = btrfs_lookup_block_group(root->fs_info,
4882 * we don't want to use the block group if it doesn't match our
4883 * allocation bits, or if its not cached.
4885 * However if we are re-searching with an ideal block group
4886 * picked out then we don't care that the block group is cached.
4888 if (block_group && block_group_bits(block_group, data) &&
4889 (block_group->cached != BTRFS_CACHE_NO ||
4890 search_start == ideal_cache_offset)) {
4891 down_read(&space_info->groups_sem);
4892 if (list_empty(&block_group->list) ||
4895 * someone is removing this block group,
4896 * we can't jump into the have_block_group
4897 * target because our list pointers are not
4900 btrfs_put_block_group(block_group);
4901 up_read(&space_info->groups_sem);
4903 index = get_block_group_index(block_group);
4904 goto have_block_group;
4906 } else if (block_group) {
4907 btrfs_put_block_group(block_group);
4911 down_read(&space_info->groups_sem);
4912 list_for_each_entry(block_group, &space_info->block_groups[index],
4917 btrfs_get_block_group(block_group);
4918 search_start = block_group->key.objectid;
4921 * this can happen if we end up cycling through all the
4922 * raid types, but we want to make sure we only allocate
4923 * for the proper type.
4925 if (!block_group_bits(block_group, data)) {
4926 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4927 BTRFS_BLOCK_GROUP_RAID1 |
4928 BTRFS_BLOCK_GROUP_RAID10;
4931 * if they asked for extra copies and this block group
4932 * doesn't provide them, bail. This does allow us to
4933 * fill raid0 from raid1.
4935 if ((data & extra) && !(block_group->flags & extra))
4940 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
4943 ret = cache_block_group(block_group, trans,
4945 if (block_group->cached == BTRFS_CACHE_FINISHED)
4946 goto have_block_group;
4948 free_percent = btrfs_block_group_used(&block_group->item);
4949 free_percent *= 100;
4950 free_percent = div64_u64(free_percent,
4951 block_group->key.offset);
4952 free_percent = 100 - free_percent;
4953 if (free_percent > ideal_cache_percent &&
4954 likely(!block_group->ro)) {
4955 ideal_cache_offset = block_group->key.objectid;
4956 ideal_cache_percent = free_percent;
4960 * We only want to start kthread caching if we are at
4961 * the point where we will wait for caching to make
4962 * progress, or if our ideal search is over and we've
4963 * found somebody to start caching.
4965 if (loop > LOOP_CACHING_NOWAIT ||
4966 (loop > LOOP_FIND_IDEAL &&
4967 atomic_read(&space_info->caching_threads) < 2)) {
4968 ret = cache_block_group(block_group, trans,
4972 found_uncached_bg = true;
4975 * If loop is set for cached only, try the next block
4978 if (loop == LOOP_FIND_IDEAL)
4982 cached = block_group_cache_done(block_group);
4983 if (unlikely(!cached))
4984 found_uncached_bg = true;
4986 if (unlikely(block_group->ro))
4990 * Ok we want to try and use the cluster allocator, so lets look
4991 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
4992 * have tried the cluster allocator plenty of times at this
4993 * point and not have found anything, so we are likely way too
4994 * fragmented for the clustering stuff to find anything, so lets
4995 * just skip it and let the allocator find whatever block it can
4998 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
5000 * the refill lock keeps out other
5001 * people trying to start a new cluster
5003 spin_lock(&last_ptr->refill_lock);
5004 if (last_ptr->block_group &&
5005 (last_ptr->block_group->ro ||
5006 !block_group_bits(last_ptr->block_group, data))) {
5008 goto refill_cluster;
5011 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5012 num_bytes, search_start);
5014 /* we have a block, we're done */
5015 spin_unlock(&last_ptr->refill_lock);
5019 spin_lock(&last_ptr->lock);
5021 * whoops, this cluster doesn't actually point to
5022 * this block group. Get a ref on the block
5023 * group is does point to and try again
5025 if (!last_ptr_loop && last_ptr->block_group &&
5026 last_ptr->block_group != block_group) {
5028 btrfs_put_block_group(block_group);
5029 block_group = last_ptr->block_group;
5030 btrfs_get_block_group(block_group);
5031 spin_unlock(&last_ptr->lock);
5032 spin_unlock(&last_ptr->refill_lock);
5035 search_start = block_group->key.objectid;
5037 * we know this block group is properly
5038 * in the list because
5039 * btrfs_remove_block_group, drops the
5040 * cluster before it removes the block
5041 * group from the list
5043 goto have_block_group;
5045 spin_unlock(&last_ptr->lock);
5048 * this cluster didn't work out, free it and
5051 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5055 /* allocate a cluster in this block group */
5056 ret = btrfs_find_space_cluster(trans, root,
5057 block_group, last_ptr,
5059 empty_cluster + empty_size);
5062 * now pull our allocation out of this
5065 offset = btrfs_alloc_from_cluster(block_group,
5066 last_ptr, num_bytes,
5069 /* we found one, proceed */
5070 spin_unlock(&last_ptr->refill_lock);
5073 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5074 && !failed_cluster_refill) {
5075 spin_unlock(&last_ptr->refill_lock);
5077 failed_cluster_refill = true;
5078 wait_block_group_cache_progress(block_group,
5079 num_bytes + empty_cluster + empty_size);
5080 goto have_block_group;
5084 * at this point we either didn't find a cluster
5085 * or we weren't able to allocate a block from our
5086 * cluster. Free the cluster we've been trying
5087 * to use, and go to the next block group
5089 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5090 spin_unlock(&last_ptr->refill_lock);
5094 offset = btrfs_find_space_for_alloc(block_group, search_start,
5095 num_bytes, empty_size);
5097 * If we didn't find a chunk, and we haven't failed on this
5098 * block group before, and this block group is in the middle of
5099 * caching and we are ok with waiting, then go ahead and wait
5100 * for progress to be made, and set failed_alloc to true.
5102 * If failed_alloc is true then we've already waited on this
5103 * block group once and should move on to the next block group.
5105 if (!offset && !failed_alloc && !cached &&
5106 loop > LOOP_CACHING_NOWAIT) {
5107 wait_block_group_cache_progress(block_group,
5108 num_bytes + empty_size);
5109 failed_alloc = true;
5110 goto have_block_group;
5111 } else if (!offset) {
5115 search_start = stripe_align(root, offset);
5116 /* move on to the next group */
5117 if (search_start + num_bytes >= search_end) {
5118 btrfs_add_free_space(block_group, offset, num_bytes);
5122 /* move on to the next group */
5123 if (search_start + num_bytes >
5124 block_group->key.objectid + block_group->key.offset) {
5125 btrfs_add_free_space(block_group, offset, num_bytes);
5129 ins->objectid = search_start;
5130 ins->offset = num_bytes;
5132 if (offset < search_start)
5133 btrfs_add_free_space(block_group, offset,
5134 search_start - offset);
5135 BUG_ON(offset > search_start);
5137 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
5138 (data & BTRFS_BLOCK_GROUP_DATA));
5139 if (ret == -EAGAIN) {
5140 btrfs_add_free_space(block_group, offset, num_bytes);
5144 /* we are all good, lets return */
5145 ins->objectid = search_start;
5146 ins->offset = num_bytes;
5148 if (offset < search_start)
5149 btrfs_add_free_space(block_group, offset,
5150 search_start - offset);
5151 BUG_ON(offset > search_start);
5154 failed_cluster_refill = false;
5155 failed_alloc = false;
5156 BUG_ON(index != get_block_group_index(block_group));
5157 btrfs_put_block_group(block_group);
5159 up_read(&space_info->groups_sem);
5161 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5164 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5165 * for them to make caching progress. Also
5166 * determine the best possible bg to cache
5167 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5168 * caching kthreads as we move along
5169 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5170 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5171 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5174 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5175 (found_uncached_bg || empty_size || empty_cluster ||
5176 allowed_chunk_alloc)) {
5178 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5179 found_uncached_bg = false;
5181 if (!ideal_cache_percent &&
5182 atomic_read(&space_info->caching_threads))
5186 * 1 of the following 2 things have happened so far
5188 * 1) We found an ideal block group for caching that
5189 * is mostly full and will cache quickly, so we might
5190 * as well wait for it.
5192 * 2) We searched for cached only and we didn't find
5193 * anything, and we didn't start any caching kthreads
5194 * either, so chances are we will loop through and
5195 * start a couple caching kthreads, and then come back
5196 * around and just wait for them. This will be slower
5197 * because we will have 2 caching kthreads reading at
5198 * the same time when we could have just started one
5199 * and waited for it to get far enough to give us an
5200 * allocation, so go ahead and go to the wait caching
5203 loop = LOOP_CACHING_WAIT;
5204 search_start = ideal_cache_offset;
5205 ideal_cache_percent = 0;
5207 } else if (loop == LOOP_FIND_IDEAL) {
5209 * Didn't find a uncached bg, wait on anything we find
5212 loop = LOOP_CACHING_WAIT;
5216 if (loop < LOOP_CACHING_WAIT) {
5221 if (loop == LOOP_ALLOC_CHUNK) {
5226 if (allowed_chunk_alloc) {
5227 ret = do_chunk_alloc(trans, root, num_bytes +
5228 2 * 1024 * 1024, data,
5229 CHUNK_ALLOC_LIMITED);
5230 allowed_chunk_alloc = 0;
5231 done_chunk_alloc = 1;
5232 } else if (!done_chunk_alloc &&
5233 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5234 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5237 if (loop < LOOP_NO_EMPTY_SIZE) {
5242 } else if (!ins->objectid) {
5246 /* we found what we needed */
5247 if (ins->objectid) {
5248 if (!(data & BTRFS_BLOCK_GROUP_DATA))
5249 trans->block_group = block_group->key.objectid;
5251 btrfs_put_block_group(block_group);
5258 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5259 int dump_block_groups)
5261 struct btrfs_block_group_cache *cache;
5264 spin_lock(&info->lock);
5265 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5266 (unsigned long long)(info->total_bytes - info->bytes_used -
5267 info->bytes_pinned - info->bytes_reserved -
5268 info->bytes_readonly),
5269 (info->full) ? "" : "not ");
5270 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5271 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5272 (unsigned long long)info->total_bytes,
5273 (unsigned long long)info->bytes_used,
5274 (unsigned long long)info->bytes_pinned,
5275 (unsigned long long)info->bytes_reserved,
5276 (unsigned long long)info->bytes_may_use,
5277 (unsigned long long)info->bytes_readonly);
5278 spin_unlock(&info->lock);
5280 if (!dump_block_groups)
5283 down_read(&info->groups_sem);
5285 list_for_each_entry(cache, &info->block_groups[index], list) {
5286 spin_lock(&cache->lock);
5287 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5288 "%llu pinned %llu reserved\n",
5289 (unsigned long long)cache->key.objectid,
5290 (unsigned long long)cache->key.offset,
5291 (unsigned long long)btrfs_block_group_used(&cache->item),
5292 (unsigned long long)cache->pinned,
5293 (unsigned long long)cache->reserved);
5294 btrfs_dump_free_space(cache, bytes);
5295 spin_unlock(&cache->lock);
5297 if (++index < BTRFS_NR_RAID_TYPES)
5299 up_read(&info->groups_sem);
5302 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5303 struct btrfs_root *root,
5304 u64 num_bytes, u64 min_alloc_size,
5305 u64 empty_size, u64 hint_byte,
5306 u64 search_end, struct btrfs_key *ins,
5310 u64 search_start = 0;
5312 data = btrfs_get_alloc_profile(root, data);
5315 * the only place that sets empty_size is btrfs_realloc_node, which
5316 * is not called recursively on allocations
5318 if (empty_size || root->ref_cows)
5319 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5320 num_bytes + 2 * 1024 * 1024, data,
5321 CHUNK_ALLOC_NO_FORCE);
5323 WARN_ON(num_bytes < root->sectorsize);
5324 ret = find_free_extent(trans, root, num_bytes, empty_size,
5325 search_start, search_end, hint_byte,
5328 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5329 num_bytes = num_bytes >> 1;
5330 num_bytes = num_bytes & ~(root->sectorsize - 1);
5331 num_bytes = max(num_bytes, min_alloc_size);
5332 do_chunk_alloc(trans, root->fs_info->extent_root,
5333 num_bytes, data, CHUNK_ALLOC_FORCE);
5336 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
5337 struct btrfs_space_info *sinfo;
5339 sinfo = __find_space_info(root->fs_info, data);
5340 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5341 "wanted %llu\n", (unsigned long long)data,
5342 (unsigned long long)num_bytes);
5343 dump_space_info(sinfo, num_bytes, 1);
5346 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5351 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5353 struct btrfs_block_group_cache *cache;
5356 cache = btrfs_lookup_block_group(root->fs_info, start);
5358 printk(KERN_ERR "Unable to find block group for %llu\n",
5359 (unsigned long long)start);
5363 if (btrfs_test_opt(root, DISCARD))
5364 ret = btrfs_discard_extent(root, start, len, NULL);
5366 btrfs_add_free_space(cache, start, len);
5367 btrfs_update_reserved_bytes(cache, len, 0, 1);
5368 btrfs_put_block_group(cache);
5370 trace_btrfs_reserved_extent_free(root, start, len);
5375 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5376 struct btrfs_root *root,
5377 u64 parent, u64 root_objectid,
5378 u64 flags, u64 owner, u64 offset,
5379 struct btrfs_key *ins, int ref_mod)
5382 struct btrfs_fs_info *fs_info = root->fs_info;
5383 struct btrfs_extent_item *extent_item;
5384 struct btrfs_extent_inline_ref *iref;
5385 struct btrfs_path *path;
5386 struct extent_buffer *leaf;
5391 type = BTRFS_SHARED_DATA_REF_KEY;
5393 type = BTRFS_EXTENT_DATA_REF_KEY;
5395 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5397 path = btrfs_alloc_path();
5401 path->leave_spinning = 1;
5402 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5406 leaf = path->nodes[0];
5407 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5408 struct btrfs_extent_item);
5409 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5410 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5411 btrfs_set_extent_flags(leaf, extent_item,
5412 flags | BTRFS_EXTENT_FLAG_DATA);
5414 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5415 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5417 struct btrfs_shared_data_ref *ref;
5418 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5419 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5420 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5422 struct btrfs_extent_data_ref *ref;
5423 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5424 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5425 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5426 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5427 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5430 btrfs_mark_buffer_dirty(path->nodes[0]);
5431 btrfs_free_path(path);
5433 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5435 printk(KERN_ERR "btrfs update block group failed for %llu "
5436 "%llu\n", (unsigned long long)ins->objectid,
5437 (unsigned long long)ins->offset);
5443 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5444 struct btrfs_root *root,
5445 u64 parent, u64 root_objectid,
5446 u64 flags, struct btrfs_disk_key *key,
5447 int level, struct btrfs_key *ins)
5450 struct btrfs_fs_info *fs_info = root->fs_info;
5451 struct btrfs_extent_item *extent_item;
5452 struct btrfs_tree_block_info *block_info;
5453 struct btrfs_extent_inline_ref *iref;
5454 struct btrfs_path *path;
5455 struct extent_buffer *leaf;
5456 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5458 path = btrfs_alloc_path();
5461 path->leave_spinning = 1;
5462 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5466 leaf = path->nodes[0];
5467 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5468 struct btrfs_extent_item);
5469 btrfs_set_extent_refs(leaf, extent_item, 1);
5470 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5471 btrfs_set_extent_flags(leaf, extent_item,
5472 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5473 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5475 btrfs_set_tree_block_key(leaf, block_info, key);
5476 btrfs_set_tree_block_level(leaf, block_info, level);
5478 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5480 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5481 btrfs_set_extent_inline_ref_type(leaf, iref,
5482 BTRFS_SHARED_BLOCK_REF_KEY);
5483 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5485 btrfs_set_extent_inline_ref_type(leaf, iref,
5486 BTRFS_TREE_BLOCK_REF_KEY);
5487 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5490 btrfs_mark_buffer_dirty(leaf);
5491 btrfs_free_path(path);
5493 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5495 printk(KERN_ERR "btrfs update block group failed for %llu "
5496 "%llu\n", (unsigned long long)ins->objectid,
5497 (unsigned long long)ins->offset);
5503 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5504 struct btrfs_root *root,
5505 u64 root_objectid, u64 owner,
5506 u64 offset, struct btrfs_key *ins)
5510 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5512 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5513 0, root_objectid, owner, offset,
5514 BTRFS_ADD_DELAYED_EXTENT, NULL);
5519 * this is used by the tree logging recovery code. It records that
5520 * an extent has been allocated and makes sure to clear the free
5521 * space cache bits as well
5523 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5524 struct btrfs_root *root,
5525 u64 root_objectid, u64 owner, u64 offset,
5526 struct btrfs_key *ins)
5529 struct btrfs_block_group_cache *block_group;
5530 struct btrfs_caching_control *caching_ctl;
5531 u64 start = ins->objectid;
5532 u64 num_bytes = ins->offset;
5534 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5535 cache_block_group(block_group, trans, NULL, 0);
5536 caching_ctl = get_caching_control(block_group);
5539 BUG_ON(!block_group_cache_done(block_group));
5540 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5543 mutex_lock(&caching_ctl->mutex);
5545 if (start >= caching_ctl->progress) {
5546 ret = add_excluded_extent(root, start, num_bytes);
5548 } else if (start + num_bytes <= caching_ctl->progress) {
5549 ret = btrfs_remove_free_space(block_group,
5553 num_bytes = caching_ctl->progress - start;
5554 ret = btrfs_remove_free_space(block_group,
5558 start = caching_ctl->progress;
5559 num_bytes = ins->objectid + ins->offset -
5560 caching_ctl->progress;
5561 ret = add_excluded_extent(root, start, num_bytes);
5565 mutex_unlock(&caching_ctl->mutex);
5566 put_caching_control(caching_ctl);
5569 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
5571 btrfs_put_block_group(block_group);
5572 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5573 0, owner, offset, ins, 1);
5577 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5578 struct btrfs_root *root,
5579 u64 bytenr, u32 blocksize,
5582 struct extent_buffer *buf;
5584 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5586 return ERR_PTR(-ENOMEM);
5587 btrfs_set_header_generation(buf, trans->transid);
5588 btrfs_set_buffer_lockdep_class(buf, level);
5589 btrfs_tree_lock(buf);
5590 clean_tree_block(trans, root, buf);
5592 btrfs_set_lock_blocking(buf);
5593 btrfs_set_buffer_uptodate(buf);
5595 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5597 * we allow two log transactions at a time, use different
5598 * EXENT bit to differentiate dirty pages.
5600 if (root->log_transid % 2 == 0)
5601 set_extent_dirty(&root->dirty_log_pages, buf->start,
5602 buf->start + buf->len - 1, GFP_NOFS);
5604 set_extent_new(&root->dirty_log_pages, buf->start,
5605 buf->start + buf->len - 1, GFP_NOFS);
5607 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5608 buf->start + buf->len - 1, GFP_NOFS);
5610 trans->blocks_used++;
5611 /* this returns a buffer locked for blocking */
5615 static struct btrfs_block_rsv *
5616 use_block_rsv(struct btrfs_trans_handle *trans,
5617 struct btrfs_root *root, u32 blocksize)
5619 struct btrfs_block_rsv *block_rsv;
5620 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
5623 block_rsv = get_block_rsv(trans, root);
5625 if (block_rsv->size == 0) {
5626 ret = reserve_metadata_bytes(trans, root, block_rsv,
5629 * If we couldn't reserve metadata bytes try and use some from
5630 * the global reserve.
5632 if (ret && block_rsv != global_rsv) {
5633 ret = block_rsv_use_bytes(global_rsv, blocksize);
5636 return ERR_PTR(ret);
5638 return ERR_PTR(ret);
5643 ret = block_rsv_use_bytes(block_rsv, blocksize);
5648 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5651 spin_lock(&block_rsv->lock);
5652 block_rsv->size += blocksize;
5653 spin_unlock(&block_rsv->lock);
5655 } else if (ret && block_rsv != global_rsv) {
5656 ret = block_rsv_use_bytes(global_rsv, blocksize);
5662 return ERR_PTR(-ENOSPC);
5665 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5667 block_rsv_add_bytes(block_rsv, blocksize, 0);
5668 block_rsv_release_bytes(block_rsv, NULL, 0);
5672 * finds a free extent and does all the dirty work required for allocation
5673 * returns the key for the extent through ins, and a tree buffer for
5674 * the first block of the extent through buf.
5676 * returns the tree buffer or NULL.
5678 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5679 struct btrfs_root *root, u32 blocksize,
5680 u64 parent, u64 root_objectid,
5681 struct btrfs_disk_key *key, int level,
5682 u64 hint, u64 empty_size)
5684 struct btrfs_key ins;
5685 struct btrfs_block_rsv *block_rsv;
5686 struct extent_buffer *buf;
5691 block_rsv = use_block_rsv(trans, root, blocksize);
5692 if (IS_ERR(block_rsv))
5693 return ERR_CAST(block_rsv);
5695 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5696 empty_size, hint, (u64)-1, &ins, 0);
5698 unuse_block_rsv(block_rsv, blocksize);
5699 return ERR_PTR(ret);
5702 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5704 BUG_ON(IS_ERR(buf));
5706 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5708 parent = ins.objectid;
5709 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5713 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5714 struct btrfs_delayed_extent_op *extent_op;
5715 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5718 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5720 memset(&extent_op->key, 0, sizeof(extent_op->key));
5721 extent_op->flags_to_set = flags;
5722 extent_op->update_key = 1;
5723 extent_op->update_flags = 1;
5724 extent_op->is_data = 0;
5726 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5727 ins.offset, parent, root_objectid,
5728 level, BTRFS_ADD_DELAYED_EXTENT,
5735 struct walk_control {
5736 u64 refs[BTRFS_MAX_LEVEL];
5737 u64 flags[BTRFS_MAX_LEVEL];
5738 struct btrfs_key update_progress;
5748 #define DROP_REFERENCE 1
5749 #define UPDATE_BACKREF 2
5751 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5752 struct btrfs_root *root,
5753 struct walk_control *wc,
5754 struct btrfs_path *path)
5762 struct btrfs_key key;
5763 struct extent_buffer *eb;
5768 if (path->slots[wc->level] < wc->reada_slot) {
5769 wc->reada_count = wc->reada_count * 2 / 3;
5770 wc->reada_count = max(wc->reada_count, 2);
5772 wc->reada_count = wc->reada_count * 3 / 2;
5773 wc->reada_count = min_t(int, wc->reada_count,
5774 BTRFS_NODEPTRS_PER_BLOCK(root));
5777 eb = path->nodes[wc->level];
5778 nritems = btrfs_header_nritems(eb);
5779 blocksize = btrfs_level_size(root, wc->level - 1);
5781 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5782 if (nread >= wc->reada_count)
5786 bytenr = btrfs_node_blockptr(eb, slot);
5787 generation = btrfs_node_ptr_generation(eb, slot);
5789 if (slot == path->slots[wc->level])
5792 if (wc->stage == UPDATE_BACKREF &&
5793 generation <= root->root_key.offset)
5796 /* We don't lock the tree block, it's OK to be racy here */
5797 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5802 if (wc->stage == DROP_REFERENCE) {
5806 if (wc->level == 1 &&
5807 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5809 if (!wc->update_ref ||
5810 generation <= root->root_key.offset)
5812 btrfs_node_key_to_cpu(eb, &key, slot);
5813 ret = btrfs_comp_cpu_keys(&key,
5814 &wc->update_progress);
5818 if (wc->level == 1 &&
5819 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5823 ret = readahead_tree_block(root, bytenr, blocksize,
5829 wc->reada_slot = slot;
5833 * hepler to process tree block while walking down the tree.
5835 * when wc->stage == UPDATE_BACKREF, this function updates
5836 * back refs for pointers in the block.
5838 * NOTE: return value 1 means we should stop walking down.
5840 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5841 struct btrfs_root *root,
5842 struct btrfs_path *path,
5843 struct walk_control *wc, int lookup_info)
5845 int level = wc->level;
5846 struct extent_buffer *eb = path->nodes[level];
5847 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5850 if (wc->stage == UPDATE_BACKREF &&
5851 btrfs_header_owner(eb) != root->root_key.objectid)
5855 * when reference count of tree block is 1, it won't increase
5856 * again. once full backref flag is set, we never clear it.
5859 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
5860 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
5861 BUG_ON(!path->locks[level]);
5862 ret = btrfs_lookup_extent_info(trans, root,
5867 BUG_ON(wc->refs[level] == 0);
5870 if (wc->stage == DROP_REFERENCE) {
5871 if (wc->refs[level] > 1)
5874 if (path->locks[level] && !wc->keep_locks) {
5875 btrfs_tree_unlock(eb);
5876 path->locks[level] = 0;
5881 /* wc->stage == UPDATE_BACKREF */
5882 if (!(wc->flags[level] & flag)) {
5883 BUG_ON(!path->locks[level]);
5884 ret = btrfs_inc_ref(trans, root, eb, 1);
5886 ret = btrfs_dec_ref(trans, root, eb, 0);
5888 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
5891 wc->flags[level] |= flag;
5895 * the block is shared by multiple trees, so it's not good to
5896 * keep the tree lock
5898 if (path->locks[level] && level > 0) {
5899 btrfs_tree_unlock(eb);
5900 path->locks[level] = 0;
5906 * hepler to process tree block pointer.
5908 * when wc->stage == DROP_REFERENCE, this function checks
5909 * reference count of the block pointed to. if the block
5910 * is shared and we need update back refs for the subtree
5911 * rooted at the block, this function changes wc->stage to
5912 * UPDATE_BACKREF. if the block is shared and there is no
5913 * need to update back, this function drops the reference
5916 * NOTE: return value 1 means we should stop walking down.
5918 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5919 struct btrfs_root *root,
5920 struct btrfs_path *path,
5921 struct walk_control *wc, int *lookup_info)
5927 struct btrfs_key key;
5928 struct extent_buffer *next;
5929 int level = wc->level;
5933 generation = btrfs_node_ptr_generation(path->nodes[level],
5934 path->slots[level]);
5936 * if the lower level block was created before the snapshot
5937 * was created, we know there is no need to update back refs
5940 if (wc->stage == UPDATE_BACKREF &&
5941 generation <= root->root_key.offset) {
5946 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5947 blocksize = btrfs_level_size(root, level - 1);
5949 next = btrfs_find_tree_block(root, bytenr, blocksize);
5951 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
5956 btrfs_tree_lock(next);
5957 btrfs_set_lock_blocking(next);
5959 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5960 &wc->refs[level - 1],
5961 &wc->flags[level - 1]);
5963 BUG_ON(wc->refs[level - 1] == 0);
5966 if (wc->stage == DROP_REFERENCE) {
5967 if (wc->refs[level - 1] > 1) {
5969 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5972 if (!wc->update_ref ||
5973 generation <= root->root_key.offset)
5976 btrfs_node_key_to_cpu(path->nodes[level], &key,
5977 path->slots[level]);
5978 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5982 wc->stage = UPDATE_BACKREF;
5983 wc->shared_level = level - 1;
5987 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5991 if (!btrfs_buffer_uptodate(next, generation)) {
5992 btrfs_tree_unlock(next);
5993 free_extent_buffer(next);
5999 if (reada && level == 1)
6000 reada_walk_down(trans, root, wc, path);
6001 next = read_tree_block(root, bytenr, blocksize, generation);
6004 btrfs_tree_lock(next);
6005 btrfs_set_lock_blocking(next);
6009 BUG_ON(level != btrfs_header_level(next));
6010 path->nodes[level] = next;
6011 path->slots[level] = 0;
6012 path->locks[level] = 1;
6018 wc->refs[level - 1] = 0;
6019 wc->flags[level - 1] = 0;
6020 if (wc->stage == DROP_REFERENCE) {
6021 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6022 parent = path->nodes[level]->start;
6024 BUG_ON(root->root_key.objectid !=
6025 btrfs_header_owner(path->nodes[level]));
6029 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6030 root->root_key.objectid, level - 1, 0);
6033 btrfs_tree_unlock(next);
6034 free_extent_buffer(next);
6040 * hepler to process tree block while walking up the tree.
6042 * when wc->stage == DROP_REFERENCE, this function drops
6043 * reference count on the block.
6045 * when wc->stage == UPDATE_BACKREF, this function changes
6046 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6047 * to UPDATE_BACKREF previously while processing the block.
6049 * NOTE: return value 1 means we should stop walking up.
6051 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6052 struct btrfs_root *root,
6053 struct btrfs_path *path,
6054 struct walk_control *wc)
6057 int level = wc->level;
6058 struct extent_buffer *eb = path->nodes[level];
6061 if (wc->stage == UPDATE_BACKREF) {
6062 BUG_ON(wc->shared_level < level);
6063 if (level < wc->shared_level)
6066 ret = find_next_key(path, level + 1, &wc->update_progress);
6070 wc->stage = DROP_REFERENCE;
6071 wc->shared_level = -1;
6072 path->slots[level] = 0;
6075 * check reference count again if the block isn't locked.
6076 * we should start walking down the tree again if reference
6079 if (!path->locks[level]) {
6081 btrfs_tree_lock(eb);
6082 btrfs_set_lock_blocking(eb);
6083 path->locks[level] = 1;
6085 ret = btrfs_lookup_extent_info(trans, root,
6090 BUG_ON(wc->refs[level] == 0);
6091 if (wc->refs[level] == 1) {
6092 btrfs_tree_unlock(eb);
6093 path->locks[level] = 0;
6099 /* wc->stage == DROP_REFERENCE */
6100 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6102 if (wc->refs[level] == 1) {
6104 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6105 ret = btrfs_dec_ref(trans, root, eb, 1);
6107 ret = btrfs_dec_ref(trans, root, eb, 0);
6110 /* make block locked assertion in clean_tree_block happy */
6111 if (!path->locks[level] &&
6112 btrfs_header_generation(eb) == trans->transid) {
6113 btrfs_tree_lock(eb);
6114 btrfs_set_lock_blocking(eb);
6115 path->locks[level] = 1;
6117 clean_tree_block(trans, root, eb);
6120 if (eb == root->node) {
6121 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6124 BUG_ON(root->root_key.objectid !=
6125 btrfs_header_owner(eb));
6127 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6128 parent = path->nodes[level + 1]->start;
6130 BUG_ON(root->root_key.objectid !=
6131 btrfs_header_owner(path->nodes[level + 1]));
6134 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
6136 wc->refs[level] = 0;
6137 wc->flags[level] = 0;
6141 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6142 struct btrfs_root *root,
6143 struct btrfs_path *path,
6144 struct walk_control *wc)
6146 int level = wc->level;
6147 int lookup_info = 1;
6150 while (level >= 0) {
6151 ret = walk_down_proc(trans, root, path, wc, lookup_info);
6158 if (path->slots[level] >=
6159 btrfs_header_nritems(path->nodes[level]))
6162 ret = do_walk_down(trans, root, path, wc, &lookup_info);
6164 path->slots[level]++;
6173 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6174 struct btrfs_root *root,
6175 struct btrfs_path *path,
6176 struct walk_control *wc, int max_level)
6178 int level = wc->level;
6181 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6182 while (level < max_level && path->nodes[level]) {
6184 if (path->slots[level] + 1 <
6185 btrfs_header_nritems(path->nodes[level])) {
6186 path->slots[level]++;
6189 ret = walk_up_proc(trans, root, path, wc);
6193 if (path->locks[level]) {
6194 btrfs_tree_unlock(path->nodes[level]);
6195 path->locks[level] = 0;
6197 free_extent_buffer(path->nodes[level]);
6198 path->nodes[level] = NULL;
6206 * drop a subvolume tree.
6208 * this function traverses the tree freeing any blocks that only
6209 * referenced by the tree.
6211 * when a shared tree block is found. this function decreases its
6212 * reference count by one. if update_ref is true, this function
6213 * also make sure backrefs for the shared block and all lower level
6214 * blocks are properly updated.
6216 int btrfs_drop_snapshot(struct btrfs_root *root,
6217 struct btrfs_block_rsv *block_rsv, int update_ref)
6219 struct btrfs_path *path;
6220 struct btrfs_trans_handle *trans;
6221 struct btrfs_root *tree_root = root->fs_info->tree_root;
6222 struct btrfs_root_item *root_item = &root->root_item;
6223 struct walk_control *wc;
6224 struct btrfs_key key;
6229 path = btrfs_alloc_path();
6232 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6235 trans = btrfs_start_transaction(tree_root, 0);
6236 BUG_ON(IS_ERR(trans));
6239 trans->block_rsv = block_rsv;
6241 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
6242 level = btrfs_header_level(root->node);
6243 path->nodes[level] = btrfs_lock_root_node(root);
6244 btrfs_set_lock_blocking(path->nodes[level]);
6245 path->slots[level] = 0;
6246 path->locks[level] = 1;
6247 memset(&wc->update_progress, 0,
6248 sizeof(wc->update_progress));
6250 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6251 memcpy(&wc->update_progress, &key,
6252 sizeof(wc->update_progress));
6254 level = root_item->drop_level;
6256 path->lowest_level = level;
6257 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6258 path->lowest_level = 0;
6266 * unlock our path, this is safe because only this
6267 * function is allowed to delete this snapshot
6269 btrfs_unlock_up_safe(path, 0);
6271 level = btrfs_header_level(root->node);
6273 btrfs_tree_lock(path->nodes[level]);
6274 btrfs_set_lock_blocking(path->nodes[level]);
6276 ret = btrfs_lookup_extent_info(trans, root,
6277 path->nodes[level]->start,
6278 path->nodes[level]->len,
6282 BUG_ON(wc->refs[level] == 0);
6284 if (level == root_item->drop_level)
6287 btrfs_tree_unlock(path->nodes[level]);
6288 WARN_ON(wc->refs[level] != 1);
6294 wc->shared_level = -1;
6295 wc->stage = DROP_REFERENCE;
6296 wc->update_ref = update_ref;
6298 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6301 ret = walk_down_tree(trans, root, path, wc);
6307 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6314 BUG_ON(wc->stage != DROP_REFERENCE);
6318 if (wc->stage == DROP_REFERENCE) {
6320 btrfs_node_key(path->nodes[level],
6321 &root_item->drop_progress,
6322 path->slots[level]);
6323 root_item->drop_level = level;
6326 BUG_ON(wc->level == 0);
6327 if (btrfs_should_end_transaction(trans, tree_root)) {
6328 ret = btrfs_update_root(trans, tree_root,
6333 btrfs_end_transaction_throttle(trans, tree_root);
6334 trans = btrfs_start_transaction(tree_root, 0);
6335 BUG_ON(IS_ERR(trans));
6337 trans->block_rsv = block_rsv;
6340 btrfs_release_path(path);
6343 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6346 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6347 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6351 /* if we fail to delete the orphan item this time
6352 * around, it'll get picked up the next time.
6354 * The most common failure here is just -ENOENT.
6356 btrfs_del_orphan_item(trans, tree_root,
6357 root->root_key.objectid);
6361 if (root->in_radix) {
6362 btrfs_free_fs_root(tree_root->fs_info, root);
6364 free_extent_buffer(root->node);
6365 free_extent_buffer(root->commit_root);
6369 btrfs_end_transaction_throttle(trans, tree_root);
6371 btrfs_free_path(path);
6376 * drop subtree rooted at tree block 'node'.
6378 * NOTE: this function will unlock and release tree block 'node'
6380 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6381 struct btrfs_root *root,
6382 struct extent_buffer *node,
6383 struct extent_buffer *parent)
6385 struct btrfs_path *path;
6386 struct walk_control *wc;
6392 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6394 path = btrfs_alloc_path();
6398 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6400 btrfs_free_path(path);
6404 btrfs_assert_tree_locked(parent);
6405 parent_level = btrfs_header_level(parent);
6406 extent_buffer_get(parent);
6407 path->nodes[parent_level] = parent;
6408 path->slots[parent_level] = btrfs_header_nritems(parent);
6410 btrfs_assert_tree_locked(node);
6411 level = btrfs_header_level(node);
6412 path->nodes[level] = node;
6413 path->slots[level] = 0;
6414 path->locks[level] = 1;
6416 wc->refs[parent_level] = 1;
6417 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6419 wc->shared_level = -1;
6420 wc->stage = DROP_REFERENCE;
6423 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6426 wret = walk_down_tree(trans, root, path, wc);
6432 wret = walk_up_tree(trans, root, path, wc, parent_level);
6440 btrfs_free_path(path);
6444 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
6447 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
6448 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
6451 * we add in the count of missing devices because we want
6452 * to make sure that any RAID levels on a degraded FS
6453 * continue to be honored.
6455 num_devices = root->fs_info->fs_devices->rw_devices +
6456 root->fs_info->fs_devices->missing_devices;
6458 if (num_devices == 1) {
6459 stripped |= BTRFS_BLOCK_GROUP_DUP;
6460 stripped = flags & ~stripped;
6462 /* turn raid0 into single device chunks */
6463 if (flags & BTRFS_BLOCK_GROUP_RAID0)
6466 /* turn mirroring into duplication */
6467 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
6468 BTRFS_BLOCK_GROUP_RAID10))
6469 return stripped | BTRFS_BLOCK_GROUP_DUP;
6472 /* they already had raid on here, just return */
6473 if (flags & stripped)
6476 stripped |= BTRFS_BLOCK_GROUP_DUP;
6477 stripped = flags & ~stripped;
6479 /* switch duplicated blocks with raid1 */
6480 if (flags & BTRFS_BLOCK_GROUP_DUP)
6481 return stripped | BTRFS_BLOCK_GROUP_RAID1;
6483 /* turn single device chunks into raid0 */
6484 return stripped | BTRFS_BLOCK_GROUP_RAID0;
6489 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
6491 struct btrfs_space_info *sinfo = cache->space_info;
6498 spin_lock(&sinfo->lock);
6499 spin_lock(&cache->lock);
6500 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6501 cache->bytes_super - btrfs_block_group_used(&cache->item);
6503 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
6504 sinfo->bytes_may_use + sinfo->bytes_readonly +
6505 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
6506 sinfo->bytes_readonly += num_bytes;
6507 sinfo->bytes_reserved += cache->reserved_pinned;
6508 cache->reserved_pinned = 0;
6513 spin_unlock(&cache->lock);
6514 spin_unlock(&sinfo->lock);
6518 int btrfs_set_block_group_ro(struct btrfs_root *root,
6519 struct btrfs_block_group_cache *cache)
6522 struct btrfs_trans_handle *trans;
6528 trans = btrfs_join_transaction(root, 1);
6529 BUG_ON(IS_ERR(trans));
6531 alloc_flags = update_block_group_flags(root, cache->flags);
6532 if (alloc_flags != cache->flags)
6533 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6536 ret = set_block_group_ro(cache);
6539 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
6540 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6544 ret = set_block_group_ro(cache);
6546 btrfs_end_transaction(trans, root);
6550 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
6551 struct btrfs_root *root, u64 type)
6553 u64 alloc_flags = get_alloc_profile(root, type);
6554 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
6559 * helper to account the unused space of all the readonly block group in the
6560 * list. takes mirrors into account.
6562 static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
6564 struct btrfs_block_group_cache *block_group;
6568 list_for_each_entry(block_group, groups_list, list) {
6569 spin_lock(&block_group->lock);
6571 if (!block_group->ro) {
6572 spin_unlock(&block_group->lock);
6576 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
6577 BTRFS_BLOCK_GROUP_RAID10 |
6578 BTRFS_BLOCK_GROUP_DUP))
6583 free_bytes += (block_group->key.offset -
6584 btrfs_block_group_used(&block_group->item)) *
6587 spin_unlock(&block_group->lock);
6594 * helper to account the unused space of all the readonly block group in the
6595 * space_info. takes mirrors into account.
6597 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
6602 spin_lock(&sinfo->lock);
6604 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
6605 if (!list_empty(&sinfo->block_groups[i]))
6606 free_bytes += __btrfs_get_ro_block_group_free_space(
6607 &sinfo->block_groups[i]);
6609 spin_unlock(&sinfo->lock);
6614 int btrfs_set_block_group_rw(struct btrfs_root *root,
6615 struct btrfs_block_group_cache *cache)
6617 struct btrfs_space_info *sinfo = cache->space_info;
6622 spin_lock(&sinfo->lock);
6623 spin_lock(&cache->lock);
6624 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
6625 cache->bytes_super - btrfs_block_group_used(&cache->item);
6626 sinfo->bytes_readonly -= num_bytes;
6628 spin_unlock(&cache->lock);
6629 spin_unlock(&sinfo->lock);
6634 * checks to see if its even possible to relocate this block group.
6636 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
6637 * ok to go ahead and try.
6639 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
6641 struct btrfs_block_group_cache *block_group;
6642 struct btrfs_space_info *space_info;
6643 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
6644 struct btrfs_device *device;
6648 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
6650 /* odd, couldn't find the block group, leave it alone */
6654 /* no bytes used, we're good */
6655 if (!btrfs_block_group_used(&block_group->item))
6658 space_info = block_group->space_info;
6659 spin_lock(&space_info->lock);
6661 full = space_info->full;
6664 * if this is the last block group we have in this space, we can't
6665 * relocate it unless we're able to allocate a new chunk below.
6667 * Otherwise, we need to make sure we have room in the space to handle
6668 * all of the extents from this block group. If we can, we're good
6670 if ((space_info->total_bytes != block_group->key.offset) &&
6671 (space_info->bytes_used + space_info->bytes_reserved +
6672 space_info->bytes_pinned + space_info->bytes_readonly +
6673 btrfs_block_group_used(&block_group->item) <
6674 space_info->total_bytes)) {
6675 spin_unlock(&space_info->lock);
6678 spin_unlock(&space_info->lock);
6681 * ok we don't have enough space, but maybe we have free space on our
6682 * devices to allocate new chunks for relocation, so loop through our
6683 * alloc devices and guess if we have enough space. However, if we
6684 * were marked as full, then we know there aren't enough chunks, and we
6691 mutex_lock(&root->fs_info->chunk_mutex);
6692 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
6693 u64 min_free = btrfs_block_group_used(&block_group->item);
6697 * check to make sure we can actually find a chunk with enough
6698 * space to fit our block group in.
6700 if (device->total_bytes > device->bytes_used + min_free) {
6701 ret = find_free_dev_extent(NULL, device, min_free,
6708 mutex_unlock(&root->fs_info->chunk_mutex);
6710 btrfs_put_block_group(block_group);
6714 static int find_first_block_group(struct btrfs_root *root,
6715 struct btrfs_path *path, struct btrfs_key *key)
6718 struct btrfs_key found_key;
6719 struct extent_buffer *leaf;
6722 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6727 slot = path->slots[0];
6728 leaf = path->nodes[0];
6729 if (slot >= btrfs_header_nritems(leaf)) {
6730 ret = btrfs_next_leaf(root, path);
6737 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6739 if (found_key.objectid >= key->objectid &&
6740 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6750 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
6752 struct btrfs_block_group_cache *block_group;
6756 struct inode *inode;
6758 block_group = btrfs_lookup_first_block_group(info, last);
6759 while (block_group) {
6760 spin_lock(&block_group->lock);
6761 if (block_group->iref)
6763 spin_unlock(&block_group->lock);
6764 block_group = next_block_group(info->tree_root,
6774 inode = block_group->inode;
6775 block_group->iref = 0;
6776 block_group->inode = NULL;
6777 spin_unlock(&block_group->lock);
6779 last = block_group->key.objectid + block_group->key.offset;
6780 btrfs_put_block_group(block_group);
6784 int btrfs_free_block_groups(struct btrfs_fs_info *info)
6786 struct btrfs_block_group_cache *block_group;
6787 struct btrfs_space_info *space_info;
6788 struct btrfs_caching_control *caching_ctl;
6791 down_write(&info->extent_commit_sem);
6792 while (!list_empty(&info->caching_block_groups)) {
6793 caching_ctl = list_entry(info->caching_block_groups.next,
6794 struct btrfs_caching_control, list);
6795 list_del(&caching_ctl->list);
6796 put_caching_control(caching_ctl);
6798 up_write(&info->extent_commit_sem);
6800 spin_lock(&info->block_group_cache_lock);
6801 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6802 block_group = rb_entry(n, struct btrfs_block_group_cache,
6804 rb_erase(&block_group->cache_node,
6805 &info->block_group_cache_tree);
6806 spin_unlock(&info->block_group_cache_lock);
6808 down_write(&block_group->space_info->groups_sem);
6809 list_del(&block_group->list);
6810 up_write(&block_group->space_info->groups_sem);
6812 if (block_group->cached == BTRFS_CACHE_STARTED)
6813 wait_block_group_cache_done(block_group);
6816 * We haven't cached this block group, which means we could
6817 * possibly have excluded extents on this block group.
6819 if (block_group->cached == BTRFS_CACHE_NO)
6820 free_excluded_extents(info->extent_root, block_group);
6822 btrfs_remove_free_space_cache(block_group);
6823 btrfs_put_block_group(block_group);
6825 spin_lock(&info->block_group_cache_lock);
6827 spin_unlock(&info->block_group_cache_lock);
6829 /* now that all the block groups are freed, go through and
6830 * free all the space_info structs. This is only called during
6831 * the final stages of unmount, and so we know nobody is
6832 * using them. We call synchronize_rcu() once before we start,
6833 * just to be on the safe side.
6837 release_global_block_rsv(info);
6839 while(!list_empty(&info->space_info)) {
6840 space_info = list_entry(info->space_info.next,
6841 struct btrfs_space_info,
6843 if (space_info->bytes_pinned > 0 ||
6844 space_info->bytes_reserved > 0) {
6846 dump_space_info(space_info, 0, 0);
6848 list_del(&space_info->list);
6854 static void __link_block_group(struct btrfs_space_info *space_info,
6855 struct btrfs_block_group_cache *cache)
6857 int index = get_block_group_index(cache);
6859 down_write(&space_info->groups_sem);
6860 list_add_tail(&cache->list, &space_info->block_groups[index]);
6861 up_write(&space_info->groups_sem);
6864 int btrfs_read_block_groups(struct btrfs_root *root)
6866 struct btrfs_path *path;
6868 struct btrfs_block_group_cache *cache;
6869 struct btrfs_fs_info *info = root->fs_info;
6870 struct btrfs_space_info *space_info;
6871 struct btrfs_key key;
6872 struct btrfs_key found_key;
6873 struct extent_buffer *leaf;
6877 root = info->extent_root;
6880 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
6881 path = btrfs_alloc_path();
6885 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
6886 if (cache_gen != 0 &&
6887 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
6889 if (btrfs_test_opt(root, CLEAR_CACHE))
6891 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
6892 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
6895 ret = find_first_block_group(root, path, &key);
6900 leaf = path->nodes[0];
6901 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6902 cache = kzalloc(sizeof(*cache), GFP_NOFS);
6907 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
6909 if (!cache->free_space_ctl) {
6915 atomic_set(&cache->count, 1);
6916 spin_lock_init(&cache->lock);
6917 cache->fs_info = info;
6918 INIT_LIST_HEAD(&cache->list);
6919 INIT_LIST_HEAD(&cache->cluster_list);
6922 cache->disk_cache_state = BTRFS_DC_CLEAR;
6924 read_extent_buffer(leaf, &cache->item,
6925 btrfs_item_ptr_offset(leaf, path->slots[0]),
6926 sizeof(cache->item));
6927 memcpy(&cache->key, &found_key, sizeof(found_key));
6929 key.objectid = found_key.objectid + found_key.offset;
6930 btrfs_release_path(path);
6931 cache->flags = btrfs_block_group_flags(&cache->item);
6932 cache->sectorsize = root->sectorsize;
6934 btrfs_init_free_space_ctl(cache);
6937 * We need to exclude the super stripes now so that the space
6938 * info has super bytes accounted for, otherwise we'll think
6939 * we have more space than we actually do.
6941 exclude_super_stripes(root, cache);
6944 * check for two cases, either we are full, and therefore
6945 * don't need to bother with the caching work since we won't
6946 * find any space, or we are empty, and we can just add all
6947 * the space in and be done with it. This saves us _alot_ of
6948 * time, particularly in the full case.
6950 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
6951 cache->last_byte_to_unpin = (u64)-1;
6952 cache->cached = BTRFS_CACHE_FINISHED;
6953 free_excluded_extents(root, cache);
6954 } else if (btrfs_block_group_used(&cache->item) == 0) {
6955 cache->last_byte_to_unpin = (u64)-1;
6956 cache->cached = BTRFS_CACHE_FINISHED;
6957 add_new_free_space(cache, root->fs_info,
6959 found_key.objectid +
6961 free_excluded_extents(root, cache);
6964 ret = update_space_info(info, cache->flags, found_key.offset,
6965 btrfs_block_group_used(&cache->item),
6968 cache->space_info = space_info;
6969 spin_lock(&cache->space_info->lock);
6970 cache->space_info->bytes_readonly += cache->bytes_super;
6971 spin_unlock(&cache->space_info->lock);
6973 __link_block_group(space_info, cache);
6975 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6978 set_avail_alloc_bits(root->fs_info, cache->flags);
6979 if (btrfs_chunk_readonly(root, cache->key.objectid))
6980 set_block_group_ro(cache);
6983 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
6984 if (!(get_alloc_profile(root, space_info->flags) &
6985 (BTRFS_BLOCK_GROUP_RAID10 |
6986 BTRFS_BLOCK_GROUP_RAID1 |
6987 BTRFS_BLOCK_GROUP_DUP)))
6990 * avoid allocating from un-mirrored block group if there are
6991 * mirrored block groups.
6993 list_for_each_entry(cache, &space_info->block_groups[3], list)
6994 set_block_group_ro(cache);
6995 list_for_each_entry(cache, &space_info->block_groups[4], list)
6996 set_block_group_ro(cache);
6999 init_global_block_rsv(info);
7002 btrfs_free_path(path);
7006 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
7007 struct btrfs_root *root, u64 bytes_used,
7008 u64 type, u64 chunk_objectid, u64 chunk_offset,
7012 struct btrfs_root *extent_root;
7013 struct btrfs_block_group_cache *cache;
7015 extent_root = root->fs_info->extent_root;
7017 root->fs_info->last_trans_log_full_commit = trans->transid;
7019 cache = kzalloc(sizeof(*cache), GFP_NOFS);
7022 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
7024 if (!cache->free_space_ctl) {
7029 cache->key.objectid = chunk_offset;
7030 cache->key.offset = size;
7031 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
7032 cache->sectorsize = root->sectorsize;
7033 cache->fs_info = root->fs_info;
7035 atomic_set(&cache->count, 1);
7036 spin_lock_init(&cache->lock);
7037 INIT_LIST_HEAD(&cache->list);
7038 INIT_LIST_HEAD(&cache->cluster_list);
7040 btrfs_init_free_space_ctl(cache);
7042 btrfs_set_block_group_used(&cache->item, bytes_used);
7043 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
7044 cache->flags = type;
7045 btrfs_set_block_group_flags(&cache->item, type);
7047 cache->last_byte_to_unpin = (u64)-1;
7048 cache->cached = BTRFS_CACHE_FINISHED;
7049 exclude_super_stripes(root, cache);
7051 add_new_free_space(cache, root->fs_info, chunk_offset,
7052 chunk_offset + size);
7054 free_excluded_extents(root, cache);
7056 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
7057 &cache->space_info);
7060 spin_lock(&cache->space_info->lock);
7061 cache->space_info->bytes_readonly += cache->bytes_super;
7062 spin_unlock(&cache->space_info->lock);
7064 __link_block_group(cache->space_info, cache);
7066 ret = btrfs_add_block_group_cache(root->fs_info, cache);
7069 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
7070 sizeof(cache->item));
7073 set_avail_alloc_bits(extent_root->fs_info, type);
7078 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
7079 struct btrfs_root *root, u64 group_start)
7081 struct btrfs_path *path;
7082 struct btrfs_block_group_cache *block_group;
7083 struct btrfs_free_cluster *cluster;
7084 struct btrfs_root *tree_root = root->fs_info->tree_root;
7085 struct btrfs_key key;
7086 struct inode *inode;
7090 root = root->fs_info->extent_root;
7092 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
7093 BUG_ON(!block_group);
7094 BUG_ON(!block_group->ro);
7097 * Free the reserved super bytes from this block group before
7100 free_excluded_extents(root, block_group);
7102 memcpy(&key, &block_group->key, sizeof(key));
7103 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
7104 BTRFS_BLOCK_GROUP_RAID1 |
7105 BTRFS_BLOCK_GROUP_RAID10))
7110 /* make sure this block group isn't part of an allocation cluster */
7111 cluster = &root->fs_info->data_alloc_cluster;
7112 spin_lock(&cluster->refill_lock);
7113 btrfs_return_cluster_to_free_space(block_group, cluster);
7114 spin_unlock(&cluster->refill_lock);
7117 * make sure this block group isn't part of a metadata
7118 * allocation cluster
7120 cluster = &root->fs_info->meta_alloc_cluster;
7121 spin_lock(&cluster->refill_lock);
7122 btrfs_return_cluster_to_free_space(block_group, cluster);
7123 spin_unlock(&cluster->refill_lock);
7125 path = btrfs_alloc_path();
7128 inode = lookup_free_space_inode(root, block_group, path);
7129 if (!IS_ERR(inode)) {
7130 btrfs_orphan_add(trans, inode);
7132 /* One for the block groups ref */
7133 spin_lock(&block_group->lock);
7134 if (block_group->iref) {
7135 block_group->iref = 0;
7136 block_group->inode = NULL;
7137 spin_unlock(&block_group->lock);
7140 spin_unlock(&block_group->lock);
7142 /* One for our lookup ref */
7146 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
7147 key.offset = block_group->key.objectid;
7150 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
7154 btrfs_release_path(path);
7156 ret = btrfs_del_item(trans, tree_root, path);
7159 btrfs_release_path(path);
7162 spin_lock(&root->fs_info->block_group_cache_lock);
7163 rb_erase(&block_group->cache_node,
7164 &root->fs_info->block_group_cache_tree);
7165 spin_unlock(&root->fs_info->block_group_cache_lock);
7167 down_write(&block_group->space_info->groups_sem);
7169 * we must use list_del_init so people can check to see if they
7170 * are still on the list after taking the semaphore
7172 list_del_init(&block_group->list);
7173 up_write(&block_group->space_info->groups_sem);
7175 if (block_group->cached == BTRFS_CACHE_STARTED)
7176 wait_block_group_cache_done(block_group);
7178 btrfs_remove_free_space_cache(block_group);
7180 spin_lock(&block_group->space_info->lock);
7181 block_group->space_info->total_bytes -= block_group->key.offset;
7182 block_group->space_info->bytes_readonly -= block_group->key.offset;
7183 block_group->space_info->disk_total -= block_group->key.offset * factor;
7184 spin_unlock(&block_group->space_info->lock);
7186 memcpy(&key, &block_group->key, sizeof(key));
7188 btrfs_clear_space_info_full(root->fs_info);
7190 btrfs_put_block_group(block_group);
7191 btrfs_put_block_group(block_group);
7193 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
7199 ret = btrfs_del_item(trans, root, path);
7201 btrfs_free_path(path);
7205 int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
7207 struct btrfs_space_info *space_info;
7208 struct btrfs_super_block *disk_super;
7214 disk_super = &fs_info->super_copy;
7215 if (!btrfs_super_root(disk_super))
7218 features = btrfs_super_incompat_flags(disk_super);
7219 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
7222 flags = BTRFS_BLOCK_GROUP_SYSTEM;
7223 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7228 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
7229 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7231 flags = BTRFS_BLOCK_GROUP_METADATA;
7232 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7236 flags = BTRFS_BLOCK_GROUP_DATA;
7237 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
7243 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
7245 return unpin_extent_range(root, start, end);
7248 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
7249 u64 num_bytes, u64 *actual_bytes)
7251 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
7254 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
7256 struct btrfs_fs_info *fs_info = root->fs_info;
7257 struct btrfs_block_group_cache *cache = NULL;
7264 cache = btrfs_lookup_block_group(fs_info, range->start);
7267 if (cache->key.objectid >= (range->start + range->len)) {
7268 btrfs_put_block_group(cache);
7272 start = max(range->start, cache->key.objectid);
7273 end = min(range->start + range->len,
7274 cache->key.objectid + cache->key.offset);
7276 if (end - start >= range->minlen) {
7277 if (!block_group_cache_done(cache)) {
7278 ret = cache_block_group(cache, NULL, root, 0);
7280 wait_block_group_cache_done(cache);
7282 ret = btrfs_trim_block_group(cache,
7288 trimmed += group_trimmed;
7290 btrfs_put_block_group(cache);
7295 cache = next_block_group(fs_info->tree_root, cache);
7298 range->len = trimmed;