1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2007 Oracle. All rights reserved.
6 #include <linux/sched.h>
7 #include <linux/sched/signal.h>
8 #include <linux/pagemap.h>
9 #include <linux/writeback.h>
10 #include <linux/blkdev.h>
11 #include <linux/sort.h>
12 #include <linux/rcupdate.h>
13 #include <linux/kthread.h>
14 #include <linux/slab.h>
15 #include <linux/ratelimit.h>
16 #include <linux/percpu_counter.h>
17 #include <linux/lockdep.h>
18 #include <linux/crc32c.h>
22 #include "print-tree.h"
26 #include "free-space-cache.h"
27 #include "free-space-tree.h"
30 #include "ref-verify.h"
31 #include "space-info.h"
32 #include "block-rsv.h"
33 #include "delalloc-space.h"
34 #include "block-group.h"
36 #include "rcu-string.h"
38 #undef SCRAMBLE_DELAYED_REFS
41 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
42 struct btrfs_delayed_ref_node *node, u64 parent,
43 u64 root_objectid, u64 owner_objectid,
44 u64 owner_offset, int refs_to_drop,
45 struct btrfs_delayed_extent_op *extra_op);
46 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
47 struct extent_buffer *leaf,
48 struct btrfs_extent_item *ei);
49 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
50 u64 parent, u64 root_objectid,
51 u64 flags, u64 owner, u64 offset,
52 struct btrfs_key *ins, int ref_mod);
53 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
54 struct btrfs_delayed_ref_node *node,
55 struct btrfs_delayed_extent_op *extent_op);
56 static int find_next_key(struct btrfs_path *path, int level,
57 struct btrfs_key *key);
59 static int block_group_bits(struct btrfs_block_group *cache, u64 bits)
61 return (cache->flags & bits) == bits;
64 int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
65 u64 start, u64 num_bytes)
67 u64 end = start + num_bytes - 1;
68 set_extent_bits(&fs_info->excluded_extents, start, end,
73 void btrfs_free_excluded_extents(struct btrfs_block_group *cache)
75 struct btrfs_fs_info *fs_info = cache->fs_info;
79 end = start + cache->length - 1;
81 clear_extent_bits(&fs_info->excluded_extents, start, end,
85 static u64 generic_ref_to_space_flags(struct btrfs_ref *ref)
87 if (ref->type == BTRFS_REF_METADATA) {
88 if (ref->tree_ref.root == BTRFS_CHUNK_TREE_OBJECTID)
89 return BTRFS_BLOCK_GROUP_SYSTEM;
91 return BTRFS_BLOCK_GROUP_METADATA;
93 return BTRFS_BLOCK_GROUP_DATA;
96 static void add_pinned_bytes(struct btrfs_fs_info *fs_info,
97 struct btrfs_ref *ref)
99 struct btrfs_space_info *space_info;
100 u64 flags = generic_ref_to_space_flags(ref);
102 space_info = btrfs_find_space_info(fs_info, flags);
104 percpu_counter_add_batch(&space_info->total_bytes_pinned, ref->len,
105 BTRFS_TOTAL_BYTES_PINNED_BATCH);
108 static void sub_pinned_bytes(struct btrfs_fs_info *fs_info,
109 struct btrfs_ref *ref)
111 struct btrfs_space_info *space_info;
112 u64 flags = generic_ref_to_space_flags(ref);
114 space_info = btrfs_find_space_info(fs_info, flags);
116 percpu_counter_add_batch(&space_info->total_bytes_pinned, -ref->len,
117 BTRFS_TOTAL_BYTES_PINNED_BATCH);
120 /* simple helper to search for an existing data extent at a given offset */
121 int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len)
124 struct btrfs_key key;
125 struct btrfs_path *path;
127 path = btrfs_alloc_path();
131 key.objectid = start;
133 key.type = BTRFS_EXTENT_ITEM_KEY;
134 ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path, 0, 0);
135 btrfs_free_path(path);
140 * helper function to lookup reference count and flags of a tree block.
142 * the head node for delayed ref is used to store the sum of all the
143 * reference count modifications queued up in the rbtree. the head
144 * node may also store the extent flags to set. This way you can check
145 * to see what the reference count and extent flags would be if all of
146 * the delayed refs are not processed.
148 int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
149 struct btrfs_fs_info *fs_info, u64 bytenr,
150 u64 offset, int metadata, u64 *refs, u64 *flags)
152 struct btrfs_delayed_ref_head *head;
153 struct btrfs_delayed_ref_root *delayed_refs;
154 struct btrfs_path *path;
155 struct btrfs_extent_item *ei;
156 struct extent_buffer *leaf;
157 struct btrfs_key key;
164 * If we don't have skinny metadata, don't bother doing anything
167 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
168 offset = fs_info->nodesize;
172 path = btrfs_alloc_path();
177 path->skip_locking = 1;
178 path->search_commit_root = 1;
182 key.objectid = bytenr;
185 key.type = BTRFS_METADATA_ITEM_KEY;
187 key.type = BTRFS_EXTENT_ITEM_KEY;
189 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 0);
193 if (ret > 0 && metadata && key.type == BTRFS_METADATA_ITEM_KEY) {
194 if (path->slots[0]) {
196 btrfs_item_key_to_cpu(path->nodes[0], &key,
198 if (key.objectid == bytenr &&
199 key.type == BTRFS_EXTENT_ITEM_KEY &&
200 key.offset == fs_info->nodesize)
206 leaf = path->nodes[0];
207 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
208 if (item_size >= sizeof(*ei)) {
209 ei = btrfs_item_ptr(leaf, path->slots[0],
210 struct btrfs_extent_item);
211 num_refs = btrfs_extent_refs(leaf, ei);
212 extent_flags = btrfs_extent_flags(leaf, ei);
215 btrfs_print_v0_err(fs_info);
217 btrfs_abort_transaction(trans, ret);
219 btrfs_handle_fs_error(fs_info, ret, NULL);
224 BUG_ON(num_refs == 0);
234 delayed_refs = &trans->transaction->delayed_refs;
235 spin_lock(&delayed_refs->lock);
236 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
238 if (!mutex_trylock(&head->mutex)) {
239 refcount_inc(&head->refs);
240 spin_unlock(&delayed_refs->lock);
242 btrfs_release_path(path);
245 * Mutex was contended, block until it's released and try
248 mutex_lock(&head->mutex);
249 mutex_unlock(&head->mutex);
250 btrfs_put_delayed_ref_head(head);
253 spin_lock(&head->lock);
254 if (head->extent_op && head->extent_op->update_flags)
255 extent_flags |= head->extent_op->flags_to_set;
257 BUG_ON(num_refs == 0);
259 num_refs += head->ref_mod;
260 spin_unlock(&head->lock);
261 mutex_unlock(&head->mutex);
263 spin_unlock(&delayed_refs->lock);
265 WARN_ON(num_refs == 0);
269 *flags = extent_flags;
271 btrfs_free_path(path);
276 * Back reference rules. Back refs have three main goals:
278 * 1) differentiate between all holders of references to an extent so that
279 * when a reference is dropped we can make sure it was a valid reference
280 * before freeing the extent.
282 * 2) Provide enough information to quickly find the holders of an extent
283 * if we notice a given block is corrupted or bad.
285 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
286 * maintenance. This is actually the same as #2, but with a slightly
287 * different use case.
289 * There are two kinds of back refs. The implicit back refs is optimized
290 * for pointers in non-shared tree blocks. For a given pointer in a block,
291 * back refs of this kind provide information about the block's owner tree
292 * and the pointer's key. These information allow us to find the block by
293 * b-tree searching. The full back refs is for pointers in tree blocks not
294 * referenced by their owner trees. The location of tree block is recorded
295 * in the back refs. Actually the full back refs is generic, and can be
296 * used in all cases the implicit back refs is used. The major shortcoming
297 * of the full back refs is its overhead. Every time a tree block gets
298 * COWed, we have to update back refs entry for all pointers in it.
300 * For a newly allocated tree block, we use implicit back refs for
301 * pointers in it. This means most tree related operations only involve
302 * implicit back refs. For a tree block created in old transaction, the
303 * only way to drop a reference to it is COW it. So we can detect the
304 * event that tree block loses its owner tree's reference and do the
305 * back refs conversion.
307 * When a tree block is COWed through a tree, there are four cases:
309 * The reference count of the block is one and the tree is the block's
310 * owner tree. Nothing to do in this case.
312 * The reference count of the block is one and the tree is not the
313 * block's owner tree. In this case, full back refs is used for pointers
314 * in the block. Remove these full back refs, add implicit back refs for
315 * every pointers in the new block.
317 * The reference count of the block is greater than one and the tree is
318 * the block's owner tree. In this case, implicit back refs is used for
319 * pointers in the block. Add full back refs for every pointers in the
320 * block, increase lower level extents' reference counts. The original
321 * implicit back refs are entailed to the new block.
323 * The reference count of the block is greater than one and the tree is
324 * not the block's owner tree. Add implicit back refs for every pointer in
325 * the new block, increase lower level extents' reference count.
327 * Back Reference Key composing:
329 * The key objectid corresponds to the first byte in the extent,
330 * The key type is used to differentiate between types of back refs.
331 * There are different meanings of the key offset for different types
334 * File extents can be referenced by:
336 * - multiple snapshots, subvolumes, or different generations in one subvol
337 * - different files inside a single subvolume
338 * - different offsets inside a file (bookend extents in file.c)
340 * The extent ref structure for the implicit back refs has fields for:
342 * - Objectid of the subvolume root
343 * - objectid of the file holding the reference
344 * - original offset in the file
345 * - how many bookend extents
347 * The key offset for the implicit back refs is hash of the first
350 * The extent ref structure for the full back refs has field for:
352 * - number of pointers in the tree leaf
354 * The key offset for the implicit back refs is the first byte of
357 * When a file extent is allocated, The implicit back refs is used.
358 * the fields are filled in:
360 * (root_key.objectid, inode objectid, offset in file, 1)
362 * When a file extent is removed file truncation, we find the
363 * corresponding implicit back refs and check the following fields:
365 * (btrfs_header_owner(leaf), inode objectid, offset in file)
367 * Btree extents can be referenced by:
369 * - Different subvolumes
371 * Both the implicit back refs and the full back refs for tree blocks
372 * only consist of key. The key offset for the implicit back refs is
373 * objectid of block's owner tree. The key offset for the full back refs
374 * is the first byte of parent block.
376 * When implicit back refs is used, information about the lowest key and
377 * level of the tree block are required. These information are stored in
378 * tree block info structure.
382 * is_data == BTRFS_REF_TYPE_BLOCK, tree block type is required,
383 * is_data == BTRFS_REF_TYPE_DATA, data type is requiried,
384 * is_data == BTRFS_REF_TYPE_ANY, either type is OK.
386 int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
387 struct btrfs_extent_inline_ref *iref,
388 enum btrfs_inline_ref_type is_data)
390 int type = btrfs_extent_inline_ref_type(eb, iref);
391 u64 offset = btrfs_extent_inline_ref_offset(eb, iref);
393 if (type == BTRFS_TREE_BLOCK_REF_KEY ||
394 type == BTRFS_SHARED_BLOCK_REF_KEY ||
395 type == BTRFS_SHARED_DATA_REF_KEY ||
396 type == BTRFS_EXTENT_DATA_REF_KEY) {
397 if (is_data == BTRFS_REF_TYPE_BLOCK) {
398 if (type == BTRFS_TREE_BLOCK_REF_KEY)
400 if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
403 * Every shared one has parent tree block,
404 * which must be aligned to sector size.
407 IS_ALIGNED(offset, eb->fs_info->sectorsize))
410 } else if (is_data == BTRFS_REF_TYPE_DATA) {
411 if (type == BTRFS_EXTENT_DATA_REF_KEY)
413 if (type == BTRFS_SHARED_DATA_REF_KEY) {
416 * Every shared one has parent tree block,
417 * which must be aligned to sector size.
420 IS_ALIGNED(offset, eb->fs_info->sectorsize))
424 ASSERT(is_data == BTRFS_REF_TYPE_ANY);
429 btrfs_print_leaf((struct extent_buffer *)eb);
430 btrfs_err(eb->fs_info,
431 "eb %llu iref 0x%lx invalid extent inline ref type %d",
432 eb->start, (unsigned long)iref, type);
435 return BTRFS_REF_TYPE_INVALID;
438 u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset)
440 u32 high_crc = ~(u32)0;
441 u32 low_crc = ~(u32)0;
444 lenum = cpu_to_le64(root_objectid);
445 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
446 lenum = cpu_to_le64(owner);
447 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
448 lenum = cpu_to_le64(offset);
449 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
451 return ((u64)high_crc << 31) ^ (u64)low_crc;
454 static u64 hash_extent_data_ref_item(struct extent_buffer *leaf,
455 struct btrfs_extent_data_ref *ref)
457 return hash_extent_data_ref(btrfs_extent_data_ref_root(leaf, ref),
458 btrfs_extent_data_ref_objectid(leaf, ref),
459 btrfs_extent_data_ref_offset(leaf, ref));
462 static int match_extent_data_ref(struct extent_buffer *leaf,
463 struct btrfs_extent_data_ref *ref,
464 u64 root_objectid, u64 owner, u64 offset)
466 if (btrfs_extent_data_ref_root(leaf, ref) != root_objectid ||
467 btrfs_extent_data_ref_objectid(leaf, ref) != owner ||
468 btrfs_extent_data_ref_offset(leaf, ref) != offset)
473 static noinline int lookup_extent_data_ref(struct btrfs_trans_handle *trans,
474 struct btrfs_path *path,
475 u64 bytenr, u64 parent,
477 u64 owner, u64 offset)
479 struct btrfs_root *root = trans->fs_info->extent_root;
480 struct btrfs_key key;
481 struct btrfs_extent_data_ref *ref;
482 struct extent_buffer *leaf;
488 key.objectid = bytenr;
490 key.type = BTRFS_SHARED_DATA_REF_KEY;
493 key.type = BTRFS_EXTENT_DATA_REF_KEY;
494 key.offset = hash_extent_data_ref(root_objectid,
499 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
511 leaf = path->nodes[0];
512 nritems = btrfs_header_nritems(leaf);
514 if (path->slots[0] >= nritems) {
515 ret = btrfs_next_leaf(root, path);
521 leaf = path->nodes[0];
522 nritems = btrfs_header_nritems(leaf);
526 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
527 if (key.objectid != bytenr ||
528 key.type != BTRFS_EXTENT_DATA_REF_KEY)
531 ref = btrfs_item_ptr(leaf, path->slots[0],
532 struct btrfs_extent_data_ref);
534 if (match_extent_data_ref(leaf, ref, root_objectid,
537 btrfs_release_path(path);
549 static noinline int insert_extent_data_ref(struct btrfs_trans_handle *trans,
550 struct btrfs_path *path,
551 u64 bytenr, u64 parent,
552 u64 root_objectid, u64 owner,
553 u64 offset, int refs_to_add)
555 struct btrfs_root *root = trans->fs_info->extent_root;
556 struct btrfs_key key;
557 struct extent_buffer *leaf;
562 key.objectid = bytenr;
564 key.type = BTRFS_SHARED_DATA_REF_KEY;
566 size = sizeof(struct btrfs_shared_data_ref);
568 key.type = BTRFS_EXTENT_DATA_REF_KEY;
569 key.offset = hash_extent_data_ref(root_objectid,
571 size = sizeof(struct btrfs_extent_data_ref);
574 ret = btrfs_insert_empty_item(trans, root, path, &key, size);
575 if (ret && ret != -EEXIST)
578 leaf = path->nodes[0];
580 struct btrfs_shared_data_ref *ref;
581 ref = btrfs_item_ptr(leaf, path->slots[0],
582 struct btrfs_shared_data_ref);
584 btrfs_set_shared_data_ref_count(leaf, ref, refs_to_add);
586 num_refs = btrfs_shared_data_ref_count(leaf, ref);
587 num_refs += refs_to_add;
588 btrfs_set_shared_data_ref_count(leaf, ref, num_refs);
591 struct btrfs_extent_data_ref *ref;
592 while (ret == -EEXIST) {
593 ref = btrfs_item_ptr(leaf, path->slots[0],
594 struct btrfs_extent_data_ref);
595 if (match_extent_data_ref(leaf, ref, root_objectid,
598 btrfs_release_path(path);
600 ret = btrfs_insert_empty_item(trans, root, path, &key,
602 if (ret && ret != -EEXIST)
605 leaf = path->nodes[0];
607 ref = btrfs_item_ptr(leaf, path->slots[0],
608 struct btrfs_extent_data_ref);
610 btrfs_set_extent_data_ref_root(leaf, ref,
612 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
613 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
614 btrfs_set_extent_data_ref_count(leaf, ref, refs_to_add);
616 num_refs = btrfs_extent_data_ref_count(leaf, ref);
617 num_refs += refs_to_add;
618 btrfs_set_extent_data_ref_count(leaf, ref, num_refs);
621 btrfs_mark_buffer_dirty(leaf);
624 btrfs_release_path(path);
628 static noinline int remove_extent_data_ref(struct btrfs_trans_handle *trans,
629 struct btrfs_path *path,
630 int refs_to_drop, int *last_ref)
632 struct btrfs_key key;
633 struct btrfs_extent_data_ref *ref1 = NULL;
634 struct btrfs_shared_data_ref *ref2 = NULL;
635 struct extent_buffer *leaf;
639 leaf = path->nodes[0];
640 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
642 if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
643 ref1 = btrfs_item_ptr(leaf, path->slots[0],
644 struct btrfs_extent_data_ref);
645 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
646 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
647 ref2 = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_shared_data_ref);
649 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
650 } else if (unlikely(key.type == BTRFS_EXTENT_REF_V0_KEY)) {
651 btrfs_print_v0_err(trans->fs_info);
652 btrfs_abort_transaction(trans, -EINVAL);
658 BUG_ON(num_refs < refs_to_drop);
659 num_refs -= refs_to_drop;
662 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
665 if (key.type == BTRFS_EXTENT_DATA_REF_KEY)
666 btrfs_set_extent_data_ref_count(leaf, ref1, num_refs);
667 else if (key.type == BTRFS_SHARED_DATA_REF_KEY)
668 btrfs_set_shared_data_ref_count(leaf, ref2, num_refs);
669 btrfs_mark_buffer_dirty(leaf);
674 static noinline u32 extent_data_ref_count(struct btrfs_path *path,
675 struct btrfs_extent_inline_ref *iref)
677 struct btrfs_key key;
678 struct extent_buffer *leaf;
679 struct btrfs_extent_data_ref *ref1;
680 struct btrfs_shared_data_ref *ref2;
684 leaf = path->nodes[0];
685 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
687 BUG_ON(key.type == BTRFS_EXTENT_REF_V0_KEY);
690 * If type is invalid, we should have bailed out earlier than
693 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
694 ASSERT(type != BTRFS_REF_TYPE_INVALID);
695 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
696 ref1 = (struct btrfs_extent_data_ref *)(&iref->offset);
697 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
699 ref2 = (struct btrfs_shared_data_ref *)(iref + 1);
700 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
702 } else if (key.type == BTRFS_EXTENT_DATA_REF_KEY) {
703 ref1 = btrfs_item_ptr(leaf, path->slots[0],
704 struct btrfs_extent_data_ref);
705 num_refs = btrfs_extent_data_ref_count(leaf, ref1);
706 } else if (key.type == BTRFS_SHARED_DATA_REF_KEY) {
707 ref2 = btrfs_item_ptr(leaf, path->slots[0],
708 struct btrfs_shared_data_ref);
709 num_refs = btrfs_shared_data_ref_count(leaf, ref2);
716 static noinline int lookup_tree_block_ref(struct btrfs_trans_handle *trans,
717 struct btrfs_path *path,
718 u64 bytenr, u64 parent,
721 struct btrfs_root *root = trans->fs_info->extent_root;
722 struct btrfs_key key;
725 key.objectid = bytenr;
727 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
730 key.type = BTRFS_TREE_BLOCK_REF_KEY;
731 key.offset = root_objectid;
734 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
740 static noinline int insert_tree_block_ref(struct btrfs_trans_handle *trans,
741 struct btrfs_path *path,
742 u64 bytenr, u64 parent,
745 struct btrfs_key key;
748 key.objectid = bytenr;
750 key.type = BTRFS_SHARED_BLOCK_REF_KEY;
753 key.type = BTRFS_TREE_BLOCK_REF_KEY;
754 key.offset = root_objectid;
757 ret = btrfs_insert_empty_item(trans, trans->fs_info->extent_root,
759 btrfs_release_path(path);
763 static inline int extent_ref_type(u64 parent, u64 owner)
766 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
768 type = BTRFS_SHARED_BLOCK_REF_KEY;
770 type = BTRFS_TREE_BLOCK_REF_KEY;
773 type = BTRFS_SHARED_DATA_REF_KEY;
775 type = BTRFS_EXTENT_DATA_REF_KEY;
780 static int find_next_key(struct btrfs_path *path, int level,
781 struct btrfs_key *key)
784 for (; level < BTRFS_MAX_LEVEL; level++) {
785 if (!path->nodes[level])
787 if (path->slots[level] + 1 >=
788 btrfs_header_nritems(path->nodes[level]))
791 btrfs_item_key_to_cpu(path->nodes[level], key,
792 path->slots[level] + 1);
794 btrfs_node_key_to_cpu(path->nodes[level], key,
795 path->slots[level] + 1);
802 * look for inline back ref. if back ref is found, *ref_ret is set
803 * to the address of inline back ref, and 0 is returned.
805 * if back ref isn't found, *ref_ret is set to the address where it
806 * should be inserted, and -ENOENT is returned.
808 * if insert is true and there are too many inline back refs, the path
809 * points to the extent item, and -EAGAIN is returned.
811 * NOTE: inline back refs are ordered in the same way that back ref
812 * items in the tree are ordered.
814 static noinline_for_stack
815 int lookup_inline_extent_backref(struct btrfs_trans_handle *trans,
816 struct btrfs_path *path,
817 struct btrfs_extent_inline_ref **ref_ret,
818 u64 bytenr, u64 num_bytes,
819 u64 parent, u64 root_objectid,
820 u64 owner, u64 offset, int insert)
822 struct btrfs_fs_info *fs_info = trans->fs_info;
823 struct btrfs_root *root = fs_info->extent_root;
824 struct btrfs_key key;
825 struct extent_buffer *leaf;
826 struct btrfs_extent_item *ei;
827 struct btrfs_extent_inline_ref *iref;
837 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
840 key.objectid = bytenr;
841 key.type = BTRFS_EXTENT_ITEM_KEY;
842 key.offset = num_bytes;
844 want = extent_ref_type(parent, owner);
846 extra_size = btrfs_extent_inline_ref_size(want);
847 path->keep_locks = 1;
852 * Owner is our level, so we can just add one to get the level for the
853 * block we are interested in.
855 if (skinny_metadata && owner < BTRFS_FIRST_FREE_OBJECTID) {
856 key.type = BTRFS_METADATA_ITEM_KEY;
861 ret = btrfs_search_slot(trans, root, &key, path, extra_size, 1);
868 * We may be a newly converted file system which still has the old fat
869 * extent entries for metadata, so try and see if we have one of those.
871 if (ret > 0 && skinny_metadata) {
872 skinny_metadata = false;
873 if (path->slots[0]) {
875 btrfs_item_key_to_cpu(path->nodes[0], &key,
877 if (key.objectid == bytenr &&
878 key.type == BTRFS_EXTENT_ITEM_KEY &&
879 key.offset == num_bytes)
883 key.objectid = bytenr;
884 key.type = BTRFS_EXTENT_ITEM_KEY;
885 key.offset = num_bytes;
886 btrfs_release_path(path);
891 if (ret && !insert) {
894 } else if (WARN_ON(ret)) {
899 leaf = path->nodes[0];
900 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
901 if (unlikely(item_size < sizeof(*ei))) {
903 btrfs_print_v0_err(fs_info);
904 btrfs_abort_transaction(trans, err);
908 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
909 flags = btrfs_extent_flags(leaf, ei);
911 ptr = (unsigned long)(ei + 1);
912 end = (unsigned long)ei + item_size;
914 if (flags & BTRFS_EXTENT_FLAG_TREE_BLOCK && !skinny_metadata) {
915 ptr += sizeof(struct btrfs_tree_block_info);
919 if (owner >= BTRFS_FIRST_FREE_OBJECTID)
920 needed = BTRFS_REF_TYPE_DATA;
922 needed = BTRFS_REF_TYPE_BLOCK;
930 iref = (struct btrfs_extent_inline_ref *)ptr;
931 type = btrfs_get_extent_inline_ref_type(leaf, iref, needed);
932 if (type == BTRFS_REF_TYPE_INVALID) {
940 ptr += btrfs_extent_inline_ref_size(type);
944 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
945 struct btrfs_extent_data_ref *dref;
946 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
947 if (match_extent_data_ref(leaf, dref, root_objectid,
952 if (hash_extent_data_ref_item(leaf, dref) <
953 hash_extent_data_ref(root_objectid, owner, offset))
957 ref_offset = btrfs_extent_inline_ref_offset(leaf, iref);
959 if (parent == ref_offset) {
963 if (ref_offset < parent)
966 if (root_objectid == ref_offset) {
970 if (ref_offset < root_objectid)
974 ptr += btrfs_extent_inline_ref_size(type);
976 if (err == -ENOENT && insert) {
977 if (item_size + extra_size >=
978 BTRFS_MAX_EXTENT_ITEM_SIZE(root)) {
983 * To add new inline back ref, we have to make sure
984 * there is no corresponding back ref item.
985 * For simplicity, we just do not add new inline back
986 * ref if there is any kind of item for this block
988 if (find_next_key(path, 0, &key) == 0 &&
989 key.objectid == bytenr &&
990 key.type < BTRFS_BLOCK_GROUP_ITEM_KEY) {
995 *ref_ret = (struct btrfs_extent_inline_ref *)ptr;
998 path->keep_locks = 0;
999 btrfs_unlock_up_safe(path, 1);
1005 * helper to add new inline back ref
1007 static noinline_for_stack
1008 void setup_inline_extent_backref(struct btrfs_fs_info *fs_info,
1009 struct btrfs_path *path,
1010 struct btrfs_extent_inline_ref *iref,
1011 u64 parent, u64 root_objectid,
1012 u64 owner, u64 offset, int refs_to_add,
1013 struct btrfs_delayed_extent_op *extent_op)
1015 struct extent_buffer *leaf;
1016 struct btrfs_extent_item *ei;
1019 unsigned long item_offset;
1024 leaf = path->nodes[0];
1025 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1026 item_offset = (unsigned long)iref - (unsigned long)ei;
1028 type = extent_ref_type(parent, owner);
1029 size = btrfs_extent_inline_ref_size(type);
1031 btrfs_extend_item(path, size);
1033 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1034 refs = btrfs_extent_refs(leaf, ei);
1035 refs += refs_to_add;
1036 btrfs_set_extent_refs(leaf, ei, refs);
1038 __run_delayed_extent_op(extent_op, leaf, ei);
1040 ptr = (unsigned long)ei + item_offset;
1041 end = (unsigned long)ei + btrfs_item_size_nr(leaf, path->slots[0]);
1042 if (ptr < end - size)
1043 memmove_extent_buffer(leaf, ptr + size, ptr,
1046 iref = (struct btrfs_extent_inline_ref *)ptr;
1047 btrfs_set_extent_inline_ref_type(leaf, iref, type);
1048 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1049 struct btrfs_extent_data_ref *dref;
1050 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1051 btrfs_set_extent_data_ref_root(leaf, dref, root_objectid);
1052 btrfs_set_extent_data_ref_objectid(leaf, dref, owner);
1053 btrfs_set_extent_data_ref_offset(leaf, dref, offset);
1054 btrfs_set_extent_data_ref_count(leaf, dref, refs_to_add);
1055 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1056 struct btrfs_shared_data_ref *sref;
1057 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1058 btrfs_set_shared_data_ref_count(leaf, sref, refs_to_add);
1059 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1060 } else if (type == BTRFS_SHARED_BLOCK_REF_KEY) {
1061 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
1063 btrfs_set_extent_inline_ref_offset(leaf, iref, root_objectid);
1065 btrfs_mark_buffer_dirty(leaf);
1068 static int lookup_extent_backref(struct btrfs_trans_handle *trans,
1069 struct btrfs_path *path,
1070 struct btrfs_extent_inline_ref **ref_ret,
1071 u64 bytenr, u64 num_bytes, u64 parent,
1072 u64 root_objectid, u64 owner, u64 offset)
1076 ret = lookup_inline_extent_backref(trans, path, ref_ret, bytenr,
1077 num_bytes, parent, root_objectid,
1082 btrfs_release_path(path);
1085 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1086 ret = lookup_tree_block_ref(trans, path, bytenr, parent,
1089 ret = lookup_extent_data_ref(trans, path, bytenr, parent,
1090 root_objectid, owner, offset);
1096 * helper to update/remove inline back ref
1098 static noinline_for_stack
1099 void update_inline_extent_backref(struct btrfs_path *path,
1100 struct btrfs_extent_inline_ref *iref,
1102 struct btrfs_delayed_extent_op *extent_op,
1105 struct extent_buffer *leaf = path->nodes[0];
1106 struct btrfs_extent_item *ei;
1107 struct btrfs_extent_data_ref *dref = NULL;
1108 struct btrfs_shared_data_ref *sref = NULL;
1116 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1117 refs = btrfs_extent_refs(leaf, ei);
1118 WARN_ON(refs_to_mod < 0 && refs + refs_to_mod <= 0);
1119 refs += refs_to_mod;
1120 btrfs_set_extent_refs(leaf, ei, refs);
1122 __run_delayed_extent_op(extent_op, leaf, ei);
1125 * If type is invalid, we should have bailed out after
1126 * lookup_inline_extent_backref().
1128 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_ANY);
1129 ASSERT(type != BTRFS_REF_TYPE_INVALID);
1131 if (type == BTRFS_EXTENT_DATA_REF_KEY) {
1132 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1133 refs = btrfs_extent_data_ref_count(leaf, dref);
1134 } else if (type == BTRFS_SHARED_DATA_REF_KEY) {
1135 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1136 refs = btrfs_shared_data_ref_count(leaf, sref);
1139 BUG_ON(refs_to_mod != -1);
1142 BUG_ON(refs_to_mod < 0 && refs < -refs_to_mod);
1143 refs += refs_to_mod;
1146 if (type == BTRFS_EXTENT_DATA_REF_KEY)
1147 btrfs_set_extent_data_ref_count(leaf, dref, refs);
1149 btrfs_set_shared_data_ref_count(leaf, sref, refs);
1152 size = btrfs_extent_inline_ref_size(type);
1153 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1154 ptr = (unsigned long)iref;
1155 end = (unsigned long)ei + item_size;
1156 if (ptr + size < end)
1157 memmove_extent_buffer(leaf, ptr, ptr + size,
1160 btrfs_truncate_item(path, item_size, 1);
1162 btrfs_mark_buffer_dirty(leaf);
1165 static noinline_for_stack
1166 int insert_inline_extent_backref(struct btrfs_trans_handle *trans,
1167 struct btrfs_path *path,
1168 u64 bytenr, u64 num_bytes, u64 parent,
1169 u64 root_objectid, u64 owner,
1170 u64 offset, int refs_to_add,
1171 struct btrfs_delayed_extent_op *extent_op)
1173 struct btrfs_extent_inline_ref *iref;
1176 ret = lookup_inline_extent_backref(trans, path, &iref, bytenr,
1177 num_bytes, parent, root_objectid,
1181 * We're adding refs to a tree block we already own, this
1182 * should not happen at all.
1184 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1185 btrfs_crit(trans->fs_info,
1186 "adding refs to an existing tree ref, bytenr %llu num_bytes %llu root_objectid %llu",
1187 bytenr, num_bytes, root_objectid);
1188 if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
1190 btrfs_crit(trans->fs_info,
1191 "path->slots[0]=%d path->nodes[0]:", path->slots[0]);
1192 btrfs_print_leaf(path->nodes[0]);
1196 update_inline_extent_backref(path, iref, refs_to_add,
1198 } else if (ret == -ENOENT) {
1199 setup_inline_extent_backref(trans->fs_info, path, iref, parent,
1200 root_objectid, owner, offset,
1201 refs_to_add, extent_op);
1207 static int remove_extent_backref(struct btrfs_trans_handle *trans,
1208 struct btrfs_path *path,
1209 struct btrfs_extent_inline_ref *iref,
1210 int refs_to_drop, int is_data, int *last_ref)
1214 BUG_ON(!is_data && refs_to_drop != 1);
1216 update_inline_extent_backref(path, iref, -refs_to_drop, NULL,
1218 } else if (is_data) {
1219 ret = remove_extent_data_ref(trans, path, refs_to_drop,
1223 ret = btrfs_del_item(trans, trans->fs_info->extent_root, path);
1228 static int btrfs_issue_discard(struct block_device *bdev, u64 start, u64 len,
1229 u64 *discarded_bytes)
1232 u64 bytes_left, end;
1233 u64 aligned_start = ALIGN(start, 1 << 9);
1235 if (WARN_ON(start != aligned_start)) {
1236 len -= aligned_start - start;
1237 len = round_down(len, 1 << 9);
1238 start = aligned_start;
1241 *discarded_bytes = 0;
1249 /* Skip any superblocks on this device. */
1250 for (j = 0; j < BTRFS_SUPER_MIRROR_MAX; j++) {
1251 u64 sb_start = btrfs_sb_offset(j);
1252 u64 sb_end = sb_start + BTRFS_SUPER_INFO_SIZE;
1253 u64 size = sb_start - start;
1255 if (!in_range(sb_start, start, bytes_left) &&
1256 !in_range(sb_end, start, bytes_left) &&
1257 !in_range(start, sb_start, BTRFS_SUPER_INFO_SIZE))
1261 * Superblock spans beginning of range. Adjust start and
1264 if (sb_start <= start) {
1265 start += sb_end - start;
1270 bytes_left = end - start;
1275 ret = blkdev_issue_discard(bdev, start >> 9, size >> 9,
1278 *discarded_bytes += size;
1279 else if (ret != -EOPNOTSUPP)
1288 bytes_left = end - start;
1292 ret = blkdev_issue_discard(bdev, start >> 9, bytes_left >> 9,
1295 *discarded_bytes += bytes_left;
1300 int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
1301 u64 num_bytes, u64 *actual_bytes)
1304 u64 discarded_bytes = 0;
1305 u64 end = bytenr + num_bytes;
1307 struct btrfs_bio *bbio = NULL;
1311 * Avoid races with device replace and make sure our bbio has devices
1312 * associated to its stripes that don't go away while we are discarding.
1314 btrfs_bio_counter_inc_blocked(fs_info);
1316 struct btrfs_bio_stripe *stripe;
1319 num_bytes = end - cur;
1320 /* Tell the block device(s) that the sectors can be discarded */
1321 ret = btrfs_map_block(fs_info, BTRFS_MAP_DISCARD, cur,
1322 &num_bytes, &bbio, 0);
1324 * Error can be -ENOMEM, -ENOENT (no such chunk mapping) or
1325 * -EOPNOTSUPP. For any such error, @num_bytes is not updated,
1326 * thus we can't continue anyway.
1331 stripe = bbio->stripes;
1332 for (i = 0; i < bbio->num_stripes; i++, stripe++) {
1334 struct request_queue *req_q;
1336 if (!stripe->dev->bdev) {
1337 ASSERT(btrfs_test_opt(fs_info, DEGRADED));
1340 req_q = bdev_get_queue(stripe->dev->bdev);
1341 if (!blk_queue_discard(req_q))
1344 ret = btrfs_issue_discard(stripe->dev->bdev,
1349 discarded_bytes += bytes;
1350 } else if (ret != -EOPNOTSUPP) {
1352 * Logic errors or -ENOMEM, or -EIO, but
1353 * unlikely to happen.
1355 * And since there are two loops, explicitly
1356 * go to out to avoid confusion.
1358 btrfs_put_bbio(bbio);
1363 * Just in case we get back EOPNOTSUPP for some reason,
1364 * just ignore the return value so we don't screw up
1365 * people calling discard_extent.
1369 btrfs_put_bbio(bbio);
1373 btrfs_bio_counter_dec(fs_info);
1376 *actual_bytes = discarded_bytes;
1379 if (ret == -EOPNOTSUPP)
1384 /* Can return -ENOMEM */
1385 int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1386 struct btrfs_ref *generic_ref)
1388 struct btrfs_fs_info *fs_info = trans->fs_info;
1389 int old_ref_mod, new_ref_mod;
1392 ASSERT(generic_ref->type != BTRFS_REF_NOT_SET &&
1393 generic_ref->action);
1394 BUG_ON(generic_ref->type == BTRFS_REF_METADATA &&
1395 generic_ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID);
1397 if (generic_ref->type == BTRFS_REF_METADATA)
1398 ret = btrfs_add_delayed_tree_ref(trans, generic_ref,
1399 NULL, &old_ref_mod, &new_ref_mod);
1401 ret = btrfs_add_delayed_data_ref(trans, generic_ref, 0,
1402 &old_ref_mod, &new_ref_mod);
1404 btrfs_ref_tree_mod(fs_info, generic_ref);
1406 if (ret == 0 && old_ref_mod < 0 && new_ref_mod >= 0)
1407 sub_pinned_bytes(fs_info, generic_ref);
1413 * __btrfs_inc_extent_ref - insert backreference for a given extent
1415 * The counterpart is in __btrfs_free_extent(), with examples and more details
1418 * @trans: Handle of transaction
1420 * @node: The delayed ref node used to get the bytenr/length for
1421 * extent whose references are incremented.
1423 * @parent: If this is a shared extent (BTRFS_SHARED_DATA_REF_KEY/
1424 * BTRFS_SHARED_BLOCK_REF_KEY) then it holds the logical
1425 * bytenr of the parent block. Since new extents are always
1426 * created with indirect references, this will only be the case
1427 * when relocating a shared extent. In that case, root_objectid
1428 * will be BTRFS_TREE_RELOC_OBJECTID. Otheriwse, parent must
1431 * @root_objectid: The id of the root where this modification has originated,
1432 * this can be either one of the well-known metadata trees or
1433 * the subvolume id which references this extent.
1435 * @owner: For data extents it is the inode number of the owning file.
1436 * For metadata extents this parameter holds the level in the
1437 * tree of the extent.
1439 * @offset: For metadata extents the offset is ignored and is currently
1440 * always passed as 0. For data extents it is the fileoffset
1441 * this extent belongs to.
1443 * @refs_to_add Number of references to add
1445 * @extent_op Pointer to a structure, holding information necessary when
1446 * updating a tree block's flags
1449 static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
1450 struct btrfs_delayed_ref_node *node,
1451 u64 parent, u64 root_objectid,
1452 u64 owner, u64 offset, int refs_to_add,
1453 struct btrfs_delayed_extent_op *extent_op)
1455 struct btrfs_path *path;
1456 struct extent_buffer *leaf;
1457 struct btrfs_extent_item *item;
1458 struct btrfs_key key;
1459 u64 bytenr = node->bytenr;
1460 u64 num_bytes = node->num_bytes;
1464 path = btrfs_alloc_path();
1468 path->leave_spinning = 1;
1469 /* this will setup the path even if it fails to insert the back ref */
1470 ret = insert_inline_extent_backref(trans, path, bytenr, num_bytes,
1471 parent, root_objectid, owner,
1472 offset, refs_to_add, extent_op);
1473 if ((ret < 0 && ret != -EAGAIN) || !ret)
1477 * Ok we had -EAGAIN which means we didn't have space to insert and
1478 * inline extent ref, so just update the reference count and add a
1481 leaf = path->nodes[0];
1482 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
1483 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1484 refs = btrfs_extent_refs(leaf, item);
1485 btrfs_set_extent_refs(leaf, item, refs + refs_to_add);
1487 __run_delayed_extent_op(extent_op, leaf, item);
1489 btrfs_mark_buffer_dirty(leaf);
1490 btrfs_release_path(path);
1492 path->leave_spinning = 1;
1493 /* now insert the actual backref */
1494 if (owner < BTRFS_FIRST_FREE_OBJECTID) {
1495 BUG_ON(refs_to_add != 1);
1496 ret = insert_tree_block_ref(trans, path, bytenr, parent,
1499 ret = insert_extent_data_ref(trans, path, bytenr, parent,
1500 root_objectid, owner, offset,
1504 btrfs_abort_transaction(trans, ret);
1506 btrfs_free_path(path);
1510 static int run_delayed_data_ref(struct btrfs_trans_handle *trans,
1511 struct btrfs_delayed_ref_node *node,
1512 struct btrfs_delayed_extent_op *extent_op,
1513 int insert_reserved)
1516 struct btrfs_delayed_data_ref *ref;
1517 struct btrfs_key ins;
1522 ins.objectid = node->bytenr;
1523 ins.offset = node->num_bytes;
1524 ins.type = BTRFS_EXTENT_ITEM_KEY;
1526 ref = btrfs_delayed_node_to_data_ref(node);
1527 trace_run_delayed_data_ref(trans->fs_info, node, ref, node->action);
1529 if (node->type == BTRFS_SHARED_DATA_REF_KEY)
1530 parent = ref->parent;
1531 ref_root = ref->root;
1533 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1535 flags |= extent_op->flags_to_set;
1536 ret = alloc_reserved_file_extent(trans, parent, ref_root,
1537 flags, ref->objectid,
1540 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1541 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1542 ref->objectid, ref->offset,
1543 node->ref_mod, extent_op);
1544 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1545 ret = __btrfs_free_extent(trans, node, parent,
1546 ref_root, ref->objectid,
1547 ref->offset, node->ref_mod,
1555 static void __run_delayed_extent_op(struct btrfs_delayed_extent_op *extent_op,
1556 struct extent_buffer *leaf,
1557 struct btrfs_extent_item *ei)
1559 u64 flags = btrfs_extent_flags(leaf, ei);
1560 if (extent_op->update_flags) {
1561 flags |= extent_op->flags_to_set;
1562 btrfs_set_extent_flags(leaf, ei, flags);
1565 if (extent_op->update_key) {
1566 struct btrfs_tree_block_info *bi;
1567 BUG_ON(!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK));
1568 bi = (struct btrfs_tree_block_info *)(ei + 1);
1569 btrfs_set_tree_block_key(leaf, bi, &extent_op->key);
1573 static int run_delayed_extent_op(struct btrfs_trans_handle *trans,
1574 struct btrfs_delayed_ref_head *head,
1575 struct btrfs_delayed_extent_op *extent_op)
1577 struct btrfs_fs_info *fs_info = trans->fs_info;
1578 struct btrfs_key key;
1579 struct btrfs_path *path;
1580 struct btrfs_extent_item *ei;
1581 struct extent_buffer *leaf;
1585 int metadata = !extent_op->is_data;
1587 if (TRANS_ABORTED(trans))
1590 if (metadata && !btrfs_fs_incompat(fs_info, SKINNY_METADATA))
1593 path = btrfs_alloc_path();
1597 key.objectid = head->bytenr;
1600 key.type = BTRFS_METADATA_ITEM_KEY;
1601 key.offset = extent_op->level;
1603 key.type = BTRFS_EXTENT_ITEM_KEY;
1604 key.offset = head->num_bytes;
1608 path->leave_spinning = 1;
1609 ret = btrfs_search_slot(trans, fs_info->extent_root, &key, path, 0, 1);
1616 if (path->slots[0] > 0) {
1618 btrfs_item_key_to_cpu(path->nodes[0], &key,
1620 if (key.objectid == head->bytenr &&
1621 key.type == BTRFS_EXTENT_ITEM_KEY &&
1622 key.offset == head->num_bytes)
1626 btrfs_release_path(path);
1629 key.objectid = head->bytenr;
1630 key.offset = head->num_bytes;
1631 key.type = BTRFS_EXTENT_ITEM_KEY;
1640 leaf = path->nodes[0];
1641 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
1643 if (unlikely(item_size < sizeof(*ei))) {
1645 btrfs_print_v0_err(fs_info);
1646 btrfs_abort_transaction(trans, err);
1650 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
1651 __run_delayed_extent_op(extent_op, leaf, ei);
1653 btrfs_mark_buffer_dirty(leaf);
1655 btrfs_free_path(path);
1659 static int run_delayed_tree_ref(struct btrfs_trans_handle *trans,
1660 struct btrfs_delayed_ref_node *node,
1661 struct btrfs_delayed_extent_op *extent_op,
1662 int insert_reserved)
1665 struct btrfs_delayed_tree_ref *ref;
1669 ref = btrfs_delayed_node_to_tree_ref(node);
1670 trace_run_delayed_tree_ref(trans->fs_info, node, ref, node->action);
1672 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1673 parent = ref->parent;
1674 ref_root = ref->root;
1676 if (node->ref_mod != 1) {
1677 btrfs_err(trans->fs_info,
1678 "btree block(%llu) has %d references rather than 1: action %d ref_root %llu parent %llu",
1679 node->bytenr, node->ref_mod, node->action, ref_root,
1683 if (node->action == BTRFS_ADD_DELAYED_REF && insert_reserved) {
1684 BUG_ON(!extent_op || !extent_op->update_flags);
1685 ret = alloc_reserved_tree_block(trans, node, extent_op);
1686 } else if (node->action == BTRFS_ADD_DELAYED_REF) {
1687 ret = __btrfs_inc_extent_ref(trans, node, parent, ref_root,
1688 ref->level, 0, 1, extent_op);
1689 } else if (node->action == BTRFS_DROP_DELAYED_REF) {
1690 ret = __btrfs_free_extent(trans, node, parent, ref_root,
1691 ref->level, 0, 1, extent_op);
1698 /* helper function to actually process a single delayed ref entry */
1699 static int run_one_delayed_ref(struct btrfs_trans_handle *trans,
1700 struct btrfs_delayed_ref_node *node,
1701 struct btrfs_delayed_extent_op *extent_op,
1702 int insert_reserved)
1706 if (TRANS_ABORTED(trans)) {
1707 if (insert_reserved)
1708 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
1712 if (node->type == BTRFS_TREE_BLOCK_REF_KEY ||
1713 node->type == BTRFS_SHARED_BLOCK_REF_KEY)
1714 ret = run_delayed_tree_ref(trans, node, extent_op,
1716 else if (node->type == BTRFS_EXTENT_DATA_REF_KEY ||
1717 node->type == BTRFS_SHARED_DATA_REF_KEY)
1718 ret = run_delayed_data_ref(trans, node, extent_op,
1722 if (ret && insert_reserved)
1723 btrfs_pin_extent(trans, node->bytenr, node->num_bytes, 1);
1727 static inline struct btrfs_delayed_ref_node *
1728 select_delayed_ref(struct btrfs_delayed_ref_head *head)
1730 struct btrfs_delayed_ref_node *ref;
1732 if (RB_EMPTY_ROOT(&head->ref_tree.rb_root))
1736 * Select a delayed ref of type BTRFS_ADD_DELAYED_REF first.
1737 * This is to prevent a ref count from going down to zero, which deletes
1738 * the extent item from the extent tree, when there still are references
1739 * to add, which would fail because they would not find the extent item.
1741 if (!list_empty(&head->ref_add_list))
1742 return list_first_entry(&head->ref_add_list,
1743 struct btrfs_delayed_ref_node, add_list);
1745 ref = rb_entry(rb_first_cached(&head->ref_tree),
1746 struct btrfs_delayed_ref_node, ref_node);
1747 ASSERT(list_empty(&ref->add_list));
1751 static void unselect_delayed_ref_head(struct btrfs_delayed_ref_root *delayed_refs,
1752 struct btrfs_delayed_ref_head *head)
1754 spin_lock(&delayed_refs->lock);
1755 head->processing = 0;
1756 delayed_refs->num_heads_ready++;
1757 spin_unlock(&delayed_refs->lock);
1758 btrfs_delayed_ref_unlock(head);
1761 static struct btrfs_delayed_extent_op *cleanup_extent_op(
1762 struct btrfs_delayed_ref_head *head)
1764 struct btrfs_delayed_extent_op *extent_op = head->extent_op;
1769 if (head->must_insert_reserved) {
1770 head->extent_op = NULL;
1771 btrfs_free_delayed_extent_op(extent_op);
1777 static int run_and_cleanup_extent_op(struct btrfs_trans_handle *trans,
1778 struct btrfs_delayed_ref_head *head)
1780 struct btrfs_delayed_extent_op *extent_op;
1783 extent_op = cleanup_extent_op(head);
1786 head->extent_op = NULL;
1787 spin_unlock(&head->lock);
1788 ret = run_delayed_extent_op(trans, head, extent_op);
1789 btrfs_free_delayed_extent_op(extent_op);
1790 return ret ? ret : 1;
1793 void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
1794 struct btrfs_delayed_ref_root *delayed_refs,
1795 struct btrfs_delayed_ref_head *head)
1797 int nr_items = 1; /* Dropping this ref head update. */
1799 if (head->total_ref_mod < 0) {
1800 struct btrfs_space_info *space_info;
1804 flags = BTRFS_BLOCK_GROUP_DATA;
1805 else if (head->is_system)
1806 flags = BTRFS_BLOCK_GROUP_SYSTEM;
1808 flags = BTRFS_BLOCK_GROUP_METADATA;
1809 space_info = btrfs_find_space_info(fs_info, flags);
1811 percpu_counter_add_batch(&space_info->total_bytes_pinned,
1813 BTRFS_TOTAL_BYTES_PINNED_BATCH);
1816 * We had csum deletions accounted for in our delayed refs rsv,
1817 * we need to drop the csum leaves for this update from our
1820 if (head->is_data) {
1821 spin_lock(&delayed_refs->lock);
1822 delayed_refs->pending_csums -= head->num_bytes;
1823 spin_unlock(&delayed_refs->lock);
1824 nr_items += btrfs_csum_bytes_to_leaves(fs_info,
1829 btrfs_delayed_refs_rsv_release(fs_info, nr_items);
1832 static int cleanup_ref_head(struct btrfs_trans_handle *trans,
1833 struct btrfs_delayed_ref_head *head)
1836 struct btrfs_fs_info *fs_info = trans->fs_info;
1837 struct btrfs_delayed_ref_root *delayed_refs;
1840 delayed_refs = &trans->transaction->delayed_refs;
1842 ret = run_and_cleanup_extent_op(trans, head);
1844 unselect_delayed_ref_head(delayed_refs, head);
1845 btrfs_debug(fs_info, "run_delayed_extent_op returned %d", ret);
1852 * Need to drop our head ref lock and re-acquire the delayed ref lock
1853 * and then re-check to make sure nobody got added.
1855 spin_unlock(&head->lock);
1856 spin_lock(&delayed_refs->lock);
1857 spin_lock(&head->lock);
1858 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root) || head->extent_op) {
1859 spin_unlock(&head->lock);
1860 spin_unlock(&delayed_refs->lock);
1863 btrfs_delete_ref_head(delayed_refs, head);
1864 spin_unlock(&head->lock);
1865 spin_unlock(&delayed_refs->lock);
1867 if (head->must_insert_reserved) {
1868 btrfs_pin_extent(trans, head->bytenr, head->num_bytes, 1);
1869 if (head->is_data) {
1870 ret = btrfs_del_csums(trans, fs_info->csum_root,
1871 head->bytenr, head->num_bytes);
1875 btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
1877 trace_run_delayed_ref_head(fs_info, head, 0);
1878 btrfs_delayed_ref_unlock(head);
1879 btrfs_put_delayed_ref_head(head);
1883 static struct btrfs_delayed_ref_head *btrfs_obtain_ref_head(
1884 struct btrfs_trans_handle *trans)
1886 struct btrfs_delayed_ref_root *delayed_refs =
1887 &trans->transaction->delayed_refs;
1888 struct btrfs_delayed_ref_head *head = NULL;
1891 spin_lock(&delayed_refs->lock);
1892 head = btrfs_select_ref_head(delayed_refs);
1894 spin_unlock(&delayed_refs->lock);
1899 * Grab the lock that says we are going to process all the refs for
1902 ret = btrfs_delayed_ref_lock(delayed_refs, head);
1903 spin_unlock(&delayed_refs->lock);
1906 * We may have dropped the spin lock to get the head mutex lock, and
1907 * that might have given someone else time to free the head. If that's
1908 * true, it has been removed from our list and we can move on.
1911 head = ERR_PTR(-EAGAIN);
1916 static int btrfs_run_delayed_refs_for_head(struct btrfs_trans_handle *trans,
1917 struct btrfs_delayed_ref_head *locked_ref,
1918 unsigned long *run_refs)
1920 struct btrfs_fs_info *fs_info = trans->fs_info;
1921 struct btrfs_delayed_ref_root *delayed_refs;
1922 struct btrfs_delayed_extent_op *extent_op;
1923 struct btrfs_delayed_ref_node *ref;
1924 int must_insert_reserved = 0;
1927 delayed_refs = &trans->transaction->delayed_refs;
1929 lockdep_assert_held(&locked_ref->mutex);
1930 lockdep_assert_held(&locked_ref->lock);
1932 while ((ref = select_delayed_ref(locked_ref))) {
1934 btrfs_check_delayed_seq(fs_info, ref->seq)) {
1935 spin_unlock(&locked_ref->lock);
1936 unselect_delayed_ref_head(delayed_refs, locked_ref);
1942 rb_erase_cached(&ref->ref_node, &locked_ref->ref_tree);
1943 RB_CLEAR_NODE(&ref->ref_node);
1944 if (!list_empty(&ref->add_list))
1945 list_del(&ref->add_list);
1947 * When we play the delayed ref, also correct the ref_mod on
1950 switch (ref->action) {
1951 case BTRFS_ADD_DELAYED_REF:
1952 case BTRFS_ADD_DELAYED_EXTENT:
1953 locked_ref->ref_mod -= ref->ref_mod;
1955 case BTRFS_DROP_DELAYED_REF:
1956 locked_ref->ref_mod += ref->ref_mod;
1961 atomic_dec(&delayed_refs->num_entries);
1964 * Record the must_insert_reserved flag before we drop the
1967 must_insert_reserved = locked_ref->must_insert_reserved;
1968 locked_ref->must_insert_reserved = 0;
1970 extent_op = locked_ref->extent_op;
1971 locked_ref->extent_op = NULL;
1972 spin_unlock(&locked_ref->lock);
1974 ret = run_one_delayed_ref(trans, ref, extent_op,
1975 must_insert_reserved);
1977 btrfs_free_delayed_extent_op(extent_op);
1979 unselect_delayed_ref_head(delayed_refs, locked_ref);
1980 btrfs_put_delayed_ref(ref);
1981 btrfs_debug(fs_info, "run_one_delayed_ref returned %d",
1986 btrfs_put_delayed_ref(ref);
1989 spin_lock(&locked_ref->lock);
1990 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
1997 * Returns 0 on success or if called with an already aborted transaction.
1998 * Returns -ENOMEM or -EIO on failure and will abort the transaction.
2000 static noinline int __btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2003 struct btrfs_fs_info *fs_info = trans->fs_info;
2004 struct btrfs_delayed_ref_root *delayed_refs;
2005 struct btrfs_delayed_ref_head *locked_ref = NULL;
2006 ktime_t start = ktime_get();
2008 unsigned long count = 0;
2009 unsigned long actual_count = 0;
2011 delayed_refs = &trans->transaction->delayed_refs;
2014 locked_ref = btrfs_obtain_ref_head(trans);
2015 if (IS_ERR_OR_NULL(locked_ref)) {
2016 if (PTR_ERR(locked_ref) == -EAGAIN) {
2025 * We need to try and merge add/drops of the same ref since we
2026 * can run into issues with relocate dropping the implicit ref
2027 * and then it being added back again before the drop can
2028 * finish. If we merged anything we need to re-loop so we can
2030 * Or we can get node references of the same type that weren't
2031 * merged when created due to bumps in the tree mod seq, and
2032 * we need to merge them to prevent adding an inline extent
2033 * backref before dropping it (triggering a BUG_ON at
2034 * insert_inline_extent_backref()).
2036 spin_lock(&locked_ref->lock);
2037 btrfs_merge_delayed_refs(trans, delayed_refs, locked_ref);
2039 ret = btrfs_run_delayed_refs_for_head(trans, locked_ref,
2041 if (ret < 0 && ret != -EAGAIN) {
2043 * Error, btrfs_run_delayed_refs_for_head already
2044 * unlocked everything so just bail out
2049 * Success, perform the usual cleanup of a processed
2052 ret = cleanup_ref_head(trans, locked_ref);
2054 /* We dropped our lock, we need to loop. */
2063 * Either success case or btrfs_run_delayed_refs_for_head
2064 * returned -EAGAIN, meaning we need to select another head
2069 } while ((nr != -1 && count < nr) || locked_ref);
2072 * We don't want to include ref heads since we can have empty ref heads
2073 * and those will drastically skew our runtime down since we just do
2074 * accounting, no actual extent tree updates.
2076 if (actual_count > 0) {
2077 u64 runtime = ktime_to_ns(ktime_sub(ktime_get(), start));
2081 * We weigh the current average higher than our current runtime
2082 * to avoid large swings in the average.
2084 spin_lock(&delayed_refs->lock);
2085 avg = fs_info->avg_delayed_ref_runtime * 3 + runtime;
2086 fs_info->avg_delayed_ref_runtime = avg >> 2; /* div by 4 */
2087 spin_unlock(&delayed_refs->lock);
2092 #ifdef SCRAMBLE_DELAYED_REFS
2094 * Normally delayed refs get processed in ascending bytenr order. This
2095 * correlates in most cases to the order added. To expose dependencies on this
2096 * order, we start to process the tree in the middle instead of the beginning
2098 static u64 find_middle(struct rb_root *root)
2100 struct rb_node *n = root->rb_node;
2101 struct btrfs_delayed_ref_node *entry;
2104 u64 first = 0, last = 0;
2108 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2109 first = entry->bytenr;
2113 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2114 last = entry->bytenr;
2119 entry = rb_entry(n, struct btrfs_delayed_ref_node, rb_node);
2120 WARN_ON(!entry->in_tree);
2122 middle = entry->bytenr;
2136 * Takes the number of bytes to be csumm'ed and figures out how many leaves it
2137 * would require to store the csums for that many bytes.
2139 u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes)
2142 u64 num_csums_per_leaf;
2145 csum_size = BTRFS_MAX_ITEM_SIZE(fs_info);
2146 num_csums_per_leaf = div64_u64(csum_size,
2147 (u64)btrfs_super_csum_size(fs_info->super_copy));
2148 num_csums = div64_u64(csum_bytes, fs_info->sectorsize);
2149 num_csums += num_csums_per_leaf - 1;
2150 num_csums = div64_u64(num_csums, num_csums_per_leaf);
2155 * this starts processing the delayed reference count updates and
2156 * extent insertions we have queued up so far. count can be
2157 * 0, which means to process everything in the tree at the start
2158 * of the run (but not newly added entries), or it can be some target
2159 * number you'd like to process.
2161 * Returns 0 on success or if called with an aborted transaction
2162 * Returns <0 on error and aborts the transaction
2164 int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
2165 unsigned long count)
2167 struct btrfs_fs_info *fs_info = trans->fs_info;
2168 struct rb_node *node;
2169 struct btrfs_delayed_ref_root *delayed_refs;
2170 struct btrfs_delayed_ref_head *head;
2172 int run_all = count == (unsigned long)-1;
2174 /* We'll clean this up in btrfs_cleanup_transaction */
2175 if (TRANS_ABORTED(trans))
2178 if (test_bit(BTRFS_FS_CREATING_FREE_SPACE_TREE, &fs_info->flags))
2181 delayed_refs = &trans->transaction->delayed_refs;
2183 count = atomic_read(&delayed_refs->num_entries) * 2;
2186 #ifdef SCRAMBLE_DELAYED_REFS
2187 delayed_refs->run_delayed_start = find_middle(&delayed_refs->root);
2189 ret = __btrfs_run_delayed_refs(trans, count);
2191 btrfs_abort_transaction(trans, ret);
2196 btrfs_create_pending_block_groups(trans);
2198 spin_lock(&delayed_refs->lock);
2199 node = rb_first_cached(&delayed_refs->href_root);
2201 spin_unlock(&delayed_refs->lock);
2204 head = rb_entry(node, struct btrfs_delayed_ref_head,
2206 refcount_inc(&head->refs);
2207 spin_unlock(&delayed_refs->lock);
2209 /* Mutex was contended, block until it's released and retry. */
2210 mutex_lock(&head->mutex);
2211 mutex_unlock(&head->mutex);
2213 btrfs_put_delayed_ref_head(head);
2221 int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
2222 struct extent_buffer *eb, u64 flags,
2223 int level, int is_data)
2225 struct btrfs_delayed_extent_op *extent_op;
2228 extent_op = btrfs_alloc_delayed_extent_op();
2232 extent_op->flags_to_set = flags;
2233 extent_op->update_flags = true;
2234 extent_op->update_key = false;
2235 extent_op->is_data = is_data ? true : false;
2236 extent_op->level = level;
2238 ret = btrfs_add_delayed_extent_op(trans, eb->start, eb->len, extent_op);
2240 btrfs_free_delayed_extent_op(extent_op);
2244 static noinline int check_delayed_ref(struct btrfs_root *root,
2245 struct btrfs_path *path,
2246 u64 objectid, u64 offset, u64 bytenr)
2248 struct btrfs_delayed_ref_head *head;
2249 struct btrfs_delayed_ref_node *ref;
2250 struct btrfs_delayed_data_ref *data_ref;
2251 struct btrfs_delayed_ref_root *delayed_refs;
2252 struct btrfs_transaction *cur_trans;
2253 struct rb_node *node;
2256 spin_lock(&root->fs_info->trans_lock);
2257 cur_trans = root->fs_info->running_transaction;
2259 refcount_inc(&cur_trans->use_count);
2260 spin_unlock(&root->fs_info->trans_lock);
2264 delayed_refs = &cur_trans->delayed_refs;
2265 spin_lock(&delayed_refs->lock);
2266 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
2268 spin_unlock(&delayed_refs->lock);
2269 btrfs_put_transaction(cur_trans);
2273 if (!mutex_trylock(&head->mutex)) {
2274 refcount_inc(&head->refs);
2275 spin_unlock(&delayed_refs->lock);
2277 btrfs_release_path(path);
2280 * Mutex was contended, block until it's released and let
2283 mutex_lock(&head->mutex);
2284 mutex_unlock(&head->mutex);
2285 btrfs_put_delayed_ref_head(head);
2286 btrfs_put_transaction(cur_trans);
2289 spin_unlock(&delayed_refs->lock);
2291 spin_lock(&head->lock);
2293 * XXX: We should replace this with a proper search function in the
2296 for (node = rb_first_cached(&head->ref_tree); node;
2297 node = rb_next(node)) {
2298 ref = rb_entry(node, struct btrfs_delayed_ref_node, ref_node);
2299 /* If it's a shared ref we know a cross reference exists */
2300 if (ref->type != BTRFS_EXTENT_DATA_REF_KEY) {
2305 data_ref = btrfs_delayed_node_to_data_ref(ref);
2308 * If our ref doesn't match the one we're currently looking at
2309 * then we have a cross reference.
2311 if (data_ref->root != root->root_key.objectid ||
2312 data_ref->objectid != objectid ||
2313 data_ref->offset != offset) {
2318 spin_unlock(&head->lock);
2319 mutex_unlock(&head->mutex);
2320 btrfs_put_transaction(cur_trans);
2324 static noinline int check_committed_ref(struct btrfs_root *root,
2325 struct btrfs_path *path,
2326 u64 objectid, u64 offset, u64 bytenr,
2329 struct btrfs_fs_info *fs_info = root->fs_info;
2330 struct btrfs_root *extent_root = fs_info->extent_root;
2331 struct extent_buffer *leaf;
2332 struct btrfs_extent_data_ref *ref;
2333 struct btrfs_extent_inline_ref *iref;
2334 struct btrfs_extent_item *ei;
2335 struct btrfs_key key;
2340 key.objectid = bytenr;
2341 key.offset = (u64)-1;
2342 key.type = BTRFS_EXTENT_ITEM_KEY;
2344 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2347 BUG_ON(ret == 0); /* Corruption */
2350 if (path->slots[0] == 0)
2354 leaf = path->nodes[0];
2355 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2357 if (key.objectid != bytenr || key.type != BTRFS_EXTENT_ITEM_KEY)
2361 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2362 ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_item);
2364 /* If extent item has more than 1 inline ref then it's shared */
2365 if (item_size != sizeof(*ei) +
2366 btrfs_extent_inline_ref_size(BTRFS_EXTENT_DATA_REF_KEY))
2370 * If extent created before last snapshot => it's shared unless the
2371 * snapshot has been deleted. Use the heuristic if strict is false.
2374 (btrfs_extent_generation(leaf, ei) <=
2375 btrfs_root_last_snapshot(&root->root_item)))
2378 iref = (struct btrfs_extent_inline_ref *)(ei + 1);
2380 /* If this extent has SHARED_DATA_REF then it's shared */
2381 type = btrfs_get_extent_inline_ref_type(leaf, iref, BTRFS_REF_TYPE_DATA);
2382 if (type != BTRFS_EXTENT_DATA_REF_KEY)
2385 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
2386 if (btrfs_extent_refs(leaf, ei) !=
2387 btrfs_extent_data_ref_count(leaf, ref) ||
2388 btrfs_extent_data_ref_root(leaf, ref) !=
2389 root->root_key.objectid ||
2390 btrfs_extent_data_ref_objectid(leaf, ref) != objectid ||
2391 btrfs_extent_data_ref_offset(leaf, ref) != offset)
2399 int btrfs_cross_ref_exist(struct btrfs_root *root, u64 objectid, u64 offset,
2400 u64 bytenr, bool strict)
2402 struct btrfs_path *path;
2405 path = btrfs_alloc_path();
2410 ret = check_committed_ref(root, path, objectid,
2411 offset, bytenr, strict);
2412 if (ret && ret != -ENOENT)
2415 ret = check_delayed_ref(root, path, objectid, offset, bytenr);
2416 } while (ret == -EAGAIN);
2419 btrfs_free_path(path);
2420 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
2425 static int __btrfs_mod_ref(struct btrfs_trans_handle *trans,
2426 struct btrfs_root *root,
2427 struct extent_buffer *buf,
2428 int full_backref, int inc)
2430 struct btrfs_fs_info *fs_info = root->fs_info;
2436 struct btrfs_key key;
2437 struct btrfs_file_extent_item *fi;
2438 struct btrfs_ref generic_ref = { 0 };
2439 bool for_reloc = btrfs_header_flag(buf, BTRFS_HEADER_FLAG_RELOC);
2445 if (btrfs_is_testing(fs_info))
2448 ref_root = btrfs_header_owner(buf);
2449 nritems = btrfs_header_nritems(buf);
2450 level = btrfs_header_level(buf);
2452 if (!test_bit(BTRFS_ROOT_SHAREABLE, &root->state) && level == 0)
2456 parent = buf->start;
2460 action = BTRFS_ADD_DELAYED_REF;
2462 action = BTRFS_DROP_DELAYED_REF;
2464 for (i = 0; i < nritems; i++) {
2466 btrfs_item_key_to_cpu(buf, &key, i);
2467 if (key.type != BTRFS_EXTENT_DATA_KEY)
2469 fi = btrfs_item_ptr(buf, i,
2470 struct btrfs_file_extent_item);
2471 if (btrfs_file_extent_type(buf, fi) ==
2472 BTRFS_FILE_EXTENT_INLINE)
2474 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
2478 num_bytes = btrfs_file_extent_disk_num_bytes(buf, fi);
2479 key.offset -= btrfs_file_extent_offset(buf, fi);
2480 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2482 generic_ref.real_root = root->root_key.objectid;
2483 btrfs_init_data_ref(&generic_ref, ref_root, key.objectid,
2485 generic_ref.skip_qgroup = for_reloc;
2487 ret = btrfs_inc_extent_ref(trans, &generic_ref);
2489 ret = btrfs_free_extent(trans, &generic_ref);
2493 bytenr = btrfs_node_blockptr(buf, i);
2494 num_bytes = fs_info->nodesize;
2495 btrfs_init_generic_ref(&generic_ref, action, bytenr,
2497 generic_ref.real_root = root->root_key.objectid;
2498 btrfs_init_tree_ref(&generic_ref, level - 1, ref_root);
2499 generic_ref.skip_qgroup = for_reloc;
2501 ret = btrfs_inc_extent_ref(trans, &generic_ref);
2503 ret = btrfs_free_extent(trans, &generic_ref);
2513 int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2514 struct extent_buffer *buf, int full_backref)
2516 return __btrfs_mod_ref(trans, root, buf, full_backref, 1);
2519 int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
2520 struct extent_buffer *buf, int full_backref)
2522 return __btrfs_mod_ref(trans, root, buf, full_backref, 0);
2525 int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr)
2527 struct btrfs_block_group *block_group;
2530 block_group = btrfs_lookup_block_group(fs_info, bytenr);
2531 if (!block_group || block_group->ro)
2534 btrfs_put_block_group(block_group);
2538 static u64 get_alloc_profile_by_root(struct btrfs_root *root, int data)
2540 struct btrfs_fs_info *fs_info = root->fs_info;
2545 flags = BTRFS_BLOCK_GROUP_DATA;
2546 else if (root == fs_info->chunk_root)
2547 flags = BTRFS_BLOCK_GROUP_SYSTEM;
2549 flags = BTRFS_BLOCK_GROUP_METADATA;
2551 ret = btrfs_get_alloc_profile(fs_info, flags);
2555 static u64 first_logical_byte(struct btrfs_fs_info *fs_info, u64 search_start)
2557 struct btrfs_block_group *cache;
2560 spin_lock(&fs_info->block_group_cache_lock);
2561 bytenr = fs_info->first_logical_byte;
2562 spin_unlock(&fs_info->block_group_cache_lock);
2564 if (bytenr < (u64)-1)
2567 cache = btrfs_lookup_first_block_group(fs_info, search_start);
2571 bytenr = cache->start;
2572 btrfs_put_block_group(cache);
2577 static int pin_down_extent(struct btrfs_trans_handle *trans,
2578 struct btrfs_block_group *cache,
2579 u64 bytenr, u64 num_bytes, int reserved)
2581 struct btrfs_fs_info *fs_info = cache->fs_info;
2583 spin_lock(&cache->space_info->lock);
2584 spin_lock(&cache->lock);
2585 cache->pinned += num_bytes;
2586 btrfs_space_info_update_bytes_pinned(fs_info, cache->space_info,
2589 cache->reserved -= num_bytes;
2590 cache->space_info->bytes_reserved -= num_bytes;
2592 spin_unlock(&cache->lock);
2593 spin_unlock(&cache->space_info->lock);
2595 percpu_counter_add_batch(&cache->space_info->total_bytes_pinned,
2596 num_bytes, BTRFS_TOTAL_BYTES_PINNED_BATCH);
2597 set_extent_dirty(&trans->transaction->pinned_extents, bytenr,
2598 bytenr + num_bytes - 1, GFP_NOFS | __GFP_NOFAIL);
2602 int btrfs_pin_extent(struct btrfs_trans_handle *trans,
2603 u64 bytenr, u64 num_bytes, int reserved)
2605 struct btrfs_block_group *cache;
2607 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
2608 BUG_ON(!cache); /* Logic error */
2610 pin_down_extent(trans, cache, bytenr, num_bytes, reserved);
2612 btrfs_put_block_group(cache);
2617 * this function must be called within transaction
2619 int btrfs_pin_extent_for_log_replay(struct btrfs_trans_handle *trans,
2620 u64 bytenr, u64 num_bytes)
2622 struct btrfs_block_group *cache;
2625 btrfs_add_excluded_extent(trans->fs_info, bytenr, num_bytes);
2627 cache = btrfs_lookup_block_group(trans->fs_info, bytenr);
2632 * pull in the free space cache (if any) so that our pin
2633 * removes the free space from the cache. We have load_only set
2634 * to one because the slow code to read in the free extents does check
2635 * the pinned extents.
2637 btrfs_cache_block_group(cache, 1);
2639 pin_down_extent(trans, cache, bytenr, num_bytes, 0);
2641 /* remove us from the free space cache (if we're there at all) */
2642 ret = btrfs_remove_free_space(cache, bytenr, num_bytes);
2643 btrfs_put_block_group(cache);
2647 static int __exclude_logged_extent(struct btrfs_fs_info *fs_info,
2648 u64 start, u64 num_bytes)
2651 struct btrfs_block_group *block_group;
2652 struct btrfs_caching_control *caching_ctl;
2654 block_group = btrfs_lookup_block_group(fs_info, start);
2658 btrfs_cache_block_group(block_group, 0);
2659 caching_ctl = btrfs_get_caching_control(block_group);
2663 BUG_ON(!btrfs_block_group_done(block_group));
2664 ret = btrfs_remove_free_space(block_group, start, num_bytes);
2666 mutex_lock(&caching_ctl->mutex);
2668 if (start >= caching_ctl->progress) {
2669 ret = btrfs_add_excluded_extent(fs_info, start,
2671 } else if (start + num_bytes <= caching_ctl->progress) {
2672 ret = btrfs_remove_free_space(block_group,
2675 num_bytes = caching_ctl->progress - start;
2676 ret = btrfs_remove_free_space(block_group,
2681 num_bytes = (start + num_bytes) -
2682 caching_ctl->progress;
2683 start = caching_ctl->progress;
2684 ret = btrfs_add_excluded_extent(fs_info, start,
2688 mutex_unlock(&caching_ctl->mutex);
2689 btrfs_put_caching_control(caching_ctl);
2691 btrfs_put_block_group(block_group);
2695 int btrfs_exclude_logged_extents(struct extent_buffer *eb)
2697 struct btrfs_fs_info *fs_info = eb->fs_info;
2698 struct btrfs_file_extent_item *item;
2699 struct btrfs_key key;
2704 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS))
2707 for (i = 0; i < btrfs_header_nritems(eb); i++) {
2708 btrfs_item_key_to_cpu(eb, &key, i);
2709 if (key.type != BTRFS_EXTENT_DATA_KEY)
2711 item = btrfs_item_ptr(eb, i, struct btrfs_file_extent_item);
2712 found_type = btrfs_file_extent_type(eb, item);
2713 if (found_type == BTRFS_FILE_EXTENT_INLINE)
2715 if (btrfs_file_extent_disk_bytenr(eb, item) == 0)
2717 key.objectid = btrfs_file_extent_disk_bytenr(eb, item);
2718 key.offset = btrfs_file_extent_disk_num_bytes(eb, item);
2719 ret = __exclude_logged_extent(fs_info, key.objectid, key.offset);
2728 btrfs_inc_block_group_reservations(struct btrfs_block_group *bg)
2730 atomic_inc(&bg->reservations);
2733 void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info)
2735 struct btrfs_caching_control *next;
2736 struct btrfs_caching_control *caching_ctl;
2737 struct btrfs_block_group *cache;
2739 down_write(&fs_info->commit_root_sem);
2741 list_for_each_entry_safe(caching_ctl, next,
2742 &fs_info->caching_block_groups, list) {
2743 cache = caching_ctl->block_group;
2744 if (btrfs_block_group_done(cache)) {
2745 cache->last_byte_to_unpin = (u64)-1;
2746 list_del_init(&caching_ctl->list);
2747 btrfs_put_caching_control(caching_ctl);
2749 cache->last_byte_to_unpin = caching_ctl->progress;
2753 up_write(&fs_info->commit_root_sem);
2755 btrfs_update_global_block_rsv(fs_info);
2759 * Returns the free cluster for the given space info and sets empty_cluster to
2760 * what it should be based on the mount options.
2762 static struct btrfs_free_cluster *
2763 fetch_cluster_info(struct btrfs_fs_info *fs_info,
2764 struct btrfs_space_info *space_info, u64 *empty_cluster)
2766 struct btrfs_free_cluster *ret = NULL;
2769 if (btrfs_mixed_space_info(space_info))
2772 if (space_info->flags & BTRFS_BLOCK_GROUP_METADATA) {
2773 ret = &fs_info->meta_alloc_cluster;
2774 if (btrfs_test_opt(fs_info, SSD))
2775 *empty_cluster = SZ_2M;
2777 *empty_cluster = SZ_64K;
2778 } else if ((space_info->flags & BTRFS_BLOCK_GROUP_DATA) &&
2779 btrfs_test_opt(fs_info, SSD_SPREAD)) {
2780 *empty_cluster = SZ_2M;
2781 ret = &fs_info->data_alloc_cluster;
2787 static int unpin_extent_range(struct btrfs_fs_info *fs_info,
2789 const bool return_free_space)
2791 struct btrfs_block_group *cache = NULL;
2792 struct btrfs_space_info *space_info;
2793 struct btrfs_block_rsv *global_rsv = &fs_info->global_block_rsv;
2794 struct btrfs_free_cluster *cluster = NULL;
2796 u64 total_unpinned = 0;
2797 u64 empty_cluster = 0;
2800 while (start <= end) {
2803 start >= cache->start + cache->length) {
2805 btrfs_put_block_group(cache);
2807 cache = btrfs_lookup_block_group(fs_info, start);
2808 BUG_ON(!cache); /* Logic error */
2810 cluster = fetch_cluster_info(fs_info,
2813 empty_cluster <<= 1;
2816 len = cache->start + cache->length - start;
2817 len = min(len, end + 1 - start);
2819 if (start < cache->last_byte_to_unpin) {
2820 len = min(len, cache->last_byte_to_unpin - start);
2821 if (return_free_space)
2822 btrfs_add_free_space(cache, start, len);
2826 total_unpinned += len;
2827 space_info = cache->space_info;
2830 * If this space cluster has been marked as fragmented and we've
2831 * unpinned enough in this block group to potentially allow a
2832 * cluster to be created inside of it go ahead and clear the
2835 if (cluster && cluster->fragmented &&
2836 total_unpinned > empty_cluster) {
2837 spin_lock(&cluster->lock);
2838 cluster->fragmented = 0;
2839 spin_unlock(&cluster->lock);
2842 spin_lock(&space_info->lock);
2843 spin_lock(&cache->lock);
2844 cache->pinned -= len;
2845 btrfs_space_info_update_bytes_pinned(fs_info, space_info, -len);
2846 space_info->max_extent_size = 0;
2847 percpu_counter_add_batch(&space_info->total_bytes_pinned,
2848 -len, BTRFS_TOTAL_BYTES_PINNED_BATCH);
2850 space_info->bytes_readonly += len;
2853 spin_unlock(&cache->lock);
2854 if (!readonly && return_free_space &&
2855 global_rsv->space_info == space_info) {
2858 spin_lock(&global_rsv->lock);
2859 if (!global_rsv->full) {
2860 to_add = min(len, global_rsv->size -
2861 global_rsv->reserved);
2862 global_rsv->reserved += to_add;
2863 btrfs_space_info_update_bytes_may_use(fs_info,
2864 space_info, to_add);
2865 if (global_rsv->reserved >= global_rsv->size)
2866 global_rsv->full = 1;
2869 spin_unlock(&global_rsv->lock);
2871 /* Add to any tickets we may have */
2872 if (!readonly && return_free_space && len)
2873 btrfs_try_granting_tickets(fs_info, space_info);
2874 spin_unlock(&space_info->lock);
2878 btrfs_put_block_group(cache);
2882 int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans)
2884 struct btrfs_fs_info *fs_info = trans->fs_info;
2885 struct btrfs_block_group *block_group, *tmp;
2886 struct list_head *deleted_bgs;
2887 struct extent_io_tree *unpin;
2892 unpin = &trans->transaction->pinned_extents;
2894 while (!TRANS_ABORTED(trans)) {
2895 struct extent_state *cached_state = NULL;
2897 mutex_lock(&fs_info->unused_bg_unpin_mutex);
2898 ret = find_first_extent_bit(unpin, 0, &start, &end,
2899 EXTENT_DIRTY, &cached_state);
2901 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
2904 if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags))
2905 clear_extent_bits(&fs_info->excluded_extents, start,
2906 end, EXTENT_UPTODATE);
2908 if (btrfs_test_opt(fs_info, DISCARD_SYNC))
2909 ret = btrfs_discard_extent(fs_info, start,
2910 end + 1 - start, NULL);
2912 clear_extent_dirty(unpin, start, end, &cached_state);
2913 unpin_extent_range(fs_info, start, end, true);
2914 mutex_unlock(&fs_info->unused_bg_unpin_mutex);
2915 free_extent_state(cached_state);
2919 if (btrfs_test_opt(fs_info, DISCARD_ASYNC)) {
2920 btrfs_discard_calc_delay(&fs_info->discard_ctl);
2921 btrfs_discard_schedule_work(&fs_info->discard_ctl, true);
2925 * Transaction is finished. We don't need the lock anymore. We
2926 * do need to clean up the block groups in case of a transaction
2929 deleted_bgs = &trans->transaction->deleted_bgs;
2930 list_for_each_entry_safe(block_group, tmp, deleted_bgs, bg_list) {
2934 if (!TRANS_ABORTED(trans))
2935 ret = btrfs_discard_extent(fs_info,
2937 block_group->length,
2940 list_del_init(&block_group->bg_list);
2941 btrfs_unfreeze_block_group(block_group);
2942 btrfs_put_block_group(block_group);
2945 const char *errstr = btrfs_decode_error(ret);
2947 "discard failed while removing blockgroup: errno=%d %s",
2956 * Drop one or more refs of @node.
2958 * 1. Locate the extent refs.
2959 * It's either inline in EXTENT/METADATA_ITEM or in keyed SHARED_* item.
2960 * Locate it, then reduce the refs number or remove the ref line completely.
2962 * 2. Update the refs count in EXTENT/METADATA_ITEM
2964 * Inline backref case:
2966 * in extent tree we have:
2968 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
2969 * refs 2 gen 6 flags DATA
2970 * extent data backref root FS_TREE objectid 258 offset 0 count 1
2971 * extent data backref root FS_TREE objectid 257 offset 0 count 1
2973 * This function gets called with:
2975 * node->bytenr = 13631488
2976 * node->num_bytes = 1048576
2977 * root_objectid = FS_TREE
2978 * owner_objectid = 257
2982 * Then we should get some like:
2984 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 16201 itemsize 82
2985 * refs 1 gen 6 flags DATA
2986 * extent data backref root FS_TREE objectid 258 offset 0 count 1
2988 * Keyed backref case:
2990 * in extent tree we have:
2992 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
2993 * refs 754 gen 6 flags DATA
2995 * item 2 key (13631488 EXTENT_DATA_REF <HASH>) itemoff 3915 itemsize 28
2996 * extent data backref root FS_TREE objectid 866 offset 0 count 1
2998 * This function get called with:
3000 * node->bytenr = 13631488
3001 * node->num_bytes = 1048576
3002 * root_objectid = FS_TREE
3003 * owner_objectid = 866
3007 * Then we should get some like:
3009 * item 0 key (13631488 EXTENT_ITEM 1048576) itemoff 3971 itemsize 24
3010 * refs 753 gen 6 flags DATA
3012 * And that (13631488 EXTENT_DATA_REF <HASH>) gets removed.
3014 static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
3015 struct btrfs_delayed_ref_node *node, u64 parent,
3016 u64 root_objectid, u64 owner_objectid,
3017 u64 owner_offset, int refs_to_drop,
3018 struct btrfs_delayed_extent_op *extent_op)
3020 struct btrfs_fs_info *info = trans->fs_info;
3021 struct btrfs_key key;
3022 struct btrfs_path *path;
3023 struct btrfs_root *extent_root = info->extent_root;
3024 struct extent_buffer *leaf;
3025 struct btrfs_extent_item *ei;
3026 struct btrfs_extent_inline_ref *iref;
3029 int extent_slot = 0;
3030 int found_extent = 0;
3034 u64 bytenr = node->bytenr;
3035 u64 num_bytes = node->num_bytes;
3037 bool skinny_metadata = btrfs_fs_incompat(info, SKINNY_METADATA);
3039 path = btrfs_alloc_path();
3043 path->leave_spinning = 1;
3045 is_data = owner_objectid >= BTRFS_FIRST_FREE_OBJECTID;
3047 if (!is_data && refs_to_drop != 1) {
3049 "invalid refs_to_drop, dropping more than 1 refs for tree block %llu refs_to_drop %u",
3050 node->bytenr, refs_to_drop);
3052 btrfs_abort_transaction(trans, ret);
3057 skinny_metadata = false;
3059 ret = lookup_extent_backref(trans, path, &iref, bytenr, num_bytes,
3060 parent, root_objectid, owner_objectid,
3064 * Either the inline backref or the SHARED_DATA_REF/
3065 * SHARED_BLOCK_REF is found
3067 * Here is a quick path to locate EXTENT/METADATA_ITEM.
3068 * It's possible the EXTENT/METADATA_ITEM is near current slot.
3070 extent_slot = path->slots[0];
3071 while (extent_slot >= 0) {
3072 btrfs_item_key_to_cpu(path->nodes[0], &key,
3074 if (key.objectid != bytenr)
3076 if (key.type == BTRFS_EXTENT_ITEM_KEY &&
3077 key.offset == num_bytes) {
3081 if (key.type == BTRFS_METADATA_ITEM_KEY &&
3082 key.offset == owner_objectid) {
3087 /* Quick path didn't find the EXTEMT/METADATA_ITEM */
3088 if (path->slots[0] - extent_slot > 5)
3093 if (!found_extent) {
3096 "invalid iref, no EXTENT/METADATA_ITEM found but has inline extent ref");
3097 btrfs_abort_transaction(trans, -EUCLEAN);
3100 /* Must be SHARED_* item, remove the backref first */
3101 ret = remove_extent_backref(trans, path, NULL,
3103 is_data, &last_ref);
3105 btrfs_abort_transaction(trans, ret);
3108 btrfs_release_path(path);
3109 path->leave_spinning = 1;
3111 /* Slow path to locate EXTENT/METADATA_ITEM */
3112 key.objectid = bytenr;
3113 key.type = BTRFS_EXTENT_ITEM_KEY;
3114 key.offset = num_bytes;
3116 if (!is_data && skinny_metadata) {
3117 key.type = BTRFS_METADATA_ITEM_KEY;
3118 key.offset = owner_objectid;
3121 ret = btrfs_search_slot(trans, extent_root,
3123 if (ret > 0 && skinny_metadata && path->slots[0]) {
3125 * Couldn't find our skinny metadata item,
3126 * see if we have ye olde extent item.
3129 btrfs_item_key_to_cpu(path->nodes[0], &key,
3131 if (key.objectid == bytenr &&
3132 key.type == BTRFS_EXTENT_ITEM_KEY &&
3133 key.offset == num_bytes)
3137 if (ret > 0 && skinny_metadata) {
3138 skinny_metadata = false;
3139 key.objectid = bytenr;
3140 key.type = BTRFS_EXTENT_ITEM_KEY;
3141 key.offset = num_bytes;
3142 btrfs_release_path(path);
3143 ret = btrfs_search_slot(trans, extent_root,
3149 "umm, got %d back from search, was looking for %llu",
3152 btrfs_print_leaf(path->nodes[0]);
3155 btrfs_abort_transaction(trans, ret);
3158 extent_slot = path->slots[0];
3160 } else if (WARN_ON(ret == -ENOENT)) {
3161 btrfs_print_leaf(path->nodes[0]);
3163 "unable to find ref byte nr %llu parent %llu root %llu owner %llu offset %llu",
3164 bytenr, parent, root_objectid, owner_objectid,
3166 btrfs_abort_transaction(trans, ret);
3169 btrfs_abort_transaction(trans, ret);
3173 leaf = path->nodes[0];
3174 item_size = btrfs_item_size_nr(leaf, extent_slot);
3175 if (unlikely(item_size < sizeof(*ei))) {
3177 btrfs_print_v0_err(info);
3178 btrfs_abort_transaction(trans, ret);
3181 ei = btrfs_item_ptr(leaf, extent_slot,
3182 struct btrfs_extent_item);
3183 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID &&
3184 key.type == BTRFS_EXTENT_ITEM_KEY) {
3185 struct btrfs_tree_block_info *bi;
3186 if (item_size < sizeof(*ei) + sizeof(*bi)) {
3188 "invalid extent item size for key (%llu, %u, %llu) owner %llu, has %u expect >= %lu",
3189 key.objectid, key.type, key.offset,
3190 owner_objectid, item_size,
3191 sizeof(*ei) + sizeof(*bi));
3192 btrfs_abort_transaction(trans, -EUCLEAN);
3195 bi = (struct btrfs_tree_block_info *)(ei + 1);
3196 WARN_ON(owner_objectid != btrfs_tree_block_level(leaf, bi));
3199 refs = btrfs_extent_refs(leaf, ei);
3200 if (refs < refs_to_drop) {
3202 "trying to drop %d refs but we only have %llu for bytenr %llu",
3203 refs_to_drop, refs, bytenr);
3204 btrfs_abort_transaction(trans, -EUCLEAN);
3207 refs -= refs_to_drop;
3211 __run_delayed_extent_op(extent_op, leaf, ei);
3213 * In the case of inline back ref, reference count will
3214 * be updated by remove_extent_backref
3217 if (!found_extent) {
3219 "invalid iref, got inlined extent ref but no EXTENT/METADATA_ITEM found");
3220 btrfs_abort_transaction(trans, -EUCLEAN);
3224 btrfs_set_extent_refs(leaf, ei, refs);
3225 btrfs_mark_buffer_dirty(leaf);
3228 ret = remove_extent_backref(trans, path, iref,
3229 refs_to_drop, is_data,
3232 btrfs_abort_transaction(trans, ret);
3237 /* In this branch refs == 1 */
3239 if (is_data && refs_to_drop !=
3240 extent_data_ref_count(path, iref)) {
3242 "invalid refs_to_drop, current refs %u refs_to_drop %u",
3243 extent_data_ref_count(path, iref),
3245 btrfs_abort_transaction(trans, -EUCLEAN);
3249 if (path->slots[0] != extent_slot) {
3251 "invalid iref, extent item key (%llu %u %llu) doesn't have wanted iref",
3252 key.objectid, key.type,
3254 btrfs_abort_transaction(trans, -EUCLEAN);
3259 * No inline ref, we must be at SHARED_* item,
3260 * And it's single ref, it must be:
3261 * | extent_slot ||extent_slot + 1|
3262 * [ EXTENT/METADATA_ITEM ][ SHARED_* ITEM ]
3264 if (path->slots[0] != extent_slot + 1) {
3266 "invalid SHARED_* item, previous item is not EXTENT/METADATA_ITEM");
3267 btrfs_abort_transaction(trans, -EUCLEAN);
3270 path->slots[0] = extent_slot;
3276 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
3279 btrfs_abort_transaction(trans, ret);
3282 btrfs_release_path(path);
3285 ret = btrfs_del_csums(trans, info->csum_root, bytenr,
3288 btrfs_abort_transaction(trans, ret);
3293 ret = add_to_free_space_tree(trans, bytenr, num_bytes);
3295 btrfs_abort_transaction(trans, ret);
3299 ret = btrfs_update_block_group(trans, bytenr, num_bytes, 0);
3301 btrfs_abort_transaction(trans, ret);
3305 btrfs_release_path(path);
3308 btrfs_free_path(path);
3312 * Leaf dump can take up a lot of log buffer, so we only do full leaf
3313 * dump for debug build.
3315 if (IS_ENABLED(CONFIG_BTRFS_DEBUG)) {
3316 btrfs_crit(info, "path->slots[0]=%d extent_slot=%d",
3317 path->slots[0], extent_slot);
3318 btrfs_print_leaf(path->nodes[0]);
3321 btrfs_free_path(path);
3326 * when we free an block, it is possible (and likely) that we free the last
3327 * delayed ref for that extent as well. This searches the delayed ref tree for
3328 * a given extent, and if there are no other delayed refs to be processed, it
3329 * removes it from the tree.
3331 static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
3334 struct btrfs_delayed_ref_head *head;
3335 struct btrfs_delayed_ref_root *delayed_refs;
3338 delayed_refs = &trans->transaction->delayed_refs;
3339 spin_lock(&delayed_refs->lock);
3340 head = btrfs_find_delayed_ref_head(delayed_refs, bytenr);
3342 goto out_delayed_unlock;
3344 spin_lock(&head->lock);
3345 if (!RB_EMPTY_ROOT(&head->ref_tree.rb_root))
3348 if (cleanup_extent_op(head) != NULL)
3352 * waiting for the lock here would deadlock. If someone else has it
3353 * locked they are already in the process of dropping it anyway
3355 if (!mutex_trylock(&head->mutex))
3358 btrfs_delete_ref_head(delayed_refs, head);
3359 head->processing = 0;
3361 spin_unlock(&head->lock);
3362 spin_unlock(&delayed_refs->lock);
3364 BUG_ON(head->extent_op);
3365 if (head->must_insert_reserved)
3368 btrfs_cleanup_ref_head_accounting(trans->fs_info, delayed_refs, head);
3369 mutex_unlock(&head->mutex);
3370 btrfs_put_delayed_ref_head(head);
3373 spin_unlock(&head->lock);
3376 spin_unlock(&delayed_refs->lock);
3380 void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
3381 struct btrfs_root *root,
3382 struct extent_buffer *buf,
3383 u64 parent, int last_ref)
3385 struct btrfs_fs_info *fs_info = root->fs_info;
3386 struct btrfs_ref generic_ref = { 0 };
3390 btrfs_init_generic_ref(&generic_ref, BTRFS_DROP_DELAYED_REF,
3391 buf->start, buf->len, parent);
3392 btrfs_init_tree_ref(&generic_ref, btrfs_header_level(buf),
3393 root->root_key.objectid);
3395 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
3396 int old_ref_mod, new_ref_mod;
3398 btrfs_ref_tree_mod(fs_info, &generic_ref);
3399 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref, NULL,
3400 &old_ref_mod, &new_ref_mod);
3401 BUG_ON(ret); /* -ENOMEM */
3402 pin = old_ref_mod >= 0 && new_ref_mod < 0;
3405 if (last_ref && btrfs_header_generation(buf) == trans->transid) {
3406 struct btrfs_block_group *cache;
3408 if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
3409 ret = check_ref_cleanup(trans, buf->start);
3415 cache = btrfs_lookup_block_group(fs_info, buf->start);
3417 if (btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
3418 pin_down_extent(trans, cache, buf->start, buf->len, 1);
3419 btrfs_put_block_group(cache);
3423 WARN_ON(test_bit(EXTENT_BUFFER_DIRTY, &buf->bflags));
3425 btrfs_add_free_space(cache, buf->start, buf->len);
3426 btrfs_free_reserved_bytes(cache, buf->len, 0);
3427 btrfs_put_block_group(cache);
3428 trace_btrfs_reserved_extent_free(fs_info, buf->start, buf->len);
3432 add_pinned_bytes(fs_info, &generic_ref);
3436 * Deleting the buffer, clear the corrupt flag since it doesn't
3439 clear_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags);
3443 /* Can return -ENOMEM */
3444 int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref)
3446 struct btrfs_fs_info *fs_info = trans->fs_info;
3447 int old_ref_mod, new_ref_mod;
3450 if (btrfs_is_testing(fs_info))
3454 * tree log blocks never actually go into the extent allocation
3455 * tree, just update pinning info and exit early.
3457 if ((ref->type == BTRFS_REF_METADATA &&
3458 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3459 (ref->type == BTRFS_REF_DATA &&
3460 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)) {
3461 /* unlocks the pinned mutex */
3462 btrfs_pin_extent(trans, ref->bytenr, ref->len, 1);
3463 old_ref_mod = new_ref_mod = 0;
3465 } else if (ref->type == BTRFS_REF_METADATA) {
3466 ret = btrfs_add_delayed_tree_ref(trans, ref, NULL,
3467 &old_ref_mod, &new_ref_mod);
3469 ret = btrfs_add_delayed_data_ref(trans, ref, 0,
3470 &old_ref_mod, &new_ref_mod);
3473 if (!((ref->type == BTRFS_REF_METADATA &&
3474 ref->tree_ref.root == BTRFS_TREE_LOG_OBJECTID) ||
3475 (ref->type == BTRFS_REF_DATA &&
3476 ref->data_ref.ref_root == BTRFS_TREE_LOG_OBJECTID)))
3477 btrfs_ref_tree_mod(fs_info, ref);
3479 if (ret == 0 && old_ref_mod >= 0 && new_ref_mod < 0)
3480 add_pinned_bytes(fs_info, ref);
3485 enum btrfs_loop_type {
3486 LOOP_CACHING_NOWAIT,
3493 btrfs_lock_block_group(struct btrfs_block_group *cache,
3497 down_read(&cache->data_rwsem);
3500 static inline void btrfs_grab_block_group(struct btrfs_block_group *cache,
3503 btrfs_get_block_group(cache);
3505 down_read(&cache->data_rwsem);
3508 static struct btrfs_block_group *btrfs_lock_cluster(
3509 struct btrfs_block_group *block_group,
3510 struct btrfs_free_cluster *cluster,
3512 __acquires(&cluster->refill_lock)
3514 struct btrfs_block_group *used_bg = NULL;
3516 spin_lock(&cluster->refill_lock);
3518 used_bg = cluster->block_group;
3522 if (used_bg == block_group)
3525 btrfs_get_block_group(used_bg);
3530 if (down_read_trylock(&used_bg->data_rwsem))
3533 spin_unlock(&cluster->refill_lock);
3535 /* We should only have one-level nested. */
3536 down_read_nested(&used_bg->data_rwsem, SINGLE_DEPTH_NESTING);
3538 spin_lock(&cluster->refill_lock);
3539 if (used_bg == cluster->block_group)
3542 up_read(&used_bg->data_rwsem);
3543 btrfs_put_block_group(used_bg);
3548 btrfs_release_block_group(struct btrfs_block_group *cache,
3552 up_read(&cache->data_rwsem);
3553 btrfs_put_block_group(cache);
3556 enum btrfs_extent_allocation_policy {
3557 BTRFS_EXTENT_ALLOC_CLUSTERED,
3561 * Structure used internally for find_free_extent() function. Wraps needed
3564 struct find_free_extent_ctl {
3565 /* Basic allocation info */
3571 /* Where to start the search inside the bg */
3574 /* For clustered allocation */
3576 struct btrfs_free_cluster *last_ptr;
3579 bool have_caching_bg;
3580 bool orig_have_caching_bg;
3582 /* RAID index, converted from flags */
3586 * Current loop number, check find_free_extent_update_loop() for details
3591 * Whether we're refilling a cluster, if true we need to re-search
3592 * current block group but don't try to refill the cluster again.
3594 bool retry_clustered;
3597 * Whether we're updating free space cache, if true we need to re-search
3598 * current block group but don't try updating free space cache again.
3600 bool retry_unclustered;
3602 /* If current block group is cached */
3605 /* Max contiguous hole found */
3606 u64 max_extent_size;
3608 /* Total free space from free space cache, not always contiguous */
3609 u64 total_free_space;
3614 /* Hint where to start looking for an empty space */
3617 /* Allocation policy */
3618 enum btrfs_extent_allocation_policy policy;
3623 * Helper function for find_free_extent().
3625 * Return -ENOENT to inform caller that we need fallback to unclustered mode.
3626 * Return -EAGAIN to inform caller that we need to re-search this block group
3627 * Return >0 to inform caller that we find nothing
3628 * Return 0 means we have found a location and set ffe_ctl->found_offset.
3630 static int find_free_extent_clustered(struct btrfs_block_group *bg,
3631 struct find_free_extent_ctl *ffe_ctl,
3632 struct btrfs_block_group **cluster_bg_ret)
3634 struct btrfs_block_group *cluster_bg;
3635 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3636 u64 aligned_cluster;
3640 cluster_bg = btrfs_lock_cluster(bg, last_ptr, ffe_ctl->delalloc);
3642 goto refill_cluster;
3643 if (cluster_bg != bg && (cluster_bg->ro ||
3644 !block_group_bits(cluster_bg, ffe_ctl->flags)))
3645 goto release_cluster;
3647 offset = btrfs_alloc_from_cluster(cluster_bg, last_ptr,
3648 ffe_ctl->num_bytes, cluster_bg->start,
3649 &ffe_ctl->max_extent_size);
3651 /* We have a block, we're done */
3652 spin_unlock(&last_ptr->refill_lock);
3653 trace_btrfs_reserve_extent_cluster(cluster_bg,
3654 ffe_ctl->search_start, ffe_ctl->num_bytes);
3655 *cluster_bg_ret = cluster_bg;
3656 ffe_ctl->found_offset = offset;
3659 WARN_ON(last_ptr->block_group != cluster_bg);
3663 * If we are on LOOP_NO_EMPTY_SIZE, we can't set up a new clusters, so
3664 * lets just skip it and let the allocator find whatever block it can
3665 * find. If we reach this point, we will have tried the cluster
3666 * allocator plenty of times and not have found anything, so we are
3667 * likely way too fragmented for the clustering stuff to find anything.
3669 * However, if the cluster is taken from the current block group,
3670 * release the cluster first, so that we stand a better chance of
3671 * succeeding in the unclustered allocation.
3673 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE && cluster_bg != bg) {
3674 spin_unlock(&last_ptr->refill_lock);
3675 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3679 /* This cluster didn't work out, free it and start over */
3680 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3682 if (cluster_bg != bg)
3683 btrfs_release_block_group(cluster_bg, ffe_ctl->delalloc);
3686 if (ffe_ctl->loop >= LOOP_NO_EMPTY_SIZE) {
3687 spin_unlock(&last_ptr->refill_lock);
3691 aligned_cluster = max_t(u64,
3692 ffe_ctl->empty_cluster + ffe_ctl->empty_size,
3693 bg->full_stripe_len);
3694 ret = btrfs_find_space_cluster(bg, last_ptr, ffe_ctl->search_start,
3695 ffe_ctl->num_bytes, aligned_cluster);
3697 /* Now pull our allocation out of this cluster */
3698 offset = btrfs_alloc_from_cluster(bg, last_ptr,
3699 ffe_ctl->num_bytes, ffe_ctl->search_start,
3700 &ffe_ctl->max_extent_size);
3702 /* We found one, proceed */
3703 spin_unlock(&last_ptr->refill_lock);
3704 trace_btrfs_reserve_extent_cluster(bg,
3705 ffe_ctl->search_start,
3706 ffe_ctl->num_bytes);
3707 ffe_ctl->found_offset = offset;
3710 } else if (!ffe_ctl->cached && ffe_ctl->loop > LOOP_CACHING_NOWAIT &&
3711 !ffe_ctl->retry_clustered) {
3712 spin_unlock(&last_ptr->refill_lock);
3714 ffe_ctl->retry_clustered = true;
3715 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3716 ffe_ctl->empty_cluster + ffe_ctl->empty_size);
3720 * At this point we either didn't find a cluster or we weren't able to
3721 * allocate a block from our cluster. Free the cluster we've been
3722 * trying to use, and go to the next block group.
3724 btrfs_return_cluster_to_free_space(NULL, last_ptr);
3725 spin_unlock(&last_ptr->refill_lock);
3730 * Return >0 to inform caller that we find nothing
3731 * Return 0 when we found an free extent and set ffe_ctrl->found_offset
3732 * Return -EAGAIN to inform caller that we need to re-search this block group
3734 static int find_free_extent_unclustered(struct btrfs_block_group *bg,
3735 struct find_free_extent_ctl *ffe_ctl)
3737 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3741 * We are doing an unclustered allocation, set the fragmented flag so
3742 * we don't bother trying to setup a cluster again until we get more
3745 if (unlikely(last_ptr)) {
3746 spin_lock(&last_ptr->lock);
3747 last_ptr->fragmented = 1;
3748 spin_unlock(&last_ptr->lock);
3750 if (ffe_ctl->cached) {
3751 struct btrfs_free_space_ctl *free_space_ctl;
3753 free_space_ctl = bg->free_space_ctl;
3754 spin_lock(&free_space_ctl->tree_lock);
3755 if (free_space_ctl->free_space <
3756 ffe_ctl->num_bytes + ffe_ctl->empty_cluster +
3757 ffe_ctl->empty_size) {
3758 ffe_ctl->total_free_space = max_t(u64,
3759 ffe_ctl->total_free_space,
3760 free_space_ctl->free_space);
3761 spin_unlock(&free_space_ctl->tree_lock);
3764 spin_unlock(&free_space_ctl->tree_lock);
3767 offset = btrfs_find_space_for_alloc(bg, ffe_ctl->search_start,
3768 ffe_ctl->num_bytes, ffe_ctl->empty_size,
3769 &ffe_ctl->max_extent_size);
3772 * If we didn't find a chunk, and we haven't failed on this block group
3773 * before, and this block group is in the middle of caching and we are
3774 * ok with waiting, then go ahead and wait for progress to be made, and
3775 * set @retry_unclustered to true.
3777 * If @retry_unclustered is true then we've already waited on this
3778 * block group once and should move on to the next block group.
3780 if (!offset && !ffe_ctl->retry_unclustered && !ffe_ctl->cached &&
3781 ffe_ctl->loop > LOOP_CACHING_NOWAIT) {
3782 btrfs_wait_block_group_cache_progress(bg, ffe_ctl->num_bytes +
3783 ffe_ctl->empty_size);
3784 ffe_ctl->retry_unclustered = true;
3786 } else if (!offset) {
3789 ffe_ctl->found_offset = offset;
3793 static int do_allocation_clustered(struct btrfs_block_group *block_group,
3794 struct find_free_extent_ctl *ffe_ctl,
3795 struct btrfs_block_group **bg_ret)
3799 /* We want to try and use the cluster allocator, so lets look there */
3800 if (ffe_ctl->last_ptr && ffe_ctl->use_cluster) {
3801 ret = find_free_extent_clustered(block_group, ffe_ctl, bg_ret);
3802 if (ret >= 0 || ret == -EAGAIN)
3804 /* ret == -ENOENT case falls through */
3807 return find_free_extent_unclustered(block_group, ffe_ctl);
3810 static int do_allocation(struct btrfs_block_group *block_group,
3811 struct find_free_extent_ctl *ffe_ctl,
3812 struct btrfs_block_group **bg_ret)
3814 switch (ffe_ctl->policy) {
3815 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3816 return do_allocation_clustered(block_group, ffe_ctl, bg_ret);
3822 static void release_block_group(struct btrfs_block_group *block_group,
3823 struct find_free_extent_ctl *ffe_ctl,
3826 switch (ffe_ctl->policy) {
3827 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3828 ffe_ctl->retry_clustered = false;
3829 ffe_ctl->retry_unclustered = false;
3835 BUG_ON(btrfs_bg_flags_to_raid_index(block_group->flags) !=
3837 btrfs_release_block_group(block_group, delalloc);
3840 static void found_extent_clustered(struct find_free_extent_ctl *ffe_ctl,
3841 struct btrfs_key *ins)
3843 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
3845 if (!ffe_ctl->use_cluster && last_ptr) {
3846 spin_lock(&last_ptr->lock);
3847 last_ptr->window_start = ins->objectid;
3848 spin_unlock(&last_ptr->lock);
3852 static void found_extent(struct find_free_extent_ctl *ffe_ctl,
3853 struct btrfs_key *ins)
3855 switch (ffe_ctl->policy) {
3856 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3857 found_extent_clustered(ffe_ctl, ins);
3864 static int chunk_allocation_failed(struct find_free_extent_ctl *ffe_ctl)
3866 switch (ffe_ctl->policy) {
3867 case BTRFS_EXTENT_ALLOC_CLUSTERED:
3869 * If we can't allocate a new chunk we've already looped through
3870 * at least once, move on to the NO_EMPTY_SIZE case.
3872 ffe_ctl->loop = LOOP_NO_EMPTY_SIZE;
3880 * Return >0 means caller needs to re-search for free extent
3881 * Return 0 means we have the needed free extent.
3882 * Return <0 means we failed to locate any free extent.
3884 static int find_free_extent_update_loop(struct btrfs_fs_info *fs_info,
3885 struct btrfs_key *ins,
3886 struct find_free_extent_ctl *ffe_ctl,
3889 struct btrfs_root *root = fs_info->extent_root;
3892 if ((ffe_ctl->loop == LOOP_CACHING_NOWAIT) &&
3893 ffe_ctl->have_caching_bg && !ffe_ctl->orig_have_caching_bg)
3894 ffe_ctl->orig_have_caching_bg = true;
3896 if (!ins->objectid && ffe_ctl->loop >= LOOP_CACHING_WAIT &&
3897 ffe_ctl->have_caching_bg)
3900 if (!ins->objectid && ++(ffe_ctl->index) < BTRFS_NR_RAID_TYPES)
3903 if (ins->objectid) {
3904 found_extent(ffe_ctl, ins);
3909 * LOOP_CACHING_NOWAIT, search partially cached block groups, kicking
3910 * caching kthreads as we move along
3911 * LOOP_CACHING_WAIT, search everything, and wait if our bg is caching
3912 * LOOP_ALLOC_CHUNK, force a chunk allocation and try again
3913 * LOOP_NO_EMPTY_SIZE, set empty_size and empty_cluster to 0 and try
3916 if (ffe_ctl->loop < LOOP_NO_EMPTY_SIZE) {
3918 if (ffe_ctl->loop == LOOP_CACHING_NOWAIT) {
3920 * We want to skip the LOOP_CACHING_WAIT step if we
3921 * don't have any uncached bgs and we've already done a
3922 * full search through.
3924 if (ffe_ctl->orig_have_caching_bg || !full_search)
3925 ffe_ctl->loop = LOOP_CACHING_WAIT;
3927 ffe_ctl->loop = LOOP_ALLOC_CHUNK;
3932 if (ffe_ctl->loop == LOOP_ALLOC_CHUNK) {
3933 struct btrfs_trans_handle *trans;
3936 trans = current->journal_info;
3940 trans = btrfs_join_transaction(root);
3942 if (IS_ERR(trans)) {
3943 ret = PTR_ERR(trans);
3947 ret = btrfs_chunk_alloc(trans, ffe_ctl->flags,
3950 /* Do not bail out on ENOSPC since we can do more. */
3952 ret = chunk_allocation_failed(ffe_ctl);
3954 btrfs_abort_transaction(trans, ret);
3958 btrfs_end_transaction(trans);
3963 if (ffe_ctl->loop == LOOP_NO_EMPTY_SIZE) {
3964 if (ffe_ctl->policy != BTRFS_EXTENT_ALLOC_CLUSTERED)
3968 * Don't loop again if we already have no empty_size and
3971 if (ffe_ctl->empty_size == 0 &&
3972 ffe_ctl->empty_cluster == 0)
3974 ffe_ctl->empty_size = 0;
3975 ffe_ctl->empty_cluster = 0;
3982 static int prepare_allocation_clustered(struct btrfs_fs_info *fs_info,
3983 struct find_free_extent_ctl *ffe_ctl,
3984 struct btrfs_space_info *space_info,
3985 struct btrfs_key *ins)
3988 * If our free space is heavily fragmented we may not be able to make
3989 * big contiguous allocations, so instead of doing the expensive search
3990 * for free space, simply return ENOSPC with our max_extent_size so we
3991 * can go ahead and search for a more manageable chunk.
3993 * If our max_extent_size is large enough for our allocation simply
3994 * disable clustering since we will likely not be able to find enough
3995 * space to create a cluster and induce latency trying.
3997 if (space_info->max_extent_size) {
3998 spin_lock(&space_info->lock);
3999 if (space_info->max_extent_size &&
4000 ffe_ctl->num_bytes > space_info->max_extent_size) {
4001 ins->offset = space_info->max_extent_size;
4002 spin_unlock(&space_info->lock);
4004 } else if (space_info->max_extent_size) {
4005 ffe_ctl->use_cluster = false;
4007 spin_unlock(&space_info->lock);
4010 ffe_ctl->last_ptr = fetch_cluster_info(fs_info, space_info,
4011 &ffe_ctl->empty_cluster);
4012 if (ffe_ctl->last_ptr) {
4013 struct btrfs_free_cluster *last_ptr = ffe_ctl->last_ptr;
4015 spin_lock(&last_ptr->lock);
4016 if (last_ptr->block_group)
4017 ffe_ctl->hint_byte = last_ptr->window_start;
4018 if (last_ptr->fragmented) {
4020 * We still set window_start so we can keep track of the
4021 * last place we found an allocation to try and save
4024 ffe_ctl->hint_byte = last_ptr->window_start;
4025 ffe_ctl->use_cluster = false;
4027 spin_unlock(&last_ptr->lock);
4033 static int prepare_allocation(struct btrfs_fs_info *fs_info,
4034 struct find_free_extent_ctl *ffe_ctl,
4035 struct btrfs_space_info *space_info,
4036 struct btrfs_key *ins)
4038 switch (ffe_ctl->policy) {
4039 case BTRFS_EXTENT_ALLOC_CLUSTERED:
4040 return prepare_allocation_clustered(fs_info, ffe_ctl,
4048 * walks the btree of allocated extents and find a hole of a given size.
4049 * The key ins is changed to record the hole:
4050 * ins->objectid == start position
4051 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4052 * ins->offset == the size of the hole.
4053 * Any available blocks before search_start are skipped.
4055 * If there is no suitable free space, we will record the max size of
4056 * the free space extent currently.
4058 * The overall logic and call chain:
4060 * find_free_extent()
4061 * |- Iterate through all block groups
4062 * | |- Get a valid block group
4063 * | |- Try to do clustered allocation in that block group
4064 * | |- Try to do unclustered allocation in that block group
4065 * | |- Check if the result is valid
4066 * | | |- If valid, then exit
4067 * | |- Jump to next block group
4069 * |- Push harder to find free extents
4070 * |- If not found, re-iterate all block groups
4072 static noinline int find_free_extent(struct btrfs_root *root,
4073 u64 ram_bytes, u64 num_bytes, u64 empty_size,
4074 u64 hint_byte_orig, struct btrfs_key *ins,
4075 u64 flags, int delalloc)
4077 struct btrfs_fs_info *fs_info = root->fs_info;
4079 int cache_block_group_error = 0;
4080 struct btrfs_block_group *block_group = NULL;
4081 struct find_free_extent_ctl ffe_ctl = {0};
4082 struct btrfs_space_info *space_info;
4083 bool full_search = false;
4085 WARN_ON(num_bytes < fs_info->sectorsize);
4087 ffe_ctl.num_bytes = num_bytes;
4088 ffe_ctl.empty_size = empty_size;
4089 ffe_ctl.flags = flags;
4090 ffe_ctl.search_start = 0;
4091 ffe_ctl.delalloc = delalloc;
4092 ffe_ctl.index = btrfs_bg_flags_to_raid_index(flags);
4093 ffe_ctl.have_caching_bg = false;
4094 ffe_ctl.orig_have_caching_bg = false;
4095 ffe_ctl.found_offset = 0;
4096 ffe_ctl.hint_byte = hint_byte_orig;
4097 ffe_ctl.policy = BTRFS_EXTENT_ALLOC_CLUSTERED;
4099 /* For clustered allocation */
4100 ffe_ctl.retry_clustered = false;
4101 ffe_ctl.retry_unclustered = false;
4102 ffe_ctl.last_ptr = NULL;
4103 ffe_ctl.use_cluster = true;
4105 ins->type = BTRFS_EXTENT_ITEM_KEY;
4109 trace_find_free_extent(root, num_bytes, empty_size, flags);
4111 space_info = btrfs_find_space_info(fs_info, flags);
4113 btrfs_err(fs_info, "No space info for %llu", flags);
4117 ret = prepare_allocation(fs_info, &ffe_ctl, space_info, ins);
4121 ffe_ctl.search_start = max(ffe_ctl.search_start,
4122 first_logical_byte(fs_info, 0));
4123 ffe_ctl.search_start = max(ffe_ctl.search_start, ffe_ctl.hint_byte);
4124 if (ffe_ctl.search_start == ffe_ctl.hint_byte) {
4125 block_group = btrfs_lookup_block_group(fs_info,
4126 ffe_ctl.search_start);
4128 * we don't want to use the block group if it doesn't match our
4129 * allocation bits, or if its not cached.
4131 * However if we are re-searching with an ideal block group
4132 * picked out then we don't care that the block group is cached.
4134 if (block_group && block_group_bits(block_group, flags) &&
4135 block_group->cached != BTRFS_CACHE_NO) {
4136 down_read(&space_info->groups_sem);
4137 if (list_empty(&block_group->list) ||
4140 * someone is removing this block group,
4141 * we can't jump into the have_block_group
4142 * target because our list pointers are not
4145 btrfs_put_block_group(block_group);
4146 up_read(&space_info->groups_sem);
4148 ffe_ctl.index = btrfs_bg_flags_to_raid_index(
4149 block_group->flags);
4150 btrfs_lock_block_group(block_group, delalloc);
4151 goto have_block_group;
4153 } else if (block_group) {
4154 btrfs_put_block_group(block_group);
4158 ffe_ctl.have_caching_bg = false;
4159 if (ffe_ctl.index == btrfs_bg_flags_to_raid_index(flags) ||
4162 down_read(&space_info->groups_sem);
4163 list_for_each_entry(block_group,
4164 &space_info->block_groups[ffe_ctl.index], list) {
4165 struct btrfs_block_group *bg_ret;
4167 /* If the block group is read-only, we can skip it entirely. */
4168 if (unlikely(block_group->ro))
4171 btrfs_grab_block_group(block_group, delalloc);
4172 ffe_ctl.search_start = block_group->start;
4175 * this can happen if we end up cycling through all the
4176 * raid types, but we want to make sure we only allocate
4177 * for the proper type.
4179 if (!block_group_bits(block_group, flags)) {
4180 u64 extra = BTRFS_BLOCK_GROUP_DUP |
4181 BTRFS_BLOCK_GROUP_RAID1_MASK |
4182 BTRFS_BLOCK_GROUP_RAID56_MASK |
4183 BTRFS_BLOCK_GROUP_RAID10;
4186 * if they asked for extra copies and this block group
4187 * doesn't provide them, bail. This does allow us to
4188 * fill raid0 from raid1.
4190 if ((flags & extra) && !(block_group->flags & extra))
4194 * This block group has different flags than we want.
4195 * It's possible that we have MIXED_GROUP flag but no
4196 * block group is mixed. Just skip such block group.
4198 btrfs_release_block_group(block_group, delalloc);
4203 ffe_ctl.cached = btrfs_block_group_done(block_group);
4204 if (unlikely(!ffe_ctl.cached)) {
4205 ffe_ctl.have_caching_bg = true;
4206 ret = btrfs_cache_block_group(block_group, 0);
4209 * If we get ENOMEM here or something else we want to
4210 * try other block groups, because it may not be fatal.
4211 * However if we can't find anything else we need to
4212 * save our return here so that we return the actual
4213 * error that caused problems, not ENOSPC.
4216 if (!cache_block_group_error)
4217 cache_block_group_error = ret;
4224 if (unlikely(block_group->cached == BTRFS_CACHE_ERROR))
4228 ret = do_allocation(block_group, &ffe_ctl, &bg_ret);
4230 if (bg_ret && bg_ret != block_group) {
4231 btrfs_release_block_group(block_group, delalloc);
4232 block_group = bg_ret;
4234 } else if (ret == -EAGAIN) {
4235 goto have_block_group;
4236 } else if (ret > 0) {
4241 ffe_ctl.search_start = round_up(ffe_ctl.found_offset,
4242 fs_info->stripesize);
4244 /* move on to the next group */
4245 if (ffe_ctl.search_start + num_bytes >
4246 block_group->start + block_group->length) {
4247 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4252 if (ffe_ctl.found_offset < ffe_ctl.search_start)
4253 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4254 ffe_ctl.search_start - ffe_ctl.found_offset);
4256 ret = btrfs_add_reserved_bytes(block_group, ram_bytes,
4257 num_bytes, delalloc);
4258 if (ret == -EAGAIN) {
4259 btrfs_add_free_space(block_group, ffe_ctl.found_offset,
4263 btrfs_inc_block_group_reservations(block_group);
4265 /* we are all good, lets return */
4266 ins->objectid = ffe_ctl.search_start;
4267 ins->offset = num_bytes;
4269 trace_btrfs_reserve_extent(block_group, ffe_ctl.search_start,
4271 btrfs_release_block_group(block_group, delalloc);
4274 release_block_group(block_group, &ffe_ctl, delalloc);
4277 up_read(&space_info->groups_sem);
4279 ret = find_free_extent_update_loop(fs_info, ins, &ffe_ctl, full_search);
4283 if (ret == -ENOSPC && !cache_block_group_error) {
4285 * Use ffe_ctl->total_free_space as fallback if we can't find
4286 * any contiguous hole.
4288 if (!ffe_ctl.max_extent_size)
4289 ffe_ctl.max_extent_size = ffe_ctl.total_free_space;
4290 spin_lock(&space_info->lock);
4291 space_info->max_extent_size = ffe_ctl.max_extent_size;
4292 spin_unlock(&space_info->lock);
4293 ins->offset = ffe_ctl.max_extent_size;
4294 } else if (ret == -ENOSPC) {
4295 ret = cache_block_group_error;
4301 * btrfs_reserve_extent - entry point to the extent allocator. Tries to find a
4302 * hole that is at least as big as @num_bytes.
4304 * @root - The root that will contain this extent
4306 * @ram_bytes - The amount of space in ram that @num_bytes take. This
4307 * is used for accounting purposes. This value differs
4308 * from @num_bytes only in the case of compressed extents.
4310 * @num_bytes - Number of bytes to allocate on-disk.
4312 * @min_alloc_size - Indicates the minimum amount of space that the
4313 * allocator should try to satisfy. In some cases
4314 * @num_bytes may be larger than what is required and if
4315 * the filesystem is fragmented then allocation fails.
4316 * However, the presence of @min_alloc_size gives a
4317 * chance to try and satisfy the smaller allocation.
4319 * @empty_size - A hint that you plan on doing more COW. This is the
4320 * size in bytes the allocator should try to find free
4321 * next to the block it returns. This is just a hint and
4322 * may be ignored by the allocator.
4324 * @hint_byte - Hint to the allocator to start searching above the byte
4325 * address passed. It might be ignored.
4327 * @ins - This key is modified to record the found hole. It will
4328 * have the following values:
4329 * ins->objectid == start position
4330 * ins->flags = BTRFS_EXTENT_ITEM_KEY
4331 * ins->offset == the size of the hole.
4333 * @is_data - Boolean flag indicating whether an extent is
4334 * allocated for data (true) or metadata (false)
4336 * @delalloc - Boolean flag indicating whether this allocation is for
4337 * delalloc or not. If 'true' data_rwsem of block groups
4338 * is going to be acquired.
4341 * Returns 0 when an allocation succeeded or < 0 when an error occurred. In
4342 * case -ENOSPC is returned then @ins->offset will contain the size of the
4343 * largest available hole the allocator managed to find.
4345 int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes,
4346 u64 num_bytes, u64 min_alloc_size,
4347 u64 empty_size, u64 hint_byte,
4348 struct btrfs_key *ins, int is_data, int delalloc)
4350 struct btrfs_fs_info *fs_info = root->fs_info;
4351 bool final_tried = num_bytes == min_alloc_size;
4355 flags = get_alloc_profile_by_root(root, is_data);
4357 WARN_ON(num_bytes < fs_info->sectorsize);
4358 ret = find_free_extent(root, ram_bytes, num_bytes, empty_size,
4359 hint_byte, ins, flags, delalloc);
4360 if (!ret && !is_data) {
4361 btrfs_dec_block_group_reservations(fs_info, ins->objectid);
4362 } else if (ret == -ENOSPC) {
4363 if (!final_tried && ins->offset) {
4364 num_bytes = min(num_bytes >> 1, ins->offset);
4365 num_bytes = round_down(num_bytes,
4366 fs_info->sectorsize);
4367 num_bytes = max(num_bytes, min_alloc_size);
4368 ram_bytes = num_bytes;
4369 if (num_bytes == min_alloc_size)
4372 } else if (btrfs_test_opt(fs_info, ENOSPC_DEBUG)) {
4373 struct btrfs_space_info *sinfo;
4375 sinfo = btrfs_find_space_info(fs_info, flags);
4377 "allocation failed flags %llu, wanted %llu",
4380 btrfs_dump_space_info(fs_info, sinfo,
4388 int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
4389 u64 start, u64 len, int delalloc)
4391 struct btrfs_block_group *cache;
4393 cache = btrfs_lookup_block_group(fs_info, start);
4395 btrfs_err(fs_info, "Unable to find block group for %llu",
4400 btrfs_add_free_space(cache, start, len);
4401 btrfs_free_reserved_bytes(cache, len, delalloc);
4402 trace_btrfs_reserved_extent_free(fs_info, start, len);
4404 btrfs_put_block_group(cache);
4408 int btrfs_pin_reserved_extent(struct btrfs_trans_handle *trans, u64 start,
4411 struct btrfs_block_group *cache;
4414 cache = btrfs_lookup_block_group(trans->fs_info, start);
4416 btrfs_err(trans->fs_info, "unable to find block group for %llu",
4421 ret = pin_down_extent(trans, cache, start, len, 1);
4422 btrfs_put_block_group(cache);
4426 static int alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4427 u64 parent, u64 root_objectid,
4428 u64 flags, u64 owner, u64 offset,
4429 struct btrfs_key *ins, int ref_mod)
4431 struct btrfs_fs_info *fs_info = trans->fs_info;
4433 struct btrfs_extent_item *extent_item;
4434 struct btrfs_extent_inline_ref *iref;
4435 struct btrfs_path *path;
4436 struct extent_buffer *leaf;
4441 type = BTRFS_SHARED_DATA_REF_KEY;
4443 type = BTRFS_EXTENT_DATA_REF_KEY;
4445 size = sizeof(*extent_item) + btrfs_extent_inline_ref_size(type);
4447 path = btrfs_alloc_path();
4451 path->leave_spinning = 1;
4452 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4455 btrfs_free_path(path);
4459 leaf = path->nodes[0];
4460 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4461 struct btrfs_extent_item);
4462 btrfs_set_extent_refs(leaf, extent_item, ref_mod);
4463 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4464 btrfs_set_extent_flags(leaf, extent_item,
4465 flags | BTRFS_EXTENT_FLAG_DATA);
4467 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4468 btrfs_set_extent_inline_ref_type(leaf, iref, type);
4470 struct btrfs_shared_data_ref *ref;
4471 ref = (struct btrfs_shared_data_ref *)(iref + 1);
4472 btrfs_set_extent_inline_ref_offset(leaf, iref, parent);
4473 btrfs_set_shared_data_ref_count(leaf, ref, ref_mod);
4475 struct btrfs_extent_data_ref *ref;
4476 ref = (struct btrfs_extent_data_ref *)(&iref->offset);
4477 btrfs_set_extent_data_ref_root(leaf, ref, root_objectid);
4478 btrfs_set_extent_data_ref_objectid(leaf, ref, owner);
4479 btrfs_set_extent_data_ref_offset(leaf, ref, offset);
4480 btrfs_set_extent_data_ref_count(leaf, ref, ref_mod);
4483 btrfs_mark_buffer_dirty(path->nodes[0]);
4484 btrfs_free_path(path);
4486 ret = remove_from_free_space_tree(trans, ins->objectid, ins->offset);
4490 ret = btrfs_update_block_group(trans, ins->objectid, ins->offset, 1);
4491 if (ret) { /* -ENOENT, logic error */
4492 btrfs_err(fs_info, "update block group failed for %llu %llu",
4493 ins->objectid, ins->offset);
4496 trace_btrfs_reserved_extent_alloc(fs_info, ins->objectid, ins->offset);
4500 static int alloc_reserved_tree_block(struct btrfs_trans_handle *trans,
4501 struct btrfs_delayed_ref_node *node,
4502 struct btrfs_delayed_extent_op *extent_op)
4504 struct btrfs_fs_info *fs_info = trans->fs_info;
4506 struct btrfs_extent_item *extent_item;
4507 struct btrfs_key extent_key;
4508 struct btrfs_tree_block_info *block_info;
4509 struct btrfs_extent_inline_ref *iref;
4510 struct btrfs_path *path;
4511 struct extent_buffer *leaf;
4512 struct btrfs_delayed_tree_ref *ref;
4513 u32 size = sizeof(*extent_item) + sizeof(*iref);
4515 u64 flags = extent_op->flags_to_set;
4516 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
4518 ref = btrfs_delayed_node_to_tree_ref(node);
4520 extent_key.objectid = node->bytenr;
4521 if (skinny_metadata) {
4522 extent_key.offset = ref->level;
4523 extent_key.type = BTRFS_METADATA_ITEM_KEY;
4524 num_bytes = fs_info->nodesize;
4526 extent_key.offset = node->num_bytes;
4527 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4528 size += sizeof(*block_info);
4529 num_bytes = node->num_bytes;
4532 path = btrfs_alloc_path();
4536 path->leave_spinning = 1;
4537 ret = btrfs_insert_empty_item(trans, fs_info->extent_root, path,
4540 btrfs_free_path(path);
4544 leaf = path->nodes[0];
4545 extent_item = btrfs_item_ptr(leaf, path->slots[0],
4546 struct btrfs_extent_item);
4547 btrfs_set_extent_refs(leaf, extent_item, 1);
4548 btrfs_set_extent_generation(leaf, extent_item, trans->transid);
4549 btrfs_set_extent_flags(leaf, extent_item,
4550 flags | BTRFS_EXTENT_FLAG_TREE_BLOCK);
4552 if (skinny_metadata) {
4553 iref = (struct btrfs_extent_inline_ref *)(extent_item + 1);
4555 block_info = (struct btrfs_tree_block_info *)(extent_item + 1);
4556 btrfs_set_tree_block_key(leaf, block_info, &extent_op->key);
4557 btrfs_set_tree_block_level(leaf, block_info, ref->level);
4558 iref = (struct btrfs_extent_inline_ref *)(block_info + 1);
4561 if (node->type == BTRFS_SHARED_BLOCK_REF_KEY) {
4562 BUG_ON(!(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF));
4563 btrfs_set_extent_inline_ref_type(leaf, iref,
4564 BTRFS_SHARED_BLOCK_REF_KEY);
4565 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->parent);
4567 btrfs_set_extent_inline_ref_type(leaf, iref,
4568 BTRFS_TREE_BLOCK_REF_KEY);
4569 btrfs_set_extent_inline_ref_offset(leaf, iref, ref->root);
4572 btrfs_mark_buffer_dirty(leaf);
4573 btrfs_free_path(path);
4575 ret = remove_from_free_space_tree(trans, extent_key.objectid,
4580 ret = btrfs_update_block_group(trans, extent_key.objectid,
4581 fs_info->nodesize, 1);
4582 if (ret) { /* -ENOENT, logic error */
4583 btrfs_err(fs_info, "update block group failed for %llu %llu",
4584 extent_key.objectid, extent_key.offset);
4588 trace_btrfs_reserved_extent_alloc(fs_info, extent_key.objectid,
4593 int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
4594 struct btrfs_root *root, u64 owner,
4595 u64 offset, u64 ram_bytes,
4596 struct btrfs_key *ins)
4598 struct btrfs_ref generic_ref = { 0 };
4601 BUG_ON(root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID);
4603 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4604 ins->objectid, ins->offset, 0);
4605 btrfs_init_data_ref(&generic_ref, root->root_key.objectid, owner, offset);
4606 btrfs_ref_tree_mod(root->fs_info, &generic_ref);
4607 ret = btrfs_add_delayed_data_ref(trans, &generic_ref,
4608 ram_bytes, NULL, NULL);
4613 * this is used by the tree logging recovery code. It records that
4614 * an extent has been allocated and makes sure to clear the free
4615 * space cache bits as well
4617 int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
4618 u64 root_objectid, u64 owner, u64 offset,
4619 struct btrfs_key *ins)
4621 struct btrfs_fs_info *fs_info = trans->fs_info;
4623 struct btrfs_block_group *block_group;
4624 struct btrfs_space_info *space_info;
4627 * Mixed block groups will exclude before processing the log so we only
4628 * need to do the exclude dance if this fs isn't mixed.
4630 if (!btrfs_fs_incompat(fs_info, MIXED_GROUPS)) {
4631 ret = __exclude_logged_extent(fs_info, ins->objectid,
4637 block_group = btrfs_lookup_block_group(fs_info, ins->objectid);
4641 space_info = block_group->space_info;
4642 spin_lock(&space_info->lock);
4643 spin_lock(&block_group->lock);
4644 space_info->bytes_reserved += ins->offset;
4645 block_group->reserved += ins->offset;
4646 spin_unlock(&block_group->lock);
4647 spin_unlock(&space_info->lock);
4649 ret = alloc_reserved_file_extent(trans, 0, root_objectid, 0, owner,
4652 btrfs_pin_extent(trans, ins->objectid, ins->offset, 1);
4653 btrfs_put_block_group(block_group);
4657 static struct extent_buffer *
4658 btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
4659 u64 bytenr, int level, u64 owner,
4660 enum btrfs_lock_nesting nest)
4662 struct btrfs_fs_info *fs_info = root->fs_info;
4663 struct extent_buffer *buf;
4665 buf = btrfs_find_create_tree_block(fs_info, bytenr);
4670 * Extra safety check in case the extent tree is corrupted and extent
4671 * allocator chooses to use a tree block which is already used and
4674 if (buf->lock_owner == current->pid) {
4675 btrfs_err_rl(fs_info,
4676 "tree block %llu owner %llu already locked by pid=%d, extent tree corruption detected",
4677 buf->start, btrfs_header_owner(buf), current->pid);
4678 free_extent_buffer(buf);
4679 return ERR_PTR(-EUCLEAN);
4682 btrfs_set_buffer_lockdep_class(owner, buf, level);
4683 __btrfs_tree_lock(buf, nest);
4684 btrfs_clean_tree_block(buf);
4685 clear_bit(EXTENT_BUFFER_STALE, &buf->bflags);
4687 btrfs_set_lock_blocking_write(buf);
4688 set_extent_buffer_uptodate(buf);
4690 memzero_extent_buffer(buf, 0, sizeof(struct btrfs_header));
4691 btrfs_set_header_level(buf, level);
4692 btrfs_set_header_bytenr(buf, buf->start);
4693 btrfs_set_header_generation(buf, trans->transid);
4694 btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
4695 btrfs_set_header_owner(buf, owner);
4696 write_extent_buffer_fsid(buf, fs_info->fs_devices->metadata_uuid);
4697 write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
4698 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
4699 buf->log_index = root->log_transid % 2;
4701 * we allow two log transactions at a time, use different
4702 * EXTENT bit to differentiate dirty pages.
4704 if (buf->log_index == 0)
4705 set_extent_dirty(&root->dirty_log_pages, buf->start,
4706 buf->start + buf->len - 1, GFP_NOFS);
4708 set_extent_new(&root->dirty_log_pages, buf->start,
4709 buf->start + buf->len - 1);
4711 buf->log_index = -1;
4712 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
4713 buf->start + buf->len - 1, GFP_NOFS);
4715 trans->dirty = true;
4716 /* this returns a buffer locked for blocking */
4721 * finds a free extent and does all the dirty work required for allocation
4722 * returns the tree buffer or an ERR_PTR on error.
4724 struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
4725 struct btrfs_root *root,
4726 u64 parent, u64 root_objectid,
4727 const struct btrfs_disk_key *key,
4728 int level, u64 hint,
4730 enum btrfs_lock_nesting nest)
4732 struct btrfs_fs_info *fs_info = root->fs_info;
4733 struct btrfs_key ins;
4734 struct btrfs_block_rsv *block_rsv;
4735 struct extent_buffer *buf;
4736 struct btrfs_delayed_extent_op *extent_op;
4737 struct btrfs_ref generic_ref = { 0 };
4740 u32 blocksize = fs_info->nodesize;
4741 bool skinny_metadata = btrfs_fs_incompat(fs_info, SKINNY_METADATA);
4743 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
4744 if (btrfs_is_testing(fs_info)) {
4745 buf = btrfs_init_new_buffer(trans, root, root->alloc_bytenr,
4746 level, root_objectid, nest);
4748 root->alloc_bytenr += blocksize;
4753 block_rsv = btrfs_use_block_rsv(trans, root, blocksize);
4754 if (IS_ERR(block_rsv))
4755 return ERR_CAST(block_rsv);
4757 ret = btrfs_reserve_extent(root, blocksize, blocksize, blocksize,
4758 empty_size, hint, &ins, 0, 0);
4762 buf = btrfs_init_new_buffer(trans, root, ins.objectid, level,
4763 root_objectid, nest);
4766 goto out_free_reserved;
4769 if (root_objectid == BTRFS_TREE_RELOC_OBJECTID) {
4771 parent = ins.objectid;
4772 flags |= BTRFS_BLOCK_FLAG_FULL_BACKREF;
4776 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
4777 extent_op = btrfs_alloc_delayed_extent_op();
4783 memcpy(&extent_op->key, key, sizeof(extent_op->key));
4785 memset(&extent_op->key, 0, sizeof(extent_op->key));
4786 extent_op->flags_to_set = flags;
4787 extent_op->update_key = skinny_metadata ? false : true;
4788 extent_op->update_flags = true;
4789 extent_op->is_data = false;
4790 extent_op->level = level;
4792 btrfs_init_generic_ref(&generic_ref, BTRFS_ADD_DELAYED_EXTENT,
4793 ins.objectid, ins.offset, parent);
4794 generic_ref.real_root = root->root_key.objectid;
4795 btrfs_init_tree_ref(&generic_ref, level, root_objectid);
4796 btrfs_ref_tree_mod(fs_info, &generic_ref);
4797 ret = btrfs_add_delayed_tree_ref(trans, &generic_ref,
4798 extent_op, NULL, NULL);
4800 goto out_free_delayed;
4805 btrfs_free_delayed_extent_op(extent_op);
4807 free_extent_buffer(buf);
4809 btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 0);
4811 btrfs_unuse_block_rsv(fs_info, block_rsv, blocksize);
4812 return ERR_PTR(ret);
4815 struct walk_control {
4816 u64 refs[BTRFS_MAX_LEVEL];
4817 u64 flags[BTRFS_MAX_LEVEL];
4818 struct btrfs_key update_progress;
4819 struct btrfs_key drop_progress;
4831 #define DROP_REFERENCE 1
4832 #define UPDATE_BACKREF 2
4834 static noinline void reada_walk_down(struct btrfs_trans_handle *trans,
4835 struct btrfs_root *root,
4836 struct walk_control *wc,
4837 struct btrfs_path *path)
4839 struct btrfs_fs_info *fs_info = root->fs_info;
4845 struct btrfs_key key;
4846 struct extent_buffer *eb;
4851 if (path->slots[wc->level] < wc->reada_slot) {
4852 wc->reada_count = wc->reada_count * 2 / 3;
4853 wc->reada_count = max(wc->reada_count, 2);
4855 wc->reada_count = wc->reada_count * 3 / 2;
4856 wc->reada_count = min_t(int, wc->reada_count,
4857 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
4860 eb = path->nodes[wc->level];
4861 nritems = btrfs_header_nritems(eb);
4863 for (slot = path->slots[wc->level]; slot < nritems; slot++) {
4864 if (nread >= wc->reada_count)
4868 bytenr = btrfs_node_blockptr(eb, slot);
4869 generation = btrfs_node_ptr_generation(eb, slot);
4871 if (slot == path->slots[wc->level])
4874 if (wc->stage == UPDATE_BACKREF &&
4875 generation <= root->root_key.offset)
4878 /* We don't lock the tree block, it's OK to be racy here */
4879 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr,
4880 wc->level - 1, 1, &refs,
4882 /* We don't care about errors in readahead. */
4887 if (wc->stage == DROP_REFERENCE) {
4891 if (wc->level == 1 &&
4892 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4894 if (!wc->update_ref ||
4895 generation <= root->root_key.offset)
4897 btrfs_node_key_to_cpu(eb, &key, slot);
4898 ret = btrfs_comp_cpu_keys(&key,
4899 &wc->update_progress);
4903 if (wc->level == 1 &&
4904 (flags & BTRFS_BLOCK_FLAG_FULL_BACKREF))
4908 readahead_tree_block(fs_info, bytenr);
4911 wc->reada_slot = slot;
4915 * helper to process tree block while walking down the tree.
4917 * when wc->stage == UPDATE_BACKREF, this function updates
4918 * back refs for pointers in the block.
4920 * NOTE: return value 1 means we should stop walking down.
4922 static noinline int walk_down_proc(struct btrfs_trans_handle *trans,
4923 struct btrfs_root *root,
4924 struct btrfs_path *path,
4925 struct walk_control *wc, int lookup_info)
4927 struct btrfs_fs_info *fs_info = root->fs_info;
4928 int level = wc->level;
4929 struct extent_buffer *eb = path->nodes[level];
4930 u64 flag = BTRFS_BLOCK_FLAG_FULL_BACKREF;
4933 if (wc->stage == UPDATE_BACKREF &&
4934 btrfs_header_owner(eb) != root->root_key.objectid)
4938 * when reference count of tree block is 1, it won't increase
4939 * again. once full backref flag is set, we never clear it.
4942 ((wc->stage == DROP_REFERENCE && wc->refs[level] != 1) ||
4943 (wc->stage == UPDATE_BACKREF && !(wc->flags[level] & flag)))) {
4944 BUG_ON(!path->locks[level]);
4945 ret = btrfs_lookup_extent_info(trans, fs_info,
4946 eb->start, level, 1,
4949 BUG_ON(ret == -ENOMEM);
4952 BUG_ON(wc->refs[level] == 0);
4955 if (wc->stage == DROP_REFERENCE) {
4956 if (wc->refs[level] > 1)
4959 if (path->locks[level] && !wc->keep_locks) {
4960 btrfs_tree_unlock_rw(eb, path->locks[level]);
4961 path->locks[level] = 0;
4966 /* wc->stage == UPDATE_BACKREF */
4967 if (!(wc->flags[level] & flag)) {
4968 BUG_ON(!path->locks[level]);
4969 ret = btrfs_inc_ref(trans, root, eb, 1);
4970 BUG_ON(ret); /* -ENOMEM */
4971 ret = btrfs_dec_ref(trans, root, eb, 0);
4972 BUG_ON(ret); /* -ENOMEM */
4973 ret = btrfs_set_disk_extent_flags(trans, eb, flag,
4974 btrfs_header_level(eb), 0);
4975 BUG_ON(ret); /* -ENOMEM */
4976 wc->flags[level] |= flag;
4980 * the block is shared by multiple trees, so it's not good to
4981 * keep the tree lock
4983 if (path->locks[level] && level > 0) {
4984 btrfs_tree_unlock_rw(eb, path->locks[level]);
4985 path->locks[level] = 0;
4991 * This is used to verify a ref exists for this root to deal with a bug where we
4992 * would have a drop_progress key that hadn't been updated properly.
4994 static int check_ref_exists(struct btrfs_trans_handle *trans,
4995 struct btrfs_root *root, u64 bytenr, u64 parent,
4998 struct btrfs_path *path;
4999 struct btrfs_extent_inline_ref *iref;
5002 path = btrfs_alloc_path();
5006 ret = lookup_extent_backref(trans, path, &iref, bytenr,
5007 root->fs_info->nodesize, parent,
5008 root->root_key.objectid, level, 0);
5009 btrfs_free_path(path);
5018 * helper to process tree block pointer.
5020 * when wc->stage == DROP_REFERENCE, this function checks
5021 * reference count of the block pointed to. if the block
5022 * is shared and we need update back refs for the subtree
5023 * rooted at the block, this function changes wc->stage to
5024 * UPDATE_BACKREF. if the block is shared and there is no
5025 * need to update back, this function drops the reference
5028 * NOTE: return value 1 means we should stop walking down.
5030 static noinline int do_walk_down(struct btrfs_trans_handle *trans,
5031 struct btrfs_root *root,
5032 struct btrfs_path *path,
5033 struct walk_control *wc, int *lookup_info)
5035 struct btrfs_fs_info *fs_info = root->fs_info;
5039 struct btrfs_key key;
5040 struct btrfs_key first_key;
5041 struct btrfs_ref ref = { 0 };
5042 struct extent_buffer *next;
5043 int level = wc->level;
5046 bool need_account = false;
5048 generation = btrfs_node_ptr_generation(path->nodes[level],
5049 path->slots[level]);
5051 * if the lower level block was created before the snapshot
5052 * was created, we know there is no need to update back refs
5055 if (wc->stage == UPDATE_BACKREF &&
5056 generation <= root->root_key.offset) {
5061 bytenr = btrfs_node_blockptr(path->nodes[level], path->slots[level]);
5062 btrfs_node_key_to_cpu(path->nodes[level], &first_key,
5063 path->slots[level]);
5065 next = find_extent_buffer(fs_info, bytenr);
5067 next = btrfs_find_create_tree_block(fs_info, bytenr);
5069 return PTR_ERR(next);
5071 btrfs_set_buffer_lockdep_class(root->root_key.objectid, next,
5075 btrfs_tree_lock(next);
5076 btrfs_set_lock_blocking_write(next);
5078 ret = btrfs_lookup_extent_info(trans, fs_info, bytenr, level - 1, 1,
5079 &wc->refs[level - 1],
5080 &wc->flags[level - 1]);
5084 if (unlikely(wc->refs[level - 1] == 0)) {
5085 btrfs_err(fs_info, "Missing references.");
5091 if (wc->stage == DROP_REFERENCE) {
5092 if (wc->refs[level - 1] > 1) {
5093 need_account = true;
5095 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5098 if (!wc->update_ref ||
5099 generation <= root->root_key.offset)
5102 btrfs_node_key_to_cpu(path->nodes[level], &key,
5103 path->slots[level]);
5104 ret = btrfs_comp_cpu_keys(&key, &wc->update_progress);
5108 wc->stage = UPDATE_BACKREF;
5109 wc->shared_level = level - 1;
5113 (wc->flags[0] & BTRFS_BLOCK_FLAG_FULL_BACKREF))
5117 if (!btrfs_buffer_uptodate(next, generation, 0)) {
5118 btrfs_tree_unlock(next);
5119 free_extent_buffer(next);
5125 if (reada && level == 1)
5126 reada_walk_down(trans, root, wc, path);
5127 next = read_tree_block(fs_info, bytenr, generation, level - 1,
5130 return PTR_ERR(next);
5131 } else if (!extent_buffer_uptodate(next)) {
5132 free_extent_buffer(next);
5135 btrfs_tree_lock(next);
5136 btrfs_set_lock_blocking_write(next);
5140 ASSERT(level == btrfs_header_level(next));
5141 if (level != btrfs_header_level(next)) {
5142 btrfs_err(root->fs_info, "mismatched level");
5146 path->nodes[level] = next;
5147 path->slots[level] = 0;
5148 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5154 wc->refs[level - 1] = 0;
5155 wc->flags[level - 1] = 0;
5156 if (wc->stage == DROP_REFERENCE) {
5157 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF) {
5158 parent = path->nodes[level]->start;
5160 ASSERT(root->root_key.objectid ==
5161 btrfs_header_owner(path->nodes[level]));
5162 if (root->root_key.objectid !=
5163 btrfs_header_owner(path->nodes[level])) {
5164 btrfs_err(root->fs_info,
5165 "mismatched block owner");
5173 * If we had a drop_progress we need to verify the refs are set
5174 * as expected. If we find our ref then we know that from here
5175 * on out everything should be correct, and we can clear the
5178 if (wc->restarted) {
5179 ret = check_ref_exists(trans, root, bytenr, parent,
5190 * Reloc tree doesn't contribute to qgroup numbers, and we have
5191 * already accounted them at merge time (replace_path),
5192 * thus we could skip expensive subtree trace here.
5194 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID &&
5196 ret = btrfs_qgroup_trace_subtree(trans, next,
5197 generation, level - 1);
5199 btrfs_err_rl(fs_info,
5200 "Error %d accounting shared subtree. Quota is out of sync, rescan required.",
5206 * We need to update the next key in our walk control so we can
5207 * update the drop_progress key accordingly. We don't care if
5208 * find_next_key doesn't find a key because that means we're at
5209 * the end and are going to clean up now.
5211 wc->drop_level = level;
5212 find_next_key(path, level, &wc->drop_progress);
5214 btrfs_init_generic_ref(&ref, BTRFS_DROP_DELAYED_REF, bytenr,
5215 fs_info->nodesize, parent);
5216 btrfs_init_tree_ref(&ref, level - 1, root->root_key.objectid);
5217 ret = btrfs_free_extent(trans, &ref);
5226 btrfs_tree_unlock(next);
5227 free_extent_buffer(next);
5233 * helper to process tree block while walking up the tree.
5235 * when wc->stage == DROP_REFERENCE, this function drops
5236 * reference count on the block.
5238 * when wc->stage == UPDATE_BACKREF, this function changes
5239 * wc->stage back to DROP_REFERENCE if we changed wc->stage
5240 * to UPDATE_BACKREF previously while processing the block.
5242 * NOTE: return value 1 means we should stop walking up.
5244 static noinline int walk_up_proc(struct btrfs_trans_handle *trans,
5245 struct btrfs_root *root,
5246 struct btrfs_path *path,
5247 struct walk_control *wc)
5249 struct btrfs_fs_info *fs_info = root->fs_info;
5251 int level = wc->level;
5252 struct extent_buffer *eb = path->nodes[level];
5255 if (wc->stage == UPDATE_BACKREF) {
5256 BUG_ON(wc->shared_level < level);
5257 if (level < wc->shared_level)
5260 ret = find_next_key(path, level + 1, &wc->update_progress);
5264 wc->stage = DROP_REFERENCE;
5265 wc->shared_level = -1;
5266 path->slots[level] = 0;
5269 * check reference count again if the block isn't locked.
5270 * we should start walking down the tree again if reference
5273 if (!path->locks[level]) {
5275 btrfs_tree_lock(eb);
5276 btrfs_set_lock_blocking_write(eb);
5277 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5279 ret = btrfs_lookup_extent_info(trans, fs_info,
5280 eb->start, level, 1,
5284 btrfs_tree_unlock_rw(eb, path->locks[level]);
5285 path->locks[level] = 0;
5288 BUG_ON(wc->refs[level] == 0);
5289 if (wc->refs[level] == 1) {
5290 btrfs_tree_unlock_rw(eb, path->locks[level]);
5291 path->locks[level] = 0;
5297 /* wc->stage == DROP_REFERENCE */
5298 BUG_ON(wc->refs[level] > 1 && !path->locks[level]);
5300 if (wc->refs[level] == 1) {
5302 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5303 ret = btrfs_dec_ref(trans, root, eb, 1);
5305 ret = btrfs_dec_ref(trans, root, eb, 0);
5306 BUG_ON(ret); /* -ENOMEM */
5307 if (is_fstree(root->root_key.objectid)) {
5308 ret = btrfs_qgroup_trace_leaf_items(trans, eb);
5310 btrfs_err_rl(fs_info,
5311 "error %d accounting leaf items, quota is out of sync, rescan required",
5316 /* make block locked assertion in btrfs_clean_tree_block happy */
5317 if (!path->locks[level] &&
5318 btrfs_header_generation(eb) == trans->transid) {
5319 btrfs_tree_lock(eb);
5320 btrfs_set_lock_blocking_write(eb);
5321 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5323 btrfs_clean_tree_block(eb);
5326 if (eb == root->node) {
5327 if (wc->flags[level] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5329 else if (root->root_key.objectid != btrfs_header_owner(eb))
5330 goto owner_mismatch;
5332 if (wc->flags[level + 1] & BTRFS_BLOCK_FLAG_FULL_BACKREF)
5333 parent = path->nodes[level + 1]->start;
5334 else if (root->root_key.objectid !=
5335 btrfs_header_owner(path->nodes[level + 1]))
5336 goto owner_mismatch;
5339 btrfs_free_tree_block(trans, root, eb, parent, wc->refs[level] == 1);
5341 wc->refs[level] = 0;
5342 wc->flags[level] = 0;
5346 btrfs_err_rl(fs_info, "unexpected tree owner, have %llu expect %llu",
5347 btrfs_header_owner(eb), root->root_key.objectid);
5351 static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
5352 struct btrfs_root *root,
5353 struct btrfs_path *path,
5354 struct walk_control *wc)
5356 int level = wc->level;
5357 int lookup_info = 1;
5360 while (level >= 0) {
5361 ret = walk_down_proc(trans, root, path, wc, lookup_info);
5368 if (path->slots[level] >=
5369 btrfs_header_nritems(path->nodes[level]))
5372 ret = do_walk_down(trans, root, path, wc, &lookup_info);
5374 path->slots[level]++;
5383 static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
5384 struct btrfs_root *root,
5385 struct btrfs_path *path,
5386 struct walk_control *wc, int max_level)
5388 int level = wc->level;
5391 path->slots[level] = btrfs_header_nritems(path->nodes[level]);
5392 while (level < max_level && path->nodes[level]) {
5394 if (path->slots[level] + 1 <
5395 btrfs_header_nritems(path->nodes[level])) {
5396 path->slots[level]++;
5399 ret = walk_up_proc(trans, root, path, wc);
5405 if (path->locks[level]) {
5406 btrfs_tree_unlock_rw(path->nodes[level],
5407 path->locks[level]);
5408 path->locks[level] = 0;
5410 free_extent_buffer(path->nodes[level]);
5411 path->nodes[level] = NULL;
5419 * drop a subvolume tree.
5421 * this function traverses the tree freeing any blocks that only
5422 * referenced by the tree.
5424 * when a shared tree block is found. this function decreases its
5425 * reference count by one. if update_ref is true, this function
5426 * also make sure backrefs for the shared block and all lower level
5427 * blocks are properly updated.
5429 * If called with for_reloc == 0, may exit early with -EAGAIN
5431 int btrfs_drop_snapshot(struct btrfs_root *root, int update_ref, int for_reloc)
5433 struct btrfs_fs_info *fs_info = root->fs_info;
5434 struct btrfs_path *path;
5435 struct btrfs_trans_handle *trans;
5436 struct btrfs_root *tree_root = fs_info->tree_root;
5437 struct btrfs_root_item *root_item = &root->root_item;
5438 struct walk_control *wc;
5439 struct btrfs_key key;
5443 bool root_dropped = false;
5445 btrfs_debug(fs_info, "Drop subvolume %llu", root->root_key.objectid);
5447 path = btrfs_alloc_path();
5453 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5455 btrfs_free_path(path);
5461 * Use join to avoid potential EINTR from transaction start. See
5462 * wait_reserve_ticket and the whole reservation callchain.
5465 trans = btrfs_join_transaction(tree_root);
5467 trans = btrfs_start_transaction(tree_root, 0);
5468 if (IS_ERR(trans)) {
5469 err = PTR_ERR(trans);
5473 err = btrfs_run_delayed_items(trans);
5478 * This will help us catch people modifying the fs tree while we're
5479 * dropping it. It is unsafe to mess with the fs tree while it's being
5480 * dropped as we unlock the root node and parent nodes as we walk down
5481 * the tree, assuming nothing will change. If something does change
5482 * then we'll have stale information and drop references to blocks we've
5485 set_bit(BTRFS_ROOT_DELETING, &root->state);
5486 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
5487 level = btrfs_header_level(root->node);
5488 path->nodes[level] = btrfs_lock_root_node(root);
5489 btrfs_set_lock_blocking_write(path->nodes[level]);
5490 path->slots[level] = 0;
5491 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5492 memset(&wc->update_progress, 0,
5493 sizeof(wc->update_progress));
5495 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
5496 memcpy(&wc->update_progress, &key,
5497 sizeof(wc->update_progress));
5499 level = root_item->drop_level;
5501 path->lowest_level = level;
5502 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5503 path->lowest_level = 0;
5511 * unlock our path, this is safe because only this
5512 * function is allowed to delete this snapshot
5514 btrfs_unlock_up_safe(path, 0);
5516 level = btrfs_header_level(root->node);
5518 btrfs_tree_lock(path->nodes[level]);
5519 btrfs_set_lock_blocking_write(path->nodes[level]);
5520 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5522 ret = btrfs_lookup_extent_info(trans, fs_info,
5523 path->nodes[level]->start,
5524 level, 1, &wc->refs[level],
5530 BUG_ON(wc->refs[level] == 0);
5532 if (level == root_item->drop_level)
5535 btrfs_tree_unlock(path->nodes[level]);
5536 path->locks[level] = 0;
5537 WARN_ON(wc->refs[level] != 1);
5542 wc->restarted = test_bit(BTRFS_ROOT_DEAD_TREE, &root->state);
5544 wc->shared_level = -1;
5545 wc->stage = DROP_REFERENCE;
5546 wc->update_ref = update_ref;
5548 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
5552 ret = walk_down_tree(trans, root, path, wc);
5558 ret = walk_up_tree(trans, root, path, wc, BTRFS_MAX_LEVEL);
5565 BUG_ON(wc->stage != DROP_REFERENCE);
5569 if (wc->stage == DROP_REFERENCE) {
5570 wc->drop_level = wc->level;
5571 btrfs_node_key_to_cpu(path->nodes[wc->drop_level],
5573 path->slots[wc->drop_level]);
5575 btrfs_cpu_key_to_disk(&root_item->drop_progress,
5576 &wc->drop_progress);
5577 root_item->drop_level = wc->drop_level;
5579 BUG_ON(wc->level == 0);
5580 if (btrfs_should_end_transaction(trans) ||
5581 (!for_reloc && btrfs_need_cleaner_sleep(fs_info))) {
5582 ret = btrfs_update_root(trans, tree_root,
5586 btrfs_abort_transaction(trans, ret);
5591 btrfs_end_transaction_throttle(trans);
5592 if (!for_reloc && btrfs_need_cleaner_sleep(fs_info)) {
5593 btrfs_debug(fs_info,
5594 "drop snapshot early exit");
5599 trans = btrfs_start_transaction(tree_root, 0);
5600 if (IS_ERR(trans)) {
5601 err = PTR_ERR(trans);
5606 btrfs_release_path(path);
5610 ret = btrfs_del_root(trans, &root->root_key);
5612 btrfs_abort_transaction(trans, ret);
5617 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
5618 ret = btrfs_find_root(tree_root, &root->root_key, path,
5621 btrfs_abort_transaction(trans, ret);
5624 } else if (ret > 0) {
5625 /* if we fail to delete the orphan item this time
5626 * around, it'll get picked up the next time.
5628 * The most common failure here is just -ENOENT.
5630 btrfs_del_orphan_item(trans, tree_root,
5631 root->root_key.objectid);
5636 * This subvolume is going to be completely dropped, and won't be
5637 * recorded as dirty roots, thus pertrans meta rsv will not be freed at
5638 * commit transaction time. So free it here manually.
5640 btrfs_qgroup_convert_reserved_meta(root, INT_MAX);
5641 btrfs_qgroup_free_meta_all_pertrans(root);
5643 if (test_bit(BTRFS_ROOT_IN_RADIX, &root->state))
5644 btrfs_add_dropped_root(trans, root);
5646 btrfs_put_root(root);
5647 root_dropped = true;
5649 btrfs_end_transaction_throttle(trans);
5652 btrfs_free_path(path);
5655 * So if we need to stop dropping the snapshot for whatever reason we
5656 * need to make sure to add it back to the dead root list so that we
5657 * keep trying to do the work later. This also cleans up roots if we
5658 * don't have it in the radix (like when we recover after a power fail
5659 * or unmount) so we don't leak memory.
5661 if (!for_reloc && !root_dropped)
5662 btrfs_add_dead_root(root);
5667 * drop subtree rooted at tree block 'node'.
5669 * NOTE: this function will unlock and release tree block 'node'
5670 * only used by relocation code
5672 int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
5673 struct btrfs_root *root,
5674 struct extent_buffer *node,
5675 struct extent_buffer *parent)
5677 struct btrfs_fs_info *fs_info = root->fs_info;
5678 struct btrfs_path *path;
5679 struct walk_control *wc;
5685 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5687 path = btrfs_alloc_path();
5691 wc = kzalloc(sizeof(*wc), GFP_NOFS);
5693 btrfs_free_path(path);
5697 btrfs_assert_tree_locked(parent);
5698 parent_level = btrfs_header_level(parent);
5699 atomic_inc(&parent->refs);
5700 path->nodes[parent_level] = parent;
5701 path->slots[parent_level] = btrfs_header_nritems(parent);
5703 btrfs_assert_tree_locked(node);
5704 level = btrfs_header_level(node);
5705 path->nodes[level] = node;
5706 path->slots[level] = 0;
5707 path->locks[level] = BTRFS_WRITE_LOCK_BLOCKING;
5709 wc->refs[parent_level] = 1;
5710 wc->flags[parent_level] = BTRFS_BLOCK_FLAG_FULL_BACKREF;
5712 wc->shared_level = -1;
5713 wc->stage = DROP_REFERENCE;
5716 wc->reada_count = BTRFS_NODEPTRS_PER_BLOCK(fs_info);
5719 wret = walk_down_tree(trans, root, path, wc);
5725 wret = walk_up_tree(trans, root, path, wc, parent_level);
5733 btrfs_free_path(path);
5738 * helper to account the unused space of all the readonly block group in the
5739 * space_info. takes mirrors into account.
5741 u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo)
5743 struct btrfs_block_group *block_group;
5747 /* It's df, we don't care if it's racy */
5748 if (list_empty(&sinfo->ro_bgs))
5751 spin_lock(&sinfo->lock);
5752 list_for_each_entry(block_group, &sinfo->ro_bgs, ro_list) {
5753 spin_lock(&block_group->lock);
5755 if (!block_group->ro) {
5756 spin_unlock(&block_group->lock);
5760 factor = btrfs_bg_type_to_factor(block_group->flags);
5761 free_bytes += (block_group->length -
5762 block_group->used) * factor;
5764 spin_unlock(&block_group->lock);
5766 spin_unlock(&sinfo->lock);
5771 int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
5774 return unpin_extent_range(fs_info, start, end, false);
5778 * It used to be that old block groups would be left around forever.
5779 * Iterating over them would be enough to trim unused space. Since we
5780 * now automatically remove them, we also need to iterate over unallocated
5783 * We don't want a transaction for this since the discard may take a
5784 * substantial amount of time. We don't require that a transaction be
5785 * running, but we do need to take a running transaction into account
5786 * to ensure that we're not discarding chunks that were released or
5787 * allocated in the current transaction.
5789 * Holding the chunks lock will prevent other threads from allocating
5790 * or releasing chunks, but it won't prevent a running transaction
5791 * from committing and releasing the memory that the pending chunks
5792 * list head uses. For that, we need to take a reference to the
5793 * transaction and hold the commit root sem. We only need to hold
5794 * it while performing the free space search since we have already
5795 * held back allocations.
5797 static int btrfs_trim_free_extents(struct btrfs_device *device, u64 *trimmed)
5799 u64 start = SZ_1M, len = 0, end = 0;
5804 /* Discard not supported = nothing to do. */
5805 if (!blk_queue_discard(bdev_get_queue(device->bdev)))
5808 /* Not writable = nothing to do. */
5809 if (!test_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state))
5812 /* No free space = nothing to do. */
5813 if (device->total_bytes <= device->bytes_used)
5819 struct btrfs_fs_info *fs_info = device->fs_info;
5822 ret = mutex_lock_interruptible(&fs_info->chunk_mutex);
5826 find_first_clear_extent_bit(&device->alloc_state, start,
5828 CHUNK_TRIMMED | CHUNK_ALLOCATED);
5830 /* Check if there are any CHUNK_* bits left */
5831 if (start > device->total_bytes) {
5832 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
5833 btrfs_warn_in_rcu(fs_info,
5834 "ignoring attempt to trim beyond device size: offset %llu length %llu device %s device size %llu",
5835 start, end - start + 1,
5836 rcu_str_deref(device->name),
5837 device->total_bytes);
5838 mutex_unlock(&fs_info->chunk_mutex);
5843 /* Ensure we skip the reserved area in the first 1M */
5844 start = max_t(u64, start, SZ_1M);
5847 * If find_first_clear_extent_bit find a range that spans the
5848 * end of the device it will set end to -1, in this case it's up
5849 * to the caller to trim the value to the size of the device.
5851 end = min(end, device->total_bytes - 1);
5853 len = end - start + 1;
5855 /* We didn't find any extents */
5857 mutex_unlock(&fs_info->chunk_mutex);
5862 ret = btrfs_issue_discard(device->bdev, start, len,
5865 set_extent_bits(&device->alloc_state, start,
5868 mutex_unlock(&fs_info->chunk_mutex);
5876 if (fatal_signal_pending(current)) {
5888 * Trim the whole filesystem by:
5889 * 1) trimming the free space in each block group
5890 * 2) trimming the unallocated space on each device
5892 * This will also continue trimming even if a block group or device encounters
5893 * an error. The return value will be the last error, or 0 if nothing bad
5896 int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range)
5898 struct btrfs_block_group *cache = NULL;
5899 struct btrfs_device *device;
5900 struct list_head *devices;
5902 u64 range_end = U64_MAX;
5913 * Check range overflow if range->len is set.
5914 * The default range->len is U64_MAX.
5916 if (range->len != U64_MAX &&
5917 check_add_overflow(range->start, range->len, &range_end))
5920 cache = btrfs_lookup_first_block_group(fs_info, range->start);
5921 for (; cache; cache = btrfs_next_block_group(cache)) {
5922 if (cache->start >= range_end) {
5923 btrfs_put_block_group(cache);
5927 start = max(range->start, cache->start);
5928 end = min(range_end, cache->start + cache->length);
5930 if (end - start >= range->minlen) {
5931 if (!btrfs_block_group_done(cache)) {
5932 ret = btrfs_cache_block_group(cache, 0);
5938 ret = btrfs_wait_block_group_cache_done(cache);
5945 ret = btrfs_trim_block_group(cache,
5951 trimmed += group_trimmed;
5962 "failed to trim %llu block group(s), last error %d",
5964 mutex_lock(&fs_info->fs_devices->device_list_mutex);
5965 devices = &fs_info->fs_devices->devices;
5966 list_for_each_entry(device, devices, dev_list) {
5967 ret = btrfs_trim_free_extents(device, &group_trimmed);
5974 trimmed += group_trimmed;
5976 mutex_unlock(&fs_info->fs_devices->device_list_mutex);
5980 "failed to trim %llu device(s), last error %d",
5981 dev_failed, dev_ret);
5982 range->len = trimmed;