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 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(extent_root, 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(root->fs_info->extent_root, path);
760 mutex_lock(&head->mutex);
761 mutex_unlock(&head->mutex);
762 btrfs_put_delayed_ref(&head->node);
765 if (head->extent_op && head->extent_op->update_flags)
766 extent_flags |= head->extent_op->flags_to_set;
768 BUG_ON(num_refs == 0);
770 num_refs += head->node.ref_mod;
771 mutex_unlock(&head->mutex);
773 spin_unlock(&delayed_refs->lock);
775 WARN_ON(num_refs == 0);
779 *flags = extent_flags;
781 btrfs_free_path(path);
786 * Back reference rules. Back refs have three main goals:
788 * 1) differentiate between all holders of references to an extent so that
789 * when a reference is dropped we can make sure it was a valid reference
790 * before freeing the extent.
792 * 2) Provide enough information to quickly find the holders of an extent
793 * if we notice a given block is corrupted or bad.
795 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
796 * maintenance. This is actually the same as #2, but with a slightly
797 * different use case.
799 * There are two kinds of back refs. The implicit back refs is optimized
800 * for pointers in non-shared tree blocks. For a given pointer in a block,
801 * back refs of this kind provide information about the block's owner tree
802 * and the pointer's key. These information allow us to find the block by
803 * b-tree searching. The full back refs is for pointers in tree blocks not
804 * referenced by their owner trees. The location of tree block is recorded
805 * in the back refs. Actually the full back refs is generic, and can be
806 * used in all cases the implicit back refs is used. The major shortcoming
807 * of the full back refs is its overhead. Every time a tree block gets
808 * COWed, we have to update back refs entry for all pointers in it.
810 * For a newly allocated tree block, we use implicit back refs for
811 * pointers in it. This means most tree related operations only involve
812 * implicit back refs. For a tree block created in old transaction, the
813 * only way to drop a reference to it is COW it. So we can detect the
814 * event that tree block loses its owner tree's reference and do the
815 * back refs conversion.
817 * When a tree block is COW'd through a tree, there are four cases:
819 * The reference count of the block is one and the tree is the block's
820 * owner tree. Nothing to do in this case.
822 * The reference count of the block is one and the tree is not the
823 * block's owner tree. In this case, full back refs is used for pointers
824 * in the block. Remove these full back refs, add implicit back refs for
825 * every pointers in the new block.
827 * The reference count of the block is greater than one and the tree is
828 * the block's owner tree. In this case, implicit back refs is used for
829 * pointers in the block. Add full back refs for every pointers in the
830 * block, increase lower level extents' reference counts. The original
831 * implicit back refs are entailed to the new block.
833 * The reference count of the block is greater than one and the tree is
834 * not the block's owner tree. Add implicit back refs for every pointer in
835 * the new block, increase lower level extents' reference count.
837 * Back Reference Key composing:
839 * The key objectid corresponds to the first byte in the extent,
840 * The key type is used to differentiate between types of back refs.
841 * There are different meanings of the key offset for different types
844 * File extents can be referenced by:
846 * - multiple snapshots, subvolumes, or different generations in one subvol
847 * - different files inside a single subvolume
848 * - different offsets inside a file (bookend extents in file.c)
850 * The extent ref structure for the implicit back refs has fields for:
852 * - Objectid of the subvolume root
853 * - objectid of the file holding the reference
854 * - original offset in the file
855 * - how many bookend extents
857 * The key offset for the implicit back refs is hash of the first
860 * The extent ref structure for the full back refs has field for:
862 * - number of pointers in the tree leaf
864 * The key offset for the implicit back refs is the first byte of
867 * When a file extent is allocated, The implicit back refs is used.
868 * the fields are filled in:
870 * (root_key.objectid, inode objectid, offset in file, 1)
872 * When a file extent is removed file truncation, we find the
873 * corresponding implicit back refs and check the following fields:
875 * (btrfs_header_owner(leaf), inode objectid, offset in file)
877 * Btree extents can be referenced by:
879 * - Different subvolumes
881 * Both the implicit back refs and the full back refs for tree blocks
882 * only consist of key. The key offset for the implicit back refs is
883 * objectid of block's owner tree. The key offset for the full back refs
884 * is the first byte of parent block.
886 * When implicit back refs is used, information about the lowest key and
887 * level of the tree block are required. These information are stored in
888 * tree block info structure.
891 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
892 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
893 struct btrfs_root *root,
894 struct btrfs_path *path,
895 u64 owner, u32 extra_size)
897 struct btrfs_extent_item *item;
898 struct btrfs_extent_item_v0 *ei0;
899 struct btrfs_extent_ref_v0 *ref0;
900 struct btrfs_tree_block_info *bi;
901 struct extent_buffer *leaf;
902 struct btrfs_key key;
903 struct btrfs_key found_key;
904 u32 new_size = sizeof(*item);
908 leaf = path->nodes[0];
909 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
911 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
912 ei0 = btrfs_item_ptr(leaf, path->slots[0],
913 struct btrfs_extent_item_v0);
914 refs = btrfs_extent_refs_v0(leaf, ei0);
916 if (owner == (u64)-1) {
918 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
919 ret = btrfs_next_leaf(root, path);
923 leaf = path->nodes[0];
925 btrfs_item_key_to_cpu(leaf, &found_key,
927 BUG_ON(key.objectid != found_key.objectid);
928 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
932 ref0 = btrfs_item_ptr(leaf, path->slots[0],
933 struct btrfs_extent_ref_v0);
934 owner = btrfs_ref_objectid_v0(leaf, ref0);
938 btrfs_release_path(root, path);
940 if (owner < BTRFS_FIRST_FREE_OBJECTID)
941 new_size += sizeof(*bi);
943 new_size -= sizeof(*ei0);
944 ret = btrfs_search_slot(trans, root, &key, path,
945 new_size + extra_size, 1);
950 ret = btrfs_extend_item(trans, root, path, new_size);
953 leaf = path->nodes[0];
954 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
955 btrfs_set_extent_refs(leaf, item, refs);
956 /* FIXME: get real generation */
957 btrfs_set_extent_generation(leaf, item, 0);
958 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
959 btrfs_set_extent_flags(leaf, item,
960 BTRFS_EXTENT_FLAG_TREE_BLOCK |
961 BTRFS_BLOCK_FLAG_FULL_BACKREF);
962 bi = (struct btrfs_tree_block_info *)(item + 1);
963 /* FIXME: get first key of the block */
964 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
965 btrfs_set_tree_block_level(leaf, bi, (int)owner);
967 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
969 btrfs_mark_buffer_dirty(leaf);
974 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
976 u32 high_crc = ~(u32)0;
977 u32 low_crc = ~(u32)0;
980 lenum = cpu_to_le64(root_objectid);
981 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
982 lenum = cpu_to_le64(owner);
983 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
984 lenum = cpu_to_le64(offset);
985 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
987 return ((u64)high_crc << 31) ^ (u64)low_crc;
990 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
991 struct btrfs_extent_data_ref *ref)
993 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
994 btrfs_extent_data_ref_objectid(leaf, ref),
995 btrfs_extent_data_ref_offset(leaf, ref));
998 static int match_extent_data_ref(struct extent_buffer *leaf,
999 struct btrfs_extent_data_ref *ref,
1000 u64 root_objectid, u64 owner, u64 offset)
1002 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1003 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1004 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1009 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1010 struct btrfs_root *root,
1011 struct btrfs_path *path,
1012 u64 bytenr, u64 parent,
1014 u64 owner, u64 offset)
1016 struct btrfs_key key;
1017 struct btrfs_extent_data_ref *ref;
1018 struct extent_buffer *leaf;
1024 key.objectid = bytenr;
1026 key.type = BTRFS_SHARED_DATA_REF_KEY;
1027 key.offset = parent;
1029 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1030 key.offset = hash_extent_data_ref(root_objectid,
1035 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1044 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1045 key.type = BTRFS_EXTENT_REF_V0_KEY;
1046 btrfs_release_path(root, path);
1047 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1058 leaf = path->nodes[0];
1059 nritems = btrfs_header_nritems(leaf);
1061 if (path->slots[0] >= nritems) {
1062 ret = btrfs_next_leaf(root, path);
1068 leaf = path->nodes[0];
1069 nritems = btrfs_header_nritems(leaf);
1073 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1074 if (key.objectid != bytenr ||
1075 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1078 ref = btrfs_item_ptr(leaf, path->slots[0],
1079 struct btrfs_extent_data_ref);
1081 if (match_extent_data_ref(leaf, ref, root_objectid,
1084 btrfs_release_path(root, path);
1096 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1097 struct btrfs_root *root,
1098 struct btrfs_path *path,
1099 u64 bytenr, u64 parent,
1100 u64 root_objectid, u64 owner,
1101 u64 offset, int refs_to_add)
1103 struct btrfs_key key;
1104 struct extent_buffer *leaf;
1109 key.objectid = bytenr;
1111 key.type = BTRFS_SHARED_DATA_REF_KEY;
1112 key.offset = parent;
1113 size = sizeof(struct btrfs_shared_data_ref);
1115 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1116 key.offset = hash_extent_data_ref(root_objectid,
1118 size = sizeof(struct btrfs_extent_data_ref);
1121 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1122 if (ret && ret != -EEXIST)
1125 leaf = path->nodes[0];
1127 struct btrfs_shared_data_ref *ref;
1128 ref = btrfs_item_ptr(leaf, path->slots[0],
1129 struct btrfs_shared_data_ref);
1131 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1133 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1134 num_refs += refs_to_add;
1135 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1138 struct btrfs_extent_data_ref *ref;
1139 while (ret == -EEXIST) {
1140 ref = btrfs_item_ptr(leaf, path->slots[0],
1141 struct btrfs_extent_data_ref);
1142 if (match_extent_data_ref(leaf, ref, root_objectid,
1145 btrfs_release_path(root, path);
1147 ret = btrfs_insert_empty_item(trans, root, path, &key,
1149 if (ret && ret != -EEXIST)
1152 leaf = path->nodes[0];
1154 ref = btrfs_item_ptr(leaf, path->slots[0],
1155 struct btrfs_extent_data_ref);
1157 btrfs_set_extent_data_ref_root(leaf, ref,
1159 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1160 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1161 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1163 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1164 num_refs += refs_to_add;
1165 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1168 btrfs_mark_buffer_dirty(leaf);
1171 btrfs_release_path(root, path);
1175 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root,
1177 struct btrfs_path *path,
1180 struct btrfs_key key;
1181 struct btrfs_extent_data_ref *ref1 = NULL;
1182 struct btrfs_shared_data_ref *ref2 = NULL;
1183 struct extent_buffer *leaf;
1187 leaf = path->nodes[0];
1188 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1190 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1191 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1192 struct btrfs_extent_data_ref);
1193 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1194 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1195 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1196 struct btrfs_shared_data_ref);
1197 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1198 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1199 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1200 struct btrfs_extent_ref_v0 *ref0;
1201 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1202 struct btrfs_extent_ref_v0);
1203 num_refs = btrfs_ref_count_v0(leaf, ref0);
1209 BUG_ON(num_refs < refs_to_drop);
1210 num_refs -= refs_to_drop;
1212 if (num_refs == 0) {
1213 ret = btrfs_del_item(trans, root, path);
1215 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1216 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1217 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1218 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1219 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1221 struct btrfs_extent_ref_v0 *ref0;
1222 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_extent_ref_v0);
1224 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1227 btrfs_mark_buffer_dirty(leaf);
1232 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1233 struct btrfs_path *path,
1234 struct btrfs_extent_inline_ref *iref)
1236 struct btrfs_key key;
1237 struct extent_buffer *leaf;
1238 struct btrfs_extent_data_ref *ref1;
1239 struct btrfs_shared_data_ref *ref2;
1242 leaf = path->nodes[0];
1243 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1245 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1246 BTRFS_EXTENT_DATA_REF_KEY) {
1247 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1248 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1250 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1251 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1253 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1254 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1255 struct btrfs_extent_data_ref);
1256 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1257 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1258 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1259 struct btrfs_shared_data_ref);
1260 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1261 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1262 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1263 struct btrfs_extent_ref_v0 *ref0;
1264 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1265 struct btrfs_extent_ref_v0);
1266 num_refs = btrfs_ref_count_v0(leaf, ref0);
1274 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1275 struct btrfs_root *root,
1276 struct btrfs_path *path,
1277 u64 bytenr, u64 parent,
1280 struct btrfs_key key;
1283 key.objectid = bytenr;
1285 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1286 key.offset = parent;
1288 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1289 key.offset = root_objectid;
1292 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1295 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1296 if (ret == -ENOENT && parent) {
1297 btrfs_release_path(root, path);
1298 key.type = BTRFS_EXTENT_REF_V0_KEY;
1299 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1307 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1308 struct btrfs_root *root,
1309 struct btrfs_path *path,
1310 u64 bytenr, u64 parent,
1313 struct btrfs_key key;
1316 key.objectid = bytenr;
1318 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1319 key.offset = parent;
1321 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1322 key.offset = root_objectid;
1325 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1326 btrfs_release_path(root, path);
1330 static inline int extent_ref_type(u64 parent, u64 owner)
1333 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1335 type = BTRFS_SHARED_BLOCK_REF_KEY;
1337 type = BTRFS_TREE_BLOCK_REF_KEY;
1340 type = BTRFS_SHARED_DATA_REF_KEY;
1342 type = BTRFS_EXTENT_DATA_REF_KEY;
1347 static int find_next_key(struct btrfs_path *path, int level,
1348 struct btrfs_key *key)
1351 for (; level < BTRFS_MAX_LEVEL; level++) {
1352 if (!path->nodes[level])
1354 if (path->slots[level] + 1 >=
1355 btrfs_header_nritems(path->nodes[level]))
1358 btrfs_item_key_to_cpu(path->nodes[level], key,
1359 path->slots[level] + 1);
1361 btrfs_node_key_to_cpu(path->nodes[level], key,
1362 path->slots[level] + 1);
1369 * look for inline back ref. if back ref is found, *ref_ret is set
1370 * to the address of inline back ref, and 0 is returned.
1372 * if back ref isn't found, *ref_ret is set to the address where it
1373 * should be inserted, and -ENOENT is returned.
1375 * if insert is true and there are too many inline back refs, the path
1376 * points to the extent item, and -EAGAIN is returned.
1378 * NOTE: inline back refs are ordered in the same way that back ref
1379 * items in the tree are ordered.
1381 static noinline_for_stack
1382 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1383 struct btrfs_root *root,
1384 struct btrfs_path *path,
1385 struct btrfs_extent_inline_ref **ref_ret,
1386 u64 bytenr, u64 num_bytes,
1387 u64 parent, u64 root_objectid,
1388 u64 owner, u64 offset, int insert)
1390 struct btrfs_key key;
1391 struct extent_buffer *leaf;
1392 struct btrfs_extent_item *ei;
1393 struct btrfs_extent_inline_ref *iref;
1404 key.objectid = bytenr;
1405 key.type = BTRFS_EXTENT_ITEM_KEY;
1406 key.offset = num_bytes;
1408 want = extent_ref_type(parent, owner);
1410 extra_size = btrfs_extent_inline_ref_size(want);
1411 path->keep_locks = 1;
1414 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1421 leaf = path->nodes[0];
1422 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1423 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1424 if (item_size < sizeof(*ei)) {
1429 ret = convert_extent_item_v0(trans, root, path, owner,
1435 leaf = path->nodes[0];
1436 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1439 BUG_ON(item_size < sizeof(*ei));
1441 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1442 flags = btrfs_extent_flags(leaf, ei);
1444 ptr = (unsigned long)(ei + 1);
1445 end = (unsigned long)ei + item_size;
1447 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1448 ptr += sizeof(struct btrfs_tree_block_info);
1451 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1460 iref = (struct btrfs_extent_inline_ref *)ptr;
1461 type = btrfs_extent_inline_ref_type(leaf, iref);
1465 ptr += btrfs_extent_inline_ref_size(type);
1469 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1470 struct btrfs_extent_data_ref *dref;
1471 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1472 if (match_extent_data_ref(leaf, dref, root_objectid,
1477 if (hash_extent_data_ref_item(leaf, dref) <
1478 hash_extent_data_ref(root_objectid, owner, offset))
1482 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1484 if (parent == ref_offset) {
1488 if (ref_offset < parent)
1491 if (root_objectid == ref_offset) {
1495 if (ref_offset < root_objectid)
1499 ptr += btrfs_extent_inline_ref_size(type);
1501 if (err == -ENOENT && insert) {
1502 if (item_size + extra_size >=
1503 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1508 * To add new inline back ref, we have to make sure
1509 * there is no corresponding back ref item.
1510 * For simplicity, we just do not add new inline back
1511 * ref if there is any kind of item for this block
1513 if (find_next_key(path, 0, &key) == 0 &&
1514 key.objectid == bytenr &&
1515 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1520 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1523 path->keep_locks = 0;
1524 btrfs_unlock_up_safe(path, 1);
1530 * helper to add new inline back ref
1532 static noinline_for_stack
1533 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1534 struct btrfs_root *root,
1535 struct btrfs_path *path,
1536 struct btrfs_extent_inline_ref *iref,
1537 u64 parent, u64 root_objectid,
1538 u64 owner, u64 offset, int refs_to_add,
1539 struct btrfs_delayed_extent_op *extent_op)
1541 struct extent_buffer *leaf;
1542 struct btrfs_extent_item *ei;
1545 unsigned long item_offset;
1551 leaf = path->nodes[0];
1552 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1553 item_offset = (unsigned long)iref - (unsigned long)ei;
1555 type = extent_ref_type(parent, owner);
1556 size = btrfs_extent_inline_ref_size(type);
1558 ret = btrfs_extend_item(trans, root, path, size);
1561 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1562 refs = btrfs_extent_refs(leaf, ei);
1563 refs += refs_to_add;
1564 btrfs_set_extent_refs(leaf, ei, refs);
1566 __run_delayed_extent_op(extent_op, leaf, ei);
1568 ptr = (unsigned long)ei + item_offset;
1569 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1570 if (ptr < end - size)
1571 memmove_extent_buffer(leaf, ptr + size, ptr,
1574 iref = (struct btrfs_extent_inline_ref *)ptr;
1575 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1576 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1577 struct btrfs_extent_data_ref *dref;
1578 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1579 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1580 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1581 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1582 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1583 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1584 struct btrfs_shared_data_ref *sref;
1585 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1586 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1587 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1588 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1589 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1591 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1593 btrfs_mark_buffer_dirty(leaf);
1597 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1598 struct btrfs_root *root,
1599 struct btrfs_path *path,
1600 struct btrfs_extent_inline_ref **ref_ret,
1601 u64 bytenr, u64 num_bytes, u64 parent,
1602 u64 root_objectid, u64 owner, u64 offset)
1606 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1607 bytenr, num_bytes, parent,
1608 root_objectid, owner, offset, 0);
1612 btrfs_release_path(root, path);
1615 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1616 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1619 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1620 root_objectid, owner, offset);
1626 * helper to update/remove inline back ref
1628 static noinline_for_stack
1629 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1630 struct btrfs_root *root,
1631 struct btrfs_path *path,
1632 struct btrfs_extent_inline_ref *iref,
1634 struct btrfs_delayed_extent_op *extent_op)
1636 struct extent_buffer *leaf;
1637 struct btrfs_extent_item *ei;
1638 struct btrfs_extent_data_ref *dref = NULL;
1639 struct btrfs_shared_data_ref *sref = NULL;
1648 leaf = path->nodes[0];
1649 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1650 refs = btrfs_extent_refs(leaf, ei);
1651 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1652 refs += refs_to_mod;
1653 btrfs_set_extent_refs(leaf, ei, refs);
1655 __run_delayed_extent_op(extent_op, leaf, ei);
1657 type = btrfs_extent_inline_ref_type(leaf, iref);
1659 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1660 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1661 refs = btrfs_extent_data_ref_count(leaf, dref);
1662 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1663 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1664 refs = btrfs_shared_data_ref_count(leaf, sref);
1667 BUG_ON(refs_to_mod != -1);
1670 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1671 refs += refs_to_mod;
1674 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1675 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1677 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1679 size = btrfs_extent_inline_ref_size(type);
1680 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1681 ptr = (unsigned long)iref;
1682 end = (unsigned long)ei + item_size;
1683 if (ptr + size < end)
1684 memmove_extent_buffer(leaf, ptr, ptr + size,
1687 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1690 btrfs_mark_buffer_dirty(leaf);
1694 static noinline_for_stack
1695 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1696 struct btrfs_root *root,
1697 struct btrfs_path *path,
1698 u64 bytenr, u64 num_bytes, u64 parent,
1699 u64 root_objectid, u64 owner,
1700 u64 offset, int refs_to_add,
1701 struct btrfs_delayed_extent_op *extent_op)
1703 struct btrfs_extent_inline_ref *iref;
1706 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1707 bytenr, num_bytes, parent,
1708 root_objectid, owner, offset, 1);
1710 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1711 ret = update_inline_extent_backref(trans, root, path, iref,
1712 refs_to_add, extent_op);
1713 } else if (ret == -ENOENT) {
1714 ret = setup_inline_extent_backref(trans, root, path, iref,
1715 parent, root_objectid,
1716 owner, offset, refs_to_add,
1722 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1723 struct btrfs_root *root,
1724 struct btrfs_path *path,
1725 u64 bytenr, u64 parent, u64 root_objectid,
1726 u64 owner, u64 offset, int refs_to_add)
1729 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1730 BUG_ON(refs_to_add != 1);
1731 ret = insert_tree_block_ref(trans, root, path, bytenr,
1732 parent, root_objectid);
1734 ret = insert_extent_data_ref(trans, root, path, bytenr,
1735 parent, root_objectid,
1736 owner, offset, refs_to_add);
1741 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1742 struct btrfs_root *root,
1743 struct btrfs_path *path,
1744 struct btrfs_extent_inline_ref *iref,
1745 int refs_to_drop, int is_data)
1749 BUG_ON(!is_data && refs_to_drop != 1);
1751 ret = update_inline_extent_backref(trans, root, path, iref,
1752 -refs_to_drop, NULL);
1753 } else if (is_data) {
1754 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1756 ret = btrfs_del_item(trans, root, path);
1761 static int btrfs_issue_discard(struct block_device *bdev,
1764 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
1767 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1768 u64 num_bytes, u64 *actual_bytes)
1771 u64 discarded_bytes = 0;
1772 struct btrfs_multi_bio *multi = NULL;
1775 /* Tell the block device(s) that the sectors can be discarded */
1776 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1777 bytenr, &num_bytes, &multi, 0);
1779 struct btrfs_bio_stripe *stripe = multi->stripes;
1783 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1784 ret = btrfs_issue_discard(stripe->dev->bdev,
1788 discarded_bytes += stripe->length;
1789 else if (ret != -EOPNOTSUPP)
1794 if (discarded_bytes && ret == -EOPNOTSUPP)
1798 *actual_bytes = discarded_bytes;
1804 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1805 struct btrfs_root *root,
1806 u64 bytenr, u64 num_bytes, u64 parent,
1807 u64 root_objectid, u64 owner, u64 offset)
1810 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1811 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1813 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1814 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1815 parent, root_objectid, (int)owner,
1816 BTRFS_ADD_DELAYED_REF, NULL);
1818 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1819 parent, root_objectid, owner, offset,
1820 BTRFS_ADD_DELAYED_REF, NULL);
1825 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1826 struct btrfs_root *root,
1827 u64 bytenr, u64 num_bytes,
1828 u64 parent, u64 root_objectid,
1829 u64 owner, u64 offset, int refs_to_add,
1830 struct btrfs_delayed_extent_op *extent_op)
1832 struct btrfs_path *path;
1833 struct extent_buffer *leaf;
1834 struct btrfs_extent_item *item;
1839 path = btrfs_alloc_path();
1844 path->leave_spinning = 1;
1845 /* this will setup the path even if it fails to insert the back ref */
1846 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1847 path, bytenr, num_bytes, parent,
1848 root_objectid, owner, offset,
1849 refs_to_add, extent_op);
1853 if (ret != -EAGAIN) {
1858 leaf = path->nodes[0];
1859 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1860 refs = btrfs_extent_refs(leaf, item);
1861 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1863 __run_delayed_extent_op(extent_op, leaf, item);
1865 btrfs_mark_buffer_dirty(leaf);
1866 btrfs_release_path(root->fs_info->extent_root, path);
1869 path->leave_spinning = 1;
1871 /* now insert the actual backref */
1872 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1873 path, bytenr, parent, root_objectid,
1874 owner, offset, refs_to_add);
1877 btrfs_free_path(path);
1881 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1882 struct btrfs_root *root,
1883 struct btrfs_delayed_ref_node *node,
1884 struct btrfs_delayed_extent_op *extent_op,
1885 int insert_reserved)
1888 struct btrfs_delayed_data_ref *ref;
1889 struct btrfs_key ins;
1894 ins.objectid = node->bytenr;
1895 ins.offset = node->num_bytes;
1896 ins.type = BTRFS_EXTENT_ITEM_KEY;
1898 ref = btrfs_delayed_node_to_data_ref(node);
1899 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1900 parent = ref->parent;
1902 ref_root = ref->root;
1904 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1906 BUG_ON(extent_op->update_key);
1907 flags |= extent_op->flags_to_set;
1909 ret = alloc_reserved_file_extent(trans, root,
1910 parent, ref_root, flags,
1911 ref->objectid, ref->offset,
1912 &ins, node->ref_mod);
1913 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1914 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1915 node->num_bytes, parent,
1916 ref_root, ref->objectid,
1917 ref->offset, node->ref_mod,
1919 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1920 ret = __btrfs_free_extent(trans, root, node->bytenr,
1921 node->num_bytes, parent,
1922 ref_root, ref->objectid,
1923 ref->offset, node->ref_mod,
1931 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1932 struct extent_buffer *leaf,
1933 struct btrfs_extent_item *ei)
1935 u64 flags = btrfs_extent_flags(leaf, ei);
1936 if (extent_op->update_flags) {
1937 flags |= extent_op->flags_to_set;
1938 btrfs_set_extent_flags(leaf, ei, flags);
1941 if (extent_op->update_key) {
1942 struct btrfs_tree_block_info *bi;
1943 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1944 bi = (struct btrfs_tree_block_info *)(ei + 1);
1945 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1949 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1950 struct btrfs_root *root,
1951 struct btrfs_delayed_ref_node *node,
1952 struct btrfs_delayed_extent_op *extent_op)
1954 struct btrfs_key key;
1955 struct btrfs_path *path;
1956 struct btrfs_extent_item *ei;
1957 struct extent_buffer *leaf;
1962 path = btrfs_alloc_path();
1966 key.objectid = node->bytenr;
1967 key.type = BTRFS_EXTENT_ITEM_KEY;
1968 key.offset = node->num_bytes;
1971 path->leave_spinning = 1;
1972 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1983 leaf = path->nodes[0];
1984 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1985 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1986 if (item_size < sizeof(*ei)) {
1987 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1993 leaf = path->nodes[0];
1994 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1997 BUG_ON(item_size < sizeof(*ei));
1998 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1999 __run_delayed_extent_op(extent_op, leaf, ei);
2001 btrfs_mark_buffer_dirty(leaf);
2003 btrfs_free_path(path);
2007 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2008 struct btrfs_root *root,
2009 struct btrfs_delayed_ref_node *node,
2010 struct btrfs_delayed_extent_op *extent_op,
2011 int insert_reserved)
2014 struct btrfs_delayed_tree_ref *ref;
2015 struct btrfs_key ins;
2019 ins.objectid = node->bytenr;
2020 ins.offset = node->num_bytes;
2021 ins.type = BTRFS_EXTENT_ITEM_KEY;
2023 ref = btrfs_delayed_node_to_tree_ref(node);
2024 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2025 parent = ref->parent;
2027 ref_root = ref->root;
2029 BUG_ON(node->ref_mod != 1);
2030 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2031 BUG_ON(!extent_op || !extent_op->update_flags ||
2032 !extent_op->update_key);
2033 ret = alloc_reserved_tree_block(trans, root,
2035 extent_op->flags_to_set,
2038 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2039 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2040 node->num_bytes, parent, ref_root,
2041 ref->level, 0, 1, extent_op);
2042 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2043 ret = __btrfs_free_extent(trans, root, node->bytenr,
2044 node->num_bytes, parent, ref_root,
2045 ref->level, 0, 1, extent_op);
2052 /* helper function to actually process a single delayed ref entry */
2053 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2054 struct btrfs_root *root,
2055 struct btrfs_delayed_ref_node *node,
2056 struct btrfs_delayed_extent_op *extent_op,
2057 int insert_reserved)
2060 if (btrfs_delayed_ref_is_head(node)) {
2061 struct btrfs_delayed_ref_head *head;
2063 * we've hit the end of the chain and we were supposed
2064 * to insert this extent into the tree. But, it got
2065 * deleted before we ever needed to insert it, so all
2066 * we have to do is clean up the accounting
2069 head = btrfs_delayed_node_to_head(node);
2070 if (insert_reserved) {
2071 btrfs_pin_extent(root, node->bytenr,
2072 node->num_bytes, 1);
2073 if (head->is_data) {
2074 ret = btrfs_del_csums(trans, root,
2080 mutex_unlock(&head->mutex);
2084 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2085 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2086 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2088 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2089 node->type == BTRFS_SHARED_DATA_REF_KEY)
2090 ret = run_delayed_data_ref(trans, root, node, extent_op,
2097 static noinline struct btrfs_delayed_ref_node *
2098 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2100 struct rb_node *node;
2101 struct btrfs_delayed_ref_node *ref;
2102 int action = BTRFS_ADD_DELAYED_REF;
2105 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2106 * this prevents ref count from going down to zero when
2107 * there still are pending delayed ref.
2109 node = rb_prev(&head->node.rb_node);
2113 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2115 if (ref->bytenr != head->node.bytenr)
2117 if (ref->action == action)
2119 node = rb_prev(node);
2121 if (action == BTRFS_ADD_DELAYED_REF) {
2122 action = BTRFS_DROP_DELAYED_REF;
2128 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2129 struct btrfs_root *root,
2130 struct list_head *cluster)
2132 struct btrfs_delayed_ref_root *delayed_refs;
2133 struct btrfs_delayed_ref_node *ref;
2134 struct btrfs_delayed_ref_head *locked_ref = NULL;
2135 struct btrfs_delayed_extent_op *extent_op;
2138 int must_insert_reserved = 0;
2140 delayed_refs = &trans->transaction->delayed_refs;
2143 /* pick a new head ref from the cluster list */
2144 if (list_empty(cluster))
2147 locked_ref = list_entry(cluster->next,
2148 struct btrfs_delayed_ref_head, cluster);
2150 /* grab the lock that says we are going to process
2151 * all the refs for this head */
2152 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2155 * we may have dropped the spin lock to get the head
2156 * mutex lock, and that might have given someone else
2157 * time to free the head. If that's true, it has been
2158 * removed from our list and we can move on.
2160 if (ret == -EAGAIN) {
2168 * record the must insert reserved flag before we
2169 * drop the spin lock.
2171 must_insert_reserved = locked_ref->must_insert_reserved;
2172 locked_ref->must_insert_reserved = 0;
2174 extent_op = locked_ref->extent_op;
2175 locked_ref->extent_op = NULL;
2178 * locked_ref is the head node, so we have to go one
2179 * node back for any delayed ref updates
2181 ref = select_delayed_ref(locked_ref);
2183 /* All delayed refs have been processed, Go ahead
2184 * and send the head node to run_one_delayed_ref,
2185 * so that any accounting fixes can happen
2187 ref = &locked_ref->node;
2189 if (extent_op && must_insert_reserved) {
2195 spin_unlock(&delayed_refs->lock);
2197 ret = run_delayed_extent_op(trans, root,
2203 spin_lock(&delayed_refs->lock);
2207 list_del_init(&locked_ref->cluster);
2212 rb_erase(&ref->rb_node, &delayed_refs->root);
2213 delayed_refs->num_entries--;
2215 spin_unlock(&delayed_refs->lock);
2217 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2218 must_insert_reserved);
2221 btrfs_put_delayed_ref(ref);
2226 spin_lock(&delayed_refs->lock);
2232 * this starts processing the delayed reference count updates and
2233 * extent insertions we have queued up so far. count can be
2234 * 0, which means to process everything in the tree at the start
2235 * of the run (but not newly added entries), or it can be some target
2236 * number you'd like to process.
2238 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2239 struct btrfs_root *root, unsigned long count)
2241 struct rb_node *node;
2242 struct btrfs_delayed_ref_root *delayed_refs;
2243 struct btrfs_delayed_ref_node *ref;
2244 struct list_head cluster;
2246 int run_all = count == (unsigned long)-1;
2249 if (root == root->fs_info->extent_root)
2250 root = root->fs_info->tree_root;
2252 delayed_refs = &trans->transaction->delayed_refs;
2253 INIT_LIST_HEAD(&cluster);
2255 spin_lock(&delayed_refs->lock);
2257 count = delayed_refs->num_entries * 2;
2261 if (!(run_all || run_most) &&
2262 delayed_refs->num_heads_ready < 64)
2266 * go find something we can process in the rbtree. We start at
2267 * the beginning of the tree, and then build a cluster
2268 * of refs to process starting at the first one we are able to
2271 ret = btrfs_find_ref_cluster(trans, &cluster,
2272 delayed_refs->run_delayed_start);
2276 ret = run_clustered_refs(trans, root, &cluster);
2279 count -= min_t(unsigned long, ret, count);
2286 node = rb_first(&delayed_refs->root);
2289 count = (unsigned long)-1;
2292 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2294 if (btrfs_delayed_ref_is_head(ref)) {
2295 struct btrfs_delayed_ref_head *head;
2297 head = btrfs_delayed_node_to_head(ref);
2298 atomic_inc(&ref->refs);
2300 spin_unlock(&delayed_refs->lock);
2301 mutex_lock(&head->mutex);
2302 mutex_unlock(&head->mutex);
2304 btrfs_put_delayed_ref(ref);
2308 node = rb_next(node);
2310 spin_unlock(&delayed_refs->lock);
2311 schedule_timeout(1);
2315 spin_unlock(&delayed_refs->lock);
2319 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2320 struct btrfs_root *root,
2321 u64 bytenr, u64 num_bytes, u64 flags,
2324 struct btrfs_delayed_extent_op *extent_op;
2327 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2331 extent_op->flags_to_set = flags;
2332 extent_op->update_flags = 1;
2333 extent_op->update_key = 0;
2334 extent_op->is_data = is_data ? 1 : 0;
2336 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2342 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2343 struct btrfs_root *root,
2344 struct btrfs_path *path,
2345 u64 objectid, u64 offset, u64 bytenr)
2347 struct btrfs_delayed_ref_head *head;
2348 struct btrfs_delayed_ref_node *ref;
2349 struct btrfs_delayed_data_ref *data_ref;
2350 struct btrfs_delayed_ref_root *delayed_refs;
2351 struct rb_node *node;
2355 delayed_refs = &trans->transaction->delayed_refs;
2356 spin_lock(&delayed_refs->lock);
2357 head = btrfs_find_delayed_ref_head(trans, bytenr);
2361 if (!mutex_trylock(&head->mutex)) {
2362 atomic_inc(&head->node.refs);
2363 spin_unlock(&delayed_refs->lock);
2365 btrfs_release_path(root->fs_info->extent_root, path);
2367 mutex_lock(&head->mutex);
2368 mutex_unlock(&head->mutex);
2369 btrfs_put_delayed_ref(&head->node);
2373 node = rb_prev(&head->node.rb_node);
2377 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2379 if (ref->bytenr != bytenr)
2383 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2386 data_ref = btrfs_delayed_node_to_data_ref(ref);
2388 node = rb_prev(node);
2390 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2391 if (ref->bytenr == bytenr)
2395 if (data_ref->root != root->root_key.objectid ||
2396 data_ref->objectid != objectid || data_ref->offset != offset)
2401 mutex_unlock(&head->mutex);
2403 spin_unlock(&delayed_refs->lock);
2407 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2408 struct btrfs_root *root,
2409 struct btrfs_path *path,
2410 u64 objectid, u64 offset, u64 bytenr)
2412 struct btrfs_root *extent_root = root->fs_info->extent_root;
2413 struct extent_buffer *leaf;
2414 struct btrfs_extent_data_ref *ref;
2415 struct btrfs_extent_inline_ref *iref;
2416 struct btrfs_extent_item *ei;
2417 struct btrfs_key key;
2421 key.objectid = bytenr;
2422 key.offset = (u64)-1;
2423 key.type = BTRFS_EXTENT_ITEM_KEY;
2425 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2431 if (path->slots[0] == 0)
2435 leaf = path->nodes[0];
2436 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2438 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2442 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2443 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2444 if (item_size < sizeof(*ei)) {
2445 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2449 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2451 if (item_size != sizeof(*ei) +
2452 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2455 if (btrfs_extent_generation(leaf, ei) <=
2456 btrfs_root_last_snapshot(&root->root_item))
2459 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2460 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2461 BTRFS_EXTENT_DATA_REF_KEY)
2464 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2465 if (btrfs_extent_refs(leaf, ei) !=
2466 btrfs_extent_data_ref_count(leaf, ref) ||
2467 btrfs_extent_data_ref_root(leaf, ref) !=
2468 root->root_key.objectid ||
2469 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2470 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2478 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2479 struct btrfs_root *root,
2480 u64 objectid, u64 offset, u64 bytenr)
2482 struct btrfs_path *path;
2486 path = btrfs_alloc_path();
2491 ret = check_committed_ref(trans, root, path, objectid,
2493 if (ret && ret != -ENOENT)
2496 ret2 = check_delayed_ref(trans, root, path, objectid,
2498 } while (ret2 == -EAGAIN);
2500 if (ret2 && ret2 != -ENOENT) {
2505 if (ret != -ENOENT || ret2 != -ENOENT)
2508 btrfs_free_path(path);
2509 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2515 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2516 struct extent_buffer *buf, u32 nr_extents)
2518 struct btrfs_key key;
2519 struct btrfs_file_extent_item *fi;
2527 if (!root->ref_cows)
2530 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2532 root_gen = root->root_key.offset;
2535 root_gen = trans->transid - 1;
2538 level = btrfs_header_level(buf);
2539 nritems = btrfs_header_nritems(buf);
2542 struct btrfs_leaf_ref *ref;
2543 struct btrfs_extent_info *info;
2545 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2551 ref->root_gen = root_gen;
2552 ref->bytenr = buf->start;
2553 ref->owner = btrfs_header_owner(buf);
2554 ref->generation = btrfs_header_generation(buf);
2555 ref->nritems = nr_extents;
2556 info = ref->extents;
2558 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2560 btrfs_item_key_to_cpu(buf, &key, i);
2561 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2563 fi = btrfs_item_ptr(buf, i,
2564 struct btrfs_file_extent_item);
2565 if (btrfs_file_extent_type(buf, fi) ==
2566 BTRFS_FILE_EXTENT_INLINE)
2568 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2569 if (disk_bytenr == 0)
2572 info->bytenr = disk_bytenr;
2574 btrfs_file_extent_disk_num_bytes(buf, fi);
2575 info->objectid = key.objectid;
2576 info->offset = key.offset;
2580 ret = btrfs_add_leaf_ref(root, ref, shared);
2581 if (ret == -EEXIST && shared) {
2582 struct btrfs_leaf_ref *old;
2583 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2585 btrfs_remove_leaf_ref(root, old);
2586 btrfs_free_leaf_ref(root, old);
2587 ret = btrfs_add_leaf_ref(root, ref, shared);
2590 btrfs_free_leaf_ref(root, ref);
2596 /* when a block goes through cow, we update the reference counts of
2597 * everything that block points to. The internal pointers of the block
2598 * can be in just about any order, and it is likely to have clusters of
2599 * things that are close together and clusters of things that are not.
2601 * To help reduce the seeks that come with updating all of these reference
2602 * counts, sort them by byte number before actual updates are done.
2604 * struct refsort is used to match byte number to slot in the btree block.
2605 * we sort based on the byte number and then use the slot to actually
2608 * struct refsort is smaller than strcut btrfs_item and smaller than
2609 * struct btrfs_key_ptr. Since we're currently limited to the page size
2610 * for a btree block, there's no way for a kmalloc of refsorts for a
2611 * single node to be bigger than a page.
2619 * for passing into sort()
2621 static int refsort_cmp(const void *a_void, const void *b_void)
2623 const struct refsort *a = a_void;
2624 const struct refsort *b = b_void;
2626 if (a->bytenr < b->bytenr)
2628 if (a->bytenr > b->bytenr)
2634 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2635 struct btrfs_root *root,
2636 struct extent_buffer *buf,
2637 int full_backref, int inc)
2644 struct btrfs_key key;
2645 struct btrfs_file_extent_item *fi;
2649 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2650 u64, u64, u64, u64, u64, u64);
2652 ref_root = btrfs_header_owner(buf);
2653 nritems = btrfs_header_nritems(buf);
2654 level = btrfs_header_level(buf);
2656 if (!root->ref_cows && level == 0)
2660 process_func = btrfs_inc_extent_ref;
2662 process_func = btrfs_free_extent;
2665 parent = buf->start;
2669 for (i = 0; i < nritems; i++) {
2671 btrfs_item_key_to_cpu(buf, &key, i);
2672 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2674 fi = btrfs_item_ptr(buf, i,
2675 struct btrfs_file_extent_item);
2676 if (btrfs_file_extent_type(buf, fi) ==
2677 BTRFS_FILE_EXTENT_INLINE)
2679 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2683 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2684 key.offset -= btrfs_file_extent_offset(buf, fi);
2685 ret = process_func(trans, root, bytenr, num_bytes,
2686 parent, ref_root, key.objectid,
2691 bytenr = btrfs_node_blockptr(buf, i);
2692 num_bytes = btrfs_level_size(root, level - 1);
2693 ret = process_func(trans, root, bytenr, num_bytes,
2694 parent, ref_root, level - 1, 0);
2705 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2706 struct extent_buffer *buf, int full_backref)
2708 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2711 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2712 struct extent_buffer *buf, int full_backref)
2714 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2717 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2718 struct btrfs_root *root,
2719 struct btrfs_path *path,
2720 struct btrfs_block_group_cache *cache)
2723 struct btrfs_root *extent_root = root->fs_info->extent_root;
2725 struct extent_buffer *leaf;
2727 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2732 leaf = path->nodes[0];
2733 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2734 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2735 btrfs_mark_buffer_dirty(leaf);
2736 btrfs_release_path(extent_root, path);
2744 static struct btrfs_block_group_cache *
2745 next_block_group(struct btrfs_root *root,
2746 struct btrfs_block_group_cache *cache)
2748 struct rb_node *node;
2749 spin_lock(&root->fs_info->block_group_cache_lock);
2750 node = rb_next(&cache->cache_node);
2751 btrfs_put_block_group(cache);
2753 cache = rb_entry(node, struct btrfs_block_group_cache,
2755 btrfs_get_block_group(cache);
2758 spin_unlock(&root->fs_info->block_group_cache_lock);
2762 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2763 struct btrfs_trans_handle *trans,
2764 struct btrfs_path *path)
2766 struct btrfs_root *root = block_group->fs_info->tree_root;
2767 struct inode *inode = NULL;
2769 int dcs = BTRFS_DC_ERROR;
2775 * If this block group is smaller than 100 megs don't bother caching the
2778 if (block_group->key.offset < (100 * 1024 * 1024)) {
2779 spin_lock(&block_group->lock);
2780 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2781 spin_unlock(&block_group->lock);
2786 inode = lookup_free_space_inode(root, block_group, path);
2787 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2788 ret = PTR_ERR(inode);
2789 btrfs_release_path(root, path);
2793 if (IS_ERR(inode)) {
2797 if (block_group->ro)
2800 ret = create_free_space_inode(root, trans, block_group, path);
2807 * We want to set the generation to 0, that way if anything goes wrong
2808 * from here on out we know not to trust this cache when we load up next
2811 BTRFS_I(inode)->generation = 0;
2812 ret = btrfs_update_inode(trans, root, inode);
2815 if (i_size_read(inode) > 0) {
2816 ret = btrfs_truncate_free_space_cache(root, trans, path,
2822 spin_lock(&block_group->lock);
2823 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2824 /* We're not cached, don't bother trying to write stuff out */
2825 dcs = BTRFS_DC_WRITTEN;
2826 spin_unlock(&block_group->lock);
2829 spin_unlock(&block_group->lock);
2831 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2836 * Just to make absolutely sure we have enough space, we're going to
2837 * preallocate 12 pages worth of space for each block group. In
2838 * practice we ought to use at most 8, but we need extra space so we can
2839 * add our header and have a terminator between the extents and the
2843 num_pages *= PAGE_CACHE_SIZE;
2845 ret = btrfs_check_data_free_space(inode, num_pages);
2849 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2850 num_pages, num_pages,
2853 dcs = BTRFS_DC_SETUP;
2854 btrfs_free_reserved_data_space(inode, num_pages);
2858 btrfs_release_path(root, path);
2860 spin_lock(&block_group->lock);
2861 block_group->disk_cache_state = dcs;
2862 spin_unlock(&block_group->lock);
2867 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2868 struct btrfs_root *root)
2870 struct btrfs_block_group_cache *cache;
2872 struct btrfs_path *path;
2875 path = btrfs_alloc_path();
2881 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2883 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2885 cache = next_block_group(root, cache);
2893 err = cache_save_setup(cache, trans, path);
2894 last = cache->key.objectid + cache->key.offset;
2895 btrfs_put_block_group(cache);
2900 err = btrfs_run_delayed_refs(trans, root,
2905 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2907 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2908 btrfs_put_block_group(cache);
2914 cache = next_block_group(root, cache);
2923 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2924 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
2926 last = cache->key.objectid + cache->key.offset;
2928 err = write_one_cache_group(trans, root, path, cache);
2930 btrfs_put_block_group(cache);
2935 * I don't think this is needed since we're just marking our
2936 * preallocated extent as written, but just in case it can't
2940 err = btrfs_run_delayed_refs(trans, root,
2945 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2948 * Really this shouldn't happen, but it could if we
2949 * couldn't write the entire preallocated extent and
2950 * splitting the extent resulted in a new block.
2953 btrfs_put_block_group(cache);
2956 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2958 cache = next_block_group(root, cache);
2967 btrfs_write_out_cache(root, trans, cache, path);
2970 * If we didn't have an error then the cache state is still
2971 * NEED_WRITE, so we can set it to WRITTEN.
2973 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2974 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2975 last = cache->key.objectid + cache->key.offset;
2976 btrfs_put_block_group(cache);
2979 btrfs_free_path(path);
2983 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2985 struct btrfs_block_group_cache *block_group;
2988 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
2989 if (!block_group || block_group->ro)
2992 btrfs_put_block_group(block_group);
2996 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
2997 u64 total_bytes, u64 bytes_used,
2998 struct btrfs_space_info **space_info)
3000 struct btrfs_space_info *found;
3004 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3005 BTRFS_BLOCK_GROUP_RAID10))
3010 found = __find_space_info(info, flags);
3012 spin_lock(&found->lock);
3013 found->total_bytes += total_bytes;
3014 found->disk_total += total_bytes * factor;
3015 found->bytes_used += bytes_used;
3016 found->disk_used += bytes_used * factor;
3018 spin_unlock(&found->lock);
3019 *space_info = found;
3022 found = kzalloc(sizeof(*found), GFP_NOFS);
3026 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3027 INIT_LIST_HEAD(&found->block_groups[i]);
3028 init_rwsem(&found->groups_sem);
3029 spin_lock_init(&found->lock);
3030 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3031 BTRFS_BLOCK_GROUP_SYSTEM |
3032 BTRFS_BLOCK_GROUP_METADATA);
3033 found->total_bytes = total_bytes;
3034 found->disk_total = total_bytes * factor;
3035 found->bytes_used = bytes_used;
3036 found->disk_used = bytes_used * factor;
3037 found->bytes_pinned = 0;
3038 found->bytes_reserved = 0;
3039 found->bytes_readonly = 0;
3040 found->bytes_may_use = 0;
3042 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
3043 found->chunk_alloc = 0;
3044 *space_info = found;
3045 list_add_rcu(&found->list, &info->space_info);
3046 atomic_set(&found->caching_threads, 0);
3050 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3052 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
3053 BTRFS_BLOCK_GROUP_RAID1 |
3054 BTRFS_BLOCK_GROUP_RAID10 |
3055 BTRFS_BLOCK_GROUP_DUP);
3057 if (flags & BTRFS_BLOCK_GROUP_DATA)
3058 fs_info->avail_data_alloc_bits |= extra_flags;
3059 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3060 fs_info->avail_metadata_alloc_bits |= extra_flags;
3061 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3062 fs_info->avail_system_alloc_bits |= extra_flags;
3066 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3069 * we add in the count of missing devices because we want
3070 * to make sure that any RAID levels on a degraded FS
3071 * continue to be honored.
3073 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3074 root->fs_info->fs_devices->missing_devices;
3076 if (num_devices == 1)
3077 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3078 if (num_devices < 4)
3079 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3081 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3082 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3083 BTRFS_BLOCK_GROUP_RAID10))) {
3084 flags &= ~BTRFS_BLOCK_GROUP_DUP;
3087 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
3088 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
3089 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
3092 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3093 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3094 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3095 (flags & BTRFS_BLOCK_GROUP_DUP)))
3096 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3100 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
3102 if (flags & BTRFS_BLOCK_GROUP_DATA)
3103 flags |= root->fs_info->avail_data_alloc_bits &
3104 root->fs_info->data_alloc_profile;
3105 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3106 flags |= root->fs_info->avail_system_alloc_bits &
3107 root->fs_info->system_alloc_profile;
3108 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3109 flags |= root->fs_info->avail_metadata_alloc_bits &
3110 root->fs_info->metadata_alloc_profile;
3111 return btrfs_reduce_alloc_profile(root, flags);
3114 u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3119 flags = BTRFS_BLOCK_GROUP_DATA;
3120 else if (root == root->fs_info->chunk_root)
3121 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3123 flags = BTRFS_BLOCK_GROUP_METADATA;
3125 return get_alloc_profile(root, flags);
3128 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3130 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
3131 BTRFS_BLOCK_GROUP_DATA);
3135 * This will check the space that the inode allocates from to make sure we have
3136 * enough space for bytes.
3138 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3140 struct btrfs_space_info *data_sinfo;
3141 struct btrfs_root *root = BTRFS_I(inode)->root;
3143 int ret = 0, committed = 0, alloc_chunk = 1;
3145 /* make sure bytes are sectorsize aligned */
3146 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3148 if (root == root->fs_info->tree_root ||
3149 BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID) {
3154 data_sinfo = BTRFS_I(inode)->space_info;
3159 /* make sure we have enough space to handle the data first */
3160 spin_lock(&data_sinfo->lock);
3161 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3162 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3163 data_sinfo->bytes_may_use;
3165 if (used + bytes > data_sinfo->total_bytes) {
3166 struct btrfs_trans_handle *trans;
3169 * if we don't have enough free bytes in this space then we need
3170 * to alloc a new chunk.
3172 if (!data_sinfo->full && alloc_chunk) {
3175 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
3176 spin_unlock(&data_sinfo->lock);
3178 alloc_target = btrfs_get_alloc_profile(root, 1);
3179 trans = btrfs_join_transaction(root, 1);
3181 return PTR_ERR(trans);
3183 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3184 bytes + 2 * 1024 * 1024,
3186 CHUNK_ALLOC_NO_FORCE);
3187 btrfs_end_transaction(trans, root);
3196 btrfs_set_inode_space_info(root, inode);
3197 data_sinfo = BTRFS_I(inode)->space_info;
3201 spin_unlock(&data_sinfo->lock);
3203 /* commit the current transaction and try again */
3205 if (!committed && !root->fs_info->open_ioctl_trans) {
3207 trans = btrfs_join_transaction(root, 1);
3209 return PTR_ERR(trans);
3210 ret = btrfs_commit_transaction(trans, root);
3216 #if 0 /* I hope we never need this code again, just in case */
3217 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3218 "%llu bytes_reserved, " "%llu bytes_pinned, "
3219 "%llu bytes_readonly, %llu may use %llu total\n",
3220 (unsigned long long)bytes,
3221 (unsigned long long)data_sinfo->bytes_used,
3222 (unsigned long long)data_sinfo->bytes_reserved,
3223 (unsigned long long)data_sinfo->bytes_pinned,
3224 (unsigned long long)data_sinfo->bytes_readonly,
3225 (unsigned long long)data_sinfo->bytes_may_use,
3226 (unsigned long long)data_sinfo->total_bytes);
3230 data_sinfo->bytes_may_use += bytes;
3231 BTRFS_I(inode)->reserved_bytes += bytes;
3232 spin_unlock(&data_sinfo->lock);
3238 * called when we are clearing an delalloc extent from the
3239 * inode's io_tree or there was an error for whatever reason
3240 * after calling btrfs_check_data_free_space
3242 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3244 struct btrfs_root *root = BTRFS_I(inode)->root;
3245 struct btrfs_space_info *data_sinfo;
3247 /* make sure bytes are sectorsize aligned */
3248 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3250 data_sinfo = BTRFS_I(inode)->space_info;
3251 spin_lock(&data_sinfo->lock);
3252 data_sinfo->bytes_may_use -= bytes;
3253 BTRFS_I(inode)->reserved_bytes -= bytes;
3254 spin_unlock(&data_sinfo->lock);
3257 static void force_metadata_allocation(struct btrfs_fs_info *info)
3259 struct list_head *head = &info->space_info;
3260 struct btrfs_space_info *found;
3263 list_for_each_entry_rcu(found, head, list) {
3264 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3265 found->force_alloc = CHUNK_ALLOC_FORCE;
3270 static int should_alloc_chunk(struct btrfs_root *root,
3271 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3274 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3275 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3278 if (force == CHUNK_ALLOC_FORCE)
3282 * in limited mode, we want to have some free space up to
3283 * about 1% of the FS size.
3285 if (force == CHUNK_ALLOC_LIMITED) {
3286 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3287 thresh = max_t(u64, 64 * 1024 * 1024,
3288 div_factor_fine(thresh, 1));
3290 if (num_bytes - num_allocated < thresh)
3295 * we have two similar checks here, one based on percentage
3296 * and once based on a hard number of 256MB. The idea
3297 * is that if we have a good amount of free
3298 * room, don't allocate a chunk. A good mount is
3299 * less than 80% utilized of the chunks we have allocated,
3300 * or more than 256MB free
3302 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3305 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
3308 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3310 /* 256MB or 5% of the FS */
3311 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3313 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
3318 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3319 struct btrfs_root *extent_root, u64 alloc_bytes,
3320 u64 flags, int force)
3322 struct btrfs_space_info *space_info;
3323 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3324 int wait_for_alloc = 0;
3327 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3329 space_info = __find_space_info(extent_root->fs_info, flags);
3331 ret = update_space_info(extent_root->fs_info, flags,
3335 BUG_ON(!space_info);
3338 spin_lock(&space_info->lock);
3339 if (space_info->force_alloc)
3340 force = space_info->force_alloc;
3341 if (space_info->full) {
3342 spin_unlock(&space_info->lock);
3346 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
3347 spin_unlock(&space_info->lock);
3349 } else if (space_info->chunk_alloc) {
3352 space_info->chunk_alloc = 1;
3355 spin_unlock(&space_info->lock);
3357 mutex_lock(&fs_info->chunk_mutex);
3360 * The chunk_mutex is held throughout the entirety of a chunk
3361 * allocation, so once we've acquired the chunk_mutex we know that the
3362 * other guy is done and we need to recheck and see if we should
3365 if (wait_for_alloc) {
3366 mutex_unlock(&fs_info->chunk_mutex);
3372 * If we have mixed data/metadata chunks we want to make sure we keep
3373 * allocating mixed chunks instead of individual chunks.
3375 if (btrfs_mixed_space_info(space_info))
3376 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3379 * if we're doing a data chunk, go ahead and make sure that
3380 * we keep a reasonable number of metadata chunks allocated in the
3383 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3384 fs_info->data_chunk_allocations++;
3385 if (!(fs_info->data_chunk_allocations %
3386 fs_info->metadata_ratio))
3387 force_metadata_allocation(fs_info);
3390 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3391 spin_lock(&space_info->lock);
3393 space_info->full = 1;
3397 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3398 space_info->chunk_alloc = 0;
3399 spin_unlock(&space_info->lock);
3400 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3405 * shrink metadata reservation for delalloc
3407 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3408 struct btrfs_root *root, u64 to_reclaim, int sync)
3410 struct btrfs_block_rsv *block_rsv;
3411 struct btrfs_space_info *space_info;
3416 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
3418 unsigned long progress;
3420 block_rsv = &root->fs_info->delalloc_block_rsv;
3421 space_info = block_rsv->space_info;
3424 reserved = space_info->bytes_reserved;
3425 progress = space_info->reservation_progress;
3430 max_reclaim = min(reserved, to_reclaim);
3432 while (loops < 1024) {
3433 /* have the flusher threads jump in and do some IO */
3435 nr_pages = min_t(unsigned long, nr_pages,
3436 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3437 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
3439 spin_lock(&space_info->lock);
3440 if (reserved > space_info->bytes_reserved)
3441 reclaimed += reserved - space_info->bytes_reserved;
3442 reserved = space_info->bytes_reserved;
3443 spin_unlock(&space_info->lock);
3447 if (reserved == 0 || reclaimed >= max_reclaim)
3450 if (trans && trans->transaction->blocked)
3453 time_left = schedule_timeout_interruptible(1);
3455 /* We were interrupted, exit */
3459 /* we've kicked the IO a few times, if anything has been freed,
3460 * exit. There is no sense in looping here for a long time
3461 * when we really need to commit the transaction, or there are
3462 * just too many writers without enough free space
3467 if (progress != space_info->reservation_progress)
3472 return reclaimed >= to_reclaim;
3476 * Retries tells us how many times we've called reserve_metadata_bytes. The
3477 * idea is if this is the first call (retries == 0) then we will add to our
3478 * reserved count if we can't make the allocation in order to hold our place
3479 * while we go and try and free up space. That way for retries > 1 we don't try
3480 * and add space, we just check to see if the amount of unused space is >= the
3481 * total space, meaning that our reservation is valid.
3483 * However if we don't intend to retry this reservation, pass -1 as retries so
3484 * that it short circuits this logic.
3486 static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3487 struct btrfs_root *root,
3488 struct btrfs_block_rsv *block_rsv,
3489 u64 orig_bytes, int flush)
3491 struct btrfs_space_info *space_info = block_rsv->space_info;
3493 u64 num_bytes = orig_bytes;
3496 bool reserved = false;
3497 bool committed = false;
3504 spin_lock(&space_info->lock);
3505 unused = space_info->bytes_used + space_info->bytes_reserved +
3506 space_info->bytes_pinned + space_info->bytes_readonly +
3507 space_info->bytes_may_use;
3510 * The idea here is that we've not already over-reserved the block group
3511 * then we can go ahead and save our reservation first and then start
3512 * flushing if we need to. Otherwise if we've already overcommitted
3513 * lets start flushing stuff first and then come back and try to make
3516 if (unused <= space_info->total_bytes) {
3517 unused = space_info->total_bytes - unused;
3518 if (unused >= num_bytes) {
3520 space_info->bytes_reserved += orig_bytes;
3524 * Ok set num_bytes to orig_bytes since we aren't
3525 * overocmmitted, this way we only try and reclaim what
3528 num_bytes = orig_bytes;
3532 * Ok we're over committed, set num_bytes to the overcommitted
3533 * amount plus the amount of bytes that we need for this
3536 num_bytes = unused - space_info->total_bytes +
3537 (orig_bytes * (retries + 1));
3541 * Couldn't make our reservation, save our place so while we're trying
3542 * to reclaim space we can actually use it instead of somebody else
3543 * stealing it from us.
3545 if (ret && !reserved) {
3546 space_info->bytes_reserved += orig_bytes;
3550 spin_unlock(&space_info->lock);
3559 * We do synchronous shrinking since we don't actually unreserve
3560 * metadata until after the IO is completed.
3562 ret = shrink_delalloc(trans, root, num_bytes, 1);
3569 * So if we were overcommitted it's possible that somebody else flushed
3570 * out enough space and we simply didn't have enough space to reclaim,
3571 * so go back around and try again.
3578 spin_lock(&space_info->lock);
3580 * Not enough space to be reclaimed, don't bother committing the
3583 if (space_info->bytes_pinned < orig_bytes)
3585 spin_unlock(&space_info->lock);
3590 if (trans || committed)
3594 trans = btrfs_join_transaction(root, 1);
3597 ret = btrfs_commit_transaction(trans, root);
3606 spin_lock(&space_info->lock);
3607 space_info->bytes_reserved -= orig_bytes;
3608 spin_unlock(&space_info->lock);
3614 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3615 struct btrfs_root *root)
3617 struct btrfs_block_rsv *block_rsv;
3619 block_rsv = trans->block_rsv;
3621 block_rsv = root->block_rsv;
3624 block_rsv = &root->fs_info->empty_block_rsv;
3629 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3633 spin_lock(&block_rsv->lock);
3634 if (block_rsv->reserved >= num_bytes) {
3635 block_rsv->reserved -= num_bytes;
3636 if (block_rsv->reserved < block_rsv->size)
3637 block_rsv->full = 0;
3640 spin_unlock(&block_rsv->lock);
3644 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3645 u64 num_bytes, int update_size)
3647 spin_lock(&block_rsv->lock);
3648 block_rsv->reserved += num_bytes;
3650 block_rsv->size += num_bytes;
3651 else if (block_rsv->reserved >= block_rsv->size)
3652 block_rsv->full = 1;
3653 spin_unlock(&block_rsv->lock);
3656 void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3657 struct btrfs_block_rsv *dest, u64 num_bytes)
3659 struct btrfs_space_info *space_info = block_rsv->space_info;
3661 spin_lock(&block_rsv->lock);
3662 if (num_bytes == (u64)-1)
3663 num_bytes = block_rsv->size;
3664 block_rsv->size -= num_bytes;
3665 if (block_rsv->reserved >= block_rsv->size) {
3666 num_bytes = block_rsv->reserved - block_rsv->size;
3667 block_rsv->reserved = block_rsv->size;
3668 block_rsv->full = 1;
3672 spin_unlock(&block_rsv->lock);
3674 if (num_bytes > 0) {
3676 spin_lock(&dest->lock);
3680 bytes_to_add = dest->size - dest->reserved;
3681 bytes_to_add = min(num_bytes, bytes_to_add);
3682 dest->reserved += bytes_to_add;
3683 if (dest->reserved >= dest->size)
3685 num_bytes -= bytes_to_add;
3687 spin_unlock(&dest->lock);
3690 spin_lock(&space_info->lock);
3691 space_info->bytes_reserved -= num_bytes;
3692 space_info->reservation_progress++;
3693 spin_unlock(&space_info->lock);
3698 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3699 struct btrfs_block_rsv *dst, u64 num_bytes)
3703 ret = block_rsv_use_bytes(src, num_bytes);
3707 block_rsv_add_bytes(dst, num_bytes, 1);
3711 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3713 memset(rsv, 0, sizeof(*rsv));
3714 spin_lock_init(&rsv->lock);
3715 atomic_set(&rsv->usage, 1);
3717 INIT_LIST_HEAD(&rsv->list);
3720 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3722 struct btrfs_block_rsv *block_rsv;
3723 struct btrfs_fs_info *fs_info = root->fs_info;
3725 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3729 btrfs_init_block_rsv(block_rsv);
3730 block_rsv->space_info = __find_space_info(fs_info,
3731 BTRFS_BLOCK_GROUP_METADATA);
3735 void btrfs_free_block_rsv(struct btrfs_root *root,
3736 struct btrfs_block_rsv *rsv)
3738 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3739 btrfs_block_rsv_release(root, rsv, (u64)-1);
3746 * make the block_rsv struct be able to capture freed space.
3747 * the captured space will re-add to the the block_rsv struct
3748 * after transaction commit
3750 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3751 struct btrfs_block_rsv *block_rsv)
3753 block_rsv->durable = 1;
3754 mutex_lock(&fs_info->durable_block_rsv_mutex);
3755 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3756 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3759 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3760 struct btrfs_root *root,
3761 struct btrfs_block_rsv *block_rsv,
3769 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
3771 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3778 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3779 struct btrfs_root *root,
3780 struct btrfs_block_rsv *block_rsv,
3781 u64 min_reserved, int min_factor)
3784 int commit_trans = 0;
3790 spin_lock(&block_rsv->lock);
3792 num_bytes = div_factor(block_rsv->size, min_factor);
3793 if (min_reserved > num_bytes)
3794 num_bytes = min_reserved;
3796 if (block_rsv->reserved >= num_bytes) {
3799 num_bytes -= block_rsv->reserved;
3800 if (block_rsv->durable &&
3801 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3804 spin_unlock(&block_rsv->lock);
3808 if (block_rsv->refill_used) {
3809 ret = reserve_metadata_bytes(trans, root, block_rsv,
3812 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3821 trans = btrfs_join_transaction(root, 1);
3822 BUG_ON(IS_ERR(trans));
3823 ret = btrfs_commit_transaction(trans, root);
3830 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3831 struct btrfs_block_rsv *dst_rsv,
3834 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3837 void btrfs_block_rsv_release(struct btrfs_root *root,
3838 struct btrfs_block_rsv *block_rsv,
3841 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3842 if (global_rsv->full || global_rsv == block_rsv ||
3843 block_rsv->space_info != global_rsv->space_info)
3845 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3849 * helper to calculate size of global block reservation.
3850 * the desired value is sum of space used by extent tree,
3851 * checksum tree and root tree
3853 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3855 struct btrfs_space_info *sinfo;
3859 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3862 * per tree used space accounting can be inaccuracy, so we
3865 spin_lock(&fs_info->extent_root->accounting_lock);
3866 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3867 spin_unlock(&fs_info->extent_root->accounting_lock);
3869 spin_lock(&fs_info->csum_root->accounting_lock);
3870 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3871 spin_unlock(&fs_info->csum_root->accounting_lock);
3873 spin_lock(&fs_info->tree_root->accounting_lock);
3874 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3875 spin_unlock(&fs_info->tree_root->accounting_lock);
3877 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3878 spin_lock(&sinfo->lock);
3879 data_used = sinfo->bytes_used;
3880 spin_unlock(&sinfo->lock);
3882 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3883 spin_lock(&sinfo->lock);
3884 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3886 meta_used = sinfo->bytes_used;
3887 spin_unlock(&sinfo->lock);
3889 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3891 num_bytes += div64_u64(data_used + meta_used, 50);
3893 if (num_bytes * 3 > meta_used)
3894 num_bytes = div64_u64(meta_used, 3);
3896 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3899 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3901 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3902 struct btrfs_space_info *sinfo = block_rsv->space_info;
3905 num_bytes = calc_global_metadata_size(fs_info);
3907 spin_lock(&block_rsv->lock);
3908 spin_lock(&sinfo->lock);
3910 block_rsv->size = num_bytes;
3912 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3913 sinfo->bytes_reserved + sinfo->bytes_readonly +
3914 sinfo->bytes_may_use;
3916 if (sinfo->total_bytes > num_bytes) {
3917 num_bytes = sinfo->total_bytes - num_bytes;
3918 block_rsv->reserved += num_bytes;
3919 sinfo->bytes_reserved += num_bytes;
3922 if (block_rsv->reserved >= block_rsv->size) {
3923 num_bytes = block_rsv->reserved - block_rsv->size;
3924 sinfo->bytes_reserved -= num_bytes;
3925 sinfo->reservation_progress++;
3926 block_rsv->reserved = block_rsv->size;
3927 block_rsv->full = 1;
3930 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3931 block_rsv->size, block_rsv->reserved);
3933 spin_unlock(&sinfo->lock);
3934 spin_unlock(&block_rsv->lock);
3937 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3939 struct btrfs_space_info *space_info;
3941 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3942 fs_info->chunk_block_rsv.space_info = space_info;
3943 fs_info->chunk_block_rsv.priority = 10;
3945 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3946 fs_info->global_block_rsv.space_info = space_info;
3947 fs_info->global_block_rsv.priority = 10;
3948 fs_info->global_block_rsv.refill_used = 1;
3949 fs_info->delalloc_block_rsv.space_info = space_info;
3950 fs_info->trans_block_rsv.space_info = space_info;
3951 fs_info->empty_block_rsv.space_info = space_info;
3952 fs_info->empty_block_rsv.priority = 10;
3954 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3955 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3956 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3957 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3958 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3960 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3962 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3964 update_global_block_rsv(fs_info);
3967 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3969 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3970 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3971 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3972 WARN_ON(fs_info->trans_block_rsv.size > 0);
3973 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3974 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3975 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3978 static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3980 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3984 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3985 struct btrfs_root *root,
3991 if (num_items == 0 || root->fs_info->chunk_root == root)
3994 num_bytes = calc_trans_metadata_size(root, num_items);
3995 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
3998 trans->bytes_reserved += num_bytes;
3999 trans->block_rsv = &root->fs_info->trans_block_rsv;
4004 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4005 struct btrfs_root *root)
4007 if (!trans->bytes_reserved)
4010 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4011 btrfs_block_rsv_release(root, trans->block_rsv,
4012 trans->bytes_reserved);
4013 trans->bytes_reserved = 0;
4016 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4017 struct inode *inode)
4019 struct btrfs_root *root = BTRFS_I(inode)->root;
4020 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4021 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4024 * one for deleting orphan item, one for updating inode and
4025 * two for calling btrfs_truncate_inode_items.
4027 * btrfs_truncate_inode_items is a delete operation, it frees
4028 * more space than it uses in most cases. So two units of
4029 * metadata space should be enough for calling it many times.
4030 * If all of the metadata space is used, we can commit
4031 * transaction and use space it freed.
4033 u64 num_bytes = calc_trans_metadata_size(root, 4);
4034 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4037 void btrfs_orphan_release_metadata(struct inode *inode)
4039 struct btrfs_root *root = BTRFS_I(inode)->root;
4040 u64 num_bytes = calc_trans_metadata_size(root, 4);
4041 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4044 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4045 struct btrfs_pending_snapshot *pending)
4047 struct btrfs_root *root = pending->root;
4048 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4049 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4051 * two for root back/forward refs, two for directory entries
4052 * and one for root of the snapshot.
4054 u64 num_bytes = calc_trans_metadata_size(root, 5);
4055 dst_rsv->space_info = src_rsv->space_info;
4056 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4059 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4061 return num_bytes >>= 3;
4064 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4066 struct btrfs_root *root = BTRFS_I(inode)->root;
4067 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4070 int reserved_extents;
4073 if (btrfs_transaction_in_commit(root->fs_info))
4074 schedule_timeout(1);
4076 num_bytes = ALIGN(num_bytes, root->sectorsize);
4078 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
4079 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4081 if (nr_extents > reserved_extents) {
4082 nr_extents -= reserved_extents;
4083 to_reserve = calc_trans_metadata_size(root, nr_extents);
4089 to_reserve += calc_csum_metadata_size(inode, num_bytes);
4090 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4094 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
4095 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
4097 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4099 if (block_rsv->size > 512 * 1024 * 1024)
4100 shrink_delalloc(NULL, root, to_reserve, 0);
4105 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4107 struct btrfs_root *root = BTRFS_I(inode)->root;
4110 int reserved_extents;
4112 num_bytes = ALIGN(num_bytes, root->sectorsize);
4113 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
4114 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
4116 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4120 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4121 if (nr_extents >= reserved_extents) {
4125 old = reserved_extents;
4126 nr_extents = reserved_extents - nr_extents;
4127 new = reserved_extents - nr_extents;
4128 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4129 reserved_extents, new);
4130 if (likely(old == reserved_extents))
4132 reserved_extents = old;
4135 to_free = calc_csum_metadata_size(inode, num_bytes);
4137 to_free += calc_trans_metadata_size(root, nr_extents);
4139 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4143 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4147 ret = btrfs_check_data_free_space(inode, num_bytes);
4151 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4153 btrfs_free_reserved_data_space(inode, num_bytes);
4160 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4162 btrfs_delalloc_release_metadata(inode, num_bytes);
4163 btrfs_free_reserved_data_space(inode, num_bytes);
4166 static int update_block_group(struct btrfs_trans_handle *trans,
4167 struct btrfs_root *root,
4168 u64 bytenr, u64 num_bytes, int alloc)
4170 struct btrfs_block_group_cache *cache = NULL;
4171 struct btrfs_fs_info *info = root->fs_info;
4172 u64 total = num_bytes;
4177 /* block accounting for super block */
4178 spin_lock(&info->delalloc_lock);
4179 old_val = btrfs_super_bytes_used(&info->super_copy);
4181 old_val += num_bytes;
4183 old_val -= num_bytes;
4184 btrfs_set_super_bytes_used(&info->super_copy, old_val);
4185 spin_unlock(&info->delalloc_lock);
4188 cache = btrfs_lookup_block_group(info, bytenr);
4191 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4192 BTRFS_BLOCK_GROUP_RAID1 |
4193 BTRFS_BLOCK_GROUP_RAID10))
4198 * If this block group has free space cache written out, we
4199 * need to make sure to load it if we are removing space. This
4200 * is because we need the unpinning stage to actually add the
4201 * space back to the block group, otherwise we will leak space.
4203 if (!alloc && cache->cached == BTRFS_CACHE_NO)
4204 cache_block_group(cache, trans, NULL, 1);
4206 byte_in_group = bytenr - cache->key.objectid;
4207 WARN_ON(byte_in_group > cache->key.offset);
4209 spin_lock(&cache->space_info->lock);
4210 spin_lock(&cache->lock);
4212 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4213 cache->disk_cache_state < BTRFS_DC_CLEAR)
4214 cache->disk_cache_state = BTRFS_DC_CLEAR;
4217 old_val = btrfs_block_group_used(&cache->item);
4218 num_bytes = min(total, cache->key.offset - byte_in_group);
4220 old_val += num_bytes;
4221 btrfs_set_block_group_used(&cache->item, old_val);
4222 cache->reserved -= num_bytes;
4223 cache->space_info->bytes_reserved -= num_bytes;
4224 cache->space_info->reservation_progress++;
4225 cache->space_info->bytes_used += num_bytes;
4226 cache->space_info->disk_used += num_bytes * factor;
4227 spin_unlock(&cache->lock);
4228 spin_unlock(&cache->space_info->lock);
4230 old_val -= num_bytes;
4231 btrfs_set_block_group_used(&cache->item, old_val);
4232 cache->pinned += num_bytes;
4233 cache->space_info->bytes_pinned += num_bytes;
4234 cache->space_info->bytes_used -= num_bytes;
4235 cache->space_info->disk_used -= num_bytes * factor;
4236 spin_unlock(&cache->lock);
4237 spin_unlock(&cache->space_info->lock);
4239 set_extent_dirty(info->pinned_extents,
4240 bytenr, bytenr + num_bytes - 1,
4241 GFP_NOFS | __GFP_NOFAIL);
4243 btrfs_put_block_group(cache);
4245 bytenr += num_bytes;
4250 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4252 struct btrfs_block_group_cache *cache;
4255 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4259 bytenr = cache->key.objectid;
4260 btrfs_put_block_group(cache);
4265 static int pin_down_extent(struct btrfs_root *root,
4266 struct btrfs_block_group_cache *cache,
4267 u64 bytenr, u64 num_bytes, int reserved)
4269 spin_lock(&cache->space_info->lock);
4270 spin_lock(&cache->lock);
4271 cache->pinned += num_bytes;
4272 cache->space_info->bytes_pinned += num_bytes;
4274 cache->reserved -= num_bytes;
4275 cache->space_info->bytes_reserved -= num_bytes;
4276 cache->space_info->reservation_progress++;
4278 spin_unlock(&cache->lock);
4279 spin_unlock(&cache->space_info->lock);
4281 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4282 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
4287 * this function must be called within transaction
4289 int btrfs_pin_extent(struct btrfs_root *root,
4290 u64 bytenr, u64 num_bytes, int reserved)
4292 struct btrfs_block_group_cache *cache;
4294 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4297 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4299 btrfs_put_block_group(cache);
4304 * update size of reserved extents. this function may return -EAGAIN
4305 * if 'reserve' is true or 'sinfo' is false.
4307 int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4308 u64 num_bytes, int reserve, int sinfo)
4312 struct btrfs_space_info *space_info = cache->space_info;
4313 spin_lock(&space_info->lock);
4314 spin_lock(&cache->lock);
4319 cache->reserved += num_bytes;
4320 space_info->bytes_reserved += num_bytes;
4324 space_info->bytes_readonly += num_bytes;
4325 cache->reserved -= num_bytes;
4326 space_info->bytes_reserved -= num_bytes;
4327 space_info->reservation_progress++;
4329 spin_unlock(&cache->lock);
4330 spin_unlock(&space_info->lock);
4332 spin_lock(&cache->lock);
4337 cache->reserved += num_bytes;
4339 cache->reserved -= num_bytes;
4341 spin_unlock(&cache->lock);
4346 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4347 struct btrfs_root *root)
4349 struct btrfs_fs_info *fs_info = root->fs_info;
4350 struct btrfs_caching_control *next;
4351 struct btrfs_caching_control *caching_ctl;
4352 struct btrfs_block_group_cache *cache;
4354 down_write(&fs_info->extent_commit_sem);
4356 list_for_each_entry_safe(caching_ctl, next,
4357 &fs_info->caching_block_groups, list) {
4358 cache = caching_ctl->block_group;
4359 if (block_group_cache_done(cache)) {
4360 cache->last_byte_to_unpin = (u64)-1;
4361 list_del_init(&caching_ctl->list);
4362 put_caching_control(caching_ctl);
4364 cache->last_byte_to_unpin = caching_ctl->progress;
4368 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4369 fs_info->pinned_extents = &fs_info->freed_extents[1];
4371 fs_info->pinned_extents = &fs_info->freed_extents[0];
4373 up_write(&fs_info->extent_commit_sem);
4375 update_global_block_rsv(fs_info);
4379 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4381 struct btrfs_fs_info *fs_info = root->fs_info;
4382 struct btrfs_block_group_cache *cache = NULL;
4385 while (start <= end) {
4387 start >= cache->key.objectid + cache->key.offset) {
4389 btrfs_put_block_group(cache);
4390 cache = btrfs_lookup_block_group(fs_info, start);
4394 len = cache->key.objectid + cache->key.offset - start;
4395 len = min(len, end + 1 - start);
4397 if (start < cache->last_byte_to_unpin) {
4398 len = min(len, cache->last_byte_to_unpin - start);
4399 btrfs_add_free_space(cache, start, len);
4404 spin_lock(&cache->space_info->lock);
4405 spin_lock(&cache->lock);
4406 cache->pinned -= len;
4407 cache->space_info->bytes_pinned -= len;
4409 cache->space_info->bytes_readonly += len;
4410 } else if (cache->reserved_pinned > 0) {
4411 len = min(len, cache->reserved_pinned);
4412 cache->reserved_pinned -= len;
4413 cache->space_info->bytes_reserved += len;
4415 spin_unlock(&cache->lock);
4416 spin_unlock(&cache->space_info->lock);
4420 btrfs_put_block_group(cache);
4424 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4425 struct btrfs_root *root)
4427 struct btrfs_fs_info *fs_info = root->fs_info;
4428 struct extent_io_tree *unpin;
4429 struct btrfs_block_rsv *block_rsv;
4430 struct btrfs_block_rsv *next_rsv;
4436 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4437 unpin = &fs_info->freed_extents[1];
4439 unpin = &fs_info->freed_extents[0];
4442 ret = find_first_extent_bit(unpin, 0, &start, &end,
4447 if (btrfs_test_opt(root, DISCARD))
4448 ret = btrfs_discard_extent(root, start,
4449 end + 1 - start, NULL);
4451 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4452 unpin_extent_range(root, start, end);
4456 mutex_lock(&fs_info->durable_block_rsv_mutex);
4457 list_for_each_entry_safe(block_rsv, next_rsv,
4458 &fs_info->durable_block_rsv_list, list) {
4460 idx = trans->transid & 0x1;
4461 if (block_rsv->freed[idx] > 0) {
4462 block_rsv_add_bytes(block_rsv,
4463 block_rsv->freed[idx], 0);
4464 block_rsv->freed[idx] = 0;
4466 if (atomic_read(&block_rsv->usage) == 0) {
4467 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4469 if (block_rsv->freed[0] == 0 &&
4470 block_rsv->freed[1] == 0) {
4471 list_del_init(&block_rsv->list);
4475 btrfs_block_rsv_release(root, block_rsv, 0);
4478 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4483 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4484 struct btrfs_root *root,
4485 u64 bytenr, u64 num_bytes, u64 parent,
4486 u64 root_objectid, u64 owner_objectid,
4487 u64 owner_offset, int refs_to_drop,
4488 struct btrfs_delayed_extent_op *extent_op)
4490 struct btrfs_key key;
4491 struct btrfs_path *path;
4492 struct btrfs_fs_info *info = root->fs_info;
4493 struct btrfs_root *extent_root = info->extent_root;
4494 struct extent_buffer *leaf;
4495 struct btrfs_extent_item *ei;
4496 struct btrfs_extent_inline_ref *iref;
4499 int extent_slot = 0;
4500 int found_extent = 0;
4505 path = btrfs_alloc_path();
4510 path->leave_spinning = 1;
4512 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4513 BUG_ON(!is_data && refs_to_drop != 1);
4515 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4516 bytenr, num_bytes, parent,
4517 root_objectid, owner_objectid,
4520 extent_slot = path->slots[0];
4521 while (extent_slot >= 0) {
4522 btrfs_item_key_to_cpu(path->nodes[0], &key,
4524 if (key.objectid != bytenr)
4526 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4527 key.offset == num_bytes) {
4531 if (path->slots[0] - extent_slot > 5)
4535 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4536 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4537 if (found_extent && item_size < sizeof(*ei))
4540 if (!found_extent) {
4542 ret = remove_extent_backref(trans, extent_root, path,
4546 btrfs_release_path(extent_root, path);
4547 path->leave_spinning = 1;
4549 key.objectid = bytenr;
4550 key.type = BTRFS_EXTENT_ITEM_KEY;
4551 key.offset = num_bytes;
4553 ret = btrfs_search_slot(trans, extent_root,
4556 printk(KERN_ERR "umm, got %d back from search"
4557 ", was looking for %llu\n", ret,
4558 (unsigned long long)bytenr);
4559 btrfs_print_leaf(extent_root, path->nodes[0]);
4562 extent_slot = path->slots[0];
4565 btrfs_print_leaf(extent_root, path->nodes[0]);
4567 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4568 "parent %llu root %llu owner %llu offset %llu\n",
4569 (unsigned long long)bytenr,
4570 (unsigned long long)parent,
4571 (unsigned long long)root_objectid,
4572 (unsigned long long)owner_objectid,
4573 (unsigned long long)owner_offset);
4576 leaf = path->nodes[0];
4577 item_size = btrfs_item_size_nr(leaf, extent_slot);
4578 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4579 if (item_size < sizeof(*ei)) {
4580 BUG_ON(found_extent || extent_slot != path->slots[0]);
4581 ret = convert_extent_item_v0(trans, extent_root, path,
4585 btrfs_release_path(extent_root, path);
4586 path->leave_spinning = 1;
4588 key.objectid = bytenr;
4589 key.type = BTRFS_EXTENT_ITEM_KEY;
4590 key.offset = num_bytes;
4592 ret = btrfs_search_slot(trans, extent_root, &key, path,
4595 printk(KERN_ERR "umm, got %d back from search"
4596 ", was looking for %llu\n", ret,
4597 (unsigned long long)bytenr);
4598 btrfs_print_leaf(extent_root, path->nodes[0]);
4601 extent_slot = path->slots[0];
4602 leaf = path->nodes[0];
4603 item_size = btrfs_item_size_nr(leaf, extent_slot);
4606 BUG_ON(item_size < sizeof(*ei));
4607 ei = btrfs_item_ptr(leaf, extent_slot,
4608 struct btrfs_extent_item);
4609 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4610 struct btrfs_tree_block_info *bi;
4611 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4612 bi = (struct btrfs_tree_block_info *)(ei + 1);
4613 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4616 refs = btrfs_extent_refs(leaf, ei);
4617 BUG_ON(refs < refs_to_drop);
4618 refs -= refs_to_drop;
4622 __run_delayed_extent_op(extent_op, leaf, ei);
4624 * In the case of inline back ref, reference count will
4625 * be updated by remove_extent_backref
4628 BUG_ON(!found_extent);
4630 btrfs_set_extent_refs(leaf, ei, refs);
4631 btrfs_mark_buffer_dirty(leaf);
4634 ret = remove_extent_backref(trans, extent_root, path,
4641 BUG_ON(is_data && refs_to_drop !=
4642 extent_data_ref_count(root, path, iref));
4644 BUG_ON(path->slots[0] != extent_slot);
4646 BUG_ON(path->slots[0] != extent_slot + 1);
4647 path->slots[0] = extent_slot;
4652 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4655 btrfs_release_path(extent_root, path);
4658 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4661 invalidate_mapping_pages(info->btree_inode->i_mapping,
4662 bytenr >> PAGE_CACHE_SHIFT,
4663 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4666 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4669 btrfs_free_path(path);
4674 * when we free an block, it is possible (and likely) that we free the last
4675 * delayed ref for that extent as well. This searches the delayed ref tree for
4676 * a given extent, and if there are no other delayed refs to be processed, it
4677 * removes it from the tree.
4679 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4680 struct btrfs_root *root, u64 bytenr)
4682 struct btrfs_delayed_ref_head *head;
4683 struct btrfs_delayed_ref_root *delayed_refs;
4684 struct btrfs_delayed_ref_node *ref;
4685 struct rb_node *node;
4688 delayed_refs = &trans->transaction->delayed_refs;
4689 spin_lock(&delayed_refs->lock);
4690 head = btrfs_find_delayed_ref_head(trans, bytenr);
4694 node = rb_prev(&head->node.rb_node);
4698 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4700 /* there are still entries for this ref, we can't drop it */
4701 if (ref->bytenr == bytenr)
4704 if (head->extent_op) {
4705 if (!head->must_insert_reserved)
4707 kfree(head->extent_op);
4708 head->extent_op = NULL;
4712 * waiting for the lock here would deadlock. If someone else has it
4713 * locked they are already in the process of dropping it anyway
4715 if (!mutex_trylock(&head->mutex))
4719 * at this point we have a head with no other entries. Go
4720 * ahead and process it.
4722 head->node.in_tree = 0;
4723 rb_erase(&head->node.rb_node, &delayed_refs->root);
4725 delayed_refs->num_entries--;
4728 * we don't take a ref on the node because we're removing it from the
4729 * tree, so we just steal the ref the tree was holding.
4731 delayed_refs->num_heads--;
4732 if (list_empty(&head->cluster))
4733 delayed_refs->num_heads_ready--;
4735 list_del_init(&head->cluster);
4736 spin_unlock(&delayed_refs->lock);
4738 BUG_ON(head->extent_op);
4739 if (head->must_insert_reserved)
4742 mutex_unlock(&head->mutex);
4743 btrfs_put_delayed_ref(&head->node);
4746 spin_unlock(&delayed_refs->lock);
4750 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4751 struct btrfs_root *root,
4752 struct extent_buffer *buf,
4753 u64 parent, int last_ref)
4755 struct btrfs_block_rsv *block_rsv;
4756 struct btrfs_block_group_cache *cache = NULL;
4759 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4760 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4761 parent, root->root_key.objectid,
4762 btrfs_header_level(buf),
4763 BTRFS_DROP_DELAYED_REF, NULL);
4770 block_rsv = get_block_rsv(trans, root);
4771 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4772 if (block_rsv->space_info != cache->space_info)
4775 if (btrfs_header_generation(buf) == trans->transid) {
4776 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4777 ret = check_ref_cleanup(trans, root, buf->start);
4782 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4783 pin_down_extent(root, cache, buf->start, buf->len, 1);
4787 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4789 btrfs_add_free_space(cache, buf->start, buf->len);
4790 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
4791 if (ret == -EAGAIN) {
4792 /* block group became read-only */
4793 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
4798 spin_lock(&block_rsv->lock);
4799 if (block_rsv->reserved < block_rsv->size) {
4800 block_rsv->reserved += buf->len;
4803 spin_unlock(&block_rsv->lock);
4806 spin_lock(&cache->space_info->lock);
4807 cache->space_info->bytes_reserved -= buf->len;
4808 cache->space_info->reservation_progress++;
4809 spin_unlock(&cache->space_info->lock);
4814 if (block_rsv->durable && !cache->ro) {
4816 spin_lock(&cache->lock);
4818 cache->reserved_pinned += buf->len;
4821 spin_unlock(&cache->lock);
4824 spin_lock(&block_rsv->lock);
4825 block_rsv->freed[trans->transid & 0x1] += buf->len;
4826 spin_unlock(&block_rsv->lock);
4831 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4834 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
4835 btrfs_put_block_group(cache);
4838 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4839 struct btrfs_root *root,
4840 u64 bytenr, u64 num_bytes, u64 parent,
4841 u64 root_objectid, u64 owner, u64 offset)
4846 * tree log blocks never actually go into the extent allocation
4847 * tree, just update pinning info and exit early.
4849 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4850 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4851 /* unlocks the pinned mutex */
4852 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4854 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4855 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4856 parent, root_objectid, (int)owner,
4857 BTRFS_DROP_DELAYED_REF, NULL);
4860 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4861 parent, root_objectid, owner,
4862 offset, BTRFS_DROP_DELAYED_REF, NULL);
4868 static u64 stripe_align(struct btrfs_root *root, u64 val)
4870 u64 mask = ((u64)root->stripesize - 1);
4871 u64 ret = (val + mask) & ~mask;
4876 * when we wait for progress in the block group caching, its because
4877 * our allocation attempt failed at least once. So, we must sleep
4878 * and let some progress happen before we try again.
4880 * This function will sleep at least once waiting for new free space to
4881 * show up, and then it will check the block group free space numbers
4882 * for our min num_bytes. Another option is to have it go ahead
4883 * and look in the rbtree for a free extent of a given size, but this
4887 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4890 struct btrfs_caching_control *caching_ctl;
4893 caching_ctl = get_caching_control(cache);
4897 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4898 (cache->free_space_ctl->free_space >= num_bytes));
4900 put_caching_control(caching_ctl);
4905 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4907 struct btrfs_caching_control *caching_ctl;
4910 caching_ctl = get_caching_control(cache);
4914 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4916 put_caching_control(caching_ctl);
4920 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4923 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4925 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4927 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4929 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4936 enum btrfs_loop_type {
4937 LOOP_FIND_IDEAL = 0,
4938 LOOP_CACHING_NOWAIT = 1,
4939 LOOP_CACHING_WAIT = 2,
4940 LOOP_ALLOC_CHUNK = 3,
4941 LOOP_NO_EMPTY_SIZE = 4,
4945 * walks the btree of allocated extents and find a hole of a given size.
4946 * The key ins is changed to record the hole:
4947 * ins->objectid == block start
4948 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4949 * ins->offset == number of blocks
4950 * Any available blocks before search_start are skipped.
4952 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4953 struct btrfs_root *orig_root,
4954 u64 num_bytes, u64 empty_size,
4955 u64 search_start, u64 search_end,
4956 u64 hint_byte, struct btrfs_key *ins,
4960 struct btrfs_root *root = orig_root->fs_info->extent_root;
4961 struct btrfs_free_cluster *last_ptr = NULL;
4962 struct btrfs_block_group_cache *block_group = NULL;
4963 int empty_cluster = 2 * 1024 * 1024;
4964 int allowed_chunk_alloc = 0;
4965 int done_chunk_alloc = 0;
4966 struct btrfs_space_info *space_info;
4967 int last_ptr_loop = 0;
4970 bool found_uncached_bg = false;
4971 bool failed_cluster_refill = false;
4972 bool failed_alloc = false;
4973 bool use_cluster = true;
4974 u64 ideal_cache_percent = 0;
4975 u64 ideal_cache_offset = 0;
4977 WARN_ON(num_bytes < root->sectorsize);
4978 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4982 space_info = __find_space_info(root->fs_info, data);
4984 printk(KERN_ERR "No space info for %d\n", data);
4989 * If the space info is for both data and metadata it means we have a
4990 * small filesystem and we can't use the clustering stuff.
4992 if (btrfs_mixed_space_info(space_info))
4993 use_cluster = false;
4995 if (orig_root->ref_cows || empty_size)
4996 allowed_chunk_alloc = 1;
4998 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
4999 last_ptr = &root->fs_info->meta_alloc_cluster;
5000 if (!btrfs_test_opt(root, SSD))
5001 empty_cluster = 64 * 1024;
5004 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5005 btrfs_test_opt(root, SSD)) {
5006 last_ptr = &root->fs_info->data_alloc_cluster;
5010 spin_lock(&last_ptr->lock);
5011 if (last_ptr->block_group)
5012 hint_byte = last_ptr->window_start;
5013 spin_unlock(&last_ptr->lock);
5016 search_start = max(search_start, first_logical_byte(root, 0));
5017 search_start = max(search_start, hint_byte);
5022 if (search_start == hint_byte) {
5024 block_group = btrfs_lookup_block_group(root->fs_info,
5027 * we don't want to use the block group if it doesn't match our
5028 * allocation bits, or if its not cached.
5030 * However if we are re-searching with an ideal block group
5031 * picked out then we don't care that the block group is cached.
5033 if (block_group && block_group_bits(block_group, data) &&
5034 (block_group->cached != BTRFS_CACHE_NO ||
5035 search_start == ideal_cache_offset)) {
5036 down_read(&space_info->groups_sem);
5037 if (list_empty(&block_group->list) ||
5040 * someone is removing this block group,
5041 * we can't jump into the have_block_group
5042 * target because our list pointers are not
5045 btrfs_put_block_group(block_group);
5046 up_read(&space_info->groups_sem);
5048 index = get_block_group_index(block_group);
5049 goto have_block_group;
5051 } else if (block_group) {
5052 btrfs_put_block_group(block_group);
5056 down_read(&space_info->groups_sem);
5057 list_for_each_entry(block_group, &space_info->block_groups[index],
5062 btrfs_get_block_group(block_group);
5063 search_start = block_group->key.objectid;
5066 * this can happen if we end up cycling through all the
5067 * raid types, but we want to make sure we only allocate
5068 * for the proper type.
5070 if (!block_group_bits(block_group, data)) {
5071 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5072 BTRFS_BLOCK_GROUP_RAID1 |
5073 BTRFS_BLOCK_GROUP_RAID10;
5076 * if they asked for extra copies and this block group
5077 * doesn't provide them, bail. This does allow us to
5078 * fill raid0 from raid1.
5080 if ((data & extra) && !(block_group->flags & extra))
5085 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
5088 ret = cache_block_group(block_group, trans,
5090 if (block_group->cached == BTRFS_CACHE_FINISHED)
5091 goto have_block_group;
5093 free_percent = btrfs_block_group_used(&block_group->item);
5094 free_percent *= 100;
5095 free_percent = div64_u64(free_percent,
5096 block_group->key.offset);
5097 free_percent = 100 - free_percent;
5098 if (free_percent > ideal_cache_percent &&
5099 likely(!block_group->ro)) {
5100 ideal_cache_offset = block_group->key.objectid;
5101 ideal_cache_percent = free_percent;
5105 * We only want to start kthread caching if we are at
5106 * the point where we will wait for caching to make
5107 * progress, or if our ideal search is over and we've
5108 * found somebody to start caching.
5110 if (loop > LOOP_CACHING_NOWAIT ||
5111 (loop > LOOP_FIND_IDEAL &&
5112 atomic_read(&space_info->caching_threads) < 2)) {
5113 ret = cache_block_group(block_group, trans,
5117 found_uncached_bg = true;
5120 * If loop is set for cached only, try the next block
5123 if (loop == LOOP_FIND_IDEAL)
5127 cached = block_group_cache_done(block_group);
5128 if (unlikely(!cached))
5129 found_uncached_bg = true;
5131 if (unlikely(block_group->ro))
5135 * Ok we want to try and use the cluster allocator, so lets look
5136 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5137 * have tried the cluster allocator plenty of times at this
5138 * point and not have found anything, so we are likely way too
5139 * fragmented for the clustering stuff to find anything, so lets
5140 * just skip it and let the allocator find whatever block it can
5143 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
5145 * the refill lock keeps out other
5146 * people trying to start a new cluster
5148 spin_lock(&last_ptr->refill_lock);
5149 if (last_ptr->block_group &&
5150 (last_ptr->block_group->ro ||
5151 !block_group_bits(last_ptr->block_group, data))) {
5153 goto refill_cluster;
5156 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5157 num_bytes, search_start);
5159 /* we have a block, we're done */
5160 spin_unlock(&last_ptr->refill_lock);
5164 spin_lock(&last_ptr->lock);
5166 * whoops, this cluster doesn't actually point to
5167 * this block group. Get a ref on the block
5168 * group is does point to and try again
5170 if (!last_ptr_loop && last_ptr->block_group &&
5171 last_ptr->block_group != block_group) {
5173 btrfs_put_block_group(block_group);
5174 block_group = last_ptr->block_group;
5175 btrfs_get_block_group(block_group);
5176 spin_unlock(&last_ptr->lock);
5177 spin_unlock(&last_ptr->refill_lock);
5180 search_start = block_group->key.objectid;
5182 * we know this block group is properly
5183 * in the list because
5184 * btrfs_remove_block_group, drops the
5185 * cluster before it removes the block
5186 * group from the list
5188 goto have_block_group;
5190 spin_unlock(&last_ptr->lock);
5193 * this cluster didn't work out, free it and
5196 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5200 /* allocate a cluster in this block group */
5201 ret = btrfs_find_space_cluster(trans, root,
5202 block_group, last_ptr,
5204 empty_cluster + empty_size);
5207 * now pull our allocation out of this
5210 offset = btrfs_alloc_from_cluster(block_group,
5211 last_ptr, num_bytes,
5214 /* we found one, proceed */
5215 spin_unlock(&last_ptr->refill_lock);
5218 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5219 && !failed_cluster_refill) {
5220 spin_unlock(&last_ptr->refill_lock);
5222 failed_cluster_refill = true;
5223 wait_block_group_cache_progress(block_group,
5224 num_bytes + empty_cluster + empty_size);
5225 goto have_block_group;
5229 * at this point we either didn't find a cluster
5230 * or we weren't able to allocate a block from our
5231 * cluster. Free the cluster we've been trying
5232 * to use, and go to the next block group
5234 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5235 spin_unlock(&last_ptr->refill_lock);
5239 offset = btrfs_find_space_for_alloc(block_group, search_start,
5240 num_bytes, empty_size);
5242 * If we didn't find a chunk, and we haven't failed on this
5243 * block group before, and this block group is in the middle of
5244 * caching and we are ok with waiting, then go ahead and wait
5245 * for progress to be made, and set failed_alloc to true.
5247 * If failed_alloc is true then we've already waited on this
5248 * block group once and should move on to the next block group.
5250 if (!offset && !failed_alloc && !cached &&
5251 loop > LOOP_CACHING_NOWAIT) {
5252 wait_block_group_cache_progress(block_group,
5253 num_bytes + empty_size);
5254 failed_alloc = true;
5255 goto have_block_group;
5256 } else if (!offset) {
5260 search_start = stripe_align(root, offset);
5261 /* move on to the next group */
5262 if (search_start + num_bytes >= search_end) {
5263 btrfs_add_free_space(block_group, offset, num_bytes);
5267 /* move on to the next group */
5268 if (search_start + num_bytes >
5269 block_group->key.objectid + block_group->key.offset) {
5270 btrfs_add_free_space(block_group, offset, num_bytes);
5274 ins->objectid = search_start;
5275 ins->offset = num_bytes;
5277 if (offset < search_start)
5278 btrfs_add_free_space(block_group, offset,
5279 search_start - offset);
5280 BUG_ON(offset > search_start);
5282 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
5283 (data & BTRFS_BLOCK_GROUP_DATA));
5284 if (ret == -EAGAIN) {
5285 btrfs_add_free_space(block_group, offset, num_bytes);
5289 /* we are all good, lets return */
5290 ins->objectid = search_start;
5291 ins->offset = num_bytes;
5293 if (offset < search_start)
5294 btrfs_add_free_space(block_group, offset,
5295 search_start - offset);
5296 BUG_ON(offset > search_start);
5299 failed_cluster_refill = false;
5300 failed_alloc = false;
5301 BUG_ON(index != get_block_group_index(block_group));
5302 btrfs_put_block_group(block_group);
5304 up_read(&space_info->groups_sem);
5306 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5309 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5310 * for them to make caching progress. Also
5311 * determine the best possible bg to cache
5312 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5313 * caching kthreads as we move along
5314 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5315 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5316 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5319 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5320 (found_uncached_bg || empty_size || empty_cluster ||
5321 allowed_chunk_alloc)) {
5323 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5324 found_uncached_bg = false;
5326 if (!ideal_cache_percent &&
5327 atomic_read(&space_info->caching_threads))
5331 * 1 of the following 2 things have happened so far
5333 * 1) We found an ideal block group for caching that
5334 * is mostly full and will cache quickly, so we might
5335 * as well wait for it.
5337 * 2) We searched for cached only and we didn't find
5338 * anything, and we didn't start any caching kthreads
5339 * either, so chances are we will loop through and
5340 * start a couple caching kthreads, and then come back
5341 * around and just wait for them. This will be slower
5342 * because we will have 2 caching kthreads reading at
5343 * the same time when we could have just started one
5344 * and waited for it to get far enough to give us an
5345 * allocation, so go ahead and go to the wait caching
5348 loop = LOOP_CACHING_WAIT;
5349 search_start = ideal_cache_offset;
5350 ideal_cache_percent = 0;
5352 } else if (loop == LOOP_FIND_IDEAL) {
5354 * Didn't find a uncached bg, wait on anything we find
5357 loop = LOOP_CACHING_WAIT;
5361 if (loop < LOOP_CACHING_WAIT) {
5366 if (loop == LOOP_ALLOC_CHUNK) {
5371 if (allowed_chunk_alloc) {
5372 ret = do_chunk_alloc(trans, root, num_bytes +
5373 2 * 1024 * 1024, data,
5374 CHUNK_ALLOC_LIMITED);
5375 allowed_chunk_alloc = 0;
5376 done_chunk_alloc = 1;
5377 } else if (!done_chunk_alloc &&
5378 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5379 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5382 if (loop < LOOP_NO_EMPTY_SIZE) {
5387 } else if (!ins->objectid) {
5391 /* we found what we needed */
5392 if (ins->objectid) {
5393 if (!(data & BTRFS_BLOCK_GROUP_DATA))
5394 trans->block_group = block_group->key.objectid;
5396 btrfs_put_block_group(block_group);
5403 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5404 int dump_block_groups)
5406 struct btrfs_block_group_cache *cache;
5409 spin_lock(&info->lock);
5410 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5411 (unsigned long long)(info->total_bytes - info->bytes_used -
5412 info->bytes_pinned - info->bytes_reserved -
5413 info->bytes_readonly),
5414 (info->full) ? "" : "not ");
5415 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5416 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5417 (unsigned long long)info->total_bytes,
5418 (unsigned long long)info->bytes_used,
5419 (unsigned long long)info->bytes_pinned,
5420 (unsigned long long)info->bytes_reserved,
5421 (unsigned long long)info->bytes_may_use,
5422 (unsigned long long)info->bytes_readonly);
5423 spin_unlock(&info->lock);
5425 if (!dump_block_groups)
5428 down_read(&info->groups_sem);
5430 list_for_each_entry(cache, &info->block_groups[index], list) {
5431 spin_lock(&cache->lock);
5432 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5433 "%llu pinned %llu reserved\n",
5434 (unsigned long long)cache->key.objectid,
5435 (unsigned long long)cache->key.offset,
5436 (unsigned long long)btrfs_block_group_used(&cache->item),
5437 (unsigned long long)cache->pinned,
5438 (unsigned long long)cache->reserved);
5439 btrfs_dump_free_space(cache, bytes);
5440 spin_unlock(&cache->lock);
5442 if (++index < BTRFS_NR_RAID_TYPES)
5444 up_read(&info->groups_sem);
5447 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5448 struct btrfs_root *root,
5449 u64 num_bytes, u64 min_alloc_size,
5450 u64 empty_size, u64 hint_byte,
5451 u64 search_end, struct btrfs_key *ins,
5455 u64 search_start = 0;
5457 data = btrfs_get_alloc_profile(root, data);
5460 * the only place that sets empty_size is btrfs_realloc_node, which
5461 * is not called recursively on allocations
5463 if (empty_size || root->ref_cows)
5464 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5465 num_bytes + 2 * 1024 * 1024, data,
5466 CHUNK_ALLOC_NO_FORCE);
5468 WARN_ON(num_bytes < root->sectorsize);
5469 ret = find_free_extent(trans, root, num_bytes, empty_size,
5470 search_start, search_end, hint_byte,
5473 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5474 num_bytes = num_bytes >> 1;
5475 num_bytes = num_bytes & ~(root->sectorsize - 1);
5476 num_bytes = max(num_bytes, min_alloc_size);
5477 do_chunk_alloc(trans, root->fs_info->extent_root,
5478 num_bytes, data, CHUNK_ALLOC_FORCE);
5481 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
5482 struct btrfs_space_info *sinfo;
5484 sinfo = __find_space_info(root->fs_info, data);
5485 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5486 "wanted %llu\n", (unsigned long long)data,
5487 (unsigned long long)num_bytes);
5488 dump_space_info(sinfo, num_bytes, 1);
5491 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5496 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5498 struct btrfs_block_group_cache *cache;
5501 cache = btrfs_lookup_block_group(root->fs_info, start);
5503 printk(KERN_ERR "Unable to find block group for %llu\n",
5504 (unsigned long long)start);
5508 if (btrfs_test_opt(root, DISCARD))
5509 ret = btrfs_discard_extent(root, start, len, NULL);
5511 btrfs_add_free_space(cache, start, len);
5512 btrfs_update_reserved_bytes(cache, len, 0, 1);
5513 btrfs_put_block_group(cache);
5515 trace_btrfs_reserved_extent_free(root, start, len);
5520 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5521 struct btrfs_root *root,
5522 u64 parent, u64 root_objectid,
5523 u64 flags, u64 owner, u64 offset,
5524 struct btrfs_key *ins, int ref_mod)
5527 struct btrfs_fs_info *fs_info = root->fs_info;
5528 struct btrfs_extent_item *extent_item;
5529 struct btrfs_extent_inline_ref *iref;
5530 struct btrfs_path *path;
5531 struct extent_buffer *leaf;
5536 type = BTRFS_SHARED_DATA_REF_KEY;
5538 type = BTRFS_EXTENT_DATA_REF_KEY;
5540 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5542 path = btrfs_alloc_path();
5546 path->leave_spinning = 1;
5547 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5551 leaf = path->nodes[0];
5552 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5553 struct btrfs_extent_item);
5554 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5555 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5556 btrfs_set_extent_flags(leaf, extent_item,
5557 flags | BTRFS_EXTENT_FLAG_DATA);
5559 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5560 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5562 struct btrfs_shared_data_ref *ref;
5563 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5564 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5565 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5567 struct btrfs_extent_data_ref *ref;
5568 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5569 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5570 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5571 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5572 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5575 btrfs_mark_buffer_dirty(path->nodes[0]);
5576 btrfs_free_path(path);
5578 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5580 printk(KERN_ERR "btrfs update block group failed for %llu "
5581 "%llu\n", (unsigned long long)ins->objectid,
5582 (unsigned long long)ins->offset);
5588 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5589 struct btrfs_root *root,
5590 u64 parent, u64 root_objectid,
5591 u64 flags, struct btrfs_disk_key *key,
5592 int level, struct btrfs_key *ins)
5595 struct btrfs_fs_info *fs_info = root->fs_info;
5596 struct btrfs_extent_item *extent_item;
5597 struct btrfs_tree_block_info *block_info;
5598 struct btrfs_extent_inline_ref *iref;
5599 struct btrfs_path *path;
5600 struct extent_buffer *leaf;
5601 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5603 path = btrfs_alloc_path();
5606 path->leave_spinning = 1;
5607 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5611 leaf = path->nodes[0];
5612 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5613 struct btrfs_extent_item);
5614 btrfs_set_extent_refs(leaf, extent_item, 1);
5615 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5616 btrfs_set_extent_flags(leaf, extent_item,
5617 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5618 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5620 btrfs_set_tree_block_key(leaf, block_info, key);
5621 btrfs_set_tree_block_level(leaf, block_info, level);
5623 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5625 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5626 btrfs_set_extent_inline_ref_type(leaf, iref,
5627 BTRFS_SHARED_BLOCK_REF_KEY);
5628 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5630 btrfs_set_extent_inline_ref_type(leaf, iref,
5631 BTRFS_TREE_BLOCK_REF_KEY);
5632 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5635 btrfs_mark_buffer_dirty(leaf);
5636 btrfs_free_path(path);
5638 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5640 printk(KERN_ERR "btrfs update block group failed for %llu "
5641 "%llu\n", (unsigned long long)ins->objectid,
5642 (unsigned long long)ins->offset);
5648 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5649 struct btrfs_root *root,
5650 u64 root_objectid, u64 owner,
5651 u64 offset, struct btrfs_key *ins)
5655 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5657 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5658 0, root_objectid, owner, offset,
5659 BTRFS_ADD_DELAYED_EXTENT, NULL);
5664 * this is used by the tree logging recovery code. It records that
5665 * an extent has been allocated and makes sure to clear the free
5666 * space cache bits as well
5668 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5669 struct btrfs_root *root,
5670 u64 root_objectid, u64 owner, u64 offset,
5671 struct btrfs_key *ins)
5674 struct btrfs_block_group_cache *block_group;
5675 struct btrfs_caching_control *caching_ctl;
5676 u64 start = ins->objectid;
5677 u64 num_bytes = ins->offset;
5679 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5680 cache_block_group(block_group, trans, NULL, 0);
5681 caching_ctl = get_caching_control(block_group);
5684 BUG_ON(!block_group_cache_done(block_group));
5685 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5688 mutex_lock(&caching_ctl->mutex);
5690 if (start >= caching_ctl->progress) {
5691 ret = add_excluded_extent(root, start, num_bytes);
5693 } else if (start + num_bytes <= caching_ctl->progress) {
5694 ret = btrfs_remove_free_space(block_group,
5698 num_bytes = caching_ctl->progress - start;
5699 ret = btrfs_remove_free_space(block_group,
5703 start = caching_ctl->progress;
5704 num_bytes = ins->objectid + ins->offset -
5705 caching_ctl->progress;
5706 ret = add_excluded_extent(root, start, num_bytes);
5710 mutex_unlock(&caching_ctl->mutex);
5711 put_caching_control(caching_ctl);
5714 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
5716 btrfs_put_block_group(block_group);
5717 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5718 0, owner, offset, ins, 1);
5722 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5723 struct btrfs_root *root,
5724 u64 bytenr, u32 blocksize,
5727 struct extent_buffer *buf;
5729 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5731 return ERR_PTR(-ENOMEM);
5732 btrfs_set_header_generation(buf, trans->transid);
5733 btrfs_set_buffer_lockdep_class(buf, level);
5734 btrfs_tree_lock(buf);
5735 clean_tree_block(trans, root, buf);
5737 btrfs_set_lock_blocking(buf);
5738 btrfs_set_buffer_uptodate(buf);
5740 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5742 * we allow two log transactions at a time, use different
5743 * EXENT bit to differentiate dirty pages.
5745 if (root->log_transid % 2 == 0)
5746 set_extent_dirty(&root->dirty_log_pages, buf->start,
5747 buf->start + buf->len - 1, GFP_NOFS);
5749 set_extent_new(&root->dirty_log_pages, buf->start,
5750 buf->start + buf->len - 1, GFP_NOFS);
5752 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5753 buf->start + buf->len - 1, GFP_NOFS);
5755 trans->blocks_used++;
5756 /* this returns a buffer locked for blocking */
5760 static struct btrfs_block_rsv *
5761 use_block_rsv(struct btrfs_trans_handle *trans,
5762 struct btrfs_root *root, u32 blocksize)
5764 struct btrfs_block_rsv *block_rsv;
5765 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
5768 block_rsv = get_block_rsv(trans, root);
5770 if (block_rsv->size == 0) {
5771 ret = reserve_metadata_bytes(trans, root, block_rsv,
5774 * If we couldn't reserve metadata bytes try and use some from
5775 * the global reserve.
5777 if (ret && block_rsv != global_rsv) {
5778 ret = block_rsv_use_bytes(global_rsv, blocksize);
5781 return ERR_PTR(ret);
5783 return ERR_PTR(ret);
5788 ret = block_rsv_use_bytes(block_rsv, blocksize);
5793 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5796 spin_lock(&block_rsv->lock);
5797 block_rsv->size += blocksize;
5798 spin_unlock(&block_rsv->lock);
5800 } else if (ret && block_rsv != global_rsv) {
5801 ret = block_rsv_use_bytes(global_rsv, blocksize);
5807 return ERR_PTR(-ENOSPC);
5810 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5812 block_rsv_add_bytes(block_rsv, blocksize, 0);
5813 block_rsv_release_bytes(block_rsv, NULL, 0);
5817 * finds a free extent and does all the dirty work required for allocation
5818 * returns the key for the extent through ins, and a tree buffer for
5819 * the first block of the extent through buf.
5821 * returns the tree buffer or NULL.
5823 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5824 struct btrfs_root *root, u32 blocksize,
5825 u64 parent, u64 root_objectid,
5826 struct btrfs_disk_key *key, int level,
5827 u64 hint, u64 empty_size)
5829 struct btrfs_key ins;
5830 struct btrfs_block_rsv *block_rsv;
5831 struct extent_buffer *buf;
5836 block_rsv = use_block_rsv(trans, root, blocksize);
5837 if (IS_ERR(block_rsv))
5838 return ERR_CAST(block_rsv);
5840 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5841 empty_size, hint, (u64)-1, &ins, 0);
5843 unuse_block_rsv(block_rsv, blocksize);
5844 return ERR_PTR(ret);
5847 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5849 BUG_ON(IS_ERR(buf));
5851 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5853 parent = ins.objectid;
5854 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5858 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5859 struct btrfs_delayed_extent_op *extent_op;
5860 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5863 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5865 memset(&extent_op->key, 0, sizeof(extent_op->key));
5866 extent_op->flags_to_set = flags;
5867 extent_op->update_key = 1;
5868 extent_op->update_flags = 1;
5869 extent_op->is_data = 0;
5871 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5872 ins.offset, parent, root_objectid,
5873 level, BTRFS_ADD_DELAYED_EXTENT,
5880 struct walk_control {
5881 u64 refs[BTRFS_MAX_LEVEL];
5882 u64 flags[BTRFS_MAX_LEVEL];
5883 struct btrfs_key update_progress;
5893 #define DROP_REFERENCE 1
5894 #define UPDATE_BACKREF 2
5896 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5897 struct btrfs_root *root,
5898 struct walk_control *wc,
5899 struct btrfs_path *path)
5907 struct btrfs_key key;
5908 struct extent_buffer *eb;
5913 if (path->slots[wc->level] < wc->reada_slot) {
5914 wc->reada_count = wc->reada_count * 2 / 3;
5915 wc->reada_count = max(wc->reada_count, 2);
5917 wc->reada_count = wc->reada_count * 3 / 2;
5918 wc->reada_count = min_t(int, wc->reada_count,
5919 BTRFS_NODEPTRS_PER_BLOCK(root));
5922 eb = path->nodes[wc->level];
5923 nritems = btrfs_header_nritems(eb);
5924 blocksize = btrfs_level_size(root, wc->level - 1);
5926 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5927 if (nread >= wc->reada_count)
5931 bytenr = btrfs_node_blockptr(eb, slot);
5932 generation = btrfs_node_ptr_generation(eb, slot);
5934 if (slot == path->slots[wc->level])
5937 if (wc->stage == UPDATE_BACKREF &&
5938 generation <= root->root_key.offset)
5941 /* We don't lock the tree block, it's OK to be racy here */
5942 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5947 if (wc->stage == DROP_REFERENCE) {
5951 if (wc->level == 1 &&
5952 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5954 if (!wc->update_ref ||
5955 generation <= root->root_key.offset)
5957 btrfs_node_key_to_cpu(eb, &key, slot);
5958 ret = btrfs_comp_cpu_keys(&key,
5959 &wc->update_progress);
5963 if (wc->level == 1 &&
5964 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5968 ret = readahead_tree_block(root, bytenr, blocksize,
5974 wc->reada_slot = slot;
5978 * hepler to process tree block while walking down the tree.
5980 * when wc->stage == UPDATE_BACKREF, this function updates
5981 * back refs for pointers in the block.
5983 * NOTE: return value 1 means we should stop walking down.
5985 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5986 struct btrfs_root *root,
5987 struct btrfs_path *path,
5988 struct walk_control *wc, int lookup_info)
5990 int level = wc->level;
5991 struct extent_buffer *eb = path->nodes[level];
5992 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5995 if (wc->stage == UPDATE_BACKREF &&
5996 btrfs_header_owner(eb) != root->root_key.objectid)
6000 * when reference count of tree block is 1, it won't increase
6001 * again. once full backref flag is set, we never clear it.
6004 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6005 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
6006 BUG_ON(!path->locks[level]);
6007 ret = btrfs_lookup_extent_info(trans, root,
6012 BUG_ON(wc->refs[level] == 0);
6015 if (wc->stage == DROP_REFERENCE) {
6016 if (wc->refs[level] > 1)
6019 if (path->locks[level] && !wc->keep_locks) {
6020 btrfs_tree_unlock(eb);
6021 path->locks[level] = 0;
6026 /* wc->stage == UPDATE_BACKREF */
6027 if (!(wc->flags[level] & flag)) {
6028 BUG_ON(!path->locks[level]);
6029 ret = btrfs_inc_ref(trans, root, eb, 1);
6031 ret = btrfs_dec_ref(trans, root, eb, 0);
6033 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6036 wc->flags[level] |= flag;
6040 * the block is shared by multiple trees, so it's not good to
6041 * keep the tree lock
6043 if (path->locks[level] && level > 0) {
6044 btrfs_tree_unlock(eb);
6045 path->locks[level] = 0;
6051 * hepler to process tree block pointer.
6053 * when wc->stage == DROP_REFERENCE, this function checks
6054 * reference count of the block pointed to. if the block
6055 * is shared and we need update back refs for the subtree
6056 * rooted at the block, this function changes wc->stage to
6057 * UPDATE_BACKREF. if the block is shared and there is no
6058 * need to update back, this function drops the reference
6061 * NOTE: return value 1 means we should stop walking down.
6063 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6064 struct btrfs_root *root,
6065 struct btrfs_path *path,
6066 struct walk_control *wc, int *lookup_info)
6072 struct btrfs_key key;
6073 struct extent_buffer *next;
6074 int level = wc->level;
6078 generation = btrfs_node_ptr_generation(path->nodes[level],
6079 path->slots[level]);
6081 * if the lower level block was created before the snapshot
6082 * was created, we know there is no need to update back refs
6085 if (wc->stage == UPDATE_BACKREF &&
6086 generation <= root->root_key.offset) {
6091 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6092 blocksize = btrfs_level_size(root, level - 1);
6094 next = btrfs_find_tree_block(root, bytenr, blocksize);
6096 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
6101 btrfs_tree_lock(next);
6102 btrfs_set_lock_blocking(next);
6104 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6105 &wc->refs[level - 1],
6106 &wc->flags[level - 1]);
6108 BUG_ON(wc->refs[level - 1] == 0);
6111 if (wc->stage == DROP_REFERENCE) {
6112 if (wc->refs[level - 1] > 1) {
6114 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6117 if (!wc->update_ref ||
6118 generation <= root->root_key.offset)
6121 btrfs_node_key_to_cpu(path->nodes[level], &key,
6122 path->slots[level]);
6123 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6127 wc->stage = UPDATE_BACKREF;
6128 wc->shared_level = level - 1;
6132 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6136 if (!btrfs_buffer_uptodate(next, generation)) {
6137 btrfs_tree_unlock(next);
6138 free_extent_buffer(next);
6144 if (reada && level == 1)
6145 reada_walk_down(trans, root, wc, path);
6146 next = read_tree_block(root, bytenr, blocksize, generation);
6149 btrfs_tree_lock(next);
6150 btrfs_set_lock_blocking(next);
6154 BUG_ON(level != btrfs_header_level(next));
6155 path->nodes[level] = next;
6156 path->slots[level] = 0;
6157 path->locks[level] = 1;
6163 wc->refs[level - 1] = 0;
6164 wc->flags[level - 1] = 0;
6165 if (wc->stage == DROP_REFERENCE) {
6166 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6167 parent = path->nodes[level]->start;
6169 BUG_ON(root->root_key.objectid !=
6170 btrfs_header_owner(path->nodes[level]));
6174 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6175 root->root_key.objectid, level - 1, 0);
6178 btrfs_tree_unlock(next);
6179 free_extent_buffer(next);
6185 * hepler to process tree block while walking up the tree.
6187 * when wc->stage == DROP_REFERENCE, this function drops
6188 * reference count on the block.
6190 * when wc->stage == UPDATE_BACKREF, this function changes
6191 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6192 * to UPDATE_BACKREF previously while processing the block.
6194 * NOTE: return value 1 means we should stop walking up.
6196 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6197 struct btrfs_root *root,
6198 struct btrfs_path *path,
6199 struct walk_control *wc)
6202 int level = wc->level;
6203 struct extent_buffer *eb = path->nodes[level];
6206 if (wc->stage == UPDATE_BACKREF) {
6207 BUG_ON(wc->shared_level < level);
6208 if (level < wc->shared_level)
6211 ret = find_next_key(path, level + 1, &wc->update_progress);
6215 wc->stage = DROP_REFERENCE;
6216 wc->shared_level = -1;
6217 path->slots[level] = 0;
6220 * check reference count again if the block isn't locked.
6221 * we should start walking down the tree again if reference
6224 if (!path->locks[level]) {
6226 btrfs_tree_lock(eb);
6227 btrfs_set_lock_blocking(eb);
6228 path->locks[level] = 1;
6230 ret = btrfs_lookup_extent_info(trans, root,
6235 BUG_ON(wc->refs[level] == 0);
6236 if (wc->refs[level] == 1) {
6237 btrfs_tree_unlock(eb);
6238 path->locks[level] = 0;
6244 /* wc->stage == DROP_REFERENCE */
6245 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6247 if (wc->refs[level] == 1) {
6249 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6250 ret = btrfs_dec_ref(trans, root, eb, 1);
6252 ret = btrfs_dec_ref(trans, root, eb, 0);
6255 /* make block locked assertion in clean_tree_block happy */
6256 if (!path->locks[level] &&
6257 btrfs_header_generation(eb) == trans->transid) {
6258 btrfs_tree_lock(eb);
6259 btrfs_set_lock_blocking(eb);
6260 path->locks[level] = 1;
6262 clean_tree_block(trans, root, eb);
6265 if (eb == root->node) {
6266 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6269 BUG_ON(root->root_key.objectid !=
6270 btrfs_header_owner(eb));
6272 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6273 parent = path->nodes[level + 1]->start;
6275 BUG_ON(root->root_key.objectid !=
6276 btrfs_header_owner(path->nodes[level + 1]));
6279 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
6281 wc->refs[level] = 0;
6282 wc->flags[level] = 0;
6286 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6287 struct btrfs_root *root,
6288 struct btrfs_path *path,
6289 struct walk_control *wc)
6291 int level = wc->level;
6292 int lookup_info = 1;
6295 while (level >= 0) {
6296 ret = walk_down_proc(trans, root, path, wc, lookup_info);
6303 if (path->slots[level] >=
6304 btrfs_header_nritems(path->nodes[level]))
6307 ret = do_walk_down(trans, root, path, wc, &lookup_info);
6309 path->slots[level]++;
6318 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6319 struct btrfs_root *root,
6320 struct btrfs_path *path,
6321 struct walk_control *wc, int max_level)
6323 int level = wc->level;
6326 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6327 while (level < max_level && path->nodes[level]) {
6329 if (path->slots[level] + 1 <
6330 btrfs_header_nritems(path->nodes[level])) {
6331 path->slots[level]++;
6334 ret = walk_up_proc(trans, root, path, wc);
6338 if (path->locks[level]) {
6339 btrfs_tree_unlock(path->nodes[level]);
6340 path->locks[level] = 0;
6342 free_extent_buffer(path->nodes[level]);
6343 path->nodes[level] = NULL;
6351 * drop a subvolume tree.
6353 * this function traverses the tree freeing any blocks that only
6354 * referenced by the tree.
6356 * when a shared tree block is found. this function decreases its
6357 * reference count by one. if update_ref is true, this function
6358 * also make sure backrefs for the shared block and all lower level
6359 * blocks are properly updated.
6361 int btrfs_drop_snapshot(struct btrfs_root *root,
6362 struct btrfs_block_rsv *block_rsv, int update_ref)
6364 struct btrfs_path *path;
6365 struct btrfs_trans_handle *trans;
6366 struct btrfs_root *tree_root = root->fs_info->tree_root;
6367 struct btrfs_root_item *root_item = &root->root_item;
6368 struct walk_control *wc;
6369 struct btrfs_key key;
6374 path = btrfs_alloc_path();
6377 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6380 trans = btrfs_start_transaction(tree_root, 0);
6381 BUG_ON(IS_ERR(trans));
6384 trans->block_rsv = block_rsv;
6386 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
6387 level = btrfs_header_level(root->node);
6388 path->nodes[level] = btrfs_lock_root_node(root);
6389 btrfs_set_lock_blocking(path->nodes[level]);
6390 path->slots[level] = 0;
6391 path->locks[level] = 1;
6392 memset(&wc->update_progress, 0,
6393 sizeof(wc->update_progress));
6395 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6396 memcpy(&wc->update_progress, &key,
6397 sizeof(wc->update_progress));
6399 level = root_item->drop_level;
6401 path->lowest_level = level;
6402 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6403 path->lowest_level = 0;
6411 * unlock our path, this is safe because only this
6412 * function is allowed to delete this snapshot
6414 btrfs_unlock_up_safe(path, 0);
6416 level = btrfs_header_level(root->node);
6418 btrfs_tree_lock(path->nodes[level]);
6419 btrfs_set_lock_blocking(path->nodes[level]);
6421 ret = btrfs_lookup_extent_info(trans, root,
6422 path->nodes[level]->start,
6423 path->nodes[level]->len,
6427 BUG_ON(wc->refs[level] == 0);
6429 if (level == root_item->drop_level)
6432 btrfs_tree_unlock(path->nodes[level]);
6433 WARN_ON(wc->refs[level] != 1);
6439 wc->shared_level = -1;
6440 wc->stage = DROP_REFERENCE;
6441 wc->update_ref = update_ref;
6443 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6446 ret = walk_down_tree(trans, root, path, wc);
6452 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6459 BUG_ON(wc->stage != DROP_REFERENCE);
6463 if (wc->stage == DROP_REFERENCE) {
6465 btrfs_node_key(path->nodes[level],
6466 &root_item->drop_progress,
6467 path->slots[level]);
6468 root_item->drop_level = level;
6471 BUG_ON(wc->level == 0);
6472 if (btrfs_should_end_transaction(trans, tree_root)) {
6473 ret = btrfs_update_root(trans, tree_root,
6478 btrfs_end_transaction_throttle(trans, tree_root);
6479 trans = btrfs_start_transaction(tree_root, 0);
6480 BUG_ON(IS_ERR(trans));
6482 trans->block_rsv = block_rsv;
6485 btrfs_release_path(root, path);
6488 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6491 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6492 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6496 /* if we fail to delete the orphan item this time
6497 * around, it'll get picked up the next time.
6499 * The most common failure here is just -ENOENT.
6501 btrfs_del_orphan_item(trans, tree_root,
6502 root->root_key.objectid);
6506 if (root->in_radix) {
6507 btrfs_free_fs_root(tree_root->fs_info, root);
6509 free_extent_buffer(root->node);
6510 free_extent_buffer(root->commit_root);
6514 btrfs_end_transaction_throttle(trans, tree_root);
6516 btrfs_free_path(path);
6521 * drop subtree rooted at tree block 'node'.
6523 * NOTE: this function will unlock and release tree block 'node'
6525 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6526 struct btrfs_root *root,
6527 struct extent_buffer *node,
6528 struct extent_buffer *parent)
6530 struct btrfs_path *path;
6531 struct walk_control *wc;
6537 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6539 path = btrfs_alloc_path();
6543 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6545 btrfs_free_path(path);
6549 btrfs_assert_tree_locked(parent);
6550 parent_level = btrfs_header_level(parent);
6551 extent_buffer_get(parent);
6552 path->nodes[parent_level] = parent;
6553 path->slots[parent_level] = btrfs_header_nritems(parent);
6555 btrfs_assert_tree_locked(node);
6556 level = btrfs_header_level(node);
6557 path->nodes[level] = node;
6558 path->slots[level] = 0;
6559 path->locks[level] = 1;
6561 wc->refs[parent_level] = 1;
6562 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6564 wc->shared_level = -1;
6565 wc->stage = DROP_REFERENCE;
6568 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6571 wret = walk_down_tree(trans, root, path, wc);
6577 wret = walk_up_tree(trans, root, path, wc, parent_level);
6585 btrfs_free_path(path);
6590 static unsigned long calc_ra(unsigned long start, unsigned long last,
6593 return min(last, start + nr - 1);
6596 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
6601 unsigned long first_index;
6602 unsigned long last_index;
6605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6606 struct file_ra_state *ra;
6607 struct btrfs_ordered_extent *ordered;
6608 unsigned int total_read = 0;
6609 unsigned int total_dirty = 0;
6612 ra = kzalloc(sizeof(*ra), GFP_NOFS);
6616 mutex_lock(&inode->i_mutex);
6617 first_index = start >> PAGE_CACHE_SHIFT;
6618 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6620 /* make sure the dirty trick played by the caller work */
6621 ret = invalidate_inode_pages2_range(inode->i_mapping,
6622 first_index, last_index);
6626 file_ra_state_init(ra, inode->i_mapping);
6628 for (i = first_index ; i <= last_index; i++) {
6629 if (total_read % ra->ra_pages == 0) {
6630 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
6631 calc_ra(i, last_index, ra->ra_pages));
6635 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
6637 page = grab_cache_page(inode->i_mapping, i);
6642 if (!PageUptodate(page)) {
6643 btrfs_readpage(NULL, page);
6645 if (!PageUptodate(page)) {
6647 page_cache_release(page);
6652 wait_on_page_writeback(page);
6654 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6655 page_end = page_start + PAGE_CACHE_SIZE - 1;
6656 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
6658 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6660 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6662 page_cache_release(page);
6663 btrfs_start_ordered_extent(inode, ordered, 1);
6664 btrfs_put_ordered_extent(ordered);
6667 set_page_extent_mapped(page);
6669 if (i == first_index)
6670 set_extent_bits(io_tree, page_start, page_end,
6671 EXTENT_BOUNDARY, GFP_NOFS);
6672 btrfs_set_extent_delalloc(inode, page_start, page_end);
6674 set_page_dirty(page);
6677 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6679 page_cache_release(page);
6684 mutex_unlock(&inode->i_mutex);
6685 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
6689 static noinline int relocate_data_extent(struct inode *reloc_inode,
6690 struct btrfs_key *extent_key,
6693 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6694 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6695 struct extent_map *em;
6696 u64 start = extent_key->objectid - offset;
6697 u64 end = start + extent_key->offset - 1;
6699 em = alloc_extent_map(GFP_NOFS);
6703 em->len = extent_key->offset;
6704 em->block_len = extent_key->offset;
6705 em->block_start = extent_key->objectid;
6706 em->bdev = root->fs_info->fs_devices->latest_bdev;
6707 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6709 /* setup extent map to cheat btrfs_readpage */
6710 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6713 write_lock(&em_tree->lock);
6714 ret = add_extent_mapping(em_tree, em);
6715 write_unlock(&em_tree->lock);
6716 if (ret != -EEXIST) {
6717 free_extent_map(em);
6720 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
6722 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6724 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
6727 struct btrfs_ref_path {
6729 u64 nodes[BTRFS_MAX_LEVEL];
6731 u64 root_generation;
6738 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6739 u64 new_nodes[BTRFS_MAX_LEVEL];
6742 struct disk_extent {
6753 static int is_cowonly_root(u64 root_objectid)
6755 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6756 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6757 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6758 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
6759 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6760 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
6765 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
6766 struct btrfs_root *extent_root,
6767 struct btrfs_ref_path *ref_path,
6770 struct extent_buffer *leaf;
6771 struct btrfs_path *path;
6772 struct btrfs_extent_ref *ref;
6773 struct btrfs_key key;
6774 struct btrfs_key found_key;
6780 path = btrfs_alloc_path();
6785 ref_path->lowest_level = -1;
6786 ref_path->current_level = -1;
6787 ref_path->shared_level = -1;
6791 level = ref_path->current_level - 1;
6792 while (level >= -1) {
6794 if (level < ref_path->lowest_level)
6798 bytenr = ref_path->nodes[level];
6800 bytenr = ref_path->extent_start;
6801 BUG_ON(bytenr == 0);
6803 parent = ref_path->nodes[level + 1];
6804 ref_path->nodes[level + 1] = 0;
6805 ref_path->current_level = level;
6806 BUG_ON(parent == 0);
6808 key.objectid = bytenr;
6809 key.offset = parent + 1;
6810 key.type = BTRFS_EXTENT_REF_KEY;
6812 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6817 leaf = path->nodes[0];
6818 nritems = btrfs_header_nritems(leaf);
6819 if (path->slots[0] >= nritems) {
6820 ret = btrfs_next_leaf(extent_root, path);
6825 leaf = path->nodes[0];
6828 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6829 if (found_key.objectid == bytenr &&
6830 found_key.type == BTRFS_EXTENT_REF_KEY) {
6831 if (level < ref_path->shared_level)
6832 ref_path->shared_level = level;
6837 btrfs_release_path(extent_root, path);
6840 /* reached lowest level */
6844 level = ref_path->current_level;
6845 while (level < BTRFS_MAX_LEVEL - 1) {
6849 bytenr = ref_path->nodes[level];
6851 bytenr = ref_path->extent_start;
6853 BUG_ON(bytenr == 0);
6855 key.objectid = bytenr;
6857 key.type = BTRFS_EXTENT_REF_KEY;
6859 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6863 leaf = path->nodes[0];
6864 nritems = btrfs_header_nritems(leaf);
6865 if (path->slots[0] >= nritems) {
6866 ret = btrfs_next_leaf(extent_root, path);
6870 /* the extent was freed by someone */
6871 if (ref_path->lowest_level == level)
6873 btrfs_release_path(extent_root, path);
6876 leaf = path->nodes[0];
6879 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6880 if (found_key.objectid != bytenr ||
6881 found_key.type != BTRFS_EXTENT_REF_KEY) {
6882 /* the extent was freed by someone */
6883 if (ref_path->lowest_level == level) {
6887 btrfs_release_path(extent_root, path);
6891 ref = btrfs_item_ptr(leaf, path->slots[0],
6892 struct btrfs_extent_ref);
6893 ref_objectid = btrfs_ref_objectid(leaf, ref);
6894 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6896 level = (int)ref_objectid;
6897 BUG_ON(level >= BTRFS_MAX_LEVEL);
6898 ref_path->lowest_level = level;
6899 ref_path->current_level = level;
6900 ref_path->nodes[level] = bytenr;
6902 WARN_ON(ref_objectid != level);
6905 WARN_ON(level != -1);
6909 if (ref_path->lowest_level == level) {
6910 ref_path->owner_objectid = ref_objectid;
6911 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6915 * the block is tree root or the block isn't in reference
6918 if (found_key.objectid == found_key.offset ||
6919 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6920 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6921 ref_path->root_generation =
6922 btrfs_ref_generation(leaf, ref);
6924 /* special reference from the tree log */
6925 ref_path->nodes[0] = found_key.offset;
6926 ref_path->current_level = 0;
6933 BUG_ON(ref_path->nodes[level] != 0);
6934 ref_path->nodes[level] = found_key.offset;
6935 ref_path->current_level = level;
6938 * the reference was created in the running transaction,
6939 * no need to continue walking up.
6941 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6942 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6943 ref_path->root_generation =
6944 btrfs_ref_generation(leaf, ref);
6949 btrfs_release_path(extent_root, path);
6952 /* reached max tree level, but no tree root found. */
6955 btrfs_free_path(path);
6959 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6960 struct btrfs_root *extent_root,
6961 struct btrfs_ref_path *ref_path,
6964 memset(ref_path, 0, sizeof(*ref_path));
6965 ref_path->extent_start = extent_start;
6967 return __next_ref_path(trans, extent_root, ref_path, 1);
6970 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6971 struct btrfs_root *extent_root,
6972 struct btrfs_ref_path *ref_path)
6974 return __next_ref_path(trans, extent_root, ref_path, 0);
6977 static noinline int get_new_locations(struct inode *reloc_inode,
6978 struct btrfs_key *extent_key,
6979 u64 offset, int no_fragment,
6980 struct disk_extent **extents,
6983 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6984 struct btrfs_path *path;
6985 struct btrfs_file_extent_item *fi;
6986 struct extent_buffer *leaf;
6987 struct disk_extent *exts = *extents;
6988 struct btrfs_key found_key;
6993 int max = *nr_extents;
6996 WARN_ON(!no_fragment && *extents);
6999 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7004 path = btrfs_alloc_path();
7006 if (exts != *extents)
7011 cur_pos = extent_key->objectid - offset;
7012 last_byte = extent_key->objectid + extent_key->offset;
7013 ret = btrfs_lookup_file_extent(NULL, root, path,
7014 btrfs_ino(reloc_inode), cur_pos, 0);
7023 leaf = path->nodes[0];
7024 nritems = btrfs_header_nritems(leaf);
7025 if (path->slots[0] >= nritems) {
7026 ret = btrfs_next_leaf(root, path);
7031 leaf = path->nodes[0];
7034 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7035 if (found_key.offset != cur_pos ||
7036 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7037 found_key.objectid != btrfs_ino(reloc_inode))
7040 fi = btrfs_item_ptr(leaf, path->slots[0],
7041 struct btrfs_file_extent_item);
7042 if (btrfs_file_extent_type(leaf, fi) !=
7043 BTRFS_FILE_EXTENT_REG ||
7044 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7048 struct disk_extent *old = exts;
7050 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
7055 memcpy(exts, old, sizeof(*exts) * nr);
7056 if (old != *extents)
7060 exts[nr].disk_bytenr =
7061 btrfs_file_extent_disk_bytenr(leaf, fi);
7062 exts[nr].disk_num_bytes =
7063 btrfs_file_extent_disk_num_bytes(leaf, fi);
7064 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7065 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7066 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7067 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7068 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7069 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7071 BUG_ON(exts[nr].offset > 0);
7072 BUG_ON(exts[nr].compression || exts[nr].encryption);
7073 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
7075 cur_pos += exts[nr].num_bytes;
7078 if (cur_pos + offset >= last_byte)
7088 BUG_ON(cur_pos + offset > last_byte);
7089 if (cur_pos + offset < last_byte) {
7095 btrfs_free_path(path);
7097 if (exts != *extents)
7106 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
7107 struct btrfs_root *root,
7108 struct btrfs_path *path,
7109 struct btrfs_key *extent_key,
7110 struct btrfs_key *leaf_key,
7111 struct btrfs_ref_path *ref_path,
7112 struct disk_extent *new_extents,
7115 struct extent_buffer *leaf;
7116 struct btrfs_file_extent_item *fi;
7117 struct inode *inode = NULL;
7118 struct btrfs_key key;
7123 u64 search_end = (u64)-1;
7126 int extent_locked = 0;
7130 memcpy(&key, leaf_key, sizeof(key));
7131 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7132 if (key.objectid < ref_path->owner_objectid ||
7133 (key.objectid == ref_path->owner_objectid &&
7134 key.type < BTRFS_EXTENT_DATA_KEY)) {
7135 key.objectid = ref_path->owner_objectid;
7136 key.type = BTRFS_EXTENT_DATA_KEY;
7142 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7146 leaf = path->nodes[0];
7147 nritems = btrfs_header_nritems(leaf);
7149 if (extent_locked && ret > 0) {
7151 * the file extent item was modified by someone
7152 * before the extent got locked.
7154 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7155 lock_end, GFP_NOFS);
7159 if (path->slots[0] >= nritems) {
7160 if (++nr_scaned > 2)
7163 BUG_ON(extent_locked);
7164 ret = btrfs_next_leaf(root, path);
7169 leaf = path->nodes[0];
7170 nritems = btrfs_header_nritems(leaf);
7173 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7175 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7176 if ((key.objectid > ref_path->owner_objectid) ||
7177 (key.objectid == ref_path->owner_objectid &&
7178 key.type > BTRFS_EXTENT_DATA_KEY) ||
7179 key.offset >= search_end)
7183 if (inode && key.objectid != btrfs_ino(inode)) {
7184 BUG_ON(extent_locked);
7185 btrfs_release_path(root, path);
7186 mutex_unlock(&inode->i_mutex);
7192 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7197 fi = btrfs_item_ptr(leaf, path->slots[0],
7198 struct btrfs_file_extent_item);
7199 extent_type = btrfs_file_extent_type(leaf, fi);
7200 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7201 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
7202 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7203 extent_key->objectid)) {
7209 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7210 ext_offset = btrfs_file_extent_offset(leaf, fi);
7212 if (search_end == (u64)-1) {
7213 search_end = key.offset - ext_offset +
7214 btrfs_file_extent_ram_bytes(leaf, fi);
7217 if (!extent_locked) {
7218 lock_start = key.offset;
7219 lock_end = lock_start + num_bytes - 1;
7221 if (lock_start > key.offset ||
7222 lock_end + 1 < key.offset + num_bytes) {
7223 unlock_extent(&BTRFS_I(inode)->io_tree,
7224 lock_start, lock_end, GFP_NOFS);
7230 btrfs_release_path(root, path);
7232 inode = btrfs_iget_locked(root->fs_info->sb,
7233 key.objectid, root);
7234 if (inode->i_state & I_NEW) {
7235 BTRFS_I(inode)->root = root;
7236 BTRFS_I(inode)->location.objectid =
7238 BTRFS_I(inode)->location.type =
7239 BTRFS_INODE_ITEM_KEY;
7240 BTRFS_I(inode)->location.offset = 0;
7241 btrfs_read_locked_inode(inode);
7242 unlock_new_inode(inode);
7245 * some code call btrfs_commit_transaction while
7246 * holding the i_mutex, so we can't use mutex_lock
7249 if (is_bad_inode(inode) ||
7250 !mutex_trylock(&inode->i_mutex)) {
7253 key.offset = (u64)-1;
7258 if (!extent_locked) {
7259 struct btrfs_ordered_extent *ordered;
7261 btrfs_release_path(root, path);
7263 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7264 lock_end, GFP_NOFS);
7265 ordered = btrfs_lookup_first_ordered_extent(inode,
7268 ordered->file_offset <= lock_end &&
7269 ordered->file_offset + ordered->len > lock_start) {
7270 unlock_extent(&BTRFS_I(inode)->io_tree,
7271 lock_start, lock_end, GFP_NOFS);
7272 btrfs_start_ordered_extent(inode, ordered, 1);
7273 btrfs_put_ordered_extent(ordered);
7274 key.offset += num_bytes;
7278 btrfs_put_ordered_extent(ordered);
7284 if (nr_extents == 1) {
7285 /* update extent pointer in place */
7286 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7287 new_extents[0].disk_bytenr);
7288 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7289 new_extents[0].disk_num_bytes);
7290 btrfs_mark_buffer_dirty(leaf);
7292 btrfs_drop_extent_cache(inode, key.offset,
7293 key.offset + num_bytes - 1, 0);
7295 ret = btrfs_inc_extent_ref(trans, root,
7296 new_extents[0].disk_bytenr,
7297 new_extents[0].disk_num_bytes,
7299 root->root_key.objectid,
7304 ret = btrfs_free_extent(trans, root,
7305 extent_key->objectid,
7308 btrfs_header_owner(leaf),
7309 btrfs_header_generation(leaf),
7313 btrfs_release_path(root, path);
7314 key.offset += num_bytes;
7322 * drop old extent pointer at first, then insert the
7323 * new pointers one bye one
7325 btrfs_release_path(root, path);
7326 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7327 key.offset + num_bytes,
7328 key.offset, &alloc_hint);
7331 for (i = 0; i < nr_extents; i++) {
7332 if (ext_offset >= new_extents[i].num_bytes) {
7333 ext_offset -= new_extents[i].num_bytes;
7336 extent_len = min(new_extents[i].num_bytes -
7337 ext_offset, num_bytes);
7339 ret = btrfs_insert_empty_item(trans, root,
7344 leaf = path->nodes[0];
7345 fi = btrfs_item_ptr(leaf, path->slots[0],
7346 struct btrfs_file_extent_item);
7347 btrfs_set_file_extent_generation(leaf, fi,
7349 btrfs_set_file_extent_type(leaf, fi,
7350 BTRFS_FILE_EXTENT_REG);
7351 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7352 new_extents[i].disk_bytenr);
7353 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7354 new_extents[i].disk_num_bytes);
7355 btrfs_set_file_extent_ram_bytes(leaf, fi,
7356 new_extents[i].ram_bytes);
7358 btrfs_set_file_extent_compression(leaf, fi,
7359 new_extents[i].compression);
7360 btrfs_set_file_extent_encryption(leaf, fi,
7361 new_extents[i].encryption);
7362 btrfs_set_file_extent_other_encoding(leaf, fi,
7363 new_extents[i].other_encoding);
7365 btrfs_set_file_extent_num_bytes(leaf, fi,
7367 ext_offset += new_extents[i].offset;
7368 btrfs_set_file_extent_offset(leaf, fi,
7370 btrfs_mark_buffer_dirty(leaf);
7372 btrfs_drop_extent_cache(inode, key.offset,
7373 key.offset + extent_len - 1, 0);
7375 ret = btrfs_inc_extent_ref(trans, root,
7376 new_extents[i].disk_bytenr,
7377 new_extents[i].disk_num_bytes,
7379 root->root_key.objectid,
7380 trans->transid, key.objectid);
7382 btrfs_release_path(root, path);
7384 inode_add_bytes(inode, extent_len);
7387 num_bytes -= extent_len;
7388 key.offset += extent_len;
7393 BUG_ON(i >= nr_extents);
7397 if (extent_locked) {
7398 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7399 lock_end, GFP_NOFS);
7403 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
7404 key.offset >= search_end)
7411 btrfs_release_path(root, path);
7413 mutex_unlock(&inode->i_mutex);
7414 if (extent_locked) {
7415 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7416 lock_end, GFP_NOFS);
7423 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7424 struct btrfs_root *root,
7425 struct extent_buffer *buf, u64 orig_start)
7430 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7431 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7433 level = btrfs_header_level(buf);
7435 struct btrfs_leaf_ref *ref;
7436 struct btrfs_leaf_ref *orig_ref;
7438 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7442 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7444 btrfs_free_leaf_ref(root, orig_ref);
7448 ref->nritems = orig_ref->nritems;
7449 memcpy(ref->extents, orig_ref->extents,
7450 sizeof(ref->extents[0]) * ref->nritems);
7452 btrfs_free_leaf_ref(root, orig_ref);
7454 ref->root_gen = trans->transid;
7455 ref->bytenr = buf->start;
7456 ref->owner = btrfs_header_owner(buf);
7457 ref->generation = btrfs_header_generation(buf);
7459 ret = btrfs_add_leaf_ref(root, ref, 0);
7461 btrfs_free_leaf_ref(root, ref);
7466 static noinline int invalidate_extent_cache(struct btrfs_root *root,
7467 struct extent_buffer *leaf,
7468 struct btrfs_block_group_cache *group,
7469 struct btrfs_root *target_root)
7471 struct btrfs_key key;
7472 struct inode *inode = NULL;
7473 struct btrfs_file_extent_item *fi;
7474 struct extent_state *cached_state = NULL;
7476 u64 skip_objectid = 0;
7480 nritems = btrfs_header_nritems(leaf);
7481 for (i = 0; i < nritems; i++) {
7482 btrfs_item_key_to_cpu(leaf, &key, i);
7483 if (key.objectid == skip_objectid ||
7484 key.type != BTRFS_EXTENT_DATA_KEY)
7486 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7487 if (btrfs_file_extent_type(leaf, fi) ==
7488 BTRFS_FILE_EXTENT_INLINE)
7490 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7492 if (!inode || btrfs_ino(inode) != key.objectid) {
7494 inode = btrfs_ilookup(target_root->fs_info->sb,
7495 key.objectid, target_root, 1);
7498 skip_objectid = key.objectid;
7501 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7503 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7504 key.offset + num_bytes - 1, 0, &cached_state,
7506 btrfs_drop_extent_cache(inode, key.offset,
7507 key.offset + num_bytes - 1, 1);
7508 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7509 key.offset + num_bytes - 1, &cached_state,
7517 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
7518 struct btrfs_root *root,
7519 struct extent_buffer *leaf,
7520 struct btrfs_block_group_cache *group,
7521 struct inode *reloc_inode)
7523 struct btrfs_key key;
7524 struct btrfs_key extent_key;
7525 struct btrfs_file_extent_item *fi;
7526 struct btrfs_leaf_ref *ref;
7527 struct disk_extent *new_extent;
7536 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7540 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7544 nritems = btrfs_header_nritems(leaf);
7545 for (i = 0; i < nritems; i++) {
7546 btrfs_item_key_to_cpu(leaf, &key, i);
7547 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7549 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7550 if (btrfs_file_extent_type(leaf, fi) ==
7551 BTRFS_FILE_EXTENT_INLINE)
7553 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7554 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7559 if (bytenr >= group->key.objectid + group->key.offset ||
7560 bytenr + num_bytes <= group->key.objectid)
7563 extent_key.objectid = bytenr;
7564 extent_key.offset = num_bytes;
7565 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7567 ret = get_new_locations(reloc_inode, &extent_key,
7568 group->key.objectid, 1,
7569 &new_extent, &nr_extent);
7574 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7575 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7576 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7577 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7579 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7580 new_extent->disk_bytenr);
7581 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7582 new_extent->disk_num_bytes);
7583 btrfs_mark_buffer_dirty(leaf);
7585 ret = btrfs_inc_extent_ref(trans, root,
7586 new_extent->disk_bytenr,
7587 new_extent->disk_num_bytes,
7589 root->root_key.objectid,
7590 trans->transid, key.objectid);
7593 ret = btrfs_free_extent(trans, root,
7594 bytenr, num_bytes, leaf->start,
7595 btrfs_header_owner(leaf),
7596 btrfs_header_generation(leaf),
7602 BUG_ON(ext_index + 1 != ref->nritems);
7603 btrfs_free_leaf_ref(root, ref);
7607 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7608 struct btrfs_root *root)
7610 struct btrfs_root *reloc_root;
7613 if (root->reloc_root) {
7614 reloc_root = root->reloc_root;
7615 root->reloc_root = NULL;
7616 list_add(&reloc_root->dead_list,
7617 &root->fs_info->dead_reloc_roots);
7619 btrfs_set_root_bytenr(&reloc_root->root_item,
7620 reloc_root->node->start);
7621 btrfs_set_root_level(&root->root_item,
7622 btrfs_header_level(reloc_root->node));
7623 memset(&reloc_root->root_item.drop_progress, 0,
7624 sizeof(struct btrfs_disk_key));
7625 reloc_root->root_item.drop_level = 0;
7627 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7628 &reloc_root->root_key,
7629 &reloc_root->root_item);
7635 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7637 struct btrfs_trans_handle *trans;
7638 struct btrfs_root *reloc_root;
7639 struct btrfs_root *prev_root = NULL;
7640 struct list_head dead_roots;
7644 INIT_LIST_HEAD(&dead_roots);
7645 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7647 while (!list_empty(&dead_roots)) {
7648 reloc_root = list_entry(dead_roots.prev,
7649 struct btrfs_root, dead_list);
7650 list_del_init(&reloc_root->dead_list);
7652 BUG_ON(reloc_root->commit_root != NULL);
7654 trans = btrfs_join_transaction(root, 1);
7655 BUG_ON(IS_ERR(trans));
7657 mutex_lock(&root->fs_info->drop_mutex);
7658 ret = btrfs_drop_snapshot(trans, reloc_root);
7661 mutex_unlock(&root->fs_info->drop_mutex);
7663 nr = trans->blocks_used;
7664 ret = btrfs_end_transaction(trans, root);
7666 btrfs_btree_balance_dirty(root, nr);
7669 free_extent_buffer(reloc_root->node);
7671 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7672 &reloc_root->root_key);
7674 mutex_unlock(&root->fs_info->drop_mutex);
7676 nr = trans->blocks_used;
7677 ret = btrfs_end_transaction(trans, root);
7679 btrfs_btree_balance_dirty(root, nr);
7682 prev_root = reloc_root;
7685 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7691 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7693 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7697 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7699 struct btrfs_root *reloc_root;
7700 struct btrfs_trans_handle *trans;
7701 struct btrfs_key location;
7705 mutex_lock(&root->fs_info->tree_reloc_mutex);
7706 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7708 found = !list_empty(&root->fs_info->dead_reloc_roots);
7709 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7712 trans = btrfs_start_transaction(root, 1);
7713 BUG_ON(IS_ERR(trans));
7714 ret = btrfs_commit_transaction(trans, root);
7718 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7719 location.offset = (u64)-1;
7720 location.type = BTRFS_ROOT_ITEM_KEY;
7722 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7723 BUG_ON(!reloc_root);
7724 ret = btrfs_orphan_cleanup(reloc_root);
7729 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
7730 struct btrfs_root *root)
7732 struct btrfs_root *reloc_root;
7733 struct extent_buffer *eb;
7734 struct btrfs_root_item *root_item;
7735 struct btrfs_key root_key;
7738 BUG_ON(!root->ref_cows);
7739 if (root->reloc_root)
7742 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7746 ret = btrfs_copy_root(trans, root, root->commit_root,
7747 &eb, BTRFS_TREE_RELOC_OBJECTID);
7750 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7751 root_key.offset = root->root_key.objectid;
7752 root_key.type = BTRFS_ROOT_ITEM_KEY;
7754 memcpy(root_item, &root->root_item, sizeof(root_item));
7755 btrfs_set_root_refs(root_item, 0);
7756 btrfs_set_root_bytenr(root_item, eb->start);
7757 btrfs_set_root_level(root_item, btrfs_header_level(eb));
7758 btrfs_set_root_generation(root_item, trans->transid);
7760 btrfs_tree_unlock(eb);
7761 free_extent_buffer(eb);
7763 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7764 &root_key, root_item);
7768 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7770 BUG_ON(IS_ERR(reloc_root));
7771 reloc_root->last_trans = trans->transid;
7772 reloc_root->commit_root = NULL;
7773 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7775 root->reloc_root = reloc_root;
7780 * Core function of space balance.
7782 * The idea is using reloc trees to relocate tree blocks in reference
7783 * counted roots. There is one reloc tree for each subvol, and all
7784 * reloc trees share same root key objectid. Reloc trees are snapshots
7785 * of the latest committed roots of subvols (root->commit_root).
7787 * To relocate a tree block referenced by a subvol, there are two steps.
7788 * COW the block through subvol's reloc tree, then update block pointer
7789 * in the subvol to point to the new block. Since all reloc trees share
7790 * same root key objectid, doing special handing for tree blocks owned
7791 * by them is easy. Once a tree block has been COWed in one reloc tree,
7792 * we can use the resulting new block directly when the same block is
7793 * required to COW again through other reloc trees. By this way, relocated
7794 * tree blocks are shared between reloc trees, so they are also shared
7797 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
7798 struct btrfs_root *root,
7799 struct btrfs_path *path,
7800 struct btrfs_key *first_key,
7801 struct btrfs_ref_path *ref_path,
7802 struct btrfs_block_group_cache *group,
7803 struct inode *reloc_inode)
7805 struct btrfs_root *reloc_root;
7806 struct extent_buffer *eb = NULL;
7807 struct btrfs_key *keys;
7811 int lowest_level = 0;
7814 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7815 lowest_level = ref_path->owner_objectid;
7817 if (!root->ref_cows) {
7818 path->lowest_level = lowest_level;
7819 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7821 path->lowest_level = 0;
7822 btrfs_release_path(root, path);
7826 mutex_lock(&root->fs_info->tree_reloc_mutex);
7827 ret = init_reloc_tree(trans, root);
7829 reloc_root = root->reloc_root;
7831 shared_level = ref_path->shared_level;
7832 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
7834 keys = ref_path->node_keys;
7835 nodes = ref_path->new_nodes;
7836 memset(&keys[shared_level + 1], 0,
7837 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7838 memset(&nodes[shared_level + 1], 0,
7839 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
7841 if (nodes[lowest_level] == 0) {
7842 path->lowest_level = lowest_level;
7843 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7846 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7847 eb = path->nodes[level];
7848 if (!eb || eb == reloc_root->node)
7850 nodes[level] = eb->start;
7852 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7854 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7857 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7858 eb = path->nodes[0];
7859 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7860 group, reloc_inode);
7863 btrfs_release_path(reloc_root, path);
7865 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
7871 * replace tree blocks in the fs tree with tree blocks in
7874 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7877 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7878 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7881 extent_buffer_get(path->nodes[0]);
7882 eb = path->nodes[0];
7883 btrfs_release_path(reloc_root, path);
7884 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7886 free_extent_buffer(eb);
7889 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7890 path->lowest_level = 0;
7894 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
7895 struct btrfs_root *root,
7896 struct btrfs_path *path,
7897 struct btrfs_key *first_key,
7898 struct btrfs_ref_path *ref_path)
7902 ret = relocate_one_path(trans, root, path, first_key,
7903 ref_path, NULL, NULL);
7909 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7910 struct btrfs_root *extent_root,
7911 struct btrfs_path *path,
7912 struct btrfs_key *extent_key)
7916 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7919 ret = btrfs_del_item(trans, extent_root, path);
7921 btrfs_release_path(extent_root, path);
7925 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7926 struct btrfs_ref_path *ref_path)
7928 struct btrfs_key root_key;
7930 root_key.objectid = ref_path->root_objectid;
7931 root_key.type = BTRFS_ROOT_ITEM_KEY;
7932 if (is_cowonly_root(ref_path->root_objectid))
7933 root_key.offset = 0;
7935 root_key.offset = (u64)-1;
7937 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7940 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7941 struct btrfs_path *path,
7942 struct btrfs_key *extent_key,
7943 struct btrfs_block_group_cache *group,
7944 struct inode *reloc_inode, int pass)
7946 struct btrfs_trans_handle *trans;
7947 struct btrfs_root *found_root;
7948 struct btrfs_ref_path *ref_path = NULL;
7949 struct disk_extent *new_extents = NULL;
7954 struct btrfs_key first_key;
7958 trans = btrfs_start_transaction(extent_root, 1);
7959 BUG_ON(IS_ERR(trans));
7961 if (extent_key->objectid == 0) {
7962 ret = del_extent_zero(trans, extent_root, path, extent_key);
7966 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7972 for (loops = 0; ; loops++) {
7974 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7975 extent_key->objectid);
7977 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7984 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7985 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7988 found_root = read_ref_root(extent_root->fs_info, ref_path);
7989 BUG_ON(!found_root);
7991 * for reference counted tree, only process reference paths
7992 * rooted at the latest committed root.
7994 if (found_root->ref_cows &&
7995 ref_path->root_generation != found_root->root_key.offset)
7998 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8001 * copy data extents to new locations
8003 u64 group_start = group->key.objectid;
8004 ret = relocate_data_extent(reloc_inode,
8013 level = ref_path->owner_objectid;
8016 if (prev_block != ref_path->nodes[level]) {
8017 struct extent_buffer *eb;
8018 u64 block_start = ref_path->nodes[level];
8019 u64 block_size = btrfs_level_size(found_root, level);
8021 eb = read_tree_block(found_root, block_start,
8027 btrfs_tree_lock(eb);
8028 BUG_ON(level != btrfs_header_level(eb));
8031 btrfs_item_key_to_cpu(eb, &first_key, 0);
8033 btrfs_node_key_to_cpu(eb, &first_key, 0);
8035 btrfs_tree_unlock(eb);
8036 free_extent_buffer(eb);
8037 prev_block = block_start;
8040 mutex_lock(&extent_root->fs_info->trans_mutex);
8041 btrfs_record_root_in_trans(found_root);
8042 mutex_unlock(&extent_root->fs_info->trans_mutex);
8043 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8045 * try to update data extent references while
8046 * keeping metadata shared between snapshots.
8049 ret = relocate_one_path(trans, found_root,
8050 path, &first_key, ref_path,
8051 group, reloc_inode);
8057 * use fallback method to process the remaining
8061 u64 group_start = group->key.objectid;
8062 new_extents = kmalloc(sizeof(*new_extents),
8069 ret = get_new_locations(reloc_inode,
8077 ret = replace_one_extent(trans, found_root,
8079 &first_key, ref_path,
8080 new_extents, nr_extents);
8082 ret = relocate_tree_block(trans, found_root, path,
8083 &first_key, ref_path);
8090 btrfs_end_transaction(trans, extent_root);
8097 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8100 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8101 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8104 * we add in the count of missing devices because we want
8105 * to make sure that any RAID levels on a degraded FS
8106 * continue to be honored.
8108 num_devices = root->fs_info->fs_devices->rw_devices +
8109 root->fs_info->fs_devices->missing_devices;
8111 if (num_devices == 1) {
8112 stripped |= BTRFS_BLOCK_GROUP_DUP;
8113 stripped = flags & ~stripped;
8115 /* turn raid0 into single device chunks */
8116 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8119 /* turn mirroring into duplication */
8120 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8121 BTRFS_BLOCK_GROUP_RAID10))
8122 return stripped | BTRFS_BLOCK_GROUP_DUP;
8125 /* they already had raid on here, just return */
8126 if (flags & stripped)
8129 stripped |= BTRFS_BLOCK_GROUP_DUP;
8130 stripped = flags & ~stripped;
8132 /* switch duplicated blocks with raid1 */
8133 if (flags & BTRFS_BLOCK_GROUP_DUP)
8134 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8136 /* turn single device chunks into raid0 */
8137 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8142 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
8144 struct btrfs_space_info *sinfo = cache->space_info;
8151 spin_lock(&sinfo->lock);
8152 spin_lock(&cache->lock);
8153 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8154 cache->bytes_super - btrfs_block_group_used(&cache->item);
8156 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8157 sinfo->bytes_may_use + sinfo->bytes_readonly +
8158 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
8159 sinfo->bytes_readonly += num_bytes;
8160 sinfo->bytes_reserved += cache->reserved_pinned;
8161 cache->reserved_pinned = 0;
8166 spin_unlock(&cache->lock);
8167 spin_unlock(&sinfo->lock);
8171 int btrfs_set_block_group_ro(struct btrfs_root *root,
8172 struct btrfs_block_group_cache *cache)
8175 struct btrfs_trans_handle *trans;
8181 trans = btrfs_join_transaction(root, 1);
8182 BUG_ON(IS_ERR(trans));
8184 alloc_flags = update_block_group_flags(root, cache->flags);
8185 if (alloc_flags != cache->flags)
8186 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8189 ret = set_block_group_ro(cache);
8192 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8193 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8197 ret = set_block_group_ro(cache);
8199 btrfs_end_transaction(trans, root);
8203 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8204 struct btrfs_root *root, u64 type)
8206 u64 alloc_flags = get_alloc_profile(root, type);
8207 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8212 * helper to account the unused space of all the readonly block group in the
8213 * list. takes mirrors into account.
8215 static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8217 struct btrfs_block_group_cache *block_group;
8221 list_for_each_entry(block_group, groups_list, list) {
8222 spin_lock(&block_group->lock);
8224 if (!block_group->ro) {
8225 spin_unlock(&block_group->lock);
8229 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8230 BTRFS_BLOCK_GROUP_RAID10 |
8231 BTRFS_BLOCK_GROUP_DUP))
8236 free_bytes += (block_group->key.offset -
8237 btrfs_block_group_used(&block_group->item)) *
8240 spin_unlock(&block_group->lock);
8247 * helper to account the unused space of all the readonly block group in the
8248 * space_info. takes mirrors into account.
8250 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8255 spin_lock(&sinfo->lock);
8257 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8258 if (!list_empty(&sinfo->block_groups[i]))
8259 free_bytes += __btrfs_get_ro_block_group_free_space(
8260 &sinfo->block_groups[i]);
8262 spin_unlock(&sinfo->lock);
8267 int btrfs_set_block_group_rw(struct btrfs_root *root,
8268 struct btrfs_block_group_cache *cache)
8270 struct btrfs_space_info *sinfo = cache->space_info;
8275 spin_lock(&sinfo->lock);
8276 spin_lock(&cache->lock);
8277 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8278 cache->bytes_super - btrfs_block_group_used(&cache->item);
8279 sinfo->bytes_readonly -= num_bytes;
8281 spin_unlock(&cache->lock);
8282 spin_unlock(&sinfo->lock);
8287 * checks to see if its even possible to relocate this block group.
8289 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8290 * ok to go ahead and try.
8292 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
8294 struct btrfs_block_group_cache *block_group;
8295 struct btrfs_space_info *space_info;
8296 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8297 struct btrfs_device *device;
8301 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
8303 /* odd, couldn't find the block group, leave it alone */
8307 /* no bytes used, we're good */
8308 if (!btrfs_block_group_used(&block_group->item))
8311 space_info = block_group->space_info;
8312 spin_lock(&space_info->lock);
8314 full = space_info->full;
8317 * if this is the last block group we have in this space, we can't
8318 * relocate it unless we're able to allocate a new chunk below.
8320 * Otherwise, we need to make sure we have room in the space to handle
8321 * all of the extents from this block group. If we can, we're good
8323 if ((space_info->total_bytes != block_group->key.offset) &&
8324 (space_info->bytes_used + space_info->bytes_reserved +
8325 space_info->bytes_pinned + space_info->bytes_readonly +
8326 btrfs_block_group_used(&block_group->item) <
8327 space_info->total_bytes)) {
8328 spin_unlock(&space_info->lock);
8331 spin_unlock(&space_info->lock);
8334 * ok we don't have enough space, but maybe we have free space on our
8335 * devices to allocate new chunks for relocation, so loop through our
8336 * alloc devices and guess if we have enough space. However, if we
8337 * were marked as full, then we know there aren't enough chunks, and we
8344 mutex_lock(&root->fs_info->chunk_mutex);
8345 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8346 u64 min_free = btrfs_block_group_used(&block_group->item);
8350 * check to make sure we can actually find a chunk with enough
8351 * space to fit our block group in.
8353 if (device->total_bytes > device->bytes_used + min_free) {
8354 ret = find_free_dev_extent(NULL, device, min_free,
8361 mutex_unlock(&root->fs_info->chunk_mutex);
8363 btrfs_put_block_group(block_group);
8367 static int find_first_block_group(struct btrfs_root *root,
8368 struct btrfs_path *path, struct btrfs_key *key)
8371 struct btrfs_key found_key;
8372 struct extent_buffer *leaf;
8375 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8380 slot = path->slots[0];
8381 leaf = path->nodes[0];
8382 if (slot >= btrfs_header_nritems(leaf)) {
8383 ret = btrfs_next_leaf(root, path);
8390 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8392 if (found_key.objectid >= key->objectid &&
8393 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8403 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8405 struct btrfs_block_group_cache *block_group;
8409 struct inode *inode;
8411 block_group = btrfs_lookup_first_block_group(info, last);
8412 while (block_group) {
8413 spin_lock(&block_group->lock);
8414 if (block_group->iref)
8416 spin_unlock(&block_group->lock);
8417 block_group = next_block_group(info->tree_root,
8427 inode = block_group->inode;
8428 block_group->iref = 0;
8429 block_group->inode = NULL;
8430 spin_unlock(&block_group->lock);
8432 last = block_group->key.objectid + block_group->key.offset;
8433 btrfs_put_block_group(block_group);
8437 int btrfs_free_block_groups(struct btrfs_fs_info *info)
8439 struct btrfs_block_group_cache *block_group;
8440 struct btrfs_space_info *space_info;
8441 struct btrfs_caching_control *caching_ctl;
8444 down_write(&info->extent_commit_sem);
8445 while (!list_empty(&info->caching_block_groups)) {
8446 caching_ctl = list_entry(info->caching_block_groups.next,
8447 struct btrfs_caching_control, list);
8448 list_del(&caching_ctl->list);
8449 put_caching_control(caching_ctl);
8451 up_write(&info->extent_commit_sem);
8453 spin_lock(&info->block_group_cache_lock);
8454 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8455 block_group = rb_entry(n, struct btrfs_block_group_cache,
8457 rb_erase(&block_group->cache_node,
8458 &info->block_group_cache_tree);
8459 spin_unlock(&info->block_group_cache_lock);
8461 down_write(&block_group->space_info->groups_sem);
8462 list_del(&block_group->list);
8463 up_write(&block_group->space_info->groups_sem);
8465 if (block_group->cached == BTRFS_CACHE_STARTED)
8466 wait_block_group_cache_done(block_group);
8469 * We haven't cached this block group, which means we could
8470 * possibly have excluded extents on this block group.
8472 if (block_group->cached == BTRFS_CACHE_NO)
8473 free_excluded_extents(info->extent_root, block_group);
8475 btrfs_remove_free_space_cache(block_group);
8476 btrfs_put_block_group(block_group);
8478 spin_lock(&info->block_group_cache_lock);
8480 spin_unlock(&info->block_group_cache_lock);
8482 /* now that all the block groups are freed, go through and
8483 * free all the space_info structs. This is only called during
8484 * the final stages of unmount, and so we know nobody is
8485 * using them. We call synchronize_rcu() once before we start,
8486 * just to be on the safe side.
8490 release_global_block_rsv(info);
8492 while(!list_empty(&info->space_info)) {
8493 space_info = list_entry(info->space_info.next,
8494 struct btrfs_space_info,
8496 if (space_info->bytes_pinned > 0 ||
8497 space_info->bytes_reserved > 0) {
8499 dump_space_info(space_info, 0, 0);
8501 list_del(&space_info->list);
8507 static void __link_block_group(struct btrfs_space_info *space_info,
8508 struct btrfs_block_group_cache *cache)
8510 int index = get_block_group_index(cache);
8512 down_write(&space_info->groups_sem);
8513 list_add_tail(&cache->list, &space_info->block_groups[index]);
8514 up_write(&space_info->groups_sem);
8517 int btrfs_read_block_groups(struct btrfs_root *root)
8519 struct btrfs_path *path;
8521 struct btrfs_block_group_cache *cache;
8522 struct btrfs_fs_info *info = root->fs_info;
8523 struct btrfs_space_info *space_info;
8524 struct btrfs_key key;
8525 struct btrfs_key found_key;
8526 struct extent_buffer *leaf;
8530 root = info->extent_root;
8533 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
8534 path = btrfs_alloc_path();
8538 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8539 if (cache_gen != 0 &&
8540 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8542 if (btrfs_test_opt(root, CLEAR_CACHE))
8544 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8545 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
8548 ret = find_first_block_group(root, path, &key);
8553 leaf = path->nodes[0];
8554 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8555 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8560 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8562 if (!cache->free_space_ctl) {
8568 atomic_set(&cache->count, 1);
8569 spin_lock_init(&cache->lock);
8570 cache->fs_info = info;
8571 INIT_LIST_HEAD(&cache->list);
8572 INIT_LIST_HEAD(&cache->cluster_list);
8575 cache->disk_cache_state = BTRFS_DC_CLEAR;
8577 read_extent_buffer(leaf, &cache->item,
8578 btrfs_item_ptr_offset(leaf, path->slots[0]),
8579 sizeof(cache->item));
8580 memcpy(&cache->key, &found_key, sizeof(found_key));
8582 key.objectid = found_key.objectid + found_key.offset;
8583 btrfs_release_path(root, path);
8584 cache->flags = btrfs_block_group_flags(&cache->item);
8585 cache->sectorsize = root->sectorsize;
8587 btrfs_init_free_space_ctl(cache);
8590 * We need to exclude the super stripes now so that the space
8591 * info has super bytes accounted for, otherwise we'll think
8592 * we have more space than we actually do.
8594 exclude_super_stripes(root, cache);
8597 * check for two cases, either we are full, and therefore
8598 * don't need to bother with the caching work since we won't
8599 * find any space, or we are empty, and we can just add all
8600 * the space in and be done with it. This saves us _alot_ of
8601 * time, particularly in the full case.
8603 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
8604 cache->last_byte_to_unpin = (u64)-1;
8605 cache->cached = BTRFS_CACHE_FINISHED;
8606 free_excluded_extents(root, cache);
8607 } else if (btrfs_block_group_used(&cache->item) == 0) {
8608 cache->last_byte_to_unpin = (u64)-1;
8609 cache->cached = BTRFS_CACHE_FINISHED;
8610 add_new_free_space(cache, root->fs_info,
8612 found_key.objectid +
8614 free_excluded_extents(root, cache);
8617 ret = update_space_info(info, cache->flags, found_key.offset,
8618 btrfs_block_group_used(&cache->item),
8621 cache->space_info = space_info;
8622 spin_lock(&cache->space_info->lock);
8623 cache->space_info->bytes_readonly += cache->bytes_super;
8624 spin_unlock(&cache->space_info->lock);
8626 __link_block_group(space_info, cache);
8628 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8631 set_avail_alloc_bits(root->fs_info, cache->flags);
8632 if (btrfs_chunk_readonly(root, cache->key.objectid))
8633 set_block_group_ro(cache);
8636 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8637 if (!(get_alloc_profile(root, space_info->flags) &
8638 (BTRFS_BLOCK_GROUP_RAID10 |
8639 BTRFS_BLOCK_GROUP_RAID1 |
8640 BTRFS_BLOCK_GROUP_DUP)))
8643 * avoid allocating from un-mirrored block group if there are
8644 * mirrored block groups.
8646 list_for_each_entry(cache, &space_info->block_groups[3], list)
8647 set_block_group_ro(cache);
8648 list_for_each_entry(cache, &space_info->block_groups[4], list)
8649 set_block_group_ro(cache);
8652 init_global_block_rsv(info);
8655 btrfs_free_path(path);
8659 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8660 struct btrfs_root *root, u64 bytes_used,
8661 u64 type, u64 chunk_objectid, u64 chunk_offset,
8665 struct btrfs_root *extent_root;
8666 struct btrfs_block_group_cache *cache;
8668 extent_root = root->fs_info->extent_root;
8670 root->fs_info->last_trans_log_full_commit = trans->transid;
8672 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8675 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8677 if (!cache->free_space_ctl) {
8682 cache->key.objectid = chunk_offset;
8683 cache->key.offset = size;
8684 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8685 cache->sectorsize = root->sectorsize;
8686 cache->fs_info = root->fs_info;
8688 atomic_set(&cache->count, 1);
8689 spin_lock_init(&cache->lock);
8690 INIT_LIST_HEAD(&cache->list);
8691 INIT_LIST_HEAD(&cache->cluster_list);
8693 btrfs_init_free_space_ctl(cache);
8695 btrfs_set_block_group_used(&cache->item, bytes_used);
8696 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8697 cache->flags = type;
8698 btrfs_set_block_group_flags(&cache->item, type);
8700 cache->last_byte_to_unpin = (u64)-1;
8701 cache->cached = BTRFS_CACHE_FINISHED;
8702 exclude_super_stripes(root, cache);
8704 add_new_free_space(cache, root->fs_info, chunk_offset,
8705 chunk_offset + size);
8707 free_excluded_extents(root, cache);
8709 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8710 &cache->space_info);
8713 spin_lock(&cache->space_info->lock);
8714 cache->space_info->bytes_readonly += cache->bytes_super;
8715 spin_unlock(&cache->space_info->lock);
8717 __link_block_group(cache->space_info, cache);
8719 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8722 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8723 sizeof(cache->item));
8726 set_avail_alloc_bits(extent_root->fs_info, type);
8731 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8732 struct btrfs_root *root, u64 group_start)
8734 struct btrfs_path *path;
8735 struct btrfs_block_group_cache *block_group;
8736 struct btrfs_free_cluster *cluster;
8737 struct btrfs_root *tree_root = root->fs_info->tree_root;
8738 struct btrfs_key key;
8739 struct inode *inode;
8743 root = root->fs_info->extent_root;
8745 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8746 BUG_ON(!block_group);
8747 BUG_ON(!block_group->ro);
8750 * Free the reserved super bytes from this block group before
8753 free_excluded_extents(root, block_group);
8755 memcpy(&key, &block_group->key, sizeof(key));
8756 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8757 BTRFS_BLOCK_GROUP_RAID1 |
8758 BTRFS_BLOCK_GROUP_RAID10))
8763 /* make sure this block group isn't part of an allocation cluster */
8764 cluster = &root->fs_info->data_alloc_cluster;
8765 spin_lock(&cluster->refill_lock);
8766 btrfs_return_cluster_to_free_space(block_group, cluster);
8767 spin_unlock(&cluster->refill_lock);
8770 * make sure this block group isn't part of a metadata
8771 * allocation cluster
8773 cluster = &root->fs_info->meta_alloc_cluster;
8774 spin_lock(&cluster->refill_lock);
8775 btrfs_return_cluster_to_free_space(block_group, cluster);
8776 spin_unlock(&cluster->refill_lock);
8778 path = btrfs_alloc_path();
8781 inode = lookup_free_space_inode(root, block_group, path);
8782 if (!IS_ERR(inode)) {
8783 btrfs_orphan_add(trans, inode);
8785 /* One for the block groups ref */
8786 spin_lock(&block_group->lock);
8787 if (block_group->iref) {
8788 block_group->iref = 0;
8789 block_group->inode = NULL;
8790 spin_unlock(&block_group->lock);
8793 spin_unlock(&block_group->lock);
8795 /* One for our lookup ref */
8799 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8800 key.offset = block_group->key.objectid;
8803 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8807 btrfs_release_path(tree_root, path);
8809 ret = btrfs_del_item(trans, tree_root, path);
8812 btrfs_release_path(tree_root, path);
8815 spin_lock(&root->fs_info->block_group_cache_lock);
8816 rb_erase(&block_group->cache_node,
8817 &root->fs_info->block_group_cache_tree);
8818 spin_unlock(&root->fs_info->block_group_cache_lock);
8820 down_write(&block_group->space_info->groups_sem);
8822 * we must use list_del_init so people can check to see if they
8823 * are still on the list after taking the semaphore
8825 list_del_init(&block_group->list);
8826 up_write(&block_group->space_info->groups_sem);
8828 if (block_group->cached == BTRFS_CACHE_STARTED)
8829 wait_block_group_cache_done(block_group);
8831 btrfs_remove_free_space_cache(block_group);
8833 spin_lock(&block_group->space_info->lock);
8834 block_group->space_info->total_bytes -= block_group->key.offset;
8835 block_group->space_info->bytes_readonly -= block_group->key.offset;
8836 block_group->space_info->disk_total -= block_group->key.offset * factor;
8837 spin_unlock(&block_group->space_info->lock);
8839 memcpy(&key, &block_group->key, sizeof(key));
8841 btrfs_clear_space_info_full(root->fs_info);
8843 btrfs_put_block_group(block_group);
8844 btrfs_put_block_group(block_group);
8846 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8852 ret = btrfs_del_item(trans, root, path);
8854 btrfs_free_path(path);
8858 int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8860 struct btrfs_space_info *space_info;
8861 struct btrfs_super_block *disk_super;
8867 disk_super = &fs_info->super_copy;
8868 if (!btrfs_super_root(disk_super))
8871 features = btrfs_super_incompat_flags(disk_super);
8872 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
8875 flags = BTRFS_BLOCK_GROUP_SYSTEM;
8876 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8881 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
8882 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8884 flags = BTRFS_BLOCK_GROUP_METADATA;
8885 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8889 flags = BTRFS_BLOCK_GROUP_DATA;
8890 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
8896 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8898 return unpin_extent_range(root, start, end);
8901 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
8902 u64 num_bytes, u64 *actual_bytes)
8904 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
8907 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8909 struct btrfs_fs_info *fs_info = root->fs_info;
8910 struct btrfs_block_group_cache *cache = NULL;
8917 cache = btrfs_lookup_block_group(fs_info, range->start);
8920 if (cache->key.objectid >= (range->start + range->len)) {
8921 btrfs_put_block_group(cache);
8925 start = max(range->start, cache->key.objectid);
8926 end = min(range->start + range->len,
8927 cache->key.objectid + cache->key.offset);
8929 if (end - start >= range->minlen) {
8930 if (!block_group_cache_done(cache)) {
8931 ret = cache_block_group(cache, NULL, root, 0);
8933 wait_block_group_cache_done(cache);
8935 ret = btrfs_trim_block_group(cache,
8941 trimmed += group_trimmed;
8943 btrfs_put_block_group(cache);
8948 cache = next_block_group(fs_info->tree_root, cache);
8951 range->len = trimmed;