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->nodesize;
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, btrfs_cache_helper,
556 caching_thread, NULL, NULL);
558 spin_lock(&cache->lock);
560 * This should be a rare occasion, but this could happen I think in the
561 * case where one thread starts to load the space cache info, and then
562 * some other thread starts a transaction commit which tries to do an
563 * allocation while the other thread is still loading the space cache
564 * info. The previous loop should have kept us from choosing this block
565 * group, but if we've moved to the state where we will wait on caching
566 * block groups we need to first check if we're doing a fast load here,
567 * so we can wait for it to finish, otherwise we could end up allocating
568 * from a block group who's cache gets evicted for one reason or
571 while (cache->cached == BTRFS_CACHE_FAST) {
572 struct btrfs_caching_control *ctl;
574 ctl = cache->caching_ctl;
575 atomic_inc(&ctl->count);
576 prepare_to_wait(&ctl->wait, &wait, TASK_UNINTERRUPTIBLE);
577 spin_unlock(&cache->lock);
581 finish_wait(&ctl->wait, &wait);
582 put_caching_control(ctl);
583 spin_lock(&cache->lock);
586 if (cache->cached != BTRFS_CACHE_NO) {
587 spin_unlock(&cache->lock);
591 WARN_ON(cache->caching_ctl);
592 cache->caching_ctl = caching_ctl;
593 cache->cached = BTRFS_CACHE_FAST;
594 spin_unlock(&cache->lock);
596 if (fs_info->mount_opt & BTRFS_MOUNT_SPACE_CACHE) {
597 ret = load_free_space_cache(fs_info, cache);
599 spin_lock(&cache->lock);
601 cache->caching_ctl = NULL;
602 cache->cached = BTRFS_CACHE_FINISHED;
603 cache->last_byte_to_unpin = (u64)-1;
605 if (load_cache_only) {
606 cache->caching_ctl = NULL;
607 cache->cached = BTRFS_CACHE_NO;
609 cache->cached = BTRFS_CACHE_STARTED;
612 spin_unlock(&cache->lock);
613 wake_up(&caching_ctl->wait);
615 put_caching_control(caching_ctl);
616 free_excluded_extents(fs_info->extent_root, cache);
621 * We are not going to do the fast caching, set cached to the
622 * appropriate value and wakeup any waiters.
624 spin_lock(&cache->lock);
625 if (load_cache_only) {
626 cache->caching_ctl = NULL;
627 cache->cached = BTRFS_CACHE_NO;
629 cache->cached = BTRFS_CACHE_STARTED;
631 spin_unlock(&cache->lock);
632 wake_up(&caching_ctl->wait);
635 if (load_cache_only) {
636 put_caching_control(caching_ctl);
640 down_write(&fs_info->commit_root_sem);
641 atomic_inc(&caching_ctl->count);
642 list_add_tail(&caching_ctl->list, &fs_info->caching_block_groups);
643 up_write(&fs_info->commit_root_sem);
645 btrfs_get_block_group(cache);
647 btrfs_queue_work(fs_info->caching_workers, &caching_ctl->work);
653 * return the block group that starts at or after bytenr
655 static struct btrfs_block_group_cache *
656 btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
658 struct btrfs_block_group_cache *cache;
660 cache = block_group_cache_tree_search(info, bytenr, 0);
666 * return the block group that contains the given bytenr
668 struct btrfs_block_group_cache *btrfs_lookup_block_group(
669 struct btrfs_fs_info *info,
672 struct btrfs_block_group_cache *cache;
674 cache = block_group_cache_tree_search(info, bytenr, 1);
679 static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
682 struct list_head *head = &info->space_info;
683 struct btrfs_space_info *found;
685 flags &= BTRFS_BLOCK_GROUP_TYPE_MASK;
688 list_for_each_entry_rcu(found, head, list) {
689 if (found->flags & flags) {
699 * after adding space to the filesystem, we need to clear the full flags
700 * on all the space infos.
702 void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
704 struct list_head *head = &info->space_info;
705 struct btrfs_space_info *found;
708 list_for_each_entry_rcu(found, head, list)
713 /* simple helper to search for an existing data extent at a given offset */
714 int btrfs_lookup_data_extent(struct btrfs_root *root, u64 start, u64 len)
717 struct btrfs_key key;
718 struct btrfs_path *path;
720 path = btrfs_alloc_path();
724 key.objectid = start;
726 key.type = BTRFS_EXTENT_ITEM_KEY;
727 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
729 btrfs_free_path(path);
734 * helper function to lookup reference count and flags of a tree block.
736 * the head node for delayed ref is used to store the sum of all the
737 * reference count modifications queued up in the rbtree. the head
738 * node may also store the extent flags to set. This way you can check
739 * to see what the reference count and extent flags would be if all of
740 * the delayed refs are not processed.
742 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root, u64 bytenr,
744 u64 offset, int metadata, u64 *refs, u64 *flags)
746 struct btrfs_delayed_ref_head *head;
747 struct btrfs_delayed_ref_root *delayed_refs;
748 struct btrfs_path *path;
749 struct btrfs_extent_item *ei;
750 struct extent_buffer *leaf;
751 struct btrfs_key key;
758 * If we don't have skinny metadata, don't bother doing anything
761 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA)) {
762 offset = root->nodesize;
766 path = btrfs_alloc_path();
771 path->skip_locking = 1;
772 path->search_commit_root = 1;
776 key.objectid = bytenr;
779 key.type = BTRFS_METADATA_ITEM_KEY;
781 key.type = BTRFS_EXTENT_ITEM_KEY;
784 ret = btrfs_search_slot(trans, root->fs_info->extent_root,
789 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
790 if (path->slots[0]) {
792 btrfs_item_key_to_cpu(path->nodes[0], &key,
794 if (key.objectid == bytenr &&
795 key.type == BTRFS_EXTENT_ITEM_KEY &&
796 key.offset == root->nodesize)
800 key.objectid = bytenr;
801 key.type = BTRFS_EXTENT_ITEM_KEY;
802 key.offset = root->nodesize;
803 btrfs_release_path(path);
809 leaf = path->nodes[0];
810 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
811 if (item_size >= sizeof(*ei)) {
812 ei = btrfs_item_ptr(leaf, path->slots[0],
813 struct btrfs_extent_item);
814 num_refs = btrfs_extent_refs(leaf, ei);
815 extent_flags = btrfs_extent_flags(leaf, ei);
817 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
818 struct btrfs_extent_item_v0 *ei0;
819 BUG_ON(item_size != sizeof(*ei0));
820 ei0 = btrfs_item_ptr(leaf, path->slots[0],
821 struct btrfs_extent_item_v0);
822 num_refs = btrfs_extent_refs_v0(leaf, ei0);
823 /* FIXME: this isn't correct for data */
824 extent_flags = BTRFS_BLOCK_FLAG_FULL_BACKREF;
829 BUG_ON(num_refs == 0);
839 delayed_refs = &trans->transaction->delayed_refs;
840 spin_lock(&delayed_refs->lock);
841 head = btrfs_find_delayed_ref_head(trans, bytenr);
843 if (!mutex_trylock(&head->mutex)) {
844 atomic_inc(&head->node.refs);
845 spin_unlock(&delayed_refs->lock);
847 btrfs_release_path(path);
850 * Mutex was contended, block until it's released and try
853 mutex_lock(&head->mutex);
854 mutex_unlock(&head->mutex);
855 btrfs_put_delayed_ref(&head->node);
858 spin_lock(&head->lock);
859 if (head->extent_op && head->extent_op->update_flags)
860 extent_flags |= head->extent_op->flags_to_set;
862 BUG_ON(num_refs == 0);
864 num_refs += head->node.ref_mod;
865 spin_unlock(&head->lock);
866 mutex_unlock(&head->mutex);
868 spin_unlock(&delayed_refs->lock);
870 WARN_ON(num_refs == 0);
874 *flags = extent_flags;
876 btrfs_free_path(path);
881 * Back reference rules. Back refs have three main goals:
883 * 1) differentiate between all holders of references to an extent so that
884 * when a reference is dropped we can make sure it was a valid reference
885 * before freeing the extent.
887 * 2) Provide enough information to quickly find the holders of an extent
888 * if we notice a given block is corrupted or bad.
890 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
891 * maintenance. This is actually the same as #2, but with a slightly
892 * different use case.
894 * There are two kinds of back refs. The implicit back refs is optimized
895 * for pointers in non-shared tree blocks. For a given pointer in a block,
896 * back refs of this kind provide information about the block's owner tree
897 * and the pointer's key. These information allow us to find the block by
898 * b-tree searching. The full back refs is for pointers in tree blocks not
899 * referenced by their owner trees. The location of tree block is recorded
900 * in the back refs. Actually the full back refs is generic, and can be
901 * used in all cases the implicit back refs is used. The major shortcoming
902 * of the full back refs is its overhead. Every time a tree block gets
903 * COWed, we have to update back refs entry for all pointers in it.
905 * For a newly allocated tree block, we use implicit back refs for
906 * pointers in it. This means most tree related operations only involve
907 * implicit back refs. For a tree block created in old transaction, the
908 * only way to drop a reference to it is COW it. So we can detect the
909 * event that tree block loses its owner tree's reference and do the
910 * back refs conversion.
912 * When a tree block is COW'd through a tree, there are four cases:
914 * The reference count of the block is one and the tree is the block's
915 * owner tree. Nothing to do in this case.
917 * The reference count of the block is one and the tree is not the
918 * block's owner tree. In this case, full back refs is used for pointers
919 * in the block. Remove these full back refs, add implicit back refs for
920 * every pointers in the new block.
922 * The reference count of the block is greater than one and the tree is
923 * the block's owner tree. In this case, implicit back refs is used for
924 * pointers in the block. Add full back refs for every pointers in the
925 * block, increase lower level extents' reference counts. The original
926 * implicit back refs are entailed to the new block.
928 * The reference count of the block is greater than one and the tree is
929 * not the block's owner tree. Add implicit back refs for every pointer in
930 * the new block, increase lower level extents' reference count.
932 * Back Reference Key composing:
934 * The key objectid corresponds to the first byte in the extent,
935 * The key type is used to differentiate between types of back refs.
936 * There are different meanings of the key offset for different types
939 * File extents can be referenced by:
941 * - multiple snapshots, subvolumes, or different generations in one subvol
942 * - different files inside a single subvolume
943 * - different offsets inside a file (bookend extents in file.c)
945 * The extent ref structure for the implicit back refs has fields for:
947 * - Objectid of the subvolume root
948 * - objectid of the file holding the reference
949 * - original offset in the file
950 * - how many bookend extents
952 * The key offset for the implicit back refs is hash of the first
955 * The extent ref structure for the full back refs has field for:
957 * - number of pointers in the tree leaf
959 * The key offset for the implicit back refs is the first byte of
962 * When a file extent is allocated, The implicit back refs is used.
963 * the fields are filled in:
965 * (root_key.objectid, inode objectid, offset in file, 1)
967 * When a file extent is removed file truncation, we find the
968 * corresponding implicit back refs and check the following fields:
970 * (btrfs_header_owner(leaf), inode objectid, offset in file)
972 * Btree extents can be referenced by:
974 * - Different subvolumes
976 * Both the implicit back refs and the full back refs for tree blocks
977 * only consist of key. The key offset for the implicit back refs is
978 * objectid of block's owner tree. The key offset for the full back refs
979 * is the first byte of parent block.
981 * When implicit back refs is used, information about the lowest key and
982 * level of the tree block are required. These information are stored in
983 * tree block info structure.
986 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
987 static int convert_extent_item_v0(struct btrfs_trans_handle *trans,
988 struct btrfs_root *root,
989 struct btrfs_path *path,
990 u64 owner, u32 extra_size)
992 struct btrfs_extent_item *item;
993 struct btrfs_extent_item_v0 *ei0;
994 struct btrfs_extent_ref_v0 *ref0;
995 struct btrfs_tree_block_info *bi;
996 struct extent_buffer *leaf;
997 struct btrfs_key key;
998 struct btrfs_key found_key;
999 u32 new_size = sizeof(*item);
1003 leaf = path->nodes[0];
1004 BUG_ON(btrfs_item_size_nr(leaf, path->slots[0]) != sizeof(*ei0));
1006 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1007 ei0 = btrfs_item_ptr(leaf, path->slots[0],
1008 struct btrfs_extent_item_v0);
1009 refs = btrfs_extent_refs_v0(leaf, ei0);
1011 if (owner == (u64)-1) {
1013 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1014 ret = btrfs_next_leaf(root, path);
1017 BUG_ON(ret > 0); /* Corruption */
1018 leaf = path->nodes[0];
1020 btrfs_item_key_to_cpu(leaf, &found_key,
1022 BUG_ON(key.objectid != found_key.objectid);
1023 if (found_key.type != BTRFS_EXTENT_REF_V0_KEY) {
1027 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1028 struct btrfs_extent_ref_v0);
1029 owner = btrfs_ref_objectid_v0(leaf, ref0);
1033 btrfs_release_path(path);
1035 if (owner < BTRFS_FIRST_FREE_OBJECTID)
1036 new_size += sizeof(*bi);
1038 new_size -= sizeof(*ei0);
1039 ret = btrfs_search_slot(trans, root, &key, path,
1040 new_size + extra_size, 1);
1043 BUG_ON(ret); /* Corruption */
1045 btrfs_extend_item(root, path, new_size);
1047 leaf = path->nodes[0];
1048 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1049 btrfs_set_extent_refs(leaf, item, refs);
1050 /* FIXME: get real generation */
1051 btrfs_set_extent_generation(leaf, item, 0);
1052 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1053 btrfs_set_extent_flags(leaf, item,
1054 BTRFS_EXTENT_FLAG_TREE_BLOCK |
1055 BTRFS_BLOCK_FLAG_FULL_BACKREF);
1056 bi = (struct btrfs_tree_block_info *)(item + 1);
1057 /* FIXME: get first key of the block */
1058 memset_extent_buffer(leaf, 0, (unsigned long)bi, sizeof(*bi));
1059 btrfs_set_tree_block_level(leaf, bi, (int)owner);
1061 btrfs_set_extent_flags(leaf, item, BTRFS_EXTENT_FLAG_DATA);
1063 btrfs_mark_buffer_dirty(leaf);
1068 static u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
1070 u32 high_crc = ~(u32)0;
1071 u32 low_crc = ~(u32)0;
1074 lenum = cpu_to_le64(root_objectid);
1075 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
1076 lenum = cpu_to_le64(owner);
1077 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1078 lenum = cpu_to_le64(offset);
1079 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
1081 return ((u64)high_crc << 31) ^ (u64)low_crc;
1084 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
1085 struct btrfs_extent_data_ref *ref)
1087 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
1088 btrfs_extent_data_ref_objectid(leaf, ref),
1089 btrfs_extent_data_ref_offset(leaf, ref));
1092 static int match_extent_data_ref(struct extent_buffer *leaf,
1093 struct btrfs_extent_data_ref *ref,
1094 u64 root_objectid, u64 owner, u64 offset)
1096 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
1097 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
1098 btrfs_extent_data_ref_offset(leaf, ref) != offset)
1103 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
1105 struct btrfs_path *path,
1106 u64 bytenr, u64 parent,
1108 u64 owner, u64 offset)
1110 struct btrfs_key key;
1111 struct btrfs_extent_data_ref *ref;
1112 struct extent_buffer *leaf;
1118 key.objectid = bytenr;
1120 key.type = BTRFS_SHARED_DATA_REF_KEY;
1121 key.offset = parent;
1123 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1124 key.offset = hash_extent_data_ref(root_objectid,
1129 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1138 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1139 key.type = BTRFS_EXTENT_REF_V0_KEY;
1140 btrfs_release_path(path);
1141 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1152 leaf = path->nodes[0];
1153 nritems = btrfs_header_nritems(leaf);
1155 if (path->slots[0] >= nritems) {
1156 ret = btrfs_next_leaf(root, path);
1162 leaf = path->nodes[0];
1163 nritems = btrfs_header_nritems(leaf);
1167 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1168 if (key.objectid != bytenr ||
1169 key.type != BTRFS_EXTENT_DATA_REF_KEY)
1172 ref = btrfs_item_ptr(leaf, path->slots[0],
1173 struct btrfs_extent_data_ref);
1175 if (match_extent_data_ref(leaf, ref, root_objectid,
1178 btrfs_release_path(path);
1190 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
1191 struct btrfs_root *root,
1192 struct btrfs_path *path,
1193 u64 bytenr, u64 parent,
1194 u64 root_objectid, u64 owner,
1195 u64 offset, int refs_to_add)
1197 struct btrfs_key key;
1198 struct extent_buffer *leaf;
1203 key.objectid = bytenr;
1205 key.type = BTRFS_SHARED_DATA_REF_KEY;
1206 key.offset = parent;
1207 size = sizeof(struct btrfs_shared_data_ref);
1209 key.type = BTRFS_EXTENT_DATA_REF_KEY;
1210 key.offset = hash_extent_data_ref(root_objectid,
1212 size = sizeof(struct btrfs_extent_data_ref);
1215 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
1216 if (ret && ret != -EEXIST)
1219 leaf = path->nodes[0];
1221 struct btrfs_shared_data_ref *ref;
1222 ref = btrfs_item_ptr(leaf, path->slots[0],
1223 struct btrfs_shared_data_ref);
1225 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
1227 num_refs = btrfs_shared_data_ref_count(leaf, ref);
1228 num_refs += refs_to_add;
1229 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
1232 struct btrfs_extent_data_ref *ref;
1233 while (ret == -EEXIST) {
1234 ref = btrfs_item_ptr(leaf, path->slots[0],
1235 struct btrfs_extent_data_ref);
1236 if (match_extent_data_ref(leaf, ref, root_objectid,
1239 btrfs_release_path(path);
1241 ret = btrfs_insert_empty_item(trans, root, path, &key,
1243 if (ret && ret != -EEXIST)
1246 leaf = path->nodes[0];
1248 ref = btrfs_item_ptr(leaf, path->slots[0],
1249 struct btrfs_extent_data_ref);
1251 btrfs_set_extent_data_ref_root(leaf, ref,
1253 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
1254 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
1255 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
1257 num_refs = btrfs_extent_data_ref_count(leaf, ref);
1258 num_refs += refs_to_add;
1259 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
1262 btrfs_mark_buffer_dirty(leaf);
1265 btrfs_release_path(path);
1269 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
1270 struct btrfs_root *root,
1271 struct btrfs_path *path,
1272 int refs_to_drop, int *last_ref)
1274 struct btrfs_key key;
1275 struct btrfs_extent_data_ref *ref1 = NULL;
1276 struct btrfs_shared_data_ref *ref2 = NULL;
1277 struct extent_buffer *leaf;
1281 leaf = path->nodes[0];
1282 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1284 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1285 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1286 struct btrfs_extent_data_ref);
1287 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1288 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1289 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1290 struct btrfs_shared_data_ref);
1291 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1292 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1293 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1294 struct btrfs_extent_ref_v0 *ref0;
1295 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1296 struct btrfs_extent_ref_v0);
1297 num_refs = btrfs_ref_count_v0(leaf, ref0);
1303 BUG_ON(num_refs < refs_to_drop);
1304 num_refs -= refs_to_drop;
1306 if (num_refs == 0) {
1307 ret = btrfs_del_item(trans, root, path);
1310 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
1311 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
1312 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
1313 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
1314 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1316 struct btrfs_extent_ref_v0 *ref0;
1317 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1318 struct btrfs_extent_ref_v0);
1319 btrfs_set_ref_count_v0(leaf, ref0, num_refs);
1322 btrfs_mark_buffer_dirty(leaf);
1327 static noinline u32 extent_data_ref_count(struct btrfs_root *root,
1328 struct btrfs_path *path,
1329 struct btrfs_extent_inline_ref *iref)
1331 struct btrfs_key key;
1332 struct extent_buffer *leaf;
1333 struct btrfs_extent_data_ref *ref1;
1334 struct btrfs_shared_data_ref *ref2;
1337 leaf = path->nodes[0];
1338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1340 if (btrfs_extent_inline_ref_type(leaf, iref) ==
1341 BTRFS_EXTENT_DATA_REF_KEY) {
1342 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
1343 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1345 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
1346 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1348 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
1349 ref1 = btrfs_item_ptr(leaf, path->slots[0],
1350 struct btrfs_extent_data_ref);
1351 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
1352 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
1353 ref2 = btrfs_item_ptr(leaf, path->slots[0],
1354 struct btrfs_shared_data_ref);
1355 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
1356 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1357 } else if (key.type == BTRFS_EXTENT_REF_V0_KEY) {
1358 struct btrfs_extent_ref_v0 *ref0;
1359 ref0 = btrfs_item_ptr(leaf, path->slots[0],
1360 struct btrfs_extent_ref_v0);
1361 num_refs = btrfs_ref_count_v0(leaf, ref0);
1369 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
1370 struct btrfs_root *root,
1371 struct btrfs_path *path,
1372 u64 bytenr, u64 parent,
1375 struct btrfs_key key;
1378 key.objectid = bytenr;
1380 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1381 key.offset = parent;
1383 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1384 key.offset = root_objectid;
1387 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1390 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1391 if (ret == -ENOENT && parent) {
1392 btrfs_release_path(path);
1393 key.type = BTRFS_EXTENT_REF_V0_KEY;
1394 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1402 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
1403 struct btrfs_root *root,
1404 struct btrfs_path *path,
1405 u64 bytenr, u64 parent,
1408 struct btrfs_key key;
1411 key.objectid = bytenr;
1413 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
1414 key.offset = parent;
1416 key.type = BTRFS_TREE_BLOCK_REF_KEY;
1417 key.offset = root_objectid;
1420 ret = btrfs_insert_empty_item(trans, root, path, &key, 0);
1421 btrfs_release_path(path);
1425 static inline int extent_ref_type(u64 parent, u64 owner)
1428 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1430 type = BTRFS_SHARED_BLOCK_REF_KEY;
1432 type = BTRFS_TREE_BLOCK_REF_KEY;
1435 type = BTRFS_SHARED_DATA_REF_KEY;
1437 type = BTRFS_EXTENT_DATA_REF_KEY;
1442 static int find_next_key(struct btrfs_path *path, int level,
1443 struct btrfs_key *key)
1446 for (; level < BTRFS_MAX_LEVEL; level++) {
1447 if (!path->nodes[level])
1449 if (path->slots[level] + 1 >=
1450 btrfs_header_nritems(path->nodes[level]))
1453 btrfs_item_key_to_cpu(path->nodes[level], key,
1454 path->slots[level] + 1);
1456 btrfs_node_key_to_cpu(path->nodes[level], key,
1457 path->slots[level] + 1);
1464 * look for inline back ref. if back ref is found, *ref_ret is set
1465 * to the address of inline back ref, and 0 is returned.
1467 * if back ref isn't found, *ref_ret is set to the address where it
1468 * should be inserted, and -ENOENT is returned.
1470 * if insert is true and there are too many inline back refs, the path
1471 * points to the extent item, and -EAGAIN is returned.
1473 * NOTE: inline back refs are ordered in the same way that back ref
1474 * items in the tree are ordered.
1476 static noinline_for_stack
1477 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
1478 struct btrfs_root *root,
1479 struct btrfs_path *path,
1480 struct btrfs_extent_inline_ref **ref_ret,
1481 u64 bytenr, u64 num_bytes,
1482 u64 parent, u64 root_objectid,
1483 u64 owner, u64 offset, int insert)
1485 struct btrfs_key key;
1486 struct extent_buffer *leaf;
1487 struct btrfs_extent_item *ei;
1488 struct btrfs_extent_inline_ref *iref;
1498 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
1501 key.objectid = bytenr;
1502 key.type = BTRFS_EXTENT_ITEM_KEY;
1503 key.offset = num_bytes;
1505 want = extent_ref_type(parent, owner);
1507 extra_size = btrfs_extent_inline_ref_size(want);
1508 path->keep_locks = 1;
1513 * Owner is our parent level, so we can just add one to get the level
1514 * for the block we are interested in.
1516 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
1517 key.type = BTRFS_METADATA_ITEM_KEY;
1522 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
1529 * We may be a newly converted file system which still has the old fat
1530 * extent entries for metadata, so try and see if we have one of those.
1532 if (ret > 0 && skinny_metadata) {
1533 skinny_metadata = false;
1534 if (path->slots[0]) {
1536 btrfs_item_key_to_cpu(path->nodes[0], &key,
1538 if (key.objectid == bytenr &&
1539 key.type == BTRFS_EXTENT_ITEM_KEY &&
1540 key.offset == num_bytes)
1544 key.objectid = bytenr;
1545 key.type = BTRFS_EXTENT_ITEM_KEY;
1546 key.offset = num_bytes;
1547 btrfs_release_path(path);
1552 if (ret && !insert) {
1555 } else if (WARN_ON(ret)) {
1560 leaf = path->nodes[0];
1561 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1562 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
1563 if (item_size < sizeof(*ei)) {
1568 ret = convert_extent_item_v0(trans, root, path, owner,
1574 leaf = path->nodes[0];
1575 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1578 BUG_ON(item_size < sizeof(*ei));
1580 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1581 flags = btrfs_extent_flags(leaf, ei);
1583 ptr = (unsigned long)(ei + 1);
1584 end = (unsigned long)ei + item_size;
1586 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
1587 ptr += sizeof(struct btrfs_tree_block_info);
1597 iref = (struct btrfs_extent_inline_ref *)ptr;
1598 type = btrfs_extent_inline_ref_type(leaf, iref);
1602 ptr += btrfs_extent_inline_ref_size(type);
1606 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1607 struct btrfs_extent_data_ref *dref;
1608 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1609 if (match_extent_data_ref(leaf, dref, root_objectid,
1614 if (hash_extent_data_ref_item(leaf, dref) <
1615 hash_extent_data_ref(root_objectid, owner, offset))
1619 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1621 if (parent == ref_offset) {
1625 if (ref_offset < parent)
1628 if (root_objectid == ref_offset) {
1632 if (ref_offset < root_objectid)
1636 ptr += btrfs_extent_inline_ref_size(type);
1638 if (err == -ENOENT && insert) {
1639 if (item_size + extra_size >=
1640 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
1645 * To add new inline back ref, we have to make sure
1646 * there is no corresponding back ref item.
1647 * For simplicity, we just do not add new inline back
1648 * ref if there is any kind of item for this block
1650 if (find_next_key(path, 0, &key) == 0 &&
1651 key.objectid == bytenr &&
1652 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
1657 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
1660 path->keep_locks = 0;
1661 btrfs_unlock_up_safe(path, 1);
1667 * helper to add new inline back ref
1669 static noinline_for_stack
1670 void setup_inline_extent_backref(struct btrfs_root *root,
1671 struct btrfs_path *path,
1672 struct btrfs_extent_inline_ref *iref,
1673 u64 parent, u64 root_objectid,
1674 u64 owner, u64 offset, int refs_to_add,
1675 struct btrfs_delayed_extent_op *extent_op)
1677 struct extent_buffer *leaf;
1678 struct btrfs_extent_item *ei;
1681 unsigned long item_offset;
1686 leaf = path->nodes[0];
1687 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1688 item_offset = (unsigned long)iref - (unsigned long)ei;
1690 type = extent_ref_type(parent, owner);
1691 size = btrfs_extent_inline_ref_size(type);
1693 btrfs_extend_item(root, path, size);
1695 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1696 refs = btrfs_extent_refs(leaf, ei);
1697 refs += refs_to_add;
1698 btrfs_set_extent_refs(leaf, ei, refs);
1700 __run_delayed_extent_op(extent_op, leaf, ei);
1702 ptr = (unsigned long)ei + item_offset;
1703 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1704 if (ptr < end - size)
1705 memmove_extent_buffer(leaf, ptr + size, ptr,
1708 iref = (struct btrfs_extent_inline_ref *)ptr;
1709 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1710 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1711 struct btrfs_extent_data_ref *dref;
1712 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1713 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1714 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1715 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1716 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1717 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1718 struct btrfs_shared_data_ref *sref;
1719 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1720 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1721 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1722 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1723 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1725 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1727 btrfs_mark_buffer_dirty(leaf);
1730 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1731 struct btrfs_root *root,
1732 struct btrfs_path *path,
1733 struct btrfs_extent_inline_ref **ref_ret,
1734 u64 bytenr, u64 num_bytes, u64 parent,
1735 u64 root_objectid, u64 owner, u64 offset)
1739 ret = lookup_inline_extent_backref(trans, root, path, ref_ret,
1740 bytenr, num_bytes, parent,
1741 root_objectid, owner, offset, 0);
1745 btrfs_release_path(path);
1748 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1749 ret = lookup_tree_block_ref(trans, root, path, bytenr, parent,
1752 ret = lookup_extent_data_ref(trans, root, path, bytenr, parent,
1753 root_objectid, owner, offset);
1759 * helper to update/remove inline back ref
1761 static noinline_for_stack
1762 void update_inline_extent_backref(struct btrfs_root *root,
1763 struct btrfs_path *path,
1764 struct btrfs_extent_inline_ref *iref,
1766 struct btrfs_delayed_extent_op *extent_op,
1769 struct extent_buffer *leaf;
1770 struct btrfs_extent_item *ei;
1771 struct btrfs_extent_data_ref *dref = NULL;
1772 struct btrfs_shared_data_ref *sref = NULL;
1780 leaf = path->nodes[0];
1781 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1782 refs = btrfs_extent_refs(leaf, ei);
1783 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1784 refs += refs_to_mod;
1785 btrfs_set_extent_refs(leaf, ei, refs);
1787 __run_delayed_extent_op(extent_op, leaf, ei);
1789 type = btrfs_extent_inline_ref_type(leaf, iref);
1791 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1792 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1793 refs = btrfs_extent_data_ref_count(leaf, dref);
1794 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1795 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1796 refs = btrfs_shared_data_ref_count(leaf, sref);
1799 BUG_ON(refs_to_mod != -1);
1802 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1803 refs += refs_to_mod;
1806 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1807 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1809 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1812 size = btrfs_extent_inline_ref_size(type);
1813 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1814 ptr = (unsigned long)iref;
1815 end = (unsigned long)ei + item_size;
1816 if (ptr + size < end)
1817 memmove_extent_buffer(leaf, ptr, ptr + size,
1820 btrfs_truncate_item(root, path, item_size, 1);
1822 btrfs_mark_buffer_dirty(leaf);
1825 static noinline_for_stack
1826 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1827 struct btrfs_root *root,
1828 struct btrfs_path *path,
1829 u64 bytenr, u64 num_bytes, u64 parent,
1830 u64 root_objectid, u64 owner,
1831 u64 offset, int refs_to_add,
1832 struct btrfs_delayed_extent_op *extent_op)
1834 struct btrfs_extent_inline_ref *iref;
1837 ret = lookup_inline_extent_backref(trans, root, path, &iref,
1838 bytenr, num_bytes, parent,
1839 root_objectid, owner, offset, 1);
1841 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID);
1842 update_inline_extent_backref(root, path, iref,
1843 refs_to_add, extent_op, NULL);
1844 } else if (ret == -ENOENT) {
1845 setup_inline_extent_backref(root, path, iref, parent,
1846 root_objectid, owner, offset,
1847 refs_to_add, extent_op);
1853 static int insert_extent_backref(struct btrfs_trans_handle *trans,
1854 struct btrfs_root *root,
1855 struct btrfs_path *path,
1856 u64 bytenr, u64 parent, u64 root_objectid,
1857 u64 owner, u64 offset, int refs_to_add)
1860 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1861 BUG_ON(refs_to_add != 1);
1862 ret = insert_tree_block_ref(trans, root, path, bytenr,
1863 parent, root_objectid);
1865 ret = insert_extent_data_ref(trans, root, path, bytenr,
1866 parent, root_objectid,
1867 owner, offset, refs_to_add);
1872 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1873 struct btrfs_root *root,
1874 struct btrfs_path *path,
1875 struct btrfs_extent_inline_ref *iref,
1876 int refs_to_drop, int is_data, int *last_ref)
1880 BUG_ON(!is_data && refs_to_drop != 1);
1882 update_inline_extent_backref(root, path, iref,
1883 -refs_to_drop, NULL, last_ref);
1884 } else if (is_data) {
1885 ret = remove_extent_data_ref(trans, root, path, refs_to_drop,
1889 ret = btrfs_del_item(trans, root, path);
1894 static int btrfs_issue_discard(struct block_device *bdev,
1897 return blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_NOFS, 0);
1900 static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
1901 u64 num_bytes, u64 *actual_bytes)
1904 u64 discarded_bytes = 0;
1905 struct btrfs_bio *bbio = NULL;
1908 /* Tell the block device(s) that the sectors can be discarded */
1909 ret = btrfs_map_block(root->fs_info, REQ_DISCARD,
1910 bytenr, &num_bytes, &bbio, 0);
1911 /* Error condition is -ENOMEM */
1913 struct btrfs_bio_stripe *stripe = bbio->stripes;
1917 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
1918 if (!stripe->dev->can_discard)
1921 ret = btrfs_issue_discard(stripe->dev->bdev,
1925 discarded_bytes += stripe->length;
1926 else if (ret != -EOPNOTSUPP)
1927 break; /* Logic errors or -ENOMEM, or -EIO but I don't know how that could happen JDM */
1930 * Just in case we get back EOPNOTSUPP for some reason,
1931 * just ignore the return value so we don't screw up
1932 * people calling discard_extent.
1940 *actual_bytes = discarded_bytes;
1943 if (ret == -EOPNOTSUPP)
1948 /* Can return -ENOMEM */
1949 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1950 struct btrfs_root *root,
1951 u64 bytenr, u64 num_bytes, u64 parent,
1952 u64 root_objectid, u64 owner, u64 offset,
1956 struct btrfs_fs_info *fs_info = root->fs_info;
1958 BUG_ON(owner < BTRFS_FIRST_FREE_OBJECTID &&
1959 root_objectid == BTRFS_TREE_LOG_OBJECTID);
1961 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1962 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
1964 parent, root_objectid, (int)owner,
1965 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
1967 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
1969 parent, root_objectid, owner, offset,
1970 BTRFS_ADD_DELAYED_REF, NULL, no_quota);
1975 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1976 struct btrfs_root *root,
1977 u64 bytenr, u64 num_bytes,
1978 u64 parent, u64 root_objectid,
1979 u64 owner, u64 offset, int refs_to_add,
1981 struct btrfs_delayed_extent_op *extent_op)
1983 struct btrfs_fs_info *fs_info = root->fs_info;
1984 struct btrfs_path *path;
1985 struct extent_buffer *leaf;
1986 struct btrfs_extent_item *item;
1987 struct btrfs_key key;
1990 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_ADD_EXCL;
1992 path = btrfs_alloc_path();
1996 if (!is_fstree(root_objectid) || !root->fs_info->quota_enabled)
2000 path->leave_spinning = 1;
2001 /* this will setup the path even if it fails to insert the back ref */
2002 ret = insert_inline_extent_backref(trans, fs_info->extent_root, path,
2003 bytenr, num_bytes, parent,
2004 root_objectid, owner, offset,
2005 refs_to_add, extent_op);
2006 if ((ret < 0 && ret != -EAGAIN) || (!ret && no_quota))
2009 * Ok we were able to insert an inline extent and it appears to be a new
2010 * reference, deal with the qgroup accounting.
2012 if (!ret && !no_quota) {
2013 ASSERT(root->fs_info->quota_enabled);
2014 leaf = path->nodes[0];
2015 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2016 item = btrfs_item_ptr(leaf, path->slots[0],
2017 struct btrfs_extent_item);
2018 if (btrfs_extent_refs(leaf, item) > (u64)refs_to_add)
2019 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2020 btrfs_release_path(path);
2022 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2023 bytenr, num_bytes, type, 0);
2028 * Ok we had -EAGAIN which means we didn't have space to insert and
2029 * inline extent ref, so just update the reference count and add a
2032 leaf = path->nodes[0];
2033 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2034 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2035 refs = btrfs_extent_refs(leaf, item);
2037 type = BTRFS_QGROUP_OPER_ADD_SHARED;
2038 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
2040 __run_delayed_extent_op(extent_op, leaf, item);
2042 btrfs_mark_buffer_dirty(leaf);
2043 btrfs_release_path(path);
2046 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
2047 bytenr, num_bytes, type, 0);
2053 path->leave_spinning = 1;
2054 /* now insert the actual backref */
2055 ret = insert_extent_backref(trans, root->fs_info->extent_root,
2056 path, bytenr, parent, root_objectid,
2057 owner, offset, refs_to_add);
2059 btrfs_abort_transaction(trans, root, ret);
2061 btrfs_free_path(path);
2065 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
2066 struct btrfs_root *root,
2067 struct btrfs_delayed_ref_node *node,
2068 struct btrfs_delayed_extent_op *extent_op,
2069 int insert_reserved)
2072 struct btrfs_delayed_data_ref *ref;
2073 struct btrfs_key ins;
2078 ins.objectid = node->bytenr;
2079 ins.offset = node->num_bytes;
2080 ins.type = BTRFS_EXTENT_ITEM_KEY;
2082 ref = btrfs_delayed_node_to_data_ref(node);
2083 trace_run_delayed_data_ref(node, ref, node->action);
2085 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
2086 parent = ref->parent;
2087 ref_root = ref->root;
2089 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2091 flags |= extent_op->flags_to_set;
2092 ret = alloc_reserved_file_extent(trans, root,
2093 parent, ref_root, flags,
2094 ref->objectid, ref->offset,
2095 &ins, node->ref_mod);
2096 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2097 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2098 node->num_bytes, parent,
2099 ref_root, ref->objectid,
2100 ref->offset, node->ref_mod,
2101 node->no_quota, extent_op);
2102 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2103 ret = __btrfs_free_extent(trans, root, node->bytenr,
2104 node->num_bytes, parent,
2105 ref_root, ref->objectid,
2106 ref->offset, node->ref_mod,
2107 extent_op, node->no_quota);
2114 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
2115 struct extent_buffer *leaf,
2116 struct btrfs_extent_item *ei)
2118 u64 flags = btrfs_extent_flags(leaf, ei);
2119 if (extent_op->update_flags) {
2120 flags |= extent_op->flags_to_set;
2121 btrfs_set_extent_flags(leaf, ei, flags);
2124 if (extent_op->update_key) {
2125 struct btrfs_tree_block_info *bi;
2126 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
2127 bi = (struct btrfs_tree_block_info *)(ei + 1);
2128 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
2132 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
2133 struct btrfs_root *root,
2134 struct btrfs_delayed_ref_node *node,
2135 struct btrfs_delayed_extent_op *extent_op)
2137 struct btrfs_key key;
2138 struct btrfs_path *path;
2139 struct btrfs_extent_item *ei;
2140 struct extent_buffer *leaf;
2144 int metadata = !extent_op->is_data;
2149 if (metadata && !btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2152 path = btrfs_alloc_path();
2156 key.objectid = node->bytenr;
2159 key.type = BTRFS_METADATA_ITEM_KEY;
2160 key.offset = extent_op->level;
2162 key.type = BTRFS_EXTENT_ITEM_KEY;
2163 key.offset = node->num_bytes;
2168 path->leave_spinning = 1;
2169 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
2177 if (path->slots[0] > 0) {
2179 btrfs_item_key_to_cpu(path->nodes[0], &key,
2181 if (key.objectid == node->bytenr &&
2182 key.type == BTRFS_EXTENT_ITEM_KEY &&
2183 key.offset == node->num_bytes)
2187 btrfs_release_path(path);
2190 key.objectid = node->bytenr;
2191 key.offset = node->num_bytes;
2192 key.type = BTRFS_EXTENT_ITEM_KEY;
2201 leaf = path->nodes[0];
2202 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2203 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2204 if (item_size < sizeof(*ei)) {
2205 ret = convert_extent_item_v0(trans, root->fs_info->extent_root,
2211 leaf = path->nodes[0];
2212 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2215 BUG_ON(item_size < sizeof(*ei));
2216 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2217 __run_delayed_extent_op(extent_op, leaf, ei);
2219 btrfs_mark_buffer_dirty(leaf);
2221 btrfs_free_path(path);
2225 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
2226 struct btrfs_root *root,
2227 struct btrfs_delayed_ref_node *node,
2228 struct btrfs_delayed_extent_op *extent_op,
2229 int insert_reserved)
2232 struct btrfs_delayed_tree_ref *ref;
2233 struct btrfs_key ins;
2236 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
2239 ref = btrfs_delayed_node_to_tree_ref(node);
2240 trace_run_delayed_tree_ref(node, ref, node->action);
2242 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2243 parent = ref->parent;
2244 ref_root = ref->root;
2246 ins.objectid = node->bytenr;
2247 if (skinny_metadata) {
2248 ins.offset = ref->level;
2249 ins.type = BTRFS_METADATA_ITEM_KEY;
2251 ins.offset = node->num_bytes;
2252 ins.type = BTRFS_EXTENT_ITEM_KEY;
2255 BUG_ON(node->ref_mod != 1);
2256 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
2257 BUG_ON(!extent_op || !extent_op->update_flags);
2258 ret = alloc_reserved_tree_block(trans, root,
2260 extent_op->flags_to_set,
2264 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
2265 ret = __btrfs_inc_extent_ref(trans, root, node->bytenr,
2266 node->num_bytes, parent, ref_root,
2267 ref->level, 0, 1, node->no_quota,
2269 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
2270 ret = __btrfs_free_extent(trans, root, node->bytenr,
2271 node->num_bytes, parent, ref_root,
2272 ref->level, 0, 1, extent_op,
2280 /* helper function to actually process a single delayed ref entry */
2281 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
2282 struct btrfs_root *root,
2283 struct btrfs_delayed_ref_node *node,
2284 struct btrfs_delayed_extent_op *extent_op,
2285 int insert_reserved)
2289 if (trans->aborted) {
2290 if (insert_reserved)
2291 btrfs_pin_extent(root, node->bytenr,
2292 node->num_bytes, 1);
2296 if (btrfs_delayed_ref_is_head(node)) {
2297 struct btrfs_delayed_ref_head *head;
2299 * we've hit the end of the chain and we were supposed
2300 * to insert this extent into the tree. But, it got
2301 * deleted before we ever needed to insert it, so all
2302 * we have to do is clean up the accounting
2305 head = btrfs_delayed_node_to_head(node);
2306 trace_run_delayed_ref_head(node, head, node->action);
2308 if (insert_reserved) {
2309 btrfs_pin_extent(root, node->bytenr,
2310 node->num_bytes, 1);
2311 if (head->is_data) {
2312 ret = btrfs_del_csums(trans, root,
2320 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
2321 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
2322 ret = run_delayed_tree_ref(trans, root, node, extent_op,
2324 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
2325 node->type == BTRFS_SHARED_DATA_REF_KEY)
2326 ret = run_delayed_data_ref(trans, root, node, extent_op,
2333 static noinline struct btrfs_delayed_ref_node *
2334 select_delayed_ref(struct btrfs_delayed_ref_head *head)
2336 struct rb_node *node;
2337 struct btrfs_delayed_ref_node *ref, *last = NULL;;
2340 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
2341 * this prevents ref count from going down to zero when
2342 * there still are pending delayed ref.
2344 node = rb_first(&head->ref_root);
2346 ref = rb_entry(node, struct btrfs_delayed_ref_node,
2348 if (ref->action == BTRFS_ADD_DELAYED_REF)
2350 else if (last == NULL)
2352 node = rb_next(node);
2358 * Returns 0 on success or if called with an already aborted transaction.
2359 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2361 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2362 struct btrfs_root *root,
2365 struct btrfs_delayed_ref_root *delayed_refs;
2366 struct btrfs_delayed_ref_node *ref;
2367 struct btrfs_delayed_ref_head *locked_ref = NULL;
2368 struct btrfs_delayed_extent_op *extent_op;
2369 struct btrfs_fs_info *fs_info = root->fs_info;
2370 ktime_t start = ktime_get();
2372 unsigned long count = 0;
2373 unsigned long actual_count = 0;
2374 int must_insert_reserved = 0;
2376 delayed_refs = &trans->transaction->delayed_refs;
2382 spin_lock(&delayed_refs->lock);
2383 locked_ref = btrfs_select_ref_head(trans);
2385 spin_unlock(&delayed_refs->lock);
2389 /* grab the lock that says we are going to process
2390 * all the refs for this head */
2391 ret = btrfs_delayed_ref_lock(trans, locked_ref);
2392 spin_unlock(&delayed_refs->lock);
2394 * we may have dropped the spin lock to get the head
2395 * mutex lock, and that might have given someone else
2396 * time to free the head. If that's true, it has been
2397 * removed from our list and we can move on.
2399 if (ret == -EAGAIN) {
2407 * We need to try and merge add/drops of the same ref since we
2408 * can run into issues with relocate dropping the implicit ref
2409 * and then it being added back again before the drop can
2410 * finish. If we merged anything we need to re-loop so we can
2413 spin_lock(&locked_ref->lock);
2414 btrfs_merge_delayed_refs(trans, fs_info, delayed_refs,
2418 * locked_ref is the head node, so we have to go one
2419 * node back for any delayed ref updates
2421 ref = select_delayed_ref(locked_ref);
2423 if (ref && ref->seq &&
2424 btrfs_check_delayed_seq(fs_info, delayed_refs, ref->seq)) {
2425 spin_unlock(&locked_ref->lock);
2426 btrfs_delayed_ref_unlock(locked_ref);
2427 spin_lock(&delayed_refs->lock);
2428 locked_ref->processing = 0;
2429 delayed_refs->num_heads_ready++;
2430 spin_unlock(&delayed_refs->lock);
2438 * record the must insert reserved flag before we
2439 * drop the spin lock.
2441 must_insert_reserved = locked_ref->must_insert_reserved;
2442 locked_ref->must_insert_reserved = 0;
2444 extent_op = locked_ref->extent_op;
2445 locked_ref->extent_op = NULL;
2450 /* All delayed refs have been processed, Go ahead
2451 * and send the head node to run_one_delayed_ref,
2452 * so that any accounting fixes can happen
2454 ref = &locked_ref->node;
2456 if (extent_op && must_insert_reserved) {
2457 btrfs_free_delayed_extent_op(extent_op);
2462 spin_unlock(&locked_ref->lock);
2463 ret = run_delayed_extent_op(trans, root,
2465 btrfs_free_delayed_extent_op(extent_op);
2469 * Need to reset must_insert_reserved if
2470 * there was an error so the abort stuff
2471 * can cleanup the reserved space
2474 if (must_insert_reserved)
2475 locked_ref->must_insert_reserved = 1;
2476 locked_ref->processing = 0;
2477 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
2478 btrfs_delayed_ref_unlock(locked_ref);
2485 * Need to drop our head ref lock and re-aqcuire the
2486 * delayed ref lock and then re-check to make sure
2489 spin_unlock(&locked_ref->lock);
2490 spin_lock(&delayed_refs->lock);
2491 spin_lock(&locked_ref->lock);
2492 if (rb_first(&locked_ref->ref_root) ||
2493 locked_ref->extent_op) {
2494 spin_unlock(&locked_ref->lock);
2495 spin_unlock(&delayed_refs->lock);
2499 delayed_refs->num_heads--;
2500 rb_erase(&locked_ref->href_node,
2501 &delayed_refs->href_root);
2502 spin_unlock(&delayed_refs->lock);
2506 rb_erase(&ref->rb_node, &locked_ref->ref_root);
2508 atomic_dec(&delayed_refs->num_entries);
2510 if (!btrfs_delayed_ref_is_head(ref)) {
2512 * when we play the delayed ref, also correct the
2515 switch (ref->action) {
2516 case BTRFS_ADD_DELAYED_REF:
2517 case BTRFS_ADD_DELAYED_EXTENT:
2518 locked_ref->node.ref_mod -= ref->ref_mod;
2520 case BTRFS_DROP_DELAYED_REF:
2521 locked_ref->node.ref_mod += ref->ref_mod;
2527 spin_unlock(&locked_ref->lock);
2529 ret = run_one_delayed_ref(trans, root, ref, extent_op,
2530 must_insert_reserved);
2532 btrfs_free_delayed_extent_op(extent_op);
2534 locked_ref->processing = 0;
2535 btrfs_delayed_ref_unlock(locked_ref);
2536 btrfs_put_delayed_ref(ref);
2537 btrfs_debug(fs_info, "run_one_delayed_ref returned %d", ret);
2542 * If this node is a head, that means all the refs in this head
2543 * have been dealt with, and we will pick the next head to deal
2544 * with, so we must unlock the head and drop it from the cluster
2545 * list before we release it.
2547 if (btrfs_delayed_ref_is_head(ref)) {
2548 btrfs_delayed_ref_unlock(locked_ref);
2551 btrfs_put_delayed_ref(ref);
2557 * We don't want to include ref heads since we can have empty ref heads
2558 * and those will drastically skew our runtime down since we just do
2559 * accounting, no actual extent tree updates.
2561 if (actual_count > 0) {
2562 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2566 * We weigh the current average higher than our current runtime
2567 * to avoid large swings in the average.
2569 spin_lock(&delayed_refs->lock);
2570 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2571 avg = div64_u64(avg, 4);
2572 fs_info->avg_delayed_ref_runtime = avg;
2573 spin_unlock(&delayed_refs->lock);
2578 #ifdef SCRAMBLE_DELAYED_REFS
2580 * Normally delayed refs get processed in ascending bytenr order. This
2581 * correlates in most cases to the order added. To expose dependencies on this
2582 * order, we start to process the tree in the middle instead of the beginning
2584 static u64 find_middle(struct rb_root *root)
2586 struct rb_node *n = root->rb_node;
2587 struct btrfs_delayed_ref_node *entry;
2590 u64 first = 0, last = 0;
2594 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2595 first = entry->bytenr;
2599 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2600 last = entry->bytenr;
2605 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2606 WARN_ON(!entry->in_tree);
2608 middle = entry->bytenr;
2621 static inline u64 heads_to_leaves(struct btrfs_root *root, u64 heads)
2625 num_bytes = heads * (sizeof(struct btrfs_extent_item) +
2626 sizeof(struct btrfs_extent_inline_ref));
2627 if (!btrfs_fs_incompat(root->fs_info, SKINNY_METADATA))
2628 num_bytes += heads * sizeof(struct btrfs_tree_block_info);
2631 * We don't ever fill up leaves all the way so multiply by 2 just to be
2632 * closer to what we're really going to want to ouse.
2634 return div64_u64(num_bytes, BTRFS_LEAF_DATA_SIZE(root));
2637 int btrfs_check_space_for_delayed_refs(struct btrfs_trans_handle *trans,
2638 struct btrfs_root *root)
2640 struct btrfs_block_rsv *global_rsv;
2641 u64 num_heads = trans->transaction->delayed_refs.num_heads_ready;
2645 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
2646 num_heads = heads_to_leaves(root, num_heads);
2648 num_bytes += (num_heads - 1) * root->nodesize;
2650 global_rsv = &root->fs_info->global_block_rsv;
2653 * If we can't allocate any more chunks lets make sure we have _lots_ of
2654 * wiggle room since running delayed refs can create more delayed refs.
2656 if (global_rsv->space_info->full)
2659 spin_lock(&global_rsv->lock);
2660 if (global_rsv->reserved <= num_bytes)
2662 spin_unlock(&global_rsv->lock);
2666 int btrfs_should_throttle_delayed_refs(struct btrfs_trans_handle *trans,
2667 struct btrfs_root *root)
2669 struct btrfs_fs_info *fs_info = root->fs_info;
2671 atomic_read(&trans->transaction->delayed_refs.num_entries);
2676 avg_runtime = fs_info->avg_delayed_ref_runtime;
2677 val = num_entries * avg_runtime;
2678 if (num_entries * avg_runtime >= NSEC_PER_SEC)
2680 if (val >= NSEC_PER_SEC / 2)
2683 return btrfs_check_space_for_delayed_refs(trans, root);
2686 struct async_delayed_refs {
2687 struct btrfs_root *root;
2691 struct completion wait;
2692 struct btrfs_work work;
2695 static void delayed_ref_async_start(struct btrfs_work *work)
2697 struct async_delayed_refs *async;
2698 struct btrfs_trans_handle *trans;
2701 async = container_of(work, struct async_delayed_refs, work);
2703 trans = btrfs_join_transaction(async->root);
2704 if (IS_ERR(trans)) {
2705 async->error = PTR_ERR(trans);
2710 * trans->sync means that when we call end_transaciton, we won't
2711 * wait on delayed refs
2714 ret = btrfs_run_delayed_refs(trans, async->root, async->count);
2718 ret = btrfs_end_transaction(trans, async->root);
2719 if (ret && !async->error)
2723 complete(&async->wait);
2728 int btrfs_async_run_delayed_refs(struct btrfs_root *root,
2729 unsigned long count, int wait)
2731 struct async_delayed_refs *async;
2734 async = kmalloc(sizeof(*async), GFP_NOFS);
2738 async->root = root->fs_info->tree_root;
2739 async->count = count;
2745 init_completion(&async->wait);
2747 btrfs_init_work(&async->work, btrfs_extent_refs_helper,
2748 delayed_ref_async_start, NULL, NULL);
2750 btrfs_queue_work(root->fs_info->extent_workers, &async->work);
2753 wait_for_completion(&async->wait);
2762 * this starts processing the delayed reference count updates and
2763 * extent insertions we have queued up so far. count can be
2764 * 0, which means to process everything in the tree at the start
2765 * of the run (but not newly added entries), or it can be some target
2766 * number you'd like to process.
2768 * Returns 0 on success or if called with an aborted transaction
2769 * Returns <0 on error and aborts the transaction
2771 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2772 struct btrfs_root *root, unsigned long count)
2774 struct rb_node *node;
2775 struct btrfs_delayed_ref_root *delayed_refs;
2776 struct btrfs_delayed_ref_head *head;
2778 int run_all = count == (unsigned long)-1;
2781 /* We'll clean this up in btrfs_cleanup_transaction */
2785 if (root == root->fs_info->extent_root)
2786 root = root->fs_info->tree_root;
2788 delayed_refs = &trans->transaction->delayed_refs;
2790 count = atomic_read(&delayed_refs->num_entries) * 2;
2795 #ifdef SCRAMBLE_DELAYED_REFS
2796 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2798 ret = __btrfs_run_delayed_refs(trans, root, count);
2800 btrfs_abort_transaction(trans, root, ret);
2805 if (!list_empty(&trans->new_bgs))
2806 btrfs_create_pending_block_groups(trans, root);
2808 spin_lock(&delayed_refs->lock);
2809 node = rb_first(&delayed_refs->href_root);
2811 spin_unlock(&delayed_refs->lock);
2814 count = (unsigned long)-1;
2817 head = rb_entry(node, struct btrfs_delayed_ref_head,
2819 if (btrfs_delayed_ref_is_head(&head->node)) {
2820 struct btrfs_delayed_ref_node *ref;
2823 atomic_inc(&ref->refs);
2825 spin_unlock(&delayed_refs->lock);
2827 * Mutex was contended, block until it's
2828 * released and try again
2830 mutex_lock(&head->mutex);
2831 mutex_unlock(&head->mutex);
2833 btrfs_put_delayed_ref(ref);
2839 node = rb_next(node);
2841 spin_unlock(&delayed_refs->lock);
2846 ret = btrfs_delayed_qgroup_accounting(trans, root->fs_info);
2849 assert_qgroups_uptodate(trans);
2853 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2854 struct btrfs_root *root,
2855 u64 bytenr, u64 num_bytes, u64 flags,
2856 int level, int is_data)
2858 struct btrfs_delayed_extent_op *extent_op;
2861 extent_op = btrfs_alloc_delayed_extent_op();
2865 extent_op->flags_to_set = flags;
2866 extent_op->update_flags = 1;
2867 extent_op->update_key = 0;
2868 extent_op->is_data = is_data ? 1 : 0;
2869 extent_op->level = level;
2871 ret = btrfs_add_delayed_extent_op(root->fs_info, trans, bytenr,
2872 num_bytes, extent_op);
2874 btrfs_free_delayed_extent_op(extent_op);
2878 static noinline int check_delayed_ref(struct btrfs_trans_handle *trans,
2879 struct btrfs_root *root,
2880 struct btrfs_path *path,
2881 u64 objectid, u64 offset, u64 bytenr)
2883 struct btrfs_delayed_ref_head *head;
2884 struct btrfs_delayed_ref_node *ref;
2885 struct btrfs_delayed_data_ref *data_ref;
2886 struct btrfs_delayed_ref_root *delayed_refs;
2887 struct rb_node *node;
2890 delayed_refs = &trans->transaction->delayed_refs;
2891 spin_lock(&delayed_refs->lock);
2892 head = btrfs_find_delayed_ref_head(trans, bytenr);
2894 spin_unlock(&delayed_refs->lock);
2898 if (!mutex_trylock(&head->mutex)) {
2899 atomic_inc(&head->node.refs);
2900 spin_unlock(&delayed_refs->lock);
2902 btrfs_release_path(path);
2905 * Mutex was contended, block until it's released and let
2908 mutex_lock(&head->mutex);
2909 mutex_unlock(&head->mutex);
2910 btrfs_put_delayed_ref(&head->node);
2913 spin_unlock(&delayed_refs->lock);
2915 spin_lock(&head->lock);
2916 node = rb_first(&head->ref_root);
2918 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2919 node = rb_next(node);
2921 /* If it's a shared ref we know a cross reference exists */
2922 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2927 data_ref = btrfs_delayed_node_to_data_ref(ref);
2930 * If our ref doesn't match the one we're currently looking at
2931 * then we have a cross reference.
2933 if (data_ref->root != root->root_key.objectid ||
2934 data_ref->objectid != objectid ||
2935 data_ref->offset != offset) {
2940 spin_unlock(&head->lock);
2941 mutex_unlock(&head->mutex);
2945 static noinline int check_committed_ref(struct btrfs_trans_handle *trans,
2946 struct btrfs_root *root,
2947 struct btrfs_path *path,
2948 u64 objectid, u64 offset, u64 bytenr)
2950 struct btrfs_root *extent_root = root->fs_info->extent_root;
2951 struct extent_buffer *leaf;
2952 struct btrfs_extent_data_ref *ref;
2953 struct btrfs_extent_inline_ref *iref;
2954 struct btrfs_extent_item *ei;
2955 struct btrfs_key key;
2959 key.objectid = bytenr;
2960 key.offset = (u64)-1;
2961 key.type = BTRFS_EXTENT_ITEM_KEY;
2963 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2966 BUG_ON(ret == 0); /* Corruption */
2969 if (path->slots[0] == 0)
2973 leaf = path->nodes[0];
2974 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2976 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2980 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2981 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
2982 if (item_size < sizeof(*ei)) {
2983 WARN_ON(item_size != sizeof(struct btrfs_extent_item_v0));
2987 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2989 if (item_size != sizeof(*ei) +
2990 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2993 if (btrfs_extent_generation(leaf, ei) <=
2994 btrfs_root_last_snapshot(&root->root_item))
2997 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2998 if (btrfs_extent_inline_ref_type(leaf, iref) !=
2999 BTRFS_EXTENT_DATA_REF_KEY)
3002 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
3003 if (btrfs_extent_refs(leaf, ei) !=
3004 btrfs_extent_data_ref_count(leaf, ref) ||
3005 btrfs_extent_data_ref_root(leaf, ref) !=
3006 root->root_key.objectid ||
3007 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
3008 btrfs_extent_data_ref_offset(leaf, ref) != offset)
3016 int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
3017 struct btrfs_root *root,
3018 u64 objectid, u64 offset, u64 bytenr)
3020 struct btrfs_path *path;
3024 path = btrfs_alloc_path();
3029 ret = check_committed_ref(trans, root, path, objectid,
3031 if (ret && ret != -ENOENT)
3034 ret2 = check_delayed_ref(trans, root, path, objectid,
3036 } while (ret2 == -EAGAIN);
3038 if (ret2 && ret2 != -ENOENT) {
3043 if (ret != -ENOENT || ret2 != -ENOENT)
3046 btrfs_free_path(path);
3047 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
3052 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
3053 struct btrfs_root *root,
3054 struct extent_buffer *buf,
3055 int full_backref, int inc)
3062 struct btrfs_key key;
3063 struct btrfs_file_extent_item *fi;
3067 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
3068 u64, u64, u64, u64, u64, u64, int);
3071 if (btrfs_test_is_dummy_root(root))
3074 ref_root = btrfs_header_owner(buf);
3075 nritems = btrfs_header_nritems(buf);
3076 level = btrfs_header_level(buf);
3078 if (!test_bit(BTRFS_ROOT_REF_COWS, &root->state) && level == 0)
3082 process_func = btrfs_inc_extent_ref;
3084 process_func = btrfs_free_extent;
3087 parent = buf->start;
3091 for (i = 0; i < nritems; i++) {
3093 btrfs_item_key_to_cpu(buf, &key, i);
3094 if (key.type != BTRFS_EXTENT_DATA_KEY)
3096 fi = btrfs_item_ptr(buf, i,
3097 struct btrfs_file_extent_item);
3098 if (btrfs_file_extent_type(buf, fi) ==
3099 BTRFS_FILE_EXTENT_INLINE)
3101 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
3105 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
3106 key.offset -= btrfs_file_extent_offset(buf, fi);
3107 ret = process_func(trans, root, bytenr, num_bytes,
3108 parent, ref_root, key.objectid,
3113 bytenr = btrfs_node_blockptr(buf, i);
3114 num_bytes = root->nodesize;
3115 ret = process_func(trans, root, bytenr, num_bytes,
3116 parent, ref_root, level - 1, 0,
3127 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3128 struct extent_buffer *buf, int full_backref)
3130 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
3133 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
3134 struct extent_buffer *buf, int full_backref)
3136 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
3139 static int write_one_cache_group(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root,
3141 struct btrfs_path *path,
3142 struct btrfs_block_group_cache *cache)
3145 struct btrfs_root *extent_root = root->fs_info->extent_root;
3147 struct extent_buffer *leaf;
3149 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
3152 BUG_ON(ret); /* Corruption */
3154 leaf = path->nodes[0];
3155 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
3156 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
3157 btrfs_mark_buffer_dirty(leaf);
3158 btrfs_release_path(path);
3161 btrfs_abort_transaction(trans, root, ret);
3168 static struct btrfs_block_group_cache *
3169 next_block_group(struct btrfs_root *root,
3170 struct btrfs_block_group_cache *cache)
3172 struct rb_node *node;
3173 spin_lock(&root->fs_info->block_group_cache_lock);
3174 node = rb_next(&cache->cache_node);
3175 btrfs_put_block_group(cache);
3177 cache = rb_entry(node, struct btrfs_block_group_cache,
3179 btrfs_get_block_group(cache);
3182 spin_unlock(&root->fs_info->block_group_cache_lock);
3186 static int cache_save_setup(struct btrfs_block_group_cache *block_group,
3187 struct btrfs_trans_handle *trans,
3188 struct btrfs_path *path)
3190 struct btrfs_root *root = block_group->fs_info->tree_root;
3191 struct inode *inode = NULL;
3193 int dcs = BTRFS_DC_ERROR;
3199 * If this block group is smaller than 100 megs don't bother caching the
3202 if (block_group->key.offset < (100 * 1024 * 1024)) {
3203 spin_lock(&block_group->lock);
3204 block_group->disk_cache_state = BTRFS_DC_WRITTEN;
3205 spin_unlock(&block_group->lock);
3210 inode = lookup_free_space_inode(root, block_group, path);
3211 if (IS_ERR(inode) && PTR_ERR(inode) != -ENOENT) {
3212 ret = PTR_ERR(inode);
3213 btrfs_release_path(path);
3217 if (IS_ERR(inode)) {
3221 if (block_group->ro)
3224 ret = create_free_space_inode(root, trans, block_group, path);
3230 /* We've already setup this transaction, go ahead and exit */
3231 if (block_group->cache_generation == trans->transid &&
3232 i_size_read(inode)) {
3233 dcs = BTRFS_DC_SETUP;
3238 * We want to set the generation to 0, that way if anything goes wrong
3239 * from here on out we know not to trust this cache when we load up next
3242 BTRFS_I(inode)->generation = 0;
3243 ret = btrfs_update_inode(trans, root, inode);
3246 if (i_size_read(inode) > 0) {
3247 ret = btrfs_check_trunc_cache_free_space(root,
3248 &root->fs_info->global_block_rsv);
3252 ret = btrfs_truncate_free_space_cache(root, trans, inode);
3257 spin_lock(&block_group->lock);
3258 if (block_group->cached != BTRFS_CACHE_FINISHED ||
3259 !btrfs_test_opt(root, SPACE_CACHE) ||
3260 block_group->delalloc_bytes) {
3262 * don't bother trying to write stuff out _if_
3263 * a) we're not cached,
3264 * b) we're with nospace_cache mount option.
3266 dcs = BTRFS_DC_WRITTEN;
3267 spin_unlock(&block_group->lock);
3270 spin_unlock(&block_group->lock);
3273 * Try to preallocate enough space based on how big the block group is.
3274 * Keep in mind this has to include any pinned space which could end up
3275 * taking up quite a bit since it's not folded into the other space
3278 num_pages = (int)div64_u64(block_group->key.offset, 256 * 1024 * 1024);
3283 num_pages *= PAGE_CACHE_SIZE;
3285 ret = btrfs_check_data_free_space(inode, num_pages);
3289 ret = btrfs_prealloc_file_range_trans(inode, trans, 0, 0, num_pages,
3290 num_pages, num_pages,
3293 dcs = BTRFS_DC_SETUP;
3294 btrfs_free_reserved_data_space(inode, num_pages);
3299 btrfs_release_path(path);
3301 spin_lock(&block_group->lock);
3302 if (!ret && dcs == BTRFS_DC_SETUP)
3303 block_group->cache_generation = trans->transid;
3304 block_group->disk_cache_state = dcs;
3305 spin_unlock(&block_group->lock);
3310 int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
3311 struct btrfs_root *root)
3313 struct btrfs_block_group_cache *cache;
3315 struct btrfs_path *path;
3318 path = btrfs_alloc_path();
3324 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3326 if (cache->disk_cache_state == BTRFS_DC_CLEAR)
3328 cache = next_block_group(root, cache);
3336 err = cache_save_setup(cache, trans, path);
3337 last = cache->key.objectid + cache->key.offset;
3338 btrfs_put_block_group(cache);
3343 err = btrfs_run_delayed_refs(trans, root,
3345 if (err) /* File system offline */
3349 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3351 if (cache->disk_cache_state == BTRFS_DC_CLEAR) {
3352 btrfs_put_block_group(cache);
3358 cache = next_block_group(root, cache);
3367 if (cache->disk_cache_state == BTRFS_DC_SETUP)
3368 cache->disk_cache_state = BTRFS_DC_NEED_WRITE;
3370 last = cache->key.objectid + cache->key.offset;
3372 err = write_one_cache_group(trans, root, path, cache);
3373 btrfs_put_block_group(cache);
3374 if (err) /* File system offline */
3380 * I don't think this is needed since we're just marking our
3381 * preallocated extent as written, but just in case it can't
3385 err = btrfs_run_delayed_refs(trans, root,
3387 if (err) /* File system offline */
3391 cache = btrfs_lookup_first_block_group(root->fs_info, last);
3394 * Really this shouldn't happen, but it could if we
3395 * couldn't write the entire preallocated extent and
3396 * splitting the extent resulted in a new block.
3399 btrfs_put_block_group(cache);
3402 if (cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3404 cache = next_block_group(root, cache);
3413 err = btrfs_write_out_cache(root, trans, cache, path);
3416 * If we didn't have an error then the cache state is still
3417 * NEED_WRITE, so we can set it to WRITTEN.
3419 if (!err && cache->disk_cache_state == BTRFS_DC_NEED_WRITE)
3420 cache->disk_cache_state = BTRFS_DC_WRITTEN;
3421 last = cache->key.objectid + cache->key.offset;
3422 btrfs_put_block_group(cache);
3426 btrfs_free_path(path);
3430 int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
3432 struct btrfs_block_group_cache *block_group;
3435 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
3436 if (!block_group || block_group->ro)
3439 btrfs_put_block_group(block_group);
3443 static const char *alloc_name(u64 flags)
3446 case BTRFS_BLOCK_GROUP_METADATA|BTRFS_BLOCK_GROUP_DATA:
3448 case BTRFS_BLOCK_GROUP_METADATA:
3450 case BTRFS_BLOCK_GROUP_DATA:
3452 case BTRFS_BLOCK_GROUP_SYSTEM:
3456 return "invalid-combination";
3460 static int update_space_info(struct btrfs_fs_info *info, u64 flags,
3461 u64 total_bytes, u64 bytes_used,
3462 struct btrfs_space_info **space_info)
3464 struct btrfs_space_info *found;
3469 if (flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
3470 BTRFS_BLOCK_GROUP_RAID10))
3475 found = __find_space_info(info, flags);
3477 spin_lock(&found->lock);
3478 found->total_bytes += total_bytes;
3479 found->disk_total += total_bytes * factor;
3480 found->bytes_used += bytes_used;
3481 found->disk_used += bytes_used * factor;
3483 spin_unlock(&found->lock);
3484 *space_info = found;
3487 found = kzalloc(sizeof(*found), GFP_NOFS);
3491 ret = percpu_counter_init(&found->total_bytes_pinned, 0);
3497 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
3498 INIT_LIST_HEAD(&found->block_groups[i]);
3499 init_rwsem(&found->groups_sem);
3500 spin_lock_init(&found->lock);
3501 found->flags = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
3502 found->total_bytes = total_bytes;
3503 found->disk_total = total_bytes * factor;
3504 found->bytes_used = bytes_used;
3505 found->disk_used = bytes_used * factor;
3506 found->bytes_pinned = 0;
3507 found->bytes_reserved = 0;
3508 found->bytes_readonly = 0;
3509 found->bytes_may_use = 0;
3511 found->force_alloc = CHUNK_ALLOC_NO_FORCE;
3512 found->chunk_alloc = 0;
3514 init_waitqueue_head(&found->wait);
3516 ret = kobject_init_and_add(&found->kobj, &space_info_ktype,
3517 info->space_info_kobj, "%s",
3518 alloc_name(found->flags));
3524 *space_info = found;
3525 list_add_rcu(&found->list, &info->space_info);
3526 if (flags & BTRFS_BLOCK_GROUP_DATA)
3527 info->data_sinfo = found;
3532 static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
3534 u64 extra_flags = chunk_to_extended(flags) &
3535 BTRFS_EXTENDED_PROFILE_MASK;
3537 write_seqlock(&fs_info->profiles_lock);
3538 if (flags & BTRFS_BLOCK_GROUP_DATA)
3539 fs_info->avail_data_alloc_bits |= extra_flags;
3540 if (flags & BTRFS_BLOCK_GROUP_METADATA)
3541 fs_info->avail_metadata_alloc_bits |= extra_flags;
3542 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3543 fs_info->avail_system_alloc_bits |= extra_flags;
3544 write_sequnlock(&fs_info->profiles_lock);
3548 * returns target flags in extended format or 0 if restripe for this
3549 * chunk_type is not in progress
3551 * should be called with either volume_mutex or balance_lock held
3553 static u64 get_restripe_target(struct btrfs_fs_info *fs_info, u64 flags)
3555 struct btrfs_balance_control *bctl = fs_info->balance_ctl;
3561 if (flags & BTRFS_BLOCK_GROUP_DATA &&
3562 bctl->data.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3563 target = BTRFS_BLOCK_GROUP_DATA | bctl->data.target;
3564 } else if (flags & BTRFS_BLOCK_GROUP_SYSTEM &&
3565 bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3566 target = BTRFS_BLOCK_GROUP_SYSTEM | bctl->sys.target;
3567 } else if (flags & BTRFS_BLOCK_GROUP_METADATA &&
3568 bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) {
3569 target = BTRFS_BLOCK_GROUP_METADATA | bctl->meta.target;
3576 * @flags: available profiles in extended format (see ctree.h)
3578 * Returns reduced profile in chunk format. If profile changing is in
3579 * progress (either running or paused) picks the target profile (if it's
3580 * already available), otherwise falls back to plain reducing.
3582 static u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
3584 u64 num_devices = root->fs_info->fs_devices->rw_devices;
3589 * see if restripe for this chunk_type is in progress, if so
3590 * try to reduce to the target profile
3592 spin_lock(&root->fs_info->balance_lock);
3593 target = get_restripe_target(root->fs_info, flags);
3595 /* pick target profile only if it's already available */
3596 if ((flags & target) & BTRFS_EXTENDED_PROFILE_MASK) {
3597 spin_unlock(&root->fs_info->balance_lock);
3598 return extended_to_chunk(target);
3601 spin_unlock(&root->fs_info->balance_lock);
3603 /* First, mask out the RAID levels which aren't possible */
3604 if (num_devices == 1)
3605 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0 |
3606 BTRFS_BLOCK_GROUP_RAID5);
3607 if (num_devices < 3)
3608 flags &= ~BTRFS_BLOCK_GROUP_RAID6;
3609 if (num_devices < 4)
3610 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
3612 tmp = flags & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID0 |
3613 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID5 |
3614 BTRFS_BLOCK_GROUP_RAID6 | BTRFS_BLOCK_GROUP_RAID10);
3617 if (tmp & BTRFS_BLOCK_GROUP_RAID6)
3618 tmp = BTRFS_BLOCK_GROUP_RAID6;
3619 else if (tmp & BTRFS_BLOCK_GROUP_RAID5)
3620 tmp = BTRFS_BLOCK_GROUP_RAID5;
3621 else if (tmp & BTRFS_BLOCK_GROUP_RAID10)
3622 tmp = BTRFS_BLOCK_GROUP_RAID10;
3623 else if (tmp & BTRFS_BLOCK_GROUP_RAID1)
3624 tmp = BTRFS_BLOCK_GROUP_RAID1;
3625 else if (tmp & BTRFS_BLOCK_GROUP_RAID0)
3626 tmp = BTRFS_BLOCK_GROUP_RAID0;
3628 return extended_to_chunk(flags | tmp);
3631 static u64 get_alloc_profile(struct btrfs_root *root, u64 orig_flags)
3638 seq = read_seqbegin(&root->fs_info->profiles_lock);
3640 if (flags & BTRFS_BLOCK_GROUP_DATA)
3641 flags |= root->fs_info->avail_data_alloc_bits;
3642 else if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
3643 flags |= root->fs_info->avail_system_alloc_bits;
3644 else if (flags & BTRFS_BLOCK_GROUP_METADATA)
3645 flags |= root->fs_info->avail_metadata_alloc_bits;
3646 } while (read_seqretry(&root->fs_info->profiles_lock, seq));
3648 return btrfs_reduce_alloc_profile(root, flags);
3651 u64 btrfs_get_alloc_profile(struct btrfs_root *root, int data)
3657 flags = BTRFS_BLOCK_GROUP_DATA;
3658 else if (root == root->fs_info->chunk_root)
3659 flags = BTRFS_BLOCK_GROUP_SYSTEM;
3661 flags = BTRFS_BLOCK_GROUP_METADATA;
3663 ret = get_alloc_profile(root, flags);
3668 * This will check the space that the inode allocates from to make sure we have
3669 * enough space for bytes.
3671 int btrfs_check_data_free_space(struct inode *inode, u64 bytes)
3673 struct btrfs_space_info *data_sinfo;
3674 struct btrfs_root *root = BTRFS_I(inode)->root;
3675 struct btrfs_fs_info *fs_info = root->fs_info;
3677 int ret = 0, committed = 0, alloc_chunk = 1;
3679 /* make sure bytes are sectorsize aligned */
3680 bytes = ALIGN(bytes, root->sectorsize);
3682 if (btrfs_is_free_space_inode(inode)) {
3684 ASSERT(current->journal_info);
3687 data_sinfo = fs_info->data_sinfo;
3692 /* make sure we have enough space to handle the data first */
3693 spin_lock(&data_sinfo->lock);
3694 used = data_sinfo->bytes_used + data_sinfo->bytes_reserved +
3695 data_sinfo->bytes_pinned + data_sinfo->bytes_readonly +
3696 data_sinfo->bytes_may_use;
3698 if (used + bytes > data_sinfo->total_bytes) {
3699 struct btrfs_trans_handle *trans;
3702 * if we don't have enough free bytes in this space then we need
3703 * to alloc a new chunk.
3705 if (!data_sinfo->full && alloc_chunk) {
3708 data_sinfo->force_alloc = CHUNK_ALLOC_FORCE;
3709 spin_unlock(&data_sinfo->lock);
3711 alloc_target = btrfs_get_alloc_profile(root, 1);
3713 * It is ugly that we don't call nolock join
3714 * transaction for the free space inode case here.
3715 * But it is safe because we only do the data space
3716 * reservation for the free space cache in the
3717 * transaction context, the common join transaction
3718 * just increase the counter of the current transaction
3719 * handler, doesn't try to acquire the trans_lock of
3722 trans = btrfs_join_transaction(root);
3724 return PTR_ERR(trans);
3726 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
3728 CHUNK_ALLOC_NO_FORCE);
3729 btrfs_end_transaction(trans, root);
3738 data_sinfo = fs_info->data_sinfo;
3744 * If we don't have enough pinned space to deal with this
3745 * allocation don't bother committing the transaction.
3747 if (percpu_counter_compare(&data_sinfo->total_bytes_pinned,
3750 spin_unlock(&data_sinfo->lock);
3752 /* commit the current transaction and try again */
3755 !atomic_read(&root->fs_info->open_ioctl_trans)) {
3758 trans = btrfs_join_transaction(root);
3760 return PTR_ERR(trans);
3761 ret = btrfs_commit_transaction(trans, root);
3767 trace_btrfs_space_reservation(root->fs_info,
3768 "space_info:enospc",
3769 data_sinfo->flags, bytes, 1);
3772 data_sinfo->bytes_may_use += bytes;
3773 trace_btrfs_space_reservation(root->fs_info, "space_info",
3774 data_sinfo->flags, bytes, 1);
3775 spin_unlock(&data_sinfo->lock);
3781 * Called if we need to clear a data reservation for this inode.
3783 void btrfs_free_reserved_data_space(struct inode *inode, u64 bytes)
3785 struct btrfs_root *root = BTRFS_I(inode)->root;
3786 struct btrfs_space_info *data_sinfo;
3788 /* make sure bytes are sectorsize aligned */
3789 bytes = ALIGN(bytes, root->sectorsize);
3791 data_sinfo = root->fs_info->data_sinfo;
3792 spin_lock(&data_sinfo->lock);
3793 WARN_ON(data_sinfo->bytes_may_use < bytes);
3794 data_sinfo->bytes_may_use -= bytes;
3795 trace_btrfs_space_reservation(root->fs_info, "space_info",
3796 data_sinfo->flags, bytes, 0);
3797 spin_unlock(&data_sinfo->lock);
3800 static void force_metadata_allocation(struct btrfs_fs_info *info)
3802 struct list_head *head = &info->space_info;
3803 struct btrfs_space_info *found;
3806 list_for_each_entry_rcu(found, head, list) {
3807 if (found->flags & BTRFS_BLOCK_GROUP_METADATA)
3808 found->force_alloc = CHUNK_ALLOC_FORCE;
3813 static inline u64 calc_global_rsv_need_space(struct btrfs_block_rsv *global)
3815 return (global->size << 1);
3818 static int should_alloc_chunk(struct btrfs_root *root,
3819 struct btrfs_space_info *sinfo, int force)
3821 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
3822 u64 num_bytes = sinfo->total_bytes - sinfo->bytes_readonly;
3823 u64 num_allocated = sinfo->bytes_used + sinfo->bytes_reserved;
3826 if (force == CHUNK_ALLOC_FORCE)
3830 * We need to take into account the global rsv because for all intents
3831 * and purposes it's used space. Don't worry about locking the
3832 * global_rsv, it doesn't change except when the transaction commits.
3834 if (sinfo->flags & BTRFS_BLOCK_GROUP_METADATA)
3835 num_allocated += calc_global_rsv_need_space(global_rsv);
3838 * in limited mode, we want to have some free space up to
3839 * about 1% of the FS size.
3841 if (force == CHUNK_ALLOC_LIMITED) {
3842 thresh = btrfs_super_total_bytes(root->fs_info->super_copy);
3843 thresh = max_t(u64, 64 * 1024 * 1024,
3844 div_factor_fine(thresh, 1));
3846 if (num_bytes - num_allocated < thresh)
3850 if (num_allocated + 2 * 1024 * 1024 < div_factor(num_bytes, 8))
3855 static u64 get_system_chunk_thresh(struct btrfs_root *root, u64 type)
3859 if (type & (BTRFS_BLOCK_GROUP_RAID10 |
3860 BTRFS_BLOCK_GROUP_RAID0 |
3861 BTRFS_BLOCK_GROUP_RAID5 |
3862 BTRFS_BLOCK_GROUP_RAID6))
3863 num_dev = root->fs_info->fs_devices->rw_devices;
3864 else if (type & BTRFS_BLOCK_GROUP_RAID1)
3867 num_dev = 1; /* DUP or single */
3869 /* metadata for updaing devices and chunk tree */
3870 return btrfs_calc_trans_metadata_size(root, num_dev + 1);
3873 static void check_system_chunk(struct btrfs_trans_handle *trans,
3874 struct btrfs_root *root, u64 type)
3876 struct btrfs_space_info *info;
3880 info = __find_space_info(root->fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
3881 spin_lock(&info->lock);
3882 left = info->total_bytes - info->bytes_used - info->bytes_pinned -
3883 info->bytes_reserved - info->bytes_readonly;
3884 spin_unlock(&info->lock);
3886 thresh = get_system_chunk_thresh(root, type);
3887 if (left < thresh && btrfs_test_opt(root, ENOSPC_DEBUG)) {
3888 btrfs_info(root->fs_info, "left=%llu, need=%llu, flags=%llu",
3889 left, thresh, type);
3890 dump_space_info(info, 0, 0);
3893 if (left < thresh) {
3896 flags = btrfs_get_alloc_profile(root->fs_info->chunk_root, 0);
3897 btrfs_alloc_chunk(trans, root, flags);
3901 static int do_chunk_alloc(struct btrfs_trans_handle *trans,
3902 struct btrfs_root *extent_root, u64 flags, int force)
3904 struct btrfs_space_info *space_info;
3905 struct btrfs_fs_info *fs_info = extent_root->fs_info;
3906 int wait_for_alloc = 0;
3909 /* Don't re-enter if we're already allocating a chunk */
3910 if (trans->allocating_chunk)
3913 space_info = __find_space_info(extent_root->fs_info, flags);
3915 ret = update_space_info(extent_root->fs_info, flags,
3917 BUG_ON(ret); /* -ENOMEM */
3919 BUG_ON(!space_info); /* Logic error */
3922 spin_lock(&space_info->lock);
3923 if (force < space_info->force_alloc)
3924 force = space_info->force_alloc;
3925 if (space_info->full) {
3926 if (should_alloc_chunk(extent_root, space_info, force))
3930 spin_unlock(&space_info->lock);
3934 if (!should_alloc_chunk(extent_root, space_info, force)) {
3935 spin_unlock(&space_info->lock);
3937 } else if (space_info->chunk_alloc) {
3940 space_info->chunk_alloc = 1;
3943 spin_unlock(&space_info->lock);
3945 mutex_lock(&fs_info->chunk_mutex);
3948 * The chunk_mutex is held throughout the entirety of a chunk
3949 * allocation, so once we've acquired the chunk_mutex we know that the
3950 * other guy is done and we need to recheck and see if we should
3953 if (wait_for_alloc) {
3954 mutex_unlock(&fs_info->chunk_mutex);
3959 trans->allocating_chunk = true;
3962 * If we have mixed data/metadata chunks we want to make sure we keep
3963 * allocating mixed chunks instead of individual chunks.
3965 if (btrfs_mixed_space_info(space_info))
3966 flags |= (BTRFS_BLOCK_GROUP_DATA | BTRFS_BLOCK_GROUP_METADATA);
3969 * if we're doing a data chunk, go ahead and make sure that
3970 * we keep a reasonable number of metadata chunks allocated in the
3973 if (flags & BTRFS_BLOCK_GROUP_DATA && fs_info->metadata_ratio) {
3974 fs_info->data_chunk_allocations++;
3975 if (!(fs_info->data_chunk_allocations %
3976 fs_info->metadata_ratio))
3977 force_metadata_allocation(fs_info);
3981 * Check if we have enough space in SYSTEM chunk because we may need
3982 * to update devices.
3984 check_system_chunk(trans, extent_root, flags);
3986 ret = btrfs_alloc_chunk(trans, extent_root, flags);
3987 trans->allocating_chunk = false;
3989 spin_lock(&space_info->lock);
3990 if (ret < 0 && ret != -ENOSPC)
3993 space_info->full = 1;
3997 space_info->force_alloc = CHUNK_ALLOC_NO_FORCE;
3999 space_info->chunk_alloc = 0;
4000 spin_unlock(&space_info->lock);
4001 mutex_unlock(&fs_info->chunk_mutex);
4005 static int can_overcommit(struct btrfs_root *root,
4006 struct btrfs_space_info *space_info, u64 bytes,
4007 enum btrfs_reserve_flush_enum flush)
4009 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4010 u64 profile = btrfs_get_alloc_profile(root, 0);
4015 used = space_info->bytes_used + space_info->bytes_reserved +
4016 space_info->bytes_pinned + space_info->bytes_readonly;
4019 * We only want to allow over committing if we have lots of actual space
4020 * free, but if we don't have enough space to handle the global reserve
4021 * space then we could end up having a real enospc problem when trying
4022 * to allocate a chunk or some other such important allocation.
4024 spin_lock(&global_rsv->lock);
4025 space_size = calc_global_rsv_need_space(global_rsv);
4026 spin_unlock(&global_rsv->lock);
4027 if (used + space_size >= space_info->total_bytes)
4030 used += space_info->bytes_may_use;
4032 spin_lock(&root->fs_info->free_chunk_lock);
4033 avail = root->fs_info->free_chunk_space;
4034 spin_unlock(&root->fs_info->free_chunk_lock);
4037 * If we have dup, raid1 or raid10 then only half of the free
4038 * space is actually useable. For raid56, the space info used
4039 * doesn't include the parity drive, so we don't have to
4042 if (profile & (BTRFS_BLOCK_GROUP_DUP |
4043 BTRFS_BLOCK_GROUP_RAID1 |
4044 BTRFS_BLOCK_GROUP_RAID10))
4048 * If we aren't flushing all things, let us overcommit up to
4049 * 1/2th of the space. If we can flush, don't let us overcommit
4050 * too much, let it overcommit up to 1/8 of the space.
4052 if (flush == BTRFS_RESERVE_FLUSH_ALL)
4057 if (used + bytes < space_info->total_bytes + avail)
4062 static void btrfs_writeback_inodes_sb_nr(struct btrfs_root *root,
4063 unsigned long nr_pages, int nr_items)
4065 struct super_block *sb = root->fs_info->sb;
4067 if (down_read_trylock(&sb->s_umount)) {
4068 writeback_inodes_sb_nr(sb, nr_pages, WB_REASON_FS_FREE_SPACE);
4069 up_read(&sb->s_umount);
4072 * We needn't worry the filesystem going from r/w to r/o though
4073 * we don't acquire ->s_umount mutex, because the filesystem
4074 * should guarantee the delalloc inodes list be empty after
4075 * the filesystem is readonly(all dirty pages are written to
4078 btrfs_start_delalloc_roots(root->fs_info, 0, nr_items);
4079 if (!current->journal_info)
4080 btrfs_wait_ordered_roots(root->fs_info, nr_items);
4084 static inline int calc_reclaim_items_nr(struct btrfs_root *root, u64 to_reclaim)
4089 bytes = btrfs_calc_trans_metadata_size(root, 1);
4090 nr = (int)div64_u64(to_reclaim, bytes);
4096 #define EXTENT_SIZE_PER_ITEM (256 * 1024)
4099 * shrink metadata reservation for delalloc
4101 static void shrink_delalloc(struct btrfs_root *root, u64 to_reclaim, u64 orig,
4104 struct btrfs_block_rsv *block_rsv;
4105 struct btrfs_space_info *space_info;
4106 struct btrfs_trans_handle *trans;
4110 unsigned long nr_pages;
4113 enum btrfs_reserve_flush_enum flush;
4115 /* Calc the number of the pages we need flush for space reservation */
4116 items = calc_reclaim_items_nr(root, to_reclaim);
4117 to_reclaim = items * EXTENT_SIZE_PER_ITEM;
4119 trans = (struct btrfs_trans_handle *)current->journal_info;
4120 block_rsv = &root->fs_info->delalloc_block_rsv;
4121 space_info = block_rsv->space_info;
4123 delalloc_bytes = percpu_counter_sum_positive(
4124 &root->fs_info->delalloc_bytes);
4125 if (delalloc_bytes == 0) {
4129 btrfs_wait_ordered_roots(root->fs_info, items);
4134 while (delalloc_bytes && loops < 3) {
4135 max_reclaim = min(delalloc_bytes, to_reclaim);
4136 nr_pages = max_reclaim >> PAGE_CACHE_SHIFT;
4137 btrfs_writeback_inodes_sb_nr(root, nr_pages, items);
4139 * We need to wait for the async pages to actually start before
4142 max_reclaim = atomic_read(&root->fs_info->async_delalloc_pages);
4146 if (max_reclaim <= nr_pages)
4149 max_reclaim -= nr_pages;
4151 wait_event(root->fs_info->async_submit_wait,
4152 atomic_read(&root->fs_info->async_delalloc_pages) <=
4156 flush = BTRFS_RESERVE_FLUSH_ALL;
4158 flush = BTRFS_RESERVE_NO_FLUSH;
4159 spin_lock(&space_info->lock);
4160 if (can_overcommit(root, space_info, orig, flush)) {
4161 spin_unlock(&space_info->lock);
4164 spin_unlock(&space_info->lock);
4167 if (wait_ordered && !trans) {
4168 btrfs_wait_ordered_roots(root->fs_info, items);
4170 time_left = schedule_timeout_killable(1);
4174 delalloc_bytes = percpu_counter_sum_positive(
4175 &root->fs_info->delalloc_bytes);
4180 * maybe_commit_transaction - possibly commit the transaction if its ok to
4181 * @root - the root we're allocating for
4182 * @bytes - the number of bytes we want to reserve
4183 * @force - force the commit
4185 * This will check to make sure that committing the transaction will actually
4186 * get us somewhere and then commit the transaction if it does. Otherwise it
4187 * will return -ENOSPC.
4189 static int may_commit_transaction(struct btrfs_root *root,
4190 struct btrfs_space_info *space_info,
4191 u64 bytes, int force)
4193 struct btrfs_block_rsv *delayed_rsv = &root->fs_info->delayed_block_rsv;
4194 struct btrfs_trans_handle *trans;
4196 trans = (struct btrfs_trans_handle *)current->journal_info;
4203 /* See if there is enough pinned space to make this reservation */
4204 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4209 * See if there is some space in the delayed insertion reservation for
4212 if (space_info != delayed_rsv->space_info)
4215 spin_lock(&delayed_rsv->lock);
4216 if (percpu_counter_compare(&space_info->total_bytes_pinned,
4217 bytes - delayed_rsv->size) >= 0) {
4218 spin_unlock(&delayed_rsv->lock);
4221 spin_unlock(&delayed_rsv->lock);
4224 trans = btrfs_join_transaction(root);
4228 return btrfs_commit_transaction(trans, root);
4232 FLUSH_DELAYED_ITEMS_NR = 1,
4233 FLUSH_DELAYED_ITEMS = 2,
4235 FLUSH_DELALLOC_WAIT = 4,
4240 static int flush_space(struct btrfs_root *root,
4241 struct btrfs_space_info *space_info, u64 num_bytes,
4242 u64 orig_bytes, int state)
4244 struct btrfs_trans_handle *trans;
4249 case FLUSH_DELAYED_ITEMS_NR:
4250 case FLUSH_DELAYED_ITEMS:
4251 if (state == FLUSH_DELAYED_ITEMS_NR)
4252 nr = calc_reclaim_items_nr(root, num_bytes) * 2;
4256 trans = btrfs_join_transaction(root);
4257 if (IS_ERR(trans)) {
4258 ret = PTR_ERR(trans);
4261 ret = btrfs_run_delayed_items_nr(trans, root, nr);
4262 btrfs_end_transaction(trans, root);
4264 case FLUSH_DELALLOC:
4265 case FLUSH_DELALLOC_WAIT:
4266 shrink_delalloc(root, num_bytes * 2, orig_bytes,
4267 state == FLUSH_DELALLOC_WAIT);
4270 trans = btrfs_join_transaction(root);
4271 if (IS_ERR(trans)) {
4272 ret = PTR_ERR(trans);
4275 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
4276 btrfs_get_alloc_profile(root, 0),
4277 CHUNK_ALLOC_NO_FORCE);
4278 btrfs_end_transaction(trans, root);
4283 ret = may_commit_transaction(root, space_info, orig_bytes, 0);
4294 btrfs_calc_reclaim_metadata_size(struct btrfs_root *root,
4295 struct btrfs_space_info *space_info)
4301 to_reclaim = min_t(u64, num_online_cpus() * 1024 * 1024,
4303 spin_lock(&space_info->lock);
4304 if (can_overcommit(root, space_info, to_reclaim,
4305 BTRFS_RESERVE_FLUSH_ALL)) {
4310 used = space_info->bytes_used + space_info->bytes_reserved +
4311 space_info->bytes_pinned + space_info->bytes_readonly +
4312 space_info->bytes_may_use;
4313 if (can_overcommit(root, space_info, 1024 * 1024,
4314 BTRFS_RESERVE_FLUSH_ALL))
4315 expected = div_factor_fine(space_info->total_bytes, 95);
4317 expected = div_factor_fine(space_info->total_bytes, 90);
4319 if (used > expected)
4320 to_reclaim = used - expected;
4323 to_reclaim = min(to_reclaim, space_info->bytes_may_use +
4324 space_info->bytes_reserved);
4326 spin_unlock(&space_info->lock);
4331 static inline int need_do_async_reclaim(struct btrfs_space_info *space_info,
4332 struct btrfs_fs_info *fs_info, u64 used)
4334 return (used >= div_factor_fine(space_info->total_bytes, 98) &&
4335 !btrfs_fs_closing(fs_info) &&
4336 !test_bit(BTRFS_FS_STATE_REMOUNTING, &fs_info->fs_state));
4339 static int btrfs_need_do_async_reclaim(struct btrfs_space_info *space_info,
4340 struct btrfs_fs_info *fs_info,
4345 spin_lock(&space_info->lock);
4347 * We run out of space and have not got any free space via flush_space,
4348 * so don't bother doing async reclaim.
4350 if (flush_state > COMMIT_TRANS && space_info->full) {
4351 spin_unlock(&space_info->lock);
4355 used = space_info->bytes_used + space_info->bytes_reserved +
4356 space_info->bytes_pinned + space_info->bytes_readonly +
4357 space_info->bytes_may_use;
4358 if (need_do_async_reclaim(space_info, fs_info, used)) {
4359 spin_unlock(&space_info->lock);
4362 spin_unlock(&space_info->lock);
4367 static void btrfs_async_reclaim_metadata_space(struct work_struct *work)
4369 struct btrfs_fs_info *fs_info;
4370 struct btrfs_space_info *space_info;
4374 fs_info = container_of(work, struct btrfs_fs_info, async_reclaim_work);
4375 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4377 to_reclaim = btrfs_calc_reclaim_metadata_size(fs_info->fs_root,
4382 flush_state = FLUSH_DELAYED_ITEMS_NR;
4384 flush_space(fs_info->fs_root, space_info, to_reclaim,
4385 to_reclaim, flush_state);
4387 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, flush_state))
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) {
4511 * We will do the space reservation dance during log replay,
4512 * which means we won't have fs_info->fs_root set, so don't do
4513 * the async reclaim as we will panic.
4515 if (!root->fs_info->log_root_recovering &&
4516 need_do_async_reclaim(space_info, root->fs_info, used) &&
4517 !work_busy(&root->fs_info->async_reclaim_work))
4518 queue_work(system_unbound_wq,
4519 &root->fs_info->async_reclaim_work);
4521 spin_unlock(&space_info->lock);
4523 if (!ret || flush == BTRFS_RESERVE_NO_FLUSH)
4526 ret = flush_space(root, space_info, num_bytes, orig_bytes,
4531 * If we are FLUSH_LIMIT, we can not flush delalloc, or the deadlock
4532 * would happen. So skip delalloc flush.
4534 if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4535 (flush_state == FLUSH_DELALLOC ||
4536 flush_state == FLUSH_DELALLOC_WAIT))
4537 flush_state = ALLOC_CHUNK;
4541 else if (flush == BTRFS_RESERVE_FLUSH_LIMIT &&
4542 flush_state < COMMIT_TRANS)
4544 else if (flush == BTRFS_RESERVE_FLUSH_ALL &&
4545 flush_state <= COMMIT_TRANS)
4549 if (ret == -ENOSPC &&
4550 unlikely(root->orphan_cleanup_state == ORPHAN_CLEANUP_STARTED)) {
4551 struct btrfs_block_rsv *global_rsv =
4552 &root->fs_info->global_block_rsv;
4554 if (block_rsv != global_rsv &&
4555 !block_rsv_use_bytes(global_rsv, orig_bytes))
4559 trace_btrfs_space_reservation(root->fs_info,
4560 "space_info:enospc",
4561 space_info->flags, orig_bytes, 1);
4563 spin_lock(&space_info->lock);
4564 space_info->flush = 0;
4565 wake_up_all(&space_info->wait);
4566 spin_unlock(&space_info->lock);
4571 static struct btrfs_block_rsv *get_block_rsv(
4572 const struct btrfs_trans_handle *trans,
4573 const struct btrfs_root *root)
4575 struct btrfs_block_rsv *block_rsv = NULL;
4577 if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4578 block_rsv = trans->block_rsv;
4580 if (root == root->fs_info->csum_root && trans->adding_csums)
4581 block_rsv = trans->block_rsv;
4583 if (root == root->fs_info->uuid_root)
4584 block_rsv = trans->block_rsv;
4587 block_rsv = root->block_rsv;
4590 block_rsv = &root->fs_info->empty_block_rsv;
4595 static int block_rsv_use_bytes(struct btrfs_block_rsv *block_rsv,
4599 spin_lock(&block_rsv->lock);
4600 if (block_rsv->reserved >= num_bytes) {
4601 block_rsv->reserved -= num_bytes;
4602 if (block_rsv->reserved < block_rsv->size)
4603 block_rsv->full = 0;
4606 spin_unlock(&block_rsv->lock);
4610 static void block_rsv_add_bytes(struct btrfs_block_rsv *block_rsv,
4611 u64 num_bytes, int update_size)
4613 spin_lock(&block_rsv->lock);
4614 block_rsv->reserved += num_bytes;
4616 block_rsv->size += num_bytes;
4617 else if (block_rsv->reserved >= block_rsv->size)
4618 block_rsv->full = 1;
4619 spin_unlock(&block_rsv->lock);
4622 int btrfs_cond_migrate_bytes(struct btrfs_fs_info *fs_info,
4623 struct btrfs_block_rsv *dest, u64 num_bytes,
4626 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
4629 if (global_rsv->space_info != dest->space_info)
4632 spin_lock(&global_rsv->lock);
4633 min_bytes = div_factor(global_rsv->size, min_factor);
4634 if (global_rsv->reserved < min_bytes + num_bytes) {
4635 spin_unlock(&global_rsv->lock);
4638 global_rsv->reserved -= num_bytes;
4639 if (global_rsv->reserved < global_rsv->size)
4640 global_rsv->full = 0;
4641 spin_unlock(&global_rsv->lock);
4643 block_rsv_add_bytes(dest, num_bytes, 1);
4647 static void block_rsv_release_bytes(struct btrfs_fs_info *fs_info,
4648 struct btrfs_block_rsv *block_rsv,
4649 struct btrfs_block_rsv *dest, u64 num_bytes)
4651 struct btrfs_space_info *space_info = block_rsv->space_info;
4653 spin_lock(&block_rsv->lock);
4654 if (num_bytes == (u64)-1)
4655 num_bytes = block_rsv->size;
4656 block_rsv->size -= num_bytes;
4657 if (block_rsv->reserved >= block_rsv->size) {
4658 num_bytes = block_rsv->reserved - block_rsv->size;
4659 block_rsv->reserved = block_rsv->size;
4660 block_rsv->full = 1;
4664 spin_unlock(&block_rsv->lock);
4666 if (num_bytes > 0) {
4668 spin_lock(&dest->lock);
4672 bytes_to_add = dest->size - dest->reserved;
4673 bytes_to_add = min(num_bytes, bytes_to_add);
4674 dest->reserved += bytes_to_add;
4675 if (dest->reserved >= dest->size)
4677 num_bytes -= bytes_to_add;
4679 spin_unlock(&dest->lock);
4682 spin_lock(&space_info->lock);
4683 space_info->bytes_may_use -= num_bytes;
4684 trace_btrfs_space_reservation(fs_info, "space_info",
4685 space_info->flags, num_bytes, 0);
4686 spin_unlock(&space_info->lock);
4691 static int block_rsv_migrate_bytes(struct btrfs_block_rsv *src,
4692 struct btrfs_block_rsv *dst, u64 num_bytes)
4696 ret = block_rsv_use_bytes(src, num_bytes);
4700 block_rsv_add_bytes(dst, num_bytes, 1);
4704 void btrfs_init_block_rsv(struct btrfs_block_rsv *rsv, unsigned short type)
4706 memset(rsv, 0, sizeof(*rsv));
4707 spin_lock_init(&rsv->lock);
4711 struct btrfs_block_rsv *btrfs_alloc_block_rsv(struct btrfs_root *root,
4712 unsigned short type)
4714 struct btrfs_block_rsv *block_rsv;
4715 struct btrfs_fs_info *fs_info = root->fs_info;
4717 block_rsv = kmalloc(sizeof(*block_rsv), GFP_NOFS);
4721 btrfs_init_block_rsv(block_rsv, type);
4722 block_rsv->space_info = __find_space_info(fs_info,
4723 BTRFS_BLOCK_GROUP_METADATA);
4727 void btrfs_free_block_rsv(struct btrfs_root *root,
4728 struct btrfs_block_rsv *rsv)
4732 btrfs_block_rsv_release(root, rsv, (u64)-1);
4736 int btrfs_block_rsv_add(struct btrfs_root *root,
4737 struct btrfs_block_rsv *block_rsv, u64 num_bytes,
4738 enum btrfs_reserve_flush_enum flush)
4745 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
4747 block_rsv_add_bytes(block_rsv, num_bytes, 1);
4754 int btrfs_block_rsv_check(struct btrfs_root *root,
4755 struct btrfs_block_rsv *block_rsv, int min_factor)
4763 spin_lock(&block_rsv->lock);
4764 num_bytes = div_factor(block_rsv->size, min_factor);
4765 if (block_rsv->reserved >= num_bytes)
4767 spin_unlock(&block_rsv->lock);
4772 int btrfs_block_rsv_refill(struct btrfs_root *root,
4773 struct btrfs_block_rsv *block_rsv, u64 min_reserved,
4774 enum btrfs_reserve_flush_enum flush)
4782 spin_lock(&block_rsv->lock);
4783 num_bytes = min_reserved;
4784 if (block_rsv->reserved >= num_bytes)
4787 num_bytes -= block_rsv->reserved;
4788 spin_unlock(&block_rsv->lock);
4793 ret = reserve_metadata_bytes(root, block_rsv, num_bytes, flush);
4795 block_rsv_add_bytes(block_rsv, num_bytes, 0);
4802 int btrfs_block_rsv_migrate(struct btrfs_block_rsv *src_rsv,
4803 struct btrfs_block_rsv *dst_rsv,
4806 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4809 void btrfs_block_rsv_release(struct btrfs_root *root,
4810 struct btrfs_block_rsv *block_rsv,
4813 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
4814 if (global_rsv == block_rsv ||
4815 block_rsv->space_info != global_rsv->space_info)
4817 block_rsv_release_bytes(root->fs_info, block_rsv, global_rsv,
4822 * helper to calculate size of global block reservation.
4823 * the desired value is sum of space used by extent tree,
4824 * checksum tree and root tree
4826 static u64 calc_global_metadata_size(struct btrfs_fs_info *fs_info)
4828 struct btrfs_space_info *sinfo;
4832 int csum_size = btrfs_super_csum_size(fs_info->super_copy);
4834 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_DATA);
4835 spin_lock(&sinfo->lock);
4836 data_used = sinfo->bytes_used;
4837 spin_unlock(&sinfo->lock);
4839 sinfo = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4840 spin_lock(&sinfo->lock);
4841 if (sinfo->flags & BTRFS_BLOCK_GROUP_DATA)
4843 meta_used = sinfo->bytes_used;
4844 spin_unlock(&sinfo->lock);
4846 num_bytes = (data_used >> fs_info->sb->s_blocksize_bits) *
4848 num_bytes += div64_u64(data_used + meta_used, 50);
4850 if (num_bytes * 3 > meta_used)
4851 num_bytes = div64_u64(meta_used, 3);
4853 return ALIGN(num_bytes, fs_info->extent_root->nodesize << 10);
4856 static void update_global_block_rsv(struct btrfs_fs_info *fs_info)
4858 struct btrfs_block_rsv *block_rsv = &fs_info->global_block_rsv;
4859 struct btrfs_space_info *sinfo = block_rsv->space_info;
4862 num_bytes = calc_global_metadata_size(fs_info);
4864 spin_lock(&sinfo->lock);
4865 spin_lock(&block_rsv->lock);
4867 block_rsv->size = min_t(u64, num_bytes, 512 * 1024 * 1024);
4869 num_bytes = sinfo->bytes_used + sinfo->bytes_pinned +
4870 sinfo->bytes_reserved + sinfo->bytes_readonly +
4871 sinfo->bytes_may_use;
4873 if (sinfo->total_bytes > num_bytes) {
4874 num_bytes = sinfo->total_bytes - num_bytes;
4875 block_rsv->reserved += num_bytes;
4876 sinfo->bytes_may_use += num_bytes;
4877 trace_btrfs_space_reservation(fs_info, "space_info",
4878 sinfo->flags, num_bytes, 1);
4881 if (block_rsv->reserved >= block_rsv->size) {
4882 num_bytes = block_rsv->reserved - block_rsv->size;
4883 sinfo->bytes_may_use -= num_bytes;
4884 trace_btrfs_space_reservation(fs_info, "space_info",
4885 sinfo->flags, num_bytes, 0);
4886 block_rsv->reserved = block_rsv->size;
4887 block_rsv->full = 1;
4890 spin_unlock(&block_rsv->lock);
4891 spin_unlock(&sinfo->lock);
4894 static void init_global_block_rsv(struct btrfs_fs_info *fs_info)
4896 struct btrfs_space_info *space_info;
4898 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_SYSTEM);
4899 fs_info->chunk_block_rsv.space_info = space_info;
4901 space_info = __find_space_info(fs_info, BTRFS_BLOCK_GROUP_METADATA);
4902 fs_info->global_block_rsv.space_info = space_info;
4903 fs_info->delalloc_block_rsv.space_info = space_info;
4904 fs_info->trans_block_rsv.space_info = space_info;
4905 fs_info->empty_block_rsv.space_info = space_info;
4906 fs_info->delayed_block_rsv.space_info = space_info;
4908 fs_info->extent_root->block_rsv = &fs_info->global_block_rsv;
4909 fs_info->csum_root->block_rsv = &fs_info->global_block_rsv;
4910 fs_info->dev_root->block_rsv = &fs_info->global_block_rsv;
4911 fs_info->tree_root->block_rsv = &fs_info->global_block_rsv;
4912 if (fs_info->quota_root)
4913 fs_info->quota_root->block_rsv = &fs_info->global_block_rsv;
4914 fs_info->chunk_root->block_rsv = &fs_info->chunk_block_rsv;
4916 update_global_block_rsv(fs_info);
4919 static void release_global_block_rsv(struct btrfs_fs_info *fs_info)
4921 block_rsv_release_bytes(fs_info, &fs_info->global_block_rsv, NULL,
4923 WARN_ON(fs_info->delalloc_block_rsv.size > 0);
4924 WARN_ON(fs_info->delalloc_block_rsv.reserved > 0);
4925 WARN_ON(fs_info->trans_block_rsv.size > 0);
4926 WARN_ON(fs_info->trans_block_rsv.reserved > 0);
4927 WARN_ON(fs_info->chunk_block_rsv.size > 0);
4928 WARN_ON(fs_info->chunk_block_rsv.reserved > 0);
4929 WARN_ON(fs_info->delayed_block_rsv.size > 0);
4930 WARN_ON(fs_info->delayed_block_rsv.reserved > 0);
4933 void btrfs_trans_release_metadata(struct btrfs_trans_handle *trans,
4934 struct btrfs_root *root)
4936 if (!trans->block_rsv)
4939 if (!trans->bytes_reserved)
4942 trace_btrfs_space_reservation(root->fs_info, "transaction",
4943 trans->transid, trans->bytes_reserved, 0);
4944 btrfs_block_rsv_release(root, trans->block_rsv, trans->bytes_reserved);
4945 trans->bytes_reserved = 0;
4948 /* Can only return 0 or -ENOSPC */
4949 int btrfs_orphan_reserve_metadata(struct btrfs_trans_handle *trans,
4950 struct inode *inode)
4952 struct btrfs_root *root = BTRFS_I(inode)->root;
4953 struct btrfs_block_rsv *src_rsv = get_block_rsv(trans, root);
4954 struct btrfs_block_rsv *dst_rsv = root->orphan_block_rsv;
4957 * We need to hold space in order to delete our orphan item once we've
4958 * added it, so this takes the reservation so we can release it later
4959 * when we are truly done with the orphan item.
4961 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4962 trace_btrfs_space_reservation(root->fs_info, "orphan",
4963 btrfs_ino(inode), num_bytes, 1);
4964 return block_rsv_migrate_bytes(src_rsv, dst_rsv, num_bytes);
4967 void btrfs_orphan_release_metadata(struct inode *inode)
4969 struct btrfs_root *root = BTRFS_I(inode)->root;
4970 u64 num_bytes = btrfs_calc_trans_metadata_size(root, 1);
4971 trace_btrfs_space_reservation(root->fs_info, "orphan",
4972 btrfs_ino(inode), num_bytes, 0);
4973 btrfs_block_rsv_release(root, root->orphan_block_rsv, num_bytes);
4977 * btrfs_subvolume_reserve_metadata() - reserve space for subvolume operation
4978 * root: the root of the parent directory
4979 * rsv: block reservation
4980 * items: the number of items that we need do reservation
4981 * qgroup_reserved: used to return the reserved size in qgroup
4983 * This function is used to reserve the space for snapshot/subvolume
4984 * creation and deletion. Those operations are different with the
4985 * common file/directory operations, they change two fs/file trees
4986 * and root tree, the number of items that the qgroup reserves is
4987 * different with the free space reservation. So we can not use
4988 * the space reseravtion mechanism in start_transaction().
4990 int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
4991 struct btrfs_block_rsv *rsv,
4993 u64 *qgroup_reserved,
4994 bool use_global_rsv)
4998 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
5000 if (root->fs_info->quota_enabled) {
5001 /* One for parent inode, two for dir entries */
5002 num_bytes = 3 * root->nodesize;
5003 ret = btrfs_qgroup_reserve(root, num_bytes);
5010 *qgroup_reserved = num_bytes;
5012 num_bytes = btrfs_calc_trans_metadata_size(root, items);
5013 rsv->space_info = __find_space_info(root->fs_info,
5014 BTRFS_BLOCK_GROUP_METADATA);
5015 ret = btrfs_block_rsv_add(root, rsv, num_bytes,
5016 BTRFS_RESERVE_FLUSH_ALL);
5018 if (ret == -ENOSPC && use_global_rsv)
5019 ret = btrfs_block_rsv_migrate(global_rsv, rsv, num_bytes);
5022 if (*qgroup_reserved)
5023 btrfs_qgroup_free(root, *qgroup_reserved);
5029 void btrfs_subvolume_release_metadata(struct btrfs_root *root,
5030 struct btrfs_block_rsv *rsv,
5031 u64 qgroup_reserved)
5033 btrfs_block_rsv_release(root, rsv, (u64)-1);
5034 if (qgroup_reserved)
5035 btrfs_qgroup_free(root, qgroup_reserved);
5039 * drop_outstanding_extent - drop an outstanding extent
5040 * @inode: the inode we're dropping the extent for
5042 * This is called when we are freeing up an outstanding extent, either called
5043 * after an error or after an extent is written. This will return the number of
5044 * reserved extents that need to be freed. This must be called with
5045 * BTRFS_I(inode)->lock held.
5047 static unsigned drop_outstanding_extent(struct inode *inode)
5049 unsigned drop_inode_space = 0;
5050 unsigned dropped_extents = 0;
5052 BUG_ON(!BTRFS_I(inode)->outstanding_extents);
5053 BTRFS_I(inode)->outstanding_extents--;
5055 if (BTRFS_I(inode)->outstanding_extents == 0 &&
5056 test_and_clear_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5057 &BTRFS_I(inode)->runtime_flags))
5058 drop_inode_space = 1;
5061 * If we have more or the same amount of outsanding extents than we have
5062 * reserved then we need to leave the reserved extents count alone.
5064 if (BTRFS_I(inode)->outstanding_extents >=
5065 BTRFS_I(inode)->reserved_extents)
5066 return drop_inode_space;
5068 dropped_extents = BTRFS_I(inode)->reserved_extents -
5069 BTRFS_I(inode)->outstanding_extents;
5070 BTRFS_I(inode)->reserved_extents -= dropped_extents;
5071 return dropped_extents + drop_inode_space;
5075 * calc_csum_metadata_size - return the amount of metada space that must be
5076 * reserved/free'd for the given bytes.
5077 * @inode: the inode we're manipulating
5078 * @num_bytes: the number of bytes in question
5079 * @reserve: 1 if we are reserving space, 0 if we are freeing space
5081 * This adjusts the number of csum_bytes in the inode and then returns the
5082 * correct amount of metadata that must either be reserved or freed. We
5083 * calculate how many checksums we can fit into one leaf and then divide the
5084 * number of bytes that will need to be checksumed by this value to figure out
5085 * how many checksums will be required. If we are adding bytes then the number
5086 * may go up and we will return the number of additional bytes that must be
5087 * reserved. If it is going down we will return the number of bytes that must
5090 * This must be called with BTRFS_I(inode)->lock held.
5092 static u64 calc_csum_metadata_size(struct inode *inode, u64 num_bytes,
5095 struct btrfs_root *root = BTRFS_I(inode)->root;
5097 int num_csums_per_leaf;
5101 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM &&
5102 BTRFS_I(inode)->csum_bytes == 0)
5105 old_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5107 BTRFS_I(inode)->csum_bytes += num_bytes;
5109 BTRFS_I(inode)->csum_bytes -= num_bytes;
5110 csum_size = BTRFS_LEAF_DATA_SIZE(root) - sizeof(struct btrfs_item);
5111 num_csums_per_leaf = (int)div64_u64(csum_size,
5112 sizeof(struct btrfs_csum_item) +
5113 sizeof(struct btrfs_disk_key));
5114 num_csums = (int)div64_u64(BTRFS_I(inode)->csum_bytes, root->sectorsize);
5115 num_csums = num_csums + num_csums_per_leaf - 1;
5116 num_csums = num_csums / num_csums_per_leaf;
5118 old_csums = old_csums + num_csums_per_leaf - 1;
5119 old_csums = old_csums / num_csums_per_leaf;
5121 /* No change, no need to reserve more */
5122 if (old_csums == num_csums)
5126 return btrfs_calc_trans_metadata_size(root,
5127 num_csums - old_csums);
5129 return btrfs_calc_trans_metadata_size(root, old_csums - num_csums);
5132 int btrfs_delalloc_reserve_metadata(struct inode *inode, u64 num_bytes)
5134 struct btrfs_root *root = BTRFS_I(inode)->root;
5135 struct btrfs_block_rsv *block_rsv = &root->fs_info->delalloc_block_rsv;
5138 unsigned nr_extents = 0;
5139 int extra_reserve = 0;
5140 enum btrfs_reserve_flush_enum flush = BTRFS_RESERVE_FLUSH_ALL;
5142 bool delalloc_lock = true;
5146 /* If we are a free space inode we need to not flush since we will be in
5147 * the middle of a transaction commit. We also don't need the delalloc
5148 * mutex since we won't race with anybody. We need this mostly to make
5149 * lockdep shut its filthy mouth.
5151 if (btrfs_is_free_space_inode(inode)) {
5152 flush = BTRFS_RESERVE_NO_FLUSH;
5153 delalloc_lock = false;
5156 if (flush != BTRFS_RESERVE_NO_FLUSH &&
5157 btrfs_transaction_in_commit(root->fs_info))
5158 schedule_timeout(1);
5161 mutex_lock(&BTRFS_I(inode)->delalloc_mutex);
5163 num_bytes = ALIGN(num_bytes, root->sectorsize);
5165 spin_lock(&BTRFS_I(inode)->lock);
5166 BTRFS_I(inode)->outstanding_extents++;
5168 if (BTRFS_I(inode)->outstanding_extents >
5169 BTRFS_I(inode)->reserved_extents)
5170 nr_extents = BTRFS_I(inode)->outstanding_extents -
5171 BTRFS_I(inode)->reserved_extents;
5174 * Add an item to reserve for updating the inode when we complete the
5177 if (!test_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5178 &BTRFS_I(inode)->runtime_flags)) {
5183 to_reserve = btrfs_calc_trans_metadata_size(root, nr_extents);
5184 to_reserve += calc_csum_metadata_size(inode, num_bytes, 1);
5185 csum_bytes = BTRFS_I(inode)->csum_bytes;
5186 spin_unlock(&BTRFS_I(inode)->lock);
5188 if (root->fs_info->quota_enabled) {
5189 ret = btrfs_qgroup_reserve(root, num_bytes +
5190 nr_extents * root->nodesize);
5195 ret = reserve_metadata_bytes(root, block_rsv, to_reserve, flush);
5196 if (unlikely(ret)) {
5197 if (root->fs_info->quota_enabled)
5198 btrfs_qgroup_free(root, num_bytes +
5199 nr_extents * root->nodesize);
5203 spin_lock(&BTRFS_I(inode)->lock);
5204 if (extra_reserve) {
5205 set_bit(BTRFS_INODE_DELALLOC_META_RESERVED,
5206 &BTRFS_I(inode)->runtime_flags);
5209 BTRFS_I(inode)->reserved_extents += nr_extents;
5210 spin_unlock(&BTRFS_I(inode)->lock);
5213 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5216 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5217 btrfs_ino(inode), to_reserve, 1);
5218 block_rsv_add_bytes(block_rsv, to_reserve, 1);
5223 spin_lock(&BTRFS_I(inode)->lock);
5224 dropped = drop_outstanding_extent(inode);
5226 * If the inodes csum_bytes is the same as the original
5227 * csum_bytes then we know we haven't raced with any free()ers
5228 * so we can just reduce our inodes csum bytes and carry on.
5230 if (BTRFS_I(inode)->csum_bytes == csum_bytes) {
5231 calc_csum_metadata_size(inode, num_bytes, 0);
5233 u64 orig_csum_bytes = BTRFS_I(inode)->csum_bytes;
5237 * This is tricky, but first we need to figure out how much we
5238 * free'd from any free-ers that occured during this
5239 * reservation, so we reset ->csum_bytes to the csum_bytes
5240 * before we dropped our lock, and then call the free for the
5241 * number of bytes that were freed while we were trying our
5244 bytes = csum_bytes - BTRFS_I(inode)->csum_bytes;
5245 BTRFS_I(inode)->csum_bytes = csum_bytes;
5246 to_free = calc_csum_metadata_size(inode, bytes, 0);
5250 * Now we need to see how much we would have freed had we not
5251 * been making this reservation and our ->csum_bytes were not
5252 * artificially inflated.
5254 BTRFS_I(inode)->csum_bytes = csum_bytes - num_bytes;
5255 bytes = csum_bytes - orig_csum_bytes;
5256 bytes = calc_csum_metadata_size(inode, bytes, 0);
5259 * Now reset ->csum_bytes to what it should be. If bytes is
5260 * more than to_free then we would have free'd more space had we
5261 * not had an artificially high ->csum_bytes, so we need to free
5262 * the remainder. If bytes is the same or less then we don't
5263 * need to do anything, the other free-ers did the correct
5266 BTRFS_I(inode)->csum_bytes = orig_csum_bytes - num_bytes;
5267 if (bytes > to_free)
5268 to_free = bytes - to_free;
5272 spin_unlock(&BTRFS_I(inode)->lock);
5274 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5277 btrfs_block_rsv_release(root, block_rsv, to_free);
5278 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5279 btrfs_ino(inode), to_free, 0);
5282 mutex_unlock(&BTRFS_I(inode)->delalloc_mutex);
5287 * btrfs_delalloc_release_metadata - release a metadata reservation for an inode
5288 * @inode: the inode to release the reservation for
5289 * @num_bytes: the number of bytes we're releasing
5291 * This will release the metadata reservation for an inode. This can be called
5292 * once we complete IO for a given set of bytes to release their metadata
5295 void btrfs_delalloc_release_metadata(struct inode *inode, u64 num_bytes)
5297 struct btrfs_root *root = BTRFS_I(inode)->root;
5301 num_bytes = ALIGN(num_bytes, root->sectorsize);
5302 spin_lock(&BTRFS_I(inode)->lock);
5303 dropped = drop_outstanding_extent(inode);
5306 to_free = calc_csum_metadata_size(inode, num_bytes, 0);
5307 spin_unlock(&BTRFS_I(inode)->lock);
5309 to_free += btrfs_calc_trans_metadata_size(root, dropped);
5311 trace_btrfs_space_reservation(root->fs_info, "delalloc",
5312 btrfs_ino(inode), to_free, 0);
5313 if (root->fs_info->quota_enabled) {
5314 btrfs_qgroup_free(root, num_bytes +
5315 dropped * root->nodesize);
5318 btrfs_block_rsv_release(root, &root->fs_info->delalloc_block_rsv,
5323 * btrfs_delalloc_reserve_space - reserve data and metadata space for delalloc
5324 * @inode: inode we're writing to
5325 * @num_bytes: the number of bytes we want to allocate
5327 * This will do the following things
5329 * o reserve space in the data space info for num_bytes
5330 * o reserve space in the metadata space info based on number of outstanding
5331 * extents and how much csums will be needed
5332 * o add to the inodes ->delalloc_bytes
5333 * o add it to the fs_info's delalloc inodes list.
5335 * This will return 0 for success and -ENOSPC if there is no space left.
5337 int btrfs_delalloc_reserve_space(struct inode *inode, u64 num_bytes)
5341 ret = btrfs_check_data_free_space(inode, num_bytes);
5345 ret = btrfs_delalloc_reserve_metadata(inode, num_bytes);
5347 btrfs_free_reserved_data_space(inode, num_bytes);
5355 * btrfs_delalloc_release_space - release data and metadata space for delalloc
5356 * @inode: inode we're releasing space for
5357 * @num_bytes: the number of bytes we want to free up
5359 * This must be matched with a call to btrfs_delalloc_reserve_space. This is
5360 * called in the case that we don't need the metadata AND data reservations
5361 * anymore. So if there is an error or we insert an inline extent.
5363 * This function will release the metadata space that was not used and will
5364 * decrement ->delalloc_bytes and remove it from the fs_info delalloc_inodes
5365 * list if there are no delalloc bytes left.
5367 void btrfs_delalloc_release_space(struct inode *inode, u64 num_bytes)
5369 btrfs_delalloc_release_metadata(inode, num_bytes);
5370 btrfs_free_reserved_data_space(inode, num_bytes);
5373 static int update_block_group(struct btrfs_root *root,
5374 u64 bytenr, u64 num_bytes, int alloc)
5376 struct btrfs_block_group_cache *cache = NULL;
5377 struct btrfs_fs_info *info = root->fs_info;
5378 u64 total = num_bytes;
5383 /* block accounting for super block */
5384 spin_lock(&info->delalloc_root_lock);
5385 old_val = btrfs_super_bytes_used(info->super_copy);
5387 old_val += num_bytes;
5389 old_val -= num_bytes;
5390 btrfs_set_super_bytes_used(info->super_copy, old_val);
5391 spin_unlock(&info->delalloc_root_lock);
5394 cache = btrfs_lookup_block_group(info, bytenr);
5397 if (cache->flags & (BTRFS_BLOCK_GROUP_DUP |
5398 BTRFS_BLOCK_GROUP_RAID1 |
5399 BTRFS_BLOCK_GROUP_RAID10))
5404 * If this block group has free space cache written out, we
5405 * need to make sure to load it if we are removing space. This
5406 * is because we need the unpinning stage to actually add the
5407 * space back to the block group, otherwise we will leak space.
5409 if (!alloc && cache->cached == BTRFS_CACHE_NO)
5410 cache_block_group(cache, 1);
5412 byte_in_group = bytenr - cache->key.objectid;
5413 WARN_ON(byte_in_group > cache->key.offset);
5415 spin_lock(&cache->space_info->lock);
5416 spin_lock(&cache->lock);
5418 if (btrfs_test_opt(root, SPACE_CACHE) &&
5419 cache->disk_cache_state < BTRFS_DC_CLEAR)
5420 cache->disk_cache_state = BTRFS_DC_CLEAR;
5423 old_val = btrfs_block_group_used(&cache->item);
5424 num_bytes = min(total, cache->key.offset - byte_in_group);
5426 old_val += num_bytes;
5427 btrfs_set_block_group_used(&cache->item, old_val);
5428 cache->reserved -= num_bytes;
5429 cache->space_info->bytes_reserved -= num_bytes;
5430 cache->space_info->bytes_used += num_bytes;
5431 cache->space_info->disk_used += num_bytes * factor;
5432 spin_unlock(&cache->lock);
5433 spin_unlock(&cache->space_info->lock);
5435 old_val -= num_bytes;
5438 * No longer have used bytes in this block group, queue
5442 spin_lock(&info->unused_bgs_lock);
5443 if (list_empty(&cache->bg_list)) {
5444 btrfs_get_block_group(cache);
5445 list_add_tail(&cache->bg_list,
5448 spin_unlock(&info->unused_bgs_lock);
5450 btrfs_set_block_group_used(&cache->item, old_val);
5451 cache->pinned += num_bytes;
5452 cache->space_info->bytes_pinned += num_bytes;
5453 cache->space_info->bytes_used -= num_bytes;
5454 cache->space_info->disk_used -= num_bytes * factor;
5455 spin_unlock(&cache->lock);
5456 spin_unlock(&cache->space_info->lock);
5458 set_extent_dirty(info->pinned_extents,
5459 bytenr, bytenr + num_bytes - 1,
5460 GFP_NOFS | __GFP_NOFAIL);
5462 btrfs_put_block_group(cache);
5464 bytenr += num_bytes;
5469 static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
5471 struct btrfs_block_group_cache *cache;
5474 spin_lock(&root->fs_info->block_group_cache_lock);
5475 bytenr = root->fs_info->first_logical_byte;
5476 spin_unlock(&root->fs_info->block_group_cache_lock);
5478 if (bytenr < (u64)-1)
5481 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
5485 bytenr = cache->key.objectid;
5486 btrfs_put_block_group(cache);
5491 static int pin_down_extent(struct btrfs_root *root,
5492 struct btrfs_block_group_cache *cache,
5493 u64 bytenr, u64 num_bytes, int reserved)
5495 spin_lock(&cache->space_info->lock);
5496 spin_lock(&cache->lock);
5497 cache->pinned += num_bytes;
5498 cache->space_info->bytes_pinned += num_bytes;
5500 cache->reserved -= num_bytes;
5501 cache->space_info->bytes_reserved -= num_bytes;
5503 spin_unlock(&cache->lock);
5504 spin_unlock(&cache->space_info->lock);
5506 set_extent_dirty(root->fs_info->pinned_extents, bytenr,
5507 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
5509 trace_btrfs_reserved_extent_free(root, bytenr, num_bytes);
5514 * this function must be called within transaction
5516 int btrfs_pin_extent(struct btrfs_root *root,
5517 u64 bytenr, u64 num_bytes, int reserved)
5519 struct btrfs_block_group_cache *cache;
5521 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
5522 BUG_ON(!cache); /* Logic error */
5524 pin_down_extent(root, cache, bytenr, num_bytes, reserved);
5526 btrfs_put_block_group(cache);
5531 * this function must be called within transaction
5533 int btrfs_pin_extent_for_log_replay(struct btrfs_root *root,
5534 u64 bytenr, u64 num_bytes)
5536 struct btrfs_block_group_cache *cache;
5539 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
5544 * pull in the free space cache (if any) so that our pin
5545 * removes the free space from the cache. We have load_only set
5546 * to one because the slow code to read in the free extents does check
5547 * the pinned extents.
5549 cache_block_group(cache, 1);
5551 pin_down_extent(root, cache, bytenr, num_bytes, 0);
5553 /* remove us from the free space cache (if we're there at all) */
5554 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
5555 btrfs_put_block_group(cache);
5559 static int __exclude_logged_extent(struct btrfs_root *root, u64 start, u64 num_bytes)
5562 struct btrfs_block_group_cache *block_group;
5563 struct btrfs_caching_control *caching_ctl;
5565 block_group = btrfs_lookup_block_group(root->fs_info, start);
5569 cache_block_group(block_group, 0);
5570 caching_ctl = get_caching_control(block_group);
5574 BUG_ON(!block_group_cache_done(block_group));
5575 ret = btrfs_remove_free_space(block_group, start, num_bytes);
5577 mutex_lock(&caching_ctl->mutex);
5579 if (start >= caching_ctl->progress) {
5580 ret = add_excluded_extent(root, start, num_bytes);
5581 } else if (start + num_bytes <= caching_ctl->progress) {
5582 ret = btrfs_remove_free_space(block_group,
5585 num_bytes = caching_ctl->progress - start;
5586 ret = btrfs_remove_free_space(block_group,
5591 num_bytes = (start + num_bytes) -
5592 caching_ctl->progress;
5593 start = caching_ctl->progress;
5594 ret = add_excluded_extent(root, start, num_bytes);
5597 mutex_unlock(&caching_ctl->mutex);
5598 put_caching_control(caching_ctl);
5600 btrfs_put_block_group(block_group);
5604 int btrfs_exclude_logged_extents(struct btrfs_root *log,
5605 struct extent_buffer *eb)
5607 struct btrfs_file_extent_item *item;
5608 struct btrfs_key key;
5612 if (!btrfs_fs_incompat(log->fs_info, MIXED_GROUPS))
5615 for (i = 0; i < btrfs_header_nritems(eb); i++) {
5616 btrfs_item_key_to_cpu(eb, &key, i);
5617 if (key.type != BTRFS_EXTENT_DATA_KEY)
5619 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
5620 found_type = btrfs_file_extent_type(eb, item);
5621 if (found_type == BTRFS_FILE_EXTENT_INLINE)
5623 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
5625 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
5626 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
5627 __exclude_logged_extent(log, key.objectid, key.offset);
5634 * btrfs_update_reserved_bytes - update the block_group and space info counters
5635 * @cache: The cache we are manipulating
5636 * @num_bytes: The number of bytes in question
5637 * @reserve: One of the reservation enums
5638 * @delalloc: The blocks are allocated for the delalloc write
5640 * This is called by the allocator when it reserves space, or by somebody who is
5641 * freeing space that was never actually used on disk. For example if you
5642 * reserve some space for a new leaf in transaction A and before transaction A
5643 * commits you free that leaf, you call this with reserve set to 0 in order to
5644 * clear the reservation.
5646 * Metadata reservations should be called with RESERVE_ALLOC so we do the proper
5647 * ENOSPC accounting. For data we handle the reservation through clearing the
5648 * delalloc bits in the io_tree. We have to do this since we could end up
5649 * allocating less disk space for the amount of data we have reserved in the
5650 * case of compression.
5652 * If this is a reservation and the block group has become read only we cannot
5653 * make the reservation and return -EAGAIN, otherwise this function always
5656 static int btrfs_update_reserved_bytes(struct btrfs_block_group_cache *cache,
5657 u64 num_bytes, int reserve, int delalloc)
5659 struct btrfs_space_info *space_info = cache->space_info;
5662 spin_lock(&space_info->lock);
5663 spin_lock(&cache->lock);
5664 if (reserve != RESERVE_FREE) {
5668 cache->reserved += num_bytes;
5669 space_info->bytes_reserved += num_bytes;
5670 if (reserve == RESERVE_ALLOC) {
5671 trace_btrfs_space_reservation(cache->fs_info,
5672 "space_info", space_info->flags,
5674 space_info->bytes_may_use -= num_bytes;
5678 cache->delalloc_bytes += num_bytes;
5682 space_info->bytes_readonly += num_bytes;
5683 cache->reserved -= num_bytes;
5684 space_info->bytes_reserved -= num_bytes;
5687 cache->delalloc_bytes -= num_bytes;
5689 spin_unlock(&cache->lock);
5690 spin_unlock(&space_info->lock);
5694 void btrfs_prepare_extent_commit(struct btrfs_trans_handle *trans,
5695 struct btrfs_root *root)
5697 struct btrfs_fs_info *fs_info = root->fs_info;
5698 struct btrfs_caching_control *next;
5699 struct btrfs_caching_control *caching_ctl;
5700 struct btrfs_block_group_cache *cache;
5702 down_write(&fs_info->commit_root_sem);
5704 list_for_each_entry_safe(caching_ctl, next,
5705 &fs_info->caching_block_groups, list) {
5706 cache = caching_ctl->block_group;
5707 if (block_group_cache_done(cache)) {
5708 cache->last_byte_to_unpin = (u64)-1;
5709 list_del_init(&caching_ctl->list);
5710 put_caching_control(caching_ctl);
5712 cache->last_byte_to_unpin = caching_ctl->progress;
5716 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5717 fs_info->pinned_extents = &fs_info->freed_extents[1];
5719 fs_info->pinned_extents = &fs_info->freed_extents[0];
5721 up_write(&fs_info->commit_root_sem);
5723 update_global_block_rsv(fs_info);
5726 static int unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
5728 struct btrfs_fs_info *fs_info = root->fs_info;
5729 struct btrfs_block_group_cache *cache = NULL;
5730 struct btrfs_space_info *space_info;
5731 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
5735 while (start <= end) {
5738 start >= cache->key.objectid + cache->key.offset) {
5740 btrfs_put_block_group(cache);
5741 cache = btrfs_lookup_block_group(fs_info, start);
5742 BUG_ON(!cache); /* Logic error */
5745 len = cache->key.objectid + cache->key.offset - start;
5746 len = min(len, end + 1 - start);
5748 if (start < cache->last_byte_to_unpin) {
5749 len = min(len, cache->last_byte_to_unpin - start);
5750 btrfs_add_free_space(cache, start, len);
5754 space_info = cache->space_info;
5756 spin_lock(&space_info->lock);
5757 spin_lock(&cache->lock);
5758 cache->pinned -= len;
5759 space_info->bytes_pinned -= len;
5760 percpu_counter_add(&space_info->total_bytes_pinned, -len);
5762 space_info->bytes_readonly += len;
5765 spin_unlock(&cache->lock);
5766 if (!readonly && global_rsv->space_info == space_info) {
5767 spin_lock(&global_rsv->lock);
5768 if (!global_rsv->full) {
5769 len = min(len, global_rsv->size -
5770 global_rsv->reserved);
5771 global_rsv->reserved += len;
5772 space_info->bytes_may_use += len;
5773 if (global_rsv->reserved >= global_rsv->size)
5774 global_rsv->full = 1;
5776 spin_unlock(&global_rsv->lock);
5778 spin_unlock(&space_info->lock);
5782 btrfs_put_block_group(cache);
5786 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
5787 struct btrfs_root *root)
5789 struct btrfs_fs_info *fs_info = root->fs_info;
5790 struct extent_io_tree *unpin;
5798 if (fs_info->pinned_extents == &fs_info->freed_extents[0])
5799 unpin = &fs_info->freed_extents[1];
5801 unpin = &fs_info->freed_extents[0];
5804 ret = find_first_extent_bit(unpin, 0, &start, &end,
5805 EXTENT_DIRTY, NULL);
5809 if (btrfs_test_opt(root, DISCARD))
5810 ret = btrfs_discard_extent(root, start,
5811 end + 1 - start, NULL);
5813 clear_extent_dirty(unpin, start, end, GFP_NOFS);
5814 unpin_extent_range(root, start, end);
5821 static void add_pinned_bytes(struct btrfs_fs_info *fs_info, u64 num_bytes,
5822 u64 owner, u64 root_objectid)
5824 struct btrfs_space_info *space_info;
5827 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
5828 if (root_objectid == BTRFS_CHUNK_TREE_OBJECTID)
5829 flags = BTRFS_BLOCK_GROUP_SYSTEM;
5831 flags = BTRFS_BLOCK_GROUP_METADATA;
5833 flags = BTRFS_BLOCK_GROUP_DATA;
5836 space_info = __find_space_info(fs_info, flags);
5837 BUG_ON(!space_info); /* Logic bug */
5838 percpu_counter_add(&space_info->total_bytes_pinned, num_bytes);
5842 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
5843 struct btrfs_root *root,
5844 u64 bytenr, u64 num_bytes, u64 parent,
5845 u64 root_objectid, u64 owner_objectid,
5846 u64 owner_offset, int refs_to_drop,
5847 struct btrfs_delayed_extent_op *extent_op,
5850 struct btrfs_key key;
5851 struct btrfs_path *path;
5852 struct btrfs_fs_info *info = root->fs_info;
5853 struct btrfs_root *extent_root = info->extent_root;
5854 struct extent_buffer *leaf;
5855 struct btrfs_extent_item *ei;
5856 struct btrfs_extent_inline_ref *iref;
5859 int extent_slot = 0;
5860 int found_extent = 0;
5865 enum btrfs_qgroup_operation_type type = BTRFS_QGROUP_OPER_SUB_EXCL;
5866 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
5869 if (!info->quota_enabled || !is_fstree(root_objectid))
5872 path = btrfs_alloc_path();
5877 path->leave_spinning = 1;
5879 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
5880 BUG_ON(!is_data && refs_to_drop != 1);
5883 skinny_metadata = 0;
5885 ret = lookup_extent_backref(trans, extent_root, path, &iref,
5886 bytenr, num_bytes, parent,
5887 root_objectid, owner_objectid,
5890 extent_slot = path->slots[0];
5891 while (extent_slot >= 0) {
5892 btrfs_item_key_to_cpu(path->nodes[0], &key,
5894 if (key.objectid != bytenr)
5896 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
5897 key.offset == num_bytes) {
5901 if (key.type == BTRFS_METADATA_ITEM_KEY &&
5902 key.offset == owner_objectid) {
5906 if (path->slots[0] - extent_slot > 5)
5910 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5911 item_size = btrfs_item_size_nr(path->nodes[0], extent_slot);
5912 if (found_extent && item_size < sizeof(*ei))
5915 if (!found_extent) {
5917 ret = remove_extent_backref(trans, extent_root, path,
5919 is_data, &last_ref);
5921 btrfs_abort_transaction(trans, extent_root, ret);
5924 btrfs_release_path(path);
5925 path->leave_spinning = 1;
5927 key.objectid = bytenr;
5928 key.type = BTRFS_EXTENT_ITEM_KEY;
5929 key.offset = num_bytes;
5931 if (!is_data && skinny_metadata) {
5932 key.type = BTRFS_METADATA_ITEM_KEY;
5933 key.offset = owner_objectid;
5936 ret = btrfs_search_slot(trans, extent_root,
5938 if (ret > 0 && skinny_metadata && path->slots[0]) {
5940 * Couldn't find our skinny metadata item,
5941 * see if we have ye olde extent item.
5944 btrfs_item_key_to_cpu(path->nodes[0], &key,
5946 if (key.objectid == bytenr &&
5947 key.type == BTRFS_EXTENT_ITEM_KEY &&
5948 key.offset == num_bytes)
5952 if (ret > 0 && skinny_metadata) {
5953 skinny_metadata = false;
5954 key.objectid = bytenr;
5955 key.type = BTRFS_EXTENT_ITEM_KEY;
5956 key.offset = num_bytes;
5957 btrfs_release_path(path);
5958 ret = btrfs_search_slot(trans, extent_root,
5963 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
5966 btrfs_print_leaf(extent_root,
5970 btrfs_abort_transaction(trans, extent_root, ret);
5973 extent_slot = path->slots[0];
5975 } else if (WARN_ON(ret == -ENOENT)) {
5976 btrfs_print_leaf(extent_root, path->nodes[0]);
5978 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
5979 bytenr, parent, root_objectid, owner_objectid,
5981 btrfs_abort_transaction(trans, extent_root, ret);
5984 btrfs_abort_transaction(trans, extent_root, ret);
5988 leaf = path->nodes[0];
5989 item_size = btrfs_item_size_nr(leaf, extent_slot);
5990 #ifdef BTRFS_COMPAT_EXTENT_TREE_V0
5991 if (item_size < sizeof(*ei)) {
5992 BUG_ON(found_extent || extent_slot != path->slots[0]);
5993 ret = convert_extent_item_v0(trans, extent_root, path,
5996 btrfs_abort_transaction(trans, extent_root, ret);
6000 btrfs_release_path(path);
6001 path->leave_spinning = 1;
6003 key.objectid = bytenr;
6004 key.type = BTRFS_EXTENT_ITEM_KEY;
6005 key.offset = num_bytes;
6007 ret = btrfs_search_slot(trans, extent_root, &key, path,
6010 btrfs_err(info, "umm, got %d back from search, was looking for %llu",
6012 btrfs_print_leaf(extent_root, path->nodes[0]);
6015 btrfs_abort_transaction(trans, extent_root, ret);
6019 extent_slot = path->slots[0];
6020 leaf = path->nodes[0];
6021 item_size = btrfs_item_size_nr(leaf, extent_slot);
6024 BUG_ON(item_size < sizeof(*ei));
6025 ei = btrfs_item_ptr(leaf, extent_slot,
6026 struct btrfs_extent_item);
6027 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
6028 key.type == BTRFS_EXTENT_ITEM_KEY) {
6029 struct btrfs_tree_block_info *bi;
6030 BUG_ON(item_size < sizeof(*ei) + sizeof(*bi));
6031 bi = (struct btrfs_tree_block_info *)(ei + 1);
6032 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
6035 refs = btrfs_extent_refs(leaf, ei);
6036 if (refs < refs_to_drop) {
6037 btrfs_err(info, "trying to drop %d refs but we only have %Lu "
6038 "for bytenr %Lu", refs_to_drop, refs, bytenr);
6040 btrfs_abort_transaction(trans, extent_root, ret);
6043 refs -= refs_to_drop;
6046 type = BTRFS_QGROUP_OPER_SUB_SHARED;
6048 __run_delayed_extent_op(extent_op, leaf, ei);
6050 * In the case of inline back ref, reference count will
6051 * be updated by remove_extent_backref
6054 BUG_ON(!found_extent);
6056 btrfs_set_extent_refs(leaf, ei, refs);
6057 btrfs_mark_buffer_dirty(leaf);
6060 ret = remove_extent_backref(trans, extent_root, path,
6062 is_data, &last_ref);
6064 btrfs_abort_transaction(trans, extent_root, ret);
6068 add_pinned_bytes(root->fs_info, -num_bytes, owner_objectid,
6072 BUG_ON(is_data && refs_to_drop !=
6073 extent_data_ref_count(root, path, iref));
6075 BUG_ON(path->slots[0] != extent_slot);
6077 BUG_ON(path->slots[0] != extent_slot + 1);
6078 path->slots[0] = extent_slot;
6084 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
6087 btrfs_abort_transaction(trans, extent_root, ret);
6090 btrfs_release_path(path);
6093 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
6095 btrfs_abort_transaction(trans, extent_root, ret);
6100 ret = update_block_group(root, bytenr, num_bytes, 0);
6102 btrfs_abort_transaction(trans, extent_root, ret);
6106 btrfs_release_path(path);
6108 /* Deal with the quota accounting */
6109 if (!ret && last_ref && !no_quota) {
6112 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
6113 type == BTRFS_QGROUP_OPER_SUB_SHARED)
6116 ret = btrfs_qgroup_record_ref(trans, info, root_objectid,
6117 bytenr, num_bytes, type,
6121 btrfs_free_path(path);
6126 * when we free an block, it is possible (and likely) that we free the last
6127 * delayed ref for that extent as well. This searches the delayed ref tree for
6128 * a given extent, and if there are no other delayed refs to be processed, it
6129 * removes it from the tree.
6131 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
6132 struct btrfs_root *root, u64 bytenr)
6134 struct btrfs_delayed_ref_head *head;
6135 struct btrfs_delayed_ref_root *delayed_refs;
6138 delayed_refs = &trans->transaction->delayed_refs;
6139 spin_lock(&delayed_refs->lock);
6140 head = btrfs_find_delayed_ref_head(trans, bytenr);
6142 goto out_delayed_unlock;
6144 spin_lock(&head->lock);
6145 if (rb_first(&head->ref_root))
6148 if (head->extent_op) {
6149 if (!head->must_insert_reserved)
6151 btrfs_free_delayed_extent_op(head->extent_op);
6152 head->extent_op = NULL;
6156 * waiting for the lock here would deadlock. If someone else has it
6157 * locked they are already in the process of dropping it anyway
6159 if (!mutex_trylock(&head->mutex))
6163 * at this point we have a head with no other entries. Go
6164 * ahead and process it.
6166 head->node.in_tree = 0;
6167 rb_erase(&head->href_node, &delayed_refs->href_root);
6169 atomic_dec(&delayed_refs->num_entries);
6172 * we don't take a ref on the node because we're removing it from the
6173 * tree, so we just steal the ref the tree was holding.
6175 delayed_refs->num_heads--;
6176 if (head->processing == 0)
6177 delayed_refs->num_heads_ready--;
6178 head->processing = 0;
6179 spin_unlock(&head->lock);
6180 spin_unlock(&delayed_refs->lock);
6182 BUG_ON(head->extent_op);
6183 if (head->must_insert_reserved)
6186 mutex_unlock(&head->mutex);
6187 btrfs_put_delayed_ref(&head->node);
6190 spin_unlock(&head->lock);
6193 spin_unlock(&delayed_refs->lock);
6197 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
6198 struct btrfs_root *root,
6199 struct extent_buffer *buf,
6200 u64 parent, int last_ref)
6202 struct btrfs_block_group_cache *cache = NULL;
6206 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6207 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
6208 buf->start, buf->len,
6209 parent, root->root_key.objectid,
6210 btrfs_header_level(buf),
6211 BTRFS_DROP_DELAYED_REF, NULL, 0);
6212 BUG_ON(ret); /* -ENOMEM */
6218 cache = btrfs_lookup_block_group(root->fs_info, buf->start);
6220 if (btrfs_header_generation(buf) == trans->transid) {
6221 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
6222 ret = check_ref_cleanup(trans, root, buf->start);
6227 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
6228 pin_down_extent(root, cache, buf->start, buf->len, 1);
6232 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
6234 btrfs_add_free_space(cache, buf->start, buf->len);
6235 btrfs_update_reserved_bytes(cache, buf->len, RESERVE_FREE, 0);
6236 trace_btrfs_reserved_extent_free(root, buf->start, buf->len);
6241 add_pinned_bytes(root->fs_info, buf->len,
6242 btrfs_header_level(buf),
6243 root->root_key.objectid);
6246 * Deleting the buffer, clear the corrupt flag since it doesn't matter
6249 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
6250 btrfs_put_block_group(cache);
6253 /* Can return -ENOMEM */
6254 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root,
6255 u64 bytenr, u64 num_bytes, u64 parent, u64 root_objectid,
6256 u64 owner, u64 offset, int no_quota)
6259 struct btrfs_fs_info *fs_info = root->fs_info;
6261 if (btrfs_test_is_dummy_root(root))
6264 add_pinned_bytes(root->fs_info, num_bytes, owner, root_objectid);
6267 * tree log blocks never actually go into the extent allocation
6268 * tree, just update pinning info and exit early.
6270 if (root_objectid == BTRFS_TREE_LOG_OBJECTID) {
6271 WARN_ON(owner >= BTRFS_FIRST_FREE_OBJECTID);
6272 /* unlocks the pinned mutex */
6273 btrfs_pin_extent(root, bytenr, num_bytes, 1);
6275 } else if (owner < BTRFS_FIRST_FREE_OBJECTID) {
6276 ret = btrfs_add_delayed_tree_ref(fs_info, trans, bytenr,
6278 parent, root_objectid, (int)owner,
6279 BTRFS_DROP_DELAYED_REF, NULL, no_quota);
6281 ret = btrfs_add_delayed_data_ref(fs_info, trans, bytenr,
6283 parent, root_objectid, owner,
6284 offset, BTRFS_DROP_DELAYED_REF,
6291 * when we wait for progress in the block group caching, its because
6292 * our allocation attempt failed at least once. So, we must sleep
6293 * and let some progress happen before we try again.
6295 * This function will sleep at least once waiting for new free space to
6296 * show up, and then it will check the block group free space numbers
6297 * for our min num_bytes. Another option is to have it go ahead
6298 * and look in the rbtree for a free extent of a given size, but this
6301 * Callers of this must check if cache->cached == BTRFS_CACHE_ERROR before using
6302 * any of the information in this block group.
6304 static noinline void
6305 wait_block_group_cache_progress(struct btrfs_block_group_cache *cache,
6308 struct btrfs_caching_control *caching_ctl;
6310 caching_ctl = get_caching_control(cache);
6314 wait_event(caching_ctl->wait, block_group_cache_done(cache) ||
6315 (cache->free_space_ctl->free_space >= num_bytes));
6317 put_caching_control(caching_ctl);
6321 wait_block_group_cache_done(struct btrfs_block_group_cache *cache)
6323 struct btrfs_caching_control *caching_ctl;
6326 caching_ctl = get_caching_control(cache);
6328 return (cache->cached == BTRFS_CACHE_ERROR) ? -EIO : 0;
6330 wait_event(caching_ctl->wait, block_group_cache_done(cache));
6331 if (cache->cached == BTRFS_CACHE_ERROR)
6333 put_caching_control(caching_ctl);
6337 int __get_raid_index(u64 flags)
6339 if (flags & BTRFS_BLOCK_GROUP_RAID10)
6340 return BTRFS_RAID_RAID10;
6341 else if (flags & BTRFS_BLOCK_GROUP_RAID1)
6342 return BTRFS_RAID_RAID1;
6343 else if (flags & BTRFS_BLOCK_GROUP_DUP)
6344 return BTRFS_RAID_DUP;
6345 else if (flags & BTRFS_BLOCK_GROUP_RAID0)
6346 return BTRFS_RAID_RAID0;
6347 else if (flags & BTRFS_BLOCK_GROUP_RAID5)
6348 return BTRFS_RAID_RAID5;
6349 else if (flags & BTRFS_BLOCK_GROUP_RAID6)
6350 return BTRFS_RAID_RAID6;
6352 return BTRFS_RAID_SINGLE; /* BTRFS_BLOCK_GROUP_SINGLE */
6355 int get_block_group_index(struct btrfs_block_group_cache *cache)
6357 return __get_raid_index(cache->flags);
6360 static const char *btrfs_raid_type_names[BTRFS_NR_RAID_TYPES] = {
6361 [BTRFS_RAID_RAID10] = "raid10",
6362 [BTRFS_RAID_RAID1] = "raid1",
6363 [BTRFS_RAID_DUP] = "dup",
6364 [BTRFS_RAID_RAID0] = "raid0",
6365 [BTRFS_RAID_SINGLE] = "single",
6366 [BTRFS_RAID_RAID5] = "raid5",
6367 [BTRFS_RAID_RAID6] = "raid6",
6370 static const char *get_raid_name(enum btrfs_raid_types type)
6372 if (type >= BTRFS_NR_RAID_TYPES)
6375 return btrfs_raid_type_names[type];
6378 enum btrfs_loop_type {
6379 LOOP_CACHING_NOWAIT = 0,
6380 LOOP_CACHING_WAIT = 1,
6381 LOOP_ALLOC_CHUNK = 2,
6382 LOOP_NO_EMPTY_SIZE = 3,
6386 btrfs_lock_block_group(struct btrfs_block_group_cache *cache,
6390 down_read(&cache->data_rwsem);
6394 btrfs_grab_block_group(struct btrfs_block_group_cache *cache,
6397 btrfs_get_block_group(cache);
6399 down_read(&cache->data_rwsem);
6402 static struct btrfs_block_group_cache *
6403 btrfs_lock_cluster(struct btrfs_block_group_cache *block_group,
6404 struct btrfs_free_cluster *cluster,
6407 struct btrfs_block_group_cache *used_bg;
6408 bool locked = false;
6410 spin_lock(&cluster->refill_lock);
6412 if (used_bg == cluster->block_group)
6415 up_read(&used_bg->data_rwsem);
6416 btrfs_put_block_group(used_bg);
6419 used_bg = cluster->block_group;
6423 if (used_bg == block_group)
6426 btrfs_get_block_group(used_bg);
6431 if (down_read_trylock(&used_bg->data_rwsem))
6434 spin_unlock(&cluster->refill_lock);
6435 down_read(&used_bg->data_rwsem);
6441 btrfs_release_block_group(struct btrfs_block_group_cache *cache,
6445 up_read(&cache->data_rwsem);
6446 btrfs_put_block_group(cache);
6450 * walks the btree of allocated extents and find a hole of a given size.
6451 * The key ins is changed to record the hole:
6452 * ins->objectid == start position
6453 * ins->flags = BTRFS_EXTENT_ITEM_KEY
6454 * ins->offset == the size of the hole.
6455 * Any available blocks before search_start are skipped.
6457 * If there is no suitable free space, we will record the max size of
6458 * the free space extent currently.
6460 static noinline int find_free_extent(struct btrfs_root *orig_root,
6461 u64 num_bytes, u64 empty_size,
6462 u64 hint_byte, struct btrfs_key *ins,
6463 u64 flags, int delalloc)
6466 struct btrfs_root *root = orig_root->fs_info->extent_root;
6467 struct btrfs_free_cluster *last_ptr = NULL;
6468 struct btrfs_block_group_cache *block_group = NULL;
6469 u64 search_start = 0;
6470 u64 max_extent_size = 0;
6471 int empty_cluster = 2 * 1024 * 1024;
6472 struct btrfs_space_info *space_info;
6474 int index = __get_raid_index(flags);
6475 int alloc_type = (flags & BTRFS_BLOCK_GROUP_DATA) ?
6476 RESERVE_ALLOC_NO_ACCOUNT : RESERVE_ALLOC;
6477 bool failed_cluster_refill = false;
6478 bool failed_alloc = false;
6479 bool use_cluster = true;
6480 bool have_caching_bg = false;
6482 WARN_ON(num_bytes < root->sectorsize);
6483 ins->type = BTRFS_EXTENT_ITEM_KEY;
6487 trace_find_free_extent(orig_root, num_bytes, empty_size, flags);
6489 space_info = __find_space_info(root->fs_info, flags);
6491 btrfs_err(root->fs_info, "No space info for %llu", flags);
6496 * If the space info is for both data and metadata it means we have a
6497 * small filesystem and we can't use the clustering stuff.
6499 if (btrfs_mixed_space_info(space_info))
6500 use_cluster = false;
6502 if (flags & BTRFS_BLOCK_GROUP_METADATA && use_cluster) {
6503 last_ptr = &root->fs_info->meta_alloc_cluster;
6504 if (!btrfs_test_opt(root, SSD))
6505 empty_cluster = 64 * 1024;
6508 if ((flags & BTRFS_BLOCK_GROUP_DATA) && use_cluster &&
6509 btrfs_test_opt(root, SSD)) {
6510 last_ptr = &root->fs_info->data_alloc_cluster;
6514 spin_lock(&last_ptr->lock);
6515 if (last_ptr->block_group)
6516 hint_byte = last_ptr->window_start;
6517 spin_unlock(&last_ptr->lock);
6520 search_start = max(search_start, first_logical_byte(root, 0));
6521 search_start = max(search_start, hint_byte);
6526 if (search_start == hint_byte) {
6527 block_group = btrfs_lookup_block_group(root->fs_info,
6530 * we don't want to use the block group if it doesn't match our
6531 * allocation bits, or if its not cached.
6533 * However if we are re-searching with an ideal block group
6534 * picked out then we don't care that the block group is cached.
6536 if (block_group && block_group_bits(block_group, flags) &&
6537 block_group->cached != BTRFS_CACHE_NO) {
6538 down_read(&space_info->groups_sem);
6539 if (list_empty(&block_group->list) ||
6542 * someone is removing this block group,
6543 * we can't jump into the have_block_group
6544 * target because our list pointers are not
6547 btrfs_put_block_group(block_group);
6548 up_read(&space_info->groups_sem);
6550 index = get_block_group_index(block_group);
6551 btrfs_lock_block_group(block_group, delalloc);
6552 goto have_block_group;
6554 } else if (block_group) {
6555 btrfs_put_block_group(block_group);
6559 have_caching_bg = false;
6560 down_read(&space_info->groups_sem);
6561 list_for_each_entry(block_group, &space_info->block_groups[index],
6566 btrfs_grab_block_group(block_group, delalloc);
6567 search_start = block_group->key.objectid;
6570 * this can happen if we end up cycling through all the
6571 * raid types, but we want to make sure we only allocate
6572 * for the proper type.
6574 if (!block_group_bits(block_group, flags)) {
6575 u64 extra = BTRFS_BLOCK_GROUP_DUP |
6576 BTRFS_BLOCK_GROUP_RAID1 |
6577 BTRFS_BLOCK_GROUP_RAID5 |
6578 BTRFS_BLOCK_GROUP_RAID6 |
6579 BTRFS_BLOCK_GROUP_RAID10;
6582 * if they asked for extra copies and this block group
6583 * doesn't provide them, bail. This does allow us to
6584 * fill raid0 from raid1.
6586 if ((flags & extra) && !(block_group->flags & extra))
6591 cached = block_group_cache_done(block_group);
6592 if (unlikely(!cached)) {
6593 ret = cache_block_group(block_group, 0);
6598 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
6600 if (unlikely(block_group->ro))
6604 * Ok we want to try and use the cluster allocator, so
6608 struct btrfs_block_group_cache *used_block_group;
6609 unsigned long aligned_cluster;
6611 * the refill lock keeps out other
6612 * people trying to start a new cluster
6614 used_block_group = btrfs_lock_cluster(block_group,
6617 if (!used_block_group)
6618 goto refill_cluster;
6620 if (used_block_group != block_group &&
6621 (used_block_group->ro ||
6622 !block_group_bits(used_block_group, flags)))
6623 goto release_cluster;
6625 offset = btrfs_alloc_from_cluster(used_block_group,
6628 used_block_group->key.objectid,
6631 /* we have a block, we're done */
6632 spin_unlock(&last_ptr->refill_lock);
6633 trace_btrfs_reserve_extent_cluster(root,
6635 search_start, num_bytes);
6636 if (used_block_group != block_group) {
6637 btrfs_release_block_group(block_group,
6639 block_group = used_block_group;
6644 WARN_ON(last_ptr->block_group != used_block_group);
6646 /* If we are on LOOP_NO_EMPTY_SIZE, we can't
6647 * set up a new clusters, so lets just skip it
6648 * and let the allocator find whatever block
6649 * it can find. If we reach this point, we
6650 * will have tried the cluster allocator
6651 * plenty of times and not have found
6652 * anything, so we are likely way too
6653 * fragmented for the clustering stuff to find
6656 * However, if the cluster is taken from the
6657 * current block group, release the cluster
6658 * first, so that we stand a better chance of
6659 * succeeding in the unclustered
6661 if (loop >= LOOP_NO_EMPTY_SIZE &&
6662 used_block_group != block_group) {
6663 spin_unlock(&last_ptr->refill_lock);
6664 btrfs_release_block_group(used_block_group,
6666 goto unclustered_alloc;
6670 * this cluster didn't work out, free it and
6673 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6675 if (used_block_group != block_group)
6676 btrfs_release_block_group(used_block_group,
6679 if (loop >= LOOP_NO_EMPTY_SIZE) {
6680 spin_unlock(&last_ptr->refill_lock);
6681 goto unclustered_alloc;
6684 aligned_cluster = max_t(unsigned long,
6685 empty_cluster + empty_size,
6686 block_group->full_stripe_len);
6688 /* allocate a cluster in this block group */
6689 ret = btrfs_find_space_cluster(root, block_group,
6690 last_ptr, search_start,
6695 * now pull our allocation out of this
6698 offset = btrfs_alloc_from_cluster(block_group,
6704 /* we found one, proceed */
6705 spin_unlock(&last_ptr->refill_lock);
6706 trace_btrfs_reserve_extent_cluster(root,
6707 block_group, search_start,
6711 } else if (!cached && loop > LOOP_CACHING_NOWAIT
6712 && !failed_cluster_refill) {
6713 spin_unlock(&last_ptr->refill_lock);
6715 failed_cluster_refill = true;
6716 wait_block_group_cache_progress(block_group,
6717 num_bytes + empty_cluster + empty_size);
6718 goto have_block_group;
6722 * at this point we either didn't find a cluster
6723 * or we weren't able to allocate a block from our
6724 * cluster. Free the cluster we've been trying
6725 * to use, and go to the next block group
6727 btrfs_return_cluster_to_free_space(NULL, last_ptr);
6728 spin_unlock(&last_ptr->refill_lock);
6733 spin_lock(&block_group->free_space_ctl->tree_lock);
6735 block_group->free_space_ctl->free_space <
6736 num_bytes + empty_cluster + empty_size) {
6737 if (block_group->free_space_ctl->free_space >
6740 block_group->free_space_ctl->free_space;
6741 spin_unlock(&block_group->free_space_ctl->tree_lock);
6744 spin_unlock(&block_group->free_space_ctl->tree_lock);
6746 offset = btrfs_find_space_for_alloc(block_group, search_start,
6747 num_bytes, empty_size,
6750 * If we didn't find a chunk, and we haven't failed on this
6751 * block group before, and this block group is in the middle of
6752 * caching and we are ok with waiting, then go ahead and wait
6753 * for progress to be made, and set failed_alloc to true.
6755 * If failed_alloc is true then we've already waited on this
6756 * block group once and should move on to the next block group.
6758 if (!offset && !failed_alloc && !cached &&
6759 loop > LOOP_CACHING_NOWAIT) {
6760 wait_block_group_cache_progress(block_group,
6761 num_bytes + empty_size);
6762 failed_alloc = true;
6763 goto have_block_group;
6764 } else if (!offset) {
6766 have_caching_bg = true;
6770 search_start = ALIGN(offset, root->stripesize);
6772 /* move on to the next group */
6773 if (search_start + num_bytes >
6774 block_group->key.objectid + block_group->key.offset) {
6775 btrfs_add_free_space(block_group, offset, num_bytes);
6779 if (offset < search_start)
6780 btrfs_add_free_space(block_group, offset,
6781 search_start - offset);
6782 BUG_ON(offset > search_start);
6784 ret = btrfs_update_reserved_bytes(block_group, num_bytes,
6785 alloc_type, delalloc);
6786 if (ret == -EAGAIN) {
6787 btrfs_add_free_space(block_group, offset, num_bytes);
6791 /* we are all good, lets return */
6792 ins->objectid = search_start;
6793 ins->offset = num_bytes;
6795 trace_btrfs_reserve_extent(orig_root, block_group,
6796 search_start, num_bytes);
6797 btrfs_release_block_group(block_group, delalloc);
6800 failed_cluster_refill = false;
6801 failed_alloc = false;
6802 BUG_ON(index != get_block_group_index(block_group));
6803 btrfs_release_block_group(block_group, delalloc);
6805 up_read(&space_info->groups_sem);
6807 if (!ins->objectid && loop >= LOOP_CACHING_WAIT && have_caching_bg)
6810 if (!ins->objectid && ++index < BTRFS_NR_RAID_TYPES)
6814 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
6815 * caching kthreads as we move along
6816 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
6817 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
6818 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
6821 if (!ins->objectid && loop < LOOP_NO_EMPTY_SIZE) {
6824 if (loop == LOOP_ALLOC_CHUNK) {
6825 struct btrfs_trans_handle *trans;
6828 trans = current->journal_info;
6832 trans = btrfs_join_transaction(root);
6834 if (IS_ERR(trans)) {
6835 ret = PTR_ERR(trans);
6839 ret = do_chunk_alloc(trans, root, flags,
6842 * Do not bail out on ENOSPC since we
6843 * can do more things.
6845 if (ret < 0 && ret != -ENOSPC)
6846 btrfs_abort_transaction(trans,
6851 btrfs_end_transaction(trans, root);
6856 if (loop == LOOP_NO_EMPTY_SIZE) {
6862 } else if (!ins->objectid) {
6864 } else if (ins->objectid) {
6869 ins->offset = max_extent_size;
6873 static void dump_space_info(struct btrfs_space_info *info, u64 bytes,
6874 int dump_block_groups)
6876 struct btrfs_block_group_cache *cache;
6879 spin_lock(&info->lock);
6880 printk(KERN_INFO "BTRFS: space_info %llu has %llu free, is %sfull\n",
6882 info->total_bytes - info->bytes_used - info->bytes_pinned -
6883 info->bytes_reserved - info->bytes_readonly,
6884 (info->full) ? "" : "not ");
6885 printk(KERN_INFO "BTRFS: space_info total=%llu, used=%llu, pinned=%llu, "
6886 "reserved=%llu, may_use=%llu, readonly=%llu\n",
6887 info->total_bytes, info->bytes_used, info->bytes_pinned,
6888 info->bytes_reserved, info->bytes_may_use,
6889 info->bytes_readonly);
6890 spin_unlock(&info->lock);
6892 if (!dump_block_groups)
6895 down_read(&info->groups_sem);
6897 list_for_each_entry(cache, &info->block_groups[index], list) {
6898 spin_lock(&cache->lock);
6899 printk(KERN_INFO "BTRFS: "
6900 "block group %llu has %llu bytes, "
6901 "%llu used %llu pinned %llu reserved %s\n",
6902 cache->key.objectid, cache->key.offset,
6903 btrfs_block_group_used(&cache->item), cache->pinned,
6904 cache->reserved, cache->ro ? "[readonly]" : "");
6905 btrfs_dump_free_space(cache, bytes);
6906 spin_unlock(&cache->lock);
6908 if (++index < BTRFS_NR_RAID_TYPES)
6910 up_read(&info->groups_sem);
6913 int btrfs_reserve_extent(struct btrfs_root *root,
6914 u64 num_bytes, u64 min_alloc_size,
6915 u64 empty_size, u64 hint_byte,
6916 struct btrfs_key *ins, int is_data, int delalloc)
6918 bool final_tried = false;
6922 flags = btrfs_get_alloc_profile(root, is_data);
6924 WARN_ON(num_bytes < root->sectorsize);
6925 ret = find_free_extent(root, num_bytes, empty_size, hint_byte, ins,
6928 if (ret == -ENOSPC) {
6929 if (!final_tried && ins->offset) {
6930 num_bytes = min(num_bytes >> 1, ins->offset);
6931 num_bytes = round_down(num_bytes, root->sectorsize);
6932 num_bytes = max(num_bytes, min_alloc_size);
6933 if (num_bytes == min_alloc_size)
6936 } else if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
6937 struct btrfs_space_info *sinfo;
6939 sinfo = __find_space_info(root->fs_info, flags);
6940 btrfs_err(root->fs_info, "allocation failed flags %llu, wanted %llu",
6943 dump_space_info(sinfo, num_bytes, 1);
6950 static int __btrfs_free_reserved_extent(struct btrfs_root *root,
6952 int pin, int delalloc)
6954 struct btrfs_block_group_cache *cache;
6957 cache = btrfs_lookup_block_group(root->fs_info, start);
6959 btrfs_err(root->fs_info, "Unable to find block group for %llu",
6964 if (btrfs_test_opt(root, DISCARD))
6965 ret = btrfs_discard_extent(root, start, len, NULL);
6968 pin_down_extent(root, cache, start, len, 1);
6970 btrfs_add_free_space(cache, start, len);
6971 btrfs_update_reserved_bytes(cache, len, RESERVE_FREE, delalloc);
6973 btrfs_put_block_group(cache);
6975 trace_btrfs_reserved_extent_free(root, start, len);
6980 int btrfs_free_reserved_extent(struct btrfs_root *root,
6981 u64 start, u64 len, int delalloc)
6983 return __btrfs_free_reserved_extent(root, start, len, 0, delalloc);
6986 int btrfs_free_and_pin_reserved_extent(struct btrfs_root *root,
6989 return __btrfs_free_reserved_extent(root, start, len, 1, 0);
6992 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
6993 struct btrfs_root *root,
6994 u64 parent, u64 root_objectid,
6995 u64 flags, u64 owner, u64 offset,
6996 struct btrfs_key *ins, int ref_mod)
6999 struct btrfs_fs_info *fs_info = root->fs_info;
7000 struct btrfs_extent_item *extent_item;
7001 struct btrfs_extent_inline_ref *iref;
7002 struct btrfs_path *path;
7003 struct extent_buffer *leaf;
7008 type = BTRFS_SHARED_DATA_REF_KEY;
7010 type = BTRFS_EXTENT_DATA_REF_KEY;
7012 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
7014 path = btrfs_alloc_path();
7018 path->leave_spinning = 1;
7019 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7022 btrfs_free_path(path);
7026 leaf = path->nodes[0];
7027 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7028 struct btrfs_extent_item);
7029 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
7030 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7031 btrfs_set_extent_flags(leaf, extent_item,
7032 flags | BTRFS_EXTENT_FLAG_DATA);
7034 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7035 btrfs_set_extent_inline_ref_type(leaf, iref, type);
7037 struct btrfs_shared_data_ref *ref;
7038 ref = (struct btrfs_shared_data_ref *)(iref + 1);
7039 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7040 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
7042 struct btrfs_extent_data_ref *ref;
7043 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
7044 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
7045 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
7046 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
7047 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
7050 btrfs_mark_buffer_dirty(path->nodes[0]);
7051 btrfs_free_path(path);
7053 /* Always set parent to 0 here since its exclusive anyway. */
7054 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7055 ins->objectid, ins->offset,
7056 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7060 ret = update_block_group(root, ins->objectid, ins->offset, 1);
7061 if (ret) { /* -ENOENT, logic error */
7062 btrfs_err(fs_info, "update block group failed for %llu %llu",
7063 ins->objectid, ins->offset);
7066 trace_btrfs_reserved_extent_alloc(root, ins->objectid, ins->offset);
7070 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
7071 struct btrfs_root *root,
7072 u64 parent, u64 root_objectid,
7073 u64 flags, struct btrfs_disk_key *key,
7074 int level, struct btrfs_key *ins,
7078 struct btrfs_fs_info *fs_info = root->fs_info;
7079 struct btrfs_extent_item *extent_item;
7080 struct btrfs_tree_block_info *block_info;
7081 struct btrfs_extent_inline_ref *iref;
7082 struct btrfs_path *path;
7083 struct extent_buffer *leaf;
7084 u32 size = sizeof(*extent_item) + sizeof(*iref);
7085 u64 num_bytes = ins->offset;
7086 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7089 if (!skinny_metadata)
7090 size += sizeof(*block_info);
7092 path = btrfs_alloc_path();
7094 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7099 path->leave_spinning = 1;
7100 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
7103 btrfs_free_and_pin_reserved_extent(root, ins->objectid,
7105 btrfs_free_path(path);
7109 leaf = path->nodes[0];
7110 extent_item = btrfs_item_ptr(leaf, path->slots[0],
7111 struct btrfs_extent_item);
7112 btrfs_set_extent_refs(leaf, extent_item, 1);
7113 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
7114 btrfs_set_extent_flags(leaf, extent_item,
7115 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
7117 if (skinny_metadata) {
7118 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
7119 num_bytes = root->nodesize;
7121 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
7122 btrfs_set_tree_block_key(leaf, block_info, key);
7123 btrfs_set_tree_block_level(leaf, block_info, level);
7124 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
7128 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
7129 btrfs_set_extent_inline_ref_type(leaf, iref,
7130 BTRFS_SHARED_BLOCK_REF_KEY);
7131 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
7133 btrfs_set_extent_inline_ref_type(leaf, iref,
7134 BTRFS_TREE_BLOCK_REF_KEY);
7135 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
7138 btrfs_mark_buffer_dirty(leaf);
7139 btrfs_free_path(path);
7142 ret = btrfs_qgroup_record_ref(trans, fs_info, root_objectid,
7143 ins->objectid, num_bytes,
7144 BTRFS_QGROUP_OPER_ADD_EXCL, 0);
7149 ret = update_block_group(root, ins->objectid, root->nodesize, 1);
7150 if (ret) { /* -ENOENT, logic error */
7151 btrfs_err(fs_info, "update block group failed for %llu %llu",
7152 ins->objectid, ins->offset);
7156 trace_btrfs_reserved_extent_alloc(root, ins->objectid, root->nodesize);
7160 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
7161 struct btrfs_root *root,
7162 u64 root_objectid, u64 owner,
7163 u64 offset, struct btrfs_key *ins)
7167 BUG_ON(root_objectid == BTRFS_TREE_LOG_OBJECTID);
7169 ret = btrfs_add_delayed_data_ref(root->fs_info, trans, ins->objectid,
7171 root_objectid, owner, offset,
7172 BTRFS_ADD_DELAYED_EXTENT, NULL, 0);
7177 * this is used by the tree logging recovery code. It records that
7178 * an extent has been allocated and makes sure to clear the free
7179 * space cache bits as well
7181 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
7182 struct btrfs_root *root,
7183 u64 root_objectid, u64 owner, u64 offset,
7184 struct btrfs_key *ins)
7187 struct btrfs_block_group_cache *block_group;
7190 * Mixed block groups will exclude before processing the log so we only
7191 * need to do the exlude dance if this fs isn't mixed.
7193 if (!btrfs_fs_incompat(root->fs_info, MIXED_GROUPS)) {
7194 ret = __exclude_logged_extent(root, ins->objectid, ins->offset);
7199 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
7203 ret = btrfs_update_reserved_bytes(block_group, ins->offset,
7204 RESERVE_ALLOC_NO_ACCOUNT, 0);
7205 BUG_ON(ret); /* logic error */
7206 ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
7207 0, owner, offset, ins, 1);
7208 btrfs_put_block_group(block_group);
7212 static struct extent_buffer *
7213 btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
7214 u64 bytenr, u32 blocksize, int level)
7216 struct extent_buffer *buf;
7218 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
7220 return ERR_PTR(-ENOMEM);
7221 btrfs_set_header_generation(buf, trans->transid);
7222 btrfs_set_buffer_lockdep_class(root->root_key.objectid, buf, level);
7223 btrfs_tree_lock(buf);
7224 clean_tree_block(trans, root, buf);
7225 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
7227 btrfs_set_lock_blocking(buf);
7228 btrfs_set_buffer_uptodate(buf);
7230 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
7231 buf->log_index = root->log_transid % 2;
7233 * we allow two log transactions at a time, use different
7234 * EXENT bit to differentiate dirty pages.
7236 if (buf->log_index == 0)
7237 set_extent_dirty(&root->dirty_log_pages, buf->start,
7238 buf->start + buf->len - 1, GFP_NOFS);
7240 set_extent_new(&root->dirty_log_pages, buf->start,
7241 buf->start + buf->len - 1, GFP_NOFS);
7243 buf->log_index = -1;
7244 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
7245 buf->start + buf->len - 1, GFP_NOFS);
7247 trans->blocks_used++;
7248 /* this returns a buffer locked for blocking */
7252 static struct btrfs_block_rsv *
7253 use_block_rsv(struct btrfs_trans_handle *trans,
7254 struct btrfs_root *root, u32 blocksize)
7256 struct btrfs_block_rsv *block_rsv;
7257 struct btrfs_block_rsv *global_rsv = &root->fs_info->global_block_rsv;
7259 bool global_updated = false;
7261 block_rsv = get_block_rsv(trans, root);
7263 if (unlikely(block_rsv->size == 0))
7266 ret = block_rsv_use_bytes(block_rsv, blocksize);
7270 if (block_rsv->failfast)
7271 return ERR_PTR(ret);
7273 if (block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL && !global_updated) {
7274 global_updated = true;
7275 update_global_block_rsv(root->fs_info);
7279 if (btrfs_test_opt(root, ENOSPC_DEBUG)) {
7280 static DEFINE_RATELIMIT_STATE(_rs,
7281 DEFAULT_RATELIMIT_INTERVAL * 10,
7282 /*DEFAULT_RATELIMIT_BURST*/ 1);
7283 if (__ratelimit(&_rs))
7285 "BTRFS: block rsv returned %d\n", ret);
7288 ret = reserve_metadata_bytes(root, block_rsv, blocksize,
7289 BTRFS_RESERVE_NO_FLUSH);
7293 * If we couldn't reserve metadata bytes try and use some from
7294 * the global reserve if its space type is the same as the global
7297 if (block_rsv->type != BTRFS_BLOCK_RSV_GLOBAL &&
7298 block_rsv->space_info == global_rsv->space_info) {
7299 ret = block_rsv_use_bytes(global_rsv, blocksize);
7303 return ERR_PTR(ret);
7306 static void unuse_block_rsv(struct btrfs_fs_info *fs_info,
7307 struct btrfs_block_rsv *block_rsv, u32 blocksize)
7309 block_rsv_add_bytes(block_rsv, blocksize, 0);
7310 block_rsv_release_bytes(fs_info, block_rsv, NULL, 0);
7314 * finds a free extent and does all the dirty work required for allocation
7315 * returns the key for the extent through ins, and a tree buffer for
7316 * the first block of the extent through buf.
7318 * returns the tree buffer or NULL.
7320 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
7321 struct btrfs_root *root,
7322 u64 parent, u64 root_objectid,
7323 struct btrfs_disk_key *key, int level,
7324 u64 hint, u64 empty_size)
7326 struct btrfs_key ins;
7327 struct btrfs_block_rsv *block_rsv;
7328 struct extent_buffer *buf;
7331 u32 blocksize = root->nodesize;
7332 bool skinny_metadata = btrfs_fs_incompat(root->fs_info,
7335 if (btrfs_test_is_dummy_root(root)) {
7336 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
7339 root->alloc_bytenr += blocksize;
7343 block_rsv = use_block_rsv(trans, root, blocksize);
7344 if (IS_ERR(block_rsv))
7345 return ERR_CAST(block_rsv);
7347 ret = btrfs_reserve_extent(root, blocksize, blocksize,
7348 empty_size, hint, &ins, 0, 0);
7350 unuse_block_rsv(root->fs_info, block_rsv, blocksize);
7351 return ERR_PTR(ret);
7354 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
7356 BUG_ON(IS_ERR(buf)); /* -ENOMEM */
7358 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
7360 parent = ins.objectid;
7361 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
7365 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
7366 struct btrfs_delayed_extent_op *extent_op;
7367 extent_op = btrfs_alloc_delayed_extent_op();
7368 BUG_ON(!extent_op); /* -ENOMEM */
7370 memcpy(&extent_op->key, key, sizeof(extent_op->key));
7372 memset(&extent_op->key, 0, sizeof(extent_op->key));
7373 extent_op->flags_to_set = flags;
7374 if (skinny_metadata)
7375 extent_op->update_key = 0;
7377 extent_op->update_key = 1;
7378 extent_op->update_flags = 1;
7379 extent_op->is_data = 0;
7380 extent_op->level = level;
7382 ret = btrfs_add_delayed_tree_ref(root->fs_info, trans,
7384 ins.offset, parent, root_objectid,
7385 level, BTRFS_ADD_DELAYED_EXTENT,
7387 BUG_ON(ret); /* -ENOMEM */
7392 struct walk_control {
7393 u64 refs[BTRFS_MAX_LEVEL];
7394 u64 flags[BTRFS_MAX_LEVEL];
7395 struct btrfs_key update_progress;
7406 #define DROP_REFERENCE 1
7407 #define UPDATE_BACKREF 2
7409 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
7410 struct btrfs_root *root,
7411 struct walk_control *wc,
7412 struct btrfs_path *path)
7420 struct btrfs_key key;
7421 struct extent_buffer *eb;
7426 if (path->slots[wc->level] < wc->reada_slot) {
7427 wc->reada_count = wc->reada_count * 2 / 3;
7428 wc->reada_count = max(wc->reada_count, 2);
7430 wc->reada_count = wc->reada_count * 3 / 2;
7431 wc->reada_count = min_t(int, wc->reada_count,
7432 BTRFS_NODEPTRS_PER_BLOCK(root));
7435 eb = path->nodes[wc->level];
7436 nritems = btrfs_header_nritems(eb);
7437 blocksize = root->nodesize;
7439 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
7440 if (nread >= wc->reada_count)
7444 bytenr = btrfs_node_blockptr(eb, slot);
7445 generation = btrfs_node_ptr_generation(eb, slot);
7447 if (slot == path->slots[wc->level])
7450 if (wc->stage == UPDATE_BACKREF &&
7451 generation <= root->root_key.offset)
7454 /* We don't lock the tree block, it's OK to be racy here */
7455 ret = btrfs_lookup_extent_info(trans, root, bytenr,
7456 wc->level - 1, 1, &refs,
7458 /* We don't care about errors in readahead. */
7463 if (wc->stage == DROP_REFERENCE) {
7467 if (wc->level == 1 &&
7468 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7470 if (!wc->update_ref ||
7471 generation <= root->root_key.offset)
7473 btrfs_node_key_to_cpu(eb, &key, slot);
7474 ret = btrfs_comp_cpu_keys(&key,
7475 &wc->update_progress);
7479 if (wc->level == 1 &&
7480 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7484 readahead_tree_block(root, bytenr, blocksize);
7487 wc->reada_slot = slot;
7490 static int account_leaf_items(struct btrfs_trans_handle *trans,
7491 struct btrfs_root *root,
7492 struct extent_buffer *eb)
7494 int nr = btrfs_header_nritems(eb);
7495 int i, extent_type, ret;
7496 struct btrfs_key key;
7497 struct btrfs_file_extent_item *fi;
7498 u64 bytenr, num_bytes;
7500 for (i = 0; i < nr; i++) {
7501 btrfs_item_key_to_cpu(eb, &key, i);
7503 if (key.type != BTRFS_EXTENT_DATA_KEY)
7506 fi = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
7507 /* filter out non qgroup-accountable extents */
7508 extent_type = btrfs_file_extent_type(eb, fi);
7510 if (extent_type == BTRFS_FILE_EXTENT_INLINE)
7513 bytenr = btrfs_file_extent_disk_bytenr(eb, fi);
7517 num_bytes = btrfs_file_extent_disk_num_bytes(eb, fi);
7519 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7522 BTRFS_QGROUP_OPER_SUB_SUBTREE, 0);
7530 * Walk up the tree from the bottom, freeing leaves and any interior
7531 * nodes which have had all slots visited. If a node (leaf or
7532 * interior) is freed, the node above it will have it's slot
7533 * incremented. The root node will never be freed.
7535 * At the end of this function, we should have a path which has all
7536 * slots incremented to the next position for a search. If we need to
7537 * read a new node it will be NULL and the node above it will have the
7538 * correct slot selected for a later read.
7540 * If we increment the root nodes slot counter past the number of
7541 * elements, 1 is returned to signal completion of the search.
7543 static int adjust_slots_upwards(struct btrfs_root *root,
7544 struct btrfs_path *path, int root_level)
7548 struct extent_buffer *eb;
7550 if (root_level == 0)
7553 while (level <= root_level) {
7554 eb = path->nodes[level];
7555 nr = btrfs_header_nritems(eb);
7556 path->slots[level]++;
7557 slot = path->slots[level];
7558 if (slot >= nr || level == 0) {
7560 * Don't free the root - we will detect this
7561 * condition after our loop and return a
7562 * positive value for caller to stop walking the tree.
7564 if (level != root_level) {
7565 btrfs_tree_unlock_rw(eb, path->locks[level]);
7566 path->locks[level] = 0;
7568 free_extent_buffer(eb);
7569 path->nodes[level] = NULL;
7570 path->slots[level] = 0;
7574 * We have a valid slot to walk back down
7575 * from. Stop here so caller can process these
7584 eb = path->nodes[root_level];
7585 if (path->slots[root_level] >= btrfs_header_nritems(eb))
7592 * root_eb is the subtree root and is locked before this function is called.
7594 static int account_shared_subtree(struct btrfs_trans_handle *trans,
7595 struct btrfs_root *root,
7596 struct extent_buffer *root_eb,
7602 struct extent_buffer *eb = root_eb;
7603 struct btrfs_path *path = NULL;
7605 BUG_ON(root_level < 0 || root_level > BTRFS_MAX_LEVEL);
7606 BUG_ON(root_eb == NULL);
7608 if (!root->fs_info->quota_enabled)
7611 if (!extent_buffer_uptodate(root_eb)) {
7612 ret = btrfs_read_buffer(root_eb, root_gen);
7617 if (root_level == 0) {
7618 ret = account_leaf_items(trans, root, root_eb);
7622 path = btrfs_alloc_path();
7627 * Walk down the tree. Missing extent blocks are filled in as
7628 * we go. Metadata is accounted every time we read a new
7631 * When we reach a leaf, we account for file extent items in it,
7632 * walk back up the tree (adjusting slot pointers as we go)
7633 * and restart the search process.
7635 extent_buffer_get(root_eb); /* For path */
7636 path->nodes[root_level] = root_eb;
7637 path->slots[root_level] = 0;
7638 path->locks[root_level] = 0; /* so release_path doesn't try to unlock */
7641 while (level >= 0) {
7642 if (path->nodes[level] == NULL) {
7647 /* We need to get child blockptr/gen from
7648 * parent before we can read it. */
7649 eb = path->nodes[level + 1];
7650 parent_slot = path->slots[level + 1];
7651 child_bytenr = btrfs_node_blockptr(eb, parent_slot);
7652 child_gen = btrfs_node_ptr_generation(eb, parent_slot);
7654 eb = read_tree_block(root, child_bytenr, child_gen);
7655 if (!eb || !extent_buffer_uptodate(eb)) {
7660 path->nodes[level] = eb;
7661 path->slots[level] = 0;
7663 btrfs_tree_read_lock(eb);
7664 btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
7665 path->locks[level] = BTRFS_READ_LOCK_BLOCKING;
7667 ret = btrfs_qgroup_record_ref(trans, root->fs_info,
7671 BTRFS_QGROUP_OPER_SUB_SUBTREE,
7679 ret = account_leaf_items(trans, root, path->nodes[level]);
7683 /* Nonzero return here means we completed our search */
7684 ret = adjust_slots_upwards(root, path, root_level);
7688 /* Restart search with new slots */
7697 btrfs_free_path(path);
7703 * helper to process tree block while walking down the tree.
7705 * when wc->stage == UPDATE_BACKREF, this function updates
7706 * back refs for pointers in the block.
7708 * NOTE: return value 1 means we should stop walking down.
7710 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
7711 struct btrfs_root *root,
7712 struct btrfs_path *path,
7713 struct walk_control *wc, int lookup_info)
7715 int level = wc->level;
7716 struct extent_buffer *eb = path->nodes[level];
7717 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
7720 if (wc->stage == UPDATE_BACKREF &&
7721 btrfs_header_owner(eb) != root->root_key.objectid)
7725 * when reference count of tree block is 1, it won't increase
7726 * again. once full backref flag is set, we never clear it.
7729 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
7730 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
7731 BUG_ON(!path->locks[level]);
7732 ret = btrfs_lookup_extent_info(trans, root,
7733 eb->start, level, 1,
7736 BUG_ON(ret == -ENOMEM);
7739 BUG_ON(wc->refs[level] == 0);
7742 if (wc->stage == DROP_REFERENCE) {
7743 if (wc->refs[level] > 1)
7746 if (path->locks[level] && !wc->keep_locks) {
7747 btrfs_tree_unlock_rw(eb, path->locks[level]);
7748 path->locks[level] = 0;
7753 /* wc->stage == UPDATE_BACKREF */
7754 if (!(wc->flags[level] & flag)) {
7755 BUG_ON(!path->locks[level]);
7756 ret = btrfs_inc_ref(trans, root, eb, 1);
7757 BUG_ON(ret); /* -ENOMEM */
7758 ret = btrfs_dec_ref(trans, root, eb, 0);
7759 BUG_ON(ret); /* -ENOMEM */
7760 ret = btrfs_set_disk_extent_flags(trans, root, eb->start,
7762 btrfs_header_level(eb), 0);
7763 BUG_ON(ret); /* -ENOMEM */
7764 wc->flags[level] |= flag;
7768 * the block is shared by multiple trees, so it's not good to
7769 * keep the tree lock
7771 if (path->locks[level] && level > 0) {
7772 btrfs_tree_unlock_rw(eb, path->locks[level]);
7773 path->locks[level] = 0;
7779 * helper to process tree block pointer.
7781 * when wc->stage == DROP_REFERENCE, this function checks
7782 * reference count of the block pointed to. if the block
7783 * is shared and we need update back refs for the subtree
7784 * rooted at the block, this function changes wc->stage to
7785 * UPDATE_BACKREF. if the block is shared and there is no
7786 * need to update back, this function drops the reference
7789 * NOTE: return value 1 means we should stop walking down.
7791 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
7792 struct btrfs_root *root,
7793 struct btrfs_path *path,
7794 struct walk_control *wc, int *lookup_info)
7800 struct btrfs_key key;
7801 struct extent_buffer *next;
7802 int level = wc->level;
7805 bool need_account = false;
7807 generation = btrfs_node_ptr_generation(path->nodes[level],
7808 path->slots[level]);
7810 * if the lower level block was created before the snapshot
7811 * was created, we know there is no need to update back refs
7814 if (wc->stage == UPDATE_BACKREF &&
7815 generation <= root->root_key.offset) {
7820 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
7821 blocksize = root->nodesize;
7823 next = btrfs_find_tree_block(root, bytenr);
7825 next = btrfs_find_create_tree_block(root, bytenr, blocksize);
7828 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
7832 btrfs_tree_lock(next);
7833 btrfs_set_lock_blocking(next);
7835 ret = btrfs_lookup_extent_info(trans, root, bytenr, level - 1, 1,
7836 &wc->refs[level - 1],
7837 &wc->flags[level - 1]);
7839 btrfs_tree_unlock(next);
7843 if (unlikely(wc->refs[level - 1] == 0)) {
7844 btrfs_err(root->fs_info, "Missing references.");
7849 if (wc->stage == DROP_REFERENCE) {
7850 if (wc->refs[level - 1] > 1) {
7851 need_account = true;
7853 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7856 if (!wc->update_ref ||
7857 generation <= root->root_key.offset)
7860 btrfs_node_key_to_cpu(path->nodes[level], &key,
7861 path->slots[level]);
7862 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
7866 wc->stage = UPDATE_BACKREF;
7867 wc->shared_level = level - 1;
7871 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
7875 if (!btrfs_buffer_uptodate(next, generation, 0)) {
7876 btrfs_tree_unlock(next);
7877 free_extent_buffer(next);
7883 if (reada && level == 1)
7884 reada_walk_down(trans, root, wc, path);
7885 next = read_tree_block(root, bytenr, generation);
7886 if (!next || !extent_buffer_uptodate(next)) {
7887 free_extent_buffer(next);
7890 btrfs_tree_lock(next);
7891 btrfs_set_lock_blocking(next);
7895 BUG_ON(level != btrfs_header_level(next));
7896 path->nodes[level] = next;
7897 path->slots[level] = 0;
7898 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
7904 wc->refs[level - 1] = 0;
7905 wc->flags[level - 1] = 0;
7906 if (wc->stage == DROP_REFERENCE) {
7907 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
7908 parent = path->nodes[level]->start;
7910 BUG_ON(root->root_key.objectid !=
7911 btrfs_header_owner(path->nodes[level]));
7916 ret = account_shared_subtree(trans, root, next,
7917 generation, level - 1);
7919 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
7920 "%d accounting shared subtree. Quota "
7921 "is out of sync, rescan required.\n",
7922 root->fs_info->sb->s_id, ret);
7925 ret = btrfs_free_extent(trans, root, bytenr, blocksize, parent,
7926 root->root_key.objectid, level - 1, 0, 0);
7927 BUG_ON(ret); /* -ENOMEM */
7929 btrfs_tree_unlock(next);
7930 free_extent_buffer(next);
7936 * helper to process tree block while walking up the tree.
7938 * when wc->stage == DROP_REFERENCE, this function drops
7939 * reference count on the block.
7941 * when wc->stage == UPDATE_BACKREF, this function changes
7942 * wc->stage back to DROP_REFERENCE if we changed wc->stage
7943 * to UPDATE_BACKREF previously while processing the block.
7945 * NOTE: return value 1 means we should stop walking up.
7947 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
7948 struct btrfs_root *root,
7949 struct btrfs_path *path,
7950 struct walk_control *wc)
7953 int level = wc->level;
7954 struct extent_buffer *eb = path->nodes[level];
7957 if (wc->stage == UPDATE_BACKREF) {
7958 BUG_ON(wc->shared_level < level);
7959 if (level < wc->shared_level)
7962 ret = find_next_key(path, level + 1, &wc->update_progress);
7966 wc->stage = DROP_REFERENCE;
7967 wc->shared_level = -1;
7968 path->slots[level] = 0;
7971 * check reference count again if the block isn't locked.
7972 * we should start walking down the tree again if reference
7975 if (!path->locks[level]) {
7977 btrfs_tree_lock(eb);
7978 btrfs_set_lock_blocking(eb);
7979 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
7981 ret = btrfs_lookup_extent_info(trans, root,
7982 eb->start, level, 1,
7986 btrfs_tree_unlock_rw(eb, path->locks[level]);
7987 path->locks[level] = 0;
7990 BUG_ON(wc->refs[level] == 0);
7991 if (wc->refs[level] == 1) {
7992 btrfs_tree_unlock_rw(eb, path->locks[level]);
7993 path->locks[level] = 0;
7999 /* wc->stage == DROP_REFERENCE */
8000 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
8002 if (wc->refs[level] == 1) {
8004 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8005 ret = btrfs_dec_ref(trans, root, eb, 1);
8007 ret = btrfs_dec_ref(trans, root, eb, 0);
8008 BUG_ON(ret); /* -ENOMEM */
8009 ret = account_leaf_items(trans, root, eb);
8011 printk_ratelimited(KERN_ERR "BTRFS: %s Error "
8012 "%d accounting leaf items. Quota "
8013 "is out of sync, rescan required.\n",
8014 root->fs_info->sb->s_id, ret);
8017 /* make block locked assertion in clean_tree_block happy */
8018 if (!path->locks[level] &&
8019 btrfs_header_generation(eb) == trans->transid) {
8020 btrfs_tree_lock(eb);
8021 btrfs_set_lock_blocking(eb);
8022 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8024 clean_tree_block(trans, root, eb);
8027 if (eb == root->node) {
8028 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8031 BUG_ON(root->root_key.objectid !=
8032 btrfs_header_owner(eb));
8034 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
8035 parent = path->nodes[level + 1]->start;
8037 BUG_ON(root->root_key.objectid !=
8038 btrfs_header_owner(path->nodes[level + 1]));
8041 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
8043 wc->refs[level] = 0;
8044 wc->flags[level] = 0;
8048 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
8049 struct btrfs_root *root,
8050 struct btrfs_path *path,
8051 struct walk_control *wc)
8053 int level = wc->level;
8054 int lookup_info = 1;
8057 while (level >= 0) {
8058 ret = walk_down_proc(trans, root, path, wc, lookup_info);
8065 if (path->slots[level] >=
8066 btrfs_header_nritems(path->nodes[level]))
8069 ret = do_walk_down(trans, root, path, wc, &lookup_info);
8071 path->slots[level]++;
8080 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
8081 struct btrfs_root *root,
8082 struct btrfs_path *path,
8083 struct walk_control *wc, int max_level)
8085 int level = wc->level;
8088 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
8089 while (level < max_level && path->nodes[level]) {
8091 if (path->slots[level] + 1 <
8092 btrfs_header_nritems(path->nodes[level])) {
8093 path->slots[level]++;
8096 ret = walk_up_proc(trans, root, path, wc);
8100 if (path->locks[level]) {
8101 btrfs_tree_unlock_rw(path->nodes[level],
8102 path->locks[level]);
8103 path->locks[level] = 0;
8105 free_extent_buffer(path->nodes[level]);
8106 path->nodes[level] = NULL;
8114 * drop a subvolume tree.
8116 * this function traverses the tree freeing any blocks that only
8117 * referenced by the tree.
8119 * when a shared tree block is found. this function decreases its
8120 * reference count by one. if update_ref is true, this function
8121 * also make sure backrefs for the shared block and all lower level
8122 * blocks are properly updated.
8124 * If called with for_reloc == 0, may exit early with -EAGAIN
8126 int btrfs_drop_snapshot(struct btrfs_root *root,
8127 struct btrfs_block_rsv *block_rsv, int update_ref,
8130 struct btrfs_path *path;
8131 struct btrfs_trans_handle *trans;
8132 struct btrfs_root *tree_root = root->fs_info->tree_root;
8133 struct btrfs_root_item *root_item = &root->root_item;
8134 struct walk_control *wc;
8135 struct btrfs_key key;
8139 bool root_dropped = false;
8141 btrfs_debug(root->fs_info, "Drop subvolume %llu", root->objectid);
8143 path = btrfs_alloc_path();
8149 wc = kzalloc(sizeof(*wc), GFP_NOFS);
8151 btrfs_free_path(path);
8156 trans = btrfs_start_transaction(tree_root, 0);
8157 if (IS_ERR(trans)) {
8158 err = PTR_ERR(trans);
8163 trans->block_rsv = block_rsv;
8165 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
8166 level = btrfs_header_level(root->node);
8167 path->nodes[level] = btrfs_lock_root_node(root);
8168 btrfs_set_lock_blocking(path->nodes[level]);
8169 path->slots[level] = 0;
8170 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8171 memset(&wc->update_progress, 0,
8172 sizeof(wc->update_progress));
8174 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
8175 memcpy(&wc->update_progress, &key,
8176 sizeof(wc->update_progress));
8178 level = root_item->drop_level;
8180 path->lowest_level = level;
8181 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
8182 path->lowest_level = 0;
8190 * unlock our path, this is safe because only this
8191 * function is allowed to delete this snapshot
8193 btrfs_unlock_up_safe(path, 0);
8195 level = btrfs_header_level(root->node);
8197 btrfs_tree_lock(path->nodes[level]);
8198 btrfs_set_lock_blocking(path->nodes[level]);
8199 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8201 ret = btrfs_lookup_extent_info(trans, root,
8202 path->nodes[level]->start,
8203 level, 1, &wc->refs[level],
8209 BUG_ON(wc->refs[level] == 0);
8211 if (level == root_item->drop_level)
8214 btrfs_tree_unlock(path->nodes[level]);
8215 path->locks[level] = 0;
8216 WARN_ON(wc->refs[level] != 1);
8222 wc->shared_level = -1;
8223 wc->stage = DROP_REFERENCE;
8224 wc->update_ref = update_ref;
8226 wc->for_reloc = for_reloc;
8227 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
8231 ret = walk_down_tree(trans, root, path, wc);
8237 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
8244 BUG_ON(wc->stage != DROP_REFERENCE);
8248 if (wc->stage == DROP_REFERENCE) {
8250 btrfs_node_key(path->nodes[level],
8251 &root_item->drop_progress,
8252 path->slots[level]);
8253 root_item->drop_level = level;
8256 BUG_ON(wc->level == 0);
8257 if (btrfs_should_end_transaction(trans, tree_root) ||
8258 (!for_reloc && btrfs_need_cleaner_sleep(root))) {
8259 ret = btrfs_update_root(trans, tree_root,
8263 btrfs_abort_transaction(trans, tree_root, ret);
8269 * Qgroup update accounting is run from
8270 * delayed ref handling. This usually works
8271 * out because delayed refs are normally the
8272 * only way qgroup updates are added. However,
8273 * we may have added updates during our tree
8274 * walk so run qgroups here to make sure we
8275 * don't lose any updates.
8277 ret = btrfs_delayed_qgroup_accounting(trans,
8280 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8281 "running qgroup updates "
8282 "during snapshot delete. "
8283 "Quota is out of sync, "
8284 "rescan required.\n", ret);
8286 btrfs_end_transaction_throttle(trans, tree_root);
8287 if (!for_reloc && btrfs_need_cleaner_sleep(root)) {
8288 pr_debug("BTRFS: drop snapshot early exit\n");
8293 trans = btrfs_start_transaction(tree_root, 0);
8294 if (IS_ERR(trans)) {
8295 err = PTR_ERR(trans);
8299 trans->block_rsv = block_rsv;
8302 btrfs_release_path(path);
8306 ret = btrfs_del_root(trans, tree_root, &root->root_key);
8308 btrfs_abort_transaction(trans, tree_root, ret);
8312 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
8313 ret = btrfs_find_root(tree_root, &root->root_key, path,
8316 btrfs_abort_transaction(trans, tree_root, ret);
8319 } else if (ret > 0) {
8320 /* if we fail to delete the orphan item this time
8321 * around, it'll get picked up the next time.
8323 * The most common failure here is just -ENOENT.
8325 btrfs_del_orphan_item(trans, tree_root,
8326 root->root_key.objectid);
8330 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state)) {
8331 btrfs_drop_and_free_fs_root(tree_root->fs_info, root);
8333 free_extent_buffer(root->node);
8334 free_extent_buffer(root->commit_root);
8335 btrfs_put_fs_root(root);
8337 root_dropped = true;
8339 ret = btrfs_delayed_qgroup_accounting(trans, tree_root->fs_info);
8341 printk_ratelimited(KERN_ERR "BTRFS: Failure %d "
8342 "running qgroup updates "
8343 "during snapshot delete. "
8344 "Quota is out of sync, "
8345 "rescan required.\n", ret);
8347 btrfs_end_transaction_throttle(trans, tree_root);
8350 btrfs_free_path(path);
8353 * So if we need to stop dropping the snapshot for whatever reason we
8354 * need to make sure to add it back to the dead root list so that we
8355 * keep trying to do the work later. This also cleans up roots if we
8356 * don't have it in the radix (like when we recover after a power fail
8357 * or unmount) so we don't leak memory.
8359 if (!for_reloc && root_dropped == false)
8360 btrfs_add_dead_root(root);
8361 if (err && err != -EAGAIN)
8362 btrfs_std_error(root->fs_info, err);
8367 * drop subtree rooted at tree block 'node'.
8369 * NOTE: this function will unlock and release tree block 'node'
8370 * only used by relocation code
8372 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
8373 struct btrfs_root *root,
8374 struct extent_buffer *node,
8375 struct extent_buffer *parent)
8377 struct btrfs_path *path;
8378 struct walk_control *wc;
8384 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
8386 path = btrfs_alloc_path();
8390 wc = kzalloc(sizeof(*wc), GFP_NOFS);
8392 btrfs_free_path(path);
8396 btrfs_assert_tree_locked(parent);
8397 parent_level = btrfs_header_level(parent);
8398 extent_buffer_get(parent);
8399 path->nodes[parent_level] = parent;
8400 path->slots[parent_level] = btrfs_header_nritems(parent);
8402 btrfs_assert_tree_locked(node);
8403 level = btrfs_header_level(node);
8404 path->nodes[level] = node;
8405 path->slots[level] = 0;
8406 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
8408 wc->refs[parent_level] = 1;
8409 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
8411 wc->shared_level = -1;
8412 wc->stage = DROP_REFERENCE;
8416 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(root);
8419 wret = walk_down_tree(trans, root, path, wc);
8425 wret = walk_up_tree(trans, root, path, wc, parent_level);
8433 btrfs_free_path(path);
8437 static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
8443 * if restripe for this chunk_type is on pick target profile and
8444 * return, otherwise do the usual balance
8446 stripped = get_restripe_target(root->fs_info, flags);
8448 return extended_to_chunk(stripped);
8450 num_devices = root->fs_info->fs_devices->rw_devices;
8452 stripped = BTRFS_BLOCK_GROUP_RAID0 |
8453 BTRFS_BLOCK_GROUP_RAID5 | BTRFS_BLOCK_GROUP_RAID6 |
8454 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
8456 if (num_devices == 1) {
8457 stripped |= BTRFS_BLOCK_GROUP_DUP;
8458 stripped = flags & ~stripped;
8460 /* turn raid0 into single device chunks */
8461 if (flags & BTRFS_BLOCK_GROUP_RAID0)
8464 /* turn mirroring into duplication */
8465 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
8466 BTRFS_BLOCK_GROUP_RAID10))
8467 return stripped | BTRFS_BLOCK_GROUP_DUP;
8469 /* they already had raid on here, just return */
8470 if (flags & stripped)
8473 stripped |= BTRFS_BLOCK_GROUP_DUP;
8474 stripped = flags & ~stripped;
8476 /* switch duplicated blocks with raid1 */
8477 if (flags & BTRFS_BLOCK_GROUP_DUP)
8478 return stripped | BTRFS_BLOCK_GROUP_RAID1;
8480 /* this is drive concat, leave it alone */
8486 static int set_block_group_ro(struct btrfs_block_group_cache *cache, int force)
8488 struct btrfs_space_info *sinfo = cache->space_info;
8490 u64 min_allocable_bytes;
8495 * We need some metadata space and system metadata space for
8496 * allocating chunks in some corner cases until we force to set
8497 * it to be readonly.
8500 (BTRFS_BLOCK_GROUP_SYSTEM | BTRFS_BLOCK_GROUP_METADATA)) &&
8502 min_allocable_bytes = 1 * 1024 * 1024;
8504 min_allocable_bytes = 0;
8506 spin_lock(&sinfo->lock);
8507 spin_lock(&cache->lock);
8514 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8515 cache->bytes_super - btrfs_block_group_used(&cache->item);
8517 if (sinfo->bytes_used + sinfo->bytes_reserved + sinfo->bytes_pinned +
8518 sinfo->bytes_may_use + sinfo->bytes_readonly + num_bytes +
8519 min_allocable_bytes <= sinfo->total_bytes) {
8520 sinfo->bytes_readonly += num_bytes;
8525 spin_unlock(&cache->lock);
8526 spin_unlock(&sinfo->lock);
8530 int btrfs_set_block_group_ro(struct btrfs_root *root,
8531 struct btrfs_block_group_cache *cache)
8534 struct btrfs_trans_handle *trans;
8540 trans = btrfs_join_transaction(root);
8542 return PTR_ERR(trans);
8544 alloc_flags = update_block_group_flags(root, cache->flags);
8545 if (alloc_flags != cache->flags) {
8546 ret = do_chunk_alloc(trans, root, alloc_flags,
8552 ret = set_block_group_ro(cache, 0);
8555 alloc_flags = get_alloc_profile(root, cache->space_info->flags);
8556 ret = do_chunk_alloc(trans, root, alloc_flags,
8560 ret = set_block_group_ro(cache, 0);
8562 btrfs_end_transaction(trans, root);
8566 int btrfs_force_chunk_alloc(struct btrfs_trans_handle *trans,
8567 struct btrfs_root *root, u64 type)
8569 u64 alloc_flags = get_alloc_profile(root, type);
8570 return do_chunk_alloc(trans, root, alloc_flags,
8575 * helper to account the unused space of all the readonly block group in the
8576 * list. takes mirrors into account.
8578 static u64 __btrfs_get_ro_block_group_free_space(struct list_head *groups_list)
8580 struct btrfs_block_group_cache *block_group;
8584 list_for_each_entry(block_group, groups_list, list) {
8585 spin_lock(&block_group->lock);
8587 if (!block_group->ro) {
8588 spin_unlock(&block_group->lock);
8592 if (block_group->flags & (BTRFS_BLOCK_GROUP_RAID1 |
8593 BTRFS_BLOCK_GROUP_RAID10 |
8594 BTRFS_BLOCK_GROUP_DUP))
8599 free_bytes += (block_group->key.offset -
8600 btrfs_block_group_used(&block_group->item)) *
8603 spin_unlock(&block_group->lock);
8610 * helper to account the unused space of all the readonly block group in the
8611 * space_info. takes mirrors into account.
8613 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
8618 spin_lock(&sinfo->lock);
8620 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++)
8621 if (!list_empty(&sinfo->block_groups[i]))
8622 free_bytes += __btrfs_get_ro_block_group_free_space(
8623 &sinfo->block_groups[i]);
8625 spin_unlock(&sinfo->lock);
8630 void btrfs_set_block_group_rw(struct btrfs_root *root,
8631 struct btrfs_block_group_cache *cache)
8633 struct btrfs_space_info *sinfo = cache->space_info;
8638 spin_lock(&sinfo->lock);
8639 spin_lock(&cache->lock);
8640 num_bytes = cache->key.offset - cache->reserved - cache->pinned -
8641 cache->bytes_super - btrfs_block_group_used(&cache->item);
8642 sinfo->bytes_readonly -= num_bytes;
8644 spin_unlock(&cache->lock);
8645 spin_unlock(&sinfo->lock);
8649 * checks to see if its even possible to relocate this block group.
8651 * @return - -1 if it's not a good idea to relocate this block group, 0 if its
8652 * ok to go ahead and try.
8654 int btrfs_can_relocate(struct btrfs_root *root, u64 bytenr)
8656 struct btrfs_block_group_cache *block_group;
8657 struct btrfs_space_info *space_info;
8658 struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
8659 struct btrfs_device *device;
8660 struct btrfs_trans_handle *trans;
8669 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
8671 /* odd, couldn't find the block group, leave it alone */
8675 min_free = btrfs_block_group_used(&block_group->item);
8677 /* no bytes used, we're good */
8681 space_info = block_group->space_info;
8682 spin_lock(&space_info->lock);
8684 full = space_info->full;
8687 * if this is the last block group we have in this space, we can't
8688 * relocate it unless we're able to allocate a new chunk below.
8690 * Otherwise, we need to make sure we have room in the space to handle
8691 * all of the extents from this block group. If we can, we're good
8693 if ((space_info->total_bytes != block_group->key.offset) &&
8694 (space_info->bytes_used + space_info->bytes_reserved +
8695 space_info->bytes_pinned + space_info->bytes_readonly +
8696 min_free < space_info->total_bytes)) {
8697 spin_unlock(&space_info->lock);
8700 spin_unlock(&space_info->lock);
8703 * ok we don't have enough space, but maybe we have free space on our
8704 * devices to allocate new chunks for relocation, so loop through our
8705 * alloc devices and guess if we have enough space. if this block
8706 * group is going to be restriped, run checks against the target
8707 * profile instead of the current one.
8719 target = get_restripe_target(root->fs_info, block_group->flags);
8721 index = __get_raid_index(extended_to_chunk(target));
8724 * this is just a balance, so if we were marked as full
8725 * we know there is no space for a new chunk
8730 index = get_block_group_index(block_group);
8733 if (index == BTRFS_RAID_RAID10) {
8737 } else if (index == BTRFS_RAID_RAID1) {
8739 } else if (index == BTRFS_RAID_DUP) {
8742 } else if (index == BTRFS_RAID_RAID0) {
8743 dev_min = fs_devices->rw_devices;
8744 do_div(min_free, dev_min);
8747 /* We need to do this so that we can look at pending chunks */
8748 trans = btrfs_join_transaction(root);
8749 if (IS_ERR(trans)) {
8750 ret = PTR_ERR(trans);
8754 mutex_lock(&root->fs_info->chunk_mutex);
8755 list_for_each_entry(device, &fs_devices->alloc_list, dev_alloc_list) {
8759 * check to make sure we can actually find a chunk with enough
8760 * space to fit our block group in.
8762 if (device->total_bytes > device->bytes_used + min_free &&
8763 !device->is_tgtdev_for_dev_replace) {
8764 ret = find_free_dev_extent(trans, device, min_free,
8769 if (dev_nr >= dev_min)
8775 mutex_unlock(&root->fs_info->chunk_mutex);
8776 btrfs_end_transaction(trans, root);
8778 btrfs_put_block_group(block_group);
8782 static int find_first_block_group(struct btrfs_root *root,
8783 struct btrfs_path *path, struct btrfs_key *key)
8786 struct btrfs_key found_key;
8787 struct extent_buffer *leaf;
8790 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
8795 slot = path->slots[0];
8796 leaf = path->nodes[0];
8797 if (slot >= btrfs_header_nritems(leaf)) {
8798 ret = btrfs_next_leaf(root, path);
8805 btrfs_item_key_to_cpu(leaf, &found_key, slot);
8807 if (found_key.objectid >= key->objectid &&
8808 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
8818 void btrfs_put_block_group_cache(struct btrfs_fs_info *info)
8820 struct btrfs_block_group_cache *block_group;
8824 struct inode *inode;
8826 block_group = btrfs_lookup_first_block_group(info, last);
8827 while (block_group) {
8828 spin_lock(&block_group->lock);
8829 if (block_group->iref)
8831 spin_unlock(&block_group->lock);
8832 block_group = next_block_group(info->tree_root,
8842 inode = block_group->inode;
8843 block_group->iref = 0;
8844 block_group->inode = NULL;
8845 spin_unlock(&block_group->lock);
8847 last = block_group->key.objectid + block_group->key.offset;
8848 btrfs_put_block_group(block_group);
8852 int btrfs_free_block_groups(struct btrfs_fs_info *info)
8854 struct btrfs_block_group_cache *block_group;
8855 struct btrfs_space_info *space_info;
8856 struct btrfs_caching_control *caching_ctl;
8859 down_write(&info->commit_root_sem);
8860 while (!list_empty(&info->caching_block_groups)) {
8861 caching_ctl = list_entry(info->caching_block_groups.next,
8862 struct btrfs_caching_control, list);
8863 list_del(&caching_ctl->list);
8864 put_caching_control(caching_ctl);
8866 up_write(&info->commit_root_sem);
8868 spin_lock(&info->unused_bgs_lock);
8869 while (!list_empty(&info->unused_bgs)) {
8870 block_group = list_first_entry(&info->unused_bgs,
8871 struct btrfs_block_group_cache,
8873 list_del_init(&block_group->bg_list);
8874 btrfs_put_block_group(block_group);
8876 spin_unlock(&info->unused_bgs_lock);
8878 spin_lock(&info->block_group_cache_lock);
8879 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
8880 block_group = rb_entry(n, struct btrfs_block_group_cache,
8882 rb_erase(&block_group->cache_node,
8883 &info->block_group_cache_tree);
8884 spin_unlock(&info->block_group_cache_lock);
8886 down_write(&block_group->space_info->groups_sem);
8887 list_del(&block_group->list);
8888 up_write(&block_group->space_info->groups_sem);
8890 if (block_group->cached == BTRFS_CACHE_STARTED)
8891 wait_block_group_cache_done(block_group);
8894 * We haven't cached this block group, which means we could
8895 * possibly have excluded extents on this block group.
8897 if (block_group->cached == BTRFS_CACHE_NO ||
8898 block_group->cached == BTRFS_CACHE_ERROR)
8899 free_excluded_extents(info->extent_root, block_group);
8901 btrfs_remove_free_space_cache(block_group);
8902 btrfs_put_block_group(block_group);
8904 spin_lock(&info->block_group_cache_lock);
8906 spin_unlock(&info->block_group_cache_lock);
8908 /* now that all the block groups are freed, go through and
8909 * free all the space_info structs. This is only called during
8910 * the final stages of unmount, and so we know nobody is
8911 * using them. We call synchronize_rcu() once before we start,
8912 * just to be on the safe side.
8916 release_global_block_rsv(info);
8918 while (!list_empty(&info->space_info)) {
8921 space_info = list_entry(info->space_info.next,
8922 struct btrfs_space_info,
8924 if (btrfs_test_opt(info->tree_root, ENOSPC_DEBUG)) {
8925 if (WARN_ON(space_info->bytes_pinned > 0 ||
8926 space_info->bytes_reserved > 0 ||
8927 space_info->bytes_may_use > 0)) {
8928 dump_space_info(space_info, 0, 0);
8931 list_del(&space_info->list);
8932 for (i = 0; i < BTRFS_NR_RAID_TYPES; i++) {
8933 struct kobject *kobj;
8934 kobj = space_info->block_group_kobjs[i];
8935 space_info->block_group_kobjs[i] = NULL;
8941 kobject_del(&space_info->kobj);
8942 kobject_put(&space_info->kobj);
8947 static void __link_block_group(struct btrfs_space_info *space_info,
8948 struct btrfs_block_group_cache *cache)
8950 int index = get_block_group_index(cache);
8953 down_write(&space_info->groups_sem);
8954 if (list_empty(&space_info->block_groups[index]))
8956 list_add_tail(&cache->list, &space_info->block_groups[index]);
8957 up_write(&space_info->groups_sem);
8960 struct raid_kobject *rkobj;
8963 rkobj = kzalloc(sizeof(*rkobj), GFP_NOFS);
8966 rkobj->raid_type = index;
8967 kobject_init(&rkobj->kobj, &btrfs_raid_ktype);
8968 ret = kobject_add(&rkobj->kobj, &space_info->kobj,
8969 "%s", get_raid_name(index));
8971 kobject_put(&rkobj->kobj);
8974 space_info->block_group_kobjs[index] = &rkobj->kobj;
8979 pr_warn("BTRFS: failed to add kobject for block cache. ignoring.\n");
8982 static struct btrfs_block_group_cache *
8983 btrfs_create_block_group_cache(struct btrfs_root *root, u64 start, u64 size)
8985 struct btrfs_block_group_cache *cache;
8987 cache = kzalloc(sizeof(*cache), GFP_NOFS);
8991 cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
8993 if (!cache->free_space_ctl) {
8998 cache->key.objectid = start;
8999 cache->key.offset = size;
9000 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9002 cache->sectorsize = root->sectorsize;
9003 cache->fs_info = root->fs_info;
9004 cache->full_stripe_len = btrfs_full_stripe_len(root,
9005 &root->fs_info->mapping_tree,
9007 atomic_set(&cache->count, 1);
9008 spin_lock_init(&cache->lock);
9009 init_rwsem(&cache->data_rwsem);
9010 INIT_LIST_HEAD(&cache->list);
9011 INIT_LIST_HEAD(&cache->cluster_list);
9012 INIT_LIST_HEAD(&cache->bg_list);
9013 btrfs_init_free_space_ctl(cache);
9018 int btrfs_read_block_groups(struct btrfs_root *root)
9020 struct btrfs_path *path;
9022 struct btrfs_block_group_cache *cache;
9023 struct btrfs_fs_info *info = root->fs_info;
9024 struct btrfs_space_info *space_info;
9025 struct btrfs_key key;
9026 struct btrfs_key found_key;
9027 struct extent_buffer *leaf;
9031 root = info->extent_root;
9034 key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
9035 path = btrfs_alloc_path();
9040 cache_gen = btrfs_super_cache_generation(root->fs_info->super_copy);
9041 if (btrfs_test_opt(root, SPACE_CACHE) &&
9042 btrfs_super_generation(root->fs_info->super_copy) != cache_gen)
9044 if (btrfs_test_opt(root, CLEAR_CACHE))
9048 ret = find_first_block_group(root, path, &key);
9054 leaf = path->nodes[0];
9055 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
9057 cache = btrfs_create_block_group_cache(root, found_key.objectid,
9066 * When we mount with old space cache, we need to
9067 * set BTRFS_DC_CLEAR and set dirty flag.
9069 * a) Setting 'BTRFS_DC_CLEAR' makes sure that we
9070 * truncate the old free space cache inode and
9072 * b) Setting 'dirty flag' makes sure that we flush
9073 * the new space cache info onto disk.
9075 cache->disk_cache_state = BTRFS_DC_CLEAR;
9076 if (btrfs_test_opt(root, SPACE_CACHE))
9080 read_extent_buffer(leaf, &cache->item,
9081 btrfs_item_ptr_offset(leaf, path->slots[0]),
9082 sizeof(cache->item));
9083 cache->flags = btrfs_block_group_flags(&cache->item);
9085 key.objectid = found_key.objectid + found_key.offset;
9086 btrfs_release_path(path);
9089 * We need to exclude the super stripes now so that the space
9090 * info has super bytes accounted for, otherwise we'll think
9091 * we have more space than we actually do.
9093 ret = exclude_super_stripes(root, cache);
9096 * We may have excluded something, so call this just in
9099 free_excluded_extents(root, cache);
9100 btrfs_put_block_group(cache);
9105 * check for two cases, either we are full, and therefore
9106 * don't need to bother with the caching work since we won't
9107 * find any space, or we are empty, and we can just add all
9108 * the space in and be done with it. This saves us _alot_ of
9109 * time, particularly in the full case.
9111 if (found_key.offset == btrfs_block_group_used(&cache->item)) {
9112 cache->last_byte_to_unpin = (u64)-1;
9113 cache->cached = BTRFS_CACHE_FINISHED;
9114 free_excluded_extents(root, cache);
9115 } else if (btrfs_block_group_used(&cache->item) == 0) {
9116 cache->last_byte_to_unpin = (u64)-1;
9117 cache->cached = BTRFS_CACHE_FINISHED;
9118 add_new_free_space(cache, root->fs_info,
9120 found_key.objectid +
9122 free_excluded_extents(root, cache);
9125 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9127 btrfs_remove_free_space_cache(cache);
9128 btrfs_put_block_group(cache);
9132 ret = update_space_info(info, cache->flags, found_key.offset,
9133 btrfs_block_group_used(&cache->item),
9136 btrfs_remove_free_space_cache(cache);
9137 spin_lock(&info->block_group_cache_lock);
9138 rb_erase(&cache->cache_node,
9139 &info->block_group_cache_tree);
9140 spin_unlock(&info->block_group_cache_lock);
9141 btrfs_put_block_group(cache);
9145 cache->space_info = space_info;
9146 spin_lock(&cache->space_info->lock);
9147 cache->space_info->bytes_readonly += cache->bytes_super;
9148 spin_unlock(&cache->space_info->lock);
9150 __link_block_group(space_info, cache);
9152 set_avail_alloc_bits(root->fs_info, cache->flags);
9153 if (btrfs_chunk_readonly(root, cache->key.objectid)) {
9154 set_block_group_ro(cache, 1);
9155 } else if (btrfs_block_group_used(&cache->item) == 0) {
9156 spin_lock(&info->unused_bgs_lock);
9157 /* Should always be true but just in case. */
9158 if (list_empty(&cache->bg_list)) {
9159 btrfs_get_block_group(cache);
9160 list_add_tail(&cache->bg_list,
9163 spin_unlock(&info->unused_bgs_lock);
9167 list_for_each_entry_rcu(space_info, &root->fs_info->space_info, list) {
9168 if (!(get_alloc_profile(root, space_info->flags) &
9169 (BTRFS_BLOCK_GROUP_RAID10 |
9170 BTRFS_BLOCK_GROUP_RAID1 |
9171 BTRFS_BLOCK_GROUP_RAID5 |
9172 BTRFS_BLOCK_GROUP_RAID6 |
9173 BTRFS_BLOCK_GROUP_DUP)))
9176 * avoid allocating from un-mirrored block group if there are
9177 * mirrored block groups.
9179 list_for_each_entry(cache,
9180 &space_info->block_groups[BTRFS_RAID_RAID0],
9182 set_block_group_ro(cache, 1);
9183 list_for_each_entry(cache,
9184 &space_info->block_groups[BTRFS_RAID_SINGLE],
9186 set_block_group_ro(cache, 1);
9189 init_global_block_rsv(info);
9192 btrfs_free_path(path);
9196 void btrfs_create_pending_block_groups(struct btrfs_trans_handle *trans,
9197 struct btrfs_root *root)
9199 struct btrfs_block_group_cache *block_group, *tmp;
9200 struct btrfs_root *extent_root = root->fs_info->extent_root;
9201 struct btrfs_block_group_item item;
9202 struct btrfs_key key;
9205 list_for_each_entry_safe(block_group, tmp, &trans->new_bgs, bg_list) {
9206 list_del_init(&block_group->bg_list);
9210 spin_lock(&block_group->lock);
9211 memcpy(&item, &block_group->item, sizeof(item));
9212 memcpy(&key, &block_group->key, sizeof(key));
9213 spin_unlock(&block_group->lock);
9215 ret = btrfs_insert_item(trans, extent_root, &key, &item,
9218 btrfs_abort_transaction(trans, extent_root, ret);
9219 ret = btrfs_finish_chunk_alloc(trans, extent_root,
9220 key.objectid, key.offset);
9222 btrfs_abort_transaction(trans, extent_root, ret);
9226 int btrfs_make_block_group(struct btrfs_trans_handle *trans,
9227 struct btrfs_root *root, u64 bytes_used,
9228 u64 type, u64 chunk_objectid, u64 chunk_offset,
9232 struct btrfs_root *extent_root;
9233 struct btrfs_block_group_cache *cache;
9235 extent_root = root->fs_info->extent_root;
9237 btrfs_set_log_full_commit(root->fs_info, trans);
9239 cache = btrfs_create_block_group_cache(root, chunk_offset, size);
9243 btrfs_set_block_group_used(&cache->item, bytes_used);
9244 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
9245 btrfs_set_block_group_flags(&cache->item, type);
9247 cache->flags = type;
9248 cache->last_byte_to_unpin = (u64)-1;
9249 cache->cached = BTRFS_CACHE_FINISHED;
9250 ret = exclude_super_stripes(root, cache);
9253 * We may have excluded something, so call this just in
9256 free_excluded_extents(root, cache);
9257 btrfs_put_block_group(cache);
9261 add_new_free_space(cache, root->fs_info, chunk_offset,
9262 chunk_offset + size);
9264 free_excluded_extents(root, cache);
9266 ret = btrfs_add_block_group_cache(root->fs_info, cache);
9268 btrfs_remove_free_space_cache(cache);
9269 btrfs_put_block_group(cache);
9273 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
9274 &cache->space_info);
9276 btrfs_remove_free_space_cache(cache);
9277 spin_lock(&root->fs_info->block_group_cache_lock);
9278 rb_erase(&cache->cache_node,
9279 &root->fs_info->block_group_cache_tree);
9280 spin_unlock(&root->fs_info->block_group_cache_lock);
9281 btrfs_put_block_group(cache);
9284 update_global_block_rsv(root->fs_info);
9286 spin_lock(&cache->space_info->lock);
9287 cache->space_info->bytes_readonly += cache->bytes_super;
9288 spin_unlock(&cache->space_info->lock);
9290 __link_block_group(cache->space_info, cache);
9292 list_add_tail(&cache->bg_list, &trans->new_bgs);
9294 set_avail_alloc_bits(extent_root->fs_info, type);
9299 static void clear_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
9301 u64 extra_flags = chunk_to_extended(flags) &
9302 BTRFS_EXTENDED_PROFILE_MASK;
9304 write_seqlock(&fs_info->profiles_lock);
9305 if (flags & BTRFS_BLOCK_GROUP_DATA)
9306 fs_info->avail_data_alloc_bits &= ~extra_flags;
9307 if (flags & BTRFS_BLOCK_GROUP_METADATA)
9308 fs_info->avail_metadata_alloc_bits &= ~extra_flags;
9309 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
9310 fs_info->avail_system_alloc_bits &= ~extra_flags;
9311 write_sequnlock(&fs_info->profiles_lock);
9314 int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
9315 struct btrfs_root *root, u64 group_start)
9317 struct btrfs_path *path;
9318 struct btrfs_block_group_cache *block_group;
9319 struct btrfs_free_cluster *cluster;
9320 struct btrfs_root *tree_root = root->fs_info->tree_root;
9321 struct btrfs_key key;
9322 struct inode *inode;
9323 struct kobject *kobj = NULL;
9328 root = root->fs_info->extent_root;
9330 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
9331 BUG_ON(!block_group);
9332 BUG_ON(!block_group->ro);
9335 * Free the reserved super bytes from this block group before
9338 free_excluded_extents(root, block_group);
9340 memcpy(&key, &block_group->key, sizeof(key));
9341 index = get_block_group_index(block_group);
9342 if (block_group->flags & (BTRFS_BLOCK_GROUP_DUP |
9343 BTRFS_BLOCK_GROUP_RAID1 |
9344 BTRFS_BLOCK_GROUP_RAID10))
9349 /* make sure this block group isn't part of an allocation cluster */
9350 cluster = &root->fs_info->data_alloc_cluster;
9351 spin_lock(&cluster->refill_lock);
9352 btrfs_return_cluster_to_free_space(block_group, cluster);
9353 spin_unlock(&cluster->refill_lock);
9356 * make sure this block group isn't part of a metadata
9357 * allocation cluster
9359 cluster = &root->fs_info->meta_alloc_cluster;
9360 spin_lock(&cluster->refill_lock);
9361 btrfs_return_cluster_to_free_space(block_group, cluster);
9362 spin_unlock(&cluster->refill_lock);
9364 path = btrfs_alloc_path();
9370 inode = lookup_free_space_inode(tree_root, block_group, path);
9371 if (!IS_ERR(inode)) {
9372 ret = btrfs_orphan_add(trans, inode);
9374 btrfs_add_delayed_iput(inode);
9378 /* One for the block groups ref */
9379 spin_lock(&block_group->lock);
9380 if (block_group->iref) {
9381 block_group->iref = 0;
9382 block_group->inode = NULL;
9383 spin_unlock(&block_group->lock);
9386 spin_unlock(&block_group->lock);
9388 /* One for our lookup ref */
9389 btrfs_add_delayed_iput(inode);
9392 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
9393 key.offset = block_group->key.objectid;
9396 ret = btrfs_search_slot(trans, tree_root, &key, path, -1, 1);
9400 btrfs_release_path(path);
9402 ret = btrfs_del_item(trans, tree_root, path);
9405 btrfs_release_path(path);
9408 spin_lock(&root->fs_info->block_group_cache_lock);
9409 rb_erase(&block_group->cache_node,
9410 &root->fs_info->block_group_cache_tree);
9412 if (root->fs_info->first_logical_byte == block_group->key.objectid)
9413 root->fs_info->first_logical_byte = (u64)-1;
9414 spin_unlock(&root->fs_info->block_group_cache_lock);
9416 down_write(&block_group->space_info->groups_sem);
9418 * we must use list_del_init so people can check to see if they
9419 * are still on the list after taking the semaphore
9421 list_del_init(&block_group->list);
9422 if (list_empty(&block_group->space_info->block_groups[index])) {
9423 kobj = block_group->space_info->block_group_kobjs[index];
9424 block_group->space_info->block_group_kobjs[index] = NULL;
9425 clear_avail_alloc_bits(root->fs_info, block_group->flags);
9427 up_write(&block_group->space_info->groups_sem);
9433 if (block_group->cached == BTRFS_CACHE_STARTED)
9434 wait_block_group_cache_done(block_group);
9436 btrfs_remove_free_space_cache(block_group);
9438 spin_lock(&block_group->space_info->lock);
9439 block_group->space_info->total_bytes -= block_group->key.offset;
9440 block_group->space_info->bytes_readonly -= block_group->key.offset;
9441 block_group->space_info->disk_total -= block_group->key.offset * factor;
9442 spin_unlock(&block_group->space_info->lock);
9444 memcpy(&key, &block_group->key, sizeof(key));
9446 btrfs_put_block_group(block_group);
9447 btrfs_put_block_group(block_group);
9449 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
9455 ret = btrfs_del_item(trans, root, path);
9457 btrfs_free_path(path);
9462 * Process the unused_bgs list and remove any that don't have any allocated
9463 * space inside of them.
9465 void btrfs_delete_unused_bgs(struct btrfs_fs_info *fs_info)
9467 struct btrfs_block_group_cache *block_group;
9468 struct btrfs_space_info *space_info;
9469 struct btrfs_root *root = fs_info->extent_root;
9470 struct btrfs_trans_handle *trans;
9476 spin_lock(&fs_info->unused_bgs_lock);
9477 while (!list_empty(&fs_info->unused_bgs)) {
9480 block_group = list_first_entry(&fs_info->unused_bgs,
9481 struct btrfs_block_group_cache,
9483 space_info = block_group->space_info;
9484 list_del_init(&block_group->bg_list);
9485 if (ret || btrfs_mixed_space_info(space_info)) {
9486 btrfs_put_block_group(block_group);
9489 spin_unlock(&fs_info->unused_bgs_lock);
9491 /* Don't want to race with allocators so take the groups_sem */
9492 down_write(&space_info->groups_sem);
9493 spin_lock(&block_group->lock);
9494 if (block_group->reserved ||
9495 btrfs_block_group_used(&block_group->item) ||
9498 * We want to bail if we made new allocations or have
9499 * outstanding allocations in this block group. We do
9500 * the ro check in case balance is currently acting on
9503 spin_unlock(&block_group->lock);
9504 up_write(&space_info->groups_sem);
9507 spin_unlock(&block_group->lock);
9509 /* We don't want to force the issue, only flip if it's ok. */
9510 ret = set_block_group_ro(block_group, 0);
9511 up_write(&space_info->groups_sem);
9518 * Want to do this before we do anything else so we can recover
9519 * properly if we fail to join the transaction.
9521 trans = btrfs_join_transaction(root);
9522 if (IS_ERR(trans)) {
9523 btrfs_set_block_group_rw(root, block_group);
9524 ret = PTR_ERR(trans);
9529 * We could have pending pinned extents for this block group,
9530 * just delete them, we don't care about them anymore.
9532 start = block_group->key.objectid;
9533 end = start + block_group->key.offset - 1;
9534 clear_extent_bits(&fs_info->freed_extents[0], start, end,
9535 EXTENT_DIRTY, GFP_NOFS);
9536 clear_extent_bits(&fs_info->freed_extents[1], start, end,
9537 EXTENT_DIRTY, GFP_NOFS);
9539 /* Reset pinned so btrfs_put_block_group doesn't complain */
9540 block_group->pinned = 0;
9543 * Btrfs_remove_chunk will abort the transaction if things go
9546 ret = btrfs_remove_chunk(trans, root,
9547 block_group->key.objectid);
9548 btrfs_end_transaction(trans, root);
9550 btrfs_put_block_group(block_group);
9551 spin_lock(&fs_info->unused_bgs_lock);
9553 spin_unlock(&fs_info->unused_bgs_lock);
9556 int btrfs_init_space_info(struct btrfs_fs_info *fs_info)
9558 struct btrfs_space_info *space_info;
9559 struct btrfs_super_block *disk_super;
9565 disk_super = fs_info->super_copy;
9566 if (!btrfs_super_root(disk_super))
9569 features = btrfs_super_incompat_flags(disk_super);
9570 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
9573 flags = BTRFS_BLOCK_GROUP_SYSTEM;
9574 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9579 flags = BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA;
9580 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9582 flags = BTRFS_BLOCK_GROUP_METADATA;
9583 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9587 flags = BTRFS_BLOCK_GROUP_DATA;
9588 ret = update_space_info(fs_info, flags, 0, 0, &space_info);
9594 int btrfs_error_unpin_extent_range(struct btrfs_root *root, u64 start, u64 end)
9596 return unpin_extent_range(root, start, end);
9599 int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr,
9600 u64 num_bytes, u64 *actual_bytes)
9602 return btrfs_discard_extent(root, bytenr, num_bytes, actual_bytes);
9605 int btrfs_trim_fs(struct btrfs_root *root, struct fstrim_range *range)
9607 struct btrfs_fs_info *fs_info = root->fs_info;
9608 struct btrfs_block_group_cache *cache = NULL;
9613 u64 total_bytes = btrfs_super_total_bytes(fs_info->super_copy);
9617 * try to trim all FS space, our block group may start from non-zero.
9619 if (range->len == total_bytes)
9620 cache = btrfs_lookup_first_block_group(fs_info, range->start);
9622 cache = btrfs_lookup_block_group(fs_info, range->start);
9625 if (cache->key.objectid >= (range->start + range->len)) {
9626 btrfs_put_block_group(cache);
9630 start = max(range->start, cache->key.objectid);
9631 end = min(range->start + range->len,
9632 cache->key.objectid + cache->key.offset);
9634 if (end - start >= range->minlen) {
9635 if (!block_group_cache_done(cache)) {
9636 ret = cache_block_group(cache, 0);
9638 btrfs_put_block_group(cache);
9641 ret = wait_block_group_cache_done(cache);
9643 btrfs_put_block_group(cache);
9647 ret = btrfs_trim_block_group(cache,
9653 trimmed += group_trimmed;
9655 btrfs_put_block_group(cache);
9660 cache = next_block_group(fs_info->tree_root, cache);
9663 range->len = trimmed;
9668 * btrfs_{start,end}_write() is similar to mnt_{want, drop}_write(),
9669 * they are used to prevent the some tasks writing data into the page cache
9670 * by nocow before the subvolume is snapshoted, but flush the data into
9671 * the disk after the snapshot creation.
9673 void btrfs_end_nocow_write(struct btrfs_root *root)
9675 percpu_counter_dec(&root->subv_writers->counter);
9677 * Make sure counter is updated before we wake up
9681 if (waitqueue_active(&root->subv_writers->wait))
9682 wake_up(&root->subv_writers->wait);
9685 int btrfs_start_nocow_write(struct btrfs_root *root)
9687 if (atomic_read(&root->will_be_snapshoted))
9690 percpu_counter_inc(&root->subv_writers->counter);
9692 * Make sure counter is updated before we check for snapshot creation.
9695 if (atomic_read(&root->will_be_snapshoted)) {
9696 btrfs_end_nocow_write(root);