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>
26 #include <linux/ratelimit.h>
27 #include <linux/percpu_counter.h>
31 #include "print-tree.h"
35 #include "free-space-cache.h"
40 #undef SCRAMBLE_DELAYED_REFS
43 * control flags for do_chunk_alloc's force field
44 * CHUNK_ALLOC_NO_FORCE means to only allocate a chunk
45 * if we really need one.
47 * CHUNK_ALLOC_LIMITED means to only try and allocate one
48 * if we have very few chunks already allocated. This is
49 * used as part of the clustering code to help make sure
50 * we have a good pool of storage to cluster in, without
51 * filling the FS with empty chunks
53 * CHUNK_ALLOC_FORCE means it must try to allocate one
57 CHUNK_ALLOC_NO_FORCE = 0,
58 CHUNK_ALLOC_LIMITED = 1,
59 CHUNK_ALLOC_FORCE = 2,
63 * Control how reservations are dealt with.
65 * RESERVE_FREE - freeing a reservation.
66 * RESERVE_ALLOC - allocating space and we need to update bytes_may_use for
68 * RESERVE_ALLOC_NO_ACCOUNT - allocating space and we should not update
69 * bytes_may_use as the ENOSPC accounting is done elsewhere
74 RESERVE_ALLOC_NO_ACCOUNT = 2,
77 static int update_block_group(struct btrfs_root *root,
78 u64 bytenr, u64 num_bytes, int alloc);
79 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
80 struct btrfs_root *root,
81 u64 bytenr, u64 num_bytes, u64 parent,
82 u64 root_objectid, u64 owner_objectid,
83 u64 owner_offset, int refs_to_drop,
84 struct btrfs_delayed_extent_op *extra_op,
86 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
87 struct extent_buffer *leaf,
88 struct btrfs_extent_item *ei);
89 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
90 struct btrfs_root *root,
91 u64 parent, u64 root_objectid,
92 u64 flags, u64 owner, u64 offset,
93 struct btrfs_key *ins, int ref_mod);
94 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
95 struct btrfs_root *root,
96 u64 parent, u64 root_objectid,
97 u64 flags, struct btrfs_disk_key *key,
98 int level, struct btrfs_key *ins,
100 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
101 struct btrfs_root *extent_root, u64 flags,
103 static int find_next_key(struct btrfs_path *path, int level,
104 struct btrfs_key *key);
105 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
106 int dump_block_groups);
107 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
108 u64 num_bytes, int reserve,
110 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
112 int btrfs_pin_extent(struct btrfs_root *root,
113 u64 bytenr, u64 num_bytes, int reserved);
116 block_group_cache_done(struct btrfs_block_group_cache *cache)
119 return cache->cached == BTRFS_CACHE_FINISHED ||
120 cache->cached == BTRFS_CACHE_ERROR;
123 static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
125 return (cache->flags & bits) == bits;
128 static void btrfs_get_block_group(struct btrfs_block_group_cache *cache)
130 atomic_inc(&cache->count);
133 void btrfs_put_block_group(struct btrfs_block_group_cache *cache)
135 if (atomic_dec_and_test(&cache->count)) {
136 WARN_ON(cache->pinned > 0);
137 WARN_ON(cache->reserved > 0);
138 kfree(cache->free_space_ctl);
144 * this adds the block group to the fs_info rb tree for the block group
147 static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
148 struct btrfs_block_group_cache *block_group)
151 struct rb_node *parent = NULL;
152 struct btrfs_block_group_cache *cache;
154 spin_lock(&info->block_group_cache_lock);
155 p = &info->block_group_cache_tree.rb_node;
159 cache = rb_entry(parent, struct btrfs_block_group_cache,
161 if (block_group->key.objectid < cache->key.objectid) {
163 } else if (block_group->key.objectid > cache->key.objectid) {
166 spin_unlock(&info->block_group_cache_lock);
171 rb_link_node(&block_group->cache_node, parent, p);
172 rb_insert_color(&block_group->cache_node,
173 &info->block_group_cache_tree);
175 if (info->first_logical_byte > block_group->key.objectid)
176 info->first_logical_byte = block_group->key.objectid;
178 spin_unlock(&info->block_group_cache_lock);
184 * This will return the block group at or after bytenr if contains is 0, else
185 * it will return the block group that contains the bytenr
187 static struct btrfs_block_group_cache *
188 block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
191 struct btrfs_block_group_cache *cache, *ret = NULL;
195 spin_lock(&info->block_group_cache_lock);
196 n = info->block_group_cache_tree.rb_node;
199 cache = rb_entry(n, struct btrfs_block_group_cache,
201 end = cache->key.objectid + cache->key.offset - 1;
202 start = cache->key.objectid;
204 if (bytenr < start) {
205 if (!contains && (!ret || start < ret->key.objectid))
208 } else if (bytenr > start) {
209 if (contains && bytenr <= end) {
220 btrfs_get_block_group(ret);
221 if (bytenr == 0 && info->first_logical_byte > ret->key.objectid)
222 info->first_logical_byte = ret->key.objectid;
224 spin_unlock(&info->block_group_cache_lock);
229 static int add_excluded_extent(struct btrfs_root *root,
230 u64 start, u64 num_bytes)
232 u64 end = start + num_bytes - 1;
233 set_extent_bits(&root->fs_info->freed_extents[0],
234 start, end, EXTENT_UPTODATE, GFP_NOFS);
235 set_extent_bits(&root->fs_info->freed_extents[1],
236 start, end, EXTENT_UPTODATE, GFP_NOFS);
240 static void free_excluded_extents(struct btrfs_root *root,
241 struct btrfs_block_group_cache *cache)
245 start = cache->key.objectid;
246 end = start + cache->key.offset - 1;
248 clear_extent_bits(&root->fs_info->freed_extents[0],
249 start, end, EXTENT_UPTODATE, GFP_NOFS);
250 clear_extent_bits(&root->fs_info->freed_extents[1],
251 start, end, EXTENT_UPTODATE, GFP_NOFS);
254 static int exclude_super_stripes(struct btrfs_root *root,
255 struct btrfs_block_group_cache *cache)
262 if (cache->key.objectid < BTRFS_SUPER_INFO_OFFSET) {
263 stripe_len = BTRFS_SUPER_INFO_OFFSET - cache->key.objectid;
264 cache->bytes_super += stripe_len;
265 ret = add_excluded_extent(root, cache->key.objectid,
271 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
272 bytenr = btrfs_sb_offset(i);
273 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
274 cache->key.objectid, bytenr,
275 0, &logical, &nr, &stripe_len);
282 if (logical[nr] > cache->key.objectid +
286 if (logical[nr] + stripe_len <= cache->key.objectid)
290 if (start < cache->key.objectid) {
291 start = cache->key.objectid;
292 len = (logical[nr] + stripe_len) - start;
294 len = min_t(u64, stripe_len,
295 cache->key.objectid +
296 cache->key.offset - start);
299 cache->bytes_super += len;
300 ret = add_excluded_extent(root, start, len);
312 static struct btrfs_caching_control *
313 get_caching_control(struct btrfs_block_group_cache *cache)
315 struct btrfs_caching_control *ctl;
317 spin_lock(&cache->lock);
318 if (cache->cached != BTRFS_CACHE_STARTED) {
319 spin_unlock(&cache->lock);
323 /* We're loading it the fast way, so we don't have a caching_ctl. */
324 if (!cache->caching_ctl) {
325 spin_unlock(&cache->lock);
329 ctl = cache->caching_ctl;
330 atomic_inc(&ctl->count);
331 spin_unlock(&cache->lock);
335 static void put_caching_control(struct btrfs_caching_control *ctl)
337 if (atomic_dec_and_test(&ctl->count))
342 * this is only called by cache_block_group, since we could have freed extents
343 * we need to check the pinned_extents for any extents that can't be used yet
344 * since their free space will be released as soon as the transaction commits.
346 static u64 add_new_free_space(struct btrfs_block_group_cache *block_group,
347 struct btrfs_fs_info *info, u64 start, u64 end)
349 u64 extent_start, extent_end, size, total_added = 0;
352 while (start < end) {
353 ret = find_first_extent_bit(info->pinned_extents, start,
354 &extent_start, &extent_end,
355 EXTENT_DIRTY | EXTENT_UPTODATE,
360 if (extent_start <= start) {
361 start = extent_end + 1;
362 } else if (extent_start > start && extent_start < end) {
363 size = extent_start - start;
365 ret = btrfs_add_free_space(block_group, start,
367 BUG_ON(ret); /* -ENOMEM or logic error */
368 start = extent_end + 1;
377 ret = btrfs_add_free_space(block_group, start, size);
378 BUG_ON(ret); /* -ENOMEM or logic error */
384 static noinline void caching_thread(struct btrfs_work *work)
386 struct btrfs_block_group_cache *block_group;
387 struct btrfs_fs_info *fs_info;
388 struct btrfs_caching_control *caching_ctl;
389 struct btrfs_root *extent_root;
390 struct btrfs_path *path;
391 struct extent_buffer *leaf;
392 struct btrfs_key key;
398 caching_ctl = container_of(work, struct btrfs_caching_control, work);
399 block_group = caching_ctl->block_group;
400 fs_info = block_group->fs_info;
401 extent_root = fs_info->extent_root;
403 path = btrfs_alloc_path();
407 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
410 * We don't want to deadlock with somebody trying to allocate a new
411 * extent for the extent root while also trying to search the extent
412 * root to add free space. So we skip locking and search the commit
413 * root, since its read-only
415 path->skip_locking = 1;
416 path->search_commit_root = 1;
421 key.type = BTRFS_EXTENT_ITEM_KEY;
423 mutex_lock(&caching_ctl->mutex);
424 /* need to make sure the commit_root doesn't disappear */
425 down_read(&fs_info->commit_root_sem);
428 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
432 leaf = path->nodes[0];
433 nritems = btrfs_header_nritems(leaf);
436 if (btrfs_fs_closing(fs_info) > 1) {
441 if (path->slots[0] < nritems) {
442 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
444 ret = find_next_key(path, 0, &key);
448 if (need_resched() ||
449 rwsem_is_contended(&fs_info->commit_root_sem)) {
450 caching_ctl->progress = last;
451 btrfs_release_path(path);
452 up_read(&fs_info->commit_root_sem);
453 mutex_unlock(&caching_ctl->mutex);
458 ret = btrfs_next_leaf(extent_root, path);
463 leaf = path->nodes[0];
464 nritems = btrfs_header_nritems(leaf);
468 if (key.objectid < last) {
471 key.type = BTRFS_EXTENT_ITEM_KEY;
473 caching_ctl->progress = last;
474 btrfs_release_path(path);
478 if (key.objectid < block_group->key.objectid) {
483 if (key.objectid >= block_group->key.objectid +
484 block_group->key.offset)
487 if (key.type == BTRFS_EXTENT_ITEM_KEY ||
488 key.type == BTRFS_METADATA_ITEM_KEY) {
489 total_found += add_new_free_space(block_group,
492 if (key.type == BTRFS_METADATA_ITEM_KEY)
493 last = key.objectid +
494 fs_info->tree_root->leafsize;
496 last = key.objectid + key.offset;
498 if (total_found > (1024 * 1024 * 2)) {
500 wake_up(&caching_ctl->wait);
507 total_found += add_new_free_space(block_group, fs_info, last,
508 block_group->key.objectid +
509 block_group->key.offset);
510 caching_ctl->progress = (u64)-1;
512 spin_lock(&block_group->lock);
513 block_group->caching_ctl = NULL;
514 block_group->cached = BTRFS_CACHE_FINISHED;
515 spin_unlock(&block_group->lock);
518 btrfs_free_path(path);
519 up_read(&fs_info->commit_root_sem);
521 free_excluded_extents(extent_root, block_group);
523 mutex_unlock(&caching_ctl->mutex);
526 spin_lock(&block_group->lock);
527 block_group->caching_ctl = NULL;
528 block_group->cached = BTRFS_CACHE_ERROR;
529 spin_unlock(&block_group->lock);
531 wake_up(&caching_ctl->wait);
533 put_caching_control(caching_ctl);
534 btrfs_put_block_group(block_group);
537 static int cache_block_group(struct btrfs_block_group_cache *cache,
541 struct btrfs_fs_info *fs_info = cache->fs_info;
542 struct btrfs_caching_control *caching_ctl;
545 caching_ctl = kzalloc(sizeof(*caching_ctl), GFP_NOFS);
549 INIT_LIST_HEAD(&caching_ctl->list);
550 mutex_init(&caching_ctl->mutex);
551 init_waitqueue_head(&caching_ctl->wait);
552 caching_ctl->block_group = cache;
553 caching_ctl->progress = cache->key.objectid;
554 atomic_set(&caching_ctl->count, 1);
555 btrfs_init_work(&caching_ctl->work, caching_thread, NULL, NULL);
557 spin_lock(&cache->lock);
559 * This should be a rare occasion, but this could happen I think in the
560 * case where one thread starts to load the space cache info, and then
561 * some other thread starts a transaction commit which tries to do an
562 * allocation while the other thread is still loading the space cache
563 * info. The previous loop should have kept us from choosing this block
564 * group, but if we've moved to the state where we will wait on caching
565 * block groups we need to first check if we're doing a fast load here,
566 * so we can wait for it to finish, otherwise we could end up allocating
567 * from a block group who's cache gets evicted for one reason or
570 while (cache->cached == BTRFS_CACHE_FAST) {
571 struct btrfs_caching_control *ctl;
573 ctl = cache->caching_ctl;
574 atomic_inc(&ctl->count);
575 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
576 spin_unlock(&cache->lock);
580 finish_wait(&ctl->wait, &wait);
581 put_caching_control(ctl);
582 spin_lock(&cache->lock);
585 if (cache->cached != BTRFS_CACHE_NO) {
586 spin_unlock(&cache->lock);
590 WARN_ON(cache->caching_ctl);
591 cache->caching_ctl = caching_ctl;
592 cache->cached = BTRFS_CACHE_FAST;
593 spin_unlock(&cache->lock);
595 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
596 ret = load_free_space_cache(fs_info, cache);
598 spin_lock(&cache->lock);
600 cache->caching_ctl = NULL;
601 cache->cached = BTRFS_CACHE_FINISHED;
602 cache->last_byte_to_unpin = (u64)-1;
604 if (load_cache_only) {
605 cache->caching_ctl = NULL;
606 cache->cached = BTRFS_CACHE_NO;
608 cache->cached = BTRFS_CACHE_STARTED;
611 spin_unlock(&cache->lock);
612 wake_up(&caching_ctl->wait);
614 put_caching_control(caching_ctl);
615 free_excluded_extents(fs_info->extent_root, cache);
620 * We are not going to do the fast caching, set cached to the
621 * appropriate value and wakeup any waiters.
623 spin_lock(&cache->lock);
624 if (load_cache_only) {
625 cache->caching_ctl = NULL;
626 cache->cached = BTRFS_CACHE_NO;
628 cache->cached = BTRFS_CACHE_STARTED;
630 spin_unlock(&cache->lock);
631 wake_up(&caching_ctl->wait);
634 if (load_cache_only) {
635 put_caching_control(caching_ctl);
639 down_write(&fs_info->commit_root_sem);
640 atomic_inc(&caching_ctl->count);
641 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
642 up_write(&fs_info->commit_root_sem);
644 btrfs_get_block_group(cache);
646 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
652 * return the block group that starts at or after bytenr
654 static struct btrfs_block_group_cache *
655 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
657 struct btrfs_block_group_cache *cache;
659 cache = block_group_cache_tree_search(info, bytenr, 0);
665 * return the block group that contains the given bytenr
667 struct btrfs_block_group_cache *btrfs_lookup_block_group(
668 struct btrfs_fs_info *info,
671 struct btrfs_block_group_cache *cache;
673 cache = block_group_cache_tree_search(info, bytenr, 1);
678 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
681 struct list_head *head = &info->space_info;
682 struct btrfs_space_info *found;
684 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
687 list_for_each_entry_rcu(found, head, list) {
688 if (found->flags & flags) {
698 * after adding space to the filesystem, we need to clear the full flags
699 * on all the space infos.
701 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
703 struct list_head *head = &info->space_info;
704 struct btrfs_space_info *found;
707 list_for_each_entry_rcu(found, head, list)
712 /* simple helper to search for an existing extent at a given offset */
713 int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
716 struct btrfs_key key;
717 struct btrfs_path *path;
719 path = btrfs_alloc_path();
723 key.objectid = start;
725 key.type = BTRFS_EXTENT_ITEM_KEY;
726 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
729 btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
730 if (key.objectid == start &&
731 key.type == BTRFS_METADATA_ITEM_KEY)
734 btrfs_free_path(path);
739 * helper function to lookup reference count and flags of a tree block.
741 * the head node for delayed ref is used to store the sum of all the
742 * reference count modifications queued up in the rbtree. the head
743 * node may also store the extent flags to set. This way you can check
744 * to see what the reference count and extent flags would be if all of
745 * the delayed refs are not processed.
747 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
748 struct btrfs_root *root, u64 bytenr,
749 u64 offset, int metadata, u64 *refs, u64 *flags)
751 struct btrfs_delayed_ref_head *head;
752 struct btrfs_delayed_ref_root *delayed_refs;
753 struct btrfs_path *path;
754 struct btrfs_extent_item *ei;
755 struct extent_buffer *leaf;
756 struct btrfs_key key;
763 * If we don't have skinny metadata, don't bother doing anything
766 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
767 offset = root->leafsize;
771 path = btrfs_alloc_path();
776 path->skip_locking = 1;
777 path->search_commit_root = 1;
781 key.objectid = bytenr;
784 key.type = BTRFS_METADATA_ITEM_KEY;
786 key.type = BTRFS_EXTENT_ITEM_KEY;
789 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
794 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
795 if (path->slots[0]) {
797 btrfs_item_key_to_cpu(path->nodes[0], &key,
799 if (key.objectid == bytenr &&
800 key.type == BTRFS_EXTENT_ITEM_KEY &&
801 key.offset == root->leafsize)
805 key.objectid = bytenr;
806 key.type = BTRFS_EXTENT_ITEM_KEY;
807 key.offset = root->leafsize;
808 btrfs_release_path(path);
814 leaf = path->nodes[0];
815 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
816 if (item_size >= sizeof(*ei)) {
817 ei = btrfs_item_ptr(leaf, path->slots[0],
818 struct btrfs_extent_item);
819 num_refs = btrfs_extent_refs(leaf, ei);
820 extent_flags = btrfs_extent_flags(leaf, ei);
822 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
823 struct btrfs_extent_item_v0 *ei0;
824 BUG_ON(item_size != sizeof(*ei0));
825 ei0 = btrfs_item_ptr(leaf, path->slots[0],
826 struct btrfs_extent_item_v0);
827 num_refs = btrfs_extent_refs_v0(leaf, ei0);
828 /* FIXME: this isn't correct for data */
829 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
834 BUG_ON(num_refs == 0);
844 delayed_refs = &trans->transaction->delayed_refs;
845 spin_lock(&delayed_refs->lock);
846 head = btrfs_find_delayed_ref_head(trans, bytenr);
848 if (!mutex_trylock(&head->mutex)) {
849 atomic_inc(&head->node.refs);
850 spin_unlock(&delayed_refs->lock);
852 btrfs_release_path(path);
855 * Mutex was contended, block until it's released and try
858 mutex_lock(&head->mutex);
859 mutex_unlock(&head->mutex);
860 btrfs_put_delayed_ref(&head->node);
863 spin_lock(&head->lock);
864 if (head->extent_op && head->extent_op->update_flags)
865 extent_flags |= head->extent_op->flags_to_set;
867 BUG_ON(num_refs == 0);
869 num_refs += head->node.ref_mod;
870 spin_unlock(&head->lock);
871 mutex_unlock(&head->mutex);
873 spin_unlock(&delayed_refs->lock);
875 WARN_ON(num_refs == 0);
879 *flags = extent_flags;
881 btrfs_free_path(path);
886 * Back reference rules. Back refs have three main goals:
888 * 1) differentiate between all holders of references to an extent so that
889 * when a reference is dropped we can make sure it was a valid reference
890 * before freeing the extent.
892 * 2) Provide enough information to quickly find the holders of an extent
893 * if we notice a given block is corrupted or bad.
895 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
896 * maintenance. This is actually the same as #2, but with a slightly
897 * different use case.
899 * There are two kinds of back refs. The implicit back refs is optimized
900 * for pointers in non-shared tree blocks. For a given pointer in a block,
901 * back refs of this kind provide information about the block's owner tree
902 * and the pointer's key. These information allow us to find the block by
903 * b-tree searching. The full back refs is for pointers in tree blocks not
904 * referenced by their owner trees. The location of tree block is recorded
905 * in the back refs. Actually the full back refs is generic, and can be
906 * used in all cases the implicit back refs is used. The major shortcoming
907 * of the full back refs is its overhead. Every time a tree block gets
908 * COWed, we have to update back refs entry for all pointers in it.
910 * For a newly allocated tree block, we use implicit back refs for
911 * pointers in it. This means most tree related operations only involve
912 * implicit back refs. For a tree block created in old transaction, the
913 * only way to drop a reference to it is COW it. So we can detect the
914 * event that tree block loses its owner tree's reference and do the
915 * back refs conversion.
917 * When a tree block is COW'd through a tree, there are four cases:
919 * The reference count of the block is one and the tree is the block's
920 * owner tree. Nothing to do in this case.
922 * The reference count of the block is one and the tree is not the
923 * block's owner tree. In this case, full back refs is used for pointers
924 * in the block. Remove these full back refs, add implicit back refs for
925 * every pointers in the new block.
927 * The reference count of the block is greater than one and the tree is
928 * the block's owner tree. In this case, implicit back refs is used for
929 * pointers in the block. Add full back refs for every pointers in the
930 * block, increase lower level extents' reference counts. The original
931 * implicit back refs are entailed to the new block.
933 * The reference count of the block is greater than one and the tree is
934 * not the block's owner tree. Add implicit back refs for every pointer in
935 * the new block, increase lower level extents' reference count.
937 * Back Reference Key composing:
939 * The key objectid corresponds to the first byte in the extent,
940 * The key type is used to differentiate between types of back refs.
941 * There are different meanings of the key offset for different types
944 * File extents can be referenced by:
946 * - multiple snapshots, subvolumes, or different generations in one subvol
947 * - different files inside a single subvolume
948 * - different offsets inside a file (bookend extents in file.c)
950 * The extent ref structure for the implicit back refs has fields for:
952 * - Objectid of the subvolume root
953 * - objectid of the file holding the reference
954 * - original offset in the file
955 * - how many bookend extents
957 * The key offset for the implicit back refs is hash of the first
960 * The extent ref structure for the full back refs has field for:
962 * - number of pointers in the tree leaf
964 * The key offset for the implicit back refs is the first byte of
967 * When a file extent is allocated, The implicit back refs is used.
968 * the fields are filled in:
970 * (root_key.objectid, inode objectid, offset in file, 1)
972 * When a file extent is removed file truncation, we find the
973 * corresponding implicit back refs and check the following fields:
975 * (btrfs_header_owner(leaf), inode objectid, offset in file)
977 * Btree extents can be referenced by:
979 * - Different subvolumes
981 * Both the implicit back refs and the full back refs for tree blocks
982 * only consist of key. The key offset for the implicit back refs is
983 * objectid of block's owner tree. The key offset for the full back refs
984 * is the first byte of parent block.
986 * When implicit back refs is used, information about the lowest key and
987 * level of the tree block are required. These information are stored in
988 * tree block info structure.
991 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
992 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
993 struct btrfs_root *root,
994 struct btrfs_path *path,
995 u64 owner, u32 extra_size)
997 struct btrfs_extent_item *item;
998 struct btrfs_extent_item_v0 *ei0;
999 struct btrfs_extent_ref_v0 *ref0;
1000 struct btrfs_tree_block_info *bi;
1001 struct extent_buffer *leaf;
1002 struct btrfs_key key;
1003 struct btrfs_key found_key;
1004 u32 new_size = sizeof(*item);
1008 leaf = path->nodes[0];
1009 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
1011 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1012 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1013 struct btrfs_extent_item_v0);
1014 refs = btrfs_extent_refs_v0(leaf, ei0);
1016 if (owner == (u64)-1) {
1018 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1019 ret = btrfs_next_leaf(root, path);
1022 BUG_ON(ret > 0); /* Corruption */
1023 leaf = path->nodes[0];
1025 btrfs_item_key_to_cpu(leaf, &found_key,
1027 BUG_ON(key.objectid != found_key.objectid);
1028 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1032 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1033 struct btrfs_extent_ref_v0);
1034 owner = btrfs_ref_objectid_v0(leaf, ref0);
1038 btrfs_release_path(path);
1040 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1041 new_size += sizeof(*bi);
1043 new_size -= sizeof(*ei0);
1044 ret = btrfs_search_slot(trans, root, &key, path,
1045 new_size + extra_size, 1);
1048 BUG_ON(ret); /* Corruption */
1050 btrfs_extend_item(root, path, new_size);
1052 leaf = path->nodes[0];
1053 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1054 btrfs_set_extent_refs(leaf, item, refs);
1055 /* FIXME: get real generation */
1056 btrfs_set_extent_generation(leaf, item, 0);
1057 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1058 btrfs_set_extent_flags(leaf, item,
1059 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1060 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1061 bi = (struct btrfs_tree_block_info *)(item + 1);
1062 /* FIXME: get first key of the block */
1063 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1064 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1066 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1068 btrfs_mark_buffer_dirty(leaf);
1073 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1075 u32 high_crc = ~(u32)0;
1076 u32 low_crc = ~(u32)0;
1079 lenum = cpu_to_le64(root_objectid);
1080 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
1081 lenum = cpu_to_le64(owner);
1082 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1083 lenum = cpu_to_le64(offset);
1084 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1086 return ((u64)high_crc << 31) ^ (u64)low_crc;
1089 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1090 struct btrfs_extent_data_ref *ref)
1092 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1093 btrfs_extent_data_ref_objectid(leaf, ref),
1094 btrfs_extent_data_ref_offset(leaf, ref));
1097 static int match_extent_data_ref(struct extent_buffer *leaf,
1098 struct btrfs_extent_data_ref *ref,
1099 u64 root_objectid, u64 owner, u64 offset)
1101 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1102 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1103 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1108 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1109 struct btrfs_root *root,
1110 struct btrfs_path *path,
1111 u64 bytenr, u64 parent,
1113 u64 owner, u64 offset)
1115 struct btrfs_key key;
1116 struct btrfs_extent_data_ref *ref;
1117 struct extent_buffer *leaf;
1123 key.objectid = bytenr;
1125 key.type = BTRFS_SHARED_DATA_REF_KEY;
1126 key.offset = parent;
1128 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1129 key.offset = hash_extent_data_ref(root_objectid,
1134 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1143 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1144 key.type = BTRFS_EXTENT_REF_V0_KEY;
1145 btrfs_release_path(path);
1146 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1157 leaf = path->nodes[0];
1158 nritems = btrfs_header_nritems(leaf);
1160 if (path->slots[0] >= nritems) {
1161 ret = btrfs_next_leaf(root, path);
1167 leaf = path->nodes[0];
1168 nritems = btrfs_header_nritems(leaf);
1172 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1173 if (key.objectid != bytenr ||
1174 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1177 ref = btrfs_item_ptr(leaf, path->slots[0],
1178 struct btrfs_extent_data_ref);
1180 if (match_extent_data_ref(leaf, ref, root_objectid,
1183 btrfs_release_path(path);
1195 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1196 struct btrfs_root *root,
1197 struct btrfs_path *path,
1198 u64 bytenr, u64 parent,
1199 u64 root_objectid, u64 owner,
1200 u64 offset, int refs_to_add)
1202 struct btrfs_key key;
1203 struct extent_buffer *leaf;
1208 key.objectid = bytenr;
1210 key.type = BTRFS_SHARED_DATA_REF_KEY;
1211 key.offset = parent;
1212 size = sizeof(struct btrfs_shared_data_ref);
1214 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1215 key.offset = hash_extent_data_ref(root_objectid,
1217 size = sizeof(struct btrfs_extent_data_ref);
1220 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1221 if (ret && ret != -EEXIST)
1224 leaf = path->nodes[0];
1226 struct btrfs_shared_data_ref *ref;
1227 ref = btrfs_item_ptr(leaf, path->slots[0],
1228 struct btrfs_shared_data_ref);
1230 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1232 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1233 num_refs += refs_to_add;
1234 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1237 struct btrfs_extent_data_ref *ref;
1238 while (ret == -EEXIST) {
1239 ref = btrfs_item_ptr(leaf, path->slots[0],
1240 struct btrfs_extent_data_ref);
1241 if (match_extent_data_ref(leaf, ref, root_objectid,
1244 btrfs_release_path(path);
1246 ret = btrfs_insert_empty_item(trans, root, path, &key,
1248 if (ret && ret != -EEXIST)
1251 leaf = path->nodes[0];
1253 ref = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_extent_data_ref);
1256 btrfs_set_extent_data_ref_root(leaf, ref,
1258 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1259 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1260 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1262 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1263 num_refs += refs_to_add;
1264 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1267 btrfs_mark_buffer_dirty(leaf);
1270 btrfs_release_path(path);
1274 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1275 struct btrfs_root *root,
1276 struct btrfs_path *path,
1277 int refs_to_drop, int *last_ref)
1279 struct btrfs_key key;
1280 struct btrfs_extent_data_ref *ref1 = NULL;
1281 struct btrfs_shared_data_ref *ref2 = NULL;
1282 struct extent_buffer *leaf;
1286 leaf = path->nodes[0];
1287 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1289 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1290 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1291 struct btrfs_extent_data_ref);
1292 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1293 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1294 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1295 struct btrfs_shared_data_ref);
1296 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1297 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1298 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1299 struct btrfs_extent_ref_v0 *ref0;
1300 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1301 struct btrfs_extent_ref_v0);
1302 num_refs = btrfs_ref_count_v0(leaf, ref0);
1308 BUG_ON(num_refs < refs_to_drop);
1309 num_refs -= refs_to_drop;
1311 if (num_refs == 0) {
1312 ret = btrfs_del_item(trans, root, path);
1315 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1316 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1317 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1318 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1319 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1321 struct btrfs_extent_ref_v0 *ref0;
1322 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1323 struct btrfs_extent_ref_v0);
1324 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1327 btrfs_mark_buffer_dirty(leaf);
1332 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1333 struct btrfs_path *path,
1334 struct btrfs_extent_inline_ref *iref)
1336 struct btrfs_key key;
1337 struct extent_buffer *leaf;
1338 struct btrfs_extent_data_ref *ref1;
1339 struct btrfs_shared_data_ref *ref2;
1342 leaf = path->nodes[0];
1343 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1345 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1346 BTRFS_EXTENT_DATA_REF_KEY) {
1347 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1348 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1350 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1351 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1353 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1354 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1355 struct btrfs_extent_data_ref);
1356 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1357 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1358 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1359 struct btrfs_shared_data_ref);
1360 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1361 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1362 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1363 struct btrfs_extent_ref_v0 *ref0;
1364 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1365 struct btrfs_extent_ref_v0);
1366 num_refs = btrfs_ref_count_v0(leaf, ref0);
1374 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1375 struct btrfs_root *root,
1376 struct btrfs_path *path,
1377 u64 bytenr, u64 parent,
1380 struct btrfs_key key;
1383 key.objectid = bytenr;
1385 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1386 key.offset = parent;
1388 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1389 key.offset = root_objectid;
1392 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1395 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1396 if (ret == -ENOENT && parent) {
1397 btrfs_release_path(path);
1398 key.type = BTRFS_EXTENT_REF_V0_KEY;
1399 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1407 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1408 struct btrfs_root *root,
1409 struct btrfs_path *path,
1410 u64 bytenr, u64 parent,
1413 struct btrfs_key key;
1416 key.objectid = bytenr;
1418 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1419 key.offset = parent;
1421 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1422 key.offset = root_objectid;
1425 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1426 btrfs_release_path(path);
1430 static inline int extent_ref_type(u64 parent, u64 owner)
1433 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1435 type = BTRFS_SHARED_BLOCK_REF_KEY;
1437 type = BTRFS_TREE_BLOCK_REF_KEY;
1440 type = BTRFS_SHARED_DATA_REF_KEY;
1442 type = BTRFS_EXTENT_DATA_REF_KEY;
1447 static int find_next_key(struct btrfs_path *path, int level,
1448 struct btrfs_key *key)
1451 for (; level < BTRFS_MAX_LEVEL; level++) {
1452 if (!path->nodes[level])
1454 if (path->slots[level] + 1 >=
1455 btrfs_header_nritems(path->nodes[level]))
1458 btrfs_item_key_to_cpu(path->nodes[level], key,
1459 path->slots[level] + 1);
1461 btrfs_node_key_to_cpu(path->nodes[level], key,
1462 path->slots[level] + 1);
1469 * look for inline back ref. if back ref is found, *ref_ret is set
1470 * to the address of inline back ref, and 0 is returned.
1472 * if back ref isn't found, *ref_ret is set to the address where it
1473 * should be inserted, and -ENOENT is returned.
1475 * if insert is true and there are too many inline back refs, the path
1476 * points to the extent item, and -EAGAIN is returned.
1478 * NOTE: inline back refs are ordered in the same way that back ref
1479 * items in the tree are ordered.
1481 static noinline_for_stack
1482 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1483 struct btrfs_root *root,
1484 struct btrfs_path *path,
1485 struct btrfs_extent_inline_ref **ref_ret,
1486 u64 bytenr, u64 num_bytes,
1487 u64 parent, u64 root_objectid,
1488 u64 owner, u64 offset, int insert)
1490 struct btrfs_key key;
1491 struct extent_buffer *leaf;
1492 struct btrfs_extent_item *ei;
1493 struct btrfs_extent_inline_ref *iref;
1503 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1506 key.objectid = bytenr;
1507 key.type = BTRFS_EXTENT_ITEM_KEY;
1508 key.offset = num_bytes;
1510 want = extent_ref_type(parent, owner);
1512 extra_size = btrfs_extent_inline_ref_size(want);
1513 path->keep_locks = 1;
1518 * Owner is our parent level, so we can just add one to get the level
1519 * for the block we are interested in.
1521 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1522 key.type = BTRFS_METADATA_ITEM_KEY;
1527 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1534 * We may be a newly converted file system which still has the old fat
1535 * extent entries for metadata, so try and see if we have one of those.
1537 if (ret > 0 && skinny_metadata) {
1538 skinny_metadata = false;
1539 if (path->slots[0]) {
1541 btrfs_item_key_to_cpu(path->nodes[0], &key,
1543 if (key.objectid == bytenr &&
1544 key.type == BTRFS_EXTENT_ITEM_KEY &&
1545 key.offset == num_bytes)
1549 key.objectid = bytenr;
1550 key.type = BTRFS_EXTENT_ITEM_KEY;
1551 key.offset = num_bytes;
1552 btrfs_release_path(path);
1557 if (ret && !insert) {
1560 } else if (WARN_ON(ret)) {
1565 leaf = path->nodes[0];
1566 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1567 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1568 if (item_size < sizeof(*ei)) {
1573 ret = convert_extent_item_v0(trans, root, path, owner,
1579 leaf = path->nodes[0];
1580 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1583 BUG_ON(item_size < sizeof(*ei));
1585 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1586 flags = btrfs_extent_flags(leaf, ei);
1588 ptr = (unsigned long)(ei + 1);
1589 end = (unsigned long)ei + item_size;
1591 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1592 ptr += sizeof(struct btrfs_tree_block_info);
1602 iref = (struct btrfs_extent_inline_ref *)ptr;
1603 type = btrfs_extent_inline_ref_type(leaf, iref);
1607 ptr += btrfs_extent_inline_ref_size(type);
1611 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1612 struct btrfs_extent_data_ref *dref;
1613 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1614 if (match_extent_data_ref(leaf, dref, root_objectid,
1619 if (hash_extent_data_ref_item(leaf, dref) <
1620 hash_extent_data_ref(root_objectid, owner, offset))
1624 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1626 if (parent == ref_offset) {
1630 if (ref_offset < parent)
1633 if (root_objectid == ref_offset) {
1637 if (ref_offset < root_objectid)
1641 ptr += btrfs_extent_inline_ref_size(type);
1643 if (err == -ENOENT && insert) {
1644 if (item_size + extra_size >=
1645 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1650 * To add new inline back ref, we have to make sure
1651 * there is no corresponding back ref item.
1652 * For simplicity, we just do not add new inline back
1653 * ref if there is any kind of item for this block
1655 if (find_next_key(path, 0, &key) == 0 &&
1656 key.objectid == bytenr &&
1657 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1662 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1665 path->keep_locks = 0;
1666 btrfs_unlock_up_safe(path, 1);
1672 * helper to add new inline back ref
1674 static noinline_for_stack
1675 void setup_inline_extent_backref(struct btrfs_root *root,
1676 struct btrfs_path *path,
1677 struct btrfs_extent_inline_ref *iref,
1678 u64 parent, u64 root_objectid,
1679 u64 owner, u64 offset, int refs_to_add,
1680 struct btrfs_delayed_extent_op *extent_op)
1682 struct extent_buffer *leaf;
1683 struct btrfs_extent_item *ei;
1686 unsigned long item_offset;
1691 leaf = path->nodes[0];
1692 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1693 item_offset = (unsigned long)iref - (unsigned long)ei;
1695 type = extent_ref_type(parent, owner);
1696 size = btrfs_extent_inline_ref_size(type);
1698 btrfs_extend_item(root, path, size);
1700 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1701 refs = btrfs_extent_refs(leaf, ei);
1702 refs += refs_to_add;
1703 btrfs_set_extent_refs(leaf, ei, refs);
1705 __run_delayed_extent_op(extent_op, leaf, ei);
1707 ptr = (unsigned long)ei + item_offset;
1708 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1709 if (ptr < end - size)
1710 memmove_extent_buffer(leaf, ptr + size, ptr,
1713 iref = (struct btrfs_extent_inline_ref *)ptr;
1714 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1715 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1716 struct btrfs_extent_data_ref *dref;
1717 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1718 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1719 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1720 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1721 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1722 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1723 struct btrfs_shared_data_ref *sref;
1724 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1725 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1726 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1727 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1728 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1730 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1732 btrfs_mark_buffer_dirty(leaf);
1735 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1736 struct btrfs_root *root,
1737 struct btrfs_path *path,
1738 struct btrfs_extent_inline_ref **ref_ret,
1739 u64 bytenr, u64 num_bytes, u64 parent,
1740 u64 root_objectid, u64 owner, u64 offset)
1744 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1745 bytenr, num_bytes, parent,
1746 root_objectid, owner, offset, 0);
1750 btrfs_release_path(path);
1753 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1754 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1757 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1758 root_objectid, owner, offset);
1764 * helper to update/remove inline back ref
1766 static noinline_for_stack
1767 void update_inline_extent_backref(struct btrfs_root *root,
1768 struct btrfs_path *path,
1769 struct btrfs_extent_inline_ref *iref,
1771 struct btrfs_delayed_extent_op *extent_op,
1774 struct extent_buffer *leaf;
1775 struct btrfs_extent_item *ei;
1776 struct btrfs_extent_data_ref *dref = NULL;
1777 struct btrfs_shared_data_ref *sref = NULL;
1785 leaf = path->nodes[0];
1786 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1787 refs = btrfs_extent_refs(leaf, ei);
1788 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1789 refs += refs_to_mod;
1790 btrfs_set_extent_refs(leaf, ei, refs);
1792 __run_delayed_extent_op(extent_op, leaf, ei);
1794 type = btrfs_extent_inline_ref_type(leaf, iref);
1796 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1797 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1798 refs = btrfs_extent_data_ref_count(leaf, dref);
1799 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1800 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1801 refs = btrfs_shared_data_ref_count(leaf, sref);
1804 BUG_ON(refs_to_mod != -1);
1807 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1808 refs += refs_to_mod;
1811 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1812 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1814 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1817 size = btrfs_extent_inline_ref_size(type);
1818 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1819 ptr = (unsigned long)iref;
1820 end = (unsigned long)ei + item_size;
1821 if (ptr + size < end)
1822 memmove_extent_buffer(leaf, ptr, ptr + size,
1825 btrfs_truncate_item(root, path, item_size, 1);
1827 btrfs_mark_buffer_dirty(leaf);
1830 static noinline_for_stack
1831 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1832 struct btrfs_root *root,
1833 struct btrfs_path *path,
1834 u64 bytenr, u64 num_bytes, u64 parent,
1835 u64 root_objectid, u64 owner,
1836 u64 offset, int refs_to_add,
1837 struct btrfs_delayed_extent_op *extent_op)
1839 struct btrfs_extent_inline_ref *iref;
1842 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1843 bytenr, num_bytes, parent,
1844 root_objectid, owner, offset, 1);
1846 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1847 update_inline_extent_backref(root, path, iref,
1848 refs_to_add, extent_op, NULL);
1849 } else if (ret == -ENOENT) {
1850 setup_inline_extent_backref(root, path, iref, parent,
1851 root_objectid, owner, offset,
1852 refs_to_add, extent_op);
1858 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1859 struct btrfs_root *root,
1860 struct btrfs_path *path,
1861 u64 bytenr, u64 parent, u64 root_objectid,
1862 u64 owner, u64 offset, int refs_to_add)
1865 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1866 BUG_ON(refs_to_add != 1);
1867 ret = insert_tree_block_ref(trans, root, path, bytenr,
1868 parent, root_objectid);
1870 ret = insert_extent_data_ref(trans, root, path, bytenr,
1871 parent, root_objectid,
1872 owner, offset, refs_to_add);
1877 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1878 struct btrfs_root *root,
1879 struct btrfs_path *path,
1880 struct btrfs_extent_inline_ref *iref,
1881 int refs_to_drop, int is_data, int *last_ref)
1885 BUG_ON(!is_data && refs_to_drop != 1);
1887 update_inline_extent_backref(root, path, iref,
1888 -refs_to_drop, NULL, last_ref);
1889 } else if (is_data) {
1890 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1894 ret = btrfs_del_item(trans, root, path);
1899 static int btrfs_issue_discard(struct block_device *bdev,
1902 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
1905 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1906 u64 num_bytes, u64 *actual_bytes)
1909 u64 discarded_bytes = 0;
1910 struct btrfs_bio *bbio = NULL;
1913 /* Tell the block device(s) that the sectors can be discarded */
1914 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
1915 bytenr, &num_bytes, &bbio, 0);
1916 /* Error condition is -ENOMEM */
1918 struct btrfs_bio_stripe *stripe = bbio->stripes;
1922 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
1923 if (!stripe->dev->can_discard)
1926 ret = btrfs_issue_discard(stripe->dev->bdev,
1930 discarded_bytes += stripe->length;
1931 else if (ret != -EOPNOTSUPP)
1932 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1935 * Just in case we get back EOPNOTSUPP for some reason,
1936 * just ignore the return value so we don't screw up
1937 * people calling discard_extent.
1945 *actual_bytes = discarded_bytes;
1948 if (ret == -EOPNOTSUPP)
1953 /* Can return -ENOMEM */
1954 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1955 struct btrfs_root *root,
1956 u64 bytenr, u64 num_bytes, u64 parent,
1957 u64 root_objectid, u64 owner, u64 offset,
1961 struct btrfs_fs_info *fs_info = root->fs_info;
1963 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1964 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1966 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1967 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1969 parent, root_objectid, (int)owner,
1970 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
1972 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1974 parent, root_objectid, owner, offset,
1975 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
1980 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1981 struct btrfs_root *root,
1982 u64 bytenr, u64 num_bytes,
1983 u64 parent, u64 root_objectid,
1984 u64 owner, u64 offset, int refs_to_add,
1986 struct btrfs_delayed_extent_op *extent_op)
1988 struct btrfs_fs_info *fs_info = root->fs_info;
1989 struct btrfs_path *path;
1990 struct extent_buffer *leaf;
1991 struct btrfs_extent_item *item;
1992 struct btrfs_key key;
1995 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL;
1997 path = btrfs_alloc_path();
2001 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2005 path->leave_spinning = 1;
2006 /* this will setup the path even if it fails to insert the back ref */
2007 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2008 bytenr, num_bytes, parent,
2009 root_objectid, owner, offset,
2010 refs_to_add, extent_op);
2011 if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota))
2014 * Ok we were able to insert an inline extent and it appears to be a new
2015 * reference, deal with the qgroup accounting.
2017 if (!ret && !no_quota) {
2018 ASSERT(root->fs_info->quota_enabled);
2019 leaf = path->nodes[0];
2020 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2021 item = btrfs_item_ptr(leaf, path->slots[0],
2022 struct btrfs_extent_item);
2023 if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add)
2024 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2025 btrfs_release_path(path);
2027 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2028 bytenr, num_bytes, type, 0);
2033 * Ok we had -EAGAIN which means we didn't have space to insert and
2034 * inline extent ref, so just update the reference count and add a
2037 leaf = path->nodes[0];
2038 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2039 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2040 refs = btrfs_extent_refs(leaf, item);
2042 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2043 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2045 __run_delayed_extent_op(extent_op, leaf, item);
2047 btrfs_mark_buffer_dirty(leaf);
2048 btrfs_release_path(path);
2051 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2052 bytenr, num_bytes, type, 0);
2058 path->leave_spinning = 1;
2059 /* now insert the actual backref */
2060 ret = insert_extent_backref(trans, root->fs_info->extent_root,
2061 path, bytenr, parent, root_objectid,
2062 owner, offset, refs_to_add);
2064 btrfs_abort_transaction(trans, root, ret);
2066 btrfs_free_path(path);
2070 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2071 struct btrfs_root *root,
2072 struct btrfs_delayed_ref_node *node,
2073 struct btrfs_delayed_extent_op *extent_op,
2074 int insert_reserved)
2077 struct btrfs_delayed_data_ref *ref;
2078 struct btrfs_key ins;
2083 ins.objectid = node->bytenr;
2084 ins.offset = node->num_bytes;
2085 ins.type = BTRFS_EXTENT_ITEM_KEY;
2087 ref = btrfs_delayed_node_to_data_ref(node);
2088 trace_run_delayed_data_ref(node, ref, node->action);
2090 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2091 parent = ref->parent;
2092 ref_root = ref->root;
2094 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2096 flags |= extent_op->flags_to_set;
2097 ret = alloc_reserved_file_extent(trans, root,
2098 parent, ref_root, flags,
2099 ref->objectid, ref->offset,
2100 &ins, node->ref_mod);
2101 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2102 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2103 node->num_bytes, parent,
2104 ref_root, ref->objectid,
2105 ref->offset, node->ref_mod,
2106 node->no_quota, extent_op);
2107 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2108 ret = __btrfs_free_extent(trans, root, node->bytenr,
2109 node->num_bytes, parent,
2110 ref_root, ref->objectid,
2111 ref->offset, node->ref_mod,
2112 extent_op, node->no_quota);
2119 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2120 struct extent_buffer *leaf,
2121 struct btrfs_extent_item *ei)
2123 u64 flags = btrfs_extent_flags(leaf, ei);
2124 if (extent_op->update_flags) {
2125 flags |= extent_op->flags_to_set;
2126 btrfs_set_extent_flags(leaf, ei, flags);
2129 if (extent_op->update_key) {
2130 struct btrfs_tree_block_info *bi;
2131 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2132 bi = (struct btrfs_tree_block_info *)(ei + 1);
2133 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2137 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root,
2139 struct btrfs_delayed_ref_node *node,
2140 struct btrfs_delayed_extent_op *extent_op)
2142 struct btrfs_key key;
2143 struct btrfs_path *path;
2144 struct btrfs_extent_item *ei;
2145 struct extent_buffer *leaf;
2149 int metadata = !extent_op->is_data;
2154 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2157 path = btrfs_alloc_path();
2161 key.objectid = node->bytenr;
2164 key.type = BTRFS_METADATA_ITEM_KEY;
2165 key.offset = extent_op->level;
2167 key.type = BTRFS_EXTENT_ITEM_KEY;
2168 key.offset = node->num_bytes;
2173 path->leave_spinning = 1;
2174 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2182 if (path->slots[0] > 0) {
2184 btrfs_item_key_to_cpu(path->nodes[0], &key,
2186 if (key.objectid == node->bytenr &&
2187 key.type == BTRFS_EXTENT_ITEM_KEY &&
2188 key.offset == node->num_bytes)
2192 btrfs_release_path(path);
2195 key.objectid = node->bytenr;
2196 key.offset = node->num_bytes;
2197 key.type = BTRFS_EXTENT_ITEM_KEY;
2206 leaf = path->nodes[0];
2207 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2208 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2209 if (item_size < sizeof(*ei)) {
2210 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2216 leaf = path->nodes[0];
2217 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2220 BUG_ON(item_size < sizeof(*ei));
2221 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2222 __run_delayed_extent_op(extent_op, leaf, ei);
2224 btrfs_mark_buffer_dirty(leaf);
2226 btrfs_free_path(path);
2230 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2231 struct btrfs_root *root,
2232 struct btrfs_delayed_ref_node *node,
2233 struct btrfs_delayed_extent_op *extent_op,
2234 int insert_reserved)
2237 struct btrfs_delayed_tree_ref *ref;
2238 struct btrfs_key ins;
2241 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2244 ref = btrfs_delayed_node_to_tree_ref(node);
2245 trace_run_delayed_tree_ref(node, ref, node->action);
2247 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2248 parent = ref->parent;
2249 ref_root = ref->root;
2251 ins.objectid = node->bytenr;
2252 if (skinny_metadata) {
2253 ins.offset = ref->level;
2254 ins.type = BTRFS_METADATA_ITEM_KEY;
2256 ins.offset = node->num_bytes;
2257 ins.type = BTRFS_EXTENT_ITEM_KEY;
2260 BUG_ON(node->ref_mod != 1);
2261 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2262 BUG_ON(!extent_op || !extent_op->update_flags);
2263 ret = alloc_reserved_tree_block(trans, root,
2265 extent_op->flags_to_set,
2269 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2270 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2271 node->num_bytes, parent, ref_root,
2272 ref->level, 0, 1, node->no_quota,
2274 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2275 ret = __btrfs_free_extent(trans, root, node->bytenr,
2276 node->num_bytes, parent, ref_root,
2277 ref->level, 0, 1, extent_op,
2285 /* helper function to actually process a single delayed ref entry */
2286 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2287 struct btrfs_root *root,
2288 struct btrfs_delayed_ref_node *node,
2289 struct btrfs_delayed_extent_op *extent_op,
2290 int insert_reserved)
2294 if (trans->aborted) {
2295 if (insert_reserved)
2296 btrfs_pin_extent(root, node->bytenr,
2297 node->num_bytes, 1);
2301 if (btrfs_delayed_ref_is_head(node)) {
2302 struct btrfs_delayed_ref_head *head;
2304 * we've hit the end of the chain and we were supposed
2305 * to insert this extent into the tree. But, it got
2306 * deleted before we ever needed to insert it, so all
2307 * we have to do is clean up the accounting
2310 head = btrfs_delayed_node_to_head(node);
2311 trace_run_delayed_ref_head(node, head, node->action);
2313 if (insert_reserved) {
2314 btrfs_pin_extent(root, node->bytenr,
2315 node->num_bytes, 1);
2316 if (head->is_data) {
2317 ret = btrfs_del_csums(trans, root,
2325 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2326 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2327 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2329 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2330 node->type == BTRFS_SHARED_DATA_REF_KEY)
2331 ret = run_delayed_data_ref(trans, root, node, extent_op,
2338 static noinline struct btrfs_delayed_ref_node *
2339 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2341 struct rb_node *node;
2342 struct btrfs_delayed_ref_node *ref, *last = NULL;;
2345 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2346 * this prevents ref count from going down to zero when
2347 * there still are pending delayed ref.
2349 node = rb_first(&head->ref_root);
2351 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2353 if (ref->action == BTRFS_ADD_DELAYED_REF)
2355 else if (last == NULL)
2357 node = rb_next(node);
2363 * Returns 0 on success or if called with an already aborted transaction.
2364 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2366 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2367 struct btrfs_root *root,
2370 struct btrfs_delayed_ref_root *delayed_refs;
2371 struct btrfs_delayed_ref_node *ref;
2372 struct btrfs_delayed_ref_head *locked_ref = NULL;
2373 struct btrfs_delayed_extent_op *extent_op;
2374 struct btrfs_fs_info *fs_info = root->fs_info;
2375 ktime_t start = ktime_get();
2377 unsigned long count = 0;
2378 unsigned long actual_count = 0;
2379 int must_insert_reserved = 0;
2381 delayed_refs = &trans->transaction->delayed_refs;
2387 spin_lock(&delayed_refs->lock);
2388 locked_ref = btrfs_select_ref_head(trans);
2390 spin_unlock(&delayed_refs->lock);
2394 /* grab the lock that says we are going to process
2395 * all the refs for this head */
2396 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2397 spin_unlock(&delayed_refs->lock);
2399 * we may have dropped the spin lock to get the head
2400 * mutex lock, and that might have given someone else
2401 * time to free the head. If that's true, it has been
2402 * removed from our list and we can move on.
2404 if (ret == -EAGAIN) {
2412 * We need to try and merge add/drops of the same ref since we
2413 * can run into issues with relocate dropping the implicit ref
2414 * and then it being added back again before the drop can
2415 * finish. If we merged anything we need to re-loop so we can
2418 spin_lock(&locked_ref->lock);
2419 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2423 * locked_ref is the head node, so we have to go one
2424 * node back for any delayed ref updates
2426 ref = select_delayed_ref(locked_ref);
2428 if (ref && ref->seq &&
2429 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
2430 spin_unlock(&locked_ref->lock);
2431 btrfs_delayed_ref_unlock(locked_ref);
2432 spin_lock(&delayed_refs->lock);
2433 locked_ref->processing = 0;
2434 delayed_refs->num_heads_ready++;
2435 spin_unlock(&delayed_refs->lock);
2443 * record the must insert reserved flag before we
2444 * drop the spin lock.
2446 must_insert_reserved = locked_ref->must_insert_reserved;
2447 locked_ref->must_insert_reserved = 0;
2449 extent_op = locked_ref->extent_op;
2450 locked_ref->extent_op = NULL;
2455 /* All delayed refs have been processed, Go ahead
2456 * and send the head node to run_one_delayed_ref,
2457 * so that any accounting fixes can happen
2459 ref = &locked_ref->node;
2461 if (extent_op && must_insert_reserved) {
2462 btrfs_free_delayed_extent_op(extent_op);
2467 spin_unlock(&locked_ref->lock);
2468 ret = run_delayed_extent_op(trans, root,
2470 btrfs_free_delayed_extent_op(extent_op);
2474 * Need to reset must_insert_reserved if
2475 * there was an error so the abort stuff
2476 * can cleanup the reserved space
2479 if (must_insert_reserved)
2480 locked_ref->must_insert_reserved = 1;
2481 locked_ref->processing = 0;
2482 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2483 btrfs_delayed_ref_unlock(locked_ref);
2490 * Need to drop our head ref lock and re-aqcuire the
2491 * delayed ref lock and then re-check to make sure
2494 spin_unlock(&locked_ref->lock);
2495 spin_lock(&delayed_refs->lock);
2496 spin_lock(&locked_ref->lock);
2497 if (rb_first(&locked_ref->ref_root) ||
2498 locked_ref->extent_op) {
2499 spin_unlock(&locked_ref->lock);
2500 spin_unlock(&delayed_refs->lock);
2504 delayed_refs->num_heads--;
2505 rb_erase(&locked_ref->href_node,
2506 &delayed_refs->href_root);
2507 spin_unlock(&delayed_refs->lock);
2511 rb_erase(&ref->rb_node, &locked_ref->ref_root);
2513 atomic_dec(&delayed_refs->num_entries);
2515 if (!btrfs_delayed_ref_is_head(ref)) {
2517 * when we play the delayed ref, also correct the
2520 switch (ref->action) {
2521 case BTRFS_ADD_DELAYED_REF:
2522 case BTRFS_ADD_DELAYED_EXTENT:
2523 locked_ref->node.ref_mod -= ref->ref_mod;
2525 case BTRFS_DROP_DELAYED_REF:
2526 locked_ref->node.ref_mod += ref->ref_mod;
2532 spin_unlock(&locked_ref->lock);
2534 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2535 must_insert_reserved);
2537 btrfs_free_delayed_extent_op(extent_op);
2539 locked_ref->processing = 0;
2540 btrfs_delayed_ref_unlock(locked_ref);
2541 btrfs_put_delayed_ref(ref);
2542 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
2547 * If this node is a head, that means all the refs in this head
2548 * have been dealt with, and we will pick the next head to deal
2549 * with, so we must unlock the head and drop it from the cluster
2550 * list before we release it.
2552 if (btrfs_delayed_ref_is_head(ref)) {
2553 btrfs_delayed_ref_unlock(locked_ref);
2556 btrfs_put_delayed_ref(ref);
2562 * We don't want to include ref heads since we can have empty ref heads
2563 * and those will drastically skew our runtime down since we just do
2564 * accounting, no actual extent tree updates.
2566 if (actual_count > 0) {
2567 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2571 * We weigh the current average higher than our current runtime
2572 * to avoid large swings in the average.
2574 spin_lock(&delayed_refs->lock);
2575 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2576 avg = div64_u64(avg, 4);
2577 fs_info->avg_delayed_ref_runtime = avg;
2578 spin_unlock(&delayed_refs->lock);
2583 #ifdef SCRAMBLE_DELAYED_REFS
2585 * Normally delayed refs get processed in ascending bytenr order. This
2586 * correlates in most cases to the order added. To expose dependencies on this
2587 * order, we start to process the tree in the middle instead of the beginning
2589 static u64 find_middle(struct rb_root *root)
2591 struct rb_node *n = root->rb_node;
2592 struct btrfs_delayed_ref_node *entry;
2595 u64 first = 0, last = 0;
2599 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2600 first = entry->bytenr;
2604 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2605 last = entry->bytenr;
2610 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2611 WARN_ON(!entry->in_tree);
2613 middle = entry->bytenr;
2626 static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2630 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2631 sizeof(struct btrfs_extent_inline_ref));
2632 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2633 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2636 * We don't ever fill up leaves all the way so multiply by 2 just to be
2637 * closer to what we're really going to want to ouse.
2639 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2642 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2643 struct btrfs_root *root)
2645 struct btrfs_block_rsv *global_rsv;
2646 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2650 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2651 num_heads = heads_to_leaves(root, num_heads);
2653 num_bytes += (num_heads - 1) * root->leafsize;
2655 global_rsv = &root->fs_info->global_block_rsv;
2658 * If we can't allocate any more chunks lets make sure we have _lots_ of
2659 * wiggle room since running delayed refs can create more delayed refs.
2661 if (global_rsv->space_info->full)
2664 spin_lock(&global_rsv->lock);
2665 if (global_rsv->reserved <= num_bytes)
2667 spin_unlock(&global_rsv->lock);
2671 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2672 struct btrfs_root *root)
2674 struct btrfs_fs_info *fs_info = root->fs_info;
2676 atomic_read(&trans->transaction->delayed_refs.num_entries);
2681 avg_runtime = fs_info->avg_delayed_ref_runtime;
2682 val = num_entries * avg_runtime;
2683 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2685 if (val >= NSEC_PER_SEC / 2)
2688 return btrfs_check_space_for_delayed_refs(trans, root);
2691 struct async_delayed_refs {
2692 struct btrfs_root *root;
2696 struct completion wait;
2697 struct btrfs_work work;
2700 static void delayed_ref_async_start(struct btrfs_work *work)
2702 struct async_delayed_refs *async;
2703 struct btrfs_trans_handle *trans;
2706 async = container_of(work, struct async_delayed_refs, work);
2708 trans = btrfs_join_transaction(async->root);
2709 if (IS_ERR(trans)) {
2710 async->error = PTR_ERR(trans);
2715 * trans->sync means that when we call end_transaciton, we won't
2716 * wait on delayed refs
2719 ret = btrfs_run_delayed_refs(trans, async->root, async->count);
2723 ret = btrfs_end_transaction(trans, async->root);
2724 if (ret && !async->error)
2728 complete(&async->wait);
2733 int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2734 unsigned long count, int wait)
2736 struct async_delayed_refs *async;
2739 async = kmalloc(sizeof(*async), GFP_NOFS);
2743 async->root = root->fs_info->tree_root;
2744 async->count = count;
2750 init_completion(&async->wait);
2752 btrfs_init_work(&async->work, delayed_ref_async_start,
2755 btrfs_queue_work(root->fs_info->extent_workers, &async->work);
2758 wait_for_completion(&async->wait);
2767 * this starts processing the delayed reference count updates and
2768 * extent insertions we have queued up so far. count can be
2769 * 0, which means to process everything in the tree at the start
2770 * of the run (but not newly added entries), or it can be some target
2771 * number you'd like to process.
2773 * Returns 0 on success or if called with an aborted transaction
2774 * Returns <0 on error and aborts the transaction
2776 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2777 struct btrfs_root *root, unsigned long count)
2779 struct rb_node *node;
2780 struct btrfs_delayed_ref_root *delayed_refs;
2781 struct btrfs_delayed_ref_head *head;
2783 int run_all = count == (unsigned long)-1;
2786 /* We'll clean this up in btrfs_cleanup_transaction */
2790 if (root == root->fs_info->extent_root)
2791 root = root->fs_info->tree_root;
2793 delayed_refs = &trans->transaction->delayed_refs;
2795 count = atomic_read(&delayed_refs->num_entries) * 2;
2800 #ifdef SCRAMBLE_DELAYED_REFS
2801 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2803 ret = __btrfs_run_delayed_refs(trans, root, count);
2805 btrfs_abort_transaction(trans, root, ret);
2810 if (!list_empty(&trans->new_bgs))
2811 btrfs_create_pending_block_groups(trans, root);
2813 spin_lock(&delayed_refs->lock);
2814 node = rb_first(&delayed_refs->href_root);
2816 spin_unlock(&delayed_refs->lock);
2819 count = (unsigned long)-1;
2822 head = rb_entry(node, struct btrfs_delayed_ref_head,
2824 if (btrfs_delayed_ref_is_head(&head->node)) {
2825 struct btrfs_delayed_ref_node *ref;
2828 atomic_inc(&ref->refs);
2830 spin_unlock(&delayed_refs->lock);
2832 * Mutex was contended, block until it's
2833 * released and try again
2835 mutex_lock(&head->mutex);
2836 mutex_unlock(&head->mutex);
2838 btrfs_put_delayed_ref(ref);
2844 node = rb_next(node);
2846 spin_unlock(&delayed_refs->lock);
2851 ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
2854 assert_qgroups_uptodate(trans);
2858 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2859 struct btrfs_root *root,
2860 u64 bytenr, u64 num_bytes, u64 flags,
2861 int level, int is_data)
2863 struct btrfs_delayed_extent_op *extent_op;
2866 extent_op = btrfs_alloc_delayed_extent_op();
2870 extent_op->flags_to_set = flags;
2871 extent_op->update_flags = 1;
2872 extent_op->update_key = 0;
2873 extent_op->is_data = is_data ? 1 : 0;
2874 extent_op->level = level;
2876 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2877 num_bytes, extent_op);
2879 btrfs_free_delayed_extent_op(extent_op);
2883 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2884 struct btrfs_root *root,
2885 struct btrfs_path *path,
2886 u64 objectid, u64 offset, u64 bytenr)
2888 struct btrfs_delayed_ref_head *head;
2889 struct btrfs_delayed_ref_node *ref;
2890 struct btrfs_delayed_data_ref *data_ref;
2891 struct btrfs_delayed_ref_root *delayed_refs;
2892 struct rb_node *node;
2895 delayed_refs = &trans->transaction->delayed_refs;
2896 spin_lock(&delayed_refs->lock);
2897 head = btrfs_find_delayed_ref_head(trans, bytenr);
2899 spin_unlock(&delayed_refs->lock);
2903 if (!mutex_trylock(&head->mutex)) {
2904 atomic_inc(&head->node.refs);
2905 spin_unlock(&delayed_refs->lock);
2907 btrfs_release_path(path);
2910 * Mutex was contended, block until it's released and let
2913 mutex_lock(&head->mutex);
2914 mutex_unlock(&head->mutex);
2915 btrfs_put_delayed_ref(&head->node);
2918 spin_unlock(&delayed_refs->lock);
2920 spin_lock(&head->lock);
2921 node = rb_first(&head->ref_root);
2923 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2924 node = rb_next(node);
2926 /* If it's a shared ref we know a cross reference exists */
2927 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2932 data_ref = btrfs_delayed_node_to_data_ref(ref);
2935 * If our ref doesn't match the one we're currently looking at
2936 * then we have a cross reference.
2938 if (data_ref->root != root->root_key.objectid ||
2939 data_ref->objectid != objectid ||
2940 data_ref->offset != offset) {
2945 spin_unlock(&head->lock);
2946 mutex_unlock(&head->mutex);
2950 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2951 struct btrfs_root *root,
2952 struct btrfs_path *path,
2953 u64 objectid, u64 offset, u64 bytenr)
2955 struct btrfs_root *extent_root = root->fs_info->extent_root;
2956 struct extent_buffer *leaf;
2957 struct btrfs_extent_data_ref *ref;
2958 struct btrfs_extent_inline_ref *iref;
2959 struct btrfs_extent_item *ei;
2960 struct btrfs_key key;
2964 key.objectid = bytenr;
2965 key.offset = (u64)-1;
2966 key.type = BTRFS_EXTENT_ITEM_KEY;
2968 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2971 BUG_ON(ret == 0); /* Corruption */
2974 if (path->slots[0] == 0)
2978 leaf = path->nodes[0];
2979 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2981 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2985 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2986 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2987 if (item_size < sizeof(*ei)) {
2988 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2992 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2994 if (item_size != sizeof(*ei) +
2995 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2998 if (btrfs_extent_generation(leaf, ei) <=
2999 btrfs_root_last_snapshot(&root->root_item))
3002 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
3003 if (btrfs_extent_inline_ref_type(leaf, iref) !=
3004 BTRFS_EXTENT_DATA_REF_KEY)
3007 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3008 if (btrfs_extent_refs(leaf, ei) !=
3009 btrfs_extent_data_ref_count(leaf, ref) ||
3010 btrfs_extent_data_ref_root(leaf, ref) !=
3011 root->root_key.objectid ||
3012 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3013 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3021 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3022 struct btrfs_root *root,
3023 u64 objectid, u64 offset, u64 bytenr)
3025 struct btrfs_path *path;
3029 path = btrfs_alloc_path();
3034 ret = check_committed_ref(trans, root, path, objectid,
3036 if (ret && ret != -ENOENT)
3039 ret2 = check_delayed_ref(trans, root, path, objectid,
3041 } while (ret2 == -EAGAIN);
3043 if (ret2 && ret2 != -ENOENT) {
3048 if (ret != -ENOENT || ret2 != -ENOENT)
3051 btrfs_free_path(path);
3052 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3057 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3058 struct btrfs_root *root,
3059 struct extent_buffer *buf,
3060 int full_backref, int inc)
3067 struct btrfs_key key;
3068 struct btrfs_file_extent_item *fi;
3072 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
3073 u64, u64, u64, u64, u64, u64, int);
3075 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
3076 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
3079 ref_root = btrfs_header_owner(buf);
3080 nritems = btrfs_header_nritems(buf);
3081 level = btrfs_header_level(buf);
3083 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
3087 process_func = btrfs_inc_extent_ref;
3089 process_func = btrfs_free_extent;
3092 parent = buf->start;
3096 for (i = 0; i < nritems; i++) {
3098 btrfs_item_key_to_cpu(buf, &key, i);
3099 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3101 fi = btrfs_item_ptr(buf, i,
3102 struct btrfs_file_extent_item);
3103 if (btrfs_file_extent_type(buf, fi) ==
3104 BTRFS_FILE_EXTENT_INLINE)
3106 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3110 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3111 key.offset -= btrfs_file_extent_offset(buf, fi);
3112 ret = process_func(trans, root, bytenr, num_bytes,
3113 parent, ref_root, key.objectid,
3118 bytenr = btrfs_node_blockptr(buf, i);
3119 num_bytes = btrfs_level_size(root, level - 1);
3120 ret = process_func(trans, root, bytenr, num_bytes,
3121 parent, ref_root, level - 1, 0,
3132 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3133 struct extent_buffer *buf, int full_backref)
3135 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
3138 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3139 struct extent_buffer *buf, int full_backref)
3141 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
3144 static int write_one_cache_group(struct btrfs_trans_handle *trans,
3145 struct btrfs_root *root,
3146 struct btrfs_path *path,
3147 struct btrfs_block_group_cache *cache)
3150 struct btrfs_root *extent_root = root->fs_info->extent_root;
3152 struct extent_buffer *leaf;
3154 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
3157 BUG_ON(ret); /* Corruption */
3159 leaf = path->nodes[0];
3160 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3161 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3162 btrfs_mark_buffer_dirty(leaf);
3163 btrfs_release_path(path);
3166 btrfs_abort_transaction(trans, root, ret);
3173 static struct btrfs_block_group_cache *
3174 next_block_group(struct btrfs_root *root,
3175 struct btrfs_block_group_cache *cache)
3177 struct rb_node *node;
3178 spin_lock(&root->fs_info->block_group_cache_lock);
3179 node = rb_next(&cache->cache_node);
3180 btrfs_put_block_group(cache);
3182 cache = rb_entry(node, struct btrfs_block_group_cache,
3184 btrfs_get_block_group(cache);
3187 spin_unlock(&root->fs_info->block_group_cache_lock);
3191 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3192 struct btrfs_trans_handle *trans,
3193 struct btrfs_path *path)
3195 struct btrfs_root *root = block_group->fs_info->tree_root;
3196 struct inode *inode = NULL;
3198 int dcs = BTRFS_DC_ERROR;
3204 * If this block group is smaller than 100 megs don't bother caching the
3207 if (block_group->key.offset < (100 * 1024 * 1024)) {
3208 spin_lock(&block_group->lock);
3209 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3210 spin_unlock(&block_group->lock);
3215 inode = lookup_free_space_inode(root, block_group, path);
3216 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3217 ret = PTR_ERR(inode);
3218 btrfs_release_path(path);
3222 if (IS_ERR(inode)) {
3226 if (block_group->ro)
3229 ret = create_free_space_inode(root, trans, block_group, path);
3235 /* We've already setup this transaction, go ahead and exit */
3236 if (block_group->cache_generation == trans->transid &&
3237 i_size_read(inode)) {
3238 dcs = BTRFS_DC_SETUP;
3243 * We want to set the generation to 0, that way if anything goes wrong
3244 * from here on out we know not to trust this cache when we load up next
3247 BTRFS_I(inode)->generation = 0;
3248 ret = btrfs_update_inode(trans, root, inode);
3251 if (i_size_read(inode) > 0) {
3252 ret = btrfs_check_trunc_cache_free_space(root,
3253 &root->fs_info->global_block_rsv);
3257 ret = btrfs_truncate_free_space_cache(root, trans, inode);
3262 spin_lock(&block_group->lock);
3263 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3264 !btrfs_test_opt(root, SPACE_CACHE) ||
3265 block_group->delalloc_bytes) {
3267 * don't bother trying to write stuff out _if_
3268 * a) we're not cached,
3269 * b) we're with nospace_cache mount option.
3271 dcs = BTRFS_DC_WRITTEN;
3272 spin_unlock(&block_group->lock);
3275 spin_unlock(&block_group->lock);
3278 * Try to preallocate enough space based on how big the block group is.
3279 * Keep in mind this has to include any pinned space which could end up
3280 * taking up quite a bit since it's not folded into the other space
3283 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
3288 num_pages *= PAGE_CACHE_SIZE;
3290 ret = btrfs_check_data_free_space(inode, num_pages);
3294 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3295 num_pages, num_pages,
3298 dcs = BTRFS_DC_SETUP;
3299 btrfs_free_reserved_data_space(inode, num_pages);
3304 btrfs_release_path(path);
3306 spin_lock(&block_group->lock);
3307 if (!ret && dcs == BTRFS_DC_SETUP)
3308 block_group->cache_generation = trans->transid;
3309 block_group->disk_cache_state = dcs;
3310 spin_unlock(&block_group->lock);
3315 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3316 struct btrfs_root *root)
3318 struct btrfs_block_group_cache *cache;
3320 struct btrfs_path *path;
3323 path = btrfs_alloc_path();
3329 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3331 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3333 cache = next_block_group(root, cache);
3341 err = cache_save_setup(cache, trans, path);
3342 last = cache->key.objectid + cache->key.offset;
3343 btrfs_put_block_group(cache);
3348 err = btrfs_run_delayed_refs(trans, root,
3350 if (err) /* File system offline */
3354 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3356 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3357 btrfs_put_block_group(cache);
3363 cache = next_block_group(root, cache);
3372 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3373 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
3375 last = cache->key.objectid + cache->key.offset;
3377 err = write_one_cache_group(trans, root, path, cache);
3378 btrfs_put_block_group(cache);
3379 if (err) /* File system offline */
3385 * I don't think this is needed since we're just marking our
3386 * preallocated extent as written, but just in case it can't
3390 err = btrfs_run_delayed_refs(trans, root,
3392 if (err) /* File system offline */
3396 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3399 * Really this shouldn't happen, but it could if we
3400 * couldn't write the entire preallocated extent and
3401 * splitting the extent resulted in a new block.
3404 btrfs_put_block_group(cache);
3407 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3409 cache = next_block_group(root, cache);
3418 err = btrfs_write_out_cache(root, trans, cache, path);
3421 * If we didn't have an error then the cache state is still
3422 * NEED_WRITE, so we can set it to WRITTEN.
3424 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3425 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3426 last = cache->key.objectid + cache->key.offset;
3427 btrfs_put_block_group(cache);
3431 btrfs_free_path(path);
3435 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3437 struct btrfs_block_group_cache *block_group;
3440 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3441 if (!block_group || block_group->ro)
3444 btrfs_put_block_group(block_group);
3448 static const char *alloc_name(u64 flags)
3451 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3453 case BTRFS_BLOCK_GROUP_METADATA:
3455 case BTRFS_BLOCK_GROUP_DATA:
3457 case BTRFS_BLOCK_GROUP_SYSTEM:
3461 return "invalid-combination";
3465 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3466 u64 total_bytes, u64 bytes_used,
3467 struct btrfs_space_info **space_info)
3469 struct btrfs_space_info *found;
3474 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3475 BTRFS_BLOCK_GROUP_RAID10))
3480 found = __find_space_info(info, flags);
3482 spin_lock(&found->lock);
3483 found->total_bytes += total_bytes;
3484 found->disk_total += total_bytes * factor;
3485 found->bytes_used += bytes_used;
3486 found->disk_used += bytes_used * factor;
3488 spin_unlock(&found->lock);
3489 *space_info = found;
3492 found = kzalloc(sizeof(*found), GFP_NOFS);
3496 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3502 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3503 INIT_LIST_HEAD(&found->block_groups[i]);
3504 init_rwsem(&found->groups_sem);
3505 spin_lock_init(&found->lock);
3506 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
3507 found->total_bytes = total_bytes;
3508 found->disk_total = total_bytes * factor;
3509 found->bytes_used = bytes_used;
3510 found->disk_used = bytes_used * factor;
3511 found->bytes_pinned = 0;
3512 found->bytes_reserved = 0;
3513 found->bytes_readonly = 0;
3514 found->bytes_may_use = 0;
3516 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
3517 found->chunk_alloc = 0;
3519 init_waitqueue_head(&found->wait);
3521 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3522 info->space_info_kobj, "%s",
3523 alloc_name(found->flags));
3529 *space_info = found;
3530 list_add_rcu(&found->list, &info->space_info);
3531 if (flags & BTRFS_BLOCK_GROUP_DATA)
3532 info->data_sinfo = found;
3537 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3539 u64 extra_flags = chunk_to_extended(flags) &
3540 BTRFS_EXTENDED_PROFILE_MASK;
3542 write_seqlock(&fs_info->profiles_lock);
3543 if (flags & BTRFS_BLOCK_GROUP_DATA)
3544 fs_info->avail_data_alloc_bits |= extra_flags;
3545 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3546 fs_info->avail_metadata_alloc_bits |= extra_flags;
3547 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3548 fs_info->avail_system_alloc_bits |= extra_flags;
3549 write_sequnlock(&fs_info->profiles_lock);
3553 * returns target flags in extended format or 0 if restripe for this
3554 * chunk_type is not in progress
3556 * should be called with either volume_mutex or balance_lock held
3558 static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3560 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3566 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3567 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3568 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3569 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3570 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3571 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3572 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3573 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3574 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3581 * @flags: available profiles in extended format (see ctree.h)
3583 * Returns reduced profile in chunk format. If profile changing is in
3584 * progress (either running or paused) picks the target profile (if it's
3585 * already available), otherwise falls back to plain reducing.
3587 static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3590 * we add in the count of missing devices because we want
3591 * to make sure that any RAID levels on a degraded FS
3592 * continue to be honored.
3594 u64 num_devices = root->fs_info->fs_devices->rw_devices +
3595 root->fs_info->fs_devices->missing_devices;
3600 * see if restripe for this chunk_type is in progress, if so
3601 * try to reduce to the target profile
3603 spin_lock(&root->fs_info->balance_lock);
3604 target = get_restripe_target(root->fs_info, flags);
3606 /* pick target profile only if it's already available */
3607 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
3608 spin_unlock(&root->fs_info->balance_lock);
3609 return extended_to_chunk(target);
3612 spin_unlock(&root->fs_info->balance_lock);
3614 /* First, mask out the RAID levels which aren't possible */
3615 if (num_devices == 1)
3616 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3617 BTRFS_BLOCK_GROUP_RAID5);
3618 if (num_devices < 3)
3619 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
3620 if (num_devices < 4)
3621 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3623 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3624 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3625 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3628 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3629 tmp = BTRFS_BLOCK_GROUP_RAID6;
3630 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3631 tmp = BTRFS_BLOCK_GROUP_RAID5;
3632 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3633 tmp = BTRFS_BLOCK_GROUP_RAID10;
3634 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3635 tmp = BTRFS_BLOCK_GROUP_RAID1;
3636 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3637 tmp = BTRFS_BLOCK_GROUP_RAID0;
3639 return extended_to_chunk(flags | tmp);
3642 static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
3649 seq = read_seqbegin(&root->fs_info->profiles_lock);
3651 if (flags & BTRFS_BLOCK_GROUP_DATA)
3652 flags |= root->fs_info->avail_data_alloc_bits;
3653 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3654 flags |= root->fs_info->avail_system_alloc_bits;
3655 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3656 flags |= root->fs_info->avail_metadata_alloc_bits;
3657 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
3659 return btrfs_reduce_alloc_profile(root, flags);
3662 u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3668 flags = BTRFS_BLOCK_GROUP_DATA;
3669 else if (root == root->fs_info->chunk_root)
3670 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3672 flags = BTRFS_BLOCK_GROUP_METADATA;
3674 ret = get_alloc_profile(root, flags);
3679 * This will check the space that the inode allocates from to make sure we have
3680 * enough space for bytes.
3682 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3684 struct btrfs_space_info *data_sinfo;
3685 struct btrfs_root *root = BTRFS_I(inode)->root;
3686 struct btrfs_fs_info *fs_info = root->fs_info;
3688 int ret = 0, committed = 0, alloc_chunk = 1;
3690 /* make sure bytes are sectorsize aligned */
3691 bytes = ALIGN(bytes, root->sectorsize);
3693 if (btrfs_is_free_space_inode(inode)) {
3695 ASSERT(current->journal_info);
3698 data_sinfo = fs_info->data_sinfo;
3703 /* make sure we have enough space to handle the data first */
3704 spin_lock(&data_sinfo->lock);
3705 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3706 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3707 data_sinfo->bytes_may_use;
3709 if (used + bytes > data_sinfo->total_bytes) {
3710 struct btrfs_trans_handle *trans;
3713 * if we don't have enough free bytes in this space then we need
3714 * to alloc a new chunk.
3716 if (!data_sinfo->full && alloc_chunk) {
3719 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
3720 spin_unlock(&data_sinfo->lock);
3722 alloc_target = btrfs_get_alloc_profile(root, 1);
3724 * It is ugly that we don't call nolock join
3725 * transaction for the free space inode case here.
3726 * But it is safe because we only do the data space
3727 * reservation for the free space cache in the
3728 * transaction context, the common join transaction
3729 * just increase the counter of the current transaction
3730 * handler, doesn't try to acquire the trans_lock of
3733 trans = btrfs_join_transaction(root);
3735 return PTR_ERR(trans);
3737 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3739 CHUNK_ALLOC_NO_FORCE);
3740 btrfs_end_transaction(trans, root);
3749 data_sinfo = fs_info->data_sinfo;
3755 * If we don't have enough pinned space to deal with this
3756 * allocation don't bother committing the transaction.
3758 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3761 spin_unlock(&data_sinfo->lock);
3763 /* commit the current transaction and try again */
3766 !atomic_read(&root->fs_info->open_ioctl_trans)) {
3769 trans = btrfs_join_transaction(root);
3771 return PTR_ERR(trans);
3772 ret = btrfs_commit_transaction(trans, root);
3778 trace_btrfs_space_reservation(root->fs_info,
3779 "space_info:enospc",
3780 data_sinfo->flags, bytes, 1);
3783 data_sinfo->bytes_may_use += bytes;
3784 trace_btrfs_space_reservation(root->fs_info, "space_info",
3785 data_sinfo->flags, bytes, 1);
3786 spin_unlock(&data_sinfo->lock);
3792 * Called if we need to clear a data reservation for this inode.
3794 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3796 struct btrfs_root *root = BTRFS_I(inode)->root;
3797 struct btrfs_space_info *data_sinfo;
3799 /* make sure bytes are sectorsize aligned */
3800 bytes = ALIGN(bytes, root->sectorsize);
3802 data_sinfo = root->fs_info->data_sinfo;
3803 spin_lock(&data_sinfo->lock);
3804 WARN_ON(data_sinfo->bytes_may_use < bytes);
3805 data_sinfo->bytes_may_use -= bytes;
3806 trace_btrfs_space_reservation(root->fs_info, "space_info",
3807 data_sinfo->flags, bytes, 0);
3808 spin_unlock(&data_sinfo->lock);
3811 static void force_metadata_allocation(struct btrfs_fs_info *info)
3813 struct list_head *head = &info->space_info;
3814 struct btrfs_space_info *found;
3817 list_for_each_entry_rcu(found, head, list) {
3818 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3819 found->force_alloc = CHUNK_ALLOC_FORCE;
3824 static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3826 return (global->size << 1);
3829 static int should_alloc_chunk(struct btrfs_root *root,
3830 struct btrfs_space_info *sinfo, int force)
3832 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3833 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3834 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3837 if (force == CHUNK_ALLOC_FORCE)
3841 * We need to take into account the global rsv because for all intents
3842 * and purposes it's used space. Don't worry about locking the
3843 * global_rsv, it doesn't change except when the transaction commits.
3845 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3846 num_allocated += calc_global_rsv_need_space(global_rsv);
3849 * in limited mode, we want to have some free space up to
3850 * about 1% of the FS size.
3852 if (force == CHUNK_ALLOC_LIMITED) {
3853 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
3854 thresh = max_t(u64, 64 * 1024 * 1024,
3855 div_factor_fine(thresh, 1));
3857 if (num_bytes - num_allocated < thresh)
3861 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
3866 static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3870 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3871 BTRFS_BLOCK_GROUP_RAID0 |
3872 BTRFS_BLOCK_GROUP_RAID5 |
3873 BTRFS_BLOCK_GROUP_RAID6))
3874 num_dev = root->fs_info->fs_devices->rw_devices;
3875 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3878 num_dev = 1; /* DUP or single */
3880 /* metadata for updaing devices and chunk tree */
3881 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3884 static void check_system_chunk(struct btrfs_trans_handle *trans,
3885 struct btrfs_root *root, u64 type)
3887 struct btrfs_space_info *info;
3891 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3892 spin_lock(&info->lock);
3893 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3894 info->bytes_reserved - info->bytes_readonly;
3895 spin_unlock(&info->lock);
3897 thresh = get_system_chunk_thresh(root, type);
3898 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3899 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3900 left, thresh, type);
3901 dump_space_info(info, 0, 0);
3904 if (left < thresh) {
3907 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3908 btrfs_alloc_chunk(trans, root, flags);
3912 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3913 struct btrfs_root *extent_root, u64 flags, int force)
3915 struct btrfs_space_info *space_info;
3916 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3917 int wait_for_alloc = 0;
3920 /* Don't re-enter if we're already allocating a chunk */
3921 if (trans->allocating_chunk)
3924 space_info = __find_space_info(extent_root->fs_info, flags);
3926 ret = update_space_info(extent_root->fs_info, flags,
3928 BUG_ON(ret); /* -ENOMEM */
3930 BUG_ON(!space_info); /* Logic error */
3933 spin_lock(&space_info->lock);
3934 if (force < space_info->force_alloc)
3935 force = space_info->force_alloc;
3936 if (space_info->full) {
3937 if (should_alloc_chunk(extent_root, space_info, force))
3941 spin_unlock(&space_info->lock);
3945 if (!should_alloc_chunk(extent_root, space_info, force)) {
3946 spin_unlock(&space_info->lock);
3948 } else if (space_info->chunk_alloc) {
3951 space_info->chunk_alloc = 1;
3954 spin_unlock(&space_info->lock);
3956 mutex_lock(&fs_info->chunk_mutex);
3959 * The chunk_mutex is held throughout the entirety of a chunk
3960 * allocation, so once we've acquired the chunk_mutex we know that the
3961 * other guy is done and we need to recheck and see if we should
3964 if (wait_for_alloc) {
3965 mutex_unlock(&fs_info->chunk_mutex);
3970 trans->allocating_chunk = true;
3973 * If we have mixed data/metadata chunks we want to make sure we keep
3974 * allocating mixed chunks instead of individual chunks.
3976 if (btrfs_mixed_space_info(space_info))
3977 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3980 * if we're doing a data chunk, go ahead and make sure that
3981 * we keep a reasonable number of metadata chunks allocated in the
3984 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3985 fs_info->data_chunk_allocations++;
3986 if (!(fs_info->data_chunk_allocations %
3987 fs_info->metadata_ratio))
3988 force_metadata_allocation(fs_info);
3992 * Check if we have enough space in SYSTEM chunk because we may need
3993 * to update devices.
3995 check_system_chunk(trans, extent_root, flags);
3997 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3998 trans->allocating_chunk = false;
4000 spin_lock(&space_info->lock);
4001 if (ret < 0 && ret != -ENOSPC)
4004 space_info->full = 1;
4008 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
4010 space_info->chunk_alloc = 0;
4011 spin_unlock(&space_info->lock);
4012 mutex_unlock(&fs_info->chunk_mutex);
4016 static int can_overcommit(struct btrfs_root *root,
4017 struct btrfs_space_info *space_info, u64 bytes,
4018 enum btrfs_reserve_flush_enum flush)
4020 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4021 u64 profile = btrfs_get_alloc_profile(root, 0);
4026 used = space_info->bytes_used + space_info->bytes_reserved +
4027 space_info->bytes_pinned + space_info->bytes_readonly;
4030 * We only want to allow over committing if we have lots of actual space
4031 * free, but if we don't have enough space to handle the global reserve
4032 * space then we could end up having a real enospc problem when trying
4033 * to allocate a chunk or some other such important allocation.
4035 spin_lock(&global_rsv->lock);
4036 space_size = calc_global_rsv_need_space(global_rsv);
4037 spin_unlock(&global_rsv->lock);
4038 if (used + space_size >= space_info->total_bytes)
4041 used += space_info->bytes_may_use;
4043 spin_lock(&root->fs_info->free_chunk_lock);
4044 avail = root->fs_info->free_chunk_space;
4045 spin_unlock(&root->fs_info->free_chunk_lock);
4048 * If we have dup, raid1 or raid10 then only half of the free
4049 * space is actually useable. For raid56, the space info used
4050 * doesn't include the parity drive, so we don't have to
4053 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4054 BTRFS_BLOCK_GROUP_RAID1 |
4055 BTRFS_BLOCK_GROUP_RAID10))
4059 * If we aren't flushing all things, let us overcommit up to
4060 * 1/2th of the space. If we can flush, don't let us overcommit
4061 * too much, let it overcommit up to 1/8 of the space.
4063 if (flush == BTRFS_RESERVE_FLUSH_ALL)
4068 if (used + bytes < space_info->total_bytes + avail)
4073 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4074 unsigned long nr_pages, int nr_items)
4076 struct super_block *sb = root->fs_info->sb;
4078 if (down_read_trylock(&sb->s_umount)) {
4079 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4080 up_read(&sb->s_umount);
4083 * We needn't worry the filesystem going from r/w to r/o though
4084 * we don't acquire ->s_umount mutex, because the filesystem
4085 * should guarantee the delalloc inodes list be empty after
4086 * the filesystem is readonly(all dirty pages are written to
4089 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
4090 if (!current->journal_info)
4091 btrfs_wait_ordered_roots(root->fs_info, nr_items);
4095 static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4100 bytes = btrfs_calc_trans_metadata_size(root, 1);
4101 nr = (int)div64_u64(to_reclaim, bytes);
4107 #define EXTENT_SIZE_PER_ITEM (256 * 1024)
4110 * shrink metadata reservation for delalloc
4112 static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4115 struct btrfs_block_rsv *block_rsv;
4116 struct btrfs_space_info *space_info;
4117 struct btrfs_trans_handle *trans;
4121 unsigned long nr_pages;
4124 enum btrfs_reserve_flush_enum flush;
4126 /* Calc the number of the pages we need flush for space reservation */
4127 items = calc_reclaim_items_nr(root, to_reclaim);
4128 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
4130 trans = (struct btrfs_trans_handle *)current->journal_info;
4131 block_rsv = &root->fs_info->delalloc_block_rsv;
4132 space_info = block_rsv->space_info;
4134 delalloc_bytes = percpu_counter_sum_positive(
4135 &root->fs_info->delalloc_bytes);
4136 if (delalloc_bytes == 0) {
4140 btrfs_wait_ordered_roots(root->fs_info, items);
4145 while (delalloc_bytes && loops < 3) {
4146 max_reclaim = min(delalloc_bytes, to_reclaim);
4147 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
4148 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
4150 * We need to wait for the async pages to actually start before
4153 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4157 if (max_reclaim <= nr_pages)
4160 max_reclaim -= nr_pages;
4162 wait_event(root->fs_info->async_submit_wait,
4163 atomic_read(&root->fs_info->async_delalloc_pages) <=
4167 flush = BTRFS_RESERVE_FLUSH_ALL;
4169 flush = BTRFS_RESERVE_NO_FLUSH;
4170 spin_lock(&space_info->lock);
4171 if (can_overcommit(root, space_info, orig, flush)) {
4172 spin_unlock(&space_info->lock);
4175 spin_unlock(&space_info->lock);
4178 if (wait_ordered && !trans) {
4179 btrfs_wait_ordered_roots(root->fs_info, items);
4181 time_left = schedule_timeout_killable(1);
4185 delalloc_bytes = percpu_counter_sum_positive(
4186 &root->fs_info->delalloc_bytes);
4191 * maybe_commit_transaction - possibly commit the transaction if its ok to
4192 * @root - the root we're allocating for
4193 * @bytes - the number of bytes we want to reserve
4194 * @force - force the commit
4196 * This will check to make sure that committing the transaction will actually
4197 * get us somewhere and then commit the transaction if it does. Otherwise it
4198 * will return -ENOSPC.
4200 static int may_commit_transaction(struct btrfs_root *root,
4201 struct btrfs_space_info *space_info,
4202 u64 bytes, int force)
4204 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4205 struct btrfs_trans_handle *trans;
4207 trans = (struct btrfs_trans_handle *)current->journal_info;
4214 /* See if there is enough pinned space to make this reservation */
4215 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4220 * See if there is some space in the delayed insertion reservation for
4223 if (space_info != delayed_rsv->space_info)
4226 spin_lock(&delayed_rsv->lock);
4227 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4228 bytes - delayed_rsv->size) >= 0) {
4229 spin_unlock(&delayed_rsv->lock);
4232 spin_unlock(&delayed_rsv->lock);
4235 trans = btrfs_join_transaction(root);
4239 return btrfs_commit_transaction(trans, root);
4243 FLUSH_DELAYED_ITEMS_NR = 1,
4244 FLUSH_DELAYED_ITEMS = 2,
4246 FLUSH_DELALLOC_WAIT = 4,
4251 static int flush_space(struct btrfs_root *root,
4252 struct btrfs_space_info *space_info, u64 num_bytes,
4253 u64 orig_bytes, int state)
4255 struct btrfs_trans_handle *trans;
4260 case FLUSH_DELAYED_ITEMS_NR:
4261 case FLUSH_DELAYED_ITEMS:
4262 if (state == FLUSH_DELAYED_ITEMS_NR)
4263 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4267 trans = btrfs_join_transaction(root);
4268 if (IS_ERR(trans)) {
4269 ret = PTR_ERR(trans);
4272 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4273 btrfs_end_transaction(trans, root);
4275 case FLUSH_DELALLOC:
4276 case FLUSH_DELALLOC_WAIT:
4277 shrink_delalloc(root, num_bytes * 2, orig_bytes,
4278 state == FLUSH_DELALLOC_WAIT);
4281 trans = btrfs_join_transaction(root);
4282 if (IS_ERR(trans)) {
4283 ret = PTR_ERR(trans);
4286 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4287 btrfs_get_alloc_profile(root, 0),
4288 CHUNK_ALLOC_NO_FORCE);
4289 btrfs_end_transaction(trans, root);
4294 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4305 btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4306 struct btrfs_space_info *space_info)
4312 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4314 spin_lock(&space_info->lock);
4315 if (can_overcommit(root, space_info, to_reclaim,
4316 BTRFS_RESERVE_FLUSH_ALL)) {
4321 used = space_info->bytes_used + space_info->bytes_reserved +
4322 space_info->bytes_pinned + space_info->bytes_readonly +
4323 space_info->bytes_may_use;
4324 if (can_overcommit(root, space_info, 1024 * 1024,
4325 BTRFS_RESERVE_FLUSH_ALL))
4326 expected = div_factor_fine(space_info->total_bytes, 95);
4328 expected = div_factor_fine(space_info->total_bytes, 90);
4330 if (used > expected)
4331 to_reclaim = used - expected;
4334 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4335 space_info->bytes_reserved);
4337 spin_unlock(&space_info->lock);
4342 static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4343 struct btrfs_fs_info *fs_info, u64 used)
4345 return (used >= div_factor_fine(space_info->total_bytes, 98) &&
4346 !btrfs_fs_closing(fs_info) &&
4347 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4350 static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
4351 struct btrfs_fs_info *fs_info)
4355 spin_lock(&space_info->lock);
4356 used = space_info->bytes_used + space_info->bytes_reserved +
4357 space_info->bytes_pinned + space_info->bytes_readonly +
4358 space_info->bytes_may_use;
4359 if (need_do_async_reclaim(space_info, fs_info, used)) {
4360 spin_unlock(&space_info->lock);
4363 spin_unlock(&space_info->lock);
4368 static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4370 struct btrfs_fs_info *fs_info;
4371 struct btrfs_space_info *space_info;
4375 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4376 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4378 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4383 flush_state = FLUSH_DELAYED_ITEMS_NR;
4385 flush_space(fs_info->fs_root, space_info, to_reclaim,
4386 to_reclaim, flush_state);
4388 if (!btrfs_need_do_async_reclaim(space_info, fs_info))
4390 } while (flush_state <= COMMIT_TRANS);
4392 if (btrfs_need_do_async_reclaim(space_info, fs_info))
4393 queue_work(system_unbound_wq, work);
4396 void btrfs_init_async_reclaim_work(struct work_struct *work)
4398 INIT_WORK(work, btrfs_async_reclaim_metadata_space);
4402 * reserve_metadata_bytes - try to reserve bytes from the block_rsv's space
4403 * @root - the root we're allocating for
4404 * @block_rsv - the block_rsv we're allocating for
4405 * @orig_bytes - the number of bytes we want
4406 * @flush - whether or not we can flush to make our reservation
4408 * This will reserve orgi_bytes number of bytes from the space info associated
4409 * with the block_rsv. If there is not enough space it will make an attempt to
4410 * flush out space to make room. It will do this by flushing delalloc if
4411 * possible or committing the transaction. If flush is 0 then no attempts to
4412 * regain reservations will be made and this will fail if there is not enough
4415 static int reserve_metadata_bytes(struct btrfs_root *root,
4416 struct btrfs_block_rsv *block_rsv,
4418 enum btrfs_reserve_flush_enum flush)
4420 struct btrfs_space_info *space_info = block_rsv->space_info;
4422 u64 num_bytes = orig_bytes;
4423 int flush_state = FLUSH_DELAYED_ITEMS_NR;
4425 bool flushing = false;
4429 spin_lock(&space_info->lock);
4431 * We only want to wait if somebody other than us is flushing and we
4432 * are actually allowed to flush all things.
4434 while (flush == BTRFS_RESERVE_FLUSH_ALL && !flushing &&
4435 space_info->flush) {
4436 spin_unlock(&space_info->lock);
4438 * If we have a trans handle we can't wait because the flusher
4439 * may have to commit the transaction, which would mean we would
4440 * deadlock since we are waiting for the flusher to finish, but
4441 * hold the current transaction open.
4443 if (current->journal_info)
4445 ret = wait_event_killable(space_info->wait, !space_info->flush);
4446 /* Must have been killed, return */
4450 spin_lock(&space_info->lock);
4454 used = space_info->bytes_used + space_info->bytes_reserved +
4455 space_info->bytes_pinned + space_info->bytes_readonly +
4456 space_info->bytes_may_use;
4459 * The idea here is that we've not already over-reserved the block group
4460 * then we can go ahead and save our reservation first and then start
4461 * flushing if we need to. Otherwise if we've already overcommitted
4462 * lets start flushing stuff first and then come back and try to make
4465 if (used <= space_info->total_bytes) {
4466 if (used + orig_bytes <= space_info->total_bytes) {
4467 space_info->bytes_may_use += orig_bytes;
4468 trace_btrfs_space_reservation(root->fs_info,
4469 "space_info", space_info->flags, orig_bytes, 1);
4473 * Ok set num_bytes to orig_bytes since we aren't
4474 * overocmmitted, this way we only try and reclaim what
4477 num_bytes = orig_bytes;
4481 * Ok we're over committed, set num_bytes to the overcommitted
4482 * amount plus the amount of bytes that we need for this
4485 num_bytes = used - space_info->total_bytes +
4489 if (ret && can_overcommit(root, space_info, orig_bytes, flush)) {
4490 space_info->bytes_may_use += orig_bytes;
4491 trace_btrfs_space_reservation(root->fs_info, "space_info",
4492 space_info->flags, orig_bytes,
4498 * Couldn't make our reservation, save our place so while we're trying
4499 * to reclaim space we can actually use it instead of somebody else
4500 * stealing it from us.
4502 * We make the other tasks wait for the flush only when we can flush
4505 if (ret && flush != BTRFS_RESERVE_NO_FLUSH) {
4507 space_info->flush = 1;
4508 } else if (!ret && space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
4510 if (need_do_async_reclaim(space_info, root->fs_info, used) &&
4511 !work_busy(&root->fs_info->async_reclaim_work))
4512 queue_work(system_unbound_wq,
4513 &root->fs_info->async_reclaim_work);
4515 spin_unlock(&space_info->lock);
4517 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
4520 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4525 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4526 * would happen. So skip delalloc flush.
4528 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4529 (flush_state == FLUSH_DELALLOC ||
4530 flush_state == FLUSH_DELALLOC_WAIT))
4531 flush_state = ALLOC_CHUNK;
4535 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4536 flush_state < COMMIT_TRANS)
4538 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4539 flush_state <= COMMIT_TRANS)
4543 if (ret == -ENOSPC &&
4544 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4545 struct btrfs_block_rsv *global_rsv =
4546 &root->fs_info->global_block_rsv;
4548 if (block_rsv != global_rsv &&
4549 !block_rsv_use_bytes(global_rsv, orig_bytes))
4553 trace_btrfs_space_reservation(root->fs_info,
4554 "space_info:enospc",
4555 space_info->flags, orig_bytes, 1);
4557 spin_lock(&space_info->lock);
4558 space_info->flush = 0;
4559 wake_up_all(&space_info->wait);
4560 spin_unlock(&space_info->lock);
4565 static struct btrfs_block_rsv *get_block_rsv(
4566 const struct btrfs_trans_handle *trans,
4567 const struct btrfs_root *root)
4569 struct btrfs_block_rsv *block_rsv = NULL;
4571 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4572 block_rsv = trans->block_rsv;
4574 if (root == root->fs_info->csum_root && trans->adding_csums)
4575 block_rsv = trans->block_rsv;
4577 if (root == root->fs_info->uuid_root)
4578 block_rsv = trans->block_rsv;
4581 block_rsv = root->block_rsv;
4584 block_rsv = &root->fs_info->empty_block_rsv;
4589 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4593 spin_lock(&block_rsv->lock);
4594 if (block_rsv->reserved >= num_bytes) {
4595 block_rsv->reserved -= num_bytes;
4596 if (block_rsv->reserved < block_rsv->size)
4597 block_rsv->full = 0;
4600 spin_unlock(&block_rsv->lock);
4604 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4605 u64 num_bytes, int update_size)
4607 spin_lock(&block_rsv->lock);
4608 block_rsv->reserved += num_bytes;
4610 block_rsv->size += num_bytes;
4611 else if (block_rsv->reserved >= block_rsv->size)
4612 block_rsv->full = 1;
4613 spin_unlock(&block_rsv->lock);
4616 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4617 struct btrfs_block_rsv *dest, u64 num_bytes,
4620 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4623 if (global_rsv->space_info != dest->space_info)
4626 spin_lock(&global_rsv->lock);
4627 min_bytes = div_factor(global_rsv->size, min_factor);
4628 if (global_rsv->reserved < min_bytes + num_bytes) {
4629 spin_unlock(&global_rsv->lock);
4632 global_rsv->reserved -= num_bytes;
4633 if (global_rsv->reserved < global_rsv->size)
4634 global_rsv->full = 0;
4635 spin_unlock(&global_rsv->lock);
4637 block_rsv_add_bytes(dest, num_bytes, 1);
4641 static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4642 struct btrfs_block_rsv *block_rsv,
4643 struct btrfs_block_rsv *dest, u64 num_bytes)
4645 struct btrfs_space_info *space_info = block_rsv->space_info;
4647 spin_lock(&block_rsv->lock);
4648 if (num_bytes == (u64)-1)
4649 num_bytes = block_rsv->size;
4650 block_rsv->size -= num_bytes;
4651 if (block_rsv->reserved >= block_rsv->size) {
4652 num_bytes = block_rsv->reserved - block_rsv->size;
4653 block_rsv->reserved = block_rsv->size;
4654 block_rsv->full = 1;
4658 spin_unlock(&block_rsv->lock);
4660 if (num_bytes > 0) {
4662 spin_lock(&dest->lock);
4666 bytes_to_add = dest->size - dest->reserved;
4667 bytes_to_add = min(num_bytes, bytes_to_add);
4668 dest->reserved += bytes_to_add;
4669 if (dest->reserved >= dest->size)
4671 num_bytes -= bytes_to_add;
4673 spin_unlock(&dest->lock);
4676 spin_lock(&space_info->lock);
4677 space_info->bytes_may_use -= num_bytes;
4678 trace_btrfs_space_reservation(fs_info, "space_info",
4679 space_info->flags, num_bytes, 0);
4680 spin_unlock(&space_info->lock);
4685 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4686 struct btrfs_block_rsv *dst, u64 num_bytes)
4690 ret = block_rsv_use_bytes(src, num_bytes);
4694 block_rsv_add_bytes(dst, num_bytes, 1);
4698 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
4700 memset(rsv, 0, sizeof(*rsv));
4701 spin_lock_init(&rsv->lock);
4705 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4706 unsigned short type)
4708 struct btrfs_block_rsv *block_rsv;
4709 struct btrfs_fs_info *fs_info = root->fs_info;
4711 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4715 btrfs_init_block_rsv(block_rsv, type);
4716 block_rsv->space_info = __find_space_info(fs_info,
4717 BTRFS_BLOCK_GROUP_METADATA);
4721 void btrfs_free_block_rsv(struct btrfs_root *root,
4722 struct btrfs_block_rsv *rsv)
4726 btrfs_block_rsv_release(root, rsv, (u64)-1);
4730 int btrfs_block_rsv_add(struct btrfs_root *root,
4731 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4732 enum btrfs_reserve_flush_enum flush)
4739 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
4741 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4748 int btrfs_block_rsv_check(struct btrfs_root *root,
4749 struct btrfs_block_rsv *block_rsv, int min_factor)
4757 spin_lock(&block_rsv->lock);
4758 num_bytes = div_factor(block_rsv->size, min_factor);
4759 if (block_rsv->reserved >= num_bytes)
4761 spin_unlock(&block_rsv->lock);
4766 int btrfs_block_rsv_refill(struct btrfs_root *root,
4767 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4768 enum btrfs_reserve_flush_enum flush)
4776 spin_lock(&block_rsv->lock);
4777 num_bytes = min_reserved;
4778 if (block_rsv->reserved >= num_bytes)
4781 num_bytes -= block_rsv->reserved;
4782 spin_unlock(&block_rsv->lock);
4787 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
4789 block_rsv_add_bytes(block_rsv, num_bytes, 0);
4796 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4797 struct btrfs_block_rsv *dst_rsv,
4800 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4803 void btrfs_block_rsv_release(struct btrfs_root *root,
4804 struct btrfs_block_rsv *block_rsv,
4807 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4808 if (global_rsv == block_rsv ||
4809 block_rsv->space_info != global_rsv->space_info)
4811 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4816 * helper to calculate size of global block reservation.
4817 * the desired value is sum of space used by extent tree,
4818 * checksum tree and root tree
4820 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4822 struct btrfs_space_info *sinfo;
4826 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
4828 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4829 spin_lock(&sinfo->lock);
4830 data_used = sinfo->bytes_used;
4831 spin_unlock(&sinfo->lock);
4833 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4834 spin_lock(&sinfo->lock);
4835 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4837 meta_used = sinfo->bytes_used;
4838 spin_unlock(&sinfo->lock);
4840 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4842 num_bytes += div64_u64(data_used + meta_used, 50);
4844 if (num_bytes * 3 > meta_used)
4845 num_bytes = div64_u64(meta_used, 3);
4847 return ALIGN(num_bytes, fs_info->extent_root->leafsize << 10);
4850 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4852 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4853 struct btrfs_space_info *sinfo = block_rsv->space_info;
4856 num_bytes = calc_global_metadata_size(fs_info);
4858 spin_lock(&sinfo->lock);
4859 spin_lock(&block_rsv->lock);
4861 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
4863 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
4864 sinfo->bytes_reserved + sinfo->bytes_readonly +
4865 sinfo->bytes_may_use;
4867 if (sinfo->total_bytes > num_bytes) {
4868 num_bytes = sinfo->total_bytes - num_bytes;
4869 block_rsv->reserved += num_bytes;
4870 sinfo->bytes_may_use += num_bytes;
4871 trace_btrfs_space_reservation(fs_info, "space_info",
4872 sinfo->flags, num_bytes, 1);
4875 if (block_rsv->reserved >= block_rsv->size) {
4876 num_bytes = block_rsv->reserved - block_rsv->size;
4877 sinfo->bytes_may_use -= num_bytes;
4878 trace_btrfs_space_reservation(fs_info, "space_info",
4879 sinfo->flags, num_bytes, 0);
4880 block_rsv->reserved = block_rsv->size;
4881 block_rsv->full = 1;
4884 spin_unlock(&block_rsv->lock);
4885 spin_unlock(&sinfo->lock);
4888 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4890 struct btrfs_space_info *space_info;
4892 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4893 fs_info->chunk_block_rsv.space_info = space_info;
4895 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4896 fs_info->global_block_rsv.space_info = space_info;
4897 fs_info->delalloc_block_rsv.space_info = space_info;
4898 fs_info->trans_block_rsv.space_info = space_info;
4899 fs_info->empty_block_rsv.space_info = space_info;
4900 fs_info->delayed_block_rsv.space_info = space_info;
4902 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4903 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4904 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4905 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
4906 if (fs_info->quota_root)
4907 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
4908 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
4910 update_global_block_rsv(fs_info);
4913 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4915 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4917 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4918 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4919 WARN_ON(fs_info->trans_block_rsv.size > 0);
4920 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4921 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4922 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
4923 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4924 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
4927 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4928 struct btrfs_root *root)
4930 if (!trans->block_rsv)
4933 if (!trans->bytes_reserved)
4936 trace_btrfs_space_reservation(root->fs_info, "transaction",
4937 trans->transid, trans->bytes_reserved, 0);
4938 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
4939 trans->bytes_reserved = 0;
4942 /* Can only return 0 or -ENOSPC */
4943 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4944 struct inode *inode)
4946 struct btrfs_root *root = BTRFS_I(inode)->root;
4947 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4948 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4951 * We need to hold space in order to delete our orphan item once we've
4952 * added it, so this takes the reservation so we can release it later
4953 * when we are truly done with the orphan item.
4955 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4956 trace_btrfs_space_reservation(root->fs_info, "orphan",
4957 btrfs_ino(inode), num_bytes, 1);
4958 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4961 void btrfs_orphan_release_metadata(struct inode *inode)
4963 struct btrfs_root *root = BTRFS_I(inode)->root;
4964 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4965 trace_btrfs_space_reservation(root->fs_info, "orphan",
4966 btrfs_ino(inode), num_bytes, 0);
4967 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4971 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4972 * root: the root of the parent directory
4973 * rsv: block reservation
4974 * items: the number of items that we need do reservation
4975 * qgroup_reserved: used to return the reserved size in qgroup
4977 * This function is used to reserve the space for snapshot/subvolume
4978 * creation and deletion. Those operations are different with the
4979 * common file/directory operations, they change two fs/file trees
4980 * and root tree, the number of items that the qgroup reserves is
4981 * different with the free space reservation. So we can not use
4982 * the space reseravtion mechanism in start_transaction().
4984 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4985 struct btrfs_block_rsv *rsv,
4987 u64 *qgroup_reserved,
4988 bool use_global_rsv)
4992 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4994 if (root->fs_info->quota_enabled) {
4995 /* One for parent inode, two for dir entries */
4996 num_bytes = 3 * root->leafsize;
4997 ret = btrfs_qgroup_reserve(root, num_bytes);
5004 *qgroup_reserved = num_bytes;
5006 num_bytes = btrfs_calc_trans_metadata_size(root, items);
5007 rsv->space_info = __find_space_info(root->fs_info,
5008 BTRFS_BLOCK_GROUP_METADATA);
5009 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5010 BTRFS_RESERVE_FLUSH_ALL);
5012 if (ret == -ENOSPC && use_global_rsv)
5013 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
5016 if (*qgroup_reserved)
5017 btrfs_qgroup_free(root, *qgroup_reserved);
5023 void btrfs_subvolume_release_metadata(struct btrfs_root *root,
5024 struct btrfs_block_rsv *rsv,
5025 u64 qgroup_reserved)
5027 btrfs_block_rsv_release(root, rsv, (u64)-1);
5028 if (qgroup_reserved)
5029 btrfs_qgroup_free(root, qgroup_reserved);
5033 * drop_outstanding_extent - drop an outstanding extent
5034 * @inode: the inode we're dropping the extent for
5036 * This is called when we are freeing up an outstanding extent, either called
5037 * after an error or after an extent is written. This will return the number of
5038 * reserved extents that need to be freed. This must be called with
5039 * BTRFS_I(inode)->lock held.
5041 static unsigned drop_outstanding_extent(struct inode *inode)
5043 unsigned drop_inode_space = 0;
5044 unsigned dropped_extents = 0;
5046 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
5047 BTRFS_I(inode)->outstanding_extents--;
5049 if (BTRFS_I(inode)->outstanding_extents == 0 &&
5050 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5051 &BTRFS_I(inode)->runtime_flags))
5052 drop_inode_space = 1;
5055 * If we have more or the same amount of outsanding extents than we have
5056 * reserved then we need to leave the reserved extents count alone.
5058 if (BTRFS_I(inode)->outstanding_extents >=
5059 BTRFS_I(inode)->reserved_extents)
5060 return drop_inode_space;
5062 dropped_extents = BTRFS_I(inode)->reserved_extents -
5063 BTRFS_I(inode)->outstanding_extents;
5064 BTRFS_I(inode)->reserved_extents -= dropped_extents;
5065 return dropped_extents + drop_inode_space;
5069 * calc_csum_metadata_size - return the amount of metada space that must be
5070 * reserved/free'd for the given bytes.
5071 * @inode: the inode we're manipulating
5072 * @num_bytes: the number of bytes in question
5073 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5075 * This adjusts the number of csum_bytes in the inode and then returns the
5076 * correct amount of metadata that must either be reserved or freed. We
5077 * calculate how many checksums we can fit into one leaf and then divide the
5078 * number of bytes that will need to be checksumed by this value to figure out
5079 * how many checksums will be required. If we are adding bytes then the number
5080 * may go up and we will return the number of additional bytes that must be
5081 * reserved. If it is going down we will return the number of bytes that must
5084 * This must be called with BTRFS_I(inode)->lock held.
5086 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5089 struct btrfs_root *root = BTRFS_I(inode)->root;
5091 int num_csums_per_leaf;
5095 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5096 BTRFS_I(inode)->csum_bytes == 0)
5099 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5101 BTRFS_I(inode)->csum_bytes += num_bytes;
5103 BTRFS_I(inode)->csum_bytes -= num_bytes;
5104 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
5105 num_csums_per_leaf = (int)div64_u64(csum_size,
5106 sizeof(struct btrfs_csum_item) +
5107 sizeof(struct btrfs_disk_key));
5108 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5109 num_csums = num_csums + num_csums_per_leaf - 1;
5110 num_csums = num_csums / num_csums_per_leaf;
5112 old_csums = old_csums + num_csums_per_leaf - 1;
5113 old_csums = old_csums / num_csums_per_leaf;
5115 /* No change, no need to reserve more */
5116 if (old_csums == num_csums)
5120 return btrfs_calc_trans_metadata_size(root,
5121 num_csums - old_csums);
5123 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
5126 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5128 struct btrfs_root *root = BTRFS_I(inode)->root;
5129 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
5132 unsigned nr_extents = 0;
5133 int extra_reserve = 0;
5134 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
5136 bool delalloc_lock = true;
5140 /* If we are a free space inode we need to not flush since we will be in
5141 * the middle of a transaction commit. We also don't need the delalloc
5142 * mutex since we won't race with anybody. We need this mostly to make
5143 * lockdep shut its filthy mouth.
5145 if (btrfs_is_free_space_inode(inode)) {
5146 flush = BTRFS_RESERVE_NO_FLUSH;
5147 delalloc_lock = false;
5150 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5151 btrfs_transaction_in_commit(root->fs_info))
5152 schedule_timeout(1);
5155 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5157 num_bytes = ALIGN(num_bytes, root->sectorsize);
5159 spin_lock(&BTRFS_I(inode)->lock);
5160 BTRFS_I(inode)->outstanding_extents++;
5162 if (BTRFS_I(inode)->outstanding_extents >
5163 BTRFS_I(inode)->reserved_extents)
5164 nr_extents = BTRFS_I(inode)->outstanding_extents -
5165 BTRFS_I(inode)->reserved_extents;
5168 * Add an item to reserve for updating the inode when we complete the
5171 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5172 &BTRFS_I(inode)->runtime_flags)) {
5177 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
5178 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
5179 csum_bytes = BTRFS_I(inode)->csum_bytes;
5180 spin_unlock(&BTRFS_I(inode)->lock);
5182 if (root->fs_info->quota_enabled) {
5183 ret = btrfs_qgroup_reserve(root, num_bytes +
5184 nr_extents * root->leafsize);
5189 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5190 if (unlikely(ret)) {
5191 if (root->fs_info->quota_enabled)
5192 btrfs_qgroup_free(root, num_bytes +
5193 nr_extents * root->leafsize);
5197 spin_lock(&BTRFS_I(inode)->lock);
5198 if (extra_reserve) {
5199 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5200 &BTRFS_I(inode)->runtime_flags);
5203 BTRFS_I(inode)->reserved_extents += nr_extents;
5204 spin_unlock(&BTRFS_I(inode)->lock);
5207 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5210 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5211 btrfs_ino(inode), to_reserve, 1);
5212 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5217 spin_lock(&BTRFS_I(inode)->lock);
5218 dropped = drop_outstanding_extent(inode);
5220 * If the inodes csum_bytes is the same as the original
5221 * csum_bytes then we know we haven't raced with any free()ers
5222 * so we can just reduce our inodes csum bytes and carry on.
5224 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
5225 calc_csum_metadata_size(inode, num_bytes, 0);
5227 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5231 * This is tricky, but first we need to figure out how much we
5232 * free'd from any free-ers that occured during this
5233 * reservation, so we reset ->csum_bytes to the csum_bytes
5234 * before we dropped our lock, and then call the free for the
5235 * number of bytes that were freed while we were trying our
5238 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5239 BTRFS_I(inode)->csum_bytes = csum_bytes;
5240 to_free = calc_csum_metadata_size(inode, bytes, 0);
5244 * Now we need to see how much we would have freed had we not
5245 * been making this reservation and our ->csum_bytes were not
5246 * artificially inflated.
5248 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5249 bytes = csum_bytes - orig_csum_bytes;
5250 bytes = calc_csum_metadata_size(inode, bytes, 0);
5253 * Now reset ->csum_bytes to what it should be. If bytes is
5254 * more than to_free then we would have free'd more space had we
5255 * not had an artificially high ->csum_bytes, so we need to free
5256 * the remainder. If bytes is the same or less then we don't
5257 * need to do anything, the other free-ers did the correct
5260 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5261 if (bytes > to_free)
5262 to_free = bytes - to_free;
5266 spin_unlock(&BTRFS_I(inode)->lock);
5268 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5271 btrfs_block_rsv_release(root, block_rsv, to_free);
5272 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5273 btrfs_ino(inode), to_free, 0);
5276 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5281 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5282 * @inode: the inode to release the reservation for
5283 * @num_bytes: the number of bytes we're releasing
5285 * This will release the metadata reservation for an inode. This can be called
5286 * once we complete IO for a given set of bytes to release their metadata
5289 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5291 struct btrfs_root *root = BTRFS_I(inode)->root;
5295 num_bytes = ALIGN(num_bytes, root->sectorsize);
5296 spin_lock(&BTRFS_I(inode)->lock);
5297 dropped = drop_outstanding_extent(inode);
5300 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
5301 spin_unlock(&BTRFS_I(inode)->lock);
5303 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5305 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5306 btrfs_ino(inode), to_free, 0);
5307 if (root->fs_info->quota_enabled) {
5308 btrfs_qgroup_free(root, num_bytes +
5309 dropped * root->leafsize);
5312 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5317 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5318 * @inode: inode we're writing to
5319 * @num_bytes: the number of bytes we want to allocate
5321 * This will do the following things
5323 * o reserve space in the data space info for num_bytes
5324 * o reserve space in the metadata space info based on number of outstanding
5325 * extents and how much csums will be needed
5326 * o add to the inodes ->delalloc_bytes
5327 * o add it to the fs_info's delalloc inodes list.
5329 * This will return 0 for success and -ENOSPC if there is no space left.
5331 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5335 ret = btrfs_check_data_free_space(inode, num_bytes);
5339 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5341 btrfs_free_reserved_data_space(inode, num_bytes);
5349 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5350 * @inode: inode we're releasing space for
5351 * @num_bytes: the number of bytes we want to free up
5353 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5354 * called in the case that we don't need the metadata AND data reservations
5355 * anymore. So if there is an error or we insert an inline extent.
5357 * This function will release the metadata space that was not used and will
5358 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5359 * list if there are no delalloc bytes left.
5361 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5363 btrfs_delalloc_release_metadata(inode, num_bytes);
5364 btrfs_free_reserved_data_space(inode, num_bytes);
5367 static int update_block_group(struct btrfs_root *root,
5368 u64 bytenr, u64 num_bytes, int alloc)
5370 struct btrfs_block_group_cache *cache = NULL;
5371 struct btrfs_fs_info *info = root->fs_info;
5372 u64 total = num_bytes;
5377 /* block accounting for super block */
5378 spin_lock(&info->delalloc_root_lock);
5379 old_val = btrfs_super_bytes_used(info->super_copy);
5381 old_val += num_bytes;
5383 old_val -= num_bytes;
5384 btrfs_set_super_bytes_used(info->super_copy, old_val);
5385 spin_unlock(&info->delalloc_root_lock);
5388 cache = btrfs_lookup_block_group(info, bytenr);
5391 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5392 BTRFS_BLOCK_GROUP_RAID1 |
5393 BTRFS_BLOCK_GROUP_RAID10))
5398 * If this block group has free space cache written out, we
5399 * need to make sure to load it if we are removing space. This
5400 * is because we need the unpinning stage to actually add the
5401 * space back to the block group, otherwise we will leak space.
5403 if (!alloc && cache->cached == BTRFS_CACHE_NO)
5404 cache_block_group(cache, 1);
5406 byte_in_group = bytenr - cache->key.objectid;
5407 WARN_ON(byte_in_group > cache->key.offset);
5409 spin_lock(&cache->space_info->lock);
5410 spin_lock(&cache->lock);
5412 if (btrfs_test_opt(root, SPACE_CACHE) &&
5413 cache->disk_cache_state < BTRFS_DC_CLEAR)
5414 cache->disk_cache_state = BTRFS_DC_CLEAR;
5417 old_val = btrfs_block_group_used(&cache->item);
5418 num_bytes = min(total, cache->key.offset - byte_in_group);
5420 old_val += num_bytes;
5421 btrfs_set_block_group_used(&cache->item, old_val);
5422 cache->reserved -= num_bytes;
5423 cache->space_info->bytes_reserved -= num_bytes;
5424 cache->space_info->bytes_used += num_bytes;
5425 cache->space_info->disk_used += num_bytes * factor;
5426 spin_unlock(&cache->lock);
5427 spin_unlock(&cache->space_info->lock);
5429 old_val -= num_bytes;
5430 btrfs_set_block_group_used(&cache->item, old_val);
5431 cache->pinned += num_bytes;
5432 cache->space_info->bytes_pinned += num_bytes;
5433 cache->space_info->bytes_used -= num_bytes;
5434 cache->space_info->disk_used -= num_bytes * factor;
5435 spin_unlock(&cache->lock);
5436 spin_unlock(&cache->space_info->lock);
5438 set_extent_dirty(info->pinned_extents,
5439 bytenr, bytenr + num_bytes - 1,
5440 GFP_NOFS | __GFP_NOFAIL);
5442 btrfs_put_block_group(cache);
5444 bytenr += num_bytes;
5449 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5451 struct btrfs_block_group_cache *cache;
5454 spin_lock(&root->fs_info->block_group_cache_lock);
5455 bytenr = root->fs_info->first_logical_byte;
5456 spin_unlock(&root->fs_info->block_group_cache_lock);
5458 if (bytenr < (u64)-1)
5461 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5465 bytenr = cache->key.objectid;
5466 btrfs_put_block_group(cache);
5471 static int pin_down_extent(struct btrfs_root *root,
5472 struct btrfs_block_group_cache *cache,
5473 u64 bytenr, u64 num_bytes, int reserved)
5475 spin_lock(&cache->space_info->lock);
5476 spin_lock(&cache->lock);
5477 cache->pinned += num_bytes;
5478 cache->space_info->bytes_pinned += num_bytes;
5480 cache->reserved -= num_bytes;
5481 cache->space_info->bytes_reserved -= num_bytes;
5483 spin_unlock(&cache->lock);
5484 spin_unlock(&cache->space_info->lock);
5486 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5487 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
5489 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
5494 * this function must be called within transaction
5496 int btrfs_pin_extent(struct btrfs_root *root,
5497 u64 bytenr, u64 num_bytes, int reserved)
5499 struct btrfs_block_group_cache *cache;
5501 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
5502 BUG_ON(!cache); /* Logic error */
5504 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5506 btrfs_put_block_group(cache);
5511 * this function must be called within transaction
5513 int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
5514 u64 bytenr, u64 num_bytes)
5516 struct btrfs_block_group_cache *cache;
5519 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
5524 * pull in the free space cache (if any) so that our pin
5525 * removes the free space from the cache. We have load_only set
5526 * to one because the slow code to read in the free extents does check
5527 * the pinned extents.
5529 cache_block_group(cache, 1);
5531 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5533 /* remove us from the free space cache (if we're there at all) */
5534 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
5535 btrfs_put_block_group(cache);
5539 static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5542 struct btrfs_block_group_cache *block_group;
5543 struct btrfs_caching_control *caching_ctl;
5545 block_group = btrfs_lookup_block_group(root->fs_info, start);
5549 cache_block_group(block_group, 0);
5550 caching_ctl = get_caching_control(block_group);
5554 BUG_ON(!block_group_cache_done(block_group));
5555 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5557 mutex_lock(&caching_ctl->mutex);
5559 if (start >= caching_ctl->progress) {
5560 ret = add_excluded_extent(root, start, num_bytes);
5561 } else if (start + num_bytes <= caching_ctl->progress) {
5562 ret = btrfs_remove_free_space(block_group,
5565 num_bytes = caching_ctl->progress - start;
5566 ret = btrfs_remove_free_space(block_group,
5571 num_bytes = (start + num_bytes) -
5572 caching_ctl->progress;
5573 start = caching_ctl->progress;
5574 ret = add_excluded_extent(root, start, num_bytes);
5577 mutex_unlock(&caching_ctl->mutex);
5578 put_caching_control(caching_ctl);
5580 btrfs_put_block_group(block_group);
5584 int btrfs_exclude_logged_extents(struct btrfs_root *log,
5585 struct extent_buffer *eb)
5587 struct btrfs_file_extent_item *item;
5588 struct btrfs_key key;
5592 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5595 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5596 btrfs_item_key_to_cpu(eb, &key, i);
5597 if (key.type != BTRFS_EXTENT_DATA_KEY)
5599 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5600 found_type = btrfs_file_extent_type(eb, item);
5601 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5603 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5605 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5606 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5607 __exclude_logged_extent(log, key.objectid, key.offset);
5614 * btrfs_update_reserved_bytes - update the block_group and space info counters
5615 * @cache: The cache we are manipulating
5616 * @num_bytes: The number of bytes in question
5617 * @reserve: One of the reservation enums
5618 * @delalloc: The blocks are allocated for the delalloc write
5620 * This is called by the allocator when it reserves space, or by somebody who is
5621 * freeing space that was never actually used on disk. For example if you
5622 * reserve some space for a new leaf in transaction A and before transaction A
5623 * commits you free that leaf, you call this with reserve set to 0 in order to
5624 * clear the reservation.
5626 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5627 * ENOSPC accounting. For data we handle the reservation through clearing the
5628 * delalloc bits in the io_tree. We have to do this since we could end up
5629 * allocating less disk space for the amount of data we have reserved in the
5630 * case of compression.
5632 * If this is a reservation and the block group has become read only we cannot
5633 * make the reservation and return -EAGAIN, otherwise this function always
5636 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5637 u64 num_bytes, int reserve, int delalloc)
5639 struct btrfs_space_info *space_info = cache->space_info;
5642 spin_lock(&space_info->lock);
5643 spin_lock(&cache->lock);
5644 if (reserve != RESERVE_FREE) {
5648 cache->reserved += num_bytes;
5649 space_info->bytes_reserved += num_bytes;
5650 if (reserve == RESERVE_ALLOC) {
5651 trace_btrfs_space_reservation(cache->fs_info,
5652 "space_info", space_info->flags,
5654 space_info->bytes_may_use -= num_bytes;
5658 cache->delalloc_bytes += num_bytes;
5662 space_info->bytes_readonly += num_bytes;
5663 cache->reserved -= num_bytes;
5664 space_info->bytes_reserved -= num_bytes;
5667 cache->delalloc_bytes -= num_bytes;
5669 spin_unlock(&cache->lock);
5670 spin_unlock(&space_info->lock);
5674 void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
5675 struct btrfs_root *root)
5677 struct btrfs_fs_info *fs_info = root->fs_info;
5678 struct btrfs_caching_control *next;
5679 struct btrfs_caching_control *caching_ctl;
5680 struct btrfs_block_group_cache *cache;
5682 down_write(&fs_info->commit_root_sem);
5684 list_for_each_entry_safe(caching_ctl, next,
5685 &fs_info->caching_block_groups, list) {
5686 cache = caching_ctl->block_group;
5687 if (block_group_cache_done(cache)) {
5688 cache->last_byte_to_unpin = (u64)-1;
5689 list_del_init(&caching_ctl->list);
5690 put_caching_control(caching_ctl);
5692 cache->last_byte_to_unpin = caching_ctl->progress;
5696 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5697 fs_info->pinned_extents = &fs_info->freed_extents[1];
5699 fs_info->pinned_extents = &fs_info->freed_extents[0];
5701 up_write(&fs_info->commit_root_sem);
5703 update_global_block_rsv(fs_info);
5706 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5708 struct btrfs_fs_info *fs_info = root->fs_info;
5709 struct btrfs_block_group_cache *cache = NULL;
5710 struct btrfs_space_info *space_info;
5711 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5715 while (start <= end) {
5718 start >= cache->key.objectid + cache->key.offset) {
5720 btrfs_put_block_group(cache);
5721 cache = btrfs_lookup_block_group(fs_info, start);
5722 BUG_ON(!cache); /* Logic error */
5725 len = cache->key.objectid + cache->key.offset - start;
5726 len = min(len, end + 1 - start);
5728 if (start < cache->last_byte_to_unpin) {
5729 len = min(len, cache->last_byte_to_unpin - start);
5730 btrfs_add_free_space(cache, start, len);
5734 space_info = cache->space_info;
5736 spin_lock(&space_info->lock);
5737 spin_lock(&cache->lock);
5738 cache->pinned -= len;
5739 space_info->bytes_pinned -= len;
5740 percpu_counter_add(&space_info->total_bytes_pinned, -len);
5742 space_info->bytes_readonly += len;
5745 spin_unlock(&cache->lock);
5746 if (!readonly && global_rsv->space_info == space_info) {
5747 spin_lock(&global_rsv->lock);
5748 if (!global_rsv->full) {
5749 len = min(len, global_rsv->size -
5750 global_rsv->reserved);
5751 global_rsv->reserved += len;
5752 space_info->bytes_may_use += len;
5753 if (global_rsv->reserved >= global_rsv->size)
5754 global_rsv->full = 1;
5756 spin_unlock(&global_rsv->lock);
5758 spin_unlock(&space_info->lock);
5762 btrfs_put_block_group(cache);
5766 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
5767 struct btrfs_root *root)
5769 struct btrfs_fs_info *fs_info = root->fs_info;
5770 struct extent_io_tree *unpin;
5778 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5779 unpin = &fs_info->freed_extents[1];
5781 unpin = &fs_info->freed_extents[0];
5784 ret = find_first_extent_bit(unpin, 0, &start, &end,
5785 EXTENT_DIRTY, NULL);
5789 if (btrfs_test_opt(root, DISCARD))
5790 ret = btrfs_discard_extent(root, start,
5791 end + 1 - start, NULL);
5793 clear_extent_dirty(unpin, start, end, GFP_NOFS);
5794 unpin_extent_range(root, start, end);
5801 static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5802 u64 owner, u64 root_objectid)
5804 struct btrfs_space_info *space_info;
5807 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5808 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5809 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5811 flags = BTRFS_BLOCK_GROUP_METADATA;
5813 flags = BTRFS_BLOCK_GROUP_DATA;
5816 space_info = __find_space_info(fs_info, flags);
5817 BUG_ON(!space_info); /* Logic bug */
5818 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5822 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5823 struct btrfs_root *root,
5824 u64 bytenr, u64 num_bytes, u64 parent,
5825 u64 root_objectid, u64 owner_objectid,
5826 u64 owner_offset, int refs_to_drop,
5827 struct btrfs_delayed_extent_op *extent_op,
5830 struct btrfs_key key;
5831 struct btrfs_path *path;
5832 struct btrfs_fs_info *info = root->fs_info;
5833 struct btrfs_root *extent_root = info->extent_root;
5834 struct extent_buffer *leaf;
5835 struct btrfs_extent_item *ei;
5836 struct btrfs_extent_inline_ref *iref;
5839 int extent_slot = 0;
5840 int found_extent = 0;
5845 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
5846 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5849 if (!info->quota_enabled || !is_fstree(root_objectid))
5852 path = btrfs_alloc_path();
5857 path->leave_spinning = 1;
5859 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5860 BUG_ON(!is_data && refs_to_drop != 1);
5863 skinny_metadata = 0;
5865 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5866 bytenr, num_bytes, parent,
5867 root_objectid, owner_objectid,
5870 extent_slot = path->slots[0];
5871 while (extent_slot >= 0) {
5872 btrfs_item_key_to_cpu(path->nodes[0], &key,
5874 if (key.objectid != bytenr)
5876 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5877 key.offset == num_bytes) {
5881 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5882 key.offset == owner_objectid) {
5886 if (path->slots[0] - extent_slot > 5)
5890 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5891 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5892 if (found_extent && item_size < sizeof(*ei))
5895 if (!found_extent) {
5897 ret = remove_extent_backref(trans, extent_root, path,
5899 is_data, &last_ref);
5901 btrfs_abort_transaction(trans, extent_root, ret);
5904 btrfs_release_path(path);
5905 path->leave_spinning = 1;
5907 key.objectid = bytenr;
5908 key.type = BTRFS_EXTENT_ITEM_KEY;
5909 key.offset = num_bytes;
5911 if (!is_data && skinny_metadata) {
5912 key.type = BTRFS_METADATA_ITEM_KEY;
5913 key.offset = owner_objectid;
5916 ret = btrfs_search_slot(trans, extent_root,
5918 if (ret > 0 && skinny_metadata && path->slots[0]) {
5920 * Couldn't find our skinny metadata item,
5921 * see if we have ye olde extent item.
5924 btrfs_item_key_to_cpu(path->nodes[0], &key,
5926 if (key.objectid == bytenr &&
5927 key.type == BTRFS_EXTENT_ITEM_KEY &&
5928 key.offset == num_bytes)
5932 if (ret > 0 && skinny_metadata) {
5933 skinny_metadata = false;
5934 key.objectid = bytenr;
5935 key.type = BTRFS_EXTENT_ITEM_KEY;
5936 key.offset = num_bytes;
5937 btrfs_release_path(path);
5938 ret = btrfs_search_slot(trans, extent_root,
5943 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5946 btrfs_print_leaf(extent_root,
5950 btrfs_abort_transaction(trans, extent_root, ret);
5953 extent_slot = path->slots[0];
5955 } else if (WARN_ON(ret == -ENOENT)) {
5956 btrfs_print_leaf(extent_root, path->nodes[0]);
5958 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5959 bytenr, parent, root_objectid, owner_objectid,
5961 btrfs_abort_transaction(trans, extent_root, ret);
5964 btrfs_abort_transaction(trans, extent_root, ret);
5968 leaf = path->nodes[0];
5969 item_size = btrfs_item_size_nr(leaf, extent_slot);
5970 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5971 if (item_size < sizeof(*ei)) {
5972 BUG_ON(found_extent || extent_slot != path->slots[0]);
5973 ret = convert_extent_item_v0(trans, extent_root, path,
5976 btrfs_abort_transaction(trans, extent_root, ret);
5980 btrfs_release_path(path);
5981 path->leave_spinning = 1;
5983 key.objectid = bytenr;
5984 key.type = BTRFS_EXTENT_ITEM_KEY;
5985 key.offset = num_bytes;
5987 ret = btrfs_search_slot(trans, extent_root, &key, path,
5990 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5992 btrfs_print_leaf(extent_root, path->nodes[0]);
5995 btrfs_abort_transaction(trans, extent_root, ret);
5999 extent_slot = path->slots[0];
6000 leaf = path->nodes[0];
6001 item_size = btrfs_item_size_nr(leaf, extent_slot);
6004 BUG_ON(item_size < sizeof(*ei));
6005 ei = btrfs_item_ptr(leaf, extent_slot,
6006 struct btrfs_extent_item);
6007 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6008 key.type == BTRFS_EXTENT_ITEM_KEY) {
6009 struct btrfs_tree_block_info *bi;
6010 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6011 bi = (struct btrfs_tree_block_info *)(ei + 1);
6012 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
6015 refs = btrfs_extent_refs(leaf, ei);
6016 if (refs < refs_to_drop) {
6017 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
6018 "for bytenr %Lu", refs_to_drop, refs, bytenr);
6020 btrfs_abort_transaction(trans, extent_root, ret);
6023 refs -= refs_to_drop;
6026 type = BTRFS_QGROUP_OPER_SUB_SHARED;
6028 __run_delayed_extent_op(extent_op, leaf, ei);
6030 * In the case of inline back ref, reference count will
6031 * be updated by remove_extent_backref
6034 BUG_ON(!found_extent);
6036 btrfs_set_extent_refs(leaf, ei, refs);
6037 btrfs_mark_buffer_dirty(leaf);
6040 ret = remove_extent_backref(trans, extent_root, path,
6042 is_data, &last_ref);
6044 btrfs_abort_transaction(trans, extent_root, ret);
6048 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
6052 BUG_ON(is_data && refs_to_drop !=
6053 extent_data_ref_count(root, path, iref));
6055 BUG_ON(path->slots[0] != extent_slot);
6057 BUG_ON(path->slots[0] != extent_slot + 1);
6058 path->slots[0] = extent_slot;
6064 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
6067 btrfs_abort_transaction(trans, extent_root, ret);
6070 btrfs_release_path(path);
6073 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
6075 btrfs_abort_transaction(trans, extent_root, ret);
6080 ret = update_block_group(root, bytenr, num_bytes, 0);
6082 btrfs_abort_transaction(trans, extent_root, ret);
6086 btrfs_release_path(path);
6088 /* Deal with the quota accounting */
6089 if (!ret && last_ref && !no_quota) {
6092 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
6093 type == BTRFS_QGROUP_OPER_SUB_SHARED)
6096 ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
6097 bytenr, num_bytes, type,
6101 btrfs_free_path(path);
6106 * when we free an block, it is possible (and likely) that we free the last
6107 * delayed ref for that extent as well. This searches the delayed ref tree for
6108 * a given extent, and if there are no other delayed refs to be processed, it
6109 * removes it from the tree.
6111 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6112 struct btrfs_root *root, u64 bytenr)
6114 struct btrfs_delayed_ref_head *head;
6115 struct btrfs_delayed_ref_root *delayed_refs;
6118 delayed_refs = &trans->transaction->delayed_refs;
6119 spin_lock(&delayed_refs->lock);
6120 head = btrfs_find_delayed_ref_head(trans, bytenr);
6122 goto out_delayed_unlock;
6124 spin_lock(&head->lock);
6125 if (rb_first(&head->ref_root))
6128 if (head->extent_op) {
6129 if (!head->must_insert_reserved)
6131 btrfs_free_delayed_extent_op(head->extent_op);
6132 head->extent_op = NULL;
6136 * waiting for the lock here would deadlock. If someone else has it
6137 * locked they are already in the process of dropping it anyway
6139 if (!mutex_trylock(&head->mutex))
6143 * at this point we have a head with no other entries. Go
6144 * ahead and process it.
6146 head->node.in_tree = 0;
6147 rb_erase(&head->href_node, &delayed_refs->href_root);
6149 atomic_dec(&delayed_refs->num_entries);
6152 * we don't take a ref on the node because we're removing it from the
6153 * tree, so we just steal the ref the tree was holding.
6155 delayed_refs->num_heads--;
6156 if (head->processing == 0)
6157 delayed_refs->num_heads_ready--;
6158 head->processing = 0;
6159 spin_unlock(&head->lock);
6160 spin_unlock(&delayed_refs->lock);
6162 BUG_ON(head->extent_op);
6163 if (head->must_insert_reserved)
6166 mutex_unlock(&head->mutex);
6167 btrfs_put_delayed_ref(&head->node);
6170 spin_unlock(&head->lock);
6173 spin_unlock(&delayed_refs->lock);
6177 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6178 struct btrfs_root *root,
6179 struct extent_buffer *buf,
6180 u64 parent, int last_ref)
6182 struct btrfs_block_group_cache *cache = NULL;
6186 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6187 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6188 buf->start, buf->len,
6189 parent, root->root_key.objectid,
6190 btrfs_header_level(buf),
6191 BTRFS_DROP_DELAYED_REF, NULL, 0);
6192 BUG_ON(ret); /* -ENOMEM */
6198 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
6200 if (btrfs_header_generation(buf) == trans->transid) {
6201 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6202 ret = check_ref_cleanup(trans, root, buf->start);
6207 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6208 pin_down_extent(root, cache, buf->start, buf->len, 1);
6212 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6214 btrfs_add_free_space(cache, buf->start, buf->len);
6215 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
6216 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
6221 add_pinned_bytes(root->fs_info, buf->len,
6222 btrfs_header_level(buf),
6223 root->root_key.objectid);
6226 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6229 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
6230 btrfs_put_block_group(cache);
6233 /* Can return -ENOMEM */
6234 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6235 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
6236 u64 owner, u64 offset, int no_quota)
6239 struct btrfs_fs_info *fs_info = root->fs_info;
6241 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
6242 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state)))
6245 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6248 * tree log blocks never actually go into the extent allocation
6249 * tree, just update pinning info and exit early.
6251 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6252 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
6253 /* unlocks the pinned mutex */
6254 btrfs_pin_extent(root, bytenr, num_bytes, 1);
6256 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6257 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6259 parent, root_objectid, (int)owner,
6260 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
6262 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6264 parent, root_objectid, owner,
6265 offset, BTRFS_DROP_DELAYED_REF,
6271 static u64 stripe_align(struct btrfs_root *root,
6272 struct btrfs_block_group_cache *cache,
6273 u64 val, u64 num_bytes)
6275 u64 ret = ALIGN(val, root->stripesize);
6280 * when we wait for progress in the block group caching, its because
6281 * our allocation attempt failed at least once. So, we must sleep
6282 * and let some progress happen before we try again.
6284 * This function will sleep at least once waiting for new free space to
6285 * show up, and then it will check the block group free space numbers
6286 * for our min num_bytes. Another option is to have it go ahead
6287 * and look in the rbtree for a free extent of a given size, but this
6290 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6291 * any of the information in this block group.
6293 static noinline void
6294 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6297 struct btrfs_caching_control *caching_ctl;
6299 caching_ctl = get_caching_control(cache);
6303 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
6304 (cache->free_space_ctl->free_space >= num_bytes));
6306 put_caching_control(caching_ctl);
6310 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6312 struct btrfs_caching_control *caching_ctl;
6315 caching_ctl = get_caching_control(cache);
6317 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
6319 wait_event(caching_ctl->wait, block_group_cache_done(cache));
6320 if (cache->cached == BTRFS_CACHE_ERROR)
6322 put_caching_control(caching_ctl);
6326 int __get_raid_index(u64 flags)
6328 if (flags & BTRFS_BLOCK_GROUP_RAID10)
6329 return BTRFS_RAID_RAID10;
6330 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
6331 return BTRFS_RAID_RAID1;
6332 else if (flags & BTRFS_BLOCK_GROUP_DUP)
6333 return BTRFS_RAID_DUP;
6334 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
6335 return BTRFS_RAID_RAID0;
6336 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
6337 return BTRFS_RAID_RAID5;
6338 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
6339 return BTRFS_RAID_RAID6;
6341 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
6344 int get_block_group_index(struct btrfs_block_group_cache *cache)
6346 return __get_raid_index(cache->flags);
6349 static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6350 [BTRFS_RAID_RAID10] = "raid10",
6351 [BTRFS_RAID_RAID1] = "raid1",
6352 [BTRFS_RAID_DUP] = "dup",
6353 [BTRFS_RAID_RAID0] = "raid0",
6354 [BTRFS_RAID_SINGLE] = "single",
6355 [BTRFS_RAID_RAID5] = "raid5",
6356 [BTRFS_RAID_RAID6] = "raid6",
6359 static const char *get_raid_name(enum btrfs_raid_types type)
6361 if (type >= BTRFS_NR_RAID_TYPES)
6364 return btrfs_raid_type_names[type];
6367 enum btrfs_loop_type {
6368 LOOP_CACHING_NOWAIT = 0,
6369 LOOP_CACHING_WAIT = 1,
6370 LOOP_ALLOC_CHUNK = 2,
6371 LOOP_NO_EMPTY_SIZE = 3,
6375 btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6379 down_read(&cache->data_rwsem);
6383 btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6386 btrfs_get_block_group(cache);
6388 down_read(&cache->data_rwsem);
6391 static struct btrfs_block_group_cache *
6392 btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6393 struct btrfs_free_cluster *cluster,
6396 struct btrfs_block_group_cache *used_bg;
6397 bool locked = false;
6399 spin_lock(&cluster->refill_lock);
6401 if (used_bg == cluster->block_group)
6404 up_read(&used_bg->data_rwsem);
6405 btrfs_put_block_group(used_bg);
6408 used_bg = cluster->block_group;
6412 if (used_bg == block_group)
6415 btrfs_get_block_group(used_bg);
6420 if (down_read_trylock(&used_bg->data_rwsem))
6423 spin_unlock(&cluster->refill_lock);
6424 down_read(&used_bg->data_rwsem);
6430 btrfs_release_block_group(struct btrfs_block_group_cache *cache,
6434 up_read(&cache->data_rwsem);
6435 btrfs_put_block_group(cache);
6439 * walks the btree of allocated extents and find a hole of a given size.
6440 * The key ins is changed to record the hole:
6441 * ins->objectid == start position
6442 * ins->flags = BTRFS_EXTENT_ITEM_KEY
6443 * ins->offset == the size of the hole.
6444 * Any available blocks before search_start are skipped.
6446 * If there is no suitable free space, we will record the max size of
6447 * the free space extent currently.
6449 static noinline int find_free_extent(struct btrfs_root *orig_root,
6450 u64 num_bytes, u64 empty_size,
6451 u64 hint_byte, struct btrfs_key *ins,
6452 u64 flags, int delalloc)
6455 struct btrfs_root *root = orig_root->fs_info->extent_root;
6456 struct btrfs_free_cluster *last_ptr = NULL;
6457 struct btrfs_block_group_cache *block_group = NULL;
6458 u64 search_start = 0;
6459 u64 max_extent_size = 0;
6460 int empty_cluster = 2 * 1024 * 1024;
6461 struct btrfs_space_info *space_info;
6463 int index = __get_raid_index(flags);
6464 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
6465 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
6466 bool failed_cluster_refill = false;
6467 bool failed_alloc = false;
6468 bool use_cluster = true;
6469 bool have_caching_bg = false;
6471 WARN_ON(num_bytes < root->sectorsize);
6472 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
6476 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
6478 space_info = __find_space_info(root->fs_info, flags);
6480 btrfs_err(root->fs_info, "No space info for %llu", flags);
6485 * If the space info is for both data and metadata it means we have a
6486 * small filesystem and we can't use the clustering stuff.
6488 if (btrfs_mixed_space_info(space_info))
6489 use_cluster = false;
6491 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
6492 last_ptr = &root->fs_info->meta_alloc_cluster;
6493 if (!btrfs_test_opt(root, SSD))
6494 empty_cluster = 64 * 1024;
6497 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
6498 btrfs_test_opt(root, SSD)) {
6499 last_ptr = &root->fs_info->data_alloc_cluster;
6503 spin_lock(&last_ptr->lock);
6504 if (last_ptr->block_group)
6505 hint_byte = last_ptr->window_start;
6506 spin_unlock(&last_ptr->lock);
6509 search_start = max(search_start, first_logical_byte(root, 0));
6510 search_start = max(search_start, hint_byte);
6515 if (search_start == hint_byte) {
6516 block_group = btrfs_lookup_block_group(root->fs_info,
6519 * we don't want to use the block group if it doesn't match our
6520 * allocation bits, or if its not cached.
6522 * However if we are re-searching with an ideal block group
6523 * picked out then we don't care that the block group is cached.
6525 if (block_group && block_group_bits(block_group, flags) &&
6526 block_group->cached != BTRFS_CACHE_NO) {
6527 down_read(&space_info->groups_sem);
6528 if (list_empty(&block_group->list) ||
6531 * someone is removing this block group,
6532 * we can't jump into the have_block_group
6533 * target because our list pointers are not
6536 btrfs_put_block_group(block_group);
6537 up_read(&space_info->groups_sem);
6539 index = get_block_group_index(block_group);
6540 btrfs_lock_block_group(block_group, delalloc);
6541 goto have_block_group;
6543 } else if (block_group) {
6544 btrfs_put_block_group(block_group);
6548 have_caching_bg = false;
6549 down_read(&space_info->groups_sem);
6550 list_for_each_entry(block_group, &space_info->block_groups[index],
6555 btrfs_grab_block_group(block_group, delalloc);
6556 search_start = block_group->key.objectid;
6559 * this can happen if we end up cycling through all the
6560 * raid types, but we want to make sure we only allocate
6561 * for the proper type.
6563 if (!block_group_bits(block_group, flags)) {
6564 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6565 BTRFS_BLOCK_GROUP_RAID1 |
6566 BTRFS_BLOCK_GROUP_RAID5 |
6567 BTRFS_BLOCK_GROUP_RAID6 |
6568 BTRFS_BLOCK_GROUP_RAID10;
6571 * if they asked for extra copies and this block group
6572 * doesn't provide them, bail. This does allow us to
6573 * fill raid0 from raid1.
6575 if ((flags & extra) && !(block_group->flags & extra))
6580 cached = block_group_cache_done(block_group);
6581 if (unlikely(!cached)) {
6582 ret = cache_block_group(block_group, 0);
6587 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6589 if (unlikely(block_group->ro))
6593 * Ok we want to try and use the cluster allocator, so
6597 struct btrfs_block_group_cache *used_block_group;
6598 unsigned long aligned_cluster;
6600 * the refill lock keeps out other
6601 * people trying to start a new cluster
6603 used_block_group = btrfs_lock_cluster(block_group,
6606 if (!used_block_group)
6607 goto refill_cluster;
6609 if (used_block_group != block_group &&
6610 (used_block_group->ro ||
6611 !block_group_bits(used_block_group, flags)))
6612 goto release_cluster;
6614 offset = btrfs_alloc_from_cluster(used_block_group,
6617 used_block_group->key.objectid,
6620 /* we have a block, we're done */
6621 spin_unlock(&last_ptr->refill_lock);
6622 trace_btrfs_reserve_extent_cluster(root,
6624 search_start, num_bytes);
6625 if (used_block_group != block_group) {
6626 btrfs_release_block_group(block_group,
6628 block_group = used_block_group;
6633 WARN_ON(last_ptr->block_group != used_block_group);
6635 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6636 * set up a new clusters, so lets just skip it
6637 * and let the allocator find whatever block
6638 * it can find. If we reach this point, we
6639 * will have tried the cluster allocator
6640 * plenty of times and not have found
6641 * anything, so we are likely way too
6642 * fragmented for the clustering stuff to find
6645 * However, if the cluster is taken from the
6646 * current block group, release the cluster
6647 * first, so that we stand a better chance of
6648 * succeeding in the unclustered
6650 if (loop >= LOOP_NO_EMPTY_SIZE &&
6651 used_block_group != block_group) {
6652 spin_unlock(&last_ptr->refill_lock);
6653 btrfs_release_block_group(used_block_group,
6655 goto unclustered_alloc;
6659 * this cluster didn't work out, free it and
6662 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6664 if (used_block_group != block_group)
6665 btrfs_release_block_group(used_block_group,
6668 if (loop >= LOOP_NO_EMPTY_SIZE) {
6669 spin_unlock(&last_ptr->refill_lock);
6670 goto unclustered_alloc;
6673 aligned_cluster = max_t(unsigned long,
6674 empty_cluster + empty_size,
6675 block_group->full_stripe_len);
6677 /* allocate a cluster in this block group */
6678 ret = btrfs_find_space_cluster(root, block_group,
6679 last_ptr, search_start,
6684 * now pull our allocation out of this
6687 offset = btrfs_alloc_from_cluster(block_group,
6693 /* we found one, proceed */
6694 spin_unlock(&last_ptr->refill_lock);
6695 trace_btrfs_reserve_extent_cluster(root,
6696 block_group, search_start,
6700 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6701 && !failed_cluster_refill) {
6702 spin_unlock(&last_ptr->refill_lock);
6704 failed_cluster_refill = true;
6705 wait_block_group_cache_progress(block_group,
6706 num_bytes + empty_cluster + empty_size);
6707 goto have_block_group;
6711 * at this point we either didn't find a cluster
6712 * or we weren't able to allocate a block from our
6713 * cluster. Free the cluster we've been trying
6714 * to use, and go to the next block group
6716 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6717 spin_unlock(&last_ptr->refill_lock);
6722 spin_lock(&block_group->free_space_ctl->tree_lock);
6724 block_group->free_space_ctl->free_space <
6725 num_bytes + empty_cluster + empty_size) {
6726 if (block_group->free_space_ctl->free_space >
6729 block_group->free_space_ctl->free_space;
6730 spin_unlock(&block_group->free_space_ctl->tree_lock);
6733 spin_unlock(&block_group->free_space_ctl->tree_lock);
6735 offset = btrfs_find_space_for_alloc(block_group, search_start,
6736 num_bytes, empty_size,
6739 * If we didn't find a chunk, and we haven't failed on this
6740 * block group before, and this block group is in the middle of
6741 * caching and we are ok with waiting, then go ahead and wait
6742 * for progress to be made, and set failed_alloc to true.
6744 * If failed_alloc is true then we've already waited on this
6745 * block group once and should move on to the next block group.
6747 if (!offset && !failed_alloc && !cached &&
6748 loop > LOOP_CACHING_NOWAIT) {
6749 wait_block_group_cache_progress(block_group,
6750 num_bytes + empty_size);
6751 failed_alloc = true;
6752 goto have_block_group;
6753 } else if (!offset) {
6755 have_caching_bg = true;
6759 search_start = stripe_align(root, block_group,
6762 /* move on to the next group */
6763 if (search_start + num_bytes >
6764 block_group->key.objectid + block_group->key.offset) {
6765 btrfs_add_free_space(block_group, offset, num_bytes);
6769 if (offset < search_start)
6770 btrfs_add_free_space(block_group, offset,
6771 search_start - offset);
6772 BUG_ON(offset > search_start);
6774 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
6775 alloc_type, delalloc);
6776 if (ret == -EAGAIN) {
6777 btrfs_add_free_space(block_group, offset, num_bytes);
6781 /* we are all good, lets return */
6782 ins->objectid = search_start;
6783 ins->offset = num_bytes;
6785 trace_btrfs_reserve_extent(orig_root, block_group,
6786 search_start, num_bytes);
6787 btrfs_release_block_group(block_group, delalloc);
6790 failed_cluster_refill = false;
6791 failed_alloc = false;
6792 BUG_ON(index != get_block_group_index(block_group));
6793 btrfs_release_block_group(block_group, delalloc);
6795 up_read(&space_info->groups_sem);
6797 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6800 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6804 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6805 * caching kthreads as we move along
6806 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6807 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6808 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6811 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
6814 if (loop == LOOP_ALLOC_CHUNK) {
6815 struct btrfs_trans_handle *trans;
6818 trans = current->journal_info;
6822 trans = btrfs_join_transaction(root);
6824 if (IS_ERR(trans)) {
6825 ret = PTR_ERR(trans);
6829 ret = do_chunk_alloc(trans, root, flags,
6832 * Do not bail out on ENOSPC since we
6833 * can do more things.
6835 if (ret < 0 && ret != -ENOSPC)
6836 btrfs_abort_transaction(trans,
6841 btrfs_end_transaction(trans, root);
6846 if (loop == LOOP_NO_EMPTY_SIZE) {
6852 } else if (!ins->objectid) {
6854 } else if (ins->objectid) {
6859 ins->offset = max_extent_size;
6863 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6864 int dump_block_groups)
6866 struct btrfs_block_group_cache *cache;
6869 spin_lock(&info->lock);
6870 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
6872 info->total_bytes - info->bytes_used - info->bytes_pinned -
6873 info->bytes_reserved - info->bytes_readonly,
6874 (info->full) ? "" : "not ");
6875 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
6876 "reserved=%llu, may_use=%llu, readonly=%llu\n",
6877 info->total_bytes, info->bytes_used, info->bytes_pinned,
6878 info->bytes_reserved, info->bytes_may_use,
6879 info->bytes_readonly);
6880 spin_unlock(&info->lock);
6882 if (!dump_block_groups)
6885 down_read(&info->groups_sem);
6887 list_for_each_entry(cache, &info->block_groups[index], list) {
6888 spin_lock(&cache->lock);
6889 printk(KERN_INFO "BTRFS: "
6890 "block group %llu has %llu bytes, "
6891 "%llu used %llu pinned %llu reserved %s\n",
6892 cache->key.objectid, cache->key.offset,
6893 btrfs_block_group_used(&cache->item), cache->pinned,
6894 cache->reserved, cache->ro ? "[readonly]" : "");
6895 btrfs_dump_free_space(cache, bytes);
6896 spin_unlock(&cache->lock);
6898 if (++index < BTRFS_NR_RAID_TYPES)
6900 up_read(&info->groups_sem);
6903 int btrfs_reserve_extent(struct btrfs_root *root,
6904 u64 num_bytes, u64 min_alloc_size,
6905 u64 empty_size, u64 hint_byte,
6906 struct btrfs_key *ins, int is_data, int delalloc)
6908 bool final_tried = false;
6912 flags = btrfs_get_alloc_profile(root, is_data);
6914 WARN_ON(num_bytes < root->sectorsize);
6915 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6918 if (ret == -ENOSPC) {
6919 if (!final_tried && ins->offset) {
6920 num_bytes = min(num_bytes >> 1, ins->offset);
6921 num_bytes = round_down(num_bytes, root->sectorsize);
6922 num_bytes = max(num_bytes, min_alloc_size);
6923 if (num_bytes == min_alloc_size)
6926 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6927 struct btrfs_space_info *sinfo;
6929 sinfo = __find_space_info(root->fs_info, flags);
6930 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6933 dump_space_info(sinfo, num_bytes, 1);
6940 static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6942 int pin, int delalloc)
6944 struct btrfs_block_group_cache *cache;
6947 cache = btrfs_lookup_block_group(root->fs_info, start);
6949 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6954 if (btrfs_test_opt(root, DISCARD))
6955 ret = btrfs_discard_extent(root, start, len, NULL);
6958 pin_down_extent(root, cache, start, len, 1);
6960 btrfs_add_free_space(cache, start, len);
6961 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
6963 btrfs_put_block_group(cache);
6965 trace_btrfs_reserved_extent_free(root, start, len);
6970 int btrfs_free_reserved_extent(struct btrfs_root *root,
6971 u64 start, u64 len, int delalloc)
6973 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
6976 int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6979 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
6982 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6983 struct btrfs_root *root,
6984 u64 parent, u64 root_objectid,
6985 u64 flags, u64 owner, u64 offset,
6986 struct btrfs_key *ins, int ref_mod)
6989 struct btrfs_fs_info *fs_info = root->fs_info;
6990 struct btrfs_extent_item *extent_item;
6991 struct btrfs_extent_inline_ref *iref;
6992 struct btrfs_path *path;
6993 struct extent_buffer *leaf;
6998 type = BTRFS_SHARED_DATA_REF_KEY;
7000 type = BTRFS_EXTENT_DATA_REF_KEY;
7002 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
7004 path = btrfs_alloc_path();
7008 path->leave_spinning = 1;
7009 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7012 btrfs_free_path(path);
7016 leaf = path->nodes[0];
7017 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7018 struct btrfs_extent_item);
7019 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
7020 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7021 btrfs_set_extent_flags(leaf, extent_item,
7022 flags | BTRFS_EXTENT_FLAG_DATA);
7024 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7025 btrfs_set_extent_inline_ref_type(leaf, iref, type);
7027 struct btrfs_shared_data_ref *ref;
7028 ref = (struct btrfs_shared_data_ref *)(iref + 1);
7029 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7030 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
7032 struct btrfs_extent_data_ref *ref;
7033 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
7034 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
7035 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
7036 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
7037 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
7040 btrfs_mark_buffer_dirty(path->nodes[0]);
7041 btrfs_free_path(path);
7043 /* Always set parent to 0 here since its exclusive anyway. */
7044 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7045 ins->objectid, ins->offset,
7046 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7050 ret = update_block_group(root, ins->objectid, ins->offset, 1);
7051 if (ret) { /* -ENOENT, logic error */
7052 btrfs_err(fs_info, "update block group failed for %llu %llu",
7053 ins->objectid, ins->offset);
7056 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
7060 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
7061 struct btrfs_root *root,
7062 u64 parent, u64 root_objectid,
7063 u64 flags, struct btrfs_disk_key *key,
7064 int level, struct btrfs_key *ins,
7068 struct btrfs_fs_info *fs_info = root->fs_info;
7069 struct btrfs_extent_item *extent_item;
7070 struct btrfs_tree_block_info *block_info;
7071 struct btrfs_extent_inline_ref *iref;
7072 struct btrfs_path *path;
7073 struct extent_buffer *leaf;
7074 u32 size = sizeof(*extent_item) + sizeof(*iref);
7075 u64 num_bytes = ins->offset;
7076 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7079 if (!skinny_metadata)
7080 size += sizeof(*block_info);
7082 path = btrfs_alloc_path();
7084 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7089 path->leave_spinning = 1;
7090 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7093 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7095 btrfs_free_path(path);
7099 leaf = path->nodes[0];
7100 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7101 struct btrfs_extent_item);
7102 btrfs_set_extent_refs(leaf, extent_item, 1);
7103 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7104 btrfs_set_extent_flags(leaf, extent_item,
7105 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
7107 if (skinny_metadata) {
7108 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7109 num_bytes = root->leafsize;
7111 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
7112 btrfs_set_tree_block_key(leaf, block_info, key);
7113 btrfs_set_tree_block_level(leaf, block_info, level);
7114 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
7118 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
7119 btrfs_set_extent_inline_ref_type(leaf, iref,
7120 BTRFS_SHARED_BLOCK_REF_KEY);
7121 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7123 btrfs_set_extent_inline_ref_type(leaf, iref,
7124 BTRFS_TREE_BLOCK_REF_KEY);
7125 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
7128 btrfs_mark_buffer_dirty(leaf);
7129 btrfs_free_path(path);
7132 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7133 ins->objectid, num_bytes,
7134 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7139 ret = update_block_group(root, ins->objectid, root->leafsize, 1);
7140 if (ret) { /* -ENOENT, logic error */
7141 btrfs_err(fs_info, "update block group failed for %llu %llu",
7142 ins->objectid, ins->offset);
7146 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->leafsize);
7150 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7151 struct btrfs_root *root,
7152 u64 root_objectid, u64 owner,
7153 u64 offset, struct btrfs_key *ins)
7157 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
7159 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7161 root_objectid, owner, offset,
7162 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
7167 * this is used by the tree logging recovery code. It records that
7168 * an extent has been allocated and makes sure to clear the free
7169 * space cache bits as well
7171 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7172 struct btrfs_root *root,
7173 u64 root_objectid, u64 owner, u64 offset,
7174 struct btrfs_key *ins)
7177 struct btrfs_block_group_cache *block_group;
7180 * Mixed block groups will exclude before processing the log so we only
7181 * need to do the exlude dance if this fs isn't mixed.
7183 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7184 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7189 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
7193 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
7194 RESERVE_ALLOC_NO_ACCOUNT, 0);
7195 BUG_ON(ret); /* logic error */
7196 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7197 0, owner, offset, ins, 1);
7198 btrfs_put_block_group(block_group);
7202 static struct extent_buffer *
7203 btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7204 u64 bytenr, u32 blocksize, int level)
7206 struct extent_buffer *buf;
7208 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
7210 return ERR_PTR(-ENOMEM);
7211 btrfs_set_header_generation(buf, trans->transid);
7212 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
7213 btrfs_tree_lock(buf);
7214 clean_tree_block(trans, root, buf);
7215 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
7217 btrfs_set_lock_blocking(buf);
7218 btrfs_set_buffer_uptodate(buf);
7220 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
7222 * we allow two log transactions at a time, use different
7223 * EXENT bit to differentiate dirty pages.
7225 if (root->log_transid % 2 == 0)
7226 set_extent_dirty(&root->dirty_log_pages, buf->start,
7227 buf->start + buf->len - 1, GFP_NOFS);
7229 set_extent_new(&root->dirty_log_pages, buf->start,
7230 buf->start + buf->len - 1, GFP_NOFS);
7232 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7233 buf->start + buf->len - 1, GFP_NOFS);
7235 trans->blocks_used++;
7236 /* this returns a buffer locked for blocking */
7240 static struct btrfs_block_rsv *
7241 use_block_rsv(struct btrfs_trans_handle *trans,
7242 struct btrfs_root *root, u32 blocksize)
7244 struct btrfs_block_rsv *block_rsv;
7245 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
7247 bool global_updated = false;
7249 block_rsv = get_block_rsv(trans, root);
7251 if (unlikely(block_rsv->size == 0))
7254 ret = block_rsv_use_bytes(block_rsv, blocksize);
7258 if (block_rsv->failfast)
7259 return ERR_PTR(ret);
7261 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7262 global_updated = true;
7263 update_global_block_rsv(root->fs_info);
7267 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7268 static DEFINE_RATELIMIT_STATE(_rs,
7269 DEFAULT_RATELIMIT_INTERVAL * 10,
7270 /*DEFAULT_RATELIMIT_BURST*/ 1);
7271 if (__ratelimit(&_rs))
7273 "BTRFS: block rsv returned %d\n", ret);
7276 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7277 BTRFS_RESERVE_NO_FLUSH);
7281 * If we couldn't reserve metadata bytes try and use some from
7282 * the global reserve if its space type is the same as the global
7285 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7286 block_rsv->space_info == global_rsv->space_info) {
7287 ret = block_rsv_use_bytes(global_rsv, blocksize);
7291 return ERR_PTR(ret);
7294 static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7295 struct btrfs_block_rsv *block_rsv, u32 blocksize)
7297 block_rsv_add_bytes(block_rsv, blocksize, 0);
7298 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
7302 * finds a free extent and does all the dirty work required for allocation
7303 * returns the key for the extent through ins, and a tree buffer for
7304 * the first block of the extent through buf.
7306 * returns the tree buffer or NULL.
7308 struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
7309 struct btrfs_root *root, u32 blocksize,
7310 u64 parent, u64 root_objectid,
7311 struct btrfs_disk_key *key, int level,
7312 u64 hint, u64 empty_size)
7314 struct btrfs_key ins;
7315 struct btrfs_block_rsv *block_rsv;
7316 struct extent_buffer *buf;
7319 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7322 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
7323 if (unlikely(test_bit(BTRFS_ROOT_DUMMY_ROOT, &root->state))) {
7324 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
7327 root->alloc_bytenr += blocksize;
7331 block_rsv = use_block_rsv(trans, root, blocksize);
7332 if (IS_ERR(block_rsv))
7333 return ERR_CAST(block_rsv);
7335 ret = btrfs_reserve_extent(root, blocksize, blocksize,
7336 empty_size, hint, &ins, 0, 0);
7338 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
7339 return ERR_PTR(ret);
7342 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7344 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
7346 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7348 parent = ins.objectid;
7349 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7353 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7354 struct btrfs_delayed_extent_op *extent_op;
7355 extent_op = btrfs_alloc_delayed_extent_op();
7356 BUG_ON(!extent_op); /* -ENOMEM */
7358 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7360 memset(&extent_op->key, 0, sizeof(extent_op->key));
7361 extent_op->flags_to_set = flags;
7362 if (skinny_metadata)
7363 extent_op->update_key = 0;
7365 extent_op->update_key = 1;
7366 extent_op->update_flags = 1;
7367 extent_op->is_data = 0;
7368 extent_op->level = level;
7370 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7372 ins.offset, parent, root_objectid,
7373 level, BTRFS_ADD_DELAYED_EXTENT,
7375 BUG_ON(ret); /* -ENOMEM */
7380 struct walk_control {
7381 u64 refs[BTRFS_MAX_LEVEL];
7382 u64 flags[BTRFS_MAX_LEVEL];
7383 struct btrfs_key update_progress;
7394 #define DROP_REFERENCE 1
7395 #define UPDATE_BACKREF 2
7397 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7398 struct btrfs_root *root,
7399 struct walk_control *wc,
7400 struct btrfs_path *path)
7408 struct btrfs_key key;
7409 struct extent_buffer *eb;
7414 if (path->slots[wc->level] < wc->reada_slot) {
7415 wc->reada_count = wc->reada_count * 2 / 3;
7416 wc->reada_count = max(wc->reada_count, 2);
7418 wc->reada_count = wc->reada_count * 3 / 2;
7419 wc->reada_count = min_t(int, wc->reada_count,
7420 BTRFS_NODEPTRS_PER_BLOCK(root));
7423 eb = path->nodes[wc->level];
7424 nritems = btrfs_header_nritems(eb);
7425 blocksize = btrfs_level_size(root, wc->level - 1);
7427 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7428 if (nread >= wc->reada_count)
7432 bytenr = btrfs_node_blockptr(eb, slot);
7433 generation = btrfs_node_ptr_generation(eb, slot);
7435 if (slot == path->slots[wc->level])
7438 if (wc->stage == UPDATE_BACKREF &&
7439 generation <= root->root_key.offset)
7442 /* We don't lock the tree block, it's OK to be racy here */
7443 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7444 wc->level - 1, 1, &refs,
7446 /* We don't care about errors in readahead. */
7451 if (wc->stage == DROP_REFERENCE) {
7455 if (wc->level == 1 &&
7456 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7458 if (!wc->update_ref ||
7459 generation <= root->root_key.offset)
7461 btrfs_node_key_to_cpu(eb, &key, slot);
7462 ret = btrfs_comp_cpu_keys(&key,
7463 &wc->update_progress);
7467 if (wc->level == 1 &&
7468 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7472 ret = readahead_tree_block(root, bytenr, blocksize,
7478 wc->reada_slot = slot;
7481 static int account_leaf_items(struct btrfs_trans_handle *trans,
7482 struct btrfs_root *root,
7483 struct extent_buffer *eb)
7485 int nr = btrfs_header_nritems(eb);
7486 int i, extent_type, ret;
7487 struct btrfs_key key;
7488 struct btrfs_file_extent_item *fi;
7489 u64 bytenr, num_bytes;
7491 for (i = 0; i < nr; i++) {
7492 btrfs_item_key_to_cpu(eb, &key, i);
7494 if (key.type != BTRFS_EXTENT_DATA_KEY)
7497 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
7498 /* filter out non qgroup-accountable extents */
7499 extent_type = btrfs_file_extent_type(eb, fi);
7501 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
7504 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
7508 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
7510 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7513 BTRFS_QGROUP_OPER_SUB_SUBTREE, 0);
7521 * Walk up the tree from the bottom, freeing leaves and any interior
7522 * nodes which have had all slots visited. If a node (leaf or
7523 * interior) is freed, the node above it will have it's slot
7524 * incremented. The root node will never be freed.
7526 * At the end of this function, we should have a path which has all
7527 * slots incremented to the next position for a search. If we need to
7528 * read a new node it will be NULL and the node above it will have the
7529 * correct slot selected for a later read.
7531 * If we increment the root nodes slot counter past the number of
7532 * elements, 1 is returned to signal completion of the search.
7534 static int adjust_slots_upwards(struct btrfs_root *root,
7535 struct btrfs_path *path, int root_level)
7539 struct extent_buffer *eb;
7541 if (root_level == 0)
7544 while (level <= root_level) {
7545 eb = path->nodes[level];
7546 nr = btrfs_header_nritems(eb);
7547 path->slots[level]++;
7548 slot = path->slots[level];
7549 if (slot >= nr || level == 0) {
7551 * Don't free the root - we will detect this
7552 * condition after our loop and return a
7553 * positive value for caller to stop walking the tree.
7555 if (level != root_level) {
7556 btrfs_tree_unlock_rw(eb, path->locks[level]);
7557 path->locks[level] = 0;
7559 free_extent_buffer(eb);
7560 path->nodes[level] = NULL;
7561 path->slots[level] = 0;
7565 * We have a valid slot to walk back down
7566 * from. Stop here so caller can process these
7575 eb = path->nodes[root_level];
7576 if (path->slots[root_level] >= btrfs_header_nritems(eb))
7583 * root_eb is the subtree root and is locked before this function is called.
7585 static int account_shared_subtree(struct btrfs_trans_handle *trans,
7586 struct btrfs_root *root,
7587 struct extent_buffer *root_eb,
7593 struct extent_buffer *eb = root_eb;
7594 struct btrfs_path *path = NULL;
7596 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
7597 BUG_ON(root_eb == NULL);
7599 if (!root->fs_info->quota_enabled)
7602 if (!extent_buffer_uptodate(root_eb)) {
7603 ret = btrfs_read_buffer(root_eb, root_gen);
7608 if (root_level == 0) {
7609 ret = account_leaf_items(trans, root, root_eb);
7613 path = btrfs_alloc_path();
7618 * Walk down the tree. Missing extent blocks are filled in as
7619 * we go. Metadata is accounted every time we read a new
7622 * When we reach a leaf, we account for file extent items in it,
7623 * walk back up the tree (adjusting slot pointers as we go)
7624 * and restart the search process.
7626 extent_buffer_get(root_eb); /* For path */
7627 path->nodes[root_level] = root_eb;
7628 path->slots[root_level] = 0;
7629 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
7632 while (level >= 0) {
7633 if (path->nodes[level] == NULL) {
7634 int child_bsize = root->nodesize;
7639 /* We need to get child blockptr/gen from
7640 * parent before we can read it. */
7641 eb = path->nodes[level + 1];
7642 parent_slot = path->slots[level + 1];
7643 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
7644 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
7646 eb = read_tree_block(root, child_bytenr, child_bsize,
7648 if (!eb || !extent_buffer_uptodate(eb)) {
7653 path->nodes[level] = eb;
7654 path->slots[level] = 0;
7656 btrfs_tree_read_lock(eb);
7657 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
7658 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
7660 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7664 BTRFS_QGROUP_OPER_SUB_SUBTREE,
7672 ret = account_leaf_items(trans, root, path->nodes[level]);
7676 /* Nonzero return here means we completed our search */
7677 ret = adjust_slots_upwards(root, path, root_level);
7681 /* Restart search with new slots */
7690 btrfs_free_path(path);
7696 * helper to process tree block while walking down the tree.
7698 * when wc->stage == UPDATE_BACKREF, this function updates
7699 * back refs for pointers in the block.
7701 * NOTE: return value 1 means we should stop walking down.
7703 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7704 struct btrfs_root *root,
7705 struct btrfs_path *path,
7706 struct walk_control *wc, int lookup_info)
7708 int level = wc->level;
7709 struct extent_buffer *eb = path->nodes[level];
7710 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7713 if (wc->stage == UPDATE_BACKREF &&
7714 btrfs_header_owner(eb) != root->root_key.objectid)
7718 * when reference count of tree block is 1, it won't increase
7719 * again. once full backref flag is set, we never clear it.
7722 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7723 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
7724 BUG_ON(!path->locks[level]);
7725 ret = btrfs_lookup_extent_info(trans, root,
7726 eb->start, level, 1,
7729 BUG_ON(ret == -ENOMEM);
7732 BUG_ON(wc->refs[level] == 0);
7735 if (wc->stage == DROP_REFERENCE) {
7736 if (wc->refs[level] > 1)
7739 if (path->locks[level] && !wc->keep_locks) {
7740 btrfs_tree_unlock_rw(eb, path->locks[level]);
7741 path->locks[level] = 0;
7746 /* wc->stage == UPDATE_BACKREF */
7747 if (!(wc->flags[level] & flag)) {
7748 BUG_ON(!path->locks[level]);
7749 ret = btrfs_inc_ref(trans, root, eb, 1);
7750 BUG_ON(ret); /* -ENOMEM */
7751 ret = btrfs_dec_ref(trans, root, eb, 0);
7752 BUG_ON(ret); /* -ENOMEM */
7753 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
7755 btrfs_header_level(eb), 0);
7756 BUG_ON(ret); /* -ENOMEM */
7757 wc->flags[level] |= flag;
7761 * the block is shared by multiple trees, so it's not good to
7762 * keep the tree lock
7764 if (path->locks[level] && level > 0) {
7765 btrfs_tree_unlock_rw(eb, path->locks[level]);
7766 path->locks[level] = 0;
7772 * helper to process tree block pointer.
7774 * when wc->stage == DROP_REFERENCE, this function checks
7775 * reference count of the block pointed to. if the block
7776 * is shared and we need update back refs for the subtree
7777 * rooted at the block, this function changes wc->stage to
7778 * UPDATE_BACKREF. if the block is shared and there is no
7779 * need to update back, this function drops the reference
7782 * NOTE: return value 1 means we should stop walking down.
7784 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7785 struct btrfs_root *root,
7786 struct btrfs_path *path,
7787 struct walk_control *wc, int *lookup_info)
7793 struct btrfs_key key;
7794 struct extent_buffer *next;
7795 int level = wc->level;
7798 bool need_account = false;
7800 generation = btrfs_node_ptr_generation(path->nodes[level],
7801 path->slots[level]);
7803 * if the lower level block was created before the snapshot
7804 * was created, we know there is no need to update back refs
7807 if (wc->stage == UPDATE_BACKREF &&
7808 generation <= root->root_key.offset) {
7813 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7814 blocksize = btrfs_level_size(root, level - 1);
7816 next = btrfs_find_tree_block(root, bytenr, blocksize);
7818 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
7821 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7825 btrfs_tree_lock(next);
7826 btrfs_set_lock_blocking(next);
7828 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
7829 &wc->refs[level - 1],
7830 &wc->flags[level - 1]);
7832 btrfs_tree_unlock(next);
7836 if (unlikely(wc->refs[level - 1] == 0)) {
7837 btrfs_err(root->fs_info, "Missing references.");
7842 if (wc->stage == DROP_REFERENCE) {
7843 if (wc->refs[level - 1] > 1) {
7844 need_account = true;
7846 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7849 if (!wc->update_ref ||
7850 generation <= root->root_key.offset)
7853 btrfs_node_key_to_cpu(path->nodes[level], &key,
7854 path->slots[level]);
7855 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7859 wc->stage = UPDATE_BACKREF;
7860 wc->shared_level = level - 1;
7864 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7868 if (!btrfs_buffer_uptodate(next, generation, 0)) {
7869 btrfs_tree_unlock(next);
7870 free_extent_buffer(next);
7876 if (reada && level == 1)
7877 reada_walk_down(trans, root, wc, path);
7878 next = read_tree_block(root, bytenr, blocksize, generation);
7879 if (!next || !extent_buffer_uptodate(next)) {
7880 free_extent_buffer(next);
7883 btrfs_tree_lock(next);
7884 btrfs_set_lock_blocking(next);
7888 BUG_ON(level != btrfs_header_level(next));
7889 path->nodes[level] = next;
7890 path->slots[level] = 0;
7891 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
7897 wc->refs[level - 1] = 0;
7898 wc->flags[level - 1] = 0;
7899 if (wc->stage == DROP_REFERENCE) {
7900 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7901 parent = path->nodes[level]->start;
7903 BUG_ON(root->root_key.objectid !=
7904 btrfs_header_owner(path->nodes[level]));
7909 ret = account_shared_subtree(trans, root, next,
7910 generation, level - 1);
7912 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
7913 "%d accounting shared subtree. Quota "
7914 "is out of sync, rescan required.\n",
7915 root->fs_info->sb->s_id, ret);
7918 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
7919 root->root_key.objectid, level - 1, 0, 0);
7920 BUG_ON(ret); /* -ENOMEM */
7922 btrfs_tree_unlock(next);
7923 free_extent_buffer(next);
7929 * helper to process tree block while walking up the tree.
7931 * when wc->stage == DROP_REFERENCE, this function drops
7932 * reference count on the block.
7934 * when wc->stage == UPDATE_BACKREF, this function changes
7935 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7936 * to UPDATE_BACKREF previously while processing the block.
7938 * NOTE: return value 1 means we should stop walking up.
7940 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7941 struct btrfs_root *root,
7942 struct btrfs_path *path,
7943 struct walk_control *wc)
7946 int level = wc->level;
7947 struct extent_buffer *eb = path->nodes[level];
7950 if (wc->stage == UPDATE_BACKREF) {
7951 BUG_ON(wc->shared_level < level);
7952 if (level < wc->shared_level)
7955 ret = find_next_key(path, level + 1, &wc->update_progress);
7959 wc->stage = DROP_REFERENCE;
7960 wc->shared_level = -1;
7961 path->slots[level] = 0;
7964 * check reference count again if the block isn't locked.
7965 * we should start walking down the tree again if reference
7968 if (!path->locks[level]) {
7970 btrfs_tree_lock(eb);
7971 btrfs_set_lock_blocking(eb);
7972 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
7974 ret = btrfs_lookup_extent_info(trans, root,
7975 eb->start, level, 1,
7979 btrfs_tree_unlock_rw(eb, path->locks[level]);
7980 path->locks[level] = 0;
7983 BUG_ON(wc->refs[level] == 0);
7984 if (wc->refs[level] == 1) {
7985 btrfs_tree_unlock_rw(eb, path->locks[level]);
7986 path->locks[level] = 0;
7992 /* wc->stage == DROP_REFERENCE */
7993 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
7995 if (wc->refs[level] == 1) {
7997 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
7998 ret = btrfs_dec_ref(trans, root, eb, 1);
8000 ret = btrfs_dec_ref(trans, root, eb, 0);
8001 BUG_ON(ret); /* -ENOMEM */
8002 ret = account_leaf_items(trans, root, eb);
8004 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
8005 "%d accounting leaf items. Quota "
8006 "is out of sync, rescan required.\n",
8007 root->fs_info->sb->s_id, ret);
8010 /* make block locked assertion in clean_tree_block happy */
8011 if (!path->locks[level] &&
8012 btrfs_header_generation(eb) == trans->transid) {
8013 btrfs_tree_lock(eb);
8014 btrfs_set_lock_blocking(eb);
8015 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8017 clean_tree_block(trans, root, eb);
8020 if (eb == root->node) {
8021 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8024 BUG_ON(root->root_key.objectid !=
8025 btrfs_header_owner(eb));
8027 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8028 parent = path->nodes[level + 1]->start;
8030 BUG_ON(root->root_key.objectid !=
8031 btrfs_header_owner(path->nodes[level + 1]));
8034 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
8036 wc->refs[level] = 0;
8037 wc->flags[level] = 0;
8041 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8042 struct btrfs_root *root,
8043 struct btrfs_path *path,
8044 struct walk_control *wc)
8046 int level = wc->level;
8047 int lookup_info = 1;
8050 while (level >= 0) {
8051 ret = walk_down_proc(trans, root, path, wc, lookup_info);
8058 if (path->slots[level] >=
8059 btrfs_header_nritems(path->nodes[level]))
8062 ret = do_walk_down(trans, root, path, wc, &lookup_info);
8064 path->slots[level]++;
8073 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
8074 struct btrfs_root *root,
8075 struct btrfs_path *path,
8076 struct walk_control *wc, int max_level)
8078 int level = wc->level;
8081 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8082 while (level < max_level && path->nodes[level]) {
8084 if (path->slots[level] + 1 <
8085 btrfs_header_nritems(path->nodes[level])) {
8086 path->slots[level]++;
8089 ret = walk_up_proc(trans, root, path, wc);
8093 if (path->locks[level]) {
8094 btrfs_tree_unlock_rw(path->nodes[level],
8095 path->locks[level]);
8096 path->locks[level] = 0;
8098 free_extent_buffer(path->nodes[level]);
8099 path->nodes[level] = NULL;
8107 * drop a subvolume tree.
8109 * this function traverses the tree freeing any blocks that only
8110 * referenced by the tree.
8112 * when a shared tree block is found. this function decreases its
8113 * reference count by one. if update_ref is true, this function
8114 * also make sure backrefs for the shared block and all lower level
8115 * blocks are properly updated.
8117 * If called with for_reloc == 0, may exit early with -EAGAIN
8119 int btrfs_drop_snapshot(struct btrfs_root *root,
8120 struct btrfs_block_rsv *block_rsv, int update_ref,
8123 struct btrfs_path *path;
8124 struct btrfs_trans_handle *trans;
8125 struct btrfs_root *tree_root = root->fs_info->tree_root;
8126 struct btrfs_root_item *root_item = &root->root_item;
8127 struct walk_control *wc;
8128 struct btrfs_key key;
8132 bool root_dropped = false;
8134 btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
8136 path = btrfs_alloc_path();
8142 wc = kzalloc(sizeof(*wc), GFP_NOFS);
8144 btrfs_free_path(path);
8149 trans = btrfs_start_transaction(tree_root, 0);
8150 if (IS_ERR(trans)) {
8151 err = PTR_ERR(trans);
8156 trans->block_rsv = block_rsv;
8158 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
8159 level = btrfs_header_level(root->node);
8160 path->nodes[level] = btrfs_lock_root_node(root);
8161 btrfs_set_lock_blocking(path->nodes[level]);
8162 path->slots[level] = 0;
8163 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8164 memset(&wc->update_progress, 0,
8165 sizeof(wc->update_progress));
8167 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
8168 memcpy(&wc->update_progress, &key,
8169 sizeof(wc->update_progress));
8171 level = root_item->drop_level;
8173 path->lowest_level = level;
8174 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8175 path->lowest_level = 0;
8183 * unlock our path, this is safe because only this
8184 * function is allowed to delete this snapshot
8186 btrfs_unlock_up_safe(path, 0);
8188 level = btrfs_header_level(root->node);
8190 btrfs_tree_lock(path->nodes[level]);
8191 btrfs_set_lock_blocking(path->nodes[level]);
8192 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8194 ret = btrfs_lookup_extent_info(trans, root,
8195 path->nodes[level]->start,
8196 level, 1, &wc->refs[level],
8202 BUG_ON(wc->refs[level] == 0);
8204 if (level == root_item->drop_level)
8207 btrfs_tree_unlock(path->nodes[level]);
8208 path->locks[level] = 0;
8209 WARN_ON(wc->refs[level] != 1);
8215 wc->shared_level = -1;
8216 wc->stage = DROP_REFERENCE;
8217 wc->update_ref = update_ref;
8219 wc->for_reloc = for_reloc;
8220 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
8224 ret = walk_down_tree(trans, root, path, wc);
8230 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
8237 BUG_ON(wc->stage != DROP_REFERENCE);
8241 if (wc->stage == DROP_REFERENCE) {
8243 btrfs_node_key(path->nodes[level],
8244 &root_item->drop_progress,
8245 path->slots[level]);
8246 root_item->drop_level = level;
8249 BUG_ON(wc->level == 0);
8250 if (btrfs_should_end_transaction(trans, tree_root) ||
8251 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
8252 ret = btrfs_update_root(trans, tree_root,
8256 btrfs_abort_transaction(trans, tree_root, ret);
8262 * Qgroup update accounting is run from
8263 * delayed ref handling. This usually works
8264 * out because delayed refs are normally the
8265 * only way qgroup updates are added. However,
8266 * we may have added updates during our tree
8267 * walk so run qgroups here to make sure we
8268 * don't lose any updates.
8270 ret = btrfs_delayed_qgroup_accounting(trans,
8273 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8274 "running qgroup updates "
8275 "during snapshot delete. "
8276 "Quota is out of sync, "
8277 "rescan required.\n", ret);
8279 btrfs_end_transaction_throttle(trans, tree_root);
8280 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
8281 pr_debug("BTRFS: drop snapshot early exit\n");
8286 trans = btrfs_start_transaction(tree_root, 0);
8287 if (IS_ERR(trans)) {
8288 err = PTR_ERR(trans);
8292 trans->block_rsv = block_rsv;
8295 btrfs_release_path(path);
8299 ret = btrfs_del_root(trans, tree_root, &root->root_key);
8301 btrfs_abort_transaction(trans, tree_root, ret);
8305 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
8306 ret = btrfs_find_root(tree_root, &root->root_key, path,
8309 btrfs_abort_transaction(trans, tree_root, ret);
8312 } else if (ret > 0) {
8313 /* if we fail to delete the orphan item this time
8314 * around, it'll get picked up the next time.
8316 * The most common failure here is just -ENOENT.
8318 btrfs_del_orphan_item(trans, tree_root,
8319 root->root_key.objectid);
8323 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
8324 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
8326 free_extent_buffer(root->node);
8327 free_extent_buffer(root->commit_root);
8328 btrfs_put_fs_root(root);
8330 root_dropped = true;
8332 ret = btrfs_delayed_qgroup_accounting(trans, tree_root->fs_info);
8334 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8335 "running qgroup updates "
8336 "during snapshot delete. "
8337 "Quota is out of sync, "
8338 "rescan required.\n", ret);
8340 btrfs_end_transaction_throttle(trans, tree_root);
8343 btrfs_free_path(path);
8346 * So if we need to stop dropping the snapshot for whatever reason we
8347 * need to make sure to add it back to the dead root list so that we
8348 * keep trying to do the work later. This also cleans up roots if we
8349 * don't have it in the radix (like when we recover after a power fail
8350 * or unmount) so we don't leak memory.
8352 if (!for_reloc && root_dropped == false)
8353 btrfs_add_dead_root(root);
8354 if (err && err != -EAGAIN)
8355 btrfs_std_error(root->fs_info, err);
8360 * drop subtree rooted at tree block 'node'.
8362 * NOTE: this function will unlock and release tree block 'node'
8363 * only used by relocation code
8365 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
8366 struct btrfs_root *root,
8367 struct extent_buffer *node,
8368 struct extent_buffer *parent)
8370 struct btrfs_path *path;
8371 struct walk_control *wc;
8377 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
8379 path = btrfs_alloc_path();
8383 wc = kzalloc(sizeof(*wc), GFP_NOFS);
8385 btrfs_free_path(path);
8389 btrfs_assert_tree_locked(parent);
8390 parent_level = btrfs_header_level(parent);
8391 extent_buffer_get(parent);
8392 path->nodes[parent_level] = parent;
8393 path->slots[parent_level] = btrfs_header_nritems(parent);
8395 btrfs_assert_tree_locked(node);
8396 level = btrfs_header_level(node);
8397 path->nodes[level] = node;
8398 path->slots[level] = 0;
8399 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8401 wc->refs[parent_level] = 1;
8402 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8404 wc->shared_level = -1;
8405 wc->stage = DROP_REFERENCE;
8409 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
8412 wret = walk_down_tree(trans, root, path, wc);
8418 wret = walk_up_tree(trans, root, path, wc, parent_level);
8426 btrfs_free_path(path);
8430 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8436 * if restripe for this chunk_type is on pick target profile and
8437 * return, otherwise do the usual balance
8439 stripped = get_restripe_target(root->fs_info, flags);
8441 return extended_to_chunk(stripped);
8444 * we add in the count of missing devices because we want
8445 * to make sure that any RAID levels on a degraded FS
8446 * continue to be honored.
8448 num_devices = root->fs_info->fs_devices->rw_devices +
8449 root->fs_info->fs_devices->missing_devices;
8451 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8452 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
8453 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8455 if (num_devices == 1) {
8456 stripped |= BTRFS_BLOCK_GROUP_DUP;
8457 stripped = flags & ~stripped;
8459 /* turn raid0 into single device chunks */
8460 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8463 /* turn mirroring into duplication */
8464 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8465 BTRFS_BLOCK_GROUP_RAID10))
8466 return stripped | BTRFS_BLOCK_GROUP_DUP;
8468 /* they already had raid on here, just return */
8469 if (flags & stripped)
8472 stripped |= BTRFS_BLOCK_GROUP_DUP;
8473 stripped = flags & ~stripped;
8475 /* switch duplicated blocks with raid1 */
8476 if (flags & BTRFS_BLOCK_GROUP_DUP)
8477 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8479 /* this is drive concat, leave it alone */
8485 static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
8487 struct btrfs_space_info *sinfo = cache->space_info;
8489 u64 min_allocable_bytes;
8494 * We need some metadata space and system metadata space for
8495 * allocating chunks in some corner cases until we force to set
8496 * it to be readonly.
8499 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
8501 min_allocable_bytes = 1 * 1024 * 1024;
8503 min_allocable_bytes = 0;
8505 spin_lock(&sinfo->lock);
8506 spin_lock(&cache->lock);
8513 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8514 cache->bytes_super - btrfs_block_group_used(&cache->item);
8516 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8517 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
8518 min_allocable_bytes <= sinfo->total_bytes) {
8519 sinfo->bytes_readonly += num_bytes;
8524 spin_unlock(&cache->lock);
8525 spin_unlock(&sinfo->lock);
8529 int btrfs_set_block_group_ro(struct btrfs_root *root,
8530 struct btrfs_block_group_cache *cache)
8533 struct btrfs_trans_handle *trans;
8539 trans = btrfs_join_transaction(root);
8541 return PTR_ERR(trans);
8543 alloc_flags = update_block_group_flags(root, cache->flags);
8544 if (alloc_flags != cache->flags) {
8545 ret = do_chunk_alloc(trans, root, alloc_flags,
8551 ret = set_block_group_ro(cache, 0);
8554 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8555 ret = do_chunk_alloc(trans, root, alloc_flags,
8559 ret = set_block_group_ro(cache, 0);
8561 btrfs_end_transaction(trans, root);
8565 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8566 struct btrfs_root *root, u64 type)
8568 u64 alloc_flags = get_alloc_profile(root, type);
8569 return do_chunk_alloc(trans, root, alloc_flags,
8574 * helper to account the unused space of all the readonly block group in the
8575 * list. takes mirrors into account.
8577 static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8579 struct btrfs_block_group_cache *block_group;
8583 list_for_each_entry(block_group, groups_list, list) {
8584 spin_lock(&block_group->lock);
8586 if (!block_group->ro) {
8587 spin_unlock(&block_group->lock);
8591 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8592 BTRFS_BLOCK_GROUP_RAID10 |
8593 BTRFS_BLOCK_GROUP_DUP))
8598 free_bytes += (block_group->key.offset -
8599 btrfs_block_group_used(&block_group->item)) *
8602 spin_unlock(&block_group->lock);
8609 * helper to account the unused space of all the readonly block group in the
8610 * space_info. takes mirrors into account.
8612 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8617 spin_lock(&sinfo->lock);
8619 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8620 if (!list_empty(&sinfo->block_groups[i]))
8621 free_bytes += __btrfs_get_ro_block_group_free_space(
8622 &sinfo->block_groups[i]);
8624 spin_unlock(&sinfo->lock);
8629 void btrfs_set_block_group_rw(struct btrfs_root *root,
8630 struct btrfs_block_group_cache *cache)
8632 struct btrfs_space_info *sinfo = cache->space_info;
8637 spin_lock(&sinfo->lock);
8638 spin_lock(&cache->lock);
8639 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8640 cache->bytes_super - btrfs_block_group_used(&cache->item);
8641 sinfo->bytes_readonly -= num_bytes;
8643 spin_unlock(&cache->lock);
8644 spin_unlock(&sinfo->lock);
8648 * checks to see if its even possible to relocate this block group.
8650 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8651 * ok to go ahead and try.
8653 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
8655 struct btrfs_block_group_cache *block_group;
8656 struct btrfs_space_info *space_info;
8657 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8658 struct btrfs_device *device;
8659 struct btrfs_trans_handle *trans;
8668 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
8670 /* odd, couldn't find the block group, leave it alone */
8674 min_free = btrfs_block_group_used(&block_group->item);
8676 /* no bytes used, we're good */
8680 space_info = block_group->space_info;
8681 spin_lock(&space_info->lock);
8683 full = space_info->full;
8686 * if this is the last block group we have in this space, we can't
8687 * relocate it unless we're able to allocate a new chunk below.
8689 * Otherwise, we need to make sure we have room in the space to handle
8690 * all of the extents from this block group. If we can, we're good
8692 if ((space_info->total_bytes != block_group->key.offset) &&
8693 (space_info->bytes_used + space_info->bytes_reserved +
8694 space_info->bytes_pinned + space_info->bytes_readonly +
8695 min_free < space_info->total_bytes)) {
8696 spin_unlock(&space_info->lock);
8699 spin_unlock(&space_info->lock);
8702 * ok we don't have enough space, but maybe we have free space on our
8703 * devices to allocate new chunks for relocation, so loop through our
8704 * alloc devices and guess if we have enough space. if this block
8705 * group is going to be restriped, run checks against the target
8706 * profile instead of the current one.
8718 target = get_restripe_target(root->fs_info, block_group->flags);
8720 index = __get_raid_index(extended_to_chunk(target));
8723 * this is just a balance, so if we were marked as full
8724 * we know there is no space for a new chunk
8729 index = get_block_group_index(block_group);
8732 if (index == BTRFS_RAID_RAID10) {
8736 } else if (index == BTRFS_RAID_RAID1) {
8738 } else if (index == BTRFS_RAID_DUP) {
8741 } else if (index == BTRFS_RAID_RAID0) {
8742 dev_min = fs_devices->rw_devices;
8743 do_div(min_free, dev_min);
8746 /* We need to do this so that we can look at pending chunks */
8747 trans = btrfs_join_transaction(root);
8748 if (IS_ERR(trans)) {
8749 ret = PTR_ERR(trans);
8753 mutex_lock(&root->fs_info->chunk_mutex);
8754 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8758 * check to make sure we can actually find a chunk with enough
8759 * space to fit our block group in.
8761 if (device->total_bytes > device->bytes_used + min_free &&
8762 !device->is_tgtdev_for_dev_replace) {
8763 ret = find_free_dev_extent(trans, device, min_free,
8768 if (dev_nr >= dev_min)
8774 mutex_unlock(&root->fs_info->chunk_mutex);
8775 btrfs_end_transaction(trans, root);
8777 btrfs_put_block_group(block_group);
8781 static int find_first_block_group(struct btrfs_root *root,
8782 struct btrfs_path *path, struct btrfs_key *key)
8785 struct btrfs_key found_key;
8786 struct extent_buffer *leaf;
8789 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8794 slot = path->slots[0];
8795 leaf = path->nodes[0];
8796 if (slot >= btrfs_header_nritems(leaf)) {
8797 ret = btrfs_next_leaf(root, path);
8804 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8806 if (found_key.objectid >= key->objectid &&
8807 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8817 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8819 struct btrfs_block_group_cache *block_group;
8823 struct inode *inode;
8825 block_group = btrfs_lookup_first_block_group(info, last);
8826 while (block_group) {
8827 spin_lock(&block_group->lock);
8828 if (block_group->iref)
8830 spin_unlock(&block_group->lock);
8831 block_group = next_block_group(info->tree_root,
8841 inode = block_group->inode;
8842 block_group->iref = 0;
8843 block_group->inode = NULL;
8844 spin_unlock(&block_group->lock);
8846 last = block_group->key.objectid + block_group->key.offset;
8847 btrfs_put_block_group(block_group);
8851 int btrfs_free_block_groups(struct btrfs_fs_info *info)
8853 struct btrfs_block_group_cache *block_group;
8854 struct btrfs_space_info *space_info;
8855 struct btrfs_caching_control *caching_ctl;
8858 down_write(&info->commit_root_sem);
8859 while (!list_empty(&info->caching_block_groups)) {
8860 caching_ctl = list_entry(info->caching_block_groups.next,
8861 struct btrfs_caching_control, list);
8862 list_del(&caching_ctl->list);
8863 put_caching_control(caching_ctl);
8865 up_write(&info->commit_root_sem);
8867 spin_lock(&info->block_group_cache_lock);
8868 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8869 block_group = rb_entry(n, struct btrfs_block_group_cache,
8871 rb_erase(&block_group->cache_node,
8872 &info->block_group_cache_tree);
8873 spin_unlock(&info->block_group_cache_lock);
8875 down_write(&block_group->space_info->groups_sem);
8876 list_del(&block_group->list);
8877 up_write(&block_group->space_info->groups_sem);
8879 if (block_group->cached == BTRFS_CACHE_STARTED)
8880 wait_block_group_cache_done(block_group);
8883 * We haven't cached this block group, which means we could
8884 * possibly have excluded extents on this block group.
8886 if (block_group->cached == BTRFS_CACHE_NO ||
8887 block_group->cached == BTRFS_CACHE_ERROR)
8888 free_excluded_extents(info->extent_root, block_group);
8890 btrfs_remove_free_space_cache(block_group);
8891 btrfs_put_block_group(block_group);
8893 spin_lock(&info->block_group_cache_lock);
8895 spin_unlock(&info->block_group_cache_lock);
8897 /* now that all the block groups are freed, go through and
8898 * free all the space_info structs. This is only called during
8899 * the final stages of unmount, and so we know nobody is
8900 * using them. We call synchronize_rcu() once before we start,
8901 * just to be on the safe side.
8905 release_global_block_rsv(info);
8907 while (!list_empty(&info->space_info)) {
8910 space_info = list_entry(info->space_info.next,
8911 struct btrfs_space_info,
8913 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8914 if (WARN_ON(space_info->bytes_pinned > 0 ||
8915 space_info->bytes_reserved > 0 ||
8916 space_info->bytes_may_use > 0)) {
8917 dump_space_info(space_info, 0, 0);
8920 list_del(&space_info->list);
8921 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
8922 struct kobject *kobj;
8923 kobj = space_info->block_group_kobjs[i];
8924 space_info->block_group_kobjs[i] = NULL;
8930 kobject_del(&space_info->kobj);
8931 kobject_put(&space_info->kobj);
8936 static void __link_block_group(struct btrfs_space_info *space_info,
8937 struct btrfs_block_group_cache *cache)
8939 int index = get_block_group_index(cache);
8942 down_write(&space_info->groups_sem);
8943 if (list_empty(&space_info->block_groups[index]))
8945 list_add_tail(&cache->list, &space_info->block_groups[index]);
8946 up_write(&space_info->groups_sem);
8949 struct raid_kobject *rkobj;
8952 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
8955 rkobj->raid_type = index;
8956 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
8957 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
8958 "%s", get_raid_name(index));
8960 kobject_put(&rkobj->kobj);
8963 space_info->block_group_kobjs[index] = &rkobj->kobj;
8968 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
8971 static struct btrfs_block_group_cache *
8972 btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8974 struct btrfs_block_group_cache *cache;
8976 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8980 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8982 if (!cache->free_space_ctl) {
8987 cache->key.objectid = start;
8988 cache->key.offset = size;
8989 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
8991 cache->sectorsize = root->sectorsize;
8992 cache->fs_info = root->fs_info;
8993 cache->full_stripe_len = btrfs_full_stripe_len(root,
8994 &root->fs_info->mapping_tree,
8996 atomic_set(&cache->count, 1);
8997 spin_lock_init(&cache->lock);
8998 init_rwsem(&cache->data_rwsem);
8999 INIT_LIST_HEAD(&cache->list);
9000 INIT_LIST_HEAD(&cache->cluster_list);
9001 INIT_LIST_HEAD(&cache->new_bg_list);
9002 btrfs_init_free_space_ctl(cache);
9007 int btrfs_read_block_groups(struct btrfs_root *root)
9009 struct btrfs_path *path;
9011 struct btrfs_block_group_cache *cache;
9012 struct btrfs_fs_info *info = root->fs_info;
9013 struct btrfs_space_info *space_info;
9014 struct btrfs_key key;
9015 struct btrfs_key found_key;
9016 struct extent_buffer *leaf;
9020 root = info->extent_root;
9023 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
9024 path = btrfs_alloc_path();
9029 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
9030 if (btrfs_test_opt(root, SPACE_CACHE) &&
9031 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
9033 if (btrfs_test_opt(root, CLEAR_CACHE))
9037 ret = find_first_block_group(root, path, &key);
9043 leaf = path->nodes[0];
9044 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9046 cache = btrfs_create_block_group_cache(root, found_key.objectid,
9055 * When we mount with old space cache, we need to
9056 * set BTRFS_DC_CLEAR and set dirty flag.
9058 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9059 * truncate the old free space cache inode and
9061 * b) Setting 'dirty flag' makes sure that we flush
9062 * the new space cache info onto disk.
9064 cache->disk_cache_state = BTRFS_DC_CLEAR;
9065 if (btrfs_test_opt(root, SPACE_CACHE))
9069 read_extent_buffer(leaf, &cache->item,
9070 btrfs_item_ptr_offset(leaf, path->slots[0]),
9071 sizeof(cache->item));
9072 cache->flags = btrfs_block_group_flags(&cache->item);
9074 key.objectid = found_key.objectid + found_key.offset;
9075 btrfs_release_path(path);
9078 * We need to exclude the super stripes now so that the space
9079 * info has super bytes accounted for, otherwise we'll think
9080 * we have more space than we actually do.
9082 ret = exclude_super_stripes(root, cache);
9085 * We may have excluded something, so call this just in
9088 free_excluded_extents(root, cache);
9089 btrfs_put_block_group(cache);
9094 * check for two cases, either we are full, and therefore
9095 * don't need to bother with the caching work since we won't
9096 * find any space, or we are empty, and we can just add all
9097 * the space in and be done with it. This saves us _alot_ of
9098 * time, particularly in the full case.
9100 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
9101 cache->last_byte_to_unpin = (u64)-1;
9102 cache->cached = BTRFS_CACHE_FINISHED;
9103 free_excluded_extents(root, cache);
9104 } else if (btrfs_block_group_used(&cache->item) == 0) {
9105 cache->last_byte_to_unpin = (u64)-1;
9106 cache->cached = BTRFS_CACHE_FINISHED;
9107 add_new_free_space(cache, root->fs_info,
9109 found_key.objectid +
9111 free_excluded_extents(root, cache);
9114 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9116 btrfs_remove_free_space_cache(cache);
9117 btrfs_put_block_group(cache);
9121 ret = update_space_info(info, cache->flags, found_key.offset,
9122 btrfs_block_group_used(&cache->item),
9125 btrfs_remove_free_space_cache(cache);
9126 spin_lock(&info->block_group_cache_lock);
9127 rb_erase(&cache->cache_node,
9128 &info->block_group_cache_tree);
9129 spin_unlock(&info->block_group_cache_lock);
9130 btrfs_put_block_group(cache);
9134 cache->space_info = space_info;
9135 spin_lock(&cache->space_info->lock);
9136 cache->space_info->bytes_readonly += cache->bytes_super;
9137 spin_unlock(&cache->space_info->lock);
9139 __link_block_group(space_info, cache);
9141 set_avail_alloc_bits(root->fs_info, cache->flags);
9142 if (btrfs_chunk_readonly(root, cache->key.objectid))
9143 set_block_group_ro(cache, 1);
9146 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
9147 if (!(get_alloc_profile(root, space_info->flags) &
9148 (BTRFS_BLOCK_GROUP_RAID10 |
9149 BTRFS_BLOCK_GROUP_RAID1 |
9150 BTRFS_BLOCK_GROUP_RAID5 |
9151 BTRFS_BLOCK_GROUP_RAID6 |
9152 BTRFS_BLOCK_GROUP_DUP)))
9155 * avoid allocating from un-mirrored block group if there are
9156 * mirrored block groups.
9158 list_for_each_entry(cache,
9159 &space_info->block_groups[BTRFS_RAID_RAID0],
9161 set_block_group_ro(cache, 1);
9162 list_for_each_entry(cache,
9163 &space_info->block_groups[BTRFS_RAID_SINGLE],
9165 set_block_group_ro(cache, 1);
9168 init_global_block_rsv(info);
9171 btrfs_free_path(path);
9175 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
9176 struct btrfs_root *root)
9178 struct btrfs_block_group_cache *block_group, *tmp;
9179 struct btrfs_root *extent_root = root->fs_info->extent_root;
9180 struct btrfs_block_group_item item;
9181 struct btrfs_key key;
9184 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs,
9186 list_del_init(&block_group->new_bg_list);
9191 spin_lock(&block_group->lock);
9192 memcpy(&item, &block_group->item, sizeof(item));
9193 memcpy(&key, &block_group->key, sizeof(key));
9194 spin_unlock(&block_group->lock);
9196 ret = btrfs_insert_item(trans, extent_root, &key, &item,
9199 btrfs_abort_transaction(trans, extent_root, ret);
9200 ret = btrfs_finish_chunk_alloc(trans, extent_root,
9201 key.objectid, key.offset);
9203 btrfs_abort_transaction(trans, extent_root, ret);
9207 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
9208 struct btrfs_root *root, u64 bytes_used,
9209 u64 type, u64 chunk_objectid, u64 chunk_offset,
9213 struct btrfs_root *extent_root;
9214 struct btrfs_block_group_cache *cache;
9216 extent_root = root->fs_info->extent_root;
9218 btrfs_set_log_full_commit(root->fs_info, trans);
9220 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
9224 btrfs_set_block_group_used(&cache->item, bytes_used);
9225 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
9226 btrfs_set_block_group_flags(&cache->item, type);
9228 cache->flags = type;
9229 cache->last_byte_to_unpin = (u64)-1;
9230 cache->cached = BTRFS_CACHE_FINISHED;
9231 ret = exclude_super_stripes(root, cache);
9234 * We may have excluded something, so call this just in
9237 free_excluded_extents(root, cache);
9238 btrfs_put_block_group(cache);
9242 add_new_free_space(cache, root->fs_info, chunk_offset,
9243 chunk_offset + size);
9245 free_excluded_extents(root, cache);
9247 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9249 btrfs_remove_free_space_cache(cache);
9250 btrfs_put_block_group(cache);
9254 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
9255 &cache->space_info);
9257 btrfs_remove_free_space_cache(cache);
9258 spin_lock(&root->fs_info->block_group_cache_lock);
9259 rb_erase(&cache->cache_node,
9260 &root->fs_info->block_group_cache_tree);
9261 spin_unlock(&root->fs_info->block_group_cache_lock);
9262 btrfs_put_block_group(cache);
9265 update_global_block_rsv(root->fs_info);
9267 spin_lock(&cache->space_info->lock);
9268 cache->space_info->bytes_readonly += cache->bytes_super;
9269 spin_unlock(&cache->space_info->lock);
9271 __link_block_group(cache->space_info, cache);
9273 list_add_tail(&cache->new_bg_list, &trans->new_bgs);
9275 set_avail_alloc_bits(extent_root->fs_info, type);
9280 static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
9282 u64 extra_flags = chunk_to_extended(flags) &
9283 BTRFS_EXTENDED_PROFILE_MASK;
9285 write_seqlock(&fs_info->profiles_lock);
9286 if (flags & BTRFS_BLOCK_GROUP_DATA)
9287 fs_info->avail_data_alloc_bits &= ~extra_flags;
9288 if (flags & BTRFS_BLOCK_GROUP_METADATA)
9289 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
9290 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
9291 fs_info->avail_system_alloc_bits &= ~extra_flags;
9292 write_sequnlock(&fs_info->profiles_lock);
9295 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
9296 struct btrfs_root *root, u64 group_start)
9298 struct btrfs_path *path;
9299 struct btrfs_block_group_cache *block_group;
9300 struct btrfs_free_cluster *cluster;
9301 struct btrfs_root *tree_root = root->fs_info->tree_root;
9302 struct btrfs_key key;
9303 struct inode *inode;
9304 struct kobject *kobj = NULL;
9309 root = root->fs_info->extent_root;
9311 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
9312 BUG_ON(!block_group);
9313 BUG_ON(!block_group->ro);
9316 * Free the reserved super bytes from this block group before
9319 free_excluded_extents(root, block_group);
9321 memcpy(&key, &block_group->key, sizeof(key));
9322 index = get_block_group_index(block_group);
9323 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
9324 BTRFS_BLOCK_GROUP_RAID1 |
9325 BTRFS_BLOCK_GROUP_RAID10))
9330 /* make sure this block group isn't part of an allocation cluster */
9331 cluster = &root->fs_info->data_alloc_cluster;
9332 spin_lock(&cluster->refill_lock);
9333 btrfs_return_cluster_to_free_space(block_group, cluster);
9334 spin_unlock(&cluster->refill_lock);
9337 * make sure this block group isn't part of a metadata
9338 * allocation cluster
9340 cluster = &root->fs_info->meta_alloc_cluster;
9341 spin_lock(&cluster->refill_lock);
9342 btrfs_return_cluster_to_free_space(block_group, cluster);
9343 spin_unlock(&cluster->refill_lock);
9345 path = btrfs_alloc_path();
9351 inode = lookup_free_space_inode(tree_root, block_group, path);
9352 if (!IS_ERR(inode)) {
9353 ret = btrfs_orphan_add(trans, inode);
9355 btrfs_add_delayed_iput(inode);
9359 /* One for the block groups ref */
9360 spin_lock(&block_group->lock);
9361 if (block_group->iref) {
9362 block_group->iref = 0;
9363 block_group->inode = NULL;
9364 spin_unlock(&block_group->lock);
9367 spin_unlock(&block_group->lock);
9369 /* One for our lookup ref */
9370 btrfs_add_delayed_iput(inode);
9373 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
9374 key.offset = block_group->key.objectid;
9377 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
9381 btrfs_release_path(path);
9383 ret = btrfs_del_item(trans, tree_root, path);
9386 btrfs_release_path(path);
9389 spin_lock(&root->fs_info->block_group_cache_lock);
9390 rb_erase(&block_group->cache_node,
9391 &root->fs_info->block_group_cache_tree);
9393 if (root->fs_info->first_logical_byte == block_group->key.objectid)
9394 root->fs_info->first_logical_byte = (u64)-1;
9395 spin_unlock(&root->fs_info->block_group_cache_lock);
9397 down_write(&block_group->space_info->groups_sem);
9399 * we must use list_del_init so people can check to see if they
9400 * are still on the list after taking the semaphore
9402 list_del_init(&block_group->list);
9403 if (list_empty(&block_group->space_info->block_groups[index])) {
9404 kobj = block_group->space_info->block_group_kobjs[index];
9405 block_group->space_info->block_group_kobjs[index] = NULL;
9406 clear_avail_alloc_bits(root->fs_info, block_group->flags);
9408 up_write(&block_group->space_info->groups_sem);
9414 if (block_group->cached == BTRFS_CACHE_STARTED)
9415 wait_block_group_cache_done(block_group);
9417 btrfs_remove_free_space_cache(block_group);
9419 spin_lock(&block_group->space_info->lock);
9420 block_group->space_info->total_bytes -= block_group->key.offset;
9421 block_group->space_info->bytes_readonly -= block_group->key.offset;
9422 block_group->space_info->disk_total -= block_group->key.offset * factor;
9423 spin_unlock(&block_group->space_info->lock);
9425 memcpy(&key, &block_group->key, sizeof(key));
9427 btrfs_clear_space_info_full(root->fs_info);
9429 btrfs_put_block_group(block_group);
9430 btrfs_put_block_group(block_group);
9432 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
9438 ret = btrfs_del_item(trans, root, path);
9440 btrfs_free_path(path);
9444 int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
9446 struct btrfs_space_info *space_info;
9447 struct btrfs_super_block *disk_super;
9453 disk_super = fs_info->super_copy;
9454 if (!btrfs_super_root(disk_super))
9457 features = btrfs_super_incompat_flags(disk_super);
9458 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
9461 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9462 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9467 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
9468 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9470 flags = BTRFS_BLOCK_GROUP_METADATA;
9471 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9475 flags = BTRFS_BLOCK_GROUP_DATA;
9476 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9482 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
9484 return unpin_extent_range(root, start, end);
9487 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
9488 u64 num_bytes, u64 *actual_bytes)
9490 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
9493 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
9495 struct btrfs_fs_info *fs_info = root->fs_info;
9496 struct btrfs_block_group_cache *cache = NULL;
9501 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
9505 * try to trim all FS space, our block group may start from non-zero.
9507 if (range->len == total_bytes)
9508 cache = btrfs_lookup_first_block_group(fs_info, range->start);
9510 cache = btrfs_lookup_block_group(fs_info, range->start);
9513 if (cache->key.objectid >= (range->start + range->len)) {
9514 btrfs_put_block_group(cache);
9518 start = max(range->start, cache->key.objectid);
9519 end = min(range->start + range->len,
9520 cache->key.objectid + cache->key.offset);
9522 if (end - start >= range->minlen) {
9523 if (!block_group_cache_done(cache)) {
9524 ret = cache_block_group(cache, 0);
9526 btrfs_put_block_group(cache);
9529 ret = wait_block_group_cache_done(cache);
9531 btrfs_put_block_group(cache);
9535 ret = btrfs_trim_block_group(cache,
9541 trimmed += group_trimmed;
9543 btrfs_put_block_group(cache);
9548 cache = next_block_group(fs_info->tree_root, cache);
9551 range->len = trimmed;
9556 * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(),
9557 * they are used to prevent the some tasks writing data into the page cache
9558 * by nocow before the subvolume is snapshoted, but flush the data into
9559 * the disk after the snapshot creation.
9561 void btrfs_end_nocow_write(struct btrfs_root *root)
9563 percpu_counter_dec(&root->subv_writers->counter);
9565 * Make sure counter is updated before we wake up
9569 if (waitqueue_active(&root->subv_writers->wait))
9570 wake_up(&root->subv_writers->wait);
9573 int btrfs_start_nocow_write(struct btrfs_root *root)
9575 if (unlikely(atomic_read(&root->will_be_snapshoted)))
9578 percpu_counter_inc(&root->subv_writers->counter);
9580 * Make sure counter is updated before we check for snapshot creation.
9583 if (unlikely(atomic_read(&root->will_be_snapshoted))) {
9584 btrfs_end_nocow_write(root);