2 * Copyright (C) 2007 Oracle. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
18 #include <linux/sched.h>
19 #include <linux/pagemap.h>
20 #include <linux/writeback.h>
21 #include <linux/blkdev.h>
22 #include <linux/sort.h>
23 #include <linux/rcupdate.h>
24 #include <linux/kthread.h>
25 #include <linux/slab.h>
30 #include "print-tree.h"
31 #include "transaction.h"
34 #include "free-space-cache.h"
36 /* control flags for do_chunk_alloc's force field
37 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
38 * if we really need one.
40 * CHUNK_ALLOC_FORCE means it must try to allocate one
42 * CHUNK_ALLOC_LIMITED means to only try and allocate one
43 * if we have very few chunks already allocated. This is
44 * used as part of the clustering code to help make sure
45 * we have a good pool of storage to cluster in, without
46 * filling the FS with empty chunks
50 CHUNK_ALLOC_NO_FORCE = 0,
51 CHUNK_ALLOC_FORCE = 1,
52 CHUNK_ALLOC_LIMITED = 2,
55 static int update_block_group(struct btrfs_trans_handle *trans,
56 struct btrfs_root *root,
57 u64 bytenr, u64 num_bytes, int alloc);
58 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
59 struct btrfs_root *root,
60 u64 bytenr, u64 num_bytes, u64 parent,
61 u64 root_objectid, u64 owner_objectid,
62 u64 owner_offset, int refs_to_drop,
63 struct btrfs_delayed_extent_op *extra_op);
64 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
65 struct extent_buffer *leaf,
66 struct btrfs_extent_item *ei);
67 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
68 struct btrfs_root *root,
69 u64 parent, u64 root_objectid,
70 u64 flags, u64 owner, u64 offset,
71 struct btrfs_key *ins, int ref_mod);
72 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
73 struct btrfs_root *root,
74 u64 parent, u64 root_objectid,
75 u64 flags, struct btrfs_disk_key *key,
76 int level, struct btrfs_key *ins);
77 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
78 struct btrfs_root *extent_root, u64 alloc_bytes,
79 u64 flags, int force);
80 static int find_next_key(struct btrfs_path *path, int level,
81 struct btrfs_key *key);
82 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
83 int dump_block_groups);
86 block_group_cache_done(struct btrfs_block_group_cache *cache)
89 return cache->cached == BTRFS_CACHE_FINISHED;
92 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
94 return (cache->flags & bits) == bits;
97 static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
99 atomic_inc(&cache->count);
102 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
104 if (atomic_dec_and_test(&cache->count)) {
105 WARN_ON(cache->pinned > 0);
106 WARN_ON(cache->reserved > 0);
107 WARN_ON(cache->reserved_pinned > 0);
113 * this adds the block group to the fs_info rb tree for the block group
116 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
117 struct btrfs_block_group_cache *block_group)
120 struct rb_node *parent = NULL;
121 struct btrfs_block_group_cache *cache;
123 spin_lock(&info->block_group_cache_lock);
124 p = &info->block_group_cache_tree.rb_node;
128 cache = rb_entry(parent, struct btrfs_block_group_cache,
130 if (block_group->key.objectid < cache->key.objectid) {
132 } else if (block_group->key.objectid > cache->key.objectid) {
135 spin_unlock(&info->block_group_cache_lock);
140 rb_link_node(&block_group->cache_node, parent, p);
141 rb_insert_color(&block_group->cache_node,
142 &info->block_group_cache_tree);
143 spin_unlock(&info->block_group_cache_lock);
149 * This will return the block group at or after bytenr if contains is 0, else
150 * it will return the block group that contains the bytenr
152 static struct btrfs_block_group_cache *
153 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
156 struct btrfs_block_group_cache *cache, *ret = NULL;
160 spin_lock(&info->block_group_cache_lock);
161 n = info->block_group_cache_tree.rb_node;
164 cache = rb_entry(n, struct btrfs_block_group_cache,
166 end = cache->key.objectid + cache->key.offset - 1;
167 start = cache->key.objectid;
169 if (bytenr < start) {
170 if (!contains && (!ret || start < ret->key.objectid))
173 } else if (bytenr > start) {
174 if (contains && bytenr <= end) {
185 btrfs_get_block_group(ret);
186 spin_unlock(&info->block_group_cache_lock);
191 static int add_excluded_extent(struct btrfs_root *root,
192 u64 start, u64 num_bytes)
194 u64 end = start + num_bytes - 1;
195 set_extent_bits(&root->fs_info->freed_extents[0],
196 start, end, EXTENT_UPTODATE, GFP_NOFS);
197 set_extent_bits(&root->fs_info->freed_extents[1],
198 start, end, EXTENT_UPTODATE, GFP_NOFS);
202 static void free_excluded_extents(struct btrfs_root *root,
203 struct btrfs_block_group_cache *cache)
207 start = cache->key.objectid;
208 end = start + cache->key.offset - 1;
210 clear_extent_bits(&root->fs_info->freed_extents[0],
211 start, end, EXTENT_UPTODATE, GFP_NOFS);
212 clear_extent_bits(&root->fs_info->freed_extents[1],
213 start, end, EXTENT_UPTODATE, GFP_NOFS);
216 static int exclude_super_stripes(struct btrfs_root *root,
217 struct btrfs_block_group_cache *cache)
224 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
225 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
226 cache->bytes_super += stripe_len;
227 ret = add_excluded_extent(root, cache->key.objectid,
232 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
233 bytenr = btrfs_sb_offset(i);
234 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
235 cache->key.objectid, bytenr,
236 0, &logical, &nr, &stripe_len);
240 cache->bytes_super += stripe_len;
241 ret = add_excluded_extent(root, logical[nr],
251 static struct btrfs_caching_control *
252 get_caching_control(struct btrfs_block_group_cache *cache)
254 struct btrfs_caching_control *ctl;
256 spin_lock(&cache->lock);
257 if (cache->cached != BTRFS_CACHE_STARTED) {
258 spin_unlock(&cache->lock);
262 /* We're loading it the fast way, so we don't have a caching_ctl. */
263 if (!cache->caching_ctl) {
264 spin_unlock(&cache->lock);
268 ctl = cache->caching_ctl;
269 atomic_inc(&ctl->count);
270 spin_unlock(&cache->lock);
274 static void put_caching_control(struct btrfs_caching_control *ctl)
276 if (atomic_dec_and_test(&ctl->count))
281 * this is only called by cache_block_group, since we could have freed extents
282 * we need to check the pinned_extents for any extents that can't be used yet
283 * since their free space will be released as soon as the transaction commits.
285 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
286 struct btrfs_fs_info *info, u64 start, u64 end)
288 u64 extent_start, extent_end, size, total_added = 0;
291 while (start < end) {
292 ret = find_first_extent_bit(info->pinned_extents, start,
293 &extent_start, &extent_end,
294 EXTENT_DIRTY | EXTENT_UPTODATE);
298 if (extent_start <= start) {
299 start = extent_end + 1;
300 } else if (extent_start > start && extent_start < end) {
301 size = extent_start - start;
303 ret = btrfs_add_free_space(block_group, start,
306 start = extent_end + 1;
315 ret = btrfs_add_free_space(block_group, start, size);
322 static int caching_kthread(void *data)
324 struct btrfs_block_group_cache *block_group = data;
325 struct btrfs_fs_info *fs_info = block_group->fs_info;
326 struct btrfs_caching_control *caching_ctl = block_group->caching_ctl;
327 struct btrfs_root *extent_root = fs_info->extent_root;
328 struct btrfs_path *path;
329 struct extent_buffer *leaf;
330 struct btrfs_key key;
336 path = btrfs_alloc_path();
340 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
343 * We don't want to deadlock with somebody trying to allocate a new
344 * extent for the extent root while also trying to search the extent
345 * root to add free space. So we skip locking and search the commit
346 * root, since its read-only
348 path->skip_locking = 1;
349 path->search_commit_root = 1;
354 key.type = BTRFS_EXTENT_ITEM_KEY;
356 mutex_lock(&caching_ctl->mutex);
357 /* need to make sure the commit_root doesn't disappear */
358 down_read(&fs_info->extent_commit_sem);
360 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
364 leaf = path->nodes[0];
365 nritems = btrfs_header_nritems(leaf);
369 if (fs_info->closing > 1) {
374 if (path->slots[0] < nritems) {
375 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
377 ret = find_next_key(path, 0, &key);
381 caching_ctl->progress = last;
382 btrfs_release_path(path);
383 up_read(&fs_info->extent_commit_sem);
384 mutex_unlock(&caching_ctl->mutex);
385 if (btrfs_transaction_in_commit(fs_info))
392 if (key.objectid < block_group->key.objectid) {
397 if (key.objectid >= block_group->key.objectid +
398 block_group->key.offset)
401 if (key.type == BTRFS_EXTENT_ITEM_KEY) {
402 total_found += add_new_free_space(block_group,
405 last = key.objectid + key.offset;
407 if (total_found > (1024 * 1024 * 2)) {
409 wake_up(&caching_ctl->wait);
416 total_found += add_new_free_space(block_group, fs_info, last,
417 block_group->key.objectid +
418 block_group->key.offset);
419 caching_ctl->progress = (u64)-1;
421 spin_lock(&block_group->lock);
422 block_group->caching_ctl = NULL;
423 block_group->cached = BTRFS_CACHE_FINISHED;
424 spin_unlock(&block_group->lock);
427 btrfs_free_path(path);
428 up_read(&fs_info->extent_commit_sem);
430 free_excluded_extents(extent_root, block_group);
432 mutex_unlock(&caching_ctl->mutex);
433 wake_up(&caching_ctl->wait);
435 put_caching_control(caching_ctl);
436 atomic_dec(&block_group->space_info->caching_threads);
437 btrfs_put_block_group(block_group);
442 static int cache_block_group(struct btrfs_block_group_cache *cache,
443 struct btrfs_trans_handle *trans,
444 struct btrfs_root *root,
447 struct btrfs_fs_info *fs_info = cache->fs_info;
448 struct btrfs_caching_control *caching_ctl;
449 struct task_struct *tsk;
453 if (cache->cached != BTRFS_CACHE_NO)
457 * We can't do the read from on-disk cache during a commit since we need
458 * to have the normal tree locking. Also if we are currently trying to
459 * allocate blocks for the tree root we can't do the fast caching since
460 * we likely hold important locks.
462 if (trans && (!trans->transaction->in_commit) &&
463 (root && root != root->fs_info->tree_root)) {
464 spin_lock(&cache->lock);
465 if (cache->cached != BTRFS_CACHE_NO) {
466 spin_unlock(&cache->lock);
469 cache->cached = BTRFS_CACHE_STARTED;
470 spin_unlock(&cache->lock);
472 ret = load_free_space_cache(fs_info, cache);
474 spin_lock(&cache->lock);
476 cache->cached = BTRFS_CACHE_FINISHED;
477 cache->last_byte_to_unpin = (u64)-1;
479 cache->cached = BTRFS_CACHE_NO;
481 spin_unlock(&cache->lock);
483 free_excluded_extents(fs_info->extent_root, cache);
491 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
492 BUG_ON(!caching_ctl);
494 INIT_LIST_HEAD(&caching_ctl->list);
495 mutex_init(&caching_ctl->mutex);
496 init_waitqueue_head(&caching_ctl->wait);
497 caching_ctl->block_group = cache;
498 caching_ctl->progress = cache->key.objectid;
499 /* one for caching kthread, one for caching block group list */
500 atomic_set(&caching_ctl->count, 2);
502 spin_lock(&cache->lock);
503 if (cache->cached != BTRFS_CACHE_NO) {
504 spin_unlock(&cache->lock);
508 cache->caching_ctl = caching_ctl;
509 cache->cached = BTRFS_CACHE_STARTED;
510 spin_unlock(&cache->lock);
512 down_write(&fs_info->extent_commit_sem);
513 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
514 up_write(&fs_info->extent_commit_sem);
516 atomic_inc(&cache->space_info->caching_threads);
517 btrfs_get_block_group(cache);
519 tsk = kthread_run(caching_kthread, cache, "btrfs-cache-%llu\n",
520 cache->key.objectid);
523 printk(KERN_ERR "error running thread %d\n", ret);
531 * return the block group that starts at or after bytenr
533 static struct btrfs_block_group_cache *
534 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
536 struct btrfs_block_group_cache *cache;
538 cache = block_group_cache_tree_search(info, bytenr, 0);
544 * return the block group that contains the given bytenr
546 struct btrfs_block_group_cache *btrfs_lookup_block_group(
547 struct btrfs_fs_info *info,
550 struct btrfs_block_group_cache *cache;
552 cache = block_group_cache_tree_search(info, bytenr, 1);
557 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
560 struct list_head *head = &info->space_info;
561 struct btrfs_space_info *found;
563 flags &= BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_SYSTEM |
564 BTRFS_BLOCK_GROUP_METADATA;
567 list_for_each_entry_rcu(found, head, list) {
568 if (found->flags & flags) {
578 * after adding space to the filesystem, we need to clear the full flags
579 * on all the space infos.
581 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
583 struct list_head *head = &info->space_info;
584 struct btrfs_space_info *found;
587 list_for_each_entry_rcu(found, head, list)
592 static u64 div_factor(u64 num, int factor)
601 static u64 div_factor_fine(u64 num, int factor)
610 u64 btrfs_find_block_group(struct btrfs_root *root,
611 u64 search_start, u64 search_hint, int owner)
613 struct btrfs_block_group_cache *cache;
615 u64 last = max(search_hint, search_start);
622 cache = btrfs_lookup_first_block_group(root->fs_info, last);
626 spin_lock(&cache->lock);
627 last = cache->key.objectid + cache->key.offset;
628 used = btrfs_block_group_used(&cache->item);
630 if ((full_search || !cache->ro) &&
631 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
632 if (used + cache->pinned + cache->reserved <
633 div_factor(cache->key.offset, factor)) {
634 group_start = cache->key.objectid;
635 spin_unlock(&cache->lock);
636 btrfs_put_block_group(cache);
640 spin_unlock(&cache->lock);
641 btrfs_put_block_group(cache);
649 if (!full_search && factor < 10) {
659 /* simple helper to search for an existing extent at a given offset */
660 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
663 struct btrfs_key key;
664 struct btrfs_path *path;
666 path = btrfs_alloc_path();
668 key.objectid = start;
670 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
671 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
673 btrfs_free_path(path);
678 * helper function to lookup reference count and flags of extent.
680 * the head node for delayed ref is used to store the sum of all the
681 * reference count modifications queued up in the rbtree. the head
682 * node may also store the extent flags to set. This way you can check
683 * to see what the reference count and extent flags would be if all of
684 * the delayed refs are not processed.
686 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
687 struct btrfs_root *root, u64 bytenr,
688 u64 num_bytes, u64 *refs, u64 *flags)
690 struct btrfs_delayed_ref_head *head;
691 struct btrfs_delayed_ref_root *delayed_refs;
692 struct btrfs_path *path;
693 struct btrfs_extent_item *ei;
694 struct extent_buffer *leaf;
695 struct btrfs_key key;
701 path = btrfs_alloc_path();
705 key.objectid = bytenr;
706 key.type = BTRFS_EXTENT_ITEM_KEY;
707 key.offset = num_bytes;
709 path->skip_locking = 1;
710 path->search_commit_root = 1;
713 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
719 leaf = path->nodes[0];
720 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
721 if (item_size >= sizeof(*ei)) {
722 ei = btrfs_item_ptr(leaf, path->slots[0],
723 struct btrfs_extent_item);
724 num_refs = btrfs_extent_refs(leaf, ei);
725 extent_flags = btrfs_extent_flags(leaf, ei);
727 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
728 struct btrfs_extent_item_v0 *ei0;
729 BUG_ON(item_size != sizeof(*ei0));
730 ei0 = btrfs_item_ptr(leaf, path->slots[0],
731 struct btrfs_extent_item_v0);
732 num_refs = btrfs_extent_refs_v0(leaf, ei0);
733 /* FIXME: this isn't correct for data */
734 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
739 BUG_ON(num_refs == 0);
749 delayed_refs = &trans->transaction->delayed_refs;
750 spin_lock(&delayed_refs->lock);
751 head = btrfs_find_delayed_ref_head(trans, bytenr);
753 if (!mutex_trylock(&head->mutex)) {
754 atomic_inc(&head->node.refs);
755 spin_unlock(&delayed_refs->lock);
757 btrfs_release_path(path);
760 * Mutex was contended, block until it's released and try
763 mutex_lock(&head->mutex);
764 mutex_unlock(&head->mutex);
765 btrfs_put_delayed_ref(&head->node);
768 if (head->extent_op && head->extent_op->update_flags)
769 extent_flags |= head->extent_op->flags_to_set;
771 BUG_ON(num_refs == 0);
773 num_refs += head->node.ref_mod;
774 mutex_unlock(&head->mutex);
776 spin_unlock(&delayed_refs->lock);
778 WARN_ON(num_refs == 0);
782 *flags = extent_flags;
784 btrfs_free_path(path);
789 * Back reference rules. Back refs have three main goals:
791 * 1) differentiate between all holders of references to an extent so that
792 * when a reference is dropped we can make sure it was a valid reference
793 * before freeing the extent.
795 * 2) Provide enough information to quickly find the holders of an extent
796 * if we notice a given block is corrupted or bad.
798 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
799 * maintenance. This is actually the same as #2, but with a slightly
800 * different use case.
802 * There are two kinds of back refs. The implicit back refs is optimized
803 * for pointers in non-shared tree blocks. For a given pointer in a block,
804 * back refs of this kind provide information about the block's owner tree
805 * and the pointer's key. These information allow us to find the block by
806 * b-tree searching. The full back refs is for pointers in tree blocks not
807 * referenced by their owner trees. The location of tree block is recorded
808 * in the back refs. Actually the full back refs is generic, and can be
809 * used in all cases the implicit back refs is used. The major shortcoming
810 * of the full back refs is its overhead. Every time a tree block gets
811 * COWed, we have to update back refs entry for all pointers in it.
813 * For a newly allocated tree block, we use implicit back refs for
814 * pointers in it. This means most tree related operations only involve
815 * implicit back refs. For a tree block created in old transaction, the
816 * only way to drop a reference to it is COW it. So we can detect the
817 * event that tree block loses its owner tree's reference and do the
818 * back refs conversion.
820 * When a tree block is COW'd through a tree, there are four cases:
822 * The reference count of the block is one and the tree is the block's
823 * owner tree. Nothing to do in this case.
825 * The reference count of the block is one and the tree is not the
826 * block's owner tree. In this case, full back refs is used for pointers
827 * in the block. Remove these full back refs, add implicit back refs for
828 * every pointers in the new block.
830 * The reference count of the block is greater than one and the tree is
831 * the block's owner tree. In this case, implicit back refs is used for
832 * pointers in the block. Add full back refs for every pointers in the
833 * block, increase lower level extents' reference counts. The original
834 * implicit back refs are entailed to the new block.
836 * The reference count of the block is greater than one and the tree is
837 * not the block's owner tree. Add implicit back refs for every pointer in
838 * the new block, increase lower level extents' reference count.
840 * Back Reference Key composing:
842 * The key objectid corresponds to the first byte in the extent,
843 * The key type is used to differentiate between types of back refs.
844 * There are different meanings of the key offset for different types
847 * File extents can be referenced by:
849 * - multiple snapshots, subvolumes, or different generations in one subvol
850 * - different files inside a single subvolume
851 * - different offsets inside a file (bookend extents in file.c)
853 * The extent ref structure for the implicit back refs has fields for:
855 * - Objectid of the subvolume root
856 * - objectid of the file holding the reference
857 * - original offset in the file
858 * - how many bookend extents
860 * The key offset for the implicit back refs is hash of the first
863 * The extent ref structure for the full back refs has field for:
865 * - number of pointers in the tree leaf
867 * The key offset for the implicit back refs is the first byte of
870 * When a file extent is allocated, The implicit back refs is used.
871 * the fields are filled in:
873 * (root_key.objectid, inode objectid, offset in file, 1)
875 * When a file extent is removed file truncation, we find the
876 * corresponding implicit back refs and check the following fields:
878 * (btrfs_header_owner(leaf), inode objectid, offset in file)
880 * Btree extents can be referenced by:
882 * - Different subvolumes
884 * Both the implicit back refs and the full back refs for tree blocks
885 * only consist of key. The key offset for the implicit back refs is
886 * objectid of block's owner tree. The key offset for the full back refs
887 * is the first byte of parent block.
889 * When implicit back refs is used, information about the lowest key and
890 * level of the tree block are required. These information are stored in
891 * tree block info structure.
894 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
895 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
896 struct btrfs_root *root,
897 struct btrfs_path *path,
898 u64 owner, u32 extra_size)
900 struct btrfs_extent_item *item;
901 struct btrfs_extent_item_v0 *ei0;
902 struct btrfs_extent_ref_v0 *ref0;
903 struct btrfs_tree_block_info *bi;
904 struct extent_buffer *leaf;
905 struct btrfs_key key;
906 struct btrfs_key found_key;
907 u32 new_size = sizeof(*item);
911 leaf = path->nodes[0];
912 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
914 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
915 ei0 = btrfs_item_ptr(leaf, path->slots[0],
916 struct btrfs_extent_item_v0);
917 refs = btrfs_extent_refs_v0(leaf, ei0);
919 if (owner == (u64)-1) {
921 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
922 ret = btrfs_next_leaf(root, path);
926 leaf = path->nodes[0];
928 btrfs_item_key_to_cpu(leaf, &found_key,
930 BUG_ON(key.objectid != found_key.objectid);
931 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
935 ref0 = btrfs_item_ptr(leaf, path->slots[0],
936 struct btrfs_extent_ref_v0);
937 owner = btrfs_ref_objectid_v0(leaf, ref0);
941 btrfs_release_path(path);
943 if (owner < BTRFS_FIRST_FREE_OBJECTID)
944 new_size += sizeof(*bi);
946 new_size -= sizeof(*ei0);
947 ret = btrfs_search_slot(trans, root, &key, path,
948 new_size + extra_size, 1);
953 ret = btrfs_extend_item(trans, root, path, new_size);
956 leaf = path->nodes[0];
957 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
958 btrfs_set_extent_refs(leaf, item, refs);
959 /* FIXME: get real generation */
960 btrfs_set_extent_generation(leaf, item, 0);
961 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
962 btrfs_set_extent_flags(leaf, item,
963 BTRFS_EXTENT_FLAG_TREE_BLOCK |
964 BTRFS_BLOCK_FLAG_FULL_BACKREF);
965 bi = (struct btrfs_tree_block_info *)(item + 1);
966 /* FIXME: get first key of the block */
967 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
968 btrfs_set_tree_block_level(leaf, bi, (int)owner);
970 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
972 btrfs_mark_buffer_dirty(leaf);
977 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
979 u32 high_crc = ~(u32)0;
980 u32 low_crc = ~(u32)0;
983 lenum = cpu_to_le64(root_objectid);
984 high_crc = crc32c(high_crc, &lenum, sizeof(lenum));
985 lenum = cpu_to_le64(owner);
986 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
987 lenum = cpu_to_le64(offset);
988 low_crc = crc32c(low_crc, &lenum, sizeof(lenum));
990 return ((u64)high_crc << 31) ^ (u64)low_crc;
993 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
994 struct btrfs_extent_data_ref *ref)
996 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
997 btrfs_extent_data_ref_objectid(leaf, ref),
998 btrfs_extent_data_ref_offset(leaf, ref));
1001 static int match_extent_data_ref(struct extent_buffer *leaf,
1002 struct btrfs_extent_data_ref *ref,
1003 u64 root_objectid, u64 owner, u64 offset)
1005 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1006 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1007 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1012 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1013 struct btrfs_root *root,
1014 struct btrfs_path *path,
1015 u64 bytenr, u64 parent,
1017 u64 owner, u64 offset)
1019 struct btrfs_key key;
1020 struct btrfs_extent_data_ref *ref;
1021 struct extent_buffer *leaf;
1027 key.objectid = bytenr;
1029 key.type = BTRFS_SHARED_DATA_REF_KEY;
1030 key.offset = parent;
1032 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1033 key.offset = hash_extent_data_ref(root_objectid,
1038 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1047 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1048 key.type = BTRFS_EXTENT_REF_V0_KEY;
1049 btrfs_release_path(path);
1050 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1061 leaf = path->nodes[0];
1062 nritems = btrfs_header_nritems(leaf);
1064 if (path->slots[0] >= nritems) {
1065 ret = btrfs_next_leaf(root, path);
1071 leaf = path->nodes[0];
1072 nritems = btrfs_header_nritems(leaf);
1076 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1077 if (key.objectid != bytenr ||
1078 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1081 ref = btrfs_item_ptr(leaf, path->slots[0],
1082 struct btrfs_extent_data_ref);
1084 if (match_extent_data_ref(leaf, ref, root_objectid,
1087 btrfs_release_path(path);
1099 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1100 struct btrfs_root *root,
1101 struct btrfs_path *path,
1102 u64 bytenr, u64 parent,
1103 u64 root_objectid, u64 owner,
1104 u64 offset, int refs_to_add)
1106 struct btrfs_key key;
1107 struct extent_buffer *leaf;
1112 key.objectid = bytenr;
1114 key.type = BTRFS_SHARED_DATA_REF_KEY;
1115 key.offset = parent;
1116 size = sizeof(struct btrfs_shared_data_ref);
1118 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1119 key.offset = hash_extent_data_ref(root_objectid,
1121 size = sizeof(struct btrfs_extent_data_ref);
1124 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1125 if (ret && ret != -EEXIST)
1128 leaf = path->nodes[0];
1130 struct btrfs_shared_data_ref *ref;
1131 ref = btrfs_item_ptr(leaf, path->slots[0],
1132 struct btrfs_shared_data_ref);
1134 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1136 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1137 num_refs += refs_to_add;
1138 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1141 struct btrfs_extent_data_ref *ref;
1142 while (ret == -EEXIST) {
1143 ref = btrfs_item_ptr(leaf, path->slots[0],
1144 struct btrfs_extent_data_ref);
1145 if (match_extent_data_ref(leaf, ref, root_objectid,
1148 btrfs_release_path(path);
1150 ret = btrfs_insert_empty_item(trans, root, path, &key,
1152 if (ret && ret != -EEXIST)
1155 leaf = path->nodes[0];
1157 ref = btrfs_item_ptr(leaf, path->slots[0],
1158 struct btrfs_extent_data_ref);
1160 btrfs_set_extent_data_ref_root(leaf, ref,
1162 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1163 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1164 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1166 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1167 num_refs += refs_to_add;
1168 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1171 btrfs_mark_buffer_dirty(leaf);
1174 btrfs_release_path(path);
1178 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root,
1180 struct btrfs_path *path,
1183 struct btrfs_key key;
1184 struct btrfs_extent_data_ref *ref1 = NULL;
1185 struct btrfs_shared_data_ref *ref2 = NULL;
1186 struct extent_buffer *leaf;
1190 leaf = path->nodes[0];
1191 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1193 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1194 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1195 struct btrfs_extent_data_ref);
1196 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1197 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1198 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1199 struct btrfs_shared_data_ref);
1200 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1201 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1202 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1203 struct btrfs_extent_ref_v0 *ref0;
1204 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1205 struct btrfs_extent_ref_v0);
1206 num_refs = btrfs_ref_count_v0(leaf, ref0);
1212 BUG_ON(num_refs < refs_to_drop);
1213 num_refs -= refs_to_drop;
1215 if (num_refs == 0) {
1216 ret = btrfs_del_item(trans, root, path);
1218 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1219 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1220 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1221 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1222 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1224 struct btrfs_extent_ref_v0 *ref0;
1225 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1226 struct btrfs_extent_ref_v0);
1227 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1230 btrfs_mark_buffer_dirty(leaf);
1235 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1236 struct btrfs_path *path,
1237 struct btrfs_extent_inline_ref *iref)
1239 struct btrfs_key key;
1240 struct extent_buffer *leaf;
1241 struct btrfs_extent_data_ref *ref1;
1242 struct btrfs_shared_data_ref *ref2;
1245 leaf = path->nodes[0];
1246 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1248 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1249 BTRFS_EXTENT_DATA_REF_KEY) {
1250 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1251 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1253 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1254 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1256 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1257 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1258 struct btrfs_extent_data_ref);
1259 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1260 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1261 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1262 struct btrfs_shared_data_ref);
1263 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1264 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1265 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1266 struct btrfs_extent_ref_v0 *ref0;
1267 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1268 struct btrfs_extent_ref_v0);
1269 num_refs = btrfs_ref_count_v0(leaf, ref0);
1277 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1278 struct btrfs_root *root,
1279 struct btrfs_path *path,
1280 u64 bytenr, u64 parent,
1283 struct btrfs_key key;
1286 key.objectid = bytenr;
1288 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1289 key.offset = parent;
1291 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1292 key.offset = root_objectid;
1295 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1298 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1299 if (ret == -ENOENT && parent) {
1300 btrfs_release_path(path);
1301 key.type = BTRFS_EXTENT_REF_V0_KEY;
1302 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1310 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1311 struct btrfs_root *root,
1312 struct btrfs_path *path,
1313 u64 bytenr, u64 parent,
1316 struct btrfs_key key;
1319 key.objectid = bytenr;
1321 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1322 key.offset = parent;
1324 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1325 key.offset = root_objectid;
1328 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1329 btrfs_release_path(path);
1333 static inline int extent_ref_type(u64 parent, u64 owner)
1336 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1338 type = BTRFS_SHARED_BLOCK_REF_KEY;
1340 type = BTRFS_TREE_BLOCK_REF_KEY;
1343 type = BTRFS_SHARED_DATA_REF_KEY;
1345 type = BTRFS_EXTENT_DATA_REF_KEY;
1350 static int find_next_key(struct btrfs_path *path, int level,
1351 struct btrfs_key *key)
1354 for (; level < BTRFS_MAX_LEVEL; level++) {
1355 if (!path->nodes[level])
1357 if (path->slots[level] + 1 >=
1358 btrfs_header_nritems(path->nodes[level]))
1361 btrfs_item_key_to_cpu(path->nodes[level], key,
1362 path->slots[level] + 1);
1364 btrfs_node_key_to_cpu(path->nodes[level], key,
1365 path->slots[level] + 1);
1372 * look for inline back ref. if back ref is found, *ref_ret is set
1373 * to the address of inline back ref, and 0 is returned.
1375 * if back ref isn't found, *ref_ret is set to the address where it
1376 * should be inserted, and -ENOENT is returned.
1378 * if insert is true and there are too many inline back refs, the path
1379 * points to the extent item, and -EAGAIN is returned.
1381 * NOTE: inline back refs are ordered in the same way that back ref
1382 * items in the tree are ordered.
1384 static noinline_for_stack
1385 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1386 struct btrfs_root *root,
1387 struct btrfs_path *path,
1388 struct btrfs_extent_inline_ref **ref_ret,
1389 u64 bytenr, u64 num_bytes,
1390 u64 parent, u64 root_objectid,
1391 u64 owner, u64 offset, int insert)
1393 struct btrfs_key key;
1394 struct extent_buffer *leaf;
1395 struct btrfs_extent_item *ei;
1396 struct btrfs_extent_inline_ref *iref;
1407 key.objectid = bytenr;
1408 key.type = BTRFS_EXTENT_ITEM_KEY;
1409 key.offset = num_bytes;
1411 want = extent_ref_type(parent, owner);
1413 extra_size = btrfs_extent_inline_ref_size(want);
1414 path->keep_locks = 1;
1417 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1424 leaf = path->nodes[0];
1425 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1426 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1427 if (item_size < sizeof(*ei)) {
1432 ret = convert_extent_item_v0(trans, root, path, owner,
1438 leaf = path->nodes[0];
1439 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1442 BUG_ON(item_size < sizeof(*ei));
1444 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1445 flags = btrfs_extent_flags(leaf, ei);
1447 ptr = (unsigned long)(ei + 1);
1448 end = (unsigned long)ei + item_size;
1450 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK) {
1451 ptr += sizeof(struct btrfs_tree_block_info);
1454 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_DATA));
1463 iref = (struct btrfs_extent_inline_ref *)ptr;
1464 type = btrfs_extent_inline_ref_type(leaf, iref);
1468 ptr += btrfs_extent_inline_ref_size(type);
1472 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1473 struct btrfs_extent_data_ref *dref;
1474 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1475 if (match_extent_data_ref(leaf, dref, root_objectid,
1480 if (hash_extent_data_ref_item(leaf, dref) <
1481 hash_extent_data_ref(root_objectid, owner, offset))
1485 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1487 if (parent == ref_offset) {
1491 if (ref_offset < parent)
1494 if (root_objectid == ref_offset) {
1498 if (ref_offset < root_objectid)
1502 ptr += btrfs_extent_inline_ref_size(type);
1504 if (err == -ENOENT && insert) {
1505 if (item_size + extra_size >=
1506 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1511 * To add new inline back ref, we have to make sure
1512 * there is no corresponding back ref item.
1513 * For simplicity, we just do not add new inline back
1514 * ref if there is any kind of item for this block
1516 if (find_next_key(path, 0, &key) == 0 &&
1517 key.objectid == bytenr &&
1518 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1523 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1526 path->keep_locks = 0;
1527 btrfs_unlock_up_safe(path, 1);
1533 * helper to add new inline back ref
1535 static noinline_for_stack
1536 int setup_inline_extent_backref(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root,
1538 struct btrfs_path *path,
1539 struct btrfs_extent_inline_ref *iref,
1540 u64 parent, u64 root_objectid,
1541 u64 owner, u64 offset, int refs_to_add,
1542 struct btrfs_delayed_extent_op *extent_op)
1544 struct extent_buffer *leaf;
1545 struct btrfs_extent_item *ei;
1548 unsigned long item_offset;
1554 leaf = path->nodes[0];
1555 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1556 item_offset = (unsigned long)iref - (unsigned long)ei;
1558 type = extent_ref_type(parent, owner);
1559 size = btrfs_extent_inline_ref_size(type);
1561 ret = btrfs_extend_item(trans, root, path, size);
1564 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1565 refs = btrfs_extent_refs(leaf, ei);
1566 refs += refs_to_add;
1567 btrfs_set_extent_refs(leaf, ei, refs);
1569 __run_delayed_extent_op(extent_op, leaf, ei);
1571 ptr = (unsigned long)ei + item_offset;
1572 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1573 if (ptr < end - size)
1574 memmove_extent_buffer(leaf, ptr + size, ptr,
1577 iref = (struct btrfs_extent_inline_ref *)ptr;
1578 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1579 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1580 struct btrfs_extent_data_ref *dref;
1581 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1582 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1583 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1584 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1585 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1586 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1587 struct btrfs_shared_data_ref *sref;
1588 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1589 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1590 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1591 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1592 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1594 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1596 btrfs_mark_buffer_dirty(leaf);
1600 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1601 struct btrfs_root *root,
1602 struct btrfs_path *path,
1603 struct btrfs_extent_inline_ref **ref_ret,
1604 u64 bytenr, u64 num_bytes, u64 parent,
1605 u64 root_objectid, u64 owner, u64 offset)
1609 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1610 bytenr, num_bytes, parent,
1611 root_objectid, owner, offset, 0);
1615 btrfs_release_path(path);
1618 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1619 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1622 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1623 root_objectid, owner, offset);
1629 * helper to update/remove inline back ref
1631 static noinline_for_stack
1632 int update_inline_extent_backref(struct btrfs_trans_handle *trans,
1633 struct btrfs_root *root,
1634 struct btrfs_path *path,
1635 struct btrfs_extent_inline_ref *iref,
1637 struct btrfs_delayed_extent_op *extent_op)
1639 struct extent_buffer *leaf;
1640 struct btrfs_extent_item *ei;
1641 struct btrfs_extent_data_ref *dref = NULL;
1642 struct btrfs_shared_data_ref *sref = NULL;
1651 leaf = path->nodes[0];
1652 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1653 refs = btrfs_extent_refs(leaf, ei);
1654 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1655 refs += refs_to_mod;
1656 btrfs_set_extent_refs(leaf, ei, refs);
1658 __run_delayed_extent_op(extent_op, leaf, ei);
1660 type = btrfs_extent_inline_ref_type(leaf, iref);
1662 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1663 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1664 refs = btrfs_extent_data_ref_count(leaf, dref);
1665 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1666 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1667 refs = btrfs_shared_data_ref_count(leaf, sref);
1670 BUG_ON(refs_to_mod != -1);
1673 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1674 refs += refs_to_mod;
1677 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1678 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1680 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1682 size = btrfs_extent_inline_ref_size(type);
1683 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1684 ptr = (unsigned long)iref;
1685 end = (unsigned long)ei + item_size;
1686 if (ptr + size < end)
1687 memmove_extent_buffer(leaf, ptr, ptr + size,
1690 ret = btrfs_truncate_item(trans, root, path, item_size, 1);
1693 btrfs_mark_buffer_dirty(leaf);
1697 static noinline_for_stack
1698 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1699 struct btrfs_root *root,
1700 struct btrfs_path *path,
1701 u64 bytenr, u64 num_bytes, u64 parent,
1702 u64 root_objectid, u64 owner,
1703 u64 offset, int refs_to_add,
1704 struct btrfs_delayed_extent_op *extent_op)
1706 struct btrfs_extent_inline_ref *iref;
1709 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1710 bytenr, num_bytes, parent,
1711 root_objectid, owner, offset, 1);
1713 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1714 ret = update_inline_extent_backref(trans, root, path, iref,
1715 refs_to_add, extent_op);
1716 } else if (ret == -ENOENT) {
1717 ret = setup_inline_extent_backref(trans, root, path, iref,
1718 parent, root_objectid,
1719 owner, offset, refs_to_add,
1725 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1726 struct btrfs_root *root,
1727 struct btrfs_path *path,
1728 u64 bytenr, u64 parent, u64 root_objectid,
1729 u64 owner, u64 offset, int refs_to_add)
1732 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1733 BUG_ON(refs_to_add != 1);
1734 ret = insert_tree_block_ref(trans, root, path, bytenr,
1735 parent, root_objectid);
1737 ret = insert_extent_data_ref(trans, root, path, bytenr,
1738 parent, root_objectid,
1739 owner, offset, refs_to_add);
1744 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1745 struct btrfs_root *root,
1746 struct btrfs_path *path,
1747 struct btrfs_extent_inline_ref *iref,
1748 int refs_to_drop, int is_data)
1752 BUG_ON(!is_data && refs_to_drop != 1);
1754 ret = update_inline_extent_backref(trans, root, path, iref,
1755 -refs_to_drop, NULL);
1756 } else if (is_data) {
1757 ret = remove_extent_data_ref(trans, root, path, refs_to_drop);
1759 ret = btrfs_del_item(trans, root, path);
1764 static int btrfs_issue_discard(struct block_device *bdev,
1767 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
1770 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1771 u64 num_bytes, u64 *actual_bytes)
1774 u64 discarded_bytes = 0;
1775 struct btrfs_multi_bio *multi = NULL;
1778 /* Tell the block device(s) that the sectors can be discarded */
1779 ret = btrfs_map_block(&root->fs_info->mapping_tree, REQ_DISCARD,
1780 bytenr, &num_bytes, &multi, 0);
1782 struct btrfs_bio_stripe *stripe = multi->stripes;
1786 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1787 ret = btrfs_issue_discard(stripe->dev->bdev,
1791 discarded_bytes += stripe->length;
1792 else if (ret != -EOPNOTSUPP)
1797 if (discarded_bytes && ret == -EOPNOTSUPP)
1801 *actual_bytes = discarded_bytes;
1807 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1808 struct btrfs_root *root,
1809 u64 bytenr, u64 num_bytes, u64 parent,
1810 u64 root_objectid, u64 owner, u64 offset)
1813 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1814 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1816 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1817 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
1818 parent, root_objectid, (int)owner,
1819 BTRFS_ADD_DELAYED_REF, NULL);
1821 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
1822 parent, root_objectid, owner, offset,
1823 BTRFS_ADD_DELAYED_REF, NULL);
1828 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1829 struct btrfs_root *root,
1830 u64 bytenr, u64 num_bytes,
1831 u64 parent, u64 root_objectid,
1832 u64 owner, u64 offset, int refs_to_add,
1833 struct btrfs_delayed_extent_op *extent_op)
1835 struct btrfs_path *path;
1836 struct extent_buffer *leaf;
1837 struct btrfs_extent_item *item;
1842 path = btrfs_alloc_path();
1847 path->leave_spinning = 1;
1848 /* this will setup the path even if it fails to insert the back ref */
1849 ret = insert_inline_extent_backref(trans, root->fs_info->extent_root,
1850 path, bytenr, num_bytes, parent,
1851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1856 if (ret != -EAGAIN) {
1861 leaf = path->nodes[0];
1862 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1863 refs = btrfs_extent_refs(leaf, item);
1864 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1866 __run_delayed_extent_op(extent_op, leaf, item);
1868 btrfs_mark_buffer_dirty(leaf);
1869 btrfs_release_path(path);
1872 path->leave_spinning = 1;
1874 /* now insert the actual backref */
1875 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1876 path, bytenr, parent, root_objectid,
1877 owner, offset, refs_to_add);
1880 btrfs_free_path(path);
1884 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *root,
1886 struct btrfs_delayed_ref_node *node,
1887 struct btrfs_delayed_extent_op *extent_op,
1888 int insert_reserved)
1891 struct btrfs_delayed_data_ref *ref;
1892 struct btrfs_key ins;
1897 ins.objectid = node->bytenr;
1898 ins.offset = node->num_bytes;
1899 ins.type = BTRFS_EXTENT_ITEM_KEY;
1901 ref = btrfs_delayed_node_to_data_ref(node);
1902 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1903 parent = ref->parent;
1905 ref_root = ref->root;
1907 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1909 BUG_ON(extent_op->update_key);
1910 flags |= extent_op->flags_to_set;
1912 ret = alloc_reserved_file_extent(trans, root,
1913 parent, ref_root, flags,
1914 ref->objectid, ref->offset,
1915 &ins, node->ref_mod);
1916 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1917 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
1918 node->num_bytes, parent,
1919 ref_root, ref->objectid,
1920 ref->offset, node->ref_mod,
1922 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1923 ret = __btrfs_free_extent(trans, root, node->bytenr,
1924 node->num_bytes, parent,
1925 ref_root, ref->objectid,
1926 ref->offset, node->ref_mod,
1934 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1935 struct extent_buffer *leaf,
1936 struct btrfs_extent_item *ei)
1938 u64 flags = btrfs_extent_flags(leaf, ei);
1939 if (extent_op->update_flags) {
1940 flags |= extent_op->flags_to_set;
1941 btrfs_set_extent_flags(leaf, ei, flags);
1944 if (extent_op->update_key) {
1945 struct btrfs_tree_block_info *bi;
1946 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1947 bi = (struct btrfs_tree_block_info *)(ei + 1);
1948 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1952 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1953 struct btrfs_root *root,
1954 struct btrfs_delayed_ref_node *node,
1955 struct btrfs_delayed_extent_op *extent_op)
1957 struct btrfs_key key;
1958 struct btrfs_path *path;
1959 struct btrfs_extent_item *ei;
1960 struct extent_buffer *leaf;
1965 path = btrfs_alloc_path();
1969 key.objectid = node->bytenr;
1970 key.type = BTRFS_EXTENT_ITEM_KEY;
1971 key.offset = node->num_bytes;
1974 path->leave_spinning = 1;
1975 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
1986 leaf = path->nodes[0];
1987 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1988 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1989 if (item_size < sizeof(*ei)) {
1990 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
1996 leaf = path->nodes[0];
1997 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2000 BUG_ON(item_size < sizeof(*ei));
2001 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2002 __run_delayed_extent_op(extent_op, leaf, ei);
2004 btrfs_mark_buffer_dirty(leaf);
2006 btrfs_free_path(path);
2010 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2011 struct btrfs_root *root,
2012 struct btrfs_delayed_ref_node *node,
2013 struct btrfs_delayed_extent_op *extent_op,
2014 int insert_reserved)
2017 struct btrfs_delayed_tree_ref *ref;
2018 struct btrfs_key ins;
2022 ins.objectid = node->bytenr;
2023 ins.offset = node->num_bytes;
2024 ins.type = BTRFS_EXTENT_ITEM_KEY;
2026 ref = btrfs_delayed_node_to_tree_ref(node);
2027 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2028 parent = ref->parent;
2030 ref_root = ref->root;
2032 BUG_ON(node->ref_mod != 1);
2033 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2034 BUG_ON(!extent_op || !extent_op->update_flags ||
2035 !extent_op->update_key);
2036 ret = alloc_reserved_tree_block(trans, root,
2038 extent_op->flags_to_set,
2041 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2042 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2043 node->num_bytes, parent, ref_root,
2044 ref->level, 0, 1, extent_op);
2045 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2046 ret = __btrfs_free_extent(trans, root, node->bytenr,
2047 node->num_bytes, parent, ref_root,
2048 ref->level, 0, 1, extent_op);
2055 /* helper function to actually process a single delayed ref entry */
2056 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2057 struct btrfs_root *root,
2058 struct btrfs_delayed_ref_node *node,
2059 struct btrfs_delayed_extent_op *extent_op,
2060 int insert_reserved)
2063 if (btrfs_delayed_ref_is_head(node)) {
2064 struct btrfs_delayed_ref_head *head;
2066 * we've hit the end of the chain and we were supposed
2067 * to insert this extent into the tree. But, it got
2068 * deleted before we ever needed to insert it, so all
2069 * we have to do is clean up the accounting
2072 head = btrfs_delayed_node_to_head(node);
2073 if (insert_reserved) {
2074 btrfs_pin_extent(root, node->bytenr,
2075 node->num_bytes, 1);
2076 if (head->is_data) {
2077 ret = btrfs_del_csums(trans, root,
2083 mutex_unlock(&head->mutex);
2087 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2088 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2089 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2091 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2092 node->type == BTRFS_SHARED_DATA_REF_KEY)
2093 ret = run_delayed_data_ref(trans, root, node, extent_op,
2100 static noinline struct btrfs_delayed_ref_node *
2101 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2103 struct rb_node *node;
2104 struct btrfs_delayed_ref_node *ref;
2105 int action = BTRFS_ADD_DELAYED_REF;
2108 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2109 * this prevents ref count from going down to zero when
2110 * there still are pending delayed ref.
2112 node = rb_prev(&head->node.rb_node);
2116 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2118 if (ref->bytenr != head->node.bytenr)
2120 if (ref->action == action)
2122 node = rb_prev(node);
2124 if (action == BTRFS_ADD_DELAYED_REF) {
2125 action = BTRFS_DROP_DELAYED_REF;
2131 static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
2132 struct btrfs_root *root,
2133 struct list_head *cluster)
2135 struct btrfs_delayed_ref_root *delayed_refs;
2136 struct btrfs_delayed_ref_node *ref;
2137 struct btrfs_delayed_ref_head *locked_ref = NULL;
2138 struct btrfs_delayed_extent_op *extent_op;
2141 int must_insert_reserved = 0;
2143 delayed_refs = &trans->transaction->delayed_refs;
2146 /* pick a new head ref from the cluster list */
2147 if (list_empty(cluster))
2150 locked_ref = list_entry(cluster->next,
2151 struct btrfs_delayed_ref_head, cluster);
2153 /* grab the lock that says we are going to process
2154 * all the refs for this head */
2155 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2158 * we may have dropped the spin lock to get the head
2159 * mutex lock, and that might have given someone else
2160 * time to free the head. If that's true, it has been
2161 * removed from our list and we can move on.
2163 if (ret == -EAGAIN) {
2171 * record the must insert reserved flag before we
2172 * drop the spin lock.
2174 must_insert_reserved = locked_ref->must_insert_reserved;
2175 locked_ref->must_insert_reserved = 0;
2177 extent_op = locked_ref->extent_op;
2178 locked_ref->extent_op = NULL;
2181 * locked_ref is the head node, so we have to go one
2182 * node back for any delayed ref updates
2184 ref = select_delayed_ref(locked_ref);
2186 /* All delayed refs have been processed, Go ahead
2187 * and send the head node to run_one_delayed_ref,
2188 * so that any accounting fixes can happen
2190 ref = &locked_ref->node;
2192 if (extent_op && must_insert_reserved) {
2198 spin_unlock(&delayed_refs->lock);
2200 ret = run_delayed_extent_op(trans, root,
2206 spin_lock(&delayed_refs->lock);
2210 list_del_init(&locked_ref->cluster);
2215 rb_erase(&ref->rb_node, &delayed_refs->root);
2216 delayed_refs->num_entries--;
2218 spin_unlock(&delayed_refs->lock);
2220 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2221 must_insert_reserved);
2224 btrfs_put_delayed_ref(ref);
2229 spin_lock(&delayed_refs->lock);
2235 * this starts processing the delayed reference count updates and
2236 * extent insertions we have queued up so far. count can be
2237 * 0, which means to process everything in the tree at the start
2238 * of the run (but not newly added entries), or it can be some target
2239 * number you'd like to process.
2241 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2242 struct btrfs_root *root, unsigned long count)
2244 struct rb_node *node;
2245 struct btrfs_delayed_ref_root *delayed_refs;
2246 struct btrfs_delayed_ref_node *ref;
2247 struct list_head cluster;
2249 int run_all = count == (unsigned long)-1;
2252 if (root == root->fs_info->extent_root)
2253 root = root->fs_info->tree_root;
2255 delayed_refs = &trans->transaction->delayed_refs;
2256 INIT_LIST_HEAD(&cluster);
2258 spin_lock(&delayed_refs->lock);
2260 count = delayed_refs->num_entries * 2;
2264 if (!(run_all || run_most) &&
2265 delayed_refs->num_heads_ready < 64)
2269 * go find something we can process in the rbtree. We start at
2270 * the beginning of the tree, and then build a cluster
2271 * of refs to process starting at the first one we are able to
2274 ret = btrfs_find_ref_cluster(trans, &cluster,
2275 delayed_refs->run_delayed_start);
2279 ret = run_clustered_refs(trans, root, &cluster);
2282 count -= min_t(unsigned long, ret, count);
2289 node = rb_first(&delayed_refs->root);
2292 count = (unsigned long)-1;
2295 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2297 if (btrfs_delayed_ref_is_head(ref)) {
2298 struct btrfs_delayed_ref_head *head;
2300 head = btrfs_delayed_node_to_head(ref);
2301 atomic_inc(&ref->refs);
2303 spin_unlock(&delayed_refs->lock);
2305 * Mutex was contended, block until it's
2306 * released and try again
2308 mutex_lock(&head->mutex);
2309 mutex_unlock(&head->mutex);
2311 btrfs_put_delayed_ref(ref);
2315 node = rb_next(node);
2317 spin_unlock(&delayed_refs->lock);
2318 schedule_timeout(1);
2322 spin_unlock(&delayed_refs->lock);
2326 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 u64 bytenr, u64 num_bytes, u64 flags,
2331 struct btrfs_delayed_extent_op *extent_op;
2334 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2338 extent_op->flags_to_set = flags;
2339 extent_op->update_flags = 1;
2340 extent_op->update_key = 0;
2341 extent_op->is_data = is_data ? 1 : 0;
2343 ret = btrfs_add_delayed_extent_op(trans, bytenr, num_bytes, extent_op);
2349 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2350 struct btrfs_root *root,
2351 struct btrfs_path *path,
2352 u64 objectid, u64 offset, u64 bytenr)
2354 struct btrfs_delayed_ref_head *head;
2355 struct btrfs_delayed_ref_node *ref;
2356 struct btrfs_delayed_data_ref *data_ref;
2357 struct btrfs_delayed_ref_root *delayed_refs;
2358 struct rb_node *node;
2362 delayed_refs = &trans->transaction->delayed_refs;
2363 spin_lock(&delayed_refs->lock);
2364 head = btrfs_find_delayed_ref_head(trans, bytenr);
2368 if (!mutex_trylock(&head->mutex)) {
2369 atomic_inc(&head->node.refs);
2370 spin_unlock(&delayed_refs->lock);
2372 btrfs_release_path(path);
2375 * Mutex was contended, block until it's released and let
2378 mutex_lock(&head->mutex);
2379 mutex_unlock(&head->mutex);
2380 btrfs_put_delayed_ref(&head->node);
2384 node = rb_prev(&head->node.rb_node);
2388 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2390 if (ref->bytenr != bytenr)
2394 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY)
2397 data_ref = btrfs_delayed_node_to_data_ref(ref);
2399 node = rb_prev(node);
2401 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2402 if (ref->bytenr == bytenr)
2406 if (data_ref->root != root->root_key.objectid ||
2407 data_ref->objectid != objectid || data_ref->offset != offset)
2412 mutex_unlock(&head->mutex);
2414 spin_unlock(&delayed_refs->lock);
2418 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2419 struct btrfs_root *root,
2420 struct btrfs_path *path,
2421 u64 objectid, u64 offset, u64 bytenr)
2423 struct btrfs_root *extent_root = root->fs_info->extent_root;
2424 struct extent_buffer *leaf;
2425 struct btrfs_extent_data_ref *ref;
2426 struct btrfs_extent_inline_ref *iref;
2427 struct btrfs_extent_item *ei;
2428 struct btrfs_key key;
2432 key.objectid = bytenr;
2433 key.offset = (u64)-1;
2434 key.type = BTRFS_EXTENT_ITEM_KEY;
2436 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2442 if (path->slots[0] == 0)
2446 leaf = path->nodes[0];
2447 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2449 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2453 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2454 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2455 if (item_size < sizeof(*ei)) {
2456 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2460 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2462 if (item_size != sizeof(*ei) +
2463 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2466 if (btrfs_extent_generation(leaf, ei) <=
2467 btrfs_root_last_snapshot(&root->root_item))
2470 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2471 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2472 BTRFS_EXTENT_DATA_REF_KEY)
2475 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2476 if (btrfs_extent_refs(leaf, ei) !=
2477 btrfs_extent_data_ref_count(leaf, ref) ||
2478 btrfs_extent_data_ref_root(leaf, ref) !=
2479 root->root_key.objectid ||
2480 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2481 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2489 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root,
2491 u64 objectid, u64 offset, u64 bytenr)
2493 struct btrfs_path *path;
2497 path = btrfs_alloc_path();
2502 ret = check_committed_ref(trans, root, path, objectid,
2504 if (ret && ret != -ENOENT)
2507 ret2 = check_delayed_ref(trans, root, path, objectid,
2509 } while (ret2 == -EAGAIN);
2511 if (ret2 && ret2 != -ENOENT) {
2516 if (ret != -ENOENT || ret2 != -ENOENT)
2519 btrfs_free_path(path);
2520 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2526 int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2527 struct extent_buffer *buf, u32 nr_extents)
2529 struct btrfs_key key;
2530 struct btrfs_file_extent_item *fi;
2538 if (!root->ref_cows)
2541 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
2543 root_gen = root->root_key.offset;
2546 root_gen = trans->transid - 1;
2549 level = btrfs_header_level(buf);
2550 nritems = btrfs_header_nritems(buf);
2553 struct btrfs_leaf_ref *ref;
2554 struct btrfs_extent_info *info;
2556 ref = btrfs_alloc_leaf_ref(root, nr_extents);
2562 ref->root_gen = root_gen;
2563 ref->bytenr = buf->start;
2564 ref->owner = btrfs_header_owner(buf);
2565 ref->generation = btrfs_header_generation(buf);
2566 ref->nritems = nr_extents;
2567 info = ref->extents;
2569 for (i = 0; nr_extents > 0 && i < nritems; i++) {
2571 btrfs_item_key_to_cpu(buf, &key, i);
2572 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2574 fi = btrfs_item_ptr(buf, i,
2575 struct btrfs_file_extent_item);
2576 if (btrfs_file_extent_type(buf, fi) ==
2577 BTRFS_FILE_EXTENT_INLINE)
2579 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2580 if (disk_bytenr == 0)
2583 info->bytenr = disk_bytenr;
2585 btrfs_file_extent_disk_num_bytes(buf, fi);
2586 info->objectid = key.objectid;
2587 info->offset = key.offset;
2591 ret = btrfs_add_leaf_ref(root, ref, shared);
2592 if (ret == -EEXIST && shared) {
2593 struct btrfs_leaf_ref *old;
2594 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
2596 btrfs_remove_leaf_ref(root, old);
2597 btrfs_free_leaf_ref(root, old);
2598 ret = btrfs_add_leaf_ref(root, ref, shared);
2601 btrfs_free_leaf_ref(root, ref);
2607 /* when a block goes through cow, we update the reference counts of
2608 * everything that block points to. The internal pointers of the block
2609 * can be in just about any order, and it is likely to have clusters of
2610 * things that are close together and clusters of things that are not.
2612 * To help reduce the seeks that come with updating all of these reference
2613 * counts, sort them by byte number before actual updates are done.
2615 * struct refsort is used to match byte number to slot in the btree block.
2616 * we sort based on the byte number and then use the slot to actually
2619 * struct refsort is smaller than strcut btrfs_item and smaller than
2620 * struct btrfs_key_ptr. Since we're currently limited to the page size
2621 * for a btree block, there's no way for a kmalloc of refsorts for a
2622 * single node to be bigger than a page.
2630 * for passing into sort()
2632 static int refsort_cmp(const void *a_void, const void *b_void)
2634 const struct refsort *a = a_void;
2635 const struct refsort *b = b_void;
2637 if (a->bytenr < b->bytenr)
2639 if (a->bytenr > b->bytenr)
2645 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2646 struct btrfs_root *root,
2647 struct extent_buffer *buf,
2648 int full_backref, int inc)
2655 struct btrfs_key key;
2656 struct btrfs_file_extent_item *fi;
2660 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
2661 u64, u64, u64, u64, u64, u64);
2663 ref_root = btrfs_header_owner(buf);
2664 nritems = btrfs_header_nritems(buf);
2665 level = btrfs_header_level(buf);
2667 if (!root->ref_cows && level == 0)
2671 process_func = btrfs_inc_extent_ref;
2673 process_func = btrfs_free_extent;
2676 parent = buf->start;
2680 for (i = 0; i < nritems; i++) {
2682 btrfs_item_key_to_cpu(buf, &key, i);
2683 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
2685 fi = btrfs_item_ptr(buf, i,
2686 struct btrfs_file_extent_item);
2687 if (btrfs_file_extent_type(buf, fi) ==
2688 BTRFS_FILE_EXTENT_INLINE)
2690 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2694 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2695 key.offset -= btrfs_file_extent_offset(buf, fi);
2696 ret = process_func(trans, root, bytenr, num_bytes,
2697 parent, ref_root, key.objectid,
2702 bytenr = btrfs_node_blockptr(buf, i);
2703 num_bytes = btrfs_level_size(root, level - 1);
2704 ret = process_func(trans, root, bytenr, num_bytes,
2705 parent, ref_root, level - 1, 0);
2716 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2717 struct extent_buffer *buf, int full_backref)
2719 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2722 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2723 struct extent_buffer *buf, int full_backref)
2725 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2728 static int write_one_cache_group(struct btrfs_trans_handle *trans,
2729 struct btrfs_root *root,
2730 struct btrfs_path *path,
2731 struct btrfs_block_group_cache *cache)
2734 struct btrfs_root *extent_root = root->fs_info->extent_root;
2736 struct extent_buffer *leaf;
2738 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
2743 leaf = path->nodes[0];
2744 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
2745 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
2746 btrfs_mark_buffer_dirty(leaf);
2747 btrfs_release_path(path);
2755 static struct btrfs_block_group_cache *
2756 next_block_group(struct btrfs_root *root,
2757 struct btrfs_block_group_cache *cache)
2759 struct rb_node *node;
2760 spin_lock(&root->fs_info->block_group_cache_lock);
2761 node = rb_next(&cache->cache_node);
2762 btrfs_put_block_group(cache);
2764 cache = rb_entry(node, struct btrfs_block_group_cache,
2766 btrfs_get_block_group(cache);
2769 spin_unlock(&root->fs_info->block_group_cache_lock);
2773 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
2774 struct btrfs_trans_handle *trans,
2775 struct btrfs_path *path)
2777 struct btrfs_root *root = block_group->fs_info->tree_root;
2778 struct inode *inode = NULL;
2780 int dcs = BTRFS_DC_ERROR;
2786 * If this block group is smaller than 100 megs don't bother caching the
2789 if (block_group->key.offset < (100 * 1024 * 1024)) {
2790 spin_lock(&block_group->lock);
2791 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
2792 spin_unlock(&block_group->lock);
2797 inode = lookup_free_space_inode(root, block_group, path);
2798 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
2799 ret = PTR_ERR(inode);
2800 btrfs_release_path(path);
2804 if (IS_ERR(inode)) {
2808 if (block_group->ro)
2811 ret = create_free_space_inode(root, trans, block_group, path);
2818 * We want to set the generation to 0, that way if anything goes wrong
2819 * from here on out we know not to trust this cache when we load up next
2822 BTRFS_I(inode)->generation = 0;
2823 ret = btrfs_update_inode(trans, root, inode);
2826 if (i_size_read(inode) > 0) {
2827 ret = btrfs_truncate_free_space_cache(root, trans, path,
2833 spin_lock(&block_group->lock);
2834 if (block_group->cached != BTRFS_CACHE_FINISHED) {
2835 /* We're not cached, don't bother trying to write stuff out */
2836 dcs = BTRFS_DC_WRITTEN;
2837 spin_unlock(&block_group->lock);
2840 spin_unlock(&block_group->lock);
2842 num_pages = (int)div64_u64(block_group->key.offset, 1024 * 1024 * 1024);
2847 * Just to make absolutely sure we have enough space, we're going to
2848 * preallocate 12 pages worth of space for each block group. In
2849 * practice we ought to use at most 8, but we need extra space so we can
2850 * add our header and have a terminator between the extents and the
2854 num_pages *= PAGE_CACHE_SIZE;
2856 ret = btrfs_check_data_free_space(inode, num_pages);
2860 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
2861 num_pages, num_pages,
2864 dcs = BTRFS_DC_SETUP;
2865 btrfs_free_reserved_data_space(inode, num_pages);
2869 btrfs_release_path(path);
2871 spin_lock(&block_group->lock);
2872 block_group->disk_cache_state = dcs;
2873 spin_unlock(&block_group->lock);
2878 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root)
2881 struct btrfs_block_group_cache *cache;
2883 struct btrfs_path *path;
2886 path = btrfs_alloc_path();
2892 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2894 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
2896 cache = next_block_group(root, cache);
2904 err = cache_save_setup(cache, trans, path);
2905 last = cache->key.objectid + cache->key.offset;
2906 btrfs_put_block_group(cache);
2911 err = btrfs_run_delayed_refs(trans, root,
2916 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2918 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
2919 btrfs_put_block_group(cache);
2925 cache = next_block_group(root, cache);
2934 if (cache->disk_cache_state == BTRFS_DC_SETUP)
2935 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
2937 last = cache->key.objectid + cache->key.offset;
2939 err = write_one_cache_group(trans, root, path, cache);
2941 btrfs_put_block_group(cache);
2946 * I don't think this is needed since we're just marking our
2947 * preallocated extent as written, but just in case it can't
2951 err = btrfs_run_delayed_refs(trans, root,
2956 cache = btrfs_lookup_first_block_group(root->fs_info, last);
2959 * Really this shouldn't happen, but it could if we
2960 * couldn't write the entire preallocated extent and
2961 * splitting the extent resulted in a new block.
2964 btrfs_put_block_group(cache);
2967 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2969 cache = next_block_group(root, cache);
2978 btrfs_write_out_cache(root, trans, cache, path);
2981 * If we didn't have an error then the cache state is still
2982 * NEED_WRITE, so we can set it to WRITTEN.
2984 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
2985 cache->disk_cache_state = BTRFS_DC_WRITTEN;
2986 last = cache->key.objectid + cache->key.offset;
2987 btrfs_put_block_group(cache);
2990 btrfs_free_path(path);
2994 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
2996 struct btrfs_block_group_cache *block_group;
2999 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3000 if (!block_group || block_group->ro)
3003 btrfs_put_block_group(block_group);
3007 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3008 u64 total_bytes, u64 bytes_used,
3009 struct btrfs_space_info **space_info)
3011 struct btrfs_space_info *found;
3015 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3016 BTRFS_BLOCK_GROUP_RAID10))
3021 found = __find_space_info(info, flags);
3023 spin_lock(&found->lock);
3024 found->total_bytes += total_bytes;
3025 found->disk_total += total_bytes * factor;
3026 found->bytes_used += bytes_used;
3027 found->disk_used += bytes_used * factor;
3029 spin_unlock(&found->lock);
3030 *space_info = found;
3033 found = kzalloc(sizeof(*found), GFP_NOFS);
3037 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3038 INIT_LIST_HEAD(&found->block_groups[i]);
3039 init_rwsem(&found->groups_sem);
3040 spin_lock_init(&found->lock);
3041 found->flags = flags & (BTRFS_BLOCK_GROUP_DATA |
3042 BTRFS_BLOCK_GROUP_SYSTEM |
3043 BTRFS_BLOCK_GROUP_METADATA);
3044 found->total_bytes = total_bytes;
3045 found->disk_total = total_bytes * factor;
3046 found->bytes_used = bytes_used;
3047 found->disk_used = bytes_used * factor;
3048 found->bytes_pinned = 0;
3049 found->bytes_reserved = 0;
3050 found->bytes_readonly = 0;
3051 found->bytes_may_use = 0;
3053 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
3054 found->chunk_alloc = 0;
3055 *space_info = found;
3056 list_add_rcu(&found->list, &info->space_info);
3057 atomic_set(&found->caching_threads, 0);
3061 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3063 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
3064 BTRFS_BLOCK_GROUP_RAID1 |
3065 BTRFS_BLOCK_GROUP_RAID10 |
3066 BTRFS_BLOCK_GROUP_DUP);
3068 if (flags & BTRFS_BLOCK_GROUP_DATA)
3069 fs_info->avail_data_alloc_bits |= extra_flags;
3070 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3071 fs_info->avail_metadata_alloc_bits |= extra_flags;
3072 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3073 fs_info->avail_system_alloc_bits |= extra_flags;
3077 u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3080 * we add in the count of missing devices because we want
3081 * to make sure that any RAID levels on a degraded FS
3082 * continue to be honored.
3084 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3085 root->fs_info->fs_devices->missing_devices;
3087 if (num_devices == 1)
3088 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
3089 if (num_devices < 4)
3090 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3092 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
3093 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3094 BTRFS_BLOCK_GROUP_RAID10))) {
3095 flags &= ~BTRFS_BLOCK_GROUP_DUP;
3098 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
3099 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
3100 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
3103 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
3104 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
3105 (flags & BTRFS_BLOCK_GROUP_RAID10) |
3106 (flags & BTRFS_BLOCK_GROUP_DUP)))
3107 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
3111 static u64 get_alloc_profile(struct btrfs_root *root, u64 flags)
3113 if (flags & BTRFS_BLOCK_GROUP_DATA)
3114 flags |= root->fs_info->avail_data_alloc_bits &
3115 root->fs_info->data_alloc_profile;
3116 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3117 flags |= root->fs_info->avail_system_alloc_bits &
3118 root->fs_info->system_alloc_profile;
3119 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3120 flags |= root->fs_info->avail_metadata_alloc_bits &
3121 root->fs_info->metadata_alloc_profile;
3122 return btrfs_reduce_alloc_profile(root, flags);
3125 u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3130 flags = BTRFS_BLOCK_GROUP_DATA;
3131 else if (root == root->fs_info->chunk_root)
3132 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3134 flags = BTRFS_BLOCK_GROUP_METADATA;
3136 return get_alloc_profile(root, flags);
3139 void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
3141 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
3142 BTRFS_BLOCK_GROUP_DATA);
3146 * This will check the space that the inode allocates from to make sure we have
3147 * enough space for bytes.
3149 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3151 struct btrfs_space_info *data_sinfo;
3152 struct btrfs_root *root = BTRFS_I(inode)->root;
3154 int ret = 0, committed = 0, alloc_chunk = 1;
3156 /* make sure bytes are sectorsize aligned */
3157 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3159 if (root == root->fs_info->tree_root) {
3164 data_sinfo = BTRFS_I(inode)->space_info;
3169 /* make sure we have enough space to handle the data first */
3170 spin_lock(&data_sinfo->lock);
3171 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3172 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3173 data_sinfo->bytes_may_use;
3175 if (used + bytes > data_sinfo->total_bytes) {
3176 struct btrfs_trans_handle *trans;
3179 * if we don't have enough free bytes in this space then we need
3180 * to alloc a new chunk.
3182 if (!data_sinfo->full && alloc_chunk) {
3185 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
3186 spin_unlock(&data_sinfo->lock);
3188 alloc_target = btrfs_get_alloc_profile(root, 1);
3189 trans = btrfs_join_transaction(root, 1);
3191 return PTR_ERR(trans);
3193 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3194 bytes + 2 * 1024 * 1024,
3196 CHUNK_ALLOC_NO_FORCE);
3197 btrfs_end_transaction(trans, root);
3206 btrfs_set_inode_space_info(root, inode);
3207 data_sinfo = BTRFS_I(inode)->space_info;
3211 spin_unlock(&data_sinfo->lock);
3213 /* commit the current transaction and try again */
3215 if (!committed && !root->fs_info->open_ioctl_trans) {
3217 trans = btrfs_join_transaction(root, 1);
3219 return PTR_ERR(trans);
3220 ret = btrfs_commit_transaction(trans, root);
3226 #if 0 /* I hope we never need this code again, just in case */
3227 printk(KERN_ERR "no space left, need %llu, %llu bytes_used, "
3228 "%llu bytes_reserved, " "%llu bytes_pinned, "
3229 "%llu bytes_readonly, %llu may use %llu total\n",
3230 (unsigned long long)bytes,
3231 (unsigned long long)data_sinfo->bytes_used,
3232 (unsigned long long)data_sinfo->bytes_reserved,
3233 (unsigned long long)data_sinfo->bytes_pinned,
3234 (unsigned long long)data_sinfo->bytes_readonly,
3235 (unsigned long long)data_sinfo->bytes_may_use,
3236 (unsigned long long)data_sinfo->total_bytes);
3240 data_sinfo->bytes_may_use += bytes;
3241 BTRFS_I(inode)->reserved_bytes += bytes;
3242 spin_unlock(&data_sinfo->lock);
3248 * called when we are clearing an delalloc extent from the
3249 * inode's io_tree or there was an error for whatever reason
3250 * after calling btrfs_check_data_free_space
3252 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3254 struct btrfs_root *root = BTRFS_I(inode)->root;
3255 struct btrfs_space_info *data_sinfo;
3257 /* make sure bytes are sectorsize aligned */
3258 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
3260 data_sinfo = BTRFS_I(inode)->space_info;
3261 spin_lock(&data_sinfo->lock);
3262 data_sinfo->bytes_may_use -= bytes;
3263 BTRFS_I(inode)->reserved_bytes -= bytes;
3264 spin_unlock(&data_sinfo->lock);
3267 static void force_metadata_allocation(struct btrfs_fs_info *info)
3269 struct list_head *head = &info->space_info;
3270 struct btrfs_space_info *found;
3273 list_for_each_entry_rcu(found, head, list) {
3274 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3275 found->force_alloc = CHUNK_ALLOC_FORCE;
3280 static int should_alloc_chunk(struct btrfs_root *root,
3281 struct btrfs_space_info *sinfo, u64 alloc_bytes,
3284 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3285 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3288 if (force == CHUNK_ALLOC_FORCE)
3292 * in limited mode, we want to have some free space up to
3293 * about 1% of the FS size.
3295 if (force == CHUNK_ALLOC_LIMITED) {
3296 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3297 thresh = max_t(u64, 64 * 1024 * 1024,
3298 div_factor_fine(thresh, 1));
3300 if (num_bytes - num_allocated < thresh)
3305 * we have two similar checks here, one based on percentage
3306 * and once based on a hard number of 256MB. The idea
3307 * is that if we have a good amount of free
3308 * room, don't allocate a chunk. A good mount is
3309 * less than 80% utilized of the chunks we have allocated,
3310 * or more than 256MB free
3312 if (num_allocated + alloc_bytes + 256 * 1024 * 1024 < num_bytes)
3315 if (num_allocated + alloc_bytes < div_factor(num_bytes, 8))
3318 thresh = btrfs_super_total_bytes(&root->fs_info->super_copy);
3320 /* 256MB or 5% of the FS */
3321 thresh = max_t(u64, 256 * 1024 * 1024, div_factor_fine(thresh, 5));
3323 if (num_bytes > thresh && sinfo->bytes_used < div_factor(num_bytes, 3))
3328 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3329 struct btrfs_root *extent_root, u64 alloc_bytes,
3330 u64 flags, int force)
3332 struct btrfs_space_info *space_info;
3333 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3334 int wait_for_alloc = 0;
3337 flags = btrfs_reduce_alloc_profile(extent_root, flags);
3339 space_info = __find_space_info(extent_root->fs_info, flags);
3341 ret = update_space_info(extent_root->fs_info, flags,
3345 BUG_ON(!space_info);
3348 spin_lock(&space_info->lock);
3349 if (space_info->force_alloc)
3350 force = space_info->force_alloc;
3351 if (space_info->full) {
3352 spin_unlock(&space_info->lock);
3356 if (!should_alloc_chunk(extent_root, space_info, alloc_bytes, force)) {
3357 spin_unlock(&space_info->lock);
3359 } else if (space_info->chunk_alloc) {
3362 space_info->chunk_alloc = 1;
3365 spin_unlock(&space_info->lock);
3367 mutex_lock(&fs_info->chunk_mutex);
3370 * The chunk_mutex is held throughout the entirety of a chunk
3371 * allocation, so once we've acquired the chunk_mutex we know that the
3372 * other guy is done and we need to recheck and see if we should
3375 if (wait_for_alloc) {
3376 mutex_unlock(&fs_info->chunk_mutex);
3382 * If we have mixed data/metadata chunks we want to make sure we keep
3383 * allocating mixed chunks instead of individual chunks.
3385 if (btrfs_mixed_space_info(space_info))
3386 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3389 * if we're doing a data chunk, go ahead and make sure that
3390 * we keep a reasonable number of metadata chunks allocated in the
3393 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3394 fs_info->data_chunk_allocations++;
3395 if (!(fs_info->data_chunk_allocations %
3396 fs_info->metadata_ratio))
3397 force_metadata_allocation(fs_info);
3400 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3401 spin_lock(&space_info->lock);
3403 space_info->full = 1;
3407 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3408 space_info->chunk_alloc = 0;
3409 spin_unlock(&space_info->lock);
3410 mutex_unlock(&extent_root->fs_info->chunk_mutex);
3415 * shrink metadata reservation for delalloc
3417 static int shrink_delalloc(struct btrfs_trans_handle *trans,
3418 struct btrfs_root *root, u64 to_reclaim, int sync)
3420 struct btrfs_block_rsv *block_rsv;
3421 struct btrfs_space_info *space_info;
3426 int nr_pages = (2 * 1024 * 1024) >> PAGE_CACHE_SHIFT;
3428 unsigned long progress;
3430 block_rsv = &root->fs_info->delalloc_block_rsv;
3431 space_info = block_rsv->space_info;
3434 reserved = space_info->bytes_reserved;
3435 progress = space_info->reservation_progress;
3440 max_reclaim = min(reserved, to_reclaim);
3442 while (loops < 1024) {
3443 /* have the flusher threads jump in and do some IO */
3445 nr_pages = min_t(unsigned long, nr_pages,
3446 root->fs_info->delalloc_bytes >> PAGE_CACHE_SHIFT);
3447 writeback_inodes_sb_nr_if_idle(root->fs_info->sb, nr_pages);
3449 spin_lock(&space_info->lock);
3450 if (reserved > space_info->bytes_reserved)
3451 reclaimed += reserved - space_info->bytes_reserved;
3452 reserved = space_info->bytes_reserved;
3453 spin_unlock(&space_info->lock);
3457 if (reserved == 0 || reclaimed >= max_reclaim)
3460 if (trans && trans->transaction->blocked)
3463 time_left = schedule_timeout_interruptible(1);
3465 /* We were interrupted, exit */
3469 /* we've kicked the IO a few times, if anything has been freed,
3470 * exit. There is no sense in looping here for a long time
3471 * when we really need to commit the transaction, or there are
3472 * just too many writers without enough free space
3477 if (progress != space_info->reservation_progress)
3482 return reclaimed >= to_reclaim;
3486 * Retries tells us how many times we've called reserve_metadata_bytes. The
3487 * idea is if this is the first call (retries == 0) then we will add to our
3488 * reserved count if we can't make the allocation in order to hold our place
3489 * while we go and try and free up space. That way for retries > 1 we don't try
3490 * and add space, we just check to see if the amount of unused space is >= the
3491 * total space, meaning that our reservation is valid.
3493 * However if we don't intend to retry this reservation, pass -1 as retries so
3494 * that it short circuits this logic.
3496 static int reserve_metadata_bytes(struct btrfs_trans_handle *trans,
3497 struct btrfs_root *root,
3498 struct btrfs_block_rsv *block_rsv,
3499 u64 orig_bytes, int flush)
3501 struct btrfs_space_info *space_info = block_rsv->space_info;
3503 u64 num_bytes = orig_bytes;
3506 bool reserved = false;
3507 bool committed = false;
3514 spin_lock(&space_info->lock);
3515 unused = space_info->bytes_used + space_info->bytes_reserved +
3516 space_info->bytes_pinned + space_info->bytes_readonly +
3517 space_info->bytes_may_use;
3520 * The idea here is that we've not already over-reserved the block group
3521 * then we can go ahead and save our reservation first and then start
3522 * flushing if we need to. Otherwise if we've already overcommitted
3523 * lets start flushing stuff first and then come back and try to make
3526 if (unused <= space_info->total_bytes) {
3527 unused = space_info->total_bytes - unused;
3528 if (unused >= num_bytes) {
3530 space_info->bytes_reserved += orig_bytes;
3534 * Ok set num_bytes to orig_bytes since we aren't
3535 * overocmmitted, this way we only try and reclaim what
3538 num_bytes = orig_bytes;
3542 * Ok we're over committed, set num_bytes to the overcommitted
3543 * amount plus the amount of bytes that we need for this
3546 num_bytes = unused - space_info->total_bytes +
3547 (orig_bytes * (retries + 1));
3551 * Couldn't make our reservation, save our place so while we're trying
3552 * to reclaim space we can actually use it instead of somebody else
3553 * stealing it from us.
3555 if (ret && !reserved) {
3556 space_info->bytes_reserved += orig_bytes;
3560 spin_unlock(&space_info->lock);
3569 * We do synchronous shrinking since we don't actually unreserve
3570 * metadata until after the IO is completed.
3572 ret = shrink_delalloc(trans, root, num_bytes, 1);
3579 * So if we were overcommitted it's possible that somebody else flushed
3580 * out enough space and we simply didn't have enough space to reclaim,
3581 * so go back around and try again.
3588 spin_lock(&space_info->lock);
3590 * Not enough space to be reclaimed, don't bother committing the
3593 if (space_info->bytes_pinned < orig_bytes)
3595 spin_unlock(&space_info->lock);
3600 if (trans || committed)
3604 trans = btrfs_join_transaction(root, 1);
3607 ret = btrfs_commit_transaction(trans, root);
3616 spin_lock(&space_info->lock);
3617 space_info->bytes_reserved -= orig_bytes;
3618 spin_unlock(&space_info->lock);
3624 static struct btrfs_block_rsv *get_block_rsv(struct btrfs_trans_handle *trans,
3625 struct btrfs_root *root)
3627 struct btrfs_block_rsv *block_rsv;
3629 block_rsv = trans->block_rsv;
3631 block_rsv = root->block_rsv;
3634 block_rsv = &root->fs_info->empty_block_rsv;
3639 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
3643 spin_lock(&block_rsv->lock);
3644 if (block_rsv->reserved >= num_bytes) {
3645 block_rsv->reserved -= num_bytes;
3646 if (block_rsv->reserved < block_rsv->size)
3647 block_rsv->full = 0;
3650 spin_unlock(&block_rsv->lock);
3654 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
3655 u64 num_bytes, int update_size)
3657 spin_lock(&block_rsv->lock);
3658 block_rsv->reserved += num_bytes;
3660 block_rsv->size += num_bytes;
3661 else if (block_rsv->reserved >= block_rsv->size)
3662 block_rsv->full = 1;
3663 spin_unlock(&block_rsv->lock);
3666 static void block_rsv_release_bytes(struct btrfs_block_rsv *block_rsv,
3667 struct btrfs_block_rsv *dest, u64 num_bytes)
3669 struct btrfs_space_info *space_info = block_rsv->space_info;
3671 spin_lock(&block_rsv->lock);
3672 if (num_bytes == (u64)-1)
3673 num_bytes = block_rsv->size;
3674 block_rsv->size -= num_bytes;
3675 if (block_rsv->reserved >= block_rsv->size) {
3676 num_bytes = block_rsv->reserved - block_rsv->size;
3677 block_rsv->reserved = block_rsv->size;
3678 block_rsv->full = 1;
3682 spin_unlock(&block_rsv->lock);
3684 if (num_bytes > 0) {
3686 spin_lock(&dest->lock);
3690 bytes_to_add = dest->size - dest->reserved;
3691 bytes_to_add = min(num_bytes, bytes_to_add);
3692 dest->reserved += bytes_to_add;
3693 if (dest->reserved >= dest->size)
3695 num_bytes -= bytes_to_add;
3697 spin_unlock(&dest->lock);
3700 spin_lock(&space_info->lock);
3701 space_info->bytes_reserved -= num_bytes;
3702 space_info->reservation_progress++;
3703 spin_unlock(&space_info->lock);
3708 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
3709 struct btrfs_block_rsv *dst, u64 num_bytes)
3713 ret = block_rsv_use_bytes(src, num_bytes);
3717 block_rsv_add_bytes(dst, num_bytes, 1);
3721 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv)
3723 memset(rsv, 0, sizeof(*rsv));
3724 spin_lock_init(&rsv->lock);
3725 atomic_set(&rsv->usage, 1);
3727 INIT_LIST_HEAD(&rsv->list);
3730 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root)
3732 struct btrfs_block_rsv *block_rsv;
3733 struct btrfs_fs_info *fs_info = root->fs_info;
3735 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
3739 btrfs_init_block_rsv(block_rsv);
3740 block_rsv->space_info = __find_space_info(fs_info,
3741 BTRFS_BLOCK_GROUP_METADATA);
3745 void btrfs_free_block_rsv(struct btrfs_root *root,
3746 struct btrfs_block_rsv *rsv)
3748 if (rsv && atomic_dec_and_test(&rsv->usage)) {
3749 btrfs_block_rsv_release(root, rsv, (u64)-1);
3756 * make the block_rsv struct be able to capture freed space.
3757 * the captured space will re-add to the the block_rsv struct
3758 * after transaction commit
3760 void btrfs_add_durable_block_rsv(struct btrfs_fs_info *fs_info,
3761 struct btrfs_block_rsv *block_rsv)
3763 block_rsv->durable = 1;
3764 mutex_lock(&fs_info->durable_block_rsv_mutex);
3765 list_add_tail(&block_rsv->list, &fs_info->durable_block_rsv_list);
3766 mutex_unlock(&fs_info->durable_block_rsv_mutex);
3769 int btrfs_block_rsv_add(struct btrfs_trans_handle *trans,
3770 struct btrfs_root *root,
3771 struct btrfs_block_rsv *block_rsv,
3779 ret = reserve_metadata_bytes(trans, root, block_rsv, num_bytes, 1);
3781 block_rsv_add_bytes(block_rsv, num_bytes, 1);
3788 int btrfs_block_rsv_check(struct btrfs_trans_handle *trans,
3789 struct btrfs_root *root,
3790 struct btrfs_block_rsv *block_rsv,
3791 u64 min_reserved, int min_factor)
3794 int commit_trans = 0;
3800 spin_lock(&block_rsv->lock);
3802 num_bytes = div_factor(block_rsv->size, min_factor);
3803 if (min_reserved > num_bytes)
3804 num_bytes = min_reserved;
3806 if (block_rsv->reserved >= num_bytes) {
3809 num_bytes -= block_rsv->reserved;
3810 if (block_rsv->durable &&
3811 block_rsv->freed[0] + block_rsv->freed[1] >= num_bytes)
3814 spin_unlock(&block_rsv->lock);
3818 if (block_rsv->refill_used) {
3819 ret = reserve_metadata_bytes(trans, root, block_rsv,
3822 block_rsv_add_bytes(block_rsv, num_bytes, 0);
3831 trans = btrfs_join_transaction(root, 1);
3832 BUG_ON(IS_ERR(trans));
3833 ret = btrfs_commit_transaction(trans, root);
3840 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
3841 struct btrfs_block_rsv *dst_rsv,
3844 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
3847 void btrfs_block_rsv_release(struct btrfs_root *root,
3848 struct btrfs_block_rsv *block_rsv,
3851 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3852 if (global_rsv->full || global_rsv == block_rsv ||
3853 block_rsv->space_info != global_rsv->space_info)
3855 block_rsv_release_bytes(block_rsv, global_rsv, num_bytes);
3859 * helper to calculate size of global block reservation.
3860 * the desired value is sum of space used by extent tree,
3861 * checksum tree and root tree
3863 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
3865 struct btrfs_space_info *sinfo;
3869 int csum_size = btrfs_super_csum_size(&fs_info->super_copy);
3872 * per tree used space accounting can be inaccuracy, so we
3875 spin_lock(&fs_info->extent_root->accounting_lock);
3876 num_bytes = btrfs_root_used(&fs_info->extent_root->root_item);
3877 spin_unlock(&fs_info->extent_root->accounting_lock);
3879 spin_lock(&fs_info->csum_root->accounting_lock);
3880 num_bytes += btrfs_root_used(&fs_info->csum_root->root_item);
3881 spin_unlock(&fs_info->csum_root->accounting_lock);
3883 spin_lock(&fs_info->tree_root->accounting_lock);
3884 num_bytes += btrfs_root_used(&fs_info->tree_root->root_item);
3885 spin_unlock(&fs_info->tree_root->accounting_lock);
3887 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
3888 spin_lock(&sinfo->lock);
3889 data_used = sinfo->bytes_used;
3890 spin_unlock(&sinfo->lock);
3892 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3893 spin_lock(&sinfo->lock);
3894 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
3896 meta_used = sinfo->bytes_used;
3897 spin_unlock(&sinfo->lock);
3899 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
3901 num_bytes += div64_u64(data_used + meta_used, 50);
3903 if (num_bytes * 3 > meta_used)
3904 num_bytes = div64_u64(meta_used, 3);
3906 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
3909 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
3911 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
3912 struct btrfs_space_info *sinfo = block_rsv->space_info;
3915 num_bytes = calc_global_metadata_size(fs_info);
3917 spin_lock(&block_rsv->lock);
3918 spin_lock(&sinfo->lock);
3920 block_rsv->size = num_bytes;
3922 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
3923 sinfo->bytes_reserved + sinfo->bytes_readonly +
3924 sinfo->bytes_may_use;
3926 if (sinfo->total_bytes > num_bytes) {
3927 num_bytes = sinfo->total_bytes - num_bytes;
3928 block_rsv->reserved += num_bytes;
3929 sinfo->bytes_reserved += num_bytes;
3932 if (block_rsv->reserved >= block_rsv->size) {
3933 num_bytes = block_rsv->reserved - block_rsv->size;
3934 sinfo->bytes_reserved -= num_bytes;
3935 sinfo->reservation_progress++;
3936 block_rsv->reserved = block_rsv->size;
3937 block_rsv->full = 1;
3940 printk(KERN_INFO"global block rsv size %llu reserved %llu\n",
3941 block_rsv->size, block_rsv->reserved);
3943 spin_unlock(&sinfo->lock);
3944 spin_unlock(&block_rsv->lock);
3947 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
3949 struct btrfs_space_info *space_info;
3951 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3952 fs_info->chunk_block_rsv.space_info = space_info;
3953 fs_info->chunk_block_rsv.priority = 10;
3955 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
3956 fs_info->global_block_rsv.space_info = space_info;
3957 fs_info->global_block_rsv.priority = 10;
3958 fs_info->global_block_rsv.refill_used = 1;
3959 fs_info->delalloc_block_rsv.space_info = space_info;
3960 fs_info->trans_block_rsv.space_info = space_info;
3961 fs_info->empty_block_rsv.space_info = space_info;
3962 fs_info->empty_block_rsv.priority = 10;
3964 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
3965 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
3966 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
3967 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
3968 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
3970 btrfs_add_durable_block_rsv(fs_info, &fs_info->global_block_rsv);
3972 btrfs_add_durable_block_rsv(fs_info, &fs_info->delalloc_block_rsv);
3974 update_global_block_rsv(fs_info);
3977 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
3979 block_rsv_release_bytes(&fs_info->global_block_rsv, NULL, (u64)-1);
3980 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
3981 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
3982 WARN_ON(fs_info->trans_block_rsv.size > 0);
3983 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
3984 WARN_ON(fs_info->chunk_block_rsv.size > 0);
3985 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
3988 static u64 calc_trans_metadata_size(struct btrfs_root *root, int num_items)
3990 return (root->leafsize + root->nodesize * (BTRFS_MAX_LEVEL - 1)) *
3994 int btrfs_trans_reserve_metadata(struct btrfs_trans_handle *trans,
3995 struct btrfs_root *root,
4001 if (num_items == 0 || root->fs_info->chunk_root == root)
4004 num_bytes = calc_trans_metadata_size(root, num_items);
4005 ret = btrfs_block_rsv_add(trans, root, &root->fs_info->trans_block_rsv,
4008 trans->bytes_reserved += num_bytes;
4009 trans->block_rsv = &root->fs_info->trans_block_rsv;
4014 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4015 struct btrfs_root *root)
4017 if (!trans->bytes_reserved)
4020 BUG_ON(trans->block_rsv != &root->fs_info->trans_block_rsv);
4021 btrfs_block_rsv_release(root, trans->block_rsv,
4022 trans->bytes_reserved);
4023 trans->bytes_reserved = 0;
4026 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4027 struct inode *inode)
4029 struct btrfs_root *root = BTRFS_I(inode)->root;
4030 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4031 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4034 * one for deleting orphan item, one for updating inode and
4035 * two for calling btrfs_truncate_inode_items.
4037 * btrfs_truncate_inode_items is a delete operation, it frees
4038 * more space than it uses in most cases. So two units of
4039 * metadata space should be enough for calling it many times.
4040 * If all of the metadata space is used, we can commit
4041 * transaction and use space it freed.
4043 u64 num_bytes = calc_trans_metadata_size(root, 4);
4044 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4047 void btrfs_orphan_release_metadata(struct inode *inode)
4049 struct btrfs_root *root = BTRFS_I(inode)->root;
4050 u64 num_bytes = calc_trans_metadata_size(root, 4);
4051 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4054 int btrfs_snap_reserve_metadata(struct btrfs_trans_handle *trans,
4055 struct btrfs_pending_snapshot *pending)
4057 struct btrfs_root *root = pending->root;
4058 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4059 struct btrfs_block_rsv *dst_rsv = &pending->block_rsv;
4061 * two for root back/forward refs, two for directory entries
4062 * and one for root of the snapshot.
4064 u64 num_bytes = calc_trans_metadata_size(root, 5);
4065 dst_rsv->space_info = src_rsv->space_info;
4066 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4069 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes)
4071 return num_bytes >>= 3;
4074 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
4076 struct btrfs_root *root = BTRFS_I(inode)->root;
4077 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
4080 int reserved_extents;
4083 if (btrfs_transaction_in_commit(root->fs_info))
4084 schedule_timeout(1);
4086 num_bytes = ALIGN(num_bytes, root->sectorsize);
4088 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents) + 1;
4089 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4091 if (nr_extents > reserved_extents) {
4092 nr_extents -= reserved_extents;
4093 to_reserve = calc_trans_metadata_size(root, nr_extents);
4099 to_reserve += calc_csum_metadata_size(inode, num_bytes);
4100 ret = reserve_metadata_bytes(NULL, root, block_rsv, to_reserve, 1);
4104 atomic_add(nr_extents, &BTRFS_I(inode)->reserved_extents);
4105 atomic_inc(&BTRFS_I(inode)->outstanding_extents);
4107 block_rsv_add_bytes(block_rsv, to_reserve, 1);
4109 if (block_rsv->size > 512 * 1024 * 1024)
4110 shrink_delalloc(NULL, root, to_reserve, 0);
4115 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
4117 struct btrfs_root *root = BTRFS_I(inode)->root;
4120 int reserved_extents;
4122 num_bytes = ALIGN(num_bytes, root->sectorsize);
4123 atomic_dec(&BTRFS_I(inode)->outstanding_extents);
4124 WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents) < 0);
4126 reserved_extents = atomic_read(&BTRFS_I(inode)->reserved_extents);
4130 nr_extents = atomic_read(&BTRFS_I(inode)->outstanding_extents);
4131 if (nr_extents >= reserved_extents) {
4135 old = reserved_extents;
4136 nr_extents = reserved_extents - nr_extents;
4137 new = reserved_extents - nr_extents;
4138 old = atomic_cmpxchg(&BTRFS_I(inode)->reserved_extents,
4139 reserved_extents, new);
4140 if (likely(old == reserved_extents))
4142 reserved_extents = old;
4145 to_free = calc_csum_metadata_size(inode, num_bytes);
4147 to_free += calc_trans_metadata_size(root, nr_extents);
4149 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
4153 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
4157 ret = btrfs_check_data_free_space(inode, num_bytes);
4161 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
4163 btrfs_free_reserved_data_space(inode, num_bytes);
4170 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
4172 btrfs_delalloc_release_metadata(inode, num_bytes);
4173 btrfs_free_reserved_data_space(inode, num_bytes);
4176 static int update_block_group(struct btrfs_trans_handle *trans,
4177 struct btrfs_root *root,
4178 u64 bytenr, u64 num_bytes, int alloc)
4180 struct btrfs_block_group_cache *cache = NULL;
4181 struct btrfs_fs_info *info = root->fs_info;
4182 u64 total = num_bytes;
4187 /* block accounting for super block */
4188 spin_lock(&info->delalloc_lock);
4189 old_val = btrfs_super_bytes_used(&info->super_copy);
4191 old_val += num_bytes;
4193 old_val -= num_bytes;
4194 btrfs_set_super_bytes_used(&info->super_copy, old_val);
4195 spin_unlock(&info->delalloc_lock);
4198 cache = btrfs_lookup_block_group(info, bytenr);
4201 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
4202 BTRFS_BLOCK_GROUP_RAID1 |
4203 BTRFS_BLOCK_GROUP_RAID10))
4208 * If this block group has free space cache written out, we
4209 * need to make sure to load it if we are removing space. This
4210 * is because we need the unpinning stage to actually add the
4211 * space back to the block group, otherwise we will leak space.
4213 if (!alloc && cache->cached == BTRFS_CACHE_NO)
4214 cache_block_group(cache, trans, NULL, 1);
4216 byte_in_group = bytenr - cache->key.objectid;
4217 WARN_ON(byte_in_group > cache->key.offset);
4219 spin_lock(&cache->space_info->lock);
4220 spin_lock(&cache->lock);
4222 if (btrfs_super_cache_generation(&info->super_copy) != 0 &&
4223 cache->disk_cache_state < BTRFS_DC_CLEAR)
4224 cache->disk_cache_state = BTRFS_DC_CLEAR;
4227 old_val = btrfs_block_group_used(&cache->item);
4228 num_bytes = min(total, cache->key.offset - byte_in_group);
4230 old_val += num_bytes;
4231 btrfs_set_block_group_used(&cache->item, old_val);
4232 cache->reserved -= num_bytes;
4233 cache->space_info->bytes_reserved -= num_bytes;
4234 cache->space_info->reservation_progress++;
4235 cache->space_info->bytes_used += num_bytes;
4236 cache->space_info->disk_used += num_bytes * factor;
4237 spin_unlock(&cache->lock);
4238 spin_unlock(&cache->space_info->lock);
4240 old_val -= num_bytes;
4241 btrfs_set_block_group_used(&cache->item, old_val);
4242 cache->pinned += num_bytes;
4243 cache->space_info->bytes_pinned += num_bytes;
4244 cache->space_info->bytes_used -= num_bytes;
4245 cache->space_info->disk_used -= num_bytes * factor;
4246 spin_unlock(&cache->lock);
4247 spin_unlock(&cache->space_info->lock);
4249 set_extent_dirty(info->pinned_extents,
4250 bytenr, bytenr + num_bytes - 1,
4251 GFP_NOFS | __GFP_NOFAIL);
4253 btrfs_put_block_group(cache);
4255 bytenr += num_bytes;
4260 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
4262 struct btrfs_block_group_cache *cache;
4265 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
4269 bytenr = cache->key.objectid;
4270 btrfs_put_block_group(cache);
4275 static int pin_down_extent(struct btrfs_root *root,
4276 struct btrfs_block_group_cache *cache,
4277 u64 bytenr, u64 num_bytes, int reserved)
4279 spin_lock(&cache->space_info->lock);
4280 spin_lock(&cache->lock);
4281 cache->pinned += num_bytes;
4282 cache->space_info->bytes_pinned += num_bytes;
4284 cache->reserved -= num_bytes;
4285 cache->space_info->bytes_reserved -= num_bytes;
4286 cache->space_info->reservation_progress++;
4288 spin_unlock(&cache->lock);
4289 spin_unlock(&cache->space_info->lock);
4291 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
4292 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
4297 * this function must be called within transaction
4299 int btrfs_pin_extent(struct btrfs_root *root,
4300 u64 bytenr, u64 num_bytes, int reserved)
4302 struct btrfs_block_group_cache *cache;
4304 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
4307 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
4309 btrfs_put_block_group(cache);
4314 * update size of reserved extents. this function may return -EAGAIN
4315 * if 'reserve' is true or 'sinfo' is false.
4317 int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
4318 u64 num_bytes, int reserve, int sinfo)
4322 struct btrfs_space_info *space_info = cache->space_info;
4323 spin_lock(&space_info->lock);
4324 spin_lock(&cache->lock);
4329 cache->reserved += num_bytes;
4330 space_info->bytes_reserved += num_bytes;
4334 space_info->bytes_readonly += num_bytes;
4335 cache->reserved -= num_bytes;
4336 space_info->bytes_reserved -= num_bytes;
4337 space_info->reservation_progress++;
4339 spin_unlock(&cache->lock);
4340 spin_unlock(&space_info->lock);
4342 spin_lock(&cache->lock);
4347 cache->reserved += num_bytes;
4349 cache->reserved -= num_bytes;
4351 spin_unlock(&cache->lock);
4356 int btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
4357 struct btrfs_root *root)
4359 struct btrfs_fs_info *fs_info = root->fs_info;
4360 struct btrfs_caching_control *next;
4361 struct btrfs_caching_control *caching_ctl;
4362 struct btrfs_block_group_cache *cache;
4364 down_write(&fs_info->extent_commit_sem);
4366 list_for_each_entry_safe(caching_ctl, next,
4367 &fs_info->caching_block_groups, list) {
4368 cache = caching_ctl->block_group;
4369 if (block_group_cache_done(cache)) {
4370 cache->last_byte_to_unpin = (u64)-1;
4371 list_del_init(&caching_ctl->list);
4372 put_caching_control(caching_ctl);
4374 cache->last_byte_to_unpin = caching_ctl->progress;
4378 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4379 fs_info->pinned_extents = &fs_info->freed_extents[1];
4381 fs_info->pinned_extents = &fs_info->freed_extents[0];
4383 up_write(&fs_info->extent_commit_sem);
4385 update_global_block_rsv(fs_info);
4389 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
4391 struct btrfs_fs_info *fs_info = root->fs_info;
4392 struct btrfs_block_group_cache *cache = NULL;
4395 while (start <= end) {
4397 start >= cache->key.objectid + cache->key.offset) {
4399 btrfs_put_block_group(cache);
4400 cache = btrfs_lookup_block_group(fs_info, start);
4404 len = cache->key.objectid + cache->key.offset - start;
4405 len = min(len, end + 1 - start);
4407 if (start < cache->last_byte_to_unpin) {
4408 len = min(len, cache->last_byte_to_unpin - start);
4409 btrfs_add_free_space(cache, start, len);
4414 spin_lock(&cache->space_info->lock);
4415 spin_lock(&cache->lock);
4416 cache->pinned -= len;
4417 cache->space_info->bytes_pinned -= len;
4419 cache->space_info->bytes_readonly += len;
4420 } else if (cache->reserved_pinned > 0) {
4421 len = min(len, cache->reserved_pinned);
4422 cache->reserved_pinned -= len;
4423 cache->space_info->bytes_reserved += len;
4425 spin_unlock(&cache->lock);
4426 spin_unlock(&cache->space_info->lock);
4430 btrfs_put_block_group(cache);
4434 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
4435 struct btrfs_root *root)
4437 struct btrfs_fs_info *fs_info = root->fs_info;
4438 struct extent_io_tree *unpin;
4439 struct btrfs_block_rsv *block_rsv;
4440 struct btrfs_block_rsv *next_rsv;
4446 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
4447 unpin = &fs_info->freed_extents[1];
4449 unpin = &fs_info->freed_extents[0];
4452 ret = find_first_extent_bit(unpin, 0, &start, &end,
4457 if (btrfs_test_opt(root, DISCARD))
4458 ret = btrfs_discard_extent(root, start,
4459 end + 1 - start, NULL);
4461 clear_extent_dirty(unpin, start, end, GFP_NOFS);
4462 unpin_extent_range(root, start, end);
4466 mutex_lock(&fs_info->durable_block_rsv_mutex);
4467 list_for_each_entry_safe(block_rsv, next_rsv,
4468 &fs_info->durable_block_rsv_list, list) {
4470 idx = trans->transid & 0x1;
4471 if (block_rsv->freed[idx] > 0) {
4472 block_rsv_add_bytes(block_rsv,
4473 block_rsv->freed[idx], 0);
4474 block_rsv->freed[idx] = 0;
4476 if (atomic_read(&block_rsv->usage) == 0) {
4477 btrfs_block_rsv_release(root, block_rsv, (u64)-1);
4479 if (block_rsv->freed[0] == 0 &&
4480 block_rsv->freed[1] == 0) {
4481 list_del_init(&block_rsv->list);
4485 btrfs_block_rsv_release(root, block_rsv, 0);
4488 mutex_unlock(&fs_info->durable_block_rsv_mutex);
4493 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
4494 struct btrfs_root *root,
4495 u64 bytenr, u64 num_bytes, u64 parent,
4496 u64 root_objectid, u64 owner_objectid,
4497 u64 owner_offset, int refs_to_drop,
4498 struct btrfs_delayed_extent_op *extent_op)
4500 struct btrfs_key key;
4501 struct btrfs_path *path;
4502 struct btrfs_fs_info *info = root->fs_info;
4503 struct btrfs_root *extent_root = info->extent_root;
4504 struct extent_buffer *leaf;
4505 struct btrfs_extent_item *ei;
4506 struct btrfs_extent_inline_ref *iref;
4509 int extent_slot = 0;
4510 int found_extent = 0;
4515 path = btrfs_alloc_path();
4520 path->leave_spinning = 1;
4522 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
4523 BUG_ON(!is_data && refs_to_drop != 1);
4525 ret = lookup_extent_backref(trans, extent_root, path, &iref,
4526 bytenr, num_bytes, parent,
4527 root_objectid, owner_objectid,
4530 extent_slot = path->slots[0];
4531 while (extent_slot >= 0) {
4532 btrfs_item_key_to_cpu(path->nodes[0], &key,
4534 if (key.objectid != bytenr)
4536 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
4537 key.offset == num_bytes) {
4541 if (path->slots[0] - extent_slot > 5)
4545 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4546 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
4547 if (found_extent && item_size < sizeof(*ei))
4550 if (!found_extent) {
4552 ret = remove_extent_backref(trans, extent_root, path,
4556 btrfs_release_path(path);
4557 path->leave_spinning = 1;
4559 key.objectid = bytenr;
4560 key.type = BTRFS_EXTENT_ITEM_KEY;
4561 key.offset = num_bytes;
4563 ret = btrfs_search_slot(trans, extent_root,
4566 printk(KERN_ERR "umm, got %d back from search"
4567 ", was looking for %llu\n", ret,
4568 (unsigned long long)bytenr);
4569 btrfs_print_leaf(extent_root, path->nodes[0]);
4572 extent_slot = path->slots[0];
4575 btrfs_print_leaf(extent_root, path->nodes[0]);
4577 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
4578 "parent %llu root %llu owner %llu offset %llu\n",
4579 (unsigned long long)bytenr,
4580 (unsigned long long)parent,
4581 (unsigned long long)root_objectid,
4582 (unsigned long long)owner_objectid,
4583 (unsigned long long)owner_offset);
4586 leaf = path->nodes[0];
4587 item_size = btrfs_item_size_nr(leaf, extent_slot);
4588 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
4589 if (item_size < sizeof(*ei)) {
4590 BUG_ON(found_extent || extent_slot != path->slots[0]);
4591 ret = convert_extent_item_v0(trans, extent_root, path,
4595 btrfs_release_path(path);
4596 path->leave_spinning = 1;
4598 key.objectid = bytenr;
4599 key.type = BTRFS_EXTENT_ITEM_KEY;
4600 key.offset = num_bytes;
4602 ret = btrfs_search_slot(trans, extent_root, &key, path,
4605 printk(KERN_ERR "umm, got %d back from search"
4606 ", was looking for %llu\n", ret,
4607 (unsigned long long)bytenr);
4608 btrfs_print_leaf(extent_root, path->nodes[0]);
4611 extent_slot = path->slots[0];
4612 leaf = path->nodes[0];
4613 item_size = btrfs_item_size_nr(leaf, extent_slot);
4616 BUG_ON(item_size < sizeof(*ei));
4617 ei = btrfs_item_ptr(leaf, extent_slot,
4618 struct btrfs_extent_item);
4619 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4620 struct btrfs_tree_block_info *bi;
4621 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
4622 bi = (struct btrfs_tree_block_info *)(ei + 1);
4623 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
4626 refs = btrfs_extent_refs(leaf, ei);
4627 BUG_ON(refs < refs_to_drop);
4628 refs -= refs_to_drop;
4632 __run_delayed_extent_op(extent_op, leaf, ei);
4634 * In the case of inline back ref, reference count will
4635 * be updated by remove_extent_backref
4638 BUG_ON(!found_extent);
4640 btrfs_set_extent_refs(leaf, ei, refs);
4641 btrfs_mark_buffer_dirty(leaf);
4644 ret = remove_extent_backref(trans, extent_root, path,
4651 BUG_ON(is_data && refs_to_drop !=
4652 extent_data_ref_count(root, path, iref));
4654 BUG_ON(path->slots[0] != extent_slot);
4656 BUG_ON(path->slots[0] != extent_slot + 1);
4657 path->slots[0] = extent_slot;
4662 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
4665 btrfs_release_path(path);
4668 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
4671 invalidate_mapping_pages(info->btree_inode->i_mapping,
4672 bytenr >> PAGE_CACHE_SHIFT,
4673 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
4676 ret = update_block_group(trans, root, bytenr, num_bytes, 0);
4679 btrfs_free_path(path);
4684 * when we free an block, it is possible (and likely) that we free the last
4685 * delayed ref for that extent as well. This searches the delayed ref tree for
4686 * a given extent, and if there are no other delayed refs to be processed, it
4687 * removes it from the tree.
4689 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
4690 struct btrfs_root *root, u64 bytenr)
4692 struct btrfs_delayed_ref_head *head;
4693 struct btrfs_delayed_ref_root *delayed_refs;
4694 struct btrfs_delayed_ref_node *ref;
4695 struct rb_node *node;
4698 delayed_refs = &trans->transaction->delayed_refs;
4699 spin_lock(&delayed_refs->lock);
4700 head = btrfs_find_delayed_ref_head(trans, bytenr);
4704 node = rb_prev(&head->node.rb_node);
4708 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
4710 /* there are still entries for this ref, we can't drop it */
4711 if (ref->bytenr == bytenr)
4714 if (head->extent_op) {
4715 if (!head->must_insert_reserved)
4717 kfree(head->extent_op);
4718 head->extent_op = NULL;
4722 * waiting for the lock here would deadlock. If someone else has it
4723 * locked they are already in the process of dropping it anyway
4725 if (!mutex_trylock(&head->mutex))
4729 * at this point we have a head with no other entries. Go
4730 * ahead and process it.
4732 head->node.in_tree = 0;
4733 rb_erase(&head->node.rb_node, &delayed_refs->root);
4735 delayed_refs->num_entries--;
4738 * we don't take a ref on the node because we're removing it from the
4739 * tree, so we just steal the ref the tree was holding.
4741 delayed_refs->num_heads--;
4742 if (list_empty(&head->cluster))
4743 delayed_refs->num_heads_ready--;
4745 list_del_init(&head->cluster);
4746 spin_unlock(&delayed_refs->lock);
4748 BUG_ON(head->extent_op);
4749 if (head->must_insert_reserved)
4752 mutex_unlock(&head->mutex);
4753 btrfs_put_delayed_ref(&head->node);
4756 spin_unlock(&delayed_refs->lock);
4760 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
4761 struct btrfs_root *root,
4762 struct extent_buffer *buf,
4763 u64 parent, int last_ref)
4765 struct btrfs_block_rsv *block_rsv;
4766 struct btrfs_block_group_cache *cache = NULL;
4769 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4770 ret = btrfs_add_delayed_tree_ref(trans, buf->start, buf->len,
4771 parent, root->root_key.objectid,
4772 btrfs_header_level(buf),
4773 BTRFS_DROP_DELAYED_REF, NULL);
4780 block_rsv = get_block_rsv(trans, root);
4781 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
4782 if (block_rsv->space_info != cache->space_info)
4785 if (btrfs_header_generation(buf) == trans->transid) {
4786 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
4787 ret = check_ref_cleanup(trans, root, buf->start);
4792 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
4793 pin_down_extent(root, cache, buf->start, buf->len, 1);
4797 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
4799 btrfs_add_free_space(cache, buf->start, buf->len);
4800 ret = btrfs_update_reserved_bytes(cache, buf->len, 0, 0);
4801 if (ret == -EAGAIN) {
4802 /* block group became read-only */
4803 btrfs_update_reserved_bytes(cache, buf->len, 0, 1);
4808 spin_lock(&block_rsv->lock);
4809 if (block_rsv->reserved < block_rsv->size) {
4810 block_rsv->reserved += buf->len;
4813 spin_unlock(&block_rsv->lock);
4816 spin_lock(&cache->space_info->lock);
4817 cache->space_info->bytes_reserved -= buf->len;
4818 cache->space_info->reservation_progress++;
4819 spin_unlock(&cache->space_info->lock);
4824 if (block_rsv->durable && !cache->ro) {
4826 spin_lock(&cache->lock);
4828 cache->reserved_pinned += buf->len;
4831 spin_unlock(&cache->lock);
4834 spin_lock(&block_rsv->lock);
4835 block_rsv->freed[trans->transid & 0x1] += buf->len;
4836 spin_unlock(&block_rsv->lock);
4841 * Deleting the buffer, clear the corrupt flag since it doesn't matter
4844 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
4845 btrfs_put_block_group(cache);
4848 int btrfs_free_extent(struct btrfs_trans_handle *trans,
4849 struct btrfs_root *root,
4850 u64 bytenr, u64 num_bytes, u64 parent,
4851 u64 root_objectid, u64 owner, u64 offset)
4856 * tree log blocks never actually go into the extent allocation
4857 * tree, just update pinning info and exit early.
4859 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
4860 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
4861 /* unlocks the pinned mutex */
4862 btrfs_pin_extent(root, bytenr, num_bytes, 1);
4864 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
4865 ret = btrfs_add_delayed_tree_ref(trans, bytenr, num_bytes,
4866 parent, root_objectid, (int)owner,
4867 BTRFS_DROP_DELAYED_REF, NULL);
4870 ret = btrfs_add_delayed_data_ref(trans, bytenr, num_bytes,
4871 parent, root_objectid, owner,
4872 offset, BTRFS_DROP_DELAYED_REF, NULL);
4878 static u64 stripe_align(struct btrfs_root *root, u64 val)
4880 u64 mask = ((u64)root->stripesize - 1);
4881 u64 ret = (val + mask) & ~mask;
4886 * when we wait for progress in the block group caching, its because
4887 * our allocation attempt failed at least once. So, we must sleep
4888 * and let some progress happen before we try again.
4890 * This function will sleep at least once waiting for new free space to
4891 * show up, and then it will check the block group free space numbers
4892 * for our min num_bytes. Another option is to have it go ahead
4893 * and look in the rbtree for a free extent of a given size, but this
4897 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
4900 struct btrfs_caching_control *caching_ctl;
4903 caching_ctl = get_caching_control(cache);
4907 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
4908 (cache->free_space >= num_bytes));
4910 put_caching_control(caching_ctl);
4915 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
4917 struct btrfs_caching_control *caching_ctl;
4920 caching_ctl = get_caching_control(cache);
4924 wait_event(caching_ctl->wait, block_group_cache_done(cache));
4926 put_caching_control(caching_ctl);
4930 static int get_block_group_index(struct btrfs_block_group_cache *cache)
4933 if (cache->flags & BTRFS_BLOCK_GROUP_RAID10)
4935 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID1)
4937 else if (cache->flags & BTRFS_BLOCK_GROUP_DUP)
4939 else if (cache->flags & BTRFS_BLOCK_GROUP_RAID0)
4946 enum btrfs_loop_type {
4947 LOOP_FIND_IDEAL = 0,
4948 LOOP_CACHING_NOWAIT = 1,
4949 LOOP_CACHING_WAIT = 2,
4950 LOOP_ALLOC_CHUNK = 3,
4951 LOOP_NO_EMPTY_SIZE = 4,
4955 * walks the btree of allocated extents and find a hole of a given size.
4956 * The key ins is changed to record the hole:
4957 * ins->objectid == block start
4958 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4959 * ins->offset == number of blocks
4960 * Any available blocks before search_start are skipped.
4962 static noinline int find_free_extent(struct btrfs_trans_handle *trans,
4963 struct btrfs_root *orig_root,
4964 u64 num_bytes, u64 empty_size,
4965 u64 search_start, u64 search_end,
4966 u64 hint_byte, struct btrfs_key *ins,
4970 struct btrfs_root *root = orig_root->fs_info->extent_root;
4971 struct btrfs_free_cluster *last_ptr = NULL;
4972 struct btrfs_block_group_cache *block_group = NULL;
4973 int empty_cluster = 2 * 1024 * 1024;
4974 int allowed_chunk_alloc = 0;
4975 int done_chunk_alloc = 0;
4976 struct btrfs_space_info *space_info;
4977 int last_ptr_loop = 0;
4980 bool found_uncached_bg = false;
4981 bool failed_cluster_refill = false;
4982 bool failed_alloc = false;
4983 bool use_cluster = true;
4984 u64 ideal_cache_percent = 0;
4985 u64 ideal_cache_offset = 0;
4987 WARN_ON(num_bytes < root->sectorsize);
4988 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
4992 space_info = __find_space_info(root->fs_info, data);
4994 printk(KERN_ERR "No space info for %d\n", data);
4999 * If the space info is for both data and metadata it means we have a
5000 * small filesystem and we can't use the clustering stuff.
5002 if (btrfs_mixed_space_info(space_info))
5003 use_cluster = false;
5005 if (orig_root->ref_cows || empty_size)
5006 allowed_chunk_alloc = 1;
5008 if (data & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
5009 last_ptr = &root->fs_info->meta_alloc_cluster;
5010 if (!btrfs_test_opt(root, SSD))
5011 empty_cluster = 64 * 1024;
5014 if ((data & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
5015 btrfs_test_opt(root, SSD)) {
5016 last_ptr = &root->fs_info->data_alloc_cluster;
5020 spin_lock(&last_ptr->lock);
5021 if (last_ptr->block_group)
5022 hint_byte = last_ptr->window_start;
5023 spin_unlock(&last_ptr->lock);
5026 search_start = max(search_start, first_logical_byte(root, 0));
5027 search_start = max(search_start, hint_byte);
5032 if (search_start == hint_byte) {
5034 block_group = btrfs_lookup_block_group(root->fs_info,
5037 * we don't want to use the block group if it doesn't match our
5038 * allocation bits, or if its not cached.
5040 * However if we are re-searching with an ideal block group
5041 * picked out then we don't care that the block group is cached.
5043 if (block_group && block_group_bits(block_group, data) &&
5044 (block_group->cached != BTRFS_CACHE_NO ||
5045 search_start == ideal_cache_offset)) {
5046 down_read(&space_info->groups_sem);
5047 if (list_empty(&block_group->list) ||
5050 * someone is removing this block group,
5051 * we can't jump into the have_block_group
5052 * target because our list pointers are not
5055 btrfs_put_block_group(block_group);
5056 up_read(&space_info->groups_sem);
5058 index = get_block_group_index(block_group);
5059 goto have_block_group;
5061 } else if (block_group) {
5062 btrfs_put_block_group(block_group);
5066 down_read(&space_info->groups_sem);
5067 list_for_each_entry(block_group, &space_info->block_groups[index],
5072 btrfs_get_block_group(block_group);
5073 search_start = block_group->key.objectid;
5076 * this can happen if we end up cycling through all the
5077 * raid types, but we want to make sure we only allocate
5078 * for the proper type.
5080 if (!block_group_bits(block_group, data)) {
5081 u64 extra = BTRFS_BLOCK_GROUP_DUP |
5082 BTRFS_BLOCK_GROUP_RAID1 |
5083 BTRFS_BLOCK_GROUP_RAID10;
5086 * if they asked for extra copies and this block group
5087 * doesn't provide them, bail. This does allow us to
5088 * fill raid0 from raid1.
5090 if ((data & extra) && !(block_group->flags & extra))
5095 if (unlikely(block_group->cached == BTRFS_CACHE_NO)) {
5098 ret = cache_block_group(block_group, trans,
5100 if (block_group->cached == BTRFS_CACHE_FINISHED)
5101 goto have_block_group;
5103 free_percent = btrfs_block_group_used(&block_group->item);
5104 free_percent *= 100;
5105 free_percent = div64_u64(free_percent,
5106 block_group->key.offset);
5107 free_percent = 100 - free_percent;
5108 if (free_percent > ideal_cache_percent &&
5109 likely(!block_group->ro)) {
5110 ideal_cache_offset = block_group->key.objectid;
5111 ideal_cache_percent = free_percent;
5115 * We only want to start kthread caching if we are at
5116 * the point where we will wait for caching to make
5117 * progress, or if our ideal search is over and we've
5118 * found somebody to start caching.
5120 if (loop > LOOP_CACHING_NOWAIT ||
5121 (loop > LOOP_FIND_IDEAL &&
5122 atomic_read(&space_info->caching_threads) < 2)) {
5123 ret = cache_block_group(block_group, trans,
5127 found_uncached_bg = true;
5130 * If loop is set for cached only, try the next block
5133 if (loop == LOOP_FIND_IDEAL)
5137 cached = block_group_cache_done(block_group);
5138 if (unlikely(!cached))
5139 found_uncached_bg = true;
5141 if (unlikely(block_group->ro))
5145 * Ok we want to try and use the cluster allocator, so lets look
5146 * there, unless we are on LOOP_NO_EMPTY_SIZE, since we will
5147 * have tried the cluster allocator plenty of times at this
5148 * point and not have found anything, so we are likely way too
5149 * fragmented for the clustering stuff to find anything, so lets
5150 * just skip it and let the allocator find whatever block it can
5153 if (last_ptr && loop < LOOP_NO_EMPTY_SIZE) {
5155 * the refill lock keeps out other
5156 * people trying to start a new cluster
5158 spin_lock(&last_ptr->refill_lock);
5159 if (last_ptr->block_group &&
5160 (last_ptr->block_group->ro ||
5161 !block_group_bits(last_ptr->block_group, data))) {
5163 goto refill_cluster;
5166 offset = btrfs_alloc_from_cluster(block_group, last_ptr,
5167 num_bytes, search_start);
5169 /* we have a block, we're done */
5170 spin_unlock(&last_ptr->refill_lock);
5174 spin_lock(&last_ptr->lock);
5176 * whoops, this cluster doesn't actually point to
5177 * this block group. Get a ref on the block
5178 * group is does point to and try again
5180 if (!last_ptr_loop && last_ptr->block_group &&
5181 last_ptr->block_group != block_group) {
5183 btrfs_put_block_group(block_group);
5184 block_group = last_ptr->block_group;
5185 btrfs_get_block_group(block_group);
5186 spin_unlock(&last_ptr->lock);
5187 spin_unlock(&last_ptr->refill_lock);
5190 search_start = block_group->key.objectid;
5192 * we know this block group is properly
5193 * in the list because
5194 * btrfs_remove_block_group, drops the
5195 * cluster before it removes the block
5196 * group from the list
5198 goto have_block_group;
5200 spin_unlock(&last_ptr->lock);
5203 * this cluster didn't work out, free it and
5206 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5210 /* allocate a cluster in this block group */
5211 ret = btrfs_find_space_cluster(trans, root,
5212 block_group, last_ptr,
5214 empty_cluster + empty_size);
5217 * now pull our allocation out of this
5220 offset = btrfs_alloc_from_cluster(block_group,
5221 last_ptr, num_bytes,
5224 /* we found one, proceed */
5225 spin_unlock(&last_ptr->refill_lock);
5228 } else if (!cached && loop > LOOP_CACHING_NOWAIT
5229 && !failed_cluster_refill) {
5230 spin_unlock(&last_ptr->refill_lock);
5232 failed_cluster_refill = true;
5233 wait_block_group_cache_progress(block_group,
5234 num_bytes + empty_cluster + empty_size);
5235 goto have_block_group;
5239 * at this point we either didn't find a cluster
5240 * or we weren't able to allocate a block from our
5241 * cluster. Free the cluster we've been trying
5242 * to use, and go to the next block group
5244 btrfs_return_cluster_to_free_space(NULL, last_ptr);
5245 spin_unlock(&last_ptr->refill_lock);
5249 offset = btrfs_find_space_for_alloc(block_group, search_start,
5250 num_bytes, empty_size);
5252 * If we didn't find a chunk, and we haven't failed on this
5253 * block group before, and this block group is in the middle of
5254 * caching and we are ok with waiting, then go ahead and wait
5255 * for progress to be made, and set failed_alloc to true.
5257 * If failed_alloc is true then we've already waited on this
5258 * block group once and should move on to the next block group.
5260 if (!offset && !failed_alloc && !cached &&
5261 loop > LOOP_CACHING_NOWAIT) {
5262 wait_block_group_cache_progress(block_group,
5263 num_bytes + empty_size);
5264 failed_alloc = true;
5265 goto have_block_group;
5266 } else if (!offset) {
5270 search_start = stripe_align(root, offset);
5271 /* move on to the next group */
5272 if (search_start + num_bytes >= search_end) {
5273 btrfs_add_free_space(block_group, offset, num_bytes);
5277 /* move on to the next group */
5278 if (search_start + num_bytes >
5279 block_group->key.objectid + block_group->key.offset) {
5280 btrfs_add_free_space(block_group, offset, num_bytes);
5284 ins->objectid = search_start;
5285 ins->offset = num_bytes;
5287 if (offset < search_start)
5288 btrfs_add_free_space(block_group, offset,
5289 search_start - offset);
5290 BUG_ON(offset > search_start);
5292 ret = btrfs_update_reserved_bytes(block_group, num_bytes, 1,
5293 (data & BTRFS_BLOCK_GROUP_DATA));
5294 if (ret == -EAGAIN) {
5295 btrfs_add_free_space(block_group, offset, num_bytes);
5299 /* we are all good, lets return */
5300 ins->objectid = search_start;
5301 ins->offset = num_bytes;
5303 if (offset < search_start)
5304 btrfs_add_free_space(block_group, offset,
5305 search_start - offset);
5306 BUG_ON(offset > search_start);
5309 failed_cluster_refill = false;
5310 failed_alloc = false;
5311 BUG_ON(index != get_block_group_index(block_group));
5312 btrfs_put_block_group(block_group);
5314 up_read(&space_info->groups_sem);
5316 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
5319 /* LOOP_FIND_IDEAL, only search caching/cached bg's, and don't wait for
5320 * for them to make caching progress. Also
5321 * determine the best possible bg to cache
5322 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
5323 * caching kthreads as we move along
5324 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
5325 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
5326 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
5329 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE &&
5330 (found_uncached_bg || empty_size || empty_cluster ||
5331 allowed_chunk_alloc)) {
5333 if (loop == LOOP_FIND_IDEAL && found_uncached_bg) {
5334 found_uncached_bg = false;
5336 if (!ideal_cache_percent &&
5337 atomic_read(&space_info->caching_threads))
5341 * 1 of the following 2 things have happened so far
5343 * 1) We found an ideal block group for caching that
5344 * is mostly full and will cache quickly, so we might
5345 * as well wait for it.
5347 * 2) We searched for cached only and we didn't find
5348 * anything, and we didn't start any caching kthreads
5349 * either, so chances are we will loop through and
5350 * start a couple caching kthreads, and then come back
5351 * around and just wait for them. This will be slower
5352 * because we will have 2 caching kthreads reading at
5353 * the same time when we could have just started one
5354 * and waited for it to get far enough to give us an
5355 * allocation, so go ahead and go to the wait caching
5358 loop = LOOP_CACHING_WAIT;
5359 search_start = ideal_cache_offset;
5360 ideal_cache_percent = 0;
5362 } else if (loop == LOOP_FIND_IDEAL) {
5364 * Didn't find a uncached bg, wait on anything we find
5367 loop = LOOP_CACHING_WAIT;
5371 if (loop < LOOP_CACHING_WAIT) {
5376 if (loop == LOOP_ALLOC_CHUNK) {
5381 if (allowed_chunk_alloc) {
5382 ret = do_chunk_alloc(trans, root, num_bytes +
5383 2 * 1024 * 1024, data,
5384 CHUNK_ALLOC_LIMITED);
5385 allowed_chunk_alloc = 0;
5386 done_chunk_alloc = 1;
5387 } else if (!done_chunk_alloc &&
5388 space_info->force_alloc == CHUNK_ALLOC_NO_FORCE) {
5389 space_info->force_alloc = CHUNK_ALLOC_LIMITED;
5392 if (loop < LOOP_NO_EMPTY_SIZE) {
5397 } else if (!ins->objectid) {
5401 /* we found what we needed */
5402 if (ins->objectid) {
5403 if (!(data & BTRFS_BLOCK_GROUP_DATA))
5404 trans->block_group = block_group->key.objectid;
5406 btrfs_put_block_group(block_group);
5413 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
5414 int dump_block_groups)
5416 struct btrfs_block_group_cache *cache;
5419 spin_lock(&info->lock);
5420 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
5421 (unsigned long long)(info->total_bytes - info->bytes_used -
5422 info->bytes_pinned - info->bytes_reserved -
5423 info->bytes_readonly),
5424 (info->full) ? "" : "not ");
5425 printk(KERN_INFO "space_info total=%llu, used=%llu, pinned=%llu, "
5426 "reserved=%llu, may_use=%llu, readonly=%llu\n",
5427 (unsigned long long)info->total_bytes,
5428 (unsigned long long)info->bytes_used,
5429 (unsigned long long)info->bytes_pinned,
5430 (unsigned long long)info->bytes_reserved,
5431 (unsigned long long)info->bytes_may_use,
5432 (unsigned long long)info->bytes_readonly);
5433 spin_unlock(&info->lock);
5435 if (!dump_block_groups)
5438 down_read(&info->groups_sem);
5440 list_for_each_entry(cache, &info->block_groups[index], list) {
5441 spin_lock(&cache->lock);
5442 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
5443 "%llu pinned %llu reserved\n",
5444 (unsigned long long)cache->key.objectid,
5445 (unsigned long long)cache->key.offset,
5446 (unsigned long long)btrfs_block_group_used(&cache->item),
5447 (unsigned long long)cache->pinned,
5448 (unsigned long long)cache->reserved);
5449 btrfs_dump_free_space(cache, bytes);
5450 spin_unlock(&cache->lock);
5452 if (++index < BTRFS_NR_RAID_TYPES)
5454 up_read(&info->groups_sem);
5457 int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
5458 struct btrfs_root *root,
5459 u64 num_bytes, u64 min_alloc_size,
5460 u64 empty_size, u64 hint_byte,
5461 u64 search_end, struct btrfs_key *ins,
5465 u64 search_start = 0;
5467 data = btrfs_get_alloc_profile(root, data);
5470 * the only place that sets empty_size is btrfs_realloc_node, which
5471 * is not called recursively on allocations
5473 if (empty_size || root->ref_cows)
5474 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
5475 num_bytes + 2 * 1024 * 1024, data,
5476 CHUNK_ALLOC_NO_FORCE);
5478 WARN_ON(num_bytes < root->sectorsize);
5479 ret = find_free_extent(trans, root, num_bytes, empty_size,
5480 search_start, search_end, hint_byte,
5483 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
5484 num_bytes = num_bytes >> 1;
5485 num_bytes = num_bytes & ~(root->sectorsize - 1);
5486 num_bytes = max(num_bytes, min_alloc_size);
5487 do_chunk_alloc(trans, root->fs_info->extent_root,
5488 num_bytes, data, CHUNK_ALLOC_FORCE);
5491 if (ret == -ENOSPC && btrfs_test_opt(root, ENOSPC_DEBUG)) {
5492 struct btrfs_space_info *sinfo;
5494 sinfo = __find_space_info(root->fs_info, data);
5495 printk(KERN_ERR "btrfs allocation failed flags %llu, "
5496 "wanted %llu\n", (unsigned long long)data,
5497 (unsigned long long)num_bytes);
5498 dump_space_info(sinfo, num_bytes, 1);
5501 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
5506 int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
5508 struct btrfs_block_group_cache *cache;
5511 cache = btrfs_lookup_block_group(root->fs_info, start);
5513 printk(KERN_ERR "Unable to find block group for %llu\n",
5514 (unsigned long long)start);
5518 if (btrfs_test_opt(root, DISCARD))
5519 ret = btrfs_discard_extent(root, start, len, NULL);
5521 btrfs_add_free_space(cache, start, len);
5522 btrfs_update_reserved_bytes(cache, len, 0, 1);
5523 btrfs_put_block_group(cache);
5525 trace_btrfs_reserved_extent_free(root, start, len);
5530 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5531 struct btrfs_root *root,
5532 u64 parent, u64 root_objectid,
5533 u64 flags, u64 owner, u64 offset,
5534 struct btrfs_key *ins, int ref_mod)
5537 struct btrfs_fs_info *fs_info = root->fs_info;
5538 struct btrfs_extent_item *extent_item;
5539 struct btrfs_extent_inline_ref *iref;
5540 struct btrfs_path *path;
5541 struct extent_buffer *leaf;
5546 type = BTRFS_SHARED_DATA_REF_KEY;
5548 type = BTRFS_EXTENT_DATA_REF_KEY;
5550 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
5552 path = btrfs_alloc_path();
5556 path->leave_spinning = 1;
5557 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5561 leaf = path->nodes[0];
5562 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5563 struct btrfs_extent_item);
5564 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
5565 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5566 btrfs_set_extent_flags(leaf, extent_item,
5567 flags | BTRFS_EXTENT_FLAG_DATA);
5569 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
5570 btrfs_set_extent_inline_ref_type(leaf, iref, type);
5572 struct btrfs_shared_data_ref *ref;
5573 ref = (struct btrfs_shared_data_ref *)(iref + 1);
5574 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5575 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
5577 struct btrfs_extent_data_ref *ref;
5578 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
5579 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
5580 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
5581 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
5582 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
5585 btrfs_mark_buffer_dirty(path->nodes[0]);
5586 btrfs_free_path(path);
5588 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5590 printk(KERN_ERR "btrfs update block group failed for %llu "
5591 "%llu\n", (unsigned long long)ins->objectid,
5592 (unsigned long long)ins->offset);
5598 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
5599 struct btrfs_root *root,
5600 u64 parent, u64 root_objectid,
5601 u64 flags, struct btrfs_disk_key *key,
5602 int level, struct btrfs_key *ins)
5605 struct btrfs_fs_info *fs_info = root->fs_info;
5606 struct btrfs_extent_item *extent_item;
5607 struct btrfs_tree_block_info *block_info;
5608 struct btrfs_extent_inline_ref *iref;
5609 struct btrfs_path *path;
5610 struct extent_buffer *leaf;
5611 u32 size = sizeof(*extent_item) + sizeof(*block_info) + sizeof(*iref);
5613 path = btrfs_alloc_path();
5616 path->leave_spinning = 1;
5617 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
5621 leaf = path->nodes[0];
5622 extent_item = btrfs_item_ptr(leaf, path->slots[0],
5623 struct btrfs_extent_item);
5624 btrfs_set_extent_refs(leaf, extent_item, 1);
5625 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
5626 btrfs_set_extent_flags(leaf, extent_item,
5627 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
5628 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
5630 btrfs_set_tree_block_key(leaf, block_info, key);
5631 btrfs_set_tree_block_level(leaf, block_info, level);
5633 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
5635 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
5636 btrfs_set_extent_inline_ref_type(leaf, iref,
5637 BTRFS_SHARED_BLOCK_REF_KEY);
5638 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
5640 btrfs_set_extent_inline_ref_type(leaf, iref,
5641 BTRFS_TREE_BLOCK_REF_KEY);
5642 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
5645 btrfs_mark_buffer_dirty(leaf);
5646 btrfs_free_path(path);
5648 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1);
5650 printk(KERN_ERR "btrfs update block group failed for %llu "
5651 "%llu\n", (unsigned long long)ins->objectid,
5652 (unsigned long long)ins->offset);
5658 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
5659 struct btrfs_root *root,
5660 u64 root_objectid, u64 owner,
5661 u64 offset, struct btrfs_key *ins)
5665 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
5667 ret = btrfs_add_delayed_data_ref(trans, ins->objectid, ins->offset,
5668 0, root_objectid, owner, offset,
5669 BTRFS_ADD_DELAYED_EXTENT, NULL);
5674 * this is used by the tree logging recovery code. It records that
5675 * an extent has been allocated and makes sure to clear the free
5676 * space cache bits as well
5678 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
5679 struct btrfs_root *root,
5680 u64 root_objectid, u64 owner, u64 offset,
5681 struct btrfs_key *ins)
5684 struct btrfs_block_group_cache *block_group;
5685 struct btrfs_caching_control *caching_ctl;
5686 u64 start = ins->objectid;
5687 u64 num_bytes = ins->offset;
5689 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
5690 cache_block_group(block_group, trans, NULL, 0);
5691 caching_ctl = get_caching_control(block_group);
5694 BUG_ON(!block_group_cache_done(block_group));
5695 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5698 mutex_lock(&caching_ctl->mutex);
5700 if (start >= caching_ctl->progress) {
5701 ret = add_excluded_extent(root, start, num_bytes);
5703 } else if (start + num_bytes <= caching_ctl->progress) {
5704 ret = btrfs_remove_free_space(block_group,
5708 num_bytes = caching_ctl->progress - start;
5709 ret = btrfs_remove_free_space(block_group,
5713 start = caching_ctl->progress;
5714 num_bytes = ins->objectid + ins->offset -
5715 caching_ctl->progress;
5716 ret = add_excluded_extent(root, start, num_bytes);
5720 mutex_unlock(&caching_ctl->mutex);
5721 put_caching_control(caching_ctl);
5724 ret = btrfs_update_reserved_bytes(block_group, ins->offset, 1, 1);
5726 btrfs_put_block_group(block_group);
5727 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
5728 0, owner, offset, ins, 1);
5732 struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
5733 struct btrfs_root *root,
5734 u64 bytenr, u32 blocksize,
5737 struct extent_buffer *buf;
5739 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
5741 return ERR_PTR(-ENOMEM);
5742 btrfs_set_header_generation(buf, trans->transid);
5743 btrfs_set_buffer_lockdep_class(buf, level);
5744 btrfs_tree_lock(buf);
5745 clean_tree_block(trans, root, buf);
5747 btrfs_set_lock_blocking(buf);
5748 btrfs_set_buffer_uptodate(buf);
5750 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
5752 * we allow two log transactions at a time, use different
5753 * EXENT bit to differentiate dirty pages.
5755 if (root->log_transid % 2 == 0)
5756 set_extent_dirty(&root->dirty_log_pages, buf->start,
5757 buf->start + buf->len - 1, GFP_NOFS);
5759 set_extent_new(&root->dirty_log_pages, buf->start,
5760 buf->start + buf->len - 1, GFP_NOFS);
5762 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
5763 buf->start + buf->len - 1, GFP_NOFS);
5765 trans->blocks_used++;
5766 /* this returns a buffer locked for blocking */
5770 static struct btrfs_block_rsv *
5771 use_block_rsv(struct btrfs_trans_handle *trans,
5772 struct btrfs_root *root, u32 blocksize)
5774 struct btrfs_block_rsv *block_rsv;
5775 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
5778 block_rsv = get_block_rsv(trans, root);
5780 if (block_rsv->size == 0) {
5781 ret = reserve_metadata_bytes(trans, root, block_rsv,
5784 * If we couldn't reserve metadata bytes try and use some from
5785 * the global reserve.
5787 if (ret && block_rsv != global_rsv) {
5788 ret = block_rsv_use_bytes(global_rsv, blocksize);
5791 return ERR_PTR(ret);
5793 return ERR_PTR(ret);
5798 ret = block_rsv_use_bytes(block_rsv, blocksize);
5803 ret = reserve_metadata_bytes(trans, root, block_rsv, blocksize,
5806 spin_lock(&block_rsv->lock);
5807 block_rsv->size += blocksize;
5808 spin_unlock(&block_rsv->lock);
5810 } else if (ret && block_rsv != global_rsv) {
5811 ret = block_rsv_use_bytes(global_rsv, blocksize);
5817 return ERR_PTR(-ENOSPC);
5820 static void unuse_block_rsv(struct btrfs_block_rsv *block_rsv, u32 blocksize)
5822 block_rsv_add_bytes(block_rsv, blocksize, 0);
5823 block_rsv_release_bytes(block_rsv, NULL, 0);
5827 * finds a free extent and does all the dirty work required for allocation
5828 * returns the key for the extent through ins, and a tree buffer for
5829 * the first block of the extent through buf.
5831 * returns the tree buffer or NULL.
5833 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
5834 struct btrfs_root *root, u32 blocksize,
5835 u64 parent, u64 root_objectid,
5836 struct btrfs_disk_key *key, int level,
5837 u64 hint, u64 empty_size)
5839 struct btrfs_key ins;
5840 struct btrfs_block_rsv *block_rsv;
5841 struct extent_buffer *buf;
5846 block_rsv = use_block_rsv(trans, root, blocksize);
5847 if (IS_ERR(block_rsv))
5848 return ERR_CAST(block_rsv);
5850 ret = btrfs_reserve_extent(trans, root, blocksize, blocksize,
5851 empty_size, hint, (u64)-1, &ins, 0);
5853 unuse_block_rsv(block_rsv, blocksize);
5854 return ERR_PTR(ret);
5857 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
5859 BUG_ON(IS_ERR(buf));
5861 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
5863 parent = ins.objectid;
5864 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
5868 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
5869 struct btrfs_delayed_extent_op *extent_op;
5870 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
5873 memcpy(&extent_op->key, key, sizeof(extent_op->key));
5875 memset(&extent_op->key, 0, sizeof(extent_op->key));
5876 extent_op->flags_to_set = flags;
5877 extent_op->update_key = 1;
5878 extent_op->update_flags = 1;
5879 extent_op->is_data = 0;
5881 ret = btrfs_add_delayed_tree_ref(trans, ins.objectid,
5882 ins.offset, parent, root_objectid,
5883 level, BTRFS_ADD_DELAYED_EXTENT,
5890 struct walk_control {
5891 u64 refs[BTRFS_MAX_LEVEL];
5892 u64 flags[BTRFS_MAX_LEVEL];
5893 struct btrfs_key update_progress;
5903 #define DROP_REFERENCE 1
5904 #define UPDATE_BACKREF 2
5906 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
5907 struct btrfs_root *root,
5908 struct walk_control *wc,
5909 struct btrfs_path *path)
5917 struct btrfs_key key;
5918 struct extent_buffer *eb;
5923 if (path->slots[wc->level] < wc->reada_slot) {
5924 wc->reada_count = wc->reada_count * 2 / 3;
5925 wc->reada_count = max(wc->reada_count, 2);
5927 wc->reada_count = wc->reada_count * 3 / 2;
5928 wc->reada_count = min_t(int, wc->reada_count,
5929 BTRFS_NODEPTRS_PER_BLOCK(root));
5932 eb = path->nodes[wc->level];
5933 nritems = btrfs_header_nritems(eb);
5934 blocksize = btrfs_level_size(root, wc->level - 1);
5936 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
5937 if (nread >= wc->reada_count)
5941 bytenr = btrfs_node_blockptr(eb, slot);
5942 generation = btrfs_node_ptr_generation(eb, slot);
5944 if (slot == path->slots[wc->level])
5947 if (wc->stage == UPDATE_BACKREF &&
5948 generation <= root->root_key.offset)
5951 /* We don't lock the tree block, it's OK to be racy here */
5952 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
5957 if (wc->stage == DROP_REFERENCE) {
5961 if (wc->level == 1 &&
5962 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5964 if (!wc->update_ref ||
5965 generation <= root->root_key.offset)
5967 btrfs_node_key_to_cpu(eb, &key, slot);
5968 ret = btrfs_comp_cpu_keys(&key,
5969 &wc->update_progress);
5973 if (wc->level == 1 &&
5974 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5978 ret = readahead_tree_block(root, bytenr, blocksize,
5984 wc->reada_slot = slot;
5988 * hepler to process tree block while walking down the tree.
5990 * when wc->stage == UPDATE_BACKREF, this function updates
5991 * back refs for pointers in the block.
5993 * NOTE: return value 1 means we should stop walking down.
5995 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
5996 struct btrfs_root *root,
5997 struct btrfs_path *path,
5998 struct walk_control *wc, int lookup_info)
6000 int level = wc->level;
6001 struct extent_buffer *eb = path->nodes[level];
6002 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6005 if (wc->stage == UPDATE_BACKREF &&
6006 btrfs_header_owner(eb) != root->root_key.objectid)
6010 * when reference count of tree block is 1, it won't increase
6011 * again. once full backref flag is set, we never clear it.
6014 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
6015 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
6016 BUG_ON(!path->locks[level]);
6017 ret = btrfs_lookup_extent_info(trans, root,
6022 BUG_ON(wc->refs[level] == 0);
6025 if (wc->stage == DROP_REFERENCE) {
6026 if (wc->refs[level] > 1)
6029 if (path->locks[level] && !wc->keep_locks) {
6030 btrfs_tree_unlock(eb);
6031 path->locks[level] = 0;
6036 /* wc->stage == UPDATE_BACKREF */
6037 if (!(wc->flags[level] & flag)) {
6038 BUG_ON(!path->locks[level]);
6039 ret = btrfs_inc_ref(trans, root, eb, 1);
6041 ret = btrfs_dec_ref(trans, root, eb, 0);
6043 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
6046 wc->flags[level] |= flag;
6050 * the block is shared by multiple trees, so it's not good to
6051 * keep the tree lock
6053 if (path->locks[level] && level > 0) {
6054 btrfs_tree_unlock(eb);
6055 path->locks[level] = 0;
6061 * hepler to process tree block pointer.
6063 * when wc->stage == DROP_REFERENCE, this function checks
6064 * reference count of the block pointed to. if the block
6065 * is shared and we need update back refs for the subtree
6066 * rooted at the block, this function changes wc->stage to
6067 * UPDATE_BACKREF. if the block is shared and there is no
6068 * need to update back, this function drops the reference
6071 * NOTE: return value 1 means we should stop walking down.
6073 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
6074 struct btrfs_root *root,
6075 struct btrfs_path *path,
6076 struct walk_control *wc, int *lookup_info)
6082 struct btrfs_key key;
6083 struct extent_buffer *next;
6084 int level = wc->level;
6088 generation = btrfs_node_ptr_generation(path->nodes[level],
6089 path->slots[level]);
6091 * if the lower level block was created before the snapshot
6092 * was created, we know there is no need to update back refs
6095 if (wc->stage == UPDATE_BACKREF &&
6096 generation <= root->root_key.offset) {
6101 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
6102 blocksize = btrfs_level_size(root, level - 1);
6104 next = btrfs_find_tree_block(root, bytenr, blocksize);
6106 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
6111 btrfs_tree_lock(next);
6112 btrfs_set_lock_blocking(next);
6114 ret = btrfs_lookup_extent_info(trans, root, bytenr, blocksize,
6115 &wc->refs[level - 1],
6116 &wc->flags[level - 1]);
6118 BUG_ON(wc->refs[level - 1] == 0);
6121 if (wc->stage == DROP_REFERENCE) {
6122 if (wc->refs[level - 1] > 1) {
6124 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6127 if (!wc->update_ref ||
6128 generation <= root->root_key.offset)
6131 btrfs_node_key_to_cpu(path->nodes[level], &key,
6132 path->slots[level]);
6133 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
6137 wc->stage = UPDATE_BACKREF;
6138 wc->shared_level = level - 1;
6142 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
6146 if (!btrfs_buffer_uptodate(next, generation)) {
6147 btrfs_tree_unlock(next);
6148 free_extent_buffer(next);
6154 if (reada && level == 1)
6155 reada_walk_down(trans, root, wc, path);
6156 next = read_tree_block(root, bytenr, blocksize, generation);
6159 btrfs_tree_lock(next);
6160 btrfs_set_lock_blocking(next);
6164 BUG_ON(level != btrfs_header_level(next));
6165 path->nodes[level] = next;
6166 path->slots[level] = 0;
6167 path->locks[level] = 1;
6173 wc->refs[level - 1] = 0;
6174 wc->flags[level - 1] = 0;
6175 if (wc->stage == DROP_REFERENCE) {
6176 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
6177 parent = path->nodes[level]->start;
6179 BUG_ON(root->root_key.objectid !=
6180 btrfs_header_owner(path->nodes[level]));
6184 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
6185 root->root_key.objectid, level - 1, 0);
6188 btrfs_tree_unlock(next);
6189 free_extent_buffer(next);
6195 * hepler to process tree block while walking up the tree.
6197 * when wc->stage == DROP_REFERENCE, this function drops
6198 * reference count on the block.
6200 * when wc->stage == UPDATE_BACKREF, this function changes
6201 * wc->stage back to DROP_REFERENCE if we changed wc->stage
6202 * to UPDATE_BACKREF previously while processing the block.
6204 * NOTE: return value 1 means we should stop walking up.
6206 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
6207 struct btrfs_root *root,
6208 struct btrfs_path *path,
6209 struct walk_control *wc)
6212 int level = wc->level;
6213 struct extent_buffer *eb = path->nodes[level];
6216 if (wc->stage == UPDATE_BACKREF) {
6217 BUG_ON(wc->shared_level < level);
6218 if (level < wc->shared_level)
6221 ret = find_next_key(path, level + 1, &wc->update_progress);
6225 wc->stage = DROP_REFERENCE;
6226 wc->shared_level = -1;
6227 path->slots[level] = 0;
6230 * check reference count again if the block isn't locked.
6231 * we should start walking down the tree again if reference
6234 if (!path->locks[level]) {
6236 btrfs_tree_lock(eb);
6237 btrfs_set_lock_blocking(eb);
6238 path->locks[level] = 1;
6240 ret = btrfs_lookup_extent_info(trans, root,
6245 BUG_ON(wc->refs[level] == 0);
6246 if (wc->refs[level] == 1) {
6247 btrfs_tree_unlock(eb);
6248 path->locks[level] = 0;
6254 /* wc->stage == DROP_REFERENCE */
6255 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
6257 if (wc->refs[level] == 1) {
6259 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6260 ret = btrfs_dec_ref(trans, root, eb, 1);
6262 ret = btrfs_dec_ref(trans, root, eb, 0);
6265 /* make block locked assertion in clean_tree_block happy */
6266 if (!path->locks[level] &&
6267 btrfs_header_generation(eb) == trans->transid) {
6268 btrfs_tree_lock(eb);
6269 btrfs_set_lock_blocking(eb);
6270 path->locks[level] = 1;
6272 clean_tree_block(trans, root, eb);
6275 if (eb == root->node) {
6276 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6279 BUG_ON(root->root_key.objectid !=
6280 btrfs_header_owner(eb));
6282 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
6283 parent = path->nodes[level + 1]->start;
6285 BUG_ON(root->root_key.objectid !=
6286 btrfs_header_owner(path->nodes[level + 1]));
6289 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
6291 wc->refs[level] = 0;
6292 wc->flags[level] = 0;
6296 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
6297 struct btrfs_root *root,
6298 struct btrfs_path *path,
6299 struct walk_control *wc)
6301 int level = wc->level;
6302 int lookup_info = 1;
6305 while (level >= 0) {
6306 ret = walk_down_proc(trans, root, path, wc, lookup_info);
6313 if (path->slots[level] >=
6314 btrfs_header_nritems(path->nodes[level]))
6317 ret = do_walk_down(trans, root, path, wc, &lookup_info);
6319 path->slots[level]++;
6328 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
6329 struct btrfs_root *root,
6330 struct btrfs_path *path,
6331 struct walk_control *wc, int max_level)
6333 int level = wc->level;
6336 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
6337 while (level < max_level && path->nodes[level]) {
6339 if (path->slots[level] + 1 <
6340 btrfs_header_nritems(path->nodes[level])) {
6341 path->slots[level]++;
6344 ret = walk_up_proc(trans, root, path, wc);
6348 if (path->locks[level]) {
6349 btrfs_tree_unlock(path->nodes[level]);
6350 path->locks[level] = 0;
6352 free_extent_buffer(path->nodes[level]);
6353 path->nodes[level] = NULL;
6361 * drop a subvolume tree.
6363 * this function traverses the tree freeing any blocks that only
6364 * referenced by the tree.
6366 * when a shared tree block is found. this function decreases its
6367 * reference count by one. if update_ref is true, this function
6368 * also make sure backrefs for the shared block and all lower level
6369 * blocks are properly updated.
6371 int btrfs_drop_snapshot(struct btrfs_root *root,
6372 struct btrfs_block_rsv *block_rsv, int update_ref)
6374 struct btrfs_path *path;
6375 struct btrfs_trans_handle *trans;
6376 struct btrfs_root *tree_root = root->fs_info->tree_root;
6377 struct btrfs_root_item *root_item = &root->root_item;
6378 struct walk_control *wc;
6379 struct btrfs_key key;
6384 path = btrfs_alloc_path();
6387 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6390 trans = btrfs_start_transaction(tree_root, 0);
6391 BUG_ON(IS_ERR(trans));
6394 trans->block_rsv = block_rsv;
6396 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
6397 level = btrfs_header_level(root->node);
6398 path->nodes[level] = btrfs_lock_root_node(root);
6399 btrfs_set_lock_blocking(path->nodes[level]);
6400 path->slots[level] = 0;
6401 path->locks[level] = 1;
6402 memset(&wc->update_progress, 0,
6403 sizeof(wc->update_progress));
6405 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
6406 memcpy(&wc->update_progress, &key,
6407 sizeof(wc->update_progress));
6409 level = root_item->drop_level;
6411 path->lowest_level = level;
6412 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6413 path->lowest_level = 0;
6421 * unlock our path, this is safe because only this
6422 * function is allowed to delete this snapshot
6424 btrfs_unlock_up_safe(path, 0);
6426 level = btrfs_header_level(root->node);
6428 btrfs_tree_lock(path->nodes[level]);
6429 btrfs_set_lock_blocking(path->nodes[level]);
6431 ret = btrfs_lookup_extent_info(trans, root,
6432 path->nodes[level]->start,
6433 path->nodes[level]->len,
6437 BUG_ON(wc->refs[level] == 0);
6439 if (level == root_item->drop_level)
6442 btrfs_tree_unlock(path->nodes[level]);
6443 WARN_ON(wc->refs[level] != 1);
6449 wc->shared_level = -1;
6450 wc->stage = DROP_REFERENCE;
6451 wc->update_ref = update_ref;
6453 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6456 ret = walk_down_tree(trans, root, path, wc);
6462 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
6469 BUG_ON(wc->stage != DROP_REFERENCE);
6473 if (wc->stage == DROP_REFERENCE) {
6475 btrfs_node_key(path->nodes[level],
6476 &root_item->drop_progress,
6477 path->slots[level]);
6478 root_item->drop_level = level;
6481 BUG_ON(wc->level == 0);
6482 if (btrfs_should_end_transaction(trans, tree_root)) {
6483 ret = btrfs_update_root(trans, tree_root,
6488 btrfs_end_transaction_throttle(trans, tree_root);
6489 trans = btrfs_start_transaction(tree_root, 0);
6490 BUG_ON(IS_ERR(trans));
6492 trans->block_rsv = block_rsv;
6495 btrfs_release_path(path);
6498 ret = btrfs_del_root(trans, tree_root, &root->root_key);
6501 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
6502 ret = btrfs_find_last_root(tree_root, root->root_key.objectid,
6506 /* if we fail to delete the orphan item this time
6507 * around, it'll get picked up the next time.
6509 * The most common failure here is just -ENOENT.
6511 btrfs_del_orphan_item(trans, tree_root,
6512 root->root_key.objectid);
6516 if (root->in_radix) {
6517 btrfs_free_fs_root(tree_root->fs_info, root);
6519 free_extent_buffer(root->node);
6520 free_extent_buffer(root->commit_root);
6524 btrfs_end_transaction_throttle(trans, tree_root);
6526 btrfs_free_path(path);
6531 * drop subtree rooted at tree block 'node'.
6533 * NOTE: this function will unlock and release tree block 'node'
6535 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
6536 struct btrfs_root *root,
6537 struct extent_buffer *node,
6538 struct extent_buffer *parent)
6540 struct btrfs_path *path;
6541 struct walk_control *wc;
6547 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
6549 path = btrfs_alloc_path();
6553 wc = kzalloc(sizeof(*wc), GFP_NOFS);
6555 btrfs_free_path(path);
6559 btrfs_assert_tree_locked(parent);
6560 parent_level = btrfs_header_level(parent);
6561 extent_buffer_get(parent);
6562 path->nodes[parent_level] = parent;
6563 path->slots[parent_level] = btrfs_header_nritems(parent);
6565 btrfs_assert_tree_locked(node);
6566 level = btrfs_header_level(node);
6567 path->nodes[level] = node;
6568 path->slots[level] = 0;
6569 path->locks[level] = 1;
6571 wc->refs[parent_level] = 1;
6572 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
6574 wc->shared_level = -1;
6575 wc->stage = DROP_REFERENCE;
6578 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
6581 wret = walk_down_tree(trans, root, path, wc);
6587 wret = walk_up_tree(trans, root, path, wc, parent_level);
6595 btrfs_free_path(path);
6600 static unsigned long calc_ra(unsigned long start, unsigned long last,
6603 return min(last, start + nr - 1);
6606 static noinline int relocate_inode_pages(struct inode *inode, u64 start,
6611 unsigned long first_index;
6612 unsigned long last_index;
6615 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6616 struct file_ra_state *ra;
6617 struct btrfs_ordered_extent *ordered;
6618 unsigned int total_read = 0;
6619 unsigned int total_dirty = 0;
6622 ra = kzalloc(sizeof(*ra), GFP_NOFS);
6626 mutex_lock(&inode->i_mutex);
6627 first_index = start >> PAGE_CACHE_SHIFT;
6628 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
6630 /* make sure the dirty trick played by the caller work */
6631 ret = invalidate_inode_pages2_range(inode->i_mapping,
6632 first_index, last_index);
6636 file_ra_state_init(ra, inode->i_mapping);
6638 for (i = first_index ; i <= last_index; i++) {
6639 if (total_read % ra->ra_pages == 0) {
6640 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
6641 calc_ra(i, last_index, ra->ra_pages));
6645 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
6647 page = grab_cache_page(inode->i_mapping, i);
6652 if (!PageUptodate(page)) {
6653 btrfs_readpage(NULL, page);
6655 if (!PageUptodate(page)) {
6657 page_cache_release(page);
6662 wait_on_page_writeback(page);
6664 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
6665 page_end = page_start + PAGE_CACHE_SIZE - 1;
6666 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
6668 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6670 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6672 page_cache_release(page);
6673 btrfs_start_ordered_extent(inode, ordered, 1);
6674 btrfs_put_ordered_extent(ordered);
6677 set_page_extent_mapped(page);
6679 if (i == first_index)
6680 set_extent_bits(io_tree, page_start, page_end,
6681 EXTENT_BOUNDARY, GFP_NOFS);
6682 btrfs_set_extent_delalloc(inode, page_start, page_end);
6684 set_page_dirty(page);
6687 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
6689 page_cache_release(page);
6694 mutex_unlock(&inode->i_mutex);
6695 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
6699 static noinline int relocate_data_extent(struct inode *reloc_inode,
6700 struct btrfs_key *extent_key,
6703 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6704 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
6705 struct extent_map *em;
6706 u64 start = extent_key->objectid - offset;
6707 u64 end = start + extent_key->offset - 1;
6709 em = alloc_extent_map();
6713 em->len = extent_key->offset;
6714 em->block_len = extent_key->offset;
6715 em->block_start = extent_key->objectid;
6716 em->bdev = root->fs_info->fs_devices->latest_bdev;
6717 set_bit(EXTENT_FLAG_PINNED, &em->flags);
6719 /* setup extent map to cheat btrfs_readpage */
6720 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6723 write_lock(&em_tree->lock);
6724 ret = add_extent_mapping(em_tree, em);
6725 write_unlock(&em_tree->lock);
6726 if (ret != -EEXIST) {
6727 free_extent_map(em);
6730 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
6732 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
6734 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
6737 struct btrfs_ref_path {
6739 u64 nodes[BTRFS_MAX_LEVEL];
6741 u64 root_generation;
6748 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
6749 u64 new_nodes[BTRFS_MAX_LEVEL];
6752 struct disk_extent {
6763 static int is_cowonly_root(u64 root_objectid)
6765 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
6766 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
6767 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
6768 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
6769 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
6770 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
6775 static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
6776 struct btrfs_root *extent_root,
6777 struct btrfs_ref_path *ref_path,
6780 struct extent_buffer *leaf;
6781 struct btrfs_path *path;
6782 struct btrfs_extent_ref *ref;
6783 struct btrfs_key key;
6784 struct btrfs_key found_key;
6790 path = btrfs_alloc_path();
6795 ref_path->lowest_level = -1;
6796 ref_path->current_level = -1;
6797 ref_path->shared_level = -1;
6801 level = ref_path->current_level - 1;
6802 while (level >= -1) {
6804 if (level < ref_path->lowest_level)
6808 bytenr = ref_path->nodes[level];
6810 bytenr = ref_path->extent_start;
6811 BUG_ON(bytenr == 0);
6813 parent = ref_path->nodes[level + 1];
6814 ref_path->nodes[level + 1] = 0;
6815 ref_path->current_level = level;
6816 BUG_ON(parent == 0);
6818 key.objectid = bytenr;
6819 key.offset = parent + 1;
6820 key.type = BTRFS_EXTENT_REF_KEY;
6822 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6827 leaf = path->nodes[0];
6828 nritems = btrfs_header_nritems(leaf);
6829 if (path->slots[0] >= nritems) {
6830 ret = btrfs_next_leaf(extent_root, path);
6835 leaf = path->nodes[0];
6838 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6839 if (found_key.objectid == bytenr &&
6840 found_key.type == BTRFS_EXTENT_REF_KEY) {
6841 if (level < ref_path->shared_level)
6842 ref_path->shared_level = level;
6847 btrfs_release_path(extent_root, path);
6850 /* reached lowest level */
6854 level = ref_path->current_level;
6855 while (level < BTRFS_MAX_LEVEL - 1) {
6859 bytenr = ref_path->nodes[level];
6861 bytenr = ref_path->extent_start;
6863 BUG_ON(bytenr == 0);
6865 key.objectid = bytenr;
6867 key.type = BTRFS_EXTENT_REF_KEY;
6869 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
6873 leaf = path->nodes[0];
6874 nritems = btrfs_header_nritems(leaf);
6875 if (path->slots[0] >= nritems) {
6876 ret = btrfs_next_leaf(extent_root, path);
6880 /* the extent was freed by someone */
6881 if (ref_path->lowest_level == level)
6883 btrfs_release_path(extent_root, path);
6886 leaf = path->nodes[0];
6889 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6890 if (found_key.objectid != bytenr ||
6891 found_key.type != BTRFS_EXTENT_REF_KEY) {
6892 /* the extent was freed by someone */
6893 if (ref_path->lowest_level == level) {
6897 btrfs_release_path(extent_root, path);
6901 ref = btrfs_item_ptr(leaf, path->slots[0],
6902 struct btrfs_extent_ref);
6903 ref_objectid = btrfs_ref_objectid(leaf, ref);
6904 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
6906 level = (int)ref_objectid;
6907 BUG_ON(level >= BTRFS_MAX_LEVEL);
6908 ref_path->lowest_level = level;
6909 ref_path->current_level = level;
6910 ref_path->nodes[level] = bytenr;
6912 WARN_ON(ref_objectid != level);
6915 WARN_ON(level != -1);
6919 if (ref_path->lowest_level == level) {
6920 ref_path->owner_objectid = ref_objectid;
6921 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
6925 * the block is tree root or the block isn't in reference
6928 if (found_key.objectid == found_key.offset ||
6929 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
6930 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6931 ref_path->root_generation =
6932 btrfs_ref_generation(leaf, ref);
6934 /* special reference from the tree log */
6935 ref_path->nodes[0] = found_key.offset;
6936 ref_path->current_level = 0;
6943 BUG_ON(ref_path->nodes[level] != 0);
6944 ref_path->nodes[level] = found_key.offset;
6945 ref_path->current_level = level;
6948 * the reference was created in the running transaction,
6949 * no need to continue walking up.
6951 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
6952 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
6953 ref_path->root_generation =
6954 btrfs_ref_generation(leaf, ref);
6959 btrfs_release_path(extent_root, path);
6962 /* reached max tree level, but no tree root found. */
6965 btrfs_free_path(path);
6969 static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
6970 struct btrfs_root *extent_root,
6971 struct btrfs_ref_path *ref_path,
6974 memset(ref_path, 0, sizeof(*ref_path));
6975 ref_path->extent_start = extent_start;
6977 return __next_ref_path(trans, extent_root, ref_path, 1);
6980 static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
6981 struct btrfs_root *extent_root,
6982 struct btrfs_ref_path *ref_path)
6984 return __next_ref_path(trans, extent_root, ref_path, 0);
6987 static noinline int get_new_locations(struct inode *reloc_inode,
6988 struct btrfs_key *extent_key,
6989 u64 offset, int no_fragment,
6990 struct disk_extent **extents,
6993 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
6994 struct btrfs_path *path;
6995 struct btrfs_file_extent_item *fi;
6996 struct extent_buffer *leaf;
6997 struct disk_extent *exts = *extents;
6998 struct btrfs_key found_key;
7003 int max = *nr_extents;
7006 WARN_ON(!no_fragment && *extents);
7009 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
7014 path = btrfs_alloc_path();
7016 if (exts != *extents)
7021 cur_pos = extent_key->objectid - offset;
7022 last_byte = extent_key->objectid + extent_key->offset;
7023 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
7033 leaf = path->nodes[0];
7034 nritems = btrfs_header_nritems(leaf);
7035 if (path->slots[0] >= nritems) {
7036 ret = btrfs_next_leaf(root, path);
7041 leaf = path->nodes[0];
7044 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
7045 if (found_key.offset != cur_pos ||
7046 found_key.type != BTRFS_EXTENT_DATA_KEY ||
7047 found_key.objectid != reloc_inode->i_ino)
7050 fi = btrfs_item_ptr(leaf, path->slots[0],
7051 struct btrfs_file_extent_item);
7052 if (btrfs_file_extent_type(leaf, fi) !=
7053 BTRFS_FILE_EXTENT_REG ||
7054 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7058 struct disk_extent *old = exts;
7060 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
7065 memcpy(exts, old, sizeof(*exts) * nr);
7066 if (old != *extents)
7070 exts[nr].disk_bytenr =
7071 btrfs_file_extent_disk_bytenr(leaf, fi);
7072 exts[nr].disk_num_bytes =
7073 btrfs_file_extent_disk_num_bytes(leaf, fi);
7074 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
7075 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7076 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7077 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
7078 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
7079 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
7081 BUG_ON(exts[nr].offset > 0);
7082 BUG_ON(exts[nr].compression || exts[nr].encryption);
7083 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
7085 cur_pos += exts[nr].num_bytes;
7088 if (cur_pos + offset >= last_byte)
7098 BUG_ON(cur_pos + offset > last_byte);
7099 if (cur_pos + offset < last_byte) {
7105 btrfs_free_path(path);
7107 if (exts != *extents)
7116 static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
7117 struct btrfs_root *root,
7118 struct btrfs_path *path,
7119 struct btrfs_key *extent_key,
7120 struct btrfs_key *leaf_key,
7121 struct btrfs_ref_path *ref_path,
7122 struct disk_extent *new_extents,
7125 struct extent_buffer *leaf;
7126 struct btrfs_file_extent_item *fi;
7127 struct inode *inode = NULL;
7128 struct btrfs_key key;
7133 u64 search_end = (u64)-1;
7136 int extent_locked = 0;
7140 memcpy(&key, leaf_key, sizeof(key));
7141 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7142 if (key.objectid < ref_path->owner_objectid ||
7143 (key.objectid == ref_path->owner_objectid &&
7144 key.type < BTRFS_EXTENT_DATA_KEY)) {
7145 key.objectid = ref_path->owner_objectid;
7146 key.type = BTRFS_EXTENT_DATA_KEY;
7152 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
7156 leaf = path->nodes[0];
7157 nritems = btrfs_header_nritems(leaf);
7159 if (extent_locked && ret > 0) {
7161 * the file extent item was modified by someone
7162 * before the extent got locked.
7164 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7165 lock_end, GFP_NOFS);
7169 if (path->slots[0] >= nritems) {
7170 if (++nr_scaned > 2)
7173 BUG_ON(extent_locked);
7174 ret = btrfs_next_leaf(root, path);
7179 leaf = path->nodes[0];
7180 nritems = btrfs_header_nritems(leaf);
7183 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
7185 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
7186 if ((key.objectid > ref_path->owner_objectid) ||
7187 (key.objectid == ref_path->owner_objectid &&
7188 key.type > BTRFS_EXTENT_DATA_KEY) ||
7189 key.offset >= search_end)
7193 if (inode && key.objectid != inode->i_ino) {
7194 BUG_ON(extent_locked);
7195 btrfs_release_path(root, path);
7196 mutex_unlock(&inode->i_mutex);
7202 if (key.type != BTRFS_EXTENT_DATA_KEY) {
7207 fi = btrfs_item_ptr(leaf, path->slots[0],
7208 struct btrfs_file_extent_item);
7209 extent_type = btrfs_file_extent_type(leaf, fi);
7210 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
7211 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
7212 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
7213 extent_key->objectid)) {
7219 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7220 ext_offset = btrfs_file_extent_offset(leaf, fi);
7222 if (search_end == (u64)-1) {
7223 search_end = key.offset - ext_offset +
7224 btrfs_file_extent_ram_bytes(leaf, fi);
7227 if (!extent_locked) {
7228 lock_start = key.offset;
7229 lock_end = lock_start + num_bytes - 1;
7231 if (lock_start > key.offset ||
7232 lock_end + 1 < key.offset + num_bytes) {
7233 unlock_extent(&BTRFS_I(inode)->io_tree,
7234 lock_start, lock_end, GFP_NOFS);
7240 btrfs_release_path(root, path);
7242 inode = btrfs_iget_locked(root->fs_info->sb,
7243 key.objectid, root);
7244 if (inode->i_state & I_NEW) {
7245 BTRFS_I(inode)->root = root;
7246 BTRFS_I(inode)->location.objectid =
7248 BTRFS_I(inode)->location.type =
7249 BTRFS_INODE_ITEM_KEY;
7250 BTRFS_I(inode)->location.offset = 0;
7251 btrfs_read_locked_inode(inode);
7252 unlock_new_inode(inode);
7255 * some code call btrfs_commit_transaction while
7256 * holding the i_mutex, so we can't use mutex_lock
7259 if (is_bad_inode(inode) ||
7260 !mutex_trylock(&inode->i_mutex)) {
7263 key.offset = (u64)-1;
7268 if (!extent_locked) {
7269 struct btrfs_ordered_extent *ordered;
7271 btrfs_release_path(root, path);
7273 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7274 lock_end, GFP_NOFS);
7275 ordered = btrfs_lookup_first_ordered_extent(inode,
7278 ordered->file_offset <= lock_end &&
7279 ordered->file_offset + ordered->len > lock_start) {
7280 unlock_extent(&BTRFS_I(inode)->io_tree,
7281 lock_start, lock_end, GFP_NOFS);
7282 btrfs_start_ordered_extent(inode, ordered, 1);
7283 btrfs_put_ordered_extent(ordered);
7284 key.offset += num_bytes;
7288 btrfs_put_ordered_extent(ordered);
7294 if (nr_extents == 1) {
7295 /* update extent pointer in place */
7296 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7297 new_extents[0].disk_bytenr);
7298 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7299 new_extents[0].disk_num_bytes);
7300 btrfs_mark_buffer_dirty(leaf);
7302 btrfs_drop_extent_cache(inode, key.offset,
7303 key.offset + num_bytes - 1, 0);
7305 ret = btrfs_inc_extent_ref(trans, root,
7306 new_extents[0].disk_bytenr,
7307 new_extents[0].disk_num_bytes,
7309 root->root_key.objectid,
7314 ret = btrfs_free_extent(trans, root,
7315 extent_key->objectid,
7318 btrfs_header_owner(leaf),
7319 btrfs_header_generation(leaf),
7323 btrfs_release_path(root, path);
7324 key.offset += num_bytes;
7332 * drop old extent pointer at first, then insert the
7333 * new pointers one bye one
7335 btrfs_release_path(root, path);
7336 ret = btrfs_drop_extents(trans, root, inode, key.offset,
7337 key.offset + num_bytes,
7338 key.offset, &alloc_hint);
7341 for (i = 0; i < nr_extents; i++) {
7342 if (ext_offset >= new_extents[i].num_bytes) {
7343 ext_offset -= new_extents[i].num_bytes;
7346 extent_len = min(new_extents[i].num_bytes -
7347 ext_offset, num_bytes);
7349 ret = btrfs_insert_empty_item(trans, root,
7354 leaf = path->nodes[0];
7355 fi = btrfs_item_ptr(leaf, path->slots[0],
7356 struct btrfs_file_extent_item);
7357 btrfs_set_file_extent_generation(leaf, fi,
7359 btrfs_set_file_extent_type(leaf, fi,
7360 BTRFS_FILE_EXTENT_REG);
7361 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7362 new_extents[i].disk_bytenr);
7363 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7364 new_extents[i].disk_num_bytes);
7365 btrfs_set_file_extent_ram_bytes(leaf, fi,
7366 new_extents[i].ram_bytes);
7368 btrfs_set_file_extent_compression(leaf, fi,
7369 new_extents[i].compression);
7370 btrfs_set_file_extent_encryption(leaf, fi,
7371 new_extents[i].encryption);
7372 btrfs_set_file_extent_other_encoding(leaf, fi,
7373 new_extents[i].other_encoding);
7375 btrfs_set_file_extent_num_bytes(leaf, fi,
7377 ext_offset += new_extents[i].offset;
7378 btrfs_set_file_extent_offset(leaf, fi,
7380 btrfs_mark_buffer_dirty(leaf);
7382 btrfs_drop_extent_cache(inode, key.offset,
7383 key.offset + extent_len - 1, 0);
7385 ret = btrfs_inc_extent_ref(trans, root,
7386 new_extents[i].disk_bytenr,
7387 new_extents[i].disk_num_bytes,
7389 root->root_key.objectid,
7390 trans->transid, key.objectid);
7392 btrfs_release_path(root, path);
7394 inode_add_bytes(inode, extent_len);
7397 num_bytes -= extent_len;
7398 key.offset += extent_len;
7403 BUG_ON(i >= nr_extents);
7407 if (extent_locked) {
7408 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7409 lock_end, GFP_NOFS);
7413 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
7414 key.offset >= search_end)
7421 btrfs_release_path(root, path);
7423 mutex_unlock(&inode->i_mutex);
7424 if (extent_locked) {
7425 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
7426 lock_end, GFP_NOFS);
7433 int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
7434 struct btrfs_root *root,
7435 struct extent_buffer *buf, u64 orig_start)
7440 BUG_ON(btrfs_header_generation(buf) != trans->transid);
7441 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
7443 level = btrfs_header_level(buf);
7445 struct btrfs_leaf_ref *ref;
7446 struct btrfs_leaf_ref *orig_ref;
7448 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
7452 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
7454 btrfs_free_leaf_ref(root, orig_ref);
7458 ref->nritems = orig_ref->nritems;
7459 memcpy(ref->extents, orig_ref->extents,
7460 sizeof(ref->extents[0]) * ref->nritems);
7462 btrfs_free_leaf_ref(root, orig_ref);
7464 ref->root_gen = trans->transid;
7465 ref->bytenr = buf->start;
7466 ref->owner = btrfs_header_owner(buf);
7467 ref->generation = btrfs_header_generation(buf);
7469 ret = btrfs_add_leaf_ref(root, ref, 0);
7471 btrfs_free_leaf_ref(root, ref);
7476 static noinline int invalidate_extent_cache(struct btrfs_root *root,
7477 struct extent_buffer *leaf,
7478 struct btrfs_block_group_cache *group,
7479 struct btrfs_root *target_root)
7481 struct btrfs_key key;
7482 struct inode *inode = NULL;
7483 struct btrfs_file_extent_item *fi;
7484 struct extent_state *cached_state = NULL;
7486 u64 skip_objectid = 0;
7490 nritems = btrfs_header_nritems(leaf);
7491 for (i = 0; i < nritems; i++) {
7492 btrfs_item_key_to_cpu(leaf, &key, i);
7493 if (key.objectid == skip_objectid ||
7494 key.type != BTRFS_EXTENT_DATA_KEY)
7496 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7497 if (btrfs_file_extent_type(leaf, fi) ==
7498 BTRFS_FILE_EXTENT_INLINE)
7500 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
7502 if (!inode || inode->i_ino != key.objectid) {
7504 inode = btrfs_ilookup(target_root->fs_info->sb,
7505 key.objectid, target_root, 1);
7508 skip_objectid = key.objectid;
7511 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
7513 lock_extent_bits(&BTRFS_I(inode)->io_tree, key.offset,
7514 key.offset + num_bytes - 1, 0, &cached_state,
7516 btrfs_drop_extent_cache(inode, key.offset,
7517 key.offset + num_bytes - 1, 1);
7518 unlock_extent_cached(&BTRFS_I(inode)->io_tree, key.offset,
7519 key.offset + num_bytes - 1, &cached_state,
7527 static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
7528 struct btrfs_root *root,
7529 struct extent_buffer *leaf,
7530 struct btrfs_block_group_cache *group,
7531 struct inode *reloc_inode)
7533 struct btrfs_key key;
7534 struct btrfs_key extent_key;
7535 struct btrfs_file_extent_item *fi;
7536 struct btrfs_leaf_ref *ref;
7537 struct disk_extent *new_extent;
7546 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
7550 ref = btrfs_lookup_leaf_ref(root, leaf->start);
7554 nritems = btrfs_header_nritems(leaf);
7555 for (i = 0; i < nritems; i++) {
7556 btrfs_item_key_to_cpu(leaf, &key, i);
7557 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
7559 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
7560 if (btrfs_file_extent_type(leaf, fi) ==
7561 BTRFS_FILE_EXTENT_INLINE)
7563 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7564 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
7569 if (bytenr >= group->key.objectid + group->key.offset ||
7570 bytenr + num_bytes <= group->key.objectid)
7573 extent_key.objectid = bytenr;
7574 extent_key.offset = num_bytes;
7575 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
7577 ret = get_new_locations(reloc_inode, &extent_key,
7578 group->key.objectid, 1,
7579 &new_extent, &nr_extent);
7584 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
7585 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
7586 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
7587 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
7589 btrfs_set_file_extent_disk_bytenr(leaf, fi,
7590 new_extent->disk_bytenr);
7591 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
7592 new_extent->disk_num_bytes);
7593 btrfs_mark_buffer_dirty(leaf);
7595 ret = btrfs_inc_extent_ref(trans, root,
7596 new_extent->disk_bytenr,
7597 new_extent->disk_num_bytes,
7599 root->root_key.objectid,
7600 trans->transid, key.objectid);
7603 ret = btrfs_free_extent(trans, root,
7604 bytenr, num_bytes, leaf->start,
7605 btrfs_header_owner(leaf),
7606 btrfs_header_generation(leaf),
7612 BUG_ON(ext_index + 1 != ref->nritems);
7613 btrfs_free_leaf_ref(root, ref);
7617 int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
7618 struct btrfs_root *root)
7620 struct btrfs_root *reloc_root;
7623 if (root->reloc_root) {
7624 reloc_root = root->reloc_root;
7625 root->reloc_root = NULL;
7626 list_add(&reloc_root->dead_list,
7627 &root->fs_info->dead_reloc_roots);
7629 btrfs_set_root_bytenr(&reloc_root->root_item,
7630 reloc_root->node->start);
7631 btrfs_set_root_level(&root->root_item,
7632 btrfs_header_level(reloc_root->node));
7633 memset(&reloc_root->root_item.drop_progress, 0,
7634 sizeof(struct btrfs_disk_key));
7635 reloc_root->root_item.drop_level = 0;
7637 ret = btrfs_update_root(trans, root->fs_info->tree_root,
7638 &reloc_root->root_key,
7639 &reloc_root->root_item);
7645 int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
7647 struct btrfs_trans_handle *trans;
7648 struct btrfs_root *reloc_root;
7649 struct btrfs_root *prev_root = NULL;
7650 struct list_head dead_roots;
7654 INIT_LIST_HEAD(&dead_roots);
7655 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
7657 while (!list_empty(&dead_roots)) {
7658 reloc_root = list_entry(dead_roots.prev,
7659 struct btrfs_root, dead_list);
7660 list_del_init(&reloc_root->dead_list);
7662 BUG_ON(reloc_root->commit_root != NULL);
7664 trans = btrfs_join_transaction(root, 1);
7665 BUG_ON(IS_ERR(trans));
7667 mutex_lock(&root->fs_info->drop_mutex);
7668 ret = btrfs_drop_snapshot(trans, reloc_root);
7671 mutex_unlock(&root->fs_info->drop_mutex);
7673 nr = trans->blocks_used;
7674 ret = btrfs_end_transaction(trans, root);
7676 btrfs_btree_balance_dirty(root, nr);
7679 free_extent_buffer(reloc_root->node);
7681 ret = btrfs_del_root(trans, root->fs_info->tree_root,
7682 &reloc_root->root_key);
7684 mutex_unlock(&root->fs_info->drop_mutex);
7686 nr = trans->blocks_used;
7687 ret = btrfs_end_transaction(trans, root);
7689 btrfs_btree_balance_dirty(root, nr);
7692 prev_root = reloc_root;
7695 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
7701 int btrfs_add_dead_reloc_root(struct btrfs_root *root)
7703 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
7707 int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
7709 struct btrfs_root *reloc_root;
7710 struct btrfs_trans_handle *trans;
7711 struct btrfs_key location;
7715 mutex_lock(&root->fs_info->tree_reloc_mutex);
7716 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
7718 found = !list_empty(&root->fs_info->dead_reloc_roots);
7719 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7722 trans = btrfs_start_transaction(root, 1);
7723 BUG_ON(IS_ERR(trans));
7724 ret = btrfs_commit_transaction(trans, root);
7728 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
7729 location.offset = (u64)-1;
7730 location.type = BTRFS_ROOT_ITEM_KEY;
7732 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
7733 BUG_ON(!reloc_root);
7734 ret = btrfs_orphan_cleanup(reloc_root);
7739 static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
7740 struct btrfs_root *root)
7742 struct btrfs_root *reloc_root;
7743 struct extent_buffer *eb;
7744 struct btrfs_root_item *root_item;
7745 struct btrfs_key root_key;
7748 BUG_ON(!root->ref_cows);
7749 if (root->reloc_root)
7752 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
7756 ret = btrfs_copy_root(trans, root, root->commit_root,
7757 &eb, BTRFS_TREE_RELOC_OBJECTID);
7760 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
7761 root_key.offset = root->root_key.objectid;
7762 root_key.type = BTRFS_ROOT_ITEM_KEY;
7764 memcpy(root_item, &root->root_item, sizeof(root_item));
7765 btrfs_set_root_refs(root_item, 0);
7766 btrfs_set_root_bytenr(root_item, eb->start);
7767 btrfs_set_root_level(root_item, btrfs_header_level(eb));
7768 btrfs_set_root_generation(root_item, trans->transid);
7770 btrfs_tree_unlock(eb);
7771 free_extent_buffer(eb);
7773 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
7774 &root_key, root_item);
7778 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
7780 BUG_ON(IS_ERR(reloc_root));
7781 reloc_root->last_trans = trans->transid;
7782 reloc_root->commit_root = NULL;
7783 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
7785 root->reloc_root = reloc_root;
7790 * Core function of space balance.
7792 * The idea is using reloc trees to relocate tree blocks in reference
7793 * counted roots. There is one reloc tree for each subvol, and all
7794 * reloc trees share same root key objectid. Reloc trees are snapshots
7795 * of the latest committed roots of subvols (root->commit_root).
7797 * To relocate a tree block referenced by a subvol, there are two steps.
7798 * COW the block through subvol's reloc tree, then update block pointer
7799 * in the subvol to point to the new block. Since all reloc trees share
7800 * same root key objectid, doing special handing for tree blocks owned
7801 * by them is easy. Once a tree block has been COWed in one reloc tree,
7802 * we can use the resulting new block directly when the same block is
7803 * required to COW again through other reloc trees. By this way, relocated
7804 * tree blocks are shared between reloc trees, so they are also shared
7807 static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
7808 struct btrfs_root *root,
7809 struct btrfs_path *path,
7810 struct btrfs_key *first_key,
7811 struct btrfs_ref_path *ref_path,
7812 struct btrfs_block_group_cache *group,
7813 struct inode *reloc_inode)
7815 struct btrfs_root *reloc_root;
7816 struct extent_buffer *eb = NULL;
7817 struct btrfs_key *keys;
7821 int lowest_level = 0;
7824 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
7825 lowest_level = ref_path->owner_objectid;
7827 if (!root->ref_cows) {
7828 path->lowest_level = lowest_level;
7829 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
7831 path->lowest_level = 0;
7832 btrfs_release_path(root, path);
7836 mutex_lock(&root->fs_info->tree_reloc_mutex);
7837 ret = init_reloc_tree(trans, root);
7839 reloc_root = root->reloc_root;
7841 shared_level = ref_path->shared_level;
7842 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
7844 keys = ref_path->node_keys;
7845 nodes = ref_path->new_nodes;
7846 memset(&keys[shared_level + 1], 0,
7847 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
7848 memset(&nodes[shared_level + 1], 0,
7849 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
7851 if (nodes[lowest_level] == 0) {
7852 path->lowest_level = lowest_level;
7853 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7856 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
7857 eb = path->nodes[level];
7858 if (!eb || eb == reloc_root->node)
7860 nodes[level] = eb->start;
7862 btrfs_item_key_to_cpu(eb, &keys[level], 0);
7864 btrfs_node_key_to_cpu(eb, &keys[level], 0);
7867 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7868 eb = path->nodes[0];
7869 ret = replace_extents_in_leaf(trans, reloc_root, eb,
7870 group, reloc_inode);
7873 btrfs_release_path(reloc_root, path);
7875 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
7881 * replace tree blocks in the fs tree with tree blocks in
7884 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
7887 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
7888 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
7891 extent_buffer_get(path->nodes[0]);
7892 eb = path->nodes[0];
7893 btrfs_release_path(reloc_root, path);
7894 ret = invalidate_extent_cache(reloc_root, eb, group, root);
7896 free_extent_buffer(eb);
7899 mutex_unlock(&root->fs_info->tree_reloc_mutex);
7900 path->lowest_level = 0;
7904 static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
7905 struct btrfs_root *root,
7906 struct btrfs_path *path,
7907 struct btrfs_key *first_key,
7908 struct btrfs_ref_path *ref_path)
7912 ret = relocate_one_path(trans, root, path, first_key,
7913 ref_path, NULL, NULL);
7919 static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
7920 struct btrfs_root *extent_root,
7921 struct btrfs_path *path,
7922 struct btrfs_key *extent_key)
7926 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
7929 ret = btrfs_del_item(trans, extent_root, path);
7931 btrfs_release_path(extent_root, path);
7935 static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
7936 struct btrfs_ref_path *ref_path)
7938 struct btrfs_key root_key;
7940 root_key.objectid = ref_path->root_objectid;
7941 root_key.type = BTRFS_ROOT_ITEM_KEY;
7942 if (is_cowonly_root(ref_path->root_objectid))
7943 root_key.offset = 0;
7945 root_key.offset = (u64)-1;
7947 return btrfs_read_fs_root_no_name(fs_info, &root_key);
7950 static noinline int relocate_one_extent(struct btrfs_root *extent_root,
7951 struct btrfs_path *path,
7952 struct btrfs_key *extent_key,
7953 struct btrfs_block_group_cache *group,
7954 struct inode *reloc_inode, int pass)
7956 struct btrfs_trans_handle *trans;
7957 struct btrfs_root *found_root;
7958 struct btrfs_ref_path *ref_path = NULL;
7959 struct disk_extent *new_extents = NULL;
7964 struct btrfs_key first_key;
7968 trans = btrfs_start_transaction(extent_root, 1);
7969 BUG_ON(IS_ERR(trans));
7971 if (extent_key->objectid == 0) {
7972 ret = del_extent_zero(trans, extent_root, path, extent_key);
7976 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
7982 for (loops = 0; ; loops++) {
7984 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
7985 extent_key->objectid);
7987 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
7994 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
7995 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
7998 found_root = read_ref_root(extent_root->fs_info, ref_path);
7999 BUG_ON(!found_root);
8001 * for reference counted tree, only process reference paths
8002 * rooted at the latest committed root.
8004 if (found_root->ref_cows &&
8005 ref_path->root_generation != found_root->root_key.offset)
8008 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8011 * copy data extents to new locations
8013 u64 group_start = group->key.objectid;
8014 ret = relocate_data_extent(reloc_inode,
8023 level = ref_path->owner_objectid;
8026 if (prev_block != ref_path->nodes[level]) {
8027 struct extent_buffer *eb;
8028 u64 block_start = ref_path->nodes[level];
8029 u64 block_size = btrfs_level_size(found_root, level);
8031 eb = read_tree_block(found_root, block_start,
8037 btrfs_tree_lock(eb);
8038 BUG_ON(level != btrfs_header_level(eb));
8041 btrfs_item_key_to_cpu(eb, &first_key, 0);
8043 btrfs_node_key_to_cpu(eb, &first_key, 0);
8045 btrfs_tree_unlock(eb);
8046 free_extent_buffer(eb);
8047 prev_block = block_start;
8050 mutex_lock(&extent_root->fs_info->trans_mutex);
8051 btrfs_record_root_in_trans(found_root);
8052 mutex_unlock(&extent_root->fs_info->trans_mutex);
8053 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
8055 * try to update data extent references while
8056 * keeping metadata shared between snapshots.
8059 ret = relocate_one_path(trans, found_root,
8060 path, &first_key, ref_path,
8061 group, reloc_inode);
8067 * use fallback method to process the remaining
8071 u64 group_start = group->key.objectid;
8072 new_extents = kmalloc(sizeof(*new_extents),
8079 ret = get_new_locations(reloc_inode,
8087 ret = replace_one_extent(trans, found_root,
8089 &first_key, ref_path,
8090 new_extents, nr_extents);
8092 ret = relocate_tree_block(trans, found_root, path,
8093 &first_key, ref_path);
8100 btrfs_end_transaction(trans, extent_root);
8107 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8110 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8111 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8114 * we add in the count of missing devices because we want
8115 * to make sure that any RAID levels on a degraded FS
8116 * continue to be honored.
8118 num_devices = root->fs_info->fs_devices->rw_devices +
8119 root->fs_info->fs_devices->missing_devices;
8121 if (num_devices == 1) {
8122 stripped |= BTRFS_BLOCK_GROUP_DUP;
8123 stripped = flags & ~stripped;
8125 /* turn raid0 into single device chunks */
8126 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8129 /* turn mirroring into duplication */
8130 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8131 BTRFS_BLOCK_GROUP_RAID10))
8132 return stripped | BTRFS_BLOCK_GROUP_DUP;
8135 /* they already had raid on here, just return */
8136 if (flags & stripped)
8139 stripped |= BTRFS_BLOCK_GROUP_DUP;
8140 stripped = flags & ~stripped;
8142 /* switch duplicated blocks with raid1 */
8143 if (flags & BTRFS_BLOCK_GROUP_DUP)
8144 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8146 /* turn single device chunks into raid0 */
8147 return stripped | BTRFS_BLOCK_GROUP_RAID0;
8152 static int set_block_group_ro(struct btrfs_block_group_cache *cache)
8154 struct btrfs_space_info *sinfo = cache->space_info;
8161 spin_lock(&sinfo->lock);
8162 spin_lock(&cache->lock);
8163 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8164 cache->bytes_super - btrfs_block_group_used(&cache->item);
8166 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8167 sinfo->bytes_may_use + sinfo->bytes_readonly +
8168 cache->reserved_pinned + num_bytes <= sinfo->total_bytes) {
8169 sinfo->bytes_readonly += num_bytes;
8170 sinfo->bytes_reserved += cache->reserved_pinned;
8171 cache->reserved_pinned = 0;
8176 spin_unlock(&cache->lock);
8177 spin_unlock(&sinfo->lock);
8181 int btrfs_set_block_group_ro(struct btrfs_root *root,
8182 struct btrfs_block_group_cache *cache)
8185 struct btrfs_trans_handle *trans;
8191 trans = btrfs_join_transaction(root, 1);
8192 BUG_ON(IS_ERR(trans));
8194 alloc_flags = update_block_group_flags(root, cache->flags);
8195 if (alloc_flags != cache->flags)
8196 do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8199 ret = set_block_group_ro(cache);
8202 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8203 ret = do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8207 ret = set_block_group_ro(cache);
8209 btrfs_end_transaction(trans, root);
8213 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8214 struct btrfs_root *root, u64 type)
8216 u64 alloc_flags = get_alloc_profile(root, type);
8217 return do_chunk_alloc(trans, root, 2 * 1024 * 1024, alloc_flags,
8222 * helper to account the unused space of all the readonly block group in the
8223 * list. takes mirrors into account.
8225 static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8227 struct btrfs_block_group_cache *block_group;
8231 list_for_each_entry(block_group, groups_list, list) {
8232 spin_lock(&block_group->lock);
8234 if (!block_group->ro) {
8235 spin_unlock(&block_group->lock);
8239 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8240 BTRFS_BLOCK_GROUP_RAID10 |
8241 BTRFS_BLOCK_GROUP_DUP))
8246 free_bytes += (block_group->key.offset -
8247 btrfs_block_group_used(&block_group->item)) *
8250 spin_unlock(&block_group->lock);
8257 * helper to account the unused space of all the readonly block group in the
8258 * space_info. takes mirrors into account.
8260 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8265 spin_lock(&sinfo->lock);
8267 for(i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8268 if (!list_empty(&sinfo->block_groups[i]))
8269 free_bytes += __btrfs_get_ro_block_group_free_space(
8270 &sinfo->block_groups[i]);
8272 spin_unlock(&sinfo->lock);
8277 int btrfs_set_block_group_rw(struct btrfs_root *root,
8278 struct btrfs_block_group_cache *cache)
8280 struct btrfs_space_info *sinfo = cache->space_info;
8285 spin_lock(&sinfo->lock);
8286 spin_lock(&cache->lock);
8287 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8288 cache->bytes_super - btrfs_block_group_used(&cache->item);
8289 sinfo->bytes_readonly -= num_bytes;
8291 spin_unlock(&cache->lock);
8292 spin_unlock(&sinfo->lock);
8297 * checks to see if its even possible to relocate this block group.
8299 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8300 * ok to go ahead and try.
8302 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
8304 struct btrfs_block_group_cache *block_group;
8305 struct btrfs_space_info *space_info;
8306 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8307 struct btrfs_device *device;
8311 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
8313 /* odd, couldn't find the block group, leave it alone */
8317 /* no bytes used, we're good */
8318 if (!btrfs_block_group_used(&block_group->item))
8321 space_info = block_group->space_info;
8322 spin_lock(&space_info->lock);
8324 full = space_info->full;
8327 * if this is the last block group we have in this space, we can't
8328 * relocate it unless we're able to allocate a new chunk below.
8330 * Otherwise, we need to make sure we have room in the space to handle
8331 * all of the extents from this block group. If we can, we're good
8333 if ((space_info->total_bytes != block_group->key.offset) &&
8334 (space_info->bytes_used + space_info->bytes_reserved +
8335 space_info->bytes_pinned + space_info->bytes_readonly +
8336 btrfs_block_group_used(&block_group->item) <
8337 space_info->total_bytes)) {
8338 spin_unlock(&space_info->lock);
8341 spin_unlock(&space_info->lock);
8344 * ok we don't have enough space, but maybe we have free space on our
8345 * devices to allocate new chunks for relocation, so loop through our
8346 * alloc devices and guess if we have enough space. However, if we
8347 * were marked as full, then we know there aren't enough chunks, and we
8354 mutex_lock(&root->fs_info->chunk_mutex);
8355 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8356 u64 min_free = btrfs_block_group_used(&block_group->item);
8360 * check to make sure we can actually find a chunk with enough
8361 * space to fit our block group in.
8363 if (device->total_bytes > device->bytes_used + min_free) {
8364 ret = find_free_dev_extent(NULL, device, min_free,
8371 mutex_unlock(&root->fs_info->chunk_mutex);
8373 btrfs_put_block_group(block_group);
8377 static int find_first_block_group(struct btrfs_root *root,
8378 struct btrfs_path *path, struct btrfs_key *key)
8381 struct btrfs_key found_key;
8382 struct extent_buffer *leaf;
8385 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8390 slot = path->slots[0];
8391 leaf = path->nodes[0];
8392 if (slot >= btrfs_header_nritems(leaf)) {
8393 ret = btrfs_next_leaf(root, path);
8400 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8402 if (found_key.objectid >= key->objectid &&
8403 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8413 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8415 struct btrfs_block_group_cache *block_group;
8419 struct inode *inode;
8421 block_group = btrfs_lookup_first_block_group(info, last);
8422 while (block_group) {
8423 spin_lock(&block_group->lock);
8424 if (block_group->iref)
8426 spin_unlock(&block_group->lock);
8427 block_group = next_block_group(info->tree_root,
8437 inode = block_group->inode;
8438 block_group->iref = 0;
8439 block_group->inode = NULL;
8440 spin_unlock(&block_group->lock);
8442 last = block_group->key.objectid + block_group->key.offset;
8443 btrfs_put_block_group(block_group);
8447 int btrfs_free_block_groups(struct btrfs_fs_info *info)
8449 struct btrfs_block_group_cache *block_group;
8450 struct btrfs_space_info *space_info;
8451 struct btrfs_caching_control *caching_ctl;
8454 down_write(&info->extent_commit_sem);
8455 while (!list_empty(&info->caching_block_groups)) {
8456 caching_ctl = list_entry(info->caching_block_groups.next,
8457 struct btrfs_caching_control, list);
8458 list_del(&caching_ctl->list);
8459 put_caching_control(caching_ctl);
8461 up_write(&info->extent_commit_sem);
8463 spin_lock(&info->block_group_cache_lock);
8464 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8465 block_group = rb_entry(n, struct btrfs_block_group_cache,
8467 rb_erase(&block_group->cache_node,
8468 &info->block_group_cache_tree);
8469 spin_unlock(&info->block_group_cache_lock);
8471 down_write(&block_group->space_info->groups_sem);
8472 list_del(&block_group->list);
8473 up_write(&block_group->space_info->groups_sem);
8475 if (block_group->cached == BTRFS_CACHE_STARTED)
8476 wait_block_group_cache_done(block_group);
8479 * We haven't cached this block group, which means we could
8480 * possibly have excluded extents on this block group.
8482 if (block_group->cached == BTRFS_CACHE_NO)
8483 free_excluded_extents(info->extent_root, block_group);
8485 btrfs_remove_free_space_cache(block_group);
8486 btrfs_put_block_group(block_group);
8488 spin_lock(&info->block_group_cache_lock);
8490 spin_unlock(&info->block_group_cache_lock);
8492 /* now that all the block groups are freed, go through and
8493 * free all the space_info structs. This is only called during
8494 * the final stages of unmount, and so we know nobody is
8495 * using them. We call synchronize_rcu() once before we start,
8496 * just to be on the safe side.
8500 release_global_block_rsv(info);
8502 while(!list_empty(&info->space_info)) {
8503 space_info = list_entry(info->space_info.next,
8504 struct btrfs_space_info,
8506 if (space_info->bytes_pinned > 0 ||
8507 space_info->bytes_reserved > 0) {
8509 dump_space_info(space_info, 0, 0);
8511 list_del(&space_info->list);
8517 static void __link_block_group(struct btrfs_space_info *space_info,
8518 struct btrfs_block_group_cache *cache)
8520 int index = get_block_group_index(cache);
8522 down_write(&space_info->groups_sem);
8523 list_add_tail(&cache->list, &space_info->block_groups[index]);
8524 up_write(&space_info->groups_sem);
8527 int btrfs_read_block_groups(struct btrfs_root *root)
8529 struct btrfs_path *path;
8531 struct btrfs_block_group_cache *cache;
8532 struct btrfs_fs_info *info = root->fs_info;
8533 struct btrfs_space_info *space_info;
8534 struct btrfs_key key;
8535 struct btrfs_key found_key;
8536 struct extent_buffer *leaf;
8540 root = info->extent_root;
8543 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
8544 path = btrfs_alloc_path();
8548 cache_gen = btrfs_super_cache_generation(&root->fs_info->super_copy);
8549 if (cache_gen != 0 &&
8550 btrfs_super_generation(&root->fs_info->super_copy) != cache_gen)
8552 if (btrfs_test_opt(root, CLEAR_CACHE))
8554 if (!btrfs_test_opt(root, SPACE_CACHE) && cache_gen)
8555 printk(KERN_INFO "btrfs: disk space caching is enabled\n");
8558 ret = find_first_block_group(root, path, &key);
8563 leaf = path->nodes[0];
8564 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
8565 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8571 atomic_set(&cache->count, 1);
8572 spin_lock_init(&cache->lock);
8573 spin_lock_init(&cache->tree_lock);
8574 cache->fs_info = info;
8575 INIT_LIST_HEAD(&cache->list);
8576 INIT_LIST_HEAD(&cache->cluster_list);
8579 cache->disk_cache_state = BTRFS_DC_CLEAR;
8582 * we only want to have 32k of ram per block group for keeping
8583 * track of free space, and if we pass 1/2 of that we want to
8584 * start converting things over to using bitmaps
8586 cache->extents_thresh = ((1024 * 32) / 2) /
8587 sizeof(struct btrfs_free_space);
8589 read_extent_buffer(leaf, &cache->item,
8590 btrfs_item_ptr_offset(leaf, path->slots[0]),
8591 sizeof(cache->item));
8592 memcpy(&cache->key, &found_key, sizeof(found_key));
8594 key.objectid = found_key.objectid + found_key.offset;
8595 btrfs_release_path(path);
8596 cache->flags = btrfs_block_group_flags(&cache->item);
8597 cache->sectorsize = root->sectorsize;
8600 * We need to exclude the super stripes now so that the space
8601 * info has super bytes accounted for, otherwise we'll think
8602 * we have more space than we actually do.
8604 exclude_super_stripes(root, cache);
8607 * check for two cases, either we are full, and therefore
8608 * don't need to bother with the caching work since we won't
8609 * find any space, or we are empty, and we can just add all
8610 * the space in and be done with it. This saves us _alot_ of
8611 * time, particularly in the full case.
8613 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
8614 cache->last_byte_to_unpin = (u64)-1;
8615 cache->cached = BTRFS_CACHE_FINISHED;
8616 free_excluded_extents(root, cache);
8617 } else if (btrfs_block_group_used(&cache->item) == 0) {
8618 cache->last_byte_to_unpin = (u64)-1;
8619 cache->cached = BTRFS_CACHE_FINISHED;
8620 add_new_free_space(cache, root->fs_info,
8622 found_key.objectid +
8624 free_excluded_extents(root, cache);
8627 ret = update_space_info(info, cache->flags, found_key.offset,
8628 btrfs_block_group_used(&cache->item),
8631 cache->space_info = space_info;
8632 spin_lock(&cache->space_info->lock);
8633 cache->space_info->bytes_readonly += cache->bytes_super;
8634 spin_unlock(&cache->space_info->lock);
8636 __link_block_group(space_info, cache);
8638 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8641 set_avail_alloc_bits(root->fs_info, cache->flags);
8642 if (btrfs_chunk_readonly(root, cache->key.objectid))
8643 set_block_group_ro(cache);
8646 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
8647 if (!(get_alloc_profile(root, space_info->flags) &
8648 (BTRFS_BLOCK_GROUP_RAID10 |
8649 BTRFS_BLOCK_GROUP_RAID1 |
8650 BTRFS_BLOCK_GROUP_DUP)))
8653 * avoid allocating from un-mirrored block group if there are
8654 * mirrored block groups.
8656 list_for_each_entry(cache, &space_info->block_groups[3], list)
8657 set_block_group_ro(cache);
8658 list_for_each_entry(cache, &space_info->block_groups[4], list)
8659 set_block_group_ro(cache);
8662 init_global_block_rsv(info);
8665 btrfs_free_path(path);
8669 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
8670 struct btrfs_root *root, u64 bytes_used,
8671 u64 type, u64 chunk_objectid, u64 chunk_offset,
8675 struct btrfs_root *extent_root;
8676 struct btrfs_block_group_cache *cache;
8678 extent_root = root->fs_info->extent_root;
8680 root->fs_info->last_trans_log_full_commit = trans->transid;
8682 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8686 cache->key.objectid = chunk_offset;
8687 cache->key.offset = size;
8688 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8689 cache->sectorsize = root->sectorsize;
8690 cache->fs_info = root->fs_info;
8693 * we only want to have 32k of ram per block group for keeping track
8694 * of free space, and if we pass 1/2 of that we want to start
8695 * converting things over to using bitmaps
8697 cache->extents_thresh = ((1024 * 32) / 2) /
8698 sizeof(struct btrfs_free_space);
8699 atomic_set(&cache->count, 1);
8700 spin_lock_init(&cache->lock);
8701 spin_lock_init(&cache->tree_lock);
8702 INIT_LIST_HEAD(&cache->list);
8703 INIT_LIST_HEAD(&cache->cluster_list);
8705 btrfs_set_block_group_used(&cache->item, bytes_used);
8706 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
8707 cache->flags = type;
8708 btrfs_set_block_group_flags(&cache->item, type);
8710 cache->last_byte_to_unpin = (u64)-1;
8711 cache->cached = BTRFS_CACHE_FINISHED;
8712 exclude_super_stripes(root, cache);
8714 add_new_free_space(cache, root->fs_info, chunk_offset,
8715 chunk_offset + size);
8717 free_excluded_extents(root, cache);
8719 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
8720 &cache->space_info);
8723 spin_lock(&cache->space_info->lock);
8724 cache->space_info->bytes_readonly += cache->bytes_super;
8725 spin_unlock(&cache->space_info->lock);
8727 __link_block_group(cache->space_info, cache);
8729 ret = btrfs_add_block_group_cache(root->fs_info, cache);
8732 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
8733 sizeof(cache->item));
8736 set_avail_alloc_bits(extent_root->fs_info, type);
8741 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
8742 struct btrfs_root *root, u64 group_start)
8744 struct btrfs_path *path;
8745 struct btrfs_block_group_cache *block_group;
8746 struct btrfs_free_cluster *cluster;
8747 struct btrfs_root *tree_root = root->fs_info->tree_root;
8748 struct btrfs_key key;
8749 struct inode *inode;
8753 root = root->fs_info->extent_root;
8755 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
8756 BUG_ON(!block_group);
8757 BUG_ON(!block_group->ro);
8760 * Free the reserved super bytes from this block group before
8763 free_excluded_extents(root, block_group);
8765 memcpy(&key, &block_group->key, sizeof(key));
8766 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
8767 BTRFS_BLOCK_GROUP_RAID1 |
8768 BTRFS_BLOCK_GROUP_RAID10))
8773 /* make sure this block group isn't part of an allocation cluster */
8774 cluster = &root->fs_info->data_alloc_cluster;
8775 spin_lock(&cluster->refill_lock);
8776 btrfs_return_cluster_to_free_space(block_group, cluster);
8777 spin_unlock(&cluster->refill_lock);
8780 * make sure this block group isn't part of a metadata
8781 * allocation cluster
8783 cluster = &root->fs_info->meta_alloc_cluster;
8784 spin_lock(&cluster->refill_lock);
8785 btrfs_return_cluster_to_free_space(block_group, cluster);
8786 spin_unlock(&cluster->refill_lock);
8788 path = btrfs_alloc_path();
8791 inode = lookup_free_space_inode(root, block_group, path);
8792 if (!IS_ERR(inode)) {
8793 btrfs_orphan_add(trans, inode);
8795 /* One for the block groups ref */
8796 spin_lock(&block_group->lock);
8797 if (block_group->iref) {
8798 block_group->iref = 0;
8799 block_group->inode = NULL;
8800 spin_unlock(&block_group->lock);
8803 spin_unlock(&block_group->lock);
8805 /* One for our lookup ref */
8809 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
8810 key.offset = block_group->key.objectid;
8813 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
8817 btrfs_release_path(path);
8819 ret = btrfs_del_item(trans, tree_root, path);
8822 btrfs_release_path(path);
8825 spin_lock(&root->fs_info->block_group_cache_lock);
8826 rb_erase(&block_group->cache_node,
8827 &root->fs_info->block_group_cache_tree);
8828 spin_unlock(&root->fs_info->block_group_cache_lock);
8830 down_write(&block_group->space_info->groups_sem);
8832 * we must use list_del_init so people can check to see if they
8833 * are still on the list after taking the semaphore
8835 list_del_init(&block_group->list);
8836 up_write(&block_group->space_info->groups_sem);
8838 if (block_group->cached == BTRFS_CACHE_STARTED)
8839 wait_block_group_cache_done(block_group);
8841 btrfs_remove_free_space_cache(block_group);
8843 spin_lock(&block_group->space_info->lock);
8844 block_group->space_info->total_bytes -= block_group->key.offset;
8845 block_group->space_info->bytes_readonly -= block_group->key.offset;
8846 block_group->space_info->disk_total -= block_group->key.offset * factor;
8847 spin_unlock(&block_group->space_info->lock);
8849 memcpy(&key, &block_group->key, sizeof(key));
8851 btrfs_clear_space_info_full(root->fs_info);
8853 btrfs_put_block_group(block_group);
8854 btrfs_put_block_group(block_group);
8856 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
8862 ret = btrfs_del_item(trans, root, path);
8864 btrfs_free_path(path);
8868 int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
8870 struct btrfs_space_info *space_info;
8873 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM, 0, 0,
8878 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA, 0, 0,
8883 ret = update_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA, 0, 0,
8891 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
8893 return unpin_extent_range(root, start, end);
8896 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
8897 u64 num_bytes, u64 *actual_bytes)
8899 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
8902 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
8904 struct btrfs_fs_info *fs_info = root->fs_info;
8905 struct btrfs_block_group_cache *cache = NULL;
8912 cache = btrfs_lookup_block_group(fs_info, range->start);
8915 if (cache->key.objectid >= (range->start + range->len)) {
8916 btrfs_put_block_group(cache);
8920 start = max(range->start, cache->key.objectid);
8921 end = min(range->start + range->len,
8922 cache->key.objectid + cache->key.offset);
8924 if (end - start >= range->minlen) {
8925 if (!block_group_cache_done(cache)) {
8926 ret = cache_block_group(cache, NULL, root, 0);
8928 wait_block_group_cache_done(cache);
8930 ret = btrfs_trim_block_group(cache,
8936 trimmed += group_trimmed;
8938 btrfs_put_block_group(cache);
8943 cache = next_block_group(fs_info->tree_root, cache);
8946 range->len = trimmed;